1998-10-03 07:41:01 +02:00
|
|
|
/*
|
2005-02-01 01:59:09 +01:00
|
|
|
* Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
|
|
|
|
* Copyright (c) 1996,1999 by Internet Software Consortium.
|
1998-10-03 07:41:01 +02:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
2005-02-01 01:59:09 +01:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
|
|
|
|
* OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
2003-11-10 20:40:46 +01:00
|
|
|
*
|
2018-05-14 18:59:43 +02:00
|
|
|
* src/port/inet_net_ntop.c
|
1998-10-03 07:41:01 +02:00
|
|
|
*/
|
|
|
|
|
2005-02-01 01:59:09 +01:00
|
|
|
#if defined(LIBC_SCCS) && !defined(lint)
|
|
|
|
static const char rcsid[] = "Id: inet_net_ntop.c,v 1.1.2.2 2004/03/09 09:17:27 marka Exp $";
|
|
|
|
#endif
|
|
|
|
|
2010-11-26 23:59:10 +01:00
|
|
|
#ifndef FRONTEND
|
2002-09-02 04:47:07 +02:00
|
|
|
#include "postgres.h"
|
2010-11-26 23:59:10 +01:00
|
|
|
#else
|
|
|
|
#include "postgres_fe.h"
|
|
|
|
#endif
|
2002-09-02 04:47:07 +02:00
|
|
|
|
1998-10-03 07:41:01 +02:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
2010-11-26 23:59:10 +01:00
|
|
|
#ifndef FRONTEND
|
2003-06-25 00:21:24 +02:00
|
|
|
#include "utils/inet.h"
|
2010-11-26 23:59:10 +01:00
|
|
|
#else
|
|
|
|
/*
|
|
|
|
* In a frontend build, we can't include inet.h, but we still need to have
|
2019-08-19 01:27:23 +02:00
|
|
|
* sensible definitions of these two constants. Note that pg_inet_net_ntop()
|
2010-11-26 23:59:10 +01:00
|
|
|
* assumes that PGSQL_AF_INET is equal to AF_INET.
|
|
|
|
*/
|
|
|
|
#define PGSQL_AF_INET (AF_INET + 0)
|
|
|
|
#define PGSQL_AF_INET6 (AF_INET + 1)
|
|
|
|
#endif
|
1998-10-04 17:35:12 +02:00
|
|
|
|
2005-02-01 01:59:09 +01:00
|
|
|
|
2003-06-25 00:21:24 +02:00
|
|
|
#define NS_IN6ADDRSZ 16
|
|
|
|
#define NS_INT16SZ 2
|
|
|
|
|
1998-10-03 07:41:01 +02:00
|
|
|
#ifdef SPRINTF_CHAR
|
|
|
|
#define SPRINTF(x) strlen(sprintf/**/x)
|
|
|
|
#else
|
|
|
|
#define SPRINTF(x) ((size_t)sprintf x)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static char *inet_net_ntop_ipv4(const u_char *src, int bits,
|
2019-05-22 19:04:48 +02:00
|
|
|
char *dst, size_t size);
|
2003-06-25 00:21:24 +02:00
|
|
|
static char *inet_net_ntop_ipv6(const u_char *src, int bits,
|
2019-05-22 19:04:48 +02:00
|
|
|
char *dst, size_t size);
|
1998-10-22 15:16:27 +02:00
|
|
|
|
2005-02-01 01:59:09 +01:00
|
|
|
|
1998-10-22 15:16:27 +02:00
|
|
|
/*
|
|
|
|
* char *
|
2019-08-19 01:27:23 +02:00
|
|
|
* pg_inet_net_ntop(af, src, bits, dst, size)
|
1998-10-22 15:16:27 +02:00
|
|
|
* convert host/network address from network to presentation format.
|
|
|
|
* "src"'s size is determined from its "af".
|
|
|
|
* return:
|
|
|
|
* pointer to dst, or NULL if an error occurred (check errno).
|
|
|
|
* note:
|
|
|
|
* 192.5.5.1/28 has a nonzero host part, which means it isn't a network
|
2019-08-19 01:27:23 +02:00
|
|
|
* as called for by pg_inet_net_pton() but it can be a host address with
|
1998-10-22 15:16:27 +02:00
|
|
|
* an included netmask.
|
|
|
|
* author:
|
|
|
|
* Paul Vixie (ISC), October 1998
|
|
|
|
*/
|
|
|
|
char *
|
2019-08-19 01:27:23 +02:00
|
|
|
pg_inet_net_ntop(int af, const void *src, int bits, char *dst, size_t size)
|
1998-10-22 15:16:27 +02:00
|
|
|
{
|
2010-11-26 23:59:10 +01:00
|
|
|
/*
|
2011-04-10 17:42:00 +02:00
|
|
|
* We need to cover both the address family constants used by the PG inet
|
|
|
|
* type (PGSQL_AF_INET and PGSQL_AF_INET6) and those used by the system
|
|
|
|
* libraries (AF_INET and AF_INET6). We can safely assume PGSQL_AF_INET
|
2011-06-09 20:32:50 +02:00
|
|
|
* == AF_INET, but the INET6 constants are very likely to be different. If
|
|
|
|
* AF_INET6 isn't defined, silently ignore it.
|
2010-11-26 23:59:10 +01:00
|
|
|
*/
|
1998-10-22 15:16:27 +02:00
|
|
|
switch (af)
|
|
|
|
{
|
2003-06-25 00:21:24 +02:00
|
|
|
case PGSQL_AF_INET:
|
1998-10-22 15:16:27 +02:00
|
|
|
return (inet_net_ntop_ipv4(src, bits, dst, size));
|
2003-06-25 00:21:24 +02:00
|
|
|
case PGSQL_AF_INET6:
|
2010-11-26 23:59:10 +01:00
|
|
|
#if defined(AF_INET6) && AF_INET6 != PGSQL_AF_INET6
|
|
|
|
case AF_INET6:
|
|
|
|
#endif
|
2003-06-25 00:21:24 +02:00
|
|
|
return (inet_net_ntop_ipv6(src, bits, dst, size));
|
1998-10-22 15:16:27 +02:00
|
|
|
default:
|
|
|
|
errno = EAFNOSUPPORT;
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* static char *
|
|
|
|
* inet_net_ntop_ipv4(src, bits, dst, size)
|
|
|
|
* convert IPv4 network address from network to presentation format.
|
|
|
|
* "src"'s size is determined from its "af".
|
|
|
|
* return:
|
|
|
|
* pointer to dst, or NULL if an error occurred (check errno).
|
|
|
|
* note:
|
|
|
|
* network byte order assumed. this means 192.5.5.240/28 has
|
|
|
|
* 0b11110000 in its fourth octet.
|
|
|
|
* author:
|
|
|
|
* Paul Vixie (ISC), October 1998
|
|
|
|
*/
|
|
|
|
static char *
|
|
|
|
inet_net_ntop_ipv4(const u_char *src, int bits, char *dst, size_t size)
|
|
|
|
{
|
1999-05-25 18:15:34 +02:00
|
|
|
char *odst = dst;
|
|
|
|
char *t;
|
2000-11-10 21:13:27 +01:00
|
|
|
int len = 4;
|
|
|
|
int b;
|
1998-10-22 15:16:27 +02:00
|
|
|
|
|
|
|
if (bits < 0 || bits > 32)
|
|
|
|
{
|
|
|
|
errno = EINVAL;
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
2000-11-10 21:13:27 +01:00
|
|
|
/* Always format all four octets, regardless of mask length. */
|
|
|
|
for (b = len; b > 0; b--)
|
1998-10-22 15:16:27 +02:00
|
|
|
{
|
2005-02-01 01:59:09 +01:00
|
|
|
if (size <= sizeof ".255")
|
1998-10-22 15:16:27 +02:00
|
|
|
goto emsgsize;
|
|
|
|
t = dst;
|
2000-11-10 21:13:27 +01:00
|
|
|
if (dst != odst)
|
1998-10-22 15:16:27 +02:00
|
|
|
*dst++ = '.';
|
2000-11-10 21:13:27 +01:00
|
|
|
dst += SPRINTF((dst, "%u", *src++));
|
1999-05-25 18:15:34 +02:00
|
|
|
size -= (size_t) (dst - t);
|
1998-10-22 15:16:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* don't print masklen if 32 bits */
|
2000-11-10 21:13:27 +01:00
|
|
|
if (bits != 32)
|
|
|
|
{
|
2005-02-01 01:59:09 +01:00
|
|
|
if (size <= sizeof "/32")
|
2000-11-10 21:13:27 +01:00
|
|
|
goto emsgsize;
|
|
|
|
dst += SPRINTF((dst, "/%u", bits));
|
|
|
|
}
|
1998-10-22 15:16:27 +02:00
|
|
|
|
|
|
|
return (odst);
|
|
|
|
|
1999-05-25 18:15:34 +02:00
|
|
|
emsgsize:
|
1998-10-22 15:16:27 +02:00
|
|
|
errno = EMSGSIZE;
|
|
|
|
return (NULL);
|
|
|
|
}
|
2003-06-25 00:21:24 +02:00
|
|
|
|
|
|
|
static int
|
2003-08-04 02:43:34 +02:00
|
|
|
decoct(const u_char *src, int bytes, char *dst, size_t size)
|
|
|
|
{
|
|
|
|
char *odst = dst;
|
|
|
|
char *t;
|
|
|
|
int b;
|
2003-06-25 00:21:24 +02:00
|
|
|
|
2003-08-04 02:43:34 +02:00
|
|
|
for (b = 1; b <= bytes; b++)
|
|
|
|
{
|
2005-02-01 01:59:09 +01:00
|
|
|
if (size <= sizeof "255.")
|
2003-06-25 00:21:24 +02:00
|
|
|
return (0);
|
|
|
|
t = dst;
|
|
|
|
dst += SPRINTF((dst, "%u", *src++));
|
2003-08-04 02:43:34 +02:00
|
|
|
if (b != bytes)
|
|
|
|
{
|
2003-06-25 00:21:24 +02:00
|
|
|
*dst++ = '.';
|
|
|
|
*dst = '\0';
|
|
|
|
}
|
2003-08-04 02:43:34 +02:00
|
|
|
size -= (size_t) (dst - t);
|
2003-06-25 00:21:24 +02:00
|
|
|
}
|
|
|
|
return (dst - odst);
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
inet_net_ntop_ipv6(const u_char *src, int bits, char *dst, size_t size)
|
|
|
|
{
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Note that int32_t and int16_t need only be "at least" large enough to
|
|
|
|
* contain a value of the specified size. On some systems, like Crays,
|
|
|
|
* there is no such thing as an integer variable with 16 bits. Keep this
|
|
|
|
* in mind if you think this function should have been coded to use
|
|
|
|
* pointer overlays. All the world's not a VAX.
|
2003-06-25 00:21:24 +02:00
|
|
|
*/
|
2003-08-04 02:43:34 +02:00
|
|
|
char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255/128"];
|
|
|
|
char *tp;
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
int base,
|
|
|
|
len;
|
|
|
|
} best, cur;
|
|
|
|
u_int words[NS_IN6ADDRSZ / NS_INT16SZ];
|
|
|
|
int i;
|
2003-06-25 00:21:24 +02:00
|
|
|
|
2003-08-04 02:43:34 +02:00
|
|
|
if ((bits < -1) || (bits > 128))
|
|
|
|
{
|
2003-06-25 00:21:24 +02:00
|
|
|
errno = EINVAL;
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Preprocess: Copy the input (bytewise) array into a wordwise array. Find
|
|
|
|
* the longest run of 0x00's in src[] for :: shorthanding.
|
2003-06-25 00:21:24 +02:00
|
|
|
*/
|
|
|
|
memset(words, '\0', sizeof words);
|
|
|
|
for (i = 0; i < NS_IN6ADDRSZ; i++)
|
|
|
|
words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
|
|
|
|
best.base = -1;
|
|
|
|
cur.base = -1;
|
2005-09-25 00:54:44 +02:00
|
|
|
best.len = 0;
|
|
|
|
cur.len = 0;
|
2003-08-04 02:43:34 +02:00
|
|
|
for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++)
|
|
|
|
{
|
|
|
|
if (words[i] == 0)
|
|
|
|
{
|
2003-06-25 00:21:24 +02:00
|
|
|
if (cur.base == -1)
|
|
|
|
cur.base = i, cur.len = 1;
|
|
|
|
else
|
|
|
|
cur.len++;
|
2003-08-04 02:43:34 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (cur.base != -1)
|
|
|
|
{
|
2003-06-25 00:21:24 +02:00
|
|
|
if (best.base == -1 || cur.len > best.len)
|
|
|
|
best = cur;
|
|
|
|
cur.base = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2003-08-04 02:43:34 +02:00
|
|
|
if (cur.base != -1)
|
|
|
|
{
|
2003-06-25 00:21:24 +02:00
|
|
|
if (best.base == -1 || cur.len > best.len)
|
|
|
|
best = cur;
|
|
|
|
}
|
|
|
|
if (best.base != -1 && best.len < 2)
|
|
|
|
best.base = -1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Format the result.
|
|
|
|
*/
|
|
|
|
tp = tmp;
|
2003-08-04 02:43:34 +02:00
|
|
|
for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++)
|
|
|
|
{
|
2003-06-25 00:21:24 +02:00
|
|
|
/* Are we inside the best run of 0x00's? */
|
|
|
|
if (best.base != -1 && i >= best.base &&
|
2003-08-04 02:43:34 +02:00
|
|
|
i < (best.base + best.len))
|
|
|
|
{
|
2003-06-25 00:21:24 +02:00
|
|
|
if (i == best.base)
|
|
|
|
*tp++ = ':';
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* Are we following an initial run of 0x00s or any real hex? */
|
|
|
|
if (i != 0)
|
|
|
|
*tp++ = ':';
|
|
|
|
/* Is this address an encapsulated IPv4? */
|
|
|
|
if (i == 6 && best.base == 0 && (best.len == 6 ||
|
Phase 3 of pgindent updates.
Don't move parenthesized lines to the left, even if that means they
flow past the right margin.
By default, BSD indent lines up statement continuation lines that are
within parentheses so that they start just to the right of the preceding
left parenthesis. However, traditionally, if that resulted in the
continuation line extending to the right of the desired right margin,
then indent would push it left just far enough to not overrun the margin,
if it could do so without making the continuation line start to the left of
the current statement indent. That makes for a weird mix of indentations
unless one has been completely rigid about never violating the 80-column
limit.
This behavior has been pretty universally panned by Postgres developers.
Hence, disable it with indent's new -lpl switch, so that parenthesized
lines are always lined up with the preceding left paren.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-21 21:35:54 +02:00
|
|
|
(best.len == 7 && words[7] != 0x0001) ||
|
|
|
|
(best.len == 5 && words[5] == 0xffff)))
|
2003-08-04 02:43:34 +02:00
|
|
|
{
|
|
|
|
int n;
|
|
|
|
|
|
|
|
n = decoct(src + 12, 4, tp, sizeof tmp - (tp - tmp));
|
|
|
|
if (n == 0)
|
|
|
|
{
|
2003-06-25 00:21:24 +02:00
|
|
|
errno = EMSGSIZE;
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
tp += strlen(tp);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
tp += SPRINTF((tp, "%x", words[i]));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Was it a trailing run of 0x00's? */
|
2003-08-04 02:43:34 +02:00
|
|
|
if (best.base != -1 && (best.base + best.len) ==
|
|
|
|
(NS_IN6ADDRSZ / NS_INT16SZ))
|
2003-06-25 00:21:24 +02:00
|
|
|
*tp++ = ':';
|
|
|
|
*tp = '\0';
|
|
|
|
|
|
|
|
if (bits != -1 && bits != 128)
|
|
|
|
tp += SPRINTF((tp, "/%u", bits));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check for overflow, copy, and we're done.
|
|
|
|
*/
|
2003-08-04 02:43:34 +02:00
|
|
|
if ((size_t) (tp - tmp) > size)
|
|
|
|
{
|
2003-06-25 00:21:24 +02:00
|
|
|
errno = EMSGSIZE;
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
strcpy(dst, tmp);
|
|
|
|
return (dst);
|
|
|
|
}
|