]>
Commit | Line | Data |
---|---|---|
fa0bc87c RM |
1 | /* Copyright (c) 1996 by Internet Software Consortium. |
2 | * | |
3 | * Permission to use, copy, modify, and distribute this software for any | |
4 | * purpose with or without fee is hereby granted, provided that the above | |
5 | * copyright notice and this permission notice appear in all copies. | |
6 | * | |
7 | * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS | |
8 | * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES | |
9 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE | |
10 | * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL | |
11 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR | |
12 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS | |
13 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS | |
14 | * SOFTWARE. | |
15 | */ | |
16 | ||
17 | #if defined(LIBC_SCCS) && !defined(lint) | |
18 | static char rcsid[] = "$Id$"; | |
19 | #endif /* LIBC_SCCS and not lint */ | |
20 | ||
21 | #include <sys/param.h> | |
22 | #include <sys/types.h> | |
23 | #include <sys/socket.h> | |
24 | #include <netinet/in.h> | |
25 | #include <arpa/inet.h> | |
26 | #include <arpa/nameser.h> | |
27 | #include <string.h> | |
28 | #include <errno.h> | |
86187531 | 29 | #include <conf/portability.h> |
fa0bc87c RM |
30 | |
31 | /* | |
32 | * WARNING: Don't even consider trying to compile this on a system where | |
33 | * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX. | |
34 | */ | |
35 | ||
dfd2257a UD |
36 | static int inet_pton4 __P((const char *src, u_char *dst)) internal_function; |
37 | static int inet_pton6 __P((const char *src, u_char *dst)) internal_function; | |
fa0bc87c RM |
38 | |
39 | /* int | |
613a76ff | 40 | * inet_pton(af, src, dst) |
fa0bc87c RM |
41 | * convert from presentation format (which usually means ASCII printable) |
42 | * to network format (which is usually some kind of binary format). | |
43 | * return: | |
44 | * 1 if the address was valid for the specified address family | |
45 | * 0 if the address wasn't valid (`dst' is untouched in this case) | |
46 | * -1 if some other error occurred (`dst' is untouched in this case, too) | |
47 | * author: | |
48 | * Paul Vixie, 1996. | |
49 | */ | |
50 | int | |
613a76ff | 51 | inet_pton(af, src, dst) |
fa0bc87c RM |
52 | int af; |
53 | const char *src; | |
54 | void *dst; | |
fa0bc87c RM |
55 | { |
56 | switch (af) { | |
57 | case AF_INET: | |
fa0bc87c RM |
58 | return (inet_pton4(src, dst)); |
59 | case AF_INET6: | |
fa0bc87c RM |
60 | return (inet_pton6(src, dst)); |
61 | default: | |
c4029823 | 62 | __set_errno (EAFNOSUPPORT); |
fa0bc87c RM |
63 | return (-1); |
64 | } | |
65 | /* NOTREACHED */ | |
66 | } | |
67 | ||
68 | /* int | |
69 | * inet_pton4(src, dst) | |
df21c858 | 70 | * like inet_aton() but without all the hexadecimal and shorthand. |
fa0bc87c RM |
71 | * return: |
72 | * 1 if `src' is a valid dotted quad, else 0. | |
73 | * notice: | |
74 | * does not touch `dst' unless it's returning 1. | |
75 | * author: | |
76 | * Paul Vixie, 1996. | |
77 | */ | |
78 | static int | |
dfd2257a | 79 | internal_function |
fa0bc87c RM |
80 | inet_pton4(src, dst) |
81 | const char *src; | |
82 | u_char *dst; | |
83 | { | |
fa0bc87c RM |
84 | int saw_digit, octets, ch; |
85 | u_char tmp[INADDRSZ], *tp; | |
86 | ||
87 | saw_digit = 0; | |
88 | octets = 0; | |
89 | *(tp = tmp) = 0; | |
90 | while ((ch = *src++) != '\0') { | |
fa0bc87c | 91 | |
61fab08a UD |
92 | if (ch >= '0' && ch <= '9') { |
93 | u_int new = *tp * 10 + (ch - '0'); | |
fa0bc87c RM |
94 | |
95 | if (new > 255) | |
96 | return (0); | |
97 | *tp = new; | |
98 | if (! saw_digit) { | |
99 | if (++octets > 4) | |
100 | return (0); | |
101 | saw_digit = 1; | |
102 | } | |
103 | } else if (ch == '.' && saw_digit) { | |
104 | if (octets == 4) | |
105 | return (0); | |
106 | *++tp = 0; | |
107 | saw_digit = 0; | |
108 | } else | |
109 | return (0); | |
110 | } | |
111 | if (octets < 4) | |
112 | return (0); | |
df21c858 UD |
113 | |
114 | memcpy(dst, tmp, INADDRSZ); | |
fa0bc87c RM |
115 | return (1); |
116 | } | |
117 | ||
118 | /* int | |
119 | * inet_pton6(src, dst) | |
120 | * convert presentation level address to network order binary form. | |
121 | * return: | |
122 | * 1 if `src' is a valid [RFC1884 2.2] address, else 0. | |
123 | * notice: | |
124 | * (1) does not touch `dst' unless it's returning 1. | |
125 | * (2) :: in a full address is silently ignored. | |
126 | * credit: | |
127 | * inspired by Mark Andrews. | |
128 | * author: | |
129 | * Paul Vixie, 1996. | |
130 | */ | |
131 | static int | |
dfd2257a | 132 | internal_function |
fa0bc87c RM |
133 | inet_pton6(src, dst) |
134 | const char *src; | |
135 | u_char *dst; | |
136 | { | |
61fab08a | 137 | static const char xdigits[] = "0123456789abcdef"; |
fa0bc87c | 138 | u_char tmp[IN6ADDRSZ], *tp, *endp, *colonp; |
61fab08a | 139 | const char *curtok; |
fa0bc87c RM |
140 | int ch, saw_xdigit; |
141 | u_int val; | |
142 | ||
61fab08a | 143 | tp = memset(tmp, '\0', IN6ADDRSZ); |
fa0bc87c RM |
144 | endp = tp + IN6ADDRSZ; |
145 | colonp = NULL; | |
146 | /* Leading :: requires some special handling. */ | |
147 | if (*src == ':') | |
148 | if (*++src != ':') | |
149 | return (0); | |
150 | curtok = src; | |
151 | saw_xdigit = 0; | |
152 | val = 0; | |
61fab08a | 153 | while ((ch = tolower (*src++)) != '\0') { |
fa0bc87c RM |
154 | const char *pch; |
155 | ||
61fab08a | 156 | pch = strchr(xdigits, ch); |
fa0bc87c RM |
157 | if (pch != NULL) { |
158 | val <<= 4; | |
159 | val |= (pch - xdigits); | |
160 | if (val > 0xffff) | |
161 | return (0); | |
162 | saw_xdigit = 1; | |
163 | continue; | |
164 | } | |
165 | if (ch == ':') { | |
166 | curtok = src; | |
167 | if (!saw_xdigit) { | |
168 | if (colonp) | |
169 | return (0); | |
170 | colonp = tp; | |
171 | continue; | |
172 | } | |
173 | if (tp + INT16SZ > endp) | |
174 | return (0); | |
175 | *tp++ = (u_char) (val >> 8) & 0xff; | |
176 | *tp++ = (u_char) val & 0xff; | |
177 | saw_xdigit = 0; | |
178 | val = 0; | |
179 | continue; | |
180 | } | |
181 | if (ch == '.' && ((tp + INADDRSZ) <= endp) && | |
182 | inet_pton4(curtok, tp) > 0) { | |
183 | tp += INADDRSZ; | |
184 | saw_xdigit = 0; | |
185 | break; /* '\0' was seen by inet_pton4(). */ | |
186 | } | |
187 | return (0); | |
188 | } | |
189 | if (saw_xdigit) { | |
190 | if (tp + INT16SZ > endp) | |
191 | return (0); | |
192 | *tp++ = (u_char) (val >> 8) & 0xff; | |
193 | *tp++ = (u_char) val & 0xff; | |
194 | } | |
195 | if (colonp != NULL) { | |
196 | /* | |
197 | * Since some memmove()'s erroneously fail to handle | |
198 | * overlapping regions, we'll do the shift by hand. | |
199 | */ | |
613a76ff | 200 | const int n = tp - colonp; |
fa0bc87c RM |
201 | int i; |
202 | ||
613a76ff RM |
203 | for (i = 1; i <= n; i++) { |
204 | endp[- i] = colonp[n - i]; | |
205 | colonp[n - i] = 0; | |
fa0bc87c RM |
206 | } |
207 | tp = endp; | |
208 | } | |
209 | if (tp != endp) | |
210 | return (0); | |
df21c858 | 211 | memcpy(dst, tmp, IN6ADDRSZ); |
fa0bc87c RM |
212 | return (1); |
213 | } |