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 /* Ignore for now not used anyway and probably never will */
43 #define GNUNET_MESSAGE_TYPE_GNS_CLIENT_LOOKUP 23
44 #define GNUNET_MESSAGE_TYPE_GNS_CLIENT_RESULT 24
47 * A result list for namestore queries
49 struct GNUNET_GNS_ResolverHandle
51 /* The name to resolve */
54 /* the request handle to reply to */
55 struct GNUNET_DNS_RequestHandle *request_handle;
57 /* the dns parser packet received */
58 struct GNUNET_DNSPARSER_Packet *packet;
60 /* the query parsed from the packet */
62 struct GNUNET_DNSPARSER_Query *query;
64 /* has this query been answered? how many matches */
67 /* the authoritative zone to query */
68 GNUNET_HashCode authority;
70 /* the name of the authoritative zone to query */
74 * we have an authority in namestore that
75 * may be able to resolve
79 /* a handle for dht lookups. should be NULL if no lookups are in progress */
80 struct GNUNET_DHT_GetHandle *get_handle;
86 * Our handle to the DNS handler library
88 struct GNUNET_DNS_Handle *dns_handle;
91 * Our handle to the DHT
93 struct GNUNET_DHT_Handle *dht_handle;
96 * Our zone's private key
98 struct GNUNET_CRYPTO_RsaPrivateKey *zone_key;
101 * Our handle to the namestore service
102 * FIXME maybe need a second handle for iteration
104 struct GNUNET_NAMESTORE_Handle *namestore_handle;
107 * Handle to iterate over our authoritative zone in namestore
109 struct GNUNET_NAMESTORE_ZoneIterator *namestore_iter;
112 * The configuration the GNS service is running with
114 const struct GNUNET_CONFIGURATION_Handle *GNS_cfg;
117 * Our notification context.
119 static struct GNUNET_SERVER_NotificationContext *nc;
124 GNUNET_HashCode zone_hash;
127 * Our tld. Maybe get from config file
129 const char* gnunet_tld = ".gnunet";
132 * Useful for zone update for DHT put
134 static int num_public_records = 3600;
135 struct GNUNET_TIME_Relative dht_update_interval;
136 GNUNET_SCHEDULER_TaskIdentifier zone_update_taskid = GNUNET_SCHEDULER_NO_TASK;
139 void reply_to_dns(struct GNUNET_GNS_ResolverHandle *answer, uint32_t rd_count,
140 const struct GNUNET_NAMESTORE_RecordData *rd);
141 void resolve_name(struct GNUNET_GNS_ResolverHandle *rh);
144 * Task run during shutdown.
150 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
152 //Kill zone task for it may make the scheduler hang
153 GNUNET_SCHEDULER_cancel(zone_update_taskid);
154 GNUNET_DNS_disconnect(dns_handle);
155 GNUNET_NAMESTORE_disconnect(namestore_handle, 0);
156 GNUNET_DHT_disconnect(dht_handle);
160 * Callback when record data is put into namestore
162 * @param cls the closure
163 * @param success GNUNET_OK on success
164 * @param emsg the error message. NULL if SUCCESS==GNUNET_OK
167 on_namestore_record_put_result(void *cls,
171 if (GNUNET_NO == success)
173 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "records already in namestore\n");
176 else if (GNUNET_YES == success)
178 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
179 "records successfully put in namestore\n");
183 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
184 "Error putting records into namestore: %s\n", emsg);
188 * Function called when we get a result from the dht
191 * @param cls the request handle
192 * @param exp lifetime
193 * @param key the key the record was stored under
194 * @param get_path get path
195 * @param get_path_length get path length
196 * @param put_path put path
197 * @param put_path_length put path length
198 * @param type the block type
199 * @param size the size of the record
200 * @param data the record data
203 process_authority_dht_result(void* cls,
204 struct GNUNET_TIME_Absolute exp,
205 const GNUNET_HashCode * key,
206 const struct GNUNET_PeerIdentity *get_path,
207 unsigned int get_path_length,
208 const struct GNUNET_PeerIdentity *put_path,
209 unsigned int put_path_length,
210 enum GNUNET_BLOCK_Type type,
211 size_t size, const void *data)
213 struct GNUNET_GNS_ResolverHandle *rh;
214 struct GNSNameRecordBlock *nrb;
215 struct GNSRecordBlock *rb;
216 uint32_t num_records;
219 GNUNET_HashCode zone, name_hash;
224 //FIXME check expiration?
225 rh = (struct GNUNET_GNS_ResolverHandle *)cls;
226 nrb = (struct GNSNameRecordBlock*)data;
228 GNUNET_DHT_get_stop (rh->get_handle);
229 rh->get_handle = NULL;
230 num_records = ntohl(nrb->rd_count);
231 struct GNUNET_NAMESTORE_RecordData rd[num_records];
232 name = (char*)&nrb[1];
233 rb = (struct GNSRecordBlock *)&name[strlen(name) + 1];
235 for (i=0; i<num_records; i++)
238 rd[i].record_type = ntohl(rb->type);
239 rd[i].data_size = ntohl(rb->data_length);
241 rd[i].expiration = GNUNET_TIME_absolute_ntoh(rb->expiration);
242 rd[i].flags = ntohl(rb->flags);
244 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
245 "Got name: %s (wanted %s)\n", name, rh->authority_name);
246 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
247 "Got type: %d raw %d (wanted %d)\n",
248 rd[i].record_type, rb->type, GNUNET_GNS_RECORD_PKEY);
249 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
250 "Got data length: %d\n", rd[i].data_size);
251 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
252 "Got flag %d\n", rd[i].flags);
253 if ((strcmp(name, rh->authority_name) == 0) &&
254 (rd[i].record_type == GNUNET_GNS_RECORD_PKEY))
256 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Authority found in DHT\n");
259 (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *)rd[i].data,
260 GNUNET_CRYPTO_RSA_KEY_LENGTH,
263 rb = (struct GNSRecordBlock*)((char*)&rb[1] + rd[i].data_size);
267 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
268 GNUNET_CRYPTO_hash_xor(key, &name_hash, &zone);
271 if (0 == GNUNET_CRYPTO_hash_cmp(&zone_hash, &zone))
273 GNUNET_NAMESTORE_record_put (namestore_handle,
280 &on_namestore_record_put_result, //cont
290 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "No authority in records\n");
291 reply_to_dns(rh, 0, NULL);
295 * Start DHT lookup for a name -> PKEY (compare NS) record in
296 * query->authority's zone
298 * @param rh the pending gns query
299 * @param name the name of the PKEY record
302 resolve_authority_dht(struct GNUNET_GNS_ResolverHandle *rh, const char* name)
305 struct GNUNET_TIME_Relative timeout;
306 GNUNET_HashCode name_hash;
307 GNUNET_HashCode lookup_key;
309 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
310 GNUNET_CRYPTO_hash_xor(&name_hash, &rh->authority, &lookup_key);
312 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5);
314 xquery = htonl(GNUNET_GNS_RECORD_PKEY);
315 //FIXME how long to wait for results?
316 rh->get_handle = GNUNET_DHT_get_start(dht_handle, timeout,
317 GNUNET_BLOCK_TYPE_TEST, //FIXME todo
319 5, //Replication level FIXME
321 &xquery, //xquery FIXME is this bad?
322 0, // for test bp sizeof(GNUNET_GNS_RECORD_PKEY),
323 &process_authority_dht_result,
329 * Function called when we get a result from the dht
332 * @param cls the request handle
333 * @param exp lifetime
334 * @param key the key the record was stored under
335 * @param get_path get path
336 * @param get_path_length get path length
337 * @param put_path put path
338 * @param put_path_length put path length
339 * @param type the block type
340 * @param size the size of the record
341 * @param data the record data
344 process_name_dht_result(void* cls,
345 struct GNUNET_TIME_Absolute exp,
346 const GNUNET_HashCode * key,
347 const struct GNUNET_PeerIdentity *get_path,
348 unsigned int get_path_length,
349 const struct GNUNET_PeerIdentity *put_path,
350 unsigned int put_path_length,
351 enum GNUNET_BLOCK_Type type,
352 size_t size, const void *data)
354 struct GNUNET_GNS_ResolverHandle *rh;
355 struct GNSNameRecordBlock *nrb;
356 struct GNSRecordBlock *rb;
357 uint32_t num_records;
360 GNUNET_HashCode zone, name_hash;
361 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "got dht result (size=%d)\n", size);
365 //FIXME maybe check expiration here, check block type
367 rh = (struct GNUNET_GNS_ResolverHandle *)cls;
368 nrb = (struct GNSNameRecordBlock*)data;
370 GNUNET_DHT_get_stop (rh->get_handle);
371 rh->get_handle = NULL;
372 num_records = ntohl(nrb->rd_count);
373 struct GNUNET_NAMESTORE_RecordData rd[num_records];
375 name = (char*)&nrb[1];
376 rb = (struct GNSRecordBlock*)&name[strlen(name) + 1];
378 for (i=0; i<num_records; i++)
380 rd[i].record_type = ntohl(rb->type);
381 rd[i].data_size = ntohl(rb->data_length);
382 rd[i].data = (char*)&rb[1];
383 rd[i].expiration = GNUNET_TIME_absolute_ntoh(rb->expiration);
384 rd[i].flags = ntohl(rb->flags);
385 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
386 "Got name: %s (wanted %s)\n", name, rh->name);
387 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
388 "Got type: %d raw %d (wanted %d)\n",
389 rd[i].record_type, rb->type, rh->query->type);
390 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
391 "Got data length: %d\n", rd[i].data_size);
392 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
393 "Got flag %d\n", rd[i].flags);
395 if ((strcmp(name, rh->name) == 0) &&
396 (rd[i].record_type == rh->query->type))
401 rb = (struct GNSRecordBlock*)((char*)&rb[1] + rd[i].data_size);
405 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
406 GNUNET_CRYPTO_hash_xor(key, &name_hash, &zone);
408 //FIXME check pubkey against existing key in namestore?
409 //https://gnunet.org/bugs/view.php?id=2179
412 GNUNET_NAMESTORE_record_put (namestore_handle,
419 &on_namestore_record_put_result, //cont
423 reply_to_dns(rh, num_records, rd);
425 reply_to_dns(rh, 0, NULL);
430 * Start DHT lookup for a (name -> query->record_type) record in
431 * query->authority's zone
433 * @param rh the pending gns query context
434 * @param name the name to query record
437 resolve_name_dht(struct GNUNET_GNS_ResolverHandle *rh, const char* name)
440 struct GNUNET_TIME_Relative timeout;
441 GNUNET_HashCode name_hash;
442 GNUNET_HashCode lookup_key;
443 struct GNUNET_CRYPTO_HashAsciiEncoded lookup_key_string;
445 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
446 GNUNET_CRYPTO_hash_xor(&name_hash, &rh->authority, &lookup_key);
447 GNUNET_CRYPTO_hash_to_enc (&lookup_key, &lookup_key_string);
448 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
449 "starting dht lookup for %s with key: %s\n",
450 name, (char*)&lookup_key_string);
452 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5);
454 xquery = htonl(rh->query->type);
455 //FIXME how long to wait for results?
456 rh->get_handle = GNUNET_DHT_get_start(dht_handle, timeout,
457 GNUNET_BLOCK_TYPE_TEST, //FIXME todo
459 5, //Replication level FIXME
461 &xquery, //xquery FIXME is this bad?
462 0, //for test bp sizeof(rh->query->type),
463 &process_name_dht_result,
470 resolve_name(struct GNUNET_GNS_ResolverHandle *rh);
473 * This is a callback function that should give us only PKEY
474 * records. Used to query the namestore for the authority (PKEY)
477 * @param cls the pending query
478 * @param key the key of the zone we did the lookup
479 * @param expiration expiration date of the record data set in the namestore
480 * @param name the name for which we need an authority
481 * @param rd_count the number of records with 'name'
482 * @param rd the record data
483 * @param signature the signature of the authority for the record data
486 process_authority_lookup(void* cls,
487 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
488 struct GNUNET_TIME_Absolute expiration,
490 unsigned int rd_count,
491 const struct GNUNET_NAMESTORE_RecordData *rd,
492 const struct GNUNET_CRYPTO_RsaSignature *signature)
494 struct GNUNET_GNS_ResolverHandle *rh;
495 struct GNUNET_TIME_Relative remaining_time;
496 GNUNET_HashCode zone;
498 rh = (struct GNUNET_GNS_ResolverHandle *)cls;
499 GNUNET_CRYPTO_hash(key, GNUNET_CRYPTO_RSA_KEY_LENGTH, &zone);
500 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
503 * No authority found in namestore.
508 * We did not find an authority in the namestore
509 * _IF_ the current authoritative zone is us we cannot resolve
510 * _ELSE_ we can still check the _expired_ dht
512 if (0 != GNUNET_CRYPTO_hash_cmp(&zone, &zone_hash) &&
513 (remaining_time.rel_value == 0))
515 resolve_authority_dht(rh, name);
518 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Authority unknown\n");
519 reply_to_dns(rh, 0, NULL);
523 //Note only 1 pkey should have been returned.. anything else would be strange
525 * We found an authority that may be able to help us
529 for (i=0; i<rd_count;i++)
532 if (strcmp(name, rh->query->name) && rd[i].record_type
533 != GNUNET_GNS_RECORD_PKEY)
536 if ((GNUNET_TIME_absolute_get_remaining (rd[i].expiration)).rel_value
539 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "This pkey is expired.\n");
540 if (remaining_time.rel_value == 0)
542 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
543 "This dht entry is expired. Refreshing\n");
544 resolve_authority_dht(rh, name);
550 GNUNET_assert(rd[i].record_type == GNUNET_GNS_RECORD_PKEY);
551 GNUNET_CRYPTO_hash(rd[i].data, GNUNET_CRYPTO_RSA_KEY_LENGTH,
562 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
563 "Authority lookup successful but no PKEY... never get here?\n");
564 reply_to_dns(rh, 0, NULL);
569 * Reply to client with the result from our lookup.
571 * @param rh the request handle of the lookup
572 * @param rd_count the number of records to return
573 * @param rd the record data
576 reply_to_dns(struct GNUNET_GNS_ResolverHandle *rh, uint32_t rd_count,
577 const struct GNUNET_NAMESTORE_RecordData *rd)
583 struct GNUNET_DNSPARSER_Packet *packet = rh->packet;
584 struct GNUNET_DNSPARSER_Record answer_records[rh->answered];
585 struct GNUNET_DNSPARSER_Record additional_records[rd_count-(rh->answered)];
586 packet->answers = answer_records;
587 packet->additional_records = additional_records;
589 len = sizeof(struct GNUNET_DNSPARSER_Record*);
590 for (i=0; i < rd_count; i++)
592 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
593 "Adding type %d to DNS response\n", rd[i].record_type);
594 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Name: %s\n", rh->name);
595 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "QName: %s\n", rh->query->name);
596 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Record %d/%d\n", i+1, rd_count);
597 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Record len %d\n", rd[i].data_size);
598 if (rd[i].record_type == rh->query->type)
600 answer_records[i].name = rh->query->name;
601 answer_records[i].type = rd[i].record_type;
602 answer_records[i].data.raw.data_len = rd[i].data_size;
603 answer_records[i].data.raw.data = (char*)rd[i].data;
604 answer_records[i].expiration_time = rd[i].expiration;
605 answer_records[i].class = GNUNET_DNSPARSER_CLASS_INTERNET;//hmmn
609 additional_records[i].name = rh->query->name;
610 additional_records[i].type = rd[i].record_type;
611 additional_records[i].data.raw.data_len = rd[i].data_size;
612 additional_records[i].data.raw.data = (char*)rd[i].data;
613 additional_records[i].expiration_time = rd[i].expiration;
614 additional_records[i].class = GNUNET_DNSPARSER_CLASS_INTERNET;//hmmn
616 //GNUNET_free(i->record); DO this later!
619 packet->num_answers = rh->answered; //answer->num_records;
620 packet->num_additional_records = rd_count-(rh->answered);
622 if (0 == GNUNET_CRYPTO_hash_cmp(&rh->authority, &zone_hash))
623 packet->flags.authoritative_answer = 1;
625 packet->flags.authoritative_answer = 0;
628 packet->flags.return_code = GNUNET_DNSPARSER_RETURN_CODE_NAME_ERROR;
630 packet->flags.return_code = GNUNET_DNSPARSER_RETURN_CODE_NO_ERROR;
632 packet->flags.query_or_response = 1;
634 //FIXME this is silently discarded
635 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
636 "Building DNS response\n");
637 ret = GNUNET_DNSPARSER_pack (packet,
638 1024, /* FIXME magic from dns redirector */
641 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
642 "Built DNS response! (ret=%d,len=%d)\n", ret, len);
643 if (ret == GNUNET_OK)
645 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
646 "Answering DNS request\n");
647 GNUNET_DNS_request_answer(rh->request_handle,
650 //GNUNET_free(answer);
651 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Answered DNS request\n");
652 //FIXME return code, free datastructures
656 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
657 "Error building DNS response! (ret=%d)", ret);
660 //FIXME into free_resolver(rh)
661 GNUNET_free(rh->name);
667 * Namestore calls this function if we have an entry for this name.
668 * (or data=null to indicate the lookup has finished
670 * @param cls the pending query
671 * @param key the key of the zone we did the lookup
672 * @param expiration expiration date of the namestore entry
673 * @param name the name for which we need an authority
674 * @param rd_count the number of records with 'name'
675 * @param rd the record data
676 * @param signature the signature of the authority for the record data
679 process_authoritative_result(void* cls,
680 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
681 struct GNUNET_TIME_Absolute expiration,
682 const char *name, unsigned int rd_count,
683 const struct GNUNET_NAMESTORE_RecordData *rd,
684 const struct GNUNET_CRYPTO_RsaSignature *signature)
686 struct GNUNET_GNS_ResolverHandle *rh;
687 struct GNUNET_TIME_Relative remaining_time;
688 GNUNET_HashCode zone;
690 rh = (struct GNUNET_GNS_ResolverHandle *) cls;
691 GNUNET_CRYPTO_hash(key, GNUNET_CRYPTO_RSA_KEY_LENGTH, &zone);
692 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
694 //FIXME Handle results in rd
699 * Lookup terminated and no results
700 * -> DHT Phase unless data is recent
702 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
703 "Namestore lookup for %s terminated without results\n", name);
706 * if this is not our zone we cannot rely on the namestore to be
707 * complete. -> Query DHT
709 if (GNUNET_CRYPTO_hash_cmp(&zone, &zone_hash))
711 if (remaining_time.rel_value == 0)
713 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
715 resolve_name_dht(rh, name);
720 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
721 "Record is still recent. No DHT lookup\n");
726 * Our zone and no result? Cannot resolve TT
728 GNUNET_assert(rh->answered == 0);
729 reply_to_dns(rh, 0, NULL); //answered should be 0
736 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
737 "Processing additional result %s from namestore\n", name);
739 for (i=0; i<rd_count;i++)
742 if (strcmp(name, rh->query->name) && rd[i].record_type != rh->query->type)
745 if ((GNUNET_TIME_absolute_get_remaining (rd[i].expiration)).rel_value
748 //FIXME there is a catch here...
749 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "This record is expired. Skipping\n");
759 * consult dht if expired
761 if ((remaining_time.rel_value == 0) && (rh->answered == 0))
763 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
764 "This dht entry is old. Refreshing.\n");
765 resolve_name_dht(rh, name);
769 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Found %d answer(s) to query!\n",
772 reply_to_dns(rh, rd_count, rd);
777 * Determine if this name is canonical.
779 * a.b.gnunet = not canonical
782 * @param name the name to test
783 * @return 1 if canonical
786 is_canonical(char* name)
788 uint32_t len = strlen(name);
791 for (i=0; i<len; i++)
793 if (*(name+i) == '.')
800 * Move one level up in the domain hierarchy and return the
801 * passed top level domain.
803 * @param name the domain
806 char* pop_tld(char* name)
810 if (is_canonical(name))
813 for (len = strlen(name); len > 0; len--)
815 if (*(name+len) == '.')
829 * The first phase of resolution.
830 * First check if the name is canonical.
831 * If it is then try to resolve directly.
832 * If not then first have to resolve the authoritative entities.
834 * @param rh the pending lookup
835 * @param zone the zone we are currently resolving in
838 resolve_name(struct GNUNET_GNS_ResolverHandle *rh)
840 if (is_canonical(rh->name))
842 /* We only need to check the current zone's ns */
843 GNUNET_NAMESTORE_lookup_record(namestore_handle,
847 &process_authoritative_result,
852 /* We have to resolve the authoritative entity first */
853 rh->authority_name = pop_tld(rh->name);
854 GNUNET_NAMESTORE_lookup_record(namestore_handle,
857 GNUNET_GNS_RECORD_PKEY,
858 &process_authority_lookup,
864 * Entry point for name resolution
865 * Lookup local namestore of our zone.
867 * Setup a new query and try to resolve
869 * @param request the request handle of the DNS request from a client
870 * @param p the DNS query packet we received
871 * @param q the DNS query we received parsed from p
874 start_resolution(struct GNUNET_DNS_RequestHandle *request,
875 struct GNUNET_DNSPARSER_Packet *p,
876 struct GNUNET_DNSPARSER_Query *q)
878 struct GNUNET_GNS_ResolverHandle *rh;
880 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Starting resolution for (%s)!\n",
883 rh = GNUNET_malloc(sizeof (struct GNUNET_GNS_ResolverHandle));
886 rh->authority = zone_hash;
888 rh->name = GNUNET_malloc(strlen(q->name)
889 - strlen(gnunet_tld) + 1);
891 strlen(q->name)-strlen(gnunet_tld) + 1);
892 memcpy(rh->name, q->name,
893 strlen(q->name)-strlen(gnunet_tld));
895 rh->request_handle = request;
897 /* Start resolution in our zone */
902 * The DNS request handler
903 * Called for every incoming DNS request.
906 * @param rh request handle to user for reply
907 * @param request_length number of bytes in request
908 * @param request udp payload of the DNS request
911 handle_dns_request(void *cls,
912 struct GNUNET_DNS_RequestHandle *rh,
913 size_t request_length,
916 struct GNUNET_DNSPARSER_Packet *p;
919 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Hijacked a DNS request...processing\n");
920 p = GNUNET_DNSPARSER_parse (request, request_length);
924 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
925 "Received malformed DNS packet, leaving it untouched\n");
926 GNUNET_DNS_request_forward (rh);
931 * Check tld and decide if we or
932 * legacy dns is responsible
934 * FIXME now in theory there could be more than 1 query in the request
935 * but if this is case we get into trouble:
936 * either we query the GNS or the DNS. We cannot do both!
937 * So I suggest to either only allow a single query per request or
938 * only allow GNS or DNS requests.
939 * The way it is implemented here now is buggy and will lead to erratic
940 * behaviour (if multiple queries are present).
942 if (p->num_queries == 0)
944 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
945 "No Queries in DNS packet... forwarding\n");
946 GNUNET_DNS_request_forward (rh);
949 if (p->num_queries > 1)
951 /* Note: We could also look for .gnunet */
952 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
953 ">1 queriy in DNS packet... odd. We only process #1\n");
957 tldoffset = p->queries[0].name + strlen(p->queries[0].name);
959 while ((*tldoffset) != '.')
962 if (0 == strcmp(tldoffset, gnunet_tld))
964 start_resolution(rh, p, p->queries);
969 * This request does not concern us. Forward to real DNS.
971 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
972 "Request for %s is forwarded to DNS\n", p->queries[0].name);
973 GNUNET_DNS_request_forward (rh);
979 * test function that stores some data in the namestore
982 put_some_records(void)
984 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Populating namestore\n");
985 /* put an A record into namestore FIXME use gnunet.org */
986 char* ipB = "5.6.7.8";
988 struct in_addr *web = GNUNET_malloc(sizeof(struct in_addr));
989 struct GNUNET_NAMESTORE_RecordData rdb_web;
991 GNUNET_assert(1 == inet_pton (AF_INET, ipB, web));
993 rdb_web.data_size = sizeof(struct in_addr);
995 rdb_web.record_type = GNUNET_DNSPARSER_TYPE_A;
996 rdb_web.expiration = GNUNET_TIME_absolute_get_forever ();
998 GNUNET_NAMESTORE_record_create (namestore_handle,
1007 update_zone_dht_next(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1009 GNUNET_NAMESTORE_zone_iterator_next(namestore_iter);
1013 record_dht_put(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1015 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "put request transmitted\n");
1020 update_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1023 * Function used to put all records successively into the DHT.
1026 * @param cls the closure (NULL)
1027 * @param key the public key of the authority (ours)
1028 * @param expiration lifetime of the namestore entry
1029 * @param name the name of the records
1030 * @param rd_count the number of records in data
1031 * @param rd the record data
1032 * @param signature the signature for the record data
1035 put_gns_record(void *cls,
1036 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
1037 struct GNUNET_TIME_Absolute expiration,
1039 unsigned int rd_count,
1040 const struct GNUNET_NAMESTORE_RecordData *rd,
1041 const struct GNUNET_CRYPTO_RsaSignature *signature)
1043 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Putting records into the DHT\n");
1044 struct GNUNET_TIME_Relative timeout;
1045 struct GNSNameRecordBlock *nrb;
1046 struct GNSRecordBlock *rb;
1047 GNUNET_HashCode name_hash;
1048 GNUNET_HashCode xor_hash;
1049 struct GNUNET_CRYPTO_HashAsciiEncoded xor_hash_string;
1051 uint32_t rd_payload_length;
1056 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Zone iteration finished\n");
1057 GNUNET_NAMESTORE_zone_iteration_stop (namestore_iter);
1058 zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_start,
1063 rd_payload_length = rd_count * sizeof(struct GNSRecordBlock);
1064 rd_payload_length += strlen(name) + 1 + sizeof(struct GNSNameRecordBlock);
1066 /* calculate payload size */
1067 for (i=0; i<rd_count; i++)
1069 rd_payload_length += rd[i].data_size;
1072 nrb = GNUNET_malloc(rd_payload_length);
1074 if (signature != NULL)
1075 memcpy(&nrb->signature, signature,
1076 sizeof(struct GNUNET_CRYPTO_RsaSignature));
1077 //FIXME signature purpose
1078 memcpy(&nrb->public_key, key,
1079 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
1081 nrb->rd_count = htonl(rd_count);
1083 memcpy(&nrb[1], name, strlen(name) + 1); //FIXME is this 0 terminated??
1085 rb = (struct GNSRecordBlock *)((char*)&nrb[1] + strlen(name) + 1);
1087 for (i=0; i<rd_count; i++)
1089 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "putting record with type %d\n",
1091 rb->type = htonl(rd[i].record_type);
1092 rb->expiration = GNUNET_TIME_absolute_hton(rd[i].expiration);
1093 rb->data_length = htonl(rd[i].data_size);
1094 rb->flags = htonl(rd[i].flags);
1095 memcpy(&rb[1], rd[i].data, rd[i].data_size);
1096 rb = &rb[1] + rd[i].data_size;
1100 * FIXME magic number 20 move to config file
1103 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 20);
1104 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
1105 GNUNET_CRYPTO_hash_xor(&zone_hash, &name_hash, &xor_hash);
1106 GNUNET_CRYPTO_hash_to_enc (&xor_hash, &xor_hash_string);
1107 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1108 "putting records for %s under key: %s with size %d\n",
1109 name, (char*)&xor_hash_string, rd_payload_length);
1111 GNUNET_DHT_put (dht_handle, &xor_hash,
1112 5, //replication level
1114 GNUNET_BLOCK_TYPE_TEST, //FIXME todo block plugin
1119 &record_dht_put, //FIXME continuation needed? success check? yes ofc
1120 NULL); //cls for cont
1122 num_public_records++;
1125 * Reschedule periodic put
1127 zone_update_taskid = GNUNET_SCHEDULER_add_delayed (dht_update_interval,
1128 &update_zone_dht_next,
1134 put_trusted(char* name, char* keyfile)
1136 struct GNUNET_NAMESTORE_RecordData rd;
1137 struct GNUNET_CRYPTO_RsaPrivateKey *key;
1138 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pkey;
1139 pkey = GNUNET_malloc(sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
1141 key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
1142 GNUNET_CRYPTO_rsa_key_get_public (key, pkey);
1144 rd.expiration = GNUNET_TIME_absolute_get_forever ();
1145 rd.data_size = sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded);
1146 rd.record_type = GNUNET_GNS_RECORD_PKEY;
1148 GNUNET_NAMESTORE_record_create (namestore_handle,
1159 * Periodically iterate over our zone and store everything in dht
1162 * @param tc task context
1165 update_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1167 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Update zone!\n");
1168 if (0 == num_public_records)
1170 dht_update_interval = GNUNET_TIME_relative_multiply(
1171 GNUNET_TIME_UNIT_SECONDS,
1176 dht_update_interval = GNUNET_TIME_relative_multiply(
1177 GNUNET_TIME_UNIT_SECONDS,
1178 (3600/num_public_records));
1180 num_public_records = 0; //start counting again
1181 namestore_iter = GNUNET_NAMESTORE_zone_iteration_start (namestore_handle,
1183 GNUNET_NAMESTORE_RF_AUTHORITY,
1184 GNUNET_NAMESTORE_RF_PRIVATE,
1190 * Process GNS requests.
1192 * @param cls closure
1193 * @param server the initialized server
1194 * @param c configuration to use
1197 run (void *cls, struct GNUNET_SERVER_Handle *server,
1198 const struct GNUNET_CONFIGURATION_Handle *c)
1201 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Init GNS\n");
1203 char* trusted_entities;
1204 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
1206 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (c, "gns",
1207 "ZONEKEY", &keyfile))
1209 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1210 "No private key for root zone specified%s!\n", keyfile);
1211 GNUNET_SCHEDULER_shutdown(0);
1215 zone_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
1216 GNUNET_CRYPTO_rsa_key_get_public (zone_key, &pkey);
1217 //zone_key = GNUNET_CRYPTO_rsa_key_create ();
1219 GNUNET_CRYPTO_hash(&pkey, GNUNET_CRYPTO_RSA_KEY_LENGTH,
1222 nc = GNUNET_SERVER_notification_context_create (server, 1);
1224 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
1228 GNUNET_CONFIGURATION_get_value_yesno (c, "gns",
1231 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1232 "DNS hijacking enabled... connecting to service.\n");
1234 * Do gnunet dns init here
1236 dns_handle = GNUNET_DNS_connect(c,
1237 GNUNET_DNS_FLAG_PRE_RESOLUTION,
1238 &handle_dns_request, /* rh */
1239 NULL); /* Closure */
1240 if (NULL == dns_handle)
1242 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1243 "Failed to connect to the dnsservice!\n");
1250 * handle to our local namestore
1252 namestore_handle = GNUNET_NAMESTORE_connect(c);
1254 if (NULL == namestore_handle)
1256 //FIXME do error handling;
1257 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1258 "Failed to connect to the namestore!\n");
1259 GNUNET_SCHEDULER_shutdown(0);
1263 char* trusted_start;
1267 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (c, "gns",
1271 trusted_start = trusted_entities;
1272 trusted_len = strlen(trusted_entities);
1273 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1274 "Found trusted entities in config file, importing\n");
1275 while ((trusted_entities-trusted_start) < trusted_len)
1277 trusted_name = trusted_entities;
1278 while (*trusted_entities != ':')
1280 *trusted_entities = '\0';
1282 trusted_key = trusted_entities;
1283 while (*trusted_entities != ',' && (*trusted_entities != '\0'))
1285 *trusted_entities = '\0';
1288 if (GNUNET_YES == GNUNET_DISK_file_test (trusted_key))
1290 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Adding %s:%s to root zone\n",
1293 put_trusted(trusted_name, trusted_key);
1297 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Keyfile %s does not exist!\n",
1299 //put_trusted(trusted_name, trusted_key); //FIXME for testing
1308 dht_handle = GNUNET_DHT_connect(c, 1); //FIXME get ht_len from cfg
1310 if (NULL == dht_handle)
1312 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not connect to DHT!\n");
1315 put_some_records(); //FIXME for testing
1318 * Schedule periodic put
1320 * We have roughly an hour for all records;
1322 dht_update_interval = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS,
1324 zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_start, NULL);
1325 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "GNS Init done!\n");
1331 * The main function for the GNS service.
1333 * @param argc number of arguments from the command line
1334 * @param argv command line arguments
1335 * @return 0 ok, 1 on error
1338 main (int argc, char *const *argv)
1344 GNUNET_SERVICE_run (argc, argv, "gns", GNUNET_SERVICE_OPTION_NONE, &run,
1349 /* end of gnunet-service-gns.c */