| 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 |
|
|---|
| 50 | static 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 | */
|
|---|
| 58 | static 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 | */
|
|---|
| 95 | static 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 | */
|
|---|
| 160 | int 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;
|
|---|
| 272 | error:
|
|---|
| 273 | free(name);
|
|---|
| 274 | return rc;
|
|---|
| 275 | }
|
|---|
| 276 |
|
|---|
| 277 | /** Decode unaligned big-endian 16-bit integer */
|
|---|
| 278 | static 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 */
|
|---|
| 286 | static 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 */
|
|---|
| 296 | uint32_t dns_uint32_t_decode(uint8_t *buf, size_t buf_size)
|
|---|
| 297 | {
|
|---|
| 298 | assert(buf_size >= 4);
|
|---|
| 299 |
|
|---|
| 300 | uint32_t w = ((uint32_t) buf[0] << 24) +
|
|---|
| 301 | ((uint32_t) buf[1] << 16) +
|
|---|
| 302 | ((uint32_t) buf[2] << 8) +
|
|---|
| 303 | buf[3];
|
|---|
| 304 |
|
|---|
| 305 | return w;
|
|---|
| 306 | }
|
|---|
| 307 |
|
|---|
| 308 | /** Decode unaligned big-endian 128-bit integer */
|
|---|
| 309 | void dns_addr128_t_decode(uint8_t *buf, size_t buf_size, addr128_t addr)
|
|---|
| 310 | {
|
|---|
| 311 | assert(buf_size >= 16);
|
|---|
| 312 |
|
|---|
| 313 | addr128_t_be2host(buf, addr);
|
|---|
| 314 | }
|
|---|
| 315 |
|
|---|
| 316 | /** Encode DNS question.
|
|---|
| 317 | *
|
|---|
| 318 | * Encode DNS question or measure the size of encoded question (with @a buf NULL,
|
|---|
| 319 | * and @a buf_size 0).
|
|---|
| 320 | *
|
|---|
| 321 | * @param question Question to encode
|
|---|
| 322 | * @param buf Buffer or NULL
|
|---|
| 323 | * @param buf_size Buffer size or 0 if @a buf is NULL
|
|---|
| 324 | * @param act_size Place to store actual encoded size
|
|---|
| 325 | */
|
|---|
| 326 | static int dns_question_encode(dns_question_t *question, uint8_t *buf,
|
|---|
| 327 | size_t buf_size, size_t *act_size)
|
|---|
| 328 | {
|
|---|
| 329 | size_t name_size;
|
|---|
| 330 | size_t di;
|
|---|
| 331 | int rc;
|
|---|
| 332 |
|
|---|
| 333 | rc = dns_name_encode(question->qname, buf, buf_size, &name_size);
|
|---|
| 334 | if (rc != EOK)
|
|---|
| 335 | return rc;
|
|---|
| 336 |
|
|---|
| 337 | *act_size = name_size + sizeof(uint16_t) + sizeof(uint16_t);
|
|---|
| 338 | if (buf == NULL)
|
|---|
| 339 | return EOK;
|
|---|
| 340 |
|
|---|
| 341 | di = name_size;
|
|---|
| 342 |
|
|---|
| 343 | dns_uint16_t_encode(question->qtype, buf + di, buf_size - di);
|
|---|
| 344 | di += sizeof(uint16_t);
|
|---|
| 345 |
|
|---|
| 346 | dns_uint16_t_encode(question->qclass, buf + di, buf_size - di);
|
|---|
| 347 | di += sizeof(uint16_t);
|
|---|
| 348 |
|
|---|
| 349 | return EOK;
|
|---|
| 350 | }
|
|---|
| 351 |
|
|---|
| 352 | /** Decode DNS question.
|
|---|
| 353 | *
|
|---|
| 354 | * @param pdu PDU from which we are decoding
|
|---|
| 355 | * @param boff Starting offset within PDU
|
|---|
| 356 | * @param rquestion Place to return dynamically allocated question
|
|---|
| 357 | * @param eoff Place to store end offset (offset after last decoded byte)
|
|---|
| 358 | */
|
|---|
| 359 | static int dns_question_decode(dns_pdu_t *pdu, size_t boff,
|
|---|
| 360 | dns_question_t **rquestion, size_t *eoff)
|
|---|
| 361 | {
|
|---|
| 362 | dns_question_t *question;
|
|---|
| 363 | size_t name_eoff;
|
|---|
| 364 | int rc;
|
|---|
| 365 |
|
|---|
| 366 | question = calloc(1, sizeof (dns_question_t));
|
|---|
| 367 | if (question == NULL)
|
|---|
| 368 | return ENOMEM;
|
|---|
| 369 |
|
|---|
| 370 | rc = dns_name_decode(pdu, boff, &question->qname, &name_eoff);
|
|---|
| 371 | if (rc != EOK) {
|
|---|
| 372 | log_msg(LOG_DEFAULT, LVL_DEBUG, "Error decoding name");
|
|---|
| 373 | free(question);
|
|---|
| 374 | return ENOMEM;
|
|---|
| 375 | }
|
|---|
| 376 |
|
|---|
| 377 | if (name_eoff + 2 * sizeof(uint16_t) > pdu->size) {
|
|---|
| 378 | free(question);
|
|---|
| 379 | return EINVAL;
|
|---|
| 380 | }
|
|---|
| 381 |
|
|---|
| 382 | question->qtype = dns_uint16_t_decode(pdu->data + name_eoff,
|
|---|
| 383 | pdu->size - name_eoff);
|
|---|
| 384 | question->qclass = dns_uint16_t_decode(pdu->data + sizeof(uint16_t)
|
|---|
| 385 | + name_eoff, pdu->size - sizeof(uint16_t) - name_eoff);
|
|---|
| 386 | *eoff = name_eoff + 2 * sizeof(uint16_t);
|
|---|
| 387 |
|
|---|
| 388 | *rquestion = question;
|
|---|
| 389 | return EOK;
|
|---|
| 390 | }
|
|---|
| 391 |
|
|---|
| 392 | /** Decode DNS resource record.
|
|---|
| 393 | *
|
|---|
| 394 | * @param pdu PDU from which we are decoding
|
|---|
| 395 | * @param boff Starting offset within PDU
|
|---|
| 396 | * @param retrr Place to return dynamically allocated resource record
|
|---|
| 397 | * @param eoff Place to store end offset (offset after last decoded byte)
|
|---|
| 398 | */
|
|---|
| 399 | static int dns_rr_decode(dns_pdu_t *pdu, size_t boff, dns_rr_t **retrr,
|
|---|
| 400 | size_t *eoff)
|
|---|
| 401 | {
|
|---|
| 402 | dns_rr_t *rr;
|
|---|
| 403 | size_t name_eoff;
|
|---|
| 404 | uint8_t *bp;
|
|---|
| 405 | size_t bsz;
|
|---|
| 406 | size_t rdlength;
|
|---|
| 407 | int rc;
|
|---|
| 408 |
|
|---|
| 409 | rr = calloc(1, sizeof(dns_rr_t));
|
|---|
| 410 | if (rr == NULL)
|
|---|
| 411 | return ENOMEM;
|
|---|
| 412 |
|
|---|
| 413 | rc = dns_name_decode(pdu, boff, &rr->name, &name_eoff);
|
|---|
| 414 | if (rc != EOK) {
|
|---|
| 415 | log_msg(LOG_DEFAULT, LVL_DEBUG, "Error decoding name");
|
|---|
| 416 | free(rr);
|
|---|
| 417 | return ENOMEM;
|
|---|
| 418 | }
|
|---|
| 419 |
|
|---|
| 420 | if (name_eoff + 2 * sizeof(uint16_t) > pdu->size) {
|
|---|
| 421 | free(rr->name);
|
|---|
| 422 | free(rr);
|
|---|
| 423 | return EINVAL;
|
|---|
| 424 | }
|
|---|
| 425 |
|
|---|
| 426 | bp = pdu->data + name_eoff;
|
|---|
| 427 | bsz = pdu->size - name_eoff;
|
|---|
| 428 |
|
|---|
| 429 | if (bsz < 3 * sizeof(uint16_t) + sizeof(uint32_t)) {
|
|---|
| 430 | free(rr->name);
|
|---|
| 431 | free(rr);
|
|---|
| 432 | return EINVAL;
|
|---|
| 433 | }
|
|---|
| 434 |
|
|---|
| 435 | rr->rtype = dns_uint16_t_decode(bp, bsz);
|
|---|
| 436 | bp += sizeof(uint16_t);
|
|---|
| 437 | bsz -= sizeof(uint16_t);
|
|---|
| 438 |
|
|---|
| 439 | rr->rclass = dns_uint16_t_decode(bp, bsz);
|
|---|
| 440 | bp += sizeof(uint16_t);
|
|---|
| 441 | bsz -= sizeof(uint16_t);
|
|---|
| 442 |
|
|---|
| 443 | rr->ttl = dns_uint32_t_decode(bp, bsz);
|
|---|
| 444 | bp += sizeof(uint32_t);
|
|---|
| 445 | bsz -= sizeof(uint32_t);
|
|---|
| 446 |
|
|---|
| 447 | rdlength = dns_uint16_t_decode(bp, bsz);
|
|---|
| 448 | bp += sizeof(uint16_t);
|
|---|
| 449 | bsz -= sizeof(uint16_t);
|
|---|
| 450 |
|
|---|
| 451 | if (rdlength > bsz) {
|
|---|
| 452 | free(rr->name);
|
|---|
| 453 | free(rr);
|
|---|
| 454 | return EINVAL;
|
|---|
| 455 | }
|
|---|
| 456 |
|
|---|
| 457 | rr->rdata_size = rdlength;
|
|---|
| 458 | rr->rdata = calloc(1, sizeof(rdlength));
|
|---|
| 459 | if (rr->rdata == NULL) {
|
|---|
| 460 | free(rr->name);
|
|---|
| 461 | free(rr);
|
|---|
| 462 | return ENOMEM;
|
|---|
| 463 | }
|
|---|
| 464 |
|
|---|
| 465 | memcpy(rr->rdata, bp, rdlength);
|
|---|
| 466 | rr->roff = bp - pdu->data;
|
|---|
| 467 | bp += rdlength;
|
|---|
| 468 | bsz -= rdlength;
|
|---|
| 469 |
|
|---|
| 470 | *eoff = bp - pdu->data;
|
|---|
| 471 | *retrr = rr;
|
|---|
| 472 | return EOK;
|
|---|
| 473 | }
|
|---|
| 474 |
|
|---|
| 475 | /** Encode DNS message.
|
|---|
| 476 | *
|
|---|
| 477 | * @param msg Message
|
|---|
| 478 | * @param rdata Place to store encoded data pointer
|
|---|
| 479 | * @param rsize Place to store encoded data size
|
|---|
| 480 | *
|
|---|
| 481 | * @return EOK on success, EINVAL if message contains invalid data,
|
|---|
| 482 | * ENOMEM if out of memory
|
|---|
| 483 | */
|
|---|
| 484 | int dns_message_encode(dns_message_t *msg, void **rdata, size_t *rsize)
|
|---|
| 485 | {
|
|---|
| 486 | uint8_t *data;
|
|---|
| 487 | size_t size;
|
|---|
| 488 | dns_header_t hdr;
|
|---|
| 489 | size_t q_size = 0;
|
|---|
| 490 | size_t di;
|
|---|
| 491 | int rc;
|
|---|
| 492 |
|
|---|
| 493 | hdr.id = host2uint16_t_be(msg->id);
|
|---|
| 494 |
|
|---|
| 495 | hdr.opbits = host2uint16_t_be(
|
|---|
| 496 | (msg->qr << OPB_QR) |
|
|---|
| 497 | (msg->opcode << OPB_OPCODE_l) |
|
|---|
| 498 | (msg->aa ? BIT_V(uint16_t, OPB_AA) : 0) |
|
|---|
| 499 | (msg->tc ? BIT_V(uint16_t, OPB_TC) : 0) |
|
|---|
| 500 | (msg->rd ? BIT_V(uint16_t, OPB_RD) : 0) |
|
|---|
| 501 | (msg->ra ? BIT_V(uint16_t, OPB_RA) : 0) |
|
|---|
| 502 | msg->rcode
|
|---|
| 503 | );
|
|---|
| 504 |
|
|---|
| 505 | hdr.qd_count = host2uint16_t_be(list_count(&msg->question));
|
|---|
| 506 | hdr.an_count = 0;
|
|---|
| 507 | hdr.ns_count = 0;
|
|---|
| 508 | hdr.ar_count = 0;
|
|---|
| 509 |
|
|---|
| 510 | size = sizeof(dns_header_t);
|
|---|
| 511 |
|
|---|
| 512 | list_foreach(msg->question, msg, dns_question_t, q) {
|
|---|
| 513 | rc = dns_question_encode(q, NULL, 0, &q_size);
|
|---|
| 514 | if (rc != EOK)
|
|---|
| 515 | return rc;
|
|---|
| 516 |
|
|---|
| 517 | assert(q_size > 0);
|
|---|
| 518 |
|
|---|
| 519 | size += q_size;
|
|---|
| 520 | }
|
|---|
| 521 |
|
|---|
| 522 | data = calloc(1, size);
|
|---|
| 523 | if (data == NULL)
|
|---|
| 524 | return ENOMEM;
|
|---|
| 525 |
|
|---|
| 526 | memcpy(data, &hdr, sizeof(dns_header_t));
|
|---|
| 527 | di = sizeof(dns_header_t);
|
|---|
| 528 |
|
|---|
| 529 | list_foreach(msg->question, msg, dns_question_t, q) {
|
|---|
| 530 | rc = dns_question_encode(q, data + di, size - di, &q_size);
|
|---|
| 531 | if (rc != EOK) {
|
|---|
| 532 | assert(rc == ENOMEM || rc == EINVAL);
|
|---|
| 533 | free(data);
|
|---|
| 534 | return rc;
|
|---|
| 535 | }
|
|---|
| 536 |
|
|---|
| 537 | assert(q_size > 0);
|
|---|
| 538 |
|
|---|
| 539 | di += q_size;
|
|---|
| 540 | }
|
|---|
| 541 |
|
|---|
| 542 | *rdata = data;
|
|---|
| 543 | *rsize = size;
|
|---|
| 544 | return EOK;
|
|---|
| 545 | }
|
|---|
| 546 |
|
|---|
| 547 | /** Decode DNS message.
|
|---|
| 548 | *
|
|---|
| 549 | * @param data Encoded PDU data
|
|---|
| 550 | * @param size Encoded PDU size
|
|---|
| 551 | * @param rmsg Place to store pointer to decoded message
|
|---|
| 552 | *
|
|---|
| 553 | * @return EOK on success, EINVAL if message contains invalid data,
|
|---|
| 554 | * ENOMEM if out of memory
|
|---|
| 555 | */
|
|---|
| 556 | int dns_message_decode(void *data, size_t size, dns_message_t **rmsg)
|
|---|
| 557 | {
|
|---|
| 558 | dns_message_t *msg;
|
|---|
| 559 | dns_header_t *hdr;
|
|---|
| 560 | size_t doff;
|
|---|
| 561 | size_t field_eoff;
|
|---|
| 562 | dns_question_t *question;
|
|---|
| 563 | dns_rr_t *rr;
|
|---|
| 564 | size_t qd_count;
|
|---|
| 565 | size_t an_count;
|
|---|
| 566 | size_t i;
|
|---|
| 567 | int rc;
|
|---|
| 568 |
|
|---|
| 569 | msg = dns_message_new();
|
|---|
| 570 | if (msg == NULL)
|
|---|
| 571 | return ENOMEM;
|
|---|
| 572 |
|
|---|
| 573 | if (size < sizeof(dns_header_t)) {
|
|---|
| 574 | rc = EINVAL;
|
|---|
| 575 | goto error;
|
|---|
| 576 | }
|
|---|
| 577 |
|
|---|
| 578 | /* Store a copy of raw message data for string decompression */
|
|---|
| 579 |
|
|---|
| 580 | msg->pdu.data = malloc(size);
|
|---|
| 581 | if (msg->pdu.data == NULL) {
|
|---|
| 582 | rc = ENOMEM;
|
|---|
| 583 | goto error;
|
|---|
| 584 | }
|
|---|
| 585 |
|
|---|
| 586 | memcpy(msg->pdu.data, data, size);
|
|---|
| 587 | msg->pdu.size = size;
|
|---|
| 588 | log_msg(LOG_DEFAULT, LVL_NOTE, "dns_message_decode: pdu->data = %p, "
|
|---|
| 589 | "pdu->size=%zu", msg->pdu.data, msg->pdu.size);
|
|---|
| 590 |
|
|---|
| 591 | hdr = data;
|
|---|
| 592 |
|
|---|
| 593 | msg->id = uint16_t_be2host(hdr->id);
|
|---|
| 594 | msg->qr = BIT_RANGE_EXTRACT(uint16_t, OPB_QR, OPB_QR, hdr->opbits);
|
|---|
| 595 | msg->opcode = BIT_RANGE_EXTRACT(uint16_t, OPB_OPCODE_h, OPB_OPCODE_l,
|
|---|
| 596 | hdr->opbits);
|
|---|
| 597 | msg->aa = BIT_RANGE_EXTRACT(uint16_t, OPB_AA, OPB_AA, hdr->opbits);
|
|---|
| 598 | msg->tc = BIT_RANGE_EXTRACT(uint16_t, OPB_TC, OPB_TC, hdr->opbits);
|
|---|
| 599 | msg->rd = BIT_RANGE_EXTRACT(uint16_t, OPB_RD, OPB_RD, hdr->opbits);
|
|---|
| 600 | msg->ra = BIT_RANGE_EXTRACT(uint16_t, OPB_RA, OPB_RA, hdr->opbits);
|
|---|
| 601 | msg->rcode = BIT_RANGE_EXTRACT(uint16_t, OPB_RCODE_h, OPB_RCODE_l,
|
|---|
| 602 | hdr->opbits);
|
|---|
| 603 |
|
|---|
| 604 | doff = sizeof(dns_header_t);
|
|---|
| 605 |
|
|---|
| 606 | qd_count = uint16_t_be2host(hdr->qd_count);
|
|---|
| 607 |
|
|---|
| 608 | for (i = 0; i < qd_count; i++) {
|
|---|
| 609 | rc = dns_question_decode(&msg->pdu, doff, &question, &field_eoff);
|
|---|
| 610 | if (rc != EOK) {
|
|---|
| 611 | log_msg(LOG_DEFAULT, LVL_DEBUG, "error decoding question");
|
|---|
| 612 | goto error;
|
|---|
| 613 | }
|
|---|
| 614 |
|
|---|
| 615 | list_append(&question->msg, &msg->question);
|
|---|
| 616 | doff = field_eoff;
|
|---|
| 617 | }
|
|---|
| 618 |
|
|---|
| 619 | an_count = uint16_t_be2host(hdr->an_count);
|
|---|
| 620 |
|
|---|
| 621 | for (i = 0; i < an_count; i++) {
|
|---|
| 622 | rc = dns_rr_decode(&msg->pdu, doff, &rr, &field_eoff);
|
|---|
| 623 | if (rc != EOK) {
|
|---|
| 624 | log_msg(LOG_DEFAULT, LVL_DEBUG, "Error decoding answer");
|
|---|
| 625 | goto error;
|
|---|
| 626 | }
|
|---|
| 627 |
|
|---|
| 628 | list_append(&rr->msg, &msg->answer);
|
|---|
| 629 | doff = field_eoff;
|
|---|
| 630 | }
|
|---|
| 631 |
|
|---|
| 632 | *rmsg = msg;
|
|---|
| 633 | return EOK;
|
|---|
| 634 | error:
|
|---|
| 635 | dns_message_destroy(msg);
|
|---|
| 636 | return rc;
|
|---|
| 637 | }
|
|---|
| 638 |
|
|---|
| 639 | /** Destroy question. */
|
|---|
| 640 | static void dns_question_destroy(dns_question_t *question)
|
|---|
| 641 | {
|
|---|
| 642 | free(question->qname);
|
|---|
| 643 | free(question);
|
|---|
| 644 | }
|
|---|
| 645 |
|
|---|
| 646 | /** Destroy resource record. */
|
|---|
| 647 | static void dns_rr_destroy(dns_rr_t *rr)
|
|---|
| 648 | {
|
|---|
| 649 | free(rr->name);
|
|---|
| 650 | free(rr->rdata);
|
|---|
| 651 | free(rr);
|
|---|
| 652 | }
|
|---|
| 653 |
|
|---|
| 654 | /** Create new empty message. */
|
|---|
| 655 | dns_message_t *dns_message_new(void)
|
|---|
| 656 | {
|
|---|
| 657 | dns_message_t *msg;
|
|---|
| 658 |
|
|---|
| 659 | msg = calloc(1, sizeof(dns_message_t));
|
|---|
| 660 | if (msg == NULL)
|
|---|
| 661 | return NULL;
|
|---|
| 662 |
|
|---|
| 663 | list_initialize(&msg->question);
|
|---|
| 664 | list_initialize(&msg->answer);
|
|---|
| 665 | list_initialize(&msg->authority);
|
|---|
| 666 | list_initialize(&msg->additional);
|
|---|
| 667 |
|
|---|
| 668 | return msg;
|
|---|
| 669 | }
|
|---|
| 670 |
|
|---|
| 671 | /** Destroy message. */
|
|---|
| 672 | void dns_message_destroy(dns_message_t *msg)
|
|---|
| 673 | {
|
|---|
| 674 | link_t *link;
|
|---|
| 675 | dns_question_t *question;
|
|---|
| 676 | dns_rr_t *rr;
|
|---|
| 677 |
|
|---|
| 678 | while (!list_empty(&msg->question)) {
|
|---|
| 679 | link = list_first(&msg->question);
|
|---|
| 680 | question = list_get_instance(link, dns_question_t, msg);
|
|---|
| 681 | list_remove(&question->msg);
|
|---|
| 682 | dns_question_destroy(question);
|
|---|
| 683 | }
|
|---|
| 684 |
|
|---|
| 685 | while (!list_empty(&msg->answer)) {
|
|---|
| 686 | link = list_first(&msg->answer);
|
|---|
| 687 | rr = list_get_instance(link, dns_rr_t, msg);
|
|---|
| 688 | list_remove(&rr->msg);
|
|---|
| 689 | dns_rr_destroy(rr);
|
|---|
| 690 | }
|
|---|
| 691 |
|
|---|
| 692 | while (!list_empty(&msg->authority)) {
|
|---|
| 693 | link = list_first(&msg->authority);
|
|---|
| 694 | rr = list_get_instance(link, dns_rr_t, msg);
|
|---|
| 695 | list_remove(&rr->msg);
|
|---|
| 696 | dns_rr_destroy(rr);
|
|---|
| 697 | }
|
|---|
| 698 |
|
|---|
| 699 | while (!list_empty(&msg->additional)) {
|
|---|
| 700 | link = list_first(&msg->additional);
|
|---|
| 701 | rr = list_get_instance(link, dns_rr_t, msg);
|
|---|
| 702 | list_remove(&rr->msg);
|
|---|
| 703 | dns_rr_destroy(rr);
|
|---|
| 704 | }
|
|---|
| 705 |
|
|---|
| 706 | free(msg->pdu.data);
|
|---|
| 707 | free(msg);
|
|---|
| 708 | }
|
|---|
| 709 |
|
|---|
| 710 | /** @}
|
|---|
| 711 | */
|
|---|