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 setsockopt (h->fd, SOL_SOCKET, SO_NOSIGPIPE, &value, sizeof(value));
\r
145 * Disable delays when sending data via the socket.
\r
146 * (GNUnet makes sure that messages are as big as
\r
147 * possible already).
\r
149 * @param h the socket to make non-delaying
\r
152 socket_set_nodelay (const struct GNUNET_NETWORK_Handle
\r
156 setsockopt (h->fd, IPPROTO_TCP, TCP_NODELAY, &value, sizeof(value));
\r
162 * accept a new connection on a socket
\r
164 * @param desc bound socket
\r
165 * @param address address of the connecting peer, may be NULL
\r
166 * @param address_len length of address
\r
167 * @return client socket
\r
169 struct GNUNET_NETWORK_Handle *
\r
170 GNUNET_NETWORK_socket_accept (const struct GNUNET_NETWORK_Handle *desc,
\r
171 struct sockaddr *address,
\r
172 socklen_t * address_len)
\r
174 struct GNUNET_NETWORK_Handle *ret;
\r
176 ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle));
\r
177 ret->fd = accept (desc->fd, address, address_len);
\r
178 if (ret->fd == INVALID_SOCKET)
\r
181 SetErrnoFromWinsockError (WSAGetLastError ());
\r
187 if (ret->fd >= FD_SETSIZE)
\r
189 GNUNET_break (0 == close (ret->fd));
\r
195 if (GNUNET_SYSERR == socket_set_blocking (ret, GNUNET_NO))
\r
197 /* we might want to treat this one as fatal... */
\r
199 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret));
\r
203 if (GNUNET_OK != socket_set_inheritable (ret))
\r
204 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
\r
205 "socket_set_inheritable");
\r
208 socket_set_nosigpipe (ret);
\r
210 socket_set_nodelay (ret);
\r
215 * Bind to a connected socket
\r
216 * @param desc socket
\r
217 * @param address address to be bound
\r
218 * @param address_len length of address
\r
219 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
\r
222 GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc,
\r
223 const struct sockaddr *address,
\r
224 socklen_t address_len)
\r
228 ret = bind (desc->fd, address, address_len);
\r
230 if (SOCKET_ERROR == ret)
\r
231 SetErrnoFromWinsockError (WSAGetLastError ());
\r
233 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
\r
239 * @param desc socket
\r
240 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
\r
243 GNUNET_NETWORK_socket_close (struct GNUNET_NETWORK_Handle *desc)
\r
249 ret = closesocket (desc->fd);
\r
250 SetErrnoFromWinsockError (WSAGetLastError ());
\r
252 ret = close (desc->fd);
\r
255 GNUNET_free (desc);
\r
257 return (ret == 0) ? GNUNET_OK : GNUNET_SYSERR;
\r
262 * @param desc socket
\r
263 * @param address peer address
\r
264 * @param address_len length of address
\r
265 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
\r
268 GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Handle *desc,
\r
269 const struct sockaddr *address,
\r
270 socklen_t address_len)
\r
274 ret = connect (desc->fd, address, address_len);
\r
276 if (SOCKET_ERROR == ret)
\r
278 SetErrnoFromWinsockError (WSAGetLastError ());
\r
279 if (errno == EWOULDBLOCK)
\r
280 errno = EINPROGRESS;
\r
283 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
\r
287 * Get socket options
\r
289 * @param desc socket
\r
290 * @param level protocol level of the option
\r
291 * @param optname identifier of the option
\r
292 * @param optval options
\r
293 * @param optlen length of optval
\r
294 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
\r
297 GNUNET_NETWORK_socket_getsockopt (const struct GNUNET_NETWORK_Handle *desc,
\r
298 int level, int optname, void *optval,
\r
299 socklen_t * optlen)
\r
303 ret = getsockopt (desc->fd, level, optname, optval, optlen);
\r
305 if (ret == 0 && level == SOL_SOCKET && optname == SO_ERROR)
\r
306 *((int *) optval) = GetErrnoFromWinsockError (*((int *) optval));
\r
307 else if (SOCKET_ERROR == ret)
\r
308 SetErrnoFromWinsockError (WSAGetLastError ());
\r
310 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
\r
314 * Listen on a socket
\r
315 * @param desc socket
\r
316 * @param backlog length of the listen queue
\r
317 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
\r
320 GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Handle *desc,
\r
325 ret = listen (desc->fd, backlog);
\r
327 if (SOCKET_ERROR == ret)
\r
328 SetErrnoFromWinsockError (WSAGetLastError ());
\r
331 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
\r
335 * Read data from a connected socket (always non-blocking).
\r
336 * @param desc socket
\r
337 * @param buffer buffer
\r
338 * @param length length of buffer
\r
341 GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Handle * desc,
\r
342 void *buffer, size_t length)
\r
348 #ifdef MSG_DONTWAIT
\r
349 flags |= MSG_DONTWAIT;
\r
351 ret = recv (desc->fd, buffer, length, flags);
\r
353 if (SOCKET_ERROR == ret)
\r
354 SetErrnoFromWinsockError (WSAGetLastError ());
\r
361 * Send data (always non-blocking).
\r
363 * @param desc socket
\r
364 * @param buffer data to send
\r
365 * @param length size of the buffer
\r
366 * @return number of bytes sent, GNUNET_SYSERR on error
\r
369 GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Handle * desc,
\r
370 const void *buffer, size_t length)
\r
376 #ifdef MSG_DONTWAIT
\r
377 flags |= MSG_DONTWAIT;
\r
379 #ifdef MSG_NOSIGNAL
\r
380 flags |= MSG_NOSIGNAL;
\r
382 ret = send (desc->fd, buffer, length, flags);
\r
384 if (SOCKET_ERROR == ret)
\r
385 SetErrnoFromWinsockError (WSAGetLastError ());
\r
393 * Send data to a particular destination (always non-blocking).
\r
394 * This function only works for UDP sockets.
\r
396 * @param desc socket
\r
397 * @param message data to send
\r
398 * @param length size of the data
\r
399 * @param dest_addr destination address
\r
400 * @param dest_len length of address
\r
401 * @return number of bytes sent, GNUNET_SYSERR on error
\r
404 GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Handle * desc,
\r
405 const void *message, size_t length,
\r
406 const struct sockaddr * dest_addr,
\r
407 socklen_t dest_len)
\r
413 #ifdef MSG_DONTWAIT
\r
414 flags |= MSG_DONTWAIT;
\r
416 #ifdef MSG_NOSIGNAL
\r
417 flags |= MSG_NOSIGNAL;
\r
419 ret = sendto (desc->fd, message, length, flags, dest_addr, dest_len);
\r
421 if (SOCKET_ERROR == ret)
\r
422 SetErrnoFromWinsockError (WSAGetLastError ());
\r
429 * Set socket option
\r
431 * @param level protocol level of the option
\r
432 * @param option_name option identifier
\r
433 * @param option_value value to set
\r
434 * @param option_len size of option_value
\r
435 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
\r
438 GNUNET_NETWORK_socket_setsockopt (struct GNUNET_NETWORK_Handle *fd,
\r
439 int level, int option_name,
\r
440 const void *option_value,
\r
441 socklen_t option_len)
\r
445 ret = setsockopt (fd->fd, level, option_name, option_value, option_len);
\r
447 if (SOCKET_ERROR == ret)
\r
448 SetErrnoFromWinsockError (WSAGetLastError ());
\r
451 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
\r
457 * Create a new socket. Configure it for non-blocking IO and
\r
458 * mark it as non-inheritable to child processes (set the
\r
459 * close-on-exec flag).
\r
461 * @param domain domain of the socket
\r
462 * @param type socket type
\r
463 * @param protocol network protocol
\r
464 * @return new socket, NULL on error
\r
466 struct GNUNET_NETWORK_Handle *
\r
467 GNUNET_NETWORK_socket_create (int domain, int type, int protocol)
\r
469 struct GNUNET_NETWORK_Handle *ret;
\r
471 ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle));
\r
472 ret->fd = socket (domain, type, protocol);
\r
473 if (INVALID_SOCKET == ret->fd)
\r
476 SetErrnoFromWinsockError (WSAGetLastError ());
\r
482 if (ret->fd >= FD_SETSIZE)
\r
484 GNUNET_break (0 == close (ret->fd));
\r
491 if (GNUNET_SYSERR == socket_set_blocking (ret, GNUNET_NO))
\r
493 /* we might want to treat this one as fatal... */
\r
495 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret));
\r
499 if (GNUNET_OK != socket_set_inheritable (ret))
\r
500 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
\r
501 "socket_set_inheritable");
\r
504 socket_set_nosigpipe (ret);
\r
506 if (type == SOCK_STREAM)
\r
507 socket_set_nodelay (ret);
\r
513 * Shut down socket operations
\r
514 * @param desc socket
\r
515 * @param how type of shutdown
\r
516 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
\r
519 GNUNET_NETWORK_socket_shutdown (struct GNUNET_NETWORK_Handle *desc,
\r
524 ret = shutdown (desc->fd, how);
\r
527 SetErrnoFromWinsockError (WSAGetLastError ());
\r
530 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
\r
536 * @param fds fd set
\r
539 GNUNET_NETWORK_fdset_zero (struct GNUNET_NETWORK_FDSet *fds)
\r
541 FD_ZERO (&fds->sds);
\r
544 GNUNET_CONTAINER_slist_clear (fds->handles);
\r
550 * Add a socket to the FD set
\r
551 * @param fds fd set
\r
552 * @param desc socket to add
\r
555 GNUNET_NETWORK_fdset_set (struct GNUNET_NETWORK_FDSet *fds,
\r
556 const struct GNUNET_NETWORK_Handle *desc)
\r
558 FD_SET (desc->fd, &fds->sds);
\r
560 if (desc->fd + 1 > fds->nsds)
\r
561 fds->nsds = desc->fd + 1;
\r
566 * Check whether a socket is part of the fd set
\r
567 * @param fds fd set
\r
568 * @param desc socket
\r
569 * @return 0 if the FD is not set
\r
572 GNUNET_NETWORK_fdset_isset (const struct GNUNET_NETWORK_FDSet *fds,
\r
573 const struct GNUNET_NETWORK_Handle *desc)
\r
575 return FD_ISSET (desc->fd, &fds->sds);
\r
580 * Add one fd set to another
\r
581 * @param dst the fd set to add to
\r
582 * @param src the fd set to add from
\r
585 GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst,
\r
586 const struct GNUNET_NETWORK_FDSet *src)
\r
590 for (nfds = src->nsds; nfds > 0; nfds--)
\r
591 if (FD_ISSET (nfds, &src->sds))
\r
593 FD_SET (nfds, &dst->sds);
\r
594 if (nfds + 1 > dst->nsds)
\r
595 dst->nsds = nfds + 1;
\r
601 * Copy one fd set to another
\r
602 * @param to destination
\r
603 * @param from source
\r
606 GNUNET_NETWORK_fdset_copy (struct GNUNET_NETWORK_FDSet *to,
\r
607 const struct GNUNET_NETWORK_FDSet *from)
\r
609 FD_COPY (&from->sds, &to->sds);
\r
610 to->nsds = from->nsds;
\r
612 struct GNUNET_CONTAINER_SList_Iterator *iter;
\r
615 GNUNET_CONTAINER_slist_clear (to->handles);
\r
617 for (iter = GNUNET_CONTAINER_slist_begin (from->handles);
\r
618 GNUNET_CONTAINER_slist_end (iter) != GNUNET_YES; GNUNET_CONTAINER_slist_next (iter))
\r
623 handle = GNUNET_CONTAINER_slist_get (iter, &len);
\r
624 GNUNET_CONTAINER_slist_add (to->handles, GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT, handle, len);
\r
631 * Copy a native fd set
\r
632 * @param to destination
\r
633 * @param from native source set
\r
634 * @param nfds the biggest socket number in from + 1
\r
637 GNUNET_NETWORK_fdset_copy_native (struct GNUNET_NETWORK_FDSet *to,
\r
638 const fd_set * from, int nfds)
\r
640 FD_COPY (from, &to->sds);
\r
646 * Add a file handle to the fd set
\r
647 * @param fds fd set
\r
648 * @param h the file handle to add
\r
651 GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds,
\r
652 const struct GNUNET_DISK_FileHandle *h)
\r
657 GNUNET_DISK_internal_file_handle_ (h, &hw, sizeof (HANDLE));
\r
658 GNUNET_CONTAINER_slist_add (fds->handles, GNUNET_NO, &hw, sizeof (HANDLE));
\r
662 GNUNET_DISK_internal_file_handle_ (h, &fd, sizeof (int));
\r
663 FD_SET (fd, &fds->sds);
\r
664 if (fd + 1 > fds->nsds)
\r
665 fds->nsds = fd + 1;
\r
670 * Check if a file handle is part of an fd set
\r
671 * @param fds fd set
\r
672 * @param h file handle
\r
673 * @return GNUNET_YES if the file handle is part of the set
\r
676 GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds,
\r
677 const struct GNUNET_DISK_FileHandle *h)
\r
680 return GNUNET_CONTAINER_slist_contains (fds->handles, h->h, sizeof (HANDLE));
\r
682 return FD_ISSET (h->fd, &fds->sds);
\r
687 * Checks if two fd sets overlap
\r
688 * @param fds1 first fd set
\r
689 * @param fds2 second fd set
\r
690 * @return GNUNET_YES if they do overlap, GNUNET_NO otherwise
\r
693 GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1,
\r
694 const struct GNUNET_NETWORK_FDSet *fds2)
\r
699 if (nfds < fds2->nsds)
\r
702 for (; nfds >= 0; nfds--)
\r
703 if (FD_ISSET (nfds, &fds1->sds) && FD_ISSET (nfds, &fds2->sds))
\r
710 * Creates an fd set
\r
711 * @return a new fd set
\r
713 struct GNUNET_NETWORK_FDSet *
\r
714 GNUNET_NETWORK_fdset_create ()
\r
716 struct GNUNET_NETWORK_FDSet *fds;
\r
718 fds = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_FDSet));
\r
720 fds->handles = GNUNET_CONTAINER_slist_create ();
\r
722 GNUNET_NETWORK_fdset_zero (fds);
\r
728 * Releases the associated memory of an fd set
\r
729 * @param fds fd set
\r
732 GNUNET_NETWORK_fdset_destroy (struct GNUNET_NETWORK_FDSet *fds)
\r
735 GNUNET_CONTAINER_slist_destroy (fds->handles);
\r
742 * Check if sockets meet certain conditions
\r
743 * @param rfds set of sockets to be checked for readability
\r
744 * @param wfds set of sockets to be checked for writability
\r
745 * @param efds set of sockets to be checked for exceptions
\r
746 * @param timeout relative value when to return
\r
747 * @return number of selected sockets, GNUNET_SYSERR on error
\r
750 GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
\r
751 struct GNUNET_NETWORK_FDSet *wfds,
\r
752 struct GNUNET_NETWORK_FDSet *efds,
\r
753 const struct GNUNET_TIME_Relative timeout)
\r
761 nfds = GNUNET_MAX (nfds, wfds->nsds);
\r
763 nfds = GNUNET_MAX (nfds, efds->nsds);
\r
768 tv.tv_sec = timeout.value / GNUNET_TIME_UNIT_SECONDS.value;
\r
769 tv.tv_usec = 1000 * (timeout.value - (tv.tv_sec * GNUNET_TIME_UNIT_SECONDS.value));
\r
770 if ( (nfds == 0) &&
\r
771 (timeout.value == GNUNET_TIME_UNIT_FOREVER_REL.value) )
\r
773 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
\r
774 _("Fatal internal logic error, process hangs in `%s' (abort with CTRL-C)!\n"),
\r
778 return select (nfds + 1,
\r
779 (rfds != NULL) ? &rfds->sds : NULL,
\r
780 (wfds != NULL) ? &wfds->sds : NULL,
\r
781 (efds != NULL) ? &efds->sds : NULL,
\r
782 (timeout.value == GNUNET_TIME_UNIT_FOREVER_REL.value)
\r
787 fd_set sock_read, sock_write, sock_except;
\r
788 fd_set aread, awrite, aexcept;
\r
790 struct timeval tvslice;
\r
794 #define SAFE_FD_ISSET(fd, set) (set != NULL && FD_ISSET(fd, set))
\r
796 /* calculate how long we need to wait in milliseconds */
\r
797 if (timeout.value == GNUNET_TIME_UNIT_FOREVER_REL.value)
\r
798 ms_total = INFINITE;
\r
800 ms_total = timeout.value / GNUNET_TIME_UNIT_MILLISECONDS.value;
\r
802 /* select() may be used as a portable way to sleep */
\r
803 if (!(rfds || wfds || efds))
\r
810 sock_read = rfds->sds;
\r
812 FD_ZERO(&sock_read);
\r
815 sock_write = wfds->sds;
\r
817 FD_ZERO(&sock_write);
\r
820 sock_except = efds->sds;
\r
822 FD_ZERO(&sock_except);
\r
824 /* multiplex between winsock select() and waiting on the handles */
\r
828 FD_ZERO (&aexcept);
\r
830 limit = GetTickCount () + ms_total;
\r
837 /* overwrite the zero'd sets here; the select call
\r
838 * will clear those that are not active */
\r
840 FD_COPY (&sock_read, &aread);
\r
841 FD_COPY (&sock_write, &awrite);
\r
842 FD_COPY (&sock_except, &aexcept);
\r
844 tvslice.tv_sec = 0;
\r
845 tvslice.tv_usec = 100000;
\r
848 select (nfds + 1, &aread, &awrite, &aexcept,
\r
849 &tvslice)) == SOCKET_ERROR)
\r
851 SetErrnoFromWinsockError (WSAGetLastError ());
\r
852 if (errno == ENOTSOCK)
\r
856 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "select");
\r
859 goto select_loop_end;
\r
863 /* Poll read pipes */
\r
866 struct GNUNET_CONTAINER_SList_Iterator *i;
\r
869 on_next = GNUNET_NO;
\r
870 for (i = GNUNET_CONTAINER_slist_begin (rfds->handles);
\r
871 GNUNET_CONTAINER_slist_end (i) != GNUNET_YES;
\r
872 on_next || GNUNET_CONTAINER_slist_next (i))
\r
877 h = *(HANDLE *) GNUNET_CONTAINER_slist_get (i, NULL);
\r
878 on_next = GNUNET_NO;
\r
880 if (!PeekNamedPipe (h, NULL, 0, NULL, &dwBytes, NULL))
\r
882 GNUNET_CONTAINER_slist_erase (i);
\r
883 on_next = GNUNET_YES;
\r
886 SetErrnoFromWinError (GetLastError ());
\r
888 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "PeekNamedPipe");
\r
890 goto select_loop_end;
\r
898 GNUNET_CONTAINER_slist_erase (i);
\r
899 on_next = GNUNET_YES;
\r
904 /* Poll for faulty pipes */
\r
907 struct GNUNET_CONTAINER_SList_Iterator *i;
\r
910 on_next = GNUNET_NO;
\r
911 for (i = GNUNET_CONTAINER_slist_begin (efds->handles);
\r
912 GNUNET_CONTAINER_slist_end (i) != GNUNET_YES;
\r
913 on_next || GNUNET_CONTAINER_slist_next (i))
\r
918 h = *(HANDLE *) GNUNET_CONTAINER_slist_get (i, NULL);
\r
920 if (PeekNamedPipe (h, NULL, 0, NULL, &dwBytes, NULL))
\r
922 GNUNET_CONTAINER_slist_erase (i);
\r
923 on_next = GNUNET_YES;
\r
928 on_next = GNUNET_NO;
\r
934 GNUNET_assert (GNUNET_CONTAINER_slist_count (wfds->handles) == 0);
\r
936 /* Check for closed sockets */
\r
937 for (i = 0; i < nfds; i++)
\r
939 if (SAFE_FD_ISSET (i, &sock_read))
\r
941 struct sockaddr addr;
\r
944 if (getpeername (i, &addr, &len) == SOCKET_ERROR)
\r
948 len = sizeof (err);
\r
950 (i, SOL_SOCKET, SO_ERROR, (char *) &err, &len) == 0
\r
951 && err == WSAENOTCONN)
\r
953 if (!SAFE_FD_ISSET (i, &aread))
\r
955 FD_SET (i, &aread);
\r
965 while (retcode == 0 && (ms_total == INFINITE || GetTickCount () < limit));
\r
971 GNUNET_NETWORK_fdset_zero (rfds);
\r
972 GNUNET_NETWORK_fdset_copy_native (rfds, &aread, retcode);
\r
977 GNUNET_NETWORK_fdset_zero (wfds);
\r
978 GNUNET_NETWORK_fdset_copy_native (wfds, &awrite, retcode);
\r
983 GNUNET_NETWORK_fdset_zero (efds);
\r
984 GNUNET_NETWORK_fdset_copy_native (efds, &aexcept, retcode);
\r
993 /* end of network.c */
\r