- new program_run and run_2
[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/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 struct GNUNET_DNS_Handle *dns_handle;
58
59 /**
60  * The root zone for this interceptor
61  */
62 static GNUNET_HashCode our_zone;
63
64 /**
65  * Reply to dns request with the result from our lookup.
66  *
67  * @param cls the closure to the request (an InterceptLookupHandle)
68  * @param rh the request handle of the lookup
69  * @param rd_count the number of records to return
70  * @param rd the record data
71  */
72 static void
73 reply_to_dns(void* cls, uint32_t rd_count,
74              const struct GNUNET_NAMESTORE_RecordData *rd)
75 {
76   int i;
77   size_t len;
78   int ret;
79   char *buf;
80   struct InterceptLookupHandle* ilh = (struct InterceptLookupHandle*)cls;
81   struct GNUNET_DNSPARSER_Packet *packet = ilh->packet;
82   unsigned int num_answers = 0;
83   
84   
85   /**
86    * Put records in the DNS packet and modify it
87    * to a response
88    */
89   for (i=0; i < rd_count; i++)
90   {
91     if (rd[i].record_type == ilh->query->type)
92       num_answers++;
93   }
94
95   struct GNUNET_DNSPARSER_Record answer_records[num_answers];
96   struct GNUNET_DNSPARSER_Record additional_records[rd_count-(num_answers)];
97   packet->answers = answer_records;
98   packet->additional_records = additional_records;
99
100   for (i=0; i < rd_count; i++)
101   {
102     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
103                "Adding type %d to DNS response\n", rd[i].record_type);
104     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Name: %s\n", ilh->query->name);
105     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Record %d/%d\n", i+1, rd_count);
106     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Record len %d\n", rd[i].data_size);
107     
108     if (rd[i].record_type == ilh->query->type)
109     {
110       answer_records[i].name = ilh->query->name;
111       answer_records[i].type = rd[i].record_type;
112       answer_records[i].data.raw.data_len = rd[i].data_size;
113       answer_records[i].data.raw.data = (char*)rd[i].data;
114       answer_records[i].expiration_time = rd[i].expiration;
115       answer_records[i].class = GNUNET_DNSPARSER_CLASS_INTERNET;//hmmn
116     }
117     else
118     {
119       additional_records[i].name = ilh->query->name;
120       additional_records[i].type = rd[i].record_type;
121       additional_records[i].data.raw.data_len = rd[i].data_size;
122       additional_records[i].data.raw.data = (char*)rd[i].data;
123       additional_records[i].expiration_time = rd[i].expiration;
124       additional_records[i].class = GNUNET_DNSPARSER_CLASS_INTERNET;//hmmn
125     }
126   }
127   
128   packet->num_answers = num_answers;
129   packet->num_additional_records = rd_count-(num_answers);
130   
131   packet->flags.authoritative_answer = 1;
132
133   if (rd == NULL)
134     packet->flags.return_code = GNUNET_DNSPARSER_RETURN_CODE_NAME_ERROR;
135   else
136     packet->flags.return_code = GNUNET_DNSPARSER_RETURN_CODE_NO_ERROR;
137   
138   packet->flags.query_or_response = 1;
139
140   
141   /**
142    * Reply to DNS
143    */
144   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
145              "Building DNS response\n");
146   ret = GNUNET_DNSPARSER_pack (packet,
147                                1024, /* FIXME magic from dns redirector */
148                                &buf,
149                                &len);
150   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
151              "Built DNS response! (ret=%d,len=%d)\n", ret, len);
152   if (ret == GNUNET_OK)
153   {
154     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
155                "Answering DNS request\n");
156     GNUNET_DNS_request_answer(ilh->request_handle,
157                               len,
158                               buf);
159
160     GNUNET_free(buf);
161     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Answered DNS request\n");
162   }
163   else
164   {
165     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
166                "Error building DNS response! (ret=%d)", ret);
167   }
168   
169   packet->num_answers = 0;
170   packet->answers = NULL;
171   packet->num_additional_records = 0;
172   packet->additional_records = NULL;
173   GNUNET_DNSPARSER_free_packet(packet);
174   //FIXME free resolver handle in resp functions in resolver!
175   //GNUNET_free((struct RecordLookupHandle*)rh->proc_cls);
176   //free_resolver_handle(rh);
177   GNUNET_free(ilh);
178 }
179
180
181 /**
182  * Entry point for name resolution
183  * Setup a new query and try to resolve
184  *
185  * @param request the request handle of the DNS request from a client
186  * @param p the DNS query packet we received
187  * @param q the DNS query we received parsed from p
188  */
189 static void
190 start_resolution_for_dns(struct GNUNET_DNS_RequestHandle *request,
191                           struct GNUNET_DNSPARSER_Packet *p,
192                           struct GNUNET_DNSPARSER_Query *q)
193 {
194   struct InterceptLookupHandle* ilh;
195   
196   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
197               "Starting resolution for %s (type=%d)!\n",
198               q->name, q->type);
199   
200   ilh = GNUNET_malloc(sizeof(struct InterceptLookupHandle));
201   ilh->packet = p;
202   ilh->query = q;
203   ilh->request_handle = request;
204   
205   /* Start resolution in our zone */
206   gns_resolver_lookup_record(our_zone, q->type, q->name, &reply_to_dns, ilh);
207 }
208
209
210
211 /**
212  * The DNS request handler
213  * Called for every incoming DNS request.
214  *
215  * @param cls closure
216  * @param rh request handle to user for reply
217  * @param request_length number of bytes in request
218  * @param request udp payload of the DNS request
219  */
220 static void
221 handle_dns_request(void *cls,
222                    struct GNUNET_DNS_RequestHandle *rh,
223                    size_t request_length,
224                    const char *request)
225 {
226   struct GNUNET_DNSPARSER_Packet *p;
227   int i;
228   char *tldoffset;
229
230   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Hijacked a DNS request...processing\n");
231   p = GNUNET_DNSPARSER_parse (request, request_length);
232   
233   if (NULL == p)
234   {
235     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
236                 "Received malformed DNS packet, leaving it untouched\n");
237     GNUNET_DNS_request_forward (rh);
238     GNUNET_DNSPARSER_free_packet (p);
239     return;
240   }
241   
242   /**
243    * Check tld and decide if we or
244    * legacy dns is responsible
245    *
246    * FIXME now in theory there could be more than 1 query in the request
247    * but if this is case we get into trouble:
248    * either we query the GNS or the DNS. We cannot do both!
249    * So I suggest to either only allow a single query per request or
250    * only allow GNS or DNS requests.
251    * The way it is implemented here now is buggy and will lead to erratic
252    * behaviour (if multiple queries are present).
253    */
254   if (p->num_queries == 0)
255   {
256     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
257                 "No Queries in DNS packet... forwarding\n");
258     GNUNET_DNS_request_forward (rh);
259     GNUNET_DNSPARSER_free_packet(p);
260     return;
261   }
262
263   if (p->num_queries > 1)
264   {
265     /* Note: We could also look for .gnunet */
266     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
267                 ">1 queriy in DNS packet... odd. We only process #1\n");
268   }
269
270   
271   /**
272    * Check for .gnunet
273    */
274   tldoffset = p->queries[0].name + strlen(p->queries[0].name) - 1;
275   
276   for (i=0; i<strlen(p->queries[0].name); i++)
277   {
278     if (*(tldoffset-i) == '.')
279       break;
280   }
281
282   i--;
283   
284   if ((i==strlen(GNUNET_GNS_TLD)-1)
285       && (0 == strcmp(tldoffset-i, GNUNET_GNS_TLD)))
286   {
287     start_resolution_for_dns(rh, p, p->queries);
288   }
289   else
290   {
291     /**
292      * This request does not concern us. Forward to real DNS.
293      */
294     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
295                "Request for %s is forwarded to DNS\n", p->queries[0].name);
296     GNUNET_DNS_request_forward (rh);
297     GNUNET_DNSPARSER_free_packet (p);
298   }
299
300 }
301
302
303 int
304 gns_interceptor_init(GNUNET_HashCode zone,
305                      const struct GNUNET_CONFIGURATION_Handle *c)
306 {
307   GNUNET_log(GNUNET_ERROR_TYPE_INFO,
308              "DNS hijacking enabled... connecting to service.\n");
309
310   our_zone = zone;
311   /**
312    * Do gnunet dns init here
313    */
314   dns_handle = GNUNET_DNS_connect(c,
315                                   GNUNET_DNS_FLAG_PRE_RESOLUTION,
316                                   &handle_dns_request, /* rh */
317                                   NULL); /* Closure */
318   if (NULL == dns_handle)
319   {
320     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
321              "Failed to connect to the dnsservice!\n");
322     return GNUNET_SYSERR;
323   }
324
325   return GNUNET_YES;
326 }
327
328 /* end of gns_interceptor.c */