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 * Struct to refer to a GNUnet TCP connection.
141 * This is more than just a socket because if the server
142 * drops the connection, the client automatically tries
143 * to reconnect (and for that needs connection information).
145 struct GNUNET_CLIENT_Connection
149 * the socket handle, NULL if not live
151 struct GNUNET_CONNECTION_Handle *sock;
155 * FIXME: why do we DUP the configuration? Avoid this!
157 struct GNUNET_CONFIGURATION_Handle *cfg;
160 * Name of the service we interact with.
165 * Context of a transmit_and_get_response operation, NULL
166 * if no such operation is pending.
168 struct TransmitGetResponseContext *tag;
171 * Handler for current receiver task.
173 GNUNET_CLIENT_MessageHandler receiver_handler;
176 * Closure for receiver_handler.
178 void *receiver_handler_cls;
181 * Handle for a pending transmission request, NULL if there is
184 struct GNUNET_CLIENT_TransmitHandle *th;
187 * Handler for service test completion (NULL unless in service_test)
189 GNUNET_SCHEDULER_Task test_cb;
192 * Deadline for calling 'test_cb'.
194 struct GNUNET_TIME_Absolute test_deadline;
197 * If we are re-trying and are delaying to do so,
198 * handle to the scheduled task managing the delay.
200 GNUNET_SCHEDULER_TaskIdentifier receive_task;
203 * Closure for test_cb (NULL unless in service_test)
208 * Buffer for received message.
213 * Timeout for receiving a response (absolute time).
215 struct GNUNET_TIME_Absolute receive_timeout;
218 * Current value for our incremental back-off (for
221 struct GNUNET_TIME_Relative back_off;
224 * Number of bytes in received_buf that are valid.
229 * Size of received_buf.
231 unsigned int received_size;
234 * Do we have a complete response in received_buf?
239 * Are we currently busy doing receive-processing?
240 * GNUNET_YES if so, GNUNET_NO if not.
245 * Are we ignoring shutdown signals?
250 * How often have we tried to connect?
252 unsigned int attempts;
257 * Try to connect to the service.
259 * @param service_name name of service to connect to
260 * @param cfg configuration to use
261 * @param attempt counter used to alternate between IP and UNIX domain sockets
262 * @return NULL on error
264 static struct GNUNET_CONNECTION_Handle *
265 do_connect (const char *service_name,
266 const struct GNUNET_CONFIGURATION_Handle *cfg,
267 unsigned int attempt)
269 struct GNUNET_CONNECTION_Handle *sock;
272 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))
283 sock = GNUNET_CONNECTION_create_from_connect_to_unixpath (cfg,
285 GNUNET_free (unixpath);
293 GNUNET_CONFIGURATION_get_value_number (cfg,
299 GNUNET_CONFIGURATION_get_value_string (cfg,
301 "HOSTNAME", &hostname)))
303 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
305 ("Could not determine valid hostname and port for service `%s' from configuration.\n"),
309 if (0 == strlen (hostname))
311 GNUNET_free (hostname);
312 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
313 _("Need a non-empty hostname for service `%s'.\n"),
317 sock = GNUNET_CONNECTION_create_from_connect (cfg,
320 GNUNET_free (hostname);
326 * Get a connection with a service.
328 * @param service_name name of the service
329 * @param cfg configuration to use
330 * @return NULL on error (service unknown to configuration)
332 struct GNUNET_CLIENT_Connection *
333 GNUNET_CLIENT_connect (const char *service_name,
334 const struct GNUNET_CONFIGURATION_Handle *cfg)
336 struct GNUNET_CLIENT_Connection *ret;
337 struct GNUNET_CONNECTION_Handle *sock;
339 sock = do_connect (service_name,
343 ret = GNUNET_malloc (sizeof (struct GNUNET_CLIENT_Connection));
346 ret->service_name = GNUNET_strdup (service_name);
347 ret->cfg = GNUNET_CONFIGURATION_dup (cfg);
348 ret->back_off = GNUNET_TIME_UNIT_MILLISECONDS;
354 * Configure this connection to ignore shutdown signals.
356 * @param h client handle
357 * @param do_ignore GNUNET_YES to ignore, GNUNET_NO to restore default
360 GNUNET_CLIENT_ignore_shutdown (struct GNUNET_CLIENT_Connection *h,
363 h->ignore_shutdown = do_ignore;
365 GNUNET_CONNECTION_ignore_shutdown (h->sock,
371 * Destroy connection with the service. This will automatically
372 * cancel any pending "receive" request (however, the handler will
373 * *NOT* be called, not even with a NULL message). Any pending
374 * transmission request will also be cancelled UNLESS the callback for
375 * the transmission request has already been called, in which case the
376 * transmission 'finish_pending_write' argument determines whether or
377 * not the write is guaranteed to complete before the socket is fully
378 * destroyed (unless, of course, there is an error with the server in
379 * which case the message may still be lost).
381 * @param finish_pending_write should a transmission already passed to the
382 * handle be completed?
383 * @param sock handle to the service connection
386 GNUNET_CLIENT_disconnect (struct GNUNET_CLIENT_Connection *sock,
387 int finish_pending_write)
389 GNUNET_assert (sock->sock != NULL);
390 if (sock->in_receive == GNUNET_YES)
392 GNUNET_CONNECTION_receive_cancel (sock->sock);
393 sock->in_receive = GNUNET_NO;
395 GNUNET_CONNECTION_destroy (sock->sock, finish_pending_write);
397 if (sock->tag != NULL)
399 GNUNET_free (sock->tag);
402 sock->receiver_handler = NULL;
403 if (sock->th != NULL)
404 GNUNET_CLIENT_notify_transmit_ready_cancel (sock->th);
405 if (sock->receive_task != GNUNET_SCHEDULER_NO_TASK)
407 GNUNET_SCHEDULER_cancel (sock->receive_task);
408 sock->receive_task = GNUNET_SCHEDULER_NO_TASK;
410 GNUNET_array_grow (sock->received_buf, sock->received_size, 0);
411 GNUNET_free (sock->service_name);
412 GNUNET_CONFIGURATION_destroy (sock->cfg);
418 * Check if message is complete
421 check_complete (struct GNUNET_CLIENT_Connection *conn)
423 if ((conn->received_pos >= sizeof (struct GNUNET_MessageHeader)) &&
424 (conn->received_pos >=
425 ntohs (((const struct GNUNET_MessageHeader *) conn->received_buf)->
427 conn->msg_complete = GNUNET_YES;
432 * Callback function for data received from the network. Note that
433 * both "available" and "errCode" would be 0 if the read simply timed out.
436 * @param buf pointer to received data
437 * @param available number of bytes availabe in "buf",
438 * possibly 0 (on errors)
439 * @param addr address of the sender
440 * @param addrlen size of addr
441 * @param errCode value of errno (on errors receiving)
444 receive_helper (void *cls,
447 const struct sockaddr *addr, socklen_t addrlen, int errCode)
449 struct GNUNET_CLIENT_Connection *conn = cls;
450 struct GNUNET_TIME_Relative remaining;
451 GNUNET_CLIENT_MessageHandler receive_handler;
452 void *receive_handler_cls;
454 GNUNET_assert (conn->msg_complete == GNUNET_NO);
455 conn->in_receive = GNUNET_NO;
456 if ((available == 0) || (conn->sock == NULL) || (errCode != 0))
458 /* signal timeout! */
459 if (NULL != (receive_handler = conn->receiver_handler))
461 receive_handler_cls = conn->receiver_handler_cls;
462 conn->receiver_handler = NULL;
463 receive_handler (receive_handler_cls, NULL);
468 /* FIXME: optimize for common fast case where buf contains the
469 entire message and we need no copying... */
472 /* slow path: append to array */
473 if (conn->received_size < conn->received_pos + available)
474 GNUNET_array_grow (conn->received_buf,
475 conn->received_size, conn->received_pos + available);
476 memcpy (&conn->received_buf[conn->received_pos], buf, available);
477 conn->received_pos += available;
478 check_complete (conn);
479 /* check for timeout */
480 remaining = GNUNET_TIME_absolute_get_remaining (conn->receive_timeout);
481 if (remaining.rel_value == 0)
483 /* signal timeout! */
484 conn->receiver_handler (conn->receiver_handler_cls, NULL);
487 /* back to receive -- either for more data or to call callback! */
488 GNUNET_CLIENT_receive (conn,
489 conn->receiver_handler,
490 conn->receiver_handler_cls, remaining);
495 * Continuation to call the receive callback.
497 * @param cls our handle to the client connection
498 * @param tc scheduler context
501 receive_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
503 struct GNUNET_CLIENT_Connection *sock = cls;
504 GNUNET_CLIENT_MessageHandler handler = sock->receiver_handler;
505 const struct GNUNET_MessageHeader *cmsg =
506 (const struct GNUNET_MessageHeader *) sock->received_buf;
507 void *handler_cls = sock->receiver_handler_cls;
508 uint16_t msize = ntohs (cmsg->size);
510 struct GNUNET_MessageHeader *msg = (struct GNUNET_MessageHeader *) mbuf;
513 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
514 "Received message of type %u and size %u\n",
518 sock->receive_task = GNUNET_SCHEDULER_NO_TASK;
519 GNUNET_assert (GNUNET_YES == sock->msg_complete);
520 GNUNET_assert (sock->received_pos >= msize);
521 memcpy (msg, cmsg, msize);
522 memmove (sock->received_buf,
523 &sock->received_buf[msize], sock->received_pos - msize);
524 sock->received_pos -= msize;
525 sock->msg_complete = GNUNET_NO;
526 sock->receiver_handler = NULL;
527 check_complete (sock);
529 handler (handler_cls, msg);
534 * Read from the service.
536 * @param sock the service
537 * @param handler function to call with the message
538 * @param handler_cls closure for handler
539 * @param timeout how long to wait until timing out
542 GNUNET_CLIENT_receive (struct GNUNET_CLIENT_Connection *sock,
543 GNUNET_CLIENT_MessageHandler handler,
544 void *handler_cls, struct GNUNET_TIME_Relative timeout)
546 if (sock->sock == NULL)
548 /* already disconnected, fail instantly! */
549 GNUNET_break (0); /* this should not happen in well-written code! */
550 handler (handler_cls, NULL);
553 sock->receiver_handler = handler;
554 sock->receiver_handler_cls = handler_cls;
555 sock->receive_timeout = GNUNET_TIME_relative_to_absolute (timeout);
556 if (GNUNET_YES == sock->msg_complete)
558 sock->receive_task = GNUNET_SCHEDULER_add_after (GNUNET_SCHEDULER_NO_TASK,
559 &receive_task, sock);
563 GNUNET_assert (sock->in_receive == GNUNET_NO);
564 sock->in_receive = GNUNET_YES;
565 GNUNET_CONNECTION_receive (sock->sock,
566 GNUNET_SERVER_MAX_MESSAGE_SIZE - 1,
567 timeout, &receive_helper, sock);
573 * Report service unavailable.
576 service_test_error (GNUNET_SCHEDULER_Task task, void *task_cls)
578 GNUNET_SCHEDULER_add_continuation (task,
580 GNUNET_SCHEDULER_REASON_TIMEOUT);
585 * Receive confirmation from test, service is up.
588 * @param msg message received, NULL on timeout or fatal error
591 confirm_handler (void *cls, const struct GNUNET_MessageHeader *msg)
593 struct GNUNET_CLIENT_Connection *conn = cls;
594 /* We may want to consider looking at the reply in more
595 detail in the future, for example, is this the
596 correct service? FIXME! */
600 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
601 "Received confirmation that service is running.\n");
603 GNUNET_SCHEDULER_add_continuation (conn->test_cb,
605 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
609 service_test_error (conn->test_cb, conn->test_cb_cls);
611 GNUNET_CLIENT_disconnect (conn, GNUNET_NO);
616 write_test (void *cls, size_t size, void *buf)
618 struct GNUNET_CLIENT_Connection *conn = cls;
619 struct GNUNET_MessageHeader *msg;
621 if (size < sizeof (struct GNUNET_MessageHeader))
624 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
625 _("Failure to transmit TEST request.\n"));
627 service_test_error (conn->test_cb, conn->test_cb_cls);
628 GNUNET_CLIENT_disconnect (conn, GNUNET_NO);
629 return 0; /* client disconnected */
632 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
633 "Transmitting `%s' request.\n", "TEST");
635 msg = (struct GNUNET_MessageHeader *) buf;
636 msg->type = htons (GNUNET_MESSAGE_TYPE_TEST);
637 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
638 GNUNET_CLIENT_receive (conn,
641 GNUNET_TIME_absolute_get_remaining (conn->test_deadline));
642 return sizeof (struct GNUNET_MessageHeader);
647 * Wait until the service is running.
649 * @param service name of the service to wait for
650 * @param cfg configuration to use
651 * @param timeout how long to wait at most in ms
652 * @param task task to run if service is running
653 * (reason will be "PREREQ_DONE" (service running)
654 * or "TIMEOUT" (service not known to be running))
655 * @param task_cls closure for task
658 GNUNET_CLIENT_service_test (const char *service,
659 const struct GNUNET_CONFIGURATION_Handle *cfg,
660 struct GNUNET_TIME_Relative timeout,
661 GNUNET_SCHEDULER_Task task, void *task_cls)
663 struct GNUNET_CLIENT_Connection *conn;
666 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
667 "Testing if service `%s' is running.\n", service);
669 conn = GNUNET_CLIENT_connect (service, cfg);
672 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
674 ("Could not connect to service `%s', must not be running.\n"),
676 service_test_error (task, task_cls);
679 conn->test_cb = task;
680 conn->test_cb_cls = task_cls;
681 conn->test_deadline = GNUNET_TIME_relative_to_absolute (timeout);
683 if (NULL == GNUNET_CLIENT_notify_transmit_ready (conn,
684 sizeof (struct GNUNET_MessageHeader),
689 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
690 _("Failure to transmit request to service `%s'\n"),
692 service_test_error (task, task_cls);
693 GNUNET_CLIENT_disconnect (conn, GNUNET_NO);
700 * Connection notifies us about failure or success of
701 * a transmission request. Either pass it on to our
702 * user or, if possible, retry.
704 * @param cls our "struct GNUNET_CLIENT_TransmissionHandle"
705 * @param size number of bytes available for transmission
706 * @param buf where to write them
707 * @return number of bytes written to buf
709 static size_t client_notify (void *cls, size_t size, void *buf);
713 * This task is run if we should re-try connection to the
714 * service after a while.
716 * @param cls our "struct GNUNET_CLIENT_TransmitHandle" of the request
720 client_delayed_retry (void *cls,
721 const struct GNUNET_SCHEDULER_TaskContext *tc)
723 struct GNUNET_CLIENT_TransmitHandle *th = cls;
725 th->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
726 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
729 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
730 "Transmission failed due to shutdown.\n");
733 th->notify (th->notify_cls, 0, NULL);
737 th->th = GNUNET_CONNECTION_notify_transmit_ready (th->sock->sock,
739 GNUNET_TIME_absolute_get_remaining
745 th->notify (th->notify_cls, 0, NULL);
753 * Connection notifies us about failure or success of a transmission
754 * request. Either pass it on to our user or, if possible, retry.
756 * @param cls our "struct GNUNET_CLIENT_TransmissionHandle"
757 * @param size number of bytes available for transmission
758 * @param buf where to write them
759 * @return number of bytes written to buf
762 client_notify (void *cls, size_t size, void *buf)
764 struct GNUNET_CLIENT_TransmitHandle *th = cls;
766 struct GNUNET_TIME_Relative delay;
772 delay = GNUNET_TIME_absolute_get_remaining (th->timeout);
773 delay.rel_value /= 2;
774 if ( (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & GNUNET_SCHEDULER_get_reason ())) ||
775 (GNUNET_YES != th->auto_retry) ||
776 (0 == --th->attempts_left) ||
777 (delay.rel_value < 1) )
780 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
781 "Transmission failed %u times, giving up.\n",
782 MAX_ATTEMPTS - th->attempts_left);
784 GNUNET_break (0 == th->notify (th->notify_cls, 0, NULL));
789 GNUNET_CONNECTION_destroy (th->sock->sock, GNUNET_NO);
790 th->sock->sock = do_connect (th->sock->service_name,
792 th->sock->attempts++);
793 GNUNET_assert (NULL != th->sock->sock);
794 GNUNET_CONNECTION_ignore_shutdown (th->sock->sock,
795 th->sock->ignore_shutdown);
796 delay = GNUNET_TIME_relative_min (delay, th->sock->back_off);
798 = GNUNET_TIME_relative_min (GNUNET_TIME_relative_multiply (th->sock->back_off, 2),
799 GNUNET_TIME_UNIT_SECONDS);
801 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
802 "Transmission failed %u times, trying again in %llums.\n",
803 MAX_ATTEMPTS - th->attempts_left,
804 (unsigned long long) delay.rel_value);
806 th->reconnect_task = GNUNET_SCHEDULER_add_delayed (delay,
807 &client_delayed_retry,
812 GNUNET_assert (size >= th->size);
813 ret = th->notify (th->notify_cls, size, buf);
820 * Ask the client to call us once the specified number of bytes
821 * are free in the transmission buffer. May call the notify
822 * method immediately if enough space is available.
824 * @param sock connection to the service
825 * @param size number of bytes to send
826 * @param timeout after how long should we give up (and call
827 * notify with buf NULL and size 0)?
828 * @param auto_retry if the connection to the service dies, should we
829 * automatically re-connect and retry (within the timeout period)
830 * or should we immediately fail in this case? Pass GNUNET_YES
831 * if the caller does not care about temporary connection errors,
832 * for example because the protocol is stateless
833 * @param notify function to call
834 * @param notify_cls closure for notify
835 * @return NULL if our buffer will never hold size bytes,
836 * a handle if the notify callback was queued (can be used to cancel)
838 struct GNUNET_CLIENT_TransmitHandle *
839 GNUNET_CLIENT_notify_transmit_ready (struct GNUNET_CLIENT_Connection *sock,
841 struct GNUNET_TIME_Relative timeout,
843 GNUNET_CONNECTION_TransmitReadyNotify
844 notify, void *notify_cls)
846 struct GNUNET_CLIENT_TransmitHandle *th;
848 if (NULL != sock->th)
850 th = GNUNET_malloc (sizeof (struct GNUNET_CLIENT_TransmitHandle));
853 th->timeout = GNUNET_TIME_relative_to_absolute (timeout);
854 th->auto_retry = auto_retry;
856 th->notify_cls = notify_cls;
857 th->attempts_left = MAX_ATTEMPTS;
858 th->th = GNUNET_CONNECTION_notify_transmit_ready (sock->sock,
874 * Cancel a request for notification.
876 * @param th handle from the original request.
879 GNUNET_CLIENT_notify_transmit_ready_cancel (struct
880 GNUNET_CLIENT_TransmitHandle *th)
882 if (th->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
884 GNUNET_break (NULL == th->th);
885 GNUNET_SCHEDULER_cancel (th->reconnect_task);
886 th->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
890 GNUNET_assert (NULL != th->th);
891 GNUNET_CONNECTION_notify_transmit_ready_cancel (th->th);
899 * Function called to notify a client about the socket
900 * begin ready to queue the message. "buf" will be
901 * NULL and "size" zero if the socket was closed for
902 * writing in the meantime.
904 * @param cls closure of type "struct TransmitGetResponseContext*"
905 * @param size number of bytes available in buf
906 * @param buf where the callee should write the message
907 * @return number of bytes written to buf
910 transmit_for_response (void *cls, size_t size, void *buf)
912 struct TransmitGetResponseContext *tc = cls;
915 tc->sock->tag = NULL;
916 msize = ntohs (tc->hdr->size);
920 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
921 _("Could not submit request, not expecting to receive a response.\n"));
923 tc->rn (tc->rn_cls, NULL);
927 GNUNET_assert (size >= msize);
928 memcpy (buf, tc->hdr, msize);
929 GNUNET_CLIENT_receive (tc->sock,
932 GNUNET_TIME_absolute_get_remaining (tc->timeout));
939 * Convenience API that combines sending a request
940 * to the service and waiting for a response.
941 * If either operation times out, the callback
942 * will be called with a "NULL" response (in which
943 * case the connection should probably be destroyed).
945 * @param sock connection to use
946 * @param hdr message to transmit
947 * @param timeout when to give up (for both transmission
948 * and for waiting for a response)
949 * @param auto_retry if the connection to the service dies, should we
950 * automatically re-connect and retry (within the timeout period)
951 * or should we immediately fail in this case? Pass GNUNET_YES
952 * if the caller does not care about temporary connection errors,
953 * for example because the protocol is stateless
954 * @param rn function to call with the response
955 * @param rn_cls closure for rn
956 * @return GNUNET_OK on success, GNUNET_SYSERR if a request
960 GNUNET_CLIENT_transmit_and_get_response (struct GNUNET_CLIENT_Connection
962 const struct GNUNET_MessageHeader
964 struct GNUNET_TIME_Relative timeout,
966 GNUNET_CLIENT_MessageHandler rn,
969 struct TransmitGetResponseContext *tc;
972 if (NULL != sock->th)
973 return GNUNET_SYSERR;
974 GNUNET_assert (sock->tag == NULL);
975 msize = ntohs (hdr->size);
976 tc = GNUNET_malloc (sizeof (struct TransmitGetResponseContext) + msize);
978 tc->hdr = (const struct GNUNET_MessageHeader *) &tc[1];
979 memcpy (&tc[1], hdr, msize);
980 tc->timeout = GNUNET_TIME_relative_to_absolute (timeout);
983 if (NULL == GNUNET_CLIENT_notify_transmit_ready (sock,
987 &transmit_for_response,
992 return GNUNET_SYSERR;
1000 /* end of client.c */