source: rtems-libbsd/freebsd/lib/libc/inet/inet_ntop.c @ 3d1e767

55-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since 3d1e767 was 3d1e767, checked in by Sebastian Huber <sebastian.huber@…>, on 04/27/16 at 08:25:22

Directly use <sys/types.h> provided by Newlib

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