2 This file is part of GNUnet.
3 Copyright (C) 2001-2016 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_resolver_service.h"
33 #include "gnunet_socks.h"
36 #define LOG(kind,...) GNUNET_log_from (kind, "util-client",__VA_ARGS__)
39 * Timeout we use on TCP connect before trying another
40 * result from the DNS resolver. Actual value used
41 * is this value divided by the number of address families.
44 #define CONNECT_RETRY_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
49 * Internal state for a client connected to a GNUnet service.
55 * During connect, we try multiple possible IP addresses
56 * to find out which one might work.
62 * This is a linked list.
64 struct AddressProbe *next;
67 * This is a doubly-linked list.
69 struct AddressProbe *prev;
72 * The address; do not free (allocated at the end of this struct).
74 const struct sockaddr *addr;
77 * Underlying OS's socket.
79 struct GNUNET_NETWORK_Handle *sock;
82 * Connection for which we are probing.
84 struct ClientState *cstate;
92 * Task waiting for the connection to finish connecting.
94 struct GNUNET_SCHEDULER_Task *task;
99 * Internal state for a client connected to a GNUnet service.
105 * The connection handle, NULL if not live
107 struct GNUNET_NETWORK_Handle *sock;
110 * Handle to a pending DNS lookup request, NULL if DNS is finished.
112 struct GNUNET_RESOLVER_RequestHandle *dns_active;
117 const struct GNUNET_CONFIGURATION_Handle *cfg;
120 * Linked list of sockets we are currently trying out
123 struct AddressProbe *ap_head;
126 * Linked list of sockets we are currently trying out
129 struct AddressProbe *ap_tail;
132 * Name of the service we interact with.
142 * Next message to transmit to the service. NULL for none.
144 const struct GNUNET_MessageHeader *msg;
147 * Task for trying to connect to the service.
149 struct GNUNET_SCHEDULER_Task *retry_task;
152 * Task for sending messages to the service.
154 struct GNUNET_SCHEDULER_Task *send_task;
157 * Task for sending messages to the service.
159 struct GNUNET_SCHEDULER_Task *recv_task;
162 * Tokenizer for inbound messages.
164 struct GNUNET_MessageStreamTokenizer *mst;
167 * Message queue under our control.
169 struct GNUNET_MQ_Handle *mq;
172 * Timeout for receiving a response (absolute time).
174 struct GNUNET_TIME_Absolute receive_timeout;
177 * Current value for our incremental back-off (for
180 struct GNUNET_TIME_Relative back_off;
183 * TCP port (0 for disabled).
185 unsigned long long port;
188 * Offset in the message where we are for transmission.
193 * How often have we tried to connect?
195 unsigned int attempts;
198 * Are we supposed to die? #GNUNET_SYSERR if destruction must be
199 * deferred, #GNUNET_NO by default, #GNUNET_YES if destruction was
208 * Try to connect to the service.
210 * @param cls the `struct ClientState` to try to connect to the service
213 start_connect (void *cls);
217 * We've failed for good to establish a connection (timeout or
218 * no more addresses to try).
220 * @param cstate the connection we tried to establish
223 connect_fail_continuation (struct ClientState *cstate)
225 GNUNET_break (NULL == cstate->ap_head);
226 GNUNET_break (NULL == cstate->ap_tail);
227 GNUNET_break (NULL == cstate->dns_active);
228 GNUNET_break (NULL == cstate->sock);
229 GNUNET_assert (NULL == cstate->send_task);
230 GNUNET_assert (NULL == cstate->recv_task);
231 // GNUNET_assert (NULL == cstate->proxy_handshake);
233 cstate->back_off = GNUNET_TIME_STD_BACKOFF (cstate->back_off);
234 LOG (GNUNET_ERROR_TYPE_DEBUG,
235 "Failed to establish connection to `%s', no further addresses to try, will try again in %s.\n",
236 cstate->service_name,
237 GNUNET_STRINGS_relative_time_to_string (cstate->back_off,
240 = GNUNET_SCHEDULER_add_delayed (cstate->back_off,
247 * We are ready to send a message to the service.
249 * @param cls the `struct ClientState` with the `msg` to transmit
252 transmit_ready (void *cls)
254 struct ClientState *cstate = cls;
258 int notify_in_flight;
260 cstate->send_task = NULL;
261 pos = (const char *) cstate->msg;
262 len = ntohs (cstate->msg->size);
263 GNUNET_assert (cstate->msg_off < len);
265 ret = GNUNET_NETWORK_socket_send (cstate->sock,
266 &pos[cstate->msg_off],
267 len - cstate->msg_off);
270 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
271 "client: error during sending message of type %u\n", ntohs(cstate->msg->type));
273 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
274 "client: retrying message of type %u\n", ntohs(cstate->msg->type));
277 GNUNET_MQ_inject_error (cstate->mq,
278 GNUNET_MQ_ERROR_WRITE);
281 notify_in_flight = (0 == cstate->msg_off);
282 cstate->msg_off += ret;
283 if (cstate->msg_off < len)
285 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
286 "client: rescheduling message of type %u\n", ntohs(cstate->msg->type));
288 = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
292 if (notify_in_flight)
293 GNUNET_MQ_impl_send_in_flight (cstate->mq);
296 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
297 "client: sending message of type %u successful\n", ntohs(cstate->msg->type));
299 GNUNET_MQ_impl_send_continue (cstate->mq);
304 * We have received a full message, pass to the MQ dispatcher.
305 * Called by the tokenizer via #receive_ready().
307 * @param cls the `struct ClientState`
308 * @param msg message we received.
309 * @return #GNUNET_OK on success, #GNUNET_SYSERR to stop further processing
312 recv_message (void *cls,
313 const struct GNUNET_MessageHeader *msg)
315 struct ClientState *cstate = cls;
317 if (GNUNET_YES == cstate->in_destroy)
318 return GNUNET_SYSERR;
319 LOG (GNUNET_ERROR_TYPE_DEBUG,
320 "Received message of type %u and size %u from %s\n",
323 cstate->service_name);
324 GNUNET_MQ_inject_message (cstate->mq,
326 if (GNUNET_YES == cstate->in_destroy)
327 return GNUNET_SYSERR;
333 * Cancel all remaining connect attempts
335 * @param cstate handle of the client state to process
338 cancel_aps (struct ClientState *cstate)
340 struct AddressProbe *pos;
342 while (NULL != (pos = cstate->ap_head))
344 GNUNET_break (GNUNET_OK ==
345 GNUNET_NETWORK_socket_close (pos->sock));
346 GNUNET_SCHEDULER_cancel (pos->task);
347 GNUNET_CONTAINER_DLL_remove (cstate->ap_head,
356 * Implement the destruction of a message queue. Implementations must
357 * not free @a mq, but should take care of @a impl_state.
359 * @param mq the message queue to destroy
360 * @param impl_state our `struct ClientState`
363 connection_client_destroy_impl (struct GNUNET_MQ_Handle *mq,
366 struct ClientState *cstate = impl_state;
368 if (GNUNET_SYSERR == cstate->in_destroy)
370 /* defer destruction */
371 cstate->in_destroy = GNUNET_YES;
375 if (NULL != cstate->dns_active)
376 GNUNET_RESOLVER_request_cancel (cstate->dns_active);
377 if (NULL != cstate->send_task)
378 GNUNET_SCHEDULER_cancel (cstate->send_task);
379 if (NULL != cstate->recv_task)
380 GNUNET_SCHEDULER_cancel (cstate->recv_task);
381 if (NULL != cstate->retry_task)
382 GNUNET_SCHEDULER_cancel (cstate->retry_task);
383 if (NULL != cstate->sock)
384 GNUNET_NETWORK_socket_close (cstate->sock);
386 GNUNET_free (cstate->service_name);
387 GNUNET_free_non_null (cstate->hostname);
388 GNUNET_MST_destroy (cstate->mst);
389 GNUNET_free (cstate);
394 * This function is called once we have data ready to read.
396 * @param cls `struct ClientState` with connection to read from
399 receive_ready (void *cls)
401 struct ClientState *cstate = cls;
404 cstate->recv_task = NULL;
405 cstate->in_destroy = GNUNET_SYSERR;
406 ret = GNUNET_MST_read (cstate->mst,
410 if (GNUNET_SYSERR == ret)
412 if (NULL != cstate->mq)
413 GNUNET_MQ_inject_error (cstate->mq,
414 GNUNET_MQ_ERROR_READ);
415 if (GNUNET_YES == cstate->in_destroy)
416 connection_client_destroy_impl (cstate->mq,
420 if (GNUNET_YES == cstate->in_destroy)
422 connection_client_destroy_impl (cstate->mq,
426 cstate->in_destroy = GNUNET_NO;
428 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
436 * We've succeeded in establishing a connection.
438 * @param cstate the connection we tried to establish
441 connect_success_continuation (struct ClientState *cstate)
443 GNUNET_assert (NULL == cstate->recv_task);
445 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
449 if (NULL != cstate->msg)
451 GNUNET_assert (NULL == cstate->send_task);
453 = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
462 * Try connecting to the server using UNIX domain sockets.
464 * @param service_name name of service to connect to
465 * @param cfg configuration to use
466 * @return NULL on error, socket connected to UNIX otherwise
468 static struct GNUNET_NETWORK_Handle *
469 try_unixpath (const char *service_name,
470 const struct GNUNET_CONFIGURATION_Handle *cfg)
473 struct GNUNET_NETWORK_Handle *sock;
475 struct sockaddr_un s_un;
479 GNUNET_CONFIGURATION_get_value_filename (cfg,
483 (0 < strlen (unixpath)))
485 /* We have a non-NULL unixpath, need to validate it */
486 if (strlen (unixpath) >= sizeof (s_un.sun_path))
488 LOG (GNUNET_ERROR_TYPE_WARNING,
489 _("UNIXPATH `%s' too long, maximum length is %llu\n"),
491 (unsigned long long) sizeof (s_un.sun_path));
492 unixpath = GNUNET_NETWORK_shorten_unixpath (unixpath);
493 LOG (GNUNET_ERROR_TYPE_INFO,
494 _("Using `%s' instead\n"),
496 if (NULL == unixpath)
502 s_un.sun_family = AF_UNIX;
503 strncpy (s_un.sun_path,
505 sizeof (s_un.sun_path) - 1);
510 abstract = GNUNET_CONFIGURATION_get_value_yesno (cfg,
512 "USE_ABSTRACT_SOCKETS");
513 if (GNUNET_YES == abstract)
514 s_un.sun_path[0] = '\0';
517 #if HAVE_SOCKADDR_UN_SUN_LEN
518 s_un.sun_len = (u_char) sizeof (struct sockaddr_un);
520 sock = GNUNET_NETWORK_socket_create (AF_UNIX,
523 if ( (NULL != sock) &&
525 GNUNET_NETWORK_socket_connect (sock,
526 (struct sockaddr *) &s_un,
528 (EINPROGRESS == errno) ) )
530 LOG (GNUNET_ERROR_TYPE_DEBUG,
531 "Successfully connected to unixpath `%s'!\n",
533 GNUNET_free (unixpath);
537 GNUNET_NETWORK_socket_close (sock);
539 GNUNET_free_non_null (unixpath);
546 * Scheduler let us know that we're either ready to write on the
547 * socket OR connect timed out. Do the right thing.
549 * @param cls the `struct AddressProbe *` with the address that we are probing
552 connect_probe_continuation (void *cls)
554 struct AddressProbe *ap = cls;
555 struct ClientState *cstate = ap->cstate;
556 const struct GNUNET_SCHEDULER_TaskContext *tc;
561 GNUNET_assert (NULL != ap->sock);
562 GNUNET_CONTAINER_DLL_remove (cstate->ap_head,
565 len = sizeof (error);
567 tc = GNUNET_SCHEDULER_get_task_context ();
568 if ( (0 == (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) ||
570 GNUNET_NETWORK_socket_getsockopt (ap->sock,
577 GNUNET_break (GNUNET_OK ==
578 GNUNET_NETWORK_socket_close (ap->sock));
580 if ( (NULL == cstate->ap_head) &&
581 // (NULL == cstate->proxy_handshake) &&
582 (NULL == cstate->dns_active) )
583 connect_fail_continuation (cstate);
586 LOG (GNUNET_ERROR_TYPE_DEBUG,
587 "Connection to `%s' succeeded!\n",
588 cstate->service_name);
589 /* trigger jobs that waited for the connection */
590 GNUNET_assert (NULL == cstate->sock);
591 cstate->sock = ap->sock;
594 connect_success_continuation (cstate);
599 * Try to establish a connection given the specified address.
600 * This function is called by the resolver once we have a DNS reply.
602 * @param cls our `struct ClientState *`
603 * @param addr address to try, NULL for "last call"
604 * @param addrlen length of @a addr
607 try_connect_using_address (void *cls,
608 const struct sockaddr *addr,
611 struct ClientState *cstate = cls;
612 struct AddressProbe *ap;
616 cstate->dns_active = NULL;
617 if ( (NULL == cstate->ap_head) &&
618 // (NULL == cstate->proxy_handshake) &&
619 (NULL == cstate->sock) )
620 connect_fail_continuation (cstate);
623 if (NULL != cstate->sock)
624 return; /* already connected */
626 LOG (GNUNET_ERROR_TYPE_DEBUG,
627 "Trying to connect using address `%s:%u'\n",
631 ap = GNUNET_malloc (sizeof (struct AddressProbe) + addrlen);
632 ap->addr = (const struct sockaddr *) &ap[1];
633 GNUNET_memcpy (&ap[1],
636 ap->addrlen = addrlen;
639 switch (ap->addr->sa_family)
642 ((struct sockaddr_in *) ap->addr)->sin_port = htons (cstate->port);
645 ((struct sockaddr_in6 *) ap->addr)->sin6_port = htons (cstate->port);
650 return; /* not supported by us */
652 ap->sock = GNUNET_NETWORK_socket_create (ap->addr->sa_family,
655 if (NULL == ap->sock)
658 return; /* not supported by OS */
661 GNUNET_NETWORK_socket_connect (ap->sock,
664 (EINPROGRESS != errno) )
666 /* maybe refused / unsupported address, try next */
667 GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO,
669 GNUNET_break (GNUNET_OK ==
670 GNUNET_NETWORK_socket_close (ap->sock));
674 GNUNET_CONTAINER_DLL_insert (cstate->ap_head,
677 ap->task = GNUNET_SCHEDULER_add_write_net (CONNECT_RETRY_TIMEOUT,
679 &connect_probe_continuation,
685 * Test whether the configuration has proper values for connection
686 * (UNIXPATH || (PORT && HOSTNAME)).
688 * @param service_name name of service to connect to
689 * @param cfg configuration to use
690 * @return #GNUNET_OK if the configuration is valid, #GNUNET_SYSERR if not
693 test_service_configuration (const char *service_name,
694 const struct GNUNET_CONFIGURATION_Handle *cfg)
696 int ret = GNUNET_SYSERR;
697 char *hostname = NULL;
698 unsigned long long port;
700 char *unixpath = NULL;
703 GNUNET_CONFIGURATION_get_value_filename (cfg,
707 (0 < strlen (unixpath)))
709 GNUNET_free_non_null (unixpath);
713 GNUNET_CONFIGURATION_have_value (cfg,
717 GNUNET_CONFIGURATION_get_value_number (cfg,
724 GNUNET_CONFIGURATION_get_value_string (cfg,
728 (0 != strlen (hostname)) )
730 GNUNET_free_non_null (hostname);
736 * Try to connect to the service.
738 * @param cls the `struct ClientState` to try to connect to the service
741 start_connect (void *cls)
743 struct ClientState *cstate = cls;
745 cstate->retry_task = NULL;
747 /* Never use a local source if a proxy is configured */
749 GNUNET_SOCKS_check_service (cstate->service_name,
752 socks_connect (cstate);
757 if ( (0 == (cstate->attempts++ % 2)) ||
758 (0 == cstate->port) ||
759 (NULL == cstate->hostname) )
761 /* on even rounds, try UNIX first, or always
762 if we do not have a DNS name and TCP port. */
763 cstate->sock = try_unixpath (cstate->service_name,
765 if (NULL != cstate->sock)
767 connect_success_continuation (cstate);
771 if ( (NULL == cstate->hostname) ||
772 (0 == cstate->port) )
774 /* All options failed. Boo! */
775 connect_fail_continuation (cstate);
779 = GNUNET_RESOLVER_ip_get (cstate->hostname,
781 CONNECT_RETRY_TIMEOUT,
782 &try_connect_using_address,
788 * Implements the transmission functionality of a message queue.
790 * @param mq the message queue
791 * @param msg the message to send
792 * @param impl_state our `struct ClientState`
795 connection_client_send_impl (struct GNUNET_MQ_Handle *mq,
796 const struct GNUNET_MessageHeader *msg,
799 struct ClientState *cstate = impl_state;
801 /* only one message at a time allowed */
802 GNUNET_assert (NULL == cstate->msg);
803 GNUNET_assert (NULL == cstate->send_task);
806 if (NULL == cstate->sock){
807 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
808 "client: waiting for socket\n");
809 return; /* still waiting for connection */
812 = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
820 * Cancel the currently sent message.
822 * @param mq message queue
823 * @param impl_state our `struct ClientState`
826 connection_client_cancel_impl (struct GNUNET_MQ_Handle *mq,
829 struct ClientState *cstate = impl_state;
831 GNUNET_assert (NULL != cstate->msg);
832 GNUNET_assert (0 == cstate->msg_off);
834 if (NULL != cstate->send_task)
836 GNUNET_SCHEDULER_cancel (cstate->send_task);
837 cstate->send_task = NULL;
843 * Create a message queue to connect to a GNUnet service.
844 * If handlers are specfied, receive messages from the connection.
846 * @param cfg our configuration
847 * @param service_name name of the service to connect to
848 * @param handlers handlers for receiving messages, can be NULL
849 * @param error_handler error handler
850 * @param error_handler_cls closure for the @a error_handler
851 * @return the message queue, NULL on error
853 struct GNUNET_MQ_Handle *
854 GNUNET_CLIENT_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
855 const char *service_name,
856 const struct GNUNET_MQ_MessageHandler *handlers,
857 GNUNET_MQ_ErrorHandler error_handler,
858 void *error_handler_cls)
860 struct ClientState *cstate;
863 test_service_configuration (service_name,
866 cstate = GNUNET_new (struct ClientState);
867 cstate->service_name = GNUNET_strdup (service_name);
869 cstate->retry_task = GNUNET_SCHEDULER_add_now (&start_connect,
871 cstate->mst = GNUNET_MST_create (&recv_message,
874 GNUNET_CONFIGURATION_have_value (cfg,
878 if (! ( (GNUNET_OK !=
879 GNUNET_CONFIGURATION_get_value_number (cfg,
883 (cstate->port > 65535) ||
885 GNUNET_CONFIGURATION_get_value_string (cfg,
888 &cstate->hostname)) ) &&
889 (0 == strlen (cstate->hostname)) )
891 GNUNET_free (cstate->hostname);
892 cstate->hostname = NULL;
893 LOG (GNUNET_ERROR_TYPE_WARNING,
894 _("Need a non-empty hostname for service `%s'.\n"),
898 cstate->mq = GNUNET_MQ_queue_for_callbacks (&connection_client_send_impl,
899 &connection_client_destroy_impl,
900 &connection_client_cancel_impl,
908 /* end of client.c */