2 This file is part of GNUnet.
3 (C) 2009, 2010, 2011, 2012 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 "gns_common.h"
39 #include "gnunet-service-gns_resolver.h"
40 #include "gnunet-service-gns_interceptor.h"
41 #include "gnunet_protocols.h"
43 #define INITIAL_ZONE_ITERATION_INTERVAL GNUNET_TIME_UNIT_MILLISECONDS
44 #define MINIMUM_ZONE_ITERATION_INTERVAL GNUNET_TIME_UNIT_SECONDS
45 #define DEFAULT_RECORD_PUT_INTERVAL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 4)
46 #define LATE_ITERATION_SPEEDUP_FACTOR 2
50 * Handle to a shorten operation from api
52 struct ClientShortenHandle
56 struct ClientShortenHandle *next;
59 struct ClientShortenHandle *prev;
61 /* the requesting client that */
62 struct GNUNET_SERVER_Client *client;
68 enum GNUNET_GNS_RecordType type;
71 char name[MAX_DNS_NAME_LENGTH];
73 /* name of private zone (relative to root) */
74 char private_zone_id[MAX_DNS_NAME_LENGTH];
76 /* name of shorten zone (relative to root) */
77 char shorten_zone_id[MAX_DNS_NAME_LENGTH];
80 struct GNUNET_CRYPTO_ShortHashCode root_zone;
83 struct GNUNET_CRYPTO_ShortHashCode private_zone;
86 struct GNUNET_CRYPTO_ShortHashCode shorten_zone;
88 /* Namestore lookup task */
89 struct GNUNET_NAMESTORE_QueueEntry *namestore_task;
95 * Handle to a get auhtority operation from api
97 struct ClientGetAuthHandle
99 /* the requesting client that */
100 struct GNUNET_SERVER_Client *client;
105 /* name to lookup authority */
112 * Handle to a lookup operation from api
114 struct ClientLookupHandle
117 /* the requesting client that */
118 struct GNUNET_SERVER_Client *client;
120 /* The zone we look up in */
121 struct GNUNET_CRYPTO_ShortHashCode zone;
123 /* Do we only want to lookup from local cache? */
130 enum GNUNET_GNS_RecordType type;
132 /* optional zone private key used for shorten */
133 struct GNUNET_CRYPTO_RsaPrivateKey *shorten_key;
135 /* the name to look up */
141 * Our handle to the DHT
143 static struct GNUNET_DHT_Handle *dht_handle;
146 * Our zone's private key
148 static struct GNUNET_CRYPTO_RsaPrivateKey *zone_key;
151 * Our handle to the namestore service
152 * FIXME maybe need a second handle for iteration
154 static struct GNUNET_NAMESTORE_Handle *namestore_handle;
157 * Handle to iterate over our authoritative zone in namestore
159 static struct GNUNET_NAMESTORE_ZoneIterator *namestore_iter;
162 * Our notification context.
164 static struct GNUNET_SERVER_NotificationContext *nc;
169 static struct GNUNET_CRYPTO_ShortHashCode zone_hash;
172 * Useful for zone update for DHT put
174 static unsigned long long num_public_records;
177 * Last seen record count
179 static unsigned long long last_num_public_records;
184 static struct GNUNET_TIME_Relative zone_iteration_interval;
186 /* dht update interval FIXME define? */
187 static struct GNUNET_TIME_Relative record_put_interval;
189 /* zone update task */
190 static GNUNET_SCHEDULER_TaskIdentifier zone_update_taskid;
192 /* automatic pkey import for name shortening */
193 static int auto_import_pkey;
195 /* first zone iteration is specia */
196 static int first_zone_iteration;
199 static struct GNUNET_TIME_Relative default_lookup_timeout;
202 static int v6_enabled;
205 static int v4_enabled;
207 /* Shorten DLL for cancelling NS requests */
208 static struct ClientShortenHandle *csh_head;
210 /* Shorten DLL for cancelling NS requests */
211 static struct ClientShortenHandle *csh_tail;
213 /* Statistics handle */
214 static struct GNUNET_STATISTICS_Handle *statistics;
218 * Task run during shutdown.
224 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
226 struct ClientShortenHandle *csh_tmp;
228 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
230 while (NULL != (csh_tmp = csh_head))
232 GNUNET_CONTAINER_DLL_remove (csh_head, csh_tail, csh_tmp);
233 GNUNET_free(csh_tmp);
235 GNUNET_SERVER_notification_context_destroy (nc);
236 gns_interceptor_stop ();
237 gns_resolver_cleanup ();
238 if (NULL != statistics)
239 GNUNET_STATISTICS_destroy (statistics, GNUNET_NO);
240 if (zone_update_taskid != GNUNET_SCHEDULER_NO_TASK)
241 GNUNET_SCHEDULER_cancel (zone_update_taskid);
242 if (NULL != namestore_iter)
243 GNUNET_NAMESTORE_zone_iteration_stop (namestore_iter);
244 GNUNET_NAMESTORE_disconnect(namestore_handle);
245 GNUNET_DHT_disconnect(dht_handle);
250 * Method called periodically that triggers iteration over authoritative records
253 * @param tc task context
256 update_zone_dht_next (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
258 zone_update_taskid = GNUNET_SCHEDULER_NO_TASK;
259 GNUNET_NAMESTORE_zone_iterator_next (namestore_iter);
264 * Continuation for DHT put
267 * @param success GNUNET_OK if the PUT was transmitted,
268 * GNUNET_NO on timeout,
269 * GNUNET_SYSERR on disconnect from service
270 * after the PUT message was transmitted
271 * (so we don't know if it was received or not)
274 record_dht_put(void *cls, int success)
276 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "put request transmitted\n");
282 update_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
286 * Function used to put all records successively into the DHT.
288 * @param cls the closure (NULL)
289 * @param key the public key of the authority (ours)
290 * @param expiration lifetime of the namestore entry
291 * @param name the name of the records
292 * @param rd_count the number of records in data
293 * @param rd the record data
294 * @param signature the signature for the record data
297 put_gns_record(void *cls,
298 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
299 struct GNUNET_TIME_Absolute expiration,
301 unsigned int rd_count,
302 const struct GNUNET_NAMESTORE_RecordData *rd,
303 const struct GNUNET_CRYPTO_RsaSignature *signature)
305 struct GNSNameRecordBlock *nrb;
306 struct GNUNET_CRYPTO_ShortHashCode zhash;
307 struct GNUNET_HashCode dht_key;
308 uint32_t rd_payload_length;
309 char* nrb_data = NULL;
311 struct GNUNET_TIME_Relative next_put_interval;
316 namestore_iter = NULL;
317 last_num_public_records = num_public_records;
318 first_zone_iteration = GNUNET_NO;
319 if (0 == num_public_records)
322 * If no records are known (startup) or none present
323 * we can safely set the interval to the value for a single
326 zone_iteration_interval = GNUNET_TIME_relative_divide (record_put_interval,
329 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
330 "No records in db.\n");
334 zone_iteration_interval = GNUNET_TIME_relative_divide (record_put_interval,
337 zone_iteration_interval = GNUNET_TIME_relative_max (MINIMUM_ZONE_ITERATION_INTERVAL,
338 zone_iteration_interval);
340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
341 "Zone iteration finished. Adjusted zone iteration interval to %s\n",
342 GNUNET_STRINGS_relative_time_to_string (zone_iteration_interval, GNUNET_YES));
343 GNUNET_STATISTICS_set (statistics,
344 "Current zone iteration interval (in ms)",
345 zone_iteration_interval.rel_value,
347 GNUNET_STATISTICS_update (statistics,
348 "Number of zone iterations", 1, GNUNET_NO);
349 GNUNET_STATISTICS_set (statistics,
350 "Number of public records in DHT",
351 last_num_public_records,
353 if (0 == num_public_records)
354 zone_update_taskid = GNUNET_SCHEDULER_add_delayed (zone_iteration_interval,
355 &update_zone_dht_start,
358 zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_start, NULL);
362 namelen = strlen (name) + 1;
365 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
366 "No records for name `%s'! Skipping.\n",
368 zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_next,
372 if (NULL == signature)
375 zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_next,
380 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
381 "Putting records for `%s' into the DHT\n", name);
382 rd_payload_length = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
383 nrb = GNUNET_malloc (rd_payload_length + namelen
384 + sizeof (struct GNSNameRecordBlock));
385 nrb->signature = *signature;
386 nrb->public_key = *key;
387 nrb->rd_count = htonl (rd_count);
388 memcpy (&nrb[1], name, namelen);
389 nrb_data = (char *) &nrb[1];
391 rd_payload_length += sizeof(struct GNSNameRecordBlock) + namelen;
392 GNUNET_CRYPTO_short_hash (key,
393 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
395 if (-1 == GNUNET_NAMESTORE_records_serialize (rd_count,
400 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
401 _("Records for name `%s' in zone %s too large to fit into DHT"),
403 GNUNET_short_h2s (&zhash));
405 zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_next,
410 GNUNET_GNS_get_key_for_record (name, &zhash, &dht_key);
411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
412 "putting %u records from zone %s for `%s' under key: %s with size %u and timeout %s\n",
414 GNUNET_short_h2s (&zhash),
416 GNUNET_h2s (&dht_key),
417 (unsigned int) rd_payload_length,
418 GNUNET_STRINGS_relative_time_to_string (DHT_OPERATION_TIMEOUT, GNUNET_YES));
420 GNUNET_STATISTICS_update (statistics,
421 "Record bytes put into DHT", rd_payload_length, GNUNET_NO);
423 (void) GNUNET_DHT_put (dht_handle, &dht_key,
424 DHT_GNS_REPLICATION_LEVEL,
425 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
426 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
430 DHT_OPERATION_TIMEOUT,
435 num_public_records++;
436 if ( (num_public_records > last_num_public_records)
437 && (GNUNET_NO == first_zone_iteration) )
439 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
440 "Last record count was lower than current record count. Reducing interval.\n");
441 zone_iteration_interval = GNUNET_TIME_relative_divide (record_put_interval,
443 next_put_interval = GNUNET_TIME_relative_divide (zone_iteration_interval,
444 LATE_ITERATION_SPEEDUP_FACTOR);
447 next_put_interval = zone_iteration_interval;
449 GNUNET_STATISTICS_set (statistics,
450 "Current zone iteration interval (ms)",
451 next_put_interval.rel_value,
453 zone_update_taskid = GNUNET_SCHEDULER_add_delayed (next_put_interval,
454 &update_zone_dht_next,
460 * Periodically iterate over our zone and store everything in dht
463 * @param tc task context
466 update_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
468 zone_update_taskid = GNUNET_SCHEDULER_NO_TASK;
470 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Scheduling DHT zone update!\n");
471 /* start counting again */
472 num_public_records = 0;
473 namestore_iter = GNUNET_NAMESTORE_zone_iteration_start (namestore_handle,
474 NULL, /* All zones */
475 GNUNET_NAMESTORE_RF_AUTHORITY,
476 GNUNET_NAMESTORE_RF_PRIVATE,
482 /* END DHT ZONE PROPAGATION */
486 * Send shorten response back to client
488 * @param cls the closure containing a client shorten handle
489 * @param name the shortened name result or NULL if cannot be shortened
492 send_shorten_response (void* cls, const char* name)
494 struct ClientShortenHandle *csh = cls;
495 struct GNUNET_GNS_ClientShortenResultMessage *rmsg;
498 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message with %s\n",
499 "SHORTEN_RESULT", name);
503 name_len = strlen (name) + 1;
504 GNUNET_STATISTICS_update (statistics,
505 "Name shorten results", 1, GNUNET_NO);
507 rmsg = GNUNET_malloc (sizeof (struct GNUNET_GNS_ClientShortenResultMessage) +
510 rmsg->id = csh->unique_id;
511 rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_SHORTEN_RESULT);
513 htons(sizeof(struct GNUNET_GNS_ClientShortenResultMessage) +
515 memcpy (&rmsg[1], name, name_len);
516 GNUNET_SERVER_notification_context_unicast (nc, csh->client,
519 if (NULL != csh->namestore_task)
520 GNUNET_NAMESTORE_cancel (csh->namestore_task);
527 * Lookup the zone infos and shorten name
529 * @param cls the client shorten handle
530 * @param key key of the zone
531 * @param expiration expiration of record
532 * @param name name found or null if no result
533 * @param rd_count number of records found
534 * @param rd record data
539 process_shorten_in_private_zone_lookup (void *cls,
540 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
541 struct GNUNET_TIME_Absolute expiration,
543 unsigned int rd_count,
544 const struct GNUNET_NAMESTORE_RecordData *rd,
545 const struct GNUNET_CRYPTO_RsaSignature *signature)
547 struct ClientShortenHandle *csh = cls;
548 struct GNUNET_CRYPTO_ShortHashCode *szone = &csh->shorten_zone;
549 struct GNUNET_CRYPTO_ShortHashCode *pzone = &csh->private_zone;
551 csh->namestore_task = NULL;
552 if (0 == strcmp (csh->private_zone_id, ""))
557 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
558 "No shorten zone in private zone!\n");
559 strcpy (csh->shorten_zone_id, "");
564 GNUNET_assert (rd_count == 1);
566 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
567 "Shorten zone %s found in private zone %s\n",
568 name, csh->private_zone_id);
570 sprintf (csh->shorten_zone_id, "%s.%s", name, csh->private_zone_id);
573 GNUNET_CONTAINER_DLL_remove (csh_head, csh_tail, csh);
575 gns_resolver_shorten_name (&csh->root_zone,
579 csh->private_zone_id,
580 csh->shorten_zone_id,
581 &send_shorten_response, csh);
587 * Lookup the zone infos and shorten name
589 * @param cls the shorten handle
590 * @param key key of the zone
591 * @param expiration expiration of record
592 * @param name name found or null if no result
593 * @param rd_count number of records found
594 * @param rd record data
599 process_shorten_in_root_zone_lookup (void *cls,
600 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
601 struct GNUNET_TIME_Absolute expiration,
603 unsigned int rd_count,
604 const struct GNUNET_NAMESTORE_RecordData *rd,
605 const struct GNUNET_CRYPTO_RsaSignature *signature)
607 struct ClientShortenHandle *csh = cls;
608 struct GNUNET_CRYPTO_ShortHashCode *szone = &csh->shorten_zone;
609 struct GNUNET_CRYPTO_ShortHashCode *pzone = &csh->private_zone;
611 csh->namestore_task = NULL;
612 if (0 == strcmp (csh->private_zone_id, ""))
617 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
618 "No shorten zone in zone and no private zone!\n");
620 strcpy (csh->shorten_zone_id, "");
622 GNUNET_CONTAINER_DLL_remove (csh_head, csh_tail, csh);
625 gns_resolver_shorten_name (&csh->root_zone,
629 csh->private_zone_id,
630 csh->shorten_zone_id,
631 &send_shorten_response, csh);
635 GNUNET_assert (rd_count == 1);
637 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
638 "Private zone %s found in root zone\n", name);
640 strcpy (csh->private_zone_id, name);
642 csh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
645 &process_shorten_in_private_zone_lookup,
651 * Lookup the zone infos and shorten name
653 * @param cls the shorten handle
654 * @param key key of the zone
655 * @param expiration expiration of record
656 * @param name name found or null if no result
657 * @param rd_count number of records found
658 * @param rd record data
663 process_private_in_root_zone_lookup (void *cls,
664 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
665 struct GNUNET_TIME_Absolute expiration,
667 unsigned int rd_count,
668 const struct GNUNET_NAMESTORE_RecordData *rd,
669 const struct GNUNET_CRYPTO_RsaSignature *signature)
671 struct ClientShortenHandle *csh = cls;
672 csh->namestore_task = NULL;
676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
677 "No private zone in root zone\n");
679 strcpy (csh->private_zone_id, "");
681 csh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
684 &process_shorten_in_root_zone_lookup,
689 GNUNET_assert (rd_count == 1);
691 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
692 "Private zone %s found in root zone\n", name);
694 strcpy (csh->private_zone_id, name);
696 csh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
699 &process_shorten_in_private_zone_lookup,
705 * Lookup the zone infos and shorten name
707 * @param csh the shorten handle
711 start_shorten_name (struct ClientShortenHandle *csh)
713 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
714 "Looking for private zone name in root zone\n");
716 csh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
719 &process_private_in_root_zone_lookup,
725 * Handle a shorten message from the api
727 * @param cls the closure (unused)
728 * @param client the client
729 * @param message the message
732 handle_shorten (void *cls,
733 struct GNUNET_SERVER_Client * client,
734 const struct GNUNET_MessageHeader * message)
737 const struct GNUNET_GNS_ClientShortenMessage *sh_msg;
738 struct ClientShortenHandle *csh;
740 char name[MAX_DNS_NAME_LENGTH];
741 char* nameptr = name;
743 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "SHORTEN");
744 msg_size = ntohs (message->size);
745 if (msg_size < sizeof (struct GNUNET_GNS_ClientShortenMessage))
748 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
751 sh_msg = (const struct GNUNET_GNS_ClientShortenMessage *) message;
752 utf_in = (const char *) &sh_msg[1];
753 if ('\0' != utf_in[msg_size - sizeof (struct GNUNET_GNS_ClientShortenMessage) - 1])
756 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
759 csh = GNUNET_malloc (sizeof (struct ClientShortenHandle));
760 csh->client = client;
761 csh->unique_id = sh_msg->id;
762 GNUNET_CONTAINER_DLL_insert (csh_head, csh_tail, csh);
763 GNUNET_STRINGS_utf8_tolower (utf_in, &nameptr);
764 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
765 "SHORTEN: Converted `%s' to `%s'\n",
768 GNUNET_SERVER_notification_context_add (nc, client);
769 if (strlen (name) < strlen (GNUNET_GNS_TLD))
771 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
772 "SHORTEN: %s is too short\n", name);
773 GNUNET_CONTAINER_DLL_remove (csh_head, csh_tail, csh);
774 send_shorten_response(csh, name);
775 GNUNET_SERVER_receive_done (client, GNUNET_OK);
778 if (strlen (name) > MAX_DNS_NAME_LENGTH)
780 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
781 "SHORTEN: %s is too long\n", name);
782 GNUNET_CONTAINER_DLL_remove (csh_head, csh_tail, csh);
783 send_shorten_response(csh, name);
784 GNUNET_SERVER_receive_done (client, GNUNET_OK);
787 if ( (! is_gnunet_tld (name)) &&
788 (! is_zkey_tld (name)) )
790 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
791 "%s is not our domain. Returning\n", name);
792 GNUNET_CONTAINER_DLL_remove (csh_head, csh_tail, csh);
793 send_shorten_response (csh, name);
794 GNUNET_SERVER_receive_done (client, GNUNET_OK);
797 csh->shorten_zone = sh_msg->shorten_zone;
798 csh->private_zone = sh_msg->private_zone;
799 strcpy (csh->name, name);
800 if (1 == ntohl(sh_msg->use_default_zone))
801 csh->root_zone = zone_hash; //Default zone
803 csh->root_zone = sh_msg->zone;
804 start_shorten_name (csh);
805 GNUNET_STATISTICS_update (statistics,
806 "Name shorten attempts", 1, GNUNET_NO);
807 GNUNET_SERVER_receive_done (client, GNUNET_OK);
812 * Send get authority response back to client
814 * @param cls the closure containing a client get auth handle
815 * @param name the shortened name result or NULL if cannot be shortened
818 send_get_auth_response(void *cls, const char* name)
820 struct ClientGetAuthHandle *cah = cls;
821 struct GNUNET_GNS_ClientGetAuthResultMessage *rmsg;
823 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message with %s\n",
824 "GET_AUTH_RESULT", name);
827 GNUNET_STATISTICS_update (statistics,
828 "Authorities resolved", 1, GNUNET_NO);
833 rmsg = GNUNET_malloc(sizeof(struct GNUNET_GNS_ClientGetAuthResultMessage)
836 rmsg->id = cah->unique_id;
837 rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_GET_AUTH_RESULT);
839 htons(sizeof(struct GNUNET_GNS_ClientGetAuthResultMessage) +
841 strcpy ((char*)&rmsg[1], name);
843 GNUNET_SERVER_notification_context_unicast (nc, cah->client,
846 GNUNET_SERVER_receive_done (cah->client, GNUNET_OK);
849 GNUNET_free_non_null(cah->name);
855 * Handle a get authority message from the api
857 * @param cls the closure
858 * @param client the client
859 * @param message the message
862 handle_get_authority (void *cls,
863 struct GNUNET_SERVER_Client * client,
864 const struct GNUNET_MessageHeader * message)
867 const struct GNUNET_GNS_ClientGetAuthMessage *sh_msg;
868 struct ClientGetAuthHandle *cah;
870 char name[MAX_DNS_NAME_LENGTH];
871 char* nameptr = name;
873 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "GET_AUTH");
874 msg_size = ntohs(message->size);
875 if (msg_size < sizeof (struct GNUNET_GNS_ClientGetAuthMessage))
878 GNUNET_SERVER_receive_done (client, GNUNET_OK);
881 GNUNET_SERVER_notification_context_add (nc, client);
883 sh_msg = (const struct GNUNET_GNS_ClientGetAuthMessage *) message;
884 utf_in = (const char *) &sh_msg[1];
885 if ('\0' != utf_in[msg_size - sizeof (struct GNUNET_GNS_ClientGetAuthMessage) - 1])
888 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
891 GNUNET_STRINGS_utf8_tolower(utf_in, &nameptr);
892 cah = GNUNET_malloc(sizeof(struct ClientGetAuthHandle));
893 cah->client = client;
894 cah->unique_id = sh_msg->id;
895 if (strlen (name) < strlen(GNUNET_GNS_TLD))
897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
898 "GET_AUTH: `%s' is too short. Returning\n", name);
900 send_get_auth_response(cah, name);
903 if (strlen (name) > MAX_DNS_NAME_LENGTH)
905 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
906 "GET_AUTH: `%s' is too long", name);
908 send_get_auth_response(cah, name);
911 if (0 != strcmp (name + strlen (name) - strlen(GNUNET_GNS_TLD),
914 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
915 "GET_AUTH: %s is not our domain. Returning\n", name);
917 send_get_auth_response(cah, name);
921 if (0 == strcmp(name, GNUNET_GNS_TLD))
923 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
924 "GET_AUTH: %s is us. Returning\n", name);
926 send_get_auth_response(cah, name);
929 cah->name = GNUNET_malloc(strlen (name)
930 - strlen (GNUNET_GNS_TLD) + 1);
931 memset (cah->name, 0,
932 strlen (name) - strlen (GNUNET_GNS_TLD) + 1);
933 memcpy (cah->name, name,
934 strlen (name) - strlen(GNUNET_GNS_TLD));
936 /* Start delegation resolution in our namestore */
937 gns_resolver_get_authority(zone_hash, zone_hash, name, &send_get_auth_response, cah);
939 GNUNET_STATISTICS_update (statistics,
940 "Authority lookup attempts", 1, GNUNET_NO);
946 * Reply to client with the result from our lookup.
948 * @param cls the closure (our client lookup handle)
949 * @param rd_count the number of records
950 * @param rd the record data
953 send_lookup_response(void* cls,
955 const struct GNUNET_NAMESTORE_RecordData *rd)
957 struct ClientLookupHandle* clh = (struct ClientLookupHandle*)cls;
958 struct GNUNET_GNS_ClientLookupResultMessage *rmsg;
961 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message with %d results\n",
962 "LOOKUP_RESULT", rd_count);
964 len = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
965 rmsg = GNUNET_malloc(len+sizeof(struct GNUNET_GNS_ClientLookupResultMessage));
967 rmsg->id = clh->unique_id;
968 rmsg->rd_count = htonl(rd_count);
969 rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT);
971 htons(len+sizeof(struct GNUNET_GNS_ClientLookupResultMessage));
973 GNUNET_NAMESTORE_records_serialize (rd_count, rd, len, (char*)&rmsg[1]);
975 GNUNET_SERVER_notification_context_unicast (nc, clh->client,
976 (const struct GNUNET_MessageHeader *) rmsg,
978 GNUNET_SERVER_receive_done (clh->client, GNUNET_OK);
981 GNUNET_free(clh->name);
983 if (NULL != clh->shorten_key)
984 GNUNET_CRYPTO_rsa_key_free (clh->shorten_key);
988 GNUNET_STATISTICS_update (statistics,
989 "Completed lookups", 1, GNUNET_NO);
993 GNUNET_STATISTICS_update (statistics,
994 "Records resolved", rd_count, GNUNET_NO);
1001 * Handle lookup requests from client
1003 * @param cls the closure
1004 * @param client the client
1005 * @param message the message
1008 handle_lookup(void *cls,
1009 struct GNUNET_SERVER_Client * client,
1010 const struct GNUNET_MessageHeader * message)
1013 const struct GNUNET_GNS_ClientLookupMessage *sh_msg;
1015 char name[MAX_DNS_NAME_LENGTH];
1016 struct ClientLookupHandle *clh;
1017 char* nameptr = name;
1020 struct GNUNET_CRYPTO_RsaPrivateKey *key;
1021 struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded *pkey;
1024 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "LOOKUP");
1025 msg_size = ntohs(message->size);
1026 if (msg_size < sizeof (struct GNUNET_GNS_ClientLookupMessage))
1029 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1032 sh_msg = (const struct GNUNET_GNS_ClientLookupMessage *) message;
1033 GNUNET_SERVER_notification_context_add (nc, client);
1034 if (GNUNET_YES == ntohl (sh_msg->have_key))
1036 pkey = (struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded *)&sh_msg[1];
1037 tmp_pkey = (char*)&sh_msg[1];
1038 key = GNUNET_CRYPTO_rsa_decode_key (tmp_pkey, ntohs(pkey->len));
1039 GNUNET_STRINGS_utf8_tolower(&tmp_pkey[ntohs(pkey->len)], &nameptr);
1044 utf_in = (const char *) &sh_msg[1];
1045 if ('\0' != utf_in[msg_size - sizeof (struct GNUNET_GNS_ClientLookupMessage) - 1])
1048 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1051 GNUNET_STRINGS_utf8_tolower(utf_in, &nameptr);
1053 namelen = strlen (name)+1;
1054 clh = GNUNET_malloc (sizeof (struct ClientLookupHandle));
1055 memset (clh, 0, sizeof (struct ClientLookupHandle));
1056 clh->client = client;
1057 clh->name = GNUNET_malloc(namelen);
1058 strcpy(clh->name, name);
1059 clh->unique_id = sh_msg->id;
1060 clh->type = ntohl(sh_msg->type);
1061 clh->shorten_key = key;
1063 only_cached = ntohl(sh_msg->only_cached);
1065 if (strlen (name) > MAX_DNS_NAME_LENGTH) {
1066 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1067 "LOOKUP: %s is too long", name);
1069 send_lookup_response(clh, 0, NULL);
1073 if ((clh->type == GNUNET_GNS_RECORD_A) &&
1074 (GNUNET_OK != v4_enabled))
1076 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1077 "LOOKUP: Query for A record but AF_INET not supported!");
1079 send_lookup_response(clh, 0, NULL);
1083 if ((clh->type == GNUNET_GNS_RECORD_AAAA) &&
1084 (GNUNET_OK != v6_enabled))
1086 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1087 "LOOKUP: Query for AAAA record but AF_INET6 not supported!");
1089 send_lookup_response(clh, 0, NULL);
1093 if (1 == ntohl(sh_msg->use_default_zone))
1094 clh->zone = zone_hash; //Default zone
1096 clh->zone = sh_msg->zone;
1098 if (GNUNET_YES == auto_import_pkey)
1100 gns_resolver_lookup_record (clh->zone, clh->zone, clh->type, clh->name,
1102 default_lookup_timeout,
1104 &send_lookup_response, clh);
1108 gns_resolver_lookup_record (clh->zone, clh->zone, clh->type, name,
1110 default_lookup_timeout,
1112 &send_lookup_response, clh);
1115 GNUNET_STATISTICS_update (statistics,
1116 "Record lookup attempts", 1, GNUNET_NO);
1121 * Test if the given AF is supported by this system.
1124 * @return GNUNET_OK if the AF is supported
1131 s = socket (af, SOCK_STREAM, 0);
1134 if (EAFNOSUPPORT == errno)
1136 fprintf (stderr, "Failed to create test socket: %s\n", STRERROR (errno));
1137 return GNUNET_SYSERR;
1149 * Process GNS requests.
1151 * @param cls closure)
1152 * @param server the initialized server
1153 * @param c configuration to use
1156 run (void *cls, struct GNUNET_SERVER_Handle *server,
1157 const struct GNUNET_CONFIGURATION_Handle *c)
1160 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
1161 unsigned long long max_parallel_bg_queries = 0;
1162 unsigned long long default_lookup_timeout_secs = 0;
1163 int ignore_pending = GNUNET_NO;
1165 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1166 {&handle_shorten, NULL, GNUNET_MESSAGE_TYPE_GNS_SHORTEN, 0},
1167 {&handle_lookup, NULL, GNUNET_MESSAGE_TYPE_GNS_LOOKUP, 0},
1168 {&handle_get_authority, NULL, GNUNET_MESSAGE_TYPE_GNS_GET_AUTH, 0}
1171 v6_enabled = test_af (AF_INET6);
1172 v4_enabled = test_af (AF_INET);
1174 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (c, "gns",
1175 "ZONEKEY", &keyfile))
1177 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1178 "No private key for root zone specified!\n");
1179 GNUNET_SCHEDULER_shutdown ();
1183 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1184 "Using keyfile %s for root zone.\n", keyfile);
1186 zone_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
1187 GNUNET_CRYPTO_rsa_key_get_public (zone_key, &pkey);
1189 GNUNET_CRYPTO_short_hash(&pkey,
1190 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1192 GNUNET_free(keyfile);
1193 namestore_handle = GNUNET_NAMESTORE_connect (c);
1194 if (NULL == namestore_handle)
1196 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1197 _("Failed to connect to the namestore!\n"));
1198 GNUNET_SCHEDULER_shutdown ();
1202 auto_import_pkey = GNUNET_NO;
1204 GNUNET_CONFIGURATION_get_value_yesno (c, "gns",
1205 "AUTO_IMPORT_PKEY"))
1207 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1208 "Automatic PKEY import is enabled.\n");
1209 auto_import_pkey = GNUNET_YES;
1213 zone_iteration_interval = INITIAL_ZONE_ITERATION_INTERVAL;
1215 record_put_interval = DEFAULT_RECORD_PUT_INTERVAL;
1218 GNUNET_CONFIGURATION_get_value_time (c, "gns",
1219 "RECORD_PUT_INTERVAL",
1220 &record_put_interval))
1222 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1223 "Record put interval: %s\n",
1224 GNUNET_STRINGS_relative_time_to_string (record_put_interval, GNUNET_YES));
1228 GNUNET_CONFIGURATION_get_value_number (c, "gns",
1229 "MAX_PARALLEL_BACKGROUND_QUERIES",
1230 &max_parallel_bg_queries))
1232 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1233 "Number of allowed parallel background queries: %llu\n",
1234 max_parallel_bg_queries);
1238 GNUNET_CONFIGURATION_get_value_yesno (c, "gns",
1239 "AUTO_IMPORT_CONFIRMATION_REQ"))
1241 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1242 "Auto import requires user confirmation\n");
1243 ignore_pending = GNUNET_YES;
1247 GNUNET_CONFIGURATION_get_value_number(c, "gns",
1248 "DEFAULT_LOOKUP_TIMEOUT",
1249 &default_lookup_timeout_secs))
1251 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1252 "Default lookup timeout: %llus\n", default_lookup_timeout_secs);
1253 default_lookup_timeout = GNUNET_TIME_relative_multiply(
1254 GNUNET_TIME_UNIT_SECONDS,
1255 default_lookup_timeout_secs);
1261 dht_handle = GNUNET_DHT_connect(c,
1262 //max_parallel_bg_queries); //FIXME get ht_len from cfg
1265 if (NULL == dht_handle)
1267 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not connect to DHT!\n");
1270 if (gns_resolver_init(namestore_handle, dht_handle, zone_hash, c,
1271 max_parallel_bg_queries,
1275 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1276 "Unable to initialize resolver!\n");
1277 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
1282 GNUNET_CONFIGURATION_get_value_yesno (c, "gns", "HIJACK_DNS"))
1284 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1285 "DNS hijacking enabled... connecting to service.\n");
1287 if (gns_interceptor_init(zone_hash, zone_key, c) == GNUNET_SYSERR)
1289 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1290 "Failed to enable the dns interceptor!\n");
1295 * Schedule periodic put
1297 * We have roughly an hour for all records;
1299 first_zone_iteration = GNUNET_YES;
1300 zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_start, NULL);
1302 GNUNET_SERVER_add_handlers (server, handlers);
1305 //GNUNET_SERVER_disconnect_notify (server,
1306 // &client_disconnect_notification,
1309 statistics = GNUNET_STATISTICS_create ("gns", c);
1311 nc = GNUNET_SERVER_notification_context_create (server, 1);
1313 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
1319 * The main function for the GNS service.
1321 * @param argc number of arguments from the command line
1322 * @param argv command line arguments
1323 * @return 0 ok, 1 on error
1326 main (int argc, char *const *argv)
1332 GNUNET_SERVICE_run (argc, argv, "gns", GNUNET_SERVICE_OPTION_NONE, &run,
1337 /* end of gnunet-service-gns.c */