source: mainline/uspace/srv/net/dnsrsrv/dns_msg.c@ 1d94e21

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 1d94e21 was 3e66428, checked in by Martin Decky <martin@…>, 12 years ago

new network address infrastructure (towards IPv6 support)

  • Property mode set to 100644
File size: 15.3 KB
Line 
1/*
2 * Copyright (c) 2013 Jiri Svoboda
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup dnsres
30 * @{
31 */
32/**
33 * @file
34 */
35
36#include <bitops.h>
37#include <byteorder.h>
38#include <errno.h>
39#include <io/log.h>
40#include <macros.h>
41#include <stdint.h>
42#include <stdlib.h>
43#include <str.h>
44
45#include "dns_msg.h"
46#include "dns_std.h"
47
48#define NAME "dnsres"
49
50static uint16_t dns_uint16_t_decode(uint8_t *, size_t);
51
52/** Extend dynamically allocated string with suffix.
53 *
54 * @a *dstr points to a dynamically alocated buffer containing a string.
55 * Reallocate this buffer so that concatenation of @a *dstr and @a suff can
56 * fit in and append @a suff.
57 */
58static int dns_dstr_ext(char **dstr, const char *suff)
59{
60 size_t s1, s2;
61 size_t nsize;
62 char *nstr;
63
64 if (*dstr == NULL) {
65 *dstr = str_dup(suff);
66 if (*dstr == NULL)
67 return ENOMEM;
68 return EOK;
69 }
70
71 s1 = str_size(*dstr);
72 s2 = str_size(suff);
73 nsize = s1 + s2 + 1;
74
75 nstr = realloc(*dstr, nsize);
76 if (nstr == NULL)
77 return ENOMEM;
78
79 str_cpy((*dstr) + s1, nsize - s1, suff);
80
81 *dstr = nstr;
82 return EOK;
83}
84
85/** Encode DNS name.
86 *
87 * Encode DNS name or measure the size of encoded name (with @a buf NULL,
88 * and @a buf_size 0).
89 *
90 * @param name String to encode
91 * @param buf Buffer or NULL
92 * @param buf_size Buffer size or 0 if @a buf is NULL
93 * @param act_size Place to store actual encoded size
94 */
95static int dns_name_encode(char *name, uint8_t *buf, size_t buf_size,
96 size_t *act_size)
97{
98 size_t off;
99 wchar_t c;
100 size_t lsize;
101 size_t pi, di;
102
103 pi = 0;
104 di = 1;
105 off = 0;
106
107 lsize = 0;
108 while (true) {
109 c = str_decode(name, &off, STR_NO_LIMIT);
110 if (c >= 127) {
111 /* Non-ASCII character */
112 log_msg(LOG_DEFAULT, LVL_DEBUG, "Non-ascii character");
113 return EINVAL;
114 }
115
116 if (c == '.' || c == '\0') {
117 /* Empty string, starting with period or two consecutive periods. */
118 if (lsize == 0) {
119 log_msg(LOG_DEFAULT, LVL_DEBUG, "Empty token");
120 return EINVAL;
121 }
122
123 if (lsize > DNS_LABEL_MAX_SIZE) {
124 /* Label too long */
125 log_msg(LOG_DEFAULT, LVL_DEBUG, "Label too long");
126 return EINVAL;
127 }
128
129 if (buf != NULL && pi < buf_size)
130 buf[pi] = (uint8_t)lsize;
131
132 lsize = 0;
133 pi = di;
134 ++di;
135
136 if (c == '\0')
137 break;
138 } else {
139 if (buf != NULL && di < buf_size)
140 buf[di] = c;
141 ++di;
142 ++lsize;
143 }
144 }
145
146 if (buf != NULL && pi < buf_size)
147 buf[pi] = 0;
148
149 *act_size = di;
150 return EOK;
151}
152
153/** Decode DNS name.
154 *
155 * @param pdu PDU from which we are decoding
156 * @param boff Starting offset within PDU
157 * @param rname Place to return dynamically allocated string
158 * @param eoff Place to store end offset (offset after last decoded byte)
159 */
160int dns_name_decode(dns_pdu_t *pdu, size_t boff, char **rname,
161 size_t *eoff)
162{
163 uint8_t *bp;
164 size_t bsize;
165 size_t lsize;
166 size_t i;
167 size_t ptr;
168 size_t eptr;
169 char *name;
170 char dbuf[2];
171 int rc;
172 bool first;
173
174 name = NULL;
175
176 if (boff > pdu->size)
177 return EINVAL;
178
179 bp = pdu->data + boff;
180 bsize = min(pdu->size - boff, DNS_NAME_MAX_SIZE);
181 first = true;
182 *eoff = 0;
183
184 while (true) {
185 if (bsize == 0) {
186 rc = EINVAL;
187 goto error;
188 }
189
190 lsize = *bp;
191 ++bp;
192 --bsize;
193
194 if (lsize == 0)
195 break;
196
197 if (!first) {
198 rc = dns_dstr_ext(&name, ".");
199 if (rc != EOK) {
200 rc = ENOMEM;
201 goto error;
202 }
203 }
204
205 if ((lsize & 0xc0) == 0xc0) {
206 /* Pointer */
207 if (bsize < 1) {
208 log_msg(LOG_DEFAULT, LVL_DEBUG, "Pointer- bsize < 1");
209 rc = EINVAL;
210 goto error;
211 }
212
213 ptr = dns_uint16_t_decode(bp - 1, bsize) & 0x3fff;
214 ++bp;
215 --bsize;
216
217 if (ptr >= (size_t)(bp - pdu->data)) {
218 log_msg(LOG_DEFAULT, LVL_DEBUG,
219 "Pointer- forward ref %zu, pos=%zu",
220 ptr, (size_t)(bp - pdu->data));
221 /* Forward reference */
222 rc = EINVAL;
223 goto error;
224 }
225
226 /*
227 * Make sure we will not decode any byte twice.
228 * XXX Is assumption correct?
229 */
230 eptr = bp - pdu->data;
231 /*
232 * This is where encoded name ends in terms where
233 * the message continues
234 */
235 *eoff = eptr;
236
237 bp = pdu->data + ptr;
238 bsize = eptr - ptr;
239 continue;
240 }
241
242 if (lsize > bsize) {
243 rc = EINVAL;
244 goto error;
245 }
246
247 for (i = 0; i < lsize; i++) {
248 if (*bp < 32 || *bp >= 127) {
249 rc = EINVAL;
250 goto error;
251 }
252
253 dbuf[0] = *bp;
254 dbuf[1] = '\0';
255
256 rc = dns_dstr_ext(&name, dbuf);
257 if (rc != EOK) {
258 rc = ENOMEM;
259 goto error;
260 }
261 ++bp;
262 --bsize;
263 }
264
265 first = false;
266 }
267
268 *rname = name;
269 if (*eoff == 0)
270 *eoff = bp - pdu->data;
271 return EOK;
272error:
273 free(name);
274 return rc;
275}
276
277/** Decode unaligned big-endian 16-bit integer */
278static uint16_t dns_uint16_t_decode(uint8_t *buf, size_t buf_size)
279{
280 assert(buf_size >= 2);
281
282 return ((uint16_t)buf[0] << 8) + buf[1];
283}
284
285/** Encode unaligned big-endian 16-bit integer */
286static void dns_uint16_t_encode(uint16_t w, uint8_t *buf, size_t buf_size)
287{
288 if (buf != NULL && buf_size >= 1)
289 buf[0] = w >> 8;
290
291 if (buf != NULL && buf_size >= 2)
292 buf[1] = w & 0xff;
293}
294
295/** Decode unaligned big-endian 32-bit integer */
296uint32_t dns_uint32_t_decode(uint8_t *buf, size_t buf_size)
297{
298 uint32_t w;
299 assert(buf_size >= 4);
300
301 w = ((uint32_t) buf[0] << 24) +
302 ((uint32_t) buf[1] << 16) +
303 ((uint32_t) buf[2] << 8) +
304 buf[3];
305
306 return w;
307}
308
309/** Encode DNS question.
310 *
311 * Encode DNS question or measure the size of encoded question (with @a buf NULL,
312 * and @a buf_size 0).
313 *
314 * @param question Question to encode
315 * @param buf Buffer or NULL
316 * @param buf_size Buffer size or 0 if @a buf is NULL
317 * @param act_size Place to store actual encoded size
318 */
319static int dns_question_encode(dns_question_t *question, uint8_t *buf,
320 size_t buf_size, size_t *act_size)
321{
322 size_t name_size;
323 size_t di;
324 int rc;
325
326 rc = dns_name_encode(question->qname, buf, buf_size, &name_size);
327 if (rc != EOK)
328 return rc;
329
330 *act_size = name_size + sizeof(uint16_t) + sizeof(uint16_t);
331 if (buf == NULL)
332 return EOK;
333
334 di = name_size;
335
336 dns_uint16_t_encode(question->qtype, buf + di, buf_size - di);
337 di += sizeof(uint16_t);
338
339 dns_uint16_t_encode(question->qclass, buf + di, buf_size - di);
340 di += sizeof(uint16_t);
341
342 return EOK;
343}
344
345/** Decode DNS question.
346 *
347 * @param pdu PDU from which we are decoding
348 * @param boff Starting offset within PDU
349 * @param rquestion Place to return dynamically allocated question
350 * @param eoff Place to store end offset (offset after last decoded byte)
351 */
352static int dns_question_decode(dns_pdu_t *pdu, size_t boff,
353 dns_question_t **rquestion, size_t *eoff)
354{
355 dns_question_t *question;
356 size_t name_eoff;
357 int rc;
358
359 question = calloc(1, sizeof (dns_question_t));
360 if (question == NULL)
361 return ENOMEM;
362
363 rc = dns_name_decode(pdu, boff, &question->qname, &name_eoff);
364 if (rc != EOK) {
365 log_msg(LOG_DEFAULT, LVL_DEBUG, "Error decoding name");
366 free(question);
367 return ENOMEM;
368 }
369
370 if (name_eoff + 2 * sizeof(uint16_t) > pdu->size) {
371 free(question);
372 return EINVAL;
373 }
374
375 question->qtype = dns_uint16_t_decode(pdu->data + name_eoff,
376 pdu->size - name_eoff);
377 question->qclass = dns_uint16_t_decode(pdu->data + sizeof(uint16_t)
378 + name_eoff, pdu->size - sizeof(uint16_t) - name_eoff);
379 *eoff = name_eoff + 2 * sizeof(uint16_t);
380
381 *rquestion = question;
382 return EOK;
383}
384
385/** Decode DNS resource record.
386 *
387 * @param pdu PDU from which we are decoding
388 * @param boff Starting offset within PDU
389 * @param retrr Place to return dynamically allocated resource record
390 * @param eoff Place to store end offset (offset after last decoded byte)
391 */
392static int dns_rr_decode(dns_pdu_t *pdu, size_t boff, dns_rr_t **retrr,
393 size_t *eoff)
394{
395 dns_rr_t *rr;
396 size_t name_eoff;
397 uint8_t *bp;
398 size_t bsz;
399 size_t rdlength;
400 int rc;
401
402 rr = calloc(1, sizeof (dns_rr_t));
403 if (rr == NULL)
404 return ENOMEM;
405
406 rc = dns_name_decode(pdu, boff, &rr->name, &name_eoff);
407 if (rc != EOK) {
408 log_msg(LOG_DEFAULT, LVL_DEBUG, "Error decoding name");
409 free(rr);
410 return ENOMEM;
411 }
412
413 if (name_eoff + 2 * sizeof(uint16_t) > pdu->size) {
414 free(rr->name);
415 free(rr);
416 return EINVAL;
417 }
418
419 bp = pdu->data + name_eoff;
420 bsz = pdu->size - name_eoff;
421
422 if (bsz < 3 * sizeof(uint16_t) + sizeof(uint32_t)) {
423 free(rr->name);
424 free(rr);
425 return EINVAL;
426 }
427
428 rr->rtype = dns_uint16_t_decode(bp, bsz);
429 bp += sizeof(uint16_t); bsz -= sizeof(uint16_t);
430
431 rr->rclass = dns_uint16_t_decode(bp, bsz);
432 bp += sizeof(uint16_t); bsz -= sizeof(uint16_t);
433
434 rr->ttl = dns_uint32_t_decode(bp, bsz);
435 bp += sizeof(uint32_t); bsz -= sizeof(uint32_t);
436
437 rdlength = dns_uint16_t_decode(bp, bsz);
438 bp += sizeof(uint16_t); bsz -= sizeof(uint16_t);
439
440 if (rdlength > bsz) {
441 free(rr->name);
442 free(rr);
443 return EINVAL;
444 }
445
446 rr->rdata_size = rdlength;
447 rr->rdata = calloc(1, sizeof(rdlength));
448 if (rr->rdata == NULL) {
449 free(rr->name);
450 free(rr);
451 return ENOMEM;
452 }
453
454 memcpy(rr->rdata, bp, rdlength);
455 rr->roff = bp - pdu->data;
456 bp += rdlength;
457 bsz -= rdlength;
458
459 *eoff = bp - pdu->data;
460 *retrr = rr;
461 return EOK;
462}
463
464/** Encode DNS message.
465 *
466 * @param msg Message
467 * @param rdata Place to store encoded data pointer
468 * @param rsize Place to store encoded data size
469 *
470 * @return EOK on success, EINVAL if message contains invalid data,
471 * ENOMEM if out of memory
472 */
473int dns_message_encode(dns_message_t *msg, void **rdata, size_t *rsize)
474{
475 uint8_t *data;
476 size_t size;
477 dns_header_t hdr;
478 size_t q_size;
479 size_t di;
480 int rc;
481
482 hdr.id = host2uint16_t_be(msg->id);
483
484 hdr.opbits = host2uint16_t_be(
485 (msg->qr << OPB_QR) |
486 (msg->opcode << OPB_OPCODE_l) |
487 (msg->aa ? BIT_V(uint16_t, OPB_AA) : 0) |
488 (msg->tc ? BIT_V(uint16_t, OPB_TC) : 0) |
489 (msg->rd ? BIT_V(uint16_t, OPB_RD) : 0) |
490 (msg->ra ? BIT_V(uint16_t, OPB_RA) : 0) |
491 msg->rcode
492 );
493
494 hdr.qd_count = host2uint16_t_be(list_count(&msg->question));
495 hdr.an_count = 0;
496 hdr.ns_count = 0;
497 hdr.ar_count = 0;
498
499 size = sizeof(dns_header_t);
500
501 list_foreach(msg->question, link) {
502 dns_question_t *q = list_get_instance(link, dns_question_t, msg);
503 rc = dns_question_encode(q, NULL, 0, &q_size);
504 if (rc != EOK)
505 return rc;
506
507 size += q_size;
508 }
509
510 data = calloc(1, size);
511 if (data == NULL)
512 return ENOMEM;
513
514 memcpy(data, &hdr, sizeof(dns_header_t));
515 di = sizeof(dns_header_t);
516
517 list_foreach(msg->question, link) {
518 dns_question_t *q = list_get_instance(link, dns_question_t, msg);
519 rc = dns_question_encode(q, data + di, size - di, &q_size);
520 if (rc != EOK) {
521 assert(rc == ENOMEM || rc == EINVAL);
522 free(data);
523 return rc;
524 }
525
526 di += q_size;
527 }
528
529 *rdata = data;
530 *rsize = size;
531 return EOK;
532}
533
534/** Decode DNS message.
535 *
536 * @param data Encoded PDU data
537 * @param size Encoded PDU size
538 * @param rmsg Place to store pointer to decoded message
539 *
540 * @return EOK on success, EINVAL if message contains invalid data,
541 * ENOMEM if out of memory
542 */
543int dns_message_decode(void *data, size_t size, dns_message_t **rmsg)
544{
545 dns_message_t *msg;
546 dns_header_t *hdr;
547 size_t doff;
548 size_t field_eoff;
549 dns_question_t *question;
550 dns_rr_t *rr;
551 size_t qd_count;
552 size_t an_count;
553 size_t i;
554 int rc;
555
556 msg = dns_message_new();
557 if (msg == NULL)
558 return ENOMEM;
559
560 if (size < sizeof(dns_header_t)) {
561 rc = EINVAL;
562 goto error;
563 }
564
565 /* Store a copy of raw message data for string decompression */
566
567 msg->pdu.data = malloc(size);
568 if (msg->pdu.data == NULL) {
569 rc = ENOMEM;
570 goto error;
571 }
572
573 memcpy(msg->pdu.data, data, size);
574 msg->pdu.size = size;
575 log_msg(LOG_DEFAULT, LVL_NOTE, "dns_message_decode: pdu->data = %p, "
576 "pdu->size=%zu", msg->pdu.data, msg->pdu.size);
577
578 hdr = data;
579
580 msg->id = uint16_t_be2host(hdr->id);
581 msg->qr = BIT_RANGE_EXTRACT(uint16_t, OPB_QR, OPB_QR, hdr->opbits);
582 msg->opcode = BIT_RANGE_EXTRACT(uint16_t, OPB_OPCODE_h, OPB_OPCODE_l,
583 hdr->opbits);
584 msg->aa = BIT_RANGE_EXTRACT(uint16_t, OPB_AA, OPB_AA, hdr->opbits);
585 msg->tc = BIT_RANGE_EXTRACT(uint16_t, OPB_TC, OPB_TC, hdr->opbits);
586 msg->rd = BIT_RANGE_EXTRACT(uint16_t, OPB_RD, OPB_RD, hdr->opbits);
587 msg->ra = BIT_RANGE_EXTRACT(uint16_t, OPB_RA, OPB_RA, hdr->opbits);
588 msg->rcode = BIT_RANGE_EXTRACT(uint16_t, OPB_RCODE_h, OPB_RCODE_l,
589 hdr->opbits);
590
591 doff = sizeof(dns_header_t);
592
593 qd_count = uint16_t_be2host(hdr->qd_count);
594
595 for (i = 0; i < qd_count; i++) {
596 rc = dns_question_decode(&msg->pdu, doff, &question, &field_eoff);
597 if (rc != EOK) {
598 log_msg(LOG_DEFAULT, LVL_DEBUG, "error decoding question");
599 goto error;
600 }
601
602 list_append(&question->msg, &msg->question);
603 doff = field_eoff;
604 }
605
606 an_count = uint16_t_be2host(hdr->an_count);
607
608 for (i = 0; i < an_count; i++) {
609 rc = dns_rr_decode(&msg->pdu, doff, &rr, &field_eoff);
610 if (rc != EOK) {
611 log_msg(LOG_DEFAULT, LVL_DEBUG, "Error decoding answer");
612 goto error;
613 }
614
615 list_append(&rr->msg, &msg->answer);
616 doff = field_eoff;
617 }
618
619 *rmsg = msg;
620 return EOK;
621error:
622 dns_message_destroy(msg);
623 return rc;
624}
625
626/** Destroy question. */
627static void dns_question_destroy(dns_question_t *question)
628{
629 free(question->qname);
630 free(question);
631}
632
633/** Destroy resource record. */
634static void dns_rr_destroy(dns_rr_t *rr)
635{
636 free(rr->name);
637 free(rr->rdata);
638 free(rr);
639}
640
641/** Create new empty message. */
642dns_message_t *dns_message_new(void)
643{
644 dns_message_t *msg;
645
646 msg = calloc(1, sizeof(dns_message_t));
647 if (msg == NULL)
648 return NULL;
649
650 list_initialize(&msg->question);
651 list_initialize(&msg->answer);
652 list_initialize(&msg->authority);
653 list_initialize(&msg->additional);
654
655 return msg;
656}
657
658/** Destroy message. */
659void dns_message_destroy(dns_message_t *msg)
660{
661 link_t *link;
662 dns_question_t *question;
663 dns_rr_t *rr;
664
665 while (!list_empty(&msg->question)) {
666 link = list_first(&msg->question);
667 question = list_get_instance(link, dns_question_t, msg);
668 list_remove(&question->msg);
669 dns_question_destroy(question);
670 }
671
672 while (!list_empty(&msg->answer)) {
673 link = list_first(&msg->answer);
674 rr = list_get_instance(link, dns_rr_t, msg);
675 list_remove(&rr->msg);
676 dns_rr_destroy(rr);
677 }
678
679 while (!list_empty(&msg->authority)) {
680 link = list_first(&msg->authority);
681 rr = list_get_instance(link, dns_rr_t, msg);
682 list_remove(&rr->msg);
683 dns_rr_destroy(rr);
684 }
685
686 while (!list_empty(&msg->additional)) {
687 link = list_first(&msg->additional);
688 rr = list_get_instance(link, dns_rr_t, msg);
689 list_remove(&rr->msg);
690 dns_rr_destroy(rr);
691 }
692
693 free(msg->pdu.data);
694 free(msg);
695}
696
697/** @}
698 */
Note: See TracBrowser for help on using the repository browser.