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_EXTRA_LOGGING
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
46 #define LOG(kind,...) GNUNET_log_from (kind, "util",__VA_ARGS__)
49 * Handle for a transmission request.
51 struct GNUNET_CLIENT_TransmitHandle
56 struct GNUNET_CLIENT_Connection *sock;
59 * Function to call to get the data for transmission.
61 GNUNET_CONNECTION_TransmitReadyNotify notify;
69 * Handle to the transmission with the underlying
72 struct GNUNET_CONNECTION_TransmitHandle *th;
75 * If we are re-trying and are delaying to do so,
76 * handle to the scheduled task managing the delay.
78 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
81 * Timeout for the operation overall.
83 struct GNUNET_TIME_Absolute timeout;
86 * Number of bytes requested.
91 * Are we allowed to re-try to connect without telling
92 * the user (of this API) about the connection troubles?
97 * Number of attempts left for transmitting the request. We may
98 * fail the first time (say because the service is not yet up), in
99 * which case (if auto_retry is set) we wait a bit and re-try
100 * (timeout permitting).
102 unsigned int attempts_left;
108 * Context for processing
109 * "GNUNET_CLIENT_transmit_and_get_response" requests.
111 struct TransmitGetResponseContext
116 struct GNUNET_CLIENT_Connection *sock;
119 * Message to transmit; do not free, allocated
120 * right after this struct.
122 const struct GNUNET_MessageHeader *hdr;
127 struct GNUNET_TIME_Absolute timeout;
130 * Function to call when done.
132 GNUNET_CLIENT_MessageHandler rn;
141 * Struct to refer to a GNUnet TCP connection.
142 * This is more than just a socket because if the server
143 * drops the connection, the client automatically tries
144 * to reconnect (and for that needs connection information).
146 struct GNUNET_CLIENT_Connection
150 * the socket handle, NULL if not live
152 struct GNUNET_CONNECTION_Handle *sock;
157 const struct GNUNET_CONFIGURATION_Handle *cfg;
160 * Name of the service we interact with.
165 * Context of a transmit_and_get_response operation, NULL
166 * if no such operation is pending.
168 struct TransmitGetResponseContext *tag;
171 * Handler for current receiver task.
173 GNUNET_CLIENT_MessageHandler receiver_handler;
176 * Closure for receiver_handler.
178 void *receiver_handler_cls;
181 * Handle for a pending transmission request, NULL if there is
184 struct GNUNET_CLIENT_TransmitHandle *th;
187 * Handler for service test completion (NULL unless in service_test)
189 GNUNET_SCHEDULER_Task test_cb;
192 * Deadline for calling 'test_cb'.
194 struct GNUNET_TIME_Absolute test_deadline;
197 * If we are re-trying and are delaying to do so,
198 * handle to the scheduled task managing the delay.
200 GNUNET_SCHEDULER_TaskIdentifier receive_task;
203 * Closure for test_cb (NULL unless in service_test)
208 * Buffer for received message.
213 * Timeout for receiving a response (absolute time).
215 struct GNUNET_TIME_Absolute receive_timeout;
218 * Current value for our incremental back-off (for
221 struct GNUNET_TIME_Relative back_off;
224 * Number of bytes in received_buf that are valid.
229 * Size of received_buf.
231 unsigned int received_size;
234 * Do we have a complete response in received_buf?
239 * Are we currently busy doing receive-processing?
240 * GNUNET_YES if so, GNUNET_NO if not.
245 * How often have we tried to connect?
247 unsigned int attempts;
253 * Try to connect to the service.
255 * @param service_name name of service to connect to
256 * @param cfg configuration to use
257 * @param attempt counter used to alternate between IP and UNIX domain sockets
258 * @return NULL on error
260 static struct GNUNET_CONNECTION_Handle *
261 do_connect (const char *service_name,
262 const struct GNUNET_CONFIGURATION_Handle *cfg, unsigned int attempt)
264 struct GNUNET_CONNECTION_Handle *sock;
267 unsigned long long port;
271 if (0 == (attempt % 2))
273 /* on even rounds, try UNIX */
275 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? */
277 sock = GNUNET_CONNECTION_create_from_connect_to_unixpath (cfg, unixpath);
281 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connected to unixpath `%s'!\n",
284 GNUNET_free (unixpath);
288 GNUNET_free_non_null (unixpath);
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 if (0 != (attempt % 2))
320 GNUNET_CONFIGURATION_get_value_string (cfg, service_name, "UNIXPATH",
322 (0 < strlen (unixpath)))
325 GNUNET_CONNECTION_create_from_connect_to_unixpath (cfg, unixpath);
328 GNUNET_free (unixpath);
329 GNUNET_free (hostname);
333 GNUNET_free_non_null (unixpath);
337 LOG (GNUNET_ERROR_TYPE_DEBUG,
338 "Port is 0 for service `%s', UNIXPATH did not work, returning NULL!\n",
341 GNUNET_free (hostname);
345 sock = GNUNET_CONNECTION_create_from_connect (cfg, hostname, port);
346 GNUNET_free (hostname);
352 * Get a connection with a service.
354 * @param service_name name of the service
355 * @param cfg configuration to use
356 * @return NULL on error (service unknown to configuration)
358 struct GNUNET_CLIENT_Connection *
359 GNUNET_CLIENT_connect (const char *service_name,
360 const struct GNUNET_CONFIGURATION_Handle *cfg)
362 struct GNUNET_CLIENT_Connection *ret;
363 struct GNUNET_CONNECTION_Handle *sock;
365 sock = do_connect (service_name, cfg, 0);
366 ret = GNUNET_malloc (sizeof (struct GNUNET_CLIENT_Connection));
369 ret->service_name = GNUNET_strdup (service_name);
371 ret->back_off = GNUNET_TIME_UNIT_MILLISECONDS;
377 * Destroy connection with the service. This will automatically
378 * cancel any pending "receive" request (however, the handler will
379 * *NOT* be called, not even with a NULL message). Any pending
380 * transmission request will also be cancelled UNLESS the callback for
381 * the transmission request has already been called, in which case the
382 * transmission 'finish_pending_write' argument determines whether or
383 * not the write is guaranteed to complete before the socket is fully
384 * destroyed (unless, of course, there is an error with the server in
385 * which case the message may still be lost).
387 * @param finish_pending_write should a transmission already passed to the
388 * handle be completed?
389 * @param sock handle to the service connection
392 GNUNET_CLIENT_disconnect (struct GNUNET_CLIENT_Connection *sock,
393 int finish_pending_write)
395 if (sock->in_receive == GNUNET_YES)
397 GNUNET_CONNECTION_receive_cancel (sock->sock);
398 sock->in_receive = GNUNET_NO;
400 if (sock->th != NULL)
402 GNUNET_CLIENT_notify_transmit_ready_cancel (sock->th);
405 if (NULL != sock->sock)
407 GNUNET_CONNECTION_destroy (sock->sock, finish_pending_write);
410 if (sock->receive_task != GNUNET_SCHEDULER_NO_TASK)
412 GNUNET_SCHEDULER_cancel (sock->receive_task);
413 sock->receive_task = GNUNET_SCHEDULER_NO_TASK;
415 if (sock->tag != NULL)
417 GNUNET_free (sock->tag);
420 sock->receiver_handler = NULL;
421 GNUNET_array_grow (sock->received_buf, sock->received_size, 0);
422 GNUNET_free (sock->service_name);
428 * Check if message is complete
431 check_complete (struct GNUNET_CLIENT_Connection *conn)
433 if ((conn->received_pos >= sizeof (struct GNUNET_MessageHeader)) &&
434 (conn->received_pos >=
435 ntohs (((const struct GNUNET_MessageHeader *) conn->received_buf)->
437 conn->msg_complete = GNUNET_YES;
442 * Callback function for data received from the network. Note that
443 * both "available" and "errCode" would be 0 if the read simply timed out.
446 * @param buf pointer to received data
447 * @param available number of bytes availabe in "buf",
448 * possibly 0 (on errors)
449 * @param addr address of the sender
450 * @param addrlen size of addr
451 * @param errCode value of errno (on errors receiving)
454 receive_helper (void *cls, const void *buf, size_t available,
455 const struct sockaddr *addr, socklen_t addrlen, int errCode)
457 struct GNUNET_CLIENT_Connection *conn = cls;
458 struct GNUNET_TIME_Relative remaining;
459 GNUNET_CLIENT_MessageHandler receive_handler;
460 void *receive_handler_cls;
462 GNUNET_assert (conn->msg_complete == GNUNET_NO);
463 conn->in_receive = GNUNET_NO;
464 if ((available == 0) || (conn->sock == NULL) || (errCode != 0))
466 /* signal timeout! */
468 LOG (GNUNET_ERROR_TYPE_DEBUG,
469 "Timeout in receive_helper, available %u, conn->sock %s, errCode `%s'\n",
470 (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;
527 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received message of type %u and size %u\n",
528 ntohs (cmsg->type), msize);
530 sock->receive_task = GNUNET_SCHEDULER_NO_TASK;
531 GNUNET_assert (GNUNET_YES == sock->msg_complete);
532 GNUNET_assert (sock->received_pos >= msize);
533 memcpy (msg, cmsg, msize);
534 memmove (sock->received_buf, &sock->received_buf[msize],
535 sock->received_pos - msize);
536 sock->received_pos -= msize;
537 sock->msg_complete = GNUNET_NO;
538 sock->receiver_handler = NULL;
539 check_complete (sock);
541 handler (handler_cls, msg);
546 * Read from the service.
548 * @param sock the service
549 * @param handler function to call with the message
550 * @param handler_cls closure for handler
551 * @param timeout how long to wait until timing out
554 GNUNET_CLIENT_receive (struct GNUNET_CLIENT_Connection *sock,
555 GNUNET_CLIENT_MessageHandler handler, void *handler_cls,
556 struct GNUNET_TIME_Relative timeout)
558 if (sock->sock == NULL)
560 /* already disconnected, fail instantly! */
561 GNUNET_break (0); /* this should not happen in well-written code! */
563 handler (handler_cls, NULL);
566 sock->receiver_handler = handler;
567 sock->receiver_handler_cls = handler_cls;
568 sock->receive_timeout = GNUNET_TIME_relative_to_absolute (timeout);
569 if (GNUNET_YES == sock->msg_complete)
571 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->receive_task);
572 sock->receive_task = GNUNET_SCHEDULER_add_now (&receive_task, sock);
576 GNUNET_assert (sock->in_receive == GNUNET_NO);
577 sock->in_receive = GNUNET_YES;
579 LOG (GNUNET_ERROR_TYPE_DEBUG, "calling GNUNET_CONNECTION_receive\n");
581 GNUNET_CONNECTION_receive (sock->sock, GNUNET_SERVER_MAX_MESSAGE_SIZE - 1,
582 timeout, &receive_helper, sock);
588 * Report service unavailable.
591 service_test_error (GNUNET_SCHEDULER_Task task, void *task_cls)
593 GNUNET_SCHEDULER_add_continuation (task, task_cls,
594 GNUNET_SCHEDULER_REASON_TIMEOUT);
599 * Receive confirmation from test, service is up.
602 * @param msg message received, NULL on timeout or fatal error
605 confirm_handler (void *cls, const struct GNUNET_MessageHeader *msg)
607 struct GNUNET_CLIENT_Connection *conn = cls;
609 /* We may want to consider looking at the reply in more
610 * detail in the future, for example, is this the
611 * correct service? FIXME! */
615 LOG (GNUNET_ERROR_TYPE_DEBUG,
616 "Received confirmation that service is running.\n");
618 GNUNET_SCHEDULER_add_continuation (conn->test_cb, conn->test_cb_cls,
619 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
623 service_test_error (conn->test_cb, conn->test_cb_cls);
625 GNUNET_CLIENT_disconnect (conn, GNUNET_NO);
630 * Send the 'TEST' message to the service. If successful, prepare to
633 * @param cls the 'struct GNUNET_CLIENT_Connection' of the connection to test
634 * @param size number of bytes available in buf
635 * @param buf where to write the message
636 * @return number of bytes written to buf
639 write_test (void *cls, size_t size, void *buf)
641 struct GNUNET_CLIENT_Connection *conn = cls;
642 struct GNUNET_MessageHeader *msg;
644 if (size < sizeof (struct GNUNET_MessageHeader))
647 LOG (GNUNET_ERROR_TYPE_DEBUG, _("Failure to transmit TEST request.\n"));
649 service_test_error (conn->test_cb, conn->test_cb_cls);
650 GNUNET_CLIENT_disconnect (conn, GNUNET_NO);
651 return 0; /* client disconnected */
654 LOG (GNUNET_ERROR_TYPE_DEBUG, "Transmitting `%s' request.\n", "TEST");
656 msg = (struct GNUNET_MessageHeader *) buf;
657 msg->type = htons (GNUNET_MESSAGE_TYPE_TEST);
658 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
659 GNUNET_CLIENT_receive (conn, &confirm_handler, conn,
660 GNUNET_TIME_absolute_get_remaining
661 (conn->test_deadline));
662 return sizeof (struct GNUNET_MessageHeader);
667 * Test if the service is running. If we are given a UNIXPATH or a local address,
668 * we do this NOT by trying to connect to the service, but by trying to BIND to
669 * the same port. If the BIND fails, we know the service is running.
671 * @param service name of the service to wait for
672 * @param cfg configuration to use
673 * @param timeout how long to wait at most
674 * @param task task to run if service is running
675 * (reason will be "PREREQ_DONE" (service running)
676 * or "TIMEOUT" (service not known to be running))
677 * @param task_cls closure for task
680 GNUNET_CLIENT_service_test (const char *service,
681 const struct GNUNET_CONFIGURATION_Handle *cfg,
682 struct GNUNET_TIME_Relative timeout,
683 GNUNET_SCHEDULER_Task task, void *task_cls)
686 unsigned long long port;
687 struct GNUNET_NETWORK_Handle *sock;
688 struct GNUNET_CLIENT_Connection *conn;
691 LOG (GNUNET_ERROR_TYPE_DEBUG, "Testing if service `%s' is running.\n",
696 /* probe UNIX support */
697 struct sockaddr_un s_un;
702 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? */
704 if (strlen (unixpath) >= sizeof (s_un.sun_path))
706 LOG (GNUNET_ERROR_TYPE_WARNING,
707 _("UNIXPATH `%s' too long, maximum length is %llu\n"), unixpath,
708 sizeof (s_un.sun_path));
712 sock = GNUNET_NETWORK_socket_create (PF_UNIX, SOCK_STREAM, 0);
715 memset (&s_un, 0, sizeof (s_un));
716 s_un.sun_family = AF_UNIX;
717 slen = strlen (unixpath) + 1;
718 if (slen >= sizeof (s_un.sun_path))
719 slen = sizeof (s_un.sun_path) - 1;
720 memcpy (s_un.sun_path, unixpath, slen);
721 s_un.sun_path[slen] = '\0';
722 slen = sizeof (struct sockaddr_un);
724 s_un.sun_path[0] = '\0';
726 #if HAVE_SOCKADDR_IN_SIN_LEN
727 s_un.sun_len = (u_char) slen;
730 GNUNET_NETWORK_socket_bind (sock, (const struct sockaddr *) &s_un,
733 /* failed to bind => service must be running */
734 GNUNET_free (unixpath);
735 (void) GNUNET_NETWORK_socket_close (sock);
736 GNUNET_SCHEDULER_add_continuation (task, task_cls,
737 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
740 (void) GNUNET_NETWORK_socket_close (sock);
745 GNUNET_free_non_null (unixpath);
751 GNUNET_CONFIGURATION_get_value_number (cfg, service, "PORT", &port)) ||
754 GNUNET_CONFIGURATION_get_value_string (cfg, service, "HOSTNAME",
757 /* UNIXPATH failed (if possible) AND IP failed => error */
758 service_test_error (task, task_cls);
762 if (0 == strcmp ("localhost", hostname)
768 /* can test using 'bind' */
769 struct sockaddr_in s_in;
771 memset (&s_in, 0, sizeof (s_in));
772 #if HAVE_SOCKADDR_IN_SIN_LEN
773 s_in.sin_len = sizeof (struct sockaddr_in);
775 s_in.sin_family = AF_INET;
776 s_in.sin_port = htons (port);
778 sock = GNUNET_NETWORK_socket_create (AF_INET, SOCK_STREAM, 0);
782 GNUNET_NETWORK_socket_bind (sock, (const struct sockaddr *) &s_in,
785 /* failed to bind => service must be running */
786 GNUNET_free (hostname);
787 (void) GNUNET_NETWORK_socket_close (sock);
788 GNUNET_SCHEDULER_add_continuation (task, task_cls,
789 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
792 (void) GNUNET_NETWORK_socket_close (sock);
796 if (0 == strcmp ("ip6-localhost", hostname)
802 /* can test using 'bind' */
803 struct sockaddr_in6 s_in6;
805 memset (&s_in6, 0, sizeof (s_in6));
806 #if HAVE_SOCKADDR_IN_SIN_LEN
807 s_in6.sin6_len = sizeof (struct sockaddr_in6);
809 s_in6.sin6_family = AF_INET6;
810 s_in6.sin6_port = htons (port);
812 sock = GNUNET_NETWORK_socket_create (AF_INET6, SOCK_STREAM, 0);
816 GNUNET_NETWORK_socket_bind (sock, (const struct sockaddr *) &s_in6,
819 /* failed to bind => service must be running */
820 GNUNET_free (hostname);
821 (void) GNUNET_NETWORK_socket_close (sock);
822 GNUNET_SCHEDULER_add_continuation (task, task_cls,
823 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
826 (void) GNUNET_NETWORK_socket_close (sock);
830 if (((0 == strcmp ("localhost", hostname)) ||
831 (0 == strcmp ("ip6-localhost", hostname)))
837 /* all binds succeeded => claim service not running right now */
838 GNUNET_free_non_null (hostname);
839 service_test_error (task, task_cls);
842 GNUNET_free_non_null (hostname);
844 /* non-localhost, try 'connect' method */
845 conn = GNUNET_CLIENT_connect (service, cfg);
848 LOG (GNUNET_ERROR_TYPE_INFO,
849 _("Could not connect to service `%s', must not be running.\n"),
851 service_test_error (task, task_cls);
854 conn->test_cb = task;
855 conn->test_cb_cls = task_cls;
856 conn->test_deadline = GNUNET_TIME_relative_to_absolute (timeout);
859 GNUNET_CLIENT_notify_transmit_ready (conn,
860 sizeof (struct GNUNET_MessageHeader),
861 timeout, GNUNET_YES, &write_test,
864 LOG (GNUNET_ERROR_TYPE_WARNING,
865 _("Failure to transmit request to service `%s'\n"), service);
866 service_test_error (task, task_cls);
867 GNUNET_CLIENT_disconnect (conn, GNUNET_NO);
874 * Connection notifies us about failure or success of
875 * a transmission request. Either pass it on to our
876 * user or, if possible, retry.
878 * @param cls our "struct GNUNET_CLIENT_TransmissionHandle"
879 * @param size number of bytes available for transmission
880 * @param buf where to write them
881 * @return number of bytes written to buf
884 client_notify (void *cls, size_t size, void *buf);
888 * This task is run if we should re-try connection to the
889 * service after a while.
891 * @param cls our "struct GNUNET_CLIENT_TransmitHandle" of the request
895 client_delayed_retry (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
897 struct GNUNET_CLIENT_TransmitHandle *th = cls;
898 struct GNUNET_TIME_Relative delay;
900 th->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
901 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
904 LOG (GNUNET_ERROR_TYPE_DEBUG, "Transmission failed due to shutdown.\n");
907 th->notify (th->notify_cls, 0, NULL);
912 do_connect (th->sock->service_name, th->sock->cfg, th->sock->attempts++);
913 if (NULL == th->sock->sock)
915 /* could happen if we're out of sockets */
917 GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining
918 (th->timeout), th->sock->back_off);
920 GNUNET_TIME_relative_min (GNUNET_TIME_relative_multiply
921 (th->sock->back_off, 2),
922 GNUNET_TIME_UNIT_SECONDS);
924 LOG (GNUNET_ERROR_TYPE_DEBUG,
925 "Transmission failed %u times, trying again in %llums.\n",
926 MAX_ATTEMPTS - th->attempts_left,
927 (unsigned long long) delay.rel_value);
930 GNUNET_SCHEDULER_add_delayed (delay, &client_delayed_retry, th);
934 GNUNET_CONNECTION_notify_transmit_ready (th->sock->sock, th->size,
935 GNUNET_TIME_absolute_get_remaining
936 (th->timeout), &client_notify,
942 th->notify (th->notify_cls, 0, NULL);
950 * Connection notifies us about failure or success of a transmission
951 * request. Either pass it on to our user or, if possible, retry.
953 * @param cls our "struct GNUNET_CLIENT_TransmissionHandle"
954 * @param size number of bytes available for transmission
955 * @param buf where to write them
956 * @return number of bytes written to buf
959 client_notify (void *cls, size_t size, void *buf)
961 struct GNUNET_CLIENT_TransmitHandle *th = cls;
963 struct GNUNET_TIME_Relative delay;
969 delay = GNUNET_TIME_absolute_get_remaining (th->timeout);
970 delay.rel_value /= 2;
972 (GNUNET_SCHEDULER_REASON_SHUTDOWN & GNUNET_SCHEDULER_get_reason ())) ||
973 (GNUNET_YES != th->auto_retry) || (0 == --th->attempts_left) ||
974 (delay.rel_value < 1))
977 LOG (GNUNET_ERROR_TYPE_DEBUG,
978 "Transmission failed %u times, giving up.\n",
979 MAX_ATTEMPTS - th->attempts_left);
981 GNUNET_break (0 == th->notify (th->notify_cls, 0, NULL));
987 LOG (GNUNET_ERROR_TYPE_DEBUG,
988 "Failed to connect to `%s', automatically trying again.\n",
989 th->sock->service_name);
991 GNUNET_CONNECTION_destroy (th->sock->sock, GNUNET_NO);
992 th->sock->sock = NULL;
993 delay = GNUNET_TIME_relative_min (delay, th->sock->back_off);
995 GNUNET_TIME_relative_min (GNUNET_TIME_relative_multiply
996 (th->sock->back_off, 2),
997 GNUNET_TIME_UNIT_SECONDS);
999 LOG (GNUNET_ERROR_TYPE_DEBUG,
1000 "Transmission failed %u times, trying again in %llums.\n",
1001 MAX_ATTEMPTS - th->attempts_left,
1002 (unsigned long long) delay.rel_value);
1005 th->reconnect_task =
1006 GNUNET_SCHEDULER_add_delayed (delay, &client_delayed_retry, th);
1009 GNUNET_assert (size >= th->size);
1010 ret = th->notify (th->notify_cls, size, buf);
1017 * Ask the client to call us once the specified number of bytes
1018 * are free in the transmission buffer. May call the notify
1019 * method immediately if enough space is available.
1021 * @param sock connection to the service
1022 * @param size number of bytes to send
1023 * @param timeout after how long should we give up (and call
1024 * notify with buf NULL and size 0)?
1025 * @param auto_retry if the connection to the service dies, should we
1026 * automatically re-connect and retry (within the timeout period)
1027 * or should we immediately fail in this case? Pass GNUNET_YES
1028 * if the caller does not care about temporary connection errors,
1029 * for example because the protocol is stateless
1030 * @param notify function to call
1031 * @param notify_cls closure for notify
1032 * @return NULL if our buffer will never hold size bytes,
1033 * a handle if the notify callback was queued (can be used to cancel)
1035 struct GNUNET_CLIENT_TransmitHandle *
1036 GNUNET_CLIENT_notify_transmit_ready (struct GNUNET_CLIENT_Connection *sock,
1038 struct GNUNET_TIME_Relative timeout,
1040 GNUNET_CONNECTION_TransmitReadyNotify
1041 notify, void *notify_cls)
1043 struct GNUNET_CLIENT_TransmitHandle *th;
1045 if (NULL != sock->th)
1047 /* If this breaks, you most likley called this function twice without waiting
1048 * for completion or canceling the request */
1052 th = GNUNET_malloc (sizeof (struct GNUNET_CLIENT_TransmitHandle));
1055 th->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1056 th->auto_retry = auto_retry;
1057 th->notify = notify;
1058 th->notify_cls = notify_cls;
1059 th->attempts_left = MAX_ATTEMPTS;
1061 if (sock->sock == NULL)
1063 th->reconnect_task =
1064 GNUNET_SCHEDULER_add_delayed (sock->back_off, &client_delayed_retry,
1071 GNUNET_CONNECTION_notify_transmit_ready (sock->sock, size, timeout,
1072 &client_notify, th);
1086 * Cancel a request for notification.
1088 * @param th handle from the original request.
1091 GNUNET_CLIENT_notify_transmit_ready_cancel (struct GNUNET_CLIENT_TransmitHandle
1094 if (th->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
1096 GNUNET_assert (NULL == th->th);
1097 GNUNET_SCHEDULER_cancel (th->reconnect_task);
1098 th->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1102 GNUNET_assert (NULL != th->th);
1103 GNUNET_CONNECTION_notify_transmit_ready_cancel (th->th);
1105 th->sock->th = NULL;
1111 * Function called to notify a client about the socket
1112 * begin ready to queue the message. "buf" will be
1113 * NULL and "size" zero if the socket was closed for
1114 * writing in the meantime.
1116 * @param cls closure of type "struct TransmitGetResponseContext*"
1117 * @param size number of bytes available in buf
1118 * @param buf where the callee should write the message
1119 * @return number of bytes written to buf
1122 transmit_for_response (void *cls, size_t size, void *buf)
1124 struct TransmitGetResponseContext *tc = cls;
1127 tc->sock->tag = NULL;
1128 msize = ntohs (tc->hdr->size);
1132 LOG (GNUNET_ERROR_TYPE_DEBUG,
1133 _("Could not submit request, not expecting to receive a response.\n"));
1136 tc->rn (tc->rn_cls, NULL);
1140 GNUNET_assert (size >= msize);
1141 memcpy (buf, tc->hdr, msize);
1142 GNUNET_CLIENT_receive (tc->sock, tc->rn, tc->rn_cls,
1143 GNUNET_TIME_absolute_get_remaining (tc->timeout));
1150 * Convenience API that combines sending a request
1151 * to the service and waiting for a response.
1152 * If either operation times out, the callback
1153 * will be called with a "NULL" response (in which
1154 * case the connection should probably be destroyed).
1156 * @param sock connection to use
1157 * @param hdr message to transmit
1158 * @param timeout when to give up (for both transmission
1159 * and for waiting for a response)
1160 * @param auto_retry if the connection to the service dies, should we
1161 * automatically re-connect and retry (within the timeout period)
1162 * or should we immediately fail in this case? Pass GNUNET_YES
1163 * if the caller does not care about temporary connection errors,
1164 * for example because the protocol is stateless
1165 * @param rn function to call with the response
1166 * @param rn_cls closure for rn
1167 * @return GNUNET_OK on success, GNUNET_SYSERR if a request
1168 * is already pending
1171 GNUNET_CLIENT_transmit_and_get_response (struct GNUNET_CLIENT_Connection *sock,
1172 const struct GNUNET_MessageHeader *hdr,
1173 struct GNUNET_TIME_Relative timeout,
1175 GNUNET_CLIENT_MessageHandler rn,
1178 struct TransmitGetResponseContext *tc;
1181 if (NULL != sock->th)
1182 return GNUNET_SYSERR;
1183 GNUNET_assert (sock->tag == NULL);
1184 msize = ntohs (hdr->size);
1185 tc = GNUNET_malloc (sizeof (struct TransmitGetResponseContext) + msize);
1187 tc->hdr = (const struct GNUNET_MessageHeader *) &tc[1];
1188 memcpy (&tc[1], hdr, msize);
1189 tc->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1191 tc->rn_cls = rn_cls;
1193 GNUNET_CLIENT_notify_transmit_ready (sock, msize, timeout, auto_retry,
1194 &transmit_for_response, tc))
1198 return GNUNET_SYSERR;
1206 /* end of client.c */