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.
23 * @file gns/gns_interceptor.c
24 * @brief GNUnet GNS interceptor logic
25 * @author Martin Schanzenbach
28 #include "gnunet_util_lib.h"
29 #include "gnunet_transport_service.h"
30 #include "gnunet_dns_service.h"
31 #include "gnunet_dnsparser_lib.h"
32 #include "gnunet-service-gns_resolver.h"
35 #define MAX_DNS_LABEL_LENGTH 63
38 * Handle to a DNS intercepted
41 struct InterceptLookupHandle
43 /* the request handle to reply to */
44 struct GNUNET_DNS_RequestHandle *request_handle;
46 /* the dns parser packet received */
47 struct GNUNET_DNSPARSER_Packet *packet;
49 /* the query parsed from the packet */
50 struct GNUNET_DNSPARSER_Query *query;
55 * Our handle to the DNS handler library
57 static struct GNUNET_DNS_Handle *dns_handle;
60 * The root zone for this interceptor
62 static GNUNET_HashCode our_zone;
67 static struct GNUNET_CRYPTO_RsaPrivateKey *our_key;
70 * Reply to dns request with the result from our lookup.
72 * @param cls the closure to the request (an InterceptLookupHandle)
73 * @param rh the request handle of the lookup
74 * @param rd_count the number of records to return
75 * @param rd the record data
78 reply_to_dns(void* cls, uint32_t rd_count,
79 const struct GNUNET_NAMESTORE_RecordData *rd)
85 struct InterceptLookupHandle* ilh = (struct InterceptLookupHandle*)cls;
86 struct GNUNET_DNSPARSER_Packet *packet = ilh->packet;
87 unsigned int num_answers = 0;
91 * Put records in the DNS packet and modify it
94 for (i=0; i < rd_count; i++)
96 if (rd[i].record_type == ilh->query->type)
100 struct GNUNET_DNSPARSER_Record answer_records[num_answers];
101 struct GNUNET_DNSPARSER_Record additional_records[rd_count-(num_answers)];
102 packet->answers = answer_records;
103 packet->additional_records = additional_records;
105 for (i=0; i < rd_count; i++)
107 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
108 "Adding type %d to DNS response\n", rd[i].record_type);
109 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Name: %s\n", ilh->query->name);
110 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Record %d/%d\n", i+1, rd_count);
111 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Record len %d\n", rd[i].data_size);
113 if (rd[i].record_type == ilh->query->type)
115 answer_records[i].name = ilh->query->name;
116 answer_records[i].type = rd[i].record_type;
117 answer_records[i].data.raw.data_len = rd[i].data_size;
118 answer_records[i].data.raw.data = (char*)rd[i].data;
119 answer_records[i].expiration_time = rd[i].expiration;
120 answer_records[i].class = GNUNET_DNSPARSER_CLASS_INTERNET;//hmmn
124 additional_records[i].name = ilh->query->name;
125 additional_records[i].type = rd[i].record_type;
126 additional_records[i].data.raw.data_len = rd[i].data_size;
127 additional_records[i].data.raw.data = (char*)rd[i].data;
128 additional_records[i].expiration_time = rd[i].expiration;
129 additional_records[i].class = GNUNET_DNSPARSER_CLASS_INTERNET;//hmmn
133 packet->num_answers = num_answers;
134 packet->num_additional_records = rd_count-(num_answers);
136 packet->flags.authoritative_answer = 1;
139 packet->flags.return_code = GNUNET_DNSPARSER_RETURN_CODE_NAME_ERROR;
141 packet->flags.return_code = GNUNET_DNSPARSER_RETURN_CODE_NO_ERROR;
143 packet->flags.query_or_response = 1;
149 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
150 "Building DNS response\n");
151 ret = GNUNET_DNSPARSER_pack (packet,
152 1024, /* FIXME magic from dns redirector */
155 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
156 "Built DNS response! (ret=%d,len=%d)\n", ret, len);
157 if (ret == GNUNET_OK)
159 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
160 "Answering DNS request\n");
161 GNUNET_DNS_request_answer(ilh->request_handle,
166 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Answered DNS request\n");
170 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
171 "Error building DNS response! (ret=%d)", ret);
174 packet->num_answers = 0;
175 packet->answers = NULL;
176 packet->num_additional_records = 0;
177 packet->additional_records = NULL;
178 GNUNET_DNSPARSER_free_packet(packet);
179 //FIXME free resolver handle in resp functions in resolver!
180 //GNUNET_free((struct RecordLookupHandle*)rh->proc_cls);
181 //free_resolver_handle(rh);
187 * Entry point for name resolution
188 * Setup a new query and try to resolve
190 * @param request the request handle of the DNS request from a client
191 * @param p the DNS query packet we received
192 * @param q the DNS query we received parsed from p
195 start_resolution_for_dns(struct GNUNET_DNS_RequestHandle *request,
196 struct GNUNET_DNSPARSER_Packet *p,
197 struct GNUNET_DNSPARSER_Query *q)
199 struct InterceptLookupHandle* ilh;
201 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
202 "Starting resolution for %s (type=%d)!\n",
205 ilh = GNUNET_malloc(sizeof(struct InterceptLookupHandle));
208 ilh->request_handle = request;
210 /* Start resolution in our zone */
211 gns_resolver_lookup_record(our_zone, q->type, q->name,
219 * The DNS request handler
220 * Called for every incoming DNS request.
223 * @param rh request handle to user for reply
224 * @param request_length number of bytes in request
225 * @param request udp payload of the DNS request
228 handle_dns_request(void *cls,
229 struct GNUNET_DNS_RequestHandle *rh,
230 size_t request_length,
233 struct GNUNET_DNSPARSER_Packet *p;
237 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Hijacked a DNS request...processing\n");
238 p = GNUNET_DNSPARSER_parse (request, request_length);
242 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
243 "Received malformed DNS packet, leaving it untouched\n");
244 GNUNET_DNS_request_forward (rh);
245 GNUNET_DNSPARSER_free_packet (p);
250 * Check tld and decide if we or
251 * legacy dns is responsible
253 * FIXME now in theory there could be more than 1 query in the request
254 * but if this is case we get into trouble:
255 * either we query the GNS or the DNS. We cannot do both!
256 * So I suggest to either only allow a single query per request or
257 * only allow GNS or DNS requests.
258 * The way it is implemented here now is buggy and will lead to erratic
259 * behaviour (if multiple queries are present).
261 if (p->num_queries == 0)
263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
264 "No Queries in DNS packet... forwarding\n");
265 GNUNET_DNS_request_forward (rh);
266 GNUNET_DNSPARSER_free_packet(p);
270 if (p->num_queries > 1)
272 /* Note: We could also look for .gnunet */
273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
274 ">1 queriy in DNS packet... odd. We only process #1\n");
281 tldoffset = p->queries[0].name + strlen(p->queries[0].name) - 1;
283 for (i=0; i<strlen(p->queries[0].name); i++)
285 if (*(tldoffset-i) == '.')
291 if ((i==strlen(GNUNET_GNS_TLD)-1)
292 && (0 == strcmp(tldoffset-i, GNUNET_GNS_TLD)))
294 start_resolution_for_dns(rh, p, p->queries);
299 * This request does not concern us. Forward to real DNS.
301 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
302 "Request for %s is forwarded to DNS\n", p->queries[0].name);
303 GNUNET_DNS_request_forward (rh);
304 GNUNET_DNSPARSER_free_packet (p);
311 * Initialized the interceptor
313 * @param zone the zone to work in
314 * @param the prov key of the zone (can be null, needed for caching)
315 * @param c the configuration
316 * @return GNUNET_OK on success
319 gns_interceptor_init(GNUNET_HashCode zone,
320 struct GNUNET_CRYPTO_RsaPrivateKey *key,
321 const struct GNUNET_CONFIGURATION_Handle *c)
323 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
324 "DNS hijacking enabled... connecting to service.\n");
329 * Do gnunet dns init here
331 dns_handle = GNUNET_DNS_connect(c,
332 GNUNET_DNS_FLAG_PRE_RESOLUTION,
333 &handle_dns_request, /* rh */
335 if (NULL == dns_handle)
337 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
338 "Failed to connect to the dnsservice!\n");
339 return GNUNET_SYSERR;
345 /* end of gns_interceptor.c */