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 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.
16 * @file gns/gnunet-service-gns.c
17 * @brief GNU Name System (main service)
18 * @author Martin Schanzenbach
19 * @author Christian Grothoff
22 #include "gnunet_util_lib.h"
23 #include "gnunet_dns_service.h"
24 #include "gnunet_dnsparser_lib.h"
25 #include "gnunet_dht_service.h"
26 #include "gnunet_namecache_service.h"
27 #include "gnunet_gnsrecord_lib.h"
28 #include "gnunet_gns_service.h"
29 #include "gnunet_statistics_service.h"
31 #include "gnunet-service-gns_resolver.h"
32 #include "gnunet-service-gns_interceptor.h"
33 #include "gnunet_protocols.h"
42 * Handle to a lookup operation from client via API.
44 struct ClientLookupHandle
48 * We keep these in a DLL.
50 struct ClientLookupHandle *next;
53 * We keep these in a DLL.
55 struct ClientLookupHandle *prev;
63 * Active handle for the lookup.
65 struct GNS_ResolverHandle *lookup;
76 * Information we track per connected client.
83 struct GNUNET_SERVICE_Client *client;
88 struct GNUNET_MQ_Handle *mq;
93 struct ClientLookupHandle *clh_head;
98 struct ClientLookupHandle *clh_tail;
103 * Representation of a TLD, mapping the respective TLD string
104 * (i.e. ".gnu") to the respective public key of the zone.
106 struct GNS_TopLevelDomain
110 * Kept in a DLL, as there are unlikely enough of these to
111 * warrant a hash map.
113 struct GNS_TopLevelDomain *next;
116 * Kept in a DLL, as there are unlikely enough of these to
117 * warrant a hash map.
119 struct GNS_TopLevelDomain *prev;
122 * Public key associated with the @a tld.
124 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
127 * Top-level domain as a string, including leading ".".
135 * Our handle to the DHT
137 static struct GNUNET_DHT_Handle *dht_handle;
140 * Our handle to the namecache service
142 static struct GNUNET_NAMECACHE_Handle *namecache_handle;
145 * #GNUNET_YES if ipv6 is supported
147 static int v6_enabled;
150 * #GNUNET_YES if ipv4 is supported
152 static int v4_enabled;
155 * Handle to the statistics service
157 static struct GNUNET_STATISTICS_Handle *statistics;
160 * Head of DLL of TLDs we map to GNS zones.
162 static struct GNS_TopLevelDomain *tld_head;
165 * Tail of DLL of TLDs we map to GNS zones.
167 static struct GNS_TopLevelDomain *tld_tail;
171 * Find GNS zone belonging to TLD @a tld.
173 * @param tld_str top-level domain to look up
174 * @param[out] pkey public key to set
175 * @return #GNUNET_YES if @a tld was found #GNUNET_NO if not
178 GNS_find_tld (const char *tld_str,
179 struct GNUNET_CRYPTO_EcdsaPublicKey *pkey)
181 if ('\0' == *tld_str)
183 for (struct GNS_TopLevelDomain *tld = tld_head;
187 if (0 == strcasecmp (tld_str,
195 GNUNET_GNSRECORD_zkey_to_pkey (tld_str + 1,
197 return GNUNET_YES; /* TLD string *was* the public key */
203 * Obtain the TLD of the given @a name.
206 * @return the part of @a name after the last ".",
207 * or @a name if @a name does not contain a "."
210 GNS_get_tld (const char *name)
215 (unsigned char) '.');
219 tld++; /* skip the '.' */
225 * Task run during shutdown.
227 * @param cls unused, NULL
230 shutdown_task (void *cls)
232 struct GNS_TopLevelDomain *tld;
235 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
237 GNS_interceptor_done ();
238 GNS_resolver_done ();
239 if (NULL != statistics)
241 GNUNET_STATISTICS_destroy (statistics,
245 if (NULL != namecache_handle)
247 GNUNET_NAMECACHE_disconnect (namecache_handle);
248 namecache_handle = NULL;
250 if (NULL != dht_handle)
252 GNUNET_DHT_disconnect (dht_handle);
255 while (NULL != (tld = tld_head))
257 GNUNET_CONTAINER_DLL_remove (tld_head,
260 GNUNET_free (tld->tld);
267 * Called whenever a client is disconnected.
270 * @param client identification of the client
271 * @param app_ctx @a client
274 client_disconnect_cb (void *cls,
275 struct GNUNET_SERVICE_Client *client,
278 struct ClientLookupHandle *clh;
279 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,
299 * Add a client to our list of active clients.
302 * @param client client to add
303 * @param mq message queue for @a client
304 * @return internal namestore client structure for this client
307 client_connect_cb (void *cls,
308 struct GNUNET_SERVICE_Client *client,
309 struct GNUNET_MQ_Handle *mq)
311 struct GnsClient *gc;
314 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
315 "Client %p connected\n",
317 gc = GNUNET_new (struct GnsClient);
325 * Reply to client with the result from our lookup.
327 * @param cls the closure (our client lookup handle)
328 * @param rd_count the number of records in @a rd
329 * @param rd the record data
332 send_lookup_response (void *cls,
334 const struct GNUNET_GNSRECORD_Data *rd)
336 struct ClientLookupHandle *clh = cls;
337 struct GnsClient *gc = clh->gc;
338 struct GNUNET_MQ_Envelope *env;
339 struct LookupResultMessage *rmsg;
342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
343 "Sending LOOKUP_RESULT message with %u results\n",
344 (unsigned int) rd_count);
345 len = GNUNET_GNSRECORD_records_get_size (rd_count,
350 GNUNET_SERVICE_client_drop (gc->client);
353 if (len > UINT16_MAX - sizeof (*rmsg))
356 GNUNET_SERVICE_client_drop (gc->client);
359 env = GNUNET_MQ_msg_extra (rmsg,
361 GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT);
362 rmsg->id = clh->request_id;
363 rmsg->rd_count = htonl (rd_count);
364 GNUNET_assert (len ==
365 GNUNET_GNSRECORD_records_serialize (rd_count,
369 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (gc->client),
371 GNUNET_CONTAINER_DLL_remove (gc->clh_head,
375 GNUNET_STATISTICS_update (statistics,
376 "Completed lookups", 1,
378 GNUNET_STATISTICS_update (statistics,
386 * Checks a #GNUNET_MESSAGE_TYPE_GNS_LOOKUP message
388 * @param cls client sending the message
389 * @param l_msg message of type `struct LookupMessage`
390 * @return #GNUNET_OK if @a l_msg is well-formed
393 check_lookup (void *cls,
394 const struct LookupMessage *l_msg)
400 msg_size = ntohs (l_msg->header.size);
401 if (msg_size < sizeof (struct LookupMessage))
404 return GNUNET_SYSERR;
406 name = (const char *) &l_msg[1];
407 if ( ('\0' != name[msg_size - sizeof (struct LookupMessage) - 1]) ||
408 (strlen (name) > GNUNET_DNSPARSER_MAX_NAME_LENGTH) )
411 return GNUNET_SYSERR;
418 * Handle lookup requests from client
420 * @param cls the closure
421 * @param client the client
422 * @param message the message
425 handle_lookup (void *cls,
426 const struct LookupMessage *sh_msg)
428 struct GnsClient *gc = cls;
429 char name[GNUNET_DNSPARSER_MAX_NAME_LENGTH + 1];
430 struct ClientLookupHandle *clh;
431 char *nameptr = name;
434 GNUNET_SERVICE_client_continue (gc->client);
435 utf_in = (const char *) &sh_msg[1];
436 GNUNET_STRINGS_utf8_tolower (utf_in,
438 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
439 "Received LOOKUP `%s' message\n",
441 clh = GNUNET_new (struct ClientLookupHandle);
442 GNUNET_CONTAINER_DLL_insert (gc->clh_head,
446 clh->request_id = sh_msg->id;
447 if ( (GNUNET_DNSPARSER_TYPE_A == ntohl (sh_msg->type)) &&
448 (GNUNET_OK != v4_enabled) )
450 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
451 "LOOKUP: Query for A record but AF_INET not supported!");
452 send_lookup_response (clh,
457 if ( (GNUNET_DNSPARSER_TYPE_AAAA == ntohl (sh_msg->type)) &&
458 (GNUNET_OK != v6_enabled) )
460 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
461 "LOOKUP: Query for AAAA record but AF_INET6 not supported!");
462 send_lookup_response (clh,
467 clh->lookup = GNS_resolver_lookup (&sh_msg->zone,
468 ntohl (sh_msg->type),
470 (enum GNUNET_GNS_LocalOptions) ntohs (sh_msg->options),
471 &send_lookup_response, clh);
472 GNUNET_STATISTICS_update (statistics,
479 * Reads the configuration and populates TLDs
482 * @param section name of section in config, always "gns"
483 * @param option name of the option, TLDs start with "."
484 * @param value value for the option, public key for TLDs
487 read_service_conf (void *cls,
492 struct GNUNET_CRYPTO_EcdsaPublicKey pk;
493 struct GNS_TopLevelDomain *tld;
497 if (option[0] != '.')
500 GNUNET_STRINGS_string_to_data (value,
505 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
508 _("Properly base32-encoded public key required"));
511 tld = GNUNET_new (struct GNS_TopLevelDomain);
512 tld->tld = GNUNET_strdup (&option[1]);
514 GNUNET_CONTAINER_DLL_insert (tld_head,
521 * Process GNS requests.
524 * @param server the initialized server
525 * @param c configuration to use
529 const struct GNUNET_CONFIGURATION_Handle *c,
530 struct GNUNET_SERVICE_Handle *service)
532 unsigned long long max_parallel_bg_queries = 16;
534 GNUNET_CONFIGURATION_iterate_section_values (c,
538 v6_enabled = GNUNET_NETWORK_test_pf (PF_INET6);
539 v4_enabled = GNUNET_NETWORK_test_pf (PF_INET);
540 namecache_handle = GNUNET_NAMECACHE_connect (c);
541 if (NULL == namecache_handle)
543 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
544 _("Failed to connect to the namecache!\n"));
545 GNUNET_SCHEDULER_shutdown ();
549 GNUNET_CONFIGURATION_get_value_number (c,
551 "MAX_PARALLEL_BACKGROUND_QUERIES",
552 &max_parallel_bg_queries))
554 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
555 "Number of allowed parallel background queries: %llu\n",
556 max_parallel_bg_queries);
558 dht_handle = GNUNET_DHT_connect (c,
559 (unsigned int) max_parallel_bg_queries);
560 if (NULL == dht_handle)
562 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
563 _("Could not connect to DHT!\n"));
564 GNUNET_SCHEDULER_add_now (&shutdown_task,
568 GNS_resolver_init (namecache_handle,
571 max_parallel_bg_queries);
573 GNUNET_CONFIGURATION_get_value_yesno (c,
577 GNS_interceptor_init (c)) )
580 GNUNET_SCHEDULER_add_now (&shutdown_task,
584 statistics = GNUNET_STATISTICS_create ("gns",
586 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
592 * Define "main" method using service macro.
596 GNUNET_SERVICE_OPTION_NONE,
599 &client_disconnect_cb,
601 GNUNET_MQ_hd_var_size (lookup,
602 GNUNET_MESSAGE_TYPE_GNS_LOOKUP,
603 struct LookupMessage,
605 GNUNET_MQ_handler_end());
608 /* end of gnunet-service-gns.c */