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.
24 * - Write xquery and block plugin
25 * - The smaller FIXME issues all around
27 * @file gns/gnunet-service-gns.c
28 * @brief GNUnet GNS service
29 * @author Martin Schanzenbach
32 #include "gnunet_util_lib.h"
33 #include "gnunet_transport_service.h"
34 #include "gnunet_dns_service.h"
35 #include "gnunet_dnsparser_lib.h"
36 #include "gnunet_dht_service.h"
37 #include "gnunet_namestore_service.h"
38 #include "gnunet_gns_service.h"
39 #include "block_gns.h"
42 #define DHT_OPERATION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
43 #define DHT_LOOKUP_TIMEOUT DHT_OPERATION_TIMEOUT
44 #define DHT_GNS_REPLICATION_LEVEL 5
46 /* Ignore for now not used anyway and probably never will */
47 #define GNUNET_MESSAGE_TYPE_GNS_CLIENT_LOOKUP 23
48 #define GNUNET_MESSAGE_TYPE_GNS_CLIENT_RESULT 24
51 * Handle to a currenty pending resolution
53 struct GNUNET_GNS_ResolverHandle
55 /* The name to resolve */
58 /* the request handle to reply to */
59 struct GNUNET_DNS_RequestHandle *request_handle;
61 /* the dns parser packet received */
62 struct GNUNET_DNSPARSER_Packet *packet;
64 /* the query parsed from the packet */
66 struct GNUNET_DNSPARSER_Query *query;
68 /* has this query been answered? how many matches */
71 /* the authoritative zone to query */
72 GNUNET_HashCode authority;
74 /* the name of the authoritative zone to query */
78 * we have an authority in namestore that
79 * may be able to resolve
83 /* a handle for dht lookups. should be NULL if no lookups are in progress */
84 struct GNUNET_DHT_GetHandle *get_handle;
86 /* timeout task for dht lookups */
87 GNUNET_SCHEDULER_TaskIdentifier dht_timeout_task;
93 * Our handle to the DNS handler library
95 struct GNUNET_DNS_Handle *dns_handle;
98 * Our handle to the DHT
100 struct GNUNET_DHT_Handle *dht_handle;
103 * Our zone's private key
105 struct GNUNET_CRYPTO_RsaPrivateKey *zone_key;
108 * Our handle to the namestore service
109 * FIXME maybe need a second handle for iteration
111 struct GNUNET_NAMESTORE_Handle *namestore_handle;
114 * Handle to iterate over our authoritative zone in namestore
116 struct GNUNET_NAMESTORE_ZoneIterator *namestore_iter;
119 * The configuration the GNS service is running with
121 const struct GNUNET_CONFIGURATION_Handle *GNS_cfg;
124 * Our notification context.
126 static struct GNUNET_SERVER_NotificationContext *nc;
131 GNUNET_HashCode zone_hash;
134 * Our tld. Maybe get from config file
136 const char* gnunet_tld = ".gnunet";
139 * Useful for zone update for DHT put
141 static int num_public_records = 3600;
142 struct GNUNET_TIME_Relative dht_update_interval;
143 GNUNET_SCHEDULER_TaskIdentifier zone_update_taskid = GNUNET_SCHEDULER_NO_TASK;
146 static void reply_to_dns(struct GNUNET_GNS_ResolverHandle *answer,
148 const struct GNUNET_NAMESTORE_RecordData *rd);
149 static void resolve_name(struct GNUNET_GNS_ResolverHandle *rh);
152 * Task run during shutdown.
158 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
160 /* Kill zone task for it may make the scheduler hang */
161 GNUNET_SCHEDULER_cancel(zone_update_taskid);
163 GNUNET_DNS_disconnect(dns_handle);
164 GNUNET_NAMESTORE_disconnect(namestore_handle, 1);
165 GNUNET_DHT_disconnect(dht_handle);
169 * Callback when record data is put into namestore
171 * @param cls the closure
172 * @param success GNUNET_OK on success
173 * @param emsg the error message. NULL if SUCCESS==GNUNET_OK
176 on_namestore_record_put_result(void *cls,
180 if (GNUNET_NO == success)
182 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "records already in namestore\n");
185 else if (GNUNET_YES == success)
187 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
188 "records successfully put in namestore\n");
192 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
193 "Error putting records into namestore: %s\n", emsg);
197 * Handle timeout for DHT requests
199 * @param cls the request handle as closure
200 * @param tc the task context
203 dht_lookup_timeout(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
205 struct GNUNET_GNS_ResolverHandle *rh = cls;
207 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
208 "dht lookup for query %s (type=%d) timed out.\n",
209 rh->name, rh->query->type);
211 GNUNET_DHT_get_stop (rh->get_handle);
212 reply_to_dns(rh, 0, NULL);
216 * Function called when we get a result from the dht
219 * @param cls the request handle
220 * @param exp lifetime
221 * @param key the key the record was stored under
222 * @param get_path get path
223 * @param get_path_length get path length
224 * @param put_path put path
225 * @param put_path_length put path length
226 * @param type the block type
227 * @param size the size of the record
228 * @param data the record data
231 process_authority_dht_result(void* cls,
232 struct GNUNET_TIME_Absolute exp,
233 const GNUNET_HashCode * key,
234 const struct GNUNET_PeerIdentity *get_path,
235 unsigned int get_path_length,
236 const struct GNUNET_PeerIdentity *put_path,
237 unsigned int put_path_length,
238 enum GNUNET_BLOCK_Type type,
239 size_t size, const void *data)
241 struct GNUNET_GNS_ResolverHandle *rh;
242 struct GNSNameRecordBlock *nrb;
243 uint32_t num_records;
245 char* rd_data = (char*) data;
248 GNUNET_HashCode zone, name_hash;
253 //FIXME check expiration?
255 rh = (struct GNUNET_GNS_ResolverHandle *)cls;
256 nrb = (struct GNSNameRecordBlock*)data;
258 /* stop dht lookup and timeout task */
259 GNUNET_DHT_get_stop (rh->get_handle);
260 GNUNET_SCHEDULER_cancel(rh->dht_timeout_task);
262 rh->get_handle = NULL;
263 num_records = ntohl(nrb->rd_count);
264 name = (char*)&nrb[1];
266 struct GNUNET_NAMESTORE_RecordData rd[num_records];
268 rd_data += strlen(name) + sizeof(struct GNSNameRecordBlock);
269 rd_size = size - strlen(name) - sizeof(struct GNSNameRecordBlock);
271 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
276 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Error deserializing data!\n");
280 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
281 "Got name: %s (wanted %s)\n", name, rh->authority_name);
282 for (i=0; i<num_records; i++)
285 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
286 "Got name: %s (wanted %s)\n", name, rh->authority_name);
287 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
288 "Got type: %d (wanted %d)\n",
289 rd[i].record_type, GNUNET_GNS_RECORD_PKEY);
290 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
291 "Got data length: %d\n", rd[i].data_size);
292 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
293 "Got flag %d\n", rd[i].flags);
295 if ((strcmp(name, rh->authority_name) == 0) &&
296 (rd[i].record_type == GNUNET_GNS_RECORD_PKEY))
298 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Authority found in DHT\n");
301 (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *)rd[i].data,
309 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
310 GNUNET_CRYPTO_hash_xor(key, &name_hash, &zone);
312 /* Save to namestore */
313 if (0 == GNUNET_CRYPTO_hash_cmp(&zone_hash, &zone))
315 GNUNET_NAMESTORE_record_put (namestore_handle,
322 &on_namestore_record_put_result, //cont
333 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "No authority in records\n");
334 reply_to_dns(rh, 0, NULL);
338 * Start DHT lookup for a name -> PKEY (compare NS) record in
339 * query->authority's zone
341 * @param rh the pending gns query
342 * @param name the name of the PKEY record
345 resolve_authority_dht(struct GNUNET_GNS_ResolverHandle *rh)
348 GNUNET_HashCode name_hash;
349 GNUNET_HashCode lookup_key;
351 GNUNET_CRYPTO_hash(rh->authority_name,
352 strlen(rh->authority_name),
354 GNUNET_CRYPTO_hash_xor(&name_hash, &rh->authority, &lookup_key);
356 rh->dht_timeout_task = GNUNET_SCHEDULER_add_delayed (DHT_LOOKUP_TIMEOUT,
357 &dht_lookup_timeout, rh);
359 xquery = htonl(GNUNET_GNS_RECORD_PKEY);
360 //FIXME how long to wait for results?
361 rh->get_handle = GNUNET_DHT_get_start(dht_handle,
362 DHT_OPERATION_TIMEOUT,
363 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
365 DHT_GNS_REPLICATION_LEVEL,
369 &process_authority_dht_result,
375 * Function called when we get a result from the dht
378 * @param cls the request handle
379 * @param exp lifetime
380 * @param key the key the record was stored under
381 * @param get_path get path
382 * @param get_path_length get path length
383 * @param put_path put path
384 * @param put_path_length put path length
385 * @param type the block type
386 * @param size the size of the record
387 * @param data the record data
390 process_name_dht_result(void* cls,
391 struct GNUNET_TIME_Absolute exp,
392 const GNUNET_HashCode * key,
393 const struct GNUNET_PeerIdentity *get_path,
394 unsigned int get_path_length,
395 const struct GNUNET_PeerIdentity *put_path,
396 unsigned int put_path_length,
397 enum GNUNET_BLOCK_Type type,
398 size_t size, const void *data)
400 struct GNUNET_GNS_ResolverHandle *rh;
401 struct GNSNameRecordBlock *nrb;
402 uint32_t num_records;
404 char* rd_data = (char*)data;
408 GNUNET_HashCode zone, name_hash;
409 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "got dht result (size=%d)\n", size);
414 //FIXME maybe check expiration here, check block type
416 rh = (struct GNUNET_GNS_ResolverHandle *)cls;
417 nrb = (struct GNSNameRecordBlock*)data;
419 /* stop lookup and timeout task */
420 GNUNET_DHT_get_stop (rh->get_handle);
421 GNUNET_SCHEDULER_cancel(rh->dht_timeout_task);
423 rh->get_handle = NULL;
424 name = (char*)&nrb[1];
425 num_records = ntohl(nrb->rd_count);
427 struct GNUNET_NAMESTORE_RecordData rd[num_records];
429 rd_data += strlen(name) + sizeof(struct GNSNameRecordBlock);
430 rd_size = size - strlen(name) - sizeof(struct GNSNameRecordBlock);
432 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (rd_size,
437 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Error deserializing data!\n");
441 for (i=0; i<num_records; i++)
443 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
444 "Got name: %s (wanted %s)\n", name, rh->name);
445 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
446 "Got type: %d (wanted %d)\n",
447 rd[i].record_type, rh->query->type);
448 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
449 "Got data length: %d\n", rd[i].data_size);
450 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
451 "Got flag %d\n", rd[i].flags);
453 if ((strcmp(name, rh->name) == 0) &&
454 (rd[i].record_type == rh->query->type))
461 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
462 GNUNET_CRYPTO_hash_xor(key, &name_hash, &zone);
465 * FIXME check pubkey against existing key in namestore?
466 * https://gnunet.org/bugs/view.php?id=2179
469 /* Save to namestore */
470 GNUNET_NAMESTORE_record_put (namestore_handle,
477 &on_namestore_record_put_result, //cont
481 reply_to_dns(rh, num_records, rd);
483 reply_to_dns(rh, 0, NULL);
492 * Start DHT lookup for a (name -> query->record_type) record in
493 * query->authority's zone
495 * @param rh the pending gns query context
496 * @param name the name to query record
499 resolve_name_dht(struct GNUNET_GNS_ResolverHandle *rh, const char* name)
502 GNUNET_HashCode name_hash;
503 GNUNET_HashCode lookup_key;
504 struct GNUNET_CRYPTO_HashAsciiEncoded lookup_key_string;
506 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
507 GNUNET_CRYPTO_hash_xor(&name_hash, &rh->authority, &lookup_key);
508 GNUNET_CRYPTO_hash_to_enc (&lookup_key, &lookup_key_string);
510 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
511 "starting dht lookup for %s with key: %s\n",
512 name, (char*)&lookup_key_string);
514 rh->dht_timeout_task = GNUNET_SCHEDULER_add_delayed(DHT_LOOKUP_TIMEOUT,
515 &dht_lookup_timeout, rh);
517 xquery = htonl(rh->query->type);
518 //FIXME how long to wait for results?
519 rh->get_handle = GNUNET_DHT_get_start(dht_handle,
520 DHT_OPERATION_TIMEOUT,
521 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
523 DHT_GNS_REPLICATION_LEVEL,
527 &process_name_dht_result,
534 resolve_name(struct GNUNET_GNS_ResolverHandle *rh);
537 * This is a callback function that should give us only PKEY
538 * records. Used to query the namestore for the authority (PKEY)
541 * @param cls the pending query
542 * @param key the key of the zone we did the lookup
543 * @param expiration expiration date of the record data set in the namestore
544 * @param name the name for which we need an authority
545 * @param rd_count the number of records with 'name'
546 * @param rd the record data
547 * @param signature the signature of the authority for the record data
550 process_authority_lookup(void* cls,
551 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
552 struct GNUNET_TIME_Absolute expiration,
554 unsigned int rd_count,
555 const struct GNUNET_NAMESTORE_RecordData *rd,
556 const struct GNUNET_CRYPTO_RsaSignature *signature)
558 struct GNUNET_GNS_ResolverHandle *rh;
559 struct GNUNET_TIME_Relative remaining_time;
560 GNUNET_HashCode zone;
562 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Got %d records from authority lookup\n",
565 rh = (struct GNUNET_GNS_ResolverHandle *)cls;
566 GNUNET_CRYPTO_hash(key,
567 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
569 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
572 * No authority found in namestore.
577 * We did not find an authority in the namestore
578 * _IF_ the current authoritative zone is us we cannot resolve
579 * _ELSE_ we can still check the _expired_ dht
581 if ((0 != GNUNET_CRYPTO_hash_cmp(&rh->authority, &zone_hash)) &&
582 (remaining_time.rel_value == 0))
584 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
585 "Authority %s unknown in namestore, trying dht\n",
587 resolve_authority_dht(rh);
590 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Authority %s unknown\n",
592 reply_to_dns(rh, 0, NULL);
596 //Note only 1 pkey should have been returned.. anything else would be strange
598 * We found an authority that may be able to help us
602 for (i=0; i<rd_count;i++)
605 if (rd[i].record_type != GNUNET_GNS_RECORD_PKEY)
608 if ((GNUNET_TIME_absolute_get_remaining (rd[i].expiration)).rel_value
611 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "This pkey is expired.\n");
612 if (remaining_time.rel_value == 0)
614 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
615 "This dht entry is expired. Refreshing\n");
616 resolve_authority_dht(rh);
623 * Resolve rest of query with new authority
625 GNUNET_assert(rd[i].record_type == GNUNET_GNS_RECORD_PKEY);
626 GNUNET_CRYPTO_hash(rd[i].data,
627 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
637 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
638 "Authority lookup successful but no PKEY... never get here\n");
639 reply_to_dns(rh, 0, NULL);
644 * Reply to client with the result from our lookup.
646 * @param rh the request handle of the lookup
647 * @param rd_count the number of records to return
648 * @param rd the record data
651 reply_to_dns(struct GNUNET_GNS_ResolverHandle *rh, uint32_t rd_count,
652 const struct GNUNET_NAMESTORE_RecordData *rd)
658 struct GNUNET_DNSPARSER_Packet *packet = rh->packet;
659 struct GNUNET_DNSPARSER_Record answer_records[rh->answered];
660 struct GNUNET_DNSPARSER_Record additional_records[rd_count-(rh->answered)];
661 packet->answers = answer_records;
662 packet->additional_records = additional_records;
665 * Put records in the DNS packet and modify it
668 len = sizeof(struct GNUNET_DNSPARSER_Record*);
669 for (i=0; i < rd_count; i++)
672 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
673 "Adding type %d to DNS response\n", rd[i].record_type);
674 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Name: %s\n", rh->name);
675 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "QName: %s\n", rh->query->name);
676 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Record %d/%d\n", i+1, rd_count);
677 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Record len %d\n", rd[i].data_size);
679 if (rd[i].record_type == rh->query->type)
681 answer_records[i].name = rh->query->name;
682 answer_records[i].type = rd[i].record_type;
683 answer_records[i].data.raw.data_len = rd[i].data_size;
684 answer_records[i].data.raw.data = (char*)rd[i].data;
685 answer_records[i].expiration_time = rd[i].expiration;
686 answer_records[i].class = GNUNET_DNSPARSER_CLASS_INTERNET;//hmmn
690 additional_records[i].name = rh->query->name;
691 additional_records[i].type = rd[i].record_type;
692 additional_records[i].data.raw.data_len = rd[i].data_size;
693 additional_records[i].data.raw.data = (char*)rd[i].data;
694 additional_records[i].expiration_time = rd[i].expiration;
695 additional_records[i].class = GNUNET_DNSPARSER_CLASS_INTERNET;//hmmn
699 packet->num_answers = rh->answered;
700 packet->num_additional_records = rd_count-(rh->answered);
702 if (0 == GNUNET_CRYPTO_hash_cmp(&rh->authority, &zone_hash))
703 packet->flags.authoritative_answer = 1;
705 packet->flags.authoritative_answer = 0;
708 packet->flags.return_code = GNUNET_DNSPARSER_RETURN_CODE_NAME_ERROR;
710 packet->flags.return_code = GNUNET_DNSPARSER_RETURN_CODE_NO_ERROR;
712 packet->flags.query_or_response = 1;
718 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
719 "Building DNS response\n");
720 ret = GNUNET_DNSPARSER_pack (packet,
721 1024, /* FIXME magic from dns redirector */
724 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
725 "Built DNS response! (ret=%d,len=%d)\n", ret, len);
726 if (ret == GNUNET_OK)
728 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
729 "Answering DNS request\n");
730 GNUNET_DNS_request_answer(rh->request_handle,
733 //GNUNET_free(answer);
734 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Answered DNS request\n");
738 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
739 "Error building DNS response! (ret=%d)", ret);
742 GNUNET_free(rh->name);
748 * Namestore calls this function if we have record for this name.
749 * (or with rd_count=0 to indicate no matches)
751 * @param cls the pending query
752 * @param key the key of the zone we did the lookup
753 * @param expiration expiration date of the namestore entry
754 * @param name the name for which we need an authority
755 * @param rd_count the number of records with 'name'
756 * @param rd the record data
757 * @param signature the signature of the authority for the record data
760 process_authoritative_result(void* cls,
761 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
762 struct GNUNET_TIME_Absolute expiration,
763 const char *name, unsigned int rd_count,
764 const struct GNUNET_NAMESTORE_RecordData *rd,
765 const struct GNUNET_CRYPTO_RsaSignature *signature)
767 struct GNUNET_GNS_ResolverHandle *rh;
768 struct GNUNET_TIME_Relative remaining_time;
769 GNUNET_HashCode zone;
771 rh = (struct GNUNET_GNS_ResolverHandle *) cls;
772 GNUNET_CRYPTO_hash(key,
773 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
775 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
780 * Lookup terminated and no results
781 * -> DHT Phase unless data is recent
783 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
784 "Namestore lookup for %s terminated without results\n", name);
787 * if this is not our zone we cannot rely on the namestore to be
788 * complete. -> Query DHT
790 if (GNUNET_CRYPTO_hash_cmp(&zone, &zone_hash))
792 if (remaining_time.rel_value == 0)
794 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
796 resolve_name_dht(rh, name);
801 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
802 "Record is still recent. No DHT lookup\n");
807 * Our zone and no result? Cannot resolve TT
809 GNUNET_assert(rh->answered == 0);
810 reply_to_dns(rh, 0, NULL);
817 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
818 "Processing additional result %s from namestore\n", name);
820 for (i=0; i<rd_count;i++)
823 if ((strcmp(name, rh->query->name) == 0)
824 && (rd[i].record_type != rh->query->type))
827 if ((GNUNET_TIME_absolute_get_remaining (rd[i].expiration)).rel_value
830 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "This record is expired. Skipping\n");
840 * consult dht if expired
842 if ((remaining_time.rel_value == 0) && (rh->answered == 0))
844 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
845 "This dht entry is old. Refreshing.\n");
846 resolve_name_dht(rh, name);
850 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Found %d answer(s) to query!\n",
853 reply_to_dns(rh, rd_count, rd);
858 * Determine if this name is canonical.
860 * a.b.gnunet = not canonical
863 * @param name the name to test
864 * @return 1 if canonical
867 is_canonical(char* name)
869 uint32_t len = strlen(name);
872 for (i=0; i<len; i++)
874 if (*(name+i) == '.')
881 * Move one level up in the domain hierarchy and return the
882 * passed top level domain.
884 * @param name the domain
892 if (is_canonical(name))
895 for (len = strlen(name); len > 0; len--)
897 if (*(name+len) == '.')
911 * The first phase of resolution.
912 * First check if the name is canonical.
913 * If it is then try to resolve directly.
914 * If not then we first have to resolve the authoritative entities.
916 * @param rh the pending lookup
919 resolve_name(struct GNUNET_GNS_ResolverHandle *rh)
921 if (is_canonical(rh->name))
923 /* We only need to check the current zone's ns */
924 GNUNET_NAMESTORE_lookup_record(namestore_handle,
928 &process_authoritative_result,
933 /* We have to resolve the authoritative entity first */
934 rh->authority_name = pop_tld(rh->name);
935 GNUNET_NAMESTORE_lookup_record(namestore_handle,
938 GNUNET_GNS_RECORD_PKEY,
939 &process_authority_lookup,
945 * Entry point for name resolution
946 * Setup a new query and try to resolve
948 * @param request the request handle of the DNS request from a client
949 * @param p the DNS query packet we received
950 * @param q the DNS query we received parsed from p
953 start_resolution(struct GNUNET_DNS_RequestHandle *request,
954 struct GNUNET_DNSPARSER_Packet *p,
955 struct GNUNET_DNSPARSER_Query *q)
957 struct GNUNET_GNS_ResolverHandle *rh;
959 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
960 "Starting resolution for %s (type=%d)!\n",
963 rh = GNUNET_malloc(sizeof (struct GNUNET_GNS_ResolverHandle));
966 rh->authority = zone_hash;
968 rh->name = GNUNET_malloc(strlen(q->name)
969 - strlen(gnunet_tld) + 1);
971 strlen(q->name)-strlen(gnunet_tld) + 1);
972 memcpy(rh->name, q->name,
973 strlen(q->name)-strlen(gnunet_tld));
975 rh->request_handle = request;
977 /* Start resolution in our zone */
982 * The DNS request handler
983 * Called for every incoming DNS request.
986 * @param rh request handle to user for reply
987 * @param request_length number of bytes in request
988 * @param request udp payload of the DNS request
991 handle_dns_request(void *cls,
992 struct GNUNET_DNS_RequestHandle *rh,
993 size_t request_length,
996 struct GNUNET_DNSPARSER_Packet *p;
999 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Hijacked a DNS request...processing\n");
1000 p = GNUNET_DNSPARSER_parse (request, request_length);
1004 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1005 "Received malformed DNS packet, leaving it untouched\n");
1006 GNUNET_DNS_request_forward (rh);
1011 * Check tld and decide if we or
1012 * legacy dns is responsible
1014 * FIXME now in theory there could be more than 1 query in the request
1015 * but if this is case we get into trouble:
1016 * either we query the GNS or the DNS. We cannot do both!
1017 * So I suggest to either only allow a single query per request or
1018 * only allow GNS or DNS requests.
1019 * The way it is implemented here now is buggy and will lead to erratic
1020 * behaviour (if multiple queries are present).
1022 if (p->num_queries == 0)
1024 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1025 "No Queries in DNS packet... forwarding\n");
1026 GNUNET_DNS_request_forward (rh);
1029 if (p->num_queries > 1)
1031 /* Note: We could also look for .gnunet */
1032 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1033 ">1 queriy in DNS packet... odd. We only process #1\n");
1040 tldoffset = p->queries[0].name + strlen(p->queries[0].name);
1042 while ((*tldoffset) != '.')
1045 if (0 == strcmp(tldoffset, gnunet_tld))
1047 start_resolution(rh, p, p->queries);
1052 * This request does not concern us. Forward to real DNS.
1054 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1055 "Request for %s is forwarded to DNS\n", p->queries[0].name);
1056 GNUNET_DNS_request_forward (rh);
1062 * test function that stores some data in the namestore
1063 * This will also be replaced by a test progrm that
1064 * directl interfaces with the namestore
1067 put_some_records(void)
1069 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Populating namestore\n");
1070 /* put an A record into namestore FIXME use gnunet.org */
1071 char* ipB = "5.6.7.8";
1073 struct in_addr *web = GNUNET_malloc(sizeof(struct in_addr));
1074 struct GNUNET_NAMESTORE_RecordData rdb_web;
1076 GNUNET_assert(1 == inet_pton (AF_INET, ipB, web));
1078 rdb_web.data_size = sizeof(struct in_addr);
1080 rdb_web.record_type = GNUNET_DNSPARSER_TYPE_A;
1081 rdb_web.expiration = GNUNET_TIME_absolute_get_forever ();
1083 GNUNET_NAMESTORE_record_create (namestore_handle,
1092 * Method called periodicattluy that triggers
1093 * iteration over root zone
1095 * @param cls closure
1096 * @param tc task context
1099 update_zone_dht_next(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1101 GNUNET_NAMESTORE_zone_iterator_next(namestore_iter);
1105 * Continuation for DHT put
1107 * @param cls closure
1108 * @param tc task context
1111 record_dht_put(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1113 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "put request transmitted\n");
1118 update_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1121 * Function used to put all records successively into the DHT.
1123 * @param cls the closure (NULL)
1124 * @param key the public key of the authority (ours)
1125 * @param expiration lifetime of the namestore entry
1126 * @param name the name of the records
1127 * @param rd_count the number of records in data
1128 * @param rd the record data
1129 * @param signature the signature for the record data
1132 put_gns_record(void *cls,
1133 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
1134 struct GNUNET_TIME_Absolute expiration,
1136 unsigned int rd_count,
1137 const struct GNUNET_NAMESTORE_RecordData *rd,
1138 const struct GNUNET_CRYPTO_RsaSignature *signature)
1141 struct GNSNameRecordBlock *nrb;
1142 GNUNET_HashCode name_hash;
1143 GNUNET_HashCode xor_hash;
1144 struct GNUNET_CRYPTO_HashAsciiEncoded xor_hash_string;
1145 uint32_t rd_payload_length;
1146 char* nrb_data = NULL;
1151 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Zone iteration finished\n");
1152 GNUNET_NAMESTORE_zone_iteration_stop (namestore_iter);
1153 zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_start,
1158 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1159 "Putting records for %s into the DHT\n", name);
1161 rd_payload_length = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
1163 nrb = GNUNET_malloc(rd_payload_length + strlen(name) + 1
1164 + sizeof(struct GNSNameRecordBlock));
1166 if (signature != NULL)
1167 nrb->signature = *signature;
1169 nrb->public_key = *key;
1171 nrb->rd_count = htonl(rd_count);
1173 memset(&nrb[1], 0, strlen(name) + 1);
1174 memcpy(&nrb[1], name, strlen(name));
1176 nrb_data = (char*)&nrb[1];
1177 nrb_data += strlen(name) + 1;
1179 if (-1 == GNUNET_NAMESTORE_records_serialize (rd_count,
1184 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Recor serialization failed!\n");
1189 * calculate DHT key: H(name) xor H(pubkey)
1191 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
1192 GNUNET_CRYPTO_hash_xor(&zone_hash, &name_hash, &xor_hash);
1193 GNUNET_CRYPTO_hash_to_enc (&xor_hash, &xor_hash_string);
1194 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1195 "putting records for %s under key: %s with size %d\n",
1196 name, (char*)&xor_hash_string, rd_payload_length);
1198 GNUNET_DHT_put (dht_handle, &xor_hash,
1199 DHT_GNS_REPLICATION_LEVEL,
1201 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
1205 DHT_OPERATION_TIMEOUT,
1207 NULL); //cls for cont
1209 num_public_records++;
1212 * Reschedule periodic put
1214 zone_update_taskid = GNUNET_SCHEDULER_add_delayed (dht_update_interval,
1215 &update_zone_dht_next,
1221 * Puts a single trusted entity into the
1222 * namestore. Will be replaced in a testcase
1223 * that directly interacts with a persistent
1226 * @param name name of entity
1227 * @param keyfile keyfile
1230 put_trusted(char* name, char* keyfile)
1232 struct GNUNET_NAMESTORE_RecordData rd;
1233 struct GNUNET_CRYPTO_RsaPrivateKey *key;
1234 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pkey;
1235 pkey = GNUNET_malloc(sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
1237 key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
1238 GNUNET_CRYPTO_rsa_key_get_public (key, pkey);
1240 rd.expiration = GNUNET_TIME_absolute_get_forever ();
1241 rd.data_size = sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded);
1242 rd.record_type = GNUNET_GNS_RECORD_PKEY;
1244 GNUNET_NAMESTORE_record_create (namestore_handle,
1255 * Periodically iterate over our zone and store everything in dht
1258 * @param tc task context
1261 update_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1263 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Starting DHT zone update!\n");
1264 if (0 == num_public_records)
1266 dht_update_interval = GNUNET_TIME_relative_multiply(
1267 GNUNET_TIME_UNIT_SECONDS,
1272 dht_update_interval = GNUNET_TIME_relative_multiply(
1273 GNUNET_TIME_UNIT_SECONDS,
1274 (3600/num_public_records));
1276 num_public_records = 0; //start counting again
1277 namestore_iter = GNUNET_NAMESTORE_zone_iteration_start (namestore_handle,
1279 GNUNET_NAMESTORE_RF_AUTHORITY,
1280 GNUNET_NAMESTORE_RF_PRIVATE,
1286 * Process GNS requests.
1288 * @param cls closure
1289 * @param server the initialized server
1290 * @param c configuration to use
1293 run (void *cls, struct GNUNET_SERVER_Handle *server,
1294 const struct GNUNET_CONFIGURATION_Handle *c)
1297 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Initializing GNS\n");
1300 char* trusted_entities;
1301 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
1303 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (c, "gns",
1304 "ZONEKEY", &keyfile))
1306 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1307 "No private key for root zone specified%s!\n", keyfile);
1308 GNUNET_SCHEDULER_shutdown(0);
1312 zone_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
1313 GNUNET_CRYPTO_rsa_key_get_public (zone_key, &pkey);
1315 GNUNET_CRYPTO_hash(&pkey, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1318 nc = GNUNET_SERVER_notification_context_create (server, 1);
1320 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
1324 GNUNET_CONFIGURATION_get_value_yesno (c, "gns",
1327 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1328 "DNS hijacking enabled... connecting to service.\n");
1330 * Do gnunet dns init here
1332 dns_handle = GNUNET_DNS_connect(c,
1333 GNUNET_DNS_FLAG_PRE_RESOLUTION,
1334 &handle_dns_request, /* rh */
1335 NULL); /* Closure */
1336 if (NULL == dns_handle)
1338 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1339 "Failed to connect to the dnsservice!\n");
1346 * handle to our local namestore
1348 namestore_handle = GNUNET_NAMESTORE_connect(c);
1350 if (NULL == namestore_handle)
1352 //FIXME do error handling;
1353 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1354 "Failed to connect to the namestore!\n");
1355 GNUNET_SCHEDULER_shutdown(0);
1359 char* trusted_start;
1363 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (c, "gns",
1367 trusted_start = trusted_entities;
1368 trusted_len = strlen(trusted_entities);
1369 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1370 "Found trusted entities in config file, importing\n");
1371 while ((trusted_entities-trusted_start) < trusted_len)
1373 trusted_name = trusted_entities;
1374 while (*trusted_entities != ':')
1376 *trusted_entities = '\0';
1378 trusted_key = trusted_entities;
1379 while (*trusted_entities != ',' && (*trusted_entities != '\0'))
1381 *trusted_entities = '\0';
1384 if (GNUNET_YES == GNUNET_DISK_file_test (trusted_key))
1386 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Adding %s:%s to root zone\n",
1389 put_trusted(trusted_name, trusted_key);
1393 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Keyfile %s does not exist!\n",
1395 //put_trusted(trusted_name, trusted_key); //FIXME for testing
1404 dht_handle = GNUNET_DHT_connect(c, 1); //FIXME get ht_len from cfg
1406 if (NULL == dht_handle)
1408 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not connect to DHT!\n");
1411 put_some_records(); //FIXME for testing
1414 * Schedule periodic put
1416 * We have roughly an hour for all records;
1418 dht_update_interval = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS,
1420 //zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_start, NULL);
1426 * The main function for the GNS service.
1428 * @param argc number of arguments from the command line
1429 * @param argv command line arguments
1430 * @return 0 ok, 1 on error
1433 main (int argc, char *const *argv)
1439 GNUNET_SERVICE_run (argc, argv, "gns", GNUNET_SERVICE_OPTION_NONE, &run,
1444 /* end of gnunet-service-gns.c */