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 * Our handle to the DHT
106 static struct GNUNET_DHT_Handle *dht_handle;
109 * Our handle to the namecache service
111 static struct GNUNET_NAMECACHE_Handle *namecache_handle;
114 * Our handle to the identity service
116 static struct GNUNET_IDENTITY_Handle *identity_handle;
119 * Our handle to the identity operation to find the master zone
120 * for intercepted queries.
122 static struct GNUNET_IDENTITY_Operation *identity_op;
125 * #GNUNET_YES if ipv6 is supported
127 static int v6_enabled;
130 * #GNUNET_YES if ipv4 is supported
132 static int v4_enabled;
135 * Handle to the statistics service
137 static struct GNUNET_STATISTICS_Handle *statistics;
141 * Task run during shutdown.
147 shutdown_task (void *cls)
149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
151 GNS_interceptor_done ();
152 if (NULL != identity_op)
154 GNUNET_IDENTITY_cancel (identity_op);
157 if (NULL != identity_handle)
159 GNUNET_IDENTITY_disconnect (identity_handle);
160 identity_handle = NULL;
162 GNS_resolver_done ();
163 if (NULL != statistics)
165 GNUNET_STATISTICS_destroy (statistics,
169 if (NULL != namecache_handle)
171 GNUNET_NAMECACHE_disconnect (namecache_handle);
172 namecache_handle = NULL;
174 if (NULL != dht_handle)
176 GNUNET_DHT_disconnect (dht_handle);
183 * Called whenever a client is disconnected.
186 * @param client identification of the client
187 * @param app_ctx @a client
190 client_disconnect_cb (void *cls,
191 struct GNUNET_SERVICE_Client *client,
194 struct ClientLookupHandle *clh;
195 struct GnsClient *gc = app_ctx;
197 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
198 "Client %p disconnected\n",
200 while (NULL != (clh = gc->clh_head))
202 if (NULL != clh->lookup)
203 GNS_resolver_lookup_cancel (clh->lookup);
204 GNUNET_CONTAINER_DLL_remove (gc->clh_head,
215 * Add a client to our list of active clients.
218 * @param client client to add
219 * @param mq message queue for @a client
220 * @return internal namestore client structure for this client
223 client_connect_cb (void *cls,
224 struct GNUNET_SERVICE_Client *client,
225 struct GNUNET_MQ_Handle *mq)
227 struct GnsClient *gc;
228 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
229 "Client %p connected\n",
231 gc = GNUNET_new (struct GnsClient);
239 * Reply to client with the result from our lookup.
241 * @param cls the closure (our client lookup handle)
242 * @param rd_count the number of records in @a rd
243 * @param rd the record data
246 send_lookup_response (void* cls,
248 const struct GNUNET_GNSRECORD_Data *rd)
250 struct ClientLookupHandle *clh = cls;
251 struct GNUNET_MQ_Envelope *env;
252 struct LookupResultMessage *rmsg;
255 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
256 "Sending LOOKUP_RESULT message with %u results\n",
257 (unsigned int) rd_count);
259 len = GNUNET_GNSRECORD_records_get_size (rd_count, rd);
260 env = GNUNET_MQ_msg_extra (rmsg,
262 GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT);
263 rmsg->id = clh->request_id;
264 rmsg->rd_count = htonl (rd_count);
265 GNUNET_GNSRECORD_records_serialize (rd_count, rd, len,
267 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq(clh->gc->client),
269 GNUNET_CONTAINER_DLL_remove (clh->gc->clh_head,
273 GNUNET_STATISTICS_update (statistics,
274 "Completed lookups", 1,
276 GNUNET_STATISTICS_update (statistics,
284 * Checks a #GNUNET_MESSAGE_TYPE_GNS_LOOKUP message
286 * @param cls client sending the message
287 * @param l_msg message of type `struct LookupMessage`
288 * @return #GNUNET_OK if @a l_msg is well-formed
291 check_lookup (void *cls,
292 const struct LookupMessage *l_msg)
297 msg_size = ntohs (l_msg->header.size);
298 if (msg_size < sizeof (struct LookupMessage))
301 return GNUNET_SYSERR;
303 name = (const char *) &l_msg[1];
304 if ( ('\0' != name[msg_size - sizeof (struct LookupMessage) - 1]) ||
305 (strlen (name) > GNUNET_DNSPARSER_MAX_NAME_LENGTH) )
308 return GNUNET_SYSERR;
315 * Handle lookup requests from client
317 * @param cls the closure
318 * @param client the client
319 * @param message the message
322 handle_lookup (void *cls,
323 const struct LookupMessage *sh_msg)
325 struct GnsClient *gc = cls;
326 char name[GNUNET_DNSPARSER_MAX_NAME_LENGTH + 1];
327 struct ClientLookupHandle *clh;
328 char *nameptr = name;
331 GNUNET_SERVICE_client_continue (gc->client);
332 utf_in = (const char *) &sh_msg[1];
333 GNUNET_STRINGS_utf8_tolower (utf_in, nameptr);
334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
335 "Received LOOKUP `%s' message\n",
338 clh = GNUNET_new (struct ClientLookupHandle);
339 GNUNET_CONTAINER_DLL_insert (gc->clh_head,
343 clh->request_id = sh_msg->id;
344 if ( (GNUNET_DNSPARSER_TYPE_A == ntohl (sh_msg->type)) &&
345 (GNUNET_OK != v4_enabled) )
347 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
348 "LOOKUP: Query for A record but AF_INET not supported!");
349 send_lookup_response (clh, 0, NULL);
352 if ( (GNUNET_DNSPARSER_TYPE_AAAA == ntohl (sh_msg->type)) &&
353 (GNUNET_OK != v6_enabled) )
355 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
356 "LOOKUP: Query for AAAA record but AF_INET6 not supported!");
357 send_lookup_response (clh, 0, NULL);
360 clh->lookup = GNS_resolver_lookup (&sh_msg->zone,
361 ntohl (sh_msg->type),
363 (enum GNUNET_GNS_LocalOptions) ntohs (sh_msg->options),
364 &send_lookup_response, clh);
365 GNUNET_STATISTICS_update (statistics,
372 * Method called to inform about the ego to be used for the master zone
373 * for DNS interceptions.
375 * This function is only called ONCE, and 'NULL' being passed in
376 * @a ego does indicate that interception is not configured.
377 * If @a ego is non-NULL, we should start to intercept DNS queries
378 * and resolve ".gnu" queries using the given ego as the master zone.
380 * @param cls closure, our `const struct GNUNET_CONFIGURATION_Handle *c`
381 * @param ego ego handle
382 * @param ctx context for application to store data for this ego
383 * (during the lifetime of this process, initially NULL)
384 * @param name name assigned by the user for this ego,
385 * NULL if the user just deleted the ego and it
386 * must thus no longer be used
389 identity_intercept_cb (void *cls,
390 struct GNUNET_IDENTITY_Ego *ego,
394 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
395 struct GNUNET_CRYPTO_EcdsaPublicKey dns_root;
398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
399 "Looking for gns-intercept ego\n");
402 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
403 _("No ego configured for `%s`\n"),
408 GNUNET_IDENTITY_ego_get_public_key (ego,
411 GNS_interceptor_init (&dns_root,
415 GNUNET_SCHEDULER_add_now (&shutdown_task,
423 * Process GNS requests.
426 * @param server the initialized server
427 * @param c configuration to use
431 const struct GNUNET_CONFIGURATION_Handle *c,
432 struct GNUNET_SERVICE_Handle *service)
434 unsigned long long max_parallel_bg_queries = 16;
436 v6_enabled = GNUNET_NETWORK_test_pf (PF_INET6);
437 v4_enabled = GNUNET_NETWORK_test_pf (PF_INET);
438 namecache_handle = GNUNET_NAMECACHE_connect (c);
439 if (NULL == namecache_handle)
441 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
442 _("Failed to connect to the namecache!\n"));
443 GNUNET_SCHEDULER_shutdown ();
447 GNUNET_CONFIGURATION_get_value_number (c,
449 "MAX_PARALLEL_BACKGROUND_QUERIES",
450 &max_parallel_bg_queries))
452 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
453 "Number of allowed parallel background queries: %llu\n",
454 max_parallel_bg_queries);
456 dht_handle = GNUNET_DHT_connect (c,
457 (unsigned int) max_parallel_bg_queries);
458 if (NULL == dht_handle)
460 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
461 _("Could not connect to DHT!\n"));
462 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
466 identity_handle = GNUNET_IDENTITY_connect (c,
469 if (NULL == identity_handle)
471 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
472 "Could not connect to identity service!\n");
476 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
477 "Looking for gns-intercept ego\n");
478 identity_op = GNUNET_IDENTITY_get (identity_handle,
480 &identity_intercept_cb,
483 GNS_resolver_init (namecache_handle,
486 max_parallel_bg_queries);
487 statistics = GNUNET_STATISTICS_create ("gns", c);
488 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
494 * Define "main" method using service macro.
498 GNUNET_SERVICE_OPTION_NONE,
501 &client_disconnect_cb,
503 GNUNET_MQ_hd_var_size (lookup,
504 GNUNET_MESSAGE_TYPE_GNS_LOOKUP,
505 struct LookupMessage,
507 GNUNET_MQ_handler_end());
510 /* end of gnunet-service-gns.c */