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_namestore_service.h"
32 #include "gnunet_gns_service.h"
36 /* TODO into gnunet_protocols */
37 #define GNUNET_MESSAGE_TYPE_GNS_CLIENT_LOOKUP 23
38 #define GNUNET_MESSAGE_TYPE_GNS_CLIENT_RESULT 24
40 struct GNUNET_GNS_QueryRecordList
45 struct GNUNET_GNS_QueryRecordList * next;
46 struct GNUNET_GNS_QueryRecordList * prev;
48 struct GNUNET_DNSPARSER_Record * record;
52 * A result list for namestore queries
54 struct GNUNET_GNS_PendingQuery
56 /* the answer packet */
57 struct GNUNET_DNSPARSER_Packet *answer;
59 /* records to put into answer packet */
60 struct GNUNET_GNS_QueryRecordList * records_head;
61 struct GNUNET_GNS_QueryRecordList * records_tail;
64 int num_authority_records; //FIXME are all of our replies auth?
68 /* the dns request id */
69 int id; // FIXME can handle->request_id also be used here?
71 /* the request handle to reply to */
72 struct GNUNET_DNS_RequestHandle *request_handle;
74 /* hast this query been answered? */
80 * Our handle to the DNS handler library
82 struct GNUNET_DNS_Handle *dns_handle;
83 struct GNUNET_DNS_Handle *dns_res_handle;
86 * Our handle to the namestore service
88 struct GNUNET_NAMESTORE_Handle *namestore_handle;
91 * The configuration the GNS service is running with
93 const struct GNUNET_CONFIGURATION_Handle *GNS_cfg;
96 * Our notification context.
98 static struct GNUNET_SERVER_NotificationContext *nc;
103 const GNUNET_HashCode *my_zone;
106 * Task run during shutdown.
112 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
114 GNUNET_DNS_disconnect(dns_handle);
115 GNUNET_NAMESTORE_disconnect(namestore_handle, 0);
119 * Phase 2 of resolution.
127 reply_to_dns(struct GNUNET_GNS_PendingQuery *answer)
129 struct GNUNET_GNS_QueryRecordList *i;
130 struct GNUNET_DNSPARSER_Packet *packet;
131 struct GNUNET_DNSPARSER_Flags dnsflags;
137 packet = GNUNET_malloc(sizeof(struct GNUNET_DNSPARSER_Packet));
139 GNUNET_malloc(sizeof(struct GNUNET_DNSPARSER_Record) * answer->num_records);
141 len = sizeof(struct GNUNET_DNSPARSER_Record*);
143 for (i=answer->records_head; i != NULL; i=i->next)
145 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
146 "Adding %s to DNS response\n", i->record->name);
147 memcpy(&packet->answers[j],
149 sizeof (struct GNUNET_DNSPARSER_Record));
150 GNUNET_free(i->record);
153 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "after memcpy\n");
154 /* FIXME how to handle auth, additional etc */
155 packet->num_answers = answer->num_records;
156 packet->num_authority_records = answer->num_authority_records;
158 dnsflags.authoritative_answer = 1;
159 dnsflags.opcode = GNUNET_DNSPARSER_OPCODE_QUERY;
160 dnsflags.return_code = GNUNET_DNSPARSER_RETURN_CODE_NO_ERROR; //not sure
161 dnsflags.query_or_response = 1;
162 packet->flags = dnsflags;
164 packet->id = answer->id;
165 ret = GNUNET_DNSPARSER_pack (packet,
166 1024, /* FIXME magic from dns redirector */
169 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
170 "Built DNS response! (ret=%d)\n", ret);
171 if (ret == GNUNET_OK)
173 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
174 "Answering DNS request\n");
175 GNUNET_DNS_request_answer(answer->request_handle,
179 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Answered DNS request\n");
180 //FIXME return code, free datastructures
184 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
185 "Error building DNS response! (ret=%d)", ret);
190 process_ns_result(void* cls, const GNUNET_HashCode *zone,
191 const char *name, uint32_t record_type,
192 struct GNUNET_TIME_Absolute expiration,
193 enum GNUNET_NAMESTORE_RecordFlags flags,
194 const struct GNUNET_NAMESTORE_SignatureLocation *sig_loc,
195 size_t size, const void *data)
197 struct GNUNET_GNS_PendingQuery *query;
198 struct GNUNET_GNS_QueryRecordList *qrecord;
199 struct GNUNET_DNSPARSER_Record *record;
200 query = (struct GNUNET_GNS_PendingQuery *) cls;
201 char bufA[INET6_ADDRSTRLEN];
207 * Last result received (or none)
208 * Do we have what we need to answer?
209 * If not -> DHT Phase
212 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
213 "Namestore lookup terminated. (answered=%d)", query->answered);
226 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
227 "Processing additional result %s from namestore\n", name);
229 qrecord = GNUNET_malloc(sizeof(struct GNUNET_GNS_QueryRecordList));
230 record = GNUNET_malloc(sizeof(struct GNUNET_DNSPARSER_Record));
231 qrecord->record = record;
233 record->name = (char*)name;
234 /* FIXME for gns records this requires the dnsparser to be modified!
237 if (record_type == GNUNET_DNSPARSER_TYPE_A)
239 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "A record: %s\n", data);
240 record->data.raw.data = GNUNET_malloc(sizeof (struct in_addr));
241 GNUNET_assert(1 == inet_pton (AF_INET, data,
242 record->data.raw.data));
243 record->data.raw.data_len = sizeof (struct in_addr);
245 record->expiration_time = expiration;
246 record->type = record_type;
247 record->class = GNUNET_DNSPARSER_CLASS_INTERNET; /* srsly? */
249 if (flags == GNUNET_NAMESTORE_RF_AUTHORITY)
251 //query->num_authority_records++;
254 if ((0 == strcmp(query->name , name)) &&
255 (query->type == record_type))
257 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Found answer to query!\n");
261 query->num_records++;
263 //FIXME watch for leaks
264 GNUNET_CONTAINER_DLL_insert(query->records_head,
272 handle_dns_response(void *cls,
273 struct GNUNET_DNS_RequestHandle *rh,
274 size_t request_length,
277 GNUNET_DNS_request_forward (rh);
282 * Phase 1 of name resolution: Lookup local namestore
284 * @param name the name to look up
285 * @param id the id of the dns request (for the reply)
286 * @param type the record type to look for
289 lookup_namestore(struct GNUNET_DNS_RequestHandle *rh, char* name, uint16_t id, uint16_t type)
291 struct GNUNET_GNS_PendingQuery *answer;
294 * Do db lookup here. Make dht lookup if necessary
295 * FIXME for now only local lookups for our zone!
297 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "This is .gnunet (%s)!\n", name);
298 answer = GNUNET_malloc(sizeof (struct GNUNET_GNS_PendingQuery));
302 answer->request_handle = rh;
304 GNUNET_NAMESTORE_lookup_name(namestore_handle,
313 * The DNS request handler
316 * @param rh request handle to user for reply
317 * @param request_length number of bytes in request
318 * @param request udp payload of the DNS request
321 handle_dns_request(void *cls,
322 struct GNUNET_DNS_RequestHandle *rh,
323 size_t request_length,
327 * parse request for tld
329 struct GNUNET_DNSPARSER_Packet *p;
334 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "hijacked a request...processing\n");
335 p = GNUNET_DNSPARSER_parse (request, request_length);
339 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
340 "Received malformed DNS packet, leaving it untouched\n");
341 GNUNET_DNS_request_forward (rh);
346 * Check tld and decide if we or
347 * legacy dns is responsible
349 for (i=0;i<p->num_queries;i++)
351 namelen = strlen(p->queries[i].name);
353 if (namelen < 7) /* this can't be .gnunet */
357 * Move our tld/root to config file
358 * Generate fake DNS reply that replaces .gnunet with .org for testing?
360 tail = p->queries[i].name+(namelen-7);
361 if (0 == strcmp(tail, ".gnunet"))
363 /* FIXME we need to answer to ALL queries in ONE response...
364 * Like this we only answer one...
366 lookup_namestore(rh, p->queries[i].name, p->id, p->queries[i].type);
371 * This request does not concern us. Forward to real DNS.
373 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
374 "Request for %s is forwarded to DNS\n", p->queries[i].name);
375 GNUNET_DNS_request_forward (rh);
382 handle_client_record_lookup(void *cls,
383 struct GNUNET_SERVER_Client *client,
384 const struct GNUNET_MessageHeader *message)
392 put_some_records(void)
394 /* put a few records into namestore */
395 char* ipA = "1.2.3.4";
396 char* ipB = "5.6.7.8";
397 GNUNET_NAMESTORE_record_put (namestore_handle,
400 GNUNET_GNS_RECORD_TYPE_A,
401 GNUNET_TIME_absolute_get_forever(),
402 GNUNET_NAMESTORE_RF_AUTHORITY,
408 GNUNET_NAMESTORE_record_put (namestore_handle,
411 GNUNET_GNS_RECORD_TYPE_A,
412 GNUNET_TIME_absolute_get_forever(),
413 GNUNET_NAMESTORE_RF_AUTHORITY,
422 * Process GNS requests.
425 * @param server the initialized server
426 * @param c configuration to use
429 run (void *cls, struct GNUNET_SERVER_Handle *server,
430 const struct GNUNET_CONFIGURATION_Handle *c)
432 /* The IPC message types */
433 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
434 /* callback, cls, type, size */
435 {&handle_client_record_lookup, NULL, GNUNET_MESSAGE_TYPE_GNS_CLIENT_LOOKUP,
440 nc = GNUNET_SERVER_notification_context_create (server, 1);
442 /* FIXME - do some config parsing
443 * - Maybe only hijack dns if option is set (HIJACK_DNS=1)
446 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
449 * Do gnunet dns init here
451 dns_handle = GNUNET_DNS_connect(c,
452 GNUNET_DNS_FLAG_PRE_RESOLUTION,
453 &handle_dns_request, /* rh */
456 dns_res_handle = GNUNET_DNS_connect(c,
457 GNUNET_DNS_FLAG_PRE_RESOLUTION,
458 &handle_dns_response, /* rh */
461 if (NULL == dns_handle)
463 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
464 "Failed to connect to the dnsservice!\n");
468 * handle to our local namestore
470 namestore_handle = GNUNET_NAMESTORE_connect(c);
472 if (NULL == namestore_handle)
474 //FIXME do error handling;
475 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
476 "Failed to connect to the namestore!\n");
481 GNUNET_SERVER_add_handlers (server, handlers);
483 * Esp the lookup would require to keep track of the clients' context
485 * GNUNET_SERVER_disconnect_notify (server, &client_disconnect, NULL);
491 * The main function for the GNS service.
493 * @param argc number of arguments from the command line
494 * @param argv command line arguments
495 * @return 0 ok, 1 on error
498 main (int argc, char *const *argv)
504 GNUNET_SERVICE_run (argc, argv, "gns", GNUNET_SERVICE_OPTION_NONE, &run,
509 /* end of gnunet-service-gns.c */