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 connecting to the server using UNIX domain sockets.
253 * @param service_name name of service to connect to
254 * @param cfg configuration to use
255 * @return NULL on error, connection to UNIX otherwise
257 static struct GNUNET_CONNECTION_Handle *
258 try_unixpath (const char *service_name,
259 const struct GNUNET_CONFIGURATION_Handle *cfg)
262 struct GNUNET_CONNECTION_Handle *connection;
266 if ((GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg, service_name, "UNIXPATH", &unixpath)) &&
267 (0 < strlen (unixpath)))
269 /* We have a non-NULL unixpath, need to validate it */
270 connection = GNUNET_CONNECTION_create_from_connect_to_unixpath (cfg, unixpath);
271 if (NULL != connection)
273 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connected to unixpath `%s'!\n",
275 GNUNET_free (unixpath);
279 GNUNET_free_non_null (unixpath);
286 * Try connecting to the server using UNIX domain sockets.
288 * @param service_name name of service to connect to
289 * @param cfg configuration to use
290 * @return GNUNET_OK if the configuration is valid, GNUNET_SYSERR if not
293 test_service_configuration (const char *service_name,
294 const struct GNUNET_CONFIGURATION_Handle *cfg)
296 int ret = GNUNET_SYSERR;
297 char *hostname = NULL;
298 unsigned long long port;
300 char *unixpath = NULL;
302 if ((GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg, service_name, "UNIXPATH", &unixpath)) &&
303 (0 < strlen (unixpath)))
305 GNUNET_free_non_null (unixpath);
309 GNUNET_CONFIGURATION_have_value (cfg, service_name, "PORT")) &&
311 GNUNET_CONFIGURATION_get_value_number (cfg, service_name, "PORT", &port)) &&
312 (port <= 65535) && (0 != port) &&
314 GNUNET_CONFIGURATION_get_value_string (cfg, service_name, "HOSTNAME",
316 (0 != strlen (hostname)) )
318 GNUNET_free_non_null (hostname);
324 * Try to connect to the service.
326 * @param service_name name of service to connect to
327 * @param cfg configuration to use
328 * @param attempt counter used to alternate between IP and UNIX domain sockets
329 * @return NULL on error
331 static struct GNUNET_CONNECTION_Handle *
332 do_connect (const char *service_name,
333 const struct GNUNET_CONFIGURATION_Handle *cfg, unsigned int attempt)
335 struct GNUNET_CONNECTION_Handle *connection;
337 unsigned long long port;
340 if (0 == (attempt % 2))
342 /* on even rounds, try UNIX first */
343 connection = try_unixpath (service_name, cfg);
344 if (NULL != connection)
348 GNUNET_CONFIGURATION_have_value (cfg, service_name, "PORT"))
351 GNUNET_CONFIGURATION_get_value_number (cfg, service_name, "PORT", &port))
354 GNUNET_CONFIGURATION_get_value_string (cfg, service_name, "HOSTNAME",
357 LOG (GNUNET_ERROR_TYPE_WARNING,
359 ("Could not determine valid hostname and port for service `%s' from configuration.\n"),
363 if (0 == strlen (hostname))
365 GNUNET_free (hostname);
366 LOG (GNUNET_ERROR_TYPE_WARNING,
367 _("Need a non-empty hostname for service `%s'.\n"), service_name);
373 /* unspecified means 0 (disabled) */
379 /* if port is 0, try UNIX */
380 connection = try_unixpath (service_name, cfg);
381 if (NULL != connection)
383 LOG (GNUNET_ERROR_TYPE_DEBUG,
384 "Port is 0 for service `%s', UNIXPATH did not work, returning NULL!\n",
386 GNUNET_free_non_null (hostname);
389 connection = GNUNET_CONNECTION_create_from_connect (cfg, hostname, port);
390 GNUNET_free (hostname);
396 * Get a connection with a service.
398 * @param service_name name of the service
399 * @param cfg configuration to use
400 * @return NULL on error (service unknown to configuration)
402 struct GNUNET_CLIENT_Connection *
403 GNUNET_CLIENT_connect (const char *service_name,
404 const struct GNUNET_CONFIGURATION_Handle *cfg)
406 struct GNUNET_CLIENT_Connection *client;
407 struct GNUNET_CONNECTION_Handle *connection;
410 test_service_configuration (service_name,
413 connection = do_connect (service_name, cfg, 0);
414 client = GNUNET_malloc (sizeof (struct GNUNET_CLIENT_Connection));
415 client->attempts = 1;
416 client->connection = connection;
417 client->service_name = GNUNET_strdup (service_name);
419 client->back_off = GNUNET_TIME_UNIT_MILLISECONDS;
425 * Destroy connection with the service. This will automatically
426 * cancel any pending "receive" request (however, the handler will
427 * *NOT* be called, not even with a NULL message). Any pending
428 * transmission request will also be cancelled UNLESS the callback for
429 * the transmission request has already been called, in which case the
430 * transmission 'finish_pending_write' argument determines whether or
431 * not the write is guaranteed to complete before the socket is fully
432 * destroyed (unless, of course, there is an error with the server in
433 * which case the message may still be lost).
435 * @param client handle to the service connection
438 GNUNET_CLIENT_disconnect (struct GNUNET_CLIENT_Connection *client)
440 if (GNUNET_YES == client->in_receive)
442 GNUNET_CONNECTION_receive_cancel (client->connection);
443 client->in_receive = GNUNET_NO;
445 if (NULL != client->th)
447 GNUNET_CLIENT_notify_transmit_ready_cancel (client->th);
450 if (NULL != client->connection)
452 GNUNET_CONNECTION_destroy (client->connection);
453 client->connection = NULL;
455 if (GNUNET_SCHEDULER_NO_TASK != client->receive_task)
457 GNUNET_SCHEDULER_cancel (client->receive_task);
458 client->receive_task = GNUNET_SCHEDULER_NO_TASK;
460 if (NULL != client->tag)
462 GNUNET_free (client->tag);
465 client->receiver_handler = NULL;
466 GNUNET_array_grow (client->received_buf, client->received_size, 0);
467 GNUNET_free (client->service_name);
468 GNUNET_free (client);
473 * Check if message is complete. Sets the "msg_complete" member
474 * in the client struct.
476 * @param client connection with the buffer to check
479 check_complete (struct GNUNET_CLIENT_Connection *client)
481 if ((client->received_pos >= sizeof (struct GNUNET_MessageHeader)) &&
482 (client->received_pos >=
483 ntohs (((const struct GNUNET_MessageHeader *) client->received_buf)->
485 client->msg_complete = GNUNET_YES;
490 * Callback function for data received from the network. Note that
491 * both "available" and "errCode" would be 0 if the read simply timed out.
494 * @param buf pointer to received data
495 * @param available number of bytes availabe in "buf",
496 * possibly 0 (on errors)
497 * @param addr address of the sender
498 * @param addrlen size of addr
499 * @param errCode value of errno (on errors receiving)
502 receive_helper (void *cls, const void *buf, size_t available,
503 const struct sockaddr *addr, socklen_t addrlen, int errCode)
505 struct GNUNET_CLIENT_Connection *client = cls;
506 struct GNUNET_TIME_Relative remaining;
507 GNUNET_CLIENT_MessageHandler receive_handler;
508 void *receive_handler_cls;
510 GNUNET_assert (GNUNET_NO == client->msg_complete);
511 GNUNET_assert (GNUNET_YES == client->in_receive);
512 client->in_receive = GNUNET_NO;
513 if ((0 == available) || (NULL == client->connection) || (0 != errCode))
515 /* signal timeout! */
516 LOG (GNUNET_ERROR_TYPE_DEBUG,
517 "Timeout in receive_helper, available %u, client->connection %s, errCode `%s'\n",
518 (unsigned int) available, NULL == client->connection ? "NULL" : "non-NULL",
520 if (NULL != (receive_handler = client->receiver_handler))
522 receive_handler_cls = client->receiver_handler_cls;
523 client->receiver_handler = NULL;
524 receive_handler (receive_handler_cls, NULL);
528 /* FIXME: optimize for common fast case where buf contains the
529 * entire message and we need no copying... */
531 /* slow path: append to array */
532 if (client->received_size < client->received_pos + available)
533 GNUNET_array_grow (client->received_buf, client->received_size,
534 client->received_pos + available);
535 memcpy (&client->received_buf[client->received_pos], buf, available);
536 client->received_pos += available;
537 check_complete (client);
538 /* check for timeout */
539 remaining = GNUNET_TIME_absolute_get_remaining (client->receive_timeout);
540 if (0 == remaining.rel_value)
542 /* signal timeout! */
543 if (NULL != client->receiver_handler)
544 client->receiver_handler (client->receiver_handler_cls, NULL);
547 /* back to receive -- either for more data or to call callback! */
548 GNUNET_CLIENT_receive (client, client->receiver_handler,
549 client->receiver_handler_cls, remaining);
554 * Continuation to call the receive callback.
556 * @param cls our handle to the client connection
557 * @param tc scheduler context
560 receive_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
562 struct GNUNET_CLIENT_Connection *client = cls;
563 GNUNET_CLIENT_MessageHandler handler = client->receiver_handler;
564 const struct GNUNET_MessageHeader *cmsg =
565 (const struct GNUNET_MessageHeader *) client->received_buf;
566 void *handler_cls = client->receiver_handler_cls;
567 uint16_t msize = ntohs (cmsg->size);
569 struct GNUNET_MessageHeader *msg = (struct GNUNET_MessageHeader *) mbuf;
571 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received message of type %u and size %u\n",
572 ntohs (cmsg->type), msize);
573 client->receive_task = GNUNET_SCHEDULER_NO_TASK;
574 GNUNET_assert (GNUNET_YES == client->msg_complete);
575 GNUNET_assert (client->received_pos >= msize);
576 memcpy (msg, cmsg, msize);
577 memmove (client->received_buf, &client->received_buf[msize],
578 client->received_pos - msize);
579 client->received_pos -= msize;
580 client->msg_complete = GNUNET_NO;
581 client->receiver_handler = NULL;
582 check_complete (client);
584 handler (handler_cls, msg);
589 * Read from the service.
591 * @param client the service
592 * @param handler function to call with the message
593 * @param handler_cls closure for handler
594 * @param timeout how long to wait until timing out
597 GNUNET_CLIENT_receive (struct GNUNET_CLIENT_Connection *client,
598 GNUNET_CLIENT_MessageHandler handler, void *handler_cls,
599 struct GNUNET_TIME_Relative timeout)
601 if (NULL == client->connection)
603 /* already disconnected, fail instantly! */
604 GNUNET_break (0); /* this should not happen in well-written code! */
606 handler (handler_cls, NULL);
609 client->receiver_handler = handler;
610 client->receiver_handler_cls = handler_cls;
611 client->receive_timeout = GNUNET_TIME_relative_to_absolute (timeout);
612 if (GNUNET_YES == client->msg_complete)
614 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == client->receive_task);
615 client->receive_task = GNUNET_SCHEDULER_add_now (&receive_task, client);
619 LOG (GNUNET_ERROR_TYPE_DEBUG, "calling GNUNET_CONNECTION_receive\n");
620 GNUNET_assert (GNUNET_NO == client->in_receive);
621 client->in_receive = GNUNET_YES;
622 GNUNET_CONNECTION_receive (client->connection, GNUNET_SERVER_MAX_MESSAGE_SIZE - 1,
623 timeout, &receive_helper, client);
629 * Report service unavailable.
632 service_test_error (GNUNET_SCHEDULER_Task task, void *task_cls)
634 GNUNET_SCHEDULER_add_continuation (task, task_cls,
635 GNUNET_SCHEDULER_REASON_TIMEOUT);
640 * Receive confirmation from test, service is up.
643 * @param msg message received, NULL on timeout or fatal error
646 confirm_handler (void *cls, const struct GNUNET_MessageHeader *msg)
648 struct GNUNET_CLIENT_Connection *client = cls;
650 /* We may want to consider looking at the reply in more
651 * detail in the future, for example, is this the
652 * correct service? FIXME! */
655 LOG (GNUNET_ERROR_TYPE_DEBUG,
656 "Received confirmation that service is running.\n");
657 GNUNET_SCHEDULER_add_continuation (client->test_cb, client->test_cb_cls,
658 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
662 service_test_error (client->test_cb, client->test_cb_cls);
664 GNUNET_CLIENT_disconnect (client);
669 * Send the 'TEST' message to the service. If successful, prepare to
672 * @param cls the 'struct GNUNET_CLIENT_Connection' of the connection to test
673 * @param size number of bytes available in buf
674 * @param buf where to write the message
675 * @return number of bytes written to buf
678 write_test (void *cls, size_t size, void *buf)
680 struct GNUNET_CLIENT_Connection *client = cls;
681 struct GNUNET_MessageHeader *msg;
683 if (size < sizeof (struct GNUNET_MessageHeader))
685 LOG (GNUNET_ERROR_TYPE_DEBUG, _("Failure to transmit TEST request.\n"));
686 service_test_error (client->test_cb, client->test_cb_cls);
687 GNUNET_CLIENT_disconnect (client);
688 return 0; /* client disconnected */
690 LOG (GNUNET_ERROR_TYPE_DEBUG, "Transmitting `%s' request.\n", "TEST");
691 msg = (struct GNUNET_MessageHeader *) buf;
692 msg->type = htons (GNUNET_MESSAGE_TYPE_TEST);
693 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
694 GNUNET_CLIENT_receive (client, &confirm_handler, client,
695 GNUNET_TIME_absolute_get_remaining
696 (client->test_deadline));
697 return sizeof (struct GNUNET_MessageHeader);
702 * Test if the service is running. If we are given a UNIXPATH or a local address,
703 * we do this NOT by trying to connect to the service, but by trying to BIND to
704 * the same port. If the BIND fails, we know the service is running.
706 * @param service name of the service to wait for
707 * @param cfg configuration to use
708 * @param timeout how long to wait at most
709 * @param task task to run if service is running
710 * (reason will be "PREREQ_DONE" (service running)
711 * or "TIMEOUT" (service not known to be running))
712 * @param task_cls closure for task
715 GNUNET_CLIENT_service_test (const char *service,
716 const struct GNUNET_CONFIGURATION_Handle *cfg,
717 struct GNUNET_TIME_Relative timeout,
718 GNUNET_SCHEDULER_Task task, void *task_cls)
721 unsigned long long port;
722 struct GNUNET_NETWORK_Handle *sock;
723 struct GNUNET_CLIENT_Connection *client;
725 LOG (GNUNET_ERROR_TYPE_DEBUG, "Testing if service `%s' is running.\n",
729 /* probe UNIX support */
730 struct sockaddr_un s_un;
735 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? */
737 if (strlen (unixpath) >= sizeof (s_un.sun_path))
739 LOG (GNUNET_ERROR_TYPE_WARNING,
740 _("UNIXPATH `%s' too long, maximum length is %llu\n"), unixpath,
741 sizeof (s_un.sun_path));
745 sock = GNUNET_NETWORK_socket_create (PF_UNIX, SOCK_STREAM, 0);
748 memset (&s_un, 0, sizeof (s_un));
749 s_un.sun_family = AF_UNIX;
750 slen = strlen (unixpath) + 1;
751 if (slen >= sizeof (s_un.sun_path))
752 slen = sizeof (s_un.sun_path) - 1;
753 memcpy (s_un.sun_path, unixpath, slen);
754 s_un.sun_path[slen] = '\0';
755 slen = sizeof (struct sockaddr_un);
757 s_un.sun_path[0] = '\0';
759 #if HAVE_SOCKADDR_IN_SIN_LEN
760 s_un.sun_len = (u_char) slen;
763 GNUNET_NETWORK_socket_bind (sock, (const struct sockaddr *) &s_un,
766 /* failed to bind => service must be running */
767 GNUNET_free (unixpath);
768 (void) GNUNET_NETWORK_socket_close (sock);
769 GNUNET_SCHEDULER_add_continuation (task, task_cls,
770 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
773 (void) GNUNET_NETWORK_socket_close (sock);
778 GNUNET_free_non_null (unixpath);
784 GNUNET_CONFIGURATION_get_value_number (cfg, service, "PORT", &port)) ||
787 GNUNET_CONFIGURATION_get_value_string (cfg, service, "HOSTNAME",
790 /* UNIXPATH failed (if possible) AND IP failed => error */
791 service_test_error (task, task_cls);
795 if (0 == strcmp ("localhost", hostname)
801 /* can test using 'bind' */
802 struct sockaddr_in s_in;
804 memset (&s_in, 0, sizeof (s_in));
805 #if HAVE_SOCKADDR_IN_SIN_LEN
806 s_in.sin_len = sizeof (struct sockaddr_in);
808 s_in.sin_family = AF_INET;
809 s_in.sin_port = htons (port);
811 sock = GNUNET_NETWORK_socket_create (AF_INET, SOCK_STREAM, 0);
815 GNUNET_NETWORK_socket_bind (sock, (const struct sockaddr *) &s_in,
818 /* failed to bind => service must be running */
819 GNUNET_free (hostname);
820 (void) GNUNET_NETWORK_socket_close (sock);
821 GNUNET_SCHEDULER_add_continuation (task, task_cls,
822 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
825 (void) GNUNET_NETWORK_socket_close (sock);
829 if (0 == strcmp ("ip6-localhost", hostname)
835 /* can test using 'bind' */
836 struct sockaddr_in6 s_in6;
838 memset (&s_in6, 0, sizeof (s_in6));
839 #if HAVE_SOCKADDR_IN_SIN_LEN
840 s_in6.sin6_len = sizeof (struct sockaddr_in6);
842 s_in6.sin6_family = AF_INET6;
843 s_in6.sin6_port = htons (port);
845 sock = GNUNET_NETWORK_socket_create (AF_INET6, SOCK_STREAM, 0);
849 GNUNET_NETWORK_socket_bind (sock, (const struct sockaddr *) &s_in6,
852 /* failed to bind => service must be running */
853 GNUNET_free (hostname);
854 (void) GNUNET_NETWORK_socket_close (sock);
855 GNUNET_SCHEDULER_add_continuation (task, task_cls,
856 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
859 (void) GNUNET_NETWORK_socket_close (sock);
863 if (((0 == strcmp ("localhost", hostname)) ||
864 (0 == strcmp ("ip6-localhost", hostname)))
870 /* all binds succeeded => claim service not running right now */
871 GNUNET_free_non_null (hostname);
872 service_test_error (task, task_cls);
875 GNUNET_free_non_null (hostname);
877 /* non-localhost, try 'connect' method */
878 client = GNUNET_CLIENT_connect (service, cfg);
881 LOG (GNUNET_ERROR_TYPE_INFO,
882 _("Could not connect to service `%s', must not be running.\n"),
884 service_test_error (task, task_cls);
887 client->test_cb = task;
888 client->test_cb_cls = task_cls;
889 client->test_deadline = GNUNET_TIME_relative_to_absolute (timeout);
890 if (NULL == GNUNET_CLIENT_notify_transmit_ready (client,
891 sizeof (struct GNUNET_MessageHeader),
892 timeout, GNUNET_YES, &write_test,
895 LOG (GNUNET_ERROR_TYPE_WARNING,
896 _("Failure to transmit request to service `%s'\n"), service);
897 service_test_error (task, task_cls);
898 GNUNET_CLIENT_disconnect (client);
905 * Connection notifies us about failure or success of
906 * a transmission request. Either pass it on to our
907 * user or, if possible, retry.
909 * @param cls our "struct GNUNET_CLIENT_TransmissionHandle"
910 * @param size number of bytes available for transmission
911 * @param buf where to write them
912 * @return number of bytes written to buf
915 client_notify (void *cls, size_t size, void *buf);
919 * This task is run if we should re-try connection to the
920 * service after a while.
922 * @param cls our "struct GNUNET_CLIENT_TransmitHandle" of the request
926 client_delayed_retry (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
928 struct GNUNET_CLIENT_TransmitHandle *th = cls;
929 struct GNUNET_TIME_Relative delay;
931 th->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
932 th->client->connection =
933 do_connect (th->client->service_name, th->client->cfg, th->client->attempts++);
934 if (NULL == th->client->connection)
936 /* could happen if we're out of sockets */
938 GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining
939 (th->timeout), th->client->back_off);
940 th->client->back_off =
941 GNUNET_TIME_relative_min (GNUNET_TIME_relative_multiply
942 (th->client->back_off, 2),
943 GNUNET_TIME_UNIT_SECONDS);
944 LOG (GNUNET_ERROR_TYPE_DEBUG,
945 "Transmission failed %u times, trying again in %llums.\n",
946 MAX_ATTEMPTS - th->attempts_left,
947 (unsigned long long) delay.rel_value);
949 GNUNET_SCHEDULER_add_delayed (delay, &client_delayed_retry, th);
953 GNUNET_CONNECTION_notify_transmit_ready (th->client->connection, th->size,
954 GNUNET_TIME_absolute_get_remaining
955 (th->timeout), &client_notify,
960 th->client->th = NULL;
961 th->notify (th->notify_cls, 0, NULL);
969 * Connection notifies us about failure or success of a transmission
970 * request. Either pass it on to our user or, if possible, retry.
972 * @param cls our "struct GNUNET_CLIENT_TransmissionHandle"
973 * @param size number of bytes available for transmission
974 * @param buf where to write them
975 * @return number of bytes written to buf
978 client_notify (void *cls, size_t size, void *buf)
980 struct GNUNET_CLIENT_TransmitHandle *th = cls;
981 struct GNUNET_CLIENT_Connection *client = th->client;
983 struct GNUNET_TIME_Relative delay;
989 delay = GNUNET_TIME_absolute_get_remaining (th->timeout);
990 delay.rel_value /= 2;
991 if ((GNUNET_YES != th->auto_retry) || (0 == --th->attempts_left) ||
992 (delay.rel_value < 1))
994 LOG (GNUNET_ERROR_TYPE_DEBUG,
995 "Transmission failed %u times, giving up.\n",
996 MAX_ATTEMPTS - th->attempts_left);
997 GNUNET_break (0 == th->notify (th->notify_cls, 0, NULL));
1002 LOG (GNUNET_ERROR_TYPE_DEBUG,
1003 "Failed to connect to `%s', automatically trying again.\n",
1004 client->service_name);
1005 if (GNUNET_YES == client->in_receive)
1007 GNUNET_CONNECTION_receive_cancel (client->connection);
1008 client->in_receive = GNUNET_NO;
1010 GNUNET_CONNECTION_destroy (client->connection);
1011 client->connection = NULL;
1012 delay = GNUNET_TIME_relative_min (delay, client->back_off);
1014 GNUNET_TIME_relative_min (GNUNET_TIME_relative_multiply
1015 (client->back_off, 2),
1016 GNUNET_TIME_UNIT_SECONDS);
1017 LOG (GNUNET_ERROR_TYPE_DEBUG,
1018 "Transmission failed %u times, trying again in %llums.\n",
1019 MAX_ATTEMPTS - th->attempts_left,
1020 (unsigned long long) delay.rel_value);
1022 th->reconnect_task =
1023 GNUNET_SCHEDULER_add_delayed (delay, &client_delayed_retry, th);
1026 GNUNET_assert (size >= th->size);
1027 ret = th->notify (th->notify_cls, size, buf);
1034 * Ask the client to call us once the specified number of bytes
1035 * are free in the transmission buffer. May call the notify
1036 * method immediately if enough space is available.
1038 * @param client connection to the service
1039 * @param size number of bytes to send
1040 * @param timeout after how long should we give up (and call
1041 * notify with buf NULL and size 0)?
1042 * @param auto_retry if the connection to the service dies, should we
1043 * automatically re-connect and retry (within the timeout period)
1044 * or should we immediately fail in this case? Pass GNUNET_YES
1045 * if the caller does not care about temporary connection errors,
1046 * for example because the protocol is stateless
1047 * @param notify function to call
1048 * @param notify_cls closure for notify
1049 * @return NULL if our buffer will never hold size bytes,
1050 * a handle if the notify callback was queued (can be used to cancel)
1052 struct GNUNET_CLIENT_TransmitHandle *
1053 GNUNET_CLIENT_notify_transmit_ready (struct GNUNET_CLIENT_Connection *client,
1055 struct GNUNET_TIME_Relative timeout,
1057 GNUNET_CONNECTION_TransmitReadyNotify
1058 notify, void *notify_cls)
1060 struct GNUNET_CLIENT_TransmitHandle *th;
1062 if (NULL != client->th)
1064 /* If this breaks, you most likley called this function twice without waiting
1065 * for completion or canceling the request */
1069 th = GNUNET_malloc (sizeof (struct GNUNET_CLIENT_TransmitHandle));
1070 th->client = client;
1072 th->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1073 th->auto_retry = auto_retry;
1074 th->notify = notify;
1075 th->notify_cls = notify_cls;
1076 th->attempts_left = MAX_ATTEMPTS;
1078 if (NULL == client->connection)
1080 th->reconnect_task =
1081 GNUNET_SCHEDULER_add_delayed (client->back_off, &client_delayed_retry,
1088 GNUNET_CONNECTION_notify_transmit_ready (client->connection, size, timeout,
1089 &client_notify, th);
1103 * Cancel a request for notification.
1105 * @param th handle from the original request.
1108 GNUNET_CLIENT_notify_transmit_ready_cancel (struct GNUNET_CLIENT_TransmitHandle
1111 if (GNUNET_SCHEDULER_NO_TASK != th->reconnect_task)
1113 GNUNET_assert (NULL == th->th);
1114 GNUNET_SCHEDULER_cancel (th->reconnect_task);
1115 th->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1119 GNUNET_assert (NULL != th->th);
1120 GNUNET_CONNECTION_notify_transmit_ready_cancel (th->th);
1122 th->client->th = NULL;
1128 * Function called to notify a client about the socket
1129 * begin ready to queue the message. "buf" will be
1130 * NULL and "size" zero if the socket was closed for
1131 * writing in the meantime.
1133 * @param cls closure of type "struct TransmitGetResponseContext*"
1134 * @param size number of bytes available in buf
1135 * @param buf where the callee should write the message
1136 * @return number of bytes written to buf
1139 transmit_for_response (void *cls, size_t size, void *buf)
1141 struct TransmitGetResponseContext *tc = cls;
1144 tc->client->tag = NULL;
1145 msize = ntohs (tc->hdr->size);
1148 LOG (GNUNET_ERROR_TYPE_DEBUG,
1149 _("Could not submit request, not expecting to receive a response.\n"));
1151 tc->rn (tc->rn_cls, NULL);
1155 GNUNET_assert (size >= msize);
1156 memcpy (buf, tc->hdr, msize);
1157 GNUNET_CLIENT_receive (tc->client, tc->rn, tc->rn_cls,
1158 GNUNET_TIME_absolute_get_remaining (tc->timeout));
1165 * Convenience API that combines sending a request
1166 * to the service and waiting for a response.
1167 * If either operation times out, the callback
1168 * will be called with a "NULL" response (in which
1169 * case the connection should probably be destroyed).
1171 * @param client connection to use
1172 * @param hdr message to transmit
1173 * @param timeout when to give up (for both transmission
1174 * and for waiting for a response)
1175 * @param auto_retry if the connection to the service dies, should we
1176 * automatically re-connect and retry (within the timeout period)
1177 * or should we immediately fail in this case? Pass GNUNET_YES
1178 * if the caller does not care about temporary connection errors,
1179 * for example because the protocol is stateless
1180 * @param rn function to call with the response
1181 * @param rn_cls closure for rn
1182 * @return GNUNET_OK on success, GNUNET_SYSERR if a request
1183 * is already pending
1186 GNUNET_CLIENT_transmit_and_get_response (struct GNUNET_CLIENT_Connection *client,
1187 const struct GNUNET_MessageHeader *hdr,
1188 struct GNUNET_TIME_Relative timeout,
1190 GNUNET_CLIENT_MessageHandler rn,
1193 struct TransmitGetResponseContext *tc;
1196 if (NULL != client->th)
1197 return GNUNET_SYSERR;
1198 GNUNET_assert (NULL == client->tag);
1199 msize = ntohs (hdr->size);
1200 tc = GNUNET_malloc (sizeof (struct TransmitGetResponseContext) + msize);
1201 tc->client = client;
1202 tc->hdr = (const struct GNUNET_MessageHeader *) &tc[1];
1203 memcpy (&tc[1], hdr, msize);
1204 tc->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1206 tc->rn_cls = rn_cls;
1208 GNUNET_CLIENT_notify_transmit_ready (client, msize, timeout, auto_retry,
1209 &transmit_for_response, tc))
1213 return GNUNET_SYSERR;
1221 /* end of client.c */