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, uint32_t rd_count,
145 const struct GNUNET_NAMESTORE_RecordData *rd);
146 void resolve_name(struct GNUNET_GNS_PendingQuery *query,
147 GNUNET_HashCode *zone);
150 * Task run during shutdown.
156 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
158 GNUNET_DNS_disconnect(dns_handle);
159 GNUNET_NAMESTORE_disconnect(namestore_handle, 0);
160 GNUNET_DHT_disconnect(dht_handle);
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 query 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_PendingQuery *query;
211 uint32_t num_records;
214 struct GNUNET_CRYPTO_RsaSignature *signature;
217 GNUNET_HashCode zone, name_hash;
222 query = (struct GNUNET_GNS_PendingQuery *)cls;
225 num_records = ntohl(*pos);
226 struct GNUNET_NAMESTORE_RecordData rd[num_records];
228 pos += sizeof(uint32_t);
230 for (i=0; i<num_records; i++)
232 namelen = ntohs(*pos);
233 pos += sizeof(uint16_t);
235 //name must be 0 terminated
239 rd[i].record_type = ntohl(*pos);
240 pos += sizeof(uint32_t);
242 rd[i].data_size = ntohl(*pos);
243 pos += sizeof(uint32_t);
246 pos += rd[i].data_size;
248 rd[i].expiration = GNUNET_TIME_absolute_ntoh(
249 *((struct GNUNET_TIME_AbsoluteNBO*)pos));
250 pos += sizeof(struct GNUNET_TIME_AbsoluteNBO);
252 rd[i].flags = ntohs(*pos);
253 pos += sizeof(uint16_t);
256 if (strcmp(name, query->name) && rd[i].record_type == query->type)
263 if ((((char*)data)-pos) < sizeof(struct GNUNET_CRYPTO_RsaSignature))
265 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
266 "Cannot parse signature in DHT response. Corrupted or Missing");
270 signature = (struct GNUNET_CRYPTO_RsaSignature*)pos;
272 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
273 GNUNET_CRYPTO_hash_xor(key, &name_hash, &zone);
276 GNUNET_NAMESTORE_record_put (namestore_handle,
283 &on_namestore_record_put_result, //cont
289 memcpy(query->authority, &zone, sizeof(GNUNET_HashCode));
290 resolve_name(query, query->authority);
293 * data is a serialized PKEY record (probably)
294 * parse, put into namestore
295 * namestore zone hash is in query.
296 * Then adjust query->name and call resolve_name
297 * with new zone (the one just received)
299 * query->authority = new_authority
300 * resolve_name(query, new_authority);
305 * Start DHT lookup for a name -> PKEY (compare NS) record in
306 * query->authority's zone
308 * @param query the pending gns query
309 * @param name the name of the PKEY record
312 resolve_authority_dht(struct GNUNET_GNS_PendingQuery *query, const char* name)
314 enum GNUNET_GNS_RecordType rtype = GNUNET_GNS_RECORD_PKEY;
315 struct GNUNET_TIME_Relative timeout;
316 GNUNET_HashCode name_hash;
317 GNUNET_HashCode lookup_key;
319 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
320 GNUNET_CRYPTO_hash_xor(&name_hash, query->authority, &lookup_key);
322 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 20);
324 //FIXME how long to wait for results?
325 GNUNET_DHT_get_start(dht_handle, timeout,
326 GNUNET_BLOCK_TYPE_TEST, //FIXME todo
328 5, //Replication level FIXME
330 &rtype, //xquery FIXME this is bad
331 sizeof(GNUNET_GNS_RECORD_PKEY),
332 &process_authority_dht_result,
338 * Function called when we get a result from the dht
341 * @param cls the query handle
342 * @param exp lifetime
343 * @param key the key the record was stored under
344 * @param get_path get path
345 * @param get_path_length get path length
346 * @param put_path put path
347 * @param put_path_length put path length
348 * @param type the block type
349 * @param size the size of the record
350 * @param data the record data
353 process_name_dht_result(void* cls,
354 struct GNUNET_TIME_Absolute exp,
355 const GNUNET_HashCode * key,
356 const struct GNUNET_PeerIdentity *get_path,
357 unsigned int get_path_length,
358 const struct GNUNET_PeerIdentity *put_path,
359 unsigned int put_path_length,
360 enum GNUNET_BLOCK_Type type,
361 size_t size, const void *data)
363 struct GNUNET_GNS_PendingQuery *query;
364 uint32_t num_records;
367 struct GNUNET_CRYPTO_RsaSignature *signature;
370 GNUNET_HashCode zone, name_hash;
375 query = (struct GNUNET_GNS_PendingQuery *)cls;
378 num_records = ntohl(*pos);
379 struct GNUNET_NAMESTORE_RecordData rd[num_records];
381 pos += sizeof(uint32_t);
383 for (i=0; i<num_records; i++)
385 namelen = ntohs(*pos);
386 pos += sizeof(uint16_t);
388 //name must be 0 terminated
392 rd[i].record_type = ntohl(*pos);
393 pos += sizeof(uint32_t);
395 rd[i].data_size = ntohl(*pos);
396 pos += sizeof(uint32_t);
399 pos += rd[i].data_size;
401 rd[i].expiration = GNUNET_TIME_absolute_ntoh(
402 *((struct GNUNET_TIME_AbsoluteNBO*)pos));
403 pos += sizeof(struct GNUNET_TIME_AbsoluteNBO);
405 rd[i].flags = ntohs(*pos);
406 pos += sizeof(uint16_t);
409 if (strcmp(name, query->name) && rd[i].record_type == query->type)
416 if ((((char*)data)-pos) < sizeof(struct GNUNET_CRYPTO_RsaSignature))
418 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
419 "Cannot parse signature in DHT response. Corrupted or Missing");
423 signature = (struct GNUNET_CRYPTO_RsaSignature*)pos;
425 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
426 GNUNET_CRYPTO_hash_xor(key, &name_hash, &zone);
429 GNUNET_NAMESTORE_record_put (namestore_handle,
436 &on_namestore_record_put_result, //cont
441 //FIXME: add records to query handle, but on stack!
442 //do we need records in query handle? can't we just
443 //pass them to reply_to_dns?
444 reply_to_dns(query, num_records, rd);
448 * data is a serialized GNS record of type
449 * Check if record type and name match in query and reply
455 * Start DHT lookup for a (name -> query->record_type) record in
456 * query->authority's zone
458 * @param query the pending gns query
459 * @param name the name to query record
462 resolve_name_dht(struct GNUNET_GNS_PendingQuery *query, const char* name)
464 struct GNUNET_TIME_Relative timeout;
465 GNUNET_HashCode name_hash;
466 GNUNET_HashCode lookup_key;
468 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
469 GNUNET_CRYPTO_hash_xor(&name_hash, query->authority, &lookup_key);
471 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 20);
473 //FIXME how long to wait for results?
474 GNUNET_DHT_get_start(dht_handle, timeout,
475 GNUNET_BLOCK_TYPE_TEST, //FIXME todo
477 5, //Replication level FIXME
479 &query->type, //xquery
481 &process_name_dht_result,
488 resolve_name(struct GNUNET_GNS_PendingQuery *query, GNUNET_HashCode *zone);
491 * This is a callback function that should give us only PKEY
492 * records. Used to query the namestore for the authority (PKEY)
495 * @param cls the pending query
496 * @param zone our zone hash
497 * @param name the name for which we need an authority
498 * @param record_type the type of record (PKEY)
499 * @param expiration expiration date of the record
500 * @param flags namestore record flags
501 * @param sig_loc the location of the record in the signature tree
502 * @param size the size of the record
503 * @param data the record data
506 process_authority_lookup(void* cls,
507 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
508 struct GNUNET_TIME_Absolute expiration,
510 unsigned int rd_count,
511 const struct GNUNET_NAMESTORE_RecordData *rd,
512 const struct GNUNET_CRYPTO_RsaSignature *signature)
514 struct GNUNET_GNS_PendingQuery *query;
515 GNUNET_HashCode zone;
517 query = (struct GNUNET_GNS_PendingQuery *)cls;
518 GNUNET_CRYPTO_hash(key, GNUNET_CRYPTO_RSA_KEY_LENGTH, &zone);
521 * No authority found in namestore.
526 * We did not find an authority in the namestore
527 * _IF_ the current authoritative zone is us we cannot resolve
528 * _ELSE_ we can still check the dht
530 if (GNUNET_CRYPTO_hash_cmp(&zone, &zone_hash))
532 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Authority unknown\n");
533 //FIXME return NX answer
540 resolve_authority_dht(query, name);
544 //Note only 1 pkey should have been returned.. anything else would be strange
546 * We found an authority that may be able to help us
550 GNUNET_assert(rd->record_type == GNUNET_GNS_RECORD_PKEY);
551 GNUNET_HashCode *pkey_hash = GNUNET_malloc(sizeof(GNUNET_HashCode));
552 GNUNET_CRYPTO_hash(rd->data, GNUNET_CRYPTO_RSA_KEY_LENGTH, pkey_hash);
553 GNUNET_free_non_null(query->authority);
554 query->authority = pkey_hash;
555 resolve_name(query, query->authority);
561 * Reply to client with the result from our lookup.
563 * @param answer the pending query used in the lookup
566 reply_to_dns(struct GNUNET_GNS_PendingQuery *answer, uint32_t rd_count,
567 const struct GNUNET_NAMESTORE_RecordData *rd)
569 struct GNUNET_DNSPARSER_Flags dnsflags;
574 struct GNUNET_DNSPARSER_Packet packet;
575 struct GNUNET_DNSPARSER_Record answer_records[answer->num_records];
576 packet.answers = answer_records;
578 len = sizeof(struct GNUNET_DNSPARSER_Record*);
579 for (i=0; i < rd_count; i++)
581 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
582 "Adding type %d to DNS response\n", rd[i].record_type);
583 answer_records[i].name = answer->original_name; //FIXME yes?
584 answer_records[i].type = rd[i].record_type;
585 answer_records[i].data.raw.data_len = rd[i].data_size;
586 answer_records[i].data.raw.data = (char*)rd[i].data;
587 answer_records[i].expiration_time = rd[i].expiration;
588 answer_records[i].class = GNUNET_DNSPARSER_CLASS_INTERNET;//hmmn
589 //GNUNET_free(i->record); DO this later!
591 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "after memcpy\n");
592 /* FIXME how to handle auth, additional etc */
593 packet.num_answers = answer->num_records;
594 packet.num_authority_records = answer->num_authority_records;
596 dnsflags.authoritative_answer = 1;
597 dnsflags.opcode = GNUNET_DNSPARSER_OPCODE_QUERY;
598 dnsflags.return_code = GNUNET_DNSPARSER_RETURN_CODE_NO_ERROR; //not sure
599 dnsflags.query_or_response = 1;
600 packet.flags = dnsflags;
602 packet.id = answer->id;
604 //FIXME this is silently discarded
605 ret = GNUNET_DNSPARSER_pack (&packet,
606 1024, /* FIXME magic from dns redirector */
609 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
610 "Built DNS response! (ret=%d)\n", ret);
611 if (ret == GNUNET_OK)
613 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
614 "Answering DNS request\n");
615 GNUNET_DNS_request_answer(answer->request_handle,
618 //GNUNET_free(answer);
619 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Answered DNS request\n");
620 //FIXME return code, free datastructures
624 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
625 "Error building DNS response! (ret=%d)", ret);
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 zone the zone of the lookup
636 * @param name the name looked up
637 * @param record_type the record type
638 * @param expiration lifetime of the record
639 * @param flags record flags
640 * @param sig_loc location of the record in the signature tree
641 * @param size the size of the record
642 * @param data the record data
645 process_authoritative_result(void* cls,
646 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
647 struct GNUNET_TIME_Absolute expiration,
648 const char *name, unsigned int rd_count,
649 const struct GNUNET_NAMESTORE_RecordData *rd,
650 const struct GNUNET_CRYPTO_RsaSignature *signature)
652 struct GNUNET_GNS_PendingQuery *query;
653 struct GNUNET_GNS_QueryRecordList *qrecord;
654 struct GNUNET_NAMESTORE_RecordData *record;
655 struct GNUNET_TIME_Relative remaining_time;
656 GNUNET_HashCode zone;
658 query = (struct GNUNET_GNS_PendingQuery *) cls;
659 GNUNET_CRYPTO_hash(key, GNUNET_CRYPTO_RSA_KEY_LENGTH, &zone);
660 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
662 //FIXME Handle results in rd
668 * Lookup terminated and no results
669 * -> DHT Phase unless data is recent
671 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
672 "Namestore lookup terminated. without results\n");
675 * if this is not our zone we cannot rely on the namestore to be
676 * complete. -> Query DHT
678 if (!GNUNET_CRYPTO_hash_cmp(&zone, &zone_hash))
680 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
681 if (remaining_time.rel_value == 0)
683 resolve_name_dht(query, name);
688 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Record is still recent. No DHT lookup\n");
693 * Our zone and no result? Cannot resolve TT
694 * FIXME modify query to say NX
696 GNUNET_assert(query->answered == 0);
697 reply_to_dns(query, 0, NULL); //answered should be 0
706 * FIXME Check record expiration and dht expiration
707 * consult dht if necessary
709 if (remaining_time.rel_value == 0)
711 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
712 "This dht entry is old. Refreshing.\n");
713 resolve_name_dht(query, name);
716 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
717 "Processing additional result %s from namestore\n", name);
719 for (i=0; i<rd_count;i++)
721 if ((GNUNET_TIME_absolute_get_remaining (rd[i].expiration)).rel_value
724 //FIXME there is a catch here...
725 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "This record is expired. Skipping\n");
728 // A time will come when this has to be freed
729 qrecord = GNUNET_malloc(sizeof(struct GNUNET_GNS_QueryRecordList));
730 record = GNUNET_malloc(sizeof(struct GNUNET_NAMESTORE_RecordData));
731 qrecord->record = record;
733 //fixme into gns_util
734 //parse_record(rd[i]->data, rd[i]->data_size, 0, record);
735 GNUNET_CONTAINER_DLL_insert(query->records_head,
738 query->num_records++;
741 //we need to resolve to the original name in the end though...
742 //keep in mind. This can also be done later probably
743 //record->name = (char*)query->original_name;
746 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Found answer to query!\n");
749 reply_to_dns(query, rd_count, rd);
754 * Determine if this name is canonical.
756 * a.b.gnunet = not canonical
759 * @param name the name to test
760 * @return 1 if canonical
763 is_canonical(char* name)
765 uint32_t len = strlen(name);
768 for (i=0; i<len; i++)
770 if (*(name+i) == '.')
777 * Move one level up in the domain hierarchy and return the
778 * passed top level domain.
779 * FIXME this needs a better name
781 * @param name the domain
784 char* pop_tld(char* name)
788 if (is_canonical(name))
791 for (len = strlen(name); len > 0; len--)
793 if (*(name+len) == '.')
800 name[len] = '\0'; //terminate string
807 * The first phase of resolution.
808 * First check if the name is canonical.
809 * If it is then try to resolve directly.
810 * If not then first have to resolve the authoritative entities.
812 * @param query the pending lookup
813 * @param zone the zone we are currently resolving in
816 resolve_name(struct GNUNET_GNS_PendingQuery *query, GNUNET_HashCode *zone)
818 if (is_canonical(query->name))
820 //We only need to check this zone's ns
821 GNUNET_NAMESTORE_lookup_record(namestore_handle,
825 &process_authoritative_result,
830 //We have to resolve the authoritative entity
831 char *new_authority = pop_tld(query->name);
832 GNUNET_NAMESTORE_lookup_record(namestore_handle,
835 GNUNET_GNS_RECORD_PKEY,
836 &process_authority_lookup,
842 * Entry point for name resolution
843 * Lookup local namestore of our zone.
845 * Setup a new query and try to resolve
847 * @param rh the request handle of the DNS request from a client
848 * @param name the name to look up
849 * @param id the id of the dns request (for the reply)
850 * @param type the record type to look for
853 start_resolution(struct GNUNET_DNS_RequestHandle *rh,
854 char* name, uint16_t id, uint16_t type)
856 struct GNUNET_GNS_PendingQuery *query;
858 //FIXME remove .gnunet here from name
859 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "This is .gnunet (%s)!\n", name);
860 query = GNUNET_malloc(sizeof (struct GNUNET_GNS_PendingQuery));
862 query->original_name = name; //Full name of original query
864 //FIXME do not forget to free!!
865 query->name = GNUNET_malloc(strlen(name)-strlen(gnunet_tld) + 1);
866 memset(query->name, 0, strlen(name)-strlen(gnunet_tld) + 1);
867 memcpy(query->name, name, strlen(name)-strlen(gnunet_tld));
870 query->request_handle = rh;
872 //Start resolution in our zone
873 resolve_name(query, &zone_hash);
877 * The DNS request handler
878 * Called for every incoming DNS request.
881 * @param rh request handle to user for reply
882 * @param request_length number of bytes in request
883 * @param request udp payload of the DNS request
886 handle_dns_request(void *cls,
887 struct GNUNET_DNS_RequestHandle *rh,
888 size_t request_length,
891 struct GNUNET_DNSPARSER_Packet *p;
895 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Hijacked a DNS request...processing\n");
896 p = GNUNET_DNSPARSER_parse (request, request_length);
900 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
901 "Received malformed DNS packet, leaving it untouched\n");
902 GNUNET_DNS_request_forward (rh);
907 * Check tld and decide if we or
908 * legacy dns is responsible
910 * FIXME now in theory there could be more than 1 query in the request
911 * but if this is case we get into trouble:
912 * either we query the GNS or the DNS. We cannot do both!
913 * So I suggest to either only allow a single query per request or
914 * only allow GNS or DNS requests.
915 * The way it is implemented here now is buggy and will lead to erratic
916 * behaviour (if multiple queries are present).
918 for (i=0;i<p->num_queries;i++)
920 tldoffset = p->queries[i].name + strlen(p->queries[i].name);
922 while ((*tldoffset) != '.')
925 if (0 == strcmp(tldoffset, gnunet_tld))
927 start_resolution(rh, p->queries[i].name, p->id, p->queries[i].type);
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[i].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 in_addr *alice = GNUNET_malloc(sizeof(struct in_addr));
952 struct in_addr *bob = GNUNET_malloc(sizeof(struct in_addr));
953 struct GNUNET_NAMESTORE_RecordData *rda;
954 struct GNUNET_NAMESTORE_RecordData *rdb;
956 rda = GNUNET_malloc(sizeof(struct GNUNET_NAMESTORE_RecordData));
957 rdb = GNUNET_malloc(sizeof(struct GNUNET_NAMESTORE_RecordData));
959 GNUNET_assert(1 == inet_pton (AF_INET, ipA, alice));
960 GNUNET_assert(1 == inet_pton (AF_INET, ipB, bob));
962 rda->data_size = sizeof(struct in_addr);
963 rdb->data_size = sizeof(struct in_addr);
966 rda->record_type = GNUNET_GNS_RECORD_TYPE_A;
967 rdb->record_type = GNUNET_GNS_RECORD_TYPE_A;
968 rda->expiration = GNUNET_TIME_absolute_get_forever ();
969 rdb->expiration = GNUNET_TIME_absolute_get_forever ();
971 GNUNET_NAMESTORE_record_create (namestore_handle,
977 GNUNET_NAMESTORE_record_create (namestore_handle,
986 update_zone_dht_next(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
988 GNUNET_NAMESTORE_zone_iterator_next(namestore_iter);
992 * Function used to put all records successively into the DHT.
994 * @param cls the closure (NULL)
995 * @param zone our root zone hash
996 * @param name the name of the record
997 * @param record_type the type of the record
998 * @param expiration lifetime of the record
999 * @param flags flags of the record
1000 * @param sig_loc location of record in signature tree
1001 * @param size size of the record
1002 * @param record_data the record data
1005 put_gns_record(void *cls,
1006 const const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
1007 struct GNUNET_TIME_Absolute expiration,
1009 unsigned int rd_count,
1010 const struct GNUNET_NAMESTORE_RecordData *rd,
1011 const struct GNUNET_CRYPTO_RsaSignature *signature)
1013 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Putting records into the DHT\n");
1014 struct GNUNET_TIME_Relative timeout;
1015 GNUNET_HashCode name_hash;
1016 GNUNET_HashCode xor_hash;
1018 if (NULL == name) //We're done
1020 GNUNET_NAMESTORE_zone_iteration_stop (namestore_iter);
1024 * FIXME magic number 20 move to config file
1026 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 20);
1027 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
1028 GNUNET_CRYPTO_hash_xor(&zone_hash, &name_hash, &xor_hash);
1029 GNUNET_DHT_put (dht_handle, &xor_hash,
1030 5, //replication level
1032 GNUNET_BLOCK_TYPE_TEST, //FIXME todo block plugin
1037 NULL, //FIXME continuation needed? success check? yes ofc
1038 NULL); //cls for cont
1040 num_public_records++;
1043 * Reschedule periodic put
1045 GNUNET_SCHEDULER_add_delayed (dht_update_interval,
1046 &update_zone_dht_next,
1052 * Periodically iterate over our zone and store everything in dht
1055 * @param tc task context
1058 update_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1060 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Update zone!\n");
1061 dht_update_interval = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS,
1062 (3600/num_public_records));
1063 num_public_records = 0; //start counting again
1064 namestore_iter = GNUNET_NAMESTORE_zone_iteration_start (namestore_handle,
1066 GNUNET_NAMESTORE_RF_AUTHORITY,
1067 GNUNET_NAMESTORE_RF_PRIVATE,
1073 * Process GNS requests.
1075 * @param cls closure
1076 * @param server the initialized server
1077 * @param c configuration to use
1080 run (void *cls, struct GNUNET_SERVER_Handle *server,
1081 const struct GNUNET_CONFIGURATION_Handle *c)
1084 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Init GNS\n");
1085 zone_key = GNUNET_CRYPTO_rsa_key_create ();
1087 GNUNET_CRYPTO_hash(zone_key, GNUNET_CRYPTO_RSA_KEY_LENGTH,//FIXME is this ok?
1089 nc = GNUNET_SERVER_notification_context_create (server, 1);
1091 /* FIXME - do some config parsing
1092 * - Maybe only hijack dns if option is set (HIJACK_DNS=1)
1095 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
1098 * Do gnunet dns init here
1100 dns_handle = GNUNET_DNS_connect(c,
1101 GNUNET_DNS_FLAG_PRE_RESOLUTION,
1102 &handle_dns_request, /* rh */
1103 NULL); /* Closure */
1105 if (NULL == dns_handle)
1107 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1108 "Failed to connect to the dnsservice!\n");
1112 * handle to our local namestore
1114 namestore_handle = GNUNET_NAMESTORE_connect(c);
1116 if (NULL == namestore_handle)
1118 //FIXME do error handling;
1119 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1120 "Failed to connect to the namestore!\n");
1126 dht_handle = GNUNET_DHT_connect(c, 1); //FIXME get ht_len from cfg
1128 if (NULL == dht_handle)
1130 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not connect to DHT!\n");
1133 put_some_records(); //FIXME for testing
1136 * Schedule periodic put
1138 * We have roughly an hour for all records;
1140 dht_update_interval = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS,
1141 60); //FIXME from cfg
1142 GNUNET_SCHEDULER_add_delayed (dht_update_interval,
1143 &update_zone_dht_start,
1145 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "GNS Init done!\n");
1151 * The main function for the GNS service.
1153 * @param argc number of arguments from the command line
1154 * @param argv command line arguments
1155 * @return 0 ok, 1 on error
1158 main (int argc, char *const *argv)
1164 GNUNET_SERVICE_run (argc, argv, "gns", GNUNET_SERVICE_OPTION_NONE, &run,
1169 /* end of gnunet-service-gns.c */