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/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/hashutils.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 int network_cmp_abbrev (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.

Referenced by network_abbrev_convert().

◆ ABBREV_BITS_INET4_SUBNET

#define ABBREV_BITS_INET4_SUBNET   25

Definition at line 43 of file network.c.

Referenced by network_abbrev_convert().

Function Documentation

◆ addressOK()

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

Definition at line 1656 of file network.c.

References Assert, byte, and PGSQL_AF_INET.

Referenced by network_in(), and network_recv().

1657 {
1658  int byte;
1659  int nbits;
1660  int maxbits;
1661  int maxbytes;
1662  unsigned char mask;
1663 
1664  if (family == PGSQL_AF_INET)
1665  {
1666  maxbits = 32;
1667  maxbytes = 4;
1668  }
1669  else
1670  {
1671  maxbits = 128;
1672  maxbytes = 16;
1673  }
1674  Assert(bits <= maxbits);
1675 
1676  if (bits == maxbits)
1677  return true;
1678 
1679  byte = bits / 8;
1680 
1681  nbits = bits % 8;
1682  mask = 0xff;
1683  if (bits != 0)
1684  mask >>= nbits;
1685 
1686  while (byte < maxbytes)
1687  {
1688  if ((a[byte] & mask) != 0)
1689  return false;
1690  mask = 0xff;
1691  byte++;
1692  }
1693 
1694  return true;
1695 }
#define PGSQL_AF_INET
Definition: inet.h:39
#define byte(x, n)
Definition: rijndael.c:68
#define Assert(condition)
Definition: c.h:732

◆ bitncmp()

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

Definition at line 1584 of file network.c.

References IS_HIGHBIT_SET.

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().

1585 {
1586  unsigned int lb,
1587  rb;
1588  int x,
1589  b;
1590 
1591  b = n / 8;
1592  x = memcmp(l, r, b);
1593  if (x || (n % 8) == 0)
1594  return x;
1595 
1596  lb = l[b];
1597  rb = r[b];
1598  for (b = n % 8; b > 0; b--)
1599  {
1600  if (IS_HIGHBIT_SET(lb) != IS_HIGHBIT_SET(rb))
1601  {
1602  if (IS_HIGHBIT_SET(lb))
1603  return 1;
1604  return -1;
1605  }
1606  lb <<= 1;
1607  rb <<= 1;
1608  }
1609  return 0;
1610 }
#define IS_HIGHBIT_SET(ch)
Definition: c.h:1075

◆ bitncommon()

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

Definition at line 1618 of file network.c.

References byte.

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().

1619 {
1620  int byte,
1621  nbits;
1622 
1623  /* number of bits to examine in last byte */
1624  nbits = n % 8;
1625 
1626  /* check whole bytes */
1627  for (byte = 0; byte < n / 8; byte++)
1628  {
1629  if (l[byte] != r[byte])
1630  {
1631  /* at least one bit in the last byte is not common */
1632  nbits = 7;
1633  break;
1634  }
1635  }
1636 
1637  /* check bits in last partial byte */
1638  if (nbits != 0)
1639  {
1640  /* calculate diff of first non-matching bytes */
1641  unsigned int diff = l[byte] ^ r[byte];
1642 
1643  /* compare the bits from the most to the least */
1644  while ((diff >> (8 - nbits)) != 0)
1645  nbits--;
1646  }
1647 
1648  return (8 * byte) + nbits;
1649 }
#define byte(x, n)
Definition: rijndael.c:68

◆ cidr_abbrev()

Datum cidr_abbrev ( PG_FUNCTION_ARGS  )

Definition at line 1255 of file network.c.

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

1256 {
1257  inet *ip = PG_GETARG_INET_PP(0);
1258  char *dst;
1259  char tmp[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255/128")];
1260 
1261  dst = inet_cidr_ntop(ip_family(ip), ip_addr(ip),
1262  ip_bits(ip), tmp, sizeof(tmp));
1263 
1264  if (dst == NULL)
1265  ereport(ERROR,
1266  (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
1267  errmsg("could not format cidr value: %m")));
1268 
1270 }
#define ip_bits(inetptr)
Definition: inet.h:74
#define ip_family(inetptr)
Definition: inet.h:71
#define ip_addr(inetptr)
Definition: inet.h:77
int errcode(int sqlerrcode)
Definition: elog.c:570
char * inet_cidr_ntop(int af, const void *src, int bits, char *dst, size_t size)
#define ERROR
Definition: elog.h:43
#define PG_GETARG_INET_PP(n)
Definition: inet.h:124
Definition: inet.h:52
#define ereport(elevel, rest)
Definition: elog.h:141
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:361
text * cstring_to_text(const char *s)
Definition: varlena.c:171
int errmsg(const char *fmt,...)
Definition: elog.c:784

◆ cidr_in()

Datum cidr_in ( PG_FUNCTION_ARGS  )

Definition at line 130 of file network.c.

References network_in(), PG_GETARG_CSTRING, and PG_RETURN_INET_P.

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

◆ cidr_out()

Datum cidr_out ( PG_FUNCTION_ARGS  )

Definition at line 174 of file network.c.

References network_out(), PG_GETARG_INET_PP, and PG_RETURN_CSTRING.

175 {
176  inet *src = PG_GETARG_INET_PP(0);
177 
178  PG_RETURN_CSTRING(network_out(src, true));
179 }
#define PG_GETARG_INET_PP(n)
Definition: inet.h:124
Definition: inet.h:52
static char * network_out(inet *src, bool is_cidr)
Definition: network.c:142
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:352

◆ cidr_recv()

Datum cidr_recv ( PG_FUNCTION_ARGS  )

Definition at line 259 of file network.c.

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

260 {
262 
263  PG_RETURN_INET_P(network_recv(buf, true));
264 }
StringInfoData * StringInfo
Definition: stringinfo.h:43
#define PG_RETURN_INET_P(x)
Definition: inet.h:125
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:271
static char * buf
Definition: pg_test_fsync.c:68
static inet * network_recv(StringInfo buf, bool is_cidr)
Definition: network.c:193

◆ cidr_send()

Datum cidr_send ( PG_FUNCTION_ARGS  )

Definition at line 301 of file network.c.

References network_send(), PG_GETARG_INET_PP, and PG_RETURN_BYTEA_P.

302 {
303  inet *addr = PG_GETARG_INET_PP(0);
304 
305  PG_RETURN_BYTEA_P(network_send(addr, true));
306 }
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:360
#define PG_GETARG_INET_PP(n)
Definition: inet.h:124
Definition: inet.h:52
static bytea * network_send(inet *addr, bool is_cidr)
Definition: network.c:271

◆ cidr_set_masklen()

Datum cidr_set_masklen ( PG_FUNCTION_ARGS  )

Definition at line 349 of file network.c.

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

350 {
351  inet *src = PG_GETARG_INET_PP(0);
352  int bits = PG_GETARG_INT32(1);
353 
354  if (bits == -1)
355  bits = ip_maxbits(src);
356 
357  if ((bits < 0) || (bits > ip_maxbits(src)))
358  ereport(ERROR,
359  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
360  errmsg("invalid mask length: %d", bits)));
361 
363 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
#define PG_RETURN_INET_P(x)
Definition: inet.h:125
int errcode(int sqlerrcode)
Definition: elog.c:570
#define ip_maxbits(inetptr)
Definition: inet.h:83
#define ERROR
Definition: elog.h:43
#define PG_GETARG_INET_PP(n)
Definition: inet.h:124
Definition: inet.h:52
#define ereport(elevel, rest)
Definition: elog.h:141
inet * cidr_set_masklen_internal(const inet *src, int bits)
Definition: network.c:369
int errmsg(const char *fmt,...)
Definition: elog.c:784

◆ cidr_set_masklen_internal()

inet* cidr_set_masklen_internal ( const inet src,
int  bits 
)

Definition at line 369 of file network.c.

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().

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

◆ clean_ipv6_addr()

void clean_ipv6_addr ( int  addr_family,
char *  addr 
)

Definition at line 2118 of file network.c.

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

2119 {
2120 #ifdef HAVE_IPV6
2121  if (addr_family == AF_INET6)
2122  {
2123  char *pct = strchr(addr, '%');
2124 
2125  if (pct)
2126  *pct = '\0';
2127  }
2128 #endif
2129 }

◆ convert_network_to_scalar()

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

Definition at line 1517 of file network.c.

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, and PGSQL_AF_INET.

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

1518 {
1519  switch (typid)
1520  {
1521  case INETOID:
1522  case CIDROID:
1523  {
1524  inet *ip = DatumGetInetPP(value);
1525  int len;
1526  double res;
1527  int i;
1528 
1529  /*
1530  * Note that we don't use the full address for IPv6.
1531  */
1532  if (ip_family(ip) == PGSQL_AF_INET)
1533  len = 4;
1534  else
1535  len = 5;
1536 
1537  res = ip_family(ip);
1538  for (i = 0; i < len; i++)
1539  {
1540  res *= 256;
1541  res += ip_addr(ip)[i];
1542  }
1543  return res;
1544  }
1545  case MACADDROID:
1546  {
1547  macaddr *mac = DatumGetMacaddrP(value);
1548  double res;
1549 
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);
1553  return res;
1554  }
1555  case MACADDR8OID:
1556  {
1558  double res;
1559 
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);
1563  return res;
1564  }
1565  }
1566 
1567  *failure = true;
1568  return 0;
1569 }
#define DatumGetMacaddrP(X)
Definition: inet.h:131
#define ip_family(inetptr)
Definition: inet.h:71
#define PGSQL_AF_INET
Definition: inet.h:39
#define DatumGetInetPP(X)
Definition: inet.h:122
unsigned char f
Definition: inet.h:101
#define ip_addr(inetptr)
Definition: inet.h:77
static struct @144 value
unsigned char h
Definition: inet.h:116
unsigned char f
Definition: inet.h:114
unsigned char g
Definition: inet.h:115
unsigned char c
Definition: inet.h:98
unsigned char a
Definition: inet.h:109
unsigned char a
Definition: inet.h:96
Definition: inet.h:107
unsigned char d
Definition: inet.h:99
unsigned char b
Definition: inet.h:110
Definition: inet.h:52
unsigned char e
Definition: inet.h:113
unsigned char c
Definition: inet.h:111
unsigned char b
Definition: inet.h:97
unsigned char e
Definition: inet.h:100
Definition: inet.h:94
#define DatumGetMacaddr8P(X)
Definition: inet.h:137
unsigned char d
Definition: inet.h:112
int i

◆ hashinet()

Datum hashinet ( PG_FUNCTION_ARGS  )

Definition at line 895 of file network.c.

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

896 {
897  inet *addr = PG_GETARG_INET_PP(0);
898  int addrsize = ip_addrsize(addr);
899 
900  /* XXX this assumes there are no pad bytes in the data structure */
901  return hash_any((unsigned char *) VARDATA_ANY(addr), addrsize + 2);
902 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define ip_addrsize(inetptr)
Definition: inet.h:80
#define PG_GETARG_INET_PP(n)
Definition: inet.h:124
Definition: inet.h:52
Datum hash_any(register const unsigned char *k, register int keylen)
Definition: hashfn.c:148

◆ hashinetextended()

Datum hashinetextended ( PG_FUNCTION_ARGS  )

Definition at line 905 of file network.c.

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

906 {
907  inet *addr = PG_GETARG_INET_PP(0);
908  int addrsize = ip_addrsize(addr);
909 
910  return hash_any_extended((unsigned char *) VARDATA_ANY(addr), addrsize + 2,
911  PG_GETARG_INT64(1));
912 }
Datum hash_any_extended(register const unsigned char *k, register int keylen, uint64 seed)
Definition: hashfn.c:374
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define ip_addrsize(inetptr)
Definition: inet.h:80
#define PG_GETARG_INET_PP(n)
Definition: inet.h:124
Definition: inet.h:52
#define PG_GETARG_INT64(n)
Definition: fmgr.h:277

◆ inet_abbrev()

Datum inet_abbrev ( PG_FUNCTION_ARGS  )

Definition at line 1237 of file network.c.

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

1238 {
1239  inet *ip = PG_GETARG_INET_PP(0);
1240  char *dst;
1241  char tmp[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255/128")];
1242 
1243  dst = inet_net_ntop(ip_family(ip), ip_addr(ip),
1244  ip_bits(ip), tmp, sizeof(tmp));
1245 
1246  if (dst == NULL)
1247  ereport(ERROR,
1248  (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
1249  errmsg("could not format inet value: %m")));
1250 
1252 }
#define ip_bits(inetptr)
Definition: inet.h:74
#define ip_family(inetptr)
Definition: inet.h:71
#define ip_addr(inetptr)
Definition: inet.h:77
int errcode(int sqlerrcode)
Definition: elog.c:570
#define ERROR
Definition: elog.h:43
#define PG_GETARG_INET_PP(n)
Definition: inet.h:124
Definition: inet.h:52
char * inet_net_ntop(int af, const void *src, int bits, char *dst, size_t size)
Definition: inet_net_ntop.c:77
#define ereport(elevel, rest)
Definition: elog.h:141
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:361
text * cstring_to_text(const char *s)
Definition: varlena.c:171
int errmsg(const char *fmt,...)
Definition: elog.c:784

◆ inet_client_addr()

Datum inet_client_addr ( PG_FUNCTION_ARGS  )

Definition at line 1731 of file network.c.

References SockAddr::addr, clean_ipv6_addr(), MyProcPort, network_in(), NI_MAXHOST, NI_NUMERICHOST, NI_NUMERICSERV, pg_getnameinfo_all(), PG_RETURN_INET_P, PG_RETURN_NULL, port, Port::raddr, and SockAddr::salen.

1732 {
1733  Port *port = MyProcPort;
1734  char remote_host[NI_MAXHOST];
1735  int ret;
1736 
1737  if (port == NULL)
1738  PG_RETURN_NULL();
1739 
1740  switch (port->raddr.addr.ss_family)
1741  {
1742  case AF_INET:
1743 #ifdef HAVE_IPV6
1744  case AF_INET6:
1745 #endif
1746  break;
1747  default:
1748  PG_RETURN_NULL();
1749  }
1750 
1751  remote_host[0] = '\0';
1752 
1753  ret = pg_getnameinfo_all(&port->raddr.addr, port->raddr.salen,
1754  remote_host, sizeof(remote_host),
1755  NULL, 0,
1757  if (ret != 0)
1758  PG_RETURN_NULL();
1759 
1760  clean_ipv6_addr(port->raddr.addr.ss_family, remote_host);
1761 
1762  PG_RETURN_INET_P(network_in(remote_host, false));
1763 }
#define NI_NUMERICHOST
Definition: getaddrinfo.h:78
struct Port * MyProcPort
Definition: globals.c:43
struct sockaddr_storage addr
Definition: pqcomm.h:64
#define PG_RETURN_INET_P(x)
Definition: inet.h:125
Definition: libpq-be.h:120
static inet * network_in(char *src, bool is_cidr)
Definition: network.c:76
SockAddr raddr
Definition: libpq-be.h:126
#define NI_MAXHOST
Definition: getaddrinfo.h:88
ACCEPT_TYPE_ARG3 salen
Definition: pqcomm.h:65
static int port
Definition: pg_regress.c:92
#define NI_NUMERICSERV
Definition: getaddrinfo.h:81
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:2118
#define PG_RETURN_NULL()
Definition: fmgr.h:335

◆ inet_client_port()

Datum inet_client_port ( PG_FUNCTION_ARGS  )

Definition at line 1770 of file network.c.

References SockAddr::addr, CStringGetDatum, DirectFunctionCall1, int4in(), MyProcPort, NI_MAXSERV, NI_NUMERICHOST, NI_NUMERICSERV, pg_getnameinfo_all(), PG_RETURN_DATUM, PG_RETURN_NULL, port, Port::raddr, and SockAddr::salen.

1771 {
1772  Port *port = MyProcPort;
1773  char remote_port[NI_MAXSERV];
1774  int ret;
1775 
1776  if (port == NULL)
1777  PG_RETURN_NULL();
1778 
1779  switch (port->raddr.addr.ss_family)
1780  {
1781  case AF_INET:
1782 #ifdef HAVE_IPV6
1783  case AF_INET6:
1784 #endif
1785  break;
1786  default:
1787  PG_RETURN_NULL();
1788  }
1789 
1790  remote_port[0] = '\0';
1791 
1792  ret = pg_getnameinfo_all(&port->raddr.addr, port->raddr.salen,
1793  NULL, 0,
1794  remote_port, sizeof(remote_port),
1796  if (ret != 0)
1797  PG_RETURN_NULL();
1798 
1800 }
#define NI_NUMERICHOST
Definition: getaddrinfo.h:78
struct Port * MyProcPort
Definition: globals.c:43
Datum int4in(PG_FUNCTION_ARGS)
Definition: int.c:268
struct sockaddr_storage addr
Definition: pqcomm.h:64
Definition: libpq-be.h:120
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:616
SockAddr raddr
Definition: libpq-be.h:126
#define CStringGetDatum(X)
Definition: postgres.h:578
#define NI_MAXSERV
Definition: getaddrinfo.h:91
ACCEPT_TYPE_ARG3 salen
Definition: pqcomm.h:65
static int port
Definition: pg_regress.c:92
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:343
#define NI_NUMERICSERV
Definition: getaddrinfo.h:81
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
#define PG_RETURN_NULL()
Definition: fmgr.h:335

◆ inet_in()

Datum inet_in ( PG_FUNCTION_ARGS  )

Definition at line 122 of file network.c.

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().

123 {
124  char *src = PG_GETARG_CSTRING(0);
125 
126  PG_RETURN_INET_P(network_in(src, false));
127 }
#define PG_RETURN_INET_P(x)
Definition: inet.h:125
static inet * network_in(char *src, bool is_cidr)
Definition: network.c:76
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:272

◆ inet_merge()

Datum inet_merge ( PG_FUNCTION_ARGS  )

Definition at line 1491 of file network.c.

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.

1492 {
1493  inet *a1 = PG_GETARG_INET_PP(0),
1494  *a2 = PG_GETARG_INET_PP(1);
1495  int commonbits;
1496 
1497  if (ip_family(a1) != ip_family(a2))
1498  ereport(ERROR,
1499  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1500  errmsg("cannot merge addresses from different families")));
1501 
1502  commonbits = bitncommon(ip_addr(a1), ip_addr(a2),
1503  Min(ip_bits(a1), ip_bits(a2)));
1504 
1506 }
#define ip_bits(inetptr)
Definition: inet.h:74
#define ip_family(inetptr)
Definition: inet.h:71
#define ip_addr(inetptr)
Definition: inet.h:77
#define Min(x, y)
Definition: c.h:904
#define PG_RETURN_INET_P(x)
Definition: inet.h:125
int bitncommon(const unsigned char *l, const unsigned char *r, int n)
Definition: network.c:1618
int errcode(int sqlerrcode)
Definition: elog.c:570
static const FormData_pg_attribute a2
Definition: heap.c:166
#define ERROR
Definition: elog.h:43
#define PG_GETARG_INET_PP(n)
Definition: inet.h:124
Definition: inet.h:52
#define ereport(elevel, rest)
Definition: elog.h:141
inet * cidr_set_masklen_internal(const inet *src, int bits)
Definition: network.c:369
int errmsg(const char *fmt,...)
Definition: elog.c:784
static const FormData_pg_attribute a1
Definition: heap.c:152

◆ inet_out()

Datum inet_out ( PG_FUNCTION_ARGS  )

Definition at line 166 of file network.c.

References network_out(), PG_GETARG_INET_PP, and PG_RETURN_CSTRING.

167 {
168  inet *src = PG_GETARG_INET_PP(0);
169 
170  PG_RETURN_CSTRING(network_out(src, false));
171 }
#define PG_GETARG_INET_PP(n)
Definition: inet.h:124
Definition: inet.h:52
static char * network_out(inet *src, bool is_cidr)
Definition: network.c:142
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:352

◆ inet_recv()

Datum inet_recv ( PG_FUNCTION_ARGS  )

Definition at line 251 of file network.c.

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

252 {
254 
255  PG_RETURN_INET_P(network_recv(buf, false));
256 }
StringInfoData * StringInfo
Definition: stringinfo.h:43
#define PG_RETURN_INET_P(x)
Definition: inet.h:125
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:271
static char * buf
Definition: pg_test_fsync.c:68
static inet * network_recv(StringInfo buf, bool is_cidr)
Definition: network.c:193

◆ inet_same_family()

Datum inet_same_family ( PG_FUNCTION_ARGS  )

Definition at line 1479 of file network.c.

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

1480 {
1481  inet *a1 = PG_GETARG_INET_PP(0);
1482  inet *a2 = PG_GETARG_INET_PP(1);
1483 
1484  PG_RETURN_BOOL(ip_family(a1) == ip_family(a2));
1485 }
#define ip_family(inetptr)
Definition: inet.h:71
static const FormData_pg_attribute a2
Definition: heap.c:166
#define PG_GETARG_INET_PP(n)
Definition: inet.h:124
Definition: inet.h:52
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
static const FormData_pg_attribute a1
Definition: heap.c:152

◆ inet_send()

Datum inet_send ( PG_FUNCTION_ARGS  )

Definition at line 293 of file network.c.

References network_send(), PG_GETARG_INET_PP, and PG_RETURN_BYTEA_P.

294 {
295  inet *addr = PG_GETARG_INET_PP(0);
296 
297  PG_RETURN_BYTEA_P(network_send(addr, false));
298 }
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:360
#define PG_GETARG_INET_PP(n)
Definition: inet.h:124
Definition: inet.h:52
static bytea * network_send(inet *addr, bool is_cidr)
Definition: network.c:271

◆ inet_server_addr()

Datum inet_server_addr ( PG_FUNCTION_ARGS  )

Definition at line 1807 of file network.c.

References SockAddr::addr, clean_ipv6_addr(), Port::laddr, MyProcPort, network_in(), NI_MAXHOST, NI_NUMERICHOST, NI_NUMERICSERV, pg_getnameinfo_all(), PG_RETURN_INET_P, PG_RETURN_NULL, port, and SockAddr::salen.

1808 {
1809  Port *port = MyProcPort;
1810  char local_host[NI_MAXHOST];
1811  int ret;
1812 
1813  if (port == NULL)
1814  PG_RETURN_NULL();
1815 
1816  switch (port->laddr.addr.ss_family)
1817  {
1818  case AF_INET:
1819 #ifdef HAVE_IPV6
1820  case AF_INET6:
1821 #endif
1822  break;
1823  default:
1824  PG_RETURN_NULL();
1825  }
1826 
1827  local_host[0] = '\0';
1828 
1829  ret = pg_getnameinfo_all(&port->laddr.addr, port->laddr.salen,
1830  local_host, sizeof(local_host),
1831  NULL, 0,
1833  if (ret != 0)
1834  PG_RETURN_NULL();
1835 
1836  clean_ipv6_addr(port->laddr.addr.ss_family, local_host);
1837 
1838  PG_RETURN_INET_P(network_in(local_host, false));
1839 }
#define NI_NUMERICHOST
Definition: getaddrinfo.h:78
struct Port * MyProcPort
Definition: globals.c:43
struct sockaddr_storage addr
Definition: pqcomm.h:64
#define PG_RETURN_INET_P(x)
Definition: inet.h:125
Definition: libpq-be.h:120
static inet * network_in(char *src, bool is_cidr)
Definition: network.c:76
#define NI_MAXHOST
Definition: getaddrinfo.h:88
ACCEPT_TYPE_ARG3 salen
Definition: pqcomm.h:65
SockAddr laddr
Definition: libpq-be.h:125
static int port
Definition: pg_regress.c:92
#define NI_NUMERICSERV
Definition: getaddrinfo.h:81
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:2118
#define PG_RETURN_NULL()
Definition: fmgr.h:335

◆ inet_server_port()

Datum inet_server_port ( PG_FUNCTION_ARGS  )

Definition at line 1846 of file network.c.

References SockAddr::addr, CStringGetDatum, DirectFunctionCall1, int4in(), Port::laddr, MyProcPort, NI_MAXSERV, NI_NUMERICHOST, NI_NUMERICSERV, pg_getnameinfo_all(), PG_RETURN_DATUM, PG_RETURN_NULL, port, and SockAddr::salen.

1847 {
1848  Port *port = MyProcPort;
1849  char local_port[NI_MAXSERV];
1850  int ret;
1851 
1852  if (port == NULL)
1853  PG_RETURN_NULL();
1854 
1855  switch (port->laddr.addr.ss_family)
1856  {
1857  case AF_INET:
1858 #ifdef HAVE_IPV6
1859  case AF_INET6:
1860 #endif
1861  break;
1862  default:
1863  PG_RETURN_NULL();
1864  }
1865 
1866  local_port[0] = '\0';
1867 
1868  ret = pg_getnameinfo_all(&port->laddr.addr, port->laddr.salen,
1869  NULL, 0,
1870  local_port, sizeof(local_port),
1872  if (ret != 0)
1873  PG_RETURN_NULL();
1874 
1876 }
#define NI_NUMERICHOST
Definition: getaddrinfo.h:78
struct Port * MyProcPort
Definition: globals.c:43
Datum int4in(PG_FUNCTION_ARGS)
Definition: int.c:268
struct sockaddr_storage addr
Definition: pqcomm.h:64
Definition: libpq-be.h:120
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:616
#define CStringGetDatum(X)
Definition: postgres.h:578
#define NI_MAXSERV
Definition: getaddrinfo.h:91
ACCEPT_TYPE_ARG3 salen
Definition: pqcomm.h:65
SockAddr laddr
Definition: libpq-be.h:125
static int port
Definition: pg_regress.c:92
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:343
#define NI_NUMERICSERV
Definition: getaddrinfo.h:81
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
#define PG_RETURN_NULL()
Definition: fmgr.h:335

◆ inet_set_masklen()

Datum inet_set_masklen ( PG_FUNCTION_ARGS  )

Definition at line 325 of file network.c.

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().

326 {
327  inet *src = PG_GETARG_INET_PP(0);
328  int bits = PG_GETARG_INT32(1);
329  inet *dst;
330 
331  if (bits == -1)
332  bits = ip_maxbits(src);
333 
334  if ((bits < 0) || (bits > ip_maxbits(src)))
335  ereport(ERROR,
336  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
337  errmsg("invalid mask length: %d", bits)));
338 
339  /* clone the original data */
340  dst = (inet *) palloc(VARSIZE_ANY(src));
341  memcpy(dst, src, VARSIZE_ANY(src));
342 
343  ip_bits(dst) = bits;
344 
345  PG_RETURN_INET_P(dst);
346 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
#define ip_bits(inetptr)
Definition: inet.h:74
#define PG_RETURN_INET_P(x)
Definition: inet.h:125
int errcode(int sqlerrcode)
Definition: elog.c:570
#define ip_maxbits(inetptr)
Definition: inet.h:83
#define ERROR
Definition: elog.h:43
#define PG_GETARG_INET_PP(n)
Definition: inet.h:124
Definition: inet.h:52
#define ereport(elevel, rest)
Definition: elog.h:141
#define VARSIZE_ANY(PTR)
Definition: postgres.h:335
void * palloc(Size size)
Definition: mcxt.c:924
int errmsg(const char *fmt,...)
Definition: elog.c:784

◆ inet_to_cidr()

Datum inet_to_cidr ( PG_FUNCTION_ARGS  )

Definition at line 310 of file network.c.

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

311 {
312  inet *src = PG_GETARG_INET_PP(0);
313  int bits;
314 
315  bits = ip_bits(src);
316 
317  /* safety check */
318  if ((bits < 0) || (bits > ip_maxbits(src)))
319  elog(ERROR, "invalid inet bit length: %d", bits);
320 
322 }
#define ip_bits(inetptr)
Definition: inet.h:74
#define PG_RETURN_INET_P(x)
Definition: inet.h:125
#define ip_maxbits(inetptr)
Definition: inet.h:83
#define ERROR
Definition: elog.h:43
#define PG_GETARG_INET_PP(n)
Definition: inet.h:124
Definition: inet.h:52
inet * cidr_set_masklen_internal(const inet *src, int bits)
Definition: network.c:369
#define elog(elevel,...)
Definition: elog.h:226

◆ inetand()

Datum inetand ( PG_FUNCTION_ARGS  )

Definition at line 1905 of file network.c.

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.

1906 {
1907  inet *ip = PG_GETARG_INET_PP(0);
1908  inet *ip2 = PG_GETARG_INET_PP(1);
1909  inet *dst;
1910 
1911  dst = (inet *) palloc0(sizeof(inet));
1912 
1913  if (ip_family(ip) != ip_family(ip2))
1914  ereport(ERROR,
1915  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1916  errmsg("cannot AND inet values of different sizes")));
1917  else
1918  {
1919  int nb = ip_addrsize(ip);
1920  unsigned char *pip = ip_addr(ip);
1921  unsigned char *pip2 = ip_addr(ip2);
1922  unsigned char *pdst = ip_addr(dst);
1923 
1924  while (nb-- > 0)
1925  pdst[nb] = pip[nb] & pip2[nb];
1926  }
1927  ip_bits(dst) = Max(ip_bits(ip), ip_bits(ip2));
1928 
1929  ip_family(dst) = ip_family(ip);
1930  SET_INET_VARSIZE(dst);
1931 
1932  PG_RETURN_INET_P(dst);
1933 }
#define ip_bits(inetptr)
Definition: inet.h:74
#define ip_family(inetptr)
Definition: inet.h:71
#define ip_addr(inetptr)
Definition: inet.h:77
#define SET_INET_VARSIZE(dst)
Definition: inet.h:86
#define PG_RETURN_INET_P(x)
Definition: inet.h:125
int errcode(int sqlerrcode)
Definition: elog.c:570
#define ip_addrsize(inetptr)
Definition: inet.h:80
#define ERROR
Definition: elog.h:43
#define PG_GETARG_INET_PP(n)
Definition: inet.h:124
Definition: inet.h:52
#define ereport(elevel, rest)
Definition: elog.h:141
void * palloc0(Size size)
Definition: mcxt.c:955
#define Max(x, y)
Definition: c.h:898
int errmsg(const char *fmt,...)
Definition: elog.c:784

◆ inetmi()

Datum inetmi ( PG_FUNCTION_ARGS  )

Definition at line 2041 of file network.c.

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

2042 {
2043  inet *ip = PG_GETARG_INET_PP(0);
2044  inet *ip2 = PG_GETARG_INET_PP(1);
2045  int64 res = 0;
2046 
2047  if (ip_family(ip) != ip_family(ip2))
2048  ereport(ERROR,
2049  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2050  errmsg("cannot subtract inet values of different sizes")));
2051  else
2052  {
2053  /*
2054  * We form the difference using the traditional complement, increment,
2055  * and add rule, with the increment part being handled by starting the
2056  * carry off at 1. If you don't think integer arithmetic is done in
2057  * two's complement, too bad.
2058  */
2059  int nb = ip_addrsize(ip);
2060  int byte = 0;
2061  unsigned char *pip = ip_addr(ip);
2062  unsigned char *pip2 = ip_addr(ip2);
2063  int carry = 1;
2064 
2065  while (nb-- > 0)
2066  {
2067  int lobyte;
2068 
2069  carry = pip[nb] + (~pip2[nb] & 0xFF) + carry;
2070  lobyte = carry & 0xFF;
2071  if (byte < sizeof(int64))
2072  {
2073  res |= ((int64) lobyte) << (byte * 8);
2074  }
2075  else
2076  {
2077  /*
2078  * Input wider than int64: check for overflow. All bytes to
2079  * the left of what will fit should be 0 or 0xFF, depending on
2080  * sign of the now-complete result.
2081  */
2082  if ((res < 0) ? (lobyte != 0xFF) : (lobyte != 0))
2083  ereport(ERROR,
2084  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
2085  errmsg("result is out of range")));
2086  }
2087  carry >>= 8;
2088  byte++;
2089  }
2090 
2091  /*
2092  * If input is narrower than int64, overflow is not possible, but we
2093  * have to do proper sign extension.
2094  */
2095  if (carry == 0 && byte < sizeof(int64))
2096  res |= ((uint64) (int64) -1) << (byte * 8);
2097  }
2098 
2099  PG_RETURN_INT64(res);
2100 }
#define ip_family(inetptr)
Definition: inet.h:71
#define PG_RETURN_INT64(x)
Definition: fmgr.h:357
#define ip_addr(inetptr)
Definition: inet.h:77
int errcode(int sqlerrcode)
Definition: elog.c:570
#define ip_addrsize(inetptr)
Definition: inet.h:80
#define ERROR
Definition: elog.h:43
#define PG_GETARG_INET_PP(n)
Definition: inet.h:124
Definition: inet.h:52
#define ereport(elevel, rest)
Definition: elog.h:141
#define byte(x, n)
Definition: rijndael.c:68
int errmsg(const char *fmt,...)
Definition: elog.c:784

◆ inetmi_int8()

Datum inetmi_int8 ( PG_FUNCTION_ARGS  )

Definition at line 2031 of file network.c.

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

2032 {
2033  inet *ip = PG_GETARG_INET_PP(0);
2034  int64 addend = PG_GETARG_INT64(1);
2035 
2036  PG_RETURN_INET_P(internal_inetpl(ip, -addend));
2037 }
#define PG_RETURN_INET_P(x)
Definition: inet.h:125
#define PG_GETARG_INET_PP(n)
Definition: inet.h:124
Definition: inet.h:52
static inet * internal_inetpl(inet *ip, int64 addend)
Definition: network.c:1969
#define PG_GETARG_INT64(n)
Definition: fmgr.h:277

◆ inetnot()

Datum inetnot ( PG_FUNCTION_ARGS  )

Definition at line 1880 of file network.c.

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

1881 {
1882  inet *ip = PG_GETARG_INET_PP(0);
1883  inet *dst;
1884 
1885  dst = (inet *) palloc0(sizeof(inet));
1886 
1887  {
1888  int nb = ip_addrsize(ip);
1889  unsigned char *pip = ip_addr(ip);
1890  unsigned char *pdst = ip_addr(dst);
1891 
1892  while (nb-- > 0)
1893  pdst[nb] = ~pip[nb];
1894  }
1895  ip_bits(dst) = ip_bits(ip);
1896 
1897  ip_family(dst) = ip_family(ip);
1898  SET_INET_VARSIZE(dst);
1899 
1900  PG_RETURN_INET_P(dst);
1901 }
#define ip_bits(inetptr)
Definition: inet.h:74
#define ip_family(inetptr)
Definition: inet.h:71
#define ip_addr(inetptr)
Definition: inet.h:77
#define SET_INET_VARSIZE(dst)
Definition: inet.h:86
#define PG_RETURN_INET_P(x)
Definition: inet.h:125
#define ip_addrsize(inetptr)
Definition: inet.h:80
#define PG_GETARG_INET_PP(n)
Definition: inet.h:124
Definition: inet.h:52
void * palloc0(Size size)
Definition: mcxt.c:955

◆ inetor()

Datum inetor ( PG_FUNCTION_ARGS  )

Definition at line 1937 of file network.c.

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.

1938 {
1939  inet *ip = PG_GETARG_INET_PP(0);
1940  inet *ip2 = PG_GETARG_INET_PP(1);
1941  inet *dst;
1942 
1943  dst = (inet *) palloc0(sizeof(inet));
1944 
1945  if (ip_family(ip) != ip_family(ip2))
1946  ereport(ERROR,
1947  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1948  errmsg("cannot OR inet values of different sizes")));
1949  else
1950  {
1951  int nb = ip_addrsize(ip);
1952  unsigned char *pip = ip_addr(ip);
1953  unsigned char *pip2 = ip_addr(ip2);
1954  unsigned char *pdst = ip_addr(dst);
1955 
1956  while (nb-- > 0)
1957  pdst[nb] = pip[nb] | pip2[nb];
1958  }
1959  ip_bits(dst) = Max(ip_bits(ip), ip_bits(ip2));
1960 
1961  ip_family(dst) = ip_family(ip);
1962  SET_INET_VARSIZE(dst);
1963 
1964  PG_RETURN_INET_P(dst);
1965 }
#define ip_bits(inetptr)
Definition: inet.h:74
#define ip_family(inetptr)
Definition: inet.h:71
#define ip_addr(inetptr)
Definition: inet.h:77
#define SET_INET_VARSIZE(dst)
Definition: inet.h:86
#define PG_RETURN_INET_P(x)
Definition: inet.h:125
int errcode(int sqlerrcode)
Definition: elog.c:570
#define ip_addrsize(inetptr)
Definition: inet.h:80
#define ERROR
Definition: elog.h:43
#define PG_GETARG_INET_PP(n)
Definition: inet.h:124
Definition: inet.h:52
#define ereport(elevel, rest)
Definition: elog.h:141
void * palloc0(Size size)
Definition: mcxt.c:955
#define Max(x, y)
Definition: c.h:898
int errmsg(const char *fmt,...)
Definition: elog.c:784

◆ inetpl()

Datum inetpl ( PG_FUNCTION_ARGS  )

Definition at line 2021 of file network.c.

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

2022 {
2023  inet *ip = PG_GETARG_INET_PP(0);
2024  int64 addend = PG_GETARG_INT64(1);
2025 
2026  PG_RETURN_INET_P(internal_inetpl(ip, addend));
2027 }
#define PG_RETURN_INET_P(x)
Definition: inet.h:125
#define PG_GETARG_INET_PP(n)
Definition: inet.h:124
Definition: inet.h:52
static inet * internal_inetpl(inet *ip, int64 addend)
Definition: network.c:1969
#define PG_GETARG_INT64(n)
Definition: fmgr.h:277

◆ internal_inetpl()

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

Definition at line 1969 of file network.c.

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

Referenced by inetmi_int8(), and inetpl().

1970 {
1971  inet *dst;
1972 
1973  dst = (inet *) palloc0(sizeof(inet));
1974 
1975  {
1976  int nb = ip_addrsize(ip);
1977  unsigned char *pip = ip_addr(ip);
1978  unsigned char *pdst = ip_addr(dst);
1979  int carry = 0;
1980 
1981  while (nb-- > 0)
1982  {
1983  carry = pip[nb] + (int) (addend & 0xFF) + carry;
1984  pdst[nb] = (unsigned char) (carry & 0xFF);
1985  carry >>= 8;
1986 
1987  /*
1988  * We have to be careful about right-shifting addend because
1989  * right-shift isn't portable for negative values, while simply
1990  * dividing by 256 doesn't work (the standard rounding is in the
1991  * wrong direction, besides which there may be machines out there
1992  * that round the wrong way). So, explicitly clear the low-order
1993  * byte to remove any doubt about the correct result of the
1994  * division, and then divide rather than shift.
1995  */
1996  addend &= ~((int64) 0xFF);
1997  addend /= 0x100;
1998  }
1999 
2000  /*
2001  * At this point we should have addend and carry both zero if original
2002  * addend was >= 0, or addend -1 and carry 1 if original addend was <
2003  * 0. Anything else means overflow.
2004  */
2005  if (!((addend == 0 && carry == 0) ||
2006  (addend == -1 && carry == 1)))
2007  ereport(ERROR,
2008  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
2009  errmsg("result is out of range")));
2010  }
2011 
2012  ip_bits(dst) = ip_bits(ip);
2013  ip_family(dst) = ip_family(ip);
2014  SET_INET_VARSIZE(dst);
2015 
2016  return dst;
2017 }
#define ip_bits(inetptr)
Definition: inet.h:74
#define ip_family(inetptr)
Definition: inet.h:71
#define ip_addr(inetptr)
Definition: inet.h:77
#define SET_INET_VARSIZE(dst)
Definition: inet.h:86
int errcode(int sqlerrcode)
Definition: elog.c:570
#define ip_addrsize(inetptr)
Definition: inet.h:80
#define ERROR
Definition: elog.h:43
Definition: inet.h:52
#define ereport(elevel, rest)
Definition: elog.h:141
void * palloc0(Size size)
Definition: mcxt.c:955
int errmsg(const char *fmt,...)
Definition: elog.c:784

◆ match_network_function()

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

Definition at line 1043 of file network.c.

References match_network_subset(), and NIL.

Referenced by network_subset_support().

1048 {
1049  switch (funcid)
1050  {
1051  case F_NETWORK_SUB:
1052  /* indexkey must be on the left */
1053  if (indexarg != 0)
1054  return NIL;
1055  return match_network_subset(leftop, rightop, false, opfamily);
1056 
1057  case F_NETWORK_SUBEQ:
1058  /* indexkey must be on the left */
1059  if (indexarg != 0)
1060  return NIL;
1061  return match_network_subset(leftop, rightop, true, opfamily);
1062 
1063  case F_NETWORK_SUP:
1064  /* indexkey must be on the right */
1065  if (indexarg != 1)
1066  return NIL;
1067  return match_network_subset(rightop, leftop, false, opfamily);
1068 
1069  case F_NETWORK_SUPEQ:
1070  /* indexkey must be on the right */
1071  if (indexarg != 1)
1072  return NIL;
1073  return match_network_subset(rightop, leftop, true, opfamily);
1074 
1075  default:
1076 
1077  /*
1078  * We'd only get here if somebody attached this support function
1079  * to an unexpected function. Maybe we should complain, but for
1080  * now, do nothing.
1081  */
1082  return NIL;
1083  }
1084 }
#define NIL
Definition: pg_list.h:65
static List * match_network_subset(Node *leftop, Node *rightop, bool is_eq, Oid opfamily)
Definition: network.c:1091

◆ match_network_subset()

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

Definition at line 1091 of file network.c.

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().

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

◆ network_abbrev_abort()

static bool network_abbrev_abort ( int  memtupcount,
SortSupport  ssup 
)
static

Definition at line 503 of file network.c.

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().

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

◆ network_abbrev_convert()

static Datum network_abbrev_convert ( Datum  original,
SortSupport  ssup 
)
static

Definition at line 640 of file network.c.

References network_sortsupport_state::abbr_card, ABBREV_BITS_INET4_NETMASK_SIZE, ABBREV_BITS_INET4_SUBNET, addHyperLogLog(), Assert, BITS_PER_BYTE, DatumBigEndianToNative, 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, SIZEOF_DATUM, and SortSupportData::ssup_extra.

Referenced by network_sortsupport().

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

◆ network_broadcast()

Datum network_broadcast ( PG_FUNCTION_ARGS  )

Definition at line 1300 of file network.c.

References byte, 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().

1301 {
1302  inet *ip = PG_GETARG_INET_PP(0);
1303  inet *dst;
1304  int byte;
1305  int bits;
1306  int maxbytes;
1307  unsigned char mask;
1308  unsigned char *a,
1309  *b;
1310 
1311  /* make sure any unused bits are zeroed */
1312  dst = (inet *) palloc0(sizeof(inet));
1313 
1314  maxbytes = ip_addrsize(ip);
1315  bits = ip_bits(ip);
1316  a = ip_addr(ip);
1317  b = ip_addr(dst);
1318 
1319  for (byte = 0; byte < maxbytes; byte++)
1320  {
1321  if (bits >= 8)
1322  {
1323  mask = 0x00;
1324  bits -= 8;
1325  }
1326  else if (bits == 0)
1327  mask = 0xff;
1328  else
1329  {
1330  mask = 0xff >> bits;
1331  bits = 0;
1332  }
1333 
1334  b[byte] = a[byte] | mask;
1335  }
1336 
1337  ip_family(dst) = ip_family(ip);
1338  ip_bits(dst) = ip_bits(ip);
1339  SET_INET_VARSIZE(dst);
1340 
1341  PG_RETURN_INET_P(dst);
1342 }
#define ip_bits(inetptr)
Definition: inet.h:74
#define ip_family(inetptr)
Definition: inet.h:71
#define ip_addr(inetptr)
Definition: inet.h:77
#define SET_INET_VARSIZE(dst)
Definition: inet.h:86
#define PG_RETURN_INET_P(x)
Definition: inet.h:125
#define ip_addrsize(inetptr)
Definition: inet.h:80
#define PG_GETARG_INET_PP(n)
Definition: inet.h:124
Definition: inet.h:52
#define byte(x, n)
Definition: rijndael.c:68
void * palloc0(Size size)
Definition: mcxt.c:955

◆ network_cmp()

Datum network_cmp ( PG_FUNCTION_ARGS  )

Definition at line 426 of file network.c.

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

Referenced by leftmostvalue_inet().

427 {
428  inet *a1 = PG_GETARG_INET_PP(0);
429  inet *a2 = PG_GETARG_INET_PP(1);
430 
432 }
#define PG_RETURN_INT32(x)
Definition: fmgr.h:344
static const FormData_pg_attribute a2
Definition: heap.c:166
static int32 network_cmp_internal(inet *a1, inet *a2)
Definition: network.c:406
#define PG_GETARG_INET_PP(n)
Definition: inet.h:124
Definition: inet.h:52
static const FormData_pg_attribute a1
Definition: heap.c:152

◆ network_cmp_abbrev()

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

Definition at line 486 of file network.c.

Referenced by network_sortsupport().

487 {
488  if (x > y)
489  return 1;
490  else if (x == y)
491  return 0;
492  else
493  return -1;
494 }

◆ network_cmp_internal()

static int32 network_cmp_internal ( inet a1,
inet a2 
)
static

Definition at line 406 of file network.c.

References 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().

407 {
408  if (ip_family(a1) == ip_family(a2))
409  {
410  int order;
411 
412  order = bitncmp(ip_addr(a1), ip_addr(a2),
413  Min(ip_bits(a1), ip_bits(a2)));
414  if (order != 0)
415  return order;
416  order = ((int) ip_bits(a1)) - ((int) ip_bits(a2));
417  if (order != 0)
418  return order;
419  return bitncmp(ip_addr(a1), ip_addr(a2), ip_maxbits(a1));
420  }
421 
422  return ip_family(a1) - ip_family(a2);
423 }
#define ip_bits(inetptr)
Definition: inet.h:74
#define ip_family(inetptr)
Definition: inet.h:71
#define ip_addr(inetptr)
Definition: inet.h:77
#define Min(x, y)
Definition: c.h:904
#define ip_maxbits(inetptr)
Definition: inet.h:83
int bitncmp(const unsigned char *l, const unsigned char *r, int n)
Definition: network.c:1584

◆ network_eq()

Datum network_eq ( PG_FUNCTION_ARGS  )

Definition at line 829 of file network.c.

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

830 {
831  inet *a1 = PG_GETARG_INET_PP(0);
832  inet *a2 = PG_GETARG_INET_PP(1);
833 
834  PG_RETURN_BOOL(network_cmp_internal(a1, a2) == 0);
835 }
static const FormData_pg_attribute a2
Definition: heap.c:166
static int32 network_cmp_internal(inet *a1, inet *a2)
Definition: network.c:406
#define PG_GETARG_INET_PP(n)
Definition: inet.h:124
Definition: inet.h:52
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
static const FormData_pg_attribute a1
Definition: heap.c:152

◆ network_family()

Datum network_family ( PG_FUNCTION_ARGS  )

Definition at line 1281 of file network.c.

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

1282 {
1283  inet *ip = PG_GETARG_INET_PP(0);
1284 
1285  switch (ip_family(ip))
1286  {
1287  case PGSQL_AF_INET:
1288  PG_RETURN_INT32(4);
1289  break;
1290  case PGSQL_AF_INET6:
1291  PG_RETURN_INT32(6);
1292  break;
1293  default:
1294  PG_RETURN_INT32(0);
1295  break;
1296  }
1297 }
#define ip_family(inetptr)
Definition: inet.h:71
#define PGSQL_AF_INET
Definition: inet.h:39
#define PG_RETURN_INT32(x)
Definition: fmgr.h:344
#define PGSQL_AF_INET6
Definition: inet.h:40
#define PG_GETARG_INET_PP(n)
Definition: inet.h:124
Definition: inet.h:52

◆ network_fast_cmp()

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

Definition at line 474 of file network.c.

References DatumGetInetPP, and network_cmp_internal().

Referenced by network_sortsupport().

475 {
476  inet *arg1 = DatumGetInetPP(x);
477  inet *arg2 = DatumGetInetPP(y);
478 
479  return network_cmp_internal(arg1, arg2);
480 }
#define DatumGetInetPP(X)
Definition: inet.h:122
static int32 network_cmp_internal(inet *a1, inet *a2)
Definition: network.c:406
Definition: inet.h:52

◆ network_ge()

Datum network_ge ( PG_FUNCTION_ARGS  )

Definition at line 838 of file network.c.

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

839 {
840  inet *a1 = PG_GETARG_INET_PP(0);
841  inet *a2 = PG_GETARG_INET_PP(1);
842 
843  PG_RETURN_BOOL(network_cmp_internal(a1, a2) >= 0);
844 }
static const FormData_pg_attribute a2
Definition: heap.c:166
static int32 network_cmp_internal(inet *a1, inet *a2)
Definition: network.c:406
#define PG_GETARG_INET_PP(n)
Definition: inet.h:124
Definition: inet.h:52
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
static const FormData_pg_attribute a1
Definition: heap.c:152

◆ network_gt()

Datum network_gt ( PG_FUNCTION_ARGS  )

Definition at line 847 of file network.c.

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

848 {
849  inet *a1 = PG_GETARG_INET_PP(0);
850  inet *a2 = PG_GETARG_INET_PP(1);
851 
853 }
static const FormData_pg_attribute a2
Definition: heap.c:166
static int32 network_cmp_internal(inet *a1, inet *a2)
Definition: network.c:406
#define PG_GETARG_INET_PP(n)
Definition: inet.h:124
Definition: inet.h:52
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
static const FormData_pg_attribute a1
Definition: heap.c:152

◆ network_host()

Datum network_host ( PG_FUNCTION_ARGS  )

Definition at line 1188 of file network.c.

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

1189 {
1190  inet *ip = PG_GETARG_INET_PP(0);
1191  char *ptr;
1192  char tmp[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255/128")];
1193 
1194  /* force display of max bits, regardless of masklen... */
1195  if (inet_net_ntop(ip_family(ip), ip_addr(ip), ip_maxbits(ip),
1196  tmp, sizeof(tmp)) == NULL)
1197  ereport(ERROR,
1198  (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
1199  errmsg("could not format inet value: %m")));
1200 
1201  /* Suppress /n if present (shouldn't happen now) */
1202  if ((ptr = strchr(tmp, '/')) != NULL)
1203  *ptr = '\0';
1204 
1206 }
#define ip_family(inetptr)
Definition: inet.h:71
#define ip_addr(inetptr)
Definition: inet.h:77
int errcode(int sqlerrcode)
Definition: elog.c:570
#define ip_maxbits(inetptr)
Definition: inet.h:83
#define ERROR
Definition: elog.h:43
#define PG_GETARG_INET_PP(n)
Definition: inet.h:124
Definition: inet.h:52
char * inet_net_ntop(int af, const void *src, int bits, char *dst, size_t size)
Definition: inet_net_ntop.c:77
#define ereport(elevel, rest)
Definition: elog.h:141
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:361
text * cstring_to_text(const char *s)
Definition: varlena.c:171
int errmsg(const char *fmt,...)
Definition: elog.c:784

◆ network_hostmask()

Datum network_hostmask ( PG_FUNCTION_ARGS  )

Definition at line 1431 of file network.c.

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

1432 {
1433  inet *ip = PG_GETARG_INET_PP(0);
1434  inet *dst;
1435  int byte;
1436  int bits;
1437  int maxbytes;
1438  unsigned char mask;
1439  unsigned char *b;
1440 
1441  /* make sure any unused bits are zeroed */
1442  dst = (inet *) palloc0(sizeof(inet));
1443 
1444  maxbytes = ip_addrsize(ip);
1445  bits = ip_maxbits(ip) - ip_bits(ip);
1446  b = ip_addr(dst);
1447 
1448  byte = maxbytes - 1;
1449 
1450  while (bits)
1451  {
1452  if (bits >= 8)
1453  {
1454  mask = 0xff;
1455  bits -= 8;
1456  }
1457  else
1458  {
1459  mask = 0xff >> (8 - bits);
1460  bits = 0;
1461  }
1462 
1463  b[byte] = mask;
1464  byte--;
1465  }
1466 
1467  ip_family(dst) = ip_family(ip);
1468  ip_bits(dst) = ip_maxbits(ip);
1469  SET_INET_VARSIZE(dst);
1470 
1471  PG_RETURN_INET_P(dst);
1472 }
#define ip_bits(inetptr)
Definition: inet.h:74
#define ip_family(inetptr)
Definition: inet.h:71
#define ip_addr(inetptr)
Definition: inet.h:77
#define SET_INET_VARSIZE(dst)
Definition: inet.h:86
#define PG_RETURN_INET_P(x)
Definition: inet.h:125
#define ip_addrsize(inetptr)
Definition: inet.h:80
#define ip_maxbits(inetptr)
Definition: inet.h:83
#define PG_GETARG_INET_PP(n)
Definition: inet.h:124
Definition: inet.h:52
#define byte(x, n)
Definition: rijndael.c:68
void * palloc0(Size size)
Definition: mcxt.c:955

◆ network_in()

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

Definition at line 76 of file network.c.

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

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

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

◆ network_larger()

Datum network_larger ( PG_FUNCTION_ARGS  )

Definition at line 880 of file network.c.

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

881 {
882  inet *a1 = PG_GETARG_INET_PP(0);
883  inet *a2 = PG_GETARG_INET_PP(1);
884 
885  if (network_cmp_internal(a1, a2) > 0)
886  PG_RETURN_INET_P(a1);
887  else
888  PG_RETURN_INET_P(a2);
889 }
#define PG_RETURN_INET_P(x)
Definition: inet.h:125
static const FormData_pg_attribute a2
Definition: heap.c:166
static int32 network_cmp_internal(inet *a1, inet *a2)
Definition: network.c:406
#define PG_GETARG_INET_PP(n)
Definition: inet.h:124
Definition: inet.h:52
static const FormData_pg_attribute a1
Definition: heap.c:152

◆ network_le()

Datum network_le ( PG_FUNCTION_ARGS  )

Definition at line 820 of file network.c.

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

821 {
822  inet *a1 = PG_GETARG_INET_PP(0);
823  inet *a2 = PG_GETARG_INET_PP(1);
824 
825  PG_RETURN_BOOL(network_cmp_internal(a1, a2) <= 0);
826 }
static const FormData_pg_attribute a2
Definition: heap.c:166
static int32 network_cmp_internal(inet *a1, inet *a2)
Definition: network.c:406
#define PG_GETARG_INET_PP(n)
Definition: inet.h:124
Definition: inet.h:52
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
static const FormData_pg_attribute a1
Definition: heap.c:152

◆ network_lt()

Datum network_lt ( PG_FUNCTION_ARGS  )

Definition at line 811 of file network.c.

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

812 {
813  inet *a1 = PG_GETARG_INET_PP(0);
814  inet *a2 = PG_GETARG_INET_PP(1);
815 
817 }
static const FormData_pg_attribute a2
Definition: heap.c:166
static int32 network_cmp_internal(inet *a1, inet *a2)
Definition: network.c:406
#define PG_GETARG_INET_PP(n)
Definition: inet.h:124
Definition: inet.h:52
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
static const FormData_pg_attribute a1
Definition: heap.c:152

◆ network_masklen()

Datum network_masklen ( PG_FUNCTION_ARGS  )

Definition at line 1273 of file network.c.

References ip_bits, PG_GETARG_INET_PP, and PG_RETURN_INT32.

1274 {
1275  inet *ip = PG_GETARG_INET_PP(0);
1276 
1277  PG_RETURN_INT32(ip_bits(ip));
1278 }
#define ip_bits(inetptr)
Definition: inet.h:74
#define PG_RETURN_INT32(x)
Definition: fmgr.h:344
#define PG_GETARG_INET_PP(n)
Definition: inet.h:124
Definition: inet.h:52

◆ network_ne()

Datum network_ne ( PG_FUNCTION_ARGS  )

Definition at line 856 of file network.c.

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

857 {
858  inet *a1 = PG_GETARG_INET_PP(0);
859  inet *a2 = PG_GETARG_INET_PP(1);
860 
861  PG_RETURN_BOOL(network_cmp_internal(a1, a2) != 0);
862 }
static const FormData_pg_attribute a2
Definition: heap.c:166
static int32 network_cmp_internal(inet *a1, inet *a2)
Definition: network.c:406
#define PG_GETARG_INET_PP(n)
Definition: inet.h:124
Definition: inet.h:52
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
static const FormData_pg_attribute a1
Definition: heap.c:152

◆ network_netmask()

Datum network_netmask ( PG_FUNCTION_ARGS  )

Definition at line 1389 of file network.c.

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

1390 {
1391  inet *ip = PG_GETARG_INET_PP(0);
1392  inet *dst;
1393  int byte;
1394  int bits;
1395  unsigned char mask;
1396  unsigned char *b;
1397 
1398  /* make sure any unused bits are zeroed */
1399  dst = (inet *) palloc0(sizeof(inet));
1400 
1401  bits = ip_bits(ip);
1402  b = ip_addr(dst);
1403 
1404  byte = 0;
1405 
1406  while (bits)
1407  {
1408  if (bits >= 8)
1409  {
1410  mask = 0xff;
1411  bits -= 8;
1412  }
1413  else
1414  {
1415  mask = 0xff << (8 - bits);
1416  bits = 0;
1417  }
1418 
1419  b[byte] = mask;
1420  byte++;
1421  }
1422 
1423  ip_family(dst) = ip_family(ip);
1424  ip_bits(dst) = ip_maxbits(ip);
1425  SET_INET_VARSIZE(dst);
1426 
1427  PG_RETURN_INET_P(dst);
1428 }
#define ip_bits(inetptr)
Definition: inet.h:74
#define ip_family(inetptr)
Definition: inet.h:71
#define ip_addr(inetptr)
Definition: inet.h:77
#define SET_INET_VARSIZE(dst)
Definition: inet.h:86
#define PG_RETURN_INET_P(x)
Definition: inet.h:125
#define ip_maxbits(inetptr)
Definition: inet.h:83
#define PG_GETARG_INET_PP(n)
Definition: inet.h:124
Definition: inet.h:52
#define byte(x, n)
Definition: rijndael.c:68
void * palloc0(Size size)
Definition: mcxt.c:955

◆ network_network()

Datum network_network ( PG_FUNCTION_ARGS  )

Definition at line 1345 of file network.c.

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

Referenced by network_scan_first().

1346 {
1347  inet *ip = PG_GETARG_INET_PP(0);
1348  inet *dst;
1349  int byte;
1350  int bits;
1351  unsigned char mask;
1352  unsigned char *a,
1353  *b;
1354 
1355  /* make sure any unused bits are zeroed */
1356  dst = (inet *) palloc0(sizeof(inet));
1357 
1358  bits = ip_bits(ip);
1359  a = ip_addr(ip);
1360  b = ip_addr(dst);
1361 
1362  byte = 0;
1363 
1364  while (bits)
1365  {
1366  if (bits >= 8)
1367  {
1368  mask = 0xff;
1369  bits -= 8;
1370  }
1371  else
1372  {
1373  mask = 0xff << (8 - bits);
1374  bits = 0;
1375  }
1376 
1377  b[byte] = a[byte] & mask;
1378  byte++;
1379  }
1380 
1381  ip_family(dst) = ip_family(ip);
1382  ip_bits(dst) = ip_bits(ip);
1383  SET_INET_VARSIZE(dst);
1384 
1385  PG_RETURN_INET_P(dst);
1386 }
#define ip_bits(inetptr)
Definition: inet.h:74
#define ip_family(inetptr)
Definition: inet.h:71
#define ip_addr(inetptr)
Definition: inet.h:77
#define SET_INET_VARSIZE(dst)
Definition: inet.h:86
#define PG_RETURN_INET_P(x)
Definition: inet.h:125
#define PG_GETARG_INET_PP(n)
Definition: inet.h:124
Definition: inet.h:52
#define byte(x, n)
Definition: rijndael.c:68
void * palloc0(Size size)
Definition: mcxt.c:955

◆ network_out()

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

Definition at line 142 of file network.c.

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

Referenced by cidr_out(), and inet_out().

143 {
144  char tmp[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255/128")];
145  char *dst;
146  int len;
147 
148  dst = inet_net_ntop(ip_family(src), ip_addr(src), ip_bits(src),
149  tmp, sizeof(tmp));
150  if (dst == NULL)
151  ereport(ERROR,
152  (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
153  errmsg("could not format inet value: %m")));
154 
155  /* For CIDR, add /n if not present */
156  if (is_cidr && strchr(tmp, '/') == NULL)
157  {
158  len = strlen(tmp);
159  snprintf(tmp + len, sizeof(tmp) - len, "/%u", ip_bits(src));
160  }
161 
162  return pstrdup(tmp);
163 }
#define ip_bits(inetptr)
Definition: inet.h:74
#define ip_family(inetptr)
Definition: inet.h:71
char * pstrdup(const char *in)
Definition: mcxt.c:1161
#define ip_addr(inetptr)
Definition: inet.h:77
int errcode(int sqlerrcode)
Definition: elog.c:570
#define ERROR
Definition: elog.h:43
char * inet_net_ntop(int af, const void *src, int bits, char *dst, size_t size)
Definition: inet_net_ntop.c:77
#define ereport(elevel, rest)
Definition: elog.h:141
int errmsg(const char *fmt,...)
Definition: elog.c:784
#define snprintf
Definition: port.h:192

◆ network_overlap()

Datum network_overlap ( PG_FUNCTION_ARGS  )

Definition at line 978 of file network.c.

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

979 {
980  inet *a1 = PG_GETARG_INET_PP(0);
981  inet *a2 = PG_GETARG_INET_PP(1);
982 
983  if (ip_family(a1) == ip_family(a2))
984  {
986  Min(ip_bits(a1), ip_bits(a2))) == 0);
987  }
988 
989  PG_RETURN_BOOL(false);
990 }
#define ip_bits(inetptr)
Definition: inet.h:74
#define ip_family(inetptr)
Definition: inet.h:71
#define ip_addr(inetptr)
Definition: inet.h:77
#define Min(x, y)
Definition: c.h:904
static const FormData_pg_attribute a2
Definition: heap.c:166
#define PG_GETARG_INET_PP(n)
Definition: inet.h:124
Definition: inet.h:52
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
int bitncmp(const unsigned char *l, const unsigned char *r, int n)
Definition: network.c:1584
static const FormData_pg_attribute a1
Definition: heap.c:152

◆ network_recv()

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

Definition at line 193 of file network.c.

References addressOK(), 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().

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

◆ network_scan_first()

Datum network_scan_first ( Datum  in)

Definition at line 1705 of file network.c.

References DirectFunctionCall1, and network_network().

Referenced by match_network_subset().

1706 {
1708 }
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:616
Datum network_network(PG_FUNCTION_ARGS)
Definition: network.c:1345

◆ network_scan_last()

Datum network_scan_last ( Datum  in)

Definition at line 1719 of file network.c.

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

Referenced by match_network_subset().

1720 {
1723  Int32GetDatum(-1));
1724 }
Datum network_broadcast(PG_FUNCTION_ARGS)
Definition: network.c:1300
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:616
Datum inet_set_masklen(PG_FUNCTION_ARGS)
Definition: network.c:325
#define Int32GetDatum(X)
Definition: postgres.h:479
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:618

◆ network_send()

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

Definition at line 271 of file network.c.

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().

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

◆ network_show()

Datum network_show ( PG_FUNCTION_ARGS  )

Definition at line 1214 of file network.c.

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

1215 {
1216  inet *ip = PG_GETARG_INET_PP(0);
1217  int len;
1218  char tmp[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255/128")];
1219 
1220  if (inet_net_ntop(ip_family(ip), ip_addr(ip), ip_maxbits(ip),
1221  tmp, sizeof(tmp)) == NULL)
1222  ereport(ERROR,
1223  (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
1224  errmsg("could not format inet value: %m")));
1225 
1226  /* Add /n if not present (which it won't be) */
1227  if (strchr(tmp, '/') == NULL)
1228  {
1229  len = strlen(tmp);
1230  snprintf(tmp + len, sizeof(tmp) - len, "/%u", ip_bits(ip));
1231  }
1232 
1234 }
#define ip_bits(inetptr)
Definition: inet.h:74
#define ip_family(inetptr)
Definition: inet.h:71
#define ip_addr(inetptr)
Definition: inet.h:77
int errcode(int sqlerrcode)
Definition: elog.c:570
#define ip_maxbits(inetptr)
Definition: inet.h:83
#define ERROR
Definition: elog.h:43
#define PG_GETARG_INET_PP(n)
Definition: inet.h:124
Definition: inet.h:52
char * inet_net_ntop(int af, const void *src, int bits, char *dst, size_t size)
Definition: inet_net_ntop.c:77
#define ereport(elevel, rest)
Definition: elog.h:141
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:361
text * cstring_to_text(const char *s)
Definition: varlena.c:171
int errmsg(const char *fmt,...)
Definition: elog.c:784
#define snprintf
Definition: port.h:192

◆ network_smaller()

Datum network_smaller ( PG_FUNCTION_ARGS  )

Definition at line 868 of file network.c.

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

869 {
870  inet *a1 = PG_GETARG_INET_PP(0);
871  inet *a2 = PG_GETARG_INET_PP(1);
872 
873  if (network_cmp_internal(a1, a2) < 0)
874  PG_RETURN_INET_P(a1);
875  else
876  PG_RETURN_INET_P(a2);
877 }
#define PG_RETURN_INET_P(x)
Definition: inet.h:125
static const FormData_pg_attribute a2
Definition: heap.c:166
static int32 network_cmp_internal(inet *a1, inet *a2)
Definition: network.c:406
#define PG_GETARG_INET_PP(n)
Definition: inet.h:124
Definition: inet.h:52
static const FormData_pg_attribute a1
Definition: heap.c:152

◆ network_sortsupport()

Datum network_sortsupport ( PG_FUNCTION_ARGS  )

Definition at line 438 of file network.c.

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_cmp_abbrev(), network_fast_cmp(), palloc(), PG_GETARG_POINTER, PG_RETURN_VOID, SortSupportData::ssup_cxt, and SortSupportData::ssup_extra.

439 {
441 
443  ssup->ssup_extra = NULL;
444 
445  if (ssup->abbreviate)
446  {
448  MemoryContext oldcontext;
449 
450  oldcontext = MemoryContextSwitchTo(ssup->ssup_cxt);
451 
452  uss = palloc(sizeof(network_sortsupport_state));
453  uss->input_count = 0;
454  uss->estimating = true;
455  initHyperLogLog(&uss->abbr_card, 10);
456 
457  ssup->ssup_extra = uss;
458 
463 
464  MemoryContextSwitchTo(oldcontext);
465  }
466 
467  PG_RETURN_VOID();
468 }
struct SortSupportData * SortSupport
Definition: sortsupport.h:58
hyperLogLogState abbr_card
Definition: network.c:51
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:271
void initHyperLogLog(hyperLogLogState *cState, uint8 bwidth)
Definition: hyperloglog.c:65
static int network_cmp_abbrev(Datum x, Datum y, SortSupport ssup)
Definition: network.c:486
MemoryContext ssup_cxt
Definition: sortsupport.h:66
int(* comparator)(Datum x, Datum y, SortSupport ssup)
Definition: sortsupport.h:106
int(* abbrev_full_comparator)(Datum x, Datum y, SortSupport ssup)
Definition: sortsupport.h:191
Datum(* abbrev_converter)(Datum original, SortSupport ssup)
Definition: sortsupport.h:172
void * ssup_extra
Definition: sortsupport.h:87
#define PG_RETURN_VOID()
Definition: fmgr.h:339
bool(* abbrev_abort)(int memtupcount, SortSupport ssup)
Definition: sortsupport.h:182
static Datum network_abbrev_convert(Datum original, SortSupport ssup)
Definition: network.c:640
static bool network_abbrev_abort(int memtupcount, SortSupport ssup)
Definition: network.c:503
void * palloc(Size size)
Definition: mcxt.c:924
static int network_fast_cmp(Datum x, Datum y, SortSupport ssup)
Definition: network.c:474

◆ network_sub()

Datum network_sub ( PG_FUNCTION_ARGS  )

Definition at line 918 of file network.c.

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

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  {
925  PG_RETURN_BOOL(ip_bits(a1) > ip_bits(a2) &&
926  bitncmp(ip_addr(a1), ip_addr(a2), ip_bits(a2)) == 0);
927  }
928 
929  PG_RETURN_BOOL(false);
930 }
#define ip_bits(inetptr)
Definition: inet.h:74
#define ip_family(inetptr)
Definition: inet.h:71
#define ip_addr(inetptr)
Definition: inet.h:77
static const FormData_pg_attribute a2
Definition: heap.c:166
#define PG_GETARG_INET_PP(n)
Definition: inet.h:124
Definition: inet.h:52
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
int bitncmp(const unsigned char *l, const unsigned char *r, int n)
Definition: network.c:1584
static const FormData_pg_attribute a1
Definition: heap.c:152

◆ network_subeq()

Datum network_subeq ( PG_FUNCTION_ARGS  )

Definition at line 933 of file network.c.

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

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  {
940  PG_RETURN_BOOL(ip_bits(a1) >= ip_bits(a2) &&
941  bitncmp(ip_addr(a1), ip_addr(a2), ip_bits(a2)) == 0);
942  }
943 
944  PG_RETURN_BOOL(false);
945 }
#define ip_bits(inetptr)
Definition: inet.h:74
#define ip_family(inetptr)
Definition: inet.h:71
#define ip_addr(inetptr)
Definition: inet.h:77
static const FormData_pg_attribute a2
Definition: heap.c:166
#define PG_GETARG_INET_PP(n)
Definition: inet.h:124
Definition: inet.h:52
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
int bitncmp(const unsigned char *l, const unsigned char *r, int n)
Definition: network.c:1584
static const FormData_pg_attribute a1
Definition: heap.c:152

◆ network_subset_support()

Datum network_subset_support ( PG_FUNCTION_ARGS  )

Definition at line 996 of file network.c.

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.

997 {
998  Node *rawreq = (Node *) PG_GETARG_POINTER(0);
999  Node *ret = NULL;
1000 
1001  if (IsA(rawreq, SupportRequestIndexCondition))
1002  {
1003  /* Try to convert operator/function call to index conditions */
1005 
1006  if (is_opclause(req->node))
1007  {
1008  OpExpr *clause = (OpExpr *) req->node;
1009 
1010  Assert(list_length(clause->args) == 2);
1011  ret = (Node *)
1013  (Node *) lsecond(clause->args),
1014  req->indexarg,
1015  req->funcid,
1016  req->opfamily);
1017  }
1018  else if (is_funcclause(req->node)) /* be paranoid */
1019  {
1020  FuncExpr *clause = (FuncExpr *) req->node;
1021 
1022  Assert(list_length(clause->args) == 2);
1023  ret = (Node *)
1025  (Node *) lsecond(clause->args),
1026  req->indexarg,
1027  req->funcid,
1028  req->opfamily);
1029  }
1030  }
1031 
1032  PG_RETURN_POINTER(ret);
1033 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:351
#define IsA(nodeptr, _type_)
Definition: nodes.h:575
List * args
Definition: primnodes.h:463
Definition: nodes.h:524
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:271
#define lsecond(l)
Definition: pg_list.h:200
static bool is_funcclause(const void *clause)
Definition: nodeFuncs.h:55
#define linitial(l)
Definition: pg_list.h:195
static List * match_network_function(Node *leftop, Node *rightop, int indexarg, Oid funcid, Oid opfamily)
Definition: network.c:1043
#define Assert(condition)
Definition: c.h:732
static int list_length(const List *l)
Definition: pg_list.h:169
static bool is_opclause(const void *clause)
Definition: nodeFuncs.h:62
List * args
Definition: primnodes.h:508

◆ network_sup()

Datum network_sup ( PG_FUNCTION_ARGS  )

Definition at line 948 of file network.c.

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

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  {
955  PG_RETURN_BOOL(ip_bits(a1) < ip_bits(a2) &&
956  bitncmp(ip_addr(a1), ip_addr(a2), ip_bits(a1)) == 0);
957  }
958 
959  PG_RETURN_BOOL(false);
960 }
#define ip_bits(inetptr)
Definition: inet.h:74
#define ip_family(inetptr)
Definition: inet.h:71
#define ip_addr(inetptr)
Definition: inet.h:77
static const FormData_pg_attribute a2
Definition: heap.c:166
#define PG_GETARG_INET_PP(n)
Definition: inet.h:124
Definition: inet.h:52
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
int bitncmp(const unsigned char *l, const unsigned char *r, int n)
Definition: network.c:1584
static const FormData_pg_attribute a1
Definition: heap.c:152

◆ network_supeq()

Datum network_supeq ( PG_FUNCTION_ARGS  )

Definition at line 963 of file network.c.

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

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  {
970  PG_RETURN_BOOL(ip_bits(a1) <= ip_bits(a2) &&
971  bitncmp(ip_addr(a1), ip_addr(a2), ip_bits(a1)) == 0);
972  }
973 
974  PG_RETURN_BOOL(false);
975 }
#define ip_bits(inetptr)
Definition: inet.h:74
#define ip_family(inetptr)
Definition: inet.h:71
#define ip_addr(inetptr)
Definition: inet.h:77
static const FormData_pg_attribute a2
Definition: heap.c:166
#define PG_GETARG_INET_PP(n)
Definition: inet.h:124
Definition: inet.h:52
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
int bitncmp(const unsigned char *l, const unsigned char *r, int n)
Definition: network.c:1584
static const FormData_pg_attribute a1
Definition: heap.c:152