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
48 * Handle for a transmission request.
50 struct GNUNET_CLIENT_TransmitHandle
55 struct GNUNET_CLIENT_Connection *sock;
58 * Function to call to get the data for transmission.
60 GNUNET_CONNECTION_TransmitReadyNotify notify;
68 * Handle to the transmission with the underlying
71 struct GNUNET_CONNECTION_TransmitHandle *th;
74 * If we are re-trying and are delaying to do so,
75 * handle to the scheduled task managing the delay.
77 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
80 * Timeout for the operation overall.
82 struct GNUNET_TIME_Absolute timeout;
85 * Number of bytes requested.
90 * Are we allowed to re-try to connect without telling
91 * the user (of this API) about the connection troubles?
96 * Number of attempts left for transmitting the request. We may
97 * fail the first time (say because the service is not yet up), in
98 * which case (if auto_retry is set) we wait a bit and re-try
99 * (timeout permitting).
101 unsigned int attempts_left;
107 * Context for processing
108 * "GNUNET_CLIENT_transmit_and_get_response" requests.
110 struct TransmitGetResponseContext
115 struct GNUNET_CLIENT_Connection *sock;
118 * Message to transmit; do not free, allocated
119 * right after this struct.
121 const struct GNUNET_MessageHeader *hdr;
126 struct GNUNET_TIME_Absolute timeout;
129 * Function to call when done.
131 GNUNET_CLIENT_MessageHandler rn;
140 * Context for handling the shutdown of a service.
142 struct ShutdownContext
145 * Scheduler to be used to call continuation
147 struct GNUNET_SCHEDULER_Handle *sched;
149 * Connection to the service that is being shutdown.
151 struct GNUNET_CLIENT_Connection *sock;
154 * Time allowed for shutdown to happen.
156 struct GNUNET_TIME_Absolute timeout;
159 * Task set up to cancel the shutdown request on timeout.
161 GNUNET_SCHEDULER_TaskIdentifier cancel_task;
164 * Task to call once shutdown complete
166 GNUNET_CLIENT_ShutdownTask cont;
169 * Closure for shutdown continuation
174 * We received a confirmation that the service will shut down.
181 * Struct to refer to a GNUnet TCP connection.
182 * This is more than just a socket because if the server
183 * drops the connection, the client automatically tries
184 * to reconnect (and for that needs connection information).
186 struct GNUNET_CLIENT_Connection
190 * the socket handle, NULL if not live
192 struct GNUNET_CONNECTION_Handle *sock;
197 struct GNUNET_SCHEDULER_Handle *sched;
202 struct GNUNET_CONFIGURATION_Handle *cfg;
205 * Name of the service we interact with.
210 * Context of a transmit_and_get_response operation, NULL
211 * if no such operation is pending.
213 struct TransmitGetResponseContext *tag;
216 * Handler for current receiver task.
218 GNUNET_CLIENT_MessageHandler receiver_handler;
221 * Closure for receiver_handler.
223 void *receiver_handler_cls;
226 * Handle for a pending transmission request, NULL if there is
229 struct GNUNET_CLIENT_TransmitHandle *th;
232 * Handler for service test completion (NULL unless in service_test)
234 GNUNET_SCHEDULER_Task test_cb;
237 * Deadline for calling 'test_cb'.
239 struct GNUNET_TIME_Absolute test_deadline;
242 * If we are re-trying and are delaying to do so,
243 * handle to the scheduled task managing the delay.
245 GNUNET_SCHEDULER_TaskIdentifier receive_task;
248 * Closure for test_cb (NULL unless in service_test)
253 * Buffer for received message.
258 * Timeout for receiving a response (absolute time).
260 struct GNUNET_TIME_Absolute receive_timeout;
263 * Current value for our incremental back-off (for
266 struct GNUNET_TIME_Relative back_off;
269 * Number of bytes in received_buf that are valid.
274 * Size of received_buf.
276 unsigned int received_size;
279 * Do we have a complete response in received_buf?
284 * Are we currently busy doing receive-processing?
285 * GNUNET_YES if so, GNUNET_NO if not.
290 * Are we ignoring shutdown signals?
297 static struct GNUNET_CONNECTION_Handle *
298 do_connect (struct GNUNET_SCHEDULER_Handle *sched,
299 const char *service_name,
300 const struct GNUNET_CONFIGURATION_Handle *cfg)
302 struct GNUNET_CONNECTION_Handle *sock;
304 unsigned long long port;
307 GNUNET_CONFIGURATION_get_value_number (cfg,
313 GNUNET_CONFIGURATION_get_value_string (cfg,
315 "HOSTNAME", &hostname)))
317 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
319 ("Could not determine valid hostname and port for service `%s' from configuration.\n"),
323 if (0 == strlen (hostname))
325 GNUNET_free (hostname);
326 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
327 _("Need a non-empty hostname for service `%s'.\n"),
331 sock = GNUNET_CONNECTION_create_from_connect (sched,
335 GNUNET_SERVER_MAX_MESSAGE_SIZE);
336 GNUNET_free (hostname);
342 * Get a connection with a service.
344 * @param sched scheduler to use
345 * @param service_name name of the service
346 * @param cfg configuration to use
347 * @return NULL on error (service unknown to configuration)
349 struct GNUNET_CLIENT_Connection *
350 GNUNET_CLIENT_connect (struct GNUNET_SCHEDULER_Handle *sched,
351 const char *service_name,
352 const struct GNUNET_CONFIGURATION_Handle *cfg)
354 struct GNUNET_CLIENT_Connection *ret;
355 struct GNUNET_CONNECTION_Handle *sock;
357 sock = do_connect (sched, service_name, cfg);
360 ret = GNUNET_malloc (sizeof (struct GNUNET_CLIENT_Connection));
363 ret->service_name = GNUNET_strdup (service_name);
364 ret->cfg = GNUNET_CONFIGURATION_dup (cfg);
365 ret->back_off = GNUNET_TIME_UNIT_MILLISECONDS;
371 * Configure this connection to ignore shutdown signals.
373 * @param h client handle
374 * @param do_ignore GNUNET_YES to ignore, GNUNET_NO to restore default
377 GNUNET_CLIENT_ignore_shutdown (struct GNUNET_CLIENT_Connection *h,
380 h->ignore_shutdown = do_ignore;
382 GNUNET_CONNECTION_ignore_shutdown (h->sock,
388 * Destroy connection with the service. This will automatically
389 * cancel any pending "receive" request (however, the handler will
390 * *NOT* be called, not even with a NULL message). Any pending
391 * transmission request will also be cancelled UNLESS the callback for
392 * the transmission request has already been called, in which case the
393 * transmission 'finish_pending_write' argument determines whether or
394 * not the write is guaranteed to complete before the socket is fully
395 * destroyed (unless, of course, there is an error with the server in
396 * which case the message may still be lost).
398 * @param finish_pending_write should a transmission already passed to the
399 * handle be completed?
400 * @param sock handle to the service connection
403 GNUNET_CLIENT_disconnect (struct GNUNET_CLIENT_Connection *sock,
404 int finish_pending_write)
406 GNUNET_assert (sock->sock != NULL);
407 if (sock->in_receive == GNUNET_YES)
409 GNUNET_CONNECTION_receive_cancel (sock->sock);
410 sock->in_receive = GNUNET_NO;
412 GNUNET_CONNECTION_destroy (sock->sock, finish_pending_write);
414 if (sock->tag != NULL)
416 GNUNET_free (sock->tag);
419 sock->receiver_handler = NULL;
420 if (sock->th != NULL)
421 GNUNET_CLIENT_notify_transmit_ready_cancel (sock->th);
422 if (sock->receive_task != GNUNET_SCHEDULER_NO_TASK)
424 GNUNET_SCHEDULER_cancel (sock->sched, sock->receive_task);
425 sock->receive_task = GNUNET_SCHEDULER_NO_TASK;
427 GNUNET_array_grow (sock->received_buf, sock->received_size, 0);
428 GNUNET_free (sock->service_name);
429 GNUNET_CONFIGURATION_destroy (sock->cfg);
435 * Check if message is complete
438 check_complete (struct GNUNET_CLIENT_Connection *conn)
440 if ((conn->received_pos >= sizeof (struct GNUNET_MessageHeader)) &&
441 (conn->received_pos >=
442 ntohs (((const struct GNUNET_MessageHeader *) conn->received_buf)->
444 conn->msg_complete = GNUNET_YES;
449 * Callback function for data received from the network. Note that
450 * both "available" and "errCode" would be 0 if the read simply timed out.
453 * @param buf pointer to received data
454 * @param available number of bytes availabe in "buf",
455 * possibly 0 (on errors)
456 * @param addr address of the sender
457 * @param addrlen size of addr
458 * @param errCode value of errno (on errors receiving)
461 receive_helper (void *cls,
464 const struct sockaddr *addr, socklen_t addrlen, int errCode)
466 struct GNUNET_CLIENT_Connection *conn = cls;
467 struct GNUNET_TIME_Relative remaining;
468 GNUNET_CLIENT_MessageHandler receive_handler;
469 void *receive_handler_cls;
471 GNUNET_assert (conn->msg_complete == GNUNET_NO);
472 conn->in_receive = GNUNET_NO;
473 if ((available == 0) || (conn->sock == NULL) || (errCode != 0))
475 /* signal timeout! */
476 if (NULL != (receive_handler = conn->receiver_handler))
478 receive_handler_cls = conn->receiver_handler_cls;
479 conn->receiver_handler = NULL;
480 receive_handler (receive_handler_cls, NULL);
485 /* FIXME: optimize for common fast case where buf contains the
486 entire message and we need no copying... */
489 /* slow path: append to array */
490 if (conn->received_size < conn->received_pos + available)
491 GNUNET_array_grow (conn->received_buf,
492 conn->received_size, conn->received_pos + available);
493 memcpy (&conn->received_buf[conn->received_pos], buf, available);
494 conn->received_pos += available;
495 check_complete (conn);
496 /* check for timeout */
497 remaining = GNUNET_TIME_absolute_get_remaining (conn->receive_timeout);
498 if (remaining.value == 0)
500 /* signal timeout! */
501 conn->receiver_handler (conn->receiver_handler_cls, NULL);
504 /* back to receive -- either for more data or to call callback! */
505 GNUNET_CLIENT_receive (conn,
506 conn->receiver_handler,
507 conn->receiver_handler_cls, remaining);
512 * Continuation to call the receive callback.
514 * @param cls our handle to the client connection
515 * @param tc scheduler context
518 receive_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
520 struct GNUNET_CLIENT_Connection *sock = cls;
521 GNUNET_CLIENT_MessageHandler handler = sock->receiver_handler;
522 const struct GNUNET_MessageHeader *cmsg =
523 (const struct GNUNET_MessageHeader *) sock->received_buf;
524 void *handler_cls = sock->receiver_handler_cls;
525 uint16_t msize = ntohs (cmsg->size);
527 struct GNUNET_MessageHeader *msg = (struct GNUNET_MessageHeader *) mbuf;
529 sock->receive_task = GNUNET_SCHEDULER_NO_TASK;
530 GNUNET_assert (GNUNET_YES == sock->msg_complete);
531 GNUNET_assert (sock->received_pos >= msize);
532 memcpy (msg, cmsg, msize);
533 memmove (sock->received_buf,
534 &sock->received_buf[msize], sock->received_pos - msize);
535 sock->received_pos -= msize;
536 sock->msg_complete = GNUNET_NO;
537 sock->receiver_handler = NULL;
538 check_complete (sock);
540 handler (handler_cls, msg);
545 * Read from the service.
547 * @param sock 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 *sock,
554 GNUNET_CLIENT_MessageHandler handler,
555 void *handler_cls, struct GNUNET_TIME_Relative timeout)
557 if (sock->sock == NULL)
559 /* already disconnected, fail instantly! */
560 GNUNET_break (0); /* this should not happen in well-written code! */
561 handler (handler_cls, NULL);
564 sock->receiver_handler = handler;
565 sock->receiver_handler_cls = handler_cls;
566 sock->receive_timeout = GNUNET_TIME_relative_to_absolute (timeout);
567 if (GNUNET_YES == sock->msg_complete)
569 sock->receive_task = GNUNET_SCHEDULER_add_after (sock->sched,
570 GNUNET_SCHEDULER_NO_TASK,
571 &receive_task, sock);
575 sock->in_receive = GNUNET_YES;
576 GNUNET_CONNECTION_receive (sock->sock,
577 GNUNET_SERVER_MAX_MESSAGE_SIZE,
578 timeout, &receive_helper, sock);
584 * Handler receiving response to service shutdown requests.
585 * First call with NULL: service misbehaving, or something.
586 * First call with GNUNET_MESSAGE_TYPE_SHUTDOWN_ACK:
587 * - service will shutdown
588 * First call with GNUNET_MESSAGE_TYPE_SHUTDOWN_REFUSE:
589 * - service will not be stopped!
591 * Second call with NULL:
592 * - service has now really shut down.
595 * @param msg NULL, indicating socket closure.
598 service_shutdown_handler (void *cls, const struct GNUNET_MessageHeader *msg)
600 struct ShutdownContext *shutdown_ctx = cls;
602 if ((msg == NULL) && (shutdown_ctx->confirmed != GNUNET_YES))
604 /* Means the other side closed the connection and never confirmed a shutdown */
605 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
606 "Service handle shutdown before ACK!\n");
607 if (shutdown_ctx->cont != NULL)
608 shutdown_ctx->cont(shutdown_ctx->cont_cls, GNUNET_SYSERR);
609 GNUNET_SCHEDULER_cancel(shutdown_ctx->sched, shutdown_ctx->cancel_task);
610 GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
611 GNUNET_free(shutdown_ctx);
613 else if ((msg == NULL) && (shutdown_ctx->confirmed == GNUNET_YES))
615 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
616 "Service shutdown complete.\n");
617 if (shutdown_ctx->cont != NULL)
618 shutdown_ctx->cont(shutdown_ctx->cont_cls, GNUNET_NO);
620 GNUNET_SCHEDULER_cancel(shutdown_ctx->sched, shutdown_ctx->cancel_task);
621 GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
622 GNUNET_free(shutdown_ctx);
626 GNUNET_assert(ntohs(msg->size) == sizeof(struct GNUNET_MessageHeader));
628 switch (ntohs(msg->type))
630 case GNUNET_MESSAGE_TYPE_SHUTDOWN_ACK:
631 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
632 "Received confirmation for service shutdown.\n");
633 shutdown_ctx->confirmed = GNUNET_YES;
634 GNUNET_CLIENT_receive (shutdown_ctx->sock,
635 &service_shutdown_handler,
637 GNUNET_TIME_UNIT_FOREVER_REL);
639 case GNUNET_MESSAGE_TYPE_SHUTDOWN_REFUSE:
640 default: /* Fall through */
641 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
642 "Service shutdown refused!\n");
643 if (shutdown_ctx->cont != NULL)
644 shutdown_ctx->cont(shutdown_ctx->cont_cls, GNUNET_YES);
646 GNUNET_SCHEDULER_cancel(shutdown_ctx->sched, shutdown_ctx->cancel_task);
647 GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
648 GNUNET_free(shutdown_ctx);
655 * Shutting down took too long, cancel receive and return error.
658 * @param tc context information (why was this task triggered now)
660 void service_shutdown_cancel (void *cls,
661 const struct GNUNET_SCHEDULER_TaskContext * tc)
663 struct ShutdownContext *shutdown_ctx = cls;
664 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "service_shutdown_cancel called!\n");
665 shutdown_ctx->cont(shutdown_ctx->cont_cls, GNUNET_SYSERR);
666 GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
667 GNUNET_free(shutdown_ctx);
672 * If possible, write a shutdown message to the target
673 * buffer and destroy the client connection.
675 * @param cls the "struct GNUNET_CLIENT_Connection" to destroy
676 * @param size number of bytes available in buf
677 * @param buf NULL on error, otherwise target buffer
678 * @return number of bytes written to buf
681 write_shutdown (void *cls, size_t size, void *buf)
683 struct GNUNET_MessageHeader *msg;
684 struct ShutdownContext *shutdown_ctx = cls;
686 if (size < sizeof (struct GNUNET_MessageHeader))
688 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
689 _("Failed to transmit shutdown request to client.\n"));
690 shutdown_ctx->cont(shutdown_ctx->cont_cls, GNUNET_SYSERR);
691 GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
692 GNUNET_free(shutdown_ctx);
693 return 0; /* client disconnected */
696 GNUNET_CLIENT_receive (shutdown_ctx->sock,
697 &service_shutdown_handler, shutdown_ctx,
698 GNUNET_TIME_UNIT_FOREVER_REL);
699 shutdown_ctx->cancel_task = GNUNET_SCHEDULER_add_delayed (shutdown_ctx->sched,
700 GNUNET_TIME_absolute_get_remaining(shutdown_ctx->timeout),
701 &service_shutdown_cancel,
703 msg = (struct GNUNET_MessageHeader *) buf;
704 msg->type = htons (GNUNET_MESSAGE_TYPE_SHUTDOWN);
705 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
706 return sizeof (struct GNUNET_MessageHeader);
711 * Request that the service should shutdown.
712 * Afterwards, the connection will automatically be
713 * disconnected. Hence the "sock" should not
714 * be used by the caller after this call
715 * (calling this function frees "sock" after a while).
717 * @param sched the scheduler to use for calling shutdown continuation
718 * @param sock the socket connected to the service
719 * @param timeout how long to wait before giving up on transmission
720 * @param cont continuation to call once the service is really down
721 * @param cont_cls closure for continuation
725 GNUNET_CLIENT_service_shutdown (struct GNUNET_SCHEDULER_Handle *sched,
726 struct GNUNET_CLIENT_Connection *sock,
727 struct GNUNET_TIME_Relative timeout,
728 GNUNET_CLIENT_ShutdownTask cont,
731 struct ShutdownContext *shutdown_ctx;
732 shutdown_ctx = GNUNET_malloc(sizeof(struct ShutdownContext));
733 shutdown_ctx->sched = sched;
734 shutdown_ctx->cont = cont;
735 shutdown_ctx->cont_cls = cont_cls;
736 shutdown_ctx->sock = sock;
737 shutdown_ctx->timeout = GNUNET_TIME_relative_to_absolute(timeout);
738 GNUNET_CONNECTION_notify_transmit_ready (sock->sock,
740 GNUNET_MessageHeader),
742 &write_shutdown, shutdown_ctx);
747 * Report service unavailable.
750 service_test_error (struct GNUNET_SCHEDULER_Handle *s,
751 GNUNET_SCHEDULER_Task task, void *task_cls)
753 GNUNET_SCHEDULER_add_continuation (s,
756 GNUNET_SCHEDULER_REASON_TIMEOUT);
761 * Receive confirmation from test, service is up.
764 * @param msg message received, NULL on timeout or fatal error
767 confirm_handler (void *cls, const struct GNUNET_MessageHeader *msg)
769 struct GNUNET_CLIENT_Connection *conn = cls;
770 /* We may want to consider looking at the reply in more
771 detail in the future, for example, is this the
772 correct service? FIXME! */
776 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
777 "Received confirmation that service is running.\n");
779 GNUNET_SCHEDULER_add_continuation (conn->sched,
782 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
786 service_test_error (conn->sched, conn->test_cb, conn->test_cb_cls);
788 GNUNET_CLIENT_disconnect (conn, GNUNET_NO);
793 write_test (void *cls, size_t size, void *buf)
795 struct GNUNET_CLIENT_Connection *conn = cls;
796 struct GNUNET_MessageHeader *msg;
798 if (size < sizeof (struct GNUNET_MessageHeader))
801 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
802 _("Failure to transmit TEST request.\n"));
804 service_test_error (conn->sched, conn->test_cb, conn->test_cb_cls);
805 GNUNET_CLIENT_disconnect (conn, GNUNET_NO);
806 return 0; /* client disconnected */
809 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
810 "Transmitting `%s' request.\n", "TEST");
812 msg = (struct GNUNET_MessageHeader *) buf;
813 msg->type = htons (GNUNET_MESSAGE_TYPE_TEST);
814 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
815 GNUNET_CLIENT_receive (conn,
818 GNUNET_TIME_absolute_get_remaining (conn->test_deadline));
819 return sizeof (struct GNUNET_MessageHeader);
824 * Wait until the service is running.
826 * @param sched scheduler to use
827 * @param service name of the service to wait for
828 * @param cfg configuration to use
829 * @param timeout how long to wait at most in ms
830 * @param task task to run if service is running
831 * (reason will be "PREREQ_DONE" (service running)
832 * or "TIMEOUT" (service not known to be running))
833 * @param task_cls closure for task
836 GNUNET_CLIENT_service_test (struct GNUNET_SCHEDULER_Handle *sched,
838 const struct GNUNET_CONFIGURATION_Handle *cfg,
839 struct GNUNET_TIME_Relative timeout,
840 GNUNET_SCHEDULER_Task task, void *task_cls)
842 struct GNUNET_CLIENT_Connection *conn;
845 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
846 "Testing if service `%s' is running.\n", service);
848 conn = GNUNET_CLIENT_connect (sched, service, cfg);
851 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
853 ("Could not connect to service `%s', must not be running.\n"),
855 service_test_error (sched, task, task_cls);
858 conn->test_cb = task;
859 conn->test_cb_cls = task_cls;
860 conn->test_deadline = GNUNET_TIME_relative_to_absolute (timeout);
862 if (NULL == GNUNET_CLIENT_notify_transmit_ready (conn,
863 sizeof (struct GNUNET_MessageHeader),
868 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
869 _("Failure to transmit request to service `%s'\n"),
871 service_test_error (sched, task, task_cls);
872 GNUNET_CLIENT_disconnect (conn, GNUNET_NO);
879 * Connection notifies us about failure or success of
880 * a transmission request. Either pass it on to our
881 * user or, if possible, retry.
883 * @param cls our "struct GNUNET_CLIENT_TransmissionHandle"
884 * @param size number of bytes available for transmission
885 * @param buf where to write them
886 * @return number of bytes written to buf
888 static size_t client_notify (void *cls, size_t size, void *buf);
892 * This task is run if we should re-try connection to the
893 * service after a while.
895 * @param cls our "struct GNUNET_CLIENT_TransmitHandle" of the request
899 client_delayed_retry (void *cls,
900 const struct GNUNET_SCHEDULER_TaskContext *tc)
902 struct GNUNET_CLIENT_TransmitHandle *th = cls;
904 th->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
905 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
908 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
909 "Transmission failed due to shutdown.\n");
912 th->notify (th->notify_cls, 0, NULL);
916 th->th = GNUNET_CONNECTION_notify_transmit_ready (th->sock->sock,
918 GNUNET_TIME_absolute_get_remaining
924 th->notify (th->notify_cls, 0, NULL);
932 * Connection notifies us about failure or success of a transmission
933 * request. Either pass it on to our user or, if possible, retry.
935 * @param cls our "struct GNUNET_CLIENT_TransmissionHandle"
936 * @param size number of bytes available for transmission
937 * @param buf where to write them
938 * @return number of bytes written to buf
941 client_notify (void *cls, size_t size, void *buf)
943 struct GNUNET_CLIENT_TransmitHandle *th = cls;
945 struct GNUNET_TIME_Relative delay;
951 delay = GNUNET_TIME_absolute_get_remaining (th->timeout);
953 if ( (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & GNUNET_SCHEDULER_get_reason (th->sock->sched))) ||
954 (GNUNET_YES != th->auto_retry) ||
955 (0 == --th->attempts_left) ||
959 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
960 "Transmission failed %u times, giving up.\n",
961 MAX_ATTEMPTS - th->attempts_left);
963 GNUNET_break (0 == th->notify (th->notify_cls, 0, NULL));
968 GNUNET_CONNECTION_destroy (th->sock->sock, GNUNET_NO);
969 th->sock->sock = do_connect (th->sock->sched,
970 th->sock->service_name, th->sock->cfg);
971 GNUNET_assert (NULL != th->sock->sock);
972 GNUNET_CONNECTION_ignore_shutdown (th->sock->sock,
973 th->sock->ignore_shutdown);
974 delay = GNUNET_TIME_relative_min (delay, th->sock->back_off);
976 = GNUNET_TIME_relative_min (GNUNET_TIME_relative_multiply (th->sock->back_off, 2),
977 GNUNET_TIME_UNIT_SECONDS);
979 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
980 "Transmission failed %u times, trying again in %llums.\n",
981 MAX_ATTEMPTS - th->attempts_left,
982 (unsigned long long) delay.value);
984 th->reconnect_task = GNUNET_SCHEDULER_add_delayed (th->sock->sched,
986 &client_delayed_retry,
991 GNUNET_assert (size >= th->size);
992 ret = th->notify (th->notify_cls, size, buf);
999 * Ask the client to call us once the specified number of bytes
1000 * are free in the transmission buffer. May call the notify
1001 * method immediately if enough space is available.
1003 * @param sock connection to the service
1004 * @param size number of bytes to send
1005 * @param timeout after how long should we give up (and call
1006 * notify with buf NULL and size 0)?
1007 * @param auto_retry if the connection to the service dies, should we
1008 * automatically re-connect and retry (within the timeout period)
1009 * or should we immediately fail in this case? Pass GNUNET_YES
1010 * if the caller does not care about temporary connection errors,
1011 * for example because the protocol is stateless
1012 * @param notify function to call
1013 * @param notify_cls closure for notify
1014 * @return NULL if our buffer will never hold size bytes,
1015 * a handle if the notify callback was queued (can be used to cancel)
1017 struct GNUNET_CLIENT_TransmitHandle *
1018 GNUNET_CLIENT_notify_transmit_ready (struct GNUNET_CLIENT_Connection *sock,
1020 struct GNUNET_TIME_Relative timeout,
1022 GNUNET_CONNECTION_TransmitReadyNotify
1023 notify, void *notify_cls)
1025 struct GNUNET_CLIENT_TransmitHandle *th;
1027 if (NULL != sock->th)
1029 th = GNUNET_malloc (sizeof (struct GNUNET_CLIENT_TransmitHandle));
1032 th->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1033 th->auto_retry = auto_retry;
1034 th->notify = notify;
1035 th->notify_cls = notify_cls;
1036 th->attempts_left = MAX_ATTEMPTS;
1037 th->th = GNUNET_CONNECTION_notify_transmit_ready (sock->sock,
1040 &client_notify, th);
1053 * Cancel a request for notification.
1055 * @param th handle from the original request.
1058 GNUNET_CLIENT_notify_transmit_ready_cancel (struct
1059 GNUNET_CLIENT_TransmitHandle *th)
1061 if (th->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
1063 GNUNET_break (NULL == th->th);
1064 GNUNET_SCHEDULER_cancel (th->sock->sched, th->reconnect_task);
1065 th->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1069 GNUNET_break (NULL != th->th);
1070 GNUNET_CONNECTION_notify_transmit_ready_cancel (th->th);
1072 th->sock->th = NULL;
1078 * Function called to notify a client about the socket
1079 * begin ready to queue the message. "buf" will be
1080 * NULL and "size" zero if the socket was closed for
1081 * writing in the meantime.
1083 * @param cls closure of type "struct TransmitGetResponseContext*"
1084 * @param size number of bytes available in buf
1085 * @param buf where the callee should write the message
1086 * @return number of bytes written to buf
1089 transmit_for_response (void *cls, size_t size, void *buf)
1091 struct TransmitGetResponseContext *tc = cls;
1094 tc->sock->tag = NULL;
1095 msize = ntohs (tc->hdr->size);
1099 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1100 _("Could not submit request, not expecting to receive a response.\n"));
1102 tc->rn (tc->rn_cls, NULL);
1106 GNUNET_assert (size >= msize);
1107 memcpy (buf, tc->hdr, msize);
1108 GNUNET_CLIENT_receive (tc->sock,
1111 GNUNET_TIME_absolute_get_remaining (tc->timeout));
1118 * Convenience API that combines sending a request
1119 * to the service and waiting for a response.
1120 * If either operation times out, the callback
1121 * will be called with a "NULL" response (in which
1122 * case the connection should probably be destroyed).
1124 * @param sock connection to use
1125 * @param hdr message to transmit
1126 * @param timeout when to give up (for both transmission
1127 * and for waiting for a response)
1128 * @param auto_retry if the connection to the service dies, should we
1129 * automatically re-connect and retry (within the timeout period)
1130 * or should we immediately fail in this case? Pass GNUNET_YES
1131 * if the caller does not care about temporary connection errors,
1132 * for example because the protocol is stateless
1133 * @param rn function to call with the response
1134 * @param rn_cls closure for rn
1135 * @return GNUNET_OK on success, GNUNET_SYSERR if a request
1136 * is already pending
1139 GNUNET_CLIENT_transmit_and_get_response (struct GNUNET_CLIENT_Connection
1141 const struct GNUNET_MessageHeader
1143 struct GNUNET_TIME_Relative timeout,
1145 GNUNET_CLIENT_MessageHandler rn,
1148 struct TransmitGetResponseContext *tc;
1151 if (NULL != sock->th)
1152 return GNUNET_SYSERR;
1153 GNUNET_assert (sock->tag == NULL);
1154 msize = ntohs (hdr->size);
1155 tc = GNUNET_malloc (sizeof (struct TransmitGetResponseContext) + msize);
1157 tc->hdr = (const struct GNUNET_MessageHeader *) &tc[1];
1158 memcpy (&tc[1], hdr, msize);
1159 tc->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1161 tc->rn_cls = rn_cls;
1162 if (NULL == GNUNET_CLIENT_notify_transmit_ready (sock,
1166 &transmit_for_response,
1171 return GNUNET_SYSERR;
1179 /* end of client.c */