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