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