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
51 struct GNUNET_NETWORK_FDSet
55 * Maximum number of any socket socket descriptor in the set
60 * Bitset with the descriptors.
66 * Linked list of handles
68 struct GNUNET_CONTAINER_SList *handles;
74 #define FD_COPY(s, d) (memcpy ((d), (s), sizeof (fd_set)))
79 * Set if a socket should use blocking or non-blocking IO.
81 * @param doBlock blocking mode
82 * @return GNUNET_OK on success, GNUNET_SYSERR on error
85 socket_set_blocking (struct GNUNET_NETWORK_Handle *fd, int doBlock)
91 if (ioctlsocket (fd->fd, FIONBIO, &mode) == SOCKET_ERROR)
94 SetErrnoFromWinsockError (WSAGetLastError ());
95 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "ioctlsocket");
102 int flags = fcntl (fd->fd, F_GETFL);
106 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "fcntl");
107 return GNUNET_SYSERR;
110 flags &= ~O_NONBLOCK;
114 if (0 != fcntl (fd->fd, F_SETFL, flags))
117 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "fcntl");
118 return GNUNET_SYSERR;
127 * Make a socket non-inheritable to child processes
129 * @param h the socket to make non-inheritable
130 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
131 * @warning Not implemented on Windows
134 socket_set_inheritable (const struct GNUNET_NETWORK_Handle *h)
138 i = fcntl (h->fd, F_GETFD);
140 return GNUNET_SYSERR;
141 if (i == (i | FD_CLOEXEC))
144 if (fcntl (h->fd, F_SETFD, i) < 0)
145 return GNUNET_SYSERR;
153 * The MSG_NOSIGNAL equivalent on Mac OS X
155 * @param h the socket to make non-delaying
158 socket_set_nosigpipe (const struct GNUNET_NETWORK_Handle *h)
162 setsockopt (h->fd, SOL_SOCKET, SO_NOSIGPIPE, &value, sizeof (value)))
163 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
169 * Disable delays when sending data via the socket.
170 * (GNUnet makes sure that messages are as big as
173 * @param h the socket to make non-delaying
176 socket_set_nodelay (const struct GNUNET_NETWORK_Handle *h)
180 setsockopt (h->fd, IPPROTO_TCP, TCP_NODELAY, &value, sizeof (value)))
181 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
186 * accept a new connection on a socket
188 * @param desc bound socket
189 * @param address address of the connecting peer, may be NULL
190 * @param address_len length of address
191 * @return client socket
193 struct GNUNET_NETWORK_Handle *
194 GNUNET_NETWORK_socket_accept (const struct GNUNET_NETWORK_Handle *desc,
195 struct sockaddr *address,
196 socklen_t * address_len)
198 struct GNUNET_NETWORK_Handle *ret;
200 ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle));
201 ret->fd = accept (desc->fd, address, address_len);
202 if (ret->fd == INVALID_SOCKET)
205 SetErrnoFromWinsockError (WSAGetLastError ());
211 if (ret->fd >= FD_SETSIZE)
213 GNUNET_break (0 == close (ret->fd));
219 if (GNUNET_SYSERR == socket_set_blocking (ret, GNUNET_NO))
223 /* we might want to treat this one as fatal... */
225 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret));
230 if (GNUNET_OK != socket_set_inheritable (ret))
231 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
232 "socket_set_inheritable");
235 socket_set_nosigpipe (ret);
238 if (address->sa_family != AF_UNIX)
240 socket_set_nodelay (ret);
246 * Bind to a connected socket
248 * @param address address to be bound
249 * @param address_len length of address
250 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
253 GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc,
254 const struct sockaddr *address,
255 socklen_t address_len)
259 ret = bind (desc->fd, address, address_len);
261 if (SOCKET_ERROR == ret)
262 SetErrnoFromWinsockError (WSAGetLastError ());
265 if ( (ret == 0) && (address->sa_family == AF_UNIX))
267 const struct sockaddr_un *un = (const struct sockaddr_un*) address;
268 if (0 != unlink (un->sun_path))
269 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
275 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
282 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
285 GNUNET_NETWORK_socket_close (struct GNUNET_NETWORK_Handle *desc)
290 ret = closesocket (desc->fd);
291 SetErrnoFromWinsockError (WSAGetLastError ());
293 ret = close (desc->fd);
296 return (ret == 0) ? GNUNET_OK : GNUNET_SYSERR;
301 * Box a native socket (and check that it is a socket).
303 * @param fd socket to box
304 * @return NULL on error (including not supported on target platform)
306 struct GNUNET_NETWORK_Handle *
307 GNUNET_NETWORK_socket_box_native (int fd)
312 struct GNUNET_NETWORK_Handle *ret;
314 if (fcntl (fd, F_GETFD) < 0)
315 return NULL; /* invalid FD */
316 ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle));
326 * @param address peer address
327 * @param address_len length of address
328 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
331 GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Handle *desc,
332 const struct sockaddr *address,
333 socklen_t address_len)
336 ret = connect (desc->fd, address, address_len);
339 if (SOCKET_ERROR == ret)
342 SetErrnoFromWinsockError (WSAGetLastError ());
343 if (errno == EWOULDBLOCK)
347 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
355 * @param level protocol level of the option
356 * @param optname identifier of the option
357 * @param optval options
358 * @param optlen length of optval
359 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
362 GNUNET_NETWORK_socket_getsockopt (const struct GNUNET_NETWORK_Handle *desc,
363 int level, int optname, void *optval,
367 ret = getsockopt (desc->fd, level, optname, optval, optlen);
370 if (ret == 0 && level == SOL_SOCKET && optname == SO_ERROR)
371 *((int *) optval) = GetErrnoFromWinsockError (*((int *) optval));
373 else if (SOCKET_ERROR == ret)
374 SetErrnoFromWinsockError (WSAGetLastError ());
377 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
384 * @param backlog length of the listen queue
385 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
388 GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Handle *desc,
392 ret = listen (desc->fd, backlog);
395 if (SOCKET_ERROR == ret)
396 SetErrnoFromWinsockError (WSAGetLastError ());
399 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
404 * How much data is available to be read on this descriptor?
406 * Returns GNUNET_NO if no data is available, or on error!
410 GNUNET_NETWORK_socket_recvfrom_amount (const struct GNUNET_NETWORK_Handle
416 /* How much is there to be read? */
418 error = ioctl (desc->fd, FIONREAD, &pending);
421 error = ioctlsocket (desc->fd, FIONREAD, &pending);
422 if (error != SOCKET_ERROR)
430 * Read data from a connected socket (always non-blocking).
432 * @param buffer buffer
433 * @param length length of buffer
434 * @param src_addr either the source to recv from, or all zeroes
435 * to be filled in by recvfrom
436 * @param addrlen length of the addr
439 GNUNET_NETWORK_socket_recvfrom (const struct GNUNET_NETWORK_Handle * desc,
440 void *buffer, size_t length,
441 struct sockaddr * src_addr,
449 flags |= MSG_DONTWAIT;
452 ret = recvfrom (desc->fd, buffer, length, flags, src_addr, addrlen);
454 if (SOCKET_ERROR == ret)
455 SetErrnoFromWinsockError (WSAGetLastError ());
462 * Read data from a connected socket (always non-blocking).
464 * @param buffer buffer
465 * @param length length of buffer
468 GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Handle * desc,
469 void *buffer, size_t length)
476 flags |= MSG_DONTWAIT;
478 ret = recv (desc->fd, buffer, length, flags);
480 if (SOCKET_ERROR == ret)
481 SetErrnoFromWinsockError (WSAGetLastError ());
488 * Send data (always non-blocking).
491 * @param buffer data to send
492 * @param length size of the buffer
493 * @return number of bytes sent, GNUNET_SYSERR on error
496 GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Handle * desc,
497 const void *buffer, size_t length)
504 flags |= MSG_DONTWAIT;
508 flags |= MSG_NOSIGNAL;
511 ret = send (desc->fd, buffer, length, flags);
514 if (SOCKET_ERROR == ret)
515 SetErrnoFromWinsockError (WSAGetLastError ());
523 * Send data to a particular destination (always non-blocking).
524 * This function only works for UDP sockets.
527 * @param message data to send
528 * @param length size of the data
529 * @param dest_addr destination address
530 * @param dest_len length of address
531 * @return number of bytes sent, GNUNET_SYSERR on error
534 GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Handle * desc,
535 const void *message, size_t length,
536 const struct sockaddr * dest_addr,
544 flags |= MSG_DONTWAIT;
547 flags |= MSG_NOSIGNAL;
549 ret = sendto (desc->fd, message, length, flags, dest_addr, dest_len);
551 if (SOCKET_ERROR == ret)
552 SetErrnoFromWinsockError (WSAGetLastError ());
561 * @param level protocol level of the option
562 * @param option_name option identifier
563 * @param option_value value to set
564 * @param option_len size of option_value
565 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
568 GNUNET_NETWORK_socket_setsockopt (struct GNUNET_NETWORK_Handle *fd,
569 int level, int option_name,
570 const void *option_value,
571 socklen_t option_len)
575 ret = setsockopt (fd->fd, level, option_name, option_value, option_len);
577 if (SOCKET_ERROR == ret)
578 SetErrnoFromWinsockError (WSAGetLastError ());
580 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
585 * Create a new socket. Configure it for non-blocking IO and
586 * mark it as non-inheritable to child processes (set the
587 * close-on-exec flag).
589 * @param domain domain of the socket
590 * @param type socket type
591 * @param protocol network protocol
592 * @return new socket, NULL on error
594 struct GNUNET_NETWORK_Handle *
595 GNUNET_NETWORK_socket_create (int domain, int type, int protocol)
597 struct GNUNET_NETWORK_Handle *ret;
598 ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle));
599 ret->fd = socket (domain, type, protocol);
600 if (INVALID_SOCKET == ret->fd)
603 SetErrnoFromWinsockError (WSAGetLastError ());
610 if (ret->fd >= FD_SETSIZE)
612 GNUNET_break (0 == close (ret->fd));
619 if (GNUNET_SYSERR == socket_set_blocking (ret, GNUNET_NO))
621 /* we might want to treat this one as fatal... */
623 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret));
628 if (GNUNET_OK != socket_set_inheritable (ret))
629 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
630 "socket_set_inheritable");
633 socket_set_nosigpipe (ret);
635 if ( (type == SOCK_STREAM)
637 && (domain != AF_UNIX)
640 socket_set_nodelay (ret);
646 * Shut down socket operations
648 * @param how type of shutdown
649 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
652 GNUNET_NETWORK_socket_shutdown (struct GNUNET_NETWORK_Handle *desc, int how)
656 ret = shutdown (desc->fd, how);
659 SetErrnoFromWinsockError (WSAGetLastError ());
661 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
670 GNUNET_NETWORK_fdset_zero (struct GNUNET_NETWORK_FDSet *fds)
675 GNUNET_CONTAINER_slist_clear (fds->handles);
680 * Add a socket to the FD set
682 * @param desc socket to add
685 GNUNET_NETWORK_fdset_set (struct GNUNET_NETWORK_FDSet *fds,
686 const struct GNUNET_NETWORK_Handle *desc)
688 FD_SET (desc->fd, &fds->sds);
689 if (desc->fd + 1 > fds->nsds)
690 fds->nsds = desc->fd + 1;
695 * Check whether a socket is part of the fd set
698 * @return 0 if the FD is not set
701 GNUNET_NETWORK_fdset_isset (const struct GNUNET_NETWORK_FDSet *fds,
702 const struct GNUNET_NETWORK_Handle *desc)
704 return FD_ISSET (desc->fd, &fds->sds);
709 * Add one fd set to another
710 * @param dst the fd set to add to
711 * @param src the fd set to add from
714 GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst,
715 const struct GNUNET_NETWORK_FDSet *src)
718 for (nfds = src->nsds; nfds > 0; nfds--)
719 if (FD_ISSET (nfds, &src->sds))
722 FD_SET (nfds, &dst->sds);
723 if (nfds + 1 > dst->nsds)
724 dst->nsds = nfds + 1;
727 GNUNET_CONTAINER_slist_append (dst->handles, src->handles);
733 * Copy one fd set to another
735 * @param to destination
739 GNUNET_NETWORK_fdset_copy (struct GNUNET_NETWORK_FDSet *to,
740 const struct GNUNET_NETWORK_FDSet *from)
742 FD_COPY (&from->sds, &to->sds);
743 to->nsds = from->nsds;
746 GNUNET_CONTAINER_slist_clear (to->handles);
747 GNUNET_CONTAINER_slist_append (to->handles, from->handles);
752 GNUNET_NETWORK_get_fd (struct GNUNET_NETWORK_Handle *desc)
758 * Copy a native fd set
760 * @param to destination
761 * @param from native source set
762 * @param nfds the biggest socket number in from + 1
765 GNUNET_NETWORK_fdset_copy_native (struct GNUNET_NETWORK_FDSet *to,
766 const fd_set * from, int nfds)
768 FD_COPY (from, &to->sds);
774 * Set a native fd in a set
776 * @param to destination
777 * @param nfd native FD to set
779 void GNUNET_NETWORK_fdset_set_native (struct GNUNET_NETWORK_FDSet *to,
782 FD_SET (nfd, &to->sds);
783 to->nsds = GNUNET_MAX (nfd + 1, to->nsds);
788 * Test native fd in a set
790 * @param to set to test, NULL for empty set
791 * @param nfd native FD to test, or -1 for none
792 * @return GNUNET_YES if FD is set in the set
795 GNUNET_NETWORK_fdset_test_native (const struct GNUNET_NETWORK_FDSet *to,
798 if ( (nfd == -1) || (to == NULL) )
800 return FD_ISSET (nfd, &to->sds) ? GNUNET_YES : GNUNET_NO;
805 * Add a file handle to the fd set
807 * @param h the file handle to add
810 GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds,
811 const struct GNUNET_DISK_FileHandle *h)
816 GNUNET_DISK_internal_file_handle_ (h, &hw, sizeof (HANDLE));
817 GNUNET_CONTAINER_slist_add (fds->handles,
818 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
819 &hw, sizeof (HANDLE));
823 GNUNET_DISK_internal_file_handle_ (h, &fd, sizeof (int));
824 FD_SET (fd, &fds->sds);
825 if (fd + 1 > fds->nsds)
833 * Check if a file handle is part of an fd set
835 * @param h file handle
836 * @return GNUNET_YES if the file handle is part of the set
839 GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds,
840 const struct GNUNET_DISK_FileHandle *h)
844 return GNUNET_CONTAINER_slist_contains (fds->handles, &h->h,
847 return FD_ISSET (h->fd, &fds->sds);
853 * Checks if two fd sets overlap
854 * @param fds1 first fd set
855 * @param fds2 second fd set
856 * @return GNUNET_YES if they do overlap, GNUNET_NO otherwise
859 GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1,
860 const struct GNUNET_NETWORK_FDSet *fds2)
864 if (nfds < fds2->nsds)
866 for (; nfds >= 0; nfds--)
867 if (FD_ISSET (nfds, &fds1->sds) && FD_ISSET (nfds, &fds2->sds))
871 struct GNUNET_CONTAINER_SList_Iterator *it;
873 for (it = GNUNET_CONTAINER_slist_begin (fds1->handles);
874 GNUNET_CONTAINER_slist_end (it) != GNUNET_YES;
875 GNUNET_CONTAINER_slist_next (it))
879 h = GNUNET_CONTAINER_slist_get (it, NULL);
880 if (GNUNET_CONTAINER_slist_contains
881 (fds2->handles, h, sizeof (HANDLE)))
883 GNUNET_CONTAINER_slist_iter_destroy (it);
887 GNUNET_CONTAINER_slist_iter_destroy (it);
896 * @return a new fd set
898 struct GNUNET_NETWORK_FDSet *
899 GNUNET_NETWORK_fdset_create ()
901 struct GNUNET_NETWORK_FDSet *fds;
902 fds = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_FDSet));
904 fds->handles = GNUNET_CONTAINER_slist_create ();
906 GNUNET_NETWORK_fdset_zero (fds);
912 * Releases the associated memory of an fd set
916 GNUNET_NETWORK_fdset_destroy (struct GNUNET_NETWORK_FDSet *fds)
919 GNUNET_CONTAINER_slist_destroy (fds->handles);
925 * Check if sockets meet certain conditions
926 * @param rfds set of sockets to be checked for readability
927 * @param wfds set of sockets to be checked for writability
928 * @param efds set of sockets to be checked for exceptions
929 * @param timeout relative value when to return
930 * @return number of selected sockets, GNUNET_SYSERR on error
933 GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
934 struct GNUNET_NETWORK_FDSet *wfds,
935 struct GNUNET_NETWORK_FDSet *efds,
936 const struct GNUNET_TIME_Relative timeout)
950 handles = GNUNET_CONTAINER_slist_count (rfds->handles);
955 nfds = GNUNET_MAX (nfds, wfds->nsds);
957 handles += GNUNET_CONTAINER_slist_count (wfds->handles);
962 nfds = GNUNET_MAX (nfds, efds->nsds);
964 handles += GNUNET_CONTAINER_slist_count (efds->handles);
969 tv.tv_sec = timeout.value / GNUNET_TIME_UNIT_SECONDS.value;
971 1000 * (timeout.value - (tv.tv_sec * GNUNET_TIME_UNIT_SECONDS.value));
972 if ((nfds == 0) && (timeout.value == GNUNET_TIME_UNIT_FOREVER_REL.value)
978 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
980 ("Fatal internal logic error, process hangs in `%s' (abort with CTRL-C)!\n"),
985 return select (nfds + 1,
986 (rfds != NULL) ? &rfds->sds : NULL,
987 (wfds != NULL) ? &wfds->sds : NULL,
988 (efds != NULL) ? &efds->sds : NULL,
989 (timeout.value == GNUNET_TIME_UNIT_FOREVER_REL.value)
994 fd_set sock_read, sock_write, sock_except;
995 fd_set aread, awrite, aexcept;
996 struct GNUNET_CONTAINER_SList *handles_read, *handles_write,
1000 struct timeval tvslice;
1004 #define SAFE_FD_ISSET(fd, set) (set != NULL && FD_ISSET(fd, set))
1006 /* calculate how long we need to wait in milliseconds */
1007 if (timeout.value == GNUNET_TIME_UNIT_FOREVER_REL.value)
1008 ms_total = INFINITE;
1011 ms_total = timeout.value / GNUNET_TIME_UNIT_MILLISECONDS.value;
1013 /* select() may be used as a portable way to sleep */
1014 if (!(rfds || wfds || efds))
1021 handles_read = GNUNET_CONTAINER_slist_create ();
1022 handles_write = GNUNET_CONTAINER_slist_create ();
1023 handles_except = GNUNET_CONTAINER_slist_create ();
1026 sock_read = rfds->sds;
1028 FD_ZERO (&sock_read);
1030 sock_write = wfds->sds;
1032 FD_ZERO (&sock_write);
1034 sock_except = efds->sds;
1036 FD_ZERO (&sock_except);
1038 /* multiplex between winsock select() and waiting on the handles */
1042 limit = GetTickCount () + ms_total;
1051 /* overwrite the zero'd sets here; the select call
1052 * will clear those that are not active */
1053 FD_COPY (&sock_read, &aread);
1054 FD_COPY (&sock_write, &awrite);
1055 FD_COPY (&sock_except, &aexcept);
1057 tvslice.tv_usec = 100000;
1059 select (nfds + 1, &aread, &awrite, &aexcept,
1060 &tvslice)) == SOCKET_ERROR)
1063 SetErrnoFromWinsockError (WSAGetLastError ());
1064 if (errno == ENOTSOCK)
1068 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "select");
1071 goto select_loop_end;
1075 /* Poll read pipes */
1079 struct GNUNET_CONTAINER_SList_Iterator *i;
1080 for (i = GNUNET_CONTAINER_slist_begin (rfds->handles);
1081 GNUNET_CONTAINER_slist_end (i) != GNUNET_YES;
1082 GNUNET_CONTAINER_slist_next (i))
1087 h = *(HANDLE *) GNUNET_CONTAINER_slist_get (i, NULL);
1088 if (!PeekNamedPipe (h, NULL, 0, NULL, &dwBytes, NULL))
1091 SetErrnoFromWinError (GetLastError ());
1094 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
1098 goto select_loop_end;
1103 GNUNET_CONTAINER_slist_add (handles_read,
1104 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1105 &h, sizeof (HANDLE));
1109 GNUNET_CONTAINER_slist_iter_destroy (i);
1112 /* Poll for faulty pipes */
1116 struct GNUNET_CONTAINER_SList_Iterator *i;
1117 for (i = GNUNET_CONTAINER_slist_begin (efds->handles);
1118 GNUNET_CONTAINER_slist_end (i) != GNUNET_YES;
1119 GNUNET_CONTAINER_slist_next (i))
1124 h = *(HANDLE *) GNUNET_CONTAINER_slist_get (i, NULL);
1125 if (!PeekNamedPipe (h, NULL, 0, NULL, &dwBytes, NULL))
1128 GNUNET_CONTAINER_slist_add (handles_except,
1129 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1130 &h, sizeof (HANDLE));
1134 GNUNET_CONTAINER_slist_iter_destroy (i);
1139 GNUNET_CONTAINER_slist_append (handles_write, wfds->handles);
1140 retcode += GNUNET_CONTAINER_slist_count (wfds->handles);
1143 /* Check for closed sockets */
1144 for (i = 0; i < nfds; i++)
1147 if (SAFE_FD_ISSET (i, &sock_read))
1150 struct sockaddr addr;
1152 if (getpeername (i, &addr, &len) == SOCKET_ERROR)
1158 (i, SOL_SOCKET, SO_ERROR, (char *) &err, &len) == 0
1159 && err == WSAENOTCONN)
1162 if (!SAFE_FD_ISSET (i, &aread))
1173 if (retcode == 0 && nfds == 0)
1174 Sleep (GNUNET_MIN (100, limit - GetTickCount ()));
1176 while (retcode == 0 && (ms_total == INFINITE || GetTickCount () < limit));
1182 GNUNET_NETWORK_fdset_zero (rfds);
1183 GNUNET_NETWORK_fdset_copy_native (rfds, &aread, retcode);
1184 GNUNET_CONTAINER_slist_clear (rfds->handles);
1185 GNUNET_CONTAINER_slist_append (rfds->handles, handles_read);
1189 GNUNET_NETWORK_fdset_zero (wfds);
1190 GNUNET_NETWORK_fdset_copy_native (wfds, &awrite, retcode);
1191 GNUNET_CONTAINER_slist_clear (wfds->handles);
1192 GNUNET_CONTAINER_slist_append (wfds->handles, handles_write);
1196 GNUNET_NETWORK_fdset_zero (efds);
1197 GNUNET_NETWORK_fdset_copy_native (efds, &aexcept, retcode);
1198 GNUNET_CONTAINER_slist_clear (efds->handles);
1199 GNUNET_CONTAINER_slist_append (efds->handles, handles_except);
1203 GNUNET_CONTAINER_slist_destroy (handles_read);
1204 GNUNET_CONTAINER_slist_destroy (handles_write);
1205 GNUNET_CONTAINER_slist_destroy (handles_except);
1212 /* end of network.c */