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)
145 i = fcntl (h->fd, F_GETFD);
146 if (i == (i | FD_CLOEXEC))
148 return (fcntl (h->fd, F_SETFD, i | FD_CLOEXEC) == 0)
149 ? GNUNET_OK : GNUNET_SYSERR;
156 * The MSG_NOSIGNAL equivalent on Mac OS X
158 * @param h the socket to make non-delaying
161 socket_set_nosigpipe (const struct GNUNET_NETWORK_Handle *h)
165 setsockopt (h->fd, SOL_SOCKET, SO_NOSIGPIPE, &value, sizeof (value)))
166 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
172 * Disable delays when sending data via the socket.
173 * (GNUnet makes sure that messages are as big as
176 * @param h the socket to make non-delaying
179 socket_set_nodelay (const struct GNUNET_NETWORK_Handle *h)
183 setsockopt (h->fd, IPPROTO_TCP, TCP_NODELAY, &value, sizeof (value)))
184 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
189 * accept a new connection on a socket
191 * @param desc bound socket
192 * @param address address of the connecting peer, may be NULL
193 * @param address_len length of address
194 * @return client socket
196 struct GNUNET_NETWORK_Handle *
197 GNUNET_NETWORK_socket_accept (const struct GNUNET_NETWORK_Handle *desc,
198 struct sockaddr *address,
199 socklen_t * address_len)
201 struct GNUNET_NETWORK_Handle *ret;
203 ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle));
204 ret->fd = accept (desc->fd, address, address_len);
205 if (ret->fd == INVALID_SOCKET)
208 SetErrnoFromWinsockError (WSAGetLastError ());
214 if (ret->fd >= FD_SETSIZE)
216 GNUNET_break (0 == close (ret->fd));
222 if (GNUNET_SYSERR == socket_set_blocking (ret, GNUNET_NO))
226 /* we might want to treat this one as fatal... */
228 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret));
233 if (GNUNET_OK != socket_set_inheritable (ret))
234 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
235 "socket_set_inheritable");
238 socket_set_nosigpipe (ret);
241 if (address->sa_family != AF_UNIX)
243 socket_set_nodelay (ret);
249 * Bind to a connected socket
251 * @param address address to be bound
252 * @param address_len length of address
253 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
256 GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc,
257 const struct sockaddr *address,
258 socklen_t address_len)
262 ret = bind (desc->fd, address, address_len);
264 if (SOCKET_ERROR == ret)
265 SetErrnoFromWinsockError (WSAGetLastError ());
268 if ( (ret == 0) && (address->sa_family == AF_UNIX))
270 const struct sockaddr_un *un = (const struct sockaddr_un*) address;
271 desc->filename = GNUNET_strdup (un->sun_path);
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);
295 if (NULL != desc->filename)
297 if (0 != unlink (desc->filename))
298 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
301 GNUNET_free (desc->filename);
306 return (ret == 0) ? GNUNET_OK : GNUNET_SYSERR;
311 * Box a native socket (and check that it is a socket).
313 * @param fd socket to box
314 * @return NULL on error (including not supported on target platform)
316 struct GNUNET_NETWORK_Handle *
317 GNUNET_NETWORK_socket_box_native (int fd)
322 struct GNUNET_NETWORK_Handle *ret;
324 if (fcntl (fd, F_GETFD) < 0)
325 return NULL; /* invalid FD */
326 ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle));
336 * @param address peer address
337 * @param address_len length of address
338 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
341 GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Handle *desc,
342 const struct sockaddr *address,
343 socklen_t address_len)
346 ret = connect (desc->fd, address, address_len);
349 if (SOCKET_ERROR == ret)
352 SetErrnoFromWinsockError (WSAGetLastError ());
353 if (errno == EWOULDBLOCK)
357 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
365 * @param level protocol level of the option
366 * @param optname identifier of the option
367 * @param optval options
368 * @param optlen length of optval
369 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
372 GNUNET_NETWORK_socket_getsockopt (const struct GNUNET_NETWORK_Handle *desc,
373 int level, int optname, void *optval,
377 ret = getsockopt (desc->fd, level, optname, optval, optlen);
380 if (ret == 0 && level == SOL_SOCKET && optname == SO_ERROR)
381 *((int *) optval) = GetErrnoFromWinsockError (*((int *) optval));
383 else if (SOCKET_ERROR == ret)
384 SetErrnoFromWinsockError (WSAGetLastError ());
387 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
394 * @param backlog length of the listen queue
395 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
398 GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Handle *desc,
402 ret = listen (desc->fd, backlog);
405 if (SOCKET_ERROR == ret)
406 SetErrnoFromWinsockError (WSAGetLastError ());
409 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
414 * How much data is available to be read on this descriptor?
416 * Returns GNUNET_NO if no data is available, or on error!
420 GNUNET_NETWORK_socket_recvfrom_amount (const struct GNUNET_NETWORK_Handle
426 /* How much is there to be read? */
428 error = ioctl (desc->fd, FIONREAD, &pending);
431 error = ioctlsocket (desc->fd, FIONREAD, &pending);
432 if (error != SOCKET_ERROR)
440 * Read data from a connected socket (always non-blocking).
442 * @param buffer buffer
443 * @param length length of buffer
444 * @param src_addr either the source to recv from, or all zeroes
445 * to be filled in by recvfrom
446 * @param addrlen length of the addr
449 GNUNET_NETWORK_socket_recvfrom (const struct GNUNET_NETWORK_Handle * desc,
450 void *buffer, size_t length,
451 struct sockaddr * src_addr,
459 flags |= MSG_DONTWAIT;
462 ret = recvfrom (desc->fd, buffer, length, flags, src_addr, addrlen);
464 if (SOCKET_ERROR == ret)
465 SetErrnoFromWinsockError (WSAGetLastError ());
472 * Read data from a connected socket (always non-blocking).
474 * @param buffer buffer
475 * @param length length of buffer
478 GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Handle * desc,
479 void *buffer, size_t length)
486 flags |= MSG_DONTWAIT;
488 ret = recv (desc->fd, buffer, length, flags);
490 if (SOCKET_ERROR == ret)
491 SetErrnoFromWinsockError (WSAGetLastError ());
498 * Send data (always non-blocking).
501 * @param buffer data to send
502 * @param length size of the buffer
503 * @return number of bytes sent, GNUNET_SYSERR on error
506 GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Handle * desc,
507 const void *buffer, size_t length)
514 flags |= MSG_DONTWAIT;
518 flags |= MSG_NOSIGNAL;
521 ret = send (desc->fd, buffer, length, flags);
524 if (SOCKET_ERROR == ret)
525 SetErrnoFromWinsockError (WSAGetLastError ());
533 * Send data to a particular destination (always non-blocking).
534 * This function only works for UDP sockets.
537 * @param message data to send
538 * @param length size of the data
539 * @param dest_addr destination address
540 * @param dest_len length of address
541 * @return number of bytes sent, GNUNET_SYSERR on error
544 GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Handle * desc,
545 const void *message, size_t length,
546 const struct sockaddr * dest_addr,
554 flags |= MSG_DONTWAIT;
557 flags |= MSG_NOSIGNAL;
559 ret = sendto (desc->fd, message, length, flags, dest_addr, dest_len);
561 if (SOCKET_ERROR == ret)
562 SetErrnoFromWinsockError (WSAGetLastError ());
571 * @param level protocol level of the option
572 * @param option_name option identifier
573 * @param option_value value to set
574 * @param option_len size of option_value
575 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
578 GNUNET_NETWORK_socket_setsockopt (struct GNUNET_NETWORK_Handle *fd,
579 int level, int option_name,
580 const void *option_value,
581 socklen_t option_len)
585 ret = setsockopt (fd->fd, level, option_name, option_value, option_len);
587 if (SOCKET_ERROR == ret)
588 SetErrnoFromWinsockError (WSAGetLastError ());
590 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
595 * Create a new socket. Configure it for non-blocking IO and
596 * mark it as non-inheritable to child processes (set the
597 * close-on-exec flag).
599 * @param domain domain of the socket
600 * @param type socket type
601 * @param protocol network protocol
602 * @return new socket, NULL on error
604 struct GNUNET_NETWORK_Handle *
605 GNUNET_NETWORK_socket_create (int domain, int type, int protocol)
607 struct GNUNET_NETWORK_Handle *ret;
608 ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle));
609 ret->fd = socket (domain, type, protocol);
610 if (INVALID_SOCKET == ret->fd)
613 SetErrnoFromWinsockError (WSAGetLastError ());
620 if (ret->fd >= FD_SETSIZE)
622 GNUNET_break (0 == close (ret->fd));
629 if (GNUNET_SYSERR == socket_set_blocking (ret, GNUNET_NO))
631 /* we might want to treat this one as fatal... */
633 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret));
638 if (GNUNET_OK != socket_set_inheritable (ret))
639 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
640 "socket_set_inheritable");
643 socket_set_nosigpipe (ret);
645 if ( (type == SOCK_STREAM)
647 && (domain != AF_UNIX)
650 socket_set_nodelay (ret);
656 * Shut down socket operations
658 * @param how type of shutdown
659 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
662 GNUNET_NETWORK_socket_shutdown (struct GNUNET_NETWORK_Handle *desc, int how)
666 ret = shutdown (desc->fd, how);
669 SetErrnoFromWinsockError (WSAGetLastError ());
671 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
680 GNUNET_NETWORK_fdset_zero (struct GNUNET_NETWORK_FDSet *fds)
685 GNUNET_CONTAINER_slist_clear (fds->handles);
690 * Add a socket to the FD set
692 * @param desc socket to add
695 GNUNET_NETWORK_fdset_set (struct GNUNET_NETWORK_FDSet *fds,
696 const struct GNUNET_NETWORK_Handle *desc)
698 FD_SET (desc->fd, &fds->sds);
699 if (desc->fd + 1 > fds->nsds)
700 fds->nsds = desc->fd + 1;
705 * Check whether a socket is part of the fd set
708 * @return 0 if the FD is not set
711 GNUNET_NETWORK_fdset_isset (const struct GNUNET_NETWORK_FDSet *fds,
712 const struct GNUNET_NETWORK_Handle *desc)
714 return FD_ISSET (desc->fd, &fds->sds);
719 * Add one fd set to another
720 * @param dst the fd set to add to
721 * @param src the fd set to add from
724 GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst,
725 const struct GNUNET_NETWORK_FDSet *src)
728 for (nfds = src->nsds; nfds > 0; nfds--)
729 if (FD_ISSET (nfds, &src->sds))
732 FD_SET (nfds, &dst->sds);
733 if (nfds + 1 > dst->nsds)
734 dst->nsds = nfds + 1;
737 GNUNET_CONTAINER_slist_append (dst->handles, src->handles);
743 * Copy one fd set to another
745 * @param to destination
749 GNUNET_NETWORK_fdset_copy (struct GNUNET_NETWORK_FDSet *to,
750 const struct GNUNET_NETWORK_FDSet *from)
752 FD_COPY (&from->sds, &to->sds);
753 to->nsds = from->nsds;
756 GNUNET_CONTAINER_slist_clear (to->handles);
757 GNUNET_CONTAINER_slist_append (to->handles, from->handles);
762 GNUNET_NETWORK_get_fd (struct GNUNET_NETWORK_Handle *desc)
768 * Copy a native fd set
770 * @param to destination
771 * @param from native source set
772 * @param nfds the biggest socket number in from + 1
775 GNUNET_NETWORK_fdset_copy_native (struct GNUNET_NETWORK_FDSet *to,
776 const fd_set * from, int nfds)
778 FD_COPY (from, &to->sds);
784 * Set a native fd in a set
786 * @param to destination
787 * @param nfd native FD to set
789 void GNUNET_NETWORK_fdset_set_native (struct GNUNET_NETWORK_FDSet *to,
792 FD_SET (nfd, &to->sds);
793 to->nsds = GNUNET_MAX (nfd + 1, to->nsds);
798 * Add a file handle to the fd set
800 * @param h the file handle to add
803 GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds,
804 const struct GNUNET_DISK_FileHandle *h)
809 GNUNET_DISK_internal_file_handle_ (h, &hw, sizeof (HANDLE));
810 GNUNET_CONTAINER_slist_add (fds->handles,
811 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
812 &hw, sizeof (HANDLE));
816 GNUNET_DISK_internal_file_handle_ (h, &fd, sizeof (int));
817 FD_SET (fd, &fds->sds);
818 if (fd + 1 > fds->nsds)
826 * Check if a file handle is part of an fd set
828 * @param h file handle
829 * @return GNUNET_YES if the file handle is part of the set
832 GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds,
833 const struct GNUNET_DISK_FileHandle *h)
837 return GNUNET_CONTAINER_slist_contains (fds->handles, &h->h,
840 return FD_ISSET (h->fd, &fds->sds);
846 * Checks if two fd sets overlap
847 * @param fds1 first fd set
848 * @param fds2 second fd set
849 * @return GNUNET_YES if they do overlap, GNUNET_NO otherwise
852 GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1,
853 const struct GNUNET_NETWORK_FDSet *fds2)
857 if (nfds < fds2->nsds)
859 for (; nfds >= 0; nfds--)
860 if (FD_ISSET (nfds, &fds1->sds) && FD_ISSET (nfds, &fds2->sds))
864 struct GNUNET_CONTAINER_SList_Iterator *it;
866 for (it = GNUNET_CONTAINER_slist_begin (fds1->handles);
867 GNUNET_CONTAINER_slist_end (it) != GNUNET_YES;
868 GNUNET_CONTAINER_slist_next (it))
872 h = GNUNET_CONTAINER_slist_get (it, NULL);
873 if (GNUNET_CONTAINER_slist_contains
874 (fds2->handles, h, sizeof (HANDLE)))
876 GNUNET_CONTAINER_slist_iter_destroy (it);
880 GNUNET_CONTAINER_slist_iter_destroy (it);
889 * @return a new fd set
891 struct GNUNET_NETWORK_FDSet *
892 GNUNET_NETWORK_fdset_create ()
894 struct GNUNET_NETWORK_FDSet *fds;
895 fds = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_FDSet));
897 fds->handles = GNUNET_CONTAINER_slist_create ();
899 GNUNET_NETWORK_fdset_zero (fds);
905 * Releases the associated memory of an fd set
909 GNUNET_NETWORK_fdset_destroy (struct GNUNET_NETWORK_FDSet *fds)
912 GNUNET_CONTAINER_slist_destroy (fds->handles);
918 * Check if sockets meet certain conditions
919 * @param rfds set of sockets to be checked for readability
920 * @param wfds set of sockets to be checked for writability
921 * @param efds set of sockets to be checked for exceptions
922 * @param timeout relative value when to return
923 * @return number of selected sockets, GNUNET_SYSERR on error
926 GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
927 struct GNUNET_NETWORK_FDSet *wfds,
928 struct GNUNET_NETWORK_FDSet *efds,
929 const struct GNUNET_TIME_Relative timeout)
943 handles = GNUNET_CONTAINER_slist_count (rfds->handles);
948 nfds = GNUNET_MAX (nfds, wfds->nsds);
950 handles += GNUNET_CONTAINER_slist_count (wfds->handles);
955 nfds = GNUNET_MAX (nfds, efds->nsds);
957 handles += GNUNET_CONTAINER_slist_count (efds->handles);
962 tv.tv_sec = timeout.value / GNUNET_TIME_UNIT_SECONDS.value;
964 1000 * (timeout.value - (tv.tv_sec * GNUNET_TIME_UNIT_SECONDS.value));
965 if ((nfds == 0) && (timeout.value == GNUNET_TIME_UNIT_FOREVER_REL.value)
971 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
973 ("Fatal internal logic error, process hangs in `%s' (abort with CTRL-C)!\n"),
978 return select (nfds + 1,
979 (rfds != NULL) ? &rfds->sds : NULL,
980 (wfds != NULL) ? &wfds->sds : NULL,
981 (efds != NULL) ? &efds->sds : NULL,
982 (timeout.value == GNUNET_TIME_UNIT_FOREVER_REL.value)
987 fd_set sock_read, sock_write, sock_except;
988 fd_set aread, awrite, aexcept;
989 struct GNUNET_CONTAINER_SList *handles_read, *handles_write,
993 struct timeval tvslice;
997 #define SAFE_FD_ISSET(fd, set) (set != NULL && FD_ISSET(fd, set))
999 /* calculate how long we need to wait in milliseconds */
1000 if (timeout.value == GNUNET_TIME_UNIT_FOREVER_REL.value)
1001 ms_total = INFINITE;
1004 ms_total = timeout.value / GNUNET_TIME_UNIT_MILLISECONDS.value;
1006 /* select() may be used as a portable way to sleep */
1007 if (!(rfds || wfds || efds))
1014 handles_read = GNUNET_CONTAINER_slist_create ();
1015 handles_write = GNUNET_CONTAINER_slist_create ();
1016 handles_except = GNUNET_CONTAINER_slist_create ();
1019 sock_read = rfds->sds;
1021 FD_ZERO (&sock_read);
1023 sock_write = wfds->sds;
1025 FD_ZERO (&sock_write);
1027 sock_except = efds->sds;
1029 FD_ZERO (&sock_except);
1031 /* multiplex between winsock select() and waiting on the handles */
1035 limit = GetTickCount () + ms_total;
1044 /* overwrite the zero'd sets here; the select call
1045 * will clear those that are not active */
1046 FD_COPY (&sock_read, &aread);
1047 FD_COPY (&sock_write, &awrite);
1048 FD_COPY (&sock_except, &aexcept);
1050 tvslice.tv_usec = 100000;
1052 select (nfds + 1, &aread, &awrite, &aexcept,
1053 &tvslice)) == SOCKET_ERROR)
1056 SetErrnoFromWinsockError (WSAGetLastError ());
1057 if (errno == ENOTSOCK)
1061 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "select");
1064 goto select_loop_end;
1068 /* Poll read pipes */
1072 struct GNUNET_CONTAINER_SList_Iterator *i;
1073 for (i = GNUNET_CONTAINER_slist_begin (rfds->handles);
1074 GNUNET_CONTAINER_slist_end (i) != GNUNET_YES;
1075 GNUNET_CONTAINER_slist_next (i))
1080 h = *(HANDLE *) GNUNET_CONTAINER_slist_get (i, NULL);
1081 if (!PeekNamedPipe (h, NULL, 0, NULL, &dwBytes, NULL))
1084 SetErrnoFromWinError (GetLastError ());
1087 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
1091 goto select_loop_end;
1096 GNUNET_CONTAINER_slist_add (handles_read,
1097 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1098 &h, sizeof (HANDLE));
1102 GNUNET_CONTAINER_slist_iter_destroy (i);
1105 /* Poll for faulty pipes */
1109 struct GNUNET_CONTAINER_SList_Iterator *i;
1110 for (i = GNUNET_CONTAINER_slist_begin (efds->handles);
1111 GNUNET_CONTAINER_slist_end (i) != GNUNET_YES;
1112 GNUNET_CONTAINER_slist_next (i))
1117 h = *(HANDLE *) GNUNET_CONTAINER_slist_get (i, NULL);
1118 if (!PeekNamedPipe (h, NULL, 0, NULL, &dwBytes, NULL))
1121 GNUNET_CONTAINER_slist_add (handles_except,
1122 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1123 &h, sizeof (HANDLE));
1127 GNUNET_CONTAINER_slist_iter_destroy (i);
1132 GNUNET_CONTAINER_slist_append (handles_write, wfds->handles);
1133 retcode += GNUNET_CONTAINER_slist_count (wfds->handles);
1136 /* Check for closed sockets */
1137 for (i = 0; i < nfds; i++)
1140 if (SAFE_FD_ISSET (i, &sock_read))
1143 struct sockaddr addr;
1145 if (getpeername (i, &addr, &len) == SOCKET_ERROR)
1151 (i, SOL_SOCKET, SO_ERROR, (char *) &err, &len) == 0
1152 && err == WSAENOTCONN)
1155 if (!SAFE_FD_ISSET (i, &aread))
1166 if (retcode == 0 && nfds == 0)
1167 Sleep (GNUNET_MIN (100, limit - GetTickCount ()));
1169 while (retcode == 0 && (ms_total == INFINITE || GetTickCount () < limit));
1177 GNUNET_NETWORK_fdset_zero (rfds);
1178 GNUNET_NETWORK_fdset_copy_native (rfds, &aread, retcode);
1179 GNUNET_CONTAINER_slist_clear (rfds->handles);
1180 GNUNET_CONTAINER_slist_append (rfds->handles, handles_read);
1186 GNUNET_NETWORK_fdset_zero (wfds);
1187 GNUNET_NETWORK_fdset_copy_native (wfds, &awrite, retcode);
1188 GNUNET_CONTAINER_slist_clear (wfds->handles);
1189 GNUNET_CONTAINER_slist_append (wfds->handles, handles_write);
1194 GNUNET_NETWORK_fdset_zero (efds);
1195 GNUNET_NETWORK_fdset_copy_native (efds, &aexcept, retcode);
1196 GNUNET_CONTAINER_slist_clear (efds->handles);
1197 GNUNET_CONTAINER_slist_append (efds->handles, handles_except);
1201 GNUNET_CONTAINER_slist_destroy (handles_read);
1202 GNUNET_CONTAINER_slist_destroy (handles_write);
1203 GNUNET_CONTAINER_slist_destroy (handles_except);
1210 /* end of network.c */