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 * Handle to a pending DNS lookup request.
250 struct GNUNET_RESOLVER_RequestHandle *dns_active;
253 * The handle we return for GNUNET_CONNECTION_notify_transmit_ready.
255 struct GNUNET_CONNECTION_TransmitHandle nth;
258 * Timeout for receiving (in absolute time).
260 struct GNUNET_TIME_Absolute receive_timeout;
263 * Functions to call after connect failed or succeeded.
265 enum ConnectContinuations ccs;
268 * Maximum number of bytes to read (for receiving).
273 * Ignore GNUNET_SCHEDULER_REASON_SHUTDOWN for this socket.
278 * Port to connect to.
283 * When shutdown, do not ever actually close the socket, but
284 * free resources. Only should ever be set if using program
285 * termination as a signal (because only then will the leaked
293 * Set the persist option on this connection handle. Indicates
294 * that the underlying socket or fd should never really be closed.
295 * Used for indicating process death.
297 * @param sock the connection to set persistent
299 void GNUNET_CONNECTION_persist_(struct GNUNET_CONNECTION_Handle *sock)
301 sock->persist = GNUNET_YES;
305 * Create a socket handle by boxing an existing OS socket. The OS
306 * socket should henceforth be no longer used directly.
307 * GNUNET_socket_destroy will close it.
309 * @param sched scheduler to use
310 * @param osSocket existing socket to box
311 * @param maxbuf maximum write buffer size for the socket (use
312 * 0 for sockets that need no write buffers, such as listen sockets)
313 * @return the boxed socket handle
315 struct GNUNET_CONNECTION_Handle *
316 GNUNET_CONNECTION_create_from_existing (struct GNUNET_SCHEDULER_Handle
318 struct GNUNET_NETWORK_Handle
319 *osSocket, size_t maxbuf)
321 struct GNUNET_CONNECTION_Handle *ret;
322 ret = GNUNET_malloc (sizeof (struct GNUNET_CONNECTION_Handle) + maxbuf);
323 ret->write_buffer = (char *) &ret[1];
324 ret->write_buffer_size = maxbuf;
325 ret->sock = osSocket;
332 * Create a socket handle by accepting on a listen socket. This
333 * function may block if the listen socket has no connection ready.
335 * @param sched scheduler to use
336 * @param access function to use to check if access is allowed
337 * @param access_cls closure for access
338 * @param lsock listen socket
339 * @param maxbuf maximum write buffer size for the socket (use
340 * 0 for sockets that need no write buffers, such as listen sockets)
341 * @return the socket handle, NULL on error
343 struct GNUNET_CONNECTION_Handle *
344 GNUNET_CONNECTION_create_from_accept (struct GNUNET_SCHEDULER_Handle
346 GNUNET_CONNECTION_AccessCheck access,
348 struct GNUNET_NETWORK_Handle *lsock,
351 struct GNUNET_CONNECTION_Handle *ret;
354 struct GNUNET_NETWORK_Handle *sock;
356 struct sockaddr_in *v4;
357 struct sockaddr_in6 *v6;
361 addrlen = sizeof (addr);
363 GNUNET_NETWORK_socket_accept (lsock, (struct sockaddr *) &addr, &addrlen);
366 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "accept");
369 if (addrlen > sizeof (addr))
372 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
376 sa = (struct sockaddr *) addr;
377 v6 = (struct sockaddr_in6 *) addr;
378 if ((sa->sa_family == AF_INET6) && (IN6_IS_ADDR_V4MAPPED (&v6->sin6_addr)))
380 /* convert to V4 address */
381 v4 = GNUNET_malloc (sizeof (struct sockaddr_in));
382 memset (v4, 0, sizeof (struct sockaddr_in));
383 v4->sin_family = AF_INET;
384 memcpy (&v4->sin_addr,
385 &((char *) &v6->sin6_addr)[sizeof (struct in6_addr) -
386 sizeof (struct in_addr)],
387 sizeof (struct in_addr));
388 v4->sin_port = v6->sin6_port;
390 addrlen = sizeof (struct sockaddr_in);
394 uaddr = GNUNET_malloc (addrlen);
395 memcpy (uaddr, addr, addrlen);
398 if ((access != NULL) &&
399 (GNUNET_YES != (aret = access (access_cls, uaddr, addrlen))))
401 if (aret == GNUNET_NO)
402 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
403 _("Access denied to `%s'\n"),
404 GNUNET_a2s (uaddr, addrlen));
405 GNUNET_break (GNUNET_OK ==
406 GNUNET_NETWORK_socket_shutdown (sock, SHUT_RDWR));
407 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
411 ret = GNUNET_malloc (sizeof (struct GNUNET_CONNECTION_Handle) + maxbuf);
412 ret->write_buffer = (char *) &ret[1];
413 ret->write_buffer_size = maxbuf;
415 ret->addrlen = addrlen;
419 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
420 _("Accepting connection from `%s': %p\n"),
421 GNUNET_a2s (uaddr, addrlen), ret);
427 * Obtain the network address of the other party.
429 * @param sock the client to get the address for
430 * @param addr where to store the address
431 * @param addrlen where to store the length of the address
432 * @return GNUNET_OK on success
435 GNUNET_CONNECTION_get_address (struct GNUNET_CONNECTION_Handle *sock,
436 void **addr, size_t * addrlen)
438 if ((sock->addr == NULL) || (sock->addrlen == 0))
440 *addr = GNUNET_malloc (sock->addrlen);
441 memcpy (*addr, sock->addr, sock->addrlen);
442 *addrlen = sock->addrlen;
448 * This function is called after establishing a connection either has
449 * succeeded or timed out. Note that it is possible that the attempt
450 * timed out and that we're immediately retrying. If we are retrying,
451 * we need to wait again (or timeout); if we succeeded, we need to
452 * wait for data (or timeout).
454 * @param cls our connection handle
455 * @param tc task context describing why we are here
458 receive_again (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
462 * Scheduler let us know that the connect task is finished (or was
463 * cancelled due to shutdown). Now really clean up.
465 * @param cls our "struct GNUNET_CONNECTION_Handle *"
469 destroy_continuation (void *cls,
470 const struct GNUNET_SCHEDULER_TaskContext *tc)
472 struct GNUNET_CONNECTION_Handle *sock = cls;
473 GNUNET_CONNECTION_TransmitReadyNotify notify;
474 struct AddressProbe *pos;
476 GNUNET_assert (sock->dns_active == NULL);
477 if (0 != (sock->ccs & COCO_TRANSMIT_READY))
480 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
481 "Destroy waits for CCS-TR to be done (%p)\n", sock);
483 sock->ccs |= COCO_DESTROY_CONTINUATION;
486 if (sock->write_task != GNUNET_SCHEDULER_NO_TASK)
489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
490 "Destroy waits for write_task to be done (%p)\n", sock);
492 GNUNET_SCHEDULER_add_after (sock->sched,
494 &destroy_continuation, sock);
497 if (0 != (sock->ccs & COCO_RECEIVE_AGAIN))
499 sock->ccs |= COCO_DESTROY_CONTINUATION;
502 if (sock->sock != NULL)
505 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
506 "Shutting down socket (%p)\n", sock);
508 if (sock->persist != GNUNET_YES)
509 GNUNET_NETWORK_socket_shutdown (sock->sock, SHUT_RDWR);
511 if (sock->read_task != GNUNET_SCHEDULER_NO_TASK)
513 GNUNET_SCHEDULER_add_after (sock->sched,
515 &destroy_continuation, sock);
519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
520 "Destroy actually runs (%p)!\n", sock);
522 if (sock->dns_active != NULL)
524 GNUNET_RESOLVER_request_cancel (sock->dns_active);
525 sock->dns_active = NULL;
527 while (NULL != (pos = sock->ap_head))
529 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (pos->sock));
530 GNUNET_SCHEDULER_cancel (sock->sched, pos->task);
531 GNUNET_CONTAINER_DLL_remove (sock->ap_head, sock->ap_tail, pos);
534 GNUNET_assert (sock->nth.timeout_task == GNUNET_SCHEDULER_NO_TASK);
535 GNUNET_assert (sock->ccs == COCO_NONE);
536 if (NULL != (notify = sock->nth.notify_ready))
538 sock->nth.notify_ready = NULL;
539 notify (sock->nth.notify_ready_cls, 0, NULL);
542 if ((sock->sock != NULL) && (sock->persist != GNUNET_YES))
543 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock->sock));
545 GNUNET_free_non_null (sock->addr);
546 GNUNET_free_non_null (sock->hostname);
548 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
549 "Freeing memory of connection %p.\n", sock);
557 * See if we are now connected. If not, wait longer for
558 * connect to succeed. If connected, we should be able
559 * to write now as well, unless we timed out.
561 * @param cls our connection handle
562 * @param tc task context describing why we are here
565 transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
569 * We've failed for good to establish a connection.
571 * @param h the connection we tried to establish
574 connect_fail_continuation (struct GNUNET_CONNECTION_Handle *h)
577 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
578 "Failed to establish TCP connection to `%s:%u', no further addresses to try (%p).\n",
579 h->hostname, h->port, h);
581 /* connect failed / timed out */
582 GNUNET_break (h->ap_head == NULL);
583 GNUNET_break (h->ap_tail == NULL);
584 GNUNET_break (h->dns_active == GNUNET_NO);
585 GNUNET_break (h->sock == NULL);
587 /* trigger jobs that used to wait on "connect_task" */
588 if (0 != (h->ccs & COCO_RECEIVE_AGAIN))
591 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
592 "connect_timeout_continuation triggers receive_again (%p)\n",
595 h->ccs -= COCO_RECEIVE_AGAIN;
596 h->read_task = GNUNET_SCHEDULER_add_after (h->sched,
597 GNUNET_SCHEDULER_NO_TASK,
600 if (0 != (h->ccs & COCO_TRANSMIT_READY))
603 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
604 "connect_timeout_continuation cancels timeout_task, triggers transmit_ready (%p)\n",
607 GNUNET_assert (h->nth.timeout_task != GNUNET_SCHEDULER_NO_TASK);
608 GNUNET_SCHEDULER_cancel (h->sched, h->nth.timeout_task);
609 h->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK;
610 h->ccs -= COCO_TRANSMIT_READY;
611 h->write_task = GNUNET_SCHEDULER_add_after (h->sched,
612 GNUNET_SCHEDULER_NO_TASK,
615 if (0 != (h->ccs & COCO_DESTROY_CONTINUATION))
618 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
619 "connect_timeout_continuation runs destroy_continuation (%p)\n",
622 h->ccs -= COCO_DESTROY_CONTINUATION;
623 GNUNET_SCHEDULER_add_continuation (h->sched,
624 &destroy_continuation,
625 h, GNUNET_SCHEDULER_REASON_TIMEOUT);
631 * We've succeeded in establishing a connection.
633 * @param h the connection we tried to establish
636 connect_success_continuation (struct GNUNET_CONNECTION_Handle *h)
639 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
640 "Connection to `%s' succeeded! (%p)\n",
641 GNUNET_a2s (h->addr, h->addrlen), h);
643 /* trigger jobs that waited for the connection */
644 if (0 != (h->ccs & COCO_RECEIVE_AGAIN))
647 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
648 "connect_success_continuation runs receive_again (%p)\n",
651 h->ccs -= COCO_RECEIVE_AGAIN;
652 h->read_task = GNUNET_SCHEDULER_add_after (h->sched,
653 GNUNET_SCHEDULER_NO_TASK,
656 if (0 != (h->ccs & COCO_TRANSMIT_READY))
659 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
660 "connect_success_continuation runs transmit_ready, cancels timeout_task (%p)\n",
663 GNUNET_assert (h->nth.timeout_task != GNUNET_SCHEDULER_NO_TASK);
664 GNUNET_SCHEDULER_cancel (h->sched, h->nth.timeout_task);
665 h->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK;
666 h->ccs -= COCO_TRANSMIT_READY;
668 GNUNET_SCHEDULER_add_write_net (h->sched,
669 GNUNET_TIME_absolute_get_remaining
670 (h->nth.transmit_timeout), h->sock,
673 if (0 != (h->ccs & COCO_DESTROY_CONTINUATION))
676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
677 "connect_success_continuation runs destroy_continuation (%p)\n",
680 h->ccs -= COCO_DESTROY_CONTINUATION;
681 GNUNET_SCHEDULER_add_continuation (h->sched,
682 &destroy_continuation,
684 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
690 * Scheduler let us know that we're either ready to write on the
691 * socket OR connect timed out. Do the right thing.
693 * @param cls the "struct AddressProbe*" with the address that we are probing
694 * @param tc success or failure info about the connect attempt.
697 connect_probe_continuation (void *cls,
698 const struct GNUNET_SCHEDULER_TaskContext *tc)
700 struct AddressProbe *ap = cls;
701 struct GNUNET_CONNECTION_Handle *h = ap->h;
702 struct AddressProbe *pos;
706 GNUNET_CONTAINER_DLL_remove (h->ap_head, h->ap_tail, ap);
707 len = sizeof (error);
710 if ((0 == (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) ||
712 GNUNET_NETWORK_socket_getsockopt (ap->sock, SOL_SOCKET, SO_ERROR,
713 &error, &len)) || (error != 0)
716 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ap->sock));
718 if ((NULL == h->ap_head) && (h->dns_active == GNUNET_NO))
719 connect_fail_continuation (h);
722 GNUNET_assert (h->sock == NULL);
723 GNUNET_assert (ap->sock != NULL);
725 GNUNET_assert (h->addr == NULL);
726 h->addr = GNUNET_malloc (ap->addrlen);
727 memcpy (h->addr, ap->addr, ap->addrlen);
728 h->addrlen = ap->addrlen;
730 /* cancel all other attempts */
731 while (NULL != (pos = h->ap_head))
733 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (pos->sock));
734 GNUNET_SCHEDULER_cancel (h->sched, pos->task);
735 GNUNET_CONTAINER_DLL_remove (h->ap_head, h->ap_tail, pos);
738 connect_success_continuation (h);
743 * Try to establish a socket connection given the specified address.
744 * This function is called by the resolver once we have a DNS reply.
746 * @param cls our "struct GNUNET_CONNECTION_Handle *"
747 * @param addr address to try, NULL for "last call"
748 * @param addrlen length of addr
751 try_connect_using_address (void *cls,
752 const struct sockaddr *addr, socklen_t addrlen)
754 struct GNUNET_CONNECTION_Handle *h = cls;
755 struct AddressProbe *ap;
756 struct GNUNET_TIME_Relative delay;
760 h->dns_active = NULL;
761 if (NULL == h->ap_head)
762 connect_fail_continuation (h);
766 return; /* already connected */
767 GNUNET_assert (h->addr == NULL);
770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
771 "Trying to connect using address `%s:%u/%s:%u'\n",
772 h->hostname, h->port, GNUNET_a2s (addr, addrlen), h->port);
774 ap = GNUNET_malloc (sizeof (struct AddressProbe) + addrlen);
775 ap->addr = (const struct sockaddr *) &ap[1];
776 memcpy (&ap[1], addr, addrlen);
777 ap->addrlen = addrlen;
780 switch (ap->addr->sa_family)
783 ((struct sockaddr_in *) ap->addr)->sin_port = htons (h->port);
786 ((struct sockaddr_in6 *) ap->addr)->sin6_port = htons (h->port);
791 return; /* not supported by us */
794 GNUNET_NETWORK_socket_create (ap->addr->sa_family, SOCK_STREAM, 0);
795 if (ap->sock == NULL)
798 return; /* not supported by OS */
801 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
802 _("Trying to connect to `%s' (%p)\n"),
803 GNUNET_a2s (ap->addr, ap->addrlen), h);
805 if ((GNUNET_OK != GNUNET_NETWORK_socket_connect (ap->sock,
808 (errno != EINPROGRESS))
810 /* maybe refused / unsupported address, try next */
811 GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "connect");
812 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ap->sock));
816 GNUNET_CONTAINER_DLL_insert (h->ap_head, h->ap_tail, ap);
817 delay = GNUNET_CONNECTION_CONNECT_RETRY_TIMEOUT;
818 if (h->nth.notify_ready != NULL)
819 delay = GNUNET_TIME_relative_min (delay,
820 GNUNET_TIME_absolute_get_remaining
821 (h->nth.transmit_timeout));
822 if (h->receiver != NULL)
823 delay = GNUNET_TIME_relative_min (delay,
824 GNUNET_TIME_absolute_get_remaining
825 (h->receive_timeout));
827 GNUNET_SCHEDULER_add_write_net (h->sched, delay, ap->sock,
828 &connect_probe_continuation, ap);
833 * Create a socket handle by (asynchronously) connecting to a host.
834 * This function returns immediately, even if the connection has not
835 * yet been established. This function only creates TCP connections.
837 * @param sched scheduler to use
838 * @param cfg configuration to use
839 * @param hostname name of the host to connect to
840 * @param port port to connect to
841 * @param maxbuf maximum write buffer size for the socket (use
842 * 0 for sockets that need no write buffers, such as listen sockets)
843 * @return the socket handle
845 struct GNUNET_CONNECTION_Handle *
846 GNUNET_CONNECTION_create_from_connect (struct GNUNET_SCHEDULER_Handle *sched,
848 GNUNET_CONFIGURATION_Handle *cfg,
849 const char *hostname, uint16_t port,
852 struct GNUNET_CONNECTION_Handle *ret;
854 GNUNET_assert (0 < strlen (hostname)); /* sanity check */
855 ret = GNUNET_malloc (sizeof (struct GNUNET_CONNECTION_Handle) + maxbuf);
858 ret->write_buffer = (char *) &ret[1];
859 ret->write_buffer_size = maxbuf;
861 ret->hostname = GNUNET_strdup (hostname);
862 ret->dns_active = GNUNET_RESOLVER_ip_get (sched,
866 GNUNET_CONNECTION_CONNECT_RETRY_TIMEOUT,
867 &try_connect_using_address, ret);
873 * Create a socket handle by (asynchronously) connecting to a host.
874 * This function returns immediately, even if the connection has not
875 * yet been established. This function only creates TCP connections.
877 * @param sched scheduler to use
878 * @param af_family address family to use
879 * @param serv_addr server address
880 * @param addrlen length of server address
881 * @param maxbuf maximum write buffer size for the socket (use
882 * 0 for sockets that need no write buffers, such as listen sockets)
883 * @return the socket handle
885 struct GNUNET_CONNECTION_Handle *
886 GNUNET_CONNECTION_create_from_sockaddr (struct GNUNET_SCHEDULER_Handle
887 *sched, int af_family,
888 const struct sockaddr *serv_addr,
889 socklen_t addrlen, size_t maxbuf)
891 struct GNUNET_NETWORK_Handle *s;
892 struct GNUNET_CONNECTION_Handle *ret;
894 s = GNUNET_NETWORK_socket_create (af_family, SOCK_STREAM, 0);
897 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING |
898 GNUNET_ERROR_TYPE_BULK, "socket");
901 if ((GNUNET_OK != GNUNET_NETWORK_socket_connect (s, serv_addr, addrlen))
902 && (errno != EINPROGRESS))
904 /* maybe refused / unsupported address, try next */
905 GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "connect");
906 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (s));
909 ret = GNUNET_CONNECTION_create_from_existing (sched, s, maxbuf);
910 ret->addr = GNUNET_malloc (addrlen);
911 memcpy (ret->addr, serv_addr, addrlen);
912 ret->addrlen = addrlen;
914 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
915 _("Trying to connect to `%s' (%p)\n"),
916 GNUNET_a2s (serv_addr, addrlen), ret);
923 * Check if socket is valid (no fatal errors have happened so far).
924 * Note that a socket that is still trying to connect is considered
927 * @param sock socket to check
928 * @return GNUNET_YES if valid, GNUNET_NO otherwise
931 GNUNET_CONNECTION_check (struct GNUNET_CONNECTION_Handle *sock)
933 if ((sock->ap_head != NULL) || (sock->dns_active != NULL))
934 return GNUNET_YES; /* still trying to connect */
935 return (sock->sock == NULL) ? GNUNET_NO : GNUNET_YES;
940 * Close the socket and free associated resources. Pending
941 * transmissions may be completed or dropped depending on the
942 * arguments. If a receive call is pending and should
943 * NOT be completed, 'GNUNET_CONNECTION_receive_cancel'
944 * should be called explicitly first.
946 * @param sock socket to destroy
947 * @param finish_pending_write should pending writes be completed or aborted?
948 * (this applies to transmissions where the data has already been
949 * read from the application; all other transmissions should be
950 * aborted using 'GNUNET_CONNECTION_notify_transmit_ready_cancel').
953 GNUNET_CONNECTION_destroy (struct GNUNET_CONNECTION_Handle *sock,
954 int finish_pending_write)
956 if (GNUNET_NO == finish_pending_write)
958 if (sock->write_task != GNUNET_SCHEDULER_NO_TASK)
960 GNUNET_SCHEDULER_cancel (sock->sched,
962 sock->write_task = GNUNET_SCHEDULER_NO_TASK;
963 sock->write_buffer_off = 0;
966 if ((sock->write_buffer_off == 0) && (sock->dns_active != NULL))
968 GNUNET_RESOLVER_request_cancel (sock->dns_active);
969 sock->dns_active = NULL;
971 GNUNET_assert (sock->sched != NULL);
972 GNUNET_SCHEDULER_add_now (sock->sched,
973 &destroy_continuation, sock);
978 * Tell the receiver callback that a timeout was reached.
981 signal_timeout (struct GNUNET_CONNECTION_Handle *sh)
983 GNUNET_CONNECTION_Receiver receiver;
986 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
987 "Network signals time out to receiver (%p)!\n", sh);
989 GNUNET_assert (NULL != (receiver = sh->receiver));
991 receiver (sh->receiver_cls, NULL, 0, NULL, 0, 0);
996 * Tell the receiver callback that we had an IO error.
999 signal_error (struct GNUNET_CONNECTION_Handle *sh, int errcode)
1001 GNUNET_CONNECTION_Receiver receiver;
1002 GNUNET_assert (NULL != (receiver = sh->receiver));
1003 sh->receiver = NULL;
1004 receiver (sh->receiver_cls, NULL, 0, sh->addr, sh->addrlen, errcode);
1009 * This function is called once we either timeout
1010 * or have data ready to read.
1013 receive_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1015 struct GNUNET_CONNECTION_Handle *sh = cls;
1016 struct GNUNET_TIME_Absolute now;
1017 char buffer[sh->max];
1019 GNUNET_CONNECTION_Receiver receiver;
1021 sh->read_task = GNUNET_SCHEDULER_NO_TASK;
1022 if ( (GNUNET_YES == sh->ignore_shutdown) &&
1023 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
1025 /* ignore shutdown request, go again immediately */
1026 #if DEBUG_CONNECTION
1027 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1028 "Ignoring shutdown signal per configuration\n");
1030 sh->read_task = GNUNET_SCHEDULER_add_read_net (tc->sched,
1031 GNUNET_TIME_absolute_get_remaining
1032 (sh->receive_timeout),
1034 &receive_ready, sh);
1037 now = GNUNET_TIME_absolute_get ();
1038 if ((now.value > sh->receive_timeout.value) ||
1039 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT)) ||
1040 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
1042 #if DEBUG_CONNECTION
1043 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1044 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1045 "Receive from `%s' encounters error: time out by %llums... (%p)\n",
1046 GNUNET_a2s (sh->addr, sh->addrlen),
1047 GNUNET_TIME_absolute_get_duration (sh->receive_timeout).
1050 signal_timeout (sh);
1053 if (sh->sock == NULL)
1055 /* connect failed for good */
1056 #if DEBUG_CONNECTION
1057 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1058 "Receive encounters error, socket closed... (%p)\n", sh);
1060 signal_error (sh, ECONNREFUSED);
1063 GNUNET_assert (GNUNET_NETWORK_fdset_isset (tc->read_ready, sh->sock));
1065 ret = GNUNET_NETWORK_socket_recv (sh->sock, buffer, sh->max);
1070 #if DEBUG_CONNECTION
1071 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1072 "Error receiving: %s\n", STRERROR (errno));
1074 signal_error (sh, errno);
1077 #if DEBUG_CONNECTION
1078 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1079 "receive_ready read %u/%u bytes from `%s' (%p)!\n",
1081 sh->max, GNUNET_a2s (sh->addr, sh->addrlen), sh);
1083 GNUNET_assert (NULL != (receiver = sh->receiver));
1084 sh->receiver = NULL;
1085 receiver (sh->receiver_cls, buffer, ret, sh->addr, sh->addrlen, 0);
1090 * This function is called after establishing a connection either has
1091 * succeeded or timed out. Note that it is possible that the attempt
1092 * timed out and that we're immediately retrying. If we are retrying,
1093 * we need to wait again (or timeout); if we succeeded, we need to
1094 * wait for data (or timeout).
1096 * @param cls our connection handle
1097 * @param tc task context describing why we are here
1100 receive_again (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1102 struct GNUNET_CONNECTION_Handle *sh = cls;
1103 struct GNUNET_TIME_Absolute now;
1105 sh->read_task = GNUNET_SCHEDULER_NO_TASK;
1106 if (sh->sock == NULL)
1108 /* not connected and no longer trying */
1109 #if DEBUG_CONNECTION
1110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1111 "Receive encounters error, socket closed (%p)...\n", sh);
1113 signal_error (sh, ECONNREFUSED);
1116 now = GNUNET_TIME_absolute_get ();
1117 if ((now.value > sh->receive_timeout.value) ||
1118 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
1120 #if DEBUG_CONNECTION
1121 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1122 "Receive encounters error: time out (%p)...\n", sh);
1124 signal_timeout (sh);
1127 GNUNET_assert (sh->sock != NULL);
1128 /* connect succeeded, wait for data! */
1129 sh->read_task = GNUNET_SCHEDULER_add_read_net (tc->sched,
1130 GNUNET_TIME_absolute_get_remaining
1131 (sh->receive_timeout),
1133 &receive_ready, sh);
1138 * Receive data from the given socket. Note that this function will
1139 * call "receiver" asynchronously using the scheduler. It will
1140 * "immediately" return. Note that there MUST only be one active
1141 * receive call per socket at any given point in time (so do not
1142 * call receive again until the receiver callback has been invoked).
1144 * @param sock socket handle
1145 * @param max maximum number of bytes to read
1146 * @param timeout maximum amount of time to wait (use -1 for "forever")
1147 * @param receiver function to call with received data
1148 * @param receiver_cls closure for receiver
1151 GNUNET_CONNECTION_receive (struct GNUNET_CONNECTION_Handle *sock,
1153 struct GNUNET_TIME_Relative timeout,
1154 GNUNET_CONNECTION_Receiver receiver,
1157 struct GNUNET_SCHEDULER_TaskContext tc;
1159 GNUNET_assert ((sock->read_task == GNUNET_SCHEDULER_NO_TASK) &&
1160 (0 == (sock->ccs & COCO_RECEIVE_AGAIN)) &&
1161 (sock->receiver == NULL));
1162 sock->receiver = receiver;
1163 sock->receiver_cls = receiver_cls;
1164 sock->receive_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1166 if (sock->sock != NULL)
1168 memset (&tc, 0, sizeof (tc));
1169 tc.sched = sock->sched;
1170 tc.reason = GNUNET_SCHEDULER_REASON_PREREQ_DONE;
1171 receive_again (sock, &tc);
1174 if ((sock->dns_active == NULL) && (sock->ap_head == NULL))
1176 receiver (receiver_cls, NULL, 0, NULL, 0, ETIMEDOUT);
1179 sock->ccs += COCO_RECEIVE_AGAIN;
1184 * Configure this connection to ignore shutdown signals.
1186 * @param sock socket handle
1187 * @param do_ignore GNUNET_YES to ignore, GNUNET_NO to restore default
1190 GNUNET_CONNECTION_ignore_shutdown (struct GNUNET_CONNECTION_Handle *sock,
1193 sock->ignore_shutdown = do_ignore;
1198 * Cancel receive job on the given socket. Note that the
1199 * receiver callback must not have been called yet in order
1200 * for the cancellation to be valid.
1202 * @param sock socket handle
1203 * @return closure of the original receiver callback closure
1206 GNUNET_CONNECTION_receive_cancel (struct GNUNET_CONNECTION_Handle *sock)
1208 if (sock->read_task != GNUNET_SCHEDULER_NO_TASK)
1210 GNUNET_assert (sock == GNUNET_SCHEDULER_cancel (sock->sched,
1212 sock->read_task = GNUNET_SCHEDULER_NO_TASK;
1216 GNUNET_assert (0 != (sock->ccs & COCO_RECEIVE_AGAIN));
1217 sock->ccs -= COCO_RECEIVE_AGAIN;
1219 sock->receiver = NULL;
1220 return sock->receiver_cls;
1225 * Try to call the transmit notify method (check if we do
1226 * have enough space available first)!
1228 * @param sock socket for which we should do this processing
1229 * @return GNUNET_YES if we were able to call notify
1232 process_notify (struct GNUNET_CONNECTION_Handle *sock)
1237 GNUNET_CONNECTION_TransmitReadyNotify notify;
1239 GNUNET_assert (sock->write_task == GNUNET_SCHEDULER_NO_TASK);
1240 if (NULL == (notify = sock->nth.notify_ready))
1242 used = sock->write_buffer_off - sock->write_buffer_pos;
1243 avail = sock->write_buffer_size - used;
1244 size = sock->nth.notify_size;
1245 if (sock->nth.notify_size > avail)
1247 sock->nth.notify_ready = NULL;
1248 if (sock->write_buffer_size - sock->write_buffer_off < size)
1250 /* need to compact */
1251 memmove (sock->write_buffer,
1252 &sock->write_buffer[sock->write_buffer_pos], used);
1253 sock->write_buffer_off -= sock->write_buffer_pos;
1254 sock->write_buffer_pos = 0;
1256 GNUNET_assert (sock->write_buffer_size - sock->write_buffer_off >= size);
1257 size = notify (sock->nth.notify_ready_cls,
1258 sock->write_buffer_size - sock->write_buffer_off,
1259 &sock->write_buffer[sock->write_buffer_off]);
1260 sock->write_buffer_off += size;
1266 * Task invoked by the scheduler when a call to transmit
1267 * is timing out (we never got enough buffer space to call
1268 * the callback function before the specified timeout
1271 * This task notifies the client about the timeout.
1274 transmit_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1276 struct GNUNET_CONNECTION_Handle *sock = cls;
1277 GNUNET_CONNECTION_TransmitReadyNotify notify;
1279 #if DEBUG_CONNECTION
1280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1281 "transmit_timeout running (%p)\n", sock);
1283 sock->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK;
1284 #if DEBUG_CONNECTION
1285 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1286 "Transmit to `%s:%u/%s' fails, time out reached (%p).\n",
1288 sock->port, GNUNET_a2s (sock->addr, sock->addrlen), sock);
1290 GNUNET_assert (0 != (sock->ccs & COCO_TRANSMIT_READY));
1291 sock->ccs -= COCO_TRANSMIT_READY; /* remove request */
1292 notify = sock->nth.notify_ready;
1293 sock->nth.notify_ready = NULL;
1294 notify (sock->nth.notify_ready_cls, 0, NULL);
1299 * Task invoked by the scheduler when we failed to connect
1300 * at the time of being asked to transmit.
1302 * This task notifies the client about the error.
1305 connect_error (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1307 struct GNUNET_CONNECTION_Handle *sock = cls;
1308 GNUNET_CONNECTION_TransmitReadyNotify notify;
1310 #if DEBUG_CONNECTION
1311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1312 "Transmission request of size %u fails, connection failed (%p).\n",
1313 sock->nth.notify_size, sock);
1315 sock->write_task = GNUNET_SCHEDULER_NO_TASK;
1316 notify = sock->nth.notify_ready;
1317 sock->nth.notify_ready = NULL;
1318 notify (sock->nth.notify_ready_cls, 0, NULL);
1323 transmit_error (struct GNUNET_CONNECTION_Handle *sock)
1325 GNUNET_CONNECTION_TransmitReadyNotify notify;
1327 if (sock->nth.notify_ready == NULL)
1328 return; /* nobody to tell about it */
1329 notify = sock->nth.notify_ready;
1330 sock->nth.notify_ready = NULL;
1331 notify (sock->nth.notify_ready_cls, 0, NULL);
1336 * See if we are now connected. If not, wait longer for
1337 * connect to succeed. If connected, we should be able
1338 * to write now as well, unless we timed out.
1340 * @param cls our connection handle
1341 * @param tc task context describing why we are here
1344 transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1346 struct GNUNET_CONNECTION_Handle *sock = cls;
1347 GNUNET_CONNECTION_TransmitReadyNotify notify;
1351 #if DEBUG_CONNECTION
1352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1353 "transmit_ready running (%p).\n", sock);
1355 GNUNET_assert (sock->write_task != GNUNET_SCHEDULER_NO_TASK);
1356 sock->write_task = GNUNET_SCHEDULER_NO_TASK;
1357 GNUNET_assert (sock->nth.timeout_task == GNUNET_SCHEDULER_NO_TASK);
1358 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1360 if (sock->ignore_shutdown == GNUNET_YES)
1361 goto SCHEDULE_WRITE; /* ignore shutdown, go again immediately */
1362 #if DEBUG_CONNECTION
1363 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1364 "Transmit to `%s' fails, shutdown happened (%p).\n",
1365 GNUNET_a2s (sock->addr, sock->addrlen), sock);
1367 notify = sock->nth.notify_ready;
1368 sock->nth.notify_ready = NULL;
1369 notify (sock->nth.notify_ready_cls, 0, NULL);
1372 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT))
1374 #if DEBUG_CONNECTION
1375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1376 "Transmit to `%s' fails, time out reached (%p).\n",
1377 GNUNET_a2s (sock->addr, sock->addrlen), sock);
1379 notify = sock->nth.notify_ready;
1380 sock->nth.notify_ready = NULL;
1381 notify (sock->nth.notify_ready_cls, 0, NULL);
1384 GNUNET_assert (NULL != sock->sock);
1385 if (tc->write_ready == NULL)
1387 /* special circumstances (in particular,
1388 PREREQ_DONE after connect): not yet ready to write,
1389 but no "fatal" error either. Hence retry. */
1390 goto SCHEDULE_WRITE;
1392 if (!GNUNET_NETWORK_fdset_isset (tc->write_ready, sock->sock))
1394 #if DEBUG_CONNECTION
1395 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1397 ("Could not satisfy pending transmission request, socket closed or connect failed (%p).\n"),
1400 if (NULL != sock->sock)
1402 GNUNET_NETWORK_socket_shutdown (sock->sock, SHUT_RDWR);
1403 GNUNET_break (GNUNET_OK ==
1404 GNUNET_NETWORK_socket_close (sock->sock));
1407 transmit_error (sock);
1408 return; /* connect failed for good, we're finished */
1410 GNUNET_assert (sock->write_buffer_off >= sock->write_buffer_pos);
1411 process_notify (sock);
1412 have = sock->write_buffer_off - sock->write_buffer_pos;
1415 /* no data ready for writing, terminate write loop */
1418 GNUNET_assert (have <= sock->write_buffer_size);
1419 GNUNET_assert (have + sock->write_buffer_pos <= sock->write_buffer_size);
1420 GNUNET_assert (sock->write_buffer_pos <= sock->write_buffer_size);
1422 ret = GNUNET_NETWORK_socket_send (sock->sock,
1423 &sock->write_buffer[sock->
1430 #if DEBUG_CONNECTION
1431 GNUNET_log_strerror (GNUNET_ERROR_TYPE_DEBUG, "send");
1433 GNUNET_NETWORK_socket_shutdown (sock->sock, SHUT_WR);
1434 transmit_error (sock);
1437 #if DEBUG_CONNECTION
1438 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1439 "transmit_ready transmitted %u/%u bytes to `%s' (%p)\n",
1441 have, GNUNET_a2s (sock->addr, sock->addrlen), sock);
1443 sock->write_buffer_pos += ret;
1444 if (sock->write_buffer_pos == sock->write_buffer_off)
1446 /* transmitted all pending data */
1447 sock->write_buffer_pos = 0;
1448 sock->write_buffer_off = 0;
1450 if ((sock->write_buffer_off == 0) && (NULL == sock->nth.notify_ready))
1451 return; /* all data sent! */
1452 /* not done writing, schedule more */
1454 #if DEBUG_CONNECTION
1455 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1456 "Re-scheduling transmit_ready (more to do) (%p).\n", sock);
1458 if (sock->write_task == GNUNET_SCHEDULER_NO_TASK)
1460 GNUNET_SCHEDULER_add_write_net (tc->sched,
1461 GNUNET_TIME_absolute_get_remaining
1462 (sock->nth.transmit_timeout),
1463 sock->sock, &transmit_ready, sock);
1468 * Ask the socket to call us once the specified number of bytes
1469 * are free in the transmission buffer. May call the notify
1470 * method immediately if enough space is available.
1472 * @param sock socket
1473 * @param size number of bytes to send
1474 * @param timeout after how long should we give up (and call
1475 * notify with buf NULL and size 0)?
1476 * @param notify function to call
1477 * @param notify_cls closure for notify
1478 * @return non-NULL if the notify callback was queued,
1479 * NULL if we are already going to notify someone else (busy)
1481 struct GNUNET_CONNECTION_TransmitHandle *
1482 GNUNET_CONNECTION_notify_transmit_ready (struct GNUNET_CONNECTION_Handle
1484 struct GNUNET_TIME_Relative timeout,
1485 GNUNET_CONNECTION_TransmitReadyNotify
1486 notify, void *notify_cls)
1488 if (sock->nth.notify_ready != NULL)
1490 GNUNET_assert (notify != NULL);
1491 GNUNET_assert (sock->write_buffer_size >= size);
1492 GNUNET_assert (sock->write_buffer_off <= sock->write_buffer_size);
1493 GNUNET_assert (sock->write_buffer_pos <= sock->write_buffer_size);
1494 GNUNET_assert (sock->write_buffer_pos <= sock->write_buffer_off);
1495 sock->nth.notify_ready = notify;
1496 sock->nth.notify_ready_cls = notify_cls;
1497 sock->nth.sh = sock;
1498 sock->nth.notify_size = size;
1499 sock->nth.transmit_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1500 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->nth.timeout_task);
1501 if ((sock->sock == NULL) &&
1502 (sock->ap_head == NULL) && (sock->dns_active == NULL))
1504 sock->write_task = GNUNET_SCHEDULER_add_now (sock->sched,
1505 &connect_error, sock);
1508 if (GNUNET_SCHEDULER_NO_TASK != sock->write_task)
1510 if (sock->sock != NULL)
1512 #if DEBUG_CONNECTION
1513 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1514 "Scheduling transmit_ready (%p).\n", sock);
1516 sock->write_task = GNUNET_SCHEDULER_add_write_net (sock->sched,
1517 GNUNET_TIME_absolute_get_remaining
1526 #if DEBUG_CONNECTION
1527 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1528 "CCS-Scheduling transmit_ready, adding timeout task (%p).\n",
1531 sock->ccs |= COCO_TRANSMIT_READY;
1532 sock->nth.timeout_task = GNUNET_SCHEDULER_add_delayed (sock->sched,
1542 * Cancel the specified transmission-ready
1546 GNUNET_CONNECTION_notify_transmit_ready_cancel (struct
1547 GNUNET_CONNECTION_TransmitHandle
1550 GNUNET_assert (h->notify_ready != NULL);
1551 if (0 != (h->sh->ccs & COCO_TRANSMIT_READY))
1553 #if DEBUG_CONNECTION
1554 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1555 "notify_transmit_ready_cancel cancels timeout_task (%p)\n",
1558 GNUNET_SCHEDULER_cancel (h->sh->sched, h->timeout_task);
1559 h->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1560 h->sh->ccs -= COCO_TRANSMIT_READY;
1564 if (h->sh->write_task != GNUNET_SCHEDULER_NO_TASK)
1566 GNUNET_SCHEDULER_cancel (h->sh->sched, h->sh->write_task);
1567 h->sh->write_task = GNUNET_SCHEDULER_NO_TASK;
1570 h->notify_ready = NULL;
1573 /* end of connection.c */