2 This file is part of GNUnet.
3 (C) 2009 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 2, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file util/network.c
23 * @brief basic, low-level networking interface
28 #include "gnunet_disk_lib.h"
30 #include "gnunet_container_lib.h"
32 #define DEBUG_NETWORK GNUNET_NO
34 #define DEBUG_W32_CYCLES GNUNET_NO
36 #ifndef INVALID_SOCKET
37 #define INVALID_SOCKET -1
41 struct GNUNET_NETWORK_Handle
51 * Address family / domain.
56 * Number of bytes in addr.
61 * Address we were bound to, or NULL.
63 struct sockaddr *addr;
68 struct GNUNET_NETWORK_FDSet
72 * Maximum number of any socket socket descriptor in the set (plus one)
77 * Bitset with the descriptors.
83 * Linked list of handles
85 struct GNUNET_CONTAINER_SList *handles;
91 #define FD_COPY(s, d) (memcpy ((d), (s), sizeof (fd_set)))
96 * Set if a socket should use blocking or non-blocking IO.
98 * @param doBlock blocking mode
99 * @return GNUNET_OK on success, GNUNET_SYSERR on error
102 socket_set_blocking (struct GNUNET_NETWORK_Handle *fd, int doBlock)
108 if (ioctlsocket (fd->fd, FIONBIO, &mode) == SOCKET_ERROR)
111 SetErrnoFromWinsockError (WSAGetLastError ());
112 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "ioctlsocket");
113 return GNUNET_SYSERR;
119 int flags = fcntl (fd->fd, F_GETFL);
123 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "fcntl");
124 return GNUNET_SYSERR;
127 flags &= ~O_NONBLOCK;
131 if (0 != fcntl (fd->fd, F_SETFL, flags))
134 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "fcntl");
135 return GNUNET_SYSERR;
144 * Make a socket non-inheritable to child processes
146 * @param h the socket to make non-inheritable
147 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
148 * @warning Not implemented on Windows
151 socket_set_inheritable (const struct GNUNET_NETWORK_Handle *h)
155 i = fcntl (h->fd, F_GETFD);
157 return GNUNET_SYSERR;
158 if (i == (i | FD_CLOEXEC))
161 if (fcntl (h->fd, F_SETFD, i) < 0)
162 return GNUNET_SYSERR;
170 * The MSG_NOSIGNAL equivalent on Mac OS X
172 * @param h the socket to make non-delaying
175 socket_set_nosigpipe (const struct GNUNET_NETWORK_Handle *h)
179 setsockopt (h->fd, SOL_SOCKET, SO_NOSIGPIPE, &abs_value, sizeof (abs_value)))
180 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
186 * Disable delays when sending data via the socket.
187 * (GNUnet makes sure that messages are as big as
190 * @param h the socket to make non-delaying
193 socket_set_nodelay (const struct GNUNET_NETWORK_Handle *h)
197 if (0 != setsockopt (h->fd, IPPROTO_TCP, TCP_NODELAY, &value, sizeof (value)))
198 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
200 const char * abs_value = "1";
201 if (0 != setsockopt (h->fd, IPPROTO_TCP, TCP_NODELAY, abs_value, sizeof (abs_value)))
202 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
208 * accept a new connection on a socket
210 * @param desc bound socket
211 * @param address address of the connecting peer, may be NULL
212 * @param address_len length of address
213 * @return client socket
215 struct GNUNET_NETWORK_Handle *
216 GNUNET_NETWORK_socket_accept (const struct GNUNET_NETWORK_Handle *desc,
217 struct sockaddr *address,
218 socklen_t * address_len)
220 struct GNUNET_NETWORK_Handle *ret;
222 ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle));
223 ret->fd = accept (desc->fd, address, address_len);
225 ret->af = address->sa_family;
228 if (ret->fd == INVALID_SOCKET)
231 SetErrnoFromWinsockError (WSAGetLastError ());
237 if (ret->fd >= FD_SETSIZE)
239 GNUNET_break (0 == close (ret->fd));
245 if (GNUNET_SYSERR == socket_set_blocking (ret, GNUNET_NO))
249 /* we might want to treat this one as fatal... */
251 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret));
256 if (GNUNET_OK != socket_set_inheritable (ret))
257 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
258 "socket_set_inheritable");
261 socket_set_nosigpipe (ret);
264 if (ret->af != AF_UNIX)
266 socket_set_nodelay (ret);
272 * Bind to a connected socket
274 * @param address address to be bound
275 * @param address_len length of address
276 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
279 GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc,
280 const struct sockaddr *address,
281 socklen_t address_len)
288 if (desc->af == AF_INET6)
289 if (0 != setsockopt (desc->fd, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof (on)))
290 GNUNET_log_strerror (GNUNET_ERROR_TYPE_DEBUG, "setsockopt");
292 /* is this needed or desired? or done elsewhere? */
293 if (0 != setsockopt (desc->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)))
294 GNUNET_log_strerror (GNUNET_ERROR_TYPE_DEBUG, "setsockopt");
300 if (address->sa_family == AF_UNIX)
302 const struct sockaddr_un *un = (const struct sockaddr_un*) address;
303 (void) unlink (un->sun_path);
307 ret = bind (desc->fd, address, address_len);
309 if (SOCKET_ERROR == ret)
310 SetErrnoFromWinsockError (WSAGetLastError ());
313 return GNUNET_SYSERR;
316 desc->addr = GNUNET_malloc (address_len);
317 memcpy (desc->addr, address, address_len);
318 desc->addrlen = address_len;
328 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
331 GNUNET_NETWORK_socket_close (struct GNUNET_NETWORK_Handle *desc)
336 ret = closesocket (desc->fd);
337 SetErrnoFromWinsockError (WSAGetLastError ());
339 ret = close (desc->fd);
343 if ( (desc->af == AF_UNIX) && (NULL != desc->addr) )
345 const struct sockaddr_un *un = (const struct sockaddr_un*) desc->addr;
346 if (0 != unlink (un->sun_path))
347 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
353 GNUNET_free_non_null (desc->addr);
355 return (ret == 0) ? GNUNET_OK : GNUNET_SYSERR;
360 * Box a native socket (and check that it is a socket).
362 * @param fd socket to box
363 * @return NULL on error (including not supported on target platform)
365 struct GNUNET_NETWORK_Handle *
366 GNUNET_NETWORK_socket_box_native (int fd)
371 struct GNUNET_NETWORK_Handle *ret;
373 if (fcntl (fd, F_GETFD) < 0)
374 return NULL; /* invalid FD */
375 ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle));
386 * @param address peer address
387 * @param address_len length of address
388 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
391 GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Handle *desc,
392 const struct sockaddr *address,
393 socklen_t address_len)
396 ret = connect (desc->fd, address, address_len);
399 if (SOCKET_ERROR == ret)
401 SetErrnoFromWinsockError (WSAGetLastError ());
402 if (errno == EWOULDBLOCK)
406 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
414 * @param level protocol level of the option
415 * @param optname identifier of the option
416 * @param optval options
417 * @param optlen length of optval
418 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
421 GNUNET_NETWORK_socket_getsockopt (const struct GNUNET_NETWORK_Handle *desc,
422 int level, int optname, void *optval,
426 ret = getsockopt (desc->fd, level, optname, optval, optlen);
429 if (ret == 0 && level == SOL_SOCKET && optname == SO_ERROR)
430 *((int *) optval) = GetErrnoFromWinsockError (*((int *) optval));
432 else if (SOCKET_ERROR == ret)
433 SetErrnoFromWinsockError (WSAGetLastError ());
436 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
443 * @param backlog length of the listen queue
444 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
447 GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Handle *desc,
451 ret = listen (desc->fd, backlog);
454 if (SOCKET_ERROR == ret)
455 SetErrnoFromWinsockError (WSAGetLastError ());
458 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
463 * How much data is available to be read on this descriptor?
465 * Returns GNUNET_NO if no data is available, or on error!
469 GNUNET_NETWORK_socket_recvfrom_amount (const struct GNUNET_NETWORK_Handle
474 /* How much is there to be read? */
477 error = ioctl (desc->fd, FIONREAD, &pending);
481 error = ioctlsocket (desc->fd, FIONREAD, &pending);
482 if (error != SOCKET_ERROR)
491 * Read data from a connected socket (always non-blocking).
493 * @param buffer buffer
494 * @param length length of buffer
495 * @param src_addr either the source to recv from, or all zeroes
496 * to be filled in by recvfrom
497 * @param addrlen length of the addr
500 GNUNET_NETWORK_socket_recvfrom (const struct GNUNET_NETWORK_Handle * desc,
501 void *buffer, size_t length,
502 struct sockaddr * src_addr,
510 flags |= MSG_DONTWAIT;
513 ret = recvfrom (desc->fd, buffer, length, flags, src_addr, addrlen);
515 if (SOCKET_ERROR == ret)
516 SetErrnoFromWinsockError (WSAGetLastError ());
523 * Read data from a connected socket (always non-blocking).
525 * @param buffer buffer
526 * @param length length of buffer
529 GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Handle * desc,
530 void *buffer, size_t length)
537 flags |= MSG_DONTWAIT;
539 ret = recv (desc->fd, buffer, length, flags);
541 if (SOCKET_ERROR == ret)
542 SetErrnoFromWinsockError (WSAGetLastError ());
549 * Send data (always non-blocking).
552 * @param buffer data to send
553 * @param length size of the buffer
554 * @return number of bytes sent, GNUNET_SYSERR on error
557 GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Handle * desc,
558 const void *buffer, size_t length)
565 flags |= MSG_DONTWAIT;
569 flags |= MSG_NOSIGNAL;
572 ret = send (desc->fd, buffer, length, flags);
575 if (SOCKET_ERROR == ret)
576 SetErrnoFromWinsockError (WSAGetLastError ());
584 * Send data to a particular destination (always non-blocking).
585 * This function only works for UDP sockets.
588 * @param message data to send
589 * @param length size of the data
590 * @param dest_addr destination address
591 * @param dest_len length of address
592 * @return number of bytes sent, GNUNET_SYSERR on error
595 GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Handle * desc,
596 const void *message, size_t length,
597 const struct sockaddr * dest_addr,
605 flags |= MSG_DONTWAIT;
608 flags |= MSG_NOSIGNAL;
610 ret = sendto (desc->fd, message, length, flags, dest_addr, dest_len);
612 if (SOCKET_ERROR == ret)
613 SetErrnoFromWinsockError (WSAGetLastError ());
622 * @param level protocol level of the option
623 * @param option_name option identifier
624 * @param option_value value to set
625 * @param option_len size of option_value
626 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
629 GNUNET_NETWORK_socket_setsockopt (struct GNUNET_NETWORK_Handle *fd,
630 int level, int option_name,
631 const void *option_value,
632 socklen_t option_len)
636 ret = setsockopt (fd->fd, level, option_name, option_value, option_len);
638 if (SOCKET_ERROR == ret)
639 SetErrnoFromWinsockError (WSAGetLastError ());
641 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
646 * Create a new socket. Configure it for non-blocking IO and
647 * mark it as non-inheritable to child processes (set the
648 * close-on-exec flag).
650 * @param domain domain of the socket
651 * @param type socket type
652 * @param protocol network protocol
653 * @return new socket, NULL on error
655 struct GNUNET_NETWORK_Handle *
656 GNUNET_NETWORK_socket_create (int domain, int type, int protocol)
658 struct GNUNET_NETWORK_Handle *ret;
660 ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle));
662 ret->fd = socket (domain, type, protocol);
663 if (INVALID_SOCKET == ret->fd)
666 SetErrnoFromWinsockError (WSAGetLastError ());
673 if (ret->fd >= FD_SETSIZE)
675 GNUNET_break (0 == close (ret->fd));
682 if (GNUNET_SYSERR == socket_set_blocking (ret, GNUNET_NO))
684 /* we might want to treat this one as fatal... */
686 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret));
691 if (GNUNET_OK != socket_set_inheritable (ret))
692 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
693 "socket_set_inheritable");
696 socket_set_nosigpipe (ret);
698 if ( (type == SOCK_STREAM)
700 && (domain != AF_UNIX)
703 socket_set_nodelay (ret);
709 * Shut down socket operations
711 * @param how type of shutdown
712 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
715 GNUNET_NETWORK_socket_shutdown (struct GNUNET_NETWORK_Handle *desc, int how)
719 ret = shutdown (desc->fd, how);
722 SetErrnoFromWinsockError (WSAGetLastError ());
724 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
729 * Disable the "CORK" feature for communication with the given socket,
730 * forcing the OS to immediately flush the buffer on transmission
731 * instead of potentially buffering multiple messages. Essentially
732 * reduces the OS send buffers to zero.
735 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
738 GNUNET_NETWORK_socket_disable_corking (struct GNUNET_NETWORK_Handle *desc)
743 if (0 != (ret = setsockopt (desc->fd, SOL_SOCKET, SO_SNDBUF, &value, sizeof (value))))
744 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
745 if (0 != (ret = setsockopt (desc->fd, SOL_SOCKET, SO_RCVBUF, &value, sizeof (value))))
746 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
748 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
757 GNUNET_NETWORK_fdset_zero (struct GNUNET_NETWORK_FDSet *fds)
762 GNUNET_CONTAINER_slist_clear (fds->handles);
767 * Add a socket to the FD set
769 * @param desc socket to add
772 GNUNET_NETWORK_fdset_set (struct GNUNET_NETWORK_FDSet *fds,
773 const struct GNUNET_NETWORK_Handle *desc)
775 FD_SET (desc->fd, &fds->sds);
776 if (desc->fd + 1 > fds->nsds)
777 fds->nsds = desc->fd + 1;
782 * Check whether a socket is part of the fd set
785 * @return 0 if the FD is not set
788 GNUNET_NETWORK_fdset_isset (const struct GNUNET_NETWORK_FDSet *fds,
789 const struct GNUNET_NETWORK_Handle *desc)
791 return FD_ISSET (desc->fd, &fds->sds);
796 * Add one fd set to another
797 * @param dst the fd set to add to
798 * @param src the fd set to add from
801 GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst,
802 const struct GNUNET_NETWORK_FDSet *src)
805 for (nfds = src->nsds; nfds > 0; nfds--)
806 if (FD_ISSET (nfds, &src->sds))
809 FD_SET (nfds, &dst->sds);
810 if (nfds + 1 > dst->nsds)
811 dst->nsds = nfds + 1;
814 GNUNET_CONTAINER_slist_append (dst->handles, src->handles);
820 * Copy one fd set to another
822 * @param to destination
826 GNUNET_NETWORK_fdset_copy (struct GNUNET_NETWORK_FDSet *to,
827 const struct GNUNET_NETWORK_FDSet *from)
829 FD_COPY (&from->sds, &to->sds);
830 to->nsds = from->nsds;
833 GNUNET_CONTAINER_slist_clear (to->handles);
834 GNUNET_CONTAINER_slist_append (to->handles, from->handles);
840 * Return file descriptor for this network handle
842 * @param desc wrapper to process
843 * @return POSIX file descriptor
846 GNUNET_NETWORK_get_fd (struct GNUNET_NETWORK_Handle *desc)
853 * Copy a native fd set
855 * @param to destination
856 * @param from native source set
857 * @param nfds the biggest socket number in from + 1
860 GNUNET_NETWORK_fdset_copy_native (struct GNUNET_NETWORK_FDSet *to,
861 const fd_set * from, int nfds)
863 FD_COPY (from, &to->sds);
869 * Set a native fd in a set
871 * @param to destination
872 * @param nfd native FD to set
874 void GNUNET_NETWORK_fdset_set_native (struct GNUNET_NETWORK_FDSet *to,
877 GNUNET_assert((nfd >= 0) && (nfd < FD_SETSIZE));
878 FD_SET (nfd, &to->sds);
879 to->nsds = GNUNET_MAX (nfd + 1, to->nsds);
884 * Test native fd in a set
886 * @param to set to test, NULL for empty set
887 * @param nfd native FD to test, or -1 for none
888 * @return GNUNET_YES if FD is set in the set
891 GNUNET_NETWORK_fdset_test_native (const struct GNUNET_NETWORK_FDSet *to,
894 if ( (nfd == -1) || (to == NULL) )
896 return FD_ISSET (nfd, &to->sds) ? GNUNET_YES : GNUNET_NO;
901 * Add a file handle to the fd set
903 * @param h the file handle to add
906 GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds,
907 const struct GNUNET_DISK_FileHandle *h)
910 GNUNET_CONTAINER_slist_add (fds->handles,
911 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
912 h, sizeof (struct GNUNET_DISK_FileHandle));
916 GNUNET_DISK_internal_file_handle_ (h, &fd, sizeof (int));
917 FD_SET (fd, &fds->sds);
918 if (fd + 1 > fds->nsds)
926 * Check if a file handle is part of an fd set
928 * @param h file handle
929 * @return GNUNET_YES if the file handle is part of the set
932 GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds,
933 const struct GNUNET_DISK_FileHandle *h)
937 return GNUNET_CONTAINER_slist_contains (fds->handles, h,
938 sizeof (struct GNUNET_DISK_FileHandle));
940 return FD_ISSET (h->fd, &fds->sds);
946 * Checks if two fd sets overlap
947 * @param fds1 first fd set
948 * @param fds2 second fd set
949 * @return GNUNET_YES if they do overlap, GNUNET_NO otherwise
952 GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1,
953 const struct GNUNET_NETWORK_FDSet *fds2)
959 if (nfds > fds2->nsds)
964 if (FD_ISSET (nfds, &fds1->sds) && FD_ISSET (nfds, &fds2->sds))
968 struct GNUNET_CONTAINER_SList_Iterator *it;
969 struct GNUNET_DISK_FileHandle *h;
973 /*This code is somewhat hacky, we are not supposed to know what's
974 inside of fd_set; also the O(n^2) is really bad... */
976 for (i = 0; i < fds1->sds.fd_count; i++)
978 for (j = 0; j < fds2->sds.fd_count; j++)
980 if (fds1->sds.fd_array[i] == fds2->sds.fd_array[j])
984 it = GNUNET_CONTAINER_slist_begin (fds1->handles);
985 while (GNUNET_CONTAINER_slist_end (it) != GNUNET_YES)
987 h = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (it, NULL);
988 if (GNUNET_CONTAINER_slist_contains
989 (fds2->handles, h, sizeof (struct GNUNET_DISK_FileHandle)))
991 GNUNET_CONTAINER_slist_iter_destroy (it);
994 GNUNET_CONTAINER_slist_next (it);
996 GNUNET_CONTAINER_slist_iter_destroy (it);
1004 * @return a new fd set
1006 struct GNUNET_NETWORK_FDSet *
1007 GNUNET_NETWORK_fdset_create ()
1009 struct GNUNET_NETWORK_FDSet *fds;
1010 fds = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_FDSet));
1012 fds->handles = GNUNET_CONTAINER_slist_create ();
1014 GNUNET_NETWORK_fdset_zero (fds);
1020 * Releases the associated memory of an fd set
1024 GNUNET_NETWORK_fdset_destroy (struct GNUNET_NETWORK_FDSet *fds)
1027 GNUNET_CONTAINER_slist_destroy (fds->handles);
1033 * Check if sockets meet certain conditions
1034 * @param rfds set of sockets to be checked for readability
1035 * @param wfds set of sockets to be checked for writability
1036 * @param efds set of sockets to be checked for exceptions
1037 * @param timeout relative value when to return
1038 * @return number of selected sockets, GNUNET_SYSERR on error
1041 GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
1042 struct GNUNET_NETWORK_FDSet *wfds,
1043 struct GNUNET_NETWORK_FDSet *efds,
1044 const struct GNUNET_TIME_Relative timeout)
1058 handles = GNUNET_CONTAINER_slist_count (rfds->handles);
1063 nfds = GNUNET_MAX (nfds, wfds->nsds);
1065 handles += GNUNET_CONTAINER_slist_count (wfds->handles);
1070 nfds = GNUNET_MAX (nfds, efds->nsds);
1072 handles += GNUNET_CONTAINER_slist_count (efds->handles);
1077 tv.tv_sec = timeout.rel_value / GNUNET_TIME_UNIT_SECONDS.rel_value;
1079 1000 * (timeout.rel_value - (tv.tv_sec * GNUNET_TIME_UNIT_SECONDS.rel_value));
1080 if ((nfds == 0) && (timeout.rel_value == GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
1086 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1088 ("Fatal internal logic error, process hangs in `%s' (abort with CTRL-C)!\n"),
1093 return select (nfds,
1094 (rfds != NULL) ? &rfds->sds : NULL,
1095 (wfds != NULL) ? &wfds->sds : NULL,
1096 (efds != NULL) ? &efds->sds : NULL,
1097 (timeout.rel_value == GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
1102 fd_set sock_read, sock_write, sock_except;
1103 fd_set aread, awrite, aexcept;
1104 struct GNUNET_CONTAINER_SList *handles_read, *handles_write,
1108 struct timeval tvslice;
1111 /* Number of milliseconds per cycle. Adapted on the fly */
1112 static unsigned int cycle_delay = 20;
1114 #define SAFE_FD_ISSET(fd, set) (set != NULL && FD_ISSET(fd, set))
1116 /* calculate how long we need to wait in milliseconds */
1117 if (timeout.rel_value == GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
1118 ms_total = INFINITE;
1121 ms_total = timeout.rel_value / GNUNET_TIME_UNIT_MILLISECONDS.rel_value;
1123 /* select() may be used as a portable way to sleep */
1124 if (!(rfds || wfds || efds))
1131 handles_read = GNUNET_CONTAINER_slist_create ();
1132 handles_write = GNUNET_CONTAINER_slist_create ();
1133 handles_except = GNUNET_CONTAINER_slist_create ();
1136 sock_read = rfds->sds;
1138 FD_ZERO (&sock_read);
1140 sock_write = wfds->sds;
1142 FD_ZERO (&sock_write);
1144 sock_except = efds->sds;
1146 FD_ZERO (&sock_except);
1148 /* multiplex between winsock select() and waiting on the handles */
1153 #if DEBUG_W32_CYCLES
1154 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1155 "Starting a cycle, delay is %dms. nfds is %d.\n", cycle_delay, nfds);
1158 limit = GetTickCount () + ms_total;
1167 /* overwrite the zero'd sets here; the select call
1168 * will clear those that are not active */
1169 FD_COPY (&sock_read, &aread);
1170 FD_COPY (&sock_write, &awrite);
1171 FD_COPY (&sock_except, &aexcept);
1173 tvslice.tv_usec = cycle_delay;
1174 #if DEBUG_W32_CYCLES
1176 for (i = 0; i < nfds; i++)
1178 if (SAFE_FD_ISSET (i, &sock_read))
1180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1181 "Going to select socket %d for reading\n", i);
1183 if (SAFE_FD_ISSET (i, &sock_write))
1185 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1186 "Going to select socket %d for writing\n", i);
1188 if (SAFE_FD_ISSET (i, &sock_except))
1190 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1191 "Going to select socket %d for exceptions\n", i);
1195 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1196 "Waiting for %d microseconds, %d left\n", cycle_delay, (limit - GetTickCount ())*1000);
1199 select (nfds + 1, &aread, &awrite, &aexcept,
1200 &tvslice)) == SOCKET_ERROR)
1203 SetErrnoFromWinsockError (WSAGetLastError ());
1204 if (errno == ENOTSOCK)
1208 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "select");
1211 goto select_loop_end;
1213 #if DEBUG_W32_CYCLES
1214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1215 "Select () returned %d, GLE is %d\n", retcode, GetLastError ());
1219 /* Poll read pipes */
1222 struct GNUNET_CONTAINER_SList_Iterator *i;
1223 #if DEBUG_W32_CYCLES
1224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1225 "Polling rfds for readable pipes\n");
1227 for (i = GNUNET_CONTAINER_slist_begin (rfds->handles);
1228 GNUNET_CONTAINER_slist_end (i) != GNUNET_YES;
1229 GNUNET_CONTAINER_slist_next (i))
1232 struct GNUNET_DISK_FileHandle *fh;
1234 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (i, NULL);
1235 if (fh->type == GNUNET_PIPE)
1237 #if DEBUG_W32_CYCLES
1238 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1239 "Polling pipe 0x%x (0x%x)\n", fh, fh->h);
1241 if (!PeekNamedPipe (fh->h, NULL, 0, NULL, &dwBytes, NULL))
1243 DWORD error_code = GetLastError ();
1246 case ERROR_BROKEN_PIPE:
1247 GNUNET_CONTAINER_slist_add (handles_read,
1248 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1249 fh, sizeof (struct GNUNET_DISK_FileHandle));
1254 SetErrnoFromWinError (error_code);
1257 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
1261 goto select_loop_end;
1267 GNUNET_CONTAINER_slist_add (handles_read,
1268 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1269 fh, sizeof (struct GNUNET_DISK_FileHandle));
1275 /* Should we wait for more bytes to read here (in case of previous EOF)? */
1276 GNUNET_CONTAINER_slist_add (handles_read,
1277 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1278 fh, sizeof (struct GNUNET_DISK_FileHandle));
1281 GNUNET_CONTAINER_slist_iter_destroy (i);
1284 /* Poll for faulty pipes */
1288 #if DEBUG_W32_CYCLES
1289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1290 "Polling efds for broken pipes\n");
1292 struct GNUNET_CONTAINER_SList_Iterator *i;
1293 for (i = GNUNET_CONTAINER_slist_begin (efds->handles);
1294 GNUNET_CONTAINER_slist_end (i) != GNUNET_YES;
1295 GNUNET_CONTAINER_slist_next (i))
1298 struct GNUNET_DISK_FileHandle *fh;
1301 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (i, NULL);
1302 if (fh->type == GNUNET_PIPE)
1304 #if DEBUG_W32_CYCLES
1305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1306 "Polling pipe 0x%x (0x%x)\n", fh, fh->h);
1308 if (!PeekNamedPipe (fh->h, NULL, 0, NULL, &dwBytes, NULL))
1311 GNUNET_CONTAINER_slist_add (handles_except,
1312 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1313 fh, sizeof (struct GNUNET_DISK_FileHandle));
1318 GNUNET_CONTAINER_slist_iter_destroy (i);
1323 GNUNET_CONTAINER_slist_append (handles_write, wfds->handles);
1324 retcode += GNUNET_CONTAINER_slist_count (wfds->handles);
1327 /* Check for closed sockets */
1328 for (i = 0; i < nfds; i++)
1331 if (SAFE_FD_ISSET (i, &sock_read))
1334 struct sockaddr addr;
1336 if (getpeername (i, &addr, &len) == SOCKET_ERROR)
1342 (i, SOL_SOCKET, SO_ERROR, (char *) &err, &len) == 0
1343 && err == WSAENOTCONN)
1346 if (!SAFE_FD_ISSET (i, &aread))
1359 /* For pipes, there have been no select() call, so the poll is
1360 * more likely to miss first time around. For now just don't increase
1361 * the delay for pipes only.
1364 cycle_delay = cycle_delay * 2 > 250000 ? 250000 : cycle_delay * 1.4;
1365 /* Missed an I/O - double the cycle time */
1366 #if DEBUG_W32_CYCLES
1367 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1368 "The cycle missed, increased the delay to %dms\n", cycle_delay);
1373 /* Successfully selected something - decrease the cycle time */
1374 /* Minimum is 5 microseconds. Decrease the delay by half,
1375 * or by 5000 - whichever is higher.
1377 cycle_delay -= cycle_delay > 5000 ? GNUNET_MAX (5000, cycle_delay / 2) : cycle_delay - 5;
1378 #if DEBUG_W32_CYCLES
1379 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1380 "The cycle hit, decreased the delay to %dms\n", cycle_delay);
1383 if (retcode == 0 && nfds == 0)
1385 long long diff = limit - GetTickCount ();
1386 diff = diff > 0 ? diff : 0;
1387 #if DEBUG_W32_CYCLES
1388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1389 "No sockets, sleeping for %d or %d ms\n", cycle_delay / 1000, diff);
1391 Sleep (GNUNET_MIN (cycle_delay / 1000, diff));
1394 while (retcode == 0 && (ms_total == INFINITE || GetTickCount () < limit));
1400 GNUNET_NETWORK_fdset_zero (rfds);
1401 GNUNET_NETWORK_fdset_copy_native (rfds, &aread, retcode);
1402 GNUNET_CONTAINER_slist_clear (rfds->handles);
1403 GNUNET_CONTAINER_slist_append (rfds->handles, handles_read);
1407 GNUNET_NETWORK_fdset_zero (wfds);
1408 GNUNET_NETWORK_fdset_copy_native (wfds, &awrite, retcode);
1409 GNUNET_CONTAINER_slist_clear (wfds->handles);
1410 GNUNET_CONTAINER_slist_append (wfds->handles, handles_write);
1414 GNUNET_NETWORK_fdset_zero (efds);
1415 GNUNET_NETWORK_fdset_copy_native (efds, &aexcept, retcode);
1416 GNUNET_CONTAINER_slist_clear (efds->handles);
1417 GNUNET_CONTAINER_slist_append (efds->handles, handles_except);
1421 GNUNET_CONTAINER_slist_destroy (handles_read);
1422 GNUNET_CONTAINER_slist_destroy (handles_write);
1423 GNUNET_CONTAINER_slist_destroy (handles_except);
1430 /* end of network.c */