1 /* $Id: inet_aton.c 5049 2001-12-12 09:06:00Z rra $
3 ** Replacement for a missing inet_aton.
5 ** Written by Russ Allbery <rra@stanford.edu>
6 ** This work is hereby placed in the public domain by its author.
8 ** Provides the same functionality as the standard library routine
9 ** inet_aton for those platforms that don't have it. inet_aton is
15 #include <netinet/in.h>
17 /* If we're running the test suite, rename inet_ntoa to avoid conflicts with
18 the system version. */
20 # define inet_aton test_inet_aton
21 int test_inet_aton(const char *, struct in_addr *);
25 inet_aton(const char *s, struct in_addr *addr)
27 unsigned long octet[4], address;
32 if (s == NULL) return 0;
34 /* Step through each period-separated part of the address. If we see
35 more than four parts, the address is invalid. */
36 for (p = s; *p != 0; part++) {
37 if (part > 3) return 0;
39 /* Determine the base of the section we're looking at. Numbers are
40 represented the same as in C; octal starts with 0, hex starts
41 with 0x, and anything else is decimal. */
54 /* Make sure there's actually a number. (A section of just "0"
55 would set base to 8 and leave us pointing at a period; allow
57 if (*p == '.' && base != 8) return 0;
60 /* Now, parse this segment of the address. For each digit, multiply
61 the result so far by the base and then add the value of the
62 digit. Be careful of arithmetic overflow in cases where an
63 unsigned long is 32 bits; we need to detect it *before* we
64 multiply by the base since otherwise we could overflow and wrap
65 and then not detect the error. */
66 for (; *p != 0 && *p != '.'; p++) {
67 if (octet[part] > 0xffffffffUL / base) return 0;
69 /* Use a switch statement to parse each digit rather than
70 assuming ASCII. Probably pointless portability.... */
72 case '0': i = 0; break;
73 case '1': i = 1; break;
74 case '2': i = 2; break;
75 case '3': i = 3; break;
76 case '4': i = 4; break;
77 case '5': i = 5; break;
78 case '6': i = 6; break;
79 case '7': i = 7; break;
80 case '8': i = 8; break;
81 case '9': i = 9; break;
82 case 'A': case 'a': i = 10; break;
83 case 'B': case 'b': i = 11; break;
84 case 'C': case 'c': i = 12; break;
85 case 'D': case 'd': i = 13; break;
86 case 'E': case 'e': i = 14; break;
87 case 'F': case 'f': i = 15; break;
90 if (i >= base) return 0;
91 octet[part] = (octet[part] * base) + i;
94 /* Advance over periods; the top of the loop will increment the
95 count of parts we've seen. We need a check here to detect an
96 illegal trailing period. */
99 if (*p == 0) return 0;
102 if (part == 0) return 0;
104 /* IPv4 allows three types of address specification:
110 If there are fewer than four segments, the final segment accounts for
111 all of the remaining portion of the address. For example, in the a.b
112 form, b is the final 24 bits of the address. We also allow a simple
113 number, which is interpreted as the 32-bit number corresponding to
114 the full IPv4 address.
116 The first for loop below ensures that any initial segments represent
117 only 8 bits of the address and builds the upper portion of the IPv4
118 address. Then, the remaining segment is checked to make sure it's no
119 bigger than the remaining space in the address and then is added into
122 for (i = 0; i < part - 1; i++) {
123 if (octet[i] > 0xff) return 0;
124 address |= octet[i] << (8 * (3 - i));
126 if (octet[i] > (0xffffffffUL >> (i * 8))) return 0;
128 if (addr != NULL) addr->s_addr = htonl(address);