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)
625 LOG (GNUNET_ERROR_TYPE_INFO,
626 _("Failed to establish TCP connection to `%s:%u', no further addresses to try.\n"),
627 h->hostname, h->port);
628 /* connect failed / timed out */
629 GNUNET_break (h->ap_head == NULL);
630 GNUNET_break (h->ap_tail == NULL);
631 GNUNET_break (h->dns_active == GNUNET_NO);
632 GNUNET_break (h->sock == NULL);
634 /* trigger jobs that used to wait on "connect_task" */
635 if (0 != (h->ccs & COCO_RECEIVE_AGAIN))
637 LOG (GNUNET_ERROR_TYPE_DEBUG,
638 "connect_fail_continuation triggers receive_again (%p)\n", h);
639 h->ccs -= COCO_RECEIVE_AGAIN;
640 h->read_task = GNUNET_SCHEDULER_add_now (&receive_again, h);
642 if (0 != (h->ccs & COCO_TRANSMIT_READY))
644 LOG (GNUNET_ERROR_TYPE_DEBUG,
645 "connect_fail_continuation cancels timeout_task, triggers transmit_ready (%p)\n",
647 GNUNET_assert (h->nth.timeout_task != GNUNET_SCHEDULER_NO_TASK);
648 GNUNET_SCHEDULER_cancel (h->nth.timeout_task);
649 h->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK;
650 h->ccs -= COCO_TRANSMIT_READY;
651 GNUNET_assert (h->nth.notify_ready != NULL);
652 GNUNET_assert (h->write_task == GNUNET_SCHEDULER_NO_TASK);
653 h->write_task = GNUNET_SCHEDULER_add_now (&transmit_ready, h);
655 if (0 != (h->ccs & COCO_DESTROY_CONTINUATION))
657 LOG (GNUNET_ERROR_TYPE_DEBUG,
658 "connect_fail_continuation runs destroy_continuation (%p)\n", h);
659 h->ccs -= COCO_DESTROY_CONTINUATION;
660 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == h->destroy_task);
661 h->destroy_task = GNUNET_SCHEDULER_add_now (&destroy_continuation, h);
667 * We've succeeded in establishing a connection.
669 * @param h the connection we tried to establish
672 connect_success_continuation (struct GNUNET_CONNECTION_Handle *h)
674 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection to `%s' succeeded! (%p)\n",
675 GNUNET_a2s (h->addr, h->addrlen), h);
676 /* trigger jobs that waited for the connection */
677 if (0 != (h->ccs & COCO_RECEIVE_AGAIN))
679 LOG (GNUNET_ERROR_TYPE_DEBUG,
680 "connect_success_continuation runs receive_again (%p)\n", h);
681 h->ccs -= COCO_RECEIVE_AGAIN;
682 h->read_task = GNUNET_SCHEDULER_add_now (&receive_again, h);
684 if (0 != (h->ccs & COCO_TRANSMIT_READY))
686 LOG (GNUNET_ERROR_TYPE_DEBUG,
687 "connect_success_continuation runs transmit_ready, cancels timeout_task (%p)\n",
689 GNUNET_assert (h->nth.timeout_task != GNUNET_SCHEDULER_NO_TASK);
690 GNUNET_SCHEDULER_cancel (h->nth.timeout_task);
691 h->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK;
692 h->ccs -= COCO_TRANSMIT_READY;
693 GNUNET_assert (h->write_task == GNUNET_SCHEDULER_NO_TASK);
694 GNUNET_assert (h->nth.notify_ready != NULL);
696 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_absolute_get_remaining
697 (h->nth.transmit_timeout), h->sock,
700 if (0 != (h->ccs & COCO_DESTROY_CONTINUATION))
702 LOG (GNUNET_ERROR_TYPE_DEBUG,
703 "connect_success_continuation runs destroy_continuation (%p)\n", h);
704 h->ccs -= COCO_DESTROY_CONTINUATION;
705 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == h->destroy_task);
706 h->destroy_task = GNUNET_SCHEDULER_add_now (&destroy_continuation, h);
712 * Scheduler let us know that we're either ready to write on the
713 * socket OR connect timed out. Do the right thing.
715 * @param cls the "struct AddressProbe*" with the address that we are probing
716 * @param tc success or failure info about the connect attempt.
719 connect_probe_continuation (void *cls,
720 const struct GNUNET_SCHEDULER_TaskContext *tc)
722 struct AddressProbe *ap = cls;
723 struct GNUNET_CONNECTION_Handle *h = ap->h;
724 struct AddressProbe *pos;
728 GNUNET_assert (ap->sock != NULL);
729 GNUNET_CONTAINER_DLL_remove (h->ap_head, h->ap_tail, ap);
730 len = sizeof (error);
733 if ((0 == (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) ||
735 GNUNET_NETWORK_socket_getsockopt (ap->sock, SOL_SOCKET, SO_ERROR, &error,
736 &len)) || (error != 0))
738 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ap->sock));
740 if ((NULL == h->ap_head) && (h->dns_active == GNUNET_NO))
741 connect_fail_continuation (h);
744 GNUNET_assert (h->sock == NULL);
746 GNUNET_assert (h->addr == NULL);
747 h->addr = GNUNET_malloc (ap->addrlen);
748 memcpy (h->addr, ap->addr, ap->addrlen);
749 h->addrlen = ap->addrlen;
751 /* cancel all other attempts */
752 while (NULL != (pos = h->ap_head))
754 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (pos->sock));
755 GNUNET_SCHEDULER_cancel (pos->task);
756 GNUNET_CONTAINER_DLL_remove (h->ap_head, h->ap_tail, pos);
759 connect_success_continuation (h);
764 * Try to establish a socket connection given the specified address.
765 * This function is called by the resolver once we have a DNS reply.
767 * @param cls our "struct GNUNET_CONNECTION_Handle *"
768 * @param addr address to try, NULL for "last call"
769 * @param addrlen length of addr
772 try_connect_using_address (void *cls, const struct sockaddr *addr,
775 struct GNUNET_CONNECTION_Handle *h = cls;
776 struct AddressProbe *ap;
777 struct GNUNET_TIME_Relative delay;
781 h->dns_active = NULL;
782 if ((NULL == h->ap_head) && (NULL == h->sock))
783 connect_fail_continuation (h);
787 return; /* already connected */
788 GNUNET_assert (h->addr == NULL);
790 LOG (GNUNET_ERROR_TYPE_DEBUG,
791 "Trying to connect using address `%s:%u/%s:%u'\n", h->hostname, h->port,
792 GNUNET_a2s (addr, addrlen), h->port);
793 ap = GNUNET_malloc (sizeof (struct AddressProbe) + addrlen);
794 ap->addr = (const struct sockaddr *) &ap[1];
795 memcpy (&ap[1], addr, addrlen);
796 ap->addrlen = addrlen;
799 switch (ap->addr->sa_family)
802 ((struct sockaddr_in *) ap->addr)->sin_port = htons (h->port);
805 ((struct sockaddr_in6 *) ap->addr)->sin6_port = htons (h->port);
810 return; /* not supported by us */
812 ap->sock = GNUNET_NETWORK_socket_create (ap->addr->sa_family, SOCK_STREAM, 0);
813 if (ap->sock == NULL)
816 return; /* not supported by OS */
818 LOG (GNUNET_ERROR_TYPE_INFO, _("Trying to connect to `%s' (%p)\n"),
819 GNUNET_a2s (ap->addr, ap->addrlen), h);
821 GNUNET_NETWORK_socket_connect (ap->sock, ap->addr, ap->addrlen)) &&
822 (errno != EINPROGRESS))
824 /* maybe refused / unsupported address, try next */
825 LOG_STRERROR (GNUNET_ERROR_TYPE_INFO, "connect");
827 LOG (GNUNET_ERROR_TYPE_INFO, _("Failed to connect to `%s' (%p)\n"),
828 GNUNET_a2s (ap->addr, ap->addrlen), h);
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)
838 GNUNET_TIME_relative_min (delay,
839 GNUNET_TIME_absolute_get_remaining (h->
840 nth.transmit_timeout));
841 if (h->receiver != NULL)
843 GNUNET_TIME_relative_min (delay,
844 GNUNET_TIME_absolute_get_remaining
845 (h->receive_timeout));
847 GNUNET_SCHEDULER_add_write_net (delay, ap->sock,
848 &connect_probe_continuation, ap);
853 * Create a socket handle by (asynchronously) connecting to a host.
854 * This function returns immediately, even if the connection has not
855 * yet been established. This function only creates TCP connections.
857 * @param cfg configuration to use
858 * @param hostname name of the host to connect to
859 * @param port port to connect to
860 * @return the socket handle
862 struct GNUNET_CONNECTION_Handle *
863 GNUNET_CONNECTION_create_from_connect (const struct GNUNET_CONFIGURATION_Handle
864 *cfg, const char *hostname,
867 struct GNUNET_CONNECTION_Handle *ret;
869 GNUNET_assert (0 < strlen (hostname)); /* sanity check */
870 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);
877 GNUNET_RESOLVER_ip_get (ret->hostname, AF_UNSPEC,
878 GNUNET_CONNECTION_CONNECT_RETRY_TIMEOUT,
879 &try_connect_using_address, ret);
885 * Create a socket handle by connecting to a UNIX domain service.
886 * This function returns immediately, even if the connection has not
887 * yet been established. This function only creates UNIX connections.
889 * @param cfg configuration to use
890 * @param unixpath path to connect to
891 * @return the socket handle, NULL on systems without UNIX support
893 struct GNUNET_CONNECTION_Handle *
894 GNUNET_CONNECTION_create_from_connect_to_unixpath (const struct
895 GNUNET_CONFIGURATION_Handle
896 *cfg, const char *unixpath)
899 struct GNUNET_CONNECTION_Handle *ret;
900 struct sockaddr_un *un;
903 GNUNET_assert (0 < strlen (unixpath)); /* sanity check */
904 un = GNUNET_malloc (sizeof (struct sockaddr_un));
905 un->sun_family = AF_UNIX;
906 slen = strlen (unixpath);
907 if (slen >= sizeof (un->sun_path))
908 slen = sizeof (un->sun_path) - 1;
909 memcpy (un->sun_path, unixpath, slen);
910 un->sun_path[slen] = '\0';
911 slen = sizeof (struct sockaddr_un);
912 #if HAVE_SOCKADDR_IN_SIN_LEN
913 un->sun_len = (u_char) slen;
916 un->sun_path[0] = '\0';
918 ret = GNUNET_malloc (sizeof (struct GNUNET_CONNECTION_Handle));
920 ret->write_buffer_size = GNUNET_SERVER_MIN_BUFFER_SIZE;
921 ret->write_buffer = GNUNET_malloc (ret->write_buffer_size);
923 ret->hostname = NULL;
924 ret->addr = (struct sockaddr *) un;
926 ret->sock = GNUNET_NETWORK_socket_create (AF_UNIX, SOCK_STREAM, 0);
927 if (NULL == ret->sock)
929 GNUNET_free (ret->addr);
930 GNUNET_free (ret->write_buffer);
935 GNUNET_NETWORK_socket_connect (ret->sock, ret->addr, ret->addrlen))
937 /* Just return; we expect everything to work eventually so don't fail HARD */
938 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret->sock));
942 connect_success_continuation (ret);
951 * Create a socket handle by (asynchronously) connecting to a host.
952 * This function returns immediately, even if the connection has not
953 * yet been established. This function only creates TCP connections.
955 * @param af_family address family to use
956 * @param serv_addr server address
957 * @param addrlen length of server address
958 * @return the socket handle
960 struct GNUNET_CONNECTION_Handle *
961 GNUNET_CONNECTION_create_from_sockaddr (int af_family,
962 const struct sockaddr *serv_addr,
965 struct GNUNET_NETWORK_Handle *s;
966 struct GNUNET_CONNECTION_Handle *ret;
969 s = GNUNET_NETWORK_socket_create (af_family, SOCK_STREAM, 0);
972 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK, "socket");
975 if ((GNUNET_OK != GNUNET_NETWORK_socket_connect (s, serv_addr, addrlen)) &&
976 (errno != EINPROGRESS))
978 /* maybe refused / unsupported address, try next */
979 LOG_STRERROR (GNUNET_ERROR_TYPE_INFO, "connect");
980 LOG (GNUNET_ERROR_TYPE_INFO, _("Attempt to connect to `%s' failed\n"),
981 GNUNET_a2s (serv_addr, addrlen));
982 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (s));
985 ret = GNUNET_CONNECTION_create_from_existing (s);
986 ret->addr = GNUNET_malloc (addrlen);
987 memcpy (ret->addr, serv_addr, addrlen);
988 ret->addrlen = addrlen;
989 LOG (GNUNET_ERROR_TYPE_INFO, _("Trying to connect to `%s' (%p)\n"),
990 GNUNET_a2s (serv_addr, addrlen), ret);
996 * Check if socket is valid (no fatal errors have happened so far).
997 * Note that a socket that is still trying to connect is considered
1000 * @param sock socket to check
1001 * @return GNUNET_YES if valid, GNUNET_NO otherwise
1004 GNUNET_CONNECTION_check (struct GNUNET_CONNECTION_Handle *sock)
1006 if ((sock->ap_head != NULL) || (sock->dns_active != NULL))
1007 return GNUNET_YES; /* still trying to connect */
1008 return (sock->sock == NULL) ? GNUNET_NO : GNUNET_YES;
1013 * Close the socket and free associated resources. Pending
1014 * transmissions may be completed or dropped depending on the
1015 * arguments. If a receive call is pending and should
1016 * NOT be completed, 'GNUNET_CONNECTION_receive_cancel'
1017 * should be called explicitly first.
1019 * @param sock socket to destroy
1020 * @param finish_pending_write should pending writes be completed or aborted?
1021 * (this applies to transmissions where the data has already been
1022 * read from the application; all other transmissions should be
1023 * aborted using 'GNUNET_CONNECTION_notify_transmit_ready_cancel').
1026 GNUNET_CONNECTION_destroy (struct GNUNET_CONNECTION_Handle *sock,
1027 int finish_pending_write)
1029 if (GNUNET_NO == finish_pending_write)
1031 if (sock->write_task != GNUNET_SCHEDULER_NO_TASK)
1033 GNUNET_SCHEDULER_cancel (sock->write_task);
1034 sock->write_task = GNUNET_SCHEDULER_NO_TASK;
1035 sock->write_buffer_off = 0;
1037 sock->nth.notify_ready = NULL;
1039 if ((sock->write_buffer_off == 0) && (sock->dns_active != NULL))
1041 GNUNET_RESOLVER_request_cancel (sock->dns_active);
1042 sock->dns_active = NULL;
1045 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task);
1046 sock->destroy_task = GNUNET_SCHEDULER_add_now (&destroy_continuation, sock);
1051 * Tell the receiver callback that a timeout was reached.
1054 signal_timeout (struct GNUNET_CONNECTION_Handle *sh)
1056 GNUNET_CONNECTION_Receiver receiver;
1058 LOG (GNUNET_ERROR_TYPE_DEBUG, "Network signals time out to receiver (%p)!\n",
1060 GNUNET_assert (NULL != (receiver = sh->receiver));
1061 sh->receiver = NULL;
1062 receiver (sh->receiver_cls, NULL, 0, NULL, 0, 0);
1067 * Tell the receiver callback that we had an IO error.
1070 signal_error (struct GNUNET_CONNECTION_Handle *sh, int errcode)
1072 GNUNET_CONNECTION_Receiver receiver;
1074 GNUNET_assert (NULL != (receiver = sh->receiver));
1075 sh->receiver = NULL;
1076 receiver (sh->receiver_cls, NULL, 0, sh->addr, sh->addrlen, errcode);
1081 * This function is called once we either timeout
1082 * or have data ready to read.
1085 receive_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1087 struct GNUNET_CONNECTION_Handle *sh = cls;
1088 struct GNUNET_TIME_Absolute now;
1089 char buffer[sh->max];
1091 GNUNET_CONNECTION_Receiver receiver;
1093 sh->read_task = GNUNET_SCHEDULER_NO_TASK;
1094 if ((GNUNET_YES == sh->ignore_shutdown) &&
1095 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
1097 /* ignore shutdown request, go again immediately */
1098 LOG (GNUNET_ERROR_TYPE_DEBUG,
1099 "Ignoring shutdown signal per configuration\n");
1101 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_absolute_get_remaining
1102 (sh->receive_timeout), sh->sock,
1103 &receive_ready, sh);
1106 now = GNUNET_TIME_absolute_get ();
1107 if ((now.abs_value > sh->receive_timeout.abs_value) ||
1108 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT)) ||
1109 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
1111 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1112 LOG (GNUNET_ERROR_TYPE_DEBUG,
1113 "Receive from `%s' encounters error: time out by %llums... (%p)\n",
1114 GNUNET_a2s (sh->addr, sh->addrlen),
1115 GNUNET_TIME_absolute_get_duration (sh->receive_timeout).rel_value,
1117 signal_timeout (sh);
1120 if (sh->sock == NULL)
1122 /* connect failed for good */
1123 LOG (GNUNET_ERROR_TYPE_DEBUG,
1124 "Receive encounters error, socket closed... (%p)\n", sh);
1125 signal_error (sh, ECONNREFUSED);
1128 GNUNET_assert (GNUNET_NETWORK_fdset_isset (tc->read_ready, sh->sock));
1130 ret = GNUNET_NETWORK_socket_recv (sh->sock, buffer, sh->max);
1135 LOG (GNUNET_ERROR_TYPE_DEBUG, "Error receiving: %s\n", STRERROR (errno));
1136 signal_error (sh, errno);
1139 LOG (GNUNET_ERROR_TYPE_DEBUG,
1140 "receive_ready read %u/%u bytes from `%s' (%p)!\n", (unsigned int) ret,
1141 sh->max, GNUNET_a2s (sh->addr, sh->addrlen), sh);
1142 GNUNET_assert (NULL != (receiver = sh->receiver));
1143 sh->receiver = NULL;
1144 receiver (sh->receiver_cls, buffer, ret, sh->addr, sh->addrlen, 0);
1149 * This function is called after establishing a connection either has
1150 * succeeded or timed out. Note that it is possible that the attempt
1151 * timed out and that we're immediately retrying. If we are retrying,
1152 * we need to wait again (or timeout); if we succeeded, we need to
1153 * wait for data (or timeout).
1155 * @param cls our connection handle
1156 * @param tc task context describing why we are here
1159 receive_again (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1161 struct GNUNET_CONNECTION_Handle *sh = cls;
1162 struct GNUNET_TIME_Absolute now;
1164 sh->read_task = GNUNET_SCHEDULER_NO_TASK;
1165 if (sh->sock == NULL)
1167 /* not connected and no longer trying */
1168 LOG (GNUNET_ERROR_TYPE_DEBUG,
1169 "Receive encounters error, socket closed (%p)...\n", sh);
1170 signal_error (sh, ECONNREFUSED);
1173 now = GNUNET_TIME_absolute_get ();
1174 if ((now.abs_value > sh->receive_timeout.abs_value) ||
1175 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
1177 LOG (GNUNET_ERROR_TYPE_DEBUG,
1178 "Receive encounters error: time out (%p)...\n", sh);
1179 signal_timeout (sh);
1182 GNUNET_assert (sh->sock != NULL);
1183 /* connect succeeded, wait for data! */
1185 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_absolute_get_remaining
1186 (sh->receive_timeout), sh->sock,
1187 &receive_ready, sh);
1192 * Receive data from the given socket. Note that this function will
1193 * call "receiver" asynchronously using the scheduler. It will
1194 * "immediately" return. Note that there MUST only be one active
1195 * receive call per socket at any given point in time (so do not
1196 * call receive again until the receiver callback has been invoked).
1198 * @param sock socket handle
1199 * @param max maximum number of bytes to read
1200 * @param timeout maximum amount of time to wait (use -1 for "forever")
1201 * @param receiver function to call with received data
1202 * @param receiver_cls closure for receiver
1205 GNUNET_CONNECTION_receive (struct GNUNET_CONNECTION_Handle *sock, size_t max,
1206 struct GNUNET_TIME_Relative timeout,
1207 GNUNET_CONNECTION_Receiver receiver,
1210 struct GNUNET_SCHEDULER_TaskContext tc;
1212 GNUNET_assert ((sock->read_task == GNUNET_SCHEDULER_NO_TASK) &&
1213 (0 == (sock->ccs & COCO_RECEIVE_AGAIN)) &&
1214 (sock->receiver == NULL));
1215 sock->receiver = receiver;
1216 sock->receiver_cls = receiver_cls;
1217 sock->receive_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1219 if (sock->sock != NULL)
1221 memset (&tc, 0, sizeof (tc));
1222 tc.reason = GNUNET_SCHEDULER_REASON_PREREQ_DONE;
1223 receive_again (sock, &tc);
1226 if ((sock->dns_active == NULL) && (sock->ap_head == NULL))
1228 receiver (receiver_cls, NULL, 0, NULL, 0, ETIMEDOUT);
1231 sock->ccs += COCO_RECEIVE_AGAIN;
1236 * Configure this connection to ignore shutdown signals.
1238 * @param sock socket handle
1239 * @param do_ignore GNUNET_YES to ignore, GNUNET_NO to restore default
1242 GNUNET_CONNECTION_ignore_shutdown (struct GNUNET_CONNECTION_Handle *sock,
1245 sock->ignore_shutdown = do_ignore;
1250 * Cancel receive job on the given socket. Note that the
1251 * receiver callback must not have been called yet in order
1252 * for the cancellation to be valid.
1254 * @param sock socket handle
1255 * @return closure of the original receiver callback closure
1258 GNUNET_CONNECTION_receive_cancel (struct GNUNET_CONNECTION_Handle *sock)
1260 if (sock->read_task != GNUNET_SCHEDULER_NO_TASK)
1262 GNUNET_assert (sock == GNUNET_SCHEDULER_cancel (sock->read_task));
1263 sock->read_task = GNUNET_SCHEDULER_NO_TASK;
1267 GNUNET_assert (0 != (sock->ccs & COCO_RECEIVE_AGAIN));
1268 sock->ccs -= COCO_RECEIVE_AGAIN;
1270 sock->receiver = NULL;
1271 return sock->receiver_cls;
1276 * Try to call the transmit notify method (check if we do
1277 * have enough space available first)!
1279 * @param sock socket for which we should do this processing
1280 * @return GNUNET_YES if we were able to call notify
1283 process_notify (struct GNUNET_CONNECTION_Handle *sock)
1288 GNUNET_CONNECTION_TransmitReadyNotify notify;
1290 GNUNET_assert (sock->write_task == GNUNET_SCHEDULER_NO_TASK);
1291 if (NULL == (notify = sock->nth.notify_ready))
1293 used = sock->write_buffer_off - sock->write_buffer_pos;
1294 avail = sock->write_buffer_size - used;
1295 size = sock->nth.notify_size;
1298 sock->nth.notify_ready = NULL;
1299 if (sock->write_buffer_size - sock->write_buffer_off < size)
1301 /* need to compact */
1302 memmove (sock->write_buffer, &sock->write_buffer[sock->write_buffer_pos],
1304 sock->write_buffer_off -= sock->write_buffer_pos;
1305 sock->write_buffer_pos = 0;
1307 avail = sock->write_buffer_size - sock->write_buffer_off;
1308 GNUNET_assert (avail >= size);
1310 notify (sock->nth.notify_ready_cls, avail,
1311 &sock->write_buffer[sock->write_buffer_off]);
1312 GNUNET_assert (size <= avail);
1313 sock->write_buffer_off += size;
1319 * Task invoked by the scheduler when a call to transmit
1320 * is timing out (we never got enough buffer space to call
1321 * the callback function before the specified timeout
1324 * This task notifies the client about the timeout.
1326 * @param cls the 'struct GNUNET_CONNECTION_Handle'
1327 * @param tc scheduler context
1330 transmit_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1332 struct GNUNET_CONNECTION_Handle *sock = cls;
1333 GNUNET_CONNECTION_TransmitReadyNotify notify;
1335 sock->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK;
1336 LOG (GNUNET_ERROR_TYPE_DEBUG,
1337 "Transmit to `%s:%u/%s' fails, time out reached (%p).\n",
1339 sock->port, GNUNET_a2s (sock->addr, sock->addrlen), sock);
1340 GNUNET_assert (0 != (sock->ccs & COCO_TRANSMIT_READY));
1341 sock->ccs -= COCO_TRANSMIT_READY; /* remove request */
1342 notify = sock->nth.notify_ready;
1343 sock->nth.notify_ready = NULL;
1344 notify (sock->nth.notify_ready_cls, 0, NULL);
1349 * Task invoked by the scheduler when we failed to connect
1350 * at the time of being asked to transmit.
1352 * This task notifies the client about the error.
1354 * @param cls the 'struct GNUNET_CONNECTION_Handle'
1355 * @param tc scheduler context
1358 connect_error (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1360 struct GNUNET_CONNECTION_Handle *sock = cls;
1361 GNUNET_CONNECTION_TransmitReadyNotify notify;
1363 LOG (GNUNET_ERROR_TYPE_DEBUG,
1364 "Transmission request of size %u fails (%s/%u), connection failed (%p).\n",
1365 sock->nth.notify_size, sock->hostname, sock->port, sock);
1366 sock->write_task = GNUNET_SCHEDULER_NO_TASK;
1367 notify = sock->nth.notify_ready;
1368 sock->nth.notify_ready = NULL;
1369 notify (sock->nth.notify_ready_cls, 0, NULL);
1379 transmit_error (struct GNUNET_CONNECTION_Handle *sock)
1381 GNUNET_CONNECTION_TransmitReadyNotify notify;
1383 if (NULL != sock->sock)
1385 GNUNET_NETWORK_socket_shutdown (sock->sock, SHUT_RDWR);
1386 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock->sock));
1388 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->write_task);
1390 if (sock->read_task != GNUNET_SCHEDULER_NO_TASK)
1392 GNUNET_SCHEDULER_cancel (sock->read_task);
1393 sock->read_task = GNUNET_SCHEDULER_NO_TASK;
1394 signal_timeout (sock);
1397 if (sock->nth.notify_ready == NULL)
1398 return; /* nobody to tell about it */
1399 notify = sock->nth.notify_ready;
1400 sock->nth.notify_ready = NULL;
1401 notify (sock->nth.notify_ready_cls, 0, NULL);
1406 * See if we are now connected. If not, wait longer for
1407 * connect to succeed. If connected, we should be able
1408 * to write now as well, unless we timed out.
1410 * @param cls our connection handle
1411 * @param tc task context describing why we are here
1414 transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1416 struct GNUNET_CONNECTION_Handle *sock = cls;
1417 GNUNET_CONNECTION_TransmitReadyNotify notify;
1421 LOG (GNUNET_ERROR_TYPE_DEBUG, "transmit_ready running (%p).\n", sock);
1422 GNUNET_assert (sock->write_task != GNUNET_SCHEDULER_NO_TASK);
1423 sock->write_task = GNUNET_SCHEDULER_NO_TASK;
1424 GNUNET_assert (sock->nth.timeout_task == GNUNET_SCHEDULER_NO_TASK);
1425 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1427 if ((sock->ignore_shutdown == GNUNET_YES) && (NULL != sock->sock))
1428 goto SCHEDULE_WRITE; /* ignore shutdown, go again immediately */
1429 LOG (GNUNET_ERROR_TYPE_DEBUG,
1430 "Transmit to `%s' fails, shutdown happened (%p).\n",
1431 GNUNET_a2s (sock->addr, sock->addrlen), sock);
1432 notify = sock->nth.notify_ready;
1435 sock->nth.notify_ready = NULL;
1436 notify (sock->nth.notify_ready_cls, 0, NULL);
1440 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT))
1442 LOG (GNUNET_ERROR_TYPE_DEBUG,
1443 "Transmit to `%s' fails, time out reached (%p).\n",
1444 GNUNET_a2s (sock->addr, sock->addrlen), sock);
1445 notify = sock->nth.notify_ready;
1446 GNUNET_assert (NULL != notify);
1447 sock->nth.notify_ready = NULL;
1448 notify (sock->nth.notify_ready_cls, 0, NULL);
1451 GNUNET_assert (NULL != sock->sock);
1452 if (tc->write_ready == NULL)
1454 /* special circumstances (in particular,
1455 * PREREQ_DONE after connect): not yet ready to write,
1456 * but no "fatal" error either. Hence retry. */
1457 goto SCHEDULE_WRITE;
1459 if (!GNUNET_NETWORK_fdset_isset (tc->write_ready, sock->sock))
1461 LOG (GNUNET_ERROR_TYPE_INFO,
1463 ("Could not satisfy pending transmission request, socket closed or connect failed (%p).\n"),
1465 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->write_task);
1466 transmit_error (sock);
1467 return; /* connect failed for good, we're finished */
1469 GNUNET_assert (sock->write_buffer_off >= sock->write_buffer_pos);
1470 if ((sock->nth.notify_ready != NULL) &&
1471 (sock->write_buffer_size < sock->nth.notify_size))
1473 sock->write_buffer =
1474 GNUNET_realloc (sock->write_buffer, sock->nth.notify_size);
1475 sock->write_buffer_size = sock->nth.notify_size;
1477 process_notify (sock);
1478 have = sock->write_buffer_off - sock->write_buffer_pos;
1481 /* no data ready for writing, terminate write loop */
1484 GNUNET_assert (have <= sock->write_buffer_size);
1485 GNUNET_assert (have + sock->write_buffer_pos <= sock->write_buffer_size);
1486 GNUNET_assert (sock->write_buffer_pos <= sock->write_buffer_size);
1489 GNUNET_NETWORK_socket_send (sock->sock,
1490 &sock->write_buffer[sock->write_buffer_pos],
1496 LOG_STRERROR (GNUNET_ERROR_TYPE_DEBUG, "send");
1497 if (GNUNET_SCHEDULER_NO_TASK != sock->write_task)
1499 GNUNET_SCHEDULER_cancel (sock->write_task);
1500 sock->write_task = GNUNET_SCHEDULER_NO_TASK;
1502 transmit_error (sock);
1505 LOG (GNUNET_ERROR_TYPE_DEBUG,
1506 "transmit_ready transmitted %u/%u bytes to `%s' (%p)\n",
1507 (unsigned int) ret, have, GNUNET_a2s (sock->addr, sock->addrlen), sock);
1508 sock->write_buffer_pos += ret;
1509 if (sock->write_buffer_pos == sock->write_buffer_off)
1511 /* transmitted all pending data */
1512 sock->write_buffer_pos = 0;
1513 sock->write_buffer_off = 0;
1515 if ((sock->write_buffer_off == 0) && (NULL == sock->nth.notify_ready))
1516 return; /* all data sent! */
1517 /* not done writing, schedule more */
1519 LOG (GNUNET_ERROR_TYPE_DEBUG,
1520 "Re-scheduling transmit_ready (more to do) (%p).\n", sock);
1521 have = sock->write_buffer_off - sock->write_buffer_pos;
1522 GNUNET_assert ((sock->nth.notify_ready != NULL) || (have > 0));
1523 if (sock->write_task == GNUNET_SCHEDULER_NO_TASK)
1525 GNUNET_SCHEDULER_add_write_net ((sock->nth.notify_ready ==
1526 NULL) ? GNUNET_TIME_UNIT_FOREVER_REL :
1527 GNUNET_TIME_absolute_get_remaining
1528 (sock->nth.transmit_timeout),
1529 sock->sock, &transmit_ready, sock);
1534 * Ask the socket to call us once the specified number of bytes
1535 * are free in the transmission buffer. May call the notify
1536 * method immediately if enough space is available.
1538 * @param sock socket
1539 * @param size number of bytes to send
1540 * @param timeout after how long should we give up (and call
1541 * notify with buf NULL and size 0)?
1542 * @param notify function to call
1543 * @param notify_cls closure for notify
1544 * @return non-NULL if the notify callback was queued,
1545 * NULL if we are already going to notify someone else (busy)
1547 struct GNUNET_CONNECTION_TransmitHandle *
1548 GNUNET_CONNECTION_notify_transmit_ready (struct GNUNET_CONNECTION_Handle *sock,
1550 struct GNUNET_TIME_Relative timeout,
1551 GNUNET_CONNECTION_TransmitReadyNotify
1552 notify, void *notify_cls)
1554 if (sock->nth.notify_ready != NULL)
1559 GNUNET_assert (notify != NULL);
1560 GNUNET_assert (size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
1561 GNUNET_assert (sock->write_buffer_off <= sock->write_buffer_size);
1562 GNUNET_assert (sock->write_buffer_pos <= sock->write_buffer_size);
1563 GNUNET_assert (sock->write_buffer_pos <= sock->write_buffer_off);
1564 sock->nth.notify_ready = notify;
1565 sock->nth.notify_ready_cls = notify_cls;
1566 sock->nth.sh = sock;
1567 sock->nth.notify_size = size;
1568 sock->nth.transmit_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1569 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->nth.timeout_task);
1570 if ((sock->sock == NULL) && (sock->ap_head == NULL) &&
1571 (sock->dns_active == NULL))
1573 if (sock->write_task != GNUNET_SCHEDULER_NO_TASK)
1574 GNUNET_SCHEDULER_cancel (sock->write_task);
1575 sock->write_task = GNUNET_SCHEDULER_add_now (&connect_error, sock);
1578 if (GNUNET_SCHEDULER_NO_TASK != sock->write_task)
1580 if (sock->sock != NULL)
1582 LOG (GNUNET_ERROR_TYPE_DEBUG, "Scheduling transmit_ready (%p).\n", sock);
1584 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_absolute_get_remaining
1585 (sock->nth.transmit_timeout),
1586 sock->sock, &transmit_ready, sock);
1590 LOG (GNUNET_ERROR_TYPE_DEBUG,
1591 "CCS-Scheduling transmit_ready, adding timeout task (%p).\n", sock);
1592 sock->ccs |= COCO_TRANSMIT_READY;
1593 sock->nth.timeout_task =
1594 GNUNET_SCHEDULER_add_delayed (timeout, &transmit_timeout, sock);
1601 * Cancel the specified transmission-ready notification.
1603 * @param th notification to cancel
1606 GNUNET_CONNECTION_notify_transmit_ready_cancel (struct
1607 GNUNET_CONNECTION_TransmitHandle
1610 GNUNET_assert (th->notify_ready != NULL);
1611 if (0 != (th->sh->ccs & COCO_TRANSMIT_READY))
1613 LOG (GNUNET_ERROR_TYPE_DEBUG,
1614 "notify_transmit_ready_cancel cancels timeout_task (%p)\n", th);
1615 GNUNET_SCHEDULER_cancel (th->timeout_task);
1616 th->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1617 th->sh->ccs -= COCO_TRANSMIT_READY;
1621 if (th->sh->write_task != GNUNET_SCHEDULER_NO_TASK)
1623 GNUNET_SCHEDULER_cancel (th->sh->write_task);
1624 th->sh->write_task = GNUNET_SCHEDULER_NO_TASK;
1627 th->notify_ready = NULL;
1630 /* end of connection.c */