2 This file is part of GNUnet.
3 Copyright (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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, 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 struct GNUNET_SCHEDULER_Task *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 struct GNUNET_SCHEDULER_Task * 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. #GNUNET_SYSERR
221 * if the connection has failed (but we may not have
222 * closed the handle itself yet).
227 * Is this the first message we are sending to the service?
232 * How often have we tried to connect?
234 unsigned int attempts;
240 * Try connecting to the server using UNIX domain sockets.
242 * @param service_name name of service to connect to
243 * @param cfg configuration to use
244 * @return NULL on error, connection to UNIX otherwise
246 static struct GNUNET_CONNECTION_Handle *
247 try_unixpath (const char *service_name,
248 const struct GNUNET_CONFIGURATION_Handle *cfg)
251 struct GNUNET_CONNECTION_Handle *connection;
253 struct sockaddr_un s_un;
256 if ((GNUNET_OK == GNUNET_CONFIGURATION_get_value_filename (cfg, service_name, "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;
304 if ((GNUNET_OK == GNUNET_CONFIGURATION_get_value_filename (cfg, service_name, "UNIXPATH", &unixpath)) &&
305 (0 < strlen (unixpath)))
307 GNUNET_free_non_null (unixpath);
311 GNUNET_CONFIGURATION_have_value (cfg, service_name, "PORT")) &&
313 GNUNET_CONFIGURATION_get_value_number (cfg, service_name, "PORT", &port)) &&
314 (port <= 65535) && (0 != port) &&
316 GNUNET_CONFIGURATION_get_value_string (cfg, service_name, "HOSTNAME",
318 (0 != strlen (hostname)) )
320 GNUNET_free_non_null (hostname);
326 * Try to connect to the service.
328 * @param service_name name of service to connect to
329 * @param cfg configuration to use
330 * @param attempt counter used to alternate between IP and UNIX domain sockets
331 * @return NULL on error
333 static struct GNUNET_CONNECTION_Handle *
334 do_connect (const char *service_name,
335 const struct GNUNET_CONFIGURATION_Handle *cfg,
336 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 GNUNET_free_non_null (hostname);
389 LOG (GNUNET_ERROR_TYPE_DEBUG,
390 "Port is 0 for service `%s', UNIXPATH did not work, returning NULL!\n",
392 GNUNET_free_non_null (hostname);
395 connection = GNUNET_CONNECTION_create_from_connect (cfg, hostname, port);
396 GNUNET_free (hostname);
402 * Get a connection with a service.
404 * @param service_name name of the service
405 * @param cfg configuration to use
406 * @return NULL on error (service unknown to configuration)
408 struct GNUNET_CLIENT_Connection *
409 GNUNET_CLIENT_connect (const char *service_name,
410 const struct GNUNET_CONFIGURATION_Handle *cfg)
412 struct GNUNET_CLIENT_Connection *client;
413 struct GNUNET_CONNECTION_Handle *connection;
416 test_service_configuration (service_name,
419 connection = do_connect (service_name, cfg, 0);
420 client = GNUNET_new (struct GNUNET_CLIENT_Connection);
421 client->first_message = GNUNET_YES;
422 client->attempts = 1;
423 client->connection = connection;
424 client->service_name = GNUNET_strdup (service_name);
426 client->back_off = GNUNET_TIME_UNIT_MILLISECONDS;
432 * Destroy connection with the service. This will automatically
433 * cancel any pending "receive" request (however, the handler will
434 * *NOT* be called, not even with a NULL message). Any pending
435 * transmission request will also be cancelled UNLESS the callback for
436 * the transmission request has already been called, in which case the
437 * transmission 'finish_pending_write' argument determines whether or
438 * not the write is guaranteed to complete before the socket is fully
439 * destroyed (unless, of course, there is an error with the server in
440 * which case the message may still be lost).
442 * @param client handle to the service connection
445 GNUNET_CLIENT_disconnect (struct GNUNET_CLIENT_Connection *client)
447 if (GNUNET_YES == client->in_receive)
449 GNUNET_CONNECTION_receive_cancel (client->connection);
450 client->in_receive = GNUNET_NO;
452 if (NULL != client->th)
454 GNUNET_CLIENT_notify_transmit_ready_cancel (client->th);
457 if (NULL != client->connection)
459 GNUNET_CONNECTION_destroy (client->connection);
460 client->connection = NULL;
462 if (NULL != client->receive_task)
464 GNUNET_SCHEDULER_cancel (client->receive_task);
465 client->receive_task = NULL;
467 if (NULL != client->tag)
469 GNUNET_free (client->tag);
472 client->receiver_handler = NULL;
473 GNUNET_array_grow (client->received_buf, client->received_size, 0);
474 GNUNET_free (client->service_name);
475 GNUNET_free (client);
480 * Check if message is complete. Sets the "msg_complete" member
481 * in the client struct.
483 * @param client connection with the buffer to check
486 check_complete (struct GNUNET_CLIENT_Connection *client)
488 if ((client->received_pos >= sizeof (struct GNUNET_MessageHeader)) &&
489 (client->received_pos >=
490 ntohs (((const struct GNUNET_MessageHeader *) client->received_buf)->
492 client->msg_complete = GNUNET_YES;
497 * Callback function for data received from the network. Note that
498 * both @a available and @a errCode would be 0 if the read simply timed out.
501 * @param buf pointer to received data
502 * @param available number of bytes availabe in @a buf,
503 * possibly 0 (on errors)
504 * @param addr address of the sender
505 * @param addrlen size of @a addr
506 * @param errCode value of errno (on errors receiving)
509 receive_helper (void *cls,
512 const struct sockaddr *addr,
516 struct GNUNET_CLIENT_Connection *client = cls;
517 struct GNUNET_TIME_Relative remaining;
518 GNUNET_CLIENT_MessageHandler receive_handler;
519 void *receive_handler_cls;
521 GNUNET_assert (GNUNET_NO == client->msg_complete);
522 GNUNET_assert (GNUNET_YES == client->in_receive);
523 client->in_receive = GNUNET_NO;
524 if ( (0 == available) ||
525 (NULL == client->connection) ||
528 /* signal timeout! */
529 LOG (GNUNET_ERROR_TYPE_DEBUG,
530 "Timeout in receive_helper, available %u, client->connection %s, errCode `%s'\n",
531 (unsigned int) available,
532 NULL == client->connection ? "NULL" : "non-NULL",
534 /* remember failure */
535 client->in_receive = GNUNET_SYSERR;
536 if (NULL != (receive_handler = client->receiver_handler))
538 receive_handler_cls = client->receiver_handler_cls;
539 client->receiver_handler = NULL;
540 receive_handler (receive_handler_cls,
545 /* FIXME: optimize for common fast case where buf contains the
546 * entire message and we need no copying... */
548 /* slow path: append to array */
549 if (client->received_size < client->received_pos + available)
550 GNUNET_array_grow (client->received_buf, client->received_size,
551 client->received_pos + available);
552 memcpy (&client->received_buf[client->received_pos], buf, available);
553 client->received_pos += available;
554 check_complete (client);
555 /* check for timeout */
556 remaining = GNUNET_TIME_absolute_get_remaining (client->receive_timeout);
557 if (0 == remaining.rel_value_us)
559 /* signal timeout! */
560 if (NULL != (receive_handler = client->receiver_handler))
562 client->receiver_handler = NULL;
563 receive_handler (client->receiver_handler_cls, NULL);
567 /* back to receive -- either for more data or to call callback! */
568 GNUNET_CLIENT_receive (client, client->receiver_handler,
569 client->receiver_handler_cls, remaining);
574 * Continuation to call the receive callback.
576 * @param cls our handle to the client connection
577 * @param tc scheduler context
580 receive_task (void *cls,
581 const struct GNUNET_SCHEDULER_TaskContext *tc)
583 struct GNUNET_CLIENT_Connection *client = cls;
584 GNUNET_CLIENT_MessageHandler handler = client->receiver_handler;
585 const struct GNUNET_MessageHeader *cmsg =
586 (const struct GNUNET_MessageHeader *) client->received_buf;
587 void *handler_cls = client->receiver_handler_cls;
588 uint16_t msize = ntohs (cmsg->size);
589 char mbuf[msize] GNUNET_ALIGN;
590 struct GNUNET_MessageHeader *msg = (struct GNUNET_MessageHeader *) mbuf;
592 client->receive_task = NULL;
593 if ( (GNUNET_SYSERR == client->in_receive) &&
594 (GNUNET_YES != client->msg_complete) )
596 /* Connection failure, signal to caller! */
597 client->receiver_handler = NULL;
599 handler (handler_cls,
603 LOG (GNUNET_ERROR_TYPE_DEBUG,
604 "Received message of type %u and size %u from %s service.\n",
607 client->service_name);
608 GNUNET_assert (GNUNET_YES == client->msg_complete);
609 GNUNET_assert (client->received_pos >= msize);
610 memcpy (msg, cmsg, msize);
611 memmove (client->received_buf, &client->received_buf[msize],
612 client->received_pos - msize);
613 client->received_pos -= msize;
614 client->msg_complete = GNUNET_NO;
615 client->receiver_handler = NULL;
616 check_complete (client);
618 handler (handler_cls, msg);
623 * Read from the service.
625 * @param client the service
626 * @param handler function to call with the message
627 * @param handler_cls closure for @a handler
628 * @param timeout how long to wait until timing out
631 GNUNET_CLIENT_receive (struct GNUNET_CLIENT_Connection *client,
632 GNUNET_CLIENT_MessageHandler handler,
634 struct GNUNET_TIME_Relative timeout)
636 if (NULL == client->connection)
638 /* already disconnected, fail instantly! */
639 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
640 "Client API violation for service `%s'\n",
641 client->service_name);
642 GNUNET_break (0); /* this should not happen in well-written code! */
644 handler (handler_cls,
648 client->receiver_handler = handler;
649 client->receiver_handler_cls = handler_cls;
650 client->receive_timeout = GNUNET_TIME_relative_to_absolute (timeout);
651 if ( (GNUNET_YES == client->msg_complete) ||
652 (GNUNET_SYSERR == client->in_receive) )
654 GNUNET_assert (NULL == client->receive_task);
655 client->receive_task = GNUNET_SCHEDULER_add_now (&receive_task, client);
658 LOG (GNUNET_ERROR_TYPE_DEBUG,
659 "calling GNUNET_CONNECTION_receive\n");
660 GNUNET_assert (GNUNET_NO == client->in_receive);
661 client->in_receive = GNUNET_YES;
662 GNUNET_CONNECTION_receive (client->connection,
663 GNUNET_SERVER_MAX_MESSAGE_SIZE - 1,
671 * Handle for a test to check if a service is running.
673 struct GNUNET_CLIENT_TestHandle
676 * Function to call with the result of the test.
678 GNUNET_CLIENT_TestResultCallback cb;
686 * Client connection we are using for the test, if any.
688 struct GNUNET_CLIENT_Connection *client;
691 * Handle for the transmission request, if any.
693 struct GNUNET_CLIENT_TransmitHandle *th;
696 * Deadline for calling @e cb.
698 struct GNUNET_TIME_Absolute test_deadline;
701 * ID of task used for asynchronous operations.
703 struct GNUNET_SCHEDULER_Task * task;
706 * Final result to report back (once known).
713 * Abort testing for service.
715 * @param th test handle
718 GNUNET_CLIENT_service_test_cancel (struct GNUNET_CLIENT_TestHandle *th)
722 GNUNET_CLIENT_notify_transmit_ready_cancel (th->th);
725 if (NULL != th->client)
727 GNUNET_CLIENT_disconnect (th->client);
730 if (NULL != th->task)
732 GNUNET_SCHEDULER_cancel (th->task);
740 * Task that reports back the result by calling the callback
741 * and then cleans up.
743 * @param cls the `struct GNUNET_CLIENT_TestHandle`
744 * @param tc scheduler context
747 report_result (void *cls,
748 const struct GNUNET_SCHEDULER_TaskContext *tc)
750 struct GNUNET_CLIENT_TestHandle *th = cls;
753 th->cb (th->cb_cls, th->result);
754 GNUNET_CLIENT_service_test_cancel (th);
759 * Report service test result asynchronously back to callback.
761 * @param th test handle with the result and the callback
762 * @param result result to report
765 service_test_report (struct GNUNET_CLIENT_TestHandle *th,
769 th->task = GNUNET_SCHEDULER_add_now (&report_result,
775 * Receive confirmation from test, service is up.
777 * @param cls closure with the `struct GNUNET_CLIENT_TestHandle`
778 * @param msg message received, NULL on timeout or fatal error
781 confirm_handler (void *cls,
782 const struct GNUNET_MessageHeader *msg)
784 struct GNUNET_CLIENT_TestHandle *th = cls;
786 /* We may want to consider looking at the reply in more
787 * detail in the future, for example, is this the
788 * correct service? FIXME! */
791 LOG (GNUNET_ERROR_TYPE_DEBUG,
792 "Received confirmation that service is running.\n");
793 service_test_report (th, GNUNET_YES);
797 service_test_report (th, GNUNET_NO);
803 * Send the 'TEST' message to the service. If successful, prepare to
806 * @param cls the `struct GNUNET_CLIENT_TestHandle` of the test
807 * @param size number of bytes available in @a buf
808 * @param buf where to write the message
809 * @return number of bytes written to @a buf
812 write_test (void *cls, size_t size, void *buf)
814 struct GNUNET_CLIENT_TestHandle *th = cls;
815 struct GNUNET_MessageHeader *msg;
818 if (size < sizeof (struct GNUNET_MessageHeader))
820 LOG (GNUNET_ERROR_TYPE_DEBUG,
821 "Failed to transmit TEST request.\n");
822 service_test_report (th, GNUNET_NO);
823 return 0; /* client disconnected */
825 LOG (GNUNET_ERROR_TYPE_DEBUG,
826 "Transmitting `%s' request.\n",
828 msg = (struct GNUNET_MessageHeader *) buf;
829 msg->type = htons (GNUNET_MESSAGE_TYPE_TEST);
830 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
831 GNUNET_CLIENT_receive (th->client,
832 &confirm_handler, th,
833 GNUNET_TIME_absolute_get_remaining
834 (th->test_deadline));
835 return sizeof (struct GNUNET_MessageHeader);
840 * Test if the service is running. If we are given a UNIXPATH or a
841 * local address, we do this NOT by trying to connect to the service,
842 * but by trying to BIND to the same port. If the BIND fails, we know
843 * the service is running.
845 * @param service name of the service to wait for
846 * @param cfg configuration to use
847 * @param timeout how long to wait at most
848 * @param cb function to call with the result
849 * @param cb_cls closure for @a cb
850 * @return handle to cancel the test
852 struct GNUNET_CLIENT_TestHandle *
853 GNUNET_CLIENT_service_test (const char *service,
854 const struct GNUNET_CONFIGURATION_Handle *cfg,
855 struct GNUNET_TIME_Relative timeout,
856 GNUNET_CLIENT_TestResultCallback cb,
859 struct GNUNET_CLIENT_TestHandle *th;
861 unsigned long long port;
862 struct GNUNET_NETWORK_Handle *sock;
864 th = GNUNET_new (struct GNUNET_CLIENT_TestHandle);
867 th->test_deadline = GNUNET_TIME_relative_to_absolute (timeout);
868 LOG (GNUNET_ERROR_TYPE_DEBUG,
869 "Testing if service `%s' is running.\n",
873 /* probe UNIX support */
874 struct sockaddr_un s_un;
880 GNUNET_CONFIGURATION_get_value_filename (cfg,
884 (0 < strlen (unixpath))) /* We have a non-NULL unixpath, does that mean it's valid? */
886 if (strlen (unixpath) >= sizeof (s_un.sun_path))
888 LOG (GNUNET_ERROR_TYPE_WARNING,
889 _("UNIXPATH `%s' too long, maximum length is %llu\n"),
891 (unsigned long long) sizeof (s_un.sun_path));
892 unixpath = GNUNET_NETWORK_shorten_unixpath (unixpath);
893 LOG (GNUNET_ERROR_TYPE_INFO,
894 _("Using `%s' instead\n"), unixpath);
898 abstract = GNUNET_CONFIGURATION_get_value_yesno (cfg,
900 "USE_ABSTRACT_SOCKETS");
902 abstract = GNUNET_NO;
904 if ((NULL != unixpath) && (GNUNET_YES != abstract))
906 if (GNUNET_SYSERR == GNUNET_DISK_directory_create_for_file (unixpath))
907 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
910 if (NULL != unixpath)
912 sock = GNUNET_NETWORK_socket_create (PF_UNIX, SOCK_STREAM, 0);
915 memset (&s_un, 0, sizeof (s_un));
916 s_un.sun_family = AF_UNIX;
917 strncpy (s_un.sun_path, unixpath, sizeof (s_un.sun_path) - 1);
918 if (GNUNET_YES == abstract)
919 s_un.sun_path[0] = '\0';
920 #if HAVE_SOCKADDR_IN_SIN_LEN
921 s_un.sun_len = (u_char) sizeof (struct sockaddr_un);
924 GNUNET_NETWORK_socket_bind (sock, (const struct sockaddr *) &s_un,
925 sizeof (struct sockaddr_un)))
927 /* failed to bind => service must be running */
928 GNUNET_free (unixpath);
929 (void) GNUNET_NETWORK_socket_close (sock);
930 service_test_report (th, GNUNET_YES);
933 (void) GNUNET_NETWORK_socket_close (sock);
937 GNUNET_free_non_null (unixpath);
943 GNUNET_CONFIGURATION_get_value_number (cfg, service, "PORT", &port)) ||
946 GNUNET_CONFIGURATION_get_value_string (cfg, service, "HOSTNAME",
949 /* UNIXPATH failed (if possible) AND IP failed => error */
950 service_test_report (th, GNUNET_SYSERR);
954 if (0 == strcmp ("localhost", hostname)
960 /* can test using 'bind' */
961 struct sockaddr_in s_in;
963 memset (&s_in, 0, sizeof (s_in));
964 #if HAVE_SOCKADDR_IN_SIN_LEN
965 s_in.sin_len = sizeof (struct sockaddr_in);
967 s_in.sin_family = AF_INET;
968 s_in.sin_port = htons (port);
970 sock = GNUNET_NETWORK_socket_create (AF_INET, SOCK_STREAM, 0);
974 GNUNET_NETWORK_socket_bind (sock, (const struct sockaddr *) &s_in,
977 /* failed to bind => service must be running */
978 GNUNET_free (hostname);
979 (void) GNUNET_NETWORK_socket_close (sock);
980 service_test_report (th, GNUNET_YES);
983 (void) GNUNET_NETWORK_socket_close (sock);
987 if (0 == strcmp ("ip6-localhost", hostname)
993 /* can test using 'bind' */
994 struct sockaddr_in6 s_in6;
996 memset (&s_in6, 0, sizeof (s_in6));
997 #if HAVE_SOCKADDR_IN_SIN_LEN
998 s_in6.sin6_len = sizeof (struct sockaddr_in6);
1000 s_in6.sin6_family = AF_INET6;
1001 s_in6.sin6_port = htons (port);
1003 sock = GNUNET_NETWORK_socket_create (AF_INET6, SOCK_STREAM, 0);
1007 GNUNET_NETWORK_socket_bind (sock, (const struct sockaddr *) &s_in6,
1010 /* failed to bind => service must be running */
1011 GNUNET_free (hostname);
1012 (void) GNUNET_NETWORK_socket_close (sock);
1013 service_test_report (th, GNUNET_YES);
1016 (void) GNUNET_NETWORK_socket_close (sock);
1020 if (((0 == strcmp ("localhost", hostname)) ||
1021 (0 == strcmp ("ip6-localhost", hostname)))
1027 /* all binds succeeded => claim service not running right now */
1028 GNUNET_free_non_null (hostname);
1029 service_test_report (th, GNUNET_NO);
1032 GNUNET_free_non_null (hostname);
1034 /* non-localhost, try 'connect' method */
1035 th->client = GNUNET_CLIENT_connect (service, cfg);
1036 if (NULL == th->client)
1038 LOG (GNUNET_ERROR_TYPE_INFO,
1039 _("Could not connect to service `%s', configuration broken.\n"),
1041 service_test_report (th, GNUNET_SYSERR);
1044 th->th = GNUNET_CLIENT_notify_transmit_ready (th->client,
1045 sizeof (struct GNUNET_MessageHeader),
1046 timeout, GNUNET_YES,
1050 LOG (GNUNET_ERROR_TYPE_WARNING,
1051 _("Failure to transmit request to service `%s'\n"), service);
1052 service_test_report (th, GNUNET_SYSERR);
1060 * Connection notifies us about failure or success of
1061 * a transmission request. Either pass it on to our
1062 * user or, if possible, retry.
1064 * @param cls our `struct GNUNET_CLIENT_TransmissionHandle`
1065 * @param size number of bytes available for transmission
1066 * @param buf where to write them
1067 * @return number of bytes written to @a buf
1070 client_notify (void *cls, size_t size, void *buf);
1074 * This task is run if we should re-try connection to the
1075 * service after a while.
1077 * @param cls our `struct GNUNET_CLIENT_TransmitHandle` of the request
1081 client_delayed_retry (void *cls,
1082 const struct GNUNET_SCHEDULER_TaskContext *tc)
1084 struct GNUNET_CLIENT_TransmitHandle *th = cls;
1085 struct GNUNET_TIME_Relative delay;
1087 th->reconnect_task = NULL;
1088 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1090 /* give up, was shutdown */
1091 th->client->th = NULL;
1092 th->notify (th->notify_cls, 0, NULL);
1096 th->client->connection =
1097 do_connect (th->client->service_name,
1099 th->client->attempts++);
1100 th->client->first_message = GNUNET_YES;
1101 if (NULL == th->client->connection)
1103 /* could happen if we're out of sockets */
1104 delay = GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining (th->timeout),
1105 th->client->back_off);
1106 th->client->back_off = GNUNET_TIME_STD_BACKOFF (th->client->back_off);
1107 LOG (GNUNET_ERROR_TYPE_DEBUG,
1108 "Transmission failed %u times, trying again in %s.\n",
1109 MAX_ATTEMPTS - th->attempts_left,
1110 GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_YES));
1111 GNUNET_assert (NULL == th->th);
1112 GNUNET_assert (NULL == th->reconnect_task);
1113 th->reconnect_task =
1114 GNUNET_SCHEDULER_add_delayed (delay, &client_delayed_retry, th);
1118 GNUNET_CONNECTION_notify_transmit_ready (th->client->connection, th->size,
1119 GNUNET_TIME_absolute_get_remaining
1120 (th->timeout), &client_notify,
1125 th->client->th = NULL;
1126 th->notify (th->notify_cls, 0, NULL);
1134 * Connection notifies us about failure or success of a transmission
1135 * request. Either pass it on to our user or, if possible, retry.
1137 * @param cls our `struct GNUNET_CLIENT_TransmissionHandle`
1138 * @param size number of bytes available for transmission
1139 * @param buf where to write them
1140 * @return number of bytes written to @a buf
1143 client_notify (void *cls,
1147 struct GNUNET_CLIENT_TransmitHandle *th = cls;
1148 struct GNUNET_CLIENT_Connection *client = th->client;
1150 struct GNUNET_TIME_Relative delay;
1152 LOG (GNUNET_ERROR_TYPE_DEBUG,
1153 "client_notify is running\n");
1158 delay = GNUNET_TIME_absolute_get_remaining (th->timeout);
1159 delay.rel_value_us /= 2;
1160 if ( (GNUNET_YES != th->auto_retry) ||
1161 (0 == --th->attempts_left) ||
1162 (delay.rel_value_us < 1)||
1163 (0 != (GNUNET_SCHEDULER_get_reason() & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
1165 LOG (GNUNET_ERROR_TYPE_DEBUG,
1166 "Transmission failed %u times, giving up.\n",
1167 MAX_ATTEMPTS - th->attempts_left);
1169 th->notify (th->notify_cls, 0, NULL));
1174 LOG (GNUNET_ERROR_TYPE_DEBUG,
1175 "Failed to connect to `%s', automatically trying again.\n",
1176 client->service_name);
1177 if (GNUNET_YES == client->in_receive)
1179 GNUNET_CONNECTION_receive_cancel (client->connection);
1180 client->in_receive = GNUNET_NO;
1182 GNUNET_CONNECTION_destroy (client->connection);
1183 client->connection = NULL;
1184 delay = GNUNET_TIME_relative_min (delay, client->back_off);
1186 GNUNET_TIME_relative_min (GNUNET_TIME_relative_multiply
1187 (client->back_off, 2),
1188 GNUNET_TIME_UNIT_SECONDS);
1189 LOG (GNUNET_ERROR_TYPE_DEBUG,
1190 "Transmission failed %u times, trying again in %s.\n",
1191 MAX_ATTEMPTS - th->attempts_left,
1192 GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_YES));
1194 GNUNET_assert (NULL == th->reconnect_task);
1195 GNUNET_assert (NULL == th->th);
1196 th->reconnect_task =
1197 GNUNET_SCHEDULER_add_delayed (delay, &client_delayed_retry, th);
1200 GNUNET_assert (size >= th->size);
1201 ret = th->notify (th->notify_cls, size, buf);
1203 if (sizeof (struct GNUNET_MessageHeader) <= ret)
1205 LOG (GNUNET_ERROR_TYPE_DEBUG,
1206 "Transmitting message of type %u and size %u to %s service.\n",
1207 ntohs (((struct GNUNET_MessageHeader *) buf)->type),
1208 ntohs (((struct GNUNET_MessageHeader *) buf)->size),
1209 client->service_name);
1216 * Ask the client to call us once the specified number of bytes
1217 * are free in the transmission buffer. Will never call the @a notify
1218 * callback in this task, but always first go into the scheduler.
1220 * @param client connection to the service
1221 * @param size number of bytes to send
1222 * @param timeout after how long should we give up (and call
1223 * notify with buf NULL and size 0)?
1224 * @param auto_retry if the connection to the service dies, should we
1225 * automatically re-connect and retry (within the timeout period)
1226 * or should we immediately fail in this case? Pass GNUNET_YES
1227 * if the caller does not care about temporary connection errors,
1228 * for example because the protocol is stateless
1229 * @param notify function to call
1230 * @param notify_cls closure for @a notify
1231 * @return NULL if our buffer will never hold size bytes,
1232 * a handle if the notify callback was queued (can be used to cancel)
1234 struct GNUNET_CLIENT_TransmitHandle *
1235 GNUNET_CLIENT_notify_transmit_ready (struct GNUNET_CLIENT_Connection *client,
1237 struct GNUNET_TIME_Relative timeout,
1239 GNUNET_CONNECTION_TransmitReadyNotify notify,
1242 struct GNUNET_CLIENT_TransmitHandle *th;
1244 if (NULL != client->th)
1246 /* If this breaks, you most likley called this function twice without waiting
1247 * for completion or canceling the request */
1251 th = GNUNET_new (struct GNUNET_CLIENT_TransmitHandle);
1252 th->client = client;
1254 th->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1255 /* always auto-retry on first message to service */
1256 th->auto_retry = (GNUNET_YES == client->first_message) ? GNUNET_YES : auto_retry;
1257 client->first_message = GNUNET_NO;
1258 th->notify = notify;
1259 th->notify_cls = notify_cls;
1260 th->attempts_left = MAX_ATTEMPTS;
1262 if (NULL == client->connection)
1264 GNUNET_assert (NULL == th->th);
1265 GNUNET_assert (NULL == th->reconnect_task);
1266 th->reconnect_task =
1267 GNUNET_SCHEDULER_add_delayed (client->back_off,
1268 &client_delayed_retry,
1273 th->th = GNUNET_CONNECTION_notify_transmit_ready (client->connection,
1291 * Cancel a request for notification.
1293 * @param th handle from the original request.
1296 GNUNET_CLIENT_notify_transmit_ready_cancel (struct GNUNET_CLIENT_TransmitHandle *th)
1298 if (NULL != th->reconnect_task)
1300 GNUNET_assert (NULL == th->th);
1301 GNUNET_SCHEDULER_cancel (th->reconnect_task);
1302 th->reconnect_task = NULL;
1306 GNUNET_assert (NULL != th->th);
1307 GNUNET_CONNECTION_notify_transmit_ready_cancel (th->th);
1309 th->client->th = NULL;
1315 * Function called to notify a client about the socket
1316 * begin ready to queue the message. @a buf will be
1317 * NULL and @a size zero if the socket was closed for
1318 * writing in the meantime.
1320 * @param cls closure of type `struct TransmitGetResponseContext *`
1321 * @param size number of bytes available in @a buf
1322 * @param buf where the callee should write the message
1323 * @return number of bytes written to @a buf
1326 transmit_for_response (void *cls,
1330 struct TransmitGetResponseContext *tc = cls;
1333 tc->client->tag = NULL;
1334 msize = ntohs (tc->hdr->size);
1337 LOG (GNUNET_ERROR_TYPE_DEBUG,
1338 "Could not submit request, not expecting to receive a response.\n");
1340 tc->rn (tc->rn_cls, NULL);
1344 GNUNET_assert (size >= msize);
1345 memcpy (buf, tc->hdr, msize);
1346 GNUNET_CLIENT_receive (tc->client,
1349 GNUNET_TIME_absolute_get_remaining (tc->timeout));
1356 * Convenience API that combines sending a request
1357 * to the service and waiting for a response.
1358 * If either operation times out, the callback
1359 * will be called with a "NULL" response (in which
1360 * case the connection should probably be destroyed).
1362 * @param client connection to use
1363 * @param hdr message to transmit
1364 * @param timeout when to give up (for both transmission
1365 * and for waiting for a response)
1366 * @param auto_retry if the connection to the service dies, should we
1367 * automatically re-connect and retry (within the timeout period)
1368 * or should we immediately fail in this case? Pass GNUNET_YES
1369 * if the caller does not care about temporary connection errors,
1370 * for example because the protocol is stateless
1371 * @param rn function to call with the response
1372 * @param rn_cls closure for @a rn
1373 * @return #GNUNET_OK on success, #GNUNET_SYSERR if a request
1374 * is already pending
1377 GNUNET_CLIENT_transmit_and_get_response (struct GNUNET_CLIENT_Connection *client,
1378 const struct GNUNET_MessageHeader *hdr,
1379 struct GNUNET_TIME_Relative timeout,
1381 GNUNET_CLIENT_MessageHandler rn,
1384 struct TransmitGetResponseContext *tc;
1387 if (NULL != client->th)
1388 return GNUNET_SYSERR;
1389 GNUNET_assert (NULL == client->tag);
1390 msize = ntohs (hdr->size);
1391 tc = GNUNET_malloc (sizeof (struct TransmitGetResponseContext) + msize);
1392 tc->client = client;
1393 tc->hdr = (const struct GNUNET_MessageHeader *) &tc[1];
1394 memcpy (&tc[1], hdr, msize);
1395 tc->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1397 tc->rn_cls = rn_cls;
1399 GNUNET_CLIENT_notify_transmit_ready (client, msize, timeout, auto_retry,
1400 &transmit_for_response, tc))
1404 return GNUNET_SYSERR;
1411 /* end of client.c */