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 #ifndef INVALID_SOCKET
35 #define INVALID_SOCKET -1
39 struct GNUNET_NETWORK_Handle
49 * Address family / domain.
55 struct GNUNET_NETWORK_FDSet
59 * Maximum number of any socket socket descriptor in the set (plus one)
64 * Bitset with the descriptors.
70 * Linked list of handles
72 struct GNUNET_CONTAINER_SList *handles;
78 #define FD_COPY(s, d) (memcpy ((d), (s), sizeof (fd_set)))
83 * Set if a socket should use blocking or non-blocking IO.
85 * @param doBlock blocking mode
86 * @return GNUNET_OK on success, GNUNET_SYSERR on error
89 socket_set_blocking (struct GNUNET_NETWORK_Handle *fd, int doBlock)
95 if (ioctlsocket (fd->fd, FIONBIO, &mode) == SOCKET_ERROR)
98 SetErrnoFromWinsockError (WSAGetLastError ());
99 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "ioctlsocket");
100 return GNUNET_SYSERR;
106 int flags = fcntl (fd->fd, F_GETFL);
110 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "fcntl");
111 return GNUNET_SYSERR;
114 flags &= ~O_NONBLOCK;
118 if (0 != fcntl (fd->fd, F_SETFL, flags))
121 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "fcntl");
122 return GNUNET_SYSERR;
131 * Make a socket non-inheritable to child processes
133 * @param h the socket to make non-inheritable
134 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
135 * @warning Not implemented on Windows
138 socket_set_inheritable (const struct GNUNET_NETWORK_Handle *h)
142 i = fcntl (h->fd, F_GETFD);
144 return GNUNET_SYSERR;
145 if (i == (i | FD_CLOEXEC))
148 if (fcntl (h->fd, F_SETFD, i) < 0)
149 return GNUNET_SYSERR;
157 * The MSG_NOSIGNAL equivalent on Mac OS X
159 * @param h the socket to make non-delaying
162 socket_set_nosigpipe (const struct GNUNET_NETWORK_Handle *h)
166 setsockopt (h->fd, SOL_SOCKET, SO_NOSIGPIPE, &value, sizeof (value)))
167 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
173 * Disable delays when sending data via the socket.
174 * (GNUnet makes sure that messages are as big as
177 * @param h the socket to make non-delaying
180 socket_set_nodelay (const struct GNUNET_NETWORK_Handle *h)
184 if (0 != setsockopt (h->fd, IPPROTO_TCP, TCP_NODELAY, &value, sizeof (value)))
185 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
187 const char * value = "1";
188 if (0 != setsockopt (h->fd, IPPROTO_TCP, TCP_NODELAY, value, sizeof (value)))
189 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
195 * accept a new connection on a socket
197 * @param desc bound socket
198 * @param address address of the connecting peer, may be NULL
199 * @param address_len length of address
200 * @return client socket
202 struct GNUNET_NETWORK_Handle *
203 GNUNET_NETWORK_socket_accept (const struct GNUNET_NETWORK_Handle *desc,
204 struct sockaddr *address,
205 socklen_t * address_len)
207 struct GNUNET_NETWORK_Handle *ret;
209 ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle));
210 ret->fd = accept (desc->fd, address, address_len);
211 ret->af = address->sa_family;
212 if (ret->fd == INVALID_SOCKET)
215 SetErrnoFromWinsockError (WSAGetLastError ());
221 if (ret->fd >= FD_SETSIZE)
223 GNUNET_break (0 == close (ret->fd));
229 if (GNUNET_SYSERR == socket_set_blocking (ret, GNUNET_NO))
233 /* we might want to treat this one as fatal... */
235 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret));
240 if (GNUNET_OK != socket_set_inheritable (ret))
241 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
242 "socket_set_inheritable");
245 socket_set_nosigpipe (ret);
248 if (address->sa_family != AF_UNIX)
250 socket_set_nodelay (ret);
256 * Bind to a connected socket
258 * @param address address to be bound
259 * @param address_len length of address
260 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
263 GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc,
264 const struct sockaddr *address,
265 socklen_t address_len)
272 if (desc->af == AF_INET6)
273 setsockopt (desc->fd, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof (on));
275 /* is this needed or desired? or done elsewhere? */
276 setsockopt (desc->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on));
280 ret = bind (desc->fd, address, address_len);
282 if (SOCKET_ERROR == ret)
283 SetErrnoFromWinsockError (WSAGetLastError ());
286 if ( (ret == 0) && (address->sa_family == AF_UNIX))
288 const struct sockaddr_un *un = (const struct sockaddr_un*) address;
289 if (0 != unlink (un->sun_path))
290 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
296 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
303 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
306 GNUNET_NETWORK_socket_close (struct GNUNET_NETWORK_Handle *desc)
311 ret = closesocket (desc->fd);
312 SetErrnoFromWinsockError (WSAGetLastError ());
314 ret = close (desc->fd);
317 return (ret == 0) ? GNUNET_OK : GNUNET_SYSERR;
322 * Box a native socket (and check that it is a socket).
324 * @param fd socket to box
325 * @return NULL on error (including not supported on target platform)
327 struct GNUNET_NETWORK_Handle *
328 GNUNET_NETWORK_socket_box_native (int fd)
333 struct GNUNET_NETWORK_Handle *ret;
335 if (fcntl (fd, F_GETFD) < 0)
336 return NULL; /* invalid FD */
337 ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle));
348 * @param address peer address
349 * @param address_len length of address
350 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
353 GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Handle *desc,
354 const struct sockaddr *address,
355 socklen_t address_len)
358 ret = connect (desc->fd, address, address_len);
361 if (SOCKET_ERROR == ret)
364 SetErrnoFromWinsockError (WSAGetLastError ());
365 if (errno == EWOULDBLOCK)
369 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
377 * @param level protocol level of the option
378 * @param optname identifier of the option
379 * @param optval options
380 * @param optlen length of optval
381 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
384 GNUNET_NETWORK_socket_getsockopt (const struct GNUNET_NETWORK_Handle *desc,
385 int level, int optname, void *optval,
389 ret = getsockopt (desc->fd, level, optname, optval, optlen);
392 if (ret == 0 && level == SOL_SOCKET && optname == SO_ERROR)
393 *((int *) optval) = GetErrnoFromWinsockError (*((int *) optval));
395 else if (SOCKET_ERROR == ret)
396 SetErrnoFromWinsockError (WSAGetLastError ());
399 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
406 * @param backlog length of the listen queue
407 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
410 GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Handle *desc,
414 ret = listen (desc->fd, backlog);
417 if (SOCKET_ERROR == ret)
418 SetErrnoFromWinsockError (WSAGetLastError ());
421 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
426 * How much data is available to be read on this descriptor?
428 * Returns GNUNET_NO if no data is available, or on error!
432 GNUNET_NETWORK_socket_recvfrom_amount (const struct GNUNET_NETWORK_Handle
437 /* How much is there to be read? */
440 error = ioctl (desc->fd, FIONREAD, &pending);
444 error = ioctlsocket (desc->fd, FIONREAD, &pending);
445 if (error != SOCKET_ERROR)
453 * Read data from a connected socket (always non-blocking).
455 * @param buffer buffer
456 * @param length length of buffer
457 * @param src_addr either the source to recv from, or all zeroes
458 * to be filled in by recvfrom
459 * @param addrlen length of the addr
462 GNUNET_NETWORK_socket_recvfrom (const struct GNUNET_NETWORK_Handle * desc,
463 void *buffer, size_t length,
464 struct sockaddr * src_addr,
472 flags |= MSG_DONTWAIT;
475 ret = recvfrom (desc->fd, buffer, length, flags, src_addr, addrlen);
477 if (SOCKET_ERROR == ret)
478 SetErrnoFromWinsockError (WSAGetLastError ());
485 * Read data from a connected socket (always non-blocking).
487 * @param buffer buffer
488 * @param length length of buffer
491 GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Handle * desc,
492 void *buffer, size_t length)
499 flags |= MSG_DONTWAIT;
501 ret = recv (desc->fd, buffer, length, flags);
503 if (SOCKET_ERROR == ret)
504 SetErrnoFromWinsockError (WSAGetLastError ());
511 * Send data (always non-blocking).
514 * @param buffer data to send
515 * @param length size of the buffer
516 * @return number of bytes sent, GNUNET_SYSERR on error
519 GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Handle * desc,
520 const void *buffer, size_t length)
527 flags |= MSG_DONTWAIT;
531 flags |= MSG_NOSIGNAL;
534 ret = send (desc->fd, buffer, length, flags);
537 if (SOCKET_ERROR == ret)
538 SetErrnoFromWinsockError (WSAGetLastError ());
546 * Send data to a particular destination (always non-blocking).
547 * This function only works for UDP sockets.
550 * @param message data to send
551 * @param length size of the data
552 * @param dest_addr destination address
553 * @param dest_len length of address
554 * @return number of bytes sent, GNUNET_SYSERR on error
557 GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Handle * desc,
558 const void *message, size_t length,
559 const struct sockaddr * dest_addr,
567 flags |= MSG_DONTWAIT;
570 flags |= MSG_NOSIGNAL;
572 ret = sendto (desc->fd, message, length, flags, dest_addr, dest_len);
574 if (SOCKET_ERROR == ret)
575 SetErrnoFromWinsockError (WSAGetLastError ());
584 * @param level protocol level of the option
585 * @param option_name option identifier
586 * @param option_value value to set
587 * @param option_len size of option_value
588 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
591 GNUNET_NETWORK_socket_setsockopt (struct GNUNET_NETWORK_Handle *fd,
592 int level, int option_name,
593 const void *option_value,
594 socklen_t option_len)
598 ret = setsockopt (fd->fd, level, option_name, option_value, option_len);
600 if (SOCKET_ERROR == ret)
601 SetErrnoFromWinsockError (WSAGetLastError ());
603 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
608 * Create a new socket. Configure it for non-blocking IO and
609 * mark it as non-inheritable to child processes (set the
610 * close-on-exec flag).
612 * @param domain domain of the socket
613 * @param type socket type
614 * @param protocol network protocol
615 * @return new socket, NULL on error
617 struct GNUNET_NETWORK_Handle *
618 GNUNET_NETWORK_socket_create (int domain, int type, int protocol)
620 struct GNUNET_NETWORK_Handle *ret;
621 ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle));
623 ret->fd = socket (domain, type, protocol);
624 if (INVALID_SOCKET == ret->fd)
627 SetErrnoFromWinsockError (WSAGetLastError ());
634 if (ret->fd >= FD_SETSIZE)
636 GNUNET_break (0 == close (ret->fd));
643 if (GNUNET_SYSERR == socket_set_blocking (ret, GNUNET_NO))
645 /* we might want to treat this one as fatal... */
647 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret));
652 if (GNUNET_OK != socket_set_inheritable (ret))
653 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
654 "socket_set_inheritable");
657 socket_set_nosigpipe (ret);
659 if ( (type == SOCK_STREAM)
661 && (domain != AF_UNIX)
664 socket_set_nodelay (ret);
670 * Shut down socket operations
672 * @param how type of shutdown
673 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
676 GNUNET_NETWORK_socket_shutdown (struct GNUNET_NETWORK_Handle *desc, int how)
680 ret = shutdown (desc->fd, how);
683 SetErrnoFromWinsockError (WSAGetLastError ());
685 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
694 GNUNET_NETWORK_fdset_zero (struct GNUNET_NETWORK_FDSet *fds)
699 GNUNET_CONTAINER_slist_clear (fds->handles);
704 * Add a socket to the FD set
706 * @param desc socket to add
709 GNUNET_NETWORK_fdset_set (struct GNUNET_NETWORK_FDSet *fds,
710 const struct GNUNET_NETWORK_Handle *desc)
712 FD_SET (desc->fd, &fds->sds);
713 if (desc->fd + 1 > fds->nsds)
714 fds->nsds = desc->fd + 1;
719 * Check whether a socket is part of the fd set
722 * @return 0 if the FD is not set
725 GNUNET_NETWORK_fdset_isset (const struct GNUNET_NETWORK_FDSet *fds,
726 const struct GNUNET_NETWORK_Handle *desc)
728 return FD_ISSET (desc->fd, &fds->sds);
733 * Add one fd set to another
734 * @param dst the fd set to add to
735 * @param src the fd set to add from
738 GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst,
739 const struct GNUNET_NETWORK_FDSet *src)
742 for (nfds = src->nsds; nfds > 0; nfds--)
743 if (FD_ISSET (nfds, &src->sds))
746 FD_SET (nfds, &dst->sds);
747 if (nfds + 1 > dst->nsds)
748 dst->nsds = nfds + 1;
751 GNUNET_CONTAINER_slist_append (dst->handles, src->handles);
757 * Copy one fd set to another
759 * @param to destination
763 GNUNET_NETWORK_fdset_copy (struct GNUNET_NETWORK_FDSet *to,
764 const struct GNUNET_NETWORK_FDSet *from)
766 FD_COPY (&from->sds, &to->sds);
767 to->nsds = from->nsds;
770 GNUNET_CONTAINER_slist_clear (to->handles);
771 GNUNET_CONTAINER_slist_append (to->handles, from->handles);
776 GNUNET_NETWORK_get_fd (struct GNUNET_NETWORK_Handle *desc)
782 * Copy a native fd set
784 * @param to destination
785 * @param from native source set
786 * @param nfds the biggest socket number in from + 1
789 GNUNET_NETWORK_fdset_copy_native (struct GNUNET_NETWORK_FDSet *to,
790 const fd_set * from, int nfds)
792 FD_COPY (from, &to->sds);
798 * Set a native fd in a set
800 * @param to destination
801 * @param nfd native FD to set
803 void GNUNET_NETWORK_fdset_set_native (struct GNUNET_NETWORK_FDSet *to,
806 FD_SET (nfd, &to->sds);
807 to->nsds = GNUNET_MAX (nfd + 1, to->nsds);
812 * Test native fd in a set
814 * @param to set to test, NULL for empty set
815 * @param nfd native FD to test, or -1 for none
816 * @return GNUNET_YES if FD is set in the set
819 GNUNET_NETWORK_fdset_test_native (const struct GNUNET_NETWORK_FDSet *to,
822 if ( (nfd == -1) || (to == NULL) )
824 return FD_ISSET (nfd, &to->sds) ? GNUNET_YES : GNUNET_NO;
829 * Add a file handle to the fd set
831 * @param h the file handle to add
834 GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds,
835 const struct GNUNET_DISK_FileHandle *h)
839 GNUNET_DISK_internal_file_handle_ (h, &hw, sizeof (HANDLE));
840 GNUNET_CONTAINER_slist_add (fds->handles,
841 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
842 &hw, sizeof (HANDLE));
846 GNUNET_DISK_internal_file_handle_ (h, &fd, sizeof (int));
847 FD_SET (fd, &fds->sds);
848 if (fd + 1 > fds->nsds)
857 * Add a W32 file handle to the fd set
859 * @param h the file handle to add
862 GNUNET_NETWORK_fdset_handle_set_native_w32_handle (struct GNUNET_NETWORK_FDSet *fds,
865 GNUNET_CONTAINER_slist_add (fds->handles,
866 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
867 &h, sizeof (HANDLE));
873 * Check if a file handle is part of an fd set
875 * @param h file handle
876 * @return GNUNET_YES if the file handle is part of the set
879 GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds,
880 const struct GNUNET_DISK_FileHandle *h)
884 return GNUNET_CONTAINER_slist_contains (fds->handles, &h->h,
887 return FD_ISSET (h->fd, &fds->sds);
893 * Checks if two fd sets overlap
894 * @param fds1 first fd set
895 * @param fds2 second fd set
896 * @return GNUNET_YES if they do overlap, GNUNET_NO otherwise
899 GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1,
900 const struct GNUNET_NETWORK_FDSet *fds2)
904 if (nfds < fds2->nsds)
906 for (; nfds >= 0; nfds--)
907 if (FD_ISSET (nfds, &fds1->sds) && FD_ISSET (nfds, &fds2->sds))
911 struct GNUNET_CONTAINER_SList_Iterator *it;
913 for (it = GNUNET_CONTAINER_slist_begin (fds1->handles);
914 GNUNET_CONTAINER_slist_end (it) != GNUNET_YES;
915 GNUNET_CONTAINER_slist_next (it))
919 h = (HANDLE *) GNUNET_CONTAINER_slist_get ((const struct GNUNET_CONTAINER_SList_Iterator *)it, NULL);
920 if (GNUNET_CONTAINER_slist_contains
921 (fds2->handles, h, sizeof (HANDLE)))
923 GNUNET_CONTAINER_slist_iter_destroy (it);
927 GNUNET_CONTAINER_slist_iter_destroy (it);
936 * @return a new fd set
938 struct GNUNET_NETWORK_FDSet *
939 GNUNET_NETWORK_fdset_create ()
941 struct GNUNET_NETWORK_FDSet *fds;
942 fds = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_FDSet));
944 fds->handles = GNUNET_CONTAINER_slist_create ();
946 GNUNET_NETWORK_fdset_zero (fds);
952 * Releases the associated memory of an fd set
956 GNUNET_NETWORK_fdset_destroy (struct GNUNET_NETWORK_FDSet *fds)
959 GNUNET_CONTAINER_slist_destroy (fds->handles);
965 * Check if sockets meet certain conditions
966 * @param rfds set of sockets to be checked for readability
967 * @param wfds set of sockets to be checked for writability
968 * @param efds set of sockets to be checked for exceptions
969 * @param timeout relative value when to return
970 * @return number of selected sockets, GNUNET_SYSERR on error
973 GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
974 struct GNUNET_NETWORK_FDSet *wfds,
975 struct GNUNET_NETWORK_FDSet *efds,
976 const struct GNUNET_TIME_Relative timeout)
990 handles = GNUNET_CONTAINER_slist_count (rfds->handles);
995 nfds = GNUNET_MAX (nfds, wfds->nsds);
997 handles += GNUNET_CONTAINER_slist_count (wfds->handles);
1002 nfds = GNUNET_MAX (nfds, efds->nsds);
1004 handles += GNUNET_CONTAINER_slist_count (efds->handles);
1009 tv.tv_sec = timeout.value / GNUNET_TIME_UNIT_SECONDS.value;
1011 1000 * (timeout.value - (tv.tv_sec * GNUNET_TIME_UNIT_SECONDS.value));
1012 if ((nfds == 0) && (timeout.value == GNUNET_TIME_UNIT_FOREVER_REL.value)
1018 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1020 ("Fatal internal logic error, process hangs in `%s' (abort with CTRL-C)!\n"),
1025 return select (nfds,
1026 (rfds != NULL) ? &rfds->sds : NULL,
1027 (wfds != NULL) ? &wfds->sds : NULL,
1028 (efds != NULL) ? &efds->sds : NULL,
1029 (timeout.value == GNUNET_TIME_UNIT_FOREVER_REL.value)
1034 fd_set sock_read, sock_write, sock_except;
1035 fd_set aread, awrite, aexcept;
1036 struct GNUNET_CONTAINER_SList *handles_read, *handles_write,
1040 struct timeval tvslice;
1044 #define SAFE_FD_ISSET(fd, set) (set != NULL && FD_ISSET(fd, set))
1046 /* calculate how long we need to wait in milliseconds */
1047 if (timeout.value == GNUNET_TIME_UNIT_FOREVER_REL.value)
1048 ms_total = INFINITE;
1051 ms_total = timeout.value / GNUNET_TIME_UNIT_MILLISECONDS.value;
1053 /* select() may be used as a portable way to sleep */
1054 if (!(rfds || wfds || efds))
1061 handles_read = GNUNET_CONTAINER_slist_create ();
1062 handles_write = GNUNET_CONTAINER_slist_create ();
1063 handles_except = GNUNET_CONTAINER_slist_create ();
1066 sock_read = rfds->sds;
1068 FD_ZERO (&sock_read);
1070 sock_write = wfds->sds;
1072 FD_ZERO (&sock_write);
1074 sock_except = efds->sds;
1076 FD_ZERO (&sock_except);
1078 /* multiplex between winsock select() and waiting on the handles */
1082 limit = GetTickCount () + ms_total;
1091 /* overwrite the zero'd sets here; the select call
1092 * will clear those that are not active */
1093 FD_COPY (&sock_read, &aread);
1094 FD_COPY (&sock_write, &awrite);
1095 FD_COPY (&sock_except, &aexcept);
1097 tvslice.tv_usec = 100000;
1099 select (nfds + 1, &aread, &awrite, &aexcept,
1100 &tvslice)) == SOCKET_ERROR)
1103 SetErrnoFromWinsockError (WSAGetLastError ());
1104 if (errno == ENOTSOCK)
1108 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "select");
1111 goto select_loop_end;
1115 /* Poll read pipes */
1119 struct GNUNET_CONTAINER_SList_Iterator *i;
1120 for (i = GNUNET_CONTAINER_slist_begin (rfds->handles);
1121 GNUNET_CONTAINER_slist_end (i) != GNUNET_YES;
1122 GNUNET_CONTAINER_slist_next (i))
1127 h = *(HANDLE *) GNUNET_CONTAINER_slist_get (i, NULL);
1128 if (!PeekNamedPipe (h, NULL, 0, NULL, &dwBytes, NULL))
1131 SetErrnoFromWinError (GetLastError ());
1134 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
1138 goto select_loop_end;
1143 GNUNET_CONTAINER_slist_add (handles_read,
1144 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1145 &h, sizeof (HANDLE));
1149 GNUNET_CONTAINER_slist_iter_destroy (i);
1152 /* Poll for faulty pipes */
1156 struct GNUNET_CONTAINER_SList_Iterator *i;
1157 for (i = GNUNET_CONTAINER_slist_begin (efds->handles);
1158 GNUNET_CONTAINER_slist_end (i) != GNUNET_YES;
1159 GNUNET_CONTAINER_slist_next (i))
1164 h = *(HANDLE *) GNUNET_CONTAINER_slist_get (i, NULL);
1165 if (!PeekNamedPipe (h, NULL, 0, NULL, &dwBytes, NULL))
1168 GNUNET_CONTAINER_slist_add (handles_except,
1169 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1170 &h, sizeof (HANDLE));
1174 GNUNET_CONTAINER_slist_iter_destroy (i);
1179 GNUNET_CONTAINER_slist_append (handles_write, wfds->handles);
1180 retcode += GNUNET_CONTAINER_slist_count (wfds->handles);
1183 /* Check for closed sockets */
1184 for (i = 0; i < nfds; i++)
1187 if (SAFE_FD_ISSET (i, &sock_read))
1190 struct sockaddr addr;
1192 if (getpeername (i, &addr, &len) == SOCKET_ERROR)
1198 (i, SOL_SOCKET, SO_ERROR, (char *) &err, &len) == 0
1199 && err == WSAENOTCONN)
1202 if (!SAFE_FD_ISSET (i, &aread))
1213 if (retcode == 0 && nfds == 0)
1214 Sleep (GNUNET_MIN (100, limit - GetTickCount ()));
1216 while (retcode == 0 && (ms_total == INFINITE || GetTickCount () < limit));
1222 GNUNET_NETWORK_fdset_zero (rfds);
1223 GNUNET_NETWORK_fdset_copy_native (rfds, &aread, retcode);
1224 GNUNET_CONTAINER_slist_clear (rfds->handles);
1225 GNUNET_CONTAINER_slist_append (rfds->handles, handles_read);
1229 GNUNET_NETWORK_fdset_zero (wfds);
1230 GNUNET_NETWORK_fdset_copy_native (wfds, &awrite, retcode);
1231 GNUNET_CONTAINER_slist_clear (wfds->handles);
1232 GNUNET_CONTAINER_slist_append (wfds->handles, handles_write);
1236 GNUNET_NETWORK_fdset_zero (efds);
1237 GNUNET_NETWORK_fdset_copy_native (efds, &aexcept, retcode);
1238 GNUNET_CONTAINER_slist_clear (efds->handles);
1239 GNUNET_CONTAINER_slist_append (efds->handles, handles_except);
1243 GNUNET_CONTAINER_slist_destroy (handles_read);
1244 GNUNET_CONTAINER_slist_destroy (handles_write);
1245 GNUNET_CONTAINER_slist_destroy (handles_except);
1252 /* end of network.c */