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
299 void GNUNET_CONNECTION_persist_(struct GNUNET_CONNECTION_Handle *sock)
301 sock->persist = GNUNET_YES;
306 * Disable the "CORK" feature for communication with the given socket,
307 * forcing the OS to immediately flush the buffer on transmission
308 * instead of potentially buffering multiple messages. Essentially
309 * reduces the OS send buffers to zero.
310 * Used to make sure that the last messages sent through the connection
311 * reach the other side before the process is terminated.
313 * @param sock the connection to make flushing and blocking
314 * @return GNUNET_OK on success
316 int GNUNET_CONNECTION_disable_corking (struct GNUNET_CONNECTION_Handle *sock)
318 return GNUNET_NETWORK_socket_disable_corking (sock->sock);
322 * Create a socket handle by boxing an existing OS socket. The OS
323 * socket should henceforth be no longer used directly.
324 * GNUNET_socket_destroy will close it.
326 * @param osSocket existing socket to box
327 * @return the boxed socket handle
329 struct GNUNET_CONNECTION_Handle *
330 GNUNET_CONNECTION_create_from_existing (struct GNUNET_NETWORK_Handle
333 struct GNUNET_CONNECTION_Handle *ret;
334 ret = GNUNET_malloc (sizeof (struct GNUNET_CONNECTION_Handle));
335 ret->write_buffer_size = GNUNET_SERVER_MIN_BUFFER_SIZE;
336 ret->write_buffer = GNUNET_malloc(ret->write_buffer_size);
337 ret->sock = osSocket;
343 * Create a socket handle by accepting on a listen socket. This
344 * function may block if the listen socket has no connection ready.
346 * @param access function to use to check if access is allowed
347 * @param access_cls closure for access
348 * @param lsock listen socket
349 * @return the socket handle, NULL on error
351 struct GNUNET_CONNECTION_Handle *
352 GNUNET_CONNECTION_create_from_accept (GNUNET_CONNECTION_AccessCheck access,
354 struct GNUNET_NETWORK_Handle *lsock)
356 struct GNUNET_CONNECTION_Handle *ret;
359 struct GNUNET_NETWORK_Handle *sock;
361 struct sockaddr_in *v4;
362 struct sockaddr_in6 *v6;
365 struct GNUNET_CONNECTION_Credentials *gcp;
366 struct GNUNET_CONNECTION_Credentials gc;
372 addrlen = sizeof (addr);
374 GNUNET_NETWORK_socket_accept (lsock, (struct sockaddr *) &addr, &addrlen);
377 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "accept");
380 if ( (addrlen > sizeof (addr)) ||
381 (addrlen < sizeof (sa_family_t)) )
384 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
388 sa = (struct sockaddr *) addr;
389 v6 = (struct sockaddr_in6 *) addr;
390 if ((sa->sa_family == AF_INET6) && (IN6_IS_ADDR_V4MAPPED (&v6->sin6_addr)))
392 /* convert to V4 address */
393 v4 = GNUNET_malloc (sizeof (struct sockaddr_in));
394 memset (v4, 0, sizeof (struct sockaddr_in));
395 v4->sin_family = AF_INET;
396 #if HAVE_SOCKADDR_IN_SIN_LEN
397 v4->sin_len = (u_char) sizeof (struct sockaddr_in);
399 memcpy (&v4->sin_addr,
400 &((char *) &v6->sin6_addr)[sizeof (struct in6_addr) -
401 sizeof (struct in_addr)],
402 sizeof (struct in_addr));
403 v4->sin_port = v6->sin6_port;
405 addrlen = sizeof (struct sockaddr_in);
409 uaddr = GNUNET_malloc (addrlen);
410 memcpy (uaddr, addr, addrlen);
415 if (sa->sa_family == AF_UNIX)
419 if (0 == getpeereid (GNUNET_NETWORK_get_fd (sock),
425 /* largely traditional GNU/Linux */
428 getsockopt (GNUNET_NETWORK_get_fd (sock),
429 SOL_SOCKET, SO_PEERCRED, &uc, &olen)) &&
430 (olen == sizeof (uc)) )
437 #if HAVE_GETPEERUCRED
438 /* this is for Solaris 10 */
442 if (0 == getpeerucred (GNUNET_NETWORK_get_fd (sock), &uc))
444 gc.uid = ucred_geteuid (uc);
445 gc.gid = ucred_getegid (uc);
454 if ((access != NULL) &&
455 (GNUNET_YES != (aret = access (access_cls, gcp, uaddr, addrlen))))
457 if (aret == GNUNET_NO)
458 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
459 _("Access denied to `%s'\n"),
460 GNUNET_a2s (uaddr, addrlen));
461 GNUNET_break (GNUNET_OK ==
462 GNUNET_NETWORK_socket_shutdown (sock, SHUT_RDWR));
463 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
467 ret = GNUNET_malloc (sizeof (struct GNUNET_CONNECTION_Handle));
468 ret->write_buffer_size = GNUNET_SERVER_MIN_BUFFER_SIZE;
469 ret->write_buffer = GNUNET_malloc(ret->write_buffer_size);
471 ret->addrlen = addrlen;
474 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
475 _("Accepting connection from `%s': %p\n"),
476 GNUNET_a2s (uaddr, addrlen), ret);
482 * Obtain the network address of the other party.
484 * @param sock the client to get the address for
485 * @param addr where to store the address
486 * @param addrlen where to store the length of the address
487 * @return GNUNET_OK on success
490 GNUNET_CONNECTION_get_address (struct GNUNET_CONNECTION_Handle *sock,
491 void **addr, size_t * addrlen)
493 if ((sock->addr == NULL) || (sock->addrlen == 0))
495 *addr = GNUNET_malloc (sock->addrlen);
496 memcpy (*addr, sock->addr, sock->addrlen);
497 *addrlen = sock->addrlen;
503 * This function is called after establishing a connection either has
504 * succeeded or timed out. Note that it is possible that the attempt
505 * timed out and that we're immediately retrying. If we are retrying,
506 * we need to wait again (or timeout); if we succeeded, we need to
507 * wait for data (or timeout).
509 * @param cls our connection handle
510 * @param tc task context describing why we are here
513 receive_again (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
517 * Scheduler let us know that the connect task is finished (or was
518 * cancelled due to shutdown). Now really clean up.
520 * @param cls our "struct GNUNET_CONNECTION_Handle *"
524 destroy_continuation (void *cls,
525 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 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
537 "Destroy waits for CCS-TR to be done (%p)\n", sock);
539 sock->ccs |= COCO_DESTROY_CONTINUATION;
542 if (sock->write_task != GNUNET_SCHEDULER_NO_TASK)
545 GNUNET_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,
551 &destroy_continuation, sock);
554 if (0 != (sock->ccs & COCO_RECEIVE_AGAIN))
556 sock->ccs |= COCO_DESTROY_CONTINUATION;
559 if (sock->sock != NULL)
562 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
563 "Shutting down socket (%p)\n", sock);
565 if (sock->persist != GNUNET_YES)
567 if ( (GNUNET_YES != GNUNET_NETWORK_socket_shutdown (sock->sock, SHUT_RDWR)) &&
568 (errno != ENOTCONN) &&
569 (errno != ECONNRESET) )
570 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "shutdown");
573 if (sock->read_task != GNUNET_SCHEDULER_NO_TASK)
575 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task);
577 = GNUNET_SCHEDULER_add_after (sock->read_task,
578 &destroy_continuation, sock);
582 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
583 "Destroy actually runs (%p)!\n", sock);
585 while (NULL != (pos = sock->ap_head))
587 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (pos->sock));
588 GNUNET_SCHEDULER_cancel (pos->task);
589 GNUNET_CONTAINER_DLL_remove (sock->ap_head, sock->ap_tail, pos);
592 GNUNET_assert (sock->nth.timeout_task == GNUNET_SCHEDULER_NO_TASK);
593 GNUNET_assert (sock->ccs == COCO_NONE);
594 if (NULL != (notify = sock->nth.notify_ready))
596 sock->nth.notify_ready = NULL;
597 notify (sock->nth.notify_ready_cls, 0, NULL);
600 if (sock->sock != NULL)
602 if (sock->persist != GNUNET_YES)
603 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock->sock));
605 GNUNET_free (sock->sock); /* at least no memory leak (we deliberately
606 leak the socket in this special case) ... */
608 GNUNET_free_non_null (sock->addr);
609 GNUNET_free_non_null (sock->hostname);
610 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task);
612 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
613 "Freeing memory of connection %p.\n", sock);
615 GNUNET_free (sock->write_buffer);
622 * See if we are now connected. If not, wait longer for
623 * connect to succeed. If connected, we should be able
624 * to write now as well, unless we timed out.
626 * @param cls our connection handle
627 * @param tc task context describing why we are here
630 transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
634 * We've failed for good to establish a connection.
636 * @param h the connection we tried to establish
639 connect_fail_continuation (struct GNUNET_CONNECTION_Handle *h)
642 GNUNET_log ((0 != strncmp (h->hostname,
645 ? GNUNET_ERROR_TYPE_INFO
646 : GNUNET_ERROR_TYPE_WARNING,
647 _("Failed to establish TCP connection to `%s:%u', no further addresses to try.\n"),
648 h->hostname, h->port);
650 /* connect failed / timed out */
651 GNUNET_break (h->ap_head == NULL);
652 GNUNET_break (h->ap_tail == NULL);
653 GNUNET_break (h->dns_active == GNUNET_NO);
654 GNUNET_break (h->sock == NULL);
656 /* trigger jobs that used to wait on "connect_task" */
657 if (0 != (h->ccs & COCO_RECEIVE_AGAIN))
660 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
661 "connect_fail_continuation triggers receive_again (%p)\n",
664 h->ccs -= COCO_RECEIVE_AGAIN;
665 h->read_task = GNUNET_SCHEDULER_add_now (&receive_again, h);
667 if (0 != (h->ccs & COCO_TRANSMIT_READY))
670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
671 "connect_fail_continuation cancels timeout_task, triggers transmit_ready (%p)\n",
674 GNUNET_assert (h->nth.timeout_task != GNUNET_SCHEDULER_NO_TASK);
675 GNUNET_SCHEDULER_cancel (h->nth.timeout_task);
676 h->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK;
677 h->ccs -= COCO_TRANSMIT_READY;
678 GNUNET_assert (h->nth.notify_ready != NULL);
679 GNUNET_assert (h->write_task == GNUNET_SCHEDULER_NO_TASK);
680 h->write_task = GNUNET_SCHEDULER_add_now (&transmit_ready, h);
682 if (0 != (h->ccs & COCO_DESTROY_CONTINUATION))
685 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
686 "connect_fail_continuation runs destroy_continuation (%p)\n",
689 h->ccs -= COCO_DESTROY_CONTINUATION;
690 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == h->destroy_task);
692 = GNUNET_SCHEDULER_add_now (&destroy_continuation,
699 * We've succeeded in establishing a connection.
701 * @param h the connection we tried to establish
704 connect_success_continuation (struct GNUNET_CONNECTION_Handle *h)
707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
708 "Connection to `%s' succeeded! (%p)\n",
709 GNUNET_a2s (h->addr, h->addrlen), h);
711 /* trigger jobs that waited for the connection */
712 if (0 != (h->ccs & COCO_RECEIVE_AGAIN))
715 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
716 "connect_success_continuation runs receive_again (%p)\n",
719 h->ccs -= COCO_RECEIVE_AGAIN;
720 h->read_task = GNUNET_SCHEDULER_add_now (&receive_again, h);
722 if (0 != (h->ccs & COCO_TRANSMIT_READY))
725 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
726 "connect_success_continuation runs transmit_ready, cancels timeout_task (%p)\n",
729 GNUNET_assert (h->nth.timeout_task != GNUNET_SCHEDULER_NO_TASK);
730 GNUNET_SCHEDULER_cancel (h->nth.timeout_task);
731 h->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK;
732 h->ccs -= COCO_TRANSMIT_READY;
733 GNUNET_assert (h->write_task == GNUNET_SCHEDULER_NO_TASK);
734 GNUNET_assert (h->nth.notify_ready != NULL);
736 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_absolute_get_remaining
737 (h->nth.transmit_timeout), h->sock,
740 if (0 != (h->ccs & COCO_DESTROY_CONTINUATION))
743 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
744 "connect_success_continuation runs destroy_continuation (%p)\n",
747 h->ccs -= COCO_DESTROY_CONTINUATION;
748 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == h->destroy_task);
750 = GNUNET_SCHEDULER_add_now (&destroy_continuation,
757 * Scheduler let us know that we're either ready to write on the
758 * socket OR connect timed out. Do the right thing.
760 * @param cls the "struct AddressProbe*" with the address that we are probing
761 * @param tc success or failure info about the connect attempt.
764 connect_probe_continuation (void *cls,
765 const struct GNUNET_SCHEDULER_TaskContext *tc)
767 struct AddressProbe *ap = cls;
768 struct GNUNET_CONNECTION_Handle *h = ap->h;
769 struct AddressProbe *pos;
773 GNUNET_assert (ap->sock != NULL);
774 GNUNET_CONTAINER_DLL_remove (h->ap_head, h->ap_tail, ap);
775 len = sizeof (error);
778 if ( (0 == (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) ||
780 GNUNET_NETWORK_socket_getsockopt (ap->sock, SOL_SOCKET, SO_ERROR,
784 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ap->sock));
786 if ((NULL == h->ap_head) && (h->dns_active == GNUNET_NO))
787 connect_fail_continuation (h);
790 GNUNET_assert (h->sock == NULL);
792 GNUNET_assert (h->addr == NULL);
793 h->addr = GNUNET_malloc (ap->addrlen);
794 memcpy (h->addr, ap->addr, ap->addrlen);
795 h->addrlen = ap->addrlen;
797 /* cancel all other attempts */
798 while (NULL != (pos = h->ap_head))
800 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (pos->sock));
801 GNUNET_SCHEDULER_cancel (pos->task);
802 GNUNET_CONTAINER_DLL_remove (h->ap_head, h->ap_tail, pos);
805 connect_success_continuation (h);
810 * Try to establish a socket connection given the specified address.
811 * This function is called by the resolver once we have a DNS reply.
813 * @param cls our "struct GNUNET_CONNECTION_Handle *"
814 * @param addr address to try, NULL for "last call"
815 * @param addrlen length of addr
818 try_connect_using_address (void *cls,
819 const struct sockaddr *addr, socklen_t addrlen)
821 struct GNUNET_CONNECTION_Handle *h = cls;
822 struct AddressProbe *ap;
823 struct GNUNET_TIME_Relative delay;
827 h->dns_active = NULL;
828 if (NULL == h->ap_head)
829 connect_fail_continuation (h);
833 return; /* already connected */
834 GNUNET_assert (h->addr == NULL);
837 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
838 "Trying to connect using address `%s:%u/%s:%u'\n",
839 h->hostname, h->port, GNUNET_a2s (addr, addrlen), h->port);
841 ap = GNUNET_malloc (sizeof (struct AddressProbe) + addrlen);
842 ap->addr = (const struct sockaddr *) &ap[1];
843 memcpy (&ap[1], addr, addrlen);
844 ap->addrlen = addrlen;
847 switch (ap->addr->sa_family)
850 ((struct sockaddr_in *) ap->addr)->sin_port = htons (h->port);
853 ((struct sockaddr_in6 *) ap->addr)->sin6_port = htons (h->port);
858 return; /* not supported by us */
861 GNUNET_NETWORK_socket_create (ap->addr->sa_family, SOCK_STREAM, 0);
862 if (ap->sock == NULL)
865 return; /* not supported by OS */
868 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
869 _("Trying to connect to `%s' (%p)\n"),
870 GNUNET_a2s (ap->addr, ap->addrlen), h);
872 if ((GNUNET_OK != GNUNET_NETWORK_socket_connect (ap->sock,
875 (errno != EINPROGRESS))
877 /* maybe refused / unsupported address, try next */
878 GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "connect");
879 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ap->sock));
883 GNUNET_CONTAINER_DLL_insert (h->ap_head, h->ap_tail, ap);
884 delay = GNUNET_CONNECTION_CONNECT_RETRY_TIMEOUT;
885 if (h->nth.notify_ready != NULL)
886 delay = GNUNET_TIME_relative_min (delay,
887 GNUNET_TIME_absolute_get_remaining
888 (h->nth.transmit_timeout));
889 if (h->receiver != NULL)
890 delay = GNUNET_TIME_relative_min (delay,
891 GNUNET_TIME_absolute_get_remaining
892 (h->receive_timeout));
894 GNUNET_SCHEDULER_add_write_net (delay, ap->sock,
895 &connect_probe_continuation, ap);
900 * Create a socket handle by (asynchronously) connecting to a host.
901 * This function returns immediately, even if the connection has not
902 * yet been established. This function only creates TCP connections.
904 * @param cfg configuration to use
905 * @param hostname name of the host to connect to
906 * @param port port to connect to
907 * @return the socket handle
909 struct GNUNET_CONNECTION_Handle *
910 GNUNET_CONNECTION_create_from_connect (const struct
911 GNUNET_CONFIGURATION_Handle *cfg,
912 const char *hostname, uint16_t port)
914 struct GNUNET_CONNECTION_Handle *ret;
916 GNUNET_assert (0 < strlen (hostname)); /* sanity check */
917 ret = GNUNET_malloc (sizeof (struct GNUNET_CONNECTION_Handle));
919 ret->write_buffer_size = GNUNET_SERVER_MIN_BUFFER_SIZE;
920 ret->write_buffer = GNUNET_malloc(ret->write_buffer_size);
922 ret->hostname = GNUNET_strdup (hostname);
923 ret->dns_active = GNUNET_RESOLVER_ip_get (ret->hostname,
925 GNUNET_CONNECTION_CONNECT_RETRY_TIMEOUT,
926 &try_connect_using_address, ret);
932 * Create a socket handle by connecting to a UNIX domain service.
933 * This function returns immediately, even if the connection has not
934 * yet been established. This function only creates UNIX connections.
936 * @param cfg configuration to use
937 * @param unixpath path to connect to
938 * @return the socket handle, NULL on systems without UNIX support
940 struct GNUNET_CONNECTION_Handle *
941 GNUNET_CONNECTION_create_from_connect_to_unixpath (const struct
942 GNUNET_CONFIGURATION_Handle *cfg,
943 const char *unixpath)
946 struct GNUNET_CONNECTION_Handle *ret;
947 struct sockaddr_un *un;
950 GNUNET_assert (0 < strlen (unixpath)); /* sanity check */
951 un = GNUNET_malloc (sizeof (struct sockaddr_un));
952 un->sun_family = AF_UNIX;
953 slen = strlen (unixpath);
954 if (slen >= sizeof (un->sun_path))
955 slen = sizeof (un->sun_path) - 1;
956 memcpy (un->sun_path,
959 un->sun_path[slen] = '\0';
960 slen = sizeof (struct sockaddr_un);
961 #if HAVE_SOCKADDR_IN_SIN_LEN
962 un->sun_len = (u_char) slen;
965 un->sun_path[0] = '\0';
967 ret = GNUNET_malloc (sizeof (struct GNUNET_CONNECTION_Handle));
969 ret->write_buffer_size = GNUNET_SERVER_MIN_BUFFER_SIZE;
970 ret->write_buffer = GNUNET_malloc(ret->write_buffer_size);
972 ret->hostname = NULL;
973 ret->addr = (struct sockaddr*) un;
975 ret->sock = GNUNET_NETWORK_socket_create (AF_UNIX, SOCK_STREAM, 0);
976 if (NULL == ret->sock)
978 GNUNET_free (ret->addr);
979 GNUNET_free (ret->write_buffer);
983 if (GNUNET_OK != GNUNET_NETWORK_socket_connect (ret->sock,
987 /* Just return; we expect everything to work eventually so don't fail HARD */
988 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret->sock));
992 connect_success_continuation (ret);
1001 * Create a socket handle by (asynchronously) connecting to a host.
1002 * This function returns immediately, even if the connection has not
1003 * yet been established. This function only creates TCP connections.
1005 * @param af_family address family to use
1006 * @param serv_addr server address
1007 * @param addrlen length of server address
1008 * @return the socket handle
1010 struct GNUNET_CONNECTION_Handle *
1011 GNUNET_CONNECTION_create_from_sockaddr (int af_family,
1012 const struct sockaddr *serv_addr,
1015 struct GNUNET_NETWORK_Handle *s;
1016 struct GNUNET_CONNECTION_Handle *ret;
1019 s = GNUNET_NETWORK_socket_create (af_family, SOCK_STREAM, 0);
1022 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING |
1023 GNUNET_ERROR_TYPE_BULK, "socket");
1026 if ((GNUNET_OK != GNUNET_NETWORK_socket_connect (s, serv_addr, addrlen))
1027 && (errno != EINPROGRESS))
1029 /* maybe refused / unsupported address, try next */
1030 GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "connect");
1031 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (s));
1034 ret = GNUNET_CONNECTION_create_from_existing (s);
1035 ret->addr = GNUNET_malloc (addrlen);
1036 memcpy (ret->addr, serv_addr, addrlen);
1037 ret->addrlen = addrlen;
1038 #if DEBUG_CONNECTION
1039 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1040 _("Trying to connect to `%s' (%p)\n"),
1041 GNUNET_a2s (serv_addr, addrlen), ret);
1048 * Check if socket is valid (no fatal errors have happened so far).
1049 * Note that a socket that is still trying to connect is considered
1052 * @param sock socket to check
1053 * @return GNUNET_YES if valid, GNUNET_NO otherwise
1056 GNUNET_CONNECTION_check (struct GNUNET_CONNECTION_Handle *sock)
1058 if ((sock->ap_head != NULL) || (sock->dns_active != NULL))
1059 return GNUNET_YES; /* still trying to connect */
1060 return (sock->sock == NULL) ? GNUNET_NO : GNUNET_YES;
1065 * Close the socket and free associated resources. Pending
1066 * transmissions may be completed or dropped depending on the
1067 * arguments. If a receive call is pending and should
1068 * NOT be completed, 'GNUNET_CONNECTION_receive_cancel'
1069 * should be called explicitly first.
1071 * @param sock socket to destroy
1072 * @param finish_pending_write should pending writes be completed or aborted?
1073 * (this applies to transmissions where the data has already been
1074 * read from the application; all other transmissions should be
1075 * aborted using 'GNUNET_CONNECTION_notify_transmit_ready_cancel').
1078 GNUNET_CONNECTION_destroy (struct GNUNET_CONNECTION_Handle *sock,
1079 int finish_pending_write)
1081 if (GNUNET_NO == finish_pending_write)
1083 if (sock->write_task != GNUNET_SCHEDULER_NO_TASK)
1085 GNUNET_SCHEDULER_cancel (sock->write_task);
1086 sock->write_task = GNUNET_SCHEDULER_NO_TASK;
1087 sock->write_buffer_off = 0;
1090 if ((sock->write_buffer_off == 0) && (sock->dns_active != NULL))
1092 GNUNET_RESOLVER_request_cancel (sock->dns_active);
1093 sock->dns_active = NULL;
1096 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task);
1098 = GNUNET_SCHEDULER_add_now (&destroy_continuation, sock);
1103 * Tell the receiver callback that a timeout was reached.
1106 signal_timeout (struct GNUNET_CONNECTION_Handle *sh)
1108 GNUNET_CONNECTION_Receiver receiver;
1110 #if DEBUG_CONNECTION
1111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1112 "Network signals time out to receiver (%p)!\n", sh);
1114 GNUNET_assert (NULL != (receiver = sh->receiver));
1115 sh->receiver = NULL;
1116 receiver (sh->receiver_cls, NULL, 0, NULL, 0, 0);
1121 * Tell the receiver callback that we had an IO error.
1124 signal_error (struct GNUNET_CONNECTION_Handle *sh, int errcode)
1126 GNUNET_CONNECTION_Receiver receiver;
1127 GNUNET_assert (NULL != (receiver = sh->receiver));
1128 sh->receiver = NULL;
1129 receiver (sh->receiver_cls, NULL, 0, sh->addr, sh->addrlen, errcode);
1134 * This function is called once we either timeout
1135 * or have data ready to read.
1138 receive_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1140 struct GNUNET_CONNECTION_Handle *sh = cls;
1141 struct GNUNET_TIME_Absolute now;
1142 char buffer[sh->max];
1144 GNUNET_CONNECTION_Receiver receiver;
1146 sh->read_task = GNUNET_SCHEDULER_NO_TASK;
1147 if ( (GNUNET_YES == sh->ignore_shutdown) &&
1148 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
1150 /* ignore shutdown request, go again immediately */
1151 #if DEBUG_CONNECTION
1152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1153 "Ignoring shutdown signal per configuration\n");
1155 sh->read_task = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_absolute_get_remaining
1156 (sh->receive_timeout),
1158 &receive_ready, sh);
1161 now = GNUNET_TIME_absolute_get ();
1162 if ((now.abs_value > sh->receive_timeout.abs_value) ||
1163 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT)) ||
1164 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
1166 #if DEBUG_CONNECTION
1167 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1169 "Receive from `%s' encounters error: time out by %llums... (%p)\n",
1170 GNUNET_a2s (sh->addr, sh->addrlen),
1171 GNUNET_TIME_absolute_get_duration (sh->receive_timeout).
1174 signal_timeout (sh);
1177 if (sh->sock == NULL)
1179 /* connect failed for good */
1180 #if DEBUG_CONNECTION
1181 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1182 "Receive encounters error, socket closed... (%p)\n", sh);
1184 signal_error (sh, ECONNREFUSED);
1187 GNUNET_assert (GNUNET_NETWORK_fdset_isset (tc->read_ready, sh->sock));
1189 ret = GNUNET_NETWORK_socket_recv (sh->sock, buffer, sh->max);
1194 #if DEBUG_CONNECTION
1195 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1196 "Error receiving: %s\n", STRERROR (errno));
1198 signal_error (sh, errno);
1201 #if DEBUG_CONNECTION
1202 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1203 "receive_ready read %u/%u bytes from `%s' (%p)!\n",
1205 sh->max, GNUNET_a2s (sh->addr, sh->addrlen), sh);
1207 GNUNET_assert (NULL != (receiver = sh->receiver));
1208 sh->receiver = NULL;
1209 receiver (sh->receiver_cls, buffer, ret, sh->addr, sh->addrlen, 0);
1214 * This function is called after establishing a connection either has
1215 * succeeded or timed out. Note that it is possible that the attempt
1216 * timed out and that we're immediately retrying. If we are retrying,
1217 * we need to wait again (or timeout); if we succeeded, we need to
1218 * wait for data (or timeout).
1220 * @param cls our connection handle
1221 * @param tc task context describing why we are here
1224 receive_again (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1226 struct GNUNET_CONNECTION_Handle *sh = cls;
1227 struct GNUNET_TIME_Absolute now;
1229 sh->read_task = GNUNET_SCHEDULER_NO_TASK;
1230 if (sh->sock == NULL)
1232 /* not connected and no longer trying */
1233 #if DEBUG_CONNECTION
1234 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1235 "Receive encounters error, socket closed (%p)...\n", sh);
1237 signal_error (sh, ECONNREFUSED);
1240 now = GNUNET_TIME_absolute_get ();
1241 if ((now.abs_value > sh->receive_timeout.abs_value) ||
1242 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
1244 #if DEBUG_CONNECTION
1245 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1246 "Receive encounters error: time out (%p)...\n", sh);
1248 signal_timeout (sh);
1251 GNUNET_assert (sh->sock != NULL);
1252 /* connect succeeded, wait for data! */
1253 sh->read_task = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_absolute_get_remaining
1254 (sh->receive_timeout),
1256 &receive_ready, sh);
1261 * Receive data from the given socket. Note that this function will
1262 * call "receiver" asynchronously using the scheduler. It will
1263 * "immediately" return. Note that there MUST only be one active
1264 * receive call per socket at any given point in time (so do not
1265 * call receive again until the receiver callback has been invoked).
1267 * @param sock socket handle
1268 * @param max maximum number of bytes to read
1269 * @param timeout maximum amount of time to wait (use -1 for "forever")
1270 * @param receiver function to call with received data
1271 * @param receiver_cls closure for receiver
1274 GNUNET_CONNECTION_receive (struct GNUNET_CONNECTION_Handle *sock,
1276 struct GNUNET_TIME_Relative timeout,
1277 GNUNET_CONNECTION_Receiver receiver,
1280 struct GNUNET_SCHEDULER_TaskContext tc;
1282 GNUNET_assert ((sock->read_task == GNUNET_SCHEDULER_NO_TASK) &&
1283 (0 == (sock->ccs & COCO_RECEIVE_AGAIN)) &&
1284 (sock->receiver == NULL));
1285 sock->receiver = receiver;
1286 sock->receiver_cls = receiver_cls;
1287 sock->receive_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1289 if (sock->sock != NULL)
1291 memset (&tc, 0, sizeof (tc));
1292 tc.reason = GNUNET_SCHEDULER_REASON_PREREQ_DONE;
1293 receive_again (sock, &tc);
1296 if ((sock->dns_active == NULL) && (sock->ap_head == NULL))
1298 receiver (receiver_cls, NULL, 0, NULL, 0, ETIMEDOUT);
1301 sock->ccs += COCO_RECEIVE_AGAIN;
1306 * Configure this connection to ignore shutdown signals.
1308 * @param sock socket handle
1309 * @param do_ignore GNUNET_YES to ignore, GNUNET_NO to restore default
1312 GNUNET_CONNECTION_ignore_shutdown (struct GNUNET_CONNECTION_Handle *sock,
1315 sock->ignore_shutdown = do_ignore;
1320 * Cancel receive job on the given socket. Note that the
1321 * receiver callback must not have been called yet in order
1322 * for the cancellation to be valid.
1324 * @param sock socket handle
1325 * @return closure of the original receiver callback closure
1328 GNUNET_CONNECTION_receive_cancel (struct GNUNET_CONNECTION_Handle *sock)
1330 if (sock->read_task != GNUNET_SCHEDULER_NO_TASK)
1332 GNUNET_assert (sock == GNUNET_SCHEDULER_cancel (sock->read_task));
1333 sock->read_task = GNUNET_SCHEDULER_NO_TASK;
1337 GNUNET_assert (0 != (sock->ccs & COCO_RECEIVE_AGAIN));
1338 sock->ccs -= COCO_RECEIVE_AGAIN;
1340 sock->receiver = NULL;
1341 return sock->receiver_cls;
1346 * Try to call the transmit notify method (check if we do
1347 * have enough space available first)!
1349 * @param sock socket for which we should do this processing
1350 * @return GNUNET_YES if we were able to call notify
1353 process_notify (struct GNUNET_CONNECTION_Handle *sock)
1358 GNUNET_CONNECTION_TransmitReadyNotify notify;
1360 GNUNET_assert (sock->write_task == GNUNET_SCHEDULER_NO_TASK);
1361 if (NULL == (notify = sock->nth.notify_ready))
1363 used = sock->write_buffer_off - sock->write_buffer_pos;
1364 avail = sock->write_buffer_size - used;
1365 size = sock->nth.notify_size;
1368 sock->nth.notify_ready = NULL;
1369 if (sock->write_buffer_size - sock->write_buffer_off < size)
1371 /* need to compact */
1372 memmove (sock->write_buffer,
1373 &sock->write_buffer[sock->write_buffer_pos], used);
1374 sock->write_buffer_off -= sock->write_buffer_pos;
1375 sock->write_buffer_pos = 0;
1377 avail = sock->write_buffer_size - sock->write_buffer_off;
1378 GNUNET_assert (avail >= size);
1379 size = notify (sock->nth.notify_ready_cls,
1381 &sock->write_buffer[sock->write_buffer_off]);
1382 GNUNET_assert (size <= avail);
1383 sock->write_buffer_off += size;
1389 * Task invoked by the scheduler when a call to transmit
1390 * is timing out (we never got enough buffer space to call
1391 * the callback function before the specified timeout
1394 * This task notifies the client about the timeout.
1396 * @param cls the 'struct GNUNET_CONNECTION_Handle'
1397 * @param tc scheduler context
1400 transmit_timeout (void *cls,
1401 const struct GNUNET_SCHEDULER_TaskContext *tc)
1403 struct GNUNET_CONNECTION_Handle *sock = cls;
1404 GNUNET_CONNECTION_TransmitReadyNotify notify;
1406 #if DEBUG_CONNECTION
1407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1408 "transmit_timeout running (%p)\n", sock);
1410 sock->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK;
1411 #if DEBUG_CONNECTION
1412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1413 "Transmit to `%s:%u/%s' fails, time out reached (%p).\n",
1415 sock->port, GNUNET_a2s (sock->addr, sock->addrlen), sock);
1417 GNUNET_assert (0 != (sock->ccs & COCO_TRANSMIT_READY));
1418 sock->ccs -= COCO_TRANSMIT_READY; /* remove request */
1419 notify = sock->nth.notify_ready;
1420 sock->nth.notify_ready = NULL;
1421 notify (sock->nth.notify_ready_cls, 0, NULL);
1426 * Task invoked by the scheduler when we failed to connect
1427 * at the time of being asked to transmit.
1429 * This task notifies the client about the error.
1431 * @param cls the 'struct GNUNET_CONNECTION_Handle'
1432 * @param tc scheduler context
1435 connect_error (void *cls,
1436 const struct GNUNET_SCHEDULER_TaskContext *tc)
1438 struct GNUNET_CONNECTION_Handle *sock = cls;
1439 GNUNET_CONNECTION_TransmitReadyNotify notify;
1441 #if DEBUG_CONNECTION
1442 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1443 "Transmission request of size %u fails (%s/%u), connection failed (%p).\n",
1444 sock->nth.notify_size,
1449 sock->write_task = GNUNET_SCHEDULER_NO_TASK;
1450 notify = sock->nth.notify_ready;
1451 sock->nth.notify_ready = NULL;
1452 notify (sock->nth.notify_ready_cls, 0, NULL);
1462 transmit_error (struct GNUNET_CONNECTION_Handle *sock)
1464 GNUNET_CONNECTION_TransmitReadyNotify notify;
1466 if (NULL != sock->sock)
1468 GNUNET_NETWORK_socket_shutdown (sock->sock, SHUT_RDWR);
1469 GNUNET_break (GNUNET_OK ==
1470 GNUNET_NETWORK_socket_close (sock->sock));
1473 if (sock->read_task != GNUNET_SCHEDULER_NO_TASK)
1475 GNUNET_SCHEDULER_cancel (sock->read_task);
1476 sock->read_task = GNUNET_SCHEDULER_NO_TASK;
1477 signal_timeout (sock);
1480 if (sock->nth.notify_ready == NULL)
1481 return; /* nobody to tell about it */
1482 notify = sock->nth.notify_ready;
1483 sock->nth.notify_ready = NULL;
1484 notify (sock->nth.notify_ready_cls, 0, NULL);
1489 * See if we are now connected. If not, wait longer for
1490 * connect to succeed. If connected, we should be able
1491 * to write now as well, unless we timed out.
1493 * @param cls our connection handle
1494 * @param tc task context describing why we are here
1497 transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1499 struct GNUNET_CONNECTION_Handle *sock = cls;
1500 GNUNET_CONNECTION_TransmitReadyNotify notify;
1504 #if DEBUG_CONNECTION
1505 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1506 "transmit_ready running (%p).\n", sock);
1508 GNUNET_assert (sock->write_task != GNUNET_SCHEDULER_NO_TASK);
1509 sock->write_task = GNUNET_SCHEDULER_NO_TASK;
1510 GNUNET_assert (sock->nth.timeout_task == GNUNET_SCHEDULER_NO_TASK);
1511 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1513 if (sock->ignore_shutdown == GNUNET_YES)
1514 goto SCHEDULE_WRITE; /* ignore shutdown, go again immediately */
1515 #if DEBUG_CONNECTION
1516 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1517 "Transmit to `%s' fails, shutdown happened (%p).\n",
1518 GNUNET_a2s (sock->addr, sock->addrlen), sock);
1520 notify = sock->nth.notify_ready;
1523 sock->nth.notify_ready = NULL;
1524 notify (sock->nth.notify_ready_cls, 0, NULL);
1528 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT))
1530 #if DEBUG_CONNECTION
1531 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1532 "Transmit to `%s' fails, time out reached (%p).\n",
1533 GNUNET_a2s (sock->addr, sock->addrlen), sock);
1535 notify = sock->nth.notify_ready;
1536 GNUNET_assert (NULL != notify);
1537 sock->nth.notify_ready = NULL;
1538 notify (sock->nth.notify_ready_cls, 0, NULL);
1541 GNUNET_assert (NULL != sock->sock);
1542 if (tc->write_ready == NULL)
1544 /* special circumstances (in particular,
1545 PREREQ_DONE after connect): not yet ready to write,
1546 but no "fatal" error either. Hence retry. */
1547 goto SCHEDULE_WRITE;
1549 if (!GNUNET_NETWORK_fdset_isset (tc->write_ready, sock->sock))
1551 #if DEBUG_CONNECTION
1552 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1554 ("Could not satisfy pending transmission request, socket closed or connect failed (%p).\n"),
1557 transmit_error (sock);
1558 return; /* connect failed for good, we're finished */
1560 GNUNET_assert (sock->write_buffer_off >= sock->write_buffer_pos);
1561 if ( (sock->nth.notify_ready != NULL) &&
1562 (sock->write_buffer_size < sock->nth.notify_size) )
1564 sock->write_buffer = GNUNET_realloc(sock->write_buffer,
1565 sock->nth.notify_size);
1566 sock->write_buffer_size = sock->nth.notify_size;
1568 process_notify (sock);
1569 have = sock->write_buffer_off - sock->write_buffer_pos;
1572 /* no data ready for writing, terminate write loop */
1575 GNUNET_assert (have <= sock->write_buffer_size);
1576 GNUNET_assert (have + sock->write_buffer_pos <= sock->write_buffer_size);
1577 GNUNET_assert (sock->write_buffer_pos <= sock->write_buffer_size);
1579 ret = GNUNET_NETWORK_socket_send (sock->sock,
1580 &sock->write_buffer[sock->
1589 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1590 _("Failed to send to `%s': %s\n"),
1591 GNUNET_a2s (sock->addr,
1595 #if DEBUG_CONNECTION
1596 GNUNET_log_strerror (GNUNET_ERROR_TYPE_DEBUG, "send");
1598 transmit_error (sock);
1601 #if DEBUG_CONNECTION
1602 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1603 "transmit_ready transmitted %u/%u bytes to `%s' (%p)\n",
1605 have, GNUNET_a2s (sock->addr, sock->addrlen), sock);
1607 sock->write_buffer_pos += ret;
1608 if (sock->write_buffer_pos == sock->write_buffer_off)
1610 /* transmitted all pending data */
1611 sock->write_buffer_pos = 0;
1612 sock->write_buffer_off = 0;
1614 if ((sock->write_buffer_off == 0) && (NULL == sock->nth.notify_ready))
1615 return; /* all data sent! */
1616 /* not done writing, schedule more */
1618 #if DEBUG_CONNECTION
1619 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1620 "Re-scheduling transmit_ready (more to do) (%p).\n", sock);
1622 have = sock->write_buffer_off - sock->write_buffer_pos;
1623 GNUNET_assert ( (sock->nth.notify_ready != NULL) || (have > 0) );
1624 if (sock->write_task == GNUNET_SCHEDULER_NO_TASK)
1626 GNUNET_SCHEDULER_add_write_net ((sock->nth.notify_ready == NULL)
1627 ? GNUNET_TIME_UNIT_FOREVER_REL
1628 : GNUNET_TIME_absolute_get_remaining (sock->nth.transmit_timeout),
1630 &transmit_ready, sock);
1635 * Ask the socket to call us once the specified number of bytes
1636 * are free in the transmission buffer. May call the notify
1637 * method immediately if enough space is available.
1639 * @param sock socket
1640 * @param size number of bytes to send
1641 * @param timeout after how long should we give up (and call
1642 * notify with buf NULL and size 0)?
1643 * @param notify function to call
1644 * @param notify_cls closure for notify
1645 * @return non-NULL if the notify callback was queued,
1646 * NULL if we are already going to notify someone else (busy)
1648 struct GNUNET_CONNECTION_TransmitHandle *
1649 GNUNET_CONNECTION_notify_transmit_ready (struct GNUNET_CONNECTION_Handle
1651 struct GNUNET_TIME_Relative timeout,
1652 GNUNET_CONNECTION_TransmitReadyNotify
1653 notify, void *notify_cls)
1655 if (sock->nth.notify_ready != NULL)
1660 GNUNET_assert (notify != NULL);
1661 GNUNET_assert (size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
1662 GNUNET_assert (sock->write_buffer_off <= sock->write_buffer_size);
1663 GNUNET_assert (sock->write_buffer_pos <= sock->write_buffer_size);
1664 GNUNET_assert (sock->write_buffer_pos <= sock->write_buffer_off);
1665 sock->nth.notify_ready = notify;
1666 sock->nth.notify_ready_cls = notify_cls;
1667 sock->nth.sh = sock;
1668 sock->nth.notify_size = size;
1669 sock->nth.transmit_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1670 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->nth.timeout_task);
1671 if ((sock->sock == NULL) &&
1672 (sock->ap_head == NULL) && (sock->dns_active == NULL))
1674 if (sock->write_task != GNUNET_SCHEDULER_NO_TASK)
1675 GNUNET_SCHEDULER_cancel (sock->write_task);
1676 sock->write_task = GNUNET_SCHEDULER_add_now (&connect_error, sock);
1679 if (GNUNET_SCHEDULER_NO_TASK != sock->write_task)
1681 if (sock->sock != NULL)
1683 #if DEBUG_CONNECTION
1684 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1685 "Scheduling transmit_ready (%p).\n", sock);
1687 sock->write_task = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_absolute_get_remaining
1696 #if DEBUG_CONNECTION
1697 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1698 "CCS-Scheduling transmit_ready, adding timeout task (%p).\n",
1701 sock->ccs |= COCO_TRANSMIT_READY;
1702 sock->nth.timeout_task = GNUNET_SCHEDULER_add_delayed (timeout,
1711 * Cancel the specified transmission-ready
1715 GNUNET_CONNECTION_notify_transmit_ready_cancel (struct
1716 GNUNET_CONNECTION_TransmitHandle
1719 GNUNET_assert (h->notify_ready != NULL);
1720 if (0 != (h->sh->ccs & COCO_TRANSMIT_READY))
1722 #if DEBUG_CONNECTION
1723 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1724 "notify_transmit_ready_cancel cancels timeout_task (%p)\n",
1727 GNUNET_SCHEDULER_cancel (h->timeout_task);
1728 h->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1729 h->sh->ccs -= COCO_TRANSMIT_READY;
1733 if (h->sh->write_task != GNUNET_SCHEDULER_NO_TASK)
1735 GNUNET_SCHEDULER_cancel (h->sh->write_task);
1736 h->sh->write_task = GNUNET_SCHEDULER_NO_TASK;
1739 h->notify_ready = NULL;
1742 /* end of connection.c */