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