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"
40 #include "gnunet_server_lib.h"
42 #define DEBUG_CONNECTION GNUNET_NO
46 * Possible functions to call after connect failed or succeeded.
48 enum ConnectContinuations
56 * Call "receive_again".
58 COCO_RECEIVE_AGAIN = 1,
61 * Call "transmit_ready".
63 COCO_TRANSMIT_READY = 2,
66 * Call "destroy_continuation".
68 COCO_DESTROY_CONTINUATION = 4
73 * Transmission handle. There can only be one for each connection.
75 struct GNUNET_CONNECTION_TransmitHandle
79 * Function to call if the send buffer has notify_size
82 GNUNET_CONNECTION_TransmitReadyNotify notify_ready;
85 * Closure for notify_ready.
87 void *notify_ready_cls;
92 struct GNUNET_CONNECTION_Handle *sh;
95 * Timeout for receiving (in absolute time).
97 struct GNUNET_TIME_Absolute transmit_timeout;
100 * Task called on timeout.
102 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
105 * At what number of bytes available in the
106 * write buffer should the notify method be called?
114 * During connect, we try multiple possible IP addresses
115 * to find out which one might work.
121 * This is a linked list.
123 struct AddressProbe *next;
126 * This is a doubly-linked list.
128 struct AddressProbe *prev;
131 * The address; do not free (allocated at the end of this struct).
133 const struct sockaddr *addr;
136 * Underlying OS's socket.
138 struct GNUNET_NETWORK_Handle *sock;
141 * Connection for which we are probing.
143 struct GNUNET_CONNECTION_Handle *h;
151 * Task waiting for the socket to finish connecting.
153 GNUNET_SCHEDULER_TaskIdentifier task;
158 * @brief handle for a network socket
160 struct GNUNET_CONNECTION_Handle
164 * Configuration to use.
166 const struct GNUNET_CONFIGURATION_Handle *cfg;
169 * Linked list of sockets we are currently trying out
172 struct AddressProbe *ap_head;
175 * Linked list of sockets we are currently trying out
178 struct AddressProbe *ap_tail;
181 * Network address of the other end-point, may be NULL.
183 struct sockaddr *addr;
186 * Pointer to the hostname if socket was
187 * created using DNS lookup, otherwise NULL.
192 * Underlying OS's socket, set to NULL after fatal errors.
194 struct GNUNET_NETWORK_Handle *sock;
197 * Function to call on data received, NULL if no receive is pending.
199 GNUNET_CONNECTION_Receiver receiver;
202 * Closure for receiver.
207 * Pointer to our write buffer.
212 * Current size of our write buffer.
214 size_t write_buffer_size;
217 * Current write-offset in write buffer (where
218 * would we write next).
220 size_t write_buffer_off;
223 * Current read-offset in write buffer (how many
224 * bytes have already been sent).
226 size_t write_buffer_pos;
234 * Read task that we may need to wait for.
236 GNUNET_SCHEDULER_TaskIdentifier read_task;
239 * Write task that we may need to wait for.
241 GNUNET_SCHEDULER_TaskIdentifier write_task;
244 * Destroy task (if already scheduled).
246 GNUNET_SCHEDULER_TaskIdentifier destroy_task;
249 * Handle to a pending DNS lookup request.
251 struct GNUNET_RESOLVER_RequestHandle *dns_active;
254 * The handle we return for GNUNET_CONNECTION_notify_transmit_ready.
256 struct GNUNET_CONNECTION_TransmitHandle nth;
259 * Timeout for receiving (in absolute time).
261 struct GNUNET_TIME_Absolute receive_timeout;
264 * Functions to call after connect failed or succeeded.
266 enum ConnectContinuations ccs;
269 * Maximum number of bytes to read (for receiving).
274 * Ignore GNUNET_SCHEDULER_REASON_SHUTDOWN for this socket.
279 * Port to connect to.
284 * When shutdown, do not ever actually close the socket, but
285 * free resources. Only should ever be set if using program
286 * termination as a signal (because only then will the leaked
294 * Set the persist option on this connection handle. Indicates
295 * that the underlying socket or fd should never really be closed.
296 * Used for indicating process death.
298 * @param sock the connection to set persistent
300 void GNUNET_CONNECTION_persist_(struct GNUNET_CONNECTION_Handle *sock)
302 sock->persist = GNUNET_YES;
306 * Create a socket handle by boxing an existing OS socket. The OS
307 * socket should henceforth be no longer used directly.
308 * GNUNET_socket_destroy will close it.
310 * @param osSocket existing socket to box
311 * @return the boxed socket handle
313 struct GNUNET_CONNECTION_Handle *
314 GNUNET_CONNECTION_create_from_existing (struct GNUNET_NETWORK_Handle
317 struct GNUNET_CONNECTION_Handle *ret;
318 ret = GNUNET_malloc (sizeof (struct GNUNET_CONNECTION_Handle));
319 ret->write_buffer_size = GNUNET_SERVER_MIN_BUFFER_SIZE;
320 ret->write_buffer = GNUNET_malloc(ret->write_buffer_size);
321 ret->sock = osSocket;
327 * Create a socket handle by accepting on a listen socket. This
328 * function may block if the listen socket has no connection ready.
330 * @param access function to use to check if access is allowed
331 * @param access_cls closure for access
332 * @param lsock listen socket
333 * @return the socket handle, NULL on error
335 struct GNUNET_CONNECTION_Handle *
336 GNUNET_CONNECTION_create_from_accept (GNUNET_CONNECTION_AccessCheck access,
338 struct GNUNET_NETWORK_Handle *lsock)
340 struct GNUNET_CONNECTION_Handle *ret;
343 struct GNUNET_NETWORK_Handle *sock;
345 struct sockaddr_in *v4;
346 struct sockaddr_in6 *v6;
350 addrlen = sizeof (addr);
352 GNUNET_NETWORK_socket_accept (lsock, (struct sockaddr *) &addr, &addrlen);
355 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "accept");
358 if ( (addrlen > sizeof (addr)) ||
359 (addrlen < sizeof (sa_family_t)) )
362 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
366 sa = (struct sockaddr *) addr;
367 v6 = (struct sockaddr_in6 *) addr;
368 if ((sa->sa_family == AF_INET6) && (IN6_IS_ADDR_V4MAPPED (&v6->sin6_addr)))
370 /* convert to V4 address */
371 v4 = GNUNET_malloc (sizeof (struct sockaddr_in));
372 memset (v4, 0, sizeof (struct sockaddr_in));
373 v4->sin_family = AF_INET;
374 memcpy (&v4->sin_addr,
375 &((char *) &v6->sin6_addr)[sizeof (struct in6_addr) -
376 sizeof (struct in_addr)],
377 sizeof (struct in_addr));
378 v4->sin_port = v6->sin6_port;
380 addrlen = sizeof (struct sockaddr_in);
384 uaddr = GNUNET_malloc (addrlen);
385 memcpy (uaddr, addr, addrlen);
388 if ((access != NULL) &&
389 (GNUNET_YES != (aret = access (access_cls, uaddr, addrlen))))
391 if (aret == GNUNET_NO)
392 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
393 _("Access denied to `%s'\n"),
394 GNUNET_a2s (uaddr, addrlen));
395 GNUNET_break (GNUNET_OK ==
396 GNUNET_NETWORK_socket_shutdown (sock, SHUT_RDWR));
397 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
401 ret = GNUNET_malloc (sizeof (struct GNUNET_CONNECTION_Handle));
402 ret->write_buffer_size = GNUNET_SERVER_MIN_BUFFER_SIZE;
403 ret->write_buffer = GNUNET_malloc(ret->write_buffer_size);
405 ret->addrlen = addrlen;
408 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
409 _("Accepting connection from `%s': %p\n"),
410 GNUNET_a2s (uaddr, addrlen), ret);
416 * Obtain the network address of the other party.
418 * @param sock the client to get the address for
419 * @param addr where to store the address
420 * @param addrlen where to store the length of the address
421 * @return GNUNET_OK on success
424 GNUNET_CONNECTION_get_address (struct GNUNET_CONNECTION_Handle *sock,
425 void **addr, size_t * addrlen)
427 if ((sock->addr == NULL) || (sock->addrlen == 0))
429 *addr = GNUNET_malloc (sock->addrlen);
430 memcpy (*addr, sock->addr, sock->addrlen);
431 *addrlen = sock->addrlen;
437 * This function is called after establishing a connection either has
438 * succeeded or timed out. Note that it is possible that the attempt
439 * timed out and that we're immediately retrying. If we are retrying,
440 * we need to wait again (or timeout); if we succeeded, we need to
441 * wait for data (or timeout).
443 * @param cls our connection handle
444 * @param tc task context describing why we are here
447 receive_again (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
451 * Scheduler let us know that the connect task is finished (or was
452 * cancelled due to shutdown). Now really clean up.
454 * @param cls our "struct GNUNET_CONNECTION_Handle *"
458 destroy_continuation (void *cls,
459 const struct GNUNET_SCHEDULER_TaskContext *tc)
461 struct GNUNET_CONNECTION_Handle *sock = cls;
462 GNUNET_CONNECTION_TransmitReadyNotify notify;
463 struct AddressProbe *pos;
465 sock->destroy_task = GNUNET_SCHEDULER_NO_TASK;
466 GNUNET_assert (sock->dns_active == NULL);
467 if (0 != (sock->ccs & COCO_TRANSMIT_READY))
470 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
471 "Destroy waits for CCS-TR to be done (%p)\n", sock);
473 sock->ccs |= COCO_DESTROY_CONTINUATION;
476 if (sock->write_task != GNUNET_SCHEDULER_NO_TASK)
479 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
480 "Destroy waits for write_task to be done (%p)\n", sock);
482 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task);
484 = GNUNET_SCHEDULER_add_after (sock->write_task,
485 &destroy_continuation, sock);
488 if (0 != (sock->ccs & COCO_RECEIVE_AGAIN))
490 sock->ccs |= COCO_DESTROY_CONTINUATION;
493 if (sock->sock != NULL)
496 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
497 "Shutting down socket (%p)\n", sock);
499 if (sock->persist != GNUNET_YES)
500 GNUNET_NETWORK_socket_shutdown (sock->sock, SHUT_RDWR);
502 if (sock->read_task != GNUNET_SCHEDULER_NO_TASK)
504 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task);
506 = GNUNET_SCHEDULER_add_after (sock->read_task,
507 &destroy_continuation, sock);
511 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
512 "Destroy actually runs (%p)!\n", sock);
514 while (NULL != (pos = sock->ap_head))
516 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (pos->sock));
517 GNUNET_SCHEDULER_cancel (pos->task);
518 GNUNET_CONTAINER_DLL_remove (sock->ap_head, sock->ap_tail, pos);
521 GNUNET_assert (sock->nth.timeout_task == GNUNET_SCHEDULER_NO_TASK);
522 GNUNET_assert (sock->ccs == COCO_NONE);
523 if (NULL != (notify = sock->nth.notify_ready))
525 sock->nth.notify_ready = NULL;
526 notify (sock->nth.notify_ready_cls, 0, NULL);
529 if (sock->sock != NULL)
531 if (sock->persist != GNUNET_YES)
532 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock->sock));
534 GNUNET_free (sock->sock); /* at least no memory leak (we deliberately
535 leak the socket in this special case) ... */
537 GNUNET_free_non_null (sock->addr);
538 GNUNET_free_non_null (sock->hostname);
539 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task);
541 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
542 "Freeing memory of connection %p.\n", sock);
544 GNUNET_free (sock->write_buffer);
551 * See if we are now connected. If not, wait longer for
552 * connect to succeed. If connected, we should be able
553 * to write now as well, unless we timed out.
555 * @param cls our connection handle
556 * @param tc task context describing why we are here
559 transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
563 * We've failed for good to establish a connection.
565 * @param h the connection we tried to establish
568 connect_fail_continuation (struct GNUNET_CONNECTION_Handle *h)
571 GNUNET_log ((0 != strncmp (h->hostname,
574 ? GNUNET_ERROR_TYPE_INFO
575 : GNUNET_ERROR_TYPE_WARNING,
576 _("Failed to establish TCP connection to `%s:%u', no further addresses to try.\n"),
577 h->hostname, h->port);
579 /* connect failed / timed out */
580 GNUNET_break (h->ap_head == NULL);
581 GNUNET_break (h->ap_tail == NULL);
582 GNUNET_break (h->dns_active == GNUNET_NO);
583 GNUNET_break (h->sock == NULL);
585 /* trigger jobs that used to wait on "connect_task" */
586 if (0 != (h->ccs & COCO_RECEIVE_AGAIN))
589 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
590 "connect_fail_continuation triggers receive_again (%p)\n",
593 h->ccs -= COCO_RECEIVE_AGAIN;
594 h->read_task = GNUNET_SCHEDULER_add_after (GNUNET_SCHEDULER_NO_TASK,
597 if (0 != (h->ccs & COCO_TRANSMIT_READY))
600 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
601 "connect_fail_continuation cancels timeout_task, triggers transmit_ready (%p)\n",
604 GNUNET_assert (h->nth.timeout_task != GNUNET_SCHEDULER_NO_TASK);
605 GNUNET_SCHEDULER_cancel (h->nth.timeout_task);
606 h->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK;
607 h->ccs -= COCO_TRANSMIT_READY;
608 GNUNET_assert (h->write_task == GNUNET_SCHEDULER_NO_TASK);
609 h->write_task = GNUNET_SCHEDULER_add_after (GNUNET_SCHEDULER_NO_TASK,
612 if (0 != (h->ccs & COCO_DESTROY_CONTINUATION))
615 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
616 "connect_fail_continuation runs destroy_continuation (%p)\n",
619 h->ccs -= COCO_DESTROY_CONTINUATION;
620 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == h->destroy_task);
622 = GNUNET_SCHEDULER_add_now (&destroy_continuation,
629 * We've succeeded in establishing a connection.
631 * @param h the connection we tried to establish
634 connect_success_continuation (struct GNUNET_CONNECTION_Handle *h)
637 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
638 "Connection to `%s' succeeded! (%p)\n",
639 GNUNET_a2s (h->addr, h->addrlen), h);
641 /* trigger jobs that waited for the connection */
642 if (0 != (h->ccs & COCO_RECEIVE_AGAIN))
645 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
646 "connect_success_continuation runs receive_again (%p)\n",
649 h->ccs -= COCO_RECEIVE_AGAIN;
650 h->read_task = GNUNET_SCHEDULER_add_after (GNUNET_SCHEDULER_NO_TASK,
653 if (0 != (h->ccs & COCO_TRANSMIT_READY))
656 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
657 "connect_success_continuation runs transmit_ready, cancels timeout_task (%p)\n",
660 GNUNET_assert (h->nth.timeout_task != GNUNET_SCHEDULER_NO_TASK);
661 GNUNET_SCHEDULER_cancel (h->nth.timeout_task);
662 h->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK;
663 h->ccs -= COCO_TRANSMIT_READY;
664 GNUNET_assert (h->write_task == GNUNET_SCHEDULER_NO_TASK);
666 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_absolute_get_remaining
667 (h->nth.transmit_timeout), h->sock,
670 if (0 != (h->ccs & COCO_DESTROY_CONTINUATION))
673 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
674 "connect_success_continuation runs destroy_continuation (%p)\n",
677 h->ccs -= COCO_DESTROY_CONTINUATION;
678 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == h->destroy_task);
680 = GNUNET_SCHEDULER_add_now (&destroy_continuation,
687 * Scheduler let us know that we're either ready to write on the
688 * socket OR connect timed out. Do the right thing.
690 * @param cls the "struct AddressProbe*" with the address that we are probing
691 * @param tc success or failure info about the connect attempt.
694 connect_probe_continuation (void *cls,
695 const struct GNUNET_SCHEDULER_TaskContext *tc)
697 struct AddressProbe *ap = cls;
698 struct GNUNET_CONNECTION_Handle *h = ap->h;
699 struct AddressProbe *pos;
703 GNUNET_assert (ap->sock != NULL);
704 GNUNET_CONTAINER_DLL_remove (h->ap_head, h->ap_tail, ap);
705 len = sizeof (error);
708 if ( (0 == (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) ||
710 GNUNET_NETWORK_socket_getsockopt (ap->sock, SOL_SOCKET, SO_ERROR,
714 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ap->sock));
716 if ((NULL == h->ap_head) && (h->dns_active == GNUNET_NO))
717 connect_fail_continuation (h);
720 GNUNET_assert (h->sock == NULL);
722 GNUNET_assert (h->addr == NULL);
723 h->addr = GNUNET_malloc (ap->addrlen);
724 memcpy (h->addr, ap->addr, ap->addrlen);
725 h->addrlen = ap->addrlen;
727 /* cancel all other attempts */
728 while (NULL != (pos = h->ap_head))
730 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (pos->sock));
731 GNUNET_SCHEDULER_cancel (pos->task);
732 GNUNET_CONTAINER_DLL_remove (h->ap_head, h->ap_tail, pos);
735 connect_success_continuation (h);
740 * Try to establish a socket connection given the specified address.
741 * This function is called by the resolver once we have a DNS reply.
743 * @param cls our "struct GNUNET_CONNECTION_Handle *"
744 * @param addr address to try, NULL for "last call"
745 * @param addrlen length of addr
748 try_connect_using_address (void *cls,
749 const struct sockaddr *addr, socklen_t addrlen)
751 struct GNUNET_CONNECTION_Handle *h = cls;
752 struct AddressProbe *ap;
753 struct GNUNET_TIME_Relative delay;
757 h->dns_active = NULL;
758 if (NULL == h->ap_head)
759 connect_fail_continuation (h);
763 return; /* already connected */
764 GNUNET_assert (h->addr == NULL);
767 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
768 "Trying to connect using address `%s:%u/%s:%u'\n",
769 h->hostname, h->port, GNUNET_a2s (addr, addrlen), h->port);
771 ap = GNUNET_malloc (sizeof (struct AddressProbe) + addrlen);
772 ap->addr = (const struct sockaddr *) &ap[1];
773 memcpy (&ap[1], addr, addrlen);
774 ap->addrlen = addrlen;
777 switch (ap->addr->sa_family)
780 ((struct sockaddr_in *) ap->addr)->sin_port = htons (h->port);
783 ((struct sockaddr_in6 *) ap->addr)->sin6_port = htons (h->port);
788 return; /* not supported by us */
791 GNUNET_NETWORK_socket_create (ap->addr->sa_family, SOCK_STREAM, 0);
792 if (ap->sock == NULL)
795 return; /* not supported by OS */
798 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
799 _("Trying to connect to `%s' (%p)\n"),
800 GNUNET_a2s (ap->addr, ap->addrlen), h);
802 if ((GNUNET_OK != GNUNET_NETWORK_socket_connect (ap->sock,
805 (errno != EINPROGRESS))
807 /* maybe refused / unsupported address, try next */
808 GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "connect");
809 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ap->sock));
813 GNUNET_CONTAINER_DLL_insert (h->ap_head, h->ap_tail, ap);
814 delay = GNUNET_CONNECTION_CONNECT_RETRY_TIMEOUT;
815 if (h->nth.notify_ready != NULL)
816 delay = GNUNET_TIME_relative_min (delay,
817 GNUNET_TIME_absolute_get_remaining
818 (h->nth.transmit_timeout));
819 if (h->receiver != NULL)
820 delay = GNUNET_TIME_relative_min (delay,
821 GNUNET_TIME_absolute_get_remaining
822 (h->receive_timeout));
824 GNUNET_SCHEDULER_add_write_net (delay, ap->sock,
825 &connect_probe_continuation, ap);
830 * Create a socket handle by (asynchronously) connecting to a host.
831 * This function returns immediately, even if the connection has not
832 * yet been established. This function only creates TCP connections.
834 * @param cfg configuration to use
835 * @param hostname name of the host to connect to
836 * @param port port to connect to
837 * @return the socket handle
839 struct GNUNET_CONNECTION_Handle *
840 GNUNET_CONNECTION_create_from_connect (const struct
841 GNUNET_CONFIGURATION_Handle *cfg,
842 const char *hostname, uint16_t port)
844 struct GNUNET_CONNECTION_Handle *ret;
846 GNUNET_assert (0 < strlen (hostname)); /* sanity check */
847 ret = GNUNET_malloc (sizeof (struct GNUNET_CONNECTION_Handle));
849 ret->write_buffer_size = GNUNET_SERVER_MIN_BUFFER_SIZE;
850 ret->write_buffer = GNUNET_malloc(ret->write_buffer_size);
852 ret->hostname = GNUNET_strdup (hostname);
853 ret->dns_active = GNUNET_RESOLVER_ip_get (cfg,
856 GNUNET_CONNECTION_CONNECT_RETRY_TIMEOUT,
857 &try_connect_using_address, ret);
863 * Create a socket handle by connecting to a UNIX domain service.
864 * This function returns immediately, even if the connection has not
865 * yet been established. This function only creates UNIX connections.
867 * @param cfg configuration to use
868 * @param unixpath path to connect to
869 * @return the socket handle, NULL on systems without UNIX support
871 struct GNUNET_CONNECTION_Handle *
872 GNUNET_CONNECTION_create_from_connect_to_unixpath (const struct
873 GNUNET_CONFIGURATION_Handle *cfg,
874 const char *unixpath)
877 struct GNUNET_CONNECTION_Handle *ret;
878 struct sockaddr_un *un;
881 GNUNET_assert (0 < strlen (unixpath)); /* sanity check */
882 un = GNUNET_malloc (sizeof (struct sockaddr_un));
883 un->sun_family = AF_UNIX;
884 slen = strlen (unixpath) + 1;
885 if (slen >= sizeof (un->sun_path))
886 slen = sizeof (un->sun_path) - 1;
887 memcpy (un->sun_path,
890 un->sun_path[slen] = '\0';
891 slen += sizeof (sa_family_t);
893 un->sun_path[0] = '\0';
894 slen = sizeof (struct sockaddr_un);
896 ret = GNUNET_malloc (sizeof (struct GNUNET_CONNECTION_Handle));
898 ret->write_buffer_size = GNUNET_SERVER_MIN_BUFFER_SIZE;
899 ret->write_buffer = GNUNET_malloc(ret->write_buffer_size);
901 ret->hostname = NULL;
902 ret->addr = (struct sockaddr*) un;
904 ret->sock = GNUNET_NETWORK_socket_create (AF_UNIX, SOCK_STREAM, 0);
905 if (NULL == ret->sock)
907 GNUNET_free (ret->addr);
908 GNUNET_free (ret->write_buffer);
912 if (GNUNET_OK != GNUNET_NETWORK_socket_connect (ret->sock,
916 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret->sock));
917 GNUNET_free (ret->addr);
918 GNUNET_free (ret->write_buffer);
922 connect_success_continuation (ret);
931 * Create a socket handle by (asynchronously) connecting to a host.
932 * This function returns immediately, even if the connection has not
933 * yet been established. This function only creates TCP connections.
935 * @param af_family address family to use
936 * @param serv_addr server address
937 * @param addrlen length of server address
938 * @return the socket handle
940 struct GNUNET_CONNECTION_Handle *
941 GNUNET_CONNECTION_create_from_sockaddr (int af_family,
942 const struct sockaddr *serv_addr,
945 struct GNUNET_NETWORK_Handle *s;
946 struct GNUNET_CONNECTION_Handle *ret;
948 s = GNUNET_NETWORK_socket_create (af_family, SOCK_STREAM, 0);
951 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING |
952 GNUNET_ERROR_TYPE_BULK, "socket");
955 if ((GNUNET_OK != GNUNET_NETWORK_socket_connect (s, serv_addr, addrlen))
956 && (errno != EINPROGRESS))
958 /* maybe refused / unsupported address, try next */
959 GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "connect");
960 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (s));
963 ret = GNUNET_CONNECTION_create_from_existing (s);
964 ret->addr = GNUNET_malloc (addrlen);
965 memcpy (ret->addr, serv_addr, addrlen);
966 ret->addrlen = addrlen;
968 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
969 _("Trying to connect to `%s' (%p)\n"),
970 GNUNET_a2s (serv_addr, addrlen), ret);
977 * Check if socket is valid (no fatal errors have happened so far).
978 * Note that a socket that is still trying to connect is considered
981 * @param sock socket to check
982 * @return GNUNET_YES if valid, GNUNET_NO otherwise
985 GNUNET_CONNECTION_check (struct GNUNET_CONNECTION_Handle *sock)
987 if ((sock->ap_head != NULL) || (sock->dns_active != NULL))
988 return GNUNET_YES; /* still trying to connect */
989 return (sock->sock == NULL) ? GNUNET_NO : GNUNET_YES;
994 * Close the socket and free associated resources. Pending
995 * transmissions may be completed or dropped depending on the
996 * arguments. If a receive call is pending and should
997 * NOT be completed, 'GNUNET_CONNECTION_receive_cancel'
998 * should be called explicitly first.
1000 * @param sock socket to destroy
1001 * @param finish_pending_write should pending writes be completed or aborted?
1002 * (this applies to transmissions where the data has already been
1003 * read from the application; all other transmissions should be
1004 * aborted using 'GNUNET_CONNECTION_notify_transmit_ready_cancel').
1007 GNUNET_CONNECTION_destroy (struct GNUNET_CONNECTION_Handle *sock,
1008 int finish_pending_write)
1010 if (GNUNET_NO == finish_pending_write)
1012 if (sock->write_task != GNUNET_SCHEDULER_NO_TASK)
1014 GNUNET_SCHEDULER_cancel (sock->write_task);
1015 sock->write_task = GNUNET_SCHEDULER_NO_TASK;
1016 sock->write_buffer_off = 0;
1019 if ((sock->write_buffer_off == 0) && (sock->dns_active != NULL))
1021 GNUNET_RESOLVER_request_cancel (sock->dns_active);
1022 sock->dns_active = NULL;
1025 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task);
1027 = GNUNET_SCHEDULER_add_now (&destroy_continuation, sock);
1032 * Tell the receiver callback that a timeout was reached.
1035 signal_timeout (struct GNUNET_CONNECTION_Handle *sh)
1037 GNUNET_CONNECTION_Receiver receiver;
1039 #if DEBUG_CONNECTION
1040 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1041 "Network signals time out to receiver (%p)!\n", sh);
1043 GNUNET_assert (NULL != (receiver = sh->receiver));
1044 sh->receiver = NULL;
1045 receiver (sh->receiver_cls, NULL, 0, NULL, 0, 0);
1050 * Tell the receiver callback that we had an IO error.
1053 signal_error (struct GNUNET_CONNECTION_Handle *sh, int errcode)
1055 GNUNET_CONNECTION_Receiver receiver;
1056 GNUNET_assert (NULL != (receiver = sh->receiver));
1057 sh->receiver = NULL;
1058 receiver (sh->receiver_cls, NULL, 0, sh->addr, sh->addrlen, errcode);
1063 * This function is called once we either timeout
1064 * or have data ready to read.
1067 receive_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1069 struct GNUNET_CONNECTION_Handle *sh = cls;
1070 struct GNUNET_TIME_Absolute now;
1071 char buffer[sh->max];
1073 GNUNET_CONNECTION_Receiver receiver;
1075 sh->read_task = GNUNET_SCHEDULER_NO_TASK;
1076 if ( (GNUNET_YES == sh->ignore_shutdown) &&
1077 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
1079 /* ignore shutdown request, go again immediately */
1080 #if DEBUG_CONNECTION
1081 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1082 "Ignoring shutdown signal per configuration\n");
1084 sh->read_task = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_absolute_get_remaining
1085 (sh->receive_timeout),
1087 &receive_ready, sh);
1090 now = GNUNET_TIME_absolute_get ();
1091 if ((now.abs_value > sh->receive_timeout.abs_value) ||
1092 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT)) ||
1093 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
1095 #if DEBUG_CONNECTION
1096 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1097 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1098 "Receive from `%s' encounters error: time out by %llums... (%p)\n",
1099 GNUNET_a2s (sh->addr, sh->addrlen),
1100 GNUNET_TIME_absolute_get_duration (sh->receive_timeout).
1103 signal_timeout (sh);
1106 if (sh->sock == NULL)
1108 /* connect failed for good */
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 GNUNET_assert (GNUNET_NETWORK_fdset_isset (tc->read_ready, sh->sock));
1118 ret = GNUNET_NETWORK_socket_recv (sh->sock, buffer, sh->max);
1123 #if DEBUG_CONNECTION
1124 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1125 "Error receiving: %s\n", STRERROR (errno));
1127 signal_error (sh, errno);
1130 #if DEBUG_CONNECTION
1131 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1132 "receive_ready read %u/%u bytes from `%s' (%p)!\n",
1134 sh->max, GNUNET_a2s (sh->addr, sh->addrlen), sh);
1136 GNUNET_assert (NULL != (receiver = sh->receiver));
1137 sh->receiver = NULL;
1138 receiver (sh->receiver_cls, buffer, ret, sh->addr, sh->addrlen, 0);
1143 * This function is called after establishing a connection either has
1144 * succeeded or timed out. Note that it is possible that the attempt
1145 * timed out and that we're immediately retrying. If we are retrying,
1146 * we need to wait again (or timeout); if we succeeded, we need to
1147 * wait for data (or timeout).
1149 * @param cls our connection handle
1150 * @param tc task context describing why we are here
1153 receive_again (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1155 struct GNUNET_CONNECTION_Handle *sh = cls;
1156 struct GNUNET_TIME_Absolute now;
1158 sh->read_task = GNUNET_SCHEDULER_NO_TASK;
1159 if (sh->sock == NULL)
1161 /* not connected and no longer trying */
1162 #if DEBUG_CONNECTION
1163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1164 "Receive encounters error, socket closed (%p)...\n", sh);
1166 signal_error (sh, ECONNREFUSED);
1169 now = GNUNET_TIME_absolute_get ();
1170 if ((now.abs_value > sh->receive_timeout.abs_value) ||
1171 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
1173 #if DEBUG_CONNECTION
1174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1175 "Receive encounters error: time out (%p)...\n", sh);
1177 signal_timeout (sh);
1180 GNUNET_assert (sh->sock != NULL);
1181 /* connect succeeded, wait for data! */
1182 sh->read_task = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_absolute_get_remaining
1183 (sh->receive_timeout),
1185 &receive_ready, sh);
1190 * Receive data from the given socket. Note that this function will
1191 * call "receiver" asynchronously using the scheduler. It will
1192 * "immediately" return. Note that there MUST only be one active
1193 * receive call per socket at any given point in time (so do not
1194 * call receive again until the receiver callback has been invoked).
1196 * @param sock socket handle
1197 * @param max maximum number of bytes to read
1198 * @param timeout maximum amount of time to wait (use -1 for "forever")
1199 * @param receiver function to call with received data
1200 * @param receiver_cls closure for receiver
1203 GNUNET_CONNECTION_receive (struct GNUNET_CONNECTION_Handle *sock,
1205 struct GNUNET_TIME_Relative timeout,
1206 GNUNET_CONNECTION_Receiver receiver,
1209 struct GNUNET_SCHEDULER_TaskContext tc;
1211 GNUNET_assert ((sock->read_task == GNUNET_SCHEDULER_NO_TASK) &&
1212 (0 == (sock->ccs & COCO_RECEIVE_AGAIN)) &&
1213 (sock->receiver == NULL));
1214 sock->receiver = receiver;
1215 sock->receiver_cls = receiver_cls;
1216 sock->receive_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1218 if (sock->sock != NULL)
1220 memset (&tc, 0, sizeof (tc));
1221 tc.reason = GNUNET_SCHEDULER_REASON_PREREQ_DONE;
1222 receive_again (sock, &tc);
1225 if ((sock->dns_active == NULL) && (sock->ap_head == NULL))
1227 receiver (receiver_cls, NULL, 0, NULL, 0, ETIMEDOUT);
1230 sock->ccs += COCO_RECEIVE_AGAIN;
1235 * Configure this connection to ignore shutdown signals.
1237 * @param sock socket handle
1238 * @param do_ignore GNUNET_YES to ignore, GNUNET_NO to restore default
1241 GNUNET_CONNECTION_ignore_shutdown (struct GNUNET_CONNECTION_Handle *sock,
1244 sock->ignore_shutdown = do_ignore;
1249 * Cancel receive job on the given socket. Note that the
1250 * receiver callback must not have been called yet in order
1251 * for the cancellation to be valid.
1253 * @param sock socket handle
1254 * @return closure of the original receiver callback closure
1257 GNUNET_CONNECTION_receive_cancel (struct GNUNET_CONNECTION_Handle *sock)
1259 if (sock->read_task != GNUNET_SCHEDULER_NO_TASK)
1261 GNUNET_assert (sock == GNUNET_SCHEDULER_cancel (sock->read_task));
1262 sock->read_task = GNUNET_SCHEDULER_NO_TASK;
1266 GNUNET_assert (0 != (sock->ccs & COCO_RECEIVE_AGAIN));
1267 sock->ccs -= COCO_RECEIVE_AGAIN;
1269 sock->receiver = NULL;
1270 return sock->receiver_cls;
1275 * Try to call the transmit notify method (check if we do
1276 * have enough space available first)!
1278 * @param sock socket for which we should do this processing
1279 * @return GNUNET_YES if we were able to call notify
1282 process_notify (struct GNUNET_CONNECTION_Handle *sock)
1287 GNUNET_CONNECTION_TransmitReadyNotify notify;
1289 GNUNET_assert (sock->write_task == GNUNET_SCHEDULER_NO_TASK);
1290 if (NULL == (notify = sock->nth.notify_ready))
1292 used = sock->write_buffer_off - sock->write_buffer_pos;
1293 avail = sock->write_buffer_size - used;
1294 size = sock->nth.notify_size;
1297 sock->nth.notify_ready = NULL;
1298 if (sock->write_buffer_size - sock->write_buffer_off < size)
1300 /* need to compact */
1301 memmove (sock->write_buffer,
1302 &sock->write_buffer[sock->write_buffer_pos], used);
1303 sock->write_buffer_off -= sock->write_buffer_pos;
1304 sock->write_buffer_pos = 0;
1306 avail = sock->write_buffer_size - sock->write_buffer_off;
1307 GNUNET_assert (avail >= size);
1308 size = notify (sock->nth.notify_ready_cls,
1310 &sock->write_buffer[sock->write_buffer_off]);
1311 GNUNET_assert (size <= avail);
1312 sock->write_buffer_off += size;
1318 * Task invoked by the scheduler when a call to transmit
1319 * is timing out (we never got enough buffer space to call
1320 * the callback function before the specified timeout
1323 * This task notifies the client about the timeout.
1326 transmit_timeout (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 "transmit_timeout running (%p)\n", sock);
1335 sock->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK;
1336 #if DEBUG_CONNECTION
1337 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1338 "Transmit to `%s:%u/%s' fails, time out reached (%p).\n",
1340 sock->port, GNUNET_a2s (sock->addr, sock->addrlen), sock);
1342 GNUNET_assert (0 != (sock->ccs & COCO_TRANSMIT_READY));
1343 sock->ccs -= COCO_TRANSMIT_READY; /* remove request */
1344 notify = sock->nth.notify_ready;
1345 sock->nth.notify_ready = NULL;
1346 notify (sock->nth.notify_ready_cls, 0, NULL);
1351 * Task invoked by the scheduler when we failed to connect
1352 * at the time of being asked to transmit.
1354 * This task notifies the client about the error.
1357 connect_error (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1359 struct GNUNET_CONNECTION_Handle *sock = cls;
1360 GNUNET_CONNECTION_TransmitReadyNotify notify;
1362 #if DEBUG_CONNECTION
1363 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1364 "Transmission request of size %u fails, connection failed (%p).\n",
1365 sock->nth.notify_size, sock);
1367 sock->write_task = GNUNET_SCHEDULER_NO_TASK;
1368 notify = sock->nth.notify_ready;
1369 sock->nth.notify_ready = NULL;
1370 notify (sock->nth.notify_ready_cls, 0, NULL);
1375 transmit_error (struct GNUNET_CONNECTION_Handle *sock)
1377 GNUNET_CONNECTION_TransmitReadyNotify notify;
1379 if (NULL != sock->sock)
1381 GNUNET_NETWORK_socket_shutdown (sock->sock, SHUT_RDWR);
1382 GNUNET_break (GNUNET_OK ==
1383 GNUNET_NETWORK_socket_close (sock->sock));
1386 if (sock->read_task != GNUNET_SCHEDULER_NO_TASK)
1388 GNUNET_SCHEDULER_cancel (sock->read_task);
1389 sock->read_task = GNUNET_SCHEDULER_NO_TASK;
1390 signal_timeout (sock);
1393 if (sock->nth.notify_ready == NULL)
1394 return; /* nobody to tell about it */
1395 notify = sock->nth.notify_ready;
1396 sock->nth.notify_ready = NULL;
1397 notify (sock->nth.notify_ready_cls, 0, NULL);
1402 * See if we are now connected. If not, wait longer for
1403 * connect to succeed. If connected, we should be able
1404 * to write now as well, unless we timed out.
1406 * @param cls our connection handle
1407 * @param tc task context describing why we are here
1410 transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1412 struct GNUNET_CONNECTION_Handle *sock = cls;
1413 GNUNET_CONNECTION_TransmitReadyNotify notify;
1417 #if DEBUG_CONNECTION
1418 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1419 "transmit_ready running (%p).\n", sock);
1421 GNUNET_assert (sock->write_task != GNUNET_SCHEDULER_NO_TASK);
1422 sock->write_task = GNUNET_SCHEDULER_NO_TASK;
1423 GNUNET_assert (sock->nth.timeout_task == GNUNET_SCHEDULER_NO_TASK);
1424 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1426 if (sock->ignore_shutdown == GNUNET_YES)
1427 goto SCHEDULE_WRITE; /* ignore shutdown, go again immediately */
1428 #if DEBUG_CONNECTION
1429 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1430 "Transmit to `%s' fails, shutdown happened (%p).\n",
1431 GNUNET_a2s (sock->addr, sock->addrlen), sock);
1433 notify = sock->nth.notify_ready;
1434 sock->nth.notify_ready = NULL;
1435 notify (sock->nth.notify_ready_cls, 0, NULL);
1438 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT))
1440 #if DEBUG_CONNECTION
1441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1442 "Transmit to `%s' fails, time out reached (%p).\n",
1443 GNUNET_a2s (sock->addr, sock->addrlen), sock);
1445 notify = sock->nth.notify_ready;
1446 sock->nth.notify_ready = NULL;
1447 notify (sock->nth.notify_ready_cls, 0, NULL);
1450 GNUNET_assert (NULL != sock->sock);
1451 if (tc->write_ready == NULL)
1453 /* special circumstances (in particular,
1454 PREREQ_DONE after connect): not yet ready to write,
1455 but no "fatal" error either. Hence retry. */
1456 goto SCHEDULE_WRITE;
1458 if (!GNUNET_NETWORK_fdset_isset (tc->write_ready, sock->sock))
1460 #if DEBUG_CONNECTION
1461 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1463 ("Could not satisfy pending transmission request, socket closed or connect failed (%p).\n"),
1466 transmit_error (sock);
1467 return; /* connect failed for good, we're finished */
1469 GNUNET_assert (sock->write_buffer_off >= sock->write_buffer_pos);
1470 if ( (sock->nth.notify_ready != NULL) &&
1471 (sock->write_buffer_size < sock->nth.notify_size) )
1473 sock->write_buffer = GNUNET_realloc(sock->write_buffer,
1474 sock->nth.notify_size);
1475 sock->write_buffer_size = sock->nth.notify_size;
1477 process_notify (sock);
1478 have = sock->write_buffer_off - sock->write_buffer_pos;
1481 /* no data ready for writing, terminate write loop */
1484 GNUNET_assert (have <= sock->write_buffer_size);
1485 GNUNET_assert (have + sock->write_buffer_pos <= sock->write_buffer_size);
1486 GNUNET_assert (sock->write_buffer_pos <= sock->write_buffer_size);
1488 ret = GNUNET_NETWORK_socket_send (sock->sock,
1489 &sock->write_buffer[sock->
1498 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1499 _("Failed to send to `%s': %s\n"),
1500 GNUNET_a2s (sock->addr,
1504 #if DEBUG_CONNECTION
1505 GNUNET_log_strerror (GNUNET_ERROR_TYPE_DEBUG, "send");
1507 transmit_error (sock);
1510 #if DEBUG_CONNECTION
1511 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1512 "transmit_ready transmitted %u/%u bytes to `%s' (%p)\n",
1514 have, GNUNET_a2s (sock->addr, sock->addrlen), sock);
1516 sock->write_buffer_pos += ret;
1517 if (sock->write_buffer_pos == sock->write_buffer_off)
1519 /* transmitted all pending data */
1520 sock->write_buffer_pos = 0;
1521 sock->write_buffer_off = 0;
1523 if ((sock->write_buffer_off == 0) && (NULL == sock->nth.notify_ready))
1524 return; /* all data sent! */
1525 /* not done writing, schedule more */
1527 #if DEBUG_CONNECTION
1528 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1529 "Re-scheduling transmit_ready (more to do) (%p).\n", sock);
1531 if (sock->write_task == GNUNET_SCHEDULER_NO_TASK)
1533 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_absolute_get_remaining
1534 (sock->nth.transmit_timeout),
1535 sock->sock, &transmit_ready, sock);
1540 * Ask the socket to call us once the specified number of bytes
1541 * are free in the transmission buffer. May call the notify
1542 * method immediately if enough space is available.
1544 * @param sock socket
1545 * @param size number of bytes to send
1546 * @param timeout after how long should we give up (and call
1547 * notify with buf NULL and size 0)?
1548 * @param notify function to call
1549 * @param notify_cls closure for notify
1550 * @return non-NULL if the notify callback was queued,
1551 * NULL if we are already going to notify someone else (busy)
1553 struct GNUNET_CONNECTION_TransmitHandle *
1554 GNUNET_CONNECTION_notify_transmit_ready (struct GNUNET_CONNECTION_Handle
1556 struct GNUNET_TIME_Relative timeout,
1557 GNUNET_CONNECTION_TransmitReadyNotify
1558 notify, void *notify_cls)
1560 if (sock->nth.notify_ready != NULL)
1562 GNUNET_assert (notify != NULL);
1563 GNUNET_assert (size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
1564 GNUNET_assert (sock->write_buffer_off <= sock->write_buffer_size);
1565 GNUNET_assert (sock->write_buffer_pos <= sock->write_buffer_size);
1566 GNUNET_assert (sock->write_buffer_pos <= sock->write_buffer_off);
1567 sock->nth.notify_ready = notify;
1568 sock->nth.notify_ready_cls = notify_cls;
1569 sock->nth.sh = sock;
1570 sock->nth.notify_size = size;
1571 sock->nth.transmit_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1572 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->nth.timeout_task);
1573 if ((sock->sock == NULL) &&
1574 (sock->ap_head == NULL) && (sock->dns_active == NULL))
1576 if (sock->write_task != GNUNET_SCHEDULER_NO_TASK)
1577 GNUNET_SCHEDULER_cancel (sock->write_task);
1578 sock->write_task = GNUNET_SCHEDULER_add_now (&connect_error, sock);
1581 if (GNUNET_SCHEDULER_NO_TASK != sock->write_task)
1583 if (sock->sock != NULL)
1585 #if DEBUG_CONNECTION
1586 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1587 "Scheduling transmit_ready (%p).\n", sock);
1589 sock->write_task = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_absolute_get_remaining
1598 #if DEBUG_CONNECTION
1599 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1600 "CCS-Scheduling transmit_ready, adding timeout task (%p).\n",
1603 sock->ccs |= COCO_TRANSMIT_READY;
1604 sock->nth.timeout_task = GNUNET_SCHEDULER_add_delayed (timeout,
1613 * Cancel the specified transmission-ready
1617 GNUNET_CONNECTION_notify_transmit_ready_cancel (struct
1618 GNUNET_CONNECTION_TransmitHandle
1621 GNUNET_assert (h->notify_ready != NULL);
1622 if (0 != (h->sh->ccs & COCO_TRANSMIT_READY))
1624 #if DEBUG_CONNECTION
1625 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1626 "notify_transmit_ready_cancel cancels timeout_task (%p)\n",
1629 GNUNET_SCHEDULER_cancel (h->timeout_task);
1630 h->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1631 h->sh->ccs -= COCO_TRANSMIT_READY;
1635 if (h->sh->write_task != GNUNET_SCHEDULER_NO_TASK)
1637 GNUNET_SCHEDULER_cancel (h->sh->write_task);
1638 h->sh->write_task = GNUNET_SCHEDULER_NO_TASK;
1641 h->notify_ready = NULL;
1644 /* end of connection.c */