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 memcpy (&v4->sin_addr,
380 &((char *) &v6->sin6_addr)[sizeof (struct in6_addr) -
381 sizeof (struct in_addr)],
382 sizeof (struct in_addr));
383 v4->sin_port = v6->sin6_port;
385 addrlen = sizeof (struct sockaddr_in);
389 uaddr = GNUNET_malloc (addrlen);
390 memcpy (uaddr, addr, addrlen);
395 if (sa->sa_family == AF_UNIX)
399 if (0 == getpeereid (GNUNET_NETWORK_get_fd (sock),
405 /* largely traditional GNU/Linux */
408 getsockopt (GNUNET_NETWORK_get_fd (sock),
409 SOL_SOCKET, SO_PEERCRED, &uc, &olen)) &&
410 (olen == sizeof (uc)) )
417 #if HAVE_GETPEERUCRED
418 /* this is for Solaris 10 */
422 if (0 == getpeerucred (GNUNET_NETWORK_get_fd (sock), &uc))
424 gc.uid = ucred_geteuid (uc);
425 gc.gid = ucred_getegid (uc);
434 if ((access != NULL) &&
435 (GNUNET_YES != (aret = access (access_cls, gcp, uaddr, addrlen))))
437 if (aret == GNUNET_NO)
438 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
439 _("Access denied to `%s'\n"),
440 GNUNET_a2s (uaddr, addrlen));
441 GNUNET_break (GNUNET_OK ==
442 GNUNET_NETWORK_socket_shutdown (sock, SHUT_RDWR));
443 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
447 ret = GNUNET_malloc (sizeof (struct GNUNET_CONNECTION_Handle));
448 ret->write_buffer_size = GNUNET_SERVER_MIN_BUFFER_SIZE;
449 ret->write_buffer = GNUNET_malloc(ret->write_buffer_size);
451 ret->addrlen = addrlen;
454 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
455 _("Accepting connection from `%s': %p\n"),
456 GNUNET_a2s (uaddr, addrlen), ret);
462 * Obtain the network address of the other party.
464 * @param sock the client to get the address for
465 * @param addr where to store the address
466 * @param addrlen where to store the length of the address
467 * @return GNUNET_OK on success
470 GNUNET_CONNECTION_get_address (struct GNUNET_CONNECTION_Handle *sock,
471 void **addr, size_t * addrlen)
473 if ((sock->addr == NULL) || (sock->addrlen == 0))
475 *addr = GNUNET_malloc (sock->addrlen);
476 memcpy (*addr, sock->addr, sock->addrlen);
477 *addrlen = sock->addrlen;
483 * This function is called after establishing a connection either has
484 * succeeded or timed out. Note that it is possible that the attempt
485 * timed out and that we're immediately retrying. If we are retrying,
486 * we need to wait again (or timeout); if we succeeded, we need to
487 * wait for data (or timeout).
489 * @param cls our connection handle
490 * @param tc task context describing why we are here
493 receive_again (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
497 * Scheduler let us know that the connect task is finished (or was
498 * cancelled due to shutdown). Now really clean up.
500 * @param cls our "struct GNUNET_CONNECTION_Handle *"
504 destroy_continuation (void *cls,
505 const struct GNUNET_SCHEDULER_TaskContext *tc)
507 struct GNUNET_CONNECTION_Handle *sock = cls;
508 GNUNET_CONNECTION_TransmitReadyNotify notify;
509 struct AddressProbe *pos;
511 sock->destroy_task = GNUNET_SCHEDULER_NO_TASK;
512 GNUNET_assert (sock->dns_active == NULL);
513 if (0 != (sock->ccs & COCO_TRANSMIT_READY))
516 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
517 "Destroy waits for CCS-TR to be done (%p)\n", sock);
519 sock->ccs |= COCO_DESTROY_CONTINUATION;
522 if (sock->write_task != GNUNET_SCHEDULER_NO_TASK)
525 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
526 "Destroy waits for write_task to be done (%p)\n", sock);
528 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task);
530 = GNUNET_SCHEDULER_add_after (sock->write_task,
531 &destroy_continuation, sock);
534 if (0 != (sock->ccs & COCO_RECEIVE_AGAIN))
536 sock->ccs |= COCO_DESTROY_CONTINUATION;
539 if (sock->sock != NULL)
542 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
543 "Shutting down socket (%p)\n", sock);
545 if (sock->persist != GNUNET_YES)
547 if ( (GNUNET_YES != GNUNET_NETWORK_socket_shutdown (sock->sock, SHUT_RDWR)) &&
548 (errno != ENOTCONN) &&
549 (errno != ECONNRESET) )
550 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "shutdown");
553 if (sock->read_task != GNUNET_SCHEDULER_NO_TASK)
555 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task);
557 = GNUNET_SCHEDULER_add_after (sock->read_task,
558 &destroy_continuation, sock);
562 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
563 "Destroy actually runs (%p)!\n", sock);
565 while (NULL != (pos = sock->ap_head))
567 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (pos->sock));
568 GNUNET_SCHEDULER_cancel (pos->task);
569 GNUNET_CONTAINER_DLL_remove (sock->ap_head, sock->ap_tail, pos);
572 GNUNET_assert (sock->nth.timeout_task == GNUNET_SCHEDULER_NO_TASK);
573 GNUNET_assert (sock->ccs == COCO_NONE);
574 if (NULL != (notify = sock->nth.notify_ready))
576 sock->nth.notify_ready = NULL;
577 notify (sock->nth.notify_ready_cls, 0, NULL);
580 if (sock->sock != NULL)
582 if (sock->persist != GNUNET_YES)
583 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock->sock));
585 GNUNET_free (sock->sock); /* at least no memory leak (we deliberately
586 leak the socket in this special case) ... */
588 GNUNET_free_non_null (sock->addr);
589 GNUNET_free_non_null (sock->hostname);
590 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task);
592 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
593 "Freeing memory of connection %p.\n", sock);
595 GNUNET_free (sock->write_buffer);
602 * See if we are now connected. If not, wait longer for
603 * connect to succeed. If connected, we should be able
604 * to write now as well, unless we timed out.
606 * @param cls our connection handle
607 * @param tc task context describing why we are here
610 transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
614 * We've failed for good to establish a connection.
616 * @param h the connection we tried to establish
619 connect_fail_continuation (struct GNUNET_CONNECTION_Handle *h)
622 GNUNET_log ((0 != strncmp (h->hostname,
625 ? GNUNET_ERROR_TYPE_INFO
626 : GNUNET_ERROR_TYPE_WARNING,
627 _("Failed to establish TCP connection to `%s:%u', no further addresses to try.\n"),
628 h->hostname, h->port);
630 /* connect failed / timed out */
631 GNUNET_break (h->ap_head == NULL);
632 GNUNET_break (h->ap_tail == NULL);
633 GNUNET_break (h->dns_active == GNUNET_NO);
634 GNUNET_break (h->sock == NULL);
636 /* trigger jobs that used to wait on "connect_task" */
637 if (0 != (h->ccs & COCO_RECEIVE_AGAIN))
640 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
641 "connect_fail_continuation triggers receive_again (%p)\n",
644 h->ccs -= COCO_RECEIVE_AGAIN;
645 h->read_task = GNUNET_SCHEDULER_add_after (GNUNET_SCHEDULER_NO_TASK,
648 if (0 != (h->ccs & COCO_TRANSMIT_READY))
651 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
652 "connect_fail_continuation cancels timeout_task, triggers transmit_ready (%p)\n",
655 GNUNET_assert (h->nth.timeout_task != GNUNET_SCHEDULER_NO_TASK);
656 GNUNET_SCHEDULER_cancel (h->nth.timeout_task);
657 h->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK;
658 h->ccs -= COCO_TRANSMIT_READY;
659 GNUNET_assert (h->write_task == GNUNET_SCHEDULER_NO_TASK);
660 h->write_task = GNUNET_SCHEDULER_add_after (GNUNET_SCHEDULER_NO_TASK,
663 if (0 != (h->ccs & COCO_DESTROY_CONTINUATION))
666 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
667 "connect_fail_continuation runs destroy_continuation (%p)\n",
670 h->ccs -= COCO_DESTROY_CONTINUATION;
671 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == h->destroy_task);
673 = GNUNET_SCHEDULER_add_now (&destroy_continuation,
680 * We've succeeded in establishing a connection.
682 * @param h the connection we tried to establish
685 connect_success_continuation (struct GNUNET_CONNECTION_Handle *h)
688 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
689 "Connection to `%s' succeeded! (%p)\n",
690 GNUNET_a2s (h->addr, h->addrlen), h);
692 /* trigger jobs that waited for the connection */
693 if (0 != (h->ccs & COCO_RECEIVE_AGAIN))
696 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
697 "connect_success_continuation runs receive_again (%p)\n",
700 h->ccs -= COCO_RECEIVE_AGAIN;
701 h->read_task = GNUNET_SCHEDULER_add_after (GNUNET_SCHEDULER_NO_TASK,
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) + 1;
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 (sa_family_t);
944 un->sun_path[0] = '\0';
945 slen = sizeof (struct sockaddr_un);
947 ret = GNUNET_malloc (sizeof (struct GNUNET_CONNECTION_Handle));
949 ret->write_buffer_size = GNUNET_SERVER_MIN_BUFFER_SIZE;
950 ret->write_buffer = GNUNET_malloc(ret->write_buffer_size);
952 ret->hostname = NULL;
953 ret->addr = (struct sockaddr*) un;
955 ret->sock = GNUNET_NETWORK_socket_create (AF_UNIX, SOCK_STREAM, 0);
956 if (NULL == ret->sock)
958 GNUNET_free (ret->addr);
959 GNUNET_free (ret->write_buffer);
963 if (GNUNET_OK != GNUNET_NETWORK_socket_connect (ret->sock,
967 /* Just return; we expect everything to work eventually so don't fail HARD */
968 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret->sock));
972 connect_success_continuation (ret);
981 * Create a socket handle by (asynchronously) connecting to a host.
982 * This function returns immediately, even if the connection has not
983 * yet been established. This function only creates TCP connections.
985 * @param af_family address family to use
986 * @param serv_addr server address
987 * @param addrlen length of server address
988 * @return the socket handle
990 struct GNUNET_CONNECTION_Handle *
991 GNUNET_CONNECTION_create_from_sockaddr (int af_family,
992 const struct sockaddr *serv_addr,
995 struct GNUNET_NETWORK_Handle *s;
996 struct GNUNET_CONNECTION_Handle *ret;
999 s = GNUNET_NETWORK_socket_create (af_family, SOCK_STREAM, 0);
1002 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING |
1003 GNUNET_ERROR_TYPE_BULK, "socket");
1006 if ((GNUNET_OK != GNUNET_NETWORK_socket_connect (s, serv_addr, addrlen))
1007 && (errno != EINPROGRESS))
1009 /* maybe refused / unsupported address, try next */
1010 GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "connect");
1011 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (s));
1014 ret = GNUNET_CONNECTION_create_from_existing (s);
1015 ret->addr = GNUNET_malloc (addrlen);
1016 memcpy (ret->addr, serv_addr, addrlen);
1017 ret->addrlen = addrlen;
1018 #if DEBUG_CONNECTION
1019 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1020 _("Trying to connect to `%s' (%p)\n"),
1021 GNUNET_a2s (serv_addr, addrlen), ret);
1028 * Check if socket is valid (no fatal errors have happened so far).
1029 * Note that a socket that is still trying to connect is considered
1032 * @param sock socket to check
1033 * @return GNUNET_YES if valid, GNUNET_NO otherwise
1036 GNUNET_CONNECTION_check (struct GNUNET_CONNECTION_Handle *sock)
1038 if ((sock->ap_head != NULL) || (sock->dns_active != NULL))
1039 return GNUNET_YES; /* still trying to connect */
1040 return (sock->sock == NULL) ? GNUNET_NO : GNUNET_YES;
1045 * Close the socket and free associated resources. Pending
1046 * transmissions may be completed or dropped depending on the
1047 * arguments. If a receive call is pending and should
1048 * NOT be completed, 'GNUNET_CONNECTION_receive_cancel'
1049 * should be called explicitly first.
1051 * @param sock socket to destroy
1052 * @param finish_pending_write should pending writes be completed or aborted?
1053 * (this applies to transmissions where the data has already been
1054 * read from the application; all other transmissions should be
1055 * aborted using 'GNUNET_CONNECTION_notify_transmit_ready_cancel').
1058 GNUNET_CONNECTION_destroy (struct GNUNET_CONNECTION_Handle *sock,
1059 int finish_pending_write)
1061 if (GNUNET_NO == finish_pending_write)
1063 if (sock->write_task != GNUNET_SCHEDULER_NO_TASK)
1065 GNUNET_SCHEDULER_cancel (sock->write_task);
1066 sock->write_task = GNUNET_SCHEDULER_NO_TASK;
1067 sock->write_buffer_off = 0;
1070 if ((sock->write_buffer_off == 0) && (sock->dns_active != NULL))
1072 GNUNET_RESOLVER_request_cancel (sock->dns_active);
1073 sock->dns_active = NULL;
1076 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task);
1078 = GNUNET_SCHEDULER_add_now (&destroy_continuation, sock);
1083 * Tell the receiver callback that a timeout was reached.
1086 signal_timeout (struct GNUNET_CONNECTION_Handle *sh)
1088 GNUNET_CONNECTION_Receiver receiver;
1090 #if DEBUG_CONNECTION
1091 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1092 "Network signals time out to receiver (%p)!\n", sh);
1094 GNUNET_assert (NULL != (receiver = sh->receiver));
1095 sh->receiver = NULL;
1096 receiver (sh->receiver_cls, NULL, 0, NULL, 0, 0);
1101 * Tell the receiver callback that we had an IO error.
1104 signal_error (struct GNUNET_CONNECTION_Handle *sh, int errcode)
1106 GNUNET_CONNECTION_Receiver receiver;
1107 GNUNET_assert (NULL != (receiver = sh->receiver));
1108 sh->receiver = NULL;
1109 receiver (sh->receiver_cls, NULL, 0, sh->addr, sh->addrlen, errcode);
1114 * This function is called once we either timeout
1115 * or have data ready to read.
1118 receive_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1120 struct GNUNET_CONNECTION_Handle *sh = cls;
1121 struct GNUNET_TIME_Absolute now;
1122 char buffer[sh->max];
1124 GNUNET_CONNECTION_Receiver receiver;
1126 sh->read_task = GNUNET_SCHEDULER_NO_TASK;
1127 if ( (GNUNET_YES == sh->ignore_shutdown) &&
1128 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
1130 /* ignore shutdown request, go again immediately */
1131 #if DEBUG_CONNECTION
1132 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1133 "Ignoring shutdown signal per configuration\n");
1135 sh->read_task = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_absolute_get_remaining
1136 (sh->receive_timeout),
1138 &receive_ready, sh);
1141 now = GNUNET_TIME_absolute_get ();
1142 if ((now.abs_value > sh->receive_timeout.abs_value) ||
1143 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT)) ||
1144 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
1146 #if DEBUG_CONNECTION
1147 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1148 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1149 "Receive from `%s' encounters error: time out by %llums... (%p)\n",
1150 GNUNET_a2s (sh->addr, sh->addrlen),
1151 GNUNET_TIME_absolute_get_duration (sh->receive_timeout).
1154 signal_timeout (sh);
1157 if (sh->sock == NULL)
1159 /* connect failed for good */
1160 #if DEBUG_CONNECTION
1161 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1162 "Receive encounters error, socket closed... (%p)\n", sh);
1164 signal_error (sh, ECONNREFUSED);
1167 GNUNET_assert (GNUNET_NETWORK_fdset_isset (tc->read_ready, sh->sock));
1169 ret = GNUNET_NETWORK_socket_recv (sh->sock, buffer, sh->max);
1174 #if DEBUG_CONNECTION
1175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1176 "Error receiving: %s\n", STRERROR (errno));
1178 signal_error (sh, errno);
1181 #if DEBUG_CONNECTION
1182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1183 "receive_ready read %u/%u bytes from `%s' (%p)!\n",
1185 sh->max, GNUNET_a2s (sh->addr, sh->addrlen), sh);
1187 GNUNET_assert (NULL != (receiver = sh->receiver));
1188 sh->receiver = NULL;
1189 receiver (sh->receiver_cls, buffer, ret, sh->addr, sh->addrlen, 0);
1194 * This function is called after establishing a connection either has
1195 * succeeded or timed out. Note that it is possible that the attempt
1196 * timed out and that we're immediately retrying. If we are retrying,
1197 * we need to wait again (or timeout); if we succeeded, we need to
1198 * wait for data (or timeout).
1200 * @param cls our connection handle
1201 * @param tc task context describing why we are here
1204 receive_again (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1206 struct GNUNET_CONNECTION_Handle *sh = cls;
1207 struct GNUNET_TIME_Absolute now;
1209 sh->read_task = GNUNET_SCHEDULER_NO_TASK;
1210 if (sh->sock == NULL)
1212 /* not connected and no longer trying */
1213 #if DEBUG_CONNECTION
1214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1215 "Receive encounters error, socket closed (%p)...\n", sh);
1217 signal_error (sh, ECONNREFUSED);
1220 now = GNUNET_TIME_absolute_get ();
1221 if ((now.abs_value > sh->receive_timeout.abs_value) ||
1222 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
1224 #if DEBUG_CONNECTION
1225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1226 "Receive encounters error: time out (%p)...\n", sh);
1228 signal_timeout (sh);
1231 GNUNET_assert (sh->sock != NULL);
1232 /* connect succeeded, wait for data! */
1233 sh->read_task = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_absolute_get_remaining
1234 (sh->receive_timeout),
1236 &receive_ready, sh);
1241 * Receive data from the given socket. Note that this function will
1242 * call "receiver" asynchronously using the scheduler. It will
1243 * "immediately" return. Note that there MUST only be one active
1244 * receive call per socket at any given point in time (so do not
1245 * call receive again until the receiver callback has been invoked).
1247 * @param sock socket handle
1248 * @param max maximum number of bytes to read
1249 * @param timeout maximum amount of time to wait (use -1 for "forever")
1250 * @param receiver function to call with received data
1251 * @param receiver_cls closure for receiver
1254 GNUNET_CONNECTION_receive (struct GNUNET_CONNECTION_Handle *sock,
1256 struct GNUNET_TIME_Relative timeout,
1257 GNUNET_CONNECTION_Receiver receiver,
1260 struct GNUNET_SCHEDULER_TaskContext tc;
1262 GNUNET_assert ((sock->read_task == GNUNET_SCHEDULER_NO_TASK) &&
1263 (0 == (sock->ccs & COCO_RECEIVE_AGAIN)) &&
1264 (sock->receiver == NULL));
1265 sock->receiver = receiver;
1266 sock->receiver_cls = receiver_cls;
1267 sock->receive_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1269 if (sock->sock != NULL)
1271 memset (&tc, 0, sizeof (tc));
1272 tc.reason = GNUNET_SCHEDULER_REASON_PREREQ_DONE;
1273 receive_again (sock, &tc);
1276 if ((sock->dns_active == NULL) && (sock->ap_head == NULL))
1278 receiver (receiver_cls, NULL, 0, NULL, 0, ETIMEDOUT);
1281 sock->ccs += COCO_RECEIVE_AGAIN;
1286 * Configure this connection to ignore shutdown signals.
1288 * @param sock socket handle
1289 * @param do_ignore GNUNET_YES to ignore, GNUNET_NO to restore default
1292 GNUNET_CONNECTION_ignore_shutdown (struct GNUNET_CONNECTION_Handle *sock,
1295 sock->ignore_shutdown = do_ignore;
1300 * Cancel receive job on the given socket. Note that the
1301 * receiver callback must not have been called yet in order
1302 * for the cancellation to be valid.
1304 * @param sock socket handle
1305 * @return closure of the original receiver callback closure
1308 GNUNET_CONNECTION_receive_cancel (struct GNUNET_CONNECTION_Handle *sock)
1310 if (sock->read_task != GNUNET_SCHEDULER_NO_TASK)
1312 GNUNET_assert (sock == GNUNET_SCHEDULER_cancel (sock->read_task));
1313 sock->read_task = GNUNET_SCHEDULER_NO_TASK;
1317 GNUNET_assert (0 != (sock->ccs & COCO_RECEIVE_AGAIN));
1318 sock->ccs -= COCO_RECEIVE_AGAIN;
1320 sock->receiver = NULL;
1321 return sock->receiver_cls;
1326 * Try to call the transmit notify method (check if we do
1327 * have enough space available first)!
1329 * @param sock socket for which we should do this processing
1330 * @return GNUNET_YES if we were able to call notify
1333 process_notify (struct GNUNET_CONNECTION_Handle *sock)
1338 GNUNET_CONNECTION_TransmitReadyNotify notify;
1340 GNUNET_assert (sock->write_task == GNUNET_SCHEDULER_NO_TASK);
1341 if (NULL == (notify = sock->nth.notify_ready))
1343 used = sock->write_buffer_off - sock->write_buffer_pos;
1344 avail = sock->write_buffer_size - used;
1345 size = sock->nth.notify_size;
1348 sock->nth.notify_ready = NULL;
1349 if (sock->write_buffer_size - sock->write_buffer_off < size)
1351 /* need to compact */
1352 memmove (sock->write_buffer,
1353 &sock->write_buffer[sock->write_buffer_pos], used);
1354 sock->write_buffer_off -= sock->write_buffer_pos;
1355 sock->write_buffer_pos = 0;
1357 avail = sock->write_buffer_size - sock->write_buffer_off;
1358 GNUNET_assert (avail >= size);
1359 size = notify (sock->nth.notify_ready_cls,
1361 &sock->write_buffer[sock->write_buffer_off]);
1362 GNUNET_assert (size <= avail);
1363 sock->write_buffer_off += size;
1369 * Task invoked by the scheduler when a call to transmit
1370 * is timing out (we never got enough buffer space to call
1371 * the callback function before the specified timeout
1374 * This task notifies the client about the timeout.
1376 * @param cls the 'struct GNUNET_CONNECTION_Handle'
1377 * @param tc scheduler context
1380 transmit_timeout (void *cls,
1381 const struct GNUNET_SCHEDULER_TaskContext *tc)
1383 struct GNUNET_CONNECTION_Handle *sock = cls;
1384 GNUNET_CONNECTION_TransmitReadyNotify notify;
1386 #if DEBUG_CONNECTION
1387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1388 "transmit_timeout running (%p)\n", sock);
1390 sock->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK;
1391 #if DEBUG_CONNECTION
1392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1393 "Transmit to `%s:%u/%s' fails, time out reached (%p).\n",
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,
1416 const struct GNUNET_SCHEDULER_TaskContext *tc)
1418 struct GNUNET_CONNECTION_Handle *sock = cls;
1419 GNUNET_CONNECTION_TransmitReadyNotify notify;
1421 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1422 "Transmission request of size %u fails (%s/%u), connection failed (%p).\n",
1423 sock->nth.notify_size,
1427 #if DEBUG_CONNECTION
1428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1429 "Transmission request of size %u fails, connection failed (%p).\n",
1430 sock->nth.notify_size, sock);
1432 sock->write_task = GNUNET_SCHEDULER_NO_TASK;
1433 notify = sock->nth.notify_ready;
1434 sock->nth.notify_ready = NULL;
1435 notify (sock->nth.notify_ready_cls, 0, NULL);
1445 transmit_error (struct GNUNET_CONNECTION_Handle *sock)
1447 GNUNET_CONNECTION_TransmitReadyNotify notify;
1449 if (NULL != sock->sock)
1451 GNUNET_NETWORK_socket_shutdown (sock->sock, SHUT_RDWR);
1452 GNUNET_break (GNUNET_OK ==
1453 GNUNET_NETWORK_socket_close (sock->sock));
1456 if (sock->read_task != GNUNET_SCHEDULER_NO_TASK)
1458 GNUNET_SCHEDULER_cancel (sock->read_task);
1459 sock->read_task = GNUNET_SCHEDULER_NO_TASK;
1460 signal_timeout (sock);
1463 if (sock->nth.notify_ready == NULL)
1464 return; /* nobody to tell about it */
1465 notify = sock->nth.notify_ready;
1466 sock->nth.notify_ready = NULL;
1467 notify (sock->nth.notify_ready_cls, 0, NULL);
1472 * See if we are now connected. If not, wait longer for
1473 * connect to succeed. If connected, we should be able
1474 * to write now as well, unless we timed out.
1476 * @param cls our connection handle
1477 * @param tc task context describing why we are here
1480 transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1482 struct GNUNET_CONNECTION_Handle *sock = cls;
1483 GNUNET_CONNECTION_TransmitReadyNotify notify;
1487 #if DEBUG_CONNECTION
1488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1489 "transmit_ready running (%p).\n", sock);
1491 GNUNET_assert (sock->write_task != GNUNET_SCHEDULER_NO_TASK);
1492 sock->write_task = GNUNET_SCHEDULER_NO_TASK;
1493 GNUNET_assert (sock->nth.timeout_task == GNUNET_SCHEDULER_NO_TASK);
1494 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1496 if (sock->ignore_shutdown == GNUNET_YES)
1497 goto SCHEDULE_WRITE; /* ignore shutdown, go again immediately */
1498 #if DEBUG_CONNECTION
1499 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1500 "Transmit to `%s' fails, shutdown happened (%p).\n",
1501 GNUNET_a2s (sock->addr, sock->addrlen), sock);
1503 notify = sock->nth.notify_ready;
1504 sock->nth.notify_ready = NULL;
1505 notify (sock->nth.notify_ready_cls, 0, NULL);
1508 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT))
1510 #if DEBUG_CONNECTION
1511 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1512 "Transmit to `%s' fails, time out reached (%p).\n",
1513 GNUNET_a2s (sock->addr, sock->addrlen), sock);
1515 notify = sock->nth.notify_ready;
1516 sock->nth.notify_ready = NULL;
1517 notify (sock->nth.notify_ready_cls, 0, NULL);
1520 GNUNET_assert (NULL != sock->sock);
1521 if (tc->write_ready == NULL)
1523 /* special circumstances (in particular,
1524 PREREQ_DONE after connect): not yet ready to write,
1525 but no "fatal" error either. Hence retry. */
1526 goto SCHEDULE_WRITE;
1528 if (!GNUNET_NETWORK_fdset_isset (tc->write_ready, sock->sock))
1530 #if DEBUG_CONNECTION
1531 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1533 ("Could not satisfy pending transmission request, socket closed or connect failed (%p).\n"),
1536 transmit_error (sock);
1537 return; /* connect failed for good, we're finished */
1539 GNUNET_assert (sock->write_buffer_off >= sock->write_buffer_pos);
1540 if ( (sock->nth.notify_ready != NULL) &&
1541 (sock->write_buffer_size < sock->nth.notify_size) )
1543 sock->write_buffer = GNUNET_realloc(sock->write_buffer,
1544 sock->nth.notify_size);
1545 sock->write_buffer_size = sock->nth.notify_size;
1547 process_notify (sock);
1548 have = sock->write_buffer_off - sock->write_buffer_pos;
1551 /* no data ready for writing, terminate write loop */
1554 GNUNET_assert (have <= sock->write_buffer_size);
1555 GNUNET_assert (have + sock->write_buffer_pos <= sock->write_buffer_size);
1556 GNUNET_assert (sock->write_buffer_pos <= sock->write_buffer_size);
1558 ret = GNUNET_NETWORK_socket_send (sock->sock,
1559 &sock->write_buffer[sock->
1568 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1569 _("Failed to send to `%s': %s\n"),
1570 GNUNET_a2s (sock->addr,
1574 #if DEBUG_CONNECTION
1575 GNUNET_log_strerror (GNUNET_ERROR_TYPE_DEBUG, "send");
1577 transmit_error (sock);
1580 #if DEBUG_CONNECTION
1581 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1582 "transmit_ready transmitted %u/%u bytes to `%s' (%p)\n",
1584 have, GNUNET_a2s (sock->addr, sock->addrlen), sock);
1586 sock->write_buffer_pos += ret;
1587 if (sock->write_buffer_pos == sock->write_buffer_off)
1589 /* transmitted all pending data */
1590 sock->write_buffer_pos = 0;
1591 sock->write_buffer_off = 0;
1593 if ((sock->write_buffer_off == 0) && (NULL == sock->nth.notify_ready))
1594 return; /* all data sent! */
1595 /* not done writing, schedule more */
1597 #if DEBUG_CONNECTION
1598 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1599 "Re-scheduling transmit_ready (more to do) (%p).\n", sock);
1601 if (sock->write_task == GNUNET_SCHEDULER_NO_TASK)
1603 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_absolute_get_remaining
1604 (sock->nth.transmit_timeout),
1605 sock->sock, &transmit_ready, sock);
1610 * Ask the socket to call us once the specified number of bytes
1611 * are free in the transmission buffer. May call the notify
1612 * method immediately if enough space is available.
1614 * @param sock socket
1615 * @param size number of bytes to send
1616 * @param timeout after how long should we give up (and call
1617 * notify with buf NULL and size 0)?
1618 * @param notify function to call
1619 * @param notify_cls closure for notify
1620 * @return non-NULL if the notify callback was queued,
1621 * NULL if we are already going to notify someone else (busy)
1623 struct GNUNET_CONNECTION_TransmitHandle *
1624 GNUNET_CONNECTION_notify_transmit_ready (struct GNUNET_CONNECTION_Handle
1626 struct GNUNET_TIME_Relative timeout,
1627 GNUNET_CONNECTION_TransmitReadyNotify
1628 notify, void *notify_cls)
1630 if (sock->nth.notify_ready != NULL)
1632 GNUNET_assert (notify != NULL);
1633 GNUNET_assert (size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
1634 GNUNET_assert (sock->write_buffer_off <= sock->write_buffer_size);
1635 GNUNET_assert (sock->write_buffer_pos <= sock->write_buffer_size);
1636 GNUNET_assert (sock->write_buffer_pos <= sock->write_buffer_off);
1637 sock->nth.notify_ready = notify;
1638 sock->nth.notify_ready_cls = notify_cls;
1639 sock->nth.sh = sock;
1640 sock->nth.notify_size = size;
1641 sock->nth.transmit_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1642 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->nth.timeout_task);
1643 if ((sock->sock == NULL) &&
1644 (sock->ap_head == NULL) && (sock->dns_active == NULL))
1646 if (sock->write_task != GNUNET_SCHEDULER_NO_TASK)
1647 GNUNET_SCHEDULER_cancel (sock->write_task);
1648 sock->write_task = GNUNET_SCHEDULER_add_now (&connect_error, sock);
1651 if (GNUNET_SCHEDULER_NO_TASK != sock->write_task)
1653 if (sock->sock != NULL)
1655 #if DEBUG_CONNECTION
1656 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1657 "Scheduling transmit_ready (%p).\n", sock);
1659 sock->write_task = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_absolute_get_remaining
1668 #if DEBUG_CONNECTION
1669 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1670 "CCS-Scheduling transmit_ready, adding timeout task (%p).\n",
1673 sock->ccs |= COCO_TRANSMIT_READY;
1674 sock->nth.timeout_task = GNUNET_SCHEDULER_add_delayed (timeout,
1683 * Cancel the specified transmission-ready
1687 GNUNET_CONNECTION_notify_transmit_ready_cancel (struct
1688 GNUNET_CONNECTION_TransmitHandle
1691 GNUNET_assert (h->notify_ready != NULL);
1692 if (0 != (h->sh->ccs & COCO_TRANSMIT_READY))
1694 #if DEBUG_CONNECTION
1695 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1696 "notify_transmit_ready_cancel cancels timeout_task (%p)\n",
1699 GNUNET_SCHEDULER_cancel (h->timeout_task);
1700 h->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1701 h->sh->ccs -= COCO_TRANSMIT_READY;
1705 if (h->sh->write_task != GNUNET_SCHEDULER_NO_TASK)
1707 GNUNET_SCHEDULER_cancel (h->sh->write_task);
1708 h->sh->write_task = GNUNET_SCHEDULER_NO_TASK;
1711 h->notify_ready = NULL;
1714 /* end of connection.c */