xref: /original-bsd/lib/libc/net/inet_addr.c (revision 7f897caf)
1 /*
2  * Copyright (c) 1983, 1990 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #if defined(LIBC_SCCS) && !defined(lint)
9 static char sccsid[] = "@(#)inet_addr.c	5.12 (Berkeley) 04/12/93";
10 #endif /* LIBC_SCCS and not lint */
11 
12 #include <sys/param.h>
13 #include <netinet/in.h>
14 #include <arpa/inet.h>
15 #include <ctype.h>
16 
17 /*
18  * Ascii internet address interpretation routine.
19  * The value returned is in network order.
20  */
21 u_long
22 inet_addr(cp)
23 	register const char *cp;
24 {
25 	struct in_addr val;
26 
27 	if (inet_aton(cp, &val))
28 		return (val.s_addr);
29 	return (INADDR_NONE);
30 }
31 
32 /*
33  * Check whether "cp" is a valid ascii representation
34  * of an Internet address and convert to a binary address.
35  * Returns 1 if the address is valid, 0 if not.
36  * This replaces inet_addr, the return value from which
37  * cannot distinguish between failure and a local broadcast address.
38  */
39 int
40 inet_aton(cp, addr)
41 	register const char *cp;
42 	struct in_addr *addr;
43 {
44 	register u_long val, base, n;
45 	register char c;
46 	u_long parts[4], *pp = parts;
47 
48 	for (;;) {
49 		/*
50 		 * Collect number up to ``.''.
51 		 * Values are specified as for C:
52 		 * 0x=hex, 0=octal, other=decimal.
53 		 */
54 		val = 0; base = 10;
55 		if (*cp == '0') {
56 			if (*++cp == 'x' || *cp == 'X')
57 				base = 16, cp++;
58 			else
59 				base = 8;
60 		}
61 		while ((c = *cp) != '\0') {
62 			if (isascii(c) && isdigit(c)) {
63 				val = (val * base) + (c - '0');
64 				cp++;
65 				continue;
66 			}
67 			if (base == 16 && isascii(c) && isxdigit(c)) {
68 				val = (val << 4) +
69 					(c + 10 - (islower(c) ? 'a' : 'A'));
70 				cp++;
71 				continue;
72 			}
73 			break;
74 		}
75 		if (*cp == '.') {
76 			/*
77 			 * Internet format:
78 			 *	a.b.c.d
79 			 *	a.b.c	(with c treated as 16-bits)
80 			 *	a.b	(with b treated as 24 bits)
81 			 */
82 			if (pp >= parts + 3 || val > 0xff)
83 				return (0);
84 			*pp++ = val, cp++;
85 		} else
86 			break;
87 	}
88 	/*
89 	 * Check for trailing characters.
90 	 */
91 	if (*cp && (!isascii(*cp) || !isspace(*cp)))
92 		return (0);
93 	/*
94 	 * Concoct the address according to
95 	 * the number of parts specified.
96 	 */
97 	n = pp - parts + 1;
98 	switch (n) {
99 
100 	case 1:				/* a -- 32 bits */
101 		break;
102 
103 	case 2:				/* a.b -- 8.24 bits */
104 		if (val > 0xffffff)
105 			return (0);
106 		val |= parts[0] << 24;
107 		break;
108 
109 	case 3:				/* a.b.c -- 8.8.16 bits */
110 		if (val > 0xffff)
111 			return (0);
112 		val |= (parts[0] << 24) | (parts[1] << 16);
113 		break;
114 
115 	case 4:				/* a.b.c.d -- 8.8.8.8 bits */
116 		if (val > 0xff)
117 			return (0);
118 		val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
119 		break;
120 	}
121 	if (addr)
122 		addr->s_addr = htonl(val);
123 	return (1);
124 }
125