Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 76 lines (64 sloc) 1.806 kB
3e25c70 import dnscache 1.00
D. J. Bernstein authored
1 #include "stralloc.h"
2 #include "uint16.h"
3 #include "byte.h"
4 #include "dns.h"
5
df36440 import djbdns 1.03
D. J. Bernstein authored
6 int dns_ip4_packet(stralloc *out,const char *buf,unsigned int len)
3e25c70 import dnscache 1.00
D. J. Bernstein authored
7 {
8 unsigned int pos;
9 char header[12];
10 uint16 numanswers;
11 uint16 datalen;
12
13 if (!stralloc_copys(out,"")) return -1;
14
15 pos = dns_packet_copy(buf,len,0,header,12); if (!pos) return -1;
16 uint16_unpack_big(header + 6,&numanswers);
17 pos = dns_packet_skipname(buf,len,pos); if (!pos) return -1;
18 pos += 4;
19
20 while (numanswers--) {
21 pos = dns_packet_skipname(buf,len,pos); if (!pos) return -1;
22 pos = dns_packet_copy(buf,len,pos,header,10); if (!pos) return -1;
23 uint16_unpack_big(header + 8,&datalen);
24 if (byte_equal(header,2,DNS_T_A))
25 if (byte_equal(header + 2,2,DNS_C_IN))
26 if (datalen == 4) {
27 if (!dns_packet_copy(buf,len,pos,header,4)) return -1;
28 if (!stralloc_catb(out,header,4)) return -1;
29 }
30 pos += datalen;
31 }
32
33 dns_sortip(out->s,out->len);
34 return 0;
35 }
36
37 static char *q = 0;
38
df36440 import djbdns 1.03
D. J. Bernstein authored
39 int dns_ip4(stralloc *out,const stralloc *fqdn)
3e25c70 import dnscache 1.00
D. J. Bernstein authored
40 {
41 unsigned int i;
42 char code;
43 char ch;
44
45 if (!stralloc_copys(out,"")) return -1;
46 code = 0;
47 for (i = 0;i <= fqdn->len;++i) {
48 if (i < fqdn->len)
49 ch = fqdn->s[i];
50 else
51 ch = '.';
52
53 if ((ch == '[') || (ch == ']')) continue;
54 if (ch == '.') {
55 if (!stralloc_append(out,&code)) return -1;
56 code = 0;
57 continue;
58 }
59 if ((ch >= '0') && (ch <= '9')) {
60 code *= 10;
61 code += ch - '0';
62 continue;
63 }
64
65 if (!dns_domain_fromdot(&q,fqdn->s,fqdn->len)) return -1;
66 if (dns_resolve(q,DNS_T_A) == -1) return -1;
67 if (dns_ip4_packet(out,dns_resolve_tx.packet,dns_resolve_tx.packetlen) == -1) return -1;
68 dns_transmit_free(&dns_resolve_tx);
69 dns_domain_free(&q);
70 return 0;
71 }
72
73 out->len &= ~3;
74 return 0;
75 }
Something went wrong with that request. Please try again.