6fb3d6eaabc520670acaf673f006b8e37e40eea6
[feisty_meow.git] / octopi / library / sockets / raw_socket.cpp
1 /*****************************************************************************\
2 *                                                                             *
3 *  Name   : raw_socket                                                        *
4 *  Author : Chris Koeritz                                                     *
5 *                                                                             *
6 *******************************************************************************
7 * Copyright (c) 1991-$now By Author.  This program is free software; you can  *
8 * redistribute it and/or modify it under the terms of the GNU General Public  *
9 * License as published by the Free Software Foundation; either version 2 of   *
10 * the License or (at your option) any later version.  This is online at:      *
11 *     http://www.fsf.org/copyleft/gpl.html                                    *
12 * Please send any updates to: fred@gruntose.com                               *
13 \*****************************************************************************/
14
15 #include "raw_socket.h"
16 #include "tcpip_stack.h"
17
18 #include <basis/functions.h>
19 #include <loggers/critical_events.h>
20 #include <loggers/program_wide_logger.h>
21 #include <timely/time_stamp.h>
22
23 #include <stdlib.h>
24 #ifdef __APPLE__
25   #include <fcntl.h>
26 #endif
27 #ifdef __UNIX__
28   #include <arpa/inet.h>
29   #include <errno.h>
30   #include <netinet/tcp.h>
31   #include <sys/ioctl.h>
32   #include <sys/socket.h>
33   #include <unistd.h>
34   #define OPTYPE (void *)
35 #endif
36 #ifdef __WIN32__
37   #define OPTYPE (char *)
38 #endif
39
40 using namespace basis;
41 using namespace loggers;
42 using namespace timely;
43
44 namespace sockets {
45
46 //#define DEBUG_RAW_SOCKET
47   // uncomment for noisy diagnostics.
48
49 #undef LOG
50 #define LOG(to_print) CLASS_EMERGENCY_LOG(program_wide_logger::get(), to_print)
51
52 const int MULTIPLE_DISCONNECT_CHECKS = 28;
53   // we will make certain that select really says there's data and ioctl
54   // really says there's no data waiting before we believe there's been
55   // a disconnect.  we'll check that state the number of times specified.
56
57 class fd_set_wrapper : public fd_set {};
58
59 //////////////
60
61 const basis::un_int NON_BLOCKING = FIONBIO;
62 const basis::un_int IOCTL_READ = FIONREAD;
63
64 #ifdef __WIN32__
65 /*
66 // defined by winsock header but not present in the winsock dll.
67 int PASCAL FAR __WSAFDIsSet(SOCKET fd, fd_set FAR *the_set)
68 {
69   int i = the_set->fd_count;
70   while (i--)
71     if (the_set->fd_array[i] == fd)
72       return true;
73   return false;
74 }
75 */
76 #endif
77
78 //////////////
79
80 raw_socket::raw_socket()
81 : _stack(new tcpip_stack())
82 {}
83
84 raw_socket::~raw_socket()
85 {
86   WHACK(_stack);
87 }
88
89 int raw_socket::close(basis::un_int &socket)
90 {
91   int to_return = 0;
92 #ifdef __WIN32__
93   to_return = closesocket(socket);
94 #endif
95 #ifdef __UNIX__
96   to_return = ::close(socket);
97 #endif
98   socket = 0;
99   return to_return;
100 }
101
102 //move this into parser bits as a OR combiner or something.
103 void combine(astring &existing, const astring &addition)
104 {
105   if (addition.t()) {
106     if (existing.t()) existing += " | ";
107     existing += addition;
108   }
109 }
110
111 astring raw_socket::interest_name(int interest)
112 {
113   astring to_return;
114   if (interest & SI_CONNECTED) combine(to_return, "CONNECTED");
115   if (interest & SI_DISCONNECTED) combine(to_return, "DISCONNECTED");
116   if (interest & SI_WRITABLE) combine(to_return, "WRITABLE");
117   if (interest & SI_READABLE) combine(to_return, "READABLE");
118   if (interest & SI_ERRONEOUS) combine(to_return, "ERRONEOUS");
119   if (!interest) combine(to_return, "NORMAL");
120   return to_return;
121 }
122
123 int raw_socket::ioctl(basis::un_int socket, int request, void *argp) const
124 {
125 #ifdef __UNIX__
126   return ::ioctl(socket, request, argp);
127 #endif
128 #ifdef __WIN32__
129   #ifdef _MSC_VER
130     return ioctlsocket(socket, request, (un_long *)argp);
131   #else
132     return ioctlsocket(socket, request, (un_int *)argp);
133   #endif
134 #endif
135 }
136
137 bool raw_socket::set_non_blocking(basis::un_int socket, bool non_blocking)
138 {
139   FUNCDEF("set_non_blocking");
140 #ifdef __APPLE__
141   int curr_flags = fcntl(socket, F_GETFL, 0);
142   if (fcntl(socket, F_SETFL, curr_flags | O_NONBLOCK) < 0) return false;
143 #else
144   int arg = int(non_blocking);
145   if (negative(ioctl(socket, NON_BLOCKING, &arg))) {
146     LOG(a_sprintf("Could not set non-blocking (FIONBIO) option on raw_socket %u.", socket));
147     return false;
148   }
149 #endif
150   return true;
151 }
152
153 bool raw_socket::set_nagle_algorithm(basis::un_int socket, bool use_nagle)
154 {
155   FUNCDEF("set_nagle_algorithm");
156   int arg = int(!use_nagle);  // opposite of the flag, since we set no-delay.
157   if (negative(setsockopt(socket, IPPROTO_TCP, TCP_NODELAY, OPTYPE &arg,
158       sizeof(arg)))) {
159     LOG(a_sprintf("Could not change nagle coalescing mode on %u.", socket));
160     return false;
161   }
162   return true;
163 }
164
165 bool raw_socket::set_broadcast(basis::un_int socket, bool broadcasting)
166 {
167   FUNCDEF("set_broadcast");
168   int arg = int(broadcasting);
169   if (negative(setsockopt(socket, SOL_SOCKET, SO_BROADCAST, OPTYPE &arg,
170       sizeof(arg)))) {
171     LOG(a_sprintf("Could not change broadcast mode on %u.", socket));
172     return false;
173   }
174   return true;
175 }
176
177 bool raw_socket::set_reuse_address(basis::un_int socket, bool reuse)
178 {
179   FUNCDEF("set_reuse_address");
180   int arg = int(reuse);
181   if (negative(setsockopt(socket, SOL_SOCKET, SO_REUSEADDR, OPTYPE &arg,
182       sizeof(arg)))) {
183     LOG(a_sprintf("Could not set reuse address mode on %u.", socket));
184     return false;
185   }
186   return true;
187 }
188
189 bool raw_socket::set_keep_alive(basis::un_int socket, bool keep_alive)
190 {
191   FUNCDEF("set_keep_alive");
192   int arg = int(keep_alive);
193   if (negative(setsockopt(socket, SOL_SOCKET, SO_KEEPALIVE, OPTYPE &arg,
194       sizeof(arg)))) {
195     LOG(a_sprintf("Could not set keep alive mode on %u.", socket));
196     return false;
197   }
198   return true;
199 }
200
201 int raw_socket::select(basis::un_int socket, int mode, int timeout) const
202 {
203   FUNCDEF("select [single]");
204   if (!socket) return SI_ERRONEOUS;
205   fd_set_wrapper read_list, write_list, exceps;
206   int ret = inner_select(socket, mode, timeout, read_list, write_list, exceps);
207   if (!ret) return 0;  // nothing is happening.
208   if (ret == SI_ERRONEOUS) return SI_ERRONEOUS;  // something bad happened.
209   // otherwise we should be at base-line status.
210   return analyze_select_result(socket, mode, read_list, write_list, exceps);
211 }
212
213 int raw_socket::inner_select(basis::un_int socket, int mode, int timeout,
214     fd_set_wrapper &read_list, fd_set_wrapper &write_list,
215     fd_set_wrapper &exceptions) const
216 {
217   FUNCDEF("inner_select");
218   // setup the file descriptor sets for the select.  we check readability,
219   // writability and exception status.
220   FD_ZERO(&read_list); FD_SET(socket, &read_list);
221   FD_ZERO(&write_list); FD_SET(socket, &write_list);
222   FD_ZERO(&exceptions); FD_SET(socket, &exceptions);
223
224   timeval base_time_out;
225   time_stamp::fill_timeval_ms(base_time_out, timeout);
226     // timeval has tv_sec=seconds, tv_usec=microseconds.
227 #if !defined(__GNU_WINDOWS__)
228   timeval *time_out = &base_time_out;
229 #elif defined(__GNU_WINDOWS__)
230   __ms_timeval win_time_out;
231   win_time_out.tv_sec = base_time_out.tv_sec;
232   win_time_out.tv_usec = base_time_out.tv_usec;
233   __ms_timeval *time_out = &win_time_out;
234 #endif
235
236   // select will tell us about the socket.
237   int ret = ::select(socket + 1,
238       (mode & SELECTING_JUST_WRITE)? NULL_POINTER : &read_list,
239       (mode & SELECTING_JUST_READ)? NULL_POINTER : &write_list,
240       &exceptions, time_out);
241   int error = critical_events::system_error();
242   if (!ret) return 0;  // nothing to report.
243
244   if (ret == SOCKET_ERROR) {
245     switch (error) {
246       // all real errors fall out to the error handling stuff.
247       case SOCK_EFAULT:  // intentional fall-through.
248       case SOCK_ENETDOWN:  // intentional fall-through.
249       case SOCK_EINVAL:  // intentional fall-through.
250       case SOCK_EINTR:  // intentional fall-through.
251 #ifdef __WIN32__
252       case SOCK_NOTINITIALISED:  // intentional fall-through.
253 #endif
254       case SOCK_ENOTSOCK:
255         break;
256
257       // hopefully all these others are bogus errors...
258       case SOCK_EINPROGRESS:  // intentional fall-through.
259       case 0:  // intentional fall-through.
260       default:
261 #ifdef DEBUG_RAW_SOCKET
262         LOG("got to weird case, in progress or zero.");
263 #endif
264         return 0;  // not really an error.
265     }
266 #ifdef DEBUG_RAW_SOCKET
267     LOG(a_sprintf("socket %u had error %d in select: %s.",
268         socket, error, _stack->tcpip_error_name(error).s()));
269 #endif
270     return SI_ERRONEOUS;
271   }
272
273   // if we got to here, then there are some things to report...
274   return SI_BASELINE;
275 }
276
277 int raw_socket::test_readability(basis::un_int socket) const
278 {
279   FUNCDEF("test_readability");
280   basis::un_int len;
281   if (negative(ioctl(socket, IOCTL_READ, &len))) {
282     LOG(astring(astring::SPRINTF, "socket %u had ioctl error: %s.",
283         socket, _stack->tcpip_error_name(critical_events::system_error()).s()));
284     return SI_ERRONEOUS;
285   } else {
286     if (positive(len)) return SI_READABLE;
287     else return SI_DISCONNECTED;
288   }
289 }
290
291 int raw_socket::analyze_select_result(basis::un_int socket, int mode,
292     fd_set_wrapper &read_list, fd_set_wrapper &write_list,
293     fd_set_wrapper &exceptions) const
294 {
295 #ifdef DEBUG_RAW_SOCKET
296   FUNCDEF("analyze_select_result");
297 #endif
298   int to_return = 0;
299
300   // in case of an exception, we return an error.
301   if (FD_ISSET(socket, &exceptions)) {
302 #ifdef DEBUG_RAW_SOCKET
303     LOG(astring(astring::SPRINTF, "exception seen for socket %u!", socket));
304 #endif
305   }
306
307   // check to see if there are bytes to read.
308   if ( ! (mode & SELECTING_JUST_WRITE) && FD_ISSET(socket, &read_list)) {
309     // make sure we have data.  if no data is available, it means a
310     // disconnect occurred.
311
312     int readable = test_readability(socket);
313     if (readable == SI_ERRONEOUS)
314       to_return |= SI_ERRONEOUS;
315     else if (readable == SI_READABLE)
316       to_return |= SI_READABLE;
317     else if (readable == SI_DISCONNECTED) {
318       // we need to check multiple times to be sure the OS really means this.
319       // either windoze seems to report an erroneous disconnect every few
320       // days or there's a bad synchronization issue as yet uncovered.
321       bool really_disconnected = true;
322       for (int i = 0; i < MULTIPLE_DISCONNECT_CHECKS; i++) {
323         fd_set_wrapper read_list, write_list, exceps;
324         int temp_ret = inner_select(socket, SELECTING_JUST_READ, 0, read_list,
325             write_list, exceps);
326         // check the return value first...
327         if (!temp_ret) {
328           // nothing happening (a zero return) means the socket's no longer
329           // claiming to have a readable state; our disconnect condition is
330           // thus violated and we can leave.
331           really_disconnected = false;
332           break;
333         }
334         if (temp_ret == SI_ERRONEOUS) {
335           // this, on the other hand, sounds really bad.  the socket doesn't
336           // seem to exist any more or something else horrid happened.
337           really_disconnected = true;
338           break;
339         }
340         // if the select worked, we can check the fd_set now for readability.
341         if (!FD_ISSET(socket, &read_list)) {
342           // we are not in a disconnected state without being told we're
343           // readable.  supposedly.
344           really_disconnected = false;
345           break;
346         }
347         // now we really test the socket for readability by making sure there
348         // really is data pending on the socket.  if it's readable but there's
349         // no data, then either a disconnection has occurred or is in progress.
350         readable = test_readability(socket);
351         if (readable != SI_DISCONNECTED) {
352           // we are not disconnected if there's really data waiting.
353           really_disconnected = false;
354           break;
355         }
356       }
357       if (really_disconnected) {
358 #ifdef DEBUG_RAW_SOCKET
359         LOG(a_sprintf("connection closed on socket %u.", socket));
360 #endif
361         to_return |= SI_DISCONNECTED;
362       }
363     }
364   }
365
366   // check writability state.
367   if (! (mode & SELECTING_JUST_READ) && FD_ISSET(socket, &write_list)) {
368     to_return |= SI_WRITABLE;
369   }
370
371   return to_return;
372 }
373
374 int raw_socket::select(int_array &read_sox, int_array &write_sox,
375     int timeout) const
376 {
377 #ifdef DEBUG_RAW_SOCKET
378   FUNCDEF("select [multiple]");
379 #endif
380   if (!read_sox.length() && !write_sox.length())
381    return 0;  // nothing happened to nothing.
382
383   int to_return = 0;  // will get bits slammed into it to report results.
384
385   // setup the file descriptor sets for the select.  we check readability,
386   // writability and exception status.
387   fd_set_wrapper read_list; FD_ZERO(&read_list);
388   fd_set_wrapper write_list; FD_ZERO(&write_list);
389   fd_set_wrapper exceptions; FD_ZERO(&exceptions);
390   // set up the lists with the sets we were handed.
391   basis::un_int highest = 0;
392   int i = 0;
393   for (i = 0; i < read_sox.length(); i++) {
394     basis::un_int sock = (basis::un_int)read_sox[i];
395     if (sock > highest) highest = sock;
396     FD_SET(sock, &read_list);
397   }
398   for (i = 0; i < write_sox.length(); i++) {
399     basis::un_int sock = (basis::un_int)write_sox[i];
400     if (sock > highest) highest = sock;
401     FD_SET(sock, &write_list);
402   }
403
404   timeval base_time_out;
405   time_stamp::fill_timeval_ms(base_time_out, timeout);
406     // timeval has tv_sec=seconds, tv_usec=microseconds.
407 #if !defined(__GNU_WINDOWS__)
408   timeval *time_out = &base_time_out;
409 #elif defined(__GNU_WINDOWS__)
410   __ms_timeval win_time_out;
411   win_time_out.tv_sec = base_time_out.tv_sec;
412   win_time_out.tv_usec = base_time_out.tv_usec;
413   __ms_timeval *time_out = &win_time_out;
414 #endif
415
416   // select will tell us about the socket.
417   int ret = ::select(highest + 1,
418       (read_sox.length())? &read_list : NULL_POINTER,
419       (write_sox.length())? &write_list : NULL_POINTER,
420       &exceptions, time_out);
421   int error = critical_events::system_error();
422
423   if (ret == SOCKET_ERROR) {
424     switch (error) {
425       // all real errors fall out to the error handling stuff.
426       case SOCK_EFAULT:  // intentional fall-through.
427       case SOCK_ENETDOWN:  // intentional fall-through.
428       case SOCK_EINVAL:  // intentional fall-through.
429       case SOCK_EINTR:  // intentional fall-through.
430 #ifdef __WIN32__
431       case SOCK_NOTINITIALISED:  // intentional fall-through.
432 #endif
433       case SOCK_ENOTSOCK:
434         break;
435
436       // hopefully all these others are bogus errors...
437       case SOCK_EINPROGRESS:  // intentional fall-through.
438       case 0:  // intentional fall-through.
439       default:
440 #ifdef DEBUG_RAW_SOCKET
441         LOG("got to weird case, in progress or zero.");
442 #endif
443
444 //hmmm: fix retd sox?  what's this outcome mean for the list?
445
446         return 0;  // not really an error.
447     }
448 #ifdef DEBUG_RAW_SOCKET
449     LOG(a_sprintf("sockets had error %d in select: %s.",
450         error, _stack->tcpip_error_name(error).s()));
451 #endif
452
453 //hmmm: fix retd sox?  what's this outcome mean for the list?
454
455     return SI_ERRONEOUS;
456   } else if (!ret) {
457     // we know of nothing exciting for any of these.
458     read_sox.reset();
459     write_sox.reset();
460     return 0;  // nothing to report.
461   }
462
463   // if we got to here, then there are some things to report...
464   // iterate through the lists and check results.
465   for (int k = 0; k < read_sox.length(); k++) {
466     basis::un_int socket = read_sox[k];
467     int interim = analyze_select_result(socket, SELECTING_JUST_READ, read_list,
468         write_list, exceptions);
469     if (!interim) {
470       // nothing happened on that guy.
471       read_sox.zap(k, k);
472       k--;  // skip back to before the whack.
473       continue;
474     }
475     to_return |= interim;  // join the results with overall result.
476   }
477   for (int p = 0; p < write_sox.length(); p++) {
478     basis::un_int socket = write_sox[p];
479     int interim = analyze_select_result(socket, SELECTING_JUST_WRITE, read_list,
480         write_list, exceptions);
481     if (!interim) {
482       // nothing happened on that guy.
483       write_sox.zap(p, p);
484       p--;  // skip back to before the whack.
485       continue;
486     }
487     to_return |= interim;  // join the results with overall result.
488   }
489
490   return to_return;
491 }
492
493 } //namespace.
494