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;
69 #define FD_COPY(s, d) (memcpy ((d), (s), sizeof (fd_set)))
74 * Set if a socket should use blocking or non-blocking IO.
76 * @param doBlock blocking mode
77 * @return GNUNET_OK on success, GNUNET_SYSERR on error
80 socket_set_blocking (struct GNUNET_NETWORK_Handle *fd, int doBlock)
87 if (ioctlsocket (fd->fd, FIONBIO, &mode) == SOCKET_ERROR)
90 SetErrnoFromWinsockError (WSAGetLastError ());
91 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "ioctlsocket");
98 int flags = fcntl (fd->fd, F_GETFL);
103 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "fcntl");
104 return GNUNET_SYSERR;
107 flags &= ~O_NONBLOCK;
111 if (0 != fcntl (fd->fd, F_SETFL, flags))
114 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "fcntl");
115 return GNUNET_SYSERR;
124 * Make a socket non-inheritable to child processes
126 * @param h the socket to make non-inheritable
127 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
128 * @warning Not implemented on Windows
131 socket_set_inheritable (const struct GNUNET_NETWORK_Handle *h)
135 i = fcntl (h->fd, F_GETFD);
137 return GNUNET_SYSERR;
138 if (i == (i | FD_CLOEXEC))
141 if (fcntl (h->fd, F_SETFD, i) < 0)
142 return GNUNET_SYSERR;
150 * The MSG_NOSIGNAL equivalent on Mac OS X
152 * @param h the socket to make non-delaying
155 socket_set_nosigpipe (const struct GNUNET_NETWORK_Handle *h)
160 setsockopt (h->fd, SOL_SOCKET, SO_NOSIGPIPE, &abs_value,
162 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
168 * Disable delays when sending data via the socket.
169 * (GNUnet makes sure that messages are as big as
172 * @param h the socket to make non-delaying
175 socket_set_nodelay (const struct GNUNET_NETWORK_Handle *h)
180 if (0 != setsockopt (h->fd, IPPROTO_TCP, TCP_NODELAY, &value, sizeof (value)))
181 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
183 const char *abs_value = "1";
186 setsockopt (h->fd, IPPROTO_TCP, TCP_NODELAY, abs_value,
188 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
194 * accept a new connection on a socket
196 * @param desc bound socket
197 * @param address address of the connecting peer, may be NULL
198 * @param address_len length of address
199 * @return client socket
201 struct GNUNET_NETWORK_Handle *
202 GNUNET_NETWORK_socket_accept (const struct GNUNET_NETWORK_Handle *desc,
203 struct sockaddr *address, socklen_t * address_len)
205 struct GNUNET_NETWORK_Handle *ret;
207 ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle));
210 struct sockaddr name;
211 int namelen = sizeof (name);
212 int gsn = getsockname (desc->fd, &name, &namelen);
215 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
216 "Accepting connection on `%s'\n",
217 GNUNET_a2s (&name, namelen));
220 ret->fd = accept (desc->fd, address, address_len);
222 ret->af = address->sa_family;
225 if (ret->fd == INVALID_SOCKET)
228 SetErrnoFromWinsockError (WSAGetLastError ());
234 if (ret->fd >= FD_SETSIZE)
236 GNUNET_break (0 == close (ret->fd));
242 if (GNUNET_SYSERR == socket_set_blocking (ret, GNUNET_NO))
246 /* we might want to treat this one as fatal... */
248 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret));
253 if (GNUNET_OK != socket_set_inheritable (ret))
254 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
255 "socket_set_inheritable");
258 socket_set_nosigpipe (ret);
261 if (ret->af != AF_UNIX)
263 socket_set_nodelay (ret);
269 * Bind to a connected socket
271 * @param address address to be bound
272 * @param address_len length of address
273 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
276 GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc,
277 const struct sockaddr *address,
278 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;
302 (void) unlink (un->sun_path);
306 ret = bind (desc->fd, address, address_len);
308 if (SOCKET_ERROR == ret)
309 SetErrnoFromWinsockError (WSAGetLastError ());
312 return GNUNET_SYSERR;
315 desc->addr = GNUNET_malloc (address_len);
316 memcpy (desc->addr, address, address_len);
317 desc->addrlen = address_len;
327 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
330 GNUNET_NETWORK_socket_close (struct GNUNET_NETWORK_Handle *desc)
338 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_NETWORK_socket_close",
339 "Closing 0x%x\n", desc->fd);
342 ret = closesocket (desc->fd);
343 error = WSAGetLastError ();
344 SetErrnoFromWinsockError (error);
346 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_NETWORK_socket_close",
347 "Closed 0x%x, closesocket() returned %d, GLE is %u\n",
348 desc->fd, ret, error);
351 ret = close (desc->fd);
355 if ((desc->af == AF_UNIX) && (NULL != desc->addr))
357 const struct sockaddr_un *un = (const struct sockaddr_un *) desc->addr;
359 if (0 != unlink (un->sun_path))
360 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
361 "unlink", un->sun_path);
365 GNUNET_free_non_null (desc->addr);
367 return (ret == 0) ? GNUNET_OK : GNUNET_SYSERR;
372 * Box a native socket (and check that it is a socket).
374 * @param fd socket to box
375 * @return NULL on error (including not supported on target platform)
377 struct GNUNET_NETWORK_Handle *
378 GNUNET_NETWORK_socket_box_native (int fd)
383 struct GNUNET_NETWORK_Handle *ret;
385 if (fcntl (fd, F_GETFD) < 0)
386 return NULL; /* invalid FD */
387 ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle));
398 * @param address peer address
399 * @param address_len length of address
400 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
403 GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Handle *desc,
404 const struct sockaddr *address,
405 socklen_t address_len)
409 ret = connect (desc->fd, address, address_len);
412 if (SOCKET_ERROR == ret)
414 SetErrnoFromWinsockError (WSAGetLastError ());
415 if (errno == EWOULDBLOCK)
419 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
427 * @param level protocol level of the option
428 * @param optname identifier of the option
429 * @param optval options
430 * @param optlen length of optval
431 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
434 GNUNET_NETWORK_socket_getsockopt (const struct GNUNET_NETWORK_Handle *desc,
435 int level, int optname, void *optval,
440 ret = getsockopt (desc->fd, level, optname, optval, optlen);
443 if (ret == 0 && level == SOL_SOCKET && optname == SO_ERROR)
444 *((int *) optval) = GetErrnoFromWinsockError (*((int *) optval));
446 else if (SOCKET_ERROR == ret)
447 SetErrnoFromWinsockError (WSAGetLastError ());
449 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
456 * @param backlog length of the listen queue
457 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
460 GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Handle *desc,
465 ret = listen (desc->fd, backlog);
468 if (SOCKET_ERROR == ret)
469 SetErrnoFromWinsockError (WSAGetLastError ());
472 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
477 * How much data is available to be read on this descriptor?
479 * Returns GNUNET_NO if no data is available, or on error!
483 GNUNET_NETWORK_socket_recvfrom_amount (const struct GNUNET_NETWORK_Handle
488 /* How much is there to be read? */
492 error = ioctl (desc->fd, FIONREAD, &pending);
497 error = ioctlsocket (desc->fd, FIONREAD, &pending);
498 if (error != SOCKET_ERROR)
507 * Read data from a connected socket (always non-blocking).
509 * @param buffer buffer
510 * @param length length of buffer
511 * @param src_addr either the source to recv from, or all zeroes
512 * to be filled in by recvfrom
513 * @param addrlen length of the addr
516 GNUNET_NETWORK_socket_recvfrom (const struct GNUNET_NETWORK_Handle * desc,
517 void *buffer, size_t length,
518 struct sockaddr * src_addr, socklen_t * addrlen)
526 flags |= MSG_DONTWAIT;
529 ret = recvfrom (desc->fd, buffer, length, flags, src_addr, addrlen);
531 if (SOCKET_ERROR == ret)
532 SetErrnoFromWinsockError (WSAGetLastError ());
539 * Read data from a connected socket (always non-blocking).
541 * @param buffer buffer
542 * @param length length of buffer
545 GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Handle * desc,
546 void *buffer, size_t length)
554 flags |= MSG_DONTWAIT;
556 ret = recv (desc->fd, buffer, length, flags);
558 if (SOCKET_ERROR == ret)
559 SetErrnoFromWinsockError (WSAGetLastError ());
566 * Send data (always non-blocking).
569 * @param buffer data to send
570 * @param length size of the buffer
571 * @return number of bytes sent, GNUNET_SYSERR on error
574 GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Handle * desc,
575 const void *buffer, size_t length)
583 flags |= MSG_DONTWAIT;
587 flags |= MSG_NOSIGNAL;
590 ret = send (desc->fd, buffer, length, flags);
593 if (SOCKET_ERROR == ret)
594 SetErrnoFromWinsockError (WSAGetLastError ());
602 * Send data to a particular destination (always non-blocking).
603 * This function only works for UDP sockets.
606 * @param message data to send
607 * @param length size of the data
608 * @param dest_addr destination address
609 * @param dest_len length of address
610 * @return number of bytes sent, GNUNET_SYSERR on error
613 GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Handle * desc,
614 const void *message, size_t length,
615 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)
765 setsockopt (desc->fd, SOL_SOCKET, SO_SNDBUF, (char *) &value,
767 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
770 setsockopt (desc->fd, SOL_SOCKET, SO_RCVBUF, (char *) &value,
772 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
776 setsockopt (desc->fd, SOL_SOCKET, SO_SNDBUF, &value, sizeof (value))))
777 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
780 setsockopt (desc->fd, SOL_SOCKET, SO_RCVBUF, &value, sizeof (value))))
781 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
784 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
793 GNUNET_NETWORK_fdset_zero (struct GNUNET_NETWORK_FDSet *fds)
798 GNUNET_CONTAINER_slist_clear (fds->handles);
803 * Add a socket to the FD set
805 * @param desc socket to add
808 GNUNET_NETWORK_fdset_set (struct GNUNET_NETWORK_FDSet *fds,
809 const struct GNUNET_NETWORK_Handle *desc)
811 FD_SET (desc->fd, &fds->sds);
812 if (desc->fd + 1 > fds->nsds)
813 fds->nsds = desc->fd + 1;
818 * Check whether a socket is part of the fd set
821 * @return 0 if the FD is not set
824 GNUNET_NETWORK_fdset_isset (const struct GNUNET_NETWORK_FDSet *fds,
825 const struct GNUNET_NETWORK_Handle *desc)
827 return FD_ISSET (desc->fd, &fds->sds);
832 * Add one fd set to another
833 * @param dst the fd set to add to
834 * @param src the fd set to add from
837 GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst,
838 const struct GNUNET_NETWORK_FDSet *src)
842 for (nfds = src->nsds; nfds > 0; nfds--)
843 if (FD_ISSET (nfds, &src->sds))
846 FD_SET (nfds, &dst->sds);
847 if (nfds + 1 > dst->nsds)
848 dst->nsds = nfds + 1;
851 GNUNET_CONTAINER_slist_append (dst->handles, src->handles);
857 * Copy one fd set to another
859 * @param to destination
863 GNUNET_NETWORK_fdset_copy (struct GNUNET_NETWORK_FDSet *to,
864 const struct GNUNET_NETWORK_FDSet *from)
866 FD_COPY (&from->sds, &to->sds);
867 to->nsds = from->nsds;
870 GNUNET_CONTAINER_slist_clear (to->handles);
871 GNUNET_CONTAINER_slist_append (to->handles, from->handles);
877 * Return file descriptor for this network handle
879 * @param desc wrapper to process
880 * @return POSIX file descriptor
883 GNUNET_NETWORK_get_fd (struct GNUNET_NETWORK_Handle *desc)
890 * Copy a native fd set
892 * @param to destination
893 * @param from native source set
894 * @param nfds the biggest socket number in from + 1
897 GNUNET_NETWORK_fdset_copy_native (struct GNUNET_NETWORK_FDSet *to,
898 const fd_set * from, int nfds)
900 FD_COPY (from, &to->sds);
906 * Set a native fd in a set
908 * @param to destination
909 * @param nfd native FD to set
912 GNUNET_NETWORK_fdset_set_native (struct GNUNET_NETWORK_FDSet *to, int nfd)
914 GNUNET_assert ((nfd >= 0) && (nfd < FD_SETSIZE));
915 FD_SET (nfd, &to->sds);
916 to->nsds = GNUNET_MAX (nfd + 1, to->nsds);
921 * Test native fd in a set
923 * @param to set to test, NULL for empty set
924 * @param nfd native FD to test, or -1 for none
925 * @return GNUNET_YES if FD is set in the set
928 GNUNET_NETWORK_fdset_test_native (const struct GNUNET_NETWORK_FDSet *to,
931 if ((nfd == -1) || (to == NULL))
933 return FD_ISSET (nfd, &to->sds) ? GNUNET_YES : GNUNET_NO;
938 * Add a file handle to the fd set
940 * @param h the file handle to add
943 GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds,
944 const struct GNUNET_DISK_FileHandle *h)
947 GNUNET_CONTAINER_slist_add (fds->handles,
948 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
949 h, sizeof (struct GNUNET_DISK_FileHandle));
954 GNUNET_DISK_internal_file_handle_ (h, &fd, sizeof (int));
955 FD_SET (fd, &fds->sds);
956 if (fd + 1 > fds->nsds)
964 * Check if a file handle is part of an fd set
966 * @param h file handle
967 * @return GNUNET_YES if the file handle is part of the set
970 GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds,
971 const struct GNUNET_DISK_FileHandle *h)
975 return GNUNET_CONTAINER_slist_contains (fds->handles, h,
977 GNUNET_DISK_FileHandle));
979 return FD_ISSET (h->fd, &fds->sds);
985 * Checks if two fd sets overlap
986 * @param fds1 first fd set
987 * @param fds2 second fd set
988 * @return GNUNET_YES if they do overlap, GNUNET_NO otherwise
991 GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1,
992 const struct GNUNET_NETWORK_FDSet *fds2)
998 if (nfds > fds2->nsds)
1003 if (FD_ISSET (nfds, &fds1->sds) && FD_ISSET (nfds, &fds2->sds))
1007 struct GNUNET_CONTAINER_SList_Iterator *it;
1008 struct GNUNET_DISK_FileHandle *h;
1012 /*This code is somewhat hacky, we are not supposed to know what's
1013 * inside of fd_set; also the O(n^2) is really bad... */
1015 for (i = 0; i < fds1->sds.fd_count; i++)
1017 for (j = 0; j < fds2->sds.fd_count; j++)
1019 if (fds1->sds.fd_array[i] == fds2->sds.fd_array[j])
1023 it = GNUNET_CONTAINER_slist_begin (fds1->handles);
1024 while (GNUNET_CONTAINER_slist_end (it) != GNUNET_YES)
1027 struct GNUNET_CONTAINER_SList_Iterator *t;
1029 h = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (it, NULL);
1031 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1032 "Checking that FD 0x%x is in another set:\n", h->h);
1033 for (t = GNUNET_CONTAINER_slist_begin (fds2->handles);
1034 GNUNET_CONTAINER_slist_end (t) != GNUNET_YES;
1035 GNUNET_CONTAINER_slist_next (t))
1037 struct GNUNET_DISK_FileHandle *fh;
1039 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (t,
1041 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "0x%x\n", fh->h);
1044 if (GNUNET_CONTAINER_slist_contains
1045 (fds2->handles, h, sizeof (struct GNUNET_DISK_FileHandle)))
1048 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Match!\n");
1050 GNUNET_CONTAINER_slist_iter_destroy (it);
1053 GNUNET_CONTAINER_slist_next (it);
1055 GNUNET_CONTAINER_slist_iter_destroy (it);
1063 * @return a new fd set
1065 struct GNUNET_NETWORK_FDSet *
1066 GNUNET_NETWORK_fdset_create ()
1068 struct GNUNET_NETWORK_FDSet *fds;
1070 fds = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_FDSet));
1072 fds->handles = GNUNET_CONTAINER_slist_create ();
1074 GNUNET_NETWORK_fdset_zero (fds);
1080 * Releases the associated memory of an fd set
1084 GNUNET_NETWORK_fdset_destroy (struct GNUNET_NETWORK_FDSet *fds)
1087 GNUNET_CONTAINER_slist_destroy (fds->handles);
1093 * Check if sockets meet certain conditions
1094 * @param rfds set of sockets to be checked for readability
1095 * @param wfds set of sockets to be checked for writability
1096 * @param efds set of sockets to be checked for exceptions
1097 * @param timeout relative value when to return
1098 * @return number of selected sockets, GNUNET_SYSERR on error
1101 GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
1102 struct GNUNET_NETWORK_FDSet *wfds,
1103 struct GNUNET_NETWORK_FDSet *efds,
1104 const struct GNUNET_TIME_Relative timeout)
1111 int read_handles = 0;
1112 int write_handles = 0;
1118 int nsock = 0, nhandles = 0, nSockEvents = 0;
1120 static HANDLE hEventRead = 0;
1121 static HANDLE hEventWrite = 0;
1122 static HANDLE hEventException = 0;
1123 static HANDLE hEventPipeWrite = 0;
1124 static HANDLE hEventReadReady = 0;
1127 int writePipePos = 0;
1129 HANDLE handle_array[FD_SETSIZE + 2];
1130 int returncode = -1;
1131 DWORD newretcode = 0;
1132 int returnedpos = 0;
1134 struct GNUNET_CONTAINER_SList *handles_read, *handles_write, *handles_except;
1136 fd_set aread, awrite, aexcept;
1139 fd_set bread, bwrite, bexcept;
1142 /* TODO: Make this growable */
1143 struct GNUNET_DISK_FileHandle *readArray[50];
1151 handles += read_handles = GNUNET_CONTAINER_slist_count (rfds->handles);
1154 struct GNUNET_CONTAINER_SList_Iterator *t;
1156 for (t = GNUNET_CONTAINER_slist_begin (rfds->handles);
1157 GNUNET_CONTAINER_slist_end (t) != GNUNET_YES;
1158 GNUNET_CONTAINER_slist_next (t))
1160 struct GNUNET_DISK_FileHandle *fh;
1162 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (t,
1164 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "FD 0x%x (0x%x) is SET in rfds\n",
1173 nfds = GNUNET_MAX (nfds, wfds->nsds);
1175 handles += write_handles = GNUNET_CONTAINER_slist_count (wfds->handles);
1180 nfds = GNUNET_MAX (nfds, efds->nsds);
1182 handles += ex_handles = GNUNET_CONTAINER_slist_count (efds->handles);
1187 (timeout.rel_value == GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
1193 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1195 ("Fatal internal logic error, process hangs in `%s' (abort with CTRL-C)!\n"),
1200 tv.tv_sec = timeout.rel_value / GNUNET_TIME_UNIT_SECONDS.rel_value;
1202 1000 * (timeout.rel_value -
1203 (tv.tv_sec * GNUNET_TIME_UNIT_SECONDS.rel_value));
1204 return select (nfds, (rfds != NULL) ? &rfds->sds : NULL,
1205 (wfds != NULL) ? &wfds->sds : NULL,
1206 (efds != NULL) ? &efds->sds : NULL,
1207 (timeout.rel_value ==
1208 GNUNET_TIME_UNIT_FOREVER_REL.rel_value) ? NULL : &tv);
1211 #define SAFE_FD_ISSET(fd, set) (set != NULL && FD_ISSET(fd, set))
1212 /* calculate how long we need to wait in milliseconds */
1213 if (timeout.rel_value == GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
1214 ms_total = INFINITE;
1216 ms_total = timeout.rel_value / GNUNET_TIME_UNIT_MILLISECONDS.rel_value;
1217 /* select() may be used as a portable way to sleep */
1218 if (!(rfds || wfds || efds))
1224 /* Events for sockets */
1226 hEventRead = CreateEvent (NULL, TRUE, FALSE, NULL);
1228 ResetEvent (hEventRead);
1229 if (!hEventReadReady)
1230 hEventReadReady = CreateEvent (NULL, TRUE, TRUE, NULL);
1232 hEventWrite = CreateEvent (NULL, TRUE, FALSE, NULL);
1234 ResetEvent (hEventWrite);
1235 if (!hEventException)
1236 hEventException = CreateEvent (NULL, TRUE, FALSE, NULL);
1238 ResetEvent (hEventException);
1240 /* Event for pipes */
1241 if (!hEventPipeWrite)
1242 hEventPipeWrite = CreateEvent (NULL, TRUE, TRUE, NULL);
1246 handles_read = GNUNET_CONTAINER_slist_create ();
1247 handles_write = GNUNET_CONTAINER_slist_create ();
1248 handles_except = GNUNET_CONTAINER_slist_create ();
1259 FD_COPY (&rfds->sds, &aread);
1261 FD_COPY (&rfds->sds, &bread);
1266 FD_COPY (&wfds->sds, &awrite);
1268 FD_COPY (&wfds->sds, &bwrite);
1273 FD_COPY (&efds->sds, &aexcept);
1275 FD_COPY (&efds->sds, &bexcept);
1278 /* We will first Add the PIPES to the events */
1280 if (rfds && read_handles)
1282 struct GNUNET_CONTAINER_SList_Iterator *i;
1284 for (i = GNUNET_CONTAINER_slist_begin (rfds->handles);
1285 GNUNET_CONTAINER_slist_end (i) != GNUNET_YES;
1286 GNUNET_CONTAINER_slist_next (i))
1288 struct GNUNET_DISK_FileHandle *fh;
1290 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (i,
1292 if (fh->type == GNUNET_PIPE)
1294 /* Read zero bytes to check the status of the pipe */
1296 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1297 "Reading 0 bytes from the pipe 0x%x\n", fh->h);
1299 if (!ReadFile (fh->h, NULL, 0, NULL, fh->oOverlapRead))
1301 DWORD error_code = GetLastError ();
1303 if (error_code == ERROR_IO_PENDING)
1306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1307 "Adding the pipe's 0x%x overlapped event to the array as %d\n",
1310 handle_array[nhandles++] = fh->oOverlapRead->hEvent;
1311 readArray[readPipes++] = fh;
1316 * SetErrnoFromWinError (error_code);
1323 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1324 "Adding the read ready event to the array as %d\n",
1327 handle_array[nhandles++] = hEventReadReady;
1328 readArray[readPipes++] = fh;
1333 GNUNET_CONTAINER_slist_add (handles_read,
1334 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1335 fh, sizeof (struct GNUNET_DISK_FileHandle));
1338 GNUNET_CONTAINER_slist_iter_destroy (i);
1340 if (wfds && write_handles)
1343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1344 "Adding the write ready event to the array as %d\n", nhandles);
1346 handle_array[nhandles++] = hEventPipeWrite;
1347 writePipePos = nhandles;
1349 if (efds && ex_handles)
1351 struct GNUNET_CONTAINER_SList_Iterator *i;
1353 for (i = GNUNET_CONTAINER_slist_begin (efds->handles);
1354 GNUNET_CONTAINER_slist_end (i) != GNUNET_YES;
1355 GNUNET_CONTAINER_slist_next (i))
1357 struct GNUNET_DISK_FileHandle *fh;
1360 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (i,
1362 if (fh->type == GNUNET_PIPE)
1364 if (!PeekNamedPipe (fh->h, NULL, 0, NULL, &dwBytes, NULL))
1366 GNUNET_CONTAINER_slist_add (handles_except,
1367 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1369 sizeof (struct GNUNET_DISK_FileHandle));
1374 GNUNET_CONTAINER_slist_iter_destroy (i);
1381 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1382 "Adding the socket read event to the array as %d\n",
1385 handle_array[nhandles++] = hEventRead;
1387 for (i = 0; i < rfds->sds.fd_count; i++)
1389 WSAEventSelect (rfds->sds.fd_array[i], hEventRead,
1390 FD_ACCEPT | FD_READ | FD_CLOSE);
1399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1400 "Adding the socket write event to the array as %d\n",
1403 handle_array[nhandles++] = hEventWrite;
1405 for (i = 0; i < wfds->sds.fd_count; i++)
1410 status = send (wfds->sds.fd_array[i], NULL, 0, 0);
1411 error = GetLastError ();
1413 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1414 "pre-send to the socket %d returned %d (%u)\n", i, status,
1417 if (status == 0 || (error != WSAEWOULDBLOCK && error != WSAENOTCONN))
1419 WSAEventSelect (wfds->sds.fd_array[i], hEventWrite,
1420 FD_WRITE | FD_CONNECT | FD_CLOSE);
1424 SetEvent (hEventWrite);
1429 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1430 "Adding the socket error event to the array as %d\n",
1433 handle_array[nhandles++] = hEventException;
1435 for (i = 0; i < efds->sds.fd_count; i++)
1437 WSAEventSelect (efds->sds.fd_array[i], hEventException,
1444 handle_array[nhandles] = NULL;
1447 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Number nfds : %d\n", nfds);
1448 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Number of handles : %d\n", nhandles);
1449 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "retcode : %d\n", newretcode);
1450 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Will wait : %d\n", ms_total);
1455 WaitForMultipleObjects (nhandles, handle_array, FALSE, ms_total);
1457 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "WaitForMultipleObjects Returned : %d\n",
1461 returnedpos = returncode - WAIT_OBJECT_0;
1463 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return pos is : %d\n", returnedpos);
1466 /* FIXME: THIS LINE IS WRONG !! We should add to handles only handles that fired the events, not all ! */
1469 * GNUNET_CONTAINER_slist_append (handles_read, rfds->handles);
1471 if (nhandles && (returnedpos < nhandles))
1478 struct timeval tvslice;
1481 tvslice.tv_usec = 10;
1482 retcode = select (nfds, &aread, &awrite, &aexcept, &tvslice);
1486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Select retcode : %d\n", retcode);
1489 /* FIXME: <= writePipePos? Really? */
1490 if ((writePipePos != -1) && (returnedpos <= writePipePos))
1492 GNUNET_CONTAINER_slist_append (handles_write, wfds->handles);
1493 retcode += write_handles;
1495 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Added write pipe\n");
1499 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ReadPipes is : %d\n", readPipes);
1501 /* We have some pipes ready for read. */
1502 /* FIXME: it is supposed to work !! Only choose the Pipes who fired the event, but it is not working */
1504 if (returnedpos < readPipes)
1507 * for (i = 0; i < readPipes; i++)
1509 * waitstatus = WaitForSingleObject (handle_array[i], 0);
1510 * GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Read pipe %d wait status is : %d\n", i, waitstatus);
1511 * if (waitstatus != WAIT_OBJECT_0)
1513 * GNUNET_CONTAINER_slist_add (handles_read,
1514 * GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1515 * readArray[i], sizeof (struct GNUNET_DISK_FileHandle));
1517 * GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Added read Pipe\n");
1520 for (i = 0; i < readPipes; i++)
1528 PeekNamedPipe (readArray[i]->h, NULL, 0, NULL, &waitstatus, NULL);
1529 error = GetLastError ();
1531 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1532 "Peek at read pipe %d (0x%x) returned %d (%d bytes available) GLE %u\n",
1533 i, readArray[i]->h, bret, waitstatus, error);
1535 if (bret == 0 || waitstatus <= 0)
1537 GNUNET_CONTAINER_slist_add (handles_read,
1538 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1540 sizeof (struct GNUNET_DISK_FileHandle));
1543 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Added read Pipe 0x%x (0x%x)\n",
1544 readArray[i], readArray[i]->h);
1548 waitstatus = WaitForSingleObject (hEventWrite, 0);
1550 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1551 "Wait for the write event returned %d\n", waitstatus);
1553 if (waitstatus == WAIT_OBJECT_0)
1555 for (i = 0; i < wfds->sds.fd_count; i++)
1560 int sizeof_so_error = sizeof (so_error);
1562 getsockopt (wfds->sds.fd_array[i], SOL_SOCKET, SO_ERROR,
1563 (char *) &so_error, &sizeof_so_error);
1565 status = send (wfds->sds.fd_array[i], NULL, 0, 0);
1566 error = GetLastError ();
1568 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1569 "send to the socket %d returned %d (%u)\n", i, status,
1573 || (error != WSAEWOULDBLOCK && error != WSAENOTCONN)
1574 || (status == -1 && gso_result == 0 && error == WSAENOTCONN &&
1575 so_error == WSAECONNREFUSED))
1577 FD_SET (wfds->sds.fd_array[i], &awrite);
1584 if (!nhandles || (returnedpos >= nhandles))
1585 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1586 "Returning from _select() with nothing!\n");
1590 struct GNUNET_CONTAINER_SList_Iterator *t;
1592 for (i = 0; i < rfds->sds.fd_count; i++)
1594 WSAEventSelect (rfds->sds.fd_array[i], hEventRead, 0);
1597 for (t = GNUNET_CONTAINER_slist_begin (rfds->handles);
1598 GNUNET_CONTAINER_slist_end (t) != GNUNET_YES;
1599 GNUNET_CONTAINER_slist_next (t))
1601 struct GNUNET_DISK_FileHandle *fh;
1603 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (t,
1605 if (fh->type == GNUNET_PIPE)
1610 GNUNET_CONTAINER_slist_iter_destroy (t);
1612 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Zeroing rfds\n");
1614 GNUNET_NETWORK_fdset_zero (rfds);
1615 if (retcode != -1 && nhandles && (returnedpos < nhandles))
1616 GNUNET_NETWORK_fdset_copy_native (rfds, &aread, retcode);
1617 GNUNET_CONTAINER_slist_append (rfds->handles, handles_read);
1621 for (i = 0; i < wfds->sds.fd_count; i++)
1623 WSAEventSelect (wfds->sds.fd_array[i], hEventWrite, 0);
1627 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Zeroing wfds\n");
1629 GNUNET_NETWORK_fdset_zero (wfds);
1630 if (retcode != -1 && nhandles && (returnedpos < nhandles))
1631 GNUNET_NETWORK_fdset_copy_native (wfds, &awrite, retcode);
1632 GNUNET_CONTAINER_slist_append (wfds->handles, handles_write);
1636 for (i = 0; i < efds->sds.fd_count; i++)
1638 WSAEventSelect (efds->sds.fd_array[i], hEventException, 0);
1642 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Zeroing efds\n");
1644 GNUNET_NETWORK_fdset_zero (efds);
1645 if (retcode != -1 && nhandles && (returnedpos < nhandles))
1646 GNUNET_NETWORK_fdset_copy_native (efds, &aexcept, retcode);
1647 GNUNET_CONTAINER_slist_append (efds->handles, handles_except);
1649 GNUNET_CONTAINER_slist_destroy (handles_read);
1650 GNUNET_CONTAINER_slist_destroy (handles_write);
1651 GNUNET_CONTAINER_slist_destroy (handles_except);
1655 struct GNUNET_CONTAINER_SList_Iterator *t;
1657 for (i = 0; i < bread.fd_count; i++)
1659 if (bread.fd_array[i] != 0)
1660 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "FD 0x%x is %s in rfds\n",
1662 (SAFE_FD_ISSET (bread.fd_array[i], rfds)) ? "SET" :
1665 for (t = GNUNET_CONTAINER_slist_begin (rfds->handles);
1666 GNUNET_CONTAINER_slist_end (t) != GNUNET_YES;
1667 GNUNET_CONTAINER_slist_next (t))
1669 struct GNUNET_DISK_FileHandle *fh;
1671 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (t,
1673 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "FD 0x%x is SET in rfds\n", fh->h);
1678 for (i = 0; i < bwrite.fd_count; i++)
1680 if (bwrite.fd_array[i] != 0)
1681 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "FD 0x%x is %s in wfds\n",
1683 (SAFE_FD_ISSET (bwrite.fd_array[i], rfds)) ? "SET" :
1689 for (i = 0; i < bexcept.fd_count; i++)
1691 if (bexcept.fd_array[i] != 0)
1692 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "FD 0x%x is %s in efds\n",
1693 bexcept.fd_array[i],
1694 (SAFE_FD_ISSET (bexcept.fd_array[i], rfds)) ? "SET" :
1698 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Returning %d or 0\n", retcode);
1700 if (nhandles && (returnedpos < nhandles))
1707 /* end of network.c */