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"
39 * How often do we re-try tranmsitting requests before giving up?
40 * Note that if we succeeded transmitting a request but failed to read
41 * a response, we do NOT re-try.
43 #define MAX_ATTEMPTS 50
45 #define LOG(kind,...) GNUNET_log_from (kind, "util",__VA_ARGS__)
48 * Handle for a transmission request.
50 struct GNUNET_CLIENT_TransmitHandle
55 struct GNUNET_CLIENT_Connection *sock;
58 * Function to call to get the data for transmission.
60 GNUNET_CONNECTION_TransmitReadyNotify notify;
68 * Handle to the transmission with the underlying
71 struct GNUNET_CONNECTION_TransmitHandle *th;
74 * If we are re-trying and are delaying to do so,
75 * handle to the scheduled task managing the delay.
77 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
80 * Timeout for the operation overall.
82 struct GNUNET_TIME_Absolute timeout;
85 * Number of bytes requested.
90 * Are we allowed to re-try to connect without telling
91 * the user (of this API) about the connection troubles?
96 * Number of attempts left for transmitting the request. We may
97 * fail the first time (say because the service is not yet up), in
98 * which case (if auto_retry is set) we wait a bit and re-try
99 * (timeout permitting).
101 unsigned int attempts_left;
107 * Context for processing
108 * "GNUNET_CLIENT_transmit_and_get_response" requests.
110 struct TransmitGetResponseContext
115 struct GNUNET_CLIENT_Connection *sock;
118 * Message to transmit; do not free, allocated
119 * right after this struct.
121 const struct GNUNET_MessageHeader *hdr;
126 struct GNUNET_TIME_Absolute timeout;
129 * Function to call when done.
131 GNUNET_CLIENT_MessageHandler rn;
140 * Struct to refer to a GNUnet TCP connection.
141 * This is more than just a socket because if the server
142 * drops the connection, the client automatically tries
143 * to reconnect (and for that needs connection information).
145 struct GNUNET_CLIENT_Connection
149 * the socket handle, NULL if not live
151 struct GNUNET_CONNECTION_Handle *sock;
156 const struct GNUNET_CONFIGURATION_Handle *cfg;
159 * Name of the service we interact with.
164 * Context of a transmit_and_get_response operation, NULL
165 * if no such operation is pending.
167 struct TransmitGetResponseContext *tag;
170 * Handler for current receiver task.
172 GNUNET_CLIENT_MessageHandler receiver_handler;
175 * Closure for receiver_handler.
177 void *receiver_handler_cls;
180 * Handle for a pending transmission request, NULL if there is
183 struct GNUNET_CLIENT_TransmitHandle *th;
186 * Handler for service test completion (NULL unless in service_test)
188 GNUNET_SCHEDULER_Task test_cb;
191 * Deadline for calling 'test_cb'.
193 struct GNUNET_TIME_Absolute test_deadline;
196 * If we are re-trying and are delaying to do so,
197 * handle to the scheduled task managing the delay.
199 GNUNET_SCHEDULER_TaskIdentifier receive_task;
202 * Closure for test_cb (NULL unless in service_test)
207 * Buffer for received message.
212 * Timeout for receiving a response (absolute time).
214 struct GNUNET_TIME_Absolute receive_timeout;
217 * Current value for our incremental back-off (for
220 struct GNUNET_TIME_Relative back_off;
223 * Number of bytes in received_buf that are valid.
228 * Size of received_buf.
230 unsigned int received_size;
233 * Do we have a complete response in received_buf?
238 * Are we currently busy doing receive-processing?
239 * GNUNET_YES if so, GNUNET_NO if not.
244 * How often have we tried to connect?
246 unsigned int attempts;
252 * Try to connect to the service.
254 * @param service_name name of service to connect to
255 * @param cfg configuration to use
256 * @param attempt counter used to alternate between IP and UNIX domain sockets
257 * @return NULL on error
259 static struct GNUNET_CONNECTION_Handle *
260 do_connect (const char *service_name,
261 const struct GNUNET_CONFIGURATION_Handle *cfg, unsigned int attempt)
263 struct GNUNET_CONNECTION_Handle *sock;
266 unsigned long long port;
270 if (0 == (attempt % 2))
272 /* on even rounds, try UNIX */
274 if ((GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg, service_name, "UNIXPATH", &unixpath)) && (0 < strlen (unixpath))) /* We have a non-NULL unixpath, does that mean it's valid? */
276 sock = GNUNET_CONNECTION_create_from_connect_to_unixpath (cfg, unixpath);
279 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connected to unixpath `%s'!\n",
281 GNUNET_free (unixpath);
285 GNUNET_free_non_null (unixpath);
290 GNUNET_CONFIGURATION_have_value (cfg, service_name, "PORT"))
293 GNUNET_CONFIGURATION_get_value_number (cfg, service_name, "PORT", &port))
296 GNUNET_CONFIGURATION_get_value_string (cfg, service_name, "HOSTNAME",
299 LOG (GNUNET_ERROR_TYPE_WARNING,
301 ("Could not determine valid hostname and port for service `%s' from configuration.\n"),
305 if (0 == strlen (hostname))
307 GNUNET_free (hostname);
308 LOG (GNUNET_ERROR_TYPE_WARNING,
309 _("Need a non-empty hostname for service `%s'.\n"), service_name);
315 /* unspecified means 0 (disabled) */
322 if (0 != (attempt % 2))
327 GNUNET_CONFIGURATION_get_value_string (cfg, service_name, "UNIXPATH",
329 (0 < strlen (unixpath)))
332 GNUNET_CONNECTION_create_from_connect_to_unixpath (cfg, unixpath);
335 GNUNET_free (unixpath);
336 GNUNET_free_non_null (hostname);
340 GNUNET_free_non_null (unixpath);
343 LOG (GNUNET_ERROR_TYPE_DEBUG,
344 "Port is 0 for service `%s', UNIXPATH did not work, returning NULL!\n",
346 GNUNET_free_non_null (hostname);
350 sock = GNUNET_CONNECTION_create_from_connect (cfg, hostname, port);
351 GNUNET_free (hostname);
357 * Get a connection with a service.
359 * @param service_name name of the service
360 * @param cfg configuration to use
361 * @return NULL on error (service unknown to configuration)
363 struct GNUNET_CLIENT_Connection *
364 GNUNET_CLIENT_connect (const char *service_name,
365 const struct GNUNET_CONFIGURATION_Handle *cfg)
367 struct GNUNET_CLIENT_Connection *ret;
368 struct GNUNET_CONNECTION_Handle *sock;
370 sock = do_connect (service_name, cfg, 0);
371 ret = GNUNET_malloc (sizeof (struct GNUNET_CLIENT_Connection));
374 ret->service_name = GNUNET_strdup (service_name);
376 ret->back_off = GNUNET_TIME_UNIT_MILLISECONDS;
382 * Destroy connection with the service. This will automatically
383 * cancel any pending "receive" request (however, the handler will
384 * *NOT* be called, not even with a NULL message). Any pending
385 * transmission request will also be cancelled UNLESS the callback for
386 * the transmission request has already been called, in which case the
387 * transmission 'finish_pending_write' argument determines whether or
388 * not the write is guaranteed to complete before the socket is fully
389 * destroyed (unless, of course, there is an error with the server in
390 * which case the message may still be lost).
392 * @param sock handle to the service connection
395 GNUNET_CLIENT_disconnect (struct GNUNET_CLIENT_Connection *sock)
397 if (sock->in_receive == GNUNET_YES)
399 GNUNET_CONNECTION_receive_cancel (sock->sock);
400 sock->in_receive = GNUNET_NO;
402 if (sock->th != NULL)
404 GNUNET_CLIENT_notify_transmit_ready_cancel (sock->th);
407 if (NULL != sock->sock)
409 GNUNET_CONNECTION_destroy (sock->sock);
412 if (sock->receive_task != GNUNET_SCHEDULER_NO_TASK)
414 GNUNET_SCHEDULER_cancel (sock->receive_task);
415 sock->receive_task = GNUNET_SCHEDULER_NO_TASK;
417 if (sock->tag != NULL)
419 GNUNET_free (sock->tag);
422 sock->receiver_handler = NULL;
423 GNUNET_array_grow (sock->received_buf, sock->received_size, 0);
424 GNUNET_free (sock->service_name);
430 * Check if message is complete
433 check_complete (struct GNUNET_CLIENT_Connection *conn)
435 if ((conn->received_pos >= sizeof (struct GNUNET_MessageHeader)) &&
436 (conn->received_pos >=
437 ntohs (((const struct GNUNET_MessageHeader *) conn->received_buf)->
439 conn->msg_complete = GNUNET_YES;
444 * Callback function for data received from the network. Note that
445 * both "available" and "errCode" would be 0 if the read simply timed out.
448 * @param buf pointer to received data
449 * @param available number of bytes availabe in "buf",
450 * possibly 0 (on errors)
451 * @param addr address of the sender
452 * @param addrlen size of addr
453 * @param errCode value of errno (on errors receiving)
456 receive_helper (void *cls, const void *buf, size_t available,
457 const struct sockaddr *addr, socklen_t addrlen, int errCode)
459 struct GNUNET_CLIENT_Connection *conn = cls;
460 struct GNUNET_TIME_Relative remaining;
461 GNUNET_CLIENT_MessageHandler receive_handler;
462 void *receive_handler_cls;
464 GNUNET_assert (conn->msg_complete == GNUNET_NO);
465 conn->in_receive = GNUNET_NO;
466 if ((available == 0) || (conn->sock == NULL) || (errCode != 0))
468 /* signal timeout! */
469 LOG (GNUNET_ERROR_TYPE_DEBUG,
470 "Timeout in receive_helper, available %u, conn->sock %s, errCode `%s'\n",
471 (unsigned int) available, conn->sock == NULL ? "NULL" : "non-NULL",
473 if (NULL != (receive_handler = conn->receiver_handler))
475 receive_handler_cls = conn->receiver_handler_cls;
476 conn->receiver_handler = NULL;
477 receive_handler (receive_handler_cls, NULL);
482 /* FIXME: optimize for common fast case where buf contains the
483 * entire message and we need no copying... */
486 /* slow path: append to array */
487 if (conn->received_size < conn->received_pos + available)
488 GNUNET_array_grow (conn->received_buf, conn->received_size,
489 conn->received_pos + available);
490 memcpy (&conn->received_buf[conn->received_pos], buf, available);
491 conn->received_pos += available;
492 check_complete (conn);
493 /* check for timeout */
494 remaining = GNUNET_TIME_absolute_get_remaining (conn->receive_timeout);
495 if (remaining.rel_value == 0)
497 /* signal timeout! */
498 if (NULL != conn->receiver_handler)
499 conn->receiver_handler (conn->receiver_handler_cls, NULL);
502 /* back to receive -- either for more data or to call callback! */
503 GNUNET_CLIENT_receive (conn, conn->receiver_handler,
504 conn->receiver_handler_cls, remaining);
509 * Continuation to call the receive callback.
511 * @param cls our handle to the client connection
512 * @param tc scheduler context
515 receive_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
517 struct GNUNET_CLIENT_Connection *sock = cls;
518 GNUNET_CLIENT_MessageHandler handler = sock->receiver_handler;
519 const struct GNUNET_MessageHeader *cmsg =
520 (const struct GNUNET_MessageHeader *) sock->received_buf;
521 void *handler_cls = sock->receiver_handler_cls;
522 uint16_t msize = ntohs (cmsg->size);
524 struct GNUNET_MessageHeader *msg = (struct GNUNET_MessageHeader *) mbuf;
526 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received message of type %u and size %u\n",
527 ntohs (cmsg->type), msize);
528 sock->receive_task = GNUNET_SCHEDULER_NO_TASK;
529 GNUNET_assert (GNUNET_YES == sock->msg_complete);
530 GNUNET_assert (sock->received_pos >= msize);
531 memcpy (msg, cmsg, msize);
532 memmove (sock->received_buf, &sock->received_buf[msize],
533 sock->received_pos - msize);
534 sock->received_pos -= msize;
535 sock->msg_complete = GNUNET_NO;
536 sock->receiver_handler = NULL;
537 check_complete (sock);
539 handler (handler_cls, msg);
544 * Read from the service.
546 * @param sock the service
547 * @param handler function to call with the message
548 * @param handler_cls closure for handler
549 * @param timeout how long to wait until timing out
552 GNUNET_CLIENT_receive (struct GNUNET_CLIENT_Connection *sock,
553 GNUNET_CLIENT_MessageHandler handler, void *handler_cls,
554 struct GNUNET_TIME_Relative timeout)
556 if (sock->sock == NULL)
558 /* already disconnected, fail instantly! */
559 GNUNET_break (0); /* this should not happen in well-written code! */
561 handler (handler_cls, NULL);
564 sock->receiver_handler = handler;
565 sock->receiver_handler_cls = handler_cls;
566 sock->receive_timeout = GNUNET_TIME_relative_to_absolute (timeout);
567 if (GNUNET_YES == sock->msg_complete)
569 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->receive_task);
570 sock->receive_task = GNUNET_SCHEDULER_add_now (&receive_task, sock);
574 GNUNET_assert (sock->in_receive == GNUNET_NO);
575 sock->in_receive = GNUNET_YES;
576 LOG (GNUNET_ERROR_TYPE_DEBUG, "calling GNUNET_CONNECTION_receive\n");
577 GNUNET_CONNECTION_receive (sock->sock, GNUNET_SERVER_MAX_MESSAGE_SIZE - 1,
578 timeout, &receive_helper, sock);
584 * Report service unavailable.
587 service_test_error (GNUNET_SCHEDULER_Task task, void *task_cls)
589 GNUNET_SCHEDULER_add_continuation (task, task_cls,
590 GNUNET_SCHEDULER_REASON_TIMEOUT);
595 * Receive confirmation from test, service is up.
598 * @param msg message received, NULL on timeout or fatal error
601 confirm_handler (void *cls, const struct GNUNET_MessageHeader *msg)
603 struct GNUNET_CLIENT_Connection *conn = cls;
605 /* We may want to consider looking at the reply in more
606 * detail in the future, for example, is this the
607 * correct service? FIXME! */
610 LOG (GNUNET_ERROR_TYPE_DEBUG,
611 "Received confirmation that service is running.\n");
612 GNUNET_SCHEDULER_add_continuation (conn->test_cb, conn->test_cb_cls,
613 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
617 service_test_error (conn->test_cb, conn->test_cb_cls);
619 GNUNET_CLIENT_disconnect (conn);
624 * Send the 'TEST' message to the service. If successful, prepare to
627 * @param cls the 'struct GNUNET_CLIENT_Connection' of the connection to test
628 * @param size number of bytes available in buf
629 * @param buf where to write the message
630 * @return number of bytes written to buf
633 write_test (void *cls, size_t size, void *buf)
635 struct GNUNET_CLIENT_Connection *conn = cls;
636 struct GNUNET_MessageHeader *msg;
638 if (size < sizeof (struct GNUNET_MessageHeader))
640 LOG (GNUNET_ERROR_TYPE_DEBUG, _("Failure to transmit TEST request.\n"));
641 service_test_error (conn->test_cb, conn->test_cb_cls);
642 GNUNET_CLIENT_disconnect (conn);
643 return 0; /* client disconnected */
645 LOG (GNUNET_ERROR_TYPE_DEBUG, "Transmitting `%s' request.\n", "TEST");
646 msg = (struct GNUNET_MessageHeader *) buf;
647 msg->type = htons (GNUNET_MESSAGE_TYPE_TEST);
648 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
649 GNUNET_CLIENT_receive (conn, &confirm_handler, conn,
650 GNUNET_TIME_absolute_get_remaining
651 (conn->test_deadline));
652 return sizeof (struct GNUNET_MessageHeader);
657 * Test if the service is running. If we are given a UNIXPATH or a local address,
658 * we do this NOT by trying to connect to the service, but by trying to BIND to
659 * the same port. If the BIND fails, we know the service is running.
661 * @param service name of the service to wait for
662 * @param cfg configuration to use
663 * @param timeout how long to wait at most
664 * @param task task to run if service is running
665 * (reason will be "PREREQ_DONE" (service running)
666 * or "TIMEOUT" (service not known to be running))
667 * @param task_cls closure for task
670 GNUNET_CLIENT_service_test (const char *service,
671 const struct GNUNET_CONFIGURATION_Handle *cfg,
672 struct GNUNET_TIME_Relative timeout,
673 GNUNET_SCHEDULER_Task task, void *task_cls)
676 unsigned long long port;
677 struct GNUNET_NETWORK_Handle *sock;
678 struct GNUNET_CLIENT_Connection *conn;
680 LOG (GNUNET_ERROR_TYPE_DEBUG, "Testing if service `%s' is running.\n",
684 /* probe UNIX support */
685 struct sockaddr_un s_un;
690 if ((GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg, service, "UNIXPATH", &unixpath)) && (0 < strlen (unixpath))) /* We have a non-NULL unixpath, does that mean it's valid? */
692 if (strlen (unixpath) >= sizeof (s_un.sun_path))
694 LOG (GNUNET_ERROR_TYPE_WARNING,
695 _("UNIXPATH `%s' too long, maximum length is %llu\n"), unixpath,
696 sizeof (s_un.sun_path));
700 sock = GNUNET_NETWORK_socket_create (PF_UNIX, SOCK_STREAM, 0);
703 memset (&s_un, 0, sizeof (s_un));
704 s_un.sun_family = AF_UNIX;
705 slen = strlen (unixpath) + 1;
706 if (slen >= sizeof (s_un.sun_path))
707 slen = sizeof (s_un.sun_path) - 1;
708 memcpy (s_un.sun_path, unixpath, slen);
709 s_un.sun_path[slen] = '\0';
710 slen = sizeof (struct sockaddr_un);
712 s_un.sun_path[0] = '\0';
714 #if HAVE_SOCKADDR_IN_SIN_LEN
715 s_un.sun_len = (u_char) slen;
718 GNUNET_NETWORK_socket_bind (sock, (const struct sockaddr *) &s_un,
721 /* failed to bind => service must be running */
722 GNUNET_free (unixpath);
723 (void) GNUNET_NETWORK_socket_close (sock);
724 GNUNET_SCHEDULER_add_continuation (task, task_cls,
725 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
728 (void) GNUNET_NETWORK_socket_close (sock);
733 GNUNET_free_non_null (unixpath);
739 GNUNET_CONFIGURATION_get_value_number (cfg, service, "PORT", &port)) ||
742 GNUNET_CONFIGURATION_get_value_string (cfg, service, "HOSTNAME",
745 /* UNIXPATH failed (if possible) AND IP failed => error */
746 service_test_error (task, task_cls);
750 if (0 == strcmp ("localhost", hostname)
756 /* can test using 'bind' */
757 struct sockaddr_in s_in;
759 memset (&s_in, 0, sizeof (s_in));
760 #if HAVE_SOCKADDR_IN_SIN_LEN
761 s_in.sin_len = sizeof (struct sockaddr_in);
763 s_in.sin_family = AF_INET;
764 s_in.sin_port = htons (port);
766 sock = GNUNET_NETWORK_socket_create (AF_INET, SOCK_STREAM, 0);
770 GNUNET_NETWORK_socket_bind (sock, (const struct sockaddr *) &s_in,
773 /* failed to bind => service must be running */
774 GNUNET_free (hostname);
775 (void) GNUNET_NETWORK_socket_close (sock);
776 GNUNET_SCHEDULER_add_continuation (task, task_cls,
777 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
780 (void) GNUNET_NETWORK_socket_close (sock);
784 if (0 == strcmp ("ip6-localhost", hostname)
790 /* can test using 'bind' */
791 struct sockaddr_in6 s_in6;
793 memset (&s_in6, 0, sizeof (s_in6));
794 #if HAVE_SOCKADDR_IN_SIN_LEN
795 s_in6.sin6_len = sizeof (struct sockaddr_in6);
797 s_in6.sin6_family = AF_INET6;
798 s_in6.sin6_port = htons (port);
800 sock = GNUNET_NETWORK_socket_create (AF_INET6, SOCK_STREAM, 0);
804 GNUNET_NETWORK_socket_bind (sock, (const struct sockaddr *) &s_in6,
807 /* failed to bind => service must be running */
808 GNUNET_free (hostname);
809 (void) GNUNET_NETWORK_socket_close (sock);
810 GNUNET_SCHEDULER_add_continuation (task, task_cls,
811 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
814 (void) GNUNET_NETWORK_socket_close (sock);
818 if (((0 == strcmp ("localhost", hostname)) ||
819 (0 == strcmp ("ip6-localhost", hostname)))
825 /* all binds succeeded => claim service not running right now */
826 GNUNET_free_non_null (hostname);
827 service_test_error (task, task_cls);
830 GNUNET_free_non_null (hostname);
832 /* non-localhost, try 'connect' method */
833 conn = GNUNET_CLIENT_connect (service, cfg);
836 LOG (GNUNET_ERROR_TYPE_INFO,
837 _("Could not connect to service `%s', must not be running.\n"),
839 service_test_error (task, task_cls);
842 conn->test_cb = task;
843 conn->test_cb_cls = task_cls;
844 conn->test_deadline = GNUNET_TIME_relative_to_absolute (timeout);
845 if (NULL == GNUNET_CLIENT_notify_transmit_ready (conn,
846 sizeof (struct GNUNET_MessageHeader),
847 timeout, GNUNET_YES, &write_test,
850 LOG (GNUNET_ERROR_TYPE_WARNING,
851 _("Failure to transmit request to service `%s'\n"), service);
852 service_test_error (task, task_cls);
853 GNUNET_CLIENT_disconnect (conn);
860 * Connection notifies us about failure or success of
861 * a transmission request. Either pass it on to our
862 * user or, if possible, retry.
864 * @param cls our "struct GNUNET_CLIENT_TransmissionHandle"
865 * @param size number of bytes available for transmission
866 * @param buf where to write them
867 * @return number of bytes written to buf
870 client_notify (void *cls, size_t size, void *buf);
874 * This task is run if we should re-try connection to the
875 * service after a while.
877 * @param cls our "struct GNUNET_CLIENT_TransmitHandle" of the request
881 client_delayed_retry (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
883 struct GNUNET_CLIENT_TransmitHandle *th = cls;
884 struct GNUNET_TIME_Relative delay;
886 th->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
887 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
889 LOG (GNUNET_ERROR_TYPE_DEBUG, "Transmission failed due to shutdown.\n");
891 th->notify (th->notify_cls, 0, NULL);
896 do_connect (th->sock->service_name, th->sock->cfg, th->sock->attempts++);
897 if (NULL == th->sock->sock)
899 /* could happen if we're out of sockets */
901 GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining
902 (th->timeout), th->sock->back_off);
904 GNUNET_TIME_relative_min (GNUNET_TIME_relative_multiply
905 (th->sock->back_off, 2),
906 GNUNET_TIME_UNIT_SECONDS);
907 LOG (GNUNET_ERROR_TYPE_DEBUG,
908 "Transmission failed %u times, trying again in %llums.\n",
909 MAX_ATTEMPTS - th->attempts_left,
910 (unsigned long long) delay.rel_value);
912 GNUNET_SCHEDULER_add_delayed (delay, &client_delayed_retry, th);
916 GNUNET_CONNECTION_notify_transmit_ready (th->sock->sock, th->size,
917 GNUNET_TIME_absolute_get_remaining
918 (th->timeout), &client_notify,
924 th->notify (th->notify_cls, 0, NULL);
932 * Connection notifies us about failure or success of a transmission
933 * request. Either pass it on to our user or, if possible, retry.
935 * @param cls our "struct GNUNET_CLIENT_TransmissionHandle"
936 * @param size number of bytes available for transmission
937 * @param buf where to write them
938 * @return number of bytes written to buf
941 client_notify (void *cls, size_t size, void *buf)
943 struct GNUNET_CLIENT_TransmitHandle *th = cls;
944 struct GNUNET_CLIENT_Connection *sock = th->sock;
946 struct GNUNET_TIME_Relative delay;
952 delay = GNUNET_TIME_absolute_get_remaining (th->timeout);
953 delay.rel_value /= 2;
955 (GNUNET_SCHEDULER_REASON_SHUTDOWN & GNUNET_SCHEDULER_get_reason ())) ||
956 (GNUNET_YES != th->auto_retry) || (0 == --th->attempts_left) ||
957 (delay.rel_value < 1))
959 LOG (GNUNET_ERROR_TYPE_DEBUG,
960 "Transmission failed %u times, giving up.\n",
961 MAX_ATTEMPTS - th->attempts_left);
962 GNUNET_break (0 == th->notify (th->notify_cls, 0, NULL));
967 LOG (GNUNET_ERROR_TYPE_DEBUG,
968 "Failed to connect to `%s', automatically trying again.\n",
970 if (sock->in_receive == GNUNET_YES)
972 GNUNET_CONNECTION_receive_cancel (sock->sock);
973 sock->in_receive = GNUNET_NO;
975 GNUNET_CONNECTION_destroy (sock->sock);
977 delay = GNUNET_TIME_relative_min (delay, sock->back_off);
979 GNUNET_TIME_relative_min (GNUNET_TIME_relative_multiply
981 GNUNET_TIME_UNIT_SECONDS);
982 LOG (GNUNET_ERROR_TYPE_DEBUG,
983 "Transmission failed %u times, trying again in %llums.\n",
984 MAX_ATTEMPTS - th->attempts_left,
985 (unsigned long long) delay.rel_value);
988 GNUNET_SCHEDULER_add_delayed (delay, &client_delayed_retry, th);
991 GNUNET_assert (size >= th->size);
992 ret = th->notify (th->notify_cls, size, buf);
999 * Ask the client to call us once the specified number of bytes
1000 * are free in the transmission buffer. May call the notify
1001 * method immediately if enough space is available.
1003 * @param sock connection to the service
1004 * @param size number of bytes to send
1005 * @param timeout after how long should we give up (and call
1006 * notify with buf NULL and size 0)?
1007 * @param auto_retry if the connection to the service dies, should we
1008 * automatically re-connect and retry (within the timeout period)
1009 * or should we immediately fail in this case? Pass GNUNET_YES
1010 * if the caller does not care about temporary connection errors,
1011 * for example because the protocol is stateless
1012 * @param notify function to call
1013 * @param notify_cls closure for notify
1014 * @return NULL if our buffer will never hold size bytes,
1015 * a handle if the notify callback was queued (can be used to cancel)
1017 struct GNUNET_CLIENT_TransmitHandle *
1018 GNUNET_CLIENT_notify_transmit_ready (struct GNUNET_CLIENT_Connection *sock,
1020 struct GNUNET_TIME_Relative timeout,
1022 GNUNET_CONNECTION_TransmitReadyNotify
1023 notify, void *notify_cls)
1025 struct GNUNET_CLIENT_TransmitHandle *th;
1027 if (NULL != sock->th)
1029 /* If this breaks, you most likley called this function twice without waiting
1030 * for completion or canceling the request */
1034 th = GNUNET_malloc (sizeof (struct GNUNET_CLIENT_TransmitHandle));
1037 th->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1038 th->auto_retry = auto_retry;
1039 th->notify = notify;
1040 th->notify_cls = notify_cls;
1041 th->attempts_left = MAX_ATTEMPTS;
1043 if (sock->sock == NULL)
1045 th->reconnect_task =
1046 GNUNET_SCHEDULER_add_delayed (sock->back_off, &client_delayed_retry,
1053 GNUNET_CONNECTION_notify_transmit_ready (sock->sock, size, timeout,
1054 &client_notify, th);
1068 * Cancel a request for notification.
1070 * @param th handle from the original request.
1073 GNUNET_CLIENT_notify_transmit_ready_cancel (struct GNUNET_CLIENT_TransmitHandle
1076 if (th->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
1078 GNUNET_assert (NULL == th->th);
1079 GNUNET_SCHEDULER_cancel (th->reconnect_task);
1080 th->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1084 GNUNET_assert (NULL != th->th);
1085 GNUNET_CONNECTION_notify_transmit_ready_cancel (th->th);
1087 th->sock->th = NULL;
1093 * Function called to notify a client about the socket
1094 * begin ready to queue the message. "buf" will be
1095 * NULL and "size" zero if the socket was closed for
1096 * writing in the meantime.
1098 * @param cls closure of type "struct TransmitGetResponseContext*"
1099 * @param size number of bytes available in buf
1100 * @param buf where the callee should write the message
1101 * @return number of bytes written to buf
1104 transmit_for_response (void *cls, size_t size, void *buf)
1106 struct TransmitGetResponseContext *tc = cls;
1109 tc->sock->tag = NULL;
1110 msize = ntohs (tc->hdr->size);
1113 LOG (GNUNET_ERROR_TYPE_DEBUG,
1114 _("Could not submit request, not expecting to receive a response.\n"));
1116 tc->rn (tc->rn_cls, NULL);
1120 GNUNET_assert (size >= msize);
1121 memcpy (buf, tc->hdr, msize);
1122 GNUNET_CLIENT_receive (tc->sock, tc->rn, tc->rn_cls,
1123 GNUNET_TIME_absolute_get_remaining (tc->timeout));
1130 * Convenience API that combines sending a request
1131 * to the service and waiting for a response.
1132 * If either operation times out, the callback
1133 * will be called with a "NULL" response (in which
1134 * case the connection should probably be destroyed).
1136 * @param sock connection to use
1137 * @param hdr message to transmit
1138 * @param timeout when to give up (for both transmission
1139 * and for waiting for a response)
1140 * @param auto_retry if the connection to the service dies, should we
1141 * automatically re-connect and retry (within the timeout period)
1142 * or should we immediately fail in this case? Pass GNUNET_YES
1143 * if the caller does not care about temporary connection errors,
1144 * for example because the protocol is stateless
1145 * @param rn function to call with the response
1146 * @param rn_cls closure for rn
1147 * @return GNUNET_OK on success, GNUNET_SYSERR if a request
1148 * is already pending
1151 GNUNET_CLIENT_transmit_and_get_response (struct GNUNET_CLIENT_Connection *sock,
1152 const struct GNUNET_MessageHeader *hdr,
1153 struct GNUNET_TIME_Relative timeout,
1155 GNUNET_CLIENT_MessageHandler rn,
1158 struct TransmitGetResponseContext *tc;
1161 if (NULL != sock->th)
1162 return GNUNET_SYSERR;
1163 GNUNET_assert (sock->tag == NULL);
1164 msize = ntohs (hdr->size);
1165 tc = GNUNET_malloc (sizeof (struct TransmitGetResponseContext) + msize);
1167 tc->hdr = (const struct GNUNET_MessageHeader *) &tc[1];
1168 memcpy (&tc[1], hdr, msize);
1169 tc->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1171 tc->rn_cls = rn_cls;
1173 GNUNET_CLIENT_notify_transmit_ready (sock, msize, timeout, auto_retry,
1174 &transmit_for_response, tc))
1178 return GNUNET_SYSERR;
1186 /* end of client.c */