2 This file is part of GNUnet.
3 (C) 2009 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 2, 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
28 #include "gnunet_disk_lib.h"
30 #include "gnunet_container_lib.h"
32 #define DEBUG_NETWORK GNUNET_YES
34 #ifndef INVALID_SOCKET
35 #define INVALID_SOCKET -1
39 struct GNUNET_NETWORK_Handle
49 * Address family / domain.
54 * Number of bytes in addr.
59 * Address we were bound to, or NULL.
61 struct sockaddr *addr;
66 struct GNUNET_NETWORK_FDSet
70 * Maximum number of any socket socket descriptor in the set (plus one)
75 * Bitset with the descriptors.
81 * Linked list of handles
83 struct GNUNET_CONTAINER_SList *handles;
89 #define FD_COPY(s, d) (memcpy ((d), (s), sizeof (fd_set)))
94 * Set if a socket should use blocking or non-blocking IO.
96 * @param doBlock blocking mode
97 * @return GNUNET_OK on success, GNUNET_SYSERR on error
100 socket_set_blocking (struct GNUNET_NETWORK_Handle *fd, int doBlock)
106 if (ioctlsocket (fd->fd, FIONBIO, &mode) == SOCKET_ERROR)
109 SetErrnoFromWinsockError (WSAGetLastError ());
110 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "ioctlsocket");
111 return GNUNET_SYSERR;
117 int flags = fcntl (fd->fd, F_GETFL);
121 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "fcntl");
122 return GNUNET_SYSERR;
125 flags &= ~O_NONBLOCK;
129 if (0 != fcntl (fd->fd, F_SETFL, flags))
132 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "fcntl");
133 return GNUNET_SYSERR;
142 * Make a socket non-inheritable to child processes
144 * @param h the socket to make non-inheritable
145 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
146 * @warning Not implemented on Windows
149 socket_set_inheritable (const struct GNUNET_NETWORK_Handle *h)
153 i = fcntl (h->fd, F_GETFD);
155 return GNUNET_SYSERR;
156 if (i == (i | FD_CLOEXEC))
159 if (fcntl (h->fd, F_SETFD, i) < 0)
160 return GNUNET_SYSERR;
168 * The MSG_NOSIGNAL equivalent on Mac OS X
170 * @param h the socket to make non-delaying
173 socket_set_nosigpipe (const struct GNUNET_NETWORK_Handle *h)
177 setsockopt (h->fd, SOL_SOCKET, SO_NOSIGPIPE, &abs_value, sizeof (abs_value)))
178 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
184 * Disable delays when sending data via the socket.
185 * (GNUnet makes sure that messages are as big as
188 * @param h the socket to make non-delaying
191 socket_set_nodelay (const struct GNUNET_NETWORK_Handle *h)
195 if (0 != setsockopt (h->fd, IPPROTO_TCP, TCP_NODELAY, &value, sizeof (value)))
196 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
198 const char * abs_value = "1";
199 if (0 != setsockopt (h->fd, IPPROTO_TCP, TCP_NODELAY, abs_value, sizeof (abs_value)))
200 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
206 * accept a new connection on a socket
208 * @param desc bound socket
209 * @param address address of the connecting peer, may be NULL
210 * @param address_len length of address
211 * @return client socket
213 struct GNUNET_NETWORK_Handle *
214 GNUNET_NETWORK_socket_accept (const struct GNUNET_NETWORK_Handle *desc,
215 struct sockaddr *address,
216 socklen_t * address_len)
218 struct GNUNET_NETWORK_Handle *ret;
220 ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle));
221 ret->fd = accept (desc->fd, address, address_len);
223 ret->af = address->sa_family;
226 if (ret->fd == INVALID_SOCKET)
229 SetErrnoFromWinsockError (WSAGetLastError ());
235 if (ret->fd >= FD_SETSIZE)
237 GNUNET_break (0 == close (ret->fd));
243 if (GNUNET_SYSERR == socket_set_blocking (ret, GNUNET_NO))
247 /* we might want to treat this one as fatal... */
249 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret));
254 if (GNUNET_OK != socket_set_inheritable (ret))
255 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
256 "socket_set_inheritable");
259 socket_set_nosigpipe (ret);
262 if (ret->af != AF_UNIX)
264 socket_set_nodelay (ret);
270 * Bind to a connected socket
272 * @param address address to be bound
273 * @param address_len length of address
274 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
277 GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc,
278 const struct sockaddr *address,
279 socklen_t address_len)
286 if (desc->af == AF_INET6)
287 if (0 != setsockopt (desc->fd, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof (on)))
288 GNUNET_log_strerror (GNUNET_ERROR_TYPE_DEBUG, "setsockopt");
290 /* is this needed or desired? or done elsewhere? */
291 if (0 != setsockopt (desc->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)))
292 GNUNET_log_strerror (GNUNET_ERROR_TYPE_DEBUG, "setsockopt");
298 if (address->sa_family == AF_UNIX)
300 const struct sockaddr_un *un = (const struct sockaddr_un*) address;
301 (void) unlink (un->sun_path);
305 ret = bind (desc->fd, address, address_len);
307 if (SOCKET_ERROR == ret)
308 SetErrnoFromWinsockError (WSAGetLastError ());
311 return GNUNET_SYSERR;
314 desc->addr = GNUNET_malloc (address_len);
315 memcpy (desc->addr, address, address_len);
316 desc->addrlen = address_len;
326 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
329 GNUNET_NETWORK_socket_close (struct GNUNET_NETWORK_Handle *desc)
334 ret = closesocket (desc->fd);
335 SetErrnoFromWinsockError (WSAGetLastError ());
337 ret = close (desc->fd);
341 if ( (desc->af == AF_UNIX) && (NULL != desc->addr) )
343 const struct sockaddr_un *un = (const struct sockaddr_un*) desc->addr;
344 if (0 != unlink (un->sun_path))
345 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
351 GNUNET_free_non_null (desc->addr);
353 return (ret == 0) ? GNUNET_OK : GNUNET_SYSERR;
358 * Box a native socket (and check that it is a socket).
360 * @param fd socket to box
361 * @return NULL on error (including not supported on target platform)
363 struct GNUNET_NETWORK_Handle *
364 GNUNET_NETWORK_socket_box_native (int fd)
369 struct GNUNET_NETWORK_Handle *ret;
371 if (fcntl (fd, F_GETFD) < 0)
372 return NULL; /* invalid FD */
373 ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle));
384 * @param address peer address
385 * @param address_len length of address
386 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
389 GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Handle *desc,
390 const struct sockaddr *address,
391 socklen_t address_len)
394 ret = connect (desc->fd, address, address_len);
397 if (SOCKET_ERROR == ret)
399 SetErrnoFromWinsockError (WSAGetLastError ());
400 if (errno == EWOULDBLOCK)
404 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
412 * @param level protocol level of the option
413 * @param optname identifier of the option
414 * @param optval options
415 * @param optlen length of optval
416 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
419 GNUNET_NETWORK_socket_getsockopt (const struct GNUNET_NETWORK_Handle *desc,
420 int level, int optname, void *optval,
424 ret = getsockopt (desc->fd, level, optname, optval, optlen);
427 if (ret == 0 && level == SOL_SOCKET && optname == SO_ERROR)
428 *((int *) optval) = GetErrnoFromWinsockError (*((int *) optval));
430 else if (SOCKET_ERROR == ret)
431 SetErrnoFromWinsockError (WSAGetLastError ());
434 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
441 * @param backlog length of the listen queue
442 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
445 GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Handle *desc,
449 ret = listen (desc->fd, backlog);
452 if (SOCKET_ERROR == ret)
453 SetErrnoFromWinsockError (WSAGetLastError ());
456 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
461 * How much data is available to be read on this descriptor?
463 * Returns GNUNET_NO if no data is available, or on error!
467 GNUNET_NETWORK_socket_recvfrom_amount (const struct GNUNET_NETWORK_Handle
472 /* How much is there to be read? */
475 error = ioctl (desc->fd, FIONREAD, &pending);
479 error = ioctlsocket (desc->fd, FIONREAD, &pending);
480 if (error != SOCKET_ERROR)
489 * Read data from a connected socket (always non-blocking).
491 * @param buffer buffer
492 * @param length length of buffer
493 * @param src_addr either the source to recv from, or all zeroes
494 * to be filled in by recvfrom
495 * @param addrlen length of the addr
498 GNUNET_NETWORK_socket_recvfrom (const struct GNUNET_NETWORK_Handle * desc,
499 void *buffer, size_t length,
500 struct sockaddr * src_addr,
508 flags |= MSG_DONTWAIT;
511 ret = recvfrom (desc->fd, buffer, length, flags, src_addr, addrlen);
513 if (SOCKET_ERROR == ret)
514 SetErrnoFromWinsockError (WSAGetLastError ());
521 * Read data from a connected socket (always non-blocking).
523 * @param buffer buffer
524 * @param length length of buffer
527 GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Handle * desc,
528 void *buffer, size_t length)
535 flags |= MSG_DONTWAIT;
537 ret = recv (desc->fd, buffer, length, flags);
539 if (SOCKET_ERROR == ret)
540 SetErrnoFromWinsockError (WSAGetLastError ());
547 * Send data (always non-blocking).
550 * @param buffer data to send
551 * @param length size of the buffer
552 * @return number of bytes sent, GNUNET_SYSERR on error
555 GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Handle * desc,
556 const void *buffer, size_t length)
563 flags |= MSG_DONTWAIT;
567 flags |= MSG_NOSIGNAL;
570 ret = send (desc->fd, buffer, length, flags);
573 if (SOCKET_ERROR == ret)
574 SetErrnoFromWinsockError (WSAGetLastError ());
582 * Send data to a particular destination (always non-blocking).
583 * This function only works for UDP sockets.
586 * @param message data to send
587 * @param length size of the data
588 * @param dest_addr destination address
589 * @param dest_len length of address
590 * @return number of bytes sent, GNUNET_SYSERR on error
593 GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Handle * desc,
594 const void *message, size_t length,
595 const struct sockaddr * dest_addr,
603 flags |= MSG_DONTWAIT;
606 flags |= MSG_NOSIGNAL;
608 ret = sendto (desc->fd, message, length, flags, dest_addr, dest_len);
610 if (SOCKET_ERROR == ret)
611 SetErrnoFromWinsockError (WSAGetLastError ());
620 * @param level protocol level of the option
621 * @param option_name option identifier
622 * @param option_value value to set
623 * @param option_len size of option_value
624 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
627 GNUNET_NETWORK_socket_setsockopt (struct GNUNET_NETWORK_Handle *fd,
628 int level, int option_name,
629 const void *option_value,
630 socklen_t option_len)
634 ret = setsockopt (fd->fd, level, option_name, option_value, option_len);
636 if (SOCKET_ERROR == ret)
637 SetErrnoFromWinsockError (WSAGetLastError ());
639 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
644 * Create a new socket. Configure it for non-blocking IO and
645 * mark it as non-inheritable to child processes (set the
646 * close-on-exec flag).
648 * @param domain domain of the socket
649 * @param type socket type
650 * @param protocol network protocol
651 * @return new socket, NULL on error
653 struct GNUNET_NETWORK_Handle *
654 GNUNET_NETWORK_socket_create (int domain, int type, int protocol)
656 struct GNUNET_NETWORK_Handle *ret;
658 ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle));
660 ret->fd = socket (domain, type, protocol);
661 if (INVALID_SOCKET == ret->fd)
664 SetErrnoFromWinsockError (WSAGetLastError ());
671 if (ret->fd >= FD_SETSIZE)
673 GNUNET_break (0 == close (ret->fd));
680 if (GNUNET_SYSERR == socket_set_blocking (ret, GNUNET_NO))
682 /* we might want to treat this one as fatal... */
684 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret));
689 if (GNUNET_OK != socket_set_inheritable (ret))
690 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
691 "socket_set_inheritable");
694 socket_set_nosigpipe (ret);
696 if ( (type == SOCK_STREAM)
698 && (domain != AF_UNIX)
701 socket_set_nodelay (ret);
707 * Shut down socket operations
709 * @param how type of shutdown
710 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
713 GNUNET_NETWORK_socket_shutdown (struct GNUNET_NETWORK_Handle *desc, int how)
717 ret = shutdown (desc->fd, how);
720 SetErrnoFromWinsockError (WSAGetLastError ());
722 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
727 * Disable the "CORK" feature for communication with the given socket,
728 * forcing the OS to immediately flush the buffer on transmission
729 * instead of potentially buffering multiple messages. Essentially
730 * reduces the OS send buffers to zero.
733 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
736 GNUNET_NETWORK_socket_disable_corking (struct GNUNET_NETWORK_Handle *desc)
741 if (0 != (ret = setsockopt (desc->fd, SOL_SOCKET, SO_SNDBUF, &value, sizeof (value))))
742 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
743 if (0 != (ret = setsockopt (desc->fd, SOL_SOCKET, SO_RCVBUF, &value, sizeof (value))))
744 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
746 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
755 GNUNET_NETWORK_fdset_zero (struct GNUNET_NETWORK_FDSet *fds)
760 GNUNET_CONTAINER_slist_clear (fds->handles);
765 * Add a socket to the FD set
767 * @param desc socket to add
770 GNUNET_NETWORK_fdset_set (struct GNUNET_NETWORK_FDSet *fds,
771 const struct GNUNET_NETWORK_Handle *desc)
773 FD_SET (desc->fd, &fds->sds);
774 if (desc->fd + 1 > fds->nsds)
775 fds->nsds = desc->fd + 1;
780 * Check whether a socket is part of the fd set
783 * @return 0 if the FD is not set
786 GNUNET_NETWORK_fdset_isset (const struct GNUNET_NETWORK_FDSet *fds,
787 const struct GNUNET_NETWORK_Handle *desc)
789 return FD_ISSET (desc->fd, &fds->sds);
794 * Add one fd set to another
795 * @param dst the fd set to add to
796 * @param src the fd set to add from
799 GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst,
800 const struct GNUNET_NETWORK_FDSet *src)
803 for (nfds = src->nsds; nfds > 0; nfds--)
804 if (FD_ISSET (nfds, &src->sds))
807 FD_SET (nfds, &dst->sds);
808 if (nfds + 1 > dst->nsds)
809 dst->nsds = nfds + 1;
812 GNUNET_CONTAINER_slist_append (dst->handles, src->handles);
818 * Copy one fd set to another
820 * @param to destination
824 GNUNET_NETWORK_fdset_copy (struct GNUNET_NETWORK_FDSet *to,
825 const struct GNUNET_NETWORK_FDSet *from)
827 FD_COPY (&from->sds, &to->sds);
828 to->nsds = from->nsds;
831 GNUNET_CONTAINER_slist_clear (to->handles);
832 GNUNET_CONTAINER_slist_append (to->handles, from->handles);
838 * Return file descriptor for this network handle
840 * @param desc wrapper to process
841 * @return POSIX file descriptor
844 GNUNET_NETWORK_get_fd (struct GNUNET_NETWORK_Handle *desc)
851 * Copy a native fd set
853 * @param to destination
854 * @param from native source set
855 * @param nfds the biggest socket number in from + 1
858 GNUNET_NETWORK_fdset_copy_native (struct GNUNET_NETWORK_FDSet *to,
859 const fd_set * from, int nfds)
861 FD_COPY (from, &to->sds);
867 * Set a native fd in a set
869 * @param to destination
870 * @param nfd native FD to set
872 void GNUNET_NETWORK_fdset_set_native (struct GNUNET_NETWORK_FDSet *to,
875 GNUNET_assert(nfd >= 0);
876 FD_SET (nfd, &to->sds);
877 to->nsds = GNUNET_MAX (nfd + 1, to->nsds);
882 * Test native fd in a set
884 * @param to set to test, NULL for empty set
885 * @param nfd native FD to test, or -1 for none
886 * @return GNUNET_YES if FD is set in the set
889 GNUNET_NETWORK_fdset_test_native (const struct GNUNET_NETWORK_FDSet *to,
892 if ( (nfd == -1) || (to == NULL) )
894 return FD_ISSET (nfd, &to->sds) ? GNUNET_YES : GNUNET_NO;
899 * Add a file handle to the fd set
901 * @param h the file handle to add
904 GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds,
905 const struct GNUNET_DISK_FileHandle *h)
908 GNUNET_CONTAINER_slist_add (fds->handles,
909 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
910 h, sizeof (struct GNUNET_DISK_FileHandle));
914 GNUNET_DISK_internal_file_handle_ (h, &fd, sizeof (int));
915 FD_SET (fd, &fds->sds);
916 if (fd + 1 > fds->nsds)
924 * Check if a file handle is part of an fd set
926 * @param h file handle
927 * @return GNUNET_YES if the file handle is part of the set
930 GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds,
931 const struct GNUNET_DISK_FileHandle *h)
935 return GNUNET_CONTAINER_slist_contains (fds->handles, h,
936 sizeof (struct GNUNET_DISK_FileHandle));
938 return FD_ISSET (h->fd, &fds->sds);
944 * Checks if two fd sets overlap
945 * @param fds1 first fd set
946 * @param fds2 second fd set
947 * @return GNUNET_YES if they do overlap, GNUNET_NO otherwise
950 GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1,
951 const struct GNUNET_NETWORK_FDSet *fds2)
957 if (nfds > fds2->nsds)
962 if (FD_ISSET (nfds, &fds1->sds) && FD_ISSET (nfds, &fds2->sds))
966 struct GNUNET_CONTAINER_SList_Iterator *it;
967 struct GNUNET_DISK_FileHandle *h;
971 /*This code is somewhat hacky, we are not supposed to know what's
972 inside of fd_set; also the O(n^2) is really bad... */
974 for (i = 0; i < fds1->sds.fd_count; i++)
976 for (j = 0; j < fds2->sds.fd_count; j++)
978 if (fds1->sds.fd_array[i] == fds2->sds.fd_array[j])
982 it = GNUNET_CONTAINER_slist_begin (fds1->handles);
983 while (GNUNET_CONTAINER_slist_end (it) != GNUNET_YES)
985 h = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (it, NULL);
986 if (GNUNET_CONTAINER_slist_contains
987 (fds2->handles, h, sizeof (struct GNUNET_DISK_FileHandle)))
989 GNUNET_CONTAINER_slist_iter_destroy (it);
992 GNUNET_CONTAINER_slist_next (it);
994 GNUNET_CONTAINER_slist_iter_destroy (it);
1002 * @return a new fd set
1004 struct GNUNET_NETWORK_FDSet *
1005 GNUNET_NETWORK_fdset_create ()
1007 struct GNUNET_NETWORK_FDSet *fds;
1008 fds = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_FDSet));
1010 fds->handles = GNUNET_CONTAINER_slist_create ();
1012 GNUNET_NETWORK_fdset_zero (fds);
1018 * Releases the associated memory of an fd set
1022 GNUNET_NETWORK_fdset_destroy (struct GNUNET_NETWORK_FDSet *fds)
1025 GNUNET_CONTAINER_slist_destroy (fds->handles);
1031 * Check if sockets meet certain conditions
1032 * @param rfds set of sockets to be checked for readability
1033 * @param wfds set of sockets to be checked for writability
1034 * @param efds set of sockets to be checked for exceptions
1035 * @param timeout relative value when to return
1036 * @return number of selected sockets, GNUNET_SYSERR on error
1039 GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
1040 struct GNUNET_NETWORK_FDSet *wfds,
1041 struct GNUNET_NETWORK_FDSet *efds,
1042 const struct GNUNET_TIME_Relative timeout)
1056 handles = GNUNET_CONTAINER_slist_count (rfds->handles);
1061 nfds = GNUNET_MAX (nfds, wfds->nsds);
1063 handles += GNUNET_CONTAINER_slist_count (wfds->handles);
1068 nfds = GNUNET_MAX (nfds, efds->nsds);
1070 handles += GNUNET_CONTAINER_slist_count (efds->handles);
1075 tv.tv_sec = timeout.rel_value / GNUNET_TIME_UNIT_SECONDS.rel_value;
1077 1000 * (timeout.rel_value - (tv.tv_sec * GNUNET_TIME_UNIT_SECONDS.rel_value));
1078 if ((nfds == 0) && (timeout.rel_value == GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
1084 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1086 ("Fatal internal logic error, process hangs in `%s' (abort with CTRL-C)!\n"),
1091 return select (nfds,
1092 (rfds != NULL) ? &rfds->sds : NULL,
1093 (wfds != NULL) ? &wfds->sds : NULL,
1094 (efds != NULL) ? &efds->sds : NULL,
1095 (timeout.rel_value == GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
1100 fd_set sock_read, sock_write, sock_except;
1101 fd_set aread, awrite, aexcept;
1102 struct GNUNET_CONTAINER_SList *handles_read, *handles_write,
1106 struct timeval tvslice;
1110 #define SAFE_FD_ISSET(fd, set) (set != NULL && FD_ISSET(fd, set))
1112 /* calculate how long we need to wait in milliseconds */
1113 if (timeout.rel_value == GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
1114 ms_total = INFINITE;
1117 ms_total = timeout.rel_value / GNUNET_TIME_UNIT_MILLISECONDS.rel_value;
1119 /* select() may be used as a portable way to sleep */
1120 if (!(rfds || wfds || efds))
1127 handles_read = GNUNET_CONTAINER_slist_create ();
1128 handles_write = GNUNET_CONTAINER_slist_create ();
1129 handles_except = GNUNET_CONTAINER_slist_create ();
1132 sock_read = rfds->sds;
1134 FD_ZERO (&sock_read);
1136 sock_write = wfds->sds;
1138 FD_ZERO (&sock_write);
1140 sock_except = efds->sds;
1142 FD_ZERO (&sock_except);
1144 /* multiplex between winsock select() and waiting on the handles */
1148 limit = GetTickCount () + ms_total;
1157 /* overwrite the zero'd sets here; the select call
1158 * will clear those that are not active */
1159 FD_COPY (&sock_read, &aread);
1160 FD_COPY (&sock_write, &awrite);
1161 FD_COPY (&sock_except, &aexcept);
1163 tvslice.tv_usec = 100000;
1165 select (nfds + 1, &aread, &awrite, &aexcept,
1166 &tvslice)) == SOCKET_ERROR)
1169 SetErrnoFromWinsockError (WSAGetLastError ());
1170 if (errno == ENOTSOCK)
1174 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "select");
1177 goto select_loop_end;
1181 /* Poll read pipes */
1185 struct GNUNET_CONTAINER_SList_Iterator *i;
1186 for (i = GNUNET_CONTAINER_slist_begin (rfds->handles);
1187 GNUNET_CONTAINER_slist_end (i) != GNUNET_YES;
1188 GNUNET_CONTAINER_slist_next (i))
1191 struct GNUNET_DISK_FileHandle *fh;
1193 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (i, NULL);
1194 if (fh->type == GNUNET_PIPE)
1196 if (!PeekNamedPipe (fh->h, NULL, 0, NULL, &dwBytes, NULL))
1198 DWORD error_code = GetLastError ();
1201 case ERROR_BROKEN_PIPE:
1202 GNUNET_CONTAINER_slist_add (handles_read,
1203 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1204 fh, sizeof (struct GNUNET_DISK_FileHandle));
1209 SetErrnoFromWinError (error_code);
1212 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
1216 goto select_loop_end;
1222 GNUNET_CONTAINER_slist_add (handles_read,
1223 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1224 fh, sizeof (struct GNUNET_DISK_FileHandle));
1230 /* Should we wait for more bytes to read here (in case of previous EOF)? */
1231 GNUNET_CONTAINER_slist_add (handles_read,
1232 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1233 fh, sizeof (struct GNUNET_DISK_FileHandle));
1236 GNUNET_CONTAINER_slist_iter_destroy (i);
1239 /* Poll for faulty pipes */
1243 struct GNUNET_CONTAINER_SList_Iterator *i;
1244 for (i = GNUNET_CONTAINER_slist_begin (efds->handles);
1245 GNUNET_CONTAINER_slist_end (i) != GNUNET_YES;
1246 GNUNET_CONTAINER_slist_next (i))
1249 struct GNUNET_DISK_FileHandle *fh;
1252 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (i, NULL);
1253 if (fh->type == GNUNET_PIPE)
1255 if (!PeekNamedPipe (fh->h, NULL, 0, NULL, &dwBytes, NULL))
1258 GNUNET_CONTAINER_slist_add (handles_except,
1259 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1260 fh, sizeof (struct GNUNET_DISK_FileHandle));
1265 GNUNET_CONTAINER_slist_iter_destroy (i);
1270 GNUNET_CONTAINER_slist_append (handles_write, wfds->handles);
1271 retcode += GNUNET_CONTAINER_slist_count (wfds->handles);
1274 /* Check for closed sockets */
1275 for (i = 0; i < nfds; i++)
1278 if (SAFE_FD_ISSET (i, &sock_read))
1281 struct sockaddr addr;
1283 if (getpeername (i, &addr, &len) == SOCKET_ERROR)
1289 (i, SOL_SOCKET, SO_ERROR, (char *) &err, &len) == 0
1290 && err == WSAENOTCONN)
1293 if (!SAFE_FD_ISSET (i, &aread))
1304 if (retcode == 0 && nfds == 0)
1305 Sleep (GNUNET_MIN (100, limit - GetTickCount ()));
1307 while (retcode == 0 && (ms_total == INFINITE || GetTickCount () < limit));
1313 GNUNET_NETWORK_fdset_zero (rfds);
1314 GNUNET_NETWORK_fdset_copy_native (rfds, &aread, retcode);
1315 GNUNET_CONTAINER_slist_clear (rfds->handles);
1316 GNUNET_CONTAINER_slist_append (rfds->handles, handles_read);
1320 GNUNET_NETWORK_fdset_zero (wfds);
1321 GNUNET_NETWORK_fdset_copy_native (wfds, &awrite, retcode);
1322 GNUNET_CONTAINER_slist_clear (wfds->handles);
1323 GNUNET_CONTAINER_slist_append (wfds->handles, handles_write);
1327 GNUNET_NETWORK_fdset_zero (efds);
1328 GNUNET_NETWORK_fdset_copy_native (efds, &aexcept, retcode);
1329 GNUNET_CONTAINER_slist_clear (efds->handles);
1330 GNUNET_CONTAINER_slist_append (efds->handles, handles_except);
1334 GNUNET_CONTAINER_slist_destroy (handles_read);
1335 GNUNET_CONTAINER_slist_destroy (handles_write);
1336 GNUNET_CONTAINER_slist_destroy (handles_except);
1343 /* end of network.c */