diff options
author | Sebastian Huber <sebastian.huber@embedded-brains.de> | 2015-04-08 15:37:49 +0200 |
---|---|---|
committer | Sebastian Huber <sebastian.huber@embedded-brains.de> | 2015-05-20 09:58:21 +0200 |
commit | 165dd8ea1256d71d6a4f52c0a66dcc2799ef8f99 (patch) | |
tree | c42072a99573d4d7cc03b2263e2c0c6d53f9c691 /freebsd/lib/libc/nameser/ns_name.c | |
parent | rtems-bsd-chunk: Include missing header file (diff) | |
download | rtems-libbsd-165dd8ea1256d71d6a4f52c0a66dcc2799ef8f99.tar.bz2 |
Update to FreeBSD Stable/9 2015-04-08
Diffstat (limited to 'freebsd/lib/libc/nameser/ns_name.c')
-rw-r--r-- | freebsd/lib/libc/nameser/ns_name.c | 255 |
1 files changed, 219 insertions, 36 deletions
diff --git a/freebsd/lib/libc/nameser/ns_name.c b/freebsd/lib/libc/nameser/ns_name.c index 181a9756..fa1a32e3 100644 --- a/freebsd/lib/libc/nameser/ns_name.c +++ b/freebsd/lib/libc/nameser/ns_name.c @@ -18,8 +18,10 @@ */ #ifndef lint -static const char rcsid[] = "$Id: ns_name.c,v 1.8.18.2 2005/04/27 05:01:08 sra Exp $"; +static const char rcsid[] = "$Id: ns_name.c,v 1.11 2009/01/23 19:59:16 each Exp $"; #endif +#include <sys/cdefs.h> +__FBSDID("$FreeBSD$"); #include "port_before.h" @@ -123,7 +125,7 @@ ns_name_ntop(const u_char *src, char *dst, size_t dstsiz) } if ((l = labellen(cp - 1)) < 0) { errno = EMSGSIZE; /*%< XXX */ - return(-1); + return (-1); } if (dn + l >= eom) { errno = EMSGSIZE; @@ -135,12 +137,12 @@ ns_name_ntop(const u_char *src, char *dst, size_t dstsiz) if (n != DNS_LABELTYPE_BITSTRING) { /* XXX: labellen should reject this case */ errno = EINVAL; - return(-1); + return (-1); } if ((m = decode_bitstring(&cp, dn, eom)) < 0) { errno = EMSGSIZE; - return(-1); + return (-1); } dn += m; continue; @@ -199,10 +201,25 @@ ns_name_ntop(const u_char *src, char *dst, size_t dstsiz) * notes: *\li Enforces label and domain length limits. */ +int +ns_name_pton(const char *src, u_char *dst, size_t dstsiz) { + return (ns_name_pton2(src, dst, dstsiz, NULL)); +} +/* + * ns_name_pton2(src, dst, dstsiz, *dstlen) + * Convert a ascii string into an encoded domain name as per RFC1035. + * return: + * -1 if it fails + * 1 if string was fully qualified + * 0 is string was not fully qualified + * side effects: + * fills in *dstlen (if non-NULL) + * notes: + * Enforces label and domain length limits. + */ int -ns_name_pton(const char *src, u_char *dst, size_t dstsiz) -{ +ns_name_pton2(const char *src, u_char *dst, size_t dstsiz, size_t *dstlen) { u_char *label, *bp, *eom; int c, n, escaped, e = 0; char *cp; @@ -217,13 +234,13 @@ ns_name_pton(const char *src, u_char *dst, size_t dstsiz) if (c == '[') { /*%< start a bit string label */ if ((cp = strchr(src, ']')) == NULL) { errno = EINVAL; /*%< ??? */ - return(-1); + return (-1); } if ((e = encode_bitsring(&src, cp + 2, &label, &bp, eom)) != 0) { errno = e; - return(-1); + return (-1); } escaped = 0; label = bp++; @@ -231,7 +248,7 @@ ns_name_pton(const char *src, u_char *dst, size_t dstsiz) goto done; else if (c != '.') { errno = EINVAL; - return(-1); + return (-1); } continue; } @@ -283,6 +300,8 @@ ns_name_pton(const char *src, u_char *dst, size_t dstsiz) errno = EMSGSIZE; return (-1); } + if (dstlen != NULL) + *dstlen = (bp - dst); return (1); } if (c == 0 || *src == '.') { @@ -320,6 +339,8 @@ ns_name_pton(const char *src, u_char *dst, size_t dstsiz) errno = EMSGSIZE; return (-1); } + if (dstlen != NULL) + *dstlen = (bp - dst); return (0); } @@ -367,7 +388,7 @@ ns_name_ntol(const u_char *src, u_char *dst, size_t dstsiz) } for ((void)NULL; l > 0; l--) { c = *cp++; - if (isupper(c)) + if (isascii(c) && isupper(c)) *dn++ = tolower(c); else *dn++ = c; @@ -387,6 +408,21 @@ int ns_name_unpack(const u_char *msg, const u_char *eom, const u_char *src, u_char *dst, size_t dstsiz) { + return (ns_name_unpack2(msg, eom, src, dst, dstsiz, NULL)); +} + +/* + * ns_name_unpack2(msg, eom, src, dst, dstsiz, *dstlen) + * Unpack a domain name from a message, source may be compressed. + * return: + * -1 if it fails, or consumed octets if it succeeds. + * side effect: + * fills in *dstlen (if non-NULL). + */ +int +ns_name_unpack2(const u_char *msg, const u_char *eom, const u_char *src, + u_char *dst, size_t dstsiz, size_t *dstlen) +{ const u_char *srcp, *dstlim; u_char *dstp; int n, len, checked, l; @@ -409,7 +445,7 @@ ns_name_unpack(const u_char *msg, const u_char *eom, const u_char *src, /* Limit checks. */ if ((l = labellen(srcp - 1)) < 0) { errno = EMSGSIZE; - return(-1); + return (-1); } if (dstp + l + 1 >= dstlim || srcp + l >= eom) { errno = EMSGSIZE; @@ -429,11 +465,12 @@ ns_name_unpack(const u_char *msg, const u_char *eom, const u_char *src, } if (len < 0) len = srcp - src + 1; - srcp = msg + (((n & 0x3f) << 8) | (*srcp & 0xff)); - if (srcp < msg || srcp >= eom) { /*%< Out of range. */ + l = ((n & 0x3f) << 8) | (*srcp & 0xff); + if (l >= eom - msg) { /*%< Out of range. */ errno = EMSGSIZE; return (-1); } + srcp = msg + l; checked += 2; /* * Check for loops in the compressed name; @@ -451,7 +488,9 @@ ns_name_unpack(const u_char *msg, const u_char *eom, const u_char *src, return (-1); /*%< flag error */ } } - *dstp = '\0'; + *dstp++ = 0; + if (dstlen != NULL) + *dstlen = dstp - dst; if (len < 0) len = srcp - src; return (len); @@ -510,7 +549,7 @@ ns_name_pack(const u_char *src, u_char *dst, int dstsiz, } if ((l0 = labellen(srcp)) < 0) { errno = EINVAL; - return(-1); + return (-1); } l += l0 + 1; if (l > MAXCDNAME) { @@ -657,7 +696,7 @@ ns_name_skip(const u_char **ptrptr, const u_char *eom) case NS_TYPE_ELT: /*%< EDNS0 extended label */ if ((l = labellen(cp - 1)) < 0) { errno = EMSGSIZE; /*%< XXX */ - return(-1); + return (-1); } cp += l; continue; @@ -678,6 +717,150 @@ ns_name_skip(const u_char **ptrptr, const u_char *eom) return (0); } +/* Find the number of octets an nname takes up, including the root label. + * (This is basically ns_name_skip() without compression-pointer support.) + * ((NOTE: can only return zero if passed-in namesiz argument is zero.)) + */ +ssize_t +ns_name_length(ns_nname_ct nname, size_t namesiz) { + ns_nname_ct orig = nname; + u_int n; + + while (namesiz-- > 0 && (n = *nname++) != 0) { + if ((n & NS_CMPRSFLGS) != 0) { + errno = EISDIR; + return (-1); + } + if (n > namesiz) { + errno = EMSGSIZE; + return (-1); + } + nname += n; + namesiz -= n; + } + return (nname - orig); +} + +/* Compare two nname's for equality. Return -1 on error (setting errno). + */ +int +ns_name_eq(ns_nname_ct a, size_t as, ns_nname_ct b, size_t bs) { + ns_nname_ct ae = a + as, be = b + bs; + int ac, bc; + + while (ac = *a, bc = *b, ac != 0 && bc != 0) { + if ((ac & NS_CMPRSFLGS) != 0 || (bc & NS_CMPRSFLGS) != 0) { + errno = EISDIR; + return (-1); + } + if (a + ac >= ae || b + bc >= be) { + errno = EMSGSIZE; + return (-1); + } + if (ac != bc || strncasecmp((const char *) ++a, + (const char *) ++b, ac) != 0) + return (0); + a += ac, b += bc; + } + return (ac == 0 && bc == 0); +} + +/* Is domain "A" owned by (at or below) domain "B"? + */ +int +ns_name_owned(ns_namemap_ct a, int an, ns_namemap_ct b, int bn) { + /* If A is shorter, it cannot be owned by B. */ + if (an < bn) + return (0); + + /* If they are unequal before the length of the shorter, A cannot... */ + while (bn > 0) { + if (a->len != b->len || + strncasecmp((const char *) a->base, + (const char *) b->base, a->len) != 0) + return (0); + a++, an--; + b++, bn--; + } + + /* A might be longer or not, but either way, B owns it. */ + return (1); +} + +/* Build an array of <base,len> tuples from an nname, top-down order. + * Return the number of tuples (labels) thus discovered. + */ +int +ns_name_map(ns_nname_ct nname, size_t namelen, ns_namemap_t map, int mapsize) { + u_int n; + int l; + + n = *nname++; + namelen--; + + /* Root zone? */ + if (n == 0) { + /* Extra data follows name? */ + if (namelen > 0) { + errno = EMSGSIZE; + return (-1); + } + return (0); + } + + /* Compression pointer? */ + if ((n & NS_CMPRSFLGS) != 0) { + errno = EISDIR; + return (-1); + } + + /* Label too long? */ + if (n > namelen) { + errno = EMSGSIZE; + return (-1); + } + + /* Recurse to get rest of name done first. */ + l = ns_name_map(nname + n, namelen - n, map, mapsize); + if (l < 0) + return (-1); + + /* Too many labels? */ + if (l >= mapsize) { + errno = ENAMETOOLONG; + return (-1); + } + + /* We're on our way back up-stack, store current map data. */ + map[l].base = nname; + map[l].len = n; + return (l + 1); +} + +/* Count the labels in a domain name. Root counts, so COM. has two. This + * is to make the result comparable to the result of ns_name_map(). + */ +int +ns_name_labels(ns_nname_ct nname, size_t namesiz) { + int ret = 0; + u_int n; + + while (namesiz-- > 0 && (n = *nname++) != 0) { + if ((n & NS_CMPRSFLGS) != 0) { + errno = EISDIR; + return (-1); + } + if (n > namesiz) { + errno = EMSGSIZE; + return (-1); + } + nname += n; + namesiz -= n; + ret++; + } + return (ret + 1); +} + /* Private. */ /*% @@ -808,7 +991,7 @@ decode_bitstring(const unsigned char **cpp, char *dn, const char *eom) plen = (blen + 3) / 4; plen += sizeof("\\[x/]") + (blen > 99 ? 3 : (blen > 9) ? 2 : 1); if (dn + plen >= eom) - return(-1); + return (-1); cp++; i = SPRINTF((dn, "\\[x")); @@ -841,12 +1024,12 @@ decode_bitstring(const unsigned char **cpp, char *dn, const char *eom) dn += i; *cpp = cp; - return(dn - beg); + return (dn - beg); } static int encode_bitsring(const char **bp, const char *end, unsigned char **labelp, - unsigned char ** dst, unsigned const char *eom) + unsigned char ** dst, unsigned const char *eom) { int afterslash = 0; const char *cp = *bp; @@ -860,23 +1043,23 @@ encode_bitsring(const char **bp, const char *end, unsigned char **labelp, /* a bitstring must contain at least 2 characters */ if (end - cp < 2) - return(EINVAL); + return (EINVAL); /* XXX: currently, only hex strings are supported */ if (*cp++ != 'x') - return(EINVAL); + return (EINVAL); if (!isxdigit((*cp) & 0xff)) /*%< reject '\[x/BLEN]' */ - return(EINVAL); + return (EINVAL); for (tp = *dst + 1; cp < end && tp < eom; cp++) { switch((c = *cp)) { case ']': /*%< end of the bitstring */ if (afterslash) { if (beg_blen == NULL) - return(EINVAL); + return (EINVAL); blen = (int)strtol(beg_blen, &end_blen, 10); if (*end_blen != ']') - return(EINVAL); + return (EINVAL); } if (count) *tp++ = ((value << 4) & 0xff); @@ -888,24 +1071,24 @@ encode_bitsring(const char **bp, const char *end, unsigned char **labelp, default: if (afterslash) { if (!isdigit(c&0xff)) - return(EINVAL); + return (EINVAL); if (beg_blen == NULL) { if (c == '0') { /* blen never begings with 0 */ - return(EINVAL); + return (EINVAL); } beg_blen = cp; } } else { if (!isxdigit(c&0xff)) - return(EINVAL); + return (EINVAL); value <<= 4; value += digitvalue[(int)c]; count += 4; tbcount += 4; if (tbcount > 256) - return(EINVAL); + return (EINVAL); if (count == 8) { *tp++ = value; count = 0; @@ -916,7 +1099,7 @@ encode_bitsring(const char **bp, const char *end, unsigned char **labelp, } done: if (cp >= end || tp >= eom) - return(EMSGSIZE); + return (EMSGSIZE); /* * bit length validation: @@ -930,10 +1113,10 @@ encode_bitsring(const char **bp, const char *end, unsigned char **labelp, int traillen; if (((blen + 3) & ~3) != tbcount) - return(EINVAL); + return (EINVAL); traillen = tbcount - blen; /*%< between 0 and 3 */ if (((value << (8 - traillen)) & 0xff) != 0) - return(EINVAL); + return (EINVAL); } else blen = tbcount; @@ -947,7 +1130,7 @@ encode_bitsring(const char **bp, const char *end, unsigned char **labelp, *bp = cp; *dst = tp; - return(0); + return (0); } static int @@ -958,18 +1141,18 @@ labellen(const u_char *lp) if ((l & NS_CMPRSFLGS) == NS_CMPRSFLGS) { /* should be avoided by the caller */ - return(-1); + return (-1); } if ((l & NS_CMPRSFLGS) == NS_TYPE_ELT) { if (l == DNS_LABELTYPE_BITSTRING) { if ((bitlen = *(lp + 1)) == 0) bitlen = 256; - return((bitlen + 7 ) / 8 + 1); + return ((bitlen + 7 ) / 8 + 1); } - return(-1); /*%< unknwon ELT */ + return (-1); /*%< unknwon ELT */ } - return(l); + return (l); } /*! \file */ |