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 "gnunet_statistics_service.h"
36 #include "block_gns.h"
38 #include "gnunet-service-gns_resolver.h"
39 #include "gnunet-service-gns_interceptor.h"
42 /* FIXME move to proper header in include */
43 #define GNUNET_MESSAGE_TYPE_GNS_LOOKUP 23
44 #define GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT 24
45 #define GNUNET_MESSAGE_TYPE_GNS_SHORTEN 25
46 #define GNUNET_MESSAGE_TYPE_GNS_SHORTEN_RESULT 26
47 #define GNUNET_MESSAGE_TYPE_GNS_GET_AUTH 27
48 #define GNUNET_MESSAGE_TYPE_GNS_GET_AUTH_RESULT 28
51 #define INITIAL_ZONE_ITERATION_INTERVAL GNUNET_TIME_UNIT_MILLISECONDS
52 #define MINIMUM_ZONE_ITERATION_INTERVAL GNUNET_TIME_UNIT_SECONDS
53 #define DEFAULT_RECORD_PUT_INTERVAL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 4)
54 #define LATE_ITERATION_SPEEDUP_FACTOR 2
57 * Handle to a shorten operation from api
59 struct ClientShortenHandle
63 struct ClientShortenHandle *next;
66 struct ClientShortenHandle *prev;
68 /* the requesting client that */
69 struct GNUNET_SERVER_Client *client;
75 enum GNUNET_GNS_RecordType type;
78 char name[MAX_DNS_NAME_LENGTH];
80 /* name of private zone (relative to root) */
81 char private_zone_id[MAX_DNS_NAME_LENGTH];
83 /* name of shorten zone (relative to root) */
84 char shorten_zone_id[MAX_DNS_NAME_LENGTH];
87 struct GNUNET_CRYPTO_ShortHashCode root_zone;
90 struct GNUNET_CRYPTO_ShortHashCode private_zone;
93 struct GNUNET_CRYPTO_ShortHashCode shorten_zone;
95 /* Namestore lookup task */
96 struct GNUNET_NAMESTORE_QueueEntry *namestore_task;
102 * Handle to a get auhtority operation from api
104 struct ClientGetAuthHandle
106 /* the requesting client that */
107 struct GNUNET_SERVER_Client *client;
112 /* name to lookup authority */
119 * Handle to a lookup operation from api
121 struct ClientLookupHandle
124 /* the requesting client that */
125 struct GNUNET_SERVER_Client *client;
127 /* The zone we look up in */
128 struct GNUNET_CRYPTO_ShortHashCode zone;
130 /* Do we only want to lookup from local cache? */
137 enum GNUNET_GNS_RecordType type;
139 /* optional zone private key used for shorten */
140 struct GNUNET_CRYPTO_RsaPrivateKey *shorten_key;
142 /* the name to look up */
143 char* name; //Needed?
147 * Our handle to the DHT
149 static struct GNUNET_DHT_Handle *dht_handle;
152 * Our zone's private key
154 struct GNUNET_CRYPTO_RsaPrivateKey *zone_key;
157 * Our handle to the namestore service
158 * FIXME maybe need a second handle for iteration
160 struct GNUNET_NAMESTORE_Handle *namestore_handle;
163 * Handle to iterate over our authoritative zone in namestore
165 struct GNUNET_NAMESTORE_ZoneIterator *namestore_iter;
168 * The configuration the GNS service is running with
170 const struct GNUNET_CONFIGURATION_Handle *GNS_cfg;
173 * Our notification context.
175 static struct GNUNET_SERVER_NotificationContext *nc;
180 struct GNUNET_CRYPTO_ShortHashCode zone_hash;
183 * Useful for zone update for DHT put
185 static unsigned long long num_public_records;
188 * Last seen record count
190 static unsigned long long last_num_public_records;
192 static struct GNUNET_TIME_Relative zone_iteration_interval;
194 /* dht update interval FIXME define? */
195 static struct GNUNET_TIME_Relative record_put_interval;
197 /* zone update task */
198 GNUNET_SCHEDULER_TaskIdentifier zone_update_taskid = GNUNET_SCHEDULER_NO_TASK;
200 /* automatic pkey import for name shortening */
201 static int auto_import_pkey;
203 /* first zone iteration is specia */
204 static int first_zone_iteration;
207 static struct GNUNET_TIME_Relative default_lookup_timeout;
210 static int v6_enabled;
213 static int v4_enabled;
215 /* Shorten DLL for cancelling NS requests */
216 static struct ClientShortenHandle *csh_head;
218 /* Shorten DLL for cancelling NS requests */
219 static struct ClientShortenHandle *csh_tail;
221 /* Statistics handle */
222 static struct GNUNET_STATISTICS_Handle *statistics;
225 * Send shorten response back to client
227 * @param cls the closure containing a client shorten handle
228 * @param name the shortened name result or NULL if cannot be shortened
231 send_shorten_response(void* cls, const char* name);
238 on_resolver_cleanup(void)
240 if (NULL != statistics)
241 GNUNET_STATISTICS_destroy (statistics, GNUNET_NO);
243 if (NULL != namestore_iter)
244 GNUNET_NAMESTORE_zone_iteration_stop (namestore_iter);
245 GNUNET_NAMESTORE_disconnect(namestore_handle);
246 GNUNET_DHT_disconnect(dht_handle);
250 * Task run during shutdown.
256 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
258 struct ClientShortenHandle *csh_tmp = csh_head;
260 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
263 while (csh_tmp != NULL)
265 GNUNET_CONTAINER_DLL_remove (csh_head, csh_tail, csh_tmp);
266 send_shorten_response (csh_tmp, csh_tmp->name);
269 GNUNET_SERVER_notification_context_destroy (nc);
271 gns_interceptor_stop();
272 gns_resolver_cleanup(&on_resolver_cleanup);
273 /* Kill zone task for it may make the scheduler hang */
274 if (zone_update_taskid != GNUNET_SCHEDULER_NO_TASK)
275 GNUNET_SCHEDULER_cancel(zone_update_taskid);
280 * Method called periodicattluy that triggers
281 * iteration over root zone
284 * @param tc task context
287 update_zone_dht_next(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
289 zone_update_taskid = GNUNET_SCHEDULER_NO_TASK;
290 GNUNET_NAMESTORE_zone_iterator_next (namestore_iter);
294 * Continuation for DHT put
297 * @param success GNUNET_OK if the PUT was transmitted,
298 * GNUNET_NO on timeout,
299 * GNUNET_SYSERR on disconnect from service
300 * after the PUT message was transmitted
301 * (so we don't know if it was received or not)
304 record_dht_put(void *cls, int success)
306 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "put request transmitted\n");
311 update_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
314 * Function used to put all records successively into the DHT.
316 * @param cls the closure (NULL)
317 * @param key the public key of the authority (ours)
318 * @param expiration lifetime of the namestore entry
319 * @param name the name of the records
320 * @param rd_count the number of records in data
321 * @param rd the record data
322 * @param signature the signature for the record data
325 put_gns_record(void *cls,
326 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
327 struct GNUNET_TIME_Absolute expiration,
329 unsigned int rd_count,
330 const struct GNUNET_NAMESTORE_RecordData *rd,
331 const struct GNUNET_CRYPTO_RsaSignature *signature)
334 struct GNSNameRecordBlock *nrb;
335 struct GNUNET_CRYPTO_ShortHashCode name_hash;
336 struct GNUNET_CRYPTO_ShortHashCode zhash;
337 struct GNUNET_HashCode xor_hash;
338 struct GNUNET_HashCode name_hash_double;
339 struct GNUNET_HashCode zone_hash_double;
340 uint32_t rd_payload_length;
341 char* nrb_data = NULL;
343 struct GNUNET_TIME_Relative next_put_interval;
350 first_zone_iteration = GNUNET_NO;
351 if (0 == num_public_records)
354 * If no records are known (startup) or none present
355 * we can safely set the interval to the value for a single
358 zone_iteration_interval = GNUNET_TIME_relative_divide (record_put_interval,
361 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
362 "No records in db.\n");
366 zone_iteration_interval = GNUNET_TIME_relative_divide (record_put_interval,
370 zone_iteration_interval = GNUNET_TIME_relative_max (MINIMUM_ZONE_ITERATION_INTERVAL,
371 zone_iteration_interval);
373 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
374 "Adjusted zone iteration interval to %llus!\n",
375 zone_iteration_interval.rel_value);
376 GNUNET_STATISTICS_set (statistics,
377 "Current zone iteration interval [msec]",
378 zone_iteration_interval.rel_value,
381 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
382 "Zone iteration finished. Rescheduling zone iteration\n");
384 namestore_iter = NULL;
385 zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_start, NULL);
386 GNUNET_STATISTICS_update (statistics,
387 "Number of zone iterations", 1, GNUNET_NO);
389 last_num_public_records = num_public_records;
390 GNUNET_STATISTICS_set (statistics,
391 "Number of public records in DHT",
392 last_num_public_records,
397 namelen = strlen(name) + 1;
401 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
402 "No records given for name %s! Skipping...\n",
404 zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_next,
409 if (signature == NULL)
411 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
412 "No signature for %s record data provided! Skipping...\n",
414 zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_next,
420 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
421 "Putting records for %s into the DHT\n", name);
423 rd_payload_length = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
425 nrb = GNUNET_malloc(rd_payload_length + namelen
426 + sizeof(struct GNSNameRecordBlock));
428 nrb->signature = *signature;
430 nrb->public_key = *key;
432 nrb->rd_count = htonl(rd_count);
434 memcpy(&nrb[1], name, namelen);
436 nrb_data = (char*)&nrb[1];
439 rd_payload_length += sizeof(struct GNSNameRecordBlock) + namelen;
441 if (-1 == GNUNET_NAMESTORE_records_serialize (rd_count,
446 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
447 "Record serialization failed! Skipping...\n");
449 zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_next,
456 * calculate DHT key: H(name) xor H(pubkey)
458 GNUNET_CRYPTO_short_hash(key,
459 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
461 GNUNET_CRYPTO_short_hash(name, strlen(name), &name_hash);
462 GNUNET_CRYPTO_short_hash_double (&name_hash, &name_hash_double);
463 GNUNET_CRYPTO_short_hash_double (&zhash, &zone_hash_double);
464 GNUNET_CRYPTO_hash_xor(&zone_hash_double, &name_hash_double, &xor_hash);
466 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
467 "zone identity: %s\n", GNUNET_h2s (&zone_hash_double));
469 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
470 "putting %d records for %s under key: %s with size %d\n",
471 rd_count, name, GNUNET_h2s (&xor_hash), rd_payload_length);
473 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
474 "DHT req to %d\n", DHT_OPERATION_TIMEOUT.rel_value);
476 GNUNET_STATISTICS_update (statistics,
477 "Record bytes put into DHT", rd_payload_length, GNUNET_NO);
479 /* FIXME: keep return value to possibly cancel? */
480 GNUNET_DHT_put (dht_handle, &xor_hash,
481 DHT_GNS_REPLICATION_LEVEL,
483 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
487 DHT_OPERATION_TIMEOUT,
489 NULL); //cls for cont
491 num_public_records++;
493 if ((num_public_records > last_num_public_records)
494 && (first_zone_iteration == GNUNET_NO))
496 zone_iteration_interval = GNUNET_TIME_relative_divide (record_put_interval,
498 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
499 "Last record count was lower than current record count... increasing.\n");
500 next_put_interval = GNUNET_TIME_relative_divide (zone_iteration_interval,
501 LATE_ITERATION_SPEEDUP_FACTOR);
505 next_put_interval = zone_iteration_interval;
507 GNUNET_STATISTICS_set (statistics,
508 "Current zone iteration interval [msec]",
509 next_put_interval.rel_value,
513 * Reschedule periodic put
515 zone_update_taskid = GNUNET_SCHEDULER_add_delayed (next_put_interval,
516 &update_zone_dht_next,
524 * Periodically iterate over our zone and store everything in dht
527 * @param tc task context
530 update_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
532 zone_update_taskid = GNUNET_SCHEDULER_NO_TASK;
534 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Scheduling DHT zone update!\n");
536 /* start counting again */
537 num_public_records = 0;
538 namestore_iter = GNUNET_NAMESTORE_zone_iteration_start (namestore_handle,
540 GNUNET_NAMESTORE_RF_AUTHORITY,
541 GNUNET_NAMESTORE_RF_PRIVATE,
546 /* END DHT ZONE PROPAGATION */
549 * Send shorten response back to client
551 * @param cls the closure containing a client shorten handle
552 * @param name the shortened name result or NULL if cannot be shortened
555 send_shorten_response(void* cls, const char* name)
557 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message with %s\n",
558 "SHORTEN_RESULT", name);
559 struct GNUNET_GNS_ClientShortenResultMessage *rmsg;
560 struct ClientShortenHandle *csh = (struct ClientShortenHandle *)cls;
567 GNUNET_STATISTICS_update (statistics,
568 "Name shorten results", 1, GNUNET_NO);
570 rmsg = GNUNET_malloc(sizeof(struct GNUNET_GNS_ClientShortenResultMessage)
573 rmsg->id = csh->unique_id;
574 rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_SHORTEN_RESULT);
576 htons(sizeof(struct GNUNET_GNS_ClientShortenResultMessage) +
579 strcpy((char*)&rmsg[1], name);
581 GNUNET_SERVER_notification_context_unicast (nc, csh->client,
582 (const struct GNUNET_MessageHeader *) rmsg,
584 GNUNET_SERVER_receive_done (csh->client, GNUNET_OK);
586 if (NULL != csh->namestore_task)
587 GNUNET_NAMESTORE_cancel (csh->namestore_task);
596 * Lookup the zone infos and shorten name
598 * @param cls the client shorten handle
599 * @param key key of the zone
600 * @param expiration expiration of record
601 * @param name name found or null if no result
602 * @param rd_count number of records found
603 * @param rd record data
608 process_shorten_in_private_zone_lookup (void *cls,
609 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
610 struct GNUNET_TIME_Absolute expiration,
612 unsigned int rd_count,
613 const struct GNUNET_NAMESTORE_RecordData *rd,
614 const struct GNUNET_CRYPTO_RsaSignature *signature)
616 struct ClientShortenHandle *csh = cls;
617 csh->namestore_task = NULL;
618 struct GNUNET_CRYPTO_ShortHashCode *szone = &csh->shorten_zone;
619 struct GNUNET_CRYPTO_ShortHashCode *pzone = &csh->private_zone;
621 if (0 == strcmp (csh->private_zone_id, ""))
626 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
627 "No shorten zone in private zone!\n");
629 strcpy (csh->shorten_zone_id, "");
636 GNUNET_assert (rd_count == 1);
638 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
639 "Shorten zone %s found in private zone %s\n",
640 name, csh->private_zone_id);
642 sprintf (csh->shorten_zone_id, "%s.%s", name, csh->private_zone_id);
645 GNUNET_CONTAINER_DLL_remove (csh_head, csh_tail, csh);
647 gns_resolver_shorten_name (&csh->root_zone,
651 csh->private_zone_id,
652 csh->shorten_zone_id,
653 &send_shorten_response, csh);
659 * Lookup the zone infos and shorten name
661 * @param cls the shorten handle
662 * @param key key of the zone
663 * @param expiration expiration of record
664 * @param name name found or null if no result
665 * @param rd_count number of records found
666 * @param rd record data
671 process_shorten_in_root_zone_lookup (void *cls,
672 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
673 struct GNUNET_TIME_Absolute expiration,
675 unsigned int rd_count,
676 const struct GNUNET_NAMESTORE_RecordData *rd,
677 const struct GNUNET_CRYPTO_RsaSignature *signature)
679 struct ClientShortenHandle *csh = cls;
680 csh->namestore_task = NULL;
681 struct GNUNET_CRYPTO_ShortHashCode *szone = &csh->shorten_zone;
682 struct GNUNET_CRYPTO_ShortHashCode *pzone = &csh->private_zone;
684 if (0 == strcmp (csh->private_zone_id, ""))
689 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
690 "No shorten zone in zone and no private zone!\n");
692 strcpy (csh->shorten_zone_id, "");
694 GNUNET_CONTAINER_DLL_remove (csh_head, csh_tail, csh);
697 gns_resolver_shorten_name (&csh->root_zone,
701 csh->private_zone_id,
702 csh->shorten_zone_id,
703 &send_shorten_response, csh);
707 GNUNET_assert (rd_count == 1);
709 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
710 "Private zone %s found in root zone\n", name);
712 strcpy (csh->private_zone_id, name);
714 csh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
717 &process_shorten_in_private_zone_lookup,
723 * Lookup the zone infos and shorten name
725 * @param cls the shorten handle
726 * @param key key of the zone
727 * @param expiration expiration of record
728 * @param name name found or null if no result
729 * @param rd_count number of records found
730 * @param rd record data
735 process_private_in_root_zone_lookup (void *cls,
736 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
737 struct GNUNET_TIME_Absolute expiration,
739 unsigned int rd_count,
740 const struct GNUNET_NAMESTORE_RecordData *rd,
741 const struct GNUNET_CRYPTO_RsaSignature *signature)
743 struct ClientShortenHandle *csh = cls;
744 csh->namestore_task = NULL;
748 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
749 "No private zone in root zone\n");
751 strcpy (csh->private_zone_id, "");
753 csh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
756 &process_shorten_in_root_zone_lookup,
761 GNUNET_assert (rd_count == 1);
763 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
764 "Private zone %s found in root zone\n", name);
766 strcpy (csh->private_zone_id, name);
768 csh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
771 &process_shorten_in_private_zone_lookup,
776 * Lookup the zone infos and shorten name
778 * @param csh the shorten handle
782 start_shorten_name (struct ClientShortenHandle *csh)
784 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
785 "Looking for private zone name in root zone\n");
787 csh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
790 &process_private_in_root_zone_lookup,
796 * Handle a shorten message from the api
798 * @param cls the closure
799 * @param client the client
800 * @param message the message
802 static void handle_shorten (void *cls,
803 struct GNUNET_SERVER_Client * client,
804 const struct GNUNET_MessageHeader * message)
806 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "SHORTEN");
809 struct ClientShortenHandle *csh;
810 char name[MAX_DNS_NAME_LENGTH];
811 char* nameptr = name;
813 if (ntohs (message->size) < sizeof (struct GNUNET_GNS_ClientShortenMessage))
816 GNUNET_SERVER_receive_done (client, GNUNET_OK);
821 struct GNUNET_GNS_ClientShortenMessage *sh_msg =
822 (struct GNUNET_GNS_ClientShortenMessage *) message;
824 msg_size = ntohs(message->size);
826 if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
829 GNUNET_SERVER_receive_done (client, GNUNET_OK);
833 csh = GNUNET_malloc(sizeof(struct ClientShortenHandle));
834 csh->client = client;
835 csh->unique_id = sh_msg->id;
837 GNUNET_CONTAINER_DLL_insert (csh_head, csh_tail, csh);
839 GNUNET_STRINGS_utf8_tolower((char*)&sh_msg[1], &nameptr);
841 if (strlen (name) < strlen(GNUNET_GNS_TLD)) {
842 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
843 "SHORTEN: %s is too short", name);
844 send_shorten_response(csh, name);
848 if (strlen (name) > MAX_DNS_NAME_LENGTH) {
849 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
850 "SHORTEN: %s is too long", name);
851 send_shorten_response(csh, name);
855 if (!is_gnunet_tld(name) && !is_zkey_tld(name))
857 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
858 "%s is not our domain. Returning\n", name);
859 send_shorten_response(csh, name);
863 csh->shorten_zone = sh_msg->shorten_zone;
864 csh->private_zone = sh_msg->private_zone;
866 strcpy (csh->name, name);
868 GNUNET_SERVER_notification_context_add (nc, client);
870 if (1 == ntohl(sh_msg->use_default_zone))
871 csh->root_zone = zone_hash; //Default zone
873 csh->root_zone = sh_msg->zone;
875 start_shorten_name (csh);
877 GNUNET_STATISTICS_update (statistics,
878 "Name shorten attempts", 1, GNUNET_NO);
884 * Send get authority response back to client
886 * @param cls the closure containing a client get auth handle
887 * @param name the shortened name result or NULL if cannot be shortened
890 send_get_auth_response(void *cls, const char* name)
892 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message with %s\n",
893 "GET_AUTH_RESULT", name);
894 struct GNUNET_GNS_ClientGetAuthResultMessage *rmsg;
895 struct ClientGetAuthHandle *cah = (struct ClientGetAuthHandle *)cls;
899 GNUNET_STATISTICS_update (statistics,
900 "Authorities resolved", 1, GNUNET_NO);
908 rmsg = GNUNET_malloc(sizeof(struct GNUNET_GNS_ClientGetAuthResultMessage)
911 rmsg->id = cah->unique_id;
912 rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_GET_AUTH_RESULT);
914 htons(sizeof(struct GNUNET_GNS_ClientGetAuthResultMessage) +
917 strcpy((char*)&rmsg[1], name);
919 GNUNET_SERVER_notification_context_unicast (nc, cah->client,
920 (const struct GNUNET_MessageHeader *) rmsg,
922 GNUNET_SERVER_receive_done (cah->client, GNUNET_OK);
925 GNUNET_free_non_null(cah->name);
934 * Handle a get authority message from the api
936 * @param cls the closure
937 * @param client the client
938 * @param message the message
940 static void handle_get_authority(void *cls,
941 struct GNUNET_SERVER_Client * client,
942 const struct GNUNET_MessageHeader * message)
944 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "GET_AUTH");
947 struct ClientGetAuthHandle *cah;
948 char name[MAX_DNS_NAME_LENGTH];
949 char* nameptr = name;
952 if (ntohs (message->size) < sizeof (struct GNUNET_GNS_ClientGetAuthMessage))
955 GNUNET_SERVER_receive_done (client, GNUNET_OK);
959 GNUNET_SERVER_notification_context_add (nc, client);
961 struct GNUNET_GNS_ClientGetAuthMessage *sh_msg =
962 (struct GNUNET_GNS_ClientGetAuthMessage *) message;
964 msg_size = ntohs(message->size);
966 if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
969 GNUNET_SERVER_receive_done (client, GNUNET_OK);
973 GNUNET_STRINGS_utf8_tolower((char*)&sh_msg[1], &nameptr);
976 cah = GNUNET_malloc(sizeof(struct ClientGetAuthHandle));
977 cah->client = client;
978 cah->unique_id = sh_msg->id;
980 if (strlen(name) < strlen(GNUNET_GNS_TLD))
982 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
983 "GET_AUTH: %s is too short. Returning\n", name);
985 send_get_auth_response(cah, name);
989 if (strlen (name) > MAX_DNS_NAME_LENGTH) {
990 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
991 "GET_AUTH: %s is too long", name);
993 send_get_auth_response(cah, name);
997 if (strcmp(name+strlen(name)-strlen(GNUNET_GNS_TLD),
998 GNUNET_GNS_TLD) != 0)
1000 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1001 "GET_AUTH: %s is not our domain. Returning\n", name);
1003 send_get_auth_response(cah, name);
1007 if (strcmp(name, GNUNET_GNS_TLD) == 0)
1009 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1010 "GET_AUTH: %s is us. Returning\n", name);
1012 send_get_auth_response(cah, name);
1016 cah->name = GNUNET_malloc(strlen(name)
1017 - strlen(GNUNET_GNS_TLD) + 1);
1018 memset(cah->name, 0,
1019 strlen(name)-strlen(GNUNET_GNS_TLD) + 1);
1020 memcpy(cah->name, name,
1021 strlen(name)-strlen(GNUNET_GNS_TLD));
1023 /* Start delegation resolution in our namestore */
1024 gns_resolver_get_authority(zone_hash, zone_hash, name, &send_get_auth_response, cah);
1026 GNUNET_STATISTICS_update (statistics,
1027 "Authority lookup attempts", 1, GNUNET_NO);
1033 * Reply to client with the result from our lookup.
1035 * @param cls the closure (our client lookup handle)
1036 * @param rd_count the number of records
1037 * @param rd the record data
1040 send_lookup_response(void* cls,
1042 const struct GNUNET_NAMESTORE_RecordData *rd)
1044 struct ClientLookupHandle* clh = (struct ClientLookupHandle*)cls;
1045 struct GNUNET_GNS_ClientLookupResultMessage *rmsg;
1048 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message with %d results\n",
1049 "LOOKUP_RESULT", rd_count);
1051 len = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
1052 rmsg = GNUNET_malloc(len+sizeof(struct GNUNET_GNS_ClientLookupResultMessage));
1054 rmsg->id = clh->unique_id;
1055 rmsg->rd_count = htonl(rd_count);
1056 rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT);
1058 htons(len+sizeof(struct GNUNET_GNS_ClientLookupResultMessage));
1060 GNUNET_NAMESTORE_records_serialize (rd_count, rd, len, (char*)&rmsg[1]);
1062 GNUNET_SERVER_notification_context_unicast (nc, clh->client,
1063 (const struct GNUNET_MessageHeader *) rmsg,
1065 GNUNET_SERVER_receive_done (clh->client, GNUNET_OK);
1068 GNUNET_free(clh->name);
1070 if (NULL != clh->shorten_key)
1071 GNUNET_free(clh->shorten_key);
1075 GNUNET_STATISTICS_update (statistics,
1076 "Completed lookups", 1, GNUNET_NO);
1080 GNUNET_STATISTICS_update (statistics,
1081 "Records resolved", rd_count, GNUNET_NO);
1088 * Handle lookup requests from client
1090 * @param cls the closure
1091 * @param client the client
1092 * @param message the message
1095 handle_lookup(void *cls,
1096 struct GNUNET_SERVER_Client * client,
1097 const struct GNUNET_MessageHeader * message)
1099 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "LOOKUP");
1101 size_t msg_size = 0;
1103 char name[MAX_DNS_NAME_LENGTH];
1104 struct ClientLookupHandle *clh;
1105 char* nameptr = name;
1107 struct GNUNET_CRYPTO_RsaPrivateKey *key;
1108 struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded *pkey;
1111 if (ntohs (message->size) < sizeof (struct GNUNET_GNS_ClientLookupMessage))
1113 GNUNET_break_op (0);
1114 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1118 GNUNET_SERVER_notification_context_add (nc, client);
1120 struct GNUNET_GNS_ClientLookupMessage *sh_msg =
1121 (struct GNUNET_GNS_ClientLookupMessage *) message;
1123 msg_size = ntohs(message->size);
1125 if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
1127 GNUNET_break_op (0);
1128 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1132 if (1 == ntohl(sh_msg->have_key))
1134 pkey = (struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded *)&sh_msg[1];
1135 tmp_pkey = (char*)&sh_msg[1];
1136 key = GNUNET_CRYPTO_rsa_decode_key (tmp_pkey, ntohs(pkey->len));
1137 GNUNET_STRINGS_utf8_tolower(&tmp_pkey[ntohs(pkey->len)], &nameptr);
1142 GNUNET_STRINGS_utf8_tolower((char*)&sh_msg[1], &nameptr);
1145 namelen = strlen(name)+1;
1146 clh = GNUNET_malloc (sizeof (struct ClientLookupHandle));
1147 memset (clh, 0, sizeof (struct ClientLookupHandle));
1148 clh->client = client;
1149 clh->name = GNUNET_malloc(namelen);
1150 strcpy(clh->name, name);
1151 clh->unique_id = sh_msg->id;
1152 clh->type = ntohl(sh_msg->type);
1153 clh->shorten_key = key;
1155 only_cached = ntohl(sh_msg->only_cached);
1157 if (strlen (name) > MAX_DNS_NAME_LENGTH) {
1158 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1159 "LOOKUP: %s is too long", name);
1161 send_lookup_response(clh, 0, NULL);
1165 if ((clh->type == GNUNET_GNS_RECORD_A) &&
1166 (GNUNET_OK != v4_enabled))
1168 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1169 "LOOKUP: Query for A record but AF_INET not supported!");
1171 send_lookup_response(clh, 0, NULL);
1175 if ((clh->type == GNUNET_GNS_RECORD_AAAA) &&
1176 (GNUNET_OK != v6_enabled))
1178 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1179 "LOOKUP: Query for AAAA record but AF_INET6 not supported!");
1181 send_lookup_response(clh, 0, NULL);
1185 if (1 == ntohl(sh_msg->use_default_zone))
1186 clh->zone = zone_hash; //Default zone
1188 clh->zone = sh_msg->zone;
1190 if (GNUNET_YES == auto_import_pkey)
1192 gns_resolver_lookup_record (clh->zone, clh->zone, clh->type, clh->name,
1194 default_lookup_timeout,
1196 &send_lookup_response, clh);
1200 gns_resolver_lookup_record (clh->zone, clh->zone, clh->type, name,
1202 default_lookup_timeout,
1204 &send_lookup_response, clh);
1207 GNUNET_STATISTICS_update (statistics,
1208 "Record lookup attempts", 1, GNUNET_NO);
1212 * Test if the given AF is supported by this system.
1215 * @return GNUNET_OK if the AF is supported
1222 s = socket (af, SOCK_STREAM, 0);
1225 if (EAFNOSUPPORT == errno)
1227 fprintf (stderr, "Failed to create test socket: %s\n", STRERROR (errno));
1228 return GNUNET_SYSERR;
1239 * Process GNS requests.
1241 * @param cls closure)
1242 * @param server the initialized server
1243 * @param c configuration to use
1246 run (void *cls, struct GNUNET_SERVER_Handle *server,
1247 const struct GNUNET_CONFIGURATION_Handle *c)
1250 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Initializing GNS\n");
1253 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
1254 unsigned long long max_parallel_bg_queries = 0;
1255 unsigned long long default_lookup_timeout_secs = 0;
1256 int ignore_pending = GNUNET_NO;
1258 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1259 {&handle_shorten, NULL, GNUNET_MESSAGE_TYPE_GNS_SHORTEN, 0},
1260 {&handle_lookup, NULL, GNUNET_MESSAGE_TYPE_GNS_LOOKUP, 0},
1261 {&handle_get_authority, NULL, GNUNET_MESSAGE_TYPE_GNS_GET_AUTH, 0}
1266 v6_enabled = test_af (AF_INET6);
1267 v4_enabled = test_af (AF_INET);
1269 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (c, "gns",
1270 "ZONEKEY", &keyfile))
1272 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1273 "No private key for root zone specified!\n");
1274 GNUNET_SCHEDULER_shutdown ();
1278 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1279 "Using keyfile %s for root zone.\n", keyfile);
1281 zone_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
1282 GNUNET_CRYPTO_rsa_key_get_public (zone_key, &pkey);
1284 GNUNET_CRYPTO_short_hash(&pkey,
1285 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1287 GNUNET_free(keyfile);
1290 * handle to our local namestore
1292 namestore_handle = GNUNET_NAMESTORE_connect(c);
1294 if (NULL == namestore_handle)
1296 //FIXME do error handling;
1297 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1298 "Failed to connect to the namestore!\n");
1299 GNUNET_SCHEDULER_shutdown ();
1305 auto_import_pkey = GNUNET_NO;
1308 GNUNET_CONFIGURATION_get_value_yesno (c, "gns",
1309 "AUTO_IMPORT_PKEY"))
1311 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1312 "Automatic PKEY import is enabled.\n");
1313 auto_import_pkey = GNUNET_YES;
1317 zone_iteration_interval = INITIAL_ZONE_ITERATION_INTERVAL;
1319 record_put_interval = DEFAULT_RECORD_PUT_INTERVAL;
1322 GNUNET_CONFIGURATION_get_value_time (c, "gns",
1323 "RECORD_PUT_INTERVAL",
1324 &record_put_interval))
1326 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1327 "Record put interval: %llu\n",
1328 record_put_interval);
1332 GNUNET_CONFIGURATION_get_value_number (c, "gns",
1333 "MAX_PARALLEL_BACKGROUND_QUERIES",
1334 &max_parallel_bg_queries))
1336 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1337 "Number of allowed parallel background queries: %llu\n",
1338 max_parallel_bg_queries);
1342 GNUNET_CONFIGURATION_get_value_yesno (c, "gns",
1343 "AUTO_IMPORT_CONFIRMATION_REQ"))
1345 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1346 "Auto import requires user confirmation\n");
1347 ignore_pending = GNUNET_YES;
1351 GNUNET_CONFIGURATION_get_value_number(c, "gns",
1352 "DEFAULT_LOOKUP_TIMEOUT",
1353 &default_lookup_timeout_secs))
1355 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1356 "Default lookup timeout: %llus\n", default_lookup_timeout_secs);
1357 default_lookup_timeout = GNUNET_TIME_relative_multiply(
1358 GNUNET_TIME_UNIT_SECONDS,
1359 default_lookup_timeout_secs);
1365 dht_handle = GNUNET_DHT_connect(c,
1366 //max_parallel_bg_queries); //FIXME get ht_len from cfg
1369 if (NULL == dht_handle)
1371 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not connect to DHT!\n");
1374 if (gns_resolver_init(namestore_handle, dht_handle, zone_hash, c,
1375 max_parallel_bg_queries,
1379 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1380 "Unable to initialize resolver!\n");
1381 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
1386 GNUNET_CONFIGURATION_get_value_yesno (c, "gns", "HIJACK_DNS"))
1388 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1389 "DNS hijacking enabled... connecting to service.\n");
1391 if (gns_interceptor_init(zone_hash, zone_key, c) == GNUNET_SYSERR)
1393 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1394 "Failed to enable the dns interceptor!\n");
1399 * Schedule periodic put
1401 * We have roughly an hour for all records;
1403 first_zone_iteration = GNUNET_YES;
1404 zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_start, NULL);
1406 GNUNET_SERVER_add_handlers (server, handlers);
1409 //GNUNET_SERVER_disconnect_notify (server,
1410 // &client_disconnect_notification,
1413 statistics = GNUNET_STATISTICS_create ("gns", c);
1415 nc = GNUNET_SERVER_notification_context_create (server, 1);
1417 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
1423 * The main function for the GNS service.
1425 * @param argc number of arguments from the command line
1426 * @param argv command line arguments
1427 * @return 0 ok, 1 on error
1430 main (int argc, char *const *argv)
1436 GNUNET_SERVICE_run (argc, argv, "gns", GNUNET_SERVICE_OPTION_NONE, &run,
1441 /* end of gnunet-service-gns.c */