Changeset 9f029aa in mainline


Ignore:
Timestamp:
2013-05-08T10:07:36Z (11 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
959d2ec
Parents:
d531bd6
Message:

Simplify slightly, comments.

Location:
uspace/srv/net/dnsrsrv
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • uspace/srv/net/dnsrsrv/dns_msg.c

    rd531bd6 r9f029aa  
    5050static uint16_t dns_uint16_t_decode(uint8_t *, size_t);
    5151
     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 */
    5258static int dns_dstr_ext(char **dstr, const char *suff)
    5359{
     
    7783}
    7884
     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 */
    7995static int dns_name_encode(char *name, uint8_t *buf, size_t buf_size,
    8096    size_t *act_size)
     
    135151}
    136152
    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 */
     160int dns_name_decode(dns_pdu_t *pdu, size_t boff, char **rname,
    138161    size_t *eoff)
    139162{
     
    151174        name = NULL;
    152175
    153         if (boff > size)
     176        if (boff > pdu->size)
    154177                return EINVAL;
    155178
    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);
    158181        first = true;
    159182        *eoff = 0;
     
    192215                        --bsize;
    193216
    194                         if (ptr >= (size_t)(bp - buf)) {
     217                        if (ptr >= (size_t)(bp - pdu->data)) {
    195218                                log_msg(LOG_DEFAULT, LVL_DEBUG,
    196219                                    "Pointer- forward ref %zu, pos=%zu",
    197                                     ptr, (size_t)(bp - buf));
     220                                    ptr, (size_t)(bp - pdu->data));
    198221                                /* Forward reference */
    199222                                rc = EINVAL;
     
    205228                         * XXX Is assumption correct?
    206229                         */
    207                         eptr = bp - buf;
     230                        eptr = bp - pdu->data;
    208231                        /*
    209232                         * This is where encoded name ends in terms where
     
    212235                        *eoff = eptr;
    213236
    214                         bp = buf + ptr;
     237                        bp = pdu->data + ptr;
    215238                        bsize = eptr - ptr;
    216239                        continue;
     
    245268        *rname = name;
    246269        if (*eoff == 0)
    247                 *eoff = bp - buf;
     270                *eoff = bp - pdu->data;
    248271        return EOK;
    249272error:
     
    284307}
    285308
     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 */
    286319static int dns_question_encode(dns_question_t *question, uint8_t *buf,
    287320    size_t buf_size, size_t *act_size)
     
    310343}
    311344
    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 */
     352static int dns_question_decode(dns_pdu_t *pdu, size_t boff,
    313353    dns_question_t **rquestion, size_t *eoff)
    314354{
     
    321361                return ENOMEM;
    322362
    323         rc = dns_name_decode(buf, buf_size, boff, &question->qname, &name_eoff);
     363        rc = dns_name_decode(pdu, boff, &question->qname, &name_eoff);
    324364        if (rc != EOK) {
    325365                log_msg(LOG_DEFAULT, LVL_DEBUG, "Error decoding name");
     
    328368        }
    329369
    330         if (name_eoff + 2 * sizeof(uint16_t) > buf_size) {
     370        if (name_eoff + 2 * sizeof(uint16_t) > pdu->size) {
    331371                free(question);
    332372                return EINVAL;
    333373        }
    334374
    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);
    338379        *eoff = name_eoff + 2 * sizeof(uint16_t);
    339380
     
    342383}
    343384
    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 */
     392static int dns_rr_decode(dns_pdu_t *pdu, size_t boff, dns_rr_t **retrr,
     393    size_t *eoff)
    346394{
    347395        dns_rr_t *rr;
     
    356404                return ENOMEM;
    357405
    358         rc = dns_name_decode(buf, buf_size, boff, &rr->name, &name_eoff);
     406        rc = dns_name_decode(pdu, boff, &rr->name, &name_eoff);
    359407        if (rc != EOK) {
    360408                log_msg(LOG_DEFAULT, LVL_DEBUG, "Error decoding name");
     
    363411        }
    364412
    365         if (name_eoff + 2 * sizeof(uint16_t) > buf_size) {
     413        if (name_eoff + 2 * sizeof(uint16_t) > pdu->size) {
    366414                free(rr->name);
    367415                free(rr);
     
    369417        }
    370418
    371         bp = buf + name_eoff;
    372         bsz = buf_size - name_eoff;
     419        bp = pdu->data + name_eoff;
     420        bsz = pdu->size - name_eoff;
    373421
    374422        if (bsz < 3 * sizeof(uint16_t) + sizeof(uint32_t)) {
     
    405453
    406454        memcpy(rr->rdata, bp, rdlength);
    407         rr->roff = bp - buf;
     455        rr->roff = bp - pdu->data;
    408456        bp += rdlength;
    409457        bsz -= rdlength;
    410458
    411         *eoff = bp - buf;
     459        *eoff = bp - pdu->data;
    412460        *retrr = rr;
    413461        return EOK;
    414462}
    415463
     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 */
    416473int dns_message_encode(dns_message_t *msg, void **rdata, size_t *rsize)
    417474{
     
    475532}
    476533
     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 */
    477543int dns_message_decode(void *data, size_t size, dns_message_t **rmsg)
    478544{
     
    499565        /* Store a copy of raw message data for string decompression */
    500566
    501         msg->raw = malloc(size);
    502         if (msg->raw == NULL) {
    503                 rc = EINVAL;
     567        msg->pdu.data = malloc(size);
     568        if (msg->pdu.data == NULL) {
     569                rc = ENOMEM;
    504570                goto error;
    505571        }
    506572
    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);
    511577
    512578        hdr = data;
     
    528594
    529595        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);
    531597                if (rc != EOK) {
    532598                        log_msg(LOG_DEFAULT, LVL_DEBUG, "error decoding question");
     
    541607
    542608        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);
    544610                if (rc != EOK) {
    545611                        log_msg(LOG_DEFAULT, LVL_DEBUG, "Error decoding answer");
     
    558624}
    559625
     626/** Destroy question. */
    560627static void dns_question_destroy(dns_question_t *question)
    561628{
     
    564631}
    565632
     633/** Destroy resource record. */
    566634static void dns_rr_destroy(dns_rr_t *rr)
    567635{
     
    571639}
    572640
     641/** Create new empty message. */
    573642dns_message_t *dns_message_new(void)
    574643{
     
    587656}
    588657
     658/** Destroy message. */
    589659void dns_message_destroy(dns_message_t *msg)
    590660{
     
    621691        }
    622692
    623         free(msg->raw);
     693        free(msg->pdu.data);
    624694        free(msg);
    625695}
  • uspace/srv/net/dnsrsrv/dns_msg.h

    rd531bd6 r9f029aa  
    4747extern dns_message_t *dns_message_new(void);
    4848extern void dns_message_destroy(dns_message_t *);
    49 extern int dns_name_decode(uint8_t *, size_t, size_t, char **, size_t *);
     49extern int dns_name_decode(dns_pdu_t *, size_t, char **, size_t *);
    5050extern uint32_t dns_uint32_t_decode(uint8_t *, size_t);
    5151
  • uspace/srv/net/dnsrsrv/dns_type.h

    rd531bd6 r9f029aa  
    4343#include "dns_std.h"
    4444
     45/** Encoded DNS PDU */
     46typedef struct {
     47        /** Encoded PDU data */
     48        uint8_t *data;
     49        /** Encoded PDU size */
     50        size_t size;
     51} dns_pdu_t;
     52
    4553/** DNS message */
    4654typedef 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;
    5157
    5258        /** Identifier */
  • uspace/srv/net/dnsrsrv/query.c

    rd531bd6 r9f029aa  
    9797                    str_cmp(rr->name, sname) == 0) {
    9898                        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);
    102101                        if (rc != EOK) {
    103102                                log_msg(LOG_DEFAULT, LVL_DEBUG,
Note: See TracChangeset for help on using the changeset viewer.