28 #include "utils/fmgroids.h"
42 #define ABBREV_BITS_INET4_NETMASK_SIZE 6
43 #define ABBREV_BITS_INET4_SUBNET 25
67 static 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)
509 "network_abbrev: estimation ends at cardinality %f"
523 if (abbr_card < uss->input_count / 2000.0 + 0.5)
528 "network_abbrev: aborting abbreviation at cardinality %f"
529 " below threshold %f after " INT64_FORMAT " values (%d rows)",
540 " values (%d rows)", abbr_card, uss->
input_count, memtupcount);
652 memcpy(&ipaddr_datum32,
ip_addr(authoritative),
sizeof(
uint32));
655 #ifndef WORDS_BIGENDIAN
658 ipaddr_datum = ipaddr_datum32;
666 memcpy(&ipaddr_datum,
ip_addr(authoritative),
sizeof(
Datum));
669 ipaddr_datum = DatumBigEndianToNative(ipaddr_datum);
695 if (
ip_bits(authoritative) == 0)
698 subnet_bitmask = ((
Datum) 0) - 1;
704 subnet_bitmask = (((
Datum) 1) << subnet_size) - 1;
705 network = ipaddr_datum & ~subnet_bitmask;
711 network = ipaddr_datum;
714 #if SIZEOF_DATUM == 8
741 subnet = ipaddr_datum & subnet_bitmask;
760 res |= network | netmask_size | subnet;
780 #if SIZEOF_DATUM == 8
1042 case F_NETWORK_SUBEQ:
1054 case F_NETWORK_SUPEQ:
1083 Oid datatype = INETOID;
1098 ((
Const *) rightop)->constisnull)
1100 rightopval = ((
Const *) rightop)->constvalue;
1110 if (opfamily != NETWORK_BTREE_FAM_OID)
1126 elog(
ERROR,
"no >= operator for opfamily %u", opfamily);
1133 elog(
ERROR,
"no > operator for opfamily %u", opfamily);
1152 elog(
ERROR,
"no <= operator for opfamily %u", opfamily);
1163 result =
lappend(result, expr);
1177 char tmp[
sizeof(
"xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255/128")];
1181 tmp,
sizeof(tmp)) == NULL)
1183 (
errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
1184 errmsg(
"could not format inet value: %m")));
1187 if ((ptr = strchr(tmp,
'/')) != NULL)
1203 char tmp[
sizeof(
"xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255/128")];
1206 tmp,
sizeof(tmp)) == NULL)
1208 (
errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
1209 errmsg(
"could not format inet value: %m")));
1212 if (strchr(tmp,
'/') == NULL)
1226 char tmp[
sizeof(
"xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255/128")];
1229 ip_bits(ip), tmp,
sizeof(tmp));
1233 (
errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
1234 errmsg(
"could not format inet value: %m")));
1244 char tmp[
sizeof(
"xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255/128")];
1247 ip_bits(ip), tmp,
sizeof(tmp));
1251 (
errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
1252 errmsg(
"could not format cidr value: %m")));
1304 for (
byte = 0;
byte < maxbytes;
byte++)
1315 mask = 0xff >> bits;
1319 b[byte] =
a[byte] | mask;
1358 mask = 0xff << (8 - bits);
1362 b[byte] =
a[byte] & mask;
1400 mask = 0xff << (8 - bits);
1433 byte = maxbytes - 1;
1444 mask = 0xff >> (8 - bits);
1484 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1485 errmsg(
"cannot merge addresses from different families")));
1523 for (
i = 0;
i <
len;
i++)
1535 res = (mac->
a << 16) | (mac->
b << 8) | (mac->
c);
1536 res *= 256 * 256 * 256;
1537 res += (mac->
d << 16) | (mac->
e << 8) | (mac->
f);
1545 res = (mac->
a << 24) | (mac->
b << 16) | (mac->
c << 8) | (mac->
d);
1546 res *= ((double) 256) * 256 * 256 * 256;
1547 res += (mac->
e << 24) | (mac->
f << 16) | (mac->
g << 8) | (mac->
h);
1569 bitncmp(
const unsigned char *l,
const unsigned char *r,
int n)
1577 x = memcmp(l, r,
b);
1578 if (
x || (n % 8) == 0)
1583 for (
b = n % 8;
b > 0;
b--)
1603 bitncommon(
const unsigned char *l,
const unsigned char *r,
int n)
1612 for (
byte = 0;
byte < n / 8;
byte++)
1614 if (l[
byte] != r[
byte])
1626 unsigned int diff = l[byte] ^ r[byte];
1629 while ((diff >> (8 - nbits)) != 0)
1633 return (8 *
byte) + nbits;
1661 if (bits == maxbits)
1671 while (
byte < maxbytes)
1673 if ((
a[
byte] & mask) != 0)
1719 char remote_host[NI_MAXHOST];
1725 switch (
port->raddr.addr.ss_family)
1734 remote_host[0] =
'\0';
1737 remote_host,
sizeof(remote_host),
1739 NI_NUMERICHOST | NI_NUMERICSERV);
1756 char remote_port[NI_MAXSERV];
1762 switch (
port->raddr.addr.ss_family)
1771 remote_port[0] =
'\0';
1775 remote_port,
sizeof(remote_port),
1776 NI_NUMERICHOST | NI_NUMERICSERV);
1791 char local_host[NI_MAXHOST];
1797 switch (
port->laddr.addr.ss_family)
1806 local_host[0] =
'\0';
1809 local_host,
sizeof(local_host),
1811 NI_NUMERICHOST | NI_NUMERICSERV);
1828 char local_port[NI_MAXSERV];
1834 switch (
port->laddr.addr.ss_family)
1843 local_port[0] =
'\0';
1847 local_port,
sizeof(local_port),
1848 NI_NUMERICHOST | NI_NUMERICSERV);
1866 unsigned char *pip =
ip_addr(ip);
1867 unsigned char *pdst =
ip_addr(dst);
1870 pdst[nb] = ~pip[nb];
1892 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1893 errmsg(
"cannot AND inet values of different sizes")));
1897 unsigned char *pip =
ip_addr(ip);
1898 unsigned char *pip2 =
ip_addr(ip2);
1899 unsigned char *pdst =
ip_addr(dst);
1902 pdst[nb] = pip[nb] & pip2[nb];
1924 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1925 errmsg(
"cannot OR inet values of different sizes")));
1929 unsigned char *pip =
ip_addr(ip);
1930 unsigned char *pip2 =
ip_addr(ip2);
1931 unsigned char *pdst =
ip_addr(dst);
1934 pdst[nb] = pip[nb] | pip2[nb];
1954 unsigned char *pip =
ip_addr(ip);
1955 unsigned char *pdst =
ip_addr(dst);
1960 carry = pip[nb] + (int) (addend & 0xFF) + carry;
1961 pdst[nb] = (
unsigned char) (carry & 0xFF);
1973 addend &= ~((int64) 0xFF);
1982 if (!((addend == 0 && carry == 0) ||
1983 (addend == -1 && carry == 1)))
1985 (
errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1986 errmsg(
"result is out of range")));
2026 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2027 errmsg(
"cannot subtract inet values of different sizes")));
2038 unsigned char *pip =
ip_addr(ip);
2039 unsigned char *pip2 =
ip_addr(ip2);
2046 carry = pip[nb] + (~pip2[nb] & 0xFF) + carry;
2047 lobyte = carry & 0xFF;
2048 if (
byte <
sizeof(int64))
2050 res |= ((int64) lobyte) << (
byte * 8);
2059 if ((
res < 0) ? (lobyte != 0xFF) : (lobyte != 0))
2061 (
errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
2062 errmsg(
"result is out of range")));
2072 if (carry == 0 &&
byte <
sizeof(int64))
2073 res |= ((uint64) (int64) -1) << (
byte * 8);
2097 if (addr_family == AF_INET6)
2099 char *pct = strchr(addr,
'%');
#define IS_HIGHBIT_SET(ch)
static void PGresult * res
elog(ERROR, "%s: %s", p2, msg)
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)
Assert(fmt[strlen(fmt) - 1] !='\n')
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)
static inet * network_recv(StringInfo buf, bool is_cidr)
Datum inet_client_addr(PG_FUNCTION_ARGS)
int bitncommon(const unsigned char *l, const unsigned char *r, int n)
Datum network_hostmask(PG_FUNCTION_ARGS)
Datum network_scan_last(Datum in)
Datum network_overlap(PG_FUNCTION_ARGS)
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)
static char * network_out(inet *src, bool is_cidr)
Datum inet_set_masklen(PG_FUNCTION_ARGS)
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)
static bytea * network_send(inet *addr, bool is_cidr)
Datum inet_same_family(PG_FUNCTION_ARGS)
inet * cidr_set_masklen_internal(const inet *src, int bits)
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)
Datum network_eq(PG_FUNCTION_ARGS)
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)
static inet * network_in(char *src, bool is_cidr, Node *escontext)
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)
#define SET_INET_VARSIZE(dst)
#define PG_GETARG_INET_PP(n)
static macaddr * DatumGetMacaddrP(Datum X)
#define ip_family(inetptr)
#define ip_addrsize(inetptr)
static inet * DatumGetInetPP(Datum X)
static macaddr8 * DatumGetMacaddr8P(Datum X)
#define ip_maxbits(inetptr)
text * cstring_to_text(const char *s)