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)
185 setsockopt (h->fd, IPPROTO_TCP, TCP_NODELAY, &value, sizeof (value)))
186 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
188 const char *abs_value = "1";
191 setsockopt (h->fd, IPPROTO_TCP, TCP_NODELAY, abs_value,
193 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
199 * accept a new connection on a socket
201 * @param desc bound socket
202 * @param address address of the connecting peer, may be NULL
203 * @param address_len length of address
204 * @return client socket
206 struct GNUNET_NETWORK_Handle *
207 GNUNET_NETWORK_socket_accept (const struct GNUNET_NETWORK_Handle *desc,
208 struct sockaddr *address,
209 socklen_t * address_len)
211 struct GNUNET_NETWORK_Handle *ret;
213 ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle));
216 struct sockaddr name;
217 socklen_t namelen = sizeof (name);
218 int gsn = getsockname (desc->fd, &name, &namelen);
221 LOG (GNUNET_ERROR_TYPE_DEBUG, "Accepting connection on `%s'\n",
222 GNUNET_a2s (&name, namelen));
225 ret->fd = accept (desc->fd, address, address_len);
227 ret->af = address->sa_family;
230 if (ret->fd == INVALID_SOCKET)
233 SetErrnoFromWinsockError (WSAGetLastError ());
239 if (ret->fd >= FD_SETSIZE)
241 GNUNET_break (0 == close (ret->fd));
247 if (GNUNET_SYSERR == socket_set_blocking (ret, GNUNET_NO))
251 /* we might want to treat this one as fatal... */
253 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret));
258 if (GNUNET_OK != socket_set_inheritable (ret))
259 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
260 "socket_set_inheritable");
263 socket_set_nosigpipe (ret);
266 if (ret->af != AF_UNIX)
268 socket_set_nodelay (ret);
274 * Bind to a connected socket
276 * @param address address to be bound
277 * @param address_len length of address
278 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
281 GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc,
282 const struct sockaddr *address,
283 socklen_t address_len)
291 if (desc->af == AF_INET6)
293 setsockopt (desc->fd, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof (on)))
294 LOG_STRERROR (GNUNET_ERROR_TYPE_DEBUG, "setsockopt");
298 /* This is required, and required here, but only on UNIX */
299 if (0 != setsockopt (desc->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)))
300 LOG_STRERROR (GNUNET_ERROR_TYPE_DEBUG, "setsockopt");
304 if (address->sa_family == AF_UNIX)
306 const struct sockaddr_un *un = (const struct sockaddr_un *) address;
308 (void) unlink (un->sun_path);
312 ret = bind (desc->fd, address, address_len);
314 if (SOCKET_ERROR == ret)
315 SetErrnoFromWinsockError (WSAGetLastError ());
318 return GNUNET_SYSERR;
321 desc->addr = GNUNET_malloc (address_len);
322 memcpy (desc->addr, address, address_len);
323 desc->addrlen = address_len;
333 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
336 GNUNET_NETWORK_socket_close (struct GNUNET_NETWORK_Handle *desc)
344 LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_NETWORK_socket_close",
345 "Closing 0x%x\n", desc->fd);
348 ret = closesocket (desc->fd);
349 error = WSAGetLastError ();
350 SetErrnoFromWinsockError (error);
352 LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_NETWORK_socket_close",
353 "Closed 0x%x, closesocket() returned %d, GLE is %u\n",
354 desc->fd, ret, error);
357 ret = close (desc->fd);
361 if ((desc->af == AF_UNIX) && (NULL != desc->addr))
363 const struct sockaddr_un *un = (const struct sockaddr_un *) desc->addr;
365 if (0 != unlink (un->sun_path))
366 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "unlink", un->sun_path);
370 GNUNET_free_non_null (desc->addr);
372 return (ret == 0) ? GNUNET_OK : GNUNET_SYSERR;
377 * Box a native socket (and check that it is a socket).
379 * @param fd socket to box
380 * @return NULL on error (including not supported on target platform)
382 struct GNUNET_NETWORK_Handle *
383 GNUNET_NETWORK_socket_box_native (int fd)
388 struct GNUNET_NETWORK_Handle *ret;
390 if (fcntl (fd, F_GETFD) < 0)
391 return NULL; /* invalid FD */
392 ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle));
403 * @param address peer address
404 * @param address_len length of address
405 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
408 GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Handle *desc,
409 const struct sockaddr *address,
410 socklen_t address_len)
414 ret = connect (desc->fd, address, address_len);
417 if (SOCKET_ERROR == ret)
419 SetErrnoFromWinsockError (WSAGetLastError ());
420 if (errno == EWOULDBLOCK)
424 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
432 * @param level protocol level of the option
433 * @param optname identifier of the option
434 * @param optval options
435 * @param optlen length of optval
436 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
439 GNUNET_NETWORK_socket_getsockopt (const struct GNUNET_NETWORK_Handle *desc,
440 int level, int optname, void *optval,
445 ret = getsockopt (desc->fd, level, optname, optval, optlen);
448 if (ret == 0 && level == SOL_SOCKET && optname == SO_ERROR)
449 *((int *) optval) = GetErrnoFromWinsockError (*((int *) optval));
451 else if (SOCKET_ERROR == ret)
452 SetErrnoFromWinsockError (WSAGetLastError ());
454 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
461 * @param backlog length of the listen queue
462 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
465 GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Handle *desc,
470 ret = listen (desc->fd, backlog);
473 if (SOCKET_ERROR == ret)
474 SetErrnoFromWinsockError (WSAGetLastError ());
477 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
482 * How much data is available to be read on this descriptor?
484 * Returns GNUNET_NO if no data is available, or on error!
488 GNUNET_NETWORK_socket_recvfrom_amount (const struct GNUNET_NETWORK_Handle *
493 /* How much is there to be read? */
497 error = ioctl (desc->fd, FIONREAD, &pending);
502 error = ioctlsocket (desc->fd, FIONREAD, &pending);
503 if (error != SOCKET_ERROR)
512 * Read data from a connected socket (always non-blocking).
514 * @param buffer buffer
515 * @param length length of buffer
516 * @param src_addr either the source to recv from, or all zeroes
517 * to be filled in by recvfrom
518 * @param addrlen length of the addr
521 GNUNET_NETWORK_socket_recvfrom (const struct GNUNET_NETWORK_Handle * desc,
522 void *buffer, size_t length,
523 struct sockaddr * src_addr,
532 flags |= MSG_DONTWAIT;
535 ret = recvfrom (desc->fd, buffer, length, flags, src_addr, addrlen);
537 if (SOCKET_ERROR == ret)
538 SetErrnoFromWinsockError (WSAGetLastError ());
545 * Read data from a connected socket (always non-blocking).
547 * @param buffer buffer
548 * @param length length of buffer
551 GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Handle * desc,
552 void *buffer, size_t length)
560 flags |= MSG_DONTWAIT;
562 ret = recv (desc->fd, buffer, length, flags);
564 if (SOCKET_ERROR == ret)
565 SetErrnoFromWinsockError (WSAGetLastError ());
572 * Send data (always non-blocking).
575 * @param buffer data to send
576 * @param length size of the buffer
577 * @return number of bytes sent, GNUNET_SYSERR on error
580 GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Handle * desc,
581 const void *buffer, size_t length)
589 flags |= MSG_DONTWAIT;
593 flags |= MSG_NOSIGNAL;
596 ret = send (desc->fd, buffer, length, flags);
599 if (SOCKET_ERROR == ret)
600 SetErrnoFromWinsockError (WSAGetLastError ());
608 * Send data to a particular destination (always non-blocking).
609 * This function only works for UDP sockets.
612 * @param message data to send
613 * @param length size of the data
614 * @param dest_addr destination address
615 * @param dest_len length of address
616 * @return number of bytes sent, GNUNET_SYSERR on error
619 GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Handle * desc,
620 const void *message, size_t length,
621 const struct sockaddr * dest_addr,
630 flags |= MSG_DONTWAIT;
633 flags |= MSG_NOSIGNAL;
635 ret = sendto (desc->fd, message, length, flags, dest_addr, dest_len);
637 if (SOCKET_ERROR == ret)
638 SetErrnoFromWinsockError (WSAGetLastError ());
647 * @param level protocol level of the option
648 * @param option_name option identifier
649 * @param option_value value to set
650 * @param option_len size of option_value
651 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
654 GNUNET_NETWORK_socket_setsockopt (struct GNUNET_NETWORK_Handle *fd, int level,
655 int option_name, const void *option_value,
656 socklen_t option_len)
660 ret = setsockopt (fd->fd, level, option_name, option_value, option_len);
662 if (SOCKET_ERROR == ret)
663 SetErrnoFromWinsockError (WSAGetLastError ());
665 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
670 * Create a new socket. Configure it for non-blocking IO and
671 * mark it as non-inheritable to child processes (set the
672 * close-on-exec flag).
674 * @param domain domain of the socket
675 * @param type socket type
676 * @param protocol network protocol
677 * @return new socket, NULL on error
679 struct GNUNET_NETWORK_Handle *
680 GNUNET_NETWORK_socket_create (int domain, int type, int protocol)
682 struct GNUNET_NETWORK_Handle *ret;
684 ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle));
686 ret->fd = socket (domain, type, protocol);
687 if (INVALID_SOCKET == ret->fd)
690 SetErrnoFromWinsockError (WSAGetLastError ());
697 if (ret->fd >= FD_SETSIZE)
699 GNUNET_break (0 == close (ret->fd));
706 if (GNUNET_SYSERR == socket_set_blocking (ret, GNUNET_NO))
708 /* we might want to treat this one as fatal... */
710 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret));
715 if (GNUNET_OK != socket_set_inheritable (ret))
716 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
717 "socket_set_inheritable");
720 socket_set_nosigpipe (ret);
722 if ((type == SOCK_STREAM)
724 && (domain != AF_UNIX)
727 socket_set_nodelay (ret);
733 * Shut down socket operations
735 * @param how type of shutdown
736 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
739 GNUNET_NETWORK_socket_shutdown (struct GNUNET_NETWORK_Handle *desc, int how)
743 ret = shutdown (desc->fd, how);
746 SetErrnoFromWinsockError (WSAGetLastError ());
748 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
753 * Disable the "CORK" feature for communication with the given socket,
754 * forcing the OS to immediately flush the buffer on transmission
755 * instead of potentially buffering multiple messages. Essentially
756 * reduces the OS send buffers to zero.
759 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
762 GNUNET_NETWORK_socket_disable_corking (struct GNUNET_NETWORK_Handle *desc)
770 setsockopt (desc->fd, SOL_SOCKET, SO_SNDBUF, (char *) &value,
772 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
775 setsockopt (desc->fd, SOL_SOCKET, SO_RCVBUF, (char *) &value,
777 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
783 setsockopt (desc->fd, SOL_SOCKET, SO_SNDBUF, &value, sizeof (value))))
784 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
787 setsockopt (desc->fd, SOL_SOCKET, SO_RCVBUF, &value, sizeof (value))))
788 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
790 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
799 GNUNET_NETWORK_fdset_zero (struct GNUNET_NETWORK_FDSet *fds)
804 GNUNET_CONTAINER_slist_clear (fds->handles);
809 * Add a socket to the FD set
811 * @param desc socket to add
814 GNUNET_NETWORK_fdset_set (struct GNUNET_NETWORK_FDSet *fds,
815 const struct GNUNET_NETWORK_Handle *desc)
817 FD_SET (desc->fd, &fds->sds);
818 if (desc->fd + 1 > fds->nsds)
819 fds->nsds = desc->fd + 1;
824 * Check whether a socket is part of the fd set
827 * @return 0 if the FD is not set
830 GNUNET_NETWORK_fdset_isset (const struct GNUNET_NETWORK_FDSet *fds,
831 const struct GNUNET_NETWORK_Handle *desc)
833 return FD_ISSET (desc->fd, &fds->sds);
838 * Add one fd set to another
839 * @param dst the fd set to add to
840 * @param src the fd set to add from
843 GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst,
844 const struct GNUNET_NETWORK_FDSet *src)
848 for (nfds = src->nsds; nfds > 0; nfds--)
849 if (FD_ISSET (nfds, &src->sds))
852 FD_SET (nfds, &dst->sds);
853 if (nfds + 1 > dst->nsds)
854 dst->nsds = nfds + 1;
857 GNUNET_CONTAINER_slist_append (dst->handles, src->handles);
863 * Copy one fd set to another
865 * @param to destination
869 GNUNET_NETWORK_fdset_copy (struct GNUNET_NETWORK_FDSet *to,
870 const struct GNUNET_NETWORK_FDSet *from)
872 FD_COPY (&from->sds, &to->sds);
873 to->nsds = from->nsds;
876 GNUNET_CONTAINER_slist_clear (to->handles);
877 GNUNET_CONTAINER_slist_append (to->handles, from->handles);
883 * Return file descriptor for this network handle
885 * @param desc wrapper to process
886 * @return POSIX file descriptor
889 GNUNET_NETWORK_get_fd (struct GNUNET_NETWORK_Handle *desc)
896 * Copy a native fd set
898 * @param to destination
899 * @param from native source set
900 * @param nfds the biggest socket number in from + 1
903 GNUNET_NETWORK_fdset_copy_native (struct GNUNET_NETWORK_FDSet *to,
904 const fd_set * from, int nfds)
906 FD_COPY (from, &to->sds);
912 * Set a native fd in a set
914 * @param to destination
915 * @param nfd native FD to set
918 GNUNET_NETWORK_fdset_set_native (struct GNUNET_NETWORK_FDSet *to, int nfd)
920 GNUNET_assert ((nfd >= 0) && (nfd < FD_SETSIZE));
921 FD_SET (nfd, &to->sds);
922 to->nsds = GNUNET_MAX (nfd + 1, to->nsds);
927 * Test native fd in a set
929 * @param to set to test, NULL for empty set
930 * @param nfd native FD to test, or -1 for none
931 * @return GNUNET_YES if FD is set in the set
934 GNUNET_NETWORK_fdset_test_native (const struct GNUNET_NETWORK_FDSet *to,
937 if ((nfd == -1) || (to == NULL))
939 return FD_ISSET (nfd, &to->sds) ? GNUNET_YES : GNUNET_NO;
944 * Add a file handle to the fd set
946 * @param h the file handle to add
949 GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds,
950 const struct GNUNET_DISK_FileHandle *h)
953 GNUNET_CONTAINER_slist_add (fds->handles,
954 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT, h,
955 sizeof (struct GNUNET_DISK_FileHandle));
960 GNUNET_DISK_internal_file_handle_ (h, &fd, sizeof (int));
961 FD_SET (fd, &fds->sds);
962 if (fd + 1 > fds->nsds)
970 * Check if a file handle is part of an fd set
972 * @param h file handle
973 * @return GNUNET_YES if the file handle is part of the set
976 GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds,
977 const struct GNUNET_DISK_FileHandle *h)
981 return GNUNET_CONTAINER_slist_contains (fds->handles, h,
983 GNUNET_DISK_FileHandle));
985 return FD_ISSET (h->fd, &fds->sds);
991 * Checks if two fd sets overlap
992 * @param fds1 first fd set
993 * @param fds2 second fd set
994 * @return GNUNET_YES if they do overlap, GNUNET_NO otherwise
997 GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1,
998 const struct GNUNET_NETWORK_FDSet *fds2)
1004 if (nfds > fds2->nsds)
1009 if (FD_ISSET (nfds, &fds1->sds) && FD_ISSET (nfds, &fds2->sds))
1013 struct GNUNET_CONTAINER_SList_Iterator it;
1014 struct GNUNET_DISK_FileHandle *h;
1018 /*This code is somewhat hacky, we are not supposed to know what's
1019 * inside of fd_set; also the O(n^2) is really bad... */
1021 for (i = 0; i < fds1->sds.fd_count; i++)
1023 for (j = 0; j < fds2->sds.fd_count; j++)
1025 if (fds1->sds.fd_array[i] == fds2->sds.fd_array[j])
1029 it = GNUNET_CONTAINER_slist_begin (fds1->handles);
1030 while (GNUNET_CONTAINER_slist_end (&it) != GNUNET_YES)
1033 struct GNUNET_CONTAINER_SList_Iterator t;
1036 (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (&it,
1039 LOG (GNUNET_ERROR_TYPE_DEBUG,
1040 "Checking that FD 0x%x is in another set:\n", h->h);
1041 for (t = GNUNET_CONTAINER_slist_begin (fds2->handles);
1042 GNUNET_CONTAINER_slist_end (&t) != GNUNET_YES;
1043 GNUNET_CONTAINER_slist_next (&t))
1045 struct GNUNET_DISK_FileHandle *fh;
1048 (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (&t,
1050 LOG (GNUNET_ERROR_TYPE_DEBUG, "0x%x\n", fh->h);
1053 if (GNUNET_CONTAINER_slist_contains
1054 (fds2->handles, h, sizeof (struct GNUNET_DISK_FileHandle)))
1057 LOG (GNUNET_ERROR_TYPE_DEBUG, "Match!\n");
1061 GNUNET_CONTAINER_slist_next (&it);
1070 * @return a new fd set
1072 struct GNUNET_NETWORK_FDSet *
1073 GNUNET_NETWORK_fdset_create ()
1075 struct GNUNET_NETWORK_FDSet *fds;
1077 fds = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_FDSet));
1079 fds->handles = GNUNET_CONTAINER_slist_create ();
1081 GNUNET_NETWORK_fdset_zero (fds);
1087 * Releases the associated memory of an fd set
1091 GNUNET_NETWORK_fdset_destroy (struct GNUNET_NETWORK_FDSet *fds)
1094 GNUNET_CONTAINER_slist_destroy (fds->handles);
1100 * Check if sockets meet certain conditions
1101 * @param rfds set of sockets to be checked for readability
1102 * @param wfds set of sockets to be checked for writability
1103 * @param efds set of sockets to be checked for exceptions
1104 * @param timeout relative value when to return
1105 * @return number of selected sockets, GNUNET_SYSERR on error
1108 GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
1109 struct GNUNET_NETWORK_FDSet *wfds,
1110 struct GNUNET_NETWORK_FDSet *efds,
1111 const struct GNUNET_TIME_Relative timeout)
1118 int read_handles = 0;
1119 int write_handles = 0;
1125 int nsock = 0, nhandles = 0, nSockEvents = 0;
1127 static HANDLE hEventRead = 0;
1128 static HANDLE hEventWrite = 0;
1129 static HANDLE hEventException = 0;
1130 static HANDLE hEventPipeWrite = 0;
1131 static HANDLE hEventReadReady = 0;
1134 int writePipePos = 0;
1136 HANDLE handle_array[FD_SETSIZE + 2];
1137 int returncode = -1;
1138 DWORD newretcode = 0;
1139 int returnedpos = 0;
1141 struct GNUNET_CONTAINER_SList *handles_read, *handles_write,
1144 fd_set aread, awrite, aexcept;
1147 fd_set bread, bwrite, bexcept;
1150 /* TODO: Make this growable */
1151 struct GNUNET_DISK_FileHandle *readArray[50];
1159 handles += read_handles = GNUNET_CONTAINER_slist_count (rfds->handles);
1162 struct GNUNET_CONTAINER_SList_Iterator t;
1164 for (t = GNUNET_CONTAINER_slist_begin (rfds->handles);
1165 GNUNET_CONTAINER_slist_end (&t) != GNUNET_YES;
1166 GNUNET_CONTAINER_slist_next (&t))
1168 struct GNUNET_DISK_FileHandle *fh;
1171 (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",
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;
1300 (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (&i,
1302 if (fh->type == GNUNET_PIPE)
1304 /* Read zero bytes to check the status of the pipe */
1306 LOG (GNUNET_ERROR_TYPE_DEBUG,
1307 "Reading 0 bytes from the pipe 0x%x\n", fh->h);
1309 if (!ReadFile (fh->h, NULL, 0, NULL, fh->oOverlapRead))
1311 DWORD error_code = GetLastError ();
1313 if (error_code == ERROR_IO_PENDING)
1316 LOG (GNUNET_ERROR_TYPE_DEBUG,
1317 "Adding the pipe's 0x%x overlapped event to the array as %d\n",
1320 handle_array[nhandles++] = fh->oOverlapRead->hEvent;
1321 readArray[readPipes++] = fh;
1326 * SetErrnoFromWinError (error_code);
1333 LOG (GNUNET_ERROR_TYPE_DEBUG,
1334 "Adding the read ready event to the array as %d\n",
1337 handle_array[nhandles++] = hEventReadReady;
1338 readArray[readPipes++] = fh;
1343 GNUNET_CONTAINER_slist_add (handles_read,
1344 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1347 GNUNET_DISK_FileHandle));
1351 if (wfds && write_handles)
1354 LOG (GNUNET_ERROR_TYPE_DEBUG,
1355 "Adding the write ready event to the array as %d\n", nhandles);
1357 handle_array[nhandles++] = hEventPipeWrite;
1358 writePipePos = nhandles;
1360 if (efds && ex_handles)
1362 struct GNUNET_CONTAINER_SList_Iterator i;
1364 for (i = GNUNET_CONTAINER_slist_begin (efds->handles);
1365 GNUNET_CONTAINER_slist_end (&i) != GNUNET_YES;
1366 GNUNET_CONTAINER_slist_next (&i))
1368 struct GNUNET_DISK_FileHandle *fh;
1372 (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (&i,
1374 if (fh->type == GNUNET_PIPE)
1376 if (!PeekNamedPipe (fh->h, NULL, 0, NULL, &dwBytes, NULL))
1378 GNUNET_CONTAINER_slist_add (handles_except,
1379 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1382 GNUNET_DISK_FileHandle));
1393 LOG (GNUNET_ERROR_TYPE_DEBUG,
1394 "Adding the socket read event to the array as %d\n", nhandles);
1396 handle_array[nhandles++] = hEventRead;
1398 for (i = 0; i < rfds->sds.fd_count; i++)
1400 WSAEventSelect (rfds->sds.fd_array[i], hEventRead,
1401 FD_ACCEPT | FD_READ | FD_CLOSE);
1410 LOG (GNUNET_ERROR_TYPE_DEBUG,
1411 "Adding the socket write event to the array as %d\n",
1414 handle_array[nhandles++] = hEventWrite;
1416 for (i = 0; i < wfds->sds.fd_count; i++)
1421 status = send (wfds->sds.fd_array[i], NULL, 0, 0);
1422 error = GetLastError ();
1424 LOG (GNUNET_ERROR_TYPE_DEBUG,
1425 "pre-send to the socket %d returned %d (%u)\n", i, status,
1429 || (error != WSAEWOULDBLOCK && error != WSAENOTCONN))
1431 WSAEventSelect (wfds->sds.fd_array[i], hEventWrite,
1432 FD_WRITE | FD_CONNECT | FD_CLOSE);
1436 SetEvent (hEventWrite);
1441 LOG (GNUNET_ERROR_TYPE_DEBUG,
1442 "Adding the socket error event to the array as %d\n",
1445 handle_array[nhandles++] = hEventException;
1447 for (i = 0; i < efds->sds.fd_count; i++)
1449 WSAEventSelect (efds->sds.fd_array[i], hEventException,
1456 handle_array[nhandles] = NULL;
1459 LOG (GNUNET_ERROR_TYPE_DEBUG, "Number nfds : %d\n", nfds);
1460 LOG (GNUNET_ERROR_TYPE_DEBUG, "Number of handles : %d\n", nhandles);
1461 LOG (GNUNET_ERROR_TYPE_DEBUG, "retcode : %d\n", newretcode);
1462 LOG (GNUNET_ERROR_TYPE_DEBUG, "Will wait : %d\n", ms_total);
1467 WaitForMultipleObjects (nhandles, handle_array, FALSE, ms_total);
1469 LOG (GNUNET_ERROR_TYPE_DEBUG, "WaitForMultipleObjects Returned : %d\n",
1473 returnedpos = returncode - WAIT_OBJECT_0;
1475 LOG (GNUNET_ERROR_TYPE_DEBUG, "return pos is : %d\n", returnedpos);
1478 /* FIXME: THIS LINE IS WRONG !! We should add to handles only handles that fired the events, not all ! */
1481 * GNUNET_CONTAINER_slist_append (handles_read, rfds->handles);
1483 if (nhandles && (returnedpos < nhandles))
1490 struct timeval tvslice;
1493 tvslice.tv_usec = 10;
1494 retcode = select (nfds, &aread, &awrite, &aexcept, &tvslice);
1498 LOG (GNUNET_ERROR_TYPE_DEBUG, "Select retcode : %d\n", retcode);
1501 /* FIXME: <= writePipePos? Really? */
1502 if ((writePipePos != -1) && (returnedpos <= writePipePos))
1504 GNUNET_CONTAINER_slist_append (handles_write, wfds->handles);
1505 retcode += write_handles;
1507 LOG (GNUNET_ERROR_TYPE_DEBUG, "Added write pipe\n");
1511 LOG (GNUNET_ERROR_TYPE_DEBUG, "ReadPipes is : %d\n", readPipes);
1513 /* We have some pipes ready for read. */
1514 /* FIXME: it is supposed to work !! Only choose the Pipes who fired the event, but it is not working */
1516 if (returnedpos < readPipes)
1519 * for (i = 0; i < readPipes; i++)
1521 * waitstatus = WaitForSingleObject (handle_array[i], 0);
1522 * LOG (GNUNET_ERROR_TYPE_DEBUG, "Read pipe %d wait status is : %d\n", i, waitstatus);
1523 * if (waitstatus != WAIT_OBJECT_0)
1525 * GNUNET_CONTAINER_slist_add (handles_read,
1526 * GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1527 * readArray[i], sizeof (struct GNUNET_DISK_FileHandle));
1529 * LOG (GNUNET_ERROR_TYPE_DEBUG, "Added read Pipe\n");
1532 for (i = 0; i < readPipes; i++)
1540 PeekNamedPipe (readArray[i]->h, NULL, 0, NULL, &waitstatus,
1542 error = GetLastError ();
1544 LOG (GNUNET_ERROR_TYPE_DEBUG,
1545 "Peek at read pipe %d (0x%x) returned %d (%d bytes available) GLE %u\n",
1546 i, readArray[i]->h, bret, waitstatus, error);
1550 if (error != ERROR_BROKEN_PIPE)
1553 else if (waitstatus <= 0)
1555 GNUNET_CONTAINER_slist_add (handles_read,
1556 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1559 GNUNET_DISK_FileHandle));
1562 LOG (GNUNET_ERROR_TYPE_DEBUG, "Added read Pipe 0x%x (0x%x)\n",
1563 readArray[i], readArray[i]->h);
1567 waitstatus = WaitForSingleObject (hEventWrite, 0);
1569 LOG (GNUNET_ERROR_TYPE_DEBUG,
1570 "Wait for the write event returned %d\n", waitstatus);
1572 if (waitstatus == WAIT_OBJECT_0)
1574 for (i = 0; i < wfds->sds.fd_count; i++)
1579 int sizeof_so_error = sizeof (so_error);
1581 getsockopt (wfds->sds.fd_array[i], SOL_SOCKET, SO_ERROR,
1582 (char *) &so_error, &sizeof_so_error);
1584 status = send (wfds->sds.fd_array[i], NULL, 0, 0);
1585 error = GetLastError ();
1587 LOG (GNUNET_ERROR_TYPE_DEBUG,
1588 "send to the socket %d returned %d (%u)\n", i, status,
1592 || (error != WSAEWOULDBLOCK && error != WSAENOTCONN)
1593 || (status == -1 && gso_result == 0 && error == WSAENOTCONN
1594 && so_error == WSAECONNREFUSED))
1596 FD_SET (wfds->sds.fd_array[i], &awrite);
1603 if (!nhandles || (returnedpos >= nhandles))
1604 LOG (GNUNET_ERROR_TYPE_DEBUG, "Returning from _select() with nothing!\n");
1608 struct GNUNET_CONTAINER_SList_Iterator t;
1610 for (i = 0; i < rfds->sds.fd_count; i++)
1612 WSAEventSelect (rfds->sds.fd_array[i], hEventRead, 0);
1615 for (t = GNUNET_CONTAINER_slist_begin (rfds->handles);
1616 GNUNET_CONTAINER_slist_end (&t) != GNUNET_YES;
1617 GNUNET_CONTAINER_slist_next (&t))
1619 struct GNUNET_DISK_FileHandle *fh;
1622 (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (&t,
1624 if (fh->type == GNUNET_PIPE)
1630 LOG (GNUNET_ERROR_TYPE_DEBUG, "Zeroing rfds\n");
1632 GNUNET_NETWORK_fdset_zero (rfds);
1633 if (retcode != -1 && nhandles && (returnedpos < nhandles))
1634 GNUNET_NETWORK_fdset_copy_native (rfds, &aread, retcode);
1635 GNUNET_CONTAINER_slist_append (rfds->handles, handles_read);
1639 for (i = 0; i < wfds->sds.fd_count; i++)
1641 WSAEventSelect (wfds->sds.fd_array[i], hEventWrite, 0);
1645 LOG (GNUNET_ERROR_TYPE_DEBUG, "Zeroing wfds\n");
1647 GNUNET_NETWORK_fdset_zero (wfds);
1648 if (retcode != -1 && nhandles && (returnedpos < nhandles))
1649 GNUNET_NETWORK_fdset_copy_native (wfds, &awrite, retcode);
1650 GNUNET_CONTAINER_slist_append (wfds->handles, handles_write);
1654 for (i = 0; i < efds->sds.fd_count; i++)
1656 WSAEventSelect (efds->sds.fd_array[i], hEventException, 0);
1660 LOG (GNUNET_ERROR_TYPE_DEBUG, "Zeroing efds\n");
1662 GNUNET_NETWORK_fdset_zero (efds);
1663 if (retcode != -1 && nhandles && (returnedpos < nhandles))
1664 GNUNET_NETWORK_fdset_copy_native (efds, &aexcept, retcode);
1665 GNUNET_CONTAINER_slist_append (efds->handles, handles_except);
1667 GNUNET_CONTAINER_slist_destroy (handles_read);
1668 GNUNET_CONTAINER_slist_destroy (handles_write);
1669 GNUNET_CONTAINER_slist_destroy (handles_except);
1673 struct GNUNET_CONTAINER_SList_Iterator t;
1675 for (i = 0; i < bread.fd_count; i++)
1677 if (bread.fd_array[i] != 0)
1678 LOG (GNUNET_ERROR_TYPE_DEBUG, "FD 0x%x is %s in rfds\n",
1680 (SAFE_FD_ISSET (bread.fd_array[i], rfds)) ? "SET" :
1683 for (t = GNUNET_CONTAINER_slist_begin (rfds->handles);
1684 GNUNET_CONTAINER_slist_end (&t) != GNUNET_YES;
1685 GNUNET_CONTAINER_slist_next (&t))
1687 struct GNUNET_DISK_FileHandle *fh;
1690 (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (&t,
1692 LOG (GNUNET_ERROR_TYPE_DEBUG, "FD 0x%x is SET in rfds\n", fh->h);
1697 for (i = 0; i < bwrite.fd_count; i++)
1699 if (bwrite.fd_array[i] != 0)
1700 LOG (GNUNET_ERROR_TYPE_DEBUG, "FD 0x%x is %s in wfds\n",
1702 (SAFE_FD_ISSET (bwrite.fd_array[i], rfds)) ? "SET" :
1708 for (i = 0; i < bexcept.fd_count; i++)
1710 if (bexcept.fd_array[i] != 0)
1711 LOG (GNUNET_ERROR_TYPE_DEBUG, "FD 0x%x is %s in efds\n",
1712 bexcept.fd_array[i],
1713 (SAFE_FD_ISSET (bexcept.fd_array[i], rfds)) ? "SET" :
1717 LOG (GNUNET_ERROR_TYPE_DEBUG, "Returning %d or 0\n", retcode);
1719 if (nhandles && (returnedpos < nhandles))
1726 /* end of network.c */