7
* from the BIND 4.9.x inetaddr.c
9
* Contains implementation of inet_aton
11
* WSHelper DNS/Hesiod Library for WINSOCK
16
* Copyright (c) 1983, 1990 Regents of the University of California.
17
* All rights reserved.
19
* Redistribution and use in source and binary forms, with or without
20
* modification, are permitted provided that the following conditions
22
* 1. Redistributions of source code must retain the above copyright
23
* notice, this list of conditions and the following disclaimer.
24
* 2. Redistributions in binary form must reproduce the above copyright
25
* notice, this list of conditions and the following disclaimer in the
26
* documentation and/or other materials provided with the distribution.
27
* 3. All advertising materials mentioning features or use of this software
28
* must display the following acknowledgement:
29
* This product includes software developed by the University of
30
* California, Berkeley and its contributors.
31
* 4. Neither the name of the University nor the names of its contributors
32
* may be used to endorse or promote products derived from this software
33
* without specific prior written permission.
35
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
36
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
38
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
39
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
40
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
41
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
42
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
43
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
44
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48
#if defined(LIBC_SCCS) && !defined(lint)
49
static char sccsid[] = "@(#)inet_addr.c 5.11 (Berkeley) 12/9/91";
50
#endif /* LIBC_SCCS and not lint */
58
converts a string containing an (Ipv4) Internet Protocol dotted address into a proper address for the in_addr structure
60
\param[in] cp Null-terminated character string representing a number expressed in the
61
Internet standard ".'' (dotted) notation.
62
\param[in, out] addr pointer to the in_addr structure. The s_addr memeber will be populated
65
\retval Returns 1 if the address is valid, 0 if not.
70
inet_aton(register const char *cp, struct in_addr *addr)
72
register u_long val, base;
75
u_long parts[4], *pp = parts;
79
* Collect number up to ``.''.
80
* Values are specified as for C:
81
* 0x=hex, 0=octal, other=decimal.
85
if (*++cp == 'x' || *cp == 'X')
90
while ((c = *cp) != '\0') {
91
if (isascii(c) && isdigit(c)) {
92
val = (val * base) + (c - '0');
96
if (base == 16 && isascii(c) && isxdigit(c)) {
98
(c + 10 - (islower(c) ? 'a' : 'A'));
108
* a.b.c (with c treated as 16-bits)
109
* a.b (with b treated as 24 bits)
111
if (pp >= parts + 3 || val > 0xff)
118
* Check for trailing characters.
120
if (*cp && (!isascii(*cp) || !isspace(*cp)))
123
* Concoct the address according to
124
* the number of parts specified.
129
case 1: /* a -- 32 bits */
132
case 2: /* a.b -- 8.24 bits */
135
val |= parts[0] << 24;
138
case 3: /* a.b.c -- 8.8.16 bits */
141
val |= (parts[0] << 24) | (parts[1] << 16);
144
case 4: /* a.b.c.d -- 8.8.8.8 bits */
147
val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
151
addr->s_addr = htonl(val);