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
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->af = address->sa_family;
211 /* NOTE: if sa_family does not exist on some platform,
212 using AF_UNSPEC should be safe */
213 ret->fd = accept (desc->fd, address, address_len);
214 if (ret->fd == INVALID_SOCKET)
217 SetErrnoFromWinsockError (WSAGetLastError ());
223 if (ret->fd >= FD_SETSIZE)
225 GNUNET_break (0 == close (ret->fd));
231 if (GNUNET_SYSERR == socket_set_blocking (ret, GNUNET_NO))
235 /* we might want to treat this one as fatal... */
237 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret));
242 if (GNUNET_OK != socket_set_inheritable (ret))
243 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
244 "socket_set_inheritable");
247 socket_set_nosigpipe (ret);
250 if (address->sa_family != AF_UNIX)
252 socket_set_nodelay (ret);
258 * Bind to a connected socket
260 * @param address address to be bound
261 * @param address_len length of address
262 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
265 GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc,
266 const struct sockaddr *address,
267 socklen_t address_len)
274 if (desc->af == AF_INET6)
275 setsockopt (desc->fd, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof (on));
277 /* is this needed or desired? or done elsewhere? */
278 setsockopt (desc->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on));
282 ret = bind (desc->fd, address, address_len);
284 if (SOCKET_ERROR == ret)
285 SetErrnoFromWinsockError (WSAGetLastError ());
288 if ( (ret == 0) && (address->sa_family == AF_UNIX))
290 const struct sockaddr_un *un = (const struct sockaddr_un*) address;
291 if (0 != unlink (un->sun_path))
292 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
298 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
305 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
308 GNUNET_NETWORK_socket_close (struct GNUNET_NETWORK_Handle *desc)
313 ret = closesocket (desc->fd);
314 SetErrnoFromWinsockError (WSAGetLastError ());
316 ret = close (desc->fd);
319 return (ret == 0) ? GNUNET_OK : GNUNET_SYSERR;
324 * Box a native socket (and check that it is a socket).
326 * @param fd socket to box
327 * @return NULL on error (including not supported on target platform)
329 struct GNUNET_NETWORK_Handle *
330 GNUNET_NETWORK_socket_box_native (int fd)
335 struct GNUNET_NETWORK_Handle *ret;
337 if (fcntl (fd, F_GETFD) < 0)
338 return NULL; /* invalid FD */
339 ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle));
350 * @param address peer address
351 * @param address_len length of address
352 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
355 GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Handle *desc,
356 const struct sockaddr *address,
357 socklen_t address_len)
360 ret = connect (desc->fd, address, address_len);
363 if (SOCKET_ERROR == ret)
366 SetErrnoFromWinsockError (WSAGetLastError ());
367 if (errno == EWOULDBLOCK)
371 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
379 * @param level protocol level of the option
380 * @param optname identifier of the option
381 * @param optval options
382 * @param optlen length of optval
383 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
386 GNUNET_NETWORK_socket_getsockopt (const struct GNUNET_NETWORK_Handle *desc,
387 int level, int optname, void *optval,
391 ret = getsockopt (desc->fd, level, optname, optval, optlen);
394 if (ret == 0 && level == SOL_SOCKET && optname == SO_ERROR)
395 *((int *) optval) = GetErrnoFromWinsockError (*((int *) optval));
397 else if (SOCKET_ERROR == ret)
398 SetErrnoFromWinsockError (WSAGetLastError ());
401 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
408 * @param backlog length of the listen queue
409 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
412 GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Handle *desc,
416 ret = listen (desc->fd, backlog);
419 if (SOCKET_ERROR == ret)
420 SetErrnoFromWinsockError (WSAGetLastError ());
423 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
428 * How much data is available to be read on this descriptor?
430 * Returns GNUNET_NO if no data is available, or on error!
434 GNUNET_NETWORK_socket_recvfrom_amount (const struct GNUNET_NETWORK_Handle
439 /* How much is there to be read? */
442 error = ioctl (desc->fd, FIONREAD, &pending);
446 error = ioctlsocket (desc->fd, FIONREAD, &pending);
447 if (error != SOCKET_ERROR)
455 * Read data from a connected socket (always non-blocking).
457 * @param buffer buffer
458 * @param length length of buffer
459 * @param src_addr either the source to recv from, or all zeroes
460 * to be filled in by recvfrom
461 * @param addrlen length of the addr
464 GNUNET_NETWORK_socket_recvfrom (const struct GNUNET_NETWORK_Handle * desc,
465 void *buffer, size_t length,
466 struct sockaddr * src_addr,
474 flags |= MSG_DONTWAIT;
477 ret = recvfrom (desc->fd, buffer, length, flags, src_addr, addrlen);
479 if (SOCKET_ERROR == ret)
480 SetErrnoFromWinsockError (WSAGetLastError ());
487 * Read data from a connected socket (always non-blocking).
489 * @param buffer buffer
490 * @param length length of buffer
493 GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Handle * desc,
494 void *buffer, size_t length)
501 flags |= MSG_DONTWAIT;
503 ret = recv (desc->fd, buffer, length, flags);
505 if (SOCKET_ERROR == ret)
506 SetErrnoFromWinsockError (WSAGetLastError ());
513 * Send data (always non-blocking).
516 * @param buffer data to send
517 * @param length size of the buffer
518 * @return number of bytes sent, GNUNET_SYSERR on error
521 GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Handle * desc,
522 const void *buffer, size_t length)
529 flags |= MSG_DONTWAIT;
533 flags |= MSG_NOSIGNAL;
536 ret = send (desc->fd, buffer, length, flags);
539 if (SOCKET_ERROR == ret)
540 SetErrnoFromWinsockError (WSAGetLastError ());
548 * Send data to a particular destination (always non-blocking).
549 * This function only works for UDP sockets.
552 * @param message data to send
553 * @param length size of the data
554 * @param dest_addr destination address
555 * @param dest_len length of address
556 * @return number of bytes sent, GNUNET_SYSERR on error
559 GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Handle * desc,
560 const void *message, size_t length,
561 const struct sockaddr * dest_addr,
569 flags |= MSG_DONTWAIT;
572 flags |= MSG_NOSIGNAL;
574 ret = sendto (desc->fd, message, length, flags, dest_addr, dest_len);
576 if (SOCKET_ERROR == ret)
577 SetErrnoFromWinsockError (WSAGetLastError ());
586 * @param level protocol level of the option
587 * @param option_name option identifier
588 * @param option_value value to set
589 * @param option_len size of option_value
590 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
593 GNUNET_NETWORK_socket_setsockopt (struct GNUNET_NETWORK_Handle *fd,
594 int level, int option_name,
595 const void *option_value,
596 socklen_t option_len)
600 ret = setsockopt (fd->fd, level, option_name, option_value, option_len);
602 if (SOCKET_ERROR == ret)
603 SetErrnoFromWinsockError (WSAGetLastError ());
605 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
610 * Create a new socket. Configure it for non-blocking IO and
611 * mark it as non-inheritable to child processes (set the
612 * close-on-exec flag).
614 * @param domain domain of the socket
615 * @param type socket type
616 * @param protocol network protocol
617 * @return new socket, NULL on error
619 struct GNUNET_NETWORK_Handle *
620 GNUNET_NETWORK_socket_create (int domain, int type, int protocol)
622 struct GNUNET_NETWORK_Handle *ret;
623 ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle));
625 ret->fd = socket (domain, type, protocol);
626 if (INVALID_SOCKET == ret->fd)
629 SetErrnoFromWinsockError (WSAGetLastError ());
636 if (ret->fd >= FD_SETSIZE)
638 GNUNET_break (0 == close (ret->fd));
645 if (GNUNET_SYSERR == socket_set_blocking (ret, GNUNET_NO))
647 /* we might want to treat this one as fatal... */
649 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret));
654 if (GNUNET_OK != socket_set_inheritable (ret))
655 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
656 "socket_set_inheritable");
659 socket_set_nosigpipe (ret);
661 if ( (type == SOCK_STREAM)
663 && (domain != AF_UNIX)
666 socket_set_nodelay (ret);
672 * Shut down socket operations
674 * @param how type of shutdown
675 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
678 GNUNET_NETWORK_socket_shutdown (struct GNUNET_NETWORK_Handle *desc, int how)
682 ret = shutdown (desc->fd, how);
685 SetErrnoFromWinsockError (WSAGetLastError ());
687 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
696 GNUNET_NETWORK_fdset_zero (struct GNUNET_NETWORK_FDSet *fds)
701 GNUNET_CONTAINER_slist_clear (fds->handles);
706 * Add a socket to the FD set
708 * @param desc socket to add
711 GNUNET_NETWORK_fdset_set (struct GNUNET_NETWORK_FDSet *fds,
712 const struct GNUNET_NETWORK_Handle *desc)
714 FD_SET (desc->fd, &fds->sds);
715 if (desc->fd + 1 > fds->nsds)
716 fds->nsds = desc->fd + 1;
721 * Check whether a socket is part of the fd set
724 * @return 0 if the FD is not set
727 GNUNET_NETWORK_fdset_isset (const struct GNUNET_NETWORK_FDSet *fds,
728 const struct GNUNET_NETWORK_Handle *desc)
730 return FD_ISSET (desc->fd, &fds->sds);
735 * Add one fd set to another
736 * @param dst the fd set to add to
737 * @param src the fd set to add from
740 GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst,
741 const struct GNUNET_NETWORK_FDSet *src)
744 for (nfds = src->nsds; nfds > 0; nfds--)
745 if (FD_ISSET (nfds, &src->sds))
748 FD_SET (nfds, &dst->sds);
749 if (nfds + 1 > dst->nsds)
750 dst->nsds = nfds + 1;
753 GNUNET_CONTAINER_slist_append (dst->handles, src->handles);
759 * Copy one fd set to another
761 * @param to destination
765 GNUNET_NETWORK_fdset_copy (struct GNUNET_NETWORK_FDSet *to,
766 const struct GNUNET_NETWORK_FDSet *from)
768 FD_COPY (&from->sds, &to->sds);
769 to->nsds = from->nsds;
772 GNUNET_CONTAINER_slist_clear (to->handles);
773 GNUNET_CONTAINER_slist_append (to->handles, from->handles);
778 GNUNET_NETWORK_get_fd (struct GNUNET_NETWORK_Handle *desc)
784 * Copy a native fd set
786 * @param to destination
787 * @param from native source set
788 * @param nfds the biggest socket number in from + 1
791 GNUNET_NETWORK_fdset_copy_native (struct GNUNET_NETWORK_FDSet *to,
792 const fd_set * from, int nfds)
794 FD_COPY (from, &to->sds);
800 * Set a native fd in a set
802 * @param to destination
803 * @param nfd native FD to set
805 void GNUNET_NETWORK_fdset_set_native (struct GNUNET_NETWORK_FDSet *to,
808 FD_SET (nfd, &to->sds);
809 to->nsds = GNUNET_MAX (nfd + 1, to->nsds);
814 * Test native fd in a set
816 * @param to set to test, NULL for empty set
817 * @param nfd native FD to test, or -1 for none
818 * @return GNUNET_YES if FD is set in the set
821 GNUNET_NETWORK_fdset_test_native (const struct GNUNET_NETWORK_FDSet *to,
824 if ( (nfd == -1) || (to == NULL) )
826 return FD_ISSET (nfd, &to->sds) ? GNUNET_YES : GNUNET_NO;
831 * Add a file handle to the fd set
833 * @param h the file handle to add
836 GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds,
837 const struct GNUNET_DISK_FileHandle *h)
842 GNUNET_DISK_internal_file_handle_ (h, &hw, sizeof (HANDLE));
843 GNUNET_CONTAINER_slist_add (fds->handles,
844 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
845 &hw, sizeof (HANDLE));
849 GNUNET_DISK_internal_file_handle_ (h, &fd, sizeof (int));
850 FD_SET (fd, &fds->sds);
851 if (fd + 1 > fds->nsds)
859 * Check if a file handle is part of an fd set
861 * @param h file handle
862 * @return GNUNET_YES if the file handle is part of the set
865 GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds,
866 const struct GNUNET_DISK_FileHandle *h)
870 return GNUNET_CONTAINER_slist_contains (fds->handles, &h->h,
873 return FD_ISSET (h->fd, &fds->sds);
879 * Checks if two fd sets overlap
880 * @param fds1 first fd set
881 * @param fds2 second fd set
882 * @return GNUNET_YES if they do overlap, GNUNET_NO otherwise
885 GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1,
886 const struct GNUNET_NETWORK_FDSet *fds2)
890 if (nfds < fds2->nsds)
892 for (; nfds >= 0; nfds--)
893 if (FD_ISSET (nfds, &fds1->sds) && FD_ISSET (nfds, &fds2->sds))
897 struct GNUNET_CONTAINER_SList_Iterator *it;
899 for (it = GNUNET_CONTAINER_slist_begin (fds1->handles);
900 GNUNET_CONTAINER_slist_end (it) != GNUNET_YES;
901 GNUNET_CONTAINER_slist_next (it))
905 h = (HANDLE *) GNUNET_CONTAINER_slist_get ((const struct GNUNET_CONTAINER_SList_Iterator *)it, NULL);
906 if (GNUNET_CONTAINER_slist_contains
907 (fds2->handles, h, sizeof (HANDLE)))
909 GNUNET_CONTAINER_slist_iter_destroy (it);
913 GNUNET_CONTAINER_slist_iter_destroy (it);
922 * @return a new fd set
924 struct GNUNET_NETWORK_FDSet *
925 GNUNET_NETWORK_fdset_create ()
927 struct GNUNET_NETWORK_FDSet *fds;
928 fds = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_FDSet));
930 fds->handles = GNUNET_CONTAINER_slist_create ();
932 GNUNET_NETWORK_fdset_zero (fds);
938 * Releases the associated memory of an fd set
942 GNUNET_NETWORK_fdset_destroy (struct GNUNET_NETWORK_FDSet *fds)
945 GNUNET_CONTAINER_slist_destroy (fds->handles);
951 * Check if sockets meet certain conditions
952 * @param rfds set of sockets to be checked for readability
953 * @param wfds set of sockets to be checked for writability
954 * @param efds set of sockets to be checked for exceptions
955 * @param timeout relative value when to return
956 * @return number of selected sockets, GNUNET_SYSERR on error
959 GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
960 struct GNUNET_NETWORK_FDSet *wfds,
961 struct GNUNET_NETWORK_FDSet *efds,
962 const struct GNUNET_TIME_Relative timeout)
976 handles = GNUNET_CONTAINER_slist_count (rfds->handles);
981 nfds = GNUNET_MAX (nfds, wfds->nsds);
983 handles += GNUNET_CONTAINER_slist_count (wfds->handles);
988 nfds = GNUNET_MAX (nfds, efds->nsds);
990 handles += GNUNET_CONTAINER_slist_count (efds->handles);
995 tv.tv_sec = timeout.value / GNUNET_TIME_UNIT_SECONDS.value;
997 1000 * (timeout.value - (tv.tv_sec * GNUNET_TIME_UNIT_SECONDS.value));
998 if ((nfds == 0) && (timeout.value == GNUNET_TIME_UNIT_FOREVER_REL.value)
1004 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1006 ("Fatal internal logic error, process hangs in `%s' (abort with CTRL-C)!\n"),
1011 return select (nfds + 1,
1012 (rfds != NULL) ? &rfds->sds : NULL,
1013 (wfds != NULL) ? &wfds->sds : NULL,
1014 (efds != NULL) ? &efds->sds : NULL,
1015 (timeout.value == GNUNET_TIME_UNIT_FOREVER_REL.value)
1020 fd_set sock_read, sock_write, sock_except;
1021 fd_set aread, awrite, aexcept;
1022 struct GNUNET_CONTAINER_SList *handles_read, *handles_write,
1026 struct timeval tvslice;
1030 #define SAFE_FD_ISSET(fd, set) (set != NULL && FD_ISSET(fd, set))
1032 /* calculate how long we need to wait in milliseconds */
1033 if (timeout.value == GNUNET_TIME_UNIT_FOREVER_REL.value)
1034 ms_total = INFINITE;
1037 ms_total = timeout.value / GNUNET_TIME_UNIT_MILLISECONDS.value;
1039 /* select() may be used as a portable way to sleep */
1040 if (!(rfds || wfds || efds))
1047 handles_read = GNUNET_CONTAINER_slist_create ();
1048 handles_write = GNUNET_CONTAINER_slist_create ();
1049 handles_except = GNUNET_CONTAINER_slist_create ();
1052 sock_read = rfds->sds;
1054 FD_ZERO (&sock_read);
1056 sock_write = wfds->sds;
1058 FD_ZERO (&sock_write);
1060 sock_except = efds->sds;
1062 FD_ZERO (&sock_except);
1064 /* multiplex between winsock select() and waiting on the handles */
1068 limit = GetTickCount () + ms_total;
1077 /* overwrite the zero'd sets here; the select call
1078 * will clear those that are not active */
1079 FD_COPY (&sock_read, &aread);
1080 FD_COPY (&sock_write, &awrite);
1081 FD_COPY (&sock_except, &aexcept);
1083 tvslice.tv_usec = 100000;
1085 select (nfds + 1, &aread, &awrite, &aexcept,
1086 &tvslice)) == SOCKET_ERROR)
1089 SetErrnoFromWinsockError (WSAGetLastError ());
1090 if (errno == ENOTSOCK)
1094 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "select");
1097 goto select_loop_end;
1101 /* Poll read pipes */
1105 struct GNUNET_CONTAINER_SList_Iterator *i;
1106 for (i = GNUNET_CONTAINER_slist_begin (rfds->handles);
1107 GNUNET_CONTAINER_slist_end (i) != GNUNET_YES;
1108 GNUNET_CONTAINER_slist_next (i))
1113 h = *(HANDLE *) GNUNET_CONTAINER_slist_get (i, NULL);
1114 if (!PeekNamedPipe (h, NULL, 0, NULL, &dwBytes, NULL))
1117 SetErrnoFromWinError (GetLastError ());
1120 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
1124 goto select_loop_end;
1129 GNUNET_CONTAINER_slist_add (handles_read,
1130 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1131 &h, sizeof (HANDLE));
1135 GNUNET_CONTAINER_slist_iter_destroy (i);
1138 /* Poll for faulty pipes */
1142 struct GNUNET_CONTAINER_SList_Iterator *i;
1143 for (i = GNUNET_CONTAINER_slist_begin (efds->handles);
1144 GNUNET_CONTAINER_slist_end (i) != GNUNET_YES;
1145 GNUNET_CONTAINER_slist_next (i))
1150 h = *(HANDLE *) GNUNET_CONTAINER_slist_get (i, NULL);
1151 if (!PeekNamedPipe (h, NULL, 0, NULL, &dwBytes, NULL))
1154 GNUNET_CONTAINER_slist_add (handles_except,
1155 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1156 &h, sizeof (HANDLE));
1160 GNUNET_CONTAINER_slist_iter_destroy (i);
1165 GNUNET_CONTAINER_slist_append (handles_write, wfds->handles);
1166 retcode += GNUNET_CONTAINER_slist_count (wfds->handles);
1169 /* Check for closed sockets */
1170 for (i = 0; i < nfds; i++)
1173 if (SAFE_FD_ISSET (i, &sock_read))
1176 struct sockaddr addr;
1178 if (getpeername (i, &addr, &len) == SOCKET_ERROR)
1184 (i, SOL_SOCKET, SO_ERROR, (char *) &err, &len) == 0
1185 && err == WSAENOTCONN)
1188 if (!SAFE_FD_ISSET (i, &aread))
1199 if (retcode == 0 && nfds == 0)
1200 Sleep (GNUNET_MIN (100, limit - GetTickCount ()));
1202 while (retcode == 0 && (ms_total == INFINITE || GetTickCount () < limit));
1208 GNUNET_NETWORK_fdset_zero (rfds);
1209 GNUNET_NETWORK_fdset_copy_native (rfds, &aread, retcode);
1210 GNUNET_CONTAINER_slist_clear (rfds->handles);
1211 GNUNET_CONTAINER_slist_append (rfds->handles, handles_read);
1215 GNUNET_NETWORK_fdset_zero (wfds);
1216 GNUNET_NETWORK_fdset_copy_native (wfds, &awrite, retcode);
1217 GNUNET_CONTAINER_slist_clear (wfds->handles);
1218 GNUNET_CONTAINER_slist_append (wfds->handles, handles_write);
1222 GNUNET_NETWORK_fdset_zero (efds);
1223 GNUNET_NETWORK_fdset_copy_native (efds, &aexcept, retcode);
1224 GNUNET_CONTAINER_slist_clear (efds->handles);
1225 GNUNET_CONTAINER_slist_append (efds->handles, handles_except);
1229 GNUNET_CONTAINER_slist_destroy (handles_read);
1230 GNUNET_CONTAINER_slist_destroy (handles_write);
1231 GNUNET_CONTAINER_slist_destroy (handles_except);
1238 /* end of network.c */