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_EXTRA_LOGGING
44 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
46 #define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall)
49 * Possible functions to call after connect failed or succeeded.
51 enum ConnectContinuations
59 * Call "receive_again".
61 COCO_RECEIVE_AGAIN = 1,
64 * Call "transmit_ready".
66 COCO_TRANSMIT_READY = 2,
69 * Call "destroy_continuation".
71 COCO_DESTROY_CONTINUATION = 4
76 * Transmission handle. There can only be one for each connection.
78 struct GNUNET_CONNECTION_TransmitHandle
82 * Function to call if the send buffer has notify_size
85 GNUNET_CONNECTION_TransmitReadyNotify notify_ready;
88 * Closure for notify_ready.
90 void *notify_ready_cls;
95 struct GNUNET_CONNECTION_Handle *sh;
98 * Timeout for receiving (in absolute time).
100 struct GNUNET_TIME_Absolute transmit_timeout;
103 * Task called on timeout.
105 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
108 * At what number of bytes available in the
109 * write buffer should the notify method be called?
117 * During connect, we try multiple possible IP addresses
118 * to find out which one might work.
124 * This is a linked list.
126 struct AddressProbe *next;
129 * This is a doubly-linked list.
131 struct AddressProbe *prev;
134 * The address; do not free (allocated at the end of this struct).
136 const struct sockaddr *addr;
139 * Underlying OS's socket.
141 struct GNUNET_NETWORK_Handle *sock;
144 * Connection for which we are probing.
146 struct GNUNET_CONNECTION_Handle *h;
154 * Task waiting for the socket to finish connecting.
156 GNUNET_SCHEDULER_TaskIdentifier task;
161 * @brief handle for a network socket
163 struct GNUNET_CONNECTION_Handle
167 * Configuration to use.
169 const struct GNUNET_CONFIGURATION_Handle *cfg;
172 * Linked list of sockets we are currently trying out
175 struct AddressProbe *ap_head;
178 * Linked list of sockets we are currently trying out
181 struct AddressProbe *ap_tail;
184 * Network address of the other end-point, may be NULL.
186 struct sockaddr *addr;
189 * Pointer to the hostname if socket was
190 * created using DNS lookup, otherwise NULL.
195 * Underlying OS's socket, set to NULL after fatal errors.
197 struct GNUNET_NETWORK_Handle *sock;
200 * Function to call on data received, NULL if no receive is pending.
202 GNUNET_CONNECTION_Receiver receiver;
205 * Closure for receiver.
210 * Pointer to our write buffer.
215 * Current size of our write buffer.
217 size_t write_buffer_size;
220 * Current write-offset in write buffer (where
221 * would we write next).
223 size_t write_buffer_off;
226 * Current read-offset in write buffer (how many
227 * bytes have already been sent).
229 size_t write_buffer_pos;
237 * Read task that we may need to wait for.
239 GNUNET_SCHEDULER_TaskIdentifier read_task;
242 * Write task that we may need to wait for.
244 GNUNET_SCHEDULER_TaskIdentifier write_task;
247 * Destroy task (if already scheduled).
249 GNUNET_SCHEDULER_TaskIdentifier destroy_task;
252 * Handle to a pending DNS lookup request.
254 struct GNUNET_RESOLVER_RequestHandle *dns_active;
257 * The handle we return for GNUNET_CONNECTION_notify_transmit_ready.
259 struct GNUNET_CONNECTION_TransmitHandle nth;
262 * Timeout for receiving (in absolute time).
264 struct GNUNET_TIME_Absolute receive_timeout;
267 * Functions to call after connect failed or succeeded.
269 enum ConnectContinuations ccs;
272 * Maximum number of bytes to read (for receiving).
277 * Ignore GNUNET_SCHEDULER_REASON_SHUTDOWN for this socket.
282 * Port to connect to.
287 * When shutdown, do not ever actually close the socket, but
288 * free resources. Only should ever be set if using program
289 * termination as a signal (because only then will the leaked
297 * Set the persist option on this connection handle. Indicates
298 * that the underlying socket or fd should never really be closed.
299 * Used for indicating process death.
301 * @param sock the connection to set persistent
304 GNUNET_CONNECTION_persist_ (struct GNUNET_CONNECTION_Handle *sock)
306 sock->persist = GNUNET_YES;
311 * Disable the "CORK" feature for communication with the given socket,
312 * forcing the OS to immediately flush the buffer on transmission
313 * instead of potentially buffering multiple messages. Essentially
314 * reduces the OS send buffers to zero.
315 * Used to make sure that the last messages sent through the connection
316 * reach the other side before the process is terminated.
318 * @param sock the connection to make flushing and blocking
319 * @return GNUNET_OK on success
322 GNUNET_CONNECTION_disable_corking (struct GNUNET_CONNECTION_Handle *sock)
324 return GNUNET_NETWORK_socket_disable_corking (sock->sock);
328 * Create a socket handle by boxing an existing OS socket. The OS
329 * socket should henceforth be no longer used directly.
330 * GNUNET_socket_destroy will close it.
332 * @param osSocket existing socket to box
333 * @return the boxed socket handle
335 struct GNUNET_CONNECTION_Handle *
336 GNUNET_CONNECTION_create_from_existing (struct GNUNET_NETWORK_Handle *osSocket)
338 struct GNUNET_CONNECTION_Handle *ret;
340 ret = GNUNET_malloc (sizeof (struct GNUNET_CONNECTION_Handle));
341 ret->write_buffer_size = GNUNET_SERVER_MIN_BUFFER_SIZE;
342 ret->write_buffer = GNUNET_malloc (ret->write_buffer_size);
343 ret->sock = osSocket;
349 * Create a socket handle by accepting on a listen socket. This
350 * function may block if the listen socket has no connection ready.
352 * @param access function to use to check if access is allowed
353 * @param access_cls closure for access
354 * @param lsock listen socket
355 * @return the socket handle, NULL on error
357 struct GNUNET_CONNECTION_Handle *
358 GNUNET_CONNECTION_create_from_accept (GNUNET_CONNECTION_AccessCheck access,
360 struct GNUNET_NETWORK_Handle *lsock)
362 struct GNUNET_CONNECTION_Handle *ret;
365 struct GNUNET_NETWORK_Handle *sock;
367 struct sockaddr_in *v4;
368 struct sockaddr_in6 *v6;
371 struct GNUNET_CONNECTION_Credentials *gcp;
372 struct GNUNET_CONNECTION_Credentials gc;
379 addrlen = sizeof (addr);
381 GNUNET_NETWORK_socket_accept (lsock, (struct sockaddr *) &addr, &addrlen);
384 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "accept");
387 if ((addrlen > sizeof (addr)) || (addrlen < sizeof (sa_family_t)))
390 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
394 sa = (struct sockaddr *) addr;
395 v6 = (struct sockaddr_in6 *) addr;
396 if ((sa->sa_family == AF_INET6) && (IN6_IS_ADDR_V4MAPPED (&v6->sin6_addr)))
398 /* convert to V4 address */
399 v4 = GNUNET_malloc (sizeof (struct sockaddr_in));
400 memset (v4, 0, sizeof (struct sockaddr_in));
401 v4->sin_family = AF_INET;
402 #if HAVE_SOCKADDR_IN_SIN_LEN
403 v4->sin_len = (u_char) sizeof (struct sockaddr_in);
405 memcpy (&v4->sin_addr,
406 &((char *) &v6->sin6_addr)[sizeof (struct in6_addr) -
407 sizeof (struct in_addr)],
408 sizeof (struct in_addr));
409 v4->sin_port = v6->sin6_port;
411 addrlen = sizeof (struct sockaddr_in);
415 uaddr = GNUNET_malloc (addrlen);
416 memcpy (uaddr, addr, addrlen);
421 if (sa->sa_family == AF_UNIX)
425 if (0 == getpeereid (GNUNET_NETWORK_get_fd (sock), &gc.uid, &gc.gid))
429 /* largely traditional GNU/Linux */
432 getsockopt (GNUNET_NETWORK_get_fd (sock), SOL_SOCKET, SO_PEERCRED, &uc,
433 &olen)) && (olen == sizeof (uc)))
440 #if HAVE_GETPEERUCRED
441 /* this is for Solaris 10 */
445 if (0 == getpeerucred (GNUNET_NETWORK_get_fd (sock), &uc))
447 gc.uid = ucred_geteuid (uc);
448 gc.gid = ucred_getegid (uc);
457 if ((access != NULL) &&
458 (GNUNET_YES != (aret = access (access_cls, gcp, uaddr, addrlen))))
460 if (aret == GNUNET_NO)
461 LOG (GNUNET_ERROR_TYPE_INFO, _("Access denied to `%s'\n"),
462 GNUNET_a2s (uaddr, addrlen));
463 GNUNET_break (GNUNET_OK ==
464 GNUNET_NETWORK_socket_shutdown (sock, SHUT_RDWR));
465 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
469 ret = GNUNET_malloc (sizeof (struct GNUNET_CONNECTION_Handle));
470 ret->write_buffer_size = GNUNET_SERVER_MIN_BUFFER_SIZE;
471 ret->write_buffer = GNUNET_malloc (ret->write_buffer_size);
473 ret->addrlen = addrlen;
476 LOG (GNUNET_ERROR_TYPE_INFO, _("Accepting connection from `%s': %p\n"),
477 GNUNET_a2s (uaddr, addrlen), ret);
483 * Obtain the network address of the other party.
485 * @param sock the client to get the address for
486 * @param addr where to store the address
487 * @param addrlen where to store the length of the address
488 * @return GNUNET_OK on success
491 GNUNET_CONNECTION_get_address (struct GNUNET_CONNECTION_Handle *sock,
492 void **addr, size_t * addrlen)
494 if ((sock->addr == NULL) || (sock->addrlen == 0))
496 *addr = GNUNET_malloc (sock->addrlen);
497 memcpy (*addr, sock->addr, sock->addrlen);
498 *addrlen = sock->addrlen;
504 * This function is called after establishing a connection either has
505 * succeeded or timed out. Note that it is possible that the attempt
506 * timed out and that we're immediately retrying. If we are retrying,
507 * we need to wait again (or timeout); if we succeeded, we need to
508 * wait for data (or timeout).
510 * @param cls our connection handle
511 * @param tc task context describing why we are here
514 receive_again (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
518 * Scheduler let us know that the connect task is finished (or was
519 * cancelled due to shutdown). Now really clean up.
521 * @param cls our "struct GNUNET_CONNECTION_Handle *"
525 destroy_continuation (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
527 struct GNUNET_CONNECTION_Handle *sock = cls;
528 GNUNET_CONNECTION_TransmitReadyNotify notify;
529 struct AddressProbe *pos;
531 sock->destroy_task = GNUNET_SCHEDULER_NO_TASK;
532 GNUNET_assert (sock->dns_active == NULL);
533 if (0 != (sock->ccs & COCO_TRANSMIT_READY))
536 LOG (GNUNET_ERROR_TYPE_DEBUG, "Destroy waits for CCS-TR to be done (%p)\n",
539 sock->ccs |= COCO_DESTROY_CONTINUATION;
542 if (sock->write_task != GNUNET_SCHEDULER_NO_TASK)
545 LOG (GNUNET_ERROR_TYPE_DEBUG,
546 "Destroy waits for write_task to be done (%p)\n", sock);
548 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task);
550 GNUNET_SCHEDULER_add_after (sock->write_task, &destroy_continuation,
554 if (0 != (sock->ccs & COCO_RECEIVE_AGAIN))
556 sock->ccs |= COCO_DESTROY_CONTINUATION;
559 if (sock->sock != NULL)
562 LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down socket (%p)\n", sock);
564 if (sock->persist != GNUNET_YES)
566 if ((GNUNET_YES != GNUNET_NETWORK_socket_shutdown (sock->sock, SHUT_RDWR))
567 && (errno != ENOTCONN) && (errno != ECONNRESET))
568 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "shutdown");
571 if (sock->read_task != GNUNET_SCHEDULER_NO_TASK)
573 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task);
575 GNUNET_SCHEDULER_add_after (sock->read_task, &destroy_continuation,
580 LOG (GNUNET_ERROR_TYPE_DEBUG, "Destroy actually runs (%p)!\n", sock);
582 while (NULL != (pos = sock->ap_head))
584 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (pos->sock));
585 GNUNET_SCHEDULER_cancel (pos->task);
586 GNUNET_CONTAINER_DLL_remove (sock->ap_head, sock->ap_tail, pos);
589 GNUNET_assert (sock->nth.timeout_task == GNUNET_SCHEDULER_NO_TASK);
590 GNUNET_assert (sock->ccs == COCO_NONE);
591 if (NULL != (notify = sock->nth.notify_ready))
593 sock->nth.notify_ready = NULL;
594 notify (sock->nth.notify_ready_cls, 0, NULL);
597 if (sock->sock != NULL)
599 if (sock->persist != GNUNET_YES)
600 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock->sock));
602 GNUNET_free (sock->sock); /* at least no memory leak (we deliberately
603 * leak the socket in this special case) ... */
605 GNUNET_free_non_null (sock->addr);
606 GNUNET_free_non_null (sock->hostname);
607 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task);
609 LOG (GNUNET_ERROR_TYPE_DEBUG, "Freeing memory of connection %p.\n", sock);
611 GNUNET_free (sock->write_buffer);
618 * See if we are now connected. If not, wait longer for
619 * connect to succeed. If connected, we should be able
620 * to write now as well, unless we timed out.
622 * @param cls our connection handle
623 * @param tc task context describing why we are here
626 transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
630 * We've failed for good to establish a connection.
632 * @param h the connection we tried to establish
635 connect_fail_continuation (struct GNUNET_CONNECTION_Handle *h)
639 strncmp (h->hostname, "localhost:",
640 10)) ? GNUNET_ERROR_TYPE_INFO : GNUNET_ERROR_TYPE_WARNING,
642 ("Failed to establish TCP connection to `%s:%u', no further addresses to try.\n"),
643 h->hostname, h->port);
645 /* connect failed / timed out */
646 GNUNET_break (h->ap_head == NULL);
647 GNUNET_break (h->ap_tail == NULL);
648 GNUNET_break (h->dns_active == GNUNET_NO);
649 GNUNET_break (h->sock == NULL);
651 /* trigger jobs that used to wait on "connect_task" */
652 if (0 != (h->ccs & COCO_RECEIVE_AGAIN))
655 LOG (GNUNET_ERROR_TYPE_DEBUG,
656 "connect_fail_continuation triggers receive_again (%p)\n", h);
658 h->ccs -= COCO_RECEIVE_AGAIN;
659 h->read_task = GNUNET_SCHEDULER_add_now (&receive_again, h);
661 if (0 != (h->ccs & COCO_TRANSMIT_READY))
664 LOG (GNUNET_ERROR_TYPE_DEBUG,
665 "connect_fail_continuation cancels timeout_task, triggers transmit_ready (%p)\n",
668 GNUNET_assert (h->nth.timeout_task != GNUNET_SCHEDULER_NO_TASK);
669 GNUNET_SCHEDULER_cancel (h->nth.timeout_task);
670 h->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK;
671 h->ccs -= COCO_TRANSMIT_READY;
672 GNUNET_assert (h->nth.notify_ready != NULL);
673 GNUNET_assert (h->write_task == GNUNET_SCHEDULER_NO_TASK);
674 h->write_task = GNUNET_SCHEDULER_add_now (&transmit_ready, h);
676 if (0 != (h->ccs & COCO_DESTROY_CONTINUATION))
679 LOG (GNUNET_ERROR_TYPE_DEBUG,
680 "connect_fail_continuation runs destroy_continuation (%p)\n", h);
682 h->ccs -= COCO_DESTROY_CONTINUATION;
683 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == h->destroy_task);
684 h->destroy_task = GNUNET_SCHEDULER_add_now (&destroy_continuation, h);
690 * We've succeeded in establishing a connection.
692 * @param h the connection we tried to establish
695 connect_success_continuation (struct GNUNET_CONNECTION_Handle *h)
698 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection to `%s' succeeded! (%p)\n",
699 GNUNET_a2s (h->addr, h->addrlen), h);
701 /* trigger jobs that waited for the connection */
702 if (0 != (h->ccs & COCO_RECEIVE_AGAIN))
705 LOG (GNUNET_ERROR_TYPE_DEBUG,
706 "connect_success_continuation runs receive_again (%p)\n", h);
708 h->ccs -= COCO_RECEIVE_AGAIN;
709 h->read_task = GNUNET_SCHEDULER_add_now (&receive_again, h);
711 if (0 != (h->ccs & COCO_TRANSMIT_READY))
714 LOG (GNUNET_ERROR_TYPE_DEBUG,
715 "connect_success_continuation runs transmit_ready, cancels timeout_task (%p)\n",
718 GNUNET_assert (h->nth.timeout_task != GNUNET_SCHEDULER_NO_TASK);
719 GNUNET_SCHEDULER_cancel (h->nth.timeout_task);
720 h->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK;
721 h->ccs -= COCO_TRANSMIT_READY;
722 GNUNET_assert (h->write_task == GNUNET_SCHEDULER_NO_TASK);
723 GNUNET_assert (h->nth.notify_ready != NULL);
725 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_absolute_get_remaining
726 (h->nth.transmit_timeout), h->sock,
729 if (0 != (h->ccs & COCO_DESTROY_CONTINUATION))
732 LOG (GNUNET_ERROR_TYPE_DEBUG,
733 "connect_success_continuation runs destroy_continuation (%p)\n", h);
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) && (NULL == h->sock))
814 connect_fail_continuation (h);
818 return; /* already connected */
819 GNUNET_assert (h->addr == NULL);
822 LOG (GNUNET_ERROR_TYPE_DEBUG,
823 "Trying to connect using address `%s:%u/%s:%u'\n", h->hostname, h->port,
824 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 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 LOG_STRERROR (GNUNET_ERROR_TYPE_INFO, "connect");
862 LOG (GNUNET_ERROR_TYPE_INFO, _("Failed to connect to `%s' (%p)\n"),
863 GNUNET_a2s (ap->addr, ap->addrlen), h);
865 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ap->sock));
869 GNUNET_CONTAINER_DLL_insert (h->ap_head, h->ap_tail, ap);
870 delay = GNUNET_CONNECTION_CONNECT_RETRY_TIMEOUT;
871 if (h->nth.notify_ready != NULL)
873 GNUNET_TIME_relative_min (delay,
874 GNUNET_TIME_absolute_get_remaining (h->
875 nth.transmit_timeout));
876 if (h->receiver != NULL)
878 GNUNET_TIME_relative_min (delay,
879 GNUNET_TIME_absolute_get_remaining
880 (h->receive_timeout));
882 GNUNET_SCHEDULER_add_write_net (delay, ap->sock,
883 &connect_probe_continuation, ap);
888 * Create a socket handle by (asynchronously) connecting to a host.
889 * This function returns immediately, even if the connection has not
890 * yet been established. This function only creates TCP connections.
892 * @param cfg configuration to use
893 * @param hostname name of the host to connect to
894 * @param port port to connect to
895 * @return the socket handle
897 struct GNUNET_CONNECTION_Handle *
898 GNUNET_CONNECTION_create_from_connect (const struct GNUNET_CONFIGURATION_Handle
899 *cfg, const char *hostname,
902 struct GNUNET_CONNECTION_Handle *ret;
904 GNUNET_assert (0 < strlen (hostname)); /* sanity check */
905 ret = GNUNET_malloc (sizeof (struct GNUNET_CONNECTION_Handle));
907 ret->write_buffer_size = GNUNET_SERVER_MIN_BUFFER_SIZE;
908 ret->write_buffer = GNUNET_malloc (ret->write_buffer_size);
910 ret->hostname = GNUNET_strdup (hostname);
912 GNUNET_RESOLVER_ip_get (ret->hostname, AF_UNSPEC,
913 GNUNET_CONNECTION_CONNECT_RETRY_TIMEOUT,
914 &try_connect_using_address, ret);
920 * Create a socket handle by connecting to a UNIX domain service.
921 * This function returns immediately, even if the connection has not
922 * yet been established. This function only creates UNIX connections.
924 * @param cfg configuration to use
925 * @param unixpath path to connect to
926 * @return the socket handle, NULL on systems without UNIX support
928 struct GNUNET_CONNECTION_Handle *
929 GNUNET_CONNECTION_create_from_connect_to_unixpath (const struct
930 GNUNET_CONFIGURATION_Handle
931 *cfg, const char *unixpath)
934 struct GNUNET_CONNECTION_Handle *ret;
935 struct sockaddr_un *un;
938 GNUNET_assert (0 < strlen (unixpath)); /* sanity check */
939 un = GNUNET_malloc (sizeof (struct sockaddr_un));
940 un->sun_family = AF_UNIX;
941 slen = strlen (unixpath);
942 if (slen >= sizeof (un->sun_path))
943 slen = sizeof (un->sun_path) - 1;
944 memcpy (un->sun_path, unixpath, slen);
945 un->sun_path[slen] = '\0';
946 slen = sizeof (struct sockaddr_un);
947 #if HAVE_SOCKADDR_IN_SIN_LEN
948 un->sun_len = (u_char) slen;
951 un->sun_path[0] = '\0';
953 ret = GNUNET_malloc (sizeof (struct GNUNET_CONNECTION_Handle));
955 ret->write_buffer_size = GNUNET_SERVER_MIN_BUFFER_SIZE;
956 ret->write_buffer = GNUNET_malloc (ret->write_buffer_size);
958 ret->hostname = NULL;
959 ret->addr = (struct sockaddr *) un;
961 ret->sock = GNUNET_NETWORK_socket_create (AF_UNIX, SOCK_STREAM, 0);
962 if (NULL == ret->sock)
964 GNUNET_free (ret->addr);
965 GNUNET_free (ret->write_buffer);
970 GNUNET_NETWORK_socket_connect (ret->sock, ret->addr, ret->addrlen))
972 /* Just return; we expect everything to work eventually so don't fail HARD */
973 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret->sock));
977 connect_success_continuation (ret);
986 * Create a socket handle by (asynchronously) connecting to a host.
987 * This function returns immediately, even if the connection has not
988 * yet been established. This function only creates TCP connections.
990 * @param af_family address family to use
991 * @param serv_addr server address
992 * @param addrlen length of server address
993 * @return the socket handle
995 struct GNUNET_CONNECTION_Handle *
996 GNUNET_CONNECTION_create_from_sockaddr (int af_family,
997 const struct sockaddr *serv_addr,
1000 struct GNUNET_NETWORK_Handle *s;
1001 struct GNUNET_CONNECTION_Handle *ret;
1004 s = GNUNET_NETWORK_socket_create (af_family, SOCK_STREAM, 0);
1007 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK, "socket");
1010 if ((GNUNET_OK != GNUNET_NETWORK_socket_connect (s, serv_addr, addrlen)) &&
1011 (errno != EINPROGRESS))
1013 /* maybe refused / unsupported address, try next */
1014 LOG_STRERROR (GNUNET_ERROR_TYPE_INFO, "connect");
1015 LOG (GNUNET_ERROR_TYPE_INFO, _("Attempt to connect to `%s' failed\n"),
1016 GNUNET_a2s (serv_addr, addrlen));
1017 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (s));
1020 ret = GNUNET_CONNECTION_create_from_existing (s);
1021 ret->addr = GNUNET_malloc (addrlen);
1022 memcpy (ret->addr, serv_addr, addrlen);
1023 ret->addrlen = addrlen;
1024 #if DEBUG_CONNECTION
1025 LOG (GNUNET_ERROR_TYPE_INFO, _("Trying to connect to `%s' (%p)\n"),
1026 GNUNET_a2s (serv_addr, addrlen), ret);
1033 * Check if socket is valid (no fatal errors have happened so far).
1034 * Note that a socket that is still trying to connect is considered
1037 * @param sock socket to check
1038 * @return GNUNET_YES if valid, GNUNET_NO otherwise
1041 GNUNET_CONNECTION_check (struct GNUNET_CONNECTION_Handle *sock)
1043 if ((sock->ap_head != NULL) || (sock->dns_active != NULL))
1044 return GNUNET_YES; /* still trying to connect */
1045 return (sock->sock == NULL) ? GNUNET_NO : GNUNET_YES;
1050 * Close the socket and free associated resources. Pending
1051 * transmissions may be completed or dropped depending on the
1052 * arguments. If a receive call is pending and should
1053 * NOT be completed, 'GNUNET_CONNECTION_receive_cancel'
1054 * should be called explicitly first.
1056 * @param sock socket to destroy
1057 * @param finish_pending_write should pending writes be completed or aborted?
1058 * (this applies to transmissions where the data has already been
1059 * read from the application; all other transmissions should be
1060 * aborted using 'GNUNET_CONNECTION_notify_transmit_ready_cancel').
1063 GNUNET_CONNECTION_destroy (struct GNUNET_CONNECTION_Handle *sock,
1064 int finish_pending_write)
1066 if (GNUNET_NO == finish_pending_write)
1068 if (sock->write_task != GNUNET_SCHEDULER_NO_TASK)
1070 GNUNET_SCHEDULER_cancel (sock->write_task);
1071 sock->write_task = GNUNET_SCHEDULER_NO_TASK;
1072 sock->write_buffer_off = 0;
1074 sock->nth.notify_ready = NULL;
1076 if ((sock->write_buffer_off == 0) && (sock->dns_active != NULL))
1078 GNUNET_RESOLVER_request_cancel (sock->dns_active);
1079 sock->dns_active = NULL;
1082 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task);
1083 sock->destroy_task = GNUNET_SCHEDULER_add_now (&destroy_continuation, sock);
1088 * Tell the receiver callback that a timeout was reached.
1091 signal_timeout (struct GNUNET_CONNECTION_Handle *sh)
1093 GNUNET_CONNECTION_Receiver receiver;
1095 #if DEBUG_CONNECTION
1096 LOG (GNUNET_ERROR_TYPE_DEBUG, "Network signals time out to receiver (%p)!\n",
1099 GNUNET_assert (NULL != (receiver = sh->receiver));
1100 sh->receiver = NULL;
1101 receiver (sh->receiver_cls, NULL, 0, NULL, 0, 0);
1106 * Tell the receiver callback that we had an IO error.
1109 signal_error (struct GNUNET_CONNECTION_Handle *sh, int errcode)
1111 GNUNET_CONNECTION_Receiver receiver;
1113 GNUNET_assert (NULL != (receiver = sh->receiver));
1114 sh->receiver = NULL;
1115 receiver (sh->receiver_cls, NULL, 0, sh->addr, sh->addrlen, errcode);
1120 * This function is called once we either timeout
1121 * or have data ready to read.
1124 receive_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1126 struct GNUNET_CONNECTION_Handle *sh = cls;
1127 struct GNUNET_TIME_Absolute now;
1128 char buffer[sh->max];
1130 GNUNET_CONNECTION_Receiver receiver;
1132 sh->read_task = GNUNET_SCHEDULER_NO_TASK;
1133 if ((GNUNET_YES == sh->ignore_shutdown) &&
1134 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
1136 /* ignore shutdown request, go again immediately */
1137 #if DEBUG_CONNECTION
1138 LOG (GNUNET_ERROR_TYPE_DEBUG,
1139 "Ignoring shutdown signal per configuration\n");
1142 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_absolute_get_remaining
1143 (sh->receive_timeout), sh->sock,
1144 &receive_ready, sh);
1147 now = GNUNET_TIME_absolute_get ();
1148 if ((now.abs_value > sh->receive_timeout.abs_value) ||
1149 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT)) ||
1150 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
1152 #if DEBUG_CONNECTION
1153 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1154 LOG (GNUNET_ERROR_TYPE_DEBUG,
1155 "Receive from `%s' encounters error: time out by %llums... (%p)\n",
1156 GNUNET_a2s (sh->addr, sh->addrlen),
1157 GNUNET_TIME_absolute_get_duration (sh->receive_timeout).rel_value,
1160 signal_timeout (sh);
1163 if (sh->sock == NULL)
1165 /* connect failed for good */
1166 #if DEBUG_CONNECTION
1167 LOG (GNUNET_ERROR_TYPE_DEBUG,
1168 "Receive encounters error, socket closed... (%p)\n", sh);
1170 signal_error (sh, ECONNREFUSED);
1173 GNUNET_assert (GNUNET_NETWORK_fdset_isset (tc->read_ready, sh->sock));
1175 ret = GNUNET_NETWORK_socket_recv (sh->sock, buffer, sh->max);
1180 #if DEBUG_CONNECTION
1181 LOG (GNUNET_ERROR_TYPE_DEBUG, "Error receiving: %s\n", STRERROR (errno));
1183 signal_error (sh, errno);
1186 #if DEBUG_CONNECTION
1187 LOG (GNUNET_ERROR_TYPE_DEBUG,
1188 "receive_ready read %u/%u bytes from `%s' (%p)!\n", (unsigned int) ret,
1189 sh->max, GNUNET_a2s (sh->addr, sh->addrlen), sh);
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 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 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 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 LOG (GNUNET_ERROR_TYPE_DEBUG,
1394 "Transmit to `%s:%u/%s' fails, time out reached (%p).\n", sock->hostname,
1395 sock->port, GNUNET_a2s (sock->addr, sock->addrlen), sock);
1397 GNUNET_assert (0 != (sock->ccs & COCO_TRANSMIT_READY));
1398 sock->ccs -= COCO_TRANSMIT_READY; /* remove request */
1399 notify = sock->nth.notify_ready;
1400 sock->nth.notify_ready = NULL;
1401 notify (sock->nth.notify_ready_cls, 0, NULL);
1406 * Task invoked by the scheduler when we failed to connect
1407 * at the time of being asked to transmit.
1409 * This task notifies the client about the error.
1411 * @param cls the 'struct GNUNET_CONNECTION_Handle'
1412 * @param tc scheduler context
1415 connect_error (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1417 struct GNUNET_CONNECTION_Handle *sock = cls;
1418 GNUNET_CONNECTION_TransmitReadyNotify notify;
1420 #if DEBUG_CONNECTION
1421 LOG (GNUNET_ERROR_TYPE_DEBUG,
1422 "Transmission request of size %u fails (%s/%u), connection failed (%p).\n",
1423 sock->nth.notify_size, sock->hostname, sock->port, sock);
1425 sock->write_task = GNUNET_SCHEDULER_NO_TASK;
1426 notify = sock->nth.notify_ready;
1427 sock->nth.notify_ready = NULL;
1428 notify (sock->nth.notify_ready_cls, 0, NULL);
1438 transmit_error (struct GNUNET_CONNECTION_Handle *sock)
1440 GNUNET_CONNECTION_TransmitReadyNotify notify;
1442 if (NULL != sock->sock)
1444 GNUNET_NETWORK_socket_shutdown (sock->sock, SHUT_RDWR);
1445 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock->sock));
1448 if (sock->read_task != GNUNET_SCHEDULER_NO_TASK)
1450 GNUNET_SCHEDULER_cancel (sock->read_task);
1451 sock->read_task = GNUNET_SCHEDULER_NO_TASK;
1452 signal_timeout (sock);
1455 if (sock->nth.notify_ready == NULL)
1456 return; /* nobody to tell about it */
1457 notify = sock->nth.notify_ready;
1458 sock->nth.notify_ready = NULL;
1459 notify (sock->nth.notify_ready_cls, 0, NULL);
1464 * See if we are now connected. If not, wait longer for
1465 * connect to succeed. If connected, we should be able
1466 * to write now as well, unless we timed out.
1468 * @param cls our connection handle
1469 * @param tc task context describing why we are here
1472 transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1474 struct GNUNET_CONNECTION_Handle *sock = cls;
1475 GNUNET_CONNECTION_TransmitReadyNotify notify;
1479 #if DEBUG_CONNECTION
1480 LOG (GNUNET_ERROR_TYPE_DEBUG, "transmit_ready running (%p).\n", sock);
1482 GNUNET_assert (sock->write_task != GNUNET_SCHEDULER_NO_TASK);
1483 sock->write_task = GNUNET_SCHEDULER_NO_TASK;
1484 GNUNET_assert (sock->nth.timeout_task == GNUNET_SCHEDULER_NO_TASK);
1485 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1487 if ((sock->ignore_shutdown == GNUNET_YES) && (NULL != sock->sock))
1488 goto SCHEDULE_WRITE; /* ignore shutdown, go again immediately */
1489 #if DEBUG_CONNECTION
1490 LOG (GNUNET_ERROR_TYPE_DEBUG,
1491 "Transmit to `%s' fails, shutdown happened (%p).\n",
1492 GNUNET_a2s (sock->addr, sock->addrlen), sock);
1494 notify = sock->nth.notify_ready;
1497 sock->nth.notify_ready = NULL;
1498 notify (sock->nth.notify_ready_cls, 0, NULL);
1502 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT))
1504 #if DEBUG_CONNECTION
1505 LOG (GNUNET_ERROR_TYPE_DEBUG,
1506 "Transmit to `%s' fails, time out reached (%p).\n",
1507 GNUNET_a2s (sock->addr, sock->addrlen), sock);
1509 notify = sock->nth.notify_ready;
1510 GNUNET_assert (NULL != notify);
1511 sock->nth.notify_ready = NULL;
1512 notify (sock->nth.notify_ready_cls, 0, NULL);
1515 GNUNET_assert (NULL != sock->sock);
1516 if (tc->write_ready == NULL)
1518 /* special circumstances (in particular,
1519 * PREREQ_DONE after connect): not yet ready to write,
1520 * but no "fatal" error either. Hence retry. */
1521 goto SCHEDULE_WRITE;
1523 if (!GNUNET_NETWORK_fdset_isset (tc->write_ready, sock->sock))
1525 #if DEBUG_CONNECTION
1526 LOG (GNUNET_ERROR_TYPE_INFO,
1528 ("Could not satisfy pending transmission request, socket closed or connect failed (%p).\n"),
1531 transmit_error (sock);
1532 return; /* connect failed for good, we're finished */
1534 GNUNET_assert (sock->write_buffer_off >= sock->write_buffer_pos);
1535 if ((sock->nth.notify_ready != NULL) &&
1536 (sock->write_buffer_size < sock->nth.notify_size))
1538 sock->write_buffer =
1539 GNUNET_realloc (sock->write_buffer, sock->nth.notify_size);
1540 sock->write_buffer_size = sock->nth.notify_size;
1542 process_notify (sock);
1543 have = sock->write_buffer_off - sock->write_buffer_pos;
1546 /* no data ready for writing, terminate write loop */
1549 GNUNET_assert (have <= sock->write_buffer_size);
1550 GNUNET_assert (have + sock->write_buffer_pos <= sock->write_buffer_size);
1551 GNUNET_assert (sock->write_buffer_pos <= sock->write_buffer_size);
1554 GNUNET_NETWORK_socket_send (sock->sock,
1555 &sock->write_buffer[sock->write_buffer_pos],
1564 LOG (GNUNET_ERROR_TYPE_ERROR, _("Failed to send to `%s': %s\n"),
1565 GNUNET_a2s (sock->addr, sock->addrlen), STRERROR (en));
1567 #if DEBUG_CONNECTION
1568 LOG_STRERROR (GNUNET_ERROR_TYPE_DEBUG, "send");
1570 transmit_error (sock);
1573 #if DEBUG_CONNECTION
1574 LOG (GNUNET_ERROR_TYPE_DEBUG,
1575 "transmit_ready transmitted %u/%u bytes to `%s' (%p)\n",
1576 (unsigned int) ret, have, GNUNET_a2s (sock->addr, sock->addrlen), sock);
1578 sock->write_buffer_pos += ret;
1579 if (sock->write_buffer_pos == sock->write_buffer_off)
1581 /* transmitted all pending data */
1582 sock->write_buffer_pos = 0;
1583 sock->write_buffer_off = 0;
1585 if ((sock->write_buffer_off == 0) && (NULL == sock->nth.notify_ready))
1586 return; /* all data sent! */
1587 /* not done writing, schedule more */
1589 #if DEBUG_CONNECTION
1590 LOG (GNUNET_ERROR_TYPE_DEBUG,
1591 "Re-scheduling transmit_ready (more to do) (%p).\n", sock);
1593 have = sock->write_buffer_off - sock->write_buffer_pos;
1594 GNUNET_assert ((sock->nth.notify_ready != NULL) || (have > 0));
1595 if (sock->write_task == GNUNET_SCHEDULER_NO_TASK)
1597 GNUNET_SCHEDULER_add_write_net ((sock->nth.notify_ready ==
1598 NULL) ? GNUNET_TIME_UNIT_FOREVER_REL :
1599 GNUNET_TIME_absolute_get_remaining
1600 (sock->nth.transmit_timeout),
1601 sock->sock, &transmit_ready, sock);
1606 * Ask the socket to call us once the specified number of bytes
1607 * are free in the transmission buffer. May call the notify
1608 * method immediately if enough space is available.
1610 * @param sock socket
1611 * @param size number of bytes to send
1612 * @param timeout after how long should we give up (and call
1613 * notify with buf NULL and size 0)?
1614 * @param notify function to call
1615 * @param notify_cls closure for notify
1616 * @return non-NULL if the notify callback was queued,
1617 * NULL if we are already going to notify someone else (busy)
1619 struct GNUNET_CONNECTION_TransmitHandle *
1620 GNUNET_CONNECTION_notify_transmit_ready (struct GNUNET_CONNECTION_Handle *sock,
1622 struct GNUNET_TIME_Relative timeout,
1623 GNUNET_CONNECTION_TransmitReadyNotify
1624 notify, void *notify_cls)
1626 if (sock->nth.notify_ready != NULL)
1631 GNUNET_assert (notify != NULL);
1632 GNUNET_assert (size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
1633 GNUNET_assert (sock->write_buffer_off <= sock->write_buffer_size);
1634 GNUNET_assert (sock->write_buffer_pos <= sock->write_buffer_size);
1635 GNUNET_assert (sock->write_buffer_pos <= sock->write_buffer_off);
1636 sock->nth.notify_ready = notify;
1637 sock->nth.notify_ready_cls = notify_cls;
1638 sock->nth.sh = sock;
1639 sock->nth.notify_size = size;
1640 sock->nth.transmit_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1641 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->nth.timeout_task);
1642 if ((sock->sock == NULL) && (sock->ap_head == NULL) &&
1643 (sock->dns_active == NULL))
1645 if (sock->write_task != GNUNET_SCHEDULER_NO_TASK)
1646 GNUNET_SCHEDULER_cancel (sock->write_task);
1647 sock->write_task = GNUNET_SCHEDULER_add_now (&connect_error, sock);
1650 if (GNUNET_SCHEDULER_NO_TASK != sock->write_task)
1652 if (sock->sock != NULL)
1654 #if DEBUG_CONNECTION
1655 LOG (GNUNET_ERROR_TYPE_DEBUG, "Scheduling transmit_ready (%p).\n", sock);
1658 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_absolute_get_remaining
1659 (sock->nth.transmit_timeout),
1660 sock->sock, &transmit_ready, sock);
1664 #if DEBUG_CONNECTION
1665 LOG (GNUNET_ERROR_TYPE_DEBUG,
1666 "CCS-Scheduling transmit_ready, adding timeout task (%p).\n", sock);
1668 sock->ccs |= COCO_TRANSMIT_READY;
1669 sock->nth.timeout_task =
1670 GNUNET_SCHEDULER_add_delayed (timeout, &transmit_timeout, sock);
1677 * Cancel the specified transmission-ready notification.
1679 * @param th notification to cancel
1682 GNUNET_CONNECTION_notify_transmit_ready_cancel (struct
1683 GNUNET_CONNECTION_TransmitHandle
1686 GNUNET_assert (th->notify_ready != NULL);
1687 if (0 != (th->sh->ccs & COCO_TRANSMIT_READY))
1689 #if DEBUG_CONNECTION
1690 LOG (GNUNET_ERROR_TYPE_DEBUG,
1691 "notify_transmit_ready_cancel cancels timeout_task (%p)\n", th);
1693 GNUNET_SCHEDULER_cancel (th->timeout_task);
1694 th->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1695 th->sh->ccs -= COCO_TRANSMIT_READY;
1699 if (th->sh->write_task != GNUNET_SCHEDULER_NO_TASK)
1701 GNUNET_SCHEDULER_cancel (th->sh->write_task);
1702 th->sh->write_task = GNUNET_SCHEDULER_NO_TASK;
1705 th->notify_ready = NULL;
1708 /* end of connection.c */