-
Notifications
You must be signed in to change notification settings - Fork 3
/
edns.cpp
166 lines (139 loc) · 4.71 KB
/
edns.cpp
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
/**
* Module that supports basic Extended DNS.
*
* See also: RFC2761
*/
#include "edns.h"
#include "dns.h"
#include "dns_data.h"
#include "settings.h"
#include <algorithm> // for std::min()
int edns_answer_packet(char* pkt_out, int buf_len, struct dns_header* hdr_out, struct edns_options* opts)
{
uint32 flags;
int pkt_out_len = 0;
if (opts->has_edns) {
if (opts->version != EDNS_VERSION) {
// XXX validate
}
// 1 byte empty qname, 2 bytes type, 2 bytes payload_size, 4 bytes flags, 2 bytes rdlength always 0
if (buf_len < 1 + 2 + 2 + 4 + 2) {
return -1;
}
hdr_out->arcount++;
// empty root name
*pkt_out = '\0';
pkt_out_len++;
flags = opts->extended_flags & DNS_EXT_HEADER_VALID_MASK;
flags |= ((uint32)opts->extended_rcode) << 24;
flags |= ((uint32)EDNS_VERSION) << 16;
pkt_out_len += dns_uint16_encode(DNS_TYPE_OPT, pkt_out + pkt_out_len);
pkt_out_len += dns_uint16_encode(opts->udp_payload_size, pkt_out + pkt_out_len);
pkt_out_len += dns_uint32_encode(flags, pkt_out + pkt_out_len);
pkt_out_len += dns_uint16_encode(0, pkt_out + pkt_out_len); // no data, yet
//XYZ: removed empty lines
}
return pkt_out_len;
}
int edns_handle_packet(const char* pkt_in, const int len, struct edns_options* opts)
{
struct dns_header h;
struct dns_question q;
uint16 rdata_len;
uint16 rtype;
uint32 flags;
uint16 udp_payload_size = 0;
int i, reslen;
int pkt_in_len = 0;
char dummy_name[DNS_MAX_DOMAIN_LENGTH + 1];
q.name = dummy_name;
if (0 == opts) {
return 0;
}
opts->has_edns = 0;
opts->extended_rcode = 0;
opts->extended_flags = 0;
opts->version = 0;
// At least 5 bytes needed for a question (one for '.' and 2 each for class and type)
if (len < pkt_in_len + DNS_HEADER_LENGTH + 5) {
return -1;
}
pkt_in_len = dns_header_decode(pkt_in, &h);
if (h.qdcount != 1) {
// Invalid packet or definately no opt record
return -1;
}
for (i = 0; i < h.qdcount; ++i) {
reslen = dns_question_decode(pkt_in + pkt_in_len, len - pkt_in_len, &q);
if (reslen <= 0) {
// Invalid question found, bailing
return -1;
}
pkt_in_len += reslen;
}
if (len < pkt_in_len)
return -1; // invalid packet
// skip sections we are not interested in
for (i = 0; i < h.ancount + h.nscount; ++i) {
reslen = dns_domain_length(pkt_in + pkt_in_len, len - pkt_in_len);
if (reslen <= 0) {
return -1;
}
pkt_in_len += reslen;
pkt_in_len += 2 + 2 + 4; // type, class, ttl
if (len < pkt_in_len + 2) {
// +2 for the rdatalen
return -1; // invalid packet
}
dns_uint16_decode(pkt_in + pkt_in_len, &rdata_len);
pkt_in_len += 2; // rdata len
pkt_in_len += rdata_len;
if (len < pkt_in_len) {
return -1; // invalid packet
}
}
// finally, we've hit the ar sections
for (i = 0; i < h.arcount; ++i) {
reslen = dns_domain_length(pkt_in + pkt_in_len, len - pkt_in_len);
if (reslen <= 0) {
return -1;
}
pkt_in_len += reslen;
if (len < (pkt_in_len + 2 + 2 + 4 + 2)) {
return -1;
}
dns_uint16_decode(pkt_in + pkt_in_len, &rtype);
pkt_in_len += 2;
if (rtype == DNS_TYPE_OPT) {
dns_uint16_decode(pkt_in + pkt_in_len, &udp_payload_size);
pkt_in_len += 2;
dns_uint32_decode(pkt_in + pkt_in_len, &flags);
pkt_in_len += 4;
dns_uint16_decode(pkt_in + pkt_in_len, &rdata_len);
pkt_in_len += 2;
opts->options = pkt_in + pkt_in_len;
opts->extended_rcode = (flags & 0xFF000000) >> 24;
opts->extended_flags = (flags & 0x0000FFFF);
opts->version = (flags & 0x00FF0000) >> 16;
opts->has_edns = 1;
if (pkt_in_len + rdata_len > len) {
return -1;
}
break;
} else {
pkt_in_len += 2 + 4; // class, ttl
dns_uint16_decode(pkt_in + pkt_in_len, &rdata_len);
pkt_in_len += 2; // rdata len
pkt_in_len += rdata_len;
if (pkt_in_len > len) {
return -1;
}
}
}
if (udp_payload_size != 0) {
//XYZ changed int -> uint16
opts->udp_payload_size = std::min((uint16)udp_payload_size, (uint16)MAX_PACKET_SIZE);
opts->udp_payload_size = std::min((uint16)opts->udp_payload_size, (uint16)max_udp_payload_size);
}
return opts->has_edns;
}