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",__VA_ARGS__)
40 * Internal state for a client connected to a GNUnet service.
46 * During connect, we try multiple possible IP addresses
47 * to find out which one might work.
53 * This is a linked list.
55 struct AddressProbe *next;
58 * This is a doubly-linked list.
60 struct AddressProbe *prev;
63 * The address; do not free (allocated at the end of this struct).
65 const struct sockaddr *addr;
68 * Underlying OS's socket.
70 struct GNUNET_NETWORK_Handle *sock;
73 * Connection for which we are probing.
75 struct ClientState *cstate;
83 * Task waiting for the connection to finish connecting.
85 struct GNUNET_SCHEDULER_Task *task;
90 * Internal state for a client connected to a GNUnet service.
96 * The connection handle, NULL if not live
98 struct GNUNET_NETWORK_Handle *sock;
101 * Handle to a pending DNS lookup request, NULL if DNS is finished.
103 struct GNUNET_RESOLVER_RequestHandle *dns_active;
108 const struct GNUNET_CONFIGURATION_Handle *cfg;
111 * Linked list of sockets we are currently trying out
114 struct AddressProbe *ap_head;
117 * Linked list of sockets we are currently trying out
120 struct AddressProbe *ap_tail;
123 * Name of the service we interact with.
133 * Next message to transmit to the service. NULL for none.
135 const struct GNUNET_MessageHeader *msg;
138 * Task for trying to connect to the service.
140 struct GNUNET_SCHEDULER_Task *retry_task;
143 * Task for sending messages to the service.
145 struct GNUNET_SCHEDULER_Task *send_task;
148 * Task for sending messages to the service.
150 struct GNUNET_SCHEDULER_Task *recv_task;
153 * Tokenizer for inbound messages.
155 struct GNUNET_MessageStreamTokenizer *mst;
158 * Message queue under our control.
160 struct GNUNET_MQ_Handle *mq;
163 * Timeout for receiving a response (absolute time).
165 struct GNUNET_TIME_Absolute receive_timeout;
168 * Current value for our incremental back-off (for
171 struct GNUNET_TIME_Relative back_off;
174 * TCP port (0 for disabled).
176 unsigned long long port;
179 * Offset in the message where we are for transmission.
184 * How often have we tried to connect?
186 unsigned int attempts;
189 * Are we supposed to die? #GNUNET_SYSERR if destruction must be
190 * deferred, #GNUNET_NO by default, #GNUNET_YES if destruction was
199 * Try to connect to the service.
201 * @param cls the `struct ClientState` to try to connect to the service
204 start_connect (void *cls);
208 * We've failed for good to establish a connection (timeout or
209 * no more addresses to try).
211 * @param cstate the connection we tried to establish
214 connect_fail_continuation (struct ClientState *cstate)
216 GNUNET_break (NULL == cstate->ap_head);
217 GNUNET_break (NULL == cstate->ap_tail);
218 GNUNET_break (NULL == cstate->dns_active);
219 GNUNET_break (NULL == cstate->sock);
220 GNUNET_assert (NULL == cstate->send_task);
221 GNUNET_assert (NULL == cstate->recv_task);
222 // GNUNET_assert (NULL == cstate->proxy_handshake);
224 cstate->back_off = GNUNET_TIME_STD_BACKOFF (cstate->back_off);
225 LOG (GNUNET_ERROR_TYPE_DEBUG,
226 "Failed to establish connection to `%s', no further addresses to try, will try again in %s.\n",
227 cstate->service_name,
228 GNUNET_STRINGS_relative_time_to_string (cstate->back_off,
231 = GNUNET_SCHEDULER_add_delayed (cstate->back_off,
238 * We are ready to send a message to the service.
240 * @param cls the `struct ClientState` with the `msg` to transmit
243 transmit_ready (void *cls)
245 struct ClientState *cstate = cls;
249 int notify_in_flight;
251 cstate->send_task = NULL;
252 pos = (const char *) cstate->msg;
253 len = ntohs (cstate->msg->size);
254 GNUNET_assert (cstate->msg_off < len);
256 ret = GNUNET_NETWORK_socket_send (cstate->sock,
257 &pos[cstate->msg_off],
258 len - cstate->msg_off);
263 GNUNET_MQ_inject_error (cstate->mq,
264 GNUNET_MQ_ERROR_WRITE);
267 notify_in_flight = (0 == cstate->msg_off);
268 cstate->msg_off += ret;
269 if (cstate->msg_off < len)
272 = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
276 if (notify_in_flight)
277 GNUNET_MQ_impl_send_in_flight (cstate->mq);
281 GNUNET_MQ_impl_send_continue (cstate->mq);
286 * We have received a full message, pass to the MQ dispatcher.
287 * Called by the tokenizer via #receive_ready().
289 * @param cls the `struct ClientState`
290 * @param msg message we received.
291 * @return #GNUNET_OK on success, #GNUNET_SYSERR to stop further processing
294 recv_message (void *cls,
295 const struct GNUNET_MessageHeader *msg)
297 struct ClientState *cstate = cls;
299 if (GNUNET_YES == cstate->in_destroy)
300 return GNUNET_SYSERR;
301 GNUNET_MQ_inject_message (cstate->mq,
303 if (GNUNET_YES == cstate->in_destroy)
304 return GNUNET_SYSERR;
310 * Cancel all remaining connect attempts
312 * @param cstate handle of the client state to process
315 cancel_aps (struct ClientState *cstate)
317 struct AddressProbe *pos;
319 while (NULL != (pos = cstate->ap_head))
321 GNUNET_break (GNUNET_OK ==
322 GNUNET_NETWORK_socket_close (pos->sock));
323 GNUNET_SCHEDULER_cancel (pos->task);
324 GNUNET_CONTAINER_DLL_remove (cstate->ap_head,
333 * Implement the destruction of a message queue. Implementations must
334 * not free @a mq, but should take care of @a impl_state.
336 * @param mq the message queue to destroy
337 * @param impl_state our `struct ClientState`
340 connection_client_destroy_impl (struct GNUNET_MQ_Handle *mq,
343 struct ClientState *cstate = impl_state;
345 if (GNUNET_SYSERR == cstate->in_destroy)
347 /* defer destruction */
348 cstate->in_destroy = GNUNET_YES;
352 if (NULL != cstate->dns_active)
353 GNUNET_RESOLVER_request_cancel (cstate->dns_active);
354 if (NULL != cstate->send_task)
355 GNUNET_SCHEDULER_cancel (cstate->send_task);
356 if (NULL != cstate->recv_task)
357 GNUNET_SCHEDULER_cancel (cstate->recv_task);
358 if (NULL != cstate->retry_task)
359 GNUNET_SCHEDULER_cancel (cstate->retry_task);
360 if (NULL != cstate->sock)
361 GNUNET_NETWORK_socket_close (cstate->sock);
363 GNUNET_free (cstate->service_name);
364 GNUNET_free_non_null (cstate->hostname);
365 GNUNET_MST_destroy (cstate->mst);
366 GNUNET_free (cstate);
371 * This function is called once we have data ready to read.
373 * @param cls `struct ClientState` with connection to read from
376 receive_ready (void *cls)
378 struct ClientState *cstate = cls;
381 cstate->recv_task = NULL;
382 cstate->in_destroy = GNUNET_SYSERR;
383 ret = GNUNET_MST_read (cstate->mst,
387 if (GNUNET_SYSERR == ret)
389 if (NULL != cstate->mq)
390 GNUNET_MQ_inject_error (cstate->mq,
391 GNUNET_MQ_ERROR_READ);
392 if (GNUNET_YES == cstate->in_destroy)
393 connection_client_destroy_impl (cstate->mq,
397 if (GNUNET_YES == cstate->in_destroy)
399 connection_client_destroy_impl (cstate->mq,
403 cstate->in_destroy = GNUNET_NO;
405 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
413 * We've succeeded in establishing a connection.
415 * @param cstate the connection we tried to establish
418 connect_success_continuation (struct ClientState *cstate)
420 GNUNET_assert (NULL == cstate->recv_task);
422 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
426 if (NULL != cstate->msg)
428 GNUNET_assert (NULL == cstate->send_task);
430 = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
439 * Try connecting to the server using UNIX domain sockets.
441 * @param service_name name of service to connect to
442 * @param cfg configuration to use
443 * @return NULL on error, socket connected to UNIX otherwise
445 static struct GNUNET_NETWORK_Handle *
446 try_unixpath (const char *service_name,
447 const struct GNUNET_CONFIGURATION_Handle *cfg)
450 struct GNUNET_NETWORK_Handle *sock;
452 struct sockaddr_un s_un;
456 GNUNET_CONFIGURATION_get_value_filename (cfg,
460 (0 < strlen (unixpath)))
462 /* We have a non-NULL unixpath, need to validate it */
463 if (strlen (unixpath) >= sizeof (s_un.sun_path))
465 LOG (GNUNET_ERROR_TYPE_WARNING,
466 _("UNIXPATH `%s' too long, maximum length is %llu\n"),
468 (unsigned long long) sizeof (s_un.sun_path));
469 unixpath = GNUNET_NETWORK_shorten_unixpath (unixpath);
470 LOG (GNUNET_ERROR_TYPE_INFO,
471 _("Using `%s' instead\n"),
473 if (NULL == unixpath)
479 s_un.sun_family = AF_UNIX;
480 strncpy (s_un.sun_path,
482 sizeof (s_un.sun_path) - 1);
487 abstract = GNUNET_CONFIGURATION_get_value_yesno (cfg,
489 "USE_ABSTRACT_SOCKETS");
490 if (GNUNET_YES == abstract)
491 s_un.sun_path[0] = '\0';
494 #if HAVE_SOCKADDR_UN_SUN_LEN
495 s_un.sun_len = (u_char) sizeof (struct sockaddr_un);
497 sock = GNUNET_NETWORK_socket_create (AF_UNIX,
500 if ( (NULL != sock) &&
502 GNUNET_NETWORK_socket_connect (sock,
503 (struct sockaddr *) &s_un,
505 (EINPROGRESS == errno) ) )
507 LOG (GNUNET_ERROR_TYPE_DEBUG,
508 "Successfully connected to unixpath `%s'!\n",
510 GNUNET_free (unixpath);
514 GNUNET_NETWORK_socket_close (sock);
516 GNUNET_free_non_null (unixpath);
523 * Scheduler let us know that we're either ready to write on the
524 * socket OR connect timed out. Do the right thing.
526 * @param cls the `struct AddressProbe *` with the address that we are probing
529 connect_probe_continuation (void *cls)
531 struct AddressProbe *ap = cls;
532 struct ClientState *cstate = ap->cstate;
533 const struct GNUNET_SCHEDULER_TaskContext *tc;
538 GNUNET_assert (NULL != ap->sock);
539 GNUNET_CONTAINER_DLL_remove (cstate->ap_head,
542 len = sizeof (error);
544 tc = GNUNET_SCHEDULER_get_task_context ();
545 if ( (0 == (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) ||
547 GNUNET_NETWORK_socket_getsockopt (ap->sock,
554 GNUNET_break (GNUNET_OK ==
555 GNUNET_NETWORK_socket_close (ap->sock));
557 if ( (NULL == cstate->ap_head) &&
558 // (NULL == cstate->proxy_handshake) &&
559 (NULL == cstate->dns_active) )
560 connect_fail_continuation (cstate);
563 LOG (GNUNET_ERROR_TYPE_DEBUG,
564 "Connection to `%s' succeeded!\n",
565 cstate->service_name);
566 /* trigger jobs that waited for the connection */
567 GNUNET_assert (NULL == cstate->sock);
568 cstate->sock = ap->sock;
571 connect_success_continuation (cstate);
576 * Try to establish a connection given the specified address.
577 * This function is called by the resolver once we have a DNS reply.
579 * @param cls our `struct ClientState *`
580 * @param addr address to try, NULL for "last call"
581 * @param addrlen length of @a addr
584 try_connect_using_address (void *cls,
585 const struct sockaddr *addr,
588 struct ClientState *cstate = cls;
589 struct AddressProbe *ap;
593 cstate->dns_active = NULL;
594 if ( (NULL == cstate->ap_head) &&
595 // (NULL == cstate->proxy_handshake) &&
596 (NULL == cstate->sock) )
597 connect_fail_continuation (cstate);
600 if (NULL != cstate->sock)
601 return; /* already connected */
603 LOG (GNUNET_ERROR_TYPE_DEBUG,
604 "Trying to connect using address `%s:%u'\n",
608 ap = GNUNET_malloc (sizeof (struct AddressProbe) + addrlen);
609 ap->addr = (const struct sockaddr *) &ap[1];
610 GNUNET_memcpy (&ap[1],
613 ap->addrlen = addrlen;
616 switch (ap->addr->sa_family)
619 ((struct sockaddr_in *) ap->addr)->sin_port = htons (cstate->port);
622 ((struct sockaddr_in6 *) ap->addr)->sin6_port = htons (cstate->port);
627 return; /* not supported by us */
629 ap->sock = GNUNET_NETWORK_socket_create (ap->addr->sa_family,
632 if (NULL == ap->sock)
635 return; /* not supported by OS */
638 GNUNET_NETWORK_socket_connect (ap->sock,
641 (EINPROGRESS != errno) )
643 /* maybe refused / unsupported address, try next */
644 GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO,
646 GNUNET_break (GNUNET_OK ==
647 GNUNET_NETWORK_socket_close (ap->sock));
651 GNUNET_CONTAINER_DLL_insert (cstate->ap_head,
654 ap->task = GNUNET_SCHEDULER_add_write_net (GNUNET_CONNECTION_CONNECT_RETRY_TIMEOUT,
656 &connect_probe_continuation,
662 * Test whether the configuration has proper values for connection
663 * (UNIXPATH || (PORT && HOSTNAME)).
665 * @param service_name name of service to connect to
666 * @param cfg configuration to use
667 * @return #GNUNET_OK if the configuration is valid, #GNUNET_SYSERR if not
670 test_service_configuration (const char *service_name,
671 const struct GNUNET_CONFIGURATION_Handle *cfg)
673 int ret = GNUNET_SYSERR;
674 char *hostname = NULL;
675 unsigned long long port;
677 char *unixpath = NULL;
680 GNUNET_CONFIGURATION_get_value_filename (cfg,
684 (0 < strlen (unixpath)))
686 GNUNET_free_non_null (unixpath);
690 GNUNET_CONFIGURATION_have_value (cfg,
694 GNUNET_CONFIGURATION_get_value_number (cfg,
701 GNUNET_CONFIGURATION_get_value_string (cfg,
705 (0 != strlen (hostname)) )
707 GNUNET_free_non_null (hostname);
713 * Try to connect to the service.
715 * @param cls the `struct ClientState` to try to connect to the service
718 start_connect (void *cls)
720 struct ClientState *cstate = cls;
722 cstate->retry_task = NULL;
724 /* Never use a local source if a proxy is configured */
726 GNUNET_SOCKS_check_service (cstate->service_name,
729 socks_connect (cstate);
734 if ( (0 == (cstate->attempts++ % 2)) ||
735 (0 == cstate->port) ||
736 (NULL == cstate->hostname) )
738 /* on even rounds, try UNIX first, or always
739 if we do not have a DNS name and TCP port. */
740 cstate->sock = try_unixpath (cstate->service_name,
742 if (NULL != cstate->sock)
744 connect_success_continuation (cstate);
748 if ( (NULL == cstate->hostname) ||
749 (0 == cstate->port) )
751 /* All options failed. Boo! */
752 connect_fail_continuation (cstate);
756 = GNUNET_RESOLVER_ip_get (cstate->hostname,
758 GNUNET_CONNECTION_CONNECT_RETRY_TIMEOUT,
759 &try_connect_using_address,
765 * Implements the transmission functionality of a message queue.
767 * @param mq the message queue
768 * @param msg the message to send
769 * @param impl_state our `struct ClientState`
772 connection_client_send_impl (struct GNUNET_MQ_Handle *mq,
773 const struct GNUNET_MessageHeader *msg,
776 struct ClientState *cstate = impl_state;
778 /* only one message at a time allowed */
779 GNUNET_assert (NULL == cstate->msg);
780 GNUNET_assert (NULL == cstate->send_task);
783 if (NULL == cstate->sock)
784 return; /* still waiting for connection */
786 = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
794 * Cancel the currently sent message.
796 * @param mq message queue
797 * @param impl_state our `struct ClientState`
800 connection_client_cancel_impl (struct GNUNET_MQ_Handle *mq,
803 struct ClientState *cstate = impl_state;
805 GNUNET_assert (NULL != cstate->msg);
806 GNUNET_assert (0 == cstate->msg_off);
808 if (NULL != cstate->send_task)
810 GNUNET_SCHEDULER_cancel (cstate->send_task);
811 cstate->send_task = NULL;
817 * Create a message queue to connect to a GNUnet service.
818 * If handlers are specfied, receive messages from the connection.
820 * @param cfg our configuration
821 * @param service_name name of the service to connect to
822 * @param handlers handlers for receiving messages, can be NULL
823 * @param error_handler error handler
824 * @param error_handler_cls closure for the @a error_handler
825 * @return the message queue, NULL on error
827 struct GNUNET_MQ_Handle *
828 GNUNET_CLIENT_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
829 const char *service_name,
830 const struct GNUNET_MQ_MessageHandler *handlers,
831 GNUNET_MQ_ErrorHandler error_handler,
832 void *error_handler_cls)
834 struct ClientState *cstate;
837 test_service_configuration (service_name,
840 cstate = GNUNET_new (struct ClientState);
841 cstate->service_name = GNUNET_strdup (service_name);
843 cstate->retry_task = GNUNET_SCHEDULER_add_now (&start_connect,
845 cstate->mst = GNUNET_MST_create (&recv_message,
848 GNUNET_CONFIGURATION_have_value (cfg,
852 if (! ( (GNUNET_OK !=
853 GNUNET_CONFIGURATION_get_value_number (cfg,
857 (cstate->port > 65535) ||
859 GNUNET_CONFIGURATION_get_value_string (cfg,
862 &cstate->hostname)) ) &&
863 (0 == strlen (cstate->hostname)) )
865 GNUNET_free (cstate->hostname);
866 cstate->hostname = NULL;
867 LOG (GNUNET_ERROR_TYPE_WARNING,
868 _("Need a non-empty hostname for service `%s'.\n"),
872 cstate->mq = GNUNET_MQ_queue_for_callbacks (&connection_client_send_impl,
873 &connection_client_destroy_impl,
874 &connection_client_cancel_impl,
882 /* end of client.c */