summaryrefslogtreecommitdiff
path: root/src/dnsfunnel/dnsfunneld_process.c
blob: 8a0ffe20eff27df1804d2fe8757524c743eb5429 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/* ISC license. */

#include <stdint.h>

#include <skalibs/uint16.h>
#include <skalibs/strerr2.h>
#include <skalibs/gensetdyn.h>

#include <s6-dns/s6dns-constants.h>
#include <s6-dns/s6dns-domain.h>
#include <s6-dns/s6dns-message.h>
#include <s6-dns/s6dns-engine.h>

#include "dnsfunneld.h"

static gensetdyn rinfo = GENSETDYN_INIT(uint8_t, 16, 3, 8) ;
#define RINFO(i) GENSETDYN_P(uint8_t, &rinfo, i)

int query_process_init ()
{
  return 1 ;
}

void query_process_reload ()
{
}

void query_process_question (uint32_t ops, s6dns_domain_t const *d, uint16_t qtype, uint16_t id, uint32_t ip, uint16_t port)
{
  if (ops & 2 && (qtype == S6DNS_T_A || qtype == S6DNS_T_AAAA))
  {
    uint32_t i ;
    if (!gensetdyn_new(&rinfo, &i)) strerr_diefu1sys(111, "process query") ;
    *RINFO(i) = (qtype == S6DNS_T_AAAA) << 7 ;
    query_new(d, S6DNS_T_A, id, ip, port, i+1) ;
    query_new(d, S6DNS_T_AAAA, id, ip, port, i+1) ; 
  }
  else query_new(d, qtype, id, ip, port, 0) ;
}

static inline unsigned int truncate_packet (char *s, unsigned int olen)
{
  s6dns_message_header_t hdr ;
  s6dns_message_counts_t counts ;
  unsigned int section ;
  unsigned int pos ;
  if (!s6dns_message_parse_init(&hdr, &counts, s, olen, &pos)) return 0 ;
  if (hdr.rcode) return 0 ;
  section = s6dns_message_parse_skipqd(&counts, s, olen, &pos) ;
  while (section)
  {
    s6dns_message_rr_t rr ;
    s6dns_message_counts_t newcounts = counts ;
    unsigned int tmp = pos ;
    if (!s6dns_message_parse_getrr(&rr, s, olen, &tmp)) return 0 ;
    section = s6dns_message_parse_next(&newcounts, &rr, s, olen, &tmp) ;
    if (tmp > 512)
    {
      hdr.counts.qd -= counts.qd ;
      hdr.counts.an -= counts.an ;
      hdr.counts.ns -= counts.ns ;
      hdr.counts.nr -= counts.nr ;
      s6dns_message_header_pack(s, &hdr) ;
      return pos ;
    }
    pos = tmp ;
    counts = newcounts ;
  }
  return olen ;
}

static inline uint16_t extract_qtype (dfquery_t const *q)
{
  s6dns_domain_t name ;
  uint16_t qtype ;
  uint16_t len ;
  s6dns_message_header_t hdr ;
  s6dns_message_counts_t counts ;
  unsigned int pos ;
  uint16_unpack_big(q->dt.sa.s, &len) ;
  if (!s6dns_message_parse_init(&hdr, &counts, q->dt.sa.s + 2, len, &pos)) return 0 ;
  if (!s6dns_message_parse_question(&counts, &name, &qtype, q->dt.sa.s + 2, len, &pos)) return 0 ;
  return qtype ;
}

static int isnxdomain (dfquery_t const *q)
{
  s6dns_message_header_t hdr ;
  s6dns_message_counts_t counts ;
  unsigned int pos ;
  if (!s6dns_message_parse_init(&hdr, &counts, s6dns_engine_packet(&q->dt), s6dns_engine_packetlen(&q->dt), &pos)) return 0 ;
  return hdr.rcode == 3 ;
}

static int input_event (dfquery_t const *q, unsigned int ev)
{
  static uint8_t const table[5][6] =
  {
    { 0x11, 0x03, 0x81, 0x02, 0x02, 0x04 },
    { 0x06, 0x06, 0x06, 0x05, 0x05, 0x05 },
    { 0x15, 0x25, 0x85, 0x06, 0x06, 0x06 },
    { 0x06, 0x06, 0x06, 0x25, 0x25, 0x45 },
    { 0x15, 0x45, 0x85, 0x06, 0x06, 0x06 }
  } ;
  uint8_t b = *RINFO(q->procid - 1) ;
  uint8_t isaux = 3 * (b >> 7 != (extract_qtype(q) == S6DNS_T_AAAA)) ;
  uint8_t state = (b >> isaux) & 7 ;
  uint8_t c = table[state][ev + isaux] ;
  state = c & 7 ;
  *RINFO(q->procid - 1) = (b & ~(7 << isaux)) | (state << isaux) ;
  if (c & 0x10) dfanswer_fail(q) ;
  if (c & 0x20) dfanswer_nxdomain(q) ;
  if (c & 0x40) dfanswer_nodata(q) ;
  if (c & 0x80) dfanswer_pass(q, s6dns_engine_packet(&q->dt), s6dns_engine_packetlen(&q->dt)) ;
  if (state >= 6) strerr_dief1x(101, "problem in main/aux transition table; please submit a bug-report.") ;
  if (state == 5) gensetdyn_delete(&rinfo, q->procid - 1) ;
  return !!(c & 0xf0) ;
}

void query_process_response_failure (uint32_t ops, dfquery_t const *q)
{
  if (ops & 2 && q->procid && input_event(q, 0)) return ;
  else dfanswer_fail(q) ;
}

void query_process_response_success (uint32_t ops, dfquery_t const *q)
{
  if (ops & 2 && q->procid && input_event(q, 1 + !isnxdomain(q))) return ;
  if (ops & 1 && s6dns_engine_packetlen(&q->dt) > 512)
  {
    unsigned int len = truncate_packet(s6dns_engine_packet(&q->dt), s6dns_engine_packetlen(&q->dt)) ;
    if (!len) dfanswer_fail(q) ;
    else dfanswer_pass(q, s6dns_engine_packet(&q->dt), len) ;
  }
  else dfanswer_pass(q, s6dns_engine_packet(&q->dt), s6dns_engine_packetlen(&q->dt)) ;
}