2 This file is part of GNUnet.
3 (C) 2001, 2002, 2006, 2008, 2009, 2012 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.
30 #include "gnunet_common.h"
31 #include "gnunet_client_lib.h"
32 #include "gnunet_protocols.h"
33 #include "gnunet_server_lib.h"
34 #include "gnunet_scheduler_lib.h"
38 * How often do we re-try tranmsitting requests before giving up?
39 * Note that if we succeeded transmitting a request but failed to read
40 * a response, we do NOT re-try.
42 #define MAX_ATTEMPTS 50
44 #define LOG(kind,...) GNUNET_log_from (kind, "util",__VA_ARGS__)
47 * Handle for a transmission request.
49 struct GNUNET_CLIENT_TransmitHandle
54 struct GNUNET_CLIENT_Connection *client;
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 *client;
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 connection handle, NULL if not live
150 struct GNUNET_CONNECTION_Handle *connection;
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 * How often have we tried to connect?
245 unsigned int attempts;
251 * Try to connect to the service.
253 * @param service_name name of service to connect to
254 * @param cfg configuration to use
255 * @param attempt counter used to alternate between IP and UNIX domain sockets
256 * @return NULL on error
258 static struct GNUNET_CONNECTION_Handle *
259 do_connect (const char *service_name,
260 const struct GNUNET_CONFIGURATION_Handle *cfg, unsigned int attempt)
262 struct GNUNET_CONNECTION_Handle *connection;
265 unsigned long long port;
269 if (0 == (attempt % 2))
271 /* on even rounds, try UNIX */
273 if ((GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg, service_name, "UNIXPATH", &unixpath)) &&
274 (0 < strlen (unixpath)))
276 /* We have a non-NULL unixpath, need to validate it */
277 connection = GNUNET_CONNECTION_create_from_connect_to_unixpath (cfg, unixpath);
278 if (NULL != connection)
280 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connected to unixpath `%s'!\n",
282 GNUNET_free (unixpath);
286 GNUNET_free_non_null (unixpath);
291 GNUNET_CONFIGURATION_have_value (cfg, service_name, "PORT"))
294 GNUNET_CONFIGURATION_get_value_number (cfg, service_name, "PORT", &port))
297 GNUNET_CONFIGURATION_get_value_string (cfg, service_name, "HOSTNAME",
300 LOG (GNUNET_ERROR_TYPE_WARNING,
302 ("Could not determine valid hostname and port for service `%s' from configuration.\n"),
306 if (0 == strlen (hostname))
308 GNUNET_free (hostname);
309 LOG (GNUNET_ERROR_TYPE_WARNING,
310 _("Need a non-empty hostname for service `%s'.\n"), service_name);
316 /* unspecified means 0 (disabled) */
323 if (0 != (attempt % 2))
328 GNUNET_CONFIGURATION_get_value_string (cfg, service_name, "UNIXPATH",
330 (0 < strlen (unixpath)))
333 GNUNET_CONNECTION_create_from_connect_to_unixpath (cfg, unixpath);
334 if (NULL != connection)
336 GNUNET_free (unixpath);
337 GNUNET_free_non_null (hostname);
341 GNUNET_free_non_null (unixpath);
344 LOG (GNUNET_ERROR_TYPE_DEBUG,
345 "Port is 0 for service `%s', UNIXPATH did not work, returning NULL!\n",
347 GNUNET_free_non_null (hostname);
350 connection = 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 *client;
368 struct GNUNET_CONNECTION_Handle *connection;
370 connection = do_connect (service_name, cfg, 0);
371 client = GNUNET_malloc (sizeof (struct GNUNET_CLIENT_Connection));
372 client->attempts = 1;
373 client->connection = connection;
374 client->service_name = GNUNET_strdup (service_name);
376 client->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 client handle to the service connection
395 GNUNET_CLIENT_disconnect (struct GNUNET_CLIENT_Connection *client)
397 if (GNUNET_YES == client->in_receive)
399 GNUNET_CONNECTION_receive_cancel (client->connection);
400 client->in_receive = GNUNET_NO;
402 if (NULL != client->th)
404 GNUNET_CLIENT_notify_transmit_ready_cancel (client->th);
407 if (NULL != client->connection)
409 GNUNET_CONNECTION_destroy (client->connection);
410 client->connection = NULL;
412 if (GNUNET_SCHEDULER_NO_TASK != client->receive_task)
414 GNUNET_SCHEDULER_cancel (client->receive_task);
415 client->receive_task = GNUNET_SCHEDULER_NO_TASK;
417 if (NULL != client->tag)
419 GNUNET_free (client->tag);
422 client->receiver_handler = NULL;
423 GNUNET_array_grow (client->received_buf, client->received_size, 0);
424 GNUNET_free (client->service_name);
425 GNUNET_free (client);
430 * Check if message is complete. Sets the "msg_complete" member
431 * in the client struct.
433 * @param client connection with the buffer to check
436 check_complete (struct GNUNET_CLIENT_Connection *client)
438 if ((client->received_pos >= sizeof (struct GNUNET_MessageHeader)) &&
439 (client->received_pos >=
440 ntohs (((const struct GNUNET_MessageHeader *) client->received_buf)->
442 client->msg_complete = GNUNET_YES;
447 * Callback function for data received from the network. Note that
448 * both "available" and "errCode" would be 0 if the read simply timed out.
451 * @param buf pointer to received data
452 * @param available number of bytes availabe in "buf",
453 * possibly 0 (on errors)
454 * @param addr address of the sender
455 * @param addrlen size of addr
456 * @param errCode value of errno (on errors receiving)
459 receive_helper (void *cls, const void *buf, size_t available,
460 const struct sockaddr *addr, socklen_t addrlen, int errCode)
462 struct GNUNET_CLIENT_Connection *client = cls;
463 struct GNUNET_TIME_Relative remaining;
464 GNUNET_CLIENT_MessageHandler receive_handler;
465 void *receive_handler_cls;
467 GNUNET_assert (GNUNET_NO == client->msg_complete);
468 client->in_receive = GNUNET_NO;
469 if ((0 == available) || (NULL == client->connection) || (0 != errCode))
471 /* signal timeout! */
472 LOG (GNUNET_ERROR_TYPE_DEBUG,
473 "Timeout in receive_helper, available %u, client->connection %s, errCode `%s'\n",
474 (unsigned int) available, NULL == client->connection ? "NULL" : "non-NULL",
476 if (NULL != (receive_handler = client->receiver_handler))
478 receive_handler_cls = client->receiver_handler_cls;
479 client->receiver_handler = NULL;
480 receive_handler (receive_handler_cls, NULL);
484 /* FIXME: optimize for common fast case where buf contains the
485 * entire message and we need no copying... */
487 /* slow path: append to array */
488 if (client->received_size < client->received_pos + available)
489 GNUNET_array_grow (client->received_buf, client->received_size,
490 client->received_pos + available);
491 memcpy (&client->received_buf[client->received_pos], buf, available);
492 client->received_pos += available;
493 check_complete (client);
494 /* check for timeout */
495 remaining = GNUNET_TIME_absolute_get_remaining (client->receive_timeout);
496 if (0 == remaining.rel_value)
498 /* signal timeout! */
499 if (NULL != client->receiver_handler)
500 client->receiver_handler (client->receiver_handler_cls, NULL);
503 /* back to receive -- either for more data or to call callback! */
504 GNUNET_CLIENT_receive (client, client->receiver_handler,
505 client->receiver_handler_cls, remaining);
510 * Continuation to call the receive callback.
512 * @param cls our handle to the client connection
513 * @param tc scheduler context
516 receive_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
518 struct GNUNET_CLIENT_Connection *client = cls;
519 GNUNET_CLIENT_MessageHandler handler = client->receiver_handler;
520 const struct GNUNET_MessageHeader *cmsg =
521 (const struct GNUNET_MessageHeader *) client->received_buf;
522 void *handler_cls = client->receiver_handler_cls;
523 uint16_t msize = ntohs (cmsg->size);
525 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);
529 client->receive_task = GNUNET_SCHEDULER_NO_TASK;
530 GNUNET_assert (GNUNET_YES == client->msg_complete);
531 GNUNET_assert (client->received_pos >= msize);
532 memcpy (msg, cmsg, msize);
533 memmove (client->received_buf, &client->received_buf[msize],
534 client->received_pos - msize);
535 client->received_pos -= msize;
536 client->msg_complete = GNUNET_NO;
537 client->receiver_handler = NULL;
538 check_complete (client);
540 handler (handler_cls, msg);
545 * Read from the service.
547 * @param client the service
548 * @param handler function to call with the message
549 * @param handler_cls closure for handler
550 * @param timeout how long to wait until timing out
553 GNUNET_CLIENT_receive (struct GNUNET_CLIENT_Connection *client,
554 GNUNET_CLIENT_MessageHandler handler, void *handler_cls,
555 struct GNUNET_TIME_Relative timeout)
557 if (NULL == client->connection)
559 /* already disconnected, fail instantly! */
560 GNUNET_break (0); /* this should not happen in well-written code! */
562 handler (handler_cls, NULL);
565 client->receiver_handler = handler;
566 client->receiver_handler_cls = handler_cls;
567 client->receive_timeout = GNUNET_TIME_relative_to_absolute (timeout);
568 if (GNUNET_YES == client->msg_complete)
570 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == client->receive_task);
571 client->receive_task = GNUNET_SCHEDULER_add_now (&receive_task, client);
575 GNUNET_assert (GNUNET_NO == client->in_receive);
576 client->in_receive = GNUNET_YES;
577 LOG (GNUNET_ERROR_TYPE_DEBUG, "calling GNUNET_CONNECTION_receive\n");
578 GNUNET_CONNECTION_receive (client->connection, GNUNET_SERVER_MAX_MESSAGE_SIZE - 1,
579 timeout, &receive_helper, client);
585 * Report service unavailable.
588 service_test_error (GNUNET_SCHEDULER_Task task, void *task_cls)
590 GNUNET_SCHEDULER_add_continuation (task, task_cls,
591 GNUNET_SCHEDULER_REASON_TIMEOUT);
596 * Receive confirmation from test, service is up.
599 * @param msg message received, NULL on timeout or fatal error
602 confirm_handler (void *cls, const struct GNUNET_MessageHeader *msg)
604 struct GNUNET_CLIENT_Connection *client = cls;
606 /* We may want to consider looking at the reply in more
607 * detail in the future, for example, is this the
608 * correct service? FIXME! */
611 LOG (GNUNET_ERROR_TYPE_DEBUG,
612 "Received confirmation that service is running.\n");
613 GNUNET_SCHEDULER_add_continuation (client->test_cb, client->test_cb_cls,
614 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
618 service_test_error (client->test_cb, client->test_cb_cls);
620 GNUNET_CLIENT_disconnect (client);
625 * Send the 'TEST' message to the service. If successful, prepare to
628 * @param cls the 'struct GNUNET_CLIENT_Connection' of the connection to test
629 * @param size number of bytes available in buf
630 * @param buf where to write the message
631 * @return number of bytes written to buf
634 write_test (void *cls, size_t size, void *buf)
636 struct GNUNET_CLIENT_Connection *client = cls;
637 struct GNUNET_MessageHeader *msg;
639 if (size < sizeof (struct GNUNET_MessageHeader))
641 LOG (GNUNET_ERROR_TYPE_DEBUG, _("Failure to transmit TEST request.\n"));
642 service_test_error (client->test_cb, client->test_cb_cls);
643 GNUNET_CLIENT_disconnect (client);
644 return 0; /* client disconnected */
646 LOG (GNUNET_ERROR_TYPE_DEBUG, "Transmitting `%s' request.\n", "TEST");
647 msg = (struct GNUNET_MessageHeader *) buf;
648 msg->type = htons (GNUNET_MESSAGE_TYPE_TEST);
649 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
650 GNUNET_CLIENT_receive (client, &confirm_handler, client,
651 GNUNET_TIME_absolute_get_remaining
652 (client->test_deadline));
653 return sizeof (struct GNUNET_MessageHeader);
658 * Test if the service is running. If we are given a UNIXPATH or a local address,
659 * we do this NOT by trying to connect to the service, but by trying to BIND to
660 * the same port. If the BIND fails, we know the service is running.
662 * @param service name of the service to wait for
663 * @param cfg configuration to use
664 * @param timeout how long to wait at most
665 * @param task task to run if service is running
666 * (reason will be "PREREQ_DONE" (service running)
667 * or "TIMEOUT" (service not known to be running))
668 * @param task_cls closure for task
671 GNUNET_CLIENT_service_test (const char *service,
672 const struct GNUNET_CONFIGURATION_Handle *cfg,
673 struct GNUNET_TIME_Relative timeout,
674 GNUNET_SCHEDULER_Task task, void *task_cls)
677 unsigned long long port;
678 struct GNUNET_NETWORK_Handle *sock;
679 struct GNUNET_CLIENT_Connection *client;
681 LOG (GNUNET_ERROR_TYPE_DEBUG, "Testing if service `%s' is running.\n",
685 /* probe UNIX support */
686 struct sockaddr_un s_un;
691 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? */
693 if (strlen (unixpath) >= sizeof (s_un.sun_path))
695 LOG (GNUNET_ERROR_TYPE_WARNING,
696 _("UNIXPATH `%s' too long, maximum length is %llu\n"), unixpath,
697 sizeof (s_un.sun_path));
701 sock = GNUNET_NETWORK_socket_create (PF_UNIX, SOCK_STREAM, 0);
704 memset (&s_un, 0, sizeof (s_un));
705 s_un.sun_family = AF_UNIX;
706 slen = strlen (unixpath) + 1;
707 if (slen >= sizeof (s_un.sun_path))
708 slen = sizeof (s_un.sun_path) - 1;
709 memcpy (s_un.sun_path, unixpath, slen);
710 s_un.sun_path[slen] = '\0';
711 slen = sizeof (struct sockaddr_un);
713 s_un.sun_path[0] = '\0';
715 #if HAVE_SOCKADDR_IN_SIN_LEN
716 s_un.sun_len = (u_char) slen;
719 GNUNET_NETWORK_socket_bind (sock, (const struct sockaddr *) &s_un,
722 /* failed to bind => service must be running */
723 GNUNET_free (unixpath);
724 (void) GNUNET_NETWORK_socket_close (sock);
725 GNUNET_SCHEDULER_add_continuation (task, task_cls,
726 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
729 (void) GNUNET_NETWORK_socket_close (sock);
734 GNUNET_free_non_null (unixpath);
740 GNUNET_CONFIGURATION_get_value_number (cfg, service, "PORT", &port)) ||
743 GNUNET_CONFIGURATION_get_value_string (cfg, service, "HOSTNAME",
746 /* UNIXPATH failed (if possible) AND IP failed => error */
747 service_test_error (task, task_cls);
751 if (0 == strcmp ("localhost", hostname)
757 /* can test using 'bind' */
758 struct sockaddr_in s_in;
760 memset (&s_in, 0, sizeof (s_in));
761 #if HAVE_SOCKADDR_IN_SIN_LEN
762 s_in.sin_len = sizeof (struct sockaddr_in);
764 s_in.sin_family = AF_INET;
765 s_in.sin_port = htons (port);
767 sock = GNUNET_NETWORK_socket_create (AF_INET, SOCK_STREAM, 0);
771 GNUNET_NETWORK_socket_bind (sock, (const struct sockaddr *) &s_in,
774 /* failed to bind => service must be running */
775 GNUNET_free (hostname);
776 (void) GNUNET_NETWORK_socket_close (sock);
777 GNUNET_SCHEDULER_add_continuation (task, task_cls,
778 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
781 (void) GNUNET_NETWORK_socket_close (sock);
785 if (0 == strcmp ("ip6-localhost", hostname)
791 /* can test using 'bind' */
792 struct sockaddr_in6 s_in6;
794 memset (&s_in6, 0, sizeof (s_in6));
795 #if HAVE_SOCKADDR_IN_SIN_LEN
796 s_in6.sin6_len = sizeof (struct sockaddr_in6);
798 s_in6.sin6_family = AF_INET6;
799 s_in6.sin6_port = htons (port);
801 sock = GNUNET_NETWORK_socket_create (AF_INET6, SOCK_STREAM, 0);
805 GNUNET_NETWORK_socket_bind (sock, (const struct sockaddr *) &s_in6,
808 /* failed to bind => service must be running */
809 GNUNET_free (hostname);
810 (void) GNUNET_NETWORK_socket_close (sock);
811 GNUNET_SCHEDULER_add_continuation (task, task_cls,
812 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
815 (void) GNUNET_NETWORK_socket_close (sock);
819 if (((0 == strcmp ("localhost", hostname)) ||
820 (0 == strcmp ("ip6-localhost", hostname)))
826 /* all binds succeeded => claim service not running right now */
827 GNUNET_free_non_null (hostname);
828 service_test_error (task, task_cls);
831 GNUNET_free_non_null (hostname);
833 /* non-localhost, try 'connect' method */
834 client = GNUNET_CLIENT_connect (service, cfg);
837 LOG (GNUNET_ERROR_TYPE_INFO,
838 _("Could not connect to service `%s', must not be running.\n"),
840 service_test_error (task, task_cls);
843 client->test_cb = task;
844 client->test_cb_cls = task_cls;
845 client->test_deadline = GNUNET_TIME_relative_to_absolute (timeout);
846 if (NULL == GNUNET_CLIENT_notify_transmit_ready (client,
847 sizeof (struct GNUNET_MessageHeader),
848 timeout, GNUNET_YES, &write_test,
851 LOG (GNUNET_ERROR_TYPE_WARNING,
852 _("Failure to transmit request to service `%s'\n"), service);
853 service_test_error (task, task_cls);
854 GNUNET_CLIENT_disconnect (client);
861 * Connection notifies us about failure or success of
862 * a transmission request. Either pass it on to our
863 * user or, if possible, retry.
865 * @param cls our "struct GNUNET_CLIENT_TransmissionHandle"
866 * @param size number of bytes available for transmission
867 * @param buf where to write them
868 * @return number of bytes written to buf
871 client_notify (void *cls, size_t size, void *buf);
875 * This task is run if we should re-try connection to the
876 * service after a while.
878 * @param cls our "struct GNUNET_CLIENT_TransmitHandle" of the request
882 client_delayed_retry (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
884 struct GNUNET_CLIENT_TransmitHandle *th = cls;
885 struct GNUNET_TIME_Relative delay;
887 th->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
888 th->client->connection =
889 do_connect (th->client->service_name, th->client->cfg, th->client->attempts++);
890 if (NULL == th->client->connection)
892 /* could happen if we're out of sockets */
894 GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining
895 (th->timeout), th->client->back_off);
896 th->client->back_off =
897 GNUNET_TIME_relative_min (GNUNET_TIME_relative_multiply
898 (th->client->back_off, 2),
899 GNUNET_TIME_UNIT_SECONDS);
900 LOG (GNUNET_ERROR_TYPE_DEBUG,
901 "Transmission failed %u times, trying again in %llums.\n",
902 MAX_ATTEMPTS - th->attempts_left,
903 (unsigned long long) delay.rel_value);
905 GNUNET_SCHEDULER_add_delayed (delay, &client_delayed_retry, th);
909 GNUNET_CONNECTION_notify_transmit_ready (th->client->connection, th->size,
910 GNUNET_TIME_absolute_get_remaining
911 (th->timeout), &client_notify,
916 th->client->th = NULL;
917 th->notify (th->notify_cls, 0, NULL);
925 * Connection notifies us about failure or success of a transmission
926 * request. Either pass it on to our 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
934 client_notify (void *cls, size_t size, void *buf)
936 struct GNUNET_CLIENT_TransmitHandle *th = cls;
937 struct GNUNET_CLIENT_Connection *client = th->client;
939 struct GNUNET_TIME_Relative delay;
945 delay = GNUNET_TIME_absolute_get_remaining (th->timeout);
946 delay.rel_value /= 2;
947 if ((GNUNET_YES != th->auto_retry) || (0 == --th->attempts_left) ||
948 (delay.rel_value < 1))
950 LOG (GNUNET_ERROR_TYPE_DEBUG,
951 "Transmission failed %u times, giving up.\n",
952 MAX_ATTEMPTS - th->attempts_left);
953 GNUNET_break (0 == th->notify (th->notify_cls, 0, NULL));
958 LOG (GNUNET_ERROR_TYPE_DEBUG,
959 "Failed to connect to `%s', automatically trying again.\n",
960 client->service_name);
961 if (GNUNET_YES == client->in_receive)
963 GNUNET_CONNECTION_receive_cancel (client->connection);
964 client->in_receive = GNUNET_NO;
966 GNUNET_CONNECTION_destroy (client->connection);
967 client->connection = NULL;
968 delay = GNUNET_TIME_relative_min (delay, client->back_off);
970 GNUNET_TIME_relative_min (GNUNET_TIME_relative_multiply
971 (client->back_off, 2),
972 GNUNET_TIME_UNIT_SECONDS);
973 LOG (GNUNET_ERROR_TYPE_DEBUG,
974 "Transmission failed %u times, trying again in %llums.\n",
975 MAX_ATTEMPTS - th->attempts_left,
976 (unsigned long long) delay.rel_value);
979 GNUNET_SCHEDULER_add_delayed (delay, &client_delayed_retry, th);
982 GNUNET_assert (size >= th->size);
983 ret = th->notify (th->notify_cls, size, buf);
990 * Ask the client to call us once the specified number of bytes
991 * are free in the transmission buffer. May call the notify
992 * method immediately if enough space is available.
994 * @param client connection to the service
995 * @param size number of bytes to send
996 * @param timeout after how long should we give up (and call
997 * notify with buf NULL and size 0)?
998 * @param auto_retry if the connection to the service dies, should we
999 * automatically re-connect and retry (within the timeout period)
1000 * or should we immediately fail in this case? Pass GNUNET_YES
1001 * if the caller does not care about temporary connection errors,
1002 * for example because the protocol is stateless
1003 * @param notify function to call
1004 * @param notify_cls closure for notify
1005 * @return NULL if our buffer will never hold size bytes,
1006 * a handle if the notify callback was queued (can be used to cancel)
1008 struct GNUNET_CLIENT_TransmitHandle *
1009 GNUNET_CLIENT_notify_transmit_ready (struct GNUNET_CLIENT_Connection *client,
1011 struct GNUNET_TIME_Relative timeout,
1013 GNUNET_CONNECTION_TransmitReadyNotify
1014 notify, void *notify_cls)
1016 struct GNUNET_CLIENT_TransmitHandle *th;
1018 if (NULL != client->th)
1020 /* If this breaks, you most likley called this function twice without waiting
1021 * for completion or canceling the request */
1025 th = GNUNET_malloc (sizeof (struct GNUNET_CLIENT_TransmitHandle));
1026 th->client = client;
1028 th->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1029 th->auto_retry = auto_retry;
1030 th->notify = notify;
1031 th->notify_cls = notify_cls;
1032 th->attempts_left = MAX_ATTEMPTS;
1034 if (NULL == client->connection)
1036 th->reconnect_task =
1037 GNUNET_SCHEDULER_add_delayed (client->back_off, &client_delayed_retry,
1044 GNUNET_CONNECTION_notify_transmit_ready (client->connection, size, timeout,
1045 &client_notify, th);
1059 * Cancel a request for notification.
1061 * @param th handle from the original request.
1064 GNUNET_CLIENT_notify_transmit_ready_cancel (struct GNUNET_CLIENT_TransmitHandle
1067 if (GNUNET_SCHEDULER_NO_TASK != th->reconnect_task)
1069 GNUNET_assert (NULL == th->th);
1070 GNUNET_SCHEDULER_cancel (th->reconnect_task);
1071 th->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1075 GNUNET_assert (NULL != th->th);
1076 GNUNET_CONNECTION_notify_transmit_ready_cancel (th->th);
1078 th->client->th = NULL;
1084 * Function called to notify a client about the socket
1085 * begin ready to queue the message. "buf" will be
1086 * NULL and "size" zero if the socket was closed for
1087 * writing in the meantime.
1089 * @param cls closure of type "struct TransmitGetResponseContext*"
1090 * @param size number of bytes available in buf
1091 * @param buf where the callee should write the message
1092 * @return number of bytes written to buf
1095 transmit_for_response (void *cls, size_t size, void *buf)
1097 struct TransmitGetResponseContext *tc = cls;
1100 tc->client->tag = NULL;
1101 msize = ntohs (tc->hdr->size);
1104 LOG (GNUNET_ERROR_TYPE_DEBUG,
1105 _("Could not submit request, not expecting to receive a response.\n"));
1107 tc->rn (tc->rn_cls, NULL);
1111 GNUNET_assert (size >= msize);
1112 memcpy (buf, tc->hdr, msize);
1113 GNUNET_CLIENT_receive (tc->client, tc->rn, tc->rn_cls,
1114 GNUNET_TIME_absolute_get_remaining (tc->timeout));
1121 * Convenience API that combines sending a request
1122 * to the service and waiting for a response.
1123 * If either operation times out, the callback
1124 * will be called with a "NULL" response (in which
1125 * case the connection should probably be destroyed).
1127 * @param client connection to use
1128 * @param hdr message to transmit
1129 * @param timeout when to give up (for both transmission
1130 * and for waiting for a response)
1131 * @param auto_retry if the connection to the service dies, should we
1132 * automatically re-connect and retry (within the timeout period)
1133 * or should we immediately fail in this case? Pass GNUNET_YES
1134 * if the caller does not care about temporary connection errors,
1135 * for example because the protocol is stateless
1136 * @param rn function to call with the response
1137 * @param rn_cls closure for rn
1138 * @return GNUNET_OK on success, GNUNET_SYSERR if a request
1139 * is already pending
1142 GNUNET_CLIENT_transmit_and_get_response (struct GNUNET_CLIENT_Connection *client,
1143 const struct GNUNET_MessageHeader *hdr,
1144 struct GNUNET_TIME_Relative timeout,
1146 GNUNET_CLIENT_MessageHandler rn,
1149 struct TransmitGetResponseContext *tc;
1152 if (NULL != client->th)
1153 return GNUNET_SYSERR;
1154 GNUNET_assert (NULL == client->tag);
1155 msize = ntohs (hdr->size);
1156 tc = GNUNET_malloc (sizeof (struct TransmitGetResponseContext) + msize);
1157 tc->client = client;
1158 tc->hdr = (const struct GNUNET_MessageHeader *) &tc[1];
1159 memcpy (&tc[1], hdr, msize);
1160 tc->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1162 tc->rn_cls = rn_cls;
1164 GNUNET_CLIENT_notify_transmit_ready (client, msize, timeout, auto_retry,
1165 &transmit_for_response, tc))
1169 return GNUNET_SYSERR;
1177 /* end of client.c */