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 * - Think about mixed dns queries (.gnunet and .org)
26 * - The smaller FIXME issues all around
28 * @file gns/gnunet-service-gns.c
29 * @brief GNUnet GNS service
30 * @author Martin Schanzenbach
33 #include "gnunet_util_lib.h"
34 #include "gnunet_transport_service.h"
35 #include "gnunet_dns_service.h"
36 #include "gnunet_dnsparser_lib.h"
37 #include "gnunet_dht_service.h"
38 #include "gnunet_namestore_service.h"
39 #include "gnunet_gns_service.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
46 struct GNUNET_GNS_QueryRecordList
51 struct GNUNET_GNS_QueryRecordList * next;
52 struct GNUNET_GNS_QueryRecordList * prev;
54 struct GNUNET_NAMESTORE_RecordData * record;
58 * A result list for namestore queries
60 struct GNUNET_GNS_PendingQuery
62 /* the answer packet */
63 struct GNUNET_DNSPARSER_Packet *answer;
65 /* records to put into answer packet */
66 struct GNUNET_GNS_QueryRecordList * records_head;
67 struct GNUNET_GNS_QueryRecordList * records_tail;
70 int num_authority_records; //FIXME are all of our replies auth?
76 /* the dns request id */
77 int id; // FIXME can handle->request_id also be used here?
79 /* the request handle to reply to */
80 struct GNUNET_DNS_RequestHandle *request_handle;
82 /* hast this query been answered? */
85 /* the authoritative zone to query */
86 GNUNET_HashCode *authority;
88 /* we have an authority in namestore that
89 * may be able to resolve
96 * Our handle to the DNS handler library
98 struct GNUNET_DNS_Handle *dns_handle;
101 * Our handle to the DHT
103 struct GNUNET_DHT_Handle *dht_handle;
106 * Our zone's private key
108 struct GNUNET_CRYPTO_RsaPrivateKey *zone_key;
111 * Our handle to the namestore service
113 struct GNUNET_NAMESTORE_Handle *namestore_handle;
115 struct GNUNET_NAMESTORE_ZoneIterator *namestore_iter;
118 * The configuration the GNS service is running with
120 const struct GNUNET_CONFIGURATION_Handle *GNS_cfg;
123 * Our notification context.
125 static struct GNUNET_SERVER_NotificationContext *nc;
130 GNUNET_HashCode zone_hash;
133 * Our tld. Maybe get from config file
135 const char* gnunet_tld = ".gnunet";
138 * Useful for zone update for DHT put
140 static int num_public_records = 3600;
141 struct GNUNET_TIME_Relative dht_update_interval;
144 void reply_to_dns(struct GNUNET_GNS_PendingQuery *answer);
145 void resolve_name(struct GNUNET_GNS_PendingQuery *query,
146 GNUNET_HashCode *zone);
149 * Task run during shutdown.
155 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
157 GNUNET_DNS_disconnect(dns_handle);
158 GNUNET_NAMESTORE_disconnect(namestore_handle, 0);
159 GNUNET_DHT_disconnect(dht_handle);
163 on_namestore_record_put_result(void *cls,
167 if (GNUNET_NO == success)
169 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "records already in namestore\n");
172 else if (GNUNET_YES == success)
174 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
175 "records successfully put in namestore\n");
179 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
180 "Error putting records into namestore: %s\n", emsg);
184 * Function called when we get a result from the dht
187 * @param cls the query handle
188 * @param exp lifetime
189 * @param key the key the record was stored under
190 * @param get_path get path
191 * @param get_path_length get path length
192 * @param put_path put path
193 * @param put_path_length put path length
194 * @param type the block type
195 * @param size the size of the record
196 * @param data the record data
199 process_authority_dht_result(void* cls,
200 struct GNUNET_TIME_Absolute exp,
201 const GNUNET_HashCode * key,
202 const struct GNUNET_PeerIdentity *get_path,
203 unsigned int get_path_length,
204 const struct GNUNET_PeerIdentity *put_path,
205 unsigned int put_path_length,
206 enum GNUNET_BLOCK_Type type,
207 size_t size, const void *data)
209 struct GNUNET_GNS_PendingQuery *query;
210 uint32_t num_records;
213 struct GNUNET_CRYPTO_RsaSignature *signature;
216 GNUNET_HashCode zone, name_hash;
221 query = (struct GNUNET_GNS_PendingQuery *)cls;
224 num_records = ntohl(*pos);
225 struct GNUNET_NAMESTORE_RecordData rd[num_records];
227 pos += sizeof(uint32_t);
229 for (i=0; i<num_records; i++)
231 namelen = ntohs(*pos);
232 pos += sizeof(uint16_t);
234 //name must be 0 terminated
238 rd[i].record_type = ntohl(*pos);
239 pos += sizeof(uint32_t);
241 rd[i].data_size = ntohl(*pos);
242 pos += sizeof(uint32_t);
245 pos += rd[i].data_size;
247 rd[i].expiration = GNUNET_TIME_absolute_ntoh(
248 *((struct GNUNET_TIME_AbsoluteNBO*)pos));
249 pos += sizeof(struct GNUNET_TIME_AbsoluteNBO);
251 rd[i].flags = ntohs(*pos);
252 pos += sizeof(uint16_t);
255 if (strcmp(name, query->name) && rd[i].record_type == query->type)
262 if ((((char*)data)-pos) < sizeof(struct GNUNET_CRYPTO_RsaSignature))
264 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
265 "Cannot parse signature in DHT response. Corrupted or Missing");
269 signature = (struct GNUNET_CRYPTO_RsaSignature*)pos;
271 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
272 GNUNET_CRYPTO_hash_xor(key, &name_hash, &zone);
275 GNUNET_NAMESTORE_record_put (namestore_handle,
282 &on_namestore_record_put_result, //cont
288 memcpy(query->authority, &zone, sizeof(GNUNET_HashCode));
289 resolve_name(query, query->authority);
292 * data is a serialized PKEY record (probably)
293 * parse, put into namestore
294 * namestore zone hash is in query.
295 * Then adjust query->name and call resolve_name
296 * with new zone (the one just received)
298 * query->authority = new_authority
299 * resolve_name(query, new_authority);
304 * Start DHT lookup for a name -> PKEY (compare NS) record in
305 * query->authority's zone
307 * @param query the pending gns query
308 * @param name the name of the PKEY record
311 resolve_authority_dht(struct GNUNET_GNS_PendingQuery *query, const char* name)
313 enum GNUNET_GNS_RecordType rtype = GNUNET_GNS_RECORD_PKEY;
314 struct GNUNET_TIME_Relative timeout;
315 GNUNET_HashCode name_hash;
316 GNUNET_HashCode lookup_key;
318 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
319 GNUNET_CRYPTO_hash_xor(&name_hash, query->authority, &lookup_key);
321 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 20);
323 //FIXME how long to wait for results?
324 GNUNET_DHT_get_start(dht_handle, timeout,
325 GNUNET_BLOCK_TYPE_TEST, //FIXME todo
327 5, //Replication level FIXME
329 &rtype, //xquery FIXME this is bad
330 sizeof(GNUNET_GNS_RECORD_PKEY),
331 &process_authority_dht_result,
337 * Function called when we get a result from the dht
340 * @param cls the query handle
341 * @param exp lifetime
342 * @param key the key the record was stored under
343 * @param get_path get path
344 * @param get_path_length get path length
345 * @param put_path put path
346 * @param put_path_length put path length
347 * @param type the block type
348 * @param size the size of the record
349 * @param data the record data
352 process_name_dht_result(void* cls,
353 struct GNUNET_TIME_Absolute exp,
354 const GNUNET_HashCode * key,
355 const struct GNUNET_PeerIdentity *get_path,
356 unsigned int get_path_length,
357 const struct GNUNET_PeerIdentity *put_path,
358 unsigned int put_path_length,
359 enum GNUNET_BLOCK_Type type,
360 size_t size, const void *data)
362 struct GNUNET_GNS_PendingQuery *query;
363 uint32_t num_records;
366 struct GNUNET_CRYPTO_RsaSignature *signature;
369 GNUNET_HashCode zone, name_hash;
374 query = (struct GNUNET_GNS_PendingQuery *)cls;
377 num_records = ntohl(*pos);
378 struct GNUNET_NAMESTORE_RecordData rd[num_records];
380 pos += sizeof(uint32_t);
382 for (i=0; i<num_records; i++)
384 namelen = ntohs(*pos);
385 pos += sizeof(uint16_t);
387 //name must be 0 terminated
391 rd[i].record_type = ntohl(*pos);
392 pos += sizeof(uint32_t);
394 rd[i].data_size = ntohl(*pos);
395 pos += sizeof(uint32_t);
398 pos += rd[i].data_size;
400 rd[i].expiration = GNUNET_TIME_absolute_ntoh(
401 *((struct GNUNET_TIME_AbsoluteNBO*)pos));
402 pos += sizeof(struct GNUNET_TIME_AbsoluteNBO);
404 rd[i].flags = ntohs(*pos);
405 pos += sizeof(uint16_t);
408 if (strcmp(name, query->name) && rd[i].record_type == query->type)
415 if ((((char*)data)-pos) < sizeof(struct GNUNET_CRYPTO_RsaSignature))
417 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
418 "Cannot parse signature in DHT response. Corrupted or Missing");
422 signature = (struct GNUNET_CRYPTO_RsaSignature*)pos;
424 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
425 GNUNET_CRYPTO_hash_xor(key, &name_hash, &zone);
428 GNUNET_NAMESTORE_record_put (namestore_handle,
435 &on_namestore_record_put_result, //cont
440 //FIXME: add records to query handle, but on stack!
441 //do we need records in query handle? can't we just
442 //pass them to reply_to_dns?
447 * data is a serialized GNS record of type
448 * Check if record type and name match in query and reply
454 * Start DHT lookup for a (name -> query->record_type) record in
455 * query->authority's zone
457 * @param query the pending gns query
458 * @param name the name to query record
461 resolve_name_dht(struct GNUNET_GNS_PendingQuery *query, const char* name)
463 struct GNUNET_TIME_Relative timeout;
464 GNUNET_HashCode name_hash;
465 GNUNET_HashCode lookup_key;
467 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
468 GNUNET_CRYPTO_hash_xor(&name_hash, query->authority, &lookup_key);
470 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 20);
472 //FIXME how long to wait for results?
473 GNUNET_DHT_get_start(dht_handle, timeout,
474 GNUNET_BLOCK_TYPE_TEST, //FIXME todo
476 5, //Replication level FIXME
478 &query->type, //xquery
480 &process_name_dht_result,
487 resolve_name(struct GNUNET_GNS_PendingQuery *query, GNUNET_HashCode *zone);
490 * This is a callback function that should give us only PKEY
491 * records. Used to query the namestore for the authority (PKEY)
494 * @param cls the pending query
495 * @param zone our zone hash
496 * @param name the name for which we need an authority
497 * @param record_type the type of record (PKEY)
498 * @param expiration expiration date of the record
499 * @param flags namestore record flags
500 * @param sig_loc the location of the record in the signature tree
501 * @param size the size of the record
502 * @param data the record data
505 process_authority_lookup(void* cls,
506 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
507 struct GNUNET_TIME_Absolute expiration,
509 unsigned int rd_count,
510 const struct GNUNET_NAMESTORE_RecordData *rd,
511 const struct GNUNET_CRYPTO_RsaSignature *signature)
513 struct GNUNET_GNS_PendingQuery *query;
514 GNUNET_HashCode zone;
516 query = (struct GNUNET_GNS_PendingQuery *)cls;
517 GNUNET_CRYPTO_hash(key, GNUNET_CRYPTO_RSA_KEY_LENGTH, &zone);
520 * No authority found in namestore.
525 * We did not find an authority in the namestore
526 * _IF_ the current authoritative zone is us we cannot resolve
527 * _ELSE_ we can still check the dht
529 if (GNUNET_CRYPTO_hash_cmp(&zone, &zone_hash))
531 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Authority unknown\n");
532 //FIXME return NX answer
539 resolve_authority_dht(query, name);
543 //Note only 1 pkey should have been returned.. anything else would be strange
545 * We found an authority that may be able to help us
549 GNUNET_assert(rd->record_type == GNUNET_GNS_RECORD_PKEY);
550 GNUNET_HashCode *pkey_hash = GNUNET_malloc(sizeof(GNUNET_HashCode));
551 GNUNET_CRYPTO_hash(rd->data, GNUNET_CRYPTO_RSA_KEY_LENGTH, pkey_hash);
552 GNUNET_free_non_null(query->authority);
553 query->authority = pkey_hash;
554 resolve_name(query, query->authority);
560 * Reply to client with the result from our lookup.
562 * @param answer the pending query used in the lookup
565 reply_to_dns(struct GNUNET_GNS_PendingQuery *answer)
567 struct GNUNET_GNS_QueryRecordList *i;
568 struct GNUNET_DNSPARSER_Flags dnsflags;
573 struct GNUNET_DNSPARSER_Packet packet;
574 struct GNUNET_DNSPARSER_Record answer_records[answer->num_records];
575 packet.answers = answer_records;
577 len = sizeof(struct GNUNET_DNSPARSER_Record*);
579 for (i=answer->records_head; i != NULL; i=i->next)
581 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
582 "Adding type %d to DNS response\n", i->record->record_type);
583 answer_records[j].name = answer->original_name; //FIXME yes?
584 answer_records[j].type = i->record->record_type;
585 answer_records[j].data.raw.data_len = i->record->data_size;
586 answer_records[j].data.raw.data = (char*)i->record->data;
587 answer_records[j].expiration_time = i->record->expiration;
588 answer_records[j].class = GNUNET_DNSPARSER_CLASS_INTERNET;//hmmn
589 //GNUNET_free(i->record); DO this later!
592 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "after memcpy\n");
593 /* FIXME how to handle auth, additional etc */
594 packet.num_answers = answer->num_records;
595 packet.num_authority_records = answer->num_authority_records;
597 dnsflags.authoritative_answer = 1;
598 dnsflags.opcode = GNUNET_DNSPARSER_OPCODE_QUERY;
599 dnsflags.return_code = GNUNET_DNSPARSER_RETURN_CODE_NO_ERROR; //not sure
600 dnsflags.query_or_response = 1;
601 packet.flags = dnsflags;
603 packet.id = answer->id;
605 //FIXME this is silently discarded
606 ret = GNUNET_DNSPARSER_pack (&packet,
607 1024, /* FIXME magic from dns redirector */
610 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
611 "Built DNS response! (ret=%d)\n", ret);
612 if (ret == GNUNET_OK)
614 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
615 "Answering DNS request\n");
616 GNUNET_DNS_request_answer(answer->request_handle,
619 //GNUNET_free(answer);
620 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Answered DNS request\n");
621 //FIXME return code, free datastructures
625 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
626 "Error building DNS response! (ret=%d)", ret);
632 * Namestore calls this function if we have an entry for this name.
633 * (or data=null to indicate the lookup has finished)
635 * @param cls the pending query
636 * @param zone the zone of the lookup
637 * @param name the name looked up
638 * @param record_type the record type
639 * @param expiration lifetime of the record
640 * @param flags record flags
641 * @param sig_loc location of the record in the signature tree
642 * @param size the size of the record
643 * @param data the record data
646 process_authoritative_result(void* cls,
647 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
648 struct GNUNET_TIME_Absolute expiration,
649 const char *name, unsigned int rd_count,
650 const struct GNUNET_NAMESTORE_RecordData *rd,
651 const struct GNUNET_CRYPTO_RsaSignature *signature)
653 struct GNUNET_GNS_PendingQuery *query;
654 struct GNUNET_GNS_QueryRecordList *qrecord;
655 struct GNUNET_NAMESTORE_RecordData *record;
656 struct GNUNET_TIME_Relative remaining_time;
657 GNUNET_HashCode zone;
659 query = (struct GNUNET_GNS_PendingQuery *) cls;
660 GNUNET_CRYPTO_hash(key, GNUNET_CRYPTO_RSA_KEY_LENGTH, &zone);
661 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
663 //FIXME Handle results in rd
669 * Lookup terminated and no results
670 * -> DHT Phase unless data is recent
672 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
673 "Namestore lookup terminated. without results\n");
676 * if this is not our zone we cannot rely on the namestore to be
677 * complete. -> Query DHT
679 if (!GNUNET_CRYPTO_hash_cmp(&zone, &zone_hash))
681 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
682 if (remaining_time.rel_value == 0)
684 resolve_name_dht(query, name);
689 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Record is still recent. No DHT lookup\n");
694 * Our zone and no result? Cannot resolve TT
695 * FIXME modify query to say NX
697 GNUNET_assert(query->answered == 0);
698 reply_to_dns(query); //answered should be 0
707 * FIXME Check record expiration and dht expiration
708 * consult dht if necessary
710 if (remaining_time.rel_value == 0)
712 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
713 "This dht entry is old. Refreshing.\n");
714 resolve_name_dht(query, name);
717 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
718 "Processing additional result %s from namestore\n", name);
720 for (i=0; i<rd_count;i++)
722 if ((GNUNET_TIME_absolute_get_remaining (rd[i].expiration)).rel_value
725 //FIXME there is a catch here...
726 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "This record is expired. Skipping\n");
729 // A time will come when this has to be freed
730 qrecord = GNUNET_malloc(sizeof(struct GNUNET_GNS_QueryRecordList));
731 record = GNUNET_malloc(sizeof(struct GNUNET_NAMESTORE_RecordData));
732 qrecord->record = record;
734 //fixme into gns_util
735 //parse_record(rd[i]->data, rd[i]->data_size, 0, record);
736 GNUNET_CONTAINER_DLL_insert(query->records_head,
739 query->num_records++;
742 //we need to resolve to the original name in the end though...
743 //keep in mind. This can also be done later probably
744 //record->name = (char*)query->original_name;
747 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Found answer to query!\n");
755 * Determine if this name is canonical.
757 * a.b.gnunet = not canonical
760 * @param name the name to test
761 * @return 1 if canonical
764 is_canonical(char* name)
766 uint32_t len = strlen(name);
769 for (i=0; i<len; i++)
771 if (*(name+i) == '.')
778 * Move one level up in the domain hierarchy and return the
779 * passed top level domain.
780 * FIXME this needs a better name
782 * @param name the domain
785 char* pop_tld(char* name)
789 if (is_canonical(name))
792 for (len = strlen(name); len > 0; len--)
794 if (*(name+len) == '.')
801 name[len] = '\0'; //terminate string
808 * The first phase of resolution.
809 * First check if the name is canonical.
810 * If it is then try to resolve directly.
811 * If not then first have to resolve the authoritative entities.
813 * @param query the pending lookup
814 * @param zone the zone we are currently resolving in
817 resolve_name(struct GNUNET_GNS_PendingQuery *query, GNUNET_HashCode *zone)
819 if (is_canonical(query->name))
821 //We only need to check this zone's ns
822 GNUNET_NAMESTORE_lookup_record(namestore_handle,
826 &process_authoritative_result,
831 //We have to resolve the authoritative entity
832 char *new_authority = pop_tld(query->name);
833 GNUNET_NAMESTORE_lookup_record(namestore_handle,
836 GNUNET_GNS_RECORD_PKEY,
837 &process_authority_lookup,
843 * Entry point for name resolution
844 * Lookup local namestore of our zone.
846 * Setup a new query and try to resolve
848 * @param rh the request handle of the DNS request from a client
849 * @param name the name to look up
850 * @param id the id of the dns request (for the reply)
851 * @param type the record type to look for
854 start_resolution(struct GNUNET_DNS_RequestHandle *rh,
855 char* name, uint16_t id, uint16_t type)
857 struct GNUNET_GNS_PendingQuery *query;
859 //FIXME remove .gnunet here from name
860 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "This is .gnunet (%s)!\n", name);
861 query = GNUNET_malloc(sizeof (struct GNUNET_GNS_PendingQuery));
863 query->original_name = name; //Full name of original query
865 //FIXME do not forget to free!!
866 query->name = GNUNET_malloc(strlen(name)-strlen(gnunet_tld) + 1);
867 memset(query->name, 0, strlen(name)-strlen(gnunet_tld) + 1);
868 memcpy(query->name, name, strlen(name)-strlen(gnunet_tld));
871 query->request_handle = rh;
873 //Start resolution in our zone
874 resolve_name(query, &zone_hash);
878 * The DNS request handler
879 * Called for every incoming DNS request.
882 * @param rh request handle to user for reply
883 * @param request_length number of bytes in request
884 * @param request udp payload of the DNS request
887 handle_dns_request(void *cls,
888 struct GNUNET_DNS_RequestHandle *rh,
889 size_t request_length,
892 struct GNUNET_DNSPARSER_Packet *p;
896 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Hijacked a DNS request...processing\n");
897 p = GNUNET_DNSPARSER_parse (request, request_length);
901 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
902 "Received malformed DNS packet, leaving it untouched\n");
903 GNUNET_DNS_request_forward (rh);
908 * Check tld and decide if we or
909 * legacy dns is responsible
911 * FIXME now in theory there could be more than 1 query in the request
912 * but if this is case we get into trouble:
913 * either we query the GNS or the DNS. We cannot do both!
914 * So I suggest to either only allow a single query per request or
915 * only allow GNS or DNS requests.
916 * The way it is implemented here now is buggy and will lead to erratic
917 * behaviour (if multiple queries are present).
919 for (i=0;i<p->num_queries;i++)
921 tldoffset = p->queries[i].name + strlen(p->queries[i].name);
923 while ((*tldoffset) != '.')
926 if (0 == strcmp(tldoffset, gnunet_tld))
928 start_resolution(rh, p->queries[i].name, p->id, p->queries[i].type);
933 * This request does not concern us. Forward to real DNS.
935 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
936 "Request for %s is forwarded to DNS\n", p->queries[i].name);
937 GNUNET_DNS_request_forward (rh);
943 * test function that stores some data in the namestore
946 put_some_records(void)
948 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Populating namestore\n");
949 /* put a few records into namestore */
950 char* ipA = "1.2.3.4";
951 char* ipB = "5.6.7.8";
952 struct in_addr *alice = GNUNET_malloc(sizeof(struct in_addr));
953 struct in_addr *bob = GNUNET_malloc(sizeof(struct in_addr));
954 struct GNUNET_NAMESTORE_RecordData *rda;
955 struct GNUNET_NAMESTORE_RecordData *rdb;
957 rda = GNUNET_malloc(sizeof(struct GNUNET_NAMESTORE_RecordData));
958 rdb = GNUNET_malloc(sizeof(struct GNUNET_NAMESTORE_RecordData));
960 GNUNET_assert(1 == inet_pton (AF_INET, ipA, alice));
961 GNUNET_assert(1 == inet_pton (AF_INET, ipB, bob));
963 rda->data_size = sizeof(struct in_addr);
964 rdb->data_size = sizeof(struct in_addr);
967 rda->record_type = GNUNET_GNS_RECORD_TYPE_A;
968 rdb->record_type = GNUNET_GNS_RECORD_TYPE_A;
969 rda->expiration = GNUNET_TIME_absolute_get_forever ();
970 rdb->expiration = GNUNET_TIME_absolute_get_forever ();
972 GNUNET_NAMESTORE_record_create (namestore_handle,
978 GNUNET_NAMESTORE_record_create (namestore_handle,
987 update_zone_dht_next(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
989 GNUNET_NAMESTORE_zone_iterator_next(namestore_iter);
993 * Function used to put all records successively into the DHT.
995 * @param cls the closure (NULL)
996 * @param zone our root zone hash
997 * @param name the name of the record
998 * @param record_type the type of the record
999 * @param expiration lifetime of the record
1000 * @param flags flags of the record
1001 * @param sig_loc location of record in signature tree
1002 * @param size size of the record
1003 * @param record_data the record data
1006 put_gns_record(void *cls,
1007 const const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
1008 struct GNUNET_TIME_Absolute expiration,
1010 unsigned int rd_count,
1011 const struct GNUNET_NAMESTORE_RecordData *rd,
1012 const struct GNUNET_CRYPTO_RsaSignature *signature)
1014 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Putting records into the DHT\n");
1015 struct GNUNET_TIME_Relative timeout;
1016 GNUNET_HashCode name_hash;
1017 GNUNET_HashCode xor_hash;
1019 if (NULL == name) //We're done
1021 GNUNET_NAMESTORE_zone_iteration_stop (namestore_iter);
1025 * FIXME magic number 20 move to config file
1027 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 20);
1028 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
1029 GNUNET_CRYPTO_hash_xor(&zone_hash, &name_hash, &xor_hash);
1030 GNUNET_DHT_put (dht_handle, &xor_hash,
1031 5, //replication level
1033 GNUNET_BLOCK_TYPE_TEST, //FIXME todo block plugin
1038 NULL, //FIXME continuation needed? success check? yes ofc
1039 NULL); //cls for cont
1041 num_public_records++;
1044 * Reschedule periodic put
1046 GNUNET_SCHEDULER_add_delayed (dht_update_interval,
1047 &update_zone_dht_next,
1053 * Periodically iterate over our zone and store everything in dht
1056 * @param tc task context
1059 update_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1061 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Update zone!\n");
1062 dht_update_interval = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS,
1063 (3600/num_public_records));
1064 num_public_records = 0; //start counting again
1065 namestore_iter = GNUNET_NAMESTORE_zone_iteration_start (namestore_handle,
1067 GNUNET_NAMESTORE_RF_AUTHORITY,
1068 GNUNET_NAMESTORE_RF_PRIVATE,
1074 * Process GNS requests.
1076 * @param cls closure
1077 * @param server the initialized server
1078 * @param c configuration to use
1081 run (void *cls, struct GNUNET_SERVER_Handle *server,
1082 const struct GNUNET_CONFIGURATION_Handle *c)
1085 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Init GNS\n");
1086 zone_key = GNUNET_CRYPTO_rsa_key_create ();
1088 GNUNET_CRYPTO_hash(zone_key, GNUNET_CRYPTO_RSA_KEY_LENGTH,//FIXME is this ok?
1090 nc = GNUNET_SERVER_notification_context_create (server, 1);
1092 /* FIXME - do some config parsing
1093 * - Maybe only hijack dns if option is set (HIJACK_DNS=1)
1096 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
1099 * Do gnunet dns init here
1101 dns_handle = GNUNET_DNS_connect(c,
1102 GNUNET_DNS_FLAG_PRE_RESOLUTION,
1103 &handle_dns_request, /* rh */
1104 NULL); /* Closure */
1106 if (NULL == dns_handle)
1108 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1109 "Failed to connect to the dnsservice!\n");
1113 * handle to our local namestore
1115 namestore_handle = GNUNET_NAMESTORE_connect(c);
1117 if (NULL == namestore_handle)
1119 //FIXME do error handling;
1120 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1121 "Failed to connect to the namestore!\n");
1127 dht_handle = GNUNET_DHT_connect(c, 1); //FIXME get ht_len from cfg
1129 if (NULL == dht_handle)
1131 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not connect to DHT!\n");
1134 put_some_records(); //FIXME for testing
1137 * Schedule periodic put
1139 * We have roughly an hour for all records;
1141 dht_update_interval = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS,
1142 60); //FIXME from cfg
1143 GNUNET_SCHEDULER_add_delayed (dht_update_interval,
1144 &update_zone_dht_start,
1146 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "GNS Init done!\n");
1152 * The main function for the GNS service.
1154 * @param argc number of arguments from the command line
1155 * @param argv command line arguments
1156 * @return 0 ok, 1 on error
1159 main (int argc, char *const *argv)
1165 GNUNET_SERVICE_run (argc, argv, "gns", GNUNET_SERVICE_OPTION_NONE, &run,
1170 /* end of gnunet-service-gns.c */