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_ResolverHandle
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?
74 /* the request handle to reply to */
75 struct GNUNET_DNS_RequestHandle *request_handle;
77 /* hast this query been answered? */
80 /* the authoritative zone to query */
81 GNUNET_HashCode *authority;
83 /* we have an authority in namestore that
84 * may be able to resolve
88 struct GNUNET_DNSPARSER_Packet *packet;
90 struct GNUNET_DNSPARSER_Query *query;
95 * Our handle to the DNS handler library
97 struct GNUNET_DNS_Handle *dns_handle;
100 * Our handle to the DHT
102 struct GNUNET_DHT_Handle *dht_handle;
105 * Our zone's private key
107 struct GNUNET_CRYPTO_RsaPrivateKey *zone_key;
110 * Our handle to the namestore service
112 struct GNUNET_NAMESTORE_Handle *namestore_handle;
114 struct GNUNET_NAMESTORE_ZoneIterator *namestore_iter;
117 * The configuration the GNS service is running with
119 const struct GNUNET_CONFIGURATION_Handle *GNS_cfg;
122 * Our notification context.
124 static struct GNUNET_SERVER_NotificationContext *nc;
129 GNUNET_HashCode zone_hash;
132 * Our tld. Maybe get from config file
134 const char* gnunet_tld = ".gnunet";
137 * Useful for zone update for DHT put
139 static int num_public_records = 3600;
140 struct GNUNET_TIME_Relative dht_update_interval;
143 void reply_to_dns(struct GNUNET_GNS_ResolverHandle *answer, uint32_t rd_count,
144 const struct GNUNET_NAMESTORE_RecordData *rd);
145 void resolve_name(struct GNUNET_GNS_ResolverHandle *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_ResolverHandle *rh;
210 uint32_t num_records;
213 struct GNUNET_CRYPTO_RsaSignature *signature;
214 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *public_key;
217 GNUNET_HashCode zone, name_hash;
222 rh = (struct GNUNET_GNS_ResolverHandle *)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, rh->query->name) && rd[i].record_type == rh->query->type)
263 if ((((char*)data)-pos) <
264 (sizeof(struct GNUNET_CRYPTO_RsaSignature) +
265 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded)))
267 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
268 "Cannot parse signature/key in DHT response. Corrupted or Missing");
272 signature = (struct GNUNET_CRYPTO_RsaSignature*)pos;
273 pos += sizeof(struct GNUNET_CRYPTO_RsaSignature);
275 public_key = (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded*)pos;
276 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
277 GNUNET_CRYPTO_hash_xor(key, &name_hash, &zone);
280 GNUNET_NAMESTORE_record_put (namestore_handle,
287 &on_namestore_record_put_result, //cont
293 memcpy(rh->authority, &zone, sizeof(GNUNET_HashCode));
294 resolve_name(rh, rh->authority);
297 * data is a serialized PKEY record (probably)
298 * parse, put into namestore
299 * namestore zone hash is in query.
300 * Then adjust query->name and call resolve_name
301 * with new zone (the one just received)
303 * query->authority = new_authority
304 * resolve_name(query, new_authority);
309 * Start DHT lookup for a name -> PKEY (compare NS) record in
310 * query->authority's zone
312 * @param query the pending gns query
313 * @param name the name of the PKEY record
316 resolve_authority_dht(struct GNUNET_GNS_ResolverHandle *rh, const char* name)
318 enum GNUNET_GNS_RecordType rtype = GNUNET_GNS_RECORD_PKEY;
319 struct GNUNET_TIME_Relative timeout;
320 GNUNET_HashCode name_hash;
321 GNUNET_HashCode lookup_key;
323 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
324 GNUNET_CRYPTO_hash_xor(&name_hash, rh->authority, &lookup_key);
326 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 20);
328 //FIXME how long to wait for results?
329 GNUNET_DHT_get_start(dht_handle, timeout,
330 GNUNET_BLOCK_TYPE_TEST, //FIXME todo
332 5, //Replication level FIXME
334 &rtype, //xquery FIXME this is bad
335 sizeof(GNUNET_GNS_RECORD_PKEY),
336 &process_authority_dht_result,
342 * Function called when we get a result from the dht
345 * @param cls the query handle
346 * @param exp lifetime
347 * @param key the key the record was stored under
348 * @param get_path get path
349 * @param get_path_length get path length
350 * @param put_path put path
351 * @param put_path_length put path length
352 * @param type the block type
353 * @param size the size of the record
354 * @param data the record data
357 process_name_dht_result(void* cls,
358 struct GNUNET_TIME_Absolute exp,
359 const GNUNET_HashCode * key,
360 const struct GNUNET_PeerIdentity *get_path,
361 unsigned int get_path_length,
362 const struct GNUNET_PeerIdentity *put_path,
363 unsigned int put_path_length,
364 enum GNUNET_BLOCK_Type type,
365 size_t size, const void *data)
367 struct GNUNET_GNS_ResolverHandle *rh;
368 uint32_t num_records;
371 struct GNUNET_CRYPTO_RsaSignature *signature;
372 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *public_key;
375 GNUNET_HashCode zone, name_hash;
380 rh = (struct GNUNET_GNS_ResolverHandle *)cls;
383 num_records = ntohl(*pos);
384 struct GNUNET_NAMESTORE_RecordData rd[num_records];
386 pos += sizeof(uint32_t);
388 for (i=0; i<num_records; i++)
390 namelen = ntohs(*pos);
391 pos += sizeof(uint16_t);
393 //name must be 0 terminated
397 rd[i].record_type = ntohl(*pos);
398 pos += sizeof(uint32_t);
400 rd[i].data_size = ntohl(*pos);
401 pos += sizeof(uint32_t);
404 pos += rd[i].data_size;
406 rd[i].expiration = GNUNET_TIME_absolute_ntoh(
407 *((struct GNUNET_TIME_AbsoluteNBO*)pos));
408 pos += sizeof(struct GNUNET_TIME_AbsoluteNBO);
410 rd[i].flags = ntohs(*pos);
411 pos += sizeof(uint16_t);
414 if (strcmp(name, rh->query->name) && rd[i].record_type == rh->query->type)
421 if ((((char*)data)-pos) <
422 (sizeof(struct GNUNET_CRYPTO_RsaSignature) +
423 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded)))
425 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
426 "Cannot parse signature/key in DHT response. Corrupted or Missing");
430 signature = (struct GNUNET_CRYPTO_RsaSignature*)pos;
431 pos += sizeof(struct GNUNET_CRYPTO_RsaSignature);
433 public_key = (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded*)pos;
435 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
436 GNUNET_CRYPTO_hash_xor(key, &name_hash, &zone);
439 GNUNET_NAMESTORE_record_put (namestore_handle,
446 &on_namestore_record_put_result, //cont
451 //FIXME: add records to query handle, but on stack!
452 //do we need records in query handle? can't we just
453 //pass them to reply_to_dns?
454 reply_to_dns(rh, num_records, rd);
458 * data is a serialized GNS record of type
459 * Check if record type and name match in query and reply
465 * Start DHT lookup for a (name -> query->record_type) record in
466 * query->authority's zone
468 * @param query the pending gns query
469 * @param name the name to query record
472 resolve_name_dht(struct GNUNET_GNS_ResolverHandle *rh, const char* name)
474 struct GNUNET_TIME_Relative timeout;
475 GNUNET_HashCode name_hash;
476 GNUNET_HashCode lookup_key;
478 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
479 GNUNET_CRYPTO_hash_xor(&name_hash, rh->authority, &lookup_key);
481 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 20);
483 //FIXME how long to wait for results?
484 GNUNET_DHT_get_start(dht_handle, timeout,
485 GNUNET_BLOCK_TYPE_TEST, //FIXME todo
487 5, //Replication level FIXME
489 &rh->query->type, //xquery
490 sizeof(rh->query->type),
491 &process_name_dht_result,
498 resolve_name(struct GNUNET_GNS_ResolverHandle *query, GNUNET_HashCode *zone);
501 * This is a callback function that should give us only PKEY
502 * records. Used to query the namestore for the authority (PKEY)
505 * @param cls the pending query
506 * @param zone our zone hash
507 * @param name the name for which we need an authority
508 * @param record_type the type of record (PKEY)
509 * @param expiration expiration date of the record
510 * @param flags namestore record flags
511 * @param sig_loc the location of the record in the signature tree
512 * @param size the size of the record
513 * @param data the record data
516 process_authority_lookup(void* cls,
517 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
518 struct GNUNET_TIME_Absolute expiration,
520 unsigned int rd_count,
521 const struct GNUNET_NAMESTORE_RecordData *rd,
522 const struct GNUNET_CRYPTO_RsaSignature *signature)
524 struct GNUNET_GNS_ResolverHandle *query;
525 GNUNET_HashCode zone;
527 query = (struct GNUNET_GNS_ResolverHandle *)cls;
528 GNUNET_CRYPTO_hash(key, GNUNET_CRYPTO_RSA_KEY_LENGTH, &zone);
531 * No authority found in namestore.
536 * We did not find an authority in the namestore
537 * _IF_ the current authoritative zone is us we cannot resolve
538 * _ELSE_ we can still check the dht
540 if (GNUNET_CRYPTO_hash_cmp(&zone, &zone_hash))
542 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Authority unknown\n");
543 //FIXME return NX answer
550 resolve_authority_dht(query, name);
554 //Note only 1 pkey should have been returned.. anything else would be strange
556 * We found an authority that may be able to help us
560 GNUNET_assert(rd->record_type == GNUNET_GNS_RECORD_PKEY);
561 GNUNET_HashCode *pkey_hash = GNUNET_malloc(sizeof(GNUNET_HashCode));
562 GNUNET_CRYPTO_hash(rd->data, GNUNET_CRYPTO_RSA_KEY_LENGTH, pkey_hash);
563 GNUNET_free_non_null(query->authority);
564 query->authority = pkey_hash;
565 resolve_name(query, query->authority);
571 * Reply to client with the result from our lookup.
573 * @param answer the pending query used in the lookup
576 reply_to_dns(struct GNUNET_GNS_ResolverHandle *rh, uint32_t rd_count,
577 const struct GNUNET_NAMESTORE_RecordData *rd)
579 struct GNUNET_DNSPARSER_Flags dnsflags;
584 struct GNUNET_DNSPARSER_Packet *packet = rh->packet;
585 struct GNUNET_DNSPARSER_Record answer_records[rh->num_records];
586 packet->answers = answer_records;
588 len = sizeof(struct GNUNET_DNSPARSER_Record*);
589 for (i=0; i < rd_count; i++)
591 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
592 "Adding type %d to DNS response\n", rd[i].record_type);
593 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Name: %s\n", rh->name);
594 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "QName: %s\n", rh->query->name);
595 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Record %d/%d\n", i+1, rd_count);
596 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Record len %d\n", rd[i].data_size);
597 answer_records[i].name = rh->query->name;
598 answer_records[i].type = rd[i].record_type;
599 answer_records[i].data.raw.data_len = rd[i].data_size;
600 answer_records[i].data.raw.data = (char*)rd[i].data;
601 answer_records[i].expiration_time = rd[i].expiration;
602 answer_records[i].class = GNUNET_DNSPARSER_CLASS_INTERNET;//hmmn
603 //GNUNET_free(i->record); DO this later!
607 * FIXME how to handle auth, additional etc
608 * PKEY might be auth, != name,record_type additional
610 //packet->num_queries = 0;
611 packet->num_additional_records = 0;
612 packet->num_answers = rd_count; //answer->num_records;
613 //packet.num_authority_records = 0;//answer->num_authority_records;
615 dnsflags.authoritative_answer = 1;
616 dnsflags.message_truncated = 0;
617 dnsflags.recursion_desired = 0;
618 dnsflags.authenticated_data = 0;
619 dnsflags.checking_disabled = 1;
621 dnsflags.recursion_available = 0;
622 dnsflags.opcode = GNUNET_DNSPARSER_OPCODE_QUERY;
624 dnsflags.return_code = GNUNET_DNSPARSER_RETURN_CODE_NAME_ERROR;
626 dnsflags.return_code = GNUNET_DNSPARSER_RETURN_CODE_NO_ERROR;
627 dnsflags.query_or_response = 1;
628 packet->flags = dnsflags;
630 //FIXME this is silently discarded
631 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
632 "Building DNS response\n");
633 ret = GNUNET_DNSPARSER_pack (packet,
634 1024, /* FIXME magic from dns redirector */
637 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
638 "Built DNS response! (ret=%d,len=%d)\n", ret, len);
639 if (ret == GNUNET_OK)
641 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
642 "Answering DNS request\n");
643 GNUNET_DNS_request_answer(rh->request_handle,
646 //GNUNET_free(answer);
647 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Answered DNS request\n");
648 //FIXME return code, free datastructures
652 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
653 "Error building DNS response! (ret=%d)", ret);
656 //FIXME into free_resolver(rh)
657 //GNUNET_DNSPARSER_free_packet(rh->packet);
658 //GNUNET_free(rh->name);
664 * Namestore calls this function if we have an entry for this name.
665 * (or data=null to indicate the lookup has finished)
667 * @param cls the pending query
668 * @param zone the zone of the lookup
669 * @param name the name looked up
670 * @param record_type the record type
671 * @param expiration lifetime of the record
672 * @param flags record flags
673 * @param sig_loc location of the record in the signature tree
674 * @param size the size of the record
675 * @param data the record data
678 process_authoritative_result(void* cls,
679 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
680 struct GNUNET_TIME_Absolute expiration,
681 const char *name, unsigned int rd_count,
682 const struct GNUNET_NAMESTORE_RecordData *rd,
683 const struct GNUNET_CRYPTO_RsaSignature *signature)
685 struct GNUNET_GNS_ResolverHandle *rh;
686 struct GNUNET_GNS_QueryRecordList *qrecord;
687 struct GNUNET_NAMESTORE_RecordData *record;
688 struct GNUNET_TIME_Relative remaining_time;
689 GNUNET_HashCode zone;
691 rh = (struct GNUNET_GNS_ResolverHandle *) cls;
692 GNUNET_CRYPTO_hash(key, GNUNET_CRYPTO_RSA_KEY_LENGTH, &zone);
693 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
695 //FIXME Handle results in rd
701 * Lookup terminated and no results
702 * -> DHT Phase unless data is recent
704 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
705 "Namestore lookup for %s terminated without results\n", name);
708 * if this is not our zone we cannot rely on the namestore to be
709 * complete. -> Query DHT
711 if (!GNUNET_CRYPTO_hash_cmp(&zone, &zone_hash))
713 remaining_time = GNUNET_TIME_absolute_get_remaining (expiration);
714 if (remaining_time.rel_value == 0)
716 resolve_name_dht(rh, name);
721 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Record is still recent. No DHT lookup\n");
726 * Our zone and no result? Cannot resolve TT
727 * FIXME modify query to say NX
729 GNUNET_assert(rh->answered == 0);
730 reply_to_dns(rh, 0, NULL); //answered should be 0
739 * FIXME Check record expiration and dht expiration
740 * consult dht if necessary
742 if (remaining_time.rel_value == 0)
744 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
745 "This dht entry is old. Refreshing.\n");
746 resolve_name_dht(rh, name);
749 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
750 "Processing additional result %s from namestore\n", name);
752 for (i=0; i<rd_count;i++)
754 if ((GNUNET_TIME_absolute_get_remaining (rd[i].expiration)).rel_value
757 //FIXME there is a catch here...
758 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "This record is expired. Skipping\n");
761 // A time will come when this has to be freed
762 qrecord = GNUNET_malloc(sizeof(struct GNUNET_GNS_QueryRecordList));
763 record = GNUNET_malloc(sizeof(struct GNUNET_NAMESTORE_RecordData));
764 qrecord->record = record;
766 //fixme into gns_util
767 //parse_record(rd[i]->data, rd[i]->data_size, 0, record);
768 GNUNET_CONTAINER_DLL_insert(rh->records_head,
774 //we need to resolve to the original name in the end though...
775 //keep in mind. This can also be done later probably
776 //record->name = (char*)query->original_name;
779 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Found answer to query!\n");
782 reply_to_dns(rh, rd_count, rd);
787 * Determine if this name is canonical.
789 * a.b.gnunet = not canonical
792 * @param name the name to test
793 * @return 1 if canonical
796 is_canonical(char* name)
798 uint32_t len = strlen(name);
801 for (i=0; i<len; i++)
803 if (*(name+i) == '.')
810 * Move one level up in the domain hierarchy and return the
811 * passed top level domain.
812 * FIXME this needs a better name
814 * @param name the domain
817 char* pop_tld(char* name)
821 if (is_canonical(name))
824 for (len = strlen(name); len > 0; len--)
826 if (*(name+len) == '.')
833 name[len] = '\0'; //terminate string
840 * The first phase of resolution.
841 * First check if the name is canonical.
842 * If it is then try to resolve directly.
843 * If not then first have to resolve the authoritative entities.
845 * @param query the pending lookup
846 * @param zone the zone we are currently resolving in
849 resolve_name(struct GNUNET_GNS_ResolverHandle *rh, GNUNET_HashCode *zone)
851 if (is_canonical(rh->name))
853 //We only need to check this zone's ns
854 GNUNET_NAMESTORE_lookup_record(namestore_handle,
858 &process_authoritative_result,
863 //We have to resolve the authoritative entity
864 char *new_authority = pop_tld(rh->name);
865 GNUNET_NAMESTORE_lookup_record(namestore_handle,
868 GNUNET_GNS_RECORD_PKEY,
869 &process_authority_lookup,
875 * Entry point for name resolution
876 * Lookup local namestore of our zone.
878 * Setup a new query and try to resolve
880 * @param rh the request handle of the DNS request from a client
881 * @param p the DNS query packet we received
882 * @param name the name to look up
883 * @param id the id of the dns request (for the reply)
884 * @param type the record type to look for
887 start_resolution(struct GNUNET_DNS_RequestHandle *request,
888 struct GNUNET_DNSPARSER_Packet *p,
889 struct GNUNET_DNSPARSER_Query *q)
891 struct GNUNET_GNS_ResolverHandle *rh;
893 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Starting resolution for (%s)!\n",
896 rh = GNUNET_malloc(sizeof (struct GNUNET_GNS_ResolverHandle));
900 //FIXME do not forget to free!!
901 rh->name = GNUNET_malloc(strlen(q->name)
902 - strlen(gnunet_tld) + 1);
904 strlen(q->name)-strlen(gnunet_tld) + 1);
905 memcpy(rh->name, q->name,
906 strlen(q->name)-strlen(gnunet_tld));
908 rh->request_handle = request;
910 //Start resolution in our zone
911 resolve_name(rh, &zone_hash);
915 * The DNS request handler
916 * Called for every incoming DNS request.
919 * @param rh request handle to user for reply
920 * @param request_length number of bytes in request
921 * @param request udp payload of the DNS request
924 handle_dns_request(void *cls,
925 struct GNUNET_DNS_RequestHandle *rh,
926 size_t request_length,
929 struct GNUNET_DNSPARSER_Packet *p;
932 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Hijacked a DNS request...processing\n");
933 p = GNUNET_DNSPARSER_parse (request, request_length);
937 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
938 "Received malformed DNS packet, leaving it untouched\n");
939 GNUNET_DNS_request_forward (rh);
944 * Check tld and decide if we or
945 * legacy dns is responsible
947 * FIXME now in theory there could be more than 1 query in the request
948 * but if this is case we get into trouble:
949 * either we query the GNS or the DNS. We cannot do both!
950 * So I suggest to either only allow a single query per request or
951 * only allow GNS or DNS requests.
952 * The way it is implemented here now is buggy and will lead to erratic
953 * behaviour (if multiple queries are present).
955 if (p->num_queries == 0)
957 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
958 "No Queries in DNS packet... forwarding\n");
959 GNUNET_DNS_request_forward (rh);
962 if (p->num_queries > 1)
964 //Note: We could also look for .gnunet
965 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
966 ">1 queriy in DNS packet... odd. We only process #1\n");
970 tldoffset = p->queries[0].name + strlen(p->queries[0].name);
972 while ((*tldoffset) != '.')
975 if (0 == strcmp(tldoffset, gnunet_tld))
977 start_resolution(rh, p, p->queries);
982 * This request does not concern us. Forward to real DNS.
984 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
985 "Request for %s is forwarded to DNS\n", p->queries[0].name);
986 GNUNET_DNS_request_forward (rh);
992 * test function that stores some data in the namestore
995 put_some_records(void)
997 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Populating namestore\n");
998 /* put a few records into namestore */
999 char* ipA = "1.2.3.4";
1000 char* ipB = "5.6.7.8";
1001 struct GNUNET_CRYPTO_RsaPrivateKey *bob_key = GNUNET_CRYPTO_rsa_key_create (); struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *bob;
1002 bob = GNUNET_malloc(sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
1004 GNUNET_CRYPTO_rsa_key_get_public (bob_key, bob);
1006 GNUNET_HashCode *bob_zone = GNUNET_malloc(sizeof(GNUNET_HashCode));
1008 GNUNET_CRYPTO_hash(bob, GNUNET_CRYPTO_RSA_KEY_LENGTH, bob_zone);
1010 struct in_addr *alice = GNUNET_malloc(sizeof(struct in_addr));
1011 struct in_addr *bob_web = GNUNET_malloc(sizeof(struct in_addr));
1012 struct GNUNET_NAMESTORE_RecordData rda;
1013 struct GNUNET_NAMESTORE_RecordData rdb;
1014 struct GNUNET_NAMESTORE_RecordData rdb_web;
1016 GNUNET_assert(1 == inet_pton (AF_INET, ipA, alice));
1017 GNUNET_assert(1 == inet_pton (AF_INET, ipB, bob_web));
1019 rda.data_size = sizeof(struct in_addr);
1020 rdb_web.data_size = sizeof(struct in_addr);
1021 rdb.data_size = sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded);
1024 rdb_web.data = bob_web;
1025 rda.record_type = GNUNET_GNS_RECORD_TYPE_A;
1026 rdb_web.record_type = GNUNET_GNS_RECORD_TYPE_A;
1027 rdb.record_type = GNUNET_GNS_RECORD_PKEY;
1028 rdb_web.expiration = GNUNET_TIME_absolute_get_forever ();
1029 rda.expiration = GNUNET_TIME_absolute_get_forever ();
1030 rdb.expiration = GNUNET_TIME_absolute_get_forever ();
1032 //alice.gnunet A IN 1.2.3.4
1033 GNUNET_NAMESTORE_record_create (namestore_handle,
1040 //www.bob.gnunet A IN 5.6.7.8
1041 GNUNET_NAMESTORE_record_create (namestore_handle,
1047 GNUNET_NAMESTORE_record_put(namestore_handle,
1050 GNUNET_TIME_absolute_get_forever (),
1059 update_zone_dht_next(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1061 GNUNET_NAMESTORE_zone_iterator_next(namestore_iter);
1065 * Function used to put all records successively into the DHT.
1067 * @param cls the closure (NULL)
1068 * @param zone our root zone hash
1069 * @param name the name of the record
1070 * @param record_type the type of the record
1071 * @param expiration lifetime of the record
1072 * @param flags flags of the record
1073 * @param sig_loc location of record in signature tree
1074 * @param size size of the record
1075 * @param record_data the record data
1078 put_gns_record(void *cls,
1079 const const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *key,
1080 struct GNUNET_TIME_Absolute expiration,
1082 unsigned int rd_count,
1083 const struct GNUNET_NAMESTORE_RecordData *rd,
1084 const struct GNUNET_CRYPTO_RsaSignature *signature)
1086 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Putting records into the DHT\n");
1087 struct GNUNET_TIME_Relative timeout;
1088 GNUNET_HashCode name_hash;
1089 GNUNET_HashCode xor_hash;
1091 if (NULL == name) //We're done
1093 GNUNET_NAMESTORE_zone_iteration_stop (namestore_iter);
1097 * FIXME magic number 20 move to config file
1099 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 20);
1100 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
1101 GNUNET_CRYPTO_hash_xor(&zone_hash, &name_hash, &xor_hash);
1102 GNUNET_DHT_put (dht_handle, &xor_hash,
1103 5, //replication level
1105 GNUNET_BLOCK_TYPE_TEST, //FIXME todo block plugin
1110 NULL, //FIXME continuation needed? success check? yes ofc
1111 NULL); //cls for cont
1113 num_public_records++;
1116 * Reschedule periodic put
1118 GNUNET_SCHEDULER_add_delayed (dht_update_interval,
1119 &update_zone_dht_next,
1125 * Periodically iterate over our zone and store everything in dht
1128 * @param tc task context
1131 update_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1133 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Update zone!\n");
1134 dht_update_interval = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS,
1135 (3600/num_public_records));
1136 num_public_records = 0; //start counting again
1137 namestore_iter = GNUNET_NAMESTORE_zone_iteration_start (namestore_handle,
1139 GNUNET_NAMESTORE_RF_AUTHORITY,
1140 GNUNET_NAMESTORE_RF_PRIVATE,
1146 * Process GNS requests.
1148 * @param cls closure
1149 * @param server the initialized server
1150 * @param c configuration to use
1153 run (void *cls, struct GNUNET_SERVER_Handle *server,
1154 const struct GNUNET_CONFIGURATION_Handle *c)
1157 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Init GNS\n");
1158 zone_key = GNUNET_CRYPTO_rsa_key_create ();
1160 GNUNET_CRYPTO_hash(zone_key, GNUNET_CRYPTO_RSA_KEY_LENGTH,//FIXME is this ok?
1162 nc = GNUNET_SERVER_notification_context_create (server, 1);
1164 /* FIXME - do some config parsing
1165 * - Maybe only hijack dns if option is set (HIJACK_DNS=1)
1168 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
1171 * Do gnunet dns init here
1173 dns_handle = GNUNET_DNS_connect(c,
1174 GNUNET_DNS_FLAG_PRE_RESOLUTION,
1175 &handle_dns_request, /* rh */
1176 NULL); /* Closure */
1178 if (NULL == dns_handle)
1180 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1181 "Failed to connect to the dnsservice!\n");
1185 * handle to our local namestore
1187 namestore_handle = GNUNET_NAMESTORE_connect(c);
1189 if (NULL == namestore_handle)
1191 //FIXME do error handling;
1192 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1193 "Failed to connect to the namestore!\n");
1199 dht_handle = GNUNET_DHT_connect(c, 1); //FIXME get ht_len from cfg
1201 if (NULL == dht_handle)
1203 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not connect to DHT!\n");
1206 put_some_records(); //FIXME for testing
1209 * Schedule periodic put
1211 * We have roughly an hour for all records;
1213 dht_update_interval = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS,
1214 60); //FIXME from cfg
1215 GNUNET_SCHEDULER_add_delayed (dht_update_interval,
1216 &update_zone_dht_start,
1218 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "GNS Init done!\n");
1224 * The main function for the GNS service.
1226 * @param argc number of arguments from the command line
1227 * @param argv command line arguments
1228 * @return 0 ok, 1 on error
1231 main (int argc, char *const *argv)
1237 GNUNET_SERVICE_run (argc, argv, "gns", GNUNET_SERVICE_OPTION_NONE, &run,
1242 /* end of gnunet-service-gns.c */