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_NO
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,
302 GNUNET_CONFIGURATION_get_value_string (cfg,
304 "HOSTNAME", &hostname)))
306 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
308 ("Could not determine valid hostname and port for service `%s' from configuration.\n"),
312 if (0 == strlen (hostname))
314 GNUNET_free (hostname);
315 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
316 _("Need a non-empty hostname for service `%s'.\n"),
323 if (0 != (attempt % 2))
328 GNUNET_CONFIGURATION_get_value_string (cfg,
330 "UNIXPATH", &unixpath)) &&
331 (0 < strlen (unixpath)))
333 sock = GNUNET_CONNECTION_create_from_connect_to_unixpath (cfg,
337 GNUNET_free (unixpath);
338 GNUNET_free (hostname);
342 GNUNET_free_non_null (unixpath);
346 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
347 "Port is 0 for service `%s', UNIXPATH did not work, returning NULL!\n",
350 GNUNET_free (hostname);
354 sock = GNUNET_CONNECTION_create_from_connect (cfg, hostname, port);
355 GNUNET_free (hostname);
361 * Get a connection with a service.
363 * @param service_name name of the service
364 * @param cfg configuration to use
365 * @return NULL on error (service unknown to configuration)
367 struct GNUNET_CLIENT_Connection *
368 GNUNET_CLIENT_connect (const char *service_name,
369 const struct GNUNET_CONFIGURATION_Handle *cfg)
371 struct GNUNET_CLIENT_Connection *ret;
372 struct GNUNET_CONNECTION_Handle *sock;
374 sock = do_connect (service_name, cfg, 0);
375 ret = GNUNET_malloc (sizeof (struct GNUNET_CLIENT_Connection));
378 ret->service_name = GNUNET_strdup (service_name);
380 ret->back_off = GNUNET_TIME_UNIT_MILLISECONDS;
386 * Configure this connection to ignore shutdown signals.
388 * @param h client handle
389 * @param do_ignore GNUNET_YES to ignore, GNUNET_NO to restore default
392 GNUNET_CLIENT_ignore_shutdown (struct GNUNET_CLIENT_Connection *h,
395 h->ignore_shutdown = do_ignore;
397 GNUNET_CONNECTION_ignore_shutdown (h->sock, do_ignore);
402 * Destroy connection with the service. This will automatically
403 * cancel any pending "receive" request (however, the handler will
404 * *NOT* be called, not even with a NULL message). Any pending
405 * transmission request will also be cancelled UNLESS the callback for
406 * the transmission request has already been called, in which case the
407 * transmission 'finish_pending_write' argument determines whether or
408 * not the write is guaranteed to complete before the socket is fully
409 * destroyed (unless, of course, there is an error with the server in
410 * which case the message may still be lost).
412 * @param finish_pending_write should a transmission already passed to the
413 * handle be completed?
414 * @param sock handle to the service connection
417 GNUNET_CLIENT_disconnect (struct GNUNET_CLIENT_Connection *sock,
418 int finish_pending_write)
420 if (sock->in_receive == GNUNET_YES)
422 GNUNET_CONNECTION_receive_cancel (sock->sock);
423 sock->in_receive = GNUNET_NO;
425 if (sock->th != NULL)
427 GNUNET_CLIENT_notify_transmit_ready_cancel (sock->th);
430 if (NULL != sock->sock)
432 GNUNET_CONNECTION_destroy (sock->sock, finish_pending_write);
435 if (sock->receive_task != GNUNET_SCHEDULER_NO_TASK)
437 GNUNET_SCHEDULER_cancel (sock->receive_task);
438 sock->receive_task = GNUNET_SCHEDULER_NO_TASK;
440 if (sock->tag != NULL)
442 GNUNET_free (sock->tag);
445 sock->receiver_handler = NULL;
446 GNUNET_array_grow (sock->received_buf, sock->received_size, 0);
447 GNUNET_free (sock->service_name);
453 * Check if message is complete
456 check_complete (struct GNUNET_CLIENT_Connection *conn)
458 if ((conn->received_pos >= sizeof (struct GNUNET_MessageHeader)) &&
459 (conn->received_pos >=
460 ntohs (((const struct GNUNET_MessageHeader *) conn->received_buf)->
462 conn->msg_complete = GNUNET_YES;
467 * Callback function for data received from the network. Note that
468 * both "available" and "errCode" would be 0 if the read simply timed out.
471 * @param buf pointer to received data
472 * @param available number of bytes availabe in "buf",
473 * possibly 0 (on errors)
474 * @param addr address of the sender
475 * @param addrlen size of addr
476 * @param errCode value of errno (on errors receiving)
479 receive_helper (void *cls,
482 const struct sockaddr *addr, socklen_t addrlen, int errCode)
484 struct GNUNET_CLIENT_Connection *conn = cls;
485 struct GNUNET_TIME_Relative remaining;
486 GNUNET_CLIENT_MessageHandler receive_handler;
487 void *receive_handler_cls;
489 GNUNET_assert (conn->msg_complete == GNUNET_NO);
490 conn->in_receive = GNUNET_NO;
491 if ((available == 0) || (conn->sock == NULL) || (errCode != 0))
493 /* signal timeout! */
495 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
496 "Timeout in receive_helper, available %u, conn->sock %s, errCode `%s'\n",
497 (unsigned int) available,
498 conn->sock == NULL ? "NULL" : "non-NULL", STRERROR (errCode));
500 if (NULL != (receive_handler = conn->receiver_handler))
502 receive_handler_cls = conn->receiver_handler_cls;
503 conn->receiver_handler = NULL;
504 receive_handler (receive_handler_cls, NULL);
509 /* FIXME: optimize for common fast case where buf contains the
510 * entire message and we need no copying... */
513 /* slow path: append to array */
514 if (conn->received_size < conn->received_pos + available)
515 GNUNET_array_grow (conn->received_buf,
516 conn->received_size, conn->received_pos + available);
517 memcpy (&conn->received_buf[conn->received_pos], buf, available);
518 conn->received_pos += available;
519 check_complete (conn);
520 /* check for timeout */
521 remaining = GNUNET_TIME_absolute_get_remaining (conn->receive_timeout);
522 if (remaining.rel_value == 0)
524 /* signal timeout! */
525 if (NULL != conn->receiver_handler)
526 conn->receiver_handler (conn->receiver_handler_cls, NULL);
529 /* back to receive -- either for more data or to call callback! */
530 GNUNET_CLIENT_receive (conn,
531 conn->receiver_handler,
532 conn->receiver_handler_cls, remaining);
537 * Continuation to call the receive callback.
539 * @param cls our handle to the client connection
540 * @param tc scheduler context
543 receive_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
545 struct GNUNET_CLIENT_Connection *sock = cls;
546 GNUNET_CLIENT_MessageHandler handler = sock->receiver_handler;
547 const struct GNUNET_MessageHeader *cmsg =
548 (const struct GNUNET_MessageHeader *) sock->received_buf;
549 void *handler_cls = sock->receiver_handler_cls;
550 uint16_t msize = ntohs (cmsg->size);
552 struct GNUNET_MessageHeader *msg = (struct GNUNET_MessageHeader *) mbuf;
555 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
556 "Received message of type %u and size %u\n",
557 ntohs (cmsg->type), msize);
559 sock->receive_task = GNUNET_SCHEDULER_NO_TASK;
560 GNUNET_assert (GNUNET_YES == sock->msg_complete);
561 GNUNET_assert (sock->received_pos >= msize);
562 memcpy (msg, cmsg, msize);
563 memmove (sock->received_buf,
564 &sock->received_buf[msize], sock->received_pos - msize);
565 sock->received_pos -= msize;
566 sock->msg_complete = GNUNET_NO;
567 sock->receiver_handler = NULL;
568 check_complete (sock);
570 handler (handler_cls, msg);
575 * Read from the service.
577 * @param sock the service
578 * @param handler function to call with the message
579 * @param handler_cls closure for handler
580 * @param timeout how long to wait until timing out
583 GNUNET_CLIENT_receive (struct GNUNET_CLIENT_Connection *sock,
584 GNUNET_CLIENT_MessageHandler handler,
585 void *handler_cls, struct GNUNET_TIME_Relative timeout)
587 if (sock->sock == NULL)
589 /* already disconnected, fail instantly! */
590 GNUNET_break (0); /* this should not happen in well-written code! */
592 handler (handler_cls, NULL);
595 sock->receiver_handler = handler;
596 sock->receiver_handler_cls = handler_cls;
597 sock->receive_timeout = GNUNET_TIME_relative_to_absolute (timeout);
598 if (GNUNET_YES == sock->msg_complete)
600 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->receive_task);
601 sock->receive_task = GNUNET_SCHEDULER_add_now (&receive_task, sock);
605 GNUNET_assert (sock->in_receive == GNUNET_NO);
606 sock->in_receive = GNUNET_YES;
608 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "calling GNUNET_CONNECTION_receive\n");
610 GNUNET_CONNECTION_receive (sock->sock,
611 GNUNET_SERVER_MAX_MESSAGE_SIZE - 1,
612 timeout, &receive_helper, sock);
618 * Report service unavailable.
621 service_test_error (GNUNET_SCHEDULER_Task task, void *task_cls)
623 GNUNET_SCHEDULER_add_continuation (task,
624 task_cls, GNUNET_SCHEDULER_REASON_TIMEOUT);
629 * Receive confirmation from test, service is up.
632 * @param msg message received, NULL on timeout or fatal error
635 confirm_handler (void *cls, const struct GNUNET_MessageHeader *msg)
637 struct GNUNET_CLIENT_Connection *conn = cls;
639 /* We may want to consider looking at the reply in more
640 * detail in the future, for example, is this the
641 * correct service? FIXME! */
645 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
646 "Received confirmation that service is running.\n");
648 GNUNET_SCHEDULER_add_continuation (conn->test_cb,
650 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
654 service_test_error (conn->test_cb, conn->test_cb_cls);
656 GNUNET_CLIENT_disconnect (conn, GNUNET_NO);
661 * Send the 'TEST' message to the service. If successful, prepare to
664 * @param cls the 'struct GNUNET_CLIENT_Connection' of the connection to test
665 * @param size number of bytes available in buf
666 * @param buf where to write the message
667 * @return number of bytes written to buf
670 write_test (void *cls, size_t size, void *buf)
672 struct GNUNET_CLIENT_Connection *conn = cls;
673 struct GNUNET_MessageHeader *msg;
675 if (size < sizeof (struct GNUNET_MessageHeader))
678 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
679 _("Failure to transmit TEST request.\n"));
681 service_test_error (conn->test_cb, conn->test_cb_cls);
682 GNUNET_CLIENT_disconnect (conn, GNUNET_NO);
683 return 0; /* client disconnected */
686 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transmitting `%s' request.\n", "TEST");
688 msg = (struct GNUNET_MessageHeader *) buf;
689 msg->type = htons (GNUNET_MESSAGE_TYPE_TEST);
690 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
691 GNUNET_CLIENT_receive (conn,
694 GNUNET_TIME_absolute_get_remaining
695 (conn->test_deadline));
696 return sizeof (struct GNUNET_MessageHeader);
701 * Test if the service is running. If we are given a UNIXPATH or a local address,
702 * we do this NOT by trying to connect to the service, but by trying to BIND to
703 * the same port. If the BIND fails, we know the service is running.
705 * @param service name of the service to wait for
706 * @param cfg configuration to use
707 * @param timeout how long to wait at most
708 * @param task task to run if service is running
709 * (reason will be "PREREQ_DONE" (service running)
710 * or "TIMEOUT" (service not known to be running))
711 * @param task_cls closure for task
714 GNUNET_CLIENT_service_test (const char *service,
715 const struct GNUNET_CONFIGURATION_Handle *cfg,
716 struct GNUNET_TIME_Relative timeout,
717 GNUNET_SCHEDULER_Task task, void *task_cls)
720 unsigned long long port;
721 struct GNUNET_NETWORK_Handle *sock;
722 struct GNUNET_CLIENT_Connection *conn;
725 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
726 "Testing if service `%s' is running.\n", service);
730 /* probe UNIX support */
731 struct sockaddr_un s_un;
736 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? */
738 if (strlen (unixpath) >= sizeof (s_un.sun_path))
740 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
741 _("UNIXPATH `%s' too long, maximum length is %llu\n"),
742 unixpath, sizeof (s_un.sun_path));
746 sock = GNUNET_NETWORK_socket_create (PF_UNIX, SOCK_STREAM, 0);
749 memset (&s_un, 0, sizeof (s_un));
750 s_un.sun_family = AF_UNIX;
751 slen = strlen (unixpath) + 1;
752 if (slen >= sizeof (s_un.sun_path))
753 slen = sizeof (s_un.sun_path) - 1;
754 memcpy (s_un.sun_path, unixpath, slen);
755 s_un.sun_path[slen] = '\0';
756 slen = sizeof (struct sockaddr_un);
758 s_un.sun_path[0] = '\0';
760 #if HAVE_SOCKADDR_IN_SIN_LEN
761 s_un.sun_len = (u_char) slen;
764 GNUNET_NETWORK_socket_bind (sock,
765 (const struct sockaddr *) &s_un,
768 /* failed to bind => service must be running */
769 GNUNET_free (unixpath);
770 (void) GNUNET_NETWORK_socket_close (sock);
771 GNUNET_SCHEDULER_add_continuation (task,
773 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
776 (void) GNUNET_NETWORK_socket_close (sock);
781 GNUNET_free_non_null (unixpath);
787 GNUNET_CONFIGURATION_get_value_number (cfg,
793 GNUNET_CONFIGURATION_get_value_string (cfg,
794 service, "HOSTNAME", &hostname)))
796 /* UNIXPATH failed (if possible) AND IP failed => error */
797 service_test_error (task, task_cls);
801 if (0 == strcmp ("localhost", hostname)
807 /* can test using 'bind' */
808 struct sockaddr_in s_in;
810 memset (&s_in, 0, sizeof (s_in));
811 #if HAVE_SOCKADDR_IN_SIN_LEN
812 s_in.sin_len = saddrlens[1];
814 s_in.sin_family = AF_INET;
815 s_in.sin_port = htons (port);
817 sock = GNUNET_NETWORK_socket_create (AF_INET, SOCK_STREAM, 0);
821 GNUNET_NETWORK_socket_bind (sock,
822 (const struct sockaddr *) &s_in,
825 /* failed to bind => service must be running */
826 GNUNET_free (hostname);
827 (void) GNUNET_NETWORK_socket_close (sock);
828 GNUNET_SCHEDULER_add_continuation (task,
830 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
833 (void) GNUNET_NETWORK_socket_close (sock);
837 if (0 == strcmp ("ip6-localhost", hostname)
843 /* can test using 'bind' */
844 struct sockaddr_in6 s_in6;
846 memset (&s_in6, 0, sizeof (s_in6));
847 #if HAVE_SOCKADDR_IN_SIN_LEN
848 s_in6.sin6_len = saddrlens[1];
850 s_in6.sin6_family = AF_INET6;
851 s_in6.sin6_port = htons (port);
853 sock = GNUNET_NETWORK_socket_create (AF_INET6, SOCK_STREAM, 0);
857 GNUNET_NETWORK_socket_bind (sock,
858 (const struct sockaddr *) &s_in6,
861 /* failed to bind => service must be running */
862 GNUNET_free (hostname);
863 (void) GNUNET_NETWORK_socket_close (sock);
864 GNUNET_SCHEDULER_add_continuation (task,
866 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
869 (void) GNUNET_NETWORK_socket_close (sock);
873 if (((0 == strcmp ("localhost", hostname)) ||
874 (0 == strcmp ("ip6-localhost", hostname)))
880 /* all binds succeeded => claim service not running right now */
881 GNUNET_free_non_null (hostname);
882 service_test_error (task, task_cls);
885 GNUNET_free_non_null (hostname);
887 /* non-localhost, try 'connect' method */
888 conn = GNUNET_CLIENT_connect (service, cfg);
891 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
893 ("Could not connect to service `%s', must not be running.\n"),
895 service_test_error (task, task_cls);
898 conn->test_cb = task;
899 conn->test_cb_cls = task_cls;
900 conn->test_deadline = GNUNET_TIME_relative_to_absolute (timeout);
902 if (NULL == GNUNET_CLIENT_notify_transmit_ready (conn,
904 GNUNET_MessageHeader),
908 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
909 _("Failure to transmit request to service `%s'\n"), service);
910 service_test_error (task, task_cls);
911 GNUNET_CLIENT_disconnect (conn, GNUNET_NO);
918 * Connection notifies us about failure or success of
919 * a transmission request. Either pass it on to our
920 * user or, if possible, retry.
922 * @param cls our "struct GNUNET_CLIENT_TransmissionHandle"
923 * @param size number of bytes available for transmission
924 * @param buf where to write them
925 * @return number of bytes written to buf
927 static size_t client_notify (void *cls, size_t size, void *buf);
931 * This task is run if we should re-try connection to the
932 * service after a while.
934 * @param cls our "struct GNUNET_CLIENT_TransmitHandle" of the request
938 client_delayed_retry (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
940 struct GNUNET_CLIENT_TransmitHandle *th = cls;
941 struct GNUNET_TIME_Relative delay;
943 th->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
944 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
947 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
948 "Transmission failed due to shutdown.\n");
951 th->notify (th->notify_cls, 0, NULL);
955 th->sock->sock = do_connect (th->sock->service_name,
956 th->sock->cfg, th->sock->attempts++);
957 if (NULL == th->sock->sock)
959 /* could happen if we're out of sockets */
961 GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining
962 (th->timeout), th->sock->back_off);
964 GNUNET_TIME_relative_min (GNUNET_TIME_relative_multiply
965 (th->sock->back_off, 2),
966 GNUNET_TIME_UNIT_SECONDS);
968 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
969 "Transmission failed %u times, trying again in %llums.\n",
970 MAX_ATTEMPTS - th->attempts_left,
971 (unsigned long long) delay.rel_value);
973 th->reconnect_task = GNUNET_SCHEDULER_add_delayed (delay,
974 &client_delayed_retry,
978 GNUNET_CONNECTION_ignore_shutdown (th->sock->sock, th->sock->ignore_shutdown);
979 th->th = GNUNET_CONNECTION_notify_transmit_ready (th->sock->sock,
981 GNUNET_TIME_absolute_get_remaining
988 th->notify (th->notify_cls, 0, NULL);
996 * Connection notifies us about failure or success of a transmission
997 * request. Either pass it on to our user or, if possible, retry.
999 * @param cls our "struct GNUNET_CLIENT_TransmissionHandle"
1000 * @param size number of bytes available for transmission
1001 * @param buf where to write them
1002 * @return number of bytes written to buf
1005 client_notify (void *cls, size_t size, void *buf)
1007 struct GNUNET_CLIENT_TransmitHandle *th = cls;
1009 struct GNUNET_TIME_Relative delay;
1012 th->sock->th = NULL;
1015 delay = GNUNET_TIME_absolute_get_remaining (th->timeout);
1016 delay.rel_value /= 2;
1018 (GNUNET_SCHEDULER_REASON_SHUTDOWN & GNUNET_SCHEDULER_get_reason ())) ||
1019 (GNUNET_YES != th->auto_retry) || (0 == --th->attempts_left) ||
1020 (delay.rel_value < 1))
1023 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1024 "Transmission failed %u times, giving up.\n",
1025 MAX_ATTEMPTS - th->attempts_left);
1027 GNUNET_break (0 == th->notify (th->notify_cls, 0, NULL));
1033 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1034 "Failed to connect to `%s', automatically trying again.\n",
1035 th->sock->service_name);
1037 GNUNET_CONNECTION_destroy (th->sock->sock, GNUNET_NO);
1038 th->sock->sock = NULL;
1039 delay = GNUNET_TIME_relative_min (delay, th->sock->back_off);
1042 GNUNET_TIME_relative_min (GNUNET_TIME_relative_multiply
1043 (th->sock->back_off, 2),
1044 GNUNET_TIME_UNIT_SECONDS);
1046 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1047 "Transmission failed %u times, trying again in %llums.\n",
1048 MAX_ATTEMPTS - th->attempts_left,
1049 (unsigned long long) delay.rel_value);
1052 th->reconnect_task = GNUNET_SCHEDULER_add_delayed (delay,
1053 &client_delayed_retry,
1057 GNUNET_assert (size >= th->size);
1058 ret = th->notify (th->notify_cls, size, buf);
1065 * Ask the client to call us once the specified number of bytes
1066 * are free in the transmission buffer. May call the notify
1067 * method immediately if enough space is available.
1069 * @param sock connection to the service
1070 * @param size number of bytes to send
1071 * @param timeout after how long should we give up (and call
1072 * notify with buf NULL and size 0)?
1073 * @param auto_retry if the connection to the service dies, should we
1074 * automatically re-connect and retry (within the timeout period)
1075 * or should we immediately fail in this case? Pass GNUNET_YES
1076 * if the caller does not care about temporary connection errors,
1077 * for example because the protocol is stateless
1078 * @param notify function to call
1079 * @param notify_cls closure for notify
1080 * @return NULL if our buffer will never hold size bytes,
1081 * a handle if the notify callback was queued (can be used to cancel)
1083 struct GNUNET_CLIENT_TransmitHandle *
1084 GNUNET_CLIENT_notify_transmit_ready (struct GNUNET_CLIENT_Connection *sock,
1086 struct GNUNET_TIME_Relative timeout,
1088 GNUNET_CONNECTION_TransmitReadyNotify
1089 notify, void *notify_cls)
1091 struct GNUNET_CLIENT_TransmitHandle *th;
1093 if (NULL != sock->th)
1095 /* If this breaks, you most likley called this function twice without waiting
1096 * for completion or canceling the request */
1100 th = GNUNET_malloc (sizeof (struct GNUNET_CLIENT_TransmitHandle));
1103 th->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1104 th->auto_retry = auto_retry;
1105 th->notify = notify;
1106 th->notify_cls = notify_cls;
1107 th->attempts_left = MAX_ATTEMPTS;
1109 if (sock->sock == NULL)
1111 th->reconnect_task = GNUNET_SCHEDULER_add_delayed (sock->back_off,
1112 &client_delayed_retry,
1118 th->th = GNUNET_CONNECTION_notify_transmit_ready (sock->sock,
1121 &client_notify, th);
1135 * Cancel a request for notification.
1137 * @param th handle from the original request.
1140 GNUNET_CLIENT_notify_transmit_ready_cancel (struct
1141 GNUNET_CLIENT_TransmitHandle *th)
1143 if (th->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
1145 GNUNET_assert (NULL == th->th);
1146 GNUNET_SCHEDULER_cancel (th->reconnect_task);
1147 th->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1151 GNUNET_assert (NULL != th->th);
1152 GNUNET_CONNECTION_notify_transmit_ready_cancel (th->th);
1154 th->sock->th = NULL;
1160 * Function called to notify a client about the socket
1161 * begin ready to queue the message. "buf" will be
1162 * NULL and "size" zero if the socket was closed for
1163 * writing in the meantime.
1165 * @param cls closure of type "struct TransmitGetResponseContext*"
1166 * @param size number of bytes available in buf
1167 * @param buf where the callee should write the message
1168 * @return number of bytes written to buf
1171 transmit_for_response (void *cls, size_t size, void *buf)
1173 struct TransmitGetResponseContext *tc = cls;
1176 tc->sock->tag = NULL;
1177 msize = ntohs (tc->hdr->size);
1181 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1183 ("Could not submit request, not expecting to receive a response.\n"));
1186 tc->rn (tc->rn_cls, NULL);
1190 GNUNET_assert (size >= msize);
1191 memcpy (buf, tc->hdr, msize);
1192 GNUNET_CLIENT_receive (tc->sock,
1195 GNUNET_TIME_absolute_get_remaining (tc->timeout));
1202 * Convenience API that combines sending a request
1203 * to the service and waiting for a response.
1204 * If either operation times out, the callback
1205 * will be called with a "NULL" response (in which
1206 * case the connection should probably be destroyed).
1208 * @param sock connection to use
1209 * @param hdr message to transmit
1210 * @param timeout when to give up (for both transmission
1211 * and for waiting for a response)
1212 * @param auto_retry if the connection to the service dies, should we
1213 * automatically re-connect and retry (within the timeout period)
1214 * or should we immediately fail in this case? Pass GNUNET_YES
1215 * if the caller does not care about temporary connection errors,
1216 * for example because the protocol is stateless
1217 * @param rn function to call with the response
1218 * @param rn_cls closure for rn
1219 * @return GNUNET_OK on success, GNUNET_SYSERR if a request
1220 * is already pending
1223 GNUNET_CLIENT_transmit_and_get_response (struct GNUNET_CLIENT_Connection
1225 const struct GNUNET_MessageHeader
1227 struct GNUNET_TIME_Relative timeout,
1229 GNUNET_CLIENT_MessageHandler rn,
1232 struct TransmitGetResponseContext *tc;
1235 if (NULL != sock->th)
1236 return GNUNET_SYSERR;
1237 GNUNET_assert (sock->tag == NULL);
1238 msize = ntohs (hdr->size);
1239 tc = GNUNET_malloc (sizeof (struct TransmitGetResponseContext) + msize);
1241 tc->hdr = (const struct GNUNET_MessageHeader *) &tc[1];
1242 memcpy (&tc[1], hdr, msize);
1243 tc->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1245 tc->rn_cls = rn_cls;
1246 if (NULL == GNUNET_CLIENT_notify_transmit_ready (sock,
1250 &transmit_for_response, tc))
1254 return GNUNET_SYSERR;
1262 /* end of client.c */