Commit | Line | Data |
---|---|---|
a11e19b7 GE |
1 | /*************************************************************************** |
2 | * Copyright (C) 2006 by Gerd v. Egidy * | |
3 | * gve@intra2net.com * | |
4 | * * | |
5 | * This library is free software; you can redistribute it and/or modify * | |
6 | * it under the terms of the GNU Lesser General Public License version * | |
7 | * 2.1 as published by the Free Software Foundation. * | |
8 | * * | |
9 | * This library is distributed in the hope that it will be useful, * | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | |
12 | * GNU Lesser General Public License for more details. * | |
13 | * * | |
14 | * You should have received a copy of the GNU Lesser General Public * | |
15 | * License along with this program; if not, write to the * | |
16 | * Free Software Foundation, Inc., * | |
17 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * | |
18 | ***************************************************************************/ | |
19 | ||
20 | #include <stdio.h> | |
21 | #include <errno.h> | |
22 | #include <stdlib.h> | |
23 | #include <unistd.h> | |
24 | #include <sys/types.h> | |
25 | #include <sys/socket.h> | |
26 | #include <sys/un.h> | |
27 | #include <sys/time.h> | |
28 | #include <arpa/inet.h> | |
29 | #include <netinet/in.h> | |
30 | #include <netdb.h> | |
31 | #include <fcntl.h> | |
32 | #include <time.h> | |
33 | #include <pwd.h> | |
34 | #include <grp.h> | |
35 | ||
36 | #include <sstream> | |
644c4d26 | 37 | #include <iostream> |
a11e19b7 GE |
38 | |
39 | #include "socket_handler.hxx" | |
40 | #include "t2n_exception.hxx" | |
a7170401 | 41 | #include "log.hxx" |
a11e19b7 GE |
42 | |
43 | using namespace std; | |
44 | ||
45 | namespace libt2n | |
46 | { | |
47 | ||
94247295 | 48 | /// set options like fast reuse and keepalive every socket should have |
a11e19b7 GE |
49 | void socket_handler::set_socket_options(int sock) |
50 | { | |
51 | int i=1; | |
52 | ||
53 | /* fast reuse enable */ | |
54 | if (setsockopt(sock,SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i)) < 0) | |
a7170401 | 55 | EXCEPTIONSTREAM(error,t2n_communication_error,"error setting socket option: " << strerror(errno)); |
a11e19b7 GE |
56 | |
57 | /* keepalive enable */ | |
58 | if (setsockopt(sock,SOL_SOCKET, SO_KEEPALIVE, &i, sizeof(i)) < 0) | |
a7170401 | 59 | EXCEPTIONSTREAM(error,t2n_communication_error,"error setting socket option: " << strerror(errno)); |
a11e19b7 GE |
60 | |
61 | /* close on exec */ | |
62 | int fdflags; | |
63 | fdflags=fcntl(sock,F_GETFD, 0); | |
64 | if (fdflags < 0) | |
a7170401 GE |
65 | EXCEPTIONSTREAM(error,t2n_communication_error,"fcntl error on socket: " << strerror(errno)); |
66 | ||
a11e19b7 GE |
67 | fdflags |= FD_CLOEXEC; |
68 | if (fcntl(sock,F_SETFD,fdflags) < 0) | |
a7170401 | 69 | EXCEPTIONSTREAM(error,t2n_communication_error,"fcntl error on socket: " << strerror(errno)); |
a11e19b7 GE |
70 | |
71 | /* non-blocking mode */ | |
72 | int flflags; | |
73 | flflags=fcntl(sock,F_GETFL,0); | |
74 | if (flflags < 0) | |
a7170401 GE |
75 | EXCEPTIONSTREAM(error,t2n_communication_error,"fcntl error on socket: " << strerror(errno)); |
76 | ||
a11e19b7 GE |
77 | flflags |= O_NONBLOCK; |
78 | if (fcntl(sock,F_SETFL,flflags) < 0) | |
a7170401 | 79 | EXCEPTIONSTREAM(error,t2n_communication_error,"fcntl error on socket: " << strerror(errno)); |
a11e19b7 GE |
80 | } |
81 | ||
94247295 GE |
82 | /// close the underlying socket connection. Don't call directly, use the version provided |
83 | /// by the connection class you are using. | |
a11e19b7 GE |
84 | void socket_handler::close() |
85 | { | |
644c4d26 GE |
86 | // graceful shutdown |
87 | shutdown(sock,SHUT_RDWR); | |
a11e19b7 GE |
88 | ::close(sock); |
89 | } | |
90 | ||
94247295 | 91 | /// is the underlying socket connection still open? |
644c4d26 GE |
92 | bool socket_handler::is_closed() |
93 | { | |
94 | int r=fcntl(sock,F_GETFL); | |
95 | ||
96 | return !(r & O_ACCMODE); | |
97 | } | |
98 | ||
94247295 | 99 | /** @brief check if new data is waiting on the raw socket |
45a2ebc9 | 100 | @param[in,out] usec_timeout wait until new data is found, max timeout usecs. |
94247295 | 101 | -1: wait endless |
45a2ebc9 | 102 | 0: return instantly |
94247295 | 103 | */ |
45a2ebc9 | 104 | bool socket_handler::data_waiting(long long usec_timeout,long long* usec_timeout_remaining) |
a11e19b7 GE |
105 | { |
106 | // just our socket | |
107 | fd_set active_fd_set; | |
108 | FD_ZERO (&active_fd_set); | |
109 | FD_SET (sock, &active_fd_set); | |
110 | ||
111 | /* set timeout */ | |
112 | struct timeval tval; | |
113 | struct timeval *timeout_ptr; | |
114 | ||
115 | if (usec_timeout == -1) | |
116 | timeout_ptr = NULL; | |
117 | else | |
118 | { | |
119 | timeout_ptr = &tval; | |
120 | ||
45a2ebc9 | 121 | // convert timeout from long long usec to int sec + int usec |
a11e19b7 GE |
122 | tval.tv_sec = usec_timeout / 1000000; |
123 | tval.tv_usec = usec_timeout % 1000000; | |
124 | } | |
125 | ||
45a2ebc9 GE |
126 | int ret=select (FD_SETSIZE, &active_fd_set, NULL, NULL, timeout_ptr); |
127 | ||
128 | // return the timeout we did not use | |
6f6d24c0 | 129 | // todo: this is linux specific according to man 2 select |
45a2ebc9 GE |
130 | if (usec_timeout > 0 && usec_timeout_remaining != NULL) |
131 | *usec_timeout_remaining=(tval.tv_sec*1000000)+tval.tv_usec; | |
132 | ||
133 | if (ret > 0) | |
a11e19b7 GE |
134 | return true; |
135 | else | |
136 | return false; | |
137 | } | |
138 | ||
94247295 GE |
139 | /** @brief read data from the raw socket and copy it into the provided buffer |
140 | @param buffer the buffer where to append the new data | |
45a2ebc9 | 141 | @param[in,out] usec_timeout wait until new data is found, max timeout usecs. |
94247295 | 142 | -1: wait endless |
45a2ebc9 | 143 | 0: return instantly |
94247295 | 144 | */ |
45a2ebc9 | 145 | bool socket_handler::fill_buffer(std::string& buffer, long long usec_timeout, long long *timeout_remaining) |
a11e19b7 | 146 | { |
07e98688 | 147 | // fast path for timeout==0 |
45a2ebc9 | 148 | if (usec_timeout==0 || data_waiting(usec_timeout,timeout_remaining)) |
a11e19b7 GE |
149 | return fill_buffer(buffer); |
150 | else | |
151 | return false; | |
152 | } | |
153 | ||
94247295 GE |
154 | /** @brief read data from the raw socket and copy it into the provided buffer. Returns |
155 | instantly if no data is waiting. | |
156 | @param buffer the buffer where to append the new data | |
157 | */ | |
a11e19b7 GE |
158 | bool socket_handler::fill_buffer(std::string& buffer) |
159 | { | |
160 | bool try_again=false; | |
161 | ||
162 | char socket_buffer[recv_buffer_size]; | |
163 | ||
164 | int nbytes = read (sock, socket_buffer, recv_buffer_size); | |
165 | if (nbytes < 0) | |
166 | { | |
167 | if (errno == EAGAIN) | |
168 | return false; // no data was waiting | |
169 | else if (errno == EINTR) | |
170 | { | |
171 | // interrupted, try again | |
a7170401 | 172 | LOGSTREAM(debug,"EINTR received on read(), trying again"); |
a11e19b7 GE |
173 | try_again=true; |
174 | } | |
175 | else | |
176 | { | |
a7170401 | 177 | LOGSTREAM(error,"error reading from socket : " << strerror(errno)); |
a11e19b7 GE |
178 | // TODO: exception? |
179 | return false; | |
180 | } | |
181 | } | |
182 | ||
183 | // End-of-file | |
184 | if (nbytes == 0 && !try_again) | |
185 | { | |
a7170401 | 186 | LOGSTREAM(debug,"0 bytes received on read(), closing connection"); |
a11e19b7 GE |
187 | close(); |
188 | return false; | |
189 | } | |
190 | ||
191 | // Data read -> store it | |
192 | if (nbytes > 0) | |
a7170401 | 193 | { |
58b327c6 | 194 | buffer.append(socket_buffer,nbytes); |
d535333f | 195 | LOGSTREAM(debug,nbytes << " bytes read"); |
a7170401 | 196 | } |
a11e19b7 GE |
197 | |
198 | // more data waiting -> recurse | |
07e98688 | 199 | if (data_waiting(0)) |
a11e19b7 GE |
200 | fill_buffer(buffer); |
201 | ||
202 | if (nbytes > 0) | |
203 | return true; | |
204 | else | |
205 | return false; | |
206 | } | |
207 | ||
94247295 GE |
208 | /// writes raw data to the socket. Don't use directly, use the write() function provided by the |
209 | /// connection because it encapsulates the data. | |
644c4d26 | 210 | void socket_handler::socket_write(const std::string& data) |
a11e19b7 GE |
211 | { |
212 | int offset = 0; | |
213 | while (offset < data.size()) | |
214 | { | |
215 | unsigned int write_size=write_block_size; | |
216 | ||
217 | if (offset+write_size > data.size()) | |
218 | write_size = data.size()-offset; | |
219 | ||
220 | int rtn; | |
221 | while ((rtn=::write(sock, data.data()+offset, write_size)) && | |
222 | rtn == -1 && (errno == EAGAIN || errno == EINTR)) | |
223 | { | |
224 | usleep (80000); | |
a7170401 | 225 | LOGSTREAM(debug,"resuming write() call after EAGAIN or EINTR"); |
a11e19b7 GE |
226 | } |
227 | ||
228 | if (rtn == -1) | |
229 | { | |
a7170401 | 230 | LOGSTREAM(error,"write() returned " << strerror(errno)); |
a11e19b7 GE |
231 | // TODO: exception? |
232 | return; | |
233 | } | |
234 | else if (rtn != write_size) | |
235 | { | |
a7170401 GE |
236 | LOGSTREAM(error,"write() wrote " << rtn << " bytes, should have been " |
237 | << write_size << " (complete: " << data.size() << ")"); | |
a11e19b7 GE |
238 | |
239 | // TODO: exception? | |
240 | return; | |
241 | } | |
242 | ||
243 | offset += write_size; | |
244 | } | |
245 | ||
a7170401 GE |
246 | LOGSTREAM(debug,"wrote " << data.size() << " bytes"); |
247 | ||
a11e19b7 GE |
248 | return; |
249 | } | |
250 | ||
251 | } |