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/network.c
23 * @brief basic, low-level TCP networking interface
24 * @author Christian Grothoff
26 * This code is rather complex. Only modify it if you
27 * 1) Have a NEW testcase showing that the new code
28 * is needed and correct
29 * 2) All EXISTING testcases pass with the new code
30 * These rules should apply in general, but for this
31 * module they are VERY, VERY important.
34 * - can we merge receive_ready and receive_again?
35 * - can we integrate the nth.timeout_task with the write_task's timeout?
39 #include "gnunet_common.h"
40 #include "gnunet_network_lib.h"
41 #include "gnunet_scheduler_lib.h"
43 #define DEBUG_NETWORK GNUNET_NO
46 * List of address families to give as hints to
47 * getaddrinfo, in reverse order of preference.
49 static int address_families[] =
50 { AF_INET, AF_INET6, AF_UNSPEC };
52 struct GNUNET_NETWORK_TransmitHandle
56 * Function to call if the send buffer has notify_size
59 GNUNET_NETWORK_TransmitReadyNotify notify_ready;
62 * Closure for notify_ready.
64 void *notify_ready_cls;
69 struct GNUNET_NETWORK_SocketHandle *sh;
72 * Timeout for receiving (in absolute time).
74 struct GNUNET_TIME_Absolute transmit_timeout;
77 * Task called on timeout.
79 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
82 * At what number of bytes available in the
83 * write buffer should the notify method be called?
90 * @brief handle for a network socket
92 struct GNUNET_NETWORK_SocketHandle
96 * Scheduler that was used for the connect task.
98 struct GNUNET_SCHEDULER_Handle *sched;
101 * Address information for connect (may be NULL).
106 * Index for the next struct addrinfo for connect attempts (may be NULL)
108 struct addrinfo *ai_pos;
111 * Network address of the other end-point, may be NULL.
113 struct sockaddr *addr;
116 * Pointer to the hostname if socket was
117 * created using DNS lookup, otherwise NULL.
122 * Pointer to our write buffer.
127 * Size of our write buffer.
129 size_t write_buffer_size;
132 * Current write-offset in write buffer (where
133 * would we write next).
135 size_t write_buffer_off;
138 * Current read-offset in write buffer (how many
139 * bytes have already been send).
141 size_t write_buffer_pos;
149 * Offset in our address family list
155 * Connect task that we may need to wait for.
157 GNUNET_SCHEDULER_TaskIdentifier connect_task;
160 * Read task that we may need to wait for.
162 GNUNET_SCHEDULER_TaskIdentifier read_task;
165 * Write task that we may need to wait for.
167 GNUNET_SCHEDULER_TaskIdentifier write_task;
170 * The handle we return for GNUNET_NETWORK_notify_transmit_ready.
172 struct GNUNET_NETWORK_TransmitHandle nth;
175 * Underlying OS's socket, set to -1 after fatal errors.
180 * Port to connect to.
185 * Function to call on data received, NULL
186 * if no receive is pending.
188 GNUNET_NETWORK_Receiver receiver;
191 * Closure for receiver.
196 * Timeout for receiving (in absolute time).
198 struct GNUNET_TIME_Absolute receive_timeout;
201 * Maximum number of bytes to read
210 * Create a socket handle by boxing an existing OS socket. The OS
211 * socket should henceforth be no longer used directly.
212 * GNUNET_socket_destroy will close it.
214 * @param sched scheduler to use
215 * @param osSocket existing socket to box
216 * @param maxbuf maximum write buffer size for the socket (use
217 * 0 for sockets that need no write buffers, such as listen sockets)
218 * @return the boxed socket handle
220 struct GNUNET_NETWORK_SocketHandle *
221 GNUNET_NETWORK_socket_create_from_existing (struct GNUNET_SCHEDULER_Handle
222 *sched, int osSocket,
225 struct GNUNET_NETWORK_SocketHandle *ret;
226 ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_SocketHandle) + maxbuf);
227 ret->write_buffer = (char *) &ret[1];
228 ret->write_buffer_size = maxbuf;
229 ret->sock = osSocket;
236 * Create a socket handle by accepting on a listen socket. This
237 * function may block if the listen socket has no connection ready.
239 * @param sched scheduler to use
240 * @param access function to use to check if access is allowed
241 * @param access_cls closure for access
242 * @param lsock listen socket
243 * @param maxbuf maximum write buffer size for the socket (use
244 * 0 for sockets that need no write buffers, such as listen sockets)
245 * @return the socket handle, NULL on error
247 struct GNUNET_NETWORK_SocketHandle *
248 GNUNET_NETWORK_socket_create_from_accept (struct GNUNET_SCHEDULER_Handle
250 GNUNET_NETWORK_AccessCheck access,
251 void *access_cls, int lsock,
254 struct GNUNET_NETWORK_SocketHandle *ret;
259 struct sockaddr_in *v4;
260 struct sockaddr_in6 *v6;
264 addrlen = sizeof (addr);
265 fd = accept (lsock, (struct sockaddr *) &addr, &addrlen);
268 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "accept");
273 if (0 != fcntl (fd, F_SETFD, fcntl (fd, F_GETFD) | FD_CLOEXEC))
274 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
277 if (addrlen > sizeof (addr))
280 GNUNET_break (0 == CLOSE (fd));
284 sa = (struct sockaddr *) addr;
285 v6 = (struct sockaddr_in6 *) addr;
286 if ((sa->sa_family == AF_INET6) && (IN6_IS_ADDR_V4MAPPED (&v6->sin6_addr)))
288 /* convert to V4 address */
289 v4 = GNUNET_malloc (sizeof (struct sockaddr_in));
290 memset (v4, 0, sizeof (struct sockaddr_in));
291 v4->sin_family = AF_INET;
292 memcpy (&v4->sin_addr,
293 &((char *) &v6->sin6_addr)[sizeof (struct in6_addr) -
294 sizeof (struct in_addr)],
295 sizeof (struct in_addr));
296 v4->sin_port = v6->sin6_port;
298 addrlen = sizeof (struct sockaddr_in);
302 uaddr = GNUNET_malloc (addrlen);
303 memcpy (uaddr, addr, addrlen);
306 if ((access != NULL) &&
307 (GNUNET_YES != (aret = access (access_cls, uaddr, addrlen))))
309 if (aret == GNUNET_NO)
310 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
311 _("Access denied to `%s'\n"),
312 GNUNET_a2s(uaddr, addrlen));
313 GNUNET_break (0 == SHUTDOWN (fd, SHUT_RDWR));
314 GNUNET_break (0 == CLOSE (fd));
319 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
320 _("Accepting connection from `%s'\n"),
321 GNUNET_a2s(uaddr, addrlen));
323 ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_SocketHandle) + maxbuf);
324 ret->write_buffer = (char *) &ret[1];
325 ret->write_buffer_size = maxbuf;
327 ret->addrlen = addrlen;
334 * Obtain the network address of the other party.
336 * @param sock the client to get the address for
337 * @param addr where to store the address
338 * @param addrlen where to store the length of the address
339 * @return GNUNET_OK on success
342 GNUNET_NETWORK_socket_get_address (struct GNUNET_NETWORK_SocketHandle *sock,
343 void **addr, size_t * addrlen)
345 if ((sock->addr == NULL) || (sock->addrlen == 0))
347 *addr = GNUNET_malloc (sock->addrlen);
348 memcpy (*addr, sock->addr, sock->addrlen);
349 *addrlen = sock->addrlen;
355 * Set if a socket should use blocking or non-blocking IO.
357 * @return GNUNET_OK on success, GNUNET_SYSERR on error
360 socket_set_blocking (int handle, int doBlock)
366 if (ioctlsocket (plibc_fd_get_handle (handle), FIONBIO, &mode) ==
369 SetErrnoFromWinsockError (WSAGetLastError ());
370 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "ioctlsocket");
371 return GNUNET_SYSERR;
374 if (ioctlsocket (handle, FIONBIO, &mode) == SOCKET_ERROR)
376 SetErrnoFromWinsockError (WSAGetLastError ());
377 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "ioctlsocket");
378 return GNUNET_SYSERR;
381 /* store the blocking mode */
383 plibc_fd_set_blocking (handle, doBlock);
385 __win_SetHandleBlockingMode (handle, doBlock);
391 int flags = fcntl (handle, F_GETFL);
394 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "fcntl");
395 return GNUNET_SYSERR;
398 flags &= ~O_NONBLOCK;
401 if (0 != fcntl (handle, F_SETFL, flags))
403 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "fcntl");
404 return GNUNET_SYSERR;
412 * Perform a DNS lookup for the hostname associated
413 * with the current socket, iterating over the address
414 * families as specified in the "address_families" array.
417 try_lookup (struct GNUNET_NETWORK_SocketHandle *sock)
419 struct addrinfo hints;
422 while ( (sock->ai_pos == NULL) &&
423 (sock->af_fam_offset > 0) )
425 if (sock->ai != NULL)
426 freeaddrinfo (sock->ai);
427 memset (&hints, 0, sizeof (hints));
428 hints.ai_family = address_families[--sock->af_fam_offset];
429 hints.ai_socktype = SOCK_STREAM;
430 if (0 != (ec = getaddrinfo (sock->hostname, NULL, &hints, &sock->ai)))
432 GNUNET_log (GNUNET_ERROR_TYPE_INFO | GNUNET_ERROR_TYPE_BULK,
433 "`%s' failed for hostname `%s': %s\n",
434 "getaddrinfo", sock->hostname, gai_strerror (ec));
437 sock->ai_pos = sock->ai;
443 * Initiate asynchronous TCP connect request.
445 * @param sock what socket to connect
446 * @return GNUNET_SYSERR error (no more addresses to try)
449 try_connect (struct GNUNET_NETWORK_SocketHandle *sock)
453 if (sock->addr != NULL)
455 GNUNET_free (sock->addr);
461 if (sock->ai_pos == NULL)
463 if (sock->ai_pos == NULL)
465 /* no more addresses to try, fatal! */
466 return GNUNET_SYSERR;
468 switch (sock->ai_pos->ai_family)
471 ((struct sockaddr_in *) sock->ai_pos->ai_addr)->sin_port =
475 ((struct sockaddr_in6 *) sock->ai_pos->ai_addr)->sin6_port =
479 sock->ai_pos = sock->ai_pos->ai_next;
482 s = SOCKET (sock->ai_pos->ai_family, SOCK_STREAM, 0);
485 /* maybe unsupported address family, try next */
486 GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "socket");
487 sock->ai_pos = sock->ai_pos->ai_next;
492 if (0 != fcntl (s, F_SETFD, fcntl (s, F_GETFD) | FD_CLOEXEC))
493 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
496 if (GNUNET_SYSERR == socket_set_blocking (s, GNUNET_NO))
498 /* we'll treat this one as fatal */
499 GNUNET_break (0 == CLOSE (s));
500 return GNUNET_SYSERR;
503 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
504 _("Trying to connect to `%s'\n"),
505 GNUNET_a2s(sock->ai_pos->ai_addr,
506 sock->ai_pos->ai_addrlen));
508 if ((0 != CONNECT (s,
509 sock->ai_pos->ai_addr,
510 sock->ai_pos->ai_addrlen)) && (errno != EINPROGRESS))
512 /* maybe refused / unsupported address, try next */
513 GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "connect");
514 GNUNET_break (0 == CLOSE (s));
515 sock->ai_pos = sock->ai_pos->ai_next;
520 /* got one! copy address information! */
521 sock->addrlen = sock->ai_pos->ai_addrlen;
522 sock->addr = GNUNET_malloc (sock->addrlen);
523 memcpy (sock->addr, sock->ai_pos->ai_addr, sock->addrlen);
524 sock->ai_pos = sock->ai_pos->ai_next;
531 * Scheduler let us know that we're either ready to
532 * write on the socket OR connect timed out. Do the
536 connect_continuation (void *cls,
537 const struct GNUNET_SCHEDULER_TaskContext *tc)
539 struct GNUNET_NETWORK_SocketHandle *sock = cls;
543 /* nobody needs to wait for us anymore... */
544 sock->connect_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
545 /* Note: write-ready does NOT mean connect succeeded,
546 we need to use getsockopt to be sure */
547 len = sizeof (error);
550 if ((0 == (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) ||
551 (0 != getsockopt (sock->sock, SOL_SOCKET, SO_ERROR, &error, &len)) ||
552 (error != 0) || (errno != 0))
555 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
556 "Failed to establish TCP connection to `%s'\n",
557 GNUNET_a2s(sock->addr, sock->addrlen));
559 /* connect failed / timed out */
560 GNUNET_break (0 == CLOSE (sock->sock));
562 if (GNUNET_SYSERR == try_connect (sock))
564 /* failed for good */
566 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
567 "Failed to establish TCP connection, no further addresses to try.\n");
569 /* connect failed / timed out */
570 GNUNET_break (sock->ai_pos == NULL);
571 freeaddrinfo (sock->ai);
575 sock->connect_task = GNUNET_SCHEDULER_add_write (tc->sched, GNUNET_NO, /* abort on shutdown */
576 GNUNET_SCHEDULER_PRIORITY_KEEP,
577 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
578 GNUNET_NETWORK_CONNECT_RETRY_TIMEOUT,
580 &connect_continuation,
584 /* connect succeeded! clean up "ai" */
586 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
587 "Connection to `%s' succeeded!\n",
588 GNUNET_a2s(sock->addr, sock->addrlen));
590 freeaddrinfo (sock->ai);
597 * Create a socket handle by (asynchronously) connecting to a host.
598 * This function returns immediately, even if the connection has not
599 * yet been established. This function only creates TCP connections.
601 * @param sched scheduler to use
602 * @param hostname name of the host to connect to
603 * @param port port to connect to
604 * @param maxbuf maximum write buffer size for the socket (use
605 * 0 for sockets that need no write buffers, such as listen sockets)
606 * @return the socket handle
608 struct GNUNET_NETWORK_SocketHandle *
609 GNUNET_NETWORK_socket_create_from_connect (struct GNUNET_SCHEDULER_Handle
610 *sched, const char *hostname,
611 uint16_t port, size_t maxbuf)
613 struct GNUNET_NETWORK_SocketHandle *ret;
615 ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_SocketHandle) + maxbuf);
618 ret->write_buffer = (char *) &ret[1];
619 ret->write_buffer_size = maxbuf;
621 ret->af_fam_offset = sizeof (address_families) / sizeof(address_families[0]);
622 ret->hostname = GNUNET_strdup (hostname);
623 if (GNUNET_SYSERR == try_connect (ret))
626 freeaddrinfo (ret->ai);
627 GNUNET_free (ret->hostname);
631 ret->connect_task = GNUNET_SCHEDULER_add_write (sched, GNUNET_NO, /* abort on shutdown */
632 GNUNET_SCHEDULER_PRIORITY_KEEP,
633 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
634 GNUNET_NETWORK_CONNECT_RETRY_TIMEOUT,
636 &connect_continuation, ret);
643 * Create a socket handle by (asynchronously) connecting to a host.
644 * This function returns immediately, even if the connection has not
645 * yet been established. This function only creates TCP connections.
647 * @param sched scheduler to use
648 * @param af_family address family to use
649 * @param serv_addr server address
650 * @param addrlen length of server address
651 * @param maxbuf maximum write buffer size for the socket (use
652 * 0 for sockets that need no write buffers, such as listen sockets)
653 * @return the socket handle
655 struct GNUNET_NETWORK_SocketHandle *
656 GNUNET_NETWORK_socket_create_from_sockaddr (struct GNUNET_SCHEDULER_Handle
657 *sched, int af_family,
658 const struct sockaddr *serv_addr,
659 socklen_t addrlen, size_t maxbuf)
662 struct GNUNET_NETWORK_SocketHandle *ret;
664 s = SOCKET (af_family, SOCK_STREAM, 0);
667 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING |
668 GNUNET_ERROR_TYPE_BULK, "socket");
672 if (0 != fcntl (s, F_SETFD, fcntl (s, F_GETFD) | FD_CLOEXEC))
673 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
676 if (GNUNET_SYSERR == socket_set_blocking (s, GNUNET_NO))
678 /* we'll treat this one as fatal */
679 GNUNET_break (0 == CLOSE (s));
683 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
684 _("Trying to connect to `%s'\n"),
685 GNUNET_a2s(serv_addr, addrlen));
687 if ((0 != CONNECT (s, serv_addr, addrlen)) && (errno != EINPROGRESS))
689 /* maybe refused / unsupported address, try next */
690 GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "connect");
691 GNUNET_break (0 == CLOSE (s));
694 ret = GNUNET_NETWORK_socket_create_from_existing (sched, s, maxbuf);
695 ret->addr = GNUNET_malloc (addrlen);
696 memcpy (ret->addr, serv_addr, addrlen);
697 ret->addrlen = addrlen;
703 * Check if socket is valid (no fatal errors have happened so far).
704 * Note that a socket that is still trying to connect is considered
707 * @param sock socket to check
708 * @return GNUNET_YES if valid, GNUNET_NO otherwise
711 GNUNET_NETWORK_socket_check (struct GNUNET_NETWORK_SocketHandle *sock)
713 if (sock->ai != NULL)
714 return GNUNET_YES; /* still trying to connect */
715 return (sock->sock == -1) ? GNUNET_NO : GNUNET_YES;
720 * Scheduler let us know that the connect task is finished (or was
721 * cancelled due to shutdown). Now really clean up.
724 destroy_continuation (void *cls,
725 const struct GNUNET_SCHEDULER_TaskContext *tc)
727 struct GNUNET_NETWORK_SocketHandle *sock = cls;
728 GNUNET_NETWORK_TransmitReadyNotify notify;
730 if (sock->write_task != GNUNET_SCHEDULER_NO_PREREQUISITE_TASK)
732 GNUNET_SCHEDULER_add_after (sock->sched,
734 GNUNET_SCHEDULER_PRIORITY_KEEP,
736 &destroy_continuation, sock);
739 if (sock->sock != -1)
742 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down socket.\n");
744 SHUTDOWN (sock->sock, SHUT_RDWR);
746 if (sock->read_task != GNUNET_SCHEDULER_NO_PREREQUISITE_TASK)
748 GNUNET_SCHEDULER_add_after (sock->sched,
750 GNUNET_SCHEDULER_PRIORITY_KEEP,
752 &destroy_continuation, sock);
755 if (NULL != (notify = sock->nth.notify_ready))
757 sock->nth.notify_ready = NULL;
758 notify (sock->nth.notify_ready_cls, 0, NULL);
759 if (sock->nth.timeout_task != GNUNET_SCHEDULER_NO_PREREQUISITE_TASK)
761 GNUNET_SCHEDULER_cancel (sock->sched, sock->nth.timeout_task);
762 sock->nth.timeout_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
765 if (sock->sock != -1)
766 GNUNET_break (0 == CLOSE (sock->sock));
767 GNUNET_free_non_null (sock->addr);
768 if (sock->ai != NULL)
769 freeaddrinfo (sock->ai);
770 GNUNET_free_non_null (sock->hostname);
776 * Close the socket and free associated resources. Pending
777 * transmissions are simply dropped. A pending receive call will be
778 * called with an error code of "EPIPE".
780 * @param sock socket to destroy
783 GNUNET_NETWORK_socket_destroy (struct GNUNET_NETWORK_SocketHandle *sock)
785 if (sock->write_buffer_off == 0)
786 sock->ai_pos = NULL; /* if we're still trying to connect and have
787 no message pending, stop trying! */
788 GNUNET_assert (sock->sched != NULL);
789 GNUNET_SCHEDULER_add_after (sock->sched,
791 GNUNET_SCHEDULER_PRIORITY_KEEP,
793 &destroy_continuation, sock);
797 * Tell the receiver callback that a timeout was reached.
800 signal_timeout (struct GNUNET_NETWORK_SocketHandle *sh)
802 GNUNET_NETWORK_Receiver receiver;
805 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
806 "Network signals time out to receiver!\n");
808 GNUNET_assert (NULL != (receiver = sh->receiver));
810 receiver (sh->receiver_cls, NULL, 0, NULL, 0, 0);
815 * Tell the receiver callback that we had an IO error.
818 signal_error (struct GNUNET_NETWORK_SocketHandle *sh, int errcode)
820 GNUNET_NETWORK_Receiver receiver;
821 GNUNET_assert (NULL != (receiver = sh->receiver));
823 receiver (sh->receiver_cls, NULL, 0, sh->addr, sh->addrlen, errcode);
828 * This function is called once we either timeout
829 * or have data ready to read.
832 receive_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
834 struct GNUNET_NETWORK_SocketHandle *sh = cls;
835 struct GNUNET_TIME_Absolute now;
836 char buffer[sh->max];
838 GNUNET_NETWORK_Receiver receiver;
840 sh->read_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
841 now = GNUNET_TIME_absolute_get ();
842 if ((now.value > sh->receive_timeout.value) ||
843 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT)) ||
844 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
847 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
848 "Receive encounters error: time out...\n");
855 /* connect failed for good */
857 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
858 "Receive encounters error, socket closed...\n");
860 signal_error (sh, ECONNREFUSED);
863 GNUNET_assert (FD_ISSET (sh->sock, tc->read_ready));
865 ret = RECV (sh->sock, buffer, sh->max,
878 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
879 "Error receiving: %s\n", STRERROR (errno));
881 signal_error (sh, errno);
885 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
886 "receive_ready read %u/%u bytes from `%s'!\n",
889 GNUNET_a2s(sh->addr, sh->addrlen));
891 GNUNET_assert (NULL != (receiver = sh->receiver));
893 receiver (sh->receiver_cls, buffer, ret, sh->addr, sh->addrlen, 0);
898 * This function is called after establishing a connection either has
899 * succeeded or timed out. Note that it is possible that the attempt
900 * timed out and that we're immediately retrying. If we are retrying,
901 * we need to wait again (or timeout); if we succeeded, we need to
902 * wait for data (or timeout).
905 receive_again (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
907 struct GNUNET_NETWORK_SocketHandle *sh = cls;
908 struct GNUNET_TIME_Absolute now;
910 sh->read_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
911 if ((sh->sock == -1) &&
912 (sh->connect_task == GNUNET_SCHEDULER_NO_PREREQUISITE_TASK))
914 /* not connected and no longer trying */
916 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
917 "Receive encounters error, socket closed...\n");
919 signal_error (sh, ECONNREFUSED);
922 now = GNUNET_TIME_absolute_get ();
923 if ((now.value > sh->receive_timeout.value) ||
924 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
927 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
928 "Receive encounters error: time out...\n");
933 if (sh->connect_task != GNUNET_SCHEDULER_NO_PREREQUISITE_TASK)
935 /* connect was retried */
936 sh->read_task = GNUNET_SCHEDULER_add_after (tc->sched,
938 GNUNET_SCHEDULER_PRIORITY_KEEP,
943 /* connect succeeded, wait for data! */
944 sh->read_task = GNUNET_SCHEDULER_add_read (tc->sched,
946 GNUNET_SCHEDULER_PRIORITY_KEEP,
948 GNUNET_TIME_absolute_get_remaining
949 (sh->receive_timeout),
950 sh->sock, &receive_ready,
956 * Receive data from the given socket. Note that this function will
957 * call "receiver" asynchronously using the scheduler. It will
958 * "immediately" return. Note that there MUST only be one active
959 * receive call per socket at any given point in time (so do not
960 * call receive again until the receiver callback has been invoked).
962 * @param sched scheduler to use
963 * @param sock socket handle
964 * @param max maximum number of bytes to read
965 * @param timeout maximum amount of time to wait (use -1 for "forever")
966 * @param receiver function to call with received data
967 * @param receiver_cls closure for receiver
968 * @return scheduler task ID used for receiving, GNUNET_SCHEDULER_NO_PREREQUISITE_TASK on error
970 GNUNET_SCHEDULER_TaskIdentifier
971 GNUNET_NETWORK_receive (struct GNUNET_NETWORK_SocketHandle *sock,
973 struct GNUNET_TIME_Relative timeout,
974 GNUNET_NETWORK_Receiver receiver, void *receiver_cls)
976 struct GNUNET_SCHEDULER_TaskContext tc;
978 GNUNET_assert ((sock->read_task == GNUNET_SCHEDULER_NO_PREREQUISITE_TASK) &&
979 (sock->receiver == NULL));
980 sock->receiver = receiver;
981 sock->receiver_cls = receiver_cls;
982 sock->receive_timeout = GNUNET_TIME_relative_to_absolute (timeout);
984 memset (&tc, 0, sizeof (tc));
985 tc.sched = sock->sched;
986 tc.reason = GNUNET_SCHEDULER_REASON_PREREQ_DONE;
987 receive_again (sock, &tc);
988 return sock->read_task;
993 * Cancel receive job on the given socket. Note that the
994 * receiver callback must not have been called yet in order
995 * for the cancellation to be valid.
997 * @param sock socket handle
998 * @param task task identifier returned from the receive call
999 * @return closure of the original receiver callback
1002 GNUNET_NETWORK_receive_cancel (struct GNUNET_NETWORK_SocketHandle *sock,
1003 GNUNET_SCHEDULER_TaskIdentifier task)
1005 GNUNET_assert (sock->read_task == task);
1006 GNUNET_assert (sock == GNUNET_SCHEDULER_cancel (sock->sched, task));
1007 sock->read_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
1008 sock->receiver = NULL;
1009 return sock->receiver_cls;
1014 * Try to call the transmit notify method (check if we do
1015 * have enough space available first)!
1017 * @param sock socket for which we should do this processing
1018 * @return GNUNET_YES if we were able to call notify
1021 process_notify (struct GNUNET_NETWORK_SocketHandle *sock)
1026 GNUNET_NETWORK_TransmitReadyNotify notify;
1028 GNUNET_assert (sock->write_task == GNUNET_SCHEDULER_NO_PREREQUISITE_TASK);
1029 if (NULL == (notify = sock->nth.notify_ready))
1031 used = sock->write_buffer_off - sock->write_buffer_pos;
1032 avail = sock->write_buffer_size - used;
1033 size = sock->nth.notify_size;
1034 if (sock->nth.notify_size > avail)
1036 sock->nth.notify_ready = NULL;
1037 if (sock->nth.timeout_task != GNUNET_SCHEDULER_NO_PREREQUISITE_TASK)
1039 GNUNET_SCHEDULER_cancel (sock->sched, sock->nth.timeout_task);
1040 sock->nth.timeout_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
1042 if (sock->write_buffer_size - sock->write_buffer_off < size)
1044 /* need to compact */
1045 memmove (sock->write_buffer,
1046 &sock->write_buffer[sock->write_buffer_pos], used);
1047 sock->write_buffer_off -= sock->write_buffer_pos;
1048 sock->write_buffer_pos = 0;
1050 GNUNET_assert (sock->write_buffer_size - sock->write_buffer_off >= size);
1051 size = notify (sock->nth.notify_ready_cls,
1052 sock->write_buffer_size - sock->write_buffer_off,
1053 &sock->write_buffer[sock->write_buffer_off]);
1054 sock->write_buffer_off += size;
1060 * Task invoked by the scheduler when a call to transmit
1061 * is timing out (we never got enough buffer space to call
1062 * the callback function before the specified timeout
1065 * This task notifies the client about the timeout.
1068 transmit_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1070 struct GNUNET_NETWORK_SocketHandle *sock = cls;
1071 GNUNET_NETWORK_TransmitReadyNotify notify;
1074 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transmit fails, time out reached.\n");
1076 notify = sock->nth.notify_ready;
1077 sock->nth.notify_ready = NULL;
1078 notify (sock->nth.notify_ready_cls, 0, NULL);
1083 transmit_error (struct GNUNET_NETWORK_SocketHandle *sock)
1085 if (sock->nth.notify_ready == NULL)
1086 return; /* nobody to tell about it */
1087 if (sock->nth.timeout_task != GNUNET_SCHEDULER_NO_PREREQUISITE_TASK)
1089 GNUNET_SCHEDULER_cancel (sock->sched, sock->nth.timeout_task);
1090 sock->nth.timeout_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
1092 transmit_timeout (sock, NULL);
1097 * See if we are now connected. If not, wait longer for
1098 * connect to succeed. If connected, we should be able
1099 * to write now as well, unless we timed out.
1102 transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1104 struct GNUNET_NETWORK_SocketHandle *sock = cls;
1108 GNUNET_assert (sock->write_task != GNUNET_SCHEDULER_NO_PREREQUISITE_TASK);
1109 sock->write_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
1110 if (sock->connect_task != GNUNET_SCHEDULER_NO_PREREQUISITE_TASK)
1112 /* still waiting for connect */
1113 GNUNET_assert (sock->write_task ==
1114 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK);
1116 GNUNET_SCHEDULER_add_delayed (tc->sched, GNUNET_NO,
1117 GNUNET_SCHEDULER_PRIORITY_KEEP,
1119 GNUNET_TIME_UNIT_ZERO, &transmit_ready,
1123 if ( (sock->sock == -1) ||
1124 ( (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT)) &&
1125 (0 == (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE)) &&
1126 (!FD_ISSET (sock->sock, tc->write_ready))) )
1129 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1130 _("Could not satisfy pending transmission request, socket closed or connect failed.\n"));
1132 if (-1 != sock->sock)
1134 SHUTDOWN (sock->sock, SHUT_RDWR);
1135 GNUNET_break (0 == CLOSE (sock->sock));
1138 transmit_error (sock);
1139 return; /* connect failed for good, we're finished */
1141 if ((tc->write_ready == NULL) || (!FD_ISSET (sock->sock, tc->write_ready)))
1143 /* special circumstances (in particular,
1144 PREREQ_DONE after connect): not yet ready to write,
1145 but no "fatal" error either. Hence retry. */
1146 goto SCHEDULE_WRITE;
1148 GNUNET_assert (sock->write_buffer_off >= sock->write_buffer_pos);
1149 process_notify (sock);
1150 have = sock->write_buffer_off - sock->write_buffer_pos;
1153 /* no data ready for writing, terminate write loop */
1157 ret = SEND (sock->sock,
1158 &sock->write_buffer[sock->write_buffer_pos],
1162 MSG_DONTWAIT | MSG_NOSIGNAL
1172 GNUNET_log_strerror (GNUNET_ERROR_TYPE_DEBUG, "send");
1174 SHUTDOWN (sock->sock, SHUT_RDWR);
1175 GNUNET_break (0 == CLOSE (sock->sock));
1177 transmit_error (sock);
1181 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1182 "transmit_ready transmitted %u/%u bytes to `%s'\n",
1185 GNUNET_a2s(sock->addr, sock->addrlen));
1187 sock->write_buffer_pos += ret;
1188 if (sock->write_buffer_pos == sock->write_buffer_off)
1190 /* transmitted all pending data */
1191 sock->write_buffer_pos = 0;
1192 sock->write_buffer_off = 0;
1194 if ((sock->write_buffer_off == 0) && (NULL == sock->nth.notify_ready))
1195 return; /* all data sent! */
1196 /* not done writing, schedule more */
1198 if (sock->write_task == GNUNET_SCHEDULER_NO_PREREQUISITE_TASK)
1200 GNUNET_SCHEDULER_add_write (tc->sched,
1202 GNUNET_SCHEDULER_PRIORITY_KEEP,
1203 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
1204 GNUNET_TIME_absolute_get_remaining (sock->nth.transmit_timeout),
1205 sock->sock, &transmit_ready, sock);
1210 * Ask the socket to call us once the specified number of bytes
1211 * are free in the transmission buffer. May call the notify
1212 * method immediately if enough space is available.
1214 * @param sock socket
1215 * @param size number of bytes to send
1216 * @param timeout after how long should we give up (and call
1217 * notify with buf NULL and size 0)?
1218 * @param notify function to call
1219 * @param notify_cls closure for notify
1220 * @return non-NULL if the notify callback was queued,
1221 * NULL if we are already going to notify someone else (busy)
1223 struct GNUNET_NETWORK_TransmitHandle *
1224 GNUNET_NETWORK_notify_transmit_ready (struct GNUNET_NETWORK_SocketHandle
1226 struct GNUNET_TIME_Relative timeout,
1227 GNUNET_NETWORK_TransmitReadyNotify
1228 notify, void *notify_cls)
1230 if (sock->nth.notify_ready != NULL)
1232 GNUNET_assert (notify != NULL);
1233 GNUNET_assert (sock->write_buffer_size >= size);
1235 if ((sock->sock == -1) &&
1236 (sock->connect_task == GNUNET_SCHEDULER_NO_PREREQUISITE_TASK))
1239 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1240 "Transmission request of size %u fails, connection failed.\n",
1243 notify (notify_cls, 0, NULL);
1246 GNUNET_assert (sock->write_buffer_off <= sock->write_buffer_size);
1247 GNUNET_assert (sock->write_buffer_pos <= sock->write_buffer_size);
1248 GNUNET_assert (sock->write_buffer_pos <= sock->write_buffer_off);
1249 sock->nth.notify_ready = notify;
1250 sock->nth.notify_ready_cls = notify_cls;
1251 sock->nth.sh = sock;
1252 sock->nth.notify_size = size;
1253 sock->nth.transmit_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1254 sock->nth.timeout_task = GNUNET_SCHEDULER_add_delayed (sock->sched,
1256 GNUNET_SCHEDULER_PRIORITY_KEEP,
1257 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
1261 if (sock->write_task == GNUNET_SCHEDULER_NO_PREREQUISITE_TASK)
1263 if (sock->connect_task == GNUNET_SCHEDULER_NO_PREREQUISITE_TASK)
1264 sock->write_task = GNUNET_SCHEDULER_add_write (sock->sched,
1266 GNUNET_SCHEDULER_PRIORITY_KEEP,
1267 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
1268 GNUNET_TIME_absolute_get_remaining (sock->nth.transmit_timeout),
1270 &transmit_ready, sock);
1272 sock->write_task = GNUNET_SCHEDULER_add_delayed (sock->sched,
1274 GNUNET_SCHEDULER_PRIORITY_KEEP,
1276 GNUNET_TIME_UNIT_ZERO,
1277 &transmit_ready, sock);
1284 * Cancel the specified transmission-ready
1288 GNUNET_NETWORK_notify_transmit_ready_cancel (struct
1289 GNUNET_NETWORK_TransmitHandle *h)
1291 GNUNET_assert (h->notify_ready != NULL);
1292 GNUNET_SCHEDULER_cancel (h->sh->sched, h->timeout_task);
1293 h->timeout_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
1294 h->notify_ready = NULL;
1298 #if 0 /* keep Emacsens' auto-indent happy */