testpkts.c revision 1.1.1.3 1 1.1 christos /*
2 1.1 christos * testpkts. Data file parse for test packets, and query matching.
3 1.1 christos *
4 1.1 christos * Data storage for specially crafted replies for testing purposes.
5 1.1 christos *
6 1.1 christos * (c) NLnet Labs, 2005, 2006, 2007, 2008
7 1.1 christos * See the file LICENSE for the license
8 1.1 christos */
9 1.1 christos
10 1.1 christos /**
11 1.1 christos * \file
12 1.1 christos * This is a debugging aid. It is not efficient, especially
13 1.1 christos * with a long config file, but it can give any reply to any query.
14 1.1 christos * This can help the developer pre-script replies for queries.
15 1.1 christos *
16 1.1 christos * You can specify a packet RR by RR with header flags to return.
17 1.1 christos *
18 1.1 christos * Missing features:
19 1.1 christos * - matching content different from reply content.
20 1.1 christos * - find way to adjust mangled packets?
21 1.1 christos */
22 1.1 christos
23 1.1 christos #include "config.h"
24 1.1 christos struct sockaddr_storage;
25 1.1 christos #include <errno.h>
26 1.1 christos #include <stdarg.h>
27 1.1 christos #include <ctype.h>
28 1.1 christos #include "testcode/testpkts.h"
29 1.1 christos #include "util/net_help.h"
30 1.1 christos #include "sldns/sbuffer.h"
31 1.1 christos #include "sldns/rrdef.h"
32 1.1 christos #include "sldns/pkthdr.h"
33 1.1 christos #include "sldns/str2wire.h"
34 1.1 christos #include "sldns/wire2str.h"
35 1.1 christos
36 1.1 christos /** max size of a packet */
37 1.1 christos #define MAX_PACKETLEN 65536
38 1.1 christos /** max line length */
39 1.1 christos #define MAX_LINE 10240
40 1.1 christos /** string to show in warnings and errors */
41 1.1 christos static const char* prog_name = "testpkts";
42 1.1 christos
43 1.1 christos #ifndef UTIL_LOG_H
44 1.1 christos /** verbosity definition for compat */
45 1.1 christos enum verbosity_value { NO_VERBOSE=0 };
46 1.1 christos #endif
47 1.1 christos /** logging routine, provided by caller */
48 1.1 christos void verbose(enum verbosity_value lvl, const char* msg, ...) ATTR_FORMAT(printf, 2, 3);
49 1.1 christos
50 1.1 christos /** print error and exit */
51 1.1 christos static void error(const char* msg, ...)
52 1.1 christos {
53 1.1 christos va_list args;
54 1.1 christos va_start(args, msg);
55 1.1 christos fprintf(stderr, "%s error: ", prog_name);
56 1.1 christos vfprintf(stderr, msg, args);
57 1.1 christos fprintf(stderr, "\n");
58 1.1 christos fflush(stderr);
59 1.1 christos va_end(args);
60 1.1 christos exit(EXIT_FAILURE);
61 1.1 christos }
62 1.1 christos
63 1.1 christos /** return if string is empty or comment */
64 1.1 christos static int isendline(char c)
65 1.1 christos {
66 1.1 christos if(c == ';' || c == '#'
67 1.1 christos || c == '\n' || c == 0)
68 1.1 christos return 1;
69 1.1 christos return 0;
70 1.1 christos }
71 1.1 christos
72 1.1 christos /** true if the string starts with the keyword given. Moves the str ahead.
73 1.1 christos * @param str: before keyword, afterwards after keyword and spaces.
74 1.1 christos * @param keyword: the keyword to match
75 1.1 christos * @return: true if keyword present. False otherwise, and str unchanged.
76 1.1 christos */
77 1.1 christos static int str_keyword(char** str, const char* keyword)
78 1.1 christos {
79 1.1 christos size_t len = strlen(keyword);
80 1.1 christos assert(str && keyword);
81 1.1 christos if(strncmp(*str, keyword, len) != 0)
82 1.1 christos return 0;
83 1.1 christos *str += len;
84 1.1 christos while(isspace((unsigned char)**str))
85 1.1 christos (*str)++;
86 1.1 christos return 1;
87 1.1 christos }
88 1.1 christos
89 1.1 christos /** Add reply packet to entry */
90 1.1 christos static struct reply_packet*
91 1.1 christos entry_add_reply(struct entry* entry)
92 1.1 christos {
93 1.1 christos struct reply_packet* pkt = (struct reply_packet*)malloc(
94 1.1 christos sizeof(struct reply_packet));
95 1.1 christos struct reply_packet ** p = &entry->reply_list;
96 1.1 christos if(!pkt) error("out of memory");
97 1.1 christos pkt->next = NULL;
98 1.1 christos pkt->packet_sleep = 0;
99 1.1 christos pkt->reply_pkt = NULL;
100 1.1 christos pkt->reply_from_hex = NULL;
101 1.1.1.2 christos pkt->raw_ednsdata = NULL;
102 1.1 christos /* link at end */
103 1.1 christos while(*p)
104 1.1 christos p = &((*p)->next);
105 1.1 christos *p = pkt;
106 1.1 christos return pkt;
107 1.1 christos }
108 1.1 christos
109 1.1 christos /** parse MATCH line */
110 1.1 christos static void matchline(char* line, struct entry* e)
111 1.1 christos {
112 1.1 christos char* parse = line;
113 1.1 christos while(*parse) {
114 1.1 christos if(isendline(*parse))
115 1.1 christos return;
116 1.1 christos if(str_keyword(&parse, "opcode")) {
117 1.1 christos e->match_opcode = 1;
118 1.1 christos } else if(str_keyword(&parse, "qtype")) {
119 1.1 christos e->match_qtype = 1;
120 1.1 christos } else if(str_keyword(&parse, "qname")) {
121 1.1 christos e->match_qname = 1;
122 1.1.1.2 christos } else if(str_keyword(&parse, "rcode")) {
123 1.1.1.2 christos e->match_rcode = 1;
124 1.1.1.2 christos } else if(str_keyword(&parse, "question")) {
125 1.1.1.2 christos e->match_question = 1;
126 1.1.1.2 christos } else if(str_keyword(&parse, "answer")) {
127 1.1.1.2 christos e->match_answer = 1;
128 1.1 christos } else if(str_keyword(&parse, "subdomain")) {
129 1.1 christos e->match_subdomain = 1;
130 1.1 christos } else if(str_keyword(&parse, "all")) {
131 1.1 christos e->match_all = 1;
132 1.1 christos } else if(str_keyword(&parse, "ttl")) {
133 1.1 christos e->match_ttl = 1;
134 1.1 christos } else if(str_keyword(&parse, "DO")) {
135 1.1 christos e->match_do = 1;
136 1.1 christos } else if(str_keyword(&parse, "noedns")) {
137 1.1 christos e->match_noedns = 1;
138 1.1.1.2 christos } else if(str_keyword(&parse, "ednsdata")) {
139 1.1.1.2 christos e->match_ednsdata_raw = 1;
140 1.1 christos } else if(str_keyword(&parse, "UDP")) {
141 1.1 christos e->match_transport = transport_udp;
142 1.1 christos } else if(str_keyword(&parse, "TCP")) {
143 1.1 christos e->match_transport = transport_tcp;
144 1.1 christos } else if(str_keyword(&parse, "serial")) {
145 1.1 christos e->match_serial = 1;
146 1.1 christos if(*parse != '=' && *parse != ':')
147 1.1 christos error("expected = or : in MATCH: %s", line);
148 1.1 christos parse++;
149 1.1 christos e->ixfr_soa_serial = (uint32_t)strtol(parse, (char**)&parse, 10);
150 1.1 christos while(isspace((unsigned char)*parse))
151 1.1 christos parse++;
152 1.1 christos } else {
153 1.1 christos error("could not parse MATCH: '%s'", parse);
154 1.1 christos }
155 1.1 christos }
156 1.1 christos }
157 1.1 christos
158 1.1 christos /** parse REPLY line */
159 1.1 christos static void replyline(char* line, uint8_t* reply, size_t reply_len,
160 1.1 christos int* do_flag)
161 1.1 christos {
162 1.1 christos char* parse = line;
163 1.1 christos if(reply_len < LDNS_HEADER_SIZE) error("packet too short for header");
164 1.1 christos while(*parse) {
165 1.1 christos if(isendline(*parse))
166 1.1 christos return;
167 1.1 christos /* opcodes */
168 1.1 christos if(str_keyword(&parse, "QUERY")) {
169 1.1 christos LDNS_OPCODE_SET(reply, LDNS_PACKET_QUERY);
170 1.1 christos } else if(str_keyword(&parse, "IQUERY")) {
171 1.1 christos LDNS_OPCODE_SET(reply, LDNS_PACKET_IQUERY);
172 1.1 christos } else if(str_keyword(&parse, "STATUS")) {
173 1.1 christos LDNS_OPCODE_SET(reply, LDNS_PACKET_STATUS);
174 1.1 christos } else if(str_keyword(&parse, "NOTIFY")) {
175 1.1 christos LDNS_OPCODE_SET(reply, LDNS_PACKET_NOTIFY);
176 1.1 christos } else if(str_keyword(&parse, "UPDATE")) {
177 1.1 christos LDNS_OPCODE_SET(reply, LDNS_PACKET_UPDATE);
178 1.1 christos /* rcodes */
179 1.1 christos } else if(str_keyword(&parse, "NOERROR")) {
180 1.1 christos LDNS_RCODE_SET(reply, LDNS_RCODE_NOERROR);
181 1.1 christos } else if(str_keyword(&parse, "FORMERR")) {
182 1.1 christos LDNS_RCODE_SET(reply, LDNS_RCODE_FORMERR);
183 1.1 christos } else if(str_keyword(&parse, "SERVFAIL")) {
184 1.1 christos LDNS_RCODE_SET(reply, LDNS_RCODE_SERVFAIL);
185 1.1 christos } else if(str_keyword(&parse, "NXDOMAIN")) {
186 1.1 christos LDNS_RCODE_SET(reply, LDNS_RCODE_NXDOMAIN);
187 1.1 christos } else if(str_keyword(&parse, "NOTIMPL")) {
188 1.1 christos LDNS_RCODE_SET(reply, LDNS_RCODE_NOTIMPL);
189 1.1 christos } else if(str_keyword(&parse, "REFUSED")) {
190 1.1 christos LDNS_RCODE_SET(reply, LDNS_RCODE_REFUSED);
191 1.1 christos } else if(str_keyword(&parse, "YXDOMAIN")) {
192 1.1 christos LDNS_RCODE_SET(reply, LDNS_RCODE_YXDOMAIN);
193 1.1 christos } else if(str_keyword(&parse, "YXRRSET")) {
194 1.1 christos LDNS_RCODE_SET(reply, LDNS_RCODE_YXRRSET);
195 1.1 christos } else if(str_keyword(&parse, "NXRRSET")) {
196 1.1 christos LDNS_RCODE_SET(reply, LDNS_RCODE_NXRRSET);
197 1.1 christos } else if(str_keyword(&parse, "NOTAUTH")) {
198 1.1 christos LDNS_RCODE_SET(reply, LDNS_RCODE_NOTAUTH);
199 1.1 christos } else if(str_keyword(&parse, "NOTZONE")) {
200 1.1 christos LDNS_RCODE_SET(reply, LDNS_RCODE_NOTZONE);
201 1.1 christos /* flags */
202 1.1 christos } else if(str_keyword(&parse, "QR")) {
203 1.1 christos LDNS_QR_SET(reply);
204 1.1 christos } else if(str_keyword(&parse, "AA")) {
205 1.1 christos LDNS_AA_SET(reply);
206 1.1 christos } else if(str_keyword(&parse, "TC")) {
207 1.1 christos LDNS_TC_SET(reply);
208 1.1 christos } else if(str_keyword(&parse, "RD")) {
209 1.1 christos LDNS_RD_SET(reply);
210 1.1 christos } else if(str_keyword(&parse, "CD")) {
211 1.1 christos LDNS_CD_SET(reply);
212 1.1 christos } else if(str_keyword(&parse, "RA")) {
213 1.1 christos LDNS_RA_SET(reply);
214 1.1 christos } else if(str_keyword(&parse, "AD")) {
215 1.1 christos LDNS_AD_SET(reply);
216 1.1 christos } else if(str_keyword(&parse, "DO")) {
217 1.1 christos *do_flag = 1;
218 1.1 christos } else {
219 1.1 christos error("could not parse REPLY: '%s'", parse);
220 1.1 christos }
221 1.1 christos }
222 1.1 christos }
223 1.1 christos
224 1.1 christos /** parse ADJUST line */
225 1.1 christos static void adjustline(char* line, struct entry* e,
226 1.1 christos struct reply_packet* pkt)
227 1.1 christos {
228 1.1 christos char* parse = line;
229 1.1 christos while(*parse) {
230 1.1 christos if(isendline(*parse))
231 1.1 christos return;
232 1.1 christos if(str_keyword(&parse, "copy_id")) {
233 1.1 christos e->copy_id = 1;
234 1.1 christos } else if(str_keyword(&parse, "copy_query")) {
235 1.1 christos e->copy_query = 1;
236 1.1.1.2 christos } else if(str_keyword(&parse, "copy_ednsdata_assume_clientsubnet")) {
237 1.1.1.2 christos e->copy_ednsdata_assume_clientsubnet = 1;
238 1.1 christos } else if(str_keyword(&parse, "sleep=")) {
239 1.1 christos e->sleeptime = (unsigned int) strtol(parse, (char**)&parse, 10);
240 1.1 christos while(isspace((unsigned char)*parse))
241 1.1 christos parse++;
242 1.1 christos } else if(str_keyword(&parse, "packet_sleep=")) {
243 1.1 christos pkt->packet_sleep = (unsigned int) strtol(parse, (char**)&parse, 10);
244 1.1 christos while(isspace((unsigned char)*parse))
245 1.1 christos parse++;
246 1.1 christos } else {
247 1.1 christos error("could not parse ADJUST: '%s'", parse);
248 1.1 christos }
249 1.1 christos }
250 1.1 christos }
251 1.1 christos
252 1.1 christos /** create new entry */
253 1.1.1.2 christos static struct entry* new_entry(void)
254 1.1 christos {
255 1.1 christos struct entry* e = (struct entry*)malloc(sizeof(struct entry));
256 1.1 christos if(!e) error("out of memory");
257 1.1 christos memset(e, 0, sizeof(*e));
258 1.1 christos e->match_opcode = 0;
259 1.1 christos e->match_qtype = 0;
260 1.1 christos e->match_qname = 0;
261 1.1.1.2 christos e->match_rcode = 0;
262 1.1.1.2 christos e->match_question = 0;
263 1.1.1.2 christos e->match_answer = 0;
264 1.1 christos e->match_subdomain = 0;
265 1.1 christos e->match_all = 0;
266 1.1 christos e->match_ttl = 0;
267 1.1 christos e->match_do = 0;
268 1.1 christos e->match_noedns = 0;
269 1.1 christos e->match_serial = 0;
270 1.1 christos e->ixfr_soa_serial = 0;
271 1.1 christos e->match_transport = transport_any;
272 1.1 christos e->reply_list = NULL;
273 1.1 christos e->copy_id = 0;
274 1.1 christos e->copy_query = 0;
275 1.1.1.2 christos e->copy_ednsdata_assume_clientsubnet = 0;
276 1.1 christos e->sleeptime = 0;
277 1.1 christos e->next = NULL;
278 1.1 christos return e;
279 1.1 christos }
280 1.1 christos
281 1.1 christos /**
282 1.1 christos * Converts a hex string to binary data
283 1.1 christos * @param hexstr: string of hex.
284 1.1 christos * @param len: is the length of the string
285 1.1 christos * @param buf: is the buffer to store the result in
286 1.1 christos * @param offset: is the starting position in the result buffer
287 1.1 christos * @param buf_len: is the length of buf.
288 1.1 christos * @return This function returns the length of the result
289 1.1 christos */
290 1.1 christos static size_t
291 1.1 christos hexstr2bin(char *hexstr, int len, uint8_t *buf, size_t offset, size_t buf_len)
292 1.1 christos {
293 1.1 christos char c;
294 1.1 christos int i;
295 1.1 christos uint8_t int8 = 0;
296 1.1 christos int sec = 0;
297 1.1 christos size_t bufpos = 0;
298 1.1 christos
299 1.1 christos if (len % 2 != 0) {
300 1.1 christos return 0;
301 1.1 christos }
302 1.1 christos
303 1.1 christos for (i=0; i<len; i++) {
304 1.1 christos c = hexstr[i];
305 1.1 christos
306 1.1 christos /* case insensitive, skip spaces */
307 1.1 christos if (c != ' ') {
308 1.1 christos if (c >= '0' && c <= '9') {
309 1.1 christos int8 += c & 0x0f;
310 1.1 christos } else if (c >= 'a' && c <= 'z') {
311 1.1 christos int8 += (c & 0x0f) + 9;
312 1.1 christos } else if (c >= 'A' && c <= 'Z') {
313 1.1 christos int8 += (c & 0x0f) + 9;
314 1.1 christos } else {
315 1.1 christos return 0;
316 1.1 christos }
317 1.1 christos
318 1.1 christos if (sec == 0) {
319 1.1 christos int8 = int8 << 4;
320 1.1 christos sec = 1;
321 1.1 christos } else {
322 1.1 christos if (bufpos + offset + 1 <= buf_len) {
323 1.1 christos buf[bufpos+offset] = int8;
324 1.1 christos int8 = 0;
325 1.1 christos sec = 0;
326 1.1 christos bufpos++;
327 1.1 christos } else {
328 1.1 christos fprintf(stderr, "Buffer too small in hexstr2bin");
329 1.1 christos }
330 1.1 christos }
331 1.1 christos }
332 1.1 christos }
333 1.1 christos return bufpos;
334 1.1 christos }
335 1.1 christos
336 1.1 christos /** convert hex buffer to binary buffer */
337 1.1 christos static sldns_buffer *
338 1.1 christos hex_buffer2wire(sldns_buffer *data_buffer)
339 1.1 christos {
340 1.1 christos sldns_buffer *wire_buffer = NULL;
341 1.1 christos int c;
342 1.1 christos
343 1.1 christos /* stat hack
344 1.1 christos * 0 = normal
345 1.1 christos * 1 = comment (skip to end of line)
346 1.1 christos * 2 = unprintable character found, read binary data directly
347 1.1 christos */
348 1.1 christos size_t data_buf_pos = 0;
349 1.1 christos int state = 0;
350 1.1 christos uint8_t *hexbuf;
351 1.1 christos int hexbufpos = 0;
352 1.1 christos size_t wirelen;
353 1.1 christos uint8_t *data_wire = (uint8_t *) sldns_buffer_begin(data_buffer);
354 1.1 christos uint8_t *wire = (uint8_t*)malloc(MAX_PACKETLEN);
355 1.1 christos if(!wire) error("out of memory");
356 1.1 christos
357 1.1 christos hexbuf = (uint8_t*)malloc(MAX_PACKETLEN);
358 1.1 christos if(!hexbuf) error("out of memory");
359 1.1 christos for (data_buf_pos = 0; data_buf_pos < sldns_buffer_position(data_buffer); data_buf_pos++) {
360 1.1 christos c = (int) data_wire[data_buf_pos];
361 1.1 christos
362 1.1 christos if (state < 2 && !isascii((unsigned char)c)) {
363 1.1 christos /*verbose("non ascii character found in file: (%d) switching to raw mode\n", c);*/
364 1.1 christos state = 2;
365 1.1 christos }
366 1.1 christos switch (state) {
367 1.1 christos case 0:
368 1.1 christos if ( (c >= '0' && c <= '9') ||
369 1.1 christos (c >= 'a' && c <= 'f') ||
370 1.1 christos (c >= 'A' && c <= 'F') )
371 1.1 christos {
372 1.1 christos if (hexbufpos >= MAX_PACKETLEN) {
373 1.1 christos error("buffer overflow");
374 1.1 christos free(hexbuf);
375 1.1 christos return 0;
376 1.1 christos
377 1.1 christos }
378 1.1 christos hexbuf[hexbufpos] = (uint8_t) c;
379 1.1 christos hexbufpos++;
380 1.1 christos } else if (c == ';') {
381 1.1 christos state = 1;
382 1.1 christos } else if (c == ' ' || c == '\t' || c == '\n') {
383 1.1 christos /* skip whitespace */
384 1.1 christos }
385 1.1 christos break;
386 1.1 christos case 1:
387 1.1 christos if (c == '\n' || c == EOF) {
388 1.1 christos state = 0;
389 1.1 christos }
390 1.1 christos break;
391 1.1 christos case 2:
392 1.1 christos if (hexbufpos >= MAX_PACKETLEN) {
393 1.1 christos error("buffer overflow");
394 1.1 christos free(hexbuf);
395 1.1 christos return 0;
396 1.1 christos }
397 1.1 christos hexbuf[hexbufpos] = (uint8_t) c;
398 1.1 christos hexbufpos++;
399 1.1 christos break;
400 1.1 christos }
401 1.1 christos }
402 1.1 christos
403 1.1 christos if (hexbufpos >= MAX_PACKETLEN) {
404 1.1 christos /*verbose("packet size reached\n");*/
405 1.1 christos }
406 1.1 christos
407 1.1 christos /* lenient mode: length must be multiple of 2 */
408 1.1 christos if (hexbufpos % 2 != 0) {
409 1.1 christos if (hexbufpos >= MAX_PACKETLEN) {
410 1.1 christos error("buffer overflow");
411 1.1 christos free(hexbuf);
412 1.1 christos return 0;
413 1.1 christos }
414 1.1 christos hexbuf[hexbufpos] = (uint8_t) '0';
415 1.1 christos hexbufpos++;
416 1.1 christos }
417 1.1 christos
418 1.1 christos if (state < 2) {
419 1.1 christos wirelen = hexstr2bin((char *) hexbuf, hexbufpos, wire, 0, MAX_PACKETLEN);
420 1.1 christos wire_buffer = sldns_buffer_new(wirelen);
421 1.1 christos sldns_buffer_new_frm_data(wire_buffer, wire, wirelen);
422 1.1 christos } else {
423 1.1 christos error("Incomplete hex data, not at byte boundary\n");
424 1.1 christos }
425 1.1 christos free(wire);
426 1.1 christos free(hexbuf);
427 1.1 christos return wire_buffer;
428 1.1 christos }
429 1.1 christos
430 1.1 christos /** parse ORIGIN */
431 1.1 christos static void
432 1.1 christos get_origin(const char* name, struct sldns_file_parse_state* pstate, char* parse)
433 1.1 christos {
434 1.1 christos /* snip off rest of the text so as to make the parse work in ldns */
435 1.1 christos char* end;
436 1.1 christos char store;
437 1.1 christos int status;
438 1.1 christos
439 1.1 christos end=parse;
440 1.1 christos while(!isspace((unsigned char)*end) && !isendline(*end))
441 1.1 christos end++;
442 1.1 christos store = *end;
443 1.1 christos *end = 0;
444 1.1 christos verbose(3, "parsing '%s'\n", parse);
445 1.1 christos status = sldns_str2wire_dname_buf(parse, pstate->origin,
446 1.1 christos &pstate->origin_len);
447 1.1 christos *end = store;
448 1.1 christos if(status != 0)
449 1.1 christos error("%s line %d:\n\t%s: %s", name, pstate->lineno,
450 1.1 christos sldns_get_errorstr_parse(status), parse);
451 1.1 christos }
452 1.1 christos
453 1.1 christos /** add RR to packet */
454 1.1 christos static void add_rr(char* rrstr, uint8_t* pktbuf, size_t pktsize,
455 1.1 christos size_t* pktlen, struct sldns_file_parse_state* pstate,
456 1.1 christos sldns_pkt_section add_section, const char* fname)
457 1.1 christos {
458 1.1 christos /* it must be a RR, parse and add to packet. */
459 1.1 christos size_t rr_len = pktsize - *pktlen;
460 1.1 christos size_t dname_len = 0;
461 1.1 christos int status;
462 1.1 christos uint8_t* origin = pstate->origin_len?pstate->origin:0;
463 1.1 christos uint8_t* prev = pstate->prev_rr_len?pstate->prev_rr:0;
464 1.1 christos if(*pktlen > pktsize || *pktlen < LDNS_HEADER_SIZE)
465 1.1 christos error("packet overflow");
466 1.1 christos
467 1.1 christos /* parse RR */
468 1.1 christos if(add_section == LDNS_SECTION_QUESTION)
469 1.1 christos status = sldns_str2wire_rr_question_buf(rrstr, pktbuf+*pktlen,
470 1.1 christos &rr_len, &dname_len, origin, pstate->origin_len,
471 1.1 christos prev, pstate->prev_rr_len);
472 1.1 christos else status = sldns_str2wire_rr_buf(rrstr, pktbuf+*pktlen, &rr_len,
473 1.1 christos &dname_len, pstate->default_ttl, origin,
474 1.1 christos pstate->origin_len, prev, pstate->prev_rr_len);
475 1.1 christos if(status != 0)
476 1.1 christos error("%s line %d:%d %s\n\t%s", fname, pstate->lineno,
477 1.1 christos LDNS_WIREPARSE_OFFSET(status),
478 1.1 christos sldns_get_errorstr_parse(status), rrstr);
479 1.1 christos *pktlen += rr_len;
480 1.1 christos
481 1.1 christos /* increase RR count */
482 1.1 christos if(add_section == LDNS_SECTION_QUESTION)
483 1.1 christos sldns_write_uint16(pktbuf+4, LDNS_QDCOUNT(pktbuf)+1);
484 1.1 christos else if(add_section == LDNS_SECTION_ANSWER)
485 1.1 christos sldns_write_uint16(pktbuf+6, LDNS_ANCOUNT(pktbuf)+1);
486 1.1 christos else if(add_section == LDNS_SECTION_AUTHORITY)
487 1.1 christos sldns_write_uint16(pktbuf+8, LDNS_NSCOUNT(pktbuf)+1);
488 1.1 christos else if(add_section == LDNS_SECTION_ADDITIONAL)
489 1.1 christos sldns_write_uint16(pktbuf+10, LDNS_ARCOUNT(pktbuf)+1);
490 1.1 christos else error("internal error bad section %d", (int)add_section);
491 1.1 christos }
492 1.1 christos
493 1.1.1.2 christos /* add EDNS 4096 opt record */
494 1.1 christos static void
495 1.1.1.2 christos add_edns(uint8_t* pktbuf, size_t pktsize, int do_flag, uint8_t *ednsdata,
496 1.1.1.2 christos uint16_t ednslen, size_t* pktlen)
497 1.1 christos {
498 1.1 christos uint8_t edns[] = {0x00, /* root label */
499 1.1 christos 0x00, LDNS_RR_TYPE_OPT, /* type */
500 1.1 christos 0x10, 0x00, /* class is UDPSIZE 4096 */
501 1.1 christos 0x00, /* TTL[0] is ext rcode */
502 1.1 christos 0x00, /* TTL[1] is edns version */
503 1.1.1.2 christos (uint8_t)(do_flag?0x80:0x00), 0x00, /* TTL[2-3] is edns flags, DO */
504 1.1.1.2 christos (uint8_t)((ednslen >> 8) & 0xff),
505 1.1.1.2 christos (uint8_t)(ednslen & 0xff), /* rdatalength */
506 1.1 christos };
507 1.1 christos if(*pktlen < LDNS_HEADER_SIZE)
508 1.1 christos return;
509 1.1.1.2 christos if(*pktlen + sizeof(edns) + ednslen > pktsize)
510 1.1 christos error("not enough space for EDNS OPT record");
511 1.1 christos memmove(pktbuf+*pktlen, edns, sizeof(edns));
512 1.1.1.2 christos memmove(pktbuf+*pktlen+sizeof(edns), ednsdata, ednslen);
513 1.1 christos sldns_write_uint16(pktbuf+10, LDNS_ARCOUNT(pktbuf)+1);
514 1.1.1.2 christos *pktlen += (sizeof(edns) + ednslen);
515 1.1 christos }
516 1.1 christos
517 1.1 christos /* Reads one entry from file. Returns entry or NULL on error. */
518 1.1 christos struct entry*
519 1.1 christos read_entry(FILE* in, const char* name, struct sldns_file_parse_state* pstate,
520 1.1 christos int skip_whitespace)
521 1.1 christos {
522 1.1 christos struct entry* current = NULL;
523 1.1 christos char line[MAX_LINE];
524 1.1 christos char* parse;
525 1.1 christos sldns_pkt_section add_section = LDNS_SECTION_QUESTION;
526 1.1 christos struct reply_packet *cur_reply = NULL;
527 1.1 christos int reading_hex = 0;
528 1.1.1.2 christos int reading_hex_ednsdata = 0;
529 1.1 christos sldns_buffer* hex_data_buffer = NULL;
530 1.1.1.2 christos sldns_buffer* hex_ednsdata_buffer = NULL;
531 1.1 christos uint8_t pktbuf[MAX_PACKETLEN];
532 1.1 christos size_t pktlen = LDNS_HEADER_SIZE;
533 1.1 christos int do_flag = 0; /* DO flag in EDNS */
534 1.1 christos memset(pktbuf, 0, pktlen); /* ID = 0, FLAGS="", and rr counts 0 */
535 1.1 christos
536 1.1 christos while(fgets(line, (int)sizeof(line), in) != NULL) {
537 1.1 christos line[MAX_LINE-1] = 0;
538 1.1 christos parse = line;
539 1.1 christos pstate->lineno++;
540 1.1 christos
541 1.1 christos while(isspace((unsigned char)*parse))
542 1.1 christos parse++;
543 1.1 christos /* test for keywords */
544 1.1 christos if(isendline(*parse))
545 1.1 christos continue; /* skip comment and empty lines */
546 1.1 christos if(str_keyword(&parse, "ENTRY_BEGIN")) {
547 1.1 christos if(current) {
548 1.1 christos error("%s line %d: previous entry does not ENTRY_END",
549 1.1 christos name, pstate->lineno);
550 1.1 christos }
551 1.1 christos current = new_entry();
552 1.1 christos current->lineno = pstate->lineno;
553 1.1 christos cur_reply = entry_add_reply(current);
554 1.1 christos continue;
555 1.1 christos } else if(str_keyword(&parse, "$ORIGIN")) {
556 1.1 christos get_origin(name, pstate, parse);
557 1.1 christos continue;
558 1.1 christos } else if(str_keyword(&parse, "$TTL")) {
559 1.1 christos pstate->default_ttl = (uint32_t)atoi(parse);
560 1.1 christos continue;
561 1.1 christos }
562 1.1 christos
563 1.1 christos /* working inside an entry */
564 1.1 christos if(!current) {
565 1.1 christos error("%s line %d: expected ENTRY_BEGIN but got %s",
566 1.1 christos name, pstate->lineno, line);
567 1.1 christos }
568 1.1 christos if(str_keyword(&parse, "MATCH")) {
569 1.1 christos matchline(parse, current);
570 1.1 christos } else if(str_keyword(&parse, "REPLY")) {
571 1.1 christos replyline(parse, pktbuf, pktlen, &do_flag);
572 1.1 christos } else if(str_keyword(&parse, "ADJUST")) {
573 1.1 christos adjustline(parse, current, cur_reply);
574 1.1 christos } else if(str_keyword(&parse, "EXTRA_PACKET")) {
575 1.1.1.3 christos /* copy current packet into buffer */
576 1.1.1.3 christos cur_reply->reply_pkt = memdup(pktbuf, pktlen);
577 1.1.1.3 christos cur_reply->reply_len = pktlen;
578 1.1.1.3 christos if(!cur_reply->reply_pkt)
579 1.1.1.3 christos error("out of memory");
580 1.1 christos cur_reply = entry_add_reply(current);
581 1.1.1.3 christos /* clear for next packet */
582 1.1.1.3 christos pktlen = LDNS_HEADER_SIZE;
583 1.1.1.3 christos memset(pktbuf, 0, pktlen); /* ID = 0, FLAGS="", and rr counts 0 */
584 1.1 christos } else if(str_keyword(&parse, "SECTION")) {
585 1.1 christos if(str_keyword(&parse, "QUESTION"))
586 1.1 christos add_section = LDNS_SECTION_QUESTION;
587 1.1 christos else if(str_keyword(&parse, "ANSWER"))
588 1.1 christos add_section = LDNS_SECTION_ANSWER;
589 1.1 christos else if(str_keyword(&parse, "AUTHORITY"))
590 1.1 christos add_section = LDNS_SECTION_AUTHORITY;
591 1.1 christos else if(str_keyword(&parse, "ADDITIONAL"))
592 1.1 christos add_section = LDNS_SECTION_ADDITIONAL;
593 1.1 christos else error("%s line %d: bad section %s", name, pstate->lineno, parse);
594 1.1 christos } else if(str_keyword(&parse, "HEX_ANSWER_BEGIN")) {
595 1.1 christos hex_data_buffer = sldns_buffer_new(MAX_PACKETLEN);
596 1.1 christos reading_hex = 1;
597 1.1 christos } else if(str_keyword(&parse, "HEX_ANSWER_END")) {
598 1.1 christos if(!reading_hex) {
599 1.1 christos error("%s line %d: HEX_ANSWER_END read but no HEX_ANSWER_BEGIN keyword seen", name, pstate->lineno);
600 1.1 christos }
601 1.1 christos reading_hex = 0;
602 1.1 christos cur_reply->reply_from_hex = hex_buffer2wire(hex_data_buffer);
603 1.1 christos sldns_buffer_free(hex_data_buffer);
604 1.1 christos hex_data_buffer = NULL;
605 1.1.1.2 christos } else if(reading_hex) {
606 1.1.1.2 christos sldns_buffer_printf(hex_data_buffer, "%s", line);
607 1.1.1.2 christos } else if(str_keyword(&parse, "HEX_EDNSDATA_BEGIN")) {
608 1.1.1.2 christos hex_ednsdata_buffer = sldns_buffer_new(MAX_PACKETLEN);
609 1.1.1.2 christos reading_hex_ednsdata = 1;
610 1.1.1.2 christos } else if(str_keyword(&parse, "HEX_EDNSDATA_END")) {
611 1.1.1.2 christos if (!reading_hex_ednsdata) {
612 1.1.1.2 christos error("%s line %d: HEX_EDNSDATA_END read but no"
613 1.1.1.2 christos "HEX_EDNSDATA_BEGIN keyword seen", name, pstate->lineno);
614 1.1.1.2 christos }
615 1.1.1.2 christos reading_hex_ednsdata = 0;
616 1.1.1.2 christos cur_reply->raw_ednsdata = hex_buffer2wire(hex_ednsdata_buffer);
617 1.1.1.2 christos sldns_buffer_free(hex_ednsdata_buffer);
618 1.1.1.2 christos hex_ednsdata_buffer = NULL;
619 1.1.1.2 christos } else if(reading_hex_ednsdata) {
620 1.1.1.2 christos sldns_buffer_printf(hex_ednsdata_buffer, "%s", line);
621 1.1 christos } else if(str_keyword(&parse, "ENTRY_END")) {
622 1.1 christos if(hex_data_buffer)
623 1.1 christos sldns_buffer_free(hex_data_buffer);
624 1.1.1.2 christos if(hex_ednsdata_buffer)
625 1.1.1.2 christos sldns_buffer_free(hex_ednsdata_buffer);
626 1.1 christos if(pktlen != 0) {
627 1.1.1.2 christos if(do_flag || cur_reply->raw_ednsdata) {
628 1.1.1.2 christos if(cur_reply->raw_ednsdata &&
629 1.1.1.2 christos sldns_buffer_limit(cur_reply->raw_ednsdata))
630 1.1.1.2 christos add_edns(pktbuf, sizeof(pktbuf), do_flag,
631 1.1.1.2 christos sldns_buffer_begin(cur_reply->raw_ednsdata),
632 1.1.1.2 christos (uint16_t)sldns_buffer_limit(cur_reply->raw_ednsdata),
633 1.1.1.2 christos &pktlen);
634 1.1.1.2 christos else
635 1.1.1.2 christos add_edns(pktbuf, sizeof(pktbuf), do_flag,
636 1.1.1.2 christos NULL, 0, &pktlen);
637 1.1.1.2 christos }
638 1.1 christos cur_reply->reply_pkt = memdup(pktbuf, pktlen);
639 1.1 christos cur_reply->reply_len = pktlen;
640 1.1 christos if(!cur_reply->reply_pkt)
641 1.1 christos error("out of memory");
642 1.1 christos }
643 1.1 christos return current;
644 1.1 christos } else {
645 1.1 christos add_rr(skip_whitespace?parse:line, pktbuf,
646 1.1 christos sizeof(pktbuf), &pktlen, pstate, add_section,
647 1.1 christos name);
648 1.1 christos }
649 1.1 christos
650 1.1 christos }
651 1.1.1.2 christos if(reading_hex) {
652 1.1 christos error("%s: End of file reached while still reading hex, "
653 1.1 christos "missing HEX_ANSWER_END\n", name);
654 1.1 christos }
655 1.1.1.2 christos if(reading_hex_ednsdata) {
656 1.1.1.2 christos error("%s: End of file reached while still reading edns data, "
657 1.1.1.2 christos "missing HEX_EDNSDATA_END\n", name);
658 1.1.1.2 christos }
659 1.1 christos if(current) {
660 1.1 christos error("%s: End of file reached while reading entry. "
661 1.1 christos "missing ENTRY_END\n", name);
662 1.1 christos }
663 1.1 christos return 0;
664 1.1 christos }
665 1.1 christos
666 1.1 christos /* reads the canned reply file and returns a list of structs */
667 1.1 christos struct entry*
668 1.1 christos read_datafile(const char* name, int skip_whitespace)
669 1.1 christos {
670 1.1 christos struct entry* list = NULL;
671 1.1 christos struct entry* last = NULL;
672 1.1 christos struct entry* current = NULL;
673 1.1 christos FILE *in;
674 1.1 christos struct sldns_file_parse_state pstate;
675 1.1 christos int entry_num = 0;
676 1.1 christos memset(&pstate, 0, sizeof(pstate));
677 1.1 christos
678 1.1 christos if((in=fopen(name, "r")) == NULL) {
679 1.1 christos error("could not open file %s: %s", name, strerror(errno));
680 1.1 christos }
681 1.1 christos
682 1.1 christos while((current = read_entry(in, name, &pstate, skip_whitespace)))
683 1.1 christos {
684 1.1 christos if(last)
685 1.1 christos last->next = current;
686 1.1 christos else list = current;
687 1.1 christos last = current;
688 1.1 christos entry_num ++;
689 1.1 christos }
690 1.1 christos verbose(1, "%s: Read %d entries\n", prog_name, entry_num);
691 1.1 christos
692 1.1 christos fclose(in);
693 1.1 christos return list;
694 1.1 christos }
695 1.1 christos
696 1.1 christos /** get qtype from packet */
697 1.1 christos static sldns_rr_type get_qtype(uint8_t* pkt, size_t pktlen)
698 1.1 christos {
699 1.1 christos uint8_t* d;
700 1.1 christos size_t dl, sl=0;
701 1.1 christos char* snull = NULL;
702 1.1 christos if(pktlen < LDNS_HEADER_SIZE)
703 1.1 christos return 0;
704 1.1 christos if(LDNS_QDCOUNT(pkt) == 0)
705 1.1 christos return 0;
706 1.1 christos /* skip over dname with dname-scan routine */
707 1.1 christos d = pkt+LDNS_HEADER_SIZE;
708 1.1 christos dl = pktlen-LDNS_HEADER_SIZE;
709 1.1 christos (void)sldns_wire2str_dname_scan(&d, &dl, &snull, &sl, pkt, pktlen);
710 1.1 christos if(dl < 2)
711 1.1 christos return 0;
712 1.1 christos return sldns_read_uint16(d);
713 1.1 christos }
714 1.1 christos
715 1.1 christos /** get qtype from packet */
716 1.1 christos static size_t get_qname_len(uint8_t* pkt, size_t pktlen)
717 1.1 christos {
718 1.1 christos uint8_t* d;
719 1.1 christos size_t dl, sl=0;
720 1.1 christos char* snull = NULL;
721 1.1 christos if(pktlen < LDNS_HEADER_SIZE)
722 1.1 christos return 0;
723 1.1 christos if(LDNS_QDCOUNT(pkt) == 0)
724 1.1 christos return 0;
725 1.1 christos /* skip over dname with dname-scan routine */
726 1.1 christos d = pkt+LDNS_HEADER_SIZE;
727 1.1 christos dl = pktlen-LDNS_HEADER_SIZE;
728 1.1 christos (void)sldns_wire2str_dname_scan(&d, &dl, &snull, &sl, pkt, pktlen);
729 1.1 christos return pktlen-dl-LDNS_HEADER_SIZE;
730 1.1 christos }
731 1.1 christos
732 1.1 christos /** returns owner from packet */
733 1.1 christos static uint8_t* get_qname(uint8_t* pkt, size_t pktlen)
734 1.1 christos {
735 1.1 christos if(pktlen < LDNS_HEADER_SIZE)
736 1.1 christos return NULL;
737 1.1 christos if(LDNS_QDCOUNT(pkt) == 0)
738 1.1 christos return NULL;
739 1.1 christos return pkt+LDNS_HEADER_SIZE;
740 1.1 christos }
741 1.1 christos
742 1.1 christos /** returns opcode from packet */
743 1.1 christos static int get_opcode(uint8_t* pkt, size_t pktlen)
744 1.1 christos {
745 1.1 christos if(pktlen < LDNS_HEADER_SIZE)
746 1.1 christos return 0;
747 1.1 christos return (int)LDNS_OPCODE_WIRE(pkt);
748 1.1 christos }
749 1.1 christos
750 1.1.1.2 christos /** returns rcode from packet */
751 1.1.1.2 christos static int get_rcode(uint8_t* pkt, size_t pktlen)
752 1.1.1.2 christos {
753 1.1.1.2 christos if(pktlen < LDNS_HEADER_SIZE)
754 1.1.1.2 christos return 0;
755 1.1.1.2 christos return (int)LDNS_RCODE_WIRE(pkt);
756 1.1.1.2 christos }
757 1.1.1.2 christos
758 1.1 christos /** get authority section SOA serial value */
759 1.1 christos static uint32_t get_serial(uint8_t* p, size_t plen)
760 1.1 christos {
761 1.1 christos uint8_t* walk = p;
762 1.1 christos size_t walk_len = plen, sl=0;
763 1.1 christos char* snull = NULL;
764 1.1 christos uint16_t i;
765 1.1 christos
766 1.1 christos if(walk_len < LDNS_HEADER_SIZE)
767 1.1 christos return 0;
768 1.1 christos walk += LDNS_HEADER_SIZE;
769 1.1 christos walk_len -= LDNS_HEADER_SIZE;
770 1.1 christos
771 1.1 christos /* skip other records with wire2str_scan */
772 1.1 christos for(i=0; i < LDNS_QDCOUNT(p); i++)
773 1.1 christos (void)sldns_wire2str_rrquestion_scan(&walk, &walk_len,
774 1.1 christos &snull, &sl, p, plen);
775 1.1 christos for(i=0; i < LDNS_ANCOUNT(p); i++)
776 1.1 christos (void)sldns_wire2str_rr_scan(&walk, &walk_len, &snull, &sl,
777 1.1 christos p, plen);
778 1.1 christos
779 1.1 christos /* walk through authority section */
780 1.1 christos for(i=0; i < LDNS_NSCOUNT(p); i++) {
781 1.1 christos /* if this is SOA then get serial, skip compressed dname */
782 1.1 christos uint8_t* dstart = walk;
783 1.1 christos size_t dlen = walk_len;
784 1.1 christos (void)sldns_wire2str_dname_scan(&dstart, &dlen, &snull, &sl,
785 1.1 christos p, plen);
786 1.1 christos if(dlen >= 2 && sldns_read_uint16(dstart) == LDNS_RR_TYPE_SOA) {
787 1.1 christos /* skip type, class, TTL, rdatalen */
788 1.1 christos if(dlen < 10)
789 1.1 christos return 0;
790 1.1 christos if(dlen < 10 + (size_t)sldns_read_uint16(dstart+8))
791 1.1 christos return 0;
792 1.1 christos dstart += 10;
793 1.1 christos dlen -= 10;
794 1.1 christos /* check third rdf */
795 1.1 christos (void)sldns_wire2str_dname_scan(&dstart, &dlen, &snull,
796 1.1 christos &sl, p, plen);
797 1.1 christos (void)sldns_wire2str_dname_scan(&dstart, &dlen, &snull,
798 1.1 christos &sl, p, plen);
799 1.1 christos if(dlen < 4)
800 1.1 christos return 0;
801 1.1 christos verbose(3, "found serial %u in msg. ",
802 1.1 christos (int)sldns_read_uint32(dstart));
803 1.1 christos return sldns_read_uint32(dstart);
804 1.1 christos }
805 1.1 christos /* move to next RR */
806 1.1 christos (void)sldns_wire2str_rr_scan(&walk, &walk_len, &snull, &sl,
807 1.1 christos p, plen);
808 1.1 christos }
809 1.1 christos return 0;
810 1.1 christos }
811 1.1 christos
812 1.1 christos /** get ptr to EDNS OPT record (and remaining length); behind the type u16 */
813 1.1 christos static int
814 1.1 christos pkt_find_edns_opt(uint8_t** p, size_t* plen)
815 1.1 christos {
816 1.1 christos /* walk over the packet with scan routines */
817 1.1 christos uint8_t* w = *p;
818 1.1 christos size_t wlen = *plen, sl=0;
819 1.1 christos char* snull = NULL;
820 1.1 christos uint16_t i;
821 1.1 christos
822 1.1 christos if(wlen < LDNS_HEADER_SIZE)
823 1.1 christos return 0;
824 1.1 christos w += LDNS_HEADER_SIZE;
825 1.1 christos wlen -= LDNS_HEADER_SIZE;
826 1.1 christos
827 1.1 christos /* skip other records with wire2str_scan */
828 1.1.1.2 christos for(i=0; i < LDNS_QDCOUNT(*p); i++)
829 1.1 christos (void)sldns_wire2str_rrquestion_scan(&w, &wlen, &snull, &sl,
830 1.1 christos *p, *plen);
831 1.1.1.2 christos for(i=0; i < LDNS_ANCOUNT(*p); i++)
832 1.1 christos (void)sldns_wire2str_rr_scan(&w, &wlen, &snull, &sl, *p, *plen);
833 1.1.1.2 christos for(i=0; i < LDNS_NSCOUNT(*p); i++)
834 1.1 christos (void)sldns_wire2str_rr_scan(&w, &wlen, &snull, &sl, *p, *plen);
835 1.1 christos
836 1.1 christos /* walk through additional section */
837 1.1.1.2 christos for(i=0; i < LDNS_ARCOUNT(*p); i++) {
838 1.1 christos /* if this is OPT then done */
839 1.1 christos uint8_t* dstart = w;
840 1.1 christos size_t dlen = wlen;
841 1.1 christos (void)sldns_wire2str_dname_scan(&dstart, &dlen, &snull, &sl,
842 1.1 christos *p, *plen);
843 1.1 christos if(dlen >= 2 && sldns_read_uint16(dstart) == LDNS_RR_TYPE_OPT) {
844 1.1 christos *p = dstart+2;
845 1.1 christos *plen = dlen-2;
846 1.1 christos return 1;
847 1.1 christos }
848 1.1 christos /* move to next RR */
849 1.1 christos (void)sldns_wire2str_rr_scan(&w, &wlen, &snull, &sl, *p, *plen);
850 1.1 christos }
851 1.1 christos return 0;
852 1.1 christos }
853 1.1 christos
854 1.1 christos /** return true if the packet has EDNS OPT record */
855 1.1 christos static int
856 1.1 christos get_has_edns(uint8_t* pkt, size_t len)
857 1.1 christos {
858 1.1 christos /* use arguments as temporary variables */
859 1.1 christos return pkt_find_edns_opt(&pkt, &len);
860 1.1 christos }
861 1.1 christos
862 1.1 christos /** return true if the DO flag is set */
863 1.1 christos static int
864 1.1 christos get_do_flag(uint8_t* pkt, size_t len)
865 1.1 christos {
866 1.1 christos uint16_t edns_bits;
867 1.1 christos uint8_t* walk = pkt;
868 1.1 christos size_t walk_len = len;
869 1.1.1.2 christos if(!pkt_find_edns_opt(&walk, &walk_len)) {
870 1.1.1.2 christos return 0;
871 1.1 christos }
872 1.1 christos if(walk_len < 6)
873 1.1 christos return 0; /* malformed */
874 1.1 christos edns_bits = sldns_read_uint16(walk+4);
875 1.1 christos return (int)(edns_bits&LDNS_EDNS_MASK_DO_BIT);
876 1.1 christos }
877 1.1 christos
878 1.1 christos /** zero TTLs in packet */
879 1.1 christos static void
880 1.1 christos zerottls(uint8_t* pkt, size_t pktlen)
881 1.1 christos {
882 1.1 christos uint8_t* walk = pkt;
883 1.1 christos size_t walk_len = pktlen, sl=0;
884 1.1 christos char* snull = NULL;
885 1.1 christos uint16_t i;
886 1.1 christos uint16_t num = LDNS_ANCOUNT(pkt)+LDNS_NSCOUNT(pkt)+LDNS_ARCOUNT(pkt);
887 1.1 christos if(walk_len < LDNS_HEADER_SIZE)
888 1.1 christos return;
889 1.1 christos walk += LDNS_HEADER_SIZE;
890 1.1 christos walk_len -= LDNS_HEADER_SIZE;
891 1.1 christos for(i=0; i < LDNS_QDCOUNT(pkt); i++)
892 1.1 christos (void)sldns_wire2str_rrquestion_scan(&walk, &walk_len,
893 1.1 christos &snull, &sl, pkt, pktlen);
894 1.1 christos for(i=0; i < num; i++) {
895 1.1 christos /* wipe TTL */
896 1.1 christos uint8_t* dstart = walk;
897 1.1 christos size_t dlen = walk_len;
898 1.1 christos (void)sldns_wire2str_dname_scan(&dstart, &dlen, &snull, &sl,
899 1.1 christos pkt, pktlen);
900 1.1 christos if(dlen < 8)
901 1.1 christos return;
902 1.1 christos sldns_write_uint32(dstart+4, 0);
903 1.1 christos /* go to next RR */
904 1.1 christos (void)sldns_wire2str_rr_scan(&walk, &walk_len, &snull, &sl,
905 1.1 christos pkt, pktlen);
906 1.1 christos }
907 1.1 christos }
908 1.1 christos
909 1.1 christos /** get one line (\n) from a string, move next to after the \n, zero \n */
910 1.1 christos static int
911 1.1 christos get_line(char** s, char** n)
912 1.1 christos {
913 1.1 christos /* at end of string? end */
914 1.1 christos if(*n == NULL || **n == 0)
915 1.1 christos return 0;
916 1.1 christos /* result starts at next string */
917 1.1 christos *s = *n;
918 1.1 christos /* find \n after that */
919 1.1 christos *n = strchr(*s, '\n');
920 1.1 christos if(*n && **n != 0) {
921 1.1 christos /* terminate line */
922 1.1 christos (*n)[0] = 0;
923 1.1 christos (*n)++;
924 1.1 christos }
925 1.1 christos return 1;
926 1.1 christos }
927 1.1 christos
928 1.1 christos /** match two RR sections without ordering */
929 1.1 christos static int
930 1.1 christos match_noloc_section(char** q, char** nq, char** p, char** np, uint16_t num)
931 1.1 christos {
932 1.1 christos /* for max number of RRs in packet */
933 1.1 christos const uint16_t numarray = 3000;
934 1.1 christos char* qlines[numarray], *plines[numarray];
935 1.1 christos uint16_t i, j, numq=0, nump=0;
936 1.1 christos if(num > numarray) fatal_exit("too many RRs");
937 1.1 christos /* gather lines */
938 1.1 christos for(i=0; i<num; i++) {
939 1.1 christos get_line(q, nq);
940 1.1 christos get_line(p, np);
941 1.1 christos qlines[numq++] = *q;
942 1.1 christos plines[nump++] = *p;
943 1.1 christos }
944 1.1 christos /* see if they are all present in the other */
945 1.1 christos for(i=0; i<num; i++) {
946 1.1 christos int found = 0;
947 1.1 christos for(j=0; j<num; j++) {
948 1.1 christos if(strcmp(qlines[i], plines[j]) == 0) {
949 1.1 christos found = 1;
950 1.1 christos break;
951 1.1 christos }
952 1.1 christos }
953 1.1 christos if(!found) {
954 1.1 christos verbose(3, "comparenoloc: failed for %s", qlines[i]);
955 1.1 christos return 0;
956 1.1 christos }
957 1.1 christos }
958 1.1 christos return 1;
959 1.1 christos }
960 1.1 christos
961 1.1 christos /** match two strings for unordered equality of RRs and everything else */
962 1.1 christos static int
963 1.1 christos match_noloc(char* q, char* p, uint8_t* q_pkt, size_t q_pkt_len,
964 1.1 christos uint8_t* p_pkt, size_t p_pkt_len)
965 1.1 christos {
966 1.1 christos char* nq = q, *np = p;
967 1.1 christos /* if no header, compare bytes */
968 1.1 christos if(p_pkt_len < LDNS_HEADER_SIZE || q_pkt_len < LDNS_HEADER_SIZE) {
969 1.1 christos if(p_pkt_len != q_pkt_len) return 0;
970 1.1 christos return memcmp(p, q, p_pkt_len);
971 1.1 christos }
972 1.1 christos /* compare RR counts */
973 1.1 christos if(LDNS_QDCOUNT(p_pkt) != LDNS_QDCOUNT(q_pkt))
974 1.1 christos return 0;
975 1.1 christos if(LDNS_ANCOUNT(p_pkt) != LDNS_ANCOUNT(q_pkt))
976 1.1 christos return 0;
977 1.1 christos if(LDNS_NSCOUNT(p_pkt) != LDNS_NSCOUNT(q_pkt))
978 1.1 christos return 0;
979 1.1 christos if(LDNS_ARCOUNT(p_pkt) != LDNS_ARCOUNT(q_pkt))
980 1.1 christos return 0;
981 1.1 christos /* get a line from both; compare; at sections do section */
982 1.1 christos get_line(&q, &nq);
983 1.1 christos get_line(&p, &np);
984 1.1 christos if(strcmp(q, p) != 0) {
985 1.1 christos /* header line opcode, rcode, id */
986 1.1 christos return 0;
987 1.1 christos }
988 1.1 christos get_line(&q, &nq);
989 1.1 christos get_line(&p, &np);
990 1.1 christos if(strcmp(q, p) != 0) {
991 1.1 christos /* header flags, rr counts */
992 1.1 christos return 0;
993 1.1 christos }
994 1.1 christos /* ;; QUESTION SECTION */
995 1.1 christos get_line(&q, &nq);
996 1.1 christos get_line(&p, &np);
997 1.1 christos if(strcmp(q, p) != 0) return 0;
998 1.1 christos if(!match_noloc_section(&q, &nq, &p, &np, LDNS_QDCOUNT(p_pkt)))
999 1.1 christos return 0;
1000 1.1 christos
1001 1.1 christos /* empty line and ;; ANSWER SECTION */
1002 1.1 christos get_line(&q, &nq);
1003 1.1 christos get_line(&p, &np);
1004 1.1 christos if(strcmp(q, p) != 0) return 0;
1005 1.1 christos get_line(&q, &nq);
1006 1.1 christos get_line(&p, &np);
1007 1.1 christos if(strcmp(q, p) != 0) return 0;
1008 1.1 christos if(!match_noloc_section(&q, &nq, &p, &np, LDNS_ANCOUNT(p_pkt)))
1009 1.1 christos return 0;
1010 1.1 christos
1011 1.1 christos /* empty line and ;; AUTHORITY SECTION */
1012 1.1 christos get_line(&q, &nq);
1013 1.1 christos get_line(&p, &np);
1014 1.1 christos if(strcmp(q, p) != 0) return 0;
1015 1.1 christos get_line(&q, &nq);
1016 1.1 christos get_line(&p, &np);
1017 1.1 christos if(strcmp(q, p) != 0) return 0;
1018 1.1 christos if(!match_noloc_section(&q, &nq, &p, &np, LDNS_NSCOUNT(p_pkt)))
1019 1.1 christos return 0;
1020 1.1 christos
1021 1.1 christos /* empty line and ;; ADDITIONAL SECTION */
1022 1.1 christos get_line(&q, &nq);
1023 1.1 christos get_line(&p, &np);
1024 1.1 christos if(strcmp(q, p) != 0) return 0;
1025 1.1 christos get_line(&q, &nq);
1026 1.1 christos get_line(&p, &np);
1027 1.1 christos if(strcmp(q, p) != 0) return 0;
1028 1.1 christos if(!match_noloc_section(&q, &nq, &p, &np, LDNS_ARCOUNT(p_pkt)))
1029 1.1 christos return 0;
1030 1.1 christos
1031 1.1 christos return 1;
1032 1.1 christos }
1033 1.1 christos
1034 1.1 christos /** lowercase domain name - does not follow compression pointers */
1035 1.1 christos static void lowercase_dname(uint8_t** p, size_t* remain)
1036 1.1 christos {
1037 1.1 christos unsigned i, llen;
1038 1.1 christos if(*remain == 0) return;
1039 1.1 christos while(**p != 0) {
1040 1.1 christos /* compressed? */
1041 1.1 christos if((**p & 0xc0) == 0xc0) {
1042 1.1 christos *p += 2;
1043 1.1 christos *remain -= 2;
1044 1.1 christos return;
1045 1.1 christos }
1046 1.1 christos llen = (unsigned int)**p;
1047 1.1 christos *p += 1;
1048 1.1 christos *remain -= 1;
1049 1.1 christos if(*remain < llen)
1050 1.1 christos llen = (unsigned int)*remain;
1051 1.1 christos for(i=0; i<llen; i++) {
1052 1.1 christos (*p)[i] = (uint8_t)tolower((int)(*p)[i]);
1053 1.1 christos }
1054 1.1 christos *p += llen;
1055 1.1 christos *remain -= llen;
1056 1.1 christos if(*remain == 0) return;
1057 1.1 christos }
1058 1.1 christos /* skip root label */
1059 1.1 christos *p += 1;
1060 1.1 christos *remain -= 1;
1061 1.1 christos }
1062 1.1 christos
1063 1.1 christos /** lowercase rdata of type */
1064 1.1 christos static void lowercase_rdata(uint8_t** p, size_t* remain,
1065 1.1 christos uint16_t rdatalen, uint16_t t)
1066 1.1 christos {
1067 1.1 christos const sldns_rr_descriptor *desc = sldns_rr_descript(t);
1068 1.1 christos uint8_t dname_count = 0;
1069 1.1 christos size_t i = 0;
1070 1.1 christos size_t rdataremain = rdatalen;
1071 1.1 christos if(!desc) {
1072 1.1 christos /* unknown type */
1073 1.1 christos *p += rdatalen;
1074 1.1 christos *remain -= rdatalen;
1075 1.1 christos return;
1076 1.1 christos }
1077 1.1 christos while(dname_count < desc->_dname_count) {
1078 1.1 christos sldns_rdf_type f = sldns_rr_descriptor_field_type(desc, i++);
1079 1.1 christos if(f == LDNS_RDF_TYPE_DNAME) {
1080 1.1 christos lowercase_dname(p, &rdataremain);
1081 1.1 christos dname_count++;
1082 1.1 christos } else if(f == LDNS_RDF_TYPE_STR) {
1083 1.1 christos uint8_t len;
1084 1.1 christos if(rdataremain == 0) return;
1085 1.1 christos len = **p;
1086 1.1 christos *p += len+1;
1087 1.1 christos rdataremain -= len+1;
1088 1.1 christos } else {
1089 1.1 christos int len = 0;
1090 1.1 christos switch(f) {
1091 1.1 christos case LDNS_RDF_TYPE_CLASS:
1092 1.1 christos case LDNS_RDF_TYPE_ALG:
1093 1.1 christos case LDNS_RDF_TYPE_INT8:
1094 1.1 christos len = 1;
1095 1.1 christos break;
1096 1.1 christos case LDNS_RDF_TYPE_INT16:
1097 1.1 christos case LDNS_RDF_TYPE_TYPE:
1098 1.1 christos case LDNS_RDF_TYPE_CERT_ALG:
1099 1.1 christos len = 2;
1100 1.1 christos break;
1101 1.1 christos case LDNS_RDF_TYPE_INT32:
1102 1.1 christos case LDNS_RDF_TYPE_TIME:
1103 1.1 christos case LDNS_RDF_TYPE_A:
1104 1.1 christos case LDNS_RDF_TYPE_PERIOD:
1105 1.1 christos len = 4;
1106 1.1 christos break;
1107 1.1 christos case LDNS_RDF_TYPE_TSIGTIME:
1108 1.1 christos len = 6;
1109 1.1 christos break;
1110 1.1 christos case LDNS_RDF_TYPE_AAAA:
1111 1.1 christos len = 16;
1112 1.1 christos break;
1113 1.1 christos default: error("bad rdf type in lowercase %d", (int)f);
1114 1.1 christos }
1115 1.1 christos *p += len;
1116 1.1 christos rdataremain -= len;
1117 1.1 christos }
1118 1.1 christos }
1119 1.1 christos /* skip remainder of rdata */
1120 1.1 christos *p += rdataremain;
1121 1.1 christos *remain -= rdatalen;
1122 1.1 christos }
1123 1.1 christos
1124 1.1 christos /** lowercase all names in the message */
1125 1.1 christos static void lowercase_pkt(uint8_t* pkt, size_t pktlen)
1126 1.1 christos {
1127 1.1 christos uint16_t i;
1128 1.1 christos uint8_t* p = pkt;
1129 1.1 christos size_t remain = pktlen;
1130 1.1 christos uint16_t t, rdatalen;
1131 1.1 christos if(pktlen < LDNS_HEADER_SIZE)
1132 1.1 christos return;
1133 1.1 christos p += LDNS_HEADER_SIZE;
1134 1.1 christos remain -= LDNS_HEADER_SIZE;
1135 1.1 christos for(i=0; i<LDNS_QDCOUNT(pkt); i++) {
1136 1.1 christos lowercase_dname(&p, &remain);
1137 1.1 christos if(remain < 4) return;
1138 1.1 christos p += 4;
1139 1.1 christos remain -= 4;
1140 1.1 christos }
1141 1.1 christos for(i=0; i<LDNS_ANCOUNT(pkt)+LDNS_NSCOUNT(pkt)+LDNS_ARCOUNT(pkt); i++) {
1142 1.1 christos lowercase_dname(&p, &remain);
1143 1.1 christos if(remain < 10) return;
1144 1.1 christos t = sldns_read_uint16(p);
1145 1.1 christos rdatalen = sldns_read_uint16(p+8);
1146 1.1 christos p += 10;
1147 1.1 christos remain -= 10;
1148 1.1 christos if(remain < rdatalen) return;
1149 1.1 christos lowercase_rdata(&p, &remain, rdatalen, t);
1150 1.1 christos }
1151 1.1 christos }
1152 1.1 christos
1153 1.1.1.2 christos /** match question section of packet */
1154 1.1.1.2 christos static int
1155 1.1.1.2 christos match_question(uint8_t* q, size_t qlen, uint8_t* p, size_t plen, int mttl)
1156 1.1.1.2 christos {
1157 1.1.1.2 christos char* qstr, *pstr, *s, *qcmpstr, *pcmpstr;
1158 1.1.1.2 christos uint8_t* qb = q, *pb = p;
1159 1.1.1.2 christos int r;
1160 1.1.1.2 christos /* zero TTLs */
1161 1.1.1.2 christos qb = memdup(q, qlen);
1162 1.1.1.2 christos pb = memdup(p, plen);
1163 1.1.1.2 christos if(!qb || !pb) error("out of memory");
1164 1.1.1.2 christos if(!mttl) {
1165 1.1.1.2 christos zerottls(qb, qlen);
1166 1.1.1.2 christos zerottls(pb, plen);
1167 1.1.1.2 christos }
1168 1.1.1.2 christos lowercase_pkt(qb, qlen);
1169 1.1.1.2 christos lowercase_pkt(pb, plen);
1170 1.1.1.2 christos qstr = sldns_wire2str_pkt(qb, qlen);
1171 1.1.1.2 christos pstr = sldns_wire2str_pkt(pb, plen);
1172 1.1.1.2 christos if(!qstr || !pstr) error("cannot pkt2string");
1173 1.1.1.2 christos
1174 1.1.1.2 christos /* remove before ;; QUESTION */
1175 1.1.1.2 christos s = strstr(qstr, ";; QUESTION SECTION");
1176 1.1.1.2 christos qcmpstr = s;
1177 1.1.1.2 christos s = strstr(pstr, ";; QUESTION SECTION");
1178 1.1.1.2 christos pcmpstr = s;
1179 1.1.1.2 christos if(!qcmpstr && !pcmpstr) {
1180 1.1.1.2 christos free(qstr);
1181 1.1.1.2 christos free(pstr);
1182 1.1.1.2 christos free(qb);
1183 1.1.1.2 christos free(pb);
1184 1.1.1.2 christos return 1;
1185 1.1.1.2 christos }
1186 1.1.1.2 christos if(!qcmpstr || !pcmpstr) {
1187 1.1.1.2 christos free(qstr);
1188 1.1.1.2 christos free(pstr);
1189 1.1.1.2 christos free(qb);
1190 1.1.1.2 christos free(pb);
1191 1.1.1.2 christos return 0;
1192 1.1.1.2 christos }
1193 1.1.1.2 christos
1194 1.1.1.2 christos /* remove after answer section, (;; AUTH, ;; ADD, ;; MSG size ..) */
1195 1.1.1.2 christos s = strstr(qcmpstr, ";; ANSWER SECTION");
1196 1.1.1.2 christos if(!s) s = strstr(qcmpstr, ";; AUTHORITY SECTION");
1197 1.1.1.2 christos if(!s) s = strstr(qcmpstr, ";; ADDITIONAL SECTION");
1198 1.1.1.2 christos if(!s) s = strstr(qcmpstr, ";; MSG SIZE");
1199 1.1.1.2 christos if(s) *s = 0;
1200 1.1.1.2 christos s = strstr(pcmpstr, ";; ANSWER SECTION");
1201 1.1.1.2 christos if(!s) s = strstr(pcmpstr, ";; AUTHORITY SECTION");
1202 1.1.1.2 christos if(!s) s = strstr(pcmpstr, ";; ADDITIONAL SECTION");
1203 1.1.1.2 christos if(!s) s = strstr(pcmpstr, ";; MSG SIZE");
1204 1.1.1.2 christos if(s) *s = 0;
1205 1.1.1.2 christos
1206 1.1.1.2 christos r = (strcmp(qcmpstr, pcmpstr) == 0);
1207 1.1.1.2 christos
1208 1.1.1.2 christos if(!r) {
1209 1.1.1.2 christos verbose(3, "mismatch question section '%s' and '%s'",
1210 1.1.1.2 christos qcmpstr, pcmpstr);
1211 1.1.1.2 christos }
1212 1.1.1.2 christos
1213 1.1.1.2 christos free(qstr);
1214 1.1.1.2 christos free(pstr);
1215 1.1.1.2 christos free(qb);
1216 1.1.1.2 christos free(pb);
1217 1.1.1.2 christos return r;
1218 1.1.1.2 christos }
1219 1.1.1.2 christos
1220 1.1.1.2 christos /** match answer section of packet */
1221 1.1.1.2 christos static int
1222 1.1.1.2 christos match_answer(uint8_t* q, size_t qlen, uint8_t* p, size_t plen, int mttl)
1223 1.1.1.2 christos {
1224 1.1.1.2 christos char* qstr, *pstr, *s, *qcmpstr, *pcmpstr;
1225 1.1.1.2 christos uint8_t* qb = q, *pb = p;
1226 1.1.1.2 christos int r;
1227 1.1.1.2 christos /* zero TTLs */
1228 1.1.1.2 christos qb = memdup(q, qlen);
1229 1.1.1.2 christos pb = memdup(p, plen);
1230 1.1.1.2 christos if(!qb || !pb) error("out of memory");
1231 1.1.1.2 christos if(!mttl) {
1232 1.1.1.2 christos zerottls(qb, qlen);
1233 1.1.1.2 christos zerottls(pb, plen);
1234 1.1.1.2 christos }
1235 1.1.1.2 christos lowercase_pkt(qb, qlen);
1236 1.1.1.2 christos lowercase_pkt(pb, plen);
1237 1.1.1.2 christos qstr = sldns_wire2str_pkt(qb, qlen);
1238 1.1.1.2 christos pstr = sldns_wire2str_pkt(pb, plen);
1239 1.1.1.2 christos if(!qstr || !pstr) error("cannot pkt2string");
1240 1.1.1.2 christos
1241 1.1.1.2 christos /* remove before ;; ANSWER */
1242 1.1.1.2 christos s = strstr(qstr, ";; ANSWER SECTION");
1243 1.1.1.2 christos qcmpstr = s;
1244 1.1.1.2 christos s = strstr(pstr, ";; ANSWER SECTION");
1245 1.1.1.2 christos pcmpstr = s;
1246 1.1.1.2 christos if(!qcmpstr && !pcmpstr) {
1247 1.1.1.2 christos free(qstr);
1248 1.1.1.2 christos free(pstr);
1249 1.1.1.2 christos free(qb);
1250 1.1.1.2 christos free(pb);
1251 1.1.1.2 christos return 1;
1252 1.1.1.2 christos }
1253 1.1.1.2 christos if(!qcmpstr || !pcmpstr) {
1254 1.1.1.2 christos free(qstr);
1255 1.1.1.2 christos free(pstr);
1256 1.1.1.2 christos free(qb);
1257 1.1.1.2 christos free(pb);
1258 1.1.1.2 christos return 0;
1259 1.1.1.2 christos }
1260 1.1.1.2 christos
1261 1.1.1.2 christos /* remove after answer section, (;; AUTH, ;; ADD, ;; MSG size ..) */
1262 1.1.1.2 christos s = strstr(qcmpstr, ";; AUTHORITY SECTION");
1263 1.1.1.2 christos if(!s) s = strstr(qcmpstr, ";; ADDITIONAL SECTION");
1264 1.1.1.2 christos if(!s) s = strstr(qcmpstr, ";; MSG SIZE");
1265 1.1.1.2 christos if(s) *s = 0;
1266 1.1.1.2 christos s = strstr(pcmpstr, ";; AUTHORITY SECTION");
1267 1.1.1.2 christos if(!s) s = strstr(pcmpstr, ";; ADDITIONAL SECTION");
1268 1.1.1.2 christos if(!s) s = strstr(pcmpstr, ";; MSG SIZE");
1269 1.1.1.2 christos if(s) *s = 0;
1270 1.1.1.2 christos
1271 1.1.1.2 christos r = (strcmp(qcmpstr, pcmpstr) == 0);
1272 1.1.1.2 christos
1273 1.1.1.2 christos if(!r) {
1274 1.1.1.2 christos verbose(3, "mismatch answer section '%s' and '%s'",
1275 1.1.1.2 christos qcmpstr, pcmpstr);
1276 1.1.1.2 christos }
1277 1.1.1.2 christos
1278 1.1.1.2 christos free(qstr);
1279 1.1.1.2 christos free(pstr);
1280 1.1.1.2 christos free(qb);
1281 1.1.1.2 christos free(pb);
1282 1.1.1.2 christos return r;
1283 1.1.1.2 christos }
1284 1.1.1.2 christos
1285 1.1 christos /** match all of the packet */
1286 1.1 christos int
1287 1.1 christos match_all(uint8_t* q, size_t qlen, uint8_t* p, size_t plen, int mttl,
1288 1.1 christos int noloc)
1289 1.1 christos {
1290 1.1 christos char* qstr, *pstr;
1291 1.1 christos uint8_t* qb = q, *pb = p;
1292 1.1 christos int r;
1293 1.1 christos /* zero TTLs */
1294 1.1 christos qb = memdup(q, qlen);
1295 1.1 christos pb = memdup(p, plen);
1296 1.1 christos if(!qb || !pb) error("out of memory");
1297 1.1 christos if(!mttl) {
1298 1.1 christos zerottls(qb, qlen);
1299 1.1 christos zerottls(pb, plen);
1300 1.1 christos }
1301 1.1 christos lowercase_pkt(qb, qlen);
1302 1.1 christos lowercase_pkt(pb, plen);
1303 1.1 christos qstr = sldns_wire2str_pkt(qb, qlen);
1304 1.1 christos pstr = sldns_wire2str_pkt(pb, plen);
1305 1.1 christos if(!qstr || !pstr) error("cannot pkt2string");
1306 1.1 christos r = (strcmp(qstr, pstr) == 0);
1307 1.1 christos if(!r) {
1308 1.1 christos /* remove ;; MSG SIZE (at end of string) */
1309 1.1 christos char* s = strstr(qstr, ";; MSG SIZE");
1310 1.1 christos if(s) *s=0;
1311 1.1 christos s = strstr(pstr, ";; MSG SIZE");
1312 1.1 christos if(s) *s=0;
1313 1.1 christos r = (strcmp(qstr, pstr) == 0);
1314 1.1 christos if(!r && !noloc) {
1315 1.1 christos /* we are going to fail see if it is because of EDNS */
1316 1.1 christos char* a = strstr(qstr, "; EDNS");
1317 1.1 christos char* b = strstr(pstr, "; EDNS");
1318 1.1 christos if( (a&&!b) || (b&&!a) ) {
1319 1.1 christos verbose(3, "mismatch in EDNS\n");
1320 1.1 christos }
1321 1.1 christos }
1322 1.1 christos }
1323 1.1 christos if(!r && noloc) {
1324 1.1 christos /* check for reordered sections */
1325 1.1 christos r = match_noloc(qstr, pstr, q, qlen, p, plen);
1326 1.1 christos }
1327 1.1.1.2 christos if(!r) {
1328 1.1.1.2 christos verbose(3, "mismatch pkt '%s' and '%s'", qstr, pstr);
1329 1.1.1.2 christos }
1330 1.1 christos free(qstr);
1331 1.1 christos free(pstr);
1332 1.1 christos free(qb);
1333 1.1 christos free(pb);
1334 1.1 christos return r;
1335 1.1 christos }
1336 1.1 christos
1337 1.1 christos /** see if domain names are equal */
1338 1.1 christos static int equal_dname(uint8_t* q, size_t qlen, uint8_t* p, size_t plen)
1339 1.1 christos {
1340 1.1 christos uint8_t* qn = get_qname(q, qlen);
1341 1.1 christos uint8_t* pn = get_qname(p, plen);
1342 1.1 christos char qs[512], ps[512];
1343 1.1 christos size_t qslen = sizeof(qs), pslen = sizeof(ps);
1344 1.1 christos char* qss = qs, *pss = ps;
1345 1.1 christos if(!qn || !pn)
1346 1.1 christos return 0;
1347 1.1 christos (void)sldns_wire2str_dname_scan(&qn, &qlen, &qss, &qslen, q, qlen);
1348 1.1 christos (void)sldns_wire2str_dname_scan(&pn, &plen, &pss, &pslen, p, plen);
1349 1.1 christos return (strcmp(qs, ps) == 0);
1350 1.1 christos }
1351 1.1 christos
1352 1.1 christos /** see if domain names are subdomain q of p */
1353 1.1 christos static int subdomain_dname(uint8_t* q, size_t qlen, uint8_t* p, size_t plen)
1354 1.1 christos {
1355 1.1 christos /* we use the tostring routines so as to test unbound's routines
1356 1.1 christos * with something else */
1357 1.1 christos uint8_t* qn = get_qname(q, qlen);
1358 1.1 christos uint8_t* pn = get_qname(p, plen);
1359 1.1 christos char qs[5120], ps[5120];
1360 1.1 christos size_t qslen = sizeof(qs), pslen = sizeof(ps);
1361 1.1 christos char* qss = qs, *pss = ps;
1362 1.1 christos if(!qn || !pn)
1363 1.1 christos return 0;
1364 1.1 christos /* decompresses domain names */
1365 1.1 christos (void)sldns_wire2str_dname_scan(&qn, &qlen, &qss, &qslen, q, qlen);
1366 1.1 christos (void)sldns_wire2str_dname_scan(&pn, &plen, &pss, &pslen, p, plen);
1367 1.1 christos /* same: false, (strict subdomain check)??? */
1368 1.1 christos if(strcmp(qs, ps) == 0)
1369 1.1 christos return 1;
1370 1.1 christos /* qs must end in ps, at a dot, without \ in front */
1371 1.1 christos qslen = strlen(qs);
1372 1.1 christos pslen = strlen(ps);
1373 1.1 christos if(qslen > pslen && strcmp(qs + (qslen-pslen), ps) == 0 &&
1374 1.1 christos qslen + 2 >= pslen && /* space for label and dot */
1375 1.1 christos qs[qslen-pslen-1] == '.') {
1376 1.1 christos unsigned int slashcount = 0;
1377 1.1 christos size_t i = qslen-pslen-2;
1378 1.1 christos while(i>0 && qs[i]=='\\') {
1379 1.1 christos i++;
1380 1.1 christos slashcount++;
1381 1.1 christos }
1382 1.1 christos if(slashcount%1 == 1) return 0; /* . preceded by \ */
1383 1.1 christos return 1;
1384 1.1 christos }
1385 1.1 christos return 0;
1386 1.1 christos }
1387 1.1 christos
1388 1.1.1.2 christos /** Match OPT RDATA (not the EDNS payload size or flags) */
1389 1.1.1.2 christos static int
1390 1.1.1.2 christos match_ednsdata(uint8_t* q, size_t qlen, uint8_t* p, size_t plen)
1391 1.1.1.2 christos {
1392 1.1.1.2 christos uint8_t* walk_q = q;
1393 1.1.1.2 christos size_t walk_qlen = qlen;
1394 1.1.1.2 christos uint8_t* walk_p = p;
1395 1.1.1.2 christos size_t walk_plen = plen;
1396 1.1.1.2 christos
1397 1.1.1.2 christos if(!pkt_find_edns_opt(&walk_q, &walk_qlen))
1398 1.1.1.2 christos walk_qlen = 0;
1399 1.1.1.2 christos if(!pkt_find_edns_opt(&walk_p, &walk_plen))
1400 1.1.1.2 christos walk_plen = 0;
1401 1.1.1.2 christos
1402 1.1.1.2 christos /* class + ttl + rdlen = 8 */
1403 1.1.1.2 christos if(walk_qlen <= 8 && walk_plen <= 8) {
1404 1.1.1.2 christos verbose(3, "NO edns opt, move on");
1405 1.1.1.2 christos return 1;
1406 1.1.1.2 christos }
1407 1.1.1.2 christos if(walk_qlen != walk_plen)
1408 1.1.1.2 christos return 0;
1409 1.1.1.2 christos
1410 1.1.1.2 christos return (memcmp(walk_p+8, walk_q+8, walk_qlen-8) == 0);
1411 1.1.1.2 christos }
1412 1.1.1.2 christos
1413 1.1 christos /* finds entry in list, or returns NULL */
1414 1.1 christos struct entry*
1415 1.1 christos find_match(struct entry* entries, uint8_t* query_pkt, size_t len,
1416 1.1 christos enum transport_type transport)
1417 1.1 christos {
1418 1.1 christos struct entry* p = entries;
1419 1.1 christos uint8_t* reply;
1420 1.1 christos size_t rlen;
1421 1.1 christos for(p=entries; p; p=p->next) {
1422 1.1 christos verbose(3, "comparepkt: ");
1423 1.1 christos reply = p->reply_list->reply_pkt;
1424 1.1 christos rlen = p->reply_list->reply_len;
1425 1.1 christos if(p->match_opcode && get_opcode(query_pkt, len) !=
1426 1.1 christos get_opcode(reply, rlen)) {
1427 1.1 christos verbose(3, "bad opcode\n");
1428 1.1 christos continue;
1429 1.1 christos }
1430 1.1 christos if(p->match_qtype && get_qtype(query_pkt, len) !=
1431 1.1 christos get_qtype(reply, rlen)) {
1432 1.1 christos verbose(3, "bad qtype %d %d\n", get_qtype(query_pkt, len), get_qtype(reply, rlen));
1433 1.1 christos continue;
1434 1.1 christos }
1435 1.1 christos if(p->match_qname) {
1436 1.1 christos if(!equal_dname(query_pkt, len, reply, rlen)) {
1437 1.1 christos verbose(3, "bad qname\n");
1438 1.1 christos continue;
1439 1.1 christos }
1440 1.1 christos }
1441 1.1.1.2 christos if(p->match_rcode) {
1442 1.1.1.2 christos if(get_rcode(query_pkt, len) != get_rcode(reply, rlen)) {
1443 1.1.1.2 christos char *r1 = sldns_wire2str_rcode(get_rcode(query_pkt, len));
1444 1.1.1.2 christos char *r2 = sldns_wire2str_rcode(get_rcode(reply, rlen));
1445 1.1.1.2 christos verbose(3, "bad rcode %s instead of %s\n",
1446 1.1.1.2 christos r1, r2);
1447 1.1.1.2 christos free(r1);
1448 1.1.1.2 christos free(r2);
1449 1.1.1.2 christos continue;
1450 1.1.1.2 christos }
1451 1.1.1.2 christos }
1452 1.1.1.2 christos if(p->match_question) {
1453 1.1.1.2 christos if(!match_question(query_pkt, len, reply, rlen,
1454 1.1.1.2 christos (int)p->match_ttl)) {
1455 1.1.1.2 christos verbose(3, "bad question section\n");
1456 1.1.1.2 christos continue;
1457 1.1.1.2 christos }
1458 1.1.1.2 christos }
1459 1.1.1.2 christos if(p->match_answer) {
1460 1.1.1.2 christos if(!match_answer(query_pkt, len, reply, rlen,
1461 1.1.1.2 christos (int)p->match_ttl)) {
1462 1.1.1.2 christos verbose(3, "bad answer section\n");
1463 1.1.1.2 christos continue;
1464 1.1.1.2 christos }
1465 1.1.1.2 christos }
1466 1.1 christos if(p->match_subdomain) {
1467 1.1 christos if(!subdomain_dname(query_pkt, len, reply, rlen)) {
1468 1.1 christos verbose(3, "bad subdomain\n");
1469 1.1 christos continue;
1470 1.1 christos }
1471 1.1 christos }
1472 1.1 christos if(p->match_serial && get_serial(query_pkt, len) != p->ixfr_soa_serial) {
1473 1.1 christos verbose(3, "bad serial\n");
1474 1.1 christos continue;
1475 1.1 christos }
1476 1.1 christos if(p->match_do && !get_do_flag(query_pkt, len)) {
1477 1.1 christos verbose(3, "no DO bit set\n");
1478 1.1 christos continue;
1479 1.1 christos }
1480 1.1 christos if(p->match_noedns && get_has_edns(query_pkt, len)) {
1481 1.1 christos verbose(3, "bad; EDNS OPT present\n");
1482 1.1 christos continue;
1483 1.1 christos }
1484 1.1.1.2 christos if(p->match_ednsdata_raw &&
1485 1.1.1.2 christos !match_ednsdata(query_pkt, len, reply, rlen)) {
1486 1.1.1.2 christos verbose(3, "bad EDNS data match.\n");
1487 1.1.1.2 christos continue;
1488 1.1.1.2 christos }
1489 1.1 christos if(p->match_transport != transport_any && p->match_transport != transport) {
1490 1.1 christos verbose(3, "bad transport\n");
1491 1.1 christos continue;
1492 1.1 christos }
1493 1.1 christos if(p->match_all && !match_all(query_pkt, len, reply, rlen,
1494 1.1 christos (int)p->match_ttl, 0)) {
1495 1.1 christos verbose(3, "bad allmatch\n");
1496 1.1 christos continue;
1497 1.1 christos }
1498 1.1 christos verbose(3, "match!\n");
1499 1.1 christos return p;
1500 1.1 christos }
1501 1.1 christos return NULL;
1502 1.1 christos }
1503 1.1 christos
1504 1.1 christos void
1505 1.1 christos adjust_packet(struct entry* match, uint8_t** answer_pkt, size_t *answer_len,
1506 1.1 christos uint8_t* query_pkt, size_t query_len)
1507 1.1 christos {
1508 1.1 christos uint8_t* orig = *answer_pkt;
1509 1.1 christos size_t origlen = *answer_len;
1510 1.1 christos uint8_t* res;
1511 1.1 christos size_t reslen;
1512 1.1 christos
1513 1.1 christos /* perform the copy; if possible; must be uncompressed */
1514 1.1 christos if(match->copy_query && origlen >= LDNS_HEADER_SIZE &&
1515 1.1 christos query_len >= LDNS_HEADER_SIZE && LDNS_QDCOUNT(query_pkt)!=0
1516 1.1 christos && LDNS_QDCOUNT(orig)==0) {
1517 1.1 christos /* no qname in output packet, insert it */
1518 1.1 christos size_t dlen = get_qname_len(query_pkt, query_len);
1519 1.1 christos reslen = origlen + dlen + 4;
1520 1.1 christos res = (uint8_t*)malloc(reslen);
1521 1.1 christos if(!res) {
1522 1.1 christos verbose(1, "out of memory; send without adjust\n");
1523 1.1 christos return;
1524 1.1 christos }
1525 1.1 christos /* copy the header, query, remainder */
1526 1.1 christos memcpy(res, orig, LDNS_HEADER_SIZE);
1527 1.1 christos memmove(res+LDNS_HEADER_SIZE, query_pkt+LDNS_HEADER_SIZE,
1528 1.1 christos dlen+4);
1529 1.1 christos memmove(res+LDNS_HEADER_SIZE+dlen+4, orig+LDNS_HEADER_SIZE,
1530 1.1 christos reslen-(LDNS_HEADER_SIZE+dlen+4));
1531 1.1 christos /* set QDCOUNT */
1532 1.1 christos sldns_write_uint16(res+4, 1);
1533 1.1 christos } else if(match->copy_query && origlen >= LDNS_HEADER_SIZE &&
1534 1.1 christos query_len >= LDNS_HEADER_SIZE && LDNS_QDCOUNT(query_pkt)!=0
1535 1.1 christos && get_qname_len(orig, origlen) == 0) {
1536 1.1 christos /* QDCOUNT(orig)!=0 but qlen == 0, therefore, an error */
1537 1.1 christos verbose(1, "error: malformed qname; send without adjust\n");
1538 1.1 christos res = memdup(orig, origlen);
1539 1.1 christos reslen = origlen;
1540 1.1 christos } else if(match->copy_query && origlen >= LDNS_HEADER_SIZE &&
1541 1.1 christos query_len >= LDNS_HEADER_SIZE && LDNS_QDCOUNT(query_pkt)!=0
1542 1.1 christos && LDNS_QDCOUNT(orig)!=0) {
1543 1.1 christos /* in this case olen != 0 and QDCOUNT(orig)!=0 */
1544 1.1 christos /* copy query section */
1545 1.1 christos size_t dlen = get_qname_len(query_pkt, query_len);
1546 1.1 christos size_t olen = get_qname_len(orig, origlen);
1547 1.1 christos reslen = origlen + dlen - olen;
1548 1.1 christos res = (uint8_t*)malloc(reslen);
1549 1.1 christos if(!res) {
1550 1.1 christos verbose(1, "out of memory; send without adjust\n");
1551 1.1 christos return;
1552 1.1 christos }
1553 1.1 christos /* copy the header, query, remainder */
1554 1.1 christos memcpy(res, orig, LDNS_HEADER_SIZE);
1555 1.1 christos memmove(res+LDNS_HEADER_SIZE, query_pkt+LDNS_HEADER_SIZE,
1556 1.1 christos dlen+4);
1557 1.1 christos memmove(res+LDNS_HEADER_SIZE+dlen+4,
1558 1.1 christos orig+LDNS_HEADER_SIZE+olen+4,
1559 1.1 christos reslen-(LDNS_HEADER_SIZE+dlen+4));
1560 1.1 christos } else {
1561 1.1 christos res = memdup(orig, origlen);
1562 1.1 christos reslen = origlen;
1563 1.1 christos }
1564 1.1 christos if(!res) {
1565 1.1 christos verbose(1, "out of memory; send without adjust\n");
1566 1.1 christos return;
1567 1.1 christos }
1568 1.1 christos /* copy the ID */
1569 1.1.1.3 christos if(match->copy_id && reslen >= 2 && query_len >= 2)
1570 1.1.1.3 christos res[1] = query_pkt[1];
1571 1.1.1.3 christos if(match->copy_id && reslen >= 1 && query_len >= 1)
1572 1.1.1.3 christos res[0] = query_pkt[0];
1573 1.1 christos
1574 1.1.1.2 christos if(match->copy_ednsdata_assume_clientsubnet) {
1575 1.1.1.2 christos /** Assume there is only one EDNS option, which is ECS.
1576 1.1.1.2 christos * Copy source mask from query to scope mask in reply. Assume
1577 1.1.1.2 christos * rest of ECS data in response (eg address) matches the query.
1578 1.1.1.2 christos */
1579 1.1.1.2 christos uint8_t* walk_q = orig;
1580 1.1.1.2 christos size_t walk_qlen = origlen;
1581 1.1.1.2 christos uint8_t* walk_p = res;
1582 1.1.1.2 christos size_t walk_plen = reslen;
1583 1.1.1.2 christos
1584 1.1.1.2 christos if(!pkt_find_edns_opt(&walk_q, &walk_qlen)) {
1585 1.1.1.2 christos walk_qlen = 0;
1586 1.1.1.2 christos }
1587 1.1.1.2 christos if(!pkt_find_edns_opt(&walk_p, &walk_plen)) {
1588 1.1.1.2 christos walk_plen = 0;
1589 1.1.1.2 christos }
1590 1.1.1.2 christos /* class + ttl + rdlen + optcode + optlen + ecs fam + ecs source
1591 1.1.1.2 christos * + ecs scope = index 15 */
1592 1.1.1.2 christos if(walk_qlen >= 15 && walk_plen >= 15) {
1593 1.1.1.2 christos walk_p[15] = walk_q[14];
1594 1.1.1.2 christos }
1595 1.1.1.2 christos }
1596 1.1.1.2 christos
1597 1.1 christos if(match->sleeptime > 0) {
1598 1.1 christos verbose(3, "sleeping for %d seconds\n", match->sleeptime);
1599 1.1 christos #ifdef HAVE_SLEEP
1600 1.1 christos sleep(match->sleeptime);
1601 1.1 christos #else
1602 1.1 christos Sleep(match->sleeptime * 1000);
1603 1.1 christos #endif
1604 1.1 christos }
1605 1.1 christos *answer_pkt = res;
1606 1.1 christos *answer_len = reslen;
1607 1.1 christos }
1608 1.1 christos
1609 1.1 christos /*
1610 1.1 christos * Parses data buffer to a query, finds the correct answer
1611 1.1 christos * and calls the given function for every packet to send.
1612 1.1 christos */
1613 1.1 christos void
1614 1.1 christos handle_query(uint8_t* inbuf, ssize_t inlen, struct entry* entries, int* count,
1615 1.1 christos enum transport_type transport, void (*sendfunc)(uint8_t*, size_t, void*),
1616 1.1 christos void* userdata, FILE* verbose_out)
1617 1.1 christos {
1618 1.1 christos struct reply_packet *p;
1619 1.1 christos uint8_t *outbuf = NULL;
1620 1.1 christos size_t outlen = 0;
1621 1.1 christos struct entry* entry = NULL;
1622 1.1 christos
1623 1.1 christos verbose(1, "query %d: id %d: %s %d bytes: ", ++(*count),
1624 1.1 christos (int)(inlen>=2?LDNS_ID_WIRE(inbuf):0),
1625 1.1 christos (transport==transport_tcp)?"TCP":"UDP", (int)inlen);
1626 1.1 christos if(verbose_out) {
1627 1.1 christos char* out = sldns_wire2str_pkt(inbuf, (size_t)inlen);
1628 1.1 christos printf("%s\n", out);
1629 1.1 christos free(out);
1630 1.1 christos }
1631 1.1 christos
1632 1.1 christos /* fill up answer packet */
1633 1.1 christos entry = find_match(entries, inbuf, (size_t)inlen, transport);
1634 1.1 christos if(!entry || !entry->reply_list) {
1635 1.1 christos verbose(1, "no answer packet for this query, no reply.\n");
1636 1.1 christos return;
1637 1.1 christos }
1638 1.1 christos for(p = entry->reply_list; p; p = p->next)
1639 1.1 christos {
1640 1.1 christos verbose(3, "Answer pkt:\n");
1641 1.1 christos if (p->reply_from_hex) {
1642 1.1 christos /* try to adjust the hex packet, if it can be
1643 1.1 christos * parsed, we can use adjust rules. if not,
1644 1.1 christos * send packet literally */
1645 1.1 christos /* still try to adjust ID if others fail */
1646 1.1 christos outlen = sldns_buffer_limit(p->reply_from_hex);
1647 1.1 christos outbuf = sldns_buffer_begin(p->reply_from_hex);
1648 1.1 christos } else {
1649 1.1 christos outbuf = p->reply_pkt;
1650 1.1 christos outlen = p->reply_len;
1651 1.1 christos }
1652 1.1 christos if(!outbuf) {
1653 1.1 christos verbose(1, "out of memory\n");
1654 1.1 christos return;
1655 1.1 christos }
1656 1.1 christos /* copies outbuf in memory allocation */
1657 1.1 christos adjust_packet(entry, &outbuf, &outlen, inbuf, (size_t)inlen);
1658 1.1 christos verbose(1, "Answer packet size: %u bytes.\n", (unsigned int)outlen);
1659 1.1 christos if(verbose_out) {
1660 1.1 christos char* out = sldns_wire2str_pkt(outbuf, outlen);
1661 1.1 christos printf("%s\n", out);
1662 1.1 christos free(out);
1663 1.1 christos }
1664 1.1 christos if(p->packet_sleep) {
1665 1.1 christos verbose(3, "sleeping for next packet %d secs\n",
1666 1.1 christos p->packet_sleep);
1667 1.1 christos #ifdef HAVE_SLEEP
1668 1.1 christos sleep(p->packet_sleep);
1669 1.1 christos #else
1670 1.1 christos Sleep(p->packet_sleep * 1000);
1671 1.1 christos #endif
1672 1.1 christos verbose(3, "wakeup for next packet "
1673 1.1 christos "(slept %d secs)\n", p->packet_sleep);
1674 1.1 christos }
1675 1.1 christos sendfunc(outbuf, outlen, userdata);
1676 1.1 christos free(outbuf);
1677 1.1 christos outbuf = NULL;
1678 1.1 christos outlen = 0;
1679 1.1 christos }
1680 1.1 christos }
1681 1.1 christos
1682 1.1 christos /** delete the list of reply packets */
1683 1.1 christos void delete_replylist(struct reply_packet* replist)
1684 1.1 christos {
1685 1.1 christos struct reply_packet *p=replist, *np;
1686 1.1 christos while(p) {
1687 1.1 christos np = p->next;
1688 1.1 christos free(p->reply_pkt);
1689 1.1 christos sldns_buffer_free(p->reply_from_hex);
1690 1.1.1.2 christos sldns_buffer_free(p->raw_ednsdata);
1691 1.1 christos free(p);
1692 1.1 christos p=np;
1693 1.1 christos }
1694 1.1 christos }
1695 1.1 christos
1696 1.1 christos void delete_entry(struct entry* list)
1697 1.1 christos {
1698 1.1 christos struct entry *p=list, *np;
1699 1.1 christos while(p) {
1700 1.1 christos np = p->next;
1701 1.1 christos delete_replylist(p->reply_list);
1702 1.1 christos free(p);
1703 1.1 christos p = np;
1704 1.1 christos }
1705 1.1 christos }
1706