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 */
279 GNUNET_CONFIGURATION_get_value_string (cfg,
281 "UNIXPATH", &unixpath)) &&
282 (0 < strlen (unixpath))) /* We have a non-NULL unixpath, does that mean it's valid? */
284 sock = GNUNET_CONNECTION_create_from_connect_to_unixpath (cfg, unixpath);
288 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Connected to unixpath `%s'!\n", unixpath);
290 GNUNET_free(unixpath);
294 GNUNET_free_non_null (unixpath);
299 GNUNET_CONFIGURATION_get_value_number (cfg,
305 GNUNET_CONFIGURATION_get_value_string (cfg,
307 "HOSTNAME", &hostname)))
309 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
310 _("Could not determine valid hostname and port for service `%s' from configuration.\n"),
314 if (0 == strlen (hostname))
316 GNUNET_free (hostname);
317 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
318 _("Need a non-empty hostname for service `%s'.\n"),
325 if (0 != (attempt % 2))
329 GNUNET_CONFIGURATION_get_value_string (cfg,
331 "UNIXPATH", &unixpath)) &&
332 (0 < strlen (unixpath)))
334 sock = GNUNET_CONNECTION_create_from_connect_to_unixpath (cfg,
336 GNUNET_free (unixpath);
339 GNUNET_free (hostname);
346 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
347 "Port is 0 for service `%s', UNIXPATH did not work, returning NULL!\n",
350 GNUNET_free (hostname);
354 sock = GNUNET_CONNECTION_create_from_connect (cfg,
357 GNUNET_free (hostname);
363 * Get a connection with a service.
365 * @param service_name name of the service
366 * @param cfg configuration to use
367 * @return NULL on error (service unknown to configuration)
369 struct GNUNET_CLIENT_Connection *
370 GNUNET_CLIENT_connect (const char *service_name,
371 const struct GNUNET_CONFIGURATION_Handle *cfg)
373 struct GNUNET_CLIENT_Connection *ret;
374 struct GNUNET_CONNECTION_Handle *sock;
376 sock = do_connect (service_name,
378 ret = GNUNET_malloc (sizeof (struct GNUNET_CLIENT_Connection));
381 ret->service_name = GNUNET_strdup (service_name);
383 ret->back_off = GNUNET_TIME_UNIT_MILLISECONDS;
389 * Configure this connection to ignore shutdown signals.
391 * @param h client handle
392 * @param do_ignore GNUNET_YES to ignore, GNUNET_NO to restore default
395 GNUNET_CLIENT_ignore_shutdown (struct GNUNET_CLIENT_Connection *h,
398 h->ignore_shutdown = do_ignore;
400 GNUNET_CONNECTION_ignore_shutdown (h->sock,
406 * Destroy connection with the service. This will automatically
407 * cancel any pending "receive" request (however, the handler will
408 * *NOT* be called, not even with a NULL message). Any pending
409 * transmission request will also be cancelled UNLESS the callback for
410 * the transmission request has already been called, in which case the
411 * transmission 'finish_pending_write' argument determines whether or
412 * not the write is guaranteed to complete before the socket is fully
413 * destroyed (unless, of course, there is an error with the server in
414 * which case the message may still be lost).
416 * @param finish_pending_write should a transmission already passed to the
417 * handle be completed?
418 * @param sock handle to the service connection
421 GNUNET_CLIENT_disconnect (struct GNUNET_CLIENT_Connection *sock,
422 int finish_pending_write)
424 if (sock->in_receive == GNUNET_YES)
426 GNUNET_CONNECTION_receive_cancel (sock->sock);
427 sock->in_receive = GNUNET_NO;
429 if (NULL != sock->sock)
431 GNUNET_CONNECTION_destroy (sock->sock, finish_pending_write);
434 if (sock->tag != NULL)
436 GNUNET_free (sock->tag);
439 sock->receiver_handler = NULL;
440 if (sock->th != NULL)
442 GNUNET_CLIENT_notify_transmit_ready_cancel (sock->th);
445 if (sock->receive_task != GNUNET_SCHEDULER_NO_TASK)
447 GNUNET_SCHEDULER_cancel (sock->receive_task);
448 sock->receive_task = GNUNET_SCHEDULER_NO_TASK;
450 GNUNET_array_grow (sock->received_buf, sock->received_size, 0);
451 GNUNET_free (sock->service_name);
457 * Check if message is complete
460 check_complete (struct GNUNET_CLIENT_Connection *conn)
462 if ((conn->received_pos >= sizeof (struct GNUNET_MessageHeader)) &&
463 (conn->received_pos >=
464 ntohs (((const struct GNUNET_MessageHeader *) conn->received_buf)->
466 conn->msg_complete = GNUNET_YES;
471 * Callback function for data received from the network. Note that
472 * both "available" and "errCode" would be 0 if the read simply timed out.
475 * @param buf pointer to received data
476 * @param available number of bytes availabe in "buf",
477 * possibly 0 (on errors)
478 * @param addr address of the sender
479 * @param addrlen size of addr
480 * @param errCode value of errno (on errors receiving)
483 receive_helper (void *cls,
486 const struct sockaddr *addr, socklen_t addrlen, int errCode)
488 struct GNUNET_CLIENT_Connection *conn = cls;
489 struct GNUNET_TIME_Relative remaining;
490 GNUNET_CLIENT_MessageHandler receive_handler;
491 void *receive_handler_cls;
493 GNUNET_assert (conn->msg_complete == GNUNET_NO);
494 conn->in_receive = GNUNET_NO;
495 if ((available == 0) || (conn->sock == NULL) || (errCode != 0))
497 /* signal timeout! */
499 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
500 "Timeout in receive_helper, available %u, conn->sock %s, errCode `%s'\n",
501 (unsigned int) available,
502 conn->sock == NULL ? "NULL" : "non-NULL",
505 if (NULL != (receive_handler = conn->receiver_handler))
507 receive_handler_cls = conn->receiver_handler_cls;
508 conn->receiver_handler = NULL;
509 receive_handler (receive_handler_cls, NULL);
514 /* FIXME: optimize for common fast case where buf contains the
515 entire message and we need no copying... */
518 /* slow path: append to array */
519 if (conn->received_size < conn->received_pos + available)
520 GNUNET_array_grow (conn->received_buf,
521 conn->received_size, conn->received_pos + available);
522 memcpy (&conn->received_buf[conn->received_pos], buf, available);
523 conn->received_pos += available;
524 check_complete (conn);
525 /* check for timeout */
526 remaining = GNUNET_TIME_absolute_get_remaining (conn->receive_timeout);
527 if (remaining.rel_value == 0)
529 /* signal timeout! */
530 conn->receiver_handler (conn->receiver_handler_cls, NULL);
533 /* back to receive -- either for more data or to call callback! */
534 GNUNET_CLIENT_receive (conn,
535 conn->receiver_handler,
536 conn->receiver_handler_cls, remaining);
541 * Continuation to call the receive callback.
543 * @param cls our handle to the client connection
544 * @param tc scheduler context
547 receive_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
549 struct GNUNET_CLIENT_Connection *sock = cls;
550 GNUNET_CLIENT_MessageHandler handler = sock->receiver_handler;
551 const struct GNUNET_MessageHeader *cmsg =
552 (const struct GNUNET_MessageHeader *) sock->received_buf;
553 void *handler_cls = sock->receiver_handler_cls;
554 uint16_t msize = ntohs (cmsg->size);
556 struct GNUNET_MessageHeader *msg = (struct GNUNET_MessageHeader *) mbuf;
559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
560 "Received message of type %u and size %u\n",
564 sock->receive_task = GNUNET_SCHEDULER_NO_TASK;
565 GNUNET_assert (GNUNET_YES == sock->msg_complete);
566 GNUNET_assert (sock->received_pos >= msize);
567 memcpy (msg, cmsg, msize);
568 memmove (sock->received_buf,
569 &sock->received_buf[msize], sock->received_pos - msize);
570 sock->received_pos -= msize;
571 sock->msg_complete = GNUNET_NO;
572 sock->receiver_handler = NULL;
573 check_complete (sock);
575 handler (handler_cls, msg);
580 * Read from the service.
582 * @param sock the service
583 * @param handler function to call with the message
584 * @param handler_cls closure for handler
585 * @param timeout how long to wait until timing out
588 GNUNET_CLIENT_receive (struct GNUNET_CLIENT_Connection *sock,
589 GNUNET_CLIENT_MessageHandler handler,
590 void *handler_cls, struct GNUNET_TIME_Relative timeout)
592 if (sock->sock == NULL)
594 /* already disconnected, fail instantly! */
595 GNUNET_break (0); /* this should not happen in well-written code! */
596 handler (handler_cls, NULL);
599 sock->receiver_handler = handler;
600 sock->receiver_handler_cls = handler_cls;
601 sock->receive_timeout = GNUNET_TIME_relative_to_absolute (timeout);
602 if (GNUNET_YES == sock->msg_complete)
604 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->receive_task);
605 sock->receive_task = GNUNET_SCHEDULER_add_now (&receive_task, sock);
609 GNUNET_assert (sock->in_receive == GNUNET_NO);
610 sock->in_receive = GNUNET_YES;
612 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "calling GNUNET_CONNECTION_receive\n");
614 GNUNET_CONNECTION_receive (sock->sock,
615 GNUNET_SERVER_MAX_MESSAGE_SIZE - 1,
616 timeout, &receive_helper, sock);
622 * Report service unavailable.
625 service_test_error (GNUNET_SCHEDULER_Task task, void *task_cls)
627 GNUNET_SCHEDULER_add_continuation (task,
629 GNUNET_SCHEDULER_REASON_TIMEOUT);
634 * Receive confirmation from test, service is up.
637 * @param msg message received, NULL on timeout or fatal error
640 confirm_handler (void *cls, const struct GNUNET_MessageHeader *msg)
642 struct GNUNET_CLIENT_Connection *conn = cls;
643 /* We may want to consider looking at the reply in more
644 detail in the future, for example, is this the
645 correct service? FIXME! */
649 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
650 "Received confirmation that service is running.\n");
652 GNUNET_SCHEDULER_add_continuation (conn->test_cb,
654 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
658 service_test_error (conn->test_cb, conn->test_cb_cls);
660 GNUNET_CLIENT_disconnect (conn, GNUNET_NO);
665 write_test (void *cls, size_t size, void *buf)
667 struct GNUNET_CLIENT_Connection *conn = cls;
668 struct GNUNET_MessageHeader *msg;
670 if (size < sizeof (struct GNUNET_MessageHeader))
673 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
674 _("Failure to transmit TEST request.\n"));
676 service_test_error (conn->test_cb, conn->test_cb_cls);
677 GNUNET_CLIENT_disconnect (conn, GNUNET_NO);
678 return 0; /* client disconnected */
681 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
682 "Transmitting `%s' request.\n", "TEST");
684 msg = (struct GNUNET_MessageHeader *) buf;
685 msg->type = htons (GNUNET_MESSAGE_TYPE_TEST);
686 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
687 GNUNET_CLIENT_receive (conn,
690 GNUNET_TIME_absolute_get_remaining (conn->test_deadline));
691 return sizeof (struct GNUNET_MessageHeader);
696 * Wait until the service is running.
698 * @param service name of the service to wait for
699 * @param cfg configuration to use
700 * @param timeout how long to wait at most in ms
701 * @param task task to run if service is running
702 * (reason will be "PREREQ_DONE" (service running)
703 * or "TIMEOUT" (service not known to be running))
704 * @param task_cls closure for task
707 GNUNET_CLIENT_service_test (const char *service,
708 const struct GNUNET_CONFIGURATION_Handle *cfg,
709 struct GNUNET_TIME_Relative timeout,
710 GNUNET_SCHEDULER_Task task, void *task_cls)
712 struct GNUNET_CLIENT_Connection *conn;
715 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
716 "Testing if service `%s' is running.\n", service);
718 conn = GNUNET_CLIENT_connect (service, cfg);
721 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
723 ("Could not connect to service `%s', must not be running.\n"),
725 service_test_error (task, task_cls);
728 conn->test_cb = task;
729 conn->test_cb_cls = task_cls;
730 conn->test_deadline = GNUNET_TIME_relative_to_absolute (timeout);
732 if (NULL == GNUNET_CLIENT_notify_transmit_ready (conn,
733 sizeof (struct GNUNET_MessageHeader),
738 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
739 _("Failure to transmit request to service `%s'\n"),
741 service_test_error (task, task_cls);
742 GNUNET_CLIENT_disconnect (conn, GNUNET_NO);
749 * Connection notifies us about failure or success of
750 * a transmission request. Either pass it on to our
751 * user or, if possible, retry.
753 * @param cls our "struct GNUNET_CLIENT_TransmissionHandle"
754 * @param size number of bytes available for transmission
755 * @param buf where to write them
756 * @return number of bytes written to buf
758 static size_t client_notify (void *cls, size_t size, void *buf);
762 * This task is run if we should re-try connection to the
763 * service after a while.
765 * @param cls our "struct GNUNET_CLIENT_TransmitHandle" of the request
769 client_delayed_retry (void *cls,
770 const struct GNUNET_SCHEDULER_TaskContext *tc)
772 struct GNUNET_CLIENT_TransmitHandle *th = cls;
773 struct GNUNET_TIME_Relative delay;
775 th->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
776 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
779 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
780 "Transmission failed due to shutdown.\n");
783 th->notify (th->notify_cls, 0, NULL);
787 th->sock->sock = do_connect (th->sock->service_name,
789 th->sock->attempts++);
790 if (NULL == th->sock->sock)
792 /* could happen if we're out of sockets */
793 delay = GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining (th->timeout),
796 = GNUNET_TIME_relative_min (GNUNET_TIME_relative_multiply (th->sock->back_off, 2),
797 GNUNET_TIME_UNIT_SECONDS);
799 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
800 "Transmission failed %u times, trying again in %llums.\n",
801 MAX_ATTEMPTS - th->attempts_left,
802 (unsigned long long) delay.rel_value);
804 th->reconnect_task = GNUNET_SCHEDULER_add_delayed (delay,
805 &client_delayed_retry,
809 GNUNET_CONNECTION_ignore_shutdown (th->sock->sock,
810 th->sock->ignore_shutdown);
811 th->th = GNUNET_CONNECTION_notify_transmit_ready (th->sock->sock,
813 GNUNET_TIME_absolute_get_remaining
819 th->notify (th->notify_cls, 0, NULL);
827 * Connection notifies us about failure or success of a transmission
828 * request. Either pass it on to our user or, if possible, retry.
830 * @param cls our "struct GNUNET_CLIENT_TransmissionHandle"
831 * @param size number of bytes available for transmission
832 * @param buf where to write them
833 * @return number of bytes written to buf
836 client_notify (void *cls, size_t size, void *buf)
838 struct GNUNET_CLIENT_TransmitHandle *th = cls;
840 struct GNUNET_TIME_Relative delay;
846 delay = GNUNET_TIME_absolute_get_remaining (th->timeout);
847 delay.rel_value /= 2;
848 if ( (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & GNUNET_SCHEDULER_get_reason ())) ||
849 (GNUNET_YES != th->auto_retry) ||
850 (0 == --th->attempts_left) ||
851 (delay.rel_value < 1) )
854 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
855 "Transmission failed %u times, giving up.\n",
856 MAX_ATTEMPTS - th->attempts_left);
858 GNUNET_break (0 == th->notify (th->notify_cls, 0, NULL));
864 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
865 "Failed to connect to `%s', automatically trying again.\n",
866 th->sock->service_name);
868 GNUNET_CONNECTION_destroy (th->sock->sock, GNUNET_NO);
869 th->sock->sock = NULL;
870 delay = GNUNET_TIME_relative_min (delay, th->sock->back_off);
872 = GNUNET_TIME_relative_min (GNUNET_TIME_relative_multiply (th->sock->back_off, 2),
873 GNUNET_TIME_UNIT_SECONDS);
875 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
876 "Transmission failed %u times, trying again in %llums.\n",
877 MAX_ATTEMPTS - th->attempts_left,
878 (unsigned long long) delay.rel_value);
880 th->reconnect_task = GNUNET_SCHEDULER_add_delayed (delay,
881 &client_delayed_retry,
886 GNUNET_assert (size >= th->size);
887 ret = th->notify (th->notify_cls, size, buf);
894 * Ask the client to call us once the specified number of bytes
895 * are free in the transmission buffer. May call the notify
896 * method immediately if enough space is available.
898 * @param sock connection to the service
899 * @param size number of bytes to send
900 * @param timeout after how long should we give up (and call
901 * notify with buf NULL and size 0)?
902 * @param auto_retry if the connection to the service dies, should we
903 * automatically re-connect and retry (within the timeout period)
904 * or should we immediately fail in this case? Pass GNUNET_YES
905 * if the caller does not care about temporary connection errors,
906 * for example because the protocol is stateless
907 * @param notify function to call
908 * @param notify_cls closure for notify
909 * @return NULL if our buffer will never hold size bytes,
910 * a handle if the notify callback was queued (can be used to cancel)
912 struct GNUNET_CLIENT_TransmitHandle *
913 GNUNET_CLIENT_notify_transmit_ready (struct GNUNET_CLIENT_Connection *sock,
915 struct GNUNET_TIME_Relative timeout,
917 GNUNET_CONNECTION_TransmitReadyNotify
918 notify, void *notify_cls)
920 struct GNUNET_CLIENT_TransmitHandle *th;
922 if (NULL != sock->th)
924 /* If this breaks, you most likley called this function twice without waiting
925 * for completion or canceling the request */
929 th = GNUNET_malloc (sizeof (struct GNUNET_CLIENT_TransmitHandle));
932 th->timeout = GNUNET_TIME_relative_to_absolute (timeout);
933 th->auto_retry = auto_retry;
935 th->notify_cls = notify_cls;
936 th->attempts_left = MAX_ATTEMPTS;
937 if (sock->sock == NULL)
939 th->reconnect_task = GNUNET_SCHEDULER_add_delayed (sock->back_off,
940 &client_delayed_retry,
946 th->th = GNUNET_CONNECTION_notify_transmit_ready (sock->sock,
963 * Cancel a request for notification.
965 * @param th handle from the original request.
968 GNUNET_CLIENT_notify_transmit_ready_cancel (struct
969 GNUNET_CLIENT_TransmitHandle *th)
971 if (th->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
973 GNUNET_break (NULL == th->th);
974 GNUNET_SCHEDULER_cancel (th->reconnect_task);
975 th->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
979 GNUNET_assert (NULL != th->th);
980 GNUNET_CONNECTION_notify_transmit_ready_cancel (th->th);
988 * Function called to notify a client about the socket
989 * begin ready to queue the message. "buf" will be
990 * NULL and "size" zero if the socket was closed for
991 * writing in the meantime.
993 * @param cls closure of type "struct TransmitGetResponseContext*"
994 * @param size number of bytes available in buf
995 * @param buf where the callee should write the message
996 * @return number of bytes written to buf
999 transmit_for_response (void *cls, size_t size, void *buf)
1001 struct TransmitGetResponseContext *tc = cls;
1004 tc->sock->tag = NULL;
1005 msize = ntohs (tc->hdr->size);
1009 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1010 _("Could not submit request, not expecting to receive a response.\n"));
1012 tc->rn (tc->rn_cls, NULL);
1016 GNUNET_assert (size >= msize);
1017 memcpy (buf, tc->hdr, msize);
1018 GNUNET_CLIENT_receive (tc->sock,
1021 GNUNET_TIME_absolute_get_remaining (tc->timeout));
1028 * Convenience API that combines sending a request
1029 * to the service and waiting for a response.
1030 * If either operation times out, the callback
1031 * will be called with a "NULL" response (in which
1032 * case the connection should probably be destroyed).
1034 * @param sock connection to use
1035 * @param hdr message to transmit
1036 * @param timeout when to give up (for both transmission
1037 * and for waiting for a response)
1038 * @param auto_retry if the connection to the service dies, should we
1039 * automatically re-connect and retry (within the timeout period)
1040 * or should we immediately fail in this case? Pass GNUNET_YES
1041 * if the caller does not care about temporary connection errors,
1042 * for example because the protocol is stateless
1043 * @param rn function to call with the response
1044 * @param rn_cls closure for rn
1045 * @return GNUNET_OK on success, GNUNET_SYSERR if a request
1046 * is already pending
1049 GNUNET_CLIENT_transmit_and_get_response (struct GNUNET_CLIENT_Connection
1051 const struct GNUNET_MessageHeader
1053 struct GNUNET_TIME_Relative timeout,
1055 GNUNET_CLIENT_MessageHandler rn,
1058 struct TransmitGetResponseContext *tc;
1061 if (NULL != sock->th)
1062 return GNUNET_SYSERR;
1063 GNUNET_assert (sock->tag == NULL);
1064 msize = ntohs (hdr->size);
1065 tc = GNUNET_malloc (sizeof (struct TransmitGetResponseContext) + msize);
1067 tc->hdr = (const struct GNUNET_MessageHeader *) &tc[1];
1068 memcpy (&tc[1], hdr, msize);
1069 tc->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1071 tc->rn_cls = rn_cls;
1072 if (NULL == GNUNET_CLIENT_notify_transmit_ready (sock,
1076 &transmit_for_response,
1081 return GNUNET_SYSERR;
1089 /* end of client.c */