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;
305 * Create a socket handle by boxing an existing OS socket. The OS
306 * socket should henceforth be no longer used directly.
307 * GNUNET_socket_destroy will close it.
309 * @param osSocket existing socket to box
310 * @return the boxed socket handle
312 struct GNUNET_CONNECTION_Handle *
313 GNUNET_CONNECTION_create_from_existing (struct GNUNET_NETWORK_Handle
316 struct GNUNET_CONNECTION_Handle *ret;
317 ret = GNUNET_malloc (sizeof (struct GNUNET_CONNECTION_Handle));
318 ret->write_buffer_size = GNUNET_SERVER_MIN_BUFFER_SIZE;
319 ret->write_buffer = GNUNET_malloc(ret->write_buffer_size);
320 ret->sock = osSocket;
326 * Create a socket handle by accepting on a listen socket. This
327 * function may block if the listen socket has no connection ready.
329 * @param access function to use to check if access is allowed
330 * @param access_cls closure for access
331 * @param lsock listen socket
332 * @return the socket handle, NULL on error
334 struct GNUNET_CONNECTION_Handle *
335 GNUNET_CONNECTION_create_from_accept (GNUNET_CONNECTION_AccessCheck access,
337 struct GNUNET_NETWORK_Handle *lsock)
339 struct GNUNET_CONNECTION_Handle *ret;
342 struct GNUNET_NETWORK_Handle *sock;
344 struct sockaddr_in *v4;
345 struct sockaddr_in6 *v6;
348 struct GNUNET_CONNECTION_Credentials *gcp;
349 struct GNUNET_CONNECTION_Credentials gc;
355 addrlen = sizeof (addr);
357 GNUNET_NETWORK_socket_accept (lsock, (struct sockaddr *) &addr, &addrlen);
360 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "accept");
363 if ( (addrlen > sizeof (addr)) ||
364 (addrlen < sizeof (sa_family_t)) )
367 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
371 sa = (struct sockaddr *) addr;
372 v6 = (struct sockaddr_in6 *) addr;
373 if ((sa->sa_family == AF_INET6) && (IN6_IS_ADDR_V4MAPPED (&v6->sin6_addr)))
375 /* convert to V4 address */
376 v4 = GNUNET_malloc (sizeof (struct sockaddr_in));
377 memset (v4, 0, sizeof (struct sockaddr_in));
378 v4->sin_family = AF_INET;
379 #if HAVE_SOCKADDR_IN_SIN_LEN
380 v4->sin_len = (u_char) sizeof (struct sockaddr_in);
382 memcpy (&v4->sin_addr,
383 &((char *) &v6->sin6_addr)[sizeof (struct in6_addr) -
384 sizeof (struct in_addr)],
385 sizeof (struct in_addr));
386 v4->sin_port = v6->sin6_port;
388 addrlen = sizeof (struct sockaddr_in);
392 uaddr = GNUNET_malloc (addrlen);
393 memcpy (uaddr, addr, addrlen);
398 if (sa->sa_family == AF_UNIX)
402 if (0 == getpeereid (GNUNET_NETWORK_get_fd (sock),
408 /* largely traditional GNU/Linux */
411 getsockopt (GNUNET_NETWORK_get_fd (sock),
412 SOL_SOCKET, SO_PEERCRED, &uc, &olen)) &&
413 (olen == sizeof (uc)) )
420 #if HAVE_GETPEERUCRED
421 /* this is for Solaris 10 */
425 if (0 == getpeerucred (GNUNET_NETWORK_get_fd (sock), &uc))
427 gc.uid = ucred_geteuid (uc);
428 gc.gid = ucred_getegid (uc);
437 if ((access != NULL) &&
438 (GNUNET_YES != (aret = access (access_cls, gcp, uaddr, addrlen))))
440 if (aret == GNUNET_NO)
441 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
442 _("Access denied to `%s'\n"),
443 GNUNET_a2s (uaddr, addrlen));
444 GNUNET_break (GNUNET_OK ==
445 GNUNET_NETWORK_socket_shutdown (sock, SHUT_RDWR));
446 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
450 ret = GNUNET_malloc (sizeof (struct GNUNET_CONNECTION_Handle));
451 ret->write_buffer_size = GNUNET_SERVER_MIN_BUFFER_SIZE;
452 ret->write_buffer = GNUNET_malloc(ret->write_buffer_size);
454 ret->addrlen = addrlen;
457 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
458 _("Accepting connection from `%s': %p\n"),
459 GNUNET_a2s (uaddr, addrlen), ret);
465 * Obtain the network address of the other party.
467 * @param sock the client to get the address for
468 * @param addr where to store the address
469 * @param addrlen where to store the length of the address
470 * @return GNUNET_OK on success
473 GNUNET_CONNECTION_get_address (struct GNUNET_CONNECTION_Handle *sock,
474 void **addr, size_t * addrlen)
476 if ((sock->addr == NULL) || (sock->addrlen == 0))
478 *addr = GNUNET_malloc (sock->addrlen);
479 memcpy (*addr, sock->addr, sock->addrlen);
480 *addrlen = sock->addrlen;
486 * This function is called after establishing a connection either has
487 * succeeded or timed out. Note that it is possible that the attempt
488 * timed out and that we're immediately retrying. If we are retrying,
489 * we need to wait again (or timeout); if we succeeded, we need to
490 * wait for data (or timeout).
492 * @param cls our connection handle
493 * @param tc task context describing why we are here
496 receive_again (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
500 * Scheduler let us know that the connect task is finished (or was
501 * cancelled due to shutdown). Now really clean up.
503 * @param cls our "struct GNUNET_CONNECTION_Handle *"
507 destroy_continuation (void *cls,
508 const struct GNUNET_SCHEDULER_TaskContext *tc)
510 struct GNUNET_CONNECTION_Handle *sock = cls;
511 GNUNET_CONNECTION_TransmitReadyNotify notify;
512 struct AddressProbe *pos;
514 sock->destroy_task = GNUNET_SCHEDULER_NO_TASK;
515 GNUNET_assert (sock->dns_active == NULL);
516 if (0 != (sock->ccs & COCO_TRANSMIT_READY))
519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
520 "Destroy waits for CCS-TR to be done (%p)\n", sock);
522 sock->ccs |= COCO_DESTROY_CONTINUATION;
525 if (sock->write_task != GNUNET_SCHEDULER_NO_TASK)
528 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
529 "Destroy waits for write_task to be done (%p)\n", sock);
531 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task);
533 = GNUNET_SCHEDULER_add_after (sock->write_task,
534 &destroy_continuation, sock);
537 if (0 != (sock->ccs & COCO_RECEIVE_AGAIN))
539 sock->ccs |= COCO_DESTROY_CONTINUATION;
542 if (sock->sock != NULL)
545 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
546 "Shutting down socket (%p)\n", sock);
548 if (sock->persist != GNUNET_YES)
550 if ( (GNUNET_YES != GNUNET_NETWORK_socket_shutdown (sock->sock, SHUT_RDWR)) &&
551 (errno != ENOTCONN) &&
552 (errno != ECONNRESET) )
553 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "shutdown");
556 if (sock->read_task != GNUNET_SCHEDULER_NO_TASK)
558 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task);
560 = GNUNET_SCHEDULER_add_after (sock->read_task,
561 &destroy_continuation, sock);
565 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
566 "Destroy actually runs (%p)!\n", sock);
568 while (NULL != (pos = sock->ap_head))
570 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (pos->sock));
571 GNUNET_SCHEDULER_cancel (pos->task);
572 GNUNET_CONTAINER_DLL_remove (sock->ap_head, sock->ap_tail, pos);
575 GNUNET_assert (sock->nth.timeout_task == GNUNET_SCHEDULER_NO_TASK);
576 GNUNET_assert (sock->ccs == COCO_NONE);
577 if (NULL != (notify = sock->nth.notify_ready))
579 sock->nth.notify_ready = NULL;
580 notify (sock->nth.notify_ready_cls, 0, NULL);
583 if (sock->sock != NULL)
585 if (sock->persist != GNUNET_YES)
586 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock->sock));
588 GNUNET_free (sock->sock); /* at least no memory leak (we deliberately
589 leak the socket in this special case) ... */
591 GNUNET_free_non_null (sock->addr);
592 GNUNET_free_non_null (sock->hostname);
593 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task);
595 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
596 "Freeing memory of connection %p.\n", sock);
598 GNUNET_free (sock->write_buffer);
605 * See if we are now connected. If not, wait longer for
606 * connect to succeed. If connected, we should be able
607 * to write now as well, unless we timed out.
609 * @param cls our connection handle
610 * @param tc task context describing why we are here
613 transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
617 * We've failed for good to establish a connection.
619 * @param h the connection we tried to establish
622 connect_fail_continuation (struct GNUNET_CONNECTION_Handle *h)
625 GNUNET_log ((0 != strncmp (h->hostname,
628 ? GNUNET_ERROR_TYPE_INFO
629 : GNUNET_ERROR_TYPE_WARNING,
630 _("Failed to establish TCP connection to `%s:%u', no further addresses to try.\n"),
631 h->hostname, h->port);
633 /* connect failed / timed out */
634 GNUNET_break (h->ap_head == NULL);
635 GNUNET_break (h->ap_tail == NULL);
636 GNUNET_break (h->dns_active == GNUNET_NO);
637 GNUNET_break (h->sock == NULL);
639 /* trigger jobs that used to wait on "connect_task" */
640 if (0 != (h->ccs & COCO_RECEIVE_AGAIN))
643 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
644 "connect_fail_continuation triggers receive_again (%p)\n",
647 h->ccs -= COCO_RECEIVE_AGAIN;
648 h->read_task = GNUNET_SCHEDULER_add_now (&receive_again, h);
650 if (0 != (h->ccs & COCO_TRANSMIT_READY))
653 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
654 "connect_fail_continuation cancels timeout_task, triggers transmit_ready (%p)\n",
657 GNUNET_assert (h->nth.timeout_task != GNUNET_SCHEDULER_NO_TASK);
658 GNUNET_SCHEDULER_cancel (h->nth.timeout_task);
659 h->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK;
660 h->ccs -= COCO_TRANSMIT_READY;
661 GNUNET_assert (h->write_task == GNUNET_SCHEDULER_NO_TASK);
662 h->write_task = GNUNET_SCHEDULER_add_now (&transmit_ready, h);
664 if (0 != (h->ccs & COCO_DESTROY_CONTINUATION))
667 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
668 "connect_fail_continuation runs destroy_continuation (%p)\n",
671 h->ccs -= COCO_DESTROY_CONTINUATION;
672 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == h->destroy_task);
674 = GNUNET_SCHEDULER_add_now (&destroy_continuation,
681 * We've succeeded in establishing a connection.
683 * @param h the connection we tried to establish
686 connect_success_continuation (struct GNUNET_CONNECTION_Handle *h)
689 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
690 "Connection to `%s' succeeded! (%p)\n",
691 GNUNET_a2s (h->addr, h->addrlen), h);
693 /* trigger jobs that waited for the connection */
694 if (0 != (h->ccs & COCO_RECEIVE_AGAIN))
697 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
698 "connect_success_continuation runs receive_again (%p)\n",
701 h->ccs -= COCO_RECEIVE_AGAIN;
702 h->read_task = GNUNET_SCHEDULER_add_now (&receive_again, h);
704 if (0 != (h->ccs & COCO_TRANSMIT_READY))
707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
708 "connect_success_continuation runs transmit_ready, cancels timeout_task (%p)\n",
711 GNUNET_assert (h->nth.timeout_task != GNUNET_SCHEDULER_NO_TASK);
712 GNUNET_SCHEDULER_cancel (h->nth.timeout_task);
713 h->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK;
714 h->ccs -= COCO_TRANSMIT_READY;
715 GNUNET_assert (h->write_task == GNUNET_SCHEDULER_NO_TASK);
717 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_absolute_get_remaining
718 (h->nth.transmit_timeout), h->sock,
721 if (0 != (h->ccs & COCO_DESTROY_CONTINUATION))
724 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
725 "connect_success_continuation runs destroy_continuation (%p)\n",
728 h->ccs -= COCO_DESTROY_CONTINUATION;
729 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == h->destroy_task);
731 = GNUNET_SCHEDULER_add_now (&destroy_continuation,
738 * Scheduler let us know that we're either ready to write on the
739 * socket OR connect timed out. Do the right thing.
741 * @param cls the "struct AddressProbe*" with the address that we are probing
742 * @param tc success or failure info about the connect attempt.
745 connect_probe_continuation (void *cls,
746 const struct GNUNET_SCHEDULER_TaskContext *tc)
748 struct AddressProbe *ap = cls;
749 struct GNUNET_CONNECTION_Handle *h = ap->h;
750 struct AddressProbe *pos;
754 GNUNET_assert (ap->sock != NULL);
755 GNUNET_CONTAINER_DLL_remove (h->ap_head, h->ap_tail, ap);
756 len = sizeof (error);
759 if ( (0 == (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) ||
761 GNUNET_NETWORK_socket_getsockopt (ap->sock, SOL_SOCKET, SO_ERROR,
765 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ap->sock));
767 if ((NULL == h->ap_head) && (h->dns_active == GNUNET_NO))
768 connect_fail_continuation (h);
771 GNUNET_assert (h->sock == NULL);
773 GNUNET_assert (h->addr == NULL);
774 h->addr = GNUNET_malloc (ap->addrlen);
775 memcpy (h->addr, ap->addr, ap->addrlen);
776 h->addrlen = ap->addrlen;
778 /* cancel all other attempts */
779 while (NULL != (pos = h->ap_head))
781 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (pos->sock));
782 GNUNET_SCHEDULER_cancel (pos->task);
783 GNUNET_CONTAINER_DLL_remove (h->ap_head, h->ap_tail, pos);
786 connect_success_continuation (h);
791 * Try to establish a socket connection given the specified address.
792 * This function is called by the resolver once we have a DNS reply.
794 * @param cls our "struct GNUNET_CONNECTION_Handle *"
795 * @param addr address to try, NULL for "last call"
796 * @param addrlen length of addr
799 try_connect_using_address (void *cls,
800 const struct sockaddr *addr, socklen_t addrlen)
802 struct GNUNET_CONNECTION_Handle *h = cls;
803 struct AddressProbe *ap;
804 struct GNUNET_TIME_Relative delay;
808 h->dns_active = NULL;
809 if (NULL == h->ap_head)
810 connect_fail_continuation (h);
814 return; /* already connected */
815 GNUNET_assert (h->addr == NULL);
818 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
819 "Trying to connect using address `%s:%u/%s:%u'\n",
820 h->hostname, h->port, GNUNET_a2s (addr, addrlen), h->port);
822 ap = GNUNET_malloc (sizeof (struct AddressProbe) + addrlen);
823 ap->addr = (const struct sockaddr *) &ap[1];
824 memcpy (&ap[1], addr, addrlen);
825 ap->addrlen = addrlen;
828 switch (ap->addr->sa_family)
831 ((struct sockaddr_in *) ap->addr)->sin_port = htons (h->port);
834 ((struct sockaddr_in6 *) ap->addr)->sin6_port = htons (h->port);
839 return; /* not supported by us */
842 GNUNET_NETWORK_socket_create (ap->addr->sa_family, SOCK_STREAM, 0);
843 if (ap->sock == NULL)
846 return; /* not supported by OS */
849 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
850 _("Trying to connect to `%s' (%p)\n"),
851 GNUNET_a2s (ap->addr, ap->addrlen), h);
853 if ((GNUNET_OK != GNUNET_NETWORK_socket_connect (ap->sock,
856 (errno != EINPROGRESS))
858 /* maybe refused / unsupported address, try next */
859 GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "connect");
860 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ap->sock));
864 GNUNET_CONTAINER_DLL_insert (h->ap_head, h->ap_tail, ap);
865 delay = GNUNET_CONNECTION_CONNECT_RETRY_TIMEOUT;
866 if (h->nth.notify_ready != NULL)
867 delay = GNUNET_TIME_relative_min (delay,
868 GNUNET_TIME_absolute_get_remaining
869 (h->nth.transmit_timeout));
870 if (h->receiver != NULL)
871 delay = GNUNET_TIME_relative_min (delay,
872 GNUNET_TIME_absolute_get_remaining
873 (h->receive_timeout));
875 GNUNET_SCHEDULER_add_write_net (delay, ap->sock,
876 &connect_probe_continuation, ap);
881 * Create a socket handle by (asynchronously) connecting to a host.
882 * This function returns immediately, even if the connection has not
883 * yet been established. This function only creates TCP connections.
885 * @param cfg configuration to use
886 * @param hostname name of the host to connect to
887 * @param port port to connect to
888 * @return the socket handle
890 struct GNUNET_CONNECTION_Handle *
891 GNUNET_CONNECTION_create_from_connect (const struct
892 GNUNET_CONFIGURATION_Handle *cfg,
893 const char *hostname, uint16_t port)
895 struct GNUNET_CONNECTION_Handle *ret;
897 GNUNET_assert (0 < strlen (hostname)); /* sanity check */
898 ret = GNUNET_malloc (sizeof (struct GNUNET_CONNECTION_Handle));
900 ret->write_buffer_size = GNUNET_SERVER_MIN_BUFFER_SIZE;
901 ret->write_buffer = GNUNET_malloc(ret->write_buffer_size);
903 ret->hostname = GNUNET_strdup (hostname);
904 ret->dns_active = GNUNET_RESOLVER_ip_get (cfg,
907 GNUNET_CONNECTION_CONNECT_RETRY_TIMEOUT,
908 &try_connect_using_address, ret);
914 * Create a socket handle by connecting to a UNIX domain service.
915 * This function returns immediately, even if the connection has not
916 * yet been established. This function only creates UNIX connections.
918 * @param cfg configuration to use
919 * @param unixpath path to connect to
920 * @return the socket handle, NULL on systems without UNIX support
922 struct GNUNET_CONNECTION_Handle *
923 GNUNET_CONNECTION_create_from_connect_to_unixpath (const struct
924 GNUNET_CONFIGURATION_Handle *cfg,
925 const char *unixpath)
928 struct GNUNET_CONNECTION_Handle *ret;
929 struct sockaddr_un *un;
932 GNUNET_assert (0 < strlen (unixpath)); /* sanity check */
933 un = GNUNET_malloc (sizeof (struct sockaddr_un));
934 un->sun_family = AF_UNIX;
935 slen = strlen (unixpath);
936 if (slen >= sizeof (un->sun_path))
937 slen = sizeof (un->sun_path) - 1;
938 memcpy (un->sun_path,
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);
965 if (GNUNET_OK != GNUNET_NETWORK_socket_connect (ret->sock,
969 /* Just return; we expect everything to work eventually so don't fail HARD */
970 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret->sock));
974 connect_success_continuation (ret);
983 * Create a socket handle by (asynchronously) connecting to a host.
984 * This function returns immediately, even if the connection has not
985 * yet been established. This function only creates TCP connections.
987 * @param af_family address family to use
988 * @param serv_addr server address
989 * @param addrlen length of server address
990 * @return the socket handle
992 struct GNUNET_CONNECTION_Handle *
993 GNUNET_CONNECTION_create_from_sockaddr (int af_family,
994 const struct sockaddr *serv_addr,
997 struct GNUNET_NETWORK_Handle *s;
998 struct GNUNET_CONNECTION_Handle *ret;
1001 s = GNUNET_NETWORK_socket_create (af_family, SOCK_STREAM, 0);
1004 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING |
1005 GNUNET_ERROR_TYPE_BULK, "socket");
1008 if ((GNUNET_OK != GNUNET_NETWORK_socket_connect (s, serv_addr, addrlen))
1009 && (errno != EINPROGRESS))
1011 /* maybe refused / unsupported address, try next */
1012 GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "connect");
1013 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (s));
1016 ret = GNUNET_CONNECTION_create_from_existing (s);
1017 ret->addr = GNUNET_malloc (addrlen);
1018 memcpy (ret->addr, serv_addr, addrlen);
1019 ret->addrlen = addrlen;
1020 #if DEBUG_CONNECTION
1021 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1022 _("Trying to connect to `%s' (%p)\n"),
1023 GNUNET_a2s (serv_addr, addrlen), ret);
1030 * Check if socket is valid (no fatal errors have happened so far).
1031 * Note that a socket that is still trying to connect is considered
1034 * @param sock socket to check
1035 * @return GNUNET_YES if valid, GNUNET_NO otherwise
1038 GNUNET_CONNECTION_check (struct GNUNET_CONNECTION_Handle *sock)
1040 if ((sock->ap_head != NULL) || (sock->dns_active != NULL))
1041 return GNUNET_YES; /* still trying to connect */
1042 return (sock->sock == NULL) ? GNUNET_NO : GNUNET_YES;
1047 * Close the socket and free associated resources. Pending
1048 * transmissions may be completed or dropped depending on the
1049 * arguments. If a receive call is pending and should
1050 * NOT be completed, 'GNUNET_CONNECTION_receive_cancel'
1051 * should be called explicitly first.
1053 * @param sock socket to destroy
1054 * @param finish_pending_write should pending writes be completed or aborted?
1055 * (this applies to transmissions where the data has already been
1056 * read from the application; all other transmissions should be
1057 * aborted using 'GNUNET_CONNECTION_notify_transmit_ready_cancel').
1060 GNUNET_CONNECTION_destroy (struct GNUNET_CONNECTION_Handle *sock,
1061 int finish_pending_write)
1063 if (GNUNET_NO == finish_pending_write)
1065 if (sock->write_task != GNUNET_SCHEDULER_NO_TASK)
1067 GNUNET_SCHEDULER_cancel (sock->write_task);
1068 sock->write_task = GNUNET_SCHEDULER_NO_TASK;
1069 sock->write_buffer_off = 0;
1072 if ((sock->write_buffer_off == 0) && (sock->dns_active != NULL))
1074 GNUNET_RESOLVER_request_cancel (sock->dns_active);
1075 sock->dns_active = NULL;
1078 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task);
1080 = GNUNET_SCHEDULER_add_now (&destroy_continuation, sock);
1085 * Tell the receiver callback that a timeout was reached.
1088 signal_timeout (struct GNUNET_CONNECTION_Handle *sh)
1090 GNUNET_CONNECTION_Receiver receiver;
1092 #if DEBUG_CONNECTION
1093 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1094 "Network signals time out to receiver (%p)!\n", sh);
1096 GNUNET_assert (NULL != (receiver = sh->receiver));
1097 sh->receiver = NULL;
1098 receiver (sh->receiver_cls, NULL, 0, NULL, 0, 0);
1103 * Tell the receiver callback that we had an IO error.
1106 signal_error (struct GNUNET_CONNECTION_Handle *sh, int errcode)
1108 GNUNET_CONNECTION_Receiver receiver;
1109 GNUNET_assert (NULL != (receiver = sh->receiver));
1110 sh->receiver = NULL;
1111 receiver (sh->receiver_cls, NULL, 0, sh->addr, sh->addrlen, errcode);
1116 * This function is called once we either timeout
1117 * or have data ready to read.
1120 receive_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1122 struct GNUNET_CONNECTION_Handle *sh = cls;
1123 struct GNUNET_TIME_Absolute now;
1124 char buffer[sh->max];
1126 GNUNET_CONNECTION_Receiver receiver;
1128 sh->read_task = GNUNET_SCHEDULER_NO_TASK;
1129 if ( (GNUNET_YES == sh->ignore_shutdown) &&
1130 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
1132 /* ignore shutdown request, go again immediately */
1133 #if DEBUG_CONNECTION
1134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1135 "Ignoring shutdown signal per configuration\n");
1137 sh->read_task = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_absolute_get_remaining
1138 (sh->receive_timeout),
1140 &receive_ready, sh);
1143 now = GNUNET_TIME_absolute_get ();
1144 if ((now.abs_value > sh->receive_timeout.abs_value) ||
1145 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT)) ||
1146 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
1148 #if DEBUG_CONNECTION
1149 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1150 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1151 "Receive from `%s' encounters error: time out by %llums... (%p)\n",
1152 GNUNET_a2s (sh->addr, sh->addrlen),
1153 GNUNET_TIME_absolute_get_duration (sh->receive_timeout).
1156 signal_timeout (sh);
1159 if (sh->sock == NULL)
1161 /* connect failed for good */
1162 #if DEBUG_CONNECTION
1163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1164 "Receive encounters error, socket closed... (%p)\n", sh);
1166 signal_error (sh, ECONNREFUSED);
1169 GNUNET_assert (GNUNET_NETWORK_fdset_isset (tc->read_ready, sh->sock));
1171 ret = GNUNET_NETWORK_socket_recv (sh->sock, buffer, sh->max);
1176 #if DEBUG_CONNECTION
1177 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1178 "Error receiving: %s\n", STRERROR (errno));
1180 signal_error (sh, errno);
1183 #if DEBUG_CONNECTION
1184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1185 "receive_ready read %u/%u bytes from `%s' (%p)!\n",
1187 sh->max, GNUNET_a2s (sh->addr, sh->addrlen), sh);
1189 GNUNET_assert (NULL != (receiver = sh->receiver));
1190 sh->receiver = NULL;
1191 receiver (sh->receiver_cls, buffer, ret, sh->addr, sh->addrlen, 0);
1196 * This function is called after establishing a connection either has
1197 * succeeded or timed out. Note that it is possible that the attempt
1198 * timed out and that we're immediately retrying. If we are retrying,
1199 * we need to wait again (or timeout); if we succeeded, we need to
1200 * wait for data (or timeout).
1202 * @param cls our connection handle
1203 * @param tc task context describing why we are here
1206 receive_again (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1208 struct GNUNET_CONNECTION_Handle *sh = cls;
1209 struct GNUNET_TIME_Absolute now;
1211 sh->read_task = GNUNET_SCHEDULER_NO_TASK;
1212 if (sh->sock == NULL)
1214 /* not connected and no longer trying */
1215 #if DEBUG_CONNECTION
1216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1217 "Receive encounters error, socket closed (%p)...\n", sh);
1219 signal_error (sh, ECONNREFUSED);
1222 now = GNUNET_TIME_absolute_get ();
1223 if ((now.abs_value > sh->receive_timeout.abs_value) ||
1224 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
1226 #if DEBUG_CONNECTION
1227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1228 "Receive encounters error: time out (%p)...\n", sh);
1230 signal_timeout (sh);
1233 GNUNET_assert (sh->sock != NULL);
1234 /* connect succeeded, wait for data! */
1235 sh->read_task = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_absolute_get_remaining
1236 (sh->receive_timeout),
1238 &receive_ready, sh);
1243 * Receive data from the given socket. Note that this function will
1244 * call "receiver" asynchronously using the scheduler. It will
1245 * "immediately" return. Note that there MUST only be one active
1246 * receive call per socket at any given point in time (so do not
1247 * call receive again until the receiver callback has been invoked).
1249 * @param sock socket handle
1250 * @param max maximum number of bytes to read
1251 * @param timeout maximum amount of time to wait (use -1 for "forever")
1252 * @param receiver function to call with received data
1253 * @param receiver_cls closure for receiver
1256 GNUNET_CONNECTION_receive (struct GNUNET_CONNECTION_Handle *sock,
1258 struct GNUNET_TIME_Relative timeout,
1259 GNUNET_CONNECTION_Receiver receiver,
1262 struct GNUNET_SCHEDULER_TaskContext tc;
1264 GNUNET_assert ((sock->read_task == GNUNET_SCHEDULER_NO_TASK) &&
1265 (0 == (sock->ccs & COCO_RECEIVE_AGAIN)) &&
1266 (sock->receiver == NULL));
1267 sock->receiver = receiver;
1268 sock->receiver_cls = receiver_cls;
1269 sock->receive_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1271 if (sock->sock != NULL)
1273 memset (&tc, 0, sizeof (tc));
1274 tc.reason = GNUNET_SCHEDULER_REASON_PREREQ_DONE;
1275 receive_again (sock, &tc);
1278 if ((sock->dns_active == NULL) && (sock->ap_head == NULL))
1280 receiver (receiver_cls, NULL, 0, NULL, 0, ETIMEDOUT);
1283 sock->ccs += COCO_RECEIVE_AGAIN;
1288 * Configure this connection to ignore shutdown signals.
1290 * @param sock socket handle
1291 * @param do_ignore GNUNET_YES to ignore, GNUNET_NO to restore default
1294 GNUNET_CONNECTION_ignore_shutdown (struct GNUNET_CONNECTION_Handle *sock,
1297 sock->ignore_shutdown = do_ignore;
1302 * Cancel receive job on the given socket. Note that the
1303 * receiver callback must not have been called yet in order
1304 * for the cancellation to be valid.
1306 * @param sock socket handle
1307 * @return closure of the original receiver callback closure
1310 GNUNET_CONNECTION_receive_cancel (struct GNUNET_CONNECTION_Handle *sock)
1312 if (sock->read_task != GNUNET_SCHEDULER_NO_TASK)
1314 GNUNET_assert (sock == GNUNET_SCHEDULER_cancel (sock->read_task));
1315 sock->read_task = GNUNET_SCHEDULER_NO_TASK;
1319 GNUNET_assert (0 != (sock->ccs & COCO_RECEIVE_AGAIN));
1320 sock->ccs -= COCO_RECEIVE_AGAIN;
1322 sock->receiver = NULL;
1323 return sock->receiver_cls;
1328 * Try to call the transmit notify method (check if we do
1329 * have enough space available first)!
1331 * @param sock socket for which we should do this processing
1332 * @return GNUNET_YES if we were able to call notify
1335 process_notify (struct GNUNET_CONNECTION_Handle *sock)
1340 GNUNET_CONNECTION_TransmitReadyNotify notify;
1342 GNUNET_assert (sock->write_task == GNUNET_SCHEDULER_NO_TASK);
1343 if (NULL == (notify = sock->nth.notify_ready))
1345 used = sock->write_buffer_off - sock->write_buffer_pos;
1346 avail = sock->write_buffer_size - used;
1347 size = sock->nth.notify_size;
1350 sock->nth.notify_ready = NULL;
1351 if (sock->write_buffer_size - sock->write_buffer_off < size)
1353 /* need to compact */
1354 memmove (sock->write_buffer,
1355 &sock->write_buffer[sock->write_buffer_pos], used);
1356 sock->write_buffer_off -= sock->write_buffer_pos;
1357 sock->write_buffer_pos = 0;
1359 avail = sock->write_buffer_size - sock->write_buffer_off;
1360 GNUNET_assert (avail >= size);
1361 size = notify (sock->nth.notify_ready_cls,
1363 &sock->write_buffer[sock->write_buffer_off]);
1364 GNUNET_assert (size <= avail);
1365 sock->write_buffer_off += size;
1371 * Task invoked by the scheduler when a call to transmit
1372 * is timing out (we never got enough buffer space to call
1373 * the callback function before the specified timeout
1376 * This task notifies the client about the timeout.
1378 * @param cls the 'struct GNUNET_CONNECTION_Handle'
1379 * @param tc scheduler context
1382 transmit_timeout (void *cls,
1383 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,
1390 "transmit_timeout running (%p)\n", sock);
1392 sock->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK;
1393 #if DEBUG_CONNECTION
1394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1395 "Transmit to `%s:%u/%s' fails, time out reached (%p).\n",
1397 sock->port, GNUNET_a2s (sock->addr, sock->addrlen), sock);
1399 GNUNET_assert (0 != (sock->ccs & COCO_TRANSMIT_READY));
1400 sock->ccs -= COCO_TRANSMIT_READY; /* remove request */
1401 notify = sock->nth.notify_ready;
1402 sock->nth.notify_ready = NULL;
1403 notify (sock->nth.notify_ready_cls, 0, NULL);
1408 * Task invoked by the scheduler when we failed to connect
1409 * at the time of being asked to transmit.
1411 * This task notifies the client about the error.
1413 * @param cls the 'struct GNUNET_CONNECTION_Handle'
1414 * @param tc scheduler context
1417 connect_error (void *cls,
1418 const struct GNUNET_SCHEDULER_TaskContext *tc)
1420 struct GNUNET_CONNECTION_Handle *sock = cls;
1421 GNUNET_CONNECTION_TransmitReadyNotify notify;
1423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1424 "Transmission request of size %u fails (%d/%s/%u), connection failed (%p).\n",
1425 sock->nth.notify_size,
1426 (sock->sock != NULL) ? *(int*) sock->sock : -1,
1430 #if DEBUG_CONNECTION
1431 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1432 "Transmission request of size %u fails, connection failed (%p).\n",
1433 sock->nth.notify_size, sock);
1435 sock->write_task = GNUNET_SCHEDULER_NO_TASK;
1436 notify = sock->nth.notify_ready;
1437 sock->nth.notify_ready = NULL;
1438 notify (sock->nth.notify_ready_cls, 0, NULL);
1448 transmit_error (struct GNUNET_CONNECTION_Handle *sock)
1450 GNUNET_CONNECTION_TransmitReadyNotify notify;
1452 if (NULL != sock->sock)
1454 GNUNET_NETWORK_socket_shutdown (sock->sock, SHUT_RDWR);
1455 GNUNET_break (GNUNET_OK ==
1456 GNUNET_NETWORK_socket_close (sock->sock));
1459 if (sock->read_task != GNUNET_SCHEDULER_NO_TASK)
1461 GNUNET_SCHEDULER_cancel (sock->read_task);
1462 sock->read_task = GNUNET_SCHEDULER_NO_TASK;
1463 signal_timeout (sock);
1466 if (sock->nth.notify_ready == NULL)
1467 return; /* nobody to tell about it */
1468 notify = sock->nth.notify_ready;
1469 sock->nth.notify_ready = NULL;
1470 notify (sock->nth.notify_ready_cls, 0, NULL);
1475 * See if we are now connected. If not, wait longer for
1476 * connect to succeed. If connected, we should be able
1477 * to write now as well, unless we timed out.
1479 * @param cls our connection handle
1480 * @param tc task context describing why we are here
1483 transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1485 struct GNUNET_CONNECTION_Handle *sock = cls;
1486 GNUNET_CONNECTION_TransmitReadyNotify notify;
1490 #if DEBUG_CONNECTION
1491 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1492 "transmit_ready running (%p).\n", sock);
1494 GNUNET_assert (sock->write_task != GNUNET_SCHEDULER_NO_TASK);
1495 sock->write_task = GNUNET_SCHEDULER_NO_TASK;
1496 GNUNET_assert (sock->nth.timeout_task == GNUNET_SCHEDULER_NO_TASK);
1497 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1499 if (sock->ignore_shutdown == GNUNET_YES)
1500 goto SCHEDULE_WRITE; /* ignore shutdown, go again immediately */
1501 #if DEBUG_CONNECTION
1502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1503 "Transmit to `%s' fails, shutdown happened (%p).\n",
1504 GNUNET_a2s (sock->addr, sock->addrlen), sock);
1506 notify = sock->nth.notify_ready;
1507 sock->nth.notify_ready = NULL;
1508 notify (sock->nth.notify_ready_cls, 0, NULL);
1511 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT))
1513 #if DEBUG_CONNECTION
1514 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1515 "Transmit to `%s' fails, time out reached (%p).\n",
1516 GNUNET_a2s (sock->addr, sock->addrlen), sock);
1518 notify = sock->nth.notify_ready;
1519 sock->nth.notify_ready = NULL;
1520 notify (sock->nth.notify_ready_cls, 0, NULL);
1523 GNUNET_assert (NULL != sock->sock);
1524 if (tc->write_ready == NULL)
1526 /* special circumstances (in particular,
1527 PREREQ_DONE after connect): not yet ready to write,
1528 but no "fatal" error either. Hence retry. */
1529 goto SCHEDULE_WRITE;
1531 if (!GNUNET_NETWORK_fdset_isset (tc->write_ready, sock->sock))
1533 #if DEBUG_CONNECTION
1534 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1536 ("Could not satisfy pending transmission request, socket closed or connect failed (%p).\n"),
1539 transmit_error (sock);
1540 return; /* connect failed for good, we're finished */
1542 GNUNET_assert (sock->write_buffer_off >= sock->write_buffer_pos);
1543 if ( (sock->nth.notify_ready != NULL) &&
1544 (sock->write_buffer_size < sock->nth.notify_size) )
1546 sock->write_buffer = GNUNET_realloc(sock->write_buffer,
1547 sock->nth.notify_size);
1548 sock->write_buffer_size = sock->nth.notify_size;
1550 process_notify (sock);
1551 have = sock->write_buffer_off - sock->write_buffer_pos;
1554 /* no data ready for writing, terminate write loop */
1557 GNUNET_assert (have <= sock->write_buffer_size);
1558 GNUNET_assert (have + sock->write_buffer_pos <= sock->write_buffer_size);
1559 GNUNET_assert (sock->write_buffer_pos <= sock->write_buffer_size);
1561 ret = GNUNET_NETWORK_socket_send (sock->sock,
1562 &sock->write_buffer[sock->
1571 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1572 _("Failed to send to `%s': %s\n"),
1573 GNUNET_a2s (sock->addr,
1577 #if DEBUG_CONNECTION
1578 GNUNET_log_strerror (GNUNET_ERROR_TYPE_DEBUG, "send");
1580 transmit_error (sock);
1583 #if DEBUG_CONNECTION
1584 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1585 "transmit_ready transmitted %u/%u bytes to `%s' (%p)\n",
1587 have, GNUNET_a2s (sock->addr, sock->addrlen), sock);
1589 sock->write_buffer_pos += ret;
1590 if (sock->write_buffer_pos == sock->write_buffer_off)
1592 /* transmitted all pending data */
1593 sock->write_buffer_pos = 0;
1594 sock->write_buffer_off = 0;
1596 if ((sock->write_buffer_off == 0) && (NULL == sock->nth.notify_ready))
1597 return; /* all data sent! */
1598 /* not done writing, schedule more */
1600 #if DEBUG_CONNECTION
1601 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1602 "Re-scheduling transmit_ready (more to do) (%p).\n", sock);
1604 if (sock->write_task == GNUNET_SCHEDULER_NO_TASK)
1606 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_absolute_get_remaining
1607 (sock->nth.transmit_timeout),
1608 sock->sock, &transmit_ready, sock);
1613 * Ask the socket to call us once the specified number of bytes
1614 * are free in the transmission buffer. May call the notify
1615 * method immediately if enough space is available.
1617 * @param sock socket
1618 * @param size number of bytes to send
1619 * @param timeout after how long should we give up (and call
1620 * notify with buf NULL and size 0)?
1621 * @param notify function to call
1622 * @param notify_cls closure for notify
1623 * @return non-NULL if the notify callback was queued,
1624 * NULL if we are already going to notify someone else (busy)
1626 struct GNUNET_CONNECTION_TransmitHandle *
1627 GNUNET_CONNECTION_notify_transmit_ready (struct GNUNET_CONNECTION_Handle
1629 struct GNUNET_TIME_Relative timeout,
1630 GNUNET_CONNECTION_TransmitReadyNotify
1631 notify, void *notify_cls)
1633 if (sock->nth.notify_ready != NULL)
1638 GNUNET_assert (notify != NULL);
1639 GNUNET_assert (size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
1640 GNUNET_assert (sock->write_buffer_off <= sock->write_buffer_size);
1641 GNUNET_assert (sock->write_buffer_pos <= sock->write_buffer_size);
1642 GNUNET_assert (sock->write_buffer_pos <= sock->write_buffer_off);
1643 sock->nth.notify_ready = notify;
1644 sock->nth.notify_ready_cls = notify_cls;
1645 sock->nth.sh = sock;
1646 sock->nth.notify_size = size;
1647 sock->nth.transmit_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1648 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->nth.timeout_task);
1649 if ((sock->sock == NULL) &&
1650 (sock->ap_head == NULL) && (sock->dns_active == NULL))
1652 if (sock->write_task != GNUNET_SCHEDULER_NO_TASK)
1653 GNUNET_SCHEDULER_cancel (sock->write_task);
1654 sock->write_task = GNUNET_SCHEDULER_add_now (&connect_error, sock);
1657 if (GNUNET_SCHEDULER_NO_TASK != sock->write_task)
1659 if (sock->sock != NULL)
1661 #if DEBUG_CONNECTION
1662 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1663 "Scheduling transmit_ready (%p).\n", sock);
1665 sock->write_task = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_absolute_get_remaining
1674 #if DEBUG_CONNECTION
1675 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1676 "CCS-Scheduling transmit_ready, adding timeout task (%p).\n",
1679 sock->ccs |= COCO_TRANSMIT_READY;
1680 sock->nth.timeout_task = GNUNET_SCHEDULER_add_delayed (timeout,
1689 * Cancel the specified transmission-ready
1693 GNUNET_CONNECTION_notify_transmit_ready_cancel (struct
1694 GNUNET_CONNECTION_TransmitHandle
1697 GNUNET_assert (h->notify_ready != NULL);
1698 if (0 != (h->sh->ccs & COCO_TRANSMIT_READY))
1700 #if DEBUG_CONNECTION
1701 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1702 "notify_transmit_ready_cancel cancels timeout_task (%p)\n",
1705 GNUNET_SCHEDULER_cancel (h->timeout_task);
1706 h->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1707 h->sh->ccs -= COCO_TRANSMIT_READY;
1711 if (h->sh->write_task != GNUNET_SCHEDULER_NO_TASK)
1713 GNUNET_SCHEDULER_cancel (h->sh->write_task);
1714 h->sh->write_task = GNUNET_SCHEDULER_NO_TASK;
1717 h->notify_ready = NULL;
1720 /* end of connection.c */