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 LOG (GNUNET_ERROR_TYPE_WARNING,
217 "Failed to establish connection to `%s', no further addresses to try.\n",
218 cstate->service_name);
219 GNUNET_break (NULL == cstate->ap_head);
220 GNUNET_break (NULL == cstate->ap_tail);
221 GNUNET_break (NULL == cstate->dns_active);
222 GNUNET_break (NULL == cstate->sock);
223 GNUNET_assert (NULL == cstate->send_task);
224 GNUNET_assert (NULL == cstate->recv_task);
225 // GNUNET_assert (NULL == cstate->proxy_handshake);
227 cstate->back_off = GNUNET_TIME_STD_BACKOFF (cstate->back_off);
229 = GNUNET_SCHEDULER_add_delayed (cstate->back_off,
236 * We are ready to send a message to the service.
238 * @param cls the `struct ClientState` with the `msg` to transmit
241 transmit_ready (void *cls)
243 struct ClientState *cstate = cls;
247 int notify_in_flight;
249 cstate->send_task = NULL;
250 pos = (const char *) cstate->msg;
251 len = ntohs (cstate->msg->size);
252 GNUNET_assert (cstate->msg_off < len);
254 ret = GNUNET_NETWORK_socket_send (cstate->sock,
255 &pos[cstate->msg_off],
256 len - cstate->msg_off);
261 GNUNET_MQ_inject_error (cstate->mq,
262 GNUNET_MQ_ERROR_WRITE);
265 notify_in_flight = (0 == cstate->msg_off);
266 cstate->msg_off += ret;
267 if (cstate->msg_off < len)
270 = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
274 if (notify_in_flight)
275 GNUNET_MQ_impl_send_in_flight (cstate->mq);
279 GNUNET_MQ_impl_send_continue (cstate->mq);
284 * We have received a full message, pass to the MQ dispatcher.
285 * Called by the tokenizer via #receive_ready().
287 * @param cls the `struct ClientState`
288 * @param msg message we received.
289 * @return #GNUNET_OK on success, #GNUNET_SYSERR to stop further processing
292 recv_message (void *cls,
293 const struct GNUNET_MessageHeader *msg)
295 struct ClientState *cstate = cls;
297 if (GNUNET_YES == cstate->in_destroy)
298 return GNUNET_SYSERR;
299 GNUNET_MQ_inject_message (cstate->mq,
301 if (GNUNET_YES == cstate->in_destroy)
302 return GNUNET_SYSERR;
308 * Cancel all remaining connect attempts
310 * @param cstate handle of the client state to process
313 cancel_aps (struct ClientState *cstate)
315 struct AddressProbe *pos;
317 while (NULL != (pos = cstate->ap_head))
319 GNUNET_break (GNUNET_OK ==
320 GNUNET_NETWORK_socket_close (pos->sock));
321 GNUNET_SCHEDULER_cancel (pos->task);
322 GNUNET_CONTAINER_DLL_remove (cstate->ap_head,
331 * Implement the destruction of a message queue. Implementations must
332 * not free @a mq, but should take care of @a impl_state.
334 * @param mq the message queue to destroy
335 * @param impl_state our `struct ClientState`
338 connection_client_destroy_impl (struct GNUNET_MQ_Handle *mq,
341 struct ClientState *cstate = impl_state;
343 if (GNUNET_SYSERR == cstate->in_destroy)
345 /* defer destruction */
346 cstate->in_destroy = GNUNET_YES;
350 if (NULL != cstate->dns_active)
351 GNUNET_RESOLVER_request_cancel (cstate->dns_active);
352 if (NULL != cstate->send_task)
353 GNUNET_SCHEDULER_cancel (cstate->send_task);
354 if (NULL != cstate->recv_task)
355 GNUNET_SCHEDULER_cancel (cstate->recv_task);
356 if (NULL != cstate->retry_task)
357 GNUNET_SCHEDULER_cancel (cstate->retry_task);
358 if (NULL != cstate->sock)
359 GNUNET_NETWORK_socket_close (cstate->sock);
361 GNUNET_free (cstate->service_name);
362 GNUNET_free_non_null (cstate->hostname);
363 GNUNET_MST_destroy (cstate->mst);
364 GNUNET_free (cstate);
369 * This function is called once we have data ready to read.
371 * @param cls `struct ClientState` with connection to read from
374 receive_ready (void *cls)
376 struct ClientState *cstate = cls;
379 cstate->recv_task = NULL;
380 cstate->in_destroy = GNUNET_SYSERR;
381 ret = GNUNET_MST_read (cstate->mst,
385 if (GNUNET_SYSERR == ret)
387 if (NULL != cstate->mq)
388 GNUNET_MQ_inject_error (cstate->mq,
389 GNUNET_MQ_ERROR_READ);
390 if (GNUNET_YES == cstate->in_destroy)
391 connection_client_destroy_impl (cstate->mq,
395 if (GNUNET_YES == cstate->in_destroy)
397 connection_client_destroy_impl (cstate->mq,
401 cstate->in_destroy = GNUNET_NO;
403 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
411 * We've succeeded in establishing a connection.
413 * @param cstate the connection we tried to establish
416 connect_success_continuation (struct ClientState *cstate)
418 GNUNET_assert (NULL == cstate->recv_task);
420 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
424 if (NULL != cstate->msg)
426 GNUNET_assert (NULL == cstate->send_task);
428 = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
437 * Try connecting to the server using UNIX domain sockets.
439 * @param service_name name of service to connect to
440 * @param cfg configuration to use
441 * @return NULL on error, socket connected to UNIX otherwise
443 static struct GNUNET_NETWORK_Handle *
444 try_unixpath (const char *service_name,
445 const struct GNUNET_CONFIGURATION_Handle *cfg)
448 struct GNUNET_NETWORK_Handle *sock;
450 struct sockaddr_un s_un;
454 GNUNET_CONFIGURATION_get_value_filename (cfg,
458 (0 < strlen (unixpath)))
460 /* We have a non-NULL unixpath, need to validate it */
461 if (strlen (unixpath) >= sizeof (s_un.sun_path))
463 LOG (GNUNET_ERROR_TYPE_WARNING,
464 _("UNIXPATH `%s' too long, maximum length is %llu\n"),
466 (unsigned long long) sizeof (s_un.sun_path));
467 unixpath = GNUNET_NETWORK_shorten_unixpath (unixpath);
468 LOG (GNUNET_ERROR_TYPE_INFO,
469 _("Using `%s' instead\n"),
471 if (NULL == unixpath)
477 s_un.sun_family = AF_UNIX;
478 strncpy (s_un.sun_path,
480 sizeof (s_un.sun_path) - 1);
485 abstract = GNUNET_CONFIGURATION_get_value_yesno (cfg,
487 "USE_ABSTRACT_SOCKETS");
488 if (GNUNET_YES == abstract)
489 s_un.sun_path[0] = '\0';
492 #if HAVE_SOCKADDR_IN_SIN_LEN
493 un.sun_len = (u_char) sizeof (struct sockaddr_un);
495 sock = GNUNET_NETWORK_socket_create (AF_UNIX,
498 if ( (NULL != sock) &&
500 GNUNET_NETWORK_socket_connect (sock,
501 (struct sockaddr *) &s_un,
503 (EINPROGRESS == errno) ) )
505 LOG (GNUNET_ERROR_TYPE_DEBUG,
506 "Successfully connected to unixpath `%s'!\n",
508 GNUNET_free (unixpath);
512 GNUNET_free_non_null (unixpath);
519 * Scheduler let us know that we're either ready to write on the
520 * socket OR connect timed out. Do the right thing.
522 * @param cls the `struct AddressProbe *` with the address that we are probing
525 connect_probe_continuation (void *cls)
527 struct AddressProbe *ap = cls;
528 struct ClientState *cstate = ap->cstate;
529 const struct GNUNET_SCHEDULER_TaskContext *tc;
534 GNUNET_assert (NULL != ap->sock);
535 GNUNET_CONTAINER_DLL_remove (cstate->ap_head,
538 len = sizeof (error);
540 tc = GNUNET_SCHEDULER_get_task_context ();
541 if ( (0 == (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) ||
543 GNUNET_NETWORK_socket_getsockopt (ap->sock,
550 GNUNET_break (GNUNET_OK ==
551 GNUNET_NETWORK_socket_close (ap->sock));
553 if ( (NULL == cstate->ap_head) &&
554 // (NULL == cstate->proxy_handshake) &&
555 (NULL == cstate->dns_active) )
556 connect_fail_continuation (cstate);
559 LOG (GNUNET_ERROR_TYPE_DEBUG,
560 "Connection to `%s' succeeded!\n",
561 cstate->service_name);
562 /* trigger jobs that waited for the connection */
563 GNUNET_assert (NULL == cstate->sock);
564 cstate->sock = ap->sock;
567 connect_success_continuation (cstate);
572 * Try to establish a connection given the specified address.
573 * This function is called by the resolver once we have a DNS reply.
575 * @param cls our `struct ClientState *`
576 * @param addr address to try, NULL for "last call"
577 * @param addrlen length of @a addr
580 try_connect_using_address (void *cls,
581 const struct sockaddr *addr,
584 struct ClientState *cstate = cls;
585 struct AddressProbe *ap;
589 cstate->dns_active = NULL;
590 if ( (NULL == cstate->ap_head) &&
591 // (NULL == cstate->proxy_handshake) &&
592 (NULL == cstate->sock) )
593 connect_fail_continuation (cstate);
596 if (NULL != cstate->sock)
597 return; /* already connected */
599 LOG (GNUNET_ERROR_TYPE_DEBUG,
600 "Trying to connect using address `%s:%u'\n",
604 ap = GNUNET_malloc (sizeof (struct AddressProbe) + addrlen);
605 ap->addr = (const struct sockaddr *) &ap[1];
606 GNUNET_memcpy (&ap[1],
609 ap->addrlen = addrlen;
612 switch (ap->addr->sa_family)
615 ((struct sockaddr_in *) ap->addr)->sin_port = htons (cstate->port);
618 ((struct sockaddr_in6 *) ap->addr)->sin6_port = htons (cstate->port);
623 return; /* not supported by us */
625 ap->sock = GNUNET_NETWORK_socket_create (ap->addr->sa_family,
628 if (NULL == ap->sock)
631 return; /* not supported by OS */
634 GNUNET_NETWORK_socket_connect (ap->sock,
637 (EINPROGRESS != errno) )
639 /* maybe refused / unsupported address, try next */
640 GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO,
642 GNUNET_break (GNUNET_OK ==
643 GNUNET_NETWORK_socket_close (ap->sock));
647 GNUNET_CONTAINER_DLL_insert (cstate->ap_head,
650 ap->task = GNUNET_SCHEDULER_add_write_net (GNUNET_CONNECTION_CONNECT_RETRY_TIMEOUT,
652 &connect_probe_continuation,
658 * Test whether the configuration has proper values for connection
659 * (UNIXPATH || (PORT && HOSTNAME)).
661 * @param service_name name of service to connect to
662 * @param cfg configuration to use
663 * @return #GNUNET_OK if the configuration is valid, #GNUNET_SYSERR if not
666 test_service_configuration (const char *service_name,
667 const struct GNUNET_CONFIGURATION_Handle *cfg)
669 int ret = GNUNET_SYSERR;
670 char *hostname = NULL;
671 unsigned long long port;
673 char *unixpath = NULL;
676 GNUNET_CONFIGURATION_get_value_filename (cfg,
680 (0 < strlen (unixpath)))
682 GNUNET_free_non_null (unixpath);
686 GNUNET_CONFIGURATION_have_value (cfg,
690 GNUNET_CONFIGURATION_get_value_number (cfg,
697 GNUNET_CONFIGURATION_get_value_string (cfg,
701 (0 != strlen (hostname)) )
703 GNUNET_free_non_null (hostname);
709 * Try to connect to the service.
711 * @param cls the `struct ClientState` to try to connect to the service
714 start_connect (void *cls)
716 struct ClientState *cstate = cls;
718 cstate->retry_task = NULL;
720 /* Never use a local source if a proxy is configured */
722 GNUNET_SOCKS_check_service (cstate->service_name,
725 socks_connect (cstate);
730 if ( (0 == (cstate->attempts++ % 2)) ||
731 (0 == cstate->port) ||
732 (NULL == cstate->hostname) )
734 /* on even rounds, try UNIX first, or always
735 if we do not have a DNS name and TCP port. */
736 cstate->sock = try_unixpath (cstate->service_name,
738 if (NULL != cstate->sock)
740 connect_success_continuation (cstate);
744 if ( (NULL == cstate->hostname) ||
745 (0 == cstate->port) )
747 /* All options failed. Boo! */
748 connect_fail_continuation (cstate);
752 = GNUNET_RESOLVER_ip_get (cstate->hostname,
754 GNUNET_CONNECTION_CONNECT_RETRY_TIMEOUT,
755 &try_connect_using_address,
761 * Implements the transmission functionality of a message queue.
763 * @param mq the message queue
764 * @param msg the message to send
765 * @param impl_state our `struct ClientState`
768 connection_client_send_impl (struct GNUNET_MQ_Handle *mq,
769 const struct GNUNET_MessageHeader *msg,
772 struct ClientState *cstate = impl_state;
774 /* only one message at a time allowed */
775 GNUNET_assert (NULL == cstate->msg);
776 GNUNET_assert (NULL == cstate->send_task);
779 if (NULL == cstate->sock)
780 return; /* still waiting for connection */
782 = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
790 * Cancel the currently sent message.
792 * @param mq message queue
793 * @param impl_state our `struct ClientState`
796 connection_client_cancel_impl (struct GNUNET_MQ_Handle *mq,
799 struct ClientState *cstate = impl_state;
801 GNUNET_assert (NULL != cstate->msg);
802 GNUNET_assert (0 == cstate->msg_off);
804 if (NULL != cstate->send_task)
806 GNUNET_SCHEDULER_cancel (cstate->send_task);
807 cstate->send_task = NULL;
813 * Create a message queue to connect to a GNUnet service.
814 * If handlers are specfied, receive messages from the connection.
816 * @param cfg our configuration
817 * @param service_name name of the service to connect to
818 * @param handlers handlers for receiving messages, can be NULL
819 * @param error_handler error handler
820 * @param error_handler_cls closure for the @a error_handler
821 * @return the message queue, NULL on error
823 struct GNUNET_MQ_Handle *
824 GNUNET_CLIENT_connecT (const struct GNUNET_CONFIGURATION_Handle *cfg,
825 const char *service_name,
826 const struct GNUNET_MQ_MessageHandler *handlers,
827 GNUNET_MQ_ErrorHandler error_handler,
828 void *error_handler_cls)
830 struct ClientState *cstate;
833 test_service_configuration (service_name,
836 cstate = GNUNET_new (struct ClientState);
837 cstate->service_name = GNUNET_strdup (service_name);
839 cstate->retry_task = GNUNET_SCHEDULER_add_now (&start_connect,
841 cstate->mst = GNUNET_MST_create (&recv_message,
844 GNUNET_CONFIGURATION_have_value (cfg,
848 if (! ( (GNUNET_OK !=
849 GNUNET_CONFIGURATION_get_value_number (cfg,
853 (cstate->port > 65535) ||
855 GNUNET_CONFIGURATION_get_value_string (cfg,
858 &cstate->hostname)) ) &&
859 (0 == strlen (cstate->hostname)) )
861 GNUNET_free (cstate->hostname);
862 cstate->hostname = NULL;
863 LOG (GNUNET_ERROR_TYPE_WARNING,
864 _("Need a non-empty hostname for service `%s'.\n"),
868 cstate->mq = GNUNET_MQ_queue_for_callbacks (&connection_client_send_impl,
869 &connection_client_destroy_impl,
870 &connection_client_cancel_impl,
878 /* end of client_new.c */