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_NO
34 #define DEBUG_W32_CYCLES GNUNET_NO
36 #ifndef INVALID_SOCKET
37 #define INVALID_SOCKET -1
41 struct GNUNET_NETWORK_Handle
51 * Address family / domain.
56 * Number of bytes in addr.
61 * Address we were bound to, or NULL.
63 struct sockaddr *addr;
68 struct GNUNET_NETWORK_FDSet
72 * Maximum number of any socket socket descriptor in the set (plus one)
77 * Bitset with the descriptors.
83 * Linked list of handles
85 struct GNUNET_CONTAINER_SList *handles;
91 #define FD_COPY(s, d) (memcpy ((d), (s), sizeof (fd_set)))
96 * Set if a socket should use blocking or non-blocking IO.
98 * @param doBlock blocking mode
99 * @return GNUNET_OK on success, GNUNET_SYSERR on error
102 socket_set_blocking (struct GNUNET_NETWORK_Handle *fd, int doBlock)
108 if (ioctlsocket (fd->fd, FIONBIO, &mode) == SOCKET_ERROR)
111 SetErrnoFromWinsockError (WSAGetLastError ());
112 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "ioctlsocket");
113 return GNUNET_SYSERR;
119 int flags = fcntl (fd->fd, F_GETFL);
123 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "fcntl");
124 return GNUNET_SYSERR;
127 flags &= ~O_NONBLOCK;
131 if (0 != fcntl (fd->fd, F_SETFL, flags))
134 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "fcntl");
135 return GNUNET_SYSERR;
144 * Make a socket non-inheritable to child processes
146 * @param h the socket to make non-inheritable
147 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
148 * @warning Not implemented on Windows
151 socket_set_inheritable (const struct GNUNET_NETWORK_Handle *h)
155 i = fcntl (h->fd, F_GETFD);
157 return GNUNET_SYSERR;
158 if (i == (i | FD_CLOEXEC))
161 if (fcntl (h->fd, F_SETFD, i) < 0)
162 return GNUNET_SYSERR;
170 * The MSG_NOSIGNAL equivalent on Mac OS X
172 * @param h the socket to make non-delaying
175 socket_set_nosigpipe (const struct GNUNET_NETWORK_Handle *h)
179 setsockopt (h->fd, SOL_SOCKET, SO_NOSIGPIPE, &abs_value, sizeof (abs_value)))
180 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
186 * Disable delays when sending data via the socket.
187 * (GNUnet makes sure that messages are as big as
190 * @param h the socket to make non-delaying
193 socket_set_nodelay (const struct GNUNET_NETWORK_Handle *h)
197 if (0 != setsockopt (h->fd, IPPROTO_TCP, TCP_NODELAY, &value, sizeof (value)))
198 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
200 const char * abs_value = "1";
201 if (0 != setsockopt (h->fd, IPPROTO_TCP, TCP_NODELAY, abs_value, sizeof (abs_value)))
202 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
208 * accept a new connection on a socket
210 * @param desc bound socket
211 * @param address address of the connecting peer, may be NULL
212 * @param address_len length of address
213 * @return client socket
215 struct GNUNET_NETWORK_Handle *
216 GNUNET_NETWORK_socket_accept (const struct GNUNET_NETWORK_Handle *desc,
217 struct sockaddr *address,
218 socklen_t * address_len)
220 struct GNUNET_NETWORK_Handle *ret;
222 ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle));
225 struct sockaddr name;
226 int namelen = sizeof (name);
227 int gsn = getsockname (desc->fd, &name, &namelen);
229 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
230 "Accepting connection on `%s'\n",
231 GNUNET_a2s (&name, namelen));
234 ret->fd = accept (desc->fd, address, address_len);
236 ret->af = address->sa_family;
239 if (ret->fd == INVALID_SOCKET)
242 SetErrnoFromWinsockError (WSAGetLastError ());
248 if (ret->fd >= FD_SETSIZE)
250 GNUNET_break (0 == close (ret->fd));
256 if (GNUNET_SYSERR == socket_set_blocking (ret, GNUNET_NO))
260 /* we might want to treat this one as fatal... */
262 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret));
267 if (GNUNET_OK != socket_set_inheritable (ret))
268 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
269 "socket_set_inheritable");
272 socket_set_nosigpipe (ret);
275 if (ret->af != AF_UNIX)
277 socket_set_nodelay (ret);
283 * Bind to a connected socket
285 * @param address address to be bound
286 * @param address_len length of address
287 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
290 GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc,
291 const struct sockaddr *address,
292 socklen_t address_len)
299 if (desc->af == AF_INET6)
300 if (0 != setsockopt (desc->fd, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof (on)))
301 GNUNET_log_strerror (GNUNET_ERROR_TYPE_DEBUG, "setsockopt");
303 /* is this needed or desired? or done elsewhere? */
304 if (0 != setsockopt (desc->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)))
305 GNUNET_log_strerror (GNUNET_ERROR_TYPE_DEBUG, "setsockopt");
311 if (address->sa_family == AF_UNIX)
313 const struct sockaddr_un *un = (const struct sockaddr_un*) address;
314 (void) unlink (un->sun_path);
318 ret = bind (desc->fd, address, address_len);
320 if (SOCKET_ERROR == ret)
321 SetErrnoFromWinsockError (WSAGetLastError ());
324 return GNUNET_SYSERR;
327 desc->addr = GNUNET_malloc (address_len);
328 memcpy (desc->addr, address, address_len);
329 desc->addrlen = address_len;
339 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
342 GNUNET_NETWORK_socket_close (struct GNUNET_NETWORK_Handle *desc)
349 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_NETWORK_socket_close", "Closing 0x%x\n", desc->fd);
352 ret = closesocket (desc->fd);
353 error = WSAGetLastError ();
354 SetErrnoFromWinsockError (error);
356 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_NETWORK_socket_close", "Closed 0x%x, closesocket() returned %d, GLE is %u\n", desc->fd, ret, error);
359 ret = close (desc->fd);
363 if ( (desc->af == AF_UNIX) && (NULL != desc->addr) )
365 const struct sockaddr_un *un = (const struct sockaddr_un*) desc->addr;
366 if (0 != unlink (un->sun_path))
367 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
373 GNUNET_free_non_null (desc->addr);
375 return (ret == 0) ? GNUNET_OK : GNUNET_SYSERR;
380 * Box a native socket (and check that it is a socket).
382 * @param fd socket to box
383 * @return NULL on error (including not supported on target platform)
385 struct GNUNET_NETWORK_Handle *
386 GNUNET_NETWORK_socket_box_native (int fd)
391 struct GNUNET_NETWORK_Handle *ret;
393 if (fcntl (fd, F_GETFD) < 0)
394 return NULL; /* invalid FD */
395 ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle));
406 * @param address peer address
407 * @param address_len length of address
408 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
411 GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Handle *desc,
412 const struct sockaddr *address,
413 socklen_t address_len)
416 ret = connect (desc->fd, address, address_len);
419 if (SOCKET_ERROR == ret)
421 SetErrnoFromWinsockError (WSAGetLastError ());
422 if (errno == EWOULDBLOCK)
426 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
434 * @param level protocol level of the option
435 * @param optname identifier of the option
436 * @param optval options
437 * @param optlen length of optval
438 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
441 GNUNET_NETWORK_socket_getsockopt (const struct GNUNET_NETWORK_Handle *desc,
442 int level, int optname, void *optval,
446 ret = getsockopt (desc->fd, level, optname, optval, optlen);
449 if (ret == 0 && level == SOL_SOCKET && optname == SO_ERROR)
450 *((int *) optval) = GetErrnoFromWinsockError (*((int *) optval));
452 else if (SOCKET_ERROR == ret)
453 SetErrnoFromWinsockError (WSAGetLastError ());
455 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
462 * @param backlog length of the listen queue
463 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
466 GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Handle *desc,
470 ret = listen (desc->fd, backlog);
473 if (SOCKET_ERROR == ret)
474 SetErrnoFromWinsockError (WSAGetLastError ());
477 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
482 * How much data is available to be read on this descriptor?
484 * Returns GNUNET_NO if no data is available, or on error!
488 GNUNET_NETWORK_socket_recvfrom_amount (const struct GNUNET_NETWORK_Handle
493 /* How much is there to be read? */
496 error = ioctl (desc->fd, FIONREAD, &pending);
500 error = ioctlsocket (desc->fd, FIONREAD, &pending);
501 if (error != SOCKET_ERROR)
510 * Read data from a connected socket (always non-blocking).
512 * @param buffer buffer
513 * @param length length of buffer
514 * @param src_addr either the source to recv from, or all zeroes
515 * to be filled in by recvfrom
516 * @param addrlen length of the addr
519 GNUNET_NETWORK_socket_recvfrom (const struct GNUNET_NETWORK_Handle * desc,
520 void *buffer, size_t length,
521 struct sockaddr * src_addr,
529 flags |= MSG_DONTWAIT;
532 ret = recvfrom (desc->fd, buffer, length, flags, src_addr, addrlen);
534 if (SOCKET_ERROR == ret)
535 SetErrnoFromWinsockError (WSAGetLastError ());
542 * Read data from a connected socket (always non-blocking).
544 * @param buffer buffer
545 * @param length length of buffer
548 GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Handle * desc,
549 void *buffer, size_t length)
556 flags |= MSG_DONTWAIT;
558 ret = recv (desc->fd, buffer, length, flags);
560 if (SOCKET_ERROR == ret)
561 SetErrnoFromWinsockError (WSAGetLastError ());
568 * Send data (always non-blocking).
571 * @param buffer data to send
572 * @param length size of the buffer
573 * @return number of bytes sent, GNUNET_SYSERR on error
576 GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Handle * desc,
577 const void *buffer, size_t length)
584 flags |= MSG_DONTWAIT;
588 flags |= MSG_NOSIGNAL;
591 ret = send (desc->fd, buffer, length, flags);
594 if (SOCKET_ERROR == ret)
595 SetErrnoFromWinsockError (WSAGetLastError ());
603 * Send data to a particular destination (always non-blocking).
604 * This function only works for UDP sockets.
607 * @param message data to send
608 * @param length size of the data
609 * @param dest_addr destination address
610 * @param dest_len length of address
611 * @return number of bytes sent, GNUNET_SYSERR on error
614 GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Handle * desc,
615 const void *message, size_t length,
616 const struct sockaddr * dest_addr,
624 flags |= MSG_DONTWAIT;
627 flags |= MSG_NOSIGNAL;
629 ret = sendto (desc->fd, message, length, flags, dest_addr, dest_len);
631 if (SOCKET_ERROR == ret)
632 SetErrnoFromWinsockError (WSAGetLastError ());
641 * @param level protocol level of the option
642 * @param option_name option identifier
643 * @param option_value value to set
644 * @param option_len size of option_value
645 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
648 GNUNET_NETWORK_socket_setsockopt (struct GNUNET_NETWORK_Handle *fd,
649 int level, int option_name,
650 const void *option_value,
651 socklen_t option_len)
655 ret = setsockopt (fd->fd, level, option_name, option_value, option_len);
657 if (SOCKET_ERROR == ret)
658 SetErrnoFromWinsockError (WSAGetLastError ());
660 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
665 * Create a new socket. Configure it for non-blocking IO and
666 * mark it as non-inheritable to child processes (set the
667 * close-on-exec flag).
669 * @param domain domain of the socket
670 * @param type socket type
671 * @param protocol network protocol
672 * @return new socket, NULL on error
674 struct GNUNET_NETWORK_Handle *
675 GNUNET_NETWORK_socket_create (int domain, int type, int protocol)
677 struct GNUNET_NETWORK_Handle *ret;
679 ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle));
681 ret->fd = socket (domain, type, protocol);
682 if (INVALID_SOCKET == ret->fd)
685 SetErrnoFromWinsockError (WSAGetLastError ());
692 if (ret->fd >= FD_SETSIZE)
694 GNUNET_break (0 == close (ret->fd));
701 if (GNUNET_SYSERR == socket_set_blocking (ret, GNUNET_NO))
703 /* we might want to treat this one as fatal... */
705 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret));
710 if (GNUNET_OK != socket_set_inheritable (ret))
711 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
712 "socket_set_inheritable");
715 socket_set_nosigpipe (ret);
717 if ( (type == SOCK_STREAM)
719 && (domain != AF_UNIX)
722 socket_set_nodelay (ret);
728 * Shut down socket operations
730 * @param how type of shutdown
731 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
734 GNUNET_NETWORK_socket_shutdown (struct GNUNET_NETWORK_Handle *desc, int how)
738 ret = shutdown (desc->fd, how);
741 SetErrnoFromWinsockError (WSAGetLastError ());
743 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
748 * Disable the "CORK" feature for communication with the given socket,
749 * forcing the OS to immediately flush the buffer on transmission
750 * instead of potentially buffering multiple messages. Essentially
751 * reduces the OS send buffers to zero.
754 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
757 GNUNET_NETWORK_socket_disable_corking (struct GNUNET_NETWORK_Handle *desc)
762 if (0 != (ret = setsockopt (desc->fd, SOL_SOCKET, SO_SNDBUF, (char *) &value, sizeof (value))))
763 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
764 if (0 != (ret = setsockopt (desc->fd, SOL_SOCKET, SO_RCVBUF, (char *) &value, sizeof (value))))
765 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
767 if (0 != (ret = setsockopt (desc->fd, SOL_SOCKET, SO_SNDBUF, &value, sizeof (value))))
768 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
769 if (0 != (ret = setsockopt (desc->fd, SOL_SOCKET, SO_RCVBUF, &value, sizeof (value))))
770 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
773 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
782 GNUNET_NETWORK_fdset_zero (struct GNUNET_NETWORK_FDSet *fds)
787 GNUNET_CONTAINER_slist_clear (fds->handles);
792 * Add a socket to the FD set
794 * @param desc socket to add
797 GNUNET_NETWORK_fdset_set (struct GNUNET_NETWORK_FDSet *fds,
798 const struct GNUNET_NETWORK_Handle *desc)
800 FD_SET (desc->fd, &fds->sds);
801 if (desc->fd + 1 > fds->nsds)
802 fds->nsds = desc->fd + 1;
807 * Check whether a socket is part of the fd set
810 * @return 0 if the FD is not set
813 GNUNET_NETWORK_fdset_isset (const struct GNUNET_NETWORK_FDSet *fds,
814 const struct GNUNET_NETWORK_Handle *desc)
816 return FD_ISSET (desc->fd, &fds->sds);
821 * Add one fd set to another
822 * @param dst the fd set to add to
823 * @param src the fd set to add from
826 GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst,
827 const struct GNUNET_NETWORK_FDSet *src)
830 for (nfds = src->nsds; nfds > 0; nfds--)
831 if (FD_ISSET (nfds, &src->sds))
834 FD_SET (nfds, &dst->sds);
835 if (nfds + 1 > dst->nsds)
836 dst->nsds = nfds + 1;
839 GNUNET_CONTAINER_slist_append (dst->handles, src->handles);
845 * Copy one fd set to another
847 * @param to destination
851 GNUNET_NETWORK_fdset_copy (struct GNUNET_NETWORK_FDSet *to,
852 const struct GNUNET_NETWORK_FDSet *from)
854 FD_COPY (&from->sds, &to->sds);
855 to->nsds = from->nsds;
858 GNUNET_CONTAINER_slist_clear (to->handles);
859 GNUNET_CONTAINER_slist_append (to->handles, from->handles);
865 * Return file descriptor for this network handle
867 * @param desc wrapper to process
868 * @return POSIX file descriptor
871 GNUNET_NETWORK_get_fd (struct GNUNET_NETWORK_Handle *desc)
878 * Copy a native fd set
880 * @param to destination
881 * @param from native source set
882 * @param nfds the biggest socket number in from + 1
885 GNUNET_NETWORK_fdset_copy_native (struct GNUNET_NETWORK_FDSet *to,
886 const fd_set * from, int nfds)
888 FD_COPY (from, &to->sds);
894 * Set a native fd in a set
896 * @param to destination
897 * @param nfd native FD to set
899 void GNUNET_NETWORK_fdset_set_native (struct GNUNET_NETWORK_FDSet *to,
902 GNUNET_assert((nfd >= 0) && (nfd < FD_SETSIZE));
903 FD_SET (nfd, &to->sds);
904 to->nsds = GNUNET_MAX (nfd + 1, to->nsds);
909 * Test native fd in a set
911 * @param to set to test, NULL for empty set
912 * @param nfd native FD to test, or -1 for none
913 * @return GNUNET_YES if FD is set in the set
916 GNUNET_NETWORK_fdset_test_native (const struct GNUNET_NETWORK_FDSet *to,
919 if ( (nfd == -1) || (to == NULL) )
921 return FD_ISSET (nfd, &to->sds) ? GNUNET_YES : GNUNET_NO;
926 * Add a file handle to the fd set
928 * @param h the file handle to add
931 GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds,
932 const struct GNUNET_DISK_FileHandle *h)
935 GNUNET_CONTAINER_slist_add (fds->handles,
936 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
937 h, sizeof (struct GNUNET_DISK_FileHandle));
941 GNUNET_DISK_internal_file_handle_ (h, &fd, sizeof (int));
942 FD_SET (fd, &fds->sds);
943 if (fd + 1 > fds->nsds)
951 * Check if a file handle is part of an fd set
953 * @param h file handle
954 * @return GNUNET_YES if the file handle is part of the set
957 GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds,
958 const struct GNUNET_DISK_FileHandle *h)
962 return GNUNET_CONTAINER_slist_contains (fds->handles, h,
963 sizeof (struct GNUNET_DISK_FileHandle));
965 return FD_ISSET (h->fd, &fds->sds);
971 * Checks if two fd sets overlap
972 * @param fds1 first fd set
973 * @param fds2 second fd set
974 * @return GNUNET_YES if they do overlap, GNUNET_NO otherwise
977 GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1,
978 const struct GNUNET_NETWORK_FDSet *fds2)
984 if (nfds > fds2->nsds)
989 if (FD_ISSET (nfds, &fds1->sds) && FD_ISSET (nfds, &fds2->sds))
993 struct GNUNET_CONTAINER_SList_Iterator *it;
994 struct GNUNET_DISK_FileHandle *h;
998 /*This code is somewhat hacky, we are not supposed to know what's
999 inside of fd_set; also the O(n^2) is really bad... */
1001 for (i = 0; i < fds1->sds.fd_count; i++)
1003 for (j = 0; j < fds2->sds.fd_count; j++)
1005 if (fds1->sds.fd_array[i] == fds2->sds.fd_array[j])
1009 it = GNUNET_CONTAINER_slist_begin (fds1->handles);
1010 while (GNUNET_CONTAINER_slist_end (it) != GNUNET_YES)
1013 struct GNUNET_CONTAINER_SList_Iterator *t;
1015 h = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (it, NULL);
1017 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Checking that FD 0x%x is in another set:\n", h->h);
1018 for (t = GNUNET_CONTAINER_slist_begin (fds2->handles);
1019 GNUNET_CONTAINER_slist_end (t) != GNUNET_YES;
1020 GNUNET_CONTAINER_slist_next (t))
1022 struct GNUNET_DISK_FileHandle *fh;
1023 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (t, NULL);
1024 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "0x%x\n", fh->h);
1027 if (GNUNET_CONTAINER_slist_contains
1028 (fds2->handles, h, sizeof (struct GNUNET_DISK_FileHandle)))
1031 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Match!\n");
1033 GNUNET_CONTAINER_slist_iter_destroy (it);
1036 GNUNET_CONTAINER_slist_next (it);
1038 GNUNET_CONTAINER_slist_iter_destroy (it);
1046 * @return a new fd set
1048 struct GNUNET_NETWORK_FDSet *
1049 GNUNET_NETWORK_fdset_create ()
1051 struct GNUNET_NETWORK_FDSet *fds;
1052 fds = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_FDSet));
1054 fds->handles = GNUNET_CONTAINER_slist_create ();
1056 GNUNET_NETWORK_fdset_zero (fds);
1062 * Releases the associated memory of an fd set
1066 GNUNET_NETWORK_fdset_destroy (struct GNUNET_NETWORK_FDSet *fds)
1069 GNUNET_CONTAINER_slist_destroy (fds->handles);
1075 * Check if sockets meet certain conditions
1076 * @param rfds set of sockets to be checked for readability
1077 * @param wfds set of sockets to be checked for writability
1078 * @param efds set of sockets to be checked for exceptions
1079 * @param timeout relative value when to return
1080 * @return number of selected sockets, GNUNET_SYSERR on error
1083 GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
1084 struct GNUNET_NETWORK_FDSet *wfds,
1085 struct GNUNET_NETWORK_FDSet *efds,
1086 const struct GNUNET_TIME_Relative timeout)
1092 int read_handles = 0;
1093 int write_handles = 0;
1099 int nsock = 0, nhandles = 0, nSockEvents = 0;
1101 static HANDLE hEventRead = 0;
1102 static HANDLE hEventWrite = 0;
1103 static HANDLE hEventException = 0;
1104 static HANDLE hEventPipeWrite = 0;
1105 static HANDLE hEventReadReady = 0;
1108 int writePipePos = 0;
1110 HANDLE handle_array[FD_SETSIZE + 2];
1111 int returncode = -1;
1112 DWORD newretcode = 0;
1113 int returnedpos = 0;
1115 struct GNUNET_CONTAINER_SList *handles_read, *handles_write, *handles_except;
1117 fd_set aread, awrite, aexcept;
1119 fd_set bread, bwrite, bexcept;
1122 /* TODO: Make this growable */
1123 struct GNUNET_DISK_FileHandle *readArray[50];
1131 handles += read_handles = GNUNET_CONTAINER_slist_count (rfds->handles);
1134 struct GNUNET_CONTAINER_SList_Iterator *t;
1135 for (t = GNUNET_CONTAINER_slist_begin (rfds->handles);
1136 GNUNET_CONTAINER_slist_end (t) != GNUNET_YES;
1137 GNUNET_CONTAINER_slist_next (t))
1139 struct GNUNET_DISK_FileHandle *fh;
1140 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (t, NULL);
1141 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "FD 0x%x (0x%x) is SET in rfds\n", fh->h, fh);
1149 nfds = GNUNET_MAX (nfds, wfds->nsds);
1151 handles += write_handles = GNUNET_CONTAINER_slist_count (wfds->handles);
1156 nfds = GNUNET_MAX (nfds, efds->nsds);
1158 handles += ex_handles = GNUNET_CONTAINER_slist_count (efds->handles);
1162 if ((nfds == 0) && (timeout.rel_value == GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
1168 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1170 ("Fatal internal logic error, process hangs in `%s' (abort with CTRL-C)!\n"),
1175 tv.tv_sec = timeout.rel_value / GNUNET_TIME_UNIT_SECONDS.rel_value;
1177 1000 * (timeout.rel_value - (tv.tv_sec * GNUNET_TIME_UNIT_SECONDS.rel_value));
1178 return select (nfds,
1179 (rfds != NULL) ? &rfds->sds : NULL,
1180 (wfds != NULL) ? &wfds->sds : NULL,
1181 (efds != NULL) ? &efds->sds : NULL,
1182 (timeout.rel_value == GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
1186 #define SAFE_FD_ISSET(fd, set) (set != NULL && FD_ISSET(fd, set))
1187 /* calculate how long we need to wait in milliseconds */
1188 if (timeout.rel_value == GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
1189 ms_total = INFINITE;
1191 ms_total = timeout.rel_value / GNUNET_TIME_UNIT_MILLISECONDS.rel_value;
1192 /* select() may be used as a portable way to sleep */
1193 if (!(rfds || wfds || efds))
1199 /* Events for sockets */
1201 hEventRead = CreateEvent (NULL, TRUE, FALSE, NULL);
1203 ResetEvent (hEventRead);
1204 if (!hEventReadReady)
1205 hEventReadReady = CreateEvent (NULL, TRUE, TRUE, NULL);
1207 hEventWrite = CreateEvent (NULL, TRUE, FALSE, NULL);
1209 ResetEvent (hEventWrite);
1210 if (!hEventException)
1211 hEventException = CreateEvent (NULL, TRUE, FALSE, NULL);
1213 ResetEvent (hEventException);
1215 /* Event for pipes */
1216 if (!hEventPipeWrite)
1217 hEventPipeWrite = CreateEvent (NULL, TRUE, TRUE, NULL);
1221 handles_read = GNUNET_CONTAINER_slist_create ();
1222 handles_write = GNUNET_CONTAINER_slist_create ();
1223 handles_except = GNUNET_CONTAINER_slist_create ();
1234 FD_COPY (&rfds->sds, &aread);
1236 FD_COPY (&rfds->sds, &bread);
1241 FD_COPY (&wfds->sds, &awrite);
1243 FD_COPY (&wfds->sds, &bwrite);
1248 FD_COPY (&efds->sds, &aexcept);
1250 FD_COPY (&efds->sds, &bexcept);
1253 /* We will first Add the PIPES to the events */
1255 if (rfds && read_handles)
1257 struct GNUNET_CONTAINER_SList_Iterator *i;
1258 for (i = GNUNET_CONTAINER_slist_begin (rfds->handles);
1259 GNUNET_CONTAINER_slist_end (i) != GNUNET_YES;
1260 GNUNET_CONTAINER_slist_next (i))
1262 struct GNUNET_DISK_FileHandle *fh;
1263 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (i, NULL);
1264 if (fh->type == GNUNET_PIPE)
1266 /* Read zero bytes to check the status of the pipe */
1268 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Reading 0 bytes from the pipe 0x%x\n", fh->h);
1270 if (!ReadFile (fh->h, NULL, 0, NULL, fh->oOverlapRead))
1272 DWORD error_code = GetLastError();
1273 if (error_code == ERROR_IO_PENDING)
1276 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding the pipe's 0x%x overlapped event to the array as %d\n", fh->h, nhandles);
1278 handle_array[nhandles++] = fh->oOverlapRead->hEvent;
1279 readArray[readPipes++] = fh;
1284 SetErrnoFromWinError (error_code);
1291 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding the read ready event to the array as %d\n", nhandles);
1293 handle_array[nhandles++] = hEventReadReady;
1294 readArray[readPipes++] = fh;
1299 GNUNET_CONTAINER_slist_add (handles_read,
1300 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1301 fh, sizeof (struct GNUNET_DISK_FileHandle));
1304 GNUNET_CONTAINER_slist_iter_destroy (i);
1306 if (wfds && write_handles)
1309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding the write ready event to the array as %d\n", nhandles);
1311 handle_array[nhandles++] = hEventPipeWrite;
1312 writePipePos = nhandles;
1314 if (efds && ex_handles)
1316 struct GNUNET_CONTAINER_SList_Iterator *i;
1317 for (i = GNUNET_CONTAINER_slist_begin (efds->handles);
1318 GNUNET_CONTAINER_slist_end (i) != GNUNET_YES;
1319 GNUNET_CONTAINER_slist_next (i))
1321 struct GNUNET_DISK_FileHandle *fh;
1324 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (i, NULL);
1325 if (fh->type == GNUNET_PIPE)
1327 if (!PeekNamedPipe (fh->h, NULL, 0, NULL, &dwBytes, NULL))
1329 GNUNET_CONTAINER_slist_add (handles_except,
1330 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1331 fh, sizeof (struct GNUNET_DISK_FileHandle));
1336 GNUNET_CONTAINER_slist_iter_destroy (i);
1343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding the socket read event to the array as %d\n", nhandles);
1345 handle_array[nhandles++] = hEventRead;
1347 for (i = 0; i < rfds->sds.fd_count; i++)
1349 WSAEventSelect (rfds->sds.fd_array[i], hEventRead, FD_ACCEPT | FD_READ | FD_CLOSE);
1357 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding the socket write event to the array as %d\n", nhandles);
1359 handle_array[nhandles++] = hEventWrite;
1361 for (i = 0; i < wfds->sds.fd_count; i++)
1365 status = send (wfds->sds.fd_array[i], NULL, 0, 0);
1366 error = GetLastError ();
1368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "pre-send to the socket %d returned %d (%u)\n", i, status, error);
1370 if (status == 0 || (error != WSAEWOULDBLOCK && error != WSAENOTCONN))
1372 WSAEventSelect (wfds->sds.fd_array[i], hEventWrite, FD_WRITE | FD_CONNECT | FD_CLOSE);
1376 SetEvent (hEventWrite);
1381 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding the socket error event to the array as %d\n", nhandles);
1383 handle_array[nhandles++] = hEventException;
1385 for (i = 0; i < efds->sds.fd_count; i++)
1387 WSAEventSelect (efds->sds.fd_array[i], hEventException, FD_OOB | FD_CLOSE);
1393 handle_array[nhandles] = NULL;
1396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Number nfds : %d\n", nfds);
1397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Number of handles : %d\n", nhandles);
1398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "retcode : %d\n", newretcode);
1399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Will wait : %d\n", ms_total);
1403 returncode = WaitForMultipleObjects (nhandles, handle_array, FALSE, ms_total);
1405 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "WaitForMultipleObjects Returned : %d\n", returncode);
1408 returnedpos = returncode - WAIT_OBJECT_0;
1410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return pos is : %d\n", returnedpos);
1413 /* FIXME: THIS LINE IS WRONG !! We should add to handles only handles that fired the events, not all ! */
1416 GNUNET_CONTAINER_slist_append (handles_read, rfds->handles);
1418 if (nhandles && (returnedpos < nhandles))
1424 struct timeval tvslice;
1426 tvslice.tv_usec = 10;
1427 retcode = select (nfds, &aread, &awrite, &aexcept, &tvslice);
1431 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Select retcode : %d\n", retcode);
1434 /* FIXME: <= writePipePos? Really? */
1435 if ((writePipePos != -1) && (returnedpos <= writePipePos))
1437 GNUNET_CONTAINER_slist_append (handles_write, wfds->handles);
1438 retcode += write_handles;
1440 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Added write pipe\n");
1444 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ReadPipes is : %d\n", readPipes);
1446 /* We have some pipes ready for read. */
1447 /* FIXME: it is supposed to work !! Only choose the Pipes who fired the event, but it is not working */
1449 if (returnedpos < readPipes)
1452 for (i = 0; i < readPipes; i++)
1454 waitstatus = WaitForSingleObject (handle_array[i], 0);
1455 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Read pipe %d wait status is : %d\n", i, waitstatus);
1456 if (waitstatus != WAIT_OBJECT_0)
1458 GNUNET_CONTAINER_slist_add (handles_read,
1459 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1460 readArray[i], sizeof (struct GNUNET_DISK_FileHandle));
1462 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Added read Pipe\n");
1465 for (i = 0; i < readPipes; i++)
1471 bret = PeekNamedPipe (readArray[i]->h, NULL, 0, NULL, &waitstatus, NULL);
1472 error = GetLastError ();
1474 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peek at read pipe %d (0x%x) returned %d (%d bytes available) GLE %u\n", i, readArray[i]->h, bret, waitstatus, error);
1476 if (bret == 0 || waitstatus <= 0)
1478 GNUNET_CONTAINER_slist_add (handles_read,
1479 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1480 readArray[i], sizeof (struct GNUNET_DISK_FileHandle));
1483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Added read Pipe 0x%x (0x%x)\n", readArray[i], readArray[i]->h);
1487 waitstatus = WaitForSingleObject (hEventWrite, 0);
1489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Wait for the write event returned %d\n", waitstatus);
1491 if (waitstatus == WAIT_OBJECT_0)
1493 for (i = 0; i < wfds->sds.fd_count; i++)
1498 int sizeof_so_error = sizeof (so_error);
1499 int gso_result = getsockopt (wfds->sds.fd_array[i], SOL_SOCKET, SO_ERROR, (char *) &so_error, &sizeof_so_error);
1500 status = send (wfds->sds.fd_array[i], NULL, 0, 0);
1501 error = GetLastError ();
1503 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send to the socket %d returned %d (%u)\n", i, status, error);
1506 || (error != WSAEWOULDBLOCK && error != WSAENOTCONN)
1507 || (status == -1 && gso_result == 0 && error == WSAENOTCONN && so_error == WSAECONNREFUSED))
1509 FD_SET (wfds->sds.fd_array[i], &awrite);
1516 if (!nhandles || (returnedpos >= nhandles))
1517 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Returning from _select() with nothing!\n");
1521 struct GNUNET_CONTAINER_SList_Iterator *t;
1522 for (i = 0; i < rfds->sds.fd_count; i++)
1524 WSAEventSelect (rfds->sds.fd_array[i], hEventRead, 0);
1527 for (t = GNUNET_CONTAINER_slist_begin (rfds->handles);
1528 GNUNET_CONTAINER_slist_end (t) != GNUNET_YES;
1529 GNUNET_CONTAINER_slist_next (t))
1531 struct GNUNET_DISK_FileHandle *fh;
1532 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (t, NULL);
1533 if (fh->type == GNUNET_PIPE)
1538 GNUNET_CONTAINER_slist_iter_destroy (t);
1540 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Zeroing rfds\n");
1542 GNUNET_NETWORK_fdset_zero (rfds);
1543 if (retcode != -1 && nhandles && (returnedpos < nhandles))
1544 GNUNET_NETWORK_fdset_copy_native (rfds, &aread, retcode);
1545 GNUNET_CONTAINER_slist_append (rfds->handles, handles_read);
1549 for (i = 0; i < wfds->sds.fd_count; i++)
1551 WSAEventSelect (wfds->sds.fd_array[i], hEventWrite, 0);
1555 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Zeroing wfds\n");
1557 GNUNET_NETWORK_fdset_zero (wfds);
1558 if (retcode != -1 && nhandles && (returnedpos < nhandles))
1559 GNUNET_NETWORK_fdset_copy_native (wfds, &awrite, retcode);
1560 GNUNET_CONTAINER_slist_append (wfds->handles, handles_write);
1564 for (i = 0; i < efds->sds.fd_count; i++)
1566 WSAEventSelect (efds->sds.fd_array[i], hEventException, 0);
1570 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Zeroing efds\n");
1572 GNUNET_NETWORK_fdset_zero (efds);
1573 if (retcode != -1 && nhandles && (returnedpos < nhandles))
1574 GNUNET_NETWORK_fdset_copy_native (efds, &aexcept, retcode);
1575 GNUNET_CONTAINER_slist_append (efds->handles, handles_except);
1577 GNUNET_CONTAINER_slist_destroy (handles_read);
1578 GNUNET_CONTAINER_slist_destroy (handles_write);
1579 GNUNET_CONTAINER_slist_destroy (handles_except);
1583 struct GNUNET_CONTAINER_SList_Iterator *t;
1584 for (i = 0; i < bread.fd_count; i++)
1586 if (bread.fd_array[i] != 0)
1587 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "FD 0x%x is %s in rfds\n", bread.fd_array[i], (SAFE_FD_ISSET (bread.fd_array[i], rfds)) ? "SET" : "NOT SET");
1589 for (t = GNUNET_CONTAINER_slist_begin (rfds->handles);
1590 GNUNET_CONTAINER_slist_end (t) != GNUNET_YES;
1591 GNUNET_CONTAINER_slist_next (t))
1593 struct GNUNET_DISK_FileHandle *fh;
1594 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (t, NULL);
1595 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "FD 0x%x is SET in rfds\n", fh->h);
1600 for (i = 0; i < bwrite.fd_count; i++)
1602 if (bwrite.fd_array[i] != 0)
1603 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "FD 0x%x is %s in wfds\n", bwrite.fd_array[i], (SAFE_FD_ISSET (bwrite.fd_array[i], rfds)) ? "SET" : "NOT SET");
1608 for (i = 0; i < bexcept.fd_count; i++)
1610 if (bexcept.fd_array[i] != 0)
1611 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "FD 0x%x is %s in efds\n", bexcept.fd_array[i], (SAFE_FD_ISSET (bexcept.fd_array[i], rfds)) ? "SET" : "NOT SET");
1614 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Returning %d or 0\n", retcode);
1616 if (nhandles && (returnedpos < nhandles))
1624 /* end of network.c */