inet_net.c revision 7d4227ea78579803a5562f03e4b63790292caac3
1bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood/* 2bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood * Copyright (c) 1983, 1993 3bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood * The Regents of the University of California. All rights reserved. 4bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood * All rights reserved. 5bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood * 6bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood * Redistribution and use in source and binary forms, with or without 7bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood * modification, are permitted provided that the following conditions 8bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood * are met: 9bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood * 1. Redistributions of source code must retain the above copyright 10bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood * notice, this list of conditions and the following disclaimer. 11bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood * 2. Redistributions in binary form must reproduce the above copyright 12bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood * notice, this list of conditions and the following disclaimer in the 13bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood * documentation and/or other materials provided with the distribution. 14bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood * 3. All advertising materials mentioning features or use of this software 15bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood * must display the following acknowledgement: 16bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood * This product includes software developed by the University of 17bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood * California, Berkeley and its contributors. 18bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood * 4. Neither the name of the University nor the names of its contributors 19bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood * may be used to endorse or promote products derived from this software 20bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood * without specific prior written permission. 21bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood * 22bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28fc3f24b4b60c10e0d3f41f70df37e11ea311cc2cJeff Sharkey * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood * SUCH DAMAGE. 33fc3f24b4b60c10e0d3f41f70df37e11ea311cc2cJeff Sharkey */ 34bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood 35bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood#if defined(LIBC_SCCS) && !defined(lint) 36bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwoodstatic char sccsid[] = "@(#)inet_network.c 8.1 (Berkeley) 6/4/93"; 37bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood#endif /* LIBC_SCCS and not lint */ 38bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood 39bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood#include "inetprivate.h" 40bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood 41bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood/* 42bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood * Internet network address interpretation routine. 43bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood * The library routines call this routine to interpret 44bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood * network numbers. 45bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood */ 46bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwoodu_int32_t 47bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwoodinet_network(const char *cp) 48bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood{ 49bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood register u_long val, base, n; 50bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood register char c; 51bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood u_long parts[4], *pp = parts; 52bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood register int i; 53bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood 54bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwoodagain: 55bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood /* 56bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood * Collect number up to ``.''. 57bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood * Values are specified as for C: 58bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood * 0x=hex, 0=octal, other=decimal. 59bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood */ 60bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood val = 0; base = 10; 61bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood /* 62fc3f24b4b60c10e0d3f41f70df37e11ea311cc2cJeff Sharkey * The 4.4BSD version of this file also accepts 'x__' as a hexa 63fc3f24b4b60c10e0d3f41f70df37e11ea311cc2cJeff Sharkey * number. I don't think this is correct. -- Uli 64bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood */ 65fc3f24b4b60c10e0d3f41f70df37e11ea311cc2cJeff Sharkey if (*cp == '0') { 66bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood if (*++cp == 'x' || *cp == 'X') 67bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood base = 16, cp++; 68bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood else 69bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood base = 8; 70bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood } 71bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood while ((c = *cp)) { 72bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood if (isdigit(c)) { 73bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood val = (val * base) + (c - '0'); 74bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood cp++; 75bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood continue; 76bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood } 77bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood if (base == 16 && isxdigit(c)) { 78bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood val = (val << 4) + (c + 10 - (islower(c) ? 'a' : 'A')); 79bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood cp++; 80ad5f83e91b6812a6dee4fea7646fa9061d9f9597Mike Lockwood continue; 81ad5f83e91b6812a6dee4fea7646fa9061d9f9597Mike Lockwood } 82bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood break; 83bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood } 84bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood if (*cp == '.') { 85bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood if (pp >= parts + 4) 86bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood return (INADDR_NONE); 87bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood *pp++ = val, cp++; 88bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood goto again; 89bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood } 90ad5f83e91b6812a6dee4fea7646fa9061d9f9597Mike Lockwood if (*cp && !isspace(*cp)) 91ad5f83e91b6812a6dee4fea7646fa9061d9f9597Mike Lockwood return (INADDR_NONE); 92ad5f83e91b6812a6dee4fea7646fa9061d9f9597Mike Lockwood *pp++ = val; 93ad5f83e91b6812a6dee4fea7646fa9061d9f9597Mike Lockwood n = pp - parts; 94ad5f83e91b6812a6dee4fea7646fa9061d9f9597Mike Lockwood if (n > 4) 95bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood return (INADDR_NONE); 96bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood for (val = 0, i = 0; i < (int)n; i++) { 97bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood val <<= 8; 98bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood val |= parts[i] & 0xff; 99bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood } 100bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood return (val); 101bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood} 102bce6f8f249ff2b65df9ed790cc460053ab8eccecMike Lockwood