OSDN Git Service

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