2 This file is part of GNUnet.
3 Copyright (C) 2009-2013 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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-network", __VA_ARGS__)
32 #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util-network", syscall, filename)
33 #define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util-network", syscall)
35 #define DEBUG_NETWORK GNUNET_EXTRA_LOGGING
38 #ifndef INVALID_SOCKET
39 #define INVALID_SOCKET -1
44 * @brief handle to a socket
46 struct GNUNET_NETWORK_Handle
55 * Address family / domain.
65 * Number of bytes in addr.
70 * Address we were bound to, or NULL.
72 struct sockaddr *addr;
78 * Test if the given protocol family is supported by this system.
80 * @param pf protocol family to test (PF_INET, PF_INET6, PF_UNIX)
81 * @return #GNUNET_OK if the PF is supported
84 GNUNET_NETWORK_test_pf (int pf)
86 static int cache_v4 = -1;
87 static int cache_v6 = -1;
88 static int cache_un = -1;
109 s = socket (pf, SOCK_STREAM, 0);
112 if (EAFNOSUPPORT != errno)
114 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
116 return GNUNET_SYSERR;
148 * Given a unixpath that is too long (larger than UNIX_PATH_MAX),
149 * shorten it to an acceptable length while keeping it unique
150 * and making sure it remains a valid filename (if possible).
152 * @param unixpath long path, will be freed (or same pointer returned
153 * with moved 0-termination).
154 * @return shortened unixpath, NULL on error
157 GNUNET_NETWORK_shorten_unixpath (char *unixpath)
159 struct sockaddr_un dummy;
162 struct GNUNET_HashCode sh;
163 struct GNUNET_CRYPTO_HashAsciiEncoded ae;
166 upm = sizeof (dummy.sun_path);
167 slen = strlen (unixpath);
169 return unixpath; /* no shortening required */
170 GNUNET_CRYPTO_hash (unixpath, slen, &sh);
171 while (16 + strlen (unixpath) >= upm)
173 if (NULL == (end = strrchr (unixpath, '/')))
175 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
176 _("Unable to shorten unix path `%s' while keeping name unique\n"),
178 GNUNET_free (unixpath);
183 GNUNET_CRYPTO_hash_to_enc (&sh, &ae);
184 ae.encoding[16] = '\0';
185 strcat (unixpath, (char *) ae.encoding);
192 * If services crash, they can leave a unix domain socket file on the
193 * disk. This needs to be manually removed, because otherwise both
194 * bind() and connect() for the respective address will fail. In this
195 * function, we test if such a left-over file exists, and if so,
196 * remove it (unless there is a listening service at the address).
198 * @param un unix domain socket address to check
201 GNUNET_NETWORK_unix_precheck (const struct sockaddr_un *un)
208 s = socket (AF_UNIX, SOCK_STREAM, 0);
211 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
212 "Failed to open AF_UNIX socket");
216 (struct sockaddr *) un,
217 sizeof (struct sockaddr_un));
219 GNUNET_break (0 == close (s));
221 return; /* another process is listening, do not remove! */
222 if (ECONNREFUSED != eno)
223 return; /* some other error, likely "no such file or directory" -- all well */
224 /* should unlink, but sanity checks first */
225 if (0 != stat (un->sun_path,
227 return; /* failed to 'stat', likely does not exist after all */
228 if (S_IFSOCK != (S_IFMT & sbuf.st_mode))
229 return; /* refuse to unlink anything except sockets */
230 /* finally, really unlink */
231 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
232 "Removing left-over `%s' from previous exeuction\n",
234 if (0 != unlink (un->sun_path))
235 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
244 #define FD_COPY(s, d) do { GNUNET_memcpy ((d), (s), sizeof (fd_set)); } while (0)
249 * Set if a socket should use blocking or non-blocking IO.
252 * @param doBlock blocking mode
253 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
256 GNUNET_NETWORK_socket_set_blocking (struct GNUNET_NETWORK_Handle *fd,
270 SetErrnoFromWinsockError (WSAGetLastError ());
271 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
273 return GNUNET_SYSERR;
279 int flags = fcntl (fd->fd, F_GETFL);
283 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
285 return GNUNET_SYSERR;
288 flags &= ~O_NONBLOCK;
292 if (0 != fcntl (fd->fd,
297 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
299 return GNUNET_SYSERR;
307 * Make a socket non-inheritable to child processes
309 * @param h the socket to make non-inheritable
310 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
311 * @warning Not implemented on Windows
314 socket_set_inheritable (const struct GNUNET_NETWORK_Handle *h)
318 i = fcntl (h->fd, F_GETFD);
320 return GNUNET_SYSERR;
321 if (i == (i | FD_CLOEXEC))
324 if (fcntl (h->fd, F_SETFD, i) < 0)
325 return GNUNET_SYSERR;
329 b = SetHandleInformation ((HANDLE) h->fd, HANDLE_FLAG_INHERIT, 0);
332 SetErrnoFromWinsockError (WSAGetLastError ());
333 return GNUNET_SYSERR;
342 * The MSG_NOSIGNAL equivalent on Mac OS X
344 * @param h the socket to make non-delaying
347 socket_set_nosigpipe (const struct GNUNET_NETWORK_Handle *h)
352 setsockopt (h->fd, SOL_SOCKET, SO_NOSIGPIPE,
353 (const void *) &abs_value,
355 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
361 * Disable delays when sending data via the socket.
362 * (GNUnet makes sure that messages are as big as
365 * @param h the socket to make non-delaying
368 socket_set_nodelay (const struct GNUNET_NETWORK_Handle *h)
377 &value, sizeof (value)))
378 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
381 const char *abs_value = "1";
387 (const void *) abs_value,
389 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
396 * Perform proper canonical initialization for a network handle.
397 * Set it to non-blocking, make it non-inheritable to child
398 * processes, disable SIGPIPE, enable "nodelay" (if non-UNIX
399 * stream socket) and check that it is smaller than FD_SETSIZE.
401 * @param h socket to initialize
402 * @param af address family of the socket
403 * @param type socket type
404 * @return #GNUNET_OK on success, #GNUNET_SYSERR if initialization
405 * failed and the handle was destroyed
408 initialize_network_handle (struct GNUNET_NETWORK_Handle *h,
416 if (h->fd == INVALID_SOCKET)
419 SetErrnoFromWinsockError (WSAGetLastError ());
424 return GNUNET_SYSERR;
427 if (h->fd >= FD_SETSIZE)
429 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (h));
431 return GNUNET_SYSERR;
434 if (GNUNET_OK != socket_set_inheritable (h))
435 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
436 "socket_set_inheritable");
438 if (GNUNET_SYSERR == GNUNET_NETWORK_socket_set_blocking (h, GNUNET_NO))
442 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (h));
444 return GNUNET_SYSERR;
447 socket_set_nosigpipe (h);
449 if ( (type == SOCK_STREAM)
454 socket_set_nodelay (h);
460 * accept a new connection on a socket
462 * @param desc bound socket
463 * @param address address of the connecting peer, may be NULL
464 * @param address_len length of @a address
465 * @return client socket
467 struct GNUNET_NETWORK_Handle *
468 GNUNET_NETWORK_socket_accept (const struct GNUNET_NETWORK_Handle *desc,
469 struct sockaddr *address,
470 socklen_t *address_len)
472 struct GNUNET_NETWORK_Handle *ret;
475 ret = GNUNET_new (struct GNUNET_NETWORK_Handle);
478 struct sockaddr_storage name;
479 socklen_t namelen = sizeof (name);
481 int gsn = getsockname (desc->fd,
482 (struct sockaddr *) &name,
486 LOG (GNUNET_ERROR_TYPE_DEBUG,
487 "Accepting connection on `%s'\n",
488 GNUNET_a2s ((const struct sockaddr *) &name,
492 ret->fd = accept (desc->fd,
503 initialize_network_handle (ret,
504 (NULL != address) ? address->sa_family : desc->af,
514 * Bind a socket to a particular address.
516 * @param desc socket to bind
517 * @param address address to be bound
518 * @param address_len length of @a address
519 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
522 GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc,
523 const struct sockaddr *address,
524 socklen_t address_len)
533 if (AF_INET6 == desc->af)
534 if (setsockopt (desc->fd,
539 LOG_STRERROR (GNUNET_ERROR_TYPE_DEBUG,
545 if (AF_UNIX == address->sa_family)
546 GNUNET_NETWORK_unix_precheck ((const struct sockaddr_un *) address);
550 /* This is required here for TCP sockets, but only on UNIX */
551 if ( (SOCK_STREAM == desc->type) &&
552 (0 != setsockopt (desc->fd,
556 LOG_STRERROR (GNUNET_ERROR_TYPE_DEBUG,
560 /* set permissions of newly created non-abstract UNIX domain socket to
561 "user-only"; applications can choose to relax this later */
562 mode_t old_mask = 0; /* assigned to make compiler happy */
563 const struct sockaddr_un *un = (const struct sockaddr_un *) address;
564 int not_abstract = 0;
566 if ((AF_UNIX == address->sa_family)
567 && ('\0' != un->sun_path[0]) ) /* Not an abstract socket */
570 old_mask = umask (S_IWGRP | S_IRGRP | S_IXGRP | S_IWOTH | S_IROTH | S_IXOTH);
573 ret = bind (desc->fd,
579 (void) umask (old_mask);
583 if (SOCKET_ERROR == ret)
584 SetErrnoFromWinsockError (WSAGetLastError ());
587 return GNUNET_SYSERR;
589 desc->addr = GNUNET_malloc (address_len);
590 GNUNET_memcpy (desc->addr, address, address_len);
591 desc->addrlen = address_len;
601 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
604 GNUNET_NETWORK_socket_close (struct GNUNET_NETWORK_Handle *desc)
612 ret = closesocket (desc->fd);
613 error = WSAGetLastError ();
614 SetErrnoFromWinsockError (error);
615 LOG (GNUNET_ERROR_TYPE_DEBUG,
616 "Closed 0x%x, closesocket() returned %d, GLE is %u\n",
621 ret = close (desc->fd);
624 const struct sockaddr_un *un = (const struct sockaddr_un *) desc->addr;
626 /* Cleanup the UNIX domain socket and its parent directories in case of non
628 if ( (AF_UNIX == desc->af) &&
629 (NULL != desc->addr) &&
630 ('\0' != un->sun_path[0]) )
632 char *dirname = GNUNET_strndup (un->sun_path,
633 sizeof (un->sun_path));
635 if (0 != unlink (dirname))
637 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING,
645 len = strlen (dirname);
646 while ((len > 0) && (dirname[len] != DIR_SEPARATOR))
649 if ((0 != len) && (0 != rmdir (dirname)))
656 /* these are normal and can just be ignored */
659 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
666 GNUNET_free (dirname);
669 GNUNET_NETWORK_socket_free_memory_only_ (desc);
670 return (ret == 0) ? GNUNET_OK : GNUNET_SYSERR;
675 * Only free memory of a socket, keep the file descriptor untouched.
680 GNUNET_NETWORK_socket_free_memory_only_ (struct GNUNET_NETWORK_Handle *desc)
682 GNUNET_free_non_null (desc->addr);
688 * Box a native socket (and check that it is a socket).
690 * @param fd socket to box
691 * @return NULL on error (including not supported on target platform)
693 struct GNUNET_NETWORK_Handle *
694 GNUNET_NETWORK_socket_box_native (SOCKTYPE fd)
696 struct GNUNET_NETWORK_Handle *ret;
700 /* FIXME: Find a better call to check that FD is valid */
702 WSAIoctl (fd, FIONBIO,
703 (void *) &i, sizeof (i),
706 return NULL; /* invalid FD */
707 ret = GNUNET_new (struct GNUNET_NETWORK_Handle);
712 if (fcntl (fd, F_GETFD) < 0)
713 return NULL; /* invalid FD */
714 ret = GNUNET_new (struct GNUNET_NETWORK_Handle);
723 * Connect a socket to some remote address.
726 * @param address peer address
727 * @param address_len length of @a address
728 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
731 GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Handle *desc,
732 const struct sockaddr *address,
733 socklen_t address_len)
737 ret = connect (desc->fd,
741 if (SOCKET_ERROR == ret)
743 SetErrnoFromWinsockError (WSAGetLastError ());
744 if (errno == EWOULDBLOCK)
748 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
756 * @param level protocol level of the option
757 * @param optname identifier of the option
758 * @param optval options
759 * @param optlen length of @a optval
760 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
763 GNUNET_NETWORK_socket_getsockopt (const struct GNUNET_NETWORK_Handle *desc,
771 ret = getsockopt (desc->fd,
778 (SOL_SOCKET == level) &&
779 (SO_ERROR == optname) )
780 *((int *) optval) = GetErrnoFromWinsockError (*((int *) optval));
781 else if (SOCKET_ERROR == ret)
782 SetErrnoFromWinsockError (WSAGetLastError ());
784 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
792 * @param backlog length of the listen queue
793 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
796 GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Handle *desc,
801 ret = listen (desc->fd,
804 if (SOCKET_ERROR == ret)
805 SetErrnoFromWinsockError (WSAGetLastError ());
807 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
812 * How much data is available to be read on this descriptor?
815 * @returns #GNUNET_SYSERR if no data is available, or on error!
818 GNUNET_NETWORK_socket_recvfrom_amount (const struct GNUNET_NETWORK_Handle *desc)
822 /* How much is there to be read? */
826 error = ioctl (desc->fd,
830 return (ssize_t) pending;
831 return GNUNET_SYSERR;
835 error = ioctlsocket (desc->fd,
838 if (error != SOCKET_ERROR)
839 return (ssize_t) pending;
840 return GNUNET_SYSERR;
846 * Read data from a socket (always non-blocking).
849 * @param buffer buffer
850 * @param length length of @a buffer
851 * @param src_addr either the source to recv from, or all zeroes
852 * to be filled in by recvfrom
853 * @param addrlen length of the @a src_addr
856 GNUNET_NETWORK_socket_recvfrom (const struct GNUNET_NETWORK_Handle *desc,
859 struct sockaddr *src_addr,
868 flags |= MSG_DONTWAIT;
871 ret = recvfrom (desc->fd,
878 if (SOCKET_ERROR == ret)
879 SetErrnoFromWinsockError (WSAGetLastError ());
886 * Read data from a connected socket (always non-blocking).
889 * @param buffer buffer
890 * @param length length of @a buffer
891 * @return number of bytes received, -1 on error
894 GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Handle *desc,
904 flags |= MSG_DONTWAIT;
906 ret = recv (desc->fd,
911 if (SOCKET_ERROR == ret)
912 SetErrnoFromWinsockError (WSAGetLastError ());
919 * Send data (always non-blocking).
922 * @param buffer data to send
923 * @param length size of the @a buffer
924 * @return number of bytes sent, #GNUNET_SYSERR on error
927 GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Handle *desc,
936 flags |= MSG_DONTWAIT;
940 flags |= MSG_NOSIGNAL;
943 ret = send (desc->fd,
948 if (SOCKET_ERROR == ret)
949 SetErrnoFromWinsockError (WSAGetLastError ());
957 * Send data to a particular destination (always non-blocking).
958 * This function only works for UDP sockets.
961 * @param message data to send
962 * @param length size of the @a message
963 * @param dest_addr destination address
964 * @param dest_len length of @a address
965 * @return number of bytes sent, #GNUNET_SYSERR on error
968 GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Handle *desc,
971 const struct sockaddr *dest_addr,
980 flags |= MSG_DONTWAIT;
983 flags |= MSG_NOSIGNAL;
985 ret = sendto (desc->fd, message, length, flags, dest_addr, dest_len);
987 if (SOCKET_ERROR == ret)
988 SetErrnoFromWinsockError (WSAGetLastError ());
998 * @param level protocol level of the option
999 * @param option_name option identifier
1000 * @param option_value value to set
1001 * @param option_len size of @a option_value
1002 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
1005 GNUNET_NETWORK_socket_setsockopt (struct GNUNET_NETWORK_Handle *fd,
1008 const void *option_value,
1009 socklen_t option_len)
1013 ret = setsockopt (fd->fd,
1019 if (SOCKET_ERROR == ret)
1020 SetErrnoFromWinsockError (WSAGetLastError ());
1022 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
1027 * Create a new socket. Configure it for non-blocking IO and
1028 * mark it as non-inheritable to child processes (set the
1029 * close-on-exec flag).
1031 * @param domain domain of the socket
1032 * @param type socket type
1033 * @param protocol network protocol
1034 * @return new socket, NULL on error
1036 struct GNUNET_NETWORK_Handle *
1037 GNUNET_NETWORK_socket_create (int domain,
1041 struct GNUNET_NETWORK_Handle *ret;
1044 fd = socket (domain, type, protocol);
1047 ret = GNUNET_new (struct GNUNET_NETWORK_Handle);
1050 initialize_network_handle (ret,
1059 * Shut down socket operations
1060 * @param desc socket
1061 * @param how type of shutdown
1062 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
1065 GNUNET_NETWORK_socket_shutdown (struct GNUNET_NETWORK_Handle *desc,
1070 ret = shutdown (desc->fd, how);
1073 SetErrnoFromWinsockError (WSAGetLastError ());
1075 return (0 == ret) ? GNUNET_OK : GNUNET_SYSERR;
1080 * Disable the "CORK" feature for communication with the given socket,
1081 * forcing the OS to immediately flush the buffer on transmission
1082 * instead of potentially buffering multiple messages. Essentially
1083 * reduces the OS send buffers to zero.
1085 * @param desc socket
1086 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
1089 GNUNET_NETWORK_socket_disable_corking (struct GNUNET_NETWORK_Handle *desc)
1098 setsockopt (desc->fd,
1103 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
1107 setsockopt (desc->fd,
1112 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
1119 setsockopt (desc->fd,
1124 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
1128 setsockopt (desc->fd,
1133 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
1136 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
1146 GNUNET_NETWORK_fdset_zero (struct GNUNET_NETWORK_FDSet *fds)
1148 FD_ZERO (&fds->sds);
1151 fds->handles_pos = 0;
1157 * Add a socket to the FD set
1160 * @param desc socket to add
1163 GNUNET_NETWORK_fdset_set (struct GNUNET_NETWORK_FDSet *fds,
1164 const struct GNUNET_NETWORK_Handle *desc)
1168 fds->nsds = GNUNET_MAX (fds->nsds,
1174 * Check whether a socket is part of the fd set
1177 * @param desc socket
1178 * @return 0 if the FD is not set
1181 GNUNET_NETWORK_fdset_isset (const struct GNUNET_NETWORK_FDSet *fds,
1182 const struct GNUNET_NETWORK_Handle *desc)
1184 return FD_ISSET (desc->fd,
1190 * Add one fd set to another
1192 * @param dst the fd set to add to
1193 * @param src the fd set to add from
1196 GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst,
1197 const struct GNUNET_NETWORK_FDSet *src)
1202 for (nfds = src->nsds; nfds >= 0; nfds--)
1203 if (FD_ISSET (nfds, &src->sds))
1204 FD_SET (nfds, &dst->sds);
1205 dst->nsds = GNUNET_MAX (dst->nsds,
1208 /* This is MinGW32-specific implementation that relies on the code that
1209 * winsock2.h defines for FD_SET. Namely, it relies on FD_SET checking
1210 * that fd being added is not already in the set.
1211 * Also relies on us knowing what's inside fd_set (fd_count and fd_array).
1213 * NOTE: I don't understand why the UNIX-logic wouldn't work
1214 * for the first part here as well. -CG
1218 for (i = 0; i < src->sds.fd_count; i++)
1219 FD_SET (src->sds.fd_array[i],
1221 dst->nsds = GNUNET_MAX (src->nsds,
1224 /* also copy over `struct GNUNET_DISK_FileHandle` array */
1225 if (dst->handles_pos + src->handles_pos > dst->handles_size)
1226 GNUNET_array_grow (dst->handles,
1228 ((dst->handles_pos + src->handles_pos) << 1));
1229 for (i = 0; i < src->handles_pos; i++)
1230 dst->handles[dst->handles_pos++] = src->handles[i];
1236 * Copy one fd set to another
1238 * @param to destination
1239 * @param from source
1242 GNUNET_NETWORK_fdset_copy (struct GNUNET_NETWORK_FDSet *to,
1243 const struct GNUNET_NETWORK_FDSet *from)
1245 FD_COPY (&from->sds,
1247 to->nsds = from->nsds;
1249 if (from->handles_pos > to->handles_size)
1250 GNUNET_array_grow (to->handles,
1252 from->handles_pos * 2);
1253 GNUNET_memcpy (to->handles,
1255 from->handles_pos * sizeof (struct GNUNET_NETWORK_Handle *));
1256 to->handles_pos = from->handles_pos;
1262 * Return file descriptor for this network handle
1264 * @param desc wrapper to process
1265 * @return POSIX file descriptor
1268 GNUNET_NETWORK_get_fd (const struct GNUNET_NETWORK_Handle *desc)
1275 * Return sockaddr for this network handle
1277 * @param desc wrapper to process
1281 GNUNET_NETWORK_get_addr (const struct GNUNET_NETWORK_Handle *desc)
1288 * Return sockaddr length for this network handle
1290 * @param desc wrapper to process
1291 * @return socklen_t for sockaddr
1294 GNUNET_NETWORK_get_addrlen (const struct GNUNET_NETWORK_Handle *desc)
1296 return desc->addrlen;
1301 * Copy a native fd set
1303 * @param to destination
1304 * @param from native source set
1305 * @param nfds the biggest socket number in from + 1
1308 GNUNET_NETWORK_fdset_copy_native (struct GNUNET_NETWORK_FDSet *to,
1319 * Set a native fd in a set
1321 * @param to destination
1322 * @param nfd native FD to set
1325 GNUNET_NETWORK_fdset_set_native (struct GNUNET_NETWORK_FDSet *to,
1328 GNUNET_assert ((nfd >= 0) && (nfd < FD_SETSIZE));
1329 FD_SET (nfd, &to->sds);
1330 to->nsds = GNUNET_MAX (nfd + 1,
1336 * Test native fd in a set
1338 * @param to set to test, NULL for empty set
1339 * @param nfd native FD to test, or -1 for none
1340 * @return #GNUNET_YES if FD is set in the set
1343 GNUNET_NETWORK_fdset_test_native (const struct GNUNET_NETWORK_FDSet *to,
1349 return FD_ISSET (nfd, &to->sds) ? GNUNET_YES : GNUNET_NO;
1354 * Add a file handle to the fd set
1356 * @param h the file handle to add
1359 GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds,
1360 const struct GNUNET_DISK_FileHandle *h)
1363 if (fds->handles_pos == fds->handles_size)
1364 GNUNET_array_grow (fds->handles,
1366 fds->handles_size * 2 + 2);
1367 fds->handles[fds->handles_pos++] = h;
1371 GNUNET_assert (GNUNET_OK ==
1372 GNUNET_DISK_internal_file_handle_ (h,
1377 fds->nsds = GNUNET_MAX (fd + 1,
1384 * Add a file handle to the fd set
1386 * @param h the file handle to add
1389 GNUNET_NETWORK_fdset_handle_set_first (struct GNUNET_NETWORK_FDSet *fds,
1390 const struct GNUNET_DISK_FileHandle *h)
1393 if (fds->handles_pos == fds->handles_size)
1394 GNUNET_array_grow (fds->handles,
1396 fds->handles_size * 2 + 2);
1397 fds->handles[fds->handles_pos] = h;
1398 if (fds->handles[0] != h)
1400 const struct GNUNET_DISK_FileHandle *bak = fds->handles[0];
1401 fds->handles[0] = h;
1402 fds->handles[fds->handles_pos] = bak;
1406 GNUNET_NETWORK_fdset_handle_set (fds, h);
1412 * Check if a file handle is part of an fd set
1415 * @param h file handle
1416 * @return #GNUNET_YES if the file handle is part of the set
1419 GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds,
1420 const struct GNUNET_DISK_FileHandle *h)
1425 for (i=0;i<fds->handles_pos;i++)
1426 if (fds->handles[i] == h)
1430 return FD_ISSET (h->fd,
1438 * Numerically compare pointers to sort them.
1439 * Used to test for overlap in the arrays.
1441 * @param p1 a pointer
1442 * @param p2 a pointer
1443 * @return -1, 0 or 1, if the p1 < p2, p1==p2 or p1 > p2.
1446 ptr_cmp (const void *p1,
1451 if ((intptr_t) p1 < (intptr_t) p2)
1459 * Checks if two fd sets overlap
1461 * @param fds1 first fd set
1462 * @param fds2 second fd set
1463 * @return #GNUNET_YES if they do overlap, #GNUNET_NO otherwise
1466 GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1,
1467 const struct GNUNET_NETWORK_FDSet *fds2)
1472 nfds = GNUNET_MIN (fds1->nsds,
1477 if ( (FD_ISSET (nfds,
1488 /* This code is somewhat hacky, we are not supposed to know what's
1489 * inside of fd_set; also the O(n^2) is really bad... */
1490 for (i = 0; i < fds1->sds.fd_count; i++)
1491 for (j = 0; j < fds2->sds.fd_count; j++)
1492 if (fds1->sds.fd_array[i] == fds2->sds.fd_array[j])
1495 /* take a short cut if possible */
1496 if ( (0 == fds1->handles_pos) ||
1497 (0 == fds2->handles_pos) )
1500 /* Sort file handles array to avoid quadratic complexity when
1501 checking for overlap */
1502 qsort (fds1->handles,
1506 qsort (fds2->handles,
1512 while ( (i < fds1->handles_pos) &&
1513 (j < fds2->handles_pos) )
1515 switch (ptr_cmp (fds1->handles[i],
1535 * @return a new fd set
1537 struct GNUNET_NETWORK_FDSet *
1538 GNUNET_NETWORK_fdset_create ()
1540 struct GNUNET_NETWORK_FDSet *fds;
1542 fds = GNUNET_new (struct GNUNET_NETWORK_FDSet);
1543 GNUNET_NETWORK_fdset_zero (fds);
1549 * Releases the associated memory of an fd set
1554 GNUNET_NETWORK_fdset_destroy (struct GNUNET_NETWORK_FDSet *fds)
1557 GNUNET_array_grow (fds->handles,
1569 struct _select_params
1587 * Timeout for select().
1604 SOCKET wakeup_socket;
1607 * Set to return value from select.
1617 _selector (LPVOID p)
1619 struct _select_params *sp = p;
1623 WaitForSingleObject (sp->standby,
1625 ResetEvent (sp->standby);
1626 sp->status = select (1,
1631 if (FD_ISSET (sp->wakeup_socket,
1634 FD_CLR (sp->wakeup_socket,
1638 SetEvent (sp->wakeup);
1644 static HANDLE hEventPipeWrite;
1646 static HANDLE hEventReadReady;
1648 static struct _select_params sp;
1650 static HANDLE select_thread;
1652 static HANDLE select_finished_event;
1654 static HANDLE select_standby_event;
1656 static SOCKET select_wakeup_socket = -1;
1658 static SOCKET select_send_socket = -1;
1660 static struct timeval select_timeout;
1664 * On W32, we actually use a thread to help with the
1665 * event loop due to W32-API limitations. This function
1666 * initializes that thread.
1669 initialize_select_thread ()
1671 SOCKET select_listening_socket = -1;
1672 struct sockaddr_in s_in;
1677 select_standby_event = CreateEvent (NULL, TRUE, FALSE, NULL);
1678 select_finished_event = CreateEvent (NULL, TRUE, FALSE, NULL);
1680 select_wakeup_socket = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
1682 select_listening_socket = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
1685 res = ioctlsocket (select_wakeup_socket, FIONBIO, &p);
1686 LOG (GNUNET_ERROR_TYPE_DEBUG,
1687 "Select thread initialization: ioctlsocket() returns %d\n",
1690 alen = sizeof (s_in);
1691 s_in.sin_family = AF_INET;
1693 s_in.sin_addr.S_un.S_un_b.s_b1 = 127;
1694 s_in.sin_addr.S_un.S_un_b.s_b2 = 0;
1695 s_in.sin_addr.S_un.S_un_b.s_b3 = 0;
1696 s_in.sin_addr.S_un.S_un_b.s_b4 = 1;
1697 res = bind (select_listening_socket,
1698 (const struct sockaddr *) &s_in,
1700 LOG (GNUNET_ERROR_TYPE_DEBUG,
1701 "Select thread initialization: bind() returns %d\n",
1704 res = getsockname (select_listening_socket,
1705 (struct sockaddr *) &s_in,
1707 LOG (GNUNET_ERROR_TYPE_DEBUG,
1708 "Select thread initialization: getsockname() returns %d\n",
1711 res = listen (select_listening_socket,
1713 LOG (GNUNET_ERROR_TYPE_DEBUG,
1714 "Select thread initialization: listen() returns %d\n",
1716 res = connect (select_wakeup_socket,
1717 (const struct sockaddr *) &s_in,
1719 LOG (GNUNET_ERROR_TYPE_DEBUG,
1720 "Select thread initialization: connect() returns %d\n",
1723 select_send_socket = accept (select_listening_socket,
1724 (struct sockaddr *) &s_in,
1727 closesocket (select_listening_socket);
1729 sp.wakeup = select_finished_event;
1730 sp.standby = select_standby_event;
1731 sp.wakeup_socket = select_wakeup_socket;
1733 select_thread = CreateThread (NULL,
1744 * Test if the given @a port is available.
1746 * @param ipproto transport protocol to test (i.e. IPPROTO_TCP)
1747 * @param port port number to test
1748 * @return #GNUNET_OK if the port is available, #GNUNET_NO if not
1751 GNUNET_NETWORK_test_port_free (int ipproto,
1754 struct GNUNET_NETWORK_Handle *socket;
1757 char open_port_str[6];
1758 struct addrinfo hint;
1759 struct addrinfo *ret;
1760 struct addrinfo *ai;
1762 GNUNET_snprintf (open_port_str,
1763 sizeof (open_port_str),
1765 (unsigned int) port);
1766 socktype = (IPPROTO_TCP == ipproto) ? SOCK_STREAM : SOCK_DGRAM;
1768 memset (&hint, 0, sizeof (hint));
1769 hint.ai_family = AF_UNSPEC; /* IPv4 and IPv6 */
1770 hint.ai_socktype = socktype;
1771 hint.ai_protocol = ipproto;
1772 hint.ai_addrlen = 0;
1773 hint.ai_addr = NULL;
1774 hint.ai_canonname = NULL;
1775 hint.ai_next = NULL;
1776 hint.ai_flags = AI_PASSIVE | AI_NUMERICSERV; /* Wild card address */
1777 GNUNET_assert (0 == getaddrinfo (NULL,
1781 bind_status = GNUNET_NO;
1782 for (ai = ret; NULL != ai; ai = ai->ai_next)
1784 socket = GNUNET_NETWORK_socket_create (ai->ai_family,
1789 bind_status = GNUNET_NETWORK_socket_bind (socket,
1792 GNUNET_NETWORK_socket_close (socket);
1793 if (GNUNET_OK != bind_status)
1803 * Check if sockets or pipes meet certain conditions
1805 * @param rfds set of sockets or pipes to be checked for readability
1806 * @param wfds set of sockets or pipes to be checked for writability
1807 * @param efds set of sockets or pipes to be checked for exceptions
1808 * @param timeout relative value when to return
1809 * @return number of selected sockets or pipes, #GNUNET_SYSERR on error
1812 GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
1813 struct GNUNET_NETWORK_FDSet *wfds,
1814 struct GNUNET_NETWORK_FDSet *efds,
1815 const struct GNUNET_TIME_Relative timeout)
1825 nfds = GNUNET_MAX (nfds,
1828 nfds = GNUNET_MAX (nfds,
1831 (timeout.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us))
1834 LOG (GNUNET_ERROR_TYPE_ERROR,
1835 _("Fatal internal logic error, process hangs in `%s' (abort with CTRL-C)!\n"),
1838 if (timeout.rel_value_us / GNUNET_TIME_UNIT_SECONDS.rel_value_us > (unsigned long long) LONG_MAX)
1840 tv.tv_sec = LONG_MAX;
1841 tv.tv_usec = 999999L;
1845 tv.tv_sec = (long) (timeout.rel_value_us / GNUNET_TIME_UNIT_SECONDS.rel_value_us);
1847 (timeout.rel_value_us -
1848 (tv.tv_sec * GNUNET_TIME_UNIT_SECONDS.rel_value_us));
1850 return select (nfds,
1851 (NULL != rfds) ? &rfds->sds : NULL,
1852 (NULL != wfds) ? &wfds->sds : NULL,
1853 (NULL != efds) ? &efds->sds : NULL,
1854 (timeout.rel_value_us ==
1855 GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us) ? NULL : &tv);
1864 * Non-blocking test if a pipe is ready for reading.
1866 * @param fh pipe handle
1867 * @return #GNUNET_YES if the pipe is ready for reading
1870 pipe_read_ready (const struct GNUNET_DISK_FileHandle *fh)
1874 DWORD waitstatus = 0;
1877 bret = PeekNamedPipe (fh->h, NULL, 0, NULL, &waitstatus, NULL);
1878 error = GetLastError ();
1881 /* TODO: either add more errors to this condition, or eliminate it
1882 * entirely (failed to peek -> pipe is in serious trouble, should
1883 * be selected as readable).
1885 if ( (error != ERROR_BROKEN_PIPE) &&
1886 (error != ERROR_INVALID_HANDLE) )
1889 else if (waitstatus <= 0)
1896 * Non-blocking test if a pipe is having an IO exception.
1898 * @param fh pipe handle
1899 * @return #GNUNET_YES if the pipe is having an IO exception.
1902 pipe_except_ready (const struct GNUNET_DISK_FileHandle *fh)
1906 if (PeekNamedPipe (fh->h, NULL, 0, NULL, &dwBytes, NULL))
1913 * Iterate over handles in fds, destructively rewrite the
1914 * handles array contents of fds so that it starts with the
1915 * handles that are ready, and update handles_pos accordingly.
1917 * @param fds set of handles (usually pipes) to be checked for readiness
1918 * @param except GNUNET_NO if fds should be checked for readiness to read,
1919 * GNUNET_YES if fds should be checked for exceptions
1920 * (there is no way to check for write-readiness - pipes are always write-ready)
1921 * @param set_for_sure a HANDLE that is known to be set already,
1922 * because WaitForMultipleObjects() returned its index.
1923 * @return number of ready handles
1926 check_handles_status (struct GNUNET_NETWORK_FDSet *fds,
1928 HANDLE set_for_sure)
1930 const struct GNUNET_DISK_FileHandle *fh;
1934 for (woff = 0, roff = 0; roff < fds->handles_pos; roff++)
1936 fh = fds->handles[roff];
1937 if (fh == set_for_sure)
1939 fds->handles[woff++] = fh;
1941 else if (fh->type == GNUNET_DISK_HANLDE_TYPE_PIPE)
1943 if ((except && pipe_except_ready (fh)) ||
1944 (!except && pipe_read_ready (fh)))
1945 fds->handles[woff++] = fh;
1947 else if (fh->type == GNUNET_DISK_HANLDE_TYPE_FILE)
1950 fds->handles[woff++] = fh;
1954 if (WAIT_OBJECT_0 == WaitForSingleObject (fh->h, 0))
1955 fds->handles[woff++] = fh;
1958 fds->handles_pos = woff;
1964 * Check if sockets or pipes meet certain conditions, version for W32.
1966 * @param rfds set of sockets or pipes to be checked for readability
1967 * @param wfds set of sockets or pipes to be checked for writability
1968 * @param efds set of sockets or pipes to be checked for exceptions
1969 * @param timeout relative value when to return
1970 * @return number of selected sockets or pipes, #GNUNET_SYSERR on error
1973 GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
1974 struct GNUNET_NETWORK_FDSet *wfds,
1975 struct GNUNET_NETWORK_FDSet *efds,
1976 const struct GNUNET_TIME_Relative timeout)
1978 const struct GNUNET_DISK_FileHandle *fh;
1987 HANDLE handle_array[FD_SETSIZE + 2];
1989 int returnedpos = 0;
1999 nfds = GNUNET_MAX (nfds, rfds->nsds);
2000 handles += rfds->handles_pos;
2004 nfds = GNUNET_MAX (nfds, wfds->nsds);
2005 handles += wfds->handles_pos;
2009 nfds = GNUNET_MAX (nfds, efds->nsds);
2010 handles += efds->handles_pos;
2014 (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == timeout.rel_value_us) &&
2018 LOG (GNUNET_ERROR_TYPE_ERROR,
2019 _("Fatal internal logic error, process hangs in `%s' (abort with CTRL-C)!\n"),
2022 #define SAFE_FD_ISSET(fd, set) (set != NULL && FD_ISSET(fd, set))
2023 /* calculate how long we need to wait in microseconds */
2024 if (timeout.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
2026 mcs_total = INFINITE;
2027 ms_rounded = INFINITE;
2031 mcs_total = timeout.rel_value_us / GNUNET_TIME_UNIT_MICROSECONDS.rel_value_us;
2032 ms_rounded = (DWORD) (mcs_total / GNUNET_TIME_UNIT_MILLISECONDS.rel_value_us);
2033 if (mcs_total > 0 && ms_rounded == 0)
2036 /* select() may be used as a portable way to sleep */
2037 if (! (rfds || wfds || efds))
2043 if (NULL == select_thread)
2044 initialize_select_thread ();
2050 FD_COPY (&rfds->sds, &aread);
2052 FD_COPY (&wfds->sds, &awrite);
2054 FD_COPY (&efds->sds, &aexcept);
2056 /* Start by doing a fast check on sockets and pipes (without
2057 waiting). It is cheap, and is sufficient most of the time. By
2058 profiling we detected that to be true in 90% of the cases.
2061 /* Do the select now */
2062 select_timeout.tv_sec = 0;
2063 select_timeout.tv_usec = 0;
2065 /* Copy all the writes to the except, so we can detect connect() errors */
2066 for (i = 0; i < awrite.fd_count; i++)
2067 FD_SET (awrite.fd_array[i],
2069 if ( (aread.fd_count > 0) ||
2070 (awrite.fd_count > 0) ||
2071 (aexcept.fd_count > 0) )
2072 selectret = select (1,
2073 (NULL != rfds) ? &aread : NULL,
2074 (NULL != wfds) ? &awrite : NULL,
2079 if (-1 == selectret)
2081 /* Throw an error early on, while we still have the context. */
2082 LOG (GNUNET_ERROR_TYPE_ERROR,
2083 "W32 select(%d, %d, %d) failed: %lu\n",
2084 rfds ? aread.fd_count : 0,
2085 wfds ? awrite.fd_count : 0,
2091 /* Check aexcept, if something is in there and we copied that
2092 FD before to detect connect() errors, add it back to the
2093 write set to report errors. */
2095 for (i = 0; i < aexcept.fd_count; i++)
2096 if (FD_ISSET (aexcept.fd_array[i],
2098 FD_SET (aexcept.fd_array[i],
2102 /* If our select returned something or is a 0-timed request, then
2103 also check the pipes and get out of here! */
2104 /* Sadly, it means code duplication :( */
2105 if ( (selectret > 0) || (0 == mcs_total) )
2110 if (rfds && (rfds->handles_pos > 0))
2111 retcode += check_handles_status (rfds, GNUNET_NO, NULL);
2113 /* wfds handles remain untouched, on W32
2114 we pretend our pipes are "always" write-ready */
2117 if (efds && (efds->handles_pos > 0))
2118 retcode += check_handles_status (efds, GNUNET_YES, NULL);
2122 GNUNET_NETWORK_fdset_zero (rfds);
2123 if (selectret != -1)
2124 GNUNET_NETWORK_fdset_copy_native (rfds, &aread, selectret);
2128 GNUNET_NETWORK_fdset_zero (wfds);
2129 if (selectret != -1)
2130 GNUNET_NETWORK_fdset_copy_native (wfds, &awrite, selectret);
2134 GNUNET_NETWORK_fdset_zero (efds);
2135 if (selectret != -1)
2136 GNUNET_NETWORK_fdset_copy_native (efds, &aexcept, selectret);
2138 if (-1 == selectret)
2140 /* Add our select() FDs to the total return value */
2141 retcode += selectret;
2145 /* If we got this far, use slower implementation that is able to do a waiting select
2146 on both sockets and pipes simultaneously */
2148 /* Events for pipes */
2149 if (! hEventReadReady)
2150 hEventReadReady = CreateEvent (NULL, TRUE, TRUE, NULL);
2151 if (! hEventPipeWrite)
2152 hEventPipeWrite = CreateEvent (NULL, TRUE, TRUE, NULL);
2159 FD_COPY (&rfds->sds, &aread);
2161 FD_COPY (&wfds->sds, &awrite);
2163 FD_COPY (&efds->sds, &aexcept);
2164 /* We will first Add the PIPES to the events */
2165 /* Track how far in `handle_array` the read pipes go,
2166 so we may by-pass them quickly if none of them
2169 if (rfds && (rfds->handles_pos > 0))
2171 for (i = 0; i <rfds->handles_pos; i++)
2173 fh = rfds->handles[i];
2174 if (fh->type == GNUNET_DISK_HANLDE_TYPE_EVENT)
2176 handle_array[nhandles++] = fh->h;
2179 if (fh->type != GNUNET_DISK_HANLDE_TYPE_PIPE)
2181 /* Read zero bytes to check the status of the pipe */
2182 if (! ReadFile (fh->h, NULL, 0, NULL, fh->oOverlapRead))
2184 DWORD error_code = GetLastError ();
2186 if (error_code == ERROR_IO_PENDING)
2188 /* add as unready */
2189 handle_array[nhandles++] = fh->oOverlapRead->hEvent;
2195 handle_array[nhandles++] = hEventReadReady;
2201 /* error also counts as ready */
2202 handle_array[nhandles++] = hEventReadReady;
2208 if (wfds && (wfds->handles_pos > 0))
2210 LOG (GNUNET_ERROR_TYPE_DEBUG,
2211 "Adding the write ready event to the array as %d\n",
2213 handle_array[nhandles++] = hEventPipeWrite;
2219 LOG (GNUNET_ERROR_TYPE_DEBUG,
2220 "Adding the socket event to the array as %d\n",
2222 handle_array[nhandles++] = select_finished_event;
2223 if (timeout.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
2229 select_timeout.tv_sec = timeout.rel_value_us / GNUNET_TIME_UNIT_SECONDS.rel_value_us;
2230 select_timeout.tv_usec = (timeout.rel_value_us -
2231 (select_timeout.tv_sec *
2232 GNUNET_TIME_UNIT_SECONDS.rel_value_us));
2233 sp.tv = &select_timeout;
2235 FD_SET (select_wakeup_socket, &aread);
2238 i = recv (select_wakeup_socket,
2239 (char *) &returnedpos,
2246 /* Failed connections cause sockets to be set in errorfds on W32,
2247 * but on POSIX it should set them in writefds.
2248 * First copy all awrite sockets to aexcept, later we'll
2249 * check aexcept and set its contents in awrite as well
2250 * Sockets are also set in errorfds when OOB data is available,
2251 * but we don't use OOB data.
2253 for (i = 0; i < awrite.fd_count; i++)
2254 FD_SET (awrite.fd_array[i],
2256 ResetEvent (select_finished_event);
2257 SetEvent (select_standby_event);
2260 /* NULL-terminate array */
2261 handle_array[nhandles] = NULL;
2262 LOG (GNUNET_ERROR_TYPE_DEBUG,
2263 "nfds: %d, handles: %d, will wait: %llu mcs\n",
2270 = WaitForMultipleObjects (nhandles,
2274 LOG (GNUNET_ERROR_TYPE_DEBUG,
2275 "WaitForMultipleObjects Returned: %d\n",
2280 GNUNET_break (0); /* This branch shouldn't actually be executed...*/
2281 i = (int) WaitForSingleObject (select_finished_event,
2283 returncode = WAIT_TIMEOUT;
2287 /* Shouldn't come this far. If it does - investigate. */
2293 /* Don't wake up select-thread when delay is 0, it should return immediately
2294 * and wake up by itself.
2297 i = send (select_send_socket,
2298 (const char *) &returnedpos,
2301 i = (int) WaitForSingleObject (select_finished_event,
2303 LOG (GNUNET_ERROR_TYPE_DEBUG,
2304 "Finished waiting for the select thread: %d %d\n",
2311 i = recv (select_wakeup_socket,
2312 (char *) &returnedpos,
2316 /* Check aexcept, add its contents to awrite */
2317 for (i = 0; i < aexcept.fd_count; i++)
2318 FD_SET (aexcept.fd_array[i], &awrite);
2321 returnedpos = returncode - WAIT_OBJECT_0;
2322 LOG (GNUNET_ERROR_TYPE_DEBUG,
2323 "return pos is: %d\n",
2328 /* We queued a zero-long read on each pipe to check
2329 * its state, now we must cancel these read operations.
2330 * This must be done while rfds->handles_pos is still
2331 * intact and matches the number of read handles that we
2332 * got from the caller.
2334 for (i = 0; i < rfds->handles_pos; i++)
2336 fh = rfds->handles[i];
2337 if (GNUNET_DISK_HANLDE_TYPE_PIPE == fh->type)
2341 /* We may have some pipes ready for reading. */
2342 if (returnedpos < read_pipes_off)
2343 retcode += check_handles_status (rfds, GNUNET_NO, handle_array[returnedpos]);
2345 rfds->handles_pos = 0;
2347 if (-1 != sp.status)
2348 GNUNET_NETWORK_fdset_copy_native (rfds, &aread, retcode);
2352 retcode += wfds->handles_pos;
2353 /* wfds handles remain untouched */
2354 if (-1 != sp.status)
2355 GNUNET_NETWORK_fdset_copy_native (wfds, &awrite, retcode);
2359 retcode += check_handles_status (rfds,
2361 returnedpos < nhandles ? handle_array[returnedpos] : NULL);
2362 if (-1 != sp.status)
2363 GNUNET_NETWORK_fdset_copy_native (efds, &aexcept, retcode);
2367 retcode += sp.status;
2375 /* end of network.c */