2 This file is part of GNUnet.
3 Copyright (C) 2011-2018 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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_gnsrecord_lib.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 client via API.
49 struct ClientLookupHandle
52 * We keep these in a DLL.
54 struct ClientLookupHandle *next;
57 * We keep these in a DLL.
59 struct ClientLookupHandle *prev;
67 * Active handle for the lookup.
69 struct GNS_ResolverHandle *lookup;
79 * Information we track per connected client.
86 struct GNUNET_SERVICE_Client *client;
91 struct GNUNET_MQ_Handle *mq;
96 struct ClientLookupHandle *clh_head;
101 struct ClientLookupHandle *clh_tail;
106 * Representation of a TLD, mapping the respective TLD string
107 * (i.e. ".gnu") to the respective public key of the zone.
109 struct GNS_TopLevelDomain
112 * Kept in a DLL, as there are unlikely enough of these to
113 * warrant a hash map.
115 struct GNS_TopLevelDomain *next;
118 * Kept in a DLL, as there are unlikely enough of these to
119 * warrant a hash map.
121 struct GNS_TopLevelDomain *prev;
124 * Public key associated with the @a tld.
126 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
129 * 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 * #GNUNET_YES if ipv6 is supported
148 static int v6_enabled;
151 * #GNUNET_YES if ipv4 is supported
153 static int v4_enabled;
156 * Handle to the statistics service
158 static struct GNUNET_STATISTICS_Handle *statistics;
161 * Head of DLL of TLDs we map to GNS zones.
163 static struct GNS_TopLevelDomain *tld_head;
166 * Tail of DLL of TLDs we map to GNS zones.
168 static struct GNS_TopLevelDomain *tld_tail;
172 * Find GNS zone belonging to TLD @a tld.
174 * @param tld_str top-level domain to look up
175 * @param[out] pkey public key to set
176 * @return #GNUNET_YES if @a tld was found #GNUNET_NO if not
179 GNS_find_tld (const char *tld_str,
180 struct GNUNET_CRYPTO_EcdsaPublicKey *pkey)
182 if ('\0' == *tld_str)
184 for (struct GNS_TopLevelDomain *tld = tld_head;
188 if (0 == strcasecmp (tld_str,
196 GNUNET_GNSRECORD_zkey_to_pkey (tld_str + 1,
198 return GNUNET_YES; /* TLD string *was* the public key */
204 * Obtain the TLD of the given @a name.
207 * @return the part of @a name after the last ".",
208 * or @a name if @a name does not contain a "."
211 GNS_get_tld (const char *name)
216 (unsigned char) '.');
220 tld++; /* skip the '.' */
226 * Task run during shutdown.
228 * @param cls unused, NULL
231 shutdown_task (void *cls)
233 struct GNS_TopLevelDomain *tld;
236 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
238 GNS_interceptor_done ();
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;
283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
284 "Client %p disconnected\n",
286 while (NULL != (clh = gc->clh_head))
288 if (NULL != clh->lookup)
289 GNS_resolver_lookup_cancel (clh->lookup);
290 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;
315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
316 "Client %p connected\n",
318 gc = GNUNET_new (struct GnsClient);
326 * Reply to client with the result from our lookup.
328 * @param cls the closure (our client lookup handle)
329 * @param rd_count the number of records in @a rd
330 * @param rd the record data
333 send_lookup_response (void *cls,
335 const struct GNUNET_GNSRECORD_Data *rd)
337 struct ClientLookupHandle *clh = cls;
338 struct GnsClient *gc = clh->gc;
339 struct GNUNET_MQ_Envelope *env;
340 struct LookupResultMessage *rmsg;
343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
344 "Sending LOOKUP_RESULT message with %u results\n",
345 (unsigned int) rd_count);
346 len = GNUNET_GNSRECORD_records_get_size (rd_count,
351 GNUNET_SERVICE_client_drop (gc->client);
354 if (len > UINT16_MAX - sizeof(*rmsg))
357 GNUNET_SERVICE_client_drop (gc->client);
360 env = GNUNET_MQ_msg_extra (rmsg,
362 GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT);
363 rmsg->id = clh->request_id;
364 rmsg->rd_count = htonl (rd_count);
365 GNUNET_assert (len ==
366 GNUNET_GNSRECORD_records_serialize (rd_count,
370 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (gc->client),
372 GNUNET_CONTAINER_DLL_remove (gc->clh_head,
376 GNUNET_STATISTICS_update (statistics,
377 "Completed lookups", 1,
379 GNUNET_STATISTICS_update (statistics,
387 * Checks a #GNUNET_MESSAGE_TYPE_GNS_LOOKUP message
389 * @param cls client sending the message
390 * @param l_msg message of type `struct LookupMessage`
391 * @return #GNUNET_OK if @a l_msg is well-formed
394 check_lookup (void *cls,
395 const struct LookupMessage *l_msg)
400 GNUNET_MQ_check_zero_termination (l_msg);
401 nlen = ntohs (l_msg->header.size) - sizeof(struct LookupMessage);
402 if (nlen > GNUNET_DNSPARSER_MAX_NAME_LENGTH)
405 return GNUNET_SYSERR;
412 * Handle lookup requests from client
414 * @param cls the closure
415 * @param client the client
416 * @param message the message
419 handle_lookup (void *cls,
420 const struct LookupMessage *sh_msg)
422 struct GnsClient *gc = cls;
423 char name[GNUNET_DNSPARSER_MAX_NAME_LENGTH + 1];
424 struct ClientLookupHandle *clh;
425 char *nameptr = name;
428 GNUNET_SERVICE_client_continue (gc->client);
429 utf_in = (const char *) &sh_msg[1];
430 GNUNET_STRINGS_utf8_tolower (utf_in,
432 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
433 "Received LOOKUP `%s' message\n",
435 clh = GNUNET_new (struct ClientLookupHandle);
436 GNUNET_CONTAINER_DLL_insert (gc->clh_head,
440 clh->request_id = sh_msg->id;
441 if ((GNUNET_DNSPARSER_TYPE_A == ntohl (sh_msg->type)) &&
442 (GNUNET_OK != v4_enabled))
444 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
445 "LOOKUP: Query for A record but AF_INET not supported!");
446 send_lookup_response (clh,
451 if ((GNUNET_DNSPARSER_TYPE_AAAA == ntohl (sh_msg->type)) &&
452 (GNUNET_OK != v6_enabled))
454 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
455 "LOOKUP: Query for AAAA record but AF_INET6 not supported!");
456 send_lookup_response (clh,
461 clh->lookup = GNS_resolver_lookup (&sh_msg->zone,
462 ntohl (sh_msg->type),
464 (enum GNUNET_GNS_LocalOptions) ntohs (
466 ntohs (sh_msg->recursion_depth_limit),
467 &send_lookup_response, clh);
468 GNUNET_STATISTICS_update (statistics,
475 * Reads the configuration and populates TLDs
478 * @param section name of section in config, always "gns"
479 * @param option name of the option, TLDs start with "."
480 * @param value value for the option, public key for TLDs
483 read_service_conf (void *cls,
488 struct GNUNET_CRYPTO_EcdsaPublicKey pk;
489 struct GNS_TopLevelDomain *tld;
493 if (option[0] != '.')
496 GNUNET_STRINGS_string_to_data (value,
501 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
505 "Properly base32-encoded public key required"));
508 tld = GNUNET_new (struct GNS_TopLevelDomain);
509 tld->tld = GNUNET_strdup (&option[1]);
511 GNUNET_CONTAINER_DLL_insert (tld_head,
518 * Process GNS requests.
521 * @param server the initialized server
522 * @param c configuration to use
526 const struct GNUNET_CONFIGURATION_Handle *c,
527 struct GNUNET_SERVICE_Handle *service)
529 unsigned long long max_parallel_bg_queries = 16;
531 GNUNET_CONFIGURATION_iterate_section_values (c,
535 v6_enabled = GNUNET_NETWORK_test_pf (PF_INET6);
536 v4_enabled = GNUNET_NETWORK_test_pf (PF_INET);
537 namecache_handle = GNUNET_NAMECACHE_connect (c);
538 if (NULL == namecache_handle)
540 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
541 _ ("Failed to connect to the namecache!\n"));
542 GNUNET_SCHEDULER_shutdown ();
546 GNUNET_CONFIGURATION_get_value_number (c,
548 "MAX_PARALLEL_BACKGROUND_QUERIES",
549 &max_parallel_bg_queries))
551 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
552 "Number of allowed parallel background queries: %llu\n",
553 max_parallel_bg_queries);
555 dht_handle = GNUNET_DHT_connect (c,
556 (unsigned int) max_parallel_bg_queries);
557 if (NULL == dht_handle)
559 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
560 _ ("Could not connect to DHT!\n"));
561 GNUNET_SCHEDULER_add_now (&shutdown_task,
565 GNS_resolver_init (namecache_handle,
568 max_parallel_bg_queries);
570 GNUNET_CONFIGURATION_get_value_yesno (c,
574 GNS_interceptor_init (c)))
577 GNUNET_SCHEDULER_add_now (&shutdown_task,
581 statistics = GNUNET_STATISTICS_create ("gns",
583 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
589 * Define "main" method using service macro.
593 GNUNET_SERVICE_OPTION_NONE,
596 &client_disconnect_cb,
598 GNUNET_MQ_hd_var_size (lookup,
599 GNUNET_MESSAGE_TYPE_GNS_LOOKUP,
600 struct LookupMessage,
602 GNUNET_MQ_handler_end ());
605 /* end of gnunet-service-gns.c */