2 This file is part of GNUnet.
3 (C) 2001, 2002, 2006, 2008, 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.
23 * @brief code for access to services
24 * @author Christian Grothoff
26 * Generic TCP code for reliable, record-oriented TCP
27 * connections between clients and service providers.
31 #include "gnunet_common.h"
32 #include "gnunet_client_lib.h"
33 #include "gnunet_protocols.h"
34 #include "gnunet_server_lib.h"
35 #include "gnunet_scheduler_lib.h"
37 #define DEBUG_CLIENT GNUNET_NO
40 * How often do we re-try tranmsitting requests before giving up?
41 * Note that if we succeeded transmitting a request but failed to read
42 * a response, we do NOT re-try.
44 #define MAX_ATTEMPTS 50
47 * Handle for a transmission request.
49 struct GNUNET_CLIENT_TransmitHandle
54 struct GNUNET_CLIENT_Connection *sock;
57 * Function to call to get the data for transmission.
59 GNUNET_CONNECTION_TransmitReadyNotify notify;
67 * Handle to the transmission with the underlying
70 struct GNUNET_CONNECTION_TransmitHandle *th;
73 * If we are re-trying and are delaying to do so,
74 * handle to the scheduled task managing the delay.
76 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
79 * Timeout for the operation overall.
81 struct GNUNET_TIME_Absolute timeout;
84 * Number of bytes requested.
89 * Are we allowed to re-try to connect without telling
90 * the user (of this API) about the connection troubles?
95 * Number of attempts left for transmitting the request. We may
96 * fail the first time (say because the service is not yet up), in
97 * which case (if auto_retry is set) we wait a bit and re-try
98 * (timeout permitting).
100 unsigned int attempts_left;
106 * Context for processing
107 * "GNUNET_CLIENT_transmit_and_get_response" requests.
109 struct TransmitGetResponseContext
114 struct GNUNET_CLIENT_Connection *sock;
117 * Message to transmit; do not free, allocated
118 * right after this struct.
120 const struct GNUNET_MessageHeader *hdr;
125 struct GNUNET_TIME_Absolute timeout;
128 * Function to call when done.
130 GNUNET_CLIENT_MessageHandler rn;
139 * Struct to refer to a GNUnet TCP connection.
140 * This is more than just a socket because if the server
141 * drops the connection, the client automatically tries
142 * to reconnect (and for that needs connection information).
144 struct GNUNET_CLIENT_Connection
148 * the socket handle, NULL if not live
150 struct GNUNET_CONNECTION_Handle *sock;
155 const struct GNUNET_CONFIGURATION_Handle *cfg;
158 * Name of the service we interact with.
163 * Context of a transmit_and_get_response operation, NULL
164 * if no such operation is pending.
166 struct TransmitGetResponseContext *tag;
169 * Handler for current receiver task.
171 GNUNET_CLIENT_MessageHandler receiver_handler;
174 * Closure for receiver_handler.
176 void *receiver_handler_cls;
179 * Handle for a pending transmission request, NULL if there is
182 struct GNUNET_CLIENT_TransmitHandle *th;
185 * Handler for service test completion (NULL unless in service_test)
187 GNUNET_SCHEDULER_Task test_cb;
190 * Deadline for calling 'test_cb'.
192 struct GNUNET_TIME_Absolute test_deadline;
195 * If we are re-trying and are delaying to do so,
196 * handle to the scheduled task managing the delay.
198 GNUNET_SCHEDULER_TaskIdentifier receive_task;
201 * Closure for test_cb (NULL unless in service_test)
206 * Buffer for received message.
211 * Timeout for receiving a response (absolute time).
213 struct GNUNET_TIME_Absolute receive_timeout;
216 * Current value for our incremental back-off (for
219 struct GNUNET_TIME_Relative back_off;
222 * Number of bytes in received_buf that are valid.
227 * Size of received_buf.
229 unsigned int received_size;
232 * Do we have a complete response in received_buf?
237 * Are we currently busy doing receive-processing?
238 * GNUNET_YES if so, GNUNET_NO if not.
243 * Are we ignoring shutdown signals?
248 * How often have we tried to connect?
250 unsigned int attempts;
256 * Try to connect to the service.
258 * @param service_name name of service to connect to
259 * @param cfg configuration to use
260 * @param attempt counter used to alternate between IP and UNIX domain sockets
261 * @return NULL on error
263 static struct GNUNET_CONNECTION_Handle *
264 do_connect (const char *service_name,
265 const struct GNUNET_CONFIGURATION_Handle *cfg,
266 unsigned int attempt)
268 struct GNUNET_CONNECTION_Handle *sock;
271 unsigned long long port;
275 if (0 == (attempt % 2))
277 /* on even rounds, try UNIX */
280 GNUNET_CONFIGURATION_get_value_string (cfg,
282 "UNIXPATH", &unixpath)) &&
283 (0 < strlen (unixpath)) ) /* We have a non-NULL unixpath, does that mean it's valid? */
285 sock = GNUNET_CONNECTION_create_from_connect_to_unixpath (cfg, unixpath);
289 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Connected to unixpath `%s'!\n", unixpath);
291 GNUNET_free(unixpath);
295 GNUNET_free_non_null (unixpath);
300 GNUNET_CONFIGURATION_get_value_number (cfg,
306 GNUNET_CONFIGURATION_get_value_string (cfg,
308 "HOSTNAME", &hostname)))
310 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
311 _("Could not determine valid hostname and port for service `%s' from configuration.\n"),
315 if (0 == strlen (hostname))
317 GNUNET_free (hostname);
318 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
319 _("Need a non-empty hostname for service `%s'.\n"),
326 if (0 != (attempt % 2))
331 GNUNET_CONFIGURATION_get_value_string (cfg,
333 "UNIXPATH", &unixpath)) &&
334 (0 < strlen (unixpath)))
336 sock = GNUNET_CONNECTION_create_from_connect_to_unixpath (cfg,
340 GNUNET_free (unixpath);
341 GNUNET_free (hostname);
345 GNUNET_free_non_null (unixpath);
349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
350 "Port is 0 for service `%s', UNIXPATH did not work, returning NULL!\n",
353 GNUNET_free (hostname);
357 sock = GNUNET_CONNECTION_create_from_connect (cfg,
360 GNUNET_free (hostname);
366 * Get a connection with a service.
368 * @param service_name name of the service
369 * @param cfg configuration to use
370 * @return NULL on error (service unknown to configuration)
372 struct GNUNET_CLIENT_Connection *
373 GNUNET_CLIENT_connect (const char *service_name,
374 const struct GNUNET_CONFIGURATION_Handle *cfg)
376 struct GNUNET_CLIENT_Connection *ret;
377 struct GNUNET_CONNECTION_Handle *sock;
379 sock = do_connect (service_name,
381 ret = GNUNET_malloc (sizeof (struct GNUNET_CLIENT_Connection));
384 ret->service_name = GNUNET_strdup (service_name);
386 ret->back_off = GNUNET_TIME_UNIT_MILLISECONDS;
392 * Configure this connection to ignore shutdown signals.
394 * @param h client handle
395 * @param do_ignore GNUNET_YES to ignore, GNUNET_NO to restore default
398 GNUNET_CLIENT_ignore_shutdown (struct GNUNET_CLIENT_Connection *h,
401 h->ignore_shutdown = do_ignore;
403 GNUNET_CONNECTION_ignore_shutdown (h->sock,
409 * Destroy connection with the service. This will automatically
410 * cancel any pending "receive" request (however, the handler will
411 * *NOT* be called, not even with a NULL message). Any pending
412 * transmission request will also be cancelled UNLESS the callback for
413 * the transmission request has already been called, in which case the
414 * transmission 'finish_pending_write' argument determines whether or
415 * not the write is guaranteed to complete before the socket is fully
416 * destroyed (unless, of course, there is an error with the server in
417 * which case the message may still be lost).
419 * @param finish_pending_write should a transmission already passed to the
420 * handle be completed?
421 * @param sock handle to the service connection
424 GNUNET_CLIENT_disconnect (struct GNUNET_CLIENT_Connection *sock,
425 int finish_pending_write)
427 if (sock->in_receive == GNUNET_YES)
429 GNUNET_CONNECTION_receive_cancel (sock->sock);
430 sock->in_receive = GNUNET_NO;
432 if (sock->th != NULL)
434 GNUNET_CLIENT_notify_transmit_ready_cancel (sock->th);
437 if (NULL != sock->sock)
439 GNUNET_CONNECTION_destroy (sock->sock, finish_pending_write);
442 if (sock->receive_task != GNUNET_SCHEDULER_NO_TASK)
444 GNUNET_SCHEDULER_cancel (sock->receive_task);
445 sock->receive_task = GNUNET_SCHEDULER_NO_TASK;
447 if (sock->tag != NULL)
449 GNUNET_free (sock->tag);
452 sock->receiver_handler = NULL;
453 GNUNET_array_grow (sock->received_buf, sock->received_size, 0);
454 GNUNET_free (sock->service_name);
460 * Check if message is complete
463 check_complete (struct GNUNET_CLIENT_Connection *conn)
465 if ((conn->received_pos >= sizeof (struct GNUNET_MessageHeader)) &&
466 (conn->received_pos >=
467 ntohs (((const struct GNUNET_MessageHeader *) conn->received_buf)->
469 conn->msg_complete = GNUNET_YES;
474 * Callback function for data received from the network. Note that
475 * both "available" and "errCode" would be 0 if the read simply timed out.
478 * @param buf pointer to received data
479 * @param available number of bytes availabe in "buf",
480 * possibly 0 (on errors)
481 * @param addr address of the sender
482 * @param addrlen size of addr
483 * @param errCode value of errno (on errors receiving)
486 receive_helper (void *cls,
489 const struct sockaddr *addr, socklen_t addrlen, int errCode)
491 struct GNUNET_CLIENT_Connection *conn = cls;
492 struct GNUNET_TIME_Relative remaining;
493 GNUNET_CLIENT_MessageHandler receive_handler;
494 void *receive_handler_cls;
496 GNUNET_assert (conn->msg_complete == GNUNET_NO);
497 conn->in_receive = GNUNET_NO;
498 if ((available == 0) || (conn->sock == NULL) || (errCode != 0))
500 /* signal timeout! */
502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
503 "Timeout in receive_helper, available %u, conn->sock %s, errCode `%s'\n",
504 (unsigned int) available,
505 conn->sock == NULL ? "NULL" : "non-NULL",
508 if (NULL != (receive_handler = conn->receiver_handler))
510 receive_handler_cls = conn->receiver_handler_cls;
511 conn->receiver_handler = NULL;
512 receive_handler (receive_handler_cls, NULL);
517 /* FIXME: optimize for common fast case where buf contains the
518 entire message and we need no copying... */
521 /* slow path: append to array */
522 if (conn->received_size < conn->received_pos + available)
523 GNUNET_array_grow (conn->received_buf,
524 conn->received_size, conn->received_pos + available);
525 memcpy (&conn->received_buf[conn->received_pos], buf, available);
526 conn->received_pos += available;
527 check_complete (conn);
528 /* check for timeout */
529 remaining = GNUNET_TIME_absolute_get_remaining (conn->receive_timeout);
530 if (remaining.rel_value == 0)
532 /* signal timeout! */
533 if (NULL != conn->receiver_handler)
534 conn->receiver_handler (conn->receiver_handler_cls, NULL);
537 /* back to receive -- either for more data or to call callback! */
538 GNUNET_CLIENT_receive (conn,
539 conn->receiver_handler,
540 conn->receiver_handler_cls, remaining);
545 * Continuation to call the receive callback.
547 * @param cls our handle to the client connection
548 * @param tc scheduler context
551 receive_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
553 struct GNUNET_CLIENT_Connection *sock = cls;
554 GNUNET_CLIENT_MessageHandler handler = sock->receiver_handler;
555 const struct GNUNET_MessageHeader *cmsg =
556 (const struct GNUNET_MessageHeader *) sock->received_buf;
557 void *handler_cls = sock->receiver_handler_cls;
558 uint16_t msize = ntohs (cmsg->size);
560 struct GNUNET_MessageHeader *msg = (struct GNUNET_MessageHeader *) mbuf;
563 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
564 "Received message of type %u and size %u\n",
568 sock->receive_task = GNUNET_SCHEDULER_NO_TASK;
569 GNUNET_assert (GNUNET_YES == sock->msg_complete);
570 GNUNET_assert (sock->received_pos >= msize);
571 memcpy (msg, cmsg, msize);
572 memmove (sock->received_buf,
573 &sock->received_buf[msize], sock->received_pos - msize);
574 sock->received_pos -= msize;
575 sock->msg_complete = GNUNET_NO;
576 sock->receiver_handler = NULL;
577 check_complete (sock);
579 handler (handler_cls, msg);
584 * Read from the service.
586 * @param sock the service
587 * @param handler function to call with the message
588 * @param handler_cls closure for handler
589 * @param timeout how long to wait until timing out
592 GNUNET_CLIENT_receive (struct GNUNET_CLIENT_Connection *sock,
593 GNUNET_CLIENT_MessageHandler handler,
594 void *handler_cls, struct GNUNET_TIME_Relative timeout)
596 if (sock->sock == NULL)
598 /* already disconnected, fail instantly! */
599 GNUNET_break (0); /* this should not happen in well-written code! */
601 handler (handler_cls, NULL);
604 sock->receiver_handler = handler;
605 sock->receiver_handler_cls = handler_cls;
606 sock->receive_timeout = GNUNET_TIME_relative_to_absolute (timeout);
607 if (GNUNET_YES == sock->msg_complete)
609 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->receive_task);
610 sock->receive_task = GNUNET_SCHEDULER_add_now (&receive_task, sock);
614 GNUNET_assert (sock->in_receive == GNUNET_NO);
615 sock->in_receive = GNUNET_YES;
617 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "calling GNUNET_CONNECTION_receive\n");
619 GNUNET_CONNECTION_receive (sock->sock,
620 GNUNET_SERVER_MAX_MESSAGE_SIZE - 1,
621 timeout, &receive_helper, sock);
627 * Report service unavailable.
630 service_test_error (GNUNET_SCHEDULER_Task task, void *task_cls)
632 GNUNET_SCHEDULER_add_continuation (task,
634 GNUNET_SCHEDULER_REASON_TIMEOUT);
639 * Receive confirmation from test, service is up.
642 * @param msg message received, NULL on timeout or fatal error
645 confirm_handler (void *cls, const struct GNUNET_MessageHeader *msg)
647 struct GNUNET_CLIENT_Connection *conn = cls;
648 /* We may want to consider looking at the reply in more
649 detail in the future, for example, is this the
650 correct service? FIXME! */
654 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
655 "Received confirmation that service is running.\n");
657 GNUNET_SCHEDULER_add_continuation (conn->test_cb,
659 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
663 service_test_error (conn->test_cb, conn->test_cb_cls);
665 GNUNET_CLIENT_disconnect (conn, GNUNET_NO);
670 * Send the 'TEST' message to the service. If successful, prepare to
673 * @param cls the 'struct GNUNET_CLIENT_Connection' of the connection to test
674 * @param size number of bytes available in buf
675 * @param buf where to write the message
676 * @return number of bytes written to buf
679 write_test (void *cls, size_t size, void *buf)
681 struct GNUNET_CLIENT_Connection *conn = cls;
682 struct GNUNET_MessageHeader *msg;
684 if (size < sizeof (struct GNUNET_MessageHeader))
687 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
688 _("Failure to transmit TEST request.\n"));
690 service_test_error (conn->test_cb, conn->test_cb_cls);
691 GNUNET_CLIENT_disconnect (conn, GNUNET_NO);
692 return 0; /* client disconnected */
695 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
696 "Transmitting `%s' request.\n", "TEST");
698 msg = (struct GNUNET_MessageHeader *) buf;
699 msg->type = htons (GNUNET_MESSAGE_TYPE_TEST);
700 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
701 GNUNET_CLIENT_receive (conn,
704 GNUNET_TIME_absolute_get_remaining (conn->test_deadline));
705 return sizeof (struct GNUNET_MessageHeader);
710 * Test if the service is running. If we are given a UNIXPATH or a local address,
711 * we do this NOT by trying to connect to the service, but by trying to BIND to
712 * the same port. If the BIND fails, we know the service is running.
714 * @param service name of the service to wait for
715 * @param cfg configuration to use
716 * @param timeout how long to wait at most
717 * @param task task to run if service is running
718 * (reason will be "PREREQ_DONE" (service running)
719 * or "TIMEOUT" (service not known to be running))
720 * @param task_cls closure for task
723 GNUNET_CLIENT_service_test (const char *service,
724 const struct GNUNET_CONFIGURATION_Handle *cfg,
725 struct GNUNET_TIME_Relative timeout,
726 GNUNET_SCHEDULER_Task task, void *task_cls)
729 unsigned long long port;
730 struct GNUNET_NETWORK_Handle *sock;
731 struct GNUNET_CLIENT_Connection *conn;
734 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
735 "Testing if service `%s' is running.\n", service);
739 /* probe UNIX support */
740 struct sockaddr_un s_un;
746 GNUNET_CONFIGURATION_get_value_string (cfg,
748 "UNIXPATH", &unixpath)) &&
749 (0 < strlen (unixpath)) ) /* We have a non-NULL unixpath, does that mean it's valid? */
751 if (strlen(unixpath) >= sizeof(s_un.sun_path))
753 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
754 _("UNIXPATH `%s' too long, maximum length is %llu\n"),
756 sizeof(s_un.sun_path));
760 sock = GNUNET_NETWORK_socket_create (PF_UNIX, SOCK_STREAM, 0);
763 memset (&s_un, 0, sizeof (s_un));
764 s_un.sun_family = AF_UNIX;
765 slen = strlen (unixpath) + 1;
766 if (slen >= sizeof (s_un.sun_path))
767 slen = sizeof (s_un.sun_path) - 1;
768 memcpy (s_un.sun_path,
771 s_un.sun_path[slen] = '\0';
772 slen = sizeof (struct sockaddr_un);
774 s_un.sun_path[0] = '\0';
776 #if HAVE_SOCKADDR_IN_SIN_LEN
777 s_un.sun_len = (u_char) slen;
780 GNUNET_NETWORK_socket_bind (sock,
781 (const struct sockaddr*) &s_un,
784 /* failed to bind => service must be running */
785 GNUNET_free (unixpath);
786 (void) GNUNET_NETWORK_socket_close (sock);
787 GNUNET_SCHEDULER_add_continuation (task,
789 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
792 (void) GNUNET_NETWORK_socket_close (sock);
797 GNUNET_free_non_null (unixpath);
803 GNUNET_CONFIGURATION_get_value_number (cfg,
809 GNUNET_CONFIGURATION_get_value_string (cfg,
811 "HOSTNAME", &hostname)))
813 /* UNIXPATH failed (if possible) AND IP failed => error */
814 service_test_error (task, task_cls);
818 if (0 == strcmp ("localhost", hostname))
820 /* can test using 'bind' */
821 struct sockaddr_in s_in;
823 memset (&s_in, 0, sizeof (s_in));
824 #if HAVE_SOCKADDR_IN_SIN_LEN
825 s_in.sin_len = saddrlens[1];
827 s_in.sin_family = AF_INET;
828 s_in.sin_port = htons (port);
830 sock = GNUNET_NETWORK_socket_create (AF_INET, SOCK_STREAM, 0);
834 GNUNET_NETWORK_socket_bind (sock,
835 (const struct sockaddr*) &s_in,
838 /* failed to bind => service must be running */
839 GNUNET_free (hostname);
840 (void) GNUNET_NETWORK_socket_close (sock);
841 GNUNET_SCHEDULER_add_continuation (task,
843 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
846 (void) GNUNET_NETWORK_socket_close (sock);
850 if (0 == strcmp ("ip6-localhost", hostname))
852 /* can test using 'bind' */
853 struct sockaddr_in6 s_in6;
855 memset (&s_in6, 0, sizeof (s_in6));
856 #if HAVE_SOCKADDR_IN_SIN_LEN
857 s_in6.sin6_len = saddrlens[1];
859 s_in6.sin6_family = AF_INET6;
860 s_in6.sin6_port = htons (port);
862 sock = GNUNET_NETWORK_socket_create (AF_INET6, SOCK_STREAM, 0);
866 GNUNET_NETWORK_socket_bind (sock,
867 (const struct sockaddr*) &s_in6,
870 /* failed to bind => service must be running */
871 GNUNET_free (hostname);
872 (void) GNUNET_NETWORK_socket_close (sock);
873 GNUNET_SCHEDULER_add_continuation (task,
875 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
878 (void) GNUNET_NETWORK_socket_close (sock);
882 if ( (0 == strcmp ("localhost", hostname)) ||
883 (0 == strcmp ("ip6-localhost", hostname)) )
885 /* all binds succeeded => claim service not running right now */
886 GNUNET_free_non_null (hostname);
887 service_test_error (task, task_cls);
890 GNUNET_free_non_null (hostname);
892 /* non-localhost, try 'connect' method */
893 conn = GNUNET_CLIENT_connect (service, cfg);
896 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
898 ("Could not connect to service `%s', must not be running.\n"),
900 service_test_error (task, task_cls);
903 conn->test_cb = task;
904 conn->test_cb_cls = task_cls;
905 conn->test_deadline = GNUNET_TIME_relative_to_absolute (timeout);
907 if (NULL == GNUNET_CLIENT_notify_transmit_ready (conn,
908 sizeof (struct GNUNET_MessageHeader),
913 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
914 _("Failure to transmit request to service `%s'\n"),
916 service_test_error (task, task_cls);
917 GNUNET_CLIENT_disconnect (conn, GNUNET_NO);
924 * Connection notifies us about failure or success of
925 * a transmission request. Either pass it on to our
926 * user or, if possible, retry.
928 * @param cls our "struct GNUNET_CLIENT_TransmissionHandle"
929 * @param size number of bytes available for transmission
930 * @param buf where to write them
931 * @return number of bytes written to buf
933 static size_t client_notify (void *cls, size_t size, void *buf);
937 * This task is run if we should re-try connection to the
938 * service after a while.
940 * @param cls our "struct GNUNET_CLIENT_TransmitHandle" of the request
944 client_delayed_retry (void *cls,
945 const struct GNUNET_SCHEDULER_TaskContext *tc)
947 struct GNUNET_CLIENT_TransmitHandle *th = cls;
948 struct GNUNET_TIME_Relative delay;
950 th->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
951 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
954 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
955 "Transmission failed due to shutdown.\n");
958 th->notify (th->notify_cls, 0, NULL);
962 th->sock->sock = do_connect (th->sock->service_name,
964 th->sock->attempts++);
965 if (NULL == th->sock->sock)
967 /* could happen if we're out of sockets */
968 delay = GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining (th->timeout),
971 = GNUNET_TIME_relative_min (GNUNET_TIME_relative_multiply (th->sock->back_off, 2),
972 GNUNET_TIME_UNIT_SECONDS);
974 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
975 "Transmission failed %u times, trying again in %llums.\n",
976 MAX_ATTEMPTS - th->attempts_left,
977 (unsigned long long) delay.rel_value);
979 th->reconnect_task = GNUNET_SCHEDULER_add_delayed (delay,
980 &client_delayed_retry,
984 GNUNET_CONNECTION_ignore_shutdown (th->sock->sock,
985 th->sock->ignore_shutdown);
986 th->th = GNUNET_CONNECTION_notify_transmit_ready (th->sock->sock,
988 GNUNET_TIME_absolute_get_remaining
995 th->notify (th->notify_cls, 0, NULL);
1003 * Connection notifies us about failure or success of a transmission
1004 * request. Either pass it on to our user or, if possible, retry.
1006 * @param cls our "struct GNUNET_CLIENT_TransmissionHandle"
1007 * @param size number of bytes available for transmission
1008 * @param buf where to write them
1009 * @return number of bytes written to buf
1012 client_notify (void *cls, size_t size, void *buf)
1014 struct GNUNET_CLIENT_TransmitHandle *th = cls;
1016 struct GNUNET_TIME_Relative delay;
1019 th->sock->th = NULL;
1022 delay = GNUNET_TIME_absolute_get_remaining (th->timeout);
1023 delay.rel_value /= 2;
1024 if ( (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & GNUNET_SCHEDULER_get_reason ())) ||
1025 (GNUNET_YES != th->auto_retry) ||
1026 (0 == --th->attempts_left) ||
1027 (delay.rel_value < 1) )
1030 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1031 "Transmission failed %u times, giving up.\n",
1032 MAX_ATTEMPTS - th->attempts_left);
1034 GNUNET_break (0 == th->notify (th->notify_cls, 0, NULL));
1040 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1041 "Failed to connect to `%s', automatically trying again.\n",
1042 th->sock->service_name);
1044 GNUNET_CONNECTION_destroy (th->sock->sock, GNUNET_NO);
1045 th->sock->sock = NULL;
1046 delay = GNUNET_TIME_relative_min (delay, th->sock->back_off);
1048 = GNUNET_TIME_relative_min (GNUNET_TIME_relative_multiply (th->sock->back_off, 2),
1049 GNUNET_TIME_UNIT_SECONDS);
1051 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1052 "Transmission failed %u times, trying again in %llums.\n",
1053 MAX_ATTEMPTS - th->attempts_left,
1054 (unsigned long long) delay.rel_value);
1057 th->reconnect_task = GNUNET_SCHEDULER_add_delayed (delay,
1058 &client_delayed_retry,
1062 GNUNET_assert (size >= th->size);
1063 ret = th->notify (th->notify_cls, size, buf);
1070 * Ask the client to call us once the specified number of bytes
1071 * are free in the transmission buffer. May call the notify
1072 * method immediately if enough space is available.
1074 * @param sock connection to the service
1075 * @param size number of bytes to send
1076 * @param timeout after how long should we give up (and call
1077 * notify with buf NULL and size 0)?
1078 * @param auto_retry if the connection to the service dies, should we
1079 * automatically re-connect and retry (within the timeout period)
1080 * or should we immediately fail in this case? Pass GNUNET_YES
1081 * if the caller does not care about temporary connection errors,
1082 * for example because the protocol is stateless
1083 * @param notify function to call
1084 * @param notify_cls closure for notify
1085 * @return NULL if our buffer will never hold size bytes,
1086 * a handle if the notify callback was queued (can be used to cancel)
1088 struct GNUNET_CLIENT_TransmitHandle *
1089 GNUNET_CLIENT_notify_transmit_ready (struct GNUNET_CLIENT_Connection *sock,
1091 struct GNUNET_TIME_Relative timeout,
1093 GNUNET_CONNECTION_TransmitReadyNotify
1094 notify, void *notify_cls)
1096 struct GNUNET_CLIENT_TransmitHandle *th;
1098 if (NULL != sock->th)
1100 /* If this breaks, you most likley called this function twice without waiting
1101 * for completion or canceling the request */
1105 th = GNUNET_malloc (sizeof (struct GNUNET_CLIENT_TransmitHandle));
1108 th->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1109 th->auto_retry = auto_retry;
1110 th->notify = notify;
1111 th->notify_cls = notify_cls;
1112 th->attempts_left = MAX_ATTEMPTS;
1114 if (sock->sock == NULL)
1116 th->reconnect_task = GNUNET_SCHEDULER_add_delayed (sock->back_off,
1117 &client_delayed_retry,
1123 th->th = GNUNET_CONNECTION_notify_transmit_ready (sock->sock,
1126 &client_notify, th);
1140 * Cancel a request for notification.
1142 * @param th handle from the original request.
1145 GNUNET_CLIENT_notify_transmit_ready_cancel (struct
1146 GNUNET_CLIENT_TransmitHandle *th)
1148 if (th->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
1150 GNUNET_assert (NULL == th->th);
1151 GNUNET_SCHEDULER_cancel (th->reconnect_task);
1152 th->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1156 GNUNET_assert (NULL != th->th);
1157 GNUNET_CONNECTION_notify_transmit_ready_cancel (th->th);
1159 th->sock->th = NULL;
1165 * Function called to notify a client about the socket
1166 * begin ready to queue the message. "buf" will be
1167 * NULL and "size" zero if the socket was closed for
1168 * writing in the meantime.
1170 * @param cls closure of type "struct TransmitGetResponseContext*"
1171 * @param size number of bytes available in buf
1172 * @param buf where the callee should write the message
1173 * @return number of bytes written to buf
1176 transmit_for_response (void *cls, size_t size, void *buf)
1178 struct TransmitGetResponseContext *tc = cls;
1181 tc->sock->tag = NULL;
1182 msize = ntohs (tc->hdr->size);
1186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1187 _("Could not submit request, not expecting to receive a response.\n"));
1190 tc->rn (tc->rn_cls, NULL);
1194 GNUNET_assert (size >= msize);
1195 memcpy (buf, tc->hdr, msize);
1196 GNUNET_CLIENT_receive (tc->sock,
1199 GNUNET_TIME_absolute_get_remaining (tc->timeout));
1206 * Convenience API that combines sending a request
1207 * to the service and waiting for a response.
1208 * If either operation times out, the callback
1209 * will be called with a "NULL" response (in which
1210 * case the connection should probably be destroyed).
1212 * @param sock connection to use
1213 * @param hdr message to transmit
1214 * @param timeout when to give up (for both transmission
1215 * and for waiting for a response)
1216 * @param auto_retry if the connection to the service dies, should we
1217 * automatically re-connect and retry (within the timeout period)
1218 * or should we immediately fail in this case? Pass GNUNET_YES
1219 * if the caller does not care about temporary connection errors,
1220 * for example because the protocol is stateless
1221 * @param rn function to call with the response
1222 * @param rn_cls closure for rn
1223 * @return GNUNET_OK on success, GNUNET_SYSERR if a request
1224 * is already pending
1227 GNUNET_CLIENT_transmit_and_get_response (struct GNUNET_CLIENT_Connection
1229 const struct GNUNET_MessageHeader
1231 struct GNUNET_TIME_Relative timeout,
1233 GNUNET_CLIENT_MessageHandler rn,
1236 struct TransmitGetResponseContext *tc;
1239 if (NULL != sock->th)
1240 return GNUNET_SYSERR;
1241 GNUNET_assert (sock->tag == NULL);
1242 msize = ntohs (hdr->size);
1243 tc = GNUNET_malloc (sizeof (struct TransmitGetResponseContext) + msize);
1245 tc->hdr = (const struct GNUNET_MessageHeader *) &tc[1];
1246 memcpy (&tc[1], hdr, msize);
1247 tc->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1249 tc->rn_cls = rn_cls;
1250 if (NULL == GNUNET_CLIENT_notify_transmit_ready (sock,
1254 &transmit_for_response,
1259 return GNUNET_SYSERR;
1267 /* end of client.c */