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"
40 #include "gnunet_protocols.h"
42 #define INITIAL_ZONE_ITERATION_INTERVAL GNUNET_TIME_UNIT_MILLISECONDS
43 #define MINIMUM_ZONE_ITERATION_INTERVAL GNUNET_TIME_UNIT_SECONDS
44 #define DEFAULT_RECORD_PUT_INTERVAL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 4)
45 #define LATE_ITERATION_SPEEDUP_FACTOR 2
49 * Handle to a shorten operation from api
51 struct ClientShortenHandle
55 struct ClientShortenHandle *next;
58 struct ClientShortenHandle *prev;
60 /* the requesting client that */
61 struct GNUNET_SERVER_Client *client;
67 enum GNUNET_GNS_RecordType type;
70 char name[MAX_DNS_NAME_LENGTH];
72 /* name of private zone (relative to root) */
73 char private_zone_id[MAX_DNS_NAME_LENGTH];
75 /* name of shorten zone (relative to root) */
76 char shorten_zone_id[MAX_DNS_NAME_LENGTH];
79 struct GNUNET_CRYPTO_ShortHashCode root_zone;
82 struct GNUNET_CRYPTO_ShortHashCode private_zone;
85 struct GNUNET_CRYPTO_ShortHashCode shorten_zone;
87 /* Namestore lookup task */
88 struct GNUNET_NAMESTORE_QueueEntry *namestore_task;
94 * Handle to a get auhtority operation from api
96 struct ClientGetAuthHandle
98 /* the requesting client that */
99 struct GNUNET_SERVER_Client *client;
104 /* name to lookup authority */
111 * Handle to a lookup operation from api
113 struct ClientLookupHandle
116 /* the requesting client that */
117 struct GNUNET_SERVER_Client *client;
119 /* The zone we look up in */
120 struct GNUNET_CRYPTO_ShortHashCode zone;
122 /* Do we only want to lookup from local cache? */
129 enum GNUNET_GNS_RecordType type;
131 /* optional zone private key used for shorten */
132 struct GNUNET_CRYPTO_RsaPrivateKey *shorten_key;
134 /* the name to look up */
135 char* name; //Needed?
139 * Our handle to the DHT
141 static struct GNUNET_DHT_Handle *dht_handle;
144 * Our zone's private key
146 struct GNUNET_CRYPTO_RsaPrivateKey *zone_key;
149 * Our handle to the namestore service
150 * FIXME maybe need a second handle for iteration
152 struct GNUNET_NAMESTORE_Handle *namestore_handle;
155 * Handle to iterate over our authoritative zone in namestore
157 struct GNUNET_NAMESTORE_ZoneIterator *namestore_iter;
160 * The configuration the GNS service is running with
162 const struct GNUNET_CONFIGURATION_Handle *GNS_cfg;
165 * Our notification context.
167 static struct GNUNET_SERVER_NotificationContext *nc;
172 struct GNUNET_CRYPTO_ShortHashCode zone_hash;
175 * Useful for zone update for DHT put
177 static unsigned long long num_public_records;
180 * Last seen record count
182 static unsigned long long last_num_public_records;
187 static struct GNUNET_TIME_Relative zone_iteration_interval;
189 /* dht update interval FIXME define? */
190 static struct GNUNET_TIME_Relative record_put_interval;
192 /* zone update task */
193 GNUNET_SCHEDULER_TaskIdentifier zone_update_taskid = GNUNET_SCHEDULER_NO_TASK;
195 /* automatic pkey import for name shortening */
196 static int auto_import_pkey;
198 /* first zone iteration is specia */
199 static int first_zone_iteration;
202 static struct GNUNET_TIME_Relative default_lookup_timeout;
205 static int v6_enabled;
208 static int v4_enabled;
210 /* Shorten DLL for cancelling NS requests */
211 static struct ClientShortenHandle *csh_head;
213 /* Shorten DLL for cancelling NS requests */
214 static struct ClientShortenHandle *csh_tail;
216 /* Statistics handle */
217 static struct GNUNET_STATISTICS_Handle *statistics;
221 * Task run during shutdown.
227 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
229 struct ClientShortenHandle *csh_tmp;
231 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
233 while (NULL != (csh_tmp = csh_head))
235 GNUNET_CONTAINER_DLL_remove (csh_head, csh_tail, csh_tmp);
236 GNUNET_free(csh_tmp);
238 GNUNET_SERVER_notification_context_destroy (nc);
239 gns_interceptor_stop ();
240 gns_resolver_cleanup ();
241 if (NULL != statistics)
242 GNUNET_STATISTICS_destroy (statistics, GNUNET_NO);
243 if (zone_update_taskid != GNUNET_SCHEDULER_NO_TASK)
244 GNUNET_SCHEDULER_cancel (zone_update_taskid);
245 if (NULL != namestore_iter)
246 GNUNET_NAMESTORE_zone_iteration_stop (namestore_iter);
247 GNUNET_NAMESTORE_disconnect(namestore_handle);
248 GNUNET_DHT_disconnect(dht_handle);
253 * Method called periodically that triggers iteration over authoritative records
256 * @param tc task context
259 update_zone_dht_next(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
261 zone_update_taskid = GNUNET_SCHEDULER_NO_TASK;
262 GNUNET_NAMESTORE_zone_iterator_next (namestore_iter);
267 * Continuation for DHT put
270 * @param success GNUNET_OK if the PUT was transmitted,
271 * GNUNET_NO on timeout,
272 * GNUNET_SYSERR on disconnect from service
273 * after the PUT message was transmitted
274 * (so we don't know if it was received or not)
277 record_dht_put(void *cls, int success)
279 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "put request transmitted\n");
285 update_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
288 * Function used to put all records successively into the DHT.
290 * @param cls the closure (NULL)
291 * @param key the public key of the authority (ours)
292 * @param expiration lifetime of the namestore entry
293 * @param name the name of the records
294 * @param rd_count the number of records in data
295 * @param rd the record data
296 * @param signature the signature for the record data
299 put_gns_record(void *cls,
300 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
301 struct GNUNET_TIME_Absolute expiration,
303 unsigned int rd_count,
304 const struct GNUNET_NAMESTORE_RecordData *rd,
305 const struct GNUNET_CRYPTO_RsaSignature *signature)
307 struct GNSNameRecordBlock *nrb;
308 struct GNUNET_CRYPTO_ShortHashCode name_hash;
309 struct GNUNET_CRYPTO_ShortHashCode zhash;
310 struct GNUNET_HashCode xor_hash;
311 struct GNUNET_HashCode name_hash_double;
312 struct GNUNET_HashCode zone_hash_double;
313 uint32_t rd_payload_length;
314 char* nrb_data = NULL;
316 struct GNUNET_TIME_Relative next_put_interval;
321 first_zone_iteration = GNUNET_NO;
322 if (0 == num_public_records)
325 * If no records are known (startup) or none present
326 * we can safely set the interval to the value for a single
329 zone_iteration_interval = GNUNET_TIME_relative_divide (record_put_interval,
332 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
333 "No records in db.\n");
337 zone_iteration_interval = GNUNET_TIME_relative_divide (record_put_interval,
341 zone_iteration_interval = GNUNET_TIME_relative_max (MINIMUM_ZONE_ITERATION_INTERVAL,
342 zone_iteration_interval);
344 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
345 "Adjusted zone iteration interval to %s\n",
346 GNUNET_STRINGS_relative_time_to_string (zone_iteration_interval, GNUNET_YES));
347 GNUNET_STATISTICS_set (statistics,
348 "Current zone iteration interval [msec]",
349 zone_iteration_interval.rel_value,
352 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
353 "Zone iteration finished. Rescheduling zone iteration\n");
355 namestore_iter = NULL;
356 if (num_public_records == 0)
357 zone_update_taskid = GNUNET_SCHEDULER_add_delayed (zone_iteration_interval,
358 &update_zone_dht_start,
361 zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_start, NULL);
362 GNUNET_STATISTICS_update (statistics,
363 "Number of zone iterations", 1, GNUNET_NO);
365 last_num_public_records = num_public_records;
366 GNUNET_STATISTICS_set (statistics,
367 "Number of public records in DHT",
368 last_num_public_records,
373 namelen = strlen(name) + 1;
376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
377 "No records for name `%s'! Skipping.\n",
379 zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_next,
383 if (signature == NULL)
386 zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_next,
391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
392 "Putting records for `%s' into the DHT\n", name);
394 rd_payload_length = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
395 nrb = GNUNET_malloc(rd_payload_length + namelen
396 + sizeof(struct GNSNameRecordBlock));
397 nrb->signature = *signature;
398 nrb->public_key = *key;
399 nrb->rd_count = htonl(rd_count);
400 memcpy(&nrb[1], name, namelen);
401 nrb_data = (char *) &nrb[1];
403 rd_payload_length += sizeof(struct GNSNameRecordBlock) + namelen;
404 GNUNET_CRYPTO_short_hash (key,
405 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
407 if (-1 == GNUNET_NAMESTORE_records_serialize (rd_count,
412 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
413 _("Records for name `%s' in zone %s too large to fit into DHT"),
415 GNUNET_h2s (&zhash) /* FIXME: write converter for short hash... */);
417 zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_next,
423 * calculate DHT key: H(name) xor H(pubkey)
425 GNUNET_CRYPTO_short_hash(name, strlen(name), &name_hash);
426 GNUNET_CRYPTO_short_hash_double (&name_hash, &name_hash_double);
427 GNUNET_CRYPTO_short_hash_double (&zhash, &zone_hash_double);
428 GNUNET_CRYPTO_hash_xor(&zone_hash_double, &name_hash_double, &xor_hash);
430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
431 "putting %u records from zone %s for `%s' under key: %s with size %u and timeout %s\n",
433 GNUNET_h2s (&zone_hash_double) /* FIXME: write converter for short hash... */,
435 GNUNET_h2s (&xor_hash),
436 (unsigned int) rd_payload_length,
437 GNUNET_STRINGS_relative_time_to_string (DHT_OPERATION_TIMEOUT, GNUNET_YES));
439 GNUNET_STATISTICS_update (statistics,
440 "Record bytes put into DHT", rd_payload_length, GNUNET_NO);
442 (void) GNUNET_DHT_put (dht_handle, &xor_hash,
443 DHT_GNS_REPLICATION_LEVEL,
444 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
445 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
449 DHT_OPERATION_TIMEOUT,
453 num_public_records++;
455 if ((num_public_records > last_num_public_records)
456 && (first_zone_iteration == GNUNET_NO))
458 zone_iteration_interval = GNUNET_TIME_relative_divide (record_put_interval,
460 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
461 "Last record count was lower than current record count... increasing.\n");
462 next_put_interval = GNUNET_TIME_relative_divide (zone_iteration_interval,
463 LATE_ITERATION_SPEEDUP_FACTOR);
467 next_put_interval = zone_iteration_interval;
469 GNUNET_STATISTICS_set (statistics,
470 "Current zone iteration interval [msec]",
471 next_put_interval.rel_value,
475 * Reschedule periodic put
477 zone_update_taskid = GNUNET_SCHEDULER_add_delayed (next_put_interval,
478 &update_zone_dht_next,
486 * Periodically iterate over our zone and store everything in dht
489 * @param tc task context
492 update_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
494 zone_update_taskid = GNUNET_SCHEDULER_NO_TASK;
496 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Scheduling DHT zone update!\n");
498 /* start counting again */
499 num_public_records = 0;
500 namestore_iter = GNUNET_NAMESTORE_zone_iteration_start (namestore_handle,
502 GNUNET_NAMESTORE_RF_AUTHORITY,
503 GNUNET_NAMESTORE_RF_PRIVATE,
508 /* END DHT ZONE PROPAGATION */
511 * Send shorten response back to client
513 * @param cls the closure containing a client shorten handle
514 * @param name the shortened name result or NULL if cannot be shortened
517 send_shorten_response(void* cls, const char* name)
519 struct ClientShortenHandle *csh = cls;
520 struct GNUNET_GNS_ClientShortenResultMessage *rmsg;
523 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message with %s\n",
524 "SHORTEN_RESULT", name);
528 name_len = strlen (name) + 1;
529 GNUNET_STATISTICS_update (statistics,
530 "Name shorten results", 1, GNUNET_NO);
532 rmsg = GNUNET_malloc(sizeof(struct GNUNET_GNS_ClientShortenResultMessage) +
535 rmsg->id = csh->unique_id;
536 rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_SHORTEN_RESULT);
538 htons(sizeof(struct GNUNET_GNS_ClientShortenResultMessage) +
540 memcpy (&rmsg[1], name, name_len);
542 GNUNET_SERVER_notification_context_unicast (nc, csh->client,
545 GNUNET_SERVER_receive_done (csh->client, GNUNET_OK);
546 if (NULL != csh->namestore_task)
547 GNUNET_NAMESTORE_cancel (csh->namestore_task);
554 * Lookup the zone infos and shorten name
556 * @param cls the client shorten handle
557 * @param key key of the zone
558 * @param expiration expiration of record
559 * @param name name found or null if no result
560 * @param rd_count number of records found
561 * @param rd record data
566 process_shorten_in_private_zone_lookup (void *cls,
567 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
568 struct GNUNET_TIME_Absolute expiration,
570 unsigned int rd_count,
571 const struct GNUNET_NAMESTORE_RecordData *rd,
572 const struct GNUNET_CRYPTO_RsaSignature *signature)
574 struct ClientShortenHandle *csh = cls;
575 csh->namestore_task = NULL;
576 struct GNUNET_CRYPTO_ShortHashCode *szone = &csh->shorten_zone;
577 struct GNUNET_CRYPTO_ShortHashCode *pzone = &csh->private_zone;
579 if (0 == strcmp (csh->private_zone_id, ""))
584 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
585 "No shorten zone in private zone!\n");
587 strcpy (csh->shorten_zone_id, "");
594 GNUNET_assert (rd_count == 1);
596 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
597 "Shorten zone %s found in private zone %s\n",
598 name, csh->private_zone_id);
600 sprintf (csh->shorten_zone_id, "%s.%s", name, csh->private_zone_id);
603 GNUNET_CONTAINER_DLL_remove (csh_head, csh_tail, csh);
605 gns_resolver_shorten_name (&csh->root_zone,
609 csh->private_zone_id,
610 csh->shorten_zone_id,
611 &send_shorten_response, csh);
617 * Lookup the zone infos and shorten name
619 * @param cls the shorten handle
620 * @param key key of the zone
621 * @param expiration expiration of record
622 * @param name name found or null if no result
623 * @param rd_count number of records found
624 * @param rd record data
629 process_shorten_in_root_zone_lookup (void *cls,
630 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
631 struct GNUNET_TIME_Absolute expiration,
633 unsigned int rd_count,
634 const struct GNUNET_NAMESTORE_RecordData *rd,
635 const struct GNUNET_CRYPTO_RsaSignature *signature)
637 struct ClientShortenHandle *csh = cls;
638 csh->namestore_task = NULL;
639 struct GNUNET_CRYPTO_ShortHashCode *szone = &csh->shorten_zone;
640 struct GNUNET_CRYPTO_ShortHashCode *pzone = &csh->private_zone;
642 if (0 == strcmp (csh->private_zone_id, ""))
647 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
648 "No shorten zone in zone and no private zone!\n");
650 strcpy (csh->shorten_zone_id, "");
652 GNUNET_CONTAINER_DLL_remove (csh_head, csh_tail, csh);
655 gns_resolver_shorten_name (&csh->root_zone,
659 csh->private_zone_id,
660 csh->shorten_zone_id,
661 &send_shorten_response, csh);
665 GNUNET_assert (rd_count == 1);
667 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
668 "Private zone %s found in root zone\n", name);
670 strcpy (csh->private_zone_id, name);
672 csh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
675 &process_shorten_in_private_zone_lookup,
681 * Lookup the zone infos and shorten name
683 * @param cls the shorten handle
684 * @param key key of the zone
685 * @param expiration expiration of record
686 * @param name name found or null if no result
687 * @param rd_count number of records found
688 * @param rd record data
693 process_private_in_root_zone_lookup (void *cls,
694 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
695 struct GNUNET_TIME_Absolute expiration,
697 unsigned int rd_count,
698 const struct GNUNET_NAMESTORE_RecordData *rd,
699 const struct GNUNET_CRYPTO_RsaSignature *signature)
701 struct ClientShortenHandle *csh = cls;
702 csh->namestore_task = NULL;
706 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
707 "No private zone in root zone\n");
709 strcpy (csh->private_zone_id, "");
711 csh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
714 &process_shorten_in_root_zone_lookup,
719 GNUNET_assert (rd_count == 1);
721 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
722 "Private zone %s found in root zone\n", name);
724 strcpy (csh->private_zone_id, name);
726 csh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
729 &process_shorten_in_private_zone_lookup,
734 * Lookup the zone infos and shorten name
736 * @param csh the shorten handle
740 start_shorten_name (struct ClientShortenHandle *csh)
742 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
743 "Looking for private zone name in root zone\n");
745 csh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
748 &process_private_in_root_zone_lookup,
754 * Handle a shorten message from the api
756 * @param cls the closure (unused)
757 * @param client the client
758 * @param message the message
761 handle_shorten (void *cls,
762 struct GNUNET_SERVER_Client * client,
763 const struct GNUNET_MessageHeader * message)
765 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "SHORTEN");
768 struct ClientShortenHandle *csh;
769 char name[MAX_DNS_NAME_LENGTH];
770 char* nameptr = name;
772 if (ntohs (message->size) < sizeof (struct GNUNET_GNS_ClientShortenMessage))
775 GNUNET_SERVER_receive_done (client, GNUNET_OK);
780 struct GNUNET_GNS_ClientShortenMessage *sh_msg =
781 (struct GNUNET_GNS_ClientShortenMessage *) message;
783 msg_size = ntohs(message->size);
785 if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
788 GNUNET_SERVER_receive_done (client, GNUNET_OK);
792 csh = GNUNET_malloc(sizeof (struct ClientShortenHandle));
793 csh->client = client;
794 csh->unique_id = sh_msg->id;
796 GNUNET_CONTAINER_DLL_insert (csh_head, csh_tail, csh);
798 GNUNET_STRINGS_utf8_tolower((char*)&sh_msg[1], &nameptr);
799 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
800 "SHORTEN: Converted %s to %s\n", (char*)&sh_msg[1], nameptr);
801 GNUNET_SERVER_notification_context_add (nc, client);
803 if (strlen (name) < strlen(GNUNET_GNS_TLD)) {
804 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
805 "SHORTEN: %s is too short\n", name);
806 GNUNET_CONTAINER_DLL_remove (csh_head, csh_tail, csh);
807 send_shorten_response(csh, name);
811 if (strlen (name) > MAX_DNS_NAME_LENGTH) {
812 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
813 "SHORTEN: %s is too long\n", name);
814 GNUNET_CONTAINER_DLL_remove (csh_head, csh_tail, csh);
815 send_shorten_response(csh, name);
819 if (!is_gnunet_tld(name) && !is_zkey_tld(name))
821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
822 "%s is not our domain. Returning\n", name);
823 GNUNET_CONTAINER_DLL_remove (csh_head, csh_tail, csh);
824 send_shorten_response(csh, name);
828 csh->shorten_zone = sh_msg->shorten_zone;
829 csh->private_zone = sh_msg->private_zone;
831 strcpy (csh->name, name);
834 if (1 == ntohl(sh_msg->use_default_zone))
835 csh->root_zone = zone_hash; //Default zone
837 csh->root_zone = sh_msg->zone;
839 start_shorten_name (csh);
841 GNUNET_STATISTICS_update (statistics,
842 "Name shorten attempts", 1, GNUNET_NO);
848 * Send get authority response back to client
850 * @param cls the closure containing a client get auth handle
851 * @param name the shortened name result or NULL if cannot be shortened
854 send_get_auth_response(void *cls, const char* name)
856 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message with %s\n",
857 "GET_AUTH_RESULT", name);
858 struct GNUNET_GNS_ClientGetAuthResultMessage *rmsg;
859 struct ClientGetAuthHandle *cah = (struct ClientGetAuthHandle *)cls;
863 GNUNET_STATISTICS_update (statistics,
864 "Authorities resolved", 1, GNUNET_NO);
872 rmsg = GNUNET_malloc(sizeof(struct GNUNET_GNS_ClientGetAuthResultMessage)
875 rmsg->id = cah->unique_id;
876 rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_GET_AUTH_RESULT);
878 htons(sizeof(struct GNUNET_GNS_ClientGetAuthResultMessage) +
881 strcpy((char*)&rmsg[1], name);
883 GNUNET_SERVER_notification_context_unicast (nc, cah->client,
886 GNUNET_SERVER_receive_done (cah->client, GNUNET_OK);
889 GNUNET_free_non_null(cah->name);
898 * Handle a get authority message from the api
900 * @param cls the closure
901 * @param client the client
902 * @param message the message
904 static void handle_get_authority(void *cls,
905 struct GNUNET_SERVER_Client * client,
906 const struct GNUNET_MessageHeader * message)
908 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "GET_AUTH");
911 struct ClientGetAuthHandle *cah;
912 char name[MAX_DNS_NAME_LENGTH];
913 char* nameptr = name;
916 if (ntohs (message->size) < sizeof (struct GNUNET_GNS_ClientGetAuthMessage))
919 GNUNET_SERVER_receive_done (client, GNUNET_OK);
923 GNUNET_SERVER_notification_context_add (nc, client);
925 struct GNUNET_GNS_ClientGetAuthMessage *sh_msg =
926 (struct GNUNET_GNS_ClientGetAuthMessage *) message;
928 msg_size = ntohs(message->size);
930 if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
933 GNUNET_SERVER_receive_done (client, GNUNET_OK);
937 GNUNET_STRINGS_utf8_tolower((char*)&sh_msg[1], &nameptr);
940 cah = GNUNET_malloc(sizeof(struct ClientGetAuthHandle));
941 cah->client = client;
942 cah->unique_id = sh_msg->id;
944 if (strlen(name) < strlen(GNUNET_GNS_TLD))
946 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
947 "GET_AUTH: %s is too short. Returning\n", name);
949 send_get_auth_response(cah, name);
953 if (strlen (name) > MAX_DNS_NAME_LENGTH) {
954 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
955 "GET_AUTH: %s is too long", name);
957 send_get_auth_response(cah, name);
961 if (strcmp(name+strlen(name)-strlen(GNUNET_GNS_TLD),
962 GNUNET_GNS_TLD) != 0)
964 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
965 "GET_AUTH: %s is not our domain. Returning\n", name);
967 send_get_auth_response(cah, name);
971 if (strcmp(name, GNUNET_GNS_TLD) == 0)
973 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
974 "GET_AUTH: %s is us. Returning\n", name);
976 send_get_auth_response(cah, name);
980 cah->name = GNUNET_malloc(strlen(name)
981 - strlen(GNUNET_GNS_TLD) + 1);
983 strlen(name)-strlen(GNUNET_GNS_TLD) + 1);
984 memcpy(cah->name, name,
985 strlen(name)-strlen(GNUNET_GNS_TLD));
987 /* Start delegation resolution in our namestore */
988 gns_resolver_get_authority(zone_hash, zone_hash, name, &send_get_auth_response, cah);
990 GNUNET_STATISTICS_update (statistics,
991 "Authority lookup attempts", 1, GNUNET_NO);
997 * Reply to client with the result from our lookup.
999 * @param cls the closure (our client lookup handle)
1000 * @param rd_count the number of records
1001 * @param rd the record data
1004 send_lookup_response(void* cls,
1006 const struct GNUNET_NAMESTORE_RecordData *rd)
1008 struct ClientLookupHandle* clh = (struct ClientLookupHandle*)cls;
1009 struct GNUNET_GNS_ClientLookupResultMessage *rmsg;
1012 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message with %d results\n",
1013 "LOOKUP_RESULT", rd_count);
1015 len = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
1016 rmsg = GNUNET_malloc(len+sizeof(struct GNUNET_GNS_ClientLookupResultMessage));
1018 rmsg->id = clh->unique_id;
1019 rmsg->rd_count = htonl(rd_count);
1020 rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT);
1022 htons(len+sizeof(struct GNUNET_GNS_ClientLookupResultMessage));
1024 GNUNET_NAMESTORE_records_serialize (rd_count, rd, len, (char*)&rmsg[1]);
1026 GNUNET_SERVER_notification_context_unicast (nc, clh->client,
1027 (const struct GNUNET_MessageHeader *) rmsg,
1029 GNUNET_SERVER_receive_done (clh->client, GNUNET_OK);
1032 GNUNET_free(clh->name);
1034 if (NULL != clh->shorten_key)
1035 GNUNET_free(clh->shorten_key);
1039 GNUNET_STATISTICS_update (statistics,
1040 "Completed lookups", 1, GNUNET_NO);
1044 GNUNET_STATISTICS_update (statistics,
1045 "Records resolved", rd_count, GNUNET_NO);
1052 * Handle lookup requests from client
1054 * @param cls the closure
1055 * @param client the client
1056 * @param message the message
1059 handle_lookup(void *cls,
1060 struct GNUNET_SERVER_Client * client,
1061 const struct GNUNET_MessageHeader * message)
1063 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "LOOKUP");
1065 size_t msg_size = 0;
1067 char name[MAX_DNS_NAME_LENGTH];
1068 struct ClientLookupHandle *clh;
1069 char* nameptr = name;
1071 struct GNUNET_CRYPTO_RsaPrivateKey *key;
1072 struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded *pkey;
1075 if (ntohs (message->size) < sizeof (struct GNUNET_GNS_ClientLookupMessage))
1077 GNUNET_break_op (0);
1078 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1082 GNUNET_SERVER_notification_context_add (nc, client);
1084 struct GNUNET_GNS_ClientLookupMessage *sh_msg =
1085 (struct GNUNET_GNS_ClientLookupMessage *) message;
1087 msg_size = ntohs(message->size);
1089 if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
1091 GNUNET_break_op (0);
1092 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1096 if (1 == ntohl(sh_msg->have_key))
1098 pkey = (struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded *)&sh_msg[1];
1099 tmp_pkey = (char*)&sh_msg[1];
1100 key = GNUNET_CRYPTO_rsa_decode_key (tmp_pkey, ntohs(pkey->len));
1101 GNUNET_STRINGS_utf8_tolower(&tmp_pkey[ntohs(pkey->len)], &nameptr);
1106 GNUNET_STRINGS_utf8_tolower((char*)&sh_msg[1], &nameptr);
1109 namelen = strlen(name)+1;
1110 clh = GNUNET_malloc (sizeof (struct ClientLookupHandle));
1111 memset (clh, 0, sizeof (struct ClientLookupHandle));
1112 clh->client = client;
1113 clh->name = GNUNET_malloc(namelen);
1114 strcpy(clh->name, name);
1115 clh->unique_id = sh_msg->id;
1116 clh->type = ntohl(sh_msg->type);
1117 clh->shorten_key = key;
1119 only_cached = ntohl(sh_msg->only_cached);
1121 if (strlen (name) > MAX_DNS_NAME_LENGTH) {
1122 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1123 "LOOKUP: %s is too long", name);
1125 send_lookup_response(clh, 0, NULL);
1129 if ((clh->type == GNUNET_GNS_RECORD_A) &&
1130 (GNUNET_OK != v4_enabled))
1132 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1133 "LOOKUP: Query for A record but AF_INET not supported!");
1135 send_lookup_response(clh, 0, NULL);
1139 if ((clh->type == GNUNET_GNS_RECORD_AAAA) &&
1140 (GNUNET_OK != v6_enabled))
1142 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1143 "LOOKUP: Query for AAAA record but AF_INET6 not supported!");
1145 send_lookup_response(clh, 0, NULL);
1149 if (1 == ntohl(sh_msg->use_default_zone))
1150 clh->zone = zone_hash; //Default zone
1152 clh->zone = sh_msg->zone;
1154 if (GNUNET_YES == auto_import_pkey)
1156 gns_resolver_lookup_record (clh->zone, clh->zone, clh->type, clh->name,
1158 default_lookup_timeout,
1160 &send_lookup_response, clh);
1164 gns_resolver_lookup_record (clh->zone, clh->zone, clh->type, name,
1166 default_lookup_timeout,
1168 &send_lookup_response, clh);
1171 GNUNET_STATISTICS_update (statistics,
1172 "Record lookup attempts", 1, GNUNET_NO);
1176 * Test if the given AF is supported by this system.
1179 * @return GNUNET_OK if the AF is supported
1186 s = socket (af, SOCK_STREAM, 0);
1189 if (EAFNOSUPPORT == errno)
1191 fprintf (stderr, "Failed to create test socket: %s\n", STRERROR (errno));
1192 return GNUNET_SYSERR;
1203 * Process GNS requests.
1205 * @param cls closure)
1206 * @param server the initialized server
1207 * @param c configuration to use
1210 run (void *cls, struct GNUNET_SERVER_Handle *server,
1211 const struct GNUNET_CONFIGURATION_Handle *c)
1214 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Initializing GNS\n");
1217 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
1218 unsigned long long max_parallel_bg_queries = 0;
1219 unsigned long long default_lookup_timeout_secs = 0;
1220 int ignore_pending = GNUNET_NO;
1222 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1223 {&handle_shorten, NULL, GNUNET_MESSAGE_TYPE_GNS_SHORTEN, 0},
1224 {&handle_lookup, NULL, GNUNET_MESSAGE_TYPE_GNS_LOOKUP, 0},
1225 {&handle_get_authority, NULL, GNUNET_MESSAGE_TYPE_GNS_GET_AUTH, 0}
1230 v6_enabled = test_af (AF_INET6);
1231 v4_enabled = test_af (AF_INET);
1233 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (c, "gns",
1234 "ZONEKEY", &keyfile))
1236 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1237 "No private key for root zone specified!\n");
1238 GNUNET_SCHEDULER_shutdown ();
1242 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1243 "Using keyfile %s for root zone.\n", keyfile);
1245 zone_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
1246 GNUNET_CRYPTO_rsa_key_get_public (zone_key, &pkey);
1248 GNUNET_CRYPTO_short_hash(&pkey,
1249 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1251 GNUNET_free(keyfile);
1254 * handle to our local namestore
1256 namestore_handle = GNUNET_NAMESTORE_connect(c);
1258 if (NULL == namestore_handle)
1260 //FIXME do error handling;
1261 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1262 "Failed to connect to the namestore!\n");
1263 GNUNET_SCHEDULER_shutdown ();
1269 auto_import_pkey = GNUNET_NO;
1272 GNUNET_CONFIGURATION_get_value_yesno (c, "gns",
1273 "AUTO_IMPORT_PKEY"))
1275 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1276 "Automatic PKEY import is enabled.\n");
1277 auto_import_pkey = GNUNET_YES;
1281 zone_iteration_interval = INITIAL_ZONE_ITERATION_INTERVAL;
1283 record_put_interval = DEFAULT_RECORD_PUT_INTERVAL;
1286 GNUNET_CONFIGURATION_get_value_time (c, "gns",
1287 "RECORD_PUT_INTERVAL",
1288 &record_put_interval))
1290 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1291 "Record put interval: %llu\n",
1292 record_put_interval);
1296 GNUNET_CONFIGURATION_get_value_number (c, "gns",
1297 "MAX_PARALLEL_BACKGROUND_QUERIES",
1298 &max_parallel_bg_queries))
1300 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1301 "Number of allowed parallel background queries: %llu\n",
1302 max_parallel_bg_queries);
1306 GNUNET_CONFIGURATION_get_value_yesno (c, "gns",
1307 "AUTO_IMPORT_CONFIRMATION_REQ"))
1309 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1310 "Auto import requires user confirmation\n");
1311 ignore_pending = GNUNET_YES;
1315 GNUNET_CONFIGURATION_get_value_number(c, "gns",
1316 "DEFAULT_LOOKUP_TIMEOUT",
1317 &default_lookup_timeout_secs))
1319 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1320 "Default lookup timeout: %llus\n", default_lookup_timeout_secs);
1321 default_lookup_timeout = GNUNET_TIME_relative_multiply(
1322 GNUNET_TIME_UNIT_SECONDS,
1323 default_lookup_timeout_secs);
1329 dht_handle = GNUNET_DHT_connect(c,
1330 //max_parallel_bg_queries); //FIXME get ht_len from cfg
1333 if (NULL == dht_handle)
1335 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not connect to DHT!\n");
1338 if (gns_resolver_init(namestore_handle, dht_handle, zone_hash, c,
1339 max_parallel_bg_queries,
1343 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1344 "Unable to initialize resolver!\n");
1345 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
1350 GNUNET_CONFIGURATION_get_value_yesno (c, "gns", "HIJACK_DNS"))
1352 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1353 "DNS hijacking enabled... connecting to service.\n");
1355 if (gns_interceptor_init(zone_hash, zone_key, c) == GNUNET_SYSERR)
1357 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1358 "Failed to enable the dns interceptor!\n");
1363 * Schedule periodic put
1365 * We have roughly an hour for all records;
1367 first_zone_iteration = GNUNET_YES;
1368 zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_start, NULL);
1370 GNUNET_SERVER_add_handlers (server, handlers);
1373 //GNUNET_SERVER_disconnect_notify (server,
1374 // &client_disconnect_notification,
1377 statistics = GNUNET_STATISTICS_create ("gns", c);
1379 nc = GNUNET_SERVER_notification_context_create (server, 1);
1381 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
1387 * The main function for the GNS service.
1389 * @param argc number of arguments from the command line
1390 * @param argv command line arguments
1391 * @return 0 ok, 1 on error
1394 main (int argc, char *const *argv)
1400 GNUNET_SERVICE_run (argc, argv, "gns", GNUNET_SERVICE_OPTION_NONE, &run,
1405 /* end of gnunet-service-gns.c */