Fix sh
[rsync/rsync.git] / lib / inet_pton.c
CommitLineData
8f694072
MP
1/*
2 * Copyright (C) 1996-2001 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
9 * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
10 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
11 * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
13 * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
14 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
15 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
3723efcb 18#include "rsync.h"
8f694072
MP
19
20#define NS_INT16SZ 2
21#define NS_INADDRSZ 4
22#define NS_IN6ADDRSZ 16
23
24/*
25 * WARNING: Don't even consider trying to compile this on a system where
26 * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX.
27 */
28
29static int inet_pton4(const char *src, unsigned char *dst);
30static int inet_pton6(const char *src, unsigned char *dst);
31
32/* int
33 * isc_net_pton(af, src, dst)
34 * convert from presentation format (which usually means ASCII printable)
35 * to network format (which is usually some kind of binary format).
36 * return:
37 * 1 if the address was valid for the specified address family
38 * 0 if the address wasn't valid (`dst' is untouched in this case)
39 * -1 if some other error occurred (`dst' is untouched in this case, too)
40 * author:
41 * Paul Vixie, 1996.
42 */
43int
44isc_net_pton(af, src, dst)
45 int af;
46 const char *src;
47 void *dst;
48{
49 switch (af) {
50 case AF_INET:
51 return (inet_pton4(src, dst));
52 case AF_INET6:
53 return (inet_pton6(src, dst));
54 default:
55 errno = EAFNOSUPPORT;
56 return (-1);
57 }
58 /* NOTREACHED */
59}
60
61/* int
62 * inet_pton4(src, dst)
63 * like inet_aton() but without all the hexadecimal and shorthand.
64 * return:
65 * 1 if `src' is a valid dotted quad, else 0.
66 * notice:
67 * does not touch `dst' unless it's returning 1.
68 * author:
69 * Paul Vixie, 1996.
70 */
71static int
72inet_pton4(src, dst)
73 const char *src;
74 unsigned char *dst;
75{
76 static const char digits[] = "0123456789";
77 int saw_digit, octets, ch;
78 unsigned char tmp[NS_INADDRSZ], *tp;
79
80 saw_digit = 0;
81 octets = 0;
82 *(tp = tmp) = 0;
83 while ((ch = *src++) != '\0') {
84 const char *pch;
85
86 if ((pch = strchr(digits, ch)) != NULL) {
87 unsigned int new = *tp * 10 + (pch - digits);
88
89 if (new > 255)
90 return (0);
91 *tp = new;
92 if (! saw_digit) {
93 if (++octets > 4)
94 return (0);
95 saw_digit = 1;
96 }
97 } else if (ch == '.' && saw_digit) {
98 if (octets == 4)
99 return (0);
100 *++tp = 0;
101 saw_digit = 0;
102 } else
103 return (0);
104 }
105 if (octets < 4)
106 return (0);
107 memcpy(dst, tmp, NS_INADDRSZ);
108 return (1);
109}
110
111/* int
112 * inet_pton6(src, dst)
113 * convert presentation level address to network order binary form.
114 * return:
115 * 1 if `src' is a valid [RFC1884 2.2] address, else 0.
116 * notice:
117 * (1) does not touch `dst' unless it's returning 1.
118 * (2) :: in a full address is silently ignored.
119 * credit:
120 * inspired by Mark Andrews.
121 * author:
122 * Paul Vixie, 1996.
123 */
124static int
125inet_pton6(src, dst)
126 const char *src;
127 unsigned char *dst;
128{
129 static const char xdigits_l[] = "0123456789abcdef",
130 xdigits_u[] = "0123456789ABCDEF";
131 unsigned char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp;
132 const char *xdigits, *curtok;
133 int ch, saw_xdigit;
134 unsigned int val;
135
136 memset((tp = tmp), '\0', NS_IN6ADDRSZ);
137 endp = tp + NS_IN6ADDRSZ;
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 = *src++) != '\0') {
147 const char *pch;
148
149 if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
150 pch = strchr((xdigits = xdigits_u), ch);
151 if (pch != NULL) {
152 val <<= 4;
153 val |= (pch - xdigits);
154 if (val > 0xffff)
155 return (0);
156 saw_xdigit = 1;
157 continue;
158 }
159 if (ch == ':') {
160 curtok = src;
161 if (!saw_xdigit) {
162 if (colonp)
163 return (0);
164 colonp = tp;
165 continue;
166 }
167 if (tp + NS_INT16SZ > endp)
168 return (0);
169 *tp++ = (unsigned char) (val >> 8) & 0xff;
170 *tp++ = (unsigned char) val & 0xff;
171 saw_xdigit = 0;
172 val = 0;
173 continue;
174 }
175 if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) &&
176 inet_pton4(curtok, tp) > 0) {
177 tp += NS_INADDRSZ;
178 saw_xdigit = 0;
179 break; /* '\0' was seen by inet_pton4(). */
180 }
181 return (0);
182 }
183 if (saw_xdigit) {
184 if (tp + NS_INT16SZ > endp)
185 return (0);
186 *tp++ = (unsigned char) (val >> 8) & 0xff;
187 *tp++ = (unsigned char) val & 0xff;
188 }
189 if (colonp != NULL) {
190 /*
191 * Since some memmove()'s erroneously fail to handle
192 * overlapping regions, we'll do the shift by hand.
193 */
194 const int n = tp - colonp;
195 int i;
196
197 for (i = 1; i <= n; i++) {
198 endp[- i] = colonp[n - i];
199 colonp[n - i] = 0;
200 }
201 tp = endp;
202 }
203 if (tp != endp)
204 return (0);
205 memcpy(dst, tmp, NS_IN6ADDRSZ);
206 return (1);
207}