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;
71 * we have an authority in namestore that
72 * may be able to resolve
76 /* a handle for dht lookups. should be NULL if no lookups are in progress */
77 struct GNUNET_DHT_GetHandle *get_handle;
83 * Our handle to the DNS handler library
85 struct GNUNET_DNS_Handle *dns_handle;
88 * Our handle to the DHT
90 struct GNUNET_DHT_Handle *dht_handle;
93 * Our zone's private key
95 struct GNUNET_CRYPTO_RsaPrivateKey *zone_key;
98 * Our handle to the namestore service
99 * FIXME maybe need a second handle for iteration
101 struct GNUNET_NAMESTORE_Handle *namestore_handle;
104 * Handle to iterate over our authoritative zone in namestore
106 struct GNUNET_NAMESTORE_ZoneIterator *namestore_iter;
109 * The configuration the GNS service is running with
111 const struct GNUNET_CONFIGURATION_Handle *GNS_cfg;
114 * Our notification context.
116 static struct GNUNET_SERVER_NotificationContext *nc;
121 GNUNET_HashCode zone_hash;
124 * Our tld. Maybe get from config file
126 const char* gnunet_tld = ".gnunet";
129 * Useful for zone update for DHT put
131 static int num_public_records = 3600;
132 struct GNUNET_TIME_Relative dht_update_interval;
133 GNUNET_SCHEDULER_TaskIdentifier zone_update_taskid = GNUNET_SCHEDULER_NO_TASK;
136 void reply_to_dns(struct GNUNET_GNS_ResolverHandle *answer, uint32_t rd_count,
137 const struct GNUNET_NAMESTORE_RecordData *rd);
138 void resolve_name(struct GNUNET_GNS_ResolverHandle *rh);
141 * Task run during shutdown.
147 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
149 //Kill zone task for it may make the scheduler hang
150 GNUNET_SCHEDULER_cancel(zone_update_taskid);
151 GNUNET_DNS_disconnect(dns_handle);
152 GNUNET_NAMESTORE_disconnect(namestore_handle, 0);
153 GNUNET_DHT_disconnect(dht_handle);
157 * Callback when record data is put into namestore
159 * @param cls the closure
160 * @param success GNUNET_OK on success
161 * @param emsg the error message. NULL if SUCCESS==GNUNET_OK
164 on_namestore_record_put_result(void *cls,
168 if (GNUNET_NO == success)
170 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "records already in namestore\n");
173 else if (GNUNET_YES == success)
175 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
176 "records successfully put in namestore\n");
180 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
181 "Error putting records into namestore: %s\n", emsg);
185 * Function called when we get a result from the dht
188 * @param cls the request handle
189 * @param exp lifetime
190 * @param key the key the record was stored under
191 * @param get_path get path
192 * @param get_path_length get path length
193 * @param put_path put path
194 * @param put_path_length put path length
195 * @param type the block type
196 * @param size the size of the record
197 * @param data the record data
200 process_authority_dht_result(void* cls,
201 struct GNUNET_TIME_Absolute exp,
202 const GNUNET_HashCode * key,
203 const struct GNUNET_PeerIdentity *get_path,
204 unsigned int get_path_length,
205 const struct GNUNET_PeerIdentity *put_path,
206 unsigned int put_path_length,
207 enum GNUNET_BLOCK_Type type,
208 size_t size, const void *data)
210 struct GNUNET_GNS_ResolverHandle *rh;
211 struct GNSNameRecordBlock *nrb;
212 struct GNSRecordBlock *rb;
213 uint32_t num_records;
216 GNUNET_HashCode zone, name_hash;
221 //FIXME check expiration?
222 rh = (struct GNUNET_GNS_ResolverHandle *)cls;
223 nrb = (struct GNSNameRecordBlock*)data;
225 GNUNET_DHT_get_stop (rh->get_handle);
226 rh->get_handle = NULL;
227 num_records = ntohl(nrb->rd_count);
228 struct GNUNET_NAMESTORE_RecordData rd[num_records];
229 name = (char*)&nrb[1];
230 rb = (struct GNSRecordBlock *)(&nrb[1] + strlen(name) + 1);
232 for (i=0; i<num_records; i++)
235 rd[i].record_type = ntohl(rb->type);
236 rd[i].data_size = ntohl(rb->data_length);
238 rd[i].expiration = GNUNET_TIME_absolute_ntoh(rb->expiration);
239 rd[i].flags = ntohl(rb->flags);
241 if (strcmp(name, rh->query->name) &&
242 (rd[i].record_type == rh->query->type))
249 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
250 GNUNET_CRYPTO_hash_xor(key, &name_hash, &zone);
253 GNUNET_NAMESTORE_record_put (namestore_handle,
260 &on_namestore_record_put_result, //cont
266 rh->authority = zone;
270 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "No authority in records\n");
271 reply_to_dns(rh, 0, NULL);
275 * Start DHT lookup for a name -> PKEY (compare NS) record in
276 * query->authority's zone
278 * @param rh the pending gns query
279 * @param name the name of the PKEY record
282 resolve_authority_dht(struct GNUNET_GNS_ResolverHandle *rh, const char* name)
285 struct GNUNET_TIME_Relative timeout;
286 GNUNET_HashCode name_hash;
287 GNUNET_HashCode lookup_key;
289 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
290 GNUNET_CRYPTO_hash_xor(&name_hash, &rh->authority, &lookup_key);
292 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5);
294 xquery = htonl(GNUNET_GNS_RECORD_PKEY);
295 //FIXME how long to wait for results?
296 rh->get_handle = GNUNET_DHT_get_start(dht_handle, timeout,
297 GNUNET_BLOCK_TYPE_TEST, //FIXME todo
299 5, //Replication level FIXME
301 &xquery, //xquery FIXME is this bad?
302 0, // for test bp sizeof(GNUNET_GNS_RECORD_PKEY),
303 &process_authority_dht_result,
309 * Function called when we get a result from the dht
312 * @param cls the request handle
313 * @param exp lifetime
314 * @param key the key the record was stored under
315 * @param get_path get path
316 * @param get_path_length get path length
317 * @param put_path put path
318 * @param put_path_length put path length
319 * @param type the block type
320 * @param size the size of the record
321 * @param data the record data
324 process_name_dht_result(void* cls,
325 struct GNUNET_TIME_Absolute exp,
326 const GNUNET_HashCode * key,
327 const struct GNUNET_PeerIdentity *get_path,
328 unsigned int get_path_length,
329 const struct GNUNET_PeerIdentity *put_path,
330 unsigned int put_path_length,
331 enum GNUNET_BLOCK_Type type,
332 size_t size, const void *data)
334 struct GNUNET_GNS_ResolverHandle *rh;
335 struct GNSNameRecordBlock *nrb;
336 struct GNSRecordBlock *rb;
337 uint32_t num_records;
340 GNUNET_HashCode zone, name_hash;
341 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "got dht result\n");
345 //FIXME maybe check expiration here, check block type
347 rh = (struct GNUNET_GNS_ResolverHandle *)cls;
348 nrb = (struct GNSNameRecordBlock*)data;
350 GNUNET_DHT_get_stop (rh->get_handle);
351 rh->get_handle = NULL;
352 num_records = ntohl(nrb->rd_count);
353 struct GNUNET_NAMESTORE_RecordData rd[num_records];
355 name = (char*)&nrb[1];
356 rb = (struct GNSRecordBlock*)(&nrb[1] + strlen(name) + 2);
358 for (i=0; i<num_records; i++)
360 rd[i].record_type = ntohl(rb->type);
361 rd[i].data_size = ntohl(rb->data_length);
362 rd[i].data = (char*)&rb[1];
363 rd[i].expiration = GNUNET_TIME_absolute_ntoh(rb->expiration);
364 rd[i].flags = ntohl(rb->flags);
365 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
366 "Got name: %s (wanted %s)\n", name, rh->name);
367 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
368 "Got type: %d (wanted %d)\n",
369 rd[i].record_type, rh->query->type);
370 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
371 "Got data length: %d\n", rd[i].data_size);
372 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
373 "Got flag %d\n", rd[i].flags);
375 if (strcmp(name, rh->name) &&
376 (rd[i].record_type == rh->query->type))
381 rb = &rb[1] + rd[i].data_size;
385 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
386 GNUNET_CRYPTO_hash_xor(key, &name_hash, &zone);
388 //FIXME check pubkey against existing key in namestore?
389 //https://gnunet.org/bugs/view.php?id=2179
392 GNUNET_NAMESTORE_record_put (namestore_handle,
399 &on_namestore_record_put_result, //cont
403 reply_to_dns(rh, num_records, rd);
405 reply_to_dns(rh, 0, NULL);
410 * Start DHT lookup for a (name -> query->record_type) record in
411 * query->authority's zone
413 * @param rh the pending gns query context
414 * @param name the name to query record
417 resolve_name_dht(struct GNUNET_GNS_ResolverHandle *rh, const char* name)
420 struct GNUNET_TIME_Relative timeout;
421 GNUNET_HashCode name_hash;
422 GNUNET_HashCode lookup_key;
423 struct GNUNET_CRYPTO_HashAsciiEncoded lookup_key_string;
425 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
426 GNUNET_CRYPTO_hash_xor(&name_hash, &rh->authority, &lookup_key);
427 GNUNET_CRYPTO_hash_to_enc (&lookup_key, &lookup_key_string);
428 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
429 "starting dht lookup for %s with key: %s\n",
430 name, (char*)&lookup_key_string);
432 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5);
434 xquery = htonl(rh->query->type);
435 //FIXME how long to wait for results?
436 rh->get_handle = GNUNET_DHT_get_start(dht_handle, timeout,
437 GNUNET_BLOCK_TYPE_TEST, //FIXME todo
439 5, //Replication level FIXME
441 &xquery, //xquery FIXME is this bad?
442 0, //for test bp sizeof(rh->query->type),
443 &process_name_dht_result,
450 resolve_name(struct GNUNET_GNS_ResolverHandle *rh);
453 * This is a callback function that should give us only PKEY
454 * records. Used to query the namestore for the authority (PKEY)
457 * @param cls the pending query
458 * @param key the key of the zone we did the lookup
459 * @param expiration expiration date of the record data set in the namestore
460 * @param name the name for which we need an authority
461 * @param rd_count the number of records with 'name'
462 * @param rd the record data
463 * @param signature the signature of the authority for the record data
466 process_authority_lookup(void* cls,
467 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
468 struct GNUNET_TIME_Absolute expiration,
470 unsigned int rd_count,
471 const struct GNUNET_NAMESTORE_RecordData *rd,
472 const struct GNUNET_CRYPTO_RsaSignature *signature)
474 struct GNUNET_GNS_ResolverHandle *rh;
475 struct GNUNET_TIME_Relative remaining_time;
476 GNUNET_HashCode zone;
478 rh = (struct GNUNET_GNS_ResolverHandle *)cls;
479 GNUNET_CRYPTO_hash(key, GNUNET_CRYPTO_RSA_KEY_LENGTH, &zone);
480 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
483 * No authority found in namestore.
488 * We did not find an authority in the namestore
489 * _IF_ the current authoritative zone is us we cannot resolve
490 * _ELSE_ we can still check the _expired_ dht
492 if (0 != GNUNET_CRYPTO_hash_cmp(&zone, &zone_hash) &&
493 (remaining_time.rel_value == 0))
495 resolve_authority_dht(rh, name);
498 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Authority unknown\n");
499 reply_to_dns(rh, 0, NULL);
503 //Note only 1 pkey should have been returned.. anything else would be strange
505 * We found an authority that may be able to help us
509 for (i=0; i<rd_count;i++)
512 if (strcmp(name, rh->query->name) && rd[i].record_type
513 != GNUNET_GNS_RECORD_PKEY)
516 if ((GNUNET_TIME_absolute_get_remaining (rd[i].expiration)).rel_value
519 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "This pkey is expired.\n");
520 if (remaining_time.rel_value == 0)
522 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
523 "This dht entry is expired. Refreshing\n");
524 resolve_authority_dht(rh, name);
530 GNUNET_assert(rd[i].record_type == GNUNET_GNS_RECORD_PKEY);
531 GNUNET_CRYPTO_hash(rd[i].data, GNUNET_CRYPTO_RSA_KEY_LENGTH,
542 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
543 "Authority lookup successful but no PKEY... never get here?\n");
544 reply_to_dns(rh, 0, NULL);
549 * Reply to client with the result from our lookup.
551 * @param rh the request handle of the lookup
552 * @param rd_count the number of records to return
553 * @param rd the record data
556 reply_to_dns(struct GNUNET_GNS_ResolverHandle *rh, uint32_t rd_count,
557 const struct GNUNET_NAMESTORE_RecordData *rd)
563 struct GNUNET_DNSPARSER_Packet *packet = rh->packet;
564 struct GNUNET_DNSPARSER_Record answer_records[rh->answered];
565 struct GNUNET_DNSPARSER_Record additional_records[rd_count-(rh->answered)];
566 packet->answers = answer_records;
567 packet->additional_records = additional_records;
569 len = sizeof(struct GNUNET_DNSPARSER_Record*);
570 for (i=0; i < rd_count; i++)
572 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
573 "Adding type %d to DNS response\n", rd[i].record_type);
574 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Name: %s\n", rh->name);
575 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "QName: %s\n", rh->query->name);
576 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Record %d/%d\n", i+1, rd_count);
577 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Record len %d\n", rd[i].data_size);
578 if (rd[i].record_type == rh->query->type)
580 answer_records[i].name = rh->query->name;
581 answer_records[i].type = rd[i].record_type;
582 answer_records[i].data.raw.data_len = rd[i].data_size;
583 answer_records[i].data.raw.data = (char*)rd[i].data;
584 answer_records[i].expiration_time = rd[i].expiration;
585 answer_records[i].class = GNUNET_DNSPARSER_CLASS_INTERNET;//hmmn
589 additional_records[i].name = rh->query->name;
590 additional_records[i].type = rd[i].record_type;
591 additional_records[i].data.raw.data_len = rd[i].data_size;
592 additional_records[i].data.raw.data = (char*)rd[i].data;
593 additional_records[i].expiration_time = rd[i].expiration;
594 additional_records[i].class = GNUNET_DNSPARSER_CLASS_INTERNET;//hmmn
596 //GNUNET_free(i->record); DO this later!
599 packet->num_answers = rh->answered; //answer->num_records;
600 packet->num_additional_records = rd_count-(rh->answered);
602 if (0 == GNUNET_CRYPTO_hash_cmp(&rh->authority, &zone_hash))
603 packet->flags.authoritative_answer = 1;
605 packet->flags.authoritative_answer = 0;
608 packet->flags.return_code = GNUNET_DNSPARSER_RETURN_CODE_NAME_ERROR;
610 packet->flags.return_code = GNUNET_DNSPARSER_RETURN_CODE_NO_ERROR;
612 packet->flags.query_or_response = 1;
614 //FIXME this is silently discarded
615 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
616 "Building DNS response\n");
617 ret = GNUNET_DNSPARSER_pack (packet,
618 1024, /* FIXME magic from dns redirector */
621 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
622 "Built DNS response! (ret=%d,len=%d)\n", ret, len);
623 if (ret == GNUNET_OK)
625 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
626 "Answering DNS request\n");
627 GNUNET_DNS_request_answer(rh->request_handle,
630 //GNUNET_free(answer);
631 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Answered DNS request\n");
632 //FIXME return code, free datastructures
636 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
637 "Error building DNS response! (ret=%d)", ret);
640 //FIXME into free_resolver(rh)
641 GNUNET_free(rh->name);
647 * Namestore calls this function if we have an entry for this name.
648 * (or data=null to indicate the lookup has finished
650 * @param cls the pending query
651 * @param key the key of the zone we did the lookup
652 * @param expiration expiration date of the namestore entry
653 * @param name the name for which we need an authority
654 * @param rd_count the number of records with 'name'
655 * @param rd the record data
656 * @param signature the signature of the authority for the record data
659 process_authoritative_result(void* cls,
660 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
661 struct GNUNET_TIME_Absolute expiration,
662 const char *name, unsigned int rd_count,
663 const struct GNUNET_NAMESTORE_RecordData *rd,
664 const struct GNUNET_CRYPTO_RsaSignature *signature)
666 struct GNUNET_GNS_ResolverHandle *rh;
667 struct GNUNET_TIME_Relative remaining_time;
668 GNUNET_HashCode zone;
670 rh = (struct GNUNET_GNS_ResolverHandle *) cls;
671 GNUNET_CRYPTO_hash(key, GNUNET_CRYPTO_RSA_KEY_LENGTH, &zone);
672 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
674 //FIXME Handle results in rd
679 * Lookup terminated and no results
680 * -> DHT Phase unless data is recent
682 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
683 "Namestore lookup for %s terminated without results\n", name);
686 * if this is not our zone we cannot rely on the namestore to be
687 * complete. -> Query DHT
689 if (GNUNET_CRYPTO_hash_cmp(&zone, &zone_hash))
691 if (remaining_time.rel_value == 0)
693 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
695 resolve_name_dht(rh, name);
700 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
701 "Record is still recent. No DHT lookup\n");
706 * Our zone and no result? Cannot resolve TT
708 GNUNET_assert(rh->answered == 0);
709 reply_to_dns(rh, 0, NULL); //answered should be 0
716 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
717 "Processing additional result %s from namestore\n", name);
719 for (i=0; i<rd_count;i++)
722 if (strcmp(name, rh->query->name) && rd[i].record_type != rh->query->type)
725 if ((GNUNET_TIME_absolute_get_remaining (rd[i].expiration)).rel_value
728 //FIXME there is a catch here...
729 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "This record is expired. Skipping\n");
739 * consult dht if expired
741 if ((remaining_time.rel_value == 0) && (rh->answered == 0))
743 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
744 "This dht entry is old. Refreshing.\n");
745 resolve_name_dht(rh, name);
749 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Found %d answer(s) to query!\n",
752 reply_to_dns(rh, rd_count, rd);
757 * Determine if this name is canonical.
759 * a.b.gnunet = not canonical
762 * @param name the name to test
763 * @return 1 if canonical
766 is_canonical(char* name)
768 uint32_t len = strlen(name);
771 for (i=0; i<len; i++)
773 if (*(name+i) == '.')
780 * Move one level up in the domain hierarchy and return the
781 * passed top level domain.
783 * @param name the domain
786 char* pop_tld(char* name)
790 if (is_canonical(name))
793 for (len = strlen(name); len > 0; len--)
795 if (*(name+len) == '.')
802 name[len] = '\0'; //terminate string
809 * The first phase of resolution.
810 * First check if the name is canonical.
811 * If it is then try to resolve directly.
812 * If not then first have to resolve the authoritative entities.
814 * @param rh the pending lookup
815 * @param zone the zone we are currently resolving in
818 resolve_name(struct GNUNET_GNS_ResolverHandle *rh)
820 if (is_canonical(rh->name))
822 //We only need to check this zone's ns
823 GNUNET_NAMESTORE_lookup_record(namestore_handle,
827 &process_authoritative_result,
832 //We have to resolve the authoritative entity
833 char *new_authority = pop_tld(rh->name);
834 GNUNET_NAMESTORE_lookup_record(namestore_handle,
837 GNUNET_GNS_RECORD_PKEY,
838 &process_authority_lookup,
844 * Entry point for name resolution
845 * Lookup local namestore of our zone.
847 * Setup a new query and try to resolve
849 * @param request the request handle of the DNS request from a client
850 * @param p the DNS query packet we received
851 * @param q the DNS query we received parsed from p
854 start_resolution(struct GNUNET_DNS_RequestHandle *request,
855 struct GNUNET_DNSPARSER_Packet *p,
856 struct GNUNET_DNSPARSER_Query *q)
858 struct GNUNET_GNS_ResolverHandle *rh;
860 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Starting resolution for (%s)!\n",
863 rh = GNUNET_malloc(sizeof (struct GNUNET_GNS_ResolverHandle));
866 rh->authority = zone_hash;
868 //FIXME do not forget to free!!
869 rh->name = GNUNET_malloc(strlen(q->name)
870 - strlen(gnunet_tld) + 1);
872 strlen(q->name)-strlen(gnunet_tld) + 1);
873 memcpy(rh->name, q->name,
874 strlen(q->name)-strlen(gnunet_tld));
876 rh->request_handle = request;
878 //Start resolution in our zone
883 * The DNS request handler
884 * Called for every incoming DNS request.
887 * @param rh request handle to user for reply
888 * @param request_length number of bytes in request
889 * @param request udp payload of the DNS request
892 handle_dns_request(void *cls,
893 struct GNUNET_DNS_RequestHandle *rh,
894 size_t request_length,
897 struct GNUNET_DNSPARSER_Packet *p;
900 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Hijacked a DNS request...processing\n");
901 p = GNUNET_DNSPARSER_parse (request, request_length);
905 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
906 "Received malformed DNS packet, leaving it untouched\n");
907 GNUNET_DNS_request_forward (rh);
912 * Check tld and decide if we or
913 * legacy dns is responsible
915 * FIXME now in theory there could be more than 1 query in the request
916 * but if this is case we get into trouble:
917 * either we query the GNS or the DNS. We cannot do both!
918 * So I suggest to either only allow a single query per request or
919 * only allow GNS or DNS requests.
920 * The way it is implemented here now is buggy and will lead to erratic
921 * behaviour (if multiple queries are present).
923 if (p->num_queries == 0)
925 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
926 "No Queries in DNS packet... forwarding\n");
927 GNUNET_DNS_request_forward (rh);
930 if (p->num_queries > 1)
932 //Note: We could also look for .gnunet
933 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
934 ">1 queriy in DNS packet... odd. We only process #1\n");
938 tldoffset = p->queries[0].name + strlen(p->queries[0].name);
940 while ((*tldoffset) != '.')
943 if (0 == strcmp(tldoffset, gnunet_tld))
945 start_resolution(rh, p, p->queries);
950 * This request does not concern us. Forward to real DNS.
952 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
953 "Request for %s is forwarded to DNS\n", p->queries[0].name);
954 GNUNET_DNS_request_forward (rh);
960 * test function that stores some data in the namestore
963 put_some_records(void)
965 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Populating namestore\n");
966 /* put a few records into namestore */
967 char* ipA = "1.2.3.4";
968 char* ipB = "5.6.7.8";
969 //struct GNUNET_CRYPTO_RsaPrivateKey *bob_key = GNUNET_CRYPTO_rsa_key_create ();
970 //struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *bob;
971 //bob = GNUNET_malloc(sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
973 //GNUNET_CRYPTO_rsa_key_get_public (bob_key, bob);
975 //GNUNET_HashCode *bob_zone = GNUNET_malloc(sizeof(GNUNET_HashCode));
977 //GNUNET_CRYPTO_hash(bob, GNUNET_CRYPTO_RSA_KEY_LENGTH, bob_zone);
979 struct in_addr *alice = GNUNET_malloc(sizeof(struct in_addr));
980 struct in_addr *web = GNUNET_malloc(sizeof(struct in_addr));
981 struct GNUNET_NAMESTORE_RecordData rda;
982 //struct GNUNET_NAMESTORE_RecordData rdb;
983 struct GNUNET_NAMESTORE_RecordData rdb_web;
985 GNUNET_assert(1 == inet_pton (AF_INET, ipA, alice));
986 GNUNET_assert(1 == inet_pton (AF_INET, ipB, web));
988 rda.data_size = sizeof(struct in_addr);
989 rdb_web.data_size = sizeof(struct in_addr);
990 //rdb.data_size = sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded);
994 rda.record_type = GNUNET_GNS_RECORD_TYPE_A;
995 rdb_web.record_type = GNUNET_DNSPARSER_TYPE_A;
996 //rdb.record_type = GNUNET_GNS_RECORD_PKEY;
997 rdb_web.expiration = GNUNET_TIME_absolute_get_forever ();
998 rda.expiration = GNUNET_TIME_absolute_get_forever ();
999 //rdb.expiration = GNUNET_TIME_absolute_get_forever ();
1001 //alice.gnunet A IN 1.2.3.4
1002 GNUNET_NAMESTORE_record_create (namestore_handle,
1008 GNUNET_NAMESTORE_record_create (namestore_handle,
1015 //www.bob.gnunet A IN 5.6.7.8
1016 GNUNET_NAMESTORE_record_create (namestore_handle,
1022 /*GNUNET_NAMESTORE_record_put(namestore_handle,
1025 GNUNET_TIME_absolute_get_forever (),
1034 update_zone_dht_next(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1036 GNUNET_NAMESTORE_zone_iterator_next(namestore_iter);
1040 record_dht_put(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1042 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "put request transmitted\n");
1047 update_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1050 * Function used to put all records successively into the DHT.
1053 * @param cls the closure (NULL)
1054 * @param key the public key of the authority (ours)
1055 * @param expiration lifetime of the namestore entry
1056 * @param name the name of the records
1057 * @param rd_count the number of records in data
1058 * @param rd the record data
1059 * @param signature the signature for the record data
1062 put_gns_record(void *cls,
1063 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
1064 struct GNUNET_TIME_Absolute expiration,
1066 unsigned int rd_count,
1067 const struct GNUNET_NAMESTORE_RecordData *rd,
1068 const struct GNUNET_CRYPTO_RsaSignature *signature)
1070 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Putting records into the DHT\n");
1071 struct GNUNET_TIME_Relative timeout;
1072 struct GNSNameRecordBlock *nrb;
1073 struct GNSRecordBlock *rb;
1074 GNUNET_HashCode name_hash;
1075 GNUNET_HashCode xor_hash;
1076 struct GNUNET_CRYPTO_HashAsciiEncoded xor_hash_string;
1078 uint32_t rd_payload_length;
1080 if (NULL == name) //We're done
1082 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Zone iteration finished\n");
1083 GNUNET_NAMESTORE_zone_iteration_stop (namestore_iter);
1084 zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_start,
1089 rd_payload_length = rd_count * sizeof(struct GNSRecordBlock);
1090 rd_payload_length += strlen(name) + 1 + sizeof(struct GNSNameRecordBlock);
1091 //Calculate payload size
1092 for (i=0; i<rd_count; i++)
1094 rd_payload_length += rd[i].data_size;
1097 nrb = GNUNET_malloc(rd_payload_length);
1099 if (signature != NULL)
1100 memcpy(&nrb->signature, signature,
1101 sizeof(struct GNUNET_CRYPTO_RsaSignature));
1102 //FIXME signature purpose
1103 memcpy(&nrb->public_key, key,
1104 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
1106 nrb->rd_count = htonl(rd_count);
1108 memcpy(&nrb[1], name, strlen(name) + 1); //FIXME is this 0 terminated??-sure hope so for we use strlen
1110 rb = (struct GNSRecordBlock *)(&nrb[1] + strlen(name) + 1);
1112 for (i=0; i<rd_count; i++)
1114 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "putting record with type %d\n",
1116 rb->type = htonl(rd[i].record_type);
1117 rb->expiration = GNUNET_TIME_absolute_hton(rd[i].expiration);
1118 rb->data_length = htonl(rd[i].data_size);
1119 rb->flags = htonl(rd[i].flags);
1120 memcpy(&rb[1], rd[i].data, rd[i].data_size);
1121 rb = &rb[1] + rd[i].data_size;
1125 * FIXME magic number 20 move to config file
1128 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 20);
1129 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
1130 GNUNET_CRYPTO_hash_xor(&zone_hash, &name_hash, &xor_hash);
1131 GNUNET_CRYPTO_hash_to_enc (&xor_hash, &xor_hash_string);
1132 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "putting new record %s under key: %s\n",
1133 name, (char*)&xor_hash_string);
1135 GNUNET_DHT_put (dht_handle, &xor_hash,
1136 5, //replication level
1138 GNUNET_BLOCK_TYPE_TEST, //FIXME todo block plugin
1143 &record_dht_put, //FIXME continuation needed? success check? yes ofc
1144 NULL); //cls for cont
1146 num_public_records++;
1149 * Reschedule periodic put
1151 zone_update_taskid = GNUNET_SCHEDULER_add_delayed (dht_update_interval,
1152 &update_zone_dht_next,
1158 put_trusted(char* name, char* keyfile)
1160 struct GNUNET_NAMESTORE_RecordData rd;
1161 struct GNUNET_CRYPTO_RsaPrivateKey *key;
1162 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pkey;
1163 pkey = GNUNET_malloc(sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
1165 key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
1166 GNUNET_CRYPTO_rsa_key_get_public (key, pkey);
1168 rd.expiration = GNUNET_TIME_absolute_get_forever ();
1169 rd.data_size = sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded);
1170 rd.record_type = GNUNET_GNS_RECORD_PKEY;
1172 GNUNET_NAMESTORE_record_create (namestore_handle,
1183 * Periodically iterate over our zone and store everything in dht
1186 * @param tc task context
1189 update_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1191 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Update zone!\n");
1192 if (0 == num_public_records)
1194 dht_update_interval = GNUNET_TIME_relative_multiply(
1195 GNUNET_TIME_UNIT_SECONDS,
1200 dht_update_interval = GNUNET_TIME_relative_multiply(
1201 GNUNET_TIME_UNIT_SECONDS,
1202 (3600/num_public_records));
1204 num_public_records = 0; //start counting again
1205 namestore_iter = GNUNET_NAMESTORE_zone_iteration_start (namestore_handle,
1207 GNUNET_NAMESTORE_RF_AUTHORITY,
1208 GNUNET_NAMESTORE_RF_PRIVATE,
1214 * Process GNS requests.
1216 * @param cls closure
1217 * @param server the initialized server
1218 * @param c configuration to use
1221 run (void *cls, struct GNUNET_SERVER_Handle *server,
1222 const struct GNUNET_CONFIGURATION_Handle *c)
1225 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Init GNS\n");
1227 char* trusted_entities;
1228 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
1230 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (c, "gns",
1231 "ZONEKEY", &keyfile))
1233 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1234 "No private key for root zone specified%s!\n", keyfile);
1235 GNUNET_SCHEDULER_shutdown(0);
1239 zone_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
1240 GNUNET_CRYPTO_rsa_key_get_public (zone_key, &pkey);
1241 //zone_key = GNUNET_CRYPTO_rsa_key_create ();
1243 GNUNET_CRYPTO_hash(&pkey, GNUNET_CRYPTO_RSA_KEY_LENGTH,
1246 nc = GNUNET_SERVER_notification_context_create (server, 1);
1248 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
1252 GNUNET_CONFIGURATION_get_value_yesno (c, "gns",
1256 * Do gnunet dns init here
1258 dns_handle = GNUNET_DNS_connect(c,
1259 GNUNET_DNS_FLAG_PRE_RESOLUTION,
1260 &handle_dns_request, /* rh */
1261 NULL); /* Closure */
1262 if (NULL == dns_handle)
1264 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1265 "Failed to connect to the dnsservice!\n");
1272 * handle to our local namestore
1274 namestore_handle = GNUNET_NAMESTORE_connect(c);
1276 if (NULL == namestore_handle)
1278 //FIXME do error handling;
1279 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1280 "Failed to connect to the namestore!\n");
1281 GNUNET_SCHEDULER_shutdown(0);
1285 char* trusted_start;
1289 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (c, "gns",
1293 trusted_start = trusted_entities;
1294 trusted_len = strlen(trusted_entities);
1295 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1296 "Found trusted entities in config file, importing\n");
1297 while ((trusted_entities-trusted_start) < trusted_len)
1299 trusted_name = trusted_entities;
1300 while (*trusted_entities != ':')
1302 *trusted_entities = '\0';
1304 trusted_key = trusted_entities;
1305 while (*trusted_entities != ',' && (*trusted_entities != '\0'))
1307 *trusted_entities = '\0';
1310 if (GNUNET_YES == GNUNET_DISK_file_test (trusted_key))
1312 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Adding %s:%s to root zone\n",
1315 put_trusted(trusted_name, trusted_key);
1319 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Keyfile %s does not exist!\n",
1321 //put_trusted(trusted_name, trusted_key); //FIXME for testing
1330 dht_handle = GNUNET_DHT_connect(c, 1); //FIXME get ht_len from cfg
1332 if (NULL == dht_handle)
1334 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not connect to DHT!\n");
1337 put_some_records(); //FIXME for testing
1340 * Schedule periodic put
1342 * We have roughly an hour for all records;
1344 dht_update_interval = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS,
1346 zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_start, NULL);
1347 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "GNS Init done!\n");
1353 * The main function for the GNS service.
1355 * @param argc number of arguments from the command line
1356 * @param argv command line arguments
1357 * @return 0 ok, 1 on error
1360 main (int argc, char *const *argv)
1366 GNUNET_SERVICE_run (argc, argv, "gns", GNUNET_SERVICE_OPTION_NONE, &run,
1371 /* end of gnunet-service-gns.c */