2 This file is part of GNUnet.
3 Copyright (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,
322 if (h->fd == INVALID_SOCKET)
325 SetErrnoFromWinsockError (WSAGetLastError ());
330 return GNUNET_SYSERR;
333 if (h->fd >= FD_SETSIZE)
335 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (h));
337 return GNUNET_SYSERR;
340 if (GNUNET_OK != socket_set_inheritable (h))
341 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
342 "socket_set_inheritable");
344 if (GNUNET_SYSERR == GNUNET_NETWORK_socket_set_blocking (h, GNUNET_NO))
348 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (h));
350 return GNUNET_SYSERR;
353 socket_set_nosigpipe (h);
355 if ( (type == SOCK_STREAM)
360 socket_set_nodelay (h);
366 * accept a new connection on a socket
368 * @param desc bound socket
369 * @param address address of the connecting peer, may be NULL
370 * @param address_len length of @a address
371 * @return client socket
373 struct GNUNET_NETWORK_Handle *
374 GNUNET_NETWORK_socket_accept (const struct GNUNET_NETWORK_Handle *desc,
375 struct sockaddr *address,
376 socklen_t *address_len)
378 struct GNUNET_NETWORK_Handle *ret;
381 ret = GNUNET_new (struct GNUNET_NETWORK_Handle);
384 struct sockaddr_storage name;
385 socklen_t namelen = sizeof (name);
387 int gsn = getsockname (desc->fd,
388 (struct sockaddr *) &name,
392 LOG (GNUNET_ERROR_TYPE_DEBUG,
393 "Accepting connection on `%s'\n",
394 GNUNET_a2s ((const struct sockaddr *) &name,
398 ret->fd = accept (desc->fd,
409 initialize_network_handle (ret,
410 (NULL != address) ? address->sa_family : desc->af,
421 * Bind a socket to a particular address.
423 * @param desc socket to bind
424 * @param address address to be bound
425 * @param address_len length of @a address
426 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
429 GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc,
430 const struct sockaddr *address,
431 socklen_t address_len)
440 if (AF_INET6 == desc->af)
441 if (setsockopt (desc->fd, IPPROTO_IPV6, IPV6_V6ONLY,
444 LOG_STRERROR (GNUNET_ERROR_TYPE_DEBUG,
453 /* This is required here for TCP sockets, but only on UNIX */
454 if ( (SOCK_STREAM == desc->type) &&
455 (0 != setsockopt (desc->fd,
459 LOG_STRERROR (GNUNET_ERROR_TYPE_DEBUG,
465 /* set permissions of newly created non-abstract UNIX domain socket to
466 "user-only"; applications can choose to relax this later */
467 mode_t old_mask = 0; /* assigned to make compiler happy */
468 const struct sockaddr_un *un;
469 int not_abstract = 0;
471 if ((AF_UNIX == address->sa_family)
472 && (NULL != (un = (const struct sockaddr_un *) address)->sun_path)
473 && ('\0' != un->sun_path[0]) ) /* Not an abstract socket */
476 old_mask = umask (S_IWGRP | S_IRGRP | S_IXGRP | S_IWOTH | S_IROTH | S_IXOTH);
479 ret = bind (desc->fd, address, address_len);
482 (void) umask (old_mask);
486 if (SOCKET_ERROR == ret)
487 SetErrnoFromWinsockError (WSAGetLastError ());
490 return GNUNET_SYSERR;
492 desc->addr = GNUNET_malloc (address_len);
493 memcpy (desc->addr, address, address_len);
494 desc->addrlen = address_len;
504 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
507 GNUNET_NETWORK_socket_close (struct GNUNET_NETWORK_Handle *desc)
515 ret = closesocket (desc->fd);
516 error = WSAGetLastError ();
517 SetErrnoFromWinsockError (error);
518 LOG (GNUNET_ERROR_TYPE_DEBUG,
519 "Closed 0x%x, closesocket() returned %d, GLE is %u\n",
524 ret = close (desc->fd);
527 const struct sockaddr_un *un;
529 /* Cleanup the UNIX domain socket and its parent directories in case of non
531 if ( (AF_UNIX == desc->af) &&
532 (NULL != desc->addr) &&
533 (NULL != (un = (const struct sockaddr_un *) desc->addr)->sun_path) &&
534 ('\0' != un->sun_path[0]) )
536 char *dirname = GNUNET_strndup (un->sun_path,
537 sizeof (un->sun_path));
539 if (0 != unlink (dirname))
541 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING,
549 len = strlen (dirname);
550 while ((len > 0) && (dirname[len] != DIR_SEPARATOR))
553 if ((0 != len) && (0 != rmdir (dirname)))
560 /* these are normal and can just be ignored */
563 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
570 GNUNET_free (dirname);
573 GNUNET_NETWORK_socket_free_memory_only_ (desc);
574 return (ret == 0) ? GNUNET_OK : GNUNET_SYSERR;
579 * Only free memory of a socket, keep the file descriptor untouched.
584 GNUNET_NETWORK_socket_free_memory_only_ (struct GNUNET_NETWORK_Handle *desc)
586 GNUNET_free_non_null (desc->addr);
592 * Box a native socket (and check that it is a socket).
594 * @param fd socket to box
595 * @return NULL on error (including not supported on target platform)
597 struct GNUNET_NETWORK_Handle *
598 GNUNET_NETWORK_socket_box_native (SOCKTYPE fd)
600 struct GNUNET_NETWORK_Handle *ret;
604 /* FIXME: Find a better call to check that FD is valid */
606 WSAIoctl (fd, FIONBIO,
607 (void *) &i, sizeof (i),
610 return NULL; /* invalid FD */
611 ret = GNUNET_new (struct GNUNET_NETWORK_Handle);
616 if (fcntl (fd, F_GETFD) < 0)
617 return NULL; /* invalid FD */
618 ret = GNUNET_new (struct GNUNET_NETWORK_Handle);
627 * Connect a socket to some remote address.
630 * @param address peer address
631 * @param address_len length of @a address
632 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
635 GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Handle *desc,
636 const struct sockaddr *address,
637 socklen_t address_len)
641 ret = connect (desc->fd,
645 if (SOCKET_ERROR == ret)
647 SetErrnoFromWinsockError (WSAGetLastError ());
648 if (errno == EWOULDBLOCK)
652 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
660 * @param level protocol level of the option
661 * @param optname identifier of the option
662 * @param optval options
663 * @param optlen length of @a optval
664 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
667 GNUNET_NETWORK_socket_getsockopt (const struct GNUNET_NETWORK_Handle *desc,
675 ret = getsockopt (desc->fd,
682 (SOL_SOCKET == level) &&
683 (SO_ERROR == optname) )
684 *((int *) optval) = GetErrnoFromWinsockError (*((int *) optval));
685 else if (SOCKET_ERROR == ret)
686 SetErrnoFromWinsockError (WSAGetLastError ());
688 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
696 * @param backlog length of the listen queue
697 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
700 GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Handle *desc,
705 ret = listen (desc->fd,
708 if (SOCKET_ERROR == ret)
709 SetErrnoFromWinsockError (WSAGetLastError ());
711 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
716 * How much data is available to be read on this descriptor?
719 * @returns #GNUNET_NO if no data is available, or on error!
722 GNUNET_NETWORK_socket_recvfrom_amount (const struct GNUNET_NETWORK_Handle *desc)
726 /* How much is there to be read? */
730 error = ioctl (desc->fd,
734 return (ssize_t) pending;
739 error = ioctlsocket (desc->fd,
742 if (error != SOCKET_ERROR)
743 return (ssize_t) pending;
750 * Read data from a socket (always non-blocking).
753 * @param buffer buffer
754 * @param length length of @a buffer
755 * @param src_addr either the source to recv from, or all zeroes
756 * to be filled in by recvfrom
757 * @param addrlen length of the @a src_addr
760 GNUNET_NETWORK_socket_recvfrom (const struct GNUNET_NETWORK_Handle *desc,
763 struct sockaddr *src_addr,
772 flags |= MSG_DONTWAIT;
775 ret = recvfrom (desc->fd,
782 if (SOCKET_ERROR == ret)
783 SetErrnoFromWinsockError (WSAGetLastError ());
790 * Read data from a connected socket (always non-blocking).
793 * @param buffer buffer
794 * @param length length of @a buffer
795 * @return number of bytes received, -1 on error
798 GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Handle *desc,
808 flags |= MSG_DONTWAIT;
810 ret = recv (desc->fd,
815 if (SOCKET_ERROR == ret)
816 SetErrnoFromWinsockError (WSAGetLastError ());
823 * Send data (always non-blocking).
826 * @param buffer data to send
827 * @param length size of the @a buffer
828 * @return number of bytes sent, #GNUNET_SYSERR on error
831 GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Handle *desc,
840 flags |= MSG_DONTWAIT;
844 flags |= MSG_NOSIGNAL;
847 ret = send (desc->fd,
852 if (SOCKET_ERROR == ret)
853 SetErrnoFromWinsockError (WSAGetLastError ());
861 * Send data to a particular destination (always non-blocking).
862 * This function only works for UDP sockets.
865 * @param message data to send
866 * @param length size of the @a message
867 * @param dest_addr destination address
868 * @param dest_len length of @a address
869 * @return number of bytes sent, #GNUNET_SYSERR on error
872 GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Handle *desc,
875 const struct sockaddr *dest_addr,
884 flags |= MSG_DONTWAIT;
887 flags |= MSG_NOSIGNAL;
889 ret = sendto (desc->fd, message, length, flags, dest_addr, dest_len);
891 if (SOCKET_ERROR == ret)
892 SetErrnoFromWinsockError (WSAGetLastError ());
902 * @param level protocol level of the option
903 * @param option_name option identifier
904 * @param option_value value to set
905 * @param option_len size of @a option_value
906 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
909 GNUNET_NETWORK_socket_setsockopt (struct GNUNET_NETWORK_Handle *fd,
912 const void *option_value,
913 socklen_t option_len)
917 ret = setsockopt (fd->fd,
923 if (SOCKET_ERROR == ret)
924 SetErrnoFromWinsockError (WSAGetLastError ());
926 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
931 * Create a new socket. Configure it for non-blocking IO and
932 * mark it as non-inheritable to child processes (set the
933 * close-on-exec flag).
935 * @param domain domain of the socket
936 * @param type socket type
937 * @param protocol network protocol
938 * @return new socket, NULL on error
940 struct GNUNET_NETWORK_Handle *
941 GNUNET_NETWORK_socket_create (int domain,
945 struct GNUNET_NETWORK_Handle *ret;
948 fd = socket (domain, type, protocol);
951 ret = GNUNET_new (struct GNUNET_NETWORK_Handle);
954 initialize_network_handle (ret,
963 * Shut down socket operations
965 * @param how type of shutdown
966 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
969 GNUNET_NETWORK_socket_shutdown (struct GNUNET_NETWORK_Handle *desc,
974 ret = shutdown (desc->fd, how);
977 SetErrnoFromWinsockError (WSAGetLastError ());
979 return (0 == ret) ? GNUNET_OK : GNUNET_SYSERR;
984 * Disable the "CORK" feature for communication with the given socket,
985 * forcing the OS to immediately flush the buffer on transmission
986 * instead of potentially buffering multiple messages. Essentially
987 * reduces the OS send buffers to zero.
990 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
993 GNUNET_NETWORK_socket_disable_corking (struct GNUNET_NETWORK_Handle *desc)
1002 setsockopt (desc->fd,
1007 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
1011 setsockopt (desc->fd,
1016 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
1023 setsockopt (desc->fd,
1028 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
1032 setsockopt (desc->fd,
1037 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
1040 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
1050 GNUNET_NETWORK_fdset_zero (struct GNUNET_NETWORK_FDSet *fds)
1052 FD_ZERO (&fds->sds);
1055 fds->handles_pos = 0;
1061 * Add a socket to the FD set
1064 * @param desc socket to add
1067 GNUNET_NETWORK_fdset_set (struct GNUNET_NETWORK_FDSet *fds,
1068 const struct GNUNET_NETWORK_Handle *desc)
1072 fds->nsds = GNUNET_MAX (fds->nsds,
1078 * Check whether a socket is part of the fd set
1081 * @param desc socket
1082 * @return 0 if the FD is not set
1085 GNUNET_NETWORK_fdset_isset (const struct GNUNET_NETWORK_FDSet *fds,
1086 const struct GNUNET_NETWORK_Handle *desc)
1088 return FD_ISSET (desc->fd,
1094 * Add one fd set to another
1096 * @param dst the fd set to add to
1097 * @param src the fd set to add from
1100 GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst,
1101 const struct GNUNET_NETWORK_FDSet *src)
1106 for (nfds = src->nsds; nfds >= 0; nfds--)
1107 if (FD_ISSET (nfds, &src->sds))
1108 FD_SET (nfds, &dst->sds);
1109 dst->nsds = GNUNET_MAX (dst->nsds,
1112 /* This is MinGW32-specific implementation that relies on the code that
1113 * winsock2.h defines for FD_SET. Namely, it relies on FD_SET checking
1114 * that fd being added is not already in the set.
1115 * Also relies on us knowing what's inside fd_set (fd_count and fd_array).
1117 * NOTE: I don't understand why the UNIX-logic wouldn't work
1118 * for the first part here as well. -CG
1122 for (i = 0; i < src->sds.fd_count; i++)
1123 FD_SET (src->sds.fd_array[i],
1125 dst->nsds = GNUNET_MAX (src->nsds,
1128 /* also copy over `struct GNUNET_DISK_FileHandle` array */
1129 if (dst->handles_pos + src->handles_pos > dst->handles_size)
1130 GNUNET_array_grow (dst->handles,
1132 ((dst->handles_pos + src->handles_pos) << 1));
1133 for (i = 0; i < src->handles_pos; i++)
1134 dst->handles[dst->handles_pos++] = src->handles[i];
1140 * Copy one fd set to another
1142 * @param to destination
1143 * @param from source
1146 GNUNET_NETWORK_fdset_copy (struct GNUNET_NETWORK_FDSet *to,
1147 const struct GNUNET_NETWORK_FDSet *from)
1149 FD_COPY (&from->sds,
1151 to->nsds = from->nsds;
1153 if (from->handles_pos > to->handles_size)
1154 GNUNET_array_grow (to->handles,
1156 from->handles_pos * 2);
1157 memcpy (to->handles,
1159 from->handles_pos * sizeof (struct GNUNET_NETWORK_Handle *));
1160 to->handles_pos = from->handles_pos;
1166 * Return file descriptor for this network handle
1168 * @param desc wrapper to process
1169 * @return POSIX file descriptor
1172 GNUNET_NETWORK_get_fd (struct GNUNET_NETWORK_Handle *desc)
1179 * Return sockaddr for this network handle
1181 * @param desc wrapper to process
1185 GNUNET_NETWORK_get_addr (struct GNUNET_NETWORK_Handle *desc)
1192 * Return sockaddr length for this network handle
1194 * @param desc wrapper to process
1195 * @return socklen_t for sockaddr
1198 GNUNET_NETWORK_get_addrlen (struct GNUNET_NETWORK_Handle *desc)
1200 return desc->addrlen;
1205 * Copy a native fd set
1207 * @param to destination
1208 * @param from native source set
1209 * @param nfds the biggest socket number in from + 1
1212 GNUNET_NETWORK_fdset_copy_native (struct GNUNET_NETWORK_FDSet *to,
1223 * Set a native fd in a set
1225 * @param to destination
1226 * @param nfd native FD to set
1229 GNUNET_NETWORK_fdset_set_native (struct GNUNET_NETWORK_FDSet *to,
1232 GNUNET_assert ((nfd >= 0) && (nfd < FD_SETSIZE));
1233 FD_SET (nfd, &to->sds);
1234 to->nsds = GNUNET_MAX (nfd + 1,
1240 * Test native fd in a set
1242 * @param to set to test, NULL for empty set
1243 * @param nfd native FD to test, or -1 for none
1244 * @return #GNUNET_YES if FD is set in the set
1247 GNUNET_NETWORK_fdset_test_native (const struct GNUNET_NETWORK_FDSet *to,
1253 return FD_ISSET (nfd, &to->sds) ? GNUNET_YES : GNUNET_NO;
1258 * Add a file handle to the fd set
1260 * @param h the file handle to add
1263 GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds,
1264 const struct GNUNET_DISK_FileHandle *h)
1267 if (fds->handles_pos == fds->handles_size)
1268 GNUNET_array_grow (fds->handles,
1270 fds->handles_size * 2 + 2);
1271 fds->handles[fds->handles_pos++] = h;
1275 GNUNET_DISK_internal_file_handle_ (h,
1280 fds->nsds = GNUNET_MAX (fd + 1,
1287 * Add a file handle to the fd set
1289 * @param h the file handle to add
1292 GNUNET_NETWORK_fdset_handle_set_first (struct GNUNET_NETWORK_FDSet *fds,
1293 const struct GNUNET_DISK_FileHandle *h)
1296 if (fds->handles_pos == fds->handles_size)
1297 GNUNET_array_grow (fds->handles,
1299 fds->handles_size * 2 + 2);
1300 fds->handles[fds->handles_pos] = h;
1301 if (fds->handles[0] != h)
1303 const struct GNUNET_DISK_FileHandle *bak = fds->handles[0];
1304 fds->handles[0] = h;
1305 fds->handles[fds->handles_pos] = bak;
1309 GNUNET_NETWORK_fdset_handle_set (fds, h);
1315 * Check if a file handle is part of an fd set
1318 * @param h file handle
1319 * @return #GNUNET_YES if the file handle is part of the set
1322 GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds,
1323 const struct GNUNET_DISK_FileHandle *h)
1328 for (i=0;i<fds->handles_pos;i++)
1329 if (fds->handles[i] == h)
1333 return FD_ISSET (h->fd,
1341 * Numerically compare pointers to sort them.
1342 * Used to test for overlap in the arrays.
1344 * @param p1 a pointer
1345 * @param p2 a pointer
1346 * @return -1, 0 or 1, if the p1 < p2, p1==p2 or p1 > p2.
1349 ptr_cmp (const void *p1,
1354 if ((intptr_t) p1 < (intptr_t) p2)
1362 * Checks if two fd sets overlap
1364 * @param fds1 first fd set
1365 * @param fds2 second fd set
1366 * @return #GNUNET_YES if they do overlap, #GNUNET_NO otherwise
1369 GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1,
1370 const struct GNUNET_NETWORK_FDSet *fds2)
1375 nfds = GNUNET_MIN (fds1->nsds,
1380 if ( (FD_ISSET (nfds,
1391 /* This code is somewhat hacky, we are not supposed to know what's
1392 * inside of fd_set; also the O(n^2) is really bad... */
1393 for (i = 0; i < fds1->sds.fd_count; i++)
1394 for (j = 0; j < fds2->sds.fd_count; j++)
1395 if (fds1->sds.fd_array[i] == fds2->sds.fd_array[j])
1398 /* take a short cut if possible */
1399 if ( (0 == fds1->handles_pos) ||
1400 (0 == fds2->handles_pos) )
1403 /* Sort file handles array to avoid quadratic complexity when
1404 checking for overlap */
1405 qsort (fds1->handles,
1409 qsort (fds2->handles,
1415 while ( (i < fds1->handles_pos) &&
1416 (j < fds2->handles_pos) )
1418 switch (ptr_cmp (fds1->handles[i],
1438 * @return a new fd set
1440 struct GNUNET_NETWORK_FDSet *
1441 GNUNET_NETWORK_fdset_create ()
1443 struct GNUNET_NETWORK_FDSet *fds;
1445 fds = GNUNET_new (struct GNUNET_NETWORK_FDSet);
1446 GNUNET_NETWORK_fdset_zero (fds);
1452 * Releases the associated memory of an fd set
1457 GNUNET_NETWORK_fdset_destroy (struct GNUNET_NETWORK_FDSet *fds)
1460 GNUNET_array_grow (fds->handles,
1472 struct _select_params
1490 * Timeout for select().
1507 SOCKET wakeup_socket;
1510 * Set to return value from select.
1520 _selector (LPVOID p)
1522 struct _select_params *sp = p;
1526 WaitForSingleObject (sp->standby,
1528 ResetEvent (sp->standby);
1529 sp->status = select (1,
1534 if (FD_ISSET (sp->wakeup_socket,
1537 FD_CLR (sp->wakeup_socket,
1541 SetEvent (sp->wakeup);
1547 static HANDLE hEventPipeWrite;
1549 static HANDLE hEventReadReady;
1551 static struct _select_params sp;
1553 static HANDLE select_thread;
1555 static HANDLE select_finished_event;
1557 static HANDLE select_standby_event;
1559 static SOCKET select_wakeup_socket = -1;
1561 static SOCKET select_send_socket = -1;
1563 static struct timeval select_timeout;
1567 * On W32, we actually use a thread to help with the
1568 * event loop due to W32-API limitations. This function
1569 * initializes that thread.
1572 initialize_select_thread ()
1574 SOCKET select_listening_socket = -1;
1575 struct sockaddr_in s_in;
1580 select_standby_event = CreateEvent (NULL, TRUE, FALSE, NULL);
1581 select_finished_event = CreateEvent (NULL, TRUE, FALSE, NULL);
1583 select_wakeup_socket = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
1585 select_listening_socket = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
1588 res = ioctlsocket (select_wakeup_socket, FIONBIO, &p);
1589 LOG (GNUNET_ERROR_TYPE_DEBUG,
1590 "Select thread initialization: ioctlsocket() returns %d\n",
1593 alen = sizeof (s_in);
1594 s_in.sin_family = AF_INET;
1596 s_in.sin_addr.S_un.S_un_b.s_b1 = 127;
1597 s_in.sin_addr.S_un.S_un_b.s_b2 = 0;
1598 s_in.sin_addr.S_un.S_un_b.s_b3 = 0;
1599 s_in.sin_addr.S_un.S_un_b.s_b4 = 1;
1600 res = bind (select_listening_socket,
1601 (const struct sockaddr *) &s_in,
1603 LOG (GNUNET_ERROR_TYPE_DEBUG,
1604 "Select thread initialization: bind() returns %d\n",
1607 res = getsockname (select_listening_socket,
1608 (struct sockaddr *) &s_in,
1610 LOG (GNUNET_ERROR_TYPE_DEBUG,
1611 "Select thread initialization: getsockname() returns %d\n",
1614 res = listen (select_listening_socket,
1616 LOG (GNUNET_ERROR_TYPE_DEBUG,
1617 "Select thread initialization: listen() returns %d\n",
1619 res = connect (select_wakeup_socket,
1620 (const struct sockaddr *) &s_in,
1622 LOG (GNUNET_ERROR_TYPE_DEBUG,
1623 "Select thread initialization: connect() returns %d\n",
1626 select_send_socket = accept (select_listening_socket,
1627 (struct sockaddr *) &s_in,
1630 closesocket (select_listening_socket);
1632 sp.wakeup = select_finished_event;
1633 sp.standby = select_standby_event;
1634 sp.wakeup_socket = select_wakeup_socket;
1636 select_thread = CreateThread (NULL,
1649 * Check if sockets or pipes meet certain conditions
1651 * @param rfds set of sockets or pipes to be checked for readability
1652 * @param wfds set of sockets or pipes to be checked for writability
1653 * @param efds set of sockets or pipes to be checked for exceptions
1654 * @param timeout relative value when to return
1655 * @return number of selected sockets or pipes, #GNUNET_SYSERR on error
1658 GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
1659 struct GNUNET_NETWORK_FDSet *wfds,
1660 struct GNUNET_NETWORK_FDSet *efds,
1661 const struct GNUNET_TIME_Relative timeout)
1671 nfds = GNUNET_MAX (nfds,
1674 nfds = GNUNET_MAX (nfds,
1677 (timeout.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us))
1680 LOG (GNUNET_ERROR_TYPE_ERROR,
1681 _("Fatal internal logic error, process hangs in `%s' (abort with CTRL-C)!\n"),
1684 tv.tv_sec = timeout.rel_value_us / GNUNET_TIME_UNIT_SECONDS.rel_value_us;
1686 (timeout.rel_value_us -
1687 (tv.tv_sec * GNUNET_TIME_UNIT_SECONDS.rel_value_us));
1688 return select (nfds,
1689 (NULL != rfds) ? &rfds->sds : NULL,
1690 (NULL != wfds) ? &wfds->sds : NULL,
1691 (NULL != efds) ? &efds->sds : NULL,
1692 (timeout.rel_value_us ==
1693 GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us) ? NULL : &tv);
1702 * Non-blocking test if a pipe is ready for reading.
1704 * @param fh pipe handle
1705 * @return #GNUNET_YES if the pipe is ready for reading
1708 pipe_read_ready (const struct GNUNET_DISK_FileHandle *fh)
1712 DWORD waitstatus = 0;
1715 bret = PeekNamedPipe (fh->h, NULL, 0, NULL, &waitstatus, NULL);
1716 error = GetLastError ();
1719 /* TODO: either add more errors to this condition, or eliminate it
1720 * entirely (failed to peek -> pipe is in serious trouble, should
1721 * be selected as readable).
1723 if ( (error != ERROR_BROKEN_PIPE) &&
1724 (error != ERROR_INVALID_HANDLE) )
1727 else if (waitstatus <= 0)
1734 * Non-blocking test if a pipe is having an IO exception.
1736 * @param fh pipe handle
1737 * @return #GNUNET_YES if the pipe is having an IO exception.
1740 pipe_except_ready (const struct GNUNET_DISK_FileHandle *fh)
1744 if (PeekNamedPipe (fh->h, NULL, 0, NULL, &dwBytes, NULL))
1751 * Iterate over handles in fds, destructively rewrite the
1752 * handles array contents of fds so that it starts with the
1753 * handles that are ready, and update handles_pos accordingly.
1755 * @param fds set of handles (usually pipes) to be checked for readiness
1756 * @param except GNUNET_NO if fds should be checked for readiness to read,
1757 * GNUNET_YES if fds should be checked for exceptions
1758 * (there is no way to check for write-readiness - pipes are always write-ready)
1759 * @param set_for_sure a HANDLE that is known to be set already,
1760 * because WaitForMultipleObjects() returned its index.
1761 * @return number of ready handles
1764 check_handles_status (struct GNUNET_NETWORK_FDSet *fds,
1766 HANDLE set_for_sure)
1768 const struct GNUNET_DISK_FileHandle *fh;
1772 for (woff = 0, roff = 0; roff < fds->handles_pos; roff++)
1774 fh = fds->handles[roff];
1775 if (fh == set_for_sure)
1777 fds->handles[woff++] = fh;
1779 else if (fh->type == GNUNET_DISK_HANLDE_TYPE_PIPE)
1781 if ((except && pipe_except_ready (fh)) ||
1782 (!except && pipe_read_ready (fh)))
1783 fds->handles[woff++] = fh;
1785 else if (fh->type == GNUNET_DISK_HANLDE_TYPE_FILE)
1788 fds->handles[woff++] = fh;
1792 if (WAIT_OBJECT_0 == WaitForSingleObject (fh->h, 0))
1793 fds->handles[woff++] = fh;
1796 fds->handles_pos = woff;
1802 * Check if sockets or pipes meet certain conditions, version for W32.
1804 * @param rfds set of sockets or pipes to be checked for readability
1805 * @param wfds set of sockets or pipes to be checked for writability
1806 * @param efds set of sockets or pipes to be checked for exceptions
1807 * @param timeout relative value when to return
1808 * @return number of selected sockets or pipes, #GNUNET_SYSERR on error
1811 GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
1812 struct GNUNET_NETWORK_FDSet *wfds,
1813 struct GNUNET_NETWORK_FDSet *efds,
1814 const struct GNUNET_TIME_Relative timeout)
1816 const struct GNUNET_DISK_FileHandle *fh;
1825 HANDLE handle_array[FD_SETSIZE + 2];
1827 int returnedpos = 0;
1837 nfds = GNUNET_MAX (nfds, rfds->nsds);
1838 handles += rfds->handles_pos;
1842 nfds = GNUNET_MAX (nfds, wfds->nsds);
1843 handles += wfds->handles_pos;
1847 nfds = GNUNET_MAX (nfds, efds->nsds);
1848 handles += efds->handles_pos;
1852 (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == timeout.rel_value_us) &&
1856 LOG (GNUNET_ERROR_TYPE_ERROR,
1857 _("Fatal internal logic error, process hangs in `%s' (abort with CTRL-C)!\n"),
1860 #define SAFE_FD_ISSET(fd, set) (set != NULL && FD_ISSET(fd, set))
1861 /* calculate how long we need to wait in microseconds */
1862 if (timeout.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
1864 mcs_total = INFINITE;
1865 ms_rounded = INFINITE;
1869 mcs_total = timeout.rel_value_us / GNUNET_TIME_UNIT_MICROSECONDS.rel_value_us;
1870 ms_rounded = (DWORD) (mcs_total / GNUNET_TIME_UNIT_MILLISECONDS.rel_value_us);
1871 if (mcs_total > 0 && ms_rounded == 0)
1874 /* select() may be used as a portable way to sleep */
1875 if (! (rfds || wfds || efds))
1881 if (NULL == select_thread)
1882 initialize_select_thread ();
1888 FD_COPY (&rfds->sds, &aread);
1890 FD_COPY (&wfds->sds, &awrite);
1892 FD_COPY (&efds->sds, &aexcept);
1894 /* Start by doing a fast check on sockets and pipes (without
1895 waiting). It is cheap, and is sufficient most of the time. By
1896 profiling we detected that to be true in 90% of the cases.
1899 /* Do the select now */
1900 select_timeout.tv_sec = 0;
1901 select_timeout.tv_usec = 0;
1903 /* Copy all the writes to the except, so we can detect connect() errors */
1904 for (i = 0; i < awrite.fd_count; i++)
1905 FD_SET (awrite.fd_array[i],
1907 if ( (aread.fd_count > 0) ||
1908 (awrite.fd_count > 0) ||
1909 (aexcept.fd_count > 0) )
1910 selectret = select (1,
1911 (NULL != rfds) ? &aread : NULL,
1912 (NULL != wfds) ? &awrite : NULL,
1917 if (-1 == selectret)
1919 /* Throw an error early on, while we still have the context. */
1920 LOG (GNUNET_ERROR_TYPE_ERROR,
1921 "W32 select(%d, %d, %d) failed: %lu\n",
1922 rfds ? aread.fd_count : 0,
1923 wfds ? awrite.fd_count : 0,
1929 /* Check aexcept, if something is in there and we copied that
1930 FD before to detect connect() errors, add it back to the
1931 write set to report errors. */
1933 for (i = 0; i < aexcept.fd_count; i++)
1934 if (FD_ISSET (aexcept.fd_array[i],
1936 FD_SET (aexcept.fd_array[i],
1940 /* If our select returned something or is a 0-timed request, then
1941 also check the pipes and get out of here! */
1942 /* Sadly, it means code duplication :( */
1943 if ( (selectret > 0) || (0 == mcs_total) )
1948 if (rfds && (rfds->handles_pos > 0))
1949 retcode += check_handles_status (rfds, GNUNET_NO, NULL);
1951 /* wfds handles remain untouched, on W32
1952 we pretend our pipes are "always" write-ready */
1955 if (efds && (efds->handles_pos > 0))
1956 retcode += check_handles_status (efds, GNUNET_YES, NULL);
1960 GNUNET_NETWORK_fdset_zero (rfds);
1961 if (selectret != -1)
1962 GNUNET_NETWORK_fdset_copy_native (rfds, &aread, selectret);
1966 GNUNET_NETWORK_fdset_zero (wfds);
1967 if (selectret != -1)
1968 GNUNET_NETWORK_fdset_copy_native (wfds, &awrite, selectret);
1972 GNUNET_NETWORK_fdset_zero (efds);
1973 if (selectret != -1)
1974 GNUNET_NETWORK_fdset_copy_native (efds, &aexcept, selectret);
1976 if (-1 == selectret)
1978 /* Add our select() FDs to the total return value */
1979 retcode += selectret;
1983 /* If we got this far, use slower implementation that is able to do a waiting select
1984 on both sockets and pipes simultaneously */
1986 /* Events for pipes */
1987 if (! hEventReadReady)
1988 hEventReadReady = CreateEvent (NULL, TRUE, TRUE, NULL);
1989 if (! hEventPipeWrite)
1990 hEventPipeWrite = CreateEvent (NULL, TRUE, TRUE, NULL);
1997 FD_COPY (&rfds->sds, &aread);
1999 FD_COPY (&wfds->sds, &awrite);
2001 FD_COPY (&efds->sds, &aexcept);
2002 /* We will first Add the PIPES to the events */
2003 /* Track how far in `handle_array` the read pipes go,
2004 so we may by-pass them quickly if none of them
2007 if (rfds && (rfds->handles_pos > 0))
2009 for (i = 0; i <rfds->handles_pos; i++)
2011 fh = rfds->handles[i];
2012 if (fh->type == GNUNET_DISK_HANLDE_TYPE_EVENT)
2014 handle_array[nhandles++] = fh->h;
2017 if (fh->type != GNUNET_DISK_HANLDE_TYPE_PIPE)
2019 /* Read zero bytes to check the status of the pipe */
2020 if (! ReadFile (fh->h, NULL, 0, NULL, fh->oOverlapRead))
2022 DWORD error_code = GetLastError ();
2024 if (error_code == ERROR_IO_PENDING)
2026 /* add as unready */
2027 handle_array[nhandles++] = fh->oOverlapRead->hEvent;
2033 handle_array[nhandles++] = hEventReadReady;
2039 /* error also counts as ready */
2040 handle_array[nhandles++] = hEventReadReady;
2046 if (wfds && (wfds->handles_pos > 0))
2048 LOG (GNUNET_ERROR_TYPE_DEBUG,
2049 "Adding the write ready event to the array as %d\n",
2051 handle_array[nhandles++] = hEventPipeWrite;
2057 LOG (GNUNET_ERROR_TYPE_DEBUG,
2058 "Adding the socket event to the array as %d\n",
2060 handle_array[nhandles++] = select_finished_event;
2061 if (timeout.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
2067 select_timeout.tv_sec = timeout.rel_value_us / GNUNET_TIME_UNIT_SECONDS.rel_value_us;
2068 select_timeout.tv_usec = (timeout.rel_value_us -
2069 (select_timeout.tv_sec *
2070 GNUNET_TIME_UNIT_SECONDS.rel_value_us));
2071 sp.tv = &select_timeout;
2073 FD_SET (select_wakeup_socket, &aread);
2076 i = recv (select_wakeup_socket,
2077 (char *) &returnedpos,
2084 /* Failed connections cause sockets to be set in errorfds on W32,
2085 * but on POSIX it should set them in writefds.
2086 * First copy all awrite sockets to aexcept, later we'll
2087 * check aexcept and set its contents in awrite as well
2088 * Sockets are also set in errorfds when OOB data is available,
2089 * but we don't use OOB data.
2091 for (i = 0; i < awrite.fd_count; i++)
2092 FD_SET (awrite.fd_array[i],
2094 ResetEvent (select_finished_event);
2095 SetEvent (select_standby_event);
2098 /* NULL-terminate array */
2099 handle_array[nhandles] = NULL;
2100 LOG (GNUNET_ERROR_TYPE_DEBUG,
2101 "nfds: %d, handles: %d, will wait: %llu mcs\n",
2108 = WaitForMultipleObjects (nhandles,
2112 LOG (GNUNET_ERROR_TYPE_DEBUG,
2113 "WaitForMultipleObjects Returned: %d\n",
2118 GNUNET_break (0); /* This branch shouldn't actually be executed...*/
2119 i = (int) WaitForSingleObject (select_finished_event,
2121 returncode = WAIT_TIMEOUT;
2125 /* Shouldn't come this far. If it does - investigate. */
2131 /* Don't wake up select-thread when delay is 0, it should return immediately
2132 * and wake up by itself.
2135 i = send (select_send_socket,
2136 (const char *) &returnedpos,
2139 i = (int) WaitForSingleObject (select_finished_event,
2141 LOG (GNUNET_ERROR_TYPE_DEBUG,
2142 "Finished waiting for the select thread: %d %d\n",
2149 i = recv (select_wakeup_socket,
2150 (char *) &returnedpos,
2154 /* Check aexcept, add its contents to awrite */
2155 for (i = 0; i < aexcept.fd_count; i++)
2156 FD_SET (aexcept.fd_array[i], &awrite);
2159 returnedpos = returncode - WAIT_OBJECT_0;
2160 LOG (GNUNET_ERROR_TYPE_DEBUG,
2161 "return pos is: %d\n",
2166 /* We queued a zero-long read on each pipe to check
2167 * its state, now we must cancel these read operations.
2168 * This must be done while rfds->handles_pos is still
2169 * intact and matches the number of read handles that we
2170 * got from the caller.
2172 for (i = 0; i < rfds->handles_pos; i++)
2174 fh = rfds->handles[i];
2175 if (GNUNET_DISK_HANLDE_TYPE_PIPE == fh->type)
2179 /* We may have some pipes ready for reading. */
2180 if (returnedpos < read_pipes_off)
2181 retcode += check_handles_status (rfds, GNUNET_NO, handle_array[returnedpos]);
2183 rfds->handles_pos = 0;
2185 if (-1 != sp.status)
2186 GNUNET_NETWORK_fdset_copy_native (rfds, &aread, retcode);
2190 retcode += wfds->handles_pos;
2191 /* wfds handles remain untouched */
2192 if (-1 != sp.status)
2193 GNUNET_NETWORK_fdset_copy_native (wfds, &awrite, retcode);
2197 retcode += check_handles_status (rfds,
2199 returnedpos < nhandles ? handle_array[returnedpos] : NULL);
2200 if (-1 != sp.status)
2201 GNUNET_NETWORK_fdset_copy_native (efds, &aexcept, retcode);
2205 retcode += sp.status;
2213 /* end of network.c */