Changeset 7262f89 in mainline for uspace/srv/net/dnsres/dns_msg.c


Ignore:
Timestamp:
2013-04-20T10:42:13Z (12 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
dc95342
Parents:
f1dcf6d
Message:

Construct domain names, fix some bugs. Parse answer, print resulting hostname and IP address.

File:
1 edited

Legend:

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

    rf1dcf6d r7262f89  
    4949static uint16_t dns_uint16_t_decode(uint8_t *, size_t);
    5050
     51static int dns_dstr_ext(char **dstr, const char *suff)
     52{
     53        size_t s1, s2;
     54        size_t nsize;
     55        char *nstr;
     56
     57        if (*dstr == NULL) {
     58                *dstr = str_dup(suff);
     59                if (*dstr == NULL)
     60                        return ENOMEM;
     61                return EOK;
     62        }
     63
     64        s1 = str_size(*dstr);
     65        s2 = str_size(suff);
     66        nsize = s1 + s2 + 1;
     67
     68        nstr = realloc(*dstr, nsize);
     69        if (nstr == NULL)
     70                return ENOMEM;
     71
     72        str_cpy((*dstr) + s1, nsize - s1, suff);
     73
     74        *dstr = nstr;
     75        return EOK;
     76}
     77
    5178#include <stdio.h>
    5279static int dns_name_encode(char *name, uint8_t *buf, size_t buf_size,
     
    6996                c = str_decode(name, &off, STR_NO_LIMIT);
    7097                printf("c=%d\n", (int)c);
    71                 if (c > 127) {
     98                if (c >= 127) {
    7299                        /* Non-ASCII character */
    73100                        printf("non-ascii character\n");
     
    121148        size_t ptr;
    122149        size_t eptr;
     150        char *name;
     151        char dbuf[2];
     152        int rc;
     153        bool first;
     154
     155        name = NULL;
    123156
    124157        if (boff > size)
     
    127160        bp = buf + boff;
    128161        bsize = min(size - boff, DNS_NAME_MAX_SIZE);
     162        first = true;
     163        *eoff = 0;
    129164
    130165        while (true) {
    131166                if (bsize == 0) {
    132                         return EINVAL;
     167                        rc = EINVAL;
     168                        goto error;
    133169                }
    134170
     
    140176                        break;
    141177
    142                 if (bp != buf + boff + 1)
     178                if (!first) {
    143179                        printf(".");
     180                        rc = dns_dstr_ext(&name, ".");
     181                        if (rc != EOK) {
     182                                rc = ENOMEM;
     183                                goto error;
     184                        }
     185                }
    144186
    145187                if ((lsize & 0xc0) == 0xc0) {
     
    148190                        if (bsize < 1) {
    149191                                printf("Pointer- bsize < 1\n");
    150                                 return EINVAL;
     192                                rc = EINVAL;
     193                                goto error;
    151194                        }
    152195
    153196                        ptr = dns_uint16_t_decode(bp - 1, bsize) & 0x3fff;
     197                        ++bp;
     198                        --bsize;
     199
    154200                        if (ptr >= (size_t)(bp - buf)) {
    155201                                printf("Pointer- forward ref %u, pos=%u\n",
    156202                                    ptr, bp - buf);
    157203                                /* Forward reference */
    158                                 return EINVAL;
     204                                rc = EINVAL;
     205                                goto error;
    159206                        }
    160207
     
    163210                         * XXX Is assumption correct?
    164211                         */
    165                         eptr = buf - bp;
     212                        eptr = bp - buf;
     213                        /*
     214                         * This is where encoded name ends in terms where
     215                         * the message continues
     216                         */
     217                        *eoff = eptr;
    166218
    167219                        printf("ptr=%u, eptr=%u\n", ptr, eptr);
     
    172224
    173225                if (lsize > bsize) {
    174                         return EINVAL;
     226                        rc = EINVAL;
     227                        goto error;
    175228                }
    176229
    177230                for (i = 0; i < lsize; i++) {
    178231                        printf("%c", *bp);
     232
     233                        if (*bp < 32 || *bp >= 127) {
     234                                rc = EINVAL;
     235                                goto error;
     236                        }
     237
     238                        dbuf[0] = *bp;
     239                        dbuf[1] = '\0';
     240
     241                        rc = dns_dstr_ext(&name, dbuf);
     242                        if (rc != EOK) {
     243                                rc = ENOMEM;
     244                                goto error;
     245                        }
    179246                        ++bp;
    180247                        --bsize;
    181248                }
     249
     250                first = false;
    182251        }
    183252
    184253        printf("\n");
    185254
    186         *eoff = bp - buf;
    187         return EOK;
     255        *rname = name;
     256        if (*eoff == 0)
     257                *eoff = bp - buf;
     258        return EOK;
     259error:
     260        free(name);
     261        return rc;
    188262}
    189263
     
    207281
    208282/** Decode unaligned big-endian 32-bit integer */
    209 static uint16_t dns_uint32_t_decode(uint8_t *buf, size_t buf_size)
    210 {
     283uint32_t dns_uint32_t_decode(uint8_t *buf, size_t buf_size)
     284{
     285        uint32_t w;
    211286        assert(buf_size >= 4);
    212287
    213         return ((uint32_t)buf[0] << 24) +
     288        w = ((uint32_t)buf[0] << 24) +
    214289            ((uint32_t)buf[1] << 16) +
    215290            ((uint32_t)buf[2] << 8) +
    216             buf[0];
     291            buf[3];
     292
     293        printf("dns_uint32_t_decode: %x, %x, %x, %x -> %x\n",
     294            buf[0], buf[1], buf[2], buf[3], w);
     295        return w;
    217296}
    218297
     
    304383        }
    305384
    306         printf("ok decoding name..\n");
     385        printf("ok decoding name.. '%s'\n", rr->name);
    307386        if (name_eoff + 2 * sizeof(uint16_t) > buf_size) {
    308387                printf("name_eoff + 2 * 2 = %d >  buf_size = %d\n",
     
    324403        rr->rtype = dns_uint16_t_decode(bp, bsz);
    325404        bp += sizeof(uint16_t); bsz -= sizeof(uint16_t);
     405        printf("rtype=%u\n", rr->rtype);
    326406
    327407        rr->rclass = dns_uint16_t_decode(bp, bsz);
    328408        bp += sizeof(uint16_t); bsz -= sizeof(uint16_t);
     409        printf("rclass=%u\n", rr->rclass);
    329410
    330411        rr->ttl = dns_uint32_t_decode(bp, bsz);
    331412        bp += sizeof(uint32_t); bsz -= sizeof(uint32_t);
     413        printf("ttl=%u\n", rr->ttl);
    332414
    333415        rdlength = dns_uint16_t_decode(bp, bsz);
    334416        bp += sizeof(uint16_t); bsz -= sizeof(uint16_t);
     417        printf("rdlength=%u\n", rdlength);
    335418
    336419        if (rdlength > bsz) {
     
    469552                printf("ok decoding question\n");
    470553
     554                list_append(&question->msg, &msg->question);
    471555                doff = field_eoff;
    472556        }
     
    484568                printf("ok decoding answer\n");
    485569
     570                list_append(&rr->msg, &msg->answer);
    486571                doff = field_eoff;
    487572        }
Note: See TracChangeset for help on using the changeset viewer.