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
41 * How often do we re-try tranmsitting requests before giving up?
42 * Note that if we succeeded transmitting a request but failed to read
43 * a response, we do NOT re-try.
45 #define MAX_ATTEMPTS 50
49 * Handle for a transmission request.
51 struct GNUNET_CLIENT_TransmitHandle
56 struct GNUNET_CLIENT_Connection *sock;
59 * Function to call to get the data for transmission.
61 GNUNET_CONNECTION_TransmitReadyNotify notify;
69 * Handle to the transmission with the underlying
72 struct GNUNET_CONNECTION_TransmitHandle *th;
75 * If we are re-trying and are delaying to do so,
76 * handle to the scheduled task managing the delay.
78 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
81 * Timeout for the operation overall.
83 struct GNUNET_TIME_Absolute timeout;
86 * Number of bytes requested.
91 * Are we allowed to re-try to connect without telling
92 * the user (of this API) about the connection troubles?
97 * Number of attempts left for transmitting the request. We may
98 * fail the first time (say because the service is not yet up), in
99 * which case (if auto_retry is set) we wait a bit and re-try
100 * (timeout permitting).
102 unsigned int attempts_left;
108 * Context for processing
109 * "GNUNET_CLIENT_transmit_and_get_response" requests.
111 struct TransmitGetResponseContext
116 struct GNUNET_CLIENT_Connection *sock;
119 * Message to transmit; do not free, allocated
120 * right after this struct.
122 const struct GNUNET_MessageHeader *hdr;
127 struct GNUNET_TIME_Absolute timeout;
130 * Function to call when done.
132 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;
158 struct GNUNET_SCHEDULER_Handle *sched;
163 struct GNUNET_CONFIGURATION_Handle *cfg;
166 * Name of the service we interact with.
171 * Context of a transmit_and_get_response operation, NULL
172 * if no such operation is pending.
174 struct TransmitGetResponseContext *tag;
177 * Handler for current receiver task.
179 GNUNET_CLIENT_MessageHandler receiver_handler;
182 * Closure for receiver_handler.
184 void *receiver_handler_cls;
187 * Handle for a pending transmission request, NULL if there is
190 struct GNUNET_CLIENT_TransmitHandle *th;
193 * Handler for service test completion (NULL unless in service_test)
195 GNUNET_SCHEDULER_Task test_cb;
198 * If we are re-trying and are delaying to do so,
199 * handle to the scheduled task managing the delay.
201 GNUNET_SCHEDULER_TaskIdentifier receive_task;
204 * Closure for test_cb (NULL unless in service_test)
209 * Buffer for received message.
214 * Timeout for receiving a response (absolute time).
216 struct GNUNET_TIME_Absolute receive_timeout;
219 * Current value for our incremental back-off (for
222 struct GNUNET_TIME_Relative back_off;
225 * Number of bytes in received_buf that are valid.
230 * Size of received_buf.
232 unsigned int received_size;
235 * Do we have a complete response in received_buf?
240 * Are we currently busy doing receive-processing?
241 * GNUNET_YES if so, GNUNET_NO if not.
246 * Are we ignoring shutdown signals?
253 static struct GNUNET_CONNECTION_Handle *
254 do_connect (struct GNUNET_SCHEDULER_Handle *sched,
255 const char *service_name,
256 const struct GNUNET_CONFIGURATION_Handle *cfg)
258 struct GNUNET_CONNECTION_Handle *sock;
260 unsigned long long port;
263 GNUNET_CONFIGURATION_get_value_number (cfg,
269 GNUNET_CONFIGURATION_get_value_string (cfg,
271 "HOSTNAME", &hostname)))
273 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
275 ("Could not determine valid hostname and port for service `%s' from configuration.\n"),
279 if (0 == strlen (hostname))
281 GNUNET_free (hostname);
282 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
283 _("Need a non-empty hostname for service `%s'.\n"),
287 sock = GNUNET_CONNECTION_create_from_connect (sched,
291 GNUNET_SERVER_MAX_MESSAGE_SIZE);
292 GNUNET_free (hostname);
298 * Get a connection with a service.
300 * @param sched scheduler to use
301 * @param service_name name of the service
302 * @param cfg configuration to use
303 * @return NULL on error (service unknown to configuration)
305 struct GNUNET_CLIENT_Connection *
306 GNUNET_CLIENT_connect (struct GNUNET_SCHEDULER_Handle *sched,
307 const char *service_name,
308 const struct GNUNET_CONFIGURATION_Handle *cfg)
310 struct GNUNET_CLIENT_Connection *ret;
311 struct GNUNET_CONNECTION_Handle *sock;
313 sock = do_connect (sched, service_name, cfg);
316 ret = GNUNET_malloc (sizeof (struct GNUNET_CLIENT_Connection));
319 ret->service_name = GNUNET_strdup (service_name);
320 ret->cfg = GNUNET_CONFIGURATION_dup (cfg);
321 ret->back_off = GNUNET_TIME_UNIT_MILLISECONDS;
326 * Configure this connection to ignore shutdown signals.
328 * @param h client handle
329 * @param do_ignore GNUNET_YES to ignore, GNUNET_NO to restore default
332 GNUNET_CLIENT_ignore_shutdown (struct GNUNET_CLIENT_Connection *h,
335 h->ignore_shutdown = do_ignore;
337 GNUNET_CONNECTION_ignore_shutdown (h->sock,
343 * Destroy connection with the service. This will automatically
344 * cancel any pending "receive" request (however, the handler will
345 * *NOT* be called, not even with a NULL message). Any pending
346 * transmission request will also be cancelled UNLESS the callback for
347 * the transmission request has already been called, in which case the
348 * transmission is guaranteed to complete before the socket is fully
349 * destroyed (unless, of course, there is an error with the server
350 * in which case the message may still be lost).
352 * @param sock handle to the service connection
355 GNUNET_CLIENT_disconnect (struct GNUNET_CLIENT_Connection *sock)
357 GNUNET_assert (sock->sock != NULL);
358 if (sock->in_receive == GNUNET_YES)
360 GNUNET_CONNECTION_receive_cancel (sock->sock);
361 sock->in_receive = GNUNET_NO;
363 GNUNET_CONNECTION_destroy (sock->sock);
365 if (sock->tag != NULL)
367 GNUNET_free (sock->tag);
370 sock->receiver_handler = NULL;
371 if (sock->th != NULL)
372 GNUNET_CLIENT_notify_transmit_ready_cancel (sock->th);
373 if (sock->receive_task != GNUNET_SCHEDULER_NO_TASK)
375 GNUNET_SCHEDULER_cancel (sock->sched, sock->receive_task);
376 sock->receive_task = GNUNET_SCHEDULER_NO_TASK;
378 GNUNET_array_grow (sock->received_buf, sock->received_size, 0);
379 GNUNET_free (sock->service_name);
380 GNUNET_CONFIGURATION_destroy (sock->cfg);
386 * Check if message is complete
389 check_complete (struct GNUNET_CLIENT_Connection *conn)
391 if ((conn->received_pos >= sizeof (struct GNUNET_MessageHeader)) &&
392 (conn->received_pos >=
393 ntohs (((const struct GNUNET_MessageHeader *) conn->received_buf)->
395 conn->msg_complete = GNUNET_YES;
400 * Callback function for data received from the network. Note that
401 * both "available" and "errCode" would be 0 if the read simply timed out.
404 * @param buf pointer to received data
405 * @param available number of bytes availabe in "buf",
406 * possibly 0 (on errors)
407 * @param addr address of the sender
408 * @param addrlen size of addr
409 * @param errCode value of errno (on errors receiving)
412 receive_helper (void *cls,
415 const struct sockaddr *addr, socklen_t addrlen, int errCode)
417 struct GNUNET_CLIENT_Connection *conn = cls;
418 struct GNUNET_TIME_Relative remaining;
419 GNUNET_CLIENT_MessageHandler receive_handler;
420 void *receive_handler_cls;
422 GNUNET_assert (conn->msg_complete == GNUNET_NO);
423 conn->in_receive = GNUNET_NO;
424 if ((available == 0) || (conn->sock == NULL) || (errCode != 0))
426 /* signal timeout! */
427 if (NULL != (receive_handler = conn->receiver_handler))
429 receive_handler_cls = conn->receiver_handler_cls;
430 conn->receiver_handler = NULL;
431 receive_handler (receive_handler_cls, NULL);
436 /* FIXME: optimize for common fast case where buf contains the
437 entire message and we need no copying... */
440 /* slow path: append to array */
441 if (conn->received_size < conn->received_pos + available)
442 GNUNET_array_grow (conn->received_buf,
443 conn->received_size, conn->received_pos + available);
444 memcpy (&conn->received_buf[conn->received_pos], buf, available);
445 conn->received_pos += available;
446 check_complete (conn);
447 /* check for timeout */
448 remaining = GNUNET_TIME_absolute_get_remaining (conn->receive_timeout);
449 if (remaining.value == 0)
451 /* signal timeout! */
452 conn->receiver_handler (conn->receiver_handler_cls, NULL);
455 /* back to receive -- either for more data or to call callback! */
456 GNUNET_CLIENT_receive (conn,
457 conn->receiver_handler,
458 conn->receiver_handler_cls, remaining);
463 * Continuation to call the receive callback.
465 * @param cls our handle to the client connection
466 * @param tc scheduler context
469 receive_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
471 struct GNUNET_CLIENT_Connection *sock = cls;
472 GNUNET_CLIENT_MessageHandler handler = sock->receiver_handler;
473 const struct GNUNET_MessageHeader *cmsg =
474 (const struct GNUNET_MessageHeader *) sock->received_buf;
475 void *handler_cls = sock->receiver_handler_cls;
476 uint16_t msize = ntohs (cmsg->size);
478 struct GNUNET_MessageHeader *msg = (struct GNUNET_MessageHeader *) mbuf;
480 sock->receive_task = GNUNET_SCHEDULER_NO_TASK;
481 GNUNET_assert (GNUNET_YES == sock->msg_complete);
482 GNUNET_assert (sock->received_pos >= msize);
483 memcpy (msg, cmsg, msize);
484 memmove (sock->received_buf,
485 &sock->received_buf[msize], sock->received_pos - msize);
486 sock->received_pos -= msize;
487 sock->msg_complete = GNUNET_NO;
488 sock->receiver_handler = NULL;
489 check_complete (sock);
491 handler (handler_cls, msg);
496 * Read from the service.
498 * @param sock the service
499 * @param handler function to call with the message
500 * @param handler_cls closure for handler
501 * @param timeout how long to wait until timing out
504 GNUNET_CLIENT_receive (struct GNUNET_CLIENT_Connection *sock,
505 GNUNET_CLIENT_MessageHandler handler,
506 void *handler_cls, struct GNUNET_TIME_Relative timeout)
508 if (sock->sock == NULL)
510 /* already disconnected, fail instantly! */
511 GNUNET_break (0); /* this should not happen in well-written code! */
512 handler (handler_cls, NULL);
515 sock->receiver_handler = handler;
516 sock->receiver_handler_cls = handler_cls;
517 sock->receive_timeout = GNUNET_TIME_relative_to_absolute (timeout);
518 if (GNUNET_YES == sock->msg_complete)
520 sock->receive_task = GNUNET_SCHEDULER_add_after (sock->sched,
521 GNUNET_SCHEDULER_NO_TASK,
522 &receive_task, sock);
526 sock->in_receive = GNUNET_YES;
527 GNUNET_CONNECTION_receive (sock->sock,
528 GNUNET_SERVER_MAX_MESSAGE_SIZE,
529 timeout, &receive_helper, sock);
535 * If possible, write a shutdown message to the target
536 * buffer and destroy the client connection.
538 * @param cls the "struct GNUNET_CLIENT_Connection" to destroy
539 * @param size number of bytes available in buf
540 * @param buf NULL on error, otherwise target buffer
541 * @return number of bytes written to buf
544 write_shutdown (void *cls, size_t size, void *buf)
546 struct GNUNET_MessageHeader *msg;
547 struct GNUNET_CLIENT_Connection *sock = cls;
549 GNUNET_CLIENT_disconnect (sock);
550 if (size < sizeof (struct GNUNET_MessageHeader))
552 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
553 _("Failed to transmit shutdown request to client.\n"));
554 return 0; /* client disconnected */
556 msg = (struct GNUNET_MessageHeader *) buf;
557 msg->type = htons (GNUNET_MESSAGE_TYPE_SHUTDOWN);
558 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
559 return sizeof (struct GNUNET_MessageHeader);
564 * Request that the service should shutdown.
565 * Afterwards, the connection will automatically be
566 * disconnected. Hence the "sock" shoud not
567 * be used by the caller after this call
568 * (calling this function frees "sock" after a while).
570 * @param sock the socket connected to the service
573 GNUNET_CLIENT_service_shutdown (struct GNUNET_CLIENT_Connection *sock)
575 GNUNET_CONNECTION_notify_transmit_ready (sock->sock,
577 GNUNET_MessageHeader),
578 GNUNET_TIME_UNIT_FOREVER_REL,
579 &write_shutdown, sock);
584 * Report service unavailable.
587 service_test_error (struct GNUNET_SCHEDULER_Handle *s,
588 GNUNET_SCHEDULER_Task task, void *task_cls)
590 GNUNET_SCHEDULER_add_continuation (s,
593 GNUNET_SCHEDULER_REASON_TIMEOUT);
598 * Receive confirmation from test, service is up.
601 * @param msg message received, NULL on timeout or fatal error
604 confirm_handler (void *cls, const struct GNUNET_MessageHeader *msg)
606 struct GNUNET_CLIENT_Connection *conn = cls;
607 /* We may want to consider looking at the reply in more
608 detail in the future, for example, is this the
609 correct service? FIXME! */
613 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
614 "Received confirmation that service is running.\n");
616 GNUNET_SCHEDULER_add_continuation (conn->sched,
619 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
623 service_test_error (conn->sched, conn->test_cb, conn->test_cb_cls);
625 GNUNET_CLIENT_disconnect (conn);
630 write_test (void *cls, size_t size, void *buf)
632 struct GNUNET_MessageHeader *msg;
634 if (size < sizeof (struct GNUNET_MessageHeader))
637 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
638 _("Failure to transmit TEST request.\n"));
640 return 0; /* client disconnected */
643 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
644 "Transmitting `%s' request.\n", "TEST");
646 msg = (struct GNUNET_MessageHeader *) buf;
647 msg->type = htons (GNUNET_MESSAGE_TYPE_TEST);
648 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
649 return sizeof (struct GNUNET_MessageHeader);
654 * Wait until the service is running.
656 * @param sched scheduler to use
657 * @param service name of the service to wait for
658 * @param cfg configuration to use
659 * @param timeout how long to wait at most in ms
660 * @param task task to run if service is running
661 * (reason will be "PREREQ_DONE" (service running)
662 * or "TIMEOUT" (service not known to be running))
663 * @param task_cls closure for task
666 GNUNET_CLIENT_service_test (struct GNUNET_SCHEDULER_Handle *sched,
668 const struct GNUNET_CONFIGURATION_Handle *cfg,
669 struct GNUNET_TIME_Relative timeout,
670 GNUNET_SCHEDULER_Task task, void *task_cls)
672 struct GNUNET_CLIENT_Connection *conn;
675 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
676 "Testing if service `%s' is running.\n", service);
678 conn = GNUNET_CLIENT_connect (sched, service, cfg);
681 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
683 ("Could not connect to service `%s', must not be running.\n"),
685 service_test_error (sched, task, task_cls);
688 conn->test_cb = task;
689 conn->test_cb_cls = task_cls;
691 GNUNET_CONNECTION_notify_transmit_ready (conn->sock,
693 GNUNET_MessageHeader),
694 timeout, &write_test, NULL))
696 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
697 _("Failure to transmit request to service `%s'\n"),
699 service_test_error (sched, task, task_cls);
700 GNUNET_CLIENT_disconnect (conn);
703 GNUNET_CLIENT_receive (conn, &confirm_handler, conn, timeout);
708 * Connection notifies us about failure or success of
709 * a transmission request. Either pass it on to our
710 * user or, if possible, retry.
712 * @param cls our "struct GNUNET_CLIENT_TransmissionHandle"
713 * @param size number of bytes available for transmission
714 * @param buf where to write them
715 * @return number of bytes written to buf
717 static size_t client_notify (void *cls, size_t size, void *buf);
721 * This task is run if we should re-try connection to the
722 * service after a while.
724 * @param cls our "struct GNUNET_CLIENT_TransmitHandle" of the request
728 client_delayed_retry (void *cls,
729 const struct GNUNET_SCHEDULER_TaskContext *tc)
731 struct GNUNET_CLIENT_TransmitHandle *th = cls;
733 th->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
734 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
737 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
738 "Transmission failed due to shutdown.\n");
741 th->notify (th->notify_cls, 0, NULL);
745 th->th = GNUNET_CONNECTION_notify_transmit_ready (th->sock->sock,
747 GNUNET_TIME_absolute_get_remaining
753 th->notify (th->notify_cls, 0, NULL);
761 * Connection notifies us about failure or success of
762 * a transmission request. Either pass it on to our
763 * user or, if possible, retry.
765 * @param cls our "struct GNUNET_CLIENT_TransmissionHandle"
766 * @param size number of bytes available for transmission
767 * @param buf where to write them
768 * @return number of bytes written to buf
771 client_notify (void *cls, size_t size, void *buf)
773 struct GNUNET_CLIENT_TransmitHandle *th = cls;
775 struct GNUNET_TIME_Relative delay;
781 delay = GNUNET_TIME_absolute_get_remaining (th->timeout);
783 if ( (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & GNUNET_SCHEDULER_get_reason (th->sock->sched))) ||
784 (GNUNET_YES != th->auto_retry) ||
785 (0 == --th->attempts_left) ||
789 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
790 "Transmission failed %u times, giving up.\n",
791 MAX_ATTEMPTS - th->attempts_left);
793 GNUNET_break (0 == th->notify (th->notify_cls, 0, NULL));
798 GNUNET_CONNECTION_destroy (th->sock->sock);
799 th->sock->sock = do_connect (th->sock->sched,
800 th->sock->service_name, th->sock->cfg);
801 GNUNET_assert (NULL != th->sock->sock);
802 GNUNET_CONNECTION_ignore_shutdown (th->sock->sock,
803 th->sock->ignore_shutdown);
804 delay = GNUNET_TIME_relative_min (delay, th->sock->back_off);
806 = GNUNET_TIME_relative_min (GNUNET_TIME_relative_multiply (th->sock->back_off, 2),
807 GNUNET_TIME_UNIT_SECONDS);
809 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
810 "Transmission failed %u times, trying again in %llums.\n",
811 MAX_ATTEMPTS - th->attempts_left,
812 (unsigned long long) delay.value);
814 th->reconnect_task = GNUNET_SCHEDULER_add_delayed (th->sock->sched,
816 &client_delayed_retry,
821 GNUNET_assert (size >= th->size);
822 ret = th->notify (th->notify_cls, size, buf);
829 * Ask the client to call us once the specified number of bytes
830 * are free in the transmission buffer. May call the notify
831 * method immediately if enough space is available.
833 * @param sock connection to the service
834 * @param size number of bytes to send
835 * @param timeout after how long should we give up (and call
836 * notify with buf NULL and size 0)?
837 * @param auto_retry if the connection to the service dies, should we
838 * automatically re-connect and retry (within the timeout period)
839 * or should we immediately fail in this case? Pass GNUNET_YES
840 * if the caller does not care about temporary connection errors,
841 * for example because the protocol is stateless
842 * @param notify function to call
843 * @param notify_cls closure for notify
844 * @return NULL if our buffer will never hold size bytes,
845 * a handle if the notify callback was queued (can be used to cancel)
847 struct GNUNET_CLIENT_TransmitHandle *
848 GNUNET_CLIENT_notify_transmit_ready (struct GNUNET_CLIENT_Connection *sock,
850 struct GNUNET_TIME_Relative timeout,
852 GNUNET_CONNECTION_TransmitReadyNotify
853 notify, void *notify_cls)
855 struct GNUNET_CLIENT_TransmitHandle *th;
857 if (NULL != sock->th)
859 th = GNUNET_malloc (sizeof (struct GNUNET_CLIENT_TransmitHandle));
862 th->timeout = GNUNET_TIME_relative_to_absolute (timeout);
863 th->auto_retry = auto_retry;
865 th->notify_cls = notify_cls;
866 th->attempts_left = MAX_ATTEMPTS;
867 th->th = GNUNET_CONNECTION_notify_transmit_ready (sock->sock,
883 * Cancel a request for notification.
885 * @param th handle from the original request.
888 GNUNET_CLIENT_notify_transmit_ready_cancel (struct
889 GNUNET_CLIENT_TransmitHandle *th)
891 if (th->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
893 GNUNET_break (NULL == th->th);
894 GNUNET_SCHEDULER_cancel (th->sock->sched, th->reconnect_task);
895 th->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
899 GNUNET_break (NULL != th->th);
900 GNUNET_CONNECTION_notify_transmit_ready_cancel (th->th);
908 * Function called to notify a client about the socket
909 * begin ready to queue the message. "buf" will be
910 * NULL and "size" zero if the socket was closed for
911 * writing in the meantime.
913 * @param cls closure of type "struct TransmitGetResponseContext*"
914 * @param size number of bytes available in buf
915 * @param buf where the callee should write the message
916 * @return number of bytes written to buf
919 transmit_for_response (void *cls, size_t size, void *buf)
921 struct TransmitGetResponseContext *tc = cls;
924 tc->sock->tag = NULL;
925 msize = ntohs (tc->hdr->size);
929 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
930 _("Could not submit request, not expecting to receive a response.\n"));
932 tc->rn (tc->rn_cls, NULL);
936 GNUNET_assert (size >= msize);
937 memcpy (buf, tc->hdr, msize);
938 GNUNET_CLIENT_receive (tc->sock,
941 GNUNET_TIME_absolute_get_remaining (tc->timeout));
948 * Convenience API that combines sending a request
949 * to the service and waiting for a response.
950 * If either operation times out, the callback
951 * will be called with a "NULL" response (in which
952 * case the connection should probably be destroyed).
954 * @param sock connection to use
955 * @param hdr message to transmit
956 * @param timeout when to give up (for both transmission
957 * and for waiting for a response)
958 * @param auto_retry if the connection to the service dies, should we
959 * automatically re-connect and retry (within the timeout period)
960 * or should we immediately fail in this case? Pass GNUNET_YES
961 * if the caller does not care about temporary connection errors,
962 * for example because the protocol is stateless
963 * @param rn function to call with the response
964 * @param rn_cls closure for rn
965 * @return GNUNET_OK on success, GNUNET_SYSERR if a request
969 GNUNET_CLIENT_transmit_and_get_response (struct GNUNET_CLIENT_Connection
971 const struct GNUNET_MessageHeader
973 struct GNUNET_TIME_Relative timeout,
975 GNUNET_CLIENT_MessageHandler rn,
978 struct TransmitGetResponseContext *tc;
981 if (NULL != sock->th)
982 return GNUNET_SYSERR;
983 GNUNET_assert (sock->tag == NULL);
984 msize = ntohs (hdr->size);
985 tc = GNUNET_malloc (sizeof (struct TransmitGetResponseContext) + msize);
987 tc->hdr = (const struct GNUNET_MessageHeader *) &tc[1];
988 memcpy (&tc[1], hdr, msize);
989 tc->timeout = GNUNET_TIME_relative_to_absolute (timeout);
992 if (NULL == GNUNET_CLIENT_notify_transmit_ready (sock,
996 &transmit_for_response,
1001 return GNUNET_SYSERR;
1009 /* end of client.c */