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;
218 //FIXME GNS block check
223 //FIXME check expiration?
224 rh = (struct GNUNET_GNS_ResolverHandle *)cls;
225 nrb = (struct GNSNameRecordBlock*)data;
227 GNUNET_DHT_get_stop (rh->get_handle);
228 rh->get_handle = NULL;
229 num_records = ntohl(nrb->rd_count);
230 struct GNUNET_NAMESTORE_RecordData rd[num_records];
231 name = (char*)&nrb[1];
232 rb = (struct GNSRecordBlock *)(&nrb[1] + strlen(name));
234 for (i=0; i<num_records; i++)
237 rd[i].record_type = ntohl(rb->type);
238 rd[i].data_size = ntohl(rb->data_length);
240 rd[i].expiration = GNUNET_TIME_absolute_ntoh(rb->expiration);
241 rd[i].flags = ntohs(rb->flags);
243 if (strcmp(name, rh->query->name) &&
244 (rd[i].record_type == rh->query->type))
251 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
252 GNUNET_CRYPTO_hash_xor(key, &name_hash, &zone);
255 GNUNET_NAMESTORE_record_put (namestore_handle,
262 &on_namestore_record_put_result, //cont
268 rh->authority = zone;
272 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "No authority in records\n");
273 reply_to_dns(rh, 0, NULL);
277 * Start DHT lookup for a name -> PKEY (compare NS) record in
278 * query->authority's zone
280 * @param rh the pending gns query
281 * @param name the name of the PKEY record
284 resolve_authority_dht(struct GNUNET_GNS_ResolverHandle *rh, const char* name)
286 enum GNUNET_GNS_RecordType rtype = GNUNET_GNS_RECORD_PKEY;
287 struct GNUNET_TIME_Relative timeout;
288 GNUNET_HashCode name_hash;
289 GNUNET_HashCode lookup_key;
291 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
292 GNUNET_CRYPTO_hash_xor(&name_hash, &rh->authority, &lookup_key);
294 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 20);
296 //FIXME how long to wait for results?
297 rh->get_handle = GNUNET_DHT_get_start(dht_handle, timeout,
298 GNUNET_BLOCK_TYPE_TEST, //FIXME todo
300 5, //Replication level FIXME
302 &rtype, //xquery FIXME this is bad
303 sizeof(GNUNET_GNS_RECORD_PKEY),
304 &process_authority_dht_result,
310 * Function called when we get a result from the dht
313 * @param cls the request handle
314 * @param exp lifetime
315 * @param key the key the record was stored under
316 * @param get_path get path
317 * @param get_path_length get path length
318 * @param put_path put path
319 * @param put_path_length put path length
320 * @param type the block type
321 * @param size the size of the record
322 * @param data the record data
325 process_name_dht_result(void* cls,
326 struct GNUNET_TIME_Absolute exp,
327 const GNUNET_HashCode * key,
328 const struct GNUNET_PeerIdentity *get_path,
329 unsigned int get_path_length,
330 const struct GNUNET_PeerIdentity *put_path,
331 unsigned int put_path_length,
332 enum GNUNET_BLOCK_Type type,
333 size_t size, const void *data)
335 struct GNUNET_GNS_ResolverHandle *rh;
336 struct GNSNameRecordBlock *nrb;
337 struct GNSRecordBlock *rb;
338 uint32_t num_records;
341 GNUNET_HashCode zone, name_hash;
346 //FIXME maybe check expiration here, check block type
348 rh = (struct GNUNET_GNS_ResolverHandle *)cls;
349 nrb = (struct GNSNameRecordBlock*)data;
351 GNUNET_DHT_get_stop (rh->get_handle);
352 rh->get_handle = NULL;
353 num_records = ntohl(nrb->rd_count);
354 struct GNUNET_NAMESTORE_RecordData rd[num_records];
356 name = (char*)&nrb[1];
357 rb = (struct GNSRecordBlock*)(&nrb[1] + strlen(name));
359 for (i=0; i<num_records; i++)
361 rd[i].record_type = ntohl(rb->type);
362 rd[i].data_size = ntohl(rb->data_length);
363 rd[i].data = (char*)&rb[1];
364 rd[i].expiration = GNUNET_TIME_absolute_ntoh(rb->expiration);
365 rd[i].flags = ntohs(rb->flags);
367 if (strcmp(name, rh->query->name) &&
368 (rd[i].record_type == rh->query->type))
375 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
376 GNUNET_CRYPTO_hash_xor(key, &name_hash, &zone);
378 //FIXME check pubkey against existing key in namestore?
379 //https://gnunet.org/bugs/view.php?id=2179
382 GNUNET_NAMESTORE_record_put (namestore_handle,
389 &on_namestore_record_put_result, //cont
393 reply_to_dns(rh, num_records, rd);
395 reply_to_dns(rh, 0, NULL);
400 * Start DHT lookup for a (name -> query->record_type) record in
401 * query->authority's zone
403 * @param rh the pending gns query context
404 * @param name the name to query record
407 resolve_name_dht(struct GNUNET_GNS_ResolverHandle *rh, const char* name)
409 struct GNUNET_TIME_Relative timeout;
410 GNUNET_HashCode name_hash;
411 GNUNET_HashCode lookup_key;
413 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
414 GNUNET_CRYPTO_hash_xor(&name_hash, &rh->authority, &lookup_key);
417 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 20);
419 //FIXME how long to wait for results?
420 rh->get_handle = GNUNET_DHT_get_start(dht_handle, timeout,
421 GNUNET_BLOCK_TYPE_TEST, //FIXME todo
423 5, //Replication level FIXME
425 &rh->query->type, //xquery
426 sizeof(rh->query->type),
427 &process_name_dht_result,
434 resolve_name(struct GNUNET_GNS_ResolverHandle *rh);
437 * This is a callback function that should give us only PKEY
438 * records. Used to query the namestore for the authority (PKEY)
441 * @param cls the pending query
442 * @param key the key of the zone we did the lookup
443 * @param expiration expiration date of the record data set in the namestore
444 * @param name the name for which we need an authority
445 * @param rd_count the number of records with 'name'
446 * @param rd the record data
447 * @param signature the signature of the authority for the record data
450 process_authority_lookup(void* cls,
451 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
452 struct GNUNET_TIME_Absolute expiration,
454 unsigned int rd_count,
455 const struct GNUNET_NAMESTORE_RecordData *rd,
456 const struct GNUNET_CRYPTO_RsaSignature *signature)
458 struct GNUNET_GNS_ResolverHandle *rh;
459 struct GNUNET_TIME_Relative remaining_time;
460 GNUNET_HashCode zone;
462 rh = (struct GNUNET_GNS_ResolverHandle *)cls;
463 GNUNET_CRYPTO_hash(key, GNUNET_CRYPTO_RSA_KEY_LENGTH, &zone);
464 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
467 * No authority found in namestore.
472 * We did not find an authority in the namestore
473 * _IF_ the current authoritative zone is us we cannot resolve
474 * _ELSE_ we can still check the _expired_ dht
476 if (0 != GNUNET_CRYPTO_hash_cmp(&zone, &zone_hash) &&
477 (remaining_time.rel_value == 0))
479 resolve_authority_dht(rh, name);
482 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Authority unknown\n");
483 reply_to_dns(rh, 0, NULL);
487 //Note only 1 pkey should have been returned.. anything else would be strange
489 * We found an authority that may be able to help us
493 for (i=0; i<rd_count;i++)
496 if (strcmp(name, rh->query->name) && rd[i].record_type
497 != GNUNET_GNS_RECORD_PKEY)
500 if ((GNUNET_TIME_absolute_get_remaining (rd[i].expiration)).rel_value
503 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "This pkey is expired.\n");
504 if (remaining_time.rel_value == 0)
506 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
507 "This dht entry is expired. Refreshing\n");
508 resolve_authority_dht(rh, name);
514 GNUNET_assert(rd[i].record_type == GNUNET_GNS_RECORD_PKEY);
515 GNUNET_CRYPTO_hash(rd[i].data, GNUNET_CRYPTO_RSA_KEY_LENGTH,
526 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
527 "Authority lookup successful but no PKEY... never get here?\n");
528 reply_to_dns(rh, 0, NULL);
533 * Reply to client with the result from our lookup.
535 * @param rh the request handle of the lookup
536 * @param rd_count the number of records to return
537 * @param rd the record data
540 reply_to_dns(struct GNUNET_GNS_ResolverHandle *rh, uint32_t rd_count,
541 const struct GNUNET_NAMESTORE_RecordData *rd)
547 struct GNUNET_DNSPARSER_Packet *packet = rh->packet;
548 struct GNUNET_DNSPARSER_Record answer_records[rh->answered];
549 struct GNUNET_DNSPARSER_Record additional_records[rd_count-(rh->answered)];
550 packet->answers = answer_records;
551 packet->additional_records = additional_records;
553 len = sizeof(struct GNUNET_DNSPARSER_Record*);
554 for (i=0; i < rd_count; i++)
556 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
557 "Adding type %d to DNS response\n", rd[i].record_type);
558 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Name: %s\n", rh->name);
559 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "QName: %s\n", rh->query->name);
560 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Record %d/%d\n", i+1, rd_count);
561 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Record len %d\n", rd[i].data_size);
562 if (rd[i].record_type == rh->query->type)
564 answer_records[i].name = rh->query->name;
565 answer_records[i].type = rd[i].record_type;
566 answer_records[i].data.raw.data_len = rd[i].data_size;
567 answer_records[i].data.raw.data = (char*)rd[i].data;
568 answer_records[i].expiration_time = rd[i].expiration;
569 answer_records[i].class = GNUNET_DNSPARSER_CLASS_INTERNET;//hmmn
573 additional_records[i].name = rh->query->name;
574 additional_records[i].type = rd[i].record_type;
575 additional_records[i].data.raw.data_len = rd[i].data_size;
576 additional_records[i].data.raw.data = (char*)rd[i].data;
577 additional_records[i].expiration_time = rd[i].expiration;
578 additional_records[i].class = GNUNET_DNSPARSER_CLASS_INTERNET;//hmmn
580 //GNUNET_free(i->record); DO this later!
583 packet->num_answers = rh->answered; //answer->num_records;
584 packet->num_additional_records = rd_count-(rh->answered);
586 if (0 == GNUNET_CRYPTO_hash_cmp(&rh->authority, &zone_hash))
587 packet->flags.authoritative_answer = 1;
589 packet->flags.authoritative_answer = 0;
592 packet->flags.return_code = GNUNET_DNSPARSER_RETURN_CODE_NAME_ERROR;
594 packet->flags.return_code = GNUNET_DNSPARSER_RETURN_CODE_NO_ERROR;
596 packet->flags.query_or_response = 1;
598 //FIXME this is silently discarded
599 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
600 "Building DNS response\n");
601 ret = GNUNET_DNSPARSER_pack (packet,
602 1024, /* FIXME magic from dns redirector */
605 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
606 "Built DNS response! (ret=%d,len=%d)\n", ret, len);
607 if (ret == GNUNET_OK)
609 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
610 "Answering DNS request\n");
611 GNUNET_DNS_request_answer(rh->request_handle,
614 //GNUNET_free(answer);
615 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Answered DNS request\n");
616 //FIXME return code, free datastructures
620 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
621 "Error building DNS response! (ret=%d)", ret);
624 //FIXME into free_resolver(rh)
625 GNUNET_free(rh->name);
631 * Namestore calls this function if we have an entry for this name.
632 * (or data=null to indicate the lookup has finished
634 * @param cls the pending query
635 * @param key the key of the zone we did the lookup
636 * @param expiration expiration date of the namestore entry
637 * @param name the name for which we need an authority
638 * @param rd_count the number of records with 'name'
639 * @param rd the record data
640 * @param signature the signature of the authority for the record data
643 process_authoritative_result(void* cls,
644 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
645 struct GNUNET_TIME_Absolute expiration,
646 const char *name, unsigned int rd_count,
647 const struct GNUNET_NAMESTORE_RecordData *rd,
648 const struct GNUNET_CRYPTO_RsaSignature *signature)
650 struct GNUNET_GNS_ResolverHandle *rh;
651 struct GNUNET_TIME_Relative remaining_time;
652 GNUNET_HashCode zone;
654 rh = (struct GNUNET_GNS_ResolverHandle *) cls;
655 GNUNET_CRYPTO_hash(key, GNUNET_CRYPTO_RSA_KEY_LENGTH, &zone);
656 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
658 //FIXME Handle results in rd
663 * Lookup terminated and no results
664 * -> DHT Phase unless data is recent
666 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
667 "Namestore lookup for %s terminated without results\n", name);
670 * if this is not our zone we cannot rely on the namestore to be
671 * complete. -> Query DHT
673 if (!GNUNET_CRYPTO_hash_cmp(&zone, &zone_hash))
675 if (remaining_time.rel_value == 0)
677 resolve_name_dht(rh, name);
682 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
683 "Record is still recent. No DHT lookup\n");
688 * Our zone and no result? Cannot resolve TT
690 GNUNET_assert(rh->answered == 0);
691 reply_to_dns(rh, 0, NULL); //answered should be 0
698 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
699 "Processing additional result %s from namestore\n", name);
701 for (i=0; i<rd_count;i++)
704 if (strcmp(name, rh->query->name) && rd[i].record_type != rh->query->type)
707 if ((GNUNET_TIME_absolute_get_remaining (rd[i].expiration)).rel_value
710 //FIXME there is a catch here...
711 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "This record is expired. Skipping\n");
721 * consult dht if expired
723 if ((remaining_time.rel_value == 0) && (rh->answered == 0))
725 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
726 "This dht entry is old. Refreshing.\n");
727 resolve_name_dht(rh, name);
731 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Found %d answer(s) to query!\n",
734 reply_to_dns(rh, rd_count, rd);
739 * Determine if this name is canonical.
741 * a.b.gnunet = not canonical
744 * @param name the name to test
745 * @return 1 if canonical
748 is_canonical(char* name)
750 uint32_t len = strlen(name);
753 for (i=0; i<len; i++)
755 if (*(name+i) == '.')
762 * Move one level up in the domain hierarchy and return the
763 * passed top level domain.
765 * @param name the domain
768 char* pop_tld(char* name)
772 if (is_canonical(name))
775 for (len = strlen(name); len > 0; len--)
777 if (*(name+len) == '.')
784 name[len] = '\0'; //terminate string
791 * The first phase of resolution.
792 * First check if the name is canonical.
793 * If it is then try to resolve directly.
794 * If not then first have to resolve the authoritative entities.
796 * @param rh the pending lookup
797 * @param zone the zone we are currently resolving in
800 resolve_name(struct GNUNET_GNS_ResolverHandle *rh)
802 if (is_canonical(rh->name))
804 //We only need to check this zone's ns
805 GNUNET_NAMESTORE_lookup_record(namestore_handle,
809 &process_authoritative_result,
814 //We have to resolve the authoritative entity
815 char *new_authority = pop_tld(rh->name);
816 GNUNET_NAMESTORE_lookup_record(namestore_handle,
819 GNUNET_GNS_RECORD_PKEY,
820 &process_authority_lookup,
826 * Entry point for name resolution
827 * Lookup local namestore of our zone.
829 * Setup a new query and try to resolve
831 * @param request the request handle of the DNS request from a client
832 * @param p the DNS query packet we received
833 * @param q the DNS query we received parsed from p
836 start_resolution(struct GNUNET_DNS_RequestHandle *request,
837 struct GNUNET_DNSPARSER_Packet *p,
838 struct GNUNET_DNSPARSER_Query *q)
840 struct GNUNET_GNS_ResolverHandle *rh;
842 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Starting resolution for (%s)!\n",
845 rh = GNUNET_malloc(sizeof (struct GNUNET_GNS_ResolverHandle));
848 rh->authority = zone_hash;
850 //FIXME do not forget to free!!
851 rh->name = GNUNET_malloc(strlen(q->name)
852 - strlen(gnunet_tld) + 1);
854 strlen(q->name)-strlen(gnunet_tld) + 1);
855 memcpy(rh->name, q->name,
856 strlen(q->name)-strlen(gnunet_tld));
858 rh->request_handle = request;
860 //Start resolution in our zone
865 * The DNS request handler
866 * Called for every incoming DNS request.
869 * @param rh request handle to user for reply
870 * @param request_length number of bytes in request
871 * @param request udp payload of the DNS request
874 handle_dns_request(void *cls,
875 struct GNUNET_DNS_RequestHandle *rh,
876 size_t request_length,
879 struct GNUNET_DNSPARSER_Packet *p;
882 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Hijacked a DNS request...processing\n");
883 p = GNUNET_DNSPARSER_parse (request, request_length);
887 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
888 "Received malformed DNS packet, leaving it untouched\n");
889 GNUNET_DNS_request_forward (rh);
894 * Check tld and decide if we or
895 * legacy dns is responsible
897 * FIXME now in theory there could be more than 1 query in the request
898 * but if this is case we get into trouble:
899 * either we query the GNS or the DNS. We cannot do both!
900 * So I suggest to either only allow a single query per request or
901 * only allow GNS or DNS requests.
902 * The way it is implemented here now is buggy and will lead to erratic
903 * behaviour (if multiple queries are present).
905 if (p->num_queries == 0)
907 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
908 "No Queries in DNS packet... forwarding\n");
909 GNUNET_DNS_request_forward (rh);
912 if (p->num_queries > 1)
914 //Note: We could also look for .gnunet
915 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
916 ">1 queriy in DNS packet... odd. We only process #1\n");
920 tldoffset = p->queries[0].name + strlen(p->queries[0].name);
922 while ((*tldoffset) != '.')
925 if (0 == strcmp(tldoffset, gnunet_tld))
927 start_resolution(rh, p, p->queries);
932 * This request does not concern us. Forward to real DNS.
934 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
935 "Request for %s is forwarded to DNS\n", p->queries[0].name);
936 GNUNET_DNS_request_forward (rh);
942 * test function that stores some data in the namestore
945 put_some_records(void)
947 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Populating namestore\n");
948 /* put a few records into namestore */
949 char* ipA = "1.2.3.4";
950 char* ipB = "5.6.7.8";
951 struct GNUNET_CRYPTO_RsaPrivateKey *bob_key = GNUNET_CRYPTO_rsa_key_create (); struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *bob;
952 bob = GNUNET_malloc(sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
954 GNUNET_CRYPTO_rsa_key_get_public (bob_key, bob);
956 GNUNET_HashCode *bob_zone = GNUNET_malloc(sizeof(GNUNET_HashCode));
958 GNUNET_CRYPTO_hash(bob, GNUNET_CRYPTO_RSA_KEY_LENGTH, bob_zone);
960 struct in_addr *alice = GNUNET_malloc(sizeof(struct in_addr));
961 struct in_addr *bob_web = GNUNET_malloc(sizeof(struct in_addr));
962 struct GNUNET_NAMESTORE_RecordData rda;
963 struct GNUNET_NAMESTORE_RecordData rdb;
964 struct GNUNET_NAMESTORE_RecordData rdb_web;
966 GNUNET_assert(1 == inet_pton (AF_INET, ipA, alice));
967 GNUNET_assert(1 == inet_pton (AF_INET, ipB, bob_web));
969 rda.data_size = sizeof(struct in_addr);
970 rdb_web.data_size = sizeof(struct in_addr);
971 rdb.data_size = sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded);
974 rdb_web.data = bob_web;
975 rda.record_type = GNUNET_GNS_RECORD_TYPE_A;
976 rdb_web.record_type = GNUNET_GNS_RECORD_TYPE_A;
977 rdb.record_type = GNUNET_GNS_RECORD_PKEY;
978 rdb_web.expiration = GNUNET_TIME_absolute_get_forever ();
979 rda.expiration = GNUNET_TIME_absolute_get_forever ();
980 rdb.expiration = GNUNET_TIME_absolute_get_forever ();
982 //alice.gnunet A IN 1.2.3.4
983 GNUNET_NAMESTORE_record_create (namestore_handle,
990 //www.bob.gnunet A IN 5.6.7.8
991 GNUNET_NAMESTORE_record_create (namestore_handle,
997 GNUNET_NAMESTORE_record_put(namestore_handle,
1000 GNUNET_TIME_absolute_get_forever (),
1009 update_zone_dht_next(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1011 GNUNET_NAMESTORE_zone_iterator_next(namestore_iter);
1016 update_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1019 * Function used to put all records successively into the DHT.
1022 * @param cls the closure (NULL)
1023 * @param key the public key of the authority (ours)
1024 * @param expiration lifetime of the namestore entry
1025 * @param name the name of the records
1026 * @param rd_count the number of records in data
1027 * @param rd the record data
1028 * @param signature the signature for the record data
1031 put_gns_record(void *cls,
1032 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
1033 struct GNUNET_TIME_Absolute expiration,
1035 unsigned int rd_count,
1036 const struct GNUNET_NAMESTORE_RecordData *rd,
1037 const struct GNUNET_CRYPTO_RsaSignature *signature)
1039 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Putting records into the DHT\n");
1040 struct GNUNET_TIME_Relative timeout;
1041 struct GNSNameRecordBlock *nrb;
1042 struct GNSRecordBlock *rb;
1043 GNUNET_HashCode name_hash;
1044 GNUNET_HashCode xor_hash;
1046 uint32_t rd_payload_length;
1048 if (NULL == name) //We're done
1050 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Zone iteration finished\n");
1051 GNUNET_NAMESTORE_zone_iteration_stop (namestore_iter);
1052 zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_start,
1057 rd_payload_length = rd_count * sizeof(struct GNSRecordBlock);
1058 rd_payload_length += strlen(name) + sizeof(struct GNSNameRecordBlock);
1059 //Calculate payload size
1060 for (i=0; i<rd_count; i++)
1062 rd_payload_length += rd[i].data_size;
1065 nrb = GNUNET_malloc(rd_payload_length);
1067 memcpy(&nrb->signature, signature,
1068 sizeof(struct GNUNET_CRYPTO_RsaSignature));
1069 //FIXME signature purpose
1070 memcpy(&nrb->public_key, key,
1071 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
1073 nrb->rd_count = htonl(rd_count);
1075 memcpy(&nrb[1], name, strlen(name)); //FIXME is this 0 terminated??
1077 rb = (struct GNSRecordBlock *)(&nrb[1]+strlen(name));
1079 for (i=0; i<rd_count; i++)
1081 rb->type = htonl(rd[i].record_type);
1082 rb->expiration = GNUNET_TIME_absolute_hton(rd[i].expiration);
1083 rb->data_length = htonl(rd[i].data_size);
1084 rb->flags = htonl(rd[i].flags);
1085 memcpy(&rb[1], rd[i].data, rd[i].data_size);
1086 rb = &rb[1] + rd[i].data_size;
1090 * FIXME magic number 20 move to config file
1092 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 20);
1093 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
1094 GNUNET_CRYPTO_hash_xor(&zone_hash, &name_hash, &xor_hash);
1095 GNUNET_DHT_put (dht_handle, &xor_hash,
1096 5, //replication level
1098 GNUNET_BLOCK_TYPE_TEST, //FIXME todo block plugin
1103 NULL, //FIXME continuation needed? success check? yes ofc
1104 NULL); //cls for cont
1106 num_public_records++;
1109 * Reschedule periodic put
1111 zone_update_taskid = GNUNET_SCHEDULER_add_delayed (dht_update_interval,
1112 &update_zone_dht_next,
1118 * Periodically iterate over our zone and store everything in dht
1121 * @param tc task context
1124 update_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1126 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Update zone!\n");
1127 if (0 == num_public_records)
1129 dht_update_interval = GNUNET_TIME_relative_multiply(
1130 GNUNET_TIME_UNIT_SECONDS,
1135 dht_update_interval = GNUNET_TIME_relative_multiply(
1136 GNUNET_TIME_UNIT_SECONDS,
1137 (3600/num_public_records));
1139 num_public_records = 0; //start counting again
1140 namestore_iter = GNUNET_NAMESTORE_zone_iteration_start (namestore_handle,
1142 GNUNET_NAMESTORE_RF_AUTHORITY,
1143 GNUNET_NAMESTORE_RF_PRIVATE,
1149 * Process GNS requests.
1151 * @param cls closure
1152 * @param server the initialized server
1153 * @param c configuration to use
1156 run (void *cls, struct GNUNET_SERVER_Handle *server,
1157 const struct GNUNET_CONFIGURATION_Handle *c)
1160 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Init GNS\n");
1161 zone_key = GNUNET_CRYPTO_rsa_key_create ();
1163 GNUNET_CRYPTO_hash(zone_key, GNUNET_CRYPTO_RSA_KEY_LENGTH,//FIXME is this ok?
1165 nc = GNUNET_SERVER_notification_context_create (server, 1);
1167 /* FIXME - do some config parsing
1168 * - Maybe only hijack dns if option is set (HIJACK_DNS=1)
1171 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
1174 * Do gnunet dns init here
1176 dns_handle = GNUNET_DNS_connect(c,
1177 GNUNET_DNS_FLAG_PRE_RESOLUTION,
1178 &handle_dns_request, /* rh */
1179 NULL); /* Closure */
1181 if (NULL == dns_handle)
1183 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1184 "Failed to connect to the dnsservice!\n");
1188 * handle to our local namestore
1190 namestore_handle = GNUNET_NAMESTORE_connect(c);
1192 if (NULL == namestore_handle)
1194 //FIXME do error handling;
1195 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1196 "Failed to connect to the namestore!\n");
1202 dht_handle = GNUNET_DHT_connect(c, 1); //FIXME get ht_len from cfg
1204 if (NULL == dht_handle)
1206 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not connect to DHT!\n");
1209 put_some_records(); //FIXME for testing
1212 * Schedule periodic put
1214 * We have roughly an hour for all records;
1216 dht_update_interval = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS,
1217 1); //FIXME from cfg
1218 zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_start, NULL);
1219 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "GNS Init done!\n");
1225 * The main function for the GNS service.
1227 * @param argc number of arguments from the command line
1228 * @param argv command line arguments
1229 * @return 0 ok, 1 on error
1232 main (int argc, char *const *argv)
1238 GNUNET_SERVICE_run (argc, argv, "gns", GNUNET_SERVICE_OPTION_NONE, &run,
1243 /* end of gnunet-service-gns.c */