2 This file is part of GNUnet.
3 (C) 2009-2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file util/network.c
23 * @brief basic, low-level networking interface
25 * @author Christian Grothoff
28 #include "gnunet_util_lib.h"
31 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
32 #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
33 #define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall)
35 #define DEBUG_NETWORK GNUNET_EXTRA_LOGGING
38 #ifndef INVALID_SOCKET
39 #define INVALID_SOCKET -1
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)
88 s = socket (pf, SOCK_STREAM, 0);
91 if (EAFNOSUPPORT == errno)
93 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
94 "Failed to create test socket: %s\n",
108 * Given a unixpath that is too long (larger than UNIX_PATH_MAX),
109 * shorten it to an acceptable length while keeping it unique
110 * and making sure it remains a valid filename (if possible).
112 * @param unixpath long path, will be freed (or same pointer returned
113 * with moved 0-termination).
114 * @return shortened unixpath, NULL on error
117 GNUNET_NETWORK_shorten_unixpath (char *unixpath)
119 struct sockaddr_un dummy;
122 struct GNUNET_HashCode sh;
123 struct GNUNET_CRYPTO_HashAsciiEncoded ae;
126 upm = sizeof (dummy.sun_path);
127 slen = strlen (unixpath);
129 return unixpath; /* no shortening required */
130 GNUNET_CRYPTO_hash (unixpath, slen, &sh);
132 strlen (unixpath) >= upm)
134 if (NULL == (end = strrchr (unixpath, '/')))
136 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
137 _("Unable to shorten unix path `%s' while keeping name unique\n"),
139 GNUNET_free (unixpath);
144 GNUNET_CRYPTO_hash_to_enc (&sh, &ae);
145 strncat (unixpath, (char*) ae.encoding, 16);
151 #define FD_COPY(s, d) (memcpy ((d), (s), sizeof (fd_set)))
156 * Set if a socket should use blocking or non-blocking IO.
159 * @param doBlock blocking mode
160 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
163 GNUNET_NETWORK_socket_set_blocking (struct GNUNET_NETWORK_Handle *fd,
177 SetErrnoFromWinsockError (WSAGetLastError ());
178 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
180 return GNUNET_SYSERR;
186 int flags = fcntl (fd->fd, F_GETFL);
191 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
193 return GNUNET_SYSERR;
196 flags &= ~O_NONBLOCK;
200 if (0 != fcntl (fd->fd,
205 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
207 return GNUNET_SYSERR;
215 * Make a socket non-inheritable to child processes
217 * @param h the socket to make non-inheritable
218 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
219 * @warning Not implemented on Windows
222 socket_set_inheritable (const struct GNUNET_NETWORK_Handle *h)
226 i = fcntl (h->fd, F_GETFD);
228 return GNUNET_SYSERR;
229 if (i == (i | FD_CLOEXEC))
232 if (fcntl (h->fd, F_SETFD, i) < 0)
233 return GNUNET_SYSERR;
237 b = SetHandleInformation ((HANDLE) h->fd, HANDLE_FLAG_INHERIT, 0);
240 SetErrnoFromWinsockError (WSAGetLastError ());
241 return GNUNET_SYSERR;
250 * The MSG_NOSIGNAL equivalent on Mac OS X
252 * @param h the socket to make non-delaying
255 socket_set_nosigpipe (const struct GNUNET_NETWORK_Handle *h)
260 setsockopt (h->fd, SOL_SOCKET, SO_NOSIGPIPE,
261 (const void *) &abs_value,
263 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
269 * Disable delays when sending data via the socket.
270 * (GNUnet makes sure that messages are as big as
273 * @param h the socket to make non-delaying
276 socket_set_nodelay (const struct GNUNET_NETWORK_Handle *h)
285 &value, sizeof (value)))
286 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
289 const char *abs_value = "1";
292 setsockopt (h->fd, IPPROTO_TCP, TCP_NODELAY,
293 (const void *) abs_value,
295 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
302 * Perform proper canonical initialization for a network handle.
303 * Set it to non-blocking, make it non-inheritable to child
304 * processes, disable SIGPIPE, enable "nodelay" (if non-UNIX
305 * stream socket) and check that it is smaller than FD_SETSIZE.
307 * @param h socket to initialize
308 * @param af address family of the socket
309 * @param type socket type
310 * @return #GNUNET_OK on success, #GNUNET_SYSERR if initialization
311 * failed and the handle was destroyed
314 initialize_network_handle (struct GNUNET_NETWORK_Handle *h,
320 if (h->fd == INVALID_SOCKET)
323 SetErrnoFromWinsockError (WSAGetLastError ());
326 return GNUNET_SYSERR;
329 if (h->fd >= FD_SETSIZE)
331 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (h));
333 return GNUNET_SYSERR;
336 if (GNUNET_OK != socket_set_inheritable (h))
337 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
338 "socket_set_inheritable");
340 if (GNUNET_SYSERR == GNUNET_NETWORK_socket_set_blocking (h, GNUNET_NO))
343 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (h));
344 return GNUNET_SYSERR;
347 socket_set_nosigpipe (h);
349 if ( (type == SOCK_STREAM)
354 socket_set_nodelay (h);
360 * accept a new connection on a socket
362 * @param desc bound socket
363 * @param address address of the connecting peer, may be NULL
364 * @param address_len length of @a address
365 * @return client socket
367 struct GNUNET_NETWORK_Handle *
368 GNUNET_NETWORK_socket_accept (const struct GNUNET_NETWORK_Handle *desc,
369 struct sockaddr *address,
370 socklen_t *address_len)
372 struct GNUNET_NETWORK_Handle *ret;
374 ret = GNUNET_new (struct GNUNET_NETWORK_Handle);
377 struct sockaddr name;
378 socklen_t namelen = sizeof (name);
379 int gsn = getsockname (desc->fd,
384 LOG (GNUNET_ERROR_TYPE_DEBUG,
385 "Accepting connection on `%s'\n",
386 GNUNET_a2s (&name, namelen));
389 ret->fd = accept (desc->fd,
398 initialize_network_handle (ret,
399 (NULL != address) ? address->sa_family : desc->af,
407 * Bind a socket to a particular address.
409 * @param desc socket to bind
410 * @param address address to be bound
411 * @param address_len length of @a address
412 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
415 GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc,
416 const struct sockaddr *address,
417 socklen_t address_len)
426 if (AF_INET6 == desc->af)
427 if (setsockopt (desc->fd, IPPROTO_IPV6, IPV6_V6ONLY,
430 LOG_STRERROR (GNUNET_ERROR_TYPE_DEBUG,
439 /* This is required here for TCP sockets, but only on UNIX */
440 if ( (SOCK_STREAM == desc->type) &&
441 (0 != setsockopt (desc->fd,
445 LOG_STRERROR (GNUNET_ERROR_TYPE_DEBUG,
451 /* set permissions of newly created non-abstract UNIX domain socket to
452 "user-only"; applications can choose to relax this later */
453 mode_t old_mask = 0; /* assigned to make compiler happy */
454 const struct sockaddr_un *un;
455 int not_abstract = 0;
457 if ((AF_UNIX == address->sa_family)
458 && (NULL != (un = (const struct sockaddr_un *) address)->sun_path)
459 && ('\0' != un->sun_path[0]) ) /* Not an abstract socket */
462 old_mask = umask (S_IWGRP | S_IRGRP | S_IXGRP | S_IWOTH | S_IROTH | S_IXOTH);
465 ret = bind (desc->fd, address, address_len);
468 (void) umask (old_mask);
472 if (SOCKET_ERROR == ret)
473 SetErrnoFromWinsockError (WSAGetLastError ());
476 return GNUNET_SYSERR;
478 desc->addr = GNUNET_malloc (address_len);
479 memcpy (desc->addr, address, address_len);
480 desc->addrlen = address_len;
490 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
493 GNUNET_NETWORK_socket_close (struct GNUNET_NETWORK_Handle *desc)
501 ret = closesocket (desc->fd);
502 error = WSAGetLastError ();
503 SetErrnoFromWinsockError (error);
504 LOG (GNUNET_ERROR_TYPE_DEBUG,
505 "Closed 0x%x, closesocket() returned %d, GLE is %u\n",
510 ret = close (desc->fd);
513 const struct sockaddr_un *un;
515 /* Cleanup the UNIX domain socket and its parent directories in case of non
517 if ( (AF_UNIX == desc->af) &&
518 (NULL != desc->addr) &&
519 (NULL != (un = (const struct sockaddr_un *) desc->addr)->sun_path) &&
520 ('\0' != un->sun_path[0]) )
522 char *dirname = GNUNET_strndup (un->sun_path,
523 sizeof (un->sun_path));
525 if (0 != unlink (dirname))
527 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING,
535 len = strlen (dirname);
536 while ((len > 0) && (dirname[len] != DIR_SEPARATOR))
539 if ((0 != len) && (0 != rmdir (dirname)))
546 /* these are normal and can just be ignored */
549 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
556 GNUNET_free (dirname);
559 GNUNET_NETWORK_socket_free_memory_only_ (desc);
560 return (ret == 0) ? GNUNET_OK : GNUNET_SYSERR;
565 * Only free memory of a socket, keep the file descriptor untouched.
570 GNUNET_NETWORK_socket_free_memory_only_ (struct GNUNET_NETWORK_Handle *desc)
572 GNUNET_free_non_null (desc->addr);
578 * Box a native socket (and check that it is a socket).
580 * @param fd socket to box
581 * @return NULL on error (including not supported on target platform)
583 struct GNUNET_NETWORK_Handle *
584 GNUNET_NETWORK_socket_box_native (SOCKTYPE fd)
586 struct GNUNET_NETWORK_Handle *ret;
590 /* FIXME: Find a better call to check that FD is valid */
592 WSAIoctl (fd, FIONBIO,
593 (void *) &i, sizeof (i),
596 return NULL; /* invalid FD */
597 ret = GNUNET_new (struct GNUNET_NETWORK_Handle);
602 if (fcntl (fd, F_GETFD) < 0)
603 return NULL; /* invalid FD */
604 ret = GNUNET_new (struct GNUNET_NETWORK_Handle);
613 * Connect a socket to some remote address.
616 * @param address peer address
617 * @param address_len length of @a address
618 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
621 GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Handle *desc,
622 const struct sockaddr *address,
623 socklen_t address_len)
627 ret = connect (desc->fd,
631 if (SOCKET_ERROR == ret)
633 SetErrnoFromWinsockError (WSAGetLastError ());
634 if (errno == EWOULDBLOCK)
638 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
646 * @param level protocol level of the option
647 * @param optname identifier of the option
648 * @param optval options
649 * @param optlen length of @a optval
650 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
653 GNUNET_NETWORK_socket_getsockopt (const struct GNUNET_NETWORK_Handle *desc,
661 ret = getsockopt (desc->fd,
668 (SOL_SOCKET == level) &&
669 (SO_ERROR == optname) )
670 *((int *) optval) = GetErrnoFromWinsockError (*((int *) optval));
671 else if (SOCKET_ERROR == ret)
672 SetErrnoFromWinsockError (WSAGetLastError ());
674 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
682 * @param backlog length of the listen queue
683 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
686 GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Handle *desc,
691 ret = listen (desc->fd,
694 if (SOCKET_ERROR == ret)
695 SetErrnoFromWinsockError (WSAGetLastError ());
697 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
702 * How much data is available to be read on this descriptor?
705 * @returns #GNUNET_NO if no data is available, or on error!
708 GNUNET_NETWORK_socket_recvfrom_amount (const struct GNUNET_NETWORK_Handle *desc)
712 /* How much is there to be read? */
716 error = ioctl (desc->fd,
720 return (ssize_t) pending;
725 error = ioctlsocket (desc->fd,
728 if (error != SOCKET_ERROR)
729 return (ssize_t) pending;
736 * Read data from a socket (always non-blocking).
739 * @param buffer buffer
740 * @param length length of @a buffer
741 * @param src_addr either the source to recv from, or all zeroes
742 * to be filled in by recvfrom
743 * @param addrlen length of the @a src_addr
746 GNUNET_NETWORK_socket_recvfrom (const struct GNUNET_NETWORK_Handle *desc,
749 struct sockaddr *src_addr,
758 flags |= MSG_DONTWAIT;
761 ret = recvfrom (desc->fd,
768 if (SOCKET_ERROR == ret)
769 SetErrnoFromWinsockError (WSAGetLastError ());
776 * Read data from a connected socket (always non-blocking).
779 * @param buffer buffer
780 * @param length length of @a buffer
781 * @return number of bytes received, -1 on error
784 GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Handle *desc,
794 flags |= MSG_DONTWAIT;
796 ret = recv (desc->fd,
801 if (SOCKET_ERROR == ret)
802 SetErrnoFromWinsockError (WSAGetLastError ());
809 * Send data (always non-blocking).
812 * @param buffer data to send
813 * @param length size of the @a buffer
814 * @return number of bytes sent, #GNUNET_SYSERR on error
817 GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Handle *desc,
826 flags |= MSG_DONTWAIT;
830 flags |= MSG_NOSIGNAL;
833 ret = send (desc->fd,
838 if (SOCKET_ERROR == ret)
839 SetErrnoFromWinsockError (WSAGetLastError ());
847 * Send data to a particular destination (always non-blocking).
848 * This function only works for UDP sockets.
851 * @param message data to send
852 * @param length size of the @a message
853 * @param dest_addr destination address
854 * @param dest_len length of @a address
855 * @return number of bytes sent, #GNUNET_SYSERR on error
858 GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Handle *desc,
861 const struct sockaddr *dest_addr,
870 flags |= MSG_DONTWAIT;
873 flags |= MSG_NOSIGNAL;
875 ret = sendto (desc->fd, message, length, flags, dest_addr, dest_len);
877 if (SOCKET_ERROR == ret)
878 SetErrnoFromWinsockError (WSAGetLastError ());
888 * @param level protocol level of the option
889 * @param option_name option identifier
890 * @param option_value value to set
891 * @param option_len size of @a option_value
892 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
895 GNUNET_NETWORK_socket_setsockopt (struct GNUNET_NETWORK_Handle *fd,
898 const void *option_value,
899 socklen_t option_len)
903 ret = setsockopt (fd->fd,
909 if (SOCKET_ERROR == ret)
910 SetErrnoFromWinsockError (WSAGetLastError ());
912 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
917 * Create a new socket. Configure it for non-blocking IO and
918 * mark it as non-inheritable to child processes (set the
919 * close-on-exec flag).
921 * @param domain domain of the socket
922 * @param type socket type
923 * @param protocol network protocol
924 * @return new socket, NULL on error
926 struct GNUNET_NETWORK_Handle *
927 GNUNET_NETWORK_socket_create (int domain,
931 struct GNUNET_NETWORK_Handle *ret;
933 ret = GNUNET_new (struct GNUNET_NETWORK_Handle);
934 ret->fd = socket (domain, type, protocol);
941 initialize_network_handle (ret,
950 * Shut down socket operations
952 * @param how type of shutdown
953 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
956 GNUNET_NETWORK_socket_shutdown (struct GNUNET_NETWORK_Handle *desc,
961 ret = shutdown (desc->fd, how);
964 SetErrnoFromWinsockError (WSAGetLastError ());
966 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
971 * Disable the "CORK" feature for communication with the given socket,
972 * forcing the OS to immediately flush the buffer on transmission
973 * instead of potentially buffering multiple messages. Essentially
974 * reduces the OS send buffers to zero.
977 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
980 GNUNET_NETWORK_socket_disable_corking (struct GNUNET_NETWORK_Handle *desc)
989 setsockopt (desc->fd,
994 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
998 setsockopt (desc->fd,
1003 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
1010 setsockopt (desc->fd,
1015 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
1019 setsockopt (desc->fd,
1024 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
1027 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
1037 GNUNET_NETWORK_fdset_zero (struct GNUNET_NETWORK_FDSet *fds)
1039 FD_ZERO (&fds->sds);
1042 fds->handles_pos = 0;
1048 * Add a socket to the FD set
1051 * @param desc socket to add
1054 GNUNET_NETWORK_fdset_set (struct GNUNET_NETWORK_FDSet *fds,
1055 const struct GNUNET_NETWORK_Handle *desc)
1059 fds->nsds = GNUNET_MAX (fds->nsds,
1065 * Check whether a socket is part of the fd set
1068 * @param desc socket
1069 * @return 0 if the FD is not set
1072 GNUNET_NETWORK_fdset_isset (const struct GNUNET_NETWORK_FDSet *fds,
1073 const struct GNUNET_NETWORK_Handle *desc)
1075 return FD_ISSET (desc->fd,
1081 * Add one fd set to another
1083 * @param dst the fd set to add to
1084 * @param src the fd set to add from
1087 GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst,
1088 const struct GNUNET_NETWORK_FDSet *src)
1093 for (nfds = src->nsds; nfds >= 0; nfds--)
1094 if (FD_ISSET (nfds, &src->sds))
1095 FD_SET (nfds, &dst->sds);
1096 dst->nsds = GNUNET_MAX (dst->nsds,
1099 /* This is MinGW32-specific implementation that relies on the code that
1100 * winsock2.h defines for FD_SET. Namely, it relies on FD_SET checking
1101 * that fd being added is not already in the set.
1102 * Also relies on us knowing what's inside fd_set (fd_count and fd_array).
1104 * NOTE: I don't understand why the UNIX-logic wouldn't work
1105 * for the first part here as well. -CG
1109 for (i = 0; i < src->sds.fd_count; i++)
1110 FD_SET (src->sds.fd_array[i],
1112 dst->nsds = GNUNET_MAX (src->nsds,
1115 /* also copy over `struct GNUNET_DISK_FileHandle` array */
1116 if (dst->handles_pos + src->handles_pos > dst->handles_size)
1117 GNUNET_array_grow (dst->handles,
1119 ((dst->handles_pos + src->handles_pos) << 1));
1120 for (i = 0; i < src->handles_pos; i++)
1121 dst->handles[dst->handles_pos++] = src->handles[i];
1127 * Copy one fd set to another
1129 * @param to destination
1130 * @param from source
1133 GNUNET_NETWORK_fdset_copy (struct GNUNET_NETWORK_FDSet *to,
1134 const struct GNUNET_NETWORK_FDSet *from)
1136 FD_COPY (&from->sds,
1138 to->nsds = from->nsds;
1140 if (from->handles_pos > to->handles_size)
1141 GNUNET_array_grow (to->handles,
1143 from->handles_pos * 2);
1144 memcpy (to->handles,
1146 from->handles_pos * sizeof (struct GNUNET_NETWORK_Handle *));
1147 to->handles_pos = from->handles_pos;
1153 * Return file descriptor for this network handle
1155 * @param desc wrapper to process
1156 * @return POSIX file descriptor
1159 GNUNET_NETWORK_get_fd (struct GNUNET_NETWORK_Handle *desc)
1166 * Return sockaddr for this network handle
1168 * @param desc wrapper to process
1172 GNUNET_NETWORK_get_addr (struct GNUNET_NETWORK_Handle *desc)
1179 * Return sockaddr length for this network handle
1181 * @param desc wrapper to process
1182 * @return socklen_t for sockaddr
1185 GNUNET_NETWORK_get_addrlen (struct GNUNET_NETWORK_Handle *desc)
1187 return desc->addrlen;
1192 * Copy a native fd set
1194 * @param to destination
1195 * @param from native source set
1196 * @param nfds the biggest socket number in from + 1
1199 GNUNET_NETWORK_fdset_copy_native (struct GNUNET_NETWORK_FDSet *to,
1210 * Set a native fd in a set
1212 * @param to destination
1213 * @param nfd native FD to set
1216 GNUNET_NETWORK_fdset_set_native (struct GNUNET_NETWORK_FDSet *to,
1219 GNUNET_assert ((nfd >= 0) && (nfd < FD_SETSIZE));
1220 FD_SET (nfd, &to->sds);
1221 to->nsds = GNUNET_MAX (nfd + 1,
1227 * Test native fd in a set
1229 * @param to set to test, NULL for empty set
1230 * @param nfd native FD to test, or -1 for none
1231 * @return #GNUNET_YES if FD is set in the set
1234 GNUNET_NETWORK_fdset_test_native (const struct GNUNET_NETWORK_FDSet *to,
1240 return FD_ISSET (nfd, &to->sds) ? GNUNET_YES : GNUNET_NO;
1245 * Add a file handle to the fd set
1247 * @param h the file handle to add
1250 GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds,
1251 const struct GNUNET_DISK_FileHandle *h)
1254 if (fds->handles_pos == fds->handles_size)
1255 GNUNET_array_grow (fds->handles,
1257 fds->handles_size * 2 + 2);
1258 fds->handles[fds->handles_pos++] = h;
1262 GNUNET_DISK_internal_file_handle_ (h,
1267 fds->nsds = GNUNET_MAX (fd + 1,
1274 * Check if a file handle is part of an fd set
1277 * @param h file handle
1278 * @return #GNUNET_YES if the file handle is part of the set
1281 GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds,
1282 const struct GNUNET_DISK_FileHandle *h)
1287 for (i=0;i<fds->handles_pos;i++)
1288 if (fds->handles[i] == h)
1292 return FD_ISSET (h->fd,
1300 * Numerically compare pointers to sort them.
1301 * Used to test for overlap in the arrays.
1303 * @param p1 a pointer
1304 * @param p2 a pointer
1305 * @return -1, 0 or 1, if the p1 < p2, p1==p2 or p1 > p2.
1308 ptr_cmp (const void *p1,
1313 if ((intptr_t) p1 < (intptr_t) p2)
1321 * Checks if two fd sets overlap
1323 * @param fds1 first fd set
1324 * @param fds2 second fd set
1325 * @return #GNUNET_YES if they do overlap, #GNUNET_NO otherwise
1328 GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1,
1329 const struct GNUNET_NETWORK_FDSet *fds2)
1334 nfds = GNUNET_MIN (fds1->nsds,
1339 if ( (FD_ISSET (nfds,
1350 /* This code is somewhat hacky, we are not supposed to know what's
1351 * inside of fd_set; also the O(n^2) is really bad... */
1352 for (i = 0; i < fds1->sds.fd_count; i++)
1353 for (j = 0; j < fds2->sds.fd_count; j++)
1354 if (fds1->sds.fd_array[i] == fds2->sds.fd_array[j])
1357 /* take a short cut if possible */
1358 if ( (0 == fds1->handles_pos) ||
1359 (0 == fds2->handles_pos) )
1362 /* Sort file handles array to avoid quadratic complexity when
1363 checking for overlap */
1364 qsort (fds1->handles,
1368 qsort (fds2->handles,
1374 while ( (i < fds1->handles_pos) &&
1375 (j < fds2->handles_pos) )
1377 switch (ptr_cmp (fds1->handles[i],
1397 * @return a new fd set
1399 struct GNUNET_NETWORK_FDSet *
1400 GNUNET_NETWORK_fdset_create ()
1402 struct GNUNET_NETWORK_FDSet *fds;
1404 fds = GNUNET_new (struct GNUNET_NETWORK_FDSet);
1405 GNUNET_NETWORK_fdset_zero (fds);
1411 * Releases the associated memory of an fd set
1416 GNUNET_NETWORK_fdset_destroy (struct GNUNET_NETWORK_FDSet *fds)
1419 GNUNET_array_grow (fds->handles,
1431 struct _select_params
1449 * Timeout for select().
1466 SOCKET wakeup_socket;
1469 * Set to return value from select.
1479 _selector (LPVOID p)
1481 struct _select_params *sp = p;
1485 WaitForSingleObject (sp->standby,
1487 ResetEvent (sp->standby);
1488 sp->status = select (1,
1493 if (FD_ISSET (sp->wakeup_socket,
1496 FD_CLR (sp->wakeup_socket,
1500 SetEvent (sp->wakeup);
1506 static HANDLE hEventPipeWrite;
1508 static HANDLE hEventReadReady;
1510 static struct _select_params sp;
1512 static HANDLE select_thread;
1514 static HANDLE select_finished_event;
1516 static HANDLE select_standby_event;
1518 static SOCKET select_wakeup_socket = -1;
1520 static SOCKET select_send_socket = -1;
1522 static struct timeval select_timeout;
1526 * On W32, we actually use a thread to help with the
1527 * event loop due to W32-API limitations. This function
1528 * initializes that thread.
1531 initialize_select_thread ()
1533 SOCKET select_listening_socket = -1;
1534 struct sockaddr_in s_in;
1539 select_standby_event = CreateEvent (NULL, TRUE, FALSE, NULL);
1540 select_finished_event = CreateEvent (NULL, TRUE, FALSE, NULL);
1542 select_wakeup_socket = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
1544 select_listening_socket = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
1547 res = ioctlsocket (select_wakeup_socket, FIONBIO, &p);
1548 LOG (GNUNET_ERROR_TYPE_DEBUG,
1549 "Select thread initialization: ioctlsocket() returns %d\n",
1552 alen = sizeof (s_in);
1553 s_in.sin_family = AF_INET;
1555 s_in.sin_addr.S_un.S_un_b.s_b1 = 127;
1556 s_in.sin_addr.S_un.S_un_b.s_b2 = 0;
1557 s_in.sin_addr.S_un.S_un_b.s_b3 = 0;
1558 s_in.sin_addr.S_un.S_un_b.s_b4 = 1;
1559 res = bind (select_listening_socket,
1560 (const struct sockaddr *) &s_in,
1562 LOG (GNUNET_ERROR_TYPE_DEBUG,
1563 "Select thread initialization: bind() returns %d\n",
1566 res = getsockname (select_listening_socket,
1567 (struct sockaddr *) &s_in,
1569 LOG (GNUNET_ERROR_TYPE_DEBUG,
1570 "Select thread initialization: getsockname() returns %d\n",
1573 res = listen (select_listening_socket,
1575 LOG (GNUNET_ERROR_TYPE_DEBUG,
1576 "Select thread initialization: listen() returns %d\n",
1578 res = connect (select_wakeup_socket,
1579 (const struct sockaddr *) &s_in,
1581 LOG (GNUNET_ERROR_TYPE_DEBUG,
1582 "Select thread initialization: connect() returns %d\n",
1585 select_send_socket = accept (select_listening_socket,
1586 (struct sockaddr *) &s_in,
1589 closesocket (select_listening_socket);
1591 sp.wakeup = select_finished_event;
1592 sp.standby = select_standby_event;
1593 sp.wakeup_socket = select_wakeup_socket;
1595 select_thread = CreateThread (NULL,
1608 * Check if sockets or pipes meet certain conditions
1610 * @param rfds set of sockets or pipes to be checked for readability
1611 * @param wfds set of sockets or pipes to be checked for writability
1612 * @param efds set of sockets or pipes to be checked for exceptions
1613 * @param timeout relative value when to return
1614 * @return number of selected sockets or pipes, #GNUNET_SYSERR on error
1617 GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
1618 struct GNUNET_NETWORK_FDSet *wfds,
1619 struct GNUNET_NETWORK_FDSet *efds,
1620 const struct GNUNET_TIME_Relative timeout)
1630 nfds = GNUNET_MAX (nfds,
1633 nfds = GNUNET_MAX (nfds,
1636 (timeout.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us))
1639 LOG (GNUNET_ERROR_TYPE_ERROR,
1640 _("Fatal internal logic error, process hangs in `%s' (abort with CTRL-C)!\n"),
1643 tv.tv_sec = timeout.rel_value_us / GNUNET_TIME_UNIT_SECONDS.rel_value_us;
1645 (timeout.rel_value_us -
1646 (tv.tv_sec * GNUNET_TIME_UNIT_SECONDS.rel_value_us));
1647 return select (nfds,
1648 (NULL != rfds) ? &rfds->sds : NULL,
1649 (NULL != wfds) ? &wfds->sds : NULL,
1650 (NULL != efds) ? &efds->sds : NULL,
1651 (timeout.rel_value_us ==
1652 GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us) ? NULL : &tv);
1661 * Non-blocking test if a pipe is ready for reading.
1663 * @param fh pipe handle
1664 * @return #GNUNET_YES if the pipe is ready for reading
1667 pipe_read_ready (struct GNUNET_DISK_FileHandle *fh)
1671 DWORD waitstatus = 0;
1674 bret = PeekNamedPipe (fh->h, NULL, 0, NULL, &waitstatus, NULL);
1675 error = GetLastError ();
1678 /* TODO: either add more errors to this condition, or eliminate it
1679 * entirely (failed to peek -> pipe is in serious trouble, should
1680 * be selected as readable).
1682 if ( (error != ERROR_BROKEN_PIPE) &&
1683 (error != ERROR_INVALID_HANDLE) )
1686 else if (waitstatus <= 0)
1693 * Non-blocking test if a pipe is having an IO exception.
1695 * @param fh pipe handle
1696 * @return #GNUNET_YES if the pipe is having an IO exception.
1699 pipe_except_ready (struct GNUNET_DISK_FileHandle *fh)
1703 if (PeekNamedPipe (fh->h, NULL, 0, NULL, &dwBytes, NULL))
1710 * Iterate over handles in fds, destructively rewrite the
1711 * handles array contents of fds so that it starts with the
1712 * handles that are ready, and update handles_pos accordingly.
1714 * @param fds set of handles (usually pipes) to be checked for readiness
1715 * @param except GNUNET_NO if fds should be checked for readiness to read,
1716 * GNUNET_YES if fds should be checked for exceptions
1717 * (there is no way to check for write-readiness - pipes are always write-ready)
1718 * @return number of ready handles
1721 check_handles_status (struct GNUNET_NETWORK_FDSet *fds, int except)
1723 struct GNUNET_DISK_FileHandle *fh;
1728 for (woff = 0, roff = 0; roff < fds->handles_pos; roff++)
1730 fh = fds->handles[roff];
1731 is_pipe = fh->type == GNUNET_DISK_HANLDE_TYPE_PIPE;
1732 if ((except && is_pipe && pipe_except_ready (fh)) ||
1733 (!except && (!is_pipe || pipe_read_ready (fh))))
1734 fds->handles[woff++] = fh;
1736 fds->handles_pos = woff;
1742 * Check if sockets or pipes meet certain conditions, version for W32.
1744 * @param rfds set of sockets or pipes to be checked for readability
1745 * @param wfds set of sockets or pipes to be checked for writability
1746 * @param efds set of sockets or pipes to be checked for exceptions
1747 * @param timeout relative value when to return
1748 * @return number of selected sockets or pipes, #GNUNET_SYSERR on error
1751 GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
1752 struct GNUNET_NETWORK_FDSet *wfds,
1753 struct GNUNET_NETWORK_FDSet *efds,
1754 const struct GNUNET_TIME_Relative timeout)
1756 struct GNUNET_DISK_FileHandle *fh;
1765 HANDLE handle_array[FD_SETSIZE + 2];
1767 int returnedpos = 0;
1777 nfds = GNUNET_MAX (nfds, rfds->nsds);
1778 handles += rfds->handles_pos;
1782 nfds = GNUNET_MAX (nfds, wfds->nsds);
1783 handles += wfds->handles_pos;
1787 nfds = GNUNET_MAX (nfds, efds->nsds);
1788 handles += efds->handles_pos;
1792 (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == timeout.rel_value_us) &&
1796 LOG (GNUNET_ERROR_TYPE_ERROR,
1797 _("Fatal internal logic error, process hangs in `%s' (abort with CTRL-C)!\n"),
1800 #define SAFE_FD_ISSET(fd, set) (set != NULL && FD_ISSET(fd, set))
1801 /* calculate how long we need to wait in microseconds */
1802 if (timeout.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
1804 mcs_total = INFINITE;
1805 ms_rounded = INFINITE;
1809 mcs_total = timeout.rel_value_us / GNUNET_TIME_UNIT_MICROSECONDS.rel_value_us;
1810 ms_rounded = (DWORD) (mcs_total / GNUNET_TIME_UNIT_MILLISECONDS.rel_value_us);
1811 if (mcs_total > 0 && ms_rounded == 0)
1814 /* select() may be used as a portable way to sleep */
1815 if (! (rfds || wfds || efds))
1821 if (NULL == select_thread)
1822 initialize_select_thread ();
1828 FD_COPY (&rfds->sds, &aread);
1830 FD_COPY (&wfds->sds, &awrite);
1832 FD_COPY (&efds->sds, &aexcept);
1834 /* Start by doing a fast check on sockets and pipes (without
1835 waiting). It is cheap, and is sufficient most of the time. By
1836 profiling we detected that to be true in 90% of the cases.
1839 /* Do the select now */
1840 select_timeout.tv_sec = 0;
1841 select_timeout.tv_usec = 0;
1843 /* Copy all the writes to the except, so we can detect connect() errors */
1844 for (i = 0; i < awrite.fd_count; i++)
1845 FD_SET (awrite.fd_array[i],
1847 if ( (aread.fd_count > 0) ||
1848 (awrite.fd_count > 0) ||
1849 (aexcept.fd_count > 0) )
1850 selectret = select (1,
1851 (NULL != rfds) ? &aread : NULL,
1852 (NULL != wfds) ? &awrite : NULL,
1857 if (-1 == selectret)
1859 /* Throw an error early on, while we still have the context. */
1860 LOG (GNUNET_ERROR_TYPE_ERROR,
1861 "W32 select(%d, %d, %d) failed: %lu\n",
1862 rfds ? aread.fd_count : 0,
1863 wfds ? awrite.fd_count : 0,
1869 /* Check aexcept, if something is in there and we copied that
1870 FD before to detect connect() errors, add it back to the
1871 write set to report errors. */
1873 for (i = 0; i < aexcept.fd_count; i++)
1874 if (FD_ISSET (aexcept.fd_array[i],
1876 FD_SET (aexcept.fd_array[i],
1880 /* If our select returned something or is a 0-timed request, then
1881 also check the pipes and get out of here! */
1882 /* Sadly, it means code duplication :( */
1883 if ( (selectret > 0) || (0 == mcs_total) )
1888 if (rfds && (rfds->handles_pos > 0))
1889 retcode += check_handles_status (rfds, GNUNET_NO);
1891 /* wfds handles remain untouched, on W32
1892 we pretend our pipes are "always" write-ready */
1895 if (efds && (efds->handles_pos > 0))
1896 retcode += check_handles_status (efds, GNUNET_YES);
1900 GNUNET_NETWORK_fdset_zero (rfds);
1901 if (selectret != -1)
1902 GNUNET_NETWORK_fdset_copy_native (rfds, &aread, selectret);
1906 GNUNET_NETWORK_fdset_zero (wfds);
1907 if (selectret != -1)
1908 GNUNET_NETWORK_fdset_copy_native (wfds, &awrite, selectret);
1912 GNUNET_NETWORK_fdset_zero (efds);
1913 if (selectret != -1)
1914 GNUNET_NETWORK_fdset_copy_native (efds, &aexcept, selectret);
1916 if (-1 == selectret)
1918 /* Add our select() FDs to the total return value */
1919 retcode += selectret;
1923 /* If we got this far, use slower implementation that is able to do a waiting select
1924 on both sockets and pipes simultaneously */
1926 /* Events for pipes */
1927 if (! hEventReadReady)
1928 hEventReadReady = CreateEvent (NULL, TRUE, TRUE, NULL);
1929 if (! hEventPipeWrite)
1930 hEventPipeWrite = CreateEvent (NULL, TRUE, TRUE, NULL);
1937 FD_COPY (&rfds->sds, &aread);
1939 FD_COPY (&wfds->sds, &awrite);
1941 FD_COPY (&efds->sds, &aexcept);
1942 /* We will first Add the PIPES to the events */
1943 /* Track how far in `handle_array` the read pipes go,
1944 so we may by-pass them quickly if none of them
1947 if (rfds && (rfds->handles_pos > 0))
1949 for (i = 0; i <rfds->handles_pos; i++)
1951 fh = rfds->handles[i];
1952 if (fh->type != GNUNET_DISK_HANLDE_TYPE_PIPE)
1954 /* Read zero bytes to check the status of the pipe */
1955 if (! ReadFile (fh->h, NULL, 0, NULL, fh->oOverlapRead))
1957 DWORD error_code = GetLastError ();
1959 if (error_code == ERROR_IO_PENDING)
1961 /* add as unready */
1962 handle_array[nhandles++] = fh->oOverlapRead->hEvent;
1968 handle_array[nhandles++] = hEventReadReady;
1974 /* error also counts as ready */
1975 handle_array[nhandles++] = hEventReadReady;
1981 if (wfds && (wfds->handles_pos > 0))
1983 LOG (GNUNET_ERROR_TYPE_DEBUG,
1984 "Adding the write ready event to the array as %d\n",
1986 handle_array[nhandles++] = hEventPipeWrite;
1992 LOG (GNUNET_ERROR_TYPE_DEBUG,
1993 "Adding the socket event to the array as %d\n",
1995 handle_array[nhandles++] = select_finished_event;
1996 if (timeout.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
2002 select_timeout.tv_sec = timeout.rel_value_us / GNUNET_TIME_UNIT_SECONDS.rel_value_us;
2003 select_timeout.tv_usec = (timeout.rel_value_us -
2004 (select_timeout.tv_sec *
2005 GNUNET_TIME_UNIT_SECONDS.rel_value_us));
2006 sp.tv = &select_timeout;
2008 FD_SET (select_wakeup_socket, &aread);
2011 i = recv (select_wakeup_socket,
2012 (char *) &returnedpos,
2019 /* Failed connections cause sockets to be set in errorfds on W32,
2020 * but on POSIX it should set them in writefds.
2021 * First copy all awrite sockets to aexcept, later we'll
2022 * check aexcept and set its contents in awrite as well
2023 * Sockets are also set in errorfds when OOB data is available,
2024 * but we don't use OOB data.
2026 for (i = 0; i < awrite.fd_count; i++)
2027 FD_SET (awrite.fd_array[i],
2029 ResetEvent (select_finished_event);
2030 SetEvent (select_standby_event);
2033 /* NULL-terminate array */
2034 handle_array[nhandles] = NULL;
2035 LOG (GNUNET_ERROR_TYPE_DEBUG,
2036 "nfds: %d, handles: %d, will wait: %llu mcs\n",
2043 = WaitForMultipleObjects (nhandles,
2047 LOG (GNUNET_ERROR_TYPE_DEBUG,
2048 "WaitForMultipleObjects Returned: %d\n",
2053 GNUNET_break (0); /* This branch shouldn't actually be executed...*/
2054 i = (int) WaitForSingleObject (select_finished_event,
2056 returncode = WAIT_TIMEOUT;
2060 /* Shouldn't come this far. If it does - investigate. */
2066 /* Don't wake up select-thread when delay is 0, it should return immediately
2067 * and wake up by itself.
2070 i = send (select_send_socket,
2071 (const char *) &returnedpos,
2074 i = (int) WaitForSingleObject (select_finished_event,
2076 LOG (GNUNET_ERROR_TYPE_DEBUG,
2077 "Finished waiting for the select thread: %d %d\n",
2084 i = recv (select_wakeup_socket,
2085 (char *) &returnedpos,
2089 /* Check aexcept, add its contents to awrite */
2090 for (i = 0; i < aexcept.fd_count; i++)
2091 FD_SET (aexcept.fd_array[i], &awrite);
2094 returnedpos = returncode - WAIT_OBJECT_0;
2095 LOG (GNUNET_ERROR_TYPE_DEBUG,
2096 "return pos is: %d\n",
2101 /* We queued a zero-long read on each pipe to check
2102 * its state, now we must cancel these read operations.
2103 * This must be done while rfds->handles_pos is still
2104 * intact and matches the number of read handles that we
2105 * got from the caller.
2107 for (i = 0; i < rfds->handles_pos; i++)
2109 fh = rfds->handles[i];
2110 if (GNUNET_DISK_HANLDE_TYPE_PIPE == fh->type)
2114 /* We may have some pipes ready for reading. */
2115 if (returnedpos < read_pipes_off)
2116 retcode += check_handles_status (rfds, GNUNET_NO);
2118 rfds->handles_pos = 0;
2120 if (-1 != sp.status)
2121 GNUNET_NETWORK_fdset_copy_native (rfds, &aread, retcode);
2125 retcode += wfds->handles_pos;
2126 /* wfds handles remain untouched */
2127 if (-1 != sp.status)
2128 GNUNET_NETWORK_fdset_copy_native (wfds, &awrite, retcode);
2132 retcode += check_handles_status (rfds, GNUNET_YES);
2133 if (-1 != sp.status)
2134 GNUNET_NETWORK_fdset_copy_native (efds, &aexcept, retcode);
2138 retcode += sp.status;
2146 /* end of network.c */