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 "autoconfig.h"
19 
20 #ifndef HAVE_INET_PTON
21 #include <stdio.h>
22 
23 #include "UpnpInet.h"
24 #include "inet_pton.h"
25 /* For upnp_strlcpy */
26 #include "genut.h"
27 
28 /*%
29  * WARNING: Don't even consider trying to compile this on a system where
30  * sizeof(int) < 4.  sizeof(int) > 4 is fine; all the world's not a VAX.
31  */
32 
33 static char    *inet_ntop4(const unsigned char *src, char *dst, socklen_t size);
34 static char    *inet_ntop6(const unsigned char *src, char *dst, socklen_t size);
35 
36 /* const char *inet_ntop(af, src, dst, size)
37  *    convert a network format address to presentation format.
38  * return:
39  *    pointer to presentation format address (`dst'), or NULL (see errno).
40  * author:
41  *    Paul Vixie, 1996.
42  */
inet_ntop(int af,const void * src,char * dst,socklen_t size)43 const char *inet_ntop(int af, const void *src, char *dst, socklen_t size)
44 {
45     switch (af) {
46     case AF_INET:
47         return (inet_ntop4((const unsigned char*)src, dst, size));
48     case AF_INET6:
49         return (inet_ntop6((const unsigned char*)src, dst, size));
50     default:
51         return (NULL);
52     }
53     /* NOTREACHED */
54 }
55 
56 /* const char *inet_ntop4(src, dst, size)
57  *    format an IPv4 address
58  * return:
59  *    `dst' (as a const)
60  * notes:
61  *    (1) uses no statics
62  *    (2) takes a u_char* not an in_addr as input
63  * author:
64  *    Paul Vixie, 1996.
65  */
inet_ntop4(const unsigned char * src,char * dst,socklen_t size)66 static char *inet_ntop4(const unsigned char *src, char *dst, socklen_t size)
67 {
68     static const char fmt[] = "%u.%u.%u.%u";
69     char tmp[sizeof "255.255.255.255"];
70     int l;
71 
72     l = snprintf(tmp, sizeof(tmp), fmt, src[0], src[1], src[2], src[3]);
73     if (l <= 0 || (socklen_t) l >= size) {
74         return (NULL);
75     }
76     upnp_strlcpy(dst, tmp, size);
77     return (dst);
78 }
79 
80 /* const char *inet_ntop6(src, dst, size)
81  *    convert IPv6 binary address into presentation (printable) format
82  * author:
83  *    Paul Vixie, 1996.
84  */
inet_ntop6(const unsigned char * src,char * dst,socklen_t size)85 static char *inet_ntop6(const unsigned char *src, char *dst, socklen_t size)
86 {
87     /*
88      * Note that int32_t and int16_t need only be "at least" large enough
89      * to contain a value of the specified size.  On some systems, like
90      * Crays, there is no such thing as an integer variable with 16 bits.
91      * Keep this in mind if you think this function should have been coded
92      * to use pointer overlays.  All the world's not a VAX.
93      */
94     char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"], *tp;
95     struct { int base, len; } best, cur;
96 #define NS_IN6ADDRSZ    16
97 #define NS_INT16SZ    2
98     u_int words[NS_IN6ADDRSZ / NS_INT16SZ];
99     int i;
100 
101     /*
102      * Preprocess:
103      *    Copy the input (bytewise) array into a wordwise array.
104      *    Find the longest run of 0x00's in src[] for :: shorthanding.
105      */
106 
107     memset(words, '\0', sizeof words);
108     for (i = 0; i < NS_IN6ADDRSZ; i++)
109         words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
110     best.base = -1;
111     best.len = 0;
112     cur.base = -1;
113     cur.len = 0;
114     for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
115         if (words[i] == 0) {
116             if (cur.base == -1)
117                 cur.base = i, cur.len = 1;
118             else
119                 cur.len++;
120         } else {
121             if (cur.base != -1) {
122                 if (best.base == -1 || cur.len > best.len)
123                     best = cur;
124                 cur.base = -1;
125             }
126         }
127     }
128     if (cur.base != -1) {
129         if (best.base == -1 || cur.len > best.len)
130             best = cur;
131     }
132     if (best.base != -1 && best.len < 2)
133         best.base = -1;
134 
135     /*
136      * Format the result.
137      */
138     tp = tmp;
139     for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
140         /* Are we inside the best run of 0x00's? */
141         if (best.base != -1 && i >= best.base &&
142             i < (best.base + best.len)) {
143             if (i == best.base)
144                 *tp++ = ':';
145             continue;
146         }
147         /* Are we following an initial run of 0x00s or any real hex? */
148         if (i != 0)
149             *tp++ = ':';
150         /* Is this address an encapsulated IPv4? */
151         if (i == 6 && best.base == 0 && (best.len == 6 ||
152             (best.len == 7 && words[7] != 0x0001) ||
153             (best.len == 5 && words[5] == 0xffff))) {
154             if (!inet_ntop4(src+12, tp, sizeof tmp - (tp - tmp)))
155                 return (NULL);
156             tp += strlen(tp);
157             break;
158         }
159         tp += sprintf(tp, "%x", words[i]);
160     }
161     /* Was it a trailing run of 0x00's? */
162     if (best.base != -1 && (best.base + best.len) ==
163         (NS_IN6ADDRSZ / NS_INT16SZ))
164         *tp++ = ':';
165     *tp++ = '\0';
166 
167     /*
168      * Check for overflow, copy, and we're done.
169      */
170     if ((socklen_t)(tp - tmp) > size) {
171         return (NULL);
172     }
173     strcpy(dst, tmp);
174     return (dst);
175 }
176 
177 
178 /*
179  * WARNING: Don't even consider trying to compile this on a system where
180  * sizeof(int) < 4.  sizeof(int) > 4 is fine; all the world's not a VAX.
181  */
182 
183 static int    inet_pton4(const char *src, u_char *dst);
184 static int    inet_pton6(const char *src, u_char *dst);
185 
186 /* int
187  * inet_pton(af, src, dst)
188  *    convert from presentation format (which usually means ASCII printable)
189  *    to network format (which is usually some kind of binary format).
190  * return:
191  *    1 if the address was valid for the specified address family
192  *    0 if the address wasn't valid (`dst' is untouched in this case)
193  *    -1 if some other error occurred (`dst' is untouched in this case, too)
194  * author:
195  *    Paul Vixie, 1996.
196  */
inet_pton(int af,const char * src,void * dst)197 int inet_pton(int af, const char *src, void *dst)
198 {
199     switch (af) {
200     case AF_INET:
201         return (inet_pton4(src, (unsigned char *)dst));
202     case AF_INET6:
203         return (inet_pton6(src, (unsigned char *)dst));
204     default:
205         return (-1);
206     }
207     /* NOTREACHED */
208 }
209 
210 /* int
211  * inet_pton4(src, dst)
212  *    like inet_aton() but without all the hexadecimal and shorthand.
213  * return:
214  *    1 if `src' is a valid dotted quad, else 0.
215  * notice:
216  *    does not touch `dst' unless it's returning 1.
217  * author:
218  *    Paul Vixie, 1996.
219  */
inet_pton4(const char * src,u_char * dst)220 static int inet_pton4(const char *src, u_char *dst)
221 {
222     static const char digits[] = "0123456789";
223     int saw_digit, octets, ch;
224 #define NS_INADDRSZ    4
225     u_char tmp[NS_INADDRSZ], *tp;
226 
227     saw_digit = 0;
228     octets = 0;
229     *(tp = tmp) = 0;
230     while ((ch = *src++) != '\0') {
231         const char *pch;
232 
233         if ((pch = strchr(digits, ch)) != NULL) {
234             u_int uiNew = *tp * 10 + (pch - digits);
235 
236             if (saw_digit && *tp == 0)
237                 return (0);
238             if (uiNew > 255)
239                 return (0);
240             *tp = uiNew;
241             if (!saw_digit) {
242                 if (++octets > 4)
243                     return (0);
244                 saw_digit = 1;
245             }
246         } else if (ch == '.' && saw_digit) {
247             if (octets == 4)
248                 return (0);
249             *++tp = 0;
250             saw_digit = 0;
251         } else
252             return (0);
253     }
254     if (octets < 4)
255         return (0);
256     memcpy(dst, tmp, NS_INADDRSZ);
257     return (1);
258 }
259 
260 /* int
261  * inet_pton6(src, dst)
262  *    convert presentation level address to network order binary form.
263  * return:
264  *    1 if `src' is a valid [RFC1884 2.2] address, else 0.
265  * notice:
266  *    (1) does not touch `dst' unless it's returning 1.
267  *    (2) :: in a full address is silently ignored.
268  * credit:
269  *    inspired by Mark Andrews.
270  * author:
271  *    Paul Vixie, 1996.
272  */
inet_pton6(const char * src,u_char * dst)273 static int inet_pton6(const char *src, u_char *dst)
274 {
275     static const char xdigits_l[] = "0123456789abcdef",
276               xdigits_u[] = "0123456789ABCDEF";
277 #define NS_IN6ADDRSZ    16
278 #define NS_INT16SZ    2
279     u_char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp;
280     const char *xdigits, *curtok;
281     int ch, seen_xdigits;
282     u_int val;
283 
284     memset((tp = tmp), '\0', NS_IN6ADDRSZ);
285     endp = tp + NS_IN6ADDRSZ;
286     colonp = NULL;
287     /* Leading :: requires some special handling. */
288     if (*src == ':')
289         if (*++src != ':')
290             return (0);
291     curtok = src;
292     seen_xdigits = 0;
293     val = 0;
294     while ((ch = *src++) != '\0') {
295         const char *pch;
296 
297         if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
298             pch = strchr((xdigits = xdigits_u), ch);
299         if (pch != NULL) {
300             val <<= 4;
301             val |= (pch - xdigits);
302             if (++seen_xdigits > 4)
303                 return (0);
304             continue;
305         }
306         if (ch == ':') {
307             curtok = src;
308             if (!seen_xdigits) {
309                 if (colonp)
310                     return (0);
311                 colonp = tp;
312                 continue;
313             } else if (*src == '\0') {
314                 return (0);
315             }
316             if (tp + NS_INT16SZ > endp)
317                 return (0);
318             *tp++ = (u_char) (val >> 8) & 0xff;
319             *tp++ = (u_char) val & 0xff;
320             seen_xdigits = 0;
321             val = 0;
322             continue;
323         }
324         if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) &&
325             inet_pton4(curtok, tp) > 0) {
326             tp += NS_INADDRSZ;
327             seen_xdigits = 0;
328             break;    /*%< '\\0' was seen by inet_pton4(). */
329         }
330         return (0);
331     }
332     if (seen_xdigits) {
333         if (tp + NS_INT16SZ > endp)
334             return (0);
335         *tp++ = (u_char) (val >> 8) & 0xff;
336         *tp++ = (u_char) val & 0xff;
337     }
338     if (colonp != NULL) {
339         /*
340          * Since some memmove()'s erroneously fail to handle
341          * overlapping regions, we'll do the shift by hand.
342          */
343         const int n = tp - colonp;
344         int i;
345 
346         if (tp == endp)
347             return (0);
348         for (i = 1; i <= n; i++) {
349             endp[- i] = colonp[n - i];
350             colonp[n - i] = 0;
351         }
352         tp = endp;
353     }
354     if (tp != endp)
355         return (0);
356     memcpy(dst, tmp, NS_IN6ADDRSZ);
357     return (1);
358 }
359 #endif /* !HAVE_INET_PTON */
360