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