Debian Codegraph @

lib/inet_ntop.c

raw
1 /* inet_ntop.c -- convert IPv4 and IPv6 addresses from binary to text form
2
3 Copyright (C) 2005-2006, 2008-2026 Free Software Foundation, Inc.
4
5 This file is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as
7 published by the Free Software Foundation; either version 2.1 of the
8 License, or (at your option) any later version.
9
10 This file is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17
18 /*
19 * Copyright (c) 1996-1999 by Internet Software Consortium.
20 *
21 * Permission to use, copy, modify, and distribute this software for any
22 * purpose with or without fee is hereby granted, provided that the above
23 * copyright notice and this permission notice appear in all copies.
24 *
25 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
26 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
28 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
29 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
30 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
31 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
32 * SOFTWARE.
33 */
34
35 #include <config.h>
36
37 /* Specification. */
38 #include <arpa/inet.h>
39
40 /* Use this to suppress gcc's "...may be used before initialized" warnings.
41 Beware: The Code argument must not contain commas. */
42 #ifndef IF_LINT
43 # if defined GCC_LINT || defined lint
44 # define IF_LINT(Code) Code
45 # else
46 # define IF_LINT(Code) /* empty */
47 # endif
48 #endif
49
50 #if HAVE_DECL_INET_NTOP
51
52 # undef inet_ntop
53
54 const char *
55 rpl_inet_ntop (int af, const void *restrict src,
56 char *restrict dst, socklen_t cnt)
57 {
58 return inet_ntop (af, src, dst, cnt);
59 }
60
61 #else
62
63 # include <stdio.h>
64 # include <string.h>
65 # include <errno.h>
66
67 # define NS_IN6ADDRSZ 16
68 # define NS_INT16SZ 2
69
70 /*
71 * WARNING: Don't even consider trying to compile this on a system where
72 * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX.
73 */
74 typedef int verify_int_size[4 <= sizeof (int) ? 1 : -1];
75
76 static const char *inet_ntop4 (const unsigned char *src, char *dst, socklen_t size);
77 # if HAVE_IPV6
78 static const char *inet_ntop6 (const unsigned char *src, char *dst, socklen_t size);
79 # endif
80
81
82 /* char *
83 * inet_ntop(af, src, dst, size)
84 * convert a network format address to presentation format.
85 * return:
86 * pointer to presentation format address ('dst'), or NULL (see errno).
87 * author:
88 * Paul Vixie, 1996.
89 */
90 const char *
91 inet_ntop (int af, const void *restrict src,
92 char *restrict dst, socklen_t cnt)
93 {
94 switch (af)
95 {
96 # if HAVE_IPV4
97 case AF_INET:
98 return (inet_ntop4 (src, dst, cnt));
99 # endif
100
101 # if HAVE_IPV6
102 case AF_INET6:
103 return (inet_ntop6 (src, dst, cnt));
104 # endif
105
106 default:
107 errno = EAFNOSUPPORT;
108 return (NULL);
109 }
110 /* NOTREACHED */
111 }
112
113 /* const char *
114 * inet_ntop4(src, dst, size)
115 * format an IPv4 address
116 * return:
117 * 'dst' (as a const)
118 * notes:
119 * (1) uses no statics
120 * (2) takes a 'unsigned char *' not an in_addr as input
121 * author:
122 * Paul Vixie, 1996.
123 */
124 static const char *
125 inet_ntop4 (const unsigned char *src, char *dst, socklen_t size)
126 {
127 char tmp[sizeof "255.255.255.255"];
128 int len = sprintf (tmp, "%u.%u.%u.%u", src[0], src[1], src[2], src[3]);
129 if (len < 0)
130 return NULL;
131
132 if (len > size)
133 {
134 errno = ENOSPC;
135 return NULL;
136 }
137
138 return strcpy (dst, tmp);
139 }
140
141 # if HAVE_IPV6
142
143 /* const char *
144 * inet_ntop6(src, dst, size)
145 * convert IPv6 binary address into presentation (printable) format
146 * author:
147 * Paul Vixie, 1996.
148 */
149 static const char *
150 inet_ntop6 (const unsigned char *src, char *dst, socklen_t size)
151 {
152 /*
153 * Preprocess:
154 * Copy the input (bytewise) array into a wordwise array.
155 * Find the longest run of 0x00's in src[] for :: shorthanding.
156 */
157 unsigned int words[NS_IN6ADDRSZ / NS_INT16SZ];
158 memset (words, '\0', sizeof words);
159 for (int i = 0; i < NS_IN6ADDRSZ; i += 2)
160 words[i / 2] = (src[i] << 8) | src[i + 1];
161
162 struct
163 {
164 int base, len;
165 } best, cur;
166 best.base = -1;
167 cur.base = -1;
168 IF_LINT(best.len = 0);
169 IF_LINT(cur.len = 0);
170
171 for (int i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++)
172 {
173 if (words[i] == 0)
174 {
175 if (cur.base == -1)
176 cur.base = i, cur.len = 1;
177 else
178 cur.len++;
179 }
180 else
181 {
182 if (cur.base != -1)
183 {
184 if (best.base == -1 || cur.len > best.len)
185 best = cur;
186 cur.base = -1;
187 }
188 }
189 }
190 if (cur.base != -1)
191 {
192 if (best.base == -1 || cur.len > best.len)
193 best = cur;
194 }
195 if (best.base != -1 && best.len < 2)
196 best.base = -1;
197
198 /*
199 * Format the result.
200 */
201 /*
202 * Note that int32_t and int16_t need only be "at least" large enough
203 * to contain a value of the specified size. On some systems, like
204 * Crays, there is no such thing as an integer variable with 16 bits.
205 * Keep this in mind if you think this function should have been coded
206 * to use pointer overlays. All the world's not a VAX.
207 */
208 char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"];
209 char *tp = tmp;
210 for (int i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++)
211 {
212 /* Are we inside the best run of 0x00's? */
213 if (best.base != -1 && i >= best.base && i < (best.base + best.len))
214 {
215 if (i == best.base)
216 *tp++ = ':';
217 continue;
218 }
219 /* Are we following an initial run of 0x00s or any real hex? */
220 if (i != 0)
221 *tp++ = ':';
222 /* Is this address an encapsulated IPv4? */
223 if (i == 6 && best.base == 0 &&
224 (best.len == 6 || (best.len == 5 && words[5] == 0xffff)))
225 {
226 if (!inet_ntop4 (src + 12, tp, sizeof tmp - (tp - tmp)))
227 return (NULL);
228 tp += strlen (tp);
229 break;
230 }
231 {
232 int len = sprintf (tp, "%x", words[i]);
233 if (len < 0)
234 return NULL;
235 tp += len;
236 }
237 }
238 /* Was it a trailing run of 0x00's? */
239 if (best.base != -1 && (best.base + best.len) ==
240 (NS_IN6ADDRSZ / NS_INT16SZ))
241 *tp++ = ':';
242 *tp++ = '\0';
243
244 /*
245 * Check for overflow, copy, and we're done.
246 */
247 if ((socklen_t) (tp - tmp) > size)
248 {
249 errno = ENOSPC;
250 return NULL;
251 }
252
253 return strcpy (dst, tmp);
254 }
255
256 # endif
257
258 #endif