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);
565 * See if we are now connected. If not, wait longer for
566 * connect to succeed. If connected, we should be able
567 * to write now as well, unless we timed out.
569 * @param cls our connection handle
570 * @param tc task context describing why we are here
573 transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
577 * We've failed for good to establish a connection.
579 * @param h the connection we tried to establish
582 connect_fail_continuation (struct GNUNET_CONNECTION_Handle *h)
585 GNUNET_log ((0 != strncmp (h->hostname,
588 ? GNUNET_ERROR_TYPE_INFO
589 : GNUNET_ERROR_TYPE_WARNING,
590 _("Failed to establish TCP connection to `%s:%u', no further addresses to try.\n"),
591 h->hostname, h->port);
593 /* connect failed / timed out */
594 GNUNET_break (h->ap_head == NULL);
595 GNUNET_break (h->ap_tail == NULL);
596 GNUNET_break (h->dns_active == GNUNET_NO);
597 GNUNET_break (h->sock == NULL);
599 /* trigger jobs that used to wait on "connect_task" */
600 if (0 != (h->ccs & COCO_RECEIVE_AGAIN))
603 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
604 "connect_fail_continuation triggers receive_again (%p)\n",
607 h->ccs -= COCO_RECEIVE_AGAIN;
608 h->read_task = GNUNET_SCHEDULER_add_after (h->sched,
609 GNUNET_SCHEDULER_NO_TASK,
612 if (0 != (h->ccs & COCO_TRANSMIT_READY))
615 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
616 "connect_fail_continuation cancels timeout_task, triggers transmit_ready (%p)\n",
619 GNUNET_assert (h->nth.timeout_task != GNUNET_SCHEDULER_NO_TASK);
620 GNUNET_SCHEDULER_cancel (h->sched, h->nth.timeout_task);
621 h->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK;
622 h->ccs -= COCO_TRANSMIT_READY;
623 GNUNET_assert (h->write_task == GNUNET_SCHEDULER_NO_TASK);
624 h->write_task = GNUNET_SCHEDULER_add_after (h->sched,
625 GNUNET_SCHEDULER_NO_TASK,
628 if (0 != (h->ccs & COCO_DESTROY_CONTINUATION))
631 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
632 "connect_fail_continuation runs destroy_continuation (%p)\n",
635 h->ccs -= COCO_DESTROY_CONTINUATION;
636 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == h->destroy_task);
638 = GNUNET_SCHEDULER_add_now (h->sched,
639 &destroy_continuation,
646 * We've succeeded in establishing a connection.
648 * @param h the connection we tried to establish
651 connect_success_continuation (struct GNUNET_CONNECTION_Handle *h)
654 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
655 "Connection to `%s' succeeded! (%p)\n",
656 GNUNET_a2s (h->addr, h->addrlen), h);
658 /* trigger jobs that waited for the connection */
659 if (0 != (h->ccs & COCO_RECEIVE_AGAIN))
662 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
663 "connect_success_continuation runs receive_again (%p)\n",
666 h->ccs -= COCO_RECEIVE_AGAIN;
667 h->read_task = GNUNET_SCHEDULER_add_after (h->sched,
668 GNUNET_SCHEDULER_NO_TASK,
671 if (0 != (h->ccs & COCO_TRANSMIT_READY))
674 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
675 "connect_success_continuation runs transmit_ready, cancels timeout_task (%p)\n",
678 GNUNET_assert (h->nth.timeout_task != GNUNET_SCHEDULER_NO_TASK);
679 GNUNET_SCHEDULER_cancel (h->sched, h->nth.timeout_task);
680 h->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK;
681 h->ccs -= COCO_TRANSMIT_READY;
682 GNUNET_assert (h->write_task == GNUNET_SCHEDULER_NO_TASK);
684 GNUNET_SCHEDULER_add_write_net (h->sched,
685 GNUNET_TIME_absolute_get_remaining
686 (h->nth.transmit_timeout), h->sock,
689 if (0 != (h->ccs & COCO_DESTROY_CONTINUATION))
692 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
693 "connect_success_continuation runs destroy_continuation (%p)\n",
696 h->ccs -= COCO_DESTROY_CONTINUATION;
697 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == h->destroy_task);
699 = GNUNET_SCHEDULER_add_now (h->sched,
700 &destroy_continuation,
707 * Scheduler let us know that we're either ready to write on the
708 * socket OR connect timed out. Do the right thing.
710 * @param cls the "struct AddressProbe*" with the address that we are probing
711 * @param tc success or failure info about the connect attempt.
714 connect_probe_continuation (void *cls,
715 const struct GNUNET_SCHEDULER_TaskContext *tc)
717 struct AddressProbe *ap = cls;
718 struct GNUNET_CONNECTION_Handle *h = ap->h;
719 struct AddressProbe *pos;
723 GNUNET_CONTAINER_DLL_remove (h->ap_head, h->ap_tail, ap);
724 len = sizeof (error);
727 if ( (0 == (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) ||
729 GNUNET_NETWORK_socket_getsockopt (ap->sock, SOL_SOCKET, SO_ERROR,
733 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ap->sock));
735 if ((NULL == h->ap_head) && (h->dns_active == GNUNET_NO))
736 connect_fail_continuation (h);
739 GNUNET_assert (h->sock == NULL);
740 GNUNET_assert (ap->sock != NULL);
742 GNUNET_assert (h->addr == NULL);
743 h->addr = GNUNET_malloc (ap->addrlen);
744 memcpy (h->addr, ap->addr, ap->addrlen);
745 h->addrlen = ap->addrlen;
747 /* cancel all other attempts */
748 while (NULL != (pos = h->ap_head))
750 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (pos->sock));
751 GNUNET_SCHEDULER_cancel (h->sched, pos->task);
752 GNUNET_CONTAINER_DLL_remove (h->ap_head, h->ap_tail, pos);
755 connect_success_continuation (h);
760 * Try to establish a socket connection given the specified address.
761 * This function is called by the resolver once we have a DNS reply.
763 * @param cls our "struct GNUNET_CONNECTION_Handle *"
764 * @param addr address to try, NULL for "last call"
765 * @param addrlen length of addr
768 try_connect_using_address (void *cls,
769 const struct sockaddr *addr, socklen_t addrlen)
771 struct GNUNET_CONNECTION_Handle *h = cls;
772 struct AddressProbe *ap;
773 struct GNUNET_TIME_Relative delay;
777 h->dns_active = NULL;
778 if (NULL == h->ap_head)
779 connect_fail_continuation (h);
783 return; /* already connected */
784 GNUNET_assert (h->addr == NULL);
787 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
788 "Trying to connect using address `%s:%u/%s:%u'\n",
789 h->hostname, h->port, GNUNET_a2s (addr, addrlen), h->port);
791 ap = GNUNET_malloc (sizeof (struct AddressProbe) + addrlen);
792 ap->addr = (const struct sockaddr *) &ap[1];
793 memcpy (&ap[1], addr, addrlen);
794 ap->addrlen = addrlen;
797 switch (ap->addr->sa_family)
800 ((struct sockaddr_in *) ap->addr)->sin_port = htons (h->port);
803 ((struct sockaddr_in6 *) ap->addr)->sin6_port = htons (h->port);
808 return; /* not supported by us */
811 GNUNET_NETWORK_socket_create (ap->addr->sa_family, SOCK_STREAM, 0);
812 if (ap->sock == NULL)
815 return; /* not supported by OS */
818 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
819 _("Trying to connect to `%s' (%p)\n"),
820 GNUNET_a2s (ap->addr, ap->addrlen), h);
822 if ((GNUNET_OK != GNUNET_NETWORK_socket_connect (ap->sock,
825 (errno != EINPROGRESS))
827 /* maybe refused / unsupported address, try next */
828 GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "connect");
829 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ap->sock));
833 GNUNET_CONTAINER_DLL_insert (h->ap_head, h->ap_tail, ap);
834 delay = GNUNET_CONNECTION_CONNECT_RETRY_TIMEOUT;
835 if (h->nth.notify_ready != NULL)
836 delay = GNUNET_TIME_relative_min (delay,
837 GNUNET_TIME_absolute_get_remaining
838 (h->nth.transmit_timeout));
839 if (h->receiver != NULL)
840 delay = GNUNET_TIME_relative_min (delay,
841 GNUNET_TIME_absolute_get_remaining
842 (h->receive_timeout));
844 GNUNET_SCHEDULER_add_write_net (h->sched, delay, ap->sock,
845 &connect_probe_continuation, ap);
850 * Create a socket handle by (asynchronously) connecting to a host.
851 * This function returns immediately, even if the connection has not
852 * yet been established. This function only creates TCP connections.
854 * @param sched scheduler to use
855 * @param cfg configuration to use
856 * @param hostname name of the host to connect to
857 * @param port port to connect to
858 * @return the socket handle
860 struct GNUNET_CONNECTION_Handle *
861 GNUNET_CONNECTION_create_from_connect (struct GNUNET_SCHEDULER_Handle *sched,
863 GNUNET_CONFIGURATION_Handle *cfg,
864 const char *hostname, uint16_t port)
866 struct GNUNET_CONNECTION_Handle *ret;
868 GNUNET_assert (0 < strlen (hostname)); /* sanity check */
869 ret = GNUNET_malloc (sizeof (struct GNUNET_CONNECTION_Handle));
872 ret->write_buffer_size = GNUNET_SERVER_MIN_BUFFER_SIZE;
873 ret->write_buffer = GNUNET_malloc(ret->write_buffer_size);
875 ret->hostname = GNUNET_strdup (hostname);
876 ret->dns_active = GNUNET_RESOLVER_ip_get (sched,
880 GNUNET_CONNECTION_CONNECT_RETRY_TIMEOUT,
881 &try_connect_using_address, ret);
887 * Create a socket handle by connecting to a UNIX domain service.
888 * This function returns immediately, even if the connection has not
889 * yet been established. This function only creates UNIX connections.
891 * @param sched scheduler to use
892 * @param cfg configuration to use
893 * @param unixpath path to connect to
894 * @return the socket handle, NULL on systems without UNIX support
896 struct GNUNET_CONNECTION_Handle *
897 GNUNET_CONNECTION_create_from_connect_to_unixpath (struct GNUNET_SCHEDULER_Handle *sched,
899 GNUNET_CONFIGURATION_Handle *cfg,
900 const char *unixpath)
903 struct GNUNET_CONNECTION_Handle *ret;
904 struct sockaddr_un *un;
907 GNUNET_assert (0 < strlen (unixpath)); /* sanity check */
908 un = GNUNET_malloc (sizeof (struct sockaddr_un));
909 un->sun_family = AF_UNIX;
910 slen = strlen (unixpath) + 1;
911 if (slen >= sizeof (un->sun_path))
912 slen = sizeof (un->sun_path) - 1;
913 memcpy (un->sun_path,
916 un->sun_path[slen] = '\0';
917 slen += sizeof (sa_family_t);
919 un->sun_path[0] = '\0';
920 slen = sizeof (struct sockaddr_un);
922 ret = GNUNET_malloc (sizeof (struct GNUNET_CONNECTION_Handle));
925 ret->write_buffer_size = GNUNET_SERVER_MIN_BUFFER_SIZE;
926 ret->write_buffer = GNUNET_malloc(ret->write_buffer_size);
928 ret->hostname = NULL;
929 ret->addr = (struct sockaddr*) un;
931 ret->sock = GNUNET_NETWORK_socket_create (AF_UNIX, SOCK_STREAM, 0);
932 if (NULL == ret->sock)
934 GNUNET_free (ret->addr);
935 GNUNET_free (ret->write_buffer);
939 if (GNUNET_OK != GNUNET_NETWORK_socket_connect (ret->sock,
943 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret->sock));
944 GNUNET_free (ret->addr);
945 GNUNET_free (ret->write_buffer);
949 connect_success_continuation (ret);
958 * Create a socket handle by (asynchronously) connecting to a host.
959 * This function returns immediately, even if the connection has not
960 * yet been established. This function only creates TCP connections.
962 * @param sched scheduler to use
963 * @param af_family address family to use
964 * @param serv_addr server address
965 * @param addrlen length of server address
966 * @return the socket handle
968 struct GNUNET_CONNECTION_Handle *
969 GNUNET_CONNECTION_create_from_sockaddr (struct GNUNET_SCHEDULER_Handle
970 *sched, int af_family,
971 const struct sockaddr *serv_addr,
974 struct GNUNET_NETWORK_Handle *s;
975 struct GNUNET_CONNECTION_Handle *ret;
977 s = GNUNET_NETWORK_socket_create (af_family, SOCK_STREAM, 0);
980 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING |
981 GNUNET_ERROR_TYPE_BULK, "socket");
984 if ((GNUNET_OK != GNUNET_NETWORK_socket_connect (s, serv_addr, addrlen))
985 && (errno != EINPROGRESS))
987 /* maybe refused / unsupported address, try next */
988 GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "connect");
989 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (s));
992 ret = GNUNET_CONNECTION_create_from_existing (sched, s);
993 ret->addr = GNUNET_malloc (addrlen);
994 memcpy (ret->addr, serv_addr, addrlen);
995 ret->addrlen = addrlen;
997 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
998 _("Trying to connect to `%s' (%p)\n"),
999 GNUNET_a2s (serv_addr, addrlen), ret);
1006 * Check if socket is valid (no fatal errors have happened so far).
1007 * Note that a socket that is still trying to connect is considered
1010 * @param sock socket to check
1011 * @return GNUNET_YES if valid, GNUNET_NO otherwise
1014 GNUNET_CONNECTION_check (struct GNUNET_CONNECTION_Handle *sock)
1016 if ((sock->ap_head != NULL) || (sock->dns_active != NULL))
1017 return GNUNET_YES; /* still trying to connect */
1018 return (sock->sock == NULL) ? GNUNET_NO : GNUNET_YES;
1023 * Close the socket and free associated resources. Pending
1024 * transmissions may be completed or dropped depending on the
1025 * arguments. If a receive call is pending and should
1026 * NOT be completed, 'GNUNET_CONNECTION_receive_cancel'
1027 * should be called explicitly first.
1029 * @param sock socket to destroy
1030 * @param finish_pending_write should pending writes be completed or aborted?
1031 * (this applies to transmissions where the data has already been
1032 * read from the application; all other transmissions should be
1033 * aborted using 'GNUNET_CONNECTION_notify_transmit_ready_cancel').
1036 GNUNET_CONNECTION_destroy (struct GNUNET_CONNECTION_Handle *sock,
1037 int finish_pending_write)
1039 if (GNUNET_NO == finish_pending_write)
1041 if (sock->write_task != GNUNET_SCHEDULER_NO_TASK)
1043 GNUNET_SCHEDULER_cancel (sock->sched,
1045 sock->write_task = GNUNET_SCHEDULER_NO_TASK;
1046 sock->write_buffer_off = 0;
1049 if ((sock->write_buffer_off == 0) && (sock->dns_active != NULL))
1051 GNUNET_RESOLVER_request_cancel (sock->dns_active);
1052 sock->dns_active = NULL;
1054 GNUNET_assert (sock->sched != NULL);
1055 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task);
1057 = GNUNET_SCHEDULER_add_now (sock->sched,
1058 &destroy_continuation, sock);
1063 * Tell the receiver callback that a timeout was reached.
1066 signal_timeout (struct GNUNET_CONNECTION_Handle *sh)
1068 GNUNET_CONNECTION_Receiver receiver;
1070 #if DEBUG_CONNECTION
1071 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1072 "Network signals time out to receiver (%p)!\n", sh);
1074 GNUNET_assert (NULL != (receiver = sh->receiver));
1075 sh->receiver = NULL;
1076 receiver (sh->receiver_cls, NULL, 0, NULL, 0, 0);
1081 * Tell the receiver callback that we had an IO error.
1084 signal_error (struct GNUNET_CONNECTION_Handle *sh, int errcode)
1086 GNUNET_CONNECTION_Receiver receiver;
1087 GNUNET_assert (NULL != (receiver = sh->receiver));
1088 sh->receiver = NULL;
1089 receiver (sh->receiver_cls, NULL, 0, sh->addr, sh->addrlen, errcode);
1094 * This function is called once we either timeout
1095 * or have data ready to read.
1098 receive_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1100 struct GNUNET_CONNECTION_Handle *sh = cls;
1101 struct GNUNET_TIME_Absolute now;
1102 char buffer[sh->max];
1104 GNUNET_CONNECTION_Receiver receiver;
1106 sh->read_task = GNUNET_SCHEDULER_NO_TASK;
1107 if ( (GNUNET_YES == sh->ignore_shutdown) &&
1108 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
1110 /* ignore shutdown request, go again immediately */
1111 #if DEBUG_CONNECTION
1112 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1113 "Ignoring shutdown signal per configuration\n");
1115 sh->read_task = GNUNET_SCHEDULER_add_read_net (tc->sched,
1116 GNUNET_TIME_absolute_get_remaining
1117 (sh->receive_timeout),
1119 &receive_ready, sh);
1122 now = GNUNET_TIME_absolute_get ();
1123 if ((now.value > sh->receive_timeout.value) ||
1124 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT)) ||
1125 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
1127 #if DEBUG_CONNECTION
1128 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1129 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1130 "Receive from `%s' encounters error: time out by %llums... (%p)\n",
1131 GNUNET_a2s (sh->addr, sh->addrlen),
1132 GNUNET_TIME_absolute_get_duration (sh->receive_timeout).
1135 signal_timeout (sh);
1138 if (sh->sock == NULL)
1140 /* connect failed for good */
1141 #if DEBUG_CONNECTION
1142 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1143 "Receive encounters error, socket closed... (%p)\n", sh);
1145 signal_error (sh, ECONNREFUSED);
1148 GNUNET_assert (GNUNET_NETWORK_fdset_isset (tc->read_ready, sh->sock));
1150 ret = GNUNET_NETWORK_socket_recv (sh->sock, buffer, sh->max);
1155 #if DEBUG_CONNECTION
1156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1157 "Error receiving: %s\n", STRERROR (errno));
1159 signal_error (sh, errno);
1162 #if DEBUG_CONNECTION
1163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1164 "receive_ready read %u/%u bytes from `%s' (%p)!\n",
1166 sh->max, GNUNET_a2s (sh->addr, sh->addrlen), sh);
1168 GNUNET_assert (NULL != (receiver = sh->receiver));
1169 sh->receiver = NULL;
1170 receiver (sh->receiver_cls, buffer, ret, sh->addr, sh->addrlen, 0);
1175 * This function is called after establishing a connection either has
1176 * succeeded or timed out. Note that it is possible that the attempt
1177 * timed out and that we're immediately retrying. If we are retrying,
1178 * we need to wait again (or timeout); if we succeeded, we need to
1179 * wait for data (or timeout).
1181 * @param cls our connection handle
1182 * @param tc task context describing why we are here
1185 receive_again (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1187 struct GNUNET_CONNECTION_Handle *sh = cls;
1188 struct GNUNET_TIME_Absolute now;
1190 sh->read_task = GNUNET_SCHEDULER_NO_TASK;
1191 if (sh->sock == NULL)
1193 /* not connected and no longer trying */
1194 #if DEBUG_CONNECTION
1195 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1196 "Receive encounters error, socket closed (%p)...\n", sh);
1198 signal_error (sh, ECONNREFUSED);
1201 now = GNUNET_TIME_absolute_get ();
1202 if ((now.value > sh->receive_timeout.value) ||
1203 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
1205 #if DEBUG_CONNECTION
1206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1207 "Receive encounters error: time out (%p)...\n", sh);
1209 signal_timeout (sh);
1212 GNUNET_assert (sh->sock != NULL);
1213 /* connect succeeded, wait for data! */
1214 sh->read_task = GNUNET_SCHEDULER_add_read_net (tc->sched,
1215 GNUNET_TIME_absolute_get_remaining
1216 (sh->receive_timeout),
1218 &receive_ready, sh);
1223 * Receive data from the given socket. Note that this function will
1224 * call "receiver" asynchronously using the scheduler. It will
1225 * "immediately" return. Note that there MUST only be one active
1226 * receive call per socket at any given point in time (so do not
1227 * call receive again until the receiver callback has been invoked).
1229 * @param sock socket handle
1230 * @param max maximum number of bytes to read
1231 * @param timeout maximum amount of time to wait (use -1 for "forever")
1232 * @param receiver function to call with received data
1233 * @param receiver_cls closure for receiver
1236 GNUNET_CONNECTION_receive (struct GNUNET_CONNECTION_Handle *sock,
1238 struct GNUNET_TIME_Relative timeout,
1239 GNUNET_CONNECTION_Receiver receiver,
1242 struct GNUNET_SCHEDULER_TaskContext tc;
1244 GNUNET_assert ((sock->read_task == GNUNET_SCHEDULER_NO_TASK) &&
1245 (0 == (sock->ccs & COCO_RECEIVE_AGAIN)) &&
1246 (sock->receiver == NULL));
1247 sock->receiver = receiver;
1248 sock->receiver_cls = receiver_cls;
1249 sock->receive_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1251 if (sock->sock != NULL)
1253 memset (&tc, 0, sizeof (tc));
1254 tc.sched = sock->sched;
1255 tc.reason = GNUNET_SCHEDULER_REASON_PREREQ_DONE;
1256 receive_again (sock, &tc);
1259 if ((sock->dns_active == NULL) && (sock->ap_head == NULL))
1261 receiver (receiver_cls, NULL, 0, NULL, 0, ETIMEDOUT);
1264 sock->ccs += COCO_RECEIVE_AGAIN;
1269 * Configure this connection to ignore shutdown signals.
1271 * @param sock socket handle
1272 * @param do_ignore GNUNET_YES to ignore, GNUNET_NO to restore default
1275 GNUNET_CONNECTION_ignore_shutdown (struct GNUNET_CONNECTION_Handle *sock,
1278 sock->ignore_shutdown = do_ignore;
1283 * Cancel receive job on the given socket. Note that the
1284 * receiver callback must not have been called yet in order
1285 * for the cancellation to be valid.
1287 * @param sock socket handle
1288 * @return closure of the original receiver callback closure
1291 GNUNET_CONNECTION_receive_cancel (struct GNUNET_CONNECTION_Handle *sock)
1293 if (sock->read_task != GNUNET_SCHEDULER_NO_TASK)
1295 GNUNET_assert (sock == GNUNET_SCHEDULER_cancel (sock->sched,
1297 sock->read_task = GNUNET_SCHEDULER_NO_TASK;
1301 GNUNET_assert (0 != (sock->ccs & COCO_RECEIVE_AGAIN));
1302 sock->ccs -= COCO_RECEIVE_AGAIN;
1304 sock->receiver = NULL;
1305 return sock->receiver_cls;
1310 * Try to call the transmit notify method (check if we do
1311 * have enough space available first)!
1313 * @param sock socket for which we should do this processing
1314 * @return GNUNET_YES if we were able to call notify
1317 process_notify (struct GNUNET_CONNECTION_Handle *sock)
1322 GNUNET_CONNECTION_TransmitReadyNotify notify;
1324 GNUNET_assert (sock->write_task == GNUNET_SCHEDULER_NO_TASK);
1325 if (NULL == (notify = sock->nth.notify_ready))
1327 used = sock->write_buffer_off - sock->write_buffer_pos;
1328 avail = sock->write_buffer_size - used;
1329 size = sock->nth.notify_size;
1330 if (sock->nth.notify_size > avail)
1332 sock->nth.notify_ready = NULL;
1333 if (sock->write_buffer_size - sock->write_buffer_off < size)
1335 /* need to compact */
1336 memmove (sock->write_buffer,
1337 &sock->write_buffer[sock->write_buffer_pos], used);
1338 sock->write_buffer_off -= sock->write_buffer_pos;
1339 sock->write_buffer_pos = 0;
1341 GNUNET_assert (sock->write_buffer_size - sock->write_buffer_off >= size);
1342 size = notify (sock->nth.notify_ready_cls,
1343 sock->write_buffer_size - sock->write_buffer_off,
1344 &sock->write_buffer[sock->write_buffer_off]);
1345 sock->write_buffer_off += size;
1351 * Task invoked by the scheduler when a call to transmit
1352 * is timing out (we never got enough buffer space to call
1353 * the callback function before the specified timeout
1356 * This task notifies the client about the timeout.
1359 transmit_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1361 struct GNUNET_CONNECTION_Handle *sock = cls;
1362 GNUNET_CONNECTION_TransmitReadyNotify notify;
1364 #if DEBUG_CONNECTION
1365 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1366 "transmit_timeout running (%p)\n", sock);
1368 sock->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK;
1369 #if DEBUG_CONNECTION
1370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1371 "Transmit to `%s:%u/%s' fails, time out reached (%p).\n",
1373 sock->port, GNUNET_a2s (sock->addr, sock->addrlen), sock);
1375 GNUNET_assert (0 != (sock->ccs & COCO_TRANSMIT_READY));
1376 sock->ccs -= COCO_TRANSMIT_READY; /* remove request */
1377 notify = sock->nth.notify_ready;
1378 sock->nth.notify_ready = NULL;
1379 notify (sock->nth.notify_ready_cls, 0, NULL);
1384 * Task invoked by the scheduler when we failed to connect
1385 * at the time of being asked to transmit.
1387 * This task notifies the client about the error.
1390 connect_error (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1392 struct GNUNET_CONNECTION_Handle *sock = cls;
1393 GNUNET_CONNECTION_TransmitReadyNotify notify;
1395 #if DEBUG_CONNECTION
1396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1397 "Transmission request of size %u fails, connection failed (%p).\n",
1398 sock->nth.notify_size, sock);
1400 sock->write_task = GNUNET_SCHEDULER_NO_TASK;
1401 notify = sock->nth.notify_ready;
1402 sock->nth.notify_ready = NULL;
1403 notify (sock->nth.notify_ready_cls, 0, NULL);
1408 transmit_error (struct GNUNET_CONNECTION_Handle *sock)
1410 GNUNET_CONNECTION_TransmitReadyNotify notify;
1412 if (NULL != sock->sock)
1414 GNUNET_NETWORK_socket_shutdown (sock->sock, SHUT_RDWR);
1415 GNUNET_break (GNUNET_OK ==
1416 GNUNET_NETWORK_socket_close (sock->sock));
1419 if (sock->read_task != GNUNET_SCHEDULER_NO_TASK)
1421 GNUNET_SCHEDULER_cancel (sock->sched,
1423 sock->read_task = GNUNET_SCHEDULER_NO_TASK;
1424 signal_timeout (sock);
1427 if (sock->nth.notify_ready == NULL)
1428 return; /* nobody to tell about it */
1429 notify = sock->nth.notify_ready;
1430 sock->nth.notify_ready = NULL;
1431 notify (sock->nth.notify_ready_cls, 0, NULL);
1436 * See if we are now connected. If not, wait longer for
1437 * connect to succeed. If connected, we should be able
1438 * to write now as well, unless we timed out.
1440 * @param cls our connection handle
1441 * @param tc task context describing why we are here
1444 transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1446 struct GNUNET_CONNECTION_Handle *sock = cls;
1447 GNUNET_CONNECTION_TransmitReadyNotify notify;
1451 #if DEBUG_CONNECTION
1452 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1453 "transmit_ready running (%p).\n", sock);
1455 GNUNET_assert (sock->write_task != GNUNET_SCHEDULER_NO_TASK);
1456 sock->write_task = GNUNET_SCHEDULER_NO_TASK;
1457 GNUNET_assert (sock->nth.timeout_task == GNUNET_SCHEDULER_NO_TASK);
1458 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1460 if (sock->ignore_shutdown == GNUNET_YES)
1461 goto SCHEDULE_WRITE; /* ignore shutdown, go again immediately */
1462 #if DEBUG_CONNECTION
1463 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1464 "Transmit to `%s' fails, shutdown happened (%p).\n",
1465 GNUNET_a2s (sock->addr, sock->addrlen), sock);
1467 notify = sock->nth.notify_ready;
1468 sock->nth.notify_ready = NULL;
1469 notify (sock->nth.notify_ready_cls, 0, NULL);
1472 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT))
1474 #if DEBUG_CONNECTION
1475 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1476 "Transmit to `%s' fails, time out reached (%p).\n",
1477 GNUNET_a2s (sock->addr, sock->addrlen), sock);
1479 notify = sock->nth.notify_ready;
1480 sock->nth.notify_ready = NULL;
1481 notify (sock->nth.notify_ready_cls, 0, NULL);
1484 GNUNET_assert (NULL != sock->sock);
1485 if (tc->write_ready == NULL)
1487 /* special circumstances (in particular,
1488 PREREQ_DONE after connect): not yet ready to write,
1489 but no "fatal" error either. Hence retry. */
1490 goto SCHEDULE_WRITE;
1492 if (!GNUNET_NETWORK_fdset_isset (tc->write_ready, sock->sock))
1494 #if DEBUG_CONNECTION
1495 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1497 ("Could not satisfy pending transmission request, socket closed or connect failed (%p).\n"),
1500 transmit_error (sock);
1501 return; /* connect failed for good, we're finished */
1503 GNUNET_assert (sock->write_buffer_off >= sock->write_buffer_pos);
1504 process_notify (sock);
1505 have = sock->write_buffer_off - sock->write_buffer_pos;
1508 /* no data ready for writing, terminate write loop */
1511 GNUNET_assert (have <= sock->write_buffer_size);
1512 GNUNET_assert (have + sock->write_buffer_pos <= sock->write_buffer_size);
1513 GNUNET_assert (sock->write_buffer_pos <= sock->write_buffer_size);
1515 ret = GNUNET_NETWORK_socket_send (sock->sock,
1516 &sock->write_buffer[sock->
1525 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1526 _("Failed to send to `%s': %s\n"),
1527 GNUNET_a2s (sock->addr,
1531 #if DEBUG_CONNECTION
1532 GNUNET_log_strerror (GNUNET_ERROR_TYPE_DEBUG, "send");
1534 transmit_error (sock);
1537 #if DEBUG_CONNECTION
1538 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1539 "transmit_ready transmitted %u/%u bytes to `%s' (%p)\n",
1541 have, GNUNET_a2s (sock->addr, sock->addrlen), sock);
1543 sock->write_buffer_pos += ret;
1544 if (sock->write_buffer_pos == sock->write_buffer_off)
1546 /* transmitted all pending data */
1547 sock->write_buffer_pos = 0;
1548 sock->write_buffer_off = 0;
1550 if ((sock->write_buffer_off == 0) && (NULL == sock->nth.notify_ready))
1551 return; /* all data sent! */
1552 /* not done writing, schedule more */
1554 #if DEBUG_CONNECTION
1555 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1556 "Re-scheduling transmit_ready (more to do) (%p).\n", sock);
1558 if (sock->write_task == GNUNET_SCHEDULER_NO_TASK)
1560 GNUNET_SCHEDULER_add_write_net (tc->sched,
1561 GNUNET_TIME_absolute_get_remaining
1562 (sock->nth.transmit_timeout),
1563 sock->sock, &transmit_ready, sock);
1568 * Ask the socket to call us once the specified number of bytes
1569 * are free in the transmission buffer. May call the notify
1570 * method immediately if enough space is available.
1572 * @param sock socket
1573 * @param size number of bytes to send
1574 * @param timeout after how long should we give up (and call
1575 * notify with buf NULL and size 0)?
1576 * @param notify function to call
1577 * @param notify_cls closure for notify
1578 * @return non-NULL if the notify callback was queued,
1579 * NULL if we are already going to notify someone else (busy)
1581 struct GNUNET_CONNECTION_TransmitHandle *
1582 GNUNET_CONNECTION_notify_transmit_ready (struct GNUNET_CONNECTION_Handle
1584 struct GNUNET_TIME_Relative timeout,
1585 GNUNET_CONNECTION_TransmitReadyNotify
1586 notify, void *notify_cls)
1589 if (sock->nth.notify_ready != NULL)
1591 GNUNET_assert (notify != NULL);
1592 if ((sock->write_buffer_size < size) && (size < GNUNET_SERVER_MAX_MESSAGE_SIZE))
1594 temp_size = sock->write_buffer_size + size;
1595 if (temp_size >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1596 temp_size = GNUNET_SERVER_MAX_MESSAGE_SIZE;
1598 sock->write_buffer = GNUNET_realloc(sock->write_buffer, temp_size);
1599 sock->write_buffer_size = temp_size;
1601 GNUNET_assert (sock->write_buffer_size >= 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 */