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"
43 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
45 #define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall)
48 * Possible functions to call after connect failed or succeeded.
50 enum ConnectContinuations
58 * Call "receive_again".
60 COCO_RECEIVE_AGAIN = 1,
63 * Call "transmit_ready".
65 COCO_TRANSMIT_READY = 2,
68 * Call "destroy_continuation".
70 COCO_DESTROY_CONTINUATION = 4
75 * Transmission handle. There can only be one for each connection.
77 struct GNUNET_CONNECTION_TransmitHandle
81 * Function to call if the send buffer has notify_size
84 GNUNET_CONNECTION_TransmitReadyNotify notify_ready;
87 * Closure for notify_ready.
89 void *notify_ready_cls;
94 struct GNUNET_CONNECTION_Handle *sh;
97 * Timeout for receiving (in absolute time).
99 struct GNUNET_TIME_Absolute transmit_timeout;
102 * Task called on timeout.
104 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
107 * At what number of bytes available in the
108 * write buffer should the notify method be called?
116 * During connect, we try multiple possible IP addresses
117 * to find out which one might work.
123 * This is a linked list.
125 struct AddressProbe *next;
128 * This is a doubly-linked list.
130 struct AddressProbe *prev;
133 * The address; do not free (allocated at the end of this struct).
135 const struct sockaddr *addr;
138 * Underlying OS's socket.
140 struct GNUNET_NETWORK_Handle *sock;
143 * Connection for which we are probing.
145 struct GNUNET_CONNECTION_Handle *h;
153 * Task waiting for the socket to finish connecting.
155 GNUNET_SCHEDULER_TaskIdentifier task;
160 * @brief handle for a network socket
162 struct GNUNET_CONNECTION_Handle
166 * Configuration to use.
168 const struct GNUNET_CONFIGURATION_Handle *cfg;
171 * Linked list of sockets we are currently trying out
174 struct AddressProbe *ap_head;
177 * Linked list of sockets we are currently trying out
180 struct AddressProbe *ap_tail;
183 * Network address of the other end-point, may be NULL.
185 struct sockaddr *addr;
188 * Pointer to the hostname if socket was
189 * created using DNS lookup, otherwise NULL.
194 * Underlying OS's socket, set to NULL after fatal errors.
196 struct GNUNET_NETWORK_Handle *sock;
199 * Function to call on data received, NULL if no receive is pending.
201 GNUNET_CONNECTION_Receiver receiver;
204 * Closure for receiver.
209 * Pointer to our write buffer.
214 * Current size of our write buffer.
216 size_t write_buffer_size;
219 * Current write-offset in write buffer (where
220 * would we write next).
222 size_t write_buffer_off;
225 * Current read-offset in write buffer (how many
226 * bytes have already been sent).
228 size_t write_buffer_pos;
236 * Read task that we may need to wait for.
238 GNUNET_SCHEDULER_TaskIdentifier read_task;
241 * Write task that we may need to wait for.
243 GNUNET_SCHEDULER_TaskIdentifier write_task;
246 * Destroy task (if already scheduled).
248 GNUNET_SCHEDULER_TaskIdentifier destroy_task;
251 * Handle to a pending DNS lookup request.
253 struct GNUNET_RESOLVER_RequestHandle *dns_active;
256 * The handle we return for GNUNET_CONNECTION_notify_transmit_ready.
258 struct GNUNET_CONNECTION_TransmitHandle nth;
261 * Timeout for receiving (in absolute time).
263 struct GNUNET_TIME_Absolute receive_timeout;
266 * Functions to call after connect failed or succeeded.
268 enum ConnectContinuations ccs;
271 * Maximum number of bytes to read (for receiving).
276 * Ignore GNUNET_SCHEDULER_REASON_SHUTDOWN for this socket.
281 * Port to connect to.
286 * When shutdown, do not ever actually close the socket, but
287 * free resources. Only should ever be set if using program
288 * termination as a signal (because only then will the leaked
296 * Set the persist option on this connection handle. Indicates
297 * that the underlying socket or fd should never really be closed.
298 * Used for indicating process death.
300 * @param sock the connection to set persistent
303 GNUNET_CONNECTION_persist_ (struct GNUNET_CONNECTION_Handle *sock)
305 sock->persist = GNUNET_YES;
310 * Disable the "CORK" feature for communication with the given socket,
311 * forcing the OS to immediately flush the buffer on transmission
312 * instead of potentially buffering multiple messages. Essentially
313 * reduces the OS send buffers to zero.
314 * Used to make sure that the last messages sent through the connection
315 * reach the other side before the process is terminated.
317 * @param sock the connection to make flushing and blocking
318 * @return GNUNET_OK on success
321 GNUNET_CONNECTION_disable_corking (struct GNUNET_CONNECTION_Handle *sock)
323 return GNUNET_NETWORK_socket_disable_corking (sock->sock);
327 * Create a socket handle by boxing an existing OS socket. The OS
328 * socket should henceforth be no longer used directly.
329 * GNUNET_socket_destroy will close it.
331 * @param osSocket existing socket to box
332 * @return the boxed socket handle
334 struct GNUNET_CONNECTION_Handle *
335 GNUNET_CONNECTION_create_from_existing (struct GNUNET_NETWORK_Handle *osSocket)
337 struct GNUNET_CONNECTION_Handle *ret;
339 ret = GNUNET_malloc (sizeof (struct GNUNET_CONNECTION_Handle));
340 ret->write_buffer_size = GNUNET_SERVER_MIN_BUFFER_SIZE;
341 ret->write_buffer = GNUNET_malloc (ret->write_buffer_size);
342 ret->sock = osSocket;
348 * Create a socket handle by accepting on a listen socket. This
349 * function may block if the listen socket has no connection ready.
351 * @param access function to use to check if access is allowed
352 * @param access_cls closure for access
353 * @param lsock listen socket
354 * @return the socket handle, NULL on error
356 struct GNUNET_CONNECTION_Handle *
357 GNUNET_CONNECTION_create_from_accept (GNUNET_CONNECTION_AccessCheck access,
359 struct GNUNET_NETWORK_Handle *lsock)
361 struct GNUNET_CONNECTION_Handle *ret;
364 struct GNUNET_NETWORK_Handle *sock;
366 struct sockaddr_in *v4;
367 struct sockaddr_in6 *v6;
370 struct GNUNET_CONNECTION_Credentials *gcp;
371 struct GNUNET_CONNECTION_Credentials gc;
378 addrlen = sizeof (addr);
380 GNUNET_NETWORK_socket_accept (lsock, (struct sockaddr *) &addr, &addrlen);
383 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "accept");
386 if ((addrlen > sizeof (addr)) || (addrlen < sizeof (sa_family_t)))
389 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
393 sa = (struct sockaddr *) addr;
394 v6 = (struct sockaddr_in6 *) addr;
395 if ((sa->sa_family == AF_INET6) && (IN6_IS_ADDR_V4MAPPED (&v6->sin6_addr)))
397 /* convert to V4 address */
398 v4 = GNUNET_malloc (sizeof (struct sockaddr_in));
399 memset (v4, 0, sizeof (struct sockaddr_in));
400 v4->sin_family = AF_INET;
401 #if HAVE_SOCKADDR_IN_SIN_LEN
402 v4->sin_len = (u_char) sizeof (struct sockaddr_in);
404 memcpy (&v4->sin_addr,
405 &((char *) &v6->sin6_addr)[sizeof (struct in6_addr) -
406 sizeof (struct in_addr)],
407 sizeof (struct in_addr));
408 v4->sin_port = v6->sin6_port;
410 addrlen = sizeof (struct sockaddr_in);
414 uaddr = GNUNET_malloc (addrlen);
415 memcpy (uaddr, addr, addrlen);
420 if (sa->sa_family == AF_UNIX)
424 if (0 == getpeereid (GNUNET_NETWORK_get_fd (sock), &gc.uid, &gc.gid))
428 /* largely traditional GNU/Linux */
431 getsockopt (GNUNET_NETWORK_get_fd (sock), SOL_SOCKET, SO_PEERCRED, &uc,
432 &olen)) && (olen == sizeof (uc)))
439 #if HAVE_GETPEERUCRED
440 /* this is for Solaris 10 */
444 if (0 == getpeerucred (GNUNET_NETWORK_get_fd (sock), &uc))
446 gc.uid = ucred_geteuid (uc);
447 gc.gid = ucred_getegid (uc);
456 if ((access != NULL) &&
457 (GNUNET_YES != (aret = access (access_cls, gcp, uaddr, addrlen))))
459 if (aret == GNUNET_NO)
460 LOG (GNUNET_ERROR_TYPE_INFO, _("Access denied to `%s'\n"),
461 GNUNET_a2s (uaddr, addrlen));
462 GNUNET_break (GNUNET_OK ==
463 GNUNET_NETWORK_socket_shutdown (sock, SHUT_RDWR));
464 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
468 ret = GNUNET_malloc (sizeof (struct GNUNET_CONNECTION_Handle));
469 ret->write_buffer_size = GNUNET_SERVER_MIN_BUFFER_SIZE;
470 ret->write_buffer = GNUNET_malloc (ret->write_buffer_size);
472 ret->addrlen = addrlen;
474 LOG (GNUNET_ERROR_TYPE_INFO,
475 _("Accepting connection from `%s': %p\n"),
476 GNUNET_a2s (uaddr, addrlen), ret);
481 * Obtain the network address of the other party.
483 * @param sock the client to get the address for
484 * @param addr where to store the address
485 * @param addrlen where to store the length of the address
486 * @return GNUNET_OK on success
489 GNUNET_CONNECTION_get_address (struct GNUNET_CONNECTION_Handle *sock,
490 void **addr, size_t * addrlen)
492 if ((sock->addr == NULL) || (sock->addrlen == 0))
494 *addr = GNUNET_malloc (sock->addrlen);
495 memcpy (*addr, sock->addr, sock->addrlen);
496 *addrlen = sock->addrlen;
502 * This function is called after establishing a connection either has
503 * succeeded or timed out. Note that it is possible that the attempt
504 * timed out and that we're immediately retrying. If we are retrying,
505 * we need to wait again (or timeout); if we succeeded, we need to
506 * wait for data (or timeout).
508 * @param cls our connection handle
509 * @param tc task context describing why we are here
512 receive_again (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
516 * Scheduler let us know that the connect task is finished (or was
517 * cancelled due to shutdown). Now really clean up.
519 * @param cls our "struct GNUNET_CONNECTION_Handle *"
523 destroy_continuation (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
525 struct GNUNET_CONNECTION_Handle *sock = cls;
526 GNUNET_CONNECTION_TransmitReadyNotify notify;
527 struct AddressProbe *pos;
529 sock->destroy_task = GNUNET_SCHEDULER_NO_TASK;
530 GNUNET_assert (sock->dns_active == NULL);
531 if (0 != (sock->ccs & COCO_TRANSMIT_READY))
533 LOG (GNUNET_ERROR_TYPE_DEBUG, "Destroy waits for CCS-TR to be done (%p)\n",
535 sock->ccs |= COCO_DESTROY_CONTINUATION;
538 if (sock->write_task != GNUNET_SCHEDULER_NO_TASK)
540 LOG (GNUNET_ERROR_TYPE_DEBUG,
541 "Destroy waits for write_task to be done (%p)\n", sock);
542 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task);
544 GNUNET_SCHEDULER_add_after (sock->write_task, &destroy_continuation,
548 if (0 != (sock->ccs & COCO_RECEIVE_AGAIN))
550 sock->ccs |= COCO_DESTROY_CONTINUATION;
553 if (sock->sock != NULL)
555 LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down socket (%p)\n", sock);
556 if (sock->persist != GNUNET_YES)
558 if ((GNUNET_YES != GNUNET_NETWORK_socket_shutdown (sock->sock, SHUT_RDWR))
559 && (errno != ENOTCONN) && (errno != ECONNRESET))
560 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "shutdown");
563 if (sock->read_task != GNUNET_SCHEDULER_NO_TASK)
565 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task);
567 GNUNET_SCHEDULER_add_after (sock->read_task, &destroy_continuation,
571 LOG (GNUNET_ERROR_TYPE_DEBUG, "Destroy actually runs (%p)!\n", sock);
572 while (NULL != (pos = sock->ap_head))
574 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (pos->sock));
575 GNUNET_SCHEDULER_cancel (pos->task);
576 GNUNET_CONTAINER_DLL_remove (sock->ap_head, sock->ap_tail, pos);
579 GNUNET_assert (sock->nth.timeout_task == GNUNET_SCHEDULER_NO_TASK);
580 GNUNET_assert (sock->ccs == COCO_NONE);
581 if (NULL != (notify = sock->nth.notify_ready))
583 sock->nth.notify_ready = NULL;
584 notify (sock->nth.notify_ready_cls, 0, NULL);
587 if (sock->sock != NULL)
589 if (sock->persist != GNUNET_YES)
590 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock->sock));
592 GNUNET_free (sock->sock); /* at least no memory leak (we deliberately
593 * leak the socket in this special case) ... */
595 GNUNET_free_non_null (sock->addr);
596 GNUNET_free_non_null (sock->hostname);
597 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task);
598 LOG (GNUNET_ERROR_TYPE_DEBUG, "Freeing memory of connection %p.\n", sock);
599 GNUNET_free (sock->write_buffer);
606 * See if we are now connected. If not, wait longer for
607 * connect to succeed. If connected, we should be able
608 * to write now as well, unless we timed out.
610 * @param cls our connection handle
611 * @param tc task context describing why we are here
614 transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
618 * We've failed for good to establish a connection.
620 * @param h the connection we tried to establish
623 connect_fail_continuation (struct GNUNET_CONNECTION_Handle *h)
626 strncmp (h->hostname, "localhost:",
627 10)) ? GNUNET_ERROR_TYPE_INFO : GNUNET_ERROR_TYPE_WARNING,
629 ("Failed to establish TCP connection to `%s:%u', no further addresses to try.\n"),
630 h->hostname, h->port);
631 /* connect failed / timed out */
632 GNUNET_break (h->ap_head == NULL);
633 GNUNET_break (h->ap_tail == NULL);
634 GNUNET_break (h->dns_active == GNUNET_NO);
635 GNUNET_break (h->sock == NULL);
637 /* trigger jobs that used to wait on "connect_task" */
638 if (0 != (h->ccs & COCO_RECEIVE_AGAIN))
640 LOG (GNUNET_ERROR_TYPE_DEBUG,
641 "connect_fail_continuation triggers receive_again (%p)\n", h);
642 h->ccs -= COCO_RECEIVE_AGAIN;
643 h->read_task = GNUNET_SCHEDULER_add_now (&receive_again, h);
645 if (0 != (h->ccs & COCO_TRANSMIT_READY))
647 LOG (GNUNET_ERROR_TYPE_DEBUG,
648 "connect_fail_continuation cancels timeout_task, triggers transmit_ready (%p)\n",
650 GNUNET_assert (h->nth.timeout_task != GNUNET_SCHEDULER_NO_TASK);
651 GNUNET_SCHEDULER_cancel (h->nth.timeout_task);
652 h->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK;
653 h->ccs -= COCO_TRANSMIT_READY;
654 GNUNET_assert (h->nth.notify_ready != NULL);
655 GNUNET_assert (h->write_task == GNUNET_SCHEDULER_NO_TASK);
656 h->write_task = GNUNET_SCHEDULER_add_now (&transmit_ready, h);
658 if (0 != (h->ccs & COCO_DESTROY_CONTINUATION))
660 LOG (GNUNET_ERROR_TYPE_DEBUG,
661 "connect_fail_continuation runs destroy_continuation (%p)\n", h);
662 h->ccs -= COCO_DESTROY_CONTINUATION;
663 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == h->destroy_task);
664 h->destroy_task = GNUNET_SCHEDULER_add_now (&destroy_continuation, h);
670 * We've succeeded in establishing a connection.
672 * @param h the connection we tried to establish
675 connect_success_continuation (struct GNUNET_CONNECTION_Handle *h)
677 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection to `%s' succeeded! (%p)\n",
678 GNUNET_a2s (h->addr, h->addrlen), h);
679 /* trigger jobs that waited for the connection */
680 if (0 != (h->ccs & COCO_RECEIVE_AGAIN))
682 LOG (GNUNET_ERROR_TYPE_DEBUG,
683 "connect_success_continuation runs receive_again (%p)\n", h);
684 h->ccs -= COCO_RECEIVE_AGAIN;
685 h->read_task = GNUNET_SCHEDULER_add_now (&receive_again, h);
687 if (0 != (h->ccs & COCO_TRANSMIT_READY))
689 LOG (GNUNET_ERROR_TYPE_DEBUG,
690 "connect_success_continuation runs transmit_ready, cancels timeout_task (%p)\n",
692 GNUNET_assert (h->nth.timeout_task != GNUNET_SCHEDULER_NO_TASK);
693 GNUNET_SCHEDULER_cancel (h->nth.timeout_task);
694 h->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK;
695 h->ccs -= COCO_TRANSMIT_READY;
696 GNUNET_assert (h->write_task == GNUNET_SCHEDULER_NO_TASK);
697 GNUNET_assert (h->nth.notify_ready != NULL);
699 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_absolute_get_remaining
700 (h->nth.transmit_timeout), h->sock,
703 if (0 != (h->ccs & COCO_DESTROY_CONTINUATION))
705 LOG (GNUNET_ERROR_TYPE_DEBUG,
706 "connect_success_continuation runs destroy_continuation (%p)\n", h);
707 h->ccs -= COCO_DESTROY_CONTINUATION;
708 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == h->destroy_task);
709 h->destroy_task = GNUNET_SCHEDULER_add_now (&destroy_continuation, h);
715 * Scheduler let us know that we're either ready to write on the
716 * socket OR connect timed out. Do the right thing.
718 * @param cls the "struct AddressProbe*" with the address that we are probing
719 * @param tc success or failure info about the connect attempt.
722 connect_probe_continuation (void *cls,
723 const struct GNUNET_SCHEDULER_TaskContext *tc)
725 struct AddressProbe *ap = cls;
726 struct GNUNET_CONNECTION_Handle *h = ap->h;
727 struct AddressProbe *pos;
731 GNUNET_assert (ap->sock != NULL);
732 GNUNET_CONTAINER_DLL_remove (h->ap_head, h->ap_tail, ap);
733 len = sizeof (error);
736 if ((0 == (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) ||
738 GNUNET_NETWORK_socket_getsockopt (ap->sock, SOL_SOCKET, SO_ERROR, &error,
739 &len)) || (error != 0))
741 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ap->sock));
743 if ((NULL == h->ap_head) && (h->dns_active == GNUNET_NO))
744 connect_fail_continuation (h);
747 GNUNET_assert (h->sock == NULL);
749 GNUNET_assert (h->addr == NULL);
750 h->addr = GNUNET_malloc (ap->addrlen);
751 memcpy (h->addr, ap->addr, ap->addrlen);
752 h->addrlen = ap->addrlen;
754 /* cancel all other attempts */
755 while (NULL != (pos = h->ap_head))
757 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (pos->sock));
758 GNUNET_SCHEDULER_cancel (pos->task);
759 GNUNET_CONTAINER_DLL_remove (h->ap_head, h->ap_tail, pos);
762 connect_success_continuation (h);
767 * Try to establish a socket connection given the specified address.
768 * This function is called by the resolver once we have a DNS reply.
770 * @param cls our "struct GNUNET_CONNECTION_Handle *"
771 * @param addr address to try, NULL for "last call"
772 * @param addrlen length of addr
775 try_connect_using_address (void *cls, const struct sockaddr *addr,
778 struct GNUNET_CONNECTION_Handle *h = cls;
779 struct AddressProbe *ap;
780 struct GNUNET_TIME_Relative delay;
784 h->dns_active = NULL;
785 if ((NULL == h->ap_head) && (NULL == h->sock))
786 connect_fail_continuation (h);
790 return; /* already connected */
791 GNUNET_assert (h->addr == NULL);
793 LOG (GNUNET_ERROR_TYPE_DEBUG,
794 "Trying to connect using address `%s:%u/%s:%u'\n", h->hostname, h->port,
795 GNUNET_a2s (addr, addrlen), h->port);
796 ap = GNUNET_malloc (sizeof (struct AddressProbe) + addrlen);
797 ap->addr = (const struct sockaddr *) &ap[1];
798 memcpy (&ap[1], addr, addrlen);
799 ap->addrlen = addrlen;
802 switch (ap->addr->sa_family)
805 ((struct sockaddr_in *) ap->addr)->sin_port = htons (h->port);
808 ((struct sockaddr_in6 *) ap->addr)->sin6_port = htons (h->port);
813 return; /* not supported by us */
815 ap->sock = GNUNET_NETWORK_socket_create (ap->addr->sa_family, SOCK_STREAM, 0);
816 if (ap->sock == NULL)
819 return; /* not supported by OS */
821 LOG (GNUNET_ERROR_TYPE_INFO, _("Trying to connect to `%s' (%p)\n"),
822 GNUNET_a2s (ap->addr, ap->addrlen), h);
824 GNUNET_NETWORK_socket_connect (ap->sock, ap->addr, ap->addrlen)) &&
825 (errno != EINPROGRESS))
827 /* maybe refused / unsupported address, try next */
828 LOG_STRERROR (GNUNET_ERROR_TYPE_INFO, "connect");
830 LOG (GNUNET_ERROR_TYPE_INFO, _("Failed to connect to `%s' (%p)\n"),
831 GNUNET_a2s (ap->addr, ap->addrlen), h);
833 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ap->sock));
837 GNUNET_CONTAINER_DLL_insert (h->ap_head, h->ap_tail, ap);
838 delay = GNUNET_CONNECTION_CONNECT_RETRY_TIMEOUT;
839 if (h->nth.notify_ready != NULL)
841 GNUNET_TIME_relative_min (delay,
842 GNUNET_TIME_absolute_get_remaining (h->
843 nth.transmit_timeout));
844 if (h->receiver != NULL)
846 GNUNET_TIME_relative_min (delay,
847 GNUNET_TIME_absolute_get_remaining
848 (h->receive_timeout));
850 GNUNET_SCHEDULER_add_write_net (delay, ap->sock,
851 &connect_probe_continuation, ap);
856 * Create a socket handle by (asynchronously) connecting to a host.
857 * This function returns immediately, even if the connection has not
858 * yet been established. This function only creates TCP connections.
860 * @param cfg configuration to use
861 * @param hostname name of the host to connect to
862 * @param port port to connect to
863 * @return the socket handle
865 struct GNUNET_CONNECTION_Handle *
866 GNUNET_CONNECTION_create_from_connect (const struct GNUNET_CONFIGURATION_Handle
867 *cfg, const char *hostname,
870 struct GNUNET_CONNECTION_Handle *ret;
872 GNUNET_assert (0 < strlen (hostname)); /* sanity check */
873 ret = GNUNET_malloc (sizeof (struct GNUNET_CONNECTION_Handle));
875 ret->write_buffer_size = GNUNET_SERVER_MIN_BUFFER_SIZE;
876 ret->write_buffer = GNUNET_malloc (ret->write_buffer_size);
878 ret->hostname = GNUNET_strdup (hostname);
880 GNUNET_RESOLVER_ip_get (ret->hostname, AF_UNSPEC,
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 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 (const struct
898 GNUNET_CONFIGURATION_Handle
899 *cfg, const char *unixpath)
902 struct GNUNET_CONNECTION_Handle *ret;
903 struct sockaddr_un *un;
906 GNUNET_assert (0 < strlen (unixpath)); /* sanity check */
907 un = GNUNET_malloc (sizeof (struct sockaddr_un));
908 un->sun_family = AF_UNIX;
909 slen = strlen (unixpath);
910 if (slen >= sizeof (un->sun_path))
911 slen = sizeof (un->sun_path) - 1;
912 memcpy (un->sun_path, unixpath, slen);
913 un->sun_path[slen] = '\0';
914 slen = sizeof (struct sockaddr_un);
915 #if HAVE_SOCKADDR_IN_SIN_LEN
916 un->sun_len = (u_char) slen;
919 un->sun_path[0] = '\0';
921 ret = GNUNET_malloc (sizeof (struct GNUNET_CONNECTION_Handle));
923 ret->write_buffer_size = GNUNET_SERVER_MIN_BUFFER_SIZE;
924 ret->write_buffer = GNUNET_malloc (ret->write_buffer_size);
926 ret->hostname = NULL;
927 ret->addr = (struct sockaddr *) un;
929 ret->sock = GNUNET_NETWORK_socket_create (AF_UNIX, SOCK_STREAM, 0);
930 if (NULL == ret->sock)
932 GNUNET_free (ret->addr);
933 GNUNET_free (ret->write_buffer);
938 GNUNET_NETWORK_socket_connect (ret->sock, ret->addr, ret->addrlen))
940 /* Just return; we expect everything to work eventually so don't fail HARD */
941 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret->sock));
945 connect_success_continuation (ret);
954 * Create a socket handle by (asynchronously) connecting to a host.
955 * This function returns immediately, even if the connection has not
956 * yet been established. This function only creates TCP connections.
958 * @param af_family address family to use
959 * @param serv_addr server address
960 * @param addrlen length of server address
961 * @return the socket handle
963 struct GNUNET_CONNECTION_Handle *
964 GNUNET_CONNECTION_create_from_sockaddr (int af_family,
965 const struct sockaddr *serv_addr,
968 struct GNUNET_NETWORK_Handle *s;
969 struct GNUNET_CONNECTION_Handle *ret;
972 s = GNUNET_NETWORK_socket_create (af_family, SOCK_STREAM, 0);
975 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK, "socket");
978 if ((GNUNET_OK != GNUNET_NETWORK_socket_connect (s, serv_addr, addrlen)) &&
979 (errno != EINPROGRESS))
981 /* maybe refused / unsupported address, try next */
982 LOG_STRERROR (GNUNET_ERROR_TYPE_INFO, "connect");
983 LOG (GNUNET_ERROR_TYPE_INFO, _("Attempt to connect to `%s' failed\n"),
984 GNUNET_a2s (serv_addr, addrlen));
985 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (s));
988 ret = GNUNET_CONNECTION_create_from_existing (s);
989 ret->addr = GNUNET_malloc (addrlen);
990 memcpy (ret->addr, serv_addr, addrlen);
991 ret->addrlen = addrlen;
992 LOG (GNUNET_ERROR_TYPE_INFO, _("Trying to connect to `%s' (%p)\n"),
993 GNUNET_a2s (serv_addr, addrlen), ret);
999 * Check if socket is valid (no fatal errors have happened so far).
1000 * Note that a socket that is still trying to connect is considered
1003 * @param sock socket to check
1004 * @return GNUNET_YES if valid, GNUNET_NO otherwise
1007 GNUNET_CONNECTION_check (struct GNUNET_CONNECTION_Handle *sock)
1009 if ((sock->ap_head != NULL) || (sock->dns_active != NULL))
1010 return GNUNET_YES; /* still trying to connect */
1011 return (sock->sock == NULL) ? GNUNET_NO : GNUNET_YES;
1016 * Close the socket and free associated resources. Pending
1017 * transmissions may be completed or dropped depending on the
1018 * arguments. If a receive call is pending and should
1019 * NOT be completed, 'GNUNET_CONNECTION_receive_cancel'
1020 * should be called explicitly first.
1022 * @param sock socket to destroy
1023 * @param finish_pending_write should pending writes be completed or aborted?
1024 * (this applies to transmissions where the data has already been
1025 * read from the application; all other transmissions should be
1026 * aborted using 'GNUNET_CONNECTION_notify_transmit_ready_cancel').
1029 GNUNET_CONNECTION_destroy (struct GNUNET_CONNECTION_Handle *sock,
1030 int finish_pending_write)
1032 if (GNUNET_NO == finish_pending_write)
1034 if (sock->write_task != GNUNET_SCHEDULER_NO_TASK)
1036 GNUNET_SCHEDULER_cancel (sock->write_task);
1037 sock->write_task = GNUNET_SCHEDULER_NO_TASK;
1038 sock->write_buffer_off = 0;
1040 sock->nth.notify_ready = NULL;
1042 if ((sock->write_buffer_off == 0) && (sock->dns_active != NULL))
1044 GNUNET_RESOLVER_request_cancel (sock->dns_active);
1045 sock->dns_active = NULL;
1048 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task);
1049 sock->destroy_task = GNUNET_SCHEDULER_add_now (&destroy_continuation, sock);
1054 * Tell the receiver callback that a timeout was reached.
1057 signal_timeout (struct GNUNET_CONNECTION_Handle *sh)
1059 GNUNET_CONNECTION_Receiver receiver;
1061 LOG (GNUNET_ERROR_TYPE_DEBUG, "Network signals time out to receiver (%p)!\n",
1063 GNUNET_assert (NULL != (receiver = sh->receiver));
1064 sh->receiver = NULL;
1065 receiver (sh->receiver_cls, NULL, 0, NULL, 0, 0);
1070 * Tell the receiver callback that we had an IO error.
1073 signal_error (struct GNUNET_CONNECTION_Handle *sh, int errcode)
1075 GNUNET_CONNECTION_Receiver receiver;
1077 GNUNET_assert (NULL != (receiver = sh->receiver));
1078 sh->receiver = NULL;
1079 receiver (sh->receiver_cls, NULL, 0, sh->addr, sh->addrlen, errcode);
1084 * This function is called once we either timeout
1085 * or have data ready to read.
1088 receive_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1090 struct GNUNET_CONNECTION_Handle *sh = cls;
1091 struct GNUNET_TIME_Absolute now;
1092 char buffer[sh->max];
1094 GNUNET_CONNECTION_Receiver receiver;
1096 sh->read_task = GNUNET_SCHEDULER_NO_TASK;
1097 if ((GNUNET_YES == sh->ignore_shutdown) &&
1098 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
1100 /* ignore shutdown request, go again immediately */
1101 LOG (GNUNET_ERROR_TYPE_DEBUG,
1102 "Ignoring shutdown signal per configuration\n");
1104 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_absolute_get_remaining
1105 (sh->receive_timeout), sh->sock,
1106 &receive_ready, sh);
1109 now = GNUNET_TIME_absolute_get ();
1110 if ((now.abs_value > sh->receive_timeout.abs_value) ||
1111 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT)) ||
1112 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
1114 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1115 LOG (GNUNET_ERROR_TYPE_DEBUG,
1116 "Receive from `%s' encounters error: time out by %llums... (%p)\n",
1117 GNUNET_a2s (sh->addr, sh->addrlen),
1118 GNUNET_TIME_absolute_get_duration (sh->receive_timeout).rel_value,
1120 signal_timeout (sh);
1123 if (sh->sock == NULL)
1125 /* connect failed for good */
1126 LOG (GNUNET_ERROR_TYPE_DEBUG,
1127 "Receive encounters error, socket closed... (%p)\n", sh);
1128 signal_error (sh, ECONNREFUSED);
1131 GNUNET_assert (GNUNET_NETWORK_fdset_isset (tc->read_ready, sh->sock));
1133 ret = GNUNET_NETWORK_socket_recv (sh->sock, buffer, sh->max);
1138 LOG (GNUNET_ERROR_TYPE_DEBUG, "Error receiving: %s\n", STRERROR (errno));
1139 signal_error (sh, errno);
1142 LOG (GNUNET_ERROR_TYPE_DEBUG,
1143 "receive_ready read %u/%u bytes from `%s' (%p)!\n", (unsigned int) ret,
1144 sh->max, GNUNET_a2s (sh->addr, sh->addrlen), sh);
1145 GNUNET_assert (NULL != (receiver = sh->receiver));
1146 sh->receiver = NULL;
1147 receiver (sh->receiver_cls, buffer, ret, sh->addr, sh->addrlen, 0);
1152 * This function is called after establishing a connection either has
1153 * succeeded or timed out. Note that it is possible that the attempt
1154 * timed out and that we're immediately retrying. If we are retrying,
1155 * we need to wait again (or timeout); if we succeeded, we need to
1156 * wait for data (or timeout).
1158 * @param cls our connection handle
1159 * @param tc task context describing why we are here
1162 receive_again (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1164 struct GNUNET_CONNECTION_Handle *sh = cls;
1165 struct GNUNET_TIME_Absolute now;
1167 sh->read_task = GNUNET_SCHEDULER_NO_TASK;
1168 if (sh->sock == NULL)
1170 /* not connected and no longer trying */
1171 LOG (GNUNET_ERROR_TYPE_DEBUG,
1172 "Receive encounters error, socket closed (%p)...\n", sh);
1173 signal_error (sh, ECONNREFUSED);
1176 now = GNUNET_TIME_absolute_get ();
1177 if ((now.abs_value > sh->receive_timeout.abs_value) ||
1178 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
1180 LOG (GNUNET_ERROR_TYPE_DEBUG,
1181 "Receive encounters error: time out (%p)...\n", sh);
1182 signal_timeout (sh);
1185 GNUNET_assert (sh->sock != NULL);
1186 /* connect succeeded, wait for data! */
1188 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_absolute_get_remaining
1189 (sh->receive_timeout), sh->sock,
1190 &receive_ready, sh);
1195 * Receive data from the given socket. Note that this function will
1196 * call "receiver" asynchronously using the scheduler. It will
1197 * "immediately" return. Note that there MUST only be one active
1198 * receive call per socket at any given point in time (so do not
1199 * call receive again until the receiver callback has been invoked).
1201 * @param sock socket handle
1202 * @param max maximum number of bytes to read
1203 * @param timeout maximum amount of time to wait (use -1 for "forever")
1204 * @param receiver function to call with received data
1205 * @param receiver_cls closure for receiver
1208 GNUNET_CONNECTION_receive (struct GNUNET_CONNECTION_Handle *sock, size_t max,
1209 struct GNUNET_TIME_Relative timeout,
1210 GNUNET_CONNECTION_Receiver receiver,
1213 struct GNUNET_SCHEDULER_TaskContext tc;
1215 GNUNET_assert ((sock->read_task == GNUNET_SCHEDULER_NO_TASK) &&
1216 (0 == (sock->ccs & COCO_RECEIVE_AGAIN)) &&
1217 (sock->receiver == NULL));
1218 sock->receiver = receiver;
1219 sock->receiver_cls = receiver_cls;
1220 sock->receive_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1222 if (sock->sock != NULL)
1224 memset (&tc, 0, sizeof (tc));
1225 tc.reason = GNUNET_SCHEDULER_REASON_PREREQ_DONE;
1226 receive_again (sock, &tc);
1229 if ((sock->dns_active == NULL) && (sock->ap_head == NULL))
1231 receiver (receiver_cls, NULL, 0, NULL, 0, ETIMEDOUT);
1234 sock->ccs += COCO_RECEIVE_AGAIN;
1239 * Configure this connection to ignore shutdown signals.
1241 * @param sock socket handle
1242 * @param do_ignore GNUNET_YES to ignore, GNUNET_NO to restore default
1245 GNUNET_CONNECTION_ignore_shutdown (struct GNUNET_CONNECTION_Handle *sock,
1248 sock->ignore_shutdown = do_ignore;
1253 * Cancel receive job on the given socket. Note that the
1254 * receiver callback must not have been called yet in order
1255 * for the cancellation to be valid.
1257 * @param sock socket handle
1258 * @return closure of the original receiver callback closure
1261 GNUNET_CONNECTION_receive_cancel (struct GNUNET_CONNECTION_Handle *sock)
1263 if (sock->read_task != GNUNET_SCHEDULER_NO_TASK)
1265 GNUNET_assert (sock == GNUNET_SCHEDULER_cancel (sock->read_task));
1266 sock->read_task = GNUNET_SCHEDULER_NO_TASK;
1270 GNUNET_assert (0 != (sock->ccs & COCO_RECEIVE_AGAIN));
1271 sock->ccs -= COCO_RECEIVE_AGAIN;
1273 sock->receiver = NULL;
1274 return sock->receiver_cls;
1279 * Try to call the transmit notify method (check if we do
1280 * have enough space available first)!
1282 * @param sock socket for which we should do this processing
1283 * @return GNUNET_YES if we were able to call notify
1286 process_notify (struct GNUNET_CONNECTION_Handle *sock)
1291 GNUNET_CONNECTION_TransmitReadyNotify notify;
1293 GNUNET_assert (sock->write_task == GNUNET_SCHEDULER_NO_TASK);
1294 if (NULL == (notify = sock->nth.notify_ready))
1296 used = sock->write_buffer_off - sock->write_buffer_pos;
1297 avail = sock->write_buffer_size - used;
1298 size = sock->nth.notify_size;
1301 sock->nth.notify_ready = NULL;
1302 if (sock->write_buffer_size - sock->write_buffer_off < size)
1304 /* need to compact */
1305 memmove (sock->write_buffer, &sock->write_buffer[sock->write_buffer_pos],
1307 sock->write_buffer_off -= sock->write_buffer_pos;
1308 sock->write_buffer_pos = 0;
1310 avail = sock->write_buffer_size - sock->write_buffer_off;
1311 GNUNET_assert (avail >= size);
1313 notify (sock->nth.notify_ready_cls, avail,
1314 &sock->write_buffer[sock->write_buffer_off]);
1315 GNUNET_assert (size <= avail);
1316 sock->write_buffer_off += size;
1322 * Task invoked by the scheduler when a call to transmit
1323 * is timing out (we never got enough buffer space to call
1324 * the callback function before the specified timeout
1327 * This task notifies the client about the timeout.
1329 * @param cls the 'struct GNUNET_CONNECTION_Handle'
1330 * @param tc scheduler context
1333 transmit_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1335 struct GNUNET_CONNECTION_Handle *sock = cls;
1336 GNUNET_CONNECTION_TransmitReadyNotify notify;
1338 sock->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK;
1339 LOG (GNUNET_ERROR_TYPE_DEBUG,
1340 "Transmit to `%s:%u/%s' fails, time out reached (%p).\n",
1342 sock->port, GNUNET_a2s (sock->addr, sock->addrlen), sock);
1343 GNUNET_assert (0 != (sock->ccs & COCO_TRANSMIT_READY));
1344 sock->ccs -= COCO_TRANSMIT_READY; /* remove request */
1345 notify = sock->nth.notify_ready;
1346 sock->nth.notify_ready = NULL;
1347 notify (sock->nth.notify_ready_cls, 0, NULL);
1352 * Task invoked by the scheduler when we failed to connect
1353 * at the time of being asked to transmit.
1355 * This task notifies the client about the error.
1357 * @param cls the 'struct GNUNET_CONNECTION_Handle'
1358 * @param tc scheduler context
1361 connect_error (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1363 struct GNUNET_CONNECTION_Handle *sock = cls;
1364 GNUNET_CONNECTION_TransmitReadyNotify notify;
1366 LOG (GNUNET_ERROR_TYPE_DEBUG,
1367 "Transmission request of size %u fails (%s/%u), connection failed (%p).\n",
1368 sock->nth.notify_size, sock->hostname, sock->port, sock);
1369 sock->write_task = GNUNET_SCHEDULER_NO_TASK;
1370 notify = sock->nth.notify_ready;
1371 sock->nth.notify_ready = NULL;
1372 notify (sock->nth.notify_ready_cls, 0, NULL);
1382 transmit_error (struct GNUNET_CONNECTION_Handle *sock)
1384 GNUNET_CONNECTION_TransmitReadyNotify notify;
1386 if (NULL != sock->sock)
1388 GNUNET_NETWORK_socket_shutdown (sock->sock, SHUT_RDWR);
1389 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock->sock));
1391 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->write_task);
1393 if (sock->read_task != GNUNET_SCHEDULER_NO_TASK)
1395 GNUNET_SCHEDULER_cancel (sock->read_task);
1396 sock->read_task = GNUNET_SCHEDULER_NO_TASK;
1397 signal_timeout (sock);
1400 if (sock->nth.notify_ready == NULL)
1401 return; /* nobody to tell about it */
1402 notify = sock->nth.notify_ready;
1403 sock->nth.notify_ready = NULL;
1404 notify (sock->nth.notify_ready_cls, 0, NULL);
1409 * See if we are now connected. If not, wait longer for
1410 * connect to succeed. If connected, we should be able
1411 * to write now as well, unless we timed out.
1413 * @param cls our connection handle
1414 * @param tc task context describing why we are here
1417 transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1419 struct GNUNET_CONNECTION_Handle *sock = cls;
1420 GNUNET_CONNECTION_TransmitReadyNotify notify;
1424 LOG (GNUNET_ERROR_TYPE_DEBUG, "transmit_ready running (%p).\n", sock);
1425 GNUNET_assert (sock->write_task != GNUNET_SCHEDULER_NO_TASK);
1426 sock->write_task = GNUNET_SCHEDULER_NO_TASK;
1427 GNUNET_assert (sock->nth.timeout_task == GNUNET_SCHEDULER_NO_TASK);
1428 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1430 if ((sock->ignore_shutdown == GNUNET_YES) && (NULL != sock->sock))
1431 goto SCHEDULE_WRITE; /* ignore shutdown, go again immediately */
1432 LOG (GNUNET_ERROR_TYPE_DEBUG,
1433 "Transmit to `%s' fails, shutdown happened (%p).\n",
1434 GNUNET_a2s (sock->addr, sock->addrlen), sock);
1435 notify = sock->nth.notify_ready;
1438 sock->nth.notify_ready = NULL;
1439 notify (sock->nth.notify_ready_cls, 0, NULL);
1443 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT))
1445 LOG (GNUNET_ERROR_TYPE_DEBUG,
1446 "Transmit to `%s' fails, time out reached (%p).\n",
1447 GNUNET_a2s (sock->addr, sock->addrlen), sock);
1448 notify = sock->nth.notify_ready;
1449 GNUNET_assert (NULL != notify);
1450 sock->nth.notify_ready = NULL;
1451 notify (sock->nth.notify_ready_cls, 0, NULL);
1454 GNUNET_assert (NULL != sock->sock);
1455 if (tc->write_ready == NULL)
1457 /* special circumstances (in particular,
1458 * PREREQ_DONE after connect): not yet ready to write,
1459 * but no "fatal" error either. Hence retry. */
1460 goto SCHEDULE_WRITE;
1462 if (!GNUNET_NETWORK_fdset_isset (tc->write_ready, sock->sock))
1464 LOG (GNUNET_ERROR_TYPE_INFO,
1466 ("Could not satisfy pending transmission request, socket closed or connect failed (%p).\n"),
1468 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->write_task);
1469 transmit_error (sock);
1470 return; /* connect failed for good, we're finished */
1472 GNUNET_assert (sock->write_buffer_off >= sock->write_buffer_pos);
1473 if ((sock->nth.notify_ready != NULL) &&
1474 (sock->write_buffer_size < sock->nth.notify_size))
1476 sock->write_buffer =
1477 GNUNET_realloc (sock->write_buffer, sock->nth.notify_size);
1478 sock->write_buffer_size = sock->nth.notify_size;
1480 process_notify (sock);
1481 have = sock->write_buffer_off - sock->write_buffer_pos;
1484 /* no data ready for writing, terminate write loop */
1487 GNUNET_assert (have <= sock->write_buffer_size);
1488 GNUNET_assert (have + sock->write_buffer_pos <= sock->write_buffer_size);
1489 GNUNET_assert (sock->write_buffer_pos <= sock->write_buffer_size);
1492 GNUNET_NETWORK_socket_send (sock->sock,
1493 &sock->write_buffer[sock->write_buffer_pos],
1499 LOG_STRERROR (GNUNET_ERROR_TYPE_DEBUG, "send");
1500 if (GNUNET_SCHEDULER_NO_TASK != sock->write_task)
1502 GNUNET_SCHEDULER_cancel (sock->write_task);
1503 sock->write_task = GNUNET_SCHEDULER_NO_TASK;
1505 transmit_error (sock);
1508 LOG (GNUNET_ERROR_TYPE_DEBUG,
1509 "transmit_ready transmitted %u/%u bytes to `%s' (%p)\n",
1510 (unsigned int) ret, have, GNUNET_a2s (sock->addr, sock->addrlen), sock);
1511 sock->write_buffer_pos += ret;
1512 if (sock->write_buffer_pos == sock->write_buffer_off)
1514 /* transmitted all pending data */
1515 sock->write_buffer_pos = 0;
1516 sock->write_buffer_off = 0;
1518 if ((sock->write_buffer_off == 0) && (NULL == sock->nth.notify_ready))
1519 return; /* all data sent! */
1520 /* not done writing, schedule more */
1522 LOG (GNUNET_ERROR_TYPE_DEBUG,
1523 "Re-scheduling transmit_ready (more to do) (%p).\n", sock);
1524 have = sock->write_buffer_off - sock->write_buffer_pos;
1525 GNUNET_assert ((sock->nth.notify_ready != NULL) || (have > 0));
1526 if (sock->write_task == GNUNET_SCHEDULER_NO_TASK)
1528 GNUNET_SCHEDULER_add_write_net ((sock->nth.notify_ready ==
1529 NULL) ? GNUNET_TIME_UNIT_FOREVER_REL :
1530 GNUNET_TIME_absolute_get_remaining
1531 (sock->nth.transmit_timeout),
1532 sock->sock, &transmit_ready, sock);
1537 * Ask the socket to call us once the specified number of bytes
1538 * are free in the transmission buffer. May call the notify
1539 * method immediately if enough space is available.
1541 * @param sock socket
1542 * @param size number of bytes to send
1543 * @param timeout after how long should we give up (and call
1544 * notify with buf NULL and size 0)?
1545 * @param notify function to call
1546 * @param notify_cls closure for notify
1547 * @return non-NULL if the notify callback was queued,
1548 * NULL if we are already going to notify someone else (busy)
1550 struct GNUNET_CONNECTION_TransmitHandle *
1551 GNUNET_CONNECTION_notify_transmit_ready (struct GNUNET_CONNECTION_Handle *sock,
1553 struct GNUNET_TIME_Relative timeout,
1554 GNUNET_CONNECTION_TransmitReadyNotify
1555 notify, void *notify_cls)
1557 if (sock->nth.notify_ready != NULL)
1562 GNUNET_assert (notify != NULL);
1563 GNUNET_assert (size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
1564 GNUNET_assert (sock->write_buffer_off <= sock->write_buffer_size);
1565 GNUNET_assert (sock->write_buffer_pos <= sock->write_buffer_size);
1566 GNUNET_assert (sock->write_buffer_pos <= sock->write_buffer_off);
1567 sock->nth.notify_ready = notify;
1568 sock->nth.notify_ready_cls = notify_cls;
1569 sock->nth.sh = sock;
1570 sock->nth.notify_size = size;
1571 sock->nth.transmit_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1572 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->nth.timeout_task);
1573 if ((sock->sock == NULL) && (sock->ap_head == NULL) &&
1574 (sock->dns_active == NULL))
1576 if (sock->write_task != GNUNET_SCHEDULER_NO_TASK)
1577 GNUNET_SCHEDULER_cancel (sock->write_task);
1578 sock->write_task = GNUNET_SCHEDULER_add_now (&connect_error, sock);
1581 if (GNUNET_SCHEDULER_NO_TASK != sock->write_task)
1583 if (sock->sock != NULL)
1585 LOG (GNUNET_ERROR_TYPE_DEBUG, "Scheduling transmit_ready (%p).\n", sock);
1587 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_absolute_get_remaining
1588 (sock->nth.transmit_timeout),
1589 sock->sock, &transmit_ready, sock);
1593 LOG (GNUNET_ERROR_TYPE_DEBUG,
1594 "CCS-Scheduling transmit_ready, adding timeout task (%p).\n", sock);
1595 sock->ccs |= COCO_TRANSMIT_READY;
1596 sock->nth.timeout_task =
1597 GNUNET_SCHEDULER_add_delayed (timeout, &transmit_timeout, sock);
1604 * Cancel the specified transmission-ready notification.
1606 * @param th notification to cancel
1609 GNUNET_CONNECTION_notify_transmit_ready_cancel (struct
1610 GNUNET_CONNECTION_TransmitHandle
1613 GNUNET_assert (th->notify_ready != NULL);
1614 if (0 != (th->sh->ccs & COCO_TRANSMIT_READY))
1616 LOG (GNUNET_ERROR_TYPE_DEBUG,
1617 "notify_transmit_ready_cancel cancels timeout_task (%p)\n", th);
1618 GNUNET_SCHEDULER_cancel (th->timeout_task);
1619 th->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1620 th->sh->ccs -= COCO_TRANSMIT_READY;
1624 if (th->sh->write_task != GNUNET_SCHEDULER_NO_TASK)
1626 GNUNET_SCHEDULER_cancel (th->sh->write_task);
1627 th->sh->write_task = GNUNET_SCHEDULER_NO_TASK;
1630 th->notify_ready = NULL;
1633 /* end of connection.c */