2 This file is part of GNUnet.
3 (C) 2001-2013 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 3, 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.
30 #include "gnunet_protocols.h"
31 #include "gnunet_util_lib.h"
35 * How often do we re-try tranmsitting requests before giving up?
36 * Note that if we succeeded transmitting a request but failed to read
37 * a response, we do NOT re-try.
39 #define MAX_ATTEMPTS 50
41 #define LOG(kind,...) GNUNET_log_from (kind, "util",__VA_ARGS__)
44 * Handle for a transmission request.
46 struct GNUNET_CLIENT_TransmitHandle
51 struct GNUNET_CLIENT_Connection *client;
54 * Function to call to get the data for transmission.
56 GNUNET_CONNECTION_TransmitReadyNotify notify;
64 * Handle to the transmission with the underlying
67 struct GNUNET_CONNECTION_TransmitHandle *th;
70 * If we are re-trying and are delaying to do so,
71 * handle to the scheduled task managing the delay.
73 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
76 * Timeout for the operation overall.
78 struct GNUNET_TIME_Absolute timeout;
81 * Number of bytes requested.
86 * Are we allowed to re-try to connect without telling
87 * the user (of this API) about the connection troubles?
92 * Number of attempts left for transmitting the request. We may
93 * fail the first time (say because the service is not yet up), in
94 * which case (if auto_retry is set) we wait a bit and re-try
95 * (timeout permitting).
97 unsigned int attempts_left;
103 * Context for processing
104 * "GNUNET_CLIENT_transmit_and_get_response" requests.
106 struct TransmitGetResponseContext
111 struct GNUNET_CLIENT_Connection *client;
114 * Message to transmit; do not free, allocated
115 * right after this struct.
117 const struct GNUNET_MessageHeader *hdr;
122 struct GNUNET_TIME_Absolute timeout;
125 * Function to call when done.
127 GNUNET_CLIENT_MessageHandler rn;
136 * Struct to refer to a GNUnet TCP connection.
137 * This is more than just a socket because if the server
138 * drops the connection, the client automatically tries
139 * to reconnect (and for that needs connection information).
141 struct GNUNET_CLIENT_Connection
145 * The connection handle, NULL if not live
147 struct GNUNET_CONNECTION_Handle *connection;
152 const struct GNUNET_CONFIGURATION_Handle *cfg;
155 * Name of the service we interact with.
160 * Context of a transmit_and_get_response operation, NULL
161 * if no such operation is pending.
163 struct TransmitGetResponseContext *tag;
166 * Handler for current receiver task.
168 GNUNET_CLIENT_MessageHandler receiver_handler;
171 * Closure for receiver_handler.
173 void *receiver_handler_cls;
176 * Handle for a pending transmission request, NULL if there is
179 struct GNUNET_CLIENT_TransmitHandle *th;
182 * If we are re-trying and are delaying to do so,
183 * handle to the scheduled task managing the delay.
185 GNUNET_SCHEDULER_TaskIdentifier receive_task;
188 * Buffer for received message.
193 * Timeout for receiving a response (absolute time).
195 struct GNUNET_TIME_Absolute receive_timeout;
198 * Current value for our incremental back-off (for
201 struct GNUNET_TIME_Relative back_off;
204 * Number of bytes in received_buf that are valid.
209 * Size of received_buf.
211 unsigned int received_size;
214 * Do we have a complete response in received_buf?
219 * Are we currently busy doing receive-processing?
220 * GNUNET_YES if so, GNUNET_NO if not.
225 * Is this the first message we are sending to the service?
230 * How often have we tried to connect?
232 unsigned int attempts;
238 * Try connecting to the server using UNIX domain sockets.
240 * @param service_name name of service to connect to
241 * @param cfg configuration to use
242 * @return NULL on error, connection to UNIX otherwise
244 static struct GNUNET_CONNECTION_Handle *
245 try_unixpath (const char *service_name,
246 const struct GNUNET_CONFIGURATION_Handle *cfg)
249 struct GNUNET_CONNECTION_Handle *connection;
251 struct sockaddr_un s_un;
255 GNUNET_CONFIGURATION_get_value_filename (cfg, service_name,
256 "UNIXPATH", &unixpath)) &&
257 (0 < strlen (unixpath)))
259 /* We have a non-NULL unixpath, need to validate it */
260 if (strlen (unixpath) >= sizeof (s_un.sun_path))
262 LOG (GNUNET_ERROR_TYPE_WARNING,
263 _("UNIXPATH `%s' too long, maximum length is %llu\n"), unixpath,
264 (unsigned long long) sizeof (s_un.sun_path));
265 unixpath = GNUNET_NETWORK_shorten_unixpath (unixpath);
266 LOG (GNUNET_ERROR_TYPE_INFO,
267 _("Using `%s' instead\n"), unixpath);
268 if (NULL == unixpath)
271 connection = GNUNET_CONNECTION_create_from_connect_to_unixpath (cfg, unixpath);
272 if (NULL != connection)
274 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connected to unixpath `%s'!\n",
276 GNUNET_free (unixpath);
280 GNUNET_free_non_null (unixpath);
287 * Test whether the configuration has proper values for connection
288 * (UNIXPATH || (PORT && HOSTNAME)).
290 * @param service_name name of service to connect to
291 * @param cfg configuration to use
292 * @return GNUNET_OK if the configuration is valid, GNUNET_SYSERR if not
295 test_service_configuration (const char *service_name,
296 const struct GNUNET_CONFIGURATION_Handle *cfg)
298 int ret = GNUNET_SYSERR;
299 char *hostname = NULL;
300 unsigned long long port;
302 char *unixpath = NULL;
305 GNUNET_CONFIGURATION_get_value_filename (cfg, service_name, "UNIXPATH", &unixpath)) &&
306 (0 < strlen (unixpath)))
308 GNUNET_free_non_null (unixpath);
312 GNUNET_CONFIGURATION_have_value (cfg, service_name, "PORT")) &&
314 GNUNET_CONFIGURATION_get_value_number (cfg, service_name, "PORT", &port)) &&
315 (port <= 65535) && (0 != port) &&
317 GNUNET_CONFIGURATION_get_value_string (cfg, service_name, "HOSTNAME",
319 (0 != strlen (hostname)) )
321 GNUNET_free_non_null (hostname);
327 * Try to connect to the service.
329 * @param service_name name of service to connect to
330 * @param cfg configuration to use
331 * @param attempt counter used to alternate between IP and UNIX domain sockets
332 * @return NULL on error
334 static struct GNUNET_CONNECTION_Handle *
335 do_connect (const char *service_name,
336 const struct GNUNET_CONFIGURATION_Handle *cfg, unsigned int attempt)
338 struct GNUNET_CONNECTION_Handle *connection;
340 unsigned long long port;
343 if (0 == (attempt % 2))
345 /* on even rounds, try UNIX first */
346 connection = try_unixpath (service_name, cfg);
347 if (NULL != connection)
351 GNUNET_CONFIGURATION_have_value (cfg, service_name, "PORT"))
354 GNUNET_CONFIGURATION_get_value_number (cfg, service_name, "PORT", &port))
357 GNUNET_CONFIGURATION_get_value_string (cfg, service_name, "HOSTNAME",
360 LOG (GNUNET_ERROR_TYPE_WARNING,
362 ("Could not determine valid hostname and port for service `%s' from configuration.\n"),
366 if (0 == strlen (hostname))
368 GNUNET_free (hostname);
369 LOG (GNUNET_ERROR_TYPE_WARNING,
370 _("Need a non-empty hostname for service `%s'.\n"), service_name);
376 /* unspecified means 0 (disabled) */
382 /* if port is 0, try UNIX */
383 connection = try_unixpath (service_name, cfg);
384 if (NULL != connection)
386 LOG (GNUNET_ERROR_TYPE_DEBUG,
387 "Port is 0 for service `%s', UNIXPATH did not work, returning NULL!\n",
389 GNUNET_free_non_null (hostname);
392 connection = GNUNET_CONNECTION_create_from_connect (cfg, hostname, port);
393 GNUNET_free (hostname);
399 * Get a connection with a service.
401 * @param service_name name of the service
402 * @param cfg configuration to use
403 * @return NULL on error (service unknown to configuration)
405 struct GNUNET_CLIENT_Connection *
406 GNUNET_CLIENT_connect (const char *service_name,
407 const struct GNUNET_CONFIGURATION_Handle *cfg)
409 struct GNUNET_CLIENT_Connection *client;
410 struct GNUNET_CONNECTION_Handle *connection;
413 test_service_configuration (service_name,
416 connection = do_connect (service_name, cfg, 0);
417 client = GNUNET_new (struct GNUNET_CLIENT_Connection);
418 client->first_message = GNUNET_YES;
419 client->attempts = 1;
420 client->connection = connection;
421 client->service_name = GNUNET_strdup (service_name);
423 client->back_off = GNUNET_TIME_UNIT_MILLISECONDS;
429 * Destroy connection with the service. This will automatically
430 * cancel any pending "receive" request (however, the handler will
431 * *NOT* be called, not even with a NULL message). Any pending
432 * transmission request will also be cancelled UNLESS the callback for
433 * the transmission request has already been called, in which case the
434 * transmission 'finish_pending_write' argument determines whether or
435 * not the write is guaranteed to complete before the socket is fully
436 * destroyed (unless, of course, there is an error with the server in
437 * which case the message may still be lost).
439 * @param client handle to the service connection
442 GNUNET_CLIENT_disconnect (struct GNUNET_CLIENT_Connection *client)
444 if (GNUNET_YES == client->in_receive)
446 GNUNET_CONNECTION_receive_cancel (client->connection);
447 client->in_receive = GNUNET_NO;
449 if (NULL != client->th)
451 GNUNET_CLIENT_notify_transmit_ready_cancel (client->th);
454 if (NULL != client->connection)
456 GNUNET_CONNECTION_destroy (client->connection);
457 client->connection = NULL;
459 if (GNUNET_SCHEDULER_NO_TASK != client->receive_task)
461 GNUNET_SCHEDULER_cancel (client->receive_task);
462 client->receive_task = GNUNET_SCHEDULER_NO_TASK;
464 if (NULL != client->tag)
466 GNUNET_free (client->tag);
469 client->receiver_handler = NULL;
470 GNUNET_array_grow (client->received_buf, client->received_size, 0);
471 GNUNET_free (client->service_name);
472 GNUNET_free (client);
477 * Check if message is complete. Sets the "msg_complete" member
478 * in the client struct.
480 * @param client connection with the buffer to check
483 check_complete (struct GNUNET_CLIENT_Connection *client)
485 if ((client->received_pos >= sizeof (struct GNUNET_MessageHeader)) &&
486 (client->received_pos >=
487 ntohs (((const struct GNUNET_MessageHeader *) client->received_buf)->
489 client->msg_complete = GNUNET_YES;
494 * Callback function for data received from the network. Note that
495 * both "available" and "errCode" would be 0 if the read simply timed out.
498 * @param buf pointer to received data
499 * @param available number of bytes availabe in "buf",
500 * possibly 0 (on errors)
501 * @param addr address of the sender
502 * @param addrlen size of addr
503 * @param errCode value of errno (on errors receiving)
506 receive_helper (void *cls, const void *buf, size_t available,
507 const struct sockaddr *addr, socklen_t addrlen, int errCode)
509 struct GNUNET_CLIENT_Connection *client = cls;
510 struct GNUNET_TIME_Relative remaining;
511 GNUNET_CLIENT_MessageHandler receive_handler;
512 void *receive_handler_cls;
514 GNUNET_assert (GNUNET_NO == client->msg_complete);
515 GNUNET_assert (GNUNET_YES == client->in_receive);
516 client->in_receive = GNUNET_NO;
517 if ((0 == available) || (NULL == client->connection) || (0 != errCode))
519 /* signal timeout! */
520 LOG (GNUNET_ERROR_TYPE_DEBUG,
521 "Timeout in receive_helper, available %u, client->connection %s, errCode `%s'\n",
522 (unsigned int) available, NULL == client->connection ? "NULL" : "non-NULL",
524 if (NULL != (receive_handler = client->receiver_handler))
526 receive_handler_cls = client->receiver_handler_cls;
527 client->receiver_handler = NULL;
528 receive_handler (receive_handler_cls, NULL);
532 /* FIXME: optimize for common fast case where buf contains the
533 * entire message and we need no copying... */
535 /* slow path: append to array */
536 if (client->received_size < client->received_pos + available)
537 GNUNET_array_grow (client->received_buf, client->received_size,
538 client->received_pos + available);
539 memcpy (&client->received_buf[client->received_pos], buf, available);
540 client->received_pos += available;
541 check_complete (client);
542 /* check for timeout */
543 remaining = GNUNET_TIME_absolute_get_remaining (client->receive_timeout);
544 if (0 == remaining.rel_value_us)
546 /* signal timeout! */
547 if (NULL != client->receiver_handler)
548 client->receiver_handler (client->receiver_handler_cls, NULL);
551 /* back to receive -- either for more data or to call callback! */
552 GNUNET_CLIENT_receive (client, client->receiver_handler,
553 client->receiver_handler_cls, remaining);
558 * Continuation to call the receive callback.
560 * @param cls our handle to the client connection
561 * @param tc scheduler context
564 receive_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
566 struct GNUNET_CLIENT_Connection *client = cls;
567 GNUNET_CLIENT_MessageHandler handler = client->receiver_handler;
568 const struct GNUNET_MessageHeader *cmsg =
569 (const struct GNUNET_MessageHeader *) client->received_buf;
570 void *handler_cls = client->receiver_handler_cls;
571 uint16_t msize = ntohs (cmsg->size);
573 struct GNUNET_MessageHeader *msg = (struct GNUNET_MessageHeader *) mbuf;
575 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received message of type %u and size %u\n",
576 ntohs (cmsg->type), msize);
577 client->receive_task = GNUNET_SCHEDULER_NO_TASK;
578 GNUNET_assert (GNUNET_YES == client->msg_complete);
579 GNUNET_assert (client->received_pos >= msize);
580 memcpy (msg, cmsg, msize);
581 memmove (client->received_buf, &client->received_buf[msize],
582 client->received_pos - msize);
583 client->received_pos -= msize;
584 client->msg_complete = GNUNET_NO;
585 client->receiver_handler = NULL;
586 check_complete (client);
588 handler (handler_cls, msg);
593 * Read from the service.
595 * @param client the service
596 * @param handler function to call with the message
597 * @param handler_cls closure for handler
598 * @param timeout how long to wait until timing out
601 GNUNET_CLIENT_receive (struct GNUNET_CLIENT_Connection *client,
602 GNUNET_CLIENT_MessageHandler handler, void *handler_cls,
603 struct GNUNET_TIME_Relative timeout)
605 if (NULL == client->connection)
607 /* already disconnected, fail instantly! */
608 GNUNET_break (0); /* this should not happen in well-written code! */
610 handler (handler_cls, NULL);
613 client->receiver_handler = handler;
614 client->receiver_handler_cls = handler_cls;
615 client->receive_timeout = GNUNET_TIME_relative_to_absolute (timeout);
616 if (GNUNET_YES == client->msg_complete)
618 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == client->receive_task);
619 client->receive_task = GNUNET_SCHEDULER_add_now (&receive_task, client);
623 LOG (GNUNET_ERROR_TYPE_DEBUG, "calling GNUNET_CONNECTION_receive\n");
624 GNUNET_assert (GNUNET_NO == client->in_receive);
625 client->in_receive = GNUNET_YES;
626 GNUNET_CONNECTION_receive (client->connection, GNUNET_SERVER_MAX_MESSAGE_SIZE - 1,
627 timeout, &receive_helper, client);
633 * Handle for a test to check if a service is running.
635 struct GNUNET_CLIENT_TestHandle
638 * Function to call with the result of the test.
640 GNUNET_CLIENT_TestResultCallback cb;
648 * Client connection we are using for the test, if any.
650 struct GNUNET_CLIENT_Connection *client;
653 * Handle for the transmission request, if any.
655 struct GNUNET_CLIENT_TransmitHandle *th;
658 * Deadline for calling 'cb'.
660 struct GNUNET_TIME_Absolute test_deadline;
663 * ID of task used for asynchronous operations.
665 GNUNET_SCHEDULER_TaskIdentifier task;
668 * Final result to report back (once known).
675 * Abort testing for service.
677 * @param th test handle
680 GNUNET_CLIENT_service_test_cancel (struct GNUNET_CLIENT_TestHandle *th)
684 GNUNET_CLIENT_notify_transmit_ready_cancel (th->th);
687 if (NULL != th->client)
689 GNUNET_CLIENT_disconnect (th->client);
692 if (GNUNET_SCHEDULER_NO_TASK != th->task)
694 GNUNET_SCHEDULER_cancel (th->task);
695 th->task = GNUNET_SCHEDULER_NO_TASK;
702 * Task that reports back the result by calling the callback
703 * and then cleans up.
705 * @param cls the 'struct GNUNET_CLIENT_TestHandle'
706 * @param tc scheduler context
709 report_result (void *cls,
710 const struct GNUNET_SCHEDULER_TaskContext *tc)
712 struct GNUNET_CLIENT_TestHandle *th = cls;
714 th->task = GNUNET_SCHEDULER_NO_TASK;
715 th->cb (th->cb_cls, th->result);
716 GNUNET_CLIENT_service_test_cancel (th);
721 * Report service test result asynchronously back to callback.
723 * @param th test handle with the result and the callback
724 * @param result result to report
727 service_test_report (struct GNUNET_CLIENT_TestHandle *th,
731 th->task = GNUNET_SCHEDULER_add_now (&report_result,
737 * Receive confirmation from test, service is up.
739 * @param cls closure with the 'struct GNUNET_CLIENT_TestHandle'
740 * @param msg message received, NULL on timeout or fatal error
743 confirm_handler (void *cls, const struct GNUNET_MessageHeader *msg)
745 struct GNUNET_CLIENT_TestHandle *th = cls;
747 /* We may want to consider looking at the reply in more
748 * detail in the future, for example, is this the
749 * correct service? FIXME! */
752 LOG (GNUNET_ERROR_TYPE_DEBUG,
753 "Received confirmation that service is running.\n");
754 service_test_report (th, GNUNET_YES);
758 service_test_report (th, GNUNET_NO);
764 * Send the 'TEST' message to the service. If successful, prepare to
767 * @param cls the 'struct GNUNET_CLIENT_TestHandle' of the test
768 * @param size number of bytes available in buf
769 * @param buf where to write the message
770 * @return number of bytes written to buf
773 write_test (void *cls, size_t size, void *buf)
775 struct GNUNET_CLIENT_TestHandle *th = cls;
776 struct GNUNET_MessageHeader *msg;
779 if (size < sizeof (struct GNUNET_MessageHeader))
781 LOG (GNUNET_ERROR_TYPE_DEBUG,
782 "Failed to transmit TEST request.\n");
783 service_test_report (th, GNUNET_NO);
784 return 0; /* client disconnected */
786 LOG (GNUNET_ERROR_TYPE_DEBUG,
787 "Transmitting `%s' request.\n",
789 msg = (struct GNUNET_MessageHeader *) buf;
790 msg->type = htons (GNUNET_MESSAGE_TYPE_TEST);
791 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
792 GNUNET_CLIENT_receive (th->client,
793 &confirm_handler, th,
794 GNUNET_TIME_absolute_get_remaining
795 (th->test_deadline));
796 return sizeof (struct GNUNET_MessageHeader);
801 * Test if the service is running. If we are given a UNIXPATH or a
802 * local address, we do this NOT by trying to connect to the service,
803 * but by trying to BIND to the same port. If the BIND fails, we know
804 * the service is running.
806 * @param service name of the service to wait for
807 * @param cfg configuration to use
808 * @param timeout how long to wait at most
809 * @param cb function to call with the result
810 * @param cb_cls closure for 'cb'
811 * @return handle to cancel the test
813 struct GNUNET_CLIENT_TestHandle *
814 GNUNET_CLIENT_service_test (const char *service,
815 const struct GNUNET_CONFIGURATION_Handle *cfg,
816 struct GNUNET_TIME_Relative timeout,
817 GNUNET_CLIENT_TestResultCallback cb, void *cb_cls)
819 struct GNUNET_CLIENT_TestHandle *th;
821 unsigned long long port;
822 struct GNUNET_NETWORK_Handle *sock;
824 th = GNUNET_new (struct GNUNET_CLIENT_TestHandle);
827 th->test_deadline = GNUNET_TIME_relative_to_absolute (timeout);
828 LOG (GNUNET_ERROR_TYPE_DEBUG,
829 "Testing if service `%s' is running.\n",
833 /* probe UNIX support */
834 struct sockaddr_un s_un;
839 GNUNET_CONFIGURATION_get_value_filename (cfg, service, "UNIXPATH", &unixpath)) &&
840 (0 < strlen (unixpath))) /* We have a non-NULL unixpath, does that mean it's valid? */
842 if (strlen (unixpath) >= sizeof (s_un.sun_path))
844 LOG (GNUNET_ERROR_TYPE_WARNING,
845 _("UNIXPATH `%s' too long, maximum length is %llu\n"), unixpath,
846 (unsigned long long) sizeof (s_un.sun_path));
847 unixpath = GNUNET_NETWORK_shorten_unixpath (unixpath);
848 LOG (GNUNET_ERROR_TYPE_INFO,
849 _("Using `%s' instead\n"), unixpath);
852 if (NULL != unixpath)
854 sock = GNUNET_NETWORK_socket_create (PF_UNIX, SOCK_STREAM, 0);
857 memset (&s_un, 0, sizeof (s_un));
858 s_un.sun_family = AF_UNIX;
859 strncpy (s_un.sun_path, unixpath, sizeof (s_un.sun_path) - 1);
860 #if HAVE_SOCKADDR_IN_SIN_LEN
861 s_un.sun_len = (u_char) sizeof (struct sockaddr_un);
864 GNUNET_NETWORK_socket_bind (sock, (const struct sockaddr *) &s_un,
865 sizeof (struct sockaddr_un)))
867 /* failed to bind => service must be running */
868 GNUNET_free (unixpath);
869 (void) GNUNET_NETWORK_socket_close (sock);
870 service_test_report (th, GNUNET_YES);
873 (void) GNUNET_NETWORK_socket_close (sock);
877 GNUNET_free_non_null (unixpath);
883 GNUNET_CONFIGURATION_get_value_number (cfg, service, "PORT", &port)) ||
886 GNUNET_CONFIGURATION_get_value_string (cfg, service, "HOSTNAME",
889 /* UNIXPATH failed (if possible) AND IP failed => error */
890 service_test_report (th, GNUNET_SYSERR);
894 if (0 == strcmp ("localhost", hostname)
900 /* can test using 'bind' */
901 struct sockaddr_in s_in;
903 memset (&s_in, 0, sizeof (s_in));
904 #if HAVE_SOCKADDR_IN_SIN_LEN
905 s_in.sin_len = sizeof (struct sockaddr_in);
907 s_in.sin_family = AF_INET;
908 s_in.sin_port = htons (port);
910 sock = GNUNET_NETWORK_socket_create (AF_INET, SOCK_STREAM, 0);
914 GNUNET_NETWORK_socket_bind (sock, (const struct sockaddr *) &s_in,
917 /* failed to bind => service must be running */
918 GNUNET_free (hostname);
919 (void) GNUNET_NETWORK_socket_close (sock);
920 service_test_report (th, GNUNET_YES);
923 (void) GNUNET_NETWORK_socket_close (sock);
927 if (0 == strcmp ("ip6-localhost", hostname)
933 /* can test using 'bind' */
934 struct sockaddr_in6 s_in6;
936 memset (&s_in6, 0, sizeof (s_in6));
937 #if HAVE_SOCKADDR_IN_SIN_LEN
938 s_in6.sin6_len = sizeof (struct sockaddr_in6);
940 s_in6.sin6_family = AF_INET6;
941 s_in6.sin6_port = htons (port);
943 sock = GNUNET_NETWORK_socket_create (AF_INET6, SOCK_STREAM, 0);
947 GNUNET_NETWORK_socket_bind (sock, (const struct sockaddr *) &s_in6,
950 /* failed to bind => service must be running */
951 GNUNET_free (hostname);
952 (void) GNUNET_NETWORK_socket_close (sock);
953 service_test_report (th, GNUNET_YES);
956 (void) GNUNET_NETWORK_socket_close (sock);
960 if (((0 == strcmp ("localhost", hostname)) ||
961 (0 == strcmp ("ip6-localhost", hostname)))
967 /* all binds succeeded => claim service not running right now */
968 GNUNET_free_non_null (hostname);
969 service_test_report (th, GNUNET_NO);
972 GNUNET_free_non_null (hostname);
974 /* non-localhost, try 'connect' method */
975 th->client = GNUNET_CLIENT_connect (service, cfg);
976 if (NULL == th->client)
978 LOG (GNUNET_ERROR_TYPE_INFO,
979 _("Could not connect to service `%s', configuration broken.\n"),
981 service_test_report (th, GNUNET_SYSERR);
984 th->th = GNUNET_CLIENT_notify_transmit_ready (th->client,
985 sizeof (struct GNUNET_MessageHeader),
990 LOG (GNUNET_ERROR_TYPE_WARNING,
991 _("Failure to transmit request to service `%s'\n"), service);
992 service_test_report (th, GNUNET_SYSERR);
1000 * Connection notifies us about failure or success of
1001 * a transmission request. Either pass it on to our
1002 * user or, if possible, retry.
1004 * @param cls our "struct GNUNET_CLIENT_TransmissionHandle"
1005 * @param size number of bytes available for transmission
1006 * @param buf where to write them
1007 * @return number of bytes written to buf
1010 client_notify (void *cls, size_t size, void *buf);
1014 * This task is run if we should re-try connection to the
1015 * service after a while.
1017 * @param cls our "struct GNUNET_CLIENT_TransmitHandle" of the request
1021 client_delayed_retry (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1023 struct GNUNET_CLIENT_TransmitHandle *th = cls;
1024 struct GNUNET_TIME_Relative delay;
1026 th->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1027 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1029 /* give up, was shutdown */
1030 th->client->th = NULL;
1031 th->notify (th->notify_cls, 0, NULL);
1035 th->client->connection =
1036 do_connect (th->client->service_name, th->client->cfg, th->client->attempts++);
1037 th->client->first_message = GNUNET_YES;
1038 if (NULL == th->client->connection)
1040 /* could happen if we're out of sockets */
1042 GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining
1043 (th->timeout), th->client->back_off);
1044 th->client->back_off =
1045 GNUNET_TIME_relative_min (GNUNET_TIME_relative_multiply
1046 (th->client->back_off, 2),
1047 GNUNET_TIME_UNIT_SECONDS);
1048 LOG (GNUNET_ERROR_TYPE_DEBUG,
1049 "Transmission failed %u times, trying again in %s.\n",
1050 MAX_ATTEMPTS - th->attempts_left,
1051 GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_YES));
1052 th->reconnect_task =
1053 GNUNET_SCHEDULER_add_delayed (delay, &client_delayed_retry, th);
1057 GNUNET_CONNECTION_notify_transmit_ready (th->client->connection, th->size,
1058 GNUNET_TIME_absolute_get_remaining
1059 (th->timeout), &client_notify,
1064 th->client->th = NULL;
1065 th->notify (th->notify_cls, 0, NULL);
1073 * Connection notifies us about failure or success of a transmission
1074 * request. Either pass it on to our user or, if possible, retry.
1076 * @param cls our "struct GNUNET_CLIENT_TransmissionHandle"
1077 * @param size number of bytes available for transmission
1078 * @param buf where to write them
1079 * @return number of bytes written to buf
1082 client_notify (void *cls, size_t size, void *buf)
1084 struct GNUNET_CLIENT_TransmitHandle *th = cls;
1085 struct GNUNET_CLIENT_Connection *client = th->client;
1087 struct GNUNET_TIME_Relative delay;
1089 LOG (GNUNET_ERROR_TYPE_DEBUG, "client_notify is running\n");
1094 delay = GNUNET_TIME_absolute_get_remaining (th->timeout);
1095 delay.rel_value_us /= 2;
1096 if ((GNUNET_YES != th->auto_retry) || (0 == --th->attempts_left) ||
1097 (delay.rel_value_us < 1)||
1098 (0 != (GNUNET_SCHEDULER_get_reason() & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
1100 LOG (GNUNET_ERROR_TYPE_DEBUG,
1101 "Transmission failed %u times, giving up.\n",
1102 MAX_ATTEMPTS - th->attempts_left);
1103 GNUNET_break (0 == th->notify (th->notify_cls, 0, NULL));
1108 LOG (GNUNET_ERROR_TYPE_DEBUG,
1109 "Failed to connect to `%s', automatically trying again.\n",
1110 client->service_name);
1111 if (GNUNET_YES == client->in_receive)
1113 GNUNET_CONNECTION_receive_cancel (client->connection);
1114 client->in_receive = GNUNET_NO;
1116 GNUNET_CONNECTION_destroy (client->connection);
1117 client->connection = NULL;
1118 delay = GNUNET_TIME_relative_min (delay, client->back_off);
1120 GNUNET_TIME_relative_min (GNUNET_TIME_relative_multiply
1121 (client->back_off, 2),
1122 GNUNET_TIME_UNIT_SECONDS);
1123 LOG (GNUNET_ERROR_TYPE_DEBUG,
1124 "Transmission failed %u times, trying again in %s.\n",
1125 MAX_ATTEMPTS - th->attempts_left,
1126 GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_YES));
1128 th->reconnect_task =
1129 GNUNET_SCHEDULER_add_delayed (delay, &client_delayed_retry, th);
1132 GNUNET_assert (size >= th->size);
1133 ret = th->notify (th->notify_cls, size, buf);
1140 * Ask the client to call us once the specified number of bytes
1141 * are free in the transmission buffer. May call the notify
1142 * method immediately if enough space is available.
1144 * @param client connection to the service
1145 * @param size number of bytes to send
1146 * @param timeout after how long should we give up (and call
1147 * notify with buf NULL and size 0)?
1148 * @param auto_retry if the connection to the service dies, should we
1149 * automatically re-connect and retry (within the timeout period)
1150 * or should we immediately fail in this case? Pass GNUNET_YES
1151 * if the caller does not care about temporary connection errors,
1152 * for example because the protocol is stateless
1153 * @param notify function to call
1154 * @param notify_cls closure for notify
1155 * @return NULL if our buffer will never hold size bytes,
1156 * a handle if the notify callback was queued (can be used to cancel)
1158 struct GNUNET_CLIENT_TransmitHandle *
1159 GNUNET_CLIENT_notify_transmit_ready (struct GNUNET_CLIENT_Connection *client,
1161 struct GNUNET_TIME_Relative timeout,
1163 GNUNET_CONNECTION_TransmitReadyNotify
1164 notify, void *notify_cls)
1166 struct GNUNET_CLIENT_TransmitHandle *th;
1168 if (NULL != client->th)
1170 /* If this breaks, you most likley called this function twice without waiting
1171 * for completion or canceling the request */
1175 th = GNUNET_malloc (sizeof (struct GNUNET_CLIENT_TransmitHandle));
1176 th->client = client;
1178 th->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1179 /* always auto-retry on first message to service */
1180 th->auto_retry = (GNUNET_YES == client->first_message) ? GNUNET_YES : auto_retry;
1181 client->first_message = GNUNET_NO;
1182 th->notify = notify;
1183 th->notify_cls = notify_cls;
1184 th->attempts_left = MAX_ATTEMPTS;
1186 if (NULL == client->connection)
1188 th->reconnect_task =
1189 GNUNET_SCHEDULER_add_delayed (client->back_off, &client_delayed_retry,
1196 GNUNET_CONNECTION_notify_transmit_ready (client->connection, size, timeout,
1197 &client_notify, th);
1211 * Cancel a request for notification.
1213 * @param th handle from the original request.
1216 GNUNET_CLIENT_notify_transmit_ready_cancel (struct GNUNET_CLIENT_TransmitHandle
1219 if (GNUNET_SCHEDULER_NO_TASK != th->reconnect_task)
1221 GNUNET_assert (NULL == th->th);
1222 GNUNET_SCHEDULER_cancel (th->reconnect_task);
1223 th->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1227 GNUNET_assert (NULL != th->th);
1228 GNUNET_CONNECTION_notify_transmit_ready_cancel (th->th);
1230 th->client->th = NULL;
1236 * Function called to notify a client about the socket
1237 * begin ready to queue the message. "buf" will be
1238 * NULL and "size" zero if the socket was closed for
1239 * writing in the meantime.
1241 * @param cls closure of type "struct TransmitGetResponseContext*"
1242 * @param size number of bytes available in buf
1243 * @param buf where the callee should write the message
1244 * @return number of bytes written to buf
1247 transmit_for_response (void *cls, size_t size, void *buf)
1249 struct TransmitGetResponseContext *tc = cls;
1252 tc->client->tag = NULL;
1253 msize = ntohs (tc->hdr->size);
1256 LOG (GNUNET_ERROR_TYPE_DEBUG,
1257 _("Could not submit request, not expecting to receive a response.\n"));
1259 tc->rn (tc->rn_cls, NULL);
1263 GNUNET_assert (size >= msize);
1264 memcpy (buf, tc->hdr, msize);
1265 GNUNET_CLIENT_receive (tc->client, tc->rn, tc->rn_cls,
1266 GNUNET_TIME_absolute_get_remaining (tc->timeout));
1273 * Convenience API that combines sending a request
1274 * to the service and waiting for a response.
1275 * If either operation times out, the callback
1276 * will be called with a "NULL" response (in which
1277 * case the connection should probably be destroyed).
1279 * @param client connection to use
1280 * @param hdr message to transmit
1281 * @param timeout when to give up (for both transmission
1282 * and for waiting for a response)
1283 * @param auto_retry if the connection to the service dies, should we
1284 * automatically re-connect and retry (within the timeout period)
1285 * or should we immediately fail in this case? Pass GNUNET_YES
1286 * if the caller does not care about temporary connection errors,
1287 * for example because the protocol is stateless
1288 * @param rn function to call with the response
1289 * @param rn_cls closure for rn
1290 * @return GNUNET_OK on success, GNUNET_SYSERR if a request
1291 * is already pending
1294 GNUNET_CLIENT_transmit_and_get_response (struct GNUNET_CLIENT_Connection *client,
1295 const struct GNUNET_MessageHeader *hdr,
1296 struct GNUNET_TIME_Relative timeout,
1298 GNUNET_CLIENT_MessageHandler rn,
1301 struct TransmitGetResponseContext *tc;
1304 if (NULL != client->th)
1305 return GNUNET_SYSERR;
1306 GNUNET_assert (NULL == client->tag);
1307 msize = ntohs (hdr->size);
1308 tc = GNUNET_malloc (sizeof (struct TransmitGetResponseContext) + msize);
1309 tc->client = client;
1310 tc->hdr = (const struct GNUNET_MessageHeader *) &tc[1];
1311 memcpy (&tc[1], hdr, msize);
1312 tc->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1314 tc->rn_cls = rn_cls;
1316 GNUNET_CLIENT_notify_transmit_ready (client, msize, timeout, auto_retry,
1317 &transmit_for_response, tc))
1321 return GNUNET_SYSERR;
1328 /* end of client.c */