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 struct GNUNET_NETWORK_Handle
\r
39 struct GNUNET_NETWORK_FDSet
\r
41 /* socket descriptors */
\r
46 struct GNUNET_CONTAINER_SList *handles;
\r
51 #define FD_COPY(s, d) (memcpy ((d), (s), sizeof (fd_set)))
\r
55 * accept a new connection on a socket
\r
57 * @param desc bound socket
\r
58 * @param address address of the connecting peer, may be NULL
\r
59 * @param address_len length of address
\r
60 * @return client socket
\r
62 struct GNUNET_NETWORK_Handle *
\r
63 GNUNET_NETWORK_socket_accept (const struct GNUNET_NETWORK_Handle *desc,
\r
64 struct sockaddr *address,
\r
65 socklen_t * address_len)
\r
67 struct GNUNET_NETWORK_Handle *ret;
\r
69 ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle));
\r
70 ret->fd = accept (desc->fd, address, address_len);
\r
72 if (INVALID_SOCKET == ret->fd)
\r
73 SetErrnoFromWinsockError (WSAGetLastError ());
\r
79 * Bind to a connected socket
\r
80 * @param desc socket
\r
81 * @param address address to be bound
\r
82 * @param address_len length of address
\r
83 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
\r
86 GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc,
\r
87 const struct sockaddr *address,
\r
88 socklen_t address_len)
\r
92 ret = bind (desc->fd, address, address_len);
\r
94 if (SOCKET_ERROR == ret)
\r
95 SetErrnoFromWinsockError (WSAGetLastError ());
\r
97 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
\r
101 * Set if a socket should use blocking or non-blocking IO.
\r
103 * @param doBlock blocking mode
\r
104 * @return GNUNET_OK on success, GNUNET_SYSERR on error
\r
107 GNUNET_NETWORK_socket_set_blocking (struct GNUNET_NETWORK_Handle *fd,
\r
113 if (ioctlsocket (fd->fd, FIONBIO, &mode) == SOCKET_ERROR)
\r
115 SetErrnoFromWinsockError (WSAGetLastError ());
\r
116 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "ioctlsocket");
\r
117 return GNUNET_SYSERR;
\r
123 int flags = fcntl (fd->fd, F_GETFL);
\r
126 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "fcntl");
\r
127 return GNUNET_SYSERR;
\r
130 flags &= ~O_NONBLOCK;
\r
132 flags |= O_NONBLOCK;
\r
133 if (0 != fcntl (fd->fd, F_SETFL, flags))
\r
135 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "fcntl");
\r
136 return GNUNET_SYSERR;
\r
144 * @param desc socket
\r
145 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
\r
148 GNUNET_NETWORK_socket_close (struct GNUNET_NETWORK_Handle *desc)
\r
152 ret = closesocket (desc->fd);
\r
153 SetErrnoFromWinsockError (WSAGetLastError ());
\r
155 ret = close (desc->fd);
\r
158 GNUNET_free (desc);
\r
159 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
\r
164 * @param desc socket
\r
165 * @param address peer address
\r
166 * @param address_len length of address
\r
167 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
\r
170 GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Handle *desc,
\r
171 const struct sockaddr *address,
\r
172 socklen_t address_len)
\r
176 ret = connect (desc->fd, address, address_len);
\r
178 if (SOCKET_ERROR == ret)
\r
180 SetErrnoFromWinsockError (WSAGetLastError ());
\r
181 if (errno == EWOULDBLOCK)
\r
182 errno = EINPROGRESS;
\r
185 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
\r
189 * Get socket options
\r
190 * @param desc socket
\r
191 * @param level protocol level of the option
\r
192 * @param optname identifier of the option
\r
193 * @param optval options
\r
194 * @param optlen length of optval
\r
195 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
\r
198 GNUNET_NETWORK_socket_getsockopt (const struct GNUNET_NETWORK_Handle *desc,
\r
199 int level, int optname, void *optval,
\r
200 socklen_t * optlen)
\r
204 ret = getsockopt (desc->fd, level, optname, optval, optlen);
\r
206 if (ret == 0 && level == SOL_SOCKET && optname == SO_ERROR)
\r
207 *((int *) optval) = GetErrnoFromWinsockError (*((int *) optval));
\r
208 else if (SOCKET_ERROR == ret)
\r
209 SetErrnoFromWinsockError (WSAGetLastError ());
\r
211 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
\r
215 * Listen on a socket
\r
216 * @param desc socket
\r
217 * @param backlog length of the listen queue
\r
218 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
\r
221 GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Handle *desc,
\r
226 ret = listen (desc->fd, backlog);
\r
228 if (SOCKET_ERROR == ret)
\r
229 SetErrnoFromWinsockError (WSAGetLastError ());
\r
232 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
\r
236 * Read data from a connected socket
\r
237 * @param desc socket
\r
238 * @param buffer buffer
\r
239 * @param length length of buffer
\r
240 * @param flags type of message reception
\r
243 GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Handle * desc,
\r
244 void *buffer, size_t length, int flags)
\r
248 ret = recv (desc->fd, buffer, length, flags);
\r
250 if (SOCKET_ERROR == ret)
\r
251 SetErrnoFromWinsockError (WSAGetLastError ());
\r
259 * @param desc socket
\r
260 * @param buffer data to send
\r
261 * @param length size of the buffer
\r
262 * @param flags type of message transmission
\r
263 * @return number of bytes sent, GNUNET_SYSERR on error
\r
266 GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Handle * desc,
\r
267 const void *buffer, size_t length, int flags)
\r
271 ret = send (desc->fd, buffer, length, flags);
\r
273 if (SOCKET_ERROR == ret)
\r
274 SetErrnoFromWinsockError (WSAGetLastError ());
\r
282 * @param desc socket
\r
283 * @param message data to send
\r
284 * @param length size of the data
\r
285 * @param flags type of message transmission
\r
286 * @param dest_addr destination address
\r
287 * @param dest_len length of address
\r
288 * @return number of bytes sent, GNUNET_SYSERR on error
\r
291 GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Handle * desc,
\r
292 const void *message, size_t length, int flags,
\r
293 const struct sockaddr * dest_addr,
\r
294 socklen_t dest_len)
\r
298 ret = sendto (desc->fd, message, length, flags, dest_addr, dest_len);
\r
300 if (SOCKET_ERROR == ret)
\r
301 SetErrnoFromWinsockError (WSAGetLastError ());
\r
308 * Set socket option
\r
310 * @param level protocol level of the option
\r
311 * @param option_name option identifier
\r
312 * @param option_value value to set
\r
313 * @param option_len size of option_value
\r
314 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
\r
317 GNUNET_NETWORK_socket_setsockopt (struct GNUNET_NETWORK_Handle *fd,
\r
318 int level, int option_name,
\r
319 const void *option_value,
\r
320 socklen_t option_len)
\r
324 ret = setsockopt (fd->fd, level, option_name, option_value, option_len);
\r
326 if (SOCKET_ERROR == ret)
\r
327 SetErrnoFromWinsockError (WSAGetLastError ());
\r
330 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
\r
334 * Create a new socket
\r
335 * @param domain domain of the socket
\r
336 * @param type socket type
\r
337 * @param protocol network protocol
\r
338 * @return new socket, NULL on error
\r
340 struct GNUNET_NETWORK_Handle *
\r
341 GNUNET_NETWORK_socket_socket (int domain, int type, int protocol)
\r
343 struct GNUNET_NETWORK_Handle *ret;
\r
345 ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle));
\r
346 ret->fd = socket (domain, type, protocol);
\r
348 if (INVALID_SOCKET == ret->fd)
\r
349 SetErrnoFromWinsockError (WSAGetLastError ());
\r
362 * Shut down socket operations
\r
363 * @param desc socket
\r
364 * @param how type of shutdown
\r
365 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
\r
368 GNUNET_NETWORK_socket_shutdown (struct GNUNET_NETWORK_Handle *desc,
\r
373 ret = shutdown (desc->fd, how);
\r
376 SetErrnoFromWinsockError (WSAGetLastError ());
\r
379 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
\r
383 * Make a non-inheritable to child processes
\r
385 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
\r
386 * @warning Not implemented on Windows
\r
389 GNUNET_NETWORK_socket_set_inheritable (const struct GNUNET_NETWORK_Handle
\r
394 return GNUNET_SYSERR;
\r
396 return fcntl (desc->fd, F_SETFD,
\r
398 F_GETFD) | FD_CLOEXEC) ==
\r
399 0 ? GNUNET_OK : GNUNET_SYSERR;
\r
406 * @param fds fd set
\r
409 GNUNET_NETWORK_fdset_zero (struct GNUNET_NETWORK_FDSet *fds)
\r
411 FD_ZERO (&fds->sds);
\r
414 GNUNET_CONTAINER_slist_clear (fds->handles);
\r
420 * Add a socket to the FD set
\r
421 * @param fds fd set
\r
422 * @param desc socket to add
\r
425 GNUNET_NETWORK_fdset_set (struct GNUNET_NETWORK_FDSet *fds,
\r
426 const struct GNUNET_NETWORK_Handle *desc)
\r
428 FD_SET (desc->fd, &fds->sds);
\r
430 if (desc->fd + 1 > fds->nsds)
\r
431 fds->nsds = desc->fd + 1;
\r
436 * Check whether a socket is part of the fd set
\r
437 * @param fds fd set
\r
438 * @param desc socket
\r
439 * @return 0 if the FD is not set
\r
442 GNUNET_NETWORK_fdset_isset (const struct GNUNET_NETWORK_FDSet *fds,
\r
443 const struct GNUNET_NETWORK_Handle *desc)
\r
445 return FD_ISSET (desc->fd, &fds->sds);
\r
450 * Add one fd set to another
\r
451 * @param dst the fd set to add to
\r
452 * @param src the fd set to add from
\r
455 GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst,
\r
456 const struct GNUNET_NETWORK_FDSet *src)
\r
460 for (nfds = src->nsds; nfds > 0; nfds--)
\r
461 if (FD_ISSET (nfds, &src->sds))
\r
463 FD_SET (nfds, &dst->sds);
\r
464 if (nfds + 1 > dst->nsds)
\r
465 dst->nsds = nfds + 1;
\r
471 * Copy one fd set to another
\r
472 * @param to destination
\r
473 * @param from source
\r
476 GNUNET_NETWORK_fdset_copy (struct GNUNET_NETWORK_FDSet *to,
\r
477 const struct GNUNET_NETWORK_FDSet *from)
\r
479 FD_COPY (&from->sds, &to->sds);
\r
480 to->nsds = from->nsds;
\r
482 struct GNUNET_CONTAINER_SList_Iterator *iter;
\r
485 GNUNET_CONTAINER_slist_clear (to->handles);
\r
487 for (iter = GNUNET_CONTAINER_slist_begin (from->handles);
\r
488 GNUNET_CONTAINER_slist_end (iter) != GNUNET_YES; GNUNET_CONTAINER_slist_next (iter))
\r
493 handle = GNUNET_CONTAINER_slist_get (iter, &len);
\r
494 GNUNET_CONTAINER_slist_add (to->handles, GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT, handle, len);
\r
501 * Copy a native fd set
\r
502 * @param to destination
\r
503 * @param from native source set
\r
504 * @param nfds the biggest socket number in from + 1
\r
507 GNUNET_NETWORK_fdset_copy_native (struct GNUNET_NETWORK_FDSet *to,
\r
508 const fd_set * from, int nfds)
\r
510 FD_COPY (from, &to->sds);
\r
516 * Add a file handle to the fd set
\r
517 * @param fds fd set
\r
518 * @param h the file handle to add
\r
521 GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds,
\r
522 const struct GNUNET_DISK_FileHandle *h)
\r
527 GNUNET_internal_disk_file_handle (h, &hw, sizeof (HANDLE));
\r
528 GNUNET_CONTAINER_slist_add (fds->handles, GNUNET_NO, &hw, sizeof (HANDLE));
\r
532 GNUNET_internal_disk_file_handle (h, &fd, sizeof (int));
\r
533 FD_SET (fd, &fds->sds);
\r
534 if (fd + 1 > fds->nsds)
\r
535 fds->nsds = fd + 1;
\r
540 * Check if a file handle is part of an fd set
\r
541 * @param fds fd set
\r
542 * @param h file handle
\r
543 * @return GNUNET_YES if the file handle is part of the set
\r
546 GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds,
\r
547 const struct GNUNET_DISK_FileHandle *h)
\r
550 return GNUNET_CONTAINER_slist_contains (fds->handles, h->h, sizeof (HANDLE));
\r
552 return FD_ISSET (h->fd, &fds->sds);
\r
557 * Checks if two fd sets overlap
\r
558 * @param fds1 first fd set
\r
559 * @param fds2 second fd set
\r
560 * @return GNUNET_YES if they do overlap, GNUNET_NO otherwise
\r
563 GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1,
\r
564 const struct GNUNET_NETWORK_FDSet *fds2)
\r
569 if (nfds < fds2->nsds)
\r
572 for (; nfds >= 0; nfds--)
\r
573 if (FD_ISSET (nfds, &fds1->sds) && FD_ISSET (nfds, &fds2->sds))
\r
580 * Creates an fd set
\r
581 * @return a new fd set
\r
583 struct GNUNET_NETWORK_FDSet *
\r
584 GNUNET_NETWORK_fdset_create ()
\r
586 struct GNUNET_NETWORK_FDSet *fds;
\r
588 fds = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_FDSet));
\r
590 fds->handles = GNUNET_CONTAINER_slist_create ();
\r
592 GNUNET_NETWORK_fdset_zero (fds);
\r
598 * Releases the associated memory of an fd set
\r
599 * @param fds fd set
\r
602 GNUNET_NETWORK_fdset_destroy (struct GNUNET_NETWORK_FDSet *fds)
\r
605 GNUNET_CONTAINER_slist_destroy (fds->handles);
\r
612 * Check if sockets meet certain conditions
\r
613 * @param rfds set of sockets to be checked for readability
\r
614 * @param wfds set of sockets to be checked for writability
\r
615 * @param efds set of sockets to be checked for exceptions
\r
616 * @param timeout relative value when to return
\r
617 * @return number of selected sockets, GNUNET_SYSERR on error
\r
620 GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
\r
621 struct GNUNET_NETWORK_FDSet *wfds,
\r
622 struct GNUNET_NETWORK_FDSet *efds,
\r
623 const struct GNUNET_TIME_Relative timeout)
\r
631 nfds = GNUNET_MAX (nfds, wfds->nsds);
\r
633 nfds = GNUNET_MAX (nfds, efds->nsds);
\r
638 tv.tv_sec = timeout.value / GNUNET_TIME_UNIT_SECONDS.value;
\r
639 tv.tv_usec = 1000 * (timeout.value - (tv.tv_sec * GNUNET_TIME_UNIT_SECONDS.value));
\r
640 if ( (nfds == 0) &&
\r
641 (timeout.value == GNUNET_TIME_UNIT_FOREVER_REL.value) )
\r
643 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
\r
644 _("Fatal internal logic error, process hangs in `%s' (abort with CTRL-C)!\n"),
\r
648 return select (nfds + 1,
\r
649 (rfds != NULL) ? &rfds->sds : NULL,
\r
650 (wfds != NULL) ? &wfds->sds : NULL,
\r
651 (efds != NULL) ? &efds->sds : NULL,
\r
652 (timeout.value == GNUNET_TIME_UNIT_FOREVER_REL.value)
\r
657 fd_set sock_read, sock_write, sock_except;
\r
658 fd_set aread, awrite, aexcept;
\r
660 struct timeval tvslice;
\r
664 #define SAFE_FD_ISSET(fd, set) (set != NULL && FD_ISSET(fd, set))
\r
666 /* calculate how long we need to wait in milliseconds */
\r
667 if (timeout.value == GNUNET_TIME_UNIT_FOREVER_REL.value)
\r
668 ms_total = INFINITE;
\r
670 ms_total = timeout.value / GNUNET_TIME_UNIT_MILLISECONDS.value;
\r
672 /* select() may be used as a portable way to sleep */
\r
673 if (!(rfds || wfds || efds))
\r
680 sock_read = rfds->sds;
\r
682 FD_ZERO(&sock_read);
\r
685 sock_write = wfds->sds;
\r
687 FD_ZERO(&sock_write);
\r
690 sock_except = efds->sds;
\r
692 FD_ZERO(&sock_except);
\r
694 /* multiplex between winsock select() and waiting on the handles */
\r
698 FD_ZERO (&aexcept);
\r
700 limit = GetTickCount () + ms_total;
\r
707 /* overwrite the zero'd sets here; the select call
\r
708 * will clear those that are not active */
\r
710 FD_COPY (&sock_read, &aread);
\r
711 FD_COPY (&sock_write, &awrite);
\r
712 FD_COPY (&sock_except, &aexcept);
\r
714 tvslice.tv_sec = 0;
\r
715 tvslice.tv_usec = 100000;
\r
718 select (nfds + 1, &aread, &awrite, &aexcept,
\r
719 &tvslice)) == SOCKET_ERROR)
\r
721 SetErrnoFromWinsockError (WSAGetLastError ());
\r
722 if (errno == ENOTSOCK)
\r
726 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "select");
\r
729 goto select_loop_end;
\r
733 /* Poll read pipes */
\r
736 struct GNUNET_CONTAINER_SList_Iterator *i;
\r
739 on_next = GNUNET_NO;
\r
740 for (i = GNUNET_CONTAINER_slist_begin (rfds->handles);
\r
741 GNUNET_CONTAINER_slist_end (i) != GNUNET_YES;
\r
742 on_next || GNUNET_CONTAINER_slist_next (i))
\r
747 h = *(HANDLE *) GNUNET_CONTAINER_slist_get (i, NULL);
\r
748 on_next = GNUNET_NO;
\r
750 if (!PeekNamedPipe (h, NULL, 0, NULL, &dwBytes, NULL))
\r
752 GNUNET_CONTAINER_slist_erase (i);
\r
753 on_next = GNUNET_YES;
\r
756 SetErrnoFromWinError (GetLastError ());
\r
758 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "PeekNamedPipe");
\r
760 goto select_loop_end;
\r
768 GNUNET_CONTAINER_slist_erase (i);
\r
769 on_next = GNUNET_YES;
\r
774 /* Poll for faulty pipes */
\r
777 struct GNUNET_CONTAINER_SList_Iterator *i;
\r
780 on_next = GNUNET_NO;
\r
781 for (i = GNUNET_CONTAINER_slist_begin (efds->handles);
\r
782 GNUNET_CONTAINER_slist_end (i) != GNUNET_YES;
\r
783 on_next || GNUNET_CONTAINER_slist_next (i))
\r
788 h = *(HANDLE *) GNUNET_CONTAINER_slist_get (i, NULL);
\r
790 if (PeekNamedPipe (h, NULL, 0, NULL, &dwBytes, NULL))
\r
792 GNUNET_CONTAINER_slist_erase (i);
\r
793 on_next = GNUNET_YES;
\r
798 on_next = GNUNET_NO;
\r
804 GNUNET_assert (GNUNET_CONTAINER_slist_count (wfds->handles) == 0);
\r
806 /* Check for closed sockets */
\r
807 for (i = 0; i < nfds; i++)
\r
809 if (SAFE_FD_ISSET (i, &sock_read))
\r
811 struct sockaddr addr;
\r
814 if (getpeername (i, &addr, &len) == SOCKET_ERROR)
\r
818 len = sizeof (err);
\r
820 (i, SOL_SOCKET, SO_ERROR, (char *) &err, &len) == 0
\r
821 && err == WSAENOTCONN)
\r
823 if (!SAFE_FD_ISSET (i, &aread))
\r
825 FD_SET (i, &aread);
\r
835 while (retcode == 0 && (ms_total == INFINITE || GetTickCount () < limit));
\r
841 GNUNET_NETWORK_fdset_zero (rfds);
\r
842 GNUNET_NETWORK_fdset_copy_native (rfds, &aread, retcode);
\r
847 GNUNET_NETWORK_fdset_zero (wfds);
\r
848 GNUNET_NETWORK_fdset_copy_native (wfds, &awrite, retcode);
\r
853 GNUNET_NETWORK_fdset_zero (efds);
\r
854 GNUNET_NETWORK_fdset_copy_native (efds, &aexcept, retcode);
\r
863 /* end of network.c */
\r