source: rtems/cpukit/libnetworking/libc/inet_ntop.c @ 3bc19384

5
Last change on this file since 3bc19384 was 029c374c, checked in by Ralf Corsepius <ralf.corsepius@…>, on 09/01/08 at 07:02:00

Stop using old-style function definitions.

  • Property mode set to 100644
File size: 4.9 KB
Line 
1/* Copyright (c) 1996 by Internet Software Consortium.
2 *
3 * Permission to use, copy, modify, and distribute this software for any
4 * purpose with or without fee is hereby granted, provided that the above
5 * copyright notice and this permission notice appear in all copies.
6 *
7 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
8 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
9 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
10 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
11 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
12 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
13 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
14 * SOFTWARE.
15 */
16
17#if HAVE_CONFIG_H
18#include "config.h"
19#endif
20
21#include <sys/param.h>
22#include <sys/types.h>
23#include <sys/socket.h>
24#include <netinet/in.h>
25#include <arpa/inet.h>
26#include <arpa/nameser.h>
27#include <errno.h>
28#include <stdio.h>
29#include <string.h>
30
31#define SPRINTF(x) ((size_t)sprintf x)
32
33/*
34 * WARNING: Don't even consider trying to compile this on a system where
35 * sizeof(int) < 4.  sizeof(int) > 4 is fine; all the world's not a VAX.
36 */
37
38static const char *inet_ntop4(const u_char *src, char *dst, socklen_t size);
39static const char *inet_ntop6(const u_char *src, char *dst, socklen_t size);
40
41/* char *
42 * inet_ntop(af, src, dst, size)
43 *      convert a network format address to presentation format.
44 * return:
45 *      pointer to presentation format address (`dst'), or NULL (see errno).
46 * author:
47 *      Paul Vixie, 1996.
48 */
49const char *
50inet_ntop(
51        int af,
52        const void *src,
53        char *dst,
54        socklen_t size)
55{
56        switch (af) {
57        case AF_INET:
58                return (inet_ntop4(src, dst, size));
59        case AF_INET6:
60                return (inet_ntop6(src, dst, size));
61        default:
62                errno = EAFNOSUPPORT;
63                return (NULL);
64        }
65        /* NOTREACHED */
66}
67
68/* const char *
69 * inet_ntop4(src, dst, size)
70 *      format an IPv4 address, more or less like inet_ntoa()
71 * return:
72 *      `dst' (as a const)
73 * notes:
74 *      (1) uses no statics
75 *      (2) takes a u_char* not an in_addr as input
76 * author:
77 *      Paul Vixie, 1996.
78 */
79static const char *
80inet_ntop4(
81        const u_char *src,
82        char *dst,
83        socklen_t size)
84{
85        static const char fmt[] = "%u.%u.%u.%u";
86        char tmp[sizeof "255.255.255.255"];
87
88        if (SPRINTF((tmp, fmt, src[0], src[1], src[2], src[3])) > size) {
89                errno = ENOSPC;
90                return (NULL);
91        }
92        strcpy(dst, tmp);
93        return (dst);
94}
95
96/* const char *
97 * inet_ntop6(src, dst, size)
98 *      convert IPv6 binary address into presentation (printable) format
99 * author:
100 *      Paul Vixie, 1996.
101 */
102static const char *
103inet_ntop6(
104        const u_char *src,
105        char *dst,
106        socklen_t size)
107{
108        /*
109         * Note that int32_t and int16_t need only be "at least" large enough
110         * to contain a value of the specified size.  On some systems, like
111         * Crays, there is no such thing as an integer variable with 16 bits.
112         * Keep this in mind if you think this function should have been coded
113         * to use pointer overlays.  All the world's not a VAX.
114         */
115        char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"], *tp;
116        struct { int base, len; } best, cur;
117        u_int words[NS_IN6ADDRSZ / NS_INT16SZ];
118        int i;
119
120        /*
121         * Preprocess:
122         *      Copy the input (bytewise) array into a wordwise array.
123         *      Find the longest run of 0x00's in src[] for :: shorthanding.
124         */
125        memset(words, '\0', sizeof words);
126        for (i = 0; i < NS_IN6ADDRSZ; i++)
127                words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
128        best.base = -1;
129        best.len  = 0;
130        cur.base = -1;
131        cur.len  = 0;
132        for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
133                if (words[i] == 0) {
134                        if (cur.base == -1)
135                                cur.base = i, cur.len = 1;
136                        else
137                                cur.len++;
138                } else {
139                        if (cur.base != -1) {
140                                if (best.base == -1 || cur.len > best.len)
141                                        best = cur;
142                                cur.base = -1;
143                        }
144                }
145        }
146        if (cur.base != -1) {
147                if (best.base == -1 || cur.len > best.len)
148                        best = cur;
149        }
150        if (best.base != -1 && best.len < 2)
151                best.base = -1;
152
153        /*
154         * Format the result.
155         */
156        tp = tmp;
157        for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
158                /* Are we inside the best run of 0x00's? */
159                if (best.base != -1 && i >= best.base &&
160                    i < (best.base + best.len)) {
161                        if (i == best.base)
162                                *tp++ = ':';
163                        continue;
164                }
165                /* Are we following an initial run of 0x00s or any real hex? */
166                if (i != 0)
167                        *tp++ = ':';
168                /* Is this address an encapsulated IPv4? */
169                if (i == 6 && best.base == 0 &&
170                    (best.len == 6 || (best.len == 5 && words[5] == 0xffff))) {
171                        if (!inet_ntop4(src+12, tp, sizeof tmp - (tp - tmp)))
172                                return (NULL);
173                        tp += strlen(tp);
174                        break;
175                }
176                tp += SPRINTF((tp, "%x", words[i]));
177        }
178        /* Was it a trailing run of 0x00's? */
179        if (best.base != -1 && (best.base + best.len) ==
180            (NS_IN6ADDRSZ / NS_INT16SZ))
181                *tp++ = ':';
182        *tp++ = '\0';
183
184        /*
185         * Check for overflow, copy, and we're done.
186         */
187        if ((size_t)(tp - tmp) > size) {
188                errno = ENOSPC;
189                return (NULL);
190        }
191        strcpy(dst, tmp);
192        return (dst);
193}
Note: See TracBrowser for help on using the repository browser.