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 * - (de-)serialisation of records/signature trees
27 * - The smaller FIXME issues all around
29 * @file gns/gnunet-service-gns.c
30 * @brief GNUnet GNS service
31 * @author Martin Schanzenbach
34 #include "gnunet_util_lib.h"
35 #include "gnunet_transport_service.h"
36 #include "gnunet_dns_service.h"
37 #include "gnunet_dnsparser_lib.h"
38 #include "gnunet_dht_service.h"
39 #include "gnunet_namestore_service.h"
40 #include "gnunet_gns_service.h"
43 /* Ignore for now not used anyway and probably never will */
44 #define GNUNET_MESSAGE_TYPE_GNS_CLIENT_LOOKUP 23
45 #define GNUNET_MESSAGE_TYPE_GNS_CLIENT_RESULT 24
47 struct GNUNET_GNS_QueryRecordList
52 struct GNUNET_GNS_QueryRecordList * next;
53 struct GNUNET_GNS_QueryRecordList * prev;
55 struct GNUNET_NAMESTORE_RecordData * record;
59 * A result list for namestore queries
61 struct GNUNET_GNS_PendingQuery
63 /* the answer packet */
64 struct GNUNET_DNSPARSER_Packet *answer;
66 /* records to put into answer packet */
67 struct GNUNET_GNS_QueryRecordList * records_head;
68 struct GNUNET_GNS_QueryRecordList * records_tail;
71 int num_authority_records; //FIXME are all of our replies auth?
77 /* the dns request id */
78 int id; // FIXME can handle->request_id also be used here?
80 /* the request handle to reply to */
81 struct GNUNET_DNS_RequestHandle *request_handle;
83 /* hast this query been answered? */
86 /* the authoritative zone to query */
87 GNUNET_HashCode *authority;
89 /* we have an authority in namestore that
90 * may be able to resolve
97 * Our handle to the DNS handler library
99 struct GNUNET_DNS_Handle *dns_handle;
102 * Our handle to the DHT
104 struct GNUNET_DHT_Handle *dht_handle;
107 * Our zone's private key
109 struct GNUNET_CRYPTO_RsaPrivateKey *zone_key;
112 * Our handle to the namestore service
114 struct GNUNET_NAMESTORE_Handle *namestore_handle;
116 struct GNUNET_NAMESTORE_ZoneIterator *namestore_iter;
119 * The configuration the GNS service is running with
121 const struct GNUNET_CONFIGURATION_Handle *GNS_cfg;
124 * Our notification context.
126 static struct GNUNET_SERVER_NotificationContext *nc;
131 GNUNET_HashCode zone_hash;
134 * Our tld. Maybe get from config file
136 const char* gnunet_tld = ".gnunet";
139 * Useful for zone update for DHT put
141 static int num_public_records = 3600;
142 struct GNUNET_TIME_Relative dht_update_interval;
145 void reply_to_dns(struct GNUNET_GNS_PendingQuery *answer);
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 * Function called when we get a result from the dht
167 * @param cls the query handle
168 * @param exp lifetime
169 * @param key the key the record was stored under
170 * @param get_path get path
171 * @param get_path_length get path length
172 * @param put_path put path
173 * @param put_path_length put path length
174 * @param type the block type
175 * @param size the size of the record
176 * @param data the record data
179 process_authority_dht_result(void* cls,
180 struct GNUNET_TIME_Absolute exp,
181 const GNUNET_HashCode * key,
182 const struct GNUNET_PeerIdentity *get_path,
183 unsigned int get_path_length,
184 const struct GNUNET_PeerIdentity *put_path,
185 unsigned int put_path_length,
186 enum GNUNET_BLOCK_Type type,
187 size_t size, const void *data)
189 struct GNUNET_GNS_PendingQuery *query;
190 uint32_t num_records;
193 struct GNUNET_CRYPTO_RsaSignature *signature;
196 GNUNET_HashCode zone, name_hash;
201 query = (struct GNUNET_GNS_PendingQuery *)cls;
204 num_records = ntohl(*pos);
205 struct GNUNET_NAMESTORE_RecordData rd[num_records];
207 pos += sizeof(uint32_t);
209 for (i=0; i<num_records; i++)
211 namelen = ntohs(*pos);
212 pos += sizeof(uint16_t);
214 //name must be 0 terminated
218 rd[i].record_type = ntohl(*pos);
219 pos += sizeof(uint32_t);
221 rd[i].data_size = ntohl(*pos);
222 pos += sizeof(uint32_t);
225 pos += rd[i].data_size;
227 rd[i].expiration = GNUNET_TIME_absolute_ntoh(
228 *((struct GNUNET_TIME_AbsoluteNBO*)pos));
229 pos += sizeof(struct GNUNET_TIME_AbsoluteNBO);
231 rd[i].flags = ntohs(*pos);
232 pos += sizeof(uint16_t);
235 if (strcmp(name, query->name) && rd[i].record_type == query->type)
242 if ((((char*)data)-pos) < sizeof(struct GNUNET_CRYPTO_RsaSignature))
244 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
245 "Cannot parse signature in DHT response. Corrupted or Missing");
249 signature = (struct GNUNET_CRYPTO_RsaSignature*)pos;
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,
268 memcpy(query->authority, &zone, sizeof(GNUNET_HashCode));
269 resolve_name(query, query->authority);
272 * data is a serialized PKEY record (probably)
273 * parse, put into namestore
274 * namestore zone hash is in query.
275 * Then adjust query->name and call resolve_name
276 * with new zone (the one just received)
278 * query->authority = new_authority
279 * resolve_name(query, new_authority);
284 * Start DHT lookup for a name -> PKEY (compare NS) record in
285 * query->authority's zone
287 * @param query the pending gns query
288 * @param name the name of the PKEY record
291 resolve_authority_dht(struct GNUNET_GNS_PendingQuery *query, const char* name)
293 enum GNUNET_GNS_RecordType rtype = GNUNET_GNS_RECORD_PKEY;
294 struct GNUNET_TIME_Relative timeout;
295 GNUNET_HashCode name_hash;
296 GNUNET_HashCode lookup_key;
298 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
299 GNUNET_CRYPTO_hash_xor(&name_hash, query->authority, &lookup_key);
301 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 20);
303 //FIXME how long to wait for results?
304 GNUNET_DHT_get_start(dht_handle, timeout,
305 GNUNET_BLOCK_TYPE_TEST, //FIXME todo
307 5, //Replication level FIXME
309 &rtype, //xquery FIXME this is bad
310 sizeof(GNUNET_GNS_RECORD_PKEY),
311 &process_authority_dht_result,
317 * Function called when we get a result from the dht
320 * @param cls the query handle
321 * @param exp lifetime
322 * @param key the key the record was stored under
323 * @param get_path get path
324 * @param get_path_length get path length
325 * @param put_path put path
326 * @param put_path_length put path length
327 * @param type the block type
328 * @param size the size of the record
329 * @param data the record data
332 process_name_dht_result(void* cls,
333 struct GNUNET_TIME_Absolute exp,
334 const GNUNET_HashCode * key,
335 const struct GNUNET_PeerIdentity *get_path,
336 unsigned int get_path_length,
337 const struct GNUNET_PeerIdentity *put_path,
338 unsigned int put_path_length,
339 enum GNUNET_BLOCK_Type type,
340 size_t size, const void *data)
342 struct GNUNET_GNS_PendingQuery *query;
343 uint32_t num_records;
346 struct GNUNET_CRYPTO_RsaSignature *signature;
349 GNUNET_HashCode zone, name_hash;
354 query = (struct GNUNET_GNS_PendingQuery *)cls;
357 num_records = ntohl(*pos);
358 struct GNUNET_NAMESTORE_RecordData rd[num_records];
360 pos += sizeof(uint32_t);
362 for (i=0; i<num_records; i++)
364 namelen = ntohs(*pos);
365 pos += sizeof(uint16_t);
367 //name must be 0 terminated
371 rd[i].record_type = ntohl(*pos);
372 pos += sizeof(uint32_t);
374 rd[i].data_size = ntohl(*pos);
375 pos += sizeof(uint32_t);
378 pos += rd[i].data_size;
380 rd[i].expiration = GNUNET_TIME_absolute_ntoh(
381 *((struct GNUNET_TIME_AbsoluteNBO*)pos));
382 pos += sizeof(struct GNUNET_TIME_AbsoluteNBO);
384 rd[i].flags = ntohs(*pos);
385 pos += sizeof(uint16_t);
388 if (strcmp(name, query->name) && rd[i].record_type == query->type)
395 if ((((char*)data)-pos) < sizeof(struct GNUNET_CRYPTO_RsaSignature))
397 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
398 "Cannot parse signature in DHT response. Corrupted or Missing");
402 signature = (struct GNUNET_CRYPTO_RsaSignature*)pos;
404 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
405 GNUNET_CRYPTO_hash_xor(key, &name_hash, &zone);
408 GNUNET_NAMESTORE_record_put (namestore_handle,
420 //FIXME: add records to query handle, but on stack!
421 //do we need records in query handle? can't we just
422 //pass them to reply_to_dns?
427 * data is a serialized GNS record of type
428 * Check if record type and name match in query and reply
434 * Start DHT lookup for a (name -> query->record_type) record in
435 * query->authority's zone
437 * @param query the pending gns query
438 * @param name the name to query record
441 resolve_name_dht(struct GNUNET_GNS_PendingQuery *query, const char* name)
443 struct GNUNET_TIME_Relative timeout;
444 GNUNET_HashCode name_hash;
445 GNUNET_HashCode lookup_key;
447 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
448 GNUNET_CRYPTO_hash_xor(&name_hash, query->authority, &lookup_key);
450 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 20);
452 //FIXME how long to wait for results?
453 GNUNET_DHT_get_start(dht_handle, timeout,
454 GNUNET_BLOCK_TYPE_TEST, //FIXME todo
456 5, //Replication level FIXME
458 &query->type, //xquery
460 &process_name_dht_result,
467 resolve_name(struct GNUNET_GNS_PendingQuery *query, GNUNET_HashCode *zone);
470 * This is a callback function that should give us only PKEY
471 * records. Used to query the namestore for the authority (PKEY)
474 * @param cls the pending query
475 * @param zone our zone hash
476 * @param name the name for which we need an authority
477 * @param record_type the type of record (PKEY)
478 * @param expiration expiration date of the record
479 * @param flags namestore record flags
480 * @param sig_loc the location of the record in the signature tree
481 * @param size the size of the record
482 * @param data the record data
485 process_authority_lookup(void* cls,
486 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
487 struct GNUNET_TIME_Absolute expiration,
489 unsigned int rd_count,
490 const struct GNUNET_NAMESTORE_RecordData *rd,
491 const struct GNUNET_CRYPTO_RsaSignature *signature)
493 struct GNUNET_GNS_PendingQuery *query;
494 GNUNET_HashCode zone;
496 query = (struct GNUNET_GNS_PendingQuery *)cls;
497 GNUNET_CRYPTO_hash(key, GNUNET_CRYPTO_RSA_KEY_LENGTH, &zone);
500 * No authority found in namestore.
505 * We did not find an authority in the namestore
506 * _IF_ the current authoritative zone is us we cannot resolve
507 * _ELSE_ we can still check the dht
509 if (GNUNET_CRYPTO_hash_cmp(&zone, &zone_hash))
511 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Authority unknown\n");
512 //FIXME return NX answer
519 resolve_authority_dht(query, name);
523 //Note only 1 pkey should have been returned.. anything else would be strange
525 * We found an authority that may be able to help us
529 GNUNET_assert(rd->record_type == GNUNET_GNS_RECORD_PKEY);
530 GNUNET_HashCode *pkey_hash = GNUNET_malloc(sizeof(GNUNET_HashCode));
531 GNUNET_CRYPTO_hash(rd->data, GNUNET_CRYPTO_RSA_KEY_LENGTH, pkey_hash);
532 GNUNET_free_non_null(query->authority);
533 query->authority = pkey_hash;
534 resolve_name(query, query->authority);
540 * Reply to client with the result from our lookup.
542 * @param answer the pending query used in the lookup
545 reply_to_dns(struct GNUNET_GNS_PendingQuery *answer)
547 struct GNUNET_GNS_QueryRecordList *i;
548 struct GNUNET_DNSPARSER_Packet *packet;
549 struct GNUNET_DNSPARSER_Flags dnsflags;
555 packet = GNUNET_malloc(sizeof(struct GNUNET_DNSPARSER_Packet));
557 GNUNET_malloc(sizeof(struct GNUNET_DNSPARSER_Record) * answer->num_records);
559 len = sizeof(struct GNUNET_DNSPARSER_Record*);
561 for (i=answer->records_head; i != NULL; i=i->next)
563 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
564 "Adding type %d to DNS response\n", i->record->record_type);
565 //FIXME build proper dnsparser record! this will fail!
566 //memcpy(&packet->answers[j],
568 // sizeof (struct GNUNET_DNSPARSER_Record));
569 GNUNET_free(i->record);
572 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "after memcpy\n");
573 /* FIXME how to handle auth, additional etc */
574 packet->num_answers = answer->num_records;
575 packet->num_authority_records = answer->num_authority_records;
577 dnsflags.authoritative_answer = 1;
578 dnsflags.opcode = GNUNET_DNSPARSER_OPCODE_QUERY;
579 dnsflags.return_code = GNUNET_DNSPARSER_RETURN_CODE_NO_ERROR; //not sure
580 dnsflags.query_or_response = 1;
581 packet->flags = dnsflags;
583 packet->id = answer->id;
585 //FIXME this is silently discarded
586 ret = GNUNET_DNSPARSER_pack (packet,
587 1024, /* FIXME magic from dns redirector */
590 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
591 "Built DNS response! (ret=%d)\n", ret);
592 if (ret == GNUNET_OK)
594 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
595 "Answering DNS request\n");
596 GNUNET_DNS_request_answer(answer->request_handle,
599 //GNUNET_free(answer);
600 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Answered DNS request\n");
601 //FIXME return code, free datastructures
605 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
606 "Error building DNS response! (ret=%d)", ret);
612 * Namestore calls this function if we have an entry for this name.
613 * (or data=null to indicate the lookup has finished)
615 * @param cls the pending query
616 * @param zone the zone of the lookup
617 * @param name the name looked up
618 * @param record_type the record type
619 * @param expiration lifetime of the record
620 * @param flags record flags
621 * @param sig_loc location of the record in the signature tree
622 * @param size the size of the record
623 * @param data the record data
626 process_authoritative_result(void* cls,
627 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
628 struct GNUNET_TIME_Absolute expiration,
629 const char *name, unsigned int rd_count,
630 const struct GNUNET_NAMESTORE_RecordData *rd,
631 const struct GNUNET_CRYPTO_RsaSignature *signature)
633 struct GNUNET_GNS_PendingQuery *query;
634 struct GNUNET_GNS_QueryRecordList *qrecord;
635 struct GNUNET_NAMESTORE_RecordData *record;
636 GNUNET_HashCode zone;
637 query = (struct GNUNET_GNS_PendingQuery *) cls;
638 GNUNET_CRYPTO_hash(key, GNUNET_CRYPTO_RSA_KEY_LENGTH, &zone);
640 //FIXME Handle results in rd
646 * Lookup terminated and no results
647 * -> DHT Phase unless data is recent
649 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
650 "Namestore lookup terminated. without results\n");
653 * if this is not our zone we cannot rely on the namestore to be
654 * complete. -> Query DHT
656 if (!GNUNET_CRYPTO_hash_cmp(&zone, &zone_hash))
658 //FIXME if very recent dht lookup -> cannot resolve
659 resolve_name_dht(query, name);
664 * Our zone and no result? Cannot resolve TT
665 * FIXME modify query to say NX
667 GNUNET_assert(query->answered == 0);
668 reply_to_dns(query); //answered should be 0
677 * FIXME Check record expiration and dht expiration
678 * consult dht if necessary
680 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
681 "Processing additional result %s from namestore\n", name);
683 for (i=0; i<rd_count;i++)
685 // A time will come when this has to be freed
686 qrecord = GNUNET_malloc(sizeof(struct GNUNET_GNS_QueryRecordList));
687 record = GNUNET_malloc(sizeof(struct GNUNET_NAMESTORE_RecordData));
688 qrecord->record = record;
690 //fixme into gns_util
691 //parse_record(rd[i]->data, rd[i]->data_size, 0, record);
692 GNUNET_CONTAINER_DLL_insert(query->records_head,
695 query->num_records++;
698 //we need to resolve to the original name in the end though...
699 //keep in mind. This can also be done later probably
700 //record->name = (char*)query->original_name;
703 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Found answer to query!\n");
711 * Determine if this name is canonical.
713 * a.b.gnunet = not canonical
716 * @param name the name to test
717 * @return 1 if canonical
720 is_canonical(char* name)
722 uint32_t len = strlen(name);
725 for (i=0; i<len; i++)
727 if (*(name+i) == '.')
734 * Move one level up in the domain hierarchy and return the
735 * passed top level domain.
736 * FIXME this needs a better name
738 * @param name the domain
741 char* pop_tld(char* name)
745 if (is_canonical(name))
748 for (len = strlen(name); len > 0; len--)
750 if (*(name+len) == '.')
757 name[len] = '\0'; //terminate string
764 * The first phase of resolution.
765 * First check if the name is canonical.
766 * If it is then try to resolve directly.
767 * If not then first have to resolve the authoritative entities.
769 * @param query the pending lookup
770 * @param zone the zone we are currently resolving in
773 resolve_name(struct GNUNET_GNS_PendingQuery *query, GNUNET_HashCode *zone)
775 if (is_canonical(query->name))
777 //We only need to check this zone's ns
778 GNUNET_NAMESTORE_lookup_record(namestore_handle,
782 &process_authoritative_result,
787 //We have to resolve the authoritative entity
788 char *new_authority = pop_tld(query->name);
789 GNUNET_NAMESTORE_lookup_record(namestore_handle,
792 GNUNET_GNS_RECORD_PKEY,
793 &process_authority_lookup,
799 * Entry point for name resolution
800 * Lookup local namestore of our zone.
802 * Setup a new query and try to resolve
804 * @param rh the request handle of the DNS request from a client
805 * @param name the name to look up
806 * @param id the id of the dns request (for the reply)
807 * @param type the record type to look for
810 start_resolution(struct GNUNET_DNS_RequestHandle *rh,
811 char* name, uint16_t id, uint16_t type)
813 struct GNUNET_GNS_PendingQuery *query;
815 //FIXME remove .gnunet here from name
816 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "This is .gnunet (%s)!\n", name);
817 query = GNUNET_malloc(sizeof (struct GNUNET_GNS_PendingQuery));
819 query->original_name = name; //Full name of original query
821 //FIXME do not forget to free!!
822 query->name = GNUNET_malloc(strlen(name)-strlen(gnunet_tld) + 1);
823 memset(query->name, 0, strlen(name)-strlen(gnunet_tld) + 1);
824 memcpy(query->name, name, strlen(name)-strlen(gnunet_tld));
827 query->request_handle = rh;
829 //Start resolution in our zone
830 resolve_name(query, &zone_hash);
834 * The DNS request handler
835 * Called for every incoming DNS request.
838 * @param rh request handle to user for reply
839 * @param request_length number of bytes in request
840 * @param request udp payload of the DNS request
843 handle_dns_request(void *cls,
844 struct GNUNET_DNS_RequestHandle *rh,
845 size_t request_length,
848 struct GNUNET_DNSPARSER_Packet *p;
852 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Hijacked a DNS request...processing\n");
853 p = GNUNET_DNSPARSER_parse (request, request_length);
857 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
858 "Received malformed DNS packet, leaving it untouched\n");
859 GNUNET_DNS_request_forward (rh);
864 * Check tld and decide if we or
865 * legacy dns is responsible
867 * FIXME now in theory there could be more than 1 query in the request
868 * but if this is case we get into trouble:
869 * either we query the GNS or the DNS. We cannot do both!
870 * So I suggest to either only allow a single query per request or
871 * only allow GNS or DNS requests.
872 * The way it is implemented here now is buggy and will lead to erratic
873 * behaviour (if multiple queries are present).
875 for (i=0;i<p->num_queries;i++)
877 tldoffset = p->queries[i].name + strlen(p->queries[i].name);
879 while ((*tldoffset) != '.')
882 if (0 == strcmp(tldoffset, gnunet_tld))
884 start_resolution(rh, p->queries[i].name, p->id, p->queries[i].type);
889 * This request does not concern us. Forward to real DNS.
891 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
892 "Request for %s is forwarded to DNS\n", p->queries[i].name);
893 GNUNET_DNS_request_forward (rh);
899 * test function that stores some data in the namestore
902 put_some_records(void)
904 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Populating namestore\n");
905 /* put a few records into namestore */
906 char* ipA = "1.2.3.4";
907 char* ipB = "5.6.7.8";
908 struct in_addr *alice = GNUNET_malloc(sizeof(struct in_addr));
909 struct in_addr *bob = GNUNET_malloc(sizeof(struct in_addr));
910 struct GNUNET_NAMESTORE_RecordData *rda;
911 struct GNUNET_NAMESTORE_RecordData *rdb;
913 rda = GNUNET_malloc(sizeof(struct GNUNET_NAMESTORE_RecordData));
914 rdb = GNUNET_malloc(sizeof(struct GNUNET_NAMESTORE_RecordData));
916 GNUNET_assert(1 == inet_pton (AF_INET, ipA, alice));
917 GNUNET_assert(1 == inet_pton (AF_INET, ipB, bob));
919 rda->data_size = sizeof(struct in_addr);
920 rdb->data_size = sizeof(struct in_addr);
923 rda->record_type = GNUNET_GNS_RECORD_TYPE_A;
924 rdb->record_type = GNUNET_GNS_RECORD_TYPE_A;
925 rda->expiration = GNUNET_TIME_absolute_get_forever ();
926 rdb->expiration = GNUNET_TIME_absolute_get_forever ();
928 GNUNET_NAMESTORE_record_create (namestore_handle,
934 GNUNET_NAMESTORE_record_create (namestore_handle,
943 update_zone_dht_next(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
945 GNUNET_NAMESTORE_zone_iterator_next(namestore_iter);
949 * Function used to put all records successively into the DHT.
951 * @param cls the closure (NULL)
952 * @param zone our root zone hash
953 * @param name the name of the record
954 * @param record_type the type of the record
955 * @param expiration lifetime of the record
956 * @param flags flags of the record
957 * @param sig_loc location of record in signature tree
958 * @param size size of the record
959 * @param record_data the record data
962 put_gns_record(void *cls,
963 const const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
964 struct GNUNET_TIME_Absolute expiration,
966 unsigned int rd_count,
967 const struct GNUNET_NAMESTORE_RecordData *rd,
968 const struct GNUNET_CRYPTO_RsaSignature *signature)
970 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Putting records into the DHT\n");
971 struct GNUNET_TIME_Relative timeout;
972 GNUNET_HashCode name_hash;
973 GNUNET_HashCode xor_hash;
975 if (NULL == name) //We're done
977 GNUNET_NAMESTORE_zone_iteration_stop (namestore_iter);
981 * FIXME magic number 20 move to config file
983 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 20);
984 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
985 GNUNET_CRYPTO_hash_xor(&zone_hash, &name_hash, &xor_hash);
986 GNUNET_DHT_put (dht_handle, &xor_hash,
987 5, //replication level
989 GNUNET_BLOCK_TYPE_TEST, //FIXME todo block plugin
994 NULL, //FIXME continuation needed? success check? yes ofc
995 NULL); //cls for cont
998 * Reschedule periodic put
1000 GNUNET_SCHEDULER_add_delayed (dht_update_interval,
1001 &update_zone_dht_next,
1007 * Periodically iterate over our zone and store everything in dht
1010 * @param tc task context
1013 update_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1015 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Update zone!\n");
1016 dht_update_interval = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS,
1017 (3600/num_public_records));
1018 namestore_iter = GNUNET_NAMESTORE_zone_iteration_start (namestore_handle,
1020 GNUNET_NAMESTORE_RF_AUTHORITY,
1021 GNUNET_NAMESTORE_RF_PRIVATE,
1027 * Process GNS requests.
1029 * @param cls closure
1030 * @param server the initialized server
1031 * @param c configuration to use
1034 run (void *cls, struct GNUNET_SERVER_Handle *server,
1035 const struct GNUNET_CONFIGURATION_Handle *c)
1038 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Init GNS\n");
1039 zone_key = GNUNET_CRYPTO_rsa_key_create ();
1041 GNUNET_CRYPTO_hash(zone_key, GNUNET_CRYPTO_RSA_KEY_LENGTH,//FIXME is this ok?
1043 nc = GNUNET_SERVER_notification_context_create (server, 1);
1045 /* FIXME - do some config parsing
1046 * - Maybe only hijack dns if option is set (HIJACK_DNS=1)
1049 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
1052 * Do gnunet dns init here
1054 dns_handle = GNUNET_DNS_connect(c,
1055 GNUNET_DNS_FLAG_PRE_RESOLUTION,
1056 &handle_dns_request, /* rh */
1057 NULL); /* Closure */
1059 if (NULL == dns_handle)
1061 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1062 "Failed to connect to the dnsservice!\n");
1066 * handle to our local namestore
1068 namestore_handle = GNUNET_NAMESTORE_connect(c);
1070 if (NULL == namestore_handle)
1072 //FIXME do error handling;
1073 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1074 "Failed to connect to the namestore!\n");
1080 dht_handle = GNUNET_DHT_connect(c, 1); //FIXME get ht_len from cfg
1082 if (NULL == dht_handle)
1084 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not connect to DHT!\n");
1087 put_some_records(); //FIXME for testing
1090 * Schedule periodic put
1092 * We have roughly an hour for all records;
1094 dht_update_interval = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS,
1095 60); //FIXME from cfg
1096 GNUNET_SCHEDULER_add_delayed (dht_update_interval,
1097 &update_zone_dht_start,
1099 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "GNS Init done!\n");
1105 * The main function for the GNS service.
1107 * @param argc number of arguments from the command line
1108 * @param argv command line arguments
1109 * @return 0 ok, 1 on error
1112 main (int argc, char *const *argv)
1118 GNUNET_SERVICE_run (argc, argv, "gns", GNUNET_SERVICE_OPTION_NONE, &run,
1123 /* end of gnunet-service-gns.c */