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"
34 #include "block_gns.h"
36 #include "gns_common.h"
37 #include "gnunet-service-gns_resolver.h"
38 #include "gnunet-service-gns_interceptor.h"
39 #include "gnunet_protocols.h"
42 * The initial interval in milliseconds btween puts in
45 #define INITIAL_PUT_INTERVAL GNUNET_TIME_UNIT_MILLISECONDS
48 * The upper bound for the zone iteration interval in milliseconds
50 #define MINIMUM_ZONE_ITERATION_INTERVAL GNUNET_TIME_UNIT_SECONDS
53 * The default put interval for the zone iteration. In case
56 #define DEFAULT_ZONE_PUBLISH_TIME_WINDOW GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 4)
59 * The factor the current zone iteration interval is divided by for each
60 * additional new record
62 #define LATE_ITERATION_SPEEDUP_FACTOR 2
66 * Handle to a shorten operation from api
68 struct ClientShortenHandle
72 * List for all shorten requests
74 struct ClientShortenHandle *next;
77 * List for all shorten requests
79 struct ClientShortenHandle *prev;
82 * Handle to the requesting client
84 struct GNUNET_SERVER_Client *client;
87 * Namestore lookup task
89 struct GNUNET_NAMESTORE_QueueEntry *namestore_task;
94 struct GNUNET_CRYPTO_ShortHashCode root_zone;
99 struct GNUNET_CRYPTO_ShortHashCode private_zone;
104 struct GNUNET_CRYPTO_ShortHashCode shorten_zone;
114 enum GNUNET_GNS_RecordType type;
119 char name[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
122 * name of private zone (relative to root)
124 char private_zone_id[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
127 * name of shorten zone (relative to root)
129 char shorten_zone_id[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
135 * Handle to a get authority operation from api
137 struct ClientGetAuthHandle
140 * Handle to the requesting client
142 struct GNUNET_SERVER_Client *client;
145 * name to lookup authority
158 * Handle to a lookup operation from api
160 struct ClientLookupHandle
164 * Handle to the requesting client
166 struct GNUNET_SERVER_Client *client;
169 * optional zone private key used for shorten
171 struct GNUNET_CRYPTO_RsaPrivateKey *shorten_key;
174 * the name to look up
179 * The zone we look up in
181 struct GNUNET_CRYPTO_ShortHashCode zone;
189 * GNUNET_YES if we only want to lookup from local cache
196 enum GNUNET_GNS_RecordType type;
201 * Our handle to the DHT
203 static struct GNUNET_DHT_Handle *dht_handle;
206 * Our zone's private key
208 static struct GNUNET_CRYPTO_RsaPrivateKey *zone_key;
211 * Our handle to the namestore service
213 static struct GNUNET_NAMESTORE_Handle *namestore_handle;
216 * Handle to iterate over our authoritative zone in namestore
218 static struct GNUNET_NAMESTORE_ZoneIterator *namestore_iter;
221 * Our notification context.
223 static struct GNUNET_SERVER_NotificationContext *nc;
228 static struct GNUNET_CRYPTO_ShortHashCode zone_hash;
231 * Useful for zone update for DHT put
233 static unsigned long long num_public_records;
236 * Last seen record count
238 static unsigned long long last_num_public_records;
241 * Zone iteration PUT interval.
243 static struct GNUNET_TIME_Relative put_interval;
246 * Time window for zone iteration
248 static struct GNUNET_TIME_Relative zone_publish_time_window;
253 static GNUNET_SCHEDULER_TaskIdentifier zone_publish_task;
256 * GNUNET_YES if automatic pkey import for name shortening
259 static int auto_import_pkey;
262 * GNUNET_YES if zone has never been published before
264 static int first_zone_iteration;
269 static struct GNUNET_TIME_Relative default_lookup_timeout;
272 * GNUNET_YES if ipv6 is supported
274 static int v6_enabled;
277 * GNUNET_YES if ipv4 is supported
279 static int v4_enabled;
282 * List for shorten requests
284 static struct ClientShortenHandle *csh_head;
287 * List for shorten requests
289 static struct ClientShortenHandle *csh_tail;
292 * Handle to the statistics service
294 static struct GNUNET_STATISTICS_Handle *statistics;
298 * Task run during shutdown.
304 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
306 struct ClientShortenHandle *csh_tmp;
308 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
310 while (NULL != (csh_tmp = csh_head))
312 GNUNET_CONTAINER_DLL_remove (csh_head, csh_tail, csh_tmp);
313 GNUNET_free (csh_tmp);
315 GNUNET_SERVER_notification_context_destroy (nc);
316 gns_interceptor_stop ();
317 gns_resolver_cleanup ();
318 if (NULL != statistics)
320 GNUNET_STATISTICS_destroy (statistics, GNUNET_NO);
323 if (GNUNET_SCHEDULER_NO_TASK != zone_publish_task)
325 GNUNET_SCHEDULER_cancel (zone_publish_task);
326 zone_publish_task = GNUNET_SCHEDULER_NO_TASK;
328 if (NULL != namestore_iter)
330 GNUNET_NAMESTORE_zone_iteration_stop (namestore_iter);
331 namestore_iter = NULL;
333 if (NULL != namestore_handle)
335 GNUNET_NAMESTORE_disconnect (namestore_handle);
336 namestore_handle = NULL;
338 if (NULL != dht_handle)
340 GNUNET_DHT_disconnect (dht_handle);
347 * Method called periodically that triggers iteration over authoritative records
350 * @param tc task context
353 publish_zone_dht_next (void *cls,
354 const struct GNUNET_SCHEDULER_TaskContext *tc)
356 zone_publish_task = GNUNET_SCHEDULER_NO_TASK;
357 GNUNET_NAMESTORE_zone_iterator_next (namestore_iter);
362 * Periodically iterate over our zone and store everything in dht
365 * @param tc task context
368 publish_zone_dht_start (void *cls,
369 const struct GNUNET_SCHEDULER_TaskContext *tc);
373 * Function used to put all records successively into the DHT.
375 * @param cls the closure (NULL)
376 * @param key the public key of the authority (ours)
377 * @param expiration lifetime of the namestore entry
378 * @param name the name of the records
379 * @param rd_count the number of records in data
380 * @param rd the record data
381 * @param signature the signature for the record data
384 put_gns_record (void *cls,
385 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
386 struct GNUNET_TIME_Absolute expiration,
388 unsigned int rd_count,
389 const struct GNUNET_NAMESTORE_RecordData *rd,
390 const struct GNUNET_CRYPTO_RsaSignature *signature)
392 struct GNSNameRecordBlock *nrb;
393 struct GNUNET_CRYPTO_ShortHashCode zhash;
394 struct GNUNET_HashCode dht_key;
395 uint32_t rd_payload_length;
396 char* nrb_data = NULL;
398 struct GNUNET_TIME_Relative next_put_interval;
403 namestore_iter = NULL;
404 last_num_public_records = num_public_records;
405 first_zone_iteration = GNUNET_NO;
406 if (0 == num_public_records)
409 * If no records are known (startup) or none present
410 * we can safely set the interval to the value for a single
413 put_interval = zone_publish_time_window;
414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
415 "No records in db.\n");
419 put_interval = GNUNET_TIME_relative_divide (zone_publish_time_window,
422 put_interval = GNUNET_TIME_relative_max (MINIMUM_ZONE_ITERATION_INTERVAL,
425 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
426 "Zone iteration finished. Adjusted zone iteration interval to %s\n",
427 GNUNET_STRINGS_relative_time_to_string (put_interval, GNUNET_YES));
428 GNUNET_STATISTICS_set (statistics,
429 "Current zone iteration interval (in ms)",
430 put_interval.rel_value,
432 GNUNET_STATISTICS_update (statistics,
433 "Number of zone iterations", 1, GNUNET_NO);
434 GNUNET_STATISTICS_set (statistics,
435 "Number of public records in DHT",
436 last_num_public_records,
438 if (0 == num_public_records)
439 zone_publish_task = GNUNET_SCHEDULER_add_delayed (put_interval,
440 &publish_zone_dht_start,
443 zone_publish_task = GNUNET_SCHEDULER_add_now (&publish_zone_dht_start, NULL);
447 namelen = strlen (name) + 1;
450 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
451 "No records for name `%s'! Skipping.\n",
453 zone_publish_task = GNUNET_SCHEDULER_add_now (&publish_zone_dht_next,
457 if (NULL == signature)
460 zone_publish_task = GNUNET_SCHEDULER_add_now (&publish_zone_dht_next,
465 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
466 "Putting records for `%s' into the DHT\n", name);
467 rd_payload_length = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
468 nrb = GNUNET_malloc (rd_payload_length + namelen
469 + sizeof (struct GNSNameRecordBlock));
470 nrb->signature = *signature;
471 nrb->public_key = *key;
472 nrb->rd_count = htonl (rd_count);
473 memcpy (&nrb[1], name, namelen);
474 nrb_data = (char *) &nrb[1];
476 rd_payload_length += sizeof(struct GNSNameRecordBlock) + namelen;
477 GNUNET_CRYPTO_short_hash (key,
478 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
480 if (-1 == GNUNET_NAMESTORE_records_serialize (rd_count,
485 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
486 _("Records for name `%s' in zone %s too large to fit into DHT"),
488 GNUNET_short_h2s (&zhash));
490 zone_publish_task = GNUNET_SCHEDULER_add_now (&publish_zone_dht_next,
495 GNUNET_GNS_get_key_for_record (name, &zhash, &dht_key);
496 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
497 "putting %u records from zone %s for `%s' under key: %s with size %u and timeout %s\n",
499 GNUNET_short_h2s (&zhash),
501 GNUNET_h2s (&dht_key),
502 (unsigned int) rd_payload_length,
503 GNUNET_STRINGS_relative_time_to_string (DHT_OPERATION_TIMEOUT, GNUNET_YES));
505 GNUNET_STATISTICS_update (statistics,
506 "Record bytes put into DHT",
507 rd_payload_length, GNUNET_NO);
509 (void) GNUNET_DHT_put (dht_handle, &dht_key,
510 DHT_GNS_REPLICATION_LEVEL,
511 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
512 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
516 DHT_OPERATION_TIMEOUT,
521 num_public_records++;
522 if ( (num_public_records > last_num_public_records)
523 && (GNUNET_NO == first_zone_iteration) )
525 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
526 "Last record count was lower than current record count. Reducing interval.\n");
527 put_interval = GNUNET_TIME_relative_divide (zone_publish_time_window,
529 next_put_interval = GNUNET_TIME_relative_divide (put_interval,
530 LATE_ITERATION_SPEEDUP_FACTOR);
533 next_put_interval = put_interval;
535 GNUNET_STATISTICS_set (statistics,
536 "Current zone iteration interval (ms)",
537 next_put_interval.rel_value,
539 zone_publish_task = GNUNET_SCHEDULER_add_delayed (next_put_interval,
540 &publish_zone_dht_next,
546 * Periodically iterate over our zone and store everything in dht
549 * @param tc task context
552 publish_zone_dht_start (void *cls,
553 const struct GNUNET_SCHEDULER_TaskContext *tc)
555 zone_publish_task = GNUNET_SCHEDULER_NO_TASK;
557 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
558 "Scheduling DHT zone update!\n");
559 /* start counting again */
560 num_public_records = 0;
561 namestore_iter = GNUNET_NAMESTORE_zone_iteration_start (namestore_handle,
562 NULL, /* All zones */
563 GNUNET_NAMESTORE_RF_AUTHORITY,
564 GNUNET_NAMESTORE_RF_PRIVATE,
570 /* END DHT ZONE PROPAGATION */
574 * Send shorten response back to client
576 * @param cls the closure containing a client shorten handle
577 * @param name the shortened name result or NULL if cannot be shortened
580 send_shorten_response (void* cls, const char* name)
582 struct ClientShortenHandle *csh = cls;
583 struct GNUNET_GNS_ClientShortenResultMessage *rmsg;
586 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
587 "Sending `%s' message with %s\n",
588 "SHORTEN_RESULT", name);
592 name_len = strlen (name) + 1;
593 GNUNET_STATISTICS_update (statistics,
594 "Name shorten results", 1, GNUNET_NO);
596 rmsg = GNUNET_malloc (sizeof (struct GNUNET_GNS_ClientShortenResultMessage) +
599 rmsg->id = csh->request_id;
600 rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_SHORTEN_RESULT);
602 htons(sizeof(struct GNUNET_GNS_ClientShortenResultMessage) +
604 memcpy (&rmsg[1], name, name_len);
605 GNUNET_SERVER_notification_context_unicast (nc, csh->client,
608 if (NULL != csh->namestore_task)
609 GNUNET_NAMESTORE_cancel (csh->namestore_task);
616 * Lookup the zone infos and shorten name
618 * @param cls the client shorten handle
619 * @param key key of the zone
620 * @param expiration expiration of record
621 * @param name name found or null if no result
622 * @param rd_count number of records found
623 * @param rd record data
628 process_shorten_in_private_zone_lookup (void *cls,
629 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
630 struct GNUNET_TIME_Absolute expiration,
632 unsigned int rd_count,
633 const struct GNUNET_NAMESTORE_RecordData *rd,
634 const struct GNUNET_CRYPTO_RsaSignature *signature)
636 struct ClientShortenHandle *csh = cls;
637 struct GNUNET_CRYPTO_ShortHashCode *szone = &csh->shorten_zone;
638 struct GNUNET_CRYPTO_ShortHashCode *pzone = &csh->private_zone;
640 csh->namestore_task = NULL;
641 if (0 == strcmp (csh->private_zone_id, ""))
645 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
646 "No shorten zone in private zone!\n");
647 strcpy (csh->shorten_zone_id, "");
652 GNUNET_break (1 == rd_count);
653 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
654 "Shorten zone %s found in private zone %s\n",
655 name, csh->private_zone_id);
657 sprintf (csh->shorten_zone_id, "%s.%s", name, csh->private_zone_id);
659 GNUNET_CONTAINER_DLL_remove (csh_head, csh_tail, csh);
660 gns_resolver_shorten_name (&csh->root_zone,
664 csh->private_zone_id,
665 csh->shorten_zone_id,
666 &send_shorten_response, csh);
672 * Lookup the zone infos and shorten name
674 * @param cls the shorten handle
675 * @param key key of the zone
676 * @param expiration expiration of record
677 * @param name name found or null if no result
678 * @param rd_count number of records found
679 * @param rd record data
684 process_shorten_in_root_zone_lookup (void *cls,
685 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
686 struct GNUNET_TIME_Absolute expiration,
688 unsigned int rd_count,
689 const struct GNUNET_NAMESTORE_RecordData *rd,
690 const struct GNUNET_CRYPTO_RsaSignature *signature)
692 struct ClientShortenHandle *csh = cls;
693 struct GNUNET_CRYPTO_ShortHashCode *szone = &csh->shorten_zone;
694 struct GNUNET_CRYPTO_ShortHashCode *pzone = &csh->private_zone;
696 csh->namestore_task = NULL;
697 if (0 == strcmp (csh->private_zone_id, ""))
701 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
702 "No shorten zone in zone and no private zone!\n");
704 strcpy (csh->shorten_zone_id, "");
705 GNUNET_CONTAINER_DLL_remove (csh_head, csh_tail, csh);
707 gns_resolver_shorten_name (&csh->root_zone,
711 csh->private_zone_id,
712 csh->shorten_zone_id,
713 &send_shorten_response, csh);
716 GNUNET_break (rd_count == 1);
717 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
718 "Private zone %s found in root zone\n", name);
719 strcpy (csh->private_zone_id, name);
720 csh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
723 &process_shorten_in_private_zone_lookup,
729 * Lookup the zone infos and shorten name
731 * @param cls the shorten handle
732 * @param key key of the zone
733 * @param expiration expiration of record
734 * @param name name found or null if no result
735 * @param rd_count number of records found
736 * @param rd record data
740 process_private_in_root_zone_lookup (void *cls,
741 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
742 struct GNUNET_TIME_Absolute expiration,
744 unsigned int rd_count,
745 const struct GNUNET_NAMESTORE_RecordData *rd,
746 const struct GNUNET_CRYPTO_RsaSignature *signature)
748 struct ClientShortenHandle *csh = cls;
750 csh->namestore_task = NULL;
753 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
754 "No private zone in root zone\n");
755 strcpy (csh->private_zone_id, "");
756 csh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
759 &process_shorten_in_root_zone_lookup,
763 GNUNET_break (1 == rd_count);
764 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
765 "Private zone `%s' found in root zone\n",
767 strcpy (csh->private_zone_id, name);
768 csh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
771 &process_shorten_in_private_zone_lookup,
777 * Handle a shorten message from the api
779 * @param cls the closure (unused)
780 * @param client the client
781 * @param message the message
784 handle_shorten (void *cls,
785 struct GNUNET_SERVER_Client * client,
786 const struct GNUNET_MessageHeader * message)
788 struct ClientShortenHandle *csh;
790 char name[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
791 char* nameptr = name;
793 const struct GNUNET_GNS_ClientShortenMessage *sh_msg;
795 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
796 "Received `%s' message\n", "SHORTEN");
797 msg_size = ntohs (message->size);
798 if (msg_size < sizeof (struct GNUNET_GNS_ClientShortenMessage))
801 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
804 sh_msg = (const struct GNUNET_GNS_ClientShortenMessage *) message;
805 utf_in = (const char *) &sh_msg[1];
806 if ('\0' != utf_in[msg_size - sizeof (struct GNUNET_GNS_ClientShortenMessage) - 1])
809 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
812 csh = GNUNET_malloc(sizeof (struct ClientShortenHandle));
813 csh->client = client;
814 csh->request_id = sh_msg->id;
815 GNUNET_CONTAINER_DLL_insert (csh_head, csh_tail, csh);
816 GNUNET_STRINGS_utf8_tolower (utf_in, &nameptr);
817 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
818 "SHORTEN: Converted `%s' to `%s'\n",
821 GNUNET_SERVER_notification_context_add (nc, client);
822 if (strlen (name) < strlen (GNUNET_GNS_TLD))
824 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
825 "SHORTEN: %s is too short\n", name);
826 GNUNET_CONTAINER_DLL_remove (csh_head, csh_tail, csh);
827 send_shorten_response(csh, name);
828 GNUNET_SERVER_receive_done (client, GNUNET_OK);
831 if (strlen (name) > GNUNET_DNSPARSER_MAX_NAME_LENGTH)
833 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
834 "SHORTEN: %s is too long\n", name);
835 GNUNET_CONTAINER_DLL_remove (csh_head, csh_tail, csh);
836 send_shorten_response(csh, name);
837 GNUNET_SERVER_receive_done (client, GNUNET_OK);
840 if ( (! is_gads_tld (name)) &&
841 (! is_zkey_tld (name)) )
843 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
844 "%s is not our domain. Returning\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 csh->shorten_zone = sh_msg->shorten_zone;
851 csh->private_zone = sh_msg->private_zone;
852 strcpy (csh->name, name);
853 if (1 == ntohl(sh_msg->use_default_zone))
854 csh->root_zone = zone_hash; //Default zone
856 csh->root_zone = sh_msg->zone;
857 csh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
860 &process_private_in_root_zone_lookup,
862 GNUNET_STATISTICS_update (statistics,
863 "Name shorten attempts", 1, GNUNET_NO);
864 GNUNET_SERVER_receive_done (client, GNUNET_OK);
869 * Send get authority response back to client
871 * @param cls the closure containing a client get auth handle
872 * @param name the name of the authority, or NULL on error
875 send_get_auth_response (void *cls,
878 struct ClientGetAuthHandle *cah = cls;
879 struct GNUNET_GNS_ClientGetAuthResultMessage *rmsg;
881 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
882 "Sending `%s' message with `%s'\n",
883 "GET_AUTH_RESULT", name);
886 GNUNET_STATISTICS_update (statistics,
887 "Authorities resolved", 1, GNUNET_NO);
891 rmsg = GNUNET_malloc (sizeof (struct GNUNET_GNS_ClientGetAuthResultMessage)
892 + strlen (name) + 1);
894 rmsg->id = cah->request_id;
895 rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_GET_AUTH_RESULT);
897 htons(sizeof(struct GNUNET_GNS_ClientGetAuthResultMessage) +
899 strcpy ((char*)&rmsg[1], name);
901 GNUNET_SERVER_notification_context_unicast (nc, cah->client,
904 GNUNET_SERVER_receive_done (cah->client, GNUNET_OK);
906 GNUNET_free_non_null(cah->name);
912 * Handle a get authority message from the api
914 * @param cls the closure
915 * @param client the client
916 * @param message the message
919 handle_get_authority (void *cls,
920 struct GNUNET_SERVER_Client * client,
921 const struct GNUNET_MessageHeader * message)
923 struct ClientGetAuthHandle *cah;
925 char name[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
926 char* nameptr = name;
928 const struct GNUNET_GNS_ClientGetAuthMessage *sh_msg;
930 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
931 "Received `%s' message\n", "GET_AUTH");
932 msg_size = ntohs(message->size);
933 if (msg_size < sizeof (struct GNUNET_GNS_ClientGetAuthMessage))
936 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
939 GNUNET_SERVER_notification_context_add (nc, client);
940 sh_msg = (const struct GNUNET_GNS_ClientGetAuthMessage *) message;
941 utf_in = (const char *) &sh_msg[1];
942 if ('\0' != utf_in[msg_size - sizeof (struct GNUNET_GNS_ClientGetAuthMessage) - 1])
945 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
948 GNUNET_STRINGS_utf8_tolower(utf_in, &nameptr);
949 cah = GNUNET_malloc(sizeof(struct ClientGetAuthHandle));
950 cah->client = client;
951 cah->request_id = sh_msg->id;
952 if (strlen (name) < strlen(GNUNET_GNS_TLD))
954 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
955 "GET_AUTH: `%s' is too short. Returning\n", name);
957 send_get_auth_response(cah, name);
960 if (strlen (name) > GNUNET_DNSPARSER_MAX_NAME_LENGTH)
962 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
963 "GET_AUTH: `%s' is too long", name);
965 send_get_auth_response(cah, name);
968 if (0 != strcmp (name + strlen (name) - strlen (GNUNET_GNS_TLD),
971 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
972 "GET_AUTH: %s is not our domain. Returning\n", name);
974 send_get_auth_response (cah, name);
978 if (0 == strcmp (name, GNUNET_GNS_TLD))
980 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
981 "GET_AUTH: %s is us. Returning\n", name);
983 send_get_auth_response(cah, name);
987 cah->name = GNUNET_malloc (strlen (name)
988 - strlen (GNUNET_GNS_TLD) + 1);
989 memcpy (cah->name, name,
990 strlen (name) - strlen (GNUNET_GNS_TLD));
992 /* Start delegation resolution in our namestore */
993 gns_resolver_get_authority (zone_hash, zone_hash, name,
994 &send_get_auth_response, cah);
995 GNUNET_STATISTICS_update (statistics,
996 "Authority lookup attempts", 1, GNUNET_NO);
1001 * Reply to client with the result from our lookup.
1003 * @param cls the closure (our client lookup handle)
1004 * @param rd_count the number of records
1005 * @param rd the record data
1008 send_lookup_response (void* cls,
1010 const struct GNUNET_NAMESTORE_RecordData *rd)
1012 struct ClientLookupHandle* clh = cls;
1013 struct GNUNET_GNS_ClientLookupResultMessage *rmsg;
1016 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message with %d results\n",
1017 "LOOKUP_RESULT", rd_count);
1019 len = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
1020 rmsg = GNUNET_malloc (len + sizeof (struct GNUNET_GNS_ClientLookupResultMessage));
1022 rmsg->id = clh->request_id;
1023 rmsg->rd_count = htonl(rd_count);
1024 rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT);
1026 htons(len+sizeof(struct GNUNET_GNS_ClientLookupResultMessage));
1028 GNUNET_NAMESTORE_records_serialize (rd_count, rd, len, (char*)&rmsg[1]);
1030 GNUNET_SERVER_notification_context_unicast (nc, clh->client,
1031 (const struct GNUNET_MessageHeader *) rmsg,
1033 GNUNET_SERVER_receive_done (clh->client, GNUNET_OK);
1036 GNUNET_free(clh->name);
1038 if (NULL != clh->shorten_key)
1039 GNUNET_CRYPTO_rsa_key_free (clh->shorten_key);
1041 GNUNET_STATISTICS_update (statistics,
1042 "Completed lookups", 1, GNUNET_NO);
1044 GNUNET_STATISTICS_update (statistics,
1045 "Records resolved", rd_count, GNUNET_NO);
1050 * Handle lookup requests from client
1052 * @param cls the closure
1053 * @param client the client
1054 * @param message the message
1057 handle_lookup (void *cls,
1058 struct GNUNET_SERVER_Client * client,
1059 const struct GNUNET_MessageHeader * message)
1062 char name[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
1063 struct ClientLookupHandle *clh;
1064 char* nameptr = name;
1067 struct GNUNET_CRYPTO_RsaPrivateKey *key;
1068 struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded *pkey;
1071 const struct GNUNET_GNS_ClientLookupMessage *sh_msg;
1073 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1074 "Received `%s' message\n", "LOOKUP");
1075 msg_size = ntohs(message->size);
1076 if (msg_size < sizeof (struct GNUNET_GNS_ClientLookupMessage))
1079 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1082 sh_msg = (const struct GNUNET_GNS_ClientLookupMessage *) message;
1083 GNUNET_SERVER_notification_context_add (nc, client);
1084 if (GNUNET_YES == ntohl (sh_msg->have_key))
1086 pkey = (struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded *) &sh_msg[1];
1087 tmp_pkey = (char*) &sh_msg[1];
1088 key = GNUNET_CRYPTO_rsa_decode_key (tmp_pkey, ntohs (pkey->len));
1089 GNUNET_STRINGS_utf8_tolower (&tmp_pkey[ntohs (pkey->len)], &nameptr);
1094 utf_in = (const char *) &sh_msg[1];
1095 if ('\0' != utf_in[msg_size - sizeof (struct GNUNET_GNS_ClientLookupMessage) - 1])
1098 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1101 GNUNET_STRINGS_utf8_tolower (utf_in, &nameptr);
1104 namelen = strlen (name) + 1;
1105 clh = GNUNET_malloc (sizeof (struct ClientLookupHandle));
1106 memset (clh, 0, sizeof (struct ClientLookupHandle));
1107 clh->client = client;
1108 clh->name = GNUNET_malloc (namelen);
1109 strcpy (clh->name, name);
1110 clh->request_id = sh_msg->id;
1111 clh->type = ntohl (sh_msg->type);
1112 clh->shorten_key = key;
1114 only_cached = ntohl (sh_msg->only_cached);
1116 if (strlen (name) > GNUNET_DNSPARSER_MAX_NAME_LENGTH) {
1117 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1118 "LOOKUP: %s is too long", name);
1120 send_lookup_response (clh, 0, NULL);
1124 if ((GNUNET_GNS_RECORD_A == clh->type) &&
1125 (GNUNET_OK != v4_enabled))
1127 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1128 "LOOKUP: Query for A record but AF_INET not supported!");
1130 send_lookup_response (clh, 0, NULL);
1134 if ((GNUNET_GNS_RECORD_AAAA == clh->type) &&
1135 (GNUNET_OK != v6_enabled))
1137 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1138 "LOOKUP: Query for AAAA record but AF_INET6 not supported!");
1140 send_lookup_response (clh, 0, NULL);
1144 if (1 == ntohl (sh_msg->use_default_zone))
1145 clh->zone = zone_hash; /* Default zone */
1147 clh->zone = sh_msg->zone;
1149 if (GNUNET_YES == auto_import_pkey)
1151 gns_resolver_lookup_record (clh->zone, clh->zone, clh->type, clh->name,
1153 default_lookup_timeout,
1155 &send_lookup_response, clh);
1159 gns_resolver_lookup_record (clh->zone, clh->zone, clh->type, name,
1161 default_lookup_timeout,
1163 &send_lookup_response, clh);
1165 GNUNET_STATISTICS_update (statistics,
1166 "Record lookup attempts", 1, GNUNET_NO);
1171 * Process GNS requests.
1173 * @param cls closure
1174 * @param server the initialized server
1175 * @param c configuration to use
1178 run (void *cls, struct GNUNET_SERVER_Handle *server,
1179 const struct GNUNET_CONFIGURATION_Handle *c)
1181 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1182 {&handle_shorten, NULL, GNUNET_MESSAGE_TYPE_GNS_SHORTEN, 0},
1183 {&handle_lookup, NULL, GNUNET_MESSAGE_TYPE_GNS_LOOKUP, 0},
1184 {&handle_get_authority, NULL, GNUNET_MESSAGE_TYPE_GNS_GET_AUTH, 0}
1187 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
1188 unsigned long long max_parallel_bg_queries = 0;
1189 int ignore_pending = GNUNET_NO;
1191 v6_enabled = GNUNET_NETWORK_test_pf (PF_INET6);
1192 v4_enabled = GNUNET_NETWORK_test_pf (PF_INET);
1194 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (c, "gns",
1195 "ZONEKEY", &keyfile))
1197 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1198 "No private key for root zone specified!\n");
1199 GNUNET_SCHEDULER_shutdown ();
1203 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1204 "Using keyfile %s for root zone.\n", keyfile);
1206 zone_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
1207 GNUNET_CRYPTO_rsa_key_get_public (zone_key, &pkey);
1208 GNUNET_CRYPTO_short_hash(&pkey,
1209 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1211 GNUNET_free(keyfile);
1212 namestore_handle = GNUNET_NAMESTORE_connect (c);
1213 if (NULL == namestore_handle)
1215 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1216 _("Failed to connect to the namestore!\n"));
1217 GNUNET_SCHEDULER_shutdown ();
1221 auto_import_pkey = GNUNET_NO;
1223 GNUNET_CONFIGURATION_get_value_yesno (c, "gns",
1224 "AUTO_IMPORT_PKEY"))
1226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1227 "Automatic PKEY import is enabled.\n");
1228 auto_import_pkey = GNUNET_YES;
1230 put_interval = INITIAL_PUT_INTERVAL;
1231 zone_publish_time_window = DEFAULT_ZONE_PUBLISH_TIME_WINDOW;
1234 GNUNET_CONFIGURATION_get_value_time (c, "gns",
1235 "ZONE_PUBLISH_TIME_WINDOW",
1236 &zone_publish_time_window))
1238 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1239 "Time window for zone iteration: %s\n",
1240 GNUNET_STRINGS_relative_time_to_string (zone_publish_time_window, GNUNET_YES));
1243 GNUNET_CONFIGURATION_get_value_number (c, "gns",
1244 "MAX_PARALLEL_BACKGROUND_QUERIES",
1245 &max_parallel_bg_queries))
1247 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1248 "Number of allowed parallel background queries: %llu\n",
1249 max_parallel_bg_queries);
1253 GNUNET_CONFIGURATION_get_value_yesno (c, "gns",
1254 "AUTO_IMPORT_CONFIRMATION_REQ"))
1256 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1257 "Auto import requires user confirmation\n");
1258 ignore_pending = GNUNET_YES;
1262 GNUNET_CONFIGURATION_get_value_time (c, "gns",
1263 "DEFAULT_LOOKUP_TIMEOUT",
1264 &default_lookup_timeout))
1266 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1267 "Default lookup timeout: %s\n",
1268 GNUNET_STRINGS_relative_time_to_string (default_lookup_timeout,
1272 dht_handle = GNUNET_DHT_connect (c,
1273 (unsigned int) max_parallel_bg_queries);
1274 if (NULL == dht_handle)
1276 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1277 _("Could not connect to DHT!\n"));
1278 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
1282 if (GNUNET_SYSERR ==
1283 gns_resolver_init (namestore_handle, dht_handle, zone_hash, c,
1284 max_parallel_bg_queries,
1287 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1288 _("Unable to initialize resolver!\n"));
1289 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
1294 GNUNET_CONFIGURATION_get_value_yesno (c, "gns", "HIJACK_DNS"))
1296 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1297 "DNS hijacking enabled. Connecting to DNS service.\n");
1299 if (GNUNET_SYSERR ==
1300 gns_interceptor_init (zone_hash, zone_key, c))
1302 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1303 "Failed to enable the DNS interceptor!\n");
1308 * Schedule periodic put
1310 * We have roughly an hour for all records;
1312 first_zone_iteration = GNUNET_YES;
1313 zone_publish_task = GNUNET_SCHEDULER_add_now (&publish_zone_dht_start, NULL);
1314 GNUNET_SERVER_add_handlers (server, handlers);
1315 statistics = GNUNET_STATISTICS_create ("gns", c);
1316 nc = GNUNET_SERVER_notification_context_create (server, 1);
1317 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
1323 * The main function for the GNS service.
1325 * @param argc number of arguments from the command line
1326 * @param argv command line arguments
1327 * @return 0 ok, 1 on error
1330 main (int argc, char *const *argv)
1336 GNUNET_SERVICE_run (argc, argv, "gns", GNUNET_SERVICE_OPTION_NONE, &run,
1341 /* end of gnunet-service-gns.c */