1 //==========================================================================
5 // INET address parse functions
7 //==========================================================================
8 //####BSDCOPYRIGHTBEGIN####
10 // -------------------------------------------
12 // Portions of this software may have been derived from OpenBSD,
13 // FreeBSD or other sources, and are covered by the appropriate
14 // copyright disclaimers included herein.
16 // Portions created by Red Hat are
17 // Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
19 // -------------------------------------------
21 //####BSDCOPYRIGHTEND####
22 //==========================================================================
23 //#####DESCRIPTIONBEGIN####
26 // Contributors: gthomas
32 //####DESCRIPTIONEND####
34 //==========================================================================
36 /* $OpenBSD: inet_addr.c,v 1.6 1999/05/03 22:31:14 yanick Exp $ */
39 * ++Copyright++ 1983, 1990, 1993
41 * Copyright (c) 1983, 1990, 1993
42 * The Regents of the University of California. All rights reserved.
44 * Redistribution and use in source and binary forms, with or without
45 * modification, are permitted provided that the following conditions
47 * 1. Redistributions of source code must retain the above copyright
48 * notice, this list of conditions and the following disclaimer.
49 * 2. Redistributions in binary form must reproduce the above copyright
50 * notice, this list of conditions and the following disclaimer in the
51 * documentation and/or other materials provided with the distribution.
52 * 3. All advertising materials mentioning features or use of this software
53 * must display the following acknowledgement:
54 * This product includes software developed by the University of
55 * California, Berkeley and its contributors.
56 * 4. Neither the name of the University nor the names of its contributors
57 * may be used to endorse or promote products derived from this software
58 * without specific prior written permission.
60 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
61 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
62 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
63 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
64 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
65 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
66 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
67 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
69 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
72 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
74 * Permission to use, copy, modify, and distribute this software for any
75 * purpose with or without fee is hereby granted, provided that the above
76 * copyright notice and this permission notice appear in all copies, and that
77 * the name of Digital Equipment Corporation not be used in advertising or
78 * publicity pertaining to distribution of the document or software without
79 * specific, written prior permission.
81 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
82 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
83 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
84 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
85 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
86 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
87 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
93 #if defined(LIBC_SCCS) && !defined(lint)
95 static char sccsid[] = "@(#)inet_addr.c 8.1 (Berkeley) 6/17/93";
96 static char rcsid[] = "$From: inet_addr.c,v 8.5 1996/08/05 08:31:35 vixie Exp $";
98 static char rcsid[] = "$OpenBSD: inet_addr.c,v 1.6 1999/05/03 22:31:14 yanick Exp $";
100 #endif /* LIBC_SCCS and not lint */
103 #include <pkgconf/net.h>
105 #include <sys/param.h>
106 #include <sys/socket.h>
107 #include <sys/ioctl.h>
111 #include <netinet/in.h>
113 #include <sys/types.h>
114 #include <sys/param.h>
115 #include <netinet/in.h>
117 #include <arpa/inet.h>
121 #define isascii(c) (((unsigned char)(c) >= 0x20) && ((unsigned char)(c) <= 0x7F))
125 * Ascii internet address interpretation routine.
126 * The value returned is in network order.
129 inet_addr(const char *cp)
133 if (inet_aton(cp, &val))
135 return (INADDR_NONE);
139 * Check whether "cp" is a valid ascii representation
140 * of an Internet address and convert to a binary address.
141 * Returns 1 if the address is valid, 0 if not.
142 * This replaces inet_addr, the return value from which
143 * cannot distinguish between failure and a local broadcast address.
146 inet_aton(const char *cp, struct in_addr *addr)
148 register in_addr_t val;
149 register int base, n;
152 register u_int *pp = parts;
157 * Collect number up to ``.''.
158 * Values are specified as for C:
159 * 0x=hex, 0=octal, isdigit=decimal.
166 if (c == 'x' || c == 'X')
167 base = 16, c = *++cp;
172 if (isascii(c) && isdigit(c)) {
173 val = (val * base) + (c - '0');
175 } else if (base == 16 && isascii(c) && isxdigit(c)) {
177 (c + 10 - (islower(c) ? 'a' : 'A'));
186 * a.b.c (with c treated as 16 bits)
187 * a.b (with b treated as 24 bits)
197 * Check for trailing characters.
199 if (c != '\0' && (!isascii(c) || !isspace(c)))
202 * Concoct the address according to
203 * the number of parts specified.
209 return (0); /* initial nondigit */
211 case 1: /* a -- 32 bits */
214 case 2: /* a.b -- 8.24 bits */
215 if ((val > 0xffffff) || (parts[0] > 0xff))
217 val |= parts[0] << 24;
220 case 3: /* a.b.c -- 8.8.16 bits */
221 if ((val > 0xffff) || (parts[0] > 0xff) || (parts[1] > 0xff))
223 val |= (parts[0] << 24) | (parts[1] << 16);
226 case 4: /* a.b.c.d -- 8.8.8.8 bits */
227 if ((val > 0xff) || (parts[0] > 0xff) || (parts[1] > 0xff) || (parts[2] > 0xff))
229 val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
233 addr->s_addr = htonl(val);