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;
156 struct GNUNET_SCHEDULER_Handle *sched;
160 * FIXME: why do we DUP the configuration? Avoid this!
162 struct GNUNET_CONFIGURATION_Handle *cfg;
165 * Name of the service we interact with.
170 * Context of a transmit_and_get_response operation, NULL
171 * if no such operation is pending.
173 struct TransmitGetResponseContext *tag;
176 * Handler for current receiver task.
178 GNUNET_CLIENT_MessageHandler receiver_handler;
181 * Closure for receiver_handler.
183 void *receiver_handler_cls;
186 * Handle for a pending transmission request, NULL if there is
189 struct GNUNET_CLIENT_TransmitHandle *th;
192 * Handler for service test completion (NULL unless in service_test)
194 GNUNET_SCHEDULER_Task test_cb;
197 * Deadline for calling 'test_cb'.
199 struct GNUNET_TIME_Absolute test_deadline;
202 * If we are re-trying and are delaying to do so,
203 * handle to the scheduled task managing the delay.
205 GNUNET_SCHEDULER_TaskIdentifier receive_task;
208 * Closure for test_cb (NULL unless in service_test)
213 * Buffer for received message.
218 * Timeout for receiving a response (absolute time).
220 struct GNUNET_TIME_Absolute receive_timeout;
223 * Current value for our incremental back-off (for
226 struct GNUNET_TIME_Relative back_off;
229 * Number of bytes in received_buf that are valid.
234 * Size of received_buf.
236 unsigned int received_size;
239 * Do we have a complete response in received_buf?
244 * Are we currently busy doing receive-processing?
245 * GNUNET_YES if so, GNUNET_NO if not.
250 * Are we ignoring shutdown signals?
255 * How often have we tried to connect?
257 unsigned int attempts;
263 * Try to connect to the service.
265 * @param sched scheduler to use
266 * @param service_name name of service to connect to
267 * @param cfg configuration to use
268 * @param attempt counter used to alternate between IP and UNIX domain sockets
269 * @return NULL on error
271 static struct GNUNET_CONNECTION_Handle *
272 do_connect (struct GNUNET_SCHEDULER_Handle *sched,
273 const char *service_name,
274 const struct GNUNET_CONFIGURATION_Handle *cfg,
275 unsigned int attempt)
277 struct GNUNET_CONNECTION_Handle *sock;
280 unsigned long long port;
283 if (0 == attempt % 2)
285 /* on even rounds, try UNIX */
287 GNUNET_CONFIGURATION_get_value_string (cfg,
289 "UNIXPATH", &unixpath))
291 sock = GNUNET_CONNECTION_create_from_connect_to_unixpath (sched,
294 GNUNET_free (unixpath);
302 GNUNET_CONFIGURATION_get_value_number (cfg,
308 GNUNET_CONFIGURATION_get_value_string (cfg,
310 "HOSTNAME", &hostname)))
312 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
314 ("Could not determine valid hostname and port for service `%s' from configuration.\n"),
318 if (0 == strlen (hostname))
320 GNUNET_free (hostname);
321 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
322 _("Need a non-empty hostname for service `%s'.\n"),
326 sock = GNUNET_CONNECTION_create_from_connect (sched,
330 GNUNET_free (hostname);
336 * Get a connection with a service.
338 * @param sched scheduler to use
339 * @param service_name name of the service
340 * @param cfg configuration to use
341 * @return NULL on error (service unknown to configuration)
343 struct GNUNET_CLIENT_Connection *
344 GNUNET_CLIENT_connect (struct GNUNET_SCHEDULER_Handle *sched,
345 const char *service_name,
346 const struct GNUNET_CONFIGURATION_Handle *cfg)
348 struct GNUNET_CLIENT_Connection *ret;
349 struct GNUNET_CONNECTION_Handle *sock;
351 sock = do_connect (sched,
356 ret = GNUNET_malloc (sizeof (struct GNUNET_CLIENT_Connection));
360 ret->service_name = GNUNET_strdup (service_name);
361 ret->cfg = GNUNET_CONFIGURATION_dup (cfg);
362 ret->back_off = GNUNET_TIME_UNIT_MILLISECONDS;
368 * Configure this connection to ignore shutdown signals.
370 * @param h client handle
371 * @param do_ignore GNUNET_YES to ignore, GNUNET_NO to restore default
374 GNUNET_CLIENT_ignore_shutdown (struct GNUNET_CLIENT_Connection *h,
377 h->ignore_shutdown = do_ignore;
379 GNUNET_CONNECTION_ignore_shutdown (h->sock,
385 * Destroy connection with the service. This will automatically
386 * cancel any pending "receive" request (however, the handler will
387 * *NOT* be called, not even with a NULL message). Any pending
388 * transmission request will also be cancelled UNLESS the callback for
389 * the transmission request has already been called, in which case the
390 * transmission 'finish_pending_write' argument determines whether or
391 * not the write is guaranteed to complete before the socket is fully
392 * destroyed (unless, of course, there is an error with the server in
393 * which case the message may still be lost).
395 * @param finish_pending_write should a transmission already passed to the
396 * handle be completed?
397 * @param sock handle to the service connection
400 GNUNET_CLIENT_disconnect (struct GNUNET_CLIENT_Connection *sock,
401 int finish_pending_write)
403 GNUNET_assert (sock->sock != NULL);
404 if (sock->in_receive == GNUNET_YES)
406 GNUNET_CONNECTION_receive_cancel (sock->sock);
407 sock->in_receive = GNUNET_NO;
409 GNUNET_CONNECTION_destroy (sock->sock, finish_pending_write);
411 if (sock->tag != NULL)
413 GNUNET_free (sock->tag);
416 sock->receiver_handler = NULL;
417 if (sock->th != NULL)
418 GNUNET_CLIENT_notify_transmit_ready_cancel (sock->th);
419 if (sock->receive_task != GNUNET_SCHEDULER_NO_TASK)
421 GNUNET_SCHEDULER_cancel (sock->sched, sock->receive_task);
422 sock->receive_task = GNUNET_SCHEDULER_NO_TASK;
424 GNUNET_array_grow (sock->received_buf, sock->received_size, 0);
425 GNUNET_free (sock->service_name);
426 GNUNET_CONFIGURATION_destroy (sock->cfg);
432 * Check if message is complete
435 check_complete (struct GNUNET_CLIENT_Connection *conn)
437 if ((conn->received_pos >= sizeof (struct GNUNET_MessageHeader)) &&
438 (conn->received_pos >=
439 ntohs (((const struct GNUNET_MessageHeader *) conn->received_buf)->
441 conn->msg_complete = GNUNET_YES;
446 * Callback function for data received from the network. Note that
447 * both "available" and "errCode" would be 0 if the read simply timed out.
450 * @param buf pointer to received data
451 * @param available number of bytes availabe in "buf",
452 * possibly 0 (on errors)
453 * @param addr address of the sender
454 * @param addrlen size of addr
455 * @param errCode value of errno (on errors receiving)
458 receive_helper (void *cls,
461 const struct sockaddr *addr, socklen_t addrlen, int errCode)
463 struct GNUNET_CLIENT_Connection *conn = cls;
464 struct GNUNET_TIME_Relative remaining;
465 GNUNET_CLIENT_MessageHandler receive_handler;
466 void *receive_handler_cls;
468 GNUNET_assert (conn->msg_complete == GNUNET_NO);
469 conn->in_receive = GNUNET_NO;
470 if ((available == 0) || (conn->sock == NULL) || (errCode != 0))
472 /* signal timeout! */
473 if (NULL != (receive_handler = conn->receiver_handler))
475 receive_handler_cls = conn->receiver_handler_cls;
476 conn->receiver_handler = NULL;
477 receive_handler (receive_handler_cls, NULL);
482 /* FIXME: optimize for common fast case where buf contains the
483 entire message and we need no copying... */
486 /* slow path: append to array */
487 if (conn->received_size < conn->received_pos + available)
488 GNUNET_array_grow (conn->received_buf,
489 conn->received_size, conn->received_pos + available);
490 memcpy (&conn->received_buf[conn->received_pos], buf, available);
491 conn->received_pos += available;
492 check_complete (conn);
493 /* check for timeout */
494 remaining = GNUNET_TIME_absolute_get_remaining (conn->receive_timeout);
495 if (remaining.rel_value == 0)
497 /* signal timeout! */
498 conn->receiver_handler (conn->receiver_handler_cls, NULL);
501 /* back to receive -- either for more data or to call callback! */
502 GNUNET_CLIENT_receive (conn,
503 conn->receiver_handler,
504 conn->receiver_handler_cls, remaining);
509 * Continuation to call the receive callback.
511 * @param cls our handle to the client connection
512 * @param tc scheduler context
515 receive_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
517 struct GNUNET_CLIENT_Connection *sock = cls;
518 GNUNET_CLIENT_MessageHandler handler = sock->receiver_handler;
519 const struct GNUNET_MessageHeader *cmsg =
520 (const struct GNUNET_MessageHeader *) sock->received_buf;
521 void *handler_cls = sock->receiver_handler_cls;
522 uint16_t msize = ntohs (cmsg->size);
524 struct GNUNET_MessageHeader *msg = (struct GNUNET_MessageHeader *) mbuf;
527 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
528 "Received message of type %u and size %u\n",
532 sock->receive_task = GNUNET_SCHEDULER_NO_TASK;
533 GNUNET_assert (GNUNET_YES == sock->msg_complete);
534 GNUNET_assert (sock->received_pos >= msize);
535 memcpy (msg, cmsg, msize);
536 memmove (sock->received_buf,
537 &sock->received_buf[msize], sock->received_pos - msize);
538 sock->received_pos -= msize;
539 sock->msg_complete = GNUNET_NO;
540 sock->receiver_handler = NULL;
541 check_complete (sock);
543 handler (handler_cls, msg);
548 * Read from the service.
550 * @param sock the service
551 * @param handler function to call with the message
552 * @param handler_cls closure for handler
553 * @param timeout how long to wait until timing out
556 GNUNET_CLIENT_receive (struct GNUNET_CLIENT_Connection *sock,
557 GNUNET_CLIENT_MessageHandler handler,
558 void *handler_cls, struct GNUNET_TIME_Relative timeout)
560 if (sock->sock == NULL)
562 /* already disconnected, fail instantly! */
563 GNUNET_break (0); /* this should not happen in well-written code! */
564 handler (handler_cls, NULL);
567 sock->receiver_handler = handler;
568 sock->receiver_handler_cls = handler_cls;
569 sock->receive_timeout = GNUNET_TIME_relative_to_absolute (timeout);
570 if (GNUNET_YES == sock->msg_complete)
572 sock->receive_task = GNUNET_SCHEDULER_add_after (sock->sched,
573 GNUNET_SCHEDULER_NO_TASK,
574 &receive_task, sock);
578 GNUNET_assert (sock->in_receive == GNUNET_NO);
579 sock->in_receive = GNUNET_YES;
580 GNUNET_CONNECTION_receive (sock->sock,
581 GNUNET_SERVER_MAX_MESSAGE_SIZE - 1,
582 timeout, &receive_helper, sock);
588 * Report service unavailable.
591 service_test_error (struct GNUNET_SCHEDULER_Handle *s,
592 GNUNET_SCHEDULER_Task task, void *task_cls)
594 GNUNET_SCHEDULER_add_continuation (s,
597 GNUNET_SCHEDULER_REASON_TIMEOUT);
602 * Receive confirmation from test, service is up.
605 * @param msg message received, NULL on timeout or fatal error
608 confirm_handler (void *cls, const struct GNUNET_MessageHeader *msg)
610 struct GNUNET_CLIENT_Connection *conn = cls;
611 /* We may want to consider looking at the reply in more
612 detail in the future, for example, is this the
613 correct service? FIXME! */
617 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
618 "Received confirmation that service is running.\n");
620 GNUNET_SCHEDULER_add_continuation (conn->sched,
623 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
627 service_test_error (conn->sched, conn->test_cb, conn->test_cb_cls);
629 GNUNET_CLIENT_disconnect (conn, GNUNET_NO);
634 write_test (void *cls, size_t size, void *buf)
636 struct GNUNET_CLIENT_Connection *conn = cls;
637 struct GNUNET_MessageHeader *msg;
639 if (size < sizeof (struct GNUNET_MessageHeader))
642 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
643 _("Failure to transmit TEST request.\n"));
645 service_test_error (conn->sched, conn->test_cb, conn->test_cb_cls);
646 GNUNET_CLIENT_disconnect (conn, GNUNET_NO);
647 return 0; /* client disconnected */
650 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
651 "Transmitting `%s' request.\n", "TEST");
653 msg = (struct GNUNET_MessageHeader *) buf;
654 msg->type = htons (GNUNET_MESSAGE_TYPE_TEST);
655 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
656 GNUNET_CLIENT_receive (conn,
659 GNUNET_TIME_absolute_get_remaining (conn->test_deadline));
660 return sizeof (struct GNUNET_MessageHeader);
665 * Wait until the service is running.
667 * @param sched scheduler to use
668 * @param service name of the service to wait for
669 * @param cfg configuration to use
670 * @param timeout how long to wait at most in ms
671 * @param task task to run if service is running
672 * (reason will be "PREREQ_DONE" (service running)
673 * or "TIMEOUT" (service not known to be running))
674 * @param task_cls closure for task
677 GNUNET_CLIENT_service_test (struct GNUNET_SCHEDULER_Handle *sched,
679 const struct GNUNET_CONFIGURATION_Handle *cfg,
680 struct GNUNET_TIME_Relative timeout,
681 GNUNET_SCHEDULER_Task task, void *task_cls)
683 struct GNUNET_CLIENT_Connection *conn;
686 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
687 "Testing if service `%s' is running.\n", service);
689 conn = GNUNET_CLIENT_connect (sched, service, cfg);
692 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
694 ("Could not connect to service `%s', must not be running.\n"),
696 service_test_error (sched, task, task_cls);
699 conn->test_cb = task;
700 conn->test_cb_cls = task_cls;
701 conn->test_deadline = GNUNET_TIME_relative_to_absolute (timeout);
703 if (NULL == GNUNET_CLIENT_notify_transmit_ready (conn,
704 sizeof (struct GNUNET_MessageHeader),
709 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
710 _("Failure to transmit request to service `%s'\n"),
712 service_test_error (sched, task, task_cls);
713 GNUNET_CLIENT_disconnect (conn, GNUNET_NO);
720 * Connection notifies us about failure or success of
721 * a transmission request. Either pass it on to our
722 * user or, if possible, retry.
724 * @param cls our "struct GNUNET_CLIENT_TransmissionHandle"
725 * @param size number of bytes available for transmission
726 * @param buf where to write them
727 * @return number of bytes written to buf
729 static size_t client_notify (void *cls, size_t size, void *buf);
733 * This task is run if we should re-try connection to the
734 * service after a while.
736 * @param cls our "struct GNUNET_CLIENT_TransmitHandle" of the request
740 client_delayed_retry (void *cls,
741 const struct GNUNET_SCHEDULER_TaskContext *tc)
743 struct GNUNET_CLIENT_TransmitHandle *th = cls;
745 th->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
746 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
749 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
750 "Transmission failed due to shutdown.\n");
753 th->notify (th->notify_cls, 0, NULL);
757 th->th = GNUNET_CONNECTION_notify_transmit_ready (th->sock->sock,
759 GNUNET_TIME_absolute_get_remaining
765 th->notify (th->notify_cls, 0, NULL);
773 * Connection notifies us about failure or success of a transmission
774 * request. Either pass it on to our user or, if possible, retry.
776 * @param cls our "struct GNUNET_CLIENT_TransmissionHandle"
777 * @param size number of bytes available for transmission
778 * @param buf where to write them
779 * @return number of bytes written to buf
782 client_notify (void *cls, size_t size, void *buf)
784 struct GNUNET_CLIENT_TransmitHandle *th = cls;
786 struct GNUNET_TIME_Relative delay;
792 delay = GNUNET_TIME_absolute_get_remaining (th->timeout);
793 delay.rel_value /= 2;
794 if ( (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & GNUNET_SCHEDULER_get_reason (th->sock->sched))) ||
795 (GNUNET_YES != th->auto_retry) ||
796 (0 == --th->attempts_left) ||
797 (delay.rel_value < 1) )
800 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
801 "Transmission failed %u times, giving up.\n",
802 MAX_ATTEMPTS - th->attempts_left);
804 GNUNET_break (0 == th->notify (th->notify_cls, 0, NULL));
809 GNUNET_CONNECTION_destroy (th->sock->sock, GNUNET_NO);
810 th->sock->sock = do_connect (th->sock->sched,
811 th->sock->service_name,
813 th->sock->attempts++);
814 GNUNET_assert (NULL != th->sock->sock);
815 GNUNET_CONNECTION_ignore_shutdown (th->sock->sock,
816 th->sock->ignore_shutdown);
817 delay = GNUNET_TIME_relative_min (delay, th->sock->back_off);
819 = GNUNET_TIME_relative_min (GNUNET_TIME_relative_multiply (th->sock->back_off, 2),
820 GNUNET_TIME_UNIT_SECONDS);
822 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
823 "Transmission failed %u times, trying again in %llums.\n",
824 MAX_ATTEMPTS - th->attempts_left,
825 (unsigned long long) delay.abs_value);
827 th->reconnect_task = GNUNET_SCHEDULER_add_delayed (th->sock->sched,
829 &client_delayed_retry,
834 GNUNET_assert (size >= th->size);
835 ret = th->notify (th->notify_cls, size, buf);
842 * Ask the client to call us once the specified number of bytes
843 * are free in the transmission buffer. May call the notify
844 * method immediately if enough space is available.
846 * @param sock connection to the service
847 * @param size number of bytes to send
848 * @param timeout after how long should we give up (and call
849 * notify with buf NULL and size 0)?
850 * @param auto_retry if the connection to the service dies, should we
851 * automatically re-connect and retry (within the timeout period)
852 * or should we immediately fail in this case? Pass GNUNET_YES
853 * if the caller does not care about temporary connection errors,
854 * for example because the protocol is stateless
855 * @param notify function to call
856 * @param notify_cls closure for notify
857 * @return NULL if our buffer will never hold size bytes,
858 * a handle if the notify callback was queued (can be used to cancel)
860 struct GNUNET_CLIENT_TransmitHandle *
861 GNUNET_CLIENT_notify_transmit_ready (struct GNUNET_CLIENT_Connection *sock,
863 struct GNUNET_TIME_Relative timeout,
865 GNUNET_CONNECTION_TransmitReadyNotify
866 notify, void *notify_cls)
868 struct GNUNET_CLIENT_TransmitHandle *th;
870 if (NULL != sock->th)
872 th = GNUNET_malloc (sizeof (struct GNUNET_CLIENT_TransmitHandle));
875 th->timeout = GNUNET_TIME_relative_to_absolute (timeout);
876 th->auto_retry = auto_retry;
878 th->notify_cls = notify_cls;
879 th->attempts_left = MAX_ATTEMPTS;
880 th->th = GNUNET_CONNECTION_notify_transmit_ready (sock->sock,
896 * Cancel a request for notification.
898 * @param th handle from the original request.
901 GNUNET_CLIENT_notify_transmit_ready_cancel (struct
902 GNUNET_CLIENT_TransmitHandle *th)
904 if (th->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
906 GNUNET_break (NULL == th->th);
907 GNUNET_SCHEDULER_cancel (th->sock->sched, th->reconnect_task);
908 th->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
912 GNUNET_assert (NULL != th->th);
913 GNUNET_CONNECTION_notify_transmit_ready_cancel (th->th);
921 * Function called to notify a client about the socket
922 * begin ready to queue the message. "buf" will be
923 * NULL and "size" zero if the socket was closed for
924 * writing in the meantime.
926 * @param cls closure of type "struct TransmitGetResponseContext*"
927 * @param size number of bytes available in buf
928 * @param buf where the callee should write the message
929 * @return number of bytes written to buf
932 transmit_for_response (void *cls, size_t size, void *buf)
934 struct TransmitGetResponseContext *tc = cls;
937 tc->sock->tag = NULL;
938 msize = ntohs (tc->hdr->size);
942 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
943 _("Could not submit request, not expecting to receive a response.\n"));
945 tc->rn (tc->rn_cls, NULL);
949 GNUNET_assert (size >= msize);
950 memcpy (buf, tc->hdr, msize);
951 GNUNET_CLIENT_receive (tc->sock,
954 GNUNET_TIME_absolute_get_remaining (tc->timeout));
961 * Convenience API that combines sending a request
962 * to the service and waiting for a response.
963 * If either operation times out, the callback
964 * will be called with a "NULL" response (in which
965 * case the connection should probably be destroyed).
967 * @param sock connection to use
968 * @param hdr message to transmit
969 * @param timeout when to give up (for both transmission
970 * and for waiting for a response)
971 * @param auto_retry if the connection to the service dies, should we
972 * automatically re-connect and retry (within the timeout period)
973 * or should we immediately fail in this case? Pass GNUNET_YES
974 * if the caller does not care about temporary connection errors,
975 * for example because the protocol is stateless
976 * @param rn function to call with the response
977 * @param rn_cls closure for rn
978 * @return GNUNET_OK on success, GNUNET_SYSERR if a request
982 GNUNET_CLIENT_transmit_and_get_response (struct GNUNET_CLIENT_Connection
984 const struct GNUNET_MessageHeader
986 struct GNUNET_TIME_Relative timeout,
988 GNUNET_CLIENT_MessageHandler rn,
991 struct TransmitGetResponseContext *tc;
994 if (NULL != sock->th)
995 return GNUNET_SYSERR;
996 GNUNET_assert (sock->tag == NULL);
997 msize = ntohs (hdr->size);
998 tc = GNUNET_malloc (sizeof (struct TransmitGetResponseContext) + msize);
1000 tc->hdr = (const struct GNUNET_MessageHeader *) &tc[1];
1001 memcpy (&tc[1], hdr, msize);
1002 tc->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1004 tc->rn_cls = rn_cls;
1005 if (NULL == GNUNET_CLIENT_notify_transmit_ready (sock,
1009 &transmit_for_response,
1014 return GNUNET_SYSERR;
1022 /* end of client.c */