19static const char rcsid[] =
"$Id: ns_name.c,v 1.3.2.4 2003/07/02 04:10:27 marka Exp $";
31#include <netinet/in.h>
51# define SPRINTF(x) strlen(sprintfx)
53# define SPRINTF(x) ((size_t)sprintf x)
56#define NS_TYPE_ELT 0x40
57#define DNS_LABELTYPE_BITSTRING 0x41
61static const char digits[] =
"0123456789";
63static const char digitvalue[256] = {
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 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
68 -1, 10, 11, 12, 13, 14, 15, -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,
70 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
71 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
72 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
73 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
74 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
75 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
76 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
77 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
78 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
79 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
84static int special(
int);
85static int printable(
int);
86static int dn_find(
const u_char *,
const u_char *,
87 const u_char *
const *,
88 const u_char *
const *);
89static int encode_bitsring(
const char **,
const char *,
90 char **,
char **,
const char *);
91static int labellen(
const u_char *);
92static int decode_bitstring(
const char **,
char *,
const char *);
118 while ((n = *cp++) != 0) {
131 if ((l = labellen(cp - 1)) < 0) {
147 if ((m = decode_bitstring((
const char **)&cp, dn, eom)) < 0)
155 for ((
void)
NULL; l > 0; l--) {
164 }
else if (!printable(c)) {
170 *dn++ = digits[c / 100];
171 *dn++ = digits[(c % 100) / 10];
172 *dn++ = digits[c % 10];
211 u_char *label, *bp, *eom;
212 int c, n, escaped, e = 0;
220 while ((c = *src++) != 0) {
223 if ((cp = strchr(src,
']')) ==
NULL) {
227 if ((e = encode_bitsring(&src,
238 if ((c = *src++) == 0)
246 else if ((cp = strchr(digits, c)) !=
NULL) {
247 n = (cp - digits) * 100;
248 if ((c = *src++) == 0 ||
249 (cp = strchr(digits, c)) ==
NULL) {
253 n += (cp - digits) * 10;
254 if ((c = *src++) == 0 ||
255 (cp = strchr(digits, c)) ==
NULL) {
267 }
else if (c ==
'\\') {
270 }
else if (c ==
'.') {
271 c = (bp - label - 1);
296 if (c == 0 || *src ==
'.') {
309 c = (bp - label - 1);
360 while ((n = *cp++) != 0) {
367 if ((l = labellen(cp - 1)) < 0) {
375 for ((
void)
NULL; l > 0; l--) {
395 u_char *dst,
size_t dstsiz)
397 const u_char *srcp, *dstlim;
399 int n, len, checked, l;
405 dstlim = dst + dstsiz;
406 if (srcp < msg || srcp >= eom) {
411 while ((n = *srcp++) != 0) {
417 if ((l = labellen(srcp - 1)) < 0) {
421 if (dstp + l + 1 >= dstlim || srcp + l >= eom) {
427 memcpy(dstp, srcp, l);
438 len = srcp - src + 1;
439 srcp = msg + (((n & 0x3f) << 8) | (*srcp & 0xff));
440 if (srcp < msg || srcp >= eom) {
450 if (checked >= eom - msg) {
486 const u_char **dnptrs,
const u_char **lastdnptr)
489 const u_char **cpp, **lpp, *eob, *msg;
497 if (dnptrs !=
NULL) {
498 if ((msg = *dnptrs++) !=
NULL) {
499 for (cpp = dnptrs; *cpp !=
NULL; cpp++)
516 if ((l0 = labellen(srcp)) < 0) {
533 if (n != 0 && msg !=
NULL) {
534 l = dn_find(srcp, msg, (
const u_char *
const *)dnptrs,
535 (
const u_char *
const *)lpp);
537 if (dstp + 1 >= eob) {
545 if (lastdnptr !=
NULL && cpp < lastdnptr - 1 &&
546 (dstp - msg) < 0x4000 && first) {
558 if (dstp + 1 + n >= eob) {
561 memcpy(dstp, srcp, n + 1);
586 char *dst,
size_t dstsiz)
614 const u_char **dnptrs,
const u_char **lastdnptr)
620 return (
ns_name_pack(tmp, dst, dstsiz, dnptrs, lastdnptr));
629 const u_char **lastdnptr)
631 while (dnptrs < lastdnptr && *dnptrs !=
NULL) {
632 if (*dnptrs >= src) {
654 while (cp < eom && (n = *cp++) != 0) {
661 if ((l = labellen(cp - 1)) < 0) {
720 return (ch > 0x20 && ch < 0x7f);
729 if (ch >= 0x41 && ch <= 0x5A)
744dn_find(
const u_char *domain,
const u_char *msg,
745 const u_char *
const *dnptrs,
746 const u_char *
const *lastdnptr)
748 const u_char *dn, *cp, *sp;
749 const u_char *
const *cpp;
752 for (cpp = dnptrs; cpp < lastdnptr; cpp++) {
761 (sp - msg) < 0x4000) {
764 while ((n = *cp++) != 0) {
770 n = labellen(cp - 1);
775 for ((
void)
NULL; n > 0; n--)
776 if (mklower(*dn++) !=
780 if (*dn ==
'\0' && *cp ==
'\0')
786 cp = msg + (((n & 0x3f) << 8) | *cp);
803decode_bitstring(
const char **cpp,
char *dn,
const char *eom)
805 const char *cp = *cpp;
807 int b, blen, plen, i;
809 if ((blen = (*cp & 0xff)) == 0)
811 plen = (blen + 3) / 4;
812 plen +=
sizeof(
"\\[x/]") + (blen > 99 ? 3 : (blen > 9) ? 2 : 1);
813 if (dn + plen >= eom)
821 for (b = blen; b > 7; b -= 8, cp++) {
822 i =
SPRINTF((dn,
"%02x", *cp & 0xff));
829 i =
SPRINTF((dn,
"%02x", tc & (0xff << (8 - b))));
836 ((tc >> 4) & 0x0f) & (0x0f << (4 - b))));
841 i =
SPRINTF((dn,
"/%d]", blen));
851encode_bitsring(
const char **bp,
const char *end,
char **labelp,
852 char ** dst,
const char *eom)
855 const char *cp = *bp;
857 const char *beg_blen;
858 char *end_blen =
NULL;
859 int value = 0, count = 0, tbcount = 0, blen = 0;
861 beg_blen = end_blen =
NULL;
870 if (!isxdigit((*cp) & 0xff))
873 for (tp = *dst + 1; cp < end && tp < eom; cp++) {
877 if (beg_blen ==
NULL)
879 blen = (int)strtol(beg_blen, &end_blen, 10);
880 if (*end_blen !=
']')
884 *tp++ = ((value << 4) & 0xff);
892 if (!isdigit(c&0xff))
894 if (beg_blen ==
NULL) {
903 if (!isxdigit(c&0xff))
906 value += digitvalue[(int)c];
920 if (cp >= end || tp >= eom)
934 if (((blen + 3) & ~3) != tbcount)
936 traillen = tbcount - blen;
937 if (((value << (8 - traillen)) & 0xff) != 0)
956labellen(
const u_char *lp)
968 if ((bitlen = *(lp + 1)) == 0)
970 return((bitlen + 7 ) / 8 + 1);
#define DNS_LABELTYPE_BITSTRING
#define ns_name_uncompress