aboutsummaryrefslogtreecommitdiff
path: root/src/dns/gnunet-service-dns.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2012-01-01 23:00:59 +0000
committerChristian Grothoff <christian@grothoff.org>2012-01-01 23:00:59 +0000
commit66ffc809472f27d69b9ad7361f8ba29c2674f716 (patch)
tree609623cb79291939f9cb81a8858853a202dae2ca /src/dns/gnunet-service-dns.c
parent131c43b2b18b12e52ff045e51025706802cbd2e2 (diff)
downloadgnunet-66ffc809472f27d69b9ad7361f8ba29c2674f716.tar.gz
gnunet-66ffc809472f27d69b9ad7361f8ba29c2674f716.zip
-moving DNS code into its own directory
Diffstat (limited to 'src/dns/gnunet-service-dns.c')
-rw-r--r--src/dns/gnunet-service-dns.c1730
1 files changed, 1730 insertions, 0 deletions
diff --git a/src/dns/gnunet-service-dns.c b/src/dns/gnunet-service-dns.c
new file mode 100644
index 000000000..ee42a7001
--- /dev/null
+++ b/src/dns/gnunet-service-dns.c
@@ -0,0 +1,1730 @@
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_constants.h>
29#include "gnunet_network_lib.h"
30#include "gnunet_os_lib.h"
31#include "gnunet_dns_service.h"
32#include "gnunet_connection_lib.h"
33#include "gnunet_protocols.h"
34#include "gnunet_applications.h"
35#include "gnunet_container_lib.h"
36#include "gnunet_dnsparser_lib.h"
37#include "gnunet_dht_service.h"
38#include "gnunet_block_lib.h"
39#include "block_dns.h"
40#include "gnunet_crypto_lib.h"
41#include "gnunet_mesh_service.h"
42#include "gnunet_signatures.h"
43
44struct GNUNET_MESH_Handle *mesh_handle;
45
46struct GNUNET_CONNECTION_TransmitHandle *server_notify;
47
48/**
49 * The UDP-Socket through which DNS-Resolves will be sent if they are not to be
50 * sent through gnunet. The port of this socket will not be hijacked.
51 */
52static struct GNUNET_NETWORK_Handle *dnsout;
53static struct GNUNET_NETWORK_Handle *dnsout6;
54
55/**
56 * The port bound to the socket dnsout
57 */
58static unsigned short dnsoutport;
59
60/**
61 * A handle to the DHT-Service
62 */
63static struct GNUNET_DHT_Handle *dht;
64
65/**
66 * The configuration to use
67 */
68static const struct GNUNET_CONFIGURATION_Handle *cfg;
69
70/**
71 * A list of DNS-Responses that have to be sent to the requesting client
72 */
73static struct answer_packet_list *head;
74
75/**
76 * The tail of the list of DNS-responses
77 */
78static struct answer_packet_list *tail;
79
80/**
81 * A structure containing a mapping from network-byte-ordered DNS-id (16 bit) to
82 * some information needed to handle this query
83 *
84 * It currently allocates at least
85 * (1 + machine-width + machine-width + 32 + 32 + 16 + machine-width + 8) * 65536 bit
86 * = 17 MiB on 64 bit.
87 * = 11 MiB on 32 bit.
88 */
89static struct
90{
91 unsigned valid:1;
92 struct GNUNET_SERVER_Client *client;
93 struct GNUNET_MESH_Tunnel *tunnel;
94 char local_ip[16];
95 char remote_ip[16];
96 char addrlen;
97 uint16_t local_port;
98 char *name;
99 uint8_t namelen;
100 uint16_t qtype;
101} query_states[UINT16_MAX + 1];
102
103/**
104 * A struct used to give more than one value as
105 * closure to receive_dht
106 */
107struct receive_dht_cls
108{
109 uint16_t id;
110 struct GNUNET_DHT_GetHandle *handle;
111};
112
113struct tunnel_notify_queue
114{
115 struct tunnel_notify_queue *next;
116 struct tunnel_notify_queue *prev;
117 void *cls;
118 size_t len;
119 GNUNET_CONNECTION_TransmitReadyNotify cb;
120};
121
122struct tunnel_state
123{
124 struct tunnel_notify_queue *head, *tail;
125 struct GNUNET_MESH_TransmitHandle *th;
126};
127
128static size_t
129send_answer (void *cls, size_t size, void *buf);
130
131static void
132client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
133{
134 if (NULL == head)
135 return;
136
137 if (head->client == client)
138 {
139 GNUNET_CONNECTION_notify_transmit_ready_cancel (server_notify);
140 server_notify =
141 GNUNET_SERVER_notify_transmit_ready (head->next->client,
142 ntohs (head->next->pkt.hdr.size),
143 GNUNET_TIME_UNIT_FOREVER_REL,
144 &send_answer, NULL);
145 }
146
147 struct answer_packet_list *element = head;
148
149 while (element != NULL)
150 {
151 if (element->client == client)
152 {
153 GNUNET_SERVER_client_drop (client);
154 GNUNET_CONTAINER_DLL_remove (head, tail, element);
155 struct answer_packet_list *t = element;
156
157 element = element->next;
158 GNUNET_free (t);
159 }
160 else
161 element = element->next;
162 }
163}
164
165/**
166 * Hijack all outgoing DNS-Traffic but for traffic leaving "our" port.
167 */
168static void
169hijack (void *cls GNUNET_UNUSED, const struct GNUNET_SCHEDULER_TaskContext *tc)
170{
171 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
172 return;
173
174 if (0 == dnsoutport)
175 {
176 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
177 "Delaying the hijacking, port is still %d!\n", dnsoutport);
178 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &hijack, NULL);
179 return;
180 }
181
182 char port_s[6];
183 char *virt_dns;
184 struct GNUNET_OS_Process *proc;
185
186 if (GNUNET_SYSERR ==
187 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "VIRTDNS", &virt_dns))
188 {
189 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
190 "No entry 'VIRTDNS' in configuration!\n");
191 exit (1);
192 }
193
194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Hijacking, port is %d\n", dnsoutport);
195 snprintf (port_s, 6, "%d", dnsoutport);
196 if (NULL !=
197 (proc =
198 GNUNET_OS_start_process (NULL, NULL, "gnunet-helper-hijack-dns",
199 "gnunet-hijack-dns", port_s, virt_dns, NULL)))
200 {
201 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (proc));
202 GNUNET_OS_process_close (proc);
203 }
204 GNUNET_free (virt_dns);
205}
206
207static void *
208new_tunnel (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
209 const struct GNUNET_PeerIdentity *initiator GNUNET_UNUSED,
210 const struct GNUNET_ATS_Information *ats GNUNET_UNUSED)
211{
212 struct tunnel_state *s = GNUNET_malloc (sizeof *s);
213
214 s->head = NULL;
215 s->tail = NULL;
216 s->th = NULL;
217 return s;
218}
219
220static void
221clean_tunnel (void *cls GNUNET_UNUSED, const struct GNUNET_MESH_Tunnel *tunnel,
222 void *tunnel_ctx)
223{
224 GNUNET_free (tunnel_ctx);
225}
226
227/**
228 * Delete the hijacking-routes
229 */
230static void
231unhijack (unsigned short port)
232{
233 char port_s[6];
234 char *virt_dns;
235 struct GNUNET_OS_Process *proc;
236
237 if (GNUNET_SYSERR ==
238 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "VIRTDNS", &virt_dns))
239 {
240 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
241 "No entry 'VIRTDNS' in configuration!\n");
242 exit (1);
243 }
244
245 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "unHijacking, port is %d\n", port);
246 snprintf (port_s, 6, "%d", port);
247 if (NULL !=
248 (proc =
249 GNUNET_OS_start_process (NULL, NULL, "gnunet-helper-hijack-dns",
250 "gnunet-hijack-dns", "-d", port_s, virt_dns,
251 NULL)))
252 {
253 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (proc));
254 GNUNET_OS_process_close (proc);
255 }
256 GNUNET_free (virt_dns);
257}
258
259/**
260 * Send the DNS-Response to the client. Gets called via the notify_transmit_ready-
261 * system.
262 */
263static size_t
264send_answer (void *cls, size_t size, void *buf)
265{
266 server_notify = NULL;
267 struct answer_packet_list *query = head;
268 size_t len = ntohs (query->pkt.hdr.size);
269
270 GNUNET_assert (len <= size);
271
272 memcpy (buf, &query->pkt.hdr, len);
273
274 GNUNET_CONTAINER_DLL_remove (head, tail, query);
275
276 /* When more data is to be sent, reschedule */
277 if (head != NULL)
278 server_notify =
279 GNUNET_SERVER_notify_transmit_ready (head->client,
280 ntohs (head->pkt.hdr.size),
281 GNUNET_TIME_UNIT_FOREVER_REL,
282 &send_answer, NULL);
283
284 GNUNET_SERVER_client_drop (query->client);
285 GNUNET_free (query);
286 return len;
287}
288
289GNUNET_NETWORK_STRUCT_BEGIN
290
291struct tunnel_cls
292{
293 struct GNUNET_MESH_Tunnel *tunnel GNUNET_PACKED;
294 struct GNUNET_MessageHeader hdr;
295 struct dns_pkt dns;
296};
297GNUNET_NETWORK_STRUCT_END
298
299struct tunnel_cls *remote_pending[UINT16_MAX];
300
301static size_t
302mesh_send_response (void *cls, size_t size, void *buf)
303{
304 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
305 struct GNUNET_MessageHeader *hdr = buf;
306 uint32_t *sz = cls;
307 struct GNUNET_MESH_Tunnel **tunnel = (struct GNUNET_MESH_Tunnel **) (sz + 1);
308 struct dns_pkt *dns = (struct dns_pkt *) (tunnel + 1);
309
310 GNUNET_MESH_tunnel_set_data (*tunnel, NULL);
311
312 hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_REMOTE_ANSWER_DNS);
313 hdr->size = htons (*sz + sizeof (struct GNUNET_MessageHeader));
314
315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
316 "Sending response, size=%d, sz=%d, sz+hdr=%d\n", size, *sz,
317 *sz + sizeof (struct GNUNET_MessageHeader));
318
319 GNUNET_assert (size >= (*sz + sizeof (struct GNUNET_MessageHeader)));
320
321 memcpy (hdr + 1, dns, *sz);
322 struct tunnel_state *s = GNUNET_MESH_tunnel_get_data (*tunnel);
323
324 if (NULL != s->head)
325 {
326 struct tunnel_notify_queue *element = s->head;
327 struct tunnel_notify_queue *head = s->head;
328 struct tunnel_notify_queue *tail = s->tail;
329
330 GNUNET_CONTAINER_DLL_remove (head, tail, element);
331
332 s->th =
333 GNUNET_MESH_notify_transmit_ready (*tunnel, GNUNET_NO, 42,
334 GNUNET_TIME_relative_divide
335 (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
336 (const struct GNUNET_PeerIdentity *)
337 NULL, element->len, element->cb,
338 element->cls);
339 }
340
341 GNUNET_free (cls);
342
343 return ntohs (hdr->size);
344}
345
346static size_t
347mesh_send (void *cls, size_t size, void *buf)
348{
349 struct tunnel_cls *cls_ = (struct tunnel_cls *) cls;
350
351 GNUNET_MESH_tunnel_set_data (cls_->tunnel, NULL);
352
353 GNUNET_assert (cls_->hdr.size <= size);
354
355 size = cls_->hdr.size;
356 cls_->hdr.size = htons (cls_->hdr.size);
357
358 memcpy (buf, &cls_->hdr, size);
359
360 struct tunnel_state *s = GNUNET_MESH_tunnel_get_data (cls_->tunnel);
361
362 if (NULL != s->head)
363 {
364 struct tunnel_notify_queue *element = s->head;
365 struct tunnel_notify_queue *head = s->head;
366 struct tunnel_notify_queue *tail = s->tail;;
367
368 GNUNET_CONTAINER_DLL_remove (head, tail, element);
369
370 s->th =
371 GNUNET_MESH_notify_transmit_ready (cls_->tunnel, GNUNET_NO, 42,
372 GNUNET_TIME_relative_divide
373 (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
374 (const struct GNUNET_PeerIdentity *)
375 NULL, element->len, element->cb,
376 element->cls);
377
378 GNUNET_free (element);
379 }
380
381 return size;
382}
383
384
385void
386mesh_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
387 const struct GNUNET_ATS_Information *atsi GNUNET_UNUSED)
388{
389 if (NULL == peer)
390 return;
391 struct tunnel_cls *cls_ = (struct tunnel_cls *) cls;
392
393 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
394 "Connected to peer %s, %x, sending query with id %d\n",
395 GNUNET_i2s (peer), peer, ntohs (cls_->dns.s.id));
396
397 struct tunnel_state *s = GNUNET_MESH_tunnel_get_data (cls_->tunnel);
398
399 if (NULL == s->head)
400 {
401 s->th =
402 GNUNET_MESH_notify_transmit_ready (cls_->tunnel, GNUNET_YES, 42,
403 GNUNET_TIME_UNIT_MINUTES, NULL,
404 cls_->hdr.size, mesh_send, cls);
405
406 }
407 else
408 {
409 struct tunnel_notify_queue *head = s->head;
410 struct tunnel_notify_queue *tail = s->tail;
411
412 struct tunnel_notify_queue *element =
413 GNUNET_malloc (sizeof (struct tunnel_notify_queue));
414 element->cls = cls;
415 element->len = cls_->hdr.size;
416 element->cb = mesh_send;
417
418 GNUNET_CONTAINER_DLL_insert_tail (head, tail, element);
419 }
420}
421
422
423static void
424send_mesh_query (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
425{
426 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
427 return;
428
429 struct tunnel_cls *cls_ = (struct tunnel_cls *) cls;
430
431 struct tunnel_state *s = GNUNET_malloc (sizeof *s);
432
433 s->head = NULL;
434 s->tail = NULL;
435 s->th = NULL;
436
437 cls_->tunnel =
438 GNUNET_MESH_tunnel_create (mesh_handle, s, mesh_connect, NULL, cls_);
439
440 GNUNET_MESH_peer_request_connect_by_type (cls_->tunnel,
441 GNUNET_APPLICATION_TYPE_INTERNET_RESOLVER);
442
443 remote_pending[cls_->dns.s.id] = cls_;
444}
445
446static int
447receive_mesh_query (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
448 void **ctx GNUNET_UNUSED,
449 const struct GNUNET_PeerIdentity *sender GNUNET_UNUSED,
450 const struct GNUNET_MessageHeader *message,
451 const struct GNUNET_ATS_Information *atsi GNUNET_UNUSED)
452{
453 struct dns_pkt *dns = (struct dns_pkt *) (message + 1);
454
455 struct sockaddr_in dest;
456
457 struct dns_pkt_parsed *pdns = parse_dns_packet (dns);
458
459 memset (&dest, 0, sizeof dest);
460 dest.sin_port = htons (53);
461 char *dns_resolver;
462
463 if (GNUNET_OK !=
464 GNUNET_CONFIGURATION_get_value_string (cfg, "dns", "EXTERNAL_DNS",
465 &dns_resolver) ||
466 1 != inet_pton (AF_INET, dns_resolver, &dest.sin_addr))
467 inet_pton (AF_INET, "8.8.8.8", &dest.sin_addr);
468
469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Querying for remote, id=%d\n",
470 ntohs (dns->s.id));
471 query_states[dns->s.id].tunnel = tunnel;
472 query_states[dns->s.id].valid = GNUNET_YES;
473
474 int i;
475
476 for (i = 0; i < ntohs (pdns->s.qdcount); i++)
477 {
478 if (pdns->queries[i]->qtype == htons (28) ||
479 pdns->queries[i]->qtype == htons (1))
480 {
481 query_states[dns->s.id].qtype = pdns->queries[i]->qtype;
482 break;
483 }
484 }
485 free_parsed_dns_packet (pdns);
486
487 GNUNET_NETWORK_socket_sendto (dnsout, dns,
488 ntohs (message->size) -
489 sizeof (struct GNUNET_MessageHeader),
490 (struct sockaddr *) &dest, sizeof dest);
491
492 return GNUNET_SYSERR;
493}
494
495static int
496receive_mesh_answer (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
497 void **ctx GNUNET_UNUSED,
498 const struct GNUNET_PeerIdentity *sender,
499 const struct GNUNET_MessageHeader *message,
500 const struct GNUNET_ATS_Information *atsi GNUNET_UNUSED)
501{
502 /* TODo: size check */
503 struct dns_pkt *dns = (struct dns_pkt *) (message + 1);
504
505 /* They sent us a packet we were not waiting for */
506 if (remote_pending[dns->s.id] == NULL ||
507 remote_pending[dns->s.id]->tunnel != tunnel)
508 return GNUNET_OK;
509
510 GNUNET_free (remote_pending[dns->s.id]);
511 remote_pending[dns->s.id] = NULL;
512
513 if (query_states[dns->s.id].valid != GNUNET_YES)
514 return GNUNET_SYSERR;
515 query_states[dns->s.id].valid = GNUNET_NO;
516
517 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
518 "Received answer from peer %s, dns-id %d\n", GNUNET_i2s (sender),
519 ntohs (dns->s.id));
520
521 size_t len = sizeof (struct answer_packet) - 1 + sizeof (struct dns_static) + query_states[dns->s.id].namelen + sizeof (struct dns_query_line) + 2 /* To hold the pointer (as defined in RFC1035) to the name */
522 + sizeof (struct dns_record_line) - 1 + 16; /* To hold the IPv6-Address */
523
524 struct answer_packet_list *answer =
525 GNUNET_malloc (len + sizeof (struct answer_packet_list) -
526 sizeof (struct answer_packet));
527
528 answer->pkt.hdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_DNS_LOCAL_RESPONSE_DNS);
529 answer->pkt.hdr.size = htons (len);
530
531 struct dns_pkt_parsed *pdns = parse_dns_packet (dns);
532
533 if (ntohs (pdns->s.ancount) < 1)
534 {
535 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Answer only contains %d answers.\n",
536 ntohs (pdns->s.ancount));
537 free_parsed_dns_packet (pdns);
538 GNUNET_free (answer);
539 return GNUNET_OK;
540 }
541
542 int i = 0;
543
544 while (i < ntohs (pdns->s.ancount) && ntohs (pdns->answers[i]->type) != 28 &&
545 ntohs (pdns->answers[i]->type) != 1)
546 {
547 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Answer contains %d.\n",
548 ntohs (pdns->answers[i]->type));
549 i++;
550 }
551
552 if (i >= ntohs (pdns->s.ancount))
553 {
554 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
555 "Answer does not contain any usable answers.\n");
556 free_parsed_dns_packet (pdns);
557 GNUNET_free (answer);
558 return GNUNET_OK;
559 }
560
561 answer->pkt.addrsize = ntohs (pdns->answers[i]->data_len);
562 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "The first answer has the addrlen %d\n",
563 answer->pkt.addrsize);
564 memcpy (answer->pkt.addr, pdns->answers[i]->data,
565 ntohs (pdns->answers[i]->data_len));
566
567 memcpy (answer->pkt.from, query_states[dns->s.id].remote_ip,
568 query_states[dns->s.id].addrlen);
569 memcpy (answer->pkt.to, query_states[dns->s.id].local_ip,
570 query_states[dns->s.id].addrlen);
571 answer->pkt.addrlen = query_states[dns->s.id].addrlen;
572 answer->pkt.dst_port = query_states[dns->s.id].local_port;
573
574 struct dns_pkt *dpkt = (struct dns_pkt *) answer->pkt.data;
575
576 dpkt->s.id = dns->s.id;
577 dpkt->s.aa = 1;
578 dpkt->s.qr = 1;
579 dpkt->s.ra = 1;
580 dpkt->s.qdcount = htons (1);
581 dpkt->s.ancount = htons (1);
582
583 memcpy (dpkt->data, query_states[dns->s.id].name,
584 query_states[dns->s.id].namelen);
585 GNUNET_free (query_states[dns->s.id].name);
586 query_states[dns->s.id].name = NULL;
587
588 struct dns_query_line *dque =
589 (struct dns_query_line *) (dpkt->data +
590 (query_states[dns->s.id].namelen));
591
592 struct dns_record_line *drec_data =
593 (struct dns_record_line *) (dpkt->data +
594 (query_states[dns->s.id].namelen) +
595 sizeof (struct dns_query_line) + 2);
596 if (htons (28) == query_states[dns->s.id].qtype)
597 {
598 answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_REMOTE_AAAA;
599 dque->type = htons (28); /* AAAA */
600 drec_data->type = htons (28); /* AAAA */
601 drec_data->data_len = htons (16);
602 }
603 else if (htons (1) == query_states[dns->s.id].qtype)
604 {
605 answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_REMOTE_A;
606 dque->type = htons (1); /* A */
607 drec_data->type = htons (1); /* A */
608 drec_data->data_len = htons (4);
609 }
610 else
611 {
612 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "dns-answer with pending qtype = %d\n",
613 query_states[dns->s.id].qtype);
614 GNUNET_assert (0);
615 }
616 dque->class = htons (1); /* IN */
617
618 char *anname =
619 (char *) (dpkt->data + (query_states[dns->s.id].namelen) +
620 sizeof (struct dns_query_line));
621 memcpy (anname, "\xc0\x0c", 2);
622 drec_data->class = htons (1); /* IN */
623
624 drec_data->ttl = pdns->answers[i]->ttl;
625
626 /* Calculate at which offset in the packet the IPv6-Address belongs, it is
627 * filled in by the daemon-vpn */
628 answer->pkt.addroffset =
629 htons ((unsigned short) ((unsigned long) (&drec_data->data) -
630 (unsigned long) (&answer->pkt)));
631
632 GNUNET_CONTAINER_DLL_insert_after (head, tail, tail, answer);
633 answer->client = query_states[dns->s.id].client;
634
635 if (server_notify == NULL)
636 server_notify =
637 GNUNET_SERVER_notify_transmit_ready (query_states[dns->s.id].client,
638 len, GNUNET_TIME_UNIT_FOREVER_REL,
639 &send_answer, NULL);
640
641 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
642 "Sent answer of length %d on to client, addroffset = %d\n", len,
643 answer->pkt.addroffset);
644
645 free_parsed_dns_packet (pdns);
646 return GNUNET_OK;
647}
648
649
650static void
651send_rev_query (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
652{
653 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
654 return;
655
656 struct dns_pkt_parsed *pdns = (struct dns_pkt_parsed *) cls;
657
658 unsigned short id = pdns->s.id;
659
660 free_parsed_dns_packet (pdns);
661
662 if (query_states[id].valid != GNUNET_YES)
663 return;
664 query_states[id].valid = GNUNET_NO;
665
666 GNUNET_assert (query_states[id].namelen == 74);
667
668 size_t len = sizeof (struct answer_packet) - 1 + sizeof (struct dns_static) + 74 /* this is the length of a reverse ipv6-lookup */
669 + sizeof (struct dns_query_line) + 2 /* To hold the pointer (as defined in RFC1035) to the name */
670 + sizeof (struct dns_record_line) - 1 -
671 2 /* We do not know the lenght of the answer yet */ ;
672
673 struct answer_packet_list *answer =
674 GNUNET_malloc (len + sizeof (struct answer_packet_list) -
675 sizeof (struct answer_packet));
676
677 answer->pkt.hdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_DNS_LOCAL_RESPONSE_DNS);
678 answer->pkt.hdr.size = htons (len);
679 answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_REV;
680
681 memcpy (answer->pkt.from, query_states[id].remote_ip,
682 query_states[id].addrlen);
683 memcpy (answer->pkt.to, query_states[id].local_ip, query_states[id].addrlen);
684
685 answer->pkt.dst_port = query_states[id].local_port;
686
687 struct dns_pkt *dpkt = (struct dns_pkt *) answer->pkt.data;
688
689 dpkt->s.id = id;
690 dpkt->s.aa = 1;
691 dpkt->s.qr = 1;
692 dpkt->s.ra = 1;
693 dpkt->s.qdcount = htons (1);
694 dpkt->s.ancount = htons (1);
695
696 memcpy (dpkt->data, query_states[id].name, query_states[id].namelen);
697 GNUNET_free (query_states[id].name);
698 query_states[id].name = NULL;
699
700 struct dns_query_line *dque =
701 (struct dns_query_line *) (dpkt->data + (query_states[id].namelen));
702 dque->type = htons (12); /* PTR */
703 dque->class = htons (1); /* IN */
704
705 char *anname =
706 (char *) (dpkt->data + (query_states[id].namelen) +
707 sizeof (struct dns_query_line));
708 memcpy (anname, "\xc0\x0c", 2);
709
710 struct dns_record_line *drec_data =
711 (struct dns_record_line *) (dpkt->data + (query_states[id].namelen) +
712 sizeof (struct dns_query_line) + 2);
713 drec_data->type = htons (12); /* AAAA */
714 drec_data->class = htons (1); /* IN */
715 /* FIXME: read the TTL from block:
716 * GNUNET_TIME_absolute_get_remaining(rec->expiration_time)
717 *
718 * But how to get the seconds out of this?
719 */
720 drec_data->ttl = htonl (3600);
721
722 /* Calculate at which offset in the packet the length of the name and the
723 * name, it is filled in by the daemon-vpn */
724 answer->pkt.addroffset =
725 htons ((unsigned short) ((unsigned long) (&drec_data->data_len) -
726 (unsigned long) (&answer->pkt)));
727
728 GNUNET_CONTAINER_DLL_insert_after (head, tail, tail, answer);
729 answer->client = query_states[id].client;
730
731 if (server_notify == NULL)
732 server_notify =
733 GNUNET_SERVER_notify_transmit_ready (query_states[id].client, len,
734 GNUNET_TIME_UNIT_FOREVER_REL,
735 &send_answer, NULL);
736}
737
738/**
739 * Receive a block from the dht.
740 */
741static void
742receive_dht (void *cls, struct GNUNET_TIME_Absolute exp GNUNET_UNUSED,
743 const GNUNET_HashCode * key GNUNET_UNUSED,
744 const struct GNUNET_PeerIdentity *get_path GNUNET_UNUSED,
745 unsigned int get_path_length GNUNET_UNUSED,
746 const struct GNUNET_PeerIdentity *put_path GNUNET_UNUSED,
747 unsigned int put_path_length GNUNET_UNUSED,
748 enum GNUNET_BLOCK_Type type, size_t size, const void *data)
749{
750
751 unsigned short id = ((struct receive_dht_cls *) cls)->id;
752 struct GNUNET_DHT_GetHandle *handle =
753 ((struct receive_dht_cls *) cls)->handle;
754 GNUNET_free (cls);
755
756 GNUNET_DHT_get_stop (handle);
757
758 GNUNET_assert (type == GNUNET_BLOCK_TYPE_DNS);
759
760 /* If no query with this id is pending, ignore the block */
761 if (query_states[id].valid != GNUNET_YES)
762 return;
763 query_states[id].valid = GNUNET_NO;
764
765 const struct GNUNET_DNS_Record *rec = data;
766
767 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
768 "Got block of size %d, peer: %08x, desc: %08x\n", size,
769 *((unsigned int *) &rec->peer),
770 *((unsigned int *) &rec->service_descriptor));
771
772 size_t len = sizeof (struct answer_packet) - 1 + sizeof (struct dns_static) + query_states[id].namelen + sizeof (struct dns_query_line) + 2 /* To hold the pointer (as defined in RFC1035) to the name */
773 + sizeof (struct dns_record_line) - 1 + 16; /* To hold the IPv6-Address */
774
775 struct answer_packet_list *answer =
776 GNUNET_malloc (len + sizeof (struct answer_packet_list) -
777 sizeof (struct answer_packet));
778
779 answer->pkt.hdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_DNS_LOCAL_RESPONSE_DNS);
780 answer->pkt.hdr.size = htons (len);
781 answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_SERVICE;
782 answer->client = query_states[id].client;
783
784 GNUNET_CRYPTO_hash (&rec->peer,
785 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
786 &answer->pkt.service_descr.peer);
787
788 memcpy (&answer->pkt.service_descr.service_descriptor,
789 &rec->service_descriptor, sizeof (GNUNET_HashCode));
790 memcpy (&answer->pkt.service_descr.service_type, &rec->service_type,
791 sizeof (answer->pkt.service_descr.service_type));
792 memcpy (&answer->pkt.service_descr.ports, &rec->ports,
793 sizeof (answer->pkt.service_descr.ports));
794
795 memcpy (answer->pkt.from, query_states[id].remote_ip,
796 query_states[id].addrlen);
797 memcpy (answer->pkt.to, query_states[id].local_ip, query_states[id].addrlen);
798 answer->pkt.addrlen = query_states[id].addrlen;
799
800 answer->pkt.dst_port = query_states[id].local_port;
801
802 struct dns_pkt *dpkt = (struct dns_pkt *) answer->pkt.data;
803
804 dpkt->s.id = id;
805 dpkt->s.aa = 1;
806 dpkt->s.qr = 1;
807 dpkt->s.ra = 1;
808 dpkt->s.qdcount = htons (1);
809 dpkt->s.ancount = htons (1);
810
811 memcpy (dpkt->data, query_states[id].name, query_states[id].namelen);
812 GNUNET_free (query_states[id].name);
813 query_states[id].name = NULL;
814
815 struct dns_query_line *dque =
816 (struct dns_query_line *) (dpkt->data + (query_states[id].namelen));
817 dque->type = htons (28); /* AAAA */
818 dque->class = htons (1); /* IN */
819
820 char *anname =
821 (char *) (dpkt->data + (query_states[id].namelen) +
822 sizeof (struct dns_query_line));
823 memcpy (anname, "\xc0\x0c", 2);
824
825 struct dns_record_line *drec_data =
826 (struct dns_record_line *) (dpkt->data + (query_states[id].namelen) +
827 sizeof (struct dns_query_line) + 2);
828 drec_data->type = htons (28); /* AAAA */
829 drec_data->class = htons (1); /* IN */
830
831 /* FIXME: read the TTL from block:
832 * GNUNET_TIME_absolute_get_remaining(rec->expiration_time)
833 *
834 * But how to get the seconds out of this?
835 */
836 drec_data->ttl = htonl (3600);
837 drec_data->data_len = htons (16);
838
839 /* Calculate at which offset in the packet the IPv6-Address belongs, it is
840 * filled in by the daemon-vpn */
841 answer->pkt.addroffset =
842 htons ((unsigned short) ((unsigned long) (&drec_data->data) -
843 (unsigned long) (&answer->pkt)));
844
845 GNUNET_CONTAINER_DLL_insert_after (head, tail, tail, answer);
846
847 if (server_notify == NULL)
848 server_notify =
849 GNUNET_SERVER_notify_transmit_ready (answer->client, len,
850 GNUNET_TIME_UNIT_FOREVER_REL,
851 &send_answer, NULL);
852}
853
854/**
855 * This receives a GNUNET_MESSAGE_TYPE_REHIJACK and rehijacks the DNS
856 */
857static void
858rehijack (void *cls GNUNET_UNUSED, struct GNUNET_SERVER_Client *client,
859 const struct GNUNET_MessageHeader *message GNUNET_UNUSED)
860{
861 unhijack (dnsoutport);
862 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &hijack, NULL);
863
864 GNUNET_SERVER_receive_done (client, GNUNET_OK);
865}
866
867/**
868 * This receives the dns-payload from the daemon-vpn and sends it on over the udp-socket
869 */
870static void
871receive_query (void *cls GNUNET_UNUSED, struct GNUNET_SERVER_Client *client,
872 const struct GNUNET_MessageHeader *message)
873{
874 struct query_packet *pkt = (struct query_packet *) message;
875 struct dns_pkt *dns = (struct dns_pkt *) pkt->data;
876 struct dns_pkt_parsed *pdns = parse_dns_packet (dns);
877
878 query_states[dns->s.id].valid = GNUNET_YES;
879 query_states[dns->s.id].client = client;
880 GNUNET_SERVER_client_keep (client);
881 memcpy (query_states[dns->s.id].local_ip, pkt->orig_from, pkt->addrlen);
882 query_states[dns->s.id].addrlen = pkt->addrlen;
883 query_states[dns->s.id].local_port = pkt->src_port;
884 memcpy (query_states[dns->s.id].remote_ip, pkt->orig_to, pkt->addrlen);
885 query_states[dns->s.id].namelen = strlen ((char *) dns->data) + 1;
886 if (query_states[dns->s.id].name != NULL)
887 GNUNET_free (query_states[dns->s.id].name);
888 query_states[dns->s.id].name =
889 GNUNET_malloc (query_states[dns->s.id].namelen);
890 memcpy (query_states[dns->s.id].name, dns->data,
891 query_states[dns->s.id].namelen);
892
893 int i;
894
895 for (i = 0; i < ntohs (pdns->s.qdcount); i++)
896 {
897 if (pdns->queries[i]->qtype == htons (28) ||
898 pdns->queries[i]->qtype == htons (1))
899 {
900 query_states[dns->s.id].qtype = pdns->queries[i]->qtype;
901 break;
902 }
903 }
904
905 /* The query is for a .gnunet-address */
906 if (pdns->queries[0]->namelen > 9 &&
907 0 == strncmp (pdns->queries[0]->name + (pdns->queries[0]->namelen - 9),
908 ".gnunet.", 9))
909 {
910 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Query for .gnunet!\n");
911 GNUNET_HashCode key;
912
913 GNUNET_CRYPTO_hash (pdns->queries[0]->name, pdns->queries[0]->namelen,
914 &key);
915
916 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Getting with key %08x, len is %d\n",
917 *((unsigned int *) &key), pdns->queries[0]->namelen);
918
919 struct receive_dht_cls *cls =
920 GNUNET_malloc (sizeof (struct receive_dht_cls));
921 cls->id = dns->s.id;
922
923 cls->handle =
924 GNUNET_DHT_get_start (dht, GNUNET_TIME_UNIT_MINUTES,
925 GNUNET_BLOCK_TYPE_DNS, &key,
926 5 /* DEFAULT_GET_REPLICATION */ ,
927 GNUNET_DHT_RO_NONE, NULL, 0, &receive_dht, cls);
928
929 goto outfree;
930 }
931
932 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Query for '%s'; namelen=%d\n",
933 pdns->queries[0]->name, pdns->queries[0]->namelen);
934
935 /* This is a PTR-Query. Check if it is for "our" network */
936 if (htons (pdns->queries[0]->qtype) == 12 && 74 == pdns->queries[0]->namelen)
937 {
938 char *ipv6addr;
939 char ipv6[16];
940 char ipv6rev[74] =
941 "X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.ip6.arpa.";
942 unsigned int i;
943 unsigned long long ipv6prefix;
944 unsigned int comparelen;
945
946 GNUNET_assert (GNUNET_OK ==
947 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn",
948 "IPV6ADDR",
949 &ipv6addr));
950 inet_pton (AF_INET6, ipv6addr, ipv6);
951 GNUNET_free (ipv6addr);
952
953 GNUNET_assert (GNUNET_OK ==
954 GNUNET_CONFIGURATION_get_value_number (cfg, "vpn",
955 "IPV6PREFIX",
956 &ipv6prefix));
957 GNUNET_assert (ipv6prefix < 127);
958 ipv6prefix = (ipv6prefix + 7) / 8;
959
960 for (i = ipv6prefix; i < 16; i++)
961 ipv6[i] = 0;
962
963 for (i = 0; i < 16; i++)
964 {
965 unsigned char c1 = ipv6[i] >> 4;
966 unsigned char c2 = ipv6[i] & 0xf;
967
968 if (c1 <= 9)
969 ipv6rev[62 - (4 * i)] = c1 + '0';
970 else
971 ipv6rev[62 - (4 * i)] = c1 + 87; /* 87 is the difference between 'a' and 10 */
972
973 if (c2 <= 9)
974 ipv6rev[62 - ((4 * i) + 2)] = c2 + '0';
975 else
976 ipv6rev[62 - ((4 * i) + 2)] = c2 + 87;
977 }
978 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "My network is %s'.\n", ipv6rev);
979 comparelen = 10 + 4 * ipv6prefix;
980 if (0 ==
981 strncmp (pdns->queries[0]->name +
982 (pdns->queries[0]->namelen - comparelen),
983 ipv6rev + (74 - comparelen), comparelen))
984 {
985 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Reverse-Query for .gnunet!\n");
986
987 GNUNET_SCHEDULER_add_now (send_rev_query, pdns);
988
989 goto out;
990 }
991 }
992
993 unsigned char virt_dns_bytes[16];
994
995 if (pkt->addrlen == 4)
996 {
997 char *virt_dns;
998
999 if (GNUNET_SYSERR ==
1000 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "VIRTDNS",
1001 &virt_dns))
1002 {
1003 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1004 "No entry 'VIRTDNS' in configuration!\n");
1005 exit (1);
1006 }
1007
1008 if (1 != inet_pton (AF_INET, virt_dns, &virt_dns_bytes))
1009 {
1010 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error parsing 'VIRTDNS': %s; %m!\n",
1011 virt_dns);
1012 exit (1);
1013 }
1014
1015 GNUNET_free (virt_dns);
1016 }
1017 else if (pkt->addrlen == 16)
1018 {
1019 char *virt_dns;
1020
1021 if (GNUNET_SYSERR ==
1022 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "VIRTDNS6",
1023 &virt_dns))
1024 {
1025 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1026 "No entry 'VIRTDNS6' in configuration!\n");
1027 exit (1);
1028 }
1029
1030 if (1 != inet_pton (AF_INET6, virt_dns, &virt_dns_bytes))
1031 {
1032 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1033 "Error parsing 'VIRTDNS6': %s; %m!\n", virt_dns);
1034 exit (1);
1035 }
1036
1037 GNUNET_free (virt_dns);
1038 }
1039 else
1040 {
1041 GNUNET_assert (0);
1042 }
1043
1044 if (memcmp (virt_dns_bytes, pkt->orig_to, pkt->addrlen) == 0)
1045 {
1046 /* This is a packet that was sent directly to the virtual dns-server
1047 *
1048 * This means we have to send this query over gnunet
1049 */
1050
1051 size_t size =
1052 sizeof (struct GNUNET_MESH_Tunnel *) +
1053 sizeof (struct GNUNET_MessageHeader) + (ntohs (message->size) -
1054 sizeof (struct query_packet) +
1055 1);
1056 struct tunnel_cls *cls_ = GNUNET_malloc (size);
1057
1058 cls_->hdr.size = size - sizeof (struct GNUNET_MESH_Tunnel *);
1059
1060 cls_->hdr.type = ntohs (GNUNET_MESSAGE_TYPE_VPN_REMOTE_QUERY_DNS);
1061 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "size: %d\n", size);
1062
1063 memcpy (&cls_->dns, dns,
1064 cls_->hdr.size - sizeof (struct GNUNET_MessageHeader));
1065 GNUNET_SCHEDULER_add_now (send_mesh_query, cls_);
1066
1067 if (ntohs (pdns->s.qdcount) == 1)
1068 {
1069 if (ntohs (pdns->queries[0]->qtype) == 1)
1070 pdns->queries[0]->qtype = htons (28);
1071 else if (ntohs (pdns->queries[0]->qtype) == 28)
1072 pdns->queries[0]->qtype = htons (1);
1073 else
1074 {
1075 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "not sending second packet\n");
1076 goto outfree;
1077 }
1078 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending second packet\n");
1079 struct dns_pkt *rdns = unparse_dns_packet (pdns);
1080 size_t size =
1081 sizeof (struct GNUNET_MESH_Tunnel *) +
1082 sizeof (struct GNUNET_MessageHeader) + (ntohs (message->size) -
1083 sizeof (struct query_packet) +
1084 1);
1085 struct tunnel_cls *cls_ = GNUNET_malloc (size);
1086
1087 cls_->hdr.size = size - sizeof (struct GNUNET_MESH_Tunnel *);
1088
1089 cls_->hdr.type = ntohs (GNUNET_MESSAGE_TYPE_VPN_REMOTE_QUERY_DNS);
1090 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "size: %d\n", size);
1091
1092 memcpy (&cls_->dns, rdns,
1093 cls_->hdr.size - sizeof (struct GNUNET_MessageHeader));
1094 GNUNET_SCHEDULER_add_now (send_mesh_query, cls_);
1095 GNUNET_free (rdns);
1096 }
1097
1098 goto outfree;
1099 }
1100
1101
1102 /* The query should be sent to the network */
1103 if (pkt->addrlen == 4)
1104 {
1105 struct sockaddr_in dest;
1106
1107 memset (&dest, 0, sizeof dest);
1108 dest.sin_port = htons (53);
1109 memcpy (&dest.sin_addr.s_addr, pkt->orig_to, pkt->addrlen);
1110
1111 GNUNET_NETWORK_socket_sendto (dnsout, dns,
1112 ntohs (pkt->hdr.size) -
1113 sizeof (struct query_packet) + 1,
1114 (struct sockaddr *) &dest, sizeof dest);
1115 }
1116 else if (pkt->addrlen == 16)
1117 {
1118 struct sockaddr_in6 dest;
1119
1120 memset (&dest, 0, sizeof dest);
1121 dest.sin6_port = htons (53);
1122 memcpy (&dest.sin6_addr, pkt->orig_to, pkt->addrlen);
1123
1124 GNUNET_NETWORK_socket_sendto (dnsout6, dns,
1125 ntohs (pkt->hdr.size) -
1126 sizeof (struct query_packet) + 1,
1127 (struct sockaddr *) &dest, sizeof dest);
1128 }
1129
1130outfree:
1131 free_parsed_dns_packet (pdns);
1132 pdns = NULL;
1133out:
1134 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1135}
1136
1137static void
1138read_response (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1139
1140static void
1141read_response6 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1142
1143static int
1144open_port6 ()
1145{
1146 struct sockaddr_in6 addr;
1147
1148 dnsout6 = GNUNET_NETWORK_socket_create (AF_INET6, SOCK_DGRAM, 0);
1149 if (dnsout6 == NULL)
1150 {
1151 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not create socket: %m\n");
1152 return GNUNET_SYSERR;
1153 }
1154 memset (&addr, 0, sizeof (struct sockaddr_in6));
1155
1156 addr.sin6_family = AF_INET6;
1157 int err = GNUNET_NETWORK_socket_bind (dnsout6,
1158 (struct sockaddr *) &addr,
1159 sizeof (struct sockaddr_in6));
1160
1161 if (err != GNUNET_OK)
1162 {
1163 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not bind a port: %m\n");
1164 return GNUNET_SYSERR;
1165 }
1166
1167 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, dnsout6,
1168 &read_response6, NULL);
1169
1170 return GNUNET_YES;
1171}
1172
1173static int
1174open_port ()
1175{
1176 struct sockaddr_in addr;
1177
1178 dnsout = GNUNET_NETWORK_socket_create (AF_INET, SOCK_DGRAM, 0);
1179 if (dnsout == NULL)
1180 return GNUNET_SYSERR;
1181 memset (&addr, 0, sizeof (struct sockaddr_in));
1182
1183 addr.sin_family = AF_INET;
1184 int err = GNUNET_NETWORK_socket_bind (dnsout,
1185 (struct sockaddr *) &addr,
1186 sizeof (struct sockaddr_in));
1187
1188 if (err != GNUNET_OK)
1189 {
1190 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not bind a port: %m\n");
1191 return GNUNET_SYSERR;
1192 }
1193
1194 /* Read the port we bound to */
1195 socklen_t addrlen = sizeof (struct sockaddr_in);
1196
1197 err =
1198 getsockname (GNUNET_NETWORK_get_fd (dnsout), (struct sockaddr *) &addr,
1199 &addrlen);
1200
1201 dnsoutport = htons (addr.sin_port);
1202
1203 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Bound to port %d.\n", dnsoutport);
1204
1205 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, dnsout,
1206 &read_response, NULL);
1207
1208 return GNUNET_YES;
1209}
1210
1211void
1212handle_response (struct dns_pkt *dns, struct sockaddr *addr, socklen_t addrlen,
1213 int r);
1214
1215/**
1216 * Read a response-packet of the UDP-Socket
1217 */
1218static void
1219read_response6 (void *cls GNUNET_UNUSED,
1220 const struct GNUNET_SCHEDULER_TaskContext *tc)
1221{
1222 struct sockaddr_in6 addr;
1223 socklen_t addrlen = sizeof (addr);
1224 int r;
1225 int len;
1226
1227 if (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)
1228 return;
1229
1230 memset (&addr, 0, sizeof addr);
1231
1232#ifndef MINGW
1233 if (0 != ioctl (GNUNET_NETWORK_get_fd (dnsout6), FIONREAD, &len))
1234 {
1235 (void) open_port6 ();
1236 return;
1237 }
1238#else
1239 /* port the code above? */
1240 len = 65536;
1241#endif
1242
1243 unsigned char buf[len];
1244 struct dns_pkt *dns = (struct dns_pkt *) buf;
1245
1246 r = GNUNET_NETWORK_socket_recvfrom (dnsout, buf, sizeof (buf),
1247 (struct sockaddr *) &addr, &addrlen);
1248
1249 if (r < 0)
1250 {
1251 (void) open_port6 ();
1252 return;
1253 }
1254
1255 struct sockaddr *addr_ = GNUNET_malloc (sizeof addr);
1256
1257 memcpy (addr_, &addr, sizeof addr);
1258 handle_response (dns, addr_, 4, r);
1259
1260 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, dnsout6,
1261 &read_response6, NULL);
1262}
1263
1264/**
1265 * Read a response-packet of the UDP-Socket
1266 */
1267static void
1268read_response (void *cls GNUNET_UNUSED,
1269 const struct GNUNET_SCHEDULER_TaskContext *tc)
1270{
1271 struct sockaddr_in addr;
1272 socklen_t addrlen = sizeof (addr);
1273 int r;
1274 int len;
1275
1276 if (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)
1277 return;
1278
1279 memset (&addr, 0, sizeof addr);
1280
1281#ifndef MINGW
1282 if (0 != ioctl (GNUNET_NETWORK_get_fd (dnsout), FIONREAD, &len))
1283 {
1284 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "ioctl");
1285 unhijack (dnsoutport);
1286 if (GNUNET_YES == open_port ())
1287 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &hijack, NULL);
1288 return;
1289 }
1290#else
1291 /* port the code above? */
1292 len = 65536;
1293#endif
1294
1295 unsigned char buf[len];
1296 struct dns_pkt *dns = (struct dns_pkt *) buf;
1297
1298 r = GNUNET_NETWORK_socket_recvfrom (dnsout, buf, sizeof (buf),
1299 (struct sockaddr *) &addr, &addrlen);
1300
1301 if (r < 0)
1302 {
1303 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "recvfrom");
1304 unhijack (dnsoutport);
1305 if (GNUNET_YES == open_port ())
1306 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &hijack, NULL);
1307 return;
1308 }
1309
1310 struct sockaddr *addr_ = GNUNET_malloc (sizeof addr);
1311
1312 memcpy (addr_, &addr, sizeof addr);
1313 handle_response (dns, addr_, 4, r);
1314
1315 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, dnsout,
1316 &read_response, NULL);
1317}
1318
1319void
1320handle_response (struct dns_pkt *dns, struct sockaddr *addr, socklen_t addrlen,
1321 int r)
1322{
1323 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Answer to query %d\n",
1324 ntohs (dns->s.id));
1325
1326
1327 if (query_states[dns->s.id].valid == GNUNET_YES)
1328 {
1329 if (query_states[dns->s.id].tunnel != NULL)
1330 {
1331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1332 "Answer to query %d for a remote peer!\n", ntohs (dns->s.id));
1333 /* This response should go through a tunnel */
1334 uint32_t *c =
1335 GNUNET_malloc (4 + sizeof (struct GNUNET_MESH_Tunnel *) + r);
1336 *c = r;
1337 struct GNUNET_MESH_Tunnel **t = (struct GNUNET_MESH_Tunnel **) (c + 1);
1338
1339 *t = query_states[dns->s.id].tunnel;
1340 memcpy (t + 1, dns, r);
1341 struct tunnel_state *s =
1342 GNUNET_MESH_tunnel_get_data (query_states[dns->s.id].tunnel);
1343 if (NULL == s->th)
1344 {
1345 s->th =
1346 GNUNET_MESH_notify_transmit_ready (query_states[dns->s.id].tunnel,
1347 GNUNET_YES, 32,
1348 GNUNET_TIME_UNIT_MINUTES, NULL,
1349 r +
1350 sizeof (struct
1351 GNUNET_MessageHeader),
1352 mesh_send_response, c);
1353 }
1354 else
1355 {
1356 struct tunnel_notify_queue *element =
1357 GNUNET_malloc (sizeof (struct tunnel_notify_queue));
1358 element->cls = c;
1359 element->len = r + sizeof (struct GNUNET_MessageHeader);
1360 element->cb = mesh_send_response;
1361
1362 GNUNET_CONTAINER_DLL_insert_tail (s->head, s->tail, element);
1363 }
1364 }
1365 else
1366 {
1367 query_states[dns->s.id].valid = GNUNET_NO;
1368
1369 size_t len = sizeof (struct answer_packet) + r - 1; /* 1 for the unsigned char data[1]; */
1370 struct answer_packet_list *answer =
1371 GNUNET_malloc (len + sizeof (struct answer_packet_list) -
1372 (sizeof (struct answer_packet)));
1373 answer->pkt.hdr.type =
1374 htons (GNUNET_MESSAGE_TYPE_VPN_DNS_LOCAL_RESPONSE_DNS);
1375 answer->pkt.hdr.size = htons (len);
1376 answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_IP;
1377 answer->pkt.addrlen = addrlen;
1378 if (addrlen == 16)
1379 {
1380 struct sockaddr_in6 *addr_ = (struct sockaddr_in6 *) addr;
1381
1382 memcpy (answer->pkt.from, &addr_->sin6_addr, addrlen);
1383 memcpy (answer->pkt.to, query_states[dns->s.id].local_ip, addrlen);
1384 }
1385 else if (addrlen == 4)
1386 {
1387 struct sockaddr_in *addr_ = (struct sockaddr_in *) addr;
1388
1389 memcpy (answer->pkt.from, &addr_->sin_addr.s_addr, addrlen);
1390 memcpy (answer->pkt.to, query_states[dns->s.id].local_ip, addrlen);
1391 }
1392 else
1393 {
1394 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "addrlen = %d\n", addrlen);
1395 GNUNET_assert (0);
1396 }
1397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending answer with addrlen = %d\n",
1398 addrlen);
1399 answer->pkt.dst_port = query_states[dns->s.id].local_port;
1400 memcpy (answer->pkt.data, dns, r);
1401 answer->client = query_states[dns->s.id].client;
1402
1403 GNUNET_CONTAINER_DLL_insert_after (head, tail, tail, answer);
1404
1405 if (server_notify == NULL)
1406 server_notify =
1407 GNUNET_SERVER_notify_transmit_ready (query_states[dns->s.id].client,
1408 len,
1409 GNUNET_TIME_UNIT_FOREVER_REL,
1410 &send_answer, NULL);
1411 }
1412 }
1413 GNUNET_free (addr);
1414}
1415
1416
1417/**
1418 * Task run during shutdown.
1419 *
1420 * @param cls unused
1421 * @param tc unused
1422 */
1423static void
1424cleanup_task (void *cls GNUNET_UNUSED,
1425 const struct GNUNET_SCHEDULER_TaskContext *tc)
1426{
1427 GNUNET_assert (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN));
1428
1429 unhijack (dnsoutport);
1430 GNUNET_DHT_disconnect (dht);
1431 GNUNET_MESH_disconnect (mesh_handle);
1432}
1433
1434/**
1435 * @brief Create a port-map from udp and tcp redirects
1436 *
1437 * @param udp_redirects
1438 * @param tcp_redirects
1439 *
1440 * @return
1441 */
1442static uint64_t
1443get_port_from_redirects (const char *udp_redirects, const char *tcp_redirects)
1444{
1445 uint64_t ret = 0;
1446 char *cpy, *hostname, *redirect;
1447 int local_port;
1448 unsigned int count = 0;
1449
1450 cpy = NULL;
1451 if (NULL != udp_redirects)
1452 {
1453 cpy = GNUNET_strdup (udp_redirects);
1454 for (redirect = strtok (cpy, " "); redirect != NULL;
1455 redirect = strtok (NULL, " "))
1456 {
1457 if (NULL == (hostname = strstr (redirect, ":")))
1458 {
1459 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1460 "Warning: option %s is not formatted correctly!\n",
1461 redirect);
1462 continue;
1463 }
1464 hostname[0] = '\0';
1465 local_port = atoi (redirect);
1466 if (!((local_port > 0) && (local_port < 65536)))
1467 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1468 "Warning: %s is not a correct port.", redirect);
1469
1470 ret |= (0xFFFF & htons (local_port));
1471 ret <<= 16;
1472 count++;
1473
1474 if (count > 4)
1475 {
1476 ret = 0;
1477 goto out;
1478 }
1479 }
1480 GNUNET_free (cpy);
1481 cpy = NULL;
1482 }
1483
1484 if (NULL != tcp_redirects)
1485 {
1486 cpy = GNUNET_strdup (tcp_redirects);
1487 for (redirect = strtok (cpy, " "); redirect != NULL;
1488 redirect = strtok (NULL, " "))
1489 {
1490 if (NULL == (hostname = strstr (redirect, ":")))
1491 {
1492 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1493 "Warning: option %s is not formatted correctly!\n",
1494 redirect);
1495 continue;
1496 }
1497 hostname[0] = '\0';
1498 local_port = atoi (redirect);
1499 if (!((local_port > 0) && (local_port < 65536)))
1500 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1501 "Warning: %s is not a correct port.", redirect);
1502
1503 ret |= (0xFFFF & htons (local_port));
1504 ret <<= 16;
1505 count++;
1506
1507 if (count > 4)
1508 {
1509 ret = 0;
1510 goto out;
1511 }
1512 }
1513 GNUNET_free (cpy);
1514 cpy = NULL;
1515 }
1516
1517out:
1518 GNUNET_free_non_null (cpy);
1519 return ret;
1520}
1521
1522static void
1523publish_name (const char *name, uint64_t ports, uint32_t service_type,
1524 struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key)
1525{
1526 size_t size = sizeof (struct GNUNET_DNS_Record);
1527 struct GNUNET_DNS_Record data;
1528
1529 memset (&data, 0, size);
1530
1531 data.purpose.size = htonl (size - sizeof (struct GNUNET_CRYPTO_RsaSignature));
1532 data.purpose.purpose = GNUNET_SIGNATURE_PURPOSE_DNS_RECORD;
1533
1534 GNUNET_CRYPTO_hash (name, strlen (name) + 1, &data.service_descriptor);
1535 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Store with key1 %x\n",
1536 *((unsigned long long *) &data.service_descriptor));
1537
1538 data.service_type = service_type;
1539 data.ports = ports;
1540
1541 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &data.peer);
1542
1543 data.expiration_time =
1544 GNUNET_TIME_absolute_hton (GNUNET_TIME_relative_to_absolute
1545 (GNUNET_TIME_relative_multiply
1546 (GNUNET_TIME_UNIT_HOURS, 2)));
1547
1548 /* Sign the block */
1549 if (GNUNET_OK !=
1550 GNUNET_CRYPTO_rsa_sign (my_private_key, &data.purpose, &data.signature))
1551 {
1552 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "could not sign DNS_Record\n");
1553 return;
1554 }
1555
1556 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Putting with key %08x, size = %d\n",
1557 *((unsigned int *) &data.service_descriptor), size);
1558
1559 GNUNET_DHT_put (dht, &data.service_descriptor,
1560 5 /* DEFAULT_PUT_REPLICATION */ ,
1561 GNUNET_DHT_RO_NONE, GNUNET_BLOCK_TYPE_DNS, size,
1562 (char *) &data,
1563 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS),
1564 GNUNET_TIME_UNIT_MINUTES, NULL, NULL);
1565}
1566
1567
1568/**
1569 * @brief Publishes the record defined by the section section
1570 *
1571 * @param cls closure
1572 * @param section the current section
1573 */
1574static void
1575publish_iterate (void *cls GNUNET_UNUSED, const char *section)
1576{
1577 char *udp_redirects;
1578 char *tcp_redirects;
1579 char *alternative_names;
1580 char *alternative_name;
1581 char *keyfile;
1582
1583 if ((strlen (section) < 8) ||
1584 (0 != strcmp (".gnunet.", section + (strlen (section) - 8))))
1585 return;
1586 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Parsing dns-name %s\n", section);
1587 if (GNUNET_OK !=
1588 GNUNET_CONFIGURATION_get_value_string (cfg, section, "UDP_REDIRECTS",
1589 &udp_redirects))
1590 udp_redirects = NULL;
1591 if (GNUNET_OK !=
1592 GNUNET_CONFIGURATION_get_value_string (cfg, section, "TCP_REDIRECTS",
1593 &tcp_redirects))
1594 tcp_redirects = NULL;
1595
1596 if (GNUNET_OK !=
1597 GNUNET_CONFIGURATION_get_value_filename (cfg, "GNUNETD", "HOSTKEY",
1598 &keyfile))
1599 {
1600 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "could not read keyfile-value\n");
1601 if (keyfile != NULL)
1602 GNUNET_free (keyfile);
1603 return;
1604 }
1605
1606 struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key =
1607 GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
1608 GNUNET_free (keyfile);
1609 GNUNET_assert (my_private_key != NULL);
1610
1611 uint64_t ports = get_port_from_redirects (udp_redirects, tcp_redirects);
1612 uint32_t service_type = 0;
1613
1614 if (NULL != udp_redirects)
1615 service_type = GNUNET_DNS_SERVICE_TYPE_UDP;
1616
1617 if (NULL != tcp_redirects)
1618 service_type |= GNUNET_DNS_SERVICE_TYPE_TCP;
1619
1620 service_type = htonl (service_type);
1621
1622
1623 publish_name (section, ports, service_type, my_private_key);
1624 if (GNUNET_OK ==
1625 GNUNET_CONFIGURATION_get_value_string (cfg, section, "ALTERNATIVE_NAMES",
1626 &alternative_names))
1627 {
1628 for (alternative_name = strtok (alternative_names, " ");
1629 alternative_name != NULL; alternative_name = strtok (NULL, " "))
1630 {
1631 char *altname =
1632 alloca (strlen (alternative_name) + strlen (section) + 1 + 1);
1633 strcpy (altname, alternative_name);
1634 strcpy (altname + strlen (alternative_name) + 1, section);
1635 altname[strlen (alternative_name)] = '.';
1636
1637 publish_name (altname, ports, service_type, my_private_key);
1638 }
1639 GNUNET_free (alternative_names);
1640 }
1641 GNUNET_CRYPTO_rsa_key_free (my_private_key);
1642 GNUNET_free_non_null (udp_redirects);
1643 GNUNET_free_non_null (tcp_redirects);
1644}
1645
1646/**
1647 * Publish a DNS-record in the DHT.
1648 */
1649static void
1650publish_names (void *cls GNUNET_UNUSED,
1651 const struct GNUNET_SCHEDULER_TaskContext *tc)
1652{
1653 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1654 return;
1655
1656 GNUNET_CONFIGURATION_iterate_sections (cfg, &publish_iterate, NULL);
1657
1658 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_HOURS, &publish_names, NULL);
1659}
1660
1661/**
1662 * @param cls closure
1663 * @param server the initialized server
1664 * @param cfg_ configuration to use
1665 */
1666static void
1667run (void *cls, struct GNUNET_SERVER_Handle *server,
1668 const struct GNUNET_CONFIGURATION_Handle *cfg_)
1669{
1670 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1671 /* callback, cls, type, size */
1672 {&receive_query, NULL, GNUNET_MESSAGE_TYPE_VPN_DNS_LOCAL_QUERY_DNS, 0},
1673 {&rehijack, NULL, GNUNET_MESSAGE_TYPE_REHIJACK,
1674 sizeof (struct GNUNET_MessageHeader)},
1675 {NULL, NULL, 0, 0}
1676 };
1677
1678 static const struct GNUNET_MESH_MessageHandler mesh_handlers[] = {
1679 {receive_mesh_query, GNUNET_MESSAGE_TYPE_VPN_REMOTE_QUERY_DNS, 0},
1680 {receive_mesh_answer, GNUNET_MESSAGE_TYPE_VPN_REMOTE_ANSWER_DNS, 0},
1681 {NULL, 0, 0}
1682 };
1683
1684 static GNUNET_MESH_ApplicationType apptypes[] = {
1685 GNUNET_APPLICATION_TYPE_END,
1686 GNUNET_APPLICATION_TYPE_END
1687 };
1688
1689 if (GNUNET_YES != open_port6 ())
1690 {
1691 GNUNET_SCHEDULER_shutdown ();
1692 return;
1693 }
1694
1695 if (GNUNET_YES != open_port ())
1696 {
1697 GNUNET_SCHEDULER_shutdown ();
1698 return;
1699 }
1700
1701 if (GNUNET_YES ==
1702 GNUNET_CONFIGURATION_get_value_yesno (cfg_, "dns", "PROVIDE_EXIT"))
1703 apptypes[0] = GNUNET_APPLICATION_TYPE_INTERNET_RESOLVER;
1704 mesh_handle =
1705 GNUNET_MESH_connect (cfg_, 42, NULL, new_tunnel, clean_tunnel,
1706 mesh_handlers, apptypes);
1707
1708 cfg = cfg_;
1709 dht = GNUNET_DHT_connect (cfg, 1024);
1710 GNUNET_SCHEDULER_add_now (publish_names, NULL);
1711 GNUNET_SERVER_add_handlers (server, handlers);
1712 GNUNET_SERVER_disconnect_notify (server, &client_disconnect, NULL);
1713 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup_task,
1714 cls);
1715}
1716
1717/**
1718 * The main function for the dns service.
1719 *
1720 * @param argc number of arguments from the command line
1721 * @param argv command line arguments
1722 * @return 0 ok, 1 on error
1723 */
1724int
1725main (int argc, char *const *argv)
1726{
1727 return (GNUNET_OK ==
1728 GNUNET_SERVICE_run (argc, argv, "dns", GNUNET_SERVICE_OPTION_NONE,
1729 &run, NULL)) ? 0 : 1;
1730}