ns_name.c revision 1.1.1.3 1 /* $NetBSD: ns_name.c,v 1.1.1.3 2007/01/27 21:45:37 christos Exp $ */
2
3 /*
4 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (c) 1996,1999 by Internet Software Consortium.
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
17 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20 #ifndef lint
21 static const char rcsid[] = "Id: ns_name.c,v 1.8.18.2 2005/04/27 05:01:08 sra Exp";
22 #endif
23
24 #include "port_before.h"
25
26 #include <sys/types.h>
27
28 #include <netinet/in.h>
29 #include <arpa/nameser.h>
30
31 #include <errno.h>
32 #include <resolv.h>
33 #include <string.h>
34 #include <ctype.h>
35 #include <stdlib.h>
36 #include <limits.h>
37
38 #include "port_after.h"
39
40 #ifdef SPRINTF_CHAR
41 # define SPRINTF(x) strlen(sprintf/**/x)
42 #else
43 # define SPRINTF(x) ((size_t)sprintf x)
44 #endif
45
46 #define NS_TYPE_ELT 0x40 /*%< EDNS0 extended label type */
47 #define DNS_LABELTYPE_BITSTRING 0x41
48
49 /* Data. */
50
51 static const char digits[] = "0123456789";
52
53 static const char digitvalue[256] = {
54 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*16*/
55 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*32*/
56 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*48*/
57 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, /*64*/
58 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*80*/
59 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*96*/
60 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*112*/
61 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*128*/
62 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
63 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
64 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
65 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
66 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
67 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
68 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
69 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*256*/
70 };
71
72 /* Forward. */
73
74 static int special(int);
75 static int printable(int);
76 static int dn_find(const u_char *, const u_char *,
77 const u_char * const *,
78 const u_char * const *);
79 static int encode_bitsring(const char **, const char *,
80 unsigned char **, unsigned char **,
81 unsigned const char *);
82 static int labellen(const u_char *);
83 static int decode_bitstring(const unsigned char **,
84 char *, const char *);
85
86 /* Public. */
87
88 /*%
89 * Convert an encoded domain name to printable ascii as per RFC1035.
90
91 * return:
92 *\li Number of bytes written to buffer, or -1 (with errno set)
93 *
94 * notes:
95 *\li The root is returned as "."
96 *\li All other domains are returned in non absolute form
97 */
98 int
99 ns_name_ntop(const u_char *src, char *dst, size_t dstsiz)
100 {
101 const u_char *cp;
102 char *dn, *eom;
103 u_char c;
104 u_int n;
105 int l;
106
107 cp = src;
108 dn = dst;
109 eom = dst + dstsiz;
110
111 while ((n = *cp++) != 0) {
112 if ((n & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
113 /* Some kind of compression pointer. */
114 errno = EMSGSIZE;
115 return (-1);
116 }
117 if (dn != dst) {
118 if (dn >= eom) {
119 errno = EMSGSIZE;
120 return (-1);
121 }
122 *dn++ = '.';
123 }
124 if ((l = labellen(cp - 1)) < 0) {
125 errno = EMSGSIZE; /*%< XXX */
126 return(-1);
127 }
128 if (dn + l >= eom) {
129 errno = EMSGSIZE;
130 return (-1);
131 }
132 if ((n & NS_CMPRSFLGS) == NS_TYPE_ELT) {
133 int m;
134
135 if (n != DNS_LABELTYPE_BITSTRING) {
136 /* XXX: labellen should reject this case */
137 errno = EINVAL;
138 return(-1);
139 }
140 if ((m = decode_bitstring(&cp, dn, eom)) < 0)
141 {
142 errno = EMSGSIZE;
143 return(-1);
144 }
145 dn += m;
146 continue;
147 }
148 for ((void)NULL; l > 0; l--) {
149 c = *cp++;
150 if (special(c)) {
151 if (dn + 1 >= eom) {
152 errno = EMSGSIZE;
153 return (-1);
154 }
155 *dn++ = '\\';
156 *dn++ = (char)c;
157 } else if (!printable(c)) {
158 if (dn + 3 >= eom) {
159 errno = EMSGSIZE;
160 return (-1);
161 }
162 *dn++ = '\\';
163 *dn++ = digits[c / 100];
164 *dn++ = digits[(c % 100) / 10];
165 *dn++ = digits[c % 10];
166 } else {
167 if (dn >= eom) {
168 errno = EMSGSIZE;
169 return (-1);
170 }
171 *dn++ = (char)c;
172 }
173 }
174 }
175 if (dn == dst) {
176 if (dn >= eom) {
177 errno = EMSGSIZE;
178 return (-1);
179 }
180 *dn++ = '.';
181 }
182 if (dn >= eom) {
183 errno = EMSGSIZE;
184 return (-1);
185 }
186 *dn++ = '\0';
187 return (dn - dst);
188 }
189
190 /*%
191 * Convert a ascii string into an encoded domain name as per RFC1035.
192 *
193 * return:
194 *
195 *\li -1 if it fails
196 *\li 1 if string was fully qualified
197 *\li 0 is string was not fully qualified
198 *
199 * notes:
200 *\li Enforces label and domain length limits.
201 */
202
203 int
204 ns_name_pton(const char *src, u_char *dst, size_t dstsiz)
205 {
206 u_char *label, *bp, *eom;
207 int c, n, escaped, e = 0;
208 char *cp;
209
210 escaped = 0;
211 bp = dst;
212 eom = dst + dstsiz;
213 label = bp++;
214
215 while ((c = *src++) != 0) {
216 if (escaped) {
217 if (c == '[') { /*%< start a bit string label */
218 if ((cp = strchr(src, ']')) == NULL) {
219 errno = EINVAL; /*%< ??? */
220 return(-1);
221 }
222 if ((e = encode_bitsring(&src, cp + 2,
223 &label, &bp, eom))
224 != 0) {
225 errno = e;
226 return(-1);
227 }
228 escaped = 0;
229 label = bp++;
230 if ((c = *src++) == 0)
231 goto done;
232 else if (c != '.') {
233 errno = EINVAL;
234 return(-1);
235 }
236 continue;
237 }
238 else if ((cp = strchr(digits, c)) != NULL) {
239 n = (cp - digits) * 100;
240 if ((c = *src++) == 0 ||
241 (cp = strchr(digits, c)) == NULL) {
242 errno = EMSGSIZE;
243 return (-1);
244 }
245 n += (cp - digits) * 10;
246 if ((c = *src++) == 0 ||
247 (cp = strchr(digits, c)) == NULL) {
248 errno = EMSGSIZE;
249 return (-1);
250 }
251 n += (cp - digits);
252 if (n > 255) {
253 errno = EMSGSIZE;
254 return (-1);
255 }
256 c = n;
257 }
258 escaped = 0;
259 } else if (c == '\\') {
260 escaped = 1;
261 continue;
262 } else if (c == '.') {
263 c = (bp - label - 1);
264 if ((c & NS_CMPRSFLGS) != 0) { /*%< Label too big. */
265 errno = EMSGSIZE;
266 return (-1);
267 }
268 if (label >= eom) {
269 errno = EMSGSIZE;
270 return (-1);
271 }
272 *label = c;
273 /* Fully qualified ? */
274 if (*src == '\0') {
275 if (c != 0) {
276 if (bp >= eom) {
277 errno = EMSGSIZE;
278 return (-1);
279 }
280 *bp++ = '\0';
281 }
282 if ((bp - dst) > MAXCDNAME) {
283 errno = EMSGSIZE;
284 return (-1);
285 }
286 return (1);
287 }
288 if (c == 0 || *src == '.') {
289 errno = EMSGSIZE;
290 return (-1);
291 }
292 label = bp++;
293 continue;
294 }
295 if (bp >= eom) {
296 errno = EMSGSIZE;
297 return (-1);
298 }
299 *bp++ = (u_char)c;
300 }
301 c = (bp - label - 1);
302 if ((c & NS_CMPRSFLGS) != 0) { /*%< Label too big. */
303 errno = EMSGSIZE;
304 return (-1);
305 }
306 done:
307 if (label >= eom) {
308 errno = EMSGSIZE;
309 return (-1);
310 }
311 *label = c;
312 if (c != 0) {
313 if (bp >= eom) {
314 errno = EMSGSIZE;
315 return (-1);
316 }
317 *bp++ = 0;
318 }
319 if ((bp - dst) > MAXCDNAME) { /*%< src too big */
320 errno = EMSGSIZE;
321 return (-1);
322 }
323 return (0);
324 }
325
326 /*%
327 * Convert a network strings labels into all lowercase.
328 *
329 * return:
330 *\li Number of bytes written to buffer, or -1 (with errno set)
331 *
332 * notes:
333 *\li Enforces label and domain length limits.
334 */
335
336 int
337 ns_name_ntol(const u_char *src, u_char *dst, size_t dstsiz)
338 {
339 const u_char *cp;
340 u_char *dn, *eom;
341 u_char c;
342 u_int n;
343 int l;
344
345 cp = src;
346 dn = dst;
347 eom = dst + dstsiz;
348
349 if (dn >= eom) {
350 errno = EMSGSIZE;
351 return (-1);
352 }
353 while ((n = *cp++) != 0) {
354 if ((n & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
355 /* Some kind of compression pointer. */
356 errno = EMSGSIZE;
357 return (-1);
358 }
359 *dn++ = n;
360 if ((l = labellen(cp - 1)) < 0) {
361 errno = EMSGSIZE;
362 return (-1);
363 }
364 if (dn + l >= eom) {
365 errno = EMSGSIZE;
366 return (-1);
367 }
368 for ((void)NULL; l > 0; l--) {
369 c = *cp++;
370 if (isupper(c))
371 *dn++ = tolower(c);
372 else
373 *dn++ = c;
374 }
375 }
376 *dn++ = '\0';
377 return (dn - dst);
378 }
379
380 /*%
381 * Unpack a domain name from a message, source may be compressed.
382 *
383 * return:
384 *\li -1 if it fails, or consumed octets if it succeeds.
385 */
386 int
387 ns_name_unpack(const u_char *msg, const u_char *eom, const u_char *src,
388 u_char *dst, size_t dstsiz)
389 {
390 const u_char *srcp, *dstlim;
391 u_char *dstp;
392 int n, len, checked, l;
393
394 len = -1;
395 checked = 0;
396 dstp = dst;
397 srcp = src;
398 dstlim = dst + dstsiz;
399 if (srcp < msg || srcp >= eom) {
400 errno = EMSGSIZE;
401 return (-1);
402 }
403 /* Fetch next label in domain name. */
404 while ((n = *srcp++) != 0) {
405 /* Check for indirection. */
406 switch (n & NS_CMPRSFLGS) {
407 case 0:
408 case NS_TYPE_ELT:
409 /* Limit checks. */
410 if ((l = labellen(srcp - 1)) < 0) {
411 errno = EMSGSIZE;
412 return(-1);
413 }
414 if (dstp + l + 1 >= dstlim || srcp + l >= eom) {
415 errno = EMSGSIZE;
416 return (-1);
417 }
418 checked += l + 1;
419 *dstp++ = n;
420 memcpy(dstp, srcp, l);
421 dstp += l;
422 srcp += l;
423 break;
424
425 case NS_CMPRSFLGS:
426 if (srcp >= eom) {
427 errno = EMSGSIZE;
428 return (-1);
429 }
430 if (len < 0)
431 len = srcp - src + 1;
432 srcp = msg + (((n & 0x3f) << 8) | (*srcp & 0xff));
433 if (srcp < msg || srcp >= eom) { /*%< Out of range. */
434 errno = EMSGSIZE;
435 return (-1);
436 }
437 checked += 2;
438 /*
439 * Check for loops in the compressed name;
440 * if we've looked at the whole message,
441 * there must be a loop.
442 */
443 if (checked >= eom - msg) {
444 errno = EMSGSIZE;
445 return (-1);
446 }
447 break;
448
449 default:
450 errno = EMSGSIZE;
451 return (-1); /*%< flag error */
452 }
453 }
454 *dstp = '\0';
455 if (len < 0)
456 len = srcp - src;
457 return (len);
458 }
459
460 /*%
461 * Pack domain name 'domain' into 'comp_dn'.
462 *
463 * return:
464 *\li Size of the compressed name, or -1.
465 *
466 * notes:
467 *\li 'dnptrs' is an array of pointers to previous compressed names.
468 *\li dnptrs[0] is a pointer to the beginning of the message. The array
469 * ends with NULL.
470 *\li 'lastdnptr' is a pointer to the end of the array pointed to
471 * by 'dnptrs'.
472 *
473 * Side effects:
474 *\li The list of pointers in dnptrs is updated for labels inserted into
475 * the message as we compress the name. If 'dnptr' is NULL, we don't
476 * try to compress names. If 'lastdnptr' is NULL, we don't update the
477 * list.
478 */
479 int
480 ns_name_pack(const u_char *src, u_char *dst, int dstsiz,
481 const u_char **dnptrs, const u_char **lastdnptr)
482 {
483 u_char *dstp;
484 const u_char **cpp, **lpp, *eob, *msg;
485 const u_char *srcp;
486 int n, l, first = 1;
487
488 srcp = src;
489 dstp = dst;
490 eob = dstp + dstsiz;
491 lpp = cpp = NULL;
492 if (dnptrs != NULL) {
493 if ((msg = *dnptrs++) != NULL) {
494 for (cpp = dnptrs; *cpp != NULL; cpp++)
495 (void)NULL;
496 lpp = cpp; /*%< end of list to search */
497 }
498 } else
499 msg = NULL;
500
501 /* make sure the domain we are about to add is legal */
502 l = 0;
503 do {
504 int l0;
505
506 n = *srcp;
507 if ((n & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
508 errno = EMSGSIZE;
509 return (-1);
510 }
511 if ((l0 = labellen(srcp)) < 0) {
512 errno = EINVAL;
513 return(-1);
514 }
515 l += l0 + 1;
516 if (l > MAXCDNAME) {
517 errno = EMSGSIZE;
518 return (-1);
519 }
520 srcp += l0 + 1;
521 } while (n != 0);
522
523 /* from here on we need to reset compression pointer array on error */
524 srcp = src;
525 do {
526 /* Look to see if we can use pointers. */
527 n = *srcp;
528 if (n != 0 && msg != NULL) {
529 l = dn_find(srcp, msg, (const u_char * const *)dnptrs,
530 (const u_char * const *)lpp);
531 if (l >= 0) {
532 if (dstp + 1 >= eob) {
533 goto cleanup;
534 }
535 *dstp++ = (l >> 8) | NS_CMPRSFLGS;
536 *dstp++ = l % 256;
537 return (dstp - dst);
538 }
539 /* Not found, save it. */
540 if (lastdnptr != NULL && cpp < lastdnptr - 1 &&
541 (dstp - msg) < 0x4000 && first) {
542 *cpp++ = dstp;
543 *cpp = NULL;
544 first = 0;
545 }
546 }
547 /* copy label to buffer */
548 if ((n & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
549 /* Should not happen. */
550 goto cleanup;
551 }
552 n = labellen(srcp);
553 if (dstp + 1 + n >= eob) {
554 goto cleanup;
555 }
556 memcpy(dstp, srcp, n + 1);
557 srcp += n + 1;
558 dstp += n + 1;
559 } while (n != 0);
560
561 if (dstp > eob) {
562 cleanup:
563 if (msg != NULL)
564 *lpp = NULL;
565 errno = EMSGSIZE;
566 return (-1);
567 }
568 return (dstp - dst);
569 }
570
571 /*%
572 * Expand compressed domain name to presentation format.
573 *
574 * return:
575 *\li Number of bytes read out of `src', or -1 (with errno set).
576 *
577 * note:
578 *\li Root domain returns as "." not "".
579 */
580 int
581 ns_name_uncompress(const u_char *msg, const u_char *eom, const u_char *src,
582 char *dst, size_t dstsiz)
583 {
584 u_char tmp[NS_MAXCDNAME];
585 int n;
586
587 if ((n = ns_name_unpack(msg, eom, src, tmp, sizeof tmp)) == -1)
588 return (-1);
589 if (ns_name_ntop(tmp, dst, dstsiz) == -1)
590 return (-1);
591 return (n);
592 }
593
594 /*%
595 * Compress a domain name into wire format, using compression pointers.
596 *
597 * return:
598 *\li Number of bytes consumed in `dst' or -1 (with errno set).
599 *
600 * notes:
601 *\li 'dnptrs' is an array of pointers to previous compressed names.
602 *\li dnptrs[0] is a pointer to the beginning of the message.
603 *\li The list ends with NULL. 'lastdnptr' is a pointer to the end of the
604 * array pointed to by 'dnptrs'. Side effect is to update the list of
605 * pointers for labels inserted into the message as we compress the name.
606 *\li If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr'
607 * is NULL, we don't update the list.
608 */
609 int
610 ns_name_compress(const char *src, u_char *dst, size_t dstsiz,
611 const u_char **dnptrs, const u_char **lastdnptr)
612 {
613 u_char tmp[NS_MAXCDNAME];
614
615 if (ns_name_pton(src, tmp, sizeof tmp) == -1)
616 return (-1);
617 return (ns_name_pack(tmp, dst, dstsiz, dnptrs, lastdnptr));
618 }
619
620 /*%
621 * Reset dnptrs so that there are no active references to pointers at or
622 * after src.
623 */
624 void
625 ns_name_rollback(const u_char *src, const u_char **dnptrs,
626 const u_char **lastdnptr)
627 {
628 while (dnptrs < lastdnptr && *dnptrs != NULL) {
629 if (*dnptrs >= src) {
630 *dnptrs = NULL;
631 break;
632 }
633 dnptrs++;
634 }
635 }
636
637 /*%
638 * Advance *ptrptr to skip over the compressed name it points at.
639 *
640 * return:
641 *\li 0 on success, -1 (with errno set) on failure.
642 */
643 int
644 ns_name_skip(const u_char **ptrptr, const u_char *eom)
645 {
646 const u_char *cp;
647 u_int n;
648 int l;
649
650 cp = *ptrptr;
651 while (cp < eom && (n = *cp++) != 0) {
652 /* Check for indirection. */
653 switch (n & NS_CMPRSFLGS) {
654 case 0: /*%< normal case, n == len */
655 cp += n;
656 continue;
657 case NS_TYPE_ELT: /*%< EDNS0 extended label */
658 if ((l = labellen(cp - 1)) < 0) {
659 errno = EMSGSIZE; /*%< XXX */
660 return(-1);
661 }
662 cp += l;
663 continue;
664 case NS_CMPRSFLGS: /*%< indirection */
665 cp++;
666 break;
667 default: /*%< illegal type */
668 errno = EMSGSIZE;
669 return (-1);
670 }
671 break;
672 }
673 if (cp > eom) {
674 errno = EMSGSIZE;
675 return (-1);
676 }
677 *ptrptr = cp;
678 return (0);
679 }
680
681 /* Private. */
682
683 /*%
684 * Thinking in noninternationalized USASCII (per the DNS spec),
685 * is this characted special ("in need of quoting") ?
686 *
687 * return:
688 *\li boolean.
689 */
690 static int
691 special(int ch) {
692 switch (ch) {
693 case 0x22: /*%< '"' */
694 case 0x2E: /*%< '.' */
695 case 0x3B: /*%< ';' */
696 case 0x5C: /*%< '\\' */
697 case 0x28: /*%< '(' */
698 case 0x29: /*%< ')' */
699 /* Special modifiers in zone files. */
700 case 0x40: /*%< '@' */
701 case 0x24: /*%< '$' */
702 return (1);
703 default:
704 return (0);
705 }
706 }
707
708 /*%
709 * Thinking in noninternationalized USASCII (per the DNS spec),
710 * is this character visible and not a space when printed ?
711 *
712 * return:
713 *\li boolean.
714 */
715 static int
716 printable(int ch) {
717 return (ch > 0x20 && ch < 0x7f);
718 }
719
720 /*%
721 * Thinking in noninternationalized USASCII (per the DNS spec),
722 * convert this character to lower case if it's upper case.
723 */
724 static int
725 mklower(int ch) {
726 if (ch >= 0x41 && ch <= 0x5A)
727 return (ch + 0x20);
728 return (ch);
729 }
730
731 /*%
732 * Search for the counted-label name in an array of compressed names.
733 *
734 * return:
735 *\li offset from msg if found, or -1.
736 *
737 * notes:
738 *\li dnptrs is the pointer to the first name on the list,
739 *\li not the pointer to the start of the message.
740 */
741 static int
742 dn_find(const u_char *domain, const u_char *msg,
743 const u_char * const *dnptrs,
744 const u_char * const *lastdnptr)
745 {
746 const u_char *dn, *cp, *sp;
747 const u_char * const *cpp;
748 u_int n;
749
750 for (cpp = dnptrs; cpp < lastdnptr; cpp++) {
751 sp = *cpp;
752 /*
753 * terminate search on:
754 * root label
755 * compression pointer
756 * unusable offset
757 */
758 while (*sp != 0 && (*sp & NS_CMPRSFLGS) == 0 &&
759 (sp - msg) < 0x4000) {
760 dn = domain;
761 cp = sp;
762 while ((n = *cp++) != 0) {
763 /*
764 * check for indirection
765 */
766 switch (n & NS_CMPRSFLGS) {
767 case 0: /*%< normal case, n == len */
768 n = labellen(cp - 1); /*%< XXX */
769 if (n != *dn++)
770 goto next;
771
772 for ((void)NULL; n > 0; n--)
773 if (mklower(*dn++) !=
774 mklower(*cp++))
775 goto next;
776 /* Is next root for both ? */
777 if (*dn == '\0' && *cp == '\0')
778 return (sp - msg);
779 if (*dn)
780 continue;
781 goto next;
782 case NS_CMPRSFLGS: /*%< indirection */
783 cp = msg + (((n & 0x3f) << 8) | *cp);
784 break;
785
786 default: /*%< illegal type */
787 errno = EMSGSIZE;
788 return (-1);
789 }
790 }
791 next: ;
792 sp += *sp + 1;
793 }
794 }
795 errno = ENOENT;
796 return (-1);
797 }
798
799 static int
800 decode_bitstring(const unsigned char **cpp, char *dn, const char *eom)
801 {
802 const unsigned char *cp = *cpp;
803 char *beg = dn, tc;
804 int b, blen, plen, i;
805
806 if ((blen = (*cp & 0xff)) == 0)
807 blen = 256;
808 plen = (blen + 3) / 4;
809 plen += sizeof("\\[x/]") + (blen > 99 ? 3 : (blen > 9) ? 2 : 1);
810 if (dn + plen >= eom)
811 return(-1);
812
813 cp++;
814 i = SPRINTF((dn, "\\[x"));
815 if (i < 0)
816 return (-1);
817 dn += i;
818 for (b = blen; b > 7; b -= 8, cp++) {
819 i = SPRINTF((dn, "%02x", *cp & 0xff));
820 if (i < 0)
821 return (-1);
822 dn += i;
823 }
824 if (b > 4) {
825 tc = *cp++;
826 i = SPRINTF((dn, "%02x", tc & (0xff << (8 - b))));
827 if (i < 0)
828 return (-1);
829 dn += i;
830 } else if (b > 0) {
831 tc = *cp++;
832 i = SPRINTF((dn, "%1x",
833 ((tc >> 4) & 0x0f) & (0x0f << (4 - b))));
834 if (i < 0)
835 return (-1);
836 dn += i;
837 }
838 i = SPRINTF((dn, "/%d]", blen));
839 if (i < 0)
840 return (-1);
841 dn += i;
842
843 *cpp = cp;
844 return(dn - beg);
845 }
846
847 static int
848 encode_bitsring(const char **bp, const char *end, unsigned char **labelp,
849 unsigned char ** dst, unsigned const char *eom)
850 {
851 int afterslash = 0;
852 const char *cp = *bp;
853 unsigned char *tp;
854 char c;
855 const char *beg_blen;
856 char *end_blen = NULL;
857 int value = 0, count = 0, tbcount = 0, blen = 0;
858
859 beg_blen = end_blen = NULL;
860
861 /* a bitstring must contain at least 2 characters */
862 if (end - cp < 2)
863 return(EINVAL);
864
865 /* XXX: currently, only hex strings are supported */
866 if (*cp++ != 'x')
867 return(EINVAL);
868 if (!isxdigit((*cp) & 0xff)) /*%< reject '\[x/BLEN]' */
869 return(EINVAL);
870
871 for (tp = *dst + 1; cp < end && tp < eom; cp++) {
872 switch((c = *cp)) {
873 case ']': /*%< end of the bitstring */
874 if (afterslash) {
875 if (beg_blen == NULL)
876 return(EINVAL);
877 blen = (int)strtol(beg_blen, &end_blen, 10);
878 if (*end_blen != ']')
879 return(EINVAL);
880 }
881 if (count)
882 *tp++ = ((value << 4) & 0xff);
883 cp++; /*%< skip ']' */
884 goto done;
885 case '/':
886 afterslash = 1;
887 break;
888 default:
889 if (afterslash) {
890 if (!isdigit(c&0xff))
891 return(EINVAL);
892 if (beg_blen == NULL) {
893
894 if (c == '0') {
895 /* blen never begings with 0 */
896 return(EINVAL);
897 }
898 beg_blen = cp;
899 }
900 } else {
901 if (!isxdigit(c&0xff))
902 return(EINVAL);
903 value <<= 4;
904 value += digitvalue[(int)c];
905 count += 4;
906 tbcount += 4;
907 if (tbcount > 256)
908 return(EINVAL);
909 if (count == 8) {
910 *tp++ = value;
911 count = 0;
912 }
913 }
914 break;
915 }
916 }
917 done:
918 if (cp >= end || tp >= eom)
919 return(EMSGSIZE);
920
921 /*
922 * bit length validation:
923 * If a <length> is present, the number of digits in the <bit-data>
924 * MUST be just sufficient to contain the number of bits specified
925 * by the <length>. If there are insignificant bits in a final
926 * hexadecimal or octal digit, they MUST be zero.
927 * RFC2673, Section 3.2.
928 */
929 if (blen > 0) {
930 int traillen;
931
932 if (((blen + 3) & ~3) != tbcount)
933 return(EINVAL);
934 traillen = tbcount - blen; /*%< between 0 and 3 */
935 if (((value << (8 - traillen)) & 0xff) != 0)
936 return(EINVAL);
937 }
938 else
939 blen = tbcount;
940 if (blen == 256)
941 blen = 0;
942
943 /* encode the type and the significant bit fields */
944 **labelp = DNS_LABELTYPE_BITSTRING;
945 **dst = blen;
946
947 *bp = cp;
948 *dst = tp;
949
950 return(0);
951 }
952
953 static int
954 labellen(const u_char *lp)
955 {
956 int bitlen;
957 u_char l = *lp;
958
959 if ((l & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
960 /* should be avoided by the caller */
961 return(-1);
962 }
963
964 if ((l & NS_CMPRSFLGS) == NS_TYPE_ELT) {
965 if (l == DNS_LABELTYPE_BITSTRING) {
966 if ((bitlen = *(lp + 1)) == 0)
967 bitlen = 256;
968 return((bitlen + 7 ) / 8 + 1);
969 }
970 return(-1); /*%< unknwon ELT */
971 }
972 return(l);
973 }
974
975 /*! \file */
976