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
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 * Configuration to use.
165 const struct GNUNET_CONFIGURATION_Handle *cfg;
168 * Linked list of sockets we are currently trying out
171 struct AddressProbe *ap_head;
174 * Linked list of sockets we are currently trying out
177 struct AddressProbe *ap_tail;
180 * Network address of the other end-point, may be NULL.
182 struct sockaddr *addr;
185 * Pointer to the hostname if socket was
186 * created using DNS lookup, otherwise NULL.
191 * Underlying OS's socket, set to NULL after fatal errors.
193 struct GNUNET_NETWORK_Handle *sock;
196 * Function to call on data received, NULL if no receive is pending.
198 GNUNET_CONNECTION_Receiver receiver;
201 * Closure for receiver.
206 * Pointer to our write buffer.
211 * Current size of our write buffer.
213 size_t write_buffer_size;
216 * Current write-offset in write buffer (where
217 * would we write next).
219 size_t write_buffer_off;
222 * Current read-offset in write buffer (how many
223 * bytes have already been sent).
225 size_t write_buffer_pos;
233 * Read task that we may need to wait for.
235 GNUNET_SCHEDULER_TaskIdentifier read_task;
238 * Write task that we may need to wait for.
240 GNUNET_SCHEDULER_TaskIdentifier write_task;
243 * Destroy task (if already scheduled).
245 GNUNET_SCHEDULER_TaskIdentifier destroy_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
300 GNUNET_CONNECTION_persist_ (struct GNUNET_CONNECTION_Handle *sock)
302 sock->persist = GNUNET_YES;
307 * Disable the "CORK" feature for communication with the given socket,
308 * forcing the OS to immediately flush the buffer on transmission
309 * instead of potentially buffering multiple messages. Essentially
310 * reduces the OS send buffers to zero.
311 * Used to make sure that the last messages sent through the connection
312 * reach the other side before the process is terminated.
314 * @param sock the connection to make flushing and blocking
315 * @return GNUNET_OK on success
318 GNUNET_CONNECTION_disable_corking (struct GNUNET_CONNECTION_Handle *sock)
320 return GNUNET_NETWORK_socket_disable_corking (sock->sock);
324 * Create a socket handle by boxing an existing OS socket. The OS
325 * socket should henceforth be no longer used directly.
326 * GNUNET_socket_destroy will close it.
328 * @param osSocket existing socket to box
329 * @return the boxed socket handle
331 struct GNUNET_CONNECTION_Handle *
332 GNUNET_CONNECTION_create_from_existing (struct GNUNET_NETWORK_Handle *osSocket)
334 struct GNUNET_CONNECTION_Handle *ret;
336 ret = GNUNET_malloc (sizeof (struct GNUNET_CONNECTION_Handle));
337 ret->write_buffer_size = GNUNET_SERVER_MIN_BUFFER_SIZE;
338 ret->write_buffer = GNUNET_malloc (ret->write_buffer_size);
339 ret->sock = osSocket;
345 * Create a socket handle by accepting on a listen socket. This
346 * function may block if the listen socket has no connection ready.
348 * @param access function to use to check if access is allowed
349 * @param access_cls closure for access
350 * @param lsock listen socket
351 * @return the socket handle, NULL on error
353 struct GNUNET_CONNECTION_Handle *
354 GNUNET_CONNECTION_create_from_accept (GNUNET_CONNECTION_AccessCheck access,
356 struct GNUNET_NETWORK_Handle *lsock)
358 struct GNUNET_CONNECTION_Handle *ret;
361 struct GNUNET_NETWORK_Handle *sock;
363 struct sockaddr_in *v4;
364 struct sockaddr_in6 *v6;
367 struct GNUNET_CONNECTION_Credentials *gcp;
368 struct GNUNET_CONNECTION_Credentials gc;
375 addrlen = sizeof (addr);
377 GNUNET_NETWORK_socket_accept (lsock, (struct sockaddr *) &addr, &addrlen);
380 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "accept");
383 if ((addrlen > sizeof (addr)) || (addrlen < sizeof (sa_family_t)))
386 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
390 sa = (struct sockaddr *) addr;
391 v6 = (struct sockaddr_in6 *) addr;
392 if ((sa->sa_family == AF_INET6) && (IN6_IS_ADDR_V4MAPPED (&v6->sin6_addr)))
394 /* convert to V4 address */
395 v4 = GNUNET_malloc (sizeof (struct sockaddr_in));
396 memset (v4, 0, sizeof (struct sockaddr_in));
397 v4->sin_family = AF_INET;
398 #if HAVE_SOCKADDR_IN_SIN_LEN
399 v4->sin_len = (u_char) sizeof (struct sockaddr_in);
401 memcpy (&v4->sin_addr,
402 &((char *) &v6->sin6_addr)[sizeof (struct in6_addr) -
403 sizeof (struct in_addr)],
404 sizeof (struct in_addr));
405 v4->sin_port = v6->sin6_port;
407 addrlen = sizeof (struct sockaddr_in);
411 uaddr = GNUNET_malloc (addrlen);
412 memcpy (uaddr, addr, addrlen);
417 if (sa->sa_family == AF_UNIX)
421 if (0 == getpeereid (GNUNET_NETWORK_get_fd (sock), &gc.uid, &gc.gid))
425 /* largely traditional GNU/Linux */
428 getsockopt (GNUNET_NETWORK_get_fd (sock), SOL_SOCKET, SO_PEERCRED, &uc,
429 &olen)) && (olen == sizeof (uc)))
436 #if HAVE_GETPEERUCRED
437 /* this is for Solaris 10 */
441 if (0 == getpeerucred (GNUNET_NETWORK_get_fd (sock), &uc))
443 gc.uid = ucred_geteuid (uc);
444 gc.gid = ucred_getegid (uc);
453 if ((access != NULL) &&
454 (GNUNET_YES != (aret = access (access_cls, gcp, uaddr, addrlen))))
456 if (aret == GNUNET_NO)
457 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Access denied to `%s'\n"),
458 GNUNET_a2s (uaddr, addrlen));
459 GNUNET_break (GNUNET_OK ==
460 GNUNET_NETWORK_socket_shutdown (sock, SHUT_RDWR));
461 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
465 ret = GNUNET_malloc (sizeof (struct GNUNET_CONNECTION_Handle));
466 ret->write_buffer_size = GNUNET_SERVER_MIN_BUFFER_SIZE;
467 ret->write_buffer = GNUNET_malloc (ret->write_buffer_size);
469 ret->addrlen = addrlen;
472 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Accepting connection from `%s': %p\n"),
473 GNUNET_a2s (uaddr, addrlen), ret);
479 * Obtain the network address of the other party.
481 * @param sock the client to get the address for
482 * @param addr where to store the address
483 * @param addrlen where to store the length of the address
484 * @return GNUNET_OK on success
487 GNUNET_CONNECTION_get_address (struct GNUNET_CONNECTION_Handle *sock,
488 void **addr, size_t * addrlen)
490 if ((sock->addr == NULL) || (sock->addrlen == 0))
492 *addr = GNUNET_malloc (sock->addrlen);
493 memcpy (*addr, sock->addr, sock->addrlen);
494 *addrlen = sock->addrlen;
500 * This function is called after establishing a connection either has
501 * succeeded or timed out. Note that it is possible that the attempt
502 * timed out and that we're immediately retrying. If we are retrying,
503 * we need to wait again (or timeout); if we succeeded, we need to
504 * wait for data (or timeout).
506 * @param cls our connection handle
507 * @param tc task context describing why we are here
510 receive_again (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
514 * Scheduler let us know that the connect task is finished (or was
515 * cancelled due to shutdown). Now really clean up.
517 * @param cls our "struct GNUNET_CONNECTION_Handle *"
521 destroy_continuation (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
523 struct GNUNET_CONNECTION_Handle *sock = cls;
524 GNUNET_CONNECTION_TransmitReadyNotify notify;
525 struct AddressProbe *pos;
527 sock->destroy_task = GNUNET_SCHEDULER_NO_TASK;
528 GNUNET_assert (sock->dns_active == NULL);
529 if (0 != (sock->ccs & COCO_TRANSMIT_READY))
532 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
533 "Destroy waits for CCS-TR to be done (%p)\n", sock);
535 sock->ccs |= COCO_DESTROY_CONTINUATION;
538 if (sock->write_task != GNUNET_SCHEDULER_NO_TASK)
541 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
542 "Destroy waits for write_task to be done (%p)\n", sock);
544 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task);
546 GNUNET_SCHEDULER_add_after (sock->write_task, &destroy_continuation,
550 if (0 != (sock->ccs & COCO_RECEIVE_AGAIN))
552 sock->ccs |= COCO_DESTROY_CONTINUATION;
555 if (sock->sock != NULL)
558 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down socket (%p)\n", sock);
560 if (sock->persist != GNUNET_YES)
562 if ((GNUNET_YES != GNUNET_NETWORK_socket_shutdown (sock->sock, SHUT_RDWR))
563 && (errno != ENOTCONN) && (errno != ECONNRESET))
564 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "shutdown");
567 if (sock->read_task != GNUNET_SCHEDULER_NO_TASK)
569 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task);
571 GNUNET_SCHEDULER_add_after (sock->read_task, &destroy_continuation,
576 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Destroy actually runs (%p)!\n", sock);
578 while (NULL != (pos = sock->ap_head))
580 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (pos->sock));
581 GNUNET_SCHEDULER_cancel (pos->task);
582 GNUNET_CONTAINER_DLL_remove (sock->ap_head, sock->ap_tail, pos);
585 GNUNET_assert (sock->nth.timeout_task == GNUNET_SCHEDULER_NO_TASK);
586 GNUNET_assert (sock->ccs == COCO_NONE);
587 if (NULL != (notify = sock->nth.notify_ready))
589 sock->nth.notify_ready = NULL;
590 notify (sock->nth.notify_ready_cls, 0, NULL);
593 if (sock->sock != NULL)
595 if (sock->persist != GNUNET_YES)
596 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock->sock));
598 GNUNET_free (sock->sock); /* at least no memory leak (we deliberately
599 * leak the socket in this special case) ... */
601 GNUNET_free_non_null (sock->addr);
602 GNUNET_free_non_null (sock->hostname);
603 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task);
605 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Freeing memory of connection %p.\n",
608 GNUNET_free (sock->write_buffer);
615 * See if we are now connected. If not, wait longer for
616 * connect to succeed. If connected, we should be able
617 * to write now as well, unless we timed out.
619 * @param cls our connection handle
620 * @param tc task context describing why we are here
623 transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
627 * We've failed for good to establish a connection.
629 * @param h the connection we tried to establish
632 connect_fail_continuation (struct GNUNET_CONNECTION_Handle *h)
636 strncmp (h->hostname, "localhost:",
637 10)) ? GNUNET_ERROR_TYPE_INFO :
638 GNUNET_ERROR_TYPE_WARNING,
640 ("Failed to establish TCP connection to `%s:%u', no further addresses to try.\n"),
641 h->hostname, h->port);
643 /* connect failed / timed out */
644 GNUNET_break (h->ap_head == NULL);
645 GNUNET_break (h->ap_tail == NULL);
646 GNUNET_break (h->dns_active == GNUNET_NO);
647 GNUNET_break (h->sock == NULL);
649 /* trigger jobs that used to wait on "connect_task" */
650 if (0 != (h->ccs & COCO_RECEIVE_AGAIN))
653 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
654 "connect_fail_continuation triggers receive_again (%p)\n", h);
656 h->ccs -= COCO_RECEIVE_AGAIN;
657 h->read_task = GNUNET_SCHEDULER_add_now (&receive_again, h);
659 if (0 != (h->ccs & COCO_TRANSMIT_READY))
662 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
663 "connect_fail_continuation cancels timeout_task, triggers transmit_ready (%p)\n",
666 GNUNET_assert (h->nth.timeout_task != GNUNET_SCHEDULER_NO_TASK);
667 GNUNET_SCHEDULER_cancel (h->nth.timeout_task);
668 h->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK;
669 h->ccs -= COCO_TRANSMIT_READY;
670 GNUNET_assert (h->nth.notify_ready != NULL);
671 GNUNET_assert (h->write_task == GNUNET_SCHEDULER_NO_TASK);
672 h->write_task = GNUNET_SCHEDULER_add_now (&transmit_ready, h);
674 if (0 != (h->ccs & COCO_DESTROY_CONTINUATION))
677 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
678 "connect_fail_continuation runs destroy_continuation (%p)\n",
681 h->ccs -= COCO_DESTROY_CONTINUATION;
682 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == h->destroy_task);
683 h->destroy_task = GNUNET_SCHEDULER_add_now (&destroy_continuation, h);
689 * We've succeeded in establishing a connection.
691 * @param h the connection we tried to establish
694 connect_success_continuation (struct GNUNET_CONNECTION_Handle *h)
697 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connection to `%s' succeeded! (%p)\n",
698 GNUNET_a2s (h->addr, h->addrlen), h);
700 /* trigger jobs that waited for the connection */
701 if (0 != (h->ccs & COCO_RECEIVE_AGAIN))
704 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
705 "connect_success_continuation runs receive_again (%p)\n", h);
707 h->ccs -= COCO_RECEIVE_AGAIN;
708 h->read_task = GNUNET_SCHEDULER_add_now (&receive_again, h);
710 if (0 != (h->ccs & COCO_TRANSMIT_READY))
713 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
714 "connect_success_continuation runs transmit_ready, cancels timeout_task (%p)\n",
717 GNUNET_assert (h->nth.timeout_task != GNUNET_SCHEDULER_NO_TASK);
718 GNUNET_SCHEDULER_cancel (h->nth.timeout_task);
719 h->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK;
720 h->ccs -= COCO_TRANSMIT_READY;
721 GNUNET_assert (h->write_task == GNUNET_SCHEDULER_NO_TASK);
722 GNUNET_assert (h->nth.notify_ready != NULL);
724 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_absolute_get_remaining
725 (h->nth.transmit_timeout), h->sock,
728 if (0 != (h->ccs & COCO_DESTROY_CONTINUATION))
731 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
732 "connect_success_continuation runs destroy_continuation (%p)\n",
735 h->ccs -= COCO_DESTROY_CONTINUATION;
736 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == h->destroy_task);
737 h->destroy_task = GNUNET_SCHEDULER_add_now (&destroy_continuation, h);
743 * Scheduler let us know that we're either ready to write on the
744 * socket OR connect timed out. Do the right thing.
746 * @param cls the "struct AddressProbe*" with the address that we are probing
747 * @param tc success or failure info about the connect attempt.
750 connect_probe_continuation (void *cls,
751 const struct GNUNET_SCHEDULER_TaskContext *tc)
753 struct AddressProbe *ap = cls;
754 struct GNUNET_CONNECTION_Handle *h = ap->h;
755 struct AddressProbe *pos;
759 GNUNET_assert (ap->sock != NULL);
760 GNUNET_CONTAINER_DLL_remove (h->ap_head, h->ap_tail, ap);
761 len = sizeof (error);
764 if ((0 == (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) ||
766 GNUNET_NETWORK_socket_getsockopt (ap->sock, SOL_SOCKET, SO_ERROR, &error,
767 &len)) || (error != 0))
769 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ap->sock));
771 if ((NULL == h->ap_head) && (h->dns_active == GNUNET_NO))
772 connect_fail_continuation (h);
775 GNUNET_assert (h->sock == NULL);
777 GNUNET_assert (h->addr == NULL);
778 h->addr = GNUNET_malloc (ap->addrlen);
779 memcpy (h->addr, ap->addr, ap->addrlen);
780 h->addrlen = ap->addrlen;
782 /* cancel all other attempts */
783 while (NULL != (pos = h->ap_head))
785 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (pos->sock));
786 GNUNET_SCHEDULER_cancel (pos->task);
787 GNUNET_CONTAINER_DLL_remove (h->ap_head, h->ap_tail, pos);
790 connect_success_continuation (h);
795 * Try to establish a socket connection given the specified address.
796 * This function is called by the resolver once we have a DNS reply.
798 * @param cls our "struct GNUNET_CONNECTION_Handle *"
799 * @param addr address to try, NULL for "last call"
800 * @param addrlen length of addr
803 try_connect_using_address (void *cls, const struct sockaddr *addr,
806 struct GNUNET_CONNECTION_Handle *h = cls;
807 struct AddressProbe *ap;
808 struct GNUNET_TIME_Relative delay;
812 h->dns_active = NULL;
813 if (NULL == h->ap_head)
814 connect_fail_continuation (h);
818 return; /* already connected */
819 GNUNET_assert (h->addr == NULL);
822 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
823 "Trying to connect using address `%s:%u/%s:%u'\n", h->hostname,
824 h->port, GNUNET_a2s (addr, addrlen), h->port);
826 ap = GNUNET_malloc (sizeof (struct AddressProbe) + addrlen);
827 ap->addr = (const struct sockaddr *) &ap[1];
828 memcpy (&ap[1], addr, addrlen);
829 ap->addrlen = addrlen;
832 switch (ap->addr->sa_family)
835 ((struct sockaddr_in *) ap->addr)->sin_port = htons (h->port);
838 ((struct sockaddr_in6 *) ap->addr)->sin6_port = htons (h->port);
843 return; /* not supported by us */
845 ap->sock = GNUNET_NETWORK_socket_create (ap->addr->sa_family, SOCK_STREAM, 0);
846 if (ap->sock == NULL)
849 return; /* not supported by OS */
852 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Trying to connect to `%s' (%p)\n"),
853 GNUNET_a2s (ap->addr, ap->addrlen), h);
856 GNUNET_NETWORK_socket_connect (ap->sock, ap->addr, ap->addrlen)) &&
857 (errno != EINPROGRESS))
859 /* maybe refused / unsupported address, try next */
860 GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "connect");
861 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ap->sock));
865 GNUNET_CONTAINER_DLL_insert (h->ap_head, h->ap_tail, ap);
866 delay = GNUNET_CONNECTION_CONNECT_RETRY_TIMEOUT;
867 if (h->nth.notify_ready != NULL)
869 GNUNET_TIME_relative_min (delay,
870 GNUNET_TIME_absolute_get_remaining (h->
871 nth.transmit_timeout));
872 if (h->receiver != NULL)
874 GNUNET_TIME_relative_min (delay,
875 GNUNET_TIME_absolute_get_remaining
876 (h->receive_timeout));
878 GNUNET_SCHEDULER_add_write_net (delay, ap->sock,
879 &connect_probe_continuation, ap);
884 * Create a socket handle by (asynchronously) connecting to a host.
885 * This function returns immediately, even if the connection has not
886 * yet been established. This function only creates TCP connections.
888 * @param cfg configuration to use
889 * @param hostname name of the host to connect to
890 * @param port port to connect to
891 * @return the socket handle
893 struct GNUNET_CONNECTION_Handle *
894 GNUNET_CONNECTION_create_from_connect (const struct GNUNET_CONFIGURATION_Handle
895 *cfg, const char *hostname,
898 struct GNUNET_CONNECTION_Handle *ret;
900 GNUNET_assert (0 < strlen (hostname)); /* sanity check */
901 ret = GNUNET_malloc (sizeof (struct GNUNET_CONNECTION_Handle));
903 ret->write_buffer_size = GNUNET_SERVER_MIN_BUFFER_SIZE;
904 ret->write_buffer = GNUNET_malloc (ret->write_buffer_size);
906 ret->hostname = GNUNET_strdup (hostname);
908 GNUNET_RESOLVER_ip_get (ret->hostname, AF_UNSPEC,
909 GNUNET_CONNECTION_CONNECT_RETRY_TIMEOUT,
910 &try_connect_using_address, ret);
916 * Create a socket handle by connecting to a UNIX domain service.
917 * This function returns immediately, even if the connection has not
918 * yet been established. This function only creates UNIX connections.
920 * @param cfg configuration to use
921 * @param unixpath path to connect to
922 * @return the socket handle, NULL on systems without UNIX support
924 struct GNUNET_CONNECTION_Handle *
925 GNUNET_CONNECTION_create_from_connect_to_unixpath (const struct
926 GNUNET_CONFIGURATION_Handle
927 *cfg, const char *unixpath)
930 struct GNUNET_CONNECTION_Handle *ret;
931 struct sockaddr_un *un;
934 GNUNET_assert (0 < strlen (unixpath)); /* sanity check */
935 un = GNUNET_malloc (sizeof (struct sockaddr_un));
936 un->sun_family = AF_UNIX;
937 slen = strlen (unixpath);
938 if (slen >= sizeof (un->sun_path))
939 slen = sizeof (un->sun_path) - 1;
940 memcpy (un->sun_path, unixpath, slen);
941 un->sun_path[slen] = '\0';
942 slen = sizeof (struct sockaddr_un);
943 #if HAVE_SOCKADDR_IN_SIN_LEN
944 un->sun_len = (u_char) slen;
947 un->sun_path[0] = '\0';
949 ret = GNUNET_malloc (sizeof (struct GNUNET_CONNECTION_Handle));
951 ret->write_buffer_size = GNUNET_SERVER_MIN_BUFFER_SIZE;
952 ret->write_buffer = GNUNET_malloc (ret->write_buffer_size);
954 ret->hostname = NULL;
955 ret->addr = (struct sockaddr *) un;
957 ret->sock = GNUNET_NETWORK_socket_create (AF_UNIX, SOCK_STREAM, 0);
958 if (NULL == ret->sock)
960 GNUNET_free (ret->addr);
961 GNUNET_free (ret->write_buffer);
966 GNUNET_NETWORK_socket_connect (ret->sock, ret->addr, ret->addrlen))
968 /* Just return; we expect everything to work eventually so don't fail HARD */
969 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret->sock));
973 connect_success_continuation (ret);
982 * Create a socket handle by (asynchronously) connecting to a host.
983 * This function returns immediately, even if the connection has not
984 * yet been established. This function only creates TCP connections.
986 * @param af_family address family to use
987 * @param serv_addr server address
988 * @param addrlen length of server address
989 * @return the socket handle
991 struct GNUNET_CONNECTION_Handle *
992 GNUNET_CONNECTION_create_from_sockaddr (int af_family,
993 const struct sockaddr *serv_addr,
996 struct GNUNET_NETWORK_Handle *s;
997 struct GNUNET_CONNECTION_Handle *ret;
1000 s = GNUNET_NETWORK_socket_create (af_family, SOCK_STREAM, 0);
1003 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
1007 if ((GNUNET_OK != GNUNET_NETWORK_socket_connect (s, serv_addr, addrlen)) &&
1008 (errno != EINPROGRESS))
1010 /* maybe refused / unsupported address, try next */
1011 GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "connect");
1012 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1013 _("Attempt to connect to `%s' failed\n"), GNUNET_a2s (serv_addr,
1015 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (s));
1018 ret = GNUNET_CONNECTION_create_from_existing (s);
1019 ret->addr = GNUNET_malloc (addrlen);
1020 memcpy (ret->addr, serv_addr, addrlen);
1021 ret->addrlen = addrlen;
1022 #if DEBUG_CONNECTION
1023 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Trying to connect to `%s' (%p)\n"),
1024 GNUNET_a2s (serv_addr, addrlen), ret);
1031 * Check if socket is valid (no fatal errors have happened so far).
1032 * Note that a socket that is still trying to connect is considered
1035 * @param sock socket to check
1036 * @return GNUNET_YES if valid, GNUNET_NO otherwise
1039 GNUNET_CONNECTION_check (struct GNUNET_CONNECTION_Handle *sock)
1041 if ((sock->ap_head != NULL) || (sock->dns_active != NULL))
1042 return GNUNET_YES; /* still trying to connect */
1043 return (sock->sock == NULL) ? GNUNET_NO : GNUNET_YES;
1048 * Close the socket and free associated resources. Pending
1049 * transmissions may be completed or dropped depending on the
1050 * arguments. If a receive call is pending and should
1051 * NOT be completed, 'GNUNET_CONNECTION_receive_cancel'
1052 * should be called explicitly first.
1054 * @param sock socket to destroy
1055 * @param finish_pending_write should pending writes be completed or aborted?
1056 * (this applies to transmissions where the data has already been
1057 * read from the application; all other transmissions should be
1058 * aborted using 'GNUNET_CONNECTION_notify_transmit_ready_cancel').
1061 GNUNET_CONNECTION_destroy (struct GNUNET_CONNECTION_Handle *sock,
1062 int finish_pending_write)
1064 if (GNUNET_NO == finish_pending_write)
1066 if (sock->write_task != GNUNET_SCHEDULER_NO_TASK)
1068 GNUNET_SCHEDULER_cancel (sock->write_task);
1069 sock->write_task = GNUNET_SCHEDULER_NO_TASK;
1070 sock->write_buffer_off = 0;
1072 sock->nth.notify_ready = NULL;
1074 if ((sock->write_buffer_off == 0) && (sock->dns_active != NULL))
1076 GNUNET_RESOLVER_request_cancel (sock->dns_active);
1077 sock->dns_active = NULL;
1080 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task);
1081 sock->destroy_task = GNUNET_SCHEDULER_add_now (&destroy_continuation, sock);
1086 * Tell the receiver callback that a timeout was reached.
1089 signal_timeout (struct GNUNET_CONNECTION_Handle *sh)
1091 GNUNET_CONNECTION_Receiver receiver;
1093 #if DEBUG_CONNECTION
1094 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1095 "Network signals time out to receiver (%p)!\n", sh);
1097 GNUNET_assert (NULL != (receiver = sh->receiver));
1098 sh->receiver = NULL;
1099 receiver (sh->receiver_cls, NULL, 0, NULL, 0, 0);
1104 * Tell the receiver callback that we had an IO error.
1107 signal_error (struct GNUNET_CONNECTION_Handle *sh, int errcode)
1109 GNUNET_CONNECTION_Receiver receiver;
1111 GNUNET_assert (NULL != (receiver = sh->receiver));
1112 sh->receiver = NULL;
1113 receiver (sh->receiver_cls, NULL, 0, sh->addr, sh->addrlen, errcode);
1118 * This function is called once we either timeout
1119 * or have data ready to read.
1122 receive_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1124 struct GNUNET_CONNECTION_Handle *sh = cls;
1125 struct GNUNET_TIME_Absolute now;
1126 char buffer[sh->max];
1128 GNUNET_CONNECTION_Receiver receiver;
1130 sh->read_task = GNUNET_SCHEDULER_NO_TASK;
1131 if ((GNUNET_YES == sh->ignore_shutdown) &&
1132 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
1134 /* ignore shutdown request, go again immediately */
1135 #if DEBUG_CONNECTION
1136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1137 "Ignoring shutdown signal per configuration\n");
1140 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_absolute_get_remaining
1141 (sh->receive_timeout), sh->sock,
1142 &receive_ready, sh);
1145 now = GNUNET_TIME_absolute_get ();
1146 if ((now.abs_value > sh->receive_timeout.abs_value) ||
1147 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT)) ||
1148 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
1150 #if DEBUG_CONNECTION
1151 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1153 "Receive from `%s' encounters error: time out by %llums... (%p)\n",
1154 GNUNET_a2s (sh->addr, sh->addrlen),
1155 GNUNET_TIME_absolute_get_duration (sh->receive_timeout).
1158 signal_timeout (sh);
1161 if (sh->sock == NULL)
1163 /* connect failed for good */
1164 #if DEBUG_CONNECTION
1165 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1166 "Receive encounters error, socket closed... (%p)\n", sh);
1168 signal_error (sh, ECONNREFUSED);
1171 GNUNET_assert (GNUNET_NETWORK_fdset_isset (tc->read_ready, sh->sock));
1173 ret = GNUNET_NETWORK_socket_recv (sh->sock, buffer, sh->max);
1178 #if DEBUG_CONNECTION
1179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Error receiving: %s\n",
1182 signal_error (sh, errno);
1185 #if DEBUG_CONNECTION
1186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1187 "receive_ready read %u/%u bytes from `%s' (%p)!\n",
1188 (unsigned int) ret, sh->max, GNUNET_a2s (sh->addr, sh->addrlen),
1191 GNUNET_assert (NULL != (receiver = sh->receiver));
1192 sh->receiver = NULL;
1193 receiver (sh->receiver_cls, buffer, ret, sh->addr, sh->addrlen, 0);
1198 * This function is called after establishing a connection either has
1199 * succeeded or timed out. Note that it is possible that the attempt
1200 * timed out and that we're immediately retrying. If we are retrying,
1201 * we need to wait again (or timeout); if we succeeded, we need to
1202 * wait for data (or timeout).
1204 * @param cls our connection handle
1205 * @param tc task context describing why we are here
1208 receive_again (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1210 struct GNUNET_CONNECTION_Handle *sh = cls;
1211 struct GNUNET_TIME_Absolute now;
1213 sh->read_task = GNUNET_SCHEDULER_NO_TASK;
1214 if (sh->sock == NULL)
1216 /* not connected and no longer trying */
1217 #if DEBUG_CONNECTION
1218 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1219 "Receive encounters error, socket closed (%p)...\n", sh);
1221 signal_error (sh, ECONNREFUSED);
1224 now = GNUNET_TIME_absolute_get ();
1225 if ((now.abs_value > sh->receive_timeout.abs_value) ||
1226 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
1228 #if DEBUG_CONNECTION
1229 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1230 "Receive encounters error: time out (%p)...\n", sh);
1232 signal_timeout (sh);
1235 GNUNET_assert (sh->sock != NULL);
1236 /* connect succeeded, wait for data! */
1238 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_absolute_get_remaining
1239 (sh->receive_timeout), sh->sock,
1240 &receive_ready, sh);
1245 * Receive data from the given socket. Note that this function will
1246 * call "receiver" asynchronously using the scheduler. It will
1247 * "immediately" return. Note that there MUST only be one active
1248 * receive call per socket at any given point in time (so do not
1249 * call receive again until the receiver callback has been invoked).
1251 * @param sock socket handle
1252 * @param max maximum number of bytes to read
1253 * @param timeout maximum amount of time to wait (use -1 for "forever")
1254 * @param receiver function to call with received data
1255 * @param receiver_cls closure for receiver
1258 GNUNET_CONNECTION_receive (struct GNUNET_CONNECTION_Handle *sock, size_t max,
1259 struct GNUNET_TIME_Relative timeout,
1260 GNUNET_CONNECTION_Receiver receiver,
1263 struct GNUNET_SCHEDULER_TaskContext tc;
1265 GNUNET_assert ((sock->read_task == GNUNET_SCHEDULER_NO_TASK) &&
1266 (0 == (sock->ccs & COCO_RECEIVE_AGAIN)) &&
1267 (sock->receiver == NULL));
1268 sock->receiver = receiver;
1269 sock->receiver_cls = receiver_cls;
1270 sock->receive_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1272 if (sock->sock != NULL)
1274 memset (&tc, 0, sizeof (tc));
1275 tc.reason = GNUNET_SCHEDULER_REASON_PREREQ_DONE;
1276 receive_again (sock, &tc);
1279 if ((sock->dns_active == NULL) && (sock->ap_head == NULL))
1281 receiver (receiver_cls, NULL, 0, NULL, 0, ETIMEDOUT);
1284 sock->ccs += COCO_RECEIVE_AGAIN;
1289 * Configure this connection to ignore shutdown signals.
1291 * @param sock socket handle
1292 * @param do_ignore GNUNET_YES to ignore, GNUNET_NO to restore default
1295 GNUNET_CONNECTION_ignore_shutdown (struct GNUNET_CONNECTION_Handle *sock,
1298 sock->ignore_shutdown = do_ignore;
1303 * Cancel receive job on the given socket. Note that the
1304 * receiver callback must not have been called yet in order
1305 * for the cancellation to be valid.
1307 * @param sock socket handle
1308 * @return closure of the original receiver callback closure
1311 GNUNET_CONNECTION_receive_cancel (struct GNUNET_CONNECTION_Handle *sock)
1313 if (sock->read_task != GNUNET_SCHEDULER_NO_TASK)
1315 GNUNET_assert (sock == GNUNET_SCHEDULER_cancel (sock->read_task));
1316 sock->read_task = GNUNET_SCHEDULER_NO_TASK;
1320 GNUNET_assert (0 != (sock->ccs & COCO_RECEIVE_AGAIN));
1321 sock->ccs -= COCO_RECEIVE_AGAIN;
1323 sock->receiver = NULL;
1324 return sock->receiver_cls;
1329 * Try to call the transmit notify method (check if we do
1330 * have enough space available first)!
1332 * @param sock socket for which we should do this processing
1333 * @return GNUNET_YES if we were able to call notify
1336 process_notify (struct GNUNET_CONNECTION_Handle *sock)
1341 GNUNET_CONNECTION_TransmitReadyNotify notify;
1343 GNUNET_assert (sock->write_task == GNUNET_SCHEDULER_NO_TASK);
1344 if (NULL == (notify = sock->nth.notify_ready))
1346 used = sock->write_buffer_off - sock->write_buffer_pos;
1347 avail = sock->write_buffer_size - used;
1348 size = sock->nth.notify_size;
1351 sock->nth.notify_ready = NULL;
1352 if (sock->write_buffer_size - sock->write_buffer_off < size)
1354 /* need to compact */
1355 memmove (sock->write_buffer, &sock->write_buffer[sock->write_buffer_pos],
1357 sock->write_buffer_off -= sock->write_buffer_pos;
1358 sock->write_buffer_pos = 0;
1360 avail = sock->write_buffer_size - sock->write_buffer_off;
1361 GNUNET_assert (avail >= size);
1363 notify (sock->nth.notify_ready_cls, avail,
1364 &sock->write_buffer[sock->write_buffer_off]);
1365 GNUNET_assert (size <= avail);
1366 sock->write_buffer_off += size;
1372 * Task invoked by the scheduler when a call to transmit
1373 * is timing out (we never got enough buffer space to call
1374 * the callback function before the specified timeout
1377 * This task notifies the client about the timeout.
1379 * @param cls the 'struct GNUNET_CONNECTION_Handle'
1380 * @param tc scheduler context
1383 transmit_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1385 struct GNUNET_CONNECTION_Handle *sock = cls;
1386 GNUNET_CONNECTION_TransmitReadyNotify notify;
1388 #if DEBUG_CONNECTION
1389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "transmit_timeout running (%p)\n", sock);
1391 sock->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK;
1392 #if DEBUG_CONNECTION
1393 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1394 "Transmit to `%s:%u/%s' fails, time out reached (%p).\n",
1395 sock->hostname, sock->port, GNUNET_a2s (sock->addr,
1396 sock->addrlen), sock);
1398 GNUNET_assert (0 != (sock->ccs & COCO_TRANSMIT_READY));
1399 sock->ccs -= COCO_TRANSMIT_READY; /* remove request */
1400 notify = sock->nth.notify_ready;
1401 sock->nth.notify_ready = NULL;
1402 notify (sock->nth.notify_ready_cls, 0, NULL);
1407 * Task invoked by the scheduler when we failed to connect
1408 * at the time of being asked to transmit.
1410 * This task notifies the client about the error.
1412 * @param cls the 'struct GNUNET_CONNECTION_Handle'
1413 * @param tc scheduler context
1416 connect_error (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1418 struct GNUNET_CONNECTION_Handle *sock = cls;
1419 GNUNET_CONNECTION_TransmitReadyNotify notify;
1421 #if DEBUG_CONNECTION
1422 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1423 "Transmission request of size %u fails (%s/%u), connection failed (%p).\n",
1424 sock->nth.notify_size, sock->hostname, sock->port, sock);
1426 sock->write_task = GNUNET_SCHEDULER_NO_TASK;
1427 notify = sock->nth.notify_ready;
1428 sock->nth.notify_ready = NULL;
1429 notify (sock->nth.notify_ready_cls, 0, NULL);
1439 transmit_error (struct GNUNET_CONNECTION_Handle *sock)
1441 GNUNET_CONNECTION_TransmitReadyNotify notify;
1443 if (NULL != sock->sock)
1445 GNUNET_NETWORK_socket_shutdown (sock->sock, SHUT_RDWR);
1446 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock->sock));
1449 if (sock->read_task != GNUNET_SCHEDULER_NO_TASK)
1451 GNUNET_SCHEDULER_cancel (sock->read_task);
1452 sock->read_task = GNUNET_SCHEDULER_NO_TASK;
1453 signal_timeout (sock);
1456 if (sock->nth.notify_ready == NULL)
1457 return; /* nobody to tell about it */
1458 notify = sock->nth.notify_ready;
1459 sock->nth.notify_ready = NULL;
1460 notify (sock->nth.notify_ready_cls, 0, NULL);
1465 * See if we are now connected. If not, wait longer for
1466 * connect to succeed. If connected, we should be able
1467 * to write now as well, unless we timed out.
1469 * @param cls our connection handle
1470 * @param tc task context describing why we are here
1473 transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1475 struct GNUNET_CONNECTION_Handle *sock = cls;
1476 GNUNET_CONNECTION_TransmitReadyNotify notify;
1480 #if DEBUG_CONNECTION
1481 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "transmit_ready running (%p).\n", sock);
1483 GNUNET_assert (sock->write_task != GNUNET_SCHEDULER_NO_TASK);
1484 sock->write_task = GNUNET_SCHEDULER_NO_TASK;
1485 GNUNET_assert (sock->nth.timeout_task == GNUNET_SCHEDULER_NO_TASK);
1486 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1488 if (sock->ignore_shutdown == GNUNET_YES)
1489 goto SCHEDULE_WRITE; /* ignore shutdown, go again immediately */
1490 #if DEBUG_CONNECTION
1491 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1492 "Transmit to `%s' fails, shutdown happened (%p).\n",
1493 GNUNET_a2s (sock->addr, sock->addrlen), sock);
1495 notify = sock->nth.notify_ready;
1498 sock->nth.notify_ready = NULL;
1499 notify (sock->nth.notify_ready_cls, 0, NULL);
1503 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT))
1505 #if DEBUG_CONNECTION
1506 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1507 "Transmit to `%s' fails, time out reached (%p).\n",
1508 GNUNET_a2s (sock->addr, sock->addrlen), sock);
1510 notify = sock->nth.notify_ready;
1511 GNUNET_assert (NULL != notify);
1512 sock->nth.notify_ready = NULL;
1513 notify (sock->nth.notify_ready_cls, 0, NULL);
1516 GNUNET_assert (NULL != sock->sock);
1517 if (tc->write_ready == NULL)
1519 /* special circumstances (in particular,
1520 * PREREQ_DONE after connect): not yet ready to write,
1521 * but no "fatal" error either. Hence retry. */
1522 goto SCHEDULE_WRITE;
1524 if (!GNUNET_NETWORK_fdset_isset (tc->write_ready, sock->sock))
1526 #if DEBUG_CONNECTION
1527 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1529 ("Could not satisfy pending transmission request, socket closed or connect failed (%p).\n"),
1532 transmit_error (sock);
1533 return; /* connect failed for good, we're finished */
1535 GNUNET_assert (sock->write_buffer_off >= sock->write_buffer_pos);
1536 if ((sock->nth.notify_ready != NULL) &&
1537 (sock->write_buffer_size < sock->nth.notify_size))
1539 sock->write_buffer =
1540 GNUNET_realloc (sock->write_buffer, sock->nth.notify_size);
1541 sock->write_buffer_size = sock->nth.notify_size;
1543 process_notify (sock);
1544 have = sock->write_buffer_off - sock->write_buffer_pos;
1547 /* no data ready for writing, terminate write loop */
1550 GNUNET_assert (have <= sock->write_buffer_size);
1551 GNUNET_assert (have + sock->write_buffer_pos <= sock->write_buffer_size);
1552 GNUNET_assert (sock->write_buffer_pos <= sock->write_buffer_size);
1555 GNUNET_NETWORK_socket_send (sock->sock,
1556 &sock->write_buffer[sock->write_buffer_pos],
1565 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to send to `%s': %s\n"),
1566 GNUNET_a2s (sock->addr, sock->addrlen), STRERROR (en));
1568 #if DEBUG_CONNECTION
1569 GNUNET_log_strerror (GNUNET_ERROR_TYPE_DEBUG, "send");
1571 transmit_error (sock);
1574 #if DEBUG_CONNECTION
1575 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1576 "transmit_ready transmitted %u/%u bytes to `%s' (%p)\n",
1577 (unsigned int) ret, have, GNUNET_a2s (sock->addr, sock->addrlen),
1580 sock->write_buffer_pos += ret;
1581 if (sock->write_buffer_pos == sock->write_buffer_off)
1583 /* transmitted all pending data */
1584 sock->write_buffer_pos = 0;
1585 sock->write_buffer_off = 0;
1587 if ((sock->write_buffer_off == 0) && (NULL == sock->nth.notify_ready))
1588 return; /* all data sent! */
1589 /* not done writing, schedule more */
1591 #if DEBUG_CONNECTION
1592 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1593 "Re-scheduling transmit_ready (more to do) (%p).\n", sock);
1595 have = sock->write_buffer_off - sock->write_buffer_pos;
1596 GNUNET_assert ((sock->nth.notify_ready != NULL) || (have > 0));
1597 if (sock->write_task == GNUNET_SCHEDULER_NO_TASK)
1599 GNUNET_SCHEDULER_add_write_net ((sock->nth.notify_ready ==
1600 NULL) ? GNUNET_TIME_UNIT_FOREVER_REL :
1601 GNUNET_TIME_absolute_get_remaining
1602 (sock->nth.transmit_timeout),
1603 sock->sock, &transmit_ready, sock);
1608 * Ask the socket to call us once the specified number of bytes
1609 * are free in the transmission buffer. May call the notify
1610 * method immediately if enough space is available.
1612 * @param sock socket
1613 * @param size number of bytes to send
1614 * @param timeout after how long should we give up (and call
1615 * notify with buf NULL and size 0)?
1616 * @param notify function to call
1617 * @param notify_cls closure for notify
1618 * @return non-NULL if the notify callback was queued,
1619 * NULL if we are already going to notify someone else (busy)
1621 struct GNUNET_CONNECTION_TransmitHandle *
1622 GNUNET_CONNECTION_notify_transmit_ready (struct GNUNET_CONNECTION_Handle *sock,
1624 struct GNUNET_TIME_Relative timeout,
1625 GNUNET_CONNECTION_TransmitReadyNotify
1626 notify, void *notify_cls)
1628 if (sock->nth.notify_ready != NULL)
1633 GNUNET_assert (notify != NULL);
1634 GNUNET_assert (size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
1635 GNUNET_assert (sock->write_buffer_off <= sock->write_buffer_size);
1636 GNUNET_assert (sock->write_buffer_pos <= sock->write_buffer_size);
1637 GNUNET_assert (sock->write_buffer_pos <= sock->write_buffer_off);
1638 sock->nth.notify_ready = notify;
1639 sock->nth.notify_ready_cls = notify_cls;
1640 sock->nth.sh = sock;
1641 sock->nth.notify_size = size;
1642 sock->nth.transmit_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1643 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->nth.timeout_task);
1644 if ((sock->sock == NULL) && (sock->ap_head == NULL) &&
1645 (sock->dns_active == NULL))
1647 if (sock->write_task != GNUNET_SCHEDULER_NO_TASK)
1648 GNUNET_SCHEDULER_cancel (sock->write_task);
1649 sock->write_task = GNUNET_SCHEDULER_add_now (&connect_error, sock);
1652 if (GNUNET_SCHEDULER_NO_TASK != sock->write_task)
1654 if (sock->sock != NULL)
1656 #if DEBUG_CONNECTION
1657 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Scheduling transmit_ready (%p).\n",
1661 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_absolute_get_remaining
1662 (sock->nth.transmit_timeout),
1663 sock->sock, &transmit_ready, sock);
1667 #if DEBUG_CONNECTION
1668 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1669 "CCS-Scheduling transmit_ready, adding timeout task (%p).\n",
1672 sock->ccs |= COCO_TRANSMIT_READY;
1673 sock->nth.timeout_task =
1674 GNUNET_SCHEDULER_add_delayed (timeout, &transmit_timeout, sock);
1681 * Cancel the specified transmission-ready notification.
1683 * @param th notification to cancel
1686 GNUNET_CONNECTION_notify_transmit_ready_cancel (struct
1687 GNUNET_CONNECTION_TransmitHandle
1690 GNUNET_assert (th->notify_ready != NULL);
1691 if (0 != (th->sh->ccs & COCO_TRANSMIT_READY))
1693 #if DEBUG_CONNECTION
1694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1695 "notify_transmit_ready_cancel cancels timeout_task (%p)\n", th);
1697 GNUNET_SCHEDULER_cancel (th->timeout_task);
1698 th->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1699 th->sh->ccs -= COCO_TRANSMIT_READY;
1703 if (th->sh->write_task != GNUNET_SCHEDULER_NO_TASK)
1705 GNUNET_SCHEDULER_cancel (th->sh->write_task);
1706 th->sh->write_task = GNUNET_SCHEDULER_NO_TASK;
1709 th->notify_ready = NULL;
1712 /* end of connection.c */