|
|
147e83 |
commit 5e30b8ef0758763effa115634e0ed7d8938e4bc0
|
|
|
147e83 |
Author: Florian Weimer <fweimer@redhat.com>
|
|
|
147e83 |
Date: Mon Jan 21 08:59:42 2019 +0100
|
|
|
147e83 |
|
|
|
147e83 |
resolv: Reformat inet_addr, inet_aton to GNU style
|
|
|
147e83 |
|
|
|
147e83 |
diff --git a/resolv/inet_addr.c b/resolv/inet_addr.c
|
|
|
147e83 |
index 022f7ea0841b6bae..32f58b0e13598b32 100644
|
|
|
147e83 |
--- a/resolv/inet_addr.c
|
|
|
147e83 |
+++ b/resolv/inet_addr.c
|
|
|
147e83 |
@@ -1,3 +1,21 @@
|
|
|
147e83 |
+/* Legacy IPv4 text-to-address functions.
|
|
|
147e83 |
+ Copyright (C) 2019 Free Software Foundation, Inc.
|
|
|
147e83 |
+ This file is part of the GNU C Library.
|
|
|
147e83 |
+
|
|
|
147e83 |
+ The GNU C Library is free software; you can redistribute it and/or
|
|
|
147e83 |
+ modify it under the terms of the GNU Lesser General Public
|
|
|
147e83 |
+ License as published by the Free Software Foundation; either
|
|
|
147e83 |
+ version 2.1 of the License, or (at your option) any later version.
|
|
|
147e83 |
+
|
|
|
147e83 |
+ The GNU C Library is distributed in the hope that it will be useful,
|
|
|
147e83 |
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
147e83 |
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
147e83 |
+ Lesser General Public License for more details.
|
|
|
147e83 |
+
|
|
|
147e83 |
+ You should have received a copy of the GNU Lesser General Public
|
|
|
147e83 |
+ License along with the GNU C Library; if not, see
|
|
|
147e83 |
+ <http://www.gnu.org/licenses/>. */
|
|
|
147e83 |
+
|
|
|
147e83 |
/*
|
|
|
147e83 |
* Copyright (c) 1983, 1990, 1993
|
|
|
147e83 |
* The Regents of the University of California. All rights reserved.
|
|
|
147e83 |
@@ -78,105 +96,97 @@
|
|
|
147e83 |
#include <limits.h>
|
|
|
147e83 |
#include <errno.h>
|
|
|
147e83 |
|
|
|
147e83 |
-/*
|
|
|
147e83 |
- * Ascii internet address interpretation routine.
|
|
|
147e83 |
- * The value returned is in network order.
|
|
|
147e83 |
- */
|
|
|
147e83 |
+/* ASCII IPv4 Internet address interpretation routine. The value
|
|
|
147e83 |
+ returned is in network order. */
|
|
|
147e83 |
in_addr_t
|
|
|
147e83 |
-__inet_addr(const char *cp) {
|
|
|
147e83 |
- struct in_addr val;
|
|
|
147e83 |
+__inet_addr (const char *cp)
|
|
|
147e83 |
+{
|
|
|
147e83 |
+ struct in_addr val;
|
|
|
147e83 |
|
|
|
147e83 |
- if (__inet_aton(cp, &val))
|
|
|
147e83 |
- return (val.s_addr);
|
|
|
147e83 |
- return (INADDR_NONE);
|
|
|
147e83 |
+ if (__inet_aton (cp, &val))
|
|
|
147e83 |
+ return val.s_addr;
|
|
|
147e83 |
+ return INADDR_NONE;
|
|
|
147e83 |
}
|
|
|
147e83 |
weak_alias (__inet_addr, inet_addr)
|
|
|
147e83 |
|
|
|
147e83 |
-/*
|
|
|
147e83 |
- * Check whether "cp" is a valid ascii representation
|
|
|
147e83 |
- * of an Internet address and convert to a binary address.
|
|
|
147e83 |
- * Returns 1 if the address is valid, 0 if not.
|
|
|
147e83 |
- * This replaces inet_addr, the return value from which
|
|
|
147e83 |
- * cannot distinguish between failure and a local broadcast address.
|
|
|
147e83 |
- */
|
|
|
147e83 |
+/* Check whether "cp" is a valid ASCII representation of an IPv4
|
|
|
147e83 |
+ Internet address and convert it to a binary address. Returns 1 if
|
|
|
147e83 |
+ the address is valid, 0 if not. This replaces inet_addr, the
|
|
|
147e83 |
+ return value from which cannot distinguish between failure and a
|
|
|
147e83 |
+ local broadcast address. */
|
|
|
147e83 |
int
|
|
|
147e83 |
-__inet_aton(const char *cp, struct in_addr *addr)
|
|
|
147e83 |
+__inet_aton (const char *cp, struct in_addr *addr)
|
|
|
147e83 |
{
|
|
|
147e83 |
- static const in_addr_t max[4] = { 0xffffffff, 0xffffff, 0xffff, 0xff };
|
|
|
147e83 |
- in_addr_t val;
|
|
|
147e83 |
- char c;
|
|
|
147e83 |
- union iaddr {
|
|
|
147e83 |
- uint8_t bytes[4];
|
|
|
147e83 |
- uint32_t word;
|
|
|
147e83 |
- } res;
|
|
|
147e83 |
- uint8_t *pp = res.bytes;
|
|
|
147e83 |
- int digit;
|
|
|
147e83 |
-
|
|
|
147e83 |
- int saved_errno = errno;
|
|
|
147e83 |
- __set_errno (0);
|
|
|
147e83 |
-
|
|
|
147e83 |
- res.word = 0;
|
|
|
147e83 |
-
|
|
|
147e83 |
- c = *cp;
|
|
|
147e83 |
- for (;;) {
|
|
|
147e83 |
- /*
|
|
|
147e83 |
- * Collect number up to ``.''.
|
|
|
147e83 |
- * Values are specified as for C:
|
|
|
147e83 |
- * 0x=hex, 0=octal, isdigit=decimal.
|
|
|
147e83 |
- */
|
|
|
147e83 |
- if (!isdigit(c))
|
|
|
147e83 |
- goto ret_0;
|
|
|
147e83 |
- {
|
|
|
147e83 |
- char *endp;
|
|
|
147e83 |
- unsigned long ul = strtoul (cp, (char **) &endp, 0);
|
|
|
147e83 |
- if (ul == ULONG_MAX && errno == ERANGE)
|
|
|
147e83 |
- goto ret_0;
|
|
|
147e83 |
- if (ul > 0xfffffffful)
|
|
|
147e83 |
- goto ret_0;
|
|
|
147e83 |
- val = ul;
|
|
|
147e83 |
- digit = cp != endp;
|
|
|
147e83 |
- cp = endp;
|
|
|
147e83 |
- }
|
|
|
147e83 |
- c = *cp;
|
|
|
147e83 |
- if (c == '.') {
|
|
|
147e83 |
- /*
|
|
|
147e83 |
- * Internet format:
|
|
|
147e83 |
- * a.b.c.d
|
|
|
147e83 |
- * a.b.c (with c treated as 16 bits)
|
|
|
147e83 |
- * a.b (with b treated as 24 bits)
|
|
|
147e83 |
- */
|
|
|
147e83 |
- if (pp > res.bytes + 2 || val > 0xff)
|
|
|
147e83 |
- goto ret_0;
|
|
|
147e83 |
- *pp++ = val;
|
|
|
147e83 |
- c = *++cp;
|
|
|
147e83 |
- } else
|
|
|
147e83 |
- break;
|
|
|
147e83 |
- }
|
|
|
147e83 |
- /*
|
|
|
147e83 |
- * Check for trailing characters.
|
|
|
147e83 |
- */
|
|
|
147e83 |
- if (c != '\0' && (!isascii(c) || !isspace(c)))
|
|
|
147e83 |
- goto ret_0;
|
|
|
147e83 |
- /*
|
|
|
147e83 |
- * Did we get a valid digit?
|
|
|
147e83 |
- */
|
|
|
147e83 |
- if (!digit)
|
|
|
147e83 |
- goto ret_0;
|
|
|
147e83 |
-
|
|
|
147e83 |
- /* Check whether the last part is in its limits depending on
|
|
|
147e83 |
- the number of parts in total. */
|
|
|
147e83 |
- if (val > max[pp - res.bytes])
|
|
|
147e83 |
+ static const in_addr_t max[4] = { 0xffffffff, 0xffffff, 0xffff, 0xff };
|
|
|
147e83 |
+ in_addr_t val;
|
|
|
147e83 |
+ char c;
|
|
|
147e83 |
+ union iaddr
|
|
|
147e83 |
+ {
|
|
|
147e83 |
+ uint8_t bytes[4];
|
|
|
147e83 |
+ uint32_t word;
|
|
|
147e83 |
+ } res;
|
|
|
147e83 |
+ uint8_t *pp = res.bytes;
|
|
|
147e83 |
+ int digit;
|
|
|
147e83 |
+
|
|
|
147e83 |
+ int saved_errno = errno;
|
|
|
147e83 |
+ __set_errno (0);
|
|
|
147e83 |
+
|
|
|
147e83 |
+ res.word = 0;
|
|
|
147e83 |
+
|
|
|
147e83 |
+ c = *cp;
|
|
|
147e83 |
+ for (;;)
|
|
|
147e83 |
+ {
|
|
|
147e83 |
+ /* Collect number up to ``.''. Values are specified as for C:
|
|
|
147e83 |
+ 0x=hex, 0=octal, isdigit=decimal. */
|
|
|
147e83 |
+ if (!isdigit (c))
|
|
|
147e83 |
+ goto ret_0;
|
|
|
147e83 |
+ {
|
|
|
147e83 |
+ char *endp;
|
|
|
147e83 |
+ unsigned long ul = strtoul (cp, &endp, 0);
|
|
|
147e83 |
+ if (ul == ULONG_MAX && errno == ERANGE)
|
|
|
147e83 |
goto ret_0;
|
|
|
147e83 |
-
|
|
|
147e83 |
- if (addr != NULL)
|
|
|
147e83 |
- addr->s_addr = res.word | htonl (val);
|
|
|
147e83 |
-
|
|
|
147e83 |
- __set_errno (saved_errno);
|
|
|
147e83 |
- return (1);
|
|
|
147e83 |
-
|
|
|
147e83 |
-ret_0:
|
|
|
147e83 |
- __set_errno (saved_errno);
|
|
|
147e83 |
- return (0);
|
|
|
147e83 |
+ if (ul > 0xfffffffful)
|
|
|
147e83 |
+ goto ret_0;
|
|
|
147e83 |
+ val = ul;
|
|
|
147e83 |
+ digit = cp != endp;
|
|
|
147e83 |
+ cp = endp;
|
|
|
147e83 |
+ }
|
|
|
147e83 |
+ c = *cp;
|
|
|
147e83 |
+ if (c == '.')
|
|
|
147e83 |
+ {
|
|
|
147e83 |
+ /* Internet format:
|
|
|
147e83 |
+ a.b.c.d
|
|
|
147e83 |
+ a.b.c (with c treated as 16 bits)
|
|
|
147e83 |
+ a.b (with b treated as 24 bits). */
|
|
|
147e83 |
+ if (pp > res.bytes + 2 || val > 0xff)
|
|
|
147e83 |
+ goto ret_0;
|
|
|
147e83 |
+ *pp++ = val;
|
|
|
147e83 |
+ c = *++cp;
|
|
|
147e83 |
+ }
|
|
|
147e83 |
+ else
|
|
|
147e83 |
+ break;
|
|
|
147e83 |
+ }
|
|
|
147e83 |
+ /* Check for trailing characters. */
|
|
|
147e83 |
+ if (c != '\0' && (!isascii (c) || !isspace (c)))
|
|
|
147e83 |
+ goto ret_0;
|
|
|
147e83 |
+ /* Did we get a valid digit? */
|
|
|
147e83 |
+ if (!digit)
|
|
|
147e83 |
+ goto ret_0;
|
|
|
147e83 |
+
|
|
|
147e83 |
+ /* Check whether the last part is in its limits depending on the
|
|
|
147e83 |
+ number of parts in total. */
|
|
|
147e83 |
+ if (val > max[pp - res.bytes])
|
|
|
147e83 |
+ goto ret_0;
|
|
|
147e83 |
+
|
|
|
147e83 |
+ if (addr != NULL)
|
|
|
147e83 |
+ addr->s_addr = res.word | htonl (val);
|
|
|
147e83 |
+
|
|
|
147e83 |
+ __set_errno (saved_errno);
|
|
|
147e83 |
+ return 1;
|
|
|
147e83 |
+
|
|
|
147e83 |
+ ret_0:
|
|
|
147e83 |
+ __set_errno (saved_errno);
|
|
|
147e83 |
+ return 0;
|
|
|
147e83 |
}
|
|
|
147e83 |
weak_alias (__inet_aton, inet_aton)
|
|
|
147e83 |
libc_hidden_def (__inet_aton)
|