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 if (0 != setsockopt (h->fd, IPPROTO_TCP, TCP_NODELAY, &value, sizeof (value)))
181 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
183 const char * value = "1";
184 if (0 != setsockopt (h->fd, IPPROTO_TCP, TCP_NODELAY, value, sizeof (value)))
185 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
191 * accept a new connection on a socket
193 * @param desc bound socket
194 * @param address address of the connecting peer, may be NULL
195 * @param address_len length of address
196 * @return client socket
198 struct GNUNET_NETWORK_Handle *
199 GNUNET_NETWORK_socket_accept (const struct GNUNET_NETWORK_Handle *desc,
200 struct sockaddr *address,
201 socklen_t * address_len)
203 struct GNUNET_NETWORK_Handle *ret;
205 ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle));
206 ret->fd = accept (desc->fd, address, address_len);
207 if (ret->fd == INVALID_SOCKET)
210 SetErrnoFromWinsockError (WSAGetLastError ());
216 if (ret->fd >= FD_SETSIZE)
218 GNUNET_break (0 == close (ret->fd));
224 if (GNUNET_SYSERR == socket_set_blocking (ret, GNUNET_NO))
228 /* we might want to treat this one as fatal... */
230 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret));
235 if (GNUNET_OK != socket_set_inheritable (ret))
236 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
237 "socket_set_inheritable");
240 socket_set_nosigpipe (ret);
243 if (address->sa_family != AF_UNIX)
245 socket_set_nodelay (ret);
251 * Bind to a connected socket
253 * @param address address to be bound
254 * @param address_len length of address
255 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
258 GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc,
259 const struct sockaddr *address,
260 socklen_t address_len)
264 ret = bind (desc->fd, address, address_len);
266 if (SOCKET_ERROR == ret)
267 SetErrnoFromWinsockError (WSAGetLastError ());
270 if ( (ret == 0) && (address->sa_family == AF_UNIX))
272 const struct sockaddr_un *un = (const struct sockaddr_un*) address;
273 if (0 != unlink (un->sun_path))
274 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
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);
301 return (ret == 0) ? GNUNET_OK : GNUNET_SYSERR;
306 * Box a native socket (and check that it is a socket).
308 * @param fd socket to box
309 * @return NULL on error (including not supported on target platform)
311 struct GNUNET_NETWORK_Handle *
312 GNUNET_NETWORK_socket_box_native (int fd)
317 struct GNUNET_NETWORK_Handle *ret;
319 if (fcntl (fd, F_GETFD) < 0)
320 return NULL; /* invalid FD */
321 ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle));
331 * @param address peer address
332 * @param address_len length of address
333 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
336 GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Handle *desc,
337 const struct sockaddr *address,
338 socklen_t address_len)
341 ret = connect (desc->fd, address, address_len);
344 if (SOCKET_ERROR == ret)
347 SetErrnoFromWinsockError (WSAGetLastError ());
348 if (errno == EWOULDBLOCK)
352 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
360 * @param level protocol level of the option
361 * @param optname identifier of the option
362 * @param optval options
363 * @param optlen length of optval
364 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
367 GNUNET_NETWORK_socket_getsockopt (const struct GNUNET_NETWORK_Handle *desc,
368 int level, int optname, void *optval,
372 ret = getsockopt (desc->fd, level, optname, optval, optlen);
375 if (ret == 0 && level == SOL_SOCKET && optname == SO_ERROR)
376 *((int *) optval) = GetErrnoFromWinsockError (*((int *) optval));
378 else if (SOCKET_ERROR == ret)
379 SetErrnoFromWinsockError (WSAGetLastError ());
382 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
389 * @param backlog length of the listen queue
390 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
393 GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Handle *desc,
397 ret = listen (desc->fd, backlog);
400 if (SOCKET_ERROR == ret)
401 SetErrnoFromWinsockError (WSAGetLastError ());
404 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
409 * How much data is available to be read on this descriptor?
411 * Returns GNUNET_NO if no data is available, or on error!
415 GNUNET_NETWORK_socket_recvfrom_amount (const struct GNUNET_NETWORK_Handle
420 /* How much is there to be read? */
423 error = ioctl (desc->fd, FIONREAD, &pending);
427 error = ioctlsocket (desc->fd, FIONREAD, &pending);
428 if (error != SOCKET_ERROR)
436 * Read data from a connected socket (always non-blocking).
438 * @param buffer buffer
439 * @param length length of buffer
440 * @param src_addr either the source to recv from, or all zeroes
441 * to be filled in by recvfrom
442 * @param addrlen length of the addr
445 GNUNET_NETWORK_socket_recvfrom (const struct GNUNET_NETWORK_Handle * desc,
446 void *buffer, size_t length,
447 struct sockaddr * src_addr,
455 flags |= MSG_DONTWAIT;
458 ret = recvfrom (desc->fd, buffer, length, flags, src_addr, addrlen);
460 if (SOCKET_ERROR == ret)
461 SetErrnoFromWinsockError (WSAGetLastError ());
468 * Read data from a connected socket (always non-blocking).
470 * @param buffer buffer
471 * @param length length of buffer
474 GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Handle * desc,
475 void *buffer, size_t length)
482 flags |= MSG_DONTWAIT;
484 ret = recv (desc->fd, buffer, length, flags);
486 if (SOCKET_ERROR == ret)
487 SetErrnoFromWinsockError (WSAGetLastError ());
494 * Send data (always non-blocking).
497 * @param buffer data to send
498 * @param length size of the buffer
499 * @return number of bytes sent, GNUNET_SYSERR on error
502 GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Handle * desc,
503 const void *buffer, size_t length)
510 flags |= MSG_DONTWAIT;
514 flags |= MSG_NOSIGNAL;
517 ret = send (desc->fd, buffer, length, flags);
520 if (SOCKET_ERROR == ret)
521 SetErrnoFromWinsockError (WSAGetLastError ());
529 * Send data to a particular destination (always non-blocking).
530 * This function only works for UDP sockets.
533 * @param message data to send
534 * @param length size of the data
535 * @param dest_addr destination address
536 * @param dest_len length of address
537 * @return number of bytes sent, GNUNET_SYSERR on error
540 GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Handle * desc,
541 const void *message, size_t length,
542 const struct sockaddr * dest_addr,
550 flags |= MSG_DONTWAIT;
553 flags |= MSG_NOSIGNAL;
555 ret = sendto (desc->fd, message, length, flags, dest_addr, dest_len);
557 if (SOCKET_ERROR == ret)
558 SetErrnoFromWinsockError (WSAGetLastError ());
567 * @param level protocol level of the option
568 * @param option_name option identifier
569 * @param option_value value to set
570 * @param option_len size of option_value
571 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
574 GNUNET_NETWORK_socket_setsockopt (struct GNUNET_NETWORK_Handle *fd,
575 int level, int option_name,
576 const void *option_value,
577 socklen_t option_len)
581 ret = setsockopt (fd->fd, level, option_name, option_value, option_len);
583 if (SOCKET_ERROR == ret)
584 SetErrnoFromWinsockError (WSAGetLastError ());
586 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
591 * Create a new socket. Configure it for non-blocking IO and
592 * mark it as non-inheritable to child processes (set the
593 * close-on-exec flag).
595 * @param domain domain of the socket
596 * @param type socket type
597 * @param protocol network protocol
598 * @return new socket, NULL on error
600 struct GNUNET_NETWORK_Handle *
601 GNUNET_NETWORK_socket_create (int domain, int type, int protocol)
603 struct GNUNET_NETWORK_Handle *ret;
604 ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle));
605 ret->fd = socket (domain, type, protocol);
606 if (INVALID_SOCKET == ret->fd)
609 SetErrnoFromWinsockError (WSAGetLastError ());
616 if (ret->fd >= FD_SETSIZE)
618 GNUNET_break (0 == close (ret->fd));
625 if (GNUNET_SYSERR == socket_set_blocking (ret, GNUNET_NO))
627 /* we might want to treat this one as fatal... */
629 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret));
634 if (GNUNET_OK != socket_set_inheritable (ret))
635 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
636 "socket_set_inheritable");
639 socket_set_nosigpipe (ret);
641 if ( (type == SOCK_STREAM)
643 && (domain != AF_UNIX)
646 socket_set_nodelay (ret);
652 * Shut down socket operations
654 * @param how type of shutdown
655 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
658 GNUNET_NETWORK_socket_shutdown (struct GNUNET_NETWORK_Handle *desc, int how)
662 ret = shutdown (desc->fd, how);
665 SetErrnoFromWinsockError (WSAGetLastError ());
667 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
676 GNUNET_NETWORK_fdset_zero (struct GNUNET_NETWORK_FDSet *fds)
681 GNUNET_CONTAINER_slist_clear (fds->handles);
686 * Add a socket to the FD set
688 * @param desc socket to add
691 GNUNET_NETWORK_fdset_set (struct GNUNET_NETWORK_FDSet *fds,
692 const struct GNUNET_NETWORK_Handle *desc)
694 FD_SET (desc->fd, &fds->sds);
695 if (desc->fd + 1 > fds->nsds)
696 fds->nsds = desc->fd + 1;
701 * Check whether a socket is part of the fd set
704 * @return 0 if the FD is not set
707 GNUNET_NETWORK_fdset_isset (const struct GNUNET_NETWORK_FDSet *fds,
708 const struct GNUNET_NETWORK_Handle *desc)
710 return FD_ISSET (desc->fd, &fds->sds);
715 * Add one fd set to another
716 * @param dst the fd set to add to
717 * @param src the fd set to add from
720 GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst,
721 const struct GNUNET_NETWORK_FDSet *src)
724 for (nfds = src->nsds; nfds > 0; nfds--)
725 if (FD_ISSET (nfds, &src->sds))
728 FD_SET (nfds, &dst->sds);
729 if (nfds + 1 > dst->nsds)
730 dst->nsds = nfds + 1;
733 GNUNET_CONTAINER_slist_append (dst->handles, src->handles);
739 * Copy one fd set to another
741 * @param to destination
745 GNUNET_NETWORK_fdset_copy (struct GNUNET_NETWORK_FDSet *to,
746 const struct GNUNET_NETWORK_FDSet *from)
748 FD_COPY (&from->sds, &to->sds);
749 to->nsds = from->nsds;
752 GNUNET_CONTAINER_slist_clear (to->handles);
753 GNUNET_CONTAINER_slist_append (to->handles, from->handles);
758 GNUNET_NETWORK_get_fd (struct GNUNET_NETWORK_Handle *desc)
764 * Copy a native fd set
766 * @param to destination
767 * @param from native source set
768 * @param nfds the biggest socket number in from + 1
771 GNUNET_NETWORK_fdset_copy_native (struct GNUNET_NETWORK_FDSet *to,
772 const fd_set * from, int nfds)
774 FD_COPY (from, &to->sds);
780 * Set a native fd in a set
782 * @param to destination
783 * @param nfd native FD to set
785 void GNUNET_NETWORK_fdset_set_native (struct GNUNET_NETWORK_FDSet *to,
788 FD_SET (nfd, &to->sds);
789 to->nsds = GNUNET_MAX (nfd + 1, to->nsds);
794 * Test native fd in a set
796 * @param to set to test, NULL for empty set
797 * @param nfd native FD to test, or -1 for none
798 * @return GNUNET_YES if FD is set in the set
801 GNUNET_NETWORK_fdset_test_native (const struct GNUNET_NETWORK_FDSet *to,
804 if ( (nfd == -1) || (to == NULL) )
806 return FD_ISSET (nfd, &to->sds) ? GNUNET_YES : GNUNET_NO;
811 * Add a file handle to the fd set
813 * @param h the file handle to add
816 GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds,
817 const struct GNUNET_DISK_FileHandle *h)
822 GNUNET_DISK_internal_file_handle_ (h, &hw, sizeof (HANDLE));
823 GNUNET_CONTAINER_slist_add (fds->handles,
824 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
825 &hw, sizeof (HANDLE));
829 GNUNET_DISK_internal_file_handle_ (h, &fd, sizeof (int));
830 FD_SET (fd, &fds->sds);
831 if (fd + 1 > fds->nsds)
839 * Check if a file handle is part of an fd set
841 * @param h file handle
842 * @return GNUNET_YES if the file handle is part of the set
845 GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds,
846 const struct GNUNET_DISK_FileHandle *h)
850 return GNUNET_CONTAINER_slist_contains (fds->handles, &h->h,
853 return FD_ISSET (h->fd, &fds->sds);
859 * Checks if two fd sets overlap
860 * @param fds1 first fd set
861 * @param fds2 second fd set
862 * @return GNUNET_YES if they do overlap, GNUNET_NO otherwise
865 GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1,
866 const struct GNUNET_NETWORK_FDSet *fds2)
870 if (nfds < fds2->nsds)
872 for (; nfds >= 0; nfds--)
873 if (FD_ISSET (nfds, &fds1->sds) && FD_ISSET (nfds, &fds2->sds))
877 struct GNUNET_CONTAINER_SList_Iterator *it;
879 for (it = GNUNET_CONTAINER_slist_begin (fds1->handles);
880 GNUNET_CONTAINER_slist_end (it) != GNUNET_YES;
881 GNUNET_CONTAINER_slist_next (it))
885 h = (HANDLE *) GNUNET_CONTAINER_slist_get ((const struct GNUNET_CONTAINER_SList_Iterator *)it, NULL);
886 if (GNUNET_CONTAINER_slist_contains
887 (fds2->handles, h, sizeof (HANDLE)))
889 GNUNET_CONTAINER_slist_iter_destroy (it);
893 GNUNET_CONTAINER_slist_iter_destroy (it);
902 * @return a new fd set
904 struct GNUNET_NETWORK_FDSet *
905 GNUNET_NETWORK_fdset_create ()
907 struct GNUNET_NETWORK_FDSet *fds;
908 fds = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_FDSet));
910 fds->handles = GNUNET_CONTAINER_slist_create ();
912 GNUNET_NETWORK_fdset_zero (fds);
918 * Releases the associated memory of an fd set
922 GNUNET_NETWORK_fdset_destroy (struct GNUNET_NETWORK_FDSet *fds)
925 GNUNET_CONTAINER_slist_destroy (fds->handles);
931 * Check if sockets meet certain conditions
932 * @param rfds set of sockets to be checked for readability
933 * @param wfds set of sockets to be checked for writability
934 * @param efds set of sockets to be checked for exceptions
935 * @param timeout relative value when to return
936 * @return number of selected sockets, GNUNET_SYSERR on error
939 GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
940 struct GNUNET_NETWORK_FDSet *wfds,
941 struct GNUNET_NETWORK_FDSet *efds,
942 const struct GNUNET_TIME_Relative timeout)
956 handles = GNUNET_CONTAINER_slist_count (rfds->handles);
961 nfds = GNUNET_MAX (nfds, wfds->nsds);
963 handles += GNUNET_CONTAINER_slist_count (wfds->handles);
968 nfds = GNUNET_MAX (nfds, efds->nsds);
970 handles += GNUNET_CONTAINER_slist_count (efds->handles);
975 tv.tv_sec = timeout.value / GNUNET_TIME_UNIT_SECONDS.value;
977 1000 * (timeout.value - (tv.tv_sec * GNUNET_TIME_UNIT_SECONDS.value));
978 if ((nfds == 0) && (timeout.value == GNUNET_TIME_UNIT_FOREVER_REL.value)
984 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
986 ("Fatal internal logic error, process hangs in `%s' (abort with CTRL-C)!\n"),
991 return select (nfds + 1,
992 (rfds != NULL) ? &rfds->sds : NULL,
993 (wfds != NULL) ? &wfds->sds : NULL,
994 (efds != NULL) ? &efds->sds : NULL,
995 (timeout.value == GNUNET_TIME_UNIT_FOREVER_REL.value)
1000 fd_set sock_read, sock_write, sock_except;
1001 fd_set aread, awrite, aexcept;
1002 struct GNUNET_CONTAINER_SList *handles_read, *handles_write,
1006 struct timeval tvslice;
1010 #define SAFE_FD_ISSET(fd, set) (set != NULL && FD_ISSET(fd, set))
1012 /* calculate how long we need to wait in milliseconds */
1013 if (timeout.value == GNUNET_TIME_UNIT_FOREVER_REL.value)
1014 ms_total = INFINITE;
1017 ms_total = timeout.value / GNUNET_TIME_UNIT_MILLISECONDS.value;
1019 /* select() may be used as a portable way to sleep */
1020 if (!(rfds || wfds || efds))
1027 handles_read = GNUNET_CONTAINER_slist_create ();
1028 handles_write = GNUNET_CONTAINER_slist_create ();
1029 handles_except = GNUNET_CONTAINER_slist_create ();
1032 sock_read = rfds->sds;
1034 FD_ZERO (&sock_read);
1036 sock_write = wfds->sds;
1038 FD_ZERO (&sock_write);
1040 sock_except = efds->sds;
1042 FD_ZERO (&sock_except);
1044 /* multiplex between winsock select() and waiting on the handles */
1048 limit = GetTickCount () + ms_total;
1057 /* overwrite the zero'd sets here; the select call
1058 * will clear those that are not active */
1059 FD_COPY (&sock_read, &aread);
1060 FD_COPY (&sock_write, &awrite);
1061 FD_COPY (&sock_except, &aexcept);
1063 tvslice.tv_usec = 100000;
1065 select (nfds + 1, &aread, &awrite, &aexcept,
1066 &tvslice)) == SOCKET_ERROR)
1069 SetErrnoFromWinsockError (WSAGetLastError ());
1070 if (errno == ENOTSOCK)
1074 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "select");
1077 goto select_loop_end;
1081 /* Poll read pipes */
1085 struct GNUNET_CONTAINER_SList_Iterator *i;
1086 for (i = GNUNET_CONTAINER_slist_begin (rfds->handles);
1087 GNUNET_CONTAINER_slist_end (i) != GNUNET_YES;
1088 GNUNET_CONTAINER_slist_next (i))
1093 h = *(HANDLE *) GNUNET_CONTAINER_slist_get (i, NULL);
1094 if (!PeekNamedPipe (h, NULL, 0, NULL, &dwBytes, NULL))
1097 SetErrnoFromWinError (GetLastError ());
1100 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
1104 goto select_loop_end;
1109 GNUNET_CONTAINER_slist_add (handles_read,
1110 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1111 &h, sizeof (HANDLE));
1115 GNUNET_CONTAINER_slist_iter_destroy (i);
1118 /* Poll for faulty pipes */
1122 struct GNUNET_CONTAINER_SList_Iterator *i;
1123 for (i = GNUNET_CONTAINER_slist_begin (efds->handles);
1124 GNUNET_CONTAINER_slist_end (i) != GNUNET_YES;
1125 GNUNET_CONTAINER_slist_next (i))
1130 h = *(HANDLE *) GNUNET_CONTAINER_slist_get (i, NULL);
1131 if (!PeekNamedPipe (h, NULL, 0, NULL, &dwBytes, NULL))
1134 GNUNET_CONTAINER_slist_add (handles_except,
1135 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1136 &h, sizeof (HANDLE));
1140 GNUNET_CONTAINER_slist_iter_destroy (i);
1145 GNUNET_CONTAINER_slist_append (handles_write, wfds->handles);
1146 retcode += GNUNET_CONTAINER_slist_count (wfds->handles);
1149 /* Check for closed sockets */
1150 for (i = 0; i < nfds; i++)
1153 if (SAFE_FD_ISSET (i, &sock_read))
1156 struct sockaddr addr;
1158 if (getpeername (i, &addr, &len) == SOCKET_ERROR)
1164 (i, SOL_SOCKET, SO_ERROR, (char *) &err, &len) == 0
1165 && err == WSAENOTCONN)
1168 if (!SAFE_FD_ISSET (i, &aread))
1179 if (retcode == 0 && nfds == 0)
1180 Sleep (GNUNET_MIN (100, limit - GetTickCount ()));
1182 while (retcode == 0 && (ms_total == INFINITE || GetTickCount () < limit));
1188 GNUNET_NETWORK_fdset_zero (rfds);
1189 GNUNET_NETWORK_fdset_copy_native (rfds, &aread, retcode);
1190 GNUNET_CONTAINER_slist_clear (rfds->handles);
1191 GNUNET_CONTAINER_slist_append (rfds->handles, handles_read);
1195 GNUNET_NETWORK_fdset_zero (wfds);
1196 GNUNET_NETWORK_fdset_copy_native (wfds, &awrite, retcode);
1197 GNUNET_CONTAINER_slist_clear (wfds->handles);
1198 GNUNET_CONTAINER_slist_append (wfds->handles, handles_write);
1202 GNUNET_NETWORK_fdset_zero (efds);
1203 GNUNET_NETWORK_fdset_copy_native (efds, &aexcept, retcode);
1204 GNUNET_CONTAINER_slist_clear (efds->handles);
1205 GNUNET_CONTAINER_slist_append (efds->handles, handles_except);
1209 GNUNET_CONTAINER_slist_destroy (handles_read);
1210 GNUNET_CONTAINER_slist_destroy (handles_write);
1211 GNUNET_CONTAINER_slist_destroy (handles_except);
1218 /* end of network.c */