Welcome to mirror list, hosted at ThFree Co, Russian Federation.

cygwin.com/git/newlib-cygwin.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'newlib/libc/sys/linux/net/ns_name.c')
-rw-r--r--newlib/libc/sys/linux/net/ns_name.c320
1 files changed, 82 insertions, 238 deletions
diff --git a/newlib/libc/sys/linux/net/ns_name.c b/newlib/libc/sys/linux/net/ns_name.c
index d6c4cc8b5..609df14f5 100644
--- a/newlib/libc/sys/linux/net/ns_name.c
+++ b/newlib/libc/sys/linux/net/ns_name.c
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 1996,1999 by Internet Software Consortium.
+ * Copyright (c) 1996 by Internet Software Consortium.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
@@ -15,25 +15,21 @@
* SOFTWARE.
*/
-#if !defined(_LIBC) && !defined(lint)
-static const char rcsid[] = "$BINDId: ns_name.c,v 8.15 2000/03/30 22:53:46 vixie Exp $";
-#endif
+#include <sys/cdefs.h>
+#include <sys/types.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/nameser.h>
-#include <ctype.h>
#include <errno.h>
#include <resolv.h>
#include <string.h>
-#include <ctype.h>
-#include "libc-symbols.h"
/* Data. */
-static const char digits[] = "0123456789";
+static char digits[] = "0123456789";
/* Forward. */
@@ -66,57 +62,34 @@ ns_name_ntop(const u_char *src, char *dst, size_t dstsiz) {
eom = dst + dstsiz;
while ((n = *cp++) != 0) {
- if ((n & NS_CMPRSFLGS) != 0 && n != 0x41) {
+ if ((n & NS_CMPRSFLGS) != 0) {
/* Some kind of compression pointer. */
- __set_errno (EMSGSIZE);
+ errno = EMSGSIZE;
return (-1);
}
if (dn != dst) {
if (dn >= eom) {
- __set_errno (EMSGSIZE);
+ errno = EMSGSIZE;
return (-1);
}
*dn++ = '.';
}
-
- if (n == 0x41) {
- n = *cp++ / 8;
- if (dn + n * 2 + 4 >= eom) {
- __set_errno (EMSGSIZE);
- return (-1);
- }
- *dn++ = '\\';
- *dn++ = '[';
- *dn++ = 'x';
-
- while (n-- > 0) {
- c = *cp++;
- unsigned u = c >> 4;
- *dn++ = u > 9 ? 'a' + u - 10 : '0' + u;
- u = c & 0xf;
- *dn++ = u > 9 ? 'a' + u - 10 : '0' + u;
- }
-
- *dn++ = ']';
- continue;
- }
-
if (dn + n >= eom) {
- __set_errno (EMSGSIZE);
+ errno = EMSGSIZE;
return (-1);
}
for ((void)NULL; n > 0; n--) {
c = *cp++;
if (special(c)) {
if (dn + 1 >= eom) {
- __set_errno (EMSGSIZE);
+ errno = EMSGSIZE;
return (-1);
}
*dn++ = '\\';
*dn++ = (char)c;
} else if (!printable(c)) {
if (dn + 3 >= eom) {
- __set_errno (EMSGSIZE);
+ errno = EMSGSIZE;
return (-1);
}
*dn++ = '\\';
@@ -125,7 +98,7 @@ ns_name_ntop(const u_char *src, char *dst, size_t dstsiz) {
*dn++ = digits[c % 10];
} else {
if (dn >= eom) {
- __set_errno (EMSGSIZE);
+ errno = EMSGSIZE;
return (-1);
}
*dn++ = (char)c;
@@ -134,19 +107,18 @@ ns_name_ntop(const u_char *src, char *dst, size_t dstsiz) {
}
if (dn == dst) {
if (dn >= eom) {
- __set_errno (EMSGSIZE);
+ errno = EMSGSIZE;
return (-1);
}
*dn++ = '.';
}
if (dn >= eom) {
- __set_errno (EMSGSIZE);
+ errno = EMSGSIZE;
return (-1);
}
*dn++ = '\0';
return (dn - dst);
}
-libresolv_hidden_def (ns_name_ntop)
/*
* ns_name_pton(src, dst, dstsiz)
@@ -176,56 +148,21 @@ ns_name_pton(const char *src, u_char *dst, size_t dstsiz) {
n = (cp - digits) * 100;
if ((c = *src++) == 0 ||
(cp = strchr(digits, c)) == NULL) {
- __set_errno (EMSGSIZE);
+ errno = EMSGSIZE;
return (-1);
}
n += (cp - digits) * 10;
if ((c = *src++) == 0 ||
(cp = strchr(digits, c)) == NULL) {
- __set_errno (EMSGSIZE);
+ errno = EMSGSIZE;
return (-1);
}
n += (cp - digits);
if (n > 255) {
- __set_errno (EMSGSIZE);
+ errno = EMSGSIZE;
return (-1);
}
c = n;
- } else if (c == '[' && label == bp - 1 && *src == 'x') {
- /* Theoretically we would have to handle \[o
- as well but we do not since we do not need
- it internally. */
- *label = 0x41;
- label = bp++;
- ++src;
- while (isxdigit (*src)) {
- n = *src > '9' ? *src - 'a' + 10 : *src - '0';
- ++src;
- if (! isxdigit(*src)) {
- __set_errno (EMSGSIZE);
- return (-1);
- }
- n <<= 4;
- n += *src > '9' ? *src - 'a' + 10 : *src - '0';
- if (bp + 1 >= eom) {
- __set_errno (EMSGSIZE);
- return (-1);
- }
- *bp++ = n;
- ++src;
- }
- *label = (bp - label - 1) * 8;
- if (*src++ != ']' || *src++ != '.') {
- __set_errno (EMSGSIZE);
- return (-1);
- }
- escaped = 0;
- label = bp++;
- if (bp >= eom) {
- __set_errno (EMSGSIZE);
- return (-1);
- }
- continue;
}
escaped = 0;
} else if (c == '\\') {
@@ -234,11 +171,11 @@ ns_name_pton(const char *src, u_char *dst, size_t dstsiz) {
} else if (c == '.') {
c = (bp - label - 1);
if ((c & NS_CMPRSFLGS) != 0) { /* Label too big. */
- __set_errno (EMSGSIZE);
+ errno = EMSGSIZE;
return (-1);
}
if (label >= eom) {
- __set_errno (EMSGSIZE);
+ errno = EMSGSIZE;
return (-1);
}
*label = c;
@@ -246,98 +183,55 @@ ns_name_pton(const char *src, u_char *dst, size_t dstsiz) {
if (*src == '\0') {
if (c != 0) {
if (bp >= eom) {
- __set_errno (EMSGSIZE);
+ errno = EMSGSIZE;
return (-1);
}
*bp++ = '\0';
}
if ((bp - dst) > MAXCDNAME) {
- __set_errno (EMSGSIZE);
+ errno = EMSGSIZE;
return (-1);
}
return (1);
}
- if (c == 0 || *src == '.') {
- __set_errno (EMSGSIZE);
+ if (c == 0) {
+ errno = EMSGSIZE;
return (-1);
}
label = bp++;
continue;
}
if (bp >= eom) {
- __set_errno (EMSGSIZE);
+ errno = EMSGSIZE;
return (-1);
}
*bp++ = (u_char)c;
}
c = (bp - label - 1);
if ((c & NS_CMPRSFLGS) != 0) { /* Label too big. */
- __set_errno (EMSGSIZE);
+ errno = EMSGSIZE;
return (-1);
}
if (label >= eom) {
- __set_errno (EMSGSIZE);
+ errno = EMSGSIZE;
return (-1);
}
*label = c;
if (c != 0) {
if (bp >= eom) {
- __set_errno (EMSGSIZE);
+ errno = EMSGSIZE;
return (-1);
}
*bp++ = 0;
}
if ((bp - dst) > MAXCDNAME) { /* src too big */
- __set_errno (EMSGSIZE);
+ errno = EMSGSIZE;
return (-1);
}
return (0);
}
/*
- * ns_name_ntol(src, dst, dstsiz)
- * Convert a network strings labels into all lowercase.
- * return:
- * Number of bytes written to buffer, or -1 (with errno set)
- * notes:
- * Enforces label and domain length limits.
- */
-
-int
-ns_name_ntol(const u_char *src, u_char *dst, size_t dstsiz) {
- const u_char *cp;
- u_char *dn, *eom;
- u_char c;
- u_int n;
-
- cp = src;
- dn = dst;
- eom = dst + dstsiz;
-
- while ((n = *cp++) != 0) {
- if ((n & NS_CMPRSFLGS) != 0) {
- /* Some kind of compression pointer. */
- __set_errno (EMSGSIZE);
- return (-1);
- }
- *dn++ = n;
- if (dn + n >= eom) {
- __set_errno (EMSGSIZE);
- return (-1);
- }
- for ((void)NULL; n > 0; n--) {
- c = *cp++;
- if (isupper(c))
- *dn++ = tolower(c);
- else
- *dn++ = c;
- }
- }
- *dn++ = '\0';
- return (dn - dst);
-}
-
-/*
* ns_name_unpack(msg, eom, src, dst, dstsiz)
* Unpack a domain name from a message, source may be compressed.
* return:
@@ -357,48 +251,36 @@ ns_name_unpack(const u_char *msg, const u_char *eom, const u_char *src,
srcp = src;
dstlim = dst + dstsiz;
if (srcp < msg || srcp >= eom) {
- __set_errno (EMSGSIZE);
+ errno = EMSGSIZE;
return (-1);
}
/* Fetch next label in domain name. */
while ((n = *srcp++) != 0) {
/* Check for indirection. */
switch (n & NS_CMPRSFLGS) {
- case 0x40:
- if (n == 0x41) {
- if (dstp + 1 >= dstlim) {
- __set_errno (EMSGSIZE);
- return (-1);
- }
- *dstp++ = 0x41;
- n = *srcp++ / 8;
- ++checked;
- } else {
- __set_errno (EMSGSIZE);
- return (-1); /* flag error */
- }
- /* FALLTHROUGH */
case 0:
/* Limit checks. */
if (dstp + n + 1 >= dstlim || srcp + n >= eom) {
- __set_errno (EMSGSIZE);
+ errno = EMSGSIZE;
return (-1);
}
checked += n + 1;
- dstp = mempcpy(dstp, srcp - 1, n + 1);
+ *dstp++ = n;
+ memcpy(dstp, srcp, n);
+ dstp += n;
srcp += n;
break;
case NS_CMPRSFLGS:
if (srcp >= eom) {
- __set_errno (EMSGSIZE);
+ errno = EMSGSIZE;
return (-1);
}
if (len < 0)
len = srcp - src + 1;
srcp = msg + (((n & 0x3f) << 8) | (*srcp & 0xff));
if (srcp < msg || srcp >= eom) { /* Out of range. */
- __set_errno (EMSGSIZE);
+ errno = EMSGSIZE;
return (-1);
}
checked += 2;
@@ -408,13 +290,13 @@ ns_name_unpack(const u_char *msg, const u_char *eom, const u_char *src,
* there must be a loop.
*/
if (checked >= eom - msg) {
- __set_errno (EMSGSIZE);
+ errno = EMSGSIZE;
return (-1);
}
break;
default:
- __set_errno (EMSGSIZE);
+ errno = EMSGSIZE;
return (-1); /* flag error */
}
}
@@ -423,7 +305,6 @@ ns_name_unpack(const u_char *msg, const u_char *eom, const u_char *src,
len = srcp - src;
return (len);
}
-libresolv_hidden_def (ns_name_unpack)
/*
* ns_name_pack(src, dst, dstsiz, dnptrs, lastdnptr)
@@ -449,7 +330,7 @@ ns_name_pack(const u_char *src, u_char *dst, int dstsiz,
u_char *dstp;
const u_char **cpp, **lpp, *eob, *msg;
const u_char *srcp;
- int n, l, first = 1;
+ int n, l;
srcp = src;
dstp = dst;
@@ -468,31 +349,29 @@ ns_name_pack(const u_char *src, u_char *dst, int dstsiz,
l = 0;
do {
n = *srcp;
- if ((n & NS_CMPRSFLGS) != 0 && n != 0x41) {
- __set_errno (EMSGSIZE);
+ if ((n & NS_CMPRSFLGS) != 0) {
+ errno = EMSGSIZE;
return (-1);
}
- if (n == 0x41)
- n = *++srcp / 8;
l += n + 1;
if (l > MAXCDNAME) {
- __set_errno (EMSGSIZE);
+ errno = EMSGSIZE;
return (-1);
}
srcp += n + 1;
} while (n != 0);
- /* from here on we need to reset compression pointer array on error */
srcp = src;
do {
/* Look to see if we can use pointers. */
n = *srcp;
- if (n != 0 && n != 0x41 && msg != NULL) {
+ if (n != 0 && msg != NULL) {
l = dn_find(srcp, msg, (const u_char * const *)dnptrs,
(const u_char * const *)lpp);
if (l >= 0) {
if (dstp + 1 >= eob) {
- goto cleanup;
+ errno = EMSGSIZE;
+ return (-1);
}
*dstp++ = (l >> 8) | NS_CMPRSFLGS;
*dstp++ = l % 256;
@@ -500,24 +379,19 @@ ns_name_pack(const u_char *src, u_char *dst, int dstsiz,
}
/* Not found, save it. */
if (lastdnptr != NULL && cpp < lastdnptr - 1 &&
- (dstp - msg) < 0x4000 && first) {
+ (dstp - msg) < 0x4000) {
*cpp++ = dstp;
*cpp = NULL;
- first = 0;
}
}
/* copy label to buffer */
- if ((n & NS_CMPRSFLGS) != 0 && n != 0x41) { /* Should not happen. */
- goto cleanup;
- }
- if (n == 0x41) {
- n = *++srcp / 8;
- if (dstp + 1 >= eob)
- goto cleanup;
- *dstp++ = 0x41;
+ if (n & NS_CMPRSFLGS) { /* Should not happen. */
+ errno = EMSGSIZE;
+ return (-1);
}
if (dstp + 1 + n >= eob) {
- goto cleanup;
+ errno = EMSGSIZE;
+ return (-1);
}
memcpy(dstp, srcp, n + 1);
srcp += n + 1;
@@ -525,12 +399,11 @@ ns_name_pack(const u_char *src, u_char *dst, int dstsiz,
} while (n != 0);
if (dstp > eob) {
-cleanup:
if (msg != NULL)
*lpp = NULL;
- __set_errno (EMSGSIZE);
+ errno = EMSGSIZE;
return (-1);
- }
+ }
return (dstp - dst);
}
@@ -548,7 +421,7 @@ ns_name_uncompress(const u_char *msg, const u_char *eom, const u_char *src,
{
u_char tmp[NS_MAXCDNAME];
int n;
-
+
if ((n = ns_name_unpack(msg, eom, src, tmp, sizeof tmp)) == -1)
return (-1);
if (ns_name_ntop(tmp, dst, dstsiz) == -1)
@@ -582,23 +455,6 @@ ns_name_compress(const char *src, u_char *dst, size_t dstsiz,
}
/*
- * Reset dnptrs so that there are no active references to pointers at or
- * after src.
- */
-void
-ns_name_rollback(const u_char *src, const u_char **dnptrs,
- const u_char **lastdnptr)
-{
- while (dnptrs < lastdnptr && *dnptrs != NULL) {
- if (*dnptrs >= src) {
- *dnptrs = NULL;
- break;
- }
- dnptrs++;
- }
-}
-
-/*
* ns_name_skip(ptrptr, eom)
* Advance *ptrptr to skip over the compressed name it points at.
* return:
@@ -620,13 +476,13 @@ ns_name_skip(const u_char **ptrptr, const u_char *eom) {
cp++;
break;
default: /* illegal type */
- __set_errno (EMSGSIZE);
+ errno = EMSGSIZE;
return (-1);
}
break;
}
if (cp > eom) {
- __set_errno (EMSGSIZE);
+ errno = EMSGSIZE;
return (-1);
}
*ptrptr = cp;
@@ -700,49 +556,37 @@ dn_find(const u_char *domain, const u_char *msg,
u_int n;
for (cpp = dnptrs; cpp < lastdnptr; cpp++) {
- sp = *cpp;
- /*
- * terminate search on:
- * root label
- * compression pointer
- * unusable offset
- */
- while (*sp != 0 && (*sp & NS_CMPRSFLGS) == 0 &&
- (sp - msg) < 0x4000) {
- dn = domain;
- cp = sp;
- while ((n = *cp++) != 0) {
- /*
- * check for indirection
- */
- switch (n & NS_CMPRSFLGS) {
- case 0: /* normal case, n == len */
- if (n != *dn++)
- goto next;
- for ((void)NULL; n > 0; n--)
- if (mklower(*dn++) !=
- mklower(*cp++))
- goto next;
- /* Is next root for both ? */
- if (*dn == '\0' && *cp == '\0')
- return (sp - msg);
- if (*dn)
- continue;
+ dn = domain;
+ sp = cp = *cpp;
+ while ((n = *cp++) != 0) {
+ /*
+ * check for indirection
+ */
+ switch (n & NS_CMPRSFLGS) {
+ case 0: /* normal case, n == len */
+ if (n != *dn++)
goto next;
-
- case NS_CMPRSFLGS: /* indirection */
- cp = msg + (((n & 0x3f) << 8) | *cp);
- break;
-
- default: /* illegal type */
- __set_errno (EMSGSIZE);
- return (-1);
- }
+ for ((void)NULL; n > 0; n--)
+ if (mklower(*dn++) != mklower(*cp++))
+ goto next;
+ /* Is next root for both ? */
+ if (*dn == '\0' && *cp == '\0')
+ return (sp - msg);
+ if (*dn)
+ continue;
+ goto next;
+
+ case NS_CMPRSFLGS: /* indirection */
+ cp = msg + (((n & 0x3f) << 8) | *cp);
+ break;
+
+ default: /* illegal type */
+ errno = EMSGSIZE;
+ return (-1);
}
- next:
- sp += *sp + 1;
}
+ next: ;
}
- __set_errno (ENOENT);
+ errno = ENOENT;
return (-1);
}