2 This file is part of GNUnet.
3 (C) 2009, 2010, 2011, 2012 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
21 * @file gns/gnunet-service-gns.c
22 * @brief GNUnet GNS service
23 * @author Martin Schanzenbach
26 #include "gnunet_util_lib.h"
27 #include "gnunet_transport_service.h"
28 #include "gnunet_dns_service.h"
29 #include "gnunet_dnsparser_lib.h"
30 #include "gnunet_dht_service.h"
31 #include "gnunet_namestore_service.h"
32 #include "gnunet_gns_service.h"
33 #include "gnunet_statistics_service.h"
35 #include "gns_common.h"
36 #include "gnunet-service-gns_resolver.h"
37 #include "gnunet-service-gns_interceptor.h"
38 #include "gnunet_protocols.h"
41 * The initial interval in milliseconds btween puts in
44 #define INITIAL_PUT_INTERVAL GNUNET_TIME_UNIT_MILLISECONDS
47 * The upper bound for the zone iteration interval in milliseconds
49 #define MINIMUM_ZONE_ITERATION_INTERVAL GNUNET_TIME_UNIT_SECONDS
52 * The default put interval for the zone iteration. In case
55 #define DEFAULT_ZONE_PUBLISH_TIME_WINDOW GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 4)
58 * The factor the current zone iteration interval is divided by for each
59 * additional new record
61 #define LATE_ITERATION_SPEEDUP_FACTOR 2
65 * Handle to a shorten operation from api
67 struct ClientShortenHandle
71 * List for all shorten requests
73 struct ClientShortenHandle *next;
76 * List for all shorten requests
78 struct ClientShortenHandle *prev;
81 * Handle to the requesting client
83 struct GNUNET_SERVER_Client *client;
86 * Namestore lookup task
88 struct GNUNET_NAMESTORE_QueueEntry *namestore_task;
93 struct GNUNET_CRYPTO_ShortHashCode root_zone;
98 struct GNUNET_CRYPTO_ShortHashCode private_zone;
103 struct GNUNET_CRYPTO_ShortHashCode shorten_zone;
113 enum GNUNET_GNS_RecordType type;
118 char name[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
121 * name of private zone (relative to root)
123 char private_zone_id[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
126 * name of shorten zone (relative to root)
128 char shorten_zone_id[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
134 * Handle to a get authority operation from api
136 struct ClientGetAuthHandle
139 * Handle to the requesting client
141 struct GNUNET_SERVER_Client *client;
144 * name to lookup authority
157 * Handle to a lookup operation from api
159 struct ClientLookupHandle
163 * Handle to the requesting client
165 struct GNUNET_SERVER_Client *client;
168 * optional zone private key used for shorten
170 struct GNUNET_CRYPTO_EccPrivateKey *shorten_key;
173 * the name to look up
178 * The zone we look up in
180 struct GNUNET_CRYPTO_ShortHashCode zone;
188 * GNUNET_YES if we only want to lookup from local cache
195 enum GNUNET_GNS_RecordType type;
200 * Our handle to the DHT
202 static struct GNUNET_DHT_Handle *dht_handle;
205 * Our zone's private key
207 static struct GNUNET_CRYPTO_EccPrivateKey *zone_key;
210 * Our handle to the namestore service
212 static struct GNUNET_NAMESTORE_Handle *namestore_handle;
215 * Handle to iterate over our authoritative zone in namestore
217 static struct GNUNET_NAMESTORE_ZoneIterator *namestore_iter;
220 * Our notification context.
222 static struct GNUNET_SERVER_NotificationContext *nc;
227 static struct GNUNET_CRYPTO_ShortHashCode zone_hash;
230 * Useful for zone update for DHT put
232 static unsigned long long num_public_records;
235 * Last seen record count
237 static unsigned long long last_num_public_records;
240 * Zone iteration PUT interval.
242 static struct GNUNET_TIME_Relative put_interval;
245 * Time window for zone iteration
247 static struct GNUNET_TIME_Relative zone_publish_time_window;
252 static GNUNET_SCHEDULER_TaskIdentifier zone_publish_task;
255 * GNUNET_YES if automatic pkey import for name shortening
258 static int auto_import_pkey;
261 * GNUNET_YES if zone has never been published before
263 static int first_zone_iteration;
268 static struct GNUNET_TIME_Relative default_lookup_timeout;
271 * GNUNET_YES if ipv6 is supported
273 static int v6_enabled;
276 * GNUNET_YES if ipv4 is supported
278 static int v4_enabled;
281 * List for shorten requests
283 static struct ClientShortenHandle *csh_head;
286 * List for shorten requests
288 static struct ClientShortenHandle *csh_tail;
291 * Handle to the statistics service
293 static struct GNUNET_STATISTICS_Handle *statistics;
297 * Task run during shutdown.
303 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
305 struct ClientShortenHandle *csh_tmp;
307 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
309 while (NULL != (csh_tmp = csh_head))
311 GNUNET_CONTAINER_DLL_remove (csh_head, csh_tail, csh_tmp);
312 GNUNET_free (csh_tmp);
314 GNUNET_SERVER_notification_context_destroy (nc);
315 gns_interceptor_stop ();
316 gns_resolver_cleanup ();
317 if (NULL != statistics)
319 GNUNET_STATISTICS_destroy (statistics, GNUNET_NO);
322 if (GNUNET_SCHEDULER_NO_TASK != zone_publish_task)
324 GNUNET_SCHEDULER_cancel (zone_publish_task);
325 zone_publish_task = GNUNET_SCHEDULER_NO_TASK;
327 if (NULL != namestore_iter)
329 GNUNET_NAMESTORE_zone_iteration_stop (namestore_iter);
330 namestore_iter = NULL;
332 if (NULL != namestore_handle)
334 GNUNET_NAMESTORE_disconnect (namestore_handle);
335 namestore_handle = NULL;
337 if (NULL != dht_handle)
339 GNUNET_DHT_disconnect (dht_handle);
346 * Method called periodically that triggers iteration over authoritative records
349 * @param tc task context
352 publish_zone_dht_next (void *cls,
353 const struct GNUNET_SCHEDULER_TaskContext *tc)
355 zone_publish_task = GNUNET_SCHEDULER_NO_TASK;
356 GNUNET_NAMESTORE_zone_iterator_next (namestore_iter);
361 * Periodically iterate over our zone and store everything in dht
364 * @param tc task context
367 publish_zone_dht_start (void *cls,
368 const struct GNUNET_SCHEDULER_TaskContext *tc);
372 * Function used to put all records successively into the DHT.
374 * @param cls the closure (NULL)
375 * @param key the public key of the authority (ours)
376 * @param expiration lifetime of the namestore entry
377 * @param name the name of the records
378 * @param rd_count the number of records in data
379 * @param rd the record data
380 * @param signature the signature for the record data
383 put_gns_record (void *cls,
384 const struct GNUNET_CRYPTO_EccPublicKey *key,
385 struct GNUNET_TIME_Absolute expiration,
387 unsigned int rd_count,
388 const struct GNUNET_NAMESTORE_RecordData *rd,
389 const struct GNUNET_CRYPTO_EccSignature *signature)
391 struct GNSNameRecordBlock *nrb;
392 struct GNUNET_CRYPTO_ShortHashCode zhash;
393 struct GNUNET_HashCode dht_key;
394 uint32_t rd_payload_length;
395 char* nrb_data = NULL;
397 struct GNUNET_TIME_Relative next_put_interval;
402 namestore_iter = NULL;
403 last_num_public_records = num_public_records;
404 first_zone_iteration = GNUNET_NO;
405 if (0 == num_public_records)
408 * If no records are known (startup) or none present
409 * we can safely set the interval to the value for a single
412 put_interval = zone_publish_time_window;
413 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
414 "No records in db.\n");
418 put_interval = GNUNET_TIME_relative_divide (zone_publish_time_window,
421 put_interval = GNUNET_TIME_relative_max (MINIMUM_ZONE_ITERATION_INTERVAL,
424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
425 "Zone iteration finished. Adjusted zone iteration interval to %s\n",
426 GNUNET_STRINGS_relative_time_to_string (put_interval, GNUNET_YES));
427 GNUNET_STATISTICS_set (statistics,
428 "Current zone iteration interval (in ms)",
429 put_interval.rel_value_us / 1000LL,
431 GNUNET_STATISTICS_update (statistics,
432 "Number of zone iterations", 1, GNUNET_NO);
433 GNUNET_STATISTICS_set (statistics,
434 "Number of public records in DHT",
435 last_num_public_records,
437 if (0 == num_public_records)
438 zone_publish_task = GNUNET_SCHEDULER_add_delayed (put_interval,
439 &publish_zone_dht_start,
442 zone_publish_task = GNUNET_SCHEDULER_add_now (&publish_zone_dht_start, NULL);
446 namelen = strlen (name) + 1;
449 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
450 "No records for name `%s'! Skipping.\n",
452 zone_publish_task = GNUNET_SCHEDULER_add_now (&publish_zone_dht_next,
456 if (NULL == signature)
459 zone_publish_task = GNUNET_SCHEDULER_add_now (&publish_zone_dht_next,
464 /* TODO 2) AB: New publishing
466 * - Use new signature S_d
467 * - Obtain new derived public key V = H(H(i,Q) * Q)
469 * - Compute encrypte record block E with HKDF(i,Q) (rd, rd_count)
470 * - Create block B = |V,E,S_d|
471 * - Compute new DHT key H(V) in TODO 3)
475 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
476 "Putting records for `%s' into the DHT\n", name);
477 rd_payload_length = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
478 nrb = GNUNET_malloc (rd_payload_length + namelen
479 + sizeof (struct GNSNameRecordBlock));
480 nrb->signature = *signature;
481 nrb->public_key = *key;
482 nrb->rd_count = htonl (rd_count);
483 memcpy (&nrb[1], name, namelen);
484 nrb_data = (char *) &nrb[1];
486 rd_payload_length += sizeof(struct GNSNameRecordBlock) + namelen;
487 GNUNET_CRYPTO_short_hash (key,
488 sizeof (struct GNUNET_CRYPTO_EccPublicKey),
490 if (-1 == GNUNET_NAMESTORE_records_serialize (rd_count,
495 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
496 _("Records for name `%s' in zone %s too large to fit into DHT"),
498 GNUNET_short_h2s (&zhash));
500 zone_publish_task = GNUNET_SCHEDULER_add_now (&publish_zone_dht_next,
504 /* TODO AB: Here records are put in the DHT: modify dht_key to H(key) = H(H(name,zone) * zone) */
505 GNUNET_GNS_get_key_for_record (name, &zhash, &dht_key);
506 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
507 "putting %u records from zone %s for `%s' under key: %s with size %u and timeout %s\n",
509 GNUNET_short_h2s (&zhash),
511 GNUNET_h2s (&dht_key),
512 (unsigned int) rd_payload_length,
513 GNUNET_STRINGS_relative_time_to_string (DHT_OPERATION_TIMEOUT, GNUNET_YES));
515 GNUNET_STATISTICS_update (statistics,
516 "Record bytes put into DHT",
517 rd_payload_length, GNUNET_NO);
519 (void) GNUNET_DHT_put (dht_handle, &dht_key,
520 DHT_GNS_REPLICATION_LEVEL,
521 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
522 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
526 DHT_OPERATION_TIMEOUT,
531 num_public_records++;
532 if ( (num_public_records > last_num_public_records)
533 && (GNUNET_NO == first_zone_iteration) )
535 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
536 "Last record count was lower than current record count. Reducing interval.\n");
537 put_interval = GNUNET_TIME_relative_divide (zone_publish_time_window,
539 next_put_interval = GNUNET_TIME_relative_divide (put_interval,
540 LATE_ITERATION_SPEEDUP_FACTOR);
543 next_put_interval = put_interval;
545 GNUNET_STATISTICS_set (statistics,
546 "Current zone iteration interval (ms)",
547 next_put_interval.rel_value_us / 1000LL,
549 zone_publish_task = GNUNET_SCHEDULER_add_delayed (next_put_interval,
550 &publish_zone_dht_next,
556 * Periodically iterate over our zone and store everything in dht
559 * @param tc task context
562 publish_zone_dht_start (void *cls,
563 const struct GNUNET_SCHEDULER_TaskContext *tc)
565 zone_publish_task = GNUNET_SCHEDULER_NO_TASK;
567 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
568 "Scheduling DHT zone update!\n");
569 /* start counting again */
570 num_public_records = 0;
571 namestore_iter = GNUNET_NAMESTORE_zone_iteration_start (namestore_handle,
572 NULL, /* All zones */
573 GNUNET_NAMESTORE_RF_AUTHORITY,
574 GNUNET_NAMESTORE_RF_PRIVATE,
580 /* END DHT ZONE PROPAGATION */
584 * Send shorten response back to client
586 * @param cls the closure containing a client shorten handle
587 * @param name the shortened name result or NULL if cannot be shortened
590 send_shorten_response (void* cls, const char* name)
592 struct ClientShortenHandle *csh = cls;
593 struct GNUNET_GNS_ClientShortenResultMessage *rmsg;
596 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
597 "Sending `%s' message with %s\n",
598 "SHORTEN_RESULT", name);
602 name_len = strlen (name) + 1;
603 GNUNET_STATISTICS_update (statistics,
604 "Name shorten results", 1, GNUNET_NO);
606 rmsg = GNUNET_malloc (sizeof (struct GNUNET_GNS_ClientShortenResultMessage) +
609 rmsg->id = csh->request_id;
610 rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_SHORTEN_RESULT);
612 htons(sizeof(struct GNUNET_GNS_ClientShortenResultMessage) +
614 memcpy (&rmsg[1], name, name_len);
615 GNUNET_SERVER_notification_context_unicast (nc, csh->client,
618 if (NULL != csh->namestore_task)
619 GNUNET_NAMESTORE_cancel (csh->namestore_task);
626 * Lookup the zone infos and shorten name
628 * @param cls the client shorten handle
629 * @param key key of the zone
630 * @param expiration expiration of record
631 * @param name name found or null if no result
632 * @param rd_count number of records found
633 * @param rd record data
638 process_shorten_in_private_zone_lookup (void *cls,
639 const struct GNUNET_CRYPTO_EccPublicKey *key,
640 struct GNUNET_TIME_Absolute expiration,
642 unsigned int rd_count,
643 const struct GNUNET_NAMESTORE_RecordData *rd,
644 const struct GNUNET_CRYPTO_EccSignature *signature)
646 struct ClientShortenHandle *csh = cls;
647 struct GNUNET_CRYPTO_ShortHashCode *szone = &csh->shorten_zone;
648 struct GNUNET_CRYPTO_ShortHashCode *pzone = &csh->private_zone;
650 csh->namestore_task = NULL;
651 if (0 == strcmp (csh->private_zone_id, ""))
655 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
656 "No shorten zone in private zone!\n");
657 strcpy (csh->shorten_zone_id, "");
662 GNUNET_break (1 == rd_count);
663 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
664 "Shorten zone %s found in private zone %s\n",
665 name, csh->private_zone_id);
667 sprintf (csh->shorten_zone_id, "%s.%s", name, csh->private_zone_id);
669 GNUNET_CONTAINER_DLL_remove (csh_head, csh_tail, csh);
670 gns_resolver_shorten_name (&csh->root_zone,
674 csh->private_zone_id,
675 csh->shorten_zone_id,
676 &send_shorten_response, csh);
682 * Lookup the zone infos and shorten name
684 * @param cls the shorten handle
685 * @param key key of the zone
686 * @param expiration expiration of record
687 * @param name name found or null if no result
688 * @param rd_count number of records found
689 * @param rd record data
694 process_shorten_in_root_zone_lookup (void *cls,
695 const struct GNUNET_CRYPTO_EccPublicKey *key,
696 struct GNUNET_TIME_Absolute expiration,
698 unsigned int rd_count,
699 const struct GNUNET_NAMESTORE_RecordData *rd,
700 const struct GNUNET_CRYPTO_EccSignature *signature)
702 struct ClientShortenHandle *csh = cls;
703 struct GNUNET_CRYPTO_ShortHashCode *szone = &csh->shorten_zone;
704 struct GNUNET_CRYPTO_ShortHashCode *pzone = &csh->private_zone;
706 csh->namestore_task = NULL;
707 if (0 == strcmp (csh->private_zone_id, ""))
711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
712 "No shorten zone in zone and no private zone!\n");
714 strcpy (csh->shorten_zone_id, "");
715 GNUNET_CONTAINER_DLL_remove (csh_head, csh_tail, csh);
717 gns_resolver_shorten_name (&csh->root_zone,
721 csh->private_zone_id,
722 csh->shorten_zone_id,
723 &send_shorten_response, csh);
726 GNUNET_break (rd_count == 1);
727 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
728 "Private zone %s found in root zone\n", name);
729 strcpy (csh->private_zone_id, name);
730 csh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
733 &process_shorten_in_private_zone_lookup,
739 * Lookup the zone infos and shorten name
741 * @param cls the shorten handle
742 * @param key key of the zone
743 * @param expiration expiration of record
744 * @param name name found or null if no result
745 * @param rd_count number of records found
746 * @param rd record data
750 process_private_in_root_zone_lookup (void *cls,
751 const struct GNUNET_CRYPTO_EccPublicKey *key,
752 struct GNUNET_TIME_Absolute expiration,
754 unsigned int rd_count,
755 const struct GNUNET_NAMESTORE_RecordData *rd,
756 const struct GNUNET_CRYPTO_EccSignature *signature)
758 struct ClientShortenHandle *csh = cls;
760 csh->namestore_task = NULL;
763 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
764 "No private zone in root zone\n");
765 strcpy (csh->private_zone_id, "");
766 csh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
769 &process_shorten_in_root_zone_lookup,
773 GNUNET_break (1 == rd_count);
774 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
775 "Private zone `%s' found in root zone\n",
777 strcpy (csh->private_zone_id, name);
778 csh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
781 &process_shorten_in_private_zone_lookup,
787 * Handle a shorten message from the api
789 * @param cls the closure (unused)
790 * @param client the client
791 * @param message the message
794 handle_shorten (void *cls,
795 struct GNUNET_SERVER_Client * client,
796 const struct GNUNET_MessageHeader * message)
798 struct ClientShortenHandle *csh;
800 char name[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
801 char* nameptr = name;
803 const struct GNUNET_GNS_ClientShortenMessage *sh_msg;
805 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
806 "Received `%s' message\n", "SHORTEN");
807 msg_size = ntohs (message->size);
808 if (msg_size < sizeof (struct GNUNET_GNS_ClientShortenMessage))
811 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
814 sh_msg = (const struct GNUNET_GNS_ClientShortenMessage *) message;
815 utf_in = (const char *) &sh_msg[1];
816 if ('\0' != utf_in[msg_size - sizeof (struct GNUNET_GNS_ClientShortenMessage) - 1])
819 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
822 csh = GNUNET_malloc(sizeof (struct ClientShortenHandle));
823 csh->client = client;
824 csh->request_id = sh_msg->id;
825 GNUNET_CONTAINER_DLL_insert (csh_head, csh_tail, csh);
826 GNUNET_STRINGS_utf8_tolower (utf_in, &nameptr);
827 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
828 "SHORTEN: Converted `%s' to `%s'\n",
831 GNUNET_SERVER_notification_context_add (nc, client);
832 if (strlen (name) < strlen (GNUNET_GNS_TLD))
834 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
835 "SHORTEN: %s is too short\n", name);
836 GNUNET_CONTAINER_DLL_remove (csh_head, csh_tail, csh);
837 send_shorten_response(csh, name);
838 GNUNET_SERVER_receive_done (client, GNUNET_OK);
841 if (strlen (name) > GNUNET_DNSPARSER_MAX_NAME_LENGTH)
843 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
844 "SHORTEN: %s is too long\n", name);
845 GNUNET_CONTAINER_DLL_remove (csh_head, csh_tail, csh);
846 send_shorten_response(csh, name);
847 GNUNET_SERVER_receive_done (client, GNUNET_OK);
850 if ( (! is_gads_tld (name)) &&
851 (! is_zkey_tld (name)) )
853 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
854 "%s is not our domain. Returning\n", name);
855 GNUNET_CONTAINER_DLL_remove (csh_head, csh_tail, csh);
856 send_shorten_response (csh, name);
857 GNUNET_SERVER_receive_done (client, GNUNET_OK);
860 csh->shorten_zone = sh_msg->shorten_zone;
861 csh->private_zone = sh_msg->private_zone;
862 strcpy (csh->name, name);
863 if (1 == ntohl(sh_msg->use_default_zone))
864 csh->root_zone = zone_hash; //Default zone
866 csh->root_zone = sh_msg->zone;
867 csh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
870 &process_private_in_root_zone_lookup,
872 GNUNET_STATISTICS_update (statistics,
873 "Name shorten attempts", 1, GNUNET_NO);
874 GNUNET_SERVER_receive_done (client, GNUNET_OK);
879 * Send get authority response back to client
881 * @param cls the closure containing a client get auth handle
882 * @param name the name of the authority, or NULL on error
885 send_get_auth_response (void *cls,
888 struct ClientGetAuthHandle *cah = cls;
889 struct GNUNET_GNS_ClientGetAuthResultMessage *rmsg;
891 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
892 "Sending `%s' message with `%s'\n",
893 "GET_AUTH_RESULT", name);
896 GNUNET_STATISTICS_update (statistics,
897 "Authorities resolved", 1, GNUNET_NO);
901 rmsg = GNUNET_malloc (sizeof (struct GNUNET_GNS_ClientGetAuthResultMessage)
902 + strlen (name) + 1);
904 rmsg->id = cah->request_id;
905 rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_GET_AUTH_RESULT);
907 htons(sizeof(struct GNUNET_GNS_ClientGetAuthResultMessage) +
909 strcpy ((char*)&rmsg[1], name);
911 GNUNET_SERVER_notification_context_unicast (nc, cah->client,
914 GNUNET_SERVER_receive_done (cah->client, GNUNET_OK);
916 GNUNET_free_non_null(cah->name);
922 * Handle a get authority message from the api
924 * @param cls the closure
925 * @param client the client
926 * @param message the message
929 handle_get_authority (void *cls,
930 struct GNUNET_SERVER_Client * client,
931 const struct GNUNET_MessageHeader * message)
933 struct ClientGetAuthHandle *cah;
935 char name[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
936 char* nameptr = name;
938 const struct GNUNET_GNS_ClientGetAuthMessage *sh_msg;
940 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
941 "Received `%s' message\n", "GET_AUTH");
942 msg_size = ntohs(message->size);
943 if (msg_size < sizeof (struct GNUNET_GNS_ClientGetAuthMessage))
946 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
949 GNUNET_SERVER_notification_context_add (nc, client);
950 sh_msg = (const struct GNUNET_GNS_ClientGetAuthMessage *) message;
951 utf_in = (const char *) &sh_msg[1];
952 if ('\0' != utf_in[msg_size - sizeof (struct GNUNET_GNS_ClientGetAuthMessage) - 1])
955 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
958 GNUNET_STRINGS_utf8_tolower(utf_in, &nameptr);
959 cah = GNUNET_malloc(sizeof(struct ClientGetAuthHandle));
960 cah->client = client;
961 cah->request_id = sh_msg->id;
962 if (strlen (name) < strlen(GNUNET_GNS_TLD))
964 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
965 "GET_AUTH: `%s' is too short. Returning\n", name);
967 send_get_auth_response(cah, name);
970 if (strlen (name) > GNUNET_DNSPARSER_MAX_NAME_LENGTH)
972 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
973 "GET_AUTH: `%s' is too long", name);
975 send_get_auth_response(cah, name);
978 if (0 != strcmp (name + strlen (name) - strlen (GNUNET_GNS_TLD),
981 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
982 "GET_AUTH: %s is not our domain. Returning\n", name);
984 send_get_auth_response (cah, name);
988 if (0 == strcmp (name, GNUNET_GNS_TLD))
990 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
991 "GET_AUTH: %s is us. Returning\n", name);
993 send_get_auth_response(cah, name);
997 cah->name = GNUNET_malloc (strlen (name)
998 - strlen (GNUNET_GNS_TLD) + 1);
999 memcpy (cah->name, name,
1000 strlen (name) - strlen (GNUNET_GNS_TLD));
1002 /* Start delegation resolution in our namestore */
1003 gns_resolver_get_authority (zone_hash, zone_hash, name,
1004 &send_get_auth_response, cah);
1005 GNUNET_STATISTICS_update (statistics,
1006 "Authority lookup attempts", 1, GNUNET_NO);
1011 * Reply to client with the result from our lookup.
1013 * @param cls the closure (our client lookup handle)
1014 * @param rd_count the number of records
1015 * @param rd the record data
1018 send_lookup_response (void* cls,
1020 const struct GNUNET_NAMESTORE_RecordData *rd)
1022 struct ClientLookupHandle* clh = cls;
1023 struct GNUNET_GNS_ClientLookupResultMessage *rmsg;
1026 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message with %d results\n",
1027 "LOOKUP_RESULT", rd_count);
1029 len = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
1030 rmsg = GNUNET_malloc (len + sizeof (struct GNUNET_GNS_ClientLookupResultMessage));
1032 rmsg->id = clh->request_id;
1033 rmsg->rd_count = htonl(rd_count);
1034 rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT);
1036 htons(len+sizeof(struct GNUNET_GNS_ClientLookupResultMessage));
1038 GNUNET_NAMESTORE_records_serialize (rd_count, rd, len, (char*)&rmsg[1]);
1040 GNUNET_SERVER_notification_context_unicast (nc, clh->client,
1041 (const struct GNUNET_MessageHeader *) rmsg,
1043 GNUNET_SERVER_receive_done (clh->client, GNUNET_OK);
1046 GNUNET_free(clh->name);
1048 if (NULL != clh->shorten_key)
1049 GNUNET_CRYPTO_ecc_key_free (clh->shorten_key);
1051 GNUNET_STATISTICS_update (statistics,
1052 "Completed lookups", 1, GNUNET_NO);
1054 GNUNET_STATISTICS_update (statistics,
1055 "Records resolved", rd_count, GNUNET_NO);
1060 * Handle lookup requests from client
1062 * @param cls the closure
1063 * @param client the client
1064 * @param message the message
1067 handle_lookup (void *cls,
1068 struct GNUNET_SERVER_Client * client,
1069 const struct GNUNET_MessageHeader * message)
1072 char name[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
1073 struct ClientLookupHandle *clh;
1074 char* nameptr = name;
1077 const struct GNUNET_CRYPTO_EccPrivateKey *key;
1079 const struct GNUNET_GNS_ClientLookupMessage *sh_msg;
1081 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1082 "Received `%s' message\n", "LOOKUP");
1083 msg_size = ntohs(message->size);
1084 if (msg_size < sizeof (struct GNUNET_GNS_ClientLookupMessage))
1087 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1090 sh_msg = (const struct GNUNET_GNS_ClientLookupMessage *) message;
1091 GNUNET_SERVER_notification_context_add (nc, client);
1092 if (GNUNET_YES == ntohl (sh_msg->have_key))
1094 key = &sh_msg->shorten_key;
1100 utf_in = (const char *) &sh_msg[1];
1101 if ('\0' != utf_in[msg_size - sizeof (struct GNUNET_GNS_ClientLookupMessage) - 1])
1104 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1107 GNUNET_STRINGS_utf8_tolower (utf_in, &nameptr);
1109 namelen = strlen (name) + 1;
1110 clh = GNUNET_malloc (sizeof (struct ClientLookupHandle));
1111 memset (clh, 0, sizeof (struct ClientLookupHandle));
1112 clh->client = client;
1113 clh->name = GNUNET_malloc (namelen);
1114 strcpy (clh->name, name);
1115 clh->request_id = sh_msg->id;
1116 clh->type = ntohl (sh_msg->type);
1119 clh->shorten_key = GNUNET_new (struct GNUNET_CRYPTO_EccPrivateKey);
1120 *clh->shorten_key = *key;
1122 only_cached = ntohl (sh_msg->only_cached);
1124 if (strlen (name) > GNUNET_DNSPARSER_MAX_NAME_LENGTH) {
1125 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1126 "LOOKUP: %s is too long", name);
1128 send_lookup_response (clh, 0, NULL);
1132 if ((GNUNET_GNS_RECORD_A == clh->type) &&
1133 (GNUNET_OK != v4_enabled))
1135 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1136 "LOOKUP: Query for A record but AF_INET not supported!");
1138 send_lookup_response (clh, 0, NULL);
1142 if ((GNUNET_GNS_RECORD_AAAA == clh->type) &&
1143 (GNUNET_OK != v6_enabled))
1145 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1146 "LOOKUP: Query for AAAA record but AF_INET6 not supported!");
1148 send_lookup_response (clh, 0, NULL);
1152 if (GNUNET_NO == ntohl (sh_msg->have_zone))
1153 clh->zone = zone_hash; /* Default zone */
1155 clh->zone = sh_msg->zone;
1157 if (GNUNET_YES == auto_import_pkey)
1159 gns_resolver_lookup_record (clh->zone, clh->zone, clh->type, clh->name,
1161 default_lookup_timeout,
1163 &send_lookup_response, clh);
1167 gns_resolver_lookup_record (clh->zone, clh->zone, clh->type, name,
1169 default_lookup_timeout,
1171 &send_lookup_response, clh);
1173 GNUNET_STATISTICS_update (statistics,
1174 "Record lookup attempts", 1, GNUNET_NO);
1179 * Process GNS requests.
1181 * @param cls closure
1182 * @param server the initialized server
1183 * @param c configuration to use
1186 run (void *cls, struct GNUNET_SERVER_Handle *server,
1187 const struct GNUNET_CONFIGURATION_Handle *c)
1189 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1190 {&handle_shorten, NULL, GNUNET_MESSAGE_TYPE_GNS_SHORTEN, 0},
1191 {&handle_lookup, NULL, GNUNET_MESSAGE_TYPE_GNS_LOOKUP, 0},
1192 {&handle_get_authority, NULL, GNUNET_MESSAGE_TYPE_GNS_GET_AUTH, 0}
1195 struct GNUNET_CRYPTO_EccPublicKey pkey;
1196 unsigned long long max_parallel_bg_queries = 0;
1197 int ignore_pending = GNUNET_NO;
1199 v6_enabled = GNUNET_NETWORK_test_pf (PF_INET6);
1200 v4_enabled = GNUNET_NETWORK_test_pf (PF_INET);
1202 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (c, "gns",
1203 "ZONEKEY", &keyfile))
1205 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1206 "No private key for root zone specified!\n");
1207 GNUNET_SCHEDULER_shutdown ();
1211 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1212 "Using keyfile %s for root zone.\n", keyfile);
1214 zone_key = GNUNET_CRYPTO_ecc_key_create_from_file (keyfile);
1215 GNUNET_CRYPTO_ecc_key_get_public (zone_key, &pkey);
1216 GNUNET_CRYPTO_short_hash (&pkey,
1217 sizeof(struct GNUNET_CRYPTO_EccPublicKey),
1219 GNUNET_free(keyfile);
1220 namestore_handle = GNUNET_NAMESTORE_connect (c);
1221 if (NULL == namestore_handle)
1223 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1224 _("Failed to connect to the namestore!\n"));
1225 GNUNET_SCHEDULER_shutdown ();
1229 auto_import_pkey = GNUNET_NO;
1231 GNUNET_CONFIGURATION_get_value_yesno (c, "gns",
1232 "AUTO_IMPORT_PKEY"))
1234 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1235 "Automatic PKEY import is enabled.\n");
1236 auto_import_pkey = GNUNET_YES;
1238 put_interval = INITIAL_PUT_INTERVAL;
1239 zone_publish_time_window = DEFAULT_ZONE_PUBLISH_TIME_WINDOW;
1242 GNUNET_CONFIGURATION_get_value_time (c, "gns",
1243 "ZONE_PUBLISH_TIME_WINDOW",
1244 &zone_publish_time_window))
1246 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1247 "Time window for zone iteration: %s\n",
1248 GNUNET_STRINGS_relative_time_to_string (zone_publish_time_window, GNUNET_YES));
1251 GNUNET_CONFIGURATION_get_value_number (c, "gns",
1252 "MAX_PARALLEL_BACKGROUND_QUERIES",
1253 &max_parallel_bg_queries))
1255 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1256 "Number of allowed parallel background queries: %llu\n",
1257 max_parallel_bg_queries);
1261 GNUNET_CONFIGURATION_get_value_yesno (c, "gns",
1262 "AUTO_IMPORT_CONFIRMATION_REQ"))
1264 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1265 "Auto import requires user confirmation\n");
1266 ignore_pending = GNUNET_YES;
1270 GNUNET_CONFIGURATION_get_value_time (c, "gns",
1271 "DEFAULT_LOOKUP_TIMEOUT",
1272 &default_lookup_timeout))
1274 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1275 "Default lookup timeout: %s\n",
1276 GNUNET_STRINGS_relative_time_to_string (default_lookup_timeout,
1280 dht_handle = GNUNET_DHT_connect (c,
1281 (unsigned int) max_parallel_bg_queries);
1282 if (NULL == dht_handle)
1284 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1285 _("Could not connect to DHT!\n"));
1286 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
1290 if (GNUNET_SYSERR ==
1291 gns_resolver_init (namestore_handle, dht_handle, zone_hash, c,
1292 max_parallel_bg_queries,
1295 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1296 _("Unable to initialize resolver!\n"));
1297 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
1302 GNUNET_CONFIGURATION_get_value_yesno (c, "gns", "HIJACK_DNS"))
1304 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1305 "DNS hijacking enabled. Connecting to DNS service.\n");
1307 if (GNUNET_SYSERR ==
1308 gns_interceptor_init (zone_hash, zone_key, c))
1310 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1311 "Failed to enable the DNS interceptor!\n");
1316 * Schedule periodic put
1318 * We have roughly an hour for all records;
1320 first_zone_iteration = GNUNET_YES;
1321 zone_publish_task = GNUNET_SCHEDULER_add_now (&publish_zone_dht_start, NULL);
1322 GNUNET_SERVER_add_handlers (server, handlers);
1323 statistics = GNUNET_STATISTICS_create ("gns", c);
1324 nc = GNUNET_SERVER_notification_context_create (server, 1);
1325 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
1331 * The main function for the GNS service.
1333 * @param argc number of arguments from the command line
1334 * @param argv command line arguments
1335 * @return 0 ok, 1 on error
1338 main (int argc, char *const *argv)
1344 GNUNET_SERVICE_run (argc, argv, "gns", GNUNET_SERVICE_OPTION_NONE, &run,
1349 /* end of gnunet-service-gns.c */