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