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);
272 GNUNET_MQ_inject_error (cstate->mq,
273 GNUNET_MQ_ERROR_WRITE);
276 notify_in_flight = (0 == cstate->msg_off);
277 cstate->msg_off += ret;
278 if (cstate->msg_off < len)
281 = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
285 if (notify_in_flight)
286 GNUNET_MQ_impl_send_in_flight (cstate->mq);
290 GNUNET_MQ_impl_send_continue (cstate->mq);
295 * We have received a full message, pass to the MQ dispatcher.
296 * Called by the tokenizer via #receive_ready().
298 * @param cls the `struct ClientState`
299 * @param msg message we received.
300 * @return #GNUNET_OK on success, #GNUNET_SYSERR to stop further processing
303 recv_message (void *cls,
304 const struct GNUNET_MessageHeader *msg)
306 struct ClientState *cstate = cls;
308 if (GNUNET_YES == cstate->in_destroy)
309 return GNUNET_SYSERR;
310 LOG (GNUNET_ERROR_TYPE_DEBUG,
311 "Received message of type %u and size %u from %s\n",
314 cstate->service_name);
315 GNUNET_MQ_inject_message (cstate->mq,
317 if (GNUNET_YES == cstate->in_destroy)
318 return GNUNET_SYSERR;
324 * Cancel all remaining connect attempts
326 * @param cstate handle of the client state to process
329 cancel_aps (struct ClientState *cstate)
331 struct AddressProbe *pos;
333 while (NULL != (pos = cstate->ap_head))
335 GNUNET_break (GNUNET_OK ==
336 GNUNET_NETWORK_socket_close (pos->sock));
337 GNUNET_SCHEDULER_cancel (pos->task);
338 GNUNET_CONTAINER_DLL_remove (cstate->ap_head,
347 * Implement the destruction of a message queue. Implementations must
348 * not free @a mq, but should take care of @a impl_state.
350 * @param mq the message queue to destroy
351 * @param impl_state our `struct ClientState`
354 connection_client_destroy_impl (struct GNUNET_MQ_Handle *mq,
357 struct ClientState *cstate = impl_state;
359 if (GNUNET_SYSERR == cstate->in_destroy)
361 /* defer destruction */
362 cstate->in_destroy = GNUNET_YES;
366 if (NULL != cstate->dns_active)
367 GNUNET_RESOLVER_request_cancel (cstate->dns_active);
368 if (NULL != cstate->send_task)
369 GNUNET_SCHEDULER_cancel (cstate->send_task);
370 if (NULL != cstate->recv_task)
371 GNUNET_SCHEDULER_cancel (cstate->recv_task);
372 if (NULL != cstate->retry_task)
373 GNUNET_SCHEDULER_cancel (cstate->retry_task);
374 if (NULL != cstate->sock)
375 GNUNET_NETWORK_socket_close (cstate->sock);
377 GNUNET_free (cstate->service_name);
378 GNUNET_free_non_null (cstate->hostname);
379 GNUNET_MST_destroy (cstate->mst);
380 GNUNET_free (cstate);
385 * This function is called once we have data ready to read.
387 * @param cls `struct ClientState` with connection to read from
390 receive_ready (void *cls)
392 struct ClientState *cstate = cls;
395 cstate->recv_task = NULL;
396 cstate->in_destroy = GNUNET_SYSERR;
397 ret = GNUNET_MST_read (cstate->mst,
401 if (GNUNET_SYSERR == ret)
403 if (NULL != cstate->mq)
404 GNUNET_MQ_inject_error (cstate->mq,
405 GNUNET_MQ_ERROR_READ);
406 if (GNUNET_YES == cstate->in_destroy)
407 connection_client_destroy_impl (cstate->mq,
411 if (GNUNET_YES == cstate->in_destroy)
413 connection_client_destroy_impl (cstate->mq,
417 cstate->in_destroy = GNUNET_NO;
419 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
427 * We've succeeded in establishing a connection.
429 * @param cstate the connection we tried to establish
432 connect_success_continuation (struct ClientState *cstate)
434 GNUNET_assert (NULL == cstate->recv_task);
436 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
440 if (NULL != cstate->msg)
442 GNUNET_assert (NULL == cstate->send_task);
444 = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
453 * Try connecting to the server using UNIX domain sockets.
455 * @param service_name name of service to connect to
456 * @param cfg configuration to use
457 * @return NULL on error, socket connected to UNIX otherwise
459 static struct GNUNET_NETWORK_Handle *
460 try_unixpath (const char *service_name,
461 const struct GNUNET_CONFIGURATION_Handle *cfg)
464 struct GNUNET_NETWORK_Handle *sock;
466 struct sockaddr_un s_un;
470 GNUNET_CONFIGURATION_get_value_filename (cfg,
474 (0 < strlen (unixpath)))
476 /* We have a non-NULL unixpath, need to validate it */
477 if (strlen (unixpath) >= sizeof (s_un.sun_path))
479 LOG (GNUNET_ERROR_TYPE_WARNING,
480 _("UNIXPATH `%s' too long, maximum length is %llu\n"),
482 (unsigned long long) sizeof (s_un.sun_path));
483 unixpath = GNUNET_NETWORK_shorten_unixpath (unixpath);
484 LOG (GNUNET_ERROR_TYPE_INFO,
485 _("Using `%s' instead\n"),
487 if (NULL == unixpath)
493 s_un.sun_family = AF_UNIX;
494 strncpy (s_un.sun_path,
496 sizeof (s_un.sun_path) - 1);
501 abstract = GNUNET_CONFIGURATION_get_value_yesno (cfg,
503 "USE_ABSTRACT_SOCKETS");
504 if (GNUNET_YES == abstract)
505 s_un.sun_path[0] = '\0';
508 #if HAVE_SOCKADDR_UN_SUN_LEN
509 s_un.sun_len = (u_char) sizeof (struct sockaddr_un);
511 sock = GNUNET_NETWORK_socket_create (AF_UNIX,
514 if ( (NULL != sock) &&
516 GNUNET_NETWORK_socket_connect (sock,
517 (struct sockaddr *) &s_un,
519 (EINPROGRESS == errno) ) )
521 LOG (GNUNET_ERROR_TYPE_DEBUG,
522 "Successfully connected to unixpath `%s'!\n",
524 GNUNET_free (unixpath);
528 GNUNET_NETWORK_socket_close (sock);
530 GNUNET_free_non_null (unixpath);
537 * Scheduler let us know that we're either ready to write on the
538 * socket OR connect timed out. Do the right thing.
540 * @param cls the `struct AddressProbe *` with the address that we are probing
543 connect_probe_continuation (void *cls)
545 struct AddressProbe *ap = cls;
546 struct ClientState *cstate = ap->cstate;
547 const struct GNUNET_SCHEDULER_TaskContext *tc;
552 GNUNET_assert (NULL != ap->sock);
553 GNUNET_CONTAINER_DLL_remove (cstate->ap_head,
556 len = sizeof (error);
558 tc = GNUNET_SCHEDULER_get_task_context ();
559 if ( (0 == (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) ||
561 GNUNET_NETWORK_socket_getsockopt (ap->sock,
568 GNUNET_break (GNUNET_OK ==
569 GNUNET_NETWORK_socket_close (ap->sock));
571 if ( (NULL == cstate->ap_head) &&
572 // (NULL == cstate->proxy_handshake) &&
573 (NULL == cstate->dns_active) )
574 connect_fail_continuation (cstate);
577 LOG (GNUNET_ERROR_TYPE_DEBUG,
578 "Connection to `%s' succeeded!\n",
579 cstate->service_name);
580 /* trigger jobs that waited for the connection */
581 GNUNET_assert (NULL == cstate->sock);
582 cstate->sock = ap->sock;
585 connect_success_continuation (cstate);
590 * Try to establish a connection given the specified address.
591 * This function is called by the resolver once we have a DNS reply.
593 * @param cls our `struct ClientState *`
594 * @param addr address to try, NULL for "last call"
595 * @param addrlen length of @a addr
598 try_connect_using_address (void *cls,
599 const struct sockaddr *addr,
602 struct ClientState *cstate = cls;
603 struct AddressProbe *ap;
607 cstate->dns_active = NULL;
608 if ( (NULL == cstate->ap_head) &&
609 // (NULL == cstate->proxy_handshake) &&
610 (NULL == cstate->sock) )
611 connect_fail_continuation (cstate);
614 if (NULL != cstate->sock)
615 return; /* already connected */
617 LOG (GNUNET_ERROR_TYPE_DEBUG,
618 "Trying to connect using address `%s:%u'\n",
622 ap = GNUNET_malloc (sizeof (struct AddressProbe) + addrlen);
623 ap->addr = (const struct sockaddr *) &ap[1];
624 GNUNET_memcpy (&ap[1],
627 ap->addrlen = addrlen;
630 switch (ap->addr->sa_family)
633 ((struct sockaddr_in *) ap->addr)->sin_port = htons (cstate->port);
636 ((struct sockaddr_in6 *) ap->addr)->sin6_port = htons (cstate->port);
641 return; /* not supported by us */
643 ap->sock = GNUNET_NETWORK_socket_create (ap->addr->sa_family,
646 if (NULL == ap->sock)
649 return; /* not supported by OS */
652 GNUNET_NETWORK_socket_connect (ap->sock,
655 (EINPROGRESS != errno) )
657 /* maybe refused / unsupported address, try next */
658 GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO,
660 GNUNET_break (GNUNET_OK ==
661 GNUNET_NETWORK_socket_close (ap->sock));
665 GNUNET_CONTAINER_DLL_insert (cstate->ap_head,
668 ap->task = GNUNET_SCHEDULER_add_write_net (CONNECT_RETRY_TIMEOUT,
670 &connect_probe_continuation,
676 * Test whether the configuration has proper values for connection
677 * (UNIXPATH || (PORT && HOSTNAME)).
679 * @param service_name name of service to connect to
680 * @param cfg configuration to use
681 * @return #GNUNET_OK if the configuration is valid, #GNUNET_SYSERR if not
684 test_service_configuration (const char *service_name,
685 const struct GNUNET_CONFIGURATION_Handle *cfg)
687 int ret = GNUNET_SYSERR;
688 char *hostname = NULL;
689 unsigned long long port;
691 char *unixpath = NULL;
694 GNUNET_CONFIGURATION_get_value_filename (cfg,
698 (0 < strlen (unixpath)))
700 GNUNET_free_non_null (unixpath);
704 GNUNET_CONFIGURATION_have_value (cfg,
708 GNUNET_CONFIGURATION_get_value_number (cfg,
715 GNUNET_CONFIGURATION_get_value_string (cfg,
719 (0 != strlen (hostname)) )
721 GNUNET_free_non_null (hostname);
727 * Try to connect to the service.
729 * @param cls the `struct ClientState` to try to connect to the service
732 start_connect (void *cls)
734 struct ClientState *cstate = cls;
736 cstate->retry_task = NULL;
738 /* Never use a local source if a proxy is configured */
740 GNUNET_SOCKS_check_service (cstate->service_name,
743 socks_connect (cstate);
748 if ( (0 == (cstate->attempts++ % 2)) ||
749 (0 == cstate->port) ||
750 (NULL == cstate->hostname) )
752 /* on even rounds, try UNIX first, or always
753 if we do not have a DNS name and TCP port. */
754 cstate->sock = try_unixpath (cstate->service_name,
756 if (NULL != cstate->sock)
758 connect_success_continuation (cstate);
762 if ( (NULL == cstate->hostname) ||
763 (0 == cstate->port) )
765 /* All options failed. Boo! */
766 connect_fail_continuation (cstate);
770 = GNUNET_RESOLVER_ip_get (cstate->hostname,
772 CONNECT_RETRY_TIMEOUT,
773 &try_connect_using_address,
779 * Implements the transmission functionality of a message queue.
781 * @param mq the message queue
782 * @param msg the message to send
783 * @param impl_state our `struct ClientState`
786 connection_client_send_impl (struct GNUNET_MQ_Handle *mq,
787 const struct GNUNET_MessageHeader *msg,
790 struct ClientState *cstate = impl_state;
792 /* only one message at a time allowed */
793 GNUNET_assert (NULL == cstate->msg);
794 GNUNET_assert (NULL == cstate->send_task);
797 if (NULL == cstate->sock)
798 return; /* still waiting for connection */
800 = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
808 * Cancel the currently sent message.
810 * @param mq message queue
811 * @param impl_state our `struct ClientState`
814 connection_client_cancel_impl (struct GNUNET_MQ_Handle *mq,
817 struct ClientState *cstate = impl_state;
819 GNUNET_assert (NULL != cstate->msg);
820 GNUNET_assert (0 == cstate->msg_off);
822 if (NULL != cstate->send_task)
824 GNUNET_SCHEDULER_cancel (cstate->send_task);
825 cstate->send_task = NULL;
831 * Create a message queue to connect to a GNUnet service.
832 * If handlers are specfied, receive messages from the connection.
834 * @param cfg our configuration
835 * @param service_name name of the service to connect to
836 * @param handlers handlers for receiving messages, can be NULL
837 * @param error_handler error handler
838 * @param error_handler_cls closure for the @a error_handler
839 * @return the message queue, NULL on error
841 struct GNUNET_MQ_Handle *
842 GNUNET_CLIENT_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
843 const char *service_name,
844 const struct GNUNET_MQ_MessageHandler *handlers,
845 GNUNET_MQ_ErrorHandler error_handler,
846 void *error_handler_cls)
848 struct ClientState *cstate;
851 test_service_configuration (service_name,
854 cstate = GNUNET_new (struct ClientState);
855 cstate->service_name = GNUNET_strdup (service_name);
857 cstate->retry_task = GNUNET_SCHEDULER_add_now (&start_connect,
859 cstate->mst = GNUNET_MST_create (&recv_message,
862 GNUNET_CONFIGURATION_have_value (cfg,
866 if (! ( (GNUNET_OK !=
867 GNUNET_CONFIGURATION_get_value_number (cfg,
871 (cstate->port > 65535) ||
873 GNUNET_CONFIGURATION_get_value_string (cfg,
876 &cstate->hostname)) ) &&
877 (0 == strlen (cstate->hostname)) )
879 GNUNET_free (cstate->hostname);
880 cstate->hostname = NULL;
881 LOG (GNUNET_ERROR_TYPE_WARNING,
882 _("Need a non-empty hostname for service `%s'.\n"),
886 cstate->mq = GNUNET_MQ_queue_for_callbacks (&connection_client_send_impl,
887 &connection_client_destroy_impl,
888 &connection_client_cancel_impl,
896 /* end of client.c */