2 This file is part of GNUnet.
3 (C) 2009, 2010, 2011 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
23 * @file gns/gnunet-service-gns.c
24 * @brief GNUnet GNS service
25 * @author Martin Schanzenbach
28 #include "gnunet_util_lib.h"
29 #include "gnunet_transport_service.h"
30 #include "gnunet_dns_service.h"
31 #include "gnunet_dnsparser_lib.h"
32 #include "gnunet_dht_service.h"
33 #include "gnunet_namestore_service.h"
34 #include "gnunet_gns_service.h"
35 #include "gnunet_statistics_service.h"
36 #include "block_gns.h"
38 #include "gnunet-service-gns_resolver.h"
39 #include "gnunet-service-gns_interceptor.h"
42 /* FIXME move to proper header in include */
43 #define GNUNET_MESSAGE_TYPE_GNS_LOOKUP 23
44 #define GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT 24
45 #define GNUNET_MESSAGE_TYPE_GNS_SHORTEN 25
46 #define GNUNET_MESSAGE_TYPE_GNS_SHORTEN_RESULT 26
47 #define GNUNET_MESSAGE_TYPE_GNS_GET_AUTH 27
48 #define GNUNET_MESSAGE_TYPE_GNS_GET_AUTH_RESULT 28
51 #define INITIAL_ZONE_ITERATION_INTERVAL GNUNET_TIME_UNIT_MILLISECONDS
52 #define MINIMUM_ZONE_ITERATION_INTERVAL GNUNET_TIME_UNIT_SECONDS
53 #define DEFAULT_RECORD_PUT_INTERVAL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 4)
54 #define LATE_ITERATION_SPEEDUP_FACTOR 2
57 * Handle to a shorten operation from api
59 struct ClientShortenHandle
63 struct ClientShortenHandle *next;
66 struct ClientShortenHandle *prev;
68 /* the requesting client that */
69 struct GNUNET_SERVER_Client *client;
75 enum GNUNET_GNS_RecordType type;
78 char name[MAX_DNS_NAME_LENGTH];
80 /* name of private zone (relative to root) */
81 char private_zone_id[MAX_DNS_NAME_LENGTH];
83 /* name of shorten zone (relative to root) */
84 char shorten_zone_id[MAX_DNS_NAME_LENGTH];
87 struct GNUNET_CRYPTO_ShortHashCode root_zone;
90 struct GNUNET_CRYPTO_ShortHashCode private_zone;
93 struct GNUNET_CRYPTO_ShortHashCode shorten_zone;
95 /* Namestore lookup task */
96 struct GNUNET_NAMESTORE_QueueEntry *namestore_task;
102 * Handle to a get auhtority operation from api
104 struct ClientGetAuthHandle
106 /* the requesting client that */
107 struct GNUNET_SERVER_Client *client;
112 /* name to lookup authority */
119 * Handle to a lookup operation from api
121 struct ClientLookupHandle
124 /* the requesting client that */
125 struct GNUNET_SERVER_Client *client;
127 /* The zone we look up in */
128 struct GNUNET_CRYPTO_ShortHashCode zone;
130 /* Do we only want to lookup from local cache? */
137 enum GNUNET_GNS_RecordType type;
139 /* optional zone private key used for shorten */
140 struct GNUNET_CRYPTO_RsaPrivateKey *shorten_key;
142 /* the name to look up */
143 char* name; //Needed?
147 * Our handle to the DHT
149 static struct GNUNET_DHT_Handle *dht_handle;
152 * Our zone's private key
154 struct GNUNET_CRYPTO_RsaPrivateKey *zone_key;
157 * Our handle to the namestore service
158 * FIXME maybe need a second handle for iteration
160 struct GNUNET_NAMESTORE_Handle *namestore_handle;
163 * Handle to iterate over our authoritative zone in namestore
165 struct GNUNET_NAMESTORE_ZoneIterator *namestore_iter;
168 * The configuration the GNS service is running with
170 const struct GNUNET_CONFIGURATION_Handle *GNS_cfg;
173 * Our notification context.
175 static struct GNUNET_SERVER_NotificationContext *nc;
180 struct GNUNET_CRYPTO_ShortHashCode zone_hash;
183 * Useful for zone update for DHT put
185 static unsigned long long num_public_records;
188 * Last seen record count
190 static unsigned long long last_num_public_records;
192 static struct GNUNET_TIME_Relative zone_iteration_interval;
194 /* dht update interval FIXME define? */
195 static struct GNUNET_TIME_Relative record_put_interval;
197 /* zone update task */
198 GNUNET_SCHEDULER_TaskIdentifier zone_update_taskid = GNUNET_SCHEDULER_NO_TASK;
200 /* automatic pkey import for name shortening */
201 static int auto_import_pkey;
203 /* first zone iteration is specia */
204 static int first_zone_iteration;
207 static struct GNUNET_TIME_Relative default_lookup_timeout;
210 static int v6_enabled;
213 static int v4_enabled;
215 /* Shorten DLL for cancelling NS requests */
216 static struct ClientShortenHandle *csh_head;
218 /* Shorten DLL for cancelling NS requests */
219 static struct ClientShortenHandle *csh_tail;
221 /* Statistics handle */
222 static struct GNUNET_STATISTICS_Handle *statistics;
225 * Send shorten response back to client
227 * @param cls the closure containing a client shorten handle
228 * @param name the shortened name result or NULL if cannot be shortened
231 send_shorten_response(void* cls, const char* name);
238 on_resolver_cleanup(void)
240 if (NULL != statistics)
241 GNUNET_STATISTICS_destroy (statistics, GNUNET_NO);
243 if (NULL != namestore_iter)
244 GNUNET_NAMESTORE_zone_iteration_stop (namestore_iter);
245 GNUNET_NAMESTORE_disconnect(namestore_handle);
246 GNUNET_DHT_disconnect(dht_handle);
250 * Task run during shutdown.
256 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
258 struct ClientShortenHandle *csh_tmp = csh_head;
260 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
263 while (csh_tmp != NULL)
265 GNUNET_CONTAINER_DLL_remove (csh_head, csh_tail, csh_tmp);
266 send_shorten_response (csh_tmp, csh_tmp->name);
269 GNUNET_SERVER_notification_context_destroy (nc);
271 gns_interceptor_stop();
272 gns_resolver_cleanup(&on_resolver_cleanup);
273 /* Kill zone task for it may make the scheduler hang */
274 if (zone_update_taskid != GNUNET_SCHEDULER_NO_TASK)
275 GNUNET_SCHEDULER_cancel(zone_update_taskid);
280 * Method called periodicattluy that triggers
281 * iteration over root zone
284 * @param tc task context
287 update_zone_dht_next(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
289 zone_update_taskid = GNUNET_SCHEDULER_NO_TASK;
290 GNUNET_NAMESTORE_zone_iterator_next (namestore_iter);
294 * Continuation for DHT put
297 * @param success GNUNET_OK if the PUT was transmitted,
298 * GNUNET_NO on timeout,
299 * GNUNET_SYSERR on disconnect from service
300 * after the PUT message was transmitted
301 * (so we don't know if it was received or not)
304 record_dht_put(void *cls, int success)
306 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "put request transmitted\n");
311 update_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
314 * Function used to put all records successively into the DHT.
316 * @param cls the closure (NULL)
317 * @param key the public key of the authority (ours)
318 * @param expiration lifetime of the namestore entry
319 * @param name the name of the records
320 * @param rd_count the number of records in data
321 * @param rd the record data
322 * @param signature the signature for the record data
325 put_gns_record(void *cls,
326 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
327 struct GNUNET_TIME_Absolute expiration,
329 unsigned int rd_count,
330 const struct GNUNET_NAMESTORE_RecordData *rd,
331 const struct GNUNET_CRYPTO_RsaSignature *signature)
334 struct GNSNameRecordBlock *nrb;
335 struct GNUNET_CRYPTO_ShortHashCode name_hash;
336 struct GNUNET_CRYPTO_ShortHashCode zhash;
337 struct GNUNET_HashCode xor_hash;
338 struct GNUNET_HashCode name_hash_double;
339 struct GNUNET_HashCode zone_hash_double;
340 uint32_t rd_payload_length;
341 char* nrb_data = NULL;
343 struct GNUNET_TIME_Relative next_put_interval;
350 first_zone_iteration = GNUNET_NO;
351 if (0 == num_public_records)
354 * If no records are known (startup) or none present
355 * we can safely set the interval to the value for a single
358 zone_iteration_interval = GNUNET_TIME_relative_divide (record_put_interval,
361 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
362 "No records in db.\n");
366 zone_iteration_interval = GNUNET_TIME_relative_divide (record_put_interval,
370 zone_iteration_interval = GNUNET_TIME_relative_max (MINIMUM_ZONE_ITERATION_INTERVAL,
371 zone_iteration_interval);
373 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
374 "Adjusted zone iteration interval to %llus!\n",
375 zone_iteration_interval.rel_value);
376 GNUNET_STATISTICS_set (statistics,
377 "Current zone iteration interval [msec]",
378 zone_iteration_interval.rel_value,
381 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
382 "Zone iteration finished. Rescheduling zone iteration\n");
384 namestore_iter = NULL;
385 if (num_public_records == 0)
386 zone_update_taskid = GNUNET_SCHEDULER_add_delayed (zone_iteration_interval,
387 &update_zone_dht_start,
390 zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_start, NULL);
391 GNUNET_STATISTICS_update (statistics,
392 "Number of zone iterations", 1, GNUNET_NO);
394 last_num_public_records = num_public_records;
395 GNUNET_STATISTICS_set (statistics,
396 "Number of public records in DHT",
397 last_num_public_records,
402 namelen = strlen(name) + 1;
406 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
407 "No records given for name %s! Skipping...\n",
409 zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_next,
414 if (signature == NULL)
416 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
417 "No signature for %s record data provided! Skipping...\n",
419 zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_next,
425 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
426 "Putting records for %s into the DHT\n", name);
428 rd_payload_length = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
430 nrb = GNUNET_malloc(rd_payload_length + namelen
431 + sizeof(struct GNSNameRecordBlock));
433 nrb->signature = *signature;
435 nrb->public_key = *key;
437 nrb->rd_count = htonl(rd_count);
439 memcpy(&nrb[1], name, namelen);
441 nrb_data = (char*)&nrb[1];
444 rd_payload_length += sizeof(struct GNSNameRecordBlock) + namelen;
446 if (-1 == GNUNET_NAMESTORE_records_serialize (rd_count,
451 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
452 "Record serialization failed! Skipping...\n");
454 zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_next,
461 * calculate DHT key: H(name) xor H(pubkey)
463 GNUNET_CRYPTO_short_hash(key,
464 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
466 GNUNET_CRYPTO_short_hash(name, strlen(name), &name_hash);
467 GNUNET_CRYPTO_short_hash_double (&name_hash, &name_hash_double);
468 GNUNET_CRYPTO_short_hash_double (&zhash, &zone_hash_double);
469 GNUNET_CRYPTO_hash_xor(&zone_hash_double, &name_hash_double, &xor_hash);
471 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
472 "zone identity: %s\n", GNUNET_h2s (&zone_hash_double));
474 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
475 "putting %d records for %s under key: %s with size %d\n",
476 rd_count, name, GNUNET_h2s (&xor_hash), rd_payload_length);
478 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
479 "DHT req to %d\n", DHT_OPERATION_TIMEOUT.rel_value);
481 GNUNET_STATISTICS_update (statistics,
482 "Record bytes put into DHT", rd_payload_length, GNUNET_NO);
484 /* FIXME: keep return value to possibly cancel? */
485 GNUNET_DHT_put (dht_handle, &xor_hash,
486 DHT_GNS_REPLICATION_LEVEL,
488 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
492 DHT_OPERATION_TIMEOUT,
494 NULL); //cls for cont
496 num_public_records++;
498 if ((num_public_records > last_num_public_records)
499 && (first_zone_iteration == GNUNET_NO))
501 zone_iteration_interval = GNUNET_TIME_relative_divide (record_put_interval,
503 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
504 "Last record count was lower than current record count... increasing.\n");
505 next_put_interval = GNUNET_TIME_relative_divide (zone_iteration_interval,
506 LATE_ITERATION_SPEEDUP_FACTOR);
510 next_put_interval = zone_iteration_interval;
512 GNUNET_STATISTICS_set (statistics,
513 "Current zone iteration interval [msec]",
514 next_put_interval.rel_value,
518 * Reschedule periodic put
520 zone_update_taskid = GNUNET_SCHEDULER_add_delayed (next_put_interval,
521 &update_zone_dht_next,
529 * Periodically iterate over our zone and store everything in dht
532 * @param tc task context
535 update_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
537 zone_update_taskid = GNUNET_SCHEDULER_NO_TASK;
539 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Scheduling DHT zone update!\n");
541 /* start counting again */
542 num_public_records = 0;
543 namestore_iter = GNUNET_NAMESTORE_zone_iteration_start (namestore_handle,
545 GNUNET_NAMESTORE_RF_AUTHORITY,
546 GNUNET_NAMESTORE_RF_PRIVATE,
551 /* END DHT ZONE PROPAGATION */
554 * Send shorten response back to client
556 * @param cls the closure containing a client shorten handle
557 * @param name the shortened name result or NULL if cannot be shortened
560 send_shorten_response(void* cls, const char* name)
562 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message with %s\n",
563 "SHORTEN_RESULT", name);
564 struct GNUNET_GNS_ClientShortenResultMessage *rmsg;
565 struct ClientShortenHandle *csh = (struct ClientShortenHandle *)cls;
572 GNUNET_STATISTICS_update (statistics,
573 "Name shorten results", 1, GNUNET_NO);
575 rmsg = GNUNET_malloc(sizeof(struct GNUNET_GNS_ClientShortenResultMessage)
578 rmsg->id = csh->unique_id;
579 rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_SHORTEN_RESULT);
581 htons(sizeof(struct GNUNET_GNS_ClientShortenResultMessage) +
584 strcpy((char*)&rmsg[1], name);
586 GNUNET_SERVER_notification_context_unicast (nc, csh->client,
587 (const struct GNUNET_MessageHeader *) rmsg,
589 GNUNET_SERVER_receive_done (csh->client, GNUNET_OK);
591 if (NULL != csh->namestore_task)
592 GNUNET_NAMESTORE_cancel (csh->namestore_task);
601 * Lookup the zone infos and shorten name
603 * @param cls the client shorten handle
604 * @param key key of the zone
605 * @param expiration expiration of record
606 * @param name name found or null if no result
607 * @param rd_count number of records found
608 * @param rd record data
613 process_shorten_in_private_zone_lookup (void *cls,
614 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
615 struct GNUNET_TIME_Absolute expiration,
617 unsigned int rd_count,
618 const struct GNUNET_NAMESTORE_RecordData *rd,
619 const struct GNUNET_CRYPTO_RsaSignature *signature)
621 struct ClientShortenHandle *csh = cls;
622 csh->namestore_task = NULL;
623 struct GNUNET_CRYPTO_ShortHashCode *szone = &csh->shorten_zone;
624 struct GNUNET_CRYPTO_ShortHashCode *pzone = &csh->private_zone;
626 if (0 == strcmp (csh->private_zone_id, ""))
631 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
632 "No shorten zone in private zone!\n");
634 strcpy (csh->shorten_zone_id, "");
641 GNUNET_assert (rd_count == 1);
643 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
644 "Shorten zone %s found in private zone %s\n",
645 name, csh->private_zone_id);
647 sprintf (csh->shorten_zone_id, "%s.%s", name, csh->private_zone_id);
650 GNUNET_CONTAINER_DLL_remove (csh_head, csh_tail, csh);
652 gns_resolver_shorten_name (&csh->root_zone,
656 csh->private_zone_id,
657 csh->shorten_zone_id,
658 &send_shorten_response, csh);
664 * Lookup the zone infos and shorten name
666 * @param cls the shorten handle
667 * @param key key of the zone
668 * @param expiration expiration of record
669 * @param name name found or null if no result
670 * @param rd_count number of records found
671 * @param rd record data
676 process_shorten_in_root_zone_lookup (void *cls,
677 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
678 struct GNUNET_TIME_Absolute expiration,
680 unsigned int rd_count,
681 const struct GNUNET_NAMESTORE_RecordData *rd,
682 const struct GNUNET_CRYPTO_RsaSignature *signature)
684 struct ClientShortenHandle *csh = cls;
685 csh->namestore_task = NULL;
686 struct GNUNET_CRYPTO_ShortHashCode *szone = &csh->shorten_zone;
687 struct GNUNET_CRYPTO_ShortHashCode *pzone = &csh->private_zone;
689 if (0 == strcmp (csh->private_zone_id, ""))
694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
695 "No shorten zone in zone and no private zone!\n");
697 strcpy (csh->shorten_zone_id, "");
699 GNUNET_CONTAINER_DLL_remove (csh_head, csh_tail, csh);
702 gns_resolver_shorten_name (&csh->root_zone,
706 csh->private_zone_id,
707 csh->shorten_zone_id,
708 &send_shorten_response, csh);
712 GNUNET_assert (rd_count == 1);
714 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
715 "Private zone %s found in root zone\n", name);
717 strcpy (csh->private_zone_id, name);
719 csh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
722 &process_shorten_in_private_zone_lookup,
728 * Lookup the zone infos and shorten name
730 * @param cls the shorten handle
731 * @param key key of the zone
732 * @param expiration expiration of record
733 * @param name name found or null if no result
734 * @param rd_count number of records found
735 * @param rd record data
740 process_private_in_root_zone_lookup (void *cls,
741 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
742 struct GNUNET_TIME_Absolute expiration,
744 unsigned int rd_count,
745 const struct GNUNET_NAMESTORE_RecordData *rd,
746 const struct GNUNET_CRYPTO_RsaSignature *signature)
748 struct ClientShortenHandle *csh = cls;
749 csh->namestore_task = NULL;
753 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
754 "No private zone in root zone\n");
756 strcpy (csh->private_zone_id, "");
758 csh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
761 &process_shorten_in_root_zone_lookup,
766 GNUNET_assert (rd_count == 1);
768 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
769 "Private zone %s found in root zone\n", name);
771 strcpy (csh->private_zone_id, name);
773 csh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
776 &process_shorten_in_private_zone_lookup,
781 * Lookup the zone infos and shorten name
783 * @param csh the shorten handle
787 start_shorten_name (struct ClientShortenHandle *csh)
789 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
790 "Looking for private zone name in root zone\n");
792 csh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
795 &process_private_in_root_zone_lookup,
801 * Handle a shorten message from the api
803 * @param cls the closure
804 * @param client the client
805 * @param message the message
807 static void handle_shorten (void *cls,
808 struct GNUNET_SERVER_Client * client,
809 const struct GNUNET_MessageHeader * message)
811 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "SHORTEN");
814 struct ClientShortenHandle *csh;
815 char name[MAX_DNS_NAME_LENGTH];
816 char* nameptr = name;
818 if (ntohs (message->size) < sizeof (struct GNUNET_GNS_ClientShortenMessage))
821 GNUNET_SERVER_receive_done (client, GNUNET_OK);
826 struct GNUNET_GNS_ClientShortenMessage *sh_msg =
827 (struct GNUNET_GNS_ClientShortenMessage *) message;
829 msg_size = ntohs(message->size);
831 if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
834 GNUNET_SERVER_receive_done (client, GNUNET_OK);
838 csh = GNUNET_malloc(sizeof (struct ClientShortenHandle));
839 csh->client = client;
840 csh->unique_id = sh_msg->id;
842 GNUNET_CONTAINER_DLL_insert (csh_head, csh_tail, csh);
844 GNUNET_STRINGS_utf8_tolower((char*)&sh_msg[1], &nameptr);
846 GNUNET_SERVER_notification_context_add (nc, client);
848 if (strlen (name) < strlen(GNUNET_GNS_TLD)) {
849 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
850 "SHORTEN: %s is too short", name);
851 GNUNET_CONTAINER_DLL_remove (csh_head, csh_tail, csh);
852 send_shorten_response(csh, name);
856 if (strlen (name) > MAX_DNS_NAME_LENGTH) {
857 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
858 "SHORTEN: %s is too long", name);
859 GNUNET_CONTAINER_DLL_remove (csh_head, csh_tail, csh);
860 send_shorten_response(csh, name);
864 if (!is_gnunet_tld(name) && !is_zkey_tld(name))
866 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
867 "%s is not our domain. Returning\n", name);
868 GNUNET_CONTAINER_DLL_remove (csh_head, csh_tail, csh);
869 send_shorten_response(csh, name);
873 csh->shorten_zone = sh_msg->shorten_zone;
874 csh->private_zone = sh_msg->private_zone;
876 strcpy (csh->name, name);
879 if (1 == ntohl(sh_msg->use_default_zone))
880 csh->root_zone = zone_hash; //Default zone
882 csh->root_zone = sh_msg->zone;
884 start_shorten_name (csh);
886 GNUNET_STATISTICS_update (statistics,
887 "Name shorten attempts", 1, GNUNET_NO);
893 * Send get authority response back to client
895 * @param cls the closure containing a client get auth handle
896 * @param name the shortened name result or NULL if cannot be shortened
899 send_get_auth_response(void *cls, const char* name)
901 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message with %s\n",
902 "GET_AUTH_RESULT", name);
903 struct GNUNET_GNS_ClientGetAuthResultMessage *rmsg;
904 struct ClientGetAuthHandle *cah = (struct ClientGetAuthHandle *)cls;
908 GNUNET_STATISTICS_update (statistics,
909 "Authorities resolved", 1, GNUNET_NO);
917 rmsg = GNUNET_malloc(sizeof(struct GNUNET_GNS_ClientGetAuthResultMessage)
920 rmsg->id = cah->unique_id;
921 rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_GET_AUTH_RESULT);
923 htons(sizeof(struct GNUNET_GNS_ClientGetAuthResultMessage) +
926 strcpy((char*)&rmsg[1], name);
928 GNUNET_SERVER_notification_context_unicast (nc, cah->client,
929 (const struct GNUNET_MessageHeader *) rmsg,
931 GNUNET_SERVER_receive_done (cah->client, GNUNET_OK);
934 GNUNET_free_non_null(cah->name);
943 * Handle a get authority message from the api
945 * @param cls the closure
946 * @param client the client
947 * @param message the message
949 static void handle_get_authority(void *cls,
950 struct GNUNET_SERVER_Client * client,
951 const struct GNUNET_MessageHeader * message)
953 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "GET_AUTH");
956 struct ClientGetAuthHandle *cah;
957 char name[MAX_DNS_NAME_LENGTH];
958 char* nameptr = name;
961 if (ntohs (message->size) < sizeof (struct GNUNET_GNS_ClientGetAuthMessage))
964 GNUNET_SERVER_receive_done (client, GNUNET_OK);
968 GNUNET_SERVER_notification_context_add (nc, client);
970 struct GNUNET_GNS_ClientGetAuthMessage *sh_msg =
971 (struct GNUNET_GNS_ClientGetAuthMessage *) message;
973 msg_size = ntohs(message->size);
975 if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
978 GNUNET_SERVER_receive_done (client, GNUNET_OK);
982 GNUNET_STRINGS_utf8_tolower((char*)&sh_msg[1], &nameptr);
985 cah = GNUNET_malloc(sizeof(struct ClientGetAuthHandle));
986 cah->client = client;
987 cah->unique_id = sh_msg->id;
989 if (strlen(name) < strlen(GNUNET_GNS_TLD))
991 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
992 "GET_AUTH: %s is too short. Returning\n", name);
994 send_get_auth_response(cah, name);
998 if (strlen (name) > MAX_DNS_NAME_LENGTH) {
999 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1000 "GET_AUTH: %s is too long", name);
1002 send_get_auth_response(cah, name);
1006 if (strcmp(name+strlen(name)-strlen(GNUNET_GNS_TLD),
1007 GNUNET_GNS_TLD) != 0)
1009 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1010 "GET_AUTH: %s is not our domain. Returning\n", name);
1012 send_get_auth_response(cah, name);
1016 if (strcmp(name, GNUNET_GNS_TLD) == 0)
1018 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1019 "GET_AUTH: %s is us. Returning\n", name);
1021 send_get_auth_response(cah, name);
1025 cah->name = GNUNET_malloc(strlen(name)
1026 - strlen(GNUNET_GNS_TLD) + 1);
1027 memset(cah->name, 0,
1028 strlen(name)-strlen(GNUNET_GNS_TLD) + 1);
1029 memcpy(cah->name, name,
1030 strlen(name)-strlen(GNUNET_GNS_TLD));
1032 /* Start delegation resolution in our namestore */
1033 gns_resolver_get_authority(zone_hash, zone_hash, name, &send_get_auth_response, cah);
1035 GNUNET_STATISTICS_update (statistics,
1036 "Authority lookup attempts", 1, GNUNET_NO);
1042 * Reply to client with the result from our lookup.
1044 * @param cls the closure (our client lookup handle)
1045 * @param rd_count the number of records
1046 * @param rd the record data
1049 send_lookup_response(void* cls,
1051 const struct GNUNET_NAMESTORE_RecordData *rd)
1053 struct ClientLookupHandle* clh = (struct ClientLookupHandle*)cls;
1054 struct GNUNET_GNS_ClientLookupResultMessage *rmsg;
1057 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message with %d results\n",
1058 "LOOKUP_RESULT", rd_count);
1060 len = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
1061 rmsg = GNUNET_malloc(len+sizeof(struct GNUNET_GNS_ClientLookupResultMessage));
1063 rmsg->id = clh->unique_id;
1064 rmsg->rd_count = htonl(rd_count);
1065 rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT);
1067 htons(len+sizeof(struct GNUNET_GNS_ClientLookupResultMessage));
1069 GNUNET_NAMESTORE_records_serialize (rd_count, rd, len, (char*)&rmsg[1]);
1071 GNUNET_SERVER_notification_context_unicast (nc, clh->client,
1072 (const struct GNUNET_MessageHeader *) rmsg,
1074 GNUNET_SERVER_receive_done (clh->client, GNUNET_OK);
1077 GNUNET_free(clh->name);
1079 if (NULL != clh->shorten_key)
1080 GNUNET_free(clh->shorten_key);
1084 GNUNET_STATISTICS_update (statistics,
1085 "Completed lookups", 1, GNUNET_NO);
1089 GNUNET_STATISTICS_update (statistics,
1090 "Records resolved", rd_count, GNUNET_NO);
1097 * Handle lookup requests from client
1099 * @param cls the closure
1100 * @param client the client
1101 * @param message the message
1104 handle_lookup(void *cls,
1105 struct GNUNET_SERVER_Client * client,
1106 const struct GNUNET_MessageHeader * message)
1108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "LOOKUP");
1110 size_t msg_size = 0;
1112 char name[MAX_DNS_NAME_LENGTH];
1113 struct ClientLookupHandle *clh;
1114 char* nameptr = name;
1116 struct GNUNET_CRYPTO_RsaPrivateKey *key;
1117 struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded *pkey;
1120 if (ntohs (message->size) < sizeof (struct GNUNET_GNS_ClientLookupMessage))
1122 GNUNET_break_op (0);
1123 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1127 GNUNET_SERVER_notification_context_add (nc, client);
1129 struct GNUNET_GNS_ClientLookupMessage *sh_msg =
1130 (struct GNUNET_GNS_ClientLookupMessage *) message;
1132 msg_size = ntohs(message->size);
1134 if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
1136 GNUNET_break_op (0);
1137 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1141 if (1 == ntohl(sh_msg->have_key))
1143 pkey = (struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded *)&sh_msg[1];
1144 tmp_pkey = (char*)&sh_msg[1];
1145 key = GNUNET_CRYPTO_rsa_decode_key (tmp_pkey, ntohs(pkey->len));
1146 GNUNET_STRINGS_utf8_tolower(&tmp_pkey[ntohs(pkey->len)], &nameptr);
1151 GNUNET_STRINGS_utf8_tolower((char*)&sh_msg[1], &nameptr);
1154 namelen = strlen(name)+1;
1155 clh = GNUNET_malloc (sizeof (struct ClientLookupHandle));
1156 memset (clh, 0, sizeof (struct ClientLookupHandle));
1157 clh->client = client;
1158 clh->name = GNUNET_malloc(namelen);
1159 strcpy(clh->name, name);
1160 clh->unique_id = sh_msg->id;
1161 clh->type = ntohl(sh_msg->type);
1162 clh->shorten_key = key;
1164 only_cached = ntohl(sh_msg->only_cached);
1166 if (strlen (name) > MAX_DNS_NAME_LENGTH) {
1167 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1168 "LOOKUP: %s is too long", name);
1170 send_lookup_response(clh, 0, NULL);
1174 if ((clh->type == GNUNET_GNS_RECORD_A) &&
1175 (GNUNET_OK != v4_enabled))
1177 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1178 "LOOKUP: Query for A record but AF_INET not supported!");
1180 send_lookup_response(clh, 0, NULL);
1184 if ((clh->type == GNUNET_GNS_RECORD_AAAA) &&
1185 (GNUNET_OK != v6_enabled))
1187 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1188 "LOOKUP: Query for AAAA record but AF_INET6 not supported!");
1190 send_lookup_response(clh, 0, NULL);
1194 if (1 == ntohl(sh_msg->use_default_zone))
1195 clh->zone = zone_hash; //Default zone
1197 clh->zone = sh_msg->zone;
1199 if (GNUNET_YES == auto_import_pkey)
1201 gns_resolver_lookup_record (clh->zone, clh->zone, clh->type, clh->name,
1203 default_lookup_timeout,
1205 &send_lookup_response, clh);
1209 gns_resolver_lookup_record (clh->zone, clh->zone, clh->type, name,
1211 default_lookup_timeout,
1213 &send_lookup_response, clh);
1216 GNUNET_STATISTICS_update (statistics,
1217 "Record lookup attempts", 1, GNUNET_NO);
1221 * Test if the given AF is supported by this system.
1224 * @return GNUNET_OK if the AF is supported
1231 s = socket (af, SOCK_STREAM, 0);
1234 if (EAFNOSUPPORT == errno)
1236 fprintf (stderr, "Failed to create test socket: %s\n", STRERROR (errno));
1237 return GNUNET_SYSERR;
1248 * Process GNS requests.
1250 * @param cls closure)
1251 * @param server the initialized server
1252 * @param c configuration to use
1255 run (void *cls, struct GNUNET_SERVER_Handle *server,
1256 const struct GNUNET_CONFIGURATION_Handle *c)
1259 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Initializing GNS\n");
1262 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
1263 unsigned long long max_parallel_bg_queries = 0;
1264 unsigned long long default_lookup_timeout_secs = 0;
1265 int ignore_pending = GNUNET_NO;
1267 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1268 {&handle_shorten, NULL, GNUNET_MESSAGE_TYPE_GNS_SHORTEN, 0},
1269 {&handle_lookup, NULL, GNUNET_MESSAGE_TYPE_GNS_LOOKUP, 0},
1270 {&handle_get_authority, NULL, GNUNET_MESSAGE_TYPE_GNS_GET_AUTH, 0}
1275 v6_enabled = test_af (AF_INET6);
1276 v4_enabled = test_af (AF_INET);
1278 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (c, "gns",
1279 "ZONEKEY", &keyfile))
1281 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1282 "No private key for root zone specified!\n");
1283 GNUNET_SCHEDULER_shutdown ();
1287 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1288 "Using keyfile %s for root zone.\n", keyfile);
1290 zone_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
1291 GNUNET_CRYPTO_rsa_key_get_public (zone_key, &pkey);
1293 GNUNET_CRYPTO_short_hash(&pkey,
1294 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1296 GNUNET_free(keyfile);
1299 * handle to our local namestore
1301 namestore_handle = GNUNET_NAMESTORE_connect(c);
1303 if (NULL == namestore_handle)
1305 //FIXME do error handling;
1306 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1307 "Failed to connect to the namestore!\n");
1308 GNUNET_SCHEDULER_shutdown ();
1314 auto_import_pkey = GNUNET_NO;
1317 GNUNET_CONFIGURATION_get_value_yesno (c, "gns",
1318 "AUTO_IMPORT_PKEY"))
1320 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1321 "Automatic PKEY import is enabled.\n");
1322 auto_import_pkey = GNUNET_YES;
1326 zone_iteration_interval = INITIAL_ZONE_ITERATION_INTERVAL;
1328 record_put_interval = DEFAULT_RECORD_PUT_INTERVAL;
1331 GNUNET_CONFIGURATION_get_value_time (c, "gns",
1332 "RECORD_PUT_INTERVAL",
1333 &record_put_interval))
1335 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1336 "Record put interval: %llu\n",
1337 record_put_interval);
1341 GNUNET_CONFIGURATION_get_value_number (c, "gns",
1342 "MAX_PARALLEL_BACKGROUND_QUERIES",
1343 &max_parallel_bg_queries))
1345 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1346 "Number of allowed parallel background queries: %llu\n",
1347 max_parallel_bg_queries);
1351 GNUNET_CONFIGURATION_get_value_yesno (c, "gns",
1352 "AUTO_IMPORT_CONFIRMATION_REQ"))
1354 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1355 "Auto import requires user confirmation\n");
1356 ignore_pending = GNUNET_YES;
1360 GNUNET_CONFIGURATION_get_value_number(c, "gns",
1361 "DEFAULT_LOOKUP_TIMEOUT",
1362 &default_lookup_timeout_secs))
1364 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1365 "Default lookup timeout: %llus\n", default_lookup_timeout_secs);
1366 default_lookup_timeout = GNUNET_TIME_relative_multiply(
1367 GNUNET_TIME_UNIT_SECONDS,
1368 default_lookup_timeout_secs);
1374 dht_handle = GNUNET_DHT_connect(c,
1375 //max_parallel_bg_queries); //FIXME get ht_len from cfg
1378 if (NULL == dht_handle)
1380 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not connect to DHT!\n");
1383 if (gns_resolver_init(namestore_handle, dht_handle, zone_hash, c,
1384 max_parallel_bg_queries,
1388 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1389 "Unable to initialize resolver!\n");
1390 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
1395 GNUNET_CONFIGURATION_get_value_yesno (c, "gns", "HIJACK_DNS"))
1397 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1398 "DNS hijacking enabled... connecting to service.\n");
1400 if (gns_interceptor_init(zone_hash, zone_key, c) == GNUNET_SYSERR)
1402 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1403 "Failed to enable the dns interceptor!\n");
1408 * Schedule periodic put
1410 * We have roughly an hour for all records;
1412 first_zone_iteration = GNUNET_YES;
1413 zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_start, NULL);
1415 GNUNET_SERVER_add_handlers (server, handlers);
1418 //GNUNET_SERVER_disconnect_notify (server,
1419 // &client_disconnect_notification,
1422 statistics = GNUNET_STATISTICS_create ("gns", c);
1424 nc = GNUNET_SERVER_notification_context_create (server, 1);
1426 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
1432 * The main function for the GNS service.
1434 * @param argc number of arguments from the command line
1435 * @param argv command line arguments
1436 * @return 0 ok, 1 on error
1439 main (int argc, char *const *argv)
1445 GNUNET_SERVICE_run (argc, argv, "gns", GNUNET_SERVICE_OPTION_NONE, &run,
1450 /* end of gnunet-service-gns.c */