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
37 struct GNUNET_NETWORK_Handle
41 struct GNUNET_NETWORK_FDSet
44 /* socket descriptors */
50 struct GNUNET_CONTAINER_SList *handles;
56 #define FD_COPY(s, d) (memcpy ((d), (s), sizeof (fd_set)))
60 * Set if a socket should use blocking or non-blocking IO.
62 * @param doBlock blocking mode
63 * @return GNUNET_OK on success, GNUNET_SYSERR on error
66 socket_set_blocking (struct GNUNET_NETWORK_Handle *fd, int doBlock)
72 if (ioctlsocket (fd->fd, FIONBIO, &mode) == SOCKET_ERROR)
75 SetErrnoFromWinsockError (WSAGetLastError ());
76 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "ioctlsocket");
83 int flags = fcntl (fd->fd, F_GETFL);
87 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "fcntl");
95 if (0 != fcntl (fd->fd, F_SETFL, flags))
98 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "fcntl");
109 * Make a socket non-inheritable to child processes
111 * @param h the socket to make non-inheritable
112 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
113 * @warning Not implemented on Windows
116 socket_set_inheritable (const struct GNUNET_NETWORK_Handle *h)
119 i = fcntl (h->fd, F_GETFD);
120 if (i == (i | FD_CLOEXEC))
122 return (fcntl (h->fd, F_SETFD, i | FD_CLOEXEC) == 0)
123 ? GNUNET_OK : GNUNET_SYSERR;
131 * The MSG_NOSIGNAL equivalent on Mac OS X
133 * @param h the socket to make non-delaying
136 socket_set_nosigpipe (const struct GNUNET_NETWORK_Handle *h)
140 setsockopt (h->fd, SOL_SOCKET, SO_NOSIGPIPE, &value, sizeof (value)))
141 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
148 * Disable delays when sending data via the socket.
149 * (GNUnet makes sure that messages are as big as
152 * @param h the socket to make non-delaying
155 socket_set_nodelay (const struct GNUNET_NETWORK_Handle *h)
159 setsockopt (h->fd, IPPROTO_TCP, TCP_NODELAY, &value, sizeof (value)))
160 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
165 * accept a new connection on a socket
167 * @param desc bound socket
168 * @param address address of the connecting peer, may be NULL
169 * @param address_len length of address
170 * @return client socket
172 struct GNUNET_NETWORK_Handle *
173 GNUNET_NETWORK_socket_accept (const struct GNUNET_NETWORK_Handle *desc,
174 struct sockaddr *address,
175 socklen_t * address_len)
177 struct GNUNET_NETWORK_Handle *ret;
178 ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle));
179 ret->fd = accept (desc->fd, address, address_len);
180 if (ret->fd == INVALID_SOCKET)
185 SetErrnoFromWinsockError (WSAGetLastError ());
193 if (ret->fd >= FD_SETSIZE)
196 GNUNET_break (0 == close (ret->fd));
203 if (GNUNET_SYSERR == socket_set_blocking (ret, GNUNET_NO))
207 /* we might want to treat this one as fatal... */
209 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret));
214 if (GNUNET_OK != socket_set_inheritable (ret))
215 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
216 "socket_set_inheritable");
220 socket_set_nosigpipe (ret);
223 socket_set_nodelay (ret);
229 * Bind to a connected socket
231 * @param address address to be bound
232 * @param address_len length of address
233 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
236 GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc,
237 const struct sockaddr *address,
238 socklen_t address_len)
241 ret = bind (desc->fd, address, address_len);
244 if (SOCKET_ERROR == ret)
245 SetErrnoFromWinsockError (WSAGetLastError ());
248 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
255 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
258 GNUNET_NETWORK_socket_close (struct GNUNET_NETWORK_Handle *desc)
264 ret = closesocket (desc->fd);
265 SetErrnoFromWinsockError (WSAGetLastError ());
268 ret = close (desc->fd);
274 return (ret == 0) ? GNUNET_OK : GNUNET_SYSERR;
281 * @param address peer address
282 * @param address_len length of address
283 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
286 GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Handle *desc,
287 const struct sockaddr *address,
288 socklen_t address_len)
291 ret = connect (desc->fd, address, address_len);
294 if (SOCKET_ERROR == ret)
297 SetErrnoFromWinsockError (WSAGetLastError ());
298 if (errno == EWOULDBLOCK)
303 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
311 * @param level protocol level of the option
312 * @param optname identifier of the option
313 * @param optval options
314 * @param optlen length of optval
315 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
318 GNUNET_NETWORK_socket_getsockopt (const struct GNUNET_NETWORK_Handle *desc,
319 int level, int optname, void *optval,
323 ret = getsockopt (desc->fd, level, optname, optval, optlen);
326 if (ret == 0 && level == SOL_SOCKET && optname == SO_ERROR)
327 *((int *) optval) = GetErrnoFromWinsockError (*((int *) optval));
329 else if (SOCKET_ERROR == ret)
330 SetErrnoFromWinsockError (WSAGetLastError ());
333 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
340 * @param backlog length of the listen queue
341 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
344 GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Handle *desc,
348 ret = listen (desc->fd, backlog);
351 if (SOCKET_ERROR == ret)
352 SetErrnoFromWinsockError (WSAGetLastError ());
355 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
360 * Read data from a connected socket (always non-blocking).
362 * @param buffer buffer
363 * @param length length of buffer
366 GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Handle * desc,
367 void *buffer, size_t length)
374 flags |= MSG_DONTWAIT;
377 ret = recv (desc->fd, buffer, length, flags);
380 if (SOCKET_ERROR == ret)
381 SetErrnoFromWinsockError (WSAGetLastError ());
389 * Send data (always non-blocking).
392 * @param buffer data to send
393 * @param length size of the buffer
394 * @return number of bytes sent, GNUNET_SYSERR on error
397 GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Handle * desc,
398 const void *buffer, size_t length)
405 flags |= MSG_DONTWAIT;
409 flags |= MSG_NOSIGNAL;
412 ret = send (desc->fd, buffer, length, flags);
415 if (SOCKET_ERROR == ret)
416 SetErrnoFromWinsockError (WSAGetLastError ());
424 * Send data to a particular destination (always non-blocking).
425 * This function only works for UDP sockets.
428 * @param message data to send
429 * @param length size of the data
430 * @param dest_addr destination address
431 * @param dest_len length of address
432 * @return number of bytes sent, GNUNET_SYSERR on error
435 GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Handle * desc,
436 const void *message, size_t length,
437 const struct sockaddr * dest_addr,
445 flags |= MSG_DONTWAIT;
449 flags |= MSG_NOSIGNAL;
452 ret = sendto (desc->fd, message, length, flags, dest_addr, dest_len);
455 if (SOCKET_ERROR == ret)
456 SetErrnoFromWinsockError (WSAGetLastError ());
466 * @param level protocol level of the option
467 * @param option_name option identifier
468 * @param option_value value to set
469 * @param option_len size of option_value
470 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
473 GNUNET_NETWORK_socket_setsockopt (struct GNUNET_NETWORK_Handle *fd,
474 int level, int option_name,
475 const void *option_value,
476 socklen_t option_len)
479 ret = setsockopt (fd->fd, level, option_name, option_value, option_len);
482 if (SOCKET_ERROR == ret)
483 SetErrnoFromWinsockError (WSAGetLastError ());
486 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
491 * Create a new socket. Configure it for non-blocking IO and
492 * mark it as non-inheritable to child processes (set the
493 * close-on-exec flag).
495 * @param domain domain of the socket
496 * @param type socket type
497 * @param protocol network protocol
498 * @return new socket, NULL on error
500 struct GNUNET_NETWORK_Handle *
501 GNUNET_NETWORK_socket_create (int domain, int type, int protocol)
503 struct GNUNET_NETWORK_Handle *ret;
504 ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle));
505 ret->fd = socket (domain, type, protocol);
506 if (INVALID_SOCKET == ret->fd)
511 SetErrnoFromWinsockError (WSAGetLastError ());
519 if (ret->fd >= FD_SETSIZE)
522 GNUNET_break (0 == close (ret->fd));
529 if (GNUNET_SYSERR == socket_set_blocking (ret, GNUNET_NO))
533 /* we might want to treat this one as fatal... */
535 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret));
540 if (GNUNET_OK != socket_set_inheritable (ret))
541 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
542 "socket_set_inheritable");
546 socket_set_nosigpipe (ret);
549 if (type == SOCK_STREAM)
550 socket_set_nodelay (ret);
556 * Shut down socket operations
558 * @param how type of shutdown
559 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
562 GNUNET_NETWORK_socket_shutdown (struct GNUNET_NETWORK_Handle *desc, int how)
565 ret = shutdown (desc->fd, how);
569 SetErrnoFromWinsockError (WSAGetLastError ());
572 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
581 GNUNET_NETWORK_fdset_zero (struct GNUNET_NETWORK_FDSet *fds)
587 GNUNET_CONTAINER_slist_clear (fds->handles);
593 * Add a socket to the FD set
595 * @param desc socket to add
598 GNUNET_NETWORK_fdset_set (struct GNUNET_NETWORK_FDSet *fds,
599 const struct GNUNET_NETWORK_Handle *desc)
601 FD_SET (desc->fd, &fds->sds);
602 if (desc->fd + 1 > fds->nsds)
603 fds->nsds = desc->fd + 1;
608 * Check whether a socket is part of the fd set
611 * @return 0 if the FD is not set
614 GNUNET_NETWORK_fdset_isset (const struct GNUNET_NETWORK_FDSet *fds,
615 const struct GNUNET_NETWORK_Handle *desc)
617 return FD_ISSET (desc->fd, &fds->sds);
622 * Add one fd set to another
623 * @param dst the fd set to add to
624 * @param src the fd set to add from
627 GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst,
628 const struct GNUNET_NETWORK_FDSet *src)
631 for (nfds = src->nsds; nfds > 0; nfds--)
632 if (FD_ISSET (nfds, &src->sds))
635 FD_SET (nfds, &dst->sds);
636 if (nfds + 1 > dst->nsds)
637 dst->nsds = nfds + 1;
643 * Copy one fd set to another
644 * @param to destination
648 GNUNET_NETWORK_fdset_copy (struct GNUNET_NETWORK_FDSet *to,
649 const struct GNUNET_NETWORK_FDSet *from)
651 FD_COPY (&from->sds, &to->sds);
652 to->nsds = from->nsds;
655 struct GNUNET_CONTAINER_SList_Iterator *iter;
656 GNUNET_CONTAINER_slist_clear (to->handles);
657 for (iter = GNUNET_CONTAINER_slist_begin (from->handles);
658 GNUNET_CONTAINER_slist_end (iter) != GNUNET_YES;
659 GNUNET_CONTAINER_slist_next (iter))
664 handle = GNUNET_CONTAINER_slist_get (iter, &len);
665 GNUNET_CONTAINER_slist_add (to->handles,
666 GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
669 GNUNET_CONTAINER_slist_iter_destroy (iter);
674 * Copy a native fd set
675 * @param to destination
676 * @param from native source set
677 * @param nfds the biggest socket number in from + 1
680 GNUNET_NETWORK_fdset_copy_native (struct GNUNET_NETWORK_FDSet *to,
681 const fd_set * from, int nfds)
683 FD_COPY (from, &to->sds);
688 * Add a file handle to the fd set
690 * @param h the file handle to add
693 GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds,
694 const struct GNUNET_DISK_FileHandle *h)
699 GNUNET_DISK_internal_file_handle_ (h, &hw, sizeof (HANDLE));
700 GNUNET_CONTAINER_slist_add (fds->handles, GNUNET_NO, &hw, sizeof (HANDLE));
704 GNUNET_DISK_internal_file_handle_ (h, &fd, sizeof (int));
705 FD_SET (fd, &fds->sds);
706 if (fd + 1 > fds->nsds)
714 * Check if a file handle is part of an fd set
716 * @param h file handle
717 * @return GNUNET_YES if the file handle is part of the set
720 GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds,
721 const struct GNUNET_DISK_FileHandle *h)
725 return GNUNET_CONTAINER_slist_contains (fds->handles, h->h,
729 return FD_ISSET (h->fd, &fds->sds);
736 * Checks if two fd sets overlap
737 * @param fds1 first fd set
738 * @param fds2 second fd set
739 * @return GNUNET_YES if they do overlap, GNUNET_NO otherwise
742 GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1,
743 const struct GNUNET_NETWORK_FDSet *fds2)
747 if (nfds < fds2->nsds)
749 for (; nfds >= 0; nfds--)
750 if (FD_ISSET (nfds, &fds1->sds) && FD_ISSET (nfds, &fds2->sds))
758 * @return a new fd set
760 struct GNUNET_NETWORK_FDSet *
761 GNUNET_NETWORK_fdset_create ()
763 struct GNUNET_NETWORK_FDSet *fds;
764 fds = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_FDSet));
767 fds->handles = GNUNET_CONTAINER_slist_create ();
770 GNUNET_NETWORK_fdset_zero (fds);
776 * Releases the associated memory of an fd set
780 GNUNET_NETWORK_fdset_destroy (struct GNUNET_NETWORK_FDSet *fds)
784 GNUNET_CONTAINER_slist_destroy (fds->handles);
791 * Check if sockets meet certain conditions
792 * @param rfds set of sockets to be checked for readability
793 * @param wfds set of sockets to be checked for writability
794 * @param efds set of sockets to be checked for exceptions
795 * @param timeout relative value when to return
796 * @return number of selected sockets, GNUNET_SYSERR on error
799 GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
800 struct GNUNET_NETWORK_FDSet *wfds,
801 struct GNUNET_NETWORK_FDSet *efds,
802 const struct GNUNET_TIME_Relative timeout)
809 nfds = GNUNET_MAX (nfds, wfds->nsds);
811 nfds = GNUNET_MAX (nfds, efds->nsds);
815 tv.tv_sec = timeout.value / GNUNET_TIME_UNIT_SECONDS.value;
817 1000 * (timeout.value - (tv.tv_sec * GNUNET_TIME_UNIT_SECONDS.value));
818 if ((nfds == 0) && (timeout.value == GNUNET_TIME_UNIT_FOREVER_REL.value))
821 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
823 ("Fatal internal logic error, process hangs in `%s' (abort with CTRL-C)!\n"),
827 return select (nfds + 1,
828 (rfds != NULL) ? &rfds->sds : NULL,
829 (wfds != NULL) ? &wfds->sds : NULL,
830 (efds != NULL) ? &efds->sds : NULL,
831 (timeout.value == GNUNET_TIME_UNIT_FOREVER_REL.value)
836 fd_set sock_read, sock_write, sock_except;
837 fd_set aread, awrite, aexcept;
839 struct timeval tvslice;
843 #define SAFE_FD_ISSET(fd, set) (set != NULL && FD_ISSET(fd, set))
845 /* calculate how long we need to wait in milliseconds */
846 if (timeout.value == GNUNET_TIME_UNIT_FOREVER_REL.value)
850 ms_total = timeout.value / GNUNET_TIME_UNIT_MILLISECONDS.value;
852 /* select() may be used as a portable way to sleep */
853 if (!(rfds || wfds || efds))
860 sock_read = rfds->sds;
863 FD_ZERO (&sock_read);
865 sock_write = wfds->sds;
868 FD_ZERO (&sock_write);
870 sock_except = efds->sds;
873 FD_ZERO (&sock_except);
875 /* multiplex between winsock select() and waiting on the handles */
879 limit = GetTickCount () + ms_total;
889 /* overwrite the zero'd sets here; the select call
890 * will clear those that are not active */
891 FD_COPY (&sock_read, &aread);
892 FD_COPY (&sock_write, &awrite);
893 FD_COPY (&sock_except, &aexcept);
895 tvslice.tv_usec = 100000;
897 select (nfds + 1, &aread, &awrite, &aexcept,
898 &tvslice)) == SOCKET_ERROR)
901 SetErrnoFromWinsockError (WSAGetLastError ());
902 if (errno == ENOTSOCK)
906 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "select");
909 goto select_loop_end;
913 /* Poll read pipes */
917 struct GNUNET_CONTAINER_SList_Iterator *i;
920 for (i = GNUNET_CONTAINER_slist_begin (rfds->handles);
921 GNUNET_CONTAINER_slist_end (i) != GNUNET_YES;
922 on_next || GNUNET_CONTAINER_slist_next (i))
927 h = *(HANDLE *) GNUNET_CONTAINER_slist_get (i, NULL);
929 if (!PeekNamedPipe (h, NULL, 0, NULL, &dwBytes, NULL))
932 GNUNET_CONTAINER_slist_erase (i);
933 on_next = GNUNET_YES;
935 SetErrnoFromWinError (GetLastError ());
938 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
942 goto select_loop_end;
954 GNUNET_CONTAINER_slist_erase (i);
955 on_next = GNUNET_YES;
958 GNUNET_CONTAINER_slist_iter_destroy (i);
961 /* Poll for faulty pipes */
965 struct GNUNET_CONTAINER_SList_Iterator *i;
968 for (i = GNUNET_CONTAINER_slist_begin (efds->handles);
969 GNUNET_CONTAINER_slist_end (i) != GNUNET_YES;
970 on_next || GNUNET_CONTAINER_slist_next (i))
975 h = *(HANDLE *) GNUNET_CONTAINER_slist_get (i, NULL);
976 if (PeekNamedPipe (h, NULL, 0, NULL, &dwBytes, NULL))
979 GNUNET_CONTAINER_slist_erase (i);
980 on_next = GNUNET_YES;
987 GNUNET_CONTAINER_slist_iter_destroy (i);
992 GNUNET_assert (GNUNET_CONTAINER_slist_count (wfds->handles) == 0);
994 /* Check for closed sockets */
995 for (i = 0; i < nfds; i++)
998 if (SAFE_FD_ISSET (i, &sock_read))
1001 struct sockaddr addr;
1003 if (getpeername (i, &addr, &len) == SOCKET_ERROR)
1009 (i, SOL_SOCKET, SO_ERROR, (char *) &err, &len) == 0
1010 && err == WSAENOTCONN)
1013 if (!SAFE_FD_ISSET (i, &aread))
1025 while (retcode == 0 && (ms_total == INFINITE || GetTickCount () < limit));
1032 GNUNET_NETWORK_fdset_zero (rfds);
1033 GNUNET_NETWORK_fdset_copy_native (rfds, &aread, retcode);
1038 GNUNET_NETWORK_fdset_zero (wfds);
1039 GNUNET_NETWORK_fdset_copy_native (wfds, &awrite, retcode);
1044 GNUNET_NETWORK_fdset_zero (efds);
1045 GNUNET_NETWORK_fdset_copy_native (efds, &aexcept, retcode);
1054 /* end of network.c */