Changeset 7262f89 in mainline for uspace/srv/net/dnsres/dns_msg.c
- 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
- File:
-
- 1 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 }
Note:
See TracChangeset
for help on using the changeset viewer.