2 This file is part of GNUnet.
3 Copyright (C) 2011-2013 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.
21 * @file gns/gnunet-service-gns.c
22 * @brief GNU Name System (main service)
23 * @author Martin Schanzenbach
24 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
28 #include "gnunet_dns_service.h"
29 #include "gnunet_dnsparser_lib.h"
30 #include "gnunet_dht_service.h"
31 #include "gnunet_namecache_service.h"
32 #include "gnunet_identity_service.h"
33 #include "gnunet_gns_service.h"
34 #include "gnunet_statistics_service.h"
36 #include "gnunet-service-gns_resolver.h"
37 #include "gnunet-service-gns_interceptor.h"
38 #include "gnunet_protocols.h"
47 * Handle to a lookup operation from api
49 struct ClientLookupHandle
53 * We keep these in a DLL.
55 struct ClientLookupHandle *next;
58 * We keep these in a DLL.
60 struct ClientLookupHandle *prev;
68 * Active handle for the lookup.
70 struct GNS_ResolverHandle *lookup;
84 struct GNUNET_SERVICE_Client *client;
89 struct GNUNET_MQ_Handle *mq;
94 struct ClientLookupHandle *clh_head;
99 struct ClientLookupHandle *clh_tail;
104 * Representation of a TLD, mapping the respective TLD string
105 * (i.e. ".gnu") to the respective public key of the zone.
107 struct GNS_TopLevelDomain
111 * Kept in a DLL, as there are unlikely enough of these to
112 * warrant a hash map.
114 struct GNS_TopLevelDomain *next;
117 * Kept in a DLL, as there are unlikely enough of these to
118 * warrant a hash map.
120 struct GNS_TopLevelDomain *prev;
123 * Public key associated with the @a tld.
125 struct GNUNET_CRYPTO_EddsaPublicKey pkey;
128 * Top-level domain as a string, including leading ".".
136 * Our handle to the DHT
138 static struct GNUNET_DHT_Handle *dht_handle;
141 * Our handle to the namecache service
143 static struct GNUNET_NAMECACHE_Handle *namecache_handle;
146 * Our handle to the identity service
148 static struct GNUNET_IDENTITY_Handle *identity_handle;
151 * Our handle to the identity operation to find the master zone
152 * for intercepted queries.
154 static struct GNUNET_IDENTITY_Operation *identity_op;
157 * #GNUNET_YES if ipv6 is supported
159 static int v6_enabled;
162 * #GNUNET_YES if ipv4 is supported
164 static int v4_enabled;
167 * Handle to the statistics service
169 static struct GNUNET_STATISTICS_Handle *statistics;
172 * Head of DLL of TLDs we map to GNS zones.
174 static struct GNS_TopLevelDomain *tld_head;
177 * Tail of DLL of TLDs we map to GNS zones.
179 static struct GNS_TopLevelDomain *tld_tail;
183 * Find GNS zone belonging to TLD @a tld.
185 * @param tld_str top-level domain to look up
186 * @param[out] pkey public key to set
187 * @return #GNUNET_YES if @a tld was found #GNUNET_NO if not
190 GNS_find_tld (const char *tld_str,
191 struct GNUNET_CRYPTO_EddsaPublicKey *pkey)
193 if ('\0' == *tld_str)
195 for (struct GNS_TopLevelDomain *tld = tld_head;
199 if (0 == strcasecmp (tld_str,
207 GNUNET_STRINGS_string_to_data (tld_str + 1,
208 strlen (tld_str + 1),
211 return GNUNET_YES; /* TLD string *was* the public key */
217 * Task run during shutdown.
223 shutdown_task (void *cls)
225 struct GNS_TopLevelDomain *tld;
226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
228 GNS_interceptor_done ();
229 if (NULL != identity_op)
231 GNUNET_IDENTITY_cancel (identity_op);
234 if (NULL != identity_handle)
236 GNUNET_IDENTITY_disconnect (identity_handle);
237 identity_handle = NULL;
239 GNS_resolver_done ();
240 if (NULL != statistics)
242 GNUNET_STATISTICS_destroy (statistics,
246 if (NULL != namecache_handle)
248 GNUNET_NAMECACHE_disconnect (namecache_handle);
249 namecache_handle = NULL;
251 if (NULL != dht_handle)
253 GNUNET_DHT_disconnect (dht_handle);
256 while (NULL != (tld = tld_head))
258 GNUNET_CONTAINER_DLL_remove (tld_head,
261 GNUNET_free (tld->tld);
268 * Called whenever a client is disconnected.
271 * @param client identification of the client
272 * @param app_ctx @a client
275 client_disconnect_cb (void *cls,
276 struct GNUNET_SERVICE_Client *client,
279 struct ClientLookupHandle *clh;
280 struct GnsClient *gc = app_ctx;
282 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
283 "Client %p disconnected\n",
285 while (NULL != (clh = gc->clh_head))
287 if (NULL != clh->lookup)
288 GNS_resolver_lookup_cancel (clh->lookup);
289 GNUNET_CONTAINER_DLL_remove (gc->clh_head,
300 * Add a client to our list of active clients.
303 * @param client client to add
304 * @param mq message queue for @a client
305 * @return internal namestore client structure for this client
308 client_connect_cb (void *cls,
309 struct GNUNET_SERVICE_Client *client,
310 struct GNUNET_MQ_Handle *mq)
312 struct GnsClient *gc;
313 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
314 "Client %p connected\n",
316 gc = GNUNET_new (struct GnsClient);
324 * Reply to client with the result from our lookup.
326 * @param cls the closure (our client lookup handle)
327 * @param rd_count the number of records in @a rd
328 * @param rd the record data
331 send_lookup_response (void* cls,
333 const struct GNUNET_GNSRECORD_Data *rd)
335 struct ClientLookupHandle *clh = cls;
336 struct GNUNET_MQ_Envelope *env;
337 struct LookupResultMessage *rmsg;
340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
341 "Sending LOOKUP_RESULT message with %u results\n",
342 (unsigned int) rd_count);
344 len = GNUNET_GNSRECORD_records_get_size (rd_count, rd);
345 env = GNUNET_MQ_msg_extra (rmsg,
347 GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT);
348 rmsg->id = clh->request_id;
349 rmsg->rd_count = htonl (rd_count);
350 GNUNET_GNSRECORD_records_serialize (rd_count, rd, len,
352 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq(clh->gc->client),
354 GNUNET_CONTAINER_DLL_remove (clh->gc->clh_head,
358 GNUNET_STATISTICS_update (statistics,
359 "Completed lookups", 1,
361 GNUNET_STATISTICS_update (statistics,
369 * Checks a #GNUNET_MESSAGE_TYPE_GNS_LOOKUP message
371 * @param cls client sending the message
372 * @param l_msg message of type `struct LookupMessage`
373 * @return #GNUNET_OK if @a l_msg is well-formed
376 check_lookup (void *cls,
377 const struct LookupMessage *l_msg)
382 msg_size = ntohs (l_msg->header.size);
383 if (msg_size < sizeof (struct LookupMessage))
386 return GNUNET_SYSERR;
388 name = (const char *) &l_msg[1];
389 if ( ('\0' != name[msg_size - sizeof (struct LookupMessage) - 1]) ||
390 (strlen (name) > GNUNET_DNSPARSER_MAX_NAME_LENGTH) )
393 return GNUNET_SYSERR;
400 * Handle lookup requests from client
402 * @param cls the closure
403 * @param client the client
404 * @param message the message
407 handle_lookup (void *cls,
408 const struct LookupMessage *sh_msg)
410 struct GnsClient *gc = cls;
411 char name[GNUNET_DNSPARSER_MAX_NAME_LENGTH + 1];
412 struct ClientLookupHandle *clh;
413 char *nameptr = name;
416 GNUNET_SERVICE_client_continue (gc->client);
417 utf_in = (const char *) &sh_msg[1];
418 GNUNET_STRINGS_utf8_tolower (utf_in, nameptr);
419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
420 "Received LOOKUP `%s' message\n",
423 clh = GNUNET_new (struct ClientLookupHandle);
424 GNUNET_CONTAINER_DLL_insert (gc->clh_head,
428 clh->request_id = sh_msg->id;
429 if ( (GNUNET_DNSPARSER_TYPE_A == ntohl (sh_msg->type)) &&
430 (GNUNET_OK != v4_enabled) )
432 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
433 "LOOKUP: Query for A record but AF_INET not supported!");
434 send_lookup_response (clh, 0, NULL);
437 if ( (GNUNET_DNSPARSER_TYPE_AAAA == ntohl (sh_msg->type)) &&
438 (GNUNET_OK != v6_enabled) )
440 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
441 "LOOKUP: Query for AAAA record but AF_INET6 not supported!");
442 send_lookup_response (clh, 0, NULL);
445 clh->lookup = GNS_resolver_lookup (&sh_msg->zone,
446 ntohl (sh_msg->type),
448 (enum GNUNET_GNS_LocalOptions) ntohs (sh_msg->options),
449 &send_lookup_response, clh);
450 GNUNET_STATISTICS_update (statistics,
457 * Method called to inform about the ego to be used for the master zone
458 * for DNS interceptions.
460 * This function is only called ONCE, and 'NULL' being passed in
461 * @a ego does indicate that interception is not configured.
462 * If @a ego is non-NULL, we should start to intercept DNS queries
463 * and resolve ".gnu" queries using the given ego as the master zone.
465 * @param cls closure, our `const struct GNUNET_CONFIGURATION_Handle *c`
466 * @param ego ego handle
467 * @param ctx context for application to store data for this ego
468 * (during the lifetime of this process, initially NULL)
469 * @param name name assigned by the user for this ego,
470 * NULL if the user just deleted the ego and it
471 * must thus no longer be used
474 identity_intercept_cb (void *cls,
475 struct GNUNET_IDENTITY_Ego *ego,
479 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
480 struct GNUNET_CRYPTO_EcdsaPublicKey dns_root;
483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
484 "Looking for gns-intercept ego\n");
487 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
488 _("No ego configured for `%s`\n"),
493 GNUNET_IDENTITY_ego_get_public_key (ego,
496 GNS_interceptor_init (&dns_root,
500 GNUNET_SCHEDULER_add_now (&shutdown_task,
508 * Reads the configuration and populates TLDs
511 * @param section name of section in config, always "gns"
512 * @param option name of the option, TLDs start with "."
513 * @param value value for the option, public key for TLDs
516 read_service_conf (void *cls,
521 struct GNUNET_CRYPTO_EddsaPublicKey pk;
522 struct GNS_TopLevelDomain *tld;
524 if (option[0] != '.')
527 GNUNET_STRINGS_string_to_data (value,
532 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
535 _("Properly base32-encoded public key required"));
538 tld = GNUNET_new (struct GNS_TopLevelDomain);
539 tld->tld = GNUNET_strdup (option);
541 GNUNET_CONTAINER_DLL_insert (tld_head,
549 * Process GNS requests.
552 * @param server the initialized server
553 * @param c configuration to use
557 const struct GNUNET_CONFIGURATION_Handle *c,
558 struct GNUNET_SERVICE_Handle *service)
560 unsigned long long max_parallel_bg_queries = 16;
562 GNUNET_CONFIGURATION_iterate_section_values (c,
566 v6_enabled = GNUNET_NETWORK_test_pf (PF_INET6);
567 v4_enabled = GNUNET_NETWORK_test_pf (PF_INET);
568 namecache_handle = GNUNET_NAMECACHE_connect (c);
569 if (NULL == namecache_handle)
571 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
572 _("Failed to connect to the namecache!\n"));
573 GNUNET_SCHEDULER_shutdown ();
577 GNUNET_CONFIGURATION_get_value_number (c,
579 "MAX_PARALLEL_BACKGROUND_QUERIES",
580 &max_parallel_bg_queries))
582 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
583 "Number of allowed parallel background queries: %llu\n",
584 max_parallel_bg_queries);
586 dht_handle = GNUNET_DHT_connect (c,
587 (unsigned int) max_parallel_bg_queries);
588 if (NULL == dht_handle)
590 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
591 _("Could not connect to DHT!\n"));
592 GNUNET_SCHEDULER_add_now (&shutdown_task,
597 identity_handle = GNUNET_IDENTITY_connect (c,
600 if (NULL == identity_handle)
602 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
603 "Could not connect to identity service!\n");
607 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
608 "Looking for gns-intercept ego\n");
609 identity_op = GNUNET_IDENTITY_get (identity_handle,
611 &identity_intercept_cb,
614 GNS_resolver_init (namecache_handle,
617 max_parallel_bg_queries);
618 statistics = GNUNET_STATISTICS_create ("gns", c);
619 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
625 * Define "main" method using service macro.
629 GNUNET_SERVICE_OPTION_NONE,
632 &client_disconnect_cb,
634 GNUNET_MQ_hd_var_size (lookup,
635 GNUNET_MESSAGE_TYPE_GNS_LOOKUP,
636 struct LookupMessage,
638 GNUNET_MQ_handler_end());
641 /* end of gnunet-service-gns.c */