2 This file is part of GNUnet.
3 Copyright (C) 2001-2013 GNUnet e.V.
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"
32 #include "gnunet_socks.h"
36 * How often do we re-try tranmsitting requests before giving up?
37 * Note that if we succeeded transmitting a request but failed to read
38 * a response, we do NOT re-try.
40 #define MAX_ATTEMPTS 50
42 #define LOG(kind,...) GNUNET_log_from (kind, "util",__VA_ARGS__)
45 * Handle for a transmission request.
47 struct GNUNET_CLIENT_TransmitHandle
52 struct GNUNET_CLIENT_Connection *client;
55 * Function to call to get the data for transmission.
57 GNUNET_CONNECTION_TransmitReadyNotify notify;
60 * Closure for @e notify.
65 * Handle to the transmission with the underlying
68 struct GNUNET_CONNECTION_TransmitHandle *th;
71 * If we are re-trying and are delaying to do so,
72 * handle to the scheduled task managing the delay.
74 struct GNUNET_SCHEDULER_Task *reconnect_task;
77 * Timeout for the operation overall.
79 struct GNUNET_TIME_Absolute timeout;
82 * Number of bytes requested.
87 * Are we allowed to re-try to connect without telling
88 * the user (of this API) about the connection troubles?
93 * Number of attempts left for transmitting the request. We may
94 * fail the first time (say because the service is not yet up), in
95 * which case (if auto_retry is set) we wait a bit and re-try
96 * (timeout permitting).
98 unsigned int attempts_left;
104 * Context for processing
105 * "GNUNET_CLIENT_transmit_and_get_response" requests.
107 struct TransmitGetResponseContext
112 struct GNUNET_CLIENT_Connection *client;
115 * Message to transmit; do not free, allocated
116 * right after this struct.
118 const struct GNUNET_MessageHeader *hdr;
123 struct GNUNET_TIME_Absolute timeout;
126 * Function to call when done.
128 GNUNET_CLIENT_MessageHandler rn;
137 * Struct to refer to a GNUnet TCP connection.
138 * This is more than just a socket because if the server
139 * drops the connection, the client automatically tries
140 * to reconnect (and for that needs connection information).
142 struct GNUNET_CLIENT_Connection
146 * The connection handle, NULL if not live
148 struct GNUNET_CONNECTION_Handle *connection;
153 const struct GNUNET_CONFIGURATION_Handle *cfg;
156 * Name of the service we interact with.
161 * Context of a transmit_and_get_response operation, NULL
162 * if no such operation is pending.
164 struct TransmitGetResponseContext *tag;
167 * Handler for current receiver task.
169 GNUNET_CLIENT_MessageHandler receiver_handler;
172 * Closure for @e receiver_handler.
174 void *receiver_handler_cls;
177 * Handle for a pending transmission request, NULL if there is
180 struct GNUNET_CLIENT_TransmitHandle *th;
183 * If we are re-trying and are delaying to do so,
184 * handle to the scheduled task managing the delay.
186 struct GNUNET_SCHEDULER_Task * receive_task;
189 * Buffer for received message.
194 * Timeout for receiving a response (absolute time).
196 struct GNUNET_TIME_Absolute receive_timeout;
199 * Current value for our incremental back-off (for
202 struct GNUNET_TIME_Relative back_off;
205 * Number of bytes in received_buf that are valid.
210 * Size of received_buf.
212 unsigned int received_size;
215 * Do we have a complete response in received_buf?
220 * Are we currently busy doing receive-processing?
221 * #GNUNET_YES if so, #GNUNET_NO if not. #GNUNET_SYSERR
222 * if the connection has failed (but we may not have
223 * closed the handle itself yet).
228 * Is this the first message we are sending to the service?
233 * How often have we tried to connect?
235 unsigned int attempts;
241 * Try connecting to the server using UNIX domain sockets.
243 * @param service_name name of service to connect to
244 * @param cfg configuration to use
245 * @return NULL on error, connection to UNIX otherwise
247 static struct GNUNET_CONNECTION_Handle *
248 try_unixpath (const char *service_name,
249 const struct GNUNET_CONFIGURATION_Handle *cfg)
252 struct GNUNET_CONNECTION_Handle *connection;
254 struct sockaddr_un s_un;
257 if ((GNUNET_OK == GNUNET_CONFIGURATION_get_value_filename (cfg, service_name, "UNIXPATH", &unixpath)) &&
258 (0 < strlen (unixpath)))
260 /* We have a non-NULL unixpath, need to validate it */
261 if (strlen (unixpath) >= sizeof (s_un.sun_path))
263 LOG (GNUNET_ERROR_TYPE_WARNING,
264 _("UNIXPATH `%s' too long, maximum length is %llu\n"), unixpath,
265 (unsigned long long) sizeof (s_un.sun_path));
266 unixpath = GNUNET_NETWORK_shorten_unixpath (unixpath);
267 LOG (GNUNET_ERROR_TYPE_INFO,
268 _("Using `%s' instead\n"), unixpath);
269 if (NULL == unixpath)
272 connection = GNUNET_CONNECTION_create_from_connect_to_unixpath (cfg, unixpath);
273 if (NULL != connection)
275 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connected to unixpath `%s'!\n",
277 GNUNET_free (unixpath);
281 GNUNET_free_non_null (unixpath);
288 * Test whether the configuration has proper values for connection
289 * (UNIXPATH || (PORT && HOSTNAME)).
291 * @param service_name name of service to connect to
292 * @param cfg configuration to use
293 * @return #GNUNET_OK if the configuration is valid, #GNUNET_SYSERR if not
296 test_service_configuration (const char *service_name,
297 const struct GNUNET_CONFIGURATION_Handle *cfg)
299 int ret = GNUNET_SYSERR;
300 char *hostname = NULL;
301 unsigned long long port;
303 char *unixpath = NULL;
305 if ((GNUNET_OK == GNUNET_CONFIGURATION_get_value_filename (cfg, service_name, "UNIXPATH", &unixpath)) &&
306 (0 < strlen (unixpath)))
308 GNUNET_free_non_null (unixpath);
312 GNUNET_CONFIGURATION_have_value (cfg, service_name, "PORT")) &&
314 GNUNET_CONFIGURATION_get_value_number (cfg, service_name, "PORT", &port)) &&
315 (port <= 65535) && (0 != port) &&
317 GNUNET_CONFIGURATION_get_value_string (cfg, service_name, "HOSTNAME",
319 (0 != strlen (hostname)) )
321 GNUNET_free_non_null (hostname);
327 * Try to connect to the service.
329 * @param service_name name of service to connect to
330 * @param cfg configuration to use
331 * @param attempt counter used to alternate between IP and UNIX domain sockets
332 * @return NULL on error
334 static struct GNUNET_CONNECTION_Handle *
335 do_connect (const char *service_name,
336 const struct GNUNET_CONFIGURATION_Handle *cfg,
337 unsigned int attempt)
339 struct GNUNET_CONNECTION_Handle *connection;
341 unsigned long long port;
343 /* Never use a local source if a proxy is configured */
344 if (GNUNET_YES == GNUNET_SOCKS_check_service (service_name,cfg))
345 return GNUNET_SOCKS_do_connect (service_name,cfg);
348 if (0 == (attempt % 2))
350 /* on even rounds, try UNIX first */
351 connection = try_unixpath (service_name, cfg);
352 if (NULL != connection)
356 GNUNET_CONFIGURATION_have_value (cfg, service_name, "PORT"))
359 GNUNET_CONFIGURATION_get_value_number (cfg, service_name, "PORT", &port))
362 GNUNET_CONFIGURATION_get_value_string (cfg, service_name, "HOSTNAME",
365 LOG (GNUNET_ERROR_TYPE_WARNING,
367 ("Could not determine valid hostname and port for service `%s' from configuration.\n"),
371 if (0 == strlen (hostname))
373 GNUNET_free (hostname);
374 LOG (GNUNET_ERROR_TYPE_WARNING,
375 _("Need a non-empty hostname for service `%s'.\n"), service_name);
381 /* unspecified means 0 (disabled) */
387 /* if port is 0, try UNIX */
388 connection = try_unixpath (service_name, cfg);
389 if (NULL != connection)
391 GNUNET_free_non_null (hostname);
394 LOG (GNUNET_ERROR_TYPE_DEBUG,
395 "Port is 0 for service `%s', UNIXPATH did not work, returning NULL!\n",
397 GNUNET_free_non_null (hostname);
400 connection = GNUNET_CONNECTION_create_from_connect (cfg, hostname, port);
401 GNUNET_free (hostname);
407 * Get a connection with a service.
409 * @param service_name name of the service
410 * @param cfg configuration to use
411 * @return NULL on error (service unknown to configuration)
413 struct GNUNET_CLIENT_Connection *
414 GNUNET_CLIENT_connect (const char *service_name,
415 const struct GNUNET_CONFIGURATION_Handle *cfg)
417 struct GNUNET_CLIENT_Connection *client;
418 struct GNUNET_CONNECTION_Handle *connection;
421 test_service_configuration (service_name,
424 connection = do_connect (service_name, cfg, 0);
425 client = GNUNET_new (struct GNUNET_CLIENT_Connection);
426 client->first_message = GNUNET_YES;
427 client->attempts = 1;
428 client->connection = connection;
429 client->service_name = GNUNET_strdup (service_name);
431 client->back_off = GNUNET_TIME_UNIT_MILLISECONDS;
437 * Destroy connection with the service. This will automatically
438 * cancel any pending "receive" request (however, the handler will
439 * *NOT* be called, not even with a NULL message). Any pending
440 * transmission request will also be cancelled UNLESS the callback for
441 * the transmission request has already been called, in which case the
442 * transmission 'finish_pending_write' argument determines whether or
443 * not the write is guaranteed to complete before the socket is fully
444 * destroyed (unless, of course, there is an error with the server in
445 * which case the message may still be lost).
447 * @param client handle to the service connection
450 GNUNET_CLIENT_disconnect (struct GNUNET_CLIENT_Connection *client)
452 if (GNUNET_YES == client->in_receive)
454 GNUNET_CONNECTION_receive_cancel (client->connection);
455 client->in_receive = GNUNET_NO;
457 if (NULL != client->th)
459 GNUNET_CLIENT_notify_transmit_ready_cancel (client->th);
462 if (NULL != client->connection)
464 GNUNET_CONNECTION_destroy (client->connection);
465 client->connection = NULL;
467 if (NULL != client->receive_task)
469 GNUNET_SCHEDULER_cancel (client->receive_task);
470 client->receive_task = NULL;
472 if (NULL != client->tag)
474 GNUNET_free (client->tag);
477 client->receiver_handler = NULL;
478 GNUNET_array_grow (client->received_buf, client->received_size, 0);
479 GNUNET_free (client->service_name);
480 GNUNET_free (client);
485 * Check if message is complete. Sets the "msg_complete" member
486 * in the client struct.
488 * @param client connection with the buffer to check
491 check_complete (struct GNUNET_CLIENT_Connection *client)
493 if ((client->received_pos >= sizeof (struct GNUNET_MessageHeader)) &&
494 (client->received_pos >=
495 ntohs (((const struct GNUNET_MessageHeader *) client->received_buf)->
497 client->msg_complete = GNUNET_YES;
502 * Callback function for data received from the network. Note that
503 * both @a available and @a errCode would be 0 if the read simply timed out.
506 * @param buf pointer to received data
507 * @param available number of bytes availabe in @a buf,
508 * possibly 0 (on errors)
509 * @param addr address of the sender
510 * @param addrlen size of @a addr
511 * @param errCode value of errno (on errors receiving)
514 receive_helper (void *cls,
517 const struct sockaddr *addr,
521 struct GNUNET_CLIENT_Connection *client = cls;
522 struct GNUNET_TIME_Relative remaining;
523 GNUNET_CLIENT_MessageHandler receive_handler;
524 void *receive_handler_cls;
526 GNUNET_assert (GNUNET_NO == client->msg_complete);
527 GNUNET_assert (GNUNET_YES == client->in_receive);
528 client->in_receive = GNUNET_NO;
529 if ( (0 == available) ||
530 (NULL == client->connection) ||
533 /* signal timeout! */
534 LOG (GNUNET_ERROR_TYPE_DEBUG,
535 "Timeout in receive_helper, available %u, client->connection %s, errCode `%s'\n",
536 (unsigned int) available,
537 NULL == client->connection ? "NULL" : "non-NULL",
539 /* remember failure */
540 client->in_receive = GNUNET_SYSERR;
541 if (NULL != (receive_handler = client->receiver_handler))
543 receive_handler_cls = client->receiver_handler_cls;
544 client->receiver_handler = NULL;
545 receive_handler (receive_handler_cls,
550 /* FIXME: optimize for common fast case where buf contains the
551 * entire message and we need no copying... */
553 /* slow path: append to array */
554 if (client->received_size < client->received_pos + available)
555 GNUNET_array_grow (client->received_buf, client->received_size,
556 client->received_pos + available);
557 memcpy (&client->received_buf[client->received_pos], buf, available);
558 client->received_pos += available;
559 check_complete (client);
560 /* check for timeout */
561 remaining = GNUNET_TIME_absolute_get_remaining (client->receive_timeout);
562 if (0 == remaining.rel_value_us)
564 /* signal timeout! */
565 if (NULL != (receive_handler = client->receiver_handler))
567 client->receiver_handler = NULL;
568 receive_handler (client->receiver_handler_cls, NULL);
572 /* back to receive -- either for more data or to call callback! */
573 GNUNET_CLIENT_receive (client, client->receiver_handler,
574 client->receiver_handler_cls, remaining);
579 * Continuation to call the receive callback.
581 * @param cls our handle to the client connection
582 * @param tc scheduler context
585 receive_task (void *cls,
586 const struct GNUNET_SCHEDULER_TaskContext *tc)
588 struct GNUNET_CLIENT_Connection *client = cls;
589 GNUNET_CLIENT_MessageHandler handler = client->receiver_handler;
590 const struct GNUNET_MessageHeader *cmsg =
591 (const struct GNUNET_MessageHeader *) client->received_buf;
592 void *handler_cls = client->receiver_handler_cls;
593 uint16_t msize = ntohs (cmsg->size);
594 char mbuf[msize] GNUNET_ALIGN;
595 struct GNUNET_MessageHeader *msg = (struct GNUNET_MessageHeader *) mbuf;
597 client->receive_task = NULL;
598 if ( (GNUNET_SYSERR == client->in_receive) &&
599 (GNUNET_YES != client->msg_complete) )
601 /* Connection failure, signal to caller! */
602 client->receiver_handler = NULL;
604 handler (handler_cls,
608 LOG (GNUNET_ERROR_TYPE_DEBUG,
609 "Received message of type %u and size %u from %s service.\n",
612 client->service_name);
613 GNUNET_assert (GNUNET_YES == client->msg_complete);
614 GNUNET_assert (client->received_pos >= msize);
615 memcpy (msg, cmsg, msize);
616 memmove (client->received_buf, &client->received_buf[msize],
617 client->received_pos - msize);
618 client->received_pos -= msize;
619 client->msg_complete = GNUNET_NO;
620 client->receiver_handler = NULL;
621 check_complete (client);
623 handler (handler_cls, msg);
628 * Read from the service.
630 * @param client the service
631 * @param handler function to call with the message
632 * @param handler_cls closure for @a handler
633 * @param timeout how long to wait until timing out
636 GNUNET_CLIENT_receive (struct GNUNET_CLIENT_Connection *client,
637 GNUNET_CLIENT_MessageHandler handler,
639 struct GNUNET_TIME_Relative timeout)
641 if (NULL == client->connection)
643 /* already disconnected, fail instantly! */
644 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
645 "Client API violation for service `%s'\n",
646 client->service_name);
647 GNUNET_break (0); /* this should not happen in well-written code! */
649 handler (handler_cls,
653 client->receiver_handler = handler;
654 client->receiver_handler_cls = handler_cls;
655 client->receive_timeout = GNUNET_TIME_relative_to_absolute (timeout);
656 if ( (GNUNET_YES == client->msg_complete) ||
657 (GNUNET_SYSERR == client->in_receive) )
659 GNUNET_assert (NULL == client->receive_task);
660 client->receive_task = GNUNET_SCHEDULER_add_now (&receive_task, client);
663 LOG (GNUNET_ERROR_TYPE_DEBUG,
664 "calling GNUNET_CONNECTION_receive\n");
665 GNUNET_assert (GNUNET_NO == client->in_receive);
666 client->in_receive = GNUNET_YES;
667 GNUNET_CONNECTION_receive (client->connection,
668 GNUNET_SERVER_MAX_MESSAGE_SIZE - 1,
676 * Handle for a test to check if a service is running.
678 struct GNUNET_CLIENT_TestHandle
681 * Function to call with the result of the test.
683 GNUNET_CLIENT_TestResultCallback cb;
691 * Client connection we are using for the test, if any.
693 struct GNUNET_CLIENT_Connection *client;
696 * Handle for the transmission request, if any.
698 struct GNUNET_CLIENT_TransmitHandle *th;
701 * Deadline for calling @e cb.
703 struct GNUNET_TIME_Absolute test_deadline;
706 * ID of task used for asynchronous operations.
708 struct GNUNET_SCHEDULER_Task * task;
711 * Final result to report back (once known).
718 * Abort testing for service.
720 * @param th test handle
723 GNUNET_CLIENT_service_test_cancel (struct GNUNET_CLIENT_TestHandle *th)
727 GNUNET_CLIENT_notify_transmit_ready_cancel (th->th);
730 if (NULL != th->client)
732 GNUNET_CLIENT_disconnect (th->client);
735 if (NULL != th->task)
737 GNUNET_SCHEDULER_cancel (th->task);
745 * Task that reports back the result by calling the callback
746 * and then cleans up.
748 * @param cls the `struct GNUNET_CLIENT_TestHandle`
749 * @param tc scheduler context
752 report_result (void *cls,
753 const struct GNUNET_SCHEDULER_TaskContext *tc)
755 struct GNUNET_CLIENT_TestHandle *th = cls;
758 th->cb (th->cb_cls, th->result);
759 GNUNET_CLIENT_service_test_cancel (th);
764 * Report service test result asynchronously back to callback.
766 * @param th test handle with the result and the callback
767 * @param result result to report
770 service_test_report (struct GNUNET_CLIENT_TestHandle *th,
774 th->task = GNUNET_SCHEDULER_add_now (&report_result,
780 * Receive confirmation from test, service is up.
782 * @param cls closure with the `struct GNUNET_CLIENT_TestHandle`
783 * @param msg message received, NULL on timeout or fatal error
786 confirm_handler (void *cls,
787 const struct GNUNET_MessageHeader *msg)
789 struct GNUNET_CLIENT_TestHandle *th = cls;
791 /* We may want to consider looking at the reply in more
792 * detail in the future, for example, is this the
793 * correct service? FIXME! */
796 LOG (GNUNET_ERROR_TYPE_DEBUG,
797 "Received confirmation that service is running.\n");
798 service_test_report (th, GNUNET_YES);
802 service_test_report (th, GNUNET_NO);
808 * Send the 'TEST' message to the service. If successful, prepare to
811 * @param cls the `struct GNUNET_CLIENT_TestHandle` of the test
812 * @param size number of bytes available in @a buf
813 * @param buf where to write the message
814 * @return number of bytes written to @a buf
817 write_test (void *cls, size_t size, void *buf)
819 struct GNUNET_CLIENT_TestHandle *th = cls;
820 struct GNUNET_MessageHeader *msg;
823 if (size < sizeof (struct GNUNET_MessageHeader))
825 LOG (GNUNET_ERROR_TYPE_DEBUG,
826 "Failed to transmit TEST request.\n");
827 service_test_report (th, GNUNET_NO);
828 return 0; /* client disconnected */
830 LOG (GNUNET_ERROR_TYPE_DEBUG,
831 "Transmitting `%s' request.\n",
833 msg = (struct GNUNET_MessageHeader *) buf;
834 msg->type = htons (GNUNET_MESSAGE_TYPE_TEST);
835 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
836 GNUNET_CLIENT_receive (th->client,
837 &confirm_handler, th,
838 GNUNET_TIME_absolute_get_remaining
839 (th->test_deadline));
840 return sizeof (struct GNUNET_MessageHeader);
845 * Test if the service is running. If we are given a UNIXPATH or a
846 * local address, we do this NOT by trying to connect to the service,
847 * but by trying to BIND to the same port. If the BIND fails, we know
848 * the service is running.
850 * @param service name of the service to wait for
851 * @param cfg configuration to use
852 * @param timeout how long to wait at most
853 * @param cb function to call with the result
854 * @param cb_cls closure for @a cb
855 * @return handle to cancel the test
857 struct GNUNET_CLIENT_TestHandle *
858 GNUNET_CLIENT_service_test (const char *service,
859 const struct GNUNET_CONFIGURATION_Handle *cfg,
860 struct GNUNET_TIME_Relative timeout,
861 GNUNET_CLIENT_TestResultCallback cb,
864 struct GNUNET_CLIENT_TestHandle *th;
866 unsigned long long port;
867 struct GNUNET_NETWORK_Handle *sock;
869 th = GNUNET_new (struct GNUNET_CLIENT_TestHandle);
872 th->test_deadline = GNUNET_TIME_relative_to_absolute (timeout);
873 LOG (GNUNET_ERROR_TYPE_DEBUG,
874 "Testing if service `%s' is running.\n",
878 /* probe UNIX support */
879 struct sockaddr_un s_un;
885 GNUNET_CONFIGURATION_get_value_filename (cfg,
889 (0 < strlen (unixpath))) /* We have a non-NULL unixpath, does that mean it's valid? */
891 if (strlen (unixpath) >= sizeof (s_un.sun_path))
893 LOG (GNUNET_ERROR_TYPE_WARNING,
894 _("UNIXPATH `%s' too long, maximum length is %llu\n"),
896 (unsigned long long) sizeof (s_un.sun_path));
897 unixpath = GNUNET_NETWORK_shorten_unixpath (unixpath);
898 LOG (GNUNET_ERROR_TYPE_INFO,
899 _("Using `%s' instead\n"), unixpath);
903 abstract = GNUNET_CONFIGURATION_get_value_yesno (cfg,
905 "USE_ABSTRACT_SOCKETS");
907 abstract = GNUNET_NO;
909 if ((NULL != unixpath) && (GNUNET_YES != abstract))
911 if (GNUNET_SYSERR == GNUNET_DISK_directory_create_for_file (unixpath))
912 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
915 if (NULL != unixpath)
917 sock = GNUNET_NETWORK_socket_create (PF_UNIX, SOCK_STREAM, 0);
920 memset (&s_un, 0, sizeof (s_un));
921 s_un.sun_family = AF_UNIX;
922 strncpy (s_un.sun_path, unixpath, sizeof (s_un.sun_path) - 1);
923 if (GNUNET_YES == abstract)
924 s_un.sun_path[0] = '\0';
925 #if HAVE_SOCKADDR_IN_SIN_LEN
926 s_un.sun_len = (u_char) sizeof (struct sockaddr_un);
929 GNUNET_NETWORK_socket_bind (sock, (const struct sockaddr *) &s_un,
930 sizeof (struct sockaddr_un)))
932 /* failed to bind => service must be running */
933 GNUNET_free (unixpath);
934 (void) GNUNET_NETWORK_socket_close (sock);
935 service_test_report (th, GNUNET_YES);
938 (void) GNUNET_NETWORK_socket_close (sock);
942 GNUNET_free_non_null (unixpath);
948 GNUNET_CONFIGURATION_get_value_number (cfg, service, "PORT", &port)) ||
951 GNUNET_CONFIGURATION_get_value_string (cfg, service, "HOSTNAME",
954 /* UNIXPATH failed (if possible) AND IP failed => error */
955 service_test_report (th, GNUNET_SYSERR);
959 if (0 == strcmp ("localhost", hostname)
965 /* can test using 'bind' */
966 struct sockaddr_in s_in;
968 memset (&s_in, 0, sizeof (s_in));
969 #if HAVE_SOCKADDR_IN_SIN_LEN
970 s_in.sin_len = sizeof (struct sockaddr_in);
972 s_in.sin_family = AF_INET;
973 s_in.sin_port = htons (port);
975 sock = GNUNET_NETWORK_socket_create (AF_INET, SOCK_STREAM, 0);
979 GNUNET_NETWORK_socket_bind (sock, (const struct sockaddr *) &s_in,
982 /* failed to bind => service must be running */
983 GNUNET_free (hostname);
984 (void) GNUNET_NETWORK_socket_close (sock);
985 service_test_report (th, GNUNET_YES);
988 (void) GNUNET_NETWORK_socket_close (sock);
992 if (0 == strcmp ("ip6-localhost", hostname)
998 /* can test using 'bind' */
999 struct sockaddr_in6 s_in6;
1001 memset (&s_in6, 0, sizeof (s_in6));
1002 #if HAVE_SOCKADDR_IN_SIN_LEN
1003 s_in6.sin6_len = sizeof (struct sockaddr_in6);
1005 s_in6.sin6_family = AF_INET6;
1006 s_in6.sin6_port = htons (port);
1008 sock = GNUNET_NETWORK_socket_create (AF_INET6, SOCK_STREAM, 0);
1012 GNUNET_NETWORK_socket_bind (sock, (const struct sockaddr *) &s_in6,
1015 /* failed to bind => service must be running */
1016 GNUNET_free (hostname);
1017 (void) GNUNET_NETWORK_socket_close (sock);
1018 service_test_report (th, GNUNET_YES);
1021 (void) GNUNET_NETWORK_socket_close (sock);
1025 if (((0 == strcmp ("localhost", hostname)) ||
1026 (0 == strcmp ("ip6-localhost", hostname)))
1032 /* all binds succeeded => claim service not running right now */
1033 GNUNET_free_non_null (hostname);
1034 service_test_report (th, GNUNET_NO);
1037 GNUNET_free_non_null (hostname);
1039 /* non-localhost, try 'connect' method */
1040 th->client = GNUNET_CLIENT_connect (service, cfg);
1041 if (NULL == th->client)
1043 LOG (GNUNET_ERROR_TYPE_INFO,
1044 _("Could not connect to service `%s', configuration broken.\n"),
1046 service_test_report (th, GNUNET_SYSERR);
1049 th->th = GNUNET_CLIENT_notify_transmit_ready (th->client,
1050 sizeof (struct GNUNET_MessageHeader),
1051 timeout, GNUNET_YES,
1055 LOG (GNUNET_ERROR_TYPE_WARNING,
1056 _("Failure to transmit request to service `%s'\n"), service);
1057 service_test_report (th, GNUNET_SYSERR);
1065 * Connection notifies us about failure or success of
1066 * a transmission request. Either pass it on to our
1067 * user or, if possible, retry.
1069 * @param cls our `struct GNUNET_CLIENT_TransmissionHandle`
1070 * @param size number of bytes available for transmission
1071 * @param buf where to write them
1072 * @return number of bytes written to @a buf
1075 client_notify (void *cls, size_t size, void *buf);
1079 * This task is run if we should re-try connection to the
1080 * service after a while.
1082 * @param cls our `struct GNUNET_CLIENT_TransmitHandle` of the request
1086 client_delayed_retry (void *cls,
1087 const struct GNUNET_SCHEDULER_TaskContext *tc)
1089 struct GNUNET_CLIENT_TransmitHandle *th = cls;
1090 struct GNUNET_TIME_Relative delay;
1092 th->reconnect_task = NULL;
1093 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1095 /* give up, was shutdown */
1096 th->client->th = NULL;
1097 th->notify (th->notify_cls, 0, NULL);
1101 th->client->connection =
1102 do_connect (th->client->service_name,
1104 th->client->attempts++);
1105 th->client->first_message = GNUNET_YES;
1106 if (NULL == th->client->connection)
1108 /* could happen if we're out of sockets */
1109 delay = GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining (th->timeout),
1110 th->client->back_off);
1111 th->client->back_off = GNUNET_TIME_STD_BACKOFF (th->client->back_off);
1112 LOG (GNUNET_ERROR_TYPE_DEBUG,
1113 "Transmission failed %u times, trying again in %s.\n",
1114 MAX_ATTEMPTS - th->attempts_left,
1115 GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_YES));
1116 GNUNET_assert (NULL == th->th);
1117 GNUNET_assert (NULL == th->reconnect_task);
1118 th->reconnect_task =
1119 GNUNET_SCHEDULER_add_delayed (delay, &client_delayed_retry, th);
1123 GNUNET_CONNECTION_notify_transmit_ready (th->client->connection, th->size,
1124 GNUNET_TIME_absolute_get_remaining
1125 (th->timeout), &client_notify,
1130 th->client->th = NULL;
1131 th->notify (th->notify_cls, 0, NULL);
1139 * Connection notifies us about failure or success of a transmission
1140 * request. Either pass it on to our user or, if possible, retry.
1142 * @param cls our `struct GNUNET_CLIENT_TransmissionHandle`
1143 * @param size number of bytes available for transmission
1144 * @param buf where to write them
1145 * @return number of bytes written to @a buf
1148 client_notify (void *cls,
1152 struct GNUNET_CLIENT_TransmitHandle *th = cls;
1153 struct GNUNET_CLIENT_Connection *client = th->client;
1155 struct GNUNET_TIME_Relative delay;
1157 LOG (GNUNET_ERROR_TYPE_DEBUG,
1158 "client_notify is running\n");
1163 delay = GNUNET_TIME_absolute_get_remaining (th->timeout);
1164 delay.rel_value_us /= 2;
1165 if ( (GNUNET_YES != th->auto_retry) ||
1166 (0 == --th->attempts_left) ||
1167 (delay.rel_value_us < 1)||
1168 (0 != (GNUNET_SCHEDULER_get_reason() & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
1170 LOG (GNUNET_ERROR_TYPE_DEBUG,
1171 "Transmission failed %u times, giving up.\n",
1172 MAX_ATTEMPTS - th->attempts_left);
1174 th->notify (th->notify_cls, 0, NULL));
1179 LOG (GNUNET_ERROR_TYPE_DEBUG,
1180 "Failed to connect to `%s', automatically trying again.\n",
1181 client->service_name);
1182 if (GNUNET_YES == client->in_receive)
1184 GNUNET_CONNECTION_receive_cancel (client->connection);
1185 client->in_receive = GNUNET_NO;
1187 GNUNET_CONNECTION_destroy (client->connection);
1188 client->connection = NULL;
1189 delay = GNUNET_TIME_relative_min (delay, client->back_off);
1191 GNUNET_TIME_relative_min (GNUNET_TIME_relative_multiply
1192 (client->back_off, 2),
1193 GNUNET_TIME_UNIT_SECONDS);
1194 LOG (GNUNET_ERROR_TYPE_DEBUG,
1195 "Transmission failed %u times, trying again in %s.\n",
1196 MAX_ATTEMPTS - th->attempts_left,
1197 GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_YES));
1199 GNUNET_assert (NULL == th->reconnect_task);
1200 GNUNET_assert (NULL == th->th);
1201 th->reconnect_task =
1202 GNUNET_SCHEDULER_add_delayed (delay, &client_delayed_retry, th);
1205 GNUNET_assert (size >= th->size);
1206 ret = th->notify (th->notify_cls, size, buf);
1208 if (sizeof (struct GNUNET_MessageHeader) <= ret)
1210 LOG (GNUNET_ERROR_TYPE_DEBUG,
1211 "Transmitting message of type %u and size %u to %s service.\n",
1212 ntohs (((struct GNUNET_MessageHeader *) buf)->type),
1213 ntohs (((struct GNUNET_MessageHeader *) buf)->size),
1214 client->service_name);
1221 * Ask the client to call us once the specified number of bytes
1222 * are free in the transmission buffer. Will never call the @a notify
1223 * callback in this task, but always first go into the scheduler.
1225 * @param client connection to the service
1226 * @param size number of bytes to send
1227 * @param timeout after how long should we give up (and call
1228 * notify with buf NULL and size 0)?
1229 * @param auto_retry if the connection to the service dies, should we
1230 * automatically re-connect and retry (within the timeout period)
1231 * or should we immediately fail in this case? Pass GNUNET_YES
1232 * if the caller does not care about temporary connection errors,
1233 * for example because the protocol is stateless
1234 * @param notify function to call
1235 * @param notify_cls closure for @a notify
1236 * @return NULL if our buffer will never hold size bytes,
1237 * a handle if the notify callback was queued (can be used to cancel)
1239 struct GNUNET_CLIENT_TransmitHandle *
1240 GNUNET_CLIENT_notify_transmit_ready (struct GNUNET_CLIENT_Connection *client,
1242 struct GNUNET_TIME_Relative timeout,
1244 GNUNET_CONNECTION_TransmitReadyNotify notify,
1247 struct GNUNET_CLIENT_TransmitHandle *th;
1249 if (NULL != client->th)
1251 /* If this breaks, you most likley called this function twice without waiting
1252 * for completion or canceling the request */
1256 th = GNUNET_new (struct GNUNET_CLIENT_TransmitHandle);
1257 th->client = client;
1259 th->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1260 /* always auto-retry on first message to service */
1261 th->auto_retry = (GNUNET_YES == client->first_message) ? GNUNET_YES : auto_retry;
1262 client->first_message = GNUNET_NO;
1263 th->notify = notify;
1264 th->notify_cls = notify_cls;
1265 th->attempts_left = MAX_ATTEMPTS;
1267 if (NULL == client->connection)
1269 GNUNET_assert (NULL == th->th);
1270 GNUNET_assert (NULL == th->reconnect_task);
1271 th->reconnect_task =
1272 GNUNET_SCHEDULER_add_delayed (client->back_off,
1273 &client_delayed_retry,
1278 th->th = GNUNET_CONNECTION_notify_transmit_ready (client->connection,
1296 * Cancel a request for notification.
1298 * @param th handle from the original request.
1301 GNUNET_CLIENT_notify_transmit_ready_cancel (struct GNUNET_CLIENT_TransmitHandle *th)
1303 if (NULL != th->reconnect_task)
1305 GNUNET_assert (NULL == th->th);
1306 GNUNET_SCHEDULER_cancel (th->reconnect_task);
1307 th->reconnect_task = NULL;
1311 GNUNET_assert (NULL != th->th);
1312 GNUNET_CONNECTION_notify_transmit_ready_cancel (th->th);
1314 th->client->th = NULL;
1320 * Function called to notify a client about the socket
1321 * begin ready to queue the message. @a buf will be
1322 * NULL and @a size zero if the socket was closed for
1323 * writing in the meantime.
1325 * @param cls closure of type `struct TransmitGetResponseContext *`
1326 * @param size number of bytes available in @a buf
1327 * @param buf where the callee should write the message
1328 * @return number of bytes written to @a buf
1331 transmit_for_response (void *cls,
1335 struct TransmitGetResponseContext *tc = cls;
1338 tc->client->tag = NULL;
1339 msize = ntohs (tc->hdr->size);
1342 LOG (GNUNET_ERROR_TYPE_DEBUG,
1343 "Could not submit request, not expecting to receive a response.\n");
1345 tc->rn (tc->rn_cls, NULL);
1349 GNUNET_assert (size >= msize);
1350 memcpy (buf, tc->hdr, msize);
1351 GNUNET_CLIENT_receive (tc->client,
1354 GNUNET_TIME_absolute_get_remaining (tc->timeout));
1361 * Convenience API that combines sending a request
1362 * to the service and waiting for a response.
1363 * If either operation times out, the callback
1364 * will be called with a "NULL" response (in which
1365 * case the connection should probably be destroyed).
1367 * @param client connection to use
1368 * @param hdr message to transmit
1369 * @param timeout when to give up (for both transmission
1370 * and for waiting for a response)
1371 * @param auto_retry if the connection to the service dies, should we
1372 * automatically re-connect and retry (within the timeout period)
1373 * or should we immediately fail in this case? Pass GNUNET_YES
1374 * if the caller does not care about temporary connection errors,
1375 * for example because the protocol is stateless
1376 * @param rn function to call with the response
1377 * @param rn_cls closure for @a rn
1378 * @return #GNUNET_OK on success, #GNUNET_SYSERR if a request
1379 * is already pending
1382 GNUNET_CLIENT_transmit_and_get_response (struct GNUNET_CLIENT_Connection *client,
1383 const struct GNUNET_MessageHeader *hdr,
1384 struct GNUNET_TIME_Relative timeout,
1386 GNUNET_CLIENT_MessageHandler rn,
1389 struct TransmitGetResponseContext *tc;
1392 if (NULL != client->th)
1393 return GNUNET_SYSERR;
1394 GNUNET_assert (NULL == client->tag);
1395 msize = ntohs (hdr->size);
1396 tc = GNUNET_malloc (sizeof (struct TransmitGetResponseContext) + msize);
1397 tc->client = client;
1398 tc->hdr = (const struct GNUNET_MessageHeader *) &tc[1];
1399 memcpy (&tc[1], hdr, msize);
1400 tc->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1402 tc->rn_cls = rn_cls;
1404 GNUNET_CLIENT_notify_transmit_ready (client, msize, timeout, auto_retry,
1405 &transmit_for_response, tc))
1409 return GNUNET_SYSERR;
1416 /* end of client.c */