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
39 #ifndef INVALID_SOCKET
40 #define INVALID_SOCKET -1
44 struct GNUNET_NETWORK_Handle
53 * Address family / domain.
58 * Number of bytes in addr.
63 * Address we were bound to, or NULL.
65 struct sockaddr *addr;
71 #define FD_COPY(s, d) (memcpy ((d), (s), sizeof (fd_set)))
76 * Set if a socket should use blocking or non-blocking IO.
78 * @param doBlock blocking mode
79 * @return GNUNET_OK on success, GNUNET_SYSERR on error
82 socket_set_blocking (struct GNUNET_NETWORK_Handle *fd, int doBlock)
89 if (ioctlsocket (fd->fd, FIONBIO, &mode) == SOCKET_ERROR)
92 SetErrnoFromWinsockError (WSAGetLastError ());
93 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "ioctlsocket");
100 int flags = fcntl (fd->fd, F_GETFL);
105 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "fcntl");
106 return GNUNET_SYSERR;
109 flags &= ~O_NONBLOCK;
113 if (0 != fcntl (fd->fd, F_SETFL, flags))
116 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "fcntl");
117 return GNUNET_SYSERR;
126 * Make a socket non-inheritable to child processes
128 * @param h the socket to make non-inheritable
129 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
130 * @warning Not implemented on Windows
133 socket_set_inheritable (const struct GNUNET_NETWORK_Handle *h)
137 i = fcntl (h->fd, F_GETFD);
139 return GNUNET_SYSERR;
140 if (i == (i | FD_CLOEXEC))
143 if (fcntl (h->fd, F_SETFD, i) < 0)
144 return GNUNET_SYSERR;
152 * The MSG_NOSIGNAL equivalent on Mac OS X
154 * @param h the socket to make non-delaying
157 socket_set_nosigpipe (const struct GNUNET_NETWORK_Handle *h)
162 setsockopt (h->fd, SOL_SOCKET, SO_NOSIGPIPE, &abs_value,
164 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
170 * Disable delays when sending data via the socket.
171 * (GNUnet makes sure that messages are as big as
174 * @param h the socket to make non-delaying
177 socket_set_nodelay (const struct GNUNET_NETWORK_Handle *h)
182 if (0 != setsockopt (h->fd, IPPROTO_TCP, TCP_NODELAY, &value, sizeof (value)))
183 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
185 const char *abs_value = "1";
188 setsockopt (h->fd, IPPROTO_TCP, TCP_NODELAY, abs_value,
190 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
196 * accept a new connection on a socket
198 * @param desc bound socket
199 * @param address address of the connecting peer, may be NULL
200 * @param address_len length of address
201 * @return client socket
203 struct GNUNET_NETWORK_Handle *
204 GNUNET_NETWORK_socket_accept (const struct GNUNET_NETWORK_Handle *desc,
205 struct sockaddr *address, socklen_t * address_len)
207 struct GNUNET_NETWORK_Handle *ret;
209 ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle));
212 struct sockaddr name;
213 socklen_t namelen = sizeof (name);
214 int gsn = getsockname (desc->fd, &name, &namelen);
217 LOG (GNUNET_ERROR_TYPE_DEBUG, "Accepting connection on `%s'\n",
218 GNUNET_a2s (&name, namelen));
221 ret->fd = accept (desc->fd, address, address_len);
223 ret->af = address->sa_family;
226 if (ret->fd == INVALID_SOCKET)
229 SetErrnoFromWinsockError (WSAGetLastError ());
235 if (ret->fd >= FD_SETSIZE)
237 GNUNET_break (0 == close (ret->fd));
243 if (GNUNET_SYSERR == socket_set_blocking (ret, GNUNET_NO))
247 /* we might want to treat this one as fatal... */
249 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret));
254 if (GNUNET_OK != socket_set_inheritable (ret))
255 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
256 "socket_set_inheritable");
259 socket_set_nosigpipe (ret);
262 if (ret->af != AF_UNIX)
264 socket_set_nodelay (ret);
270 * Bind to a connected socket
272 * @param address address to be bound
273 * @param address_len length of address
274 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
277 GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc,
278 const struct sockaddr *address,
279 socklen_t address_len)
287 if (desc->af == AF_INET6)
288 if (0 != setsockopt (desc->fd, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof (on)))
289 LOG_STRERROR (GNUNET_ERROR_TYPE_DEBUG, "setsockopt");
293 /* This is required, and required here, but only on UNIX */
294 if (0 != setsockopt (desc->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)))
295 LOG_STRERROR (GNUNET_ERROR_TYPE_DEBUG, "setsockopt");
299 if (address->sa_family == AF_UNIX)
301 const struct sockaddr_un *un = (const struct sockaddr_un *) address;
303 (void) unlink (un->sun_path);
307 ret = bind (desc->fd, address, address_len);
309 if (SOCKET_ERROR == ret)
310 SetErrnoFromWinsockError (WSAGetLastError ());
313 return GNUNET_SYSERR;
316 desc->addr = GNUNET_malloc (address_len);
317 memcpy (desc->addr, address, address_len);
318 desc->addrlen = address_len;
328 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
331 GNUNET_NETWORK_socket_close (struct GNUNET_NETWORK_Handle *desc)
339 ret = closesocket (desc->fd);
340 error = WSAGetLastError ();
341 SetErrnoFromWinsockError (error);
342 LOG (GNUNET_ERROR_TYPE_DEBUG,
343 "Closed 0x%x, closesocket() returned %d, GLE is %u\n", desc->fd, ret,
346 ret = close (desc->fd);
350 if ((desc->af == AF_UNIX) && (NULL != desc->addr))
352 const struct sockaddr_un *un = (const struct sockaddr_un *) desc->addr;
354 if (0 != unlink (un->sun_path))
355 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "unlink", un->sun_path);
359 GNUNET_free_non_null (desc->addr);
361 return (ret == 0) ? GNUNET_OK : GNUNET_SYSERR;
366 * Box a native socket (and check that it is a socket).
368 * @param fd socket to box
369 * @return NULL on error (including not supported on target platform)
371 struct GNUNET_NETWORK_Handle *
372 GNUNET_NETWORK_socket_box_native (SOCKTYPE fd)
374 struct GNUNET_NETWORK_Handle *ret;
378 /* FIXME: Find a better call to check that FD is valid */
379 if (WSAIoctl (fd, FIONBIO, (void *) &i, sizeof (i), NULL, 0, &d, NULL, NULL) != 0)
380 return NULL; /* invalid FD */
381 ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle));
386 if (fcntl (fd, F_GETFD) < 0)
387 return NULL; /* invalid FD */
388 ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle));
399 * @param address peer address
400 * @param address_len length of address
401 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
404 GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Handle *desc,
405 const struct sockaddr *address,
406 socklen_t address_len)
410 ret = connect (desc->fd, address, address_len);
413 if (SOCKET_ERROR == ret)
415 SetErrnoFromWinsockError (WSAGetLastError ());
416 if (errno == EWOULDBLOCK)
420 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
428 * @param level protocol level of the option
429 * @param optname identifier of the option
430 * @param optval options
431 * @param optlen length of optval
432 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
435 GNUNET_NETWORK_socket_getsockopt (const struct GNUNET_NETWORK_Handle *desc,
436 int level, int optname, void *optval,
441 ret = getsockopt (desc->fd, level, optname, optval, optlen);
444 if (ret == 0 && level == SOL_SOCKET && optname == SO_ERROR)
445 *((int *) optval) = GetErrnoFromWinsockError (*((int *) optval));
447 else if (SOCKET_ERROR == ret)
448 SetErrnoFromWinsockError (WSAGetLastError ());
450 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
457 * @param backlog length of the listen queue
458 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
461 GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Handle *desc,
466 ret = listen (desc->fd, backlog);
469 if (SOCKET_ERROR == ret)
470 SetErrnoFromWinsockError (WSAGetLastError ());
473 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
478 * How much data is available to be read on this descriptor?
480 * Returns GNUNET_NO if no data is available, or on error!
484 GNUNET_NETWORK_socket_recvfrom_amount (const struct GNUNET_NETWORK_Handle *
489 /* How much is there to be read? */
493 error = ioctl (desc->fd, FIONREAD, &pending);
498 error = ioctlsocket (desc->fd, FIONREAD, &pending);
499 if (error != SOCKET_ERROR)
508 * Read data from a connected socket (always non-blocking).
510 * @param buffer buffer
511 * @param length length of buffer
512 * @param src_addr either the source to recv from, or all zeroes
513 * to be filled in by recvfrom
514 * @param addrlen length of the addr
517 GNUNET_NETWORK_socket_recvfrom (const struct GNUNET_NETWORK_Handle * desc,
518 void *buffer, size_t length,
519 struct sockaddr * src_addr, socklen_t * addrlen)
527 flags |= MSG_DONTWAIT;
530 ret = recvfrom (desc->fd, buffer, length, flags, src_addr, addrlen);
532 if (SOCKET_ERROR == ret)
533 SetErrnoFromWinsockError (WSAGetLastError ());
540 * Read data from a connected socket (always non-blocking).
542 * @param buffer buffer
543 * @param length length of buffer
546 GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Handle * desc,
547 void *buffer, size_t length)
555 flags |= MSG_DONTWAIT;
557 ret = recv (desc->fd, buffer, length, flags);
559 if (SOCKET_ERROR == ret)
560 SetErrnoFromWinsockError (WSAGetLastError ());
567 * Send data (always non-blocking).
570 * @param buffer data to send
571 * @param length size of the buffer
572 * @return number of bytes sent, GNUNET_SYSERR on error
575 GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Handle * desc,
576 const void *buffer, size_t length)
584 flags |= MSG_DONTWAIT;
588 flags |= MSG_NOSIGNAL;
591 ret = send (desc->fd, buffer, length, flags);
594 if (SOCKET_ERROR == ret)
595 SetErrnoFromWinsockError (WSAGetLastError ());
603 * Send data to a particular destination (always non-blocking).
604 * This function only works for UDP sockets.
607 * @param message data to send
608 * @param length size of the data
609 * @param dest_addr destination address
610 * @param dest_len length of address
611 * @return number of bytes sent, GNUNET_SYSERR on error
614 GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Handle * desc,
615 const void *message, size_t length,
616 const struct sockaddr * dest_addr,
625 flags |= MSG_DONTWAIT;
628 flags |= MSG_NOSIGNAL;
630 ret = sendto (desc->fd, message, length, flags, dest_addr, dest_len);
632 if (SOCKET_ERROR == ret)
633 SetErrnoFromWinsockError (WSAGetLastError ());
642 * @param level protocol level of the option
643 * @param option_name option identifier
644 * @param option_value value to set
645 * @param option_len size of option_value
646 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
649 GNUNET_NETWORK_socket_setsockopt (struct GNUNET_NETWORK_Handle *fd, int level,
650 int option_name, const void *option_value,
651 socklen_t option_len)
655 ret = setsockopt (fd->fd, level, option_name, option_value, option_len);
657 if (SOCKET_ERROR == ret)
658 SetErrnoFromWinsockError (WSAGetLastError ());
660 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
665 * Create a new socket. Configure it for non-blocking IO and
666 * mark it as non-inheritable to child processes (set the
667 * close-on-exec flag).
669 * @param domain domain of the socket
670 * @param type socket type
671 * @param protocol network protocol
672 * @return new socket, NULL on error
674 struct GNUNET_NETWORK_Handle *
675 GNUNET_NETWORK_socket_create (int domain, int type, int protocol)
677 struct GNUNET_NETWORK_Handle *ret;
679 ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle));
681 ret->fd = socket (domain, type, protocol);
682 if (INVALID_SOCKET == ret->fd)
685 SetErrnoFromWinsockError (WSAGetLastError ());
692 if (ret->fd >= FD_SETSIZE)
694 GNUNET_break (0 == close (ret->fd));
701 if (GNUNET_SYSERR == socket_set_blocking (ret, GNUNET_NO))
703 /* we might want to treat this one as fatal... */
705 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret));
710 if (GNUNET_OK != socket_set_inheritable (ret))
711 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
712 "socket_set_inheritable");
715 socket_set_nosigpipe (ret);
717 if ((type == SOCK_STREAM)
719 && (domain != AF_UNIX)
722 socket_set_nodelay (ret);
728 * Shut down socket operations
730 * @param how type of shutdown
731 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
734 GNUNET_NETWORK_socket_shutdown (struct GNUNET_NETWORK_Handle *desc, int how)
738 ret = shutdown (desc->fd, how);
741 SetErrnoFromWinsockError (WSAGetLastError ());
743 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
748 * Disable the "CORK" feature for communication with the given socket,
749 * forcing the OS to immediately flush the buffer on transmission
750 * instead of potentially buffering multiple messages. Essentially
751 * reduces the OS send buffers to zero.
754 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
757 GNUNET_NETWORK_socket_disable_corking (struct GNUNET_NETWORK_Handle *desc)
766 setsockopt (desc->fd, SOL_SOCKET, SO_SNDBUF, (char *) &value,
768 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
771 setsockopt (desc->fd, SOL_SOCKET, SO_RCVBUF, (char *) &value,
773 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
779 setsockopt (desc->fd, SOL_SOCKET, SO_SNDBUF, &value, sizeof (value))))
780 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
783 setsockopt (desc->fd, SOL_SOCKET, SO_RCVBUF, &value, sizeof (value))))
784 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
786 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
795 GNUNET_NETWORK_fdset_zero (struct GNUNET_NETWORK_FDSet *fds)
800 GNUNET_CONTAINER_slist_clear (fds->handles);
805 * Add a socket to the FD set
807 * @param desc socket to add
810 GNUNET_NETWORK_fdset_set (struct GNUNET_NETWORK_FDSet *fds,
811 const struct GNUNET_NETWORK_Handle *desc)
813 FD_SET (desc->fd, &fds->sds);
814 if (desc->fd + 1 > fds->nsds)
815 fds->nsds = desc->fd + 1;
820 * Check whether a socket is part of the fd set
823 * @return 0 if the FD is not set
826 GNUNET_NETWORK_fdset_isset (const struct GNUNET_NETWORK_FDSet *fds,
827 const struct GNUNET_NETWORK_Handle *desc)
829 return FD_ISSET (desc->fd, &fds->sds);
834 * Add one fd set to another
835 * @param dst the fd set to add to
836 * @param src the fd set to add from
839 GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst,
840 const struct GNUNET_NETWORK_FDSet *src)
844 for (nfds = src->nsds; nfds > 0; nfds--)
845 if (FD_ISSET (nfds, &src->sds))
848 FD_SET (nfds, &dst->sds);
849 if (nfds + 1 > dst->nsds)
850 dst->nsds = nfds + 1;
853 GNUNET_CONTAINER_slist_append (dst->handles, src->handles);
859 * Copy one fd set to another
861 * @param to destination
865 GNUNET_NETWORK_fdset_copy (struct GNUNET_NETWORK_FDSet *to,
866 const struct GNUNET_NETWORK_FDSet *from)
868 FD_COPY (&from->sds, &to->sds);
869 to->nsds = from->nsds;
872 GNUNET_CONTAINER_slist_clear (to->handles);
873 GNUNET_CONTAINER_slist_append (to->handles, from->handles);
879 * Return file descriptor for this network handle
881 * @param desc wrapper to process
882 * @return POSIX file descriptor
885 GNUNET_NETWORK_get_fd (struct GNUNET_NETWORK_Handle *desc)
892 * Copy a native fd set
894 * @param to destination
895 * @param from native source set
896 * @param nfds the biggest socket number in from + 1
899 GNUNET_NETWORK_fdset_copy_native (struct GNUNET_NETWORK_FDSet *to,
900 const fd_set * from, int nfds)
902 FD_COPY (from, &to->sds);
908 * Set a native fd in a set
910 * @param to destination
911 * @param nfd native FD to set
914 GNUNET_NETWORK_fdset_set_native (struct GNUNET_NETWORK_FDSet *to, int nfd)
916 GNUNET_assert ((nfd >= 0) && (nfd < FD_SETSIZE));
917 FD_SET (nfd, &to->sds);
918 to->nsds = GNUNET_MAX (nfd + 1, to->nsds);
923 * Test native fd in a set
925 * @param to set to test, NULL for empty set
926 * @param nfd native FD to test, or -1 for none
927 * @return GNUNET_YES if FD is set in the set
930 GNUNET_NETWORK_fdset_test_native (const struct GNUNET_NETWORK_FDSet *to,
933 if ((nfd == -1) || (to == NULL))
935 return FD_ISSET (nfd, &to->sds) ? GNUNET_YES : GNUNET_NO;
940 * Add a file handle to the fd set
942 * @param h the file handle to add
945 GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds,
946 const struct GNUNET_DISK_FileHandle *h)
949 GNUNET_CONTAINER_slist_add (fds->handles,
950 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT, h,
951 sizeof (struct GNUNET_DISK_FileHandle));
956 GNUNET_DISK_internal_file_handle_ (h, &fd, sizeof (int));
957 FD_SET (fd, &fds->sds);
958 if (fd + 1 > fds->nsds)
966 * Check if a file handle is part of an fd set
968 * @param h file handle
969 * @return GNUNET_YES if the file handle is part of the set
972 GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds,
973 const struct GNUNET_DISK_FileHandle *h)
977 return GNUNET_CONTAINER_slist_contains (fds->handles, h,
979 GNUNET_DISK_FileHandle));
981 return FD_ISSET (h->fd, &fds->sds);
987 * Checks if two fd sets overlap
988 * @param fds1 first fd set
989 * @param fds2 second fd set
990 * @return GNUNET_YES if they do overlap, GNUNET_NO otherwise
993 GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1,
994 const struct GNUNET_NETWORK_FDSet *fds2)
1000 if (nfds > fds2->nsds)
1005 if (FD_ISSET (nfds, &fds1->sds) && FD_ISSET (nfds, &fds2->sds))
1009 struct GNUNET_CONTAINER_SList_Iterator it;
1010 struct GNUNET_DISK_FileHandle *h;
1014 /*This code is somewhat hacky, we are not supposed to know what's
1015 * inside of fd_set; also the O(n^2) is really bad... */
1017 for (i = 0; i < fds1->sds.fd_count; i++)
1019 for (j = 0; j < fds2->sds.fd_count; j++)
1021 if (fds1->sds.fd_array[i] == fds2->sds.fd_array[j])
1025 it = GNUNET_CONTAINER_slist_begin (fds1->handles);
1026 while (GNUNET_CONTAINER_slist_end (&it) != GNUNET_YES)
1029 struct GNUNET_CONTAINER_SList_Iterator t;
1031 h = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (&it,
1034 LOG (GNUNET_ERROR_TYPE_DEBUG, "Checking that FD 0x%x is in another set:\n",
1036 for (t = GNUNET_CONTAINER_slist_begin (fds2->handles);
1037 GNUNET_CONTAINER_slist_end (&t) != GNUNET_YES;
1038 GNUNET_CONTAINER_slist_next (&t))
1040 struct GNUNET_DISK_FileHandle *fh;
1042 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (&t,
1044 LOG (GNUNET_ERROR_TYPE_DEBUG, "0x%x\n", fh->h);
1047 if (GNUNET_CONTAINER_slist_contains
1048 (fds2->handles, h, sizeof (struct GNUNET_DISK_FileHandle)))
1051 LOG (GNUNET_ERROR_TYPE_DEBUG, "Match!\n");
1055 GNUNET_CONTAINER_slist_next (&it);
1064 * @return a new fd set
1066 struct GNUNET_NETWORK_FDSet *
1067 GNUNET_NETWORK_fdset_create ()
1069 struct GNUNET_NETWORK_FDSet *fds;
1071 fds = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_FDSet));
1073 fds->handles = GNUNET_CONTAINER_slist_create ();
1075 GNUNET_NETWORK_fdset_zero (fds);
1081 * Releases the associated memory of an fd set
1085 GNUNET_NETWORK_fdset_destroy (struct GNUNET_NETWORK_FDSet *fds)
1088 GNUNET_CONTAINER_slist_destroy (fds->handles);
1094 * Check if sockets meet certain conditions
1095 * @param rfds set of sockets to be checked for readability
1096 * @param wfds set of sockets to be checked for writability
1097 * @param efds set of sockets to be checked for exceptions
1098 * @param timeout relative value when to return
1099 * @return number of selected sockets, GNUNET_SYSERR on error
1102 GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
1103 struct GNUNET_NETWORK_FDSet *wfds,
1104 struct GNUNET_NETWORK_FDSet *efds,
1105 const struct GNUNET_TIME_Relative timeout)
1112 int read_handles = 0;
1113 int write_handles = 0;
1119 int nsock = 0, nhandles = 0, nSockEvents = 0;
1121 static HANDLE hEventRead = 0;
1122 static HANDLE hEventWrite = 0;
1123 static HANDLE hEventException = 0;
1124 static HANDLE hEventPipeWrite = 0;
1125 static HANDLE hEventReadReady = 0;
1128 int writePipePos = 0;
1130 HANDLE handle_array[FD_SETSIZE + 2];
1131 int returncode = -1;
1132 DWORD newretcode = 0;
1133 int returnedpos = 0;
1135 struct GNUNET_CONTAINER_SList *handles_read, *handles_write, *handles_except;
1137 fd_set aread, awrite, aexcept;
1140 fd_set bread, bwrite, bexcept;
1143 /* TODO: Make this growable */
1144 struct GNUNET_DISK_FileHandle *readArray[50];
1152 handles += read_handles = GNUNET_CONTAINER_slist_count (rfds->handles);
1155 struct GNUNET_CONTAINER_SList_Iterator t;
1157 for (t = GNUNET_CONTAINER_slist_begin (rfds->handles);
1158 GNUNET_CONTAINER_slist_end (&t) != GNUNET_YES;
1159 GNUNET_CONTAINER_slist_next (&t))
1161 struct GNUNET_DISK_FileHandle *fh;
1163 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (&t,
1165 LOG (GNUNET_ERROR_TYPE_DEBUG, "FD 0x%x (0x%x) is SET in rfds\n", fh->h,
1174 nfds = GNUNET_MAX (nfds, wfds->nsds);
1176 handles += write_handles = GNUNET_CONTAINER_slist_count (wfds->handles);
1181 nfds = GNUNET_MAX (nfds, efds->nsds);
1183 handles += ex_handles = GNUNET_CONTAINER_slist_count (efds->handles);
1188 (timeout.rel_value == GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
1194 LOG (GNUNET_ERROR_TYPE_ERROR,
1196 ("Fatal internal logic error, process hangs in `%s' (abort with CTRL-C)!\n"),
1201 tv.tv_sec = timeout.rel_value / GNUNET_TIME_UNIT_SECONDS.rel_value;
1203 1000 * (timeout.rel_value -
1204 (tv.tv_sec * GNUNET_TIME_UNIT_SECONDS.rel_value));
1205 return select (nfds, (rfds != NULL) ? &rfds->sds : NULL,
1206 (wfds != NULL) ? &wfds->sds : NULL,
1207 (efds != NULL) ? &efds->sds : NULL,
1208 (timeout.rel_value ==
1209 GNUNET_TIME_UNIT_FOREVER_REL.rel_value) ? NULL : &tv);
1212 #define SAFE_FD_ISSET(fd, set) (set != NULL && FD_ISSET(fd, set))
1213 /* calculate how long we need to wait in milliseconds */
1214 if (timeout.rel_value == GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
1215 ms_total = INFINITE;
1217 ms_total = timeout.rel_value / GNUNET_TIME_UNIT_MILLISECONDS.rel_value;
1218 /* select() may be used as a portable way to sleep */
1219 if (!(rfds || wfds || efds))
1225 /* Events for sockets */
1227 hEventRead = CreateEvent (NULL, TRUE, FALSE, NULL);
1229 ResetEvent (hEventRead);
1230 if (!hEventReadReady)
1231 hEventReadReady = CreateEvent (NULL, TRUE, TRUE, NULL);
1233 hEventWrite = CreateEvent (NULL, TRUE, FALSE, NULL);
1235 ResetEvent (hEventWrite);
1236 if (!hEventException)
1237 hEventException = CreateEvent (NULL, TRUE, FALSE, NULL);
1239 ResetEvent (hEventException);
1241 /* Event for pipes */
1242 if (!hEventPipeWrite)
1243 hEventPipeWrite = CreateEvent (NULL, TRUE, TRUE, NULL);
1247 handles_read = GNUNET_CONTAINER_slist_create ();
1248 handles_write = GNUNET_CONTAINER_slist_create ();
1249 handles_except = GNUNET_CONTAINER_slist_create ();
1260 FD_COPY (&rfds->sds, &aread);
1262 FD_COPY (&rfds->sds, &bread);
1267 FD_COPY (&wfds->sds, &awrite);
1269 FD_COPY (&wfds->sds, &bwrite);
1274 FD_COPY (&efds->sds, &aexcept);
1276 FD_COPY (&efds->sds, &bexcept);
1279 /* We will first Add the PIPES to the events */
1281 if (rfds && read_handles)
1283 struct GNUNET_CONTAINER_SList_Iterator i;
1285 for (i = GNUNET_CONTAINER_slist_begin (rfds->handles);
1286 GNUNET_CONTAINER_slist_end (&i) != GNUNET_YES;
1287 GNUNET_CONTAINER_slist_next (&i))
1289 struct GNUNET_DISK_FileHandle *fh;
1291 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (&i,
1293 if (fh->type == GNUNET_PIPE)
1295 /* Read zero bytes to check the status of the pipe */
1296 LOG (GNUNET_ERROR_TYPE_DEBUG, "Reading 0 bytes from the pipe 0x%x\n",
1298 if (!ReadFile (fh->h, NULL, 0, NULL, fh->oOverlapRead))
1300 DWORD error_code = GetLastError ();
1302 if (error_code == ERROR_IO_PENDING)
1304 LOG (GNUNET_ERROR_TYPE_DEBUG,
1305 "Adding the pipe's 0x%x overlapped event to the array as %d\n",
1307 handle_array[nhandles++] = fh->oOverlapRead->hEvent;
1308 readArray[readPipes++] = fh;
1312 LOG (GNUNET_ERROR_TYPE_DEBUG,
1313 "Read failed, adding the read ready event to the array as %d\n", nhandles);
1314 handle_array[nhandles++] = hEventReadReady;
1315 readArray[readPipes++] = fh;
1320 LOG (GNUNET_ERROR_TYPE_DEBUG,
1321 "Adding the read ready event to the array as %d\n", nhandles);
1322 handle_array[nhandles++] = hEventReadReady;
1323 readArray[readPipes++] = fh;
1328 GNUNET_CONTAINER_slist_add (handles_read,
1329 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1330 fh, sizeof (struct GNUNET_DISK_FileHandle));
1334 if (wfds && write_handles)
1336 LOG (GNUNET_ERROR_TYPE_DEBUG,
1337 "Adding the write ready event to the array as %d\n", nhandles);
1338 handle_array[nhandles++] = hEventPipeWrite;
1339 writePipePos = nhandles;
1341 if (efds && ex_handles)
1343 struct GNUNET_CONTAINER_SList_Iterator i;
1345 for (i = GNUNET_CONTAINER_slist_begin (efds->handles);
1346 GNUNET_CONTAINER_slist_end (&i) != GNUNET_YES;
1347 GNUNET_CONTAINER_slist_next (&i))
1349 struct GNUNET_DISK_FileHandle *fh;
1352 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (&i,
1354 if (fh->type == GNUNET_PIPE)
1356 if (!PeekNamedPipe (fh->h, NULL, 0, NULL, &dwBytes, NULL))
1358 GNUNET_CONTAINER_slist_add (handles_except,
1359 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1361 sizeof (struct GNUNET_DISK_FileHandle));
1371 LOG (GNUNET_ERROR_TYPE_DEBUG,
1372 "Adding the socket read event to the array as %d\n", nhandles);
1373 handle_array[nhandles++] = hEventRead;
1375 for (i = 0; i < rfds->sds.fd_count; i++)
1377 WSAEventSelect (rfds->sds.fd_array[i], hEventRead,
1378 FD_ACCEPT | FD_READ | FD_CLOSE);
1386 LOG (GNUNET_ERROR_TYPE_DEBUG,
1387 "Adding the socket write event to the array as %d\n", nhandles);
1388 handle_array[nhandles++] = hEventWrite;
1390 for (i = 0; i < wfds->sds.fd_count; i++)
1395 status = send (wfds->sds.fd_array[i], NULL, 0, 0);
1396 error = GetLastError ();
1397 LOG (GNUNET_ERROR_TYPE_DEBUG,
1398 "pre-send to the socket %d returned %d (%u)\n", i, status, error);
1399 if (status == 0 || (error != WSAEWOULDBLOCK && error != WSAENOTCONN))
1401 WSAEventSelect (wfds->sds.fd_array[i], hEventWrite,
1402 FD_WRITE | FD_CONNECT | FD_CLOSE);
1406 SetEvent (hEventWrite);
1410 LOG (GNUNET_ERROR_TYPE_DEBUG,
1411 "Adding the socket error event to the array as %d\n", nhandles);
1412 handle_array[nhandles++] = hEventException;
1414 for (i = 0; i < efds->sds.fd_count; i++)
1416 WSAEventSelect (efds->sds.fd_array[i], hEventException,
1423 handle_array[nhandles] = NULL;
1426 LOG (GNUNET_ERROR_TYPE_DEBUG, "Number nfds : %d\n", nfds);
1427 LOG (GNUNET_ERROR_TYPE_DEBUG, "Number of handles : %d\n", nhandles);
1428 LOG (GNUNET_ERROR_TYPE_DEBUG, "retcode : %d\n", newretcode);
1429 LOG (GNUNET_ERROR_TYPE_DEBUG, "Will wait : %d\n", ms_total);
1434 WaitForMultipleObjects (nhandles, handle_array, FALSE, ms_total);
1436 LOG (GNUNET_ERROR_TYPE_DEBUG, "WaitForMultipleObjects Returned : %d\n",
1440 returnedpos = returncode - WAIT_OBJECT_0;
1441 LOG (GNUNET_ERROR_TYPE_DEBUG, "return pos is : %d\n", returnedpos);
1443 /* FIXME: THIS LINE IS WRONG !! We should add to handles only handles that fired the events, not all ! */
1446 * GNUNET_CONTAINER_slist_append (handles_read, rfds->handles);
1448 if (nhandles && (returnedpos < nhandles))
1455 struct timeval tvslice;
1458 tvslice.tv_usec = 10;
1459 retcode = select (nfds, &aread, &awrite, &aexcept, &tvslice);
1462 LOG (GNUNET_ERROR_TYPE_DEBUG, "Select retcode : %d\n", retcode);
1464 /* FIXME: <= writePipePos? Really? */
1465 if ((writePipePos != -1) && (returnedpos <= writePipePos))
1467 GNUNET_CONTAINER_slist_append (handles_write, wfds->handles);
1468 retcode += write_handles;
1469 LOG (GNUNET_ERROR_TYPE_DEBUG, "Added write pipe\n");
1471 LOG (GNUNET_ERROR_TYPE_DEBUG, "ReadPipes is : %d\n", readPipes);
1472 /* We have some pipes ready for read. */
1473 /* FIXME: it is supposed to work !! Only choose the Pipes who fired the event, but it is not working */
1475 if (returnedpos < readPipes)
1478 * for (i = 0; i < readPipes; i++)
1480 * waitstatus = WaitForSingleObject (handle_array[i], 0);
1481 * LOG (GNUNET_ERROR_TYPE_DEBUG, "Read pipe %d wait status is : %d\n", i, waitstatus);
1482 * if (waitstatus != WAIT_OBJECT_0)
1484 * GNUNET_CONTAINER_slist_add (handles_read,
1485 * GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1486 * readArray[i], sizeof (struct GNUNET_DISK_FileHandle));
1488 * LOG (GNUNET_ERROR_TYPE_DEBUG, "Added read Pipe\n");
1491 for (i = 0; i < readPipes; i++)
1499 PeekNamedPipe (readArray[i]->h, NULL, 0, NULL, &waitstatus, NULL);
1500 error = GetLastError ();
1501 LOG (GNUNET_ERROR_TYPE_DEBUG,
1502 "Peek at read pipe %d (0x%x) returned %d (%d bytes available) GLE %u\n",
1503 i, readArray[i]->h, bret, waitstatus, error);
1506 if (error != ERROR_BROKEN_PIPE)
1509 else if (waitstatus <= 0)
1511 GNUNET_CONTAINER_slist_add (handles_read,
1512 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1514 sizeof (struct GNUNET_DISK_FileHandle));
1516 LOG (GNUNET_ERROR_TYPE_DEBUG, "Added read Pipe 0x%x (0x%x)\n",
1517 readArray[i], readArray[i]->h);
1520 waitstatus = WaitForSingleObject (hEventWrite, 0);
1521 LOG (GNUNET_ERROR_TYPE_DEBUG, "Wait for the write event returned %d\n",
1523 if (waitstatus == WAIT_OBJECT_0)
1525 for (i = 0; i < wfds->sds.fd_count; i++)
1530 int sizeof_so_error = sizeof (so_error);
1532 getsockopt (wfds->sds.fd_array[i], SOL_SOCKET, SO_ERROR,
1533 (char *) &so_error, &sizeof_so_error);
1535 status = send (wfds->sds.fd_array[i], NULL, 0, 0);
1536 error = GetLastError ();
1537 LOG (GNUNET_ERROR_TYPE_DEBUG,
1538 "send to the socket %d returned %d (%u)\n", i, status, error);
1539 if (status == 0 || (error != WSAEWOULDBLOCK && error != WSAENOTCONN) ||
1540 (status == -1 && gso_result == 0 && error == WSAENOTCONN &&
1541 so_error == WSAECONNREFUSED))
1543 FD_SET (wfds->sds.fd_array[i], &awrite);
1550 if (!nhandles || (returnedpos >= nhandles))
1551 LOG (GNUNET_ERROR_TYPE_DEBUG, "Returning from _select() with nothing!\n");
1555 struct GNUNET_CONTAINER_SList_Iterator t;
1557 for (i = 0; i < rfds->sds.fd_count; i++)
1559 WSAEventSelect (rfds->sds.fd_array[i], hEventRead, 0);
1562 for (t = GNUNET_CONTAINER_slist_begin (rfds->handles);
1563 GNUNET_CONTAINER_slist_end (&t) != GNUNET_YES;
1564 GNUNET_CONTAINER_slist_next (&t))
1566 struct GNUNET_DISK_FileHandle *fh;
1568 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (&t,
1570 if (fh->type == GNUNET_PIPE)
1575 LOG (GNUNET_ERROR_TYPE_DEBUG, "Zeroing rfds\n");
1576 GNUNET_NETWORK_fdset_zero (rfds);
1577 if (retcode != -1 && nhandles && (returnedpos < nhandles))
1578 GNUNET_NETWORK_fdset_copy_native (rfds, &aread, retcode);
1579 GNUNET_CONTAINER_slist_append (rfds->handles, handles_read);
1583 for (i = 0; i < wfds->sds.fd_count; i++)
1585 WSAEventSelect (wfds->sds.fd_array[i], hEventWrite, 0);
1588 LOG (GNUNET_ERROR_TYPE_DEBUG, "Zeroing wfds\n");
1589 GNUNET_NETWORK_fdset_zero (wfds);
1590 if (retcode != -1 && nhandles && (returnedpos < nhandles))
1591 GNUNET_NETWORK_fdset_copy_native (wfds, &awrite, retcode);
1592 GNUNET_CONTAINER_slist_append (wfds->handles, handles_write);
1596 for (i = 0; i < efds->sds.fd_count; i++)
1598 WSAEventSelect (efds->sds.fd_array[i], hEventException, 0);
1601 LOG (GNUNET_ERROR_TYPE_DEBUG, "Zeroing efds\n");
1602 GNUNET_NETWORK_fdset_zero (efds);
1603 if (retcode != -1 && nhandles && (returnedpos < nhandles))
1604 GNUNET_NETWORK_fdset_copy_native (efds, &aexcept, retcode);
1605 GNUNET_CONTAINER_slist_append (efds->handles, handles_except);
1607 GNUNET_CONTAINER_slist_destroy (handles_read);
1608 GNUNET_CONTAINER_slist_destroy (handles_write);
1609 GNUNET_CONTAINER_slist_destroy (handles_except);
1613 struct GNUNET_CONTAINER_SList_Iterator t;
1615 for (i = 0; i < bread.fd_count; i++)
1617 if (bread.fd_array[i] != 0)
1618 LOG (GNUNET_ERROR_TYPE_DEBUG, "FD 0x%x is %s in rfds\n",
1620 (SAFE_FD_ISSET (bread.fd_array[i], rfds)) ? "SET" : "NOT SET");
1622 for (t = GNUNET_CONTAINER_slist_begin (rfds->handles);
1623 GNUNET_CONTAINER_slist_end (&t) != GNUNET_YES;
1624 GNUNET_CONTAINER_slist_next (&t))
1626 struct GNUNET_DISK_FileHandle *fh;
1628 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (&t,
1630 LOG (GNUNET_ERROR_TYPE_DEBUG, "FD 0x%x is SET in rfds\n", fh->h);
1635 for (i = 0; i < bwrite.fd_count; i++)
1637 if (bwrite.fd_array[i] != 0)
1638 LOG (GNUNET_ERROR_TYPE_DEBUG, "FD 0x%x is %s in wfds\n",
1640 (SAFE_FD_ISSET (bwrite.fd_array[i], rfds)) ? "SET" : "NOT SET");
1645 for (i = 0; i < bexcept.fd_count; i++)
1647 if (bexcept.fd_array[i] != 0)
1648 LOG (GNUNET_ERROR_TYPE_DEBUG, "FD 0x%x is %s in efds\n",
1649 bexcept.fd_array[i],
1650 (SAFE_FD_ISSET (bexcept.fd_array[i], rfds)) ? "SET" : "NOT SET");
1653 LOG (GNUNET_ERROR_TYPE_DEBUG, "Returning %d or 0\n", retcode);
1655 if (nhandles && (returnedpos < nhandles))
1662 /* end of network.c */