Commit | Line | Data |
---|---|---|
bb0cfa24 | 1 | /* |
177ad8dd | 2 | * Copyright (c) 1983, 1990 Regents of the University of California. |
140b4f5f KB |
3 | * All rights reserved. |
4 | * | |
269a7923 | 5 | * %sccs.include.redist.c% |
bb0cfa24 DF |
6 | */ |
7 | ||
2ce81398 | 8 | #if defined(LIBC_SCCS) && !defined(lint) |
bc3b908a | 9 | static char sccsid[] = "@(#)inet_addr.c 5.11 (Berkeley) %G%"; |
140b4f5f | 10 | #endif /* LIBC_SCCS and not lint */ |
5e649950 | 11 | |
24fac7d8 | 12 | #include <sys/param.h> |
056d5bbd | 13 | #include <netinet/in.h> |
24fac7d8 | 14 | #include <arpa/inet.h> |
dcdb79c9 | 15 | #include <ctype.h> |
5e649950 | 16 | |
ff79189c | 17 | /* |
177ad8dd | 18 | * Ascii internet address interpretation routine. |
d6c70669 | 19 | * The value returned is in network order. |
ff79189c | 20 | */ |
b2053919 | 21 | u_long |
ff79189c | 22 | inet_addr(cp) |
24fac7d8 | 23 | register const char *cp; |
177ad8dd MK |
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 | */ | |
bc3b908a | 39 | unsigned long |
177ad8dd | 40 | inet_aton(cp, addr) |
bc3b908a | 41 | register const char *cp; |
177ad8dd | 42 | struct in_addr *addr; |
ff79189c | 43 | { |
d6c70669 | 44 | register u_long val, base, n; |
ff79189c | 45 | register char c; |
d6c70669 | 46 | u_long parts[4], *pp = parts; |
ff79189c | 47 | |
177ad8dd | 48 | for (;;) { |
ff79189c | 49 | /* |
177ad8dd MK |
50 | * Collect number up to ``.''. |
51 | * Values are specified as for C: | |
52 | * 0x=hex, 0=octal, other=decimal. | |
ff79189c | 53 | */ |
177ad8dd MK |
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; | |
ff79189c | 87 | } |
d6c70669 SL |
88 | /* |
89 | * Check for trailing characters. | |
90 | */ | |
177ad8dd MK |
91 | if (*cp && (!isascii(*cp) || !isspace(*cp))) |
92 | return (0); | |
d6c70669 SL |
93 | /* |
94 | * Concoct the address according to | |
95 | * the number of parts specified. | |
96 | */ | |
177ad8dd | 97 | n = pp - parts + 1; |
d6c70669 SL |
98 | switch (n) { |
99 | ||
100 | case 1: /* a -- 32 bits */ | |
d6c70669 SL |
101 | break; |
102 | ||
103 | case 2: /* a.b -- 8.24 bits */ | |
177ad8dd MK |
104 | if (val > 0xffffff) |
105 | return (0); | |
106 | val |= parts[0] << 24; | |
d6c70669 SL |
107 | break; |
108 | ||
109 | case 3: /* a.b.c -- 8.8.16 bits */ | |
177ad8dd MK |
110 | if (val > 0xffff) |
111 | return (0); | |
112 | val |= (parts[0] << 24) | (parts[1] << 16); | |
d6c70669 SL |
113 | break; |
114 | ||
115 | case 4: /* a.b.c.d -- 8.8.8.8 bits */ | |
177ad8dd MK |
116 | if (val > 0xff) |
117 | return (0); | |
118 | val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8); | |
d6c70669 | 119 | break; |
ff79189c | 120 | } |
177ad8dd MK |
121 | if (addr) |
122 | addr->s_addr = htonl(val); | |
123 | return (1); | |
ff79189c | 124 | } |