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"
41 /* FIXME move to proper header in include */
42 #define GNUNET_MESSAGE_TYPE_GNS_LOOKUP 23
43 #define GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT 24
44 #define GNUNET_MESSAGE_TYPE_GNS_SHORTEN 25
45 #define GNUNET_MESSAGE_TYPE_GNS_SHORTEN_RESULT 26
46 #define GNUNET_MESSAGE_TYPE_GNS_GET_AUTH 27
47 #define GNUNET_MESSAGE_TYPE_GNS_GET_AUTH_RESULT 28
50 #define INITIAL_ZONE_ITERATION_INTERVAL GNUNET_TIME_UNIT_MILLISECONDS
51 #define MINIMUM_ZONE_ITERATION_INTERVAL GNUNET_TIME_UNIT_SECONDS
52 #define DEFAULT_RECORD_PUT_INTERVAL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 4)
55 * Handle to a shorten operation from api
57 struct ClientShortenHandle
61 struct ClientShortenHandle *next;
64 struct ClientShortenHandle *prev;
66 /* the requesting client that */
67 struct GNUNET_SERVER_Client *client;
73 enum GNUNET_GNS_RecordType type;
76 char name[MAX_DNS_NAME_LENGTH];
78 /* name of private zone (relative to root) */
79 char private_zone_id[MAX_DNS_NAME_LENGTH];
81 /* name of shorten zone (relative to root) */
82 char shorten_zone_id[MAX_DNS_NAME_LENGTH];
85 struct GNUNET_CRYPTO_ShortHashCode root_zone;
88 struct GNUNET_CRYPTO_ShortHashCode private_zone;
91 struct GNUNET_CRYPTO_ShortHashCode shorten_zone;
93 /* Namestore lookup task */
94 struct GNUNET_NAMESTORE_QueueEntry *namestore_task;
100 * Handle to a get auhtority operation from api
102 struct ClientGetAuthHandle
104 /* the requesting client that */
105 struct GNUNET_SERVER_Client *client;
110 /* name to lookup authority */
117 * Handle to a lookup operation from api
119 struct ClientLookupHandle
122 /* the requesting client that */
123 struct GNUNET_SERVER_Client *client;
125 /* The zone we look up in */
126 struct GNUNET_CRYPTO_ShortHashCode zone;
128 /* Do we only want to lookup from local cache? */
135 enum GNUNET_GNS_RecordType type;
137 /* optional zone private key used for shorten */
138 struct GNUNET_CRYPTO_RsaPrivateKey *shorten_key;
140 /* the name to look up */
141 char* name; //Needed?
145 * Our handle to the DHT
147 static struct GNUNET_DHT_Handle *dht_handle;
150 * Our zone's private key
152 struct GNUNET_CRYPTO_RsaPrivateKey *zone_key;
155 * Our handle to the namestore service
156 * FIXME maybe need a second handle for iteration
158 struct GNUNET_NAMESTORE_Handle *namestore_handle;
161 * Handle to iterate over our authoritative zone in namestore
163 struct GNUNET_NAMESTORE_ZoneIterator *namestore_iter;
166 * The configuration the GNS service is running with
168 const struct GNUNET_CONFIGURATION_Handle *GNS_cfg;
171 * Our notification context.
173 static struct GNUNET_SERVER_NotificationContext *nc;
178 struct GNUNET_CRYPTO_ShortHashCode zone_hash;
181 * Useful for zone update for DHT put
183 static unsigned long long num_public_records;
186 * Last seen record count
188 static unsigned long long last_num_public_records;
190 static struct GNUNET_TIME_Relative zone_iteration_interval;
192 /* dht update interval FIXME define? */
193 static struct GNUNET_TIME_Relative record_put_interval;
195 /* zone update task */
196 GNUNET_SCHEDULER_TaskIdentifier zone_update_taskid = GNUNET_SCHEDULER_NO_TASK;
198 /* automatic pkey import for name shortening */
199 static int auto_import_pkey;
202 static struct GNUNET_TIME_Relative default_lookup_timeout;
205 static int v6_enabled;
208 static int v4_enabled;
210 /* Shorten DLL for cancelling NS requests */
211 static struct ClientShortenHandle *csh_head;
213 /* Shorten DLL for cancelling NS requests */
214 static struct ClientShortenHandle *csh_tail;
216 /* Statistics handle */
217 static struct GNUNET_STATISTICS_Handle *statistics;
220 * Send shorten response back to client
222 * @param cls the closure containing a client shorten handle
223 * @param name the shortened name result or NULL if cannot be shortened
226 send_shorten_response(void* cls, const char* name);
233 on_resolver_cleanup(void)
235 if (NULL != statistics)
236 GNUNET_STATISTICS_destroy (statistics, GNUNET_NO);
238 if (NULL != namestore_iter)
239 GNUNET_NAMESTORE_zone_iteration_stop (namestore_iter);
240 GNUNET_NAMESTORE_disconnect(namestore_handle);
241 GNUNET_DHT_disconnect(dht_handle);
245 * Task run during shutdown.
251 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
253 struct ClientShortenHandle *csh_tmp = csh_head;
255 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
258 while (csh_tmp != NULL)
260 GNUNET_CONTAINER_DLL_remove (csh_head, csh_tail, csh_tmp);
261 send_shorten_response (csh_tmp, csh_tmp->name);
264 GNUNET_SERVER_notification_context_destroy (nc);
266 gns_interceptor_stop();
267 gns_resolver_cleanup(&on_resolver_cleanup);
268 /* Kill zone task for it may make the scheduler hang */
269 if (zone_update_taskid != GNUNET_SCHEDULER_NO_TASK)
270 GNUNET_SCHEDULER_cancel(zone_update_taskid);
275 * Method called periodicattluy that triggers
276 * iteration over root zone
279 * @param tc task context
282 update_zone_dht_next(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
284 zone_update_taskid = GNUNET_SCHEDULER_NO_TASK;
285 GNUNET_NAMESTORE_zone_iterator_next (namestore_iter);
289 * Continuation for DHT put
292 * @param success GNUNET_OK if the PUT was transmitted,
293 * GNUNET_NO on timeout,
294 * GNUNET_SYSERR on disconnect from service
295 * after the PUT message was transmitted
296 * (so we don't know if it was received or not)
299 record_dht_put(void *cls, int success)
301 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "put request transmitted\n");
306 update_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
309 * Function used to put all records successively into the DHT.
311 * @param cls the closure (NULL)
312 * @param key the public key of the authority (ours)
313 * @param expiration lifetime of the namestore entry
314 * @param name the name of the records
315 * @param rd_count the number of records in data
316 * @param rd the record data
317 * @param signature the signature for the record data
320 put_gns_record(void *cls,
321 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
322 struct GNUNET_TIME_Absolute expiration,
324 unsigned int rd_count,
325 const struct GNUNET_NAMESTORE_RecordData *rd,
326 const struct GNUNET_CRYPTO_RsaSignature *signature)
329 struct GNSNameRecordBlock *nrb;
330 struct GNUNET_CRYPTO_ShortHashCode name_hash;
331 struct GNUNET_CRYPTO_ShortHashCode zhash;
332 struct GNUNET_HashCode xor_hash;
333 struct GNUNET_HashCode name_hash_double;
334 struct GNUNET_HashCode zone_hash_double;
335 uint32_t rd_payload_length;
336 char* nrb_data = NULL;
338 int interval_adjustment = 1;
339 struct GNUNET_TIME_Relative next_put_interval;
346 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
347 "Zone iteration finished. Rescheduling zone iteration\n");
349 namestore_iter = NULL;
350 zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_start, NULL);
351 GNUNET_STATISTICS_update (statistics,
352 "Number of zone iterations", 1, GNUNET_NO);
354 last_num_public_records = num_public_records;
355 GNUNET_STATISTICS_set (statistics,
356 "Number of public records in DHT",
357 last_num_public_records,
362 namelen = strlen(name) + 1;
366 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
367 "No records given for name %s! Skipping...\n",
369 zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_next,
374 if (signature == NULL)
376 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
377 "No signature for %s record data provided! Skipping...\n",
379 zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_next,
385 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
386 "Putting records for %s into the DHT\n", name);
388 rd_payload_length = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
390 nrb = GNUNET_malloc(rd_payload_length + namelen
391 + sizeof(struct GNSNameRecordBlock));
393 nrb->signature = *signature;
395 nrb->public_key = *key;
397 nrb->rd_count = htonl(rd_count);
399 memcpy(&nrb[1], name, namelen);
401 nrb_data = (char*)&nrb[1];
404 rd_payload_length += sizeof(struct GNSNameRecordBlock) + namelen;
406 if (-1 == GNUNET_NAMESTORE_records_serialize (rd_count,
411 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
412 "Record serialization failed! Skipping...\n");
414 zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_next,
421 * calculate DHT key: H(name) xor H(pubkey)
423 GNUNET_CRYPTO_short_hash(key,
424 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
426 GNUNET_CRYPTO_short_hash(name, strlen(name), &name_hash);
427 GNUNET_CRYPTO_short_hash_double (&name_hash, &name_hash_double);
428 GNUNET_CRYPTO_short_hash_double (&zhash, &zone_hash_double);
429 GNUNET_CRYPTO_hash_xor(&zone_hash_double, &name_hash_double, &xor_hash);
431 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
432 "zone identity: %s\n", GNUNET_h2s (&zone_hash_double));
434 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
435 "putting %d records for %s under key: %s with size %d\n",
436 rd_count, name, GNUNET_h2s (&xor_hash), rd_payload_length);
438 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
439 "DHT req to %d\n", DHT_OPERATION_TIMEOUT.rel_value);
441 GNUNET_STATISTICS_update (statistics,
442 "Records put into DHT", rd_count, GNUNET_NO);
443 GNUNET_STATISTICS_update (statistics,
444 "Record bytes put into DHT", rd_payload_length, GNUNET_NO);
446 /* FIXME: keep return value to possibly cancel? */
447 GNUNET_DHT_put (dht_handle, &xor_hash,
448 DHT_GNS_REPLICATION_LEVEL,
450 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
454 DHT_OPERATION_TIMEOUT,
456 NULL); //cls for cont
458 num_public_records++;
460 if (num_public_records > last_num_public_records)
462 interval_adjustment = ceil ((double)num_public_records / (double)last_num_public_records);
463 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
464 "Last record count was lower than current record count... increasing.\n");
465 next_put_interval = GNUNET_TIME_relative_divide (zone_iteration_interval,
466 interval_adjustment);
470 next_put_interval = zone_iteration_interval;
472 GNUNET_STATISTICS_update (statistics,
473 "Current zone iteration interval [msec]",
474 next_put_interval.rel_value,
478 * Reschedule periodic put
480 zone_update_taskid = GNUNET_SCHEDULER_add_delayed (next_put_interval,
481 &update_zone_dht_next,
489 * Periodically iterate over our zone and store everything in dht
492 * @param tc task context
495 update_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
497 zone_update_taskid = GNUNET_SCHEDULER_NO_TASK;
499 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Scheduling DHT zone update!\n");
501 if (0 == last_num_public_records)
504 * If no records are known (startup) or none present
505 * we can safely set the interval to 1s
507 zone_iteration_interval = INITIAL_ZONE_ITERATION_INTERVAL;
508 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
509 "No records in db. Adjusted zone iteration interval to %llums\n",
510 zone_iteration_interval.rel_value);
511 GNUNET_STATISTICS_set (statistics,
512 "Current zone iteration interval [msec]",
513 zone_iteration_interval.rel_value,
518 zone_iteration_interval = GNUNET_TIME_relative_divide (record_put_interval,
519 last_num_public_records);
520 zone_iteration_interval = GNUNET_TIME_relative_max (MINIMUM_ZONE_ITERATION_INTERVAL,
521 zone_iteration_interval);
523 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
524 "Adjusted zone iteration interval to %llus!\n",
525 zone_iteration_interval.rel_value);
526 GNUNET_STATISTICS_set (statistics,
527 "Current zone iteration interval [msec]",
528 zone_iteration_interval.rel_value,
532 /* start counting again */
533 num_public_records = 0;
534 namestore_iter = GNUNET_NAMESTORE_zone_iteration_start (namestore_handle,
536 GNUNET_NAMESTORE_RF_AUTHORITY,
537 GNUNET_NAMESTORE_RF_PRIVATE,
542 /* END DHT ZONE PROPAGATION */
545 * Send shorten response back to client
547 * @param cls the closure containing a client shorten handle
548 * @param name the shortened name result or NULL if cannot be shortened
551 send_shorten_response(void* cls, const char* name)
553 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message with %s\n",
554 "SHORTEN_RESULT", name);
555 struct GNUNET_GNS_ClientShortenResultMessage *rmsg;
556 struct ClientShortenHandle *csh = (struct ClientShortenHandle *)cls;
563 GNUNET_STATISTICS_update (statistics,
564 "Name shorten results", 1, GNUNET_NO);
566 rmsg = GNUNET_malloc(sizeof(struct GNUNET_GNS_ClientShortenResultMessage)
569 rmsg->id = csh->unique_id;
570 rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_SHORTEN_RESULT);
572 htons(sizeof(struct GNUNET_GNS_ClientShortenResultMessage) +
575 strcpy((char*)&rmsg[1], name);
577 GNUNET_SERVER_notification_context_unicast (nc, csh->client,
578 (const struct GNUNET_MessageHeader *) rmsg,
580 GNUNET_SERVER_receive_done (csh->client, GNUNET_OK);
582 if (NULL != csh->namestore_task)
583 GNUNET_NAMESTORE_cancel (csh->namestore_task);
592 * Lookup the zone infos and shorten name
594 * @param cls the client shorten handle
595 * @param key key of the zone
596 * @param expiration expiration of record
597 * @param name name found or null if no result
598 * @param rd_count number of records found
599 * @param rd record data
604 process_shorten_in_private_zone_lookup (void *cls,
605 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
606 struct GNUNET_TIME_Absolute expiration,
608 unsigned int rd_count,
609 const struct GNUNET_NAMESTORE_RecordData *rd,
610 const struct GNUNET_CRYPTO_RsaSignature *signature)
612 struct ClientShortenHandle *csh = cls;
613 csh->namestore_task = NULL;
614 struct GNUNET_CRYPTO_ShortHashCode *szone = &csh->shorten_zone;
615 struct GNUNET_CRYPTO_ShortHashCode *pzone = &csh->private_zone;
617 if (0 == strcmp (csh->private_zone_id, ""))
622 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
623 "No shorten zone in private zone!\n");
625 strcpy (csh->shorten_zone_id, "");
632 GNUNET_assert (rd_count == 1);
634 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
635 "Shorten zone %s found in private zone %s\n",
636 name, csh->private_zone_id);
638 sprintf (csh->shorten_zone_id, "%s.%s", name, csh->private_zone_id);
641 GNUNET_CONTAINER_DLL_remove (csh_head, csh_tail, csh);
643 gns_resolver_shorten_name (&csh->root_zone,
647 csh->private_zone_id,
648 csh->shorten_zone_id,
649 &send_shorten_response, csh);
655 * Lookup the zone infos and shorten name
657 * @param cls the shorten handle
658 * @param key key of the zone
659 * @param expiration expiration of record
660 * @param name name found or null if no result
661 * @param rd_count number of records found
662 * @param rd record data
667 process_shorten_in_root_zone_lookup (void *cls,
668 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
669 struct GNUNET_TIME_Absolute expiration,
671 unsigned int rd_count,
672 const struct GNUNET_NAMESTORE_RecordData *rd,
673 const struct GNUNET_CRYPTO_RsaSignature *signature)
675 struct ClientShortenHandle *csh = cls;
676 csh->namestore_task = NULL;
677 struct GNUNET_CRYPTO_ShortHashCode *szone = &csh->shorten_zone;
678 struct GNUNET_CRYPTO_ShortHashCode *pzone = &csh->private_zone;
680 if (0 == strcmp (csh->private_zone_id, ""))
685 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
686 "No shorten zone in zone and no private zone!\n");
688 strcpy (csh->shorten_zone_id, "");
690 GNUNET_CONTAINER_DLL_remove (csh_head, csh_tail, csh);
693 gns_resolver_shorten_name (&csh->root_zone,
697 csh->private_zone_id,
698 csh->shorten_zone_id,
699 &send_shorten_response, csh);
703 GNUNET_assert (rd_count == 1);
705 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
706 "Private zone %s found in root zone\n", name);
708 strcpy (csh->private_zone_id, name);
710 csh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
713 &process_shorten_in_private_zone_lookup,
719 * Lookup the zone infos and shorten name
721 * @param cls the shorten handle
722 * @param key key of the zone
723 * @param expiration expiration of record
724 * @param name name found or null if no result
725 * @param rd_count number of records found
726 * @param rd record data
731 process_private_in_root_zone_lookup (void *cls,
732 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
733 struct GNUNET_TIME_Absolute expiration,
735 unsigned int rd_count,
736 const struct GNUNET_NAMESTORE_RecordData *rd,
737 const struct GNUNET_CRYPTO_RsaSignature *signature)
739 struct ClientShortenHandle *csh = cls;
740 csh->namestore_task = NULL;
744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
745 "No private zone in root zone\n");
747 strcpy (csh->private_zone_id, "");
749 csh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
752 &process_shorten_in_root_zone_lookup,
757 GNUNET_assert (rd_count == 1);
759 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
760 "Private zone %s found in root zone\n", name);
762 strcpy (csh->private_zone_id, name);
764 csh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
767 &process_shorten_in_private_zone_lookup,
772 * Lookup the zone infos and shorten name
774 * @param csh the shorten handle
778 start_shorten_name (struct ClientShortenHandle *csh)
780 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
781 "Looking for private zone name in root zone\n");
783 csh->namestore_task = GNUNET_NAMESTORE_zone_to_name (namestore_handle,
786 &process_private_in_root_zone_lookup,
792 * Handle a shorten message from the api
794 * @param cls the closure
795 * @param client the client
796 * @param message the message
798 static void handle_shorten (void *cls,
799 struct GNUNET_SERVER_Client * client,
800 const struct GNUNET_MessageHeader * message)
802 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "SHORTEN");
805 struct ClientShortenHandle *csh;
806 char name[MAX_DNS_NAME_LENGTH];
807 char* nameptr = name;
809 if (ntohs (message->size) < sizeof (struct GNUNET_GNS_ClientShortenMessage))
812 GNUNET_SERVER_receive_done (client, GNUNET_OK);
817 struct GNUNET_GNS_ClientShortenMessage *sh_msg =
818 (struct GNUNET_GNS_ClientShortenMessage *) message;
820 msg_size = ntohs(message->size);
822 if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
825 GNUNET_SERVER_receive_done (client, GNUNET_OK);
829 csh = GNUNET_malloc(sizeof(struct ClientShortenHandle));
830 csh->client = client;
831 csh->unique_id = sh_msg->id;
833 GNUNET_CONTAINER_DLL_insert (csh_head, csh_tail, csh);
835 GNUNET_STRINGS_utf8_tolower((char*)&sh_msg[1], &nameptr);
837 if (strlen (name) < strlen(GNUNET_GNS_TLD)) {
838 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
839 "SHORTEN: %s is too short", name);
840 send_shorten_response(csh, name);
844 if (strlen (name) > MAX_DNS_NAME_LENGTH) {
845 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
846 "SHORTEN: %s is too long", name);
847 send_shorten_response(csh, name);
851 if (!is_gnunet_tld(name) && !is_zkey_tld(name))
853 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
854 "%s is not our domain. Returning\n", name);
855 send_shorten_response(csh, name);
859 csh->shorten_zone = sh_msg->shorten_zone;
860 csh->private_zone = sh_msg->private_zone;
862 strcpy (csh->name, name);
864 GNUNET_SERVER_notification_context_add (nc, client);
866 if (1 == ntohl(sh_msg->use_default_zone))
867 csh->root_zone = zone_hash; //Default zone
869 csh->root_zone = sh_msg->zone;
871 start_shorten_name (csh);
873 GNUNET_STATISTICS_update (statistics,
874 "Name shorten attempts", 1, GNUNET_NO);
880 * Send get authority response back to client
882 * @param cls the closure containing a client get auth handle
883 * @param name the shortened name result or NULL if cannot be shortened
886 send_get_auth_response(void *cls, const char* name)
888 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message with %s\n",
889 "GET_AUTH_RESULT", name);
890 struct GNUNET_GNS_ClientGetAuthResultMessage *rmsg;
891 struct ClientGetAuthHandle *cah = (struct ClientGetAuthHandle *)cls;
895 GNUNET_STATISTICS_update (statistics,
896 "Authorities resolved", 1, GNUNET_NO);
904 rmsg = GNUNET_malloc(sizeof(struct GNUNET_GNS_ClientGetAuthResultMessage)
907 rmsg->id = cah->unique_id;
908 rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_GET_AUTH_RESULT);
910 htons(sizeof(struct GNUNET_GNS_ClientGetAuthResultMessage) +
913 strcpy((char*)&rmsg[1], name);
915 GNUNET_SERVER_notification_context_unicast (nc, cah->client,
916 (const struct GNUNET_MessageHeader *) rmsg,
918 GNUNET_SERVER_receive_done (cah->client, GNUNET_OK);
921 GNUNET_free_non_null(cah->name);
930 * Handle a get authority message from the api
932 * @param cls the closure
933 * @param client the client
934 * @param message the message
936 static void handle_get_authority(void *cls,
937 struct GNUNET_SERVER_Client * client,
938 const struct GNUNET_MessageHeader * message)
940 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "GET_AUTH");
943 struct ClientGetAuthHandle *cah;
944 char name[MAX_DNS_NAME_LENGTH];
945 char* nameptr = name;
948 if (ntohs (message->size) < sizeof (struct GNUNET_GNS_ClientGetAuthMessage))
951 GNUNET_SERVER_receive_done (client, GNUNET_OK);
955 GNUNET_SERVER_notification_context_add (nc, client);
957 struct GNUNET_GNS_ClientGetAuthMessage *sh_msg =
958 (struct GNUNET_GNS_ClientGetAuthMessage *) message;
960 msg_size = ntohs(message->size);
962 if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
965 GNUNET_SERVER_receive_done (client, GNUNET_OK);
969 GNUNET_STRINGS_utf8_tolower((char*)&sh_msg[1], &nameptr);
972 cah = GNUNET_malloc(sizeof(struct ClientGetAuthHandle));
973 cah->client = client;
974 cah->unique_id = sh_msg->id;
976 if (strlen(name) < strlen(GNUNET_GNS_TLD))
978 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
979 "GET_AUTH: %s is too short. Returning\n", name);
981 send_get_auth_response(cah, name);
985 if (strlen (name) > MAX_DNS_NAME_LENGTH) {
986 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
987 "GET_AUTH: %s is too long", name);
989 send_get_auth_response(cah, name);
993 if (strcmp(name+strlen(name)-strlen(GNUNET_GNS_TLD),
994 GNUNET_GNS_TLD) != 0)
996 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
997 "GET_AUTH: %s is not our domain. Returning\n", name);
999 send_get_auth_response(cah, name);
1003 if (strcmp(name, GNUNET_GNS_TLD) == 0)
1005 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1006 "GET_AUTH: %s is us. Returning\n", name);
1008 send_get_auth_response(cah, name);
1012 cah->name = GNUNET_malloc(strlen(name)
1013 - strlen(GNUNET_GNS_TLD) + 1);
1014 memset(cah->name, 0,
1015 strlen(name)-strlen(GNUNET_GNS_TLD) + 1);
1016 memcpy(cah->name, name,
1017 strlen(name)-strlen(GNUNET_GNS_TLD));
1019 /* Start delegation resolution in our namestore */
1020 gns_resolver_get_authority(zone_hash, zone_hash, name, &send_get_auth_response, cah);
1022 GNUNET_STATISTICS_update (statistics,
1023 "Authority lookup attempts", 1, GNUNET_NO);
1029 * Reply to client with the result from our lookup.
1031 * @param cls the closure (our client lookup handle)
1032 * @param rd_count the number of records
1033 * @param rd the record data
1036 send_lookup_response(void* cls,
1038 const struct GNUNET_NAMESTORE_RecordData *rd)
1040 struct ClientLookupHandle* clh = (struct ClientLookupHandle*)cls;
1041 struct GNUNET_GNS_ClientLookupResultMessage *rmsg;
1044 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message with %d results\n",
1045 "LOOKUP_RESULT", rd_count);
1047 len = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
1048 rmsg = GNUNET_malloc(len+sizeof(struct GNUNET_GNS_ClientLookupResultMessage));
1050 rmsg->id = clh->unique_id;
1051 rmsg->rd_count = htonl(rd_count);
1052 rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT);
1054 htons(len+sizeof(struct GNUNET_GNS_ClientLookupResultMessage));
1056 GNUNET_NAMESTORE_records_serialize (rd_count, rd, len, (char*)&rmsg[1]);
1058 GNUNET_SERVER_notification_context_unicast (nc, clh->client,
1059 (const struct GNUNET_MessageHeader *) rmsg,
1061 GNUNET_SERVER_receive_done (clh->client, GNUNET_OK);
1064 GNUNET_free(clh->name);
1066 if (NULL != clh->shorten_key)
1067 GNUNET_free(clh->shorten_key);
1071 GNUNET_STATISTICS_update (statistics,
1072 "Completed lookups", 1, GNUNET_NO);
1076 GNUNET_STATISTICS_update (statistics,
1077 "Records resolved", rd_count, GNUNET_NO);
1084 * Handle lookup requests from client
1086 * @param cls the closure
1087 * @param client the client
1088 * @param message the message
1091 handle_lookup(void *cls,
1092 struct GNUNET_SERVER_Client * client,
1093 const struct GNUNET_MessageHeader * message)
1095 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "LOOKUP");
1097 size_t msg_size = 0;
1099 char name[MAX_DNS_NAME_LENGTH];
1100 struct ClientLookupHandle *clh;
1101 char* nameptr = name;
1103 struct GNUNET_CRYPTO_RsaPrivateKey *key;
1104 struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded *pkey;
1107 if (ntohs (message->size) < sizeof (struct GNUNET_GNS_ClientLookupMessage))
1109 GNUNET_break_op (0);
1110 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1114 GNUNET_SERVER_notification_context_add (nc, client);
1116 struct GNUNET_GNS_ClientLookupMessage *sh_msg =
1117 (struct GNUNET_GNS_ClientLookupMessage *) message;
1119 msg_size = ntohs(message->size);
1121 if (msg_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
1123 GNUNET_break_op (0);
1124 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1128 if (1 == ntohl(sh_msg->have_key))
1130 pkey = (struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded *)&sh_msg[1];
1131 tmp_pkey = (char*)&sh_msg[1];
1132 key = GNUNET_CRYPTO_rsa_decode_key (tmp_pkey, ntohs(pkey->len));
1133 GNUNET_STRINGS_utf8_tolower(&tmp_pkey[ntohs(pkey->len)], &nameptr);
1138 GNUNET_STRINGS_utf8_tolower((char*)&sh_msg[1], &nameptr);
1141 namelen = strlen(name)+1;
1142 clh = GNUNET_malloc (sizeof (struct ClientLookupHandle));
1143 memset (clh, 0, sizeof (struct ClientLookupHandle));
1144 clh->client = client;
1145 clh->name = GNUNET_malloc(namelen);
1146 strcpy(clh->name, name);
1147 clh->unique_id = sh_msg->id;
1148 clh->type = ntohl(sh_msg->type);
1149 clh->shorten_key = key;
1151 only_cached = ntohl(sh_msg->only_cached);
1153 if (strlen (name) > MAX_DNS_NAME_LENGTH) {
1154 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1155 "LOOKUP: %s is too long", name);
1157 send_lookup_response(clh, 0, NULL);
1161 if ((clh->type == GNUNET_GNS_RECORD_A) &&
1162 (GNUNET_OK != v4_enabled))
1164 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1165 "LOOKUP: Query for A record but AF_INET not supported!");
1167 send_lookup_response(clh, 0, NULL);
1171 if ((clh->type == GNUNET_GNS_RECORD_AAAA) &&
1172 (GNUNET_OK != v6_enabled))
1174 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1175 "LOOKUP: Query for AAAA record but AF_INET6 not supported!");
1177 send_lookup_response(clh, 0, NULL);
1181 if (1 == ntohl(sh_msg->use_default_zone))
1182 clh->zone = zone_hash; //Default zone
1184 clh->zone = sh_msg->zone;
1186 if (GNUNET_YES == auto_import_pkey)
1188 gns_resolver_lookup_record (clh->zone, clh->zone, clh->type, clh->name,
1190 default_lookup_timeout,
1192 &send_lookup_response, clh);
1196 gns_resolver_lookup_record (clh->zone, clh->zone, clh->type, name,
1198 default_lookup_timeout,
1200 &send_lookup_response, clh);
1203 GNUNET_STATISTICS_update (statistics,
1204 "Record lookup attempts", 1, GNUNET_NO);
1208 * Test if the given AF is supported by this system.
1211 * @return GNUNET_OK if the AF is supported
1218 s = socket (af, SOCK_STREAM, 0);
1221 if (EAFNOSUPPORT == errno)
1223 fprintf (stderr, "Failed to create test socket: %s\n", STRERROR (errno));
1224 return GNUNET_SYSERR;
1235 * Process GNS requests.
1237 * @param cls closure)
1238 * @param server the initialized server
1239 * @param c configuration to use
1242 run (void *cls, struct GNUNET_SERVER_Handle *server,
1243 const struct GNUNET_CONFIGURATION_Handle *c)
1246 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Initializing GNS\n");
1249 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
1250 unsigned long long max_parallel_bg_queries = 0;
1251 unsigned long long default_lookup_timeout_secs = 0;
1252 int ignore_pending = GNUNET_NO;
1254 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1255 {&handle_shorten, NULL, GNUNET_MESSAGE_TYPE_GNS_SHORTEN, 0},
1256 {&handle_lookup, NULL, GNUNET_MESSAGE_TYPE_GNS_LOOKUP, 0},
1257 {&handle_get_authority, NULL, GNUNET_MESSAGE_TYPE_GNS_GET_AUTH, 0}
1262 v6_enabled = test_af (AF_INET6);
1263 v4_enabled = test_af (AF_INET);
1265 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (c, "gns",
1266 "ZONEKEY", &keyfile))
1268 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1269 "No private key for root zone specified!\n");
1270 GNUNET_SCHEDULER_shutdown ();
1274 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1275 "Using keyfile %s for root zone.\n", keyfile);
1277 zone_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
1278 GNUNET_CRYPTO_rsa_key_get_public (zone_key, &pkey);
1280 GNUNET_CRYPTO_short_hash(&pkey,
1281 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1283 GNUNET_free(keyfile);
1286 * handle to our local namestore
1288 namestore_handle = GNUNET_NAMESTORE_connect(c);
1290 if (NULL == namestore_handle)
1292 //FIXME do error handling;
1293 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1294 "Failed to connect to the namestore!\n");
1295 GNUNET_SCHEDULER_shutdown ();
1301 auto_import_pkey = GNUNET_NO;
1304 GNUNET_CONFIGURATION_get_value_yesno (c, "gns",
1305 "AUTO_IMPORT_PKEY"))
1307 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1308 "Automatic PKEY import is enabled.\n");
1309 auto_import_pkey = GNUNET_YES;
1313 zone_iteration_interval = INITIAL_ZONE_ITERATION_INTERVAL; // yuck
1315 record_put_interval = DEFAULT_RECORD_PUT_INTERVAL;
1318 GNUNET_CONFIGURATION_get_value_time (c, "gns",
1319 "RECORD_PUT_INTERVAL",
1320 &record_put_interval))
1322 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1323 "Record put interval: %llu\n",
1324 record_put_interval);
1328 GNUNET_CONFIGURATION_get_value_number (c, "gns",
1329 "MAX_PARALLEL_BACKGROUND_QUERIES",
1330 &max_parallel_bg_queries))
1332 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1333 "Number of allowed parallel background queries: %llu\n",
1334 max_parallel_bg_queries);
1338 GNUNET_CONFIGURATION_get_value_yesno (c, "gns",
1339 "AUTO_IMPORT_CONFIRMATION_REQ"))
1341 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1342 "Auto import requires user confirmation\n");
1343 ignore_pending = GNUNET_YES;
1347 GNUNET_CONFIGURATION_get_value_number(c, "gns",
1348 "DEFAULT_LOOKUP_TIMEOUT",
1349 &default_lookup_timeout_secs))
1351 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1352 "Default lookup timeout: %llus\n", default_lookup_timeout_secs);
1353 default_lookup_timeout = GNUNET_TIME_relative_multiply(
1354 GNUNET_TIME_UNIT_SECONDS,
1355 default_lookup_timeout_secs);
1361 dht_handle = GNUNET_DHT_connect(c,
1362 //max_parallel_bg_queries); //FIXME get ht_len from cfg
1365 if (NULL == dht_handle)
1367 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not connect to DHT!\n");
1370 if (gns_resolver_init(namestore_handle, dht_handle, zone_hash, c,
1371 max_parallel_bg_queries,
1375 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1376 "Unable to initialize resolver!\n");
1377 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
1382 GNUNET_CONFIGURATION_get_value_yesno (c, "gns", "HIJACK_DNS"))
1384 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1385 "DNS hijacking enabled... connecting to service.\n");
1387 if (gns_interceptor_init(zone_hash, zone_key, c) == GNUNET_SYSERR)
1389 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1390 "Failed to enable the dns interceptor!\n");
1395 * Schedule periodic put
1397 * We have roughly an hour for all records;
1399 zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_start, NULL);
1401 GNUNET_SERVER_add_handlers (server, handlers);
1404 //GNUNET_SERVER_disconnect_notify (server,
1405 // &client_disconnect_notification,
1408 statistics = GNUNET_STATISTICS_create ("gns", c);
1410 nc = GNUNET_SERVER_notification_context_create (server, 1);
1412 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
1418 * The main function for the GNS service.
1420 * @param argc number of arguments from the command line
1421 * @param argv command line arguments
1422 * @return 0 ok, 1 on error
1425 main (int argc, char *const *argv)
1431 GNUNET_SERVICE_run (argc, argv, "gns", GNUNET_SERVICE_OPTION_NONE, &run,
1436 /* end of gnunet-service-gns.c */