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;
161 struct GNUNET_CONFIGURATION_Handle *cfg;
164 * Name of the service we interact with.
169 * Context of a transmit_and_get_response operation, NULL
170 * if no such operation is pending.
172 struct TransmitGetResponseContext *tag;
175 * Handler for current receiver task.
177 GNUNET_CLIENT_MessageHandler receiver_handler;
180 * Closure for receiver_handler.
182 void *receiver_handler_cls;
185 * Handle for a pending transmission request, NULL if there is
188 struct GNUNET_CLIENT_TransmitHandle *th;
191 * Handler for service test completion (NULL unless in service_test)
193 GNUNET_SCHEDULER_Task test_cb;
196 * Deadline for calling 'test_cb'.
198 struct GNUNET_TIME_Absolute test_deadline;
201 * If we are re-trying and are delaying to do so,
202 * handle to the scheduled task managing the delay.
204 GNUNET_SCHEDULER_TaskIdentifier receive_task;
207 * Closure for test_cb (NULL unless in service_test)
212 * Buffer for received message.
217 * Timeout for receiving a response (absolute time).
219 struct GNUNET_TIME_Absolute receive_timeout;
222 * Current value for our incremental back-off (for
225 struct GNUNET_TIME_Relative back_off;
228 * Number of bytes in received_buf that are valid.
233 * Size of received_buf.
235 unsigned int received_size;
238 * Do we have a complete response in received_buf?
243 * Are we currently busy doing receive-processing?
244 * GNUNET_YES if so, GNUNET_NO if not.
249 * Are we ignoring shutdown signals?
256 static struct GNUNET_CONNECTION_Handle *
257 do_connect (struct GNUNET_SCHEDULER_Handle *sched,
258 const char *service_name,
259 const struct GNUNET_CONFIGURATION_Handle *cfg)
261 struct GNUNET_CONNECTION_Handle *sock;
263 unsigned long long port;
266 GNUNET_CONFIGURATION_get_value_number (cfg,
272 GNUNET_CONFIGURATION_get_value_string (cfg,
274 "HOSTNAME", &hostname)))
276 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
278 ("Could not determine valid hostname and port for service `%s' from configuration.\n"),
282 if (0 == strlen (hostname))
284 GNUNET_free (hostname);
285 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
286 _("Need a non-empty hostname for service `%s'.\n"),
290 sock = GNUNET_CONNECTION_create_from_connect (sched,
294 GNUNET_SERVER_MAX_MESSAGE_SIZE);
295 GNUNET_free (hostname);
301 * Get a connection with a service.
303 * @param sched scheduler to use
304 * @param service_name name of the service
305 * @param cfg configuration to use
306 * @return NULL on error (service unknown to configuration)
308 struct GNUNET_CLIENT_Connection *
309 GNUNET_CLIENT_connect (struct GNUNET_SCHEDULER_Handle *sched,
310 const char *service_name,
311 const struct GNUNET_CONFIGURATION_Handle *cfg)
313 struct GNUNET_CLIENT_Connection *ret;
314 struct GNUNET_CONNECTION_Handle *sock;
316 sock = do_connect (sched, service_name, cfg);
319 ret = GNUNET_malloc (sizeof (struct GNUNET_CLIENT_Connection));
322 ret->service_name = GNUNET_strdup (service_name);
323 ret->cfg = GNUNET_CONFIGURATION_dup (cfg);
324 ret->back_off = GNUNET_TIME_UNIT_MILLISECONDS;
330 * Configure this connection to ignore shutdown signals.
332 * @param h client handle
333 * @param do_ignore GNUNET_YES to ignore, GNUNET_NO to restore default
336 GNUNET_CLIENT_ignore_shutdown (struct GNUNET_CLIENT_Connection *h,
339 h->ignore_shutdown = do_ignore;
341 GNUNET_CONNECTION_ignore_shutdown (h->sock,
347 * Destroy connection with the service. This will automatically
348 * cancel any pending "receive" request (however, the handler will
349 * *NOT* be called, not even with a NULL message). Any pending
350 * transmission request will also be cancelled UNLESS the callback for
351 * the transmission request has already been called, in which case the
352 * transmission 'finish_pending_write' argument determines whether or
353 * not the write is guaranteed to complete before the socket is fully
354 * destroyed (unless, of course, there is an error with the server in
355 * which case the message may still be lost).
357 * @param finish_pending_write should a transmission already passed to the
358 * handle be completed?
359 * @param sock handle to the service connection
362 GNUNET_CLIENT_disconnect (struct GNUNET_CLIENT_Connection *sock,
363 int finish_pending_write)
365 GNUNET_assert (sock->sock != NULL);
366 if (sock->in_receive == GNUNET_YES)
368 GNUNET_CONNECTION_receive_cancel (sock->sock);
369 sock->in_receive = GNUNET_NO;
371 GNUNET_CONNECTION_destroy (sock->sock, finish_pending_write);
373 if (sock->tag != NULL)
375 GNUNET_free (sock->tag);
378 sock->receiver_handler = NULL;
379 if (sock->th != NULL)
380 GNUNET_CLIENT_notify_transmit_ready_cancel (sock->th);
381 if (sock->receive_task != GNUNET_SCHEDULER_NO_TASK)
383 GNUNET_SCHEDULER_cancel (sock->sched, sock->receive_task);
384 sock->receive_task = GNUNET_SCHEDULER_NO_TASK;
386 GNUNET_array_grow (sock->received_buf, sock->received_size, 0);
387 GNUNET_free (sock->service_name);
388 GNUNET_CONFIGURATION_destroy (sock->cfg);
394 * Check if message is complete
397 check_complete (struct GNUNET_CLIENT_Connection *conn)
399 if ((conn->received_pos >= sizeof (struct GNUNET_MessageHeader)) &&
400 (conn->received_pos >=
401 ntohs (((const struct GNUNET_MessageHeader *) conn->received_buf)->
403 conn->msg_complete = GNUNET_YES;
408 * Callback function for data received from the network. Note that
409 * both "available" and "errCode" would be 0 if the read simply timed out.
412 * @param buf pointer to received data
413 * @param available number of bytes availabe in "buf",
414 * possibly 0 (on errors)
415 * @param addr address of the sender
416 * @param addrlen size of addr
417 * @param errCode value of errno (on errors receiving)
420 receive_helper (void *cls,
423 const struct sockaddr *addr, socklen_t addrlen, int errCode)
425 struct GNUNET_CLIENT_Connection *conn = cls;
426 struct GNUNET_TIME_Relative remaining;
427 GNUNET_CLIENT_MessageHandler receive_handler;
428 void *receive_handler_cls;
430 GNUNET_assert (conn->msg_complete == GNUNET_NO);
431 conn->in_receive = GNUNET_NO;
432 if ((available == 0) || (conn->sock == NULL) || (errCode != 0))
434 /* signal timeout! */
435 if (NULL != (receive_handler = conn->receiver_handler))
437 receive_handler_cls = conn->receiver_handler_cls;
438 conn->receiver_handler = NULL;
439 receive_handler (receive_handler_cls, NULL);
444 /* FIXME: optimize for common fast case where buf contains the
445 entire message and we need no copying... */
448 /* slow path: append to array */
449 if (conn->received_size < conn->received_pos + available)
450 GNUNET_array_grow (conn->received_buf,
451 conn->received_size, conn->received_pos + available);
452 memcpy (&conn->received_buf[conn->received_pos], buf, available);
453 conn->received_pos += available;
454 check_complete (conn);
455 /* check for timeout */
456 remaining = GNUNET_TIME_absolute_get_remaining (conn->receive_timeout);
457 if (remaining.value == 0)
459 /* signal timeout! */
460 conn->receiver_handler (conn->receiver_handler_cls, NULL);
463 /* back to receive -- either for more data or to call callback! */
464 GNUNET_CLIENT_receive (conn,
465 conn->receiver_handler,
466 conn->receiver_handler_cls, remaining);
471 * Continuation to call the receive callback.
473 * @param cls our handle to the client connection
474 * @param tc scheduler context
477 receive_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
479 struct GNUNET_CLIENT_Connection *sock = cls;
480 GNUNET_CLIENT_MessageHandler handler = sock->receiver_handler;
481 const struct GNUNET_MessageHeader *cmsg =
482 (const struct GNUNET_MessageHeader *) sock->received_buf;
483 void *handler_cls = sock->receiver_handler_cls;
484 uint16_t msize = ntohs (cmsg->size);
486 struct GNUNET_MessageHeader *msg = (struct GNUNET_MessageHeader *) mbuf;
488 sock->receive_task = GNUNET_SCHEDULER_NO_TASK;
489 GNUNET_assert (GNUNET_YES == sock->msg_complete);
490 GNUNET_assert (sock->received_pos >= msize);
491 memcpy (msg, cmsg, msize);
492 memmove (sock->received_buf,
493 &sock->received_buf[msize], sock->received_pos - msize);
494 sock->received_pos -= msize;
495 sock->msg_complete = GNUNET_NO;
496 sock->receiver_handler = NULL;
497 check_complete (sock);
499 handler (handler_cls, msg);
504 * Read from the service.
506 * @param sock the service
507 * @param handler function to call with the message
508 * @param handler_cls closure for handler
509 * @param timeout how long to wait until timing out
512 GNUNET_CLIENT_receive (struct GNUNET_CLIENT_Connection *sock,
513 GNUNET_CLIENT_MessageHandler handler,
514 void *handler_cls, struct GNUNET_TIME_Relative timeout)
516 if (sock->sock == NULL)
518 /* already disconnected, fail instantly! */
519 GNUNET_break (0); /* this should not happen in well-written code! */
520 handler (handler_cls, NULL);
523 sock->receiver_handler = handler;
524 sock->receiver_handler_cls = handler_cls;
525 sock->receive_timeout = GNUNET_TIME_relative_to_absolute (timeout);
526 if (GNUNET_YES == sock->msg_complete)
528 sock->receive_task = GNUNET_SCHEDULER_add_after (sock->sched,
529 GNUNET_SCHEDULER_NO_TASK,
530 &receive_task, sock);
534 GNUNET_assert (sock->in_receive == GNUNET_NO);
535 sock->in_receive = GNUNET_YES;
536 GNUNET_CONNECTION_receive (sock->sock,
537 GNUNET_SERVER_MAX_MESSAGE_SIZE,
538 timeout, &receive_helper, sock);
544 * Report service unavailable.
547 service_test_error (struct GNUNET_SCHEDULER_Handle *s,
548 GNUNET_SCHEDULER_Task task, void *task_cls)
550 GNUNET_SCHEDULER_add_continuation (s,
553 GNUNET_SCHEDULER_REASON_TIMEOUT);
558 * Receive confirmation from test, service is up.
561 * @param msg message received, NULL on timeout or fatal error
564 confirm_handler (void *cls, const struct GNUNET_MessageHeader *msg)
566 struct GNUNET_CLIENT_Connection *conn = cls;
567 /* We may want to consider looking at the reply in more
568 detail in the future, for example, is this the
569 correct service? FIXME! */
573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
574 "Received confirmation that service is running.\n");
576 GNUNET_SCHEDULER_add_continuation (conn->sched,
579 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
583 service_test_error (conn->sched, conn->test_cb, conn->test_cb_cls);
585 GNUNET_CLIENT_disconnect (conn, GNUNET_NO);
590 write_test (void *cls, size_t size, void *buf)
592 struct GNUNET_CLIENT_Connection *conn = cls;
593 struct GNUNET_MessageHeader *msg;
595 if (size < sizeof (struct GNUNET_MessageHeader))
598 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
599 _("Failure to transmit TEST request.\n"));
601 service_test_error (conn->sched, conn->test_cb, conn->test_cb_cls);
602 GNUNET_CLIENT_disconnect (conn, GNUNET_NO);
603 return 0; /* client disconnected */
606 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
607 "Transmitting `%s' request.\n", "TEST");
609 msg = (struct GNUNET_MessageHeader *) buf;
610 msg->type = htons (GNUNET_MESSAGE_TYPE_TEST);
611 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
612 GNUNET_CLIENT_receive (conn,
615 GNUNET_TIME_absolute_get_remaining (conn->test_deadline));
616 return sizeof (struct GNUNET_MessageHeader);
621 * Wait until the service is running.
623 * @param sched scheduler to use
624 * @param service name of the service to wait for
625 * @param cfg configuration to use
626 * @param timeout how long to wait at most in ms
627 * @param task task to run if service is running
628 * (reason will be "PREREQ_DONE" (service running)
629 * or "TIMEOUT" (service not known to be running))
630 * @param task_cls closure for task
633 GNUNET_CLIENT_service_test (struct GNUNET_SCHEDULER_Handle *sched,
635 const struct GNUNET_CONFIGURATION_Handle *cfg,
636 struct GNUNET_TIME_Relative timeout,
637 GNUNET_SCHEDULER_Task task, void *task_cls)
639 struct GNUNET_CLIENT_Connection *conn;
642 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
643 "Testing if service `%s' is running.\n", service);
645 conn = GNUNET_CLIENT_connect (sched, service, cfg);
648 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
650 ("Could not connect to service `%s', must not be running.\n"),
652 service_test_error (sched, task, task_cls);
655 conn->test_cb = task;
656 conn->test_cb_cls = task_cls;
657 conn->test_deadline = GNUNET_TIME_relative_to_absolute (timeout);
659 if (NULL == GNUNET_CLIENT_notify_transmit_ready (conn,
660 sizeof (struct GNUNET_MessageHeader),
665 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
666 _("Failure to transmit request to service `%s'\n"),
668 service_test_error (sched, task, task_cls);
669 GNUNET_CLIENT_disconnect (conn, GNUNET_NO);
676 * Connection notifies us about failure or success of
677 * a transmission request. Either pass it on to our
678 * user or, if possible, retry.
680 * @param cls our "struct GNUNET_CLIENT_TransmissionHandle"
681 * @param size number of bytes available for transmission
682 * @param buf where to write them
683 * @return number of bytes written to buf
685 static size_t client_notify (void *cls, size_t size, void *buf);
689 * This task is run if we should re-try connection to the
690 * service after a while.
692 * @param cls our "struct GNUNET_CLIENT_TransmitHandle" of the request
696 client_delayed_retry (void *cls,
697 const struct GNUNET_SCHEDULER_TaskContext *tc)
699 struct GNUNET_CLIENT_TransmitHandle *th = cls;
701 th->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
702 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
705 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
706 "Transmission failed due to shutdown.\n");
709 th->notify (th->notify_cls, 0, NULL);
713 th->th = GNUNET_CONNECTION_notify_transmit_ready (th->sock->sock,
715 GNUNET_TIME_absolute_get_remaining
721 th->notify (th->notify_cls, 0, NULL);
729 * Connection notifies us about failure or success of a transmission
730 * request. Either pass it on to our user or, if possible, retry.
732 * @param cls our "struct GNUNET_CLIENT_TransmissionHandle"
733 * @param size number of bytes available for transmission
734 * @param buf where to write them
735 * @return number of bytes written to buf
738 client_notify (void *cls, size_t size, void *buf)
740 struct GNUNET_CLIENT_TransmitHandle *th = cls;
742 struct GNUNET_TIME_Relative delay;
748 delay = GNUNET_TIME_absolute_get_remaining (th->timeout);
750 if ( (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & GNUNET_SCHEDULER_get_reason (th->sock->sched))) ||
751 (GNUNET_YES != th->auto_retry) ||
752 (0 == --th->attempts_left) ||
756 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
757 "Transmission failed %u times, giving up.\n",
758 MAX_ATTEMPTS - th->attempts_left);
760 GNUNET_break (0 == th->notify (th->notify_cls, 0, NULL));
765 GNUNET_CONNECTION_destroy (th->sock->sock, GNUNET_NO);
766 th->sock->sock = do_connect (th->sock->sched,
767 th->sock->service_name, th->sock->cfg);
768 GNUNET_assert (NULL != th->sock->sock);
769 GNUNET_CONNECTION_ignore_shutdown (th->sock->sock,
770 th->sock->ignore_shutdown);
771 delay = GNUNET_TIME_relative_min (delay, th->sock->back_off);
773 = GNUNET_TIME_relative_min (GNUNET_TIME_relative_multiply (th->sock->back_off, 2),
774 GNUNET_TIME_UNIT_SECONDS);
776 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
777 "Transmission failed %u times, trying again in %llums.\n",
778 MAX_ATTEMPTS - th->attempts_left,
779 (unsigned long long) delay.value);
781 th->reconnect_task = GNUNET_SCHEDULER_add_delayed (th->sock->sched,
783 &client_delayed_retry,
788 GNUNET_assert (size >= th->size);
789 ret = th->notify (th->notify_cls, size, buf);
796 * Ask the client to call us once the specified number of bytes
797 * are free in the transmission buffer. May call the notify
798 * method immediately if enough space is available.
800 * @param sock connection to the service
801 * @param size number of bytes to send
802 * @param timeout after how long should we give up (and call
803 * notify with buf NULL and size 0)?
804 * @param auto_retry if the connection to the service dies, should we
805 * automatically re-connect and retry (within the timeout period)
806 * or should we immediately fail in this case? Pass GNUNET_YES
807 * if the caller does not care about temporary connection errors,
808 * for example because the protocol is stateless
809 * @param notify function to call
810 * @param notify_cls closure for notify
811 * @return NULL if our buffer will never hold size bytes,
812 * a handle if the notify callback was queued (can be used to cancel)
814 struct GNUNET_CLIENT_TransmitHandle *
815 GNUNET_CLIENT_notify_transmit_ready (struct GNUNET_CLIENT_Connection *sock,
817 struct GNUNET_TIME_Relative timeout,
819 GNUNET_CONNECTION_TransmitReadyNotify
820 notify, void *notify_cls)
822 struct GNUNET_CLIENT_TransmitHandle *th;
824 if (NULL != sock->th)
826 th = GNUNET_malloc (sizeof (struct GNUNET_CLIENT_TransmitHandle));
829 th->timeout = GNUNET_TIME_relative_to_absolute (timeout);
830 th->auto_retry = auto_retry;
832 th->notify_cls = notify_cls;
833 th->attempts_left = MAX_ATTEMPTS;
834 th->th = GNUNET_CONNECTION_notify_transmit_ready (sock->sock,
850 * Cancel a request for notification.
852 * @param th handle from the original request.
855 GNUNET_CLIENT_notify_transmit_ready_cancel (struct
856 GNUNET_CLIENT_TransmitHandle *th)
858 if (th->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
860 GNUNET_break (NULL == th->th);
861 GNUNET_SCHEDULER_cancel (th->sock->sched, th->reconnect_task);
862 th->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
866 GNUNET_break (NULL != th->th);
867 GNUNET_CONNECTION_notify_transmit_ready_cancel (th->th);
875 * Function called to notify a client about the socket
876 * begin ready to queue the message. "buf" will be
877 * NULL and "size" zero if the socket was closed for
878 * writing in the meantime.
880 * @param cls closure of type "struct TransmitGetResponseContext*"
881 * @param size number of bytes available in buf
882 * @param buf where the callee should write the message
883 * @return number of bytes written to buf
886 transmit_for_response (void *cls, size_t size, void *buf)
888 struct TransmitGetResponseContext *tc = cls;
891 tc->sock->tag = NULL;
892 msize = ntohs (tc->hdr->size);
896 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
897 _("Could not submit request, not expecting to receive a response.\n"));
899 tc->rn (tc->rn_cls, NULL);
903 GNUNET_assert (size >= msize);
904 memcpy (buf, tc->hdr, msize);
905 GNUNET_CLIENT_receive (tc->sock,
908 GNUNET_TIME_absolute_get_remaining (tc->timeout));
915 * Convenience API that combines sending a request
916 * to the service and waiting for a response.
917 * If either operation times out, the callback
918 * will be called with a "NULL" response (in which
919 * case the connection should probably be destroyed).
921 * @param sock connection to use
922 * @param hdr message to transmit
923 * @param timeout when to give up (for both transmission
924 * and for waiting for a response)
925 * @param auto_retry if the connection to the service dies, should we
926 * automatically re-connect and retry (within the timeout period)
927 * or should we immediately fail in this case? Pass GNUNET_YES
928 * if the caller does not care about temporary connection errors,
929 * for example because the protocol is stateless
930 * @param rn function to call with the response
931 * @param rn_cls closure for rn
932 * @return GNUNET_OK on success, GNUNET_SYSERR if a request
936 GNUNET_CLIENT_transmit_and_get_response (struct GNUNET_CLIENT_Connection
938 const struct GNUNET_MessageHeader
940 struct GNUNET_TIME_Relative timeout,
942 GNUNET_CLIENT_MessageHandler rn,
945 struct TransmitGetResponseContext *tc;
948 if (NULL != sock->th)
949 return GNUNET_SYSERR;
950 GNUNET_assert (sock->tag == NULL);
951 msize = ntohs (hdr->size);
952 tc = GNUNET_malloc (sizeof (struct TransmitGetResponseContext) + msize);
954 tc->hdr = (const struct GNUNET_MessageHeader *) &tc[1];
955 memcpy (&tc[1], hdr, msize);
956 tc->timeout = GNUNET_TIME_relative_to_absolute (timeout);
959 if (NULL == GNUNET_CLIENT_notify_transmit_ready (sock,
963 &transmit_for_response,
968 return GNUNET_SYSERR;
976 /* end of client.c */