1 /*
2 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3 *
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
7 *
8 * See the COPYRIGHT file distributed with this work for additional
9 * information regarding copyright ownership.
10 */
11
12 /*! \file lwinetpton.c
13 */
14
15 #include <config.h>
16
17 #include <errno.h>
18 #include <string.h>
19
20 #include <lwres/net.h>
21
22 #define NS_INT16SZ 2
23 #define NS_INADDRSZ 4
24 #define NS_IN6ADDRSZ 16
25
26 /*
27 * WARNING: Don't even consider trying to compile this on a system where
28 * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX.
29 */
30
31 static int inet_pton4(const char *src, unsigned char *dst);
32 static int inet_pton6(const char *src, unsigned char *dst);
33
34 /*!
35 * int
36 * lwres_net_pton(af, src, dst)
37 * convert from presentation format (which usually means ASCII printable)
38 * to network format (which is usually some kind of binary format).
39 * return:
40 * 1 if the address was valid for the specified address family
41 * 0 if the address wasn't valid (`dst' is untouched in this case)
42 * -1 if some other error occurred (`dst' is untouched in this case, too)
43 * author:
44 * Paul Vixie, 1996.
45 */
46 int
lwres_net_pton(int af,const char * src,void * dst)47 lwres_net_pton(int af, const char *src, void *dst) {
48 switch (af) {
49 case AF_INET:
50 return (inet_pton4(src, dst));
51 case AF_INET6:
52 return (inet_pton6(src, dst));
53 default:
54 errno = EAFNOSUPPORT;
55 return (-1);
56 }
57 /* NOTREACHED */
58 }
59
60 /*! int
61 * inet_pton4(src, dst)
62 * like inet_aton() but without all the hexadecimal and shorthand.
63 * return:
64 * 1 if `src' is a valid dotted quad, else 0.
65 * notice:
66 * does not touch `dst' unless it's returning 1.
67 * author:
68 * Paul Vixie, 1996.
69 */
70 static int
inet_pton4(const char * src,unsigned char * dst)71 inet_pton4(const char *src, unsigned char *dst) {
72 static const char digits[] = "0123456789";
73 int saw_digit, octets, ch;
74 unsigned char tmp[NS_INADDRSZ] = { 0 }, *tp;
75
76 saw_digit = 0;
77 octets = 0;
78 tp = tmp;
79 while ((ch = *src++) != '\0') {
80 const char *pch;
81
82 if ((pch = strchr(digits, ch)) != NULL) {
83 unsigned int byte = *tp * 10;
84
85 byte += (unsigned int)(pch - digits);
86 if (byte > 255)
87 return (0);
88 *tp = byte;
89 if (! saw_digit) {
90 if (++octets > 4)
91 return (0);
92 saw_digit = 1;
93 }
94 } else if (ch == '.' && saw_digit) {
95 if (octets == 4)
96 return (0);
97 tp++;
98 saw_digit = 0;
99 } else
100 return (0);
101 }
102 if (octets < 4)
103 return (0);
104 memmove(dst, tmp, NS_INADDRSZ);
105 return (1);
106 }
107
108 /*! int
109 * inet_pton6(src, dst)
110 * convert presentation level address to network order binary form.
111 * return:
112 * 1 if `src' is a valid [RFC1884 2.2] address, else 0.
113 * notice:
114 * (1) does not touch `dst' unless it's returning 1.
115 * (2) :: in a full address is silently ignored.
116 * credit:
117 * inspired by Mark Andrews.
118 * author:
119 * Paul Vixie, 1996.
120 */
121 static int
inet_pton6(const char * src,unsigned char * dst)122 inet_pton6(const char *src, unsigned char *dst) {
123 static const char xdigits_l[] = "0123456789abcdef",
124 xdigits_u[] = "0123456789ABCDEF";
125 unsigned char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp;
126 const char *xdigits, *curtok;
127 int ch, seen_xdigits;
128 unsigned int val;
129
130 memset((tp = tmp), '\0', NS_IN6ADDRSZ);
131 endp = tp + NS_IN6ADDRSZ;
132 colonp = NULL;
133 /* Leading :: requires some special handling. */
134 if (*src == ':')
135 if (*++src != ':')
136 return (0);
137 curtok = src;
138 seen_xdigits = 0;
139 val = 0;
140 while ((ch = *src++) != '\0') {
141 const char *pch;
142
143 if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
144 pch = strchr((xdigits = xdigits_u), ch);
145 if (pch != NULL) {
146 val <<= 4;
147 val |= (pch - xdigits);
148 if (++seen_xdigits > 4)
149 return (0);
150 continue;
151 }
152 if (ch == ':') {
153 curtok = src;
154 if (!seen_xdigits) {
155 if (colonp)
156 return (0);
157 colonp = tp;
158 continue;
159 }
160 if (tp + NS_INT16SZ > endp)
161 return (0);
162 *tp++ = (unsigned char) (val >> 8) & 0xff;
163 *tp++ = (unsigned char) val & 0xff;
164 seen_xdigits = 0;
165 val = 0;
166 continue;
167 }
168 if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) &&
169 inet_pton4(curtok, tp) > 0) {
170 tp += NS_INADDRSZ;
171 seen_xdigits = 0;
172 break; /* '\0' was seen by inet_pton4(). */
173 }
174 return (0);
175 }
176 if (seen_xdigits) {
177 if (tp + NS_INT16SZ > endp)
178 return (0);
179 *tp++ = (unsigned char) (val >> 8) & 0xff;
180 *tp++ = (unsigned char) val & 0xff;
181 }
182 if (colonp != NULL) {
183 /*
184 * Since some memmove()'s erroneously fail to handle
185 * overlapping regions, we'll do the shift by hand.
186 */
187 const int n = (int)(tp - colonp);
188 int i;
189
190 for (i = 1; i <= n; i++) {
191 endp[- i] = colonp[n - i];
192 colonp[n - i] = 0;
193 }
194 tp = endp;
195 }
196 if (tp != endp)
197 return (0);
198 memmove(dst, tmp, NS_IN6ADDRSZ);
199 return (1);
200 }
201