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, "Service shutdown complete.\n");
616 if (shutdown_ctx->cont != NULL)
617 shutdown_ctx->cont(shutdown_ctx->cont_cls, GNUNET_NO);
619 GNUNET_SCHEDULER_cancel(shutdown_ctx->sched, shutdown_ctx->cancel_task);
620 GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
621 GNUNET_free(shutdown_ctx);
625 GNUNET_assert(ntohs(msg->size) == sizeof(struct GNUNET_MessageHeader));
627 switch (ntohs(msg->type))
629 case GNUNET_MESSAGE_TYPE_SHUTDOWN_ACK:
630 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
631 "Received confirmation for service shutdown.\n");
632 shutdown_ctx->confirmed = GNUNET_YES;
633 GNUNET_CLIENT_receive (shutdown_ctx->sock,
634 &service_shutdown_handler,
636 GNUNET_TIME_UNIT_FOREVER_REL);
638 case GNUNET_MESSAGE_TYPE_SHUTDOWN_REFUSE:
639 default: /* Fall through */
640 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
641 "Service shutdown refused!\n");
642 if (shutdown_ctx->cont != NULL)
643 shutdown_ctx->cont(shutdown_ctx->cont_cls, GNUNET_YES);
645 GNUNET_SCHEDULER_cancel(shutdown_ctx->sched, shutdown_ctx->cancel_task);
646 GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
647 GNUNET_free(shutdown_ctx);
654 * Shutting down took too long, cancel receive and return error.
657 * @param tc context information (why was this task triggered now)
659 void service_shutdown_cancel (void *cls,
660 const struct GNUNET_SCHEDULER_TaskContext * tc)
662 struct ShutdownContext *shutdown_ctx = cls;
663 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "service_shutdown_cancel called!\n");
664 shutdown_ctx->cont(shutdown_ctx->cont_cls, GNUNET_SYSERR);
665 GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
666 GNUNET_free(shutdown_ctx);
671 * If possible, write a shutdown message to the target
672 * buffer and destroy the client connection.
674 * @param cls the "struct GNUNET_CLIENT_Connection" to destroy
675 * @param size number of bytes available in buf
676 * @param buf NULL on error, otherwise target buffer
677 * @return number of bytes written to buf
680 write_shutdown (void *cls, size_t size, void *buf)
682 struct GNUNET_MessageHeader *msg;
683 struct ShutdownContext *shutdown_ctx = cls;
685 if (size < sizeof (struct GNUNET_MessageHeader))
687 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
688 _("Failed to transmit shutdown request to client.\n"));
689 shutdown_ctx->cont(shutdown_ctx->cont_cls, GNUNET_SYSERR);
690 GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
691 GNUNET_free(shutdown_ctx);
692 return 0; /* client disconnected */
695 GNUNET_CLIENT_receive (shutdown_ctx->sock,
696 &service_shutdown_handler, shutdown_ctx,
697 GNUNET_TIME_UNIT_FOREVER_REL);
698 shutdown_ctx->cancel_task = GNUNET_SCHEDULER_add_delayed (shutdown_ctx->sched,
699 GNUNET_TIME_absolute_get_remaining(shutdown_ctx->timeout),
700 &service_shutdown_cancel,
702 msg = (struct GNUNET_MessageHeader *) buf;
703 msg->type = htons (GNUNET_MESSAGE_TYPE_SHUTDOWN);
704 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
705 return sizeof (struct GNUNET_MessageHeader);
710 * Request that the service should shutdown.
711 * Afterwards, the connection will automatically be
712 * disconnected. Hence the "sock" should not
713 * be used by the caller after this call
714 * (calling this function frees "sock" after a while).
716 * @param sched the scheduler to use for calling shutdown continuation
717 * @param sock the socket connected to the service
718 * @param timeout how long to wait before giving up on transmission
719 * @param cont continuation to call once the service is really down
720 * @param cont_cls closure for continuation
724 GNUNET_CLIENT_service_shutdown (struct GNUNET_SCHEDULER_Handle *sched,
725 struct GNUNET_CLIENT_Connection *sock,
726 struct GNUNET_TIME_Relative timeout,
727 GNUNET_CLIENT_ShutdownTask cont,
730 struct ShutdownContext *shutdown_ctx;
731 shutdown_ctx = GNUNET_malloc(sizeof(struct ShutdownContext));
732 shutdown_ctx->sched = sched;
733 shutdown_ctx->cont = cont;
734 shutdown_ctx->cont_cls = cont_cls;
735 shutdown_ctx->sock = sock;
736 shutdown_ctx->timeout = GNUNET_TIME_relative_to_absolute(timeout);
737 GNUNET_CONNECTION_notify_transmit_ready (sock->sock,
739 GNUNET_MessageHeader),
741 &write_shutdown, shutdown_ctx);
746 * Report service unavailable.
749 service_test_error (struct GNUNET_SCHEDULER_Handle *s,
750 GNUNET_SCHEDULER_Task task, void *task_cls)
752 GNUNET_SCHEDULER_add_continuation (s,
755 GNUNET_SCHEDULER_REASON_TIMEOUT);
760 * Receive confirmation from test, service is up.
763 * @param msg message received, NULL on timeout or fatal error
766 confirm_handler (void *cls, const struct GNUNET_MessageHeader *msg)
768 struct GNUNET_CLIENT_Connection *conn = cls;
769 /* We may want to consider looking at the reply in more
770 detail in the future, for example, is this the
771 correct service? FIXME! */
775 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
776 "Received confirmation that service is running.\n");
778 GNUNET_SCHEDULER_add_continuation (conn->sched,
781 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
785 service_test_error (conn->sched, conn->test_cb, conn->test_cb_cls);
787 GNUNET_CLIENT_disconnect (conn, GNUNET_NO);
792 write_test (void *cls, size_t size, void *buf)
794 struct GNUNET_CLIENT_Connection *conn = cls;
795 struct GNUNET_MessageHeader *msg;
797 if (size < sizeof (struct GNUNET_MessageHeader))
800 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
801 _("Failure to transmit TEST request.\n"));
803 service_test_error (conn->sched, conn->test_cb, conn->test_cb_cls);
804 GNUNET_CLIENT_disconnect (conn, GNUNET_NO);
805 return 0; /* client disconnected */
808 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
809 "Transmitting `%s' request.\n", "TEST");
811 msg = (struct GNUNET_MessageHeader *) buf;
812 msg->type = htons (GNUNET_MESSAGE_TYPE_TEST);
813 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
814 GNUNET_CLIENT_receive (conn,
817 GNUNET_TIME_absolute_get_remaining (conn->test_deadline));
818 return sizeof (struct GNUNET_MessageHeader);
823 * Wait until the service is running.
825 * @param sched scheduler to use
826 * @param service name of the service to wait for
827 * @param cfg configuration to use
828 * @param timeout how long to wait at most in ms
829 * @param task task to run if service is running
830 * (reason will be "PREREQ_DONE" (service running)
831 * or "TIMEOUT" (service not known to be running))
832 * @param task_cls closure for task
835 GNUNET_CLIENT_service_test (struct GNUNET_SCHEDULER_Handle *sched,
837 const struct GNUNET_CONFIGURATION_Handle *cfg,
838 struct GNUNET_TIME_Relative timeout,
839 GNUNET_SCHEDULER_Task task, void *task_cls)
841 struct GNUNET_CLIENT_Connection *conn;
844 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
845 "Testing if service `%s' is running.\n", service);
847 conn = GNUNET_CLIENT_connect (sched, service, cfg);
850 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
852 ("Could not connect to service `%s', must not be running.\n"),
854 service_test_error (sched, task, task_cls);
857 conn->test_cb = task;
858 conn->test_cb_cls = task_cls;
859 conn->test_deadline = GNUNET_TIME_relative_to_absolute (timeout);
861 if (NULL == GNUNET_CLIENT_notify_transmit_ready (conn,
862 sizeof (struct GNUNET_MessageHeader),
867 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
868 _("Failure to transmit request to service `%s'\n"),
870 service_test_error (sched, task, task_cls);
871 GNUNET_CLIENT_disconnect (conn, GNUNET_NO);
878 * Connection notifies us about failure or success of
879 * a transmission request. Either pass it on to our
880 * user or, if possible, retry.
882 * @param cls our "struct GNUNET_CLIENT_TransmissionHandle"
883 * @param size number of bytes available for transmission
884 * @param buf where to write them
885 * @return number of bytes written to buf
887 static size_t client_notify (void *cls, size_t size, void *buf);
891 * This task is run if we should re-try connection to the
892 * service after a while.
894 * @param cls our "struct GNUNET_CLIENT_TransmitHandle" of the request
898 client_delayed_retry (void *cls,
899 const struct GNUNET_SCHEDULER_TaskContext *tc)
901 struct GNUNET_CLIENT_TransmitHandle *th = cls;
903 th->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
904 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
907 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
908 "Transmission failed due to shutdown.\n");
911 th->notify (th->notify_cls, 0, NULL);
915 th->th = GNUNET_CONNECTION_notify_transmit_ready (th->sock->sock,
917 GNUNET_TIME_absolute_get_remaining
923 th->notify (th->notify_cls, 0, NULL);
931 * Connection notifies us about failure or success of a transmission
932 * request. Either pass it on to our user or, if possible, retry.
934 * @param cls our "struct GNUNET_CLIENT_TransmissionHandle"
935 * @param size number of bytes available for transmission
936 * @param buf where to write them
937 * @return number of bytes written to buf
940 client_notify (void *cls, size_t size, void *buf)
942 struct GNUNET_CLIENT_TransmitHandle *th = cls;
944 struct GNUNET_TIME_Relative delay;
950 delay = GNUNET_TIME_absolute_get_remaining (th->timeout);
952 if ( (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & GNUNET_SCHEDULER_get_reason (th->sock->sched))) ||
953 (GNUNET_YES != th->auto_retry) ||
954 (0 == --th->attempts_left) ||
958 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
959 "Transmission failed %u times, giving up.\n",
960 MAX_ATTEMPTS - th->attempts_left);
962 GNUNET_break (0 == th->notify (th->notify_cls, 0, NULL));
967 GNUNET_CONNECTION_destroy (th->sock->sock, GNUNET_NO);
968 th->sock->sock = do_connect (th->sock->sched,
969 th->sock->service_name, th->sock->cfg);
970 GNUNET_assert (NULL != th->sock->sock);
971 GNUNET_CONNECTION_ignore_shutdown (th->sock->sock,
972 th->sock->ignore_shutdown);
973 delay = GNUNET_TIME_relative_min (delay, th->sock->back_off);
975 = GNUNET_TIME_relative_min (GNUNET_TIME_relative_multiply (th->sock->back_off, 2),
976 GNUNET_TIME_UNIT_SECONDS);
978 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
979 "Transmission failed %u times, trying again in %llums.\n",
980 MAX_ATTEMPTS - th->attempts_left,
981 (unsigned long long) delay.value);
983 th->reconnect_task = GNUNET_SCHEDULER_add_delayed (th->sock->sched,
985 &client_delayed_retry,
990 GNUNET_assert (size >= th->size);
991 ret = th->notify (th->notify_cls, size, buf);
998 * Ask the client to call us once the specified number of bytes
999 * are free in the transmission buffer. May call the notify
1000 * method immediately if enough space is available.
1002 * @param sock connection to the service
1003 * @param size number of bytes to send
1004 * @param timeout after how long should we give up (and call
1005 * notify with buf NULL and size 0)?
1006 * @param auto_retry if the connection to the service dies, should we
1007 * automatically re-connect and retry (within the timeout period)
1008 * or should we immediately fail in this case? Pass GNUNET_YES
1009 * if the caller does not care about temporary connection errors,
1010 * for example because the protocol is stateless
1011 * @param notify function to call
1012 * @param notify_cls closure for notify
1013 * @return NULL if our buffer will never hold size bytes,
1014 * a handle if the notify callback was queued (can be used to cancel)
1016 struct GNUNET_CLIENT_TransmitHandle *
1017 GNUNET_CLIENT_notify_transmit_ready (struct GNUNET_CLIENT_Connection *sock,
1019 struct GNUNET_TIME_Relative timeout,
1021 GNUNET_CONNECTION_TransmitReadyNotify
1022 notify, void *notify_cls)
1024 struct GNUNET_CLIENT_TransmitHandle *th;
1026 if (NULL != sock->th)
1028 th = GNUNET_malloc (sizeof (struct GNUNET_CLIENT_TransmitHandle));
1031 th->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1032 th->auto_retry = auto_retry;
1033 th->notify = notify;
1034 th->notify_cls = notify_cls;
1035 th->attempts_left = MAX_ATTEMPTS;
1036 th->th = GNUNET_CONNECTION_notify_transmit_ready (sock->sock,
1039 &client_notify, th);
1052 * Cancel a request for notification.
1054 * @param th handle from the original request.
1057 GNUNET_CLIENT_notify_transmit_ready_cancel (struct
1058 GNUNET_CLIENT_TransmitHandle *th)
1060 if (th->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
1062 GNUNET_break (NULL == th->th);
1063 GNUNET_SCHEDULER_cancel (th->sock->sched, th->reconnect_task);
1064 th->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1068 GNUNET_break (NULL != th->th);
1069 GNUNET_CONNECTION_notify_transmit_ready_cancel (th->th);
1071 th->sock->th = NULL;
1077 * Function called to notify a client about the socket
1078 * begin ready to queue the message. "buf" will be
1079 * NULL and "size" zero if the socket was closed for
1080 * writing in the meantime.
1082 * @param cls closure of type "struct TransmitGetResponseContext*"
1083 * @param size number of bytes available in buf
1084 * @param buf where the callee should write the message
1085 * @return number of bytes written to buf
1088 transmit_for_response (void *cls, size_t size, void *buf)
1090 struct TransmitGetResponseContext *tc = cls;
1093 tc->sock->tag = NULL;
1094 msize = ntohs (tc->hdr->size);
1098 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1099 _("Could not submit request, not expecting to receive a response.\n"));
1101 tc->rn (tc->rn_cls, NULL);
1105 GNUNET_assert (size >= msize);
1106 memcpy (buf, tc->hdr, msize);
1107 GNUNET_CLIENT_receive (tc->sock,
1110 GNUNET_TIME_absolute_get_remaining (tc->timeout));
1117 * Convenience API that combines sending a request
1118 * to the service and waiting for a response.
1119 * If either operation times out, the callback
1120 * will be called with a "NULL" response (in which
1121 * case the connection should probably be destroyed).
1123 * @param sock connection to use
1124 * @param hdr message to transmit
1125 * @param timeout when to give up (for both transmission
1126 * and for waiting for a response)
1127 * @param auto_retry if the connection to the service dies, should we
1128 * automatically re-connect and retry (within the timeout period)
1129 * or should we immediately fail in this case? Pass GNUNET_YES
1130 * if the caller does not care about temporary connection errors,
1131 * for example because the protocol is stateless
1132 * @param rn function to call with the response
1133 * @param rn_cls closure for rn
1134 * @return GNUNET_OK on success, GNUNET_SYSERR if a request
1135 * is already pending
1138 GNUNET_CLIENT_transmit_and_get_response (struct GNUNET_CLIENT_Connection
1140 const struct GNUNET_MessageHeader
1142 struct GNUNET_TIME_Relative timeout,
1144 GNUNET_CLIENT_MessageHandler rn,
1147 struct TransmitGetResponseContext *tc;
1150 if (NULL != sock->th)
1151 return GNUNET_SYSERR;
1152 GNUNET_assert (sock->tag == NULL);
1153 msize = ntohs (hdr->size);
1154 tc = GNUNET_malloc (sizeof (struct TransmitGetResponseContext) + msize);
1156 tc->hdr = (const struct GNUNET_MessageHeader *) &tc[1];
1157 memcpy (&tc[1], hdr, msize);
1158 tc->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1160 tc->rn_cls = rn_cls;
1161 if (NULL == GNUNET_CLIENT_notify_transmit_ready (sock,
1165 &transmit_for_response,
1170 return GNUNET_SYSERR;
1178 /* end of client.c */