2 This file is part of GNUnet.
3 (C) 2009-2013 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 3, 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
25 * @author Christian Grothoff
29 #include "gnunet_util_lib.h"
31 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
32 #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
33 #define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall)
35 #define DEBUG_NETWORK GNUNET_EXTRA_LOGGING
38 #ifndef INVALID_SOCKET
39 #define INVALID_SOCKET -1
43 struct GNUNET_NETWORK_Handle
52 * Address family / domain.
62 * Number of bytes in addr.
67 * Address we were bound to, or NULL.
69 struct sockaddr *addr;
75 * Test if the given protocol family is supported by this system.
77 * @param pf protocol family to test (PF_INET, PF_INET6, PF_UNIX)
78 * @return #GNUNET_OK if the PF is supported
81 GNUNET_NETWORK_test_pf (int pf)
85 s = socket (pf, SOCK_STREAM, 0);
88 if (EAFNOSUPPORT == errno)
90 fprintf (stderr, "Failed to create test socket: %s\n", STRERROR (errno));
103 * Given a unixpath that is too long (larger than UNIX_PATH_MAX),
104 * shorten it to an acceptable length while keeping it unique
105 * and making sure it remains a valid filename (if possible).
107 * @param unixpath long path, will be freed (or same pointer returned
108 * with moved 0-termination).
109 * @return shortened unixpath, NULL on error
112 GNUNET_NETWORK_shorten_unixpath (char *unixpath)
114 struct sockaddr_un dummy;
117 struct GNUNET_HashCode sh;
118 struct GNUNET_CRYPTO_HashAsciiEncoded ae;
121 upm = sizeof (dummy.sun_path);
122 slen = strlen (unixpath);
124 return unixpath; /* no shortening required */
125 GNUNET_CRYPTO_hash (unixpath, slen, &sh);
126 while (sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) +
127 strlen (unixpath) >= upm)
129 if (NULL == (end = strrchr (unixpath, '/')))
131 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
132 _("Unable to shorten unix path `%s' while keeping name unique\n"),
134 GNUNET_free (unixpath);
139 GNUNET_CRYPTO_hash_to_enc (&sh, &ae);
140 strcat (unixpath, (char*) ae.encoding);
146 #define FD_COPY(s, d) (memcpy ((d), (s), sizeof (fd_set)))
151 * Set if a socket should use blocking or non-blocking IO.
154 * @param doBlock blocking mode
155 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
158 GNUNET_NETWORK_socket_set_blocking (struct GNUNET_NETWORK_Handle *fd, int doBlock)
165 if (ioctlsocket (fd->fd, FIONBIO, &mode) == SOCKET_ERROR)
168 SetErrnoFromWinsockError (WSAGetLastError ());
169 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "ioctlsocket");
170 return GNUNET_SYSERR;
176 int flags = fcntl (fd->fd, F_GETFL);
181 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "fcntl");
182 return GNUNET_SYSERR;
185 flags &= ~O_NONBLOCK;
189 if (0 != fcntl (fd->fd, F_SETFL, flags))
192 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "fcntl");
193 return GNUNET_SYSERR;
201 * Make a socket non-inheritable to child processes
203 * @param h the socket to make non-inheritable
204 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
205 * @warning Not implemented on Windows
208 socket_set_inheritable (const struct GNUNET_NETWORK_Handle *h)
212 i = fcntl (h->fd, F_GETFD);
214 return GNUNET_SYSERR;
215 if (i == (i | FD_CLOEXEC))
218 if (fcntl (h->fd, F_SETFD, i) < 0)
219 return GNUNET_SYSERR;
223 b = SetHandleInformation ((HANDLE) h->fd, HANDLE_FLAG_INHERIT, 0);
226 SetErrnoFromWinsockError (WSAGetLastError ());
227 return GNUNET_SYSERR;
236 * The MSG_NOSIGNAL equivalent on Mac OS X
238 * @param h the socket to make non-delaying
241 socket_set_nosigpipe (const struct GNUNET_NETWORK_Handle *h)
246 setsockopt (h->fd, SOL_SOCKET, SO_NOSIGPIPE,
247 (const void *) &abs_value,
249 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
255 * Disable delays when sending data via the socket.
256 * (GNUnet makes sure that messages are as big as
259 * @param h the socket to make non-delaying
262 socket_set_nodelay (const struct GNUNET_NETWORK_Handle *h)
267 if (0 != setsockopt (h->fd, IPPROTO_TCP, TCP_NODELAY, &value, sizeof (value)))
268 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
270 const char *abs_value = "1";
273 setsockopt (h->fd, IPPROTO_TCP, TCP_NODELAY,
274 (const void *) abs_value,
276 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
282 * Perform proper canonical initialization for a network handle.
283 * Set it to non-blocking, make it non-inheritable to child
284 * processes, disable SIGPIPE, enable "nodelay" (if non-UNIX
285 * stream socket) and check that it is smaller than FD_SETSIZE.
287 * @param h socket to initialize
288 * @param af address family of the socket
289 * @param type socket type
290 * @return #GNUNET_OK on success, #GNUNET_SYSERR if initialization
291 * failed and the handle was destroyed
294 initialize_network_handle (struct GNUNET_NETWORK_Handle *h,
299 if (h->fd == INVALID_SOCKET)
302 SetErrnoFromWinsockError (WSAGetLastError ());
305 return GNUNET_SYSERR;
308 if (h->fd >= FD_SETSIZE)
310 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (h));
312 return GNUNET_SYSERR;
315 if (GNUNET_OK != socket_set_inheritable (h))
316 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
317 "socket_set_inheritable");
319 if (GNUNET_SYSERR == GNUNET_NETWORK_socket_set_blocking (h, GNUNET_NO))
322 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (h));
323 return GNUNET_SYSERR;
326 socket_set_nosigpipe (h);
328 if ( (type == SOCK_STREAM)
333 socket_set_nodelay (h);
339 * accept a new connection on a socket
341 * @param desc bound socket
342 * @param address address of the connecting peer, may be NULL
343 * @param address_len length of address
344 * @return client socket
346 struct GNUNET_NETWORK_Handle *
347 GNUNET_NETWORK_socket_accept (const struct GNUNET_NETWORK_Handle *desc,
348 struct sockaddr *address, socklen_t * address_len)
350 struct GNUNET_NETWORK_Handle *ret;
352 ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle));
355 struct sockaddr name;
356 socklen_t namelen = sizeof (name);
357 int gsn = getsockname (desc->fd, &name, &namelen);
360 LOG (GNUNET_ERROR_TYPE_DEBUG, "Accepting connection on `%s'\n",
361 GNUNET_a2s (&name, namelen));
364 ret->fd = accept (desc->fd, address, address_len);
365 if (GNUNET_OK != initialize_network_handle (ret,
366 (NULL != address) ? address->sa_family : desc->af,
374 * Bind a socket to a particular address.
376 * @param desc socket to bind
377 * @param address address to be bound
378 * @param address_len length of address
379 * @param flags flags affecting bind behaviour
380 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
383 GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc,
384 const struct sockaddr *address,
385 socklen_t address_len,
395 if (desc->af == AF_INET6)
396 if (setsockopt (desc->fd, IPPROTO_IPV6, IPV6_V6ONLY,
399 LOG_STRERROR (GNUNET_ERROR_TYPE_DEBUG, "setsockopt");
407 /* This is required here for TCP sockets, but only on UNIX */
408 if ((SOCK_STREAM == desc->type)
409 && (0 != setsockopt (desc->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on))))
410 LOG_STRERROR (GNUNET_ERROR_TYPE_DEBUG, "setsockopt");
415 if (address->sa_family == AF_UNIX && (flags & GNUNET_BIND_EXCLUSIVE) == 0)
417 const struct sockaddr_un *un = (const struct sockaddr_un *) address;
419 (void) unlink (un->sun_path);
423 ret = bind (desc->fd, address, address_len);
425 if (SOCKET_ERROR == ret)
426 SetErrnoFromWinsockError (WSAGetLastError ());
429 return GNUNET_SYSERR;
432 desc->addr = GNUNET_malloc (address_len);
433 memcpy (desc->addr, address, address_len);
434 desc->addrlen = address_len;
445 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
448 GNUNET_NETWORK_socket_close (struct GNUNET_NETWORK_Handle *desc)
456 ret = closesocket (desc->fd);
457 error = WSAGetLastError ();
458 SetErrnoFromWinsockError (error);
459 LOG (GNUNET_ERROR_TYPE_DEBUG,
460 "Closed 0x%x, closesocket() returned %d, GLE is %u\n", desc->fd, ret,
463 ret = close (desc->fd);
467 if ((desc->af == AF_UNIX) && (NULL != desc->addr))
469 const struct sockaddr_un *un = (const struct sockaddr_un *) desc->addr;
471 if (0 != unlink (un->sun_path))
472 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "unlink", un->sun_path);
476 GNUNET_free_non_null (desc->addr);
478 return (ret == 0) ? GNUNET_OK : GNUNET_SYSERR;
483 * Box a native socket (and check that it is a socket).
485 * @param fd socket to box
486 * @return NULL on error (including not supported on target platform)
488 struct GNUNET_NETWORK_Handle *
489 GNUNET_NETWORK_socket_box_native (SOCKTYPE fd)
491 struct GNUNET_NETWORK_Handle *ret;
495 /* FIXME: Find a better call to check that FD is valid */
496 if (WSAIoctl (fd, FIONBIO, (void *) &i, sizeof (i), NULL, 0, &d, NULL, NULL) != 0)
497 return NULL; /* invalid FD */
498 ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle));
503 if (fcntl (fd, F_GETFD) < 0)
504 return NULL; /* invalid FD */
505 ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle));
514 * Connect a socket to some remote address.
517 * @param address peer address
518 * @param address_len length of address
519 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
522 GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Handle *desc,
523 const struct sockaddr *address,
524 socklen_t address_len)
528 ret = connect (desc->fd, address, address_len);
531 if (SOCKET_ERROR == ret)
533 SetErrnoFromWinsockError (WSAGetLastError ());
534 if (errno == EWOULDBLOCK)
538 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
546 * @param level protocol level of the option
547 * @param optname identifier of the option
548 * @param optval options
549 * @param optlen length of optval
550 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
553 GNUNET_NETWORK_socket_getsockopt (const struct GNUNET_NETWORK_Handle *desc,
554 int level, int optname, void *optval,
559 ret = getsockopt (desc->fd, level, optname, optval, optlen);
562 if (ret == 0 && level == SOL_SOCKET && optname == SO_ERROR)
563 *((int *) optval) = GetErrnoFromWinsockError (*((int *) optval));
565 else if (SOCKET_ERROR == ret)
566 SetErrnoFromWinsockError (WSAGetLastError ());
568 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
576 * @param backlog length of the listen queue
577 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
580 GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Handle *desc,
585 ret = listen (desc->fd, backlog);
588 if (SOCKET_ERROR == ret)
589 SetErrnoFromWinsockError (WSAGetLastError ());
592 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
597 * How much data is available to be read on this descriptor?
600 * @returns #GNUNET_NO if no data is available, or on error!
603 GNUNET_NETWORK_socket_recvfrom_amount (const struct GNUNET_NETWORK_Handle *
608 /* How much is there to be read? */
612 error = ioctl (desc->fd, FIONREAD, &pending);
614 return (ssize_t) pending;
619 error = ioctlsocket (desc->fd, FIONREAD, &pending);
620 if (error != SOCKET_ERROR)
621 return (ssize_t) pending;
628 * Read data from a socket (always non-blocking).
631 * @param buffer buffer
632 * @param length length of buffer
633 * @param src_addr either the source to recv from, or all zeroes
634 * to be filled in by recvfrom
635 * @param addrlen length of the addr
638 GNUNET_NETWORK_socket_recvfrom (const struct GNUNET_NETWORK_Handle *desc,
639 void *buffer, size_t length,
640 struct sockaddr *src_addr, socklen_t *addrlen)
648 flags |= MSG_DONTWAIT;
651 ret = recvfrom (desc->fd, buffer, length, flags, src_addr, addrlen);
653 if (SOCKET_ERROR == ret)
654 SetErrnoFromWinsockError (WSAGetLastError ());
661 * Read data from a connected socket (always non-blocking).
664 * @param buffer buffer
665 * @param length length of buffer
668 GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Handle * desc,
669 void *buffer, size_t length)
677 flags |= MSG_DONTWAIT;
679 ret = recv (desc->fd, buffer, length, flags);
681 if (SOCKET_ERROR == ret)
682 SetErrnoFromWinsockError (WSAGetLastError ());
689 * Send data (always non-blocking).
692 * @param buffer data to send
693 * @param length size of the buffer
694 * @return number of bytes sent, GNUNET_SYSERR on error
697 GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Handle * desc,
698 const void *buffer, size_t length)
706 flags |= MSG_DONTWAIT;
710 flags |= MSG_NOSIGNAL;
713 ret = send (desc->fd, buffer, length, flags);
716 if (SOCKET_ERROR == ret)
717 SetErrnoFromWinsockError (WSAGetLastError ());
725 * Send data to a particular destination (always non-blocking).
726 * This function only works for UDP sockets.
729 * @param message data to send
730 * @param length size of the data
731 * @param dest_addr destination address
732 * @param dest_len length of address
733 * @return number of bytes sent, #GNUNET_SYSERR on error
736 GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Handle * desc,
737 const void *message, size_t length,
738 const struct sockaddr * dest_addr,
747 flags |= MSG_DONTWAIT;
750 flags |= MSG_NOSIGNAL;
752 ret = sendto (desc->fd, message, length, flags, dest_addr, dest_len);
754 if (SOCKET_ERROR == ret)
755 SetErrnoFromWinsockError (WSAGetLastError ());
765 * @param level protocol level of the option
766 * @param option_name option identifier
767 * @param option_value value to set
768 * @param option_len size of option_value
769 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
772 GNUNET_NETWORK_socket_setsockopt (struct GNUNET_NETWORK_Handle *fd, int level,
773 int option_name, const void *option_value,
774 socklen_t option_len)
778 ret = setsockopt (fd->fd, level, option_name, option_value, option_len);
780 if (SOCKET_ERROR == ret)
781 SetErrnoFromWinsockError (WSAGetLastError ());
783 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
788 * Create a new socket. Configure it for non-blocking IO and
789 * mark it as non-inheritable to child processes (set the
790 * close-on-exec flag).
792 * @param domain domain of the socket
793 * @param type socket type
794 * @param protocol network protocol
795 * @return new socket, NULL on error
797 struct GNUNET_NETWORK_Handle *
798 GNUNET_NETWORK_socket_create (int domain, int type, int protocol)
800 struct GNUNET_NETWORK_Handle *ret;
802 ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle));
803 ret->fd = socket (domain, type, protocol);
805 initialize_network_handle (ret, domain, type))
812 * Shut down socket operations
814 * @param how type of shutdown
815 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
818 GNUNET_NETWORK_socket_shutdown (struct GNUNET_NETWORK_Handle *desc, int how)
822 ret = shutdown (desc->fd, how);
825 SetErrnoFromWinsockError (WSAGetLastError ());
827 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
832 * Disable the "CORK" feature for communication with the given socket,
833 * forcing the OS to immediately flush the buffer on transmission
834 * instead of potentially buffering multiple messages. Essentially
835 * reduces the OS send buffers to zero.
838 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
841 GNUNET_NETWORK_socket_disable_corking (struct GNUNET_NETWORK_Handle *desc)
850 setsockopt (desc->fd, SOL_SOCKET, SO_SNDBUF, (char *) &value,
852 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
855 setsockopt (desc->fd, SOL_SOCKET, SO_RCVBUF, (char *) &value,
857 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
863 setsockopt (desc->fd, SOL_SOCKET, SO_SNDBUF, &value, sizeof (value))))
864 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
867 setsockopt (desc->fd, SOL_SOCKET, SO_RCVBUF, &value, sizeof (value))))
868 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
870 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
880 GNUNET_NETWORK_fdset_zero (struct GNUNET_NETWORK_FDSet *fds)
885 GNUNET_CONTAINER_slist_clear (fds->handles);
891 * Add a socket to the FD set
894 * @param desc socket to add
897 GNUNET_NETWORK_fdset_set (struct GNUNET_NETWORK_FDSet *fds,
898 const struct GNUNET_NETWORK_Handle *desc)
900 FD_SET (desc->fd, &fds->sds);
901 if (desc->fd + 1 > fds->nsds)
902 fds->nsds = desc->fd + 1;
907 * Check whether a socket is part of the fd set
911 * @return 0 if the FD is not set
914 GNUNET_NETWORK_fdset_isset (const struct GNUNET_NETWORK_FDSet *fds,
915 const struct GNUNET_NETWORK_Handle *desc)
917 return FD_ISSET (desc->fd, &fds->sds);
922 * Add one fd set to another
924 * @param dst the fd set to add to
925 * @param src the fd set to add from
928 GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst,
929 const struct GNUNET_NETWORK_FDSet *src)
934 for (nfds = src->nsds; nfds > 0; nfds--)
935 if (FD_ISSET (nfds, &src->sds))
938 FD_SET (nfds, &dst->sds);
939 if (nfds + 1 > dst->nsds)
940 dst->nsds = nfds + 1;
943 /* This is MinGW32-specific implementation that relies on the code that
944 * winsock2.h defines for FD_SET. Namely, it relies on FD_SET checking
945 * that fd being added is not already in the set.
946 * Also relies on us knowing what's inside fd_set (fd_count and fd_array).
949 for (i = 0; i < src->sds.fd_count; i++)
950 FD_SET (src->sds.fd_array[i], &dst->sds);
951 if (src->nsds > dst->nsds)
952 dst->nsds = src->nsds;
954 GNUNET_CONTAINER_slist_append (dst->handles, src->handles);
960 * Copy one fd set to another
962 * @param to destination
966 GNUNET_NETWORK_fdset_copy (struct GNUNET_NETWORK_FDSet *to,
967 const struct GNUNET_NETWORK_FDSet *from)
969 FD_COPY (&from->sds, &to->sds);
970 to->nsds = from->nsds;
973 GNUNET_CONTAINER_slist_clear (to->handles);
974 GNUNET_CONTAINER_slist_append (to->handles, from->handles);
980 * Return file descriptor for this network handle
982 * @param desc wrapper to process
983 * @return POSIX file descriptor
986 GNUNET_NETWORK_get_fd (struct GNUNET_NETWORK_Handle *desc)
993 * Return sockaddr for this network handle
995 * @param desc wrapper to process
999 GNUNET_NETWORK_get_addr (struct GNUNET_NETWORK_Handle *desc)
1006 * Return sockaddr length for this network handle
1008 * @param desc wrapper to process
1009 * @return socklen_t for sockaddr
1012 GNUNET_NETWORK_get_addrlen (struct GNUNET_NETWORK_Handle *desc)
1014 return desc->addrlen;
1019 * Copy a native fd set
1021 * @param to destination
1022 * @param from native source set
1023 * @param nfds the biggest socket number in from + 1
1026 GNUNET_NETWORK_fdset_copy_native (struct GNUNET_NETWORK_FDSet *to,
1027 const fd_set * from, int nfds)
1029 FD_COPY (from, &to->sds);
1035 * Set a native fd in a set
1037 * @param to destination
1038 * @param nfd native FD to set
1041 GNUNET_NETWORK_fdset_set_native (struct GNUNET_NETWORK_FDSet *to, int nfd)
1043 GNUNET_assert ((nfd >= 0) && (nfd < FD_SETSIZE));
1044 FD_SET (nfd, &to->sds);
1045 to->nsds = GNUNET_MAX (nfd + 1, to->nsds);
1050 * Test native fd in a set
1052 * @param to set to test, NULL for empty set
1053 * @param nfd native FD to test, or -1 for none
1054 * @return #GNUNET_YES if FD is set in the set
1057 GNUNET_NETWORK_fdset_test_native (const struct GNUNET_NETWORK_FDSet *to,
1060 if ((nfd == -1) || (to == NULL))
1062 return FD_ISSET (nfd, &to->sds) ? GNUNET_YES : GNUNET_NO;
1067 * Add a file handle to the fd set
1069 * @param h the file handle to add
1072 GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds,
1073 const struct GNUNET_DISK_FileHandle *h)
1076 GNUNET_CONTAINER_slist_add (fds->handles,
1077 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT, h,
1078 sizeof (struct GNUNET_DISK_FileHandle));
1083 GNUNET_DISK_internal_file_handle_ (h, &fd, sizeof (int));
1084 FD_SET (fd, &fds->sds);
1085 if (fd + 1 > fds->nsds)
1093 * Check if a file handle is part of an fd set
1095 * @param h file handle
1096 * @return #GNUNET_YES if the file handle is part of the set
1099 GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds,
1100 const struct GNUNET_DISK_FileHandle *h)
1104 return GNUNET_CONTAINER_slist_contains (fds->handles, h,
1106 GNUNET_DISK_FileHandle));
1108 return FD_ISSET (h->fd, &fds->sds);
1114 * Checks if two fd sets overlap
1115 * @param fds1 first fd set
1116 * @param fds2 second fd set
1117 * @return #GNUNET_YES if they do overlap, #GNUNET_NO otherwise
1120 GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1,
1121 const struct GNUNET_NETWORK_FDSet *fds2)
1127 if (nfds > fds2->nsds)
1132 if (FD_ISSET (nfds, &fds1->sds) && FD_ISSET (nfds, &fds2->sds))
1136 struct GNUNET_CONTAINER_SList_Iterator it;
1137 struct GNUNET_DISK_FileHandle *h;
1141 /*This code is somewhat hacky, we are not supposed to know what's
1142 * inside of fd_set; also the O(n^2) is really bad... */
1144 for (i = 0; i < fds1->sds.fd_count; i++)
1146 for (j = 0; j < fds2->sds.fd_count; j++)
1148 if (fds1->sds.fd_array[i] == fds2->sds.fd_array[j])
1152 it = GNUNET_CONTAINER_slist_begin (fds1->handles);
1153 while (GNUNET_CONTAINER_slist_end (&it) != GNUNET_YES)
1156 struct GNUNET_CONTAINER_SList_Iterator t;
1158 h = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (&it,
1161 LOG (GNUNET_ERROR_TYPE_DEBUG, "Checking that FD 0x%x is in another set:\n",
1163 for (t = GNUNET_CONTAINER_slist_begin (fds2->handles);
1164 GNUNET_CONTAINER_slist_end (&t) != GNUNET_YES;
1165 GNUNET_CONTAINER_slist_next (&t))
1167 struct GNUNET_DISK_FileHandle *fh;
1169 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (&t,
1171 LOG (GNUNET_ERROR_TYPE_DEBUG, "0x%x\n", fh->h);
1174 if (GNUNET_CONTAINER_slist_contains
1175 (fds2->handles, h, sizeof (struct GNUNET_DISK_FileHandle)))
1179 GNUNET_CONTAINER_slist_next (&it);
1189 * @return a new fd set
1191 struct GNUNET_NETWORK_FDSet *
1192 GNUNET_NETWORK_fdset_create ()
1194 struct GNUNET_NETWORK_FDSet *fds;
1196 fds = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_FDSet));
1198 fds->handles = GNUNET_CONTAINER_slist_create ();
1200 GNUNET_NETWORK_fdset_zero (fds);
1206 * Releases the associated memory of an fd set
1211 GNUNET_NETWORK_fdset_destroy (struct GNUNET_NETWORK_FDSet *fds)
1214 GNUNET_CONTAINER_slist_destroy (fds->handles);
1220 struct _select_params
1228 SOCKET wakeup_socket;
1233 _selector (LPVOID p)
1235 struct _select_params *sp = p;
1239 WaitForSingleObject (sp->standby, INFINITE);
1240 ResetEvent (sp->standby);
1241 sp->status = select (1, sp->r, sp->w, sp->e, sp->tv);
1242 if (FD_ISSET (sp->wakeup_socket, sp->r))
1244 FD_CLR (sp->wakeup_socket, sp->r);
1247 SetEvent (sp->wakeup);
1255 * Check if sockets or pipes meet certain conditions
1256 * @param rfds set of sockets or pipes to be checked for readability
1257 * @param wfds set of sockets or pipes to be checked for writability
1258 * @param efds set of sockets or pipes to be checked for exceptions
1259 * @param timeout relative value when to return
1260 * @return number of selected sockets or pipes, #GNUNET_SYSERR on error
1263 GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
1264 struct GNUNET_NETWORK_FDSet *wfds,
1265 struct GNUNET_NETWORK_FDSet *efds,
1266 const struct GNUNET_TIME_Relative timeout)
1272 int read_handles = 0;
1273 int write_handles = 0;
1281 static HANDLE hEventPipeWrite = 0;
1282 static HANDLE hEventReadReady = 0;
1284 static struct _select_params sp;
1285 static HANDLE select_thread = NULL;
1286 static HANDLE select_finished_event = NULL;
1287 static HANDLE select_standby_event = NULL;
1288 static SOCKET select_wakeup_socket = -1;
1289 static SOCKET select_send_socket = -1;
1290 static struct timeval select_timeout;
1293 int writePipePos = 0;
1295 HANDLE handle_array[FD_SETSIZE + 2];
1296 int returncode = -1;
1297 int returnedpos = 0;
1299 struct GNUNET_CONTAINER_SList *handles_read;
1300 struct GNUNET_CONTAINER_SList *handles_write;
1301 struct GNUNET_CONTAINER_SList *handles_except;
1315 /* TODO: Make this growable */
1316 struct GNUNET_DISK_FileHandle *readArray[50];
1324 handles += read_handles = GNUNET_CONTAINER_slist_count (rfds->handles);
1327 struct GNUNET_CONTAINER_SList_Iterator t;
1329 for (t = GNUNET_CONTAINER_slist_begin (rfds->handles);
1330 GNUNET_CONTAINER_slist_end (&t) != GNUNET_YES;
1331 GNUNET_CONTAINER_slist_next (&t))
1333 struct GNUNET_DISK_FileHandle *fh;
1335 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (&t,
1337 LOG (GNUNET_ERROR_TYPE_DEBUG, "FD 0x%x (0x%x) is SET in rfds\n", fh->h,
1346 nfds = GNUNET_MAX (nfds, wfds->nsds);
1348 handles += write_handles = GNUNET_CONTAINER_slist_count (wfds->handles);
1353 nfds = GNUNET_MAX (nfds, efds->nsds);
1355 handles += ex_handles = GNUNET_CONTAINER_slist_count (efds->handles);
1360 (timeout.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
1367 LOG (GNUNET_ERROR_TYPE_ERROR,
1369 ("Fatal internal logic error, process hangs in `%s' (abort with CTRL-C)!\n"),
1373 tv.tv_sec = timeout.rel_value_us / GNUNET_TIME_UNIT_SECONDS.rel_value_us;
1375 (timeout.rel_value_us -
1376 (tv.tv_sec * GNUNET_TIME_UNIT_SECONDS.rel_value_us));
1377 return select (nfds,
1378 (NULL != rfds) ? &rfds->sds : NULL,
1379 (NULL != wfds) ? &wfds->sds : NULL,
1380 (NULL != efds) ? &efds->sds : NULL,
1381 (timeout.rel_value_us ==
1382 GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us) ? NULL : &tv);
1385 #define SAFE_FD_ISSET(fd, set) (set != NULL && FD_ISSET(fd, set))
1386 /* calculate how long we need to wait in milliseconds */
1387 if (timeout.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
1388 ms_total = INFINITE;
1391 ms_total = timeout.rel_value_us / GNUNET_TIME_UNIT_MILLISECONDS.rel_value_us;
1392 if (timeout.rel_value_us / GNUNET_TIME_UNIT_MILLISECONDS.rel_value_us > 0xFFFFFFFFLL - 1)
1395 ms_total = 0xFFFFFFFF - 1;
1398 /* select() may be used as a portable way to sleep */
1399 if (!(rfds || wfds || efds))
1405 if (NULL == select_thread)
1407 SOCKET select_listening_socket = -1;
1408 struct sockaddr_in s_in;
1413 select_standby_event = CreateEvent (NULL, TRUE, FALSE, NULL);
1414 select_finished_event = CreateEvent (NULL, TRUE, FALSE, NULL);
1416 select_wakeup_socket = WSASocket (AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, WSA_FLAG_OVERLAPPED);
1418 select_listening_socket = WSASocket (AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, WSA_FLAG_OVERLAPPED);
1421 res = ioctlsocket (select_wakeup_socket, FIONBIO, &p);
1422 LOG (GNUNET_ERROR_TYPE_DEBUG, "Select thread initialization: ioctlsocket() returns %d\n", res);
1424 alen = sizeof (s_in);
1425 s_in.sin_family = AF_INET;
1427 s_in.sin_addr.S_un.S_un_b.s_b1 = 127;
1428 s_in.sin_addr.S_un.S_un_b.s_b2 = 0;
1429 s_in.sin_addr.S_un.S_un_b.s_b3 = 0;
1430 s_in.sin_addr.S_un.S_un_b.s_b4 = 1;
1431 res = bind (select_listening_socket, (const struct sockaddr *) &s_in, sizeof (s_in));
1432 LOG (GNUNET_ERROR_TYPE_DEBUG, "Select thread initialization: bind() returns %d\n", res);
1434 res = getsockname (select_listening_socket, (struct sockaddr *) &s_in, &alen);
1435 LOG (GNUNET_ERROR_TYPE_DEBUG, "Select thread initialization: getsockname() returns %d\n", res);
1437 res = listen (select_listening_socket, SOMAXCONN);
1438 LOG (GNUNET_ERROR_TYPE_DEBUG, "Select thread initialization: listen() returns %d\n", res);
1440 res = connect (select_wakeup_socket, (const struct sockaddr *) &s_in, sizeof (s_in));
1441 LOG (GNUNET_ERROR_TYPE_DEBUG, "Select thread initialization: connect() returns %d\n", res);
1443 select_send_socket = accept (select_listening_socket, (struct sockaddr *) &s_in, &alen);
1445 closesocket (select_listening_socket);
1447 sp.wakeup = select_finished_event;
1448 sp.standby = select_standby_event;
1449 sp.wakeup_socket = select_wakeup_socket;
1451 select_thread = CreateThread (NULL, 0, _selector, &sp, 0, NULL);
1455 handles_read = GNUNET_CONTAINER_slist_create ();
1456 handles_write = GNUNET_CONTAINER_slist_create ();
1457 handles_except = GNUNET_CONTAINER_slist_create ();
1468 FD_COPY (&rfds->sds, &aread);
1470 FD_COPY (&rfds->sds, &bread);
1475 FD_COPY (&wfds->sds, &awrite);
1477 FD_COPY (&wfds->sds, &bwrite);
1482 FD_COPY (&efds->sds, &aexcept);
1484 FD_COPY (&efds->sds, &bexcept);
1488 /* Start by doing a fast check on sockets and pipes (without waiting). It is cheap, and is sufficient most of the time.
1489 By profiling we detected that to be true in 90% of the cases.
1492 /* Do the select now */
1493 select_timeout.tv_sec = 0;
1494 select_timeout.tv_usec = 0;
1496 /* Copy all the writes to the except, so we can detect connect() errors */
1497 for (i = 0; i < awrite.fd_count; i++)
1498 FD_SET (awrite.fd_array[i], &aexcept);
1499 if (aread.fd_count > 0 || awrite.fd_count > 0 || aexcept.fd_count > 0)
1500 selectret = select (1, (rfds != NULL) ? &aread : NULL,
1501 (wfds != NULL) ? &awrite : NULL, &aexcept, &select_timeout);
1504 if (selectret == -1)
1506 /* Throw an error early on, while we still have the context. */
1507 LOG (GNUNET_ERROR_TYPE_ERROR, "W32 select(%d, %d, %d) failed: %lu\n",
1508 rfds ? aread.fd_count : 0, wfds ? awrite.fd_count : 0, aexcept.fd_count, GetLastError ());
1512 /* Check aexcept, add its contents to awrite
1513 This is technically wrong (aexcept might have its own descriptors), we should
1514 have checked that descriptors were in awrite originally before re-adding them from
1515 aexcept. Luckily, GNUnet never uses aexcept for anything, so this does not become a problem (yet). */
1516 for (i = 0; i < aexcept.fd_count; i++)
1517 FD_SET (aexcept.fd_array[i], &awrite);
1519 /* If our select returned something or is a 0-timed request, then also check the pipes and get out of here! */
1520 /* Sadly, it means code duplication :( */
1521 if ((selectret > 0) || (ms_total == 0))
1524 if (rfds && read_handles)
1526 struct GNUNET_CONTAINER_SList_Iterator i;
1529 for (c = 0, i = GNUNET_CONTAINER_slist_begin (rfds->handles);
1530 GNUNET_CONTAINER_slist_end (&i) != GNUNET_YES;
1531 GNUNET_CONTAINER_slist_next (&i), c++)
1533 struct GNUNET_DISK_FileHandle *fh;
1535 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (&i,NULL);
1536 if (fh->type == GNUNET_DISK_HANLDE_TYPE_PIPE)
1542 DWORD waitstatus = 0;
1543 bret = PeekNamedPipe (fh->h, NULL, 0, NULL, &waitstatus, NULL);
1544 error = GetLastError ();
1545 LOG (GNUNET_ERROR_TYPE_DEBUG, "Peek at read pipe %d (0x%x) returned %d (%d bytes available) GLE %u\n",
1546 c, fh->h, bret, waitstatus, error);
1549 /* TODO: either add more errors to this condition, or eliminate it
1550 * entirely (failed to peek -> pipe is in serious trouble, should
1551 * be selected as readable).
1553 if (error != ERROR_BROKEN_PIPE && error != ERROR_INVALID_HANDLE)
1556 else if (waitstatus <= 0)
1558 GNUNET_CONTAINER_slist_add (handles_read, GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1559 fh, sizeof (struct GNUNET_DISK_FileHandle));
1561 LOG (GNUNET_ERROR_TYPE_DEBUG, "Added read Pipe 0x%x (0x%x)\n",
1566 GNUNET_CONTAINER_slist_add (handles_read, GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1567 fh, sizeof (struct GNUNET_DISK_FileHandle));
1572 if (wfds && write_handles)
1574 LOG (GNUNET_ERROR_TYPE_DEBUG,
1575 "Adding the write ready event to the array as %d\n", nhandles);
1576 GNUNET_CONTAINER_slist_append (handles_write, wfds->handles);
1577 retcode += write_handles;
1579 if (efds && ex_handles)
1581 struct GNUNET_CONTAINER_SList_Iterator i;
1583 for (i = GNUNET_CONTAINER_slist_begin (efds->handles);
1584 GNUNET_CONTAINER_slist_end (&i) != GNUNET_YES;
1585 GNUNET_CONTAINER_slist_next (&i))
1587 struct GNUNET_DISK_FileHandle *fh;
1590 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (&i, NULL);
1591 if (fh->type == GNUNET_DISK_HANLDE_TYPE_PIPE)
1593 if (PeekNamedPipe (fh->h, NULL, 0, NULL, &dwBytes, NULL))
1595 GNUNET_CONTAINER_slist_add (handles_except, GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1596 fh, sizeof (struct GNUNET_DISK_FileHandle));
1602 /* Add our select() result.*/
1604 retcode += selectret;
1608 GNUNET_NETWORK_fdset_zero (rfds);
1609 if (selectret != -1)
1610 GNUNET_NETWORK_fdset_copy_native (rfds, &aread, selectret);
1611 GNUNET_CONTAINER_slist_append (rfds->handles, handles_read);
1615 GNUNET_NETWORK_fdset_zero (wfds);
1616 if (selectret != -1)
1617 GNUNET_NETWORK_fdset_copy_native (wfds, &awrite, selectret);
1618 GNUNET_CONTAINER_slist_append (wfds->handles, handles_write);
1622 GNUNET_NETWORK_fdset_zero (efds);
1623 if (selectret != -1)
1624 GNUNET_NETWORK_fdset_copy_native (efds, &aexcept, selectret);
1625 GNUNET_CONTAINER_slist_append (efds->handles, handles_except);
1627 GNUNET_CONTAINER_slist_destroy (handles_read);
1628 GNUNET_CONTAINER_slist_destroy (handles_write);
1629 GNUNET_CONTAINER_slist_destroy (handles_except);
1631 if (selectret == -1)
1636 /* If we got this far, use slower implementation that is able to do a waiting select
1637 on both sockets and pipes simultaneously */
1639 /* Events for pipes */
1640 if (!hEventReadReady)
1641 hEventReadReady = CreateEvent (NULL, TRUE, TRUE, NULL);
1642 if (!hEventPipeWrite)
1643 hEventPipeWrite = CreateEvent (NULL, TRUE, TRUE, NULL);
1659 FD_COPY (&rfds->sds, &aread);
1661 FD_COPY (&rfds->sds, &bread);
1666 FD_COPY (&wfds->sds, &awrite);
1668 FD_COPY (&wfds->sds, &bwrite);
1673 FD_COPY (&efds->sds, &aexcept);
1675 FD_COPY (&efds->sds, &bexcept);
1678 /* We will first Add the PIPES to the events */
1680 if (rfds && read_handles)
1682 struct GNUNET_CONTAINER_SList_Iterator i;
1684 for (i = GNUNET_CONTAINER_slist_begin (rfds->handles);
1685 GNUNET_CONTAINER_slist_end (&i) != GNUNET_YES;
1686 GNUNET_CONTAINER_slist_next (&i))
1688 struct GNUNET_DISK_FileHandle *fh;
1690 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (&i,
1692 if (fh->type == GNUNET_DISK_HANLDE_TYPE_PIPE)
1694 /* Read zero bytes to check the status of the pipe */
1695 LOG (GNUNET_ERROR_TYPE_DEBUG, "Reading 0 bytes from the pipe 0x%x\n",
1697 if (!ReadFile (fh->h, NULL, 0, NULL, fh->oOverlapRead))
1699 DWORD error_code = GetLastError ();
1701 if (error_code == ERROR_IO_PENDING)
1703 LOG (GNUNET_ERROR_TYPE_DEBUG,
1704 "Adding the pipe's 0x%x overlapped event to the array as %d\n",
1706 handle_array[nhandles++] = fh->oOverlapRead->hEvent;
1707 readArray[readPipes++] = fh;
1711 LOG (GNUNET_ERROR_TYPE_DEBUG,
1712 "Read failed, adding the read ready event to the array as %d\n", nhandles);
1713 handle_array[nhandles++] = hEventReadReady;
1714 readArray[readPipes++] = fh;
1719 LOG (GNUNET_ERROR_TYPE_DEBUG,
1720 "Adding the read ready event to the array as %d\n", nhandles);
1721 handle_array[nhandles++] = hEventReadReady;
1722 readArray[readPipes++] = fh;
1727 GNUNET_CONTAINER_slist_add (handles_read,
1728 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1729 fh, sizeof (struct GNUNET_DISK_FileHandle));
1733 if (wfds && write_handles)
1735 LOG (GNUNET_ERROR_TYPE_DEBUG,
1736 "Adding the write ready event to the array as %d\n", nhandles);
1737 handle_array[nhandles++] = hEventPipeWrite;
1738 writePipePos = nhandles;
1740 if (efds && ex_handles)
1742 struct GNUNET_CONTAINER_SList_Iterator i;
1744 for (i = GNUNET_CONTAINER_slist_begin (efds->handles);
1745 GNUNET_CONTAINER_slist_end (&i) != GNUNET_YES;
1746 GNUNET_CONTAINER_slist_next (&i))
1748 struct GNUNET_DISK_FileHandle *fh;
1751 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (&i,
1753 if (fh->type == GNUNET_DISK_HANLDE_TYPE_PIPE)
1755 if (!PeekNamedPipe (fh->h, NULL, 0, NULL, &dwBytes, NULL))
1757 GNUNET_CONTAINER_slist_add (handles_except,
1758 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1760 sizeof (struct GNUNET_DISK_FileHandle));
1770 LOG (GNUNET_ERROR_TYPE_DEBUG,
1771 "Adding the socket event to the array as %d\n",
1773 handle_array[nhandles++] = select_finished_event;
1774 if (timeout.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
1778 select_timeout.tv_sec = timeout.rel_value_us / GNUNET_TIME_UNIT_SECONDS.rel_value_us;
1779 select_timeout.tv_usec =(timeout.rel_value_us -
1780 (select_timeout.tv_sec * GNUNET_TIME_UNIT_SECONDS.rel_value_us));
1781 sp.tv = &select_timeout;
1783 FD_SET (select_wakeup_socket, &aread);
1787 /* Failed connections cause sockets to be set in errorfds on W32,
1788 * but on POSIX it should set them in writefds.
1789 * First copy all awrite sockets to aexcept, later we'll
1790 * check aexcept and set its contents in awrite as well
1791 * Sockets are also set in errorfds when OOB data is available,
1792 * but we don't use OOB data.
1794 for (i = 0; i < awrite.fd_count; i++)
1795 FD_SET (awrite.fd_array[i], &aexcept);
1796 ResetEvent (select_finished_event);
1797 SetEvent (select_standby_event);
1800 handle_array[nhandles] = NULL;
1801 LOG (GNUNET_ERROR_TYPE_DEBUG, "nfds: %d, handles: %d, will wait: %llu ms\n",
1802 nfds, nhandles, (unsigned long long) ms_total);
1806 WaitForMultipleObjects (nhandles, handle_array, FALSE, ms_total);
1807 LOG (GNUNET_ERROR_TYPE_DEBUG, "WaitForMultipleObjects Returned : %d\n",
1812 i = (int) WaitForSingleObject (select_finished_event, INFINITE);
1813 returncode = WAIT_TIMEOUT;
1817 /* Shouldn't come this far. If it does - investigate. */
1823 /* Don't wake up select-thread when delay is 0, it should return immediately
1824 * and wake up by itself.
1827 i = send (select_send_socket, (const char *) &returnedpos, 1, 0);
1828 i = (int) WaitForSingleObject (select_finished_event, INFINITE);
1829 LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished waiting for the select thread: %d %d\n", i, sp.status);
1834 i = recv (select_wakeup_socket, (char *) &returnedpos, 1, 0);
1837 /* Check aexcept, add its contents to awrite */
1838 for (i = 0; i < aexcept.fd_count; i++)
1839 FD_SET (aexcept.fd_array[i], &awrite);
1842 returnedpos = returncode - WAIT_OBJECT_0;
1843 LOG (GNUNET_ERROR_TYPE_DEBUG, "return pos is : %d\n", returnedpos);
1845 if (nhandles && (returnedpos < nhandles))
1850 retcode += sp.status;
1852 if ((writePipePos != -1) && (returnedpos < writePipePos))
1854 GNUNET_CONTAINER_slist_append (handles_write, wfds->handles);
1855 retcode += write_handles;
1856 LOG (GNUNET_ERROR_TYPE_DEBUG, "Added write pipe\n");
1858 LOG (GNUNET_ERROR_TYPE_DEBUG, "ReadPipes is : %d\n", readPipes);
1859 /* We have some pipes ready for read. */
1860 if (returnedpos < readPipes)
1862 for (i = 0; i < readPipes; i++)
1870 PeekNamedPipe (readArray[i]->h, NULL, 0, NULL, &waitstatus, NULL);
1871 error = GetLastError ();
1872 LOG (GNUNET_ERROR_TYPE_DEBUG,
1873 "Peek at read pipe %d (0x%x) returned %d (%d bytes available) GLE %u\n",
1874 i, readArray[i]->h, bret, waitstatus, error);
1877 /* TODO: either add more errors to this condition, or eliminate it
1878 * entirely (failed to peek -> pipe is in serious trouble, should
1879 * be selected as readable).
1881 if (error != ERROR_BROKEN_PIPE && error != ERROR_INVALID_HANDLE)
1884 else if (waitstatus <= 0)
1886 GNUNET_CONTAINER_slist_add (handles_read,
1887 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1889 sizeof (struct GNUNET_DISK_FileHandle));
1891 LOG (GNUNET_ERROR_TYPE_DEBUG, "Added read Pipe 0x%x (0x%x)\n",
1892 readArray[i], readArray[i]->h);
1896 if (!nhandles || (returnedpos >= nhandles))
1897 LOG (GNUNET_ERROR_TYPE_DEBUG, "Returning from _select() with nothing!\n");
1900 struct GNUNET_CONTAINER_SList_Iterator t;
1902 for (t = GNUNET_CONTAINER_slist_begin (rfds->handles);
1903 GNUNET_CONTAINER_slist_end (&t) != GNUNET_YES;
1904 GNUNET_CONTAINER_slist_next (&t))
1906 struct GNUNET_DISK_FileHandle *fh;
1908 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (&t,
1910 if (fh->type == GNUNET_DISK_HANLDE_TYPE_PIPE)
1915 LOG (GNUNET_ERROR_TYPE_DEBUG, "Zeroing rfds%s\n", (retcode != -1 && nhandles && (returnedpos < nhandles)) ? ", copying fdset" : "");
1916 GNUNET_NETWORK_fdset_zero (rfds);
1917 if (retcode != -1 && nhandles && (returnedpos < nhandles))
1918 GNUNET_NETWORK_fdset_copy_native (rfds, &aread, retcode);
1919 GNUNET_CONTAINER_slist_append (rfds->handles, handles_read);
1923 LOG (GNUNET_ERROR_TYPE_DEBUG, "Zeroing wfds%s\n", (retcode != -1 && nhandles && (returnedpos < nhandles)) ? ", copying fdset" : "");
1924 GNUNET_NETWORK_fdset_zero (wfds);
1925 if (retcode != -1 && nhandles && (returnedpos < nhandles))
1926 GNUNET_NETWORK_fdset_copy_native (wfds, &awrite, retcode);
1927 GNUNET_CONTAINER_slist_append (wfds->handles, handles_write);
1931 LOG (GNUNET_ERROR_TYPE_DEBUG, "Zeroing efds%s\n", (retcode != -1 && nhandles && (returnedpos < nhandles)) ? ", copying fdset" : "");
1932 GNUNET_NETWORK_fdset_zero (efds);
1933 if (retcode != -1 && nhandles && (returnedpos < nhandles))
1934 GNUNET_NETWORK_fdset_copy_native (efds, &aexcept, retcode);
1935 GNUNET_CONTAINER_slist_append (efds->handles, handles_except);
1937 GNUNET_CONTAINER_slist_destroy (handles_read);
1938 GNUNET_CONTAINER_slist_destroy (handles_write);
1939 GNUNET_CONTAINER_slist_destroy (handles_except);
1943 struct GNUNET_CONTAINER_SList_Iterator t;
1945 LOG (GNUNET_ERROR_TYPE_DEBUG, "rfds:\n");
1946 for (i = 0; i < rfds->sds.fd_count; i++)
1948 LOG (GNUNET_ERROR_TYPE_DEBUG, "%d\n", rfds->sds.fd_array[i]);
1950 for (t = GNUNET_CONTAINER_slist_begin (rfds->handles);
1951 GNUNET_CONTAINER_slist_end (&t) != GNUNET_YES;
1952 GNUNET_CONTAINER_slist_next (&t))
1954 struct GNUNET_DISK_FileHandle *fh;
1956 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (&t,
1958 LOG (GNUNET_ERROR_TYPE_DEBUG, "%d\n", fh->h);
1963 LOG (GNUNET_ERROR_TYPE_DEBUG, "wfds:\n");
1964 for (i = 0; i < wfds->sds.fd_count; i++)
1966 LOG (GNUNET_ERROR_TYPE_DEBUG, "%d\n", wfds->sds.fd_array[i]);
1971 LOG (GNUNET_ERROR_TYPE_DEBUG, "efds:\n");
1972 for (i = 0; i < efds->sds.fd_count; i++)
1974 LOG (GNUNET_ERROR_TYPE_DEBUG, "%d\n", efds->sds.fd_array[i]);
1977 LOG (GNUNET_ERROR_TYPE_DEBUG, "Returning %d or 0\n", retcode);
1979 if (nhandles && (returnedpos < nhandles))
1986 /* end of network.c */