2 This file is part of GNUnet.
3 Copyright (C) 2009-2013 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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-network", __VA_ARGS__)
32 #define LOG_STRERROR_FILE(kind, syscall, \
33 filename) GNUNET_log_from_strerror_file (kind, \
37 #define LOG_STRERROR(kind, syscall) GNUNET_log_from_strerror (kind, \
41 #define DEBUG_NETWORK GNUNET_EXTRA_LOGGING
44 #ifndef INVALID_SOCKET
45 #define INVALID_SOCKET -1
50 * @brief handle to a socket
52 struct GNUNET_NETWORK_Handle
57 * Address family / domain.
67 * Number of bytes in addr.
72 * Address we were bound to, or NULL.
74 struct sockaddr *addr;
79 * Test if the given protocol family is supported by this system.
81 * @param pf protocol family to test (PF_INET, PF_INET6, PF_UNIX)
82 * @return #GNUNET_OK if the PF is supported
85 GNUNET_NETWORK_test_pf (int pf)
87 static int cache_v4 = -1;
88 static int cache_v6 = -1;
89 static int cache_un = -1;
112 s = socket (pf, SOCK_STREAM, 0);
115 if (EAFNOSUPPORT != errno)
117 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
119 return GNUNET_SYSERR;
149 * Given a unixpath that is too long (larger than UNIX_PATH_MAX),
150 * shorten it to an acceptable length while keeping it unique
151 * and making sure it remains a valid filename (if possible).
153 * @param unixpath long path, will be freed (or same pointer returned
154 * with moved 0-termination).
155 * @return shortened unixpath, NULL on error
158 GNUNET_NETWORK_shorten_unixpath (char *unixpath)
160 struct sockaddr_un dummy;
163 struct GNUNET_HashCode sh;
164 struct GNUNET_CRYPTO_HashAsciiEncoded ae;
167 upm = sizeof(dummy.sun_path);
168 slen = strlen (unixpath);
170 return unixpath; /* no shortening required */
171 GNUNET_CRYPTO_hash (unixpath, slen, &sh);
172 while (16 + strlen (unixpath) >= upm)
174 if (NULL == (end = strrchr (unixpath, '/')))
176 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
178 "Unable to shorten unix path `%s' while keeping name unique\n"),
180 GNUNET_free (unixpath);
185 GNUNET_CRYPTO_hash_to_enc (&sh, &ae);
186 ae.encoding[16] = '\0';
187 strcat (unixpath, (char *) ae.encoding);
193 * If services crash, they can leave a unix domain socket file on the
194 * disk. This needs to be manually removed, because otherwise both
195 * bind() and connect() for the respective address will fail. In this
196 * function, we test if such a left-over file exists, and if so,
197 * remove it (unless there is a listening service at the address).
199 * @param un unix domain socket address to check
202 GNUNET_NETWORK_unix_precheck (const struct sockaddr_un *un)
209 s = socket (AF_UNIX, SOCK_STREAM, 0);
212 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
213 "Failed to open AF_UNIX socket");
217 (struct sockaddr *) un,
218 sizeof(struct sockaddr_un));
220 GNUNET_break (0 == close (s));
222 return; /* another process is listening, do not remove! */
223 if (ECONNREFUSED != eno)
224 return; /* some other error, likely "no such file or directory" -- all well */
225 /* should unlink, but sanity checks first */
226 if (0 != stat (un->sun_path,
228 return; /* failed to 'stat', likely does not exist after all */
229 if (S_IFSOCK != (S_IFMT & sbuf.st_mode))
230 return; /* refuse to unlink anything except sockets */
231 /* finally, really unlink */
232 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
233 "Removing left-over `%s' from previous exeuction\n",
235 if (0 != unlink (un->sun_path))
236 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
243 #define FD_COPY(s, d) do { GNUNET_memcpy ((d), (s), sizeof(fd_set)); } while (0)
248 * Set if a socket should use blocking or non-blocking IO.
251 * @param doBlock blocking mode
252 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
255 GNUNET_NETWORK_socket_set_blocking (struct GNUNET_NETWORK_Handle *fd,
258 int flags = fcntl (fd->fd, F_GETFL);
262 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
264 return GNUNET_SYSERR;
267 flags &= ~O_NONBLOCK;
271 if (0 != fcntl (fd->fd,
276 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
278 return GNUNET_SYSERR;
285 * Make a socket non-inheritable to child processes
287 * @param h the socket to make non-inheritable
288 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
289 * @warning Not implemented on Windows
292 socket_set_inheritable (const struct GNUNET_NETWORK_Handle *h)
295 i = fcntl (h->fd, F_GETFD);
297 return GNUNET_SYSERR;
298 if (i == (i | FD_CLOEXEC))
301 if (fcntl (h->fd, F_SETFD, i) < 0)
302 return GNUNET_SYSERR;
310 * The MSG_NOSIGNAL equivalent on Mac OS X
312 * @param h the socket to make non-delaying
315 socket_set_nosigpipe (const struct GNUNET_NETWORK_Handle *h)
320 setsockopt (h->fd, SOL_SOCKET, SO_NOSIGPIPE,
321 (const void *) &abs_value,
323 return GNUNET_SYSERR;
332 * Disable delays when sending data via the socket.
333 * (GNUnet makes sure that messages are as big as
336 * @param h the socket to make non-delaying
339 socket_set_nodelay (const struct GNUNET_NETWORK_Handle *h)
347 &value, sizeof(value)))
348 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
354 * Perform proper canonical initialization for a network handle.
355 * Set it to non-blocking, make it non-inheritable to child
356 * processes, disable SIGPIPE, enable "nodelay" (if non-UNIX
357 * stream socket) and check that it is smaller than FD_SETSIZE.
359 * @param h socket to initialize
360 * @param af address family of the socket
361 * @param type socket type
362 * @return #GNUNET_OK on success, #GNUNET_SYSERR if initialization
363 * failed and the handle was destroyed
366 initialize_network_handle (struct GNUNET_NETWORK_Handle *h,
374 if (h->fd == INVALID_SOCKET)
379 return GNUNET_SYSERR;
382 if (h->fd >= FD_SETSIZE)
384 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (h));
386 return GNUNET_SYSERR;
389 if (GNUNET_OK != socket_set_inheritable (h))
390 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
391 "socket_set_inheritable");
393 if (GNUNET_SYSERR == GNUNET_NETWORK_socket_set_blocking (h, GNUNET_NO))
397 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (h));
399 return GNUNET_SYSERR;
402 if (GNUNET_SYSERR == socket_set_nosigpipe (h))
406 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (h));
408 return GNUNET_SYSERR;
411 if ((type == SOCK_STREAM)
416 socket_set_nodelay (h);
422 * accept a new connection on a socket
424 * @param desc bound socket
425 * @param address address of the connecting peer, may be NULL
426 * @param address_len length of @a address
427 * @return client socket
429 struct GNUNET_NETWORK_Handle *
430 GNUNET_NETWORK_socket_accept (const struct GNUNET_NETWORK_Handle *desc,
431 struct sockaddr *address,
432 socklen_t *address_len)
434 struct GNUNET_NETWORK_Handle *ret;
437 ret = GNUNET_new (struct GNUNET_NETWORK_Handle);
440 struct sockaddr_storage name;
441 socklen_t namelen = sizeof(name);
443 int gsn = getsockname (desc->fd,
444 (struct sockaddr *) &name,
448 LOG (GNUNET_ERROR_TYPE_DEBUG,
449 "Accepting connection on `%s'\n",
450 GNUNET_a2s ((const struct sockaddr *) &name,
454 ret->fd = accept (desc->fd,
465 initialize_network_handle (ret,
466 (NULL != address) ? address->sa_family :
477 * Bind a socket to a particular address.
479 * @param desc socket to bind
480 * @param address address to be bound
481 * @param address_len length of @a address
482 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
485 GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc,
486 const struct sockaddr *address,
487 socklen_t address_len)
496 if (AF_INET6 == desc->af)
497 if (setsockopt (desc->fd,
502 LOG_STRERROR (GNUNET_ERROR_TYPE_DEBUG,
507 if (AF_UNIX == address->sa_family)
508 GNUNET_NETWORK_unix_precheck ((const struct sockaddr_un *) address);
512 /* This is required here for TCP sockets, but only on UNIX */
513 if ((SOCK_STREAM == desc->type) &&
514 (0 != setsockopt (desc->fd,
518 LOG_STRERROR (GNUNET_ERROR_TYPE_DEBUG,
522 /* set permissions of newly created non-abstract UNIX domain socket to
523 "user-only"; applications can choose to relax this later */
524 mode_t old_mask = 0; /* assigned to make compiler happy */
525 const struct sockaddr_un *un = (const struct sockaddr_un *) address;
526 int not_abstract = 0;
528 if ((AF_UNIX == address->sa_family)
529 && ('\0' != un->sun_path[0])) /* Not an abstract socket */
532 old_mask = umask (S_IWGRP | S_IRGRP | S_IXGRP | S_IWOTH | S_IROTH
535 ret = bind (desc->fd,
540 (void) umask (old_mask);
543 return GNUNET_SYSERR;
545 desc->addr = GNUNET_malloc (address_len);
546 GNUNET_memcpy (desc->addr, address, address_len);
547 desc->addrlen = address_len;
557 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
560 GNUNET_NETWORK_socket_close (struct GNUNET_NETWORK_Handle *desc)
564 ret = close (desc->fd);
566 const struct sockaddr_un *un = (const struct sockaddr_un *) desc->addr;
568 /* Cleanup the UNIX domain socket and its parent directories in case of non
570 if ((AF_UNIX == desc->af) &&
571 (NULL != desc->addr) &&
572 ('\0' != un->sun_path[0]))
574 char *dirname = GNUNET_strndup (un->sun_path,
575 sizeof(un->sun_path));
577 if (0 != unlink (dirname))
579 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING,
587 len = strlen (dirname);
588 while ((len > 0) && (dirname[len] != DIR_SEPARATOR))
591 if ((0 != len) && (0 != rmdir (dirname)))
598 /* these are normal and can just be ignored */
602 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
609 GNUNET_free (dirname);
611 GNUNET_NETWORK_socket_free_memory_only_ (desc);
612 return (ret == 0) ? GNUNET_OK : GNUNET_SYSERR;
617 * Only free memory of a socket, keep the file descriptor untouched.
622 GNUNET_NETWORK_socket_free_memory_only_ (struct GNUNET_NETWORK_Handle *desc)
624 GNUNET_free_non_null (desc->addr);
630 * Box a native socket (and check that it is a socket).
632 * @param fd socket to box
633 * @return NULL on error (including not supported on target platform)
635 struct GNUNET_NETWORK_Handle *
636 GNUNET_NETWORK_socket_box_native (int fd)
638 struct GNUNET_NETWORK_Handle *ret;
640 if (fcntl (fd, F_GETFD) < 0)
641 return NULL; /* invalid FD */
642 ret = GNUNET_new (struct GNUNET_NETWORK_Handle);
650 * Connect a socket to some remote address.
653 * @param address peer address
654 * @param address_len length of @a address
655 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
658 GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Handle *desc,
659 const struct sockaddr *address,
660 socklen_t address_len)
664 ret = connect (desc->fd,
668 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
676 * @param level protocol level of the option
677 * @param optname identifier of the option
678 * @param optval options
679 * @param optlen length of @a optval
680 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
683 GNUNET_NETWORK_socket_getsockopt (const struct GNUNET_NETWORK_Handle *desc,
691 ret = getsockopt (desc->fd,
696 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
704 * @param backlog length of the listen queue
705 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
708 GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Handle *desc,
713 ret = listen (desc->fd,
716 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
721 * How much data is available to be read on this descriptor?
724 * @returns #GNUNET_SYSERR if no data is available, or on error!
727 GNUNET_NETWORK_socket_recvfrom_amount (const struct GNUNET_NETWORK_Handle *desc)
731 /* How much is there to be read? */
734 error = ioctl (desc->fd,
738 return (ssize_t) pending;
739 return GNUNET_SYSERR;
744 * Read data from a socket (always non-blocking).
747 * @param buffer buffer
748 * @param length length of @a buffer
749 * @param src_addr either the source to recv from, or all zeroes
750 * to be filled in by recvfrom
751 * @param addrlen length of the @a src_addr
754 GNUNET_NETWORK_socket_recvfrom (const struct GNUNET_NETWORK_Handle *desc,
757 struct sockaddr *src_addr,
766 flags |= MSG_DONTWAIT;
768 ret = recvfrom (desc->fd,
779 * Read data from a connected socket (always non-blocking).
782 * @param buffer buffer
783 * @param length length of @a buffer
784 * @return number of bytes received, -1 on error
787 GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Handle *desc,
797 flags |= MSG_DONTWAIT;
799 ret = recv (desc->fd,
808 * Send data (always non-blocking).
811 * @param buffer data to send
812 * @param length size of the @a buffer
813 * @return number of bytes sent, #GNUNET_SYSERR on error
816 GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Handle *desc,
825 flags |= MSG_DONTWAIT;
828 flags |= MSG_NOSIGNAL;
830 ret = send (desc->fd,
839 * Send data to a particular destination (always non-blocking).
840 * This function only works for UDP sockets.
843 * @param message data to send
844 * @param length size of the @a message
845 * @param dest_addr destination address
846 * @param dest_len length of @a address
847 * @return number of bytes sent, #GNUNET_SYSERR on error
850 GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Handle *desc,
853 const struct sockaddr *dest_addr,
862 flags |= MSG_DONTWAIT;
865 flags |= MSG_NOSIGNAL;
867 ret = sendto (desc->fd, message, length, flags, dest_addr, dest_len);
876 * @param level protocol level of the option
877 * @param option_name option identifier
878 * @param option_value value to set
879 * @param option_len size of @a option_value
880 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
883 GNUNET_NETWORK_socket_setsockopt (struct GNUNET_NETWORK_Handle *fd,
886 const void *option_value,
887 socklen_t option_len)
891 ret = setsockopt (fd->fd,
897 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
902 * Create a new socket. Configure it for non-blocking IO and
903 * mark it as non-inheritable to child processes (set the
904 * close-on-exec flag).
906 * @param domain domain of the socket
907 * @param type socket type
908 * @param protocol network protocol
909 * @return new socket, NULL on error
911 struct GNUNET_NETWORK_Handle *
912 GNUNET_NETWORK_socket_create (int domain,
916 struct GNUNET_NETWORK_Handle *ret;
919 fd = socket (domain, type, protocol);
922 ret = GNUNET_new (struct GNUNET_NETWORK_Handle);
925 initialize_network_handle (ret,
934 * Shut down socket operations
936 * @param how type of shutdown
937 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
940 GNUNET_NETWORK_socket_shutdown (struct GNUNET_NETWORK_Handle *desc,
945 ret = shutdown (desc->fd, how);
947 return (0 == ret) ? GNUNET_OK : GNUNET_SYSERR;
952 * Disable the "CORK" feature for communication with the given socket,
953 * forcing the OS to immediately flush the buffer on transmission
954 * instead of potentially buffering multiple messages. Essentially
955 * reduces the OS send buffers to zero.
958 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
961 GNUNET_NETWORK_socket_disable_corking (struct GNUNET_NETWORK_Handle *desc)
970 setsockopt (desc->fd,
975 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
979 setsockopt (desc->fd,
984 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
987 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
997 GNUNET_NETWORK_fdset_zero (struct GNUNET_NETWORK_FDSet *fds)
1005 * Add a socket to the FD set
1008 * @param desc socket to add
1011 GNUNET_NETWORK_fdset_set (struct GNUNET_NETWORK_FDSet *fds,
1012 const struct GNUNET_NETWORK_Handle *desc)
1016 fds->nsds = GNUNET_MAX (fds->nsds,
1022 * Check whether a socket is part of the fd set
1025 * @param desc socket
1026 * @return 0 if the FD is not set
1029 GNUNET_NETWORK_fdset_isset (const struct GNUNET_NETWORK_FDSet *fds,
1030 const struct GNUNET_NETWORK_Handle *desc)
1032 return FD_ISSET (desc->fd,
1038 * Add one fd set to another
1040 * @param dst the fd set to add to
1041 * @param src the fd set to add from
1044 GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst,
1045 const struct GNUNET_NETWORK_FDSet *src)
1049 for (nfds = src->nsds; nfds >= 0; nfds--)
1050 if (FD_ISSET (nfds, &src->sds))
1051 FD_SET (nfds, &dst->sds);
1052 dst->nsds = GNUNET_MAX (dst->nsds,
1058 * Copy one fd set to another
1060 * @param to destination
1061 * @param from source
1064 GNUNET_NETWORK_fdset_copy (struct GNUNET_NETWORK_FDSet *to,
1065 const struct GNUNET_NETWORK_FDSet *from)
1067 FD_COPY (&from->sds,
1069 to->nsds = from->nsds;
1074 * Return file descriptor for this network handle
1076 * @param desc wrapper to process
1077 * @return POSIX file descriptor
1080 GNUNET_NETWORK_get_fd (const struct GNUNET_NETWORK_Handle *desc)
1087 * Return sockaddr for this network handle
1089 * @param desc wrapper to process
1093 GNUNET_NETWORK_get_addr (const struct GNUNET_NETWORK_Handle *desc)
1100 * Return sockaddr length for this network handle
1102 * @param desc wrapper to process
1103 * @return socklen_t for sockaddr
1106 GNUNET_NETWORK_get_addrlen (const struct GNUNET_NETWORK_Handle *desc)
1108 return desc->addrlen;
1113 * Copy a native fd set
1115 * @param to destination
1116 * @param from native source set
1117 * @param nfds the biggest socket number in from + 1
1120 GNUNET_NETWORK_fdset_copy_native (struct GNUNET_NETWORK_FDSet *to,
1131 * Set a native fd in a set
1133 * @param to destination
1134 * @param nfd native FD to set
1137 GNUNET_NETWORK_fdset_set_native (struct GNUNET_NETWORK_FDSet *to,
1140 GNUNET_assert ((nfd >= 0) && (nfd < FD_SETSIZE));
1141 FD_SET (nfd, &to->sds);
1142 to->nsds = GNUNET_MAX (nfd + 1,
1148 * Test native fd in a set
1150 * @param to set to test, NULL for empty set
1151 * @param nfd native FD to test, or -1 for none
1152 * @return #GNUNET_YES if FD is set in the set
1155 GNUNET_NETWORK_fdset_test_native (const struct GNUNET_NETWORK_FDSet *to,
1161 return FD_ISSET (nfd, &to->sds) ? GNUNET_YES : GNUNET_NO;
1166 * Add a file handle to the fd set
1168 * @param h the file handle to add
1171 GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds,
1172 const struct GNUNET_DISK_FileHandle *h)
1176 GNUNET_assert (GNUNET_OK ==
1177 GNUNET_DISK_internal_file_handle_ (h,
1182 fds->nsds = GNUNET_MAX (fd + 1,
1188 * Add a file handle to the fd set
1190 * @param h the file handle to add
1193 GNUNET_NETWORK_fdset_handle_set_first (struct GNUNET_NETWORK_FDSet *fds,
1194 const struct GNUNET_DISK_FileHandle *h)
1196 GNUNET_NETWORK_fdset_handle_set (fds, h);
1201 * Check if a file handle is part of an fd set
1204 * @param h file handle
1205 * @return #GNUNET_YES if the file handle is part of the set
1208 GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds,
1209 const struct GNUNET_DISK_FileHandle *h)
1211 return FD_ISSET (h->fd,
1217 * Checks if two fd sets overlap
1219 * @param fds1 first fd set
1220 * @param fds2 second fd set
1221 * @return #GNUNET_YES if they do overlap, #GNUNET_NO otherwise
1224 GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1,
1225 const struct GNUNET_NETWORK_FDSet *fds2)
1229 nfds = GNUNET_MIN (fds1->nsds,
1234 if ((FD_ISSET (nfds,
1247 * @return a new fd set
1249 struct GNUNET_NETWORK_FDSet *
1250 GNUNET_NETWORK_fdset_create ()
1252 struct GNUNET_NETWORK_FDSet *fds;
1254 fds = GNUNET_new (struct GNUNET_NETWORK_FDSet);
1255 GNUNET_NETWORK_fdset_zero (fds);
1261 * Releases the associated memory of an fd set
1266 GNUNET_NETWORK_fdset_destroy (struct GNUNET_NETWORK_FDSet *fds)
1273 * Test if the given @a port is available.
1275 * @param ipproto transport protocol to test (i.e. IPPROTO_TCP)
1276 * @param port port number to test
1277 * @return #GNUNET_OK if the port is available, #GNUNET_NO if not
1280 GNUNET_NETWORK_test_port_free (int ipproto,
1283 struct GNUNET_NETWORK_Handle *socket;
1286 char open_port_str[6];
1287 struct addrinfo hint;
1288 struct addrinfo *ret;
1289 struct addrinfo *ai;
1291 GNUNET_snprintf (open_port_str,
1292 sizeof(open_port_str),
1294 (unsigned int) port);
1295 socktype = (IPPROTO_TCP == ipproto) ? SOCK_STREAM : SOCK_DGRAM;
1297 memset (&hint, 0, sizeof(hint));
1298 hint.ai_family = AF_UNSPEC; /* IPv4 and IPv6 */
1299 hint.ai_socktype = socktype;
1300 hint.ai_protocol = ipproto;
1301 hint.ai_addrlen = 0;
1302 hint.ai_addr = NULL;
1303 hint.ai_canonname = NULL;
1304 hint.ai_next = NULL;
1305 hint.ai_flags = AI_PASSIVE | AI_NUMERICSERV; /* Wild card address */
1306 GNUNET_assert (0 == getaddrinfo (NULL,
1310 bind_status = GNUNET_NO;
1311 for (ai = ret; NULL != ai; ai = ai->ai_next)
1313 socket = GNUNET_NETWORK_socket_create (ai->ai_family,
1318 bind_status = GNUNET_NETWORK_socket_bind (socket,
1321 GNUNET_NETWORK_socket_close (socket);
1322 if (GNUNET_OK != bind_status)
1331 * Check if sockets or pipes meet certain conditions
1333 * @param rfds set of sockets or pipes to be checked for readability
1334 * @param wfds set of sockets or pipes to be checked for writability
1335 * @param efds set of sockets or pipes to be checked for exceptions
1336 * @param timeout relative value when to return
1337 * @return number of selected sockets or pipes, #GNUNET_SYSERR on error
1340 GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
1341 struct GNUNET_NETWORK_FDSet *wfds,
1342 struct GNUNET_NETWORK_FDSet *efds,
1343 const struct GNUNET_TIME_Relative timeout)
1353 nfds = GNUNET_MAX (nfds,
1356 nfds = GNUNET_MAX (nfds,
1359 (timeout.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us))
1362 LOG (GNUNET_ERROR_TYPE_ERROR,
1364 "Fatal internal logic error, process hangs in `%s' (abort with CTRL-C)!\n"),
1367 if (timeout.rel_value_us / GNUNET_TIME_UNIT_SECONDS.rel_value_us > (unsigned
1371 tv.tv_sec = LONG_MAX;
1372 tv.tv_usec = 999999L;
1376 tv.tv_sec = (long) (timeout.rel_value_us
1377 / GNUNET_TIME_UNIT_SECONDS.rel_value_us);
1379 (timeout.rel_value_us
1380 - (tv.tv_sec * GNUNET_TIME_UNIT_SECONDS.rel_value_us));
1382 return select (nfds,
1383 (NULL != rfds) ? &rfds->sds : NULL,
1384 (NULL != wfds) ? &wfds->sds : NULL,
1385 (NULL != efds) ? &efds->sds : NULL,
1386 (timeout.rel_value_us ==
1387 GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us) ? NULL : &tv);
1391 /* end of network.c */