summaryrefslogtreecommitdiff
path: root/src/dnsstub/dns_ip.c
blob: 74bc24fae973c1b9c9bf84bbae95d13178432f31 (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include "stralloc.h"
#include "uint_t.h"
#include "byte.h"
#include "ip.h"
#include "dnsresolv.h"

/**
	* @file dns_ip.c
	* @authors djb, fefe, feh
	* @ref ucspi-tcp6
	* @brief DNS IP query
  */

static char *q = 0;

int dns_ip4_packet(stralloc *out,const char *buf,unsigned int len)
{
  unsigned int pos;
  char header[12];
  uint16 numanswers;
  uint16 datalen;
  int ranswers = 0;

  if (!stralloc_copys(out,"")) return DNS_MEM;

  pos = dns_packet_copy(buf,len,0,header,12); if (!pos) return DNS_ERR;
  uint16_unpack_big(header + 6,&numanswers);
  pos = dns_packet_skipname(buf,len,pos); if (!pos) return DNS_ERR;
  pos += 4;

  while (numanswers--) {
    pos = dns_packet_skipname(buf,len,pos); if (!pos) return DNS_ERR;
    pos = dns_packet_copy(buf,len,pos,header,10); if (!pos) return DNS_ERR;
    uint16_unpack_big(header + 8,&datalen);
    if (byte_equal(header,2,DNS_T_A))
      if (byte_equal(header + 2,2,DNS_C_IN))
        if (datalen == 4) {
          if (!dns_packet_copy(buf,len,pos,header,4)) return DNS_ERR;
          if (!stralloc_catb(out,header,4)) return DNS_MEM;
      }
    pos += datalen;
    ++ranswers;
  }

  dns_sortip4(out->s,out->len);
  return ranswers;
}

int dns_ip4(stralloc *out,stralloc *fqdn)
{
  unsigned int i;
  char code = 0;
	int dot = 0;
  char ch; 
  char ip[4];
  int r;
  int rc = 0;

  if (!stralloc_copys(out,"")) return DNS_MEM;
  if (!stralloc_readyplus(fqdn,1)) return DNS_MEM;

  fqdn->s[fqdn->len] = 0;		/* test FQDN string */
	for (i = 1; i < fqdn->len; i++) {
	  if (fqdn->s[i] >= '_') { code = 127; break; }
	  if (fqdn->s[i] == '.') dot++;
	}

	if (code != 127 && dot == 3) /* if FQDN is just IPv4 */
    if (ip4_scan(fqdn->s,ip) || ip4_scanbracket(fqdn->s,ip)) {
      if (!stralloc_copyb(out,ip,4)) return DNS_MEM;
      return 1;
    }

  code = 0;
  for (i = 0; i <= fqdn->len; ++i) {
    if (i < fqdn->len)
      ch = fqdn->s[i];
    else
      ch = '.';

    if ((ch == '[') || (ch == ']')) continue;
    if (ch == '.') {
      if (!stralloc_append(out,&code)) return DNS_MEM;
      code = 0;
      continue;
    }
    if ((ch >= '0') && (ch <= '9')) {
      code *= 10;
      code += ch - '0';
      continue;
    }

    if (dns_domain_fromdot(&q,fqdn->s,fqdn->len) <= 0) return DNS_ERR;	// fdqn -> A query -> response
    if (dns_resolve(q,DNS_T_A) >= 0) {
      if ((r = dns_ip4_packet(out,dns_resolve_tx.packet,dns_resolve_tx.packetlen)) < 0) return DNS_ERR;
      dns_transmit_free(&dns_resolve_tx);
      dns_domain_free(&q);
      rc += r;
    }

    return rc; 
  }

  out->len &= ~3;
  return 0;
}

int dns_ip6_packet(stralloc *out,const char *buf,unsigned int len)
{
  unsigned int pos;
  char header[16];
  uint16 numanswers;
  uint16 datalen;
  int ranswers = 0;

  if (!stralloc_copys(out,"")) return DNS_MEM;

  pos = dns_packet_copy(buf,len,0,header,12); if (!pos) return DNS_ERR;
  uint16_unpack_big(header + 6,&numanswers);
  pos = dns_packet_skipname(buf,len,pos); if (!pos) return DNS_ERR;
  pos += 4;

  while (numanswers--) {
    pos = dns_packet_skipname(buf,len,pos); if (!pos) return DNS_ERR;
    pos = dns_packet_copy(buf,len,pos,header,10); if (!pos) return DNS_ERR;
    uint16_unpack_big(header + 8,&datalen);
    if (byte_equal(header,2,DNS_T_AAAA)) {
      if (byte_equal(header + 2,2,DNS_C_IN))
        if (datalen == 16) {
          if (!dns_packet_copy(buf,len,pos,header,16)) return DNS_ERR;
          if (!stralloc_catb(out,header,16)) return DNS_MEM;
        }
    } else if (byte_equal(header,2,DNS_T_A))
      if (byte_equal(header + 2,2,DNS_C_IN))
        if (datalen == 4) {
          byte_copy(header,12,V4mappedprefix);
          if (!dns_packet_copy(buf,len,pos,header + 12,4)) return DNS_ERR;
          if (!stralloc_catb(out,header,16)) return DNS_MEM;
        }
    pos += datalen;
    ++ranswers;
  }

  dns_sortip6(out->s,out->len);
  return ranswers;
}

int dns_ip6(stralloc *out,stralloc *fqdn)
{
  unsigned int i;
  char code;
  char ch;
  char ip[16];
  int r;
  int rc = 0;

  if (!stralloc_copys(out,"")) return DNS_MEM;
  if (!stralloc_readyplus(fqdn,1)) return DNS_MEM;

  fqdn->s[fqdn->len] = 0;		/* if FQDN is just IPv6 */
  if (ip6_scan(fqdn->s,ip) || ip6_scanbracket(fqdn->s,ip)) {
    if (!stralloc_copyb(out,ip,16)) return DNS_MEM;
    return 1;
  }

  code = 0;
  for (i = 0; i <= fqdn->len; ++i) {
    if (i < fqdn->len)
      ch = fqdn->s[i];
    else
      ch = '.';

    if ((ch == '[') || (ch == ']')) continue;
    if (ch == '.') {
      if (!stralloc_append(out,&code)) return DNS_MEM;
      code = 0;
      continue;
    }
    if ((ch >= '0') && (ch <= '9')) {
      code *= 10;
      code += ch - '0';
      continue;
    }

    if (dns_domain_fromdot(&q,fqdn->s,fqdn->len) <= 0) return DNS_ERR; // fqdn -> AAAA query -> response
    if (dns_resolve(q,DNS_T_AAAA) >= 0) {
      if ((r = dns_ip6_packet(out,dns_resolve_tx.packet,dns_resolve_tx.packetlen)) < 0) return DNS_ERR;
      dns_transmit_free(&dns_resolve_tx);
      dns_domain_free(&q);
      rc += r;
    }

    return rc;
  }
  
  out->len &= ~3;
  return 0;
}