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> |
517d1214 | 38 | #include <algorithm> |
a11e19b7 GE |
39 | |
40 | #include "socket_handler.hxx" | |
41 | #include "t2n_exception.hxx" | |
a7170401 | 42 | #include "log.hxx" |
a11e19b7 GE |
43 | |
44 | using namespace std; | |
45 | ||
46 | namespace libt2n | |
47 | { | |
48 | ||
517d1214 RP |
49 | socket_handler::socket_handler(int _sock, socket_type_value _socket_type) |
50 | : sock(_sock) | |
51 | , recv_buffer_size( default_recv_buffer_size ) | |
52 | , write_block_size( default_write_block_size ) | |
c7857475 | 53 | , write_timeout( default_write_timeout ) |
517d1214 RP |
54 | , socket_type(_socket_type) |
55 | { | |
56 | } | |
57 | ||
56f3994d TJ |
58 | /** |
59 | * Destructor. Closes open socket | |
60 | */ | |
61 | socket_handler::~socket_handler() | |
62 | { | |
63 | if (sock != -1) | |
64 | { | |
65 | shutdown(sock,SHUT_RDWR); | |
66 | ::close(sock); | |
67 | ||
68 | sock = -1; | |
69 | } | |
70 | } | |
71 | ||
72 | /// close the underlying socket connection. Don't call directly, use the version provided | |
73 | /// by the connection class you are using. | |
74 | void socket_handler::close() | |
75 | { | |
76 | LOGSTREAM(debug,"close connection"); | |
77 | // graceful shutdown | |
78 | shutdown(sock,SHUT_RDWR); | |
79 | ::close(sock); | |
80 | ||
81 | sock = -1; | |
82 | } | |
517d1214 | 83 | |
94247295 | 84 | /// set options like fast reuse and keepalive every socket should have |
a11e19b7 GE |
85 | void socket_handler::set_socket_options(int sock) |
86 | { | |
87 | int i=1; | |
88 | ||
89 | /* fast reuse enable */ | |
90 | if (setsockopt(sock,SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i)) < 0) | |
a7170401 | 91 | EXCEPTIONSTREAM(error,t2n_communication_error,"error setting socket option: " << strerror(errno)); |
a11e19b7 GE |
92 | |
93 | /* keepalive enable */ | |
94 | if (setsockopt(sock,SOL_SOCKET, SO_KEEPALIVE, &i, sizeof(i)) < 0) | |
a7170401 | 95 | EXCEPTIONSTREAM(error,t2n_communication_error,"error setting socket option: " << strerror(errno)); |
a11e19b7 GE |
96 | |
97 | /* close on exec */ | |
98 | int fdflags; | |
99 | fdflags=fcntl(sock,F_GETFD, 0); | |
100 | if (fdflags < 0) | |
a7170401 GE |
101 | EXCEPTIONSTREAM(error,t2n_communication_error,"fcntl error on socket: " << strerror(errno)); |
102 | ||
a11e19b7 GE |
103 | fdflags |= FD_CLOEXEC; |
104 | if (fcntl(sock,F_SETFD,fdflags) < 0) | |
a7170401 | 105 | EXCEPTIONSTREAM(error,t2n_communication_error,"fcntl error on socket: " << strerror(errno)); |
a11e19b7 GE |
106 | |
107 | /* non-blocking mode */ | |
108 | int flflags; | |
109 | flflags=fcntl(sock,F_GETFL,0); | |
110 | if (flflags < 0) | |
a7170401 GE |
111 | EXCEPTIONSTREAM(error,t2n_communication_error,"fcntl error on socket: " << strerror(errno)); |
112 | ||
a11e19b7 GE |
113 | flflags |= O_NONBLOCK; |
114 | if (fcntl(sock,F_SETFL,flflags) < 0) | |
a7170401 | 115 | EXCEPTIONSTREAM(error,t2n_communication_error,"fcntl error on socket: " << strerror(errno)); |
a11e19b7 GE |
116 | } |
117 | ||
94247295 | 118 | /// is the underlying socket connection still open? |
644c4d26 GE |
119 | bool socket_handler::is_closed() |
120 | { | |
121 | int r=fcntl(sock,F_GETFL); | |
122 | ||
123 | return !(r & O_ACCMODE); | |
124 | } | |
125 | ||
517d1214 RP |
126 | |
127 | /** | |
128 | * @brief set a new size for the receive buffer. | |
129 | * @param new_recv_buffer_size the new size for the receive buffer. | |
130 | * | |
131 | * The receive buffer determines the amount of data which is tried to read at once | |
132 | * from the underlying socket. | |
133 | * | |
134 | * The value is normalized to be at least 512 bytes and at max 32K bytes. | |
135 | */ | |
136 | void socket_handler::set_recv_buffer_size(unsigned int new_recv_buffer_size) | |
137 | { | |
138 | recv_buffer_size= std::max( 512u, std::min( 32u * 1024u, new_recv_buffer_size )); | |
139 | } // | |
140 | ||
141 | ||
142 | /** | |
143 | * @brief set new size for the data chunks when writeing. | |
144 | * @param new_write_block_size the new chunk size. | |
145 | * | |
c7857475 | 146 | * The write block size determines the amount of data which is tried to write |
517d1214 | 147 | * to the socket when data needs to be sended. |
c7857475 | 148 | * Since writing data is done in a loop, this does not limit the amunt of data which can |
517d1214 RP |
149 | * be written. |
150 | * | |
151 | * The value is normalized to be at least 512 bytes and at max 32K bytes. | |
152 | */ | |
153 | void socket_handler::set_write_block_size(unsigned int new_write_block_size) | |
154 | { | |
155 | write_block_size= std::max( 512u, std::min( 32u * 1024u, new_write_block_size )); | |
156 | } // | |
157 | ||
158 | ||
c7857475 GE |
159 | /** |
160 | * @brief set new timeout for writing a block | |
161 | * @param new_write_timeout the new timeout in usecs, -1: wait endless | |
162 | * | |
163 | * The write timeout determines the maximum amount of time that is waited | |
164 | * between writing each block. If the timeout is exceeded, write will | |
165 | * throw t2n_transfer_error | |
166 | */ | |
167 | void socket_handler::set_write_timeout(long long new_write_timeout) | |
168 | { | |
169 | write_block_size=new_write_timeout; | |
170 | } // | |
171 | ||
172 | ||
94247295 | 173 | /** @brief check if new data is waiting on the raw socket |
45a2ebc9 | 174 | @param[in,out] usec_timeout wait until new data is found, max timeout usecs. |
94247295 | 175 | -1: wait endless |
45a2ebc9 | 176 | 0: return instantly |
9a5d7790 | 177 | @param[out] usec_timeout_remaining microseconds from the timeout that were not used |
94247295 | 178 | */ |
45a2ebc9 | 179 | bool socket_handler::data_waiting(long long usec_timeout,long long* usec_timeout_remaining) |
a11e19b7 GE |
180 | { |
181 | // just our socket | |
182 | fd_set active_fd_set; | |
183 | FD_ZERO (&active_fd_set); | |
184 | FD_SET (sock, &active_fd_set); | |
185 | ||
186 | /* set timeout */ | |
187 | struct timeval tval; | |
188 | struct timeval *timeout_ptr; | |
189 | ||
190 | if (usec_timeout == -1) | |
191 | timeout_ptr = NULL; | |
192 | else | |
193 | { | |
194 | timeout_ptr = &tval; | |
195 | ||
45a2ebc9 | 196 | // convert timeout from long long usec to int sec + int usec |
a11e19b7 GE |
197 | tval.tv_sec = usec_timeout / 1000000; |
198 | tval.tv_usec = usec_timeout % 1000000; | |
199 | } | |
200 | ||
45a2ebc9 GE |
201 | int ret=select (FD_SETSIZE, &active_fd_set, NULL, NULL, timeout_ptr); |
202 | ||
203 | // return the timeout we did not use | |
6f6d24c0 | 204 | // todo: this is linux specific according to man 2 select |
45a2ebc9 GE |
205 | if (usec_timeout > 0 && usec_timeout_remaining != NULL) |
206 | *usec_timeout_remaining=(tval.tv_sec*1000000)+tval.tv_usec; | |
207 | ||
208 | if (ret > 0) | |
a11e19b7 GE |
209 | return true; |
210 | else | |
211 | return false; | |
212 | } | |
213 | ||
94247295 GE |
214 | /** @brief read data from the raw socket and copy it into the provided buffer |
215 | @param buffer the buffer where to append the new data | |
45a2ebc9 | 216 | @param[in,out] usec_timeout wait until new data is found, max timeout usecs. |
94247295 | 217 | -1: wait endless |
45a2ebc9 | 218 | 0: return instantly |
9a5d7790 | 219 | @param[out] usec_timeout_remaining microseconds from the timeout that were not used |
94247295 | 220 | */ |
9a5d7790 | 221 | bool socket_handler::fill_buffer(std::string& buffer, long long usec_timeout, long long *usec_timeout_remaining) |
a11e19b7 | 222 | { |
07e98688 | 223 | // fast path for timeout==0 |
9a5d7790 | 224 | if (usec_timeout==0 || data_waiting(usec_timeout,usec_timeout_remaining)) |
a11e19b7 GE |
225 | return fill_buffer(buffer); |
226 | else | |
227 | return false; | |
228 | } | |
229 | ||
94247295 GE |
230 | /** @brief read data from the raw socket and copy it into the provided buffer. Returns |
231 | instantly if no data is waiting. | |
232 | @param buffer the buffer where to append the new data | |
233 | */ | |
a11e19b7 GE |
234 | bool socket_handler::fill_buffer(std::string& buffer) |
235 | { | |
236 | bool try_again=false; | |
237 | ||
238 | char socket_buffer[recv_buffer_size]; | |
239 | ||
240 | int nbytes = read (sock, socket_buffer, recv_buffer_size); | |
fb3345ad | 241 | |
a11e19b7 GE |
242 | if (nbytes < 0) |
243 | { | |
244 | if (errno == EAGAIN) | |
245 | return false; // no data was waiting | |
246 | else if (errno == EINTR) | |
247 | { | |
248 | // interrupted, try again | |
a7170401 | 249 | LOGSTREAM(debug,"EINTR received on read(), trying again"); |
a11e19b7 GE |
250 | try_again=true; |
251 | } | |
252 | else | |
c7857475 | 253 | EXCEPTIONSTREAM(error,t2n_transfer_error,"error reading from socket : " << strerror(errno)); |
a11e19b7 GE |
254 | } |
255 | ||
256 | // End-of-file | |
257 | if (nbytes == 0 && !try_again) | |
258 | { | |
a7170401 | 259 | LOGSTREAM(debug,"0 bytes received on read(), closing connection"); |
a11e19b7 GE |
260 | close(); |
261 | return false; | |
262 | } | |
263 | ||
264 | // Data read -> store it | |
265 | if (nbytes > 0) | |
a7170401 | 266 | { |
58b327c6 | 267 | buffer.append(socket_buffer,nbytes); |
d535333f | 268 | LOGSTREAM(debug,nbytes << " bytes read"); |
a7170401 | 269 | } |
a11e19b7 GE |
270 | |
271 | // more data waiting -> recurse | |
07e98688 | 272 | if (data_waiting(0)) |
a11e19b7 GE |
273 | fill_buffer(buffer); |
274 | ||
275 | if (nbytes > 0) | |
276 | return true; | |
277 | else | |
278 | return false; | |
279 | } | |
280 | ||
94247295 GE |
281 | /// writes raw data to the socket. Don't use directly, use the write() function provided by the |
282 | /// connection because it encapsulates the data. | |
644c4d26 | 283 | void socket_handler::socket_write(const std::string& data) |
a11e19b7 | 284 | { |
4b995a82 | 285 | unsigned int offset = 0; |
a11e19b7 GE |
286 | while (offset < data.size()) |
287 | { | |
288 | unsigned int write_size=write_block_size; | |
289 | ||
290 | if (offset+write_size > data.size()) | |
291 | write_size = data.size()-offset; | |
292 | ||
293 | int rtn; | |
c7857475 GE |
294 | while ((rtn=::write(sock, data.data()+offset, write_size)) == -1 && |
295 | (errno == EAGAIN || errno == EINTR)) | |
a11e19b7 | 296 | { |
c7857475 | 297 | wait_ready_to_write(sock,write_timeout); |
a7170401 | 298 | LOGSTREAM(debug,"resuming write() call after EAGAIN or EINTR"); |
a11e19b7 GE |
299 | } |
300 | ||
301 | if (rtn == -1) | |
c7857475 | 302 | EXCEPTIONSTREAM(error,t2n_transfer_error,"write() returned " << strerror(errno)); |
a11e19b7 GE |
303 | else if (rtn != write_size) |
304 | { | |
517d1214 | 305 | LOGSTREAM(debug,"write() wrote " << rtn << " bytes, should have been " |
a7170401 | 306 | << write_size << " (complete: " << data.size() << ")"); |
a11e19b7 | 307 | |
517d1214 | 308 | write_size = rtn; |
a11e19b7 GE |
309 | } |
310 | ||
311 | offset += write_size; | |
312 | } | |
313 | ||
a7170401 GE |
314 | LOGSTREAM(debug,"wrote " << data.size() << " bytes"); |
315 | ||
a11e19b7 | 316 | return; |
517d1214 | 317 | } // eo socket_handler::socket_write(const std::string&) |
a11e19b7 | 318 | |
c7857475 GE |
319 | /// wait until the socket is ready to write again |
320 | void socket_handler::wait_ready_to_write(int socket, long long write_block_timeout) | |
321 | { | |
322 | // prepare socket sets | |
323 | fd_set write_set[1]; | |
324 | fd_set except_set[1]; | |
325 | FD_ZERO(write_set); | |
326 | FD_ZERO(except_set); | |
327 | FD_SET(socket, write_set); | |
328 | FD_SET(socket, except_set); | |
329 | ||
330 | // prepare timeout struct | |
331 | struct timeval tval; | |
332 | struct timeval *timeout_ptr; | |
333 | ||
334 | if (write_block_timeout == -1) | |
335 | timeout_ptr = NULL; | |
336 | else | |
337 | { | |
338 | timeout_ptr = &tval; | |
339 | ||
340 | // convert timeout from long long usec to int sec + int usec | |
341 | tval.tv_sec = write_block_timeout / 1000000; | |
342 | tval.tv_usec = write_block_timeout % 1000000; | |
343 | } | |
344 | ||
345 | // let's wait for the socket to become writable again... | |
346 | int rtn; | |
347 | while ((rtn=::select(socket+1, NULL, write_set, except_set, timeout_ptr)) ==-1 && errno == EINTR); | |
348 | ||
349 | if (rtn > 0 && (!FD_ISSET(socket,write_set)) && FD_ISSET(socket, except_set)) | |
350 | { | |
351 | // if we are selected but cannot write and have an exception | |
352 | // we have serious trouble... | |
353 | EXCEPTIONSTREAM(error,t2n_transfer_error,"exception on socket; cannot write any more."); | |
354 | } | |
355 | ||
356 | if (rtn==0) | |
357 | EXCEPTIONSTREAM(error,t2n_transfer_error,"timeout on select() for write"); | |
358 | ||
359 | if (rtn==-1) | |
360 | EXCEPTIONSTREAM(error,t2n_transfer_error,"cannot select() for write: " << strerror(errno)); | |
361 | } | |
362 | ||
363 | ||
a11e19b7 | 364 | } |