andre@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ andre@0: /******************************************************************************* andre@0: * The following function pr_inet_aton is based on the BSD function inet_aton andre@0: * with some modifications. The license and copyright notices applying to this andre@0: * function appear below. Modifications are also according to the license below. andre@0: ******************************************************************************/ andre@0: andre@0: #include "prnetdb.h" andre@0: andre@0: /* andre@0: * Copyright (c) 1983, 1990, 1993 andre@0: * The Regents of the University of California. All rights reserved. andre@0: * andre@0: * Redistribution and use in source and binary forms, with or without andre@0: * modification, are permitted provided that the following conditions andre@0: * are met: andre@0: * 1. Redistributions of source code must retain the above copyright andre@0: * notice, this list of conditions and the following disclaimer. andre@0: * 2. Redistributions in binary form must reproduce the above copyright andre@0: * notice, this list of conditions and the following disclaimer in the andre@0: * documentation and/or other materials provided with the distribution. andre@0: * 4. Neither the name of the University nor the names of its contributors andre@0: * may be used to endorse or promote products derived from this software andre@0: * without specific prior written permission. andre@0: * andre@0: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND andre@0: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE andre@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE andre@0: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE andre@0: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL andre@0: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS andre@0: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) andre@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT andre@0: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY andre@0: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF andre@0: * SUCH DAMAGE. andre@0: */ andre@0: andre@0: /* andre@0: * Portions Copyright (c) 1993 by Digital Equipment Corporation. andre@0: * andre@0: * Permission to use, copy, modify, and distribute this software for any andre@0: * purpose with or without fee is hereby granted, provided that the above andre@0: * copyright notice and this permission notice appear in all copies, and that andre@0: * the name of Digital Equipment Corporation not be used in advertising or andre@0: * publicity pertaining to distribution of the document or software without andre@0: * specific, written prior permission. andre@0: * andre@0: * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL andre@0: * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES andre@0: * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT andre@0: * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL andre@0: * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR andre@0: * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS andre@0: * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS andre@0: * SOFTWARE. andre@0: */ andre@0: andre@0: /* andre@0: * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") andre@0: * Portions Copyright (c) 1996-1999 by Internet Software Consortium. andre@0: * andre@0: * Permission to use, copy, modify, and distribute this software for any andre@0: * purpose with or without fee is hereby granted, provided that the above andre@0: * copyright notice and this permission notice appear in all copies. andre@0: * andre@0: * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES andre@0: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF andre@0: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR andre@0: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES andre@0: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN andre@0: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT andre@0: * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. andre@0: */ andre@0: andre@0: #define XX 127 andre@0: static const unsigned char index_hex[256] = { andre@0: XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, andre@0: XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, andre@0: XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, andre@0: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,XX,XX, XX,XX,XX,XX, andre@0: XX,10,11,12, 13,14,15,XX, XX,XX,XX,XX, XX,XX,XX,XX, andre@0: XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, andre@0: XX,10,11,12, 13,14,15,XX, XX,XX,XX,XX, XX,XX,XX,XX, andre@0: XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, andre@0: XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, andre@0: XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, andre@0: XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, andre@0: XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, andre@0: XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, andre@0: XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, andre@0: XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, andre@0: XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, andre@0: }; andre@0: andre@0: static PRBool _isdigit(char c) { return c >= '0' && c <= '9'; } andre@0: static PRBool _isxdigit(char c) { return index_hex[(unsigned char) c] != XX; } andre@0: static PRBool _isspace(char c) { return c == ' ' || (c >= '\t' && c <= '\r'); } andre@0: #undef XX andre@0: andre@0: int andre@0: pr_inet_aton(const char *cp, PRUint32 *addr) andre@0: { andre@0: PRUint32 val; andre@0: int base, n; andre@0: char c; andre@0: PRUint8 parts[4]; andre@0: PRUint8 *pp = parts; andre@0: int digit; andre@0: andre@0: c = *cp; andre@0: for (;;) { andre@0: /* andre@0: * Collect number up to ``.''. andre@0: * Values are specified as for C: andre@0: * 0x=hex, 0=octal, isdigit=decimal. andre@0: */ andre@0: if (!_isdigit(c)) andre@0: return (0); andre@0: val = 0; base = 10; digit = 0; andre@0: if (c == '0') { andre@0: c = *++cp; andre@0: if (c == 'x' || c == 'X') andre@0: base = 16, c = *++cp; andre@0: else { andre@0: base = 8; andre@0: digit = 1; andre@0: } andre@0: } andre@0: for (;;) { andre@0: if (_isdigit(c)) { andre@0: if (base == 8 && (c == '8' || c == '9')) andre@0: return (0); andre@0: val = (val * base) + (c - '0'); andre@0: c = *++cp; andre@0: digit = 1; andre@0: } else if (base == 16 && _isxdigit(c)) { andre@0: val = (val << 4) + index_hex[(unsigned char) c]; andre@0: c = *++cp; andre@0: digit = 1; andre@0: } else andre@0: break; andre@0: } andre@0: if (c == '.') { andre@0: /* andre@0: * Internet format: andre@0: * a.b.c.d andre@0: * a.b.c (with c treated as 16 bits) andre@0: * a.b (with b treated as 24 bits) andre@0: */ andre@0: if (pp >= parts + 3 || val > 0xffU) andre@0: return (0); andre@0: *pp++ = val; andre@0: c = *++cp; andre@0: } else andre@0: break; andre@0: } andre@0: /* andre@0: * Check for trailing characters. andre@0: */ andre@0: if (c != '\0' && !_isspace(c)) andre@0: return (0); andre@0: /* andre@0: * Did we get a valid digit? andre@0: */ andre@0: if (!digit) andre@0: return (0); andre@0: /* andre@0: * Concoct the address according to andre@0: * the number of parts specified. andre@0: */ andre@0: n = pp - parts + 1; andre@0: switch (n) { andre@0: case 1: /*%< a -- 32 bits */ andre@0: break; andre@0: andre@0: case 2: /*%< a.b -- 8.24 bits */ andre@0: if (val > 0xffffffU) andre@0: return (0); andre@0: val |= parts[0] << 24; andre@0: break; andre@0: andre@0: case 3: /*%< a.b.c -- 8.8.16 bits */ andre@0: if (val > 0xffffU) andre@0: return (0); andre@0: val |= (parts[0] << 24) | (parts[1] << 16); andre@0: break; andre@0: andre@0: case 4: /*%< a.b.c.d -- 8.8.8.8 bits */ andre@0: if (val > 0xffU) andre@0: return (0); andre@0: val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8); andre@0: break; andre@0: } andre@0: *addr = PR_htonl(val); andre@0: return (1); andre@0: } andre@0: