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 LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
33 #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
34 #define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall)
36 #define DEBUG_NETWORK GNUNET_EXTRA_LOGGING
38 #define DEBUG_W32_CYCLES GNUNET_EXTRA_LOGGING
40 #ifndef INVALID_SOCKET
41 #define INVALID_SOCKET -1
45 struct GNUNET_NETWORK_Handle
55 * Address family / domain.
60 * Number of bytes in addr.
65 * Address we were bound to, or NULL.
67 struct sockaddr *addr;
73 #define FD_COPY(s, d) (memcpy ((d), (s), sizeof (fd_set)))
78 * Set if a socket should use blocking or non-blocking IO.
80 * @param doBlock blocking mode
81 * @return GNUNET_OK on success, GNUNET_SYSERR on error
84 socket_set_blocking (struct GNUNET_NETWORK_Handle *fd, int doBlock)
91 if (ioctlsocket (fd->fd, FIONBIO, &mode) == SOCKET_ERROR)
94 SetErrnoFromWinsockError (WSAGetLastError ());
95 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "ioctlsocket");
102 int flags = fcntl (fd->fd, F_GETFL);
107 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "fcntl");
108 return GNUNET_SYSERR;
111 flags &= ~O_NONBLOCK;
115 if (0 != fcntl (fd->fd, F_SETFL, flags))
118 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "fcntl");
119 return GNUNET_SYSERR;
128 * Make a socket non-inheritable to child processes
130 * @param h the socket to make non-inheritable
131 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
132 * @warning Not implemented on Windows
135 socket_set_inheritable (const struct GNUNET_NETWORK_Handle *h)
139 i = fcntl (h->fd, F_GETFD);
141 return GNUNET_SYSERR;
142 if (i == (i | FD_CLOEXEC))
145 if (fcntl (h->fd, F_SETFD, i) < 0)
146 return GNUNET_SYSERR;
154 * The MSG_NOSIGNAL equivalent on Mac OS X
156 * @param h the socket to make non-delaying
159 socket_set_nosigpipe (const struct GNUNET_NETWORK_Handle *h)
164 setsockopt (h->fd, SOL_SOCKET, SO_NOSIGPIPE, &abs_value,
166 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
172 * Disable delays when sending data via the socket.
173 * (GNUnet makes sure that messages are as big as
176 * @param h the socket to make non-delaying
179 socket_set_nodelay (const struct GNUNET_NETWORK_Handle *h)
184 if (0 != setsockopt (h->fd, IPPROTO_TCP, TCP_NODELAY, &value, sizeof (value)))
185 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
187 const char *abs_value = "1";
190 setsockopt (h->fd, IPPROTO_TCP, TCP_NODELAY, abs_value,
192 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
198 * accept a new connection on a socket
200 * @param desc bound socket
201 * @param address address of the connecting peer, may be NULL
202 * @param address_len length of address
203 * @return client socket
205 struct GNUNET_NETWORK_Handle *
206 GNUNET_NETWORK_socket_accept (const struct GNUNET_NETWORK_Handle *desc,
207 struct sockaddr *address, socklen_t * address_len)
209 struct GNUNET_NETWORK_Handle *ret;
211 ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle));
214 struct sockaddr name;
215 socklen_t namelen = sizeof (name);
216 int gsn = getsockname (desc->fd, &name, &namelen);
219 LOG (GNUNET_ERROR_TYPE_DEBUG, "Accepting connection on `%s'\n",
220 GNUNET_a2s (&name, namelen));
223 ret->fd = accept (desc->fd, address, address_len);
225 ret->af = address->sa_family;
228 if (ret->fd == INVALID_SOCKET)
231 SetErrnoFromWinsockError (WSAGetLastError ());
237 if (ret->fd >= FD_SETSIZE)
239 GNUNET_break (0 == close (ret->fd));
245 if (GNUNET_SYSERR == socket_set_blocking (ret, GNUNET_NO))
249 /* we might want to treat this one as fatal... */
251 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret));
256 if (GNUNET_OK != socket_set_inheritable (ret))
257 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
258 "socket_set_inheritable");
261 socket_set_nosigpipe (ret);
264 if (ret->af != AF_UNIX)
266 socket_set_nodelay (ret);
272 * Bind to a connected socket
274 * @param address address to be bound
275 * @param address_len length of address
276 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
279 GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc,
280 const struct sockaddr *address,
281 socklen_t address_len)
289 if (desc->af == AF_INET6)
290 if (0 != setsockopt (desc->fd, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof (on)))
291 LOG_STRERROR (GNUNET_ERROR_TYPE_DEBUG, "setsockopt");
295 /* This is required, and required here, but only on UNIX */
296 if (0 != setsockopt (desc->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)))
297 LOG_STRERROR (GNUNET_ERROR_TYPE_DEBUG, "setsockopt");
301 if (address->sa_family == AF_UNIX)
303 const struct sockaddr_un *un = (const struct sockaddr_un *) address;
305 (void) unlink (un->sun_path);
309 ret = bind (desc->fd, address, address_len);
311 if (SOCKET_ERROR == ret)
312 SetErrnoFromWinsockError (WSAGetLastError ());
315 return GNUNET_SYSERR;
318 desc->addr = GNUNET_malloc (address_len);
319 memcpy (desc->addr, address, address_len);
320 desc->addrlen = address_len;
330 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
333 GNUNET_NETWORK_socket_close (struct GNUNET_NETWORK_Handle *desc)
341 LOG (GNUNET_ERROR_TYPE_DEBUG, "Closing 0x%x\n",
345 ret = closesocket (desc->fd);
346 error = WSAGetLastError ();
347 SetErrnoFromWinsockError (error);
349 LOG (GNUNET_ERROR_TYPE_DEBUG,
350 "Closed 0x%x, closesocket() returned %d, GLE is %u\n", desc->fd, ret,
354 ret = close (desc->fd);
358 if ((desc->af == AF_UNIX) && (NULL != desc->addr))
360 const struct sockaddr_un *un = (const struct sockaddr_un *) desc->addr;
362 if (0 != unlink (un->sun_path))
363 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "unlink", un->sun_path);
367 GNUNET_free_non_null (desc->addr);
369 return (ret == 0) ? GNUNET_OK : GNUNET_SYSERR;
374 * Box a native socket (and check that it is a socket).
376 * @param fd socket to box
377 * @return NULL on error (including not supported on target platform)
379 struct GNUNET_NETWORK_Handle *
380 GNUNET_NETWORK_socket_box_native (int fd)
385 struct GNUNET_NETWORK_Handle *ret;
387 if (fcntl (fd, F_GETFD) < 0)
388 return NULL; /* invalid FD */
389 ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle));
400 * @param address peer address
401 * @param address_len length of address
402 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
405 GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Handle *desc,
406 const struct sockaddr *address,
407 socklen_t address_len)
411 ret = connect (desc->fd, address, address_len);
414 if (SOCKET_ERROR == ret)
416 SetErrnoFromWinsockError (WSAGetLastError ());
417 if (errno == EWOULDBLOCK)
421 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
429 * @param level protocol level of the option
430 * @param optname identifier of the option
431 * @param optval options
432 * @param optlen length of optval
433 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
436 GNUNET_NETWORK_socket_getsockopt (const struct GNUNET_NETWORK_Handle *desc,
437 int level, int optname, void *optval,
442 ret = getsockopt (desc->fd, level, optname, optval, optlen);
445 if (ret == 0 && level == SOL_SOCKET && optname == SO_ERROR)
446 *((int *) optval) = GetErrnoFromWinsockError (*((int *) optval));
448 else if (SOCKET_ERROR == ret)
449 SetErrnoFromWinsockError (WSAGetLastError ());
451 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
458 * @param backlog length of the listen queue
459 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
462 GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Handle *desc,
467 ret = listen (desc->fd, backlog);
470 if (SOCKET_ERROR == ret)
471 SetErrnoFromWinsockError (WSAGetLastError ());
474 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
479 * How much data is available to be read on this descriptor?
481 * Returns GNUNET_NO if no data is available, or on error!
485 GNUNET_NETWORK_socket_recvfrom_amount (const struct GNUNET_NETWORK_Handle *
490 /* How much is there to be read? */
494 error = ioctl (desc->fd, FIONREAD, &pending);
499 error = ioctlsocket (desc->fd, FIONREAD, &pending);
500 if (error != SOCKET_ERROR)
509 * Read data from a connected socket (always non-blocking).
511 * @param buffer buffer
512 * @param length length of buffer
513 * @param src_addr either the source to recv from, or all zeroes
514 * to be filled in by recvfrom
515 * @param addrlen length of the addr
518 GNUNET_NETWORK_socket_recvfrom (const struct GNUNET_NETWORK_Handle * desc,
519 void *buffer, size_t length,
520 struct sockaddr * src_addr, socklen_t * addrlen)
528 flags |= MSG_DONTWAIT;
531 ret = recvfrom (desc->fd, buffer, length, flags, src_addr, addrlen);
533 if (SOCKET_ERROR == ret)
534 SetErrnoFromWinsockError (WSAGetLastError ());
541 * Read data from a connected socket (always non-blocking).
543 * @param buffer buffer
544 * @param length length of buffer
547 GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Handle * desc,
548 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)
585 flags |= MSG_DONTWAIT;
589 flags |= MSG_NOSIGNAL;
592 ret = send (desc->fd, buffer, length, flags);
595 if (SOCKET_ERROR == ret)
596 SetErrnoFromWinsockError (WSAGetLastError ());
604 * Send data to a particular destination (always non-blocking).
605 * This function only works for UDP sockets.
608 * @param message data to send
609 * @param length size of the data
610 * @param dest_addr destination address
611 * @param dest_len length of address
612 * @return number of bytes sent, GNUNET_SYSERR on error
615 GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Handle * desc,
616 const void *message, size_t length,
617 const struct sockaddr * dest_addr,
626 flags |= MSG_DONTWAIT;
629 flags |= MSG_NOSIGNAL;
631 ret = sendto (desc->fd, message, length, flags, dest_addr, dest_len);
633 if (SOCKET_ERROR == ret)
634 SetErrnoFromWinsockError (WSAGetLastError ());
643 * @param level protocol level of the option
644 * @param option_name option identifier
645 * @param option_value value to set
646 * @param option_len size of option_value
647 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
650 GNUNET_NETWORK_socket_setsockopt (struct GNUNET_NETWORK_Handle *fd, int level,
651 int option_name, const void *option_value,
652 socklen_t option_len)
656 ret = setsockopt (fd->fd, level, option_name, option_value, option_len);
658 if (SOCKET_ERROR == ret)
659 SetErrnoFromWinsockError (WSAGetLastError ());
661 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
666 * Create a new socket. Configure it for non-blocking IO and
667 * mark it as non-inheritable to child processes (set the
668 * close-on-exec flag).
670 * @param domain domain of the socket
671 * @param type socket type
672 * @param protocol network protocol
673 * @return new socket, NULL on error
675 struct GNUNET_NETWORK_Handle *
676 GNUNET_NETWORK_socket_create (int domain, int type, int protocol)
678 struct GNUNET_NETWORK_Handle *ret;
680 ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle));
682 ret->fd = socket (domain, type, protocol);
683 if (INVALID_SOCKET == ret->fd)
686 SetErrnoFromWinsockError (WSAGetLastError ());
693 if (ret->fd >= FD_SETSIZE)
695 GNUNET_break (0 == close (ret->fd));
702 if (GNUNET_SYSERR == socket_set_blocking (ret, GNUNET_NO))
704 /* we might want to treat this one as fatal... */
706 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret));
711 if (GNUNET_OK != socket_set_inheritable (ret))
712 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
713 "socket_set_inheritable");
716 socket_set_nosigpipe (ret);
718 if ((type == SOCK_STREAM)
720 && (domain != AF_UNIX)
723 socket_set_nodelay (ret);
729 * Shut down socket operations
731 * @param how type of shutdown
732 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
735 GNUNET_NETWORK_socket_shutdown (struct GNUNET_NETWORK_Handle *desc, int how)
739 ret = shutdown (desc->fd, how);
742 SetErrnoFromWinsockError (WSAGetLastError ());
744 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
749 * Disable the "CORK" feature for communication with the given socket,
750 * forcing the OS to immediately flush the buffer on transmission
751 * instead of potentially buffering multiple messages. Essentially
752 * reduces the OS send buffers to zero.
755 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
758 GNUNET_NETWORK_socket_disable_corking (struct GNUNET_NETWORK_Handle *desc)
767 setsockopt (desc->fd, SOL_SOCKET, SO_SNDBUF, (char *) &value,
769 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
772 setsockopt (desc->fd, SOL_SOCKET, SO_RCVBUF, (char *) &value,
774 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
780 setsockopt (desc->fd, SOL_SOCKET, SO_SNDBUF, &value, sizeof (value))))
781 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
784 setsockopt (desc->fd, SOL_SOCKET, SO_RCVBUF, &value, sizeof (value))))
785 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
787 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
796 GNUNET_NETWORK_fdset_zero (struct GNUNET_NETWORK_FDSet *fds)
801 GNUNET_CONTAINER_slist_clear (fds->handles);
806 * Add a socket to the FD set
808 * @param desc socket to add
811 GNUNET_NETWORK_fdset_set (struct GNUNET_NETWORK_FDSet *fds,
812 const struct GNUNET_NETWORK_Handle *desc)
814 FD_SET (desc->fd, &fds->sds);
815 if (desc->fd + 1 > fds->nsds)
816 fds->nsds = desc->fd + 1;
821 * Check whether a socket is part of the fd set
824 * @return 0 if the FD is not set
827 GNUNET_NETWORK_fdset_isset (const struct GNUNET_NETWORK_FDSet *fds,
828 const struct GNUNET_NETWORK_Handle *desc)
830 return FD_ISSET (desc->fd, &fds->sds);
835 * Add one fd set to another
836 * @param dst the fd set to add to
837 * @param src the fd set to add from
840 GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst,
841 const struct GNUNET_NETWORK_FDSet *src)
845 for (nfds = src->nsds; nfds > 0; nfds--)
846 if (FD_ISSET (nfds, &src->sds))
849 FD_SET (nfds, &dst->sds);
850 if (nfds + 1 > dst->nsds)
851 dst->nsds = nfds + 1;
854 GNUNET_CONTAINER_slist_append (dst->handles, src->handles);
860 * Copy one fd set to another
862 * @param to destination
866 GNUNET_NETWORK_fdset_copy (struct GNUNET_NETWORK_FDSet *to,
867 const struct GNUNET_NETWORK_FDSet *from)
869 FD_COPY (&from->sds, &to->sds);
870 to->nsds = from->nsds;
873 GNUNET_CONTAINER_slist_clear (to->handles);
874 GNUNET_CONTAINER_slist_append (to->handles, from->handles);
880 * Return file descriptor for this network handle
882 * @param desc wrapper to process
883 * @return POSIX file descriptor
886 GNUNET_NETWORK_get_fd (struct GNUNET_NETWORK_Handle *desc)
893 * Copy a native fd set
895 * @param to destination
896 * @param from native source set
897 * @param nfds the biggest socket number in from + 1
900 GNUNET_NETWORK_fdset_copy_native (struct GNUNET_NETWORK_FDSet *to,
901 const fd_set * from, int nfds)
903 FD_COPY (from, &to->sds);
909 * Set a native fd in a set
911 * @param to destination
912 * @param nfd native FD to set
915 GNUNET_NETWORK_fdset_set_native (struct GNUNET_NETWORK_FDSet *to, int nfd)
917 GNUNET_assert ((nfd >= 0) && (nfd < FD_SETSIZE));
918 FD_SET (nfd, &to->sds);
919 to->nsds = GNUNET_MAX (nfd + 1, to->nsds);
924 * Test native fd in a set
926 * @param to set to test, NULL for empty set
927 * @param nfd native FD to test, or -1 for none
928 * @return GNUNET_YES if FD is set in the set
931 GNUNET_NETWORK_fdset_test_native (const struct GNUNET_NETWORK_FDSet *to,
934 if ((nfd == -1) || (to == NULL))
936 return FD_ISSET (nfd, &to->sds) ? GNUNET_YES : GNUNET_NO;
941 * Add a file handle to the fd set
943 * @param h the file handle to add
946 GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds,
947 const struct GNUNET_DISK_FileHandle *h)
950 GNUNET_CONTAINER_slist_add (fds->handles,
951 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT, h,
952 sizeof (struct GNUNET_DISK_FileHandle));
957 GNUNET_DISK_internal_file_handle_ (h, &fd, sizeof (int));
958 FD_SET (fd, &fds->sds);
959 if (fd + 1 > fds->nsds)
967 * Check if a file handle is part of an fd set
969 * @param h file handle
970 * @return GNUNET_YES if the file handle is part of the set
973 GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds,
974 const struct GNUNET_DISK_FileHandle *h)
978 return GNUNET_CONTAINER_slist_contains (fds->handles, h,
980 GNUNET_DISK_FileHandle));
982 return FD_ISSET (h->fd, &fds->sds);
988 * Checks if two fd sets overlap
989 * @param fds1 first fd set
990 * @param fds2 second fd set
991 * @return GNUNET_YES if they do overlap, GNUNET_NO otherwise
994 GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1,
995 const struct GNUNET_NETWORK_FDSet *fds2)
1001 if (nfds > fds2->nsds)
1006 if (FD_ISSET (nfds, &fds1->sds) && FD_ISSET (nfds, &fds2->sds))
1010 struct GNUNET_CONTAINER_SList_Iterator it;
1011 struct GNUNET_DISK_FileHandle *h;
1015 /*This code is somewhat hacky, we are not supposed to know what's
1016 * inside of fd_set; also the O(n^2) is really bad... */
1018 for (i = 0; i < fds1->sds.fd_count; i++)
1020 for (j = 0; j < fds2->sds.fd_count; j++)
1022 if (fds1->sds.fd_array[i] == fds2->sds.fd_array[j])
1026 it = GNUNET_CONTAINER_slist_begin (fds1->handles);
1027 while (GNUNET_CONTAINER_slist_end (&it) != GNUNET_YES)
1030 struct GNUNET_CONTAINER_SList_Iterator t;
1032 h = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (&it,
1035 LOG (GNUNET_ERROR_TYPE_DEBUG, "Checking that FD 0x%x is in another set:\n",
1037 for (t = GNUNET_CONTAINER_slist_begin (fds2->handles);
1038 GNUNET_CONTAINER_slist_end (&t) != GNUNET_YES;
1039 GNUNET_CONTAINER_slist_next (&t))
1041 struct GNUNET_DISK_FileHandle *fh;
1043 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (&t,
1045 LOG (GNUNET_ERROR_TYPE_DEBUG, "0x%x\n", fh->h);
1048 if (GNUNET_CONTAINER_slist_contains
1049 (fds2->handles, h, sizeof (struct GNUNET_DISK_FileHandle)))
1052 LOG (GNUNET_ERROR_TYPE_DEBUG, "Match!\n");
1056 GNUNET_CONTAINER_slist_next (&it);
1065 * @return a new fd set
1067 struct GNUNET_NETWORK_FDSet *
1068 GNUNET_NETWORK_fdset_create ()
1070 struct GNUNET_NETWORK_FDSet *fds;
1072 fds = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_FDSet));
1074 fds->handles = GNUNET_CONTAINER_slist_create ();
1076 GNUNET_NETWORK_fdset_zero (fds);
1082 * Releases the associated memory of an fd set
1086 GNUNET_NETWORK_fdset_destroy (struct GNUNET_NETWORK_FDSet *fds)
1089 GNUNET_CONTAINER_slist_destroy (fds->handles);
1095 * Check if sockets meet certain conditions
1096 * @param rfds set of sockets to be checked for readability
1097 * @param wfds set of sockets to be checked for writability
1098 * @param efds set of sockets to be checked for exceptions
1099 * @param timeout relative value when to return
1100 * @return number of selected sockets, GNUNET_SYSERR on error
1103 GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
1104 struct GNUNET_NETWORK_FDSet *wfds,
1105 struct GNUNET_NETWORK_FDSet *efds,
1106 const struct GNUNET_TIME_Relative timeout)
1113 int read_handles = 0;
1114 int write_handles = 0;
1120 int nsock = 0, nhandles = 0, nSockEvents = 0;
1122 static HANDLE hEventRead = 0;
1123 static HANDLE hEventWrite = 0;
1124 static HANDLE hEventException = 0;
1125 static HANDLE hEventPipeWrite = 0;
1126 static HANDLE hEventReadReady = 0;
1129 int writePipePos = 0;
1131 HANDLE handle_array[FD_SETSIZE + 2];
1132 int returncode = -1;
1133 DWORD newretcode = 0;
1134 int returnedpos = 0;
1136 struct GNUNET_CONTAINER_SList *handles_read, *handles_write, *handles_except;
1138 fd_set aread, awrite, aexcept;
1141 fd_set bread, bwrite, bexcept;
1144 /* TODO: Make this growable */
1145 struct GNUNET_DISK_FileHandle *readArray[50];
1153 handles += read_handles = GNUNET_CONTAINER_slist_count (rfds->handles);
1156 struct GNUNET_CONTAINER_SList_Iterator t;
1158 for (t = GNUNET_CONTAINER_slist_begin (rfds->handles);
1159 GNUNET_CONTAINER_slist_end (&t) != GNUNET_YES;
1160 GNUNET_CONTAINER_slist_next (&t))
1162 struct GNUNET_DISK_FileHandle *fh;
1164 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (&t,
1166 LOG (GNUNET_ERROR_TYPE_DEBUG, "FD 0x%x (0x%x) is SET in rfds\n", fh->h,
1175 nfds = GNUNET_MAX (nfds, wfds->nsds);
1177 handles += write_handles = GNUNET_CONTAINER_slist_count (wfds->handles);
1182 nfds = GNUNET_MAX (nfds, efds->nsds);
1184 handles += ex_handles = GNUNET_CONTAINER_slist_count (efds->handles);
1189 (timeout.rel_value == GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
1195 LOG (GNUNET_ERROR_TYPE_ERROR,
1197 ("Fatal internal logic error, process hangs in `%s' (abort with CTRL-C)!\n"),
1202 tv.tv_sec = timeout.rel_value / GNUNET_TIME_UNIT_SECONDS.rel_value;
1204 1000 * (timeout.rel_value -
1205 (tv.tv_sec * GNUNET_TIME_UNIT_SECONDS.rel_value));
1206 return select (nfds, (rfds != NULL) ? &rfds->sds : NULL,
1207 (wfds != NULL) ? &wfds->sds : NULL,
1208 (efds != NULL) ? &efds->sds : NULL,
1209 (timeout.rel_value ==
1210 GNUNET_TIME_UNIT_FOREVER_REL.rel_value) ? NULL : &tv);
1213 #define SAFE_FD_ISSET(fd, set) (set != NULL && FD_ISSET(fd, set))
1214 /* calculate how long we need to wait in milliseconds */
1215 if (timeout.rel_value == GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
1216 ms_total = INFINITE;
1218 ms_total = timeout.rel_value / GNUNET_TIME_UNIT_MILLISECONDS.rel_value;
1219 /* select() may be used as a portable way to sleep */
1220 if (!(rfds || wfds || efds))
1226 /* Events for sockets */
1228 hEventRead = CreateEvent (NULL, TRUE, FALSE, NULL);
1230 ResetEvent (hEventRead);
1231 if (!hEventReadReady)
1232 hEventReadReady = CreateEvent (NULL, TRUE, TRUE, NULL);
1234 hEventWrite = CreateEvent (NULL, TRUE, FALSE, NULL);
1236 ResetEvent (hEventWrite);
1237 if (!hEventException)
1238 hEventException = CreateEvent (NULL, TRUE, FALSE, NULL);
1240 ResetEvent (hEventException);
1242 /* Event for pipes */
1243 if (!hEventPipeWrite)
1244 hEventPipeWrite = CreateEvent (NULL, TRUE, TRUE, NULL);
1248 handles_read = GNUNET_CONTAINER_slist_create ();
1249 handles_write = GNUNET_CONTAINER_slist_create ();
1250 handles_except = GNUNET_CONTAINER_slist_create ();
1261 FD_COPY (&rfds->sds, &aread);
1263 FD_COPY (&rfds->sds, &bread);
1268 FD_COPY (&wfds->sds, &awrite);
1270 FD_COPY (&wfds->sds, &bwrite);
1275 FD_COPY (&efds->sds, &aexcept);
1277 FD_COPY (&efds->sds, &bexcept);
1280 /* We will first Add the PIPES to the events */
1282 if (rfds && read_handles)
1284 struct GNUNET_CONTAINER_SList_Iterator i;
1286 for (i = GNUNET_CONTAINER_slist_begin (rfds->handles);
1287 GNUNET_CONTAINER_slist_end (&i) != GNUNET_YES;
1288 GNUNET_CONTAINER_slist_next (&i))
1290 struct GNUNET_DISK_FileHandle *fh;
1292 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (&i,
1294 if (fh->type == GNUNET_PIPE)
1296 /* Read zero bytes to check the status of the pipe */
1298 LOG (GNUNET_ERROR_TYPE_DEBUG, "Reading 0 bytes from the pipe 0x%x\n",
1301 if (!ReadFile (fh->h, NULL, 0, NULL, fh->oOverlapRead))
1303 DWORD error_code = GetLastError ();
1305 if (error_code == ERROR_IO_PENDING)
1308 LOG (GNUNET_ERROR_TYPE_DEBUG,
1309 "Adding the pipe's 0x%x overlapped event to the array as %d\n",
1312 handle_array[nhandles++] = fh->oOverlapRead->hEvent;
1313 readArray[readPipes++] = fh;
1318 * SetErrnoFromWinError (error_code);
1325 LOG (GNUNET_ERROR_TYPE_DEBUG,
1326 "Adding the read ready event to the array as %d\n", nhandles);
1328 handle_array[nhandles++] = hEventReadReady;
1329 readArray[readPipes++] = fh;
1334 GNUNET_CONTAINER_slist_add (handles_read,
1335 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1336 fh, sizeof (struct GNUNET_DISK_FileHandle));
1340 if (wfds && write_handles)
1343 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));
1380 LOG (GNUNET_ERROR_TYPE_DEBUG,
1381 "Adding the socket read event to the array as %d\n", nhandles);
1383 handle_array[nhandles++] = hEventRead;
1385 for (i = 0; i < rfds->sds.fd_count; i++)
1387 WSAEventSelect (rfds->sds.fd_array[i], hEventRead,
1388 FD_ACCEPT | FD_READ | FD_CLOSE);
1397 LOG (GNUNET_ERROR_TYPE_DEBUG,
1398 "Adding the socket write event to the array as %d\n", nhandles);
1400 handle_array[nhandles++] = hEventWrite;
1402 for (i = 0; i < wfds->sds.fd_count; i++)
1407 status = send (wfds->sds.fd_array[i], NULL, 0, 0);
1408 error = GetLastError ();
1410 LOG (GNUNET_ERROR_TYPE_DEBUG,
1411 "pre-send to the socket %d returned %d (%u)\n", i, status, error);
1413 if (status == 0 || (error != WSAEWOULDBLOCK && error != WSAENOTCONN))
1415 WSAEventSelect (wfds->sds.fd_array[i], hEventWrite,
1416 FD_WRITE | FD_CONNECT | FD_CLOSE);
1420 SetEvent (hEventWrite);
1425 LOG (GNUNET_ERROR_TYPE_DEBUG,
1426 "Adding the socket error event to the array as %d\n", nhandles);
1428 handle_array[nhandles++] = hEventException;
1430 for (i = 0; i < efds->sds.fd_count; i++)
1432 WSAEventSelect (efds->sds.fd_array[i], hEventException,
1439 handle_array[nhandles] = NULL;
1442 LOG (GNUNET_ERROR_TYPE_DEBUG, "Number nfds : %d\n", nfds);
1443 LOG (GNUNET_ERROR_TYPE_DEBUG, "Number of handles : %d\n", nhandles);
1444 LOG (GNUNET_ERROR_TYPE_DEBUG, "retcode : %d\n", newretcode);
1445 LOG (GNUNET_ERROR_TYPE_DEBUG, "Will wait : %d\n", ms_total);
1450 WaitForMultipleObjects (nhandles, handle_array, FALSE, ms_total);
1452 LOG (GNUNET_ERROR_TYPE_DEBUG, "WaitForMultipleObjects Returned : %d\n",
1456 returnedpos = returncode - WAIT_OBJECT_0;
1458 LOG (GNUNET_ERROR_TYPE_DEBUG, "return pos is : %d\n", returnedpos);
1461 /* FIXME: THIS LINE IS WRONG !! We should add to handles only handles that fired the events, not all ! */
1464 * GNUNET_CONTAINER_slist_append (handles_read, rfds->handles);
1466 if (nhandles && (returnedpos < nhandles))
1473 struct timeval tvslice;
1476 tvslice.tv_usec = 10;
1477 retcode = select (nfds, &aread, &awrite, &aexcept, &tvslice);
1481 LOG (GNUNET_ERROR_TYPE_DEBUG, "Select retcode : %d\n", retcode);
1484 /* FIXME: <= writePipePos? Really? */
1485 if ((writePipePos != -1) && (returnedpos <= writePipePos))
1487 GNUNET_CONTAINER_slist_append (handles_write, wfds->handles);
1488 retcode += write_handles;
1490 LOG (GNUNET_ERROR_TYPE_DEBUG, "Added write pipe\n");
1494 LOG (GNUNET_ERROR_TYPE_DEBUG, "ReadPipes is : %d\n", readPipes);
1496 /* We have some pipes ready for read. */
1497 /* FIXME: it is supposed to work !! Only choose the Pipes who fired the event, but it is not working */
1499 if (returnedpos < readPipes)
1502 * for (i = 0; i < readPipes; i++)
1504 * waitstatus = WaitForSingleObject (handle_array[i], 0);
1505 * LOG (GNUNET_ERROR_TYPE_DEBUG, "Read pipe %d wait status is : %d\n", i, waitstatus);
1506 * if (waitstatus != WAIT_OBJECT_0)
1508 * GNUNET_CONTAINER_slist_add (handles_read,
1509 * GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1510 * readArray[i], sizeof (struct GNUNET_DISK_FileHandle));
1512 * LOG (GNUNET_ERROR_TYPE_DEBUG, "Added read Pipe\n");
1515 for (i = 0; i < readPipes; i++)
1523 PeekNamedPipe (readArray[i]->h, NULL, 0, NULL, &waitstatus, NULL);
1524 error = GetLastError ();
1526 LOG (GNUNET_ERROR_TYPE_DEBUG,
1527 "Peek at read pipe %d (0x%x) returned %d (%d bytes available) GLE %u\n",
1528 i, readArray[i]->h, bret, waitstatus, error);
1532 if (error != ERROR_BROKEN_PIPE)
1535 else if (waitstatus <= 0)
1537 GNUNET_CONTAINER_slist_add (handles_read,
1538 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1540 sizeof (struct GNUNET_DISK_FileHandle));
1543 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 LOG (GNUNET_ERROR_TYPE_DEBUG, "Wait for the write event returned %d\n",
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 LOG (GNUNET_ERROR_TYPE_DEBUG,
1569 "send to the socket %d returned %d (%u)\n", i, status, error);
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 LOG (GNUNET_ERROR_TYPE_DEBUG, "Returning from _select() with nothing!\n");
1587 struct GNUNET_CONTAINER_SList_Iterator t;
1589 for (i = 0; i < rfds->sds.fd_count; i++)
1591 WSAEventSelect (rfds->sds.fd_array[i], hEventRead, 0);
1594 for (t = GNUNET_CONTAINER_slist_begin (rfds->handles);
1595 GNUNET_CONTAINER_slist_end (&t) != GNUNET_YES;
1596 GNUNET_CONTAINER_slist_next (&t))
1598 struct GNUNET_DISK_FileHandle *fh;
1600 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (&t,
1602 if (fh->type == GNUNET_PIPE)
1608 LOG (GNUNET_ERROR_TYPE_DEBUG, "Zeroing rfds\n");
1610 GNUNET_NETWORK_fdset_zero (rfds);
1611 if (retcode != -1 && nhandles && (returnedpos < nhandles))
1612 GNUNET_NETWORK_fdset_copy_native (rfds, &aread, retcode);
1613 GNUNET_CONTAINER_slist_append (rfds->handles, handles_read);
1617 for (i = 0; i < wfds->sds.fd_count; i++)
1619 WSAEventSelect (wfds->sds.fd_array[i], hEventWrite, 0);
1623 LOG (GNUNET_ERROR_TYPE_DEBUG, "Zeroing wfds\n");
1625 GNUNET_NETWORK_fdset_zero (wfds);
1626 if (retcode != -1 && nhandles && (returnedpos < nhandles))
1627 GNUNET_NETWORK_fdset_copy_native (wfds, &awrite, retcode);
1628 GNUNET_CONTAINER_slist_append (wfds->handles, handles_write);
1632 for (i = 0; i < efds->sds.fd_count; i++)
1634 WSAEventSelect (efds->sds.fd_array[i], hEventException, 0);
1638 LOG (GNUNET_ERROR_TYPE_DEBUG, "Zeroing efds\n");
1640 GNUNET_NETWORK_fdset_zero (efds);
1641 if (retcode != -1 && nhandles && (returnedpos < nhandles))
1642 GNUNET_NETWORK_fdset_copy_native (efds, &aexcept, retcode);
1643 GNUNET_CONTAINER_slist_append (efds->handles, handles_except);
1645 GNUNET_CONTAINER_slist_destroy (handles_read);
1646 GNUNET_CONTAINER_slist_destroy (handles_write);
1647 GNUNET_CONTAINER_slist_destroy (handles_except);
1651 struct GNUNET_CONTAINER_SList_Iterator t;
1653 for (i = 0; i < bread.fd_count; i++)
1655 if (bread.fd_array[i] != 0)
1656 LOG (GNUNET_ERROR_TYPE_DEBUG, "FD 0x%x is %s in rfds\n",
1658 (SAFE_FD_ISSET (bread.fd_array[i], rfds)) ? "SET" : "NOT SET");
1660 for (t = GNUNET_CONTAINER_slist_begin (rfds->handles);
1661 GNUNET_CONTAINER_slist_end (&t) != GNUNET_YES;
1662 GNUNET_CONTAINER_slist_next (&t))
1664 struct GNUNET_DISK_FileHandle *fh;
1666 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (&t,
1668 LOG (GNUNET_ERROR_TYPE_DEBUG, "FD 0x%x is SET in rfds\n", fh->h);
1673 for (i = 0; i < bwrite.fd_count; i++)
1675 if (bwrite.fd_array[i] != 0)
1676 LOG (GNUNET_ERROR_TYPE_DEBUG, "FD 0x%x is %s in wfds\n",
1678 (SAFE_FD_ISSET (bwrite.fd_array[i], rfds)) ? "SET" : "NOT SET");
1683 for (i = 0; i < bexcept.fd_count; i++)
1685 if (bexcept.fd_array[i] != 0)
1686 LOG (GNUNET_ERROR_TYPE_DEBUG, "FD 0x%x is %s in efds\n",
1687 bexcept.fd_array[i],
1688 (SAFE_FD_ISSET (bexcept.fd_array[i], rfds)) ? "SET" : "NOT SET");
1691 LOG (GNUNET_ERROR_TYPE_DEBUG, "Returning %d or 0\n", retcode);
1693 if (nhandles && (returnedpos < nhandles))
1700 /* end of network.c */