Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /*******************************************************************************
3 : * The following function pr_inet_aton is based on the BSD function inet_aton
4 : * with some modifications. The license and copyright notices applying to this
5 : * function appear below. Modifications are also according to the license below.
6 : ******************************************************************************/
7 :
8 : #include "prnetdb.h"
9 :
10 : /*
11 : * Copyright (c) 1983, 1990, 1993
12 : * The Regents of the University of California. All rights reserved.
13 : *
14 : * Redistribution and use in source and binary forms, with or without
15 : * modification, are permitted provided that the following conditions
16 : * are met:
17 : * 1. Redistributions of source code must retain the above copyright
18 : * notice, this list of conditions and the following disclaimer.
19 : * 2. Redistributions in binary form must reproduce the above copyright
20 : * notice, this list of conditions and the following disclaimer in the
21 : * documentation and/or other materials provided with the distribution.
22 : * 4. Neither the name of the University nor the names of its contributors
23 : * may be used to endorse or promote products derived from this software
24 : * without specific prior written permission.
25 : *
26 : * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 : * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 : * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 : * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 : * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 : * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 : * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 : * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 : * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 : * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 : * SUCH DAMAGE.
37 : */
38 :
39 : /*
40 : * Portions Copyright (c) 1993 by Digital Equipment Corporation.
41 : *
42 : * Permission to use, copy, modify, and distribute this software for any
43 : * purpose with or without fee is hereby granted, provided that the above
44 : * copyright notice and this permission notice appear in all copies, and that
45 : * the name of Digital Equipment Corporation not be used in advertising or
46 : * publicity pertaining to distribution of the document or software without
47 : * specific, written prior permission.
48 : *
49 : * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
50 : * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
51 : * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
52 : * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
53 : * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
54 : * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
55 : * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
56 : * SOFTWARE.
57 : */
58 :
59 : /*
60 : * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
61 : * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
62 : *
63 : * Permission to use, copy, modify, and distribute this software for any
64 : * purpose with or without fee is hereby granted, provided that the above
65 : * copyright notice and this permission notice appear in all copies.
66 : *
67 : * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
68 : * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
69 : * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
70 : * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
71 : * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
72 : * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
73 : * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
74 : */
75 :
76 : #define XX 127
77 : static const unsigned char index_hex[256] = {
78 : XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
79 : XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
80 : XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
81 : 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,XX,XX, XX,XX,XX,XX,
82 : XX,10,11,12, 13,14,15,XX, XX,XX,XX,XX, XX,XX,XX,XX,
83 : XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
84 : XX,10,11,12, 13,14,15,XX, XX,XX,XX,XX, XX,XX,XX,XX,
85 : XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
86 : XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
87 : XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
88 : XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
89 : XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
90 : XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
91 : XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
92 : XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
93 : XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
94 : };
95 :
96 171 : static PRBool _isdigit(char c) { return c >= '0' && c <= '9'; }
97 0 : static PRBool _isxdigit(char c) { return index_hex[(unsigned char) c] != XX; }
98 0 : static PRBool _isspace(char c) { return c == ' ' || (c >= '\t' && c <= '\r'); }
99 : #undef XX
100 :
101 : int
102 94 : pr_inet_aton(const char *cp, PRUint32 *addr)
103 : {
104 : PRUint32 val;
105 : int base, n;
106 : char c;
107 : PRUint8 parts[4];
108 94 : PRUint8 *pp = parts;
109 : int digit;
110 :
111 94 : c = *cp;
112 : for (;;) {
113 : /*
114 : * Collect number up to ``.''.
115 : * Values are specified as for C:
116 : * 0x=hex, 0=octal, isdigit=decimal.
117 : */
118 136 : if (!_isdigit(c))
119 87 : return (0);
120 28 : val = 0; base = 10; digit = 0;
121 28 : if (c == '0') {
122 14 : c = *++cp;
123 14 : if (c == 'x' || c == 'X')
124 0 : base = 16, c = *++cp;
125 : else {
126 14 : base = 8;
127 14 : digit = 1;
128 : }
129 : }
130 : for (;;) {
131 84 : if (_isdigit(c)) {
132 28 : if (base == 8 && (c == '8' || c == '9'))
133 0 : return (0);
134 28 : val = (val * base) + (c - '0');
135 28 : c = *++cp;
136 28 : digit = 1;
137 28 : } else if (base == 16 && _isxdigit(c)) {
138 0 : val = (val << 4) + index_hex[(unsigned char) c];
139 0 : c = *++cp;
140 0 : digit = 1;
141 : } else
142 : break;
143 : }
144 28 : if (c == '.') {
145 : /*
146 : * Internet format:
147 : * a.b.c.d
148 : * a.b.c (with c treated as 16 bits)
149 : * a.b (with b treated as 24 bits)
150 : */
151 21 : if (pp >= parts + 3 || val > 0xffU)
152 0 : return (0);
153 21 : *pp++ = val;
154 21 : c = *++cp;
155 : } else
156 7 : break;
157 : }
158 : /*
159 : * Check for trailing characters.
160 : */
161 7 : if (c != '\0' && !_isspace(c))
162 0 : return (0);
163 : /*
164 : * Did we get a valid digit?
165 : */
166 7 : if (!digit)
167 0 : return (0);
168 : /*
169 : * Concoct the address according to
170 : * the number of parts specified.
171 : */
172 7 : n = pp - parts + 1;
173 7 : switch (n) {
174 : case 1: /*%< a -- 32 bits */
175 0 : break;
176 :
177 : case 2: /*%< a.b -- 8.24 bits */
178 0 : if (val > 0xffffffU)
179 0 : return (0);
180 0 : val |= (unsigned int)parts[0] << 24;
181 0 : break;
182 :
183 : case 3: /*%< a.b.c -- 8.8.16 bits */
184 0 : if (val > 0xffffU)
185 0 : return (0);
186 0 : val |= ((unsigned int)parts[0] << 24) | ((unsigned int)parts[1] << 16);
187 0 : break;
188 :
189 : case 4: /*%< a.b.c.d -- 8.8.8.8 bits */
190 7 : if (val > 0xffU)
191 0 : return (0);
192 21 : val |= ((unsigned int)parts[0] << 24) |
193 14 : ((unsigned int)parts[1] << 16) |
194 7 : ((unsigned int)parts[2] << 8);
195 7 : break;
196 : }
197 7 : *addr = PR_htonl(val);
198 7 : return (1);
199 : }
200 :
|