7b1ebab698e6467a8f141128cd706bdf69fb5f53
[oweals/gnunet.git] / src / vpn / gnunet-service-dns.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009 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  * @file vpn/gnunet-service-dns.c
23  * @author Philipp Toelke
24  */
25 #include "platform.h"
26 #include "gnunet_getopt_lib.h"
27 #include "gnunet_service_lib.h"
28 #include "gnunet_network_lib.h"
29 #include "gnunet_os_lib.h"
30 #include "gnunet-service-dns-p.h"
31 #include "gnunet_protocols.h"
32 #include "gnunet-vpn-packet.h"
33 #include "gnunet-vpn-pretty-print.h"
34 #include "gnunet_container_lib.h"
35 #include "gnunet-dns-parser.h"
36 #include "gnunet_dht_service.h"
37 #include "gnunet_block_lib.h"
38 #include "gnunet_block_dns.h"
39
40 struct dns_cls {
41         struct GNUNET_SCHEDULER_Handle *sched;
42
43         struct GNUNET_NETWORK_Handle *dnsout;
44
45         struct GNUNET_DHT_Handle *dht;
46
47         unsigned short dnsoutport;
48
49         struct answer_packet_list *head;
50         struct answer_packet_list *tail;
51 };
52 static struct dns_cls mycls;
53
54 struct dns_query_id_state {
55         unsigned valid:1;
56         struct GNUNET_SERVER_Client* client;
57         unsigned local_ip:32;
58         unsigned remote_ip:32;
59         unsigned local_port:16;
60 };
61 static struct dns_query_id_state query_states[65536]; /* This is < 1.5MiB */
62
63 void hijack(unsigned short port) {
64         char port_s[6];
65
66         GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Hijacking, port is %d\n", port);
67         snprintf(port_s, 6, "%d", port);
68         GNUNET_OS_start_process(NULL, NULL, "gnunet-helper-hijack-dns", "gnunet-hijack-dns", port_s, NULL);
69 }
70
71 void unhijack(unsigned short port) {
72         char port_s[6];
73
74         GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "unHijacking, port is %d\n", port);
75         snprintf(port_s, 6, "%d", port);
76         GNUNET_OS_start_process(NULL, NULL, "gnunet-helper-hijack-dns", "gnunet-hijack-dns", "-d", port_s, NULL);
77 }
78
79 size_t send_answer(void* cls, size_t size, void* buf);
80
81 struct receive_dht_cls {
82   unsigned short id;
83   struct GNUNET_DHT_GetHandle* handle;
84 };
85
86 void receive_dht(void *cls,
87                  struct GNUNET_TIME_Absolute exp,
88                  const GNUNET_HashCode *key,
89                  const struct GNUNET_PeerIdentity *const *get_path,
90                  const struct GNUNET_PeerIdentity *const *put_path,
91                  enum GNUNET_BLOCK_Type type,
92                  size_t size,
93                  const void *data)
94 {
95   unsigned short id = ((struct receive_dht_cls*)cls)->id;
96   struct GNUNET_DHT_GetHandle* handle = ((struct receive_dht_cls*)cls)->handle;
97   GNUNET_free(cls);
98
99   GNUNET_assert(type == GNUNET_BLOCK_TYPE_DNS);
100
101   if (query_states[id].valid != GNUNET_YES) return;
102   query_states[id].valid = GNUNET_NO;
103
104   const struct GNUNET_DNS_Record* rec = data;
105   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Got block of size %d, peer: %08x, desc: %08x\n", size, *((unsigned int*)&rec->peer), *((unsigned int*)&rec->service_descriptor));
106
107   size_t len = sizeof(struct answer_packet) + size - 1; /* 1 for the unsigned char data[1]; */
108   struct answer_packet_list* answer = GNUNET_malloc(len + 2*sizeof(struct answer_packet_list*));
109   answer->pkt.hdr.type = htons(GNUNET_MESSAGE_TYPE_LOCAL_RESPONSE_DNS);
110   answer->pkt.hdr.size = htons(len);
111   answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_SERVICE;
112
113   answer->pkt.from = query_states[id].remote_ip;
114
115   answer->pkt.to = query_states[id].local_ip;
116   answer->pkt.dst_port = query_states[id].local_port;
117
118   answer->pkt.id = id;
119
120   memcpy(answer->pkt.data, data, size);
121
122   GNUNET_CONTAINER_DLL_insert_after(mycls.head, mycls.tail, mycls.tail, answer);
123
124   GNUNET_SERVER_notify_transmit_ready(query_states[id].client,
125                                       len,
126                                       GNUNET_TIME_UNIT_FOREVER_REL,
127                                       &send_answer,
128                                       query_states[id].client);
129
130   GNUNET_DHT_get_stop(handle);
131 }
132
133 /**
134  * This receives the dns-payload from the daemon-vpn and sends it on over the udp-socket
135  */
136 void receive_query(void *cls, struct GNUNET_SERVER_Client *client, const struct GNUNET_MessageHeader *message)
137 {
138         struct query_packet* pkt = (struct query_packet*)message;
139         struct dns_pkt* dns = (struct dns_pkt*)pkt->data;
140         struct dns_pkt_parsed* pdns = parse_dns_packet(dns);
141
142         query_states[dns->s.id].valid = GNUNET_YES;
143         query_states[dns->s.id].client = client;
144         query_states[dns->s.id].local_ip = pkt->orig_from;
145         query_states[dns->s.id].local_port = pkt->src_port;
146         query_states[dns->s.id].remote_ip = pkt->orig_to;
147
148         if (pdns->queries[0]->namelen > 9 &&
149             0 == strncmp(pdns->queries[0]->name+(pdns->queries[0]->namelen - 9), ".gnunet.", 9)) {
150
151             GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Query for .gnunet!\n");
152             GNUNET_HashCode key;
153             GNUNET_CRYPTO_hash(pdns->queries[0]->name, pdns->queries[0]->namelen, &key);
154
155             GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Getting with key %08x, len is %d\n", *((unsigned int*)&key), pdns->queries[0]->namelen);
156
157             struct receive_dht_cls* cls = GNUNET_malloc(sizeof(struct receive_dht_cls));
158             cls->id = dns->s.id;
159
160             cls->handle = GNUNET_DHT_get_start(mycls.dht,
161                                  GNUNET_TIME_UNIT_MINUTES,
162                                  GNUNET_BLOCK_TYPE_DNS,
163                                  &key,
164                                  GNUNET_DHT_RO_NONE,
165                                  NULL,
166                                  0,
167                                  NULL,
168                                  0,
169                                  receive_dht,
170                                  cls);
171
172             goto out;
173         }
174
175         /* The query should be sent to the network */
176         GNUNET_free(pdns);
177
178         struct sockaddr_in dest;
179         memset(&dest, 0, sizeof dest);
180         dest.sin_port = htons(53);
181         dest.sin_addr.s_addr = pkt->orig_to;
182
183         /* int r = */ GNUNET_NETWORK_socket_sendto(mycls.dnsout, dns, ntohs(pkt->hdr.size) - sizeof(struct query_packet) + 1, (struct sockaddr*) &dest, sizeof dest);
184
185 out:
186         GNUNET_SERVER_receive_done(client, GNUNET_OK);
187 }
188
189 size_t send_answer(void* cls, size_t size, void* buf) {
190         struct answer_packet_list* query = mycls.head;
191         size_t len = ntohs(query->pkt.hdr.size);
192
193         GNUNET_assert(len <= size);
194
195         memcpy(buf, &query->pkt.hdr, len);
196
197         GNUNET_CONTAINER_DLL_remove (mycls.head, mycls.tail, query);
198
199         GNUNET_free(query);
200
201         if (mycls.head != NULL) {
202                 GNUNET_SERVER_notify_transmit_ready(cls, ntohs(mycls.head->pkt.hdr.size), GNUNET_TIME_UNIT_FOREVER_REL, &send_answer, cls);
203         }
204
205         return len;
206 }
207
208 static void read_response (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) {
209         unsigned char buf[65536];
210         struct dns_pkt* dns = (struct dns_pkt*)buf;
211
212         if (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)
213                 return;
214
215         struct sockaddr_in addr;
216         memset(&addr, 0, sizeof addr);
217         unsigned int addrlen = sizeof addr;
218
219         int r;
220         r = GNUNET_NETWORK_socket_recvfrom(mycls.dnsout, buf, 65536, (struct sockaddr*)&addr, &addrlen);
221
222         /* if (r < 0) TODO */
223
224         if (query_states[dns->s.id].valid == GNUNET_YES) {
225                 query_states[dns->s.id].valid = GNUNET_NO;
226
227                 size_t len = sizeof(struct answer_packet) + r - 1; /* 1 for the unsigned char data[1]; */
228                 struct answer_packet_list* answer = GNUNET_malloc(len + 2*sizeof(struct answer_packet_list*));
229                 answer->pkt.hdr.type = htons(GNUNET_MESSAGE_TYPE_LOCAL_RESPONSE_DNS);
230                 answer->pkt.hdr.size = htons(len);
231                 answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_IP;
232                 answer->pkt.from = addr.sin_addr.s_addr;
233                 answer->pkt.to = query_states[dns->s.id].local_ip;
234                 answer->pkt.dst_port = query_states[dns->s.id].local_port;
235                 memcpy(answer->pkt.data, buf, r);
236
237                 GNUNET_CONTAINER_DLL_insert_after(mycls.head, mycls.tail, mycls.tail, answer);
238
239                 /* struct GNUNET_CONNECTION_TransmitHandle* th = */ GNUNET_SERVER_notify_transmit_ready(query_states[dns->s.id].client, len, GNUNET_TIME_UNIT_FOREVER_REL, &send_answer, query_states[dns->s.id].client);
240         }
241
242         GNUNET_SCHEDULER_add_read_net(mycls.sched, GNUNET_TIME_UNIT_FOREVER_REL, mycls.dnsout, &read_response, NULL);
243 }
244
245
246 /**
247  * Task run during shutdown.
248  *
249  * @param cls unused
250  * @param tc unused
251  */
252 static void
253 cleanup_task (void *cls,
254               const struct GNUNET_SCHEDULER_TaskContext *tc)
255 {
256         unhijack(mycls.dnsoutport);
257         GNUNET_DHT_disconnect(mycls.dht);
258 }
259
260 static void
261 publish_name (void *cls,
262              const struct GNUNET_SCHEDULER_TaskContext *tc)
263 {
264   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
265     return;
266
267   char* name = "philipptoelke.gnunet.";
268   size_t size = sizeof(struct GNUNET_DNS_Record) + strlen(name);
269   struct GNUNET_DNS_Record *data = alloca(size);
270   memset(data, 0, size);
271   memcpy(data->name, name, strlen(name) + 1);
272   data->namelen = strlen(name) + 1;
273   *((unsigned int*)&data->service_descriptor) = 0x11223344;
274   *((unsigned int*)&data->peer) = 0x55667788;
275
276   GNUNET_HashCode key;
277   GNUNET_CRYPTO_hash(name, strlen(name)+1, &key);
278   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Putting with key %08x, len is %d\n", *((unsigned int*)&key), strlen(name));
279
280   GNUNET_DHT_put(mycls.dht,
281                       &key,
282                       GNUNET_DHT_RO_NONE,
283                       GNUNET_BLOCK_TYPE_DNS,
284                       size,
285                       (char*)data,
286                       GNUNET_TIME_relative_to_absolute(GNUNET_TIME_UNIT_HOURS),
287                       GNUNET_TIME_UNIT_MINUTES,
288                       NULL,
289                       NULL);
290
291   GNUNET_SCHEDULER_add_delayed (mycls.sched, GNUNET_TIME_UNIT_HOURS, publish_name, NULL);
292 }
293
294 /**
295  * @param cls closure
296  * @param sched scheduler to use
297  * @param server the initialized server
298  * @param cfg configuration to use
299  */
300 static void
301 run (void *cls,
302      struct GNUNET_SCHEDULER_Handle *sched,
303      struct GNUNET_SERVER_Handle *server,
304      const struct GNUNET_CONFIGURATION_Handle *cfg)
305 {
306   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
307           /* callback, cls, type, size */
308     {&receive_query, NULL, GNUNET_MESSAGE_TYPE_LOCAL_QUERY_DNS, 0},
309     {NULL, NULL, 0, 0}
310   };
311
312   {
313   int i;
314   for (i = 0; i < 65536; i++) {
315     query_states[i].valid = GNUNET_NO;
316   }
317   }
318
319   mycls.dht = GNUNET_DHT_connect(sched, cfg, 1024);
320
321   struct sockaddr_in addr;
322
323   mycls.sched = sched;
324   mycls.dnsout = GNUNET_NETWORK_socket_create (AF_INET, SOCK_DGRAM, 0);
325   if (mycls.dnsout == NULL) 
326     return;
327   memset(&addr, 0, sizeof(struct sockaddr_in));
328
329   int err = GNUNET_NETWORK_socket_bind (mycls.dnsout,
330                                         (struct sockaddr*)&addr, 
331                                         sizeof(struct sockaddr_in));
332
333   if (err != GNUNET_YES) {
334         GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not bind a port, exiting\n");
335         return;
336   }
337   socklen_t addrlen = sizeof(struct sockaddr_in);
338   err = getsockname(GNUNET_NETWORK_get_fd(mycls.dnsout),
339                     (struct sockaddr*) &addr, 
340                     &addrlen);
341
342   mycls.dnsoutport = htons(addr.sin_port);
343
344   hijack(htons(addr.sin_port));
345
346   GNUNET_SCHEDULER_add_now (mycls.sched, publish_name, NULL);
347
348         GNUNET_SCHEDULER_add_read_net(sched, GNUNET_TIME_UNIT_FOREVER_REL, mycls.dnsout, &read_response, NULL);
349
350   GNUNET_SERVER_add_handlers (server, handlers);
351   GNUNET_SCHEDULER_add_delayed (sched,
352                   GNUNET_TIME_UNIT_FOREVER_REL,
353                   &cleanup_task,
354                   cls);
355 }
356
357 /**
358  * The main function for the dns service.
359  *
360  * @param argc number of arguments from the command line
361  * @param argv command line arguments
362  * @return 0 ok, 1 on error
363  */
364 int
365 main (int argc, char *const *argv)
366 {
367   return (GNUNET_OK ==
368           GNUNET_SERVICE_run (argc,
369                               argv,
370                               "dns",
371                               GNUNET_SERVICE_OPTION_NONE,
372                               &run, NULL)) ? 0 : 1;
373 }