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, "Accepting connection on `%s'\n",
216 GNUNET_a2s (&name, namelen));
219 ret->fd = accept (desc->fd, address, address_len);
221 ret->af = address->sa_family;
224 if (ret->fd == INVALID_SOCKET)
227 SetErrnoFromWinsockError (WSAGetLastError ());
233 if (ret->fd >= FD_SETSIZE)
235 GNUNET_break (0 == close (ret->fd));
241 if (GNUNET_SYSERR == socket_set_blocking (ret, GNUNET_NO))
245 /* we might want to treat this one as fatal... */
247 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret));
252 if (GNUNET_OK != socket_set_inheritable (ret))
253 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
254 "socket_set_inheritable");
257 socket_set_nosigpipe (ret);
260 if (ret->af != AF_UNIX)
262 socket_set_nodelay (ret);
268 * Bind to a connected socket
270 * @param address address to be bound
271 * @param address_len length of address
272 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
275 GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc,
276 const struct sockaddr *address,
277 socklen_t address_len)
285 if (desc->af == AF_INET6)
286 if (0 != setsockopt (desc->fd, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof (on)))
287 GNUNET_log_strerror (GNUNET_ERROR_TYPE_DEBUG, "setsockopt");
291 /* This is required, and required here, but only on UNIX */
292 if (0 != setsockopt (desc->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)))
293 GNUNET_log_strerror (GNUNET_ERROR_TYPE_DEBUG, "setsockopt");
297 if (address->sa_family == AF_UNIX)
299 const struct sockaddr_un *un = (const struct sockaddr_un *) address;
301 (void) unlink (un->sun_path);
305 ret = bind (desc->fd, address, address_len);
307 if (SOCKET_ERROR == ret)
308 SetErrnoFromWinsockError (WSAGetLastError ());
311 return GNUNET_SYSERR;
314 desc->addr = GNUNET_malloc (address_len);
315 memcpy (desc->addr, address, address_len);
316 desc->addrlen = address_len;
326 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
329 GNUNET_NETWORK_socket_close (struct GNUNET_NETWORK_Handle *desc)
337 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_NETWORK_socket_close",
338 "Closing 0x%x\n", desc->fd);
341 ret = closesocket (desc->fd);
342 error = WSAGetLastError ();
343 SetErrnoFromWinsockError (error);
345 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_NETWORK_socket_close",
346 "Closed 0x%x, closesocket() returned %d, GLE is %u\n",
347 desc->fd, ret, error);
350 ret = close (desc->fd);
354 if ((desc->af == AF_UNIX) && (NULL != desc->addr))
356 const struct sockaddr_un *un = (const struct sockaddr_un *) desc->addr;
358 if (0 != unlink (un->sun_path))
359 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
364 GNUNET_free_non_null (desc->addr);
366 return (ret == 0) ? GNUNET_OK : GNUNET_SYSERR;
371 * Box a native socket (and check that it is a socket).
373 * @param fd socket to box
374 * @return NULL on error (including not supported on target platform)
376 struct GNUNET_NETWORK_Handle *
377 GNUNET_NETWORK_socket_box_native (int fd)
382 struct GNUNET_NETWORK_Handle *ret;
384 if (fcntl (fd, F_GETFD) < 0)
385 return NULL; /* invalid FD */
386 ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle));
397 * @param address peer address
398 * @param address_len length of address
399 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
402 GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Handle *desc,
403 const struct sockaddr *address,
404 socklen_t address_len)
408 ret = connect (desc->fd, address, address_len);
411 if (SOCKET_ERROR == ret)
413 SetErrnoFromWinsockError (WSAGetLastError ());
414 if (errno == EWOULDBLOCK)
418 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
426 * @param level protocol level of the option
427 * @param optname identifier of the option
428 * @param optval options
429 * @param optlen length of optval
430 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
433 GNUNET_NETWORK_socket_getsockopt (const struct GNUNET_NETWORK_Handle *desc,
434 int level, int optname, void *optval,
439 ret = getsockopt (desc->fd, level, optname, optval, optlen);
442 if (ret == 0 && level == SOL_SOCKET && optname == SO_ERROR)
443 *((int *) optval) = GetErrnoFromWinsockError (*((int *) optval));
445 else if (SOCKET_ERROR == ret)
446 SetErrnoFromWinsockError (WSAGetLastError ());
448 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
455 * @param backlog length of the listen queue
456 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
459 GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Handle *desc,
464 ret = listen (desc->fd, backlog);
467 if (SOCKET_ERROR == ret)
468 SetErrnoFromWinsockError (WSAGetLastError ());
471 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
476 * How much data is available to be read on this descriptor?
478 * Returns GNUNET_NO if no data is available, or on error!
482 GNUNET_NETWORK_socket_recvfrom_amount (const struct GNUNET_NETWORK_Handle *
487 /* How much is there to be read? */
491 error = ioctl (desc->fd, FIONREAD, &pending);
496 error = ioctlsocket (desc->fd, FIONREAD, &pending);
497 if (error != SOCKET_ERROR)
506 * Read data from a connected socket (always non-blocking).
508 * @param buffer buffer
509 * @param length length of buffer
510 * @param src_addr either the source to recv from, or all zeroes
511 * to be filled in by recvfrom
512 * @param addrlen length of the addr
515 GNUNET_NETWORK_socket_recvfrom (const struct GNUNET_NETWORK_Handle * desc,
516 void *buffer, size_t length,
517 struct sockaddr * src_addr, socklen_t * addrlen)
525 flags |= MSG_DONTWAIT;
528 ret = recvfrom (desc->fd, buffer, length, flags, src_addr, addrlen);
530 if (SOCKET_ERROR == ret)
531 SetErrnoFromWinsockError (WSAGetLastError ());
538 * Read data from a connected socket (always non-blocking).
540 * @param buffer buffer
541 * @param length length of buffer
544 GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Handle * desc,
545 void *buffer, size_t length)
553 flags |= MSG_DONTWAIT;
555 ret = recv (desc->fd, buffer, length, flags);
557 if (SOCKET_ERROR == ret)
558 SetErrnoFromWinsockError (WSAGetLastError ());
565 * Send data (always non-blocking).
568 * @param buffer data to send
569 * @param length size of the buffer
570 * @return number of bytes sent, GNUNET_SYSERR on error
573 GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Handle * desc,
574 const void *buffer, size_t length)
582 flags |= MSG_DONTWAIT;
586 flags |= MSG_NOSIGNAL;
589 ret = send (desc->fd, buffer, length, flags);
592 if (SOCKET_ERROR == ret)
593 SetErrnoFromWinsockError (WSAGetLastError ());
601 * Send data to a particular destination (always non-blocking).
602 * This function only works for UDP sockets.
605 * @param message data to send
606 * @param length size of the data
607 * @param dest_addr destination address
608 * @param dest_len length of address
609 * @return number of bytes sent, GNUNET_SYSERR on error
612 GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Handle * desc,
613 const void *message, size_t length,
614 const struct sockaddr * dest_addr,
623 flags |= MSG_DONTWAIT;
626 flags |= MSG_NOSIGNAL;
628 ret = sendto (desc->fd, message, length, flags, dest_addr, dest_len);
630 if (SOCKET_ERROR == ret)
631 SetErrnoFromWinsockError (WSAGetLastError ());
640 * @param level protocol level of the option
641 * @param option_name option identifier
642 * @param option_value value to set
643 * @param option_len size of option_value
644 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
647 GNUNET_NETWORK_socket_setsockopt (struct GNUNET_NETWORK_Handle *fd, int level,
648 int option_name, const void *option_value,
649 socklen_t option_len)
653 ret = setsockopt (fd->fd, level, option_name, option_value, option_len);
655 if (SOCKET_ERROR == ret)
656 SetErrnoFromWinsockError (WSAGetLastError ());
658 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
663 * Create a new socket. Configure it for non-blocking IO and
664 * mark it as non-inheritable to child processes (set the
665 * close-on-exec flag).
667 * @param domain domain of the socket
668 * @param type socket type
669 * @param protocol network protocol
670 * @return new socket, NULL on error
672 struct GNUNET_NETWORK_Handle *
673 GNUNET_NETWORK_socket_create (int domain, int type, int protocol)
675 struct GNUNET_NETWORK_Handle *ret;
677 ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle));
679 ret->fd = socket (domain, type, protocol);
680 if (INVALID_SOCKET == ret->fd)
683 SetErrnoFromWinsockError (WSAGetLastError ());
690 if (ret->fd >= FD_SETSIZE)
692 GNUNET_break (0 == close (ret->fd));
699 if (GNUNET_SYSERR == socket_set_blocking (ret, GNUNET_NO))
701 /* we might want to treat this one as fatal... */
703 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret));
708 if (GNUNET_OK != socket_set_inheritable (ret))
709 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
710 "socket_set_inheritable");
713 socket_set_nosigpipe (ret);
715 if ((type == SOCK_STREAM)
717 && (domain != AF_UNIX)
720 socket_set_nodelay (ret);
726 * Shut down socket operations
728 * @param how type of shutdown
729 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
732 GNUNET_NETWORK_socket_shutdown (struct GNUNET_NETWORK_Handle *desc, int how)
736 ret = shutdown (desc->fd, how);
739 SetErrnoFromWinsockError (WSAGetLastError ());
741 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
746 * Disable the "CORK" feature for communication with the given socket,
747 * forcing the OS to immediately flush the buffer on transmission
748 * instead of potentially buffering multiple messages. Essentially
749 * reduces the OS send buffers to zero.
752 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
755 GNUNET_NETWORK_socket_disable_corking (struct GNUNET_NETWORK_Handle *desc)
763 setsockopt (desc->fd, SOL_SOCKET, SO_SNDBUF, (char *) &value,
765 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
768 setsockopt (desc->fd, SOL_SOCKET, SO_RCVBUF, (char *) &value,
770 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");
783 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
792 GNUNET_NETWORK_fdset_zero (struct GNUNET_NETWORK_FDSet *fds)
797 GNUNET_CONTAINER_slist_clear (fds->handles);
802 * Add a socket to the FD set
804 * @param desc socket to add
807 GNUNET_NETWORK_fdset_set (struct GNUNET_NETWORK_FDSet *fds,
808 const struct GNUNET_NETWORK_Handle *desc)
810 FD_SET (desc->fd, &fds->sds);
811 if (desc->fd + 1 > fds->nsds)
812 fds->nsds = desc->fd + 1;
817 * Check whether a socket is part of the fd set
820 * @return 0 if the FD is not set
823 GNUNET_NETWORK_fdset_isset (const struct GNUNET_NETWORK_FDSet *fds,
824 const struct GNUNET_NETWORK_Handle *desc)
826 return FD_ISSET (desc->fd, &fds->sds);
831 * Add one fd set to another
832 * @param dst the fd set to add to
833 * @param src the fd set to add from
836 GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst,
837 const struct GNUNET_NETWORK_FDSet *src)
841 for (nfds = src->nsds; nfds > 0; nfds--)
842 if (FD_ISSET (nfds, &src->sds))
845 FD_SET (nfds, &dst->sds);
846 if (nfds + 1 > dst->nsds)
847 dst->nsds = nfds + 1;
850 GNUNET_CONTAINER_slist_append (dst->handles, src->handles);
856 * Copy one fd set to another
858 * @param to destination
862 GNUNET_NETWORK_fdset_copy (struct GNUNET_NETWORK_FDSet *to,
863 const struct GNUNET_NETWORK_FDSet *from)
865 FD_COPY (&from->sds, &to->sds);
866 to->nsds = from->nsds;
869 GNUNET_CONTAINER_slist_clear (to->handles);
870 GNUNET_CONTAINER_slist_append (to->handles, from->handles);
876 * Return file descriptor for this network handle
878 * @param desc wrapper to process
879 * @return POSIX file descriptor
882 GNUNET_NETWORK_get_fd (struct GNUNET_NETWORK_Handle *desc)
889 * Copy a native fd set
891 * @param to destination
892 * @param from native source set
893 * @param nfds the biggest socket number in from + 1
896 GNUNET_NETWORK_fdset_copy_native (struct GNUNET_NETWORK_FDSet *to,
897 const fd_set * from, int nfds)
899 FD_COPY (from, &to->sds);
905 * Set a native fd in a set
907 * @param to destination
908 * @param nfd native FD to set
911 GNUNET_NETWORK_fdset_set_native (struct GNUNET_NETWORK_FDSet *to, int nfd)
913 GNUNET_assert ((nfd >= 0) && (nfd < FD_SETSIZE));
914 FD_SET (nfd, &to->sds);
915 to->nsds = GNUNET_MAX (nfd + 1, to->nsds);
920 * Test native fd in a set
922 * @param to set to test, NULL for empty set
923 * @param nfd native FD to test, or -1 for none
924 * @return GNUNET_YES if FD is set in the set
927 GNUNET_NETWORK_fdset_test_native (const struct GNUNET_NETWORK_FDSet *to,
930 if ((nfd == -1) || (to == NULL))
932 return FD_ISSET (nfd, &to->sds) ? GNUNET_YES : GNUNET_NO;
937 * Add a file handle to the fd set
939 * @param h the file handle to add
942 GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds,
943 const struct GNUNET_DISK_FileHandle *h)
946 GNUNET_CONTAINER_slist_add (fds->handles,
947 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT, h,
948 sizeof (struct GNUNET_DISK_FileHandle));
953 GNUNET_DISK_internal_file_handle_ (h, &fd, sizeof (int));
954 FD_SET (fd, &fds->sds);
955 if (fd + 1 > fds->nsds)
963 * Check if a file handle is part of an fd set
965 * @param h file handle
966 * @return GNUNET_YES if the file handle is part of the set
969 GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds,
970 const struct GNUNET_DISK_FileHandle *h)
974 return GNUNET_CONTAINER_slist_contains (fds->handles, h,
976 GNUNET_DISK_FileHandle));
978 return FD_ISSET (h->fd, &fds->sds);
984 * Checks if two fd sets overlap
985 * @param fds1 first fd set
986 * @param fds2 second fd set
987 * @return GNUNET_YES if they do overlap, GNUNET_NO otherwise
990 GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1,
991 const struct GNUNET_NETWORK_FDSet *fds2)
997 if (nfds > fds2->nsds)
1002 if (FD_ISSET (nfds, &fds1->sds) && FD_ISSET (nfds, &fds2->sds))
1006 struct GNUNET_CONTAINER_SList_Iterator *it;
1007 struct GNUNET_DISK_FileHandle *h;
1011 /*This code is somewhat hacky, we are not supposed to know what's
1012 * inside of fd_set; also the O(n^2) is really bad... */
1014 for (i = 0; i < fds1->sds.fd_count; i++)
1016 for (j = 0; j < fds2->sds.fd_count; j++)
1018 if (fds1->sds.fd_array[i] == fds2->sds.fd_array[j])
1022 it = GNUNET_CONTAINER_slist_begin (fds1->handles);
1023 while (GNUNET_CONTAINER_slist_end (it) != GNUNET_YES)
1026 struct GNUNET_CONTAINER_SList_Iterator *t;
1028 h = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (it, NULL);
1030 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1031 "Checking that FD 0x%x is in another set:\n", h->h);
1032 for (t = GNUNET_CONTAINER_slist_begin (fds2->handles);
1033 GNUNET_CONTAINER_slist_end (t) != GNUNET_YES;
1034 GNUNET_CONTAINER_slist_next (t))
1036 struct GNUNET_DISK_FileHandle *fh;
1038 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (t,
1040 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "0x%x\n", fh->h);
1043 if (GNUNET_CONTAINER_slist_contains
1044 (fds2->handles, h, sizeof (struct GNUNET_DISK_FileHandle)))
1047 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Match!\n");
1049 GNUNET_CONTAINER_slist_iter_destroy (it);
1052 GNUNET_CONTAINER_slist_next (it);
1054 GNUNET_CONTAINER_slist_iter_destroy (it);
1062 * @return a new fd set
1064 struct GNUNET_NETWORK_FDSet *
1065 GNUNET_NETWORK_fdset_create ()
1067 struct GNUNET_NETWORK_FDSet *fds;
1069 fds = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_FDSet));
1071 fds->handles = GNUNET_CONTAINER_slist_create ();
1073 GNUNET_NETWORK_fdset_zero (fds);
1079 * Releases the associated memory of an fd set
1083 GNUNET_NETWORK_fdset_destroy (struct GNUNET_NETWORK_FDSet *fds)
1086 GNUNET_CONTAINER_slist_destroy (fds->handles);
1092 * Check if sockets meet certain conditions
1093 * @param rfds set of sockets to be checked for readability
1094 * @param wfds set of sockets to be checked for writability
1095 * @param efds set of sockets to be checked for exceptions
1096 * @param timeout relative value when to return
1097 * @return number of selected sockets, GNUNET_SYSERR on error
1100 GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
1101 struct GNUNET_NETWORK_FDSet *wfds,
1102 struct GNUNET_NETWORK_FDSet *efds,
1103 const struct GNUNET_TIME_Relative timeout)
1110 int read_handles = 0;
1111 int write_handles = 0;
1117 int nsock = 0, nhandles = 0, nSockEvents = 0;
1119 static HANDLE hEventRead = 0;
1120 static HANDLE hEventWrite = 0;
1121 static HANDLE hEventException = 0;
1122 static HANDLE hEventPipeWrite = 0;
1123 static HANDLE hEventReadReady = 0;
1126 int writePipePos = 0;
1128 HANDLE handle_array[FD_SETSIZE + 2];
1129 int returncode = -1;
1130 DWORD newretcode = 0;
1131 int returnedpos = 0;
1133 struct GNUNET_CONTAINER_SList *handles_read, *handles_write, *handles_except;
1135 fd_set aread, awrite, aexcept;
1138 fd_set bread, bwrite, bexcept;
1141 /* TODO: Make this growable */
1142 struct GNUNET_DISK_FileHandle *readArray[50];
1150 handles += read_handles = GNUNET_CONTAINER_slist_count (rfds->handles);
1153 struct GNUNET_CONTAINER_SList_Iterator *t;
1155 for (t = GNUNET_CONTAINER_slist_begin (rfds->handles);
1156 GNUNET_CONTAINER_slist_end (t) != GNUNET_YES;
1157 GNUNET_CONTAINER_slist_next (t))
1159 struct GNUNET_DISK_FileHandle *fh;
1161 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (t,
1163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "FD 0x%x (0x%x) is SET in rfds\n",
1172 nfds = GNUNET_MAX (nfds, wfds->nsds);
1174 handles += write_handles = GNUNET_CONTAINER_slist_count (wfds->handles);
1179 nfds = GNUNET_MAX (nfds, efds->nsds);
1181 handles += ex_handles = GNUNET_CONTAINER_slist_count (efds->handles);
1186 (timeout.rel_value == GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
1192 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1194 ("Fatal internal logic error, process hangs in `%s' (abort with CTRL-C)!\n"),
1199 tv.tv_sec = timeout.rel_value / GNUNET_TIME_UNIT_SECONDS.rel_value;
1201 1000 * (timeout.rel_value -
1202 (tv.tv_sec * GNUNET_TIME_UNIT_SECONDS.rel_value));
1203 return select (nfds, (rfds != NULL) ? &rfds->sds : NULL,
1204 (wfds != NULL) ? &wfds->sds : NULL,
1205 (efds != NULL) ? &efds->sds : NULL,
1206 (timeout.rel_value ==
1207 GNUNET_TIME_UNIT_FOREVER_REL.rel_value) ? NULL : &tv);
1210 #define SAFE_FD_ISSET(fd, set) (set != NULL && FD_ISSET(fd, set))
1211 /* calculate how long we need to wait in milliseconds */
1212 if (timeout.rel_value == GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
1213 ms_total = INFINITE;
1215 ms_total = timeout.rel_value / GNUNET_TIME_UNIT_MILLISECONDS.rel_value;
1216 /* select() may be used as a portable way to sleep */
1217 if (!(rfds || wfds || efds))
1223 /* Events for sockets */
1225 hEventRead = CreateEvent (NULL, TRUE, FALSE, NULL);
1227 ResetEvent (hEventRead);
1228 if (!hEventReadReady)
1229 hEventReadReady = CreateEvent (NULL, TRUE, TRUE, NULL);
1231 hEventWrite = CreateEvent (NULL, TRUE, FALSE, NULL);
1233 ResetEvent (hEventWrite);
1234 if (!hEventException)
1235 hEventException = CreateEvent (NULL, TRUE, FALSE, NULL);
1237 ResetEvent (hEventException);
1239 /* Event for pipes */
1240 if (!hEventPipeWrite)
1241 hEventPipeWrite = CreateEvent (NULL, TRUE, TRUE, NULL);
1245 handles_read = GNUNET_CONTAINER_slist_create ();
1246 handles_write = GNUNET_CONTAINER_slist_create ();
1247 handles_except = GNUNET_CONTAINER_slist_create ();
1258 FD_COPY (&rfds->sds, &aread);
1260 FD_COPY (&rfds->sds, &bread);
1265 FD_COPY (&wfds->sds, &awrite);
1267 FD_COPY (&wfds->sds, &bwrite);
1272 FD_COPY (&efds->sds, &aexcept);
1274 FD_COPY (&efds->sds, &bexcept);
1277 /* We will first Add the PIPES to the events */
1279 if (rfds && read_handles)
1281 struct GNUNET_CONTAINER_SList_Iterator *i;
1283 for (i = GNUNET_CONTAINER_slist_begin (rfds->handles);
1284 GNUNET_CONTAINER_slist_end (i) != GNUNET_YES;
1285 GNUNET_CONTAINER_slist_next (i))
1287 struct GNUNET_DISK_FileHandle *fh;
1289 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (i,
1291 if (fh->type == GNUNET_PIPE)
1293 /* Read zero bytes to check the status of the pipe */
1295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1296 "Reading 0 bytes from the pipe 0x%x\n", fh->h);
1298 if (!ReadFile (fh->h, NULL, 0, NULL, fh->oOverlapRead))
1300 DWORD error_code = GetLastError ();
1302 if (error_code == ERROR_IO_PENDING)
1305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1306 "Adding the pipe's 0x%x overlapped event to the array as %d\n",
1309 handle_array[nhandles++] = fh->oOverlapRead->hEvent;
1310 readArray[readPipes++] = fh;
1315 * SetErrnoFromWinError (error_code);
1322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1323 "Adding the read ready event to the array as %d\n",
1326 handle_array[nhandles++] = hEventReadReady;
1327 readArray[readPipes++] = fh;
1332 GNUNET_CONTAINER_slist_add (handles_read,
1333 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1334 fh, sizeof (struct GNUNET_DISK_FileHandle));
1337 GNUNET_CONTAINER_slist_iter_destroy (i);
1339 if (wfds && write_handles)
1342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1343 "Adding the write ready event to the array as %d\n", nhandles);
1345 handle_array[nhandles++] = hEventPipeWrite;
1346 writePipePos = nhandles;
1348 if (efds && ex_handles)
1350 struct GNUNET_CONTAINER_SList_Iterator *i;
1352 for (i = GNUNET_CONTAINER_slist_begin (efds->handles);
1353 GNUNET_CONTAINER_slist_end (i) != GNUNET_YES;
1354 GNUNET_CONTAINER_slist_next (i))
1356 struct GNUNET_DISK_FileHandle *fh;
1359 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (i,
1361 if (fh->type == GNUNET_PIPE)
1363 if (!PeekNamedPipe (fh->h, NULL, 0, NULL, &dwBytes, NULL))
1365 GNUNET_CONTAINER_slist_add (handles_except,
1366 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1368 sizeof (struct GNUNET_DISK_FileHandle));
1373 GNUNET_CONTAINER_slist_iter_destroy (i);
1380 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1381 "Adding the socket read event to the array as %d\n",
1384 handle_array[nhandles++] = hEventRead;
1386 for (i = 0; i < rfds->sds.fd_count; i++)
1388 WSAEventSelect (rfds->sds.fd_array[i], hEventRead,
1389 FD_ACCEPT | FD_READ | FD_CLOSE);
1398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1399 "Adding the socket write event to the array as %d\n",
1402 handle_array[nhandles++] = hEventWrite;
1404 for (i = 0; i < wfds->sds.fd_count; i++)
1409 status = send (wfds->sds.fd_array[i], NULL, 0, 0);
1410 error = GetLastError ();
1412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1413 "pre-send to the socket %d returned %d (%u)\n", i, status,
1416 if (status == 0 || (error != WSAEWOULDBLOCK && error != WSAENOTCONN))
1418 WSAEventSelect (wfds->sds.fd_array[i], hEventWrite,
1419 FD_WRITE | FD_CONNECT | FD_CLOSE);
1423 SetEvent (hEventWrite);
1428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1429 "Adding the socket error event to the array as %d\n",
1432 handle_array[nhandles++] = hEventException;
1434 for (i = 0; i < efds->sds.fd_count; i++)
1436 WSAEventSelect (efds->sds.fd_array[i], hEventException,
1443 handle_array[nhandles] = NULL;
1446 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Number nfds : %d\n", nfds);
1447 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Number of handles : %d\n", nhandles);
1448 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "retcode : %d\n", newretcode);
1449 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Will wait : %d\n", ms_total);
1454 WaitForMultipleObjects (nhandles, handle_array, FALSE, ms_total);
1456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "WaitForMultipleObjects Returned : %d\n",
1460 returnedpos = returncode - WAIT_OBJECT_0;
1462 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return pos is : %d\n", returnedpos);
1465 /* FIXME: THIS LINE IS WRONG !! We should add to handles only handles that fired the events, not all ! */
1468 * GNUNET_CONTAINER_slist_append (handles_read, rfds->handles);
1470 if (nhandles && (returnedpos < nhandles))
1477 struct timeval tvslice;
1480 tvslice.tv_usec = 10;
1481 retcode = select (nfds, &aread, &awrite, &aexcept, &tvslice);
1485 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Select retcode : %d\n", retcode);
1488 /* FIXME: <= writePipePos? Really? */
1489 if ((writePipePos != -1) && (returnedpos <= writePipePos))
1491 GNUNET_CONTAINER_slist_append (handles_write, wfds->handles);
1492 retcode += write_handles;
1494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Added write pipe\n");
1498 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ReadPipes is : %d\n", readPipes);
1500 /* We have some pipes ready for read. */
1501 /* FIXME: it is supposed to work !! Only choose the Pipes who fired the event, but it is not working */
1503 if (returnedpos < readPipes)
1506 * for (i = 0; i < readPipes; i++)
1508 * waitstatus = WaitForSingleObject (handle_array[i], 0);
1509 * GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Read pipe %d wait status is : %d\n", i, waitstatus);
1510 * if (waitstatus != WAIT_OBJECT_0)
1512 * GNUNET_CONTAINER_slist_add (handles_read,
1513 * GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1514 * readArray[i], sizeof (struct GNUNET_DISK_FileHandle));
1516 * GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Added read Pipe\n");
1519 for (i = 0; i < readPipes; i++)
1527 PeekNamedPipe (readArray[i]->h, NULL, 0, NULL, &waitstatus, NULL);
1528 error = GetLastError ();
1530 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1531 "Peek at read pipe %d (0x%x) returned %d (%d bytes available) GLE %u\n",
1532 i, readArray[i]->h, bret, waitstatus, error);
1534 if (bret == 0 || waitstatus <= 0)
1536 GNUNET_CONTAINER_slist_add (handles_read,
1537 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1539 sizeof (struct GNUNET_DISK_FileHandle));
1542 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Added read Pipe 0x%x (0x%x)\n",
1543 readArray[i], readArray[i]->h);
1547 waitstatus = WaitForSingleObject (hEventWrite, 0);
1549 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1550 "Wait for the write event returned %d\n", waitstatus);
1552 if (waitstatus == WAIT_OBJECT_0)
1554 for (i = 0; i < wfds->sds.fd_count; i++)
1559 int sizeof_so_error = sizeof (so_error);
1561 getsockopt (wfds->sds.fd_array[i], SOL_SOCKET, SO_ERROR,
1562 (char *) &so_error, &sizeof_so_error);
1564 status = send (wfds->sds.fd_array[i], NULL, 0, 0);
1565 error = GetLastError ();
1567 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1568 "send to the socket %d returned %d (%u)\n", i, status,
1571 if (status == 0 || (error != WSAEWOULDBLOCK && error != WSAENOTCONN) ||
1572 (status == -1 && gso_result == 0 && error == WSAENOTCONN &&
1573 so_error == WSAECONNREFUSED))
1575 FD_SET (wfds->sds.fd_array[i], &awrite);
1582 if (!nhandles || (returnedpos >= nhandles))
1583 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1584 "Returning from _select() with nothing!\n");
1588 struct GNUNET_CONTAINER_SList_Iterator *t;
1590 for (i = 0; i < rfds->sds.fd_count; i++)
1592 WSAEventSelect (rfds->sds.fd_array[i], hEventRead, 0);
1595 for (t = GNUNET_CONTAINER_slist_begin (rfds->handles);
1596 GNUNET_CONTAINER_slist_end (t) != GNUNET_YES;
1597 GNUNET_CONTAINER_slist_next (t))
1599 struct GNUNET_DISK_FileHandle *fh;
1601 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (t,
1603 if (fh->type == GNUNET_PIPE)
1608 GNUNET_CONTAINER_slist_iter_destroy (t);
1610 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Zeroing rfds\n");
1612 GNUNET_NETWORK_fdset_zero (rfds);
1613 if (retcode != -1 && nhandles && (returnedpos < nhandles))
1614 GNUNET_NETWORK_fdset_copy_native (rfds, &aread, retcode);
1615 GNUNET_CONTAINER_slist_append (rfds->handles, handles_read);
1619 for (i = 0; i < wfds->sds.fd_count; i++)
1621 WSAEventSelect (wfds->sds.fd_array[i], hEventWrite, 0);
1625 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Zeroing wfds\n");
1627 GNUNET_NETWORK_fdset_zero (wfds);
1628 if (retcode != -1 && nhandles && (returnedpos < nhandles))
1629 GNUNET_NETWORK_fdset_copy_native (wfds, &awrite, retcode);
1630 GNUNET_CONTAINER_slist_append (wfds->handles, handles_write);
1634 for (i = 0; i < efds->sds.fd_count; i++)
1636 WSAEventSelect (efds->sds.fd_array[i], hEventException, 0);
1640 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Zeroing efds\n");
1642 GNUNET_NETWORK_fdset_zero (efds);
1643 if (retcode != -1 && nhandles && (returnedpos < nhandles))
1644 GNUNET_NETWORK_fdset_copy_native (efds, &aexcept, retcode);
1645 GNUNET_CONTAINER_slist_append (efds->handles, handles_except);
1647 GNUNET_CONTAINER_slist_destroy (handles_read);
1648 GNUNET_CONTAINER_slist_destroy (handles_write);
1649 GNUNET_CONTAINER_slist_destroy (handles_except);
1653 struct GNUNET_CONTAINER_SList_Iterator *t;
1655 for (i = 0; i < bread.fd_count; i++)
1657 if (bread.fd_array[i] != 0)
1658 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "FD 0x%x is %s in rfds\n",
1660 (SAFE_FD_ISSET (bread.fd_array[i], rfds)) ? "SET" :
1663 for (t = GNUNET_CONTAINER_slist_begin (rfds->handles);
1664 GNUNET_CONTAINER_slist_end (t) != GNUNET_YES;
1665 GNUNET_CONTAINER_slist_next (t))
1667 struct GNUNET_DISK_FileHandle *fh;
1669 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (t,
1671 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "FD 0x%x is SET in rfds\n", fh->h);
1676 for (i = 0; i < bwrite.fd_count; i++)
1678 if (bwrite.fd_array[i] != 0)
1679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "FD 0x%x is %s in wfds\n",
1681 (SAFE_FD_ISSET (bwrite.fd_array[i], rfds)) ? "SET" :
1687 for (i = 0; i < bexcept.fd_count; i++)
1689 if (bexcept.fd_array[i] != 0)
1690 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "FD 0x%x is %s in efds\n",
1691 bexcept.fd_array[i],
1692 (SAFE_FD_ISSET (bexcept.fd_array[i], rfds)) ? "SET" :
1696 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Returning %d or 0\n", retcode);
1698 if (nhandles && (returnedpos < nhandles))
1705 /* end of network.c */