/Users/deen/code/yugabyte-db/build/debugcov-clang-dynamic-arm64-ninja/postgres_build/src/interfaces/libpq/inet_net_ntop.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") |
3 | | * Copyright (c) 1996,1999 by Internet Software Consortium. |
4 | | * |
5 | | * Permission to use, copy, modify, and distribute this software for any |
6 | | * purpose with or without fee is hereby granted, provided that the above |
7 | | * copyright notice and this permission notice appear in all copies. |
8 | | * |
9 | | * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES |
10 | | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
11 | | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR |
12 | | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
13 | | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
14 | | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
15 | | * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
16 | | * |
17 | | * src/port/inet_net_ntop.c |
18 | | */ |
19 | | |
20 | | #if defined(LIBC_SCCS) && !defined(lint) |
21 | | static const char rcsid[] = "Id: inet_net_ntop.c,v 1.1.2.2 2004/03/09 09:17:27 marka Exp $"; |
22 | | #endif |
23 | | |
24 | | #ifndef FRONTEND |
25 | | #include "postgres.h" |
26 | | #else |
27 | | #include "postgres_fe.h" |
28 | | #endif |
29 | | |
30 | | #include <sys/socket.h> |
31 | | #include <netinet/in.h> |
32 | | #include <arpa/inet.h> |
33 | | |
34 | | #ifndef FRONTEND |
35 | | #include "utils/inet.h" |
36 | | #else |
37 | | /* |
38 | | * In a frontend build, we can't include inet.h, but we still need to have |
39 | | * sensible definitions of these two constants. Note that inet_net_ntop() |
40 | | * assumes that PGSQL_AF_INET is equal to AF_INET. |
41 | | */ |
42 | 140k | #define PGSQL_AF_INET (AF_INET + 0) |
43 | 0 | #define PGSQL_AF_INET6 (AF_INET + 1) |
44 | | #endif |
45 | | |
46 | | |
47 | 0 | #define NS_IN6ADDRSZ 16 |
48 | 0 | #define NS_INT16SZ 2 |
49 | | |
50 | | #ifdef SPRINTF_CHAR |
51 | | #define SPRINTF(x) strlen(sprintf/**/x) |
52 | | #else |
53 | 564k | #define SPRINTF(x) ((size_t)sprintf x) |
54 | | #endif |
55 | | |
56 | | static char *inet_net_ntop_ipv4(const u_char *src, int bits, |
57 | | char *dst, size_t size); |
58 | | static char *inet_net_ntop_ipv6(const u_char *src, int bits, |
59 | | char *dst, size_t size); |
60 | | |
61 | | |
62 | | /* |
63 | | * char * |
64 | | * inet_net_ntop(af, src, bits, dst, size) |
65 | | * convert host/network address from network to presentation format. |
66 | | * "src"'s size is determined from its "af". |
67 | | * return: |
68 | | * pointer to dst, or NULL if an error occurred (check errno). |
69 | | * note: |
70 | | * 192.5.5.1/28 has a nonzero host part, which means it isn't a network |
71 | | * as called for by inet_net_pton() but it can be a host address with |
72 | | * an included netmask. |
73 | | * author: |
74 | | * Paul Vixie (ISC), October 1998 |
75 | | */ |
76 | | char * |
77 | | inet_net_ntop(int af, const void *src, int bits, char *dst, size_t size) |
78 | 140k | { |
79 | | /* |
80 | | * We need to cover both the address family constants used by the PG inet |
81 | | * type (PGSQL_AF_INET and PGSQL_AF_INET6) and those used by the system |
82 | | * libraries (AF_INET and AF_INET6). We can safely assume PGSQL_AF_INET |
83 | | * == AF_INET, but the INET6 constants are very likely to be different. If |
84 | | * AF_INET6 isn't defined, silently ignore it. |
85 | | */ |
86 | 140k | switch (af) |
87 | 140k | { |
88 | 140k | case PGSQL_AF_INET: |
89 | 140k | return (inet_net_ntop_ipv4(src, bits, dst, size)); |
90 | 0 | case PGSQL_AF_INET6: |
91 | 0 | #if defined(AF_INET6) && AF_INET6 != PGSQL_AF_INET6 |
92 | 0 | case AF_INET6: |
93 | 0 | #endif |
94 | 0 | return (inet_net_ntop_ipv6(src, bits, dst, size)); |
95 | 0 | default: |
96 | 0 | errno = EAFNOSUPPORT; |
97 | 0 | return (NULL); |
98 | 140k | } |
99 | 140k | } |
100 | | |
101 | | /* |
102 | | * static char * |
103 | | * inet_net_ntop_ipv4(src, bits, dst, size) |
104 | | * convert IPv4 network address from network to presentation format. |
105 | | * "src"'s size is determined from its "af". |
106 | | * return: |
107 | | * pointer to dst, or NULL if an error occurred (check errno). |
108 | | * note: |
109 | | * network byte order assumed. this means 192.5.5.240/28 has |
110 | | * 0b11110000 in its fourth octet. |
111 | | * author: |
112 | | * Paul Vixie (ISC), October 1998 |
113 | | */ |
114 | | static char * |
115 | | inet_net_ntop_ipv4(const u_char *src, int bits, char *dst, size_t size) |
116 | 140k | { |
117 | 140k | char *odst = dst; |
118 | 140k | char *t; |
119 | 140k | int len = 4; |
120 | 140k | int b; |
121 | | |
122 | 140k | if (bits < 0 || bits > 32) |
123 | 0 | { |
124 | 0 | errno = EINVAL; |
125 | 0 | return (NULL); |
126 | 0 | } |
127 | | |
128 | | /* Always format all four octets, regardless of mask length. */ |
129 | 704k | for (b = len; 140k b > 0; b--563k ) |
130 | 563k | { |
131 | 563k | if (size <= sizeof ".255") |
132 | 0 | goto emsgsize; |
133 | 563k | t = dst; |
134 | 563k | if (dst != odst) |
135 | 422k | *dst++ = '.'; |
136 | 563k | dst += SPRINTF((dst, "%u", *src++)); |
137 | 563k | size -= (size_t) (dst - t); |
138 | 563k | } |
139 | | |
140 | | /* don't print masklen if 32 bits */ |
141 | 140k | if (bits != 32) |
142 | 632 | { |
143 | 632 | if (size <= sizeof "/32") |
144 | 0 | goto emsgsize; |
145 | 632 | dst += SPRINTF((dst, "/%u", bits)); |
146 | 632 | } |
147 | | |
148 | 140k | return (odst); |
149 | | |
150 | 0 | emsgsize: |
151 | 0 | errno = EMSGSIZE; |
152 | 0 | return (NULL); |
153 | 140k | } |
154 | | |
155 | | static int |
156 | | decoct(const u_char *src, int bytes, char *dst, size_t size) |
157 | 0 | { |
158 | 0 | char *odst = dst; |
159 | 0 | char *t; |
160 | 0 | int b; |
161 | |
|
162 | 0 | for (b = 1; b <= bytes; b++) |
163 | 0 | { |
164 | 0 | if (size <= sizeof "255.") |
165 | 0 | return (0); |
166 | 0 | t = dst; |
167 | 0 | dst += SPRINTF((dst, "%u", *src++)); |
168 | 0 | if (b != bytes) |
169 | 0 | { |
170 | 0 | *dst++ = '.'; |
171 | 0 | *dst = '\0'; |
172 | 0 | } |
173 | 0 | size -= (size_t) (dst - t); |
174 | 0 | } |
175 | 0 | return (dst - odst); |
176 | 0 | } |
177 | | |
178 | | static char * |
179 | | inet_net_ntop_ipv6(const u_char *src, int bits, char *dst, size_t size) |
180 | 0 | { |
181 | | /* |
182 | | * Note that int32_t and int16_t need only be "at least" large enough to |
183 | | * contain a value of the specified size. On some systems, like Crays, |
184 | | * there is no such thing as an integer variable with 16 bits. Keep this |
185 | | * in mind if you think this function should have been coded to use |
186 | | * pointer overlays. All the world's not a VAX. |
187 | | */ |
188 | 0 | char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255/128"]; |
189 | 0 | char *tp; |
190 | 0 | struct |
191 | 0 | { |
192 | 0 | int base, |
193 | 0 | len; |
194 | 0 | } best, cur; |
195 | 0 | u_int words[NS_IN6ADDRSZ / NS_INT16SZ]; |
196 | 0 | int i; |
197 | |
|
198 | 0 | if ((bits < -1) || (bits > 128)) |
199 | 0 | { |
200 | 0 | errno = EINVAL; |
201 | 0 | return (NULL); |
202 | 0 | } |
203 | | |
204 | | /* |
205 | | * Preprocess: Copy the input (bytewise) array into a wordwise array. Find |
206 | | * the longest run of 0x00's in src[] for :: shorthanding. |
207 | | */ |
208 | 0 | memset(words, '\0', sizeof words); |
209 | 0 | for (i = 0; i < NS_IN6ADDRSZ; i++) |
210 | 0 | words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3)); |
211 | 0 | best.base = -1; |
212 | 0 | cur.base = -1; |
213 | 0 | best.len = 0; |
214 | 0 | cur.len = 0; |
215 | 0 | for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) |
216 | 0 | { |
217 | 0 | if (words[i] == 0) |
218 | 0 | { |
219 | 0 | if (cur.base == -1) |
220 | 0 | cur.base = i, cur.len = 1; |
221 | 0 | else |
222 | 0 | cur.len++; |
223 | 0 | } |
224 | 0 | else |
225 | 0 | { |
226 | 0 | if (cur.base != -1) |
227 | 0 | { |
228 | 0 | if (best.base == -1 || cur.len > best.len) |
229 | 0 | best = cur; |
230 | 0 | cur.base = -1; |
231 | 0 | } |
232 | 0 | } |
233 | 0 | } |
234 | 0 | if (cur.base != -1) |
235 | 0 | { |
236 | 0 | if (best.base == -1 || cur.len > best.len) |
237 | 0 | best = cur; |
238 | 0 | } |
239 | 0 | if (best.base != -1 && best.len < 2) |
240 | 0 | best.base = -1; |
241 | | |
242 | | /* |
243 | | * Format the result. |
244 | | */ |
245 | 0 | tp = tmp; |
246 | 0 | for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) |
247 | 0 | { |
248 | | /* Are we inside the best run of 0x00's? */ |
249 | 0 | if (best.base != -1 && i >= best.base && |
250 | 0 | i < (best.base + best.len)) |
251 | 0 | { |
252 | 0 | if (i == best.base) |
253 | 0 | *tp++ = ':'; |
254 | 0 | continue; |
255 | 0 | } |
256 | | /* Are we following an initial run of 0x00s or any real hex? */ |
257 | 0 | if (i != 0) |
258 | 0 | *tp++ = ':'; |
259 | | /* Is this address an encapsulated IPv4? */ |
260 | 0 | if (i == 6 && best.base == 0 && (best.len == 6 || |
261 | 0 | (best.len == 7 && words[7] != 0x0001) || |
262 | 0 | (best.len == 5 && words[5] == 0xffff))) |
263 | 0 | { |
264 | 0 | int n; |
265 | |
|
266 | 0 | n = decoct(src + 12, 4, tp, sizeof tmp - (tp - tmp)); |
267 | 0 | if (n == 0) |
268 | 0 | { |
269 | 0 | errno = EMSGSIZE; |
270 | 0 | return (NULL); |
271 | 0 | } |
272 | 0 | tp += strlen(tp); |
273 | 0 | break; |
274 | 0 | } |
275 | 0 | tp += SPRINTF((tp, "%x", words[i])); |
276 | 0 | } |
277 | | |
278 | | /* Was it a trailing run of 0x00's? */ |
279 | 0 | if (best.base != -1 && (best.base + best.len) == |
280 | 0 | (NS_IN6ADDRSZ / NS_INT16SZ)) |
281 | 0 | *tp++ = ':'; |
282 | 0 | *tp = '\0'; |
283 | |
|
284 | 0 | if (bits != -1 && bits != 128) |
285 | 0 | tp += SPRINTF((tp, "/%u", bits)); |
286 | | |
287 | | /* |
288 | | * Check for overflow, copy, and we're done. |
289 | | */ |
290 | 0 | if ((size_t) (tp - tmp) > size) |
291 | 0 | { |
292 | 0 | errno = EMSGSIZE; |
293 | 0 | return (NULL); |
294 | 0 | } |
295 | 0 | strcpy(dst, tmp); |
296 | 0 | return (dst); |
297 | 0 | } |