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/>.
20 * @file util/network.c
21 * @brief basic, low-level networking interface
23 * @author Christian Grothoff
26 #include "gnunet_util_lib.h"
29 #define LOG(kind,...) GNUNET_log_from (kind, "util-network", __VA_ARGS__)
30 #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util-network", syscall, filename)
31 #define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util-network", syscall)
33 #define DEBUG_NETWORK GNUNET_EXTRA_LOGGING
36 #ifndef INVALID_SOCKET
37 #define INVALID_SOCKET -1
42 * @brief handle to a socket
44 struct GNUNET_NETWORK_Handle
53 * Address family / domain.
63 * Number of bytes in addr.
68 * Address we were bound to, or NULL.
70 struct sockaddr *addr;
76 * Test if the given protocol family is supported by this system.
78 * @param pf protocol family to test (PF_INET, PF_INET6, PF_UNIX)
79 * @return #GNUNET_OK if the PF is supported
82 GNUNET_NETWORK_test_pf (int pf)
86 s = socket (pf, SOCK_STREAM, 0);
89 if (EAFNOSUPPORT == errno)
91 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
92 "Failed to create test socket: %s\n",
106 * Given a unixpath that is too long (larger than UNIX_PATH_MAX),
107 * shorten it to an acceptable length while keeping it unique
108 * and making sure it remains a valid filename (if possible).
110 * @param unixpath long path, will be freed (or same pointer returned
111 * with moved 0-termination).
112 * @return shortened unixpath, NULL on error
115 GNUNET_NETWORK_shorten_unixpath (char *unixpath)
117 struct sockaddr_un dummy;
120 struct GNUNET_HashCode sh;
121 struct GNUNET_CRYPTO_HashAsciiEncoded ae;
124 upm = sizeof (dummy.sun_path);
125 slen = strlen (unixpath);
127 return unixpath; /* no shortening required */
128 GNUNET_CRYPTO_hash (unixpath, slen, &sh);
129 while (16 + 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);
149 * If services crash, they can leave a unix domain socket file on the
150 * disk. This needs to be manually removed, because otherwise both
151 * bind() and connect() for the respective address will fail. In this
152 * function, we test if such a left-over file exists, and if so,
153 * remove it (unless there is a listening service at the address).
155 * @param un unix domain socket address to check
158 GNUNET_NETWORK_unix_precheck (const struct sockaddr_un *un)
165 s = socket (AF_UNIX, SOCK_STREAM, 0);
168 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
169 "Failed to open AF_UNIX socket");
173 (struct sockaddr *) un,
174 sizeof (struct sockaddr_un));
176 GNUNET_break (0 == close (s));
178 return; /* another process is listening, do not remove! */
179 if (ECONNREFUSED != eno)
180 return; /* some other error, likely "no such file or directory" -- all well */
181 /* should unlink, but sanity checks first */
182 if (0 != stat (un->sun_path,
184 return; /* failed to 'stat', likely does not exist after all */
185 if (S_IFSOCK != (S_IFMT & sbuf.st_mode))
186 return; /* refuse to unlink anything except sockets */
187 /* finally, really unlink */
188 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
189 "Removing left-over `%s' from previous exeuction\n",
191 if (0 != unlink (un->sun_path))
192 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
201 #define FD_COPY(s, d) do { GNUNET_memcpy ((d), (s), sizeof (fd_set)); } while (0)
206 * Set if a socket should use blocking or non-blocking IO.
209 * @param doBlock blocking mode
210 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
213 GNUNET_NETWORK_socket_set_blocking (struct GNUNET_NETWORK_Handle *fd,
227 SetErrnoFromWinsockError (WSAGetLastError ());
228 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
230 return GNUNET_SYSERR;
236 int flags = fcntl (fd->fd, F_GETFL);
240 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
242 return GNUNET_SYSERR;
245 flags &= ~O_NONBLOCK;
249 if (0 != fcntl (fd->fd,
254 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
256 return GNUNET_SYSERR;
264 * Make a socket non-inheritable to child processes
266 * @param h the socket to make non-inheritable
267 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
268 * @warning Not implemented on Windows
271 socket_set_inheritable (const struct GNUNET_NETWORK_Handle *h)
275 i = fcntl (h->fd, F_GETFD);
277 return GNUNET_SYSERR;
278 if (i == (i | FD_CLOEXEC))
281 if (fcntl (h->fd, F_SETFD, i) < 0)
282 return GNUNET_SYSERR;
286 b = SetHandleInformation ((HANDLE) h->fd, HANDLE_FLAG_INHERIT, 0);
289 SetErrnoFromWinsockError (WSAGetLastError ());
290 return GNUNET_SYSERR;
299 * The MSG_NOSIGNAL equivalent on Mac OS X
301 * @param h the socket to make non-delaying
304 socket_set_nosigpipe (const struct GNUNET_NETWORK_Handle *h)
309 setsockopt (h->fd, SOL_SOCKET, SO_NOSIGPIPE,
310 (const void *) &abs_value,
312 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
318 * Disable delays when sending data via the socket.
319 * (GNUnet makes sure that messages are as big as
322 * @param h the socket to make non-delaying
325 socket_set_nodelay (const struct GNUNET_NETWORK_Handle *h)
334 &value, sizeof (value)))
335 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
338 const char *abs_value = "1";
344 (const void *) abs_value,
346 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
353 * Perform proper canonical initialization for a network handle.
354 * Set it to non-blocking, make it non-inheritable to child
355 * processes, disable SIGPIPE, enable "nodelay" (if non-UNIX
356 * stream socket) and check that it is smaller than FD_SETSIZE.
358 * @param h socket to initialize
359 * @param af address family of the socket
360 * @param type socket type
361 * @return #GNUNET_OK on success, #GNUNET_SYSERR if initialization
362 * failed and the handle was destroyed
365 initialize_network_handle (struct GNUNET_NETWORK_Handle *h,
373 if (h->fd == INVALID_SOCKET)
376 SetErrnoFromWinsockError (WSAGetLastError ());
381 return GNUNET_SYSERR;
384 if (h->fd >= FD_SETSIZE)
386 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (h));
388 return GNUNET_SYSERR;
391 if (GNUNET_OK != socket_set_inheritable (h))
392 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
393 "socket_set_inheritable");
395 if (GNUNET_SYSERR == GNUNET_NETWORK_socket_set_blocking (h, GNUNET_NO))
399 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (h));
401 return GNUNET_SYSERR;
404 socket_set_nosigpipe (h);
406 if ( (type == SOCK_STREAM)
411 socket_set_nodelay (h);
417 * accept a new connection on a socket
419 * @param desc bound socket
420 * @param address address of the connecting peer, may be NULL
421 * @param address_len length of @a address
422 * @return client socket
424 struct GNUNET_NETWORK_Handle *
425 GNUNET_NETWORK_socket_accept (const struct GNUNET_NETWORK_Handle *desc,
426 struct sockaddr *address,
427 socklen_t *address_len)
429 struct GNUNET_NETWORK_Handle *ret;
432 ret = GNUNET_new (struct GNUNET_NETWORK_Handle);
435 struct sockaddr_storage name;
436 socklen_t namelen = sizeof (name);
438 int gsn = getsockname (desc->fd,
439 (struct sockaddr *) &name,
443 LOG (GNUNET_ERROR_TYPE_DEBUG,
444 "Accepting connection on `%s'\n",
445 GNUNET_a2s ((const struct sockaddr *) &name,
449 ret->fd = accept (desc->fd,
460 initialize_network_handle (ret,
461 (NULL != address) ? address->sa_family : desc->af,
472 * Bind a socket to a particular address.
474 * @param desc socket to bind
475 * @param address address to be bound
476 * @param address_len length of @a address
477 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
480 GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc,
481 const struct sockaddr *address,
482 socklen_t address_len)
491 if (AF_INET6 == desc->af)
492 if (setsockopt (desc->fd,
497 LOG_STRERROR (GNUNET_ERROR_TYPE_DEBUG,
503 if (AF_UNIX == address->sa_family)
504 GNUNET_NETWORK_unix_precheck ((const struct sockaddr_un *) address);
508 /* This is required here for TCP sockets, but only on UNIX */
509 if ( (SOCK_STREAM == desc->type) &&
510 (0 != setsockopt (desc->fd,
514 LOG_STRERROR (GNUNET_ERROR_TYPE_DEBUG,
518 /* set permissions of newly created non-abstract UNIX domain socket to
519 "user-only"; applications can choose to relax this later */
520 mode_t old_mask = 0; /* assigned to make compiler happy */
521 const struct sockaddr_un *un = (const struct sockaddr_un *) address;
522 int not_abstract = 0;
524 if ((AF_UNIX == address->sa_family)
525 && ('\0' != un->sun_path[0]) ) /* Not an abstract socket */
528 old_mask = umask (S_IWGRP | S_IRGRP | S_IXGRP | S_IWOTH | S_IROTH | S_IXOTH);
531 ret = bind (desc->fd,
537 (void) umask (old_mask);
541 if (SOCKET_ERROR == ret)
542 SetErrnoFromWinsockError (WSAGetLastError ());
545 return GNUNET_SYSERR;
547 desc->addr = GNUNET_malloc (address_len);
548 GNUNET_memcpy (desc->addr, address, address_len);
549 desc->addrlen = address_len;
559 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
562 GNUNET_NETWORK_socket_close (struct GNUNET_NETWORK_Handle *desc)
570 ret = closesocket (desc->fd);
571 error = WSAGetLastError ();
572 SetErrnoFromWinsockError (error);
573 LOG (GNUNET_ERROR_TYPE_DEBUG,
574 "Closed 0x%x, closesocket() returned %d, GLE is %u\n",
579 ret = close (desc->fd);
582 const struct sockaddr_un *un = (const struct sockaddr_un *) desc->addr;
584 /* Cleanup the UNIX domain socket and its parent directories in case of non
586 if ( (AF_UNIX == desc->af) &&
587 (NULL != desc->addr) &&
588 ('\0' != un->sun_path[0]) )
590 char *dirname = GNUNET_strndup (un->sun_path,
591 sizeof (un->sun_path));
593 if (0 != unlink (dirname))
595 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING,
603 len = strlen (dirname);
604 while ((len > 0) && (dirname[len] != DIR_SEPARATOR))
607 if ((0 != len) && (0 != rmdir (dirname)))
614 /* these are normal and can just be ignored */
617 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
624 GNUNET_free (dirname);
627 GNUNET_NETWORK_socket_free_memory_only_ (desc);
628 return (ret == 0) ? GNUNET_OK : GNUNET_SYSERR;
633 * Only free memory of a socket, keep the file descriptor untouched.
638 GNUNET_NETWORK_socket_free_memory_only_ (struct GNUNET_NETWORK_Handle *desc)
640 GNUNET_free_non_null (desc->addr);
646 * Box a native socket (and check that it is a socket).
648 * @param fd socket to box
649 * @return NULL on error (including not supported on target platform)
651 struct GNUNET_NETWORK_Handle *
652 GNUNET_NETWORK_socket_box_native (SOCKTYPE fd)
654 struct GNUNET_NETWORK_Handle *ret;
658 /* FIXME: Find a better call to check that FD is valid */
660 WSAIoctl (fd, FIONBIO,
661 (void *) &i, sizeof (i),
664 return NULL; /* invalid FD */
665 ret = GNUNET_new (struct GNUNET_NETWORK_Handle);
670 if (fcntl (fd, F_GETFD) < 0)
671 return NULL; /* invalid FD */
672 ret = GNUNET_new (struct GNUNET_NETWORK_Handle);
681 * Connect a socket to some remote address.
684 * @param address peer address
685 * @param address_len length of @a address
686 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
689 GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Handle *desc,
690 const struct sockaddr *address,
691 socklen_t address_len)
695 ret = connect (desc->fd,
699 if (SOCKET_ERROR == ret)
701 SetErrnoFromWinsockError (WSAGetLastError ());
702 if (errno == EWOULDBLOCK)
706 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
714 * @param level protocol level of the option
715 * @param optname identifier of the option
716 * @param optval options
717 * @param optlen length of @a optval
718 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
721 GNUNET_NETWORK_socket_getsockopt (const struct GNUNET_NETWORK_Handle *desc,
729 ret = getsockopt (desc->fd,
736 (SOL_SOCKET == level) &&
737 (SO_ERROR == optname) )
738 *((int *) optval) = GetErrnoFromWinsockError (*((int *) optval));
739 else if (SOCKET_ERROR == ret)
740 SetErrnoFromWinsockError (WSAGetLastError ());
742 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
750 * @param backlog length of the listen queue
751 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
754 GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Handle *desc,
759 ret = listen (desc->fd,
762 if (SOCKET_ERROR == ret)
763 SetErrnoFromWinsockError (WSAGetLastError ());
765 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
770 * How much data is available to be read on this descriptor?
773 * @returns #GNUNET_SYSERR if no data is available, or on error!
776 GNUNET_NETWORK_socket_recvfrom_amount (const struct GNUNET_NETWORK_Handle *desc)
780 /* How much is there to be read? */
784 error = ioctl (desc->fd,
788 return (ssize_t) pending;
789 return GNUNET_SYSERR;
793 error = ioctlsocket (desc->fd,
796 if (error != SOCKET_ERROR)
797 return (ssize_t) pending;
798 return GNUNET_SYSERR;
804 * Read data from a socket (always non-blocking).
807 * @param buffer buffer
808 * @param length length of @a buffer
809 * @param src_addr either the source to recv from, or all zeroes
810 * to be filled in by recvfrom
811 * @param addrlen length of the @a src_addr
814 GNUNET_NETWORK_socket_recvfrom (const struct GNUNET_NETWORK_Handle *desc,
817 struct sockaddr *src_addr,
826 flags |= MSG_DONTWAIT;
829 ret = recvfrom (desc->fd,
836 if (SOCKET_ERROR == ret)
837 SetErrnoFromWinsockError (WSAGetLastError ());
844 * Read data from a connected socket (always non-blocking).
847 * @param buffer buffer
848 * @param length length of @a buffer
849 * @return number of bytes received, -1 on error
852 GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Handle *desc,
862 flags |= MSG_DONTWAIT;
864 ret = recv (desc->fd,
869 if (SOCKET_ERROR == ret)
870 SetErrnoFromWinsockError (WSAGetLastError ());
877 * Send data (always non-blocking).
880 * @param buffer data to send
881 * @param length size of the @a buffer
882 * @return number of bytes sent, #GNUNET_SYSERR on error
885 GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Handle *desc,
894 flags |= MSG_DONTWAIT;
898 flags |= MSG_NOSIGNAL;
901 ret = send (desc->fd,
906 if (SOCKET_ERROR == ret)
907 SetErrnoFromWinsockError (WSAGetLastError ());
915 * Send data to a particular destination (always non-blocking).
916 * This function only works for UDP sockets.
919 * @param message data to send
920 * @param length size of the @a message
921 * @param dest_addr destination address
922 * @param dest_len length of @a address
923 * @return number of bytes sent, #GNUNET_SYSERR on error
926 GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Handle *desc,
929 const struct sockaddr *dest_addr,
938 flags |= MSG_DONTWAIT;
941 flags |= MSG_NOSIGNAL;
943 ret = sendto (desc->fd, message, length, flags, dest_addr, dest_len);
945 if (SOCKET_ERROR == ret)
946 SetErrnoFromWinsockError (WSAGetLastError ());
956 * @param level protocol level of the option
957 * @param option_name option identifier
958 * @param option_value value to set
959 * @param option_len size of @a option_value
960 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
963 GNUNET_NETWORK_socket_setsockopt (struct GNUNET_NETWORK_Handle *fd,
966 const void *option_value,
967 socklen_t option_len)
971 ret = setsockopt (fd->fd,
977 if (SOCKET_ERROR == ret)
978 SetErrnoFromWinsockError (WSAGetLastError ());
980 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
985 * Create a new socket. Configure it for non-blocking IO and
986 * mark it as non-inheritable to child processes (set the
987 * close-on-exec flag).
989 * @param domain domain of the socket
990 * @param type socket type
991 * @param protocol network protocol
992 * @return new socket, NULL on error
994 struct GNUNET_NETWORK_Handle *
995 GNUNET_NETWORK_socket_create (int domain,
999 struct GNUNET_NETWORK_Handle *ret;
1002 fd = socket (domain, type, protocol);
1005 ret = GNUNET_new (struct GNUNET_NETWORK_Handle);
1008 initialize_network_handle (ret,
1017 * Shut down socket operations
1018 * @param desc socket
1019 * @param how type of shutdown
1020 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
1023 GNUNET_NETWORK_socket_shutdown (struct GNUNET_NETWORK_Handle *desc,
1028 ret = shutdown (desc->fd, how);
1031 SetErrnoFromWinsockError (WSAGetLastError ());
1033 return (0 == ret) ? GNUNET_OK : GNUNET_SYSERR;
1038 * Disable the "CORK" feature for communication with the given socket,
1039 * forcing the OS to immediately flush the buffer on transmission
1040 * instead of potentially buffering multiple messages. Essentially
1041 * reduces the OS send buffers to zero.
1043 * @param desc socket
1044 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
1047 GNUNET_NETWORK_socket_disable_corking (struct GNUNET_NETWORK_Handle *desc)
1056 setsockopt (desc->fd,
1061 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
1065 setsockopt (desc->fd,
1070 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
1077 setsockopt (desc->fd,
1082 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
1086 setsockopt (desc->fd,
1091 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
1094 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
1104 GNUNET_NETWORK_fdset_zero (struct GNUNET_NETWORK_FDSet *fds)
1106 FD_ZERO (&fds->sds);
1109 fds->handles_pos = 0;
1115 * Add a socket to the FD set
1118 * @param desc socket to add
1121 GNUNET_NETWORK_fdset_set (struct GNUNET_NETWORK_FDSet *fds,
1122 const struct GNUNET_NETWORK_Handle *desc)
1126 fds->nsds = GNUNET_MAX (fds->nsds,
1132 * Check whether a socket is part of the fd set
1135 * @param desc socket
1136 * @return 0 if the FD is not set
1139 GNUNET_NETWORK_fdset_isset (const struct GNUNET_NETWORK_FDSet *fds,
1140 const struct GNUNET_NETWORK_Handle *desc)
1142 return FD_ISSET (desc->fd,
1148 * Add one fd set to another
1150 * @param dst the fd set to add to
1151 * @param src the fd set to add from
1154 GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst,
1155 const struct GNUNET_NETWORK_FDSet *src)
1160 for (nfds = src->nsds; nfds >= 0; nfds--)
1161 if (FD_ISSET (nfds, &src->sds))
1162 FD_SET (nfds, &dst->sds);
1163 dst->nsds = GNUNET_MAX (dst->nsds,
1166 /* This is MinGW32-specific implementation that relies on the code that
1167 * winsock2.h defines for FD_SET. Namely, it relies on FD_SET checking
1168 * that fd being added is not already in the set.
1169 * Also relies on us knowing what's inside fd_set (fd_count and fd_array).
1171 * NOTE: I don't understand why the UNIX-logic wouldn't work
1172 * for the first part here as well. -CG
1176 for (i = 0; i < src->sds.fd_count; i++)
1177 FD_SET (src->sds.fd_array[i],
1179 dst->nsds = GNUNET_MAX (src->nsds,
1182 /* also copy over `struct GNUNET_DISK_FileHandle` array */
1183 if (dst->handles_pos + src->handles_pos > dst->handles_size)
1184 GNUNET_array_grow (dst->handles,
1186 ((dst->handles_pos + src->handles_pos) << 1));
1187 for (i = 0; i < src->handles_pos; i++)
1188 dst->handles[dst->handles_pos++] = src->handles[i];
1194 * Copy one fd set to another
1196 * @param to destination
1197 * @param from source
1200 GNUNET_NETWORK_fdset_copy (struct GNUNET_NETWORK_FDSet *to,
1201 const struct GNUNET_NETWORK_FDSet *from)
1203 FD_COPY (&from->sds,
1205 to->nsds = from->nsds;
1207 if (from->handles_pos > to->handles_size)
1208 GNUNET_array_grow (to->handles,
1210 from->handles_pos * 2);
1211 GNUNET_memcpy (to->handles,
1213 from->handles_pos * sizeof (struct GNUNET_NETWORK_Handle *));
1214 to->handles_pos = from->handles_pos;
1220 * Return file descriptor for this network handle
1222 * @param desc wrapper to process
1223 * @return POSIX file descriptor
1226 GNUNET_NETWORK_get_fd (const struct GNUNET_NETWORK_Handle *desc)
1233 * Return sockaddr for this network handle
1235 * @param desc wrapper to process
1239 GNUNET_NETWORK_get_addr (const struct GNUNET_NETWORK_Handle *desc)
1246 * Return sockaddr length for this network handle
1248 * @param desc wrapper to process
1249 * @return socklen_t for sockaddr
1252 GNUNET_NETWORK_get_addrlen (const struct GNUNET_NETWORK_Handle *desc)
1254 return desc->addrlen;
1259 * Copy a native fd set
1261 * @param to destination
1262 * @param from native source set
1263 * @param nfds the biggest socket number in from + 1
1266 GNUNET_NETWORK_fdset_copy_native (struct GNUNET_NETWORK_FDSet *to,
1277 * Set a native fd in a set
1279 * @param to destination
1280 * @param nfd native FD to set
1283 GNUNET_NETWORK_fdset_set_native (struct GNUNET_NETWORK_FDSet *to,
1286 GNUNET_assert ((nfd >= 0) && (nfd < FD_SETSIZE));
1287 FD_SET (nfd, &to->sds);
1288 to->nsds = GNUNET_MAX (nfd + 1,
1294 * Test native fd in a set
1296 * @param to set to test, NULL for empty set
1297 * @param nfd native FD to test, or -1 for none
1298 * @return #GNUNET_YES if FD is set in the set
1301 GNUNET_NETWORK_fdset_test_native (const struct GNUNET_NETWORK_FDSet *to,
1307 return FD_ISSET (nfd, &to->sds) ? GNUNET_YES : GNUNET_NO;
1312 * Add a file handle to the fd set
1314 * @param h the file handle to add
1317 GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds,
1318 const struct GNUNET_DISK_FileHandle *h)
1321 if (fds->handles_pos == fds->handles_size)
1322 GNUNET_array_grow (fds->handles,
1324 fds->handles_size * 2 + 2);
1325 fds->handles[fds->handles_pos++] = h;
1329 GNUNET_assert (GNUNET_OK ==
1330 GNUNET_DISK_internal_file_handle_ (h,
1335 fds->nsds = GNUNET_MAX (fd + 1,
1342 * Add a file handle to the fd set
1344 * @param h the file handle to add
1347 GNUNET_NETWORK_fdset_handle_set_first (struct GNUNET_NETWORK_FDSet *fds,
1348 const struct GNUNET_DISK_FileHandle *h)
1351 if (fds->handles_pos == fds->handles_size)
1352 GNUNET_array_grow (fds->handles,
1354 fds->handles_size * 2 + 2);
1355 fds->handles[fds->handles_pos] = h;
1356 if (fds->handles[0] != h)
1358 const struct GNUNET_DISK_FileHandle *bak = fds->handles[0];
1359 fds->handles[0] = h;
1360 fds->handles[fds->handles_pos] = bak;
1364 GNUNET_NETWORK_fdset_handle_set (fds, h);
1370 * Check if a file handle is part of an fd set
1373 * @param h file handle
1374 * @return #GNUNET_YES if the file handle is part of the set
1377 GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds,
1378 const struct GNUNET_DISK_FileHandle *h)
1383 for (i=0;i<fds->handles_pos;i++)
1384 if (fds->handles[i] == h)
1388 return FD_ISSET (h->fd,
1396 * Numerically compare pointers to sort them.
1397 * Used to test for overlap in the arrays.
1399 * @param p1 a pointer
1400 * @param p2 a pointer
1401 * @return -1, 0 or 1, if the p1 < p2, p1==p2 or p1 > p2.
1404 ptr_cmp (const void *p1,
1409 if ((intptr_t) p1 < (intptr_t) p2)
1417 * Checks if two fd sets overlap
1419 * @param fds1 first fd set
1420 * @param fds2 second fd set
1421 * @return #GNUNET_YES if they do overlap, #GNUNET_NO otherwise
1424 GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1,
1425 const struct GNUNET_NETWORK_FDSet *fds2)
1430 nfds = GNUNET_MIN (fds1->nsds,
1435 if ( (FD_ISSET (nfds,
1446 /* This code is somewhat hacky, we are not supposed to know what's
1447 * inside of fd_set; also the O(n^2) is really bad... */
1448 for (i = 0; i < fds1->sds.fd_count; i++)
1449 for (j = 0; j < fds2->sds.fd_count; j++)
1450 if (fds1->sds.fd_array[i] == fds2->sds.fd_array[j])
1453 /* take a short cut if possible */
1454 if ( (0 == fds1->handles_pos) ||
1455 (0 == fds2->handles_pos) )
1458 /* Sort file handles array to avoid quadratic complexity when
1459 checking for overlap */
1460 qsort (fds1->handles,
1464 qsort (fds2->handles,
1470 while ( (i < fds1->handles_pos) &&
1471 (j < fds2->handles_pos) )
1473 switch (ptr_cmp (fds1->handles[i],
1493 * @return a new fd set
1495 struct GNUNET_NETWORK_FDSet *
1496 GNUNET_NETWORK_fdset_create ()
1498 struct GNUNET_NETWORK_FDSet *fds;
1500 fds = GNUNET_new (struct GNUNET_NETWORK_FDSet);
1501 GNUNET_NETWORK_fdset_zero (fds);
1507 * Releases the associated memory of an fd set
1512 GNUNET_NETWORK_fdset_destroy (struct GNUNET_NETWORK_FDSet *fds)
1515 GNUNET_array_grow (fds->handles,
1527 struct _select_params
1545 * Timeout for select().
1562 SOCKET wakeup_socket;
1565 * Set to return value from select.
1575 _selector (LPVOID p)
1577 struct _select_params *sp = p;
1581 WaitForSingleObject (sp->standby,
1583 ResetEvent (sp->standby);
1584 sp->status = select (1,
1589 if (FD_ISSET (sp->wakeup_socket,
1592 FD_CLR (sp->wakeup_socket,
1596 SetEvent (sp->wakeup);
1602 static HANDLE hEventPipeWrite;
1604 static HANDLE hEventReadReady;
1606 static struct _select_params sp;
1608 static HANDLE select_thread;
1610 static HANDLE select_finished_event;
1612 static HANDLE select_standby_event;
1614 static SOCKET select_wakeup_socket = -1;
1616 static SOCKET select_send_socket = -1;
1618 static struct timeval select_timeout;
1622 * On W32, we actually use a thread to help with the
1623 * event loop due to W32-API limitations. This function
1624 * initializes that thread.
1627 initialize_select_thread ()
1629 SOCKET select_listening_socket = -1;
1630 struct sockaddr_in s_in;
1635 select_standby_event = CreateEvent (NULL, TRUE, FALSE, NULL);
1636 select_finished_event = CreateEvent (NULL, TRUE, FALSE, NULL);
1638 select_wakeup_socket = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
1640 select_listening_socket = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
1643 res = ioctlsocket (select_wakeup_socket, FIONBIO, &p);
1644 LOG (GNUNET_ERROR_TYPE_DEBUG,
1645 "Select thread initialization: ioctlsocket() returns %d\n",
1648 alen = sizeof (s_in);
1649 s_in.sin_family = AF_INET;
1651 s_in.sin_addr.S_un.S_un_b.s_b1 = 127;
1652 s_in.sin_addr.S_un.S_un_b.s_b2 = 0;
1653 s_in.sin_addr.S_un.S_un_b.s_b3 = 0;
1654 s_in.sin_addr.S_un.S_un_b.s_b4 = 1;
1655 res = bind (select_listening_socket,
1656 (const struct sockaddr *) &s_in,
1658 LOG (GNUNET_ERROR_TYPE_DEBUG,
1659 "Select thread initialization: bind() returns %d\n",
1662 res = getsockname (select_listening_socket,
1663 (struct sockaddr *) &s_in,
1665 LOG (GNUNET_ERROR_TYPE_DEBUG,
1666 "Select thread initialization: getsockname() returns %d\n",
1669 res = listen (select_listening_socket,
1671 LOG (GNUNET_ERROR_TYPE_DEBUG,
1672 "Select thread initialization: listen() returns %d\n",
1674 res = connect (select_wakeup_socket,
1675 (const struct sockaddr *) &s_in,
1677 LOG (GNUNET_ERROR_TYPE_DEBUG,
1678 "Select thread initialization: connect() returns %d\n",
1681 select_send_socket = accept (select_listening_socket,
1682 (struct sockaddr *) &s_in,
1685 closesocket (select_listening_socket);
1687 sp.wakeup = select_finished_event;
1688 sp.standby = select_standby_event;
1689 sp.wakeup_socket = select_wakeup_socket;
1691 select_thread = CreateThread (NULL,
1702 * Test if the given @a port is available.
1704 * @param ipproto transport protocol to test (i.e. IPPROTO_TCP)
1705 * @param port port number to test
1706 * @return #GNUNET_OK if the port is available, #GNUNET_NO if not
1709 GNUNET_NETWORK_test_port_free (int ipproto,
1712 struct GNUNET_NETWORK_Handle *socket;
1715 char open_port_str[6];
1716 struct addrinfo hint;
1717 struct addrinfo *ret;
1718 struct addrinfo *ai;
1720 GNUNET_snprintf (open_port_str,
1721 sizeof (open_port_str),
1723 (unsigned int) port);
1724 socktype = (IPPROTO_TCP == ipproto) ? SOCK_STREAM : SOCK_DGRAM;
1726 memset (&hint, 0, sizeof (hint));
1727 hint.ai_family = AF_UNSPEC; /* IPv4 and IPv6 */
1728 hint.ai_socktype = socktype;
1729 hint.ai_protocol = ipproto;
1730 hint.ai_addrlen = 0;
1731 hint.ai_addr = NULL;
1732 hint.ai_canonname = NULL;
1733 hint.ai_next = NULL;
1734 hint.ai_flags = AI_PASSIVE | AI_NUMERICSERV; /* Wild card address */
1735 GNUNET_assert (0 == getaddrinfo (NULL,
1739 bind_status = GNUNET_NO;
1740 for (ai = ret; NULL != ai; ai = ai->ai_next)
1742 socket = GNUNET_NETWORK_socket_create (ai->ai_family,
1747 bind_status = GNUNET_NETWORK_socket_bind (socket,
1750 GNUNET_NETWORK_socket_close (socket);
1751 if (GNUNET_OK != bind_status)
1761 * Check if sockets or pipes meet certain conditions
1763 * @param rfds set of sockets or pipes to be checked for readability
1764 * @param wfds set of sockets or pipes to be checked for writability
1765 * @param efds set of sockets or pipes to be checked for exceptions
1766 * @param timeout relative value when to return
1767 * @return number of selected sockets or pipes, #GNUNET_SYSERR on error
1770 GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
1771 struct GNUNET_NETWORK_FDSet *wfds,
1772 struct GNUNET_NETWORK_FDSet *efds,
1773 const struct GNUNET_TIME_Relative timeout)
1783 nfds = GNUNET_MAX (nfds,
1786 nfds = GNUNET_MAX (nfds,
1789 (timeout.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us))
1792 LOG (GNUNET_ERROR_TYPE_ERROR,
1793 _("Fatal internal logic error, process hangs in `%s' (abort with CTRL-C)!\n"),
1796 if (timeout.rel_value_us / GNUNET_TIME_UNIT_SECONDS.rel_value_us > (unsigned long long) LONG_MAX)
1798 tv.tv_sec = LONG_MAX;
1799 tv.tv_usec = 999999L;
1803 tv.tv_sec = (long) (timeout.rel_value_us / GNUNET_TIME_UNIT_SECONDS.rel_value_us);
1805 (timeout.rel_value_us -
1806 (tv.tv_sec * GNUNET_TIME_UNIT_SECONDS.rel_value_us));
1808 return select (nfds,
1809 (NULL != rfds) ? &rfds->sds : NULL,
1810 (NULL != wfds) ? &wfds->sds : NULL,
1811 (NULL != efds) ? &efds->sds : NULL,
1812 (timeout.rel_value_us ==
1813 GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us) ? NULL : &tv);
1822 * Non-blocking test if a pipe is ready for reading.
1824 * @param fh pipe handle
1825 * @return #GNUNET_YES if the pipe is ready for reading
1828 pipe_read_ready (const struct GNUNET_DISK_FileHandle *fh)
1832 DWORD waitstatus = 0;
1835 bret = PeekNamedPipe (fh->h, NULL, 0, NULL, &waitstatus, NULL);
1836 error = GetLastError ();
1839 /* TODO: either add more errors to this condition, or eliminate it
1840 * entirely (failed to peek -> pipe is in serious trouble, should
1841 * be selected as readable).
1843 if ( (error != ERROR_BROKEN_PIPE) &&
1844 (error != ERROR_INVALID_HANDLE) )
1847 else if (waitstatus <= 0)
1854 * Non-blocking test if a pipe is having an IO exception.
1856 * @param fh pipe handle
1857 * @return #GNUNET_YES if the pipe is having an IO exception.
1860 pipe_except_ready (const struct GNUNET_DISK_FileHandle *fh)
1864 if (PeekNamedPipe (fh->h, NULL, 0, NULL, &dwBytes, NULL))
1871 * Iterate over handles in fds, destructively rewrite the
1872 * handles array contents of fds so that it starts with the
1873 * handles that are ready, and update handles_pos accordingly.
1875 * @param fds set of handles (usually pipes) to be checked for readiness
1876 * @param except GNUNET_NO if fds should be checked for readiness to read,
1877 * GNUNET_YES if fds should be checked for exceptions
1878 * (there is no way to check for write-readiness - pipes are always write-ready)
1879 * @param set_for_sure a HANDLE that is known to be set already,
1880 * because WaitForMultipleObjects() returned its index.
1881 * @return number of ready handles
1884 check_handles_status (struct GNUNET_NETWORK_FDSet *fds,
1886 HANDLE set_for_sure)
1888 const struct GNUNET_DISK_FileHandle *fh;
1892 for (woff = 0, roff = 0; roff < fds->handles_pos; roff++)
1894 fh = fds->handles[roff];
1895 if (fh == set_for_sure)
1897 fds->handles[woff++] = fh;
1899 else if (fh->type == GNUNET_DISK_HANLDE_TYPE_PIPE)
1901 if ((except && pipe_except_ready (fh)) ||
1902 (!except && pipe_read_ready (fh)))
1903 fds->handles[woff++] = fh;
1905 else if (fh->type == GNUNET_DISK_HANLDE_TYPE_FILE)
1908 fds->handles[woff++] = fh;
1912 if (WAIT_OBJECT_0 == WaitForSingleObject (fh->h, 0))
1913 fds->handles[woff++] = fh;
1916 fds->handles_pos = woff;
1922 * Check if sockets or pipes meet certain conditions, version for W32.
1924 * @param rfds set of sockets or pipes to be checked for readability
1925 * @param wfds set of sockets or pipes to be checked for writability
1926 * @param efds set of sockets or pipes to be checked for exceptions
1927 * @param timeout relative value when to return
1928 * @return number of selected sockets or pipes, #GNUNET_SYSERR on error
1931 GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
1932 struct GNUNET_NETWORK_FDSet *wfds,
1933 struct GNUNET_NETWORK_FDSet *efds,
1934 const struct GNUNET_TIME_Relative timeout)
1936 const struct GNUNET_DISK_FileHandle *fh;
1945 HANDLE handle_array[FD_SETSIZE + 2];
1947 int returnedpos = 0;
1957 nfds = GNUNET_MAX (nfds, rfds->nsds);
1958 handles += rfds->handles_pos;
1962 nfds = GNUNET_MAX (nfds, wfds->nsds);
1963 handles += wfds->handles_pos;
1967 nfds = GNUNET_MAX (nfds, efds->nsds);
1968 handles += efds->handles_pos;
1972 (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == timeout.rel_value_us) &&
1976 LOG (GNUNET_ERROR_TYPE_ERROR,
1977 _("Fatal internal logic error, process hangs in `%s' (abort with CTRL-C)!\n"),
1980 #define SAFE_FD_ISSET(fd, set) (set != NULL && FD_ISSET(fd, set))
1981 /* calculate how long we need to wait in microseconds */
1982 if (timeout.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
1984 mcs_total = INFINITE;
1985 ms_rounded = INFINITE;
1989 mcs_total = timeout.rel_value_us / GNUNET_TIME_UNIT_MICROSECONDS.rel_value_us;
1990 ms_rounded = (DWORD) (mcs_total / GNUNET_TIME_UNIT_MILLISECONDS.rel_value_us);
1991 if (mcs_total > 0 && ms_rounded == 0)
1994 /* select() may be used as a portable way to sleep */
1995 if (! (rfds || wfds || efds))
2001 if (NULL == select_thread)
2002 initialize_select_thread ();
2008 FD_COPY (&rfds->sds, &aread);
2010 FD_COPY (&wfds->sds, &awrite);
2012 FD_COPY (&efds->sds, &aexcept);
2014 /* Start by doing a fast check on sockets and pipes (without
2015 waiting). It is cheap, and is sufficient most of the time. By
2016 profiling we detected that to be true in 90% of the cases.
2019 /* Do the select now */
2020 select_timeout.tv_sec = 0;
2021 select_timeout.tv_usec = 0;
2023 /* Copy all the writes to the except, so we can detect connect() errors */
2024 for (i = 0; i < awrite.fd_count; i++)
2025 FD_SET (awrite.fd_array[i],
2027 if ( (aread.fd_count > 0) ||
2028 (awrite.fd_count > 0) ||
2029 (aexcept.fd_count > 0) )
2030 selectret = select (1,
2031 (NULL != rfds) ? &aread : NULL,
2032 (NULL != wfds) ? &awrite : NULL,
2037 if (-1 == selectret)
2039 /* Throw an error early on, while we still have the context. */
2040 LOG (GNUNET_ERROR_TYPE_ERROR,
2041 "W32 select(%d, %d, %d) failed: %lu\n",
2042 rfds ? aread.fd_count : 0,
2043 wfds ? awrite.fd_count : 0,
2049 /* Check aexcept, if something is in there and we copied that
2050 FD before to detect connect() errors, add it back to the
2051 write set to report errors. */
2053 for (i = 0; i < aexcept.fd_count; i++)
2054 if (FD_ISSET (aexcept.fd_array[i],
2056 FD_SET (aexcept.fd_array[i],
2060 /* If our select returned something or is a 0-timed request, then
2061 also check the pipes and get out of here! */
2062 /* Sadly, it means code duplication :( */
2063 if ( (selectret > 0) || (0 == mcs_total) )
2068 if (rfds && (rfds->handles_pos > 0))
2069 retcode += check_handles_status (rfds, GNUNET_NO, NULL);
2071 /* wfds handles remain untouched, on W32
2072 we pretend our pipes are "always" write-ready */
2075 if (efds && (efds->handles_pos > 0))
2076 retcode += check_handles_status (efds, GNUNET_YES, NULL);
2080 GNUNET_NETWORK_fdset_zero (rfds);
2081 if (selectret != -1)
2082 GNUNET_NETWORK_fdset_copy_native (rfds, &aread, selectret);
2086 GNUNET_NETWORK_fdset_zero (wfds);
2087 if (selectret != -1)
2088 GNUNET_NETWORK_fdset_copy_native (wfds, &awrite, selectret);
2092 GNUNET_NETWORK_fdset_zero (efds);
2093 if (selectret != -1)
2094 GNUNET_NETWORK_fdset_copy_native (efds, &aexcept, selectret);
2096 if (-1 == selectret)
2098 /* Add our select() FDs to the total return value */
2099 retcode += selectret;
2103 /* If we got this far, use slower implementation that is able to do a waiting select
2104 on both sockets and pipes simultaneously */
2106 /* Events for pipes */
2107 if (! hEventReadReady)
2108 hEventReadReady = CreateEvent (NULL, TRUE, TRUE, NULL);
2109 if (! hEventPipeWrite)
2110 hEventPipeWrite = CreateEvent (NULL, TRUE, TRUE, NULL);
2117 FD_COPY (&rfds->sds, &aread);
2119 FD_COPY (&wfds->sds, &awrite);
2121 FD_COPY (&efds->sds, &aexcept);
2122 /* We will first Add the PIPES to the events */
2123 /* Track how far in `handle_array` the read pipes go,
2124 so we may by-pass them quickly if none of them
2127 if (rfds && (rfds->handles_pos > 0))
2129 for (i = 0; i <rfds->handles_pos; i++)
2131 fh = rfds->handles[i];
2132 if (fh->type == GNUNET_DISK_HANLDE_TYPE_EVENT)
2134 handle_array[nhandles++] = fh->h;
2137 if (fh->type != GNUNET_DISK_HANLDE_TYPE_PIPE)
2139 /* Read zero bytes to check the status of the pipe */
2140 if (! ReadFile (fh->h, NULL, 0, NULL, fh->oOverlapRead))
2142 DWORD error_code = GetLastError ();
2144 if (error_code == ERROR_IO_PENDING)
2146 /* add as unready */
2147 handle_array[nhandles++] = fh->oOverlapRead->hEvent;
2153 handle_array[nhandles++] = hEventReadReady;
2159 /* error also counts as ready */
2160 handle_array[nhandles++] = hEventReadReady;
2166 if (wfds && (wfds->handles_pos > 0))
2168 LOG (GNUNET_ERROR_TYPE_DEBUG,
2169 "Adding the write ready event to the array as %d\n",
2171 handle_array[nhandles++] = hEventPipeWrite;
2177 LOG (GNUNET_ERROR_TYPE_DEBUG,
2178 "Adding the socket event to the array as %d\n",
2180 handle_array[nhandles++] = select_finished_event;
2181 if (timeout.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
2187 select_timeout.tv_sec = timeout.rel_value_us / GNUNET_TIME_UNIT_SECONDS.rel_value_us;
2188 select_timeout.tv_usec = (timeout.rel_value_us -
2189 (select_timeout.tv_sec *
2190 GNUNET_TIME_UNIT_SECONDS.rel_value_us));
2191 sp.tv = &select_timeout;
2193 FD_SET (select_wakeup_socket, &aread);
2196 i = recv (select_wakeup_socket,
2197 (char *) &returnedpos,
2204 /* Failed connections cause sockets to be set in errorfds on W32,
2205 * but on POSIX it should set them in writefds.
2206 * First copy all awrite sockets to aexcept, later we'll
2207 * check aexcept and set its contents in awrite as well
2208 * Sockets are also set in errorfds when OOB data is available,
2209 * but we don't use OOB data.
2211 for (i = 0; i < awrite.fd_count; i++)
2212 FD_SET (awrite.fd_array[i],
2214 ResetEvent (select_finished_event);
2215 SetEvent (select_standby_event);
2218 /* NULL-terminate array */
2219 handle_array[nhandles] = NULL;
2220 LOG (GNUNET_ERROR_TYPE_DEBUG,
2221 "nfds: %d, handles: %d, will wait: %llu mcs\n",
2228 = WaitForMultipleObjects (nhandles,
2232 LOG (GNUNET_ERROR_TYPE_DEBUG,
2233 "WaitForMultipleObjects Returned: %d\n",
2238 GNUNET_break (0); /* This branch shouldn't actually be executed...*/
2239 i = (int) WaitForSingleObject (select_finished_event,
2241 returncode = WAIT_TIMEOUT;
2245 /* Shouldn't come this far. If it does - investigate. */
2251 /* Don't wake up select-thread when delay is 0, it should return immediately
2252 * and wake up by itself.
2255 i = send (select_send_socket,
2256 (const char *) &returnedpos,
2259 i = (int) WaitForSingleObject (select_finished_event,
2261 LOG (GNUNET_ERROR_TYPE_DEBUG,
2262 "Finished waiting for the select thread: %d %d\n",
2269 i = recv (select_wakeup_socket,
2270 (char *) &returnedpos,
2274 /* Check aexcept, add its contents to awrite */
2275 for (i = 0; i < aexcept.fd_count; i++)
2276 FD_SET (aexcept.fd_array[i], &awrite);
2279 returnedpos = returncode - WAIT_OBJECT_0;
2280 LOG (GNUNET_ERROR_TYPE_DEBUG,
2281 "return pos is: %d\n",
2286 /* We queued a zero-long read on each pipe to check
2287 * its state, now we must cancel these read operations.
2288 * This must be done while rfds->handles_pos is still
2289 * intact and matches the number of read handles that we
2290 * got from the caller.
2292 for (i = 0; i < rfds->handles_pos; i++)
2294 fh = rfds->handles[i];
2295 if (GNUNET_DISK_HANLDE_TYPE_PIPE == fh->type)
2299 /* We may have some pipes ready for reading. */
2300 if (returnedpos < read_pipes_off)
2301 retcode += check_handles_status (rfds, GNUNET_NO, handle_array[returnedpos]);
2303 rfds->handles_pos = 0;
2305 if (-1 != sp.status)
2306 GNUNET_NETWORK_fdset_copy_native (rfds, &aread, retcode);
2310 retcode += wfds->handles_pos;
2311 /* wfds handles remain untouched */
2312 if (-1 != sp.status)
2313 GNUNET_NETWORK_fdset_copy_native (wfds, &awrite, retcode);
2317 retcode += check_handles_status (rfds,
2319 returnedpos < nhandles ? handle_array[returnedpos] : NULL);
2320 if (-1 != sp.status)
2321 GNUNET_NETWORK_fdset_copy_native (efds, &aexcept, retcode);
2325 retcode += sp.status;
2333 /* end of network.c */