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