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 * Handle to a currenty pending resolution
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);
155 GNUNET_DNS_disconnect(dns_handle);
156 GNUNET_NAMESTORE_disconnect(namestore_handle, 0);
157 GNUNET_DHT_disconnect(dht_handle);
161 * Callback when record data is put into namestore
163 * @param cls the closure
164 * @param success GNUNET_OK on success
165 * @param emsg the error message. NULL if SUCCESS==GNUNET_OK
168 on_namestore_record_put_result(void *cls,
172 if (GNUNET_NO == success)
174 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "records already in namestore\n");
177 else if (GNUNET_YES == success)
179 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
180 "records successfully put in namestore\n");
184 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
185 "Error putting records into namestore: %s\n", emsg);
189 * Function called when we get a result from the dht
192 * @param cls the request handle
193 * @param exp lifetime
194 * @param key the key the record was stored under
195 * @param get_path get path
196 * @param get_path_length get path length
197 * @param put_path put path
198 * @param put_path_length put path length
199 * @param type the block type
200 * @param size the size of the record
201 * @param data the record data
204 process_authority_dht_result(void* cls,
205 struct GNUNET_TIME_Absolute exp,
206 const GNUNET_HashCode * key,
207 const struct GNUNET_PeerIdentity *get_path,
208 unsigned int get_path_length,
209 const struct GNUNET_PeerIdentity *put_path,
210 unsigned int put_path_length,
211 enum GNUNET_BLOCK_Type type,
212 size_t size, const void *data)
214 struct GNUNET_GNS_ResolverHandle *rh;
215 struct GNSNameRecordBlock *nrb;
216 struct GNSRecordBlock *rb;
217 uint32_t num_records;
220 GNUNET_HashCode zone, name_hash;
225 //FIXME check expiration?
227 rh = (struct GNUNET_GNS_ResolverHandle *)cls;
228 nrb = (struct GNSNameRecordBlock*)data;
230 GNUNET_DHT_get_stop (rh->get_handle);
231 rh->get_handle = NULL;
232 num_records = ntohl(nrb->rd_count);
233 struct GNUNET_NAMESTORE_RecordData rd[num_records];
234 name = (char*)&nrb[1];
235 rb = (struct GNSRecordBlock *)&name[strlen(name) + 1];
237 for (i=0; i<num_records; i++)
240 rd[i].record_type = ntohl(rb->type);
241 rd[i].data_size = ntohl(rb->data_length);
243 rd[i].expiration = GNUNET_TIME_absolute_ntoh(rb->expiration);
244 rd[i].flags = ntohl(rb->flags);
246 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
247 "Got name: %s (wanted %s)\n", name, rh->authority_name);
248 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
249 "Got type: %d raw %d (wanted %d)\n",
250 rd[i].record_type, rb->type, GNUNET_GNS_RECORD_PKEY);
251 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
252 "Got data length: %d\n", rd[i].data_size);
253 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
254 "Got flag %d\n", rd[i].flags);
256 if ((strcmp(name, rh->authority_name) == 0) &&
257 (rd[i].record_type == GNUNET_GNS_RECORD_PKEY))
259 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Authority found in DHT\n");
262 (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *)rd[i].data,
266 rb = (struct GNSRecordBlock*)((char*)&rb[1] + rd[i].data_size);
270 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
271 GNUNET_CRYPTO_hash_xor(key, &name_hash, &zone);
273 /* Save to namestore */
274 if (0 == GNUNET_CRYPTO_hash_cmp(&zone_hash, &zone))
276 GNUNET_NAMESTORE_record_put (namestore_handle,
283 &on_namestore_record_put_result, //cont
293 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "No authority in records\n");
294 reply_to_dns(rh, 0, NULL);
298 * Start DHT lookup for a name -> PKEY (compare NS) record in
299 * query->authority's zone
301 * @param rh the pending gns query
302 * @param name the name of the PKEY record
305 resolve_authority_dht(struct GNUNET_GNS_ResolverHandle *rh)
308 struct GNUNET_TIME_Relative timeout;
309 GNUNET_HashCode name_hash;
310 GNUNET_HashCode lookup_key;
312 GNUNET_CRYPTO_hash(rh->authority_name,
313 strlen(rh->authority_name),
315 GNUNET_CRYPTO_hash_xor(&name_hash, &rh->authority, &lookup_key);
317 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5);
319 xquery = htonl(GNUNET_GNS_RECORD_PKEY);
320 //FIXME how long to wait for results?
321 rh->get_handle = GNUNET_DHT_get_start(dht_handle, timeout,
322 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
324 5, //Replication level FIXME
328 &process_authority_dht_result,
334 * Function called when we get a result from the dht
337 * @param cls the request handle
338 * @param exp lifetime
339 * @param key the key the record was stored under
340 * @param get_path get path
341 * @param get_path_length get path length
342 * @param put_path put path
343 * @param put_path_length put path length
344 * @param type the block type
345 * @param size the size of the record
346 * @param data the record data
349 process_name_dht_result(void* cls,
350 struct GNUNET_TIME_Absolute exp,
351 const GNUNET_HashCode * key,
352 const struct GNUNET_PeerIdentity *get_path,
353 unsigned int get_path_length,
354 const struct GNUNET_PeerIdentity *put_path,
355 unsigned int put_path_length,
356 enum GNUNET_BLOCK_Type type,
357 size_t size, const void *data)
359 struct GNUNET_GNS_ResolverHandle *rh;
360 struct GNSNameRecordBlock *nrb;
361 struct GNSRecordBlock *rb;
362 uint32_t num_records;
365 GNUNET_HashCode zone, name_hash;
366 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "got dht result (size=%d)\n", size);
371 //FIXME maybe check expiration here, check block type
373 rh = (struct GNUNET_GNS_ResolverHandle *)cls;
374 nrb = (struct GNSNameRecordBlock*)data;
376 GNUNET_DHT_get_stop (rh->get_handle);
377 rh->get_handle = NULL;
378 num_records = ntohl(nrb->rd_count);
379 struct GNUNET_NAMESTORE_RecordData rd[num_records];
381 name = (char*)&nrb[1];
382 rb = (struct GNSRecordBlock*)&name[strlen(name) + 1];
384 for (i=0; i<num_records; i++)
386 rd[i].record_type = ntohl(rb->type);
387 rd[i].data_size = ntohl(rb->data_length);
388 rd[i].data = (char*)&rb[1];
389 rd[i].expiration = GNUNET_TIME_absolute_ntoh(rb->expiration);
390 rd[i].flags = ntohl(rb->flags);
392 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
393 "Got name: %s (wanted %s)\n", name, rh->name);
394 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
395 "Got type: %d raw %d (wanted %d)\n",
396 rd[i].record_type, rb->type, rh->query->type);
397 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
398 "Got data length: %d\n", rd[i].data_size);
399 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
400 "Got flag %d\n", rd[i].flags);
403 if ((strcmp(name, rh->name) == 0) &&
404 (rd[i].record_type == rh->query->type))
409 rb = (struct GNSRecordBlock*)((char*)&rb[1] + rd[i].data_size);
413 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
414 GNUNET_CRYPTO_hash_xor(key, &name_hash, &zone);
417 * FIXME check pubkey against existing key in namestore?
418 * https://gnunet.org/bugs/view.php?id=2179
421 /* Save to namestore */
422 GNUNET_NAMESTORE_record_put (namestore_handle,
429 &on_namestore_record_put_result, //cont
433 reply_to_dns(rh, num_records, rd);
435 reply_to_dns(rh, 0, NULL);
440 * Start DHT lookup for a (name -> query->record_type) record in
441 * query->authority's zone
443 * @param rh the pending gns query context
444 * @param name the name to query record
447 resolve_name_dht(struct GNUNET_GNS_ResolverHandle *rh, const char* name)
450 struct GNUNET_TIME_Relative timeout;
451 GNUNET_HashCode name_hash;
452 GNUNET_HashCode lookup_key;
453 struct GNUNET_CRYPTO_HashAsciiEncoded lookup_key_string;
455 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
456 GNUNET_CRYPTO_hash_xor(&name_hash, &rh->authority, &lookup_key);
457 GNUNET_CRYPTO_hash_to_enc (&lookup_key, &lookup_key_string);
459 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
460 "starting dht lookup for %s with key: %s\n",
461 name, (char*)&lookup_key_string);
463 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5);
465 xquery = htonl(rh->query->type);
466 //FIXME how long to wait for results?
467 rh->get_handle = GNUNET_DHT_get_start(dht_handle, timeout,
468 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
470 5, //Replication level FIXME
472 &xquery, //xquery FIXME is this bad?
474 &process_name_dht_result,
481 resolve_name(struct GNUNET_GNS_ResolverHandle *rh);
484 * This is a callback function that should give us only PKEY
485 * records. Used to query the namestore for the authority (PKEY)
488 * @param cls the pending query
489 * @param key the key of the zone we did the lookup
490 * @param expiration expiration date of the record data set in the namestore
491 * @param name the name for which we need an authority
492 * @param rd_count the number of records with 'name'
493 * @param rd the record data
494 * @param signature the signature of the authority for the record data
497 process_authority_lookup(void* cls,
498 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
499 struct GNUNET_TIME_Absolute expiration,
501 unsigned int rd_count,
502 const struct GNUNET_NAMESTORE_RecordData *rd,
503 const struct GNUNET_CRYPTO_RsaSignature *signature)
505 struct GNUNET_GNS_ResolverHandle *rh;
506 struct GNUNET_TIME_Relative remaining_time;
507 GNUNET_HashCode zone;
509 rh = (struct GNUNET_GNS_ResolverHandle *)cls;
510 GNUNET_CRYPTO_hash(key,
511 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
513 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
516 * No authority found in namestore.
521 * We did not find an authority in the namestore
522 * _IF_ the current authoritative zone is us we cannot resolve
523 * _ELSE_ we can still check the _expired_ dht
525 if (0 != GNUNET_CRYPTO_hash_cmp(&zone, &zone_hash) &&
526 (remaining_time.rel_value == 0))
528 resolve_authority_dht(rh);
531 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Authority %s unknown\n",
533 reply_to_dns(rh, 0, NULL);
537 //Note only 1 pkey should have been returned.. anything else would be strange
539 * We found an authority that may be able to help us
543 for (i=0; i<rd_count;i++)
546 if (rd[i].record_type != GNUNET_GNS_RECORD_PKEY)
549 if ((GNUNET_TIME_absolute_get_remaining (rd[i].expiration)).rel_value
552 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "This pkey is expired.\n");
553 if (remaining_time.rel_value == 0)
555 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
556 "This dht entry is expired. Refreshing\n");
557 resolve_authority_dht(rh);
564 * Resolve rest of query with new authority
566 GNUNET_assert(rd[i].record_type == GNUNET_GNS_RECORD_PKEY);
567 GNUNET_CRYPTO_hash(rd[i].data,
568 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
578 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
579 "Authority lookup successful but no PKEY... never get here\n");
580 reply_to_dns(rh, 0, NULL);
585 * Reply to client with the result from our lookup.
587 * @param rh the request handle of the lookup
588 * @param rd_count the number of records to return
589 * @param rd the record data
592 reply_to_dns(struct GNUNET_GNS_ResolverHandle *rh, uint32_t rd_count,
593 const struct GNUNET_NAMESTORE_RecordData *rd)
599 struct GNUNET_DNSPARSER_Packet *packet = rh->packet;
600 struct GNUNET_DNSPARSER_Record answer_records[rh->answered];
601 struct GNUNET_DNSPARSER_Record additional_records[rd_count-(rh->answered)];
602 packet->answers = answer_records;
603 packet->additional_records = additional_records;
606 * Put records in the DNS packet and modify it
609 len = sizeof(struct GNUNET_DNSPARSER_Record*);
610 for (i=0; i < rd_count; i++)
613 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
614 "Adding type %d to DNS response\n", rd[i].record_type);
615 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Name: %s\n", rh->name);
616 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "QName: %s\n", rh->query->name);
617 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Record %d/%d\n", i+1, rd_count);
618 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Record len %d\n", rd[i].data_size);
620 if (rd[i].record_type == rh->query->type)
622 answer_records[i].name = rh->query->name;
623 answer_records[i].type = rd[i].record_type;
624 answer_records[i].data.raw.data_len = rd[i].data_size;
625 answer_records[i].data.raw.data = (char*)rd[i].data;
626 answer_records[i].expiration_time = rd[i].expiration;
627 answer_records[i].class = GNUNET_DNSPARSER_CLASS_INTERNET;//hmmn
631 additional_records[i].name = rh->query->name;
632 additional_records[i].type = rd[i].record_type;
633 additional_records[i].data.raw.data_len = rd[i].data_size;
634 additional_records[i].data.raw.data = (char*)rd[i].data;
635 additional_records[i].expiration_time = rd[i].expiration;
636 additional_records[i].class = GNUNET_DNSPARSER_CLASS_INTERNET;//hmmn
640 packet->num_answers = rh->answered;
641 packet->num_additional_records = rd_count-(rh->answered);
643 if (0 == GNUNET_CRYPTO_hash_cmp(&rh->authority, &zone_hash))
644 packet->flags.authoritative_answer = 1;
646 packet->flags.authoritative_answer = 0;
649 packet->flags.return_code = GNUNET_DNSPARSER_RETURN_CODE_NAME_ERROR;
651 packet->flags.return_code = GNUNET_DNSPARSER_RETURN_CODE_NO_ERROR;
653 packet->flags.query_or_response = 1;
659 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
660 "Building DNS response\n");
661 ret = GNUNET_DNSPARSER_pack (packet,
662 1024, /* FIXME magic from dns redirector */
665 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
666 "Built DNS response! (ret=%d,len=%d)\n", ret, len);
667 if (ret == GNUNET_OK)
669 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
670 "Answering DNS request\n");
671 GNUNET_DNS_request_answer(rh->request_handle,
674 //GNUNET_free(answer);
675 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Answered DNS request\n");
679 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
680 "Error building DNS response! (ret=%d)", ret);
683 GNUNET_free(rh->name);
689 * Namestore calls this function if we have record for this name.
690 * (or with rd_count=0 to indicate no matches)
692 * @param cls the pending query
693 * @param key the key of the zone we did the lookup
694 * @param expiration expiration date of the namestore entry
695 * @param name the name for which we need an authority
696 * @param rd_count the number of records with 'name'
697 * @param rd the record data
698 * @param signature the signature of the authority for the record data
701 process_authoritative_result(void* cls,
702 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
703 struct GNUNET_TIME_Absolute expiration,
704 const char *name, unsigned int rd_count,
705 const struct GNUNET_NAMESTORE_RecordData *rd,
706 const struct GNUNET_CRYPTO_RsaSignature *signature)
708 struct GNUNET_GNS_ResolverHandle *rh;
709 struct GNUNET_TIME_Relative remaining_time;
710 GNUNET_HashCode zone;
712 rh = (struct GNUNET_GNS_ResolverHandle *) cls;
713 GNUNET_CRYPTO_hash(key,
714 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
716 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
721 * Lookup terminated and no results
722 * -> DHT Phase unless data is recent
724 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
725 "Namestore lookup for %s terminated without results\n", name);
728 * if this is not our zone we cannot rely on the namestore to be
729 * complete. -> Query DHT
731 if (GNUNET_CRYPTO_hash_cmp(&zone, &zone_hash))
733 if (remaining_time.rel_value == 0)
735 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
737 resolve_name_dht(rh, name);
742 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
743 "Record is still recent. No DHT lookup\n");
748 * Our zone and no result? Cannot resolve TT
750 GNUNET_assert(rh->answered == 0);
751 reply_to_dns(rh, 0, NULL);
758 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
759 "Processing additional result %s from namestore\n", name);
761 for (i=0; i<rd_count;i++)
764 if ((strcmp(name, rh->query->name) == 0)
765 && (rd[i].record_type != rh->query->type))
768 if ((GNUNET_TIME_absolute_get_remaining (rd[i].expiration)).rel_value
771 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "This record is expired. Skipping\n");
781 * consult dht if expired
783 if ((remaining_time.rel_value == 0) && (rh->answered == 0))
785 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
786 "This dht entry is old. Refreshing.\n");
787 resolve_name_dht(rh, name);
791 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Found %d answer(s) to query!\n",
794 reply_to_dns(rh, rd_count, rd);
799 * Determine if this name is canonical.
801 * a.b.gnunet = not canonical
804 * @param name the name to test
805 * @return 1 if canonical
808 is_canonical(char* name)
810 uint32_t len = strlen(name);
813 for (i=0; i<len; i++)
815 if (*(name+i) == '.')
822 * Move one level up in the domain hierarchy and return the
823 * passed top level domain.
825 * @param name the domain
828 char* pop_tld(char* name)
832 if (is_canonical(name))
835 for (len = strlen(name); len > 0; len--)
837 if (*(name+len) == '.')
851 * The first phase of resolution.
852 * First check if the name is canonical.
853 * If it is then try to resolve directly.
854 * If not then we first have to resolve the authoritative entities.
856 * @param rh the pending lookup
859 resolve_name(struct GNUNET_GNS_ResolverHandle *rh)
861 if (is_canonical(rh->name))
863 /* We only need to check the current zone's ns */
864 GNUNET_NAMESTORE_lookup_record(namestore_handle,
868 &process_authoritative_result,
873 /* We have to resolve the authoritative entity first */
874 rh->authority_name = pop_tld(rh->name);
875 GNUNET_NAMESTORE_lookup_record(namestore_handle,
878 GNUNET_GNS_RECORD_PKEY,
879 &process_authority_lookup,
885 * Entry point for name resolution
886 * Setup a new query and try to resolve
888 * @param request the request handle of the DNS request from a client
889 * @param p the DNS query packet we received
890 * @param q the DNS query we received parsed from p
893 start_resolution(struct GNUNET_DNS_RequestHandle *request,
894 struct GNUNET_DNSPARSER_Packet *p,
895 struct GNUNET_DNSPARSER_Query *q)
897 struct GNUNET_GNS_ResolverHandle *rh;
899 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting resolution for (%s)!\n",
902 rh = GNUNET_malloc(sizeof (struct GNUNET_GNS_ResolverHandle));
905 rh->authority = zone_hash;
907 rh->name = GNUNET_malloc(strlen(q->name)
908 - strlen(gnunet_tld) + 1);
910 strlen(q->name)-strlen(gnunet_tld) + 1);
911 memcpy(rh->name, q->name,
912 strlen(q->name)-strlen(gnunet_tld));
914 rh->request_handle = request;
916 /* Start resolution in our zone */
921 * The DNS request handler
922 * Called for every incoming DNS request.
925 * @param rh request handle to user for reply
926 * @param request_length number of bytes in request
927 * @param request udp payload of the DNS request
930 handle_dns_request(void *cls,
931 struct GNUNET_DNS_RequestHandle *rh,
932 size_t request_length,
935 struct GNUNET_DNSPARSER_Packet *p;
938 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Hijacked a DNS request...processing\n");
939 p = GNUNET_DNSPARSER_parse (request, request_length);
943 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
944 "Received malformed DNS packet, leaving it untouched\n");
945 GNUNET_DNS_request_forward (rh);
950 * Check tld and decide if we or
951 * legacy dns is responsible
953 * FIXME now in theory there could be more than 1 query in the request
954 * but if this is case we get into trouble:
955 * either we query the GNS or the DNS. We cannot do both!
956 * So I suggest to either only allow a single query per request or
957 * only allow GNS or DNS requests.
958 * The way it is implemented here now is buggy and will lead to erratic
959 * behaviour (if multiple queries are present).
961 if (p->num_queries == 0)
963 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
964 "No Queries in DNS packet... forwarding\n");
965 GNUNET_DNS_request_forward (rh);
968 if (p->num_queries > 1)
970 /* Note: We could also look for .gnunet */
971 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
972 ">1 queriy in DNS packet... odd. We only process #1\n");
979 tldoffset = p->queries[0].name + strlen(p->queries[0].name);
981 while ((*tldoffset) != '.')
984 if (0 == strcmp(tldoffset, gnunet_tld))
986 start_resolution(rh, p, p->queries);
991 * This request does not concern us. Forward to real DNS.
993 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
994 "Request for %s is forwarded to DNS\n", p->queries[0].name);
995 GNUNET_DNS_request_forward (rh);
1001 * test function that stores some data in the namestore
1002 * This will also be replaced by a test progrm that
1003 * directl interfaces with the namestore
1006 put_some_records(void)
1008 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Populating namestore\n");
1009 /* put an A record into namestore FIXME use gnunet.org */
1010 char* ipB = "5.6.7.8";
1012 struct in_addr *web = GNUNET_malloc(sizeof(struct in_addr));
1013 struct GNUNET_NAMESTORE_RecordData rdb_web;
1015 GNUNET_assert(1 == inet_pton (AF_INET, ipB, web));
1017 rdb_web.data_size = sizeof(struct in_addr);
1019 rdb_web.record_type = GNUNET_DNSPARSER_TYPE_A;
1020 rdb_web.expiration = GNUNET_TIME_absolute_get_forever ();
1022 GNUNET_NAMESTORE_record_create (namestore_handle,
1031 * Method called periodicattluy that triggers
1032 * iteration over root zone
1034 * @param cls closure
1035 * @param tc task context
1038 update_zone_dht_next(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1040 GNUNET_NAMESTORE_zone_iterator_next(namestore_iter);
1044 * Continuation for DHT put
1046 * @param cls closure
1047 * @param tc task context
1050 record_dht_put(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1052 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "put request transmitted\n");
1057 update_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1060 * Function used to put all records successively into the DHT.
1063 * @param cls the closure (NULL)
1064 * @param key the public key of the authority (ours)
1065 * @param expiration lifetime of the namestore entry
1066 * @param name the name of the records
1067 * @param rd_count the number of records in data
1068 * @param rd the record data
1069 * @param signature the signature for the record data
1072 put_gns_record(void *cls,
1073 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
1074 struct GNUNET_TIME_Absolute expiration,
1076 unsigned int rd_count,
1077 const struct GNUNET_NAMESTORE_RecordData *rd,
1078 const struct GNUNET_CRYPTO_RsaSignature *signature)
1080 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1081 "Putting records for %s into the DHT\n", name);
1082 struct GNUNET_TIME_Relative timeout;
1083 struct GNSNameRecordBlock *nrb;
1084 struct GNSRecordBlock *rb;
1085 GNUNET_HashCode name_hash;
1086 GNUNET_HashCode xor_hash;
1087 struct GNUNET_CRYPTO_HashAsciiEncoded xor_hash_string;
1089 uint32_t rd_payload_length;
1094 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Zone iteration finished\n");
1095 GNUNET_NAMESTORE_zone_iteration_stop (namestore_iter);
1096 zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_start,
1101 rd_payload_length = rd_count * sizeof(struct GNSRecordBlock);
1102 rd_payload_length += strlen(name) + 1 + sizeof(struct GNSNameRecordBlock);
1104 /* calculate payload size */
1105 for (i=0; i<rd_count; i++)
1107 rd_payload_length += rd[i].data_size;
1110 nrb = GNUNET_malloc(rd_payload_length);
1112 if (signature != NULL)
1113 memcpy(&nrb->signature, signature,
1114 sizeof(struct GNUNET_CRYPTO_RsaSignature));
1115 //FIXME signature purpose
1116 memcpy(&nrb->public_key, key,
1117 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
1119 nrb->rd_count = htonl(rd_count);
1121 memcpy(&nrb[1], name, strlen(name) + 1); //FIXME is this 0 terminated??
1123 rb = (struct GNSRecordBlock *)((char*)&nrb[1] + strlen(name) + 1);
1125 for (i=0; i<rd_count; i++)
1127 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Read record with type %d\n",
1129 rb->type = htonl(rd[i].record_type);
1130 rb->expiration = GNUNET_TIME_absolute_hton(rd[i].expiration);
1131 rb->data_length = htonl(rd[i].data_size);
1132 rb->flags = htonl(rd[i].flags);
1133 memcpy(&rb[1], rd[i].data, rd[i].data_size);
1134 rb = &rb[1] + rd[i].data_size;
1138 * FIXME magic number 20 move to config file
1141 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 20);
1144 * calculate DHT key: H(name) xor H(pubkey)
1146 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
1147 GNUNET_CRYPTO_hash_xor(&zone_hash, &name_hash, &xor_hash);
1148 GNUNET_CRYPTO_hash_to_enc (&xor_hash, &xor_hash_string);
1149 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1150 "putting records for %s under key: %s with size %d\n",
1151 name, (char*)&xor_hash_string, rd_payload_length);
1153 GNUNET_DHT_put (dht_handle, &xor_hash,
1154 5, //replication level
1156 GNUNET_BLOCK_TYPE_GNS_NAMERECORD, //FIXME todo block plugin
1161 &record_dht_put, //FIXME continuation needed? success check? yes ofc
1162 NULL); //cls for cont
1164 num_public_records++;
1167 * Reschedule periodic put
1169 zone_update_taskid = GNUNET_SCHEDULER_add_delayed (dht_update_interval,
1170 &update_zone_dht_next,
1176 * Puts a single trusted entity into the
1177 * namestore. Will be replaced in a testcase
1178 * that directly interacts with a persistent
1181 * @param name name of entity
1182 * @param keyfile keyfile
1185 put_trusted(char* name, char* keyfile)
1187 struct GNUNET_NAMESTORE_RecordData rd;
1188 struct GNUNET_CRYPTO_RsaPrivateKey *key;
1189 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pkey;
1190 pkey = GNUNET_malloc(sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
1192 key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
1193 GNUNET_CRYPTO_rsa_key_get_public (key, pkey);
1195 rd.expiration = GNUNET_TIME_absolute_get_forever ();
1196 rd.data_size = sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded);
1197 rd.record_type = GNUNET_GNS_RECORD_PKEY;
1199 GNUNET_NAMESTORE_record_create (namestore_handle,
1210 * Periodically iterate over our zone and store everything in dht
1213 * @param tc task context
1216 update_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1218 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Starting DHT zone update!\n");
1219 if (0 == num_public_records)
1221 dht_update_interval = GNUNET_TIME_relative_multiply(
1222 GNUNET_TIME_UNIT_SECONDS,
1227 dht_update_interval = GNUNET_TIME_relative_multiply(
1228 GNUNET_TIME_UNIT_SECONDS,
1229 (3600/num_public_records));
1231 num_public_records = 0; //start counting again
1232 namestore_iter = GNUNET_NAMESTORE_zone_iteration_start (namestore_handle,
1234 GNUNET_NAMESTORE_RF_AUTHORITY,
1235 GNUNET_NAMESTORE_RF_PRIVATE,
1241 * Process GNS requests.
1243 * @param cls closure
1244 * @param server the initialized server
1245 * @param c configuration to use
1248 run (void *cls, struct GNUNET_SERVER_Handle *server,
1249 const struct GNUNET_CONFIGURATION_Handle *c)
1252 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Initializing GNS\n");
1255 char* trusted_entities;
1256 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
1258 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (c, "gns",
1259 "ZONEKEY", &keyfile))
1261 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1262 "No private key for root zone specified%s!\n", keyfile);
1263 GNUNET_SCHEDULER_shutdown(0);
1267 zone_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
1268 GNUNET_CRYPTO_rsa_key_get_public (zone_key, &pkey);
1270 GNUNET_CRYPTO_hash(&pkey, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1273 nc = GNUNET_SERVER_notification_context_create (server, 1);
1275 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
1279 GNUNET_CONFIGURATION_get_value_yesno (c, "gns",
1282 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1283 "DNS hijacking enabled... connecting to service.\n");
1285 * Do gnunet dns init here
1287 dns_handle = GNUNET_DNS_connect(c,
1288 GNUNET_DNS_FLAG_PRE_RESOLUTION,
1289 &handle_dns_request, /* rh */
1290 NULL); /* Closure */
1291 if (NULL == dns_handle)
1293 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1294 "Failed to connect to the dnsservice!\n");
1301 * handle to our local namestore
1303 namestore_handle = GNUNET_NAMESTORE_connect(c);
1305 if (NULL == namestore_handle)
1307 //FIXME do error handling;
1308 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1309 "Failed to connect to the namestore!\n");
1310 GNUNET_SCHEDULER_shutdown(0);
1314 char* trusted_start;
1318 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (c, "gns",
1322 trusted_start = trusted_entities;
1323 trusted_len = strlen(trusted_entities);
1324 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1325 "Found trusted entities in config file, importing\n");
1326 while ((trusted_entities-trusted_start) < trusted_len)
1328 trusted_name = trusted_entities;
1329 while (*trusted_entities != ':')
1331 *trusted_entities = '\0';
1333 trusted_key = trusted_entities;
1334 while (*trusted_entities != ',' && (*trusted_entities != '\0'))
1336 *trusted_entities = '\0';
1339 if (GNUNET_YES == GNUNET_DISK_file_test (trusted_key))
1341 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Adding %s:%s to root zone\n",
1344 put_trusted(trusted_name, trusted_key);
1348 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Keyfile %s does not exist!\n",
1350 //put_trusted(trusted_name, trusted_key); //FIXME for testing
1359 dht_handle = GNUNET_DHT_connect(c, 1); //FIXME get ht_len from cfg
1361 if (NULL == dht_handle)
1363 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not connect to DHT!\n");
1366 put_some_records(); //FIXME for testing
1369 * Schedule periodic put
1371 * We have roughly an hour for all records;
1373 dht_update_interval = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS,
1375 zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_start, NULL);
1381 * The main function for the GNS service.
1383 * @param argc number of arguments from the command line
1384 * @param argv command line arguments
1385 * @return 0 ok, 1 on error
1388 main (int argc, char *const *argv)
1394 GNUNET_SERVICE_run (argc, argv, "gns", GNUNET_SERVICE_OPTION_NONE, &run,
1399 /* end of gnunet-service-gns.c */