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
46 #define UNIXPATH_RETRIES 0
50 * Handle for a transmission request.
52 struct GNUNET_CLIENT_TransmitHandle
57 struct GNUNET_CLIENT_Connection *sock;
60 * Function to call to get the data for transmission.
62 GNUNET_CONNECTION_TransmitReadyNotify notify;
70 * Handle to the transmission with the underlying
73 struct GNUNET_CONNECTION_TransmitHandle *th;
76 * If we are re-trying and are delaying to do so,
77 * handle to the scheduled task managing the delay.
79 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
82 * Timeout for the operation overall.
84 struct GNUNET_TIME_Absolute timeout;
87 * Number of bytes requested.
92 * Are we allowed to re-try to connect without telling
93 * the user (of this API) about the connection troubles?
98 * Number of attempts left for transmitting the request. We may
99 * fail the first time (say because the service is not yet up), in
100 * which case (if auto_retry is set) we wait a bit and re-try
101 * (timeout permitting).
103 unsigned int attempts_left;
109 * Context for processing
110 * "GNUNET_CLIENT_transmit_and_get_response" requests.
112 struct TransmitGetResponseContext
117 struct GNUNET_CLIENT_Connection *sock;
120 * Message to transmit; do not free, allocated
121 * right after this struct.
123 const struct GNUNET_MessageHeader *hdr;
128 struct GNUNET_TIME_Absolute timeout;
131 * Function to call when done.
133 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;
157 * FIXME: why do we DUP the configuration? Avoid this!
159 struct GNUNET_CONFIGURATION_Handle *cfg;
162 * Name of the service we interact with.
167 * Context of a transmit_and_get_response operation, NULL
168 * if no such operation is pending.
170 struct TransmitGetResponseContext *tag;
173 * Handler for current receiver task.
175 GNUNET_CLIENT_MessageHandler receiver_handler;
178 * Closure for receiver_handler.
180 void *receiver_handler_cls;
183 * Handle for a pending transmission request, NULL if there is
186 struct GNUNET_CLIENT_TransmitHandle *th;
189 * Handler for service test completion (NULL unless in service_test)
191 GNUNET_SCHEDULER_Task test_cb;
194 * Deadline for calling 'test_cb'.
196 struct GNUNET_TIME_Absolute test_deadline;
199 * If we are re-trying and are delaying to do so,
200 * handle to the scheduled task managing the delay.
202 GNUNET_SCHEDULER_TaskIdentifier receive_task;
205 * Closure for test_cb (NULL unless in service_test)
210 * Buffer for received message.
215 * Timeout for receiving a response (absolute time).
217 struct GNUNET_TIME_Absolute receive_timeout;
220 * Current value for our incremental back-off (for
223 struct GNUNET_TIME_Relative back_off;
226 * Number of bytes in received_buf that are valid.
231 * Size of received_buf.
233 unsigned int received_size;
236 * Do we have a complete response in received_buf?
241 * Are we currently busy doing receive-processing?
242 * GNUNET_YES if so, GNUNET_NO if not.
247 * Are we ignoring shutdown signals?
252 * How often have we tried to connect?
254 unsigned int attempts;
260 * Try to connect to the service.
262 * @param service_name name of service to connect to
263 * @param cfg configuration to use
264 * @param attempt counter used to alternate between IP and UNIX domain sockets
265 * @return NULL on error
267 static struct GNUNET_CONNECTION_Handle *
268 do_connect (const char *service_name,
269 const struct GNUNET_CONFIGURATION_Handle *cfg,
270 unsigned int attempt)
272 struct GNUNET_CONNECTION_Handle *sock;
275 unsigned long long port;
280 if (0 == (attempt % 2))
282 /* on even rounds, try UNIX */
284 GNUNET_CONFIGURATION_get_value_string (cfg,
286 "UNIXPATH", &unixpath)) &&
287 (0 < strlen (unixpath))) /* We have a non-NULL unixpath, does that mean it's valid? */
290 sock = GNUNET_CONNECTION_create_from_connect_to_unixpath (cfg, unixpath);
291 while ((NULL == sock) && (count < UNIXPATH_RETRIES))
294 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Failed to connect to unixpath `%s', retrying!\n", unixpath);
298 sock = GNUNET_CONNECTION_create_from_connect_to_unixpath (cfg, unixpath);
305 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Connected to unixpath `%s'!\n", unixpath);
307 GNUNET_free(unixpath);
311 GNUNET_free_non_null (unixpath);
316 GNUNET_CONFIGURATION_get_value_number (cfg,
322 GNUNET_CONFIGURATION_get_value_string (cfg,
324 "HOSTNAME", &hostname)))
326 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
328 ("Could not determine valid hostname and port for service `%s' from configuration.\n"),
332 if (0 == strlen (hostname))
334 GNUNET_free (hostname);
335 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
336 _("Need a non-empty hostname for service `%s'.\n"),
343 if (0 != (attempt % 2))
347 GNUNET_CONFIGURATION_get_value_string (cfg,
349 "UNIXPATH", &unixpath)) &&
350 (0 < strlen (unixpath)))
352 sock = GNUNET_CONNECTION_create_from_connect_to_unixpath (cfg,
354 GNUNET_free (unixpath);
362 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Port is 0 for service `%s', unixpath didn't work, returning NULL(!)!\n", service_name);
365 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Creating from connect!\n");
366 sock = GNUNET_CONNECTION_create_from_connect (cfg,
369 GNUNET_free (hostname);
375 * Get a connection with a service.
377 * @param service_name name of the service
378 * @param cfg configuration to use
379 * @return NULL on error (service unknown to configuration)
381 struct GNUNET_CLIENT_Connection *
382 GNUNET_CLIENT_connect (const char *service_name,
383 const struct GNUNET_CONFIGURATION_Handle *cfg)
385 struct GNUNET_CLIENT_Connection *ret;
386 struct GNUNET_CONNECTION_Handle *sock;
388 sock = do_connect (service_name,
392 ret = GNUNET_malloc (sizeof (struct GNUNET_CLIENT_Connection));
395 ret->service_name = GNUNET_strdup (service_name);
396 ret->cfg = GNUNET_CONFIGURATION_dup (cfg);
397 ret->back_off = GNUNET_TIME_UNIT_MILLISECONDS;
403 * Configure this connection to ignore shutdown signals.
405 * @param h client handle
406 * @param do_ignore GNUNET_YES to ignore, GNUNET_NO to restore default
409 GNUNET_CLIENT_ignore_shutdown (struct GNUNET_CLIENT_Connection *h,
412 h->ignore_shutdown = do_ignore;
414 GNUNET_CONNECTION_ignore_shutdown (h->sock,
420 * Destroy connection with the service. This will automatically
421 * cancel any pending "receive" request (however, the handler will
422 * *NOT* be called, not even with a NULL message). Any pending
423 * transmission request will also be cancelled UNLESS the callback for
424 * the transmission request has already been called, in which case the
425 * transmission 'finish_pending_write' argument determines whether or
426 * not the write is guaranteed to complete before the socket is fully
427 * destroyed (unless, of course, there is an error with the server in
428 * which case the message may still be lost).
430 * @param finish_pending_write should a transmission already passed to the
431 * handle be completed?
432 * @param sock handle to the service connection
435 GNUNET_CLIENT_disconnect (struct GNUNET_CLIENT_Connection *sock,
436 int finish_pending_write)
438 GNUNET_assert (sock->sock != NULL);
439 if (sock->in_receive == GNUNET_YES)
441 GNUNET_CONNECTION_receive_cancel (sock->sock);
442 sock->in_receive = GNUNET_NO;
444 GNUNET_CONNECTION_destroy (sock->sock, finish_pending_write);
446 if (sock->tag != NULL)
448 GNUNET_free (sock->tag);
451 sock->receiver_handler = NULL;
452 if (sock->th != NULL)
453 GNUNET_CLIENT_notify_transmit_ready_cancel (sock->th);
454 if (sock->receive_task != GNUNET_SCHEDULER_NO_TASK)
456 GNUNET_SCHEDULER_cancel (sock->receive_task);
457 sock->receive_task = GNUNET_SCHEDULER_NO_TASK;
459 GNUNET_array_grow (sock->received_buf, sock->received_size, 0);
460 GNUNET_free (sock->service_name);
461 GNUNET_CONFIGURATION_destroy (sock->cfg);
467 * Check if message is complete
470 check_complete (struct GNUNET_CLIENT_Connection *conn)
472 if ((conn->received_pos >= sizeof (struct GNUNET_MessageHeader)) &&
473 (conn->received_pos >=
474 ntohs (((const struct GNUNET_MessageHeader *) conn->received_buf)->
476 conn->msg_complete = GNUNET_YES;
481 * Callback function for data received from the network. Note that
482 * both "available" and "errCode" would be 0 if the read simply timed out.
485 * @param buf pointer to received data
486 * @param available number of bytes availabe in "buf",
487 * possibly 0 (on errors)
488 * @param addr address of the sender
489 * @param addrlen size of addr
490 * @param errCode value of errno (on errors receiving)
493 receive_helper (void *cls,
496 const struct sockaddr *addr, socklen_t addrlen, int errCode)
498 struct GNUNET_CLIENT_Connection *conn = cls;
499 struct GNUNET_TIME_Relative remaining;
500 GNUNET_CLIENT_MessageHandler receive_handler;
501 void *receive_handler_cls;
503 GNUNET_assert (conn->msg_complete == GNUNET_NO);
504 conn->in_receive = GNUNET_NO;
505 if ((available == 0) || (conn->sock == NULL) || (errCode != 0))
507 /* signal timeout! */
509 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);
511 if (NULL != (receive_handler = conn->receiver_handler))
513 receive_handler_cls = conn->receiver_handler_cls;
514 conn->receiver_handler = NULL;
515 receive_handler (receive_handler_cls, NULL);
520 /* FIXME: optimize for common fast case where buf contains the
521 entire message and we need no copying... */
524 /* slow path: append to array */
525 if (conn->received_size < conn->received_pos + available)
526 GNUNET_array_grow (conn->received_buf,
527 conn->received_size, conn->received_pos + available);
528 memcpy (&conn->received_buf[conn->received_pos], buf, available);
529 conn->received_pos += available;
530 check_complete (conn);
531 /* check for timeout */
532 remaining = GNUNET_TIME_absolute_get_remaining (conn->receive_timeout);
533 if (remaining.rel_value == 0)
535 /* signal timeout! */
536 conn->receiver_handler (conn->receiver_handler_cls, NULL);
539 /* back to receive -- either for more data or to call callback! */
540 GNUNET_CLIENT_receive (conn,
541 conn->receiver_handler,
542 conn->receiver_handler_cls, remaining);
547 * Continuation to call the receive callback.
549 * @param cls our handle to the client connection
550 * @param tc scheduler context
553 receive_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
555 struct GNUNET_CLIENT_Connection *sock = cls;
556 GNUNET_CLIENT_MessageHandler handler = sock->receiver_handler;
557 const struct GNUNET_MessageHeader *cmsg =
558 (const struct GNUNET_MessageHeader *) sock->received_buf;
559 void *handler_cls = sock->receiver_handler_cls;
560 uint16_t msize = ntohs (cmsg->size);
562 struct GNUNET_MessageHeader *msg = (struct GNUNET_MessageHeader *) mbuf;
565 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
566 "Received message of type %u and size %u\n",
570 sock->receive_task = GNUNET_SCHEDULER_NO_TASK;
571 GNUNET_assert (GNUNET_YES == sock->msg_complete);
572 GNUNET_assert (sock->received_pos >= msize);
573 memcpy (msg, cmsg, msize);
574 memmove (sock->received_buf,
575 &sock->received_buf[msize], sock->received_pos - msize);
576 sock->received_pos -= msize;
577 sock->msg_complete = GNUNET_NO;
578 sock->receiver_handler = NULL;
579 check_complete (sock);
581 handler (handler_cls, msg);
586 * Read from the service.
588 * @param sock the service
589 * @param handler function to call with the message
590 * @param handler_cls closure for handler
591 * @param timeout how long to wait until timing out
594 GNUNET_CLIENT_receive (struct GNUNET_CLIENT_Connection *sock,
595 GNUNET_CLIENT_MessageHandler handler,
596 void *handler_cls, struct GNUNET_TIME_Relative timeout)
598 if (sock->sock == NULL)
600 /* already disconnected, fail instantly! */
601 GNUNET_break (0); /* this should not happen in well-written code! */
602 handler (handler_cls, NULL);
605 sock->receiver_handler = handler;
606 sock->receiver_handler_cls = handler_cls;
607 sock->receive_timeout = GNUNET_TIME_relative_to_absolute (timeout);
608 if (GNUNET_YES == sock->msg_complete)
610 sock->receive_task = GNUNET_SCHEDULER_add_after (GNUNET_SCHEDULER_NO_TASK,
611 &receive_task, sock);
615 GNUNET_assert (sock->in_receive == GNUNET_NO);
616 sock->in_receive = GNUNET_YES;
618 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "calling GNUNET_CONNECTION_receive\n");
620 GNUNET_CONNECTION_receive (sock->sock,
621 GNUNET_SERVER_MAX_MESSAGE_SIZE - 1,
622 timeout, &receive_helper, sock);
628 * Report service unavailable.
631 service_test_error (GNUNET_SCHEDULER_Task task, void *task_cls)
633 GNUNET_SCHEDULER_add_continuation (task,
635 GNUNET_SCHEDULER_REASON_TIMEOUT);
640 * Receive confirmation from test, service is up.
643 * @param msg message received, NULL on timeout or fatal error
646 confirm_handler (void *cls, const struct GNUNET_MessageHeader *msg)
648 struct GNUNET_CLIENT_Connection *conn = cls;
649 /* We may want to consider looking at the reply in more
650 detail in the future, for example, is this the
651 correct service? FIXME! */
655 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
656 "Received confirmation that service is running.\n");
658 GNUNET_SCHEDULER_add_continuation (conn->test_cb,
660 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
664 service_test_error (conn->test_cb, conn->test_cb_cls);
666 GNUNET_CLIENT_disconnect (conn, GNUNET_NO);
671 write_test (void *cls, size_t size, void *buf)
673 struct GNUNET_CLIENT_Connection *conn = cls;
674 struct GNUNET_MessageHeader *msg;
676 if (size < sizeof (struct GNUNET_MessageHeader))
679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
680 _("Failure to transmit TEST request.\n"));
682 service_test_error (conn->test_cb, conn->test_cb_cls);
683 GNUNET_CLIENT_disconnect (conn, GNUNET_NO);
684 return 0; /* client disconnected */
687 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
688 "Transmitting `%s' request.\n", "TEST");
690 msg = (struct GNUNET_MessageHeader *) buf;
691 msg->type = htons (GNUNET_MESSAGE_TYPE_TEST);
692 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
693 GNUNET_CLIENT_receive (conn,
696 GNUNET_TIME_absolute_get_remaining (conn->test_deadline));
697 return sizeof (struct GNUNET_MessageHeader);
702 * Wait until the service is running.
704 * @param service name of the service to wait for
705 * @param cfg configuration to use
706 * @param timeout how long to wait at most in ms
707 * @param task task to run if service is running
708 * (reason will be "PREREQ_DONE" (service running)
709 * or "TIMEOUT" (service not known to be running))
710 * @param task_cls closure for task
713 GNUNET_CLIENT_service_test (const char *service,
714 const struct GNUNET_CONFIGURATION_Handle *cfg,
715 struct GNUNET_TIME_Relative timeout,
716 GNUNET_SCHEDULER_Task task, void *task_cls)
718 struct GNUNET_CLIENT_Connection *conn;
721 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
722 "Testing if service `%s' is running.\n", service);
724 conn = GNUNET_CLIENT_connect (service, cfg);
727 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
729 ("Could not connect to service `%s', must not be running.\n"),
731 service_test_error (task, task_cls);
734 conn->test_cb = task;
735 conn->test_cb_cls = task_cls;
736 conn->test_deadline = GNUNET_TIME_relative_to_absolute (timeout);
738 if (NULL == GNUNET_CLIENT_notify_transmit_ready (conn,
739 sizeof (struct GNUNET_MessageHeader),
744 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
745 _("Failure to transmit request to service `%s'\n"),
747 service_test_error (task, task_cls);
748 GNUNET_CLIENT_disconnect (conn, GNUNET_NO);
755 * Connection notifies us about failure or success of
756 * a transmission request. Either pass it on to our
757 * user or, if possible, retry.
759 * @param cls our "struct GNUNET_CLIENT_TransmissionHandle"
760 * @param size number of bytes available for transmission
761 * @param buf where to write them
762 * @return number of bytes written to buf
764 static size_t client_notify (void *cls, size_t size, void *buf);
768 * This task is run if we should re-try connection to the
769 * service after a while.
771 * @param cls our "struct GNUNET_CLIENT_TransmitHandle" of the request
775 client_delayed_retry (void *cls,
776 const struct GNUNET_SCHEDULER_TaskContext *tc)
778 struct GNUNET_CLIENT_TransmitHandle *th = cls;
780 th->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
781 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
784 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
785 "Transmission failed due to shutdown.\n");
788 th->notify (th->notify_cls, 0, NULL);
792 th->th = GNUNET_CONNECTION_notify_transmit_ready (th->sock->sock,
794 GNUNET_TIME_absolute_get_remaining
800 th->notify (th->notify_cls, 0, NULL);
808 * Connection notifies us about failure or success of a transmission
809 * request. Either pass it on to our user or, if possible, retry.
811 * @param cls our "struct GNUNET_CLIENT_TransmissionHandle"
812 * @param size number of bytes available for transmission
813 * @param buf where to write them
814 * @return number of bytes written to buf
817 client_notify (void *cls, size_t size, void *buf)
819 struct GNUNET_CLIENT_TransmitHandle *th = cls;
821 struct GNUNET_TIME_Relative delay;
827 delay = GNUNET_TIME_absolute_get_remaining (th->timeout);
828 delay.rel_value /= 2;
829 if ( (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & GNUNET_SCHEDULER_get_reason ())) ||
830 (GNUNET_YES != th->auto_retry) ||
831 (0 == --th->attempts_left) ||
832 (delay.rel_value < 1) )
835 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
836 "Transmission failed %u times, giving up.\n",
837 MAX_ATTEMPTS - th->attempts_left);
839 GNUNET_break (0 == th->notify (th->notify_cls, 0, NULL));
844 GNUNET_CONNECTION_destroy (th->sock->sock, GNUNET_NO);
845 th->sock->sock = do_connect (th->sock->service_name,
847 th->sock->attempts++);
848 GNUNET_assert (NULL != th->sock->sock);
849 GNUNET_CONNECTION_ignore_shutdown (th->sock->sock,
850 th->sock->ignore_shutdown);
851 delay = GNUNET_TIME_relative_min (delay, th->sock->back_off);
853 = GNUNET_TIME_relative_min (GNUNET_TIME_relative_multiply (th->sock->back_off, 2),
854 GNUNET_TIME_UNIT_SECONDS);
856 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
857 "Transmission failed %u times, trying again in %llums.\n",
858 MAX_ATTEMPTS - th->attempts_left,
859 (unsigned long long) delay.rel_value);
861 th->reconnect_task = GNUNET_SCHEDULER_add_delayed (delay,
862 &client_delayed_retry,
867 GNUNET_assert (size >= th->size);
868 ret = th->notify (th->notify_cls, size, buf);
875 * Ask the client to call us once the specified number of bytes
876 * are free in the transmission buffer. May call the notify
877 * method immediately if enough space is available.
879 * @param sock connection to the service
880 * @param size number of bytes to send
881 * @param timeout after how long should we give up (and call
882 * notify with buf NULL and size 0)?
883 * @param auto_retry if the connection to the service dies, should we
884 * automatically re-connect and retry (within the timeout period)
885 * or should we immediately fail in this case? Pass GNUNET_YES
886 * if the caller does not care about temporary connection errors,
887 * for example because the protocol is stateless
888 * @param notify function to call
889 * @param notify_cls closure for notify
890 * @return NULL if our buffer will never hold size bytes,
891 * a handle if the notify callback was queued (can be used to cancel)
893 struct GNUNET_CLIENT_TransmitHandle *
894 GNUNET_CLIENT_notify_transmit_ready (struct GNUNET_CLIENT_Connection *sock,
896 struct GNUNET_TIME_Relative timeout,
898 GNUNET_CONNECTION_TransmitReadyNotify
899 notify, void *notify_cls)
901 struct GNUNET_CLIENT_TransmitHandle *th;
903 if (NULL != sock->th)
905 th = GNUNET_malloc (sizeof (struct GNUNET_CLIENT_TransmitHandle));
908 th->timeout = GNUNET_TIME_relative_to_absolute (timeout);
909 th->auto_retry = auto_retry;
911 th->notify_cls = notify_cls;
912 th->attempts_left = MAX_ATTEMPTS;
913 th->th = GNUNET_CONNECTION_notify_transmit_ready (sock->sock,
929 * Cancel a request for notification.
931 * @param th handle from the original request.
934 GNUNET_CLIENT_notify_transmit_ready_cancel (struct
935 GNUNET_CLIENT_TransmitHandle *th)
937 if (th->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
939 GNUNET_break (NULL == th->th);
940 GNUNET_SCHEDULER_cancel (th->reconnect_task);
941 th->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
945 GNUNET_assert (NULL != th->th);
946 GNUNET_CONNECTION_notify_transmit_ready_cancel (th->th);
954 * Function called to notify a client about the socket
955 * begin ready to queue the message. "buf" will be
956 * NULL and "size" zero if the socket was closed for
957 * writing in the meantime.
959 * @param cls closure of type "struct TransmitGetResponseContext*"
960 * @param size number of bytes available in buf
961 * @param buf where the callee should write the message
962 * @return number of bytes written to buf
965 transmit_for_response (void *cls, size_t size, void *buf)
967 struct TransmitGetResponseContext *tc = cls;
970 tc->sock->tag = NULL;
971 msize = ntohs (tc->hdr->size);
975 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
976 _("Could not submit request, not expecting to receive a response.\n"));
978 tc->rn (tc->rn_cls, NULL);
982 GNUNET_assert (size >= msize);
983 memcpy (buf, tc->hdr, msize);
984 GNUNET_CLIENT_receive (tc->sock,
987 GNUNET_TIME_absolute_get_remaining (tc->timeout));
994 * Convenience API that combines sending a request
995 * to the service and waiting for a response.
996 * If either operation times out, the callback
997 * will be called with a "NULL" response (in which
998 * case the connection should probably be destroyed).
1000 * @param sock connection to use
1001 * @param hdr message to transmit
1002 * @param timeout when to give up (for both transmission
1003 * and for waiting for a response)
1004 * @param auto_retry if the connection to the service dies, should we
1005 * automatically re-connect and retry (within the timeout period)
1006 * or should we immediately fail in this case? Pass GNUNET_YES
1007 * if the caller does not care about temporary connection errors,
1008 * for example because the protocol is stateless
1009 * @param rn function to call with the response
1010 * @param rn_cls closure for rn
1011 * @return GNUNET_OK on success, GNUNET_SYSERR if a request
1012 * is already pending
1015 GNUNET_CLIENT_transmit_and_get_response (struct GNUNET_CLIENT_Connection
1017 const struct GNUNET_MessageHeader
1019 struct GNUNET_TIME_Relative timeout,
1021 GNUNET_CLIENT_MessageHandler rn,
1024 struct TransmitGetResponseContext *tc;
1027 if (NULL != sock->th)
1028 return GNUNET_SYSERR;
1029 GNUNET_assert (sock->tag == NULL);
1030 msize = ntohs (hdr->size);
1031 tc = GNUNET_malloc (sizeof (struct TransmitGetResponseContext) + msize);
1033 tc->hdr = (const struct GNUNET_MessageHeader *) &tc[1];
1034 memcpy (&tc[1], hdr, msize);
1035 tc->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1037 tc->rn_cls = rn_cls;
1038 if (NULL == GNUNET_CLIENT_notify_transmit_ready (sock,
1042 &transmit_for_response,
1047 return GNUNET_SYSERR;
1055 /* end of client.c */