remove unneeded field
[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 "block_dns.h"
39 #include "gnunet_crypto_lib.h"
40 #include "gnunet_signatures.h"
41
42 struct dns_cls {
43         struct GNUNET_SCHEDULER_Handle *sched;
44
45         struct GNUNET_NETWORK_Handle *dnsout;
46
47         struct GNUNET_DHT_Handle *dht;
48
49         unsigned short dnsoutport;
50
51         const struct GNUNET_CONFIGURATION_Handle *cfg;
52
53         struct answer_packet_list *head;
54         struct answer_packet_list *tail;
55 };
56 static struct dns_cls mycls;
57
58 struct dns_query_id_state {
59         unsigned valid:1;
60         struct GNUNET_SERVER_Client* client;
61         unsigned local_ip:32;
62         unsigned remote_ip:32;
63         unsigned local_port:16;
64         char* name;
65         unsigned namelen:8;
66 };
67 static struct dns_query_id_state query_states[65536]; /* This is < 1.5MiB */
68
69 void hijack(unsigned short port) {
70         char port_s[6];
71
72         GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Hijacking, port is %d\n", port);
73         snprintf(port_s, 6, "%d", port);
74         GNUNET_OS_start_process(NULL, NULL, "gnunet-helper-hijack-dns", "gnunet-hijack-dns", port_s, NULL);
75 }
76
77 void unhijack(unsigned short port) {
78         char port_s[6];
79
80         GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "unHijacking, port is %d\n", port);
81         snprintf(port_s, 6, "%d", port);
82         GNUNET_OS_start_process(NULL, NULL, "gnunet-helper-hijack-dns", "gnunet-hijack-dns", "-d", port_s, NULL);
83 }
84
85 size_t send_answer(void* cls, size_t size, void* buf);
86
87 struct receive_dht_cls {
88   unsigned short id;
89   struct GNUNET_DHT_GetHandle* handle;
90 };
91
92 void receive_dht(void *cls,
93                  struct GNUNET_TIME_Absolute exp,
94                  const GNUNET_HashCode *key,
95                  const struct GNUNET_PeerIdentity *const *get_path,
96                  const struct GNUNET_PeerIdentity *const *put_path,
97                  enum GNUNET_BLOCK_Type type,
98                  size_t size,
99                  const void *data)
100 {
101   unsigned short id = ((struct receive_dht_cls*)cls)->id;
102   struct GNUNET_DHT_GetHandle* handle = ((struct receive_dht_cls*)cls)->handle;
103   GNUNET_free(cls);
104
105   GNUNET_assert(type == GNUNET_BLOCK_TYPE_DNS);
106
107   if (query_states[id].valid != GNUNET_YES) return;
108   query_states[id].valid = GNUNET_NO;
109
110   const struct GNUNET_DNS_Record* rec = data;
111   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));
112
113   size_t len = sizeof(struct answer_packet) - 1 \
114                + sizeof(struct dns_static) \
115                + query_states[id].namelen \
116                + sizeof(struct dns_query_line) \
117                + 2 /* To hold the pointer to the name */ \
118                + sizeof(struct dns_record_line) - 1 \
119                + 16;
120                ;
121   struct answer_packet_list* answer = GNUNET_malloc(len + 2*sizeof(struct answer_packet_list*));
122   memset(answer, 0, len + 2*sizeof(struct answer_packet_list*));
123
124   answer->pkt.hdr.type = htons(GNUNET_MESSAGE_TYPE_LOCAL_RESPONSE_DNS);
125   answer->pkt.hdr.size = htons(len);
126   answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_SERVICE;
127
128   GNUNET_CRYPTO_hash(&rec->peer, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), &answer->pkt.peer);
129   memcpy(&answer->pkt.service_descriptor, &rec->service_descriptor, sizeof(GNUNET_HashCode));
130   memcpy(&answer->pkt.service_type, &rec->service_type, sizeof(answer->pkt.service_type));
131   memcpy(&answer->pkt.ports, &rec->ports, sizeof(answer->pkt.ports));
132
133   answer->pkt.from = query_states[id].remote_ip;
134
135   answer->pkt.to = query_states[id].local_ip;
136   answer->pkt.dst_port = query_states[id].local_port;
137
138   struct dns_pkt *dpkt = (struct dns_pkt*)answer->pkt.data;
139
140   dpkt->s.id = id;
141   dpkt->s.aa = 1;
142   dpkt->s.qr = 1;
143   dpkt->s.ra = 1;
144   dpkt->s.qdcount = htons(1);
145   dpkt->s.ancount = htons(1);
146
147   memcpy(dpkt->data, query_states[id].name, query_states[id].namelen);
148   GNUNET_free(query_states[id].name);
149
150
151   struct dns_query_line* dque = (struct dns_query_line*)(dpkt->data+(query_states[id].namelen));
152   dque->type = htons(28);
153   dque->class = htons(1);
154
155   char* anname = (char*)(dpkt->data+(query_states[id].namelen)+sizeof(struct dns_query_line));
156   memcpy(anname, (char[]){0xc0, 0x0c}, 2);
157
158   struct dns_record_line *drec_data = (struct dns_record_line*)(dpkt->data+(query_states[id].namelen)+sizeof(struct dns_query_line)+2);
159   drec_data->type = htons(28); /* AAAA */
160   drec_data->class = htons(1); /* IN */
161   drec_data->ttl = htonl(3600); /* FIXME: read from block */
162   drec_data->data_len = htons(16);
163
164   answer->pkt.addroffset = htons((unsigned short)((unsigned long)(&drec_data->data)-(unsigned long)(&answer->pkt)));
165
166   GNUNET_CONTAINER_DLL_insert_after(mycls.head, mycls.tail, mycls.tail, answer);
167
168   GNUNET_SERVER_notify_transmit_ready(query_states[id].client,
169                                       len,
170                                       GNUNET_TIME_UNIT_FOREVER_REL,
171                                       &send_answer,
172                                       query_states[id].client);
173
174   GNUNET_DHT_get_stop(handle);
175 }
176
177 /**
178  * This receives a GNUNET_MESSAGE_TYPE_REHIJACK and rehijacks the DNS
179  */
180 void rehijack(void *cls, struct GNUNET_SERVER_Client *client, const struct GNUNET_MessageHeader *message) {
181     unhijack(mycls.dnsoutport);
182     hijack(mycls.dnsoutport);
183 }
184
185 /**
186  * This receives the dns-payload from the daemon-vpn and sends it on over the udp-socket
187  */
188 void receive_query(void *cls, struct GNUNET_SERVER_Client *client, const struct GNUNET_MessageHeader *message)
189 {
190         struct query_packet* pkt = (struct query_packet*)message;
191         struct dns_pkt* dns = (struct dns_pkt*)pkt->data;
192         struct dns_pkt_parsed* pdns = parse_dns_packet(dns);
193
194         query_states[dns->s.id].valid = GNUNET_YES;
195         query_states[dns->s.id].client = client;
196         query_states[dns->s.id].local_ip = pkt->orig_from;
197         query_states[dns->s.id].local_port = pkt->src_port;
198         query_states[dns->s.id].remote_ip = pkt->orig_to;
199         query_states[dns->s.id].namelen = strlen((char*)dns->data) + 1;
200         query_states[dns->s.id].name = GNUNET_malloc(query_states[dns->s.id].namelen);
201         memcpy(query_states[dns->s.id].name, dns->data, query_states[dns->s.id].namelen);
202
203         if (pdns->queries[0]->namelen > 9 &&
204             0 == strncmp(pdns->queries[0]->name+(pdns->queries[0]->namelen - 9), ".gnunet.", 9)) {
205
206             GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Query for .gnunet!\n");
207             GNUNET_HashCode key;
208             GNUNET_CRYPTO_hash(pdns->queries[0]->name, pdns->queries[0]->namelen, &key);
209
210             GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Getting with key %08x, len is %d\n", *((unsigned int*)&key), pdns->queries[0]->namelen);
211
212             struct receive_dht_cls* cls = GNUNET_malloc(sizeof(struct receive_dht_cls));
213             cls->id = dns->s.id;
214
215             cls->handle = GNUNET_DHT_get_start(mycls.dht,
216                                  GNUNET_TIME_UNIT_MINUTES,
217                                  GNUNET_BLOCK_TYPE_DNS,
218                                  &key,
219                                  GNUNET_DHT_RO_NONE,
220                                  NULL,
221                                  0,
222                                  NULL,
223                                  0,
224                                  receive_dht,
225                                  cls);
226
227             goto out;
228         }
229
230         /* The query should be sent to the network */
231         GNUNET_free(pdns);
232
233         struct sockaddr_in dest;
234         memset(&dest, 0, sizeof dest);
235         dest.sin_port = htons(53);
236         dest.sin_addr.s_addr = pkt->orig_to;
237
238         /* int r = */ GNUNET_NETWORK_socket_sendto(mycls.dnsout, dns, ntohs(pkt->hdr.size) - sizeof(struct query_packet) + 1, (struct sockaddr*) &dest, sizeof dest);
239
240 out:
241         GNUNET_SERVER_receive_done(client, GNUNET_OK);
242 }
243
244 size_t send_answer(void* cls, size_t size, void* buf) {
245         struct answer_packet_list* query = mycls.head;
246         size_t len = ntohs(query->pkt.hdr.size);
247
248         GNUNET_assert(len <= size);
249
250         memcpy(buf, &query->pkt.hdr, len);
251
252         GNUNET_CONTAINER_DLL_remove (mycls.head, mycls.tail, query);
253
254         GNUNET_free(query);
255
256         if (mycls.head != NULL) {
257                 GNUNET_SERVER_notify_transmit_ready(cls, ntohs(mycls.head->pkt.hdr.size), GNUNET_TIME_UNIT_FOREVER_REL, &send_answer, cls);
258         }
259
260         return len;
261 }
262
263 static void read_response (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) {
264         unsigned char buf[65536];
265         struct dns_pkt* dns = (struct dns_pkt*)buf;
266
267         if (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)
268                 return;
269
270         struct sockaddr_in addr;
271         memset(&addr, 0, sizeof addr);
272         unsigned int addrlen = sizeof addr;
273
274         int r;
275         r = GNUNET_NETWORK_socket_recvfrom(mycls.dnsout, buf, 65536, (struct sockaddr*)&addr, &addrlen);
276
277         /* if (r < 0) TODO */
278
279         if (query_states[dns->s.id].valid == GNUNET_YES) {
280                 query_states[dns->s.id].valid = GNUNET_NO;
281
282                 size_t len = sizeof(struct answer_packet) + r - 1; /* 1 for the unsigned char data[1]; */
283                 struct answer_packet_list* answer = GNUNET_malloc(len + 2*sizeof(struct answer_packet_list*));
284                 answer->pkt.hdr.type = htons(GNUNET_MESSAGE_TYPE_LOCAL_RESPONSE_DNS);
285                 answer->pkt.hdr.size = htons(len);
286                 answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_IP;
287                 answer->pkt.from = addr.sin_addr.s_addr;
288                 answer->pkt.to = query_states[dns->s.id].local_ip;
289                 answer->pkt.dst_port = query_states[dns->s.id].local_port;
290                 memcpy(answer->pkt.data, buf, r);
291
292                 GNUNET_CONTAINER_DLL_insert_after(mycls.head, mycls.tail, mycls.tail, answer);
293
294                 /* 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);
295         }
296
297         GNUNET_SCHEDULER_add_read_net(mycls.sched, GNUNET_TIME_UNIT_FOREVER_REL, mycls.dnsout, &read_response, NULL);
298 }
299
300
301 /**
302  * Task run during shutdown.
303  *
304  * @param cls unused
305  * @param tc unused
306  */
307 static void
308 cleanup_task (void *cls,
309               const struct GNUNET_SCHEDULER_TaskContext *tc)
310 {
311         unhijack(mycls.dnsoutport);
312         GNUNET_DHT_disconnect(mycls.dht);
313 }
314
315 static void
316 publish_name (void *cls,
317              const struct GNUNET_SCHEDULER_TaskContext *tc)
318 {
319   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
320     return;
321
322   char* name = "philipptoelke.gnunet.";
323   size_t size = sizeof(struct GNUNET_DNS_Record);
324   struct GNUNET_DNS_Record data;
325   memset(&data, 0, size);
326
327   data.purpose.size = htonl(size - sizeof(struct GNUNET_CRYPTO_RsaSignature));
328   data.purpose.purpose = GNUNET_SIGNATURE_PURPOSE_DNS_RECORD;
329
330   GNUNET_CRYPTO_hash(name, strlen(name)+1, &data.service_descriptor);
331
332   char* keyfile;
333   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename(mycls.cfg, "GNUNETD",
334                                                            "HOSTKEY", &keyfile))
335     {
336       GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "could not read keyfile-value\n");
337       if (keyfile != NULL) GNUNET_free(keyfile);
338       return;
339     }
340
341   struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file(keyfile);
342   GNUNET_free(keyfile);
343
344   GNUNET_CRYPTO_rsa_key_get_public(my_private_key, &data.peer);
345
346   data.expiration_time = GNUNET_TIME_relative_to_absolute(GNUNET_TIME_UNIT_HOURS);
347
348   /* Sign the block
349    */
350
351   if (GNUNET_OK != GNUNET_CRYPTO_rsa_sign(my_private_key,
352                                           &data.purpose,
353                                           &data.signature))
354     {
355       GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "could not sign DNS_Record\n");
356       return;
357     }
358   GNUNET_free(my_private_key);
359
360   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Putting with key %08x\n", *((unsigned int*)&data.service_descriptor));
361
362   GNUNET_DHT_put(mycls.dht,
363                  &data.service_descriptor,
364                  GNUNET_DHT_RO_NONE,
365                  GNUNET_BLOCK_TYPE_DNS,
366                  size,
367                  (char*)&data,
368                  GNUNET_TIME_relative_to_absolute(GNUNET_TIME_UNIT_HOURS),
369                  GNUNET_TIME_UNIT_MINUTES,
370                  NULL,
371                  NULL);
372
373   GNUNET_SCHEDULER_add_delayed (mycls.sched, GNUNET_TIME_UNIT_HOURS, publish_name, NULL);
374 }
375
376 /**
377  * @param cls closure
378  * @param sched scheduler to use
379  * @param server the initialized server
380  * @param cfg configuration to use
381  */
382 static void
383 run (void *cls,
384      struct GNUNET_SCHEDULER_Handle *sched,
385      struct GNUNET_SERVER_Handle *server,
386      const struct GNUNET_CONFIGURATION_Handle *cfg)
387 {
388   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
389           /* callback, cls, type, size */
390     {&receive_query, NULL, GNUNET_MESSAGE_TYPE_LOCAL_QUERY_DNS, 0},
391     {&rehijack, NULL, GNUNET_MESSAGE_TYPE_REHIJACK, sizeof(struct GNUNET_MessageHeader)},
392     {NULL, NULL, 0, 0}
393   };
394
395   mycls.cfg = cfg;
396
397   {
398   int i;
399   for (i = 0; i < 65536; i++) {
400     query_states[i].valid = GNUNET_NO;
401   }
402   }
403
404   mycls.dht = GNUNET_DHT_connect(sched, cfg, 1024);
405
406   struct sockaddr_in addr;
407
408   mycls.sched = sched;
409   mycls.dnsout = GNUNET_NETWORK_socket_create (AF_INET, SOCK_DGRAM, 0);
410   if (mycls.dnsout == NULL) 
411     return;
412   memset(&addr, 0, sizeof(struct sockaddr_in));
413
414   int err = GNUNET_NETWORK_socket_bind (mycls.dnsout,
415                                         (struct sockaddr*)&addr, 
416                                         sizeof(struct sockaddr_in));
417
418   if (err != GNUNET_YES) {
419         GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not bind a port, exiting\n");
420         return;
421   }
422   socklen_t addrlen = sizeof(struct sockaddr_in);
423   err = getsockname(GNUNET_NETWORK_get_fd(mycls.dnsout),
424                     (struct sockaddr*) &addr, 
425                     &addrlen);
426
427   mycls.dnsoutport = htons(addr.sin_port);
428
429   hijack(htons(addr.sin_port));
430
431   GNUNET_SCHEDULER_add_now (mycls.sched, publish_name, NULL);
432
433         GNUNET_SCHEDULER_add_read_net(sched, GNUNET_TIME_UNIT_FOREVER_REL, mycls.dnsout, &read_response, NULL);
434
435   GNUNET_SERVER_add_handlers (server, handlers);
436   GNUNET_SCHEDULER_add_delayed (sched,
437                   GNUNET_TIME_UNIT_FOREVER_REL,
438                   &cleanup_task,
439                   cls);
440 }
441
442 /**
443  * The main function for the dns service.
444  *
445  * @param argc number of arguments from the command line
446  * @param argv command line arguments
447  * @return 0 ok, 1 on error
448  */
449 int
450 main (int argc, char *const *argv)
451 {
452   return (GNUNET_OK ==
453           GNUNET_SERVICE_run (argc,
454                               argv,
455                               "dns",
456                               GNUNET_SERVICE_OPTION_NONE,
457                               &run, NULL)) ? 0 : 1;
458 }