2 This file is part of GNUnet.
3 (C) 2001-2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
23 * @brief code for access to services
24 * @author Christian Grothoff
26 * Generic TCP code for reliable, record-oriented TCP
27 * connections between clients and service providers.
30 #include "gnunet_protocols.h"
31 #include "gnunet_util_lib.h"
35 * How often do we re-try tranmsitting requests before giving up?
36 * Note that if we succeeded transmitting a request but failed to read
37 * a response, we do NOT re-try.
39 #define MAX_ATTEMPTS 50
41 #define LOG(kind,...) GNUNET_log_from (kind, "util",__VA_ARGS__)
44 * Handle for a transmission request.
46 struct GNUNET_CLIENT_TransmitHandle
51 struct GNUNET_CLIENT_Connection *client;
54 * Function to call to get the data for transmission.
56 GNUNET_CONNECTION_TransmitReadyNotify notify;
64 * Handle to the transmission with the underlying
67 struct GNUNET_CONNECTION_TransmitHandle *th;
70 * If we are re-trying and are delaying to do so,
71 * handle to the scheduled task managing the delay.
73 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
76 * Timeout for the operation overall.
78 struct GNUNET_TIME_Absolute timeout;
81 * Number of bytes requested.
86 * Are we allowed to re-try to connect without telling
87 * the user (of this API) about the connection troubles?
92 * Number of attempts left for transmitting the request. We may
93 * fail the first time (say because the service is not yet up), in
94 * which case (if auto_retry is set) we wait a bit and re-try
95 * (timeout permitting).
97 unsigned int attempts_left;
103 * Context for processing
104 * "GNUNET_CLIENT_transmit_and_get_response" requests.
106 struct TransmitGetResponseContext
111 struct GNUNET_CLIENT_Connection *client;
114 * Message to transmit; do not free, allocated
115 * right after this struct.
117 const struct GNUNET_MessageHeader *hdr;
122 struct GNUNET_TIME_Absolute timeout;
125 * Function to call when done.
127 GNUNET_CLIENT_MessageHandler rn;
136 * Struct to refer to a GNUnet TCP connection.
137 * This is more than just a socket because if the server
138 * drops the connection, the client automatically tries
139 * to reconnect (and for that needs connection information).
141 struct GNUNET_CLIENT_Connection
145 * The connection handle, NULL if not live
147 struct GNUNET_CONNECTION_Handle *connection;
152 const struct GNUNET_CONFIGURATION_Handle *cfg;
155 * Name of the service we interact with.
160 * Context of a transmit_and_get_response operation, NULL
161 * if no such operation is pending.
163 struct TransmitGetResponseContext *tag;
166 * Handler for current receiver task.
168 GNUNET_CLIENT_MessageHandler receiver_handler;
171 * Closure for receiver_handler.
173 void *receiver_handler_cls;
176 * Handle for a pending transmission request, NULL if there is
179 struct GNUNET_CLIENT_TransmitHandle *th;
182 * If we are re-trying and are delaying to do so,
183 * handle to the scheduled task managing the delay.
185 GNUNET_SCHEDULER_TaskIdentifier receive_task;
188 * Buffer for received message.
193 * Timeout for receiving a response (absolute time).
195 struct GNUNET_TIME_Absolute receive_timeout;
198 * Current value for our incremental back-off (for
201 struct GNUNET_TIME_Relative back_off;
204 * Number of bytes in received_buf that are valid.
209 * Size of received_buf.
211 unsigned int received_size;
214 * Do we have a complete response in received_buf?
219 * Are we currently busy doing receive-processing?
220 * GNUNET_YES if so, GNUNET_NO if not.
225 * Is this the first message we are sending to the service?
230 * How often have we tried to connect?
232 unsigned int attempts;
238 * Try connecting to the server using UNIX domain sockets.
240 * @param service_name name of service to connect to
241 * @param cfg configuration to use
242 * @return NULL on error, connection to UNIX otherwise
244 static struct GNUNET_CONNECTION_Handle *
245 try_unixpath (const char *service_name,
246 const struct GNUNET_CONFIGURATION_Handle *cfg)
249 struct GNUNET_CONNECTION_Handle *connection;
251 struct sockaddr_un s_un;
254 if ((GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg, service_name, "UNIXPATH", &unixpath)) &&
255 (0 < strlen (unixpath)))
257 /* We have a non-NULL unixpath, need to validate it */
258 if (strlen (unixpath) >= sizeof (s_un.sun_path))
260 LOG (GNUNET_ERROR_TYPE_WARNING,
261 _("UNIXPATH `%s' too long, maximum length is %llu\n"), unixpath,
262 (unsigned long long) sizeof (s_un.sun_path));
263 unixpath = GNUNET_NETWORK_shorten_unixpath (unixpath);
264 LOG (GNUNET_ERROR_TYPE_INFO,
265 _("Using `%s' instead\n"), unixpath);
266 if (NULL == unixpath)
269 connection = GNUNET_CONNECTION_create_from_connect_to_unixpath (cfg, unixpath);
270 if (NULL != connection)
272 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connected to unixpath `%s'!\n",
274 GNUNET_free (unixpath);
278 GNUNET_free_non_null (unixpath);
285 * Test whether the configuration has proper values for connection
286 * (UNIXPATH || (PORT && HOSTNAME)).
288 * @param service_name name of service to connect to
289 * @param cfg configuration to use
290 * @return GNUNET_OK if the configuration is valid, GNUNET_SYSERR if not
293 test_service_configuration (const char *service_name,
294 const struct GNUNET_CONFIGURATION_Handle *cfg)
296 int ret = GNUNET_SYSERR;
297 char *hostname = NULL;
298 unsigned long long port;
300 char *unixpath = NULL;
302 if ((GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg, service_name, "UNIXPATH", &unixpath)) &&
303 (0 < strlen (unixpath)))
305 GNUNET_free_non_null (unixpath);
309 GNUNET_CONFIGURATION_have_value (cfg, service_name, "PORT")) &&
311 GNUNET_CONFIGURATION_get_value_number (cfg, service_name, "PORT", &port)) &&
312 (port <= 65535) && (0 != port) &&
314 GNUNET_CONFIGURATION_get_value_string (cfg, service_name, "HOSTNAME",
316 (0 != strlen (hostname)) )
318 GNUNET_free_non_null (hostname);
324 * Try to connect to the service.
326 * @param service_name name of service to connect to
327 * @param cfg configuration to use
328 * @param attempt counter used to alternate between IP and UNIX domain sockets
329 * @return NULL on error
331 static struct GNUNET_CONNECTION_Handle *
332 do_connect (const char *service_name,
333 const struct GNUNET_CONFIGURATION_Handle *cfg, unsigned int attempt)
335 struct GNUNET_CONNECTION_Handle *connection;
337 unsigned long long port;
340 if (0 == (attempt % 2))
342 /* on even rounds, try UNIX first */
343 connection = try_unixpath (service_name, cfg);
344 if (NULL != connection)
348 GNUNET_CONFIGURATION_have_value (cfg, service_name, "PORT"))
351 GNUNET_CONFIGURATION_get_value_number (cfg, service_name, "PORT", &port))
354 GNUNET_CONFIGURATION_get_value_string (cfg, service_name, "HOSTNAME",
357 LOG (GNUNET_ERROR_TYPE_WARNING,
359 ("Could not determine valid hostname and port for service `%s' from configuration.\n"),
363 if (0 == strlen (hostname))
365 GNUNET_free (hostname);
366 LOG (GNUNET_ERROR_TYPE_WARNING,
367 _("Need a non-empty hostname for service `%s'.\n"), service_name);
373 /* unspecified means 0 (disabled) */
379 /* if port is 0, try UNIX */
380 connection = try_unixpath (service_name, cfg);
381 if (NULL != connection)
383 LOG (GNUNET_ERROR_TYPE_DEBUG,
384 "Port is 0 for service `%s', UNIXPATH did not work, returning NULL!\n",
386 GNUNET_free_non_null (hostname);
389 connection = GNUNET_CONNECTION_create_from_connect (cfg, hostname, port);
390 GNUNET_free (hostname);
396 * Get a connection with a service.
398 * @param service_name name of the service
399 * @param cfg configuration to use
400 * @return NULL on error (service unknown to configuration)
402 struct GNUNET_CLIENT_Connection *
403 GNUNET_CLIENT_connect (const char *service_name,
404 const struct GNUNET_CONFIGURATION_Handle *cfg)
406 struct GNUNET_CLIENT_Connection *client;
407 struct GNUNET_CONNECTION_Handle *connection;
410 test_service_configuration (service_name,
413 connection = do_connect (service_name, cfg, 0);
414 client = GNUNET_new (struct GNUNET_CLIENT_Connection);
415 client->first_message = GNUNET_YES;
416 client->attempts = 1;
417 client->connection = connection;
418 client->service_name = GNUNET_strdup (service_name);
420 client->back_off = GNUNET_TIME_UNIT_MILLISECONDS;
426 * Destroy connection with the service. This will automatically
427 * cancel any pending "receive" request (however, the handler will
428 * *NOT* be called, not even with a NULL message). Any pending
429 * transmission request will also be cancelled UNLESS the callback for
430 * the transmission request has already been called, in which case the
431 * transmission 'finish_pending_write' argument determines whether or
432 * not the write is guaranteed to complete before the socket is fully
433 * destroyed (unless, of course, there is an error with the server in
434 * which case the message may still be lost).
436 * @param client handle to the service connection
439 GNUNET_CLIENT_disconnect (struct GNUNET_CLIENT_Connection *client)
441 if (GNUNET_YES == client->in_receive)
443 GNUNET_CONNECTION_receive_cancel (client->connection);
444 client->in_receive = GNUNET_NO;
446 if (NULL != client->th)
448 GNUNET_CLIENT_notify_transmit_ready_cancel (client->th);
451 if (NULL != client->connection)
453 GNUNET_CONNECTION_destroy (client->connection);
454 client->connection = NULL;
456 if (GNUNET_SCHEDULER_NO_TASK != client->receive_task)
458 GNUNET_SCHEDULER_cancel (client->receive_task);
459 client->receive_task = GNUNET_SCHEDULER_NO_TASK;
461 if (NULL != client->tag)
463 GNUNET_free (client->tag);
466 client->receiver_handler = NULL;
467 GNUNET_array_grow (client->received_buf, client->received_size, 0);
468 GNUNET_free (client->service_name);
469 GNUNET_free (client);
474 * Check if message is complete. Sets the "msg_complete" member
475 * in the client struct.
477 * @param client connection with the buffer to check
480 check_complete (struct GNUNET_CLIENT_Connection *client)
482 if ((client->received_pos >= sizeof (struct GNUNET_MessageHeader)) &&
483 (client->received_pos >=
484 ntohs (((const struct GNUNET_MessageHeader *) client->received_buf)->
486 client->msg_complete = GNUNET_YES;
491 * Callback function for data received from the network. Note that
492 * both "available" and "errCode" would be 0 if the read simply timed out.
495 * @param buf pointer to received data
496 * @param available number of bytes availabe in "buf",
497 * possibly 0 (on errors)
498 * @param addr address of the sender
499 * @param addrlen size of addr
500 * @param errCode value of errno (on errors receiving)
503 receive_helper (void *cls, const void *buf, size_t available,
504 const struct sockaddr *addr, socklen_t addrlen, int errCode)
506 struct GNUNET_CLIENT_Connection *client = cls;
507 struct GNUNET_TIME_Relative remaining;
508 GNUNET_CLIENT_MessageHandler receive_handler;
509 void *receive_handler_cls;
511 GNUNET_assert (GNUNET_NO == client->msg_complete);
512 GNUNET_assert (GNUNET_YES == client->in_receive);
513 client->in_receive = GNUNET_NO;
514 if ((0 == available) || (NULL == client->connection) || (0 != errCode))
516 /* signal timeout! */
517 LOG (GNUNET_ERROR_TYPE_DEBUG,
518 "Timeout in receive_helper, available %u, client->connection %s, errCode `%s'\n",
519 (unsigned int) available, NULL == client->connection ? "NULL" : "non-NULL",
521 if (NULL != (receive_handler = client->receiver_handler))
523 receive_handler_cls = client->receiver_handler_cls;
524 client->receiver_handler = NULL;
525 receive_handler (receive_handler_cls, NULL);
529 /* FIXME: optimize for common fast case where buf contains the
530 * entire message and we need no copying... */
532 /* slow path: append to array */
533 if (client->received_size < client->received_pos + available)
534 GNUNET_array_grow (client->received_buf, client->received_size,
535 client->received_pos + available);
536 memcpy (&client->received_buf[client->received_pos], buf, available);
537 client->received_pos += available;
538 check_complete (client);
539 /* check for timeout */
540 remaining = GNUNET_TIME_absolute_get_remaining (client->receive_timeout);
541 if (0 == remaining.rel_value_us)
543 /* signal timeout! */
544 if (NULL != client->receiver_handler)
545 client->receiver_handler (client->receiver_handler_cls, NULL);
548 /* back to receive -- either for more data or to call callback! */
549 GNUNET_CLIENT_receive (client, client->receiver_handler,
550 client->receiver_handler_cls, remaining);
555 * Continuation to call the receive callback.
557 * @param cls our handle to the client connection
558 * @param tc scheduler context
561 receive_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
563 struct GNUNET_CLIENT_Connection *client = cls;
564 GNUNET_CLIENT_MessageHandler handler = client->receiver_handler;
565 const struct GNUNET_MessageHeader *cmsg =
566 (const struct GNUNET_MessageHeader *) client->received_buf;
567 void *handler_cls = client->receiver_handler_cls;
568 uint16_t msize = ntohs (cmsg->size);
570 struct GNUNET_MessageHeader *msg = (struct GNUNET_MessageHeader *) mbuf;
572 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received message of type %u and size %u\n",
573 ntohs (cmsg->type), msize);
574 client->receive_task = GNUNET_SCHEDULER_NO_TASK;
575 GNUNET_assert (GNUNET_YES == client->msg_complete);
576 GNUNET_assert (client->received_pos >= msize);
577 memcpy (msg, cmsg, msize);
578 memmove (client->received_buf, &client->received_buf[msize],
579 client->received_pos - msize);
580 client->received_pos -= msize;
581 client->msg_complete = GNUNET_NO;
582 client->receiver_handler = NULL;
583 check_complete (client);
585 handler (handler_cls, msg);
590 * Read from the service.
592 * @param client the service
593 * @param handler function to call with the message
594 * @param handler_cls closure for handler
595 * @param timeout how long to wait until timing out
598 GNUNET_CLIENT_receive (struct GNUNET_CLIENT_Connection *client,
599 GNUNET_CLIENT_MessageHandler handler, void *handler_cls,
600 struct GNUNET_TIME_Relative timeout)
602 if (NULL == client->connection)
604 /* already disconnected, fail instantly! */
605 GNUNET_break (0); /* this should not happen in well-written code! */
607 handler (handler_cls, NULL);
610 client->receiver_handler = handler;
611 client->receiver_handler_cls = handler_cls;
612 client->receive_timeout = GNUNET_TIME_relative_to_absolute (timeout);
613 if (GNUNET_YES == client->msg_complete)
615 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == client->receive_task);
616 client->receive_task = GNUNET_SCHEDULER_add_now (&receive_task, client);
620 LOG (GNUNET_ERROR_TYPE_DEBUG, "calling GNUNET_CONNECTION_receive\n");
621 GNUNET_assert (GNUNET_NO == client->in_receive);
622 client->in_receive = GNUNET_YES;
623 GNUNET_CONNECTION_receive (client->connection, GNUNET_SERVER_MAX_MESSAGE_SIZE - 1,
624 timeout, &receive_helper, client);
630 * Handle for a test to check if a service is running.
632 struct GNUNET_CLIENT_TestHandle
635 * Function to call with the result of the test.
637 GNUNET_CLIENT_TestResultCallback cb;
645 * Client connection we are using for the test, if any.
647 struct GNUNET_CLIENT_Connection *client;
650 * Handle for the transmission request, if any.
652 struct GNUNET_CLIENT_TransmitHandle *th;
655 * Deadline for calling 'cb'.
657 struct GNUNET_TIME_Absolute test_deadline;
660 * ID of task used for asynchronous operations.
662 GNUNET_SCHEDULER_TaskIdentifier task;
665 * Final result to report back (once known).
672 * Abort testing for service.
674 * @param th test handle
677 GNUNET_CLIENT_service_test_cancel (struct GNUNET_CLIENT_TestHandle *th)
681 GNUNET_CLIENT_notify_transmit_ready_cancel (th->th);
684 if (NULL != th->client)
686 GNUNET_CLIENT_disconnect (th->client);
689 if (GNUNET_SCHEDULER_NO_TASK != th->task)
691 GNUNET_SCHEDULER_cancel (th->task);
692 th->task = GNUNET_SCHEDULER_NO_TASK;
699 * Task that reports back the result by calling the callback
700 * and then cleans up.
702 * @param cls the 'struct GNUNET_CLIENT_TestHandle'
703 * @param tc scheduler context
706 report_result (void *cls,
707 const struct GNUNET_SCHEDULER_TaskContext *tc)
709 struct GNUNET_CLIENT_TestHandle *th = cls;
711 th->task = GNUNET_SCHEDULER_NO_TASK;
712 th->cb (th->cb_cls, th->result);
713 GNUNET_CLIENT_service_test_cancel (th);
718 * Report service test result asynchronously back to callback.
720 * @param th test handle with the result and the callback
721 * @param result result to report
724 service_test_report (struct GNUNET_CLIENT_TestHandle *th,
728 th->task = GNUNET_SCHEDULER_add_now (&report_result,
734 * Receive confirmation from test, service is up.
736 * @param cls closure with the 'struct GNUNET_CLIENT_TestHandle'
737 * @param msg message received, NULL on timeout or fatal error
740 confirm_handler (void *cls, const struct GNUNET_MessageHeader *msg)
742 struct GNUNET_CLIENT_TestHandle *th = cls;
744 /* We may want to consider looking at the reply in more
745 * detail in the future, for example, is this the
746 * correct service? FIXME! */
749 LOG (GNUNET_ERROR_TYPE_DEBUG,
750 "Received confirmation that service is running.\n");
751 service_test_report (th, GNUNET_YES);
755 service_test_report (th, GNUNET_NO);
761 * Send the 'TEST' message to the service. If successful, prepare to
764 * @param cls the 'struct GNUNET_CLIENT_TestHandle' of the test
765 * @param size number of bytes available in buf
766 * @param buf where to write the message
767 * @return number of bytes written to buf
770 write_test (void *cls, size_t size, void *buf)
772 struct GNUNET_CLIENT_TestHandle *th = cls;
773 struct GNUNET_MessageHeader *msg;
776 if (size < sizeof (struct GNUNET_MessageHeader))
778 LOG (GNUNET_ERROR_TYPE_DEBUG,
779 "Failed to transmit TEST request.\n");
780 service_test_report (th, GNUNET_NO);
781 return 0; /* client disconnected */
783 LOG (GNUNET_ERROR_TYPE_DEBUG,
784 "Transmitting `%s' request.\n",
786 msg = (struct GNUNET_MessageHeader *) buf;
787 msg->type = htons (GNUNET_MESSAGE_TYPE_TEST);
788 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
789 GNUNET_CLIENT_receive (th->client,
790 &confirm_handler, th,
791 GNUNET_TIME_absolute_get_remaining
792 (th->test_deadline));
793 return sizeof (struct GNUNET_MessageHeader);
798 * Test if the service is running. If we are given a UNIXPATH or a
799 * local address, we do this NOT by trying to connect to the service,
800 * but by trying to BIND to the same port. If the BIND fails, we know
801 * the service is running.
803 * @param service name of the service to wait for
804 * @param cfg configuration to use
805 * @param timeout how long to wait at most
806 * @param cb function to call with the result
807 * @param cb_cls closure for 'cb'
808 * @return handle to cancel the test
810 struct GNUNET_CLIENT_TestHandle *
811 GNUNET_CLIENT_service_test (const char *service,
812 const struct GNUNET_CONFIGURATION_Handle *cfg,
813 struct GNUNET_TIME_Relative timeout,
814 GNUNET_CLIENT_TestResultCallback cb, void *cb_cls)
816 struct GNUNET_CLIENT_TestHandle *th;
818 unsigned long long port;
819 struct GNUNET_NETWORK_Handle *sock;
821 th = GNUNET_new (struct GNUNET_CLIENT_TestHandle);
824 th->test_deadline = GNUNET_TIME_relative_to_absolute (timeout);
825 LOG (GNUNET_ERROR_TYPE_DEBUG,
826 "Testing if service `%s' is running.\n",
830 /* probe UNIX support */
831 struct sockaddr_un s_un;
836 if ((GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg, service, "UNIXPATH", &unixpath)) && (0 < strlen (unixpath))) /* We have a non-NULL unixpath, does that mean it's valid? */
838 if (strlen (unixpath) >= sizeof (s_un.sun_path))
840 LOG (GNUNET_ERROR_TYPE_WARNING,
841 _("UNIXPATH `%s' too long, maximum length is %llu\n"), unixpath,
842 (unsigned long long) sizeof (s_un.sun_path));
843 unixpath = GNUNET_NETWORK_shorten_unixpath (unixpath);
844 LOG (GNUNET_ERROR_TYPE_INFO,
845 _("Using `%s' instead\n"), unixpath);
848 if (NULL != unixpath)
850 sock = GNUNET_NETWORK_socket_create (PF_UNIX, SOCK_STREAM, 0);
853 memset (&s_un, 0, sizeof (s_un));
854 s_un.sun_family = AF_UNIX;
855 slen = strlen (unixpath) + 1;
856 if (slen >= sizeof (s_un.sun_path))
857 slen = sizeof (s_un.sun_path) - 1;
858 memcpy (s_un.sun_path, unixpath, slen);
859 s_un.sun_path[slen] = '\0';
860 slen = sizeof (struct sockaddr_un);
862 s_un.sun_path[0] = '\0';
864 #if HAVE_SOCKADDR_IN_SIN_LEN
865 s_un.sun_len = (u_char) slen;
868 GNUNET_NETWORK_socket_bind (sock, (const struct sockaddr *) &s_un,
869 slen, GNUNET_BIND_EXCLUSIVE))
871 /* failed to bind => service must be running */
872 GNUNET_free (unixpath);
873 (void) GNUNET_NETWORK_socket_close (sock);
874 service_test_report (th, GNUNET_YES);
877 (void) GNUNET_NETWORK_socket_close (sock);
881 GNUNET_free_non_null (unixpath);
887 GNUNET_CONFIGURATION_get_value_number (cfg, service, "PORT", &port)) ||
890 GNUNET_CONFIGURATION_get_value_string (cfg, service, "HOSTNAME",
893 /* UNIXPATH failed (if possible) AND IP failed => error */
894 service_test_report (th, GNUNET_SYSERR);
898 if (0 == strcmp ("localhost", hostname)
904 /* can test using 'bind' */
905 struct sockaddr_in s_in;
907 memset (&s_in, 0, sizeof (s_in));
908 #if HAVE_SOCKADDR_IN_SIN_LEN
909 s_in.sin_len = sizeof (struct sockaddr_in);
911 s_in.sin_family = AF_INET;
912 s_in.sin_port = htons (port);
914 sock = GNUNET_NETWORK_socket_create (AF_INET, SOCK_STREAM, 0);
918 GNUNET_NETWORK_socket_bind (sock, (const struct sockaddr *) &s_in,
919 sizeof (s_in), GNUNET_BIND_EXCLUSIVE))
921 /* failed to bind => service must be running */
922 GNUNET_free (hostname);
923 (void) GNUNET_NETWORK_socket_close (sock);
924 service_test_report (th, GNUNET_YES);
927 (void) GNUNET_NETWORK_socket_close (sock);
931 if (0 == strcmp ("ip6-localhost", hostname)
937 /* can test using 'bind' */
938 struct sockaddr_in6 s_in6;
940 memset (&s_in6, 0, sizeof (s_in6));
941 #if HAVE_SOCKADDR_IN_SIN_LEN
942 s_in6.sin6_len = sizeof (struct sockaddr_in6);
944 s_in6.sin6_family = AF_INET6;
945 s_in6.sin6_port = htons (port);
947 sock = GNUNET_NETWORK_socket_create (AF_INET6, SOCK_STREAM, 0);
951 GNUNET_NETWORK_socket_bind (sock, (const struct sockaddr *) &s_in6,
952 sizeof (s_in6), GNUNET_BIND_EXCLUSIVE))
954 /* failed to bind => service must be running */
955 GNUNET_free (hostname);
956 (void) GNUNET_NETWORK_socket_close (sock);
957 service_test_report (th, GNUNET_YES);
960 (void) GNUNET_NETWORK_socket_close (sock);
964 if (((0 == strcmp ("localhost", hostname)) ||
965 (0 == strcmp ("ip6-localhost", hostname)))
971 /* all binds succeeded => claim service not running right now */
972 GNUNET_free_non_null (hostname);
973 service_test_report (th, GNUNET_NO);
976 GNUNET_free_non_null (hostname);
978 /* non-localhost, try 'connect' method */
979 th->client = GNUNET_CLIENT_connect (service, cfg);
980 if (NULL == th->client)
982 LOG (GNUNET_ERROR_TYPE_INFO,
983 _("Could not connect to service `%s', configuration broken.\n"),
985 service_test_report (th, GNUNET_SYSERR);
988 th->th = GNUNET_CLIENT_notify_transmit_ready (th->client,
989 sizeof (struct GNUNET_MessageHeader),
994 LOG (GNUNET_ERROR_TYPE_WARNING,
995 _("Failure to transmit request to service `%s'\n"), service);
996 service_test_report (th, GNUNET_SYSERR);
1004 * Connection notifies us about failure or success of
1005 * a transmission request. Either pass it on to our
1006 * user or, if possible, retry.
1008 * @param cls our "struct GNUNET_CLIENT_TransmissionHandle"
1009 * @param size number of bytes available for transmission
1010 * @param buf where to write them
1011 * @return number of bytes written to buf
1014 client_notify (void *cls, size_t size, void *buf);
1018 * This task is run if we should re-try connection to the
1019 * service after a while.
1021 * @param cls our "struct GNUNET_CLIENT_TransmitHandle" of the request
1025 client_delayed_retry (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1027 struct GNUNET_CLIENT_TransmitHandle *th = cls;
1028 struct GNUNET_TIME_Relative delay;
1030 th->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1031 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1033 /* give up, was shutdown */
1034 th->client->th = NULL;
1035 th->notify (th->notify_cls, 0, NULL);
1039 th->client->connection =
1040 do_connect (th->client->service_name, th->client->cfg, th->client->attempts++);
1041 th->client->first_message = GNUNET_YES;
1042 if (NULL == th->client->connection)
1044 /* could happen if we're out of sockets */
1046 GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining
1047 (th->timeout), th->client->back_off);
1048 th->client->back_off =
1049 GNUNET_TIME_relative_min (GNUNET_TIME_relative_multiply
1050 (th->client->back_off, 2),
1051 GNUNET_TIME_UNIT_SECONDS);
1052 LOG (GNUNET_ERROR_TYPE_DEBUG,
1053 "Transmission failed %u times, trying again in %s.\n",
1054 MAX_ATTEMPTS - th->attempts_left,
1055 GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_YES));
1056 th->reconnect_task =
1057 GNUNET_SCHEDULER_add_delayed (delay, &client_delayed_retry, th);
1061 GNUNET_CONNECTION_notify_transmit_ready (th->client->connection, th->size,
1062 GNUNET_TIME_absolute_get_remaining
1063 (th->timeout), &client_notify,
1068 th->client->th = NULL;
1069 th->notify (th->notify_cls, 0, NULL);
1077 * Connection notifies us about failure or success of a transmission
1078 * request. Either pass it on to our user or, if possible, retry.
1080 * @param cls our "struct GNUNET_CLIENT_TransmissionHandle"
1081 * @param size number of bytes available for transmission
1082 * @param buf where to write them
1083 * @return number of bytes written to buf
1086 client_notify (void *cls, size_t size, void *buf)
1088 struct GNUNET_CLIENT_TransmitHandle *th = cls;
1089 struct GNUNET_CLIENT_Connection *client = th->client;
1091 struct GNUNET_TIME_Relative delay;
1093 LOG (GNUNET_ERROR_TYPE_DEBUG, "client_notify is running\n");
1098 delay = GNUNET_TIME_absolute_get_remaining (th->timeout);
1099 delay.rel_value_us /= 2;
1100 if ((GNUNET_YES != th->auto_retry) || (0 == --th->attempts_left) ||
1101 (delay.rel_value_us < 1)||
1102 (0 != (GNUNET_SCHEDULER_get_reason() & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
1104 LOG (GNUNET_ERROR_TYPE_DEBUG,
1105 "Transmission failed %u times, giving up.\n",
1106 MAX_ATTEMPTS - th->attempts_left);
1107 GNUNET_break (0 == th->notify (th->notify_cls, 0, NULL));
1112 LOG (GNUNET_ERROR_TYPE_DEBUG,
1113 "Failed to connect to `%s', automatically trying again.\n",
1114 client->service_name);
1115 if (GNUNET_YES == client->in_receive)
1117 GNUNET_CONNECTION_receive_cancel (client->connection);
1118 client->in_receive = GNUNET_NO;
1120 GNUNET_CONNECTION_destroy (client->connection);
1121 client->connection = NULL;
1122 delay = GNUNET_TIME_relative_min (delay, client->back_off);
1124 GNUNET_TIME_relative_min (GNUNET_TIME_relative_multiply
1125 (client->back_off, 2),
1126 GNUNET_TIME_UNIT_SECONDS);
1127 LOG (GNUNET_ERROR_TYPE_DEBUG,
1128 "Transmission failed %u times, trying again in %s.\n",
1129 MAX_ATTEMPTS - th->attempts_left,
1130 GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_YES));
1132 th->reconnect_task =
1133 GNUNET_SCHEDULER_add_delayed (delay, &client_delayed_retry, th);
1136 GNUNET_assert (size >= th->size);
1137 ret = th->notify (th->notify_cls, size, buf);
1144 * Ask the client to call us once the specified number of bytes
1145 * are free in the transmission buffer. May call the notify
1146 * method immediately if enough space is available.
1148 * @param client connection to the service
1149 * @param size number of bytes to send
1150 * @param timeout after how long should we give up (and call
1151 * notify with buf NULL and size 0)?
1152 * @param auto_retry if the connection to the service dies, should we
1153 * automatically re-connect and retry (within the timeout period)
1154 * or should we immediately fail in this case? Pass GNUNET_YES
1155 * if the caller does not care about temporary connection errors,
1156 * for example because the protocol is stateless
1157 * @param notify function to call
1158 * @param notify_cls closure for notify
1159 * @return NULL if our buffer will never hold size bytes,
1160 * a handle if the notify callback was queued (can be used to cancel)
1162 struct GNUNET_CLIENT_TransmitHandle *
1163 GNUNET_CLIENT_notify_transmit_ready (struct GNUNET_CLIENT_Connection *client,
1165 struct GNUNET_TIME_Relative timeout,
1167 GNUNET_CONNECTION_TransmitReadyNotify
1168 notify, void *notify_cls)
1170 struct GNUNET_CLIENT_TransmitHandle *th;
1172 if (NULL != client->th)
1174 /* If this breaks, you most likley called this function twice without waiting
1175 * for completion or canceling the request */
1179 th = GNUNET_malloc (sizeof (struct GNUNET_CLIENT_TransmitHandle));
1180 th->client = client;
1182 th->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1183 /* always auto-retry on first message to service */
1184 th->auto_retry = (GNUNET_YES == client->first_message) ? GNUNET_YES : auto_retry;
1185 client->first_message = GNUNET_NO;
1186 th->notify = notify;
1187 th->notify_cls = notify_cls;
1188 th->attempts_left = MAX_ATTEMPTS;
1190 if (NULL == client->connection)
1192 th->reconnect_task =
1193 GNUNET_SCHEDULER_add_delayed (client->back_off, &client_delayed_retry,
1200 GNUNET_CONNECTION_notify_transmit_ready (client->connection, size, timeout,
1201 &client_notify, th);
1215 * Cancel a request for notification.
1217 * @param th handle from the original request.
1220 GNUNET_CLIENT_notify_transmit_ready_cancel (struct GNUNET_CLIENT_TransmitHandle
1223 if (GNUNET_SCHEDULER_NO_TASK != th->reconnect_task)
1225 GNUNET_assert (NULL == th->th);
1226 GNUNET_SCHEDULER_cancel (th->reconnect_task);
1227 th->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1231 GNUNET_assert (NULL != th->th);
1232 GNUNET_CONNECTION_notify_transmit_ready_cancel (th->th);
1234 th->client->th = NULL;
1240 * Function called to notify a client about the socket
1241 * begin ready to queue the message. "buf" will be
1242 * NULL and "size" zero if the socket was closed for
1243 * writing in the meantime.
1245 * @param cls closure of type "struct TransmitGetResponseContext*"
1246 * @param size number of bytes available in buf
1247 * @param buf where the callee should write the message
1248 * @return number of bytes written to buf
1251 transmit_for_response (void *cls, size_t size, void *buf)
1253 struct TransmitGetResponseContext *tc = cls;
1256 tc->client->tag = NULL;
1257 msize = ntohs (tc->hdr->size);
1260 LOG (GNUNET_ERROR_TYPE_DEBUG,
1261 _("Could not submit request, not expecting to receive a response.\n"));
1263 tc->rn (tc->rn_cls, NULL);
1267 GNUNET_assert (size >= msize);
1268 memcpy (buf, tc->hdr, msize);
1269 GNUNET_CLIENT_receive (tc->client, tc->rn, tc->rn_cls,
1270 GNUNET_TIME_absolute_get_remaining (tc->timeout));
1277 * Convenience API that combines sending a request
1278 * to the service and waiting for a response.
1279 * If either operation times out, the callback
1280 * will be called with a "NULL" response (in which
1281 * case the connection should probably be destroyed).
1283 * @param client connection to use
1284 * @param hdr message to transmit
1285 * @param timeout when to give up (for both transmission
1286 * and for waiting for a response)
1287 * @param auto_retry if the connection to the service dies, should we
1288 * automatically re-connect and retry (within the timeout period)
1289 * or should we immediately fail in this case? Pass GNUNET_YES
1290 * if the caller does not care about temporary connection errors,
1291 * for example because the protocol is stateless
1292 * @param rn function to call with the response
1293 * @param rn_cls closure for rn
1294 * @return GNUNET_OK on success, GNUNET_SYSERR if a request
1295 * is already pending
1298 GNUNET_CLIENT_transmit_and_get_response (struct GNUNET_CLIENT_Connection *client,
1299 const struct GNUNET_MessageHeader *hdr,
1300 struct GNUNET_TIME_Relative timeout,
1302 GNUNET_CLIENT_MessageHandler rn,
1305 struct TransmitGetResponseContext *tc;
1308 if (NULL != client->th)
1309 return GNUNET_SYSERR;
1310 GNUNET_assert (NULL == client->tag);
1311 msize = ntohs (hdr->size);
1312 tc = GNUNET_malloc (sizeof (struct TransmitGetResponseContext) + msize);
1313 tc->client = client;
1314 tc->hdr = (const struct GNUNET_MessageHeader *) &tc[1];
1315 memcpy (&tc[1], hdr, msize);
1316 tc->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1318 tc->rn_cls = rn_cls;
1320 GNUNET_CLIENT_notify_transmit_ready (client, msize, timeout, auto_retry,
1321 &transmit_for_response, tc))
1325 return GNUNET_SYSERR;
1332 /* end of client.c */