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

6-freebsd-12
Last change on this file was 18fa92c, checked in by Sebastian Huber <sebastian.huber@…>, on 08/20/18 at 13:53:03

Update to FreeBSD head 2018-02-01

Git mirror commit d079ae0442af8fa3cfd6d7ede190d04e64a2c0d4.

Update #3472.

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