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);
222 ret->af = address->sa_family;
223 if (ret->fd == INVALID_SOCKET)
226 SetErrnoFromWinsockError (WSAGetLastError ());
232 if (ret->fd >= FD_SETSIZE)
234 GNUNET_break (0 == close (ret->fd));
240 if (GNUNET_SYSERR == socket_set_blocking (ret, GNUNET_NO))
244 /* we might want to treat this one as fatal... */
246 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret));
251 if (GNUNET_OK != socket_set_inheritable (ret))
252 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
253 "socket_set_inheritable");
256 socket_set_nosigpipe (ret);
259 if (address->sa_family != AF_UNIX)
261 socket_set_nodelay (ret);
267 * Bind to a connected socket
269 * @param address address to be bound
270 * @param address_len length of address
271 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
274 GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc,
275 const struct sockaddr *address,
276 socklen_t address_len)
283 if (desc->af == AF_INET6)
284 if (0 != setsockopt (desc->fd, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof (on)))
285 GNUNET_log_strerror (GNUNET_ERROR_TYPE_DEBUG, "setsockopt");
287 /* is this needed or desired? or done elsewhere? */
288 if (0 != setsockopt (desc->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)))
289 GNUNET_log_strerror (GNUNET_ERROR_TYPE_DEBUG, "setsockopt");
295 if (address->sa_family == AF_UNIX)
297 const struct sockaddr_un *un = (const struct sockaddr_un*) address;
298 (void) unlink (un->sun_path);
302 ret = bind (desc->fd, address, address_len);
304 if (SOCKET_ERROR == ret)
305 SetErrnoFromWinsockError (WSAGetLastError ());
308 return GNUNET_SYSERR;
311 desc->addr = GNUNET_malloc (address_len);
312 memcpy (desc->addr, address, address_len);
313 desc->addrlen = address_len;
323 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
326 GNUNET_NETWORK_socket_close (struct GNUNET_NETWORK_Handle *desc)
331 ret = closesocket (desc->fd);
332 SetErrnoFromWinsockError (WSAGetLastError ());
334 ret = close (desc->fd);
338 if ( (desc->af == AF_UNIX) && (NULL != desc->addr) )
340 const struct sockaddr_un *un = (const struct sockaddr_un*) desc->addr;
341 if (0 != unlink (un->sun_path))
342 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
348 GNUNET_free_non_null (desc->addr);
350 return (ret == 0) ? GNUNET_OK : GNUNET_SYSERR;
355 * Box a native socket (and check that it is a socket).
357 * @param fd socket to box
358 * @return NULL on error (including not supported on target platform)
360 struct GNUNET_NETWORK_Handle *
361 GNUNET_NETWORK_socket_box_native (int fd)
366 struct GNUNET_NETWORK_Handle *ret;
368 if (fcntl (fd, F_GETFD) < 0)
369 return NULL; /* invalid FD */
370 ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle));
381 * @param address peer address
382 * @param address_len length of address
383 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
386 GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Handle *desc,
387 const struct sockaddr *address,
388 socklen_t address_len)
391 ret = connect (desc->fd, address, address_len);
394 if (SOCKET_ERROR == ret)
397 SetErrnoFromWinsockError (WSAGetLastError ());
398 if (errno == EWOULDBLOCK)
402 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
410 * @param level protocol level of the option
411 * @param optname identifier of the option
412 * @param optval options
413 * @param optlen length of optval
414 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
417 GNUNET_NETWORK_socket_getsockopt (const struct GNUNET_NETWORK_Handle *desc,
418 int level, int optname, void *optval,
422 ret = getsockopt (desc->fd, level, optname, optval, optlen);
425 if (ret == 0 && level == SOL_SOCKET && optname == SO_ERROR)
426 *((int *) optval) = GetErrnoFromWinsockError (*((int *) optval));
428 else if (SOCKET_ERROR == ret)
429 SetErrnoFromWinsockError (WSAGetLastError ());
432 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
439 * @param backlog length of the listen queue
440 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
443 GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Handle *desc,
447 ret = listen (desc->fd, backlog);
450 if (SOCKET_ERROR == ret)
451 SetErrnoFromWinsockError (WSAGetLastError ());
454 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
459 * How much data is available to be read on this descriptor?
461 * Returns GNUNET_NO if no data is available, or on error!
465 GNUNET_NETWORK_socket_recvfrom_amount (const struct GNUNET_NETWORK_Handle
470 /* How much is there to be read? */
473 error = ioctl (desc->fd, FIONREAD, &pending);
477 error = ioctlsocket (desc->fd, FIONREAD, &pending);
478 if (error != SOCKET_ERROR)
487 * Read data from a connected socket (always non-blocking).
489 * @param buffer buffer
490 * @param length length of buffer
491 * @param src_addr either the source to recv from, or all zeroes
492 * to be filled in by recvfrom
493 * @param addrlen length of the addr
496 GNUNET_NETWORK_socket_recvfrom (const struct GNUNET_NETWORK_Handle * desc,
497 void *buffer, size_t length,
498 struct sockaddr * src_addr,
506 flags |= MSG_DONTWAIT;
509 ret = recvfrom (desc->fd, buffer, length, flags, src_addr, addrlen);
511 if (SOCKET_ERROR == ret)
512 SetErrnoFromWinsockError (WSAGetLastError ());
519 * Read data from a connected socket (always non-blocking).
521 * @param buffer buffer
522 * @param length length of buffer
525 GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Handle * desc,
526 void *buffer, size_t length)
533 flags |= MSG_DONTWAIT;
535 ret = recv (desc->fd, buffer, length, flags);
537 if (SOCKET_ERROR == ret)
538 SetErrnoFromWinsockError (WSAGetLastError ());
545 * Send data (always non-blocking).
548 * @param buffer data to send
549 * @param length size of the buffer
550 * @return number of bytes sent, GNUNET_SYSERR on error
553 GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Handle * desc,
554 const void *buffer, size_t length)
561 flags |= MSG_DONTWAIT;
565 flags |= MSG_NOSIGNAL;
568 ret = send (desc->fd, buffer, length, flags);
571 if (SOCKET_ERROR == ret)
572 SetErrnoFromWinsockError (WSAGetLastError ());
580 * Send data to a particular destination (always non-blocking).
581 * This function only works for UDP sockets.
584 * @param message data to send
585 * @param length size of the data
586 * @param dest_addr destination address
587 * @param dest_len length of address
588 * @return number of bytes sent, GNUNET_SYSERR on error
591 GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Handle * desc,
592 const void *message, size_t length,
593 const struct sockaddr * dest_addr,
601 flags |= MSG_DONTWAIT;
604 flags |= MSG_NOSIGNAL;
606 ret = sendto (desc->fd, message, length, flags, dest_addr, dest_len);
608 if (SOCKET_ERROR == ret)
609 SetErrnoFromWinsockError (WSAGetLastError ());
618 * @param level protocol level of the option
619 * @param option_name option identifier
620 * @param option_value value to set
621 * @param option_len size of option_value
622 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
625 GNUNET_NETWORK_socket_setsockopt (struct GNUNET_NETWORK_Handle *fd,
626 int level, int option_name,
627 const void *option_value,
628 socklen_t option_len)
632 ret = setsockopt (fd->fd, level, option_name, option_value, option_len);
634 if (SOCKET_ERROR == ret)
635 SetErrnoFromWinsockError (WSAGetLastError ());
637 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
642 * Create a new socket. Configure it for non-blocking IO and
643 * mark it as non-inheritable to child processes (set the
644 * close-on-exec flag).
646 * @param domain domain of the socket
647 * @param type socket type
648 * @param protocol network protocol
649 * @return new socket, NULL on error
651 struct GNUNET_NETWORK_Handle *
652 GNUNET_NETWORK_socket_create (int domain, int type, int protocol)
654 struct GNUNET_NETWORK_Handle *ret;
656 ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle));
658 ret->fd = socket (domain, type, protocol);
659 if (INVALID_SOCKET == ret->fd)
662 SetErrnoFromWinsockError (WSAGetLastError ());
669 if (ret->fd >= FD_SETSIZE)
671 GNUNET_break (0 == close (ret->fd));
678 if (GNUNET_SYSERR == socket_set_blocking (ret, GNUNET_NO))
680 /* we might want to treat this one as fatal... */
682 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret));
687 if (GNUNET_OK != socket_set_inheritable (ret))
688 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
689 "socket_set_inheritable");
692 socket_set_nosigpipe (ret);
694 if ( (type == SOCK_STREAM)
696 && (domain != AF_UNIX)
699 socket_set_nodelay (ret);
705 * Shut down socket operations
707 * @param how type of shutdown
708 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
711 GNUNET_NETWORK_socket_shutdown (struct GNUNET_NETWORK_Handle *desc, int how)
715 ret = shutdown (desc->fd, how);
718 SetErrnoFromWinsockError (WSAGetLastError ());
720 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
725 * Disable the "CORK" feature for communication with the given socket,
726 * forcing the OS to immediately flush the buffer on transmission
727 * instead of potentially buffering multiple messages. Essentially
728 * reduces the OS send buffers to zero.
731 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
734 GNUNET_NETWORK_socket_disable_corking (struct GNUNET_NETWORK_Handle *desc)
739 if (0 != (ret = setsockopt (desc->fd, SOL_SOCKET, SO_SNDBUF, &value, sizeof (value))))
740 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
741 if (0 != (ret = setsockopt (desc->fd, SOL_SOCKET, SO_RCVBUF, &value, sizeof (value))))
742 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
744 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
753 GNUNET_NETWORK_fdset_zero (struct GNUNET_NETWORK_FDSet *fds)
758 GNUNET_CONTAINER_slist_clear (fds->handles);
763 * Add a socket to the FD set
765 * @param desc socket to add
768 GNUNET_NETWORK_fdset_set (struct GNUNET_NETWORK_FDSet *fds,
769 const struct GNUNET_NETWORK_Handle *desc)
771 FD_SET (desc->fd, &fds->sds);
772 if (desc->fd + 1 > fds->nsds)
773 fds->nsds = desc->fd + 1;
778 * Check whether a socket is part of the fd set
781 * @return 0 if the FD is not set
784 GNUNET_NETWORK_fdset_isset (const struct GNUNET_NETWORK_FDSet *fds,
785 const struct GNUNET_NETWORK_Handle *desc)
787 return FD_ISSET (desc->fd, &fds->sds);
792 * Add one fd set to another
793 * @param dst the fd set to add to
794 * @param src the fd set to add from
797 GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst,
798 const struct GNUNET_NETWORK_FDSet *src)
801 for (nfds = src->nsds; nfds > 0; nfds--)
802 if (FD_ISSET (nfds, &src->sds))
805 FD_SET (nfds, &dst->sds);
806 if (nfds + 1 > dst->nsds)
807 dst->nsds = nfds + 1;
810 GNUNET_CONTAINER_slist_append (dst->handles, src->handles);
816 * Copy one fd set to another
818 * @param to destination
822 GNUNET_NETWORK_fdset_copy (struct GNUNET_NETWORK_FDSet *to,
823 const struct GNUNET_NETWORK_FDSet *from)
825 FD_COPY (&from->sds, &to->sds);
826 to->nsds = from->nsds;
829 GNUNET_CONTAINER_slist_clear (to->handles);
830 GNUNET_CONTAINER_slist_append (to->handles, from->handles);
836 * Return file descriptor for this network handle
838 * @param desc wrapper to process
839 * @return POSIX file descriptor
842 GNUNET_NETWORK_get_fd (struct GNUNET_NETWORK_Handle *desc)
849 * Copy a native fd set
851 * @param to destination
852 * @param from native source set
853 * @param nfds the biggest socket number in from + 1
856 GNUNET_NETWORK_fdset_copy_native (struct GNUNET_NETWORK_FDSet *to,
857 const fd_set * from, int nfds)
859 FD_COPY (from, &to->sds);
865 * Set a native fd in a set
867 * @param to destination
868 * @param nfd native FD to set
870 void GNUNET_NETWORK_fdset_set_native (struct GNUNET_NETWORK_FDSet *to,
873 GNUNET_assert(nfd >= 0);
874 FD_SET (nfd, &to->sds);
875 to->nsds = GNUNET_MAX (nfd + 1, to->nsds);
880 * Test native fd in a set
882 * @param to set to test, NULL for empty set
883 * @param nfd native FD to test, or -1 for none
884 * @return GNUNET_YES if FD is set in the set
887 GNUNET_NETWORK_fdset_test_native (const struct GNUNET_NETWORK_FDSet *to,
890 if ( (nfd == -1) || (to == NULL) )
892 return FD_ISSET (nfd, &to->sds) ? GNUNET_YES : GNUNET_NO;
897 * Add a file handle to the fd set
899 * @param h the file handle to add
902 GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds,
903 const struct GNUNET_DISK_FileHandle *h)
906 GNUNET_CONTAINER_slist_add (fds->handles,
907 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
908 h, sizeof (struct GNUNET_DISK_FileHandle));
912 GNUNET_DISK_internal_file_handle_ (h, &fd, sizeof (int));
913 FD_SET (fd, &fds->sds);
914 if (fd + 1 > fds->nsds)
922 * Check if a file handle is part of an fd set
924 * @param h file handle
925 * @return GNUNET_YES if the file handle is part of the set
928 GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds,
929 const struct GNUNET_DISK_FileHandle *h)
933 return GNUNET_CONTAINER_slist_contains (fds->handles, h,
934 sizeof (struct GNUNET_DISK_FileHandle));
936 return FD_ISSET (h->fd, &fds->sds);
942 * Checks if two fd sets overlap
943 * @param fds1 first fd set
944 * @param fds2 second fd set
945 * @return GNUNET_YES if they do overlap, GNUNET_NO otherwise
948 GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1,
949 const struct GNUNET_NETWORK_FDSet *fds2)
955 if (nfds > fds2->nsds)
960 if (FD_ISSET (nfds, &fds1->sds) && FD_ISSET (nfds, &fds2->sds))
964 struct GNUNET_CONTAINER_SList_Iterator *it;
965 struct GNUNET_DISK_FileHandle *h;
969 /*This code is somewhat hacky, we are not supposed to know what's
970 inside of fd_set; also the O(n^2) is really bad... */
972 for (i = 0; i < fds1->sds.fd_count; i++)
974 for (j = 0; j < fds2->sds.fd_count; j++)
976 if (fds1->sds.fd_array[i] == fds2->sds.fd_array[j])
980 it = GNUNET_CONTAINER_slist_begin (fds1->handles);
981 while (GNUNET_CONTAINER_slist_end (it) != GNUNET_YES)
983 h = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (it, NULL);
984 if (GNUNET_CONTAINER_slist_contains
985 (fds2->handles, h, sizeof (struct GNUNET_DISK_FileHandle)))
987 GNUNET_CONTAINER_slist_iter_destroy (it);
990 GNUNET_CONTAINER_slist_next (it);
992 GNUNET_CONTAINER_slist_iter_destroy (it);
1000 * @return a new fd set
1002 struct GNUNET_NETWORK_FDSet *
1003 GNUNET_NETWORK_fdset_create ()
1005 struct GNUNET_NETWORK_FDSet *fds;
1006 fds = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_FDSet));
1008 fds->handles = GNUNET_CONTAINER_slist_create ();
1010 GNUNET_NETWORK_fdset_zero (fds);
1016 * Releases the associated memory of an fd set
1020 GNUNET_NETWORK_fdset_destroy (struct GNUNET_NETWORK_FDSet *fds)
1023 GNUNET_CONTAINER_slist_destroy (fds->handles);
1029 * Check if sockets meet certain conditions
1030 * @param rfds set of sockets to be checked for readability
1031 * @param wfds set of sockets to be checked for writability
1032 * @param efds set of sockets to be checked for exceptions
1033 * @param timeout relative value when to return
1034 * @return number of selected sockets, GNUNET_SYSERR on error
1037 GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
1038 struct GNUNET_NETWORK_FDSet *wfds,
1039 struct GNUNET_NETWORK_FDSet *efds,
1040 const struct GNUNET_TIME_Relative timeout)
1054 handles = GNUNET_CONTAINER_slist_count (rfds->handles);
1059 nfds = GNUNET_MAX (nfds, wfds->nsds);
1061 handles += GNUNET_CONTAINER_slist_count (wfds->handles);
1066 nfds = GNUNET_MAX (nfds, efds->nsds);
1068 handles += GNUNET_CONTAINER_slist_count (efds->handles);
1073 tv.tv_sec = timeout.rel_value / GNUNET_TIME_UNIT_SECONDS.rel_value;
1075 1000 * (timeout.rel_value - (tv.tv_sec * GNUNET_TIME_UNIT_SECONDS.rel_value));
1076 if ((nfds == 0) && (timeout.rel_value == GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
1082 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1084 ("Fatal internal logic error, process hangs in `%s' (abort with CTRL-C)!\n"),
1089 return select (nfds,
1090 (rfds != NULL) ? &rfds->sds : NULL,
1091 (wfds != NULL) ? &wfds->sds : NULL,
1092 (efds != NULL) ? &efds->sds : NULL,
1093 (timeout.rel_value == GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
1098 fd_set sock_read, sock_write, sock_except;
1099 fd_set aread, awrite, aexcept;
1100 struct GNUNET_CONTAINER_SList *handles_read, *handles_write,
1104 struct timeval tvslice;
1108 #define SAFE_FD_ISSET(fd, set) (set != NULL && FD_ISSET(fd, set))
1110 /* calculate how long we need to wait in milliseconds */
1111 if (timeout.rel_value == GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
1112 ms_total = INFINITE;
1115 ms_total = timeout.rel_value / GNUNET_TIME_UNIT_MILLISECONDS.rel_value;
1117 /* select() may be used as a portable way to sleep */
1118 if (!(rfds || wfds || efds))
1125 handles_read = GNUNET_CONTAINER_slist_create ();
1126 handles_write = GNUNET_CONTAINER_slist_create ();
1127 handles_except = GNUNET_CONTAINER_slist_create ();
1130 sock_read = rfds->sds;
1132 FD_ZERO (&sock_read);
1134 sock_write = wfds->sds;
1136 FD_ZERO (&sock_write);
1138 sock_except = efds->sds;
1140 FD_ZERO (&sock_except);
1142 /* multiplex between winsock select() and waiting on the handles */
1146 limit = GetTickCount () + ms_total;
1155 /* overwrite the zero'd sets here; the select call
1156 * will clear those that are not active */
1157 FD_COPY (&sock_read, &aread);
1158 FD_COPY (&sock_write, &awrite);
1159 FD_COPY (&sock_except, &aexcept);
1161 tvslice.tv_usec = 100000;
1163 select (nfds + 1, &aread, &awrite, &aexcept,
1164 &tvslice)) == SOCKET_ERROR)
1167 SetErrnoFromWinsockError (WSAGetLastError ());
1168 if (errno == ENOTSOCK)
1172 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "select");
1175 goto select_loop_end;
1179 /* Poll read pipes */
1183 struct GNUNET_CONTAINER_SList_Iterator *i;
1184 for (i = GNUNET_CONTAINER_slist_begin (rfds->handles);
1185 GNUNET_CONTAINER_slist_end (i) != GNUNET_YES;
1186 GNUNET_CONTAINER_slist_next (i))
1189 struct GNUNET_DISK_FileHandle *fh;
1191 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (i, NULL);
1192 if (fh->type == GNUNET_PIPE)
1194 if (!PeekNamedPipe (fh->h, NULL, 0, NULL, &dwBytes, NULL))
1196 DWORD error_code = GetLastError ();
1199 case ERROR_BROKEN_PIPE:
1200 GNUNET_CONTAINER_slist_add (handles_read,
1201 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1202 fh, sizeof (struct GNUNET_DISK_FileHandle));
1207 SetErrnoFromWinError (error_code);
1210 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
1214 goto select_loop_end;
1220 GNUNET_CONTAINER_slist_add (handles_read,
1221 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1222 fh, sizeof (struct GNUNET_DISK_FileHandle));
1228 /* Should we wait for more bytes to read here (in case of previous EOF)? */
1229 GNUNET_CONTAINER_slist_add (handles_read,
1230 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1231 fh, sizeof (struct GNUNET_DISK_FileHandle));
1234 GNUNET_CONTAINER_slist_iter_destroy (i);
1237 /* Poll for faulty pipes */
1241 struct GNUNET_CONTAINER_SList_Iterator *i;
1242 for (i = GNUNET_CONTAINER_slist_begin (efds->handles);
1243 GNUNET_CONTAINER_slist_end (i) != GNUNET_YES;
1244 GNUNET_CONTAINER_slist_next (i))
1247 struct GNUNET_DISK_FileHandle *fh;
1250 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (i, NULL);
1251 if (fh->type == GNUNET_PIPE)
1253 if (!PeekNamedPipe (fh->h, NULL, 0, NULL, &dwBytes, NULL))
1256 GNUNET_CONTAINER_slist_add (handles_except,
1257 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1258 fh, sizeof (struct GNUNET_DISK_FileHandle));
1263 GNUNET_CONTAINER_slist_iter_destroy (i);
1268 GNUNET_CONTAINER_slist_append (handles_write, wfds->handles);
1269 retcode += GNUNET_CONTAINER_slist_count (wfds->handles);
1272 /* Check for closed sockets */
1273 for (i = 0; i < nfds; i++)
1276 if (SAFE_FD_ISSET (i, &sock_read))
1279 struct sockaddr addr;
1281 if (getpeername (i, &addr, &len) == SOCKET_ERROR)
1287 (i, SOL_SOCKET, SO_ERROR, (char *) &err, &len) == 0
1288 && err == WSAENOTCONN)
1291 if (!SAFE_FD_ISSET (i, &aread))
1302 if (retcode == 0 && nfds == 0)
1303 Sleep (GNUNET_MIN (100, limit - GetTickCount ()));
1305 while (retcode == 0 && (ms_total == INFINITE || GetTickCount () < limit));
1311 GNUNET_NETWORK_fdset_zero (rfds);
1312 GNUNET_NETWORK_fdset_copy_native (rfds, &aread, retcode);
1313 GNUNET_CONTAINER_slist_clear (rfds->handles);
1314 GNUNET_CONTAINER_slist_append (rfds->handles, handles_read);
1318 GNUNET_NETWORK_fdset_zero (wfds);
1319 GNUNET_NETWORK_fdset_copy_native (wfds, &awrite, retcode);
1320 GNUNET_CONTAINER_slist_clear (wfds->handles);
1321 GNUNET_CONTAINER_slist_append (wfds->handles, handles_write);
1325 GNUNET_NETWORK_fdset_zero (efds);
1326 GNUNET_NETWORK_fdset_copy_native (efds, &aexcept, retcode);
1327 GNUNET_CONTAINER_slist_clear (efds->handles);
1328 GNUNET_CONTAINER_slist_append (efds->handles, handles_except);
1332 GNUNET_CONTAINER_slist_destroy (handles_read);
1333 GNUNET_CONTAINER_slist_destroy (handles_write);
1334 GNUNET_CONTAINER_slist_destroy (handles_except);
1341 /* end of network.c */