17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2001 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*8a7aa2a5SYuri Pankov /*	All Rights Reserved	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
327c478bd9Sstevel@tonic-gate  * The Regents of the University of California
337c478bd9Sstevel@tonic-gate  * All Rights Reserved
347c478bd9Sstevel@tonic-gate  *
357c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
367c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
377c478bd9Sstevel@tonic-gate  * contributors.
387c478bd9Sstevel@tonic-gate  */
397c478bd9Sstevel@tonic-gate 
40*8a7aa2a5SYuri Pankov #include "lint.h"
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #include <sys/types.h>
43*8a7aa2a5SYuri Pankov 
447c478bd9Sstevel@tonic-gate #include <netinet/in.h>
457c478bd9Sstevel@tonic-gate 
46*8a7aa2a5SYuri Pankov #include <ctype.h>
47*8a7aa2a5SYuri Pankov 
487c478bd9Sstevel@tonic-gate /*
497c478bd9Sstevel@tonic-gate  * Internet network address interpretation routine.
507c478bd9Sstevel@tonic-gate  * The library routines call this routine to interpret
517c478bd9Sstevel@tonic-gate  * network numbers.
527c478bd9Sstevel@tonic-gate  */
537c478bd9Sstevel@tonic-gate in_addr_t
547c478bd9Sstevel@tonic-gate inet_network(const char *cp)
557c478bd9Sstevel@tonic-gate {
567c478bd9Sstevel@tonic-gate 	in_addr_t val;
577c478bd9Sstevel@tonic-gate 	int base;
587c478bd9Sstevel@tonic-gate 	ptrdiff_t n;
597c478bd9Sstevel@tonic-gate 	char c;
607c478bd9Sstevel@tonic-gate 	in_addr_t parts[4], *pp = parts;
617c478bd9Sstevel@tonic-gate 	int i;
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate again:
647c478bd9Sstevel@tonic-gate 	val = 0; base = 10;
657c478bd9Sstevel@tonic-gate 	if (*cp == '0') {
667c478bd9Sstevel@tonic-gate 		if (*++cp == 'x' || *cp == 'X')
677c478bd9Sstevel@tonic-gate 			base = 16, cp++;
687c478bd9Sstevel@tonic-gate 		else
697c478bd9Sstevel@tonic-gate 			base = 8;
707c478bd9Sstevel@tonic-gate 	}
717c478bd9Sstevel@tonic-gate 	while ((c = *cp) != NULL) {
727c478bd9Sstevel@tonic-gate 		if (isdigit(c)) {
737c478bd9Sstevel@tonic-gate 			if ((c - '0') >= base)
74*8a7aa2a5SYuri Pankov 				break;
757c478bd9Sstevel@tonic-gate 			val = (val * base) + (c - '0');
767c478bd9Sstevel@tonic-gate 			cp++;
777c478bd9Sstevel@tonic-gate 			continue;
787c478bd9Sstevel@tonic-gate 		}
797c478bd9Sstevel@tonic-gate 		if (base == 16 && isxdigit(c)) {
807c478bd9Sstevel@tonic-gate 			val = (val << 4) + (c + 10 - (islower(c) ? 'a' : 'A'));
817c478bd9Sstevel@tonic-gate 			cp++;
827c478bd9Sstevel@tonic-gate 			continue;
837c478bd9Sstevel@tonic-gate 		}
847c478bd9Sstevel@tonic-gate 		break;
857c478bd9Sstevel@tonic-gate 	}
867c478bd9Sstevel@tonic-gate 	if (pp >= parts + 4 || val > 0xff)
877c478bd9Sstevel@tonic-gate 		return ((in_addr_t)-1);
887c478bd9Sstevel@tonic-gate 	*pp++ = val;
897c478bd9Sstevel@tonic-gate 	if (*cp == '.') {
907c478bd9Sstevel@tonic-gate 		cp++;
917c478bd9Sstevel@tonic-gate 		goto again;
927c478bd9Sstevel@tonic-gate 	}
937c478bd9Sstevel@tonic-gate 	if (*cp != '\0' && !isspace(*cp))
947c478bd9Sstevel@tonic-gate 		return ((in_addr_t)-1);
957c478bd9Sstevel@tonic-gate 	n = pp - parts;
967c478bd9Sstevel@tonic-gate 	for (val = 0, i = 0; i < n; i++) {
977c478bd9Sstevel@tonic-gate 		val <<= 8;
987c478bd9Sstevel@tonic-gate 		val |= parts[i];
997c478bd9Sstevel@tonic-gate 	}
1007c478bd9Sstevel@tonic-gate 	return (val);
1017c478bd9Sstevel@tonic-gate }
102