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,
266 unsigned int attempt)
268 struct GNUNET_CONNECTION_Handle *sock;
271 unsigned long long port;
275 if (0 == (attempt % 2))
277 /* on even rounds, try UNIX */
280 GNUNET_CONFIGURATION_get_value_string (cfg,
282 "UNIXPATH", &unixpath)) &&
283 (0 < strlen (unixpath)) ) /* We have a non-NULL unixpath, does that mean it's valid? */
285 sock = GNUNET_CONNECTION_create_from_connect_to_unixpath (cfg, unixpath);
289 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Connected to unixpath `%s'!\n", unixpath);
291 GNUNET_free(unixpath);
295 GNUNET_free_non_null (unixpath);
300 GNUNET_CONFIGURATION_get_value_number (cfg,
306 GNUNET_CONFIGURATION_get_value_string (cfg,
308 "HOSTNAME", &hostname)))
310 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
311 _("Could not determine valid hostname and port for service `%s' from configuration.\n"),
315 if (0 == strlen (hostname))
317 GNUNET_free (hostname);
318 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
319 _("Need a non-empty hostname for service `%s'.\n"),
326 if (0 != (attempt % 2))
331 GNUNET_CONFIGURATION_get_value_string (cfg,
333 "UNIXPATH", &unixpath)) &&
334 (0 < strlen (unixpath)))
336 sock = GNUNET_CONNECTION_create_from_connect_to_unixpath (cfg,
340 GNUNET_free (unixpath);
341 GNUNET_free (hostname);
345 GNUNET_free_non_null (unixpath);
349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
350 "Port is 0 for service `%s', UNIXPATH did not work, returning NULL!\n",
353 GNUNET_free (hostname);
357 sock = GNUNET_CONNECTION_create_from_connect (cfg,
360 GNUNET_free (hostname);
366 * Get a connection with a service.
368 * @param service_name name of the service
369 * @param cfg configuration to use
370 * @return NULL on error (service unknown to configuration)
372 struct GNUNET_CLIENT_Connection *
373 GNUNET_CLIENT_connect (const char *service_name,
374 const struct GNUNET_CONFIGURATION_Handle *cfg)
376 struct GNUNET_CLIENT_Connection *ret;
377 struct GNUNET_CONNECTION_Handle *sock;
379 sock = do_connect (service_name,
381 ret = GNUNET_malloc (sizeof (struct GNUNET_CLIENT_Connection));
384 ret->service_name = GNUNET_strdup (service_name);
386 ret->back_off = GNUNET_TIME_UNIT_MILLISECONDS;
392 * Configure this connection to ignore shutdown signals.
394 * @param h client handle
395 * @param do_ignore GNUNET_YES to ignore, GNUNET_NO to restore default
398 GNUNET_CLIENT_ignore_shutdown (struct GNUNET_CLIENT_Connection *h,
401 h->ignore_shutdown = do_ignore;
403 GNUNET_CONNECTION_ignore_shutdown (h->sock,
409 * Destroy connection with the service. This will automatically
410 * cancel any pending "receive" request (however, the handler will
411 * *NOT* be called, not even with a NULL message). Any pending
412 * transmission request will also be cancelled UNLESS the callback for
413 * the transmission request has already been called, in which case the
414 * transmission 'finish_pending_write' argument determines whether or
415 * not the write is guaranteed to complete before the socket is fully
416 * destroyed (unless, of course, there is an error with the server in
417 * which case the message may still be lost).
419 * @param finish_pending_write should a transmission already passed to the
420 * handle be completed?
421 * @param sock handle to the service connection
424 GNUNET_CLIENT_disconnect (struct GNUNET_CLIENT_Connection *sock,
425 int finish_pending_write)
427 if (sock->in_receive == GNUNET_YES)
429 GNUNET_CONNECTION_receive_cancel (sock->sock);
430 sock->in_receive = GNUNET_NO;
432 if (sock->th != NULL)
434 GNUNET_CLIENT_notify_transmit_ready_cancel (sock->th);
437 if (NULL != sock->sock)
439 GNUNET_CONNECTION_destroy (sock->sock, finish_pending_write);
442 if (sock->receive_task != GNUNET_SCHEDULER_NO_TASK)
444 GNUNET_SCHEDULER_cancel (sock->receive_task);
445 sock->receive_task = GNUNET_SCHEDULER_NO_TASK;
447 if (sock->tag != NULL)
449 GNUNET_free (sock->tag);
452 sock->receiver_handler = NULL;
453 GNUNET_array_grow (sock->received_buf, sock->received_size, 0);
454 GNUNET_free (sock->service_name);
460 * Check if message is complete
463 check_complete (struct GNUNET_CLIENT_Connection *conn)
465 if ((conn->received_pos >= sizeof (struct GNUNET_MessageHeader)) &&
466 (conn->received_pos >=
467 ntohs (((const struct GNUNET_MessageHeader *) conn->received_buf)->
469 conn->msg_complete = GNUNET_YES;
474 * Callback function for data received from the network. Note that
475 * both "available" and "errCode" would be 0 if the read simply timed out.
478 * @param buf pointer to received data
479 * @param available number of bytes availabe in "buf",
480 * possibly 0 (on errors)
481 * @param addr address of the sender
482 * @param addrlen size of addr
483 * @param errCode value of errno (on errors receiving)
486 receive_helper (void *cls,
489 const struct sockaddr *addr, socklen_t addrlen, int errCode)
491 struct GNUNET_CLIENT_Connection *conn = cls;
492 struct GNUNET_TIME_Relative remaining;
493 GNUNET_CLIENT_MessageHandler receive_handler;
494 void *receive_handler_cls;
496 GNUNET_assert (conn->msg_complete == GNUNET_NO);
497 conn->in_receive = GNUNET_NO;
498 if ((available == 0) || (conn->sock == NULL) || (errCode != 0))
500 /* signal timeout! */
502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
503 "Timeout in receive_helper, available %u, conn->sock %s, errCode `%s'\n",
504 (unsigned int) available,
505 conn->sock == NULL ? "NULL" : "non-NULL",
508 if (NULL != (receive_handler = conn->receiver_handler))
510 receive_handler_cls = conn->receiver_handler_cls;
511 conn->receiver_handler = NULL;
512 receive_handler (receive_handler_cls, NULL);
517 /* FIXME: optimize for common fast case where buf contains the
518 entire message and we need no copying... */
521 /* slow path: append to array */
522 if (conn->received_size < conn->received_pos + available)
523 GNUNET_array_grow (conn->received_buf,
524 conn->received_size, conn->received_pos + available);
525 memcpy (&conn->received_buf[conn->received_pos], buf, available);
526 conn->received_pos += available;
527 check_complete (conn);
528 /* check for timeout */
529 remaining = GNUNET_TIME_absolute_get_remaining (conn->receive_timeout);
530 if (remaining.rel_value == 0)
532 /* signal timeout! */
533 if (NULL != conn->receiver_handler)
534 conn->receiver_handler (conn->receiver_handler_cls, NULL);
537 /* back to receive -- either for more data or to call callback! */
538 GNUNET_CLIENT_receive (conn,
539 conn->receiver_handler,
540 conn->receiver_handler_cls, remaining);
545 * Continuation to call the receive callback.
547 * @param cls our handle to the client connection
548 * @param tc scheduler context
551 receive_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
553 struct GNUNET_CLIENT_Connection *sock = cls;
554 GNUNET_CLIENT_MessageHandler handler = sock->receiver_handler;
555 const struct GNUNET_MessageHeader *cmsg =
556 (const struct GNUNET_MessageHeader *) sock->received_buf;
557 void *handler_cls = sock->receiver_handler_cls;
558 uint16_t msize = ntohs (cmsg->size);
560 struct GNUNET_MessageHeader *msg = (struct GNUNET_MessageHeader *) mbuf;
563 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
564 "Received message of type %u and size %u\n",
568 sock->receive_task = GNUNET_SCHEDULER_NO_TASK;
569 GNUNET_assert (GNUNET_YES == sock->msg_complete);
570 GNUNET_assert (sock->received_pos >= msize);
571 memcpy (msg, cmsg, msize);
572 memmove (sock->received_buf,
573 &sock->received_buf[msize], sock->received_pos - msize);
574 sock->received_pos -= msize;
575 sock->msg_complete = GNUNET_NO;
576 sock->receiver_handler = NULL;
577 check_complete (sock);
579 handler (handler_cls, msg);
584 * Read from the service.
586 * @param sock the service
587 * @param handler function to call with the message
588 * @param handler_cls closure for handler
589 * @param timeout how long to wait until timing out
592 GNUNET_CLIENT_receive (struct GNUNET_CLIENT_Connection *sock,
593 GNUNET_CLIENT_MessageHandler handler,
594 void *handler_cls, struct GNUNET_TIME_Relative timeout)
596 if (sock->sock == NULL)
598 /* already disconnected, fail instantly! */
599 GNUNET_break (0); /* this should not happen in well-written code! */
601 handler (handler_cls, NULL);
604 sock->receiver_handler = handler;
605 sock->receiver_handler_cls = handler_cls;
606 sock->receive_timeout = GNUNET_TIME_relative_to_absolute (timeout);
607 if (GNUNET_YES == sock->msg_complete)
609 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->receive_task);
610 sock->receive_task = GNUNET_SCHEDULER_add_now (&receive_task, sock);
614 GNUNET_assert (sock->in_receive == GNUNET_NO);
615 sock->in_receive = GNUNET_YES;
617 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "calling GNUNET_CONNECTION_receive\n");
619 GNUNET_CONNECTION_receive (sock->sock,
620 GNUNET_SERVER_MAX_MESSAGE_SIZE - 1,
621 timeout, &receive_helper, sock);
627 * Report service unavailable.
630 service_test_error (GNUNET_SCHEDULER_Task task, void *task_cls)
632 GNUNET_SCHEDULER_add_continuation (task,
634 GNUNET_SCHEDULER_REASON_TIMEOUT);
639 * Receive confirmation from test, service is up.
642 * @param msg message received, NULL on timeout or fatal error
645 confirm_handler (void *cls, const struct GNUNET_MessageHeader *msg)
647 struct GNUNET_CLIENT_Connection *conn = cls;
648 /* We may want to consider looking at the reply in more
649 detail in the future, for example, is this the
650 correct service? FIXME! */
654 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
655 "Received confirmation that service is running.\n");
657 GNUNET_SCHEDULER_add_continuation (conn->test_cb,
659 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
663 service_test_error (conn->test_cb, conn->test_cb_cls);
665 GNUNET_CLIENT_disconnect (conn, GNUNET_NO);
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,
687 "Transmitting `%s' request.\n", "TEST");
689 msg = (struct GNUNET_MessageHeader *) buf;
690 msg->type = htons (GNUNET_MESSAGE_TYPE_TEST);
691 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
692 GNUNET_CLIENT_receive (conn,
695 GNUNET_TIME_absolute_get_remaining (conn->test_deadline));
696 return sizeof (struct GNUNET_MessageHeader);
701 * Wait until the service is running.
703 * @param service name of the service to wait for
704 * @param cfg configuration to use
705 * @param timeout how long to wait at most in ms
706 * @param task task to run if service is running
707 * (reason will be "PREREQ_DONE" (service running)
708 * or "TIMEOUT" (service not known to be running))
709 * @param task_cls closure for task
712 GNUNET_CLIENT_service_test (const char *service,
713 const struct GNUNET_CONFIGURATION_Handle *cfg,
714 struct GNUNET_TIME_Relative timeout,
715 GNUNET_SCHEDULER_Task task, void *task_cls)
717 struct GNUNET_CLIENT_Connection *conn;
720 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
721 "Testing if service `%s' is running.\n", service);
723 conn = GNUNET_CLIENT_connect (service, cfg);
726 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
728 ("Could not connect to service `%s', must not be running.\n"),
730 service_test_error (task, task_cls);
733 conn->test_cb = task;
734 conn->test_cb_cls = task_cls;
735 conn->test_deadline = GNUNET_TIME_relative_to_absolute (timeout);
737 if (NULL == GNUNET_CLIENT_notify_transmit_ready (conn,
738 sizeof (struct GNUNET_MessageHeader),
743 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
744 _("Failure to transmit request to service `%s'\n"),
746 service_test_error (task, task_cls);
747 GNUNET_CLIENT_disconnect (conn, GNUNET_NO);
754 * Connection notifies us about failure or success of
755 * a transmission request. Either pass it on to our
756 * user or, if possible, retry.
758 * @param cls our "struct GNUNET_CLIENT_TransmissionHandle"
759 * @param size number of bytes available for transmission
760 * @param buf where to write them
761 * @return number of bytes written to buf
763 static size_t client_notify (void *cls, size_t size, void *buf);
767 * This task is run if we should re-try connection to the
768 * service after a while.
770 * @param cls our "struct GNUNET_CLIENT_TransmitHandle" of the request
774 client_delayed_retry (void *cls,
775 const struct GNUNET_SCHEDULER_TaskContext *tc)
777 struct GNUNET_CLIENT_TransmitHandle *th = cls;
778 struct GNUNET_TIME_Relative delay;
780 th->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
781 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
784 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
785 "Transmission failed due to shutdown.\n");
788 th->notify (th->notify_cls, 0, NULL);
792 th->sock->sock = do_connect (th->sock->service_name,
794 th->sock->attempts++);
795 if (NULL == th->sock->sock)
797 /* could happen if we're out of sockets */
798 delay = GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining (th->timeout),
801 = GNUNET_TIME_relative_min (GNUNET_TIME_relative_multiply (th->sock->back_off, 2),
802 GNUNET_TIME_UNIT_SECONDS);
804 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
805 "Transmission failed %u times, trying again in %llums.\n",
806 MAX_ATTEMPTS - th->attempts_left,
807 (unsigned long long) delay.rel_value);
809 th->reconnect_task = GNUNET_SCHEDULER_add_delayed (delay,
810 &client_delayed_retry,
814 GNUNET_CONNECTION_ignore_shutdown (th->sock->sock,
815 th->sock->ignore_shutdown);
816 th->th = GNUNET_CONNECTION_notify_transmit_ready (th->sock->sock,
818 GNUNET_TIME_absolute_get_remaining
825 th->notify (th->notify_cls, 0, NULL);
833 * Connection notifies us about failure or success of a transmission
834 * request. Either pass it on to our user or, if possible, retry.
836 * @param cls our "struct GNUNET_CLIENT_TransmissionHandle"
837 * @param size number of bytes available for transmission
838 * @param buf where to write them
839 * @return number of bytes written to buf
842 client_notify (void *cls, size_t size, void *buf)
844 struct GNUNET_CLIENT_TransmitHandle *th = cls;
846 struct GNUNET_TIME_Relative delay;
852 delay = GNUNET_TIME_absolute_get_remaining (th->timeout);
853 delay.rel_value /= 2;
854 if ( (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & GNUNET_SCHEDULER_get_reason ())) ||
855 (GNUNET_YES != th->auto_retry) ||
856 (0 == --th->attempts_left) ||
857 (delay.rel_value < 1) )
860 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
861 "Transmission failed %u times, giving up.\n",
862 MAX_ATTEMPTS - th->attempts_left);
864 GNUNET_break (0 == th->notify (th->notify_cls, 0, NULL));
870 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
871 "Failed to connect to `%s', automatically trying again.\n",
872 th->sock->service_name);
874 GNUNET_CONNECTION_destroy (th->sock->sock, GNUNET_NO);
875 th->sock->sock = NULL;
876 delay = GNUNET_TIME_relative_min (delay, th->sock->back_off);
878 = GNUNET_TIME_relative_min (GNUNET_TIME_relative_multiply (th->sock->back_off, 2),
879 GNUNET_TIME_UNIT_SECONDS);
881 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
882 "Transmission failed %u times, trying again in %llums.\n",
883 MAX_ATTEMPTS - th->attempts_left,
884 (unsigned long long) delay.rel_value);
887 th->reconnect_task = GNUNET_SCHEDULER_add_delayed (delay,
888 &client_delayed_retry,
892 GNUNET_assert (size >= th->size);
893 ret = th->notify (th->notify_cls, size, buf);
900 * Ask the client to call us once the specified number of bytes
901 * are free in the transmission buffer. May call the notify
902 * method immediately if enough space is available.
904 * @param sock connection to the service
905 * @param size number of bytes to send
906 * @param timeout after how long should we give up (and call
907 * notify with buf NULL and size 0)?
908 * @param auto_retry if the connection to the service dies, should we
909 * automatically re-connect and retry (within the timeout period)
910 * or should we immediately fail in this case? Pass GNUNET_YES
911 * if the caller does not care about temporary connection errors,
912 * for example because the protocol is stateless
913 * @param notify function to call
914 * @param notify_cls closure for notify
915 * @return NULL if our buffer will never hold size bytes,
916 * a handle if the notify callback was queued (can be used to cancel)
918 struct GNUNET_CLIENT_TransmitHandle *
919 GNUNET_CLIENT_notify_transmit_ready (struct GNUNET_CLIENT_Connection *sock,
921 struct GNUNET_TIME_Relative timeout,
923 GNUNET_CONNECTION_TransmitReadyNotify
924 notify, void *notify_cls)
926 struct GNUNET_CLIENT_TransmitHandle *th;
928 if (NULL != sock->th)
930 /* If this breaks, you most likley called this function twice without waiting
931 * for completion or canceling the request */
935 th = GNUNET_malloc (sizeof (struct GNUNET_CLIENT_TransmitHandle));
938 th->timeout = GNUNET_TIME_relative_to_absolute (timeout);
939 th->auto_retry = auto_retry;
941 th->notify_cls = notify_cls;
942 th->attempts_left = MAX_ATTEMPTS;
944 if (sock->sock == NULL)
946 th->reconnect_task = GNUNET_SCHEDULER_add_delayed (sock->back_off,
947 &client_delayed_retry,
953 th->th = GNUNET_CONNECTION_notify_transmit_ready (sock->sock,
970 * Cancel a request for notification.
972 * @param th handle from the original request.
975 GNUNET_CLIENT_notify_transmit_ready_cancel (struct
976 GNUNET_CLIENT_TransmitHandle *th)
978 if (th->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
980 GNUNET_assert (NULL == th->th);
981 GNUNET_SCHEDULER_cancel (th->reconnect_task);
982 th->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
986 GNUNET_assert (NULL != th->th);
987 GNUNET_CONNECTION_notify_transmit_ready_cancel (th->th);
995 * Function called to notify a client about the socket
996 * begin ready to queue the message. "buf" will be
997 * NULL and "size" zero if the socket was closed for
998 * writing in the meantime.
1000 * @param cls closure of type "struct TransmitGetResponseContext*"
1001 * @param size number of bytes available in buf
1002 * @param buf where the callee should write the message
1003 * @return number of bytes written to buf
1006 transmit_for_response (void *cls, size_t size, void *buf)
1008 struct TransmitGetResponseContext *tc = cls;
1011 tc->sock->tag = NULL;
1012 msize = ntohs (tc->hdr->size);
1016 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1017 _("Could not submit request, not expecting to receive a response.\n"));
1020 tc->rn (tc->rn_cls, NULL);
1024 GNUNET_assert (size >= msize);
1025 memcpy (buf, tc->hdr, msize);
1026 GNUNET_CLIENT_receive (tc->sock,
1029 GNUNET_TIME_absolute_get_remaining (tc->timeout));
1036 * Convenience API that combines sending a request
1037 * to the service and waiting for a response.
1038 * If either operation times out, the callback
1039 * will be called with a "NULL" response (in which
1040 * case the connection should probably be destroyed).
1042 * @param sock connection to use
1043 * @param hdr message to transmit
1044 * @param timeout when to give up (for both transmission
1045 * and for waiting for a response)
1046 * @param auto_retry if the connection to the service dies, should we
1047 * automatically re-connect and retry (within the timeout period)
1048 * or should we immediately fail in this case? Pass GNUNET_YES
1049 * if the caller does not care about temporary connection errors,
1050 * for example because the protocol is stateless
1051 * @param rn function to call with the response
1052 * @param rn_cls closure for rn
1053 * @return GNUNET_OK on success, GNUNET_SYSERR if a request
1054 * is already pending
1057 GNUNET_CLIENT_transmit_and_get_response (struct GNUNET_CLIENT_Connection
1059 const struct GNUNET_MessageHeader
1061 struct GNUNET_TIME_Relative timeout,
1063 GNUNET_CLIENT_MessageHandler rn,
1066 struct TransmitGetResponseContext *tc;
1069 if (NULL != sock->th)
1070 return GNUNET_SYSERR;
1071 GNUNET_assert (sock->tag == NULL);
1072 msize = ntohs (hdr->size);
1073 tc = GNUNET_malloc (sizeof (struct TransmitGetResponseContext) + msize);
1075 tc->hdr = (const struct GNUNET_MessageHeader *) &tc[1];
1076 memcpy (&tc[1], hdr, msize);
1077 tc->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1079 tc->rn_cls = rn_cls;
1080 if (NULL == GNUNET_CLIENT_notify_transmit_ready (sock,
1084 &transmit_for_response,
1089 return GNUNET_SYSERR;
1097 /* end of client.c */