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;
59 * Closure for @e 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 @e 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;
254 if ((GNUNET_OK == GNUNET_CONFIGURATION_get_value_filename (cfg, service_name, "UNIXPATH", &unixpath)) &&
255 (0 < strlen (unixpath)))
257 /* We have a non-NULL unixpath, need to validate it */
258 if (strlen (unixpath) >= sizeof (s_un.sun_path))
260 LOG (GNUNET_ERROR_TYPE_WARNING,
261 _("UNIXPATH `%s' too long, maximum length is %llu\n"), unixpath,
262 (unsigned long long) sizeof (s_un.sun_path));
263 unixpath = GNUNET_NETWORK_shorten_unixpath (unixpath);
264 LOG (GNUNET_ERROR_TYPE_INFO,
265 _("Using `%s' instead\n"), unixpath);
266 if (NULL == unixpath)
269 connection = GNUNET_CONNECTION_create_from_connect_to_unixpath (cfg, unixpath);
270 if (NULL != connection)
272 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connected to unixpath `%s'!\n",
274 GNUNET_free (unixpath);
278 GNUNET_free_non_null (unixpath);
285 * Test whether the configuration has proper values for connection
286 * (UNIXPATH || (PORT && HOSTNAME)).
288 * @param service_name name of service to connect to
289 * @param cfg configuration to use
290 * @return #GNUNET_OK if the configuration is valid, #GNUNET_SYSERR if not
293 test_service_configuration (const char *service_name,
294 const struct GNUNET_CONFIGURATION_Handle *cfg)
296 int ret = GNUNET_SYSERR;
297 char *hostname = NULL;
298 unsigned long long port;
300 char *unixpath = NULL;
302 if ((GNUNET_OK == GNUNET_CONFIGURATION_get_value_filename (cfg, service_name, "UNIXPATH", &unixpath)) &&
303 (0 < strlen (unixpath)))
305 GNUNET_free_non_null (unixpath);
309 GNUNET_CONFIGURATION_have_value (cfg, service_name, "PORT")) &&
311 GNUNET_CONFIGURATION_get_value_number (cfg, service_name, "PORT", &port)) &&
312 (port <= 65535) && (0 != port) &&
314 GNUNET_CONFIGURATION_get_value_string (cfg, service_name, "HOSTNAME",
316 (0 != strlen (hostname)) )
318 GNUNET_free_non_null (hostname);
324 * Try to connect to the service.
326 * @param service_name name of service to connect to
327 * @param cfg configuration to use
328 * @param attempt counter used to alternate between IP and UNIX domain sockets
329 * @return NULL on error
331 static struct GNUNET_CONNECTION_Handle *
332 do_connect (const char *service_name,
333 const struct GNUNET_CONFIGURATION_Handle *cfg,
334 unsigned int attempt)
336 struct GNUNET_CONNECTION_Handle *connection;
338 unsigned long long port;
341 if (0 == (attempt % 2))
343 /* on even rounds, try UNIX first */
344 connection = try_unixpath (service_name, cfg);
345 if (NULL != connection)
349 GNUNET_CONFIGURATION_have_value (cfg, service_name, "PORT"))
352 GNUNET_CONFIGURATION_get_value_number (cfg, service_name, "PORT", &port))
355 GNUNET_CONFIGURATION_get_value_string (cfg, service_name, "HOSTNAME",
358 LOG (GNUNET_ERROR_TYPE_WARNING,
360 ("Could not determine valid hostname and port for service `%s' from configuration.\n"),
364 if (0 == strlen (hostname))
366 GNUNET_free (hostname);
367 LOG (GNUNET_ERROR_TYPE_WARNING,
368 _("Need a non-empty hostname for service `%s'.\n"), service_name);
374 /* unspecified means 0 (disabled) */
380 /* if port is 0, try UNIX */
381 connection = try_unixpath (service_name, cfg);
382 if (NULL != connection)
384 GNUNET_free_non_null (hostname);
387 LOG (GNUNET_ERROR_TYPE_DEBUG,
388 "Port is 0 for service `%s', UNIXPATH did not work, returning NULL!\n",
390 GNUNET_free_non_null (hostname);
393 connection = GNUNET_CONNECTION_create_from_connect (cfg, hostname, port);
394 GNUNET_free (hostname);
400 * Get a connection with a service.
402 * @param service_name name of the service
403 * @param cfg configuration to use
404 * @return NULL on error (service unknown to configuration)
406 struct GNUNET_CLIENT_Connection *
407 GNUNET_CLIENT_connect (const char *service_name,
408 const struct GNUNET_CONFIGURATION_Handle *cfg)
410 struct GNUNET_CLIENT_Connection *client;
411 struct GNUNET_CONNECTION_Handle *connection;
414 test_service_configuration (service_name,
417 connection = do_connect (service_name, cfg, 0);
418 client = GNUNET_new (struct GNUNET_CLIENT_Connection);
419 client->first_message = GNUNET_YES;
420 client->attempts = 1;
421 client->connection = connection;
422 client->service_name = GNUNET_strdup (service_name);
424 client->back_off = GNUNET_TIME_UNIT_MILLISECONDS;
430 * Destroy connection with the service. This will automatically
431 * cancel any pending "receive" request (however, the handler will
432 * *NOT* be called, not even with a NULL message). Any pending
433 * transmission request will also be cancelled UNLESS the callback for
434 * the transmission request has already been called, in which case the
435 * transmission 'finish_pending_write' argument determines whether or
436 * not the write is guaranteed to complete before the socket is fully
437 * destroyed (unless, of course, there is an error with the server in
438 * which case the message may still be lost).
440 * @param client handle to the service connection
443 GNUNET_CLIENT_disconnect (struct GNUNET_CLIENT_Connection *client)
445 if (GNUNET_YES == client->in_receive)
447 GNUNET_CONNECTION_receive_cancel (client->connection);
448 client->in_receive = GNUNET_NO;
450 if (NULL != client->th)
452 GNUNET_CLIENT_notify_transmit_ready_cancel (client->th);
455 if (NULL != client->connection)
457 GNUNET_CONNECTION_destroy (client->connection);
458 client->connection = NULL;
460 if (GNUNET_SCHEDULER_NO_TASK != client->receive_task)
462 GNUNET_SCHEDULER_cancel (client->receive_task);
463 client->receive_task = GNUNET_SCHEDULER_NO_TASK;
465 if (NULL != client->tag)
467 GNUNET_free (client->tag);
470 client->receiver_handler = NULL;
471 GNUNET_array_grow (client->received_buf, client->received_size, 0);
472 GNUNET_free (client->service_name);
473 GNUNET_free (client);
478 * Check if message is complete. Sets the "msg_complete" member
479 * in the client struct.
481 * @param client connection with the buffer to check
484 check_complete (struct GNUNET_CLIENT_Connection *client)
486 if ((client->received_pos >= sizeof (struct GNUNET_MessageHeader)) &&
487 (client->received_pos >=
488 ntohs (((const struct GNUNET_MessageHeader *) client->received_buf)->
490 client->msg_complete = GNUNET_YES;
495 * Callback function for data received from the network. Note that
496 * both @a available and @a errCode would be 0 if the read simply timed out.
499 * @param buf pointer to received data
500 * @param available number of bytes availabe in @a buf,
501 * possibly 0 (on errors)
502 * @param addr address of the sender
503 * @param addrlen size of @a addr
504 * @param errCode value of errno (on errors receiving)
507 receive_helper (void *cls, const void *buf, size_t available,
508 const struct sockaddr *addr, socklen_t addrlen, int errCode)
510 struct GNUNET_CLIENT_Connection *client = cls;
511 struct GNUNET_TIME_Relative remaining;
512 GNUNET_CLIENT_MessageHandler receive_handler;
513 void *receive_handler_cls;
515 GNUNET_assert (GNUNET_NO == client->msg_complete);
516 GNUNET_assert (GNUNET_YES == client->in_receive);
517 client->in_receive = GNUNET_NO;
518 if ((0 == available) || (NULL == client->connection) || (0 != errCode))
520 /* signal timeout! */
521 LOG (GNUNET_ERROR_TYPE_DEBUG,
522 "Timeout in receive_helper, available %u, client->connection %s, errCode `%s'\n",
523 (unsigned int) available, NULL == client->connection ? "NULL" : "non-NULL",
525 if (NULL != (receive_handler = client->receiver_handler))
527 receive_handler_cls = client->receiver_handler_cls;
528 client->receiver_handler = NULL;
529 receive_handler (receive_handler_cls, NULL);
533 /* FIXME: optimize for common fast case where buf contains the
534 * entire message and we need no copying... */
536 /* slow path: append to array */
537 if (client->received_size < client->received_pos + available)
538 GNUNET_array_grow (client->received_buf, client->received_size,
539 client->received_pos + available);
540 memcpy (&client->received_buf[client->received_pos], buf, available);
541 client->received_pos += available;
542 check_complete (client);
543 /* check for timeout */
544 remaining = GNUNET_TIME_absolute_get_remaining (client->receive_timeout);
545 if (0 == remaining.rel_value_us)
547 /* signal timeout! */
548 if (NULL != (receive_handler = client->receiver_handler))
550 client->receiver_handler = NULL;
551 receive_handler (client->receiver_handler_cls, NULL);
555 /* back to receive -- either for more data or to call callback! */
556 GNUNET_CLIENT_receive (client, client->receiver_handler,
557 client->receiver_handler_cls, remaining);
562 * Continuation to call the receive callback.
564 * @param cls our handle to the client connection
565 * @param tc scheduler context
568 receive_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
570 struct GNUNET_CLIENT_Connection *client = cls;
571 GNUNET_CLIENT_MessageHandler handler = client->receiver_handler;
572 const struct GNUNET_MessageHeader *cmsg =
573 (const struct GNUNET_MessageHeader *) client->received_buf;
574 void *handler_cls = client->receiver_handler_cls;
575 uint16_t msize = ntohs (cmsg->size);
576 char mbuf[msize] GNUNET_ALIGN;
577 struct GNUNET_MessageHeader *msg = (struct GNUNET_MessageHeader *) mbuf;
579 LOG (GNUNET_ERROR_TYPE_DEBUG,
580 "Received message of type %u and size %u from %s service.\n",
583 client->service_name);
584 client->receive_task = GNUNET_SCHEDULER_NO_TASK;
585 GNUNET_assert (GNUNET_YES == client->msg_complete);
586 GNUNET_assert (client->received_pos >= msize);
587 memcpy (msg, cmsg, msize);
588 memmove (client->received_buf, &client->received_buf[msize],
589 client->received_pos - msize);
590 client->received_pos -= msize;
591 client->msg_complete = GNUNET_NO;
592 client->receiver_handler = NULL;
593 check_complete (client);
595 handler (handler_cls, msg);
600 * Read from the service.
602 * @param client the service
603 * @param handler function to call with the message
604 * @param handler_cls closure for @a handler
605 * @param timeout how long to wait until timing out
608 GNUNET_CLIENT_receive (struct GNUNET_CLIENT_Connection *client,
609 GNUNET_CLIENT_MessageHandler handler,
611 struct GNUNET_TIME_Relative timeout)
613 if (NULL == client->connection)
615 /* already disconnected, fail instantly! */
616 GNUNET_break (0); /* this should not happen in well-written code! */
618 handler (handler_cls, NULL);
621 client->receiver_handler = handler;
622 client->receiver_handler_cls = handler_cls;
623 client->receive_timeout = GNUNET_TIME_relative_to_absolute (timeout);
624 if (GNUNET_YES == client->msg_complete)
626 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == client->receive_task);
627 client->receive_task = GNUNET_SCHEDULER_add_now (&receive_task, client);
631 LOG (GNUNET_ERROR_TYPE_DEBUG, "calling GNUNET_CONNECTION_receive\n");
632 GNUNET_assert (GNUNET_NO == client->in_receive);
633 client->in_receive = GNUNET_YES;
634 GNUNET_CONNECTION_receive (client->connection, GNUNET_SERVER_MAX_MESSAGE_SIZE - 1,
635 timeout, &receive_helper, client);
641 * Handle for a test to check if a service is running.
643 struct GNUNET_CLIENT_TestHandle
646 * Function to call with the result of the test.
648 GNUNET_CLIENT_TestResultCallback cb;
656 * Client connection we are using for the test, if any.
658 struct GNUNET_CLIENT_Connection *client;
661 * Handle for the transmission request, if any.
663 struct GNUNET_CLIENT_TransmitHandle *th;
666 * Deadline for calling @e cb.
668 struct GNUNET_TIME_Absolute test_deadline;
671 * ID of task used for asynchronous operations.
673 GNUNET_SCHEDULER_TaskIdentifier task;
676 * Final result to report back (once known).
683 * Abort testing for service.
685 * @param th test handle
688 GNUNET_CLIENT_service_test_cancel (struct GNUNET_CLIENT_TestHandle *th)
692 GNUNET_CLIENT_notify_transmit_ready_cancel (th->th);
695 if (NULL != th->client)
697 GNUNET_CLIENT_disconnect (th->client);
700 if (GNUNET_SCHEDULER_NO_TASK != th->task)
702 GNUNET_SCHEDULER_cancel (th->task);
703 th->task = GNUNET_SCHEDULER_NO_TASK;
710 * Task that reports back the result by calling the callback
711 * and then cleans up.
713 * @param cls the `struct GNUNET_CLIENT_TestHandle`
714 * @param tc scheduler context
717 report_result (void *cls,
718 const struct GNUNET_SCHEDULER_TaskContext *tc)
720 struct GNUNET_CLIENT_TestHandle *th = cls;
722 th->task = GNUNET_SCHEDULER_NO_TASK;
723 th->cb (th->cb_cls, th->result);
724 GNUNET_CLIENT_service_test_cancel (th);
729 * Report service test result asynchronously back to callback.
731 * @param th test handle with the result and the callback
732 * @param result result to report
735 service_test_report (struct GNUNET_CLIENT_TestHandle *th,
739 th->task = GNUNET_SCHEDULER_add_now (&report_result,
745 * Receive confirmation from test, service is up.
747 * @param cls closure with the `struct GNUNET_CLIENT_TestHandle`
748 * @param msg message received, NULL on timeout or fatal error
751 confirm_handler (void *cls, const struct GNUNET_MessageHeader *msg)
753 struct GNUNET_CLIENT_TestHandle *th = cls;
755 /* We may want to consider looking at the reply in more
756 * detail in the future, for example, is this the
757 * correct service? FIXME! */
760 LOG (GNUNET_ERROR_TYPE_DEBUG,
761 "Received confirmation that service is running.\n");
762 service_test_report (th, GNUNET_YES);
766 service_test_report (th, GNUNET_NO);
772 * Send the 'TEST' message to the service. If successful, prepare to
775 * @param cls the `struct GNUNET_CLIENT_TestHandle` of the test
776 * @param size number of bytes available in @a buf
777 * @param buf where to write the message
778 * @return number of bytes written to @a buf
781 write_test (void *cls, size_t size, void *buf)
783 struct GNUNET_CLIENT_TestHandle *th = cls;
784 struct GNUNET_MessageHeader *msg;
787 if (size < sizeof (struct GNUNET_MessageHeader))
789 LOG (GNUNET_ERROR_TYPE_DEBUG,
790 "Failed to transmit TEST request.\n");
791 service_test_report (th, GNUNET_NO);
792 return 0; /* client disconnected */
794 LOG (GNUNET_ERROR_TYPE_DEBUG,
795 "Transmitting `%s' request.\n",
797 msg = (struct GNUNET_MessageHeader *) buf;
798 msg->type = htons (GNUNET_MESSAGE_TYPE_TEST);
799 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
800 GNUNET_CLIENT_receive (th->client,
801 &confirm_handler, th,
802 GNUNET_TIME_absolute_get_remaining
803 (th->test_deadline));
804 return sizeof (struct GNUNET_MessageHeader);
809 * Test if the service is running. If we are given a UNIXPATH or a
810 * local address, we do this NOT by trying to connect to the service,
811 * but by trying to BIND to the same port. If the BIND fails, we know
812 * the service is running.
814 * @param service name of the service to wait for
815 * @param cfg configuration to use
816 * @param timeout how long to wait at most
817 * @param cb function to call with the result
818 * @param cb_cls closure for @a cb
819 * @return handle to cancel the test
821 struct GNUNET_CLIENT_TestHandle *
822 GNUNET_CLIENT_service_test (const char *service,
823 const struct GNUNET_CONFIGURATION_Handle *cfg,
824 struct GNUNET_TIME_Relative timeout,
825 GNUNET_CLIENT_TestResultCallback cb,
828 struct GNUNET_CLIENT_TestHandle *th;
830 unsigned long long port;
831 struct GNUNET_NETWORK_Handle *sock;
833 th = GNUNET_new (struct GNUNET_CLIENT_TestHandle);
836 th->test_deadline = GNUNET_TIME_relative_to_absolute (timeout);
837 LOG (GNUNET_ERROR_TYPE_DEBUG,
838 "Testing if service `%s' is running.\n",
842 /* probe UNIX support */
843 struct sockaddr_un s_un;
848 GNUNET_CONFIGURATION_get_value_filename (cfg,
852 (0 < strlen (unixpath))) /* We have a non-NULL unixpath, does that mean it's valid? */
854 if (strlen (unixpath) >= sizeof (s_un.sun_path))
856 LOG (GNUNET_ERROR_TYPE_WARNING,
857 _("UNIXPATH `%s' too long, maximum length is %llu\n"),
859 (unsigned long long) sizeof (s_un.sun_path));
860 unixpath = GNUNET_NETWORK_shorten_unixpath (unixpath);
861 LOG (GNUNET_ERROR_TYPE_INFO,
862 _("Using `%s' instead\n"), unixpath);
865 if (NULL != unixpath)
867 if (GNUNET_SYSERR == GNUNET_DISK_directory_create_for_file (unixpath))
868 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
870 sock = GNUNET_NETWORK_socket_create (PF_UNIX, SOCK_STREAM, 0);
873 memset (&s_un, 0, sizeof (s_un));
874 s_un.sun_family = AF_UNIX;
875 strncpy (s_un.sun_path, unixpath, sizeof (s_un.sun_path) - 1);
876 #if HAVE_SOCKADDR_IN_SIN_LEN
877 s_un.sun_len = (u_char) sizeof (struct sockaddr_un);
880 GNUNET_NETWORK_socket_bind (sock, (const struct sockaddr *) &s_un,
881 sizeof (struct sockaddr_un)))
883 /* failed to bind => service must be running */
884 GNUNET_free (unixpath);
885 (void) GNUNET_NETWORK_socket_close (sock);
886 service_test_report (th, GNUNET_YES);
889 (void) GNUNET_NETWORK_socket_close (sock);
893 GNUNET_free_non_null (unixpath);
899 GNUNET_CONFIGURATION_get_value_number (cfg, service, "PORT", &port)) ||
902 GNUNET_CONFIGURATION_get_value_string (cfg, service, "HOSTNAME",
905 /* UNIXPATH failed (if possible) AND IP failed => error */
906 service_test_report (th, GNUNET_SYSERR);
910 if (0 == strcmp ("localhost", hostname)
916 /* can test using 'bind' */
917 struct sockaddr_in s_in;
919 memset (&s_in, 0, sizeof (s_in));
920 #if HAVE_SOCKADDR_IN_SIN_LEN
921 s_in.sin_len = sizeof (struct sockaddr_in);
923 s_in.sin_family = AF_INET;
924 s_in.sin_port = htons (port);
926 sock = GNUNET_NETWORK_socket_create (AF_INET, SOCK_STREAM, 0);
930 GNUNET_NETWORK_socket_bind (sock, (const struct sockaddr *) &s_in,
933 /* failed to bind => service must be running */
934 GNUNET_free (hostname);
935 (void) GNUNET_NETWORK_socket_close (sock);
936 service_test_report (th, GNUNET_YES);
939 (void) GNUNET_NETWORK_socket_close (sock);
943 if (0 == strcmp ("ip6-localhost", hostname)
949 /* can test using 'bind' */
950 struct sockaddr_in6 s_in6;
952 memset (&s_in6, 0, sizeof (s_in6));
953 #if HAVE_SOCKADDR_IN_SIN_LEN
954 s_in6.sin6_len = sizeof (struct sockaddr_in6);
956 s_in6.sin6_family = AF_INET6;
957 s_in6.sin6_port = htons (port);
959 sock = GNUNET_NETWORK_socket_create (AF_INET6, SOCK_STREAM, 0);
963 GNUNET_NETWORK_socket_bind (sock, (const struct sockaddr *) &s_in6,
966 /* failed to bind => service must be running */
967 GNUNET_free (hostname);
968 (void) GNUNET_NETWORK_socket_close (sock);
969 service_test_report (th, GNUNET_YES);
972 (void) GNUNET_NETWORK_socket_close (sock);
976 if (((0 == strcmp ("localhost", hostname)) ||
977 (0 == strcmp ("ip6-localhost", hostname)))
983 /* all binds succeeded => claim service not running right now */
984 GNUNET_free_non_null (hostname);
985 service_test_report (th, GNUNET_NO);
988 GNUNET_free_non_null (hostname);
990 /* non-localhost, try 'connect' method */
991 th->client = GNUNET_CLIENT_connect (service, cfg);
992 if (NULL == th->client)
994 LOG (GNUNET_ERROR_TYPE_INFO,
995 _("Could not connect to service `%s', configuration broken.\n"),
997 service_test_report (th, GNUNET_SYSERR);
1000 th->th = GNUNET_CLIENT_notify_transmit_ready (th->client,
1001 sizeof (struct GNUNET_MessageHeader),
1002 timeout, GNUNET_YES,
1006 LOG (GNUNET_ERROR_TYPE_WARNING,
1007 _("Failure to transmit request to service `%s'\n"), service);
1008 service_test_report (th, GNUNET_SYSERR);
1016 * Connection notifies us about failure or success of
1017 * a transmission request. Either pass it on to our
1018 * user or, if possible, retry.
1020 * @param cls our `struct GNUNET_CLIENT_TransmissionHandle`
1021 * @param size number of bytes available for transmission
1022 * @param buf where to write them
1023 * @return number of bytes written to buf
1026 client_notify (void *cls, size_t size, void *buf);
1030 * This task is run if we should re-try connection to the
1031 * service after a while.
1033 * @param cls our `struct GNUNET_CLIENT_TransmitHandle` of the request
1037 client_delayed_retry (void *cls,
1038 const struct GNUNET_SCHEDULER_TaskContext *tc)
1040 struct GNUNET_CLIENT_TransmitHandle *th = cls;
1041 struct GNUNET_TIME_Relative delay;
1043 th->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1044 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1046 /* give up, was shutdown */
1047 th->client->th = NULL;
1048 th->notify (th->notify_cls, 0, NULL);
1052 th->client->connection =
1053 do_connect (th->client->service_name,
1055 th->client->attempts++);
1056 th->client->first_message = GNUNET_YES;
1057 if (NULL == th->client->connection)
1059 /* could happen if we're out of sockets */
1061 GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining
1062 (th->timeout), th->client->back_off);
1063 th->client->back_off =
1064 GNUNET_TIME_relative_min (GNUNET_TIME_relative_multiply
1065 (th->client->back_off, 2),
1066 GNUNET_TIME_UNIT_SECONDS);
1067 LOG (GNUNET_ERROR_TYPE_DEBUG,
1068 "Transmission failed %u times, trying again in %s.\n",
1069 MAX_ATTEMPTS - th->attempts_left,
1070 GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_YES));
1071 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == th->reconnect_task);
1072 th->reconnect_task =
1073 GNUNET_SCHEDULER_add_delayed (delay, &client_delayed_retry, th);
1077 GNUNET_CONNECTION_notify_transmit_ready (th->client->connection, th->size,
1078 GNUNET_TIME_absolute_get_remaining
1079 (th->timeout), &client_notify,
1084 th->client->th = NULL;
1085 th->notify (th->notify_cls, 0, NULL);
1093 * Connection notifies us about failure or success of a transmission
1094 * request. Either pass it on to our user or, if possible, retry.
1096 * @param cls our `struct GNUNET_CLIENT_TransmissionHandle`
1097 * @param size number of bytes available for transmission
1098 * @param buf where to write them
1099 * @return number of bytes written to @a buf
1102 client_notify (void *cls, size_t size, void *buf)
1104 struct GNUNET_CLIENT_TransmitHandle *th = cls;
1105 struct GNUNET_CLIENT_Connection *client = th->client;
1107 struct GNUNET_TIME_Relative delay;
1109 LOG (GNUNET_ERROR_TYPE_DEBUG,
1110 "client_notify is running\n");
1115 delay = GNUNET_TIME_absolute_get_remaining (th->timeout);
1116 delay.rel_value_us /= 2;
1117 if ((GNUNET_YES != th->auto_retry) || (0 == --th->attempts_left) ||
1118 (delay.rel_value_us < 1)||
1119 (0 != (GNUNET_SCHEDULER_get_reason() & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
1121 LOG (GNUNET_ERROR_TYPE_DEBUG,
1122 "Transmission failed %u times, giving up.\n",
1123 MAX_ATTEMPTS - th->attempts_left);
1124 GNUNET_break (0 == th->notify (th->notify_cls, 0, NULL));
1129 LOG (GNUNET_ERROR_TYPE_DEBUG,
1130 "Failed to connect to `%s', automatically trying again.\n",
1131 client->service_name);
1132 if (GNUNET_YES == client->in_receive)
1134 GNUNET_CONNECTION_receive_cancel (client->connection);
1135 client->in_receive = GNUNET_NO;
1137 GNUNET_CONNECTION_destroy (client->connection);
1138 client->connection = NULL;
1139 delay = GNUNET_TIME_relative_min (delay, client->back_off);
1141 GNUNET_TIME_relative_min (GNUNET_TIME_relative_multiply
1142 (client->back_off, 2),
1143 GNUNET_TIME_UNIT_SECONDS);
1144 LOG (GNUNET_ERROR_TYPE_DEBUG,
1145 "Transmission failed %u times, trying again in %s.\n",
1146 MAX_ATTEMPTS - th->attempts_left,
1147 GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_YES));
1149 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == th->reconnect_task);
1150 th->reconnect_task =
1151 GNUNET_SCHEDULER_add_delayed (delay, &client_delayed_retry, th);
1154 GNUNET_assert (size >= th->size);
1155 ret = th->notify (th->notify_cls, size, buf);
1157 if (sizeof (struct GNUNET_MessageHeader) <= ret)
1159 LOG (GNUNET_ERROR_TYPE_DEBUG,
1160 "Transmitting message of type %u and size %u to %s service.\n",
1161 ntohs (((struct GNUNET_MessageHeader *) buf)->type),
1162 ntohs (((struct GNUNET_MessageHeader *) buf)->size),
1163 client->service_name);
1170 * Ask the client to call us once the specified number of bytes
1171 * are free in the transmission buffer. May call the notify
1172 * method immediately if enough space is available.
1174 * @param client connection to the service
1175 * @param size number of bytes to send
1176 * @param timeout after how long should we give up (and call
1177 * notify with buf NULL and size 0)?
1178 * @param auto_retry if the connection to the service dies, should we
1179 * automatically re-connect and retry (within the timeout period)
1180 * or should we immediately fail in this case? Pass GNUNET_YES
1181 * if the caller does not care about temporary connection errors,
1182 * for example because the protocol is stateless
1183 * @param notify function to call
1184 * @param notify_cls closure for @a notify
1185 * @return NULL if our buffer will never hold size bytes,
1186 * a handle if the notify callback was queued (can be used to cancel)
1188 struct GNUNET_CLIENT_TransmitHandle *
1189 GNUNET_CLIENT_notify_transmit_ready (struct GNUNET_CLIENT_Connection *client,
1191 struct GNUNET_TIME_Relative timeout,
1193 GNUNET_CONNECTION_TransmitReadyNotify notify,
1196 struct GNUNET_CLIENT_TransmitHandle *th;
1198 if (NULL != client->th)
1200 /* If this breaks, you most likley called this function twice without waiting
1201 * for completion or canceling the request */
1205 th = GNUNET_new (struct GNUNET_CLIENT_TransmitHandle);
1206 th->client = client;
1208 th->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1209 /* always auto-retry on first message to service */
1210 th->auto_retry = (GNUNET_YES == client->first_message) ? GNUNET_YES : auto_retry;
1211 client->first_message = GNUNET_NO;
1212 th->notify = notify;
1213 th->notify_cls = notify_cls;
1214 th->attempts_left = MAX_ATTEMPTS;
1216 if (NULL == client->connection)
1218 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == th->reconnect_task);
1219 th->reconnect_task =
1220 GNUNET_SCHEDULER_add_delayed (client->back_off,
1221 &client_delayed_retry,
1228 GNUNET_CONNECTION_notify_transmit_ready (client->connection, size, timeout,
1229 &client_notify, th);
1243 * Cancel a request for notification.
1245 * @param th handle from the original request.
1248 GNUNET_CLIENT_notify_transmit_ready_cancel (struct GNUNET_CLIENT_TransmitHandle *th)
1250 if (GNUNET_SCHEDULER_NO_TASK != th->reconnect_task)
1252 GNUNET_assert (NULL == th->th);
1253 GNUNET_SCHEDULER_cancel (th->reconnect_task);
1254 th->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1258 GNUNET_assert (NULL != th->th);
1259 GNUNET_CONNECTION_notify_transmit_ready_cancel (th->th);
1261 th->client->th = NULL;
1267 * Function called to notify a client about the socket
1268 * begin ready to queue the message. @a buf will be
1269 * NULL and @a size zero if the socket was closed for
1270 * writing in the meantime.
1272 * @param cls closure of type "struct TransmitGetResponseContext*"
1273 * @param size number of bytes available in @a buf
1274 * @param buf where the callee should write the message
1275 * @return number of bytes written to @a buf
1278 transmit_for_response (void *cls,
1282 struct TransmitGetResponseContext *tc = cls;
1285 tc->client->tag = NULL;
1286 msize = ntohs (tc->hdr->size);
1289 LOG (GNUNET_ERROR_TYPE_DEBUG,
1290 _("Could not submit request, not expecting to receive a response.\n"));
1292 tc->rn (tc->rn_cls, NULL);
1296 GNUNET_assert (size >= msize);
1297 memcpy (buf, tc->hdr, msize);
1298 GNUNET_CLIENT_receive (tc->client, tc->rn, tc->rn_cls,
1299 GNUNET_TIME_absolute_get_remaining (tc->timeout));
1306 * Convenience API that combines sending a request
1307 * to the service and waiting for a response.
1308 * If either operation times out, the callback
1309 * will be called with a "NULL" response (in which
1310 * case the connection should probably be destroyed).
1312 * @param client connection to use
1313 * @param hdr message to transmit
1314 * @param timeout when to give up (for both transmission
1315 * and for waiting for a response)
1316 * @param auto_retry if the connection to the service dies, should we
1317 * automatically re-connect and retry (within the timeout period)
1318 * or should we immediately fail in this case? Pass GNUNET_YES
1319 * if the caller does not care about temporary connection errors,
1320 * for example because the protocol is stateless
1321 * @param rn function to call with the response
1322 * @param rn_cls closure for @a rn
1323 * @return #GNUNET_OK on success, #GNUNET_SYSERR if a request
1324 * is already pending
1327 GNUNET_CLIENT_transmit_and_get_response (struct GNUNET_CLIENT_Connection *client,
1328 const struct GNUNET_MessageHeader *hdr,
1329 struct GNUNET_TIME_Relative timeout,
1331 GNUNET_CLIENT_MessageHandler rn,
1334 struct TransmitGetResponseContext *tc;
1337 if (NULL != client->th)
1338 return GNUNET_SYSERR;
1339 GNUNET_assert (NULL == client->tag);
1340 msize = ntohs (hdr->size);
1341 tc = GNUNET_malloc (sizeof (struct TransmitGetResponseContext) + msize);
1342 tc->client = client;
1343 tc->hdr = (const struct GNUNET_MessageHeader *) &tc[1];
1344 memcpy (&tc[1], hdr, msize);
1345 tc->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1347 tc->rn_cls = rn_cls;
1349 GNUNET_CLIENT_notify_transmit_ready (client, msize, timeout, auto_retry,
1350 &transmit_for_response, tc))
1354 return GNUNET_SYSERR;
1361 /* end of client.c */