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
46 #define UNIXPATH_RETRIES 0
50 * Handle for a transmission request.
52 struct GNUNET_CLIENT_TransmitHandle
57 struct GNUNET_CLIENT_Connection *sock;
60 * Function to call to get the data for transmission.
62 GNUNET_CONNECTION_TransmitReadyNotify notify;
70 * Handle to the transmission with the underlying
73 struct GNUNET_CONNECTION_TransmitHandle *th;
76 * If we are re-trying and are delaying to do so,
77 * handle to the scheduled task managing the delay.
79 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
82 * Timeout for the operation overall.
84 struct GNUNET_TIME_Absolute timeout;
87 * Number of bytes requested.
92 * Are we allowed to re-try to connect without telling
93 * the user (of this API) about the connection troubles?
98 * Number of attempts left for transmitting the request. We may
99 * fail the first time (say because the service is not yet up), in
100 * which case (if auto_retry is set) we wait a bit and re-try
101 * (timeout permitting).
103 unsigned int attempts_left;
109 * Context for processing
110 * "GNUNET_CLIENT_transmit_and_get_response" requests.
112 struct TransmitGetResponseContext
117 struct GNUNET_CLIENT_Connection *sock;
120 * Message to transmit; do not free, allocated
121 * right after this struct.
123 const struct GNUNET_MessageHeader *hdr;
128 struct GNUNET_TIME_Absolute timeout;
131 * Function to call when done.
133 GNUNET_CLIENT_MessageHandler rn;
142 * Struct to refer to a GNUnet TCP connection.
143 * This is more than just a socket because if the server
144 * drops the connection, the client automatically tries
145 * to reconnect (and for that needs connection information).
147 struct GNUNET_CLIENT_Connection
151 * the socket handle, NULL if not live
153 struct GNUNET_CONNECTION_Handle *sock;
157 * FIXME: why do we DUP the configuration? Avoid this!
159 struct GNUNET_CONFIGURATION_Handle *cfg;
162 * Name of the service we interact with.
167 * Context of a transmit_and_get_response operation, NULL
168 * if no such operation is pending.
170 struct TransmitGetResponseContext *tag;
173 * Handler for current receiver task.
175 GNUNET_CLIENT_MessageHandler receiver_handler;
178 * Closure for receiver_handler.
180 void *receiver_handler_cls;
183 * Handle for a pending transmission request, NULL if there is
186 struct GNUNET_CLIENT_TransmitHandle *th;
189 * Handler for service test completion (NULL unless in service_test)
191 GNUNET_SCHEDULER_Task test_cb;
194 * Deadline for calling 'test_cb'.
196 struct GNUNET_TIME_Absolute test_deadline;
199 * If we are re-trying and are delaying to do so,
200 * handle to the scheduled task managing the delay.
202 GNUNET_SCHEDULER_TaskIdentifier receive_task;
205 * Closure for test_cb (NULL unless in service_test)
210 * Buffer for received message.
215 * Timeout for receiving a response (absolute time).
217 struct GNUNET_TIME_Absolute receive_timeout;
220 * Current value for our incremental back-off (for
223 struct GNUNET_TIME_Relative back_off;
226 * Number of bytes in received_buf that are valid.
231 * Size of received_buf.
233 unsigned int received_size;
236 * Do we have a complete response in received_buf?
241 * Are we currently busy doing receive-processing?
242 * GNUNET_YES if so, GNUNET_NO if not.
247 * Are we ignoring shutdown signals?
252 * How often have we tried to connect?
254 unsigned int attempts;
260 * Try to connect to the service.
262 * @param service_name name of service to connect to
263 * @param cfg configuration to use
264 * @param attempt counter used to alternate between IP and UNIX domain sockets
265 * @return NULL on error
267 static struct GNUNET_CONNECTION_Handle *
268 do_connect (const char *service_name,
269 const struct GNUNET_CONFIGURATION_Handle *cfg,
270 unsigned int attempt)
272 struct GNUNET_CONNECTION_Handle *sock;
275 unsigned long long port;
280 if (0 == (attempt % 2))
282 /* on even rounds, try UNIX */
284 GNUNET_CONFIGURATION_get_value_string (cfg,
286 "UNIXPATH", &unixpath)) &&
287 (0 < strlen (unixpath))) /* We have a non-NULL unixpath, does that mean it's valid? */
290 sock = GNUNET_CONNECTION_create_from_connect_to_unixpath (cfg, unixpath);
291 while ((NULL == sock) && (count < UNIXPATH_RETRIES))
294 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Failed to connect to unixpath `%s', retrying!\n", unixpath);
298 sock = GNUNET_CONNECTION_create_from_connect_to_unixpath (cfg, unixpath);
301 GNUNET_free (unixpath);
305 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Connected to unixpath `%s'!\n", unixpath);
314 GNUNET_CONFIGURATION_get_value_number (cfg,
320 GNUNET_CONFIGURATION_get_value_string (cfg,
322 "HOSTNAME", &hostname)))
324 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
326 ("Could not determine valid hostname and port for service `%s' from configuration.\n"),
330 if (0 == strlen (hostname))
332 GNUNET_free (hostname);
333 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
334 _("Need a non-empty hostname for service `%s'.\n"),
341 if (0 != (attempt % 2))
345 GNUNET_CONFIGURATION_get_value_string (cfg,
347 "UNIXPATH", &unixpath)) &&
348 (0 < strlen (unixpath)))
350 sock = GNUNET_CONNECTION_create_from_connect_to_unixpath (cfg,
352 GNUNET_free (unixpath);
360 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Port is 0 for service `%s', unixpath didn't work, returning NULL(!)!\n", service_name);
363 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Creating from connect!\n");
364 sock = GNUNET_CONNECTION_create_from_connect (cfg,
367 GNUNET_free (hostname);
373 * Get a connection with a service.
375 * @param service_name name of the service
376 * @param cfg configuration to use
377 * @return NULL on error (service unknown to configuration)
379 struct GNUNET_CLIENT_Connection *
380 GNUNET_CLIENT_connect (const char *service_name,
381 const struct GNUNET_CONFIGURATION_Handle *cfg)
383 struct GNUNET_CLIENT_Connection *ret;
384 struct GNUNET_CONNECTION_Handle *sock;
386 sock = do_connect (service_name,
390 ret = GNUNET_malloc (sizeof (struct GNUNET_CLIENT_Connection));
393 ret->service_name = GNUNET_strdup (service_name);
394 ret->cfg = GNUNET_CONFIGURATION_dup (cfg);
395 ret->back_off = GNUNET_TIME_UNIT_MILLISECONDS;
401 * Configure this connection to ignore shutdown signals.
403 * @param h client handle
404 * @param do_ignore GNUNET_YES to ignore, GNUNET_NO to restore default
407 GNUNET_CLIENT_ignore_shutdown (struct GNUNET_CLIENT_Connection *h,
410 h->ignore_shutdown = do_ignore;
412 GNUNET_CONNECTION_ignore_shutdown (h->sock,
418 * Destroy connection with the service. This will automatically
419 * cancel any pending "receive" request (however, the handler will
420 * *NOT* be called, not even with a NULL message). Any pending
421 * transmission request will also be cancelled UNLESS the callback for
422 * the transmission request has already been called, in which case the
423 * transmission 'finish_pending_write' argument determines whether or
424 * not the write is guaranteed to complete before the socket is fully
425 * destroyed (unless, of course, there is an error with the server in
426 * which case the message may still be lost).
428 * @param finish_pending_write should a transmission already passed to the
429 * handle be completed?
430 * @param sock handle to the service connection
433 GNUNET_CLIENT_disconnect (struct GNUNET_CLIENT_Connection *sock,
434 int finish_pending_write)
436 GNUNET_assert (sock->sock != NULL);
437 if (sock->in_receive == GNUNET_YES)
439 GNUNET_CONNECTION_receive_cancel (sock->sock);
440 sock->in_receive = GNUNET_NO;
442 GNUNET_CONNECTION_destroy (sock->sock, finish_pending_write);
444 if (sock->tag != NULL)
446 GNUNET_free (sock->tag);
449 sock->receiver_handler = NULL;
450 if (sock->th != NULL)
451 GNUNET_CLIENT_notify_transmit_ready_cancel (sock->th);
452 if (sock->receive_task != GNUNET_SCHEDULER_NO_TASK)
454 GNUNET_SCHEDULER_cancel (sock->receive_task);
455 sock->receive_task = GNUNET_SCHEDULER_NO_TASK;
457 GNUNET_array_grow (sock->received_buf, sock->received_size, 0);
458 GNUNET_free (sock->service_name);
459 GNUNET_CONFIGURATION_destroy (sock->cfg);
465 * Check if message is complete
468 check_complete (struct GNUNET_CLIENT_Connection *conn)
470 if ((conn->received_pos >= sizeof (struct GNUNET_MessageHeader)) &&
471 (conn->received_pos >=
472 ntohs (((const struct GNUNET_MessageHeader *) conn->received_buf)->
474 conn->msg_complete = GNUNET_YES;
479 * Callback function for data received from the network. Note that
480 * both "available" and "errCode" would be 0 if the read simply timed out.
483 * @param buf pointer to received data
484 * @param available number of bytes availabe in "buf",
485 * possibly 0 (on errors)
486 * @param addr address of the sender
487 * @param addrlen size of addr
488 * @param errCode value of errno (on errors receiving)
491 receive_helper (void *cls,
494 const struct sockaddr *addr, socklen_t addrlen, int errCode)
496 struct GNUNET_CLIENT_Connection *conn = cls;
497 struct GNUNET_TIME_Relative remaining;
498 GNUNET_CLIENT_MessageHandler receive_handler;
499 void *receive_handler_cls;
501 GNUNET_assert (conn->msg_complete == GNUNET_NO);
502 conn->in_receive = GNUNET_NO;
503 if ((available == 0) || (conn->sock == NULL) || (errCode != 0))
505 /* signal timeout! */
507 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "timeout in receive_helper, available %d, conn->sock %s, errCode %d\n", available, conn->sock == NULL ? "NULL" : "non-NULL", errCode);
509 if (NULL != (receive_handler = conn->receiver_handler))
511 receive_handler_cls = conn->receiver_handler_cls;
512 conn->receiver_handler = NULL;
513 receive_handler (receive_handler_cls, NULL);
518 /* FIXME: optimize for common fast case where buf contains the
519 entire message and we need no copying... */
522 /* slow path: append to array */
523 if (conn->received_size < conn->received_pos + available)
524 GNUNET_array_grow (conn->received_buf,
525 conn->received_size, conn->received_pos + available);
526 memcpy (&conn->received_buf[conn->received_pos], buf, available);
527 conn->received_pos += available;
528 check_complete (conn);
529 /* check for timeout */
530 remaining = GNUNET_TIME_absolute_get_remaining (conn->receive_timeout);
531 if (remaining.rel_value == 0)
533 /* signal timeout! */
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! */
600 handler (handler_cls, NULL);
603 sock->receiver_handler = handler;
604 sock->receiver_handler_cls = handler_cls;
605 sock->receive_timeout = GNUNET_TIME_relative_to_absolute (timeout);
606 if (GNUNET_YES == sock->msg_complete)
608 sock->receive_task = GNUNET_SCHEDULER_add_after (GNUNET_SCHEDULER_NO_TASK,
609 &receive_task, sock);
613 GNUNET_assert (sock->in_receive == GNUNET_NO);
614 sock->in_receive = GNUNET_YES;
616 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "calling GNUNET_CONNECTION_receive\n");
618 GNUNET_CONNECTION_receive (sock->sock,
619 GNUNET_SERVER_MAX_MESSAGE_SIZE - 1,
620 timeout, &receive_helper, sock);
626 * Report service unavailable.
629 service_test_error (GNUNET_SCHEDULER_Task task, void *task_cls)
631 GNUNET_SCHEDULER_add_continuation (task,
633 GNUNET_SCHEDULER_REASON_TIMEOUT);
638 * Receive confirmation from test, service is up.
641 * @param msg message received, NULL on timeout or fatal error
644 confirm_handler (void *cls, const struct GNUNET_MessageHeader *msg)
646 struct GNUNET_CLIENT_Connection *conn = cls;
647 /* We may want to consider looking at the reply in more
648 detail in the future, for example, is this the
649 correct service? FIXME! */
653 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
654 "Received confirmation that service is running.\n");
656 GNUNET_SCHEDULER_add_continuation (conn->test_cb,
658 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
662 service_test_error (conn->test_cb, conn->test_cb_cls);
664 GNUNET_CLIENT_disconnect (conn, GNUNET_NO);
669 write_test (void *cls, size_t size, void *buf)
671 struct GNUNET_CLIENT_Connection *conn = cls;
672 struct GNUNET_MessageHeader *msg;
674 if (size < sizeof (struct GNUNET_MessageHeader))
677 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
678 _("Failure to transmit TEST request.\n"));
680 service_test_error (conn->test_cb, conn->test_cb_cls);
681 GNUNET_CLIENT_disconnect (conn, GNUNET_NO);
682 return 0; /* client disconnected */
685 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
686 "Transmitting `%s' request.\n", "TEST");
688 msg = (struct GNUNET_MessageHeader *) buf;
689 msg->type = htons (GNUNET_MESSAGE_TYPE_TEST);
690 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
691 GNUNET_CLIENT_receive (conn,
694 GNUNET_TIME_absolute_get_remaining (conn->test_deadline));
695 return sizeof (struct GNUNET_MessageHeader);
700 * Wait until the service is running.
702 * @param service name of the service to wait for
703 * @param cfg configuration to use
704 * @param timeout how long to wait at most in ms
705 * @param task task to run if service is running
706 * (reason will be "PREREQ_DONE" (service running)
707 * or "TIMEOUT" (service not known to be running))
708 * @param task_cls closure for task
711 GNUNET_CLIENT_service_test (const char *service,
712 const struct GNUNET_CONFIGURATION_Handle *cfg,
713 struct GNUNET_TIME_Relative timeout,
714 GNUNET_SCHEDULER_Task task, void *task_cls)
716 struct GNUNET_CLIENT_Connection *conn;
719 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
720 "Testing if service `%s' is running.\n", service);
722 conn = GNUNET_CLIENT_connect (service, cfg);
725 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
727 ("Could not connect to service `%s', must not be running.\n"),
729 service_test_error (task, task_cls);
732 conn->test_cb = task;
733 conn->test_cb_cls = task_cls;
734 conn->test_deadline = GNUNET_TIME_relative_to_absolute (timeout);
736 if (NULL == GNUNET_CLIENT_notify_transmit_ready (conn,
737 sizeof (struct GNUNET_MessageHeader),
742 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
743 _("Failure to transmit request to service `%s'\n"),
745 service_test_error (task, task_cls);
746 GNUNET_CLIENT_disconnect (conn, GNUNET_NO);
753 * Connection notifies us about failure or success of
754 * a transmission request. Either pass it on to our
755 * user or, if possible, retry.
757 * @param cls our "struct GNUNET_CLIENT_TransmissionHandle"
758 * @param size number of bytes available for transmission
759 * @param buf where to write them
760 * @return number of bytes written to buf
762 static size_t client_notify (void *cls, size_t size, void *buf);
766 * This task is run if we should re-try connection to the
767 * service after a while.
769 * @param cls our "struct GNUNET_CLIENT_TransmitHandle" of the request
773 client_delayed_retry (void *cls,
774 const struct GNUNET_SCHEDULER_TaskContext *tc)
776 struct GNUNET_CLIENT_TransmitHandle *th = cls;
778 th->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
779 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
782 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
783 "Transmission failed due to shutdown.\n");
786 th->notify (th->notify_cls, 0, NULL);
790 th->th = GNUNET_CONNECTION_notify_transmit_ready (th->sock->sock,
792 GNUNET_TIME_absolute_get_remaining
798 th->notify (th->notify_cls, 0, NULL);
806 * Connection notifies us about failure or success of a transmission
807 * request. Either pass it on to our user or, if possible, retry.
809 * @param cls our "struct GNUNET_CLIENT_TransmissionHandle"
810 * @param size number of bytes available for transmission
811 * @param buf where to write them
812 * @return number of bytes written to buf
815 client_notify (void *cls, size_t size, void *buf)
817 struct GNUNET_CLIENT_TransmitHandle *th = cls;
819 struct GNUNET_TIME_Relative delay;
825 delay = GNUNET_TIME_absolute_get_remaining (th->timeout);
826 delay.rel_value /= 2;
827 if ( (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & GNUNET_SCHEDULER_get_reason ())) ||
828 (GNUNET_YES != th->auto_retry) ||
829 (0 == --th->attempts_left) ||
830 (delay.rel_value < 1) )
833 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
834 "Transmission failed %u times, giving up.\n",
835 MAX_ATTEMPTS - th->attempts_left);
837 GNUNET_break (0 == th->notify (th->notify_cls, 0, NULL));
842 GNUNET_CONNECTION_destroy (th->sock->sock, GNUNET_NO);
843 th->sock->sock = do_connect (th->sock->service_name,
845 th->sock->attempts++);
846 GNUNET_assert (NULL != th->sock->sock);
847 GNUNET_CONNECTION_ignore_shutdown (th->sock->sock,
848 th->sock->ignore_shutdown);
849 delay = GNUNET_TIME_relative_min (delay, th->sock->back_off);
851 = GNUNET_TIME_relative_min (GNUNET_TIME_relative_multiply (th->sock->back_off, 2),
852 GNUNET_TIME_UNIT_SECONDS);
854 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
855 "Transmission failed %u times, trying again in %llums.\n",
856 MAX_ATTEMPTS - th->attempts_left,
857 (unsigned long long) delay.rel_value);
859 th->reconnect_task = GNUNET_SCHEDULER_add_delayed (delay,
860 &client_delayed_retry,
865 GNUNET_assert (size >= th->size);
866 ret = th->notify (th->notify_cls, size, buf);
873 * Ask the client to call us once the specified number of bytes
874 * are free in the transmission buffer. May call the notify
875 * method immediately if enough space is available.
877 * @param sock connection to the service
878 * @param size number of bytes to send
879 * @param timeout after how long should we give up (and call
880 * notify with buf NULL and size 0)?
881 * @param auto_retry if the connection to the service dies, should we
882 * automatically re-connect and retry (within the timeout period)
883 * or should we immediately fail in this case? Pass GNUNET_YES
884 * if the caller does not care about temporary connection errors,
885 * for example because the protocol is stateless
886 * @param notify function to call
887 * @param notify_cls closure for notify
888 * @return NULL if our buffer will never hold size bytes,
889 * a handle if the notify callback was queued (can be used to cancel)
891 struct GNUNET_CLIENT_TransmitHandle *
892 GNUNET_CLIENT_notify_transmit_ready (struct GNUNET_CLIENT_Connection *sock,
894 struct GNUNET_TIME_Relative timeout,
896 GNUNET_CONNECTION_TransmitReadyNotify
897 notify, void *notify_cls)
899 struct GNUNET_CLIENT_TransmitHandle *th;
901 if (NULL != sock->th)
903 th = GNUNET_malloc (sizeof (struct GNUNET_CLIENT_TransmitHandle));
906 th->timeout = GNUNET_TIME_relative_to_absolute (timeout);
907 th->auto_retry = auto_retry;
909 th->notify_cls = notify_cls;
910 th->attempts_left = MAX_ATTEMPTS;
911 th->th = GNUNET_CONNECTION_notify_transmit_ready (sock->sock,
927 * Cancel a request for notification.
929 * @param th handle from the original request.
932 GNUNET_CLIENT_notify_transmit_ready_cancel (struct
933 GNUNET_CLIENT_TransmitHandle *th)
935 if (th->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
937 GNUNET_break (NULL == th->th);
938 GNUNET_SCHEDULER_cancel (th->reconnect_task);
939 th->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
943 GNUNET_assert (NULL != th->th);
944 GNUNET_CONNECTION_notify_transmit_ready_cancel (th->th);
952 * Function called to notify a client about the socket
953 * begin ready to queue the message. "buf" will be
954 * NULL and "size" zero if the socket was closed for
955 * writing in the meantime.
957 * @param cls closure of type "struct TransmitGetResponseContext*"
958 * @param size number of bytes available in buf
959 * @param buf where the callee should write the message
960 * @return number of bytes written to buf
963 transmit_for_response (void *cls, size_t size, void *buf)
965 struct TransmitGetResponseContext *tc = cls;
968 tc->sock->tag = NULL;
969 msize = ntohs (tc->hdr->size);
973 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
974 _("Could not submit request, not expecting to receive a response.\n"));
976 tc->rn (tc->rn_cls, NULL);
980 GNUNET_assert (size >= msize);
981 memcpy (buf, tc->hdr, msize);
982 GNUNET_CLIENT_receive (tc->sock,
985 GNUNET_TIME_absolute_get_remaining (tc->timeout));
992 * Convenience API that combines sending a request
993 * to the service and waiting for a response.
994 * If either operation times out, the callback
995 * will be called with a "NULL" response (in which
996 * case the connection should probably be destroyed).
998 * @param sock connection to use
999 * @param hdr message to transmit
1000 * @param timeout when to give up (for both transmission
1001 * and for waiting for a response)
1002 * @param auto_retry if the connection to the service dies, should we
1003 * automatically re-connect and retry (within the timeout period)
1004 * or should we immediately fail in this case? Pass GNUNET_YES
1005 * if the caller does not care about temporary connection errors,
1006 * for example because the protocol is stateless
1007 * @param rn function to call with the response
1008 * @param rn_cls closure for rn
1009 * @return GNUNET_OK on success, GNUNET_SYSERR if a request
1010 * is already pending
1013 GNUNET_CLIENT_transmit_and_get_response (struct GNUNET_CLIENT_Connection
1015 const struct GNUNET_MessageHeader
1017 struct GNUNET_TIME_Relative timeout,
1019 GNUNET_CLIENT_MessageHandler rn,
1022 struct TransmitGetResponseContext *tc;
1025 if (NULL != sock->th)
1026 return GNUNET_SYSERR;
1027 GNUNET_assert (sock->tag == NULL);
1028 msize = ntohs (hdr->size);
1029 tc = GNUNET_malloc (sizeof (struct TransmitGetResponseContext) + msize);
1031 tc->hdr = (const struct GNUNET_MessageHeader *) &tc[1];
1032 memcpy (&tc[1], hdr, msize);
1033 tc->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1035 tc->rn_cls = rn_cls;
1036 if (NULL == GNUNET_CLIENT_notify_transmit_ready (sock,
1040 &transmit_for_response,
1045 return GNUNET_SYSERR;
1053 /* end of client.c */