OSDN Git Service

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