VirtualBox

source: vbox/trunk/src/libs/curl-8.0.1/lib/inet_ntop.c@ 100906

Last change on this file since 100906 was 99344, checked in by vboxsync, 2 years ago

curl-8.0.1: Applied and adjusted our curl changes to 7.87.0 bugref:10417

  • Property svn:eol-style set to native
File size: 5.5 KB
Line 
1/*
2 * Copyright (C) 1996-2022 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
9 * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
10 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
11 * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
13 * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
14 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
15 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 *
17 * SPDX-License-Identifier: ISC
18 */
19/*
20 * Original code by Paul Vixie. "curlified" by Gisle Vanem.
21 */
22
23#include "curl_setup.h"
24
25#ifndef HAVE_INET_NTOP
26
27#ifdef HAVE_SYS_PARAM_H
28#include <sys/param.h>
29#endif
30#ifdef HAVE_NETINET_IN_H
31#include <netinet/in.h>
32#endif
33#ifdef HAVE_ARPA_INET_H
34#include <arpa/inet.h>
35#endif
36
37#include "inet_ntop.h"
38#include "curl_printf.h"
39
40#define IN6ADDRSZ 16
41#define INADDRSZ 4
42#define INT16SZ 2
43
44/*
45 * If ENABLE_IPV6 is disabled, we still want to parse IPv6 addresses, so make
46 * sure we have _some_ value for AF_INET6 without polluting our fake value
47 * everywhere.
48 */
49#if !defined(ENABLE_IPV6) && !defined(AF_INET6)
50#define AF_INET6 (AF_INET + 1)
51#endif
52
53/*
54 * Format an IPv4 address, more or less like inet_ntop().
55 *
56 * Returns `dst' (as a const)
57 * Note:
58 * - uses no statics
59 * - takes a unsigned char* not an in_addr as input
60 */
61static char *inet_ntop4 (const unsigned char *src, char *dst, size_t size)
62{
63 char tmp[sizeof("255.255.255.255")];
64 size_t len;
65
66 DEBUGASSERT(size >= 16);
67
68 tmp[0] = '\0';
69 (void)msnprintf(tmp, sizeof(tmp), "%d.%d.%d.%d",
70 ((int)((unsigned char)src[0])) & 0xff,
71 ((int)((unsigned char)src[1])) & 0xff,
72 ((int)((unsigned char)src[2])) & 0xff,
73 ((int)((unsigned char)src[3])) & 0xff);
74
75 len = strlen(tmp);
76 if(len == 0 || len >= size) {
77 errno = ENOSPC;
78 return (NULL);
79 }
80 strcpy(dst, tmp);
81 return dst;
82}
83
84/*
85 * Convert IPv6 binary address into presentation (printable) format.
86 */
87static char *inet_ntop6 (const unsigned char *src, char *dst, size_t size)
88{
89 /*
90 * Note that int32_t and int16_t need only be "at least" large enough
91 * to contain a value of the specified size. On some systems, like
92 * Crays, there is no such thing as an integer variable with 16 bits.
93 * Keep this in mind if you think this function should have been coded
94 * to use pointer overlays. All the world's not a VAX.
95 */
96 char tmp[sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")];
97 char *tp;
98 struct {
99 long base;
100 long len;
101 } best, cur;
102 unsigned long words[IN6ADDRSZ / INT16SZ];
103 int i;
104
105 /* Preprocess:
106 * Copy the input (bytewise) array into a wordwise array.
107 * Find the longest run of 0x00's in src[] for :: shorthanding.
108 */
109 memset(words, '\0', sizeof(words));
110 for(i = 0; i < IN6ADDRSZ; i++)
111 words[i/2] |= (src[i] << ((1 - (i % 2)) << 3));
112
113 best.base = -1;
114 cur.base = -1;
115 best.len = 0;
116 cur.len = 0;
117
118 for(i = 0; i < (IN6ADDRSZ / INT16SZ); i++) {
119 if(words[i] == 0) {
120 if(cur.base == -1)
121 cur.base = i, cur.len = 1;
122 else
123 cur.len++;
124 }
125 else if(cur.base != -1) {
126 if(best.base == -1 || cur.len > best.len)
127 best = cur;
128 cur.base = -1;
129 }
130 }
131 if((cur.base != -1) && (best.base == -1 || cur.len > best.len))
132 best = cur;
133 if(best.base != -1 && best.len < 2)
134 best.base = -1;
135 /* Format the result. */
136 tp = tmp;
137 for(i = 0; i < (IN6ADDRSZ / INT16SZ); i++) {
138 /* Are we inside the best run of 0x00's? */
139 if(best.base != -1 && i >= best.base && i < (best.base + best.len)) {
140 if(i == best.base)
141 *tp++ = ':';
142 continue;
143 }
144
145 /* Are we following an initial run of 0x00s or any real hex?
146 */
147 if(i)
148 *tp++ = ':';
149
150 /* Is this address an encapsulated IPv4?
151 */
152 if(i == 6 && best.base == 0 &&
153 (best.len == 6 || (best.len == 5 && words[5] == 0xffff))) {
154 if(!inet_ntop4(src + 12, tp, sizeof(tmp) - (tp - tmp))) {
155 errno = ENOSPC;
156 return (NULL);
157 }
158 tp += strlen(tp);
159 break;
160 }
161 tp += msnprintf(tp, 5, "%lx", words[i]);
162 }
163
164 /* Was it a trailing run of 0x00's?
165 */
166 if(best.base != -1 && (best.base + best.len) == (IN6ADDRSZ / INT16SZ))
167 *tp++ = ':';
168 *tp++ = '\0';
169
170 /* Check for overflow, copy, and we're done.
171 */
172 if((size_t)(tp - tmp) > size) {
173 errno = ENOSPC;
174 return (NULL);
175 }
176 strcpy(dst, tmp);
177 return dst;
178}
179
180/*
181 * Convert a network format address to presentation format.
182 *
183 * Returns pointer to presentation format address (`buf').
184 * Returns NULL on error and errno set with the specific
185 * error, EAFNOSUPPORT or ENOSPC.
186 *
187 * On Windows we store the error in the thread errno, not
188 * in the winsock error code. This is to avoid losing the
189 * actual last winsock error. So when this function returns
190 * NULL, check errno not SOCKERRNO.
191 */
192char *Curl_inet_ntop(int af, const void *src, char *buf, size_t size)
193{
194 switch(af) {
195 case AF_INET:
196 return inet_ntop4((const unsigned char *)src, buf, size);
197 case AF_INET6:
198 return inet_ntop6((const unsigned char *)src, buf, size);
199 default:
200 errno = EAFNOSUPPORT;
201 return NULL;
202 }
203}
204#endif /* HAVE_INET_NTOP */
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette