2 This file is part of GNUnet.
3 Copyright (C) 2009-2013 GNUnet e.V.
5 GNUnet is free software; you can redistribute it and/or modify
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, 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);
152 * If services crash, they can leave a unix domain socket file on the
153 * disk. This needs to be manually removed, because otherwise both
154 * bind() and connect() for the respective address will fail. In this
155 * function, we test if such a left-over file exists, and if so,
156 * remove it (unless there is a listening service at the address).
158 * @param un unix domain socket address to check
161 GNUNET_NETWORK_unix_precheck (const struct sockaddr_un *un)
168 s = socket (AF_UNIX, SOCK_STREAM, 0);
171 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
172 "Failed to open AF_UNIX socket");
176 (struct sockaddr *) un,
177 sizeof (struct sockaddr_un));
179 GNUNET_break (0 == close (s));
181 return; /* another process is listening, do not remove! */
182 if (ECONNREFUSED != eno)
183 return; /* some other error, likely "no such file or directory" -- all well */
184 /* should unlink, but sanity checks first */
185 if (0 != stat (un->sun_path,
187 return; /* failed to 'stat', likely does not exist after all */
188 if (S_IFSOCK != (S_IFMT & sbuf.st_mode))
189 return; /* refuse to unlink anything except sockets */
190 /* finally, really unlink */
191 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
192 "Removing left-over `%s' from previous exeuction\n",
194 if (0 != unlink (un->sun_path))
195 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
204 #define FD_COPY(s, d) do { GNUNET_memcpy ((d), (s), sizeof (fd_set)); } while (0)
209 * Set if a socket should use blocking or non-blocking IO.
212 * @param doBlock blocking mode
213 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
216 GNUNET_NETWORK_socket_set_blocking (struct GNUNET_NETWORK_Handle *fd,
230 SetErrnoFromWinsockError (WSAGetLastError ());
231 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
233 return GNUNET_SYSERR;
239 int flags = fcntl (fd->fd, F_GETFL);
244 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
246 return GNUNET_SYSERR;
249 flags &= ~O_NONBLOCK;
253 if (0 != fcntl (fd->fd,
258 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
260 return GNUNET_SYSERR;
268 * Make a socket non-inheritable to child processes
270 * @param h the socket to make non-inheritable
271 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
272 * @warning Not implemented on Windows
275 socket_set_inheritable (const struct GNUNET_NETWORK_Handle *h)
279 i = fcntl (h->fd, F_GETFD);
281 return GNUNET_SYSERR;
282 if (i == (i | FD_CLOEXEC))
285 if (fcntl (h->fd, F_SETFD, i) < 0)
286 return GNUNET_SYSERR;
290 b = SetHandleInformation ((HANDLE) h->fd, HANDLE_FLAG_INHERIT, 0);
293 SetErrnoFromWinsockError (WSAGetLastError ());
294 return GNUNET_SYSERR;
303 * The MSG_NOSIGNAL equivalent on Mac OS X
305 * @param h the socket to make non-delaying
308 socket_set_nosigpipe (const struct GNUNET_NETWORK_Handle *h)
313 setsockopt (h->fd, SOL_SOCKET, SO_NOSIGPIPE,
314 (const void *) &abs_value,
316 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
322 * Disable delays when sending data via the socket.
323 * (GNUnet makes sure that messages are as big as
326 * @param h the socket to make non-delaying
329 socket_set_nodelay (const struct GNUNET_NETWORK_Handle *h)
338 &value, sizeof (value)))
339 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
342 const char *abs_value = "1";
345 setsockopt (h->fd, IPPROTO_TCP, TCP_NODELAY,
346 (const void *) abs_value,
348 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
355 * Perform proper canonical initialization for a network handle.
356 * Set it to non-blocking, make it non-inheritable to child
357 * processes, disable SIGPIPE, enable "nodelay" (if non-UNIX
358 * stream socket) and check that it is smaller than FD_SETSIZE.
360 * @param h socket to initialize
361 * @param af address family of the socket
362 * @param type socket type
363 * @return #GNUNET_OK on success, #GNUNET_SYSERR if initialization
364 * failed and the handle was destroyed
367 initialize_network_handle (struct GNUNET_NETWORK_Handle *h,
375 if (h->fd == INVALID_SOCKET)
378 SetErrnoFromWinsockError (WSAGetLastError ());
383 return GNUNET_SYSERR;
386 if (h->fd >= FD_SETSIZE)
388 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (h));
390 return GNUNET_SYSERR;
393 if (GNUNET_OK != socket_set_inheritable (h))
394 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
395 "socket_set_inheritable");
397 if (GNUNET_SYSERR == GNUNET_NETWORK_socket_set_blocking (h, GNUNET_NO))
401 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (h));
403 return GNUNET_SYSERR;
406 socket_set_nosigpipe (h);
408 if ( (type == SOCK_STREAM)
413 socket_set_nodelay (h);
419 * accept a new connection on a socket
421 * @param desc bound socket
422 * @param address address of the connecting peer, may be NULL
423 * @param address_len length of @a address
424 * @return client socket
426 struct GNUNET_NETWORK_Handle *
427 GNUNET_NETWORK_socket_accept (const struct GNUNET_NETWORK_Handle *desc,
428 struct sockaddr *address,
429 socklen_t *address_len)
431 struct GNUNET_NETWORK_Handle *ret;
434 ret = GNUNET_new (struct GNUNET_NETWORK_Handle);
437 struct sockaddr_storage name;
438 socklen_t namelen = sizeof (name);
440 int gsn = getsockname (desc->fd,
441 (struct sockaddr *) &name,
445 LOG (GNUNET_ERROR_TYPE_DEBUG,
446 "Accepting connection on `%s'\n",
447 GNUNET_a2s ((const struct sockaddr *) &name,
451 ret->fd = accept (desc->fd,
462 initialize_network_handle (ret,
463 (NULL != address) ? address->sa_family : desc->af,
474 * Bind a socket to a particular address.
476 * @param desc socket to bind
477 * @param address address to be bound
478 * @param address_len length of @a address
479 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
482 GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc,
483 const struct sockaddr *address,
484 socklen_t address_len)
493 if (AF_INET6 == desc->af)
494 if (setsockopt (desc->fd, IPPROTO_IPV6, IPV6_V6ONLY,
497 LOG_STRERROR (GNUNET_ERROR_TYPE_DEBUG,
503 if (AF_UNIX == address->sa_family)
504 GNUNET_NETWORK_unix_precheck ((const struct sockaddr_un *) address);
508 /* This is required here for TCP sockets, but only on UNIX */
509 if ( (SOCK_STREAM == desc->type) &&
510 (0 != setsockopt (desc->fd,
514 LOG_STRERROR (GNUNET_ERROR_TYPE_DEBUG,
518 /* set permissions of newly created non-abstract UNIX domain socket to
519 "user-only"; applications can choose to relax this later */
520 mode_t old_mask = 0; /* assigned to make compiler happy */
521 const struct sockaddr_un *un;
522 int not_abstract = 0;
524 if ((AF_UNIX == address->sa_family)
525 && (NULL != (un = (const struct sockaddr_un *) address)->sun_path)
526 && ('\0' != un->sun_path[0]) ) /* Not an abstract socket */
529 old_mask = umask (S_IWGRP | S_IRGRP | S_IXGRP | S_IWOTH | S_IROTH | S_IXOTH);
532 ret = bind (desc->fd,
538 (void) umask (old_mask);
542 if (SOCKET_ERROR == ret)
543 SetErrnoFromWinsockError (WSAGetLastError ());
546 return GNUNET_SYSERR;
548 desc->addr = GNUNET_malloc (address_len);
549 GNUNET_memcpy (desc->addr, address, address_len);
550 desc->addrlen = address_len;
560 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
563 GNUNET_NETWORK_socket_close (struct GNUNET_NETWORK_Handle *desc)
571 ret = closesocket (desc->fd);
572 error = WSAGetLastError ();
573 SetErrnoFromWinsockError (error);
574 LOG (GNUNET_ERROR_TYPE_DEBUG,
575 "Closed 0x%x, closesocket() returned %d, GLE is %u\n",
580 ret = close (desc->fd);
583 const struct sockaddr_un *un;
585 /* Cleanup the UNIX domain socket and its parent directories in case of non
587 if ( (AF_UNIX == desc->af) &&
588 (NULL != desc->addr) &&
589 (NULL != (un = (const struct sockaddr_un *) desc->addr)->sun_path) &&
590 ('\0' != un->sun_path[0]) )
592 char *dirname = GNUNET_strndup (un->sun_path,
593 sizeof (un->sun_path));
595 if (0 != unlink (dirname))
597 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING,
605 len = strlen (dirname);
606 while ((len > 0) && (dirname[len] != DIR_SEPARATOR))
609 if ((0 != len) && (0 != rmdir (dirname)))
616 /* these are normal and can just be ignored */
619 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
626 GNUNET_free (dirname);
629 GNUNET_NETWORK_socket_free_memory_only_ (desc);
630 return (ret == 0) ? GNUNET_OK : GNUNET_SYSERR;
635 * Only free memory of a socket, keep the file descriptor untouched.
640 GNUNET_NETWORK_socket_free_memory_only_ (struct GNUNET_NETWORK_Handle *desc)
642 GNUNET_free_non_null (desc->addr);
648 * Box a native socket (and check that it is a socket).
650 * @param fd socket to box
651 * @return NULL on error (including not supported on target platform)
653 struct GNUNET_NETWORK_Handle *
654 GNUNET_NETWORK_socket_box_native (SOCKTYPE fd)
656 struct GNUNET_NETWORK_Handle *ret;
660 /* FIXME: Find a better call to check that FD is valid */
662 WSAIoctl (fd, FIONBIO,
663 (void *) &i, sizeof (i),
666 return NULL; /* invalid FD */
667 ret = GNUNET_new (struct GNUNET_NETWORK_Handle);
672 if (fcntl (fd, F_GETFD) < 0)
673 return NULL; /* invalid FD */
674 ret = GNUNET_new (struct GNUNET_NETWORK_Handle);
683 * Connect a socket to some remote address.
686 * @param address peer address
687 * @param address_len length of @a address
688 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
691 GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Handle *desc,
692 const struct sockaddr *address,
693 socklen_t address_len)
697 ret = connect (desc->fd,
701 if (SOCKET_ERROR == ret)
703 SetErrnoFromWinsockError (WSAGetLastError ());
704 if (errno == EWOULDBLOCK)
708 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
716 * @param level protocol level of the option
717 * @param optname identifier of the option
718 * @param optval options
719 * @param optlen length of @a optval
720 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
723 GNUNET_NETWORK_socket_getsockopt (const struct GNUNET_NETWORK_Handle *desc,
731 ret = getsockopt (desc->fd,
738 (SOL_SOCKET == level) &&
739 (SO_ERROR == optname) )
740 *((int *) optval) = GetErrnoFromWinsockError (*((int *) optval));
741 else if (SOCKET_ERROR == ret)
742 SetErrnoFromWinsockError (WSAGetLastError ());
744 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
752 * @param backlog length of the listen queue
753 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
756 GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Handle *desc,
761 ret = listen (desc->fd,
764 if (SOCKET_ERROR == ret)
765 SetErrnoFromWinsockError (WSAGetLastError ());
767 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
772 * How much data is available to be read on this descriptor?
775 * @returns #GNUNET_NO if no data is available, or on error!
778 GNUNET_NETWORK_socket_recvfrom_amount (const struct GNUNET_NETWORK_Handle *desc)
782 /* How much is there to be read? */
786 error = ioctl (desc->fd,
790 return (ssize_t) pending;
795 error = ioctlsocket (desc->fd,
798 if (error != SOCKET_ERROR)
799 return (ssize_t) pending;
806 * Read data from a socket (always non-blocking).
809 * @param buffer buffer
810 * @param length length of @a buffer
811 * @param src_addr either the source to recv from, or all zeroes
812 * to be filled in by recvfrom
813 * @param addrlen length of the @a src_addr
816 GNUNET_NETWORK_socket_recvfrom (const struct GNUNET_NETWORK_Handle *desc,
819 struct sockaddr *src_addr,
828 flags |= MSG_DONTWAIT;
831 ret = recvfrom (desc->fd,
838 if (SOCKET_ERROR == ret)
839 SetErrnoFromWinsockError (WSAGetLastError ());
846 * Read data from a connected socket (always non-blocking).
849 * @param buffer buffer
850 * @param length length of @a buffer
851 * @return number of bytes received, -1 on error
854 GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Handle *desc,
864 flags |= MSG_DONTWAIT;
866 ret = recv (desc->fd,
871 if (SOCKET_ERROR == ret)
872 SetErrnoFromWinsockError (WSAGetLastError ());
879 * Send data (always non-blocking).
882 * @param buffer data to send
883 * @param length size of the @a buffer
884 * @return number of bytes sent, #GNUNET_SYSERR on error
887 GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Handle *desc,
896 flags |= MSG_DONTWAIT;
900 flags |= MSG_NOSIGNAL;
903 ret = send (desc->fd,
908 if (SOCKET_ERROR == ret)
909 SetErrnoFromWinsockError (WSAGetLastError ());
917 * Send data to a particular destination (always non-blocking).
918 * This function only works for UDP sockets.
921 * @param message data to send
922 * @param length size of the @a message
923 * @param dest_addr destination address
924 * @param dest_len length of @a address
925 * @return number of bytes sent, #GNUNET_SYSERR on error
928 GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Handle *desc,
931 const struct sockaddr *dest_addr,
940 flags |= MSG_DONTWAIT;
943 flags |= MSG_NOSIGNAL;
945 ret = sendto (desc->fd, message, length, flags, dest_addr, dest_len);
947 if (SOCKET_ERROR == ret)
948 SetErrnoFromWinsockError (WSAGetLastError ());
958 * @param level protocol level of the option
959 * @param option_name option identifier
960 * @param option_value value to set
961 * @param option_len size of @a option_value
962 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
965 GNUNET_NETWORK_socket_setsockopt (struct GNUNET_NETWORK_Handle *fd,
968 const void *option_value,
969 socklen_t option_len)
973 ret = setsockopt (fd->fd,
979 if (SOCKET_ERROR == ret)
980 SetErrnoFromWinsockError (WSAGetLastError ());
982 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
987 * Create a new socket. Configure it for non-blocking IO and
988 * mark it as non-inheritable to child processes (set the
989 * close-on-exec flag).
991 * @param domain domain of the socket
992 * @param type socket type
993 * @param protocol network protocol
994 * @return new socket, NULL on error
996 struct GNUNET_NETWORK_Handle *
997 GNUNET_NETWORK_socket_create (int domain,
1001 struct GNUNET_NETWORK_Handle *ret;
1004 fd = socket (domain, type, protocol);
1007 ret = GNUNET_new (struct GNUNET_NETWORK_Handle);
1010 initialize_network_handle (ret,
1019 * Shut down socket operations
1020 * @param desc socket
1021 * @param how type of shutdown
1022 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
1025 GNUNET_NETWORK_socket_shutdown (struct GNUNET_NETWORK_Handle *desc,
1030 ret = shutdown (desc->fd, how);
1033 SetErrnoFromWinsockError (WSAGetLastError ());
1035 return (0 == ret) ? GNUNET_OK : GNUNET_SYSERR;
1040 * Disable the "CORK" feature for communication with the given socket,
1041 * forcing the OS to immediately flush the buffer on transmission
1042 * instead of potentially buffering multiple messages. Essentially
1043 * reduces the OS send buffers to zero.
1045 * @param desc socket
1046 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
1049 GNUNET_NETWORK_socket_disable_corking (struct GNUNET_NETWORK_Handle *desc)
1058 setsockopt (desc->fd,
1063 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
1067 setsockopt (desc->fd,
1072 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
1079 setsockopt (desc->fd,
1084 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
1088 setsockopt (desc->fd,
1093 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
1096 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
1106 GNUNET_NETWORK_fdset_zero (struct GNUNET_NETWORK_FDSet *fds)
1108 FD_ZERO (&fds->sds);
1111 fds->handles_pos = 0;
1117 * Add a socket to the FD set
1120 * @param desc socket to add
1123 GNUNET_NETWORK_fdset_set (struct GNUNET_NETWORK_FDSet *fds,
1124 const struct GNUNET_NETWORK_Handle *desc)
1128 fds->nsds = GNUNET_MAX (fds->nsds,
1134 * Check whether a socket is part of the fd set
1137 * @param desc socket
1138 * @return 0 if the FD is not set
1141 GNUNET_NETWORK_fdset_isset (const struct GNUNET_NETWORK_FDSet *fds,
1142 const struct GNUNET_NETWORK_Handle *desc)
1144 return FD_ISSET (desc->fd,
1150 * Add one fd set to another
1152 * @param dst the fd set to add to
1153 * @param src the fd set to add from
1156 GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst,
1157 const struct GNUNET_NETWORK_FDSet *src)
1162 for (nfds = src->nsds; nfds >= 0; nfds--)
1163 if (FD_ISSET (nfds, &src->sds))
1164 FD_SET (nfds, &dst->sds);
1165 dst->nsds = GNUNET_MAX (dst->nsds,
1168 /* This is MinGW32-specific implementation that relies on the code that
1169 * winsock2.h defines for FD_SET. Namely, it relies on FD_SET checking
1170 * that fd being added is not already in the set.
1171 * Also relies on us knowing what's inside fd_set (fd_count and fd_array).
1173 * NOTE: I don't understand why the UNIX-logic wouldn't work
1174 * for the first part here as well. -CG
1178 for (i = 0; i < src->sds.fd_count; i++)
1179 FD_SET (src->sds.fd_array[i],
1181 dst->nsds = GNUNET_MAX (src->nsds,
1184 /* also copy over `struct GNUNET_DISK_FileHandle` array */
1185 if (dst->handles_pos + src->handles_pos > dst->handles_size)
1186 GNUNET_array_grow (dst->handles,
1188 ((dst->handles_pos + src->handles_pos) << 1));
1189 for (i = 0; i < src->handles_pos; i++)
1190 dst->handles[dst->handles_pos++] = src->handles[i];
1196 * Copy one fd set to another
1198 * @param to destination
1199 * @param from source
1202 GNUNET_NETWORK_fdset_copy (struct GNUNET_NETWORK_FDSet *to,
1203 const struct GNUNET_NETWORK_FDSet *from)
1205 FD_COPY (&from->sds,
1207 to->nsds = from->nsds;
1209 if (from->handles_pos > to->handles_size)
1210 GNUNET_array_grow (to->handles,
1212 from->handles_pos * 2);
1213 GNUNET_memcpy (to->handles,
1215 from->handles_pos * sizeof (struct GNUNET_NETWORK_Handle *));
1216 to->handles_pos = from->handles_pos;
1222 * Return file descriptor for this network handle
1224 * @param desc wrapper to process
1225 * @return POSIX file descriptor
1228 GNUNET_NETWORK_get_fd (struct GNUNET_NETWORK_Handle *desc)
1235 * Return sockaddr for this network handle
1237 * @param desc wrapper to process
1241 GNUNET_NETWORK_get_addr (struct GNUNET_NETWORK_Handle *desc)
1248 * Return sockaddr length for this network handle
1250 * @param desc wrapper to process
1251 * @return socklen_t for sockaddr
1254 GNUNET_NETWORK_get_addrlen (struct GNUNET_NETWORK_Handle *desc)
1256 return desc->addrlen;
1261 * Copy a native fd set
1263 * @param to destination
1264 * @param from native source set
1265 * @param nfds the biggest socket number in from + 1
1268 GNUNET_NETWORK_fdset_copy_native (struct GNUNET_NETWORK_FDSet *to,
1279 * Set a native fd in a set
1281 * @param to destination
1282 * @param nfd native FD to set
1285 GNUNET_NETWORK_fdset_set_native (struct GNUNET_NETWORK_FDSet *to,
1288 GNUNET_assert ((nfd >= 0) && (nfd < FD_SETSIZE));
1289 FD_SET (nfd, &to->sds);
1290 to->nsds = GNUNET_MAX (nfd + 1,
1296 * Test native fd in a set
1298 * @param to set to test, NULL for empty set
1299 * @param nfd native FD to test, or -1 for none
1300 * @return #GNUNET_YES if FD is set in the set
1303 GNUNET_NETWORK_fdset_test_native (const struct GNUNET_NETWORK_FDSet *to,
1309 return FD_ISSET (nfd, &to->sds) ? GNUNET_YES : GNUNET_NO;
1314 * Add a file handle to the fd set
1316 * @param h the file handle to add
1319 GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds,
1320 const struct GNUNET_DISK_FileHandle *h)
1323 if (fds->handles_pos == fds->handles_size)
1324 GNUNET_array_grow (fds->handles,
1326 fds->handles_size * 2 + 2);
1327 fds->handles[fds->handles_pos++] = h;
1331 GNUNET_DISK_internal_file_handle_ (h,
1336 fds->nsds = GNUNET_MAX (fd + 1,
1343 * Add a file handle to the fd set
1345 * @param h the file handle to add
1348 GNUNET_NETWORK_fdset_handle_set_first (struct GNUNET_NETWORK_FDSet *fds,
1349 const struct GNUNET_DISK_FileHandle *h)
1352 if (fds->handles_pos == fds->handles_size)
1353 GNUNET_array_grow (fds->handles,
1355 fds->handles_size * 2 + 2);
1356 fds->handles[fds->handles_pos] = h;
1357 if (fds->handles[0] != h)
1359 const struct GNUNET_DISK_FileHandle *bak = fds->handles[0];
1360 fds->handles[0] = h;
1361 fds->handles[fds->handles_pos] = bak;
1365 GNUNET_NETWORK_fdset_handle_set (fds, h);
1371 * Check if a file handle is part of an fd set
1374 * @param h file handle
1375 * @return #GNUNET_YES if the file handle is part of the set
1378 GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds,
1379 const struct GNUNET_DISK_FileHandle *h)
1384 for (i=0;i<fds->handles_pos;i++)
1385 if (fds->handles[i] == h)
1389 return FD_ISSET (h->fd,
1397 * Numerically compare pointers to sort them.
1398 * Used to test for overlap in the arrays.
1400 * @param p1 a pointer
1401 * @param p2 a pointer
1402 * @return -1, 0 or 1, if the p1 < p2, p1==p2 or p1 > p2.
1405 ptr_cmp (const void *p1,
1410 if ((intptr_t) p1 < (intptr_t) p2)
1418 * Checks if two fd sets overlap
1420 * @param fds1 first fd set
1421 * @param fds2 second fd set
1422 * @return #GNUNET_YES if they do overlap, #GNUNET_NO otherwise
1425 GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1,
1426 const struct GNUNET_NETWORK_FDSet *fds2)
1431 nfds = GNUNET_MIN (fds1->nsds,
1436 if ( (FD_ISSET (nfds,
1447 /* This code is somewhat hacky, we are not supposed to know what's
1448 * inside of fd_set; also the O(n^2) is really bad... */
1449 for (i = 0; i < fds1->sds.fd_count; i++)
1450 for (j = 0; j < fds2->sds.fd_count; j++)
1451 if (fds1->sds.fd_array[i] == fds2->sds.fd_array[j])
1454 /* take a short cut if possible */
1455 if ( (0 == fds1->handles_pos) ||
1456 (0 == fds2->handles_pos) )
1459 /* Sort file handles array to avoid quadratic complexity when
1460 checking for overlap */
1461 qsort (fds1->handles,
1465 qsort (fds2->handles,
1471 while ( (i < fds1->handles_pos) &&
1472 (j < fds2->handles_pos) )
1474 switch (ptr_cmp (fds1->handles[i],
1494 * @return a new fd set
1496 struct GNUNET_NETWORK_FDSet *
1497 GNUNET_NETWORK_fdset_create ()
1499 struct GNUNET_NETWORK_FDSet *fds;
1501 fds = GNUNET_new (struct GNUNET_NETWORK_FDSet);
1502 GNUNET_NETWORK_fdset_zero (fds);
1508 * Releases the associated memory of an fd set
1513 GNUNET_NETWORK_fdset_destroy (struct GNUNET_NETWORK_FDSet *fds)
1516 GNUNET_array_grow (fds->handles,
1528 struct _select_params
1546 * Timeout for select().
1563 SOCKET wakeup_socket;
1566 * Set to return value from select.
1576 _selector (LPVOID p)
1578 struct _select_params *sp = p;
1582 WaitForSingleObject (sp->standby,
1584 ResetEvent (sp->standby);
1585 sp->status = select (1,
1590 if (FD_ISSET (sp->wakeup_socket,
1593 FD_CLR (sp->wakeup_socket,
1597 SetEvent (sp->wakeup);
1603 static HANDLE hEventPipeWrite;
1605 static HANDLE hEventReadReady;
1607 static struct _select_params sp;
1609 static HANDLE select_thread;
1611 static HANDLE select_finished_event;
1613 static HANDLE select_standby_event;
1615 static SOCKET select_wakeup_socket = -1;
1617 static SOCKET select_send_socket = -1;
1619 static struct timeval select_timeout;
1623 * On W32, we actually use a thread to help with the
1624 * event loop due to W32-API limitations. This function
1625 * initializes that thread.
1628 initialize_select_thread ()
1630 SOCKET select_listening_socket = -1;
1631 struct sockaddr_in s_in;
1636 select_standby_event = CreateEvent (NULL, TRUE, FALSE, NULL);
1637 select_finished_event = CreateEvent (NULL, TRUE, FALSE, NULL);
1639 select_wakeup_socket = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
1641 select_listening_socket = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
1644 res = ioctlsocket (select_wakeup_socket, FIONBIO, &p);
1645 LOG (GNUNET_ERROR_TYPE_DEBUG,
1646 "Select thread initialization: ioctlsocket() returns %d\n",
1649 alen = sizeof (s_in);
1650 s_in.sin_family = AF_INET;
1652 s_in.sin_addr.S_un.S_un_b.s_b1 = 127;
1653 s_in.sin_addr.S_un.S_un_b.s_b2 = 0;
1654 s_in.sin_addr.S_un.S_un_b.s_b3 = 0;
1655 s_in.sin_addr.S_un.S_un_b.s_b4 = 1;
1656 res = bind (select_listening_socket,
1657 (const struct sockaddr *) &s_in,
1659 LOG (GNUNET_ERROR_TYPE_DEBUG,
1660 "Select thread initialization: bind() returns %d\n",
1663 res = getsockname (select_listening_socket,
1664 (struct sockaddr *) &s_in,
1666 LOG (GNUNET_ERROR_TYPE_DEBUG,
1667 "Select thread initialization: getsockname() returns %d\n",
1670 res = listen (select_listening_socket,
1672 LOG (GNUNET_ERROR_TYPE_DEBUG,
1673 "Select thread initialization: listen() returns %d\n",
1675 res = connect (select_wakeup_socket,
1676 (const struct sockaddr *) &s_in,
1678 LOG (GNUNET_ERROR_TYPE_DEBUG,
1679 "Select thread initialization: connect() returns %d\n",
1682 select_send_socket = accept (select_listening_socket,
1683 (struct sockaddr *) &s_in,
1686 closesocket (select_listening_socket);
1688 sp.wakeup = select_finished_event;
1689 sp.standby = select_standby_event;
1690 sp.wakeup_socket = select_wakeup_socket;
1692 select_thread = CreateThread (NULL,
1705 * Check if sockets or pipes meet certain conditions
1707 * @param rfds set of sockets or pipes to be checked for readability
1708 * @param wfds set of sockets or pipes to be checked for writability
1709 * @param efds set of sockets or pipes to be checked for exceptions
1710 * @param timeout relative value when to return
1711 * @return number of selected sockets or pipes, #GNUNET_SYSERR on error
1714 GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
1715 struct GNUNET_NETWORK_FDSet *wfds,
1716 struct GNUNET_NETWORK_FDSet *efds,
1717 const struct GNUNET_TIME_Relative timeout)
1727 nfds = GNUNET_MAX (nfds,
1730 nfds = GNUNET_MAX (nfds,
1733 (timeout.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us))
1736 LOG (GNUNET_ERROR_TYPE_ERROR,
1737 _("Fatal internal logic error, process hangs in `%s' (abort with CTRL-C)!\n"),
1740 tv.tv_sec = timeout.rel_value_us / GNUNET_TIME_UNIT_SECONDS.rel_value_us;
1742 (timeout.rel_value_us -
1743 (tv.tv_sec * GNUNET_TIME_UNIT_SECONDS.rel_value_us));
1744 return select (nfds,
1745 (NULL != rfds) ? &rfds->sds : NULL,
1746 (NULL != wfds) ? &wfds->sds : NULL,
1747 (NULL != efds) ? &efds->sds : NULL,
1748 (timeout.rel_value_us ==
1749 GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us) ? NULL : &tv);
1758 * Non-blocking test if a pipe is ready for reading.
1760 * @param fh pipe handle
1761 * @return #GNUNET_YES if the pipe is ready for reading
1764 pipe_read_ready (const struct GNUNET_DISK_FileHandle *fh)
1768 DWORD waitstatus = 0;
1771 bret = PeekNamedPipe (fh->h, NULL, 0, NULL, &waitstatus, NULL);
1772 error = GetLastError ();
1775 /* TODO: either add more errors to this condition, or eliminate it
1776 * entirely (failed to peek -> pipe is in serious trouble, should
1777 * be selected as readable).
1779 if ( (error != ERROR_BROKEN_PIPE) &&
1780 (error != ERROR_INVALID_HANDLE) )
1783 else if (waitstatus <= 0)
1790 * Non-blocking test if a pipe is having an IO exception.
1792 * @param fh pipe handle
1793 * @return #GNUNET_YES if the pipe is having an IO exception.
1796 pipe_except_ready (const struct GNUNET_DISK_FileHandle *fh)
1800 if (PeekNamedPipe (fh->h, NULL, 0, NULL, &dwBytes, NULL))
1807 * Iterate over handles in fds, destructively rewrite the
1808 * handles array contents of fds so that it starts with the
1809 * handles that are ready, and update handles_pos accordingly.
1811 * @param fds set of handles (usually pipes) to be checked for readiness
1812 * @param except GNUNET_NO if fds should be checked for readiness to read,
1813 * GNUNET_YES if fds should be checked for exceptions
1814 * (there is no way to check for write-readiness - pipes are always write-ready)
1815 * @param set_for_sure a HANDLE that is known to be set already,
1816 * because WaitForMultipleObjects() returned its index.
1817 * @return number of ready handles
1820 check_handles_status (struct GNUNET_NETWORK_FDSet *fds,
1822 HANDLE set_for_sure)
1824 const struct GNUNET_DISK_FileHandle *fh;
1828 for (woff = 0, roff = 0; roff < fds->handles_pos; roff++)
1830 fh = fds->handles[roff];
1831 if (fh == set_for_sure)
1833 fds->handles[woff++] = fh;
1835 else if (fh->type == GNUNET_DISK_HANLDE_TYPE_PIPE)
1837 if ((except && pipe_except_ready (fh)) ||
1838 (!except && pipe_read_ready (fh)))
1839 fds->handles[woff++] = fh;
1841 else if (fh->type == GNUNET_DISK_HANLDE_TYPE_FILE)
1844 fds->handles[woff++] = fh;
1848 if (WAIT_OBJECT_0 == WaitForSingleObject (fh->h, 0))
1849 fds->handles[woff++] = fh;
1852 fds->handles_pos = woff;
1858 * Check if sockets or pipes meet certain conditions, version for W32.
1860 * @param rfds set of sockets or pipes to be checked for readability
1861 * @param wfds set of sockets or pipes to be checked for writability
1862 * @param efds set of sockets or pipes to be checked for exceptions
1863 * @param timeout relative value when to return
1864 * @return number of selected sockets or pipes, #GNUNET_SYSERR on error
1867 GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
1868 struct GNUNET_NETWORK_FDSet *wfds,
1869 struct GNUNET_NETWORK_FDSet *efds,
1870 const struct GNUNET_TIME_Relative timeout)
1872 const struct GNUNET_DISK_FileHandle *fh;
1881 HANDLE handle_array[FD_SETSIZE + 2];
1883 int returnedpos = 0;
1893 nfds = GNUNET_MAX (nfds, rfds->nsds);
1894 handles += rfds->handles_pos;
1898 nfds = GNUNET_MAX (nfds, wfds->nsds);
1899 handles += wfds->handles_pos;
1903 nfds = GNUNET_MAX (nfds, efds->nsds);
1904 handles += efds->handles_pos;
1908 (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == timeout.rel_value_us) &&
1912 LOG (GNUNET_ERROR_TYPE_ERROR,
1913 _("Fatal internal logic error, process hangs in `%s' (abort with CTRL-C)!\n"),
1916 #define SAFE_FD_ISSET(fd, set) (set != NULL && FD_ISSET(fd, set))
1917 /* calculate how long we need to wait in microseconds */
1918 if (timeout.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
1920 mcs_total = INFINITE;
1921 ms_rounded = INFINITE;
1925 mcs_total = timeout.rel_value_us / GNUNET_TIME_UNIT_MICROSECONDS.rel_value_us;
1926 ms_rounded = (DWORD) (mcs_total / GNUNET_TIME_UNIT_MILLISECONDS.rel_value_us);
1927 if (mcs_total > 0 && ms_rounded == 0)
1930 /* select() may be used as a portable way to sleep */
1931 if (! (rfds || wfds || efds))
1937 if (NULL == select_thread)
1938 initialize_select_thread ();
1944 FD_COPY (&rfds->sds, &aread);
1946 FD_COPY (&wfds->sds, &awrite);
1948 FD_COPY (&efds->sds, &aexcept);
1950 /* Start by doing a fast check on sockets and pipes (without
1951 waiting). It is cheap, and is sufficient most of the time. By
1952 profiling we detected that to be true in 90% of the cases.
1955 /* Do the select now */
1956 select_timeout.tv_sec = 0;
1957 select_timeout.tv_usec = 0;
1959 /* Copy all the writes to the except, so we can detect connect() errors */
1960 for (i = 0; i < awrite.fd_count; i++)
1961 FD_SET (awrite.fd_array[i],
1963 if ( (aread.fd_count > 0) ||
1964 (awrite.fd_count > 0) ||
1965 (aexcept.fd_count > 0) )
1966 selectret = select (1,
1967 (NULL != rfds) ? &aread : NULL,
1968 (NULL != wfds) ? &awrite : NULL,
1973 if (-1 == selectret)
1975 /* Throw an error early on, while we still have the context. */
1976 LOG (GNUNET_ERROR_TYPE_ERROR,
1977 "W32 select(%d, %d, %d) failed: %lu\n",
1978 rfds ? aread.fd_count : 0,
1979 wfds ? awrite.fd_count : 0,
1985 /* Check aexcept, if something is in there and we copied that
1986 FD before to detect connect() errors, add it back to the
1987 write set to report errors. */
1989 for (i = 0; i < aexcept.fd_count; i++)
1990 if (FD_ISSET (aexcept.fd_array[i],
1992 FD_SET (aexcept.fd_array[i],
1996 /* If our select returned something or is a 0-timed request, then
1997 also check the pipes and get out of here! */
1998 /* Sadly, it means code duplication :( */
1999 if ( (selectret > 0) || (0 == mcs_total) )
2004 if (rfds && (rfds->handles_pos > 0))
2005 retcode += check_handles_status (rfds, GNUNET_NO, NULL);
2007 /* wfds handles remain untouched, on W32
2008 we pretend our pipes are "always" write-ready */
2011 if (efds && (efds->handles_pos > 0))
2012 retcode += check_handles_status (efds, GNUNET_YES, NULL);
2016 GNUNET_NETWORK_fdset_zero (rfds);
2017 if (selectret != -1)
2018 GNUNET_NETWORK_fdset_copy_native (rfds, &aread, selectret);
2022 GNUNET_NETWORK_fdset_zero (wfds);
2023 if (selectret != -1)
2024 GNUNET_NETWORK_fdset_copy_native (wfds, &awrite, selectret);
2028 GNUNET_NETWORK_fdset_zero (efds);
2029 if (selectret != -1)
2030 GNUNET_NETWORK_fdset_copy_native (efds, &aexcept, selectret);
2032 if (-1 == selectret)
2034 /* Add our select() FDs to the total return value */
2035 retcode += selectret;
2039 /* If we got this far, use slower implementation that is able to do a waiting select
2040 on both sockets and pipes simultaneously */
2042 /* Events for pipes */
2043 if (! hEventReadReady)
2044 hEventReadReady = CreateEvent (NULL, TRUE, TRUE, NULL);
2045 if (! hEventPipeWrite)
2046 hEventPipeWrite = CreateEvent (NULL, TRUE, TRUE, NULL);
2053 FD_COPY (&rfds->sds, &aread);
2055 FD_COPY (&wfds->sds, &awrite);
2057 FD_COPY (&efds->sds, &aexcept);
2058 /* We will first Add the PIPES to the events */
2059 /* Track how far in `handle_array` the read pipes go,
2060 so we may by-pass them quickly if none of them
2063 if (rfds && (rfds->handles_pos > 0))
2065 for (i = 0; i <rfds->handles_pos; i++)
2067 fh = rfds->handles[i];
2068 if (fh->type == GNUNET_DISK_HANLDE_TYPE_EVENT)
2070 handle_array[nhandles++] = fh->h;
2073 if (fh->type != GNUNET_DISK_HANLDE_TYPE_PIPE)
2075 /* Read zero bytes to check the status of the pipe */
2076 if (! ReadFile (fh->h, NULL, 0, NULL, fh->oOverlapRead))
2078 DWORD error_code = GetLastError ();
2080 if (error_code == ERROR_IO_PENDING)
2082 /* add as unready */
2083 handle_array[nhandles++] = fh->oOverlapRead->hEvent;
2089 handle_array[nhandles++] = hEventReadReady;
2095 /* error also counts as ready */
2096 handle_array[nhandles++] = hEventReadReady;
2102 if (wfds && (wfds->handles_pos > 0))
2104 LOG (GNUNET_ERROR_TYPE_DEBUG,
2105 "Adding the write ready event to the array as %d\n",
2107 handle_array[nhandles++] = hEventPipeWrite;
2113 LOG (GNUNET_ERROR_TYPE_DEBUG,
2114 "Adding the socket event to the array as %d\n",
2116 handle_array[nhandles++] = select_finished_event;
2117 if (timeout.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
2123 select_timeout.tv_sec = timeout.rel_value_us / GNUNET_TIME_UNIT_SECONDS.rel_value_us;
2124 select_timeout.tv_usec = (timeout.rel_value_us -
2125 (select_timeout.tv_sec *
2126 GNUNET_TIME_UNIT_SECONDS.rel_value_us));
2127 sp.tv = &select_timeout;
2129 FD_SET (select_wakeup_socket, &aread);
2132 i = recv (select_wakeup_socket,
2133 (char *) &returnedpos,
2140 /* Failed connections cause sockets to be set in errorfds on W32,
2141 * but on POSIX it should set them in writefds.
2142 * First copy all awrite sockets to aexcept, later we'll
2143 * check aexcept and set its contents in awrite as well
2144 * Sockets are also set in errorfds when OOB data is available,
2145 * but we don't use OOB data.
2147 for (i = 0; i < awrite.fd_count; i++)
2148 FD_SET (awrite.fd_array[i],
2150 ResetEvent (select_finished_event);
2151 SetEvent (select_standby_event);
2154 /* NULL-terminate array */
2155 handle_array[nhandles] = NULL;
2156 LOG (GNUNET_ERROR_TYPE_DEBUG,
2157 "nfds: %d, handles: %d, will wait: %llu mcs\n",
2164 = WaitForMultipleObjects (nhandles,
2168 LOG (GNUNET_ERROR_TYPE_DEBUG,
2169 "WaitForMultipleObjects Returned: %d\n",
2174 GNUNET_break (0); /* This branch shouldn't actually be executed...*/
2175 i = (int) WaitForSingleObject (select_finished_event,
2177 returncode = WAIT_TIMEOUT;
2181 /* Shouldn't come this far. If it does - investigate. */
2187 /* Don't wake up select-thread when delay is 0, it should return immediately
2188 * and wake up by itself.
2191 i = send (select_send_socket,
2192 (const char *) &returnedpos,
2195 i = (int) WaitForSingleObject (select_finished_event,
2197 LOG (GNUNET_ERROR_TYPE_DEBUG,
2198 "Finished waiting for the select thread: %d %d\n",
2205 i = recv (select_wakeup_socket,
2206 (char *) &returnedpos,
2210 /* Check aexcept, add its contents to awrite */
2211 for (i = 0; i < aexcept.fd_count; i++)
2212 FD_SET (aexcept.fd_array[i], &awrite);
2215 returnedpos = returncode - WAIT_OBJECT_0;
2216 LOG (GNUNET_ERROR_TYPE_DEBUG,
2217 "return pos is: %d\n",
2222 /* We queued a zero-long read on each pipe to check
2223 * its state, now we must cancel these read operations.
2224 * This must be done while rfds->handles_pos is still
2225 * intact and matches the number of read handles that we
2226 * got from the caller.
2228 for (i = 0; i < rfds->handles_pos; i++)
2230 fh = rfds->handles[i];
2231 if (GNUNET_DISK_HANLDE_TYPE_PIPE == fh->type)
2235 /* We may have some pipes ready for reading. */
2236 if (returnedpos < read_pipes_off)
2237 retcode += check_handles_status (rfds, GNUNET_NO, handle_array[returnedpos]);
2239 rfds->handles_pos = 0;
2241 if (-1 != sp.status)
2242 GNUNET_NETWORK_fdset_copy_native (rfds, &aread, retcode);
2246 retcode += wfds->handles_pos;
2247 /* wfds handles remain untouched */
2248 if (-1 != sp.status)
2249 GNUNET_NETWORK_fdset_copy_native (wfds, &awrite, retcode);
2253 retcode += check_handles_status (rfds,
2255 returnedpos < nhandles ? handle_array[returnedpos] : NULL);
2256 if (-1 != sp.status)
2257 GNUNET_NETWORK_fdset_copy_native (efds, &aexcept, retcode);
2261 retcode += sp.status;
2269 /* end of network.c */