2 This file is part of GNUnet.
3 (C) 2009, 2010, 2011 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 "block_gns.h"
37 #include "gnunet-service-gns_resolver.h"
38 #include "gnunet-service-gns_interceptor.h"
40 /* FIXME move to proper header in include */
41 #define GNUNET_MESSAGE_TYPE_GNS_LOOKUP 23
42 #define GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT 24
43 #define GNUNET_MESSAGE_TYPE_GNS_SHORTEN 25
44 #define GNUNET_MESSAGE_TYPE_GNS_SHORTEN_RESULT 26
45 #define GNUNET_MESSAGE_TYPE_GNS_GET_AUTH 27
46 #define GNUNET_MESSAGE_TYPE_GNS_GET_AUTH_RESULT 28
50 * Handle to a shorten operation from api
52 struct ClientShortenHandle
54 /* the requesting client that */
55 struct GNUNET_SERVER_Client *client;
61 enum GNUNET_GNS_RecordType type;
63 /* optional zone private key used for shorten */
64 struct GNUNET_CRYPTO_RsaPrivateKey *shorten_key;
73 * Handle to a get auhtority operation from api
75 struct ClientGetAuthHandle
77 /* the requesting client that */
78 struct GNUNET_SERVER_Client *client;
83 /* name to lookup authority */
90 * Handle to a lookup operation from api
92 struct ClientLookupHandle
94 /* the requesting client that */
95 struct GNUNET_SERVER_Client *client;
101 enum GNUNET_GNS_RecordType type;
103 /* optional zone private key used for shorten */
104 struct GNUNET_CRYPTO_RsaPrivateKey *shorten_key;
106 /* the name to look up */
107 char* name; //Needed?
111 * Our handle to the DHT
113 static struct GNUNET_DHT_Handle *dht_handle;
116 * Our zone's private key
118 struct GNUNET_CRYPTO_RsaPrivateKey *zone_key;
121 * Our handle to the namestore service
122 * FIXME maybe need a second handle for iteration
124 struct GNUNET_NAMESTORE_Handle *namestore_handle;
127 * Handle to iterate over our authoritative zone in namestore
129 struct GNUNET_NAMESTORE_ZoneIterator *namestore_iter;
132 * The configuration the GNS service is running with
134 const struct GNUNET_CONFIGURATION_Handle *GNS_cfg;
137 * Our notification context.
139 static struct GNUNET_SERVER_NotificationContext *nc;
144 struct GNUNET_CRYPTO_ShortHashCode zone_hash;
147 * Useful for zone update for DHT put
149 static int num_public_records;
152 * update interval in seconds
154 static unsigned long long max_record_put_interval;
156 static unsigned long long dht_max_update_interval;
158 /* dht update interval FIXME define? */
159 static struct GNUNET_TIME_Relative record_put_interval;
161 /* zone update task */
162 GNUNET_SCHEDULER_TaskIdentifier zone_update_taskid = GNUNET_SCHEDULER_NO_TASK;
164 /* automatic pkey import for name shortening */
165 static int auto_import_pkey;
168 static struct GNUNET_TIME_Relative default_lookup_timeout;
174 on_resolver_cleanup(void)
176 GNUNET_NAMESTORE_disconnect(namestore_handle, 1);
177 GNUNET_DHT_disconnect(dht_handle);
181 * Task run during shutdown.
187 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
190 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
192 /* Kill zone task for it may make the scheduler hang */
193 if (zone_update_taskid != GNUNET_SCHEDULER_NO_TASK)
194 GNUNET_SCHEDULER_cancel(zone_update_taskid);
196 GNUNET_SERVER_notification_context_destroy (nc);
198 gns_interceptor_stop();
199 gns_resolver_cleanup(&on_resolver_cleanup);
205 * Method called periodicattluy that triggers
206 * iteration over root zone
209 * @param tc task context
212 update_zone_dht_next(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
214 zone_update_taskid = GNUNET_SCHEDULER_NO_TASK;
215 GNUNET_NAMESTORE_zone_iterator_next(namestore_iter);
219 * Continuation for DHT put
222 * @param success GNUNET_OK if the PUT was transmitted,
223 * GNUNET_NO on timeout,
224 * GNUNET_SYSERR on disconnect from service
225 * after the PUT message was transmitted
226 * (so we don't know if it was received or not)
229 record_dht_put(void *cls, int success)
231 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "put request transmitted\n");
236 update_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
239 * Function used to put all records successively into the DHT.
241 * @param cls the closure (NULL)
242 * @param key the public key of the authority (ours)
243 * @param expiration lifetime of the namestore entry
244 * @param name the name of the records
245 * @param rd_count the number of records in data
246 * @param rd the record data
247 * @param signature the signature for the record data
250 put_gns_record(void *cls,
251 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
252 struct GNUNET_TIME_Absolute expiration,
254 unsigned int rd_count,
255 const struct GNUNET_NAMESTORE_RecordData *rd,
256 const struct GNUNET_CRYPTO_RsaSignature *signature)
259 struct GNSNameRecordBlock *nrb;
260 struct GNUNET_CRYPTO_ShortHashCode name_hash;
261 struct GNUNET_CRYPTO_ShortHashCode zhash;
262 GNUNET_HashCode xor_hash;
263 GNUNET_HashCode name_hash_double;
264 GNUNET_HashCode zone_hash_double;
265 uint32_t rd_payload_length;
266 char* nrb_data = NULL;
272 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
273 "Zone iteration finished. Rescheduling put in %ds\n",
274 dht_max_update_interval);
275 zone_update_taskid = GNUNET_SCHEDULER_add_delayed (
276 GNUNET_TIME_relative_multiply(
277 GNUNET_TIME_UNIT_SECONDS,
278 dht_max_update_interval
280 &update_zone_dht_start,
285 namelen = strlen(name) + 1;
287 if (signature == NULL)
289 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
290 "No signature for %s record data provided! Skipping...\n",
292 zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_next,
298 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
299 "Putting records for %s into the DHT\n", name);
301 rd_payload_length = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
303 nrb = GNUNET_malloc(rd_payload_length + namelen
304 + sizeof(struct GNSNameRecordBlock));
306 nrb->signature = *signature;
308 nrb->public_key = *key;
310 nrb->rd_count = htonl(rd_count);
312 memcpy(&nrb[1], name, namelen);
314 nrb_data = (char*)&nrb[1];
317 rd_payload_length += sizeof(struct GNSNameRecordBlock) + namelen;
319 if (-1 == GNUNET_NAMESTORE_records_serialize (rd_count,
324 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
325 "Record serialization failed! Skipping...\n");
327 zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_next,
334 * calculate DHT key: H(name) xor H(pubkey)
336 GNUNET_CRYPTO_short_hash(key,
337 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
339 GNUNET_CRYPTO_short_hash(name, strlen(name), &name_hash);
340 GNUNET_CRYPTO_short_hash_double (&name_hash, &name_hash_double);
341 GNUNET_CRYPTO_short_hash_double (&zhash, &zone_hash_double);
342 GNUNET_CRYPTO_hash_xor(&zone_hash_double, &name_hash_double, &xor_hash);
344 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
345 "zone identity: %s\n", GNUNET_h2s (&zone_hash_double));
347 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
348 "putting records for %s under key: %s with size %d\n",
349 name, GNUNET_h2s (&xor_hash), rd_payload_length);
351 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
352 "DHT req to %d\n", DHT_OPERATION_TIMEOUT.rel_value);
353 /* FIXME: keep return value to possibly cancel? */
354 GNUNET_DHT_put (dht_handle, &xor_hash,
355 DHT_GNS_REPLICATION_LEVEL,
357 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
361 DHT_OPERATION_TIMEOUT,
363 NULL); //cls for cont
365 num_public_records++;
368 * Reschedule periodic put
370 zone_update_taskid = GNUNET_SCHEDULER_add_delayed (record_put_interval,
371 &update_zone_dht_next,
379 * Periodically iterate over our zone and store everything in dht
382 * @param tc task context
385 update_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
387 unsigned long long interval = 0;
389 zone_update_taskid = GNUNET_SCHEDULER_NO_TASK;
391 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Scheduling DHT zone update!\n");
392 if (0 == num_public_records)
395 * If no records are known (startup) or none present
396 * we can safely set the interval to 1s
398 record_put_interval = GNUNET_TIME_relative_multiply(
399 GNUNET_TIME_UNIT_SECONDS,
401 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
402 "No records in db. Adjusted record put interval to 1s\n");
406 interval = max_record_put_interval/num_public_records;
409 record_put_interval = GNUNET_TIME_relative_multiply(
410 GNUNET_TIME_UNIT_SECONDS,
412 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
413 "Adjusted DHT update interval to %ds!\n",
417 /* start counting again */
418 num_public_records = 0;
419 namestore_iter = GNUNET_NAMESTORE_zone_iteration_start (namestore_handle,
421 GNUNET_NAMESTORE_RF_AUTHORITY,
422 GNUNET_NAMESTORE_RF_PRIVATE,
428 * Lookup the shorten key for the zone
430 * @param zone the zone we want a private key for
431 * @return NULL of not found else the key
433 struct GNUNET_CRYPTO_RsaPrivateKey*
434 lookup_shorten_key(struct GNUNET_CRYPTO_ShortHashCode *short_zone)
437 struct GNUNET_CRYPTO_ShortHashAsciiEncoded zonename;
439 struct GNUNET_CRYPTO_RsaPrivateKey *key = NULL;
441 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
442 "Looking for shorten zonekey\n");
444 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (GNS_cfg,
446 "ZONEFILE_DIRECTORY", &keydir))
448 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
449 "No zonefile directory!\n");
453 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
454 "Zonefile directory is %s\n", keydir);
456 GNUNET_CRYPTO_short_hash_to_enc (short_zone, &zonename);
458 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
459 "Zonefile for shorten is %s.zkey\n", &zonename);
461 GNUNET_asprintf(&location, "%s%s%s.zkey", keydir,
462 DIR_SEPARATOR_STR, &zonename);
464 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
465 "Checking for %s\n", location);
467 if (GNUNET_YES == GNUNET_DISK_file_test (location))
468 key = GNUNET_CRYPTO_rsa_key_create_from_file (location);
470 GNUNET_free(location);
477 /* END DHT ZONE PROPAGATION */
480 * Send shorten response back to client
482 * @param cls the closure containing a client shorten handle
483 * @param name the shortened name result or NULL if cannot be shortened
486 send_shorten_response(void* cls, const char* name)
488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message with %s\n",
489 "SHORTEN_RESULT", name);
490 struct GNUNET_GNS_ClientShortenResultMessage *rmsg;
491 struct ClientShortenHandle *csh = (struct ClientShortenHandle *)cls;
498 rmsg = GNUNET_malloc(sizeof(struct GNUNET_GNS_ClientShortenResultMessage)
501 rmsg->id = csh->unique_id;
502 rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_SHORTEN_RESULT);
504 htons(sizeof(struct GNUNET_GNS_ClientShortenResultMessage) +
507 strcpy((char*)&rmsg[1], name);
509 GNUNET_SERVER_notification_context_unicast (nc, csh->client,
510 (const struct GNUNET_MessageHeader *) rmsg,
512 GNUNET_SERVER_receive_done (csh->client, GNUNET_OK);
515 GNUNET_free_non_null(csh->name);
516 GNUNET_free_non_null(csh->shorten_key);
522 * Handle a shorten message from the api
524 * @param cls the closure
525 * @param client the client
526 * @param message the message
528 static void handle_shorten(void *cls,
529 struct GNUNET_SERVER_Client * client,
530 const struct GNUNET_MessageHeader * message)
532 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "SHORTEN");
535 struct ClientShortenHandle *csh;
536 char name[MAX_DNS_NAME_LENGTH];
537 char* nameptr = name;
538 struct GNUNET_CRYPTO_ShortHashCode zone;
539 struct GNUNET_CRYPTO_RsaPrivateKey *key;
541 if (ntohs (message->size) < sizeof (struct GNUNET_GNS_ClientShortenMessage))
544 GNUNET_SERVER_receive_done (client, GNUNET_OK);
549 struct GNUNET_GNS_ClientShortenMessage *sh_msg =
550 (struct GNUNET_GNS_ClientShortenMessage *) message;
552 msg_size = ntohs(message->size);
554 if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
557 GNUNET_SERVER_receive_done (client, GNUNET_OK);
561 csh = GNUNET_malloc(sizeof(struct ClientShortenHandle));
562 csh->client = client;
563 csh->unique_id = sh_msg->id;
564 csh->shorten_key = NULL;
566 GNUNET_STRINGS_utf8_tolower((char*)&sh_msg[1], &nameptr);
568 if (strlen (name) < strlen(GNUNET_GNS_TLD)) {
569 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
570 "SHORTEN: %s is too short", name);
572 send_shorten_response(csh, name);
576 if (strlen (name) > MAX_DNS_NAME_LENGTH) {
577 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
578 "SHORTEN: %s is too long", name);
580 send_shorten_response(csh, name);
584 if (!is_gnunet_tld(name) && !is_zkey_tld(name))
586 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
587 "%s is not our domain. Returning\n", name);
589 send_shorten_response(csh, name);
593 GNUNET_SERVER_notification_context_add (nc, client);
595 if (1 == ntohl(sh_msg->use_default_zone))
596 zone = zone_hash; //Default zone
600 /* Start shortening */
601 if (GNUNET_YES == auto_import_pkey)
603 if (0 == ntohl(sh_msg->use_shorten_zone))
607 key = lookup_shorten_key(&sh_msg->shorten_zone);
608 csh->shorten_key = key;
610 gns_resolver_shorten_name(zone, zone, name, key,
611 &send_shorten_response, csh);
614 gns_resolver_shorten_name(zone, zone, name, NULL,
615 &send_shorten_response, csh);
620 * Send get authority response back to client
622 * @param cls the closure containing a client get auth handle
623 * @param name the shortened name result or NULL if cannot be shortened
626 send_get_auth_response(void *cls, const char* name)
628 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message with %s\n",
629 "GET_AUTH_RESULT", name);
630 struct GNUNET_GNS_ClientGetAuthResultMessage *rmsg;
631 struct ClientGetAuthHandle *cah = (struct ClientGetAuthHandle *)cls;
638 rmsg = GNUNET_malloc(sizeof(struct GNUNET_GNS_ClientGetAuthResultMessage)
641 rmsg->id = cah->unique_id;
642 rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_GET_AUTH_RESULT);
644 htons(sizeof(struct GNUNET_GNS_ClientGetAuthResultMessage) +
647 strcpy((char*)&rmsg[1], name);
649 GNUNET_SERVER_notification_context_unicast (nc, cah->client,
650 (const struct GNUNET_MessageHeader *) rmsg,
652 GNUNET_SERVER_receive_done (cah->client, GNUNET_OK);
654 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Cleaning up handles...\n");
657 GNUNET_free_non_null(cah->name);
660 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "done.\n");
666 * Handle a get authority message from the api
668 * @param cls the closure
669 * @param client the client
670 * @param message the message
672 static void handle_get_authority(void *cls,
673 struct GNUNET_SERVER_Client * client,
674 const struct GNUNET_MessageHeader * message)
676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "GET_AUTH");
679 struct ClientGetAuthHandle *cah;
680 char name[MAX_DNS_NAME_LENGTH];
681 char* nameptr = name;
684 if (ntohs (message->size) < sizeof (struct GNUNET_GNS_ClientGetAuthMessage))
687 GNUNET_SERVER_receive_done (client, GNUNET_OK);
691 GNUNET_SERVER_notification_context_add (nc, client);
693 struct GNUNET_GNS_ClientGetAuthMessage *sh_msg =
694 (struct GNUNET_GNS_ClientGetAuthMessage *) message;
696 msg_size = ntohs(message->size);
698 if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
701 GNUNET_SERVER_receive_done (client, GNUNET_OK);
705 GNUNET_STRINGS_utf8_tolower((char*)&sh_msg[1], &nameptr);
708 cah = GNUNET_malloc(sizeof(struct ClientGetAuthHandle));
709 cah->client = client;
710 cah->unique_id = sh_msg->id;
712 if (strlen(name) < strlen(GNUNET_GNS_TLD))
714 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
715 "GET_AUTH: %s is too short. Returning\n", name);
717 send_get_auth_response(cah, name);
721 if (strlen (name) > MAX_DNS_NAME_LENGTH) {
722 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
723 "GET_AUTH: %s is too long", name);
725 send_get_auth_response(cah, name);
729 if (strcmp(name+strlen(name)-strlen(GNUNET_GNS_TLD),
730 GNUNET_GNS_TLD) != 0)
732 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
733 "GET_AUTH: %s is not our domain. Returning\n", name);
735 send_get_auth_response(cah, name);
739 if (strcmp(name, GNUNET_GNS_TLD) == 0)
741 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
742 "GET_AUTH: %s is us. Returning\n", name);
744 send_get_auth_response(cah, name);
748 cah->name = GNUNET_malloc(strlen(name)
749 - strlen(GNUNET_GNS_TLD) + 1);
751 strlen(name)-strlen(GNUNET_GNS_TLD) + 1);
752 memcpy(cah->name, name,
753 strlen(name)-strlen(GNUNET_GNS_TLD));
755 /* Start delegation resolution in our namestore */
756 gns_resolver_get_authority(zone_hash, zone_hash, name, &send_get_auth_response, cah);
762 * Reply to client with the result from our lookup.
764 * @param cls the closure (our client lookup handle)
765 * @param rd_count the number of records
766 * @param rd the record data
769 send_lookup_response(void* cls,
771 const struct GNUNET_NAMESTORE_RecordData *rd)
773 struct ClientLookupHandle* clh = (struct ClientLookupHandle*)cls;
774 struct GNUNET_GNS_ClientLookupResultMessage *rmsg;
777 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message with %d results\n",
778 "LOOKUP_RESULT", rd_count);
780 len = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
781 rmsg = GNUNET_malloc(len+sizeof(struct GNUNET_GNS_ClientLookupResultMessage));
783 rmsg->id = clh->unique_id;
784 rmsg->rd_count = htonl(rd_count);
785 rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT);
787 htons(len+sizeof(struct GNUNET_GNS_ClientLookupResultMessage));
789 GNUNET_NAMESTORE_records_serialize (rd_count, rd, len, (char*)&rmsg[1]);
791 GNUNET_SERVER_notification_context_unicast (nc, clh->client,
792 (const struct GNUNET_MessageHeader *) rmsg,
794 GNUNET_SERVER_receive_done (clh->client, GNUNET_OK);
797 GNUNET_free(clh->name);
799 if (NULL != clh->shorten_key)
800 GNUNET_free(clh->shorten_key);
808 * Handle lookup requests from client
810 * @param cls the closure
811 * @param client the client
812 * @param message the message
815 handle_lookup(void *cls,
816 struct GNUNET_SERVER_Client * client,
817 const struct GNUNET_MessageHeader * message)
819 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "LOOKUP");
823 char name[MAX_DNS_NAME_LENGTH];
824 struct ClientLookupHandle *clh;
825 char* nameptr = name;
826 struct GNUNET_CRYPTO_RsaPrivateKey *key = NULL;
827 struct GNUNET_CRYPTO_ShortHashCode zone;
829 if (ntohs (message->size) < sizeof (struct GNUNET_GNS_ClientLookupMessage))
832 GNUNET_SERVER_receive_done (client, GNUNET_OK);
836 GNUNET_SERVER_notification_context_add (nc, client);
838 struct GNUNET_GNS_ClientLookupMessage *sh_msg =
839 (struct GNUNET_GNS_ClientLookupMessage *) message;
841 msg_size = ntohs(message->size);
843 if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
846 GNUNET_SERVER_receive_done (client, GNUNET_OK);
850 GNUNET_STRINGS_utf8_tolower((char*)&sh_msg[1], &nameptr);
851 namelen = strlen(name)+1;
852 clh = GNUNET_malloc(sizeof(struct ClientLookupHandle));
853 clh->client = client;
854 clh->name = GNUNET_malloc(namelen);
855 strcpy(clh->name, name);
856 clh->unique_id = sh_msg->id;
857 clh->type = ntohl(sh_msg->type);
858 clh->shorten_key = NULL;
860 if (strlen (name) > MAX_DNS_NAME_LENGTH) {
861 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
862 "LOOKUP: %s is too long", name);
864 send_lookup_response(clh, 0, NULL);
868 if (1 == ntohl(sh_msg->use_default_zone))
869 zone = zone_hash; //Default zone
873 if (GNUNET_YES == auto_import_pkey)
875 if (1 == ntohl(sh_msg->use_shorten_zone))
879 key = lookup_shorten_key(&sh_msg->shorten_zone);
880 clh->shorten_key = key;
883 gns_resolver_lookup_record(zone, zone, clh->type, name,
885 default_lookup_timeout,
886 &send_lookup_response, clh);
890 gns_resolver_lookup_record(zone, zone, clh->type, name,
892 default_lookup_timeout,
893 &send_lookup_response, clh);
900 * Process GNS requests.
902 * @param cls closure)
903 * @param server the initialized server
904 * @param c configuration to use
907 run (void *cls, struct GNUNET_SERVER_Handle *server,
908 const struct GNUNET_CONFIGURATION_Handle *c)
911 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Initializing GNS\n");
914 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
915 unsigned long long max_parallel_bg_queries = 0;
916 unsigned long long default_lookup_timeout_secs = 0;
917 int ignore_pending = GNUNET_NO;
919 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
920 {&handle_shorten, NULL, GNUNET_MESSAGE_TYPE_GNS_SHORTEN, 0},
921 {&handle_lookup, NULL, GNUNET_MESSAGE_TYPE_GNS_LOOKUP, 0},
922 {&handle_get_authority, NULL, GNUNET_MESSAGE_TYPE_GNS_GET_AUTH, 0}
927 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (c, "gns",
928 "ZONEKEY", &keyfile))
930 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
931 "No private key for root zone specified!\n");
932 GNUNET_SCHEDULER_shutdown ();
936 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
937 "Using keyfile %s for root zone.\n", keyfile);
939 zone_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
940 GNUNET_CRYPTO_rsa_key_get_public (zone_key, &pkey);
942 GNUNET_CRYPTO_short_hash(&pkey,
943 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
945 GNUNET_free(keyfile);
948 * handle to our local namestore
950 namestore_handle = GNUNET_NAMESTORE_connect(c);
952 if (NULL == namestore_handle)
954 //FIXME do error handling;
955 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
956 "Failed to connect to the namestore!\n");
957 GNUNET_SCHEDULER_shutdown ();
963 auto_import_pkey = GNUNET_NO;
966 GNUNET_CONFIGURATION_get_value_yesno (c, "gns",
969 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
970 "Automatic PKEY import is enabled.\n");
971 auto_import_pkey = GNUNET_YES;
975 dht_max_update_interval = GNUNET_GNS_DHT_MAX_UPDATE_INTERVAL;
978 GNUNET_CONFIGURATION_get_value_number (c, "gns",
980 &dht_max_update_interval))
982 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
983 "DHT zone update interval: %d\n",
984 dht_max_update_interval);
987 max_record_put_interval = 1;
990 GNUNET_CONFIGURATION_get_value_number (c, "gns",
991 "RECORD_PUT_INTERVAL",
992 &max_record_put_interval))
994 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
995 "Record put interval: %d\n",
996 max_record_put_interval);
1000 GNUNET_CONFIGURATION_get_value_number (c, "gns",
1001 "MAX_PARALLEL_BACKGROUND_QUERIES",
1002 &max_parallel_bg_queries))
1004 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1005 "Number of allowed parallel background queries: %d\n",
1006 max_parallel_bg_queries);
1010 GNUNET_CONFIGURATION_get_value_yesno (c, "gns",
1011 "AUTO_IMPORT_CONFIRMATION_REQ"))
1013 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1014 "Auto import requires user confirmation\n");
1015 ignore_pending = GNUNET_YES;
1019 GNUNET_CONFIGURATION_get_value_number(c, "gns",
1020 "DEFAULT_LOOKUP_TIMEOUT",
1021 &default_lookup_timeout_secs))
1023 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1024 "Default lookup timeout: %ds\n", default_lookup_timeout_secs);
1025 default_lookup_timeout = GNUNET_TIME_relative_multiply(
1026 GNUNET_TIME_UNIT_SECONDS,
1027 default_lookup_timeout_secs);
1033 dht_handle = GNUNET_DHT_connect(c,
1034 //max_parallel_bg_queries); //FIXME get ht_len from cfg
1037 if (NULL == dht_handle)
1039 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not connect to DHT!\n");
1042 if (gns_resolver_init(namestore_handle, dht_handle, zone_hash,
1043 max_parallel_bg_queries,
1047 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1048 "Unable to initialize resolver!\n");
1049 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
1054 GNUNET_CONFIGURATION_get_value_yesno (c, "gns", "HIJACK_DNS"))
1056 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1057 "DNS hijacking enabled... connecting to service.\n");
1059 if (gns_interceptor_init(zone_hash, zone_key, c) == GNUNET_SYSERR)
1061 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1062 "Failed to enable the dns interceptor!\n");
1067 * Schedule periodic put
1069 * We have roughly an hour for all records;
1071 record_put_interval = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS,
1073 zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_start, NULL);
1075 GNUNET_SERVER_add_handlers (server, handlers);
1078 //GNUNET_SERVER_disconnect_notify (server,
1079 // &client_disconnect_notification,
1082 nc = GNUNET_SERVER_notification_context_create (server, 1);
1084 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
1090 * The main function for the GNS service.
1092 * @param argc number of arguments from the command line
1093 * @param argv command line arguments
1094 * @return 0 ok, 1 on error
1097 main (int argc, char *const *argv)
1103 GNUNET_SERVICE_run (argc, argv, "gns", GNUNET_SERVICE_OPTION_NONE, &run,
1108 /* end of gnunet-service-gns.c */