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.
22 * @file gns/gnunet-service-gns.c
23 * @brief GNUnet GNS service
24 * @author Martin Schanzenbach
27 #include "gnunet_util_lib.h"
28 #include "gnunet_transport_service.h"
29 #include "gnunet_dns_service.h"
30 #include "gnunet_dnsparser_lib.h"
31 #include "gnunet_dht_service.h"
32 #include "gnunet_namestore_service.h"
33 #include "gnunet_gns_service.h"
37 /* TODO into gnunet_protocols */
38 #define GNUNET_MESSAGE_TYPE_GNS_CLIENT_LOOKUP 23
39 #define GNUNET_MESSAGE_TYPE_GNS_CLIENT_RESULT 24
41 struct GNUNET_GNS_QueryRecordList
46 struct GNUNET_GNS_QueryRecordList * next;
47 struct GNUNET_GNS_QueryRecordList * prev;
49 struct GNUNET_DNSPARSER_Record * record;
53 * A result list for namestore queries
55 struct GNUNET_GNS_PendingQuery
57 /* the answer packet */
58 struct GNUNET_DNSPARSER_Packet *answer;
60 /* records to put into answer packet */
61 struct GNUNET_GNS_QueryRecordList * records_head;
62 struct GNUNET_GNS_QueryRecordList * records_tail;
65 int num_authority_records; //FIXME are all of our replies auth?
69 /* the dns request id */
70 int id; // FIXME can handle->request_id also be used here?
72 /* the request handle to reply to */
73 struct GNUNET_DNS_RequestHandle *request_handle;
75 /* hast this query been answered? */
78 /* we have an authority in namestore that
79 * may be able to resolve
86 * Our handle to the DNS handler library
88 struct GNUNET_DNS_Handle *dns_handle;
91 * Our handle to the DHT
93 struct GNUNET_DHT_Handle *dht_handle;
95 struct GNUNET_TIME_Relative dht_update_interval;
99 * FIXME get the real deal
101 struct GNUNET_CRYPTO_RsaPrivateKey *zone_key;
104 * Our handle to the namestore service
106 struct GNUNET_NAMESTORE_Handle *namestore_handle;
109 * The configuration the GNS service is running with
111 const struct GNUNET_CONFIGURATION_Handle *GNS_cfg;
114 * Our notification context.
116 static struct GNUNET_SERVER_NotificationContext *nc;
121 GNUNET_HashCode *zone_hash;
124 * Task run during shutdown.
130 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
132 GNUNET_DNS_disconnect(dns_handle);
133 GNUNET_NAMESTORE_disconnect(namestore_handle, 0);
134 GNUNET_DHT_disconnect(dht_handle);
138 handle_dht_reply(void* cls,
139 struct GNUNET_TIME_Absolute exp,
140 const GNUNET_HashCode * key,
141 const struct GNUNET_PeerIdentity *get_path,
142 unsigned int get_path_length,
143 const struct GNUNET_PeerIdentity *put_path,
144 unsigned int put_path_length,
145 enum GNUNET_BLOCK_Type type,
146 size_t size, const void *data)
151 process_auth_query(void* cls, const GNUNET_HashCode *zone,
152 const char *name, uint32_t record_type,
153 struct GNUNET_TIME_Absolute expiration,
154 enum GNUNET_NAMESTORE_RecordFlags flags,
155 const struct GNUNET_NAMESTORE_SignatureLocation *sig_loc,
156 size_t size, const void *data)
158 struct GNUNET_GNS_PendingQuery *query;
160 query = (struct GNUNET_GNS_PendingQuery *)cls;
164 * FIXME We assume this will never return our authority
166 if ((NULL == data) && !query->authority_found)
168 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Find authority\n");
169 if (0 == strcmp(name, "gnunet"))
171 // Reached tld return nx
172 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "NX record\n");
176 //Move name to next level
177 while ((*name) != '.')
181 GNUNET_NAMESTORE_lookup_name(namestore_handle,
184 GNUNET_GNS_RECORD_PKEY,
191 * We found a PKEY that may be able to help us
193 GNUNET_HashCode *key = (GNUNET_HashCode*) data;
196 struct GNUNET_TIME_Relative timeout =
197 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 20);
198 GNUNET_DHT_get_start (dht_handle,
200 GNUNET_BLOCK_TYPE_TEST, //TODO our plugin
204 query->name, //xquery FIXME nobody will know this name
214 * Phase 2 of resolution.
217 lookup_dht(struct GNUNET_GNS_PendingQuery *query)
220 * In this phase we first want to find the
221 * responsible authority in the namestore
223 GNUNET_NAMESTORE_lookup_name(namestore_handle,
226 GNUNET_GNS_RECORD_PKEY,
232 reply_to_dns(struct GNUNET_GNS_PendingQuery *answer)
234 struct GNUNET_GNS_QueryRecordList *i;
235 struct GNUNET_DNSPARSER_Packet *packet;
236 struct GNUNET_DNSPARSER_Flags dnsflags;
242 packet = GNUNET_malloc(sizeof(struct GNUNET_DNSPARSER_Packet));
244 GNUNET_malloc(sizeof(struct GNUNET_DNSPARSER_Record) * answer->num_records);
246 len = sizeof(struct GNUNET_DNSPARSER_Record*);
248 for (i=answer->records_head; i != NULL; i=i->next)
250 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
251 "Adding %s to DNS response\n", i->record->name);
252 memcpy(&packet->answers[j],
254 sizeof (struct GNUNET_DNSPARSER_Record));
255 GNUNET_free(i->record);
258 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "after memcpy\n");
259 /* FIXME how to handle auth, additional etc */
260 packet->num_answers = answer->num_records;
261 packet->num_authority_records = answer->num_authority_records;
263 dnsflags.authoritative_answer = 1;
264 dnsflags.opcode = GNUNET_DNSPARSER_OPCODE_QUERY;
265 dnsflags.return_code = GNUNET_DNSPARSER_RETURN_CODE_NO_ERROR; //not sure
266 dnsflags.query_or_response = 1;
267 packet->flags = dnsflags;
269 packet->id = answer->id;
270 ret = GNUNET_DNSPARSER_pack (packet,
271 1024, /* FIXME magic from dns redirector */
274 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
275 "Built DNS response! (ret=%d)\n", ret);
276 if (ret == GNUNET_OK)
278 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
279 "Answering DNS request\n");
280 GNUNET_DNS_request_answer(answer->request_handle,
284 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Answered DNS request\n");
285 //FIXME return code, free datastructures
289 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
290 "Error building DNS response! (ret=%d)", ret);
295 process_ns_result(void* cls, const GNUNET_HashCode *zone,
296 const char *name, uint32_t record_type,
297 struct GNUNET_TIME_Absolute expiration,
298 enum GNUNET_NAMESTORE_RecordFlags flags,
299 const struct GNUNET_NAMESTORE_SignatureLocation *sig_loc,
300 size_t size, const void *data)
302 struct GNUNET_GNS_PendingQuery *query;
303 struct GNUNET_GNS_QueryRecordList *qrecord;
304 struct GNUNET_DNSPARSER_Record *record;
305 query = (struct GNUNET_GNS_PendingQuery *) cls;
311 * Last result received (or none)
312 * Do we have what we need to answer?
313 * If not -> DHT Phase
316 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
317 "Namestore lookup terminated. (answered=%d)", query->answered);
322 lookup_dht(query); //TODO
330 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
331 "Processing additional result %s from namestore\n", name);
333 qrecord = GNUNET_malloc(sizeof(struct GNUNET_GNS_QueryRecordList));
334 record = GNUNET_malloc(sizeof(struct GNUNET_DNSPARSER_Record));
335 qrecord->record = record;
337 record->name = (char*)name;
338 /* FIXME for gns records this requires the dnsparser to be modified!
340 * maybe store record data appropriately in namestore to avoid this
343 if (record_type == GNUNET_DNSPARSER_TYPE_A)
345 record->data.raw.data = (char*)data;
346 record->data.raw.data_len = size;
348 record->expiration_time = expiration;
349 record->type = record_type;
350 record->class = GNUNET_DNSPARSER_CLASS_INTERNET; /* srsly? */
352 if (flags == GNUNET_NAMESTORE_RF_AUTHORITY)
354 //query->num_authority_records++;
357 if ((0 == strcmp(query->name , name)) &&
358 (query->type == record_type))
360 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Found answer to query!\n");
364 query->num_records++;
366 //FIXME watch for leaks
367 GNUNET_CONTAINER_DLL_insert(query->records_head,
375 * Phase 1 of name resolution: Lookup local namestore
377 * @param name the name to look up
378 * @param id the id of the dns request (for the reply)
379 * @param type the record type to look for
382 lookup_namestore(struct GNUNET_DNS_RequestHandle *rh,
383 char* name, uint16_t id, uint16_t type)
385 struct GNUNET_GNS_PendingQuery *answer;
388 * Do db lookup here. Make dht lookup if necessary
389 * FIXME for now only local lookups for our zone!
391 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "This is .gnunet (%s)!\n", name);
392 answer = GNUNET_malloc(sizeof (struct GNUNET_GNS_PendingQuery));
396 answer->request_handle = rh;
398 GNUNET_NAMESTORE_lookup_name(namestore_handle,
407 * The DNS request handler
410 * @param rh request handle to user for reply
411 * @param request_length number of bytes in request
412 * @param request udp payload of the DNS request
415 handle_dns_request(void *cls,
416 struct GNUNET_DNS_RequestHandle *rh,
417 size_t request_length,
421 * parse request for tld
423 struct GNUNET_DNSPARSER_Packet *p;
428 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "hijacked a request...processing\n");
429 p = GNUNET_DNSPARSER_parse (request, request_length);
433 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
434 "Received malformed DNS packet, leaving it untouched\n");
435 GNUNET_DNS_request_forward (rh);
440 * Check tld and decide if we or
441 * legacy dns is responsible
443 for (i=0;i<p->num_queries;i++)
445 namelen = strlen(p->queries[i].name);
447 if (namelen < 7) /* this can't be .gnunet */
450 * Move our tld/root to config file
451 * Generate fake DNS reply that replaces .gnunet with .org for testing?
453 tail = p->queries[i].name+(namelen-7);
454 if (0 == strcmp(tail, ".gnunet"))
456 /* FIXME we need to answer to ALL queries in ONE response...
457 * What happens if some requests should be handled by us and
459 * Like this we only answer one...
461 lookup_namestore(rh, p->queries[i].name, p->id, p->queries[i].type);
466 * This request does not concern us. Forward to real DNS.
468 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
469 "Request for %s is forwarded to DNS\n", p->queries[i].name);
470 GNUNET_DNS_request_forward (rh);
477 handle_client_record_lookup(void *cls,
478 struct GNUNET_SERVER_Client *client,
479 const struct GNUNET_MessageHeader *message)
487 put_some_records(void)
489 /* put a few records into namestore */
490 char* ipA = "1.2.3.4";
491 char* ipB = "5.6.7.8";
492 struct in_addr *alice = GNUNET_malloc(sizeof(struct in_addr));
493 struct in_addr *bob = GNUNET_malloc(sizeof(struct in_addr));
494 GNUNET_assert(1 == inet_pton (AF_INET, ipA, alice));
495 GNUNET_assert(1 == inet_pton (AF_INET, ipB, bob));
496 GNUNET_NAMESTORE_record_put (namestore_handle,
499 GNUNET_GNS_RECORD_TYPE_A,
500 GNUNET_TIME_absolute_get_forever(),
501 GNUNET_NAMESTORE_RF_AUTHORITY,
503 sizeof(struct in_addr),
507 GNUNET_NAMESTORE_record_put (namestore_handle,
510 GNUNET_GNS_RECORD_TYPE_A,
511 GNUNET_TIME_absolute_get_forever(),
512 GNUNET_NAMESTORE_RF_AUTHORITY,
514 sizeof(struct in_addr),
520 //Prototype... needed in put function
522 update_zone_dht(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
525 put_gns_record(void *cls, const GNUNET_HashCode *zone, const char *name,
526 uint32_t record_type, struct GNUNET_TIME_Absolute expiration,
527 enum GNUNET_NAMESTORE_RecordFlags flags,
528 const struct GNUNET_NAMESTORE_SignatureLocation *sig_loc,
529 size_t size, const void *record_data)
531 struct GNUNET_TIME_Relative timeout;
535 struct GNUNET_TIME_AbsoluteNBO exp_nbo;
536 exp_nbo = GNUNET_TIME_absolute_hton (expiration);
537 uint32_t namelen = htonl(strlen(name));
538 uint16_t flags_nbo = htons(flags);
539 uint64_t offset = GNUNET_htonll(sig_loc->offset);
540 uint32_t depth = htonl(sig_loc->depth);
541 uint32_t revision = htonl(sig_loc->revision);
544 * I guess this can be done prettier
546 size_t record_len = sizeof(size_t) + sizeof(uint32_t) +
548 sizeof(struct GNUNET_NAMESTORE_SignatureLocation) +
549 sizeof(uint32_t) + strlen(name) + size;
551 record_type = htonl(record_type);
553 data = GNUNET_malloc(record_len);
557 memcpy(data_ptr, &namelen, sizeof(size_t));
558 data_ptr += sizeof(size_t);
560 memcpy(data_ptr, name, namelen);
563 memcpy(data_ptr, &record_type, sizeof(uint32_t));
564 data_ptr += sizeof(uint32_t);
566 memcpy(data_ptr, &exp_nbo, sizeof(struct GNUNET_TIME_AbsoluteNBO));
567 data_ptr += sizeof(struct GNUNET_TIME_AbsoluteNBO);
569 memcpy(data_ptr, &flags_nbo, sizeof(uint16_t));
570 data_ptr += sizeof(uint16_t);
572 memcpy(data_ptr, &offset, sizeof(uint64_t));
573 data_ptr += sizeof(uint64_t);
575 memcpy(data_ptr, &depth, sizeof(uint32_t));
576 data_ptr += sizeof(uint32_t);
578 memcpy(data_ptr, &revision, sizeof(uint32_t));
579 data_ptr += sizeof(uint32_t);
581 memcpy(data_ptr, &size, sizeof(uint32_t));
582 data_ptr += sizeof(uint32_t);
585 * FIXME note that this only works with raw data in nbo
586 * write helper function that converts properly and returns buffer
588 memcpy(data_ptr, record_data, size);
590 /*Doing this made me sad...*/
595 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 20);
597 GNUNET_DHT_put (dht_handle, zone_hash,
600 GNUNET_BLOCK_TYPE_TEST, //FIXME todo
603 expiration, //FIXME from record makes sense?
605 NULL, //FIXME continuation needed? success check?
606 NULL); //cls for cont
611 GNUNET_SCHEDULER_add_delayed (dht_update_interval,
618 * Periodically iterate over our zone and store everything in dht
621 update_zone_dht(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
623 GNUNET_NAMESTORE_zone_transfer (namestore_handle, zone_hash,
629 * Process GNS requests.
632 * @param server the initialized server
633 * @param c configuration to use
636 run (void *cls, struct GNUNET_SERVER_Handle *server,
637 const struct GNUNET_CONFIGURATION_Handle *c)
640 /* The IPC message types */
641 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
642 /* callback, cls, type, size */
643 {&handle_client_record_lookup, NULL, GNUNET_MESSAGE_TYPE_GNS_CLIENT_LOOKUP,
648 zone_key = GNUNET_CRYPTO_rsa_key_create ();
649 GNUNET_CRYPTO_hash(zone_key, GNUNET_CRYPTO_RSA_KEY_LENGTH,//FIXME is this ok?
652 nc = GNUNET_SERVER_notification_context_create (server, 1);
654 /* FIXME - do some config parsing
655 * - Maybe only hijack dns if option is set (HIJACK_DNS=1)
658 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
661 * Do gnunet dns init here
663 dns_handle = GNUNET_DNS_connect(c,
664 GNUNET_DNS_FLAG_PRE_RESOLUTION,
665 &handle_dns_request, /* rh */
668 if (NULL == dns_handle)
670 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
671 "Failed to connect to the dnsservice!\n");
675 * handle to our local namestore
677 namestore_handle = GNUNET_NAMESTORE_connect(c);
679 if (NULL == namestore_handle)
681 //FIXME do error handling;
682 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
683 "Failed to connect to the namestore!\n");
689 dht_handle = GNUNET_DHT_connect(c, 1); //FIXME get ht_len from cfg
691 if (NULL == dht_handle)
693 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not connect to DHT!\n");
696 dht_update_interval = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS,
697 60); //FIXME from cfg
698 GNUNET_SCHEDULER_add_delayed (dht_update_interval,
704 GNUNET_SERVER_add_handlers (server, handlers);
706 * Esp the lookup would require to keep track of the clients' context
708 * GNUNET_SERVER_disconnect_notify (server, &client_disconnect, NULL);
714 * The main function for the GNS service.
716 * @param argc number of arguments from the command line
717 * @param argv command line arguments
718 * @return 0 ok, 1 on error
721 main (int argc, char *const *argv)
727 GNUNET_SERVICE_run (argc, argv, "gns", GNUNET_SERVICE_OPTION_NONE, &run,
732 /* end of gnunet-service-gns.c */