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/connection.c
23 * @brief TCP connection management
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.
35 #include "gnunet_common.h"
36 #include "gnunet_connection_lib.h"
37 #include "gnunet_container_lib.h"
38 #include "gnunet_resolver_service.h"
39 #include "gnunet_scheduler_lib.h"
41 #define DEBUG_CONNECTION GNUNET_NO
45 * Possible functions to call after connect failed or succeeded.
47 enum ConnectContinuations
55 * Call "receive_again".
57 COCO_RECEIVE_AGAIN = 1,
60 * Call "transmit_ready".
62 COCO_TRANSMIT_READY = 2,
65 * Call "destroy_continuation".
67 COCO_DESTROY_CONTINUATION = 4
72 * Transmission handle. There can only be one for each connection.
74 struct GNUNET_CONNECTION_TransmitHandle
78 * Function to call if the send buffer has notify_size
81 GNUNET_CONNECTION_TransmitReadyNotify notify_ready;
84 * Closure for notify_ready.
86 void *notify_ready_cls;
91 struct GNUNET_CONNECTION_Handle *sh;
94 * Timeout for receiving (in absolute time).
96 struct GNUNET_TIME_Absolute transmit_timeout;
99 * Task called on timeout.
101 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
104 * At what number of bytes available in the
105 * write buffer should the notify method be called?
113 * During connect, we try multiple possible IP addresses
114 * to find out which one might work.
120 * This is a linked list.
122 struct AddressProbe *next;
125 * This is a doubly-linked list.
127 struct AddressProbe *prev;
130 * The address; do not free (allocated at the end of this struct).
132 const struct sockaddr *addr;
135 * Underlying OS's socket.
137 struct GNUNET_NETWORK_Handle *sock;
140 * Connection for which we are probing.
142 struct GNUNET_CONNECTION_Handle *h;
150 * Task waiting for the socket to finish connecting.
152 GNUNET_SCHEDULER_TaskIdentifier task;
157 * @brief handle for a network socket
159 struct GNUNET_CONNECTION_Handle
163 * Scheduler that was used for the connect task.
165 struct GNUNET_SCHEDULER_Handle *sched;
168 * Configuration to use.
170 const struct GNUNET_CONFIGURATION_Handle *cfg;
173 * Linked list of sockets we are currently trying out
176 struct AddressProbe *ap_head;
179 * Linked list of sockets we are currently trying out
182 struct AddressProbe *ap_tail;
185 * Network address of the other end-point, may be NULL.
187 struct sockaddr *addr;
190 * Pointer to the hostname if socket was
191 * created using DNS lookup, otherwise NULL.
196 * Underlying OS's socket, set to NULL after fatal errors.
198 struct GNUNET_NETWORK_Handle *sock;
201 * Function to call on data received, NULL if no receive is pending.
203 GNUNET_CONNECTION_Receiver receiver;
206 * Closure for receiver.
211 * Pointer to our write buffer.
216 * Size of our write buffer.
218 size_t write_buffer_size;
221 * Current write-offset in write buffer (where
222 * would we write next).
224 size_t write_buffer_off;
227 * Current read-offset in write buffer (how many
228 * bytes have already been send).
230 size_t write_buffer_pos;
238 * Read task that we may need to wait for.
240 GNUNET_SCHEDULER_TaskIdentifier read_task;
243 * Write task that we may need to wait for.
245 GNUNET_SCHEDULER_TaskIdentifier write_task;
248 * Destroy task (if already scheduled).
250 GNUNET_SCHEDULER_TaskIdentifier destroy_task;
253 * Handle to a pending DNS lookup request.
255 struct GNUNET_RESOLVER_RequestHandle *dns_active;
258 * The handle we return for GNUNET_CONNECTION_notify_transmit_ready.
260 struct GNUNET_CONNECTION_TransmitHandle nth;
263 * Timeout for receiving (in absolute time).
265 struct GNUNET_TIME_Absolute receive_timeout;
268 * Functions to call after connect failed or succeeded.
270 enum ConnectContinuations ccs;
273 * Maximum number of bytes to read (for receiving).
278 * Ignore GNUNET_SCHEDULER_REASON_SHUTDOWN for this socket.
283 * Port to connect to.
288 * When shutdown, do not ever actually close the socket, but
289 * free resources. Only should ever be set if using program
290 * termination as a signal (because only then will the leaked
298 * Set the persist option on this connection handle. Indicates
299 * that the underlying socket or fd should never really be closed.
300 * Used for indicating process death.
302 * @param sock the connection to set persistent
304 void GNUNET_CONNECTION_persist_(struct GNUNET_CONNECTION_Handle *sock)
306 sock->persist = GNUNET_YES;
310 * Create a socket handle by boxing an existing OS socket. The OS
311 * socket should henceforth be no longer used directly.
312 * GNUNET_socket_destroy will close it.
314 * @param sched scheduler to use
315 * @param osSocket existing socket to box
316 * @param maxbuf maximum write buffer size for the socket (use
317 * 0 for sockets that need no write buffers, such as listen sockets)
318 * @return the boxed socket handle
320 struct GNUNET_CONNECTION_Handle *
321 GNUNET_CONNECTION_create_from_existing (struct GNUNET_SCHEDULER_Handle
323 struct GNUNET_NETWORK_Handle
324 *osSocket, size_t maxbuf)
326 struct GNUNET_CONNECTION_Handle *ret;
327 ret = GNUNET_malloc (sizeof (struct GNUNET_CONNECTION_Handle) + maxbuf);
328 ret->write_buffer = (char *) &ret[1];
329 ret->write_buffer_size = maxbuf;
330 ret->sock = osSocket;
337 * Create a socket handle by accepting on a listen socket. This
338 * function may block if the listen socket has no connection ready.
340 * @param sched scheduler to use
341 * @param access function to use to check if access is allowed
342 * @param access_cls closure for access
343 * @param lsock listen socket
344 * @param maxbuf maximum write buffer size for the socket (use
345 * 0 for sockets that need no write buffers, such as listen sockets)
346 * @return the socket handle, NULL on error
348 struct GNUNET_CONNECTION_Handle *
349 GNUNET_CONNECTION_create_from_accept (struct GNUNET_SCHEDULER_Handle
351 GNUNET_CONNECTION_AccessCheck access,
353 struct GNUNET_NETWORK_Handle *lsock,
356 struct GNUNET_CONNECTION_Handle *ret;
359 struct GNUNET_NETWORK_Handle *sock;
361 struct sockaddr_in *v4;
362 struct sockaddr_in6 *v6;
366 addrlen = sizeof (addr);
368 GNUNET_NETWORK_socket_accept (lsock, (struct sockaddr *) &addr, &addrlen);
371 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "accept");
374 if (addrlen > sizeof (addr))
377 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
381 sa = (struct sockaddr *) addr;
382 v6 = (struct sockaddr_in6 *) addr;
383 if ((sa->sa_family == AF_INET6) && (IN6_IS_ADDR_V4MAPPED (&v6->sin6_addr)))
385 /* convert to V4 address */
386 v4 = GNUNET_malloc (sizeof (struct sockaddr_in));
387 memset (v4, 0, sizeof (struct sockaddr_in));
388 v4->sin_family = AF_INET;
389 memcpy (&v4->sin_addr,
390 &((char *) &v6->sin6_addr)[sizeof (struct in6_addr) -
391 sizeof (struct in_addr)],
392 sizeof (struct in_addr));
393 v4->sin_port = v6->sin6_port;
395 addrlen = sizeof (struct sockaddr_in);
399 uaddr = GNUNET_malloc (addrlen);
400 memcpy (uaddr, addr, addrlen);
403 if ((access != NULL) &&
404 (GNUNET_YES != (aret = access (access_cls, uaddr, addrlen))))
406 if (aret == GNUNET_NO)
407 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
408 _("Access denied to `%s'\n"),
409 GNUNET_a2s (uaddr, addrlen));
410 GNUNET_break (GNUNET_OK ==
411 GNUNET_NETWORK_socket_shutdown (sock, SHUT_RDWR));
412 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
416 ret = GNUNET_malloc (sizeof (struct GNUNET_CONNECTION_Handle) + maxbuf);
417 ret->write_buffer = (char *) &ret[1];
418 ret->write_buffer_size = maxbuf;
420 ret->addrlen = addrlen;
424 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
425 _("Accepting connection from `%s': %p\n"),
426 GNUNET_a2s (uaddr, addrlen), ret);
432 * Obtain the network address of the other party.
434 * @param sock the client to get the address for
435 * @param addr where to store the address
436 * @param addrlen where to store the length of the address
437 * @return GNUNET_OK on success
440 GNUNET_CONNECTION_get_address (struct GNUNET_CONNECTION_Handle *sock,
441 void **addr, size_t * addrlen)
443 if ((sock->addr == NULL) || (sock->addrlen == 0))
445 *addr = GNUNET_malloc (sock->addrlen);
446 memcpy (*addr, sock->addr, sock->addrlen);
447 *addrlen = sock->addrlen;
453 * This function is called after establishing a connection either has
454 * succeeded or timed out. Note that it is possible that the attempt
455 * timed out and that we're immediately retrying. If we are retrying,
456 * we need to wait again (or timeout); if we succeeded, we need to
457 * wait for data (or timeout).
459 * @param cls our connection handle
460 * @param tc task context describing why we are here
463 receive_again (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
467 * Scheduler let us know that the connect task is finished (or was
468 * cancelled due to shutdown). Now really clean up.
470 * @param cls our "struct GNUNET_CONNECTION_Handle *"
474 destroy_continuation (void *cls,
475 const struct GNUNET_SCHEDULER_TaskContext *tc)
477 struct GNUNET_CONNECTION_Handle *sock = cls;
478 GNUNET_CONNECTION_TransmitReadyNotify notify;
479 struct AddressProbe *pos;
481 sock->destroy_task = GNUNET_SCHEDULER_NO_TASK;
482 GNUNET_assert (sock->dns_active == NULL);
483 if (0 != (sock->ccs & COCO_TRANSMIT_READY))
486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
487 "Destroy waits for CCS-TR to be done (%p)\n", sock);
489 sock->ccs |= COCO_DESTROY_CONTINUATION;
492 if (sock->write_task != GNUNET_SCHEDULER_NO_TASK)
495 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
496 "Destroy waits for write_task to be done (%p)\n", sock);
498 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task);
500 = GNUNET_SCHEDULER_add_after (sock->sched,
502 &destroy_continuation, sock);
505 if (0 != (sock->ccs & COCO_RECEIVE_AGAIN))
507 sock->ccs |= COCO_DESTROY_CONTINUATION;
510 if (sock->sock != NULL)
513 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
514 "Shutting down socket (%p)\n", sock);
516 if (sock->persist != GNUNET_YES)
517 GNUNET_NETWORK_socket_shutdown (sock->sock, SHUT_RDWR);
519 if (sock->read_task != GNUNET_SCHEDULER_NO_TASK)
521 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task);
523 = GNUNET_SCHEDULER_add_after (sock->sched,
525 &destroy_continuation, sock);
529 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
530 "Destroy actually runs (%p)!\n", sock);
532 if (sock->dns_active != NULL)
534 GNUNET_RESOLVER_request_cancel (sock->dns_active);
535 sock->dns_active = NULL;
537 while (NULL != (pos = sock->ap_head))
539 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (pos->sock));
540 GNUNET_SCHEDULER_cancel (sock->sched, pos->task);
541 GNUNET_CONTAINER_DLL_remove (sock->ap_head, sock->ap_tail, pos);
544 GNUNET_assert (sock->nth.timeout_task == GNUNET_SCHEDULER_NO_TASK);
545 GNUNET_assert (sock->ccs == COCO_NONE);
546 if (NULL != (notify = sock->nth.notify_ready))
548 sock->nth.notify_ready = NULL;
549 notify (sock->nth.notify_ready_cls, 0, NULL);
552 if (sock->sock != NULL)
554 if (sock->persist != GNUNET_YES)
555 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock->sock));
557 GNUNET_free (sock->sock); /* at least no memory leak (we deliberately
558 leak the socket in this special case) ... */
560 GNUNET_free_non_null (sock->addr);
561 GNUNET_free_non_null (sock->hostname);
562 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task);
564 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
565 "Freeing memory of connection %p.\n", sock);
573 * See if we are now connected. If not, wait longer for
574 * connect to succeed. If connected, we should be able
575 * to write now as well, unless we timed out.
577 * @param cls our connection handle
578 * @param tc task context describing why we are here
581 transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
585 * We've failed for good to establish a connection.
587 * @param h the connection we tried to establish
590 connect_fail_continuation (struct GNUNET_CONNECTION_Handle *h)
592 GNUNET_log ((0 != strncmp (h->hostname,
595 ? GNUNET_ERROR_TYPE_INFO
596 : GNUNET_ERROR_TYPE_WARNING,
597 _("Failed to establish TCP connection to `%s:%u', no further addresses to try.\n"),
598 h->hostname, h->port);
599 /* connect failed / timed out */
600 GNUNET_break (h->ap_head == NULL);
601 GNUNET_break (h->ap_tail == NULL);
602 GNUNET_break (h->dns_active == GNUNET_NO);
603 GNUNET_break (h->sock == NULL);
605 /* trigger jobs that used to wait on "connect_task" */
606 if (0 != (h->ccs & COCO_RECEIVE_AGAIN))
609 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
610 "connect_fail_continuation triggers receive_again (%p)\n",
613 h->ccs -= COCO_RECEIVE_AGAIN;
614 h->read_task = GNUNET_SCHEDULER_add_after (h->sched,
615 GNUNET_SCHEDULER_NO_TASK,
618 if (0 != (h->ccs & COCO_TRANSMIT_READY))
621 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
622 "connect_fail_continuation cancels timeout_task, triggers transmit_ready (%p)\n",
625 GNUNET_assert (h->nth.timeout_task != GNUNET_SCHEDULER_NO_TASK);
626 GNUNET_SCHEDULER_cancel (h->sched, h->nth.timeout_task);
627 h->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK;
628 h->ccs -= COCO_TRANSMIT_READY;
629 h->write_task = GNUNET_SCHEDULER_add_after (h->sched,
630 GNUNET_SCHEDULER_NO_TASK,
633 if (0 != (h->ccs & COCO_DESTROY_CONTINUATION))
636 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
637 "connect_fail_continuation runs destroy_continuation (%p)\n",
640 h->ccs -= COCO_DESTROY_CONTINUATION;
641 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == h->destroy_task);
643 = GNUNET_SCHEDULER_add_now (h->sched,
644 &destroy_continuation,
651 * We've succeeded in establishing a connection.
653 * @param h the connection we tried to establish
656 connect_success_continuation (struct GNUNET_CONNECTION_Handle *h)
659 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
660 "Connection to `%s' succeeded! (%p)\n",
661 GNUNET_a2s (h->addr, h->addrlen), h);
663 /* trigger jobs that waited for the connection */
664 if (0 != (h->ccs & COCO_RECEIVE_AGAIN))
667 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
668 "connect_success_continuation runs receive_again (%p)\n",
671 h->ccs -= COCO_RECEIVE_AGAIN;
672 h->read_task = GNUNET_SCHEDULER_add_after (h->sched,
673 GNUNET_SCHEDULER_NO_TASK,
676 if (0 != (h->ccs & COCO_TRANSMIT_READY))
679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
680 "connect_success_continuation runs transmit_ready, cancels timeout_task (%p)\n",
683 GNUNET_assert (h->nth.timeout_task != GNUNET_SCHEDULER_NO_TASK);
684 GNUNET_SCHEDULER_cancel (h->sched, h->nth.timeout_task);
685 h->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK;
686 h->ccs -= COCO_TRANSMIT_READY;
688 GNUNET_SCHEDULER_add_write_net (h->sched,
689 GNUNET_TIME_absolute_get_remaining
690 (h->nth.transmit_timeout), h->sock,
693 if (0 != (h->ccs & COCO_DESTROY_CONTINUATION))
696 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
697 "connect_success_continuation runs destroy_continuation (%p)\n",
700 h->ccs -= COCO_DESTROY_CONTINUATION;
701 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == h->destroy_task);
703 = GNUNET_SCHEDULER_add_now (h->sched,
704 &destroy_continuation,
711 * Scheduler let us know that we're either ready to write on the
712 * socket OR connect timed out. Do the right thing.
714 * @param cls the "struct AddressProbe*" with the address that we are probing
715 * @param tc success or failure info about the connect attempt.
718 connect_probe_continuation (void *cls,
719 const struct GNUNET_SCHEDULER_TaskContext *tc)
721 struct AddressProbe *ap = cls;
722 struct GNUNET_CONNECTION_Handle *h = ap->h;
723 struct AddressProbe *pos;
727 GNUNET_CONTAINER_DLL_remove (h->ap_head, h->ap_tail, ap);
728 len = sizeof (error);
731 if ((0 == (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) ||
733 GNUNET_NETWORK_socket_getsockopt (ap->sock, SOL_SOCKET, SO_ERROR,
734 &error, &len)) || (error != 0)
737 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ap->sock));
739 if ((NULL == h->ap_head) && (h->dns_active == GNUNET_NO))
740 connect_fail_continuation (h);
743 GNUNET_assert (h->sock == NULL);
744 GNUNET_assert (ap->sock != NULL);
746 GNUNET_assert (h->addr == NULL);
747 h->addr = GNUNET_malloc (ap->addrlen);
748 memcpy (h->addr, ap->addr, ap->addrlen);
749 h->addrlen = ap->addrlen;
751 /* cancel all other attempts */
752 while (NULL != (pos = h->ap_head))
754 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (pos->sock));
755 GNUNET_SCHEDULER_cancel (h->sched, pos->task);
756 GNUNET_CONTAINER_DLL_remove (h->ap_head, h->ap_tail, pos);
759 connect_success_continuation (h);
764 * Try to establish a socket connection given the specified address.
765 * This function is called by the resolver once we have a DNS reply.
767 * @param cls our "struct GNUNET_CONNECTION_Handle *"
768 * @param addr address to try, NULL for "last call"
769 * @param addrlen length of addr
772 try_connect_using_address (void *cls,
773 const struct sockaddr *addr, socklen_t addrlen)
775 struct GNUNET_CONNECTION_Handle *h = cls;
776 struct AddressProbe *ap;
777 struct GNUNET_TIME_Relative delay;
781 h->dns_active = NULL;
782 if (NULL == h->ap_head)
783 connect_fail_continuation (h);
787 return; /* already connected */
788 GNUNET_assert (h->addr == NULL);
791 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
792 "Trying to connect using address `%s:%u/%s:%u'\n",
793 h->hostname, h->port, GNUNET_a2s (addr, addrlen), h->port);
795 ap = GNUNET_malloc (sizeof (struct AddressProbe) + addrlen);
796 ap->addr = (const struct sockaddr *) &ap[1];
797 memcpy (&ap[1], addr, addrlen);
798 ap->addrlen = addrlen;
801 switch (ap->addr->sa_family)
804 ((struct sockaddr_in *) ap->addr)->sin_port = htons (h->port);
807 ((struct sockaddr_in6 *) ap->addr)->sin6_port = htons (h->port);
812 return; /* not supported by us */
815 GNUNET_NETWORK_socket_create (ap->addr->sa_family, SOCK_STREAM, 0);
816 if (ap->sock == NULL)
819 return; /* not supported by OS */
821 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
822 _("Trying to connect to `%s' (%p)\n"),
823 GNUNET_a2s (ap->addr, ap->addrlen), h);
824 if ((GNUNET_OK != GNUNET_NETWORK_socket_connect (ap->sock,
827 (errno != EINPROGRESS))
829 /* maybe refused / unsupported address, try next */
830 GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "connect");
831 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ap->sock));
835 GNUNET_CONTAINER_DLL_insert (h->ap_head, h->ap_tail, ap);
836 delay = GNUNET_CONNECTION_CONNECT_RETRY_TIMEOUT;
837 if (h->nth.notify_ready != NULL)
838 delay = GNUNET_TIME_relative_min (delay,
839 GNUNET_TIME_absolute_get_remaining
840 (h->nth.transmit_timeout));
841 if (h->receiver != NULL)
842 delay = GNUNET_TIME_relative_min (delay,
843 GNUNET_TIME_absolute_get_remaining
844 (h->receive_timeout));
846 GNUNET_SCHEDULER_add_write_net (h->sched, delay, ap->sock,
847 &connect_probe_continuation, ap);
852 * Create a socket handle by (asynchronously) connecting to a host.
853 * This function returns immediately, even if the connection has not
854 * yet been established. This function only creates TCP connections.
856 * @param sched scheduler to use
857 * @param cfg configuration to use
858 * @param hostname name of the host to connect to
859 * @param port port to connect to
860 * @param maxbuf maximum write buffer size for the socket (use
861 * 0 for sockets that need no write buffers, such as listen sockets)
862 * @return the socket handle
864 struct GNUNET_CONNECTION_Handle *
865 GNUNET_CONNECTION_create_from_connect (struct GNUNET_SCHEDULER_Handle *sched,
867 GNUNET_CONFIGURATION_Handle *cfg,
868 const char *hostname, uint16_t port,
871 struct GNUNET_CONNECTION_Handle *ret;
873 GNUNET_assert (0 < strlen (hostname)); /* sanity check */
874 ret = GNUNET_malloc (sizeof (struct GNUNET_CONNECTION_Handle) + maxbuf);
877 ret->write_buffer = (char *) &ret[1];
878 ret->write_buffer_size = maxbuf;
880 ret->hostname = GNUNET_strdup (hostname);
881 ret->dns_active = GNUNET_RESOLVER_ip_get (sched,
885 GNUNET_CONNECTION_CONNECT_RETRY_TIMEOUT,
886 &try_connect_using_address, ret);
892 * Create a socket handle by (asynchronously) connecting to a host.
893 * This function returns immediately, even if the connection has not
894 * yet been established. This function only creates TCP connections.
896 * @param sched scheduler to use
897 * @param af_family address family to use
898 * @param serv_addr server address
899 * @param addrlen length of server address
900 * @param maxbuf maximum write buffer size for the socket (use
901 * 0 for sockets that need no write buffers, such as listen sockets)
902 * @return the socket handle
904 struct GNUNET_CONNECTION_Handle *
905 GNUNET_CONNECTION_create_from_sockaddr (struct GNUNET_SCHEDULER_Handle
906 *sched, int af_family,
907 const struct sockaddr *serv_addr,
908 socklen_t addrlen, size_t maxbuf)
910 struct GNUNET_NETWORK_Handle *s;
911 struct GNUNET_CONNECTION_Handle *ret;
913 s = GNUNET_NETWORK_socket_create (af_family, SOCK_STREAM, 0);
916 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING |
917 GNUNET_ERROR_TYPE_BULK, "socket");
920 if ((GNUNET_OK != GNUNET_NETWORK_socket_connect (s, serv_addr, addrlen))
921 && (errno != EINPROGRESS))
923 /* maybe refused / unsupported address, try next */
924 GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "connect");
925 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (s));
928 ret = GNUNET_CONNECTION_create_from_existing (sched, s, maxbuf);
929 ret->addr = GNUNET_malloc (addrlen);
930 memcpy (ret->addr, serv_addr, addrlen);
931 ret->addrlen = addrlen;
933 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
934 _("Trying to connect to `%s' (%p)\n"),
935 GNUNET_a2s (serv_addr, addrlen), ret);
942 * Check if socket is valid (no fatal errors have happened so far).
943 * Note that a socket that is still trying to connect is considered
946 * @param sock socket to check
947 * @return GNUNET_YES if valid, GNUNET_NO otherwise
950 GNUNET_CONNECTION_check (struct GNUNET_CONNECTION_Handle *sock)
952 if ((sock->ap_head != NULL) || (sock->dns_active != NULL))
953 return GNUNET_YES; /* still trying to connect */
954 return (sock->sock == NULL) ? GNUNET_NO : GNUNET_YES;
959 * Close the socket and free associated resources. Pending
960 * transmissions may be completed or dropped depending on the
961 * arguments. If a receive call is pending and should
962 * NOT be completed, 'GNUNET_CONNECTION_receive_cancel'
963 * should be called explicitly first.
965 * @param sock socket to destroy
966 * @param finish_pending_write should pending writes be completed or aborted?
967 * (this applies to transmissions where the data has already been
968 * read from the application; all other transmissions should be
969 * aborted using 'GNUNET_CONNECTION_notify_transmit_ready_cancel').
972 GNUNET_CONNECTION_destroy (struct GNUNET_CONNECTION_Handle *sock,
973 int finish_pending_write)
975 if (GNUNET_NO == finish_pending_write)
977 if (sock->write_task != GNUNET_SCHEDULER_NO_TASK)
979 GNUNET_SCHEDULER_cancel (sock->sched,
981 sock->write_task = GNUNET_SCHEDULER_NO_TASK;
982 sock->write_buffer_off = 0;
985 if ((sock->write_buffer_off == 0) && (sock->dns_active != NULL))
987 GNUNET_RESOLVER_request_cancel (sock->dns_active);
988 sock->dns_active = NULL;
990 GNUNET_assert (sock->sched != NULL);
991 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task);
993 = GNUNET_SCHEDULER_add_now (sock->sched,
994 &destroy_continuation, sock);
999 * Tell the receiver callback that a timeout was reached.
1002 signal_timeout (struct GNUNET_CONNECTION_Handle *sh)
1004 GNUNET_CONNECTION_Receiver receiver;
1006 #if DEBUG_CONNECTION
1007 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1008 "Network signals time out to receiver (%p)!\n", sh);
1010 GNUNET_assert (NULL != (receiver = sh->receiver));
1011 sh->receiver = NULL;
1012 receiver (sh->receiver_cls, NULL, 0, NULL, 0, 0);
1017 * Tell the receiver callback that we had an IO error.
1020 signal_error (struct GNUNET_CONNECTION_Handle *sh, int errcode)
1022 GNUNET_CONNECTION_Receiver receiver;
1023 GNUNET_assert (NULL != (receiver = sh->receiver));
1024 sh->receiver = NULL;
1025 receiver (sh->receiver_cls, NULL, 0, sh->addr, sh->addrlen, errcode);
1030 * This function is called once we either timeout
1031 * or have data ready to read.
1034 receive_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1036 struct GNUNET_CONNECTION_Handle *sh = cls;
1037 struct GNUNET_TIME_Absolute now;
1038 char buffer[sh->max];
1040 GNUNET_CONNECTION_Receiver receiver;
1042 sh->read_task = GNUNET_SCHEDULER_NO_TASK;
1043 if ( (GNUNET_YES == sh->ignore_shutdown) &&
1044 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
1046 /* ignore shutdown request, go again immediately */
1047 #if DEBUG_CONNECTION
1048 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1049 "Ignoring shutdown signal per configuration\n");
1051 sh->read_task = GNUNET_SCHEDULER_add_read_net (tc->sched,
1052 GNUNET_TIME_absolute_get_remaining
1053 (sh->receive_timeout),
1055 &receive_ready, sh);
1058 now = GNUNET_TIME_absolute_get ();
1059 if ((now.value > sh->receive_timeout.value) ||
1060 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT)) ||
1061 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
1063 #if DEBUG_CONNECTION
1064 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1065 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1066 "Receive from `%s' encounters error: time out by %llums... (%p)\n",
1067 GNUNET_a2s (sh->addr, sh->addrlen),
1068 GNUNET_TIME_absolute_get_duration (sh->receive_timeout).
1071 signal_timeout (sh);
1074 if (sh->sock == NULL)
1076 /* connect failed for good */
1077 #if DEBUG_CONNECTION
1078 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1079 "Receive encounters error, socket closed... (%p)\n", sh);
1081 signal_error (sh, ECONNREFUSED);
1084 GNUNET_assert (GNUNET_NETWORK_fdset_isset (tc->read_ready, sh->sock));
1086 ret = GNUNET_NETWORK_socket_recv (sh->sock, buffer, sh->max);
1091 #if DEBUG_CONNECTION
1092 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1093 "Error receiving: %s\n", STRERROR (errno));
1095 signal_error (sh, errno);
1098 #if DEBUG_CONNECTION
1099 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1100 "receive_ready read %u/%u bytes from `%s' (%p)!\n",
1102 sh->max, GNUNET_a2s (sh->addr, sh->addrlen), sh);
1104 GNUNET_assert (NULL != (receiver = sh->receiver));
1105 sh->receiver = NULL;
1106 receiver (sh->receiver_cls, buffer, ret, sh->addr, sh->addrlen, 0);
1111 * This function is called after establishing a connection either has
1112 * succeeded or timed out. Note that it is possible that the attempt
1113 * timed out and that we're immediately retrying. If we are retrying,
1114 * we need to wait again (or timeout); if we succeeded, we need to
1115 * wait for data (or timeout).
1117 * @param cls our connection handle
1118 * @param tc task context describing why we are here
1121 receive_again (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1123 struct GNUNET_CONNECTION_Handle *sh = cls;
1124 struct GNUNET_TIME_Absolute now;
1126 sh->read_task = GNUNET_SCHEDULER_NO_TASK;
1127 if (sh->sock == NULL)
1129 /* not connected and no longer trying */
1130 #if DEBUG_CONNECTION
1131 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1132 "Receive encounters error, socket closed (%p)...\n", sh);
1134 signal_error (sh, ECONNREFUSED);
1137 now = GNUNET_TIME_absolute_get ();
1138 if ((now.value > sh->receive_timeout.value) ||
1139 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
1141 #if DEBUG_CONNECTION
1142 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1143 "Receive encounters error: time out (%p)...\n", sh);
1145 signal_timeout (sh);
1148 GNUNET_assert (sh->sock != NULL);
1149 /* connect succeeded, wait for data! */
1150 sh->read_task = GNUNET_SCHEDULER_add_read_net (tc->sched,
1151 GNUNET_TIME_absolute_get_remaining
1152 (sh->receive_timeout),
1154 &receive_ready, sh);
1159 * Receive data from the given socket. Note that this function will
1160 * call "receiver" asynchronously using the scheduler. It will
1161 * "immediately" return. Note that there MUST only be one active
1162 * receive call per socket at any given point in time (so do not
1163 * call receive again until the receiver callback has been invoked).
1165 * @param sock socket handle
1166 * @param max maximum number of bytes to read
1167 * @param timeout maximum amount of time to wait (use -1 for "forever")
1168 * @param receiver function to call with received data
1169 * @param receiver_cls closure for receiver
1172 GNUNET_CONNECTION_receive (struct GNUNET_CONNECTION_Handle *sock,
1174 struct GNUNET_TIME_Relative timeout,
1175 GNUNET_CONNECTION_Receiver receiver,
1178 struct GNUNET_SCHEDULER_TaskContext tc;
1180 GNUNET_assert ((sock->read_task == GNUNET_SCHEDULER_NO_TASK) &&
1181 (0 == (sock->ccs & COCO_RECEIVE_AGAIN)) &&
1182 (sock->receiver == NULL));
1183 sock->receiver = receiver;
1184 sock->receiver_cls = receiver_cls;
1185 sock->receive_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1187 if (sock->sock != NULL)
1189 memset (&tc, 0, sizeof (tc));
1190 tc.sched = sock->sched;
1191 tc.reason = GNUNET_SCHEDULER_REASON_PREREQ_DONE;
1192 receive_again (sock, &tc);
1195 if ((sock->dns_active == NULL) && (sock->ap_head == NULL))
1197 receiver (receiver_cls, NULL, 0, NULL, 0, ETIMEDOUT);
1200 sock->ccs += COCO_RECEIVE_AGAIN;
1205 * Configure this connection to ignore shutdown signals.
1207 * @param sock socket handle
1208 * @param do_ignore GNUNET_YES to ignore, GNUNET_NO to restore default
1211 GNUNET_CONNECTION_ignore_shutdown (struct GNUNET_CONNECTION_Handle *sock,
1214 sock->ignore_shutdown = do_ignore;
1219 * Cancel receive job on the given socket. Note that the
1220 * receiver callback must not have been called yet in order
1221 * for the cancellation to be valid.
1223 * @param sock socket handle
1224 * @return closure of the original receiver callback closure
1227 GNUNET_CONNECTION_receive_cancel (struct GNUNET_CONNECTION_Handle *sock)
1229 if (sock->read_task != GNUNET_SCHEDULER_NO_TASK)
1231 GNUNET_assert (sock == GNUNET_SCHEDULER_cancel (sock->sched,
1233 sock->read_task = GNUNET_SCHEDULER_NO_TASK;
1237 GNUNET_assert (0 != (sock->ccs & COCO_RECEIVE_AGAIN));
1238 sock->ccs -= COCO_RECEIVE_AGAIN;
1240 sock->receiver = NULL;
1241 return sock->receiver_cls;
1246 * Try to call the transmit notify method (check if we do
1247 * have enough space available first)!
1249 * @param sock socket for which we should do this processing
1250 * @return GNUNET_YES if we were able to call notify
1253 process_notify (struct GNUNET_CONNECTION_Handle *sock)
1258 GNUNET_CONNECTION_TransmitReadyNotify notify;
1260 GNUNET_assert (sock->write_task == GNUNET_SCHEDULER_NO_TASK);
1261 if (NULL == (notify = sock->nth.notify_ready))
1263 used = sock->write_buffer_off - sock->write_buffer_pos;
1264 avail = sock->write_buffer_size - used;
1265 size = sock->nth.notify_size;
1266 if (sock->nth.notify_size > avail)
1268 sock->nth.notify_ready = NULL;
1269 if (sock->write_buffer_size - sock->write_buffer_off < size)
1271 /* need to compact */
1272 memmove (sock->write_buffer,
1273 &sock->write_buffer[sock->write_buffer_pos], used);
1274 sock->write_buffer_off -= sock->write_buffer_pos;
1275 sock->write_buffer_pos = 0;
1277 GNUNET_assert (sock->write_buffer_size - sock->write_buffer_off >= size);
1278 size = notify (sock->nth.notify_ready_cls,
1279 sock->write_buffer_size - sock->write_buffer_off,
1280 &sock->write_buffer[sock->write_buffer_off]);
1281 sock->write_buffer_off += size;
1287 * Task invoked by the scheduler when a call to transmit
1288 * is timing out (we never got enough buffer space to call
1289 * the callback function before the specified timeout
1292 * This task notifies the client about the timeout.
1295 transmit_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1297 struct GNUNET_CONNECTION_Handle *sock = cls;
1298 GNUNET_CONNECTION_TransmitReadyNotify notify;
1300 #if DEBUG_CONNECTION
1301 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1302 "transmit_timeout running (%p)\n", sock);
1304 sock->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK;
1305 #if DEBUG_CONNECTION
1306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1307 "Transmit to `%s:%u/%s' fails, time out reached (%p).\n",
1309 sock->port, GNUNET_a2s (sock->addr, sock->addrlen), sock);
1311 GNUNET_assert (0 != (sock->ccs & COCO_TRANSMIT_READY));
1312 sock->ccs -= COCO_TRANSMIT_READY; /* remove request */
1313 notify = sock->nth.notify_ready;
1314 sock->nth.notify_ready = NULL;
1315 notify (sock->nth.notify_ready_cls, 0, NULL);
1320 * Task invoked by the scheduler when we failed to connect
1321 * at the time of being asked to transmit.
1323 * This task notifies the client about the error.
1326 connect_error (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1328 struct GNUNET_CONNECTION_Handle *sock = cls;
1329 GNUNET_CONNECTION_TransmitReadyNotify notify;
1331 #if DEBUG_CONNECTION
1332 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1333 "Transmission request of size %u fails, connection failed (%p).\n",
1334 sock->nth.notify_size, sock);
1336 sock->write_task = GNUNET_SCHEDULER_NO_TASK;
1337 notify = sock->nth.notify_ready;
1338 sock->nth.notify_ready = NULL;
1339 notify (sock->nth.notify_ready_cls, 0, NULL);
1344 transmit_error (struct GNUNET_CONNECTION_Handle *sock)
1346 GNUNET_CONNECTION_TransmitReadyNotify notify;
1348 if (NULL != sock->sock)
1350 GNUNET_NETWORK_socket_shutdown (sock->sock, SHUT_RDWR);
1351 GNUNET_break (GNUNET_OK ==
1352 GNUNET_NETWORK_socket_close (sock->sock));
1355 if (sock->read_task != GNUNET_SCHEDULER_NO_TASK)
1357 GNUNET_SCHEDULER_cancel (sock->sched,
1359 sock->read_task = GNUNET_SCHEDULER_NO_TASK;
1360 signal_timeout (sock);
1363 if (sock->nth.notify_ready == NULL)
1364 return; /* nobody to tell about it */
1365 notify = sock->nth.notify_ready;
1366 sock->nth.notify_ready = NULL;
1367 notify (sock->nth.notify_ready_cls, 0, NULL);
1372 * See if we are now connected. If not, wait longer for
1373 * connect to succeed. If connected, we should be able
1374 * to write now as well, unless we timed out.
1376 * @param cls our connection handle
1377 * @param tc task context describing why we are here
1380 transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1382 struct GNUNET_CONNECTION_Handle *sock = cls;
1383 GNUNET_CONNECTION_TransmitReadyNotify notify;
1387 #if DEBUG_CONNECTION
1388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1389 "transmit_ready running (%p).\n", sock);
1391 GNUNET_assert (sock->write_task != GNUNET_SCHEDULER_NO_TASK);
1392 sock->write_task = GNUNET_SCHEDULER_NO_TASK;
1393 GNUNET_assert (sock->nth.timeout_task == GNUNET_SCHEDULER_NO_TASK);
1394 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1396 if (sock->ignore_shutdown == GNUNET_YES)
1397 goto SCHEDULE_WRITE; /* ignore shutdown, go again immediately */
1398 #if DEBUG_CONNECTION
1399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1400 "Transmit to `%s' fails, shutdown happened (%p).\n",
1401 GNUNET_a2s (sock->addr, sock->addrlen), sock);
1403 notify = sock->nth.notify_ready;
1404 sock->nth.notify_ready = NULL;
1405 notify (sock->nth.notify_ready_cls, 0, NULL);
1408 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT))
1410 #if DEBUG_CONNECTION
1411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1412 "Transmit to `%s' fails, time out reached (%p).\n",
1413 GNUNET_a2s (sock->addr, sock->addrlen), sock);
1415 notify = sock->nth.notify_ready;
1416 sock->nth.notify_ready = NULL;
1417 notify (sock->nth.notify_ready_cls, 0, NULL);
1420 GNUNET_assert (NULL != sock->sock);
1421 if (tc->write_ready == NULL)
1423 /* special circumstances (in particular,
1424 PREREQ_DONE after connect): not yet ready to write,
1425 but no "fatal" error either. Hence retry. */
1426 goto SCHEDULE_WRITE;
1428 if (!GNUNET_NETWORK_fdset_isset (tc->write_ready, sock->sock))
1430 #if DEBUG_CONNECTION
1431 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1433 ("Could not satisfy pending transmission request, socket closed or connect failed (%p).\n"),
1436 transmit_error (sock);
1437 return; /* connect failed for good, we're finished */
1439 GNUNET_assert (sock->write_buffer_off >= sock->write_buffer_pos);
1440 process_notify (sock);
1441 have = sock->write_buffer_off - sock->write_buffer_pos;
1444 /* no data ready for writing, terminate write loop */
1447 GNUNET_assert (have <= sock->write_buffer_size);
1448 GNUNET_assert (have + sock->write_buffer_pos <= sock->write_buffer_size);
1449 GNUNET_assert (sock->write_buffer_pos <= sock->write_buffer_size);
1451 ret = GNUNET_NETWORK_socket_send (sock->sock,
1452 &sock->write_buffer[sock->
1461 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1462 _("Failed to send to `%s': %s\n"),
1463 GNUNET_a2s (sock->addr,
1467 #if DEBUG_CONNECTION
1468 GNUNET_log_strerror (GNUNET_ERROR_TYPE_DEBUG, "send");
1470 transmit_error (sock);
1473 #if DEBUG_CONNECTION
1474 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1475 "transmit_ready transmitted %u/%u bytes to `%s' (%p)\n",
1477 have, GNUNET_a2s (sock->addr, sock->addrlen), sock);
1479 sock->write_buffer_pos += ret;
1480 if (sock->write_buffer_pos == sock->write_buffer_off)
1482 /* transmitted all pending data */
1483 sock->write_buffer_pos = 0;
1484 sock->write_buffer_off = 0;
1486 if ((sock->write_buffer_off == 0) && (NULL == sock->nth.notify_ready))
1487 return; /* all data sent! */
1488 /* not done writing, schedule more */
1490 #if DEBUG_CONNECTION
1491 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1492 "Re-scheduling transmit_ready (more to do) (%p).\n", sock);
1494 if (sock->write_task == GNUNET_SCHEDULER_NO_TASK)
1496 GNUNET_SCHEDULER_add_write_net (tc->sched,
1497 GNUNET_TIME_absolute_get_remaining
1498 (sock->nth.transmit_timeout),
1499 sock->sock, &transmit_ready, sock);
1504 * Ask the socket to call us once the specified number of bytes
1505 * are free in the transmission buffer. May call the notify
1506 * method immediately if enough space is available.
1508 * @param sock socket
1509 * @param size number of bytes to send
1510 * @param timeout after how long should we give up (and call
1511 * notify with buf NULL and size 0)?
1512 * @param notify function to call
1513 * @param notify_cls closure for notify
1514 * @return non-NULL if the notify callback was queued,
1515 * NULL if we are already going to notify someone else (busy)
1517 struct GNUNET_CONNECTION_TransmitHandle *
1518 GNUNET_CONNECTION_notify_transmit_ready (struct GNUNET_CONNECTION_Handle
1520 struct GNUNET_TIME_Relative timeout,
1521 GNUNET_CONNECTION_TransmitReadyNotify
1522 notify, void *notify_cls)
1524 if (sock->nth.notify_ready != NULL)
1526 GNUNET_assert (notify != NULL);
1527 GNUNET_assert (sock->write_buffer_size >= size);
1528 GNUNET_assert (sock->write_buffer_off <= sock->write_buffer_size);
1529 GNUNET_assert (sock->write_buffer_pos <= sock->write_buffer_size);
1530 GNUNET_assert (sock->write_buffer_pos <= sock->write_buffer_off);
1531 sock->nth.notify_ready = notify;
1532 sock->nth.notify_ready_cls = notify_cls;
1533 sock->nth.sh = sock;
1534 sock->nth.notify_size = size;
1535 sock->nth.transmit_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1536 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->nth.timeout_task);
1537 if ((sock->sock == NULL) &&
1538 (sock->ap_head == NULL) && (sock->dns_active == NULL))
1540 sock->write_task = GNUNET_SCHEDULER_add_now (sock->sched,
1541 &connect_error, sock);
1544 if (GNUNET_SCHEDULER_NO_TASK != sock->write_task)
1546 if (sock->sock != NULL)
1548 #if DEBUG_CONNECTION
1549 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1550 "Scheduling transmit_ready (%p).\n", sock);
1552 sock->write_task = GNUNET_SCHEDULER_add_write_net (sock->sched,
1553 GNUNET_TIME_absolute_get_remaining
1562 #if DEBUG_CONNECTION
1563 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1564 "CCS-Scheduling transmit_ready, adding timeout task (%p).\n",
1567 sock->ccs |= COCO_TRANSMIT_READY;
1568 sock->nth.timeout_task = GNUNET_SCHEDULER_add_delayed (sock->sched,
1578 * Cancel the specified transmission-ready
1582 GNUNET_CONNECTION_notify_transmit_ready_cancel (struct
1583 GNUNET_CONNECTION_TransmitHandle
1586 GNUNET_assert (h->notify_ready != NULL);
1587 if (0 != (h->sh->ccs & COCO_TRANSMIT_READY))
1589 #if DEBUG_CONNECTION
1590 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1591 "notify_transmit_ready_cancel cancels timeout_task (%p)\n",
1594 GNUNET_SCHEDULER_cancel (h->sh->sched, h->timeout_task);
1595 h->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1596 h->sh->ccs -= COCO_TRANSMIT_READY;
1600 if (h->sh->write_task != GNUNET_SCHEDULER_NO_TASK)
1602 GNUNET_SCHEDULER_cancel (h->sh->sched, h->sh->write_task);
1603 h->sh->write_task = GNUNET_SCHEDULER_NO_TASK;
1606 h->notify_ready = NULL;
1609 /* end of connection.c */