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 (SOCKTYPE fd)
382 struct GNUNET_NETWORK_Handle *ret;
386 /* FIXME: Find a better call to check that FD is valid */
387 if (WSAIoctl (fd, FIONBIO, (void *) &i, sizeof (i), NULL, 0, &d, NULL, NULL) != 0)
388 return NULL; /* invalid FD */
389 ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle));
394 if (fcntl (fd, F_GETFD) < 0)
395 return NULL; /* invalid FD */
396 ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle));
407 * @param address peer address
408 * @param address_len length of address
409 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
412 GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Handle *desc,
413 const struct sockaddr *address,
414 socklen_t address_len)
418 ret = connect (desc->fd, address, address_len);
421 if (SOCKET_ERROR == ret)
423 SetErrnoFromWinsockError (WSAGetLastError ());
424 if (errno == EWOULDBLOCK)
428 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
436 * @param level protocol level of the option
437 * @param optname identifier of the option
438 * @param optval options
439 * @param optlen length of optval
440 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
443 GNUNET_NETWORK_socket_getsockopt (const struct GNUNET_NETWORK_Handle *desc,
444 int level, int optname, void *optval,
449 ret = getsockopt (desc->fd, level, optname, optval, optlen);
452 if (ret == 0 && level == SOL_SOCKET && optname == SO_ERROR)
453 *((int *) optval) = GetErrnoFromWinsockError (*((int *) optval));
455 else if (SOCKET_ERROR == ret)
456 SetErrnoFromWinsockError (WSAGetLastError ());
458 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
465 * @param backlog length of the listen queue
466 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
469 GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Handle *desc,
474 ret = listen (desc->fd, backlog);
477 if (SOCKET_ERROR == ret)
478 SetErrnoFromWinsockError (WSAGetLastError ());
481 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
486 * How much data is available to be read on this descriptor?
488 * Returns GNUNET_NO if no data is available, or on error!
492 GNUNET_NETWORK_socket_recvfrom_amount (const struct GNUNET_NETWORK_Handle *
497 /* How much is there to be read? */
501 error = ioctl (desc->fd, FIONREAD, &pending);
506 error = ioctlsocket (desc->fd, FIONREAD, &pending);
507 if (error != SOCKET_ERROR)
516 * Read data from a connected socket (always non-blocking).
518 * @param buffer buffer
519 * @param length length of buffer
520 * @param src_addr either the source to recv from, or all zeroes
521 * to be filled in by recvfrom
522 * @param addrlen length of the addr
525 GNUNET_NETWORK_socket_recvfrom (const struct GNUNET_NETWORK_Handle * desc,
526 void *buffer, size_t length,
527 struct sockaddr * src_addr, socklen_t * addrlen)
535 flags |= MSG_DONTWAIT;
538 ret = recvfrom (desc->fd, buffer, length, flags, src_addr, addrlen);
540 if (SOCKET_ERROR == ret)
541 SetErrnoFromWinsockError (WSAGetLastError ());
548 * Read data from a connected socket (always non-blocking).
550 * @param buffer buffer
551 * @param length length of buffer
554 GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Handle * desc,
555 void *buffer, size_t length)
563 flags |= MSG_DONTWAIT;
565 ret = recv (desc->fd, buffer, length, flags);
567 if (SOCKET_ERROR == ret)
568 SetErrnoFromWinsockError (WSAGetLastError ());
575 * Send data (always non-blocking).
578 * @param buffer data to send
579 * @param length size of the buffer
580 * @return number of bytes sent, GNUNET_SYSERR on error
583 GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Handle * desc,
584 const void *buffer, size_t length)
592 flags |= MSG_DONTWAIT;
596 flags |= MSG_NOSIGNAL;
599 ret = send (desc->fd, buffer, length, flags);
602 if (SOCKET_ERROR == ret)
603 SetErrnoFromWinsockError (WSAGetLastError ());
611 * Send data to a particular destination (always non-blocking).
612 * This function only works for UDP sockets.
615 * @param message data to send
616 * @param length size of the data
617 * @param dest_addr destination address
618 * @param dest_len length of address
619 * @return number of bytes sent, GNUNET_SYSERR on error
622 GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Handle * desc,
623 const void *message, size_t length,
624 const struct sockaddr * dest_addr,
633 flags |= MSG_DONTWAIT;
636 flags |= MSG_NOSIGNAL;
638 ret = sendto (desc->fd, message, length, flags, dest_addr, dest_len);
640 if (SOCKET_ERROR == ret)
641 SetErrnoFromWinsockError (WSAGetLastError ());
650 * @param level protocol level of the option
651 * @param option_name option identifier
652 * @param option_value value to set
653 * @param option_len size of option_value
654 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
657 GNUNET_NETWORK_socket_setsockopt (struct GNUNET_NETWORK_Handle *fd, int level,
658 int option_name, const void *option_value,
659 socklen_t option_len)
663 ret = setsockopt (fd->fd, level, option_name, option_value, option_len);
665 if (SOCKET_ERROR == ret)
666 SetErrnoFromWinsockError (WSAGetLastError ());
668 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
673 * Create a new socket. Configure it for non-blocking IO and
674 * mark it as non-inheritable to child processes (set the
675 * close-on-exec flag).
677 * @param domain domain of the socket
678 * @param type socket type
679 * @param protocol network protocol
680 * @return new socket, NULL on error
682 struct GNUNET_NETWORK_Handle *
683 GNUNET_NETWORK_socket_create (int domain, int type, int protocol)
685 struct GNUNET_NETWORK_Handle *ret;
687 ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle));
689 ret->fd = socket (domain, type, protocol);
690 if (INVALID_SOCKET == ret->fd)
693 SetErrnoFromWinsockError (WSAGetLastError ());
700 if (ret->fd >= FD_SETSIZE)
702 GNUNET_break (0 == close (ret->fd));
709 if (GNUNET_SYSERR == socket_set_blocking (ret, GNUNET_NO))
711 /* we might want to treat this one as fatal... */
713 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret));
718 if (GNUNET_OK != socket_set_inheritable (ret))
719 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
720 "socket_set_inheritable");
723 socket_set_nosigpipe (ret);
725 if ((type == SOCK_STREAM)
727 && (domain != AF_UNIX)
730 socket_set_nodelay (ret);
736 * Shut down socket operations
738 * @param how type of shutdown
739 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
742 GNUNET_NETWORK_socket_shutdown (struct GNUNET_NETWORK_Handle *desc, int how)
746 ret = shutdown (desc->fd, how);
749 SetErrnoFromWinsockError (WSAGetLastError ());
751 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
756 * Disable the "CORK" feature for communication with the given socket,
757 * forcing the OS to immediately flush the buffer on transmission
758 * instead of potentially buffering multiple messages. Essentially
759 * reduces the OS send buffers to zero.
762 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
765 GNUNET_NETWORK_socket_disable_corking (struct GNUNET_NETWORK_Handle *desc)
774 setsockopt (desc->fd, SOL_SOCKET, SO_SNDBUF, (char *) &value,
776 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
779 setsockopt (desc->fd, SOL_SOCKET, SO_RCVBUF, (char *) &value,
781 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
787 setsockopt (desc->fd, SOL_SOCKET, SO_SNDBUF, &value, sizeof (value))))
788 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
791 setsockopt (desc->fd, SOL_SOCKET, SO_RCVBUF, &value, sizeof (value))))
792 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
794 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
803 GNUNET_NETWORK_fdset_zero (struct GNUNET_NETWORK_FDSet *fds)
808 GNUNET_CONTAINER_slist_clear (fds->handles);
813 * Add a socket to the FD set
815 * @param desc socket to add
818 GNUNET_NETWORK_fdset_set (struct GNUNET_NETWORK_FDSet *fds,
819 const struct GNUNET_NETWORK_Handle *desc)
821 FD_SET (desc->fd, &fds->sds);
822 if (desc->fd + 1 > fds->nsds)
823 fds->nsds = desc->fd + 1;
828 * Check whether a socket is part of the fd set
831 * @return 0 if the FD is not set
834 GNUNET_NETWORK_fdset_isset (const struct GNUNET_NETWORK_FDSet *fds,
835 const struct GNUNET_NETWORK_Handle *desc)
837 return FD_ISSET (desc->fd, &fds->sds);
842 * Add one fd set to another
843 * @param dst the fd set to add to
844 * @param src the fd set to add from
847 GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst,
848 const struct GNUNET_NETWORK_FDSet *src)
852 for (nfds = src->nsds; nfds > 0; nfds--)
853 if (FD_ISSET (nfds, &src->sds))
856 FD_SET (nfds, &dst->sds);
857 if (nfds + 1 > dst->nsds)
858 dst->nsds = nfds + 1;
861 GNUNET_CONTAINER_slist_append (dst->handles, src->handles);
867 * Copy one fd set to another
869 * @param to destination
873 GNUNET_NETWORK_fdset_copy (struct GNUNET_NETWORK_FDSet *to,
874 const struct GNUNET_NETWORK_FDSet *from)
876 FD_COPY (&from->sds, &to->sds);
877 to->nsds = from->nsds;
880 GNUNET_CONTAINER_slist_clear (to->handles);
881 GNUNET_CONTAINER_slist_append (to->handles, from->handles);
887 * Return file descriptor for this network handle
889 * @param desc wrapper to process
890 * @return POSIX file descriptor
893 GNUNET_NETWORK_get_fd (struct GNUNET_NETWORK_Handle *desc)
900 * Copy a native fd set
902 * @param to destination
903 * @param from native source set
904 * @param nfds the biggest socket number in from + 1
907 GNUNET_NETWORK_fdset_copy_native (struct GNUNET_NETWORK_FDSet *to,
908 const fd_set * from, int nfds)
910 FD_COPY (from, &to->sds);
916 * Set a native fd in a set
918 * @param to destination
919 * @param nfd native FD to set
922 GNUNET_NETWORK_fdset_set_native (struct GNUNET_NETWORK_FDSet *to, int nfd)
924 GNUNET_assert ((nfd >= 0) && (nfd < FD_SETSIZE));
925 FD_SET (nfd, &to->sds);
926 to->nsds = GNUNET_MAX (nfd + 1, to->nsds);
931 * Test native fd in a set
933 * @param to set to test, NULL for empty set
934 * @param nfd native FD to test, or -1 for none
935 * @return GNUNET_YES if FD is set in the set
938 GNUNET_NETWORK_fdset_test_native (const struct GNUNET_NETWORK_FDSet *to,
941 if ((nfd == -1) || (to == NULL))
943 return FD_ISSET (nfd, &to->sds) ? GNUNET_YES : GNUNET_NO;
948 * Add a file handle to the fd set
950 * @param h the file handle to add
953 GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds,
954 const struct GNUNET_DISK_FileHandle *h)
957 GNUNET_CONTAINER_slist_add (fds->handles,
958 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT, h,
959 sizeof (struct GNUNET_DISK_FileHandle));
964 GNUNET_DISK_internal_file_handle_ (h, &fd, sizeof (int));
965 FD_SET (fd, &fds->sds);
966 if (fd + 1 > fds->nsds)
974 * Check if a file handle is part of an fd set
976 * @param h file handle
977 * @return GNUNET_YES if the file handle is part of the set
980 GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds,
981 const struct GNUNET_DISK_FileHandle *h)
985 return GNUNET_CONTAINER_slist_contains (fds->handles, h,
987 GNUNET_DISK_FileHandle));
989 return FD_ISSET (h->fd, &fds->sds);
995 * Checks if two fd sets overlap
996 * @param fds1 first fd set
997 * @param fds2 second fd set
998 * @return GNUNET_YES if they do overlap, GNUNET_NO otherwise
1001 GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1,
1002 const struct GNUNET_NETWORK_FDSet *fds2)
1008 if (nfds > fds2->nsds)
1013 if (FD_ISSET (nfds, &fds1->sds) && FD_ISSET (nfds, &fds2->sds))
1017 struct GNUNET_CONTAINER_SList_Iterator it;
1018 struct GNUNET_DISK_FileHandle *h;
1022 /*This code is somewhat hacky, we are not supposed to know what's
1023 * inside of fd_set; also the O(n^2) is really bad... */
1025 for (i = 0; i < fds1->sds.fd_count; i++)
1027 for (j = 0; j < fds2->sds.fd_count; j++)
1029 if (fds1->sds.fd_array[i] == fds2->sds.fd_array[j])
1033 it = GNUNET_CONTAINER_slist_begin (fds1->handles);
1034 while (GNUNET_CONTAINER_slist_end (&it) != GNUNET_YES)
1037 struct GNUNET_CONTAINER_SList_Iterator t;
1039 h = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (&it,
1042 LOG (GNUNET_ERROR_TYPE_DEBUG, "Checking that FD 0x%x is in another set:\n",
1044 for (t = GNUNET_CONTAINER_slist_begin (fds2->handles);
1045 GNUNET_CONTAINER_slist_end (&t) != GNUNET_YES;
1046 GNUNET_CONTAINER_slist_next (&t))
1048 struct GNUNET_DISK_FileHandle *fh;
1050 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (&t,
1052 LOG (GNUNET_ERROR_TYPE_DEBUG, "0x%x\n", fh->h);
1055 if (GNUNET_CONTAINER_slist_contains
1056 (fds2->handles, h, sizeof (struct GNUNET_DISK_FileHandle)))
1059 LOG (GNUNET_ERROR_TYPE_DEBUG, "Match!\n");
1063 GNUNET_CONTAINER_slist_next (&it);
1072 * @return a new fd set
1074 struct GNUNET_NETWORK_FDSet *
1075 GNUNET_NETWORK_fdset_create ()
1077 struct GNUNET_NETWORK_FDSet *fds;
1079 fds = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_FDSet));
1081 fds->handles = GNUNET_CONTAINER_slist_create ();
1083 GNUNET_NETWORK_fdset_zero (fds);
1089 * Releases the associated memory of an fd set
1093 GNUNET_NETWORK_fdset_destroy (struct GNUNET_NETWORK_FDSet *fds)
1096 GNUNET_CONTAINER_slist_destroy (fds->handles);
1102 * Check if sockets meet certain conditions
1103 * @param rfds set of sockets to be checked for readability
1104 * @param wfds set of sockets to be checked for writability
1105 * @param efds set of sockets to be checked for exceptions
1106 * @param timeout relative value when to return
1107 * @return number of selected sockets, GNUNET_SYSERR on error
1110 GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
1111 struct GNUNET_NETWORK_FDSet *wfds,
1112 struct GNUNET_NETWORK_FDSet *efds,
1113 const struct GNUNET_TIME_Relative timeout)
1120 int read_handles = 0;
1121 int write_handles = 0;
1127 int nsock = 0, nhandles = 0, nSockEvents = 0;
1129 static HANDLE hEventRead = 0;
1130 static HANDLE hEventWrite = 0;
1131 static HANDLE hEventException = 0;
1132 static HANDLE hEventPipeWrite = 0;
1133 static HANDLE hEventReadReady = 0;
1136 int writePipePos = 0;
1138 HANDLE handle_array[FD_SETSIZE + 2];
1139 int returncode = -1;
1140 DWORD newretcode = 0;
1141 int returnedpos = 0;
1143 struct GNUNET_CONTAINER_SList *handles_read, *handles_write, *handles_except;
1145 fd_set aread, awrite, aexcept;
1148 fd_set bread, bwrite, bexcept;
1151 /* TODO: Make this growable */
1152 struct GNUNET_DISK_FileHandle *readArray[50];
1160 handles += read_handles = GNUNET_CONTAINER_slist_count (rfds->handles);
1163 struct GNUNET_CONTAINER_SList_Iterator t;
1165 for (t = GNUNET_CONTAINER_slist_begin (rfds->handles);
1166 GNUNET_CONTAINER_slist_end (&t) != GNUNET_YES;
1167 GNUNET_CONTAINER_slist_next (&t))
1169 struct GNUNET_DISK_FileHandle *fh;
1171 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (&t,
1173 LOG (GNUNET_ERROR_TYPE_DEBUG, "FD 0x%x (0x%x) is SET in rfds\n", fh->h,
1182 nfds = GNUNET_MAX (nfds, wfds->nsds);
1184 handles += write_handles = GNUNET_CONTAINER_slist_count (wfds->handles);
1189 nfds = GNUNET_MAX (nfds, efds->nsds);
1191 handles += ex_handles = GNUNET_CONTAINER_slist_count (efds->handles);
1196 (timeout.rel_value == GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
1202 LOG (GNUNET_ERROR_TYPE_ERROR,
1204 ("Fatal internal logic error, process hangs in `%s' (abort with CTRL-C)!\n"),
1209 tv.tv_sec = timeout.rel_value / GNUNET_TIME_UNIT_SECONDS.rel_value;
1211 1000 * (timeout.rel_value -
1212 (tv.tv_sec * GNUNET_TIME_UNIT_SECONDS.rel_value));
1213 return select (nfds, (rfds != NULL) ? &rfds->sds : NULL,
1214 (wfds != NULL) ? &wfds->sds : NULL,
1215 (efds != NULL) ? &efds->sds : NULL,
1216 (timeout.rel_value ==
1217 GNUNET_TIME_UNIT_FOREVER_REL.rel_value) ? NULL : &tv);
1220 #define SAFE_FD_ISSET(fd, set) (set != NULL && FD_ISSET(fd, set))
1221 /* calculate how long we need to wait in milliseconds */
1222 if (timeout.rel_value == GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
1223 ms_total = INFINITE;
1225 ms_total = timeout.rel_value / GNUNET_TIME_UNIT_MILLISECONDS.rel_value;
1226 /* select() may be used as a portable way to sleep */
1227 if (!(rfds || wfds || efds))
1233 /* Events for sockets */
1235 hEventRead = CreateEvent (NULL, TRUE, FALSE, NULL);
1237 ResetEvent (hEventRead);
1238 if (!hEventReadReady)
1239 hEventReadReady = CreateEvent (NULL, TRUE, TRUE, NULL);
1241 hEventWrite = CreateEvent (NULL, TRUE, FALSE, NULL);
1243 ResetEvent (hEventWrite);
1244 if (!hEventException)
1245 hEventException = CreateEvent (NULL, TRUE, FALSE, NULL);
1247 ResetEvent (hEventException);
1249 /* Event for pipes */
1250 if (!hEventPipeWrite)
1251 hEventPipeWrite = CreateEvent (NULL, TRUE, TRUE, NULL);
1255 handles_read = GNUNET_CONTAINER_slist_create ();
1256 handles_write = GNUNET_CONTAINER_slist_create ();
1257 handles_except = GNUNET_CONTAINER_slist_create ();
1268 FD_COPY (&rfds->sds, &aread);
1270 FD_COPY (&rfds->sds, &bread);
1275 FD_COPY (&wfds->sds, &awrite);
1277 FD_COPY (&wfds->sds, &bwrite);
1282 FD_COPY (&efds->sds, &aexcept);
1284 FD_COPY (&efds->sds, &bexcept);
1287 /* We will first Add the PIPES to the events */
1289 if (rfds && read_handles)
1291 struct GNUNET_CONTAINER_SList_Iterator i;
1293 for (i = GNUNET_CONTAINER_slist_begin (rfds->handles);
1294 GNUNET_CONTAINER_slist_end (&i) != GNUNET_YES;
1295 GNUNET_CONTAINER_slist_next (&i))
1297 struct GNUNET_DISK_FileHandle *fh;
1299 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (&i,
1301 if (fh->type == GNUNET_PIPE)
1303 /* Read zero bytes to check the status of the pipe */
1305 LOG (GNUNET_ERROR_TYPE_DEBUG, "Reading 0 bytes from the pipe 0x%x\n",
1308 if (!ReadFile (fh->h, NULL, 0, NULL, fh->oOverlapRead))
1310 DWORD error_code = GetLastError ();
1312 if (error_code == ERROR_IO_PENDING)
1315 LOG (GNUNET_ERROR_TYPE_DEBUG,
1316 "Adding the pipe's 0x%x overlapped event to the array as %d\n",
1319 handle_array[nhandles++] = fh->oOverlapRead->hEvent;
1320 readArray[readPipes++] = fh;
1325 * SetErrnoFromWinError (error_code);
1332 LOG (GNUNET_ERROR_TYPE_DEBUG,
1333 "Adding the read ready event to the array as %d\n", nhandles);
1335 handle_array[nhandles++] = hEventReadReady;
1336 readArray[readPipes++] = fh;
1341 GNUNET_CONTAINER_slist_add (handles_read,
1342 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1343 fh, sizeof (struct GNUNET_DISK_FileHandle));
1347 if (wfds && write_handles)
1350 LOG (GNUNET_ERROR_TYPE_DEBUG,
1351 "Adding the write ready event to the array as %d\n", nhandles);
1353 handle_array[nhandles++] = hEventPipeWrite;
1354 writePipePos = nhandles;
1356 if (efds && ex_handles)
1358 struct GNUNET_CONTAINER_SList_Iterator i;
1360 for (i = GNUNET_CONTAINER_slist_begin (efds->handles);
1361 GNUNET_CONTAINER_slist_end (&i) != GNUNET_YES;
1362 GNUNET_CONTAINER_slist_next (&i))
1364 struct GNUNET_DISK_FileHandle *fh;
1367 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (&i,
1369 if (fh->type == GNUNET_PIPE)
1371 if (!PeekNamedPipe (fh->h, NULL, 0, NULL, &dwBytes, NULL))
1373 GNUNET_CONTAINER_slist_add (handles_except,
1374 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1376 sizeof (struct GNUNET_DISK_FileHandle));
1387 LOG (GNUNET_ERROR_TYPE_DEBUG,
1388 "Adding the socket read event to the array as %d\n", nhandles);
1390 handle_array[nhandles++] = hEventRead;
1392 for (i = 0; i < rfds->sds.fd_count; i++)
1394 WSAEventSelect (rfds->sds.fd_array[i], hEventRead,
1395 FD_ACCEPT | FD_READ | FD_CLOSE);
1404 LOG (GNUNET_ERROR_TYPE_DEBUG,
1405 "Adding the socket write event to the array as %d\n", nhandles);
1407 handle_array[nhandles++] = hEventWrite;
1409 for (i = 0; i < wfds->sds.fd_count; i++)
1414 status = send (wfds->sds.fd_array[i], NULL, 0, 0);
1415 error = GetLastError ();
1417 LOG (GNUNET_ERROR_TYPE_DEBUG,
1418 "pre-send to the socket %d returned %d (%u)\n", i, status, error);
1420 if (status == 0 || (error != WSAEWOULDBLOCK && error != WSAENOTCONN))
1422 WSAEventSelect (wfds->sds.fd_array[i], hEventWrite,
1423 FD_WRITE | FD_CONNECT | FD_CLOSE);
1427 SetEvent (hEventWrite);
1432 LOG (GNUNET_ERROR_TYPE_DEBUG,
1433 "Adding the socket error event to the array as %d\n", nhandles);
1435 handle_array[nhandles++] = hEventException;
1437 for (i = 0; i < efds->sds.fd_count; i++)
1439 WSAEventSelect (efds->sds.fd_array[i], hEventException,
1446 handle_array[nhandles] = NULL;
1449 LOG (GNUNET_ERROR_TYPE_DEBUG, "Number nfds : %d\n", nfds);
1450 LOG (GNUNET_ERROR_TYPE_DEBUG, "Number of handles : %d\n", nhandles);
1451 LOG (GNUNET_ERROR_TYPE_DEBUG, "retcode : %d\n", newretcode);
1452 LOG (GNUNET_ERROR_TYPE_DEBUG, "Will wait : %d\n", ms_total);
1457 WaitForMultipleObjects (nhandles, handle_array, FALSE, ms_total);
1459 LOG (GNUNET_ERROR_TYPE_DEBUG, "WaitForMultipleObjects Returned : %d\n",
1463 returnedpos = returncode - WAIT_OBJECT_0;
1465 LOG (GNUNET_ERROR_TYPE_DEBUG, "return pos is : %d\n", returnedpos);
1468 /* FIXME: THIS LINE IS WRONG !! We should add to handles only handles that fired the events, not all ! */
1471 * GNUNET_CONTAINER_slist_append (handles_read, rfds->handles);
1473 if (nhandles && (returnedpos < nhandles))
1480 struct timeval tvslice;
1483 tvslice.tv_usec = 10;
1484 retcode = select (nfds, &aread, &awrite, &aexcept, &tvslice);
1488 LOG (GNUNET_ERROR_TYPE_DEBUG, "Select retcode : %d\n", retcode);
1491 /* FIXME: <= writePipePos? Really? */
1492 if ((writePipePos != -1) && (returnedpos <= writePipePos))
1494 GNUNET_CONTAINER_slist_append (handles_write, wfds->handles);
1495 retcode += write_handles;
1497 LOG (GNUNET_ERROR_TYPE_DEBUG, "Added write pipe\n");
1501 LOG (GNUNET_ERROR_TYPE_DEBUG, "ReadPipes is : %d\n", readPipes);
1503 /* We have some pipes ready for read. */
1504 /* FIXME: it is supposed to work !! Only choose the Pipes who fired the event, but it is not working */
1506 if (returnedpos < readPipes)
1509 * for (i = 0; i < readPipes; i++)
1511 * waitstatus = WaitForSingleObject (handle_array[i], 0);
1512 * LOG (GNUNET_ERROR_TYPE_DEBUG, "Read pipe %d wait status is : %d\n", i, waitstatus);
1513 * if (waitstatus != WAIT_OBJECT_0)
1515 * GNUNET_CONTAINER_slist_add (handles_read,
1516 * GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1517 * readArray[i], sizeof (struct GNUNET_DISK_FileHandle));
1519 * LOG (GNUNET_ERROR_TYPE_DEBUG, "Added read Pipe\n");
1522 for (i = 0; i < readPipes; i++)
1530 PeekNamedPipe (readArray[i]->h, NULL, 0, NULL, &waitstatus, NULL);
1531 error = GetLastError ();
1533 LOG (GNUNET_ERROR_TYPE_DEBUG,
1534 "Peek at read pipe %d (0x%x) returned %d (%d bytes available) GLE %u\n",
1535 i, readArray[i]->h, bret, waitstatus, error);
1539 if (error != ERROR_BROKEN_PIPE)
1542 else if (waitstatus <= 0)
1544 GNUNET_CONTAINER_slist_add (handles_read,
1545 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1547 sizeof (struct GNUNET_DISK_FileHandle));
1550 LOG (GNUNET_ERROR_TYPE_DEBUG, "Added read Pipe 0x%x (0x%x)\n",
1551 readArray[i], readArray[i]->h);
1555 waitstatus = WaitForSingleObject (hEventWrite, 0);
1557 LOG (GNUNET_ERROR_TYPE_DEBUG, "Wait for the write event returned %d\n",
1560 if (waitstatus == WAIT_OBJECT_0)
1562 for (i = 0; i < wfds->sds.fd_count; i++)
1567 int sizeof_so_error = sizeof (so_error);
1569 getsockopt (wfds->sds.fd_array[i], SOL_SOCKET, SO_ERROR,
1570 (char *) &so_error, &sizeof_so_error);
1572 status = send (wfds->sds.fd_array[i], NULL, 0, 0);
1573 error = GetLastError ();
1575 LOG (GNUNET_ERROR_TYPE_DEBUG,
1576 "send to the socket %d returned %d (%u)\n", i, status, error);
1578 if (status == 0 || (error != WSAEWOULDBLOCK && error != WSAENOTCONN) ||
1579 (status == -1 && gso_result == 0 && error == WSAENOTCONN &&
1580 so_error == WSAECONNREFUSED))
1582 FD_SET (wfds->sds.fd_array[i], &awrite);
1589 if (!nhandles || (returnedpos >= nhandles))
1590 LOG (GNUNET_ERROR_TYPE_DEBUG, "Returning from _select() with nothing!\n");
1594 struct GNUNET_CONTAINER_SList_Iterator t;
1596 for (i = 0; i < rfds->sds.fd_count; i++)
1598 WSAEventSelect (rfds->sds.fd_array[i], hEventRead, 0);
1601 for (t = GNUNET_CONTAINER_slist_begin (rfds->handles);
1602 GNUNET_CONTAINER_slist_end (&t) != GNUNET_YES;
1603 GNUNET_CONTAINER_slist_next (&t))
1605 struct GNUNET_DISK_FileHandle *fh;
1607 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (&t,
1609 if (fh->type == GNUNET_PIPE)
1615 LOG (GNUNET_ERROR_TYPE_DEBUG, "Zeroing rfds\n");
1617 GNUNET_NETWORK_fdset_zero (rfds);
1618 if (retcode != -1 && nhandles && (returnedpos < nhandles))
1619 GNUNET_NETWORK_fdset_copy_native (rfds, &aread, retcode);
1620 GNUNET_CONTAINER_slist_append (rfds->handles, handles_read);
1624 for (i = 0; i < wfds->sds.fd_count; i++)
1626 WSAEventSelect (wfds->sds.fd_array[i], hEventWrite, 0);
1630 LOG (GNUNET_ERROR_TYPE_DEBUG, "Zeroing wfds\n");
1632 GNUNET_NETWORK_fdset_zero (wfds);
1633 if (retcode != -1 && nhandles && (returnedpos < nhandles))
1634 GNUNET_NETWORK_fdset_copy_native (wfds, &awrite, retcode);
1635 GNUNET_CONTAINER_slist_append (wfds->handles, handles_write);
1639 for (i = 0; i < efds->sds.fd_count; i++)
1641 WSAEventSelect (efds->sds.fd_array[i], hEventException, 0);
1645 LOG (GNUNET_ERROR_TYPE_DEBUG, "Zeroing efds\n");
1647 GNUNET_NETWORK_fdset_zero (efds);
1648 if (retcode != -1 && nhandles && (returnedpos < nhandles))
1649 GNUNET_NETWORK_fdset_copy_native (efds, &aexcept, retcode);
1650 GNUNET_CONTAINER_slist_append (efds->handles, handles_except);
1652 GNUNET_CONTAINER_slist_destroy (handles_read);
1653 GNUNET_CONTAINER_slist_destroy (handles_write);
1654 GNUNET_CONTAINER_slist_destroy (handles_except);
1658 struct GNUNET_CONTAINER_SList_Iterator t;
1660 for (i = 0; i < bread.fd_count; i++)
1662 if (bread.fd_array[i] != 0)
1663 LOG (GNUNET_ERROR_TYPE_DEBUG, "FD 0x%x is %s in rfds\n",
1665 (SAFE_FD_ISSET (bread.fd_array[i], rfds)) ? "SET" : "NOT SET");
1667 for (t = GNUNET_CONTAINER_slist_begin (rfds->handles);
1668 GNUNET_CONTAINER_slist_end (&t) != GNUNET_YES;
1669 GNUNET_CONTAINER_slist_next (&t))
1671 struct GNUNET_DISK_FileHandle *fh;
1673 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (&t,
1675 LOG (GNUNET_ERROR_TYPE_DEBUG, "FD 0x%x is SET in rfds\n", fh->h);
1680 for (i = 0; i < bwrite.fd_count; i++)
1682 if (bwrite.fd_array[i] != 0)
1683 LOG (GNUNET_ERROR_TYPE_DEBUG, "FD 0x%x is %s in wfds\n",
1685 (SAFE_FD_ISSET (bwrite.fd_array[i], rfds)) ? "SET" : "NOT SET");
1690 for (i = 0; i < bexcept.fd_count; i++)
1692 if (bexcept.fd_array[i] != 0)
1693 LOG (GNUNET_ERROR_TYPE_DEBUG, "FD 0x%x is %s in efds\n",
1694 bexcept.fd_array[i],
1695 (SAFE_FD_ISSET (bexcept.fd_array[i], rfds)) ? "SET" : "NOT SET");
1698 LOG (GNUNET_ERROR_TYPE_DEBUG, "Returning %d or 0\n", retcode);
1700 if (nhandles && (returnedpos < nhandles))
1707 /* end of network.c */