djbdnscurve6 45
djbdnscurve6
Loading...
Searching...
No Matches
dns_ip.c
Go to the documentation of this file.
1#include "stralloc.h"
2#include "uint_t.h"
3#include "byte.h"
4#include "ip.h"
5#include "dnsresolv.h"
6
7static char *q = 0;
8
9int dns_ip4_packet(stralloc *out,const char *buf,unsigned int len)
10{
11 unsigned int pos;
12 char header[12];
13 uint16 numanswers;
14 uint16 datalen;
15 int ranswers = 0;
16
17 if (!stralloc_copys(out,"")) return DNS_MEM;
18
19 pos = dns_packet_copy(buf,len,0,header,12); if (!pos) return DNS_ERR;
20 uint16_unpack_big(header + 6,&numanswers);
21 pos = dns_packet_skipname(buf,len,pos); if (!pos) return DNS_ERR;
22 pos += 4;
23
24 while (numanswers--) {
25 pos = dns_packet_skipname(buf,len,pos); if (!pos) return DNS_ERR;
26 pos = dns_packet_copy(buf,len,pos,header,10); if (!pos) return DNS_ERR;
27 uint16_unpack_big(header + 8,&datalen);
28 if (byte_equal(header,2,DNS_T_A))
29 if (byte_equal(header + 2,2,DNS_C_IN))
30 if (datalen == 4) {
31 if (!dns_packet_copy(buf,len,pos,header,4)) return DNS_ERR;
32 if (!stralloc_catb(out,header,4)) return DNS_MEM;
33 }
34 pos += datalen;
35 ++ranswers;
36 }
37
38 dns_sortip4(out->s,out->len);
39 return ranswers;
40}
41
42int dns_ip4(stralloc *out,stralloc *fqdn)
43{
44 unsigned int i;
45 char code;
46 char ch;
47 char ip[4];
48 int r;
49 int rc = 0;
50
51 if (!stralloc_copys(out,"")) return DNS_MEM;
52 if (!stralloc_readyplus(fqdn,1)) return DNS_MEM;
53
54 fqdn->s[fqdn->len] = 0; /* if FQDN is just IPv4 */
55 if (ip4_scan(fqdn->s,ip)) {
56 if (!stralloc_copyb(out,ip,4)) return DNS_MEM;
57 return 1;
58 }
59
60 code = 0;
61 for (i = 0; i <= fqdn->len; ++i) {
62 if (i < fqdn->len)
63 ch = fqdn->s[i];
64 else
65 ch = '.';
66
67 if ((ch == '[') || (ch == ']')) continue;
68 if (ch == '.') {
69 if (!stralloc_append(out,&code)) return DNS_MEM;
70 code = 0;
71 continue;
72 }
73 if ((ch >= '0') && (ch <= '9')) {
74 code *= 10;
75 code += ch - '0';
76 continue;
77 }
78
79 if (dns_domain_fromdot(&q,fqdn->s,fqdn->len) <= 0) return DNS_ERR; // fdqn -> A query -> response
80 if (dns_resolve(q,DNS_T_A) >= 0) {
84 rc += r;
85 }
86 return rc;
87 }
88
89 out->len &= ~3;
90 return 0;
91}
92
93int dns_ip6_packet(stralloc *out,const char *buf,unsigned int len)
94{
95 unsigned int pos;
96 char header[16];
97 uint16 numanswers;
98 uint16 datalen;
99 int ranswers = 0;
100
101 if (!stralloc_copys(out,"")) return DNS_MEM;
102
103 pos = dns_packet_copy(buf,len,0,header,12); if (!pos) return DNS_ERR;
104 uint16_unpack_big(header + 6,&numanswers);
105 pos = dns_packet_skipname(buf,len,pos); if (!pos) return DNS_ERR;
106 pos += 4;
107
108 while (numanswers--) {
109 pos = dns_packet_skipname(buf,len,pos); if (!pos) return DNS_ERR;
110 pos = dns_packet_copy(buf,len,pos,header,10); if (!pos) return DNS_ERR;
111 uint16_unpack_big(header + 8,&datalen);
112 if (byte_equal(header,2,DNS_T_AAAA)) {
113 if (byte_equal(header + 2,2,DNS_C_IN))
114 if (datalen == 16) {
115 if (!dns_packet_copy(buf,len,pos,header,16)) return DNS_ERR;
116 if (!stralloc_catb(out,header,16)) return DNS_MEM;
117 }
118 } else if (byte_equal(header,2,DNS_T_A))
119 if (byte_equal(header + 2,2,DNS_C_IN))
120 if (datalen == 4) {
121 byte_copy(header,12,V4mappedprefix);
122 if (!dns_packet_copy(buf,len,pos,header + 12,4)) return DNS_ERR;
123 if (!stralloc_catb(out,header,16)) return DNS_MEM;
124 }
125 pos += datalen;
126 ++ranswers;
127 }
128
129 dns_sortip6(out->s,out->len);
130 return ranswers;
131}
132
133int dns_ip6(stralloc *out,stralloc *fqdn)
134{
135 unsigned int i;
136 char code;
137 char ch;
138 char ip[16];
139 int r;
140 int rc = 0;
141
142 if (!stralloc_copys(out,"")) return DNS_MEM;
143 if (!stralloc_readyplus(fqdn,1)) return DNS_MEM;
144
145 fqdn->s[fqdn->len] = 0; /* if FQDN is just IPv6 */
146 if (ip6_scan(fqdn->s,ip)) {
147 if (!stralloc_copyb(out,ip,16)) return DNS_MEM;
148 return 1;
149 }
150
151 code = 0;
152 for (i = 0; i <= fqdn->len; ++i) {
153 if (i < fqdn->len)
154 ch = fqdn->s[i];
155 else
156 ch = '.';
157
158 if ((ch == '[') || (ch == ']')) continue;
159 if (ch == '.') {
160 if (!stralloc_append(out,&code)) return DNS_MEM;
161 code = 0;
162 continue;
163 }
164 if ((ch >= '0') && (ch <= '9')) {
165 code *= 10;
166 code += ch - '0';
167 continue;
168 }
169
170 if (dns_domain_fromdot(&q,fqdn->s,fqdn->len) <= 0) return DNS_ERR; // fqdn -> AAAA query -> response
171 if (dns_resolve(q,DNS_T_AAAA) >= 0) {
174 dns_domain_free(&q);
175 rc += r;
176 }
177 return rc;
178 }
179
180 out->len &= ~3;
181 return 0;
182}
char ip[16]
Definition: axfrdns.c:125
uint16 len
Definition: axfrdns.c:319
char buf[MSGSIZE]
Definition: axfrdns.c:318
#define DNS_ERR
Definition: dns.h:32
#define DNS_T_A
Definition: dns.h:56
#define DNS_C_IN
Definition: dns.h:53
#define DNS_T_AAAA
Definition: dns.h:67
#define DNS_MEM
Definition: dns.h:31
int dns_domain_fromdot(char **out, const char *buf, unsigned int n)
Definition: dns_dfd.c:6
void dns_domain_free(char **out)
Definition: dns_domain.c:17
int dns_ip4_packet(stralloc *out, const char *buf, unsigned int len)
Definition: dns_ip.c:9
int dns_ip6_packet(stralloc *out, const char *buf, unsigned int len)
Definition: dns_ip.c:93
int dns_ip4(stralloc *out, stralloc *fqdn)
Definition: dns_ip.c:42
int dns_ip6(stralloc *out, stralloc *fqdn)
Definition: dns_ip.c:133
unsigned int dns_packet_copy(const char *buf, unsigned int len, unsigned int pos, char *out, unsigned int outlen)
Definition: dns_packet.c:8
unsigned int dns_packet_skipname(const char *buf, unsigned int len, unsigned int pos)
Definition: dns_packet.c:18
int dns_resolve(const char *q, const char qtype[2])
Definition: dns_resolve.c:9
struct dns_transmit dns_resolve_tx
Definition: dns_resolve.c:7
void dns_sortip4(char *s, unsigned int n)
Definition: dns_sortip.c:10
void dns_sortip6(char *s, unsigned int n)
Definition: dns_sortip.c:25
void dns_transmit_free(struct dns_transmit *d)
Definition: dns_transmit.c:95
void out(const char *s, unsigned int len)
Definition: generic-conf.c:54
unsigned int packetlen
Definition: dns.h:96
char * packet
Definition: dns.h:95