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? */
81 * Our handle to the DNS handler library
83 struct GNUNET_DNS_Handle *dns_handle;
86 * Our handle to the DHT
88 struct GNUNET_DHT_Handle *dht_handle;
91 * Our handle to the namestore service
93 struct GNUNET_NAMESTORE_Handle *namestore_handle;
96 * The configuration the GNS service is running with
98 const struct GNUNET_CONFIGURATION_Handle *GNS_cfg;
101 * Our notification context.
103 static struct GNUNET_SERVER_NotificationContext *nc;
108 const GNUNET_HashCode *my_zone;
111 * Task run during shutdown.
117 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
119 GNUNET_DNS_disconnect(dns_handle);
120 GNUNET_NAMESTORE_disconnect(namestore_handle, 0);
124 * Phase 2 of resolution.
127 lookup_dht(struct GNUNET_GNS_PendingQuery *answer)
132 reply_to_dns(struct GNUNET_GNS_PendingQuery *answer)
134 struct GNUNET_GNS_QueryRecordList *i;
135 struct GNUNET_DNSPARSER_Packet *packet;
136 struct GNUNET_DNSPARSER_Flags dnsflags;
142 packet = GNUNET_malloc(sizeof(struct GNUNET_DNSPARSER_Packet));
144 GNUNET_malloc(sizeof(struct GNUNET_DNSPARSER_Record) * answer->num_records);
146 len = sizeof(struct GNUNET_DNSPARSER_Record*);
148 for (i=answer->records_head; i != NULL; i=i->next)
150 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
151 "Adding %s to DNS response\n", i->record->name);
152 memcpy(&packet->answers[j],
154 sizeof (struct GNUNET_DNSPARSER_Record));
155 GNUNET_free(i->record);
158 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "after memcpy\n");
159 /* FIXME how to handle auth, additional etc */
160 packet->num_answers = answer->num_records;
161 packet->num_authority_records = answer->num_authority_records;
163 dnsflags.authoritative_answer = 1;
164 dnsflags.opcode = GNUNET_DNSPARSER_OPCODE_QUERY;
165 dnsflags.return_code = GNUNET_DNSPARSER_RETURN_CODE_NO_ERROR; //not sure
166 dnsflags.query_or_response = 1;
167 packet->flags = dnsflags;
169 packet->id = answer->id;
170 ret = GNUNET_DNSPARSER_pack (packet,
171 1024, /* FIXME magic from dns redirector */
174 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
175 "Built DNS response! (ret=%d)\n", ret);
176 if (ret == GNUNET_OK)
178 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
179 "Answering DNS request\n");
180 GNUNET_DNS_request_answer(answer->request_handle,
184 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Answered DNS request\n");
185 //FIXME return code, free datastructures
189 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
190 "Error building DNS response! (ret=%d)", ret);
195 process_ns_result(void* cls, const GNUNET_HashCode *zone,
196 const char *name, uint32_t record_type,
197 struct GNUNET_TIME_Absolute expiration,
198 enum GNUNET_NAMESTORE_RecordFlags flags,
199 const struct GNUNET_NAMESTORE_SignatureLocation *sig_loc,
200 size_t size, const void *data)
202 struct GNUNET_GNS_PendingQuery *query;
203 struct GNUNET_GNS_QueryRecordList *qrecord;
204 struct GNUNET_DNSPARSER_Record *record;
205 query = (struct GNUNET_GNS_PendingQuery *) cls;
211 * Last result received (or none)
212 * Do we have what we need to answer?
213 * If not -> DHT Phase
216 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
217 "Namestore lookup terminated. (answered=%d)", query->answered);
222 lookup_dht(query); //TODO
230 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
231 "Processing additional result %s from namestore\n", name);
233 qrecord = GNUNET_malloc(sizeof(struct GNUNET_GNS_QueryRecordList));
234 record = GNUNET_malloc(sizeof(struct GNUNET_DNSPARSER_Record));
235 qrecord->record = record;
237 record->name = (char*)name;
238 /* FIXME for gns records this requires the dnsparser to be modified!
240 * maybe store record data appropriately in namestore to avoid this
243 if (record_type == GNUNET_DNSPARSER_TYPE_A)
245 record->data.raw.data = (char*)data;
246 record->data.raw.data_len = size;
248 record->expiration_time = expiration;
249 record->type = record_type;
250 record->class = GNUNET_DNSPARSER_CLASS_INTERNET; /* srsly? */
252 if (flags == GNUNET_NAMESTORE_RF_AUTHORITY)
254 //query->num_authority_records++;
257 if ((0 == strcmp(query->name , name)) &&
258 (query->type == record_type))
260 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Found answer to query!\n");
264 query->num_records++;
266 //FIXME watch for leaks
267 GNUNET_CONTAINER_DLL_insert(query->records_head,
275 * Phase 1 of name resolution: Lookup local namestore
277 * @param name the name to look up
278 * @param id the id of the dns request (for the reply)
279 * @param type the record type to look for
282 lookup_namestore(struct GNUNET_DNS_RequestHandle *rh,
283 char* name, uint16_t id, uint16_t type)
285 struct GNUNET_GNS_PendingQuery *answer;
288 * Do db lookup here. Make dht lookup if necessary
289 * FIXME for now only local lookups for our zone!
291 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "This is .gnunet (%s)!\n", name);
292 answer = GNUNET_malloc(sizeof (struct GNUNET_GNS_PendingQuery));
296 answer->request_handle = rh;
298 GNUNET_NAMESTORE_lookup_name(namestore_handle,
307 * The DNS request handler
310 * @param rh request handle to user for reply
311 * @param request_length number of bytes in request
312 * @param request udp payload of the DNS request
315 handle_dns_request(void *cls,
316 struct GNUNET_DNS_RequestHandle *rh,
317 size_t request_length,
321 * parse request for tld
323 struct GNUNET_DNSPARSER_Packet *p;
328 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "hijacked a request...processing\n");
329 p = GNUNET_DNSPARSER_parse (request, request_length);
333 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
334 "Received malformed DNS packet, leaving it untouched\n");
335 GNUNET_DNS_request_forward (rh);
340 * Check tld and decide if we or
341 * legacy dns is responsible
343 for (i=0;i<p->num_queries;i++)
345 namelen = strlen(p->queries[i].name);
347 if (namelen < 7) /* this can't be .gnunet */
350 * Move our tld/root to config file
351 * Generate fake DNS reply that replaces .gnunet with .org for testing?
353 tail = p->queries[i].name+(namelen-7);
354 if (0 == strcmp(tail, ".gnunet"))
356 /* FIXME we need to answer to ALL queries in ONE response...
357 * What happens if some requests should be handles by us and
359 * Like this we only answer one...
361 lookup_namestore(rh, p->queries[i].name, p->id, p->queries[i].type);
366 * This request does not concern us. Forward to real DNS.
368 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
369 "Request for %s is forwarded to DNS\n", p->queries[i].name);
370 GNUNET_DNS_request_forward (rh);
377 handle_client_record_lookup(void *cls,
378 struct GNUNET_SERVER_Client *client,
379 const struct GNUNET_MessageHeader *message)
387 put_some_records(void)
389 /* put a few records into namestore */
390 char* ipA = "1.2.3.4";
391 char* ipB = "5.6.7.8";
392 struct in_addr *alice = GNUNET_malloc(sizeof(struct in_addr));
393 struct in_addr *bob = GNUNET_malloc(sizeof(struct in_addr));
394 GNUNET_assert(1 == inet_pton (AF_INET, ipA, alice));
395 GNUNET_assert(1 == inet_pton (AF_INET, ipB, bob));
396 GNUNET_NAMESTORE_record_put (namestore_handle,
399 GNUNET_GNS_RECORD_TYPE_A,
400 GNUNET_TIME_absolute_get_forever(),
401 GNUNET_NAMESTORE_RF_AUTHORITY,
403 sizeof(struct in_addr),
407 GNUNET_NAMESTORE_record_put (namestore_handle,
410 GNUNET_GNS_RECORD_TYPE_A,
411 GNUNET_TIME_absolute_get_forever(),
412 GNUNET_NAMESTORE_RF_AUTHORITY,
414 sizeof(struct in_addr),
421 * Process GNS requests.
424 * @param server the initialized server
425 * @param c configuration to use
428 run (void *cls, struct GNUNET_SERVER_Handle *server,
429 const struct GNUNET_CONFIGURATION_Handle *c)
431 /* The IPC message types */
432 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
433 /* callback, cls, type, size */
434 {&handle_client_record_lookup, NULL, GNUNET_MESSAGE_TYPE_GNS_CLIENT_LOOKUP,
439 nc = GNUNET_SERVER_notification_context_create (server, 1);
441 /* FIXME - do some config parsing
442 * - Maybe only hijack dns if option is set (HIJACK_DNS=1)
445 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
448 * Do gnunet dns init here
450 dns_handle = GNUNET_DNS_connect(c,
451 GNUNET_DNS_FLAG_PRE_RESOLUTION,
452 &handle_dns_request, /* rh */
455 if (NULL == dns_handle)
457 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
458 "Failed to connect to the dnsservice!\n");
462 * handle to our local namestore
464 namestore_handle = GNUNET_NAMESTORE_connect(c);
466 if (NULL == namestore_handle)
468 //FIXME do error handling;
469 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
470 "Failed to connect to the namestore!\n");
476 dht_handle = GNUNET_DHT_connect(c, 1); //FIXME get ht_len from cfg
478 if (NULL == dht_handle)
480 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not connect to DHT!\n");
485 GNUNET_SERVER_add_handlers (server, handlers);
487 * Esp the lookup would require to keep track of the clients' context
489 * GNUNET_SERVER_disconnect_notify (server, &client_disconnect, NULL);
495 * The main function for the GNS service.
497 * @param argc number of arguments from the command line
498 * @param argv command line arguments
499 * @return 0 ok, 1 on error
502 main (int argc, char *const *argv)
508 GNUNET_SERVICE_run (argc, argv, "gns", GNUNET_SERVICE_OPTION_NONE, &run,
513 /* end of gnunet-service-gns.c */