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
59 * accept a new connection on a socket
\r
61 * @param desc bound socket
\r
62 * @param address address of the connecting peer, may be NULL
\r
63 * @param address_len length of address
\r
64 * @return client socket
\r
66 struct GNUNET_NETWORK_Handle *
\r
67 GNUNET_NETWORK_socket_accept (const struct GNUNET_NETWORK_Handle *desc,
\r
68 struct sockaddr *address,
\r
69 socklen_t * address_len)
\r
71 struct GNUNET_NETWORK_Handle *ret;
\r
73 ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle));
\r
74 ret->fd = accept (desc->fd, address, address_len);
\r
75 if (ret->fd == INVALID_SOCKET)
\r
78 SetErrnoFromWinsockError (WSAGetLastError ());
\r
84 if (ret->fd >= FD_SETSIZE)
\r
96 * Bind to a connected socket
\r
97 * @param desc socket
\r
98 * @param address address to be bound
\r
99 * @param address_len length of address
\r
100 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
\r
103 GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc,
\r
104 const struct sockaddr *address,
\r
105 socklen_t address_len)
\r
109 ret = bind (desc->fd, address, address_len);
\r
111 if (SOCKET_ERROR == ret)
\r
112 SetErrnoFromWinsockError (WSAGetLastError ());
\r
114 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
\r
118 * Set if a socket should use blocking or non-blocking IO.
\r
120 * @param doBlock blocking mode
\r
121 * @return GNUNET_OK on success, GNUNET_SYSERR on error
\r
124 GNUNET_NETWORK_socket_set_blocking (struct GNUNET_NETWORK_Handle *fd,
\r
130 if (ioctlsocket (fd->fd, FIONBIO, &mode) == SOCKET_ERROR)
\r
132 SetErrnoFromWinsockError (WSAGetLastError ());
\r
133 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "ioctlsocket");
\r
134 return GNUNET_SYSERR;
\r
140 int flags = fcntl (fd->fd, F_GETFL);
\r
143 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "fcntl");
\r
144 return GNUNET_SYSERR;
\r
147 flags &= ~O_NONBLOCK;
\r
149 flags |= O_NONBLOCK;
\r
150 if (0 != fcntl (fd->fd, F_SETFL, flags))
\r
152 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "fcntl");
\r
153 return GNUNET_SYSERR;
\r
161 * @param desc socket
\r
162 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
\r
165 GNUNET_NETWORK_socket_close (struct GNUNET_NETWORK_Handle *desc)
\r
171 ret = closesocket (desc->fd);
\r
172 SetErrnoFromWinsockError (WSAGetLastError ());
\r
174 ret = close (desc->fd);
\r
177 GNUNET_free (desc);
\r
179 return (ret == 0) ? GNUNET_OK : GNUNET_SYSERR;
\r
184 * @param desc socket
\r
185 * @param address peer address
\r
186 * @param address_len length of address
\r
187 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
\r
190 GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Handle *desc,
\r
191 const struct sockaddr *address,
\r
192 socklen_t address_len)
\r
196 ret = connect (desc->fd, address, address_len);
\r
198 if (SOCKET_ERROR == ret)
\r
200 SetErrnoFromWinsockError (WSAGetLastError ());
\r
201 if (errno == EWOULDBLOCK)
\r
202 errno = EINPROGRESS;
\r
205 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
\r
209 * Get socket options
\r
210 * @param desc socket
\r
211 * @param level protocol level of the option
\r
212 * @param optname identifier of the option
\r
213 * @param optval options
\r
214 * @param optlen length of optval
\r
215 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
\r
218 GNUNET_NETWORK_socket_getsockopt (const struct GNUNET_NETWORK_Handle *desc,
\r
219 int level, int optname, void *optval,
\r
220 socklen_t * optlen)
\r
224 ret = getsockopt (desc->fd, level, optname, optval, optlen);
\r
226 if (ret == 0 && level == SOL_SOCKET && optname == SO_ERROR)
\r
227 *((int *) optval) = GetErrnoFromWinsockError (*((int *) optval));
\r
228 else if (SOCKET_ERROR == ret)
\r
229 SetErrnoFromWinsockError (WSAGetLastError ());
\r
231 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
\r
235 * Listen on a socket
\r
236 * @param desc socket
\r
237 * @param backlog length of the listen queue
\r
238 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
\r
241 GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Handle *desc,
\r
246 ret = listen (desc->fd, backlog);
\r
248 if (SOCKET_ERROR == ret)
\r
249 SetErrnoFromWinsockError (WSAGetLastError ());
\r
252 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
\r
256 * Read data from a connected socket
\r
257 * @param desc socket
\r
258 * @param buffer buffer
\r
259 * @param length length of buffer
\r
260 * @param flags type of message reception
\r
263 GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Handle * desc,
\r
264 void *buffer, size_t length, int flags)
\r
268 ret = recv (desc->fd, buffer, length, flags);
\r
270 if (SOCKET_ERROR == ret)
\r
271 SetErrnoFromWinsockError (WSAGetLastError ());
\r
279 * @param desc socket
\r
280 * @param buffer data to send
\r
281 * @param length size of the buffer
\r
282 * @param flags type of message transmission
\r
283 * @return number of bytes sent, GNUNET_SYSERR on error
\r
286 GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Handle * desc,
\r
287 const void *buffer, size_t length, int flags)
\r
291 ret = send (desc->fd, buffer, length, flags);
\r
293 if (SOCKET_ERROR == ret)
\r
294 SetErrnoFromWinsockError (WSAGetLastError ());
\r
302 * @param desc socket
\r
303 * @param message data to send
\r
304 * @param length size of the data
\r
305 * @param flags type of message transmission
\r
306 * @param dest_addr destination address
\r
307 * @param dest_len length of address
\r
308 * @return number of bytes sent, GNUNET_SYSERR on error
\r
311 GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Handle * desc,
\r
312 const void *message, size_t length, int flags,
\r
313 const struct sockaddr * dest_addr,
\r
314 socklen_t dest_len)
\r
318 ret = sendto (desc->fd, message, length, flags, dest_addr, dest_len);
\r
320 if (SOCKET_ERROR == ret)
\r
321 SetErrnoFromWinsockError (WSAGetLastError ());
\r
328 * Set socket option
\r
330 * @param level protocol level of the option
\r
331 * @param option_name option identifier
\r
332 * @param option_value value to set
\r
333 * @param option_len size of option_value
\r
334 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
\r
337 GNUNET_NETWORK_socket_setsockopt (struct GNUNET_NETWORK_Handle *fd,
\r
338 int level, int option_name,
\r
339 const void *option_value,
\r
340 socklen_t option_len)
\r
344 ret = setsockopt (fd->fd, level, option_name, option_value, option_len);
\r
346 if (SOCKET_ERROR == ret)
\r
347 SetErrnoFromWinsockError (WSAGetLastError ());
\r
350 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
\r
354 * Create a new socket
\r
355 * @param domain domain of the socket
\r
356 * @param type socket type
\r
357 * @param protocol network protocol
\r
358 * @return new socket, NULL on error
\r
360 struct GNUNET_NETWORK_Handle *
\r
361 GNUNET_NETWORK_socket_socket (int domain, int type, int protocol)
\r
363 struct GNUNET_NETWORK_Handle *ret;
\r
365 ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle));
\r
366 ret->fd = socket (domain, type, protocol);
\r
367 if (INVALID_SOCKET == ret->fd)
\r
370 SetErrnoFromWinsockError (WSAGetLastError ());
\r
376 if (ret->fd >= FD_SETSIZE)
\r
388 * Shut down socket operations
\r
389 * @param desc socket
\r
390 * @param how type of shutdown
\r
391 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
\r
394 GNUNET_NETWORK_socket_shutdown (struct GNUNET_NETWORK_Handle *desc,
\r
399 ret = shutdown (desc->fd, how);
\r
402 SetErrnoFromWinsockError (WSAGetLastError ());
\r
405 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
\r
409 * Make a non-inheritable to child processes
\r
411 * @param h the socket to make non-inheritable
\r
412 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
\r
413 * @warning Not implemented on Windows
\r
416 GNUNET_NETWORK_socket_set_inheritable (const struct GNUNET_NETWORK_Handle
\r
421 return GNUNET_SYSERR;
\r
425 i = fcntl (h->fd, F_GETFD);
\r
426 if (i == (i | FD_CLOEXEC))
\r
428 return (fcntl (h->fd, F_SETFD, i | FD_CLOEXEC) == 0)
\r
429 ? GNUNET_OK : GNUNET_SYSERR;
\r
436 * @param fds fd set
\r
439 GNUNET_NETWORK_fdset_zero (struct GNUNET_NETWORK_FDSet *fds)
\r
441 FD_ZERO (&fds->sds);
\r
444 GNUNET_CONTAINER_slist_clear (fds->handles);
\r
450 * Add a socket to the FD set
\r
451 * @param fds fd set
\r
452 * @param desc socket to add
\r
455 GNUNET_NETWORK_fdset_set (struct GNUNET_NETWORK_FDSet *fds,
\r
456 const struct GNUNET_NETWORK_Handle *desc)
\r
458 FD_SET (desc->fd, &fds->sds);
\r
460 if (desc->fd + 1 > fds->nsds)
\r
461 fds->nsds = desc->fd + 1;
\r
466 * Check whether a socket is part of the fd set
\r
467 * @param fds fd set
\r
468 * @param desc socket
\r
469 * @return 0 if the FD is not set
\r
472 GNUNET_NETWORK_fdset_isset (const struct GNUNET_NETWORK_FDSet *fds,
\r
473 const struct GNUNET_NETWORK_Handle *desc)
\r
475 return FD_ISSET (desc->fd, &fds->sds);
\r
480 * Add one fd set to another
\r
481 * @param dst the fd set to add to
\r
482 * @param src the fd set to add from
\r
485 GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst,
\r
486 const struct GNUNET_NETWORK_FDSet *src)
\r
490 for (nfds = src->nsds; nfds > 0; nfds--)
\r
491 if (FD_ISSET (nfds, &src->sds))
\r
493 FD_SET (nfds, &dst->sds);
\r
494 if (nfds + 1 > dst->nsds)
\r
495 dst->nsds = nfds + 1;
\r
501 * Copy one fd set to another
\r
502 * @param to destination
\r
503 * @param from source
\r
506 GNUNET_NETWORK_fdset_copy (struct GNUNET_NETWORK_FDSet *to,
\r
507 const struct GNUNET_NETWORK_FDSet *from)
\r
509 FD_COPY (&from->sds, &to->sds);
\r
510 to->nsds = from->nsds;
\r
512 struct GNUNET_CONTAINER_SList_Iterator *iter;
\r
515 GNUNET_CONTAINER_slist_clear (to->handles);
\r
517 for (iter = GNUNET_CONTAINER_slist_begin (from->handles);
\r
518 GNUNET_CONTAINER_slist_end (iter) != GNUNET_YES; GNUNET_CONTAINER_slist_next (iter))
\r
523 handle = GNUNET_CONTAINER_slist_get (iter, &len);
\r
524 GNUNET_CONTAINER_slist_add (to->handles, GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT, handle, len);
\r
531 * Copy a native fd set
\r
532 * @param to destination
\r
533 * @param from native source set
\r
534 * @param nfds the biggest socket number in from + 1
\r
537 GNUNET_NETWORK_fdset_copy_native (struct GNUNET_NETWORK_FDSet *to,
\r
538 const fd_set * from, int nfds)
\r
540 FD_COPY (from, &to->sds);
\r
546 * Add a file handle to the fd set
\r
547 * @param fds fd set
\r
548 * @param h the file handle to add
\r
551 GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds,
\r
552 const struct GNUNET_DISK_FileHandle *h)
\r
557 GNUNET_DISK_internal_file_handle_ (h, &hw, sizeof (HANDLE));
\r
558 GNUNET_CONTAINER_slist_add (fds->handles, GNUNET_NO, &hw, sizeof (HANDLE));
\r
562 GNUNET_DISK_internal_file_handle_ (h, &fd, sizeof (int));
\r
563 FD_SET (fd, &fds->sds);
\r
564 if (fd + 1 > fds->nsds)
\r
565 fds->nsds = fd + 1;
\r
570 * Check if a file handle is part of an fd set
\r
571 * @param fds fd set
\r
572 * @param h file handle
\r
573 * @return GNUNET_YES if the file handle is part of the set
\r
576 GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds,
\r
577 const struct GNUNET_DISK_FileHandle *h)
\r
580 return GNUNET_CONTAINER_slist_contains (fds->handles, h->h, sizeof (HANDLE));
\r
582 return FD_ISSET (h->fd, &fds->sds);
\r
587 * Checks if two fd sets overlap
\r
588 * @param fds1 first fd set
\r
589 * @param fds2 second fd set
\r
590 * @return GNUNET_YES if they do overlap, GNUNET_NO otherwise
\r
593 GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1,
\r
594 const struct GNUNET_NETWORK_FDSet *fds2)
\r
599 if (nfds < fds2->nsds)
\r
602 for (; nfds >= 0; nfds--)
\r
603 if (FD_ISSET (nfds, &fds1->sds) && FD_ISSET (nfds, &fds2->sds))
\r
610 * Creates an fd set
\r
611 * @return a new fd set
\r
613 struct GNUNET_NETWORK_FDSet *
\r
614 GNUNET_NETWORK_fdset_create ()
\r
616 struct GNUNET_NETWORK_FDSet *fds;
\r
618 fds = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_FDSet));
\r
620 fds->handles = GNUNET_CONTAINER_slist_create ();
\r
622 GNUNET_NETWORK_fdset_zero (fds);
\r
628 * Releases the associated memory of an fd set
\r
629 * @param fds fd set
\r
632 GNUNET_NETWORK_fdset_destroy (struct GNUNET_NETWORK_FDSet *fds)
\r
635 GNUNET_CONTAINER_slist_destroy (fds->handles);
\r
642 * Check if sockets meet certain conditions
\r
643 * @param rfds set of sockets to be checked for readability
\r
644 * @param wfds set of sockets to be checked for writability
\r
645 * @param efds set of sockets to be checked for exceptions
\r
646 * @param timeout relative value when to return
\r
647 * @return number of selected sockets, GNUNET_SYSERR on error
\r
650 GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
\r
651 struct GNUNET_NETWORK_FDSet *wfds,
\r
652 struct GNUNET_NETWORK_FDSet *efds,
\r
653 const struct GNUNET_TIME_Relative timeout)
\r
661 nfds = GNUNET_MAX (nfds, wfds->nsds);
\r
663 nfds = GNUNET_MAX (nfds, efds->nsds);
\r
668 tv.tv_sec = timeout.value / GNUNET_TIME_UNIT_SECONDS.value;
\r
669 tv.tv_usec = 1000 * (timeout.value - (tv.tv_sec * GNUNET_TIME_UNIT_SECONDS.value));
\r
670 if ( (nfds == 0) &&
\r
671 (timeout.value == GNUNET_TIME_UNIT_FOREVER_REL.value) )
\r
673 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
\r
674 _("Fatal internal logic error, process hangs in `%s' (abort with CTRL-C)!\n"),
\r
678 return select (nfds + 1,
\r
679 (rfds != NULL) ? &rfds->sds : NULL,
\r
680 (wfds != NULL) ? &wfds->sds : NULL,
\r
681 (efds != NULL) ? &efds->sds : NULL,
\r
682 (timeout.value == GNUNET_TIME_UNIT_FOREVER_REL.value)
\r
687 fd_set sock_read, sock_write, sock_except;
\r
688 fd_set aread, awrite, aexcept;
\r
690 struct timeval tvslice;
\r
694 #define SAFE_FD_ISSET(fd, set) (set != NULL && FD_ISSET(fd, set))
\r
696 /* calculate how long we need to wait in milliseconds */
\r
697 if (timeout.value == GNUNET_TIME_UNIT_FOREVER_REL.value)
\r
698 ms_total = INFINITE;
\r
700 ms_total = timeout.value / GNUNET_TIME_UNIT_MILLISECONDS.value;
\r
702 /* select() may be used as a portable way to sleep */
\r
703 if (!(rfds || wfds || efds))
\r
710 sock_read = rfds->sds;
\r
712 FD_ZERO(&sock_read);
\r
715 sock_write = wfds->sds;
\r
717 FD_ZERO(&sock_write);
\r
720 sock_except = efds->sds;
\r
722 FD_ZERO(&sock_except);
\r
724 /* multiplex between winsock select() and waiting on the handles */
\r
728 FD_ZERO (&aexcept);
\r
730 limit = GetTickCount () + ms_total;
\r
737 /* overwrite the zero'd sets here; the select call
\r
738 * will clear those that are not active */
\r
740 FD_COPY (&sock_read, &aread);
\r
741 FD_COPY (&sock_write, &awrite);
\r
742 FD_COPY (&sock_except, &aexcept);
\r
744 tvslice.tv_sec = 0;
\r
745 tvslice.tv_usec = 100000;
\r
748 select (nfds + 1, &aread, &awrite, &aexcept,
\r
749 &tvslice)) == SOCKET_ERROR)
\r
751 SetErrnoFromWinsockError (WSAGetLastError ());
\r
752 if (errno == ENOTSOCK)
\r
756 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "select");
\r
759 goto select_loop_end;
\r
763 /* Poll read pipes */
\r
766 struct GNUNET_CONTAINER_SList_Iterator *i;
\r
769 on_next = GNUNET_NO;
\r
770 for (i = GNUNET_CONTAINER_slist_begin (rfds->handles);
\r
771 GNUNET_CONTAINER_slist_end (i) != GNUNET_YES;
\r
772 on_next || GNUNET_CONTAINER_slist_next (i))
\r
777 h = *(HANDLE *) GNUNET_CONTAINER_slist_get (i, NULL);
\r
778 on_next = GNUNET_NO;
\r
780 if (!PeekNamedPipe (h, NULL, 0, NULL, &dwBytes, NULL))
\r
782 GNUNET_CONTAINER_slist_erase (i);
\r
783 on_next = GNUNET_YES;
\r
786 SetErrnoFromWinError (GetLastError ());
\r
788 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "PeekNamedPipe");
\r
790 goto select_loop_end;
\r
798 GNUNET_CONTAINER_slist_erase (i);
\r
799 on_next = GNUNET_YES;
\r
804 /* Poll for faulty pipes */
\r
807 struct GNUNET_CONTAINER_SList_Iterator *i;
\r
810 on_next = GNUNET_NO;
\r
811 for (i = GNUNET_CONTAINER_slist_begin (efds->handles);
\r
812 GNUNET_CONTAINER_slist_end (i) != GNUNET_YES;
\r
813 on_next || GNUNET_CONTAINER_slist_next (i))
\r
818 h = *(HANDLE *) GNUNET_CONTAINER_slist_get (i, NULL);
\r
820 if (PeekNamedPipe (h, NULL, 0, NULL, &dwBytes, NULL))
\r
822 GNUNET_CONTAINER_slist_erase (i);
\r
823 on_next = GNUNET_YES;
\r
828 on_next = GNUNET_NO;
\r
834 GNUNET_assert (GNUNET_CONTAINER_slist_count (wfds->handles) == 0);
\r
836 /* Check for closed sockets */
\r
837 for (i = 0; i < nfds; i++)
\r
839 if (SAFE_FD_ISSET (i, &sock_read))
\r
841 struct sockaddr addr;
\r
844 if (getpeername (i, &addr, &len) == SOCKET_ERROR)
\r
848 len = sizeof (err);
\r
850 (i, SOL_SOCKET, SO_ERROR, (char *) &err, &len) == 0
\r
851 && err == WSAENOTCONN)
\r
853 if (!SAFE_FD_ISSET (i, &aread))
\r
855 FD_SET (i, &aread);
\r
865 while (retcode == 0 && (ms_total == INFINITE || GetTickCount () < limit));
\r
871 GNUNET_NETWORK_fdset_zero (rfds);
\r
872 GNUNET_NETWORK_fdset_copy_native (rfds, &aread, retcode);
\r
877 GNUNET_NETWORK_fdset_zero (wfds);
\r
878 GNUNET_NETWORK_fdset_copy_native (wfds, &awrite, retcode);
\r
883 GNUNET_NETWORK_fdset_zero (efds);
\r
884 GNUNET_NETWORK_fdset_copy_native (efds, &aexcept, retcode);
\r
893 /* end of network.c */
\r