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 if (num_public_records == 0)
386 zone_update_taskid = GNUNET_SCHEDULER_add_delayed (zone_iteration_interval,
387 &update_zone_dht_start,
390 zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_start, NULL);
391 GNUNET_STATISTICS_update (statistics,
392 "Number of zone iterations", 1, GNUNET_NO);
394 last_num_public_records = num_public_records;
395 GNUNET_STATISTICS_set (statistics,
396 "Number of public records in DHT",
397 last_num_public_records,
402 namelen = strlen(name) + 1;
406 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
407 "No records given for name %s! Skipping...\n",
409 zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_next,
414 if (signature == NULL)
416 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
417 "No signature for %s record data provided! Skipping...\n",
419 zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_next,
425 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
426 "Putting records for %s into the DHT\n", name);
428 rd_payload_length = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
430 nrb = GNUNET_malloc(rd_payload_length + namelen
431 + sizeof(struct GNSNameRecordBlock));
433 nrb->signature = *signature;
435 nrb->public_key = *key;
437 nrb->rd_count = htonl(rd_count);
439 memcpy(&nrb[1], name, namelen);
441 nrb_data = (char*)&nrb[1];
444 rd_payload_length += sizeof(struct GNSNameRecordBlock) + namelen;
446 if (-1 == GNUNET_NAMESTORE_records_serialize (rd_count,
451 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
452 "Record serialization failed! Skipping...\n");
454 zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_next,
461 * calculate DHT key: H(name) xor H(pubkey)
463 GNUNET_CRYPTO_short_hash(key,
464 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
466 GNUNET_CRYPTO_short_hash(name, strlen(name), &name_hash);
467 GNUNET_CRYPTO_short_hash_double (&name_hash, &name_hash_double);
468 GNUNET_CRYPTO_short_hash_double (&zhash, &zone_hash_double);
469 GNUNET_CRYPTO_hash_xor(&zone_hash_double, &name_hash_double, &xor_hash);
471 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
472 "zone identity: %s\n", GNUNET_h2s (&zone_hash_double));
474 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
475 "putting %d records for %s under key: %s with size %d\n",
476 rd_count, name, GNUNET_h2s (&xor_hash), rd_payload_length);
478 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
479 "DHT req to %d\n", DHT_OPERATION_TIMEOUT.rel_value);
481 GNUNET_STATISTICS_update (statistics,
482 "Record bytes put into DHT", rd_payload_length, GNUNET_NO);
484 /* FIXME: keep return value to possibly cancel? */
485 GNUNET_DHT_put (dht_handle, &xor_hash,
486 DHT_GNS_REPLICATION_LEVEL,
488 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
492 DHT_OPERATION_TIMEOUT,
494 NULL); //cls for cont
496 num_public_records++;
498 if ((num_public_records > last_num_public_records)
499 && (first_zone_iteration == GNUNET_NO))
501 zone_iteration_interval = GNUNET_TIME_relative_divide (record_put_interval,
503 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
504 "Last record count was lower than current record count... increasing.\n");
505 next_put_interval = GNUNET_TIME_relative_divide (zone_iteration_interval,
506 LATE_ITERATION_SPEEDUP_FACTOR);
510 next_put_interval = zone_iteration_interval;
512 GNUNET_STATISTICS_set (statistics,
513 "Current zone iteration interval [msec]",
514 next_put_interval.rel_value,
518 * Reschedule periodic put
520 zone_update_taskid = GNUNET_SCHEDULER_add_delayed (next_put_interval,
521 &update_zone_dht_next,
529 * Periodically iterate over our zone and store everything in dht
532 * @param tc task context
535 update_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
537 zone_update_taskid = GNUNET_SCHEDULER_NO_TASK;
539 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Scheduling DHT zone update!\n");
541 /* start counting again */
542 num_public_records = 0;
543 namestore_iter = GNUNET_NAMESTORE_zone_iteration_start (namestore_handle,
545 GNUNET_NAMESTORE_RF_AUTHORITY,
546 GNUNET_NAMESTORE_RF_PRIVATE,
551 /* 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 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message with %s\n",
563 "SHORTEN_RESULT", name);
564 struct GNUNET_GNS_ClientShortenResultMessage *rmsg;
565 struct ClientShortenHandle *csh = (struct ClientShortenHandle *)cls;
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->unique_id;
579 rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_SHORTEN_RESULT);
581 htons(sizeof(struct GNUNET_GNS_ClientShortenResultMessage) +
584 strcpy((char*)&rmsg[1], name);
586 GNUNET_SERVER_notification_context_unicast (nc, csh->client,
587 (const struct GNUNET_MessageHeader *) rmsg,
589 GNUNET_SERVER_receive_done (csh->client, GNUNET_OK);
591 if (NULL != csh->namestore_task)
592 GNUNET_NAMESTORE_cancel (csh->namestore_task);
601 * Lookup the zone infos and shorten name
603 * @param cls the client shorten handle
604 * @param key key of the zone
605 * @param expiration expiration of record
606 * @param name name found or null if no result
607 * @param rd_count number of records found
608 * @param rd record data
613 process_shorten_in_private_zone_lookup (void *cls,
614 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
615 struct GNUNET_TIME_Absolute expiration,
617 unsigned int rd_count,
618 const struct GNUNET_NAMESTORE_RecordData *rd,
619 const struct GNUNET_CRYPTO_RsaSignature *signature)
621 struct ClientShortenHandle *csh = cls;
622 csh->namestore_task = NULL;
623 struct GNUNET_CRYPTO_ShortHashCode *szone = &csh->shorten_zone;
624 struct GNUNET_CRYPTO_ShortHashCode *pzone = &csh->private_zone;
626 if (0 == strcmp (csh->private_zone_id, ""))
631 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
632 "No shorten zone in private zone!\n");
634 strcpy (csh->shorten_zone_id, "");
641 GNUNET_assert (rd_count == 1);
643 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
644 "Shorten zone %s found in private zone %s\n",
645 name, csh->private_zone_id);
647 sprintf (csh->shorten_zone_id, "%s.%s", name, csh->private_zone_id);
650 GNUNET_CONTAINER_DLL_remove (csh_head, csh_tail, csh);
652 gns_resolver_shorten_name (&csh->root_zone,
656 csh->private_zone_id,
657 csh->shorten_zone_id,
658 &send_shorten_response, csh);
664 * Lookup the zone infos and shorten name
666 * @param cls the shorten handle
667 * @param key key of the zone
668 * @param expiration expiration of record
669 * @param name name found or null if no result
670 * @param rd_count number of records found
671 * @param rd record data
676 process_shorten_in_root_zone_lookup (void *cls,
677 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
678 struct GNUNET_TIME_Absolute expiration,
680 unsigned int rd_count,
681 const struct GNUNET_NAMESTORE_RecordData *rd,
682 const struct GNUNET_CRYPTO_RsaSignature *signature)
684 struct ClientShortenHandle *csh = cls;
685 csh->namestore_task = NULL;
686 struct GNUNET_CRYPTO_ShortHashCode *szone = &csh->shorten_zone;
687 struct GNUNET_CRYPTO_ShortHashCode *pzone = &csh->private_zone;
689 if (0 == strcmp (csh->private_zone_id, ""))
694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
695 "No shorten zone in zone and no private zone!\n");
697 strcpy (csh->shorten_zone_id, "");
699 GNUNET_CONTAINER_DLL_remove (csh_head, csh_tail, csh);
702 gns_resolver_shorten_name (&csh->root_zone,
706 csh->private_zone_id,
707 csh->shorten_zone_id,
708 &send_shorten_response, csh);
712 GNUNET_assert (rd_count == 1);
714 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
715 "Private zone %s found in root zone\n", name);
717 strcpy (csh->private_zone_id, name);
719 csh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
722 &process_shorten_in_private_zone_lookup,
728 * Lookup the zone infos and shorten name
730 * @param cls the shorten handle
731 * @param key key of the zone
732 * @param expiration expiration of record
733 * @param name name found or null if no result
734 * @param rd_count number of records found
735 * @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;
749 csh->namestore_task = NULL;
753 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
754 "No private zone in root zone\n");
756 strcpy (csh->private_zone_id, "");
758 csh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
761 &process_shorten_in_root_zone_lookup,
766 GNUNET_assert (rd_count == 1);
768 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
769 "Private zone %s found in root zone\n", name);
771 strcpy (csh->private_zone_id, name);
773 csh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
776 &process_shorten_in_private_zone_lookup,
781 * Lookup the zone infos and shorten name
783 * @param csh the shorten handle
787 start_shorten_name (struct ClientShortenHandle *csh)
789 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
790 "Looking for private zone name in root zone\n");
792 csh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
795 &process_private_in_root_zone_lookup,
801 * Handle a shorten message from the api
803 * @param cls the closure
804 * @param client the client
805 * @param message the message
807 static void handle_shorten (void *cls,
808 struct GNUNET_SERVER_Client * client,
809 const struct GNUNET_MessageHeader * message)
811 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "SHORTEN");
814 struct ClientShortenHandle *csh;
815 char name[MAX_DNS_NAME_LENGTH];
816 char* nameptr = name;
818 if (ntohs (message->size) < sizeof (struct GNUNET_GNS_ClientShortenMessage))
821 GNUNET_SERVER_receive_done (client, GNUNET_OK);
826 struct GNUNET_GNS_ClientShortenMessage *sh_msg =
827 (struct GNUNET_GNS_ClientShortenMessage *) message;
829 msg_size = ntohs(message->size);
831 if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
834 GNUNET_SERVER_receive_done (client, GNUNET_OK);
838 csh = GNUNET_malloc(sizeof(struct ClientShortenHandle));
839 csh->client = client;
840 csh->unique_id = sh_msg->id;
842 GNUNET_CONTAINER_DLL_insert (csh_head, csh_tail, csh);
844 GNUNET_STRINGS_utf8_tolower((char*)&sh_msg[1], &nameptr);
846 if (strlen (name) < strlen(GNUNET_GNS_TLD)) {
847 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
848 "SHORTEN: %s is too short", name);
849 send_shorten_response(csh, name);
853 if (strlen (name) > MAX_DNS_NAME_LENGTH) {
854 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
855 "SHORTEN: %s is too long", name);
856 send_shorten_response(csh, name);
860 if (!is_gnunet_tld(name) && !is_zkey_tld(name))
862 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
863 "%s is not our domain. Returning\n", name);
864 send_shorten_response(csh, name);
868 csh->shorten_zone = sh_msg->shorten_zone;
869 csh->private_zone = sh_msg->private_zone;
871 strcpy (csh->name, name);
873 GNUNET_SERVER_notification_context_add (nc, client);
875 if (1 == ntohl(sh_msg->use_default_zone))
876 csh->root_zone = zone_hash; //Default zone
878 csh->root_zone = sh_msg->zone;
880 start_shorten_name (csh);
882 GNUNET_STATISTICS_update (statistics,
883 "Name shorten attempts", 1, GNUNET_NO);
889 * Send get authority response back to client
891 * @param cls the closure containing a client get auth handle
892 * @param name the shortened name result or NULL if cannot be shortened
895 send_get_auth_response(void *cls, const char* name)
897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message with %s\n",
898 "GET_AUTH_RESULT", name);
899 struct GNUNET_GNS_ClientGetAuthResultMessage *rmsg;
900 struct ClientGetAuthHandle *cah = (struct ClientGetAuthHandle *)cls;
904 GNUNET_STATISTICS_update (statistics,
905 "Authorities resolved", 1, GNUNET_NO);
913 rmsg = GNUNET_malloc(sizeof(struct GNUNET_GNS_ClientGetAuthResultMessage)
916 rmsg->id = cah->unique_id;
917 rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_GET_AUTH_RESULT);
919 htons(sizeof(struct GNUNET_GNS_ClientGetAuthResultMessage) +
922 strcpy((char*)&rmsg[1], name);
924 GNUNET_SERVER_notification_context_unicast (nc, cah->client,
925 (const struct GNUNET_MessageHeader *) rmsg,
927 GNUNET_SERVER_receive_done (cah->client, GNUNET_OK);
930 GNUNET_free_non_null(cah->name);
939 * Handle a get authority message from the api
941 * @param cls the closure
942 * @param client the client
943 * @param message the message
945 static void handle_get_authority(void *cls,
946 struct GNUNET_SERVER_Client * client,
947 const struct GNUNET_MessageHeader * message)
949 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "GET_AUTH");
952 struct ClientGetAuthHandle *cah;
953 char name[MAX_DNS_NAME_LENGTH];
954 char* nameptr = name;
957 if (ntohs (message->size) < sizeof (struct GNUNET_GNS_ClientGetAuthMessage))
960 GNUNET_SERVER_receive_done (client, GNUNET_OK);
964 GNUNET_SERVER_notification_context_add (nc, client);
966 struct GNUNET_GNS_ClientGetAuthMessage *sh_msg =
967 (struct GNUNET_GNS_ClientGetAuthMessage *) message;
969 msg_size = ntohs(message->size);
971 if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
974 GNUNET_SERVER_receive_done (client, GNUNET_OK);
978 GNUNET_STRINGS_utf8_tolower((char*)&sh_msg[1], &nameptr);
981 cah = GNUNET_malloc(sizeof(struct ClientGetAuthHandle));
982 cah->client = client;
983 cah->unique_id = sh_msg->id;
985 if (strlen(name) < strlen(GNUNET_GNS_TLD))
987 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
988 "GET_AUTH: %s is too short. Returning\n", name);
990 send_get_auth_response(cah, name);
994 if (strlen (name) > MAX_DNS_NAME_LENGTH) {
995 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
996 "GET_AUTH: %s is too long", name);
998 send_get_auth_response(cah, name);
1002 if (strcmp(name+strlen(name)-strlen(GNUNET_GNS_TLD),
1003 GNUNET_GNS_TLD) != 0)
1005 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1006 "GET_AUTH: %s is not our domain. Returning\n", name);
1008 send_get_auth_response(cah, name);
1012 if (strcmp(name, GNUNET_GNS_TLD) == 0)
1014 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1015 "GET_AUTH: %s is us. Returning\n", name);
1017 send_get_auth_response(cah, name);
1021 cah->name = GNUNET_malloc(strlen(name)
1022 - strlen(GNUNET_GNS_TLD) + 1);
1023 memset(cah->name, 0,
1024 strlen(name)-strlen(GNUNET_GNS_TLD) + 1);
1025 memcpy(cah->name, name,
1026 strlen(name)-strlen(GNUNET_GNS_TLD));
1028 /* Start delegation resolution in our namestore */
1029 gns_resolver_get_authority(zone_hash, zone_hash, name, &send_get_auth_response, cah);
1031 GNUNET_STATISTICS_update (statistics,
1032 "Authority lookup attempts", 1, GNUNET_NO);
1038 * Reply to client with the result from our lookup.
1040 * @param cls the closure (our client lookup handle)
1041 * @param rd_count the number of records
1042 * @param rd the record data
1045 send_lookup_response(void* cls,
1047 const struct GNUNET_NAMESTORE_RecordData *rd)
1049 struct ClientLookupHandle* clh = (struct ClientLookupHandle*)cls;
1050 struct GNUNET_GNS_ClientLookupResultMessage *rmsg;
1053 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message with %d results\n",
1054 "LOOKUP_RESULT", rd_count);
1056 len = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
1057 rmsg = GNUNET_malloc(len+sizeof(struct GNUNET_GNS_ClientLookupResultMessage));
1059 rmsg->id = clh->unique_id;
1060 rmsg->rd_count = htonl(rd_count);
1061 rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT);
1063 htons(len+sizeof(struct GNUNET_GNS_ClientLookupResultMessage));
1065 GNUNET_NAMESTORE_records_serialize (rd_count, rd, len, (char*)&rmsg[1]);
1067 GNUNET_SERVER_notification_context_unicast (nc, clh->client,
1068 (const struct GNUNET_MessageHeader *) rmsg,
1070 GNUNET_SERVER_receive_done (clh->client, GNUNET_OK);
1073 GNUNET_free(clh->name);
1075 if (NULL != clh->shorten_key)
1076 GNUNET_free(clh->shorten_key);
1080 GNUNET_STATISTICS_update (statistics,
1081 "Completed lookups", 1, GNUNET_NO);
1085 GNUNET_STATISTICS_update (statistics,
1086 "Records resolved", rd_count, GNUNET_NO);
1093 * Handle lookup requests from client
1095 * @param cls the closure
1096 * @param client the client
1097 * @param message the message
1100 handle_lookup(void *cls,
1101 struct GNUNET_SERVER_Client * client,
1102 const struct GNUNET_MessageHeader * message)
1104 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "LOOKUP");
1106 size_t msg_size = 0;
1108 char name[MAX_DNS_NAME_LENGTH];
1109 struct ClientLookupHandle *clh;
1110 char* nameptr = name;
1112 struct GNUNET_CRYPTO_RsaPrivateKey *key;
1113 struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded *pkey;
1116 if (ntohs (message->size) < sizeof (struct GNUNET_GNS_ClientLookupMessage))
1118 GNUNET_break_op (0);
1119 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1123 GNUNET_SERVER_notification_context_add (nc, client);
1125 struct GNUNET_GNS_ClientLookupMessage *sh_msg =
1126 (struct GNUNET_GNS_ClientLookupMessage *) message;
1128 msg_size = ntohs(message->size);
1130 if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
1132 GNUNET_break_op (0);
1133 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1137 if (1 == ntohl(sh_msg->have_key))
1139 pkey = (struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded *)&sh_msg[1];
1140 tmp_pkey = (char*)&sh_msg[1];
1141 key = GNUNET_CRYPTO_rsa_decode_key (tmp_pkey, ntohs(pkey->len));
1142 GNUNET_STRINGS_utf8_tolower(&tmp_pkey[ntohs(pkey->len)], &nameptr);
1147 GNUNET_STRINGS_utf8_tolower((char*)&sh_msg[1], &nameptr);
1150 namelen = strlen(name)+1;
1151 clh = GNUNET_malloc (sizeof (struct ClientLookupHandle));
1152 memset (clh, 0, sizeof (struct ClientLookupHandle));
1153 clh->client = client;
1154 clh->name = GNUNET_malloc(namelen);
1155 strcpy(clh->name, name);
1156 clh->unique_id = sh_msg->id;
1157 clh->type = ntohl(sh_msg->type);
1158 clh->shorten_key = key;
1160 only_cached = ntohl(sh_msg->only_cached);
1162 if (strlen (name) > MAX_DNS_NAME_LENGTH) {
1163 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1164 "LOOKUP: %s is too long", name);
1166 send_lookup_response(clh, 0, NULL);
1170 if ((clh->type == GNUNET_GNS_RECORD_A) &&
1171 (GNUNET_OK != v4_enabled))
1173 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1174 "LOOKUP: Query for A record but AF_INET not supported!");
1176 send_lookup_response(clh, 0, NULL);
1180 if ((clh->type == GNUNET_GNS_RECORD_AAAA) &&
1181 (GNUNET_OK != v6_enabled))
1183 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1184 "LOOKUP: Query for AAAA record but AF_INET6 not supported!");
1186 send_lookup_response(clh, 0, NULL);
1190 if (1 == ntohl(sh_msg->use_default_zone))
1191 clh->zone = zone_hash; //Default zone
1193 clh->zone = sh_msg->zone;
1195 if (GNUNET_YES == auto_import_pkey)
1197 gns_resolver_lookup_record (clh->zone, clh->zone, clh->type, clh->name,
1199 default_lookup_timeout,
1201 &send_lookup_response, clh);
1205 gns_resolver_lookup_record (clh->zone, clh->zone, clh->type, name,
1207 default_lookup_timeout,
1209 &send_lookup_response, clh);
1212 GNUNET_STATISTICS_update (statistics,
1213 "Record lookup attempts", 1, GNUNET_NO);
1217 * Test if the given AF is supported by this system.
1220 * @return GNUNET_OK if the AF is supported
1227 s = socket (af, SOCK_STREAM, 0);
1230 if (EAFNOSUPPORT == errno)
1232 fprintf (stderr, "Failed to create test socket: %s\n", STRERROR (errno));
1233 return GNUNET_SYSERR;
1244 * Process GNS requests.
1246 * @param cls closure)
1247 * @param server the initialized server
1248 * @param c configuration to use
1251 run (void *cls, struct GNUNET_SERVER_Handle *server,
1252 const struct GNUNET_CONFIGURATION_Handle *c)
1255 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Initializing GNS\n");
1258 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
1259 unsigned long long max_parallel_bg_queries = 0;
1260 unsigned long long default_lookup_timeout_secs = 0;
1261 int ignore_pending = GNUNET_NO;
1263 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1264 {&handle_shorten, NULL, GNUNET_MESSAGE_TYPE_GNS_SHORTEN, 0},
1265 {&handle_lookup, NULL, GNUNET_MESSAGE_TYPE_GNS_LOOKUP, 0},
1266 {&handle_get_authority, NULL, GNUNET_MESSAGE_TYPE_GNS_GET_AUTH, 0}
1271 v6_enabled = test_af (AF_INET6);
1272 v4_enabled = test_af (AF_INET);
1274 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (c, "gns",
1275 "ZONEKEY", &keyfile))
1277 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1278 "No private key for root zone specified!\n");
1279 GNUNET_SCHEDULER_shutdown ();
1283 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1284 "Using keyfile %s for root zone.\n", keyfile);
1286 zone_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
1287 GNUNET_CRYPTO_rsa_key_get_public (zone_key, &pkey);
1289 GNUNET_CRYPTO_short_hash(&pkey,
1290 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1292 GNUNET_free(keyfile);
1295 * handle to our local namestore
1297 namestore_handle = GNUNET_NAMESTORE_connect(c);
1299 if (NULL == namestore_handle)
1301 //FIXME do error handling;
1302 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1303 "Failed to connect to the namestore!\n");
1304 GNUNET_SCHEDULER_shutdown ();
1310 auto_import_pkey = GNUNET_NO;
1313 GNUNET_CONFIGURATION_get_value_yesno (c, "gns",
1314 "AUTO_IMPORT_PKEY"))
1316 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1317 "Automatic PKEY import is enabled.\n");
1318 auto_import_pkey = GNUNET_YES;
1322 zone_iteration_interval = INITIAL_ZONE_ITERATION_INTERVAL;
1324 record_put_interval = DEFAULT_RECORD_PUT_INTERVAL;
1327 GNUNET_CONFIGURATION_get_value_time (c, "gns",
1328 "RECORD_PUT_INTERVAL",
1329 &record_put_interval))
1331 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1332 "Record put interval: %llu\n",
1333 record_put_interval);
1337 GNUNET_CONFIGURATION_get_value_number (c, "gns",
1338 "MAX_PARALLEL_BACKGROUND_QUERIES",
1339 &max_parallel_bg_queries))
1341 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1342 "Number of allowed parallel background queries: %llu\n",
1343 max_parallel_bg_queries);
1347 GNUNET_CONFIGURATION_get_value_yesno (c, "gns",
1348 "AUTO_IMPORT_CONFIRMATION_REQ"))
1350 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1351 "Auto import requires user confirmation\n");
1352 ignore_pending = GNUNET_YES;
1356 GNUNET_CONFIGURATION_get_value_number(c, "gns",
1357 "DEFAULT_LOOKUP_TIMEOUT",
1358 &default_lookup_timeout_secs))
1360 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1361 "Default lookup timeout: %llus\n", default_lookup_timeout_secs);
1362 default_lookup_timeout = GNUNET_TIME_relative_multiply(
1363 GNUNET_TIME_UNIT_SECONDS,
1364 default_lookup_timeout_secs);
1370 dht_handle = GNUNET_DHT_connect(c,
1371 //max_parallel_bg_queries); //FIXME get ht_len from cfg
1374 if (NULL == dht_handle)
1376 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not connect to DHT!\n");
1379 if (gns_resolver_init(namestore_handle, dht_handle, zone_hash, c,
1380 max_parallel_bg_queries,
1384 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1385 "Unable to initialize resolver!\n");
1386 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
1391 GNUNET_CONFIGURATION_get_value_yesno (c, "gns", "HIJACK_DNS"))
1393 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1394 "DNS hijacking enabled... connecting to service.\n");
1396 if (gns_interceptor_init(zone_hash, zone_key, c) == GNUNET_SYSERR)
1398 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1399 "Failed to enable the dns interceptor!\n");
1404 * Schedule periodic put
1406 * We have roughly an hour for all records;
1408 first_zone_iteration = GNUNET_YES;
1409 zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_start, NULL);
1411 GNUNET_SERVER_add_handlers (server, handlers);
1414 //GNUNET_SERVER_disconnect_notify (server,
1415 // &client_disconnect_notification,
1418 statistics = GNUNET_STATISTICS_create ("gns", c);
1420 nc = GNUNET_SERVER_notification_context_create (server, 1);
1422 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
1428 * The main function for the GNS service.
1430 * @param argc number of arguments from the command line
1431 * @param argv command line arguments
1432 * @return 0 ok, 1 on error
1435 main (int argc, char *const *argv)
1441 GNUNET_SERVICE_run (argc, argv, "gns", GNUNET_SERVICE_OPTION_NONE, &run,
1446 /* end of gnunet-service-gns.c */