Changeset 7262f89 in mainline
- Timestamp:
- 2013-04-20T10:42:13Z (12 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- dc95342
- Parents:
- f1dcf6d
- Location:
- uspace/srv/net/dnsres
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/srv/net/dnsres/dns_msg.c
rf1dcf6d r7262f89 49 49 static uint16_t dns_uint16_t_decode(uint8_t *, size_t); 50 50 51 static 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 51 78 #include <stdio.h> 52 79 static int dns_name_encode(char *name, uint8_t *buf, size_t buf_size, … … 69 96 c = str_decode(name, &off, STR_NO_LIMIT); 70 97 printf("c=%d\n", (int)c); 71 if (c > 127) {98 if (c >= 127) { 72 99 /* Non-ASCII character */ 73 100 printf("non-ascii character\n"); … … 121 148 size_t ptr; 122 149 size_t eptr; 150 char *name; 151 char dbuf[2]; 152 int rc; 153 bool first; 154 155 name = NULL; 123 156 124 157 if (boff > size) … … 127 160 bp = buf + boff; 128 161 bsize = min(size - boff, DNS_NAME_MAX_SIZE); 162 first = true; 163 *eoff = 0; 129 164 130 165 while (true) { 131 166 if (bsize == 0) { 132 return EINVAL; 167 rc = EINVAL; 168 goto error; 133 169 } 134 170 … … 140 176 break; 141 177 142 if ( bp != buf + boff + 1)178 if (!first) { 143 179 printf("."); 180 rc = dns_dstr_ext(&name, "."); 181 if (rc != EOK) { 182 rc = ENOMEM; 183 goto error; 184 } 185 } 144 186 145 187 if ((lsize & 0xc0) == 0xc0) { … … 148 190 if (bsize < 1) { 149 191 printf("Pointer- bsize < 1\n"); 150 return EINVAL; 192 rc = EINVAL; 193 goto error; 151 194 } 152 195 153 196 ptr = dns_uint16_t_decode(bp - 1, bsize) & 0x3fff; 197 ++bp; 198 --bsize; 199 154 200 if (ptr >= (size_t)(bp - buf)) { 155 201 printf("Pointer- forward ref %u, pos=%u\n", 156 202 ptr, bp - buf); 157 203 /* Forward reference */ 158 return EINVAL; 204 rc = EINVAL; 205 goto error; 159 206 } 160 207 … … 163 210 * XXX Is assumption correct? 164 211 */ 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; 166 218 167 219 printf("ptr=%u, eptr=%u\n", ptr, eptr); … … 172 224 173 225 if (lsize > bsize) { 174 return EINVAL; 226 rc = EINVAL; 227 goto error; 175 228 } 176 229 177 230 for (i = 0; i < lsize; i++) { 178 231 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 } 179 246 ++bp; 180 247 --bsize; 181 248 } 249 250 first = false; 182 251 } 183 252 184 253 printf("\n"); 185 254 186 *eoff = bp - buf; 187 return EOK; 255 *rname = name; 256 if (*eoff == 0) 257 *eoff = bp - buf; 258 return EOK; 259 error: 260 free(name); 261 return rc; 188 262 } 189 263 … … 207 281 208 282 /** Decode unaligned big-endian 32-bit integer */ 209 static uint16_t dns_uint32_t_decode(uint8_t *buf, size_t buf_size) 210 { 283 uint32_t dns_uint32_t_decode(uint8_t *buf, size_t buf_size) 284 { 285 uint32_t w; 211 286 assert(buf_size >= 4); 212 287 213 return((uint32_t)buf[0] << 24) +288 w = ((uint32_t)buf[0] << 24) + 214 289 ((uint32_t)buf[1] << 16) + 215 290 ((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; 217 296 } 218 297 … … 304 383 } 305 384 306 printf("ok decoding name.. \n");385 printf("ok decoding name.. '%s'\n", rr->name); 307 386 if (name_eoff + 2 * sizeof(uint16_t) > buf_size) { 308 387 printf("name_eoff + 2 * 2 = %d > buf_size = %d\n", … … 324 403 rr->rtype = dns_uint16_t_decode(bp, bsz); 325 404 bp += sizeof(uint16_t); bsz -= sizeof(uint16_t); 405 printf("rtype=%u\n", rr->rtype); 326 406 327 407 rr->rclass = dns_uint16_t_decode(bp, bsz); 328 408 bp += sizeof(uint16_t); bsz -= sizeof(uint16_t); 409 printf("rclass=%u\n", rr->rclass); 329 410 330 411 rr->ttl = dns_uint32_t_decode(bp, bsz); 331 412 bp += sizeof(uint32_t); bsz -= sizeof(uint32_t); 413 printf("ttl=%u\n", rr->ttl); 332 414 333 415 rdlength = dns_uint16_t_decode(bp, bsz); 334 416 bp += sizeof(uint16_t); bsz -= sizeof(uint16_t); 417 printf("rdlength=%u\n", rdlength); 335 418 336 419 if (rdlength > bsz) { … … 469 552 printf("ok decoding question\n"); 470 553 554 list_append(&question->msg, &msg->question); 471 555 doff = field_eoff; 472 556 } … … 484 568 printf("ok decoding answer\n"); 485 569 570 list_append(&rr->msg, &msg->answer); 486 571 doff = field_eoff; 487 572 } -
uspace/srv/net/dnsres/dns_msg.h
rf1dcf6d r7262f89 45 45 extern int dns_message_encode(dns_message_t *, void **, size_t *); 46 46 extern int dns_message_decode(void *, size_t, dns_message_t **); 47 extern uint32_t dns_uint32_t_decode(uint8_t *, size_t); 47 48 48 49 #endif -
uspace/srv/net/dnsres/dns_type.h
rf1dcf6d r7262f89 38 38 39 39 #include <adt/list.h> 40 #include <inet/inet.h> 40 41 #include <stdbool.h> 41 42 #include <stdint.h> … … 80 81 /** Unencoded DNS resource record */ 81 82 typedef struct { 83 link_t msg; 82 84 /** Domain name */ 83 85 char *name; … … 97 99 /** Host information */ 98 100 typedef struct { 101 /** Host name */ 102 char *name; 103 /** Host address */ 104 inet_addr_t addr; 99 105 } dns_host_info_t; 100 106 -
uspace/srv/net/dnsres/dnsres.c
rf1dcf6d r7262f89 35 35 36 36 #include <stdio.h> 37 #include <stdlib.h> 37 38 #include <errno.h> 38 39 … … 43 44 #define NAME "dnsres" 44 45 46 static 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 45 60 int main(int argc, char *argv[]) 46 61 { 47 62 dns_host_info_t hinfo; 63 char *astr; 48 64 int rc; 49 65 50 66 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); 52 68 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 } 53 81 54 82 return 0; -
uspace/srv/net/dnsres/query.c
rf1dcf6d r7262f89 36 36 #include <errno.h> 37 37 #include <mem.h> 38 #include <str.h> 38 39 40 #include "dns_msg.h" 39 41 #include "dns_std.h" 40 42 #include "dns_type.h" … … 44 46 static uint16_t msg_id; 45 47 48 #include <stdio.h> 46 49 int dns_name2host(const char *name, dns_host_info_t *info) 47 50 { … … 72 75 return rc; 73 76 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; 75 99 } 76 100
Note:
See TracChangeset
for help on using the changeset viewer.