Home | History | Annotate | Line # | Download | only in inet
inet_addr.c revision 1.4
      1  1.4     kamil /*	$NetBSD: inet_addr.c,v 1.4 2018/07/26 00:20:41 kamil Exp $	*/
      2  1.1  christos 
      3  1.1  christos /*
      4  1.1  christos  * Copyright (c) 1983, 1990, 1993
      5  1.1  christos  *    The Regents of the University of California.  All rights reserved.
      6  1.1  christos  *
      7  1.1  christos  * Redistribution and use in source and binary forms, with or without
      8  1.1  christos  * modification, are permitted provided that the following conditions
      9  1.1  christos  * are met:
     10  1.1  christos  * 1. Redistributions of source code must retain the above copyright
     11  1.1  christos  *    notice, this list of conditions and the following disclaimer.
     12  1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  christos  *    documentation and/or other materials provided with the distribution.
     15  1.1  christos  * 3. All advertising materials mentioning features or use of this software
     16  1.1  christos  *    must display the following acknowledgement:
     17  1.1  christos  * 	This product includes software developed by the University of
     18  1.1  christos  * 	California, Berkeley and its contributors.
     19  1.1  christos  * 4. Neither the name of the University nor the names of its contributors
     20  1.1  christos  *    may be used to endorse or promote products derived from this software
     21  1.1  christos  *    without specific prior written permission.
     22  1.1  christos  *
     23  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  1.1  christos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  1.1  christos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  1.1  christos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  1.1  christos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  1.1  christos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  1.1  christos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  1.1  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  1.1  christos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  1.1  christos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  1.1  christos  * SUCH DAMAGE.
     34  1.1  christos  */
     35  1.1  christos 
     36  1.1  christos /*
     37  1.1  christos  * Portions Copyright (c) 1993 by Digital Equipment Corporation.
     38  1.1  christos  *
     39  1.1  christos  * Permission to use, copy, modify, and distribute this software for any
     40  1.1  christos  * purpose with or without fee is hereby granted, provided that the above
     41  1.1  christos  * copyright notice and this permission notice appear in all copies, and that
     42  1.1  christos  * the name of Digital Equipment Corporation not be used in advertising or
     43  1.1  christos  * publicity pertaining to distribution of the document or software without
     44  1.1  christos  * specific, written prior permission.
     45  1.1  christos  *
     46  1.1  christos  * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
     47  1.1  christos  * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
     48  1.1  christos  * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
     49  1.1  christos  * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
     50  1.1  christos  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
     51  1.1  christos  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
     52  1.1  christos  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
     53  1.1  christos  * SOFTWARE.
     54  1.1  christos  */
     55  1.1  christos 
     56  1.1  christos /*
     57  1.1  christos  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
     58  1.1  christos  * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
     59  1.1  christos  *
     60  1.1  christos  * Permission to use, copy, modify, and distribute this software for any
     61  1.1  christos  * purpose with or without fee is hereby granted, provided that the above
     62  1.1  christos  * copyright notice and this permission notice appear in all copies.
     63  1.1  christos  *
     64  1.1  christos  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
     65  1.1  christos  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     66  1.1  christos  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
     67  1.1  christos  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     68  1.1  christos  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     69  1.1  christos  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
     70  1.1  christos  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     71  1.1  christos  */
     72  1.1  christos 
     73  1.1  christos #if !defined(_KERNEL) && !defined(_STANDALONE)
     74  1.1  christos #include <sys/cdefs.h>
     75  1.1  christos #if defined(LIBC_SCCS) && !defined(lint)
     76  1.1  christos #if 0
     77  1.1  christos static const char sccsid[] = "@(#)inet_addr.c	8.1 (Berkeley) 6/17/93";
     78  1.1  christos static const char rcsid[] = "Id: inet_addr.c,v 1.2.206.2 2004/03/17 00:29:45 marka Exp";
     79  1.1  christos #else
     80  1.4     kamil __RCSID("$NetBSD: inet_addr.c,v 1.4 2018/07/26 00:20:41 kamil Exp $");
     81  1.1  christos #endif
     82  1.1  christos #endif /* LIBC_SCCS and not lint */
     83  1.1  christos 
     84  1.1  christos #include "port_before.h"
     85  1.1  christos 
     86  1.1  christos #include "namespace.h"
     87  1.1  christos #include <sys/types.h>
     88  1.1  christos #include <sys/param.h>
     89  1.1  christos 
     90  1.1  christos #include <netinet/in.h>
     91  1.1  christos #include <arpa/inet.h>
     92  1.1  christos 
     93  1.1  christos #include <ctype.h>
     94  1.1  christos 
     95  1.1  christos #include "port_after.h"
     96  1.1  christos 
     97  1.1  christos #ifdef __weak_alias
     98  1.1  christos __weak_alias(inet_aton,_inet_aton)
     99  1.1  christos #endif
    100  1.1  christos #else
    101  1.1  christos #include <lib/libkern/libkern.h>
    102  1.1  christos #include <netinet/in.h>
    103  1.1  christos #endif
    104  1.1  christos 
    105  1.1  christos /*
    106  1.1  christos  * Ascii internet address interpretation routine.
    107  1.1  christos  * The value returned is in network order.
    108  1.1  christos  */
    109  1.2       apb uint32_t
    110  1.1  christos inet_addr(const char *cp) {
    111  1.1  christos 	struct in_addr val;
    112  1.1  christos 
    113  1.1  christos 	if (inet_aton(cp, &val))
    114  1.1  christos 		return (val.s_addr);
    115  1.1  christos 	return (INADDR_NONE);
    116  1.1  christos }
    117  1.1  christos 
    118  1.1  christos /*
    119  1.1  christos  * Check whether "cp" is a valid ascii representation
    120  1.1  christos  * of an Internet address and convert to a binary address.
    121  1.1  christos  * Returns 1 if the address is valid, 0 if not.
    122  1.1  christos  * This replaces inet_addr, the return value from which
    123  1.1  christos  * cannot distinguish between failure and a local broadcast address.
    124  1.1  christos  */
    125  1.1  christos int
    126  1.1  christos inet_aton(const char *cp, struct in_addr *addr) {
    127  1.2       apb 	uint32_t val;
    128  1.3  christos 	int base;
    129  1.3  christos 	size_t n;
    130  1.1  christos 	char c;
    131  1.2       apb 	uint8_t parts[4];
    132  1.2       apb 	uint8_t *pp = parts;
    133  1.1  christos 	int digit;
    134  1.1  christos 
    135  1.1  christos 	c = *cp;
    136  1.1  christos 	for (;;) {
    137  1.1  christos 		/*
    138  1.1  christos 		 * Collect number up to ``.''.
    139  1.1  christos 		 * Values are specified as for C:
    140  1.1  christos 		 * 0x=hex, 0=octal, isdigit=decimal.
    141  1.1  christos 		 */
    142  1.1  christos 		if (!isdigit((unsigned char)c))
    143  1.1  christos 			return (0);
    144  1.1  christos 		val = 0; base = 10; digit = 0;
    145  1.1  christos 		if (c == '0') {
    146  1.1  christos 			c = *++cp;
    147  1.1  christos 			if (c == 'x' || c == 'X')
    148  1.1  christos 				base = 16, c = *++cp;
    149  1.1  christos 			else {
    150  1.1  christos 				base = 8;
    151  1.1  christos 				digit = 1 ;
    152  1.1  christos 			}
    153  1.1  christos 		}
    154  1.1  christos 		for (;;) {
    155  1.1  christos 			if (isascii(c) && isdigit((unsigned char)c)) {
    156  1.1  christos 				if (base == 8 && (c == '8' || c == '9'))
    157  1.1  christos 					return (0);
    158  1.1  christos 				val = (val * base) + (c - '0');
    159  1.1  christos 				c = *++cp;
    160  1.1  christos 				digit = 1;
    161  1.1  christos 			} else if (base == 16 && isascii(c) &&
    162  1.1  christos 				   isxdigit((unsigned char)c)) {
    163  1.1  christos 				val = (val << 4) |
    164  1.1  christos 					(c + 10 - (islower((unsigned char)c) ? 'a' : 'A'));
    165  1.1  christos 				c = *++cp;
    166  1.1  christos 				digit = 1;
    167  1.1  christos 			} else
    168  1.1  christos 				break;
    169  1.1  christos 		}
    170  1.1  christos 		if (c == '.') {
    171  1.1  christos 			/*
    172  1.1  christos 			 * Internet format:
    173  1.1  christos 			 *	a.b.c.d
    174  1.1  christos 			 *	a.b.c	(with c treated as 16 bits)
    175  1.1  christos 			 *	a.b	(with b treated as 24 bits)
    176  1.1  christos 			 */
    177  1.1  christos 			if (pp >= parts + 3 || val > 0xffU)
    178  1.1  christos 				return (0);
    179  1.1  christos 			*pp++ = val;
    180  1.1  christos 			c = *++cp;
    181  1.1  christos 		} else
    182  1.1  christos 			break;
    183  1.1  christos 	}
    184  1.1  christos 	/*
    185  1.1  christos 	 * Check for trailing characters.
    186  1.1  christos 	 */
    187  1.1  christos 	if (c != '\0' && (!isascii(c) || !isspace((unsigned char)c)))
    188  1.1  christos 		return (0);
    189  1.1  christos 	/*
    190  1.1  christos 	 * Did we get a valid digit?
    191  1.1  christos 	 */
    192  1.1  christos 	if (!digit)
    193  1.1  christos 		return (0);
    194  1.1  christos 	/*
    195  1.1  christos 	 * Concoct the address according to
    196  1.1  christos 	 * the number of parts specified.
    197  1.1  christos 	 */
    198  1.1  christos 	n = pp - parts + 1;
    199  1.1  christos 	switch (n) {
    200  1.1  christos 	case 1:				/* a -- 32 bits */
    201  1.1  christos 		break;
    202  1.1  christos 
    203  1.1  christos 	case 2:				/* a.b -- 8.24 bits */
    204  1.1  christos 		if (val > 0xffffffU)
    205  1.1  christos 			return (0);
    206  1.4     kamil 		val |= (uint32_t)parts[0] << 24;
    207  1.1  christos 		break;
    208  1.1  christos 
    209  1.1  christos 	case 3:				/* a.b.c -- 8.8.16 bits */
    210  1.1  christos 		if (val > 0xffffU)
    211  1.1  christos 			return (0);
    212  1.4     kamil 		val |= ((uint32_t)parts[0] << 24) | (parts[1] << 16);
    213  1.1  christos 		break;
    214  1.1  christos 
    215  1.1  christos 	case 4:				/* a.b.c.d -- 8.8.8.8 bits */
    216  1.1  christos 		if (val > 0xffU)
    217  1.1  christos 			return (0);
    218  1.4     kamil 		val |= ((uint32_t)parts[0] << 24) | (parts[1] << 16) |
    219  1.4     kamil 		        (parts[2] << 8);
    220  1.1  christos 		break;
    221  1.1  christos 	}
    222  1.1  christos 	if (addr != NULL)
    223  1.1  christos 		addr->s_addr = htonl(val);
    224  1.1  christos 	return (1);
    225  1.1  christos }
    226