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
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_Descriptor
\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
54 struct GNUNET_NETWORK_Descriptor *
\r
55 GNUNET_NETWORK_socket_accept (const struct GNUNET_NETWORK_Descriptor *desc,
\r
56 struct sockaddr *address,
\r
57 socklen_t * address_len)
\r
59 struct GNUNET_NETWORK_Descriptor *ret;
\r
61 ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Descriptor));
\r
62 ret->fd = accept (desc->fd, address, address_len);
\r
64 if (INVALID_SOCKET == ret->fd)
\r
65 SetErrnoFromWinsockError (WSAGetLastError ());
\r
71 GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Descriptor *desc,
\r
72 const struct sockaddr *address,
\r
73 socklen_t address_len)
\r
77 ret = bind (desc->fd, address, address_len);
\r
79 if (SOCKET_ERROR == ret)
\r
80 SetErrnoFromWinsockError (WSAGetLastError ());
\r
86 * Set if a socket should use blocking or non-blocking IO.
\r
88 * @return GNUNET_OK on success, GNUNET_SYSERR on error
\r
91 GNUNET_NETWORK_socket_set_blocking (struct GNUNET_NETWORK_Descriptor *fd,
\r
97 if (ioctlsocket (fd->fd, FIONBIO, &mode) == SOCKET_ERROR)
\r
99 SetErrnoFromWinsockError (WSAGetLastError ());
\r
100 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "ioctlsocket");
\r
101 return GNUNET_SYSERR;
\r
107 int flags = fcntl (fd->fd, F_GETFL);
\r
110 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "fcntl");
\r
111 return GNUNET_SYSERR;
\r
114 flags &= ~O_NONBLOCK;
\r
116 flags |= O_NONBLOCK;
\r
117 if (0 != fcntl (fd->fd, F_SETFL, flags))
\r
119 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "fcntl");
\r
120 return GNUNET_SYSERR;
\r
127 GNUNET_NETWORK_socket_close (struct GNUNET_NETWORK_Descriptor *desc)
\r
131 ret = closesocket (desc->fd);
\r
132 if (SOCKET_ERROR != ret)
\r
133 GNUNET_free (desc);
\r
135 SetErrnoFromWinsockError (WSAGetLastError ());
\r
137 ret = close (desc->fd);
\r
140 GNUNET_free (desc);
\r
148 GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Descriptor *desc,
\r
149 const struct sockaddr *address,
\r
150 socklen_t address_len)
\r
154 ret = connect (desc->fd, address, address_len);
\r
156 if (SOCKET_ERROR == ret)
\r
157 SetErrnoFromWinsockError (WSAGetLastError ());
\r
163 GNUNET_NETWORK_socket_getsockopt (const struct GNUNET_NETWORK_Descriptor *desc,
\r
164 int level, int optname, void *optval,
\r
165 socklen_t * optlen)
\r
169 ret = getsockopt (desc->fd, level, optname, optval, optlen);
\r
171 if (ret == 0 && level == SOL_SOCKET && optname == SO_ERROR)
\r
172 *((int *) optval) = GetErrnoFromWinsockError (*((int *) optval));
\r
173 else if (SOCKET_ERROR == ret)
\r
174 SetErrnoFromWinsockError (WSAGetLastError ());
\r
180 GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Descriptor *desc,
\r
185 ret = listen (desc->fd, backlog);
\r
187 if (SOCKET_ERROR == ret)
\r
188 SetErrnoFromWinsockError (WSAGetLastError ());
\r
195 GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Descriptor * desc,
\r
196 void *buffer, size_t length, int flags)
\r
200 ret = recv (desc->fd, buffer, length, flags);
\r
202 if (SOCKET_ERROR == ret)
\r
203 SetErrnoFromWinsockError (WSAGetLastError ());
\r
210 GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Descriptor * desc,
\r
211 const void *buffer, size_t length, int flags)
\r
215 ret = send (desc->fd, buffer, length, flags);
\r
217 if (SOCKET_ERROR == ret)
\r
218 SetErrnoFromWinsockError (WSAGetLastError ());
\r
225 GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Descriptor * desc,
\r
226 const void *message, size_t length, int flags,
\r
227 const struct sockaddr * dest_addr,
\r
228 socklen_t dest_len)
\r
232 ret = sendto (desc->fd, message, length, flags, dest_addr, dest_len);
\r
234 if (SOCKET_ERROR == ret)
\r
235 SetErrnoFromWinsockError (WSAGetLastError ());
\r
242 GNUNET_NETWORK_socket_setsockopt (struct GNUNET_NETWORK_Descriptor *fd,
\r
243 int level, int option_name,
\r
244 const void *option_value,
\r
245 socklen_t option_len)
\r
249 ret = setsockopt (fd->fd, level, option_name, option_value, option_len);
\r
251 if (SOCKET_ERROR == ret)
\r
252 SetErrnoFromWinsockError (WSAGetLastError ());
\r
258 struct GNUNET_NETWORK_Descriptor *
\r
259 GNUNET_NETWORK_socket_socket (int domain, int type, int protocol)
\r
261 struct GNUNET_NETWORK_Descriptor *ret;
\r
263 ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Descriptor));
\r
264 ret->fd = socket (domain, type, protocol);
\r
266 if (INVALID_SOCKET == ret->fd)
\r
267 SetErrnoFromWinsockError (WSAGetLastError ());
\r
280 GNUNET_NETWORK_socket_shutdown (struct GNUNET_NETWORK_Descriptor *desc,
\r
285 ret = shutdown (desc->fd, how);
\r
288 SetErrnoFromWinsockError (WSAGetLastError ());
\r
295 GNUNET_NETWORK_socket_set_inheritable (const struct GNUNET_NETWORK_Descriptor
\r
300 return GNUNET_SYSERR;
\r
302 return fcntl (desc->fd, F_SETFD,
\r
304 F_GETFD) | FD_CLOEXEC) ==
\r
305 0 ? GNUNET_OK : GNUNET_SYSERR;
\r
310 GNUNET_NETWORK_fdset_zero (struct GNUNET_NETWORK_FDSet *fds)
\r
312 FD_ZERO (&fds->sds);
\r
316 GNUNET_CONTAINER_vector_destroy (fds->handles);
\r
317 fds->handles = GNUNET_CONTAINER_vector_create (2);
\r
322 GNUNET_NETWORK_fdset_set (struct GNUNET_NETWORK_FDSet *fds,
\r
323 const struct GNUNET_NETWORK_Descriptor *desc)
\r
325 FD_SET (desc->fd, &fds->sds);
\r
327 if (desc->fd + 1 > fds->nsds)
\r
328 fds->nsds = desc->fd + 1;
\r
332 GNUNET_NETWORK_fdset_isset (const struct GNUNET_NETWORK_FDSet *fds,
\r
333 const struct GNUNET_NETWORK_Descriptor *desc)
\r
335 return FD_ISSET (desc->fd, &fds->sds);
\r
339 GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst,
\r
340 const struct GNUNET_NETWORK_FDSet *src)
\r
344 for (nfds = src->nsds; nfds > 0; nfds--)
\r
345 if (FD_ISSET (nfds, &src->sds))
\r
347 FD_SET (nfds, &dst->sds);
\r
348 if (nfds + 1 > dst->nsds)
\r
349 dst->nsds = nfds + 1;
\r
354 GNUNET_NETWORK_fdset_copy (struct GNUNET_NETWORK_FDSet *to,
\r
355 const struct GNUNET_NETWORK_FDSet *from)
\r
357 FD_COPY (&from->sds, &to->sds);
\r
358 to->nsds = from->nsds;
\r
363 GNUNET_CONTAINER_vector_destroy (to->handles);
\r
364 to->handles = GNUNET_CONTAINER_vector_create (2);
\r
365 for (obj = GNUNET_CONTAINER_vector_get_first (from->handles); obj != NULL;
\r
366 obj = GNUNET_CONTAINER_vector_get_next (from->handles))
\r
368 GNUNET_CONTAINER_vector_insert_last (to->handles, obj);
\r
374 GNUNET_NETWORK_fdset_copy_native (struct GNUNET_NETWORK_FDSet *to,
\r
375 const fd_set * from, int nfds)
\r
377 FD_COPY (from, &to->sds);
\r
382 GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds,
\r
383 const struct GNUNET_DISK_FileHandle *h)
\r
388 GNUNET_internal_disk_file_handle (h, &hw, sizeof (HANDLE));
\r
389 GNUNET_CONTAINER_vector_insert_last (fds->handles, h);
\r
393 GNUNET_internal_disk_file_handle (h, &fd, sizeof (int));
\r
394 FD_SET (fd, &fds->sds);
\r
395 if (fd + 1 > fds->nsds)
\r
396 fds->nsds = fd + 1;
\r
401 GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds,
\r
402 const struct GNUNET_DISK_FileHandle *h)
\r
405 return GNUNET_CONTAINER_vector_index_of (fds->handles, h->h) !=
\r
408 return FD_ISSET (h->fd, &fds->sds);
\r
413 GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1,
\r
414 const struct GNUNET_NETWORK_FDSet *fds2)
\r
419 if (nfds < fds2->nsds)
\r
422 for (; nfds >= 0; nfds--)
\r
423 if (FD_ISSET (nfds, &fds1->sds) && FD_ISSET (nfds, &fds2->sds))
\r
429 struct GNUNET_NETWORK_FDSet *
\r
430 GNUNET_NETWORK_fdset_create ()
\r
432 struct GNUNET_NETWORK_FDSet *fds;
\r
434 fds = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_FDSet));
\r
436 fds->handles = NULL;
\r
438 GNUNET_NETWORK_fdset_zero (fds);
\r
444 GNUNET_NETWORK_fdset_destroy (struct GNUNET_NETWORK_FDSet *fds)
\r
447 GNUNET_CONTAINER_vector_destroy (fds->handles);
\r
453 GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
\r
454 struct GNUNET_NETWORK_FDSet *wfds,
\r
455 struct GNUNET_NETWORK_FDSet *efds,
\r
456 const struct GNUNET_TIME_Relative timeout)
\r
464 if (wfds && wfds->nsds > nfds)
\r
466 if (efds && efds->nsds > nfds)
\r
472 tv.tv_sec = timeout.value / GNUNET_TIME_UNIT_SECONDS.value;
\r
473 tv.tv_usec = (timeout.value - (tv.tv_sec * GNUNET_TIME_UNIT_SECONDS.value))
\r
474 / GNUNET_TIME_UNIT_MILLISECONDS.value;
\r
476 return select (nfds + 1, rfds ? &rfds->sds : NULL, wfds ? &wfds->sds : NULL,
\r
477 efds ? &efds->sds : NULL, timeout.value
\r
478 == GNUNET_TIME_UNIT_FOREVER_REL.value ? NULL : &tv);
\r
481 fd_set sock_read, sock_write, sock_except;
\r
482 fd_set aread, awrite, aexcept;
\r
484 struct timeval tvslice;
\r
488 #define SAFE_FD_ISSET(fd, set) (set != NULL && FD_ISSET(fd, set))
\r
490 /* calculate how long we need to wait in milliseconds */
\r
491 if (timeout.value == GNUNET_TIME_UNIT_FOREVER_REL.value)
\r
492 ms_total = INFINITE;
\r
494 ms_total = timeout.value / GNUNET_TIME_UNIT_MILLISECONDS.value;
\r
496 /* select() may be used as a portable way to sleep */
\r
497 if (!(rfds || wfds || efds))
\r
505 sock_read = rfds->sds;
\r
507 FD_ZERO(&sock_read);
\r
510 sock_write = wfds->sds;
\r
512 FD_ZERO(&sock_write);
\r
515 sock_except = efds->sds;
\r
517 FD_ZERO(&sock_except);
\r
521 FD_COPY (&rfds->sds, &sock_read);
\r
523 FD_ZERO(&sock_read);
\r
526 FD_COPY (&wfds->sds, &sock_write);
\r
528 FD_ZERO(&sock_write);
\r
531 FD_COPY (&efds->sds, &sock_except);
\r
533 FD_ZERO(&sock_except);
\r
536 /* multiplex between winsock select() and waiting on the handles */
\r
540 FD_ZERO (&aexcept);
\r
542 limit = GetTickCount () + ms_total;
\r
549 /* overwrite the zero'd sets here; the select call
\r
550 * will clear those that are not active */
\r
552 FD_COPY (&sock_read, &aread);
\r
553 FD_COPY (&sock_write, &awrite);
\r
554 FD_COPY (&sock_except, &aexcept);
\r
556 tvslice.tv_sec = 0;
\r
557 tvslice.tv_usec = 100000;
\r
560 select (nfds + 1, &aread, &awrite, &aexcept,
\r
561 &tvslice)) == SOCKET_ERROR)
\r
563 SetErrnoFromWinsockError (WSAGetLastError ());
\r
564 if (errno == ENOTSOCK)
\r
568 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "select");
\r
571 goto select_loop_end;
\r
575 /* Poll read pipes */
\r
577 for (i = GNUNET_CONTAINER_vector_size (rfds->handles) - 1; i >= 0; i--)
\r
582 (GNUNET_CONTAINER_vector_get_at (rfds->handles, i), NULL, 0,
\r
583 NULL, &dwBytes, NULL))
\r
585 GNUNET_CONTAINER_vector_remove_at (rfds->handles, i);
\r
588 SetErrnoFromWinError (GetLastError ());
\r
590 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "PeekNamedPipe");
\r
592 goto select_loop_end;
\r
599 GNUNET_CONTAINER_vector_remove_at (rfds->handles, i);
\r
602 /* Poll for faulty pipes */
\r
604 for (i = GNUNET_CONTAINER_vector_size (efds->handles); i >= 0; i--)
\r
609 (GNUNET_CONTAINER_vector_get_at (rfds->handles, i), NULL, 0,
\r
610 NULL, &dwBytes, NULL))
\r
612 GNUNET_CONTAINER_vector_remove_at (efds->handles, i);
\r
620 GNUNET_assert (GNUNET_CONTAINER_vector_size (wfds->handles) == 0);
\r
622 /* Check for closed sockets */
\r
623 for (i = 0; i < nfds; i++)
\r
625 if (SAFE_FD_ISSET (i, &sock_read))
\r
627 struct sockaddr addr;
\r
630 if (getpeername (i, &addr, &len) == SOCKET_ERROR)
\r
634 len = sizeof (err);
\r
636 (i, SOL_SOCKET, SO_ERROR, (char *) &err, &len) == 0
\r
637 && err == WSAENOTCONN)
\r
639 if (!SAFE_FD_ISSET (i, &aread))
\r
641 FD_SET (i, &aread);
\r
651 while (retcode == 0 && (ms_total == INFINITE || GetTickCount () < limit));
\r
657 GNUNET_NETWORK_fdset_zero (rfds);
\r
658 GNUNET_NETWORK_fdset_copy_native (rfds, &aread, retcode);
\r
663 GNUNET_NETWORK_fdset_zero (wfds);
\r
664 GNUNET_NETWORK_fdset_copy_native (wfds, &awrite, retcode);
\r
669 GNUNET_NETWORK_fdset_zero (efds);
\r
670 GNUNET_NETWORK_fdset_copy_native (efds, &aexcept, retcode);
\r