Changeset 7262f89 in mainline


Ignore:
Timestamp:
2013-04-20T10:42:13Z (11 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.

Location:
uspace/srv/net/dnsres
Files:
5 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        }
  • uspace/srv/net/dnsres/dns_msg.h

    rf1dcf6d r7262f89  
    4545extern int dns_message_encode(dns_message_t *, void **, size_t *);
    4646extern int dns_message_decode(void *, size_t, dns_message_t **);
     47extern uint32_t dns_uint32_t_decode(uint8_t *, size_t);
    4748
    4849#endif
  • uspace/srv/net/dnsres/dns_type.h

    rf1dcf6d r7262f89  
    3838
    3939#include <adt/list.h>
     40#include <inet/inet.h>
    4041#include <stdbool.h>
    4142#include <stdint.h>
     
    8081/** Unencoded DNS resource record */
    8182typedef struct {
     83        link_t msg;
    8284        /** Domain name */
    8385        char *name;
     
    9799/** Host information */
    98100typedef struct {
     101        /** Host name */
     102        char *name;
     103        /** Host address */
     104        inet_addr_t addr;
    99105} dns_host_info_t;
    100106
  • uspace/srv/net/dnsres/dnsres.c

    rf1dcf6d r7262f89  
    3535
    3636#include <stdio.h>
     37#include <stdlib.h>
    3738#include <errno.h>
    3839
     
    4344#define NAME  "dnsres"
    4445
     46static int addr_format(inet_addr_t *addr, char **bufp)
     47{
     48        int rc;
     49
     50        rc = asprintf(bufp, "%d.%d.%d.%d", addr->ipv4 >> 24,
     51            (addr->ipv4 >> 16) & 0xff, (addr->ipv4 >> 8) & 0xff,
     52            addr->ipv4 & 0xff);
     53
     54        if (rc < 0)
     55                return ENOMEM;
     56
     57        return EOK;
     58}
     59
    4560int main(int argc, char *argv[])
    4661{
    4762        dns_host_info_t hinfo;
     63        char *astr;
    4864        int rc;
    4965
    5066        printf("%s: DNS Resolution Service\n", NAME);
    51         rc = dns_name2host("helenos.org", &hinfo);
     67        rc = dns_name2host(argc < 2 ? "helenos.org" : argv[1], &hinfo);
    5268        printf("dns_name2host() -> rc = %d\n", rc);
     69
     70        if (rc == EOK) {
     71                rc = addr_format(&hinfo.addr, &astr);
     72                if (rc != EOK) {
     73                        printf("Out of memory\n");
     74                        return ENOMEM;
     75                }
     76
     77                printf("hostname: %s\n", hinfo.name);
     78                printf("IPv4 address: %s\n", astr);
     79                free(astr);
     80        }
    5381
    5482        return 0;
  • uspace/srv/net/dnsres/query.c

    rf1dcf6d r7262f89  
    3636#include <errno.h>
    3737#include <mem.h>
     38#include <str.h>
    3839
     40#include "dns_msg.h"
    3941#include "dns_std.h"
    4042#include "dns_type.h"
     
    4446static uint16_t msg_id;
    4547
     48#include <stdio.h>
    4649int dns_name2host(const char *name, dns_host_info_t *info)
    4750{
     
    7275                return rc;
    7376
    74         return EOK;
     77        list_foreach(amsg->answer, link) {
     78                dns_rr_t *rr = list_get_instance(link, dns_rr_t, msg);
     79
     80                printf(" - '%s' %u/%u, dsize %u\n",
     81                        rr->name, rr->rtype, rr->rclass, rr->rdata_size);
     82
     83                if (rr->rtype == DTYPE_A && rr->rclass == DC_IN) {
     84                        if (rr->rdata_size != sizeof(uint32_t)) {
     85                                printf("rdata_size = %u - fail\n", rr->rdata_size);
     86                                return EIO;
     87                        }
     88
     89                        info->name = str_dup(rr->name);
     90                        info->addr.ipv4 = dns_uint32_t_decode(rr->rdata, rr->rdata_size);
     91                        printf("info->addr = %x\n", info->addr.ipv4);
     92                        return EOK;
     93                }
     94        }
     95
     96        printf("no A/IN found, fail\n");
     97
     98        return EIO;
    7599}
    76100
Note: See TracChangeset for help on using the changeset viewer.