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)) /* Means the other side closed the connection and never confirmed a shutdown */
604 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Service handle shutdown before ACK!\n");
605 if (shutdown_ctx->cont != NULL)
606 shutdown_ctx->cont(shutdown_ctx->cont_cls, GNUNET_SYSERR);
608 GNUNET_SCHEDULER_cancel(shutdown_ctx->sched, shutdown_ctx->cancel_task);
609 GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
610 GNUNET_free(shutdown_ctx);
612 else if ((msg == NULL) && (shutdown_ctx->confirmed == GNUNET_YES))
614 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Service shutdown complete.\n");
615 if (shutdown_ctx->cont != NULL)
616 shutdown_ctx->cont(shutdown_ctx->cont_cls, GNUNET_NO);
618 GNUNET_SCHEDULER_cancel(shutdown_ctx->sched, shutdown_ctx->cancel_task);
619 GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
620 GNUNET_free(shutdown_ctx);
624 GNUNET_assert(ntohs(msg->size) == sizeof(struct GNUNET_MessageHeader));
626 switch (ntohs(msg->type))
628 case GNUNET_MESSAGE_TYPE_SHUTDOWN_ACK:
629 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received confirmation for service shutdown.\n");
630 shutdown_ctx->confirmed = GNUNET_YES;
631 GNUNET_CLIENT_receive (shutdown_ctx->sock, &service_shutdown_handler, shutdown_ctx, GNUNET_TIME_UNIT_FOREVER_REL);
633 case GNUNET_MESSAGE_TYPE_SHUTDOWN_REFUSE:
634 default: /* Fall through */
635 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Service shutdown refused!\n");
636 if (shutdown_ctx->cont != NULL)
637 shutdown_ctx->cont(shutdown_ctx->cont_cls, GNUNET_YES);
639 GNUNET_SCHEDULER_cancel(shutdown_ctx->sched, shutdown_ctx->cancel_task);
640 GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
641 GNUNET_free(shutdown_ctx);
648 * Shutting down took too long, cancel receive and return error.
651 * @param tc context information (why was this task triggered now)
653 void service_shutdown_cancel (void *cls,
654 const struct GNUNET_SCHEDULER_TaskContext * tc)
656 struct ShutdownContext *shutdown_ctx = cls;
657 fprintf(stderr, "service_shutdown_cancel called!\n");
658 shutdown_ctx->cont(shutdown_ctx->cont_cls, GNUNET_SYSERR);
659 GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
660 GNUNET_free(shutdown_ctx);
663 * If possible, write a shutdown message to the target
664 * buffer and destroy the client connection.
666 * @param cls the "struct GNUNET_CLIENT_Connection" to destroy
667 * @param size number of bytes available in buf
668 * @param buf NULL on error, otherwise target buffer
669 * @return number of bytes written to buf
672 write_shutdown (void *cls, size_t size, void *buf)
674 struct GNUNET_MessageHeader *msg;
675 struct ShutdownContext *shutdown_ctx = cls;
677 if (size < sizeof (struct GNUNET_MessageHeader))
679 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
680 _("Failed to transmit shutdown request to client.\n"));
682 shutdown_ctx->cont(shutdown_ctx->cont_cls, GNUNET_SYSERR);
683 GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
684 GNUNET_free(shutdown_ctx);
685 return 0; /* client disconnected */
688 GNUNET_CLIENT_receive (shutdown_ctx->sock, &service_shutdown_handler, shutdown_ctx, GNUNET_TIME_UNIT_FOREVER_REL);
689 shutdown_ctx->cancel_task = GNUNET_SCHEDULER_add_delayed (shutdown_ctx->sched, GNUNET_TIME_absolute_get_remaining(shutdown_ctx->timeout), &service_shutdown_cancel, shutdown_ctx);
690 msg = (struct GNUNET_MessageHeader *) buf;
691 msg->type = htons (GNUNET_MESSAGE_TYPE_SHUTDOWN);
692 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
693 return sizeof (struct GNUNET_MessageHeader);
698 * Request that the service should shutdown.
699 * Afterwards, the connection will automatically be
700 * disconnected. Hence the "sock" should not
701 * be used by the caller after this call
702 * (calling this function frees "sock" after a while).
704 * @param sched the scheduler to use for calling shutdown continuation
705 * @param sock the socket connected to the service
706 * @param timeout how long to wait before giving up on transmission
707 * @param cont continuation to call once the service is really down
708 * @param cont_cls closure for continuation
712 GNUNET_CLIENT_service_shutdown (struct GNUNET_SCHEDULER_Handle *sched,
713 struct GNUNET_CLIENT_Connection *sock,
714 struct GNUNET_TIME_Relative timeout,
715 GNUNET_CLIENT_ShutdownTask cont,
718 struct ShutdownContext *shutdown_ctx;
719 shutdown_ctx = GNUNET_malloc(sizeof(struct ShutdownContext));
720 shutdown_ctx->sched = sched;
721 shutdown_ctx->cont = cont;
722 shutdown_ctx->cont_cls = cont_cls;
723 shutdown_ctx->sock = sock;
724 shutdown_ctx->timeout = GNUNET_TIME_relative_to_absolute(timeout);
726 GNUNET_CONNECTION_notify_transmit_ready (sock->sock,
728 GNUNET_MessageHeader),
730 &write_shutdown, shutdown_ctx);
735 * Report service unavailable.
738 service_test_error (struct GNUNET_SCHEDULER_Handle *s,
739 GNUNET_SCHEDULER_Task task, void *task_cls)
741 GNUNET_SCHEDULER_add_continuation (s,
744 GNUNET_SCHEDULER_REASON_TIMEOUT);
749 * Receive confirmation from test, service is up.
752 * @param msg message received, NULL on timeout or fatal error
755 confirm_handler (void *cls, const struct GNUNET_MessageHeader *msg)
757 struct GNUNET_CLIENT_Connection *conn = cls;
758 /* We may want to consider looking at the reply in more
759 detail in the future, for example, is this the
760 correct service? FIXME! */
764 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
765 "Received confirmation that service is running.\n");
767 GNUNET_SCHEDULER_add_continuation (conn->sched,
770 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
774 service_test_error (conn->sched, conn->test_cb, conn->test_cb_cls);
776 GNUNET_CLIENT_disconnect (conn, GNUNET_NO);
781 write_test (void *cls, size_t size, void *buf)
783 struct GNUNET_CLIENT_Connection *conn = cls;
784 struct GNUNET_MessageHeader *msg;
786 if (size < sizeof (struct GNUNET_MessageHeader))
789 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
790 _("Failure to transmit TEST request.\n"));
792 service_test_error (conn->sched, conn->test_cb, conn->test_cb_cls);
793 return 0; /* client disconnected */
796 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
797 "Transmitting `%s' request.\n", "TEST");
799 msg = (struct GNUNET_MessageHeader *) buf;
800 msg->type = htons (GNUNET_MESSAGE_TYPE_TEST);
801 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
802 GNUNET_CLIENT_receive (conn,
805 GNUNET_TIME_absolute_get_remaining (conn->test_deadline));
806 return sizeof (struct GNUNET_MessageHeader);
811 * Wait until the service is running.
813 * @param sched scheduler to use
814 * @param service name of the service to wait for
815 * @param cfg configuration to use
816 * @param timeout how long to wait at most in ms
817 * @param task task to run if service is running
818 * (reason will be "PREREQ_DONE" (service running)
819 * or "TIMEOUT" (service not known to be running))
820 * @param task_cls closure for task
823 GNUNET_CLIENT_service_test (struct GNUNET_SCHEDULER_Handle *sched,
825 const struct GNUNET_CONFIGURATION_Handle *cfg,
826 struct GNUNET_TIME_Relative timeout,
827 GNUNET_SCHEDULER_Task task, void *task_cls)
829 struct GNUNET_CLIENT_Connection *conn;
832 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
833 "Testing if service `%s' is running.\n", service);
835 conn = GNUNET_CLIENT_connect (sched, service, cfg);
838 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
840 ("Could not connect to service `%s', must not be running.\n"),
842 service_test_error (sched, task, task_cls);
845 conn->test_cb = task;
846 conn->test_cb_cls = task_cls;
847 conn->test_deadline = GNUNET_TIME_relative_to_absolute (timeout);
849 if (NULL == GNUNET_CLIENT_notify_transmit_ready (conn,
850 sizeof (struct GNUNET_MessageHeader),
855 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
856 _("Failure to transmit request to service `%s'\n"),
858 service_test_error (sched, task, task_cls);
859 GNUNET_CLIENT_disconnect (conn, GNUNET_NO);
866 * Connection notifies us about failure or success of
867 * a transmission request. Either pass it on to our
868 * user or, if possible, retry.
870 * @param cls our "struct GNUNET_CLIENT_TransmissionHandle"
871 * @param size number of bytes available for transmission
872 * @param buf where to write them
873 * @return number of bytes written to buf
875 static size_t client_notify (void *cls, size_t size, void *buf);
879 * This task is run if we should re-try connection to the
880 * service after a while.
882 * @param cls our "struct GNUNET_CLIENT_TransmitHandle" of the request
886 client_delayed_retry (void *cls,
887 const struct GNUNET_SCHEDULER_TaskContext *tc)
889 struct GNUNET_CLIENT_TransmitHandle *th = cls;
891 th->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
892 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
895 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
896 "Transmission failed due to shutdown.\n");
899 th->notify (th->notify_cls, 0, NULL);
903 th->th = GNUNET_CONNECTION_notify_transmit_ready (th->sock->sock,
905 GNUNET_TIME_absolute_get_remaining
911 th->notify (th->notify_cls, 0, NULL);
919 * Connection notifies us about failure or success of a transmission
920 * request. Either pass it on to our 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
928 client_notify (void *cls, size_t size, void *buf)
930 struct GNUNET_CLIENT_TransmitHandle *th = cls;
932 struct GNUNET_TIME_Relative delay;
938 delay = GNUNET_TIME_absolute_get_remaining (th->timeout);
940 if ( (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & GNUNET_SCHEDULER_get_reason (th->sock->sched))) ||
941 (GNUNET_YES != th->auto_retry) ||
942 (0 == --th->attempts_left) ||
946 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
947 "Transmission failed %u times, giving up.\n",
948 MAX_ATTEMPTS - th->attempts_left);
950 GNUNET_break (0 == th->notify (th->notify_cls, 0, NULL));
955 GNUNET_CONNECTION_destroy (th->sock->sock, GNUNET_NO);
956 th->sock->sock = do_connect (th->sock->sched,
957 th->sock->service_name, th->sock->cfg);
958 GNUNET_assert (NULL != th->sock->sock);
959 GNUNET_CONNECTION_ignore_shutdown (th->sock->sock,
960 th->sock->ignore_shutdown);
961 delay = GNUNET_TIME_relative_min (delay, th->sock->back_off);
963 = GNUNET_TIME_relative_min (GNUNET_TIME_relative_multiply (th->sock->back_off, 2),
964 GNUNET_TIME_UNIT_SECONDS);
966 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
967 "Transmission failed %u times, trying again in %llums.\n",
968 MAX_ATTEMPTS - th->attempts_left,
969 (unsigned long long) delay.value);
971 th->reconnect_task = GNUNET_SCHEDULER_add_delayed (th->sock->sched,
973 &client_delayed_retry,
978 GNUNET_assert (size >= th->size);
979 ret = th->notify (th->notify_cls, size, buf);
986 * Ask the client to call us once the specified number of bytes
987 * are free in the transmission buffer. May call the notify
988 * method immediately if enough space is available.
990 * @param sock connection to the service
991 * @param size number of bytes to send
992 * @param timeout after how long should we give up (and call
993 * notify with buf NULL and size 0)?
994 * @param auto_retry if the connection to the service dies, should we
995 * automatically re-connect and retry (within the timeout period)
996 * or should we immediately fail in this case? Pass GNUNET_YES
997 * if the caller does not care about temporary connection errors,
998 * for example because the protocol is stateless
999 * @param notify function to call
1000 * @param notify_cls closure for notify
1001 * @return NULL if our buffer will never hold size bytes,
1002 * a handle if the notify callback was queued (can be used to cancel)
1004 struct GNUNET_CLIENT_TransmitHandle *
1005 GNUNET_CLIENT_notify_transmit_ready (struct GNUNET_CLIENT_Connection *sock,
1007 struct GNUNET_TIME_Relative timeout,
1009 GNUNET_CONNECTION_TransmitReadyNotify
1010 notify, void *notify_cls)
1012 struct GNUNET_CLIENT_TransmitHandle *th;
1014 if (NULL != sock->th)
1016 th = GNUNET_malloc (sizeof (struct GNUNET_CLIENT_TransmitHandle));
1019 th->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1020 th->auto_retry = auto_retry;
1021 th->notify = notify;
1022 th->notify_cls = notify_cls;
1023 th->attempts_left = MAX_ATTEMPTS;
1024 th->th = GNUNET_CONNECTION_notify_transmit_ready (sock->sock,
1027 &client_notify, th);
1040 * Cancel a request for notification.
1042 * @param th handle from the original request.
1045 GNUNET_CLIENT_notify_transmit_ready_cancel (struct
1046 GNUNET_CLIENT_TransmitHandle *th)
1048 if (th->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
1050 GNUNET_break (NULL == th->th);
1051 GNUNET_SCHEDULER_cancel (th->sock->sched, th->reconnect_task);
1052 th->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1056 GNUNET_break (NULL != th->th);
1057 GNUNET_CONNECTION_notify_transmit_ready_cancel (th->th);
1059 th->sock->th = NULL;
1065 * Function called to notify a client about the socket
1066 * begin ready to queue the message. "buf" will be
1067 * NULL and "size" zero if the socket was closed for
1068 * writing in the meantime.
1070 * @param cls closure of type "struct TransmitGetResponseContext*"
1071 * @param size number of bytes available in buf
1072 * @param buf where the callee should write the message
1073 * @return number of bytes written to buf
1076 transmit_for_response (void *cls, size_t size, void *buf)
1078 struct TransmitGetResponseContext *tc = cls;
1081 tc->sock->tag = NULL;
1082 msize = ntohs (tc->hdr->size);
1086 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1087 _("Could not submit request, not expecting to receive a response.\n"));
1089 tc->rn (tc->rn_cls, NULL);
1093 GNUNET_assert (size >= msize);
1094 memcpy (buf, tc->hdr, msize);
1095 GNUNET_CLIENT_receive (tc->sock,
1098 GNUNET_TIME_absolute_get_remaining (tc->timeout));
1105 * Convenience API that combines sending a request
1106 * to the service and waiting for a response.
1107 * If either operation times out, the callback
1108 * will be called with a "NULL" response (in which
1109 * case the connection should probably be destroyed).
1111 * @param sock connection to use
1112 * @param hdr message to transmit
1113 * @param timeout when to give up (for both transmission
1114 * and for waiting for a response)
1115 * @param auto_retry if the connection to the service dies, should we
1116 * automatically re-connect and retry (within the timeout period)
1117 * or should we immediately fail in this case? Pass GNUNET_YES
1118 * if the caller does not care about temporary connection errors,
1119 * for example because the protocol is stateless
1120 * @param rn function to call with the response
1121 * @param rn_cls closure for rn
1122 * @return GNUNET_OK on success, GNUNET_SYSERR if a request
1123 * is already pending
1126 GNUNET_CLIENT_transmit_and_get_response (struct GNUNET_CLIENT_Connection
1128 const struct GNUNET_MessageHeader
1130 struct GNUNET_TIME_Relative timeout,
1132 GNUNET_CLIENT_MessageHandler rn,
1135 struct TransmitGetResponseContext *tc;
1138 if (NULL != sock->th)
1139 return GNUNET_SYSERR;
1140 GNUNET_assert (sock->tag == NULL);
1141 msize = ntohs (hdr->size);
1142 tc = GNUNET_malloc (sizeof (struct TransmitGetResponseContext) + msize);
1144 tc->hdr = (const struct GNUNET_MessageHeader *) &tc[1];
1145 memcpy (&tc[1], hdr, msize);
1146 tc->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1148 tc->rn_cls = rn_cls;
1149 if (NULL == GNUNET_CLIENT_notify_transmit_ready (sock,
1153 &transmit_for_response,
1158 return GNUNET_SYSERR;
1166 /* end of client.c */