2 This file is part of GNUnet.
3 (C) 2009 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 2, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file util/network.c
23 * @brief basic, low-level networking interface
28 #include "gnunet_disk_lib.h"
30 #include "gnunet_container_lib.h"
32 #define DEBUG_NETWORK GNUNET_NO
34 #define DEBUG_W32_CYCLES GNUNET_NO
36 #ifndef INVALID_SOCKET
37 #define INVALID_SOCKET -1
41 struct GNUNET_NETWORK_Handle
51 * Address family / domain.
56 * Number of bytes in addr.
61 * Address we were bound to, or NULL.
63 struct sockaddr *addr;
69 #define FD_COPY(s, d) (memcpy ((d), (s), sizeof (fd_set)))
74 * Set if a socket should use blocking or non-blocking IO.
76 * @param doBlock blocking mode
77 * @return GNUNET_OK on success, GNUNET_SYSERR on error
80 socket_set_blocking (struct GNUNET_NETWORK_Handle *fd, int doBlock)
87 if (ioctlsocket (fd->fd, FIONBIO, &mode) == SOCKET_ERROR)
90 SetErrnoFromWinsockError (WSAGetLastError ());
91 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "ioctlsocket");
98 int flags = fcntl (fd->fd, F_GETFL);
103 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "fcntl");
104 return GNUNET_SYSERR;
107 flags &= ~O_NONBLOCK;
111 if (0 != fcntl (fd->fd, F_SETFL, flags))
114 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "fcntl");
115 return GNUNET_SYSERR;
124 * Make a socket non-inheritable to child processes
126 * @param h the socket to make non-inheritable
127 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
128 * @warning Not implemented on Windows
131 socket_set_inheritable (const struct GNUNET_NETWORK_Handle *h)
135 i = fcntl (h->fd, F_GETFD);
137 return GNUNET_SYSERR;
138 if (i == (i | FD_CLOEXEC))
141 if (fcntl (h->fd, F_SETFD, i) < 0)
142 return GNUNET_SYSERR;
150 * The MSG_NOSIGNAL equivalent on Mac OS X
152 * @param h the socket to make non-delaying
155 socket_set_nosigpipe (const struct GNUNET_NETWORK_Handle *h)
160 setsockopt (h->fd, SOL_SOCKET, SO_NOSIGPIPE, &abs_value,
162 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
168 * Disable delays when sending data via the socket.
169 * (GNUnet makes sure that messages are as big as
172 * @param h the socket to make non-delaying
175 socket_set_nodelay (const struct GNUNET_NETWORK_Handle *h)
180 if (0 != setsockopt (h->fd, IPPROTO_TCP, TCP_NODELAY, &value, sizeof (value)))
181 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
183 const char *abs_value = "1";
186 setsockopt (h->fd, IPPROTO_TCP, TCP_NODELAY, abs_value,
188 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
194 * accept a new connection on a socket
196 * @param desc bound socket
197 * @param address address of the connecting peer, may be NULL
198 * @param address_len length of address
199 * @return client socket
201 struct GNUNET_NETWORK_Handle *
202 GNUNET_NETWORK_socket_accept (const struct GNUNET_NETWORK_Handle *desc,
203 struct sockaddr *address, socklen_t * address_len)
205 struct GNUNET_NETWORK_Handle *ret;
207 ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle));
210 struct sockaddr name;
211 int namelen = sizeof (name);
212 int gsn = getsockname (desc->fd, &name, &namelen);
215 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Accepting connection on `%s'\n",
216 GNUNET_a2s (&name, namelen));
219 ret->fd = accept (desc->fd, address, address_len);
221 ret->af = address->sa_family;
224 if (ret->fd == INVALID_SOCKET)
227 SetErrnoFromWinsockError (WSAGetLastError ());
233 if (ret->fd >= FD_SETSIZE)
235 GNUNET_break (0 == close (ret->fd));
241 if (GNUNET_SYSERR == socket_set_blocking (ret, GNUNET_NO))
245 /* we might want to treat this one as fatal... */
247 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret));
252 if (GNUNET_OK != socket_set_inheritable (ret))
253 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
254 "socket_set_inheritable");
257 socket_set_nosigpipe (ret);
260 if (ret->af != AF_UNIX)
262 socket_set_nodelay (ret);
268 * Bind to a connected socket
270 * @param address address to be bound
271 * @param address_len length of address
272 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
275 GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc,
276 const struct sockaddr *address,
277 socklen_t address_len)
285 if (desc->af == AF_INET6)
286 if (0 != setsockopt (desc->fd, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof (on)))
287 GNUNET_log_strerror (GNUNET_ERROR_TYPE_DEBUG, "setsockopt");
289 /* is this needed or desired? or done elsewhere? */
290 if (0 != setsockopt (desc->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)))
291 GNUNET_log_strerror (GNUNET_ERROR_TYPE_DEBUG, "setsockopt");
297 if (address->sa_family == AF_UNIX)
299 const struct sockaddr_un *un = (const struct sockaddr_un *) address;
301 (void) unlink (un->sun_path);
305 ret = bind (desc->fd, address, address_len);
307 if (SOCKET_ERROR == ret)
308 SetErrnoFromWinsockError (WSAGetLastError ());
311 return GNUNET_SYSERR;
314 desc->addr = GNUNET_malloc (address_len);
315 memcpy (desc->addr, address, address_len);
316 desc->addrlen = address_len;
326 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
329 GNUNET_NETWORK_socket_close (struct GNUNET_NETWORK_Handle *desc)
337 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_NETWORK_socket_close",
338 "Closing 0x%x\n", desc->fd);
341 ret = closesocket (desc->fd);
342 error = WSAGetLastError ();
343 SetErrnoFromWinsockError (error);
345 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_NETWORK_socket_close",
346 "Closed 0x%x, closesocket() returned %d, GLE is %u\n",
347 desc->fd, ret, error);
350 ret = close (desc->fd);
354 if ((desc->af == AF_UNIX) && (NULL != desc->addr))
356 const struct sockaddr_un *un = (const struct sockaddr_un *) desc->addr;
358 if (0 != unlink (un->sun_path))
359 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
364 GNUNET_free_non_null (desc->addr);
366 return (ret == 0) ? GNUNET_OK : GNUNET_SYSERR;
371 * Box a native socket (and check that it is a socket).
373 * @param fd socket to box
374 * @return NULL on error (including not supported on target platform)
376 struct GNUNET_NETWORK_Handle *
377 GNUNET_NETWORK_socket_box_native (int fd)
382 struct GNUNET_NETWORK_Handle *ret;
384 if (fcntl (fd, F_GETFD) < 0)
385 return NULL; /* invalid FD */
386 ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle));
397 * @param address peer address
398 * @param address_len length of address
399 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
402 GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Handle *desc,
403 const struct sockaddr *address,
404 socklen_t address_len)
408 ret = connect (desc->fd, address, address_len);
411 if (SOCKET_ERROR == ret)
413 SetErrnoFromWinsockError (WSAGetLastError ());
414 if (errno == EWOULDBLOCK)
418 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
426 * @param level protocol level of the option
427 * @param optname identifier of the option
428 * @param optval options
429 * @param optlen length of optval
430 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
433 GNUNET_NETWORK_socket_getsockopt (const struct GNUNET_NETWORK_Handle *desc,
434 int level, int optname, void *optval,
439 ret = getsockopt (desc->fd, level, optname, optval, optlen);
442 if (ret == 0 && level == SOL_SOCKET && optname == SO_ERROR)
443 *((int *) optval) = GetErrnoFromWinsockError (*((int *) optval));
445 else if (SOCKET_ERROR == ret)
446 SetErrnoFromWinsockError (WSAGetLastError ());
448 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
455 * @param backlog length of the listen queue
456 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
459 GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Handle *desc,
464 ret = listen (desc->fd, backlog);
467 if (SOCKET_ERROR == ret)
468 SetErrnoFromWinsockError (WSAGetLastError ());
471 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
476 * How much data is available to be read on this descriptor?
478 * Returns GNUNET_NO if no data is available, or on error!
482 GNUNET_NETWORK_socket_recvfrom_amount (const struct GNUNET_NETWORK_Handle *
487 /* How much is there to be read? */
491 error = ioctl (desc->fd, FIONREAD, &pending);
496 error = ioctlsocket (desc->fd, FIONREAD, &pending);
497 if (error != SOCKET_ERROR)
506 * Read data from a connected socket (always non-blocking).
508 * @param buffer buffer
509 * @param length length of buffer
510 * @param src_addr either the source to recv from, or all zeroes
511 * to be filled in by recvfrom
512 * @param addrlen length of the addr
515 GNUNET_NETWORK_socket_recvfrom (const struct GNUNET_NETWORK_Handle * desc,
516 void *buffer, size_t length,
517 struct sockaddr * src_addr, socklen_t * addrlen)
525 flags |= MSG_DONTWAIT;
528 ret = recvfrom (desc->fd, buffer, length, flags, src_addr, addrlen);
530 if (SOCKET_ERROR == ret)
531 SetErrnoFromWinsockError (WSAGetLastError ());
538 * Read data from a connected socket (always non-blocking).
540 * @param buffer buffer
541 * @param length length of buffer
544 GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Handle * desc,
545 void *buffer, size_t length)
553 flags |= MSG_DONTWAIT;
555 ret = recv (desc->fd, buffer, length, flags);
557 if (SOCKET_ERROR == ret)
558 SetErrnoFromWinsockError (WSAGetLastError ());
565 * Send data (always non-blocking).
568 * @param buffer data to send
569 * @param length size of the buffer
570 * @return number of bytes sent, GNUNET_SYSERR on error
573 GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Handle * desc,
574 const void *buffer, size_t length)
582 flags |= MSG_DONTWAIT;
586 flags |= MSG_NOSIGNAL;
589 ret = send (desc->fd, buffer, length, flags);
592 if (SOCKET_ERROR == ret)
593 SetErrnoFromWinsockError (WSAGetLastError ());
601 * Send data to a particular destination (always non-blocking).
602 * This function only works for UDP sockets.
605 * @param message data to send
606 * @param length size of the data
607 * @param dest_addr destination address
608 * @param dest_len length of address
609 * @return number of bytes sent, GNUNET_SYSERR on error
612 GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Handle * desc,
613 const void *message, size_t length,
614 const struct sockaddr * dest_addr,
623 flags |= MSG_DONTWAIT;
626 flags |= MSG_NOSIGNAL;
628 ret = sendto (desc->fd, message, length, flags, dest_addr, dest_len);
630 if (SOCKET_ERROR == ret)
631 SetErrnoFromWinsockError (WSAGetLastError ());
640 * @param level protocol level of the option
641 * @param option_name option identifier
642 * @param option_value value to set
643 * @param option_len size of option_value
644 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
647 GNUNET_NETWORK_socket_setsockopt (struct GNUNET_NETWORK_Handle *fd, int level,
648 int option_name, const void *option_value,
649 socklen_t option_len)
653 ret = setsockopt (fd->fd, level, option_name, option_value, option_len);
655 if (SOCKET_ERROR == ret)
656 SetErrnoFromWinsockError (WSAGetLastError ());
658 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
663 * Create a new socket. Configure it for non-blocking IO and
664 * mark it as non-inheritable to child processes (set the
665 * close-on-exec flag).
667 * @param domain domain of the socket
668 * @param type socket type
669 * @param protocol network protocol
670 * @return new socket, NULL on error
672 struct GNUNET_NETWORK_Handle *
673 GNUNET_NETWORK_socket_create (int domain, int type, int protocol)
675 struct GNUNET_NETWORK_Handle *ret;
677 ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle));
679 ret->fd = socket (domain, type, protocol);
680 if (INVALID_SOCKET == ret->fd)
683 SetErrnoFromWinsockError (WSAGetLastError ());
690 if (ret->fd >= FD_SETSIZE)
692 GNUNET_break (0 == close (ret->fd));
699 if (GNUNET_SYSERR == socket_set_blocking (ret, GNUNET_NO))
701 /* we might want to treat this one as fatal... */
703 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret));
708 if (GNUNET_OK != socket_set_inheritable (ret))
709 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
710 "socket_set_inheritable");
713 socket_set_nosigpipe (ret);
715 if ((type == SOCK_STREAM)
717 && (domain != AF_UNIX)
720 socket_set_nodelay (ret);
726 * Shut down socket operations
728 * @param how type of shutdown
729 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
732 GNUNET_NETWORK_socket_shutdown (struct GNUNET_NETWORK_Handle *desc, int how)
736 ret = shutdown (desc->fd, how);
739 SetErrnoFromWinsockError (WSAGetLastError ());
741 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
746 * Disable the "CORK" feature for communication with the given socket,
747 * forcing the OS to immediately flush the buffer on transmission
748 * instead of potentially buffering multiple messages. Essentially
749 * reduces the OS send buffers to zero.
752 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
755 GNUNET_NETWORK_socket_disable_corking (struct GNUNET_NETWORK_Handle *desc)
763 setsockopt (desc->fd, SOL_SOCKET, SO_SNDBUF, (char *) &value,
765 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
768 setsockopt (desc->fd, SOL_SOCKET, SO_RCVBUF, (char *) &value,
770 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
774 setsockopt (desc->fd, SOL_SOCKET, SO_SNDBUF, &value, sizeof (value))))
775 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
778 setsockopt (desc->fd, SOL_SOCKET, SO_RCVBUF, &value, sizeof (value))))
779 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
782 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
791 GNUNET_NETWORK_fdset_zero (struct GNUNET_NETWORK_FDSet *fds)
796 GNUNET_CONTAINER_slist_clear (fds->handles);
801 * Add a socket to the FD set
803 * @param desc socket to add
806 GNUNET_NETWORK_fdset_set (struct GNUNET_NETWORK_FDSet *fds,
807 const struct GNUNET_NETWORK_Handle *desc)
809 FD_SET (desc->fd, &fds->sds);
810 if (desc->fd + 1 > fds->nsds)
811 fds->nsds = desc->fd + 1;
816 * Check whether a socket is part of the fd set
819 * @return 0 if the FD is not set
822 GNUNET_NETWORK_fdset_isset (const struct GNUNET_NETWORK_FDSet *fds,
823 const struct GNUNET_NETWORK_Handle *desc)
825 return FD_ISSET (desc->fd, &fds->sds);
830 * Add one fd set to another
831 * @param dst the fd set to add to
832 * @param src the fd set to add from
835 GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst,
836 const struct GNUNET_NETWORK_FDSet *src)
840 for (nfds = src->nsds; nfds > 0; nfds--)
841 if (FD_ISSET (nfds, &src->sds))
844 FD_SET (nfds, &dst->sds);
845 if (nfds + 1 > dst->nsds)
846 dst->nsds = nfds + 1;
849 GNUNET_CONTAINER_slist_append (dst->handles, src->handles);
855 * Copy one fd set to another
857 * @param to destination
861 GNUNET_NETWORK_fdset_copy (struct GNUNET_NETWORK_FDSet *to,
862 const struct GNUNET_NETWORK_FDSet *from)
864 FD_COPY (&from->sds, &to->sds);
865 to->nsds = from->nsds;
868 GNUNET_CONTAINER_slist_clear (to->handles);
869 GNUNET_CONTAINER_slist_append (to->handles, from->handles);
875 * Return file descriptor for this network handle
877 * @param desc wrapper to process
878 * @return POSIX file descriptor
881 GNUNET_NETWORK_get_fd (struct GNUNET_NETWORK_Handle *desc)
888 * Copy a native fd set
890 * @param to destination
891 * @param from native source set
892 * @param nfds the biggest socket number in from + 1
895 GNUNET_NETWORK_fdset_copy_native (struct GNUNET_NETWORK_FDSet *to,
896 const fd_set * from, int nfds)
898 FD_COPY (from, &to->sds);
904 * Set a native fd in a set
906 * @param to destination
907 * @param nfd native FD to set
910 GNUNET_NETWORK_fdset_set_native (struct GNUNET_NETWORK_FDSet *to, int nfd)
912 GNUNET_assert ((nfd >= 0) && (nfd < FD_SETSIZE));
913 FD_SET (nfd, &to->sds);
914 to->nsds = GNUNET_MAX (nfd + 1, to->nsds);
919 * Test native fd in a set
921 * @param to set to test, NULL for empty set
922 * @param nfd native FD to test, or -1 for none
923 * @return GNUNET_YES if FD is set in the set
926 GNUNET_NETWORK_fdset_test_native (const struct GNUNET_NETWORK_FDSet *to,
929 if ((nfd == -1) || (to == NULL))
931 return FD_ISSET (nfd, &to->sds) ? GNUNET_YES : GNUNET_NO;
936 * Add a file handle to the fd set
938 * @param h the file handle to add
941 GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds,
942 const struct GNUNET_DISK_FileHandle *h)
945 GNUNET_CONTAINER_slist_add (fds->handles,
946 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT, h,
947 sizeof (struct GNUNET_DISK_FileHandle));
952 GNUNET_DISK_internal_file_handle_ (h, &fd, sizeof (int));
953 FD_SET (fd, &fds->sds);
954 if (fd + 1 > fds->nsds)
962 * Check if a file handle is part of an fd set
964 * @param h file handle
965 * @return GNUNET_YES if the file handle is part of the set
968 GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds,
969 const struct GNUNET_DISK_FileHandle *h)
973 return GNUNET_CONTAINER_slist_contains (fds->handles, h,
975 GNUNET_DISK_FileHandle));
977 return FD_ISSET (h->fd, &fds->sds);
983 * Checks if two fd sets overlap
984 * @param fds1 first fd set
985 * @param fds2 second fd set
986 * @return GNUNET_YES if they do overlap, GNUNET_NO otherwise
989 GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1,
990 const struct GNUNET_NETWORK_FDSet *fds2)
996 if (nfds > fds2->nsds)
1001 if (FD_ISSET (nfds, &fds1->sds) && FD_ISSET (nfds, &fds2->sds))
1005 struct GNUNET_CONTAINER_SList_Iterator *it;
1006 struct GNUNET_DISK_FileHandle *h;
1010 /*This code is somewhat hacky, we are not supposed to know what's
1011 * inside of fd_set; also the O(n^2) is really bad... */
1013 for (i = 0; i < fds1->sds.fd_count; i++)
1015 for (j = 0; j < fds2->sds.fd_count; j++)
1017 if (fds1->sds.fd_array[i] == fds2->sds.fd_array[j])
1021 it = GNUNET_CONTAINER_slist_begin (fds1->handles);
1022 while (GNUNET_CONTAINER_slist_end (it) != GNUNET_YES)
1025 struct GNUNET_CONTAINER_SList_Iterator *t;
1027 h = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (it, NULL);
1029 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1030 "Checking that FD 0x%x is in another set:\n", h->h);
1031 for (t = GNUNET_CONTAINER_slist_begin (fds2->handles);
1032 GNUNET_CONTAINER_slist_end (t) != GNUNET_YES;
1033 GNUNET_CONTAINER_slist_next (t))
1035 struct GNUNET_DISK_FileHandle *fh;
1037 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (t,
1039 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "0x%x\n", fh->h);
1042 if (GNUNET_CONTAINER_slist_contains
1043 (fds2->handles, h, sizeof (struct GNUNET_DISK_FileHandle)))
1046 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Match!\n");
1048 GNUNET_CONTAINER_slist_iter_destroy (it);
1051 GNUNET_CONTAINER_slist_next (it);
1053 GNUNET_CONTAINER_slist_iter_destroy (it);
1061 * @return a new fd set
1063 struct GNUNET_NETWORK_FDSet *
1064 GNUNET_NETWORK_fdset_create ()
1066 struct GNUNET_NETWORK_FDSet *fds;
1068 fds = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_FDSet));
1070 fds->handles = GNUNET_CONTAINER_slist_create ();
1072 GNUNET_NETWORK_fdset_zero (fds);
1078 * Releases the associated memory of an fd set
1082 GNUNET_NETWORK_fdset_destroy (struct GNUNET_NETWORK_FDSet *fds)
1085 GNUNET_CONTAINER_slist_destroy (fds->handles);
1091 * Check if sockets meet certain conditions
1092 * @param rfds set of sockets to be checked for readability
1093 * @param wfds set of sockets to be checked for writability
1094 * @param efds set of sockets to be checked for exceptions
1095 * @param timeout relative value when to return
1096 * @return number of selected sockets, GNUNET_SYSERR on error
1099 GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
1100 struct GNUNET_NETWORK_FDSet *wfds,
1101 struct GNUNET_NETWORK_FDSet *efds,
1102 const struct GNUNET_TIME_Relative timeout)
1109 int read_handles = 0;
1110 int write_handles = 0;
1116 int nsock = 0, nhandles = 0, nSockEvents = 0;
1118 static HANDLE hEventRead = 0;
1119 static HANDLE hEventWrite = 0;
1120 static HANDLE hEventException = 0;
1121 static HANDLE hEventPipeWrite = 0;
1122 static HANDLE hEventReadReady = 0;
1125 int writePipePos = 0;
1127 HANDLE handle_array[FD_SETSIZE + 2];
1128 int returncode = -1;
1129 DWORD newretcode = 0;
1130 int returnedpos = 0;
1132 struct GNUNET_CONTAINER_SList *handles_read, *handles_write, *handles_except;
1134 fd_set aread, awrite, aexcept;
1137 fd_set bread, bwrite, bexcept;
1140 /* TODO: Make this growable */
1141 struct GNUNET_DISK_FileHandle *readArray[50];
1149 handles += read_handles = GNUNET_CONTAINER_slist_count (rfds->handles);
1152 struct GNUNET_CONTAINER_SList_Iterator *t;
1154 for (t = GNUNET_CONTAINER_slist_begin (rfds->handles);
1155 GNUNET_CONTAINER_slist_end (t) != GNUNET_YES;
1156 GNUNET_CONTAINER_slist_next (t))
1158 struct GNUNET_DISK_FileHandle *fh;
1160 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (t,
1162 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "FD 0x%x (0x%x) is SET in rfds\n",
1171 nfds = GNUNET_MAX (nfds, wfds->nsds);
1173 handles += write_handles = GNUNET_CONTAINER_slist_count (wfds->handles);
1178 nfds = GNUNET_MAX (nfds, efds->nsds);
1180 handles += ex_handles = GNUNET_CONTAINER_slist_count (efds->handles);
1185 (timeout.rel_value == GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
1191 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1193 ("Fatal internal logic error, process hangs in `%s' (abort with CTRL-C)!\n"),
1198 tv.tv_sec = timeout.rel_value / GNUNET_TIME_UNIT_SECONDS.rel_value;
1200 1000 * (timeout.rel_value -
1201 (tv.tv_sec * GNUNET_TIME_UNIT_SECONDS.rel_value));
1202 return select (nfds, (rfds != NULL) ? &rfds->sds : NULL,
1203 (wfds != NULL) ? &wfds->sds : NULL,
1204 (efds != NULL) ? &efds->sds : NULL,
1205 (timeout.rel_value ==
1206 GNUNET_TIME_UNIT_FOREVER_REL.rel_value) ? NULL : &tv);
1209 #define SAFE_FD_ISSET(fd, set) (set != NULL && FD_ISSET(fd, set))
1210 /* calculate how long we need to wait in milliseconds */
1211 if (timeout.rel_value == GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
1212 ms_total = INFINITE;
1214 ms_total = timeout.rel_value / GNUNET_TIME_UNIT_MILLISECONDS.rel_value;
1215 /* select() may be used as a portable way to sleep */
1216 if (!(rfds || wfds || efds))
1222 /* Events for sockets */
1224 hEventRead = CreateEvent (NULL, TRUE, FALSE, NULL);
1226 ResetEvent (hEventRead);
1227 if (!hEventReadReady)
1228 hEventReadReady = CreateEvent (NULL, TRUE, TRUE, NULL);
1230 hEventWrite = CreateEvent (NULL, TRUE, FALSE, NULL);
1232 ResetEvent (hEventWrite);
1233 if (!hEventException)
1234 hEventException = CreateEvent (NULL, TRUE, FALSE, NULL);
1236 ResetEvent (hEventException);
1238 /* Event for pipes */
1239 if (!hEventPipeWrite)
1240 hEventPipeWrite = CreateEvent (NULL, TRUE, TRUE, NULL);
1244 handles_read = GNUNET_CONTAINER_slist_create ();
1245 handles_write = GNUNET_CONTAINER_slist_create ();
1246 handles_except = GNUNET_CONTAINER_slist_create ();
1257 FD_COPY (&rfds->sds, &aread);
1259 FD_COPY (&rfds->sds, &bread);
1264 FD_COPY (&wfds->sds, &awrite);
1266 FD_COPY (&wfds->sds, &bwrite);
1271 FD_COPY (&efds->sds, &aexcept);
1273 FD_COPY (&efds->sds, &bexcept);
1276 /* We will first Add the PIPES to the events */
1278 if (rfds && read_handles)
1280 struct GNUNET_CONTAINER_SList_Iterator *i;
1282 for (i = GNUNET_CONTAINER_slist_begin (rfds->handles);
1283 GNUNET_CONTAINER_slist_end (i) != GNUNET_YES;
1284 GNUNET_CONTAINER_slist_next (i))
1286 struct GNUNET_DISK_FileHandle *fh;
1288 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (i,
1290 if (fh->type == GNUNET_PIPE)
1292 /* Read zero bytes to check the status of the pipe */
1294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1295 "Reading 0 bytes from the pipe 0x%x\n", fh->h);
1297 if (!ReadFile (fh->h, NULL, 0, NULL, fh->oOverlapRead))
1299 DWORD error_code = GetLastError ();
1301 if (error_code == ERROR_IO_PENDING)
1304 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1305 "Adding the pipe's 0x%x overlapped event to the array as %d\n",
1308 handle_array[nhandles++] = fh->oOverlapRead->hEvent;
1309 readArray[readPipes++] = fh;
1314 * SetErrnoFromWinError (error_code);
1321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1322 "Adding the read ready event to the array as %d\n",
1325 handle_array[nhandles++] = hEventReadReady;
1326 readArray[readPipes++] = fh;
1331 GNUNET_CONTAINER_slist_add (handles_read,
1332 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1333 fh, sizeof (struct GNUNET_DISK_FileHandle));
1336 GNUNET_CONTAINER_slist_iter_destroy (i);
1338 if (wfds && write_handles)
1341 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1342 "Adding the write ready event to the array as %d\n", nhandles);
1344 handle_array[nhandles++] = hEventPipeWrite;
1345 writePipePos = nhandles;
1347 if (efds && ex_handles)
1349 struct GNUNET_CONTAINER_SList_Iterator *i;
1351 for (i = GNUNET_CONTAINER_slist_begin (efds->handles);
1352 GNUNET_CONTAINER_slist_end (i) != GNUNET_YES;
1353 GNUNET_CONTAINER_slist_next (i))
1355 struct GNUNET_DISK_FileHandle *fh;
1358 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (i,
1360 if (fh->type == GNUNET_PIPE)
1362 if (!PeekNamedPipe (fh->h, NULL, 0, NULL, &dwBytes, NULL))
1364 GNUNET_CONTAINER_slist_add (handles_except,
1365 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1367 sizeof (struct GNUNET_DISK_FileHandle));
1372 GNUNET_CONTAINER_slist_iter_destroy (i);
1379 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1380 "Adding the socket read event to the array as %d\n",
1383 handle_array[nhandles++] = hEventRead;
1385 for (i = 0; i < rfds->sds.fd_count; i++)
1387 WSAEventSelect (rfds->sds.fd_array[i], hEventRead,
1388 FD_ACCEPT | FD_READ | FD_CLOSE);
1397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1398 "Adding the socket write event to the array as %d\n",
1401 handle_array[nhandles++] = hEventWrite;
1403 for (i = 0; i < wfds->sds.fd_count; i++)
1408 status = send (wfds->sds.fd_array[i], NULL, 0, 0);
1409 error = GetLastError ();
1411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1412 "pre-send to the socket %d returned %d (%u)\n", i, status,
1415 if (status == 0 || (error != WSAEWOULDBLOCK && error != WSAENOTCONN))
1417 WSAEventSelect (wfds->sds.fd_array[i], hEventWrite,
1418 FD_WRITE | FD_CONNECT | FD_CLOSE);
1422 SetEvent (hEventWrite);
1427 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1428 "Adding the socket error event to the array as %d\n",
1431 handle_array[nhandles++] = hEventException;
1433 for (i = 0; i < efds->sds.fd_count; i++)
1435 WSAEventSelect (efds->sds.fd_array[i], hEventException,
1442 handle_array[nhandles] = NULL;
1445 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Number nfds : %d\n", nfds);
1446 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Number of handles : %d\n", nhandles);
1447 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "retcode : %d\n", newretcode);
1448 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Will wait : %d\n", ms_total);
1453 WaitForMultipleObjects (nhandles, handle_array, FALSE, ms_total);
1455 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "WaitForMultipleObjects Returned : %d\n",
1459 returnedpos = returncode - WAIT_OBJECT_0;
1461 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return pos is : %d\n", returnedpos);
1464 /* FIXME: THIS LINE IS WRONG !! We should add to handles only handles that fired the events, not all ! */
1467 * GNUNET_CONTAINER_slist_append (handles_read, rfds->handles);
1469 if (nhandles && (returnedpos < nhandles))
1476 struct timeval tvslice;
1479 tvslice.tv_usec = 10;
1480 retcode = select (nfds, &aread, &awrite, &aexcept, &tvslice);
1484 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Select retcode : %d\n", retcode);
1487 /* FIXME: <= writePipePos? Really? */
1488 if ((writePipePos != -1) && (returnedpos <= writePipePos))
1490 GNUNET_CONTAINER_slist_append (handles_write, wfds->handles);
1491 retcode += write_handles;
1493 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Added write pipe\n");
1497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ReadPipes is : %d\n", readPipes);
1499 /* We have some pipes ready for read. */
1500 /* FIXME: it is supposed to work !! Only choose the Pipes who fired the event, but it is not working */
1502 if (returnedpos < readPipes)
1505 * for (i = 0; i < readPipes; i++)
1507 * waitstatus = WaitForSingleObject (handle_array[i], 0);
1508 * GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Read pipe %d wait status is : %d\n", i, waitstatus);
1509 * if (waitstatus != WAIT_OBJECT_0)
1511 * GNUNET_CONTAINER_slist_add (handles_read,
1512 * GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1513 * readArray[i], sizeof (struct GNUNET_DISK_FileHandle));
1515 * GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Added read Pipe\n");
1518 for (i = 0; i < readPipes; i++)
1526 PeekNamedPipe (readArray[i]->h, NULL, 0, NULL, &waitstatus, NULL);
1527 error = GetLastError ();
1529 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1530 "Peek at read pipe %d (0x%x) returned %d (%d bytes available) GLE %u\n",
1531 i, readArray[i]->h, bret, waitstatus, error);
1533 if (bret == 0 || waitstatus <= 0)
1535 GNUNET_CONTAINER_slist_add (handles_read,
1536 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1538 sizeof (struct GNUNET_DISK_FileHandle));
1541 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Added read Pipe 0x%x (0x%x)\n",
1542 readArray[i], readArray[i]->h);
1546 waitstatus = WaitForSingleObject (hEventWrite, 0);
1548 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1549 "Wait for the write event returned %d\n", waitstatus);
1551 if (waitstatus == WAIT_OBJECT_0)
1553 for (i = 0; i < wfds->sds.fd_count; i++)
1558 int sizeof_so_error = sizeof (so_error);
1560 getsockopt (wfds->sds.fd_array[i], SOL_SOCKET, SO_ERROR,
1561 (char *) &so_error, &sizeof_so_error);
1563 status = send (wfds->sds.fd_array[i], NULL, 0, 0);
1564 error = GetLastError ();
1566 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1567 "send to the socket %d returned %d (%u)\n", i, status,
1570 if (status == 0 || (error != WSAEWOULDBLOCK && error != WSAENOTCONN) ||
1571 (status == -1 && gso_result == 0 && error == WSAENOTCONN &&
1572 so_error == WSAECONNREFUSED))
1574 FD_SET (wfds->sds.fd_array[i], &awrite);
1581 if (!nhandles || (returnedpos >= nhandles))
1582 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1583 "Returning from _select() with nothing!\n");
1587 struct GNUNET_CONTAINER_SList_Iterator *t;
1589 for (i = 0; i < rfds->sds.fd_count; i++)
1591 WSAEventSelect (rfds->sds.fd_array[i], hEventRead, 0);
1594 for (t = GNUNET_CONTAINER_slist_begin (rfds->handles);
1595 GNUNET_CONTAINER_slist_end (t) != GNUNET_YES;
1596 GNUNET_CONTAINER_slist_next (t))
1598 struct GNUNET_DISK_FileHandle *fh;
1600 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (t,
1602 if (fh->type == GNUNET_PIPE)
1607 GNUNET_CONTAINER_slist_iter_destroy (t);
1609 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Zeroing rfds\n");
1611 GNUNET_NETWORK_fdset_zero (rfds);
1612 if (retcode != -1 && nhandles && (returnedpos < nhandles))
1613 GNUNET_NETWORK_fdset_copy_native (rfds, &aread, retcode);
1614 GNUNET_CONTAINER_slist_append (rfds->handles, handles_read);
1618 for (i = 0; i < wfds->sds.fd_count; i++)
1620 WSAEventSelect (wfds->sds.fd_array[i], hEventWrite, 0);
1624 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Zeroing wfds\n");
1626 GNUNET_NETWORK_fdset_zero (wfds);
1627 if (retcode != -1 && nhandles && (returnedpos < nhandles))
1628 GNUNET_NETWORK_fdset_copy_native (wfds, &awrite, retcode);
1629 GNUNET_CONTAINER_slist_append (wfds->handles, handles_write);
1633 for (i = 0; i < efds->sds.fd_count; i++)
1635 WSAEventSelect (efds->sds.fd_array[i], hEventException, 0);
1639 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Zeroing efds\n");
1641 GNUNET_NETWORK_fdset_zero (efds);
1642 if (retcode != -1 && nhandles && (returnedpos < nhandles))
1643 GNUNET_NETWORK_fdset_copy_native (efds, &aexcept, retcode);
1644 GNUNET_CONTAINER_slist_append (efds->handles, handles_except);
1646 GNUNET_CONTAINER_slist_destroy (handles_read);
1647 GNUNET_CONTAINER_slist_destroy (handles_write);
1648 GNUNET_CONTAINER_slist_destroy (handles_except);
1652 struct GNUNET_CONTAINER_SList_Iterator *t;
1654 for (i = 0; i < bread.fd_count; i++)
1656 if (bread.fd_array[i] != 0)
1657 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "FD 0x%x is %s in rfds\n",
1659 (SAFE_FD_ISSET (bread.fd_array[i], rfds)) ? "SET" :
1662 for (t = GNUNET_CONTAINER_slist_begin (rfds->handles);
1663 GNUNET_CONTAINER_slist_end (t) != GNUNET_YES;
1664 GNUNET_CONTAINER_slist_next (t))
1666 struct GNUNET_DISK_FileHandle *fh;
1668 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (t,
1670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "FD 0x%x is SET in rfds\n", fh->h);
1675 for (i = 0; i < bwrite.fd_count; i++)
1677 if (bwrite.fd_array[i] != 0)
1678 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "FD 0x%x is %s in wfds\n",
1680 (SAFE_FD_ISSET (bwrite.fd_array[i], rfds)) ? "SET" :
1686 for (i = 0; i < bexcept.fd_count; i++)
1688 if (bexcept.fd_array[i] != 0)
1689 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "FD 0x%x is %s in efds\n",
1690 bexcept.fd_array[i],
1691 (SAFE_FD_ISSET (bexcept.fd_array[i], rfds)) ? "SET" :
1695 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Returning %d or 0\n", retcode);
1697 if (nhandles && (returnedpos < nhandles))
1704 /* end of network.c */