1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* 23*7c478bd9Sstevel@tonic-gate * Copyright 1987 Sun Microsystems, Inc. All rights reserved. 24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 25*7c478bd9Sstevel@tonic-gate */ 26*7c478bd9Sstevel@tonic-gate 27*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*7c478bd9Sstevel@tonic-gate /* from UCB 4.5 82/11/14 */ 29*7c478bd9Sstevel@tonic-gate 30*7c478bd9Sstevel@tonic-gate #include <sys/types.h> 31*7c478bd9Sstevel@tonic-gate #include <ctype.h> 32*7c478bd9Sstevel@tonic-gate #include <netinet/in.h> 33*7c478bd9Sstevel@tonic-gate 34*7c478bd9Sstevel@tonic-gate /* 35*7c478bd9Sstevel@tonic-gate * Internet address interpretation routine. 36*7c478bd9Sstevel@tonic-gate * All the network library routines call this 37*7c478bd9Sstevel@tonic-gate * routine to interpret entries in the data bases 38*7c478bd9Sstevel@tonic-gate * which are expected to be an address. 39*7c478bd9Sstevel@tonic-gate * The value returned is in network order. 40*7c478bd9Sstevel@tonic-gate */ 41*7c478bd9Sstevel@tonic-gate u_long 42*7c478bd9Sstevel@tonic-gate inet_addr(cp) 43*7c478bd9Sstevel@tonic-gate register char *cp; 44*7c478bd9Sstevel@tonic-gate { 45*7c478bd9Sstevel@tonic-gate register u_long val, base, n; 46*7c478bd9Sstevel@tonic-gate register char c; 47*7c478bd9Sstevel@tonic-gate u_long parts[4], *pp = parts; 48*7c478bd9Sstevel@tonic-gate 49*7c478bd9Sstevel@tonic-gate again: 50*7c478bd9Sstevel@tonic-gate /* 51*7c478bd9Sstevel@tonic-gate * Collect number up to ``.''. 52*7c478bd9Sstevel@tonic-gate * Values are specified as for C: 53*7c478bd9Sstevel@tonic-gate * 0x=hex, 0=octal, other=decimal. 54*7c478bd9Sstevel@tonic-gate */ 55*7c478bd9Sstevel@tonic-gate val = 0; base = 10; 56*7c478bd9Sstevel@tonic-gate if (*cp == '0') { 57*7c478bd9Sstevel@tonic-gate if (*++cp == 'x' || *cp == 'X') 58*7c478bd9Sstevel@tonic-gate base = 16, cp++; 59*7c478bd9Sstevel@tonic-gate else 60*7c478bd9Sstevel@tonic-gate base = 8; 61*7c478bd9Sstevel@tonic-gate } 62*7c478bd9Sstevel@tonic-gate while (c = *cp) { 63*7c478bd9Sstevel@tonic-gate if (isdigit(c)) { 64*7c478bd9Sstevel@tonic-gate if ((c - '0') >= base) 65*7c478bd9Sstevel@tonic-gate break; 66*7c478bd9Sstevel@tonic-gate val = (val * base) + (c - '0'); 67*7c478bd9Sstevel@tonic-gate cp++; 68*7c478bd9Sstevel@tonic-gate continue; 69*7c478bd9Sstevel@tonic-gate } 70*7c478bd9Sstevel@tonic-gate if (base == 16 && isxdigit(c)) { 71*7c478bd9Sstevel@tonic-gate val = (val << 4) + (c + 10 - (islower(c) ? 'a' : 'A')); 72*7c478bd9Sstevel@tonic-gate cp++; 73*7c478bd9Sstevel@tonic-gate continue; 74*7c478bd9Sstevel@tonic-gate } 75*7c478bd9Sstevel@tonic-gate break; 76*7c478bd9Sstevel@tonic-gate } 77*7c478bd9Sstevel@tonic-gate if (*cp == '.') { 78*7c478bd9Sstevel@tonic-gate /* 79*7c478bd9Sstevel@tonic-gate * Internet format: 80*7c478bd9Sstevel@tonic-gate * a.b.c.d 81*7c478bd9Sstevel@tonic-gate * a.b.c (with c treated as 16-bits) 82*7c478bd9Sstevel@tonic-gate * a.b (with b treated as 24 bits) 83*7c478bd9Sstevel@tonic-gate */ 84*7c478bd9Sstevel@tonic-gate if (pp >= parts + 4) 85*7c478bd9Sstevel@tonic-gate return (-1); 86*7c478bd9Sstevel@tonic-gate *pp++ = val, cp++; 87*7c478bd9Sstevel@tonic-gate goto again; 88*7c478bd9Sstevel@tonic-gate } 89*7c478bd9Sstevel@tonic-gate /* 90*7c478bd9Sstevel@tonic-gate * Check for trailing characters. 91*7c478bd9Sstevel@tonic-gate */ 92*7c478bd9Sstevel@tonic-gate if (*cp && !isspace(*cp)) 93*7c478bd9Sstevel@tonic-gate return (-1); 94*7c478bd9Sstevel@tonic-gate *pp++ = val; 95*7c478bd9Sstevel@tonic-gate /* 96*7c478bd9Sstevel@tonic-gate * Concoct the address according to 97*7c478bd9Sstevel@tonic-gate * the number of parts specified. 98*7c478bd9Sstevel@tonic-gate */ 99*7c478bd9Sstevel@tonic-gate n = pp - parts; 100*7c478bd9Sstevel@tonic-gate switch (n) { 101*7c478bd9Sstevel@tonic-gate 102*7c478bd9Sstevel@tonic-gate case 1: /* a -- 32 bits */ 103*7c478bd9Sstevel@tonic-gate val = parts[0]; 104*7c478bd9Sstevel@tonic-gate break; 105*7c478bd9Sstevel@tonic-gate 106*7c478bd9Sstevel@tonic-gate case 2: /* a.b -- 8.24 bits */ 107*7c478bd9Sstevel@tonic-gate val = (parts[0] << 24) | (parts[1] & 0xffffff); 108*7c478bd9Sstevel@tonic-gate break; 109*7c478bd9Sstevel@tonic-gate 110*7c478bd9Sstevel@tonic-gate case 3: /* a.b.c -- 8.8.16 bits */ 111*7c478bd9Sstevel@tonic-gate val = (parts[0] << 24) | ((parts[1] & 0xff) << 16) | 112*7c478bd9Sstevel@tonic-gate (parts[2] & 0xffff); 113*7c478bd9Sstevel@tonic-gate break; 114*7c478bd9Sstevel@tonic-gate 115*7c478bd9Sstevel@tonic-gate case 4: /* a.b.c.d -- 8.8.8.8 bits */ 116*7c478bd9Sstevel@tonic-gate val = (parts[0] << 24) | ((parts[1] & 0xff) << 16) | 117*7c478bd9Sstevel@tonic-gate ((parts[2] & 0xff) << 8) | (parts[3] & 0xff); 118*7c478bd9Sstevel@tonic-gate break; 119*7c478bd9Sstevel@tonic-gate 120*7c478bd9Sstevel@tonic-gate default: 121*7c478bd9Sstevel@tonic-gate return (-1); 122*7c478bd9Sstevel@tonic-gate } 123*7c478bd9Sstevel@tonic-gate val = htonl(val); 124*7c478bd9Sstevel@tonic-gate return (val); 125*7c478bd9Sstevel@tonic-gate } 126