-bugfixes, code cleanup
[oweals/gnunet.git] / src / gns / gnunet-service-gns_interceptor.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2010, 2011, 2012 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20 /**
21  * @file gns/gnunet-service-gns_interceptor.c
22  * @brief GNUnet GNS interceptor logic
23  * @author Martin Schanzenbach
24  */
25 #include "platform.h"
26 #include "gnunet_util_lib.h"
27 #include "gnunet_transport_service.h"
28 #include "gnunet_dns_service.h"
29 #include "gnunet_dnsparser_lib.h"
30 #include "gnunet-service-gns_resolver.h"
31 #include "gns.h"
32
33 #define MAX_DNS_LABEL_LENGTH 63
34
35 /**
36  * Handle to a DNS intercepted
37  * reslution request
38  */
39 struct InterceptLookupHandle
40 {
41   /**
42    * the request handle to reply to 
43    */
44   struct GNUNET_DNS_RequestHandle *request_handle;
45   
46   /**
47    * the dns parser packet received 
48    */
49   struct GNUNET_DNSPARSER_Packet *packet;
50   
51   /**
52    * the query parsed from the packet 
53    */
54   struct GNUNET_DNSPARSER_Query *query;
55 };
56
57
58 /**
59  * Our handle to the DNS handler library
60  */
61 static struct GNUNET_DNS_Handle *dns_handle;
62
63 /**
64  * The root zone for this interceptor
65  */
66 static struct GNUNET_CRYPTO_ShortHashCode our_zone;
67
68 /**
69  * Our priv key
70  */
71 static struct GNUNET_CRYPTO_RsaPrivateKey *our_key;
72
73 /**
74  * Default timeout
75  */
76 static struct GNUNET_TIME_Relative default_lookup_timeout;
77
78
79 /**
80  * Reply to dns request with the result from our lookup.
81  *
82  * @param cls the closure to the request (an InterceptLookupHandle)
83  * @param rd_count the number of records to return
84  * @param rd the record data
85  */
86 static void
87 reply_to_dns (void* cls, uint32_t rd_count,
88               const struct GNUNET_NAMESTORE_RecordData *rd)
89 {
90   uint32_t i;
91   size_t len;
92   int ret;
93   char *buf;
94   struct InterceptLookupHandle* ilh = (struct InterceptLookupHandle*)cls;
95   struct GNUNET_DNSPARSER_Packet *packet = ilh->packet;
96   unsigned int num_answers = 0;
97   
98   
99   /**
100    * Put records in the DNS packet and modify it
101    * to a response
102    */
103   for (i=0; i < rd_count; i++)
104   {
105     if (rd[i].record_type == ilh->query->type)
106       num_answers++;
107   }
108
109   struct GNUNET_DNSPARSER_Record answer_records[num_answers];
110   struct GNUNET_DNSPARSER_Record additional_records[rd_count-(num_answers)];
111   packet->answers = answer_records;
112   packet->additional_records = additional_records;
113
114   for (i=0; i < rd_count; i++)
115   {
116     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
117                "Adding type %d to DNS response\n", rd[i].record_type);
118     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Name: %s\n", ilh->query->name);
119     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Record %d/%d\n", i+1, rd_count);
120     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Record len %d\n", rd[i].data_size);
121     
122     if (rd[i].record_type == ilh->query->type)
123     {
124       answer_records[i].name = ilh->query->name;
125       answer_records[i].type = rd[i].record_type;
126       switch(rd[i].record_type)
127       {
128        case GNUNET_GNS_RECORD_NS:
129        case GNUNET_GNS_RECORD_CNAME:
130        case GNUNET_GNS_RECORD_PTR:
131          answer_records[i].data.hostname = (char*)rd[i].data;
132          break;
133        case GNUNET_GNS_RECORD_SOA:
134          answer_records[i].data.soa =
135            (struct GNUNET_DNSPARSER_SoaRecord *)rd[i].data;
136          break;
137        case GNUNET_GNS_RECORD_MX:
138          answer_records[i].data.mx =
139            (struct GNUNET_DNSPARSER_MxRecord *)rd[i].data;
140          break;
141        default:
142         answer_records[i].data.raw.data_len = rd[i].data_size;
143         answer_records[i].data.raw.data = (char*)rd[i].data;
144       }
145       GNUNET_break (0 == (rd[i].flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION));
146       answer_records[i].expiration_time.abs_value = rd[i].expiration_time;
147       answer_records[i].class = GNUNET_DNSPARSER_CLASS_INTERNET;//hmmn
148     }
149     else
150     {
151       additional_records[i].name = ilh->query->name;
152       additional_records[i].type = rd[i].record_type;
153       switch(rd[i].record_type)
154       {
155        case GNUNET_GNS_RECORD_NS:
156        case GNUNET_GNS_RECORD_CNAME:
157        case GNUNET_GNS_RECORD_PTR:
158          additional_records[i].data.hostname = (char*)rd[i].data;
159          break;
160        case GNUNET_GNS_RECORD_SOA:
161          additional_records[i].data.soa =
162            (struct GNUNET_DNSPARSER_SoaRecord *)rd[i].data;
163          break;
164        case GNUNET_GNS_RECORD_MX:
165          additional_records[i].data.mx =
166            (struct GNUNET_DNSPARSER_MxRecord *)rd[i].data;
167          break;
168        default:
169         additional_records[i].data.raw.data_len = rd[i].data_size;
170         additional_records[i].data.raw.data = (char*)rd[i].data;
171       }
172       GNUNET_break (0 == (rd[i].flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION));
173       additional_records[i].expiration_time.abs_value = rd[i].expiration_time;
174       additional_records[i].class = GNUNET_DNSPARSER_CLASS_INTERNET;//hmmn
175     }
176   }
177   
178   packet->num_answers = num_answers;
179   packet->num_additional_records = rd_count-(num_answers);
180   
181   packet->flags.authoritative_answer = 1;
182
183   if (rd == NULL)
184     packet->flags.return_code = GNUNET_DNSPARSER_RETURN_CODE_NAME_ERROR;
185   else
186     packet->flags.return_code = GNUNET_DNSPARSER_RETURN_CODE_NO_ERROR;
187   
188   packet->flags.query_or_response = 1;
189
190   
191   /**
192    * Reply to DNS
193    */
194   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
195              "Building DNS response\n");
196   ret = GNUNET_DNSPARSER_pack (packet,
197                                1024, /* FIXME magic from dns redirector */
198                                &buf,
199                                &len);
200   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
201               "Built DNS response! (ret=%d,len=%d)\n",
202               ret, len);
203   if (ret == GNUNET_OK)
204   {
205     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
206                 "Answering DNS request\n");
207     GNUNET_DNS_request_answer (ilh->request_handle,
208                                len,
209                                buf);
210
211     GNUNET_free (buf);
212     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
213                 "Answered DNS request\n");
214   }
215   else
216   {
217     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
218                 "Error building DNS response! (ret=%d)", ret);
219   }
220   
221   packet->num_answers = 0;
222   packet->answers = NULL;
223   packet->num_additional_records = 0;
224   packet->additional_records = NULL;
225   GNUNET_DNSPARSER_free_packet(packet);
226   GNUNET_free(ilh);
227 }
228
229
230 /**
231  * Entry point for name resolution
232  * Setup a new query and try to resolve
233  *
234  * @param request the request handle of the DNS request from a client
235  * @param p the DNS query packet we received
236  * @param q the DNS query we received parsed from p
237  */
238 static void
239 start_resolution_for_dns (struct GNUNET_DNS_RequestHandle *request,
240                           struct GNUNET_DNSPARSER_Packet *p,
241                           struct GNUNET_DNSPARSER_Query *q)
242 {
243   struct InterceptLookupHandle* ilh;
244   
245   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
246               "Starting resolution for %s (type=%d)!\n",
247               q->name, q->type);
248   ilh = GNUNET_malloc(sizeof(struct InterceptLookupHandle));
249   ilh->packet = p;
250   ilh->query = q;
251   ilh->request_handle = request;
252   
253   /* Start resolution in our zone */
254   gns_resolver_lookup_record(our_zone, our_zone, q->type, q->name,
255                              our_key,
256                              default_lookup_timeout,
257                              GNUNET_NO,
258                              &reply_to_dns, ilh);
259 }
260
261
262 /**
263  * The DNS request handler
264  * Called for every incoming DNS request.
265  *
266  * @param cls closure
267  * @param rh request handle to user for reply
268  * @param request_length number of bytes in request
269  * @param request udp payload of the DNS request
270  */
271 static void
272 handle_dns_request (void *cls,
273                     struct GNUNET_DNS_RequestHandle *rh,
274                     size_t request_length,
275                     const char *request)
276 {
277   struct GNUNET_DNSPARSER_Packet *p;
278
279   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
280               "Hijacked a DNS request...processing\n");
281   if (NULL == (p = GNUNET_DNSPARSER_parse (request, request_length)))
282   {
283     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
284                 "Received malformed DNS packet, leaving it untouched\n");
285     GNUNET_DNS_request_forward (rh);
286     GNUNET_DNSPARSER_free_packet (p);
287     return;
288   }
289   
290   /**
291    * Check tld and decide if we or
292    * legacy dns is responsible
293    *
294    * FIXME now in theory there could be more than 1 query in the request
295    * but if this is case we get into trouble:
296    * either we query the GNS or the DNS. We cannot do both!
297    * So I suggest to either only allow a single query per request or
298    * only allow GNS or DNS requests.
299    * The way it is implemented here now is buggy and will lead to erratic
300    * behaviour (if multiple queries are present).
301    */
302   if (0 == p->num_queries)
303   {
304     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
305                 "No Queries in DNS packet... forwarding\n");
306     GNUNET_DNS_request_forward (rh);
307     GNUNET_DNSPARSER_free_packet(p);
308     return;
309   }
310
311   /**
312    * Check for .gads/.zkey
313    */
314   
315   if ((is_gnunet_tld(p->queries[0].name) == GNUNET_YES) ||
316       (is_zkey_tld(p->queries[0].name) == GNUNET_YES) ||
317       (strcmp(p->queries[0].name, GNUNET_GNS_TLD) == 0))
318   {
319     if (p->num_queries > 1)
320     {
321       /* Note: We could also look for .gads */
322       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
323                   ">1 queriy in DNS packet... odd. We only process #1\n");
324     }
325     start_resolution_for_dns (rh, p, p->queries);
326     return;
327   }
328   /**
329    * This request does not concern us. Forward to real DNS.
330    */
331   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
332               "Request for %s is forwarded to DNS\n", 
333               p->queries[0].name);
334   GNUNET_DNS_request_forward (rh);
335   GNUNET_DNSPARSER_free_packet (p);
336 }
337
338
339 /**
340  * Initialized the interceptor
341  *
342  * @param zone the zone to work in
343  * @param key the prov key of the zone (can be null, needed for caching)
344  * @param c the configuration
345  * @return GNUNET_OK on success
346  */
347 int
348 gns_interceptor_init (struct GNUNET_CRYPTO_ShortHashCode zone,
349                       struct GNUNET_CRYPTO_RsaPrivateKey *key,
350                       const struct GNUNET_CONFIGURATION_Handle *c)
351 {
352   GNUNET_log(GNUNET_ERROR_TYPE_INFO,
353              "DNS hijacking enabled... connecting to service.\n");
354   our_zone = zone;
355   our_key = key;
356   /**
357    * Do gnunet dns init here
358    */
359   dns_handle = GNUNET_DNS_connect (c,
360                                    GNUNET_DNS_FLAG_PRE_RESOLUTION,
361                                    &handle_dns_request, /* rh */
362                                    NULL); /* Closure */
363
364   if (GNUNET_OK !=
365       GNUNET_CONFIGURATION_get_value_time (c, "gns",
366                                            "DEFAULT_LOOKUP_TIMEOUT",
367                                            &default_lookup_timeout))
368     default_lookup_timeout = GNUNET_TIME_UNIT_ZERO;
369   if (NULL == dns_handle)
370   {
371     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
372              "Failed to connect to the dnsservice!\n");
373     return GNUNET_SYSERR;
374   }
375   return GNUNET_YES;
376 }
377
378
379 /**
380  * Disconnect from interceptor
381  */
382 void
383 gns_interceptor_stop ()
384 {
385   if (NULL != dns_handle)
386   {
387     GNUNET_DNS_disconnect(dns_handle);
388     dns_handle = NULL;
389   }
390 }
391
392 /* end of gns_interceptor.c */