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_storage name;
378 socklen_t namelen = sizeof (name);
380 int gsn = getsockname (desc->fd,
381 (struct sockaddr *) &name,
385 LOG (GNUNET_ERROR_TYPE_DEBUG,
386 "Accepting connection on `%s'\n",
387 GNUNET_a2s ((const struct sockaddr *) &name,
391 ret->fd = accept (desc->fd,
400 initialize_network_handle (ret,
401 (NULL != address) ? address->sa_family : desc->af,
409 * Bind a socket to a particular address.
411 * @param desc socket to bind
412 * @param address address to be bound
413 * @param address_len length of @a address
414 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
417 GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc,
418 const struct sockaddr *address,
419 socklen_t address_len)
428 if (AF_INET6 == desc->af)
429 if (setsockopt (desc->fd, IPPROTO_IPV6, IPV6_V6ONLY,
432 LOG_STRERROR (GNUNET_ERROR_TYPE_DEBUG,
441 /* This is required here for TCP sockets, but only on UNIX */
442 if ( (SOCK_STREAM == desc->type) &&
443 (0 != setsockopt (desc->fd,
447 LOG_STRERROR (GNUNET_ERROR_TYPE_DEBUG,
453 /* set permissions of newly created non-abstract UNIX domain socket to
454 "user-only"; applications can choose to relax this later */
455 mode_t old_mask = 0; /* assigned to make compiler happy */
456 const struct sockaddr_un *un;
457 int not_abstract = 0;
459 if ((AF_UNIX == address->sa_family)
460 && (NULL != (un = (const struct sockaddr_un *) address)->sun_path)
461 && ('\0' != un->sun_path[0]) ) /* Not an abstract socket */
464 old_mask = umask (S_IWGRP | S_IRGRP | S_IXGRP | S_IWOTH | S_IROTH | S_IXOTH);
467 ret = bind (desc->fd, address, address_len);
470 (void) umask (old_mask);
474 if (SOCKET_ERROR == ret)
475 SetErrnoFromWinsockError (WSAGetLastError ());
478 return GNUNET_SYSERR;
480 desc->addr = GNUNET_malloc (address_len);
481 memcpy (desc->addr, address, address_len);
482 desc->addrlen = address_len;
492 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
495 GNUNET_NETWORK_socket_close (struct GNUNET_NETWORK_Handle *desc)
503 ret = closesocket (desc->fd);
504 error = WSAGetLastError ();
505 SetErrnoFromWinsockError (error);
506 LOG (GNUNET_ERROR_TYPE_DEBUG,
507 "Closed 0x%x, closesocket() returned %d, GLE is %u\n",
512 ret = close (desc->fd);
515 const struct sockaddr_un *un;
517 /* Cleanup the UNIX domain socket and its parent directories in case of non
519 if ( (AF_UNIX == desc->af) &&
520 (NULL != desc->addr) &&
521 (NULL != (un = (const struct sockaddr_un *) desc->addr)->sun_path) &&
522 ('\0' != un->sun_path[0]) )
524 char *dirname = GNUNET_strndup (un->sun_path,
525 sizeof (un->sun_path));
527 if (0 != unlink (dirname))
529 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING,
537 len = strlen (dirname);
538 while ((len > 0) && (dirname[len] != DIR_SEPARATOR))
541 if ((0 != len) && (0 != rmdir (dirname)))
548 /* these are normal and can just be ignored */
551 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
558 GNUNET_free (dirname);
561 GNUNET_NETWORK_socket_free_memory_only_ (desc);
562 return (ret == 0) ? GNUNET_OK : GNUNET_SYSERR;
567 * Only free memory of a socket, keep the file descriptor untouched.
572 GNUNET_NETWORK_socket_free_memory_only_ (struct GNUNET_NETWORK_Handle *desc)
574 GNUNET_free_non_null (desc->addr);
580 * Box a native socket (and check that it is a socket).
582 * @param fd socket to box
583 * @return NULL on error (including not supported on target platform)
585 struct GNUNET_NETWORK_Handle *
586 GNUNET_NETWORK_socket_box_native (SOCKTYPE fd)
588 struct GNUNET_NETWORK_Handle *ret;
592 /* FIXME: Find a better call to check that FD is valid */
594 WSAIoctl (fd, FIONBIO,
595 (void *) &i, sizeof (i),
598 return NULL; /* invalid FD */
599 ret = GNUNET_new (struct GNUNET_NETWORK_Handle);
604 if (fcntl (fd, F_GETFD) < 0)
605 return NULL; /* invalid FD */
606 ret = GNUNET_new (struct GNUNET_NETWORK_Handle);
615 * Connect a socket to some remote address.
618 * @param address peer address
619 * @param address_len length of @a address
620 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
623 GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Handle *desc,
624 const struct sockaddr *address,
625 socklen_t address_len)
629 ret = connect (desc->fd,
633 if (SOCKET_ERROR == ret)
635 SetErrnoFromWinsockError (WSAGetLastError ());
636 if (errno == EWOULDBLOCK)
640 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
648 * @param level protocol level of the option
649 * @param optname identifier of the option
650 * @param optval options
651 * @param optlen length of @a optval
652 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
655 GNUNET_NETWORK_socket_getsockopt (const struct GNUNET_NETWORK_Handle *desc,
663 ret = getsockopt (desc->fd,
670 (SOL_SOCKET == level) &&
671 (SO_ERROR == optname) )
672 *((int *) optval) = GetErrnoFromWinsockError (*((int *) optval));
673 else if (SOCKET_ERROR == ret)
674 SetErrnoFromWinsockError (WSAGetLastError ());
676 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
684 * @param backlog length of the listen queue
685 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
688 GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Handle *desc,
693 ret = listen (desc->fd,
696 if (SOCKET_ERROR == ret)
697 SetErrnoFromWinsockError (WSAGetLastError ());
699 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
704 * How much data is available to be read on this descriptor?
707 * @returns #GNUNET_NO if no data is available, or on error!
710 GNUNET_NETWORK_socket_recvfrom_amount (const struct GNUNET_NETWORK_Handle *desc)
714 /* How much is there to be read? */
718 error = ioctl (desc->fd,
722 return (ssize_t) pending;
727 error = ioctlsocket (desc->fd,
730 if (error != SOCKET_ERROR)
731 return (ssize_t) pending;
738 * Read data from a socket (always non-blocking).
741 * @param buffer buffer
742 * @param length length of @a buffer
743 * @param src_addr either the source to recv from, or all zeroes
744 * to be filled in by recvfrom
745 * @param addrlen length of the @a src_addr
748 GNUNET_NETWORK_socket_recvfrom (const struct GNUNET_NETWORK_Handle *desc,
751 struct sockaddr *src_addr,
760 flags |= MSG_DONTWAIT;
763 ret = recvfrom (desc->fd,
770 if (SOCKET_ERROR == ret)
771 SetErrnoFromWinsockError (WSAGetLastError ());
778 * Read data from a connected socket (always non-blocking).
781 * @param buffer buffer
782 * @param length length of @a buffer
783 * @return number of bytes received, -1 on error
786 GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Handle *desc,
796 flags |= MSG_DONTWAIT;
798 ret = recv (desc->fd,
803 if (SOCKET_ERROR == ret)
804 SetErrnoFromWinsockError (WSAGetLastError ());
811 * Send data (always non-blocking).
814 * @param buffer data to send
815 * @param length size of the @a buffer
816 * @return number of bytes sent, #GNUNET_SYSERR on error
819 GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Handle *desc,
828 flags |= MSG_DONTWAIT;
832 flags |= MSG_NOSIGNAL;
835 ret = send (desc->fd,
840 if (SOCKET_ERROR == ret)
841 SetErrnoFromWinsockError (WSAGetLastError ());
849 * Send data to a particular destination (always non-blocking).
850 * This function only works for UDP sockets.
853 * @param message data to send
854 * @param length size of the @a message
855 * @param dest_addr destination address
856 * @param dest_len length of @a address
857 * @return number of bytes sent, #GNUNET_SYSERR on error
860 GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Handle *desc,
863 const struct sockaddr *dest_addr,
872 flags |= MSG_DONTWAIT;
875 flags |= MSG_NOSIGNAL;
877 ret = sendto (desc->fd, message, length, flags, dest_addr, dest_len);
879 if (SOCKET_ERROR == ret)
880 SetErrnoFromWinsockError (WSAGetLastError ());
890 * @param level protocol level of the option
891 * @param option_name option identifier
892 * @param option_value value to set
893 * @param option_len size of @a option_value
894 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
897 GNUNET_NETWORK_socket_setsockopt (struct GNUNET_NETWORK_Handle *fd,
900 const void *option_value,
901 socklen_t option_len)
905 ret = setsockopt (fd->fd,
911 if (SOCKET_ERROR == ret)
912 SetErrnoFromWinsockError (WSAGetLastError ());
914 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
919 * Create a new socket. Configure it for non-blocking IO and
920 * mark it as non-inheritable to child processes (set the
921 * close-on-exec flag).
923 * @param domain domain of the socket
924 * @param type socket type
925 * @param protocol network protocol
926 * @return new socket, NULL on error
928 struct GNUNET_NETWORK_Handle *
929 GNUNET_NETWORK_socket_create (int domain,
933 struct GNUNET_NETWORK_Handle *ret;
935 ret = GNUNET_new (struct GNUNET_NETWORK_Handle);
936 ret->fd = socket (domain, type, protocol);
943 initialize_network_handle (ret,
952 * Shut down socket operations
954 * @param how type of shutdown
955 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
958 GNUNET_NETWORK_socket_shutdown (struct GNUNET_NETWORK_Handle *desc,
963 ret = shutdown (desc->fd, how);
966 SetErrnoFromWinsockError (WSAGetLastError ());
968 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
973 * Disable the "CORK" feature for communication with the given socket,
974 * forcing the OS to immediately flush the buffer on transmission
975 * instead of potentially buffering multiple messages. Essentially
976 * reduces the OS send buffers to zero.
979 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
982 GNUNET_NETWORK_socket_disable_corking (struct GNUNET_NETWORK_Handle *desc)
991 setsockopt (desc->fd,
996 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
1000 setsockopt (desc->fd,
1005 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
1012 setsockopt (desc->fd,
1017 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
1021 setsockopt (desc->fd,
1026 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
1029 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
1039 GNUNET_NETWORK_fdset_zero (struct GNUNET_NETWORK_FDSet *fds)
1041 FD_ZERO (&fds->sds);
1044 fds->handles_pos = 0;
1050 * Add a socket to the FD set
1053 * @param desc socket to add
1056 GNUNET_NETWORK_fdset_set (struct GNUNET_NETWORK_FDSet *fds,
1057 const struct GNUNET_NETWORK_Handle *desc)
1061 fds->nsds = GNUNET_MAX (fds->nsds,
1067 * Check whether a socket is part of the fd set
1070 * @param desc socket
1071 * @return 0 if the FD is not set
1074 GNUNET_NETWORK_fdset_isset (const struct GNUNET_NETWORK_FDSet *fds,
1075 const struct GNUNET_NETWORK_Handle *desc)
1077 return FD_ISSET (desc->fd,
1083 * Add one fd set to another
1085 * @param dst the fd set to add to
1086 * @param src the fd set to add from
1089 GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst,
1090 const struct GNUNET_NETWORK_FDSet *src)
1095 for (nfds = src->nsds; nfds >= 0; nfds--)
1096 if (FD_ISSET (nfds, &src->sds))
1097 FD_SET (nfds, &dst->sds);
1098 dst->nsds = GNUNET_MAX (dst->nsds,
1101 /* This is MinGW32-specific implementation that relies on the code that
1102 * winsock2.h defines for FD_SET. Namely, it relies on FD_SET checking
1103 * that fd being added is not already in the set.
1104 * Also relies on us knowing what's inside fd_set (fd_count and fd_array).
1106 * NOTE: I don't understand why the UNIX-logic wouldn't work
1107 * for the first part here as well. -CG
1111 for (i = 0; i < src->sds.fd_count; i++)
1112 FD_SET (src->sds.fd_array[i],
1114 dst->nsds = GNUNET_MAX (src->nsds,
1117 /* also copy over `struct GNUNET_DISK_FileHandle` array */
1118 if (dst->handles_pos + src->handles_pos > dst->handles_size)
1119 GNUNET_array_grow (dst->handles,
1121 ((dst->handles_pos + src->handles_pos) << 1));
1122 for (i = 0; i < src->handles_pos; i++)
1123 dst->handles[dst->handles_pos++] = src->handles[i];
1129 * Copy one fd set to another
1131 * @param to destination
1132 * @param from source
1135 GNUNET_NETWORK_fdset_copy (struct GNUNET_NETWORK_FDSet *to,
1136 const struct GNUNET_NETWORK_FDSet *from)
1138 FD_COPY (&from->sds,
1140 to->nsds = from->nsds;
1142 if (from->handles_pos > to->handles_size)
1143 GNUNET_array_grow (to->handles,
1145 from->handles_pos * 2);
1146 memcpy (to->handles,
1148 from->handles_pos * sizeof (struct GNUNET_NETWORK_Handle *));
1149 to->handles_pos = from->handles_pos;
1155 * Return file descriptor for this network handle
1157 * @param desc wrapper to process
1158 * @return POSIX file descriptor
1161 GNUNET_NETWORK_get_fd (struct GNUNET_NETWORK_Handle *desc)
1168 * Return sockaddr for this network handle
1170 * @param desc wrapper to process
1174 GNUNET_NETWORK_get_addr (struct GNUNET_NETWORK_Handle *desc)
1181 * Return sockaddr length for this network handle
1183 * @param desc wrapper to process
1184 * @return socklen_t for sockaddr
1187 GNUNET_NETWORK_get_addrlen (struct GNUNET_NETWORK_Handle *desc)
1189 return desc->addrlen;
1194 * Copy a native fd set
1196 * @param to destination
1197 * @param from native source set
1198 * @param nfds the biggest socket number in from + 1
1201 GNUNET_NETWORK_fdset_copy_native (struct GNUNET_NETWORK_FDSet *to,
1212 * Set a native fd in a set
1214 * @param to destination
1215 * @param nfd native FD to set
1218 GNUNET_NETWORK_fdset_set_native (struct GNUNET_NETWORK_FDSet *to,
1221 GNUNET_assert ((nfd >= 0) && (nfd < FD_SETSIZE));
1222 FD_SET (nfd, &to->sds);
1223 to->nsds = GNUNET_MAX (nfd + 1,
1229 * Test native fd in a set
1231 * @param to set to test, NULL for empty set
1232 * @param nfd native FD to test, or -1 for none
1233 * @return #GNUNET_YES if FD is set in the set
1236 GNUNET_NETWORK_fdset_test_native (const struct GNUNET_NETWORK_FDSet *to,
1242 return FD_ISSET (nfd, &to->sds) ? GNUNET_YES : GNUNET_NO;
1247 * Add a file handle to the fd set
1249 * @param h the file handle to add
1252 GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds,
1253 const struct GNUNET_DISK_FileHandle *h)
1256 if (fds->handles_pos == fds->handles_size)
1257 GNUNET_array_grow (fds->handles,
1259 fds->handles_size * 2 + 2);
1260 fds->handles[fds->handles_pos++] = h;
1264 GNUNET_DISK_internal_file_handle_ (h,
1269 fds->nsds = GNUNET_MAX (fd + 1,
1276 * Check if a file handle is part of an fd set
1279 * @param h file handle
1280 * @return #GNUNET_YES if the file handle is part of the set
1283 GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds,
1284 const struct GNUNET_DISK_FileHandle *h)
1289 for (i=0;i<fds->handles_pos;i++)
1290 if (fds->handles[i] == h)
1294 return FD_ISSET (h->fd,
1302 * Numerically compare pointers to sort them.
1303 * Used to test for overlap in the arrays.
1305 * @param p1 a pointer
1306 * @param p2 a pointer
1307 * @return -1, 0 or 1, if the p1 < p2, p1==p2 or p1 > p2.
1310 ptr_cmp (const void *p1,
1315 if ((intptr_t) p1 < (intptr_t) p2)
1323 * Checks if two fd sets overlap
1325 * @param fds1 first fd set
1326 * @param fds2 second fd set
1327 * @return #GNUNET_YES if they do overlap, #GNUNET_NO otherwise
1330 GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1,
1331 const struct GNUNET_NETWORK_FDSet *fds2)
1336 nfds = GNUNET_MIN (fds1->nsds,
1341 if ( (FD_ISSET (nfds,
1352 /* This code is somewhat hacky, we are not supposed to know what's
1353 * inside of fd_set; also the O(n^2) is really bad... */
1354 for (i = 0; i < fds1->sds.fd_count; i++)
1355 for (j = 0; j < fds2->sds.fd_count; j++)
1356 if (fds1->sds.fd_array[i] == fds2->sds.fd_array[j])
1359 /* take a short cut if possible */
1360 if ( (0 == fds1->handles_pos) ||
1361 (0 == fds2->handles_pos) )
1364 /* Sort file handles array to avoid quadratic complexity when
1365 checking for overlap */
1366 qsort (fds1->handles,
1370 qsort (fds2->handles,
1376 while ( (i < fds1->handles_pos) &&
1377 (j < fds2->handles_pos) )
1379 switch (ptr_cmp (fds1->handles[i],
1399 * @return a new fd set
1401 struct GNUNET_NETWORK_FDSet *
1402 GNUNET_NETWORK_fdset_create ()
1404 struct GNUNET_NETWORK_FDSet *fds;
1406 fds = GNUNET_new (struct GNUNET_NETWORK_FDSet);
1407 GNUNET_NETWORK_fdset_zero (fds);
1413 * Releases the associated memory of an fd set
1418 GNUNET_NETWORK_fdset_destroy (struct GNUNET_NETWORK_FDSet *fds)
1421 GNUNET_array_grow (fds->handles,
1433 struct _select_params
1451 * Timeout for select().
1468 SOCKET wakeup_socket;
1471 * Set to return value from select.
1481 _selector (LPVOID p)
1483 struct _select_params *sp = p;
1487 WaitForSingleObject (sp->standby,
1489 ResetEvent (sp->standby);
1490 sp->status = select (1,
1495 if (FD_ISSET (sp->wakeup_socket,
1498 FD_CLR (sp->wakeup_socket,
1502 SetEvent (sp->wakeup);
1508 static HANDLE hEventPipeWrite;
1510 static HANDLE hEventReadReady;
1512 static struct _select_params sp;
1514 static HANDLE select_thread;
1516 static HANDLE select_finished_event;
1518 static HANDLE select_standby_event;
1520 static SOCKET select_wakeup_socket = -1;
1522 static SOCKET select_send_socket = -1;
1524 static struct timeval select_timeout;
1528 * On W32, we actually use a thread to help with the
1529 * event loop due to W32-API limitations. This function
1530 * initializes that thread.
1533 initialize_select_thread ()
1535 SOCKET select_listening_socket = -1;
1536 struct sockaddr_in s_in;
1541 select_standby_event = CreateEvent (NULL, TRUE, FALSE, NULL);
1542 select_finished_event = CreateEvent (NULL, TRUE, FALSE, NULL);
1544 select_wakeup_socket = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
1546 select_listening_socket = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
1549 res = ioctlsocket (select_wakeup_socket, FIONBIO, &p);
1550 LOG (GNUNET_ERROR_TYPE_DEBUG,
1551 "Select thread initialization: ioctlsocket() returns %d\n",
1554 alen = sizeof (s_in);
1555 s_in.sin_family = AF_INET;
1557 s_in.sin_addr.S_un.S_un_b.s_b1 = 127;
1558 s_in.sin_addr.S_un.S_un_b.s_b2 = 0;
1559 s_in.sin_addr.S_un.S_un_b.s_b3 = 0;
1560 s_in.sin_addr.S_un.S_un_b.s_b4 = 1;
1561 res = bind (select_listening_socket,
1562 (const struct sockaddr *) &s_in,
1564 LOG (GNUNET_ERROR_TYPE_DEBUG,
1565 "Select thread initialization: bind() returns %d\n",
1568 res = getsockname (select_listening_socket,
1569 (struct sockaddr *) &s_in,
1571 LOG (GNUNET_ERROR_TYPE_DEBUG,
1572 "Select thread initialization: getsockname() returns %d\n",
1575 res = listen (select_listening_socket,
1577 LOG (GNUNET_ERROR_TYPE_DEBUG,
1578 "Select thread initialization: listen() returns %d\n",
1580 res = connect (select_wakeup_socket,
1581 (const struct sockaddr *) &s_in,
1583 LOG (GNUNET_ERROR_TYPE_DEBUG,
1584 "Select thread initialization: connect() returns %d\n",
1587 select_send_socket = accept (select_listening_socket,
1588 (struct sockaddr *) &s_in,
1591 closesocket (select_listening_socket);
1593 sp.wakeup = select_finished_event;
1594 sp.standby = select_standby_event;
1595 sp.wakeup_socket = select_wakeup_socket;
1597 select_thread = CreateThread (NULL,
1610 * Check if sockets or pipes meet certain conditions
1612 * @param rfds set of sockets or pipes to be checked for readability
1613 * @param wfds set of sockets or pipes to be checked for writability
1614 * @param efds set of sockets or pipes to be checked for exceptions
1615 * @param timeout relative value when to return
1616 * @return number of selected sockets or pipes, #GNUNET_SYSERR on error
1619 GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
1620 struct GNUNET_NETWORK_FDSet *wfds,
1621 struct GNUNET_NETWORK_FDSet *efds,
1622 const struct GNUNET_TIME_Relative timeout)
1632 nfds = GNUNET_MAX (nfds,
1635 nfds = GNUNET_MAX (nfds,
1638 (timeout.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us))
1641 LOG (GNUNET_ERROR_TYPE_ERROR,
1642 _("Fatal internal logic error, process hangs in `%s' (abort with CTRL-C)!\n"),
1645 tv.tv_sec = timeout.rel_value_us / GNUNET_TIME_UNIT_SECONDS.rel_value_us;
1647 (timeout.rel_value_us -
1648 (tv.tv_sec * GNUNET_TIME_UNIT_SECONDS.rel_value_us));
1649 return select (nfds,
1650 (NULL != rfds) ? &rfds->sds : NULL,
1651 (NULL != wfds) ? &wfds->sds : NULL,
1652 (NULL != efds) ? &efds->sds : NULL,
1653 (timeout.rel_value_us ==
1654 GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us) ? NULL : &tv);
1663 * Non-blocking test if a pipe is ready for reading.
1665 * @param fh pipe handle
1666 * @return #GNUNET_YES if the pipe is ready for reading
1669 pipe_read_ready (struct GNUNET_DISK_FileHandle *fh)
1673 DWORD waitstatus = 0;
1676 bret = PeekNamedPipe (fh->h, NULL, 0, NULL, &waitstatus, NULL);
1677 error = GetLastError ();
1680 /* TODO: either add more errors to this condition, or eliminate it
1681 * entirely (failed to peek -> pipe is in serious trouble, should
1682 * be selected as readable).
1684 if ( (error != ERROR_BROKEN_PIPE) &&
1685 (error != ERROR_INVALID_HANDLE) )
1688 else if (waitstatus <= 0)
1695 * Non-blocking test if a pipe is having an IO exception.
1697 * @param fh pipe handle
1698 * @return #GNUNET_YES if the pipe is having an IO exception.
1701 pipe_except_ready (struct GNUNET_DISK_FileHandle *fh)
1705 if (PeekNamedPipe (fh->h, NULL, 0, NULL, &dwBytes, NULL))
1712 * Iterate over handles in fds, destructively rewrite the
1713 * handles array contents of fds so that it starts with the
1714 * handles that are ready, and update handles_pos accordingly.
1716 * @param fds set of handles (usually pipes) to be checked for readiness
1717 * @param except GNUNET_NO if fds should be checked for readiness to read,
1718 * GNUNET_YES if fds should be checked for exceptions
1719 * (there is no way to check for write-readiness - pipes are always write-ready)
1720 * @return number of ready handles
1723 check_handles_status (struct GNUNET_NETWORK_FDSet *fds, int except)
1725 struct GNUNET_DISK_FileHandle *fh;
1730 for (woff = 0, roff = 0; roff < fds->handles_pos; roff++)
1732 fh = fds->handles[roff];
1733 is_pipe = fh->type == GNUNET_DISK_HANLDE_TYPE_PIPE;
1734 if ((except && is_pipe && pipe_except_ready (fh)) ||
1735 (!except && (!is_pipe || pipe_read_ready (fh))))
1736 fds->handles[woff++] = fh;
1738 fds->handles_pos = woff;
1744 * Check if sockets or pipes meet certain conditions, version for W32.
1746 * @param rfds set of sockets or pipes to be checked for readability
1747 * @param wfds set of sockets or pipes to be checked for writability
1748 * @param efds set of sockets or pipes to be checked for exceptions
1749 * @param timeout relative value when to return
1750 * @return number of selected sockets or pipes, #GNUNET_SYSERR on error
1753 GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
1754 struct GNUNET_NETWORK_FDSet *wfds,
1755 struct GNUNET_NETWORK_FDSet *efds,
1756 const struct GNUNET_TIME_Relative timeout)
1758 struct GNUNET_DISK_FileHandle *fh;
1767 HANDLE handle_array[FD_SETSIZE + 2];
1769 int returnedpos = 0;
1779 nfds = GNUNET_MAX (nfds, rfds->nsds);
1780 handles += rfds->handles_pos;
1784 nfds = GNUNET_MAX (nfds, wfds->nsds);
1785 handles += wfds->handles_pos;
1789 nfds = GNUNET_MAX (nfds, efds->nsds);
1790 handles += efds->handles_pos;
1794 (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == timeout.rel_value_us) &&
1798 LOG (GNUNET_ERROR_TYPE_ERROR,
1799 _("Fatal internal logic error, process hangs in `%s' (abort with CTRL-C)!\n"),
1802 #define SAFE_FD_ISSET(fd, set) (set != NULL && FD_ISSET(fd, set))
1803 /* calculate how long we need to wait in microseconds */
1804 if (timeout.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
1806 mcs_total = INFINITE;
1807 ms_rounded = INFINITE;
1811 mcs_total = timeout.rel_value_us / GNUNET_TIME_UNIT_MICROSECONDS.rel_value_us;
1812 ms_rounded = (DWORD) (mcs_total / GNUNET_TIME_UNIT_MILLISECONDS.rel_value_us);
1813 if (mcs_total > 0 && ms_rounded == 0)
1816 /* select() may be used as a portable way to sleep */
1817 if (! (rfds || wfds || efds))
1823 if (NULL == select_thread)
1824 initialize_select_thread ();
1830 FD_COPY (&rfds->sds, &aread);
1832 FD_COPY (&wfds->sds, &awrite);
1834 FD_COPY (&efds->sds, &aexcept);
1836 /* Start by doing a fast check on sockets and pipes (without
1837 waiting). It is cheap, and is sufficient most of the time. By
1838 profiling we detected that to be true in 90% of the cases.
1841 /* Do the select now */
1842 select_timeout.tv_sec = 0;
1843 select_timeout.tv_usec = 0;
1845 /* Copy all the writes to the except, so we can detect connect() errors */
1846 for (i = 0; i < awrite.fd_count; i++)
1847 FD_SET (awrite.fd_array[i],
1849 if ( (aread.fd_count > 0) ||
1850 (awrite.fd_count > 0) ||
1851 (aexcept.fd_count > 0) )
1852 selectret = select (1,
1853 (NULL != rfds) ? &aread : NULL,
1854 (NULL != wfds) ? &awrite : NULL,
1859 if (-1 == selectret)
1861 /* Throw an error early on, while we still have the context. */
1862 LOG (GNUNET_ERROR_TYPE_ERROR,
1863 "W32 select(%d, %d, %d) failed: %lu\n",
1864 rfds ? aread.fd_count : 0,
1865 wfds ? awrite.fd_count : 0,
1871 /* Check aexcept, if something is in there and we copied that
1872 FD before to detect connect() errors, add it back to the
1873 write set to report errors. */
1875 for (i = 0; i < aexcept.fd_count; i++)
1876 if (FD_ISSET (aexcept.fd_array[i],
1878 FD_SET (aexcept.fd_array[i],
1882 /* If our select returned something or is a 0-timed request, then
1883 also check the pipes and get out of here! */
1884 /* Sadly, it means code duplication :( */
1885 if ( (selectret > 0) || (0 == mcs_total) )
1890 if (rfds && (rfds->handles_pos > 0))
1891 retcode += check_handles_status (rfds, GNUNET_NO);
1893 /* wfds handles remain untouched, on W32
1894 we pretend our pipes are "always" write-ready */
1897 if (efds && (efds->handles_pos > 0))
1898 retcode += check_handles_status (efds, GNUNET_YES);
1902 GNUNET_NETWORK_fdset_zero (rfds);
1903 if (selectret != -1)
1904 GNUNET_NETWORK_fdset_copy_native (rfds, &aread, selectret);
1908 GNUNET_NETWORK_fdset_zero (wfds);
1909 if (selectret != -1)
1910 GNUNET_NETWORK_fdset_copy_native (wfds, &awrite, selectret);
1914 GNUNET_NETWORK_fdset_zero (efds);
1915 if (selectret != -1)
1916 GNUNET_NETWORK_fdset_copy_native (efds, &aexcept, selectret);
1918 if (-1 == selectret)
1920 /* Add our select() FDs to the total return value */
1921 retcode += selectret;
1925 /* If we got this far, use slower implementation that is able to do a waiting select
1926 on both sockets and pipes simultaneously */
1928 /* Events for pipes */
1929 if (! hEventReadReady)
1930 hEventReadReady = CreateEvent (NULL, TRUE, TRUE, NULL);
1931 if (! hEventPipeWrite)
1932 hEventPipeWrite = CreateEvent (NULL, TRUE, TRUE, NULL);
1939 FD_COPY (&rfds->sds, &aread);
1941 FD_COPY (&wfds->sds, &awrite);
1943 FD_COPY (&efds->sds, &aexcept);
1944 /* We will first Add the PIPES to the events */
1945 /* Track how far in `handle_array` the read pipes go,
1946 so we may by-pass them quickly if none of them
1949 if (rfds && (rfds->handles_pos > 0))
1951 for (i = 0; i <rfds->handles_pos; i++)
1953 fh = rfds->handles[i];
1954 if (fh->type != GNUNET_DISK_HANLDE_TYPE_PIPE)
1956 /* Read zero bytes to check the status of the pipe */
1957 if (! ReadFile (fh->h, NULL, 0, NULL, fh->oOverlapRead))
1959 DWORD error_code = GetLastError ();
1961 if (error_code == ERROR_IO_PENDING)
1963 /* add as unready */
1964 handle_array[nhandles++] = fh->oOverlapRead->hEvent;
1970 handle_array[nhandles++] = hEventReadReady;
1976 /* error also counts as ready */
1977 handle_array[nhandles++] = hEventReadReady;
1983 if (wfds && (wfds->handles_pos > 0))
1985 LOG (GNUNET_ERROR_TYPE_DEBUG,
1986 "Adding the write ready event to the array as %d\n",
1988 handle_array[nhandles++] = hEventPipeWrite;
1994 LOG (GNUNET_ERROR_TYPE_DEBUG,
1995 "Adding the socket event to the array as %d\n",
1997 handle_array[nhandles++] = select_finished_event;
1998 if (timeout.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
2004 select_timeout.tv_sec = timeout.rel_value_us / GNUNET_TIME_UNIT_SECONDS.rel_value_us;
2005 select_timeout.tv_usec = (timeout.rel_value_us -
2006 (select_timeout.tv_sec *
2007 GNUNET_TIME_UNIT_SECONDS.rel_value_us));
2008 sp.tv = &select_timeout;
2010 FD_SET (select_wakeup_socket, &aread);
2013 i = recv (select_wakeup_socket,
2014 (char *) &returnedpos,
2021 /* Failed connections cause sockets to be set in errorfds on W32,
2022 * but on POSIX it should set them in writefds.
2023 * First copy all awrite sockets to aexcept, later we'll
2024 * check aexcept and set its contents in awrite as well
2025 * Sockets are also set in errorfds when OOB data is available,
2026 * but we don't use OOB data.
2028 for (i = 0; i < awrite.fd_count; i++)
2029 FD_SET (awrite.fd_array[i],
2031 ResetEvent (select_finished_event);
2032 SetEvent (select_standby_event);
2035 /* NULL-terminate array */
2036 handle_array[nhandles] = NULL;
2037 LOG (GNUNET_ERROR_TYPE_DEBUG,
2038 "nfds: %d, handles: %d, will wait: %llu mcs\n",
2045 = WaitForMultipleObjects (nhandles,
2049 LOG (GNUNET_ERROR_TYPE_DEBUG,
2050 "WaitForMultipleObjects Returned: %d\n",
2055 GNUNET_break (0); /* This branch shouldn't actually be executed...*/
2056 i = (int) WaitForSingleObject (select_finished_event,
2058 returncode = WAIT_TIMEOUT;
2062 /* Shouldn't come this far. If it does - investigate. */
2068 /* Don't wake up select-thread when delay is 0, it should return immediately
2069 * and wake up by itself.
2072 i = send (select_send_socket,
2073 (const char *) &returnedpos,
2076 i = (int) WaitForSingleObject (select_finished_event,
2078 LOG (GNUNET_ERROR_TYPE_DEBUG,
2079 "Finished waiting for the select thread: %d %d\n",
2086 i = recv (select_wakeup_socket,
2087 (char *) &returnedpos,
2091 /* Check aexcept, add its contents to awrite */
2092 for (i = 0; i < aexcept.fd_count; i++)
2093 FD_SET (aexcept.fd_array[i], &awrite);
2096 returnedpos = returncode - WAIT_OBJECT_0;
2097 LOG (GNUNET_ERROR_TYPE_DEBUG,
2098 "return pos is: %d\n",
2103 /* We queued a zero-long read on each pipe to check
2104 * its state, now we must cancel these read operations.
2105 * This must be done while rfds->handles_pos is still
2106 * intact and matches the number of read handles that we
2107 * got from the caller.
2109 for (i = 0; i < rfds->handles_pos; i++)
2111 fh = rfds->handles[i];
2112 if (GNUNET_DISK_HANLDE_TYPE_PIPE == fh->type)
2116 /* We may have some pipes ready for reading. */
2117 if (returnedpos < read_pipes_off)
2118 retcode += check_handles_status (rfds, GNUNET_NO);
2120 rfds->handles_pos = 0;
2122 if (-1 != sp.status)
2123 GNUNET_NETWORK_fdset_copy_native (rfds, &aread, retcode);
2127 retcode += wfds->handles_pos;
2128 /* wfds handles remain untouched */
2129 if (-1 != sp.status)
2130 GNUNET_NETWORK_fdset_copy_native (wfds, &awrite, retcode);
2134 retcode += check_handles_status (rfds, GNUNET_YES);
2135 if (-1 != sp.status)
2136 GNUNET_NETWORK_fdset_copy_native (efds, &aexcept, retcode);
2140 retcode += sp.status;
2148 /* end of network.c */