diff options
Diffstat (limited to 'src/lib/libc/net/inet_net_pton.c')
-rw-r--r-- | src/lib/libc/net/inet_net_pton.c | 199 |
1 files changed, 199 insertions, 0 deletions
diff --git a/src/lib/libc/net/inet_net_pton.c b/src/lib/libc/net/inet_net_pton.c new file mode 100644 index 0000000000..e544ca7e28 --- /dev/null +++ b/src/lib/libc/net/inet_net_pton.c | |||
@@ -0,0 +1,199 @@ | |||
1 | /* $OpenBSD: inet_net_pton.c,v 1.4 2005/03/25 13:24:12 otto Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (c) 1996 by Internet Software Consortium. | ||
5 | * | ||
6 | * Permission to use, copy, modify, and distribute this software for any | ||
7 | * purpose with or without fee is hereby granted, provided that the above | ||
8 | * copyright notice and this permission notice appear in all copies. | ||
9 | * | ||
10 | * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS | ||
11 | * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES | ||
12 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE | ||
13 | * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL | ||
14 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR | ||
15 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS | ||
16 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS | ||
17 | * SOFTWARE. | ||
18 | */ | ||
19 | |||
20 | #if defined(LIBC_SCCS) && !defined(lint) | ||
21 | #if 0 | ||
22 | static const char rcsid[] = "$From: inet_net_pton.c,v 8.3 1996/11/11 06:36:52 vixie Exp $"; | ||
23 | #else | ||
24 | static const char rcsid[] = "$OpenBSD: inet_net_pton.c,v 1.4 2005/03/25 13:24:12 otto Exp $"; | ||
25 | #endif | ||
26 | #endif | ||
27 | |||
28 | #include <sys/types.h> | ||
29 | #include <sys/socket.h> | ||
30 | #include <netinet/in.h> | ||
31 | #include <arpa/inet.h> | ||
32 | |||
33 | #include <assert.h> | ||
34 | #include <ctype.h> | ||
35 | #include <errno.h> | ||
36 | #include <stdio.h> | ||
37 | #include <string.h> | ||
38 | #include <stdlib.h> | ||
39 | |||
40 | static int inet_net_pton_ipv4(const char *, u_char *, size_t); | ||
41 | |||
42 | /* | ||
43 | * static int | ||
44 | * inet_net_pton(af, src, dst, size) | ||
45 | * convert network number from presentation to network format. | ||
46 | * accepts hex octets, hex strings, decimal octets, and /CIDR. | ||
47 | * "size" is in bytes and describes "dst". | ||
48 | * return: | ||
49 | * number of bits, either imputed classfully or specified with /CIDR, | ||
50 | * or -1 if some failure occurred (check errno). ENOENT means it was | ||
51 | * not a valid network specification. | ||
52 | * author: | ||
53 | * Paul Vixie (ISC), June 1996 | ||
54 | */ | ||
55 | int | ||
56 | inet_net_pton(int af, const char *src, void *dst, size_t size) | ||
57 | { | ||
58 | switch (af) { | ||
59 | case AF_INET: | ||
60 | return (inet_net_pton_ipv4(src, dst, size)); | ||
61 | default: | ||
62 | errno = EAFNOSUPPORT; | ||
63 | return (-1); | ||
64 | } | ||
65 | } | ||
66 | |||
67 | /* | ||
68 | * static int | ||
69 | * inet_net_pton_ipv4(src, dst, size) | ||
70 | * convert IPv4 network number from presentation to network format. | ||
71 | * accepts hex octets, hex strings, decimal octets, and /CIDR. | ||
72 | * "size" is in bytes and describes "dst". | ||
73 | * return: | ||
74 | * number of bits, either imputed classfully or specified with /CIDR, | ||
75 | * or -1 if some failure occurred (check errno). ENOENT means it was | ||
76 | * not an IPv4 network specification. | ||
77 | * note: | ||
78 | * network byte order assumed. this means 192.5.5.240/28 has | ||
79 | * 0x11110000 in its fourth octet. | ||
80 | * author: | ||
81 | * Paul Vixie (ISC), June 1996 | ||
82 | */ | ||
83 | static int | ||
84 | inet_net_pton_ipv4(const char *src, u_char *dst, size_t size) | ||
85 | { | ||
86 | static const char | ||
87 | xdigits[] = "0123456789abcdef", | ||
88 | digits[] = "0123456789"; | ||
89 | int n, ch, tmp, dirty, bits; | ||
90 | const u_char *odst = dst; | ||
91 | |||
92 | ch = *src++; | ||
93 | if (ch == '0' && (src[0] == 'x' || src[0] == 'X') | ||
94 | && isascii(src[1]) && isxdigit(src[1])) { | ||
95 | /* Hexadecimal: Eat nybble string. */ | ||
96 | if (size <= 0) | ||
97 | goto emsgsize; | ||
98 | *dst = 0, dirty = 0; | ||
99 | src++; /* skip x or X. */ | ||
100 | while ((ch = *src++) != '\0' && | ||
101 | isascii(ch) && isxdigit(ch)) { | ||
102 | if (isupper(ch)) | ||
103 | ch = tolower(ch); | ||
104 | n = strchr(xdigits, ch) - xdigits; | ||
105 | assert(n >= 0 && n <= 15); | ||
106 | *dst |= n; | ||
107 | if (!dirty++) | ||
108 | *dst <<= 4; | ||
109 | else if (size-- > 0) | ||
110 | *++dst = 0, dirty = 0; | ||
111 | else | ||
112 | goto emsgsize; | ||
113 | } | ||
114 | if (dirty) | ||
115 | size--; | ||
116 | } else if (isascii(ch) && isdigit(ch)) { | ||
117 | /* Decimal: eat dotted digit string. */ | ||
118 | for (;;) { | ||
119 | tmp = 0; | ||
120 | do { | ||
121 | n = strchr(digits, ch) - digits; | ||
122 | assert(n >= 0 && n <= 9); | ||
123 | tmp *= 10; | ||
124 | tmp += n; | ||
125 | if (tmp > 255) | ||
126 | goto enoent; | ||
127 | } while ((ch = *src++) != '\0' && | ||
128 | isascii(ch) && isdigit(ch)); | ||
129 | if (size-- <= 0) | ||
130 | goto emsgsize; | ||
131 | *dst++ = (u_char) tmp; | ||
132 | if (ch == '\0' || ch == '/') | ||
133 | break; | ||
134 | if (ch != '.') | ||
135 | goto enoent; | ||
136 | ch = *src++; | ||
137 | if (!isascii(ch) || !isdigit(ch)) | ||
138 | goto enoent; | ||
139 | } | ||
140 | } else | ||
141 | goto enoent; | ||
142 | |||
143 | bits = -1; | ||
144 | if (ch == '/' && isascii(src[0]) && isdigit(src[0]) && dst > odst) { | ||
145 | /* CIDR width specifier. Nothing can follow it. */ | ||
146 | ch = *src++; /* Skip over the /. */ | ||
147 | bits = 0; | ||
148 | do { | ||
149 | n = strchr(digits, ch) - digits; | ||
150 | assert(n >= 0 && n <= 9); | ||
151 | bits *= 10; | ||
152 | bits += n; | ||
153 | } while ((ch = *src++) != '\0' && | ||
154 | isascii(ch) && isdigit(ch)); | ||
155 | if (ch != '\0') | ||
156 | goto enoent; | ||
157 | if (bits > 32) | ||
158 | goto emsgsize; | ||
159 | } | ||
160 | |||
161 | /* Firey death and destruction unless we prefetched EOS. */ | ||
162 | if (ch != '\0') | ||
163 | goto enoent; | ||
164 | |||
165 | /* If nothing was written to the destination, we found no address. */ | ||
166 | if (dst == odst) | ||
167 | goto enoent; | ||
168 | /* If no CIDR spec was given, infer width from net class. */ | ||
169 | if (bits == -1) { | ||
170 | if (*odst >= 240) /* Class E */ | ||
171 | bits = 32; | ||
172 | else if (*odst >= 224) /* Class D */ | ||
173 | bits = 4; | ||
174 | else if (*odst >= 192) /* Class C */ | ||
175 | bits = 24; | ||
176 | else if (*odst >= 128) /* Class B */ | ||
177 | bits = 16; | ||
178 | else /* Class A */ | ||
179 | bits = 8; | ||
180 | /* If imputed mask is narrower than specified octets, widen. */ | ||
181 | if (bits < ((dst - odst) * 8)) | ||
182 | bits = (dst - odst) * 8; | ||
183 | } | ||
184 | /* Extend network to cover the actual mask. */ | ||
185 | while (bits > ((dst - odst) * 8)) { | ||
186 | if (size-- <= 0) | ||
187 | goto emsgsize; | ||
188 | *dst++ = '\0'; | ||
189 | } | ||
190 | return (bits); | ||
191 | |||
192 | enoent: | ||
193 | errno = ENOENT; | ||
194 | return (-1); | ||
195 | |||
196 | emsgsize: | ||
197 | errno = EMSGSIZE; | ||
198 | return (-1); | ||
199 | } | ||