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
46 * For UNIX domain listen sockets, underlying filename to be removed
59 struct GNUNET_NETWORK_FDSet
63 * Maximum number of any socket socket descriptor in the set
68 * Bitset with the descriptors.
74 * Linked list of handles
76 struct GNUNET_CONTAINER_SList *handles;
82 #define FD_COPY(s, d) (memcpy ((d), (s), sizeof (fd_set)))
87 * Set if a socket should use blocking or non-blocking IO.
89 * @param doBlock blocking mode
90 * @return GNUNET_OK on success, GNUNET_SYSERR on error
93 socket_set_blocking (struct GNUNET_NETWORK_Handle *fd, int doBlock)
99 if (ioctlsocket (fd->fd, FIONBIO, &mode) == SOCKET_ERROR)
102 SetErrnoFromWinsockError (WSAGetLastError ());
103 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "ioctlsocket");
104 return GNUNET_SYSERR;
110 int flags = fcntl (fd->fd, F_GETFL);
114 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "fcntl");
115 return GNUNET_SYSERR;
118 flags &= ~O_NONBLOCK;
122 if (0 != fcntl (fd->fd, F_SETFL, flags))
125 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "fcntl");
126 return GNUNET_SYSERR;
135 * Make a socket non-inheritable to child processes
137 * @param h the socket to make non-inheritable
138 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
139 * @warning Not implemented on Windows
142 socket_set_inheritable (const struct GNUNET_NETWORK_Handle *h)
146 i = fcntl (h->fd, F_GETFD);
148 return GNUNET_SYSERR;
149 if (i == (i | FD_CLOEXEC))
152 if (fcntl (h->fd, F_SETFD, i) < 0)
153 return GNUNET_SYSERR;
161 * The MSG_NOSIGNAL equivalent on Mac OS X
163 * @param h the socket to make non-delaying
166 socket_set_nosigpipe (const struct GNUNET_NETWORK_Handle *h)
170 setsockopt (h->fd, SOL_SOCKET, SO_NOSIGPIPE, &value, sizeof (value)))
171 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
177 * Disable delays when sending data via the socket.
178 * (GNUnet makes sure that messages are as big as
181 * @param h the socket to make non-delaying
184 socket_set_nodelay (const struct GNUNET_NETWORK_Handle *h)
188 setsockopt (h->fd, IPPROTO_TCP, TCP_NODELAY, &value, sizeof (value)))
189 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
194 * accept a new connection on a socket
196 * @param desc bound socket
197 * @param address address of the connecting peer, may be NULL
198 * @param address_len length of address
199 * @return client socket
201 struct GNUNET_NETWORK_Handle *
202 GNUNET_NETWORK_socket_accept (const struct GNUNET_NETWORK_Handle *desc,
203 struct sockaddr *address,
204 socklen_t * address_len)
206 struct GNUNET_NETWORK_Handle *ret;
208 ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle));
209 ret->fd = accept (desc->fd, address, address_len);
210 if (ret->fd == INVALID_SOCKET)
213 SetErrnoFromWinsockError (WSAGetLastError ());
219 if (ret->fd >= FD_SETSIZE)
221 GNUNET_break (0 == close (ret->fd));
227 if (GNUNET_SYSERR == socket_set_blocking (ret, GNUNET_NO))
231 /* we might want to treat this one as fatal... */
233 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret));
238 if (GNUNET_OK != socket_set_inheritable (ret))
239 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
240 "socket_set_inheritable");
243 socket_set_nosigpipe (ret);
246 if (address->sa_family != AF_UNIX)
248 socket_set_nodelay (ret);
254 * Bind to a connected socket
256 * @param address address to be bound
257 * @param address_len length of address
258 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
261 GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc,
262 const struct sockaddr *address,
263 socklen_t address_len)
267 ret = bind (desc->fd, address, address_len);
269 if (SOCKET_ERROR == ret)
270 SetErrnoFromWinsockError (WSAGetLastError ());
273 if ( (ret == 0) && (address->sa_family == AF_UNIX))
275 const struct sockaddr_un *un = (const struct sockaddr_un*) address;
276 desc->filename = GNUNET_strdup (un->sun_path);
280 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
287 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
290 GNUNET_NETWORK_socket_close (struct GNUNET_NETWORK_Handle *desc)
295 ret = closesocket (desc->fd);
296 SetErrnoFromWinsockError (WSAGetLastError ());
298 ret = close (desc->fd);
300 if (NULL != desc->filename)
302 if (0 != unlink (desc->filename))
303 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
306 GNUNET_free (desc->filename);
311 return (ret == 0) ? GNUNET_OK : GNUNET_SYSERR;
316 * Box a native socket (and check that it is a socket).
318 * @param fd socket to box
319 * @return NULL on error (including not supported on target platform)
321 struct GNUNET_NETWORK_Handle *
322 GNUNET_NETWORK_socket_box_native (int fd)
327 struct GNUNET_NETWORK_Handle *ret;
329 if (fcntl (fd, F_GETFD) < 0)
330 return NULL; /* invalid FD */
331 ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle));
341 * @param address peer address
342 * @param address_len length of address
343 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
346 GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Handle *desc,
347 const struct sockaddr *address,
348 socklen_t address_len)
351 ret = connect (desc->fd, address, address_len);
354 if (SOCKET_ERROR == ret)
357 SetErrnoFromWinsockError (WSAGetLastError ());
358 if (errno == EWOULDBLOCK)
362 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
370 * @param level protocol level of the option
371 * @param optname identifier of the option
372 * @param optval options
373 * @param optlen length of optval
374 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
377 GNUNET_NETWORK_socket_getsockopt (const struct GNUNET_NETWORK_Handle *desc,
378 int level, int optname, void *optval,
382 ret = getsockopt (desc->fd, level, optname, optval, optlen);
385 if (ret == 0 && level == SOL_SOCKET && optname == SO_ERROR)
386 *((int *) optval) = GetErrnoFromWinsockError (*((int *) optval));
388 else if (SOCKET_ERROR == ret)
389 SetErrnoFromWinsockError (WSAGetLastError ());
392 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
399 * @param backlog length of the listen queue
400 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
403 GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Handle *desc,
407 ret = listen (desc->fd, backlog);
410 if (SOCKET_ERROR == ret)
411 SetErrnoFromWinsockError (WSAGetLastError ());
414 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
419 * How much data is available to be read on this descriptor?
421 * Returns GNUNET_NO if no data is available, or on error!
425 GNUNET_NETWORK_socket_recvfrom_amount (const struct GNUNET_NETWORK_Handle
431 /* How much is there to be read? */
433 error = ioctl (desc->fd, FIONREAD, &pending);
436 error = ioctlsocket (desc->fd, FIONREAD, &pending);
437 if (error != SOCKET_ERROR)
445 * Read data from a connected socket (always non-blocking).
447 * @param buffer buffer
448 * @param length length of buffer
449 * @param src_addr either the source to recv from, or all zeroes
450 * to be filled in by recvfrom
451 * @param addrlen length of the addr
454 GNUNET_NETWORK_socket_recvfrom (const struct GNUNET_NETWORK_Handle * desc,
455 void *buffer, size_t length,
456 struct sockaddr * src_addr,
464 flags |= MSG_DONTWAIT;
467 ret = recvfrom (desc->fd, buffer, length, flags, src_addr, addrlen);
469 if (SOCKET_ERROR == ret)
470 SetErrnoFromWinsockError (WSAGetLastError ());
477 * Read data from a connected socket (always non-blocking).
479 * @param buffer buffer
480 * @param length length of buffer
483 GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Handle * desc,
484 void *buffer, size_t length)
491 flags |= MSG_DONTWAIT;
493 ret = recv (desc->fd, buffer, length, flags);
495 if (SOCKET_ERROR == ret)
496 SetErrnoFromWinsockError (WSAGetLastError ());
503 * Send data (always non-blocking).
506 * @param buffer data to send
507 * @param length size of the buffer
508 * @return number of bytes sent, GNUNET_SYSERR on error
511 GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Handle * desc,
512 const void *buffer, size_t length)
519 flags |= MSG_DONTWAIT;
523 flags |= MSG_NOSIGNAL;
526 ret = send (desc->fd, buffer, length, flags);
529 if (SOCKET_ERROR == ret)
530 SetErrnoFromWinsockError (WSAGetLastError ());
538 * Send data to a particular destination (always non-blocking).
539 * This function only works for UDP sockets.
542 * @param message data to send
543 * @param length size of the data
544 * @param dest_addr destination address
545 * @param dest_len length of address
546 * @return number of bytes sent, GNUNET_SYSERR on error
549 GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Handle * desc,
550 const void *message, size_t length,
551 const struct sockaddr * dest_addr,
559 flags |= MSG_DONTWAIT;
562 flags |= MSG_NOSIGNAL;
564 ret = sendto (desc->fd, message, length, flags, dest_addr, dest_len);
566 if (SOCKET_ERROR == ret)
567 SetErrnoFromWinsockError (WSAGetLastError ());
576 * @param level protocol level of the option
577 * @param option_name option identifier
578 * @param option_value value to set
579 * @param option_len size of option_value
580 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
583 GNUNET_NETWORK_socket_setsockopt (struct GNUNET_NETWORK_Handle *fd,
584 int level, int option_name,
585 const void *option_value,
586 socklen_t option_len)
590 ret = setsockopt (fd->fd, level, option_name, option_value, option_len);
592 if (SOCKET_ERROR == ret)
593 SetErrnoFromWinsockError (WSAGetLastError ());
595 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
600 * Create a new socket. Configure it for non-blocking IO and
601 * mark it as non-inheritable to child processes (set the
602 * close-on-exec flag).
604 * @param domain domain of the socket
605 * @param type socket type
606 * @param protocol network protocol
607 * @return new socket, NULL on error
609 struct GNUNET_NETWORK_Handle *
610 GNUNET_NETWORK_socket_create (int domain, int type, int protocol)
612 struct GNUNET_NETWORK_Handle *ret;
613 ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle));
614 ret->fd = socket (domain, type, protocol);
615 if (INVALID_SOCKET == ret->fd)
618 SetErrnoFromWinsockError (WSAGetLastError ());
625 if (ret->fd >= FD_SETSIZE)
627 GNUNET_break (0 == close (ret->fd));
634 if (GNUNET_SYSERR == socket_set_blocking (ret, GNUNET_NO))
636 /* we might want to treat this one as fatal... */
638 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret));
643 if (GNUNET_OK != socket_set_inheritable (ret))
644 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
645 "socket_set_inheritable");
648 socket_set_nosigpipe (ret);
650 if ( (type == SOCK_STREAM)
652 && (domain != AF_UNIX)
655 socket_set_nodelay (ret);
661 * Shut down socket operations
663 * @param how type of shutdown
664 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
667 GNUNET_NETWORK_socket_shutdown (struct GNUNET_NETWORK_Handle *desc, int how)
671 ret = shutdown (desc->fd, how);
674 SetErrnoFromWinsockError (WSAGetLastError ());
676 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
685 GNUNET_NETWORK_fdset_zero (struct GNUNET_NETWORK_FDSet *fds)
690 GNUNET_CONTAINER_slist_clear (fds->handles);
695 * Add a socket to the FD set
697 * @param desc socket to add
700 GNUNET_NETWORK_fdset_set (struct GNUNET_NETWORK_FDSet *fds,
701 const struct GNUNET_NETWORK_Handle *desc)
703 FD_SET (desc->fd, &fds->sds);
704 if (desc->fd + 1 > fds->nsds)
705 fds->nsds = desc->fd + 1;
710 * Check whether a socket is part of the fd set
713 * @return 0 if the FD is not set
716 GNUNET_NETWORK_fdset_isset (const struct GNUNET_NETWORK_FDSet *fds,
717 const struct GNUNET_NETWORK_Handle *desc)
719 return FD_ISSET (desc->fd, &fds->sds);
724 * Add one fd set to another
725 * @param dst the fd set to add to
726 * @param src the fd set to add from
729 GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst,
730 const struct GNUNET_NETWORK_FDSet *src)
733 for (nfds = src->nsds; nfds > 0; nfds--)
734 if (FD_ISSET (nfds, &src->sds))
737 FD_SET (nfds, &dst->sds);
738 if (nfds + 1 > dst->nsds)
739 dst->nsds = nfds + 1;
742 GNUNET_CONTAINER_slist_append (dst->handles, src->handles);
748 * Copy one fd set to another
750 * @param to destination
754 GNUNET_NETWORK_fdset_copy (struct GNUNET_NETWORK_FDSet *to,
755 const struct GNUNET_NETWORK_FDSet *from)
757 FD_COPY (&from->sds, &to->sds);
758 to->nsds = from->nsds;
761 GNUNET_CONTAINER_slist_clear (to->handles);
762 GNUNET_CONTAINER_slist_append (to->handles, from->handles);
767 GNUNET_NETWORK_get_fd (struct GNUNET_NETWORK_Handle *desc)
773 * Copy a native fd set
775 * @param to destination
776 * @param from native source set
777 * @param nfds the biggest socket number in from + 1
780 GNUNET_NETWORK_fdset_copy_native (struct GNUNET_NETWORK_FDSet *to,
781 const fd_set * from, int nfds)
783 FD_COPY (from, &to->sds);
789 * Set a native fd in a set
791 * @param to destination
792 * @param nfd native FD to set
794 void GNUNET_NETWORK_fdset_set_native (struct GNUNET_NETWORK_FDSet *to,
797 FD_SET (nfd, &to->sds);
798 to->nsds = GNUNET_MAX (nfd + 1, to->nsds);
803 * Add a file handle to the fd set
805 * @param h the file handle to add
808 GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds,
809 const struct GNUNET_DISK_FileHandle *h)
814 GNUNET_DISK_internal_file_handle_ (h, &hw, sizeof (HANDLE));
815 GNUNET_CONTAINER_slist_add (fds->handles,
816 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
817 &hw, sizeof (HANDLE));
821 GNUNET_DISK_internal_file_handle_ (h, &fd, sizeof (int));
822 FD_SET (fd, &fds->sds);
823 if (fd + 1 > fds->nsds)
831 * Check if a file handle is part of an fd set
833 * @param h file handle
834 * @return GNUNET_YES if the file handle is part of the set
837 GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds,
838 const struct GNUNET_DISK_FileHandle *h)
842 return GNUNET_CONTAINER_slist_contains (fds->handles, &h->h,
845 return FD_ISSET (h->fd, &fds->sds);
851 * Checks if two fd sets overlap
852 * @param fds1 first fd set
853 * @param fds2 second fd set
854 * @return GNUNET_YES if they do overlap, GNUNET_NO otherwise
857 GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1,
858 const struct GNUNET_NETWORK_FDSet *fds2)
862 if (nfds < fds2->nsds)
864 for (; nfds >= 0; nfds--)
865 if (FD_ISSET (nfds, &fds1->sds) && FD_ISSET (nfds, &fds2->sds))
869 struct GNUNET_CONTAINER_SList_Iterator *it;
871 for (it = GNUNET_CONTAINER_slist_begin (fds1->handles);
872 GNUNET_CONTAINER_slist_end (it) != GNUNET_YES;
873 GNUNET_CONTAINER_slist_next (it))
877 h = GNUNET_CONTAINER_slist_get (it, NULL);
878 if (GNUNET_CONTAINER_slist_contains
879 (fds2->handles, h, sizeof (HANDLE)))
881 GNUNET_CONTAINER_slist_iter_destroy (it);
885 GNUNET_CONTAINER_slist_iter_destroy (it);
894 * @return a new fd set
896 struct GNUNET_NETWORK_FDSet *
897 GNUNET_NETWORK_fdset_create ()
899 struct GNUNET_NETWORK_FDSet *fds;
900 fds = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_FDSet));
902 fds->handles = GNUNET_CONTAINER_slist_create ();
904 GNUNET_NETWORK_fdset_zero (fds);
910 * Releases the associated memory of an fd set
914 GNUNET_NETWORK_fdset_destroy (struct GNUNET_NETWORK_FDSet *fds)
917 GNUNET_CONTAINER_slist_destroy (fds->handles);
923 * Check if sockets meet certain conditions
924 * @param rfds set of sockets to be checked for readability
925 * @param wfds set of sockets to be checked for writability
926 * @param efds set of sockets to be checked for exceptions
927 * @param timeout relative value when to return
928 * @return number of selected sockets, GNUNET_SYSERR on error
931 GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
932 struct GNUNET_NETWORK_FDSet *wfds,
933 struct GNUNET_NETWORK_FDSet *efds,
934 const struct GNUNET_TIME_Relative timeout)
948 handles = GNUNET_CONTAINER_slist_count (rfds->handles);
953 nfds = GNUNET_MAX (nfds, wfds->nsds);
955 handles += GNUNET_CONTAINER_slist_count (wfds->handles);
960 nfds = GNUNET_MAX (nfds, efds->nsds);
962 handles += GNUNET_CONTAINER_slist_count (efds->handles);
967 tv.tv_sec = timeout.value / GNUNET_TIME_UNIT_SECONDS.value;
969 1000 * (timeout.value - (tv.tv_sec * GNUNET_TIME_UNIT_SECONDS.value));
970 if ((nfds == 0) && (timeout.value == GNUNET_TIME_UNIT_FOREVER_REL.value)
976 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
978 ("Fatal internal logic error, process hangs in `%s' (abort with CTRL-C)!\n"),
983 return select (nfds + 1,
984 (rfds != NULL) ? &rfds->sds : NULL,
985 (wfds != NULL) ? &wfds->sds : NULL,
986 (efds != NULL) ? &efds->sds : NULL,
987 (timeout.value == GNUNET_TIME_UNIT_FOREVER_REL.value)
992 fd_set sock_read, sock_write, sock_except;
993 fd_set aread, awrite, aexcept;
994 struct GNUNET_CONTAINER_SList *handles_read, *handles_write,
998 struct timeval tvslice;
1002 #define SAFE_FD_ISSET(fd, set) (set != NULL && FD_ISSET(fd, set))
1004 /* calculate how long we need to wait in milliseconds */
1005 if (timeout.value == GNUNET_TIME_UNIT_FOREVER_REL.value)
1006 ms_total = INFINITE;
1009 ms_total = timeout.value / GNUNET_TIME_UNIT_MILLISECONDS.value;
1011 /* select() may be used as a portable way to sleep */
1012 if (!(rfds || wfds || efds))
1019 handles_read = GNUNET_CONTAINER_slist_create ();
1020 handles_write = GNUNET_CONTAINER_slist_create ();
1021 handles_except = GNUNET_CONTAINER_slist_create ();
1024 sock_read = rfds->sds;
1026 FD_ZERO (&sock_read);
1028 sock_write = wfds->sds;
1030 FD_ZERO (&sock_write);
1032 sock_except = efds->sds;
1034 FD_ZERO (&sock_except);
1036 /* multiplex between winsock select() and waiting on the handles */
1040 limit = GetTickCount () + ms_total;
1049 /* overwrite the zero'd sets here; the select call
1050 * will clear those that are not active */
1051 FD_COPY (&sock_read, &aread);
1052 FD_COPY (&sock_write, &awrite);
1053 FD_COPY (&sock_except, &aexcept);
1055 tvslice.tv_usec = 100000;
1057 select (nfds + 1, &aread, &awrite, &aexcept,
1058 &tvslice)) == SOCKET_ERROR)
1061 SetErrnoFromWinsockError (WSAGetLastError ());
1062 if (errno == ENOTSOCK)
1066 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "select");
1069 goto select_loop_end;
1073 /* Poll read pipes */
1077 struct GNUNET_CONTAINER_SList_Iterator *i;
1078 for (i = GNUNET_CONTAINER_slist_begin (rfds->handles);
1079 GNUNET_CONTAINER_slist_end (i) != GNUNET_YES;
1080 GNUNET_CONTAINER_slist_next (i))
1085 h = *(HANDLE *) GNUNET_CONTAINER_slist_get (i, NULL);
1086 if (!PeekNamedPipe (h, NULL, 0, NULL, &dwBytes, NULL))
1089 SetErrnoFromWinError (GetLastError ());
1092 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
1096 goto select_loop_end;
1101 GNUNET_CONTAINER_slist_add (handles_read,
1102 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1103 &h, sizeof (HANDLE));
1107 GNUNET_CONTAINER_slist_iter_destroy (i);
1110 /* Poll for faulty pipes */
1114 struct GNUNET_CONTAINER_SList_Iterator *i;
1115 for (i = GNUNET_CONTAINER_slist_begin (efds->handles);
1116 GNUNET_CONTAINER_slist_end (i) != GNUNET_YES;
1117 GNUNET_CONTAINER_slist_next (i))
1122 h = *(HANDLE *) GNUNET_CONTAINER_slist_get (i, NULL);
1123 if (!PeekNamedPipe (h, NULL, 0, NULL, &dwBytes, NULL))
1126 GNUNET_CONTAINER_slist_add (handles_except,
1127 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1128 &h, sizeof (HANDLE));
1132 GNUNET_CONTAINER_slist_iter_destroy (i);
1137 GNUNET_CONTAINER_slist_append (handles_write, wfds->handles);
1138 retcode += GNUNET_CONTAINER_slist_count (wfds->handles);
1141 /* Check for closed sockets */
1142 for (i = 0; i < nfds; i++)
1145 if (SAFE_FD_ISSET (i, &sock_read))
1148 struct sockaddr addr;
1150 if (getpeername (i, &addr, &len) == SOCKET_ERROR)
1156 (i, SOL_SOCKET, SO_ERROR, (char *) &err, &len) == 0
1157 && err == WSAENOTCONN)
1160 if (!SAFE_FD_ISSET (i, &aread))
1171 if (retcode == 0 && nfds == 0)
1172 Sleep (GNUNET_MIN (100, limit - GetTickCount ()));
1174 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);
1191 GNUNET_NETWORK_fdset_zero (wfds);
1192 GNUNET_NETWORK_fdset_copy_native (wfds, &awrite, retcode);
1193 GNUNET_CONTAINER_slist_clear (wfds->handles);
1194 GNUNET_CONTAINER_slist_append (wfds->handles, handles_write);
1199 GNUNET_NETWORK_fdset_zero (efds);
1200 GNUNET_NETWORK_fdset_copy_native (efds, &aexcept, retcode);
1201 GNUNET_CONTAINER_slist_clear (efds->handles);
1202 GNUNET_CONTAINER_slist_append (efds->handles, handles_except);
1206 GNUNET_CONTAINER_slist_destroy (handles_read);
1207 GNUNET_CONTAINER_slist_destroy (handles_write);
1208 GNUNET_CONTAINER_slist_destroy (handles_except);
1215 /* end of network.c */