2 This file is part of GNUnet.
\r
3 (C) 2009 Christian Grothoff (and other contributing authors)
\r
5 GNUnet is free software; you can redistribute it and/or modify
\r
6 it under the terms of the GNU General Public License as published
\r
7 by the Free Software Foundation; either version 2, or (at your
\r
8 option) any later version.
\r
10 GNUnet is distributed in the hope that it will be useful, but
\r
11 WITHOUT ANY WARRANTY; without even the implied warranty of
\r
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
\r
13 General Public License for more details.
\r
15 You should have received a copy of the GNU General Public License
\r
16 along with GNUnet; see the file COPYING. If not, write to the
\r
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
\r
18 Boston, MA 02111-1307, USA.
\r
22 * @file util/network.c
\r
23 * @brief basic, low-level networking interface
\r
24 * @author Nils Durner
\r
27 #include "platform.h"
\r
28 #include "gnunet_disk_lib.h"
\r
30 #include "gnunet_container_lib.h"
\r
32 #define DEBUG_SOCK GNUNET_NO
\r
34 #ifndef INVALID_SOCKET
\r
35 #define INVALID_SOCKET -1
\r
38 struct GNUNET_NETWORK_Handle
\r
43 struct GNUNET_NETWORK_FDSet
\r
45 /* socket descriptors */
\r
50 struct GNUNET_CONTAINER_SList *handles;
\r
55 #define FD_COPY(s, d) (memcpy ((d), (s), sizeof (fd_set)))
\r
61 * Set if a socket should use blocking or non-blocking IO.
\r
63 * @param doBlock blocking mode
\r
64 * @return GNUNET_OK on success, GNUNET_SYSERR on error
\r
67 socket_set_blocking (struct GNUNET_NETWORK_Handle *fd,
\r
73 if (ioctlsocket (fd->fd, FIONBIO, &mode) == SOCKET_ERROR)
\r
75 SetErrnoFromWinsockError (WSAGetLastError ());
\r
76 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "ioctlsocket");
\r
77 return GNUNET_SYSERR;
\r
83 int flags = fcntl (fd->fd, F_GETFL);
\r
86 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "fcntl");
\r
87 return GNUNET_SYSERR;
\r
90 flags &= ~O_NONBLOCK;
\r
92 flags |= O_NONBLOCK;
\r
93 if (0 != fcntl (fd->fd, F_SETFL, flags))
\r
95 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "fcntl");
\r
96 return GNUNET_SYSERR;
\r
105 * Make a socket non-inheritable to child processes
\r
107 * @param h the socket to make non-inheritable
\r
108 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
\r
109 * @warning Not implemented on Windows
\r
112 socket_set_inheritable (const struct GNUNET_NETWORK_Handle
\r
117 i = fcntl (h->fd, F_GETFD);
\r
118 if (i == (i | FD_CLOEXEC))
\r
120 return (fcntl (h->fd, F_SETFD, i | FD_CLOEXEC) == 0)
\r
121 ? GNUNET_OK : GNUNET_SYSERR;
\r
129 * The MSG_NOSIGNAL equivalent on Mac OS X
\r
131 * @param h the socket to make non-delaying
\r
134 socket_set_nosigpipe (const struct GNUNET_NETWORK_Handle
\r
138 if (0 != setsockopt (h->fd, SOL_SOCKET, SO_NOSIGPIPE, &value, sizeof(value)))
\r
139 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
\r
147 * Disable delays when sending data via the socket.
\r
148 * (GNUnet makes sure that messages are as big as
\r
149 * possible already).
\r
151 * @param h the socket to make non-delaying
\r
154 socket_set_nodelay (const struct GNUNET_NETWORK_Handle
\r
158 if (0 != setsockopt (h->fd, IPPROTO_TCP, TCP_NODELAY, &value, sizeof(value)))
\r
159 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
\r
167 * accept a new connection on a socket
\r
169 * @param desc bound socket
\r
170 * @param address address of the connecting peer, may be NULL
\r
171 * @param address_len length of address
\r
172 * @return client socket
\r
174 struct GNUNET_NETWORK_Handle *
\r
175 GNUNET_NETWORK_socket_accept (const struct GNUNET_NETWORK_Handle *desc,
\r
176 struct sockaddr *address,
\r
177 socklen_t * address_len)
\r
179 struct GNUNET_NETWORK_Handle *ret;
\r
181 ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle));
\r
182 ret->fd = accept (desc->fd, address, address_len);
\r
183 if (ret->fd == INVALID_SOCKET)
\r
186 SetErrnoFromWinsockError (WSAGetLastError ());
\r
192 if (ret->fd >= FD_SETSIZE)
\r
194 GNUNET_break (0 == close (ret->fd));
\r
200 if (GNUNET_SYSERR == socket_set_blocking (ret, GNUNET_NO))
\r
202 /* we might want to treat this one as fatal... */
\r
204 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret));
\r
208 if (GNUNET_OK != socket_set_inheritable (ret))
\r
209 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
\r
210 "socket_set_inheritable");
\r
213 socket_set_nosigpipe (ret);
\r
215 socket_set_nodelay (ret);
\r
220 * Bind to a connected socket
\r
221 * @param desc socket
\r
222 * @param address address to be bound
\r
223 * @param address_len length of address
\r
224 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
\r
227 GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc,
\r
228 const struct sockaddr *address,
\r
229 socklen_t address_len)
\r
233 ret = bind (desc->fd, address, address_len);
\r
235 if (SOCKET_ERROR == ret)
\r
236 SetErrnoFromWinsockError (WSAGetLastError ());
\r
238 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
\r
244 * @param desc socket
\r
245 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
\r
248 GNUNET_NETWORK_socket_close (struct GNUNET_NETWORK_Handle *desc)
\r
254 ret = closesocket (desc->fd);
\r
255 SetErrnoFromWinsockError (WSAGetLastError ());
\r
257 ret = close (desc->fd);
\r
260 GNUNET_free (desc);
\r
262 return (ret == 0) ? GNUNET_OK : GNUNET_SYSERR;
\r
267 * @param desc socket
\r
268 * @param address peer address
\r
269 * @param address_len length of address
\r
270 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
\r
273 GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Handle *desc,
\r
274 const struct sockaddr *address,
\r
275 socklen_t address_len)
\r
279 ret = connect (desc->fd, address, address_len);
\r
281 if (SOCKET_ERROR == ret)
\r
283 SetErrnoFromWinsockError (WSAGetLastError ());
\r
284 if (errno == EWOULDBLOCK)
\r
285 errno = EINPROGRESS;
\r
288 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
\r
292 * Get socket options
\r
294 * @param desc socket
\r
295 * @param level protocol level of the option
\r
296 * @param optname identifier of the option
\r
297 * @param optval options
\r
298 * @param optlen length of optval
\r
299 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
\r
302 GNUNET_NETWORK_socket_getsockopt (const struct GNUNET_NETWORK_Handle *desc,
\r
303 int level, int optname, void *optval,
\r
304 socklen_t * optlen)
\r
308 ret = getsockopt (desc->fd, level, optname, optval, optlen);
\r
310 if (ret == 0 && level == SOL_SOCKET && optname == SO_ERROR)
\r
311 *((int *) optval) = GetErrnoFromWinsockError (*((int *) optval));
\r
312 else if (SOCKET_ERROR == ret)
\r
313 SetErrnoFromWinsockError (WSAGetLastError ());
\r
315 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
\r
319 * Listen on a socket
\r
320 * @param desc socket
\r
321 * @param backlog length of the listen queue
\r
322 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
\r
325 GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Handle *desc,
\r
330 ret = listen (desc->fd, backlog);
\r
332 if (SOCKET_ERROR == ret)
\r
333 SetErrnoFromWinsockError (WSAGetLastError ());
\r
336 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
\r
340 * Read data from a connected socket (always non-blocking).
\r
341 * @param desc socket
\r
342 * @param buffer buffer
\r
343 * @param length length of buffer
\r
346 GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Handle * desc,
\r
347 void *buffer, size_t length)
\r
353 #ifdef MSG_DONTWAIT
\r
354 flags |= MSG_DONTWAIT;
\r
356 ret = recv (desc->fd, buffer, length, flags);
\r
358 if (SOCKET_ERROR == ret)
\r
359 SetErrnoFromWinsockError (WSAGetLastError ());
\r
366 * Send data (always non-blocking).
\r
368 * @param desc socket
\r
369 * @param buffer data to send
\r
370 * @param length size of the buffer
\r
371 * @return number of bytes sent, GNUNET_SYSERR on error
\r
374 GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Handle * desc,
\r
375 const void *buffer, size_t length)
\r
381 #ifdef MSG_DONTWAIT
\r
382 flags |= MSG_DONTWAIT;
\r
384 #ifdef MSG_NOSIGNAL
\r
385 flags |= MSG_NOSIGNAL;
\r
387 ret = send (desc->fd, buffer, length, flags);
\r
389 if (SOCKET_ERROR == ret)
\r
390 SetErrnoFromWinsockError (WSAGetLastError ());
\r
398 * Send data to a particular destination (always non-blocking).
\r
399 * This function only works for UDP sockets.
\r
401 * @param desc socket
\r
402 * @param message data to send
\r
403 * @param length size of the data
\r
404 * @param dest_addr destination address
\r
405 * @param dest_len length of address
\r
406 * @return number of bytes sent, GNUNET_SYSERR on error
\r
409 GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Handle * desc,
\r
410 const void *message, size_t length,
\r
411 const struct sockaddr * dest_addr,
\r
412 socklen_t dest_len)
\r
418 #ifdef MSG_DONTWAIT
\r
419 flags |= MSG_DONTWAIT;
\r
421 #ifdef MSG_NOSIGNAL
\r
422 flags |= MSG_NOSIGNAL;
\r
424 ret = sendto (desc->fd, message, length, flags, dest_addr, dest_len);
\r
426 if (SOCKET_ERROR == ret)
\r
427 SetErrnoFromWinsockError (WSAGetLastError ());
\r
434 * Set socket option
\r
436 * @param level protocol level of the option
\r
437 * @param option_name option identifier
\r
438 * @param option_value value to set
\r
439 * @param option_len size of option_value
\r
440 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
\r
443 GNUNET_NETWORK_socket_setsockopt (struct GNUNET_NETWORK_Handle *fd,
\r
444 int level, int option_name,
\r
445 const void *option_value,
\r
446 socklen_t option_len)
\r
450 ret = setsockopt (fd->fd, level, option_name, option_value, option_len);
\r
452 if (SOCKET_ERROR == ret)
\r
453 SetErrnoFromWinsockError (WSAGetLastError ());
\r
456 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
\r
462 * Create a new socket. Configure it for non-blocking IO and
\r
463 * mark it as non-inheritable to child processes (set the
\r
464 * close-on-exec flag).
\r
466 * @param domain domain of the socket
\r
467 * @param type socket type
\r
468 * @param protocol network protocol
\r
469 * @return new socket, NULL on error
\r
471 struct GNUNET_NETWORK_Handle *
\r
472 GNUNET_NETWORK_socket_create (int domain, int type, int protocol)
\r
474 struct GNUNET_NETWORK_Handle *ret;
\r
476 ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle));
\r
477 ret->fd = socket (domain, type, protocol);
\r
478 if (INVALID_SOCKET == ret->fd)
\r
481 SetErrnoFromWinsockError (WSAGetLastError ());
\r
487 if (ret->fd >= FD_SETSIZE)
\r
489 GNUNET_break (0 == close (ret->fd));
\r
496 if (GNUNET_SYSERR == socket_set_blocking (ret, GNUNET_NO))
\r
498 /* we might want to treat this one as fatal... */
\r
500 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret));
\r
504 if (GNUNET_OK != socket_set_inheritable (ret))
\r
505 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
\r
506 "socket_set_inheritable");
\r
509 socket_set_nosigpipe (ret);
\r
511 if (type == SOCK_STREAM)
\r
512 socket_set_nodelay (ret);
\r
518 * Shut down socket operations
\r
519 * @param desc socket
\r
520 * @param how type of shutdown
\r
521 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
\r
524 GNUNET_NETWORK_socket_shutdown (struct GNUNET_NETWORK_Handle *desc,
\r
529 ret = shutdown (desc->fd, how);
\r
532 SetErrnoFromWinsockError (WSAGetLastError ());
\r
535 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
\r
541 * @param fds fd set
\r
544 GNUNET_NETWORK_fdset_zero (struct GNUNET_NETWORK_FDSet *fds)
\r
546 FD_ZERO (&fds->sds);
\r
549 GNUNET_CONTAINER_slist_clear (fds->handles);
\r
555 * Add a socket to the FD set
\r
556 * @param fds fd set
\r
557 * @param desc socket to add
\r
560 GNUNET_NETWORK_fdset_set (struct GNUNET_NETWORK_FDSet *fds,
\r
561 const struct GNUNET_NETWORK_Handle *desc)
\r
563 FD_SET (desc->fd, &fds->sds);
\r
565 if (desc->fd + 1 > fds->nsds)
\r
566 fds->nsds = desc->fd + 1;
\r
571 * Check whether a socket is part of the fd set
\r
572 * @param fds fd set
\r
573 * @param desc socket
\r
574 * @return 0 if the FD is not set
\r
577 GNUNET_NETWORK_fdset_isset (const struct GNUNET_NETWORK_FDSet *fds,
\r
578 const struct GNUNET_NETWORK_Handle *desc)
\r
580 return FD_ISSET (desc->fd, &fds->sds);
\r
585 * Add one fd set to another
\r
586 * @param dst the fd set to add to
\r
587 * @param src the fd set to add from
\r
590 GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst,
\r
591 const struct GNUNET_NETWORK_FDSet *src)
\r
595 for (nfds = src->nsds; nfds > 0; nfds--)
\r
596 if (FD_ISSET (nfds, &src->sds))
\r
598 FD_SET (nfds, &dst->sds);
\r
599 if (nfds + 1 > dst->nsds)
\r
600 dst->nsds = nfds + 1;
\r
606 * Copy one fd set to another
\r
607 * @param to destination
\r
608 * @param from source
\r
611 GNUNET_NETWORK_fdset_copy (struct GNUNET_NETWORK_FDSet *to,
\r
612 const struct GNUNET_NETWORK_FDSet *from)
\r
614 FD_COPY (&from->sds, &to->sds);
\r
615 to->nsds = from->nsds;
\r
617 struct GNUNET_CONTAINER_SList_Iterator *iter;
\r
620 GNUNET_CONTAINER_slist_clear (to->handles);
\r
622 for (iter = GNUNET_CONTAINER_slist_begin (from->handles);
\r
623 GNUNET_CONTAINER_slist_end (iter) != GNUNET_YES; GNUNET_CONTAINER_slist_next (iter))
\r
628 handle = GNUNET_CONTAINER_slist_get (iter, &len);
\r
629 GNUNET_CONTAINER_slist_add (to->handles, GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT, handle, len);
\r
636 * Copy a native fd set
\r
637 * @param to destination
\r
638 * @param from native source set
\r
639 * @param nfds the biggest socket number in from + 1
\r
642 GNUNET_NETWORK_fdset_copy_native (struct GNUNET_NETWORK_FDSet *to,
\r
643 const fd_set * from, int nfds)
\r
645 FD_COPY (from, &to->sds);
\r
651 * Add a file handle to the fd set
\r
652 * @param fds fd set
\r
653 * @param h the file handle to add
\r
656 GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds,
\r
657 const struct GNUNET_DISK_FileHandle *h)
\r
662 GNUNET_DISK_internal_file_handle_ (h, &hw, sizeof (HANDLE));
\r
663 GNUNET_CONTAINER_slist_add (fds->handles, GNUNET_NO, &hw, sizeof (HANDLE));
\r
667 GNUNET_DISK_internal_file_handle_ (h, &fd, sizeof (int));
\r
668 FD_SET (fd, &fds->sds);
\r
669 if (fd + 1 > fds->nsds)
\r
670 fds->nsds = fd + 1;
\r
675 * Check if a file handle is part of an fd set
\r
676 * @param fds fd set
\r
677 * @param h file handle
\r
678 * @return GNUNET_YES if the file handle is part of the set
\r
681 GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds,
\r
682 const struct GNUNET_DISK_FileHandle *h)
\r
685 return GNUNET_CONTAINER_slist_contains (fds->handles, h->h, sizeof (HANDLE));
\r
687 return FD_ISSET (h->fd, &fds->sds);
\r
692 * Checks if two fd sets overlap
\r
693 * @param fds1 first fd set
\r
694 * @param fds2 second fd set
\r
695 * @return GNUNET_YES if they do overlap, GNUNET_NO otherwise
\r
698 GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1,
\r
699 const struct GNUNET_NETWORK_FDSet *fds2)
\r
704 if (nfds < fds2->nsds)
\r
707 for (; nfds >= 0; nfds--)
\r
708 if (FD_ISSET (nfds, &fds1->sds) && FD_ISSET (nfds, &fds2->sds))
\r
715 * Creates an fd set
\r
716 * @return a new fd set
\r
718 struct GNUNET_NETWORK_FDSet *
\r
719 GNUNET_NETWORK_fdset_create ()
\r
721 struct GNUNET_NETWORK_FDSet *fds;
\r
723 fds = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_FDSet));
\r
725 fds->handles = GNUNET_CONTAINER_slist_create ();
\r
727 GNUNET_NETWORK_fdset_zero (fds);
\r
733 * Releases the associated memory of an fd set
\r
734 * @param fds fd set
\r
737 GNUNET_NETWORK_fdset_destroy (struct GNUNET_NETWORK_FDSet *fds)
\r
740 GNUNET_CONTAINER_slist_destroy (fds->handles);
\r
747 * Check if sockets meet certain conditions
\r
748 * @param rfds set of sockets to be checked for readability
\r
749 * @param wfds set of sockets to be checked for writability
\r
750 * @param efds set of sockets to be checked for exceptions
\r
751 * @param timeout relative value when to return
\r
752 * @return number of selected sockets, GNUNET_SYSERR on error
\r
755 GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
\r
756 struct GNUNET_NETWORK_FDSet *wfds,
\r
757 struct GNUNET_NETWORK_FDSet *efds,
\r
758 const struct GNUNET_TIME_Relative timeout)
\r
766 nfds = GNUNET_MAX (nfds, wfds->nsds);
\r
768 nfds = GNUNET_MAX (nfds, efds->nsds);
\r
773 tv.tv_sec = timeout.value / GNUNET_TIME_UNIT_SECONDS.value;
\r
774 tv.tv_usec = 1000 * (timeout.value - (tv.tv_sec * GNUNET_TIME_UNIT_SECONDS.value));
\r
775 if ( (nfds == 0) &&
\r
776 (timeout.value == GNUNET_TIME_UNIT_FOREVER_REL.value) )
\r
778 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
\r
779 _("Fatal internal logic error, process hangs in `%s' (abort with CTRL-C)!\n"),
\r
783 return select (nfds + 1,
\r
784 (rfds != NULL) ? &rfds->sds : NULL,
\r
785 (wfds != NULL) ? &wfds->sds : NULL,
\r
786 (efds != NULL) ? &efds->sds : NULL,
\r
787 (timeout.value == GNUNET_TIME_UNIT_FOREVER_REL.value)
\r
792 fd_set sock_read, sock_write, sock_except;
\r
793 fd_set aread, awrite, aexcept;
\r
795 struct timeval tvslice;
\r
799 #define SAFE_FD_ISSET(fd, set) (set != NULL && FD_ISSET(fd, set))
\r
801 /* calculate how long we need to wait in milliseconds */
\r
802 if (timeout.value == GNUNET_TIME_UNIT_FOREVER_REL.value)
\r
803 ms_total = INFINITE;
\r
805 ms_total = timeout.value / GNUNET_TIME_UNIT_MILLISECONDS.value;
\r
807 /* select() may be used as a portable way to sleep */
\r
808 if (!(rfds || wfds || efds))
\r
815 sock_read = rfds->sds;
\r
817 FD_ZERO(&sock_read);
\r
820 sock_write = wfds->sds;
\r
822 FD_ZERO(&sock_write);
\r
825 sock_except = efds->sds;
\r
827 FD_ZERO(&sock_except);
\r
829 /* multiplex between winsock select() and waiting on the handles */
\r
833 FD_ZERO (&aexcept);
\r
835 limit = GetTickCount () + ms_total;
\r
842 /* overwrite the zero'd sets here; the select call
\r
843 * will clear those that are not active */
\r
845 FD_COPY (&sock_read, &aread);
\r
846 FD_COPY (&sock_write, &awrite);
\r
847 FD_COPY (&sock_except, &aexcept);
\r
849 tvslice.tv_sec = 0;
\r
850 tvslice.tv_usec = 100000;
\r
853 select (nfds + 1, &aread, &awrite, &aexcept,
\r
854 &tvslice)) == SOCKET_ERROR)
\r
856 SetErrnoFromWinsockError (WSAGetLastError ());
\r
857 if (errno == ENOTSOCK)
\r
861 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "select");
\r
864 goto select_loop_end;
\r
868 /* Poll read pipes */
\r
871 struct GNUNET_CONTAINER_SList_Iterator *i;
\r
874 on_next = GNUNET_NO;
\r
875 for (i = GNUNET_CONTAINER_slist_begin (rfds->handles);
\r
876 GNUNET_CONTAINER_slist_end (i) != GNUNET_YES;
\r
877 on_next || GNUNET_CONTAINER_slist_next (i))
\r
882 h = *(HANDLE *) GNUNET_CONTAINER_slist_get (i, NULL);
\r
883 on_next = GNUNET_NO;
\r
885 if (!PeekNamedPipe (h, NULL, 0, NULL, &dwBytes, NULL))
\r
887 GNUNET_CONTAINER_slist_erase (i);
\r
888 on_next = GNUNET_YES;
\r
891 SetErrnoFromWinError (GetLastError ());
\r
893 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "PeekNamedPipe");
\r
895 goto select_loop_end;
\r
903 GNUNET_CONTAINER_slist_erase (i);
\r
904 on_next = GNUNET_YES;
\r
909 /* Poll for faulty pipes */
\r
912 struct GNUNET_CONTAINER_SList_Iterator *i;
\r
915 on_next = GNUNET_NO;
\r
916 for (i = GNUNET_CONTAINER_slist_begin (efds->handles);
\r
917 GNUNET_CONTAINER_slist_end (i) != GNUNET_YES;
\r
918 on_next || GNUNET_CONTAINER_slist_next (i))
\r
923 h = *(HANDLE *) GNUNET_CONTAINER_slist_get (i, NULL);
\r
925 if (PeekNamedPipe (h, NULL, 0, NULL, &dwBytes, NULL))
\r
927 GNUNET_CONTAINER_slist_erase (i);
\r
928 on_next = GNUNET_YES;
\r
933 on_next = GNUNET_NO;
\r
939 GNUNET_assert (GNUNET_CONTAINER_slist_count (wfds->handles) == 0);
\r
941 /* Check for closed sockets */
\r
942 for (i = 0; i < nfds; i++)
\r
944 if (SAFE_FD_ISSET (i, &sock_read))
\r
946 struct sockaddr addr;
\r
949 if (getpeername (i, &addr, &len) == SOCKET_ERROR)
\r
953 len = sizeof (err);
\r
955 (i, SOL_SOCKET, SO_ERROR, (char *) &err, &len) == 0
\r
956 && err == WSAENOTCONN)
\r
958 if (!SAFE_FD_ISSET (i, &aread))
\r
960 FD_SET (i, &aread);
\r
970 while (retcode == 0 && (ms_total == INFINITE || GetTickCount () < limit));
\r
976 GNUNET_NETWORK_fdset_zero (rfds);
\r
977 GNUNET_NETWORK_fdset_copy_native (rfds, &aread, retcode);
\r
982 GNUNET_NETWORK_fdset_zero (wfds);
\r
983 GNUNET_NETWORK_fdset_copy_native (wfds, &awrite, retcode);
\r
988 GNUNET_NETWORK_fdset_zero (efds);
\r
989 GNUNET_NETWORK_fdset_copy_native (efds, &aexcept, retcode);
\r
998 /* end of network.c */
\r