Thanks to visit codestin.com
Credit goes to doxygen.postgresql.org

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 "catalog/pg_type.h"
#include "common/hashfn.h"
#include "common/ip.h"
#include "lib/hyperloglog.h"
#include "libpq/libpq-be.h"
#include "libpq/pqformat.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "nodes/supportnodes.h"
#include "utils/builtins.h"
#include "utils/fmgroids.h"
#include "utils/guc.h"
#include "utils/inet.h"
#include "utils/lsyscache.h"
#include "utils/sortsupport.h"
Include dependency graph for network.c:

Go to the source code of this file.

Data Structures

struct  network_sortsupport_state
 

Macros

#define ABBREV_BITS_INET4_NETMASK_SIZE   6
 
#define ABBREV_BITS_INET4_SUBNET   25
 

Functions

static int32 network_cmp_internal (inet *a1, inet *a2)
 
static int network_fast_cmp (Datum x, Datum y, SortSupport ssup)
 
static bool network_abbrev_abort (int memtupcount, SortSupport ssup)
 
static Datum network_abbrev_convert (Datum original, SortSupport ssup)
 
static Listmatch_network_function (Node *leftop, Node *rightop, int indexarg, Oid funcid, Oid opfamily)
 
static Listmatch_network_subset (Node *leftop, Node *rightop, bool is_eq, Oid opfamily)
 
static bool addressOK (unsigned char *a, int bits, int family)
 
static inetinternal_inetpl (inet *ip, int64 addend)
 
static inetnetwork_in (char *src, bool is_cidr, Node *escontext)
 
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 40 of file network.c.

◆ ABBREV_BITS_INET4_SUBNET

#define ABBREV_BITS_INET4_SUBNET   25

Definition at line 41 of file network.c.

Function Documentation

◆ addressOK()

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

Definition at line 1574 of file network.c.

1575{
1576 int byte;
1577 int nbits;
1578 int maxbits;
1579 int maxbytes;
1580 unsigned char mask;
1581
1582 if (family == PGSQL_AF_INET)
1583 {
1584 maxbits = 32;
1585 maxbytes = 4;
1586 }
1587 else
1588 {
1589 maxbits = 128;
1590 maxbytes = 16;
1591 }
1592 Assert(bits <= maxbits);
1593
1594 if (bits == maxbits)
1595 return true;
1596
1597 byte = bits / 8;
1598
1599 nbits = bits % 8;
1600 mask = 0xff;
1601 if (bits != 0)
1602 mask >>= nbits;
1603
1604 while (byte < maxbytes)
1605 {
1606 if ((a[byte] & mask) != 0)
1607 return false;
1608 mask = 0xff;
1609 byte++;
1610 }
1611
1612 return true;
1613}
Assert(PointerIsAligned(start, uint64))
int a
Definition: isn.c:73
#define PGSQL_AF_INET
Definition: inet.h:39

References a, Assert(), and PGSQL_AF_INET.

Referenced by network_in(), and network_recv().

◆ bitncmp()

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

Definition at line 1502 of file network.c.

1503{
1504 unsigned int lb,
1505 rb;
1506 int x,
1507 b;
1508
1509 b = n / 8;
1510 x = memcmp(l, r, b);
1511 if (x || (n % 8) == 0)
1512 return x;
1513
1514 lb = l[b];
1515 rb = r[b];
1516 for (b = n % 8; b > 0; b--)
1517 {
1518 if (IS_HIGHBIT_SET(lb) != IS_HIGHBIT_SET(rb))
1519 {
1520 if (IS_HIGHBIT_SET(lb))
1521 return 1;
1522 return -1;
1523 }
1524 lb <<= 1;
1525 rb <<= 1;
1526 }
1527 return 0;
1528}
#define IS_HIGHBIT_SET(ch)
Definition: c.h:1155
int b
Definition: isn.c:74
int x
Definition: isn.c:75

References b, IS_HIGHBIT_SET, and x.

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

◆ bitncommon()

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

Definition at line 1536 of file network.c.

1537{
1538 int byte,
1539 nbits;
1540
1541 /* number of bits to examine in last byte */
1542 nbits = n % 8;
1543
1544 /* check whole bytes */
1545 for (byte = 0; byte < n / 8; byte++)
1546 {
1547 if (l[byte] != r[byte])
1548 {
1549 /* at least one bit in the last byte is not common */
1550 nbits = 7;
1551 break;
1552 }
1553 }
1554
1555 /* check bits in last partial byte */
1556 if (nbits != 0)
1557 {
1558 /* calculate diff of first non-matching bytes */
1559 unsigned int diff = l[byte] ^ r[byte];
1560
1561 /* compare the bits from the most to the least */
1562 while ((diff >> (8 - nbits)) != 0)
1563 nbits--;
1564 }
1565
1566 return (8 * byte) + nbits;
1567}

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

◆ cidr_abbrev()

Datum cidr_abbrev ( PG_FUNCTION_ARGS  )

Definition at line 1173 of file network.c.

1174{
1175 inet *ip = PG_GETARG_INET_PP(0);
1176 char *dst;
1177 char tmp[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255/128")];
1178
1179 dst = pg_inet_cidr_ntop(ip_family(ip), ip_addr(ip),
1180 ip_bits(ip), tmp, sizeof(tmp));
1181
1182 if (dst == NULL)
1183 ereport(ERROR,
1184 (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
1185 errmsg("could not format cidr value: %m")));
1186
1188}
int errcode(int sqlerrcode)
Definition: elog.c:854
int errmsg(const char *fmt,...)
Definition: elog.c:1071
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:150
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
char * pg_inet_cidr_ntop(int af, const void *src, int bits, char *dst, size_t size)
Definition: inet.h:53
#define ip_addr(inetptr)
Definition: inet.h:77
#define PG_GETARG_INET_PP(n)
Definition: inet.h:134
#define ip_family(inetptr)
Definition: inet.h:71
#define ip_bits(inetptr)
Definition: inet.h:74
text * cstring_to_text(const char *s)
Definition: varlena.c:181

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

◆ cidr_in()

Datum cidr_in ( PG_FUNCTION_ARGS  )

Definition at line 127 of file network.c.

128{
129 char *src = PG_GETARG_CSTRING(0);
130
131 PG_RETURN_INET_P(network_in(src, true, fcinfo->context));
132}
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:277
static inet * network_in(char *src, bool is_cidr, Node *escontext)
Definition: network.c:73
#define PG_RETURN_INET_P(x)
Definition: inet.h:135

References network_in(), PG_GETARG_CSTRING, and PG_RETURN_INET_P.

◆ cidr_out()

Datum cidr_out ( PG_FUNCTION_ARGS  )

Definition at line 171 of file network.c.

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

References network_out(), PG_GETARG_INET_PP, and PG_RETURN_CSTRING.

◆ cidr_recv()

Datum cidr_recv ( PG_FUNCTION_ARGS  )

Definition at line 256 of file network.c.

257{
259
261}
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
static inet * network_recv(StringInfo buf, bool is_cidr)
Definition: network.c:190
static char * buf
Definition: pg_test_fsync.c:72
struct StringInfoData * StringInfo
Definition: string.h:15

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

◆ cidr_send()

Datum cidr_send ( PG_FUNCTION_ARGS  )

Definition at line 296 of file network.c.

297{
298 inet *addr = PG_GETARG_INET_PP(0);
299
300 PG_RETURN_BYTEA_P(network_send(addr, true));
301}
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:371
static bytea * network_send(inet *addr, bool is_cidr)
Definition: network.c:268

References network_send(), PG_GETARG_INET_PP, and PG_RETURN_BYTEA_P.

◆ cidr_set_masklen()

Datum cidr_set_masklen ( PG_FUNCTION_ARGS  )

Definition at line 344 of file network.c.

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

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

◆ cidr_set_masklen_internal()

inet * cidr_set_masklen_internal ( const inet src,
int  bits 
)

Definition at line 364 of file network.c.

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

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

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

◆ clean_ipv6_addr()

void clean_ipv6_addr ( int  addr_family,
char *  addr 
)

Definition at line 2028 of file network.c.

2029{
2030 if (addr_family == AF_INET6)
2031 {
2032 char *pct = strchr(addr, '%');
2033
2034 if (pct)
2035 *pct = '\0';
2036 }
2037}

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

◆ convert_network_to_scalar()

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

Definition at line 1435 of file network.c.

1436{
1437 switch (typid)
1438 {
1439 case INETOID:
1440 case CIDROID:
1441 {
1442 inet *ip = DatumGetInetPP(value);
1443 int len;
1444 double res;
1445 int i;
1446
1447 /*
1448 * Note that we don't use the full address for IPv6.
1449 */
1450 if (ip_family(ip) == PGSQL_AF_INET)
1451 len = 4;
1452 else
1453 len = 5;
1454
1455 res = ip_family(ip);
1456 for (i = 0; i < len; i++)
1457 {
1458 res *= 256;
1459 res += ip_addr(ip)[i];
1460 }
1461 return res;
1462 }
1463 case MACADDROID:
1464 {
1466 double res;
1467
1468 res = (mac->a << 16) | (mac->b << 8) | (mac->c);
1469 res *= 256 * 256 * 256;
1470 res += (mac->d << 16) | (mac->e << 8) | (mac->f);
1471 return res;
1472 }
1473 case MACADDR8OID:
1474 {
1476 double res;
1477
1478 res = (mac->a << 24) | (mac->b << 16) | (mac->c << 8) | (mac->d);
1479 res *= ((double) 256) * 256 * 256 * 256;
1480 res += (mac->e << 24) | (mac->f << 16) | (mac->g << 8) | (mac->h);
1481 return res;
1482 }
1483 }
1484
1485 *failure = true;
1486 return 0;
1487}
static struct @166 value
int i
Definition: isn.c:77
const void size_t len
Definition: inet.h:108
unsigned char c
Definition: inet.h:111
unsigned char b
Definition: inet.h:110
unsigned char d
Definition: inet.h:112
unsigned char e
Definition: inet.h:113
unsigned char g
Definition: inet.h:115
unsigned char h
Definition: inet.h:116
unsigned char a
Definition: inet.h:109
unsigned char f
Definition: inet.h:114
Definition: inet.h:95
unsigned char e
Definition: inet.h:100
unsigned char b
Definition: inet.h:97
unsigned char f
Definition: inet.h:101
unsigned char c
Definition: inet.h:98
unsigned char a
Definition: inet.h:96
unsigned char d
Definition: inet.h:99
static macaddr8 * DatumGetMacaddr8P(Datum X)
Definition: inet.h:163
static inet * DatumGetInetPP(Datum X)
Definition: inet.h:123
static macaddr * DatumGetMacaddrP(Datum X)
Definition: inet.h:147

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

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

◆ hashinet()

Datum hashinet ( PG_FUNCTION_ARGS  )

Definition at line 840 of file network.c.

841{
842 inet *addr = PG_GETARG_INET_PP(0);
843 int addrsize = ip_addrsize(addr);
844
845 /* XXX this assumes there are no pad bytes in the data structure */
846 return hash_any((unsigned char *) VARDATA_ANY(addr), addrsize + 2);
847}
static Datum hash_any(const unsigned char *k, int keylen)
Definition: hashfn.h:31
#define ip_addrsize(inetptr)
Definition: inet.h:80
static char * VARDATA_ANY(const void *PTR)
Definition: varatt.h:486

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

◆ hashinetextended()

Datum hashinetextended ( PG_FUNCTION_ARGS  )

Definition at line 850 of file network.c.

851{
852 inet *addr = PG_GETARG_INET_PP(0);
853 int addrsize = ip_addrsize(addr);
854
855 return hash_any_extended((unsigned char *) VARDATA_ANY(addr), addrsize + 2,
856 PG_GETARG_INT64(1));
857}
#define PG_GETARG_INT64(n)
Definition: fmgr.h:283
static Datum hash_any_extended(const unsigned char *k, int keylen, uint64 seed)
Definition: hashfn.h:37

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

◆ inet_abbrev()

Datum inet_abbrev ( PG_FUNCTION_ARGS  )

Definition at line 1155 of file network.c.

1156{
1157 inet *ip = PG_GETARG_INET_PP(0);
1158 char *dst;
1159 char tmp[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255/128")];
1160
1161 dst = pg_inet_net_ntop(ip_family(ip), ip_addr(ip),
1162 ip_bits(ip), tmp, sizeof(tmp));
1163
1164 if (dst == NULL)
1165 ereport(ERROR,
1166 (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
1167 errmsg("could not format inet value: %m")));
1168
1170}
char * pg_inet_net_ntop(int af, const void *src, int bits, char *dst, size_t size)
Definition: inet_net_ntop.c:77

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

◆ inet_client_addr()

Datum inet_client_addr ( PG_FUNCTION_ARGS  )

Definition at line 1649 of file network.c.

1650{
1651 Port *port = MyProcPort;
1652 char remote_host[NI_MAXHOST];
1653 int ret;
1654
1655 if (port == NULL)
1657
1658 switch (port->raddr.addr.ss_family)
1659 {
1660 case AF_INET:
1661 case AF_INET6:
1662 break;
1663 default:
1665 }
1666
1667 remote_host[0] = '\0';
1668
1669 ret = pg_getnameinfo_all(&port->raddr.addr, port->raddr.salen,
1670 remote_host, sizeof(remote_host),
1671 NULL, 0,
1672 NI_NUMERICHOST | NI_NUMERICSERV);
1673 if (ret != 0)
1675
1676 clean_ipv6_addr(port->raddr.addr.ss_family, remote_host);
1677
1678 PG_RETURN_INET_P(network_in(remote_host, false, NULL));
1679}
#define PG_RETURN_NULL()
Definition: fmgr.h:345
struct Port * MyProcPort
Definition: globals.c:51
int pg_getnameinfo_all(const struct sockaddr_storage *addr, int salen, char *node, int nodelen, char *service, int servicelen, int flags)
Definition: ip.c:114
void clean_ipv6_addr(int addr_family, char *addr)
Definition: network.c:2028
static int port
Definition: pg_regress.c:115
Definition: libpq-be.h:129

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

◆ inet_client_port()

Datum inet_client_port ( PG_FUNCTION_ARGS  )

Definition at line 1686 of file network.c.

1687{
1688 Port *port = MyProcPort;
1689 char remote_port[NI_MAXSERV];
1690 int ret;
1691
1692 if (port == NULL)
1694
1695 switch (port->raddr.addr.ss_family)
1696 {
1697 case AF_INET:
1698 case AF_INET6:
1699 break;
1700 default:
1702 }
1703
1704 remote_port[0] = '\0';
1705
1706 ret = pg_getnameinfo_all(&port->raddr.addr, port->raddr.salen,
1707 NULL, 0,
1708 remote_port, sizeof(remote_port),
1709 NI_NUMERICHOST | NI_NUMERICSERV);
1710 if (ret != 0)
1712
1714}
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:682
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:353
Datum int4in(PG_FUNCTION_ARGS)
Definition: int.c:287
static Datum CStringGetDatum(const char *X)
Definition: postgres.h:360

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

◆ inet_in()

Datum inet_in ( PG_FUNCTION_ARGS  )

Definition at line 119 of file network.c.

120{
121 char *src = PG_GETARG_CSTRING(0);
122
123 PG_RETURN_INET_P(network_in(src, false, fcinfo->context));
124}

References network_in(), PG_GETARG_CSTRING, and PG_RETURN_INET_P.

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

◆ inet_merge()

Datum inet_merge ( PG_FUNCTION_ARGS  )

Definition at line 1409 of file network.c.

1410{
1412 *a2 = PG_GETARG_INET_PP(1);
1413 int commonbits;
1414
1415 if (ip_family(a1) != ip_family(a2))
1416 ereport(ERROR,
1417 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1418 errmsg("cannot merge addresses from different families")));
1419
1420 commonbits = bitncommon(ip_addr(a1), ip_addr(a2),
1421 Min(ip_bits(a1), ip_bits(a2)));
1422
1424}
#define Min(x, y)
Definition: c.h:1004
static const FormData_pg_attribute a1
Definition: heap.c:144
static const FormData_pg_attribute a2
Definition: heap.c:157
int bitncommon(const unsigned char *l, const unsigned char *r, int n)
Definition: network.c:1536

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

◆ inet_out()

Datum inet_out ( PG_FUNCTION_ARGS  )

Definition at line 163 of file network.c.

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

References network_out(), PG_GETARG_INET_PP, and PG_RETURN_CSTRING.

◆ inet_recv()

Datum inet_recv ( PG_FUNCTION_ARGS  )

Definition at line 248 of file network.c.

249{
251
253}

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

◆ inet_same_family()

Datum inet_same_family ( PG_FUNCTION_ARGS  )

Definition at line 1397 of file network.c.

1398{
1401
1403}
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359

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

◆ inet_send()

Datum inet_send ( PG_FUNCTION_ARGS  )

Definition at line 288 of file network.c.

289{
290 inet *addr = PG_GETARG_INET_PP(0);
291
292 PG_RETURN_BYTEA_P(network_send(addr, false));
293}

References network_send(), PG_GETARG_INET_PP, and PG_RETURN_BYTEA_P.

◆ inet_server_addr()

Datum inet_server_addr ( PG_FUNCTION_ARGS  )

Definition at line 1721 of file network.c.

1722{
1723 Port *port = MyProcPort;
1724 char local_host[NI_MAXHOST];
1725 int ret;
1726
1727 if (port == NULL)
1729
1730 switch (port->laddr.addr.ss_family)
1731 {
1732 case AF_INET:
1733 case AF_INET6:
1734 break;
1735 default:
1737 }
1738
1739 local_host[0] = '\0';
1740
1741 ret = pg_getnameinfo_all(&port->laddr.addr, port->laddr.salen,
1742 local_host, sizeof(local_host),
1743 NULL, 0,
1744 NI_NUMERICHOST | NI_NUMERICSERV);
1745 if (ret != 0)
1747
1748 clean_ipv6_addr(port->laddr.addr.ss_family, local_host);
1749
1750 PG_RETURN_INET_P(network_in(local_host, false, NULL));
1751}

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

◆ inet_server_port()

Datum inet_server_port ( PG_FUNCTION_ARGS  )

Definition at line 1758 of file network.c.

1759{
1760 Port *port = MyProcPort;
1761 char local_port[NI_MAXSERV];
1762 int ret;
1763
1764 if (port == NULL)
1766
1767 switch (port->laddr.addr.ss_family)
1768 {
1769 case AF_INET:
1770 case AF_INET6:
1771 break;
1772 default:
1774 }
1775
1776 local_port[0] = '\0';
1777
1778 ret = pg_getnameinfo_all(&port->laddr.addr, port->laddr.salen,
1779 NULL, 0,
1780 local_port, sizeof(local_port),
1781 NI_NUMERICHOST | NI_NUMERICSERV);
1782 if (ret != 0)
1784
1786}

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

◆ inet_set_masklen()

Datum inet_set_masklen ( PG_FUNCTION_ARGS  )

Definition at line 320 of file network.c.

321{
322 inet *src = PG_GETARG_INET_PP(0);
323 int bits = PG_GETARG_INT32(1);
324 inet *dst;
325
326 if (bits == -1)
327 bits = ip_maxbits(src);
328
329 if ((bits < 0) || (bits > ip_maxbits(src)))
331 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
332 errmsg("invalid mask length: %d", bits)));
333
334 /* clone the original data */
335 dst = (inet *) palloc(VARSIZE_ANY(src));
336 memcpy(dst, src, VARSIZE_ANY(src));
337
338 ip_bits(dst) = bits;
339
340 PG_RETURN_INET_P(dst);
341}
void * palloc(Size size)
Definition: mcxt.c:1365
static Size VARSIZE_ANY(const void *PTR)
Definition: varatt.h:460

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

Referenced by network_scan_last().

◆ inet_to_cidr()

Datum inet_to_cidr ( PG_FUNCTION_ARGS  )

Definition at line 305 of file network.c.

306{
307 inet *src = PG_GETARG_INET_PP(0);
308 int bits;
309
310 bits = ip_bits(src);
311
312 /* safety check */
313 if ((bits < 0) || (bits > ip_maxbits(src)))
314 elog(ERROR, "invalid inet bit length: %d", bits);
315
317}
#define elog(elevel,...)
Definition: elog.h:226

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

◆ inetand()

Datum inetand ( PG_FUNCTION_ARGS  )

Definition at line 1815 of file network.c.

1816{
1817 inet *ip = PG_GETARG_INET_PP(0);
1818 inet *ip2 = PG_GETARG_INET_PP(1);
1819 inet *dst;
1820
1821 dst = (inet *) palloc0(sizeof(inet));
1822
1823 if (ip_family(ip) != ip_family(ip2))
1824 ereport(ERROR,
1825 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1826 errmsg("cannot AND inet values of different sizes")));
1827 else
1828 {
1829 int nb = ip_addrsize(ip);
1830 unsigned char *pip = ip_addr(ip);
1831 unsigned char *pip2 = ip_addr(ip2);
1832 unsigned char *pdst = ip_addr(dst);
1833
1834 while (--nb >= 0)
1835 pdst[nb] = pip[nb] & pip2[nb];
1836 }
1837 ip_bits(dst) = Max(ip_bits(ip), ip_bits(ip2));
1838
1839 ip_family(dst) = ip_family(ip);
1840 SET_INET_VARSIZE(dst);
1841
1842 PG_RETURN_INET_P(dst);
1843}
#define Max(x, y)
Definition: c.h:998

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

◆ inetmi()

Datum inetmi ( PG_FUNCTION_ARGS  )

Definition at line 1951 of file network.c.

1952{
1953 inet *ip = PG_GETARG_INET_PP(0);
1954 inet *ip2 = PG_GETARG_INET_PP(1);
1955 int64 res = 0;
1956
1957 if (ip_family(ip) != ip_family(ip2))
1958 ereport(ERROR,
1959 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1960 errmsg("cannot subtract inet values of different sizes")));
1961 else
1962 {
1963 /*
1964 * We form the difference using the traditional complement, increment,
1965 * and add rule, with the increment part being handled by starting the
1966 * carry off at 1. If you don't think integer arithmetic is done in
1967 * two's complement, too bad.
1968 */
1969 int nb = ip_addrsize(ip);
1970 int byte = 0;
1971 unsigned char *pip = ip_addr(ip);
1972 unsigned char *pip2 = ip_addr(ip2);
1973 int carry = 1;
1974
1975 while (--nb >= 0)
1976 {
1977 int lobyte;
1978
1979 carry = pip[nb] + (~pip2[nb] & 0xFF) + carry;
1980 lobyte = carry & 0xFF;
1981 if (byte < sizeof(int64))
1982 {
1983 res |= ((int64) lobyte) << (byte * 8);
1984 }
1985 else
1986 {
1987 /*
1988 * Input wider than int64: check for overflow. All bytes to
1989 * the left of what will fit should be 0 or 0xFF, depending on
1990 * sign of the now-complete result.
1991 */
1992 if ((res < 0) ? (lobyte != 0xFF) : (lobyte != 0))
1993 ereport(ERROR,
1994 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1995 errmsg("result is out of range")));
1996 }
1997 carry >>= 8;
1998 byte++;
1999 }
2000
2001 /*
2002 * If input is narrower than int64, overflow is not possible, but we
2003 * have to do proper sign extension.
2004 */
2005 if (carry == 0 && byte < sizeof(int64))
2006 res |= ((uint64) (int64) -1) << (byte * 8);
2007 }
2008
2009 PG_RETURN_INT64(res);
2010}
int64_t int64
Definition: c.h:536
uint64_t uint64
Definition: c.h:540
#define PG_RETURN_INT64(x)
Definition: fmgr.h:368

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

◆ inetmi_int8()

Datum inetmi_int8 ( PG_FUNCTION_ARGS  )

Definition at line 1941 of file network.c.

1942{
1943 inet *ip = PG_GETARG_INET_PP(0);
1944 int64 addend = PG_GETARG_INT64(1);
1945
1946 PG_RETURN_INET_P(internal_inetpl(ip, -addend));
1947}
static inet * internal_inetpl(inet *ip, int64 addend)
Definition: network.c:1879

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

◆ inetnot()

Datum inetnot ( PG_FUNCTION_ARGS  )

Definition at line 1790 of file network.c.

1791{
1792 inet *ip = PG_GETARG_INET_PP(0);
1793 inet *dst;
1794
1795 dst = (inet *) palloc0(sizeof(inet));
1796
1797 {
1798 int nb = ip_addrsize(ip);
1799 unsigned char *pip = ip_addr(ip);
1800 unsigned char *pdst = ip_addr(dst);
1801
1802 while (--nb >= 0)
1803 pdst[nb] = ~pip[nb];
1804 }
1805 ip_bits(dst) = ip_bits(ip);
1806
1807 ip_family(dst) = ip_family(ip);
1808 SET_INET_VARSIZE(dst);
1809
1810 PG_RETURN_INET_P(dst);
1811}

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

◆ inetor()

Datum inetor ( PG_FUNCTION_ARGS  )

Definition at line 1847 of file network.c.

1848{
1849 inet *ip = PG_GETARG_INET_PP(0);
1850 inet *ip2 = PG_GETARG_INET_PP(1);
1851 inet *dst;
1852
1853 dst = (inet *) palloc0(sizeof(inet));
1854
1855 if (ip_family(ip) != ip_family(ip2))
1856 ereport(ERROR,
1857 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1858 errmsg("cannot OR inet values of different sizes")));
1859 else
1860 {
1861 int nb = ip_addrsize(ip);
1862 unsigned char *pip = ip_addr(ip);
1863 unsigned char *pip2 = ip_addr(ip2);
1864 unsigned char *pdst = ip_addr(dst);
1865
1866 while (--nb >= 0)
1867 pdst[nb] = pip[nb] | pip2[nb];
1868 }
1869 ip_bits(dst) = Max(ip_bits(ip), ip_bits(ip2));
1870
1871 ip_family(dst) = ip_family(ip);
1872 SET_INET_VARSIZE(dst);
1873
1874 PG_RETURN_INET_P(dst);
1875}

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

◆ inetpl()

Datum inetpl ( PG_FUNCTION_ARGS  )

Definition at line 1931 of file network.c.

1932{
1933 inet *ip = PG_GETARG_INET_PP(0);
1934 int64 addend = PG_GETARG_INT64(1);
1935
1936 PG_RETURN_INET_P(internal_inetpl(ip, addend));
1937}

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

◆ internal_inetpl()

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

Definition at line 1879 of file network.c.

1880{
1881 inet *dst;
1882
1883 dst = (inet *) palloc0(sizeof(inet));
1884
1885 {
1886 int nb = ip_addrsize(ip);
1887 unsigned char *pip = ip_addr(ip);
1888 unsigned char *pdst = ip_addr(dst);
1889 int carry = 0;
1890
1891 while (--nb >= 0)
1892 {
1893 carry = pip[nb] + (int) (addend & 0xFF) + carry;
1894 pdst[nb] = (unsigned char) (carry & 0xFF);
1895 carry >>= 8;
1896
1897 /*
1898 * We have to be careful about right-shifting addend because
1899 * right-shift isn't portable for negative values, while simply
1900 * dividing by 256 doesn't work (the standard rounding is in the
1901 * wrong direction, besides which there may be machines out there
1902 * that round the wrong way). So, explicitly clear the low-order
1903 * byte to remove any doubt about the correct result of the
1904 * division, and then divide rather than shift.
1905 */
1906 addend &= ~((int64) 0xFF);
1907 addend /= 0x100;
1908 }
1909
1910 /*
1911 * At this point we should have addend and carry both zero if original
1912 * addend was >= 0, or addend -1 and carry 1 if original addend was <
1913 * 0. Anything else means overflow.
1914 */
1915 if (!((addend == 0 && carry == 0) ||
1916 (addend == -1 && carry == 1)))
1917 ereport(ERROR,
1918 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1919 errmsg("result is out of range")));
1920 }
1921
1922 ip_bits(dst) = ip_bits(ip);
1923 ip_family(dst) = ip_family(ip);
1924 SET_INET_VARSIZE(dst);
1925
1926 return dst;
1927}

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

Referenced by inetmi_int8(), and inetpl().

◆ match_network_function()

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

Definition at line 988 of file network.c.

993{
994 switch (funcid)
995 {
996 case F_NETWORK_SUB:
997 /* indexkey must be on the left */
998 if (indexarg != 0)
999 return NIL;
1000 return match_network_subset(leftop, rightop, false, opfamily);
1001
1002 case F_NETWORK_SUBEQ:
1003 /* indexkey must be on the left */
1004 if (indexarg != 0)
1005 return NIL;
1006 return match_network_subset(leftop, rightop, true, opfamily);
1007
1008 case F_NETWORK_SUP:
1009 /* indexkey must be on the right */
1010 if (indexarg != 1)
1011 return NIL;
1012 return match_network_subset(rightop, leftop, false, opfamily);
1013
1014 case F_NETWORK_SUPEQ:
1015 /* indexkey must be on the right */
1016 if (indexarg != 1)
1017 return NIL;
1018 return match_network_subset(rightop, leftop, true, opfamily);
1019
1020 default:
1021
1022 /*
1023 * We'd only get here if somebody attached this support function
1024 * to an unexpected function. Maybe we should complain, but for
1025 * now, do nothing.
1026 */
1027 return NIL;
1028 }
1029}
static List * match_network_subset(Node *leftop, Node *rightop, bool is_eq, Oid opfamily)
Definition: network.c:1036
#define NIL
Definition: pg_list.h:68

References match_network_subset(), and NIL.

Referenced by network_subset_support().

◆ match_network_subset()

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

Definition at line 1036 of file network.c.

1040{
1041 List *result;
1042 Datum rightopval;
1043 Oid datatype = INETOID;
1044 Oid opr1oid;
1045 Oid opr2oid;
1046 Datum opr1right;
1047 Datum opr2right;
1048 Expr *expr;
1049
1050 /*
1051 * Can't do anything with a non-constant or NULL comparison value.
1052 *
1053 * Note that since we restrict ourselves to cases with a hard constant on
1054 * the RHS, it's a-fortiori a pseudoconstant, and we don't need to worry
1055 * about verifying that.
1056 */
1057 if (!IsA(rightop, Const) ||
1058 ((Const *) rightop)->constisnull)
1059 return NIL;
1060 rightopval = ((Const *) rightop)->constvalue;
1061
1062 /*
1063 * create clause "key >= network_scan_first( rightopval )", or ">" if the
1064 * operator disallows equality.
1065 */
1066 opr1oid = get_opfamily_member_for_cmptype(opfamily, datatype, datatype, is_eq ? COMPARE_GE : COMPARE_GT);
1067 if (opr1oid == InvalidOid)
1068 return NIL;
1069
1070 opr1right = network_scan_first(rightopval);
1071
1072 expr = make_opclause(opr1oid, BOOLOID, false,
1073 (Expr *) leftop,
1074 (Expr *) makeConst(datatype, -1,
1075 InvalidOid, /* not collatable */
1076 -1, opr1right,
1077 false, false),
1079 result = list_make1(expr);
1080
1081 /* create clause "key <= network_scan_last( rightopval )" */
1082
1083 opr2oid = get_opfamily_member_for_cmptype(opfamily, datatype, datatype, COMPARE_LE);
1084 if (opr2oid == InvalidOid)
1085 return NIL;
1086
1087 opr2right = network_scan_last(rightopval);
1088
1089 expr = make_opclause(opr2oid, BOOLOID, false,
1090 (Expr *) leftop,
1091 (Expr *) makeConst(datatype, -1,
1092 InvalidOid, /* not collatable */
1093 -1, opr2right,
1094 false, false),
1096 result = lappend(result, expr);
1097
1098 return result;
1099}
@ COMPARE_LE
Definition: cmptype.h:35
@ COMPARE_GT
Definition: cmptype.h:38
@ COMPARE_GE
Definition: cmptype.h:37
List * lappend(List *list, void *datum)
Definition: list.c:339
Oid get_opfamily_member_for_cmptype(Oid opfamily, Oid lefttype, Oid righttype, CompareType cmptype)
Definition: lsyscache.c:197
Expr * make_opclause(Oid opno, Oid opresulttype, bool opretset, Expr *leftop, Expr *rightop, Oid opcollid, Oid inputcollid)
Definition: makefuncs.c:701
Const * makeConst(Oid consttype, int32 consttypmod, Oid constcollid, int constlen, Datum constvalue, bool constisnull, bool constbyval)
Definition: makefuncs.c:350
Datum network_scan_last(Datum in)
Definition: network.c:1637
Datum network_scan_first(Datum in)
Definition: network.c:1623
#define IsA(nodeptr, _type_)
Definition: nodes.h:164
#define list_make1(x1)
Definition: pg_list.h:212
uint64_t Datum
Definition: postgres.h:70
#define InvalidOid
Definition: postgres_ext.h:37
unsigned int Oid
Definition: postgres_ext.h:32
Definition: pg_list.h:54

References COMPARE_GE, COMPARE_GT, COMPARE_LE, get_opfamily_member_for_cmptype(), InvalidOid, IsA, lappend(), list_make1, make_opclause(), makeConst(), network_scan_first(), network_scan_last(), and NIL.

Referenced by match_network_function().

◆ network_abbrev_abort()

static bool network_abbrev_abort ( int  memtupcount,
SortSupport  ssup 
)
static

Definition at line 484 of file network.c.

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

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

Referenced by network_sortsupport().

◆ network_abbrev_convert()

static Datum network_abbrev_convert ( Datum  original,
SortSupport  ssup 
)
static

Definition at line 593 of file network.c.

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

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

Referenced by network_sortsupport().

◆ network_broadcast()

Datum network_broadcast ( PG_FUNCTION_ARGS  )

Definition at line 1218 of file network.c.

1219{
1220 inet *ip = PG_GETARG_INET_PP(0);
1221 inet *dst;
1222 int byte;
1223 int bits;
1224 int maxbytes;
1225 unsigned char mask;
1226 unsigned char *a,
1227 *b;
1228
1229 /* make sure any unused bits are zeroed */
1230 dst = (inet *) palloc0(sizeof(inet));
1231
1232 maxbytes = ip_addrsize(ip);
1233 bits = ip_bits(ip);
1234 a = ip_addr(ip);
1235 b = ip_addr(dst);
1236
1237 for (byte = 0; byte < maxbytes; byte++)
1238 {
1239 if (bits >= 8)
1240 {
1241 mask = 0x00;
1242 bits -= 8;
1243 }
1244 else if (bits == 0)
1245 mask = 0xff;
1246 else
1247 {
1248 mask = 0xff >> bits;
1249 bits = 0;
1250 }
1251
1252 b[byte] = a[byte] | mask;
1253 }
1254
1255 ip_family(dst) = ip_family(ip);
1256 ip_bits(dst) = ip_bits(ip);
1257 SET_INET_VARSIZE(dst);
1258
1259 PG_RETURN_INET_P(dst);
1260}

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

Referenced by network_scan_last().

◆ network_cmp()

Datum network_cmp ( PG_FUNCTION_ARGS  )

Definition at line 421 of file network.c.

422{
425
427}
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354
static int32 network_cmp_internal(inet *a1, inet *a2)
Definition: network.c:401

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

◆ network_cmp_internal()

static int32 network_cmp_internal ( inet a1,
inet a2 
)
static

Definition at line 401 of file network.c.

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

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

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

◆ network_eq()

Datum network_eq ( PG_FUNCTION_ARGS  )

Definition at line 774 of file network.c.

775{
778
780}

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

◆ network_family()

Datum network_family ( PG_FUNCTION_ARGS  )

Definition at line 1199 of file network.c.

1200{
1201 inet *ip = PG_GETARG_INET_PP(0);
1202
1203 switch (ip_family(ip))
1204 {
1205 case PGSQL_AF_INET:
1206 PG_RETURN_INT32(4);
1207 break;
1208 case PGSQL_AF_INET6:
1209 PG_RETURN_INT32(6);
1210 break;
1211 default:
1212 PG_RETURN_INT32(0);
1213 break;
1214 }
1215}

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

◆ network_fast_cmp()

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

Definition at line 469 of file network.c.

470{
471 inet *arg1 = DatumGetInetPP(x);
472 inet *arg2 = DatumGetInetPP(y);
473
474 return network_cmp_internal(arg1, arg2);
475}
int y
Definition: isn.c:76

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

Referenced by network_sortsupport().

◆ network_ge()

Datum network_ge ( PG_FUNCTION_ARGS  )

Definition at line 783 of file network.c.

784{
787
789}

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

◆ network_gt()

Datum network_gt ( PG_FUNCTION_ARGS  )

Definition at line 792 of file network.c.

793{
796
798}

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

◆ network_host()

Datum network_host ( PG_FUNCTION_ARGS  )

Definition at line 1106 of file network.c.

1107{
1108 inet *ip = PG_GETARG_INET_PP(0);
1109 char *ptr;
1110 char tmp[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255/128")];
1111
1112 /* force display of max bits, regardless of masklen... */
1113 if (pg_inet_net_ntop(ip_family(ip), ip_addr(ip), ip_maxbits(ip),
1114 tmp, sizeof(tmp)) == NULL)
1115 ereport(ERROR,
1116 (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
1117 errmsg("could not format inet value: %m")));
1118
1119 /* Suppress /n if present (shouldn't happen now) */
1120 if ((ptr = strchr(tmp, '/')) != NULL)
1121 *ptr = '\0';
1122
1124}

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

◆ network_hostmask()

Datum network_hostmask ( PG_FUNCTION_ARGS  )

Definition at line 1349 of file network.c.

1350{
1351 inet *ip = PG_GETARG_INET_PP(0);
1352 inet *dst;
1353 int byte;
1354 int bits;
1355 int maxbytes;
1356 unsigned char mask;
1357 unsigned char *b;
1358
1359 /* make sure any unused bits are zeroed */
1360 dst = (inet *) palloc0(sizeof(inet));
1361
1362 maxbytes = ip_addrsize(ip);
1363 bits = ip_maxbits(ip) - ip_bits(ip);
1364 b = ip_addr(dst);
1365
1366 byte = maxbytes - 1;
1367
1368 while (bits)
1369 {
1370 if (bits >= 8)
1371 {
1372 mask = 0xff;
1373 bits -= 8;
1374 }
1375 else
1376 {
1377 mask = 0xff >> (8 - bits);
1378 bits = 0;
1379 }
1380
1381 b[byte] = mask;
1382 byte--;
1383 }
1384
1385 ip_family(dst) = ip_family(ip);
1386 ip_bits(dst) = ip_maxbits(ip);
1387 SET_INET_VARSIZE(dst);
1388
1389 PG_RETURN_INET_P(dst);
1390}

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

◆ network_in()

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

Definition at line 73 of file network.c.

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

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

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

◆ network_larger()

Datum network_larger ( PG_FUNCTION_ARGS  )

Definition at line 825 of file network.c.

826{
829
830 if (network_cmp_internal(a1, a2) > 0)
832 else
834}

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

◆ network_le()

Datum network_le ( PG_FUNCTION_ARGS  )

Definition at line 765 of file network.c.

766{
769
771}

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

◆ network_lt()

Datum network_lt ( PG_FUNCTION_ARGS  )

Definition at line 756 of file network.c.

757{
760
762}

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

◆ network_masklen()

Datum network_masklen ( PG_FUNCTION_ARGS  )

Definition at line 1191 of file network.c.

1192{
1193 inet *ip = PG_GETARG_INET_PP(0);
1194
1196}

References ip_bits, PG_GETARG_INET_PP, and PG_RETURN_INT32.

◆ network_ne()

Datum network_ne ( PG_FUNCTION_ARGS  )

Definition at line 801 of file network.c.

802{
805
807}

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

◆ network_netmask()

Datum network_netmask ( PG_FUNCTION_ARGS  )

Definition at line 1307 of file network.c.

1308{
1309 inet *ip = PG_GETARG_INET_PP(0);
1310 inet *dst;
1311 int byte;
1312 int bits;
1313 unsigned char mask;
1314 unsigned char *b;
1315
1316 /* make sure any unused bits are zeroed */
1317 dst = (inet *) palloc0(sizeof(inet));
1318
1319 bits = ip_bits(ip);
1320 b = ip_addr(dst);
1321
1322 byte = 0;
1323
1324 while (bits)
1325 {
1326 if (bits >= 8)
1327 {
1328 mask = 0xff;
1329 bits -= 8;
1330 }
1331 else
1332 {
1333 mask = 0xff << (8 - bits);
1334 bits = 0;
1335 }
1336
1337 b[byte] = mask;
1338 byte++;
1339 }
1340
1341 ip_family(dst) = ip_family(ip);
1342 ip_bits(dst) = ip_maxbits(ip);
1343 SET_INET_VARSIZE(dst);
1344
1345 PG_RETURN_INET_P(dst);
1346}

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

◆ network_network()

Datum network_network ( PG_FUNCTION_ARGS  )

Definition at line 1263 of file network.c.

1264{
1265 inet *ip = PG_GETARG_INET_PP(0);
1266 inet *dst;
1267 int byte;
1268 int bits;
1269 unsigned char mask;
1270 unsigned char *a,
1271 *b;
1272
1273 /* make sure any unused bits are zeroed */
1274 dst = (inet *) palloc0(sizeof(inet));
1275
1276 bits = ip_bits(ip);
1277 a = ip_addr(ip);
1278 b = ip_addr(dst);
1279
1280 byte = 0;
1281
1282 while (bits)
1283 {
1284 if (bits >= 8)
1285 {
1286 mask = 0xff;
1287 bits -= 8;
1288 }
1289 else
1290 {
1291 mask = 0xff << (8 - bits);
1292 bits = 0;
1293 }
1294
1295 b[byte] = a[byte] & mask;
1296 byte++;
1297 }
1298
1299 ip_family(dst) = ip_family(ip);
1300 ip_bits(dst) = ip_bits(ip);
1301 SET_INET_VARSIZE(dst);
1302
1303 PG_RETURN_INET_P(dst);
1304}

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

Referenced by network_scan_first().

◆ network_out()

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

Definition at line 139 of file network.c.

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

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

Referenced by cidr_out(), and inet_out().

◆ network_overlap()

Datum network_overlap ( PG_FUNCTION_ARGS  )

Definition at line 923 of file network.c.

924{
927
928 if (ip_family(a1) == ip_family(a2))
929 {
931 Min(ip_bits(a1), ip_bits(a2))) == 0);
932 }
933
934 PG_RETURN_BOOL(false);
935}

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

◆ network_recv()

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

Definition at line 190 of file network.c.

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

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

Referenced by cidr_recv(), and inet_recv().

◆ network_scan_first()

Datum network_scan_first ( Datum  in)

Definition at line 1623 of file network.c.

1624{
1626}
Datum network_network(PG_FUNCTION_ARGS)
Definition: network.c:1263

References DirectFunctionCall1, and network_network().

Referenced by match_network_subset().

◆ network_scan_last()

Datum network_scan_last ( Datum  in)

Definition at line 1637 of file network.c.

1638{
1641 Int32GetDatum(-1));
1642}
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:684
Datum network_broadcast(PG_FUNCTION_ARGS)
Definition: network.c:1218
Datum inet_set_masklen(PG_FUNCTION_ARGS)
Definition: network.c:320
static Datum Int32GetDatum(int32 X)
Definition: postgres.h:222

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

Referenced by match_network_subset().

◆ network_send()

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

Definition at line 268 of file network.c.

269{
271 char *addrptr;
272 int nb,
273 i;
274
276 pq_sendbyte(&buf, ip_family(addr));
277 pq_sendbyte(&buf, ip_bits(addr));
278 pq_sendbyte(&buf, is_cidr);
279 nb = ip_addrsize(addr);
280 pq_sendbyte(&buf, nb);
281 addrptr = (char *) ip_addr(addr);
282 for (i = 0; i < nb; i++)
283 pq_sendbyte(&buf, addrptr[i]);
284 return pq_endtypsend(&buf);
285}
void pq_begintypsend(StringInfo buf)
Definition: pqformat.c:326
bytea * pq_endtypsend(StringInfo buf)
Definition: pqformat.c:346
static void pq_sendbyte(StringInfo buf, uint8 byt)
Definition: pqformat.h:160

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

Referenced by cidr_send(), and inet_send().

◆ network_show()

Datum network_show ( PG_FUNCTION_ARGS  )

Definition at line 1132 of file network.c.

1133{
1134 inet *ip = PG_GETARG_INET_PP(0);
1135 int len;
1136 char tmp[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255/128")];
1137
1138 if (pg_inet_net_ntop(ip_family(ip), ip_addr(ip), ip_maxbits(ip),
1139 tmp, sizeof(tmp)) == NULL)
1140 ereport(ERROR,
1141 (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
1142 errmsg("could not format inet value: %m")));
1143
1144 /* Add /n if not present (which it won't be) */
1145 if (strchr(tmp, '/') == NULL)
1146 {
1147 len = strlen(tmp);
1148 snprintf(tmp + len, sizeof(tmp) - len, "/%u", ip_bits(ip));
1149 }
1150
1152}

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

◆ network_smaller()

Datum network_smaller ( PG_FUNCTION_ARGS  )

Definition at line 813 of file network.c.

814{
817
818 if (network_cmp_internal(a1, a2) < 0)
820 else
822}

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

◆ network_sortsupport()

Datum network_sortsupport ( PG_FUNCTION_ARGS  )

Definition at line 433 of file network.c.

434{
436
438 ssup->ssup_extra = NULL;
439
440 if (ssup->abbreviate)
441 {
443 MemoryContext oldcontext;
444
445 oldcontext = MemoryContextSwitchTo(ssup->ssup_cxt);
446
447 uss = palloc(sizeof(network_sortsupport_state));
448 uss->input_count = 0;
449 uss->estimating = true;
450 initHyperLogLog(&uss->abbr_card, 10);
451
452 ssup->ssup_extra = uss;
453
458
459 MemoryContextSwitchTo(oldcontext);
460 }
461
463}
#define PG_RETURN_VOID()
Definition: fmgr.h:349
void initHyperLogLog(hyperLogLogState *cState, uint8 bwidth)
Definition: hyperloglog.c:66
static int network_fast_cmp(Datum x, Datum y, SortSupport ssup)
Definition: network.c:469
static Datum network_abbrev_convert(Datum original, SortSupport ssup)
Definition: network.c:593
static bool network_abbrev_abort(int memtupcount, SortSupport ssup)
Definition: network.c:484
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:124
struct SortSupportData * SortSupport
Definition: sortsupport.h:58
int(* comparator)(Datum x, Datum y, SortSupport ssup)
Definition: sortsupport.h:106
Datum(* abbrev_converter)(Datum original, SortSupport ssup)
Definition: sortsupport.h:172
MemoryContext ssup_cxt
Definition: sortsupport.h:66
int(* abbrev_full_comparator)(Datum x, Datum y, SortSupport ssup)
Definition: sortsupport.h:191
bool(* abbrev_abort)(int memtupcount, SortSupport ssup)
Definition: sortsupport.h:182
int ssup_datum_unsigned_cmp(Datum x, Datum y, SortSupport ssup)
Definition: tuplesort.c:3133

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

◆ network_sub()

Datum network_sub ( PG_FUNCTION_ARGS  )

Definition at line 863 of file network.c.

864{
867
868 if (ip_family(a1) == ip_family(a2))
869 {
871 bitncmp(ip_addr(a1), ip_addr(a2), ip_bits(a2)) == 0);
872 }
873
874 PG_RETURN_BOOL(false);
875}

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

◆ network_subeq()

Datum network_subeq ( PG_FUNCTION_ARGS  )

Definition at line 878 of file network.c.

879{
882
883 if (ip_family(a1) == ip_family(a2))
884 {
886 bitncmp(ip_addr(a1), ip_addr(a2), ip_bits(a2)) == 0);
887 }
888
889 PG_RETURN_BOOL(false);
890}

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

◆ network_subset_support()

Datum network_subset_support ( PG_FUNCTION_ARGS  )

Definition at line 941 of file network.c.

942{
943 Node *rawreq = (Node *) PG_GETARG_POINTER(0);
944 Node *ret = NULL;
945
947 {
948 /* Try to convert operator/function call to index conditions */
950
951 if (is_opclause(req->node))
952 {
953 OpExpr *clause = (OpExpr *) req->node;
954
955 Assert(list_length(clause->args) == 2);
956 ret = (Node *)
958 (Node *) lsecond(clause->args),
959 req->indexarg,
960 req->funcid,
961 req->opfamily);
962 }
963 else if (is_funcclause(req->node)) /* be paranoid */
964 {
965 FuncExpr *clause = (FuncExpr *) req->node;
966
967 Assert(list_length(clause->args) == 2);
968 ret = (Node *)
970 (Node *) lsecond(clause->args),
971 req->indexarg,
972 req->funcid,
973 req->opfamily);
974 }
975 }
976
978}
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
static List * match_network_function(Node *leftop, Node *rightop, int indexarg, Oid funcid, Oid opfamily)
Definition: network.c:988
static bool is_opclause(const void *clause)
Definition: nodeFuncs.h:76
static bool is_funcclause(const void *clause)
Definition: nodeFuncs.h:69
static int list_length(const List *l)
Definition: pg_list.h:152
#define linitial(l)
Definition: pg_list.h:178
#define lsecond(l)
Definition: pg_list.h:183
List * args
Definition: primnodes.h:787
Definition: nodes.h:135
List * args
Definition: primnodes.h:855

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

◆ network_sup()

Datum network_sup ( PG_FUNCTION_ARGS  )

Definition at line 893 of file network.c.

894{
897
898 if (ip_family(a1) == ip_family(a2))
899 {
901 bitncmp(ip_addr(a1), ip_addr(a2), ip_bits(a1)) == 0);
902 }
903
904 PG_RETURN_BOOL(false);
905}

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

◆ network_supeq()

Datum network_supeq ( PG_FUNCTION_ARGS  )

Definition at line 908 of file network.c.

909{
912
913 if (ip_family(a1) == ip_family(a2))
914 {
916 bitncmp(ip_addr(a1), ip_addr(a2), ip_bits(a1)) == 0);
917 }
918
919 PG_RETURN_BOOL(false);
920}

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