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 #ifndef INVALID_SOCKET
35 #define INVALID_SOCKET -1
39 struct GNUNET_NETWORK_Handle
49 * Address family / domain.
55 struct GNUNET_NETWORK_FDSet
59 * Maximum number of any socket socket descriptor in the set (plus one)
64 * Bitset with the descriptors.
70 * Linked list of handles
72 struct GNUNET_CONTAINER_SList *handles;
78 #define FD_COPY(s, d) (memcpy ((d), (s), sizeof (fd_set)))
83 * Set if a socket should use blocking or non-blocking IO.
85 * @param doBlock blocking mode
86 * @return GNUNET_OK on success, GNUNET_SYSERR on error
89 socket_set_blocking (struct GNUNET_NETWORK_Handle *fd, int doBlock)
95 if (ioctlsocket (fd->fd, FIONBIO, &mode) == SOCKET_ERROR)
98 SetErrnoFromWinsockError (WSAGetLastError ());
99 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "ioctlsocket");
100 return GNUNET_SYSERR;
106 int flags = fcntl (fd->fd, F_GETFL);
110 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "fcntl");
111 return GNUNET_SYSERR;
114 flags &= ~O_NONBLOCK;
118 if (0 != fcntl (fd->fd, F_SETFL, flags))
121 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "fcntl");
122 return GNUNET_SYSERR;
131 * Make a socket non-inheritable to child processes
133 * @param h the socket to make non-inheritable
134 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
135 * @warning Not implemented on Windows
138 socket_set_inheritable (const struct GNUNET_NETWORK_Handle *h)
142 i = fcntl (h->fd, F_GETFD);
144 return GNUNET_SYSERR;
145 if (i == (i | FD_CLOEXEC))
148 if (fcntl (h->fd, F_SETFD, i) < 0)
149 return GNUNET_SYSERR;
157 * The MSG_NOSIGNAL equivalent on Mac OS X
159 * @param h the socket to make non-delaying
162 socket_set_nosigpipe (const struct GNUNET_NETWORK_Handle *h)
166 setsockopt (h->fd, SOL_SOCKET, SO_NOSIGPIPE, &abs_value, sizeof (abs_value)))
167 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
173 * Disable delays when sending data via the socket.
174 * (GNUnet makes sure that messages are as big as
177 * @param h the socket to make non-delaying
180 socket_set_nodelay (const struct GNUNET_NETWORK_Handle *h)
184 if (0 != setsockopt (h->fd, IPPROTO_TCP, TCP_NODELAY, &value, sizeof (value)))
185 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
187 const char * abs_value = "1";
188 if (0 != setsockopt (h->fd, IPPROTO_TCP, TCP_NODELAY, abs_value, sizeof (abs_value)))
189 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
195 * accept a new connection on a socket
197 * @param desc bound socket
198 * @param address address of the connecting peer, may be NULL
199 * @param address_len length of address
200 * @return client socket
202 struct GNUNET_NETWORK_Handle *
203 GNUNET_NETWORK_socket_accept (const struct GNUNET_NETWORK_Handle *desc,
204 struct sockaddr *address,
205 socklen_t * address_len)
207 struct GNUNET_NETWORK_Handle *ret;
209 ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle));
210 ret->fd = accept (desc->fd, address, address_len);
211 ret->af = address->sa_family;
212 if (ret->fd == INVALID_SOCKET)
215 SetErrnoFromWinsockError (WSAGetLastError ());
221 if (ret->fd >= FD_SETSIZE)
223 GNUNET_break (0 == close (ret->fd));
229 if (GNUNET_SYSERR == socket_set_blocking (ret, GNUNET_NO))
233 /* we might want to treat this one as fatal... */
235 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret));
240 if (GNUNET_OK != socket_set_inheritable (ret))
241 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
242 "socket_set_inheritable");
245 socket_set_nosigpipe (ret);
248 if (address->sa_family != AF_UNIX)
250 socket_set_nodelay (ret);
256 * Bind to a connected socket
258 * @param address address to be bound
259 * @param address_len length of address
260 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
263 GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc,
264 const struct sockaddr *address,
265 socklen_t address_len)
272 if (desc->af == AF_INET6)
273 if (0 != setsockopt (desc->fd, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof (on)))
274 GNUNET_log_strerror (GNUNET_ERROR_TYPE_DEBUG, "setsockopt");
276 /* is this needed or desired? or done elsewhere? */
277 if (0 != setsockopt (desc->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)))
278 GNUNET_log_strerror (GNUNET_ERROR_TYPE_DEBUG, "setsockopt");
282 ret = bind (desc->fd, address, address_len);
284 if (SOCKET_ERROR == ret)
285 SetErrnoFromWinsockError (WSAGetLastError ());
288 if ( (ret == 0) && (address->sa_family == AF_UNIX))
290 const struct sockaddr_un *un = (const struct sockaddr_un*) address;
291 if (0 != unlink (un->sun_path))
292 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
298 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
305 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
308 GNUNET_NETWORK_socket_close (struct GNUNET_NETWORK_Handle *desc)
313 ret = closesocket (desc->fd);
314 SetErrnoFromWinsockError (WSAGetLastError ());
316 ret = close (desc->fd);
319 return (ret == 0) ? GNUNET_OK : GNUNET_SYSERR;
324 * Box a native socket (and check that it is a socket).
326 * @param fd socket to box
327 * @return NULL on error (including not supported on target platform)
329 struct GNUNET_NETWORK_Handle *
330 GNUNET_NETWORK_socket_box_native (int fd)
335 struct GNUNET_NETWORK_Handle *ret;
337 if (fcntl (fd, F_GETFD) < 0)
338 return NULL; /* invalid FD */
339 ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle));
350 * @param address peer address
351 * @param address_len length of address
352 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
355 GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Handle *desc,
356 const struct sockaddr *address,
357 socklen_t address_len)
360 ret = connect (desc->fd, address, address_len);
363 if (SOCKET_ERROR == ret)
366 SetErrnoFromWinsockError (WSAGetLastError ());
367 if (errno == EWOULDBLOCK)
371 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
379 * @param level protocol level of the option
380 * @param optname identifier of the option
381 * @param optval options
382 * @param optlen length of optval
383 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
386 GNUNET_NETWORK_socket_getsockopt (const struct GNUNET_NETWORK_Handle *desc,
387 int level, int optname, void *optval,
391 ret = getsockopt (desc->fd, level, optname, optval, optlen);
394 if (ret == 0 && level == SOL_SOCKET && optname == SO_ERROR)
395 *((int *) optval) = GetErrnoFromWinsockError (*((int *) optval));
397 else if (SOCKET_ERROR == ret)
398 SetErrnoFromWinsockError (WSAGetLastError ());
401 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
408 * @param backlog length of the listen queue
409 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
412 GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Handle *desc,
416 ret = listen (desc->fd, backlog);
419 if (SOCKET_ERROR == ret)
420 SetErrnoFromWinsockError (WSAGetLastError ());
423 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
428 * How much data is available to be read on this descriptor?
430 * Returns GNUNET_NO if no data is available, or on error!
434 GNUNET_NETWORK_socket_recvfrom_amount (const struct GNUNET_NETWORK_Handle
439 /* How much is there to be read? */
442 error = ioctl (desc->fd, FIONREAD, &pending);
446 error = ioctlsocket (desc->fd, FIONREAD, &pending);
447 if (error != SOCKET_ERROR)
456 * Read data from a connected socket (always non-blocking).
458 * @param buffer buffer
459 * @param length length of buffer
460 * @param src_addr either the source to recv from, or all zeroes
461 * to be filled in by recvfrom
462 * @param addrlen length of the addr
465 GNUNET_NETWORK_socket_recvfrom (const struct GNUNET_NETWORK_Handle * desc,
466 void *buffer, size_t length,
467 struct sockaddr * src_addr,
475 flags |= MSG_DONTWAIT;
478 ret = recvfrom (desc->fd, buffer, length, flags, src_addr, addrlen);
480 if (SOCKET_ERROR == ret)
481 SetErrnoFromWinsockError (WSAGetLastError ());
488 * Read data from a connected socket (always non-blocking).
490 * @param buffer buffer
491 * @param length length of buffer
494 GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Handle * desc,
495 void *buffer, size_t length)
502 flags |= MSG_DONTWAIT;
504 ret = recv (desc->fd, buffer, length, flags);
506 if (SOCKET_ERROR == ret)
507 SetErrnoFromWinsockError (WSAGetLastError ());
514 * Send data (always non-blocking).
517 * @param buffer data to send
518 * @param length size of the buffer
519 * @return number of bytes sent, GNUNET_SYSERR on error
522 GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Handle * desc,
523 const void *buffer, size_t length)
530 flags |= MSG_DONTWAIT;
534 flags |= MSG_NOSIGNAL;
537 ret = send (desc->fd, buffer, length, flags);
540 if (SOCKET_ERROR == ret)
541 SetErrnoFromWinsockError (WSAGetLastError ());
549 * Send data to a particular destination (always non-blocking).
550 * This function only works for UDP sockets.
553 * @param message data to send
554 * @param length size of the data
555 * @param dest_addr destination address
556 * @param dest_len length of address
557 * @return number of bytes sent, GNUNET_SYSERR on error
560 GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Handle * desc,
561 const void *message, size_t length,
562 const struct sockaddr * dest_addr,
570 flags |= MSG_DONTWAIT;
573 flags |= MSG_NOSIGNAL;
575 ret = sendto (desc->fd, message, length, flags, dest_addr, dest_len);
577 if (SOCKET_ERROR == ret)
578 SetErrnoFromWinsockError (WSAGetLastError ());
587 * @param level protocol level of the option
588 * @param option_name option identifier
589 * @param option_value value to set
590 * @param option_len size of option_value
591 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
594 GNUNET_NETWORK_socket_setsockopt (struct GNUNET_NETWORK_Handle *fd,
595 int level, int option_name,
596 const void *option_value,
597 socklen_t option_len)
601 ret = setsockopt (fd->fd, level, option_name, option_value, option_len);
603 if (SOCKET_ERROR == ret)
604 SetErrnoFromWinsockError (WSAGetLastError ());
606 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
611 * Create a new socket. Configure it for non-blocking IO and
612 * mark it as non-inheritable to child processes (set the
613 * close-on-exec flag).
615 * @param domain domain of the socket
616 * @param type socket type
617 * @param protocol network protocol
618 * @return new socket, NULL on error
620 struct GNUNET_NETWORK_Handle *
621 GNUNET_NETWORK_socket_create (int domain, int type, int protocol)
623 struct GNUNET_NETWORK_Handle *ret;
624 ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle));
626 ret->fd = socket (domain, type, protocol);
627 if (INVALID_SOCKET == ret->fd)
630 SetErrnoFromWinsockError (WSAGetLastError ());
637 if (ret->fd >= FD_SETSIZE)
639 GNUNET_break (0 == close (ret->fd));
646 if (GNUNET_SYSERR == socket_set_blocking (ret, GNUNET_NO))
648 /* we might want to treat this one as fatal... */
650 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret));
655 if (GNUNET_OK != socket_set_inheritable (ret))
656 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
657 "socket_set_inheritable");
660 socket_set_nosigpipe (ret);
662 if ( (type == SOCK_STREAM)
664 && (domain != AF_UNIX)
667 socket_set_nodelay (ret);
673 * Shut down socket operations
675 * @param how type of shutdown
676 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
679 GNUNET_NETWORK_socket_shutdown (struct GNUNET_NETWORK_Handle *desc, int how)
683 ret = shutdown (desc->fd, how);
686 SetErrnoFromWinsockError (WSAGetLastError ());
688 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
697 GNUNET_NETWORK_fdset_zero (struct GNUNET_NETWORK_FDSet *fds)
702 GNUNET_CONTAINER_slist_clear (fds->handles);
707 * Add a socket to the FD set
709 * @param desc socket to add
712 GNUNET_NETWORK_fdset_set (struct GNUNET_NETWORK_FDSet *fds,
713 const struct GNUNET_NETWORK_Handle *desc)
715 FD_SET (desc->fd, &fds->sds);
716 if (desc->fd + 1 > fds->nsds)
717 fds->nsds = desc->fd + 1;
722 * Check whether a socket is part of the fd set
725 * @return 0 if the FD is not set
728 GNUNET_NETWORK_fdset_isset (const struct GNUNET_NETWORK_FDSet *fds,
729 const struct GNUNET_NETWORK_Handle *desc)
731 return FD_ISSET (desc->fd, &fds->sds);
736 * Add one fd set to another
737 * @param dst the fd set to add to
738 * @param src the fd set to add from
741 GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst,
742 const struct GNUNET_NETWORK_FDSet *src)
745 for (nfds = src->nsds; nfds > 0; nfds--)
746 if (FD_ISSET (nfds, &src->sds))
749 FD_SET (nfds, &dst->sds);
750 if (nfds + 1 > dst->nsds)
751 dst->nsds = nfds + 1;
754 GNUNET_CONTAINER_slist_append (dst->handles, src->handles);
760 * Copy one fd set to another
762 * @param to destination
766 GNUNET_NETWORK_fdset_copy (struct GNUNET_NETWORK_FDSet *to,
767 const struct GNUNET_NETWORK_FDSet *from)
769 FD_COPY (&from->sds, &to->sds);
770 to->nsds = from->nsds;
773 GNUNET_CONTAINER_slist_clear (to->handles);
774 GNUNET_CONTAINER_slist_append (to->handles, from->handles);
780 * Return file descriptor for this network handle
782 * @param desc wrapper to process
783 * @return POSIX file descriptor
786 GNUNET_NETWORK_get_fd (struct GNUNET_NETWORK_Handle *desc)
793 * Copy a native fd set
795 * @param to destination
796 * @param from native source set
797 * @param nfds the biggest socket number in from + 1
800 GNUNET_NETWORK_fdset_copy_native (struct GNUNET_NETWORK_FDSet *to,
801 const fd_set * from, int nfds)
803 FD_COPY (from, &to->sds);
809 * Set a native fd in a set
811 * @param to destination
812 * @param nfd native FD to set
814 void GNUNET_NETWORK_fdset_set_native (struct GNUNET_NETWORK_FDSet *to,
817 GNUNET_assert(nfd >= 0);
818 FD_SET (nfd, &to->sds);
819 to->nsds = GNUNET_MAX (nfd + 1, to->nsds);
824 * Test native fd in a set
826 * @param to set to test, NULL for empty set
827 * @param nfd native FD to test, or -1 for none
828 * @return GNUNET_YES if FD is set in the set
831 GNUNET_NETWORK_fdset_test_native (const struct GNUNET_NETWORK_FDSet *to,
834 if ( (nfd == -1) || (to == NULL) )
836 return FD_ISSET (nfd, &to->sds) ? GNUNET_YES : GNUNET_NO;
841 * Add a file handle to the fd set
843 * @param h the file handle to add
846 GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds,
847 const struct GNUNET_DISK_FileHandle *h)
850 GNUNET_CONTAINER_slist_add (fds->handles,
851 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
852 h, sizeof (struct GNUNET_DISK_FileHandle));
856 GNUNET_DISK_internal_file_handle_ (h, &fd, sizeof (int));
857 FD_SET (fd, &fds->sds);
858 if (fd + 1 > fds->nsds)
866 * Check if a file handle is part of an fd set
868 * @param h file handle
869 * @return GNUNET_YES if the file handle is part of the set
872 GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds,
873 const struct GNUNET_DISK_FileHandle *h)
877 return GNUNET_CONTAINER_slist_contains (fds->handles, h,
878 sizeof (struct GNUNET_DISK_FileHandle));
880 return FD_ISSET (h->fd, &fds->sds);
886 * Checks if two fd sets overlap
887 * @param fds1 first fd set
888 * @param fds2 second fd set
889 * @return GNUNET_YES if they do overlap, GNUNET_NO otherwise
892 GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1,
893 const struct GNUNET_NETWORK_FDSet *fds2)
899 if (nfds > fds2->nsds)
904 if (FD_ISSET (nfds, &fds1->sds) && FD_ISSET (nfds, &fds2->sds))
908 struct GNUNET_CONTAINER_SList_Iterator *it;
909 struct GNUNET_DISK_FileHandle *h;
913 /*This code is somewhat hacky, we are not supposed to know what's
914 inside of fd_set; also the O(n^2) is really bad... */
916 for (i = 0; i < fds1->sds.fd_count; i++)
918 for (j = 0; j < fds2->sds.fd_count; j++)
920 if (fds1->sds.fd_array[i] == fds2->sds.fd_array[j])
924 it = GNUNET_CONTAINER_slist_begin (fds1->handles);
925 while (GNUNET_CONTAINER_slist_end (it) != GNUNET_YES)
927 h = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (it, NULL);
928 if (GNUNET_CONTAINER_slist_contains
929 (fds2->handles, h, sizeof (struct GNUNET_DISK_FileHandle)))
931 GNUNET_CONTAINER_slist_iter_destroy (it);
934 GNUNET_CONTAINER_slist_next (it);
936 GNUNET_CONTAINER_slist_iter_destroy (it);
944 * @return a new fd set
946 struct GNUNET_NETWORK_FDSet *
947 GNUNET_NETWORK_fdset_create ()
949 struct GNUNET_NETWORK_FDSet *fds;
950 fds = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_FDSet));
952 fds->handles = GNUNET_CONTAINER_slist_create ();
954 GNUNET_NETWORK_fdset_zero (fds);
960 * Releases the associated memory of an fd set
964 GNUNET_NETWORK_fdset_destroy (struct GNUNET_NETWORK_FDSet *fds)
967 GNUNET_CONTAINER_slist_destroy (fds->handles);
973 * Check if sockets meet certain conditions
974 * @param rfds set of sockets to be checked for readability
975 * @param wfds set of sockets to be checked for writability
976 * @param efds set of sockets to be checked for exceptions
977 * @param timeout relative value when to return
978 * @return number of selected sockets, GNUNET_SYSERR on error
981 GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
982 struct GNUNET_NETWORK_FDSet *wfds,
983 struct GNUNET_NETWORK_FDSet *efds,
984 const struct GNUNET_TIME_Relative timeout)
998 handles = GNUNET_CONTAINER_slist_count (rfds->handles);
1003 nfds = GNUNET_MAX (nfds, wfds->nsds);
1005 handles += GNUNET_CONTAINER_slist_count (wfds->handles);
1010 nfds = GNUNET_MAX (nfds, efds->nsds);
1012 handles += GNUNET_CONTAINER_slist_count (efds->handles);
1017 tv.tv_sec = timeout.rel_value / GNUNET_TIME_UNIT_SECONDS.rel_value;
1019 1000 * (timeout.rel_value - (tv.tv_sec * GNUNET_TIME_UNIT_SECONDS.rel_value));
1020 if ((nfds == 0) && (timeout.rel_value == GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
1026 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1028 ("Fatal internal logic error, process hangs in `%s' (abort with CTRL-C)!\n"),
1033 return select (nfds,
1034 (rfds != NULL) ? &rfds->sds : NULL,
1035 (wfds != NULL) ? &wfds->sds : NULL,
1036 (efds != NULL) ? &efds->sds : NULL,
1037 (timeout.rel_value == GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
1042 fd_set sock_read, sock_write, sock_except;
1043 fd_set aread, awrite, aexcept;
1044 struct GNUNET_CONTAINER_SList *handles_read, *handles_write,
1048 struct timeval tvslice;
1052 #define SAFE_FD_ISSET(fd, set) (set != NULL && FD_ISSET(fd, set))
1054 /* calculate how long we need to wait in milliseconds */
1055 if (timeout.rel_value == GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
1056 ms_total = INFINITE;
1059 ms_total = timeout.rel_value / GNUNET_TIME_UNIT_MILLISECONDS.rel_value;
1061 /* select() may be used as a portable way to sleep */
1062 if (!(rfds || wfds || efds))
1069 handles_read = GNUNET_CONTAINER_slist_create ();
1070 handles_write = GNUNET_CONTAINER_slist_create ();
1071 handles_except = GNUNET_CONTAINER_slist_create ();
1074 sock_read = rfds->sds;
1076 FD_ZERO (&sock_read);
1078 sock_write = wfds->sds;
1080 FD_ZERO (&sock_write);
1082 sock_except = efds->sds;
1084 FD_ZERO (&sock_except);
1086 /* multiplex between winsock select() and waiting on the handles */
1090 limit = GetTickCount () + ms_total;
1099 /* overwrite the zero'd sets here; the select call
1100 * will clear those that are not active */
1101 FD_COPY (&sock_read, &aread);
1102 FD_COPY (&sock_write, &awrite);
1103 FD_COPY (&sock_except, &aexcept);
1105 tvslice.tv_usec = 100000;
1107 select (nfds + 1, &aread, &awrite, &aexcept,
1108 &tvslice)) == SOCKET_ERROR)
1111 SetErrnoFromWinsockError (WSAGetLastError ());
1112 if (errno == ENOTSOCK)
1116 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "select");
1119 goto select_loop_end;
1123 /* Poll read pipes */
1127 struct GNUNET_CONTAINER_SList_Iterator *i;
1128 for (i = GNUNET_CONTAINER_slist_begin (rfds->handles);
1129 GNUNET_CONTAINER_slist_end (i) != GNUNET_YES;
1130 GNUNET_CONTAINER_slist_next (i))
1133 struct GNUNET_DISK_FileHandle *fh;
1135 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (i, NULL);
1136 if (fh->type == GNUNET_PIPE)
1138 if (!PeekNamedPipe (fh->h, NULL, 0, NULL, &dwBytes, NULL))
1140 DWORD error_code = GetLastError ();
1143 case ERROR_BROKEN_PIPE:
1144 GNUNET_CONTAINER_slist_add (handles_read,
1145 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1146 fh, sizeof (struct GNUNET_DISK_FileHandle));
1151 SetErrnoFromWinError (error_code);
1154 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
1158 goto select_loop_end;
1164 GNUNET_CONTAINER_slist_add (handles_read,
1165 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1166 fh, sizeof (struct GNUNET_DISK_FileHandle));
1172 /* Should we wait for more bytes to read here (in case of previous EOF)? */
1173 GNUNET_CONTAINER_slist_add (handles_read,
1174 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1175 fh, sizeof (struct GNUNET_DISK_FileHandle));
1178 GNUNET_CONTAINER_slist_iter_destroy (i);
1181 /* Poll for faulty pipes */
1185 struct GNUNET_CONTAINER_SList_Iterator *i;
1186 for (i = GNUNET_CONTAINER_slist_begin (efds->handles);
1187 GNUNET_CONTAINER_slist_end (i) != GNUNET_YES;
1188 GNUNET_CONTAINER_slist_next (i))
1191 struct GNUNET_DISK_FileHandle *fh;
1194 fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (i, NULL);
1195 if (fh->type == GNUNET_PIPE)
1197 if (!PeekNamedPipe (fh->h, NULL, 0, NULL, &dwBytes, NULL))
1200 GNUNET_CONTAINER_slist_add (handles_except,
1201 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
1202 fh, sizeof (struct GNUNET_DISK_FileHandle));
1207 GNUNET_CONTAINER_slist_iter_destroy (i);
1212 GNUNET_CONTAINER_slist_append (handles_write, wfds->handles);
1213 retcode += GNUNET_CONTAINER_slist_count (wfds->handles);
1216 /* Check for closed sockets */
1217 for (i = 0; i < nfds; i++)
1220 if (SAFE_FD_ISSET (i, &sock_read))
1223 struct sockaddr addr;
1225 if (getpeername (i, &addr, &len) == SOCKET_ERROR)
1231 (i, SOL_SOCKET, SO_ERROR, (char *) &err, &len) == 0
1232 && err == WSAENOTCONN)
1235 if (!SAFE_FD_ISSET (i, &aread))
1246 if (retcode == 0 && nfds == 0)
1247 Sleep (GNUNET_MIN (100, limit - GetTickCount ()));
1249 while (retcode == 0 && (ms_total == INFINITE || GetTickCount () < limit));
1255 GNUNET_NETWORK_fdset_zero (rfds);
1256 GNUNET_NETWORK_fdset_copy_native (rfds, &aread, retcode);
1257 GNUNET_CONTAINER_slist_clear (rfds->handles);
1258 GNUNET_CONTAINER_slist_append (rfds->handles, handles_read);
1262 GNUNET_NETWORK_fdset_zero (wfds);
1263 GNUNET_NETWORK_fdset_copy_native (wfds, &awrite, retcode);
1264 GNUNET_CONTAINER_slist_clear (wfds->handles);
1265 GNUNET_CONTAINER_slist_append (wfds->handles, handles_write);
1269 GNUNET_NETWORK_fdset_zero (efds);
1270 GNUNET_NETWORK_fdset_copy_native (efds, &aexcept, retcode);
1271 GNUNET_CONTAINER_slist_clear (efds->handles);
1272 GNUNET_CONTAINER_slist_append (efds->handles, handles_except);
1276 GNUNET_CONTAINER_slist_destroy (handles_read);
1277 GNUNET_CONTAINER_slist_destroy (handles_write);
1278 GNUNET_CONTAINER_slist_destroy (handles_except);
1285 /* end of network.c */