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,
487 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
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);
845 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
846 "SHORTEN: Converted %s to %s\n", (char*)&sh_msg[1], nameptr);
847 GNUNET_SERVER_notification_context_add (nc, client);
849 if (strlen (name) < strlen(GNUNET_GNS_TLD)) {
850 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
851 "SHORTEN: %s is too short\n", name);
852 GNUNET_CONTAINER_DLL_remove (csh_head, csh_tail, csh);
853 send_shorten_response(csh, name);
857 if (strlen (name) > MAX_DNS_NAME_LENGTH) {
858 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
859 "SHORTEN: %s is too long\n", name);
860 GNUNET_CONTAINER_DLL_remove (csh_head, csh_tail, csh);
861 send_shorten_response(csh, name);
865 if (!is_gnunet_tld(name) && !is_zkey_tld(name))
867 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
868 "%s is not our domain. Returning\n", name);
869 GNUNET_CONTAINER_DLL_remove (csh_head, csh_tail, csh);
870 send_shorten_response(csh, name);
874 csh->shorten_zone = sh_msg->shorten_zone;
875 csh->private_zone = sh_msg->private_zone;
877 strcpy (csh->name, name);
880 if (1 == ntohl(sh_msg->use_default_zone))
881 csh->root_zone = zone_hash; //Default zone
883 csh->root_zone = sh_msg->zone;
885 start_shorten_name (csh);
887 GNUNET_STATISTICS_update (statistics,
888 "Name shorten attempts", 1, GNUNET_NO);
894 * Send get authority response back to client
896 * @param cls the closure containing a client get auth handle
897 * @param name the shortened name result or NULL if cannot be shortened
900 send_get_auth_response(void *cls, const char* name)
902 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message with %s\n",
903 "GET_AUTH_RESULT", name);
904 struct GNUNET_GNS_ClientGetAuthResultMessage *rmsg;
905 struct ClientGetAuthHandle *cah = (struct ClientGetAuthHandle *)cls;
909 GNUNET_STATISTICS_update (statistics,
910 "Authorities resolved", 1, GNUNET_NO);
918 rmsg = GNUNET_malloc(sizeof(struct GNUNET_GNS_ClientGetAuthResultMessage)
921 rmsg->id = cah->unique_id;
922 rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_GET_AUTH_RESULT);
924 htons(sizeof(struct GNUNET_GNS_ClientGetAuthResultMessage) +
927 strcpy((char*)&rmsg[1], name);
929 GNUNET_SERVER_notification_context_unicast (nc, cah->client,
930 (const struct GNUNET_MessageHeader *) rmsg,
932 GNUNET_SERVER_receive_done (cah->client, GNUNET_OK);
935 GNUNET_free_non_null(cah->name);
944 * Handle a get authority message from the api
946 * @param cls the closure
947 * @param client the client
948 * @param message the message
950 static void handle_get_authority(void *cls,
951 struct GNUNET_SERVER_Client * client,
952 const struct GNUNET_MessageHeader * message)
954 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "GET_AUTH");
957 struct ClientGetAuthHandle *cah;
958 char name[MAX_DNS_NAME_LENGTH];
959 char* nameptr = name;
962 if (ntohs (message->size) < sizeof (struct GNUNET_GNS_ClientGetAuthMessage))
965 GNUNET_SERVER_receive_done (client, GNUNET_OK);
969 GNUNET_SERVER_notification_context_add (nc, client);
971 struct GNUNET_GNS_ClientGetAuthMessage *sh_msg =
972 (struct GNUNET_GNS_ClientGetAuthMessage *) message;
974 msg_size = ntohs(message->size);
976 if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
979 GNUNET_SERVER_receive_done (client, GNUNET_OK);
983 GNUNET_STRINGS_utf8_tolower((char*)&sh_msg[1], &nameptr);
986 cah = GNUNET_malloc(sizeof(struct ClientGetAuthHandle));
987 cah->client = client;
988 cah->unique_id = sh_msg->id;
990 if (strlen(name) < strlen(GNUNET_GNS_TLD))
992 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
993 "GET_AUTH: %s is too short. Returning\n", name);
995 send_get_auth_response(cah, name);
999 if (strlen (name) > MAX_DNS_NAME_LENGTH) {
1000 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1001 "GET_AUTH: %s is too long", name);
1003 send_get_auth_response(cah, name);
1007 if (strcmp(name+strlen(name)-strlen(GNUNET_GNS_TLD),
1008 GNUNET_GNS_TLD) != 0)
1010 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1011 "GET_AUTH: %s is not our domain. Returning\n", name);
1013 send_get_auth_response(cah, name);
1017 if (strcmp(name, GNUNET_GNS_TLD) == 0)
1019 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1020 "GET_AUTH: %s is us. Returning\n", name);
1022 send_get_auth_response(cah, name);
1026 cah->name = GNUNET_malloc(strlen(name)
1027 - strlen(GNUNET_GNS_TLD) + 1);
1028 memset(cah->name, 0,
1029 strlen(name)-strlen(GNUNET_GNS_TLD) + 1);
1030 memcpy(cah->name, name,
1031 strlen(name)-strlen(GNUNET_GNS_TLD));
1033 /* Start delegation resolution in our namestore */
1034 gns_resolver_get_authority(zone_hash, zone_hash, name, &send_get_auth_response, cah);
1036 GNUNET_STATISTICS_update (statistics,
1037 "Authority lookup attempts", 1, GNUNET_NO);
1043 * Reply to client with the result from our lookup.
1045 * @param cls the closure (our client lookup handle)
1046 * @param rd_count the number of records
1047 * @param rd the record data
1050 send_lookup_response(void* cls,
1052 const struct GNUNET_NAMESTORE_RecordData *rd)
1054 struct ClientLookupHandle* clh = (struct ClientLookupHandle*)cls;
1055 struct GNUNET_GNS_ClientLookupResultMessage *rmsg;
1058 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message with %d results\n",
1059 "LOOKUP_RESULT", rd_count);
1061 len = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
1062 rmsg = GNUNET_malloc(len+sizeof(struct GNUNET_GNS_ClientLookupResultMessage));
1064 rmsg->id = clh->unique_id;
1065 rmsg->rd_count = htonl(rd_count);
1066 rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT);
1068 htons(len+sizeof(struct GNUNET_GNS_ClientLookupResultMessage));
1070 GNUNET_NAMESTORE_records_serialize (rd_count, rd, len, (char*)&rmsg[1]);
1072 GNUNET_SERVER_notification_context_unicast (nc, clh->client,
1073 (const struct GNUNET_MessageHeader *) rmsg,
1075 GNUNET_SERVER_receive_done (clh->client, GNUNET_OK);
1078 GNUNET_free(clh->name);
1080 if (NULL != clh->shorten_key)
1081 GNUNET_free(clh->shorten_key);
1085 GNUNET_STATISTICS_update (statistics,
1086 "Completed lookups", 1, GNUNET_NO);
1090 GNUNET_STATISTICS_update (statistics,
1091 "Records resolved", rd_count, GNUNET_NO);
1098 * Handle lookup requests from client
1100 * @param cls the closure
1101 * @param client the client
1102 * @param message the message
1105 handle_lookup(void *cls,
1106 struct GNUNET_SERVER_Client * client,
1107 const struct GNUNET_MessageHeader * message)
1109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "LOOKUP");
1111 size_t msg_size = 0;
1113 char name[MAX_DNS_NAME_LENGTH];
1114 struct ClientLookupHandle *clh;
1115 char* nameptr = name;
1117 struct GNUNET_CRYPTO_RsaPrivateKey *key;
1118 struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded *pkey;
1121 if (ntohs (message->size) < sizeof (struct GNUNET_GNS_ClientLookupMessage))
1123 GNUNET_break_op (0);
1124 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1128 GNUNET_SERVER_notification_context_add (nc, client);
1130 struct GNUNET_GNS_ClientLookupMessage *sh_msg =
1131 (struct GNUNET_GNS_ClientLookupMessage *) message;
1133 msg_size = ntohs(message->size);
1135 if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
1137 GNUNET_break_op (0);
1138 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1142 if (1 == ntohl(sh_msg->have_key))
1144 pkey = (struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded *)&sh_msg[1];
1145 tmp_pkey = (char*)&sh_msg[1];
1146 key = GNUNET_CRYPTO_rsa_decode_key (tmp_pkey, ntohs(pkey->len));
1147 GNUNET_STRINGS_utf8_tolower(&tmp_pkey[ntohs(pkey->len)], &nameptr);
1152 GNUNET_STRINGS_utf8_tolower((char*)&sh_msg[1], &nameptr);
1155 namelen = strlen(name)+1;
1156 clh = GNUNET_malloc (sizeof (struct ClientLookupHandle));
1157 memset (clh, 0, sizeof (struct ClientLookupHandle));
1158 clh->client = client;
1159 clh->name = GNUNET_malloc(namelen);
1160 strcpy(clh->name, name);
1161 clh->unique_id = sh_msg->id;
1162 clh->type = ntohl(sh_msg->type);
1163 clh->shorten_key = key;
1165 only_cached = ntohl(sh_msg->only_cached);
1167 if (strlen (name) > MAX_DNS_NAME_LENGTH) {
1168 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1169 "LOOKUP: %s is too long", name);
1171 send_lookup_response(clh, 0, NULL);
1175 if ((clh->type == GNUNET_GNS_RECORD_A) &&
1176 (GNUNET_OK != v4_enabled))
1178 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1179 "LOOKUP: Query for A record but AF_INET not supported!");
1181 send_lookup_response(clh, 0, NULL);
1185 if ((clh->type == GNUNET_GNS_RECORD_AAAA) &&
1186 (GNUNET_OK != v6_enabled))
1188 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1189 "LOOKUP: Query for AAAA record but AF_INET6 not supported!");
1191 send_lookup_response(clh, 0, NULL);
1195 if (1 == ntohl(sh_msg->use_default_zone))
1196 clh->zone = zone_hash; //Default zone
1198 clh->zone = sh_msg->zone;
1200 if (GNUNET_YES == auto_import_pkey)
1202 gns_resolver_lookup_record (clh->zone, clh->zone, clh->type, clh->name,
1204 default_lookup_timeout,
1206 &send_lookup_response, clh);
1210 gns_resolver_lookup_record (clh->zone, clh->zone, clh->type, name,
1212 default_lookup_timeout,
1214 &send_lookup_response, clh);
1217 GNUNET_STATISTICS_update (statistics,
1218 "Record lookup attempts", 1, GNUNET_NO);
1222 * Test if the given AF is supported by this system.
1225 * @return GNUNET_OK if the AF is supported
1232 s = socket (af, SOCK_STREAM, 0);
1235 if (EAFNOSUPPORT == errno)
1237 fprintf (stderr, "Failed to create test socket: %s\n", STRERROR (errno));
1238 return GNUNET_SYSERR;
1249 * Process GNS requests.
1251 * @param cls closure)
1252 * @param server the initialized server
1253 * @param c configuration to use
1256 run (void *cls, struct GNUNET_SERVER_Handle *server,
1257 const struct GNUNET_CONFIGURATION_Handle *c)
1260 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Initializing GNS\n");
1263 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
1264 unsigned long long max_parallel_bg_queries = 0;
1265 unsigned long long default_lookup_timeout_secs = 0;
1266 int ignore_pending = GNUNET_NO;
1268 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1269 {&handle_shorten, NULL, GNUNET_MESSAGE_TYPE_GNS_SHORTEN, 0},
1270 {&handle_lookup, NULL, GNUNET_MESSAGE_TYPE_GNS_LOOKUP, 0},
1271 {&handle_get_authority, NULL, GNUNET_MESSAGE_TYPE_GNS_GET_AUTH, 0}
1276 v6_enabled = test_af (AF_INET6);
1277 v4_enabled = test_af (AF_INET);
1279 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (c, "gns",
1280 "ZONEKEY", &keyfile))
1282 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1283 "No private key for root zone specified!\n");
1284 GNUNET_SCHEDULER_shutdown ();
1288 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1289 "Using keyfile %s for root zone.\n", keyfile);
1291 zone_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
1292 GNUNET_CRYPTO_rsa_key_get_public (zone_key, &pkey);
1294 GNUNET_CRYPTO_short_hash(&pkey,
1295 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1297 GNUNET_free(keyfile);
1300 * handle to our local namestore
1302 namestore_handle = GNUNET_NAMESTORE_connect(c);
1304 if (NULL == namestore_handle)
1306 //FIXME do error handling;
1307 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1308 "Failed to connect to the namestore!\n");
1309 GNUNET_SCHEDULER_shutdown ();
1315 auto_import_pkey = GNUNET_NO;
1318 GNUNET_CONFIGURATION_get_value_yesno (c, "gns",
1319 "AUTO_IMPORT_PKEY"))
1321 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1322 "Automatic PKEY import is enabled.\n");
1323 auto_import_pkey = GNUNET_YES;
1327 zone_iteration_interval = INITIAL_ZONE_ITERATION_INTERVAL;
1329 record_put_interval = DEFAULT_RECORD_PUT_INTERVAL;
1332 GNUNET_CONFIGURATION_get_value_time (c, "gns",
1333 "RECORD_PUT_INTERVAL",
1334 &record_put_interval))
1336 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1337 "Record put interval: %llu\n",
1338 record_put_interval);
1342 GNUNET_CONFIGURATION_get_value_number (c, "gns",
1343 "MAX_PARALLEL_BACKGROUND_QUERIES",
1344 &max_parallel_bg_queries))
1346 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1347 "Number of allowed parallel background queries: %llu\n",
1348 max_parallel_bg_queries);
1352 GNUNET_CONFIGURATION_get_value_yesno (c, "gns",
1353 "AUTO_IMPORT_CONFIRMATION_REQ"))
1355 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1356 "Auto import requires user confirmation\n");
1357 ignore_pending = GNUNET_YES;
1361 GNUNET_CONFIGURATION_get_value_number(c, "gns",
1362 "DEFAULT_LOOKUP_TIMEOUT",
1363 &default_lookup_timeout_secs))
1365 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1366 "Default lookup timeout: %llus\n", default_lookup_timeout_secs);
1367 default_lookup_timeout = GNUNET_TIME_relative_multiply(
1368 GNUNET_TIME_UNIT_SECONDS,
1369 default_lookup_timeout_secs);
1375 dht_handle = GNUNET_DHT_connect(c,
1376 //max_parallel_bg_queries); //FIXME get ht_len from cfg
1379 if (NULL == dht_handle)
1381 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not connect to DHT!\n");
1384 if (gns_resolver_init(namestore_handle, dht_handle, zone_hash, c,
1385 max_parallel_bg_queries,
1389 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1390 "Unable to initialize resolver!\n");
1391 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
1396 GNUNET_CONFIGURATION_get_value_yesno (c, "gns", "HIJACK_DNS"))
1398 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1399 "DNS hijacking enabled... connecting to service.\n");
1401 if (gns_interceptor_init(zone_hash, zone_key, c) == GNUNET_SYSERR)
1403 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1404 "Failed to enable the dns interceptor!\n");
1409 * Schedule periodic put
1411 * We have roughly an hour for all records;
1413 first_zone_iteration = GNUNET_YES;
1414 zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_start, NULL);
1416 GNUNET_SERVER_add_handlers (server, handlers);
1419 //GNUNET_SERVER_disconnect_notify (server,
1420 // &client_disconnect_notification,
1423 statistics = GNUNET_STATISTICS_create ("gns", c);
1425 nc = GNUNET_SERVER_notification_context_create (server, 1);
1427 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
1433 * The main function for the GNS service.
1435 * @param argc number of arguments from the command line
1436 * @param argv command line arguments
1437 * @return 0 ok, 1 on error
1440 main (int argc, char *const *argv)
1446 GNUNET_SERVICE_run (argc, argv, "gns", GNUNET_SERVICE_OPTION_NONE, &run,
1451 /* end of gnunet-service-gns.c */