File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / ntp / lib / isc / inet_aton.c
Revision 1.1: download - view: text, annotated - select for diffs - revision graph
Tue May 29 12:08:38 2012 UTC (12 years, 1 month ago) by misho
CVS tags: MAIN, HEAD
Initial revision

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>