inet_addr.c revision 1.1.1.1.6.2 1 1.1.1.1.6.2 riz /* $NetBSD: inet_addr.c,v 1.1.1.1.6.2 2011/01/09 20:42:52 riz Exp $ */
2 1.1.1.1.6.2 riz
3 1.1.1.1.6.2 riz /*
4 1.1.1.1.6.2 riz * Copyright (c) 1983, 1990, 1993
5 1.1.1.1.6.2 riz * The Regents of the University of California. All rights reserved.
6 1.1.1.1.6.2 riz *
7 1.1.1.1.6.2 riz * Redistribution and use in source and binary forms, with or without
8 1.1.1.1.6.2 riz * modification, are permitted provided that the following conditions
9 1.1.1.1.6.2 riz * are met:
10 1.1.1.1.6.2 riz * 1. Redistributions of source code must retain the above copyright
11 1.1.1.1.6.2 riz * notice, this list of conditions and the following disclaimer.
12 1.1.1.1.6.2 riz * 2. Redistributions in binary form must reproduce the above copyright
13 1.1.1.1.6.2 riz * notice, this list of conditions and the following disclaimer in the
14 1.1.1.1.6.2 riz * documentation and/or other materials provided with the distribution.
15 1.1.1.1.6.2 riz * 3. All advertising materials mentioning features or use of this software
16 1.1.1.1.6.2 riz * must display the following acknowledgement:
17 1.1.1.1.6.2 riz * This product includes software developed by the University of
18 1.1.1.1.6.2 riz * California, Berkeley and its contributors.
19 1.1.1.1.6.2 riz * 4. Neither the name of the University nor the names of its contributors
20 1.1.1.1.6.2 riz * may be used to endorse or promote products derived from this software
21 1.1.1.1.6.2 riz * without specific prior written permission.
22 1.1.1.1.6.2 riz *
23 1.1.1.1.6.2 riz * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1.1.1.6.2 riz * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1.1.1.6.2 riz * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1.1.1.6.2 riz * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1.1.1.6.2 riz * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1.1.1.6.2 riz * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1.1.1.6.2 riz * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1.1.1.6.2 riz * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1.1.1.6.2 riz * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1.1.1.6.2 riz * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1.1.1.6.2 riz * SUCH DAMAGE.
34 1.1.1.1.6.2 riz */
35 1.1.1.1.6.2 riz
36 1.1.1.1.6.2 riz /*
37 1.1.1.1.6.2 riz * Portions Copyright (c) 1993 by Digital Equipment Corporation.
38 1.1.1.1.6.2 riz *
39 1.1.1.1.6.2 riz * Permission to use, copy, modify, and distribute this software for any
40 1.1.1.1.6.2 riz * purpose with or without fee is hereby granted, provided that the above
41 1.1.1.1.6.2 riz * copyright notice and this permission notice appear in all copies, and that
42 1.1.1.1.6.2 riz * the name of Digital Equipment Corporation not be used in advertising or
43 1.1.1.1.6.2 riz * publicity pertaining to distribution of the document or software without
44 1.1.1.1.6.2 riz * specific, written prior permission.
45 1.1.1.1.6.2 riz *
46 1.1.1.1.6.2 riz * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
47 1.1.1.1.6.2 riz * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
48 1.1.1.1.6.2 riz * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
49 1.1.1.1.6.2 riz * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
50 1.1.1.1.6.2 riz * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
51 1.1.1.1.6.2 riz * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
52 1.1.1.1.6.2 riz * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
53 1.1.1.1.6.2 riz * SOFTWARE.
54 1.1.1.1.6.2 riz */
55 1.1.1.1.6.2 riz
56 1.1.1.1.6.2 riz /*
57 1.1.1.1.6.2 riz * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
58 1.1.1.1.6.2 riz * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
59 1.1.1.1.6.2 riz *
60 1.1.1.1.6.2 riz * Permission to use, copy, modify, and distribute this software for any
61 1.1.1.1.6.2 riz * purpose with or without fee is hereby granted, provided that the above
62 1.1.1.1.6.2 riz * copyright notice and this permission notice appear in all copies.
63 1.1.1.1.6.2 riz *
64 1.1.1.1.6.2 riz * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
65 1.1.1.1.6.2 riz * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
66 1.1.1.1.6.2 riz * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
67 1.1.1.1.6.2 riz * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
68 1.1.1.1.6.2 riz * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
69 1.1.1.1.6.2 riz * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
70 1.1.1.1.6.2 riz * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
71 1.1.1.1.6.2 riz */
72 1.1.1.1.6.2 riz
73 1.1.1.1.6.2 riz #if defined(LIBC_SCCS) && !defined(lint)
74 1.1.1.1.6.2 riz static const char sccsid[] = "@(#)inet_addr.c 8.1 (Berkeley) 6/17/93";
75 1.1.1.1.6.2 riz static const char rcsid[] = "Id: inet_addr.c,v 1.5 2005/04/27 04:56:19 sra Exp";
76 1.1.1.1.6.2 riz #endif /* LIBC_SCCS and not lint */
77 1.1.1.1.6.2 riz
78 1.1.1.1.6.2 riz #include "port_before.h"
79 1.1.1.1.6.2 riz
80 1.1.1.1.6.2 riz #include <sys/types.h>
81 1.1.1.1.6.2 riz #include <sys/param.h>
82 1.1.1.1.6.2 riz
83 1.1.1.1.6.2 riz #include <netinet/in.h>
84 1.1.1.1.6.2 riz #include <arpa/inet.h>
85 1.1.1.1.6.2 riz
86 1.1.1.1.6.2 riz #include <ctype.h>
87 1.1.1.1.6.2 riz
88 1.1.1.1.6.2 riz #include "port_after.h"
89 1.1.1.1.6.2 riz
90 1.1.1.1.6.2 riz /*%
91 1.1.1.1.6.2 riz * Ascii internet address interpretation routine.
92 1.1.1.1.6.2 riz * The value returned is in network order.
93 1.1.1.1.6.2 riz */
94 1.1.1.1.6.2 riz u_long
95 1.1.1.1.6.2 riz inet_addr(const char *cp) {
96 1.1.1.1.6.2 riz struct in_addr val;
97 1.1.1.1.6.2 riz
98 1.1.1.1.6.2 riz if (inet_aton(cp, &val))
99 1.1.1.1.6.2 riz return (val.s_addr);
100 1.1.1.1.6.2 riz return (INADDR_NONE);
101 1.1.1.1.6.2 riz }
102 1.1.1.1.6.2 riz
103 1.1.1.1.6.2 riz /*%
104 1.1.1.1.6.2 riz * Check whether "cp" is a valid ascii representation
105 1.1.1.1.6.2 riz * of an Internet address and convert to a binary address.
106 1.1.1.1.6.2 riz * Returns 1 if the address is valid, 0 if not.
107 1.1.1.1.6.2 riz * This replaces inet_addr, the return value from which
108 1.1.1.1.6.2 riz * cannot distinguish between failure and a local broadcast address.
109 1.1.1.1.6.2 riz */
110 1.1.1.1.6.2 riz int
111 1.1.1.1.6.2 riz inet_aton(const char *cp, struct in_addr *addr) {
112 1.1.1.1.6.2 riz u_long val;
113 1.1.1.1.6.2 riz int base, n;
114 1.1.1.1.6.2 riz char c;
115 1.1.1.1.6.2 riz u_int8_t parts[4];
116 1.1.1.1.6.2 riz u_int8_t *pp = parts;
117 1.1.1.1.6.2 riz int digit;
118 1.1.1.1.6.2 riz
119 1.1.1.1.6.2 riz c = *cp;
120 1.1.1.1.6.2 riz for (;;) {
121 1.1.1.1.6.2 riz /*
122 1.1.1.1.6.2 riz * Collect number up to ``.''.
123 1.1.1.1.6.2 riz * Values are specified as for C:
124 1.1.1.1.6.2 riz * 0x=hex, 0=octal, isdigit=decimal.
125 1.1.1.1.6.2 riz */
126 1.1.1.1.6.2 riz if (!isdigit((unsigned char)c))
127 1.1.1.1.6.2 riz return (0);
128 1.1.1.1.6.2 riz val = 0; base = 10; digit = 0;
129 1.1.1.1.6.2 riz if (c == '0') {
130 1.1.1.1.6.2 riz c = *++cp;
131 1.1.1.1.6.2 riz if (c == 'x' || c == 'X')
132 1.1.1.1.6.2 riz base = 16, c = *++cp;
133 1.1.1.1.6.2 riz else {
134 1.1.1.1.6.2 riz base = 8;
135 1.1.1.1.6.2 riz digit = 1 ;
136 1.1.1.1.6.2 riz }
137 1.1.1.1.6.2 riz }
138 1.1.1.1.6.2 riz for (;;) {
139 1.1.1.1.6.2 riz if (isascii(c) && isdigit((unsigned char)c)) {
140 1.1.1.1.6.2 riz if (base == 8 && (c == '8' || c == '9'))
141 1.1.1.1.6.2 riz return (0);
142 1.1.1.1.6.2 riz val = (val * base) + (c - '0');
143 1.1.1.1.6.2 riz c = *++cp;
144 1.1.1.1.6.2 riz digit = 1;
145 1.1.1.1.6.2 riz } else if (base == 16 && isascii(c) &&
146 1.1.1.1.6.2 riz isxdigit((unsigned char)c)) {
147 1.1.1.1.6.2 riz val = (val << 4) |
148 1.1.1.1.6.2 riz (c + 10 - (islower((unsigned char)c) ? 'a' : 'A'));
149 1.1.1.1.6.2 riz c = *++cp;
150 1.1.1.1.6.2 riz digit = 1;
151 1.1.1.1.6.2 riz } else
152 1.1.1.1.6.2 riz break;
153 1.1.1.1.6.2 riz }
154 1.1.1.1.6.2 riz if (c == '.') {
155 1.1.1.1.6.2 riz /*
156 1.1.1.1.6.2 riz * Internet format:
157 1.1.1.1.6.2 riz * a.b.c.d
158 1.1.1.1.6.2 riz * a.b.c (with c treated as 16 bits)
159 1.1.1.1.6.2 riz * a.b (with b treated as 24 bits)
160 1.1.1.1.6.2 riz */
161 1.1.1.1.6.2 riz if (pp >= parts + 3 || val > 0xffU)
162 1.1.1.1.6.2 riz return (0);
163 1.1.1.1.6.2 riz *pp++ = val;
164 1.1.1.1.6.2 riz c = *++cp;
165 1.1.1.1.6.2 riz } else
166 1.1.1.1.6.2 riz break;
167 1.1.1.1.6.2 riz }
168 1.1.1.1.6.2 riz /*
169 1.1.1.1.6.2 riz * Check for trailing characters.
170 1.1.1.1.6.2 riz */
171 1.1.1.1.6.2 riz if (c != '\0' && (!isascii(c) || !isspace((unsigned char)c)))
172 1.1.1.1.6.2 riz return (0);
173 1.1.1.1.6.2 riz /*
174 1.1.1.1.6.2 riz * Did we get a valid digit?
175 1.1.1.1.6.2 riz */
176 1.1.1.1.6.2 riz if (!digit)
177 1.1.1.1.6.2 riz return (0);
178 1.1.1.1.6.2 riz /*
179 1.1.1.1.6.2 riz * Concoct the address according to
180 1.1.1.1.6.2 riz * the number of parts specified.
181 1.1.1.1.6.2 riz */
182 1.1.1.1.6.2 riz n = pp - parts + 1;
183 1.1.1.1.6.2 riz switch (n) {
184 1.1.1.1.6.2 riz case 1: /*%< a -- 32 bits */
185 1.1.1.1.6.2 riz break;
186 1.1.1.1.6.2 riz
187 1.1.1.1.6.2 riz case 2: /*%< a.b -- 8.24 bits */
188 1.1.1.1.6.2 riz if (val > 0xffffffU)
189 1.1.1.1.6.2 riz return (0);
190 1.1.1.1.6.2 riz val |= parts[0] << 24;
191 1.1.1.1.6.2 riz break;
192 1.1.1.1.6.2 riz
193 1.1.1.1.6.2 riz case 3: /*%< a.b.c -- 8.8.16 bits */
194 1.1.1.1.6.2 riz if (val > 0xffffU)
195 1.1.1.1.6.2 riz return (0);
196 1.1.1.1.6.2 riz val |= (parts[0] << 24) | (parts[1] << 16);
197 1.1.1.1.6.2 riz break;
198 1.1.1.1.6.2 riz
199 1.1.1.1.6.2 riz case 4: /*%< a.b.c.d -- 8.8.8.8 bits */
200 1.1.1.1.6.2 riz if (val > 0xffU)
201 1.1.1.1.6.2 riz return (0);
202 1.1.1.1.6.2 riz val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
203 1.1.1.1.6.2 riz break;
204 1.1.1.1.6.2 riz }
205 1.1.1.1.6.2 riz if (addr != NULL)
206 1.1.1.1.6.2 riz addr->s_addr = htonl(val);
207 1.1.1.1.6.2 riz return (1);
208 1.1.1.1.6.2 riz }
209 1.1.1.1.6.2 riz
210 1.1.1.1.6.2 riz /*! \file */
211