2 This file is part of GNUnet.
3 (C) 2009-2013 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.
21 * @file gns/gnunet-service-gns_interceptor.c
22 * @brief GNUnet GNS interceptor logic
23 * @author Martin Schanzenbach
24 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
28 #include "gnunet_dns_service.h"
29 #include "gnunet_dnsparser_lib.h"
30 #include "gnunet-service-gns_resolver.h"
31 #include "gnunet-service-gns_interceptor.h"
36 * Handle to a DNS intercepted
39 struct InterceptLookupHandle
43 * We keep these in a DLL.
45 struct InterceptLookupHandle *next;
48 * We keep these in a DLL.
50 struct InterceptLookupHandle *prev;
53 * the request handle to reply to
55 struct GNUNET_DNS_RequestHandle *request_handle;
58 * the dns parser packet received
60 struct GNUNET_DNSPARSER_Packet *packet;
63 * Handle for the lookup operation.
65 struct GNS_ResolverHandle *lookup;
71 * Our handle to the DNS handler library
73 static struct GNUNET_DNS_Handle *dns_handle;
76 * Key of the zone we start lookups in.
78 static struct GNUNET_CRYPTO_EccPublicSignKey zone;
83 static struct InterceptLookupHandle *ilh_head;
88 static struct InterceptLookupHandle *ilh_tail;
92 * Reply to dns request with the result from our lookup.
94 * @param cls the closure to the request (an InterceptLookupHandle)
95 * @param rd_count the number of records to return
96 * @param rd the record data
99 reply_to_dns (void *cls, uint32_t rd_count,
100 const struct GNUNET_NAMESTORE_RecordData *rd)
102 struct InterceptLookupHandle *ilh = cls;
103 struct GNUNET_DNSPARSER_Packet *packet = ilh->packet;
104 struct GNUNET_DNSPARSER_Query *query = &packet->queries[0];
109 unsigned int num_answers;
111 /* Put records in the DNS packet */
113 for (i=0; i < rd_count; i++)
114 if (rd[i].record_type == query->type)
118 struct GNUNET_DNSPARSER_Record answer_records[num_answers];
119 struct GNUNET_DNSPARSER_Record additional_records[rd_count - num_answers];
121 packet->answers = answer_records;
122 packet->additional_records = additional_records;
124 /* FIXME: need to handle #GNUNET_NAMESTORE_RF_SHADOW_RECORD option
125 (by ignoring records where this flag is set if there is any
126 other record of that type in the result set) */
127 for (i=0; i < rd_count; i++)
129 if (rd[i].record_type == query->type)
131 answer_records[i].name = query->name;
132 answer_records[i].type = rd[i].record_type;
133 switch(rd[i].record_type)
135 case GNUNET_DNSPARSER_TYPE_NS:
136 case GNUNET_DNSPARSER_TYPE_CNAME:
137 case GNUNET_DNSPARSER_TYPE_PTR:
138 answer_records[i].data.hostname = (char*)rd[i].data;
140 case GNUNET_DNSPARSER_TYPE_SOA:
141 answer_records[i].data.soa =
142 (struct GNUNET_DNSPARSER_SoaRecord *)rd[i].data;
144 case GNUNET_DNSPARSER_TYPE_MX:
145 answer_records[i].data.mx =
146 (struct GNUNET_DNSPARSER_MxRecord *)rd[i].data;
149 answer_records[i].data.raw.data_len = rd[i].data_size;
150 answer_records[i].data.raw.data = (char*)rd[i].data;
153 GNUNET_break (0 == (rd[i].flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION));
154 answer_records[i].expiration_time.abs_value_us = rd[i].expiration_time;
155 answer_records[i].class = GNUNET_TUN_DNS_CLASS_INTERNET;
159 additional_records[i].name = query->name;
160 additional_records[i].type = rd[i].record_type;
161 switch(rd[i].record_type)
163 case GNUNET_DNSPARSER_TYPE_NS:
164 case GNUNET_DNSPARSER_TYPE_CNAME:
165 case GNUNET_DNSPARSER_TYPE_PTR:
166 additional_records[i].data.hostname = (char*)rd[i].data;
168 case GNUNET_DNSPARSER_TYPE_SOA:
169 additional_records[i].data.soa =
170 (struct GNUNET_DNSPARSER_SoaRecord *)rd[i].data;
172 case GNUNET_DNSPARSER_TYPE_MX:
173 additional_records[i].data.mx =
174 (struct GNUNET_DNSPARSER_MxRecord *)rd[i].data;
177 additional_records[i].data.raw.data_len = rd[i].data_size;
178 additional_records[i].data.raw.data = (char*)rd[i].data;
181 GNUNET_break (0 == (rd[i].flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION));
182 additional_records[i].expiration_time.abs_value_us = rd[i].expiration_time;
183 additional_records[i].class = GNUNET_TUN_DNS_CLASS_INTERNET;
186 packet->num_answers = num_answers;
187 packet->num_additional_records = rd_count - num_answers;
188 packet->flags.authoritative_answer = 1;
190 packet->flags.return_code = GNUNET_TUN_DNS_RETURN_CODE_NAME_ERROR;
192 packet->flags.return_code = GNUNET_TUN_DNS_RETURN_CODE_NO_ERROR;
193 packet->flags.query_or_response = 1;
194 ret = GNUNET_DNSPARSER_pack (packet,
195 1024, /* maximum allowed size for DNS reply */
198 if (GNUNET_OK != ret)
200 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
201 _("Error converting GNS response to DNS response!\n"));
205 GNUNET_DNS_request_answer (ilh->request_handle,
210 packet->num_answers = 0;
211 packet->answers = NULL;
212 packet->num_additional_records = 0;
213 packet->additional_records = NULL;
214 GNUNET_DNSPARSER_free_packet (packet);
216 GNUNET_CONTAINER_DLL_remove (ilh_head, ilh_tail, ilh);
222 * The DNS request handler. Called for every incoming DNS request.
224 * @param cls closure, unused
225 * @param rh request handle to user for reply
226 * @param request_length number of bytes in @a request
227 * @param request UDP payload of the DNS request
230 handle_dns_request (void *cls,
231 struct GNUNET_DNS_RequestHandle *rh,
232 size_t request_length,
235 struct GNUNET_DNSPARSER_Packet *p;
236 struct InterceptLookupHandle *ilh;
238 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
239 "Hijacked a DNS request. Processing.\n");
240 if (NULL == (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);
249 /* Check TLD and decide if we or legacy dns is responsible */
250 if (1 != p->num_queries)
252 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
253 "Not exactly one query in DNS packet. Forwarding untouched.\n");
254 GNUNET_DNS_request_forward (rh);
255 GNUNET_DNSPARSER_free_packet(p);
259 /* Check for GNS TLDs. */
260 if ( (GNUNET_YES == is_gnu_tld (p->queries[0].name)) ||
261 (GNUNET_YES == is_zkey_tld (p->queries[0].name)) ||
262 (0 == strcmp (p->queries[0].name, GNUNET_GNS_TLD)) )
264 /* Start resolution in GNS */
265 ilh = GNUNET_new (struct InterceptLookupHandle);
266 GNUNET_CONTAINER_DLL_insert (ilh_head, ilh_tail, ilh);
268 ilh->request_handle = rh;
269 ilh->lookup = GNS_resolver_lookup (&zone,
272 NULL /* FIXME: enable shorten for DNS intercepts? */,
277 /* This request does not concern us. Forward to real DNS. */
278 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
279 "Request for `%s' is forwarded to DNS untouched.\n",
281 GNUNET_DNS_request_forward (rh);
282 GNUNET_DNSPARSER_free_packet (p);
287 * Initialized the interceptor
289 * @param gnu_zone the zone to work in
290 * @param c the configuration
291 * @return GNUNET_OK on success
294 GNS_interceptor_init (const struct GNUNET_CRYPTO_EccPublicSignKey *gnu_zone,
295 const struct GNUNET_CONFIGURATION_Handle *c)
297 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
298 "DNS hijacking enabled. Connecting to DNS service.\n");
300 dns_handle = GNUNET_DNS_connect (c,
301 GNUNET_DNS_FLAG_PRE_RESOLUTION,
304 if (NULL == dns_handle)
306 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
307 _("Failed to connect to the DNS service!\n"));
308 return GNUNET_SYSERR;
315 * Disconnect from interceptor
318 GNS_interceptor_done ()
320 struct InterceptLookupHandle *ilh;
322 while (NULL != (ilh = ilh_head))
324 GNUNET_CONTAINER_DLL_remove (ilh_head, ilh_tail, ilh);
325 GNS_resolver_lookup_cancel (ilh->lookup);
326 GNUNET_DNS_request_drop (ilh->request_handle);
327 GNUNET_DNSPARSER_free_packet (ilh->packet);
330 if (NULL != dns_handle)
332 GNUNET_DNS_disconnect (dns_handle);
337 /* end of gnunet-service-gns_interceptor.c */