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
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, unsigned int attempt)
267 struct GNUNET_CONNECTION_Handle *sock;
270 unsigned long long port;
274 if (0 == (attempt % 2))
276 /* on even rounds, try UNIX */
278 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? */
280 sock = GNUNET_CONNECTION_create_from_connect_to_unixpath (cfg, unixpath);
284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connected to unixpath `%s'!\n",
287 GNUNET_free (unixpath);
291 GNUNET_free_non_null (unixpath);
296 GNUNET_CONFIGURATION_get_value_number (cfg, service_name, "PORT", &port))
299 GNUNET_CONFIGURATION_get_value_string (cfg, service_name, "HOSTNAME",
302 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
304 ("Could not determine valid hostname and port for service `%s' from configuration.\n"),
308 if (0 == strlen (hostname))
310 GNUNET_free (hostname);
311 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
312 _("Need a non-empty hostname for service `%s'.\n"),
319 if (0 != (attempt % 2))
324 GNUNET_CONFIGURATION_get_value_string (cfg, service_name, "UNIXPATH",
326 (0 < strlen (unixpath)))
329 GNUNET_CONNECTION_create_from_connect_to_unixpath (cfg, unixpath);
332 GNUNET_free (unixpath);
333 GNUNET_free (hostname);
337 GNUNET_free_non_null (unixpath);
341 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
342 "Port is 0 for service `%s', UNIXPATH did not work, returning NULL!\n",
345 GNUNET_free (hostname);
349 sock = GNUNET_CONNECTION_create_from_connect (cfg, hostname, port);
350 GNUNET_free (hostname);
356 * Get a connection with a service.
358 * @param service_name name of the service
359 * @param cfg configuration to use
360 * @return NULL on error (service unknown to configuration)
362 struct GNUNET_CLIENT_Connection *
363 GNUNET_CLIENT_connect (const char *service_name,
364 const struct GNUNET_CONFIGURATION_Handle *cfg)
366 struct GNUNET_CLIENT_Connection *ret;
367 struct GNUNET_CONNECTION_Handle *sock;
369 sock = do_connect (service_name, cfg, 0);
370 ret = GNUNET_malloc (sizeof (struct GNUNET_CLIENT_Connection));
373 ret->service_name = GNUNET_strdup (service_name);
375 ret->back_off = GNUNET_TIME_UNIT_MILLISECONDS;
381 * Configure this connection to ignore shutdown signals.
383 * @param h client handle
384 * @param do_ignore GNUNET_YES to ignore, GNUNET_NO to restore default
387 GNUNET_CLIENT_ignore_shutdown (struct GNUNET_CLIENT_Connection *h,
390 h->ignore_shutdown = do_ignore;
392 GNUNET_CONNECTION_ignore_shutdown (h->sock, do_ignore);
397 * Destroy connection with the service. This will automatically
398 * cancel any pending "receive" request (however, the handler will
399 * *NOT* be called, not even with a NULL message). Any pending
400 * transmission request will also be cancelled UNLESS the callback for
401 * the transmission request has already been called, in which case the
402 * transmission 'finish_pending_write' argument determines whether or
403 * not the write is guaranteed to complete before the socket is fully
404 * destroyed (unless, of course, there is an error with the server in
405 * which case the message may still be lost).
407 * @param finish_pending_write should a transmission already passed to the
408 * handle be completed?
409 * @param sock handle to the service connection
412 GNUNET_CLIENT_disconnect (struct GNUNET_CLIENT_Connection *sock,
413 int finish_pending_write)
415 if (sock->in_receive == GNUNET_YES)
417 GNUNET_CONNECTION_receive_cancel (sock->sock);
418 sock->in_receive = GNUNET_NO;
420 if (sock->th != NULL)
422 GNUNET_CLIENT_notify_transmit_ready_cancel (sock->th);
425 if (NULL != sock->sock)
427 GNUNET_CONNECTION_destroy (sock->sock, finish_pending_write);
430 if (sock->receive_task != GNUNET_SCHEDULER_NO_TASK)
432 GNUNET_SCHEDULER_cancel (sock->receive_task);
433 sock->receive_task = GNUNET_SCHEDULER_NO_TASK;
435 if (sock->tag != NULL)
437 GNUNET_free (sock->tag);
440 sock->receiver_handler = NULL;
441 GNUNET_array_grow (sock->received_buf, sock->received_size, 0);
442 GNUNET_free (sock->service_name);
448 * Check if message is complete
451 check_complete (struct GNUNET_CLIENT_Connection *conn)
453 if ((conn->received_pos >= sizeof (struct GNUNET_MessageHeader)) &&
454 (conn->received_pos >=
455 ntohs (((const struct GNUNET_MessageHeader *) conn->received_buf)->
457 conn->msg_complete = GNUNET_YES;
462 * Callback function for data received from the network. Note that
463 * both "available" and "errCode" would be 0 if the read simply timed out.
466 * @param buf pointer to received data
467 * @param available number of bytes availabe in "buf",
468 * possibly 0 (on errors)
469 * @param addr address of the sender
470 * @param addrlen size of addr
471 * @param errCode value of errno (on errors receiving)
474 receive_helper (void *cls, const void *buf, size_t available,
475 const struct sockaddr *addr, socklen_t addrlen, int errCode)
477 struct GNUNET_CLIENT_Connection *conn = cls;
478 struct GNUNET_TIME_Relative remaining;
479 GNUNET_CLIENT_MessageHandler receive_handler;
480 void *receive_handler_cls;
482 GNUNET_assert (conn->msg_complete == GNUNET_NO);
483 conn->in_receive = GNUNET_NO;
484 if ((available == 0) || (conn->sock == NULL) || (errCode != 0))
486 /* signal timeout! */
488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
489 "Timeout in receive_helper, available %u, conn->sock %s, errCode `%s'\n",
490 (unsigned int) available,
491 conn->sock == NULL ? "NULL" : "non-NULL", STRERROR (errCode));
493 if (NULL != (receive_handler = conn->receiver_handler))
495 receive_handler_cls = conn->receiver_handler_cls;
496 conn->receiver_handler = NULL;
497 receive_handler (receive_handler_cls, NULL);
502 /* FIXME: optimize for common fast case where buf contains the
503 * entire message and we need no copying... */
506 /* slow path: append to array */
507 if (conn->received_size < conn->received_pos + available)
508 GNUNET_array_grow (conn->received_buf, conn->received_size,
509 conn->received_pos + available);
510 memcpy (&conn->received_buf[conn->received_pos], buf, available);
511 conn->received_pos += available;
512 check_complete (conn);
513 /* check for timeout */
514 remaining = GNUNET_TIME_absolute_get_remaining (conn->receive_timeout);
515 if (remaining.rel_value == 0)
517 /* signal timeout! */
518 if (NULL != conn->receiver_handler)
519 conn->receiver_handler (conn->receiver_handler_cls, NULL);
522 /* back to receive -- either for more data or to call callback! */
523 GNUNET_CLIENT_receive (conn, conn->receiver_handler,
524 conn->receiver_handler_cls, remaining);
529 * Continuation to call the receive callback.
531 * @param cls our handle to the client connection
532 * @param tc scheduler context
535 receive_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
537 struct GNUNET_CLIENT_Connection *sock = cls;
538 GNUNET_CLIENT_MessageHandler handler = sock->receiver_handler;
539 const struct GNUNET_MessageHeader *cmsg =
540 (const struct GNUNET_MessageHeader *) sock->received_buf;
541 void *handler_cls = sock->receiver_handler_cls;
542 uint16_t msize = ntohs (cmsg->size);
544 struct GNUNET_MessageHeader *msg = (struct GNUNET_MessageHeader *) mbuf;
547 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
548 "Received message of type %u and size %u\n", ntohs (cmsg->type),
551 sock->receive_task = GNUNET_SCHEDULER_NO_TASK;
552 GNUNET_assert (GNUNET_YES == sock->msg_complete);
553 GNUNET_assert (sock->received_pos >= msize);
554 memcpy (msg, cmsg, msize);
555 memmove (sock->received_buf, &sock->received_buf[msize],
556 sock->received_pos - msize);
557 sock->received_pos -= msize;
558 sock->msg_complete = GNUNET_NO;
559 sock->receiver_handler = NULL;
560 check_complete (sock);
562 handler (handler_cls, msg);
567 * Read from the service.
569 * @param sock the service
570 * @param handler function to call with the message
571 * @param handler_cls closure for handler
572 * @param timeout how long to wait until timing out
575 GNUNET_CLIENT_receive (struct GNUNET_CLIENT_Connection *sock,
576 GNUNET_CLIENT_MessageHandler handler, void *handler_cls,
577 struct GNUNET_TIME_Relative timeout)
579 if (sock->sock == NULL)
581 /* already disconnected, fail instantly! */
582 GNUNET_break (0); /* this should not happen in well-written code! */
584 handler (handler_cls, NULL);
587 sock->receiver_handler = handler;
588 sock->receiver_handler_cls = handler_cls;
589 sock->receive_timeout = GNUNET_TIME_relative_to_absolute (timeout);
590 if (GNUNET_YES == sock->msg_complete)
592 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->receive_task);
593 sock->receive_task = GNUNET_SCHEDULER_add_now (&receive_task, sock);
597 GNUNET_assert (sock->in_receive == GNUNET_NO);
598 sock->in_receive = GNUNET_YES;
600 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "calling GNUNET_CONNECTION_receive\n");
602 GNUNET_CONNECTION_receive (sock->sock, GNUNET_SERVER_MAX_MESSAGE_SIZE - 1,
603 timeout, &receive_helper, sock);
609 * Report service unavailable.
612 service_test_error (GNUNET_SCHEDULER_Task task, void *task_cls)
614 GNUNET_SCHEDULER_add_continuation (task, task_cls,
615 GNUNET_SCHEDULER_REASON_TIMEOUT);
620 * Receive confirmation from test, service is up.
623 * @param msg message received, NULL on timeout or fatal error
626 confirm_handler (void *cls, const struct GNUNET_MessageHeader *msg)
628 struct GNUNET_CLIENT_Connection *conn = cls;
630 /* We may want to consider looking at the reply in more
631 * detail in the future, for example, is this the
632 * correct service? FIXME! */
636 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
637 "Received confirmation that service is running.\n");
639 GNUNET_SCHEDULER_add_continuation (conn->test_cb, conn->test_cb_cls,
640 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
644 service_test_error (conn->test_cb, conn->test_cb_cls);
646 GNUNET_CLIENT_disconnect (conn, GNUNET_NO);
651 * Send the 'TEST' message to the service. If successful, prepare to
654 * @param cls the 'struct GNUNET_CLIENT_Connection' of the connection to test
655 * @param size number of bytes available in buf
656 * @param buf where to write the message
657 * @return number of bytes written to buf
660 write_test (void *cls, size_t size, void *buf)
662 struct GNUNET_CLIENT_Connection *conn = cls;
663 struct GNUNET_MessageHeader *msg;
665 if (size < sizeof (struct GNUNET_MessageHeader))
668 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
669 _("Failure to transmit TEST request.\n"));
671 service_test_error (conn->test_cb, conn->test_cb_cls);
672 GNUNET_CLIENT_disconnect (conn, GNUNET_NO);
673 return 0; /* client disconnected */
676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transmitting `%s' request.\n", "TEST");
678 msg = (struct GNUNET_MessageHeader *) buf;
679 msg->type = htons (GNUNET_MESSAGE_TYPE_TEST);
680 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
681 GNUNET_CLIENT_receive (conn, &confirm_handler, conn,
682 GNUNET_TIME_absolute_get_remaining
683 (conn->test_deadline));
684 return sizeof (struct GNUNET_MessageHeader);
689 * Test if the service is running. If we are given a UNIXPATH or a local address,
690 * we do this NOT by trying to connect to the service, but by trying to BIND to
691 * the same port. If the BIND fails, we know the service is running.
693 * @param service name of the service to wait for
694 * @param cfg configuration to use
695 * @param timeout how long to wait at most
696 * @param task task to run if service is running
697 * (reason will be "PREREQ_DONE" (service running)
698 * or "TIMEOUT" (service not known to be running))
699 * @param task_cls closure for task
702 GNUNET_CLIENT_service_test (const char *service,
703 const struct GNUNET_CONFIGURATION_Handle *cfg,
704 struct GNUNET_TIME_Relative timeout,
705 GNUNET_SCHEDULER_Task task, void *task_cls)
708 unsigned long long port;
709 struct GNUNET_NETWORK_Handle *sock;
710 struct GNUNET_CLIENT_Connection *conn;
713 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testing if service `%s' is running.\n",
718 /* probe UNIX support */
719 struct sockaddr_un s_un;
724 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? */
726 if (strlen (unixpath) >= sizeof (s_un.sun_path))
728 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
729 _("UNIXPATH `%s' too long, maximum length is %llu\n"),
730 unixpath, sizeof (s_un.sun_path));
734 sock = GNUNET_NETWORK_socket_create (PF_UNIX, SOCK_STREAM, 0);
737 memset (&s_un, 0, sizeof (s_un));
738 s_un.sun_family = AF_UNIX;
739 slen = strlen (unixpath) + 1;
740 if (slen >= sizeof (s_un.sun_path))
741 slen = sizeof (s_un.sun_path) - 1;
742 memcpy (s_un.sun_path, unixpath, slen);
743 s_un.sun_path[slen] = '\0';
744 slen = sizeof (struct sockaddr_un);
746 s_un.sun_path[0] = '\0';
748 #if HAVE_SOCKADDR_IN_SIN_LEN
749 s_un.sun_len = (u_char) slen;
752 GNUNET_NETWORK_socket_bind (sock, (const struct sockaddr *) &s_un,
755 /* failed to bind => service must be running */
756 GNUNET_free (unixpath);
757 (void) GNUNET_NETWORK_socket_close (sock);
758 GNUNET_SCHEDULER_add_continuation (task, task_cls,
759 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
762 (void) GNUNET_NETWORK_socket_close (sock);
767 GNUNET_free_non_null (unixpath);
773 GNUNET_CONFIGURATION_get_value_number (cfg, service, "PORT", &port)) ||
776 GNUNET_CONFIGURATION_get_value_string (cfg, service, "HOSTNAME",
779 /* UNIXPATH failed (if possible) AND IP failed => error */
780 service_test_error (task, task_cls);
784 if (0 == strcmp ("localhost", hostname)
790 /* can test using 'bind' */
791 struct sockaddr_in s_in;
793 memset (&s_in, 0, sizeof (s_in));
794 #if HAVE_SOCKADDR_IN_SIN_LEN
795 s_in.sin_len = sizeof (struct sockaddr_in);
797 s_in.sin_family = AF_INET;
798 s_in.sin_port = htons (port);
800 sock = GNUNET_NETWORK_socket_create (AF_INET, SOCK_STREAM, 0);
804 GNUNET_NETWORK_socket_bind (sock, (const struct sockaddr *) &s_in,
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 ("ip6-localhost", hostname)
824 /* can test using 'bind' */
825 struct sockaddr_in6 s_in6;
827 memset (&s_in6, 0, sizeof (s_in6));
828 #if HAVE_SOCKADDR_IN_SIN_LEN
829 s_in6.sin6_len = sizeof (struct sockaddr_in6);
831 s_in6.sin6_family = AF_INET6;
832 s_in6.sin6_port = htons (port);
834 sock = GNUNET_NETWORK_socket_create (AF_INET6, SOCK_STREAM, 0);
838 GNUNET_NETWORK_socket_bind (sock, (const struct sockaddr *) &s_in6,
841 /* failed to bind => service must be running */
842 GNUNET_free (hostname);
843 (void) GNUNET_NETWORK_socket_close (sock);
844 GNUNET_SCHEDULER_add_continuation (task, task_cls,
845 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
848 (void) GNUNET_NETWORK_socket_close (sock);
852 if (((0 == strcmp ("localhost", hostname)) ||
853 (0 == strcmp ("ip6-localhost", hostname)))
859 /* all binds succeeded => claim service not running right now */
860 GNUNET_free_non_null (hostname);
861 service_test_error (task, task_cls);
864 GNUNET_free_non_null (hostname);
866 /* non-localhost, try 'connect' method */
867 conn = GNUNET_CLIENT_connect (service, cfg);
870 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
871 _("Could not connect to service `%s', must not be running.\n"),
873 service_test_error (task, task_cls);
876 conn->test_cb = task;
877 conn->test_cb_cls = task_cls;
878 conn->test_deadline = GNUNET_TIME_relative_to_absolute (timeout);
881 GNUNET_CLIENT_notify_transmit_ready (conn,
882 sizeof (struct GNUNET_MessageHeader),
883 timeout, GNUNET_YES, &write_test,
886 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
887 _("Failure to transmit request to service `%s'\n"), service);
888 service_test_error (task, task_cls);
889 GNUNET_CLIENT_disconnect (conn, GNUNET_NO);
896 * Connection notifies us about failure or success of
897 * a transmission request. Either pass it on to our
898 * user or, if possible, retry.
900 * @param cls our "struct GNUNET_CLIENT_TransmissionHandle"
901 * @param size number of bytes available for transmission
902 * @param buf where to write them
903 * @return number of bytes written to buf
906 client_notify (void *cls, size_t size, void *buf);
910 * This task is run if we should re-try connection to the
911 * service after a while.
913 * @param cls our "struct GNUNET_CLIENT_TransmitHandle" of the request
917 client_delayed_retry (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
919 struct GNUNET_CLIENT_TransmitHandle *th = cls;
920 struct GNUNET_TIME_Relative delay;
922 th->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
923 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
926 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
927 "Transmission failed due to shutdown.\n");
930 th->notify (th->notify_cls, 0, NULL);
935 do_connect (th->sock->service_name, th->sock->cfg, th->sock->attempts++);
936 if (NULL == th->sock->sock)
938 /* could happen if we're out of sockets */
940 GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining
941 (th->timeout), th->sock->back_off);
943 GNUNET_TIME_relative_min (GNUNET_TIME_relative_multiply
944 (th->sock->back_off, 2),
945 GNUNET_TIME_UNIT_SECONDS);
947 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
948 "Transmission failed %u times, trying again in %llums.\n",
949 MAX_ATTEMPTS - th->attempts_left,
950 (unsigned long long) delay.rel_value);
953 GNUNET_SCHEDULER_add_delayed (delay, &client_delayed_retry, th);
956 GNUNET_CONNECTION_ignore_shutdown (th->sock->sock, th->sock->ignore_shutdown);
958 GNUNET_CONNECTION_notify_transmit_ready (th->sock->sock, th->size,
959 GNUNET_TIME_absolute_get_remaining
960 (th->timeout), &client_notify,
966 th->notify (th->notify_cls, 0, NULL);
974 * Connection notifies us about failure or success of a transmission
975 * request. Either pass it on to our user or, if possible, retry.
977 * @param cls our "struct GNUNET_CLIENT_TransmissionHandle"
978 * @param size number of bytes available for transmission
979 * @param buf where to write them
980 * @return number of bytes written to buf
983 client_notify (void *cls, size_t size, void *buf)
985 struct GNUNET_CLIENT_TransmitHandle *th = cls;
987 struct GNUNET_TIME_Relative delay;
993 delay = GNUNET_TIME_absolute_get_remaining (th->timeout);
994 delay.rel_value /= 2;
996 (GNUNET_SCHEDULER_REASON_SHUTDOWN & GNUNET_SCHEDULER_get_reason ())) ||
997 (GNUNET_YES != th->auto_retry) || (0 == --th->attempts_left) ||
998 (delay.rel_value < 1))
1001 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1002 "Transmission failed %u times, giving up.\n",
1003 MAX_ATTEMPTS - th->attempts_left);
1005 GNUNET_break (0 == th->notify (th->notify_cls, 0, NULL));
1011 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1012 "Failed to connect to `%s', automatically trying again.\n",
1013 th->sock->service_name);
1015 GNUNET_CONNECTION_destroy (th->sock->sock, GNUNET_NO);
1016 th->sock->sock = NULL;
1017 delay = GNUNET_TIME_relative_min (delay, th->sock->back_off);
1018 th->sock->back_off =
1019 GNUNET_TIME_relative_min (GNUNET_TIME_relative_multiply
1020 (th->sock->back_off, 2),
1021 GNUNET_TIME_UNIT_SECONDS);
1023 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1024 "Transmission failed %u times, trying again in %llums.\n",
1025 MAX_ATTEMPTS - th->attempts_left,
1026 (unsigned long long) delay.rel_value);
1029 th->reconnect_task =
1030 GNUNET_SCHEDULER_add_delayed (delay, &client_delayed_retry, th);
1033 GNUNET_assert (size >= th->size);
1034 ret = th->notify (th->notify_cls, size, buf);
1041 * Ask the client to call us once the specified number of bytes
1042 * are free in the transmission buffer. May call the notify
1043 * method immediately if enough space is available.
1045 * @param sock connection to the service
1046 * @param size number of bytes to send
1047 * @param timeout after how long should we give up (and call
1048 * notify with buf NULL and size 0)?
1049 * @param auto_retry if the connection to the service dies, should we
1050 * automatically re-connect and retry (within the timeout period)
1051 * or should we immediately fail in this case? Pass GNUNET_YES
1052 * if the caller does not care about temporary connection errors,
1053 * for example because the protocol is stateless
1054 * @param notify function to call
1055 * @param notify_cls closure for notify
1056 * @return NULL if our buffer will never hold size bytes,
1057 * a handle if the notify callback was queued (can be used to cancel)
1059 struct GNUNET_CLIENT_TransmitHandle *
1060 GNUNET_CLIENT_notify_transmit_ready (struct GNUNET_CLIENT_Connection *sock,
1062 struct GNUNET_TIME_Relative timeout,
1064 GNUNET_CONNECTION_TransmitReadyNotify
1065 notify, void *notify_cls)
1067 struct GNUNET_CLIENT_TransmitHandle *th;
1069 if (NULL != sock->th)
1071 /* If this breaks, you most likley called this function twice without waiting
1072 * for completion or canceling the request */
1076 th = GNUNET_malloc (sizeof (struct GNUNET_CLIENT_TransmitHandle));
1079 th->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1080 th->auto_retry = auto_retry;
1081 th->notify = notify;
1082 th->notify_cls = notify_cls;
1083 th->attempts_left = MAX_ATTEMPTS;
1085 if (sock->sock == NULL)
1087 th->reconnect_task =
1088 GNUNET_SCHEDULER_add_delayed (sock->back_off, &client_delayed_retry,
1095 GNUNET_CONNECTION_notify_transmit_ready (sock->sock, size, timeout,
1096 &client_notify, th);
1110 * Cancel a request for notification.
1112 * @param th handle from the original request.
1115 GNUNET_CLIENT_notify_transmit_ready_cancel (struct GNUNET_CLIENT_TransmitHandle
1118 if (th->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
1120 GNUNET_assert (NULL == th->th);
1121 GNUNET_SCHEDULER_cancel (th->reconnect_task);
1122 th->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1126 GNUNET_assert (NULL != th->th);
1127 GNUNET_CONNECTION_notify_transmit_ready_cancel (th->th);
1129 th->sock->th = NULL;
1135 * Function called to notify a client about the socket
1136 * begin ready to queue the message. "buf" will be
1137 * NULL and "size" zero if the socket was closed for
1138 * writing in the meantime.
1140 * @param cls closure of type "struct TransmitGetResponseContext*"
1141 * @param size number of bytes available in buf
1142 * @param buf where the callee should write the message
1143 * @return number of bytes written to buf
1146 transmit_for_response (void *cls, size_t size, void *buf)
1148 struct TransmitGetResponseContext *tc = cls;
1151 tc->sock->tag = NULL;
1152 msize = ntohs (tc->hdr->size);
1156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1158 ("Could not submit request, not expecting to receive a response.\n"));
1161 tc->rn (tc->rn_cls, NULL);
1165 GNUNET_assert (size >= msize);
1166 memcpy (buf, tc->hdr, msize);
1167 GNUNET_CLIENT_receive (tc->sock, tc->rn, tc->rn_cls,
1168 GNUNET_TIME_absolute_get_remaining (tc->timeout));
1175 * Convenience API that combines sending a request
1176 * to the service and waiting for a response.
1177 * If either operation times out, the callback
1178 * will be called with a "NULL" response (in which
1179 * case the connection should probably be destroyed).
1181 * @param sock connection to use
1182 * @param hdr message to transmit
1183 * @param timeout when to give up (for both transmission
1184 * and for waiting for a response)
1185 * @param auto_retry if the connection to the service dies, should we
1186 * automatically re-connect and retry (within the timeout period)
1187 * or should we immediately fail in this case? Pass GNUNET_YES
1188 * if the caller does not care about temporary connection errors,
1189 * for example because the protocol is stateless
1190 * @param rn function to call with the response
1191 * @param rn_cls closure for rn
1192 * @return GNUNET_OK on success, GNUNET_SYSERR if a request
1193 * is already pending
1196 GNUNET_CLIENT_transmit_and_get_response (struct GNUNET_CLIENT_Connection *sock,
1197 const struct GNUNET_MessageHeader *hdr,
1198 struct GNUNET_TIME_Relative timeout,
1200 GNUNET_CLIENT_MessageHandler rn,
1203 struct TransmitGetResponseContext *tc;
1206 if (NULL != sock->th)
1207 return GNUNET_SYSERR;
1208 GNUNET_assert (sock->tag == NULL);
1209 msize = ntohs (hdr->size);
1210 tc = GNUNET_malloc (sizeof (struct TransmitGetResponseContext) + msize);
1212 tc->hdr = (const struct GNUNET_MessageHeader *) &tc[1];
1213 memcpy (&tc[1], hdr, msize);
1214 tc->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1216 tc->rn_cls = rn_cls;
1218 GNUNET_CLIENT_notify_transmit_ready (sock, msize, timeout, auto_retry,
1219 &transmit_for_response, tc))
1223 return GNUNET_SYSERR;
1231 /* end of client.c */