PostgreSQL Source Code  git master
network.c File Reference
#include "postgres.h"
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "access/stratnum.h"
#include "catalog/pg_opfamily.h"
#include "catalog/pg_type.h"
#include "common/hashfn.h"
#include "common/ip.h"
#include "lib/hyperloglog.h"
#include "libpq/libpq-be.h"
#include "libpq/pqformat.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "nodes/supportnodes.h"
#include "utils/builtins.h"
#include "utils/fmgroids.h"
#include "utils/guc.h"
#include "utils/inet.h"
#include "utils/lsyscache.h"
#include "utils/sortsupport.h"
Include dependency graph for network.c:

Go to the source code of this file.

Data Structures

struct  network_sortsupport_state
 

Macros

#define ABBREV_BITS_INET4_NETMASK_SIZE   6
 
#define ABBREV_BITS_INET4_SUBNET   25
 

Functions

static int32 network_cmp_internal (inet *a1, inet *a2)
 
static int network_fast_cmp (Datum x, Datum y, SortSupport ssup)
 
static bool network_abbrev_abort (int memtupcount, SortSupport ssup)
 
static Datum network_abbrev_convert (Datum original, SortSupport ssup)
 
static Listmatch_network_function (Node *leftop, Node *rightop, int indexarg, Oid funcid, Oid opfamily)
 
static Listmatch_network_subset (Node *leftop, Node *rightop, bool is_eq, Oid opfamily)
 
static bool addressOK (unsigned char *a, int bits, int family)
 
static inetinternal_inetpl (inet *ip, int64 addend)
 
static inetnetwork_in (char *src, bool is_cidr)
 
Datum inet_in (PG_FUNCTION_ARGS)
 
Datum cidr_in (PG_FUNCTION_ARGS)
 
static char * network_out (inet *src, bool is_cidr)
 
Datum inet_out (PG_FUNCTION_ARGS)
 
Datum cidr_out (PG_FUNCTION_ARGS)
 
static inetnetwork_recv (StringInfo buf, bool is_cidr)
 
Datum inet_recv (PG_FUNCTION_ARGS)
 
Datum cidr_recv (PG_FUNCTION_ARGS)
 
static byteanetwork_send (inet *addr, bool is_cidr)
 
Datum inet_send (PG_FUNCTION_ARGS)
 
Datum cidr_send (PG_FUNCTION_ARGS)
 
Datum inet_to_cidr (PG_FUNCTION_ARGS)
 
Datum inet_set_masklen (PG_FUNCTION_ARGS)
 
Datum cidr_set_masklen (PG_FUNCTION_ARGS)
 
inetcidr_set_masklen_internal (const inet *src, int bits)
 
Datum network_cmp (PG_FUNCTION_ARGS)
 
Datum network_sortsupport (PG_FUNCTION_ARGS)
 
Datum network_lt (PG_FUNCTION_ARGS)
 
Datum network_le (PG_FUNCTION_ARGS)
 
Datum network_eq (PG_FUNCTION_ARGS)
 
Datum network_ge (PG_FUNCTION_ARGS)
 
Datum network_gt (PG_FUNCTION_ARGS)
 
Datum network_ne (PG_FUNCTION_ARGS)
 
Datum network_smaller (PG_FUNCTION_ARGS)
 
Datum network_larger (PG_FUNCTION_ARGS)
 
Datum hashinet (PG_FUNCTION_ARGS)
 
Datum hashinetextended (PG_FUNCTION_ARGS)
 
Datum network_sub (PG_FUNCTION_ARGS)
 
Datum network_subeq (PG_FUNCTION_ARGS)
 
Datum network_sup (PG_FUNCTION_ARGS)
 
Datum network_supeq (PG_FUNCTION_ARGS)
 
Datum network_overlap (PG_FUNCTION_ARGS)
 
Datum network_subset_support (PG_FUNCTION_ARGS)
 
Datum network_host (PG_FUNCTION_ARGS)
 
Datum network_show (PG_FUNCTION_ARGS)
 
Datum inet_abbrev (PG_FUNCTION_ARGS)
 
Datum cidr_abbrev (PG_FUNCTION_ARGS)
 
Datum network_masklen (PG_FUNCTION_ARGS)
 
Datum network_family (PG_FUNCTION_ARGS)
 
Datum network_broadcast (PG_FUNCTION_ARGS)
 
Datum network_network (PG_FUNCTION_ARGS)
 
Datum network_netmask (PG_FUNCTION_ARGS)
 
Datum network_hostmask (PG_FUNCTION_ARGS)
 
Datum inet_same_family (PG_FUNCTION_ARGS)
 
Datum inet_merge (PG_FUNCTION_ARGS)
 
double convert_network_to_scalar (Datum value, Oid typid, bool *failure)
 
int bitncmp (const unsigned char *l, const unsigned char *r, int n)
 
int bitncommon (const unsigned char *l, const unsigned char *r, int n)
 
Datum network_scan_first (Datum in)
 
Datum network_scan_last (Datum in)
 
Datum inet_client_addr (PG_FUNCTION_ARGS)
 
Datum inet_client_port (PG_FUNCTION_ARGS)
 
Datum inet_server_addr (PG_FUNCTION_ARGS)
 
Datum inet_server_port (PG_FUNCTION_ARGS)
 
Datum inetnot (PG_FUNCTION_ARGS)
 
Datum inetand (PG_FUNCTION_ARGS)
 
Datum inetor (PG_FUNCTION_ARGS)
 
Datum inetpl (PG_FUNCTION_ARGS)
 
Datum inetmi_int8 (PG_FUNCTION_ARGS)
 
Datum inetmi (PG_FUNCTION_ARGS)
 
void clean_ipv6_addr (int addr_family, char *addr)
 

Macro Definition Documentation

◆ ABBREV_BITS_INET4_NETMASK_SIZE

#define ABBREV_BITS_INET4_NETMASK_SIZE   6

Definition at line 42 of file network.c.

◆ ABBREV_BITS_INET4_SUBNET

#define ABBREV_BITS_INET4_SUBNET   25

Definition at line 43 of file network.c.

Function Documentation

◆ addressOK()

static bool addressOK ( unsigned char *  a,
int  bits,
int  family 
)
static

Definition at line 1641 of file network.c.

1642 {
1643  int byte;
1644  int nbits;
1645  int maxbits;
1646  int maxbytes;
1647  unsigned char mask;
1648 
1649  if (family == PGSQL_AF_INET)
1650  {
1651  maxbits = 32;
1652  maxbytes = 4;
1653  }
1654  else
1655  {
1656  maxbits = 128;
1657  maxbytes = 16;
1658  }
1659  Assert(bits <= maxbits);
1660 
1661  if (bits == maxbits)
1662  return true;
1663 
1664  byte = bits / 8;
1665 
1666  nbits = bits % 8;
1667  mask = 0xff;
1668  if (bits != 0)
1669  mask >>= nbits;
1670 
1671  while (byte < maxbytes)
1672  {
1673  if ((a[byte] & mask) != 0)
1674  return false;
1675  mask = 0xff;
1676  byte++;
1677  }
1678 
1679  return true;
1680 }
int a
Definition: isn.c:69
Assert(fmt[strlen(fmt) - 1] !='\n')
#define PGSQL_AF_INET
Definition: inet.h:39

References a, Assert(), and PGSQL_AF_INET.

Referenced by network_in(), and network_recv().

◆ bitncmp()

int bitncmp ( const unsigned char *  l,
const unsigned char *  r,
int  n 
)

Definition at line 1569 of file network.c.

1570 {
1571  unsigned int lb,
1572  rb;
1573  int x,
1574  b;
1575 
1576  b = n / 8;
1577  x = memcmp(l, r, b);
1578  if (x || (n % 8) == 0)
1579  return x;
1580 
1581  lb = l[b];
1582  rb = r[b];
1583  for (b = n % 8; b > 0; b--)
1584  {
1585  if (IS_HIGHBIT_SET(lb) != IS_HIGHBIT_SET(rb))
1586  {
1587  if (IS_HIGHBIT_SET(lb))
1588  return 1;
1589  return -1;
1590  }
1591  lb <<= 1;
1592  rb <<= 1;
1593  }
1594  return 0;
1595 }
#define IS_HIGHBIT_SET(ch)
Definition: c.h:1164
int b
Definition: isn.c:70
int x
Definition: isn.c:71

References b, IS_HIGHBIT_SET, and x.

Referenced by inet_gist_consistent(), inet_inclusion_cmp(), inet_spg_choose(), inet_spg_consistent_bitmap(), network_cmp_internal(), network_overlap(), network_sub(), network_subeq(), network_sup(), and network_supeq().

◆ bitncommon()

int bitncommon ( const unsigned char *  l,
const unsigned char *  r,
int  n 
)

Definition at line 1603 of file network.c.

1604 {
1605  int byte,
1606  nbits;
1607 
1608  /* number of bits to examine in last byte */
1609  nbits = n % 8;
1610 
1611  /* check whole bytes */
1612  for (byte = 0; byte < n / 8; byte++)
1613  {
1614  if (l[byte] != r[byte])
1615  {
1616  /* at least one bit in the last byte is not common */
1617  nbits = 7;
1618  break;
1619  }
1620  }
1621 
1622  /* check bits in last partial byte */
1623  if (nbits != 0)
1624  {
1625  /* calculate diff of first non-matching bytes */
1626  unsigned int diff = l[byte] ^ r[byte];
1627 
1628  /* compare the bits from the most to the least */
1629  while ((diff >> (8 - nbits)) != 0)
1630  nbits--;
1631  }
1632 
1633  return (8 * byte) + nbits;
1634 }

Referenced by calc_inet_union_params(), calc_inet_union_params_indexed(), inet_gist_penalty(), inet_hist_match_divider(), inet_merge(), inet_spg_choose(), and inet_spg_picksplit().

◆ cidr_abbrev()

Datum cidr_abbrev ( PG_FUNCTION_ARGS  )

Definition at line 1240 of file network.c.

1241 {
1242  inet *ip = PG_GETARG_INET_PP(0);
1243  char *dst;
1244  char tmp[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255/128")];
1245 
1246  dst = pg_inet_cidr_ntop(ip_family(ip), ip_addr(ip),
1247  ip_bits(ip), tmp, sizeof(tmp));
1248 
1249  if (dst == NULL)
1250  ereport(ERROR,
1251  (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
1252  errmsg("could not format cidr value: %m")));
1253 
1255 }
int errcode(int sqlerrcode)
Definition: elog.c:693
int errmsg(const char *fmt,...)
Definition: elog.c:904
#define ERROR
Definition: elog.h:33
#define ereport(elevel,...)
Definition: elog.h:143
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
char * pg_inet_cidr_ntop(int af, const void *src, int bits, char *dst, size_t size)
Definition: inet.h:53
#define ip_addr(inetptr)
Definition: inet.h:77
#define PG_GETARG_INET_PP(n)
Definition: inet.h:124
#define ip_family(inetptr)
Definition: inet.h:71
#define ip_bits(inetptr)
Definition: inet.h:74
text * cstring_to_text(const char *s)
Definition: varlena.c:188

References cstring_to_text(), ereport, errcode(), errmsg(), ERROR, ip_addr, ip_bits, ip_family, PG_GETARG_INET_PP, pg_inet_cidr_ntop(), and PG_RETURN_TEXT_P.

◆ cidr_in()

Datum cidr_in ( PG_FUNCTION_ARGS  )

Definition at line 129 of file network.c.

130 {
131  char *src = PG_GETARG_CSTRING(0);
132 
133  PG_RETURN_INET_P(network_in(src, true));
134 }
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:277
static inet * network_in(char *src, bool is_cidr)
Definition: network.c:75
#define PG_RETURN_INET_P(x)
Definition: inet.h:125

References network_in(), PG_GETARG_CSTRING, and PG_RETURN_INET_P.

◆ cidr_out()

Datum cidr_out ( PG_FUNCTION_ARGS  )

Definition at line 173 of file network.c.

174 {
175  inet *src = PG_GETARG_INET_PP(0);
176 
177  PG_RETURN_CSTRING(network_out(src, true));
178 }
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:362
static char * network_out(inet *src, bool is_cidr)
Definition: network.c:141

References network_out(), PG_GETARG_INET_PP, and PG_RETURN_CSTRING.

◆ cidr_recv()

Datum cidr_recv ( PG_FUNCTION_ARGS  )

Definition at line 258 of file network.c.

259 {
261 
263 }
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
static inet * network_recv(StringInfo buf, bool is_cidr)
Definition: network.c:192
static char * buf
Definition: pg_test_fsync.c:67
StringInfoData * StringInfo
Definition: stringinfo.h:44

References buf, network_recv(), PG_GETARG_POINTER, and PG_RETURN_INET_P.

◆ cidr_send()

Datum cidr_send ( PG_FUNCTION_ARGS  )

Definition at line 300 of file network.c.

301 {
302  inet *addr = PG_GETARG_INET_PP(0);
303 
304  PG_RETURN_BYTEA_P(network_send(addr, true));
305 }
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:371
static bytea * network_send(inet *addr, bool is_cidr)
Definition: network.c:270

References network_send(), PG_GETARG_INET_PP, and PG_RETURN_BYTEA_P.

◆ cidr_set_masklen()

Datum cidr_set_masklen ( PG_FUNCTION_ARGS  )

Definition at line 348 of file network.c.

349 {
350  inet *src = PG_GETARG_INET_PP(0);
351  int bits = PG_GETARG_INT32(1);
352 
353  if (bits == -1)
354  bits = ip_maxbits(src);
355 
356  if ((bits < 0) || (bits > ip_maxbits(src)))
357  ereport(ERROR,
358  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
359  errmsg("invalid mask length: %d", bits)));
360 
362 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
inet * cidr_set_masklen_internal(const inet *src, int bits)
Definition: network.c:368
#define ip_maxbits(inetptr)
Definition: inet.h:83

References cidr_set_masklen_internal(), ereport, errcode(), errmsg(), ERROR, ip_maxbits, PG_GETARG_INET_PP, PG_GETARG_INT32, and PG_RETURN_INET_P.

◆ cidr_set_masklen_internal()

inet* cidr_set_masklen_internal ( const inet src,
int  bits 
)

Definition at line 368 of file network.c.

369 {
370  inet *dst = (inet *) palloc0(sizeof(inet));
371 
372  ip_family(dst) = ip_family(src);
373  ip_bits(dst) = bits;
374 
375  if (bits > 0)
376  {
377  Assert(bits <= ip_maxbits(dst));
378 
379  /* Clone appropriate bytes of the address, leaving the rest 0 */
380  memcpy(ip_addr(dst), ip_addr(src), (bits + 7) / 8);
381 
382  /* Clear any unwanted bits in the last partial byte */
383  if (bits % 8)
384  ip_addr(dst)[bits / 8] &= ~(0xFF >> (bits % 8));
385  }
386 
387  /* Set varlena header correctly */
388  SET_INET_VARSIZE(dst);
389 
390  return dst;
391 }
void * palloc0(Size size)
Definition: mcxt.c:1099
#define SET_INET_VARSIZE(dst)
Definition: inet.h:86

References Assert(), ip_addr, ip_bits, ip_family, ip_maxbits, palloc0(), and SET_INET_VARSIZE.

Referenced by cidr_set_masklen(), inet_merge(), inet_spg_choose(), inet_spg_picksplit(), and inet_to_cidr().

◆ clean_ipv6_addr()

void clean_ipv6_addr ( int  addr_family,
char *  addr 
)

Definition at line 2103 of file network.c.

2104 {
2105 #ifdef HAVE_IPV6
2106  if (addr_family == AF_INET6)
2107  {
2108  char *pct = strchr(addr, '%');
2109 
2110  if (pct)
2111  *pct = '\0';
2112  }
2113 #endif
2114 }

Referenced by fill_hba_line(), inet_client_addr(), inet_server_addr(), pg_stat_get_activity(), and pg_stat_get_backend_client_addr().

◆ convert_network_to_scalar()

double convert_network_to_scalar ( Datum  value,
Oid  typid,
bool failure 
)

Definition at line 1502 of file network.c.

1503 {
1504  switch (typid)
1505  {
1506  case INETOID:
1507  case CIDROID:
1508  {
1509  inet *ip = DatumGetInetPP(value);
1510  int len;
1511  double res;
1512  int i;
1513 
1514  /*
1515  * Note that we don't use the full address for IPv6.
1516  */
1517  if (ip_family(ip) == PGSQL_AF_INET)
1518  len = 4;
1519  else
1520  len = 5;
1521 
1522  res = ip_family(ip);
1523  for (i = 0; i < len; i++)
1524  {
1525  res *= 256;
1526  res += ip_addr(ip)[i];
1527  }
1528  return res;
1529  }
1530  case MACADDROID:
1531  {
1532  macaddr *mac = DatumGetMacaddrP(value);
1533  double res;
1534 
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);
1538  return res;
1539  }
1540  case MACADDR8OID:
1541  {
1543  double res;
1544 
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);
1548  return res;
1549  }
1550  }
1551 
1552  *failure = true;
1553  return 0;
1554 }
static struct @151 value
int i
Definition: isn.c:73
const void size_t len
Definition: inet.h:108
unsigned char c
Definition: inet.h:111
unsigned char b
Definition: inet.h:110
unsigned char d
Definition: inet.h:112
unsigned char e
Definition: inet.h:113
unsigned char g
Definition: inet.h:115
unsigned char h
Definition: inet.h:116
unsigned char a
Definition: inet.h:109
unsigned char f
Definition: inet.h:114
Definition: inet.h:95
unsigned char e
Definition: inet.h:100
unsigned char b
Definition: inet.h:97
unsigned char f
Definition: inet.h:101
unsigned char c
Definition: inet.h:98
unsigned char a
Definition: inet.h:96
unsigned char d
Definition: inet.h:99
#define DatumGetMacaddr8P(X)
Definition: inet.h:137
#define DatumGetInetPP(X)
Definition: inet.h:122
#define DatumGetMacaddrP(X)
Definition: inet.h:131

References macaddr::a, macaddr8::a, macaddr::b, macaddr8::b, macaddr::c, macaddr8::c, macaddr::d, macaddr8::d, DatumGetInetPP, DatumGetMacaddr8P, DatumGetMacaddrP, macaddr::e, macaddr8::e, macaddr::f, macaddr8::f, macaddr8::g, macaddr8::h, i, ip_addr, ip_family, len, PGSQL_AF_INET, res, and value.

Referenced by convert_to_scalar(), gbt_inet_compress(), and gbt_inet_consistent().

◆ hashinet()

Datum hashinet ( PG_FUNCTION_ARGS  )

Definition at line 880 of file network.c.

881 {
882  inet *addr = PG_GETARG_INET_PP(0);
883  int addrsize = ip_addrsize(addr);
884 
885  /* XXX this assumes there are no pad bytes in the data structure */
886  return hash_any((unsigned char *) VARDATA_ANY(addr), addrsize + 2);
887 }
static Datum hash_any(const unsigned char *k, int keylen)
Definition: hashfn.h:31
#define VARDATA_ANY(PTR)
Definition: postgres.h:361
#define ip_addrsize(inetptr)
Definition: inet.h:80

References hash_any(), ip_addrsize, PG_GETARG_INET_PP, and VARDATA_ANY.

◆ hashinetextended()

Datum hashinetextended ( PG_FUNCTION_ARGS  )

Definition at line 890 of file network.c.

891 {
892  inet *addr = PG_GETARG_INET_PP(0);
893  int addrsize = ip_addrsize(addr);
894 
895  return hash_any_extended((unsigned char *) VARDATA_ANY(addr), addrsize + 2,
896  PG_GETARG_INT64(1));
897 }
#define PG_GETARG_INT64(n)
Definition: fmgr.h:283
static Datum hash_any_extended(const unsigned char *k, int keylen, uint64 seed)
Definition: hashfn.h:37

References hash_any_extended(), ip_addrsize, PG_GETARG_INET_PP, PG_GETARG_INT64, and VARDATA_ANY.

◆ inet_abbrev()

Datum inet_abbrev ( PG_FUNCTION_ARGS  )

Definition at line 1222 of file network.c.

1223 {
1224  inet *ip = PG_GETARG_INET_PP(0);
1225  char *dst;
1226  char tmp[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255/128")];
1227 
1228  dst = pg_inet_net_ntop(ip_family(ip), ip_addr(ip),
1229  ip_bits(ip), tmp, sizeof(tmp));
1230 
1231  if (dst == NULL)
1232  ereport(ERROR,
1233  (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
1234  errmsg("could not format inet value: %m")));
1235 
1237 }
char * pg_inet_net_ntop(int af, const void *src, int bits, char *dst, size_t size)
Definition: inet_net_ntop.c:77

References cstring_to_text(), ereport, errcode(), errmsg(), ERROR, ip_addr, ip_bits, ip_family, PG_GETARG_INET_PP, pg_inet_net_ntop(), and PG_RETURN_TEXT_P.

◆ inet_client_addr()

Datum inet_client_addr ( PG_FUNCTION_ARGS  )

Definition at line 1716 of file network.c.

1717 {
1718  Port *port = MyProcPort;
1719  char remote_host[NI_MAXHOST];
1720  int ret;
1721 
1722  if (port == NULL)
1723  PG_RETURN_NULL();
1724 
1725  switch (port->raddr.addr.ss_family)
1726  {
1727  case AF_INET:
1728 #ifdef HAVE_IPV6
1729  case AF_INET6:
1730 #endif
1731  break;
1732  default:
1733  PG_RETURN_NULL();
1734  }
1735 
1736  remote_host[0] = '\0';
1737 
1738  ret = pg_getnameinfo_all(&port->raddr.addr, port->raddr.salen,
1739  remote_host, sizeof(remote_host),
1740  NULL, 0,
1742  if (ret != 0)
1743  PG_RETURN_NULL();
1744 
1745  clean_ipv6_addr(port->raddr.addr.ss_family, remote_host);
1746 
1747  PG_RETURN_INET_P(network_in(remote_host, false));
1748 }
#define PG_RETURN_NULL()
Definition: fmgr.h:345
#define NI_NUMERICHOST
Definition: getaddrinfo.h:78
#define NI_MAXHOST
Definition: getaddrinfo.h:88
#define NI_NUMERICSERV
Definition: getaddrinfo.h:81
struct Port * MyProcPort
Definition: globals.c:47
int pg_getnameinfo_all(const struct sockaddr_storage *addr, int salen, char *node, int nodelen, char *service, int servicelen, int flags)
Definition: ip.c:122
void clean_ipv6_addr(int addr_family, char *addr)
Definition: network.c:2103
static int port
Definition: pg_regress.c:92
Definition: libpq-be.h:125

References clean_ipv6_addr(), MyProcPort, network_in(), NI_MAXHOST, NI_NUMERICHOST, NI_NUMERICSERV, pg_getnameinfo_all(), PG_RETURN_INET_P, PG_RETURN_NULL, and port.

◆ inet_client_port()

Datum inet_client_port ( PG_FUNCTION_ARGS  )

Definition at line 1755 of file network.c.

1756 {
1757  Port *port = MyProcPort;
1758  char remote_port[NI_MAXSERV];
1759  int ret;
1760 
1761  if (port == NULL)
1762  PG_RETURN_NULL();
1763 
1764  switch (port->raddr.addr.ss_family)
1765  {
1766  case AF_INET:
1767 #ifdef HAVE_IPV6
1768  case AF_INET6:
1769 #endif
1770  break;
1771  default:
1772  PG_RETURN_NULL();
1773  }
1774 
1775  remote_port[0] = '\0';
1776 
1777  ret = pg_getnameinfo_all(&port->raddr.addr, port->raddr.salen,
1778  NULL, 0,
1779  remote_port, sizeof(remote_port),
1781  if (ret != 0)
1782  PG_RETURN_NULL();
1783 
1785 }
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:631
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:353
#define NI_MAXSERV
Definition: getaddrinfo.h:91
Datum int4in(PG_FUNCTION_ARGS)
Definition: int.c:290
#define CStringGetDatum(X)
Definition: postgres.h:622

References CStringGetDatum, DirectFunctionCall1, int4in(), MyProcPort, NI_MAXSERV, NI_NUMERICHOST, NI_NUMERICSERV, pg_getnameinfo_all(), PG_RETURN_DATUM, PG_RETURN_NULL, and port.

◆ inet_in()

Datum inet_in ( PG_FUNCTION_ARGS  )

Definition at line 121 of file network.c.

122 {
123  char *src = PG_GETARG_CSTRING(0);
124 
125  PG_RETURN_INET_P(network_in(src, false));
126 }

References network_in(), PG_GETARG_CSTRING, and PG_RETURN_INET_P.

Referenced by leftmostvalue_inet(), pg_stat_get_activity(), and pg_stat_get_backend_client_addr().

◆ inet_merge()

Datum inet_merge ( PG_FUNCTION_ARGS  )

Definition at line 1476 of file network.c.

1477 {
1478  inet *a1 = PG_GETARG_INET_PP(0),
1479  *a2 = PG_GETARG_INET_PP(1);
1480  int commonbits;
1481 
1482  if (ip_family(a1) != ip_family(a2))
1483  ereport(ERROR,
1484  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1485  errmsg("cannot merge addresses from different families")));
1486 
1487  commonbits = bitncommon(ip_addr(a1), ip_addr(a2),
1488  Min(ip_bits(a1), ip_bits(a2)));
1489 
1491 }
#define Min(x, y)
Definition: c.h:997
static const FormData_pg_attribute a1
Definition: heap.c:141
static const FormData_pg_attribute a2
Definition: heap.c:155
int bitncommon(const unsigned char *l, const unsigned char *r, int n)
Definition: network.c:1603

References a1, a2, bitncommon(), cidr_set_masklen_internal(), ereport, errcode(), errmsg(), ERROR, ip_addr, ip_bits, ip_family, Min, PG_GETARG_INET_PP, and PG_RETURN_INET_P.

◆ inet_out()

Datum inet_out ( PG_FUNCTION_ARGS  )

Definition at line 165 of file network.c.

166 {
167  inet *src = PG_GETARG_INET_PP(0);
168 
169  PG_RETURN_CSTRING(network_out(src, false));
170 }

References network_out(), PG_GETARG_INET_PP, and PG_RETURN_CSTRING.

◆ inet_recv()

Datum inet_recv ( PG_FUNCTION_ARGS  )

Definition at line 250 of file network.c.

251 {
253 
255 }

References buf, network_recv(), PG_GETARG_POINTER, and PG_RETURN_INET_P.

◆ inet_same_family()

Datum inet_same_family ( PG_FUNCTION_ARGS  )

Definition at line 1464 of file network.c.

1465 {
1466  inet *a1 = PG_GETARG_INET_PP(0);
1467  inet *a2 = PG_GETARG_INET_PP(1);
1468 
1470 }
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359

References a1, a2, ip_family, PG_GETARG_INET_PP, and PG_RETURN_BOOL.

◆ inet_send()

Datum inet_send ( PG_FUNCTION_ARGS  )

Definition at line 292 of file network.c.

293 {
294  inet *addr = PG_GETARG_INET_PP(0);
295 
296  PG_RETURN_BYTEA_P(network_send(addr, false));
297 }

References network_send(), PG_GETARG_INET_PP, and PG_RETURN_BYTEA_P.

◆ inet_server_addr()

Datum inet_server_addr ( PG_FUNCTION_ARGS  )

Definition at line 1792 of file network.c.

1793 {
1794  Port *port = MyProcPort;
1795  char local_host[NI_MAXHOST];
1796  int ret;
1797 
1798  if (port == NULL)
1799  PG_RETURN_NULL();
1800 
1801  switch (port->laddr.addr.ss_family)
1802  {
1803  case AF_INET:
1804 #ifdef HAVE_IPV6
1805  case AF_INET6:
1806 #endif
1807  break;
1808  default:
1809  PG_RETURN_NULL();
1810  }
1811 
1812  local_host[0] = '\0';
1813 
1814  ret = pg_getnameinfo_all(&port->laddr.addr, port->laddr.salen,
1815  local_host, sizeof(local_host),
1816  NULL, 0,
1818  if (ret != 0)
1819  PG_RETURN_NULL();
1820 
1821  clean_ipv6_addr(port->laddr.addr.ss_family, local_host);
1822 
1823  PG_RETURN_INET_P(network_in(local_host, false));
1824 }

References clean_ipv6_addr(), MyProcPort, network_in(), NI_MAXHOST, NI_NUMERICHOST, NI_NUMERICSERV, pg_getnameinfo_all(), PG_RETURN_INET_P, PG_RETURN_NULL, and port.

◆ inet_server_port()

Datum inet_server_port ( PG_FUNCTION_ARGS  )

Definition at line 1831 of file network.c.

1832 {
1833  Port *port = MyProcPort;
1834  char local_port[NI_MAXSERV];
1835  int ret;
1836 
1837  if (port == NULL)
1838  PG_RETURN_NULL();
1839 
1840  switch (port->laddr.addr.ss_family)
1841  {
1842  case AF_INET:
1843 #ifdef HAVE_IPV6
1844  case AF_INET6:
1845 #endif
1846  break;
1847  default:
1848  PG_RETURN_NULL();
1849  }
1850 
1851  local_port[0] = '\0';
1852 
1853  ret = pg_getnameinfo_all(&port->laddr.addr, port->laddr.salen,
1854  NULL, 0,
1855  local_port, sizeof(local_port),
1857  if (ret != 0)
1858  PG_RETURN_NULL();
1859 
1861 }

References CStringGetDatum, DirectFunctionCall1, int4in(), MyProcPort, NI_MAXSERV, NI_NUMERICHOST, NI_NUMERICSERV, pg_getnameinfo_all(), PG_RETURN_DATUM, PG_RETURN_NULL, and port.

◆ inet_set_masklen()

Datum inet_set_masklen ( PG_FUNCTION_ARGS  )

Definition at line 324 of file network.c.

325 {
326  inet *src = PG_GETARG_INET_PP(0);
327  int bits = PG_GETARG_INT32(1);
328  inet *dst;
329 
330  if (bits == -1)
331  bits = ip_maxbits(src);
332 
333  if ((bits < 0) || (bits > ip_maxbits(src)))
334  ereport(ERROR,
335  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
336  errmsg("invalid mask length: %d", bits)));
337 
338  /* clone the original data */
339  dst = (inet *) palloc(VARSIZE_ANY(src));
340  memcpy(dst, src, VARSIZE_ANY(src));
341 
342  ip_bits(dst) = bits;
343 
344  PG_RETURN_INET_P(dst);
345 }
void * palloc(Size size)
Definition: mcxt.c:1068
#define VARSIZE_ANY(PTR)
Definition: postgres.h:348

References ereport, errcode(), errmsg(), ERROR, ip_bits, ip_maxbits, palloc(), PG_GETARG_INET_PP, PG_GETARG_INT32, PG_RETURN_INET_P, and VARSIZE_ANY.

Referenced by network_scan_last().

◆ inet_to_cidr()

Datum inet_to_cidr ( PG_FUNCTION_ARGS  )

Definition at line 309 of file network.c.

310 {
311  inet *src = PG_GETARG_INET_PP(0);
312  int bits;
313 
314  bits = ip_bits(src);
315 
316  /* safety check */
317  if ((bits < 0) || (bits > ip_maxbits(src)))
318  elog(ERROR, "invalid inet bit length: %d", bits);
319 
321 }

References cidr_set_masklen_internal(), elog(), ERROR, ip_bits, ip_maxbits, PG_GETARG_INET_PP, and PG_RETURN_INET_P.

◆ inetand()

Datum inetand ( PG_FUNCTION_ARGS  )

Definition at line 1890 of file network.c.

1891 {
1892  inet *ip = PG_GETARG_INET_PP(0);
1893  inet *ip2 = PG_GETARG_INET_PP(1);
1894  inet *dst;
1895 
1896  dst = (inet *) palloc0(sizeof(inet));
1897 
1898  if (ip_family(ip) != ip_family(ip2))
1899  ereport(ERROR,
1900  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1901  errmsg("cannot AND inet values of different sizes")));
1902  else
1903  {
1904  int nb = ip_addrsize(ip);
1905  unsigned char *pip = ip_addr(ip);
1906  unsigned char *pip2 = ip_addr(ip2);
1907  unsigned char *pdst = ip_addr(dst);
1908 
1909  while (nb-- > 0)
1910  pdst[nb] = pip[nb] & pip2[nb];
1911  }
1912  ip_bits(dst) = Max(ip_bits(ip), ip_bits(ip2));
1913 
1914  ip_family(dst) = ip_family(ip);
1915  SET_INET_VARSIZE(dst);
1916 
1917  PG_RETURN_INET_P(dst);
1918 }
#define Max(x, y)
Definition: c.h:991

References ereport, errcode(), errmsg(), ERROR, ip_addr, ip_addrsize, ip_bits, ip_family, Max, palloc0(), PG_GETARG_INET_PP, PG_RETURN_INET_P, and SET_INET_VARSIZE.

◆ inetmi()

Datum inetmi ( PG_FUNCTION_ARGS  )

Definition at line 2026 of file network.c.

2027 {
2028  inet *ip = PG_GETARG_INET_PP(0);
2029  inet *ip2 = PG_GETARG_INET_PP(1);
2030  int64 res = 0;
2031 
2032  if (ip_family(ip) != ip_family(ip2))
2033  ereport(ERROR,
2034  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2035  errmsg("cannot subtract inet values of different sizes")));
2036  else
2037  {
2038  /*
2039  * We form the difference using the traditional complement, increment,
2040  * and add rule, with the increment part being handled by starting the
2041  * carry off at 1. If you don't think integer arithmetic is done in
2042  * two's complement, too bad.
2043  */
2044  int nb = ip_addrsize(ip);
2045  int byte = 0;
2046  unsigned char *pip = ip_addr(ip);
2047  unsigned char *pip2 = ip_addr(ip2);
2048  int carry = 1;
2049 
2050  while (nb-- > 0)
2051  {
2052  int lobyte;
2053 
2054  carry = pip[nb] + (~pip2[nb] & 0xFF) + carry;
2055  lobyte = carry & 0xFF;
2056  if (byte < sizeof(int64))
2057  {
2058  res |= ((int64) lobyte) << (byte * 8);
2059  }
2060  else
2061  {
2062  /*
2063  * Input wider than int64: check for overflow. All bytes to
2064  * the left of what will fit should be 0 or 0xFF, depending on
2065  * sign of the now-complete result.
2066  */
2067  if ((res < 0) ? (lobyte != 0xFF) : (lobyte != 0))
2068  ereport(ERROR,
2069  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
2070  errmsg("result is out of range")));
2071  }
2072  carry >>= 8;
2073  byte++;
2074  }
2075 
2076  /*
2077  * If input is narrower than int64, overflow is not possible, but we
2078  * have to do proper sign extension.
2079  */
2080  if (carry == 0 && byte < sizeof(int64))
2081  res |= ((uint64) (int64) -1) << (byte * 8);
2082  }
2083 
2085 }
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368

References ereport, errcode(), errmsg(), ERROR, ip_addr, ip_addrsize, ip_family, PG_GETARG_INET_PP, PG_RETURN_INT64, and res.

◆ inetmi_int8()

Datum inetmi_int8 ( PG_FUNCTION_ARGS  )

Definition at line 2016 of file network.c.

2017 {
2018  inet *ip = PG_GETARG_INET_PP(0);
2019  int64 addend = PG_GETARG_INT64(1);
2020 
2021  PG_RETURN_INET_P(internal_inetpl(ip, -addend));
2022 }
static inet * internal_inetpl(inet *ip, int64 addend)
Definition: network.c:1954

References internal_inetpl(), PG_GETARG_INET_PP, PG_GETARG_INT64, and PG_RETURN_INET_P.

◆ inetnot()

Datum inetnot ( PG_FUNCTION_ARGS  )

Definition at line 1865 of file network.c.

1866 {
1867  inet *ip = PG_GETARG_INET_PP(0);
1868  inet *dst;
1869 
1870  dst = (inet *) palloc0(sizeof(inet));
1871 
1872  {
1873  int nb = ip_addrsize(ip);
1874  unsigned char *pip = ip_addr(ip);
1875  unsigned char *pdst = ip_addr(dst);
1876 
1877  while (nb-- > 0)
1878  pdst[nb] = ~pip[nb];
1879  }
1880  ip_bits(dst) = ip_bits(ip);
1881 
1882  ip_family(dst) = ip_family(ip);
1883  SET_INET_VARSIZE(dst);
1884 
1885  PG_RETURN_INET_P(dst);
1886 }

References ip_addr, ip_addrsize, ip_bits, ip_family, palloc0(), PG_GETARG_INET_PP, PG_RETURN_INET_P, and SET_INET_VARSIZE.

◆ inetor()

Datum inetor ( PG_FUNCTION_ARGS  )

Definition at line 1922 of file network.c.

1923 {
1924  inet *ip = PG_GETARG_INET_PP(0);
1925  inet *ip2 = PG_GETARG_INET_PP(1);
1926  inet *dst;
1927 
1928  dst = (inet *) palloc0(sizeof(inet));
1929 
1930  if (ip_family(ip) != ip_family(ip2))
1931  ereport(ERROR,
1932  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1933  errmsg("cannot OR inet values of different sizes")));
1934  else
1935  {
1936  int nb = ip_addrsize(ip);
1937  unsigned char *pip = ip_addr(ip);
1938  unsigned char *pip2 = ip_addr(ip2);
1939  unsigned char *pdst = ip_addr(dst);
1940 
1941  while (nb-- > 0)
1942  pdst[nb] = pip[nb] | pip2[nb];
1943  }
1944  ip_bits(dst) = Max(ip_bits(ip), ip_bits(ip2));
1945 
1946  ip_family(dst) = ip_family(ip);
1947  SET_INET_VARSIZE(dst);
1948 
1949  PG_RETURN_INET_P(dst);
1950 }

References ereport, errcode(), errmsg(), ERROR, ip_addr, ip_addrsize, ip_bits, ip_family, Max, palloc0(), PG_GETARG_INET_PP, PG_RETURN_INET_P, and SET_INET_VARSIZE.

◆ inetpl()

Datum inetpl ( PG_FUNCTION_ARGS  )

Definition at line 2006 of file network.c.

2007 {
2008  inet *ip = PG_GETARG_INET_PP(0);
2009  int64 addend = PG_GETARG_INT64(1);
2010 
2011  PG_RETURN_INET_P(internal_inetpl(ip, addend));
2012 }

References internal_inetpl(), PG_GETARG_INET_PP, PG_GETARG_INT64, and PG_RETURN_INET_P.

◆ internal_inetpl()

static inet * internal_inetpl ( inet ip,
int64  addend 
)
static

Definition at line 1954 of file network.c.

1955 {
1956  inet *dst;
1957 
1958  dst = (inet *) palloc0(sizeof(inet));
1959 
1960  {
1961  int nb = ip_addrsize(ip);
1962  unsigned char *pip = ip_addr(ip);
1963  unsigned char *pdst = ip_addr(dst);
1964  int carry = 0;
1965 
1966  while (nb-- > 0)
1967  {
1968  carry = pip[nb] + (int) (addend & 0xFF) + carry;
1969  pdst[nb] = (unsigned char) (carry & 0xFF);
1970  carry >>= 8;
1971 
1972  /*
1973  * We have to be careful about right-shifting addend because
1974  * right-shift isn't portable for negative values, while simply
1975  * dividing by 256 doesn't work (the standard rounding is in the
1976  * wrong direction, besides which there may be machines out there
1977  * that round the wrong way). So, explicitly clear the low-order
1978  * byte to remove any doubt about the correct result of the
1979  * division, and then divide rather than shift.
1980  */
1981  addend &= ~((int64) 0xFF);
1982  addend /= 0x100;
1983  }
1984 
1985  /*
1986  * At this point we should have addend and carry both zero if original
1987  * addend was >= 0, or addend -1 and carry 1 if original addend was <
1988  * 0. Anything else means overflow.
1989  */
1990  if (!((addend == 0 && carry == 0) ||
1991  (addend == -1 && carry == 1)))
1992  ereport(ERROR,
1993  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1994  errmsg("result is out of range")));
1995  }
1996 
1997  ip_bits(dst) = ip_bits(ip);
1998  ip_family(dst) = ip_family(ip);
1999  SET_INET_VARSIZE(dst);
2000 
2001  return dst;
2002 }

References ereport, errcode(), errmsg(), ERROR, ip_addr, ip_addrsize, ip_bits, ip_family, palloc0(), and SET_INET_VARSIZE.

Referenced by inetmi_int8(), and inetpl().

◆ match_network_function()

static List * match_network_function ( Node leftop,
Node rightop,
int  indexarg,
Oid  funcid,
Oid  opfamily 
)
static

Definition at line 1028 of file network.c.

1033 {
1034  switch (funcid)
1035  {
1036  case F_NETWORK_SUB:
1037  /* indexkey must be on the left */
1038  if (indexarg != 0)
1039  return NIL;
1040  return match_network_subset(leftop, rightop, false, opfamily);
1041 
1042  case F_NETWORK_SUBEQ:
1043  /* indexkey must be on the left */
1044  if (indexarg != 0)
1045  return NIL;
1046  return match_network_subset(leftop, rightop, true, opfamily);
1047 
1048  case F_NETWORK_SUP:
1049  /* indexkey must be on the right */
1050  if (indexarg != 1)
1051  return NIL;
1052  return match_network_subset(rightop, leftop, false, opfamily);
1053 
1054  case F_NETWORK_SUPEQ:
1055  /* indexkey must be on the right */
1056  if (indexarg != 1)
1057  return NIL;
1058  return match_network_subset(rightop, leftop, true, opfamily);
1059 
1060  default:
1061 
1062  /*
1063  * We'd only get here if somebody attached this support function
1064  * to an unexpected function. Maybe we should complain, but for
1065  * now, do nothing.
1066  */
1067  return NIL;
1068  }
1069 }
static List * match_network_subset(Node *leftop, Node *rightop, bool is_eq, Oid opfamily)
Definition: network.c:1076
#define NIL
Definition: pg_list.h:66

References match_network_subset(), and NIL.

Referenced by network_subset_support().

◆ match_network_subset()

static List * match_network_subset ( Node leftop,
Node rightop,
bool  is_eq,
Oid  opfamily 
)
static

Definition at line 1076 of file network.c.

1080 {
1081  List *result;
1082  Datum rightopval;
1083  Oid datatype = INETOID;
1084  Oid opr1oid;
1085  Oid opr2oid;
1086  Datum opr1right;
1087  Datum opr2right;
1088  Expr *expr;
1089 
1090  /*
1091  * Can't do anything with a non-constant or NULL comparison value.
1092  *
1093  * Note that since we restrict ourselves to cases with a hard constant on
1094  * the RHS, it's a-fortiori a pseudoconstant, and we don't need to worry
1095  * about verifying that.
1096  */
1097  if (!IsA(rightop, Const) ||
1098  ((Const *) rightop)->constisnull)
1099  return NIL;
1100  rightopval = ((Const *) rightop)->constvalue;
1101 
1102  /*
1103  * Must check that index's opfamily supports the operators we will want to
1104  * apply.
1105  *
1106  * We insist on the opfamily being the specific one we expect, else we'd
1107  * do the wrong thing if someone were to make a reverse-sort opfamily with
1108  * the same operators.
1109  */
1110  if (opfamily != NETWORK_BTREE_FAM_OID)
1111  return NIL;
1112 
1113  /*
1114  * create clause "key >= network_scan_first( rightopval )", or ">" if the
1115  * operator disallows equality.
1116  *
1117  * Note: seeing that this function supports only fixed values for opfamily
1118  * and datatype, we could just hard-wire the operator OIDs instead of
1119  * looking them up. But for now it seems better to be general.
1120  */
1121  if (is_eq)
1122  {
1123  opr1oid = get_opfamily_member(opfamily, datatype, datatype,
1125  if (opr1oid == InvalidOid)
1126  elog(ERROR, "no >= operator for opfamily %u", opfamily);
1127  }
1128  else
1129  {
1130  opr1oid = get_opfamily_member(opfamily, datatype, datatype,
1132  if (opr1oid == InvalidOid)
1133  elog(ERROR, "no > operator for opfamily %u", opfamily);
1134  }
1135 
1136  opr1right = network_scan_first(rightopval);
1137 
1138  expr = make_opclause(opr1oid, BOOLOID, false,
1139  (Expr *) leftop,
1140  (Expr *) makeConst(datatype, -1,
1141  InvalidOid, /* not collatable */
1142  -1, opr1right,
1143  false, false),
1145  result = list_make1(expr);
1146 
1147  /* create clause "key <= network_scan_last( rightopval )" */
1148 
1149  opr2oid = get_opfamily_member(opfamily, datatype, datatype,
1151  if (opr2oid == InvalidOid)
1152  elog(ERROR, "no <= operator for opfamily %u", opfamily);
1153 
1154  opr2right = network_scan_last(rightopval);
1155 
1156  expr = make_opclause(opr2oid, BOOLOID, false,
1157  (Expr *) leftop,
1158  (Expr *) makeConst(datatype, -1,
1159  InvalidOid, /* not collatable */
1160  -1, opr2right,
1161  false, false),
1163  result = lappend(result, expr);
1164 
1165  return result;
1166 }
List * lappend(List *list, void *datum)
Definition: list.c:338
Oid get_opfamily_member(Oid opfamily, Oid lefttype, Oid righttype, int16 strategy)
Definition: lsyscache.c:164
Expr * make_opclause(Oid opno, Oid opresulttype, bool opretset, Expr *leftop, Expr *rightop, Oid opcollid, Oid inputcollid)
Definition: makefuncs.c:611
Const * makeConst(Oid consttype, int32 consttypmod, Oid constcollid, int constlen, Datum constvalue, bool constisnull, bool constbyval)
Definition: makefuncs.c:300
Datum network_scan_last(Datum in)
Definition: network.c:1704
Datum network_scan_first(Datum in)
Definition: network.c:1690
#define IsA(nodeptr, _type_)
Definition: nodes.h:625
#define list_make1(x1)
Definition: pg_list.h:210
uintptr_t Datum
Definition: postgres.h:411
#define InvalidOid
Definition: postgres_ext.h:36
unsigned int Oid
Definition: postgres_ext.h:31
#define BTGreaterStrategyNumber
Definition: stratnum.h:33
#define BTLessEqualStrategyNumber
Definition: stratnum.h:30
#define BTGreaterEqualStrategyNumber
Definition: stratnum.h:32
Definition: pg_list.h:52

References BTGreaterEqualStrategyNumber, BTGreaterStrategyNumber, BTLessEqualStrategyNumber, elog(), ERROR, get_opfamily_member(), InvalidOid, IsA, lappend(), list_make1, make_opclause(), makeConst(), network_scan_first(), network_scan_last(), and NIL.

Referenced by match_network_function().

◆ network_abbrev_abort()

static bool network_abbrev_abort ( int  memtupcount,
SortSupport  ssup 
)
static

Definition at line 488 of file network.c.

489 {
491  double abbr_card;
492 
493  if (memtupcount < 10000 || uss->input_count < 10000 || !uss->estimating)
494  return false;
495 
496  abbr_card = estimateHyperLogLog(&uss->abbr_card);
497 
498  /*
499  * If we have >100k distinct values, then even if we were sorting many
500  * billion rows we'd likely still break even, and the penalty of undoing
501  * that many rows of abbrevs would probably not be worth it. At this point
502  * we stop counting because we know that we're now fully committed.
503  */
504  if (abbr_card > 100000.0)
505  {
506 #ifdef TRACE_SORT
507  if (trace_sort)
508  elog(LOG,
509  "network_abbrev: estimation ends at cardinality %f"
510  " after " INT64_FORMAT " values (%d rows)",
511  abbr_card, uss->input_count, memtupcount);
512 #endif
513  uss->estimating = false;
514  return false;
515  }
516 
517  /*
518  * Target minimum cardinality is 1 per ~2k of non-null inputs. 0.5 row
519  * fudge factor allows us to abort earlier on genuinely pathological data
520  * where we've had exactly one abbreviated value in the first 2k
521  * (non-null) rows.
522  */
523  if (abbr_card < uss->input_count / 2000.0 + 0.5)
524  {
525 #ifdef TRACE_SORT
526  if (trace_sort)
527  elog(LOG,
528  "network_abbrev: aborting abbreviation at cardinality %f"
529  " below threshold %f after " INT64_FORMAT " values (%d rows)",
530  abbr_card, uss->input_count / 2000.0 + 0.5, uss->input_count,
531  memtupcount);
532 #endif
533  return true;
534  }
535 
536 #ifdef TRACE_SORT
537  if (trace_sort)
538  elog(LOG,
539  "network_abbrev: cardinality %f after " INT64_FORMAT
540  " values (%d rows)", abbr_card, uss->input_count, memtupcount);
541 #endif
542 
543  return false;
544 }
#define INT64_FORMAT
Definition: c.h:494
#define LOG
Definition: elog.h:25
double estimateHyperLogLog(hyperLogLogState *cState)
Definition: hyperloglog.c:186
void * ssup_extra
Definition: sortsupport.h:87
hyperLogLogState abbr_card
Definition: network.c:51
bool trace_sort
Definition: tuplesort.c:144

References network_sortsupport_state::abbr_card, elog(), estimateHyperLogLog(), network_sortsupport_state::estimating, network_sortsupport_state::input_count, INT64_FORMAT, LOG, SortSupportData::ssup_extra, and trace_sort.

Referenced by network_sortsupport().

◆ network_abbrev_convert()

static Datum network_abbrev_convert ( Datum  original,
SortSupport  ssup 
)
static

Definition at line 625 of file network.c.

626 {
628  inet *authoritative = DatumGetInetPP(original);
629  Datum res,
630  ipaddr_datum,
631  subnet_bitmask,
632  network;
633  int subnet_size;
634 
635  Assert(ip_family(authoritative) == PGSQL_AF_INET ||
636  ip_family(authoritative) == PGSQL_AF_INET6);
637 
638  /*
639  * Get an unsigned integer representation of the IP address by taking its
640  * first 4 or 8 bytes. Always take all 4 bytes of an IPv4 address. Take
641  * the first 8 bytes of an IPv6 address with an 8 byte datum and 4 bytes
642  * otherwise.
643  *
644  * We're consuming an array of unsigned char, so byteswap on little endian
645  * systems (an inet's ipaddr field stores the most significant byte
646  * first).
647  */
648  if (ip_family(authoritative) == PGSQL_AF_INET)
649  {
650  uint32 ipaddr_datum32;
651 
652  memcpy(&ipaddr_datum32, ip_addr(authoritative), sizeof(uint32));
653 
654  /* Must byteswap on little-endian machines */
655 #ifndef WORDS_BIGENDIAN
656  ipaddr_datum = pg_bswap32(ipaddr_datum32);
657 #else
658  ipaddr_datum = ipaddr_datum32;
659 #endif
660 
661  /* Initialize result without setting ipfamily bit */
662  res = (Datum) 0;
663  }
664  else
665  {
666  memcpy(&ipaddr_datum, ip_addr(authoritative), sizeof(Datum));
667 
668  /* Must byteswap on little-endian machines */
669  ipaddr_datum = DatumBigEndianToNative(ipaddr_datum);
670 
671  /* Initialize result with ipfamily (most significant) bit set */
672  res = ((Datum) 1) << (SIZEOF_DATUM * BITS_PER_BYTE - 1);
673  }
674 
675  /*
676  * ipaddr_datum must be "split": high order bits go in "network" component
677  * of abbreviated key (often with zeroed bits at the end due to masking),
678  * while low order bits go in "subnet" component when there is space for
679  * one. This is often accomplished by generating a temp datum subnet
680  * bitmask, which we may reuse later when generating the subnet bits
681  * themselves. (Note that subnet bits are only used with IPv4 datums on
682  * platforms where datum is 8 bytes.)
683  *
684  * The number of bits in subnet is used to generate a datum subnet
685  * bitmask. For example, with a /24 IPv4 datum there are 8 subnet bits
686  * (since 32 - 24 is 8), so the final subnet bitmask is B'1111 1111'. We
687  * need explicit handling for cases where the ipaddr bits cannot all fit
688  * in a datum, though (otherwise we'd incorrectly mask the network
689  * component with IPv6 values).
690  */
691  subnet_size = ip_maxbits(authoritative) - ip_bits(authoritative);
692  Assert(subnet_size >= 0);
693  /* subnet size must work with prefix ipaddr cases */
694  subnet_size %= SIZEOF_DATUM * BITS_PER_BYTE;
695  if (ip_bits(authoritative) == 0)
696  {
697  /* Fit as many ipaddr bits as possible into subnet */
698  subnet_bitmask = ((Datum) 0) - 1;
699  network = 0;
700  }
701  else if (ip_bits(authoritative) < SIZEOF_DATUM * BITS_PER_BYTE)
702  {
703  /* Split ipaddr bits between network and subnet */
704  subnet_bitmask = (((Datum) 1) << subnet_size) - 1;
705  network = ipaddr_datum & ~subnet_bitmask;
706  }
707  else
708  {
709  /* Fit as many ipaddr bits as possible into network */
710  subnet_bitmask = 0;
711  network = ipaddr_datum;
712  }
713 
714 #if SIZEOF_DATUM == 8
715  if (ip_family(authoritative) == PGSQL_AF_INET)
716  {
717  /*
718  * IPv4 with 8 byte datums: keep all 32 netmasked bits, netmask size,
719  * and most significant 25 subnet bits
720  */
721  Datum netmask_size = (Datum) ip_bits(authoritative);
722  Datum subnet;
723 
724  /*
725  * Shift left 31 bits: 6 bits netmask size + 25 subnet bits.
726  *
727  * We don't make any distinction between network bits that are zero
728  * due to masking and "true"/non-masked zero bits. An abbreviated
729  * comparison that is resolved by comparing a non-masked and non-zero
730  * bit to a masked/zeroed bit is effectively resolved based on
731  * ip_bits(), even though the comparison won't reach the netmask_size
732  * bits.
733  */
734  network <<= (ABBREV_BITS_INET4_NETMASK_SIZE +
736 
737  /* Shift size to make room for subnet bits at the end */
738  netmask_size <<= ABBREV_BITS_INET4_SUBNET;
739 
740  /* Extract subnet bits without shifting them */
741  subnet = ipaddr_datum & subnet_bitmask;
742 
743  /*
744  * If we have more than 25 subnet bits, we can't fit everything. Shift
745  * subnet down to avoid clobbering bits that are only supposed to be
746  * used for netmask_size.
747  *
748  * Discarding the least significant subnet bits like this is correct
749  * because abbreviated comparisons that are resolved at the subnet
750  * level must have had equal netmask_size/ip_bits() values in order to
751  * get that far.
752  */
753  if (subnet_size > ABBREV_BITS_INET4_SUBNET)
754  subnet >>= subnet_size - ABBREV_BITS_INET4_SUBNET;
755 
756  /*
757  * Assemble the final abbreviated key without clobbering the ipfamily
758  * bit that must remain a zero.
759  */
760  res |= network | netmask_size | subnet;
761  }
762  else
763 #endif
764  {
765  /*
766  * 4 byte datums, or IPv6 with 8 byte datums: Use as many of the
767  * netmasked bits as will fit in final abbreviated key. Avoid
768  * clobbering the ipfamily bit that was set earlier.
769  */
770  res |= network >> 1;
771  }
772 
773  uss->input_count += 1;
774 
775  /* Hash abbreviated key */
776  if (uss->estimating)
777  {
778  uint32 tmp;
779 
780 #if SIZEOF_DATUM == 8
781  tmp = (uint32) res ^ (uint32) ((uint64) res >> 32);
782 #else /* SIZEOF_DATUM != 8 */
783  tmp = (uint32) res;
784 #endif
785 
787  }
788 
789  return res;
790 }
unsigned int uint32
Definition: c.h:452
static Datum hash_uint32(uint32 k)
Definition: hashfn.h:43
void addHyperLogLog(hyperLogLogState *cState, uint32 hash)
Definition: hyperloglog.c:167
#define ABBREV_BITS_INET4_SUBNET
Definition: network.c:43
#define ABBREV_BITS_INET4_NETMASK_SIZE
Definition: network.c:42
static uint32 pg_bswap32(uint32 x)
Definition: pg_bswap.h:64
#define BITS_PER_BYTE
#define SIZEOF_DATUM
Definition: postgres.h:428
#define DatumGetUInt32(X)
Definition: postgres.h:530
#define PGSQL_AF_INET6
Definition: inet.h:40

References network_sortsupport_state::abbr_card, ABBREV_BITS_INET4_NETMASK_SIZE, ABBREV_BITS_INET4_SUBNET, addHyperLogLog(), Assert(), BITS_PER_BYTE, DatumGetInetPP, DatumGetUInt32, network_sortsupport_state::estimating, hash_uint32(), network_sortsupport_state::input_count, ip_addr, ip_bits, ip_family, ip_maxbits, pg_bswap32(), PGSQL_AF_INET, PGSQL_AF_INET6, res, SIZEOF_DATUM, and SortSupportData::ssup_extra.

Referenced by network_sortsupport().

◆ network_broadcast()

Datum network_broadcast ( PG_FUNCTION_ARGS  )

Definition at line 1285 of file network.c.

1286 {
1287  inet *ip = PG_GETARG_INET_PP(0);
1288  inet *dst;
1289  int byte;
1290  int bits;
1291  int maxbytes;
1292  unsigned char mask;
1293  unsigned char *a,
1294  *b;
1295 
1296  /* make sure any unused bits are zeroed */
1297  dst = (inet *) palloc0(sizeof(inet));
1298 
1299  maxbytes = ip_addrsize(ip);
1300  bits = ip_bits(ip);
1301  a = ip_addr(ip);
1302  b = ip_addr(dst);
1303 
1304  for (byte = 0; byte < maxbytes; byte++)
1305  {
1306  if (bits >= 8)
1307  {
1308  mask = 0x00;
1309  bits -= 8;
1310  }
1311  else if (bits == 0)
1312  mask = 0xff;
1313  else
1314  {
1315  mask = 0xff >> bits;
1316  bits = 0;
1317  }
1318 
1319  b[byte] = a[byte] | mask;
1320  }
1321 
1322  ip_family(dst) = ip_family(ip);
1323  ip_bits(dst) = ip_bits(ip);
1324  SET_INET_VARSIZE(dst);
1325 
1326  PG_RETURN_INET_P(dst);
1327 }

References a, b, ip_addr, ip_addrsize, ip_bits, ip_family, palloc0(), PG_GETARG_INET_PP, PG_RETURN_INET_P, and SET_INET_VARSIZE.

Referenced by network_scan_last().

◆ network_cmp()

Datum network_cmp ( PG_FUNCTION_ARGS  )

Definition at line 425 of file network.c.

426 {
427  inet *a1 = PG_GETARG_INET_PP(0);
428  inet *a2 = PG_GETARG_INET_PP(1);
429 
431 }
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354
static int32 network_cmp_internal(inet *a1, inet *a2)
Definition: network.c:405

References a1, a2, network_cmp_internal(), PG_GETARG_INET_PP, and PG_RETURN_INT32.

◆ network_cmp_internal()

static int32 network_cmp_internal ( inet a1,
inet a2 
)
static

Definition at line 405 of file network.c.

406 {
407  if (ip_family(a1) == ip_family(a2))
408  {
409  int order;
410 
411  order = bitncmp(ip_addr(a1), ip_addr(a2),
412  Min(ip_bits(a1), ip_bits(a2)));
413  if (order != 0)
414  return order;
415  order = ((int) ip_bits(a1)) - ((int) ip_bits(a2));
416  if (order != 0)
417  return order;
418  return bitncmp(ip_addr(a1), ip_addr(a2), ip_maxbits(a1));
419  }
420 
421  return ip_family(a1) - ip_family(a2);
422 }
int bitncmp(const unsigned char *l, const unsigned char *r, int n)
Definition: network.c:1569

References a1, a2, bitncmp(), ip_addr, ip_bits, ip_family, ip_maxbits, and Min.

Referenced by network_cmp(), network_eq(), network_fast_cmp(), network_ge(), network_gt(), network_larger(), network_le(), network_lt(), network_ne(), and network_smaller().

◆ network_eq()

Datum network_eq ( PG_FUNCTION_ARGS  )

Definition at line 814 of file network.c.

815 {
816  inet *a1 = PG_GETARG_INET_PP(0);
817  inet *a2 = PG_GETARG_INET_PP(1);
818 
820 }

References a1, a2, network_cmp_internal(), PG_GETARG_INET_PP, and PG_RETURN_BOOL.

◆ network_family()

Datum network_family ( PG_FUNCTION_ARGS  )

Definition at line 1266 of file network.c.

1267 {
1268  inet *ip = PG_GETARG_INET_PP(0);
1269 
1270  switch (ip_family(ip))
1271  {
1272  case PGSQL_AF_INET:
1273  PG_RETURN_INT32(4);
1274  break;
1275  case PGSQL_AF_INET6:
1276  PG_RETURN_INT32(6);
1277  break;
1278  default:
1279  PG_RETURN_INT32(0);
1280  break;
1281  }
1282 }

References ip_family, PG_GETARG_INET_PP, PG_RETURN_INT32, PGSQL_AF_INET, and PGSQL_AF_INET6.

◆ network_fast_cmp()

static int network_fast_cmp ( Datum  x,
Datum  y,
SortSupport  ssup 
)
static

Definition at line 473 of file network.c.

474 {
475  inet *arg1 = DatumGetInetPP(x);
476  inet *arg2 = DatumGetInetPP(y);
477 
478  return network_cmp_internal(arg1, arg2);
479 }
int y
Definition: isn.c:72

References DatumGetInetPP, network_cmp_internal(), x, and y.

Referenced by network_sortsupport().

◆ network_ge()

Datum network_ge ( PG_FUNCTION_ARGS  )

Definition at line 823 of file network.c.

824 {
825  inet *a1 = PG_GETARG_INET_PP(0);
826  inet *a2 = PG_GETARG_INET_PP(1);
827 
829 }

References a1, a2, network_cmp_internal(), PG_GETARG_INET_PP, and PG_RETURN_BOOL.

◆ network_gt()

Datum network_gt ( PG_FUNCTION_ARGS  )

Definition at line 832 of file network.c.

833 {
834  inet *a1 = PG_GETARG_INET_PP(0);
835  inet *a2 = PG_GETARG_INET_PP(1);
836 
838 }

References a1, a2, network_cmp_internal(), PG_GETARG_INET_PP, and PG_RETURN_BOOL.

◆ network_host()

Datum network_host ( PG_FUNCTION_ARGS  )

Definition at line 1173 of file network.c.

1174 {
1175  inet *ip = PG_GETARG_INET_PP(0);
1176  char *ptr;
1177  char tmp[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255/128")];
1178 
1179  /* force display of max bits, regardless of masklen... */
1180  if (pg_inet_net_ntop(ip_family(ip), ip_addr(ip), ip_maxbits(ip),
1181  tmp, sizeof(tmp)) == NULL)
1182  ereport(ERROR,
1183  (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
1184  errmsg("could not format inet value: %m")));
1185 
1186  /* Suppress /n if present (shouldn't happen now) */
1187  if ((ptr = strchr(tmp, '/')) != NULL)
1188  *ptr = '\0';
1189 
1191 }

References cstring_to_text(), ereport, errcode(), errmsg(), ERROR, ip_addr, ip_family, ip_maxbits, PG_GETARG_INET_PP, pg_inet_net_ntop(), and PG_RETURN_TEXT_P.

◆ network_hostmask()

Datum network_hostmask ( PG_FUNCTION_ARGS  )

Definition at line 1416 of file network.c.

1417 {
1418  inet *ip = PG_GETARG_INET_PP(0);
1419  inet *dst;
1420  int byte;
1421  int bits;
1422  int maxbytes;
1423  unsigned char mask;
1424  unsigned char *b;
1425 
1426  /* make sure any unused bits are zeroed */
1427  dst = (inet *) palloc0(sizeof(inet));
1428 
1429  maxbytes = ip_addrsize(ip);
1430  bits = ip_maxbits(ip) - ip_bits(ip);
1431  b = ip_addr(dst);
1432 
1433  byte = maxbytes - 1;
1434 
1435  while (bits)
1436  {
1437  if (bits >= 8)
1438  {
1439  mask = 0xff;
1440  bits -= 8;
1441  }
1442  else
1443  {
1444  mask = 0xff >> (8 - bits);
1445  bits = 0;
1446  }
1447 
1448  b[byte] = mask;
1449  byte--;
1450  }
1451 
1452  ip_family(dst) = ip_family(ip);
1453  ip_bits(dst) = ip_maxbits(ip);
1454  SET_INET_VARSIZE(dst);
1455 
1456  PG_RETURN_INET_P(dst);
1457 }

References b, ip_addr, ip_addrsize, ip_bits, ip_family, ip_maxbits, palloc0(), PG_GETARG_INET_PP, PG_RETURN_INET_P, and SET_INET_VARSIZE.

◆ network_in()

static inet* network_in ( char *  src,
bool  is_cidr 
)
static

Definition at line 75 of file network.c.

76 {
77  int bits;
78  inet *dst;
79 
80  dst = (inet *) palloc0(sizeof(inet));
81 
82  /*
83  * First, check to see if this is an IPv6 or IPv4 address. IPv6 addresses
84  * will have a : somewhere in them (several, in fact) so if there is one
85  * present, assume it's V6, otherwise assume it's V4.
86  */
87 
88  if (strchr(src, ':') != NULL)
90  else
91  ip_family(dst) = PGSQL_AF_INET;
92 
93  bits = pg_inet_net_pton(ip_family(dst), src, ip_addr(dst),
94  is_cidr ? ip_addrsize(dst) : -1);
95  if ((bits < 0) || (bits > ip_maxbits(dst)))
96  ereport(ERROR,
97  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
98  /* translator: first %s is inet or cidr */
99  errmsg("invalid input syntax for type %s: \"%s\"",
100  is_cidr ? "cidr" : "inet", src)));
101 
102  /*
103  * Error check: CIDR values must not have any bits set beyond the masklen.
104  */
105  if (is_cidr)
106  {
107  if (!addressOK(ip_addr(dst), bits, ip_family(dst)))
108  ereport(ERROR,
109  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
110  errmsg("invalid cidr value: \"%s\"", src),
111  errdetail("Value has bits set to right of mask.")));
112  }
113 
114  ip_bits(dst) = bits;
115  SET_INET_VARSIZE(dst);
116 
117  return dst;
118 }
int errdetail(const char *fmt,...)
Definition: elog.c:1037
int pg_inet_net_pton(int af, const char *src, void *dst, size_t size)
Definition: inet_net_pton.c:62
static bool addressOK(unsigned char *a, int bits, int family)
Definition: network.c:1641

References addressOK(), ereport, errcode(), errdetail(), errmsg(), ERROR, ip_addr, ip_addrsize, ip_bits, ip_family, ip_maxbits, palloc0(), pg_inet_net_pton(), PGSQL_AF_INET, PGSQL_AF_INET6, and SET_INET_VARSIZE.

Referenced by cidr_in(), inet_client_addr(), inet_in(), and inet_server_addr().

◆ network_larger()

Datum network_larger ( PG_FUNCTION_ARGS  )

Definition at line 865 of file network.c.

866 {
867  inet *a1 = PG_GETARG_INET_PP(0);
868  inet *a2 = PG_GETARG_INET_PP(1);
869 
870  if (network_cmp_internal(a1, a2) > 0)
872  else
874 }

References a1, a2, network_cmp_internal(), PG_GETARG_INET_PP, and PG_RETURN_INET_P.

◆ network_le()

Datum network_le ( PG_FUNCTION_ARGS  )

Definition at line 805 of file network.c.

806 {
807  inet *a1 = PG_GETARG_INET_PP(0);
808  inet *a2 = PG_GETARG_INET_PP(1);
809 
811 }

References a1, a2, network_cmp_internal(), PG_GETARG_INET_PP, and PG_RETURN_BOOL.

◆ network_lt()

Datum network_lt ( PG_FUNCTION_ARGS  )

Definition at line 796 of file network.c.

797 {
798  inet *a1 = PG_GETARG_INET_PP(0);
799  inet *a2 = PG_GETARG_INET_PP(1);
800 
802 }

References a1, a2, network_cmp_internal(), PG_GETARG_INET_PP, and PG_RETURN_BOOL.

◆ network_masklen()

Datum network_masklen ( PG_FUNCTION_ARGS  )

Definition at line 1258 of file network.c.

1259 {
1260  inet *ip = PG_GETARG_INET_PP(0);
1261 
1262  PG_RETURN_INT32(ip_bits(ip));
1263 }

References ip_bits, PG_GETARG_INET_PP, and PG_RETURN_INT32.

◆ network_ne()

Datum network_ne ( PG_FUNCTION_ARGS  )

Definition at line 841 of file network.c.

842 {
843  inet *a1 = PG_GETARG_INET_PP(0);
844  inet *a2 = PG_GETARG_INET_PP(1);
845 
847 }

References a1, a2, network_cmp_internal(), PG_GETARG_INET_PP, and PG_RETURN_BOOL.

◆ network_netmask()

Datum network_netmask ( PG_FUNCTION_ARGS  )

Definition at line 1374 of file network.c.

1375 {
1376  inet *ip = PG_GETARG_INET_PP(0);
1377  inet *dst;
1378  int byte;
1379  int bits;
1380  unsigned char mask;
1381  unsigned char *b;
1382 
1383  /* make sure any unused bits are zeroed */
1384  dst = (inet *) palloc0(sizeof(inet));
1385 
1386  bits = ip_bits(ip);
1387  b = ip_addr(dst);
1388 
1389  byte = 0;
1390 
1391  while (bits)
1392  {
1393  if (bits >= 8)
1394  {
1395  mask = 0xff;
1396  bits -= 8;
1397  }
1398  else
1399  {
1400  mask = 0xff << (8 - bits);
1401  bits = 0;
1402  }
1403 
1404  b[byte] = mask;
1405  byte++;
1406  }
1407 
1408  ip_family(dst) = ip_family(ip);
1409  ip_bits(dst) = ip_maxbits(ip);
1410  SET_INET_VARSIZE(dst);
1411 
1412  PG_RETURN_INET_P(dst);
1413 }

References b, ip_addr, ip_bits, ip_family, ip_maxbits, palloc0(), PG_GETARG_INET_PP, PG_RETURN_INET_P, and SET_INET_VARSIZE.

◆ network_network()

Datum network_network ( PG_FUNCTION_ARGS  )

Definition at line 1330 of file network.c.

1331 {
1332  inet *ip = PG_GETARG_INET_PP(0);
1333  inet *dst;
1334  int byte;
1335  int bits;
1336  unsigned char mask;
1337  unsigned char *a,
1338  *b;
1339 
1340  /* make sure any unused bits are zeroed */
1341  dst = (inet *) palloc0(sizeof(inet));
1342 
1343  bits = ip_bits(ip);
1344  a = ip_addr(ip);
1345  b = ip_addr(dst);
1346 
1347  byte = 0;
1348 
1349  while (bits)
1350  {
1351  if (bits >= 8)
1352  {
1353  mask = 0xff;
1354  bits -= 8;
1355  }
1356  else
1357  {
1358  mask = 0xff << (8 - bits);
1359  bits = 0;
1360  }
1361 
1362  b[byte] = a[byte] & mask;
1363  byte++;
1364  }
1365 
1366  ip_family(dst) = ip_family(ip);
1367  ip_bits(dst) = ip_bits(ip);
1368  SET_INET_VARSIZE(dst);
1369 
1370  PG_RETURN_INET_P(dst);
1371 }

References a, b, ip_addr, ip_bits, ip_family, palloc0(), PG_GETARG_INET_PP, PG_RETURN_INET_P, and SET_INET_VARSIZE.

Referenced by network_scan_first().

◆ network_out()

static char* network_out ( inet src,
bool  is_cidr 
)
static

Definition at line 141 of file network.c.

142 {
143  char tmp[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255/128")];
144  char *dst;
145  int len;
146 
147  dst = pg_inet_net_ntop(ip_family(src), ip_addr(src), ip_bits(src),
148  tmp, sizeof(tmp));
149  if (dst == NULL)
150  ereport(ERROR,
151  (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
152  errmsg("could not format inet value: %m")));
153 
154  /* For CIDR, add /n if not present */
155  if (is_cidr && strchr(tmp, '/') == NULL)
156  {
157  len = strlen(tmp);
158  snprintf(tmp + len, sizeof(tmp) - len, "/%u", ip_bits(src));
159  }
160 
161  return pstrdup(tmp);
162 }
char * pstrdup(const char *in)
Definition: mcxt.c:1305
#define snprintf
Definition: port.h:225

References ereport, errcode(), errmsg(), ERROR, ip_addr, ip_bits, ip_family, len, pg_inet_net_ntop(), pstrdup(), and snprintf.

Referenced by cidr_out(), and inet_out().

◆ network_overlap()

Datum network_overlap ( PG_FUNCTION_ARGS  )

Definition at line 963 of file network.c.

964 {
965  inet *a1 = PG_GETARG_INET_PP(0);
966  inet *a2 = PG_GETARG_INET_PP(1);
967 
968  if (ip_family(a1) == ip_family(a2))
969  {
971  Min(ip_bits(a1), ip_bits(a2))) == 0);
972  }
973 
974  PG_RETURN_BOOL(false);
975 }

References a1, a2, bitncmp(), ip_addr, ip_bits, ip_family, Min, PG_GETARG_INET_PP, and PG_RETURN_BOOL.

◆ network_recv()

static inet* network_recv ( StringInfo  buf,
bool  is_cidr 
)
static

Definition at line 192 of file network.c.

193 {
194  inet *addr;
195  char *addrptr;
196  int bits;
197  int nb,
198  i;
199 
200  /* make sure any unused bits in a CIDR value are zeroed */
201  addr = (inet *) palloc0(sizeof(inet));
202 
203  ip_family(addr) = pq_getmsgbyte(buf);
204  if (ip_family(addr) != PGSQL_AF_INET &&
205  ip_family(addr) != PGSQL_AF_INET6)
206  ereport(ERROR,
207  (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
208  /* translator: %s is inet or cidr */
209  errmsg("invalid address family in external \"%s\" value",
210  is_cidr ? "cidr" : "inet")));
211  bits = pq_getmsgbyte(buf);
212  if (bits < 0 || bits > ip_maxbits(addr))
213  ereport(ERROR,
214  (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
215  /* translator: %s is inet or cidr */
216  errmsg("invalid bits in external \"%s\" value",
217  is_cidr ? "cidr" : "inet")));
218  ip_bits(addr) = bits;
219  i = pq_getmsgbyte(buf); /* ignore is_cidr */
220  nb = pq_getmsgbyte(buf);
221  if (nb != ip_addrsize(addr))
222  ereport(ERROR,
223  (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
224  /* translator: %s is inet or cidr */
225  errmsg("invalid length in external \"%s\" value",
226  is_cidr ? "cidr" : "inet")));
227 
228  addrptr = (char *) ip_addr(addr);
229  for (i = 0; i < nb; i++)
230  addrptr[i] = pq_getmsgbyte(buf);
231 
232  /*
233  * Error check: CIDR values must not have any bits set beyond the masklen.
234  */
235  if (is_cidr)
236  {
237  if (!addressOK(ip_addr(addr), bits, ip_family(addr)))
238  ereport(ERROR,
239  (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
240  errmsg("invalid external \"cidr\" value"),
241  errdetail("Value has bits set to right of mask.")));
242  }
243 
244  SET_INET_VARSIZE(addr);
245 
246  return addr;
247 }
int pq_getmsgbyte(StringInfo msg)
Definition: pqformat.c:401

References addressOK(), buf, ereport, errcode(), errdetail(), errmsg(), ERROR, i, ip_addr, ip_addrsize, ip_bits, ip_family, ip_maxbits, palloc0(), PGSQL_AF_INET, PGSQL_AF_INET6, pq_getmsgbyte(), and SET_INET_VARSIZE.

Referenced by cidr_recv(), and inet_recv().

◆ network_scan_first()

Datum network_scan_first ( Datum  in)

Definition at line 1690 of file network.c.

1691 {
1693 }
Datum network_network(PG_FUNCTION_ARGS)
Definition: network.c:1330

References DirectFunctionCall1, and network_network().

Referenced by match_network_subset().

◆ network_scan_last()

Datum network_scan_last ( Datum  in)

Definition at line 1704 of file network.c.

1705 {
1708  Int32GetDatum(-1));
1709 }
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:633
Datum network_broadcast(PG_FUNCTION_ARGS)
Definition: network.c:1285
Datum inet_set_masklen(PG_FUNCTION_ARGS)
Definition: network.c:324
#define Int32GetDatum(X)
Definition: postgres.h:523

References DirectFunctionCall1, DirectFunctionCall2, inet_set_masklen(), Int32GetDatum, and network_broadcast().

Referenced by match_network_subset().

◆ network_send()

static bytea* network_send ( inet addr,
bool  is_cidr 
)
static

Definition at line 270 of file network.c.

271 {
273  char *addrptr;
274  int nb,
275  i;
276 
278  pq_sendbyte(&buf, ip_family(addr));
279  pq_sendbyte(&buf, ip_bits(addr));
280  pq_sendbyte(&buf, is_cidr);
281  nb = ip_addrsize(addr);
282  if (nb < 0)
283  nb = 0;
284  pq_sendbyte(&buf, nb);
285  addrptr = (char *) ip_addr(addr);
286  for (i = 0; i < nb; i++)
287  pq_sendbyte(&buf, addrptr[i]);
288  return pq_endtypsend(&buf);
289 }
void pq_begintypsend(StringInfo buf)
Definition: pqformat.c:328
bytea * pq_endtypsend(StringInfo buf)
Definition: pqformat.c:348
static void pq_sendbyte(StringInfo buf, uint8 byt)
Definition: pqformat.h:161

References buf, i, ip_addr, ip_addrsize, ip_bits, ip_family, pq_begintypsend(), pq_endtypsend(), and pq_sendbyte().

Referenced by cidr_send(), and inet_send().

◆ network_show()

Datum network_show ( PG_FUNCTION_ARGS  )

Definition at line 1199 of file network.c.

1200 {
1201  inet *ip = PG_GETARG_INET_PP(0);
1202  int len;
1203  char tmp[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255/128")];
1204 
1205  if (pg_inet_net_ntop(ip_family(ip), ip_addr(ip), ip_maxbits(ip),
1206  tmp, sizeof(tmp)) == NULL)
1207  ereport(ERROR,
1208  (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
1209  errmsg("could not format inet value: %m")));
1210 
1211  /* Add /n if not present (which it won't be) */
1212  if (strchr(tmp, '/') == NULL)
1213  {
1214  len = strlen(tmp);
1215  snprintf(tmp + len, sizeof(tmp) - len, "/%u", ip_bits(ip));
1216  }
1217 
1219 }

References cstring_to_text(), ereport, errcode(), errmsg(), ERROR, ip_addr, ip_bits, ip_family, ip_maxbits, len, PG_GETARG_INET_PP, pg_inet_net_ntop(), PG_RETURN_TEXT_P, and snprintf.

◆ network_smaller()

Datum network_smaller ( PG_FUNCTION_ARGS  )

Definition at line 853 of file network.c.

854 {
855  inet *a1 = PG_GETARG_INET_PP(0);
856  inet *a2 = PG_GETARG_INET_PP(1);
857 
858  if (network_cmp_internal(a1, a2) < 0)
860  else
862 }

References a1, a2, network_cmp_internal(), PG_GETARG_INET_PP, and PG_RETURN_INET_P.

◆ network_sortsupport()

Datum network_sortsupport ( PG_FUNCTION_ARGS  )

Definition at line 437 of file network.c.

438 {
440 
442  ssup->ssup_extra = NULL;
443 
444  if (ssup->abbreviate)
445  {
447  MemoryContext oldcontext;
448 
449  oldcontext = MemoryContextSwitchTo(ssup->ssup_cxt);
450 
451  uss = palloc(sizeof(network_sortsupport_state));
452  uss->input_count = 0;
453  uss->estimating = true;
454  initHyperLogLog(&uss->abbr_card, 10);
455 
456  ssup->ssup_extra = uss;
457 
462 
463  MemoryContextSwitchTo(oldcontext);
464  }
465 
466  PG_RETURN_VOID();
467 }
#define PG_RETURN_VOID()
Definition: fmgr.h:349
void initHyperLogLog(hyperLogLogState *cState, uint8 bwidth)
Definition: hyperloglog.c:66
static int network_fast_cmp(Datum x, Datum y, SortSupport ssup)
Definition: network.c:473
static Datum network_abbrev_convert(Datum original, SortSupport ssup)
Definition: network.c:625
static bool network_abbrev_abort(int memtupcount, SortSupport ssup)
Definition: network.c:488
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
struct SortSupportData * SortSupport
Definition: sortsupport.h:58
int(* comparator)(Datum x, Datum y, SortSupport ssup)
Definition: sortsupport.h:106
Datum(* abbrev_converter)(Datum original, SortSupport ssup)
Definition: sortsupport.h:172
MemoryContext ssup_cxt
Definition: sortsupport.h:66
int(* abbrev_full_comparator)(Datum x, Datum y, SortSupport ssup)
Definition: sortsupport.h:191
bool(* abbrev_abort)(int memtupcount, SortSupport ssup)
Definition: sortsupport.h:182
int ssup_datum_unsigned_cmp(Datum x, Datum y, SortSupport ssup)
Definition: tuplesort.c:4899

References network_sortsupport_state::abbr_card, SortSupportData::abbrev_abort, SortSupportData::abbrev_converter, SortSupportData::abbrev_full_comparator, SortSupportData::abbreviate, SortSupportData::comparator, network_sortsupport_state::estimating, initHyperLogLog(), network_sortsupport_state::input_count, MemoryContextSwitchTo(), network_abbrev_abort(), network_abbrev_convert(), network_fast_cmp(), palloc(), PG_GETARG_POINTER, PG_RETURN_VOID, SortSupportData::ssup_cxt, ssup_datum_unsigned_cmp(), and SortSupportData::ssup_extra.

◆ network_sub()

Datum network_sub ( PG_FUNCTION_ARGS  )

Definition at line 903 of file network.c.

904 {
905  inet *a1 = PG_GETARG_INET_PP(0);
906  inet *a2 = PG_GETARG_INET_PP(1);
907 
908  if (ip_family(a1) == ip_family(a2))
909  {
911  bitncmp(ip_addr(a1), ip_addr(a2), ip_bits(a2)) == 0);
912  }
913 
914  PG_RETURN_BOOL(false);
915 }

References a1, a2, bitncmp(), ip_addr, ip_bits, ip_family, PG_GETARG_INET_PP, and PG_RETURN_BOOL.

◆ network_subeq()

Datum network_subeq ( PG_FUNCTION_ARGS  )

Definition at line 918 of file network.c.

919 {
920  inet *a1 = PG_GETARG_INET_PP(0);
921  inet *a2 = PG_GETARG_INET_PP(1);
922 
923  if (ip_family(a1) == ip_family(a2))
924  {
926  bitncmp(ip_addr(a1), ip_addr(a2), ip_bits(a2)) == 0);
927  }
928 
929  PG_RETURN_BOOL(false);
930 }

References a1, a2, bitncmp(), ip_addr, ip_bits, ip_family, PG_GETARG_INET_PP, and PG_RETURN_BOOL.

◆ network_subset_support()

Datum network_subset_support ( PG_FUNCTION_ARGS  )

Definition at line 981 of file network.c.

982 {
983  Node *rawreq = (Node *) PG_GETARG_POINTER(0);
984  Node *ret = NULL;
985 
986  if (IsA(rawreq, SupportRequestIndexCondition))
987  {
988  /* Try to convert operator/function call to index conditions */
990 
991  if (is_opclause(req->node))
992  {
993  OpExpr *clause = (OpExpr *) req->node;
994 
995  Assert(list_length(clause->args) == 2);
996  ret = (Node *)
998  (Node *) lsecond(clause->args),
999  req->indexarg,
1000  req->funcid,
1001  req->opfamily);
1002  }
1003  else if (is_funcclause(req->node)) /* be paranoid */
1004  {
1005  FuncExpr *clause = (FuncExpr *) req->node;
1006 
1007  Assert(list_length(clause->args) == 2);
1008  ret = (Node *)
1010  (Node *) lsecond(clause->args),
1011  req->indexarg,
1012  req->funcid,
1013  req->opfamily);
1014  }
1015  }
1016 
1017  PG_RETURN_POINTER(ret);
1018 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
static List * match_network_function(Node *leftop, Node *rightop, int indexarg, Oid funcid, Oid opfamily)
Definition: network.c:1028
static bool is_opclause(const void *clause)
Definition: nodeFuncs.h:64
static bool is_funcclause(const void *clause)
Definition: nodeFuncs.h:57
static int list_length(const List *l)
Definition: pg_list.h:150
#define linitial(l)
Definition: pg_list.h:176
#define lsecond(l)
Definition: pg_list.h:181
List * args
Definition: primnodes.h:593
Definition: nodes.h:575
List * args
Definition: primnodes.h:652

References FuncExpr::args, OpExpr::args, Assert(), SupportRequestIndexCondition::funcid, SupportRequestIndexCondition::indexarg, is_funcclause(), is_opclause(), IsA, linitial, list_length(), lsecond, match_network_function(), SupportRequestIndexCondition::node, SupportRequestIndexCondition::opfamily, PG_GETARG_POINTER, and PG_RETURN_POINTER.

◆ network_sup()

Datum network_sup ( PG_FUNCTION_ARGS  )

Definition at line 933 of file network.c.

934 {
935  inet *a1 = PG_GETARG_INET_PP(0);
936  inet *a2 = PG_GETARG_INET_PP(1);
937 
938  if (ip_family(a1) == ip_family(a2))
939  {
941  bitncmp(ip_addr(a1), ip_addr(a2), ip_bits(a1)) == 0);
942  }
943 
944  PG_RETURN_BOOL(false);
945 }

References a1, a2, bitncmp(), ip_addr, ip_bits, ip_family, PG_GETARG_INET_PP, and PG_RETURN_BOOL.

◆ network_supeq()

Datum network_supeq ( PG_FUNCTION_ARGS  )

Definition at line 948 of file network.c.

949 {
950  inet *a1 = PG_GETARG_INET_PP(0);
951  inet *a2 = PG_GETARG_INET_PP(1);
952 
953  if (ip_family(a1) == ip_family(a2))
954  {
956  bitncmp(ip_addr(a1), ip_addr(a2), ip_bits(a1)) == 0);
957  }
958 
959  PG_RETURN_BOOL(false);
960 }

References a1, a2, bitncmp(), ip_addr, ip_bits, ip_family, PG_GETARG_INET_PP, and PG_RETURN_BOOL.