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_Vector *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 if (SOCKET_ERROR != ret)
\r
154 GNUNET_free (desc);
\r
156 SetErrnoFromWinsockError (WSAGetLastError ());
\r
158 ret = close (desc->fd);
\r
161 GNUNET_free (desc);
\r
165 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
\r
170 * @param desc socket
\r
171 * @param address peer address
\r
172 * @param address_len length of address
\r
173 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
\r
176 GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Handle *desc,
\r
177 const struct sockaddr *address,
\r
178 socklen_t address_len)
\r
182 ret = connect (desc->fd, address, address_len);
\r
184 if (SOCKET_ERROR == ret)
\r
186 SetErrnoFromWinsockError (WSAGetLastError ());
\r
187 if (errno == EWOULDBLOCK)
\r
188 errno = EINPROGRESS;
\r
191 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
\r
195 * Get socket options
\r
196 * @param desc socket
\r
197 * @param level protocol level of the option
\r
198 * @param optname identifier of the option
\r
199 * @param optval options
\r
200 * @param optlen length of optval
\r
201 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
\r
204 GNUNET_NETWORK_socket_getsockopt (const struct GNUNET_NETWORK_Handle *desc,
\r
205 int level, int optname, void *optval,
\r
206 socklen_t * optlen)
\r
210 ret = getsockopt (desc->fd, level, optname, optval, optlen);
\r
212 if (ret == 0 && level == SOL_SOCKET && optname == SO_ERROR)
\r
213 *((int *) optval) = GetErrnoFromWinsockError (*((int *) optval));
\r
214 else if (SOCKET_ERROR == ret)
\r
215 SetErrnoFromWinsockError (WSAGetLastError ());
\r
217 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
\r
221 * Listen on a socket
\r
222 * @param desc socket
\r
223 * @param backlog length of the listen queue
\r
224 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
\r
227 GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Handle *desc,
\r
232 ret = listen (desc->fd, backlog);
\r
234 if (SOCKET_ERROR == ret)
\r
235 SetErrnoFromWinsockError (WSAGetLastError ());
\r
238 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
\r
242 * Read data from a connected socket
\r
243 * @param desc socket
\r
244 * @param buffer buffer
\r
245 * @param length length of buffer
\r
246 * @param flags type of message reception
\r
249 GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Handle * desc,
\r
250 void *buffer, size_t length, int flags)
\r
254 ret = recv (desc->fd, buffer, length, flags);
\r
256 if (SOCKET_ERROR == ret)
\r
257 SetErrnoFromWinsockError (WSAGetLastError ());
\r
265 * @param desc socket
\r
266 * @param buffer data to send
\r
267 * @param length size of the buffer
\r
268 * @param flags type of message transmission
\r
269 * @return number of bytes sent, GNUNET_SYSERR on error
\r
272 GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Handle * desc,
\r
273 const void *buffer, size_t length, int flags)
\r
277 ret = send (desc->fd, buffer, length, flags);
\r
279 if (SOCKET_ERROR == ret)
\r
280 SetErrnoFromWinsockError (WSAGetLastError ());
\r
288 * @param desc socket
\r
289 * @param message data to send
\r
290 * @param length size of the data
\r
291 * @param flags type of message transmission
\r
292 * @param dest_addr destination address
\r
293 * @param dest_len length of address
\r
294 * @return number of bytes sent, GNUNET_SYSERR on error
\r
297 GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Handle * desc,
\r
298 const void *message, size_t length, int flags,
\r
299 const struct sockaddr * dest_addr,
\r
300 socklen_t dest_len)
\r
304 ret = sendto (desc->fd, message, length, flags, dest_addr, dest_len);
\r
306 if (SOCKET_ERROR == ret)
\r
307 SetErrnoFromWinsockError (WSAGetLastError ());
\r
314 * Set socket option
\r
316 * @param level protocol level of the option
\r
317 * @param option_name option identifier
\r
318 * @param option_value value to set
\r
319 * @param option_len size of option_value
\r
320 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
\r
323 GNUNET_NETWORK_socket_setsockopt (struct GNUNET_NETWORK_Handle *fd,
\r
324 int level, int option_name,
\r
325 const void *option_value,
\r
326 socklen_t option_len)
\r
330 ret = setsockopt (fd->fd, level, option_name, option_value, option_len);
\r
332 if (SOCKET_ERROR == ret)
\r
333 SetErrnoFromWinsockError (WSAGetLastError ());
\r
336 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
\r
340 * Create a new socket
\r
341 * @param domain domain of the socket
\r
342 * @param type socket type
\r
343 * @param protocol network protocol
\r
344 * @return new socket, NULL on error
\r
346 struct GNUNET_NETWORK_Handle *
\r
347 GNUNET_NETWORK_socket_socket (int domain, int type, int protocol)
\r
349 struct GNUNET_NETWORK_Handle *ret;
\r
351 ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle));
\r
352 ret->fd = socket (domain, type, protocol);
\r
354 if (INVALID_SOCKET == ret->fd)
\r
355 SetErrnoFromWinsockError (WSAGetLastError ());
\r
368 * Shut down socket operations
\r
369 * @param desc socket
\r
370 * @param how type of shutdown
\r
371 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
\r
374 GNUNET_NETWORK_socket_shutdown (struct GNUNET_NETWORK_Handle *desc,
\r
379 ret = shutdown (desc->fd, how);
\r
382 SetErrnoFromWinsockError (WSAGetLastError ());
\r
385 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
\r
389 * Make a non-inheritable to child processes
\r
391 * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
\r
392 * @warning Not implemented on Windows
\r
395 GNUNET_NETWORK_socket_set_inheritable (const struct GNUNET_NETWORK_Handle
\r
400 return GNUNET_SYSERR;
\r
402 return fcntl (desc->fd, F_SETFD,
\r
404 F_GETFD) | FD_CLOEXEC) ==
\r
405 0 ? GNUNET_OK : GNUNET_SYSERR;
\r
412 * @param fds fd set
\r
415 GNUNET_NETWORK_fdset_zero (struct GNUNET_NETWORK_FDSet *fds)
\r
417 FD_ZERO (&fds->sds);
\r
421 GNUNET_CONTAINER_vector_destroy (fds->handles);
\r
422 fds->handles = GNUNET_CONTAINER_vector_create (2);
\r
428 * Add a socket to the FD set
\r
429 * @param fds fd set
\r
430 * @param desc socket to add
\r
433 GNUNET_NETWORK_fdset_set (struct GNUNET_NETWORK_FDSet *fds,
\r
434 const struct GNUNET_NETWORK_Handle *desc)
\r
436 FD_SET (desc->fd, &fds->sds);
\r
438 if (desc->fd + 1 > fds->nsds)
\r
439 fds->nsds = desc->fd + 1;
\r
444 * Check whether a socket is part of the fd set
\r
445 * @param fds fd set
\r
446 * @param desc socket
\r
447 * @return 0 if the FD is not set
\r
450 GNUNET_NETWORK_fdset_isset (const struct GNUNET_NETWORK_FDSet *fds,
\r
451 const struct GNUNET_NETWORK_Handle *desc)
\r
453 return FD_ISSET (desc->fd, &fds->sds);
\r
458 * Add one fd set to another
\r
459 * @param dst the fd set to add to
\r
460 * @param src the fd set to add from
\r
463 GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst,
\r
464 const struct GNUNET_NETWORK_FDSet *src)
\r
468 for (nfds = src->nsds; nfds > 0; nfds--)
\r
469 if (FD_ISSET (nfds, &src->sds))
\r
471 FD_SET (nfds, &dst->sds);
\r
472 if (nfds + 1 > dst->nsds)
\r
473 dst->nsds = nfds + 1;
\r
479 * Copy one fd set to another
\r
480 * @param to destination
\r
481 * @param from source
\r
484 GNUNET_NETWORK_fdset_copy (struct GNUNET_NETWORK_FDSet *to,
\r
485 const struct GNUNET_NETWORK_FDSet *from)
\r
487 FD_COPY (&from->sds, &to->sds);
\r
488 to->nsds = from->nsds;
\r
493 GNUNET_CONTAINER_vector_destroy (to->handles);
\r
494 to->handles = GNUNET_CONTAINER_vector_create (2);
\r
495 for (obj = GNUNET_CONTAINER_vector_get_first (from->handles); obj != NULL;
\r
496 obj = GNUNET_CONTAINER_vector_get_next (from->handles))
\r
498 GNUNET_CONTAINER_vector_insert_last (to->handles, obj);
\r
505 * Copy a native fd set
\r
506 * @param to destination
\r
507 * @param from native source set
\r
508 * @param nfds the biggest socket number in from + 1
\r
511 GNUNET_NETWORK_fdset_copy_native (struct GNUNET_NETWORK_FDSet *to,
\r
512 const fd_set * from, int nfds)
\r
514 FD_COPY (from, &to->sds);
\r
520 * Add a file handle to the fd set
\r
521 * @param fds fd set
\r
522 * @param h the file handle to add
\r
525 GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds,
\r
526 const struct GNUNET_DISK_FileHandle *h)
\r
531 GNUNET_internal_disk_file_handle (h, &hw, sizeof (HANDLE));
\r
532 GNUNET_CONTAINER_vector_insert_last (fds->handles, h);
\r
536 GNUNET_internal_disk_file_handle (h, &fd, sizeof (int));
\r
537 FD_SET (fd, &fds->sds);
\r
538 if (fd + 1 > fds->nsds)
\r
539 fds->nsds = fd + 1;
\r
544 * Check if a file handle is part of an fd set
\r
545 * @param fds fd set
\r
546 * @param h file handle
\r
547 * @return GNUNET_YES if the file handle is part of the set
\r
550 GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds,
\r
551 const struct GNUNET_DISK_FileHandle *h)
\r
554 return GNUNET_CONTAINER_vector_index_of (fds->handles, h->h) !=
\r
557 return FD_ISSET (h->fd, &fds->sds);
\r
562 * Checks if two fd sets overlap
\r
563 * @param fds1 first fd set
\r
564 * @param fds2 second fd set
\r
565 * @return GNUNET_YES if they do overlap, GNUNET_NO otherwise
\r
568 GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1,
\r
569 const struct GNUNET_NETWORK_FDSet *fds2)
\r
574 if (nfds < fds2->nsds)
\r
577 for (; nfds >= 0; nfds--)
\r
578 if (FD_ISSET (nfds, &fds1->sds) && FD_ISSET (nfds, &fds2->sds))
\r
585 * Creates an fd set
\r
586 * @return a new fd set
\r
588 struct GNUNET_NETWORK_FDSet *
\r
589 GNUNET_NETWORK_fdset_create ()
\r
591 struct GNUNET_NETWORK_FDSet *fds;
\r
593 fds = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_FDSet));
\r
595 fds->handles = NULL;
\r
597 GNUNET_NETWORK_fdset_zero (fds);
\r
604 * Releases the associated memory of an fd set
\r
605 * @param fds fd set
\r
608 GNUNET_NETWORK_fdset_destroy (struct GNUNET_NETWORK_FDSet *fds)
\r
611 GNUNET_CONTAINER_vector_destroy (fds->handles);
\r
618 * Check if sockets meet certain conditions
\r
619 * @param rfds set of sockets to be checked for readability
\r
620 * @param wfds set of sockets to be checked for writability
\r
621 * @param efds set of sockets to be checked for exceptions
\r
622 * @param timeout relative value when to return
\r
623 * @return number of selected sockets, GNUNET_SYSERR on error
\r
626 GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
\r
627 struct GNUNET_NETWORK_FDSet *wfds,
\r
628 struct GNUNET_NETWORK_FDSet *efds,
\r
629 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 return select (nfds + 1,
\r
648 (rfds != NULL) ? &rfds->sds : NULL,
\r
649 (wfds != NULL) ? &wfds->sds : NULL,
\r
650 (efds != NULL) ? &efds->sds : NULL,
\r
651 (timeout.value == GNUNET_TIME_UNIT_FOREVER_REL.value)
\r
656 fd_set sock_read, sock_write, sock_except;
\r
657 fd_set aread, awrite, aexcept;
\r
659 struct timeval tvslice;
\r
663 #define SAFE_FD_ISSET(fd, set) (set != NULL && FD_ISSET(fd, set))
\r
665 /* calculate how long we need to wait in milliseconds */
\r
666 if (timeout.value == GNUNET_TIME_UNIT_FOREVER_REL.value)
\r
667 ms_total = INFINITE;
\r
669 ms_total = timeout.value / GNUNET_TIME_UNIT_MILLISECONDS.value;
\r
671 /* select() may be used as a portable way to sleep */
\r
672 if (!(rfds || wfds || efds))
\r
679 sock_read = rfds->sds;
\r
681 FD_ZERO(&sock_read);
\r
684 sock_write = wfds->sds;
\r
686 FD_ZERO(&sock_write);
\r
689 sock_except = efds->sds;
\r
691 FD_ZERO(&sock_except);
\r
693 /* multiplex between winsock select() and waiting on the handles */
\r
697 FD_ZERO (&aexcept);
\r
699 limit = GetTickCount () + ms_total;
\r
706 /* overwrite the zero'd sets here; the select call
\r
707 * will clear those that are not active */
\r
709 FD_COPY (&sock_read, &aread);
\r
710 FD_COPY (&sock_write, &awrite);
\r
711 FD_COPY (&sock_except, &aexcept);
\r
713 tvslice.tv_sec = 0;
\r
714 tvslice.tv_usec = 100000;
\r
717 select (nfds + 1, &aread, &awrite, &aexcept,
\r
718 &tvslice)) == SOCKET_ERROR)
\r
720 SetErrnoFromWinsockError (WSAGetLastError ());
\r
721 if (errno == ENOTSOCK)
\r
725 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "select");
\r
728 goto select_loop_end;
\r
732 /* Poll read pipes */
\r
734 for (i = GNUNET_CONTAINER_vector_size (rfds->handles) - 1; i >= 0; i--)
\r
739 (GNUNET_CONTAINER_vector_get_at (rfds->handles, i), NULL, 0,
\r
740 NULL, &dwBytes, NULL))
\r
742 GNUNET_CONTAINER_vector_remove_at (rfds->handles, i);
\r
745 SetErrnoFromWinError (GetLastError ());
\r
747 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "PeekNamedPipe");
\r
749 goto select_loop_end;
\r
756 GNUNET_CONTAINER_vector_remove_at (rfds->handles, i);
\r
759 /* Poll for faulty pipes */
\r
761 for (i = GNUNET_CONTAINER_vector_size (efds->handles); i >= 0; i--)
\r
766 (GNUNET_CONTAINER_vector_get_at (rfds->handles, i), NULL, 0,
\r
767 NULL, &dwBytes, NULL))
\r
769 GNUNET_CONTAINER_vector_remove_at (efds->handles, i);
\r
777 GNUNET_assert (GNUNET_CONTAINER_vector_size (wfds->handles) == 0);
\r
779 /* Check for closed sockets */
\r
780 for (i = 0; i < nfds; i++)
\r
782 if (SAFE_FD_ISSET (i, &sock_read))
\r
784 struct sockaddr addr;
\r
787 if (getpeername (i, &addr, &len) == SOCKET_ERROR)
\r
791 len = sizeof (err);
\r
793 (i, SOL_SOCKET, SO_ERROR, (char *) &err, &len) == 0
\r
794 && err == WSAENOTCONN)
\r
796 if (!SAFE_FD_ISSET (i, &aread))
\r
798 FD_SET (i, &aread);
\r
808 while (retcode == 0 && (ms_total == INFINITE || GetTickCount () < limit));
\r
814 GNUNET_NETWORK_fdset_zero (rfds);
\r
815 GNUNET_NETWORK_fdset_copy_native (rfds, &aread, retcode);
\r
820 GNUNET_NETWORK_fdset_zero (wfds);
\r
821 GNUNET_NETWORK_fdset_copy_native (wfds, &awrite, retcode);
\r
826 GNUNET_NETWORK_fdset_zero (efds);
\r
827 GNUNET_NETWORK_fdset_copy_native (efds, &aexcept, retcode);
\r
836 /* end of network.c */
\r