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., 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 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->reconnect_task);
1112 th->reconnect_task =
1113 GNUNET_SCHEDULER_add_delayed (delay, &client_delayed_retry, th);
1117 GNUNET_CONNECTION_notify_transmit_ready (th->client->connection, th->size,
1118 GNUNET_TIME_absolute_get_remaining
1119 (th->timeout), &client_notify,
1124 th->client->th = NULL;
1125 th->notify (th->notify_cls, 0, NULL);
1133 * Connection notifies us about failure or success of a transmission
1134 * request. Either pass it on to our user or, if possible, retry.
1136 * @param cls our `struct GNUNET_CLIENT_TransmissionHandle`
1137 * @param size number of bytes available for transmission
1138 * @param buf where to write them
1139 * @return number of bytes written to @a buf
1142 client_notify (void *cls,
1146 struct GNUNET_CLIENT_TransmitHandle *th = cls;
1147 struct GNUNET_CLIENT_Connection *client = th->client;
1149 struct GNUNET_TIME_Relative delay;
1151 LOG (GNUNET_ERROR_TYPE_DEBUG,
1152 "client_notify is running\n");
1157 delay = GNUNET_TIME_absolute_get_remaining (th->timeout);
1158 delay.rel_value_us /= 2;
1159 if ( (GNUNET_YES != th->auto_retry) ||
1160 (0 == --th->attempts_left) ||
1161 (delay.rel_value_us < 1)||
1162 (0 != (GNUNET_SCHEDULER_get_reason() & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
1164 LOG (GNUNET_ERROR_TYPE_DEBUG,
1165 "Transmission failed %u times, giving up.\n",
1166 MAX_ATTEMPTS - th->attempts_left);
1168 th->notify (th->notify_cls, 0, NULL));
1173 LOG (GNUNET_ERROR_TYPE_DEBUG,
1174 "Failed to connect to `%s', automatically trying again.\n",
1175 client->service_name);
1176 if (GNUNET_YES == client->in_receive)
1178 GNUNET_CONNECTION_receive_cancel (client->connection);
1179 client->in_receive = GNUNET_NO;
1181 GNUNET_CONNECTION_destroy (client->connection);
1182 client->connection = NULL;
1183 delay = GNUNET_TIME_relative_min (delay, client->back_off);
1185 GNUNET_TIME_relative_min (GNUNET_TIME_relative_multiply
1186 (client->back_off, 2),
1187 GNUNET_TIME_UNIT_SECONDS);
1188 LOG (GNUNET_ERROR_TYPE_DEBUG,
1189 "Transmission failed %u times, trying again in %s.\n",
1190 MAX_ATTEMPTS - th->attempts_left,
1191 GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_YES));
1193 GNUNET_assert (NULL == th->reconnect_task);
1194 th->reconnect_task =
1195 GNUNET_SCHEDULER_add_delayed (delay, &client_delayed_retry, th);
1198 GNUNET_assert (size >= th->size);
1199 ret = th->notify (th->notify_cls, size, buf);
1201 if (sizeof (struct GNUNET_MessageHeader) <= ret)
1203 LOG (GNUNET_ERROR_TYPE_DEBUG,
1204 "Transmitting message of type %u and size %u to %s service.\n",
1205 ntohs (((struct GNUNET_MessageHeader *) buf)->type),
1206 ntohs (((struct GNUNET_MessageHeader *) buf)->size),
1207 client->service_name);
1214 * Ask the client to call us once the specified number of bytes
1215 * are free in the transmission buffer. Will never call the @a notify
1216 * callback in this task, but always first go into the scheduler.
1218 * @param client connection to the service
1219 * @param size number of bytes to send
1220 * @param timeout after how long should we give up (and call
1221 * notify with buf NULL and size 0)?
1222 * @param auto_retry if the connection to the service dies, should we
1223 * automatically re-connect and retry (within the timeout period)
1224 * or should we immediately fail in this case? Pass GNUNET_YES
1225 * if the caller does not care about temporary connection errors,
1226 * for example because the protocol is stateless
1227 * @param notify function to call
1228 * @param notify_cls closure for @a notify
1229 * @return NULL if our buffer will never hold size bytes,
1230 * a handle if the notify callback was queued (can be used to cancel)
1232 struct GNUNET_CLIENT_TransmitHandle *
1233 GNUNET_CLIENT_notify_transmit_ready (struct GNUNET_CLIENT_Connection *client,
1235 struct GNUNET_TIME_Relative timeout,
1237 GNUNET_CONNECTION_TransmitReadyNotify notify,
1240 struct GNUNET_CLIENT_TransmitHandle *th;
1242 if (NULL != client->th)
1244 /* If this breaks, you most likley called this function twice without waiting
1245 * for completion or canceling the request */
1249 th = GNUNET_new (struct GNUNET_CLIENT_TransmitHandle);
1250 th->client = client;
1252 th->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1253 /* always auto-retry on first message to service */
1254 th->auto_retry = (GNUNET_YES == client->first_message) ? GNUNET_YES : auto_retry;
1255 client->first_message = GNUNET_NO;
1256 th->notify = notify;
1257 th->notify_cls = notify_cls;
1258 th->attempts_left = MAX_ATTEMPTS;
1260 if (NULL == client->connection)
1262 GNUNET_assert (NULL == th->reconnect_task);
1263 th->reconnect_task =
1264 GNUNET_SCHEDULER_add_delayed (client->back_off,
1265 &client_delayed_retry,
1270 th->th = GNUNET_CONNECTION_notify_transmit_ready (client->connection,
1288 * Cancel a request for notification.
1290 * @param th handle from the original request.
1293 GNUNET_CLIENT_notify_transmit_ready_cancel (struct GNUNET_CLIENT_TransmitHandle *th)
1295 if (NULL != th->reconnect_task)
1297 GNUNET_assert (NULL == th->th);
1298 GNUNET_SCHEDULER_cancel (th->reconnect_task);
1299 th->reconnect_task = NULL;
1303 GNUNET_assert (NULL != th->th);
1304 GNUNET_CONNECTION_notify_transmit_ready_cancel (th->th);
1306 th->client->th = NULL;
1312 * Function called to notify a client about the socket
1313 * begin ready to queue the message. @a buf will be
1314 * NULL and @a size zero if the socket was closed for
1315 * writing in the meantime.
1317 * @param cls closure of type `struct TransmitGetResponseContext *`
1318 * @param size number of bytes available in @a buf
1319 * @param buf where the callee should write the message
1320 * @return number of bytes written to @a buf
1323 transmit_for_response (void *cls,
1327 struct TransmitGetResponseContext *tc = cls;
1330 tc->client->tag = NULL;
1331 msize = ntohs (tc->hdr->size);
1334 LOG (GNUNET_ERROR_TYPE_DEBUG,
1335 "Could not submit request, not expecting to receive a response.\n");
1337 tc->rn (tc->rn_cls, NULL);
1341 GNUNET_assert (size >= msize);
1342 memcpy (buf, tc->hdr, msize);
1343 GNUNET_CLIENT_receive (tc->client,
1346 GNUNET_TIME_absolute_get_remaining (tc->timeout));
1353 * Convenience API that combines sending a request
1354 * to the service and waiting for a response.
1355 * If either operation times out, the callback
1356 * will be called with a "NULL" response (in which
1357 * case the connection should probably be destroyed).
1359 * @param client connection to use
1360 * @param hdr message to transmit
1361 * @param timeout when to give up (for both transmission
1362 * and for waiting for a response)
1363 * @param auto_retry if the connection to the service dies, should we
1364 * automatically re-connect and retry (within the timeout period)
1365 * or should we immediately fail in this case? Pass GNUNET_YES
1366 * if the caller does not care about temporary connection errors,
1367 * for example because the protocol is stateless
1368 * @param rn function to call with the response
1369 * @param rn_cls closure for @a rn
1370 * @return #GNUNET_OK on success, #GNUNET_SYSERR if a request
1371 * is already pending
1374 GNUNET_CLIENT_transmit_and_get_response (struct GNUNET_CLIENT_Connection *client,
1375 const struct GNUNET_MessageHeader *hdr,
1376 struct GNUNET_TIME_Relative timeout,
1378 GNUNET_CLIENT_MessageHandler rn,
1381 struct TransmitGetResponseContext *tc;
1384 if (NULL != client->th)
1385 return GNUNET_SYSERR;
1386 GNUNET_assert (NULL == client->tag);
1387 msize = ntohs (hdr->size);
1388 tc = GNUNET_malloc (sizeof (struct TransmitGetResponseContext) + msize);
1389 tc->client = client;
1390 tc->hdr = (const struct GNUNET_MessageHeader *) &tc[1];
1391 memcpy (&tc[1], hdr, msize);
1392 tc->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1394 tc->rn_cls = rn_cls;
1396 GNUNET_CLIENT_notify_transmit_ready (client, msize, timeout, auto_retry,
1397 &transmit_for_response, tc))
1401 return GNUNET_SYSERR;
1408 /* end of client.c */