Changeset 9f029aa in mainline
- Timestamp:
- 2013-05-08T10:07:36Z (12 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 959d2ec
- Parents:
- d531bd6
- Location:
- uspace/srv/net/dnsrsrv
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/srv/net/dnsrsrv/dns_msg.c
rd531bd6 r9f029aa 50 50 static uint16_t dns_uint16_t_decode(uint8_t *, size_t); 51 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 */ 52 58 static int dns_dstr_ext(char **dstr, const char *suff) 53 59 { … … 77 83 } 78 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 */ 79 95 static int dns_name_encode(char *name, uint8_t *buf, size_t buf_size, 80 96 size_t *act_size) … … 135 151 } 136 152 137 int dns_name_decode(uint8_t *buf, size_t size, size_t boff, char **rname, 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, 138 161 size_t *eoff) 139 162 { … … 151 174 name = NULL; 152 175 153 if (boff > size)176 if (boff > pdu->size) 154 177 return EINVAL; 155 178 156 bp = buf+ boff;157 bsize = min( size - boff, DNS_NAME_MAX_SIZE);179 bp = pdu->data + boff; 180 bsize = min(pdu->size - boff, DNS_NAME_MAX_SIZE); 158 181 first = true; 159 182 *eoff = 0; … … 192 215 --bsize; 193 216 194 if (ptr >= (size_t)(bp - buf)) {217 if (ptr >= (size_t)(bp - pdu->data)) { 195 218 log_msg(LOG_DEFAULT, LVL_DEBUG, 196 219 "Pointer- forward ref %zu, pos=%zu", 197 ptr, (size_t)(bp - buf));220 ptr, (size_t)(bp - pdu->data)); 198 221 /* Forward reference */ 199 222 rc = EINVAL; … … 205 228 * XXX Is assumption correct? 206 229 */ 207 eptr = bp - buf;230 eptr = bp - pdu->data; 208 231 /* 209 232 * This is where encoded name ends in terms where … … 212 235 *eoff = eptr; 213 236 214 bp = buf+ ptr;237 bp = pdu->data + ptr; 215 238 bsize = eptr - ptr; 216 239 continue; … … 245 268 *rname = name; 246 269 if (*eoff == 0) 247 *eoff = bp - buf;270 *eoff = bp - pdu->data; 248 271 return EOK; 249 272 error: … … 284 307 } 285 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 */ 286 319 static int dns_question_encode(dns_question_t *question, uint8_t *buf, 287 320 size_t buf_size, size_t *act_size) … … 310 343 } 311 344 312 static int dns_question_decode(uint8_t *buf, size_t buf_size, size_t boff, 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 */ 352 static int dns_question_decode(dns_pdu_t *pdu, size_t boff, 313 353 dns_question_t **rquestion, size_t *eoff) 314 354 { … … 321 361 return ENOMEM; 322 362 323 rc = dns_name_decode( buf, buf_size, boff, &question->qname, &name_eoff);363 rc = dns_name_decode(pdu, boff, &question->qname, &name_eoff); 324 364 if (rc != EOK) { 325 365 log_msg(LOG_DEFAULT, LVL_DEBUG, "Error decoding name"); … … 328 368 } 329 369 330 if (name_eoff + 2 * sizeof(uint16_t) > buf_size) {370 if (name_eoff + 2 * sizeof(uint16_t) > pdu->size) { 331 371 free(question); 332 372 return EINVAL; 333 373 } 334 374 335 question->qtype = dns_uint16_t_decode(buf + name_eoff, buf_size - name_eoff); 336 question->qclass = dns_uint16_t_decode(buf + sizeof(uint16_t) + name_eoff, 337 buf_size - sizeof(uint16_t) - name_eoff); 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); 338 379 *eoff = name_eoff + 2 * sizeof(uint16_t); 339 380 … … 342 383 } 343 384 344 static int dns_rr_decode(uint8_t *buf, size_t buf_size, size_t boff, 345 dns_rr_t **retrr, size_t *eoff) 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 */ 392 static int dns_rr_decode(dns_pdu_t *pdu, size_t boff, dns_rr_t **retrr, 393 size_t *eoff) 346 394 { 347 395 dns_rr_t *rr; … … 356 404 return ENOMEM; 357 405 358 rc = dns_name_decode( buf, buf_size, boff, &rr->name, &name_eoff);406 rc = dns_name_decode(pdu, boff, &rr->name, &name_eoff); 359 407 if (rc != EOK) { 360 408 log_msg(LOG_DEFAULT, LVL_DEBUG, "Error decoding name"); … … 363 411 } 364 412 365 if (name_eoff + 2 * sizeof(uint16_t) > buf_size) {413 if (name_eoff + 2 * sizeof(uint16_t) > pdu->size) { 366 414 free(rr->name); 367 415 free(rr); … … 369 417 } 370 418 371 bp = buf+ name_eoff;372 bsz = buf_size - name_eoff;419 bp = pdu->data + name_eoff; 420 bsz = pdu->size - name_eoff; 373 421 374 422 if (bsz < 3 * sizeof(uint16_t) + sizeof(uint32_t)) { … … 405 453 406 454 memcpy(rr->rdata, bp, rdlength); 407 rr->roff = bp - buf;455 rr->roff = bp - pdu->data; 408 456 bp += rdlength; 409 457 bsz -= rdlength; 410 458 411 *eoff = bp - buf;459 *eoff = bp - pdu->data; 412 460 *retrr = rr; 413 461 return EOK; 414 462 } 415 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 */ 416 473 int dns_message_encode(dns_message_t *msg, void **rdata, size_t *rsize) 417 474 { … … 475 532 } 476 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 */ 477 543 int dns_message_decode(void *data, size_t size, dns_message_t **rmsg) 478 544 { … … 499 565 /* Store a copy of raw message data for string decompression */ 500 566 501 msg-> raw= malloc(size);502 if (msg-> raw== NULL) {503 rc = E INVAL;567 msg->pdu.data = malloc(size); 568 if (msg->pdu.data == NULL) { 569 rc = ENOMEM; 504 570 goto error; 505 571 } 506 572 507 memcpy(msg-> raw, data, size);508 msg-> raw_size = size;509 log_msg(LOG_DEFAULT, LVL_NOTE, "dns_message_decode: msg->raw = %p, msg->raw_size=%zu",510 msg->raw, msg->raw_size);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); 511 577 512 578 hdr = data; … … 528 594 529 595 for (i = 0; i < qd_count; i++) { 530 rc = dns_question_decode( data, size, doff, &question, &field_eoff);596 rc = dns_question_decode(&msg->pdu, doff, &question, &field_eoff); 531 597 if (rc != EOK) { 532 598 log_msg(LOG_DEFAULT, LVL_DEBUG, "error decoding question"); … … 541 607 542 608 for (i = 0; i < an_count; i++) { 543 rc = dns_rr_decode( data, size, doff, &rr, &field_eoff);609 rc = dns_rr_decode(&msg->pdu, doff, &rr, &field_eoff); 544 610 if (rc != EOK) { 545 611 log_msg(LOG_DEFAULT, LVL_DEBUG, "Error decoding answer"); … … 558 624 } 559 625 626 /** Destroy question. */ 560 627 static void dns_question_destroy(dns_question_t *question) 561 628 { … … 564 631 } 565 632 633 /** Destroy resource record. */ 566 634 static void dns_rr_destroy(dns_rr_t *rr) 567 635 { … … 571 639 } 572 640 641 /** Create new empty message. */ 573 642 dns_message_t *dns_message_new(void) 574 643 { … … 587 656 } 588 657 658 /** Destroy message. */ 589 659 void dns_message_destroy(dns_message_t *msg) 590 660 { … … 621 691 } 622 692 623 free(msg-> raw);693 free(msg->pdu.data); 624 694 free(msg); 625 695 } -
uspace/srv/net/dnsrsrv/dns_msg.h
rd531bd6 r9f029aa 47 47 extern dns_message_t *dns_message_new(void); 48 48 extern void dns_message_destroy(dns_message_t *); 49 extern int dns_name_decode( uint8_t *, size_t, size_t, char **, size_t *);49 extern int dns_name_decode(dns_pdu_t *, size_t, char **, size_t *); 50 50 extern uint32_t dns_uint32_t_decode(uint8_t *, size_t); 51 51 -
uspace/srv/net/dnsrsrv/dns_type.h
rd531bd6 r9f029aa 43 43 #include "dns_std.h" 44 44 45 /** Encoded DNS PDU */ 46 typedef struct { 47 /** Encoded PDU data */ 48 uint8_t *data; 49 /** Encoded PDU size */ 50 size_t size; 51 } dns_pdu_t; 52 45 53 /** DNS message */ 46 54 typedef struct { 47 /** Raw message data */ 48 void *raw; 49 /** Raw message size */ 50 size_t raw_size; 55 /* Encoded PDU */ 56 dns_pdu_t pdu; 51 57 52 58 /** Identifier */ -
uspace/srv/net/dnsrsrv/query.c
rd531bd6 r9f029aa 97 97 str_cmp(rr->name, sname) == 0) { 98 98 log_msg(LOG_DEFAULT, LVL_DEBUG, "decode cname (%p, %zu, %zu)", 99 amsg->raw, amsg->raw_size, rr->roff); 100 rc = dns_name_decode(amsg->raw, amsg->raw_size, rr->roff, 101 &cname, &eoff); 99 amsg->pdu.data, amsg->pdu.size, rr->roff); 100 rc = dns_name_decode(&amsg->pdu, rr->roff, &cname, &eoff); 102 101 if (rc != EOK) { 103 102 log_msg(LOG_DEFAULT, LVL_DEBUG,
Note:
See TracChangeset
for help on using the changeset viewer.