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 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.
17 * @file util/network.c
18 * @brief basic, low-level networking interface
20 * @author Christian Grothoff
23 #include "gnunet_util_lib.h"
26 #define LOG(kind,...) GNUNET_log_from (kind, "util-network", __VA_ARGS__)
27 #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util-network", syscall, filename)
28 #define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util-network", syscall)
30 #define DEBUG_NETWORK GNUNET_EXTRA_LOGGING
33 #ifndef INVALID_SOCKET
34 #define INVALID_SOCKET -1
39 * @brief handle to a socket
41 struct GNUNET_NETWORK_Handle
50 * Address family / domain.
60 * Number of bytes in addr.
65 * Address we were bound to, or NULL.
67 struct sockaddr *addr;
73 * Test if the given protocol family is supported by this system.
75 * @param pf protocol family to test (PF_INET, PF_INET6, PF_UNIX)
76 * @return #GNUNET_OK if the PF is supported
79 GNUNET_NETWORK_test_pf (int pf)
83 s = socket (pf, SOCK_STREAM, 0);
86 if (EAFNOSUPPORT == errno)
88 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
89 "Failed to create test socket: %s\n",
103 * Given a unixpath that is too long (larger than UNIX_PATH_MAX),
104 * shorten it to an acceptable length while keeping it unique
105 * and making sure it remains a valid filename (if possible).
107 * @param unixpath long path, will be freed (or same pointer returned
108 * with moved 0-termination).
109 * @return shortened unixpath, NULL on error
112 GNUNET_NETWORK_shorten_unixpath (char *unixpath)
114 struct sockaddr_un dummy;
117 struct GNUNET_HashCode sh;
118 struct GNUNET_CRYPTO_HashAsciiEncoded ae;
121 upm = sizeof (dummy.sun_path);
122 slen = strlen (unixpath);
124 return unixpath; /* no shortening required */
125 GNUNET_CRYPTO_hash (unixpath, slen, &sh);
126 while (16 + strlen (unixpath) >= upm)
128 if (NULL == (end = strrchr (unixpath, '/')))
130 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
131 _("Unable to shorten unix path `%s' while keeping name unique\n"),
133 GNUNET_free (unixpath);
138 GNUNET_CRYPTO_hash_to_enc (&sh, &ae);
139 strncat (unixpath, (char *) ae.encoding, 16);
146 * If services crash, they can leave a unix domain socket file on the
147 * disk. This needs to be manually removed, because otherwise both
148 * bind() and connect() for the respective address will fail. In this
149 * function, we test if such a left-over file exists, and if so,
150 * remove it (unless there is a listening service at the address).
152 * @param un unix domain socket address to check
155 GNUNET_NETWORK_unix_precheck (const struct sockaddr_un *un)
162 s = socket (AF_UNIX, SOCK_STREAM, 0);
165 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
166 "Failed to open AF_UNIX socket");
170 (struct sockaddr *) un,
171 sizeof (struct sockaddr_un));
173 GNUNET_break (0 == close (s));
175 return; /* another process is listening, do not remove! */
176 if (ECONNREFUSED != eno)
177 return; /* some other error, likely "no such file or directory" -- all well */
178 /* should unlink, but sanity checks first */
179 if (0 != stat (un->sun_path,
181 return; /* failed to 'stat', likely does not exist after all */
182 if (S_IFSOCK != (S_IFMT & sbuf.st_mode))
183 return; /* refuse to unlink anything except sockets */
184 /* finally, really unlink */
185 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
186 "Removing left-over `%s' from previous exeuction\n",
188 if (0 != unlink (un->sun_path))
189 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
198 #define FD_COPY(s, d) do { GNUNET_memcpy ((d), (s), sizeof (fd_set)); } while (0)
203 * Set if a socket should use blocking or non-blocking IO.
206 * @param doBlock blocking mode
207 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
210 GNUNET_NETWORK_socket_set_blocking (struct GNUNET_NETWORK_Handle *fd,
224 SetErrnoFromWinsockError (WSAGetLastError ());
225 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
227 return GNUNET_SYSERR;
233 int flags = fcntl (fd->fd, F_GETFL);
237 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
239 return GNUNET_SYSERR;
242 flags &= ~O_NONBLOCK;
246 if (0 != fcntl (fd->fd,
251 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
253 return GNUNET_SYSERR;
261 * Make a socket non-inheritable to child processes
263 * @param h the socket to make non-inheritable
264 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
265 * @warning Not implemented on Windows
268 socket_set_inheritable (const struct GNUNET_NETWORK_Handle *h)
272 i = fcntl (h->fd, F_GETFD);
274 return GNUNET_SYSERR;
275 if (i == (i | FD_CLOEXEC))
278 if (fcntl (h->fd, F_SETFD, i) < 0)
279 return GNUNET_SYSERR;
283 b = SetHandleInformation ((HANDLE) h->fd, HANDLE_FLAG_INHERIT, 0);
286 SetErrnoFromWinsockError (WSAGetLastError ());
287 return GNUNET_SYSERR;
296 * The MSG_NOSIGNAL equivalent on Mac OS X
298 * @param h the socket to make non-delaying
301 socket_set_nosigpipe (const struct GNUNET_NETWORK_Handle *h)
306 setsockopt (h->fd, SOL_SOCKET, SO_NOSIGPIPE,
307 (const void *) &abs_value,
309 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
315 * Disable delays when sending data via the socket.
316 * (GNUnet makes sure that messages are as big as
319 * @param h the socket to make non-delaying
322 socket_set_nodelay (const struct GNUNET_NETWORK_Handle *h)
331 &value, sizeof (value)))
332 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
335 const char *abs_value = "1";
341 (const void *) abs_value,
343 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
350 * Perform proper canonical initialization for a network handle.
351 * Set it to non-blocking, make it non-inheritable to child
352 * processes, disable SIGPIPE, enable "nodelay" (if non-UNIX
353 * stream socket) and check that it is smaller than FD_SETSIZE.
355 * @param h socket to initialize
356 * @param af address family of the socket
357 * @param type socket type
358 * @return #GNUNET_OK on success, #GNUNET_SYSERR if initialization
359 * failed and the handle was destroyed
362 initialize_network_handle (struct GNUNET_NETWORK_Handle *h,
370 if (h->fd == INVALID_SOCKET)
373 SetErrnoFromWinsockError (WSAGetLastError ());
378 return GNUNET_SYSERR;
381 if (h->fd >= FD_SETSIZE)
383 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (h));
385 return GNUNET_SYSERR;
388 if (GNUNET_OK != socket_set_inheritable (h))
389 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
390 "socket_set_inheritable");
392 if (GNUNET_SYSERR == GNUNET_NETWORK_socket_set_blocking (h, GNUNET_NO))
396 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (h));
398 return GNUNET_SYSERR;
401 socket_set_nosigpipe (h);
403 if ( (type == SOCK_STREAM)
408 socket_set_nodelay (h);
414 * accept a new connection on a socket
416 * @param desc bound socket
417 * @param address address of the connecting peer, may be NULL
418 * @param address_len length of @a address
419 * @return client socket
421 struct GNUNET_NETWORK_Handle *
422 GNUNET_NETWORK_socket_accept (const struct GNUNET_NETWORK_Handle *desc,
423 struct sockaddr *address,
424 socklen_t *address_len)
426 struct GNUNET_NETWORK_Handle *ret;
429 ret = GNUNET_new (struct GNUNET_NETWORK_Handle);
432 struct sockaddr_storage name;
433 socklen_t namelen = sizeof (name);
435 int gsn = getsockname (desc->fd,
436 (struct sockaddr *) &name,
440 LOG (GNUNET_ERROR_TYPE_DEBUG,
441 "Accepting connection on `%s'\n",
442 GNUNET_a2s ((const struct sockaddr *) &name,
446 ret->fd = accept (desc->fd,
457 initialize_network_handle (ret,
458 (NULL != address) ? address->sa_family : desc->af,
469 * Bind a socket to a particular address.
471 * @param desc socket to bind
472 * @param address address to be bound
473 * @param address_len length of @a address
474 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
477 GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc,
478 const struct sockaddr *address,
479 socklen_t address_len)
488 if (AF_INET6 == desc->af)
489 if (setsockopt (desc->fd,
494 LOG_STRERROR (GNUNET_ERROR_TYPE_DEBUG,
500 if (AF_UNIX == address->sa_family)
501 GNUNET_NETWORK_unix_precheck ((const struct sockaddr_un *) address);
505 /* This is required here for TCP sockets, but only on UNIX */
506 if ( (SOCK_STREAM == desc->type) &&
507 (0 != setsockopt (desc->fd,
511 LOG_STRERROR (GNUNET_ERROR_TYPE_DEBUG,
515 /* set permissions of newly created non-abstract UNIX domain socket to
516 "user-only"; applications can choose to relax this later */
517 mode_t old_mask = 0; /* assigned to make compiler happy */
518 const struct sockaddr_un *un = (const struct sockaddr_un *) address;
519 int not_abstract = 0;
521 if ((AF_UNIX == address->sa_family)
522 && ('\0' != un->sun_path[0]) ) /* Not an abstract socket */
525 old_mask = umask (S_IWGRP | S_IRGRP | S_IXGRP | S_IWOTH | S_IROTH | S_IXOTH);
528 ret = bind (desc->fd,
534 (void) umask (old_mask);
538 if (SOCKET_ERROR == ret)
539 SetErrnoFromWinsockError (WSAGetLastError ());
542 return GNUNET_SYSERR;
544 desc->addr = GNUNET_malloc (address_len);
545 GNUNET_memcpy (desc->addr, address, address_len);
546 desc->addrlen = address_len;
556 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
559 GNUNET_NETWORK_socket_close (struct GNUNET_NETWORK_Handle *desc)
567 ret = closesocket (desc->fd);
568 error = WSAGetLastError ();
569 SetErrnoFromWinsockError (error);
570 LOG (GNUNET_ERROR_TYPE_DEBUG,
571 "Closed 0x%x, closesocket() returned %d, GLE is %u\n",
576 ret = close (desc->fd);
579 const struct sockaddr_un *un = (const struct sockaddr_un *) desc->addr;
581 /* Cleanup the UNIX domain socket and its parent directories in case of non
583 if ( (AF_UNIX == desc->af) &&
584 (NULL != desc->addr) &&
585 ('\0' != un->sun_path[0]) )
587 char *dirname = GNUNET_strndup (un->sun_path,
588 sizeof (un->sun_path));
590 if (0 != unlink (dirname))
592 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING,
600 len = strlen (dirname);
601 while ((len > 0) && (dirname[len] != DIR_SEPARATOR))
604 if ((0 != len) && (0 != rmdir (dirname)))
611 /* these are normal and can just be ignored */
614 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
621 GNUNET_free (dirname);
624 GNUNET_NETWORK_socket_free_memory_only_ (desc);
625 return (ret == 0) ? GNUNET_OK : GNUNET_SYSERR;
630 * Only free memory of a socket, keep the file descriptor untouched.
635 GNUNET_NETWORK_socket_free_memory_only_ (struct GNUNET_NETWORK_Handle *desc)
637 GNUNET_free_non_null (desc->addr);
643 * Box a native socket (and check that it is a socket).
645 * @param fd socket to box
646 * @return NULL on error (including not supported on target platform)
648 struct GNUNET_NETWORK_Handle *
649 GNUNET_NETWORK_socket_box_native (SOCKTYPE fd)
651 struct GNUNET_NETWORK_Handle *ret;
655 /* FIXME: Find a better call to check that FD is valid */
657 WSAIoctl (fd, FIONBIO,
658 (void *) &i, sizeof (i),
661 return NULL; /* invalid FD */
662 ret = GNUNET_new (struct GNUNET_NETWORK_Handle);
667 if (fcntl (fd, F_GETFD) < 0)
668 return NULL; /* invalid FD */
669 ret = GNUNET_new (struct GNUNET_NETWORK_Handle);
678 * Connect a socket to some remote address.
681 * @param address peer address
682 * @param address_len length of @a address
683 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
686 GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Handle *desc,
687 const struct sockaddr *address,
688 socklen_t address_len)
692 ret = connect (desc->fd,
696 if (SOCKET_ERROR == ret)
698 SetErrnoFromWinsockError (WSAGetLastError ());
699 if (errno == EWOULDBLOCK)
703 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
711 * @param level protocol level of the option
712 * @param optname identifier of the option
713 * @param optval options
714 * @param optlen length of @a optval
715 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
718 GNUNET_NETWORK_socket_getsockopt (const struct GNUNET_NETWORK_Handle *desc,
726 ret = getsockopt (desc->fd,
733 (SOL_SOCKET == level) &&
734 (SO_ERROR == optname) )
735 *((int *) optval) = GetErrnoFromWinsockError (*((int *) optval));
736 else if (SOCKET_ERROR == ret)
737 SetErrnoFromWinsockError (WSAGetLastError ());
739 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
747 * @param backlog length of the listen queue
748 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
751 GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Handle *desc,
756 ret = listen (desc->fd,
759 if (SOCKET_ERROR == ret)
760 SetErrnoFromWinsockError (WSAGetLastError ());
762 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
767 * How much data is available to be read on this descriptor?
770 * @returns #GNUNET_SYSERR if no data is available, or on error!
773 GNUNET_NETWORK_socket_recvfrom_amount (const struct GNUNET_NETWORK_Handle *desc)
777 /* How much is there to be read? */
781 error = ioctl (desc->fd,
785 return (ssize_t) pending;
786 return GNUNET_SYSERR;
790 error = ioctlsocket (desc->fd,
793 if (error != SOCKET_ERROR)
794 return (ssize_t) pending;
795 return GNUNET_SYSERR;
801 * Read data from a socket (always non-blocking).
804 * @param buffer buffer
805 * @param length length of @a buffer
806 * @param src_addr either the source to recv from, or all zeroes
807 * to be filled in by recvfrom
808 * @param addrlen length of the @a src_addr
811 GNUNET_NETWORK_socket_recvfrom (const struct GNUNET_NETWORK_Handle *desc,
814 struct sockaddr *src_addr,
823 flags |= MSG_DONTWAIT;
826 ret = recvfrom (desc->fd,
833 if (SOCKET_ERROR == ret)
834 SetErrnoFromWinsockError (WSAGetLastError ());
841 * Read data from a connected socket (always non-blocking).
844 * @param buffer buffer
845 * @param length length of @a buffer
846 * @return number of bytes received, -1 on error
849 GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Handle *desc,
859 flags |= MSG_DONTWAIT;
861 ret = recv (desc->fd,
866 if (SOCKET_ERROR == ret)
867 SetErrnoFromWinsockError (WSAGetLastError ());
874 * Send data (always non-blocking).
877 * @param buffer data to send
878 * @param length size of the @a buffer
879 * @return number of bytes sent, #GNUNET_SYSERR on error
882 GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Handle *desc,
891 flags |= MSG_DONTWAIT;
895 flags |= MSG_NOSIGNAL;
898 ret = send (desc->fd,
903 if (SOCKET_ERROR == ret)
904 SetErrnoFromWinsockError (WSAGetLastError ());
912 * Send data to a particular destination (always non-blocking).
913 * This function only works for UDP sockets.
916 * @param message data to send
917 * @param length size of the @a message
918 * @param dest_addr destination address
919 * @param dest_len length of @a address
920 * @return number of bytes sent, #GNUNET_SYSERR on error
923 GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Handle *desc,
926 const struct sockaddr *dest_addr,
935 flags |= MSG_DONTWAIT;
938 flags |= MSG_NOSIGNAL;
940 ret = sendto (desc->fd, message, length, flags, dest_addr, dest_len);
942 if (SOCKET_ERROR == ret)
943 SetErrnoFromWinsockError (WSAGetLastError ());
953 * @param level protocol level of the option
954 * @param option_name option identifier
955 * @param option_value value to set
956 * @param option_len size of @a option_value
957 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
960 GNUNET_NETWORK_socket_setsockopt (struct GNUNET_NETWORK_Handle *fd,
963 const void *option_value,
964 socklen_t option_len)
968 ret = setsockopt (fd->fd,
974 if (SOCKET_ERROR == ret)
975 SetErrnoFromWinsockError (WSAGetLastError ());
977 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
982 * Create a new socket. Configure it for non-blocking IO and
983 * mark it as non-inheritable to child processes (set the
984 * close-on-exec flag).
986 * @param domain domain of the socket
987 * @param type socket type
988 * @param protocol network protocol
989 * @return new socket, NULL on error
991 struct GNUNET_NETWORK_Handle *
992 GNUNET_NETWORK_socket_create (int domain,
996 struct GNUNET_NETWORK_Handle *ret;
999 fd = socket (domain, type, protocol);
1002 ret = GNUNET_new (struct GNUNET_NETWORK_Handle);
1005 initialize_network_handle (ret,
1014 * Shut down socket operations
1015 * @param desc socket
1016 * @param how type of shutdown
1017 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
1020 GNUNET_NETWORK_socket_shutdown (struct GNUNET_NETWORK_Handle *desc,
1025 ret = shutdown (desc->fd, how);
1028 SetErrnoFromWinsockError (WSAGetLastError ());
1030 return (0 == ret) ? GNUNET_OK : GNUNET_SYSERR;
1035 * Disable the "CORK" feature for communication with the given socket,
1036 * forcing the OS to immediately flush the buffer on transmission
1037 * instead of potentially buffering multiple messages. Essentially
1038 * reduces the OS send buffers to zero.
1040 * @param desc socket
1041 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
1044 GNUNET_NETWORK_socket_disable_corking (struct GNUNET_NETWORK_Handle *desc)
1053 setsockopt (desc->fd,
1058 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
1062 setsockopt (desc->fd,
1067 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
1074 setsockopt (desc->fd,
1079 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
1083 setsockopt (desc->fd,
1088 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
1091 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
1101 GNUNET_NETWORK_fdset_zero (struct GNUNET_NETWORK_FDSet *fds)
1103 FD_ZERO (&fds->sds);
1106 fds->handles_pos = 0;
1112 * Add a socket to the FD set
1115 * @param desc socket to add
1118 GNUNET_NETWORK_fdset_set (struct GNUNET_NETWORK_FDSet *fds,
1119 const struct GNUNET_NETWORK_Handle *desc)
1123 fds->nsds = GNUNET_MAX (fds->nsds,
1129 * Check whether a socket is part of the fd set
1132 * @param desc socket
1133 * @return 0 if the FD is not set
1136 GNUNET_NETWORK_fdset_isset (const struct GNUNET_NETWORK_FDSet *fds,
1137 const struct GNUNET_NETWORK_Handle *desc)
1139 return FD_ISSET (desc->fd,
1145 * Add one fd set to another
1147 * @param dst the fd set to add to
1148 * @param src the fd set to add from
1151 GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst,
1152 const struct GNUNET_NETWORK_FDSet *src)
1157 for (nfds = src->nsds; nfds >= 0; nfds--)
1158 if (FD_ISSET (nfds, &src->sds))
1159 FD_SET (nfds, &dst->sds);
1160 dst->nsds = GNUNET_MAX (dst->nsds,
1163 /* This is MinGW32-specific implementation that relies on the code that
1164 * winsock2.h defines for FD_SET. Namely, it relies on FD_SET checking
1165 * that fd being added is not already in the set.
1166 * Also relies on us knowing what's inside fd_set (fd_count and fd_array).
1168 * NOTE: I don't understand why the UNIX-logic wouldn't work
1169 * for the first part here as well. -CG
1173 for (i = 0; i < src->sds.fd_count; i++)
1174 FD_SET (src->sds.fd_array[i],
1176 dst->nsds = GNUNET_MAX (src->nsds,
1179 /* also copy over `struct GNUNET_DISK_FileHandle` array */
1180 if (dst->handles_pos + src->handles_pos > dst->handles_size)
1181 GNUNET_array_grow (dst->handles,
1183 ((dst->handles_pos + src->handles_pos) << 1));
1184 for (i = 0; i < src->handles_pos; i++)
1185 dst->handles[dst->handles_pos++] = src->handles[i];
1191 * Copy one fd set to another
1193 * @param to destination
1194 * @param from source
1197 GNUNET_NETWORK_fdset_copy (struct GNUNET_NETWORK_FDSet *to,
1198 const struct GNUNET_NETWORK_FDSet *from)
1200 FD_COPY (&from->sds,
1202 to->nsds = from->nsds;
1204 if (from->handles_pos > to->handles_size)
1205 GNUNET_array_grow (to->handles,
1207 from->handles_pos * 2);
1208 GNUNET_memcpy (to->handles,
1210 from->handles_pos * sizeof (struct GNUNET_NETWORK_Handle *));
1211 to->handles_pos = from->handles_pos;
1217 * Return file descriptor for this network handle
1219 * @param desc wrapper to process
1220 * @return POSIX file descriptor
1223 GNUNET_NETWORK_get_fd (const struct GNUNET_NETWORK_Handle *desc)
1230 * Return sockaddr for this network handle
1232 * @param desc wrapper to process
1236 GNUNET_NETWORK_get_addr (const struct GNUNET_NETWORK_Handle *desc)
1243 * Return sockaddr length for this network handle
1245 * @param desc wrapper to process
1246 * @return socklen_t for sockaddr
1249 GNUNET_NETWORK_get_addrlen (const struct GNUNET_NETWORK_Handle *desc)
1251 return desc->addrlen;
1256 * Copy a native fd set
1258 * @param to destination
1259 * @param from native source set
1260 * @param nfds the biggest socket number in from + 1
1263 GNUNET_NETWORK_fdset_copy_native (struct GNUNET_NETWORK_FDSet *to,
1274 * Set a native fd in a set
1276 * @param to destination
1277 * @param nfd native FD to set
1280 GNUNET_NETWORK_fdset_set_native (struct GNUNET_NETWORK_FDSet *to,
1283 GNUNET_assert ((nfd >= 0) && (nfd < FD_SETSIZE));
1284 FD_SET (nfd, &to->sds);
1285 to->nsds = GNUNET_MAX (nfd + 1,
1291 * Test native fd in a set
1293 * @param to set to test, NULL for empty set
1294 * @param nfd native FD to test, or -1 for none
1295 * @return #GNUNET_YES if FD is set in the set
1298 GNUNET_NETWORK_fdset_test_native (const struct GNUNET_NETWORK_FDSet *to,
1304 return FD_ISSET (nfd, &to->sds) ? GNUNET_YES : GNUNET_NO;
1309 * Add a file handle to the fd set
1311 * @param h the file handle to add
1314 GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds,
1315 const struct GNUNET_DISK_FileHandle *h)
1318 if (fds->handles_pos == fds->handles_size)
1319 GNUNET_array_grow (fds->handles,
1321 fds->handles_size * 2 + 2);
1322 fds->handles[fds->handles_pos++] = h;
1326 GNUNET_assert (GNUNET_OK ==
1327 GNUNET_DISK_internal_file_handle_ (h,
1332 fds->nsds = GNUNET_MAX (fd + 1,
1339 * Add a file handle to the fd set
1341 * @param h the file handle to add
1344 GNUNET_NETWORK_fdset_handle_set_first (struct GNUNET_NETWORK_FDSet *fds,
1345 const struct GNUNET_DISK_FileHandle *h)
1348 if (fds->handles_pos == fds->handles_size)
1349 GNUNET_array_grow (fds->handles,
1351 fds->handles_size * 2 + 2);
1352 fds->handles[fds->handles_pos] = h;
1353 if (fds->handles[0] != h)
1355 const struct GNUNET_DISK_FileHandle *bak = fds->handles[0];
1356 fds->handles[0] = h;
1357 fds->handles[fds->handles_pos] = bak;
1361 GNUNET_NETWORK_fdset_handle_set (fds, h);
1367 * Check if a file handle is part of an fd set
1370 * @param h file handle
1371 * @return #GNUNET_YES if the file handle is part of the set
1374 GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds,
1375 const struct GNUNET_DISK_FileHandle *h)
1380 for (i=0;i<fds->handles_pos;i++)
1381 if (fds->handles[i] == h)
1385 return FD_ISSET (h->fd,
1393 * Numerically compare pointers to sort them.
1394 * Used to test for overlap in the arrays.
1396 * @param p1 a pointer
1397 * @param p2 a pointer
1398 * @return -1, 0 or 1, if the p1 < p2, p1==p2 or p1 > p2.
1401 ptr_cmp (const void *p1,
1406 if ((intptr_t) p1 < (intptr_t) p2)
1414 * Checks if two fd sets overlap
1416 * @param fds1 first fd set
1417 * @param fds2 second fd set
1418 * @return #GNUNET_YES if they do overlap, #GNUNET_NO otherwise
1421 GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1,
1422 const struct GNUNET_NETWORK_FDSet *fds2)
1427 nfds = GNUNET_MIN (fds1->nsds,
1432 if ( (FD_ISSET (nfds,
1443 /* This code is somewhat hacky, we are not supposed to know what's
1444 * inside of fd_set; also the O(n^2) is really bad... */
1445 for (i = 0; i < fds1->sds.fd_count; i++)
1446 for (j = 0; j < fds2->sds.fd_count; j++)
1447 if (fds1->sds.fd_array[i] == fds2->sds.fd_array[j])
1450 /* take a short cut if possible */
1451 if ( (0 == fds1->handles_pos) ||
1452 (0 == fds2->handles_pos) )
1455 /* Sort file handles array to avoid quadratic complexity when
1456 checking for overlap */
1457 qsort (fds1->handles,
1461 qsort (fds2->handles,
1467 while ( (i < fds1->handles_pos) &&
1468 (j < fds2->handles_pos) )
1470 switch (ptr_cmp (fds1->handles[i],
1490 * @return a new fd set
1492 struct GNUNET_NETWORK_FDSet *
1493 GNUNET_NETWORK_fdset_create ()
1495 struct GNUNET_NETWORK_FDSet *fds;
1497 fds = GNUNET_new (struct GNUNET_NETWORK_FDSet);
1498 GNUNET_NETWORK_fdset_zero (fds);
1504 * Releases the associated memory of an fd set
1509 GNUNET_NETWORK_fdset_destroy (struct GNUNET_NETWORK_FDSet *fds)
1512 GNUNET_array_grow (fds->handles,
1524 struct _select_params
1542 * Timeout for select().
1559 SOCKET wakeup_socket;
1562 * Set to return value from select.
1572 _selector (LPVOID p)
1574 struct _select_params *sp = p;
1578 WaitForSingleObject (sp->standby,
1580 ResetEvent (sp->standby);
1581 sp->status = select (1,
1586 if (FD_ISSET (sp->wakeup_socket,
1589 FD_CLR (sp->wakeup_socket,
1593 SetEvent (sp->wakeup);
1599 static HANDLE hEventPipeWrite;
1601 static HANDLE hEventReadReady;
1603 static struct _select_params sp;
1605 static HANDLE select_thread;
1607 static HANDLE select_finished_event;
1609 static HANDLE select_standby_event;
1611 static SOCKET select_wakeup_socket = -1;
1613 static SOCKET select_send_socket = -1;
1615 static struct timeval select_timeout;
1619 * On W32, we actually use a thread to help with the
1620 * event loop due to W32-API limitations. This function
1621 * initializes that thread.
1624 initialize_select_thread ()
1626 SOCKET select_listening_socket = -1;
1627 struct sockaddr_in s_in;
1632 select_standby_event = CreateEvent (NULL, TRUE, FALSE, NULL);
1633 select_finished_event = CreateEvent (NULL, TRUE, FALSE, NULL);
1635 select_wakeup_socket = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
1637 select_listening_socket = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
1640 res = ioctlsocket (select_wakeup_socket, FIONBIO, &p);
1641 LOG (GNUNET_ERROR_TYPE_DEBUG,
1642 "Select thread initialization: ioctlsocket() returns %d\n",
1645 alen = sizeof (s_in);
1646 s_in.sin_family = AF_INET;
1648 s_in.sin_addr.S_un.S_un_b.s_b1 = 127;
1649 s_in.sin_addr.S_un.S_un_b.s_b2 = 0;
1650 s_in.sin_addr.S_un.S_un_b.s_b3 = 0;
1651 s_in.sin_addr.S_un.S_un_b.s_b4 = 1;
1652 res = bind (select_listening_socket,
1653 (const struct sockaddr *) &s_in,
1655 LOG (GNUNET_ERROR_TYPE_DEBUG,
1656 "Select thread initialization: bind() returns %d\n",
1659 res = getsockname (select_listening_socket,
1660 (struct sockaddr *) &s_in,
1662 LOG (GNUNET_ERROR_TYPE_DEBUG,
1663 "Select thread initialization: getsockname() returns %d\n",
1666 res = listen (select_listening_socket,
1668 LOG (GNUNET_ERROR_TYPE_DEBUG,
1669 "Select thread initialization: listen() returns %d\n",
1671 res = connect (select_wakeup_socket,
1672 (const struct sockaddr *) &s_in,
1674 LOG (GNUNET_ERROR_TYPE_DEBUG,
1675 "Select thread initialization: connect() returns %d\n",
1678 select_send_socket = accept (select_listening_socket,
1679 (struct sockaddr *) &s_in,
1682 closesocket (select_listening_socket);
1684 sp.wakeup = select_finished_event;
1685 sp.standby = select_standby_event;
1686 sp.wakeup_socket = select_wakeup_socket;
1688 select_thread = CreateThread (NULL,
1699 * Test if the given @a port is available.
1701 * @param ipproto transport protocol to test (i.e. IPPROTO_TCP)
1702 * @param port port number to test
1703 * @return #GNUNET_OK if the port is available, #GNUNET_NO if not
1706 GNUNET_NETWORK_test_port_free (int ipproto,
1709 struct GNUNET_NETWORK_Handle *socket;
1712 char open_port_str[6];
1713 struct addrinfo hint;
1714 struct addrinfo *ret;
1715 struct addrinfo *ai;
1717 GNUNET_snprintf (open_port_str,
1718 sizeof (open_port_str),
1720 (unsigned int) port);
1721 socktype = (IPPROTO_TCP == ipproto) ? SOCK_STREAM : SOCK_DGRAM;
1723 memset (&hint, 0, sizeof (hint));
1724 hint.ai_family = AF_UNSPEC; /* IPv4 and IPv6 */
1725 hint.ai_socktype = socktype;
1726 hint.ai_protocol = ipproto;
1727 hint.ai_addrlen = 0;
1728 hint.ai_addr = NULL;
1729 hint.ai_canonname = NULL;
1730 hint.ai_next = NULL;
1731 hint.ai_flags = AI_PASSIVE | AI_NUMERICSERV; /* Wild card address */
1732 GNUNET_assert (0 == getaddrinfo (NULL,
1736 bind_status = GNUNET_NO;
1737 for (ai = ret; NULL != ai; ai = ai->ai_next)
1739 socket = GNUNET_NETWORK_socket_create (ai->ai_family,
1744 bind_status = GNUNET_NETWORK_socket_bind (socket,
1747 GNUNET_NETWORK_socket_close (socket);
1748 if (GNUNET_OK != bind_status)
1758 * Check if sockets or pipes meet certain conditions
1760 * @param rfds set of sockets or pipes to be checked for readability
1761 * @param wfds set of sockets or pipes to be checked for writability
1762 * @param efds set of sockets or pipes to be checked for exceptions
1763 * @param timeout relative value when to return
1764 * @return number of selected sockets or pipes, #GNUNET_SYSERR on error
1767 GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
1768 struct GNUNET_NETWORK_FDSet *wfds,
1769 struct GNUNET_NETWORK_FDSet *efds,
1770 const struct GNUNET_TIME_Relative timeout)
1780 nfds = GNUNET_MAX (nfds,
1783 nfds = GNUNET_MAX (nfds,
1786 (timeout.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us))
1789 LOG (GNUNET_ERROR_TYPE_ERROR,
1790 _("Fatal internal logic error, process hangs in `%s' (abort with CTRL-C)!\n"),
1793 if (timeout.rel_value_us / GNUNET_TIME_UNIT_SECONDS.rel_value_us > (unsigned long long) LONG_MAX)
1795 tv.tv_sec = LONG_MAX;
1796 tv.tv_usec = 999999L;
1800 tv.tv_sec = (long) (timeout.rel_value_us / GNUNET_TIME_UNIT_SECONDS.rel_value_us);
1802 (timeout.rel_value_us -
1803 (tv.tv_sec * GNUNET_TIME_UNIT_SECONDS.rel_value_us));
1805 return select (nfds,
1806 (NULL != rfds) ? &rfds->sds : NULL,
1807 (NULL != wfds) ? &wfds->sds : NULL,
1808 (NULL != efds) ? &efds->sds : NULL,
1809 (timeout.rel_value_us ==
1810 GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us) ? NULL : &tv);
1819 * Non-blocking test if a pipe is ready for reading.
1821 * @param fh pipe handle
1822 * @return #GNUNET_YES if the pipe is ready for reading
1825 pipe_read_ready (const struct GNUNET_DISK_FileHandle *fh)
1829 DWORD waitstatus = 0;
1832 bret = PeekNamedPipe (fh->h, NULL, 0, NULL, &waitstatus, NULL);
1833 error = GetLastError ();
1836 /* TODO: either add more errors to this condition, or eliminate it
1837 * entirely (failed to peek -> pipe is in serious trouble, should
1838 * be selected as readable).
1840 if ( (error != ERROR_BROKEN_PIPE) &&
1841 (error != ERROR_INVALID_HANDLE) )
1844 else if (waitstatus <= 0)
1851 * Non-blocking test if a pipe is having an IO exception.
1853 * @param fh pipe handle
1854 * @return #GNUNET_YES if the pipe is having an IO exception.
1857 pipe_except_ready (const struct GNUNET_DISK_FileHandle *fh)
1861 if (PeekNamedPipe (fh->h, NULL, 0, NULL, &dwBytes, NULL))
1868 * Iterate over handles in fds, destructively rewrite the
1869 * handles array contents of fds so that it starts with the
1870 * handles that are ready, and update handles_pos accordingly.
1872 * @param fds set of handles (usually pipes) to be checked for readiness
1873 * @param except GNUNET_NO if fds should be checked for readiness to read,
1874 * GNUNET_YES if fds should be checked for exceptions
1875 * (there is no way to check for write-readiness - pipes are always write-ready)
1876 * @param set_for_sure a HANDLE that is known to be set already,
1877 * because WaitForMultipleObjects() returned its index.
1878 * @return number of ready handles
1881 check_handles_status (struct GNUNET_NETWORK_FDSet *fds,
1883 HANDLE set_for_sure)
1885 const struct GNUNET_DISK_FileHandle *fh;
1889 for (woff = 0, roff = 0; roff < fds->handles_pos; roff++)
1891 fh = fds->handles[roff];
1892 if (fh == set_for_sure)
1894 fds->handles[woff++] = fh;
1896 else if (fh->type == GNUNET_DISK_HANLDE_TYPE_PIPE)
1898 if ((except && pipe_except_ready (fh)) ||
1899 (!except && pipe_read_ready (fh)))
1900 fds->handles[woff++] = fh;
1902 else if (fh->type == GNUNET_DISK_HANLDE_TYPE_FILE)
1905 fds->handles[woff++] = fh;
1909 if (WAIT_OBJECT_0 == WaitForSingleObject (fh->h, 0))
1910 fds->handles[woff++] = fh;
1913 fds->handles_pos = woff;
1919 * Check if sockets or pipes meet certain conditions, version for W32.
1921 * @param rfds set of sockets or pipes to be checked for readability
1922 * @param wfds set of sockets or pipes to be checked for writability
1923 * @param efds set of sockets or pipes to be checked for exceptions
1924 * @param timeout relative value when to return
1925 * @return number of selected sockets or pipes, #GNUNET_SYSERR on error
1928 GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
1929 struct GNUNET_NETWORK_FDSet *wfds,
1930 struct GNUNET_NETWORK_FDSet *efds,
1931 const struct GNUNET_TIME_Relative timeout)
1933 const struct GNUNET_DISK_FileHandle *fh;
1942 HANDLE handle_array[FD_SETSIZE + 2];
1944 int returnedpos = 0;
1954 nfds = GNUNET_MAX (nfds, rfds->nsds);
1955 handles += rfds->handles_pos;
1959 nfds = GNUNET_MAX (nfds, wfds->nsds);
1960 handles += wfds->handles_pos;
1964 nfds = GNUNET_MAX (nfds, efds->nsds);
1965 handles += efds->handles_pos;
1969 (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == timeout.rel_value_us) &&
1973 LOG (GNUNET_ERROR_TYPE_ERROR,
1974 _("Fatal internal logic error, process hangs in `%s' (abort with CTRL-C)!\n"),
1977 #define SAFE_FD_ISSET(fd, set) (set != NULL && FD_ISSET(fd, set))
1978 /* calculate how long we need to wait in microseconds */
1979 if (timeout.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
1981 mcs_total = INFINITE;
1982 ms_rounded = INFINITE;
1986 mcs_total = timeout.rel_value_us / GNUNET_TIME_UNIT_MICROSECONDS.rel_value_us;
1987 ms_rounded = (DWORD) (mcs_total / GNUNET_TIME_UNIT_MILLISECONDS.rel_value_us);
1988 if (mcs_total > 0 && ms_rounded == 0)
1991 /* select() may be used as a portable way to sleep */
1992 if (! (rfds || wfds || efds))
1998 if (NULL == select_thread)
1999 initialize_select_thread ();
2005 FD_COPY (&rfds->sds, &aread);
2007 FD_COPY (&wfds->sds, &awrite);
2009 FD_COPY (&efds->sds, &aexcept);
2011 /* Start by doing a fast check on sockets and pipes (without
2012 waiting). It is cheap, and is sufficient most of the time. By
2013 profiling we detected that to be true in 90% of the cases.
2016 /* Do the select now */
2017 select_timeout.tv_sec = 0;
2018 select_timeout.tv_usec = 0;
2020 /* Copy all the writes to the except, so we can detect connect() errors */
2021 for (i = 0; i < awrite.fd_count; i++)
2022 FD_SET (awrite.fd_array[i],
2024 if ( (aread.fd_count > 0) ||
2025 (awrite.fd_count > 0) ||
2026 (aexcept.fd_count > 0) )
2027 selectret = select (1,
2028 (NULL != rfds) ? &aread : NULL,
2029 (NULL != wfds) ? &awrite : NULL,
2034 if (-1 == selectret)
2036 /* Throw an error early on, while we still have the context. */
2037 LOG (GNUNET_ERROR_TYPE_ERROR,
2038 "W32 select(%d, %d, %d) failed: %lu\n",
2039 rfds ? aread.fd_count : 0,
2040 wfds ? awrite.fd_count : 0,
2046 /* Check aexcept, if something is in there and we copied that
2047 FD before to detect connect() errors, add it back to the
2048 write set to report errors. */
2050 for (i = 0; i < aexcept.fd_count; i++)
2051 if (FD_ISSET (aexcept.fd_array[i],
2053 FD_SET (aexcept.fd_array[i],
2057 /* If our select returned something or is a 0-timed request, then
2058 also check the pipes and get out of here! */
2059 /* Sadly, it means code duplication :( */
2060 if ( (selectret > 0) || (0 == mcs_total) )
2065 if (rfds && (rfds->handles_pos > 0))
2066 retcode += check_handles_status (rfds, GNUNET_NO, NULL);
2068 /* wfds handles remain untouched, on W32
2069 we pretend our pipes are "always" write-ready */
2072 if (efds && (efds->handles_pos > 0))
2073 retcode += check_handles_status (efds, GNUNET_YES, NULL);
2077 GNUNET_NETWORK_fdset_zero (rfds);
2078 if (selectret != -1)
2079 GNUNET_NETWORK_fdset_copy_native (rfds, &aread, selectret);
2083 GNUNET_NETWORK_fdset_zero (wfds);
2084 if (selectret != -1)
2085 GNUNET_NETWORK_fdset_copy_native (wfds, &awrite, selectret);
2089 GNUNET_NETWORK_fdset_zero (efds);
2090 if (selectret != -1)
2091 GNUNET_NETWORK_fdset_copy_native (efds, &aexcept, selectret);
2093 if (-1 == selectret)
2095 /* Add our select() FDs to the total return value */
2096 retcode += selectret;
2100 /* If we got this far, use slower implementation that is able to do a waiting select
2101 on both sockets and pipes simultaneously */
2103 /* Events for pipes */
2104 if (! hEventReadReady)
2105 hEventReadReady = CreateEvent (NULL, TRUE, TRUE, NULL);
2106 if (! hEventPipeWrite)
2107 hEventPipeWrite = CreateEvent (NULL, TRUE, TRUE, NULL);
2114 FD_COPY (&rfds->sds, &aread);
2116 FD_COPY (&wfds->sds, &awrite);
2118 FD_COPY (&efds->sds, &aexcept);
2119 /* We will first Add the PIPES to the events */
2120 /* Track how far in `handle_array` the read pipes go,
2121 so we may by-pass them quickly if none of them
2124 if (rfds && (rfds->handles_pos > 0))
2126 for (i = 0; i <rfds->handles_pos; i++)
2128 fh = rfds->handles[i];
2129 if (fh->type == GNUNET_DISK_HANLDE_TYPE_EVENT)
2131 handle_array[nhandles++] = fh->h;
2134 if (fh->type != GNUNET_DISK_HANLDE_TYPE_PIPE)
2136 /* Read zero bytes to check the status of the pipe */
2137 if (! ReadFile (fh->h, NULL, 0, NULL, fh->oOverlapRead))
2139 DWORD error_code = GetLastError ();
2141 if (error_code == ERROR_IO_PENDING)
2143 /* add as unready */
2144 handle_array[nhandles++] = fh->oOverlapRead->hEvent;
2150 handle_array[nhandles++] = hEventReadReady;
2156 /* error also counts as ready */
2157 handle_array[nhandles++] = hEventReadReady;
2163 if (wfds && (wfds->handles_pos > 0))
2165 LOG (GNUNET_ERROR_TYPE_DEBUG,
2166 "Adding the write ready event to the array as %d\n",
2168 handle_array[nhandles++] = hEventPipeWrite;
2174 LOG (GNUNET_ERROR_TYPE_DEBUG,
2175 "Adding the socket event to the array as %d\n",
2177 handle_array[nhandles++] = select_finished_event;
2178 if (timeout.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
2184 select_timeout.tv_sec = timeout.rel_value_us / GNUNET_TIME_UNIT_SECONDS.rel_value_us;
2185 select_timeout.tv_usec = (timeout.rel_value_us -
2186 (select_timeout.tv_sec *
2187 GNUNET_TIME_UNIT_SECONDS.rel_value_us));
2188 sp.tv = &select_timeout;
2190 FD_SET (select_wakeup_socket, &aread);
2193 i = recv (select_wakeup_socket,
2194 (char *) &returnedpos,
2201 /* Failed connections cause sockets to be set in errorfds on W32,
2202 * but on POSIX it should set them in writefds.
2203 * First copy all awrite sockets to aexcept, later we'll
2204 * check aexcept and set its contents in awrite as well
2205 * Sockets are also set in errorfds when OOB data is available,
2206 * but we don't use OOB data.
2208 for (i = 0; i < awrite.fd_count; i++)
2209 FD_SET (awrite.fd_array[i],
2211 ResetEvent (select_finished_event);
2212 SetEvent (select_standby_event);
2215 /* NULL-terminate array */
2216 handle_array[nhandles] = NULL;
2217 LOG (GNUNET_ERROR_TYPE_DEBUG,
2218 "nfds: %d, handles: %d, will wait: %llu mcs\n",
2225 = WaitForMultipleObjects (nhandles,
2229 LOG (GNUNET_ERROR_TYPE_DEBUG,
2230 "WaitForMultipleObjects Returned: %d\n",
2235 GNUNET_break (0); /* This branch shouldn't actually be executed...*/
2236 i = (int) WaitForSingleObject (select_finished_event,
2238 returncode = WAIT_TIMEOUT;
2242 /* Shouldn't come this far. If it does - investigate. */
2248 /* Don't wake up select-thread when delay is 0, it should return immediately
2249 * and wake up by itself.
2252 i = send (select_send_socket,
2253 (const char *) &returnedpos,
2256 i = (int) WaitForSingleObject (select_finished_event,
2258 LOG (GNUNET_ERROR_TYPE_DEBUG,
2259 "Finished waiting for the select thread: %d %d\n",
2266 i = recv (select_wakeup_socket,
2267 (char *) &returnedpos,
2271 /* Check aexcept, add its contents to awrite */
2272 for (i = 0; i < aexcept.fd_count; i++)
2273 FD_SET (aexcept.fd_array[i], &awrite);
2276 returnedpos = returncode - WAIT_OBJECT_0;
2277 LOG (GNUNET_ERROR_TYPE_DEBUG,
2278 "return pos is: %d\n",
2283 /* We queued a zero-long read on each pipe to check
2284 * its state, now we must cancel these read operations.
2285 * This must be done while rfds->handles_pos is still
2286 * intact and matches the number of read handles that we
2287 * got from the caller.
2289 for (i = 0; i < rfds->handles_pos; i++)
2291 fh = rfds->handles[i];
2292 if (GNUNET_DISK_HANLDE_TYPE_PIPE == fh->type)
2296 /* We may have some pipes ready for reading. */
2297 if (returnedpos < read_pipes_off)
2298 retcode += check_handles_status (rfds, GNUNET_NO, handle_array[returnedpos]);
2300 rfds->handles_pos = 0;
2302 if (-1 != sp.status)
2303 GNUNET_NETWORK_fdset_copy_native (rfds, &aread, retcode);
2307 retcode += wfds->handles_pos;
2308 /* wfds handles remain untouched */
2309 if (-1 != sp.status)
2310 GNUNET_NETWORK_fdset_copy_native (wfds, &awrite, retcode);
2314 retcode += check_handles_status (rfds,
2316 returnedpos < nhandles ? handle_array[returnedpos] : NULL);
2317 if (-1 != sp.status)
2318 GNUNET_NETWORK_fdset_copy_native (efds, &aexcept, retcode);
2322 retcode += sp.status;
2330 /* end of network.c */