2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
2N/A/* All Rights Reserved */
2N/A/*
2N/A * Portions of this source code were derived from Berkeley
2N/A * 4.3 BSD under license from the Regents of the University of
2N/A * California.
2N/A */
2N/A
2N/A/*
2N/A * Copyright (c) 1983, 1990, 1993
2N/A * The Regents of the University of California. All rights reserved.
2N/A *
2N/A * Redistribution and use in source and binary forms, with or without
2N/A * modification, are permitted provided that the following conditions
2N/A * are met:
2N/A * 1. Redistributions of source code must retain the above copyright
2N/A * notice, this list of conditions and the following disclaimer.
2N/A * 2. Redistributions in binary form must reproduce the above copyright
2N/A * notice, this list of conditions and the following disclaimer in the
2N/A * documentation and/or other materials provided with the distribution.
2N/A * 3. All advertising materials mentioning features or use of this software
2N/A * must display the following acknowledgement:
2N/A * This product includes software developed by the University of
2N/A * California, Berkeley and its contributors.
2N/A * 4. Neither the name of the University nor the names of its contributors
2N/A * may be used to endorse or promote products derived from this software
2N/A * without specific prior written permission.
2N/A *
2N/A * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2N/A * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2N/A * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2N/A * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2N/A * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2N/A * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2N/A * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2N/A * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2N/A * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2N/A * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2N/A * SUCH DAMAGE.
2N/A */
2N/A
2N/A/*
2N/A * Portions Copyright (c) 1993 by Digital Equipment Corporation.
2N/A *
2N/A * Permission to use, copy, modify, and distribute this software for any
2N/A * purpose with or without fee is hereby granted, provided that the above
2N/A * copyright notice and this permission notice appear in all copies, and that
2N/A * the name of Digital Equipment Corporation not be used in advertising or
2N/A * publicity pertaining to distribution of the document or software without
2N/A * specific, written prior permission.
2N/A *
2N/A * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
2N/A * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
2N/A * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
2N/A * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
2N/A * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
2N/A * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
2N/A * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
2N/A * SOFTWARE.
2N/A */
2N/A
2N/A/*
2N/A * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
2N/A *
2N/A * Permission to use, copy, modify, and distribute this software for any
2N/A * purpose with or without fee is hereby granted, provided that the above
2N/A * copyright notice and this permission notice appear in all copies.
2N/A *
2N/A * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
2N/A * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
2N/A * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
2N/A * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
2N/A * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
2N/A * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
2N/A * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
2N/A * SOFTWARE.
2N/A */
2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A/*
2N/A * Convert network-format internet address
2N/A * to base 256 d.d.d.d representation.
2N/A *
2N/A * Reentrant interface
2N/A */
2N/A
2N/A#include "mt.h"
2N/A#include "rpc_mt.h"
2N/A#include <errno.h>
2N/A#include <sys/types.h>
2N/A#include <ctype.h>
2N/A#include <netinet/in.h>
2N/A#include <stdio.h>
2N/A#include <stdlib.h>
2N/A
2N/A
2N/Achar *
2N/Ainet_ntoa_r(in, b)
2N/A struct in_addr in;
2N/A char b[]; /* Assumed >= 18 bytes */
2N/A{
2N/A char *p;
2N/A
2N/A p = (char *)&in;
2N/A#define UC(b) (((int)b)&0xff)
2N/A (void) sprintf(b, "%d.%d.%d.%d",
2N/A UC(p[0]), UC(p[1]), UC(p[2]), UC(p[3]));
2N/A return (b);
2N/A}
2N/A
2N/Achar *
2N/Ainet_ntoa(in)
2N/A struct in_addr in;
2N/A{
2N/A char *b;
2N/A static char b_main[18];
2N/A static pthread_key_t ntoa_key = PTHREAD_ONCE_KEY_NP;
2N/A
2N/A if (thr_main())
2N/A b = b_main;
2N/A else if ((b = thr_get_storage(&ntoa_key, 18, free)) == NULL)
2N/A b = b_main;
2N/A
2N/A return (inet_ntoa_r(in, b));
2N/A}
2N/A
2N/A/*
2N/A * Check whether "cp" is a valid ascii representation
2N/A * of an Internet address and convert to a binary address.
2N/A * Returns 1 if the address is valid, 0 if not.
2N/A * This replaces inet_addr, the return value from which
2N/A * cannot distinguish between failure and a local broadcast address.
2N/A */
2N/Aint
2N/Ainet_aton(const char *cp, struct in_addr *addr)
2N/A{
2N/A uint32_t val;
2N/A int base, n;
2N/A char c;
2N/A unsigned int parts[4];
2N/A unsigned int *pp = parts;
2N/A
2N/A
2N/A c = *cp;
2N/A for (;;) {
2N/A /*
2N/A * Collect number up to ``.''.
2N/A * Values are specified as for C:
2N/A * 0x=hex, 0=octal, isdigit=decimal.
2N/A */
2N/A if (!isdigit(c))
2N/A return (0);
2N/A val = 0; base = 10;
2N/A if (c == '0') {
2N/A c = *++cp;
2N/A if (c == 'x' || c == 'X')
2N/A base = 16, c = *++cp;
2N/A else
2N/A base = 8;
2N/A }
2N/A for (;;) {
2N/A if (isascii(c) && isdigit(c)) {
2N/A val = (val * base) + (c - '0');
2N/A c = *++cp;
2N/A } else if (base == 16 && isascii(c) && isxdigit(c)) {
2N/A val = (val << 4) |
2N/A (c + 10 - (islower(c) ? 'a' : 'A'));
2N/A c = *++cp;
2N/A } else
2N/A break;
2N/A }
2N/A if (c == '.') {
2N/A /*
2N/A * Internet format:
2N/A * a.b.c.d
2N/A * a.b.c (with c treated as 16 bits)
2N/A * a.b (with b treated as 24 bits)
2N/A */
2N/A if (pp >= parts + 3)
2N/A return (0);
2N/A *pp++ = val;
2N/A c = *++cp;
2N/A } else
2N/A break;
2N/A }
2N/A /*
2N/A * Check for trailing characters.
2N/A */
2N/A if (c != '\0' && (!isascii(c) || !isspace(c)))
2N/A return (0);
2N/A /*
2N/A * Concoct the address according to
2N/A * the number of parts specified.
2N/A */
2N/A n = pp - parts + 1;
2N/A switch (n) {
2N/A
2N/A case 0:
2N/A return (0); /* initial nondigit */
2N/A
2N/A case 1: /* a -- 32 bits */
2N/A break;
2N/A
2N/A case 2: /* a.b -- 8.24 bits */
2N/A if ((val > 0xffffff) || (parts[0] > 0xff))
2N/A return (0);
2N/A val |= parts[0] << 24;
2N/A break;
2N/A
2N/A case 3: /* a.b.c -- 8.8.16 bits */
2N/A if ((val > 0xffff) || (parts[0] > 0xff) || (parts[1] > 0xff))
2N/A return (0);
2N/A val |= (parts[0] << 24) | (parts[1] << 16);
2N/A break;
2N/A
2N/A case 4: /* a.b.c.d -- 8.8.8.8 bits */
2N/A if ((val > 0xff) || (parts[0] > 0xff) || (parts[1] > 0xff) ||
2N/A (parts[2] > 0xff))
2N/A return (0);
2N/A val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
2N/A break;
2N/A }
2N/A if (addr)
2N/A addr->s_addr = htonl(val);
2N/A return (1);
2N/A}
2N/A
2N/A/*
2N/A * Internet address interpretation routine.
2N/A * All the network library routines call this
2N/A * routine to interpret entries in the data bases
2N/A * which are expected to be an address.
2N/A * The value returned is in network order.
2N/A */
2N/Ain_addr_t
2N/Ainet_addr(const char *cp)
2N/A{
2N/A struct in_addr val;
2N/A
2N/A if (inet_aton(cp, &val))
2N/A return (val.s_addr);
2N/A return (INADDR_NONE);
2N/A}
2N/A
2N/A/*
2N/A * Return the network number from an internet
2N/A * address; handles class a/b/c network #'s.
2N/A */
2N/Auint_t
2N/Ainet_netof(struct in_addr in)
2N/A{
2N/A uint32_t i = ntohl(in.s_addr);
2N/A
2N/A if (IN_CLASSA(i))
2N/A return ((i & IN_CLASSA_NET) >> IN_CLASSA_NSHIFT);
2N/A if (IN_CLASSB(i))
2N/A return ((i & IN_CLASSB_NET) >> IN_CLASSB_NSHIFT);
2N/A return ((i & IN_CLASSC_NET) >> IN_CLASSC_NSHIFT);
2N/A}