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
46 * Possible functions to call after connect failed or succeeded.
48 enum ConnectContinuations
56 * Call "receive_again".
58 COCO_RECEIVE_AGAIN = 1,
61 * Call "transmit_ready".
63 COCO_TRANSMIT_READY = 2,
66 * Call "destroy_continuation".
68 COCO_DESTROY_CONTINUATION = 4
73 * Transmission handle. There can only be one for each connection.
75 struct GNUNET_CONNECTION_TransmitHandle
79 * Function to call if the send buffer has notify_size
82 GNUNET_CONNECTION_TransmitReadyNotify notify_ready;
85 * Closure for notify_ready.
87 void *notify_ready_cls;
92 struct GNUNET_CONNECTION_Handle *sh;
95 * Timeout for receiving (in absolute time).
97 struct GNUNET_TIME_Absolute transmit_timeout;
100 * Task called on timeout.
102 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
105 * At what number of bytes available in the
106 * write buffer should the notify method be called?
114 * During connect, we try multiple possible IP addresses
115 * to find out which one might work.
121 * This is a linked list.
123 struct AddressProbe *next;
126 * This is a doubly-linked list.
128 struct AddressProbe *prev;
131 * The address; do not free (allocated at the end of this struct).
133 const struct sockaddr *addr;
136 * Underlying OS's socket.
138 struct GNUNET_NETWORK_Handle *sock;
141 * Connection for which we are probing.
143 struct GNUNET_CONNECTION_Handle *h;
151 * Task waiting for the socket to finish connecting.
153 GNUNET_SCHEDULER_TaskIdentifier task;
158 * @brief handle for a network socket
160 struct GNUNET_CONNECTION_Handle
164 * Scheduler that was used for the connect task.
166 struct GNUNET_SCHEDULER_Handle *sched;
169 * Configuration to use.
171 const struct GNUNET_CONFIGURATION_Handle *cfg;
174 * Linked list of sockets we are currently trying out
177 struct AddressProbe *ap_head;
180 * Linked list of sockets we are currently trying out
183 struct AddressProbe *ap_tail;
186 * Network address of the other end-point, may be NULL.
188 struct sockaddr *addr;
191 * Pointer to the hostname if socket was
192 * created using DNS lookup, otherwise NULL.
197 * Underlying OS's socket, set to NULL after fatal errors.
199 struct GNUNET_NETWORK_Handle *sock;
202 * Function to call on data received, NULL if no receive is pending.
204 GNUNET_CONNECTION_Receiver receiver;
207 * Closure for receiver.
212 * Pointer to our write buffer.
217 * Current size of our write buffer.
219 size_t write_buffer_size;
222 * Current write-offset in write buffer (where
223 * would we write next).
225 size_t write_buffer_off;
228 * Current read-offset in write buffer (how many
229 * bytes have already been sent).
231 size_t write_buffer_pos;
239 * Read task that we may need to wait for.
241 GNUNET_SCHEDULER_TaskIdentifier read_task;
244 * Write task that we may need to wait for.
246 GNUNET_SCHEDULER_TaskIdentifier write_task;
249 * Destroy task (if already scheduled).
251 GNUNET_SCHEDULER_TaskIdentifier destroy_task;
254 * Handle to a pending DNS lookup request.
256 struct GNUNET_RESOLVER_RequestHandle *dns_active;
259 * The handle we return for GNUNET_CONNECTION_notify_transmit_ready.
261 struct GNUNET_CONNECTION_TransmitHandle nth;
264 * Timeout for receiving (in absolute time).
266 struct GNUNET_TIME_Absolute receive_timeout;
269 * Functions to call after connect failed or succeeded.
271 enum ConnectContinuations ccs;
274 * Maximum number of bytes to read (for receiving).
279 * Ignore GNUNET_SCHEDULER_REASON_SHUTDOWN for this socket.
284 * Port to connect to.
289 * When shutdown, do not ever actually close the socket, but
290 * free resources. Only should ever be set if using program
291 * termination as a signal (because only then will the leaked
299 * Set the persist option on this connection handle. Indicates
300 * that the underlying socket or fd should never really be closed.
301 * Used for indicating process death.
303 * @param sock the connection to set persistent
305 void GNUNET_CONNECTION_persist_(struct GNUNET_CONNECTION_Handle *sock)
307 sock->persist = GNUNET_YES;
311 * Create a socket handle by boxing an existing OS socket. The OS
312 * socket should henceforth be no longer used directly.
313 * GNUNET_socket_destroy will close it.
315 * @param sched scheduler to use
316 * @param osSocket existing socket to box
317 * @return the boxed socket handle
319 struct GNUNET_CONNECTION_Handle *
320 GNUNET_CONNECTION_create_from_existing (struct GNUNET_SCHEDULER_Handle
322 struct GNUNET_NETWORK_Handle
325 struct GNUNET_CONNECTION_Handle *ret;
326 ret = GNUNET_malloc (sizeof (struct GNUNET_CONNECTION_Handle));
327 ret->write_buffer_size = GNUNET_SERVER_MIN_BUFFER_SIZE;
328 ret->write_buffer = GNUNET_malloc(ret->write_buffer_size);
329 ret->sock = osSocket;
336 * Create a socket handle by accepting on a listen socket. This
337 * function may block if the listen socket has no connection ready.
339 * @param sched scheduler to use
340 * @param access function to use to check if access is allowed
341 * @param access_cls closure for access
342 * @param lsock listen socket
343 * @return the socket handle, NULL on error
345 struct GNUNET_CONNECTION_Handle *
346 GNUNET_CONNECTION_create_from_accept (struct GNUNET_SCHEDULER_Handle
348 GNUNET_CONNECTION_AccessCheck access,
350 struct GNUNET_NETWORK_Handle *lsock)
352 struct GNUNET_CONNECTION_Handle *ret;
355 struct GNUNET_NETWORK_Handle *sock;
357 struct sockaddr_in *v4;
358 struct sockaddr_in6 *v6;
362 addrlen = sizeof (addr);
364 GNUNET_NETWORK_socket_accept (lsock, (struct sockaddr *) &addr, &addrlen);
367 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "accept");
370 if ( (addrlen > sizeof (addr)) ||
371 (addrlen < sizeof (sa_family_t)) )
374 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
378 sa = (struct sockaddr *) addr;
379 v6 = (struct sockaddr_in6 *) addr;
380 if ((sa->sa_family == AF_INET6) && (IN6_IS_ADDR_V4MAPPED (&v6->sin6_addr)))
382 /* convert to V4 address */
383 v4 = GNUNET_malloc (sizeof (struct sockaddr_in));
384 memset (v4, 0, sizeof (struct sockaddr_in));
385 v4->sin_family = AF_INET;
386 memcpy (&v4->sin_addr,
387 &((char *) &v6->sin6_addr)[sizeof (struct in6_addr) -
388 sizeof (struct in_addr)],
389 sizeof (struct in_addr));
390 v4->sin_port = v6->sin6_port;
392 addrlen = sizeof (struct sockaddr_in);
396 uaddr = GNUNET_malloc (addrlen);
397 memcpy (uaddr, addr, addrlen);
400 if ((access != NULL) &&
401 (GNUNET_YES != (aret = access (access_cls, uaddr, addrlen))))
403 if (aret == GNUNET_NO)
404 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
405 _("Access denied to `%s'\n"),
406 GNUNET_a2s (uaddr, addrlen));
407 GNUNET_break (GNUNET_OK ==
408 GNUNET_NETWORK_socket_shutdown (sock, SHUT_RDWR));
409 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
413 ret = GNUNET_malloc (sizeof (struct GNUNET_CONNECTION_Handle));
414 ret->write_buffer_size = GNUNET_SERVER_MIN_BUFFER_SIZE;
415 ret->write_buffer = GNUNET_malloc(ret->write_buffer_size);
417 ret->addrlen = addrlen;
421 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
422 _("Accepting connection from `%s': %p\n"),
423 GNUNET_a2s (uaddr, addrlen), ret);
429 * Obtain the network address of the other party.
431 * @param sock the client to get the address for
432 * @param addr where to store the address
433 * @param addrlen where to store the length of the address
434 * @return GNUNET_OK on success
437 GNUNET_CONNECTION_get_address (struct GNUNET_CONNECTION_Handle *sock,
438 void **addr, size_t * addrlen)
440 if ((sock->addr == NULL) || (sock->addrlen == 0))
442 *addr = GNUNET_malloc (sock->addrlen);
443 memcpy (*addr, sock->addr, sock->addrlen);
444 *addrlen = sock->addrlen;
450 * This function is called after establishing a connection either has
451 * succeeded or timed out. Note that it is possible that the attempt
452 * timed out and that we're immediately retrying. If we are retrying,
453 * we need to wait again (or timeout); if we succeeded, we need to
454 * wait for data (or timeout).
456 * @param cls our connection handle
457 * @param tc task context describing why we are here
460 receive_again (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
464 * Scheduler let us know that the connect task is finished (or was
465 * cancelled due to shutdown). Now really clean up.
467 * @param cls our "struct GNUNET_CONNECTION_Handle *"
471 destroy_continuation (void *cls,
472 const struct GNUNET_SCHEDULER_TaskContext *tc)
474 struct GNUNET_CONNECTION_Handle *sock = cls;
475 GNUNET_CONNECTION_TransmitReadyNotify notify;
476 struct AddressProbe *pos;
478 sock->destroy_task = GNUNET_SCHEDULER_NO_TASK;
479 GNUNET_assert (sock->dns_active == NULL);
480 if (0 != (sock->ccs & COCO_TRANSMIT_READY))
483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
484 "Destroy waits for CCS-TR to be done (%p)\n", sock);
486 sock->ccs |= COCO_DESTROY_CONTINUATION;
489 if (sock->write_task != GNUNET_SCHEDULER_NO_TASK)
492 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
493 "Destroy waits for write_task to be done (%p)\n", sock);
495 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task);
497 = GNUNET_SCHEDULER_add_after (sock->sched,
499 &destroy_continuation, sock);
502 if (0 != (sock->ccs & COCO_RECEIVE_AGAIN))
504 sock->ccs |= COCO_DESTROY_CONTINUATION;
507 if (sock->sock != NULL)
510 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
511 "Shutting down socket (%p)\n", sock);
513 if (sock->persist != GNUNET_YES)
514 GNUNET_NETWORK_socket_shutdown (sock->sock, SHUT_RDWR);
516 if (sock->read_task != GNUNET_SCHEDULER_NO_TASK)
518 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task);
520 = GNUNET_SCHEDULER_add_after (sock->sched,
522 &destroy_continuation, sock);
526 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
527 "Destroy actually runs (%p)!\n", sock);
529 while (NULL != (pos = sock->ap_head))
531 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (pos->sock));
532 GNUNET_SCHEDULER_cancel (sock->sched, pos->task);
533 GNUNET_CONTAINER_DLL_remove (sock->ap_head, sock->ap_tail, pos);
536 GNUNET_assert (sock->nth.timeout_task == GNUNET_SCHEDULER_NO_TASK);
537 GNUNET_assert (sock->ccs == COCO_NONE);
538 if (NULL != (notify = sock->nth.notify_ready))
540 sock->nth.notify_ready = NULL;
541 notify (sock->nth.notify_ready_cls, 0, NULL);
544 if (sock->sock != NULL)
546 if (sock->persist != GNUNET_YES)
547 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock->sock));
549 GNUNET_free (sock->sock); /* at least no memory leak (we deliberately
550 leak the socket in this special case) ... */
552 GNUNET_free_non_null (sock->addr);
553 GNUNET_free_non_null (sock->hostname);
554 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task);
556 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
557 "Freeing memory of connection %p.\n", sock);
559 GNUNET_free (sock->write_buffer);
566 * See if we are now connected. If not, wait longer for
567 * connect to succeed. If connected, we should be able
568 * to write now as well, unless we timed out.
570 * @param cls our connection handle
571 * @param tc task context describing why we are here
574 transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
578 * We've failed for good to establish a connection.
580 * @param h the connection we tried to establish
583 connect_fail_continuation (struct GNUNET_CONNECTION_Handle *h)
586 GNUNET_log ((0 != strncmp (h->hostname,
589 ? GNUNET_ERROR_TYPE_INFO
590 : GNUNET_ERROR_TYPE_WARNING,
591 _("Failed to establish TCP connection to `%s:%u', no further addresses to try.\n"),
592 h->hostname, h->port);
594 /* connect failed / timed out */
595 GNUNET_break (h->ap_head == NULL);
596 GNUNET_break (h->ap_tail == NULL);
597 GNUNET_break (h->dns_active == GNUNET_NO);
598 GNUNET_break (h->sock == NULL);
600 /* trigger jobs that used to wait on "connect_task" */
601 if (0 != (h->ccs & COCO_RECEIVE_AGAIN))
604 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
605 "connect_fail_continuation triggers receive_again (%p)\n",
608 h->ccs -= COCO_RECEIVE_AGAIN;
609 h->read_task = GNUNET_SCHEDULER_add_after (h->sched,
610 GNUNET_SCHEDULER_NO_TASK,
613 if (0 != (h->ccs & COCO_TRANSMIT_READY))
616 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
617 "connect_fail_continuation cancels timeout_task, triggers transmit_ready (%p)\n",
620 GNUNET_assert (h->nth.timeout_task != GNUNET_SCHEDULER_NO_TASK);
621 GNUNET_SCHEDULER_cancel (h->sched, h->nth.timeout_task);
622 h->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK;
623 h->ccs -= COCO_TRANSMIT_READY;
624 GNUNET_assert (h->write_task == GNUNET_SCHEDULER_NO_TASK);
625 h->write_task = GNUNET_SCHEDULER_add_after (h->sched,
626 GNUNET_SCHEDULER_NO_TASK,
629 if (0 != (h->ccs & COCO_DESTROY_CONTINUATION))
632 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
633 "connect_fail_continuation runs destroy_continuation (%p)\n",
636 h->ccs -= COCO_DESTROY_CONTINUATION;
637 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == h->destroy_task);
639 = GNUNET_SCHEDULER_add_now (h->sched,
640 &destroy_continuation,
647 * We've succeeded in establishing a connection.
649 * @param h the connection we tried to establish
652 connect_success_continuation (struct GNUNET_CONNECTION_Handle *h)
655 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
656 "Connection to `%s' succeeded! (%p)\n",
657 GNUNET_a2s (h->addr, h->addrlen), h);
659 /* trigger jobs that waited for the connection */
660 if (0 != (h->ccs & COCO_RECEIVE_AGAIN))
663 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
664 "connect_success_continuation runs receive_again (%p)\n",
667 h->ccs -= COCO_RECEIVE_AGAIN;
668 h->read_task = GNUNET_SCHEDULER_add_after (h->sched,
669 GNUNET_SCHEDULER_NO_TASK,
672 if (0 != (h->ccs & COCO_TRANSMIT_READY))
675 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
676 "connect_success_continuation runs transmit_ready, cancels timeout_task (%p)\n",
679 GNUNET_assert (h->nth.timeout_task != GNUNET_SCHEDULER_NO_TASK);
680 GNUNET_SCHEDULER_cancel (h->sched, h->nth.timeout_task);
681 h->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK;
682 h->ccs -= COCO_TRANSMIT_READY;
683 GNUNET_assert (h->write_task == GNUNET_SCHEDULER_NO_TASK);
685 GNUNET_SCHEDULER_add_write_net (h->sched,
686 GNUNET_TIME_absolute_get_remaining
687 (h->nth.transmit_timeout), h->sock,
690 if (0 != (h->ccs & COCO_DESTROY_CONTINUATION))
693 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
694 "connect_success_continuation runs destroy_continuation (%p)\n",
697 h->ccs -= COCO_DESTROY_CONTINUATION;
698 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == h->destroy_task);
700 = GNUNET_SCHEDULER_add_now (h->sched,
701 &destroy_continuation,
708 * Scheduler let us know that we're either ready to write on the
709 * socket OR connect timed out. Do the right thing.
711 * @param cls the "struct AddressProbe*" with the address that we are probing
712 * @param tc success or failure info about the connect attempt.
715 connect_probe_continuation (void *cls,
716 const struct GNUNET_SCHEDULER_TaskContext *tc)
718 struct AddressProbe *ap = cls;
719 struct GNUNET_CONNECTION_Handle *h = ap->h;
720 struct AddressProbe *pos;
724 GNUNET_assert (ap->sock != NULL);
725 GNUNET_CONTAINER_DLL_remove (h->ap_head, h->ap_tail, ap);
726 len = sizeof (error);
729 if ( (0 == (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) ||
731 GNUNET_NETWORK_socket_getsockopt (ap->sock, SOL_SOCKET, SO_ERROR,
735 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ap->sock));
737 if ((NULL == h->ap_head) && (h->dns_active == GNUNET_NO))
738 connect_fail_continuation (h);
741 GNUNET_assert (h->sock == NULL);
743 GNUNET_assert (h->addr == NULL);
744 h->addr = GNUNET_malloc (ap->addrlen);
745 memcpy (h->addr, ap->addr, ap->addrlen);
746 h->addrlen = ap->addrlen;
748 /* cancel all other attempts */
749 while (NULL != (pos = h->ap_head))
751 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (pos->sock));
752 GNUNET_SCHEDULER_cancel (h->sched, pos->task);
753 GNUNET_CONTAINER_DLL_remove (h->ap_head, h->ap_tail, pos);
756 connect_success_continuation (h);
761 * Try to establish a socket connection given the specified address.
762 * This function is called by the resolver once we have a DNS reply.
764 * @param cls our "struct GNUNET_CONNECTION_Handle *"
765 * @param addr address to try, NULL for "last call"
766 * @param addrlen length of addr
769 try_connect_using_address (void *cls,
770 const struct sockaddr *addr, socklen_t addrlen)
772 struct GNUNET_CONNECTION_Handle *h = cls;
773 struct AddressProbe *ap;
774 struct GNUNET_TIME_Relative delay;
778 h->dns_active = NULL;
779 if (NULL == h->ap_head)
780 connect_fail_continuation (h);
784 return; /* already connected */
785 GNUNET_assert (h->addr == NULL);
788 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
789 "Trying to connect using address `%s:%u/%s:%u'\n",
790 h->hostname, h->port, GNUNET_a2s (addr, addrlen), h->port);
792 ap = GNUNET_malloc (sizeof (struct AddressProbe) + addrlen);
793 ap->addr = (const struct sockaddr *) &ap[1];
794 memcpy (&ap[1], addr, addrlen);
795 ap->addrlen = addrlen;
798 switch (ap->addr->sa_family)
801 ((struct sockaddr_in *) ap->addr)->sin_port = htons (h->port);
804 ((struct sockaddr_in6 *) ap->addr)->sin6_port = htons (h->port);
809 return; /* not supported by us */
812 GNUNET_NETWORK_socket_create (ap->addr->sa_family, SOCK_STREAM, 0);
813 if (ap->sock == NULL)
816 return; /* not supported by OS */
819 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
820 _("Trying to connect to `%s' (%p)\n"),
821 GNUNET_a2s (ap->addr, ap->addrlen), h);
823 if ((GNUNET_OK != GNUNET_NETWORK_socket_connect (ap->sock,
826 (errno != EINPROGRESS))
828 /* maybe refused / unsupported address, try next */
829 GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "connect");
830 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ap->sock));
834 GNUNET_CONTAINER_DLL_insert (h->ap_head, h->ap_tail, ap);
835 delay = GNUNET_CONNECTION_CONNECT_RETRY_TIMEOUT;
836 if (h->nth.notify_ready != NULL)
837 delay = GNUNET_TIME_relative_min (delay,
838 GNUNET_TIME_absolute_get_remaining
839 (h->nth.transmit_timeout));
840 if (h->receiver != NULL)
841 delay = GNUNET_TIME_relative_min (delay,
842 GNUNET_TIME_absolute_get_remaining
843 (h->receive_timeout));
845 GNUNET_SCHEDULER_add_write_net (h->sched, delay, ap->sock,
846 &connect_probe_continuation, ap);
851 * Create a socket handle by (asynchronously) connecting to a host.
852 * This function returns immediately, even if the connection has not
853 * yet been established. This function only creates TCP connections.
855 * @param sched scheduler to use
856 * @param cfg configuration to use
857 * @param hostname name of the host to connect to
858 * @param port port to connect to
859 * @return the socket handle
861 struct GNUNET_CONNECTION_Handle *
862 GNUNET_CONNECTION_create_from_connect (struct GNUNET_SCHEDULER_Handle *sched,
864 GNUNET_CONFIGURATION_Handle *cfg,
865 const char *hostname, uint16_t port)
867 struct GNUNET_CONNECTION_Handle *ret;
869 GNUNET_assert (0 < strlen (hostname)); /* sanity check */
870 ret = GNUNET_malloc (sizeof (struct GNUNET_CONNECTION_Handle));
873 ret->write_buffer_size = GNUNET_SERVER_MIN_BUFFER_SIZE;
874 ret->write_buffer = GNUNET_malloc(ret->write_buffer_size);
876 ret->hostname = GNUNET_strdup (hostname);
877 ret->dns_active = GNUNET_RESOLVER_ip_get (sched,
881 GNUNET_CONNECTION_CONNECT_RETRY_TIMEOUT,
882 &try_connect_using_address, ret);
888 * Create a socket handle by connecting to a UNIX domain service.
889 * This function returns immediately, even if the connection has not
890 * yet been established. This function only creates UNIX connections.
892 * @param sched scheduler to use
893 * @param cfg configuration to use
894 * @param unixpath path to connect to
895 * @return the socket handle, NULL on systems without UNIX support
897 struct GNUNET_CONNECTION_Handle *
898 GNUNET_CONNECTION_create_from_connect_to_unixpath (struct GNUNET_SCHEDULER_Handle *sched,
900 GNUNET_CONFIGURATION_Handle *cfg,
901 const char *unixpath)
904 struct GNUNET_CONNECTION_Handle *ret;
905 struct sockaddr_un *un;
908 GNUNET_assert (0 < strlen (unixpath)); /* sanity check */
909 un = GNUNET_malloc (sizeof (struct sockaddr_un));
910 un->sun_family = AF_UNIX;
911 slen = strlen (unixpath) + 1;
912 if (slen >= sizeof (un->sun_path))
913 slen = sizeof (un->sun_path) - 1;
914 memcpy (un->sun_path,
917 un->sun_path[slen] = '\0';
918 slen += sizeof (sa_family_t);
920 un->sun_path[0] = '\0';
921 slen = sizeof (struct sockaddr_un);
923 ret = GNUNET_malloc (sizeof (struct GNUNET_CONNECTION_Handle));
926 ret->write_buffer_size = GNUNET_SERVER_MIN_BUFFER_SIZE;
927 ret->write_buffer = GNUNET_malloc(ret->write_buffer_size);
929 ret->hostname = NULL;
930 ret->addr = (struct sockaddr*) un;
932 ret->sock = GNUNET_NETWORK_socket_create (AF_UNIX, SOCK_STREAM, 0);
933 if (NULL == ret->sock)
935 GNUNET_free (ret->addr);
936 GNUNET_free (ret->write_buffer);
940 if (GNUNET_OK != GNUNET_NETWORK_socket_connect (ret->sock,
944 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret->sock));
945 GNUNET_free (ret->addr);
946 GNUNET_free (ret->write_buffer);
950 connect_success_continuation (ret);
959 * Create a socket handle by (asynchronously) connecting to a host.
960 * This function returns immediately, even if the connection has not
961 * yet been established. This function only creates TCP connections.
963 * @param sched scheduler to use
964 * @param af_family address family to use
965 * @param serv_addr server address
966 * @param addrlen length of server address
967 * @return the socket handle
969 struct GNUNET_CONNECTION_Handle *
970 GNUNET_CONNECTION_create_from_sockaddr (struct GNUNET_SCHEDULER_Handle
971 *sched, int af_family,
972 const struct sockaddr *serv_addr,
975 struct GNUNET_NETWORK_Handle *s;
976 struct GNUNET_CONNECTION_Handle *ret;
978 s = GNUNET_NETWORK_socket_create (af_family, SOCK_STREAM, 0);
981 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING |
982 GNUNET_ERROR_TYPE_BULK, "socket");
985 if ((GNUNET_OK != GNUNET_NETWORK_socket_connect (s, serv_addr, addrlen))
986 && (errno != EINPROGRESS))
988 /* maybe refused / unsupported address, try next */
989 GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "connect");
990 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (s));
993 ret = GNUNET_CONNECTION_create_from_existing (sched, s);
994 ret->addr = GNUNET_malloc (addrlen);
995 memcpy (ret->addr, serv_addr, addrlen);
996 ret->addrlen = addrlen;
998 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
999 _("Trying to connect to `%s' (%p)\n"),
1000 GNUNET_a2s (serv_addr, addrlen), ret);
1007 * Check if socket is valid (no fatal errors have happened so far).
1008 * Note that a socket that is still trying to connect is considered
1011 * @param sock socket to check
1012 * @return GNUNET_YES if valid, GNUNET_NO otherwise
1015 GNUNET_CONNECTION_check (struct GNUNET_CONNECTION_Handle *sock)
1017 if ((sock->ap_head != NULL) || (sock->dns_active != NULL))
1018 return GNUNET_YES; /* still trying to connect */
1019 return (sock->sock == NULL) ? GNUNET_NO : GNUNET_YES;
1024 * Close the socket and free associated resources. Pending
1025 * transmissions may be completed or dropped depending on the
1026 * arguments. If a receive call is pending and should
1027 * NOT be completed, 'GNUNET_CONNECTION_receive_cancel'
1028 * should be called explicitly first.
1030 * @param sock socket to destroy
1031 * @param finish_pending_write should pending writes be completed or aborted?
1032 * (this applies to transmissions where the data has already been
1033 * read from the application; all other transmissions should be
1034 * aborted using 'GNUNET_CONNECTION_notify_transmit_ready_cancel').
1037 GNUNET_CONNECTION_destroy (struct GNUNET_CONNECTION_Handle *sock,
1038 int finish_pending_write)
1040 if (GNUNET_NO == finish_pending_write)
1042 if (sock->write_task != GNUNET_SCHEDULER_NO_TASK)
1044 GNUNET_SCHEDULER_cancel (sock->sched,
1046 sock->write_task = GNUNET_SCHEDULER_NO_TASK;
1047 sock->write_buffer_off = 0;
1050 if ((sock->write_buffer_off == 0) && (sock->dns_active != NULL))
1052 GNUNET_RESOLVER_request_cancel (sock->dns_active);
1053 sock->dns_active = NULL;
1055 GNUNET_assert (sock->sched != NULL);
1056 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task);
1058 = GNUNET_SCHEDULER_add_now (sock->sched,
1059 &destroy_continuation, sock);
1064 * Tell the receiver callback that a timeout was reached.
1067 signal_timeout (struct GNUNET_CONNECTION_Handle *sh)
1069 GNUNET_CONNECTION_Receiver receiver;
1071 #if DEBUG_CONNECTION
1072 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1073 "Network signals time out to receiver (%p)!\n", sh);
1075 GNUNET_assert (NULL != (receiver = sh->receiver));
1076 sh->receiver = NULL;
1077 receiver (sh->receiver_cls, NULL, 0, NULL, 0, 0);
1082 * Tell the receiver callback that we had an IO error.
1085 signal_error (struct GNUNET_CONNECTION_Handle *sh, int errcode)
1087 GNUNET_CONNECTION_Receiver receiver;
1088 GNUNET_assert (NULL != (receiver = sh->receiver));
1089 sh->receiver = NULL;
1090 receiver (sh->receiver_cls, NULL, 0, sh->addr, sh->addrlen, errcode);
1095 * This function is called once we either timeout
1096 * or have data ready to read.
1099 receive_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1101 struct GNUNET_CONNECTION_Handle *sh = cls;
1102 struct GNUNET_TIME_Absolute now;
1103 char buffer[sh->max];
1105 GNUNET_CONNECTION_Receiver receiver;
1107 sh->read_task = GNUNET_SCHEDULER_NO_TASK;
1108 if ( (GNUNET_YES == sh->ignore_shutdown) &&
1109 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
1111 /* ignore shutdown request, go again immediately */
1112 #if DEBUG_CONNECTION
1113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1114 "Ignoring shutdown signal per configuration\n");
1116 sh->read_task = GNUNET_SCHEDULER_add_read_net (tc->sched,
1117 GNUNET_TIME_absolute_get_remaining
1118 (sh->receive_timeout),
1120 &receive_ready, sh);
1123 now = GNUNET_TIME_absolute_get ();
1124 if ((now.value > sh->receive_timeout.value) ||
1125 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT)) ||
1126 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
1128 #if DEBUG_CONNECTION
1129 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1131 "Receive from `%s' encounters error: time out by %llums... (%p)\n",
1132 GNUNET_a2s (sh->addr, sh->addrlen),
1133 GNUNET_TIME_absolute_get_duration (sh->receive_timeout).
1136 signal_timeout (sh);
1139 if (sh->sock == NULL)
1141 /* connect failed for good */
1142 #if DEBUG_CONNECTION
1143 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1144 "Receive encounters error, socket closed... (%p)\n", sh);
1146 signal_error (sh, ECONNREFUSED);
1149 GNUNET_assert (GNUNET_NETWORK_fdset_isset (tc->read_ready, sh->sock));
1151 ret = GNUNET_NETWORK_socket_recv (sh->sock, buffer, sh->max);
1156 #if DEBUG_CONNECTION
1157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1158 "Error receiving: %s\n", STRERROR (errno));
1160 signal_error (sh, errno);
1163 #if DEBUG_CONNECTION
1164 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1165 "receive_ready read %u/%u bytes from `%s' (%p)!\n",
1167 sh->max, GNUNET_a2s (sh->addr, sh->addrlen), sh);
1169 GNUNET_assert (NULL != (receiver = sh->receiver));
1170 sh->receiver = NULL;
1171 receiver (sh->receiver_cls, buffer, ret, sh->addr, sh->addrlen, 0);
1176 * This function is called after establishing a connection either has
1177 * succeeded or timed out. Note that it is possible that the attempt
1178 * timed out and that we're immediately retrying. If we are retrying,
1179 * we need to wait again (or timeout); if we succeeded, we need to
1180 * wait for data (or timeout).
1182 * @param cls our connection handle
1183 * @param tc task context describing why we are here
1186 receive_again (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1188 struct GNUNET_CONNECTION_Handle *sh = cls;
1189 struct GNUNET_TIME_Absolute now;
1191 sh->read_task = GNUNET_SCHEDULER_NO_TASK;
1192 if (sh->sock == NULL)
1194 /* not connected and no longer trying */
1195 #if DEBUG_CONNECTION
1196 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1197 "Receive encounters error, socket closed (%p)...\n", sh);
1199 signal_error (sh, ECONNREFUSED);
1202 now = GNUNET_TIME_absolute_get ();
1203 if ((now.value > sh->receive_timeout.value) ||
1204 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
1206 #if DEBUG_CONNECTION
1207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1208 "Receive encounters error: time out (%p)...\n", sh);
1210 signal_timeout (sh);
1213 GNUNET_assert (sh->sock != NULL);
1214 /* connect succeeded, wait for data! */
1215 sh->read_task = GNUNET_SCHEDULER_add_read_net (tc->sched,
1216 GNUNET_TIME_absolute_get_remaining
1217 (sh->receive_timeout),
1219 &receive_ready, sh);
1224 * Receive data from the given socket. Note that this function will
1225 * call "receiver" asynchronously using the scheduler. It will
1226 * "immediately" return. Note that there MUST only be one active
1227 * receive call per socket at any given point in time (so do not
1228 * call receive again until the receiver callback has been invoked).
1230 * @param sock socket handle
1231 * @param max maximum number of bytes to read
1232 * @param timeout maximum amount of time to wait (use -1 for "forever")
1233 * @param receiver function to call with received data
1234 * @param receiver_cls closure for receiver
1237 GNUNET_CONNECTION_receive (struct GNUNET_CONNECTION_Handle *sock,
1239 struct GNUNET_TIME_Relative timeout,
1240 GNUNET_CONNECTION_Receiver receiver,
1243 struct GNUNET_SCHEDULER_TaskContext tc;
1245 GNUNET_assert ((sock->read_task == GNUNET_SCHEDULER_NO_TASK) &&
1246 (0 == (sock->ccs & COCO_RECEIVE_AGAIN)) &&
1247 (sock->receiver == NULL));
1248 sock->receiver = receiver;
1249 sock->receiver_cls = receiver_cls;
1250 sock->receive_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1252 if (sock->sock != NULL)
1254 memset (&tc, 0, sizeof (tc));
1255 tc.sched = sock->sched;
1256 tc.reason = GNUNET_SCHEDULER_REASON_PREREQ_DONE;
1257 receive_again (sock, &tc);
1260 if ((sock->dns_active == NULL) && (sock->ap_head == NULL))
1262 receiver (receiver_cls, NULL, 0, NULL, 0, ETIMEDOUT);
1265 sock->ccs += COCO_RECEIVE_AGAIN;
1270 * Configure this connection to ignore shutdown signals.
1272 * @param sock socket handle
1273 * @param do_ignore GNUNET_YES to ignore, GNUNET_NO to restore default
1276 GNUNET_CONNECTION_ignore_shutdown (struct GNUNET_CONNECTION_Handle *sock,
1279 sock->ignore_shutdown = do_ignore;
1284 * Cancel receive job on the given socket. Note that the
1285 * receiver callback must not have been called yet in order
1286 * for the cancellation to be valid.
1288 * @param sock socket handle
1289 * @return closure of the original receiver callback closure
1292 GNUNET_CONNECTION_receive_cancel (struct GNUNET_CONNECTION_Handle *sock)
1294 if (sock->read_task != GNUNET_SCHEDULER_NO_TASK)
1296 GNUNET_assert (sock == GNUNET_SCHEDULER_cancel (sock->sched,
1298 sock->read_task = GNUNET_SCHEDULER_NO_TASK;
1302 GNUNET_assert (0 != (sock->ccs & COCO_RECEIVE_AGAIN));
1303 sock->ccs -= COCO_RECEIVE_AGAIN;
1305 sock->receiver = NULL;
1306 return sock->receiver_cls;
1311 * Try to call the transmit notify method (check if we do
1312 * have enough space available first)!
1314 * @param sock socket for which we should do this processing
1315 * @return GNUNET_YES if we were able to call notify
1318 process_notify (struct GNUNET_CONNECTION_Handle *sock)
1323 GNUNET_CONNECTION_TransmitReadyNotify notify;
1325 GNUNET_assert (sock->write_task == GNUNET_SCHEDULER_NO_TASK);
1326 if (NULL == (notify = sock->nth.notify_ready))
1328 used = sock->write_buffer_off - sock->write_buffer_pos;
1329 avail = sock->write_buffer_size - used;
1330 size = sock->nth.notify_size;
1333 sock->nth.notify_ready = NULL;
1334 if (sock->write_buffer_size - sock->write_buffer_off < size)
1336 /* need to compact */
1337 memmove (sock->write_buffer,
1338 &sock->write_buffer[sock->write_buffer_pos], used);
1339 sock->write_buffer_off -= sock->write_buffer_pos;
1340 sock->write_buffer_pos = 0;
1342 avail = sock->write_buffer_size - sock->write_buffer_off;
1343 GNUNET_assert (avail >= size);
1344 size = notify (sock->nth.notify_ready_cls,
1346 &sock->write_buffer[sock->write_buffer_off]);
1347 GNUNET_assert (size <= avail);
1348 sock->write_buffer_off += size;
1354 * Task invoked by the scheduler when a call to transmit
1355 * is timing out (we never got enough buffer space to call
1356 * the callback function before the specified timeout
1359 * This task notifies the client about the timeout.
1362 transmit_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1364 struct GNUNET_CONNECTION_Handle *sock = cls;
1365 GNUNET_CONNECTION_TransmitReadyNotify notify;
1367 #if DEBUG_CONNECTION
1368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1369 "transmit_timeout running (%p)\n", sock);
1371 sock->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK;
1372 #if DEBUG_CONNECTION
1373 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1374 "Transmit to `%s:%u/%s' fails, time out reached (%p).\n",
1376 sock->port, GNUNET_a2s (sock->addr, sock->addrlen), sock);
1378 GNUNET_assert (0 != (sock->ccs & COCO_TRANSMIT_READY));
1379 sock->ccs -= COCO_TRANSMIT_READY; /* remove request */
1380 notify = sock->nth.notify_ready;
1381 sock->nth.notify_ready = NULL;
1382 notify (sock->nth.notify_ready_cls, 0, NULL);
1387 * Task invoked by the scheduler when we failed to connect
1388 * at the time of being asked to transmit.
1390 * This task notifies the client about the error.
1393 connect_error (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1395 struct GNUNET_CONNECTION_Handle *sock = cls;
1396 GNUNET_CONNECTION_TransmitReadyNotify notify;
1398 #if DEBUG_CONNECTION
1399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1400 "Transmission request of size %u fails, connection failed (%p).\n",
1401 sock->nth.notify_size, sock);
1403 sock->write_task = GNUNET_SCHEDULER_NO_TASK;
1404 notify = sock->nth.notify_ready;
1405 sock->nth.notify_ready = NULL;
1406 notify (sock->nth.notify_ready_cls, 0, NULL);
1411 transmit_error (struct GNUNET_CONNECTION_Handle *sock)
1413 GNUNET_CONNECTION_TransmitReadyNotify notify;
1415 if (NULL != sock->sock)
1417 GNUNET_NETWORK_socket_shutdown (sock->sock, SHUT_RDWR);
1418 GNUNET_break (GNUNET_OK ==
1419 GNUNET_NETWORK_socket_close (sock->sock));
1422 if (sock->read_task != GNUNET_SCHEDULER_NO_TASK)
1424 GNUNET_SCHEDULER_cancel (sock->sched,
1426 sock->read_task = GNUNET_SCHEDULER_NO_TASK;
1427 signal_timeout (sock);
1430 if (sock->nth.notify_ready == NULL)
1431 return; /* nobody to tell about it */
1432 notify = sock->nth.notify_ready;
1433 sock->nth.notify_ready = NULL;
1434 notify (sock->nth.notify_ready_cls, 0, NULL);
1439 * See if we are now connected. If not, wait longer for
1440 * connect to succeed. If connected, we should be able
1441 * to write now as well, unless we timed out.
1443 * @param cls our connection handle
1444 * @param tc task context describing why we are here
1447 transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1449 struct GNUNET_CONNECTION_Handle *sock = cls;
1450 GNUNET_CONNECTION_TransmitReadyNotify notify;
1454 #if DEBUG_CONNECTION
1455 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1456 "transmit_ready running (%p).\n", sock);
1458 GNUNET_assert (sock->write_task != GNUNET_SCHEDULER_NO_TASK);
1459 sock->write_task = GNUNET_SCHEDULER_NO_TASK;
1460 GNUNET_assert (sock->nth.timeout_task == GNUNET_SCHEDULER_NO_TASK);
1461 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1463 if (sock->ignore_shutdown == GNUNET_YES)
1464 goto SCHEDULE_WRITE; /* ignore shutdown, go again immediately */
1465 #if DEBUG_CONNECTION
1466 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1467 "Transmit to `%s' fails, shutdown happened (%p).\n",
1468 GNUNET_a2s (sock->addr, sock->addrlen), sock);
1470 notify = sock->nth.notify_ready;
1471 sock->nth.notify_ready = NULL;
1472 notify (sock->nth.notify_ready_cls, 0, NULL);
1475 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT))
1477 #if DEBUG_CONNECTION
1478 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1479 "Transmit to `%s' fails, time out reached (%p).\n",
1480 GNUNET_a2s (sock->addr, sock->addrlen), sock);
1482 notify = sock->nth.notify_ready;
1483 sock->nth.notify_ready = NULL;
1484 notify (sock->nth.notify_ready_cls, 0, NULL);
1487 GNUNET_assert (NULL != sock->sock);
1488 if (tc->write_ready == NULL)
1490 /* special circumstances (in particular,
1491 PREREQ_DONE after connect): not yet ready to write,
1492 but no "fatal" error either. Hence retry. */
1493 goto SCHEDULE_WRITE;
1495 if (!GNUNET_NETWORK_fdset_isset (tc->write_ready, sock->sock))
1497 #if DEBUG_CONNECTION
1498 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1500 ("Could not satisfy pending transmission request, socket closed or connect failed (%p).\n"),
1503 transmit_error (sock);
1504 return; /* connect failed for good, we're finished */
1506 GNUNET_assert (sock->write_buffer_off >= sock->write_buffer_pos);
1507 if ( (sock->nth.notify_ready != NULL) &&
1508 (sock->write_buffer_size < sock->nth.notify_size) )
1510 sock->write_buffer = GNUNET_realloc(sock->write_buffer,
1511 sock->nth.notify_size);
1512 sock->write_buffer_size = sock->nth.notify_size;
1514 process_notify (sock);
1515 have = sock->write_buffer_off - sock->write_buffer_pos;
1518 /* no data ready for writing, terminate write loop */
1521 GNUNET_assert (have <= sock->write_buffer_size);
1522 GNUNET_assert (have + sock->write_buffer_pos <= sock->write_buffer_size);
1523 GNUNET_assert (sock->write_buffer_pos <= sock->write_buffer_size);
1525 ret = GNUNET_NETWORK_socket_send (sock->sock,
1526 &sock->write_buffer[sock->
1535 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1536 _("Failed to send to `%s': %s\n"),
1537 GNUNET_a2s (sock->addr,
1541 #if DEBUG_CONNECTION
1542 GNUNET_log_strerror (GNUNET_ERROR_TYPE_DEBUG, "send");
1544 transmit_error (sock);
1547 #if DEBUG_CONNECTION
1548 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1549 "transmit_ready transmitted %u/%u bytes to `%s' (%p)\n",
1551 have, GNUNET_a2s (sock->addr, sock->addrlen), sock);
1553 sock->write_buffer_pos += ret;
1554 if (sock->write_buffer_pos == sock->write_buffer_off)
1556 /* transmitted all pending data */
1557 sock->write_buffer_pos = 0;
1558 sock->write_buffer_off = 0;
1560 if ((sock->write_buffer_off == 0) && (NULL == sock->nth.notify_ready))
1561 return; /* all data sent! */
1562 /* not done writing, schedule more */
1564 #if DEBUG_CONNECTION
1565 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1566 "Re-scheduling transmit_ready (more to do) (%p).\n", sock);
1568 if (sock->write_task == GNUNET_SCHEDULER_NO_TASK)
1570 GNUNET_SCHEDULER_add_write_net (tc->sched,
1571 GNUNET_TIME_absolute_get_remaining
1572 (sock->nth.transmit_timeout),
1573 sock->sock, &transmit_ready, sock);
1578 * Ask the socket to call us once the specified number of bytes
1579 * are free in the transmission buffer. May call the notify
1580 * method immediately if enough space is available.
1582 * @param sock socket
1583 * @param size number of bytes to send
1584 * @param timeout after how long should we give up (and call
1585 * notify with buf NULL and size 0)?
1586 * @param notify function to call
1587 * @param notify_cls closure for notify
1588 * @return non-NULL if the notify callback was queued,
1589 * NULL if we are already going to notify someone else (busy)
1591 struct GNUNET_CONNECTION_TransmitHandle *
1592 GNUNET_CONNECTION_notify_transmit_ready (struct GNUNET_CONNECTION_Handle
1594 struct GNUNET_TIME_Relative timeout,
1595 GNUNET_CONNECTION_TransmitReadyNotify
1596 notify, void *notify_cls)
1598 if (sock->nth.notify_ready != NULL)
1600 GNUNET_assert (notify != NULL);
1601 GNUNET_assert (size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
1602 GNUNET_assert (sock->write_buffer_off <= sock->write_buffer_size);
1603 GNUNET_assert (sock->write_buffer_pos <= sock->write_buffer_size);
1604 GNUNET_assert (sock->write_buffer_pos <= sock->write_buffer_off);
1605 sock->nth.notify_ready = notify;
1606 sock->nth.notify_ready_cls = notify_cls;
1607 sock->nth.sh = sock;
1608 sock->nth.notify_size = size;
1609 sock->nth.transmit_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1610 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->nth.timeout_task);
1611 if ((sock->sock == NULL) &&
1612 (sock->ap_head == NULL) && (sock->dns_active == NULL))
1614 if (sock->write_task != GNUNET_SCHEDULER_NO_TASK)
1615 GNUNET_SCHEDULER_cancel (sock->sched,
1617 sock->write_task = GNUNET_SCHEDULER_add_now (sock->sched,
1618 &connect_error, sock);
1621 if (GNUNET_SCHEDULER_NO_TASK != sock->write_task)
1623 if (sock->sock != NULL)
1625 #if DEBUG_CONNECTION
1626 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1627 "Scheduling transmit_ready (%p).\n", sock);
1629 sock->write_task = GNUNET_SCHEDULER_add_write_net (sock->sched,
1630 GNUNET_TIME_absolute_get_remaining
1639 #if DEBUG_CONNECTION
1640 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1641 "CCS-Scheduling transmit_ready, adding timeout task (%p).\n",
1644 sock->ccs |= COCO_TRANSMIT_READY;
1645 sock->nth.timeout_task = GNUNET_SCHEDULER_add_delayed (sock->sched,
1655 * Cancel the specified transmission-ready
1659 GNUNET_CONNECTION_notify_transmit_ready_cancel (struct
1660 GNUNET_CONNECTION_TransmitHandle
1663 GNUNET_assert (h->notify_ready != NULL);
1664 if (0 != (h->sh->ccs & COCO_TRANSMIT_READY))
1666 #if DEBUG_CONNECTION
1667 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1668 "notify_transmit_ready_cancel cancels timeout_task (%p)\n",
1671 GNUNET_SCHEDULER_cancel (h->sh->sched, h->timeout_task);
1672 h->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1673 h->sh->ccs -= COCO_TRANSMIT_READY;
1677 if (h->sh->write_task != GNUNET_SCHEDULER_NO_TASK)
1679 GNUNET_SCHEDULER_cancel (h->sh->sched, h->sh->write_task);
1680 h->sh->write_task = GNUNET_SCHEDULER_NO_TASK;
1683 h->notify_ready = NULL;
1686 /* end of connection.c */