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
47 * Handle for a transmission request.
49 struct GNUNET_CLIENT_TransmitHandle
54 struct GNUNET_CLIENT_Connection *sock;
57 * Function to call to get the data for transmission.
59 GNUNET_CONNECTION_TransmitReadyNotify notify;
67 * Handle to the transmission with the underlying
70 struct GNUNET_CONNECTION_TransmitHandle *th;
73 * If we are re-trying and are delaying to do so,
74 * handle to the scheduled task managing the delay.
76 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
79 * Timeout for the operation overall.
81 struct GNUNET_TIME_Absolute timeout;
84 * Number of bytes requested.
89 * Are we allowed to re-try to connect without telling
90 * the user (of this API) about the connection troubles?
95 * Number of attempts left for transmitting the request. We may
96 * fail the first time (say because the service is not yet up), in
97 * which case (if auto_retry is set) we wait a bit and re-try
98 * (timeout permitting).
100 unsigned int attempts_left;
106 * Context for processing
107 * "GNUNET_CLIENT_transmit_and_get_response" requests.
109 struct TransmitGetResponseContext
114 struct GNUNET_CLIENT_Connection *sock;
117 * Message to transmit; do not free, allocated
118 * right after this struct.
120 const struct GNUNET_MessageHeader *hdr;
125 struct GNUNET_TIME_Absolute timeout;
128 * Function to call when done.
130 GNUNET_CLIENT_MessageHandler rn;
139 * Struct to refer to a GNUnet TCP connection.
140 * This is more than just a socket because if the server
141 * drops the connection, the client automatically tries
142 * to reconnect (and for that needs connection information).
144 struct GNUNET_CLIENT_Connection
148 * the socket handle, NULL if not live
150 struct GNUNET_CONNECTION_Handle *sock;
154 * FIXME: why do we DUP the configuration? Avoid this!
156 struct GNUNET_CONFIGURATION_Handle *cfg;
159 * Name of the service we interact with.
164 * Context of a transmit_and_get_response operation, NULL
165 * if no such operation is pending.
167 struct TransmitGetResponseContext *tag;
170 * Handler for current receiver task.
172 GNUNET_CLIENT_MessageHandler receiver_handler;
175 * Closure for receiver_handler.
177 void *receiver_handler_cls;
180 * Handle for a pending transmission request, NULL if there is
183 struct GNUNET_CLIENT_TransmitHandle *th;
186 * Handler for service test completion (NULL unless in service_test)
188 GNUNET_SCHEDULER_Task test_cb;
191 * Deadline for calling 'test_cb'.
193 struct GNUNET_TIME_Absolute test_deadline;
196 * If we are re-trying and are delaying to do so,
197 * handle to the scheduled task managing the delay.
199 GNUNET_SCHEDULER_TaskIdentifier receive_task;
202 * Closure for test_cb (NULL unless in service_test)
207 * Buffer for received message.
212 * Timeout for receiving a response (absolute time).
214 struct GNUNET_TIME_Absolute receive_timeout;
217 * Current value for our incremental back-off (for
220 struct GNUNET_TIME_Relative back_off;
223 * Number of bytes in received_buf that are valid.
228 * Size of received_buf.
230 unsigned int received_size;
233 * Do we have a complete response in received_buf?
238 * Are we currently busy doing receive-processing?
239 * GNUNET_YES if so, GNUNET_NO if not.
244 * Are we ignoring shutdown signals?
249 * How often have we tried to connect?
251 unsigned int attempts;
257 * Try to connect to the service.
259 * @param service_name name of service to connect to
260 * @param cfg configuration to use
261 * @param attempt counter used to alternate between IP and UNIX domain sockets
262 * @return NULL on error
264 static struct GNUNET_CONNECTION_Handle *
265 do_connect (const char *service_name,
266 const struct GNUNET_CONFIGURATION_Handle *cfg,
267 unsigned int attempt)
269 struct GNUNET_CONNECTION_Handle *sock;
272 unsigned long long port;
276 if (0 == (attempt % 2))
278 /* on even rounds, try UNIX */
280 GNUNET_CONFIGURATION_get_value_string (cfg,
282 "UNIXPATH", &unixpath)) &&
283 (0 < strlen (unixpath))) /* We have a non-NULL unixpath, does that mean it's valid? */
285 sock = GNUNET_CONNECTION_create_from_connect_to_unixpath (cfg, unixpath);
289 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Connected to unixpath `%s'!\n", unixpath);
291 GNUNET_free(unixpath);
295 GNUNET_free_non_null (unixpath);
300 GNUNET_CONFIGURATION_get_value_number (cfg,
306 GNUNET_CONFIGURATION_get_value_string (cfg,
308 "HOSTNAME", &hostname)))
310 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
311 _("Could not determine valid hostname and port for service `%s' from configuration.\n"),
315 if (0 == strlen (hostname))
317 GNUNET_free (hostname);
318 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
319 _("Need a non-empty hostname for service `%s'.\n"),
326 if (0 != (attempt % 2))
330 GNUNET_CONFIGURATION_get_value_string (cfg,
332 "UNIXPATH", &unixpath)) &&
333 (0 < strlen (unixpath)))
335 sock = GNUNET_CONNECTION_create_from_connect_to_unixpath (cfg,
337 GNUNET_free (unixpath);
344 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
345 "Port is 0 for service `%s', UNIXPATH did not work, returning NULL!\n",
348 GNUNET_free (hostname);
352 sock = GNUNET_CONNECTION_create_from_connect (cfg,
355 GNUNET_free (hostname);
361 * Get a connection with a service.
363 * @param service_name name of the service
364 * @param cfg configuration to use
365 * @return NULL on error (service unknown to configuration)
367 struct GNUNET_CLIENT_Connection *
368 GNUNET_CLIENT_connect (const char *service_name,
369 const struct GNUNET_CONFIGURATION_Handle *cfg)
371 struct GNUNET_CLIENT_Connection *ret;
372 struct GNUNET_CONNECTION_Handle *sock;
374 sock = do_connect (service_name,
378 ret = GNUNET_malloc (sizeof (struct GNUNET_CLIENT_Connection));
381 ret->service_name = GNUNET_strdup (service_name);
382 ret->cfg = GNUNET_CONFIGURATION_dup (cfg);
383 ret->back_off = GNUNET_TIME_UNIT_MILLISECONDS;
389 * Configure this connection to ignore shutdown signals.
391 * @param h client handle
392 * @param do_ignore GNUNET_YES to ignore, GNUNET_NO to restore default
395 GNUNET_CLIENT_ignore_shutdown (struct GNUNET_CLIENT_Connection *h,
398 h->ignore_shutdown = do_ignore;
400 GNUNET_CONNECTION_ignore_shutdown (h->sock,
406 * Destroy connection with the service. This will automatically
407 * cancel any pending "receive" request (however, the handler will
408 * *NOT* be called, not even with a NULL message). Any pending
409 * transmission request will also be cancelled UNLESS the callback for
410 * the transmission request has already been called, in which case the
411 * transmission 'finish_pending_write' argument determines whether or
412 * not the write is guaranteed to complete before the socket is fully
413 * destroyed (unless, of course, there is an error with the server in
414 * which case the message may still be lost).
416 * @param finish_pending_write should a transmission already passed to the
417 * handle be completed?
418 * @param sock handle to the service connection
421 GNUNET_CLIENT_disconnect (struct GNUNET_CLIENT_Connection *sock,
422 int finish_pending_write)
424 GNUNET_assert (sock->sock != NULL);
425 if (sock->in_receive == GNUNET_YES)
427 GNUNET_CONNECTION_receive_cancel (sock->sock);
428 sock->in_receive = GNUNET_NO;
430 GNUNET_CONNECTION_destroy (sock->sock, finish_pending_write);
432 if (sock->tag != NULL)
434 GNUNET_free (sock->tag);
437 sock->receiver_handler = NULL;
438 if (sock->th != NULL)
439 GNUNET_CLIENT_notify_transmit_ready_cancel (sock->th);
440 if (sock->receive_task != GNUNET_SCHEDULER_NO_TASK)
442 GNUNET_SCHEDULER_cancel (sock->receive_task);
443 sock->receive_task = GNUNET_SCHEDULER_NO_TASK;
445 GNUNET_array_grow (sock->received_buf, sock->received_size, 0);
446 GNUNET_free (sock->service_name);
447 GNUNET_CONFIGURATION_destroy (sock->cfg);
453 * Check if message is complete
456 check_complete (struct GNUNET_CLIENT_Connection *conn)
458 if ((conn->received_pos >= sizeof (struct GNUNET_MessageHeader)) &&
459 (conn->received_pos >=
460 ntohs (((const struct GNUNET_MessageHeader *) conn->received_buf)->
462 conn->msg_complete = GNUNET_YES;
467 * Callback function for data received from the network. Note that
468 * both "available" and "errCode" would be 0 if the read simply timed out.
471 * @param buf pointer to received data
472 * @param available number of bytes availabe in "buf",
473 * possibly 0 (on errors)
474 * @param addr address of the sender
475 * @param addrlen size of addr
476 * @param errCode value of errno (on errors receiving)
479 receive_helper (void *cls,
482 const struct sockaddr *addr, socklen_t addrlen, int errCode)
484 struct GNUNET_CLIENT_Connection *conn = cls;
485 struct GNUNET_TIME_Relative remaining;
486 GNUNET_CLIENT_MessageHandler receive_handler;
487 void *receive_handler_cls;
489 GNUNET_assert (conn->msg_complete == GNUNET_NO);
490 conn->in_receive = GNUNET_NO;
491 if ((available == 0) || (conn->sock == NULL) || (errCode != 0))
493 /* signal timeout! */
495 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "timeout in receive_helper, available %d, conn->sock %s, errCode %d\n", available, conn->sock == NULL ? "NULL" : "non-NULL", errCode);
497 if (NULL != (receive_handler = conn->receiver_handler))
499 receive_handler_cls = conn->receiver_handler_cls;
500 conn->receiver_handler = NULL;
501 receive_handler (receive_handler_cls, NULL);
506 /* FIXME: optimize for common fast case where buf contains the
507 entire message and we need no copying... */
510 /* slow path: append to array */
511 if (conn->received_size < conn->received_pos + available)
512 GNUNET_array_grow (conn->received_buf,
513 conn->received_size, conn->received_pos + available);
514 memcpy (&conn->received_buf[conn->received_pos], buf, available);
515 conn->received_pos += available;
516 check_complete (conn);
517 /* check for timeout */
518 remaining = GNUNET_TIME_absolute_get_remaining (conn->receive_timeout);
519 if (remaining.rel_value == 0)
521 /* signal timeout! */
522 conn->receiver_handler (conn->receiver_handler_cls, NULL);
525 /* back to receive -- either for more data or to call callback! */
526 GNUNET_CLIENT_receive (conn,
527 conn->receiver_handler,
528 conn->receiver_handler_cls, remaining);
533 * Continuation to call the receive callback.
535 * @param cls our handle to the client connection
536 * @param tc scheduler context
539 receive_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
541 struct GNUNET_CLIENT_Connection *sock = cls;
542 GNUNET_CLIENT_MessageHandler handler = sock->receiver_handler;
543 const struct GNUNET_MessageHeader *cmsg =
544 (const struct GNUNET_MessageHeader *) sock->received_buf;
545 void *handler_cls = sock->receiver_handler_cls;
546 uint16_t msize = ntohs (cmsg->size);
548 struct GNUNET_MessageHeader *msg = (struct GNUNET_MessageHeader *) mbuf;
551 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
552 "Received message of type %u and size %u\n",
556 sock->receive_task = GNUNET_SCHEDULER_NO_TASK;
557 GNUNET_assert (GNUNET_YES == sock->msg_complete);
558 GNUNET_assert (sock->received_pos >= msize);
559 memcpy (msg, cmsg, msize);
560 memmove (sock->received_buf,
561 &sock->received_buf[msize], sock->received_pos - msize);
562 sock->received_pos -= msize;
563 sock->msg_complete = GNUNET_NO;
564 sock->receiver_handler = NULL;
565 check_complete (sock);
567 handler (handler_cls, msg);
572 * Read from the service.
574 * @param sock the service
575 * @param handler function to call with the message
576 * @param handler_cls closure for handler
577 * @param timeout how long to wait until timing out
580 GNUNET_CLIENT_receive (struct GNUNET_CLIENT_Connection *sock,
581 GNUNET_CLIENT_MessageHandler handler,
582 void *handler_cls, struct GNUNET_TIME_Relative timeout)
584 if (sock->sock == NULL)
586 /* already disconnected, fail instantly! */
587 GNUNET_break (0); /* this should not happen in well-written code! */
588 handler (handler_cls, NULL);
591 sock->receiver_handler = handler;
592 sock->receiver_handler_cls = handler_cls;
593 sock->receive_timeout = GNUNET_TIME_relative_to_absolute (timeout);
594 if (GNUNET_YES == sock->msg_complete)
596 sock->receive_task = GNUNET_SCHEDULER_add_after (GNUNET_SCHEDULER_NO_TASK,
597 &receive_task, sock);
601 GNUNET_assert (sock->in_receive == GNUNET_NO);
602 sock->in_receive = GNUNET_YES;
604 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "calling GNUNET_CONNECTION_receive\n");
606 GNUNET_CONNECTION_receive (sock->sock,
607 GNUNET_SERVER_MAX_MESSAGE_SIZE - 1,
608 timeout, &receive_helper, sock);
614 * Report service unavailable.
617 service_test_error (GNUNET_SCHEDULER_Task task, void *task_cls)
619 GNUNET_SCHEDULER_add_continuation (task,
621 GNUNET_SCHEDULER_REASON_TIMEOUT);
626 * Receive confirmation from test, service is up.
629 * @param msg message received, NULL on timeout or fatal error
632 confirm_handler (void *cls, const struct GNUNET_MessageHeader *msg)
634 struct GNUNET_CLIENT_Connection *conn = cls;
635 /* We may want to consider looking at the reply in more
636 detail in the future, for example, is this the
637 correct service? FIXME! */
641 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
642 "Received confirmation that service is running.\n");
644 GNUNET_SCHEDULER_add_continuation (conn->test_cb,
646 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
650 service_test_error (conn->test_cb, conn->test_cb_cls);
652 GNUNET_CLIENT_disconnect (conn, GNUNET_NO);
657 write_test (void *cls, size_t size, void *buf)
659 struct GNUNET_CLIENT_Connection *conn = cls;
660 struct GNUNET_MessageHeader *msg;
662 if (size < sizeof (struct GNUNET_MessageHeader))
665 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
666 _("Failure to transmit TEST request.\n"));
668 service_test_error (conn->test_cb, conn->test_cb_cls);
669 GNUNET_CLIENT_disconnect (conn, GNUNET_NO);
670 return 0; /* client disconnected */
673 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
674 "Transmitting `%s' request.\n", "TEST");
676 msg = (struct GNUNET_MessageHeader *) buf;
677 msg->type = htons (GNUNET_MESSAGE_TYPE_TEST);
678 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
679 GNUNET_CLIENT_receive (conn,
682 GNUNET_TIME_absolute_get_remaining (conn->test_deadline));
683 return sizeof (struct GNUNET_MessageHeader);
688 * Wait until the service is running.
690 * @param service name of the service to wait for
691 * @param cfg configuration to use
692 * @param timeout how long to wait at most in ms
693 * @param task task to run if service is running
694 * (reason will be "PREREQ_DONE" (service running)
695 * or "TIMEOUT" (service not known to be running))
696 * @param task_cls closure for task
699 GNUNET_CLIENT_service_test (const char *service,
700 const struct GNUNET_CONFIGURATION_Handle *cfg,
701 struct GNUNET_TIME_Relative timeout,
702 GNUNET_SCHEDULER_Task task, void *task_cls)
704 struct GNUNET_CLIENT_Connection *conn;
707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
708 "Testing if service `%s' is running.\n", service);
710 conn = GNUNET_CLIENT_connect (service, cfg);
713 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
715 ("Could not connect to service `%s', must not be running.\n"),
717 service_test_error (task, task_cls);
720 conn->test_cb = task;
721 conn->test_cb_cls = task_cls;
722 conn->test_deadline = GNUNET_TIME_relative_to_absolute (timeout);
724 if (NULL == GNUNET_CLIENT_notify_transmit_ready (conn,
725 sizeof (struct GNUNET_MessageHeader),
730 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
731 _("Failure to transmit request to service `%s'\n"),
733 service_test_error (task, task_cls);
734 GNUNET_CLIENT_disconnect (conn, GNUNET_NO);
741 * Connection notifies us about failure or success of
742 * a transmission request. Either pass it on to our
743 * user or, if possible, retry.
745 * @param cls our "struct GNUNET_CLIENT_TransmissionHandle"
746 * @param size number of bytes available for transmission
747 * @param buf where to write them
748 * @return number of bytes written to buf
750 static size_t client_notify (void *cls, size_t size, void *buf);
754 * This task is run if we should re-try connection to the
755 * service after a while.
757 * @param cls our "struct GNUNET_CLIENT_TransmitHandle" of the request
761 client_delayed_retry (void *cls,
762 const struct GNUNET_SCHEDULER_TaskContext *tc)
764 struct GNUNET_CLIENT_TransmitHandle *th = cls;
766 th->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
767 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
771 "Transmission failed due to shutdown.\n");
774 th->notify (th->notify_cls, 0, NULL);
778 th->th = GNUNET_CONNECTION_notify_transmit_ready (th->sock->sock,
780 GNUNET_TIME_absolute_get_remaining
786 th->notify (th->notify_cls, 0, NULL);
794 * Connection notifies us about failure or success of a transmission
795 * request. Either pass it on to our user or, if possible, retry.
797 * @param cls our "struct GNUNET_CLIENT_TransmissionHandle"
798 * @param size number of bytes available for transmission
799 * @param buf where to write them
800 * @return number of bytes written to buf
803 client_notify (void *cls, size_t size, void *buf)
805 struct GNUNET_CLIENT_TransmitHandle *th = cls;
807 struct GNUNET_TIME_Relative delay;
813 delay = GNUNET_TIME_absolute_get_remaining (th->timeout);
814 delay.rel_value /= 2;
815 if ( (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & GNUNET_SCHEDULER_get_reason ())) ||
816 (GNUNET_YES != th->auto_retry) ||
817 (0 == --th->attempts_left) ||
818 (delay.rel_value < 1) )
821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
822 "Transmission failed %u times, giving up.\n",
823 MAX_ATTEMPTS - th->attempts_left);
825 GNUNET_break (0 == th->notify (th->notify_cls, 0, NULL));
830 GNUNET_CONNECTION_destroy (th->sock->sock, GNUNET_NO);
831 th->sock->sock = do_connect (th->sock->service_name,
833 th->sock->attempts++);
834 GNUNET_assert (NULL != th->sock->sock);
835 GNUNET_CONNECTION_ignore_shutdown (th->sock->sock,
836 th->sock->ignore_shutdown);
837 delay = GNUNET_TIME_relative_min (delay, th->sock->back_off);
839 = GNUNET_TIME_relative_min (GNUNET_TIME_relative_multiply (th->sock->back_off, 2),
840 GNUNET_TIME_UNIT_SECONDS);
842 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
843 "Transmission failed %u times, trying again in %llums.\n",
844 MAX_ATTEMPTS - th->attempts_left,
845 (unsigned long long) delay.rel_value);
847 th->reconnect_task = GNUNET_SCHEDULER_add_delayed (delay,
848 &client_delayed_retry,
853 GNUNET_assert (size >= th->size);
854 ret = th->notify (th->notify_cls, size, buf);
861 * Ask the client to call us once the specified number of bytes
862 * are free in the transmission buffer. May call the notify
863 * method immediately if enough space is available.
865 * @param sock connection to the service
866 * @param size number of bytes to send
867 * @param timeout after how long should we give up (and call
868 * notify with buf NULL and size 0)?
869 * @param auto_retry if the connection to the service dies, should we
870 * automatically re-connect and retry (within the timeout period)
871 * or should we immediately fail in this case? Pass GNUNET_YES
872 * if the caller does not care about temporary connection errors,
873 * for example because the protocol is stateless
874 * @param notify function to call
875 * @param notify_cls closure for notify
876 * @return NULL if our buffer will never hold size bytes,
877 * a handle if the notify callback was queued (can be used to cancel)
879 struct GNUNET_CLIENT_TransmitHandle *
880 GNUNET_CLIENT_notify_transmit_ready (struct GNUNET_CLIENT_Connection *sock,
882 struct GNUNET_TIME_Relative timeout,
884 GNUNET_CONNECTION_TransmitReadyNotify
885 notify, void *notify_cls)
887 struct GNUNET_CLIENT_TransmitHandle *th;
889 if (NULL != sock->th)
891 th = GNUNET_malloc (sizeof (struct GNUNET_CLIENT_TransmitHandle));
894 th->timeout = GNUNET_TIME_relative_to_absolute (timeout);
895 th->auto_retry = auto_retry;
897 th->notify_cls = notify_cls;
898 th->attempts_left = MAX_ATTEMPTS;
899 th->th = GNUNET_CONNECTION_notify_transmit_ready (sock->sock,
915 * Cancel a request for notification.
917 * @param th handle from the original request.
920 GNUNET_CLIENT_notify_transmit_ready_cancel (struct
921 GNUNET_CLIENT_TransmitHandle *th)
923 if (th->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
925 GNUNET_break (NULL == th->th);
926 GNUNET_SCHEDULER_cancel (th->reconnect_task);
927 th->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
931 GNUNET_assert (NULL != th->th);
932 GNUNET_CONNECTION_notify_transmit_ready_cancel (th->th);
940 * Function called to notify a client about the socket
941 * begin ready to queue the message. "buf" will be
942 * NULL and "size" zero if the socket was closed for
943 * writing in the meantime.
945 * @param cls closure of type "struct TransmitGetResponseContext*"
946 * @param size number of bytes available in buf
947 * @param buf where the callee should write the message
948 * @return number of bytes written to buf
951 transmit_for_response (void *cls, size_t size, void *buf)
953 struct TransmitGetResponseContext *tc = cls;
956 tc->sock->tag = NULL;
957 msize = ntohs (tc->hdr->size);
961 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
962 _("Could not submit request, not expecting to receive a response.\n"));
964 tc->rn (tc->rn_cls, NULL);
968 GNUNET_assert (size >= msize);
969 memcpy (buf, tc->hdr, msize);
970 GNUNET_CLIENT_receive (tc->sock,
973 GNUNET_TIME_absolute_get_remaining (tc->timeout));
980 * Convenience API that combines sending a request
981 * to the service and waiting for a response.
982 * If either operation times out, the callback
983 * will be called with a "NULL" response (in which
984 * case the connection should probably be destroyed).
986 * @param sock connection to use
987 * @param hdr message to transmit
988 * @param timeout when to give up (for both transmission
989 * and for waiting for a response)
990 * @param auto_retry if the connection to the service dies, should we
991 * automatically re-connect and retry (within the timeout period)
992 * or should we immediately fail in this case? Pass GNUNET_YES
993 * if the caller does not care about temporary connection errors,
994 * for example because the protocol is stateless
995 * @param rn function to call with the response
996 * @param rn_cls closure for rn
997 * @return GNUNET_OK on success, GNUNET_SYSERR if a request
1001 GNUNET_CLIENT_transmit_and_get_response (struct GNUNET_CLIENT_Connection
1003 const struct GNUNET_MessageHeader
1005 struct GNUNET_TIME_Relative timeout,
1007 GNUNET_CLIENT_MessageHandler rn,
1010 struct TransmitGetResponseContext *tc;
1013 if (NULL != sock->th)
1014 return GNUNET_SYSERR;
1015 GNUNET_assert (sock->tag == NULL);
1016 msize = ntohs (hdr->size);
1017 tc = GNUNET_malloc (sizeof (struct TransmitGetResponseContext) + msize);
1019 tc->hdr = (const struct GNUNET_MessageHeader *) &tc[1];
1020 memcpy (&tc[1], hdr, msize);
1021 tc->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1023 tc->rn_cls = rn_cls;
1024 if (NULL == GNUNET_CLIENT_notify_transmit_ready (sock,
1028 &transmit_for_response,
1033 return GNUNET_SYSERR;
1041 /* end of client.c */