28#include "utils/fmgroids.h"
42#define ABBREV_BITS_INET4_NETMASK_SIZE 6
43#define ABBREV_BITS_INET4_SUBNET 25
67static bool addressOK(
unsigned char *
a,
int bits,
int family);
88 if (strchr(src,
':') != NULL)
97 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
99 errmsg(
"invalid input syntax for type %s: \"%s\"",
100 is_cidr ?
"cidr" :
"inet", src)));
109 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
110 errmsg(
"invalid cidr value: \"%s\"", src),
111 errdetail(
"Value has bits set to right of mask.")));
143 char tmp[
sizeof(
"xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255/128")];
151 (
errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
152 errmsg(
"could not format inet value: %m")));
155 if (is_cidr && strchr(tmp,
'/') == NULL)
207 (
errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
209 errmsg(
"invalid address family in external \"%s\" value",
210 is_cidr ?
"cidr" :
"inet")));
214 (
errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
216 errmsg(
"invalid bits in external \"%s\" value",
217 is_cidr ?
"cidr" :
"inet")));
223 (
errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
225 errmsg(
"invalid length in external \"%s\" value",
226 is_cidr ?
"cidr" :
"inet")));
228 addrptr = (
char *)
ip_addr(addr);
229 for (
i = 0;
i < nb;
i++)
239 (
errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
240 errmsg(
"invalid external \"cidr\" value"),
241 errdetail(
"Value has bits set to right of mask.")));
285 addrptr = (
char *)
ip_addr(addr);
286 for (
i = 0;
i < nb;
i++)
318 elog(
ERROR,
"invalid inet bit length: %d", bits);
335 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
336 errmsg(
"invalid mask length: %d", bits)));
358 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
359 errmsg(
"invalid mask length: %d", bits)));
384 ip_addr(dst)[bits / 8] &= ~(0xFF >> (bits % 8));
493 if (memtupcount < 10000 || uss->input_count < 10000 || !uss->estimating)
504 if (abbr_card > 100000.0)
508 "network_abbrev: estimation ends at cardinality %f"
521 if (abbr_card < uss->input_count / 2000.0 + 0.5)
525 "network_abbrev: aborting abbreviation at cardinality %f"
526 " below threshold %f after " INT64_FORMAT " values (%d rows)",
535 " values (%d rows)", abbr_card, uss->
input_count, memtupcount);
646 memcpy(&ipaddr_datum32,
ip_addr(authoritative),
sizeof(
uint32));
649#ifndef WORDS_BIGENDIAN
652 ipaddr_datum = ipaddr_datum32;
660 memcpy(&ipaddr_datum,
ip_addr(authoritative),
sizeof(
Datum));
663 ipaddr_datum = DatumBigEndianToNative(ipaddr_datum);
689 if (
ip_bits(authoritative) == 0)
692 subnet_bitmask = ((
Datum) 0) - 1;
698 subnet_bitmask = (((
Datum) 1) << subnet_size) - 1;
699 network = ipaddr_datum & ~subnet_bitmask;
705 network = ipaddr_datum;
735 subnet = ipaddr_datum & subnet_bitmask;
754 res |= network | netmask_size | subnet;
1036 case F_NETWORK_SUBEQ:
1048 case F_NETWORK_SUPEQ:
1077 Oid datatype = INETOID;
1092 ((
Const *) rightop)->constisnull)
1094 rightopval = ((
Const *) rightop)->constvalue;
1104 if (opfamily != NETWORK_BTREE_FAM_OID)
1120 elog(
ERROR,
"no >= operator for opfamily %u", opfamily);
1127 elog(
ERROR,
"no > operator for opfamily %u", opfamily);
1146 elog(
ERROR,
"no <= operator for opfamily %u", opfamily);
1157 result =
lappend(result, expr);
1171 char tmp[
sizeof(
"xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255/128")];
1175 tmp,
sizeof(tmp)) == NULL)
1177 (
errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
1178 errmsg(
"could not format inet value: %m")));
1181 if ((ptr = strchr(tmp,
'/')) != NULL)
1197 char tmp[
sizeof(
"xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255/128")];
1200 tmp,
sizeof(tmp)) == NULL)
1202 (
errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
1203 errmsg(
"could not format inet value: %m")));
1206 if (strchr(tmp,
'/') == NULL)
1220 char tmp[
sizeof(
"xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255/128")];
1223 ip_bits(ip), tmp,
sizeof(tmp));
1227 (
errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
1228 errmsg(
"could not format inet value: %m")));
1238 char tmp[
sizeof(
"xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255/128")];
1241 ip_bits(ip), tmp,
sizeof(tmp));
1245 (
errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
1246 errmsg(
"could not format cidr value: %m")));
1298 for (
byte = 0;
byte < maxbytes;
byte++)
1309 mask = 0xff >> bits;
1313 b[byte] =
a[byte] | mask;
1352 mask = 0xff << (8 - bits);
1356 b[byte] =
a[byte] & mask;
1394 mask = 0xff << (8 - bits);
1427 byte = maxbytes - 1;
1438 mask = 0xff >> (8 - bits);
1478 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1479 errmsg(
"cannot merge addresses from different families")));
1517 for (
i = 0;
i <
len;
i++)
1529 res = (mac->
a << 16) | (mac->
b << 8) | (mac->
c);
1530 res *= 256 * 256 * 256;
1531 res += (mac->
d << 16) | (mac->
e << 8) | (mac->
f);
1539 res = (mac->
a << 24) | (mac->
b << 16) | (mac->
c << 8) | (mac->
d);
1540 res *= ((double) 256) * 256 * 256 * 256;
1541 res += (mac->
e << 24) | (mac->
f << 16) | (mac->
g << 8) | (mac->
h);
1563bitncmp(
const unsigned char *l,
const unsigned char *r,
int n)
1571 x = memcmp(l, r,
b);
1572 if (
x || (n % 8) == 0)
1577 for (
b = n % 8;
b > 0;
b--)
1606 for (
byte = 0;
byte < n / 8;
byte++)
1608 if (l[
byte] != r[
byte])
1620 unsigned int diff = l[byte] ^ r[byte];
1623 while ((diff >> (8 - nbits)) != 0)
1627 return (8 *
byte) + nbits;
1655 if (bits == maxbits)
1665 while (
byte < maxbytes)
1667 if ((
a[
byte] & mask) != 0)
1713 char remote_host[NI_MAXHOST];
1719 switch (
port->raddr.addr.ss_family)
1728 remote_host[0] =
'\0';
1731 remote_host,
sizeof(remote_host),
1733 NI_NUMERICHOST | NI_NUMERICSERV);
1750 char remote_port[NI_MAXSERV];
1756 switch (
port->raddr.addr.ss_family)
1765 remote_port[0] =
'\0';
1769 remote_port,
sizeof(remote_port),
1770 NI_NUMERICHOST | NI_NUMERICSERV);
1785 char local_host[NI_MAXHOST];
1791 switch (
port->laddr.addr.ss_family)
1800 local_host[0] =
'\0';
1803 local_host,
sizeof(local_host),
1805 NI_NUMERICHOST | NI_NUMERICSERV);
1822 char local_port[NI_MAXSERV];
1828 switch (
port->laddr.addr.ss_family)
1837 local_port[0] =
'\0';
1841 local_port,
sizeof(local_port),
1842 NI_NUMERICHOST | NI_NUMERICSERV);
1860 unsigned char *pip =
ip_addr(ip);
1861 unsigned char *pdst =
ip_addr(dst);
1864 pdst[nb] = ~pip[nb];
1886 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1887 errmsg(
"cannot AND inet values of different sizes")));
1891 unsigned char *pip =
ip_addr(ip);
1892 unsigned char *pip2 =
ip_addr(ip2);
1893 unsigned char *pdst =
ip_addr(dst);
1896 pdst[nb] = pip[nb] & pip2[nb];
1918 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1919 errmsg(
"cannot OR inet values of different sizes")));
1923 unsigned char *pip =
ip_addr(ip);
1924 unsigned char *pip2 =
ip_addr(ip2);
1925 unsigned char *pdst =
ip_addr(dst);
1928 pdst[nb] = pip[nb] | pip2[nb];
1948 unsigned char *pip =
ip_addr(ip);
1949 unsigned char *pdst =
ip_addr(dst);
1954 carry = pip[nb] + (int) (addend & 0xFF) + carry;
1955 pdst[nb] = (
unsigned char) (carry & 0xFF);
1967 addend &= ~((
int64) 0xFF);
1976 if (!((addend == 0 && carry == 0) ||
1977 (addend == -1 && carry == 1)))
1979 (
errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1980 errmsg(
"result is out of range")));
2020 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2021 errmsg(
"cannot subtract inet values of different sizes")));
2032 unsigned char *pip =
ip_addr(ip);
2033 unsigned char *pip2 =
ip_addr(ip2);
2040 carry = pip[nb] + (~pip2[nb] & 0xFF) + carry;
2041 lobyte = carry & 0xFF;
2042 if (
byte <
sizeof(
int64))
2044 res |= ((
int64) lobyte) << (
byte * 8);
2053 if ((
res < 0) ? (lobyte != 0xFF) : (lobyte != 0))
2055 (
errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
2056 errmsg(
"result is out of range")));
2066 if (carry == 0 &&
byte <
sizeof(
int64))
2091 if (addr_family == AF_INET6)
2093 char *pct = strchr(addr,
'%');
#define IS_HIGHBIT_SET(ch)
#define Assert(condition)
static void PGresult * res
int errdetail(const char *fmt,...)
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereturn(context, dummy_value,...)
#define ereport(elevel,...)
#define PG_RETURN_BYTEA_P(x)
#define DirectFunctionCall2(func, arg1, arg2)
#define PG_GETARG_POINTER(n)
#define PG_RETURN_CSTRING(x)
#define PG_RETURN_INT64(x)
#define DirectFunctionCall1(func, arg1)
#define PG_GETARG_CSTRING(n)
#define PG_GETARG_INT64(n)
#define PG_RETURN_TEXT_P(x)
#define PG_RETURN_INT32(x)
#define PG_GETARG_INT32(n)
#define PG_RETURN_DATUM(x)
#define PG_RETURN_POINTER(x)
#define PG_RETURN_BOOL(x)
static Datum hash_uint32(uint32 k)
static Datum hash_any_extended(const unsigned char *k, int keylen, uint64 seed)
static Datum hash_any(const unsigned char *k, int keylen)
static const FormData_pg_attribute a1
static const FormData_pg_attribute a2
void initHyperLogLog(hyperLogLogState *cState, uint8 bwidth)
double estimateHyperLogLog(hyperLogLogState *cState)
void addHyperLogLog(hyperLogLogState *cState, uint32 hash)
char * pg_inet_cidr_ntop(int af, const void *src, int bits, char *dst, size_t size)
int pg_inet_net_pton(int af, const char *src, void *dst, size_t size)
Datum int4in(PG_FUNCTION_ARGS)
int pg_getnameinfo_all(const struct sockaddr_storage *addr, int salen, char *node, int nodelen, char *service, int servicelen, int flags)
List * lappend(List *list, void *datum)
Oid get_opfamily_member(Oid opfamily, Oid lefttype, Oid righttype, int16 strategy)
Expr * make_opclause(Oid opno, Oid opresulttype, bool opretset, Expr *leftop, Expr *rightop, Oid opcollid, Oid inputcollid)
Const * makeConst(Oid consttype, int32 consttypmod, Oid constcollid, int constlen, Datum constvalue, bool constisnull, bool constbyval)
char * pstrdup(const char *in)
void * palloc0(Size size)
Datum inet_server_addr(PG_FUNCTION_ARGS)
Datum hashinet(PG_FUNCTION_ARGS)
static List * match_network_subset(Node *leftop, Node *rightop, bool is_eq, Oid opfamily)
static int network_fast_cmp(Datum x, Datum y, SortSupport ssup)
Datum inet_send(PG_FUNCTION_ARGS)
static List * match_network_function(Node *leftop, Node *rightop, int indexarg, Oid funcid, Oid opfamily)
void clean_ipv6_addr(int addr_family, char *addr)
Datum cidr_out(PG_FUNCTION_ARGS)
Datum inet_client_addr(PG_FUNCTION_ARGS)
int bitncommon(const unsigned char *l, const unsigned char *r, int n)
inet * cidr_set_masklen_internal(const inet *src, int bits)
Datum network_hostmask(PG_FUNCTION_ARGS)
Datum network_scan_last(Datum in)
Datum network_overlap(PG_FUNCTION_ARGS)
static inet * network_in(char *src, bool is_cidr, Node *escontext)
Datum inet_to_cidr(PG_FUNCTION_ARGS)
Datum inet_abbrev(PG_FUNCTION_ARGS)
Datum inetand(PG_FUNCTION_ARGS)
Datum network_subset_support(PG_FUNCTION_ARGS)
Datum network_sup(PG_FUNCTION_ARGS)
Datum network_host(PG_FUNCTION_ARGS)
Datum network_supeq(PG_FUNCTION_ARGS)
Datum inetpl(PG_FUNCTION_ARGS)
Datum cidr_abbrev(PG_FUNCTION_ARGS)
Datum inet_server_port(PG_FUNCTION_ARGS)
static bool addressOK(unsigned char *a, int bits, int family)
int bitncmp(const unsigned char *l, const unsigned char *r, int n)
Datum network_netmask(PG_FUNCTION_ARGS)
Datum network_broadcast(PG_FUNCTION_ARGS)
Datum network_smaller(PG_FUNCTION_ARGS)
Datum inetor(PG_FUNCTION_ARGS)
Datum inet_set_masklen(PG_FUNCTION_ARGS)
static inet * network_recv(StringInfo buf, bool is_cidr)
Datum inet_in(PG_FUNCTION_ARGS)
Datum inetnot(PG_FUNCTION_ARGS)
Datum network_ge(PG_FUNCTION_ARGS)
#define ABBREV_BITS_INET4_SUBNET
Datum inetmi_int8(PG_FUNCTION_ARGS)
Datum inet_same_family(PG_FUNCTION_ARGS)
double convert_network_to_scalar(Datum value, Oid typid, bool *failure)
Datum hashinetextended(PG_FUNCTION_ARGS)
Datum cidr_send(PG_FUNCTION_ARGS)
Datum network_sub(PG_FUNCTION_ARGS)
Datum network_ne(PG_FUNCTION_ARGS)
#define ABBREV_BITS_INET4_NETMASK_SIZE
Datum network_le(PG_FUNCTION_ARGS)
Datum network_sortsupport(PG_FUNCTION_ARGS)
Datum cidr_recv(PG_FUNCTION_ARGS)
Datum inet_client_port(PG_FUNCTION_ARGS)
static inet * internal_inetpl(inet *ip, int64 addend)
Datum inetmi(PG_FUNCTION_ARGS)
Datum cidr_set_masklen(PG_FUNCTION_ARGS)
Datum network_family(PG_FUNCTION_ARGS)
Datum network_show(PG_FUNCTION_ARGS)
static int32 network_cmp_internal(inet *a1, inet *a2)
Datum network_lt(PG_FUNCTION_ARGS)
static bytea * network_send(inet *addr, bool is_cidr)
Datum network_eq(PG_FUNCTION_ARGS)
static char * network_out(inet *src, bool is_cidr)
Datum network_subeq(PG_FUNCTION_ARGS)
Datum network_gt(PG_FUNCTION_ARGS)
Datum inet_merge(PG_FUNCTION_ARGS)
Datum inet_recv(PG_FUNCTION_ARGS)
Datum network_cmp(PG_FUNCTION_ARGS)
Datum network_larger(PG_FUNCTION_ARGS)
Datum network_network(PG_FUNCTION_ARGS)
static Datum network_abbrev_convert(Datum original, SortSupport ssup)
static bool network_abbrev_abort(int memtupcount, SortSupport ssup)
Datum network_masklen(PG_FUNCTION_ARGS)
Datum network_scan_first(Datum in)
Datum inet_out(PG_FUNCTION_ARGS)
Datum cidr_in(PG_FUNCTION_ARGS)
static bool is_opclause(const void *clause)
static bool is_funcclause(const void *clause)
#define IsA(nodeptr, _type_)
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
static uint32 pg_bswap32(uint32 x)
static int list_length(const List *l)
char * pg_inet_net_ntop(int af, const void *src, int bits, char *dst, size_t size)
static uint32 DatumGetUInt32(Datum X)
static Datum CStringGetDatum(const char *X)
static Datum Int32GetDatum(int32 X)
struct SortSupportData * SortSupport
#define BTGreaterStrategyNumber
#define BTLessEqualStrategyNumber
#define BTGreaterEqualStrategyNumber
StringInfoData * StringInfo
int(* comparator)(Datum x, Datum y, SortSupport ssup)
Datum(* abbrev_converter)(Datum original, SortSupport ssup)
int(* abbrev_full_comparator)(Datum x, Datum y, SortSupport ssup)
bool(* abbrev_abort)(int memtupcount, SortSupport ssup)
hyperLogLogState abbr_card
int ssup_datum_unsigned_cmp(Datum x, Datum y, SortSupport ssup)
#define PG_RETURN_INET_P(x)
static macaddr8 * DatumGetMacaddr8P(Datum X)
static inet * DatumGetInetPP(Datum X)
#define SET_INET_VARSIZE(dst)
#define PG_GETARG_INET_PP(n)
#define ip_family(inetptr)
static macaddr * DatumGetMacaddrP(Datum X)
#define ip_addrsize(inetptr)
#define ip_maxbits(inetptr)
text * cstring_to_text(const char *s)