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 sock->in_receive = GNUNET_YES;
535 GNUNET_CONNECTION_receive (sock->sock,
536 GNUNET_SERVER_MAX_MESSAGE_SIZE,
537 timeout, &receive_helper, sock);
543 * Report service unavailable.
546 service_test_error (struct GNUNET_SCHEDULER_Handle *s,
547 GNUNET_SCHEDULER_Task task, void *task_cls)
549 GNUNET_SCHEDULER_add_continuation (s,
552 GNUNET_SCHEDULER_REASON_TIMEOUT);
557 * Receive confirmation from test, service is up.
560 * @param msg message received, NULL on timeout or fatal error
563 confirm_handler (void *cls, const struct GNUNET_MessageHeader *msg)
565 struct GNUNET_CLIENT_Connection *conn = cls;
566 /* We may want to consider looking at the reply in more
567 detail in the future, for example, is this the
568 correct service? FIXME! */
572 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
573 "Received confirmation that service is running.\n");
575 GNUNET_SCHEDULER_add_continuation (conn->sched,
578 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
582 service_test_error (conn->sched, conn->test_cb, conn->test_cb_cls);
584 GNUNET_CLIENT_disconnect (conn, GNUNET_NO);
589 write_test (void *cls, size_t size, void *buf)
591 struct GNUNET_CLIENT_Connection *conn = cls;
592 struct GNUNET_MessageHeader *msg;
594 if (size < sizeof (struct GNUNET_MessageHeader))
597 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
598 _("Failure to transmit TEST request.\n"));
600 service_test_error (conn->sched, conn->test_cb, conn->test_cb_cls);
601 GNUNET_CLIENT_disconnect (conn, GNUNET_NO);
602 return 0; /* client disconnected */
605 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
606 "Transmitting `%s' request.\n", "TEST");
608 msg = (struct GNUNET_MessageHeader *) buf;
609 msg->type = htons (GNUNET_MESSAGE_TYPE_TEST);
610 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
611 GNUNET_CLIENT_receive (conn,
614 GNUNET_TIME_absolute_get_remaining (conn->test_deadline));
615 return sizeof (struct GNUNET_MessageHeader);
620 * Wait until the service is running.
622 * @param sched scheduler to use
623 * @param service name of the service to wait for
624 * @param cfg configuration to use
625 * @param timeout how long to wait at most in ms
626 * @param task task to run if service is running
627 * (reason will be "PREREQ_DONE" (service running)
628 * or "TIMEOUT" (service not known to be running))
629 * @param task_cls closure for task
632 GNUNET_CLIENT_service_test (struct GNUNET_SCHEDULER_Handle *sched,
634 const struct GNUNET_CONFIGURATION_Handle *cfg,
635 struct GNUNET_TIME_Relative timeout,
636 GNUNET_SCHEDULER_Task task, void *task_cls)
638 struct GNUNET_CLIENT_Connection *conn;
641 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
642 "Testing if service `%s' is running.\n", service);
644 conn = GNUNET_CLIENT_connect (sched, service, cfg);
647 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
649 ("Could not connect to service `%s', must not be running.\n"),
651 service_test_error (sched, task, task_cls);
654 conn->test_cb = task;
655 conn->test_cb_cls = task_cls;
656 conn->test_deadline = GNUNET_TIME_relative_to_absolute (timeout);
658 if (NULL == GNUNET_CLIENT_notify_transmit_ready (conn,
659 sizeof (struct GNUNET_MessageHeader),
664 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
665 _("Failure to transmit request to service `%s'\n"),
667 service_test_error (sched, task, task_cls);
668 GNUNET_CLIENT_disconnect (conn, GNUNET_NO);
675 * Connection notifies us about failure or success of
676 * a transmission request. Either pass it on to our
677 * user or, if possible, retry.
679 * @param cls our "struct GNUNET_CLIENT_TransmissionHandle"
680 * @param size number of bytes available for transmission
681 * @param buf where to write them
682 * @return number of bytes written to buf
684 static size_t client_notify (void *cls, size_t size, void *buf);
688 * This task is run if we should re-try connection to the
689 * service after a while.
691 * @param cls our "struct GNUNET_CLIENT_TransmitHandle" of the request
695 client_delayed_retry (void *cls,
696 const struct GNUNET_SCHEDULER_TaskContext *tc)
698 struct GNUNET_CLIENT_TransmitHandle *th = cls;
700 th->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
701 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
704 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
705 "Transmission failed due to shutdown.\n");
708 th->notify (th->notify_cls, 0, NULL);
712 th->th = GNUNET_CONNECTION_notify_transmit_ready (th->sock->sock,
714 GNUNET_TIME_absolute_get_remaining
720 th->notify (th->notify_cls, 0, NULL);
728 * Connection notifies us about failure or success of a transmission
729 * request. Either pass it on to our user or, if possible, retry.
731 * @param cls our "struct GNUNET_CLIENT_TransmissionHandle"
732 * @param size number of bytes available for transmission
733 * @param buf where to write them
734 * @return number of bytes written to buf
737 client_notify (void *cls, size_t size, void *buf)
739 struct GNUNET_CLIENT_TransmitHandle *th = cls;
741 struct GNUNET_TIME_Relative delay;
747 delay = GNUNET_TIME_absolute_get_remaining (th->timeout);
749 if ( (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & GNUNET_SCHEDULER_get_reason (th->sock->sched))) ||
750 (GNUNET_YES != th->auto_retry) ||
751 (0 == --th->attempts_left) ||
755 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
756 "Transmission failed %u times, giving up.\n",
757 MAX_ATTEMPTS - th->attempts_left);
759 GNUNET_break (0 == th->notify (th->notify_cls, 0, NULL));
764 GNUNET_CONNECTION_destroy (th->sock->sock, GNUNET_NO);
765 th->sock->sock = do_connect (th->sock->sched,
766 th->sock->service_name, th->sock->cfg);
767 GNUNET_assert (NULL != th->sock->sock);
768 GNUNET_CONNECTION_ignore_shutdown (th->sock->sock,
769 th->sock->ignore_shutdown);
770 delay = GNUNET_TIME_relative_min (delay, th->sock->back_off);
772 = GNUNET_TIME_relative_min (GNUNET_TIME_relative_multiply (th->sock->back_off, 2),
773 GNUNET_TIME_UNIT_SECONDS);
775 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
776 "Transmission failed %u times, trying again in %llums.\n",
777 MAX_ATTEMPTS - th->attempts_left,
778 (unsigned long long) delay.value);
780 th->reconnect_task = GNUNET_SCHEDULER_add_delayed (th->sock->sched,
782 &client_delayed_retry,
787 GNUNET_assert (size >= th->size);
788 ret = th->notify (th->notify_cls, size, buf);
795 * Ask the client to call us once the specified number of bytes
796 * are free in the transmission buffer. May call the notify
797 * method immediately if enough space is available.
799 * @param sock connection to the service
800 * @param size number of bytes to send
801 * @param timeout after how long should we give up (and call
802 * notify with buf NULL and size 0)?
803 * @param auto_retry if the connection to the service dies, should we
804 * automatically re-connect and retry (within the timeout period)
805 * or should we immediately fail in this case? Pass GNUNET_YES
806 * if the caller does not care about temporary connection errors,
807 * for example because the protocol is stateless
808 * @param notify function to call
809 * @param notify_cls closure for notify
810 * @return NULL if our buffer will never hold size bytes,
811 * a handle if the notify callback was queued (can be used to cancel)
813 struct GNUNET_CLIENT_TransmitHandle *
814 GNUNET_CLIENT_notify_transmit_ready (struct GNUNET_CLIENT_Connection *sock,
816 struct GNUNET_TIME_Relative timeout,
818 GNUNET_CONNECTION_TransmitReadyNotify
819 notify, void *notify_cls)
821 struct GNUNET_CLIENT_TransmitHandle *th;
823 if (NULL != sock->th)
825 th = GNUNET_malloc (sizeof (struct GNUNET_CLIENT_TransmitHandle));
828 th->timeout = GNUNET_TIME_relative_to_absolute (timeout);
829 th->auto_retry = auto_retry;
831 th->notify_cls = notify_cls;
832 th->attempts_left = MAX_ATTEMPTS;
833 th->th = GNUNET_CONNECTION_notify_transmit_ready (sock->sock,
849 * Cancel a request for notification.
851 * @param th handle from the original request.
854 GNUNET_CLIENT_notify_transmit_ready_cancel (struct
855 GNUNET_CLIENT_TransmitHandle *th)
857 if (th->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
859 GNUNET_break (NULL == th->th);
860 GNUNET_SCHEDULER_cancel (th->sock->sched, th->reconnect_task);
861 th->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
865 GNUNET_break (NULL != th->th);
866 GNUNET_CONNECTION_notify_transmit_ready_cancel (th->th);
874 * Function called to notify a client about the socket
875 * begin ready to queue the message. "buf" will be
876 * NULL and "size" zero if the socket was closed for
877 * writing in the meantime.
879 * @param cls closure of type "struct TransmitGetResponseContext*"
880 * @param size number of bytes available in buf
881 * @param buf where the callee should write the message
882 * @return number of bytes written to buf
885 transmit_for_response (void *cls, size_t size, void *buf)
887 struct TransmitGetResponseContext *tc = cls;
890 tc->sock->tag = NULL;
891 msize = ntohs (tc->hdr->size);
895 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
896 _("Could not submit request, not expecting to receive a response.\n"));
898 tc->rn (tc->rn_cls, NULL);
902 GNUNET_assert (size >= msize);
903 memcpy (buf, tc->hdr, msize);
904 GNUNET_CLIENT_receive (tc->sock,
907 GNUNET_TIME_absolute_get_remaining (tc->timeout));
914 * Convenience API that combines sending a request
915 * to the service and waiting for a response.
916 * If either operation times out, the callback
917 * will be called with a "NULL" response (in which
918 * case the connection should probably be destroyed).
920 * @param sock connection to use
921 * @param hdr message to transmit
922 * @param timeout when to give up (for both transmission
923 * and for waiting for a response)
924 * @param auto_retry if the connection to the service dies, should we
925 * automatically re-connect and retry (within the timeout period)
926 * or should we immediately fail in this case? Pass GNUNET_YES
927 * if the caller does not care about temporary connection errors,
928 * for example because the protocol is stateless
929 * @param rn function to call with the response
930 * @param rn_cls closure for rn
931 * @return GNUNET_OK on success, GNUNET_SYSERR if a request
935 GNUNET_CLIENT_transmit_and_get_response (struct GNUNET_CLIENT_Connection
937 const struct GNUNET_MessageHeader
939 struct GNUNET_TIME_Relative timeout,
941 GNUNET_CLIENT_MessageHandler rn,
944 struct TransmitGetResponseContext *tc;
947 if (NULL != sock->th)
948 return GNUNET_SYSERR;
949 GNUNET_assert (sock->tag == NULL);
950 msize = ntohs (hdr->size);
951 tc = GNUNET_malloc (sizeof (struct TransmitGetResponseContext) + msize);
953 tc->hdr = (const struct GNUNET_MessageHeader *) &tc[1];
954 memcpy (&tc[1], hdr, msize);
955 tc->timeout = GNUNET_TIME_relative_to_absolute (timeout);
958 if (NULL == GNUNET_CLIENT_notify_transmit_ready (sock,
962 &transmit_for_response,
967 return GNUNET_SYSERR;
975 /* end of client.c */