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.
23 * @file gns/gnunet-service-gns.c
24 * @brief GNUnet GNS service
25 * @author Martin Schanzenbach
28 #include "gnunet_util_lib.h"
29 #include "gnunet_transport_service.h"
30 #include "gnunet_dns_service.h"
31 #include "gnunet_dnsparser_lib.h"
32 #include "gnunet_dht_service.h"
33 #include "gnunet_namestore_service.h"
34 #include "gnunet_gns_service.h"
35 #include "gnunet_statistics_service.h"
36 #include "block_gns.h"
38 #include "gns_common.h"
39 #include "gnunet-service-gns_resolver.h"
40 #include "gnunet-service-gns_interceptor.h"
41 #include "gnunet_protocols.h"
44 * The initial interval in milliseconds btween puts in
47 #define INITIAL_PUT_INTERVAL GNUNET_TIME_UNIT_MILLISECONDS
50 * The upper bound for the zone iteration interval in milliseconds
52 #define MINIMUM_ZONE_ITERATION_INTERVAL GNUNET_TIME_UNIT_SECONDS
55 * The default put interval for the zone iteration. In case
58 #define DEFAULT_ZONE_PUBLISH_TIME_WINDOW GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 4)
61 * The factor the current zone iteration interval is divided by for each
62 * additional new record
64 #define LATE_ITERATION_SPEEDUP_FACTOR 2
68 * Handle to a shorten operation from api
70 struct ClientShortenHandle
74 * List for all shorten requests
76 struct ClientShortenHandle *next;
78 * List for all shorten requests
80 struct ClientShortenHandle *prev;
83 * Handle to the requesting client
85 struct GNUNET_SERVER_Client *client;
95 enum GNUNET_GNS_RecordType type;
100 char name[MAX_DNS_NAME_LENGTH];
103 * name of private zone (relative to root)
105 char private_zone_id[MAX_DNS_NAME_LENGTH];
108 * name of shorten zone (relative to root)
110 char shorten_zone_id[MAX_DNS_NAME_LENGTH];
115 struct GNUNET_CRYPTO_ShortHashCode root_zone;
120 struct GNUNET_CRYPTO_ShortHashCode private_zone;
125 struct GNUNET_CRYPTO_ShortHashCode shorten_zone;
128 * Namestore lookup task
130 struct GNUNET_NAMESTORE_QueueEntry *namestore_task;
135 * Handle to a get authority operation from api
137 struct ClientGetAuthHandle
140 * Handle to the requesting client */
141 struct GNUNET_SERVER_Client *client;
149 * name to lookup authority
156 * Handle to a lookup operation from api
158 struct ClientLookupHandle
162 * Handle to the requesting client
164 struct GNUNET_SERVER_Client *client;
167 * The zone we look up in
169 struct GNUNET_CRYPTO_ShortHashCode zone;
172 * GNUNET_YES if we only want to lookup from local cache
184 enum GNUNET_GNS_RecordType type;
187 * optional zone private key used for shorten
189 struct GNUNET_CRYPTO_RsaPrivateKey *shorten_key;
192 * the name to look up
199 * Our handle to the DHT
201 static struct GNUNET_DHT_Handle *dht_handle;
204 * Our zone's private key
206 static struct GNUNET_CRYPTO_RsaPrivateKey *zone_key;
209 * Our handle to the namestore service
211 static struct GNUNET_NAMESTORE_Handle *namestore_handle;
214 * Handle to iterate over our authoritative zone in namestore
216 static struct GNUNET_NAMESTORE_ZoneIterator *namestore_iter;
219 * Our notification context.
221 static struct GNUNET_SERVER_NotificationContext *nc;
226 static struct GNUNET_CRYPTO_ShortHashCode zone_hash;
229 * Useful for zone update for DHT put
231 static unsigned long long num_public_records;
234 * Last seen record count
236 static unsigned long long last_num_public_records;
239 * Zone iteration PUT interval.
241 static struct GNUNET_TIME_Relative put_interval;
244 * Time window for zone iteration
246 static struct GNUNET_TIME_Relative zone_publish_time_window;
251 static GNUNET_SCHEDULER_TaskIdentifier zone_publish_task;
254 * GNUNET_YES if automatic pkey import for name shortening
257 static int auto_import_pkey;
260 * GNUNET_YES if zone has never been published before
262 static int first_zone_iteration;
267 static struct GNUNET_TIME_Relative default_lookup_timeout;
270 * GNUNET_YES if ipv6 is supported
272 static int v6_enabled;
275 * GNUNET_YES if ipv4 is supported
277 static int v4_enabled;
280 * List for shorten requests
282 static struct ClientShortenHandle *csh_head;
285 * List for shorten requests
287 static struct ClientShortenHandle *csh_tail;
290 * Handle to the statistics service
292 static struct GNUNET_STATISTICS_Handle *statistics;
296 * Task run during shutdown.
302 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
304 struct ClientShortenHandle *csh_tmp;
306 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
308 while (NULL != (csh_tmp = csh_head))
310 GNUNET_CONTAINER_DLL_remove (csh_head, csh_tail, csh_tmp);
311 GNUNET_free(csh_tmp);
313 GNUNET_SERVER_notification_context_destroy (nc);
314 gns_interceptor_stop ();
315 gns_resolver_cleanup ();
316 if (NULL != statistics)
317 GNUNET_STATISTICS_destroy (statistics, GNUNET_NO);
318 if (zone_publish_task != GNUNET_SCHEDULER_NO_TASK)
319 GNUNET_SCHEDULER_cancel (zone_publish_task);
320 if (NULL != namestore_iter)
321 GNUNET_NAMESTORE_zone_iteration_stop (namestore_iter);
322 GNUNET_NAMESTORE_disconnect(namestore_handle);
323 GNUNET_DHT_disconnect(dht_handle);
328 * Method called periodically that triggers iteration over authoritative records
331 * @param tc task context
334 publish_zone_dht_next (void *cls,
335 const struct GNUNET_SCHEDULER_TaskContext *tc)
337 zone_publish_task = GNUNET_SCHEDULER_NO_TASK;
338 GNUNET_NAMESTORE_zone_iterator_next (namestore_iter);
343 * Periodically iterate over our zone and store everything in dht
346 * @param tc task context
349 publish_zone_dht_start (void *cls,
350 const struct GNUNET_SCHEDULER_TaskContext *tc);
354 * Function used to put all records successively into the DHT.
356 * @param cls the closure (NULL)
357 * @param key the public key of the authority (ours)
358 * @param expiration lifetime of the namestore entry
359 * @param name the name of the records
360 * @param rd_count the number of records in data
361 * @param rd the record data
362 * @param signature the signature for the record data
365 put_gns_record(void *cls,
366 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
367 struct GNUNET_TIME_Absolute expiration,
369 unsigned int rd_count,
370 const struct GNUNET_NAMESTORE_RecordData *rd,
371 const struct GNUNET_CRYPTO_RsaSignature *signature)
373 struct GNSNameRecordBlock *nrb;
374 struct GNUNET_CRYPTO_ShortHashCode zhash;
375 struct GNUNET_HashCode dht_key;
376 uint32_t rd_payload_length;
377 char* nrb_data = NULL;
379 struct GNUNET_TIME_Relative next_put_interval;
384 namestore_iter = NULL;
385 last_num_public_records = num_public_records;
386 first_zone_iteration = GNUNET_NO;
387 if (0 == num_public_records)
390 * If no records are known (startup) or none present
391 * we can safely set the interval to the value for a single
394 put_interval = GNUNET_TIME_relative_divide (zone_publish_time_window,
397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
398 "No records in db.\n");
402 put_interval = GNUNET_TIME_relative_divide (zone_publish_time_window,
405 put_interval = GNUNET_TIME_relative_max (MINIMUM_ZONE_ITERATION_INTERVAL,
408 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
409 "Zone iteration finished. Adjusted zone iteration interval to %s\n",
410 GNUNET_STRINGS_relative_time_to_string (put_interval, GNUNET_YES));
411 GNUNET_STATISTICS_set (statistics,
412 "Current zone iteration interval (in ms)",
413 put_interval.rel_value,
415 GNUNET_STATISTICS_update (statistics,
416 "Number of zone iterations", 1, GNUNET_NO);
417 GNUNET_STATISTICS_set (statistics,
418 "Number of public records in DHT",
419 last_num_public_records,
421 if (0 == num_public_records)
422 zone_publish_task = GNUNET_SCHEDULER_add_delayed (put_interval,
423 &publish_zone_dht_start,
426 zone_publish_task = GNUNET_SCHEDULER_add_now (&publish_zone_dht_start, NULL);
430 namelen = strlen (name) + 1;
433 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
434 "No records for name `%s'! Skipping.\n",
436 zone_publish_task = GNUNET_SCHEDULER_add_now (&publish_zone_dht_next,
440 if (NULL == signature)
443 zone_publish_task = GNUNET_SCHEDULER_add_now (&publish_zone_dht_next,
448 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
449 "Putting records for `%s' into the DHT\n", name);
450 rd_payload_length = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
451 nrb = GNUNET_malloc (rd_payload_length + namelen
452 + sizeof (struct GNSNameRecordBlock));
453 nrb->signature = *signature;
454 nrb->public_key = *key;
455 nrb->rd_count = htonl (rd_count);
456 memcpy (&nrb[1], name, namelen);
457 nrb_data = (char *) &nrb[1];
459 rd_payload_length += sizeof(struct GNSNameRecordBlock) + namelen;
460 GNUNET_CRYPTO_short_hash (key,
461 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
463 if (-1 == GNUNET_NAMESTORE_records_serialize (rd_count,
468 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
469 _("Records for name `%s' in zone %s too large to fit into DHT"),
471 GNUNET_short_h2s (&zhash));
473 zone_publish_task = GNUNET_SCHEDULER_add_now (&publish_zone_dht_next,
478 GNUNET_GNS_get_key_for_record (name, &zhash, &dht_key);
479 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
480 "putting %u records from zone %s for `%s' under key: %s with size %u and timeout %s\n",
482 GNUNET_short_h2s (&zhash),
484 GNUNET_h2s (&dht_key),
485 (unsigned int) rd_payload_length,
486 GNUNET_STRINGS_relative_time_to_string (DHT_OPERATION_TIMEOUT, GNUNET_YES));
488 GNUNET_STATISTICS_update (statistics,
489 "Record bytes put into DHT", rd_payload_length, GNUNET_NO);
491 (void) GNUNET_DHT_put (dht_handle, &dht_key,
492 DHT_GNS_REPLICATION_LEVEL,
493 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
494 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
498 DHT_OPERATION_TIMEOUT,
503 num_public_records++;
504 if ( (num_public_records > last_num_public_records)
505 && (GNUNET_NO == first_zone_iteration) )
507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
508 "Last record count was lower than current record count. Reducing interval.\n");
509 put_interval = GNUNET_TIME_relative_divide (zone_publish_time_window,
511 next_put_interval = GNUNET_TIME_relative_divide (put_interval,
512 LATE_ITERATION_SPEEDUP_FACTOR);
515 next_put_interval = put_interval;
517 GNUNET_STATISTICS_set (statistics,
518 "Current zone iteration interval (ms)",
519 next_put_interval.rel_value,
521 zone_publish_task = GNUNET_SCHEDULER_add_delayed (next_put_interval,
522 &publish_zone_dht_next,
528 * Periodically iterate over our zone and store everything in dht
531 * @param tc task context
534 publish_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
536 zone_publish_task = GNUNET_SCHEDULER_NO_TASK;
538 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Scheduling DHT zone update!\n");
539 /* start counting again */
540 num_public_records = 0;
541 namestore_iter = GNUNET_NAMESTORE_zone_iteration_start (namestore_handle,
542 NULL, /* All zones */
543 GNUNET_NAMESTORE_RF_AUTHORITY,
544 GNUNET_NAMESTORE_RF_PRIVATE,
550 /* END DHT ZONE PROPAGATION */
554 * Send shorten response back to client
556 * @param cls the closure containing a client shorten handle
557 * @param name the shortened name result or NULL if cannot be shortened
560 send_shorten_response (void* cls, const char* name)
562 struct ClientShortenHandle *csh = cls;
563 struct GNUNET_GNS_ClientShortenResultMessage *rmsg;
566 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message with %s\n",
567 "SHORTEN_RESULT", name);
571 name_len = strlen (name) + 1;
572 GNUNET_STATISTICS_update (statistics,
573 "Name shorten results", 1, GNUNET_NO);
575 rmsg = GNUNET_malloc (sizeof (struct GNUNET_GNS_ClientShortenResultMessage) +
578 rmsg->id = csh->request_id;
579 rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_SHORTEN_RESULT);
581 htons(sizeof(struct GNUNET_GNS_ClientShortenResultMessage) +
583 memcpy (&rmsg[1], name, name_len);
584 GNUNET_SERVER_notification_context_unicast (nc, csh->client,
587 if (NULL != csh->namestore_task)
588 GNUNET_NAMESTORE_cancel (csh->namestore_task);
595 * Lookup the zone infos and shorten name
597 * @param cls the client shorten handle
598 * @param key key of the zone
599 * @param expiration expiration of record
600 * @param name name found or null if no result
601 * @param rd_count number of records found
602 * @param rd record data
607 process_shorten_in_private_zone_lookup (void *cls,
608 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
609 struct GNUNET_TIME_Absolute expiration,
611 unsigned int rd_count,
612 const struct GNUNET_NAMESTORE_RecordData *rd,
613 const struct GNUNET_CRYPTO_RsaSignature *signature)
615 struct ClientShortenHandle *csh = cls;
616 struct GNUNET_CRYPTO_ShortHashCode *szone = &csh->shorten_zone;
617 struct GNUNET_CRYPTO_ShortHashCode *pzone = &csh->private_zone;
619 csh->namestore_task = NULL;
620 if (0 == strcmp (csh->private_zone_id, ""))
625 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
626 "No shorten zone in private zone!\n");
627 strcpy (csh->shorten_zone_id, "");
632 GNUNET_assert (rd_count == 1);
634 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
635 "Shorten zone %s found in private zone %s\n",
636 name, csh->private_zone_id);
638 sprintf (csh->shorten_zone_id, "%s.%s", name, csh->private_zone_id);
641 GNUNET_CONTAINER_DLL_remove (csh_head, csh_tail, csh);
643 gns_resolver_shorten_name (&csh->root_zone,
647 csh->private_zone_id,
648 csh->shorten_zone_id,
649 &send_shorten_response, csh);
655 * Lookup the zone infos and shorten name
657 * @param cls the shorten handle
658 * @param key key of the zone
659 * @param expiration expiration of record
660 * @param name name found or null if no result
661 * @param rd_count number of records found
662 * @param rd record data
667 process_shorten_in_root_zone_lookup (void *cls,
668 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
669 struct GNUNET_TIME_Absolute expiration,
671 unsigned int rd_count,
672 const struct GNUNET_NAMESTORE_RecordData *rd,
673 const struct GNUNET_CRYPTO_RsaSignature *signature)
675 struct ClientShortenHandle *csh = cls;
676 struct GNUNET_CRYPTO_ShortHashCode *szone = &csh->shorten_zone;
677 struct GNUNET_CRYPTO_ShortHashCode *pzone = &csh->private_zone;
679 csh->namestore_task = NULL;
680 if (0 == strcmp (csh->private_zone_id, ""))
685 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
686 "No shorten zone in zone and no private zone!\n");
688 strcpy (csh->shorten_zone_id, "");
690 GNUNET_CONTAINER_DLL_remove (csh_head, csh_tail, csh);
693 gns_resolver_shorten_name (&csh->root_zone,
697 csh->private_zone_id,
698 csh->shorten_zone_id,
699 &send_shorten_response, csh);
703 GNUNET_assert (rd_count == 1);
705 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
706 "Private zone %s found in root zone\n", name);
708 strcpy (csh->private_zone_id, name);
710 csh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
713 &process_shorten_in_private_zone_lookup,
719 * Lookup the zone infos and shorten name
721 * @param cls the shorten handle
722 * @param key key of the zone
723 * @param expiration expiration of record
724 * @param name name found or null if no result
725 * @param rd_count number of records found
726 * @param rd record data
731 process_private_in_root_zone_lookup (void *cls,
732 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
733 struct GNUNET_TIME_Absolute expiration,
735 unsigned int rd_count,
736 const struct GNUNET_NAMESTORE_RecordData *rd,
737 const struct GNUNET_CRYPTO_RsaSignature *signature)
739 struct ClientShortenHandle *csh = cls;
740 csh->namestore_task = NULL;
744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
745 "No private zone in root zone\n");
747 strcpy (csh->private_zone_id, "");
749 csh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
752 &process_shorten_in_root_zone_lookup,
757 GNUNET_assert (rd_count == 1);
759 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
760 "Private zone %s found in root zone\n", name);
762 strcpy (csh->private_zone_id, name);
764 csh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
767 &process_shorten_in_private_zone_lookup,
773 * Lookup the zone infos and shorten name
775 * @param csh the shorten handle
779 start_shorten_name (struct ClientShortenHandle *csh)
781 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
782 "Looking for private zone name in root zone\n");
784 csh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
787 &process_private_in_root_zone_lookup,
793 * Handle a shorten message from the api
795 * @param cls the closure (unused)
796 * @param client the client
797 * @param message the message
800 handle_shorten (void *cls,
801 struct GNUNET_SERVER_Client * client,
802 const struct GNUNET_MessageHeader * message)
804 struct ClientShortenHandle *csh;
806 char name[MAX_DNS_NAME_LENGTH];
807 char* nameptr = name;
809 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "SHORTEN");
810 msg_size = ntohs (message->size);
811 if (msg_size < sizeof (struct GNUNET_GNS_ClientShortenMessage))
814 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
819 const struct GNUNET_GNS_ClientShortenMessage *sh_msg =
820 (const struct GNUNET_GNS_ClientShortenMessage *) message;
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) > MAX_DNS_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_gnunet_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 start_shorten_name (csh);
868 GNUNET_STATISTICS_update (statistics,
869 "Name shorten attempts", 1, GNUNET_NO);
870 GNUNET_SERVER_receive_done (client, GNUNET_OK);
875 * Send get authority response back to client
877 * @param cls the closure containing a client get auth handle
878 * @param name the shortened name result or NULL if cannot be shortened
881 send_get_auth_response(void *cls, const char* name)
883 struct ClientGetAuthHandle *cah = cls;
884 struct GNUNET_GNS_ClientGetAuthResultMessage *rmsg;
886 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message with %s\n",
887 "GET_AUTH_RESULT", name);
890 GNUNET_STATISTICS_update (statistics,
891 "Authorities resolved", 1, GNUNET_NO);
896 rmsg = GNUNET_malloc(sizeof(struct GNUNET_GNS_ClientGetAuthResultMessage)
899 rmsg->id = cah->request_id;
900 rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_GET_AUTH_RESULT);
902 htons(sizeof(struct GNUNET_GNS_ClientGetAuthResultMessage) +
904 strcpy ((char*)&rmsg[1], name);
906 GNUNET_SERVER_notification_context_unicast (nc, cah->client,
909 GNUNET_SERVER_receive_done (cah->client, GNUNET_OK);
912 GNUNET_free_non_null(cah->name);
918 * Handle a get authority message from the api
920 * @param cls the closure
921 * @param client the client
922 * @param message the message
925 handle_get_authority (void *cls,
926 struct GNUNET_SERVER_Client * client,
927 const struct GNUNET_MessageHeader * message)
929 struct ClientGetAuthHandle *cah;
931 char name[MAX_DNS_NAME_LENGTH];
932 char* nameptr = name;
934 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "GET_AUTH");
935 msg_size = ntohs(message->size);
936 if (msg_size < sizeof (struct GNUNET_GNS_ClientGetAuthMessage))
939 GNUNET_SERVER_receive_done (client, GNUNET_OK);
942 GNUNET_SERVER_notification_context_add (nc, client);
944 struct GNUNET_GNS_ClientGetAuthMessage *sh_msg =
945 (struct GNUNET_GNS_ClientGetAuthMessage *) message;
947 GNUNET_STRINGS_utf8_tolower((const char*)&sh_msg[1], &nameptr);
950 cah = GNUNET_malloc(sizeof(struct ClientGetAuthHandle));
951 cah->client = client;
952 cah->request_id = sh_msg->id;
953 if (strlen (name) < strlen(GNUNET_GNS_TLD))
955 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
956 "GET_AUTH: `%s' is too short. Returning\n", name);
958 send_get_auth_response(cah, name);
961 if (strlen (name) > MAX_DNS_NAME_LENGTH)
963 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
964 "GET_AUTH: `%s' is too long", name);
966 send_get_auth_response(cah, name);
969 if (0 != strcmp (name + strlen (name) - strlen(GNUNET_GNS_TLD),
972 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
973 "GET_AUTH: %s is not our domain. Returning\n", name);
975 send_get_auth_response(cah, name);
979 if (0 == strcmp(name, GNUNET_GNS_TLD))
981 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
982 "GET_AUTH: %s is us. Returning\n", name);
984 send_get_auth_response(cah, name);
988 cah->name = GNUNET_malloc (strlen (name)
989 - strlen (GNUNET_GNS_TLD) + 1);
990 memcpy(cah->name, name,
991 strlen (name) - strlen (GNUNET_GNS_TLD));
993 /* Start delegation resolution in our namestore */
994 gns_resolver_get_authority (zone_hash, zone_hash, name,
995 &send_get_auth_response, cah);
996 GNUNET_STATISTICS_update (statistics,
997 "Authority lookup attempts", 1, GNUNET_NO);
1003 * Reply to client with the result from our lookup.
1005 * @param cls the closure (our client lookup handle)
1006 * @param rd_count the number of records
1007 * @param rd the record data
1010 send_lookup_response(void* cls,
1012 const struct GNUNET_NAMESTORE_RecordData *rd)
1014 struct ClientLookupHandle* clh = cls;
1015 struct GNUNET_GNS_ClientLookupResultMessage *rmsg;
1018 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message with %d results\n",
1019 "LOOKUP_RESULT", rd_count);
1021 len = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
1022 rmsg = GNUNET_malloc (
1023 len + sizeof (struct GNUNET_GNS_ClientLookupResultMessage));
1025 rmsg->id = clh->request_id;
1026 rmsg->rd_count = htonl(rd_count);
1027 rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT);
1029 htons(len+sizeof(struct GNUNET_GNS_ClientLookupResultMessage));
1031 GNUNET_NAMESTORE_records_serialize (rd_count, rd, len, (char*)&rmsg[1]);
1033 GNUNET_SERVER_notification_context_unicast (nc, clh->client,
1034 (const struct GNUNET_MessageHeader *) rmsg,
1036 GNUNET_SERVER_receive_done (clh->client, GNUNET_OK);
1039 GNUNET_free(clh->name);
1041 if (NULL != clh->shorten_key)
1042 GNUNET_CRYPTO_rsa_key_free (clh->shorten_key);
1045 GNUNET_STATISTICS_update (statistics,
1046 "Completed lookups", 1, GNUNET_NO);
1048 GNUNET_STATISTICS_update (statistics,
1049 "Records resolved", rd_count, GNUNET_NO);
1054 * Handle lookup requests from client
1056 * @param cls the closure
1057 * @param client the client
1058 * @param message the message
1061 handle_lookup(void *cls,
1062 struct GNUNET_SERVER_Client * client,
1063 const struct GNUNET_MessageHeader * message)
1066 char name[MAX_DNS_NAME_LENGTH];
1067 struct ClientLookupHandle *clh;
1068 char* nameptr = name;
1071 struct GNUNET_CRYPTO_RsaPrivateKey *key;
1072 struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded *pkey;
1075 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "LOOKUP");
1076 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "LOOKUP");
1077 msg_size = ntohs(message->size);
1078 if (msg_size < sizeof (struct GNUNET_GNS_ClientLookupMessage))
1081 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1084 sh_msg = (const struct GNUNET_GNS_ClientLookupMessage *) message;
1085 GNUNET_SERVER_notification_context_add (nc, client);
1087 struct GNUNET_GNS_ClientLookupMessage *sh_msg =
1088 (struct GNUNET_GNS_ClientLookupMessage *) message;
1090 if (GNUNET_YES == ntohl (sh_msg->have_key))
1092 pkey = (struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded *) &sh_msg[1];
1093 tmp_pkey = (char*) &sh_msg[1];
1094 key = GNUNET_CRYPTO_rsa_decode_key (tmp_pkey, ntohs (pkey->len));
1095 GNUNET_STRINGS_utf8_tolower (&tmp_pkey[ntohs (pkey->len)], &nameptr);
1100 GNUNET_STRINGS_utf8_tolower ((char*) &sh_msg[1], &nameptr);
1103 namelen = strlen(name)+1;
1104 clh = GNUNET_malloc (sizeof (struct ClientLookupHandle));
1105 memset (clh, 0, sizeof (struct ClientLookupHandle));
1106 clh->client = client;
1107 clh->name = GNUNET_malloc (namelen);
1108 strcpy (clh->name, name);
1109 clh->request_id = sh_msg->id;
1110 clh->type = ntohl (sh_msg->type);
1111 clh->shorten_key = key;
1113 only_cached = ntohl (sh_msg->only_cached);
1115 if (strlen (name) > MAX_DNS_NAME_LENGTH) {
1116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1117 "LOOKUP: %s is too long", name);
1119 send_lookup_response (clh, 0, NULL);
1123 if ((clh->type == GNUNET_GNS_RECORD_A) &&
1124 (GNUNET_OK != v4_enabled))
1126 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1127 "LOOKUP: Query for A record but AF_INET not supported!");
1129 send_lookup_response (clh, 0, NULL);
1133 if ((clh->type == GNUNET_GNS_RECORD_AAAA) &&
1134 (GNUNET_OK != v6_enabled))
1136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1137 "LOOKUP: Query for AAAA record but AF_INET6 not supported!");
1139 send_lookup_response (clh, 0, NULL);
1143 if (1 == ntohl (sh_msg->use_default_zone))
1144 clh->zone = zone_hash; //Default zone
1146 clh->zone = sh_msg->zone;
1148 if (GNUNET_YES == auto_import_pkey)
1150 gns_resolver_lookup_record (clh->zone, clh->zone, clh->type, clh->name,
1152 default_lookup_timeout,
1154 &send_lookup_response, clh);
1158 gns_resolver_lookup_record (clh->zone, clh->zone, clh->type, name,
1160 default_lookup_timeout,
1162 &send_lookup_response, clh);
1165 GNUNET_STATISTICS_update (statistics,
1166 "Record lookup attempts", 1, GNUNET_NO);
1171 * Test if the given AF is supported by this system.
1174 * @return GNUNET_OK if the AF is supported
1181 s = socket (af, SOCK_STREAM, 0);
1184 if (EAFNOSUPPORT == errno)
1186 fprintf (stderr, "Failed to create test socket: %s\n", STRERROR (errno));
1187 return GNUNET_SYSERR;
1199 * Process GNS requests.
1201 * @param cls closure)
1202 * @param server the initialized server
1203 * @param c configuration to use
1206 run (void *cls, struct GNUNET_SERVER_Handle *server,
1207 const struct GNUNET_CONFIGURATION_Handle *c)
1210 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
1211 unsigned long long max_parallel_bg_queries = 0;
1212 unsigned long long default_lookup_timeout_secs = 0;
1213 int ignore_pending = GNUNET_NO;
1215 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1216 {&handle_shorten, NULL, GNUNET_MESSAGE_TYPE_GNS_SHORTEN, 0},
1217 {&handle_lookup, NULL, GNUNET_MESSAGE_TYPE_GNS_LOOKUP, 0},
1218 {&handle_get_authority, NULL, GNUNET_MESSAGE_TYPE_GNS_GET_AUTH, 0}
1221 v6_enabled = test_af (AF_INET6);
1222 v4_enabled = test_af (AF_INET);
1224 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (c, "gns",
1225 "ZONEKEY", &keyfile))
1227 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1228 "No private key for root zone specified!\n");
1229 GNUNET_SCHEDULER_shutdown ();
1233 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1234 "Using keyfile %s for root zone.\n", keyfile);
1236 zone_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
1237 GNUNET_CRYPTO_rsa_key_get_public (zone_key, &pkey);
1239 GNUNET_CRYPTO_short_hash(&pkey,
1240 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1242 GNUNET_free(keyfile);
1243 namestore_handle = GNUNET_NAMESTORE_connect (c);
1244 if (NULL == namestore_handle)
1246 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1247 _("Failed to connect to the namestore!\n"));
1248 GNUNET_SCHEDULER_shutdown ();
1252 auto_import_pkey = GNUNET_NO;
1254 GNUNET_CONFIGURATION_get_value_yesno (c, "gns",
1255 "AUTO_IMPORT_PKEY"))
1257 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1258 "Automatic PKEY import is enabled.\n");
1259 auto_import_pkey = GNUNET_YES;
1263 put_interval = INITIAL_PUT_INTERVAL;
1265 zone_publish_time_window = DEFAULT_ZONE_PUBLISH_TIME_WINDOW;
1268 GNUNET_CONFIGURATION_get_value_time (c, "gns",
1269 "ZONE_PUBLISH_TIME_WINDOW",
1270 &zone_publish_time_window))
1272 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1273 "Time window for zone iteration: %s\n",
1274 GNUNET_STRINGS_relative_time_to_string (zone_publish_time_window, GNUNET_YES));
1278 GNUNET_CONFIGURATION_get_value_number (c, "gns",
1279 "MAX_PARALLEL_BACKGROUND_QUERIES",
1280 &max_parallel_bg_queries))
1282 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1283 "Number of allowed parallel background queries: %llu\n",
1284 max_parallel_bg_queries);
1288 GNUNET_CONFIGURATION_get_value_yesno (c, "gns",
1289 "AUTO_IMPORT_CONFIRMATION_REQ"))
1291 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1292 "Auto import requires user confirmation\n");
1293 ignore_pending = GNUNET_YES;
1297 GNUNET_CONFIGURATION_get_value_number(c, "gns",
1298 "DEFAULT_LOOKUP_TIMEOUT",
1299 &default_lookup_timeout_secs))
1301 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1302 "Default lookup timeout: %llus\n", default_lookup_timeout_secs);
1303 default_lookup_timeout = GNUNET_TIME_relative_multiply(
1304 GNUNET_TIME_UNIT_SECONDS,
1305 default_lookup_timeout_secs);
1311 dht_handle = GNUNET_DHT_connect(c,
1312 //max_parallel_bg_queries); //FIXME get ht_len from cfg
1315 if (NULL == dht_handle)
1317 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not connect to DHT!\n");
1320 if (gns_resolver_init(namestore_handle, dht_handle, zone_hash, c,
1321 max_parallel_bg_queries,
1325 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1326 "Unable to initialize resolver!\n");
1327 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
1332 GNUNET_CONFIGURATION_get_value_yesno (c, "gns", "HIJACK_DNS"))
1334 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1335 "DNS hijacking enabled... connecting to service.\n");
1337 if (gns_interceptor_init(zone_hash, zone_key, c) == GNUNET_SYSERR)
1339 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1340 "Failed to enable the dns interceptor!\n");
1345 * Schedule periodic put
1347 * We have roughly an hour for all records;
1349 first_zone_iteration = GNUNET_YES;
1350 zone_publish_task = GNUNET_SCHEDULER_add_now (&publish_zone_dht_start, NULL);
1352 GNUNET_SERVER_add_handlers (server, handlers);
1355 //GNUNET_SERVER_disconnect_notify (server,
1356 // &client_disconnect_notification,
1359 statistics = GNUNET_STATISTICS_create ("gns", c);
1361 nc = GNUNET_SERVER_notification_context_create (server, 1);
1363 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
1369 * The main function for the GNS service.
1371 * @param argc number of arguments from the command line
1372 * @param argv command line arguments
1373 * @return 0 ok, 1 on error
1376 main (int argc, char *const *argv)
1382 GNUNET_SERVICE_run (argc, argv, "gns", GNUNET_SERVICE_OPTION_NONE, &run,
1387 /* end of gnunet-service-gns.c */