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
154 ret = closesocket (desc->fd);
\r
155 SetErrnoFromWinsockError (WSAGetLastError ());
\r
157 ret = close (desc->fd);
\r
160 GNUNET_free (desc);
\r
162 return (ret == 0) ? GNUNET_OK : GNUNET_SYSERR;
\r
167 * @param desc socket
\r
168 * @param address peer address
\r
169 * @param address_len length of address
\r
170 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
\r
173 GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Handle *desc,
\r
174 const struct sockaddr *address,
\r
175 socklen_t address_len)
\r
179 ret = connect (desc->fd, address, address_len);
\r
181 if (SOCKET_ERROR == ret)
\r
183 SetErrnoFromWinsockError (WSAGetLastError ());
\r
184 if (errno == EWOULDBLOCK)
\r
185 errno = EINPROGRESS;
\r
188 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
\r
192 * Get socket options
\r
193 * @param desc socket
\r
194 * @param level protocol level of the option
\r
195 * @param optname identifier of the option
\r
196 * @param optval options
\r
197 * @param optlen length of optval
\r
198 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
\r
201 GNUNET_NETWORK_socket_getsockopt (const struct GNUNET_NETWORK_Handle *desc,
\r
202 int level, int optname, void *optval,
\r
203 socklen_t * optlen)
\r
207 ret = getsockopt (desc->fd, level, optname, optval, optlen);
\r
209 if (ret == 0 && level == SOL_SOCKET && optname == SO_ERROR)
\r
210 *((int *) optval) = GetErrnoFromWinsockError (*((int *) optval));
\r
211 else if (SOCKET_ERROR == ret)
\r
212 SetErrnoFromWinsockError (WSAGetLastError ());
\r
214 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
\r
218 * Listen on a socket
\r
219 * @param desc socket
\r
220 * @param backlog length of the listen queue
\r
221 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
\r
224 GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Handle *desc,
\r
229 ret = listen (desc->fd, backlog);
\r
231 if (SOCKET_ERROR == ret)
\r
232 SetErrnoFromWinsockError (WSAGetLastError ());
\r
235 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
\r
239 * Read data from a connected socket
\r
240 * @param desc socket
\r
241 * @param buffer buffer
\r
242 * @param length length of buffer
\r
243 * @param flags type of message reception
\r
246 GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Handle * desc,
\r
247 void *buffer, size_t length, int flags)
\r
251 ret = recv (desc->fd, buffer, length, flags);
\r
253 if (SOCKET_ERROR == ret)
\r
254 SetErrnoFromWinsockError (WSAGetLastError ());
\r
262 * @param desc socket
\r
263 * @param buffer data to send
\r
264 * @param length size of the buffer
\r
265 * @param flags type of message transmission
\r
266 * @return number of bytes sent, GNUNET_SYSERR on error
\r
269 GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Handle * desc,
\r
270 const void *buffer, size_t length, int flags)
\r
274 ret = send (desc->fd, buffer, length, flags);
\r
276 if (SOCKET_ERROR == ret)
\r
277 SetErrnoFromWinsockError (WSAGetLastError ());
\r
285 * @param desc socket
\r
286 * @param message data to send
\r
287 * @param length size of the data
\r
288 * @param flags type of message transmission
\r
289 * @param dest_addr destination address
\r
290 * @param dest_len length of address
\r
291 * @return number of bytes sent, GNUNET_SYSERR on error
\r
294 GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Handle * desc,
\r
295 const void *message, size_t length, int flags,
\r
296 const struct sockaddr * dest_addr,
\r
297 socklen_t dest_len)
\r
301 ret = sendto (desc->fd, message, length, flags, dest_addr, dest_len);
\r
303 if (SOCKET_ERROR == ret)
\r
304 SetErrnoFromWinsockError (WSAGetLastError ());
\r
311 * Set socket option
\r
313 * @param level protocol level of the option
\r
314 * @param option_name option identifier
\r
315 * @param option_value value to set
\r
316 * @param option_len size of option_value
\r
317 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
\r
320 GNUNET_NETWORK_socket_setsockopt (struct GNUNET_NETWORK_Handle *fd,
\r
321 int level, int option_name,
\r
322 const void *option_value,
\r
323 socklen_t option_len)
\r
327 ret = setsockopt (fd->fd, level, option_name, option_value, option_len);
\r
329 if (SOCKET_ERROR == ret)
\r
330 SetErrnoFromWinsockError (WSAGetLastError ());
\r
333 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
\r
337 * Create a new socket
\r
338 * @param domain domain of the socket
\r
339 * @param type socket type
\r
340 * @param protocol network protocol
\r
341 * @return new socket, NULL on error
\r
343 struct GNUNET_NETWORK_Handle *
\r
344 GNUNET_NETWORK_socket_socket (int domain, int type, int protocol)
\r
346 struct GNUNET_NETWORK_Handle *ret;
\r
348 ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle));
\r
349 ret->fd = socket (domain, type, protocol);
\r
351 if (INVALID_SOCKET == ret->fd)
\r
352 SetErrnoFromWinsockError (WSAGetLastError ());
\r
365 * Shut down socket operations
\r
366 * @param desc socket
\r
367 * @param how type of shutdown
\r
368 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
\r
371 GNUNET_NETWORK_socket_shutdown (struct GNUNET_NETWORK_Handle *desc,
\r
376 ret = shutdown (desc->fd, how);
\r
379 SetErrnoFromWinsockError (WSAGetLastError ());
\r
382 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
\r
386 * Make a non-inheritable to child processes
\r
388 * @param h the socket to make non-inheritable
\r
389 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
\r
390 * @warning Not implemented on Windows
\r
393 GNUNET_NETWORK_socket_set_inheritable (const struct GNUNET_NETWORK_Handle
\r
398 return GNUNET_SYSERR;
\r
402 i = fcntl (h->fd, F_GETFD);
\r
403 if (i == (i | FD_CLOEXEC))
\r
405 return (fcntl (h->fd, F_SETFD, i | FD_CLOEXEC) == 0)
\r
406 ? GNUNET_OK : GNUNET_SYSERR;
\r
413 * @param fds fd set
\r
416 GNUNET_NETWORK_fdset_zero (struct GNUNET_NETWORK_FDSet *fds)
\r
418 FD_ZERO (&fds->sds);
\r
421 GNUNET_CONTAINER_slist_clear (fds->handles);
\r
427 * Add a socket to the FD set
\r
428 * @param fds fd set
\r
429 * @param desc socket to add
\r
432 GNUNET_NETWORK_fdset_set (struct GNUNET_NETWORK_FDSet *fds,
\r
433 const struct GNUNET_NETWORK_Handle *desc)
\r
435 FD_SET (desc->fd, &fds->sds);
\r
437 if (desc->fd + 1 > fds->nsds)
\r
438 fds->nsds = desc->fd + 1;
\r
443 * Check whether a socket is part of the fd set
\r
444 * @param fds fd set
\r
445 * @param desc socket
\r
446 * @return 0 if the FD is not set
\r
449 GNUNET_NETWORK_fdset_isset (const struct GNUNET_NETWORK_FDSet *fds,
\r
450 const struct GNUNET_NETWORK_Handle *desc)
\r
452 return FD_ISSET (desc->fd, &fds->sds);
\r
457 * Add one fd set to another
\r
458 * @param dst the fd set to add to
\r
459 * @param src the fd set to add from
\r
462 GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst,
\r
463 const struct GNUNET_NETWORK_FDSet *src)
\r
467 for (nfds = src->nsds; nfds > 0; nfds--)
\r
468 if (FD_ISSET (nfds, &src->sds))
\r
470 FD_SET (nfds, &dst->sds);
\r
471 if (nfds + 1 > dst->nsds)
\r
472 dst->nsds = nfds + 1;
\r
478 * Copy one fd set to another
\r
479 * @param to destination
\r
480 * @param from source
\r
483 GNUNET_NETWORK_fdset_copy (struct GNUNET_NETWORK_FDSet *to,
\r
484 const struct GNUNET_NETWORK_FDSet *from)
\r
486 FD_COPY (&from->sds, &to->sds);
\r
487 to->nsds = from->nsds;
\r
489 struct GNUNET_CONTAINER_SList_Iterator *iter;
\r
492 GNUNET_CONTAINER_slist_clear (to->handles);
\r
494 for (iter = GNUNET_CONTAINER_slist_begin (from->handles);
\r
495 GNUNET_CONTAINER_slist_end (iter) != GNUNET_YES; GNUNET_CONTAINER_slist_next (iter))
\r
500 handle = GNUNET_CONTAINER_slist_get (iter, &len);
\r
501 GNUNET_CONTAINER_slist_add (to->handles, GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT, handle, len);
\r
508 * Copy a native fd set
\r
509 * @param to destination
\r
510 * @param from native source set
\r
511 * @param nfds the biggest socket number in from + 1
\r
514 GNUNET_NETWORK_fdset_copy_native (struct GNUNET_NETWORK_FDSet *to,
\r
515 const fd_set * from, int nfds)
\r
517 FD_COPY (from, &to->sds);
\r
523 * Add a file handle to the fd set
\r
524 * @param fds fd set
\r
525 * @param h the file handle to add
\r
528 GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds,
\r
529 const struct GNUNET_DISK_FileHandle *h)
\r
534 GNUNET_DISK_internal_file_handle_ (h, &hw, sizeof (HANDLE));
\r
535 GNUNET_CONTAINER_slist_add (fds->handles, GNUNET_NO, &hw, sizeof (HANDLE));
\r
539 GNUNET_DISK_internal_file_handle_ (h, &fd, sizeof (int));
\r
540 FD_SET (fd, &fds->sds);
\r
541 if (fd + 1 > fds->nsds)
\r
542 fds->nsds = fd + 1;
\r
547 * Check if a file handle is part of an fd set
\r
548 * @param fds fd set
\r
549 * @param h file handle
\r
550 * @return GNUNET_YES if the file handle is part of the set
\r
553 GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds,
\r
554 const struct GNUNET_DISK_FileHandle *h)
\r
557 return GNUNET_CONTAINER_slist_contains (fds->handles, h->h, sizeof (HANDLE));
\r
559 return FD_ISSET (h->fd, &fds->sds);
\r
564 * Checks if two fd sets overlap
\r
565 * @param fds1 first fd set
\r
566 * @param fds2 second fd set
\r
567 * @return GNUNET_YES if they do overlap, GNUNET_NO otherwise
\r
570 GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1,
\r
571 const struct GNUNET_NETWORK_FDSet *fds2)
\r
576 if (nfds < fds2->nsds)
\r
579 for (; nfds >= 0; nfds--)
\r
580 if (FD_ISSET (nfds, &fds1->sds) && FD_ISSET (nfds, &fds2->sds))
\r
587 * Creates an fd set
\r
588 * @return a new fd set
\r
590 struct GNUNET_NETWORK_FDSet *
\r
591 GNUNET_NETWORK_fdset_create ()
\r
593 struct GNUNET_NETWORK_FDSet *fds;
\r
595 fds = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_FDSet));
\r
597 fds->handles = GNUNET_CONTAINER_slist_create ();
\r
599 GNUNET_NETWORK_fdset_zero (fds);
\r
605 * Releases the associated memory of an fd set
\r
606 * @param fds fd set
\r
609 GNUNET_NETWORK_fdset_destroy (struct GNUNET_NETWORK_FDSet *fds)
\r
612 GNUNET_CONTAINER_slist_destroy (fds->handles);
\r
619 * Check if sockets meet certain conditions
\r
620 * @param rfds set of sockets to be checked for readability
\r
621 * @param wfds set of sockets to be checked for writability
\r
622 * @param efds set of sockets to be checked for exceptions
\r
623 * @param timeout relative value when to return
\r
624 * @return number of selected sockets, GNUNET_SYSERR on error
\r
627 GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
\r
628 struct GNUNET_NETWORK_FDSet *wfds,
\r
629 struct GNUNET_NETWORK_FDSet *efds,
\r
630 const struct GNUNET_TIME_Relative timeout)
\r
638 nfds = GNUNET_MAX (nfds, wfds->nsds);
\r
640 nfds = GNUNET_MAX (nfds, efds->nsds);
\r
645 tv.tv_sec = timeout.value / GNUNET_TIME_UNIT_SECONDS.value;
\r
646 tv.tv_usec = 1000 * (timeout.value - (tv.tv_sec * GNUNET_TIME_UNIT_SECONDS.value));
\r
647 if ( (nfds == 0) &&
\r
648 (timeout.value == GNUNET_TIME_UNIT_FOREVER_REL.value) )
\r
650 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
\r
651 _("Fatal internal logic error, process hangs in `%s' (abort with CTRL-C)!\n"),
\r
655 return select (nfds + 1,
\r
656 (rfds != NULL) ? &rfds->sds : NULL,
\r
657 (wfds != NULL) ? &wfds->sds : NULL,
\r
658 (efds != NULL) ? &efds->sds : NULL,
\r
659 (timeout.value == GNUNET_TIME_UNIT_FOREVER_REL.value)
\r
664 fd_set sock_read, sock_write, sock_except;
\r
665 fd_set aread, awrite, aexcept;
\r
667 struct timeval tvslice;
\r
671 #define SAFE_FD_ISSET(fd, set) (set != NULL && FD_ISSET(fd, set))
\r
673 /* calculate how long we need to wait in milliseconds */
\r
674 if (timeout.value == GNUNET_TIME_UNIT_FOREVER_REL.value)
\r
675 ms_total = INFINITE;
\r
677 ms_total = timeout.value / GNUNET_TIME_UNIT_MILLISECONDS.value;
\r
679 /* select() may be used as a portable way to sleep */
\r
680 if (!(rfds || wfds || efds))
\r
687 sock_read = rfds->sds;
\r
689 FD_ZERO(&sock_read);
\r
692 sock_write = wfds->sds;
\r
694 FD_ZERO(&sock_write);
\r
697 sock_except = efds->sds;
\r
699 FD_ZERO(&sock_except);
\r
701 /* multiplex between winsock select() and waiting on the handles */
\r
705 FD_ZERO (&aexcept);
\r
707 limit = GetTickCount () + ms_total;
\r
714 /* overwrite the zero'd sets here; the select call
\r
715 * will clear those that are not active */
\r
717 FD_COPY (&sock_read, &aread);
\r
718 FD_COPY (&sock_write, &awrite);
\r
719 FD_COPY (&sock_except, &aexcept);
\r
721 tvslice.tv_sec = 0;
\r
722 tvslice.tv_usec = 100000;
\r
725 select (nfds + 1, &aread, &awrite, &aexcept,
\r
726 &tvslice)) == SOCKET_ERROR)
\r
728 SetErrnoFromWinsockError (WSAGetLastError ());
\r
729 if (errno == ENOTSOCK)
\r
733 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "select");
\r
736 goto select_loop_end;
\r
740 /* Poll read pipes */
\r
743 struct GNUNET_CONTAINER_SList_Iterator *i;
\r
746 on_next = GNUNET_NO;
\r
747 for (i = GNUNET_CONTAINER_slist_begin (rfds->handles);
\r
748 GNUNET_CONTAINER_slist_end (i) != GNUNET_YES;
\r
749 on_next || GNUNET_CONTAINER_slist_next (i))
\r
754 h = *(HANDLE *) GNUNET_CONTAINER_slist_get (i, NULL);
\r
755 on_next = GNUNET_NO;
\r
757 if (!PeekNamedPipe (h, NULL, 0, NULL, &dwBytes, NULL))
\r
759 GNUNET_CONTAINER_slist_erase (i);
\r
760 on_next = GNUNET_YES;
\r
763 SetErrnoFromWinError (GetLastError ());
\r
765 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "PeekNamedPipe");
\r
767 goto select_loop_end;
\r
775 GNUNET_CONTAINER_slist_erase (i);
\r
776 on_next = GNUNET_YES;
\r
781 /* Poll for faulty pipes */
\r
784 struct GNUNET_CONTAINER_SList_Iterator *i;
\r
787 on_next = GNUNET_NO;
\r
788 for (i = GNUNET_CONTAINER_slist_begin (efds->handles);
\r
789 GNUNET_CONTAINER_slist_end (i) != GNUNET_YES;
\r
790 on_next || GNUNET_CONTAINER_slist_next (i))
\r
795 h = *(HANDLE *) GNUNET_CONTAINER_slist_get (i, NULL);
\r
797 if (PeekNamedPipe (h, NULL, 0, NULL, &dwBytes, NULL))
\r
799 GNUNET_CONTAINER_slist_erase (i);
\r
800 on_next = GNUNET_YES;
\r
805 on_next = GNUNET_NO;
\r
811 GNUNET_assert (GNUNET_CONTAINER_slist_count (wfds->handles) == 0);
\r
813 /* Check for closed sockets */
\r
814 for (i = 0; i < nfds; i++)
\r
816 if (SAFE_FD_ISSET (i, &sock_read))
\r
818 struct sockaddr addr;
\r
821 if (getpeername (i, &addr, &len) == SOCKET_ERROR)
\r
825 len = sizeof (err);
\r
827 (i, SOL_SOCKET, SO_ERROR, (char *) &err, &len) == 0
\r
828 && err == WSAENOTCONN)
\r
830 if (!SAFE_FD_ISSET (i, &aread))
\r
832 FD_SET (i, &aread);
\r
842 while (retcode == 0 && (ms_total == INFINITE || GetTickCount () < limit));
\r
848 GNUNET_NETWORK_fdset_zero (rfds);
\r
849 GNUNET_NETWORK_fdset_copy_native (rfds, &aread, retcode);
\r
854 GNUNET_NETWORK_fdset_zero (wfds);
\r
855 GNUNET_NETWORK_fdset_copy_native (wfds, &awrite, retcode);
\r
860 GNUNET_NETWORK_fdset_zero (efds);
\r
861 GNUNET_NETWORK_fdset_copy_native (efds, &aexcept, retcode);
\r
870 /* end of network.c */
\r