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_YES
45 struct GNUNET_NETWORK_TransmitHandle
49 * Function to call if the send buffer has notify_size
52 GNUNET_NETWORK_TransmitReadyNotify notify_ready;
55 * Closure for notify_ready.
57 void *notify_ready_cls;
62 struct GNUNET_NETWORK_SocketHandle *sh;
65 * Timeout for receiving (in absolute time).
67 struct GNUNET_TIME_Absolute transmit_timeout;
70 * Task called on timeout.
72 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
75 * At what number of bytes available in the
76 * write buffer should the notify method be called?
83 * @brief handle for a network socket
85 struct GNUNET_NETWORK_SocketHandle
89 * Scheduler that was used for the connect task.
91 struct GNUNET_SCHEDULER_Handle *sched;
94 * Address information for connect (may be NULL).
99 * Index for the next struct addrinfo for connect attempts (may be NULL)
101 struct addrinfo *ai_pos;
104 * Network address of the other end-point, may be NULL.
106 struct sockaddr *addr;
109 * Pointer to our write buffer.
114 * Size of our write buffer.
116 size_t write_buffer_size;
119 * Current write-offset in write buffer (where
120 * would we write next).
122 size_t write_buffer_off;
125 * Current read-offset in write buffer (how many
126 * bytes have already been send).
128 size_t write_buffer_pos;
136 * Connect task that we may need to wait for.
138 GNUNET_SCHEDULER_TaskIdentifier connect_task;
141 * Read task that we may need to wait for.
143 GNUNET_SCHEDULER_TaskIdentifier read_task;
146 * Write task that we may need to wait for.
148 GNUNET_SCHEDULER_TaskIdentifier write_task;
151 * The handle we return for GNUNET_NETWORK_notify_transmit_ready.
153 struct GNUNET_NETWORK_TransmitHandle nth;
156 * Underlying OS's socket, set to -1 after fatal errors.
161 * Port to connect to.
166 * Function to call on data received, NULL
167 * if no receive is pending.
169 GNUNET_NETWORK_Receiver receiver;
172 * Closure for receiver.
177 * Timeout for receiving (in absolute time).
179 struct GNUNET_TIME_Absolute receive_timeout;
182 * Maximum number of bytes to read
191 * Create a socket handle by boxing an existing OS socket. The OS
192 * socket should henceforth be no longer used directly.
193 * GNUNET_socket_destroy will close it.
195 * @param sched scheduler to use
196 * @param osSocket existing socket to box
197 * @param maxbuf maximum write buffer size for the socket (use
198 * 0 for sockets that need no write buffers, such as listen sockets)
199 * @return the boxed socket handle
201 struct GNUNET_NETWORK_SocketHandle *
202 GNUNET_NETWORK_socket_create_from_existing (struct GNUNET_SCHEDULER_Handle
203 *sched, int osSocket,
206 struct GNUNET_NETWORK_SocketHandle *ret;
207 ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_SocketHandle) + maxbuf);
208 ret->write_buffer = (char *) &ret[1];
209 ret->write_buffer_size = maxbuf;
210 ret->sock = osSocket;
217 * Create a socket handle by accepting on a listen socket. This
218 * function may block if the listen socket has no connection ready.
220 * @param sched scheduler to use
221 * @param access function to use to check if access is allowed
222 * @param access_cls closure for access
223 * @param lsock listen socket
224 * @param maxbuf maximum write buffer size for the socket (use
225 * 0 for sockets that need no write buffers, such as listen sockets)
226 * @return the socket handle, NULL on error
228 struct GNUNET_NETWORK_SocketHandle *
229 GNUNET_NETWORK_socket_create_from_accept (struct GNUNET_SCHEDULER_Handle
231 GNUNET_NETWORK_AccessCheck access,
232 void *access_cls, int lsock,
235 struct GNUNET_NETWORK_SocketHandle *ret;
240 struct sockaddr_in *v4;
241 struct sockaddr_in6 *v6;
245 addrlen = sizeof (addr);
246 fd = accept (lsock, (struct sockaddr *) &addr, &addrlen);
249 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "accept");
252 if (0 != fcntl (fd, F_SETFD, fcntl (fd, F_GETFD) | FD_CLOEXEC))
253 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
255 if (addrlen > sizeof (addr))
258 GNUNET_break (0 == CLOSE (fd));
262 sa = (struct sockaddr *) addr;
263 v6 = (struct sockaddr_in6 *) addr;
264 if ((sa->sa_family == AF_INET6) && (IN6_IS_ADDR_V4MAPPED (&v6->sin6_addr)))
266 /* convert to V4 address */
267 v4 = GNUNET_malloc (sizeof (struct sockaddr_in));
268 memset (v4, 0, sizeof (struct sockaddr_in));
269 v4->sin_family = AF_INET;
270 memcpy (&v4->sin_addr,
271 &((char *) &v6->sin6_addr)[sizeof (struct in6_addr) -
272 sizeof (struct in_addr)],
273 sizeof (struct in_addr));
274 v4->sin_port = v6->sin6_port;
276 addrlen = sizeof (struct sockaddr_in);
280 uaddr = GNUNET_malloc (addrlen);
281 memcpy (uaddr, addr, addrlen);
284 if ((access != NULL) &&
285 (GNUNET_YES != (aret = access (access_cls, uaddr, addrlen))))
287 if (aret == GNUNET_NO)
288 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
289 _("Access denied to `%s'\n"),
290 GNUNET_a2s(uaddr, addrlen));
291 GNUNET_break (0 == SHUTDOWN (fd, SHUT_RDWR));
292 GNUNET_break (0 == CLOSE (fd));
296 ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_SocketHandle) + maxbuf);
297 ret->write_buffer = (char *) &ret[1];
298 ret->write_buffer_size = maxbuf;
300 ret->addrlen = addrlen;
307 * Obtain the network address of the other party.
309 * @param sock the client to get the address for
310 * @param addr where to store the address
311 * @param addrlen where to store the length of the address
312 * @return GNUNET_OK on success
315 GNUNET_NETWORK_socket_get_address (struct GNUNET_NETWORK_SocketHandle *sock,
316 void **addr, size_t * addrlen)
318 if ((sock->addr == NULL) || (sock->addrlen == 0))
320 *addr = GNUNET_malloc (sock->addrlen);
321 memcpy (*addr, sock->addr, sock->addrlen);
322 *addrlen = sock->addrlen;
328 * Set if a socket should use blocking or non-blocking IO.
330 * @return GNUNET_OK on success, GNUNET_SYSERR on error
333 socket_set_blocking (int handle, int doBlock)
339 if (ioctlsocket (plibc_fd_get_handle (handle), FIONBIO, &mode) ==
342 SetErrnoFromWinsockError (WSAGetLastError ());
343 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "ioctlsocket");
344 return GNUNET_SYSERR;
347 if (ioctlsocket (handle, FIONBIO, &mode) == SOCKET_ERROR)
349 SetErrnoFromWinsockError (WSAGetLastError ());
350 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "ioctlsocket");
351 return GNUNET_SYSERR;
354 /* store the blocking mode */
356 plibc_fd_set_blocking (handle, doBlock);
358 __win_SetHandleBlockingMode (handle, doBlock);
364 int flags = fcntl (handle, F_GETFL);
367 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "fcntl");
368 return GNUNET_SYSERR;
371 flags &= ~O_NONBLOCK;
374 if (0 != fcntl (handle, F_SETFL, flags))
376 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "fcntl");
377 return GNUNET_SYSERR;
385 * Initiate asynchronous TCP connect request.
387 * @param sock what socket to connect
388 * @return GNUNET_SYSERR error (no more addresses to try)
391 try_connect (struct GNUNET_NETWORK_SocketHandle *sock)
395 if (sock->addr != NULL)
397 GNUNET_free (sock->addr);
403 if (sock->ai_pos == NULL)
405 /* no more addresses to try, fatal! */
406 return GNUNET_SYSERR;
408 switch (sock->ai_pos->ai_family)
411 ((struct sockaddr_in *) sock->ai_pos->ai_addr)->sin_port =
415 ((struct sockaddr_in6 *) sock->ai_pos->ai_addr)->sin6_port =
419 sock->ai_pos = sock->ai_pos->ai_next;
422 s = SOCKET (sock->ai_pos->ai_family, SOCK_STREAM, 0);
425 /* maybe unsupported address family, try next */
426 GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "socket");
427 sock->ai_pos = sock->ai_pos->ai_next;
430 if (0 != fcntl (s, F_SETFD, fcntl (s, F_GETFD) | FD_CLOEXEC))
431 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
433 if (GNUNET_SYSERR == socket_set_blocking (s, GNUNET_NO))
435 /* we'll treat this one as fatal */
436 GNUNET_break (0 == CLOSE (s));
437 return GNUNET_SYSERR;
439 if ((0 != CONNECT (s,
440 sock->ai_pos->ai_addr,
441 sock->ai_pos->ai_addrlen)) && (errno != EINPROGRESS))
443 /* maybe refused / unsupported address, try next */
444 GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "connect");
445 GNUNET_break (0 == CLOSE (s));
450 /* got one! copy address information! */
451 sock->addrlen = sock->ai_pos->ai_addrlen;
452 sock->addr = GNUNET_malloc (sock->addrlen);
453 memcpy (sock->addr, sock->ai_pos->ai_addr, sock->addrlen);
454 sock->ai_pos = sock->ai_pos->ai_next;
461 * Scheduler let us know that we're either ready to
462 * write on the socket OR connect timed out. Do the
466 connect_continuation (void *cls,
467 const struct GNUNET_SCHEDULER_TaskContext *tc)
469 struct GNUNET_NETWORK_SocketHandle *sock = cls;
473 /* nobody needs to wait for us anymore... */
474 sock->connect_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
475 /* Note: write-ready does NOT mean connect succeeded,
476 we need to use getsockopt to be sure */
477 len = sizeof (error);
480 if ((0 == (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) ||
481 (0 != getsockopt (sock->sock, SOL_SOCKET, SO_ERROR, &error, &len)) ||
482 (error != 0) || (errno != 0))
485 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
486 "Failed to establish TCP connection to `%s'\n",
487 GNUNET_a2s(sock->addr, sock->addrlen));
489 /* connect failed / timed out */
490 GNUNET_break (0 == CLOSE (sock->sock));
492 if (GNUNET_SYSERR == try_connect (sock))
494 /* failed for good */
496 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
497 "Failed to establish TCP connection, no further addresses to try.\n");
499 /* connect failed / timed out */
500 GNUNET_break (sock->ai_pos == NULL);
501 freeaddrinfo (sock->ai);
505 sock->connect_task = GNUNET_SCHEDULER_add_write (tc->sched, GNUNET_NO, /* abort on shutdown */
506 GNUNET_SCHEDULER_PRIORITY_KEEP,
507 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
508 GNUNET_NETWORK_CONNECT_RETRY_TIMEOUT,
510 &connect_continuation,
514 /* connect succeeded! clean up "ai" */
516 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connection succeeded!\n");
518 freeaddrinfo (sock->ai);
525 * Create a socket handle by (asynchronously) connecting to a host.
526 * This function returns immediately, even if the connection has not
527 * yet been established. This function only creates TCP connections.
529 * @param sched scheduler to use
530 * @param hostname name of the host to connect to
531 * @param port port to connect to
532 * @param maxbuf maximum write buffer size for the socket (use
533 * 0 for sockets that need no write buffers, such as listen sockets)
534 * @return the socket handle
536 struct GNUNET_NETWORK_SocketHandle *
537 GNUNET_NETWORK_socket_create_from_connect (struct GNUNET_SCHEDULER_Handle
538 *sched, const char *hostname,
539 uint16_t port, size_t maxbuf)
541 struct GNUNET_NETWORK_SocketHandle *ret;
542 struct addrinfo hints;
545 ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_SocketHandle) + maxbuf);
548 ret->write_buffer = (char *) &ret[1];
549 ret->write_buffer_size = maxbuf;
551 memset (&hints, 0, sizeof (hints));
552 hints.ai_family = AF_UNSPEC;
553 hints.ai_socktype = SOCK_STREAM;
554 if (0 != (ec = getaddrinfo (hostname, NULL, &hints, &ret->ai)))
556 GNUNET_log (GNUNET_ERROR_TYPE_INFO | GNUNET_ERROR_TYPE_BULK,
557 "`%s' failed for hostname `%s': %s\n",
558 "getaddrinfo", hostname, gai_strerror (ec));
562 ret->ai_pos = ret->ai;
563 if (GNUNET_SYSERR == try_connect (ret))
565 freeaddrinfo (ret->ai);
569 ret->connect_task = GNUNET_SCHEDULER_add_write (sched, GNUNET_NO, /* abort on shutdown */
570 GNUNET_SCHEDULER_PRIORITY_KEEP,
571 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
572 GNUNET_NETWORK_CONNECT_RETRY_TIMEOUT,
574 &connect_continuation, ret);
581 * Create a socket handle by (asynchronously) connecting to a host.
582 * This function returns immediately, even if the connection has not
583 * yet been established. This function only creates TCP connections.
585 * @param sched scheduler to use
586 * @param af_family address family to use
587 * @param serv_addr server address
588 * @param addrlen length of server address
589 * @param maxbuf maximum write buffer size for the socket (use
590 * 0 for sockets that need no write buffers, such as listen sockets)
591 * @return the socket handle
593 struct GNUNET_NETWORK_SocketHandle *
594 GNUNET_NETWORK_socket_create_from_sockaddr (struct GNUNET_SCHEDULER_Handle
595 *sched, int af_family,
596 const struct sockaddr *serv_addr,
597 socklen_t addrlen, size_t maxbuf)
601 s = SOCKET (af_family, SOCK_STREAM, 0);
604 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING |
605 GNUNET_ERROR_TYPE_BULK, "socket");
608 if (0 != fcntl (s, F_SETFD, fcntl (s, F_GETFD) | FD_CLOEXEC))
609 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
611 if (GNUNET_SYSERR == socket_set_blocking (s, GNUNET_NO))
613 /* we'll treat this one as fatal */
614 GNUNET_break (0 == CLOSE (s));
617 if ((0 != CONNECT (s, serv_addr, addrlen)) && (errno != EINPROGRESS))
619 /* maybe refused / unsupported address, try next */
620 GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "connect");
621 GNUNET_break (0 == CLOSE (s));
624 return GNUNET_NETWORK_socket_create_from_existing (sched, s, maxbuf);
629 * Check if socket is valid (no fatal errors have happened so far).
630 * Note that a socket that is still trying to connect is considered
633 * @param sock socket to check
634 * @return GNUNET_YES if valid, GNUNET_NO otherwise
637 GNUNET_NETWORK_socket_check (struct GNUNET_NETWORK_SocketHandle *sock)
639 if (sock->ai != NULL)
640 return GNUNET_YES; /* still trying to connect */
641 return (sock->sock == -1) ? GNUNET_NO : GNUNET_YES;
646 * Scheduler let us know that the connect task is finished (or was
647 * cancelled due to shutdown). Now really clean up.
650 destroy_continuation (void *cls,
651 const struct GNUNET_SCHEDULER_TaskContext *tc)
653 struct GNUNET_NETWORK_SocketHandle *sock = cls;
654 GNUNET_NETWORK_TransmitReadyNotify notify;
656 if (sock->write_task != GNUNET_SCHEDULER_NO_PREREQUISITE_TASK)
659 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
660 "Destroy code waiting for writes to complete.\n");
662 GNUNET_SCHEDULER_add_after (sock->sched,
664 GNUNET_SCHEDULER_PRIORITY_KEEP,
666 &destroy_continuation, sock);
669 if (sock->sock != -1)
672 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down socket.\n");
674 SHUTDOWN (sock->sock, SHUT_RDWR);
676 if (sock->read_task != GNUNET_SCHEDULER_NO_PREREQUISITE_TASK)
679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
680 "Destroy code waiting for receive to complete.\n");
682 GNUNET_SCHEDULER_add_after (sock->sched,
684 GNUNET_SCHEDULER_PRIORITY_KEEP,
686 &destroy_continuation, sock);
689 if (NULL != (notify = sock->nth.notify_ready))
691 sock->nth.notify_ready = NULL;
692 notify (sock->nth.notify_ready_cls, 0, NULL);
693 if (sock->nth.timeout_task != GNUNET_SCHEDULER_NO_PREREQUISITE_TASK)
695 GNUNET_SCHEDULER_cancel (sock->sched, sock->nth.timeout_task);
696 sock->nth.timeout_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
699 if (sock->sock != -1)
702 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Closing socket.\n");
704 GNUNET_break (0 == CLOSE (sock->sock));
706 GNUNET_free_non_null (sock->addr);
707 if (sock->ai != NULL)
708 freeaddrinfo (sock->ai);
714 * Close the socket and free associated resources. Pending
715 * transmissions are simply dropped. A pending receive call will be
716 * called with an error code of "EPIPE".
718 * @param sock socket to destroy
721 GNUNET_NETWORK_socket_destroy (struct GNUNET_NETWORK_SocketHandle *sock)
724 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
725 "Network asked to destroy socket %p\n", sock);
727 if (sock->write_buffer_off == 0)
728 sock->ai_pos = NULL; /* if we're still trying to connect and have
729 no message pending, stop trying! */
730 GNUNET_assert (sock->sched != NULL);
731 GNUNET_SCHEDULER_add_after (sock->sched,
733 GNUNET_SCHEDULER_PRIORITY_KEEP,
735 &destroy_continuation, sock);
739 * Tell the receiver callback that a timeout was reached.
742 signal_timeout (struct GNUNET_NETWORK_SocketHandle *sh)
744 GNUNET_NETWORK_Receiver receiver;
747 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
748 "Network signals timeout to receiver!\n");
750 GNUNET_assert (NULL != (receiver = sh->receiver));
752 receiver (sh->receiver_cls, NULL, 0, NULL, 0, 0);
757 * Tell the receiver callback that we had an IO error.
760 signal_error (struct GNUNET_NETWORK_SocketHandle *sh, int errcode)
762 GNUNET_NETWORK_Receiver receiver;
763 GNUNET_assert (NULL != (receiver = sh->receiver));
765 receiver (sh->receiver_cls, NULL, 0, sh->addr, sh->addrlen, errcode);
770 * This function is called once we either timeout
771 * or have data ready to read.
774 receive_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
776 struct GNUNET_NETWORK_SocketHandle *sh = cls;
777 struct GNUNET_TIME_Absolute now;
778 char buffer[sh->max];
780 GNUNET_NETWORK_Receiver receiver;
782 sh->read_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
783 now = GNUNET_TIME_absolute_get ();
784 if ((now.value > sh->receive_timeout.value) ||
785 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT)) ||
786 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
789 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
790 "Receive encounters error: timeout...\n");
797 /* connect failed for good */
799 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
800 "Receive encounters error, socket closed...\n");
802 signal_error (sh, ECONNREFUSED);
805 GNUNET_assert (FD_ISSET (sh->sock, tc->read_ready));
807 ret = RECV (sh->sock, buffer, sh->max, MSG_DONTWAIT);
813 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
814 "Error receiving: %s\n", STRERROR (errno));
816 signal_error (sh, errno);
820 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
821 "Receive got %d bytes from OS!\n", ret);
823 GNUNET_assert (NULL != (receiver = sh->receiver));
825 receiver (sh->receiver_cls, buffer, ret, sh->addr, sh->addrlen, 0);
830 * This function is called after establishing a connection either has
831 * succeeded or timed out. Note that it is possible that the attempt
832 * timed out and that we're immediately retrying. If we are retrying,
833 * we need to wait again (or timeout); if we succeeded, we need to
834 * wait for data (or timeout).
837 receive_again (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
839 struct GNUNET_NETWORK_SocketHandle *sh = cls;
840 struct GNUNET_TIME_Absolute now;
842 sh->read_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
843 if ((sh->sock == -1) &&
844 (sh->connect_task == GNUNET_SCHEDULER_NO_PREREQUISITE_TASK))
846 /* not connected and no longer trying */
848 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
849 "Receive encounters error, socket closed...\n");
851 signal_error (sh, ECONNREFUSED);
854 now = GNUNET_TIME_absolute_get ();
855 if ((now.value > sh->receive_timeout.value) ||
856 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
859 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
860 "Receive encounters error: timeout...\n");
865 if (sh->connect_task != GNUNET_SCHEDULER_NO_PREREQUISITE_TASK)
867 /* connect was retried */
869 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
870 "Receive still waits on connect...\n");
872 sh->read_task = GNUNET_SCHEDULER_add_after (tc->sched,
874 GNUNET_SCHEDULER_PRIORITY_KEEP,
880 /* connect succeeded, wait for data! */
882 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
883 "Receive now waits for socket...\n");
885 sh->read_task = GNUNET_SCHEDULER_add_read (tc->sched,
887 GNUNET_SCHEDULER_PRIORITY_KEEP,
889 GNUNET_TIME_absolute_get_remaining
890 (sh->receive_timeout),
891 sh->sock, &receive_ready,
898 * Receive data from the given socket. Note that this function will
899 * call "receiver" asynchronously using the scheduler. It will
900 * "immediately" return. Note that there MUST only be one active
901 * receive call per socket at any given point in time (so do not
902 * call receive again until the receiver callback has been invoked).
904 * @param sched scheduler to use
905 * @param sock socket handle
906 * @param max maximum number of bytes to read
907 * @param timeout maximum amount of time to wait (use -1 for "forever")
908 * @param receiver function to call with received data
909 * @param receiver_cls closure for receiver
910 * @return scheduler task ID used for receiving, GNUNET_SCHEDULER_NO_PREREQUISITE_TASK on error
912 GNUNET_SCHEDULER_TaskIdentifier
913 GNUNET_NETWORK_receive (struct GNUNET_NETWORK_SocketHandle *sock,
915 struct GNUNET_TIME_Relative timeout,
916 GNUNET_NETWORK_Receiver receiver, void *receiver_cls)
918 struct GNUNET_SCHEDULER_TaskContext tc;
920 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
921 "Network asked to receive from socket...\n");
923 GNUNET_assert ((sock->read_task == GNUNET_SCHEDULER_NO_PREREQUISITE_TASK) &&
924 (sock->receiver == NULL));
925 sock->receiver = receiver;
926 sock->receiver_cls = receiver_cls;
927 sock->receive_timeout = GNUNET_TIME_relative_to_absolute (timeout);
929 memset (&tc, 0, sizeof (tc));
930 tc.sched = sock->sched;
931 tc.reason = GNUNET_SCHEDULER_REASON_PREREQ_DONE;
932 receive_again (sock, &tc);
933 return sock->read_task;
938 * Cancel receive job on the given socket. Note that the
939 * receiver callback must not have been called yet in order
940 * for the cancellation to be valid.
942 * @param sock socket handle
943 * @param task task identifier returned from the receive call
944 * @return closure of the original receiver callback
947 GNUNET_NETWORK_receive_cancel (struct GNUNET_NETWORK_SocketHandle *sock,
948 GNUNET_SCHEDULER_TaskIdentifier task)
950 GNUNET_assert (sock->read_task == task);
951 GNUNET_assert (sock == GNUNET_SCHEDULER_cancel (sock->sched, task));
952 sock->read_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
953 sock->receiver = NULL;
954 return sock->receiver_cls;
959 * Try to call the transmit notify method (check if we do
960 * have enough space available first)!
962 * @param sock socket for which we should do this processing
963 * @return GNUNET_YES if we were able to call notify
966 process_notify (struct GNUNET_NETWORK_SocketHandle *sock)
971 GNUNET_NETWORK_TransmitReadyNotify notify;
973 GNUNET_assert (sock->write_task == GNUNET_SCHEDULER_NO_PREREQUISITE_TASK);
974 if (NULL == (notify = sock->nth.notify_ready))
976 used = sock->write_buffer_off - sock->write_buffer_pos;
977 avail = sock->write_buffer_size - used;
978 size = sock->nth.notify_size;
979 if (sock->nth.notify_size > avail)
981 sock->nth.notify_ready = NULL;
982 if (sock->nth.timeout_task != GNUNET_SCHEDULER_NO_PREREQUISITE_TASK)
984 GNUNET_SCHEDULER_cancel (sock->sched, sock->nth.timeout_task);
985 sock->nth.timeout_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
987 if (sock->write_buffer_size - sock->write_buffer_off < size)
989 /* need to compact */
990 memmove (sock->write_buffer,
991 &sock->write_buffer[sock->write_buffer_pos], used);
992 sock->write_buffer_off -= sock->write_buffer_pos;
993 sock->write_buffer_pos = 0;
995 GNUNET_assert (sock->write_buffer_size - sock->write_buffer_off >= size);
996 size = notify (sock->nth.notify_ready_cls,
997 sock->write_buffer_size - sock->write_buffer_off,
998 &sock->write_buffer[sock->write_buffer_off]);
999 sock->write_buffer_off += size;
1005 * Task invoked by the scheduler when a call to transmit
1006 * is timing out (we never got enough buffer space to call
1007 * the callback function before the specified timeout
1010 * This task notifies the client about the timeout.
1013 transmit_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1015 struct GNUNET_NETWORK_SocketHandle *sock = cls;
1016 GNUNET_NETWORK_TransmitReadyNotify notify;
1019 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transmit fails, time out reached.\n");
1021 notify = sock->nth.notify_ready;
1022 sock->nth.notify_ready = NULL;
1023 notify (sock->nth.notify_ready_cls, 0, NULL);
1028 transmit_error (struct GNUNET_NETWORK_SocketHandle *sock)
1030 if (sock->nth.notify_ready == NULL)
1031 return; /* nobody to tell about it */
1032 if (sock->nth.timeout_task != GNUNET_SCHEDULER_NO_PREREQUISITE_TASK)
1034 GNUNET_SCHEDULER_cancel (sock->sched, sock->nth.timeout_task);
1035 sock->nth.timeout_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
1037 transmit_timeout (sock, NULL);
1042 * See if we are now connected. If not, wait longer for
1043 * connect to succeed. If connected, we should be able
1044 * to write now as well, unless we timed out.
1047 transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1049 struct GNUNET_NETWORK_SocketHandle *sock = cls;
1053 GNUNET_assert (sock->write_task != GNUNET_SCHEDULER_NO_PREREQUISITE_TASK);
1054 sock->write_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
1055 if (sock->connect_task != GNUNET_SCHEDULER_NO_PREREQUISITE_TASK)
1057 /* still waiting for connect */
1059 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1060 "Transmission still waiting for connect...\n");
1062 GNUNET_assert (sock->write_task ==
1063 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK);
1065 GNUNET_SCHEDULER_add_delayed (tc->sched, GNUNET_NO,
1066 GNUNET_SCHEDULER_PRIORITY_KEEP,
1068 GNUNET_TIME_UNIT_ZERO, &transmit_ready,
1072 if (sock->sock == -1)
1074 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1076 ("Did not transmit request, socket closed or connect failed.\n"));
1077 transmit_error (sock);
1078 return; /* connect failed for good, we're finished */
1080 if ((tc->write_ready == NULL) || (!FD_ISSET (sock->sock, tc->write_ready)))
1083 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1084 "Socket not yet ready for writing, will wait for that.\n");
1086 goto SCHEDULE_WRITE;
1089 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1090 "Transmit ready called --- will try to send\n");
1092 GNUNET_assert (sock->write_buffer_off >= sock->write_buffer_pos);
1093 process_notify (sock);
1094 have = sock->write_buffer_off - sock->write_buffer_pos;
1098 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No data ready for writing.\n");
1103 ret = SEND (sock->sock,
1104 &sock->write_buffer[sock->write_buffer_pos],
1105 have, MSG_DONTWAIT | MSG_NOSIGNAL);
1111 GNUNET_log_strerror (GNUNET_ERROR_TYPE_DEBUG, "send");
1113 SHUTDOWN (sock->sock, SHUT_RDWR);
1114 GNUNET_break (0 == CLOSE (sock->sock));
1116 transmit_error (sock);
1120 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transmitted %d bytes to OS\n", ret);
1122 sock->write_buffer_pos += ret;
1123 if (sock->write_buffer_pos == sock->write_buffer_off)
1125 /* transmitted all pending data */
1126 sock->write_buffer_pos = 0;
1127 sock->write_buffer_off = 0;
1129 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1130 "Transmission buffer now empty.\n", ret);
1133 if ((sock->write_buffer_off == 0) && (NULL == sock->nth.notify_ready))
1134 return; /* all data sent! */
1135 /* not done writing, schedule more */
1137 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1138 "More data ready for transmission, scheduling task again!\n");
1141 if (sock->write_task == GNUNET_SCHEDULER_NO_PREREQUISITE_TASK)
1143 GNUNET_SCHEDULER_add_write (tc->sched,
1145 GNUNET_SCHEDULER_PRIORITY_KEEP,
1146 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
1147 GNUNET_TIME_UNIT_FOREVER_REL,
1148 sock->sock, &transmit_ready, sock);
1153 * Ask the socket to call us once the specified number of bytes
1154 * are free in the transmission buffer. May call the notify
1155 * method immediately if enough space is available.
1157 * @param sock socket
1158 * @param size number of bytes to send
1159 * @param timeout after how long should we give up (and call
1160 * notify with buf NULL and size 0)?
1161 * @param notify function to call
1162 * @param notify_cls closure for notify
1163 * @return non-NULL if the notify callback was queued,
1164 * NULL if we are already going to notify someone else (busy)
1166 struct GNUNET_NETWORK_TransmitHandle *
1167 GNUNET_NETWORK_notify_transmit_ready (struct GNUNET_NETWORK_SocketHandle
1169 struct GNUNET_TIME_Relative timeout,
1170 GNUNET_NETWORK_TransmitReadyNotify
1171 notify, void *notify_cls)
1173 if (sock->nth.notify_ready != NULL)
1175 GNUNET_assert (notify != NULL);
1176 GNUNET_assert (sock->write_buffer_size >= size);
1178 if ((sock->sock == -1) &&
1179 (sock->connect_task == GNUNET_SCHEDULER_NO_PREREQUISITE_TASK))
1182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1183 "Transmit fails, connection failed.\n");
1185 notify (notify_cls, 0, NULL);
1188 GNUNET_assert (sock->write_buffer_off <= sock->write_buffer_size);
1189 GNUNET_assert (sock->write_buffer_pos <= sock->write_buffer_size);
1190 GNUNET_assert (sock->write_buffer_pos <= sock->write_buffer_off);
1191 sock->nth.notify_ready = notify;
1192 sock->nth.notify_ready_cls = notify_cls;
1193 sock->nth.sh = sock;
1194 sock->nth.notify_size = size;
1195 sock->nth.transmit_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1196 sock->nth.timeout_task = GNUNET_SCHEDULER_add_delayed (sock->sched,
1198 GNUNET_SCHEDULER_PRIORITY_KEEP,
1199 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
1204 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1205 "Scheduling asynchronous transmission once connect is done...\n");
1207 if (sock->write_task == GNUNET_SCHEDULER_NO_PREREQUISITE_TASK)
1208 sock->write_task = GNUNET_SCHEDULER_add_delayed (sock->sched,
1210 GNUNET_SCHEDULER_PRIORITY_KEEP,
1212 GNUNET_TIME_UNIT_ZERO,
1213 &transmit_ready, sock);
1219 * Cancel the specified transmission-ready
1223 GNUNET_NETWORK_notify_transmit_ready_cancel (struct
1224 GNUNET_NETWORK_TransmitHandle *h)
1226 GNUNET_assert (h->notify_ready != NULL);
1227 GNUNET_SCHEDULER_cancel (h->sh->sched, h->timeout_task);
1228 h->timeout_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
1229 h->notify_ready = NULL;
1233 #if 0 /* keep Emacsens' auto-indent happy */