OSDN Git Service

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