28 #include "utils/fmgroids.h" 42 #define ABBREV_BITS_INET4_NETMASK_SIZE 6 43 #define ABBREV_BITS_INET4_SUBNET 25 68 static bool addressOK(
unsigned char *a,
int bits,
int family);
89 if (strchr(src,
':') != NULL)
98 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
100 errmsg(
"invalid input syntax for type %s: \"%s\"",
101 is_cidr ?
"cidr" :
"inet", src)));
110 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
111 errmsg(
"invalid cidr value: \"%s\"", src),
112 errdetail(
"Value has bits set to right of mask.")));
144 char tmp[
sizeof(
"xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255/128")];
152 (
errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
153 errmsg(
"could not format inet value: %m")));
156 if (is_cidr && strchr(tmp,
'/') == NULL)
208 (
errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
210 errmsg(
"invalid address family in external \"%s\" value",
211 is_cidr ?
"cidr" :
"inet")));
215 (
errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
217 errmsg(
"invalid bits in external \"%s\" value",
218 is_cidr ?
"cidr" :
"inet")));
224 (
errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
226 errmsg(
"invalid length in external \"%s\" value",
227 is_cidr ?
"cidr" :
"inet")));
229 addrptr = (
char *)
ip_addr(addr);
230 for (i = 0; i < nb; i++)
240 (
errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
241 errmsg(
"invalid external \"cidr\" value"),
242 errdetail(
"Value has bits set to right of mask.")));
286 addrptr = (
char *)
ip_addr(addr);
287 for (i = 0; i < nb; i++)
319 elog(
ERROR,
"invalid inet bit length: %d", bits);
336 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
337 errmsg(
"invalid mask length: %d", bits)));
359 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
360 errmsg(
"invalid mask length: %d", bits)));
385 ip_addr(dst)[bits / 8] &= ~(0xFF >> (bits % 8));
508 if (memtupcount < 10000 || uss->input_count < 10000 || !uss->estimating)
519 if (abbr_card > 100000.0)
524 "network_abbrev: estimation ends at cardinality %f" 538 if (abbr_card < uss->input_count / 2000.0 + 0.5)
543 "network_abbrev: aborting abbreviation at cardinality %f" 544 " below threshold %f after " INT64_FORMAT " values (%d rows)",
555 " values (%d rows)", abbr_card, uss->
input_count, memtupcount);
667 memcpy(&ipaddr_datum32,
ip_addr(authoritative),
sizeof(
uint32));
670 #ifndef WORDS_BIGENDIAN 673 ipaddr_datum = ipaddr_datum32;
681 memcpy(&ipaddr_datum,
ip_addr(authoritative),
sizeof(
Datum));
684 ipaddr_datum = DatumBigEndianToNative(ipaddr_datum);
710 if (
ip_bits(authoritative) == 0)
713 subnet_bitmask = ((
Datum) 0) - 1;
719 subnet_bitmask = (((
Datum) 1) << subnet_size) - 1;
720 network = ipaddr_datum & ~subnet_bitmask;
726 network = ipaddr_datum;
729 #if SIZEOF_DATUM == 8 756 subnet = ipaddr_datum & subnet_bitmask;
775 res |= network | netmask_size | subnet;
795 #if SIZEOF_DATUM == 8 1057 case F_NETWORK_SUBEQ:
1069 case F_NETWORK_SUPEQ:
1098 Oid datatype = INETOID;
1113 ((
Const *) rightop)->constisnull)
1115 rightopval = ((
Const *) rightop)->constvalue;
1125 if (opfamily != NETWORK_BTREE_FAM_OID)
1141 elog(
ERROR,
"no >= operator for opfamily %u", opfamily);
1148 elog(
ERROR,
"no > operator for opfamily %u", opfamily);
1167 elog(
ERROR,
"no <= operator for opfamily %u", opfamily);
1178 result =
lappend(result, expr);
1192 char tmp[
sizeof(
"xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255/128")];
1196 tmp,
sizeof(tmp)) == NULL)
1198 (
errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
1199 errmsg(
"could not format inet value: %m")));
1202 if ((ptr = strchr(tmp,
'/')) != NULL)
1218 char tmp[
sizeof(
"xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255/128")];
1221 tmp,
sizeof(tmp)) == NULL)
1223 (
errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
1224 errmsg(
"could not format inet value: %m")));
1227 if (strchr(tmp,
'/') == NULL)
1241 char tmp[
sizeof(
"xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255/128")];
1244 ip_bits(ip), tmp,
sizeof(tmp));
1248 (
errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
1249 errmsg(
"could not format inet value: %m")));
1259 char tmp[
sizeof(
"xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255/128")];
1262 ip_bits(ip), tmp,
sizeof(tmp));
1266 (
errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
1267 errmsg(
"could not format cidr value: %m")));
1319 for (byte = 0; byte < maxbytes; byte++)
1330 mask = 0xff >> bits;
1373 mask = 0xff << (8 - bits);
1415 mask = 0xff << (8 - bits);
1448 byte = maxbytes - 1;
1459 mask = 0xff >> (8 - bits);
1499 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1500 errmsg(
"cannot merge addresses from different families")));
1538 for (i = 0; i < len; i++)
1550 res = (mac->
a << 16) | (mac->
b << 8) | (mac->
c);
1551 res *= 256 * 256 * 256;
1552 res += (mac->
d << 16) | (mac->
e << 8) | (mac->
f);
1560 res = (mac->
a << 24) | (mac->
b << 16) | (mac->
c << 8) | (mac->
d);
1561 res *= ((double) 256) * 256 * 256 * 256;
1562 res += (mac->
e << 24) | (mac->
f << 16) | (mac->
g << 8) | (mac->
h);
1584 bitncmp(
const unsigned char *l,
const unsigned char *r,
int n)
1592 x = memcmp(l, r, b);
1593 if (x || (n % 8) == 0)
1598 for (b = n % 8; b > 0; b--)
1618 bitncommon(
const unsigned char *l,
const unsigned char *r,
int n)
1627 for (byte = 0; byte < n / 8; byte++)
1629 if (l[byte] != r[byte])
1641 unsigned int diff = l[
byte] ^ r[
byte];
1644 while ((diff >> (8 - nbits)) != 0)
1648 return (8 * byte) + nbits;
1676 if (bits == maxbits)
1686 while (byte < maxbytes)
1688 if ((a[byte] & mask) != 0)
1751 remote_host[0] =
'\0';
1754 remote_host,
sizeof(remote_host),
1790 remote_port[0] =
'\0';
1794 remote_port,
sizeof(remote_port),
1827 local_host[0] =
'\0';
1830 local_host,
sizeof(local_host),
1866 local_port[0] =
'\0';
1870 local_port,
sizeof(local_port),
1889 unsigned char *pip =
ip_addr(ip);
1890 unsigned char *pdst =
ip_addr(dst);
1893 pdst[nb] = ~pip[nb];
1915 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1916 errmsg(
"cannot AND inet values of different sizes")));
1920 unsigned char *pip =
ip_addr(ip);
1921 unsigned char *pip2 =
ip_addr(ip2);
1922 unsigned char *pdst =
ip_addr(dst);
1925 pdst[nb] = pip[nb] & pip2[nb];
1947 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1948 errmsg(
"cannot OR inet values of different sizes")));
1952 unsigned char *pip =
ip_addr(ip);
1953 unsigned char *pip2 =
ip_addr(ip2);
1954 unsigned char *pdst =
ip_addr(dst);
1957 pdst[nb] = pip[nb] | pip2[nb];
1977 unsigned char *pip =
ip_addr(ip);
1978 unsigned char *pdst =
ip_addr(dst);
1983 carry = pip[nb] + (int) (addend & 0xFF) + carry;
1984 pdst[nb] = (
unsigned char) (carry & 0xFF);
1996 addend &= ~((int64) 0xFF);
2005 if (!((addend == 0 && carry == 0) ||
2006 (addend == -1 && carry == 1)))
2008 (
errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
2009 errmsg(
"result is out of range")));
2049 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2050 errmsg(
"cannot subtract inet values of different sizes")));
2061 unsigned char *pip =
ip_addr(ip);
2062 unsigned char *pip2 =
ip_addr(ip2);
2069 carry = pip[nb] + (~pip2[nb] & 0xFF) + carry;
2070 lobyte = carry & 0xFF;
2071 if (byte <
sizeof(int64))
2073 res |= ((int64) lobyte) << (byte * 8);
2082 if ((res < 0) ? (lobyte != 0xFF) : (lobyte != 0))
2084 (
errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
2085 errmsg(
"result is out of range")));
2095 if (carry == 0 && byte <
sizeof(int64))
2096 res |= ((uint64) (int64) -1) << (byte * 8);
2121 if (addr_family == AF_INET6)
2123 char *pct = strchr(addr,
'%');
struct SortSupportData * SortSupport
#define PG_RETURN_POINTER(x)
Datum network_broadcast(PG_FUNCTION_ARGS)
#define DatumGetUInt32(X)
#define PG_GETARG_INT32(n)
#define ABBREV_BITS_INET4_SUBNET
Datum inet_same_family(PG_FUNCTION_ARGS)
#define IsA(nodeptr, _type_)
int pg_inet_net_pton(int af, const char *src, void *dst, size_t size)
Datum hashinet(PG_FUNCTION_ARGS)
#define DatumGetMacaddrP(X)
#define BTGreaterStrategyNumber
Datum inetpl(PG_FUNCTION_ARGS)
#define ip_family(inetptr)
static bool addressOK(unsigned char *a, int bits, int family)
Datum cidr_recv(PG_FUNCTION_ARGS)
#define PG_RETURN_INT64(x)
Datum network_scan_last(Datum in)
hyperLogLogState abbr_card
#define DatumGetInetPP(X)
char * pstrdup(const char *in)
Datum int4in(PG_FUNCTION_ARGS)
StringInfoData * StringInfo
#define SET_INET_VARSIZE(dst)
struct sockaddr_storage addr
Datum cidr_set_masklen(PG_FUNCTION_ARGS)
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
#define PG_RETURN_INT32(x)
Datum cidr_send(PG_FUNCTION_ARGS)
#define PG_RETURN_INET_P(x)
int bitncommon(const unsigned char *l, const unsigned char *r, int n)
int errcode(int sqlerrcode)
Datum network_family(PG_FUNCTION_ARGS)
Datum cidr_out(PG_FUNCTION_ARGS)
Datum inet_out(PG_FUNCTION_ARGS)
Datum network_host(PG_FUNCTION_ARGS)
#define PG_GETARG_POINTER(n)
Datum network_le(PG_FUNCTION_ARGS)
static inet * network_in(char *src, bool is_cidr)
#define ip_addrsize(inetptr)
#define DirectFunctionCall1(func, arg1)
#define PG_RETURN_BYTEA_P(x)
Datum network_netmask(PG_FUNCTION_ARGS)
static Datum hash_any_extended(const unsigned char *k, int keylen, uint64 seed)
Const * makeConst(Oid consttype, int32 consttypmod, Oid constcollid, int constlen, Datum constvalue, bool constisnull, bool constbyval)
Expr * make_opclause(Oid opno, Oid opresulttype, bool opretset, Expr *leftop, Expr *rightop, Oid opcollid, Oid inputcollid)
Datum network_masklen(PG_FUNCTION_ARGS)
Datum network_ne(PG_FUNCTION_ARGS)
Datum inet_set_masklen(PG_FUNCTION_ARGS)
Datum network_lt(PG_FUNCTION_ARGS)
static bool is_funcclause(const void *clause)
#define BTLessEqualStrategyNumber
#define ip_maxbits(inetptr)
static const FormData_pg_attribute a2
void initHyperLogLog(hyperLogLogState *cState, uint8 bwidth)
static int network_cmp_abbrev(Datum x, Datum y, SortSupport ssup)
#define IS_HIGHBIT_SET(ch)
Datum network_subset_support(PG_FUNCTION_ARGS)
double convert_network_to_scalar(Datum value, Oid typid, bool *failure)
int(* comparator)(Datum x, Datum y, SortSupport ssup)
static int32 network_cmp_internal(inet *a1, inet *a2)
int(* abbrev_full_comparator)(Datum x, Datum y, SortSupport ssup)
Datum network_show(PG_FUNCTION_ARGS)
#define PG_GETARG_INET_PP(n)
static List * match_network_subset(Node *leftop, Node *rightop, bool is_eq, Oid opfamily)
static List * match_network_function(Node *leftop, Node *rightop, int indexarg, Oid funcid, Oid opfamily)
int errdetail(const char *fmt,...)
#define CStringGetDatum(X)
Oid get_opfamily_member(Oid opfamily, Oid lefttype, Oid righttype, int16 strategy)
Datum(* abbrev_converter)(Datum original, SortSupport ssup)
char * pg_inet_net_ntop(int af, const void *src, int bits, char *dst, size_t size)
static Datum hash_any(const unsigned char *k, int keylen)
Datum network_smaller(PG_FUNCTION_ARGS)
static inet * internal_inetpl(inet *ip, int64 addend)
Datum network_sortsupport(PG_FUNCTION_ARGS)
Datum inetmi_int8(PG_FUNCTION_ARGS)
Datum network_gt(PG_FUNCTION_ARGS)
Datum network_eq(PG_FUNCTION_ARGS)
List * lappend(List *list, void *datum)
Datum network_ge(PG_FUNCTION_ARGS)
Datum network_scan_first(Datum in)
Datum inet_abbrev(PG_FUNCTION_ARGS)
void * palloc0(Size size)
Datum cidr_abbrev(PG_FUNCTION_ARGS)
#define PG_RETURN_BOOL(x)
#define PG_RETURN_DATUM(x)
Datum network_subeq(PG_FUNCTION_ARGS)
double estimateHyperLogLog(hyperLogLogState *cState)
Datum inet_to_cidr(PG_FUNCTION_ARGS)
#define ereport(elevel,...)
Datum inet_send(PG_FUNCTION_ARGS)
Datum inetmi(PG_FUNCTION_ARGS)
#define PG_RETURN_TEXT_P(x)
static bytea * network_send(inet *addr, bool is_cidr)
text * cstring_to_text(const char *s)
Datum network_overlap(PG_FUNCTION_ARGS)
#define Assert(condition)
int pg_getnameinfo_all(const struct sockaddr_storage *addr, int salen, char *node, int nodelen, char *service, int servicelen, int flags)
static Datum hash_uint32(uint32 k)
bool(* abbrev_abort)(int memtupcount, SortSupport ssup)
Datum inet_in(PG_FUNCTION_ARGS)
static char * network_out(inet *src, bool is_cidr)
#define PG_RETURN_CSTRING(x)
Datum hashinetextended(PG_FUNCTION_ARGS)
Datum inet_client_addr(PG_FUNCTION_ARGS)
void clean_ipv6_addr(int addr_family, char *addr)
static int list_length(const List *l)
Datum network_sup(PG_FUNCTION_ARGS)
inet * cidr_set_masklen_internal(const inet *src, int bits)
void addHyperLogLog(hyperLogLogState *cState, uint32 hash)
Datum inet_recv(PG_FUNCTION_ARGS)
Datum cidr_in(PG_FUNCTION_ARGS)
#define ABBREV_BITS_INET4_NETMASK_SIZE
static inet * network_recv(StringInfo buf, bool is_cidr)
Datum network_hostmask(PG_FUNCTION_ARGS)
static Datum network_abbrev_convert(Datum original, SortSupport ssup)
Datum inet_client_port(PG_FUNCTION_ARGS)
Datum inetor(PG_FUNCTION_ARGS)
static bool network_abbrev_abort(int memtupcount, SortSupport ssup)
Datum inet_server_port(PG_FUNCTION_ARGS)
#define DatumGetMacaddr8P(X)
Datum inetnot(PG_FUNCTION_ARGS)
int errmsg(const char *fmt,...)
int bitncmp(const unsigned char *l, const unsigned char *r, int n)
Datum network_larger(PG_FUNCTION_ARGS)
#define PG_GETARG_CSTRING(n)
static uint32 pg_bswap32(uint32 x)
Datum network_sub(PG_FUNCTION_ARGS)
Datum network_cmp(PG_FUNCTION_ARGS)
Datum network_supeq(PG_FUNCTION_ARGS)
Datum inet_merge(PG_FUNCTION_ARGS)
static bool is_opclause(const void *clause)
static const FormData_pg_attribute a1
char * pg_inet_cidr_ntop(int af, const void *src, int bits, char *dst, size_t size)
Datum inet_server_addr(PG_FUNCTION_ARGS)
#define PG_GETARG_INT64(n)
Datum network_network(PG_FUNCTION_ARGS)
static int network_fast_cmp(Datum x, Datum y, SortSupport ssup)
#define DirectFunctionCall2(func, arg1, arg2)
#define BTGreaterEqualStrategyNumber
Datum inetand(PG_FUNCTION_ARGS)