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)
56 * Handle to a shorten operation from api
58 struct ClientShortenHandle
62 struct ClientShortenHandle *next;
65 struct ClientShortenHandle *prev;
67 /* the requesting client that */
68 struct GNUNET_SERVER_Client *client;
74 enum GNUNET_GNS_RecordType type;
77 char name[MAX_DNS_NAME_LENGTH];
79 /* name of private zone (relative to root) */
80 char private_zone_id[MAX_DNS_NAME_LENGTH];
82 /* name of shorten zone (relative to root) */
83 char shorten_zone_id[MAX_DNS_NAME_LENGTH];
86 struct GNUNET_CRYPTO_ShortHashCode root_zone;
89 struct GNUNET_CRYPTO_ShortHashCode private_zone;
92 struct GNUNET_CRYPTO_ShortHashCode shorten_zone;
94 /* Namestore lookup task */
95 struct GNUNET_NAMESTORE_QueueEntry *namestore_task;
101 * Handle to a get auhtority operation from api
103 struct ClientGetAuthHandle
105 /* the requesting client that */
106 struct GNUNET_SERVER_Client *client;
111 /* name to lookup authority */
118 * Handle to a lookup operation from api
120 struct ClientLookupHandle
123 /* the requesting client that */
124 struct GNUNET_SERVER_Client *client;
126 /* The zone we look up in */
127 struct GNUNET_CRYPTO_ShortHashCode zone;
129 /* Do we only want to lookup from local cache? */
136 enum GNUNET_GNS_RecordType type;
138 /* optional zone private key used for shorten */
139 struct GNUNET_CRYPTO_RsaPrivateKey *shorten_key;
141 /* the name to look up */
142 char* name; //Needed?
146 * Our handle to the DHT
148 static struct GNUNET_DHT_Handle *dht_handle;
151 * Our zone's private key
153 struct GNUNET_CRYPTO_RsaPrivateKey *zone_key;
156 * Our handle to the namestore service
157 * FIXME maybe need a second handle for iteration
159 struct GNUNET_NAMESTORE_Handle *namestore_handle;
162 * Handle to iterate over our authoritative zone in namestore
164 struct GNUNET_NAMESTORE_ZoneIterator *namestore_iter;
167 * The configuration the GNS service is running with
169 const struct GNUNET_CONFIGURATION_Handle *GNS_cfg;
172 * Our notification context.
174 static struct GNUNET_SERVER_NotificationContext *nc;
179 struct GNUNET_CRYPTO_ShortHashCode zone_hash;
182 * Useful for zone update for DHT put
184 static unsigned long long num_public_records;
187 * Last seen record count
189 static unsigned long long last_num_public_records;
191 static struct GNUNET_TIME_Relative zone_iteration_interval;
193 /* dht update interval FIXME define? */
194 static struct GNUNET_TIME_Relative record_put_interval;
196 /* zone update task */
197 GNUNET_SCHEDULER_TaskIdentifier zone_update_taskid = GNUNET_SCHEDULER_NO_TASK;
199 /* automatic pkey import for name shortening */
200 static int auto_import_pkey;
203 static struct GNUNET_TIME_Relative default_lookup_timeout;
206 static int v6_enabled;
209 static int v4_enabled;
211 /* Shorten DLL for cancelling NS requests */
212 static struct ClientShortenHandle *csh_head;
214 /* Shorten DLL for cancelling NS requests */
215 static struct ClientShortenHandle *csh_tail;
217 /* Statistics handle */
218 static struct GNUNET_STATISTICS_Handle *statistics;
221 * Send shorten response back to client
223 * @param cls the closure containing a client shorten handle
224 * @param name the shortened name result or NULL if cannot be shortened
227 send_shorten_response(void* cls, const char* name);
234 on_resolver_cleanup(void)
236 if (NULL != statistics)
237 GNUNET_STATISTICS_destroy (statistics, GNUNET_NO);
239 if (NULL != namestore_iter)
240 GNUNET_NAMESTORE_zone_iteration_stop (namestore_iter);
241 GNUNET_NAMESTORE_disconnect(namestore_handle);
242 GNUNET_DHT_disconnect(dht_handle);
246 * Task run during shutdown.
252 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
254 struct ClientShortenHandle *csh_tmp = csh_head;
256 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
259 while (csh_tmp != NULL)
261 GNUNET_CONTAINER_DLL_remove (csh_head, csh_tail, csh_tmp);
262 send_shorten_response (csh_tmp, csh_tmp->name);
265 GNUNET_SERVER_notification_context_destroy (nc);
267 gns_interceptor_stop();
268 gns_resolver_cleanup(&on_resolver_cleanup);
269 /* Kill zone task for it may make the scheduler hang */
270 if (zone_update_taskid != GNUNET_SCHEDULER_NO_TASK)
271 GNUNET_SCHEDULER_cancel(zone_update_taskid);
276 * Method called periodicattluy that triggers
277 * iteration over root zone
280 * @param tc task context
283 update_zone_dht_next(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
285 zone_update_taskid = GNUNET_SCHEDULER_NO_TASK;
286 GNUNET_NAMESTORE_zone_iterator_next (namestore_iter);
290 * Continuation for DHT put
293 * @param success GNUNET_OK if the PUT was transmitted,
294 * GNUNET_NO on timeout,
295 * GNUNET_SYSERR on disconnect from service
296 * after the PUT message was transmitted
297 * (so we don't know if it was received or not)
300 record_dht_put(void *cls, int success)
302 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "put request transmitted\n");
307 update_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
310 * Function used to put all records successively into the DHT.
312 * @param cls the closure (NULL)
313 * @param key the public key of the authority (ours)
314 * @param expiration lifetime of the namestore entry
315 * @param name the name of the records
316 * @param rd_count the number of records in data
317 * @param rd the record data
318 * @param signature the signature for the record data
321 put_gns_record(void *cls,
322 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
323 struct GNUNET_TIME_Absolute expiration,
325 unsigned int rd_count,
326 const struct GNUNET_NAMESTORE_RecordData *rd,
327 const struct GNUNET_CRYPTO_RsaSignature *signature)
330 struct GNSNameRecordBlock *nrb;
331 struct GNUNET_CRYPTO_ShortHashCode name_hash;
332 struct GNUNET_CRYPTO_ShortHashCode zhash;
333 struct GNUNET_HashCode xor_hash;
334 struct GNUNET_HashCode name_hash_double;
335 struct GNUNET_HashCode zone_hash_double;
336 uint32_t rd_payload_length;
337 char* nrb_data = NULL;
339 int interval_adjustment = 1;
340 struct GNUNET_TIME_Relative next_put_interval;
347 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
348 "Zone iteration finished. Rescheduling zone iteration\n");
350 namestore_iter = NULL;
351 zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_start, NULL);
352 GNUNET_STATISTICS_update (statistics,
353 "Number of zone iterations", 1, GNUNET_NO);
355 last_num_public_records = num_public_records;
356 GNUNET_STATISTICS_set (statistics,
357 "Number of public records in DHT",
358 last_num_public_records,
363 namelen = strlen(name) + 1;
367 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
368 "No records given for name %s! Skipping...\n",
370 zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_next,
375 if (signature == NULL)
377 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
378 "No signature for %s record data provided! Skipping...\n",
380 zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_next,
386 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
387 "Putting records for %s into the DHT\n", name);
389 rd_payload_length = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
391 nrb = GNUNET_malloc(rd_payload_length + namelen
392 + sizeof(struct GNSNameRecordBlock));
394 nrb->signature = *signature;
396 nrb->public_key = *key;
398 nrb->rd_count = htonl(rd_count);
400 memcpy(&nrb[1], name, namelen);
402 nrb_data = (char*)&nrb[1];
405 rd_payload_length += sizeof(struct GNSNameRecordBlock) + namelen;
407 if (-1 == GNUNET_NAMESTORE_records_serialize (rd_count,
412 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
413 "Record serialization failed! Skipping...\n");
415 zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_next,
422 * calculate DHT key: H(name) xor H(pubkey)
424 GNUNET_CRYPTO_short_hash(key,
425 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
427 GNUNET_CRYPTO_short_hash(name, strlen(name), &name_hash);
428 GNUNET_CRYPTO_short_hash_double (&name_hash, &name_hash_double);
429 GNUNET_CRYPTO_short_hash_double (&zhash, &zone_hash_double);
430 GNUNET_CRYPTO_hash_xor(&zone_hash_double, &name_hash_double, &xor_hash);
432 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
433 "zone identity: %s\n", GNUNET_h2s (&zone_hash_double));
435 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
436 "putting %d records for %s under key: %s with size %d\n",
437 rd_count, name, GNUNET_h2s (&xor_hash), rd_payload_length);
439 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
440 "DHT req to %d\n", DHT_OPERATION_TIMEOUT.rel_value);
442 GNUNET_STATISTICS_update (statistics,
443 "Record bytes put into DHT", rd_payload_length, GNUNET_NO);
445 /* FIXME: keep return value to possibly cancel? */
446 GNUNET_DHT_put (dht_handle, &xor_hash,
447 DHT_GNS_REPLICATION_LEVEL,
449 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
453 DHT_OPERATION_TIMEOUT,
455 NULL); //cls for cont
457 num_public_records++;
459 if (num_public_records > last_num_public_records)
461 interval_adjustment = ceil ((double)num_public_records / (double)last_num_public_records);
462 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
463 "Last record count was lower than current record count... increasing.\n");
464 next_put_interval = GNUNET_TIME_relative_divide (zone_iteration_interval,
465 interval_adjustment);
469 next_put_interval = zone_iteration_interval;
471 GNUNET_STATISTICS_set (statistics,
472 "Current zone iteration interval [msec]",
473 next_put_interval.rel_value,
477 * Reschedule periodic put
479 zone_update_taskid = GNUNET_SCHEDULER_add_delayed (next_put_interval,
480 &update_zone_dht_next,
488 * Periodically iterate over our zone and store everything in dht
491 * @param tc task context
494 update_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
496 zone_update_taskid = GNUNET_SCHEDULER_NO_TASK;
498 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Scheduling DHT zone update!\n");
500 if (0 == last_num_public_records)
503 * If no records are known (startup) or none present
504 * we can safely set the interval to 1s
506 zone_iteration_interval = INITIAL_ZONE_ITERATION_INTERVAL;
507 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
508 "No records in db. Adjusted zone iteration interval to %llums\n",
509 zone_iteration_interval.rel_value);
510 GNUNET_STATISTICS_set (statistics,
511 "Current zone iteration interval [msec]",
512 zone_iteration_interval.rel_value,
517 zone_iteration_interval = GNUNET_TIME_relative_divide (record_put_interval,
518 last_num_public_records);
519 zone_iteration_interval = GNUNET_TIME_relative_max (MINIMUM_ZONE_ITERATION_INTERVAL,
520 zone_iteration_interval);
522 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
523 "Adjusted zone iteration interval to %llus!\n",
524 zone_iteration_interval.rel_value);
525 GNUNET_STATISTICS_set (statistics,
526 "Current zone iteration interval [msec]",
527 zone_iteration_interval.rel_value,
531 /* start counting again */
532 num_public_records = 0;
533 namestore_iter = GNUNET_NAMESTORE_zone_iteration_start (namestore_handle,
535 GNUNET_NAMESTORE_RF_AUTHORITY,
536 GNUNET_NAMESTORE_RF_PRIVATE,
541 /* END DHT ZONE PROPAGATION */
544 * Send shorten response back to client
546 * @param cls the closure containing a client shorten handle
547 * @param name the shortened name result or NULL if cannot be shortened
550 send_shorten_response(void* cls, const char* name)
552 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message with %s\n",
553 "SHORTEN_RESULT", name);
554 struct GNUNET_GNS_ClientShortenResultMessage *rmsg;
555 struct ClientShortenHandle *csh = (struct ClientShortenHandle *)cls;
562 GNUNET_STATISTICS_update (statistics,
563 "Name shorten results", 1, GNUNET_NO);
565 rmsg = GNUNET_malloc(sizeof(struct GNUNET_GNS_ClientShortenResultMessage)
568 rmsg->id = csh->unique_id;
569 rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_SHORTEN_RESULT);
571 htons(sizeof(struct GNUNET_GNS_ClientShortenResultMessage) +
574 strcpy((char*)&rmsg[1], name);
576 GNUNET_SERVER_notification_context_unicast (nc, csh->client,
577 (const struct GNUNET_MessageHeader *) rmsg,
579 GNUNET_SERVER_receive_done (csh->client, GNUNET_OK);
581 if (NULL != csh->namestore_task)
582 GNUNET_NAMESTORE_cancel (csh->namestore_task);
591 * Lookup the zone infos and shorten name
593 * @param cls the client shorten handle
594 * @param key key of the zone
595 * @param expiration expiration of record
596 * @param name name found or null if no result
597 * @param rd_count number of records found
598 * @param rd record data
603 process_shorten_in_private_zone_lookup (void *cls,
604 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
605 struct GNUNET_TIME_Absolute expiration,
607 unsigned int rd_count,
608 const struct GNUNET_NAMESTORE_RecordData *rd,
609 const struct GNUNET_CRYPTO_RsaSignature *signature)
611 struct ClientShortenHandle *csh = cls;
612 csh->namestore_task = NULL;
613 struct GNUNET_CRYPTO_ShortHashCode *szone = &csh->shorten_zone;
614 struct GNUNET_CRYPTO_ShortHashCode *pzone = &csh->private_zone;
616 if (0 == strcmp (csh->private_zone_id, ""))
621 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
622 "No shorten zone in private zone!\n");
624 strcpy (csh->shorten_zone_id, "");
631 GNUNET_assert (rd_count == 1);
633 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
634 "Shorten zone %s found in private zone %s\n",
635 name, csh->private_zone_id);
637 sprintf (csh->shorten_zone_id, "%s.%s", name, csh->private_zone_id);
640 GNUNET_CONTAINER_DLL_remove (csh_head, csh_tail, csh);
642 gns_resolver_shorten_name (&csh->root_zone,
646 csh->private_zone_id,
647 csh->shorten_zone_id,
648 &send_shorten_response, csh);
654 * Lookup the zone infos and shorten name
656 * @param cls the shorten handle
657 * @param key key of the zone
658 * @param expiration expiration of record
659 * @param name name found or null if no result
660 * @param rd_count number of records found
661 * @param rd record data
666 process_shorten_in_root_zone_lookup (void *cls,
667 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
668 struct GNUNET_TIME_Absolute expiration,
670 unsigned int rd_count,
671 const struct GNUNET_NAMESTORE_RecordData *rd,
672 const struct GNUNET_CRYPTO_RsaSignature *signature)
674 struct ClientShortenHandle *csh = cls;
675 csh->namestore_task = NULL;
676 struct GNUNET_CRYPTO_ShortHashCode *szone = &csh->shorten_zone;
677 struct GNUNET_CRYPTO_ShortHashCode *pzone = &csh->private_zone;
679 if (0 == strcmp (csh->private_zone_id, ""))
684 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
685 "No shorten zone in zone and no private zone!\n");
687 strcpy (csh->shorten_zone_id, "");
689 GNUNET_CONTAINER_DLL_remove (csh_head, csh_tail, csh);
692 gns_resolver_shorten_name (&csh->root_zone,
696 csh->private_zone_id,
697 csh->shorten_zone_id,
698 &send_shorten_response, csh);
702 GNUNET_assert (rd_count == 1);
704 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
705 "Private zone %s found in root zone\n", name);
707 strcpy (csh->private_zone_id, name);
709 csh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
712 &process_shorten_in_private_zone_lookup,
718 * Lookup the zone infos and shorten name
720 * @param cls the shorten handle
721 * @param key key of the zone
722 * @param expiration expiration of record
723 * @param name name found or null if no result
724 * @param rd_count number of records found
725 * @param rd record data
730 process_private_in_root_zone_lookup (void *cls,
731 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
732 struct GNUNET_TIME_Absolute expiration,
734 unsigned int rd_count,
735 const struct GNUNET_NAMESTORE_RecordData *rd,
736 const struct GNUNET_CRYPTO_RsaSignature *signature)
738 struct ClientShortenHandle *csh = cls;
739 csh->namestore_task = NULL;
743 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
744 "No private zone in root zone\n");
746 strcpy (csh->private_zone_id, "");
748 csh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
751 &process_shorten_in_root_zone_lookup,
756 GNUNET_assert (rd_count == 1);
758 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
759 "Private zone %s found in root zone\n", name);
761 strcpy (csh->private_zone_id, name);
763 csh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
766 &process_shorten_in_private_zone_lookup,
771 * Lookup the zone infos and shorten name
773 * @param csh the shorten handle
777 start_shorten_name (struct ClientShortenHandle *csh)
779 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
780 "Looking for private zone name in root zone\n");
782 csh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
785 &process_private_in_root_zone_lookup,
791 * Handle a shorten message from the api
793 * @param cls the closure
794 * @param client the client
795 * @param message the message
797 static void handle_shorten (void *cls,
798 struct GNUNET_SERVER_Client * client,
799 const struct GNUNET_MessageHeader * message)
801 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "SHORTEN");
804 struct ClientShortenHandle *csh;
805 char name[MAX_DNS_NAME_LENGTH];
806 char* nameptr = name;
808 if (ntohs (message->size) < sizeof (struct GNUNET_GNS_ClientShortenMessage))
811 GNUNET_SERVER_receive_done (client, GNUNET_OK);
816 struct GNUNET_GNS_ClientShortenMessage *sh_msg =
817 (struct GNUNET_GNS_ClientShortenMessage *) message;
819 msg_size = ntohs(message->size);
821 if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
824 GNUNET_SERVER_receive_done (client, GNUNET_OK);
828 csh = GNUNET_malloc(sizeof(struct ClientShortenHandle));
829 csh->client = client;
830 csh->unique_id = sh_msg->id;
832 GNUNET_CONTAINER_DLL_insert (csh_head, csh_tail, csh);
834 GNUNET_STRINGS_utf8_tolower((char*)&sh_msg[1], &nameptr);
836 if (strlen (name) < strlen(GNUNET_GNS_TLD)) {
837 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
838 "SHORTEN: %s is too short", name);
839 send_shorten_response(csh, name);
843 if (strlen (name) > MAX_DNS_NAME_LENGTH) {
844 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
845 "SHORTEN: %s is too long", name);
846 send_shorten_response(csh, name);
850 if (!is_gnunet_tld(name) && !is_zkey_tld(name))
852 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
853 "%s is not our domain. Returning\n", name);
854 send_shorten_response(csh, name);
858 csh->shorten_zone = sh_msg->shorten_zone;
859 csh->private_zone = sh_msg->private_zone;
861 strcpy (csh->name, name);
863 GNUNET_SERVER_notification_context_add (nc, client);
865 if (1 == ntohl(sh_msg->use_default_zone))
866 csh->root_zone = zone_hash; //Default zone
868 csh->root_zone = sh_msg->zone;
870 start_shorten_name (csh);
872 GNUNET_STATISTICS_update (statistics,
873 "Name shorten attempts", 1, GNUNET_NO);
879 * Send get authority response back to client
881 * @param cls the closure containing a client get auth handle
882 * @param name the shortened name result or NULL if cannot be shortened
885 send_get_auth_response(void *cls, const char* name)
887 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message with %s\n",
888 "GET_AUTH_RESULT", name);
889 struct GNUNET_GNS_ClientGetAuthResultMessage *rmsg;
890 struct ClientGetAuthHandle *cah = (struct ClientGetAuthHandle *)cls;
894 GNUNET_STATISTICS_update (statistics,
895 "Authorities resolved", 1, GNUNET_NO);
903 rmsg = GNUNET_malloc(sizeof(struct GNUNET_GNS_ClientGetAuthResultMessage)
906 rmsg->id = cah->unique_id;
907 rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_GET_AUTH_RESULT);
909 htons(sizeof(struct GNUNET_GNS_ClientGetAuthResultMessage) +
912 strcpy((char*)&rmsg[1], name);
914 GNUNET_SERVER_notification_context_unicast (nc, cah->client,
915 (const struct GNUNET_MessageHeader *) rmsg,
917 GNUNET_SERVER_receive_done (cah->client, GNUNET_OK);
920 GNUNET_free_non_null(cah->name);
929 * Handle a get authority message from the api
931 * @param cls the closure
932 * @param client the client
933 * @param message the message
935 static void handle_get_authority(void *cls,
936 struct GNUNET_SERVER_Client * client,
937 const struct GNUNET_MessageHeader * message)
939 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "GET_AUTH");
942 struct ClientGetAuthHandle *cah;
943 char name[MAX_DNS_NAME_LENGTH];
944 char* nameptr = name;
947 if (ntohs (message->size) < sizeof (struct GNUNET_GNS_ClientGetAuthMessage))
950 GNUNET_SERVER_receive_done (client, GNUNET_OK);
954 GNUNET_SERVER_notification_context_add (nc, client);
956 struct GNUNET_GNS_ClientGetAuthMessage *sh_msg =
957 (struct GNUNET_GNS_ClientGetAuthMessage *) message;
959 msg_size = ntohs(message->size);
961 if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
964 GNUNET_SERVER_receive_done (client, GNUNET_OK);
968 GNUNET_STRINGS_utf8_tolower((char*)&sh_msg[1], &nameptr);
971 cah = GNUNET_malloc(sizeof(struct ClientGetAuthHandle));
972 cah->client = client;
973 cah->unique_id = sh_msg->id;
975 if (strlen(name) < strlen(GNUNET_GNS_TLD))
977 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
978 "GET_AUTH: %s is too short. Returning\n", name);
980 send_get_auth_response(cah, name);
984 if (strlen (name) > MAX_DNS_NAME_LENGTH) {
985 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
986 "GET_AUTH: %s is too long", name);
988 send_get_auth_response(cah, name);
992 if (strcmp(name+strlen(name)-strlen(GNUNET_GNS_TLD),
993 GNUNET_GNS_TLD) != 0)
995 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
996 "GET_AUTH: %s is not our domain. Returning\n", name);
998 send_get_auth_response(cah, name);
1002 if (strcmp(name, GNUNET_GNS_TLD) == 0)
1004 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1005 "GET_AUTH: %s is us. Returning\n", name);
1007 send_get_auth_response(cah, name);
1011 cah->name = GNUNET_malloc(strlen(name)
1012 - strlen(GNUNET_GNS_TLD) + 1);
1013 memset(cah->name, 0,
1014 strlen(name)-strlen(GNUNET_GNS_TLD) + 1);
1015 memcpy(cah->name, name,
1016 strlen(name)-strlen(GNUNET_GNS_TLD));
1018 /* Start delegation resolution in our namestore */
1019 gns_resolver_get_authority(zone_hash, zone_hash, name, &send_get_auth_response, cah);
1021 GNUNET_STATISTICS_update (statistics,
1022 "Authority lookup attempts", 1, GNUNET_NO);
1028 * Reply to client with the result from our lookup.
1030 * @param cls the closure (our client lookup handle)
1031 * @param rd_count the number of records
1032 * @param rd the record data
1035 send_lookup_response(void* cls,
1037 const struct GNUNET_NAMESTORE_RecordData *rd)
1039 struct ClientLookupHandle* clh = (struct ClientLookupHandle*)cls;
1040 struct GNUNET_GNS_ClientLookupResultMessage *rmsg;
1043 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message with %d results\n",
1044 "LOOKUP_RESULT", rd_count);
1046 len = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
1047 rmsg = GNUNET_malloc(len+sizeof(struct GNUNET_GNS_ClientLookupResultMessage));
1049 rmsg->id = clh->unique_id;
1050 rmsg->rd_count = htonl(rd_count);
1051 rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT);
1053 htons(len+sizeof(struct GNUNET_GNS_ClientLookupResultMessage));
1055 GNUNET_NAMESTORE_records_serialize (rd_count, rd, len, (char*)&rmsg[1]);
1057 GNUNET_SERVER_notification_context_unicast (nc, clh->client,
1058 (const struct GNUNET_MessageHeader *) rmsg,
1060 GNUNET_SERVER_receive_done (clh->client, GNUNET_OK);
1063 GNUNET_free(clh->name);
1065 if (NULL != clh->shorten_key)
1066 GNUNET_free(clh->shorten_key);
1070 GNUNET_STATISTICS_update (statistics,
1071 "Completed lookups", 1, GNUNET_NO);
1075 GNUNET_STATISTICS_update (statistics,
1076 "Records resolved", rd_count, GNUNET_NO);
1083 * Handle lookup requests from client
1085 * @param cls the closure
1086 * @param client the client
1087 * @param message the message
1090 handle_lookup(void *cls,
1091 struct GNUNET_SERVER_Client * client,
1092 const struct GNUNET_MessageHeader * message)
1094 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "LOOKUP");
1096 size_t msg_size = 0;
1098 char name[MAX_DNS_NAME_LENGTH];
1099 struct ClientLookupHandle *clh;
1100 char* nameptr = name;
1102 struct GNUNET_CRYPTO_RsaPrivateKey *key;
1103 struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded *pkey;
1106 if (ntohs (message->size) < sizeof (struct GNUNET_GNS_ClientLookupMessage))
1108 GNUNET_break_op (0);
1109 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1113 GNUNET_SERVER_notification_context_add (nc, client);
1115 struct GNUNET_GNS_ClientLookupMessage *sh_msg =
1116 (struct GNUNET_GNS_ClientLookupMessage *) message;
1118 msg_size = ntohs(message->size);
1120 if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
1122 GNUNET_break_op (0);
1123 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1127 if (1 == ntohl(sh_msg->have_key))
1129 pkey = (struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded *)&sh_msg[1];
1130 tmp_pkey = (char*)&sh_msg[1];
1131 key = GNUNET_CRYPTO_rsa_decode_key (tmp_pkey, ntohs(pkey->len));
1132 GNUNET_STRINGS_utf8_tolower(&tmp_pkey[ntohs(pkey->len)], &nameptr);
1137 GNUNET_STRINGS_utf8_tolower((char*)&sh_msg[1], &nameptr);
1140 namelen = strlen(name)+1;
1141 clh = GNUNET_malloc (sizeof (struct ClientLookupHandle));
1142 memset (clh, 0, sizeof (struct ClientLookupHandle));
1143 clh->client = client;
1144 clh->name = GNUNET_malloc(namelen);
1145 strcpy(clh->name, name);
1146 clh->unique_id = sh_msg->id;
1147 clh->type = ntohl(sh_msg->type);
1148 clh->shorten_key = key;
1150 only_cached = ntohl(sh_msg->only_cached);
1152 if (strlen (name) > MAX_DNS_NAME_LENGTH) {
1153 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1154 "LOOKUP: %s is too long", name);
1156 send_lookup_response(clh, 0, NULL);
1160 if ((clh->type == GNUNET_GNS_RECORD_A) &&
1161 (GNUNET_OK != v4_enabled))
1163 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1164 "LOOKUP: Query for A record but AF_INET not supported!");
1166 send_lookup_response(clh, 0, NULL);
1170 if ((clh->type == GNUNET_GNS_RECORD_AAAA) &&
1171 (GNUNET_OK != v6_enabled))
1173 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1174 "LOOKUP: Query for AAAA record but AF_INET6 not supported!");
1176 send_lookup_response(clh, 0, NULL);
1180 if (1 == ntohl(sh_msg->use_default_zone))
1181 clh->zone = zone_hash; //Default zone
1183 clh->zone = sh_msg->zone;
1185 if (GNUNET_YES == auto_import_pkey)
1187 gns_resolver_lookup_record (clh->zone, clh->zone, clh->type, clh->name,
1189 default_lookup_timeout,
1191 &send_lookup_response, clh);
1195 gns_resolver_lookup_record (clh->zone, clh->zone, clh->type, name,
1197 default_lookup_timeout,
1199 &send_lookup_response, clh);
1202 GNUNET_STATISTICS_update (statistics,
1203 "Record lookup attempts", 1, GNUNET_NO);
1207 * Test if the given AF is supported by this system.
1210 * @return GNUNET_OK if the AF is supported
1217 s = socket (af, SOCK_STREAM, 0);
1220 if (EAFNOSUPPORT == errno)
1222 fprintf (stderr, "Failed to create test socket: %s\n", STRERROR (errno));
1223 return GNUNET_SYSERR;
1234 * Process GNS requests.
1236 * @param cls closure)
1237 * @param server the initialized server
1238 * @param c configuration to use
1241 run (void *cls, struct GNUNET_SERVER_Handle *server,
1242 const struct GNUNET_CONFIGURATION_Handle *c)
1245 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Initializing GNS\n");
1248 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
1249 unsigned long long max_parallel_bg_queries = 0;
1250 unsigned long long default_lookup_timeout_secs = 0;
1251 int ignore_pending = GNUNET_NO;
1253 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1254 {&handle_shorten, NULL, GNUNET_MESSAGE_TYPE_GNS_SHORTEN, 0},
1255 {&handle_lookup, NULL, GNUNET_MESSAGE_TYPE_GNS_LOOKUP, 0},
1256 {&handle_get_authority, NULL, GNUNET_MESSAGE_TYPE_GNS_GET_AUTH, 0}
1261 v6_enabled = test_af (AF_INET6);
1262 v4_enabled = test_af (AF_INET);
1264 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (c, "gns",
1265 "ZONEKEY", &keyfile))
1267 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1268 "No private key for root zone specified!\n");
1269 GNUNET_SCHEDULER_shutdown ();
1273 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1274 "Using keyfile %s for root zone.\n", keyfile);
1276 zone_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
1277 GNUNET_CRYPTO_rsa_key_get_public (zone_key, &pkey);
1279 GNUNET_CRYPTO_short_hash(&pkey,
1280 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1282 GNUNET_free(keyfile);
1285 * handle to our local namestore
1287 namestore_handle = GNUNET_NAMESTORE_connect(c);
1289 if (NULL == namestore_handle)
1291 //FIXME do error handling;
1292 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1293 "Failed to connect to the namestore!\n");
1294 GNUNET_SCHEDULER_shutdown ();
1300 auto_import_pkey = GNUNET_NO;
1303 GNUNET_CONFIGURATION_get_value_yesno (c, "gns",
1304 "AUTO_IMPORT_PKEY"))
1306 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1307 "Automatic PKEY import is enabled.\n");
1308 auto_import_pkey = GNUNET_YES;
1312 zone_iteration_interval = INITIAL_ZONE_ITERATION_INTERVAL; // yuck
1314 record_put_interval = DEFAULT_RECORD_PUT_INTERVAL;
1317 GNUNET_CONFIGURATION_get_value_time (c, "gns",
1318 "RECORD_PUT_INTERVAL",
1319 &record_put_interval))
1321 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1322 "Record put interval: %llu\n",
1323 record_put_interval);
1327 GNUNET_CONFIGURATION_get_value_number (c, "gns",
1328 "MAX_PARALLEL_BACKGROUND_QUERIES",
1329 &max_parallel_bg_queries))
1331 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1332 "Number of allowed parallel background queries: %llu\n",
1333 max_parallel_bg_queries);
1337 GNUNET_CONFIGURATION_get_value_yesno (c, "gns",
1338 "AUTO_IMPORT_CONFIRMATION_REQ"))
1340 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1341 "Auto import requires user confirmation\n");
1342 ignore_pending = GNUNET_YES;
1346 GNUNET_CONFIGURATION_get_value_number(c, "gns",
1347 "DEFAULT_LOOKUP_TIMEOUT",
1348 &default_lookup_timeout_secs))
1350 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1351 "Default lookup timeout: %llus\n", default_lookup_timeout_secs);
1352 default_lookup_timeout = GNUNET_TIME_relative_multiply(
1353 GNUNET_TIME_UNIT_SECONDS,
1354 default_lookup_timeout_secs);
1360 dht_handle = GNUNET_DHT_connect(c,
1361 //max_parallel_bg_queries); //FIXME get ht_len from cfg
1364 if (NULL == dht_handle)
1366 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not connect to DHT!\n");
1369 if (gns_resolver_init(namestore_handle, dht_handle, zone_hash, c,
1370 max_parallel_bg_queries,
1374 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1375 "Unable to initialize resolver!\n");
1376 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
1381 GNUNET_CONFIGURATION_get_value_yesno (c, "gns", "HIJACK_DNS"))
1383 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1384 "DNS hijacking enabled... connecting to service.\n");
1386 if (gns_interceptor_init(zone_hash, zone_key, c) == GNUNET_SYSERR)
1388 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1389 "Failed to enable the dns interceptor!\n");
1394 * Schedule periodic put
1396 * We have roughly an hour for all records;
1398 zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_start, NULL);
1400 GNUNET_SERVER_add_handlers (server, handlers);
1403 //GNUNET_SERVER_disconnect_notify (server,
1404 // &client_disconnect_notification,
1407 statistics = GNUNET_STATISTICS_create ("gns", c);
1409 nc = GNUNET_SERVER_notification_context_create (server, 1);
1411 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
1417 * The main function for the GNS service.
1419 * @param argc number of arguments from the command line
1420 * @param argv command line arguments
1421 * @return 0 ok, 1 on error
1424 main (int argc, char *const *argv)
1430 GNUNET_SERVICE_run (argc, argv, "gns", GNUNET_SERVICE_OPTION_NONE, &run,
1435 /* end of gnunet-service-gns.c */