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
28 #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 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
91 "Failed to create test socket: %s\n",
105 * Given a unixpath that is too long (larger than UNIX_PATH_MAX),
106 * shorten it to an acceptable length while keeping it unique
107 * and making sure it remains a valid filename (if possible).
109 * @param unixpath long path, will be freed (or same pointer returned
110 * with moved 0-termination).
111 * @return shortened unixpath, NULL on error
114 GNUNET_NETWORK_shorten_unixpath (char *unixpath)
116 struct sockaddr_un dummy;
119 struct GNUNET_HashCode sh;
120 struct GNUNET_CRYPTO_HashAsciiEncoded ae;
123 upm = sizeof (dummy.sun_path);
124 slen = strlen (unixpath);
126 return unixpath; /* no shortening required */
127 GNUNET_CRYPTO_hash (unixpath, slen, &sh);
129 strlen (unixpath) >= upm)
131 if (NULL == (end = strrchr (unixpath, '/')))
133 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
134 _("Unable to shorten unix path `%s' while keeping name unique\n"),
136 GNUNET_free (unixpath);
141 GNUNET_CRYPTO_hash_to_enc (&sh, &ae);
142 strncat (unixpath, (char*) ae.encoding, 16);
148 #define FD_COPY(s, d) (memcpy ((d), (s), sizeof (fd_set)))
153 * Set if a socket should use blocking or non-blocking IO.
156 * @param doBlock blocking mode
157 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
160 GNUNET_NETWORK_socket_set_blocking (struct GNUNET_NETWORK_Handle *fd,
168 if (ioctlsocket (fd->fd, FIONBIO, &mode) == SOCKET_ERROR)
171 SetErrnoFromWinsockError (WSAGetLastError ());
172 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "ioctlsocket");
173 return GNUNET_SYSERR;
179 int flags = fcntl (fd->fd, F_GETFL);
184 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "fcntl");
185 return GNUNET_SYSERR;
188 flags &= ~O_NONBLOCK;
192 if (0 != fcntl (fd->fd, F_SETFL, flags))
195 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "fcntl");
196 return GNUNET_SYSERR;
204 * Make a socket non-inheritable to child processes
206 * @param h the socket to make non-inheritable
207 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
208 * @warning Not implemented on Windows
211 socket_set_inheritable (const struct GNUNET_NETWORK_Handle *h)
215 i = fcntl (h->fd, F_GETFD);
217 return GNUNET_SYSERR;
218 if (i == (i | FD_CLOEXEC))
221 if (fcntl (h->fd, F_SETFD, i) < 0)
222 return GNUNET_SYSERR;
226 b = SetHandleInformation ((HANDLE) h->fd, HANDLE_FLAG_INHERIT, 0);
229 SetErrnoFromWinsockError (WSAGetLastError ());
230 return GNUNET_SYSERR;
239 * The MSG_NOSIGNAL equivalent on Mac OS X
241 * @param h the socket to make non-delaying
244 socket_set_nosigpipe (const struct GNUNET_NETWORK_Handle *h)
249 setsockopt (h->fd, SOL_SOCKET, SO_NOSIGPIPE,
250 (const void *) &abs_value,
252 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
258 * Disable delays when sending data via the socket.
259 * (GNUnet makes sure that messages are as big as
262 * @param h the socket to make non-delaying
265 socket_set_nodelay (const struct GNUNET_NETWORK_Handle *h)
270 if (0 != setsockopt (h->fd, IPPROTO_TCP, TCP_NODELAY, &value, sizeof (value)))
271 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
273 const char *abs_value = "1";
276 setsockopt (h->fd, IPPROTO_TCP, TCP_NODELAY,
277 (const void *) abs_value,
279 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
285 * Perform proper canonical initialization for a network handle.
286 * Set it to non-blocking, make it non-inheritable to child
287 * processes, disable SIGPIPE, enable "nodelay" (if non-UNIX
288 * stream socket) and check that it is smaller than FD_SETSIZE.
290 * @param h socket to initialize
291 * @param af address family of the socket
292 * @param type socket type
293 * @return #GNUNET_OK on success, #GNUNET_SYSERR if initialization
294 * failed and the handle was destroyed
297 initialize_network_handle (struct GNUNET_NETWORK_Handle *h,
302 if (h->fd == INVALID_SOCKET)
305 SetErrnoFromWinsockError (WSAGetLastError ());
308 return GNUNET_SYSERR;
311 if (h->fd >= FD_SETSIZE)
313 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (h));
315 return GNUNET_SYSERR;
318 if (GNUNET_OK != socket_set_inheritable (h))
319 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
320 "socket_set_inheritable");
322 if (GNUNET_SYSERR == GNUNET_NETWORK_socket_set_blocking (h, GNUNET_NO))
325 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (h));
326 return GNUNET_SYSERR;
329 socket_set_nosigpipe (h);
331 if ( (type == SOCK_STREAM)
336 socket_set_nodelay (h);
342 * accept a new connection on a socket
344 * @param desc bound socket
345 * @param address address of the connecting peer, may be NULL
346 * @param address_len length of @a address
347 * @return client socket
349 struct GNUNET_NETWORK_Handle *
350 GNUNET_NETWORK_socket_accept (const struct GNUNET_NETWORK_Handle *desc,
351 struct sockaddr *address,
352 socklen_t *address_len)
354 struct GNUNET_NETWORK_Handle *ret;
356 ret = GNUNET_new (struct GNUNET_NETWORK_Handle);
359 struct sockaddr name;
360 socklen_t namelen = sizeof (name);
361 int gsn = getsockname (desc->fd, &name, &namelen);
364 LOG (GNUNET_ERROR_TYPE_DEBUG,
365 "Accepting connection on `%s'\n",
366 GNUNET_a2s (&name, namelen));
369 ret->fd = accept (desc->fd, address, address_len);
375 if (GNUNET_OK != initialize_network_handle (ret,
376 (NULL != address) ? address->sa_family : desc->af,
384 * Bind a socket to a particular address.
386 * @param desc socket to bind
387 * @param address address to be bound
388 * @param address_len length of @a address
389 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
392 GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc,
393 const struct sockaddr *address,
394 socklen_t address_len)
403 if (AF_INET6 == desc->af)
404 if (setsockopt (desc->fd, IPPROTO_IPV6, IPV6_V6ONLY,
407 LOG_STRERROR (GNUNET_ERROR_TYPE_DEBUG, "setsockopt");
415 /* This is required here for TCP sockets, but only on UNIX */
416 if ((SOCK_STREAM == desc->type)
417 && (0 != setsockopt (desc->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on))))
418 LOG_STRERROR (GNUNET_ERROR_TYPE_DEBUG, "setsockopt");
423 /* set permissions of newly created non-abstract UNIX domain socket to
424 "user-only"; applications can choose to relax this later */
425 mode_t old_mask = 0; /* assigned to make compiler happy */
426 const struct sockaddr_un *un;
427 int not_abstract = 0;
429 if ((AF_UNIX == address->sa_family)
430 && (NULL != (un = (const struct sockaddr_un *) address)->sun_path)
431 && ('\0' != un->sun_path[0]) ) /* Not an abstract socket */
434 old_mask = umask (S_IWGRP | S_IRGRP | S_IXGRP | S_IWOTH | S_IROTH | S_IXOTH);
437 ret = bind (desc->fd, address, address_len);
440 (void) umask (old_mask);
444 if (SOCKET_ERROR == ret)
445 SetErrnoFromWinsockError (WSAGetLastError ());
448 return GNUNET_SYSERR;
450 desc->addr = GNUNET_malloc (address_len);
451 memcpy (desc->addr, address, address_len);
452 desc->addrlen = address_len;
462 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
465 GNUNET_NETWORK_socket_close (struct GNUNET_NETWORK_Handle *desc)
473 ret = closesocket (desc->fd);
474 error = WSAGetLastError ();
475 SetErrnoFromWinsockError (error);
476 LOG (GNUNET_ERROR_TYPE_DEBUG,
477 "Closed 0x%x, closesocket() returned %d, GLE is %u\n", desc->fd, ret,
480 ret = close (desc->fd);
483 const struct sockaddr_un *un;
485 /* Cleanup the UNIX domain socket and its parent directories in case of non
487 if ((AF_UNIX == desc->af) && (NULL != desc->addr)
488 && (NULL != (un = (const struct sockaddr_un *) desc->addr)->sun_path)
489 && ('\0' != un->sun_path[0]))
491 char *dirname = GNUNET_strndup (un->sun_path,
492 sizeof (un->sun_path));
494 if (0 != unlink (dirname))
496 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING,
504 len = strlen (dirname);
505 while ((len > 0) && (dirname[len] != DIR_SEPARATOR))
508 if ((0 != len) && (0 != rmdir (dirname)))
515 /* these are normal and can just be ignored */
518 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
525 GNUNET_free (dirname);
528 GNUNET_NETWORK_socket_free_memory_only_ (desc);
529 return (ret == 0) ? GNUNET_OK : GNUNET_SYSERR;
534 * Only free memory of a socket, keep the file descriptor untouched.
539 GNUNET_NETWORK_socket_free_memory_only_ (struct GNUNET_NETWORK_Handle *desc)
541 GNUNET_free_non_null (desc->addr);
547 * Box a native socket (and check that it is a socket).
549 * @param fd socket to box
550 * @return NULL on error (including not supported on target platform)
552 struct GNUNET_NETWORK_Handle *
553 GNUNET_NETWORK_socket_box_native (SOCKTYPE fd)
555 struct GNUNET_NETWORK_Handle *ret;
559 /* FIXME: Find a better call to check that FD is valid */
560 if (WSAIoctl (fd, FIONBIO, (void *) &i, sizeof (i), NULL, 0, &d, NULL, NULL) != 0)
561 return NULL; /* invalid FD */
562 ret = GNUNET_new (struct GNUNET_NETWORK_Handle);
567 if (fcntl (fd, F_GETFD) < 0)
568 return NULL; /* invalid FD */
569 ret = GNUNET_new (struct GNUNET_NETWORK_Handle);
578 * Connect a socket to some remote address.
581 * @param address peer address
582 * @param address_len length of @a address
583 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
586 GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Handle *desc,
587 const struct sockaddr *address,
588 socklen_t address_len)
592 ret = connect (desc->fd, address, address_len);
595 if (SOCKET_ERROR == ret)
597 SetErrnoFromWinsockError (WSAGetLastError ());
598 if (errno == EWOULDBLOCK)
602 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
610 * @param level protocol level of the option
611 * @param optname identifier of the option
612 * @param optval options
613 * @param optlen length of @a optval
614 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
617 GNUNET_NETWORK_socket_getsockopt (const struct GNUNET_NETWORK_Handle *desc,
618 int level, int optname, void *optval,
623 ret = getsockopt (desc->fd, level, optname, optval, optlen);
626 if (ret == 0 && level == SOL_SOCKET && optname == SO_ERROR)
627 *((int *) optval) = GetErrnoFromWinsockError (*((int *) optval));
629 else if (SOCKET_ERROR == ret)
630 SetErrnoFromWinsockError (WSAGetLastError ());
632 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
640 * @param backlog length of the listen queue
641 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
644 GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Handle *desc,
649 ret = listen (desc->fd, backlog);
652 if (SOCKET_ERROR == ret)
653 SetErrnoFromWinsockError (WSAGetLastError ());
656 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
661 * How much data is available to be read on this descriptor?
664 * @returns #GNUNET_NO if no data is available, or on error!
667 GNUNET_NETWORK_socket_recvfrom_amount (const struct GNUNET_NETWORK_Handle *
672 /* How much is there to be read? */
676 error = ioctl (desc->fd, FIONREAD, &pending);
678 return (ssize_t) pending;
683 error = ioctlsocket (desc->fd, FIONREAD, &pending);
684 if (error != SOCKET_ERROR)
685 return (ssize_t) pending;
692 * Read data from a socket (always non-blocking).
695 * @param buffer buffer
696 * @param length length of @a buffer
697 * @param src_addr either the source to recv from, or all zeroes
698 * to be filled in by recvfrom
699 * @param addrlen length of the @a src_addr
702 GNUNET_NETWORK_socket_recvfrom (const struct GNUNET_NETWORK_Handle *desc,
703 void *buffer, size_t length,
704 struct sockaddr *src_addr, socklen_t *addrlen)
712 flags |= MSG_DONTWAIT;
715 ret = recvfrom (desc->fd, buffer, length, flags, src_addr, addrlen);
717 if (SOCKET_ERROR == ret)
718 SetErrnoFromWinsockError (WSAGetLastError ());
725 * Read data from a connected socket (always non-blocking).
728 * @param buffer buffer
729 * @param length length of @a buffer
732 GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Handle * desc,
733 void *buffer, size_t length)
741 flags |= MSG_DONTWAIT;
743 ret = recv (desc->fd, buffer, length, flags);
745 if (SOCKET_ERROR == ret)
746 SetErrnoFromWinsockError (WSAGetLastError ());
753 * Send data (always non-blocking).
756 * @param buffer data to send
757 * @param length size of the @a buffer
758 * @return number of bytes sent, #GNUNET_SYSERR on error
761 GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Handle * desc,
762 const void *buffer, size_t length)
770 flags |= MSG_DONTWAIT;
774 flags |= MSG_NOSIGNAL;
777 ret = send (desc->fd, buffer, length, flags);
780 if (SOCKET_ERROR == ret)
781 SetErrnoFromWinsockError (WSAGetLastError ());
789 * Send data to a particular destination (always non-blocking).
790 * This function only works for UDP sockets.
793 * @param message data to send
794 * @param length size of the data
795 * @param dest_addr destination address
796 * @param dest_len length of @a address
797 * @return number of bytes sent, #GNUNET_SYSERR on error
800 GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Handle * desc,
801 const void *message, size_t length,
802 const struct sockaddr * dest_addr,
811 flags |= MSG_DONTWAIT;
814 flags |= MSG_NOSIGNAL;
816 ret = sendto (desc->fd, message, length, flags, dest_addr, dest_len);
818 if (SOCKET_ERROR == ret)
819 SetErrnoFromWinsockError (WSAGetLastError ());
829 * @param level protocol level of the option
830 * @param option_name option identifier
831 * @param option_value value to set
832 * @param option_len size of @a option_value
833 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
836 GNUNET_NETWORK_socket_setsockopt (struct GNUNET_NETWORK_Handle *fd,
839 const void *option_value,
840 socklen_t option_len)
844 ret = setsockopt (fd->fd,
850 if (SOCKET_ERROR == ret)
851 SetErrnoFromWinsockError (WSAGetLastError ());
853 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
858 * Create a new socket. Configure it for non-blocking IO and
859 * mark it as non-inheritable to child processes (set the
860 * close-on-exec flag).
862 * @param domain domain of the socket
863 * @param type socket type
864 * @param protocol network protocol
865 * @return new socket, NULL on error
867 struct GNUNET_NETWORK_Handle *
868 GNUNET_NETWORK_socket_create (int domain, int type, int protocol)
870 struct GNUNET_NETWORK_Handle *ret;
872 ret = GNUNET_new (struct GNUNET_NETWORK_Handle);
873 ret->fd = socket (domain, type, protocol);
880 initialize_network_handle (ret, domain, type))
887 * Shut down socket operations
889 * @param how type of shutdown
890 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
893 GNUNET_NETWORK_socket_shutdown (struct GNUNET_NETWORK_Handle *desc, int how)
897 ret = shutdown (desc->fd, how);
900 SetErrnoFromWinsockError (WSAGetLastError ());
902 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
907 * Disable the "CORK" feature for communication with the given socket,
908 * forcing the OS to immediately flush the buffer on transmission
909 * instead of potentially buffering multiple messages. Essentially
910 * reduces the OS send buffers to zero.
913 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
916 GNUNET_NETWORK_socket_disable_corking (struct GNUNET_NETWORK_Handle *desc)
925 setsockopt (desc->fd, SOL_SOCKET, SO_SNDBUF, (char *) &value,
927 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
930 setsockopt (desc->fd, SOL_SOCKET, SO_RCVBUF, (char *) &value,
932 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
938 setsockopt (desc->fd, SOL_SOCKET, SO_SNDBUF, &value, sizeof (value))))
939 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
942 setsockopt (desc->fd, SOL_SOCKET, SO_RCVBUF, &value, sizeof (value))))
943 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
945 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
955 GNUNET_NETWORK_fdset_zero (struct GNUNET_NETWORK_FDSet *fds)
960 GNUNET_CONTAINER_slist_clear (fds->handles);
966 * Add a socket to the FD set
969 * @param desc socket to add
972 GNUNET_NETWORK_fdset_set (struct GNUNET_NETWORK_FDSet *fds,
973 const struct GNUNET_NETWORK_Handle *desc)
975 FD_SET (desc->fd, &fds->sds);
976 if (desc->fd + 1 > fds->nsds)
977 fds->nsds = desc->fd + 1;
982 * Check whether a socket is part of the fd set
986 * @return 0 if the FD is not set
989 GNUNET_NETWORK_fdset_isset (const struct GNUNET_NETWORK_FDSet *fds,
990 const struct GNUNET_NETWORK_Handle *desc)
992 return FD_ISSET (desc->fd, &fds->sds);
997 * Add one fd set to another
999 * @param dst the fd set to add to
1000 * @param src the fd set to add from
1003 GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst,
1004 const struct GNUNET_NETWORK_FDSet *src)
1009 for (nfds = src->nsds; nfds >= 0; nfds--)
1010 if (FD_ISSET (nfds, &src->sds))
1013 FD_SET (nfds, &dst->sds);
1014 if (nfds + 1 > dst->nsds)
1015 dst->nsds = nfds + 1;
1018 /* This is MinGW32-specific implementation that relies on the code that
1019 * winsock2.h defines for FD_SET. Namely, it relies on FD_SET checking
1020 * that fd being added is not already in the set.
1021 * Also relies on us knowing what's inside fd_set (fd_count and fd_array).
1024 for (i = 0; i < src->sds.fd_count; i++)
1025 FD_SET (src->sds.fd_array[i], &dst->sds);
1026 if (src->nsds > dst->nsds)
1027 dst->nsds = src->nsds;
1029 GNUNET_CONTAINER_slist_append (dst->handles, src->handles);
1035 * Copy one fd set to another
1037 * @param to destination
1038 * @param from source
1041 GNUNET_NETWORK_fdset_copy (struct GNUNET_NETWORK_FDSet *to,
1042 const struct GNUNET_NETWORK_FDSet *from)
1044 FD_COPY (&from->sds, &to->sds);
1045 to->nsds = from->nsds;
1048 GNUNET_CONTAINER_slist_clear (to->handles);
1049 GNUNET_CONTAINER_slist_append (to->handles, from->handles);
1055 * Return file descriptor for this network handle
1057 * @param desc wrapper to process
1058 * @return POSIX file descriptor
1061 GNUNET_NETWORK_get_fd (struct GNUNET_NETWORK_Handle *desc)
1068 * Return sockaddr for this network handle
1070 * @param desc wrapper to process
1074 GNUNET_NETWORK_get_addr (struct GNUNET_NETWORK_Handle *desc)
1081 * Return sockaddr length for this network handle
1083 * @param desc wrapper to process
1084 * @return socklen_t for sockaddr
1087 GNUNET_NETWORK_get_addrlen (struct GNUNET_NETWORK_Handle *desc)
1089 return desc->addrlen;
1094 * Copy a native fd set
1096 * @param to destination
1097 * @param from native source set
1098 * @param nfds the biggest socket number in from + 1
1101 GNUNET_NETWORK_fdset_copy_native (struct GNUNET_NETWORK_FDSet *to,
1102 const fd_set * from, int nfds)
1104 FD_COPY (from, &to->sds);
1110 * Set a native fd in a set
1112 * @param to destination
1113 * @param nfd native FD to set
1116 GNUNET_NETWORK_fdset_set_native (struct GNUNET_NETWORK_FDSet *to, int nfd)
1118 GNUNET_assert ((nfd >= 0) && (nfd < FD_SETSIZE));
1119 FD_SET (nfd, &to->sds);
1120 to->nsds = GNUNET_MAX (nfd + 1, to->nsds);
1125 * Test native fd in a set
1127 * @param to set to test, NULL for empty set
1128 * @param nfd native FD to test, or -1 for none
1129 * @return #GNUNET_YES if FD is set in the set
1132 GNUNET_NETWORK_fdset_test_native (const struct GNUNET_NETWORK_FDSet *to,
1135 if ((nfd == -1) || (to == NULL))
1137 return FD_ISSET (nfd, &to->sds) ? GNUNET_YES : GNUNET_NO;
1142 * Add a file handle to the fd set
1144 * @param h the file handle to add
1147 GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds,
1148 const struct GNUNET_DISK_FileHandle *h)
1151 GNUNET_CONTAINER_slist_add (fds->handles,
1152 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT, h,
1153 sizeof (struct GNUNET_DISK_FileHandle));
1158 GNUNET_DISK_internal_file_handle_ (h, &fd, sizeof (int));
1159 FD_SET (fd, &fds->sds);
1160 if (fd + 1 > fds->nsds)
1168 * Check if a file handle is part of an fd set
1170 * @param h file handle
1171 * @return #GNUNET_YES if the file handle is part of the set
1174 GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds,
1175 const struct GNUNET_DISK_FileHandle *h)
1179 return GNUNET_CONTAINER_slist_contains (fds->handles, h,
1181 GNUNET_DISK_FileHandle));
1183 return FD_ISSET (h->fd, &fds->sds);
1189 * Checks if two fd sets overlap
1190 * @param fds1 first fd set
1191 * @param fds2 second fd set
1192 * @return #GNUNET_YES if they do overlap, #GNUNET_NO otherwise
1195 GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1,
1196 const struct GNUNET_NETWORK_FDSet *fds2)
1202 if (nfds > fds2->nsds)
1207 if (FD_ISSET (nfds, &fds1->sds) && FD_ISSET (nfds, &fds2->sds))
1211 struct GNUNET_CONTAINER_SList_Iterator it;
1212 struct GNUNET_DISK_FileHandle *h;
1216 /*This code is somewhat hacky, we are not supposed to know what's
1217 * inside of fd_set; also the O(n^2) is really bad... */
1219 for (i = 0; i < fds1->sds.fd_count; i++)
1221 for (j = 0; j < fds2->sds.fd_count; j++)
1223 if (fds1->sds.fd_array[i] == fds2->sds.fd_array[j])
1227 it = GNUNET_CONTAINER_slist_begin (fds1->handles);
1228 while (GNUNET_CONTAINER_slist_end (&it) != GNUNET_YES)
1231 struct GNUNET_CONTAINER_SList_Iterator t;
1233 h = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (&it,
1236 LOG (GNUNET_ERROR_TYPE_DEBUG, "Checking that FD 0x%x is in another set:\n",
1238 for (t = GNUNET_CONTAINER_slist_begin (fds2->handles);
1239 GNUNET_CONTAINER_slist_end (&t) != GNUNET_YES;
1240 GNUNET_CONTAINER_slist_next (&t))
1242 struct GNUNET_DISK_FileHandle *fh;
1244 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (&t,
1246 LOG (GNUNET_ERROR_TYPE_DEBUG, "0x%x\n", fh->h);
1249 if (GNUNET_CONTAINER_slist_contains
1250 (fds2->handles, h, sizeof (struct GNUNET_DISK_FileHandle)))
1254 GNUNET_CONTAINER_slist_next (&it);
1264 * @return a new fd set
1266 struct GNUNET_NETWORK_FDSet *
1267 GNUNET_NETWORK_fdset_create ()
1269 struct GNUNET_NETWORK_FDSet *fds;
1271 fds = GNUNET_new (struct GNUNET_NETWORK_FDSet);
1273 fds->handles = GNUNET_CONTAINER_slist_create ();
1275 GNUNET_NETWORK_fdset_zero (fds);
1281 * Releases the associated memory of an fd set
1286 GNUNET_NETWORK_fdset_destroy (struct GNUNET_NETWORK_FDSet *fds)
1289 GNUNET_CONTAINER_slist_destroy (fds->handles);
1295 struct _select_params
1303 SOCKET wakeup_socket;
1308 _selector (LPVOID p)
1310 struct _select_params *sp = p;
1314 WaitForSingleObject (sp->standby, INFINITE);
1315 ResetEvent (sp->standby);
1316 sp->status = select (1, sp->r, sp->w, sp->e, sp->tv);
1317 if (FD_ISSET (sp->wakeup_socket, sp->r))
1319 FD_CLR (sp->wakeup_socket, sp->r);
1322 SetEvent (sp->wakeup);
1331 * Check if sockets or pipes meet certain conditions
1333 * @param rfds set of sockets or pipes to be checked for readability
1334 * @param wfds set of sockets or pipes to be checked for writability
1335 * @param efds set of sockets or pipes to be checked for exceptions
1336 * @param timeout relative value when to return
1337 * @return number of selected sockets or pipes, #GNUNET_SYSERR on error
1340 GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
1341 struct GNUNET_NETWORK_FDSet *wfds,
1342 struct GNUNET_NETWORK_FDSet *efds,
1343 const struct GNUNET_TIME_Relative timeout)
1353 nfds = GNUNET_MAX (nfds, wfds->nsds);
1355 nfds = GNUNET_MAX (nfds, efds->nsds);
1357 (timeout.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us))
1360 LOG (GNUNET_ERROR_TYPE_ERROR,
1361 _("Fatal internal logic error, process hangs in `%s' (abort with CTRL-C)!\n"),
1364 tv.tv_sec = timeout.rel_value_us / GNUNET_TIME_UNIT_SECONDS.rel_value_us;
1366 (timeout.rel_value_us -
1367 (tv.tv_sec * GNUNET_TIME_UNIT_SECONDS.rel_value_us));
1368 return select (nfds,
1369 (NULL != rfds) ? &rfds->sds : NULL,
1370 (NULL != wfds) ? &wfds->sds : NULL,
1371 (NULL != efds) ? &efds->sds : NULL,
1372 (timeout.rel_value_us ==
1373 GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us) ? NULL : &tv);
1382 * Check if sockets or pipes meet certain conditions, version for W32.
1384 * @param rfds set of sockets or pipes to be checked for readability
1385 * @param wfds set of sockets or pipes to be checked for writability
1386 * @param efds set of sockets or pipes to be checked for exceptions
1387 * @param timeout relative value when to return
1388 * @return number of selected sockets or pipes, #GNUNET_SYSERR on error
1391 GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
1392 struct GNUNET_NETWORK_FDSet *wfds,
1393 struct GNUNET_NETWORK_FDSet *efds,
1394 const struct GNUNET_TIME_Relative timeout)
1399 int read_handles = 0;
1400 int write_handles = 0;
1404 uint64_t mcs_total = 0;
1405 DWORD ms_rounded = 0;
1409 static HANDLE hEventPipeWrite = 0;
1410 static HANDLE hEventReadReady = 0;
1412 static struct _select_params sp;
1413 static HANDLE select_thread = NULL;
1414 static HANDLE select_finished_event = NULL;
1415 static HANDLE select_standby_event = NULL;
1416 static SOCKET select_wakeup_socket = -1;
1417 static SOCKET select_send_socket = -1;
1418 static struct timeval select_timeout;
1421 int writePipePos = 0;
1423 HANDLE handle_array[FD_SETSIZE + 2];
1424 int returncode = -1;
1425 int returnedpos = 0;
1427 struct GNUNET_CONTAINER_SList *handles_read;
1428 struct GNUNET_CONTAINER_SList *handles_write;
1429 struct GNUNET_CONTAINER_SList *handles_except;
1443 /* TODO: Make this growable */
1444 struct GNUNET_DISK_FileHandle *readArray[50];
1450 handles += read_handles = GNUNET_CONTAINER_slist_count (rfds->handles);
1453 struct GNUNET_CONTAINER_SList_Iterator t;
1455 for (t = GNUNET_CONTAINER_slist_begin (rfds->handles);
1456 GNUNET_CONTAINER_slist_end (&t) != GNUNET_YES;
1457 GNUNET_CONTAINER_slist_next (&t))
1459 struct GNUNET_DISK_FileHandle *fh;
1461 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (&t,
1463 LOG (GNUNET_ERROR_TYPE_DEBUG, "FD 0x%x (0x%x) is SET in rfds\n", fh->h,
1471 nfds = GNUNET_MAX (nfds, wfds->nsds);
1472 handles += write_handles = GNUNET_CONTAINER_slist_count (wfds->handles);
1476 nfds = GNUNET_MAX (nfds, efds->nsds);
1477 handles += ex_handles = GNUNET_CONTAINER_slist_count (efds->handles);
1481 (timeout.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
1485 LOG (GNUNET_ERROR_TYPE_ERROR,
1486 _("Fatal internal logic error, process hangs in `%s' (abort with CTRL-C)!\n"),
1489 #define SAFE_FD_ISSET(fd, set) (set != NULL && FD_ISSET(fd, set))
1490 /* calculate how long we need to wait in microseconds */
1491 if (timeout.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
1493 mcs_total = INFINITE;
1494 ms_rounded = INFINITE;
1498 mcs_total = timeout.rel_value_us / GNUNET_TIME_UNIT_MICROSECONDS.rel_value_us;
1499 ms_rounded = (DWORD) (mcs_total / GNUNET_TIME_UNIT_MILLISECONDS.rel_value_us);
1500 if (mcs_total > 0 && ms_rounded == 0)
1503 /* select() may be used as a portable way to sleep */
1504 if (!(rfds || wfds || efds))
1510 if (NULL == select_thread)
1512 SOCKET select_listening_socket = -1;
1513 struct sockaddr_in s_in;
1518 select_standby_event = CreateEvent (NULL, TRUE, FALSE, NULL);
1519 select_finished_event = CreateEvent (NULL, TRUE, FALSE, NULL);
1521 select_wakeup_socket = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
1523 select_listening_socket = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
1526 res = ioctlsocket (select_wakeup_socket, FIONBIO, &p);
1527 LOG (GNUNET_ERROR_TYPE_DEBUG, "Select thread initialization: ioctlsocket() returns %d\n", res);
1529 alen = sizeof (s_in);
1530 s_in.sin_family = AF_INET;
1532 s_in.sin_addr.S_un.S_un_b.s_b1 = 127;
1533 s_in.sin_addr.S_un.S_un_b.s_b2 = 0;
1534 s_in.sin_addr.S_un.S_un_b.s_b3 = 0;
1535 s_in.sin_addr.S_un.S_un_b.s_b4 = 1;
1536 res = bind (select_listening_socket, (const struct sockaddr *) &s_in, sizeof (s_in));
1537 LOG (GNUNET_ERROR_TYPE_DEBUG, "Select thread initialization: bind() returns %d\n", res);
1539 res = getsockname (select_listening_socket, (struct sockaddr *) &s_in, &alen);
1540 LOG (GNUNET_ERROR_TYPE_DEBUG, "Select thread initialization: getsockname() returns %d\n", res);
1542 res = listen (select_listening_socket, SOMAXCONN);
1543 LOG (GNUNET_ERROR_TYPE_DEBUG, "Select thread initialization: listen() returns %d\n", res);
1545 res = connect (select_wakeup_socket, (const struct sockaddr *) &s_in, sizeof (s_in));
1546 LOG (GNUNET_ERROR_TYPE_DEBUG, "Select thread initialization: connect() returns %d\n", res);
1548 select_send_socket = accept (select_listening_socket, (struct sockaddr *) &s_in, &alen);
1550 closesocket (select_listening_socket);
1552 sp.wakeup = select_finished_event;
1553 sp.standby = select_standby_event;
1554 sp.wakeup_socket = select_wakeup_socket;
1556 select_thread = CreateThread (NULL, 0, _selector, &sp, 0, NULL);
1560 handles_read = GNUNET_CONTAINER_slist_create ();
1561 handles_write = GNUNET_CONTAINER_slist_create ();
1562 handles_except = GNUNET_CONTAINER_slist_create ();
1573 FD_COPY (&rfds->sds, &aread);
1575 FD_COPY (&rfds->sds, &bread);
1580 FD_COPY (&wfds->sds, &awrite);
1582 FD_COPY (&wfds->sds, &bwrite);
1587 FD_COPY (&efds->sds, &aexcept);
1589 FD_COPY (&efds->sds, &bexcept);
1593 /* Start by doing a fast check on sockets and pipes (without waiting). It is cheap, and is sufficient most of the time.
1594 By profiling we detected that to be true in 90% of the cases.
1597 /* Do the select now */
1598 select_timeout.tv_sec = 0;
1599 select_timeout.tv_usec = 0;
1601 /* Copy all the writes to the except, so we can detect connect() errors */
1602 for (i = 0; i < awrite.fd_count; i++)
1603 FD_SET (awrite.fd_array[i], &aexcept);
1604 if (aread.fd_count > 0 || awrite.fd_count > 0 || aexcept.fd_count > 0)
1605 selectret = select (1, (rfds != NULL) ? &aread : NULL,
1606 (wfds != NULL) ? &awrite : NULL, &aexcept, &select_timeout);
1609 if (selectret == -1)
1611 /* Throw an error early on, while we still have the context. */
1612 LOG (GNUNET_ERROR_TYPE_ERROR, "W32 select(%d, %d, %d) failed: %lu\n",
1613 rfds ? aread.fd_count : 0, wfds ? awrite.fd_count : 0, aexcept.fd_count, GetLastError ());
1617 /* Check aexcept, add its contents to awrite
1618 This is technically wrong (aexcept might have its own descriptors), we should
1619 have checked that descriptors were in awrite originally before re-adding them from
1620 aexcept. Luckily, GNUnet never uses aexcept for anything, so this does not become a problem (yet). */
1621 for (i = 0; i < aexcept.fd_count; i++)
1622 FD_SET (aexcept.fd_array[i], &awrite);
1624 /* If our select returned something or is a 0-timed request, then also check the pipes and get out of here! */
1625 /* Sadly, it means code duplication :( */
1626 if ((selectret > 0) || (mcs_total == 0))
1629 if (rfds && read_handles)
1631 struct GNUNET_CONTAINER_SList_Iterator i;
1634 for (c = 0, i = GNUNET_CONTAINER_slist_begin (rfds->handles);
1635 GNUNET_CONTAINER_slist_end (&i) != GNUNET_YES;
1636 GNUNET_CONTAINER_slist_next (&i), c++)
1638 struct GNUNET_DISK_FileHandle *fh;
1640 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (&i,NULL);
1641 if (fh->type == GNUNET_DISK_HANLDE_TYPE_PIPE)
1647 DWORD waitstatus = 0;
1648 bret = PeekNamedPipe (fh->h, NULL, 0, NULL, &waitstatus, NULL);
1649 error = GetLastError ();
1650 LOG (GNUNET_ERROR_TYPE_DEBUG, "Peek at read pipe %d (0x%x) returned %d (%d bytes available) GLE %u\n",
1651 c, fh->h, bret, waitstatus, error);
1654 /* TODO: either add more errors to this condition, or eliminate it
1655 * entirely (failed to peek -> pipe is in serious trouble, should
1656 * be selected as readable).
1658 if (error != ERROR_BROKEN_PIPE && error != ERROR_INVALID_HANDLE)
1661 else if (waitstatus <= 0)
1663 GNUNET_CONTAINER_slist_add (handles_read, GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1664 fh, sizeof (struct GNUNET_DISK_FileHandle));
1666 LOG (GNUNET_ERROR_TYPE_DEBUG, "Added read Pipe 0x%x (0x%x)\n",
1671 GNUNET_CONTAINER_slist_add (handles_read, GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1672 fh, sizeof (struct GNUNET_DISK_FileHandle));
1677 if (wfds && write_handles)
1679 LOG (GNUNET_ERROR_TYPE_DEBUG,
1680 "Adding the write ready event to the array as %d\n", nhandles);
1681 GNUNET_CONTAINER_slist_append (handles_write, wfds->handles);
1682 retcode += write_handles;
1684 if (efds && ex_handles)
1686 struct GNUNET_CONTAINER_SList_Iterator i;
1688 for (i = GNUNET_CONTAINER_slist_begin (efds->handles);
1689 GNUNET_CONTAINER_slist_end (&i) != GNUNET_YES;
1690 GNUNET_CONTAINER_slist_next (&i))
1692 struct GNUNET_DISK_FileHandle *fh;
1695 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (&i, NULL);
1696 if (fh->type == GNUNET_DISK_HANLDE_TYPE_PIPE)
1698 if (PeekNamedPipe (fh->h, NULL, 0, NULL, &dwBytes, NULL))
1700 GNUNET_CONTAINER_slist_add (handles_except, GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1701 fh, sizeof (struct GNUNET_DISK_FileHandle));
1707 /* Add our select() result.*/
1709 retcode += selectret;
1713 GNUNET_NETWORK_fdset_zero (rfds);
1714 if (selectret != -1)
1715 GNUNET_NETWORK_fdset_copy_native (rfds, &aread, selectret);
1716 GNUNET_CONTAINER_slist_append (rfds->handles, handles_read);
1720 GNUNET_NETWORK_fdset_zero (wfds);
1721 if (selectret != -1)
1722 GNUNET_NETWORK_fdset_copy_native (wfds, &awrite, selectret);
1723 GNUNET_CONTAINER_slist_append (wfds->handles, handles_write);
1727 GNUNET_NETWORK_fdset_zero (efds);
1728 if (selectret != -1)
1729 GNUNET_NETWORK_fdset_copy_native (efds, &aexcept, selectret);
1730 GNUNET_CONTAINER_slist_append (efds->handles, handles_except);
1732 GNUNET_CONTAINER_slist_destroy (handles_read);
1733 GNUNET_CONTAINER_slist_destroy (handles_write);
1734 GNUNET_CONTAINER_slist_destroy (handles_except);
1736 if (selectret == -1)
1741 /* If we got this far, use slower implementation that is able to do a waiting select
1742 on both sockets and pipes simultaneously */
1744 /* Events for pipes */
1745 if (!hEventReadReady)
1746 hEventReadReady = CreateEvent (NULL, TRUE, TRUE, NULL);
1747 if (!hEventPipeWrite)
1748 hEventPipeWrite = CreateEvent (NULL, TRUE, TRUE, NULL);
1764 FD_COPY (&rfds->sds, &aread);
1766 FD_COPY (&rfds->sds, &bread);
1771 FD_COPY (&wfds->sds, &awrite);
1773 FD_COPY (&wfds->sds, &bwrite);
1778 FD_COPY (&efds->sds, &aexcept);
1780 FD_COPY (&efds->sds, &bexcept);
1783 /* We will first Add the PIPES to the events */
1785 if (rfds && read_handles)
1787 struct GNUNET_CONTAINER_SList_Iterator i;
1789 for (i = GNUNET_CONTAINER_slist_begin (rfds->handles);
1790 GNUNET_CONTAINER_slist_end (&i) != GNUNET_YES;
1791 GNUNET_CONTAINER_slist_next (&i))
1793 struct GNUNET_DISK_FileHandle *fh;
1795 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (&i,
1797 if (fh->type == GNUNET_DISK_HANLDE_TYPE_PIPE)
1799 /* Read zero bytes to check the status of the pipe */
1800 LOG (GNUNET_ERROR_TYPE_DEBUG, "Reading 0 bytes from the pipe 0x%x\n",
1802 if (!ReadFile (fh->h, NULL, 0, NULL, fh->oOverlapRead))
1804 DWORD error_code = GetLastError ();
1806 if (error_code == ERROR_IO_PENDING)
1808 LOG (GNUNET_ERROR_TYPE_DEBUG,
1809 "Adding the pipe's 0x%x overlapped event to the array as %d\n",
1811 handle_array[nhandles++] = fh->oOverlapRead->hEvent;
1812 readArray[readPipes++] = fh;
1816 LOG (GNUNET_ERROR_TYPE_DEBUG,
1817 "Read failed, adding the read ready event to the array as %d\n", nhandles);
1818 handle_array[nhandles++] = hEventReadReady;
1819 readArray[readPipes++] = fh;
1824 LOG (GNUNET_ERROR_TYPE_DEBUG,
1825 "Adding the read ready event to the array as %d\n", nhandles);
1826 handle_array[nhandles++] = hEventReadReady;
1827 readArray[readPipes++] = fh;
1832 GNUNET_CONTAINER_slist_add (handles_read,
1833 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1834 fh, sizeof (struct GNUNET_DISK_FileHandle));
1838 if (wfds && write_handles)
1840 LOG (GNUNET_ERROR_TYPE_DEBUG,
1841 "Adding the write ready event to the array as %d\n", nhandles);
1842 handle_array[nhandles++] = hEventPipeWrite;
1843 writePipePos = nhandles;
1845 if (efds && ex_handles)
1847 struct GNUNET_CONTAINER_SList_Iterator i;
1849 for (i = GNUNET_CONTAINER_slist_begin (efds->handles);
1850 GNUNET_CONTAINER_slist_end (&i) != GNUNET_YES;
1851 GNUNET_CONTAINER_slist_next (&i))
1853 struct GNUNET_DISK_FileHandle *fh;
1856 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (&i,
1858 if (fh->type == GNUNET_DISK_HANLDE_TYPE_PIPE)
1860 if (!PeekNamedPipe (fh->h, NULL, 0, NULL, &dwBytes, NULL))
1862 GNUNET_CONTAINER_slist_add (handles_except,
1863 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1865 sizeof (struct GNUNET_DISK_FileHandle));
1875 LOG (GNUNET_ERROR_TYPE_DEBUG,
1876 "Adding the socket event to the array as %d\n",
1878 handle_array[nhandles++] = select_finished_event;
1879 if (timeout.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
1883 select_timeout.tv_sec = timeout.rel_value_us / GNUNET_TIME_UNIT_SECONDS.rel_value_us;
1884 select_timeout.tv_usec =(timeout.rel_value_us -
1885 (select_timeout.tv_sec * GNUNET_TIME_UNIT_SECONDS.rel_value_us));
1886 sp.tv = &select_timeout;
1888 FD_SET (select_wakeup_socket, &aread);
1891 i = recv (select_wakeup_socket, (char *) &returnedpos, 1, 0);
1896 /* Failed connections cause sockets to be set in errorfds on W32,
1897 * but on POSIX it should set them in writefds.
1898 * First copy all awrite sockets to aexcept, later we'll
1899 * check aexcept and set its contents in awrite as well
1900 * Sockets are also set in errorfds when OOB data is available,
1901 * but we don't use OOB data.
1903 for (i = 0; i < awrite.fd_count; i++)
1904 FD_SET (awrite.fd_array[i], &aexcept);
1905 ResetEvent (select_finished_event);
1906 SetEvent (select_standby_event);
1909 handle_array[nhandles] = NULL;
1910 LOG (GNUNET_ERROR_TYPE_DEBUG, "nfds: %d, handles: %d, will wait: %llu mcs\n",
1911 nfds, nhandles, mcs_total);
1915 WaitForMultipleObjects (nhandles, handle_array, FALSE, ms_rounded);
1916 LOG (GNUNET_ERROR_TYPE_DEBUG, "WaitForMultipleObjects Returned : %d\n", returncode);
1920 GNUNET_break (0); /* This branch shouldn't actually be executed...*/
1921 i = (int) WaitForSingleObject (select_finished_event, INFINITE);
1922 returncode = WAIT_TIMEOUT;
1926 /* Shouldn't come this far. If it does - investigate. */
1932 /* Don't wake up select-thread when delay is 0, it should return immediately
1933 * and wake up by itself.
1936 i = send (select_send_socket, (const char *) &returnedpos, 1, 0);
1937 i = (int) WaitForSingleObject (select_finished_event, INFINITE);
1938 LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished waiting for the select thread: %d %d\n", i, sp.status);
1943 i = recv (select_wakeup_socket, (char *) &returnedpos, 1, 0);
1946 /* Check aexcept, add its contents to awrite */
1947 for (i = 0; i < aexcept.fd_count; i++)
1948 FD_SET (aexcept.fd_array[i], &awrite);
1951 returnedpos = returncode - WAIT_OBJECT_0;
1952 LOG (GNUNET_ERROR_TYPE_DEBUG, "return pos is : %d\n", returnedpos);
1954 if (nhandles && (returnedpos < nhandles))
1959 retcode += sp.status;
1961 if ((writePipePos != -1) && (returnedpos < writePipePos))
1963 GNUNET_CONTAINER_slist_append (handles_write, wfds->handles);
1964 retcode += write_handles;
1965 LOG (GNUNET_ERROR_TYPE_DEBUG, "Added write pipe\n");
1967 LOG (GNUNET_ERROR_TYPE_DEBUG, "ReadPipes is : %d\n", readPipes);
1968 /* We have some pipes ready for read. */
1969 if (returnedpos < readPipes)
1971 for (i = 0; i < readPipes; i++)
1979 PeekNamedPipe (readArray[i]->h, NULL, 0, NULL, &waitstatus, NULL);
1980 error = GetLastError ();
1981 LOG (GNUNET_ERROR_TYPE_DEBUG,
1982 "Peek at read pipe %d (0x%x) returned %d (%d bytes available) GLE %u\n",
1983 i, readArray[i]->h, bret, waitstatus, error);
1986 /* TODO: either add more errors to this condition, or eliminate it
1987 * entirely (failed to peek -> pipe is in serious trouble, should
1988 * be selected as readable).
1990 if (error != ERROR_BROKEN_PIPE && error != ERROR_INVALID_HANDLE)
1993 else if (waitstatus <= 0)
1995 GNUNET_CONTAINER_slist_add (handles_read,
1996 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1998 sizeof (struct GNUNET_DISK_FileHandle));
2000 LOG (GNUNET_ERROR_TYPE_DEBUG, "Added read Pipe 0x%x (0x%x)\n",
2001 readArray[i], readArray[i]->h);
2005 if (!nhandles || (returnedpos >= nhandles))
2006 LOG (GNUNET_ERROR_TYPE_DEBUG, "Returning from _select() with nothing!\n");
2009 struct GNUNET_CONTAINER_SList_Iterator t;
2011 for (t = GNUNET_CONTAINER_slist_begin (rfds->handles);
2012 GNUNET_CONTAINER_slist_end (&t) != GNUNET_YES;
2013 GNUNET_CONTAINER_slist_next (&t))
2015 struct GNUNET_DISK_FileHandle *fh;
2017 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (&t,
2019 if (fh->type == GNUNET_DISK_HANLDE_TYPE_PIPE)
2024 LOG (GNUNET_ERROR_TYPE_DEBUG, "Zeroing rfds%s\n", (retcode != -1 && nhandles && (returnedpos < nhandles)) ? ", copying fdset" : "");
2025 GNUNET_NETWORK_fdset_zero (rfds);
2026 if (retcode != -1 && nhandles && (returnedpos < nhandles))
2027 GNUNET_NETWORK_fdset_copy_native (rfds, &aread, retcode);
2028 GNUNET_CONTAINER_slist_append (rfds->handles, handles_read);
2032 LOG (GNUNET_ERROR_TYPE_DEBUG, "Zeroing wfds%s\n", (retcode != -1 && nhandles && (returnedpos < nhandles)) ? ", copying fdset" : "");
2033 GNUNET_NETWORK_fdset_zero (wfds);
2034 if (retcode != -1 && nhandles && (returnedpos < nhandles))
2035 GNUNET_NETWORK_fdset_copy_native (wfds, &awrite, retcode);
2036 GNUNET_CONTAINER_slist_append (wfds->handles, handles_write);
2040 LOG (GNUNET_ERROR_TYPE_DEBUG, "Zeroing efds%s\n", (retcode != -1 && nhandles && (returnedpos < nhandles)) ? ", copying fdset" : "");
2041 GNUNET_NETWORK_fdset_zero (efds);
2042 if (retcode != -1 && nhandles && (returnedpos < nhandles))
2043 GNUNET_NETWORK_fdset_copy_native (efds, &aexcept, retcode);
2044 GNUNET_CONTAINER_slist_append (efds->handles, handles_except);
2046 GNUNET_CONTAINER_slist_destroy (handles_read);
2047 GNUNET_CONTAINER_slist_destroy (handles_write);
2048 GNUNET_CONTAINER_slist_destroy (handles_except);
2052 struct GNUNET_CONTAINER_SList_Iterator t;
2054 LOG (GNUNET_ERROR_TYPE_DEBUG, "rfds:\n");
2055 for (i = 0; i < rfds->sds.fd_count; i++)
2057 LOG (GNUNET_ERROR_TYPE_DEBUG, "%d\n", rfds->sds.fd_array[i]);
2059 for (t = GNUNET_CONTAINER_slist_begin (rfds->handles);
2060 GNUNET_CONTAINER_slist_end (&t) != GNUNET_YES;
2061 GNUNET_CONTAINER_slist_next (&t))
2063 struct GNUNET_DISK_FileHandle *fh;
2065 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (&t,
2067 LOG (GNUNET_ERROR_TYPE_DEBUG, "%d\n", fh->h);
2072 LOG (GNUNET_ERROR_TYPE_DEBUG, "wfds:\n");
2073 for (i = 0; i < wfds->sds.fd_count; i++)
2075 LOG (GNUNET_ERROR_TYPE_DEBUG, "%d\n", wfds->sds.fd_array[i]);
2080 LOG (GNUNET_ERROR_TYPE_DEBUG, "efds:\n");
2081 for (i = 0; i < efds->sds.fd_count; i++)
2083 LOG (GNUNET_ERROR_TYPE_DEBUG, "%d\n", efds->sds.fd_array[i]);
2086 LOG (GNUNET_ERROR_TYPE_DEBUG, "Returning %d or 0\n", retcode);
2088 if (nhandles && (returnedpos < nhandles))
2097 /* end of network.c */