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.
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_DNSPARSER_Record * 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;
105 struct GNUNET_TIME_Relative dht_update_interval;
108 * Our zone's private key
110 struct GNUNET_CRYPTO_RsaPrivateKey *zone_key;
113 * Our handle to the namestore service
115 struct GNUNET_NAMESTORE_Handle *namestore_handle;
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 * Task run during shutdown.
139 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
141 GNUNET_DNS_disconnect(dns_handle);
142 GNUNET_NAMESTORE_disconnect(namestore_handle, 0);
143 GNUNET_DHT_disconnect(dht_handle);
148 * This is where it gets tricky
149 * 1. we store (cache) all replies. Simple.
150 * 2. If we see an authority "closer" to the name
151 * we have to start a new query. Unless we get
153 * It is important that the authority is closer
154 * because else we might end up in an endless loop
155 * (maybe keep track of queried keys?)
156 * Of course we could just limit the resolution
157 * with a timeout (makes sense for clients) but we need
158 * to know when to stop querying.
159 * 3. Also the name returned for the record here will probably
160 * not match our name. How do we check this?
163 handle_dht_reply(void* cls,
164 struct GNUNET_TIME_Absolute exp,
165 const GNUNET_HashCode * key,
166 const struct GNUNET_PeerIdentity *get_path,
167 unsigned int get_path_length,
168 const struct GNUNET_PeerIdentity *put_path,
169 unsigned int put_path_length,
170 enum GNUNET_BLOCK_Type type,
171 size_t size, const void *data)
176 resolve_authority_dht(struct GNUNET_GNS_PendingQuery *query)
181 * This is a callback function that should give us only PKEY
182 * records. Used to iteratively query the namestore for 'closest'
185 * @param cls the pending query
186 * @param zone our zone hash
187 * @param name the name for which we need an authority
188 * @param record_type the type of record (PKEY)
189 * @param expiration expiration date of the record
190 * @param flags namestore record flags
191 * @param sig_loc the location of the record in the signature tree
192 * @param size the size of the record
193 * @param data the record data
196 process_authority_lookup(void* cls, const GNUNET_HashCode *zone,
197 const char *name, uint32_t record_type,
198 struct GNUNET_TIME_Absolute expiration,
199 enum GNUNET_NAMESTORE_RecordFlags flags,
200 const struct GNUNET_NAMESTORE_SignatureLocation *sig_loc,
201 size_t size, const void *data)
203 struct GNUNET_GNS_PendingQuery *query;
205 query = (struct GNUNET_GNS_PendingQuery *)cls;
208 * No authority found in namestore.
212 if (query->authority_found)
214 query->authority_found = 0;
215 //FIXME continue lookup
220 * We did not find an authority in the namestore
221 * _IF_ the current authoritative zone is not us. we can
223 * _ELSE_ we cannot resolve
225 if (GNUNET_CRYPTO_hash_cmp(zone, zone_hash))
227 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "NX record\n");
228 //FIXME return NX answer
232 resolve_authority_dht(query);
237 * We found an authority that may be able to help us
240 query->authority_found = 1;
241 GNUNET_HashCode *key = (GNUNET_HashCode*) data; //FIXME i assume this works
242 query->authority = key;
248 * Reply to client with the result from our lookup.
250 * @param answer the pending query used in the lookup
253 reply_to_dns(struct GNUNET_GNS_PendingQuery *answer)
255 struct GNUNET_GNS_QueryRecordList *i;
256 struct GNUNET_DNSPARSER_Packet *packet;
257 struct GNUNET_DNSPARSER_Flags dnsflags;
263 packet = GNUNET_malloc(sizeof(struct GNUNET_DNSPARSER_Packet));
265 GNUNET_malloc(sizeof(struct GNUNET_DNSPARSER_Record) * answer->num_records);
267 len = sizeof(struct GNUNET_DNSPARSER_Record*);
269 for (i=answer->records_head; i != NULL; i=i->next)
271 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
272 "Adding %s to DNS response\n", i->record->name);
273 memcpy(&packet->answers[j],
275 sizeof (struct GNUNET_DNSPARSER_Record));
276 GNUNET_free(i->record);
279 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "after memcpy\n");
280 /* FIXME how to handle auth, additional etc */
281 packet->num_answers = answer->num_records;
282 packet->num_authority_records = answer->num_authority_records;
284 dnsflags.authoritative_answer = 1;
285 dnsflags.opcode = GNUNET_DNSPARSER_OPCODE_QUERY;
286 dnsflags.return_code = GNUNET_DNSPARSER_RETURN_CODE_NO_ERROR; //not sure
287 dnsflags.query_or_response = 1;
288 packet->flags = dnsflags;
290 packet->id = answer->id;
292 //FIXME this is silently discarded
293 ret = GNUNET_DNSPARSER_pack (packet,
294 1024, /* FIXME magic from dns redirector */
297 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
298 "Built DNS response! (ret=%d)\n", ret);
299 if (ret == GNUNET_OK)
301 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
302 "Answering DNS request\n");
303 GNUNET_DNS_request_answer(answer->request_handle,
307 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Answered DNS request\n");
308 //FIXME return code, free datastructures
312 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
313 "Error building DNS response! (ret=%d)", ret);
318 resolve_name_dht(struct GNUNET_GNS_PendingQuery *query)
323 * Namestore calls this function if we have an entry for this name.
324 * (or data=null to indicate the lookup has finished)
326 * @param cls the pending query
327 * @param zone the zone of the lookup
328 * @param name the name looked up
329 * @param record_type the record type
330 * @param expiration lifetime of the record
331 * @param flags record flags
332 * @param sig_loc location of the record in the signature tree
333 * @param size the size of the record
334 * @param data the record data
337 process_authoritative_result(void* cls, const GNUNET_HashCode *zone,
338 const char *name, uint32_t record_type,
339 struct GNUNET_TIME_Absolute expiration,
340 enum GNUNET_NAMESTORE_RecordFlags flags,
341 const struct GNUNET_NAMESTORE_SignatureLocation *sig_loc,
342 size_t size, const void *data)
344 struct GNUNET_GNS_PendingQuery *query;
345 struct GNUNET_GNS_QueryRecordList *qrecord;
346 struct GNUNET_DNSPARSER_Record *record;
347 query = (struct GNUNET_GNS_PendingQuery *) cls;
355 * Do we have what we need to answer?
356 * If not -> DHT Phase
357 * if full_name == next_name and not anwered we cannot resolve
359 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
360 "Namestore lookup terminated. (answered=%d)", query->answered);
368 * if this is not our zone we cannot rely on the namestore to be
369 * complete. -> Query DHT
371 if (!GNUNET_CRYPTO_hash_cmp(zone, zone_hash))
374 resolve_name_dht(query);
379 * Our zone and no result? Cannot resolve TT
380 * FIXME modify query to say NX
390 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
391 "Processing additional result for %s from namestore\n", name);
393 qrecord = GNUNET_malloc(sizeof(struct GNUNET_GNS_QueryRecordList));
394 record = GNUNET_malloc(sizeof(struct GNUNET_DNSPARSER_Record));
395 qrecord->record = record;
397 record->name = (char*)query->original_name;
400 * FIXME for gns records this requires the dnsparser to be modified!
401 * or use RAW. But RAW data need serialization!
402 * maybe store record data appropriately in namestore to avoid a
403 * huge switch statement?
405 if (record_type == GNUNET_DNSPARSER_TYPE_A)
407 record->data.raw.data = (char*)data;
408 record->data.raw.data_len = size;
410 record->expiration_time = expiration;
411 record->type = record_type;
412 record->class = GNUNET_DNSPARSER_CLASS_INTERNET; /* srsly? */
414 //FIXME authoritative answer if we find a result in namestore
415 if (flags == GNUNET_NAMESTORE_RF_AUTHORITY)
417 //query->num_authority_records++;
421 * This seems to take into account that the result could
422 * be different in name and or record type...
423 * but to me this does not make sense
425 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Found answer to query!\n");
428 query->num_records++;
431 * FIXME watch for leaks
432 * properly free pendingquery when the time comes
434 GNUNET_CONTAINER_DLL_insert(query->records_head,
441 is_canonical(char* name)
446 char* move_up(char* name)
452 resolve_name(struct GNUNET_GNS_PendingQuery *query, GNUNET_HashCode *zone)
454 if (is_canonical(query->name))
456 //We only need to check this zone's ns
457 GNUNET_NAMESTORE_lookup_name(namestore_handle,
461 &process_authoritative_result,
466 //We have to resolve the authoritative entity
467 char *new_authority = move_up(query->name);
468 GNUNET_NAMESTORE_lookup_name(namestore_handle,
471 GNUNET_GNS_RECORD_PKEY,
472 &process_authority_lookup,
478 * Phase 1 of name resolution
479 * Lookup local namestore. If we find a match there we can
480 * provide an authoritative answer without the dht.
481 * If we don't we have to start querying the dht.
483 * FIXME now it is possible that we have a foreign zone (or even the result)
484 * cached in our namestore. Look up as well? We need a list of cached zones
487 * @param rh the request handle of the DNS request from a client
488 * @param name the name to look up
489 * @param id the id of the dns request (for the reply)
490 * @param type the record type to look for
493 start_resolution(struct GNUNET_DNS_RequestHandle *rh,
494 char* name, uint16_t id, uint16_t type)
496 struct GNUNET_GNS_PendingQuery *query;
498 //FIXME remove .gnunet here from name
499 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "This is .gnunet (%s)!\n", name);
500 query = GNUNET_malloc(sizeof (struct GNUNET_GNS_PendingQuery));
502 query->original_name = name; //Full name of original query
503 query->name = name; // FIXME without tld
505 query->request_handle = rh;
507 //Start resolution in our zone
508 resolve_name(query, zone_hash);
512 * The DNS request handler
513 * Called for every incoming DNS request.
516 * @param rh request handle to user for reply
517 * @param request_length number of bytes in request
518 * @param request udp payload of the DNS request
521 handle_dns_request(void *cls,
522 struct GNUNET_DNS_RequestHandle *rh,
523 size_t request_length,
526 struct GNUNET_DNSPARSER_Packet *p;
530 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Hijacked a DNS request...processing\n");
531 p = GNUNET_DNSPARSER_parse (request, request_length);
535 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
536 "Received malformed DNS packet, leaving it untouched\n");
537 GNUNET_DNS_request_forward (rh);
542 * Check tld and decide if we or
543 * legacy dns is responsible
545 * FIXME now in theory there could be more than 1 query in the request
546 * but if this is case we get into trouble:
547 * either we query the GNS or the DNS. We cannot do both!
548 * So I suggest to either only allow a single query per request or
549 * only allow GNS or DNS requests.
550 * The way it is implemented here now is buggy and will lead to erratic
551 * behaviour (if multiple queries are present).
553 for (i=0;i<p->num_queries;i++)
555 tldoffset = p->queries[i].name + strlen(p->queries[i].name);
557 while ((*tldoffset) != '.')
561 * FIXME Move our tld/root to config file
563 if (0 == strcmp(tldoffset, ".gnunet"))
565 start_resolution(rh, p->queries[i].name, p->id, p->queries[i].type);
570 * This request does not concern us. Forward to real DNS.
572 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
573 "Request for %s is forwarded to DNS\n", p->queries[i].name);
574 GNUNET_DNS_request_forward (rh);
580 * test function that stores some data in the namestore
583 put_some_records(void)
585 /* put a few records into namestore */
586 char* ipA = "1.2.3.4";
587 char* ipB = "5.6.7.8";
588 struct in_addr *alice = GNUNET_malloc(sizeof(struct in_addr));
589 struct in_addr *bob = GNUNET_malloc(sizeof(struct in_addr));
590 GNUNET_assert(1 == inet_pton (AF_INET, ipA, alice));
591 GNUNET_assert(1 == inet_pton (AF_INET, ipB, bob));
592 GNUNET_NAMESTORE_record_put (namestore_handle,
595 GNUNET_GNS_RECORD_TYPE_A,
596 GNUNET_TIME_absolute_get_forever(),
597 GNUNET_NAMESTORE_RF_AUTHORITY,
599 sizeof(struct in_addr),
603 GNUNET_NAMESTORE_record_put (namestore_handle,
606 GNUNET_GNS_RECORD_TYPE_A,
607 GNUNET_TIME_absolute_get_forever(),
608 GNUNET_NAMESTORE_RF_AUTHORITY,
610 sizeof(struct in_addr),
616 //Prototype... needed in put function
618 update_zone_dht(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
621 * Function used to put all records successively into the DHT.
622 * FIXME also serializes records. maybe do this somewhere else...
623 * FIXME don't store private records (maybe zone transfer does this)
625 * @param cls the closure (NULL)
626 * @param zone our root zone hash
627 * @param name the name of the record
628 * @param record_type the type of the record
629 * @param expiration lifetime of the record
630 * @param flags flags of the record
631 * @param sig_loc location of record in signature tree
632 * @param size size of the record
633 * @param record_data the record data
636 put_gns_record(void *cls, const GNUNET_HashCode *zone, const char *name,
637 uint32_t record_type, struct GNUNET_TIME_Absolute expiration,
638 enum GNUNET_NAMESTORE_RecordFlags flags,
639 const struct GNUNET_NAMESTORE_SignatureLocation *sig_loc,
640 size_t size, const void *record_data)
642 struct GNUNET_TIME_Relative timeout;
646 struct GNUNET_TIME_AbsoluteNBO exp_nbo;
647 exp_nbo = GNUNET_TIME_absolute_hton (expiration);
648 uint32_t namelen = htonl(strlen(name));
649 uint16_t flags_nbo = htons(flags);
650 uint64_t offset = GNUNET_htonll(sig_loc->offset);
651 uint32_t depth = htonl(sig_loc->depth);
652 uint32_t revision = htonl(sig_loc->revision);
653 GNUNET_HashCode name_hash;
654 GNUNET_HashCode xor_hash;
657 * I guess this can be done prettier
658 * FIXME extract into function, maybe even into different file
660 size_t record_len = sizeof(size_t) + sizeof(uint32_t) +
662 sizeof(struct GNUNET_NAMESTORE_SignatureLocation) +
663 sizeof(uint32_t) + strlen(name) + size;
665 record_type = htonl(record_type);
667 data = GNUNET_malloc(record_len);
671 memcpy(data_ptr, &namelen, sizeof(size_t));
672 data_ptr += sizeof(size_t);
674 memcpy(data_ptr, name, namelen);
677 memcpy(data_ptr, &record_type, sizeof(uint32_t));
678 data_ptr += sizeof(uint32_t);
680 memcpy(data_ptr, &exp_nbo, sizeof(struct GNUNET_TIME_AbsoluteNBO));
681 data_ptr += sizeof(struct GNUNET_TIME_AbsoluteNBO);
683 memcpy(data_ptr, &flags_nbo, sizeof(uint16_t));
684 data_ptr += sizeof(uint16_t);
686 memcpy(data_ptr, &offset, sizeof(uint64_t));
687 data_ptr += sizeof(uint64_t);
689 memcpy(data_ptr, &depth, sizeof(uint32_t));
690 data_ptr += sizeof(uint32_t);
692 memcpy(data_ptr, &revision, sizeof(uint32_t));
693 data_ptr += sizeof(uint32_t);
695 memcpy(data_ptr, &size, sizeof(uint32_t));
696 data_ptr += sizeof(uint32_t);
699 * FIXME note that this only works with raw data in nbo
700 * write helper function that converts properly and returns buffer
702 memcpy(data_ptr, record_data, size);
704 /*Doing this made me sad...*/
707 * FIXME magic number 20 move to config file
709 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 20);
711 GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
712 GNUNET_CRYPTO_hash_xor(zone_hash, &name_hash, &xor_hash);
713 GNUNET_DHT_put (dht_handle, &xor_hash,
714 5, //replication level
716 GNUNET_BLOCK_TYPE_TEST, //FIXME todo block plugin
719 expiration, //FIXME from record makes sense? is absolute?
721 NULL, //FIXME continuation needed? success check? yes ofc
722 NULL); //cls for cont
725 * Reschedule periodic put
727 GNUNET_SCHEDULER_add_delayed (dht_update_interval,
734 * Periodically iterate over our zone and store everything in dht
737 * @param tc task context
740 update_zone_dht(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
742 GNUNET_NAMESTORE_zone_transfer (namestore_handle, zone_hash,
748 * Process GNS requests.
751 * @param server the initialized server
752 * @param c configuration to use
755 run (void *cls, struct GNUNET_SERVER_Handle *server,
756 const struct GNUNET_CONFIGURATION_Handle *c)
759 zone_key = GNUNET_CRYPTO_rsa_key_create ();
760 GNUNET_CRYPTO_hash(zone_key, GNUNET_CRYPTO_RSA_KEY_LENGTH,//FIXME is this ok?
763 nc = GNUNET_SERVER_notification_context_create (server, 1);
765 /* FIXME - do some config parsing
766 * - Maybe only hijack dns if option is set (HIJACK_DNS=1)
769 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
772 * Do gnunet dns init here
774 dns_handle = GNUNET_DNS_connect(c,
775 GNUNET_DNS_FLAG_PRE_RESOLUTION,
776 &handle_dns_request, /* rh */
779 if (NULL == dns_handle)
781 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
782 "Failed to connect to the dnsservice!\n");
786 * handle to our local namestore
788 namestore_handle = GNUNET_NAMESTORE_connect(c);
790 if (NULL == namestore_handle)
792 //FIXME do error handling;
793 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
794 "Failed to connect to the namestore!\n");
800 dht_handle = GNUNET_DHT_connect(c, 1); //FIXME get ht_len from cfg
802 if (NULL == dht_handle)
804 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not connect to DHT!\n");
807 put_some_records(); //FIXME for testing
810 * Schedule periodic put
813 dht_update_interval = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS,
814 60); //FIXME from cfg
815 GNUNET_SCHEDULER_add_delayed (dht_update_interval,
823 * The main function for the GNS service.
825 * @param argc number of arguments from the command line
826 * @param argv command line arguments
827 * @return 0 ok, 1 on error
830 main (int argc, char *const *argv)
836 GNUNET_SERVICE_run (argc, argv, "gns", GNUNET_SERVICE_OPTION_NONE, &run,
841 /* end of gnunet-service-gns.c */