2
* Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3
* Copyright (c) 1996,1999 by Internet Software Consortium.
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.
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.
17
* src/backend/utils/adt/inet_net_ntop.c
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 $";
26
#include <sys/types.h>
27
#include <sys/socket.h>
28
#include <netinet/in.h>
29
#include <arpa/inet.h>
31
#include "utils/builtins.h"
32
#include "utils/inet.h"
36
#define SPRINTF(x) strlen(sprintf/**/x)
38
#define SPRINTF(x) ((size_t)sprintf x)
41
static char *inet_cidr_ntop_ipv4(const u_char *src, int bits,
42
char *dst, size_t size);
43
static char *inet_cidr_ntop_ipv6(const u_char *src, int bits,
44
char *dst, size_t size);
48
* inet_cidr_ntop(af, src, bits, dst, size)
49
* convert network number from network to presentation format.
50
* generates CIDR style result always.
52
* pointer to dst, or NULL if an error occurred (check errno).
54
* Paul Vixie (ISC), July 1996
57
inet_cidr_ntop(int af, const void *src, int bits, char *dst, size_t size)
62
return (inet_cidr_ntop_ipv4(src, bits, dst, size));
64
return (inet_cidr_ntop_ipv6(src, bits, dst, size));
74
* inet_cidr_ntop_ipv4(src, bits, dst, size)
75
* convert IPv4 network number from network to presentation format.
76
* generates CIDR style result always.
78
* pointer to dst, or NULL if an error occurred (check errno).
80
* network byte order assumed. this means 192.5.5.240/28 has
81
* 0b11110000 in its fourth octet.
83
* Paul Vixie (ISC), July 1996
86
inet_cidr_ntop_ipv4(const u_char *src, int bits, char *dst, size_t size)
93
if (bits < 0 || bits > 32)
101
if (size < sizeof "0")
108
/* Format whole octets. */
109
for (b = bits / 8; b > 0; b--)
111
if (size <= sizeof "255.")
114
dst += SPRINTF((dst, "%u", *src++));
120
size -= (size_t) (dst - t);
123
/* Format partial octet. */
127
if (size <= sizeof ".255")
132
m = ((1 << b) - 1) << (8 - b);
133
dst += SPRINTF((dst, "%u", *src & m));
134
size -= (size_t) (dst - t);
137
/* Format CIDR /width. */
138
if (size <= sizeof "/32")
140
dst += SPRINTF((dst, "/%u", bits));
150
* inet_cidr_ntop_ipv6(src, bits, fakebits, dst, size)
151
* convert IPv6 network number from network to presentation format.
152
* generates CIDR style result always. Picks the shortest representation
153
* unless the IP is really IPv4.
154
* always prints specified number of bits (bits).
156
* pointer to dst, or NULL if an error occurred (check errno).
158
* network byte order assumed. this means 192.5.5.240/28 has
159
* 0x11110000 in its fourth octet.
161
* Vadim Kogan (UCB), June 2001
162
* Original version (IPv4) by Paul Vixie (ISC), July 1996
166
inet_cidr_ntop_ipv6(const u_char *src, int bits, char *dst, size_t size)
177
unsigned char inbuf[16];
178
char outbuf[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255/128")];
183
if (bits < 0 || bits > 128)
199
/* Copy src to private buffer. Zero host part. */
201
memcpy(inbuf, src, p);
202
memset(inbuf + p, 0, 16 - p);
212
/* how many words need to be displayed in output */
213
words = (bits + 15) / 16;
217
/* Find the longest substring of zero's */
218
zero_s = zero_l = tmp_zero_s = tmp_zero_l = 0;
219
for (i = 0; i < (words * 2); i += 2)
221
if ((s[i] | s[i + 1]) == 0)
229
if (tmp_zero_l && zero_l < tmp_zero_l)
238
if (tmp_zero_l && zero_l < tmp_zero_l)
244
if (zero_l != words && zero_s == 0 && ((zero_l == 6) ||
245
((zero_l == 5 && s[10] == 0xff && s[11] == 0xff) ||
246
((zero_l == 7 && s[14] != 0 && s[15] != 1)))))
249
/* Format whole words. */
250
for (p = 0; p < words; p++)
252
if (zero_l != 0 && p >= zero_s && p < zero_s + zero_l)
254
/* Time to skip some zeros */
264
if (is_ipv4 && p > 5)
266
*cp++ = (p == 6) ? ':' : '.';
267
cp += SPRINTF((cp, "%u", *s++));
268
/* we can potentially drop the last octet */
269
if (p != 7 || bits > 120)
272
cp += SPRINTF((cp, "%u", *s++));
279
cp += SPRINTF((cp, "%x", *s * 256 + s[1]));
284
/* Format CIDR /width. */
285
(void) SPRINTF((cp, "/%u", bits));
286
if (strlen(outbuf) + 1 > size)