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/hash.h"
#include "catalog/pg_type.h"
#include "common/ip.h"
#include "libpq/libpq-be.h"
#include "libpq/pqformat.h"
#include "miscadmin.h"
#include "utils/builtins.h"
#include "utils/inet.h"
Include dependency graph for network.c:

Go to the source code of this file.

Functions

static int32 network_cmp_internal (inet *a1, inet *a2)
 
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_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_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)
 
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)
 

Function Documentation

◆ addressOK()

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

Definition at line 1052 of file network.c.

References Assert, byte, and PGSQL_AF_INET.

Referenced by network_in(), and network_recv().

1053 {
1054  int byte;
1055  int nbits;
1056  int maxbits;
1057  int maxbytes;
1058  unsigned char mask;
1059 
1060  if (family == PGSQL_AF_INET)
1061  {
1062  maxbits = 32;
1063  maxbytes = 4;
1064  }
1065  else
1066  {
1067  maxbits = 128;
1068  maxbytes = 16;
1069  }
1070  Assert(bits <= maxbits);
1071 
1072  if (bits == maxbits)
1073  return true;
1074 
1075  byte = bits / 8;
1076 
1077  nbits = bits % 8;
1078  mask = 0xff;
1079  if (bits != 0)
1080  mask >>= nbits;
1081 
1082  while (byte < maxbytes)
1083  {
1084  if ((a[byte] & mask) != 0)
1085  return false;
1086  mask = 0xff;
1087  byte++;
1088  }
1089 
1090  return true;
1091 }
#define PGSQL_AF_INET
Definition: inet.h:39
#define byte(x, n)
Definition: rijndael.c:68
#define Assert(condition)
Definition: c.h:670

◆ bitncmp()

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

Definition at line 980 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().

981 {
982  unsigned int lb,
983  rb;
984  int x,
985  b;
986 
987  b = n / 8;
988  x = memcmp(l, r, b);
989  if (x || (n % 8) == 0)
990  return x;
991 
992  lb = l[b];
993  rb = r[b];
994  for (b = n % 8; b > 0; b--)
995  {
996  if (IS_HIGHBIT_SET(lb) != IS_HIGHBIT_SET(rb))
997  {
998  if (IS_HIGHBIT_SET(lb))
999  return 1;
1000  return -1;
1001  }
1002  lb <<= 1;
1003  rb <<= 1;
1004  }
1005  return 0;
1006 }
#define IS_HIGHBIT_SET(ch)
Definition: c.h:939

◆ bitncommon()

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

Definition at line 1014 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().

1015 {
1016  int byte,
1017  nbits;
1018 
1019  /* number of bits to examine in last byte */
1020  nbits = n % 8;
1021 
1022  /* check whole bytes */
1023  for (byte = 0; byte < n / 8; byte++)
1024  {
1025  if (l[byte] != r[byte])
1026  {
1027  /* at least one bit in the last byte is not common */
1028  nbits = 7;
1029  break;
1030  }
1031  }
1032 
1033  /* check bits in last partial byte */
1034  if (nbits != 0)
1035  {
1036  /* calculate diff of first non-matching bytes */
1037  unsigned int diff = l[byte] ^ r[byte];
1038 
1039  /* compare the bits from the most to the least */
1040  while ((diff >> (8 - nbits)) != 0)
1041  nbits--;
1042  }
1043 
1044  return (8 * byte) + nbits;
1045 }
#define byte(x, n)
Definition: rijndael.c:68

◆ cidr_abbrev()

Datum cidr_abbrev ( PG_FUNCTION_ARGS  )

Definition at line 648 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.

649 {
650  inet *ip = PG_GETARG_INET_PP(0);
651  char *dst;
652  char tmp[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255/128")];
653 
654  dst = inet_cidr_ntop(ip_family(ip), ip_addr(ip),
655  ip_bits(ip), tmp, sizeof(tmp));
656 
657  if (dst == NULL)
658  ereport(ERROR,
659  (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
660  errmsg("could not format cidr value: %m")));
661 
663 }
#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:575
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:122
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:331
text * cstring_to_text(const char *s)
Definition: varlena.c:149
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ cidr_in()

Datum cidr_in ( PG_FUNCTION_ARGS  )

Definition at line 88 of file network.c.

References network_in(), PG_GETARG_CSTRING, and PG_RETURN_INET_P.

89 {
90  char *src = PG_GETARG_CSTRING(0);
91 
92  PG_RETURN_INET_P(network_in(src, true));
93 }
#define PG_RETURN_INET_P(x)
Definition: inet.h:125
static inet * network_in(char *src, bool is_cidr)
Definition: network.c:34
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:242

◆ cidr_out()

Datum cidr_out ( PG_FUNCTION_ARGS  )

Definition at line 132 of file network.c.

References network_out(), PG_GETARG_INET_PP, and PG_RETURN_CSTRING.

133 {
134  inet *src = PG_GETARG_INET_PP(0);
135 
136  PG_RETURN_CSTRING(network_out(src, true));
137 }
#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:100
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:322

◆ cidr_recv()

Datum cidr_recv ( PG_FUNCTION_ARGS  )

Definition at line 217 of file network.c.

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

218 {
220 
221  PG_RETURN_INET_P(network_recv(buf, true));
222 }
StringInfoData * StringInfo
Definition: stringinfo.h:43
#define PG_RETURN_INET_P(x)
Definition: inet.h:125
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
static char * buf
Definition: pg_test_fsync.c:67
static inet * network_recv(StringInfo buf, bool is_cidr)
Definition: network.c:151

◆ cidr_send()

Datum cidr_send ( PG_FUNCTION_ARGS  )

Definition at line 259 of file network.c.

References network_send(), PG_GETARG_INET_PP, and PG_RETURN_BYTEA_P.

260 {
261  inet *addr = PG_GETARG_INET_PP(0);
262 
263  PG_RETURN_BYTEA_P(network_send(addr, true));
264 }
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:330
#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:229

◆ cidr_set_masklen()

Datum cidr_set_masklen ( PG_FUNCTION_ARGS  )

Definition at line 307 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.

308 {
309  inet *src = PG_GETARG_INET_PP(0);
310  int bits = PG_GETARG_INT32(1);
311 
312  if (bits == -1)
313  bits = ip_maxbits(src);
314 
315  if ((bits < 0) || (bits > ip_maxbits(src)))
316  ereport(ERROR,
317  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
318  errmsg("invalid mask length: %d", bits)));
319 
321 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
#define PG_RETURN_INET_P(x)
Definition: inet.h:125
int errcode(int sqlerrcode)
Definition: elog.c:575
#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:122
inet * cidr_set_masklen_internal(const inet *src, int bits)
Definition: network.c:327
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ cidr_set_masklen_internal()

inet* cidr_set_masklen_internal ( const inet src,
int  bits 
)

Definition at line 327 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().

328 {
329  inet *dst = (inet *) palloc0(sizeof(inet));
330 
331  ip_family(dst) = ip_family(src);
332  ip_bits(dst) = bits;
333 
334  if (bits > 0)
335  {
336  Assert(bits <= ip_maxbits(dst));
337 
338  /* Clone appropriate bytes of the address, leaving the rest 0 */
339  memcpy(ip_addr(dst), ip_addr(src), (bits + 7) / 8);
340 
341  /* Clear any unwanted bits in the last partial byte */
342  if (bits % 8)
343  ip_addr(dst)[bits / 8] &= ~(0xFF >> (bits % 8));
344  }
345 
346  /* Set varlena header correctly */
347  SET_INET_VARSIZE(dst);
348 
349  return dst;
350 }
#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:877
#define Assert(condition)
Definition: c.h:670

◆ clean_ipv6_addr()

void clean_ipv6_addr ( int  addr_family,
char *  addr 
)

Definition at line 1514 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().

1515 {
1516 #ifdef HAVE_IPV6
1517  if (addr_family == AF_INET6)
1518  {
1519  char *pct = strchr(addr, '%');
1520 
1521  if (pct)
1522  *pct = '\0';
1523  }
1524 #endif
1525 }

◆ convert_network_to_scalar()

double convert_network_to_scalar ( Datum  value,
Oid  typid 
)

Definition at line 907 of file network.c.

References macaddr::a, macaddr8::a, macaddr::b, macaddr8::b, macaddr::c, macaddr8::c, CIDROID, macaddr::d, macaddr8::d, DatumGetInetPP, DatumGetMacaddr8P, DatumGetMacaddrP, macaddr::e, macaddr8::e, elog, ERROR, macaddr::f, macaddr8::f, macaddr8::g, macaddr8::h, i, INETOID, ip_addr, ip_family, MACADDR8OID, MACADDROID, and PGSQL_AF_INET.

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

908 {
909  switch (typid)
910  {
911  case INETOID:
912  case CIDROID:
913  {
914  inet *ip = DatumGetInetPP(value);
915  int len;
916  double res;
917  int i;
918 
919  /*
920  * Note that we don't use the full address for IPv6.
921  */
922  if (ip_family(ip) == PGSQL_AF_INET)
923  len = 4;
924  else
925  len = 5;
926 
927  res = ip_family(ip);
928  for (i = 0; i < len; i++)
929  {
930  res *= 256;
931  res += ip_addr(ip)[i];
932  }
933  return res;
934 
935  break;
936  }
937  case MACADDROID:
938  {
940  double res;
941 
942  res = (mac->a << 16) | (mac->b << 8) | (mac->c);
943  res *= 256 * 256 * 256;
944  res += (mac->d << 16) | (mac->e << 8) | (mac->f);
945  return res;
946  }
947  case MACADDR8OID:
948  {
950  double res;
951 
952  res = (mac->a << 24) | (mac->b << 16) | (mac->c << 8) | (mac->d);
953  res *= ((double) 256) * 256 * 256 * 256;
954  res += (mac->e << 24) | (mac->f << 16) | (mac->g << 8) | (mac->h);
955  return res;
956  }
957  }
958 
959  /*
960  * Can't get here unless someone tries to use scalarineqsel() on an
961  * operator with one network and one non-network operand.
962  */
963  elog(ERROR, "unsupported type: %u", typid);
964  return 0;
965 }
#define CIDROID
Definition: pg_type.h:451
#define DatumGetMacaddrP(X)
Definition: inet.h:131
#define ip_family(inetptr)
Definition: inet.h:71
#define PGSQL_AF_INET
Definition: inet.h:39
#define INETOID
Definition: pg_type.h:448
#define DatumGetInetPP(X)
Definition: inet.h:122
unsigned char f
Definition: inet.h:101
#define ip_addr(inetptr)
Definition: inet.h:77
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
#define ERROR
Definition: elog.h:43
unsigned char a
Definition: inet.h:96
Definition: inet.h:107
static struct @121 value
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
#define MACADDR8OID
Definition: pg_type.h:454
unsigned char d
Definition: inet.h:112
int i
#define MACADDROID
Definition: pg_type.h:445
#define elog
Definition: elog.h:219

◆ hashinet()

Datum hashinet ( PG_FUNCTION_ARGS  )

Definition at line 480 of file network.c.

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

481 {
482  inet *addr = PG_GETARG_INET_PP(0);
483  int addrsize = ip_addrsize(addr);
484 
485  /* XXX this assumes there are no pad bytes in the data structure */
486  return hash_any((unsigned char *) VARDATA_ANY(addr), addrsize + 2);
487 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
#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: hashfunc.c:428

◆ hashinetextended()

Datum hashinetextended ( PG_FUNCTION_ARGS  )

Definition at line 490 of file network.c.

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

491 {
492  inet *addr = PG_GETARG_INET_PP(0);
493  int addrsize = ip_addrsize(addr);
494 
495  return hash_any_extended((unsigned char *) VARDATA_ANY(addr), addrsize + 2,
496  PG_GETARG_INT64(1));
497 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
#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:247
Datum hash_any_extended(register const unsigned char *k, register int keylen, uint64 seed)
Definition: hashfunc.c:634

◆ inet_abbrev()

Datum inet_abbrev ( PG_FUNCTION_ARGS  )

Definition at line 630 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.

631 {
632  inet *ip = PG_GETARG_INET_PP(0);
633  char *dst;
634  char tmp[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255/128")];
635 
636  dst = inet_net_ntop(ip_family(ip), ip_addr(ip),
637  ip_bits(ip), tmp, sizeof(tmp));
638 
639  if (dst == NULL)
640  ereport(ERROR,
641  (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
642  errmsg("could not format inet value: %m")));
643 
645 }
#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:575
#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:122
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:331
text * cstring_to_text(const char *s)
Definition: varlena.c:149
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ inet_client_addr()

Datum inet_client_addr ( PG_FUNCTION_ARGS  )

Definition at line 1127 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.

1128 {
1129  Port *port = MyProcPort;
1130  char remote_host[NI_MAXHOST];
1131  int ret;
1132 
1133  if (port == NULL)
1134  PG_RETURN_NULL();
1135 
1136  switch (port->raddr.addr.ss_family)
1137  {
1138  case AF_INET:
1139 #ifdef HAVE_IPV6
1140  case AF_INET6:
1141 #endif
1142  break;
1143  default:
1144  PG_RETURN_NULL();
1145  }
1146 
1147  remote_host[0] = '\0';
1148 
1149  ret = pg_getnameinfo_all(&port->raddr.addr, port->raddr.salen,
1150  remote_host, sizeof(remote_host),
1151  NULL, 0,
1153  if (ret != 0)
1154  PG_RETURN_NULL();
1155 
1156  clean_ipv6_addr(port->raddr.addr.ss_family, remote_host);
1157 
1158  PG_RETURN_INET_P(network_in(remote_host, false));
1159 }
#define NI_NUMERICHOST
Definition: getaddrinfo.h:78
struct Port * MyProcPort
Definition: globals.c:41
struct sockaddr_storage addr
Definition: pqcomm.h:64
#define PG_RETURN_INET_P(x)
Definition: inet.h:125
Definition: libpq-be.h:116
static inet * network_in(char *src, bool is_cidr)
Definition: network.c:34
SockAddr raddr
Definition: libpq-be.h:122
#define NI_MAXHOST
Definition: getaddrinfo.h:88
ACCEPT_TYPE_ARG3 salen
Definition: pqcomm.h:65
static int port
Definition: pg_regress.c:90
#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:1514
#define PG_RETURN_NULL()
Definition: fmgr.h:305

◆ inet_client_port()

Datum inet_client_port ( PG_FUNCTION_ARGS  )

Definition at line 1166 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.

1167 {
1168  Port *port = MyProcPort;
1169  char remote_port[NI_MAXSERV];
1170  int ret;
1171 
1172  if (port == NULL)
1173  PG_RETURN_NULL();
1174 
1175  switch (port->raddr.addr.ss_family)
1176  {
1177  case AF_INET:
1178 #ifdef HAVE_IPV6
1179  case AF_INET6:
1180 #endif
1181  break;
1182  default:
1183  PG_RETURN_NULL();
1184  }
1185 
1186  remote_port[0] = '\0';
1187 
1188  ret = pg_getnameinfo_all(&port->raddr.addr, port->raddr.salen,
1189  NULL, 0,
1190  remote_port, sizeof(remote_port),
1192  if (ret != 0)
1193  PG_RETURN_NULL();
1194 
1196 }
#define NI_NUMERICHOST
Definition: getaddrinfo.h:78
struct Port * MyProcPort
Definition: globals.c:41
Datum int4in(PG_FUNCTION_ARGS)
Definition: int.c:266
struct sockaddr_storage addr
Definition: pqcomm.h:64
Definition: libpq-be.h:116
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:585
SockAddr raddr
Definition: libpq-be.h:122
#define CStringGetDatum(X)
Definition: postgres.h:584
#define NI_MAXSERV
Definition: getaddrinfo.h:91
ACCEPT_TYPE_ARG3 salen
Definition: pqcomm.h:65
static int port
Definition: pg_regress.c:90
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:313
#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:305

◆ inet_in()

Datum inet_in ( PG_FUNCTION_ARGS  )

Definition at line 80 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().

81 {
82  char *src = PG_GETARG_CSTRING(0);
83 
84  PG_RETURN_INET_P(network_in(src, false));
85 }
#define PG_RETURN_INET_P(x)
Definition: inet.h:125
static inet * network_in(char *src, bool is_cidr)
Definition: network.c:34
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:242

◆ inet_merge()

Datum inet_merge ( PG_FUNCTION_ARGS  )

Definition at line 884 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.

885 {
886  inet *a1 = PG_GETARG_INET_PP(0),
887  *a2 = PG_GETARG_INET_PP(1);
888  int commonbits;
889 
890  if (ip_family(a1) != ip_family(a2))
891  ereport(ERROR,
892  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
893  errmsg("cannot merge addresses from different families")));
894 
895  commonbits = bitncommon(ip_addr(a1), ip_addr(a2),
896  Min(ip_bits(a1), ip_bits(a2)));
897 
899 }
#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:802
#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:1014
int errcode(int sqlerrcode)
Definition: elog.c:575
#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:122
static FormData_pg_attribute a1
Definition: heap.c:144
inet * cidr_set_masklen_internal(const inet *src, int bits)
Definition: network.c:327
int errmsg(const char *fmt,...)
Definition: elog.c:797
static FormData_pg_attribute a2
Definition: heap.c:150

◆ inet_out()

Datum inet_out ( PG_FUNCTION_ARGS  )

Definition at line 124 of file network.c.

References network_out(), PG_GETARG_INET_PP, and PG_RETURN_CSTRING.

125 {
126  inet *src = PG_GETARG_INET_PP(0);
127 
128  PG_RETURN_CSTRING(network_out(src, false));
129 }
#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:100
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:322

◆ inet_recv()

Datum inet_recv ( PG_FUNCTION_ARGS  )

Definition at line 209 of file network.c.

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

210 {
212 
213  PG_RETURN_INET_P(network_recv(buf, false));
214 }
StringInfoData * StringInfo
Definition: stringinfo.h:43
#define PG_RETURN_INET_P(x)
Definition: inet.h:125
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
static char * buf
Definition: pg_test_fsync.c:67
static inet * network_recv(StringInfo buf, bool is_cidr)
Definition: network.c:151

◆ inet_same_family()

Datum inet_same_family ( PG_FUNCTION_ARGS  )

Definition at line 872 of file network.c.

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

873 {
874  inet *a1 = PG_GETARG_INET_PP(0);
875  inet *a2 = PG_GETARG_INET_PP(1);
876 
877  PG_RETURN_BOOL(ip_family(a1) == ip_family(a2));
878 }
#define ip_family(inetptr)
Definition: inet.h:71
#define PG_GETARG_INET_PP(n)
Definition: inet.h:124
Definition: inet.h:52
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
static FormData_pg_attribute a1
Definition: heap.c:144
static FormData_pg_attribute a2
Definition: heap.c:150

◆ inet_send()

Datum inet_send ( PG_FUNCTION_ARGS  )

Definition at line 251 of file network.c.

References network_send(), PG_GETARG_INET_PP, and PG_RETURN_BYTEA_P.

252 {
253  inet *addr = PG_GETARG_INET_PP(0);
254 
255  PG_RETURN_BYTEA_P(network_send(addr, false));
256 }
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:330
#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:229

◆ inet_server_addr()

Datum inet_server_addr ( PG_FUNCTION_ARGS  )

Definition at line 1203 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.

1204 {
1205  Port *port = MyProcPort;
1206  char local_host[NI_MAXHOST];
1207  int ret;
1208 
1209  if (port == NULL)
1210  PG_RETURN_NULL();
1211 
1212  switch (port->laddr.addr.ss_family)
1213  {
1214  case AF_INET:
1215 #ifdef HAVE_IPV6
1216  case AF_INET6:
1217 #endif
1218  break;
1219  default:
1220  PG_RETURN_NULL();
1221  }
1222 
1223  local_host[0] = '\0';
1224 
1225  ret = pg_getnameinfo_all(&port->laddr.addr, port->laddr.salen,
1226  local_host, sizeof(local_host),
1227  NULL, 0,
1229  if (ret != 0)
1230  PG_RETURN_NULL();
1231 
1232  clean_ipv6_addr(port->laddr.addr.ss_family, local_host);
1233 
1234  PG_RETURN_INET_P(network_in(local_host, false));
1235 }
#define NI_NUMERICHOST
Definition: getaddrinfo.h:78
struct Port * MyProcPort
Definition: globals.c:41
struct sockaddr_storage addr
Definition: pqcomm.h:64
#define PG_RETURN_INET_P(x)
Definition: inet.h:125
Definition: libpq-be.h:116
static inet * network_in(char *src, bool is_cidr)
Definition: network.c:34
#define NI_MAXHOST
Definition: getaddrinfo.h:88
ACCEPT_TYPE_ARG3 salen
Definition: pqcomm.h:65
SockAddr laddr
Definition: libpq-be.h:121
static int port
Definition: pg_regress.c:90
#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:1514
#define PG_RETURN_NULL()
Definition: fmgr.h:305

◆ inet_server_port()

Datum inet_server_port ( PG_FUNCTION_ARGS  )

Definition at line 1242 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.

1243 {
1244  Port *port = MyProcPort;
1245  char local_port[NI_MAXSERV];
1246  int ret;
1247 
1248  if (port == NULL)
1249  PG_RETURN_NULL();
1250 
1251  switch (port->laddr.addr.ss_family)
1252  {
1253  case AF_INET:
1254 #ifdef HAVE_IPV6
1255  case AF_INET6:
1256 #endif
1257  break;
1258  default:
1259  PG_RETURN_NULL();
1260  }
1261 
1262  local_port[0] = '\0';
1263 
1264  ret = pg_getnameinfo_all(&port->laddr.addr, port->laddr.salen,
1265  NULL, 0,
1266  local_port, sizeof(local_port),
1268  if (ret != 0)
1269  PG_RETURN_NULL();
1270 
1272 }
#define NI_NUMERICHOST
Definition: getaddrinfo.h:78
struct Port * MyProcPort
Definition: globals.c:41
Datum int4in(PG_FUNCTION_ARGS)
Definition: int.c:266
struct sockaddr_storage addr
Definition: pqcomm.h:64
Definition: libpq-be.h:116
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:585
#define CStringGetDatum(X)
Definition: postgres.h:584
#define NI_MAXSERV
Definition: getaddrinfo.h:91
ACCEPT_TYPE_ARG3 salen
Definition: pqcomm.h:65
SockAddr laddr
Definition: libpq-be.h:121
static int port
Definition: pg_regress.c:90
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:313
#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:305

◆ inet_set_masklen()

Datum inet_set_masklen ( PG_FUNCTION_ARGS  )

Definition at line 283 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().

284 {
285  inet *src = PG_GETARG_INET_PP(0);
286  int bits = PG_GETARG_INT32(1);
287  inet *dst;
288 
289  if (bits == -1)
290  bits = ip_maxbits(src);
291 
292  if ((bits < 0) || (bits > ip_maxbits(src)))
293  ereport(ERROR,
294  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
295  errmsg("invalid mask length: %d", bits)));
296 
297  /* clone the original data */
298  dst = (inet *) palloc(VARSIZE_ANY(src));
299  memcpy(dst, src, VARSIZE_ANY(src));
300 
301  ip_bits(dst) = bits;
302 
303  PG_RETURN_INET_P(dst);
304 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
#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:575
#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:122
#define VARSIZE_ANY(PTR)
Definition: postgres.h:334
void * palloc(Size size)
Definition: mcxt.c:848
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ inet_to_cidr()

Datum inet_to_cidr ( PG_FUNCTION_ARGS  )

Definition at line 268 of file network.c.

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

269 {
270  inet *src = PG_GETARG_INET_PP(0);
271  int bits;
272 
273  bits = ip_bits(src);
274 
275  /* safety check */
276  if ((bits < 0) || (bits > ip_maxbits(src)))
277  elog(ERROR, "invalid inet bit length: %d", bits);
278 
280 }
#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:327
#define elog
Definition: elog.h:219

◆ inetand()

Datum inetand ( PG_FUNCTION_ARGS  )

Definition at line 1301 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.

1302 {
1303  inet *ip = PG_GETARG_INET_PP(0);
1304  inet *ip2 = PG_GETARG_INET_PP(1);
1305  inet *dst;
1306 
1307  dst = (inet *) palloc0(sizeof(inet));
1308 
1309  if (ip_family(ip) != ip_family(ip2))
1310  ereport(ERROR,
1311  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1312  errmsg("cannot AND inet values of different sizes")));
1313  else
1314  {
1315  int nb = ip_addrsize(ip);
1316  unsigned char *pip = ip_addr(ip);
1317  unsigned char *pip2 = ip_addr(ip2);
1318  unsigned char *pdst = ip_addr(dst);
1319 
1320  while (nb-- > 0)
1321  pdst[nb] = pip[nb] & pip2[nb];
1322  }
1323  ip_bits(dst) = Max(ip_bits(ip), ip_bits(ip2));
1324 
1325  ip_family(dst) = ip_family(ip);
1326  SET_INET_VARSIZE(dst);
1327 
1328  PG_RETURN_INET_P(dst);
1329 }
#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:575
#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:122
void * palloc0(Size size)
Definition: mcxt.c:877
#define Max(x, y)
Definition: c.h:796
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ inetmi()

Datum inetmi ( PG_FUNCTION_ARGS  )

Definition at line 1437 of file network.c.

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

1438 {
1439  inet *ip = PG_GETARG_INET_PP(0);
1440  inet *ip2 = PG_GETARG_INET_PP(1);
1441  int64 res = 0;
1442 
1443  if (ip_family(ip) != ip_family(ip2))
1444  ereport(ERROR,
1445  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1446  errmsg("cannot subtract inet values of different sizes")));
1447  else
1448  {
1449  /*
1450  * We form the difference using the traditional complement, increment,
1451  * and add rule, with the increment part being handled by starting the
1452  * carry off at 1. If you don't think integer arithmetic is done in
1453  * two's complement, too bad.
1454  */
1455  int nb = ip_addrsize(ip);
1456  int byte = 0;
1457  unsigned char *pip = ip_addr(ip);
1458  unsigned char *pip2 = ip_addr(ip2);
1459  int carry = 1;
1460 
1461  while (nb-- > 0)
1462  {
1463  int lobyte;
1464 
1465  carry = pip[nb] + (~pip2[nb] & 0xFF) + carry;
1466  lobyte = carry & 0xFF;
1467  if (byte < sizeof(int64))
1468  {
1469  res |= ((int64) lobyte) << (byte * 8);
1470  }
1471  else
1472  {
1473  /*
1474  * Input wider than int64: check for overflow. All bytes to
1475  * the left of what will fit should be 0 or 0xFF, depending on
1476  * sign of the now-complete result.
1477  */
1478  if ((res < 0) ? (lobyte != 0xFF) : (lobyte != 0))
1479  ereport(ERROR,
1480  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1481  errmsg("result is out of range")));
1482  }
1483  carry >>= 8;
1484  byte++;
1485  }
1486 
1487  /*
1488  * If input is narrower than int64, overflow is not possible, but we
1489  * have to do proper sign extension.
1490  */
1491  if (carry == 0 && byte < sizeof(int64))
1492  res |= ((int64) -1) << (byte * 8);
1493  }
1494 
1495  PG_RETURN_INT64(res);
1496 }
#define ip_family(inetptr)
Definition: inet.h:71
#define PG_RETURN_INT64(x)
Definition: fmgr.h:327
#define ip_addr(inetptr)
Definition: inet.h:77
int errcode(int sqlerrcode)
Definition: elog.c:575
#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:122
#define byte(x, n)
Definition: rijndael.c:68
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ inetmi_int8()

Datum inetmi_int8 ( PG_FUNCTION_ARGS  )

Definition at line 1427 of file network.c.

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

1428 {
1429  inet *ip = PG_GETARG_INET_PP(0);
1430  int64 addend = PG_GETARG_INT64(1);
1431 
1432  PG_RETURN_INET_P(internal_inetpl(ip, -addend));
1433 }
#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:1365
#define PG_GETARG_INT64(n)
Definition: fmgr.h:247

◆ inetnot()

Datum inetnot ( PG_FUNCTION_ARGS  )

Definition at line 1276 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.

1277 {
1278  inet *ip = PG_GETARG_INET_PP(0);
1279  inet *dst;
1280 
1281  dst = (inet *) palloc0(sizeof(inet));
1282 
1283  {
1284  int nb = ip_addrsize(ip);
1285  unsigned char *pip = ip_addr(ip);
1286  unsigned char *pdst = ip_addr(dst);
1287 
1288  while (nb-- > 0)
1289  pdst[nb] = ~pip[nb];
1290  }
1291  ip_bits(dst) = ip_bits(ip);
1292 
1293  ip_family(dst) = ip_family(ip);
1294  SET_INET_VARSIZE(dst);
1295 
1296  PG_RETURN_INET_P(dst);
1297 }
#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:877

◆ inetor()

Datum inetor ( PG_FUNCTION_ARGS  )

Definition at line 1333 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.

1334 {
1335  inet *ip = PG_GETARG_INET_PP(0);
1336  inet *ip2 = PG_GETARG_INET_PP(1);
1337  inet *dst;
1338 
1339  dst = (inet *) palloc0(sizeof(inet));
1340 
1341  if (ip_family(ip) != ip_family(ip2))
1342  ereport(ERROR,
1343  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1344  errmsg("cannot OR inet values of different sizes")));
1345  else
1346  {
1347  int nb = ip_addrsize(ip);
1348  unsigned char *pip = ip_addr(ip);
1349  unsigned char *pip2 = ip_addr(ip2);
1350  unsigned char *pdst = ip_addr(dst);
1351 
1352  while (nb-- > 0)
1353  pdst[nb] = pip[nb] | pip2[nb];
1354  }
1355  ip_bits(dst) = Max(ip_bits(ip), ip_bits(ip2));
1356 
1357  ip_family(dst) = ip_family(ip);
1358  SET_INET_VARSIZE(dst);
1359 
1360  PG_RETURN_INET_P(dst);
1361 }
#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:575
#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:122
void * palloc0(Size size)
Definition: mcxt.c:877
#define Max(x, y)
Definition: c.h:796
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ inetpl()

Datum inetpl ( PG_FUNCTION_ARGS  )

Definition at line 1417 of file network.c.

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

1418 {
1419  inet *ip = PG_GETARG_INET_PP(0);
1420  int64 addend = PG_GETARG_INT64(1);
1421 
1422  PG_RETURN_INET_P(internal_inetpl(ip, addend));
1423 }
#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:1365
#define PG_GETARG_INT64(n)
Definition: fmgr.h:247

◆ internal_inetpl()

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

Definition at line 1365 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().

1366 {
1367  inet *dst;
1368 
1369  dst = (inet *) palloc0(sizeof(inet));
1370 
1371  {
1372  int nb = ip_addrsize(ip);
1373  unsigned char *pip = ip_addr(ip);
1374  unsigned char *pdst = ip_addr(dst);
1375  int carry = 0;
1376 
1377  while (nb-- > 0)
1378  {
1379  carry = pip[nb] + (int) (addend & 0xFF) + carry;
1380  pdst[nb] = (unsigned char) (carry & 0xFF);
1381  carry >>= 8;
1382 
1383  /*
1384  * We have to be careful about right-shifting addend because
1385  * right-shift isn't portable for negative values, while simply
1386  * dividing by 256 doesn't work (the standard rounding is in the
1387  * wrong direction, besides which there may be machines out there
1388  * that round the wrong way). So, explicitly clear the low-order
1389  * byte to remove any doubt about the correct result of the
1390  * division, and then divide rather than shift.
1391  */
1392  addend &= ~((int64) 0xFF);
1393  addend /= 0x100;
1394  }
1395 
1396  /*
1397  * At this point we should have addend and carry both zero if original
1398  * addend was >= 0, or addend -1 and carry 1 if original addend was <
1399  * 0. Anything else means overflow.
1400  */
1401  if (!((addend == 0 && carry == 0) ||
1402  (addend == -1 && carry == 1)))
1403  ereport(ERROR,
1404  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1405  errmsg("result is out of range")));
1406  }
1407 
1408  ip_bits(dst) = ip_bits(ip);
1409  ip_family(dst) = ip_family(ip);
1410  SET_INET_VARSIZE(dst);
1411 
1412  return dst;
1413 }
#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:575
#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:122
void * palloc0(Size size)
Definition: mcxt.c:877
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ network_broadcast()

Datum network_broadcast ( PG_FUNCTION_ARGS  )

Definition at line 693 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().

694 {
695  inet *ip = PG_GETARG_INET_PP(0);
696  inet *dst;
697  int byte;
698  int bits;
699  int maxbytes;
700  unsigned char mask;
701  unsigned char *a,
702  *b;
703 
704  /* make sure any unused bits are zeroed */
705  dst = (inet *) palloc0(sizeof(inet));
706 
707  maxbytes = ip_addrsize(ip);
708  bits = ip_bits(ip);
709  a = ip_addr(ip);
710  b = ip_addr(dst);
711 
712  for (byte = 0; byte < maxbytes; byte++)
713  {
714  if (bits >= 8)
715  {
716  mask = 0x00;
717  bits -= 8;
718  }
719  else if (bits == 0)
720  mask = 0xff;
721  else
722  {
723  mask = 0xff >> bits;
724  bits = 0;
725  }
726 
727  b[byte] = a[byte] | mask;
728  }
729 
730  ip_family(dst) = ip_family(ip);
731  ip_bits(dst) = ip_bits(ip);
732  SET_INET_VARSIZE(dst);
733 
734  PG_RETURN_INET_P(dst);
735 }
#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:877

◆ network_cmp()

Datum network_cmp ( PG_FUNCTION_ARGS  )

Definition at line 384 of file network.c.

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

Referenced by leftmostvalue_inet().

385 {
386  inet *a1 = PG_GETARG_INET_PP(0);
387  inet *a2 = PG_GETARG_INET_PP(1);
388 
390 }
#define PG_RETURN_INT32(x)
Definition: fmgr.h:314
static int32 network_cmp_internal(inet *a1, inet *a2)
Definition: network.c:364
#define PG_GETARG_INET_PP(n)
Definition: inet.h:124
Definition: inet.h:52
static FormData_pg_attribute a1
Definition: heap.c:144
static FormData_pg_attribute a2
Definition: heap.c:150

◆ network_cmp_internal()

static int32 network_cmp_internal ( inet a1,
inet a2 
)
static

Definition at line 364 of file network.c.

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

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

365 {
366  if (ip_family(a1) == ip_family(a2))
367  {
368  int order;
369 
370  order = bitncmp(ip_addr(a1), ip_addr(a2),
371  Min(ip_bits(a1), ip_bits(a2)));
372  if (order != 0)
373  return order;
374  order = ((int) ip_bits(a1)) - ((int) ip_bits(a2));
375  if (order != 0)
376  return order;
377  return bitncmp(ip_addr(a1), ip_addr(a2), ip_maxbits(a1));
378  }
379 
380  return ip_family(a1) - ip_family(a2);
381 }
#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:802
#define ip_maxbits(inetptr)
Definition: inet.h:83
int bitncmp(const unsigned char *l, const unsigned char *r, int n)
Definition: network.c:980

◆ network_eq()

Datum network_eq ( PG_FUNCTION_ARGS  )

Definition at line 414 of file network.c.

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

415 {
416  inet *a1 = PG_GETARG_INET_PP(0);
417  inet *a2 = PG_GETARG_INET_PP(1);
418 
419  PG_RETURN_BOOL(network_cmp_internal(a1, a2) == 0);
420 }
static int32 network_cmp_internal(inet *a1, inet *a2)
Definition: network.c:364
#define PG_GETARG_INET_PP(n)
Definition: inet.h:124
Definition: inet.h:52
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
static FormData_pg_attribute a1
Definition: heap.c:144
static FormData_pg_attribute a2
Definition: heap.c:150

◆ network_family()

Datum network_family ( PG_FUNCTION_ARGS  )

Definition at line 674 of file network.c.

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

675 {
676  inet *ip = PG_GETARG_INET_PP(0);
677 
678  switch (ip_family(ip))
679  {
680  case PGSQL_AF_INET:
681  PG_RETURN_INT32(4);
682  break;
683  case PGSQL_AF_INET6:
684  PG_RETURN_INT32(6);
685  break;
686  default:
687  PG_RETURN_INT32(0);
688  break;
689  }
690 }
#define ip_family(inetptr)
Definition: inet.h:71
#define PGSQL_AF_INET
Definition: inet.h:39
#define PG_RETURN_INT32(x)
Definition: fmgr.h:314
#define PGSQL_AF_INET6
Definition: inet.h:40
#define PG_GETARG_INET_PP(n)
Definition: inet.h:124
Definition: inet.h:52

◆ network_ge()

Datum network_ge ( PG_FUNCTION_ARGS  )

Definition at line 423 of file network.c.

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

424 {
425  inet *a1 = PG_GETARG_INET_PP(0);
426  inet *a2 = PG_GETARG_INET_PP(1);
427 
428  PG_RETURN_BOOL(network_cmp_internal(a1, a2) >= 0);
429 }
static int32 network_cmp_internal(inet *a1, inet *a2)
Definition: network.c:364
#define PG_GETARG_INET_PP(n)
Definition: inet.h:124
Definition: inet.h:52
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
static FormData_pg_attribute a1
Definition: heap.c:144
static FormData_pg_attribute a2
Definition: heap.c:150

◆ network_gt()

Datum network_gt ( PG_FUNCTION_ARGS  )

Definition at line 432 of file network.c.

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

433 {
434  inet *a1 = PG_GETARG_INET_PP(0);
435  inet *a2 = PG_GETARG_INET_PP(1);
436 
438 }
static int32 network_cmp_internal(inet *a1, inet *a2)
Definition: network.c:364
#define PG_GETARG_INET_PP(n)
Definition: inet.h:124
Definition: inet.h:52
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
static FormData_pg_attribute a1
Definition: heap.c:144
static FormData_pg_attribute a2
Definition: heap.c:150

◆ network_host()

Datum network_host ( PG_FUNCTION_ARGS  )

Definition at line 581 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.

582 {
583  inet *ip = PG_GETARG_INET_PP(0);
584  char *ptr;
585  char tmp[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255/128")];
586 
587  /* force display of max bits, regardless of masklen... */
588  if (inet_net_ntop(ip_family(ip), ip_addr(ip), ip_maxbits(ip),
589  tmp, sizeof(tmp)) == NULL)
590  ereport(ERROR,
591  (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
592  errmsg("could not format inet value: %m")));
593 
594  /* Suppress /n if present (shouldn't happen now) */
595  if ((ptr = strchr(tmp, '/')) != NULL)
596  *ptr = '\0';
597 
599 }
#define ip_family(inetptr)
Definition: inet.h:71
#define ip_addr(inetptr)
Definition: inet.h:77
int errcode(int sqlerrcode)
Definition: elog.c:575
#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:122
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:331
text * cstring_to_text(const char *s)
Definition: varlena.c:149
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ network_hostmask()

Datum network_hostmask ( PG_FUNCTION_ARGS  )

Definition at line 824 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.

825 {
826  inet *ip = PG_GETARG_INET_PP(0);
827  inet *dst;
828  int byte;
829  int bits;
830  int maxbytes;
831  unsigned char mask;
832  unsigned char *b;
833 
834  /* make sure any unused bits are zeroed */
835  dst = (inet *) palloc0(sizeof(inet));
836 
837  maxbytes = ip_addrsize(ip);
838  bits = ip_maxbits(ip) - ip_bits(ip);
839  b = ip_addr(dst);
840 
841  byte = maxbytes - 1;
842 
843  while (bits)
844  {
845  if (bits >= 8)
846  {
847  mask = 0xff;
848  bits -= 8;
849  }
850  else
851  {
852  mask = 0xff >> (8 - bits);
853  bits = 0;
854  }
855 
856  b[byte] = mask;
857  byte--;
858  }
859 
860  ip_family(dst) = ip_family(ip);
861  ip_bits(dst) = ip_maxbits(ip);
862  SET_INET_VARSIZE(dst);
863 
864  PG_RETURN_INET_P(dst);
865 }
#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:877

◆ network_in()

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

Definition at line 34 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().

35 {
36  int bits;
37  inet *dst;
38 
39  dst = (inet *) palloc0(sizeof(inet));
40 
41  /*
42  * First, check to see if this is an IPv6 or IPv4 address. IPv6 addresses
43  * will have a : somewhere in them (several, in fact) so if there is one
44  * present, assume it's V6, otherwise assume it's V4.
45  */
46 
47  if (strchr(src, ':') != NULL)
49  else
50  ip_family(dst) = PGSQL_AF_INET;
51 
52  bits = inet_net_pton(ip_family(dst), src, ip_addr(dst),
53  is_cidr ? ip_addrsize(dst) : -1);
54  if ((bits < 0) || (bits > ip_maxbits(dst)))
55  ereport(ERROR,
56  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
57  /* translator: first %s is inet or cidr */
58  errmsg("invalid input syntax for type %s: \"%s\"",
59  is_cidr ? "cidr" : "inet", src)));
60 
61  /*
62  * Error check: CIDR values must not have any bits set beyond the masklen.
63  */
64  if (is_cidr)
65  {
66  if (!addressOK(ip_addr(dst), bits, ip_family(dst)))
67  ereport(ERROR,
68  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
69  errmsg("invalid cidr value: \"%s\"", src),
70  errdetail("Value has bits set to right of mask.")));
71  }
72 
73  ip_bits(dst) = bits;
74  SET_INET_VARSIZE(dst);
75 
76  return dst;
77 }
#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:1052
#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:575
#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:873
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:122
void * palloc0(Size size)
Definition: mcxt.c:877
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ network_larger()

Datum network_larger ( PG_FUNCTION_ARGS  )

Definition at line 465 of file network.c.

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

466 {
467  inet *a1 = PG_GETARG_INET_PP(0);
468  inet *a2 = PG_GETARG_INET_PP(1);
469 
470  if (network_cmp_internal(a1, a2) > 0)
471  PG_RETURN_INET_P(a1);
472  else
473  PG_RETURN_INET_P(a2);
474 }
#define PG_RETURN_INET_P(x)
Definition: inet.h:125
static int32 network_cmp_internal(inet *a1, inet *a2)
Definition: network.c:364
#define PG_GETARG_INET_PP(n)
Definition: inet.h:124
Definition: inet.h:52
static FormData_pg_attribute a1
Definition: heap.c:144
static FormData_pg_attribute a2
Definition: heap.c:150

◆ network_le()

Datum network_le ( PG_FUNCTION_ARGS  )

Definition at line 405 of file network.c.

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

406 {
407  inet *a1 = PG_GETARG_INET_PP(0);
408  inet *a2 = PG_GETARG_INET_PP(1);
409 
410  PG_RETURN_BOOL(network_cmp_internal(a1, a2) <= 0);
411 }
static int32 network_cmp_internal(inet *a1, inet *a2)
Definition: network.c:364
#define PG_GETARG_INET_PP(n)
Definition: inet.h:124
Definition: inet.h:52
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
static FormData_pg_attribute a1
Definition: heap.c:144
static FormData_pg_attribute a2
Definition: heap.c:150

◆ network_lt()

Datum network_lt ( PG_FUNCTION_ARGS  )

Definition at line 396 of file network.c.

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

397 {
398  inet *a1 = PG_GETARG_INET_PP(0);
399  inet *a2 = PG_GETARG_INET_PP(1);
400 
402 }
static int32 network_cmp_internal(inet *a1, inet *a2)
Definition: network.c:364
#define PG_GETARG_INET_PP(n)
Definition: inet.h:124
Definition: inet.h:52
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
static FormData_pg_attribute a1
Definition: heap.c:144
static FormData_pg_attribute a2
Definition: heap.c:150

◆ network_masklen()

Datum network_masklen ( PG_FUNCTION_ARGS  )

Definition at line 666 of file network.c.

References ip_bits, PG_GETARG_INET_PP, and PG_RETURN_INT32.

667 {
668  inet *ip = PG_GETARG_INET_PP(0);
669 
671 }
#define ip_bits(inetptr)
Definition: inet.h:74
#define PG_RETURN_INT32(x)
Definition: fmgr.h:314
#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 441 of file network.c.

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

442 {
443  inet *a1 = PG_GETARG_INET_PP(0);
444  inet *a2 = PG_GETARG_INET_PP(1);
445 
446  PG_RETURN_BOOL(network_cmp_internal(a1, a2) != 0);
447 }
static int32 network_cmp_internal(inet *a1, inet *a2)
Definition: network.c:364
#define PG_GETARG_INET_PP(n)
Definition: inet.h:124
Definition: inet.h:52
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
static FormData_pg_attribute a1
Definition: heap.c:144
static FormData_pg_attribute a2
Definition: heap.c:150

◆ network_netmask()

Datum network_netmask ( PG_FUNCTION_ARGS  )

Definition at line 782 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.

783 {
784  inet *ip = PG_GETARG_INET_PP(0);
785  inet *dst;
786  int byte;
787  int bits;
788  unsigned char mask;
789  unsigned char *b;
790 
791  /* make sure any unused bits are zeroed */
792  dst = (inet *) palloc0(sizeof(inet));
793 
794  bits = ip_bits(ip);
795  b = ip_addr(dst);
796 
797  byte = 0;
798 
799  while (bits)
800  {
801  if (bits >= 8)
802  {
803  mask = 0xff;
804  bits -= 8;
805  }
806  else
807  {
808  mask = 0xff << (8 - bits);
809  bits = 0;
810  }
811 
812  b[byte] = mask;
813  byte++;
814  }
815 
816  ip_family(dst) = ip_family(ip);
817  ip_bits(dst) = ip_maxbits(ip);
818  SET_INET_VARSIZE(dst);
819 
820  PG_RETURN_INET_P(dst);
821 }
#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:877

◆ network_network()

Datum network_network ( PG_FUNCTION_ARGS  )

Definition at line 738 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().

739 {
740  inet *ip = PG_GETARG_INET_PP(0);
741  inet *dst;
742  int byte;
743  int bits;
744  unsigned char mask;
745  unsigned char *a,
746  *b;
747 
748  /* make sure any unused bits are zeroed */
749  dst = (inet *) palloc0(sizeof(inet));
750 
751  bits = ip_bits(ip);
752  a = ip_addr(ip);
753  b = ip_addr(dst);
754 
755  byte = 0;
756 
757  while (bits)
758  {
759  if (bits >= 8)
760  {
761  mask = 0xff;
762  bits -= 8;
763  }
764  else
765  {
766  mask = 0xff << (8 - bits);
767  bits = 0;
768  }
769 
770  b[byte] = a[byte] & mask;
771  byte++;
772  }
773 
774  ip_family(dst) = ip_family(ip);
775  ip_bits(dst) = ip_bits(ip);
776  SET_INET_VARSIZE(dst);
777 
778  PG_RETURN_INET_P(dst);
779 }
#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:877

◆ network_out()

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

Definition at line 100 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().

101 {
102  char tmp[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255/128")];
103  char *dst;
104  int len;
105 
106  dst = inet_net_ntop(ip_family(src), ip_addr(src), ip_bits(src),
107  tmp, sizeof(tmp));
108  if (dst == NULL)
109  ereport(ERROR,
110  (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
111  errmsg("could not format inet value: %m")));
112 
113  /* For CIDR, add /n if not present */
114  if (is_cidr && strchr(tmp, '/') == NULL)
115  {
116  len = strlen(tmp);
117  snprintf(tmp + len, sizeof(tmp) - len, "/%u", ip_bits(src));
118  }
119 
120  return pstrdup(tmp);
121 }
#define ip_bits(inetptr)
Definition: inet.h:74
#define ip_family(inetptr)
Definition: inet.h:71
char * pstrdup(const char *in)
Definition: mcxt.c:1076
#define ip_addr(inetptr)
Definition: inet.h:77
int errcode(int sqlerrcode)
Definition: elog.c:575
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
#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:122
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ network_overlap()

Datum network_overlap ( PG_FUNCTION_ARGS  )

Definition at line 563 of file network.c.

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

564 {
565  inet *a1 = PG_GETARG_INET_PP(0);
566  inet *a2 = PG_GETARG_INET_PP(1);
567 
568  if (ip_family(a1) == ip_family(a2))
569  {
571  Min(ip_bits(a1), ip_bits(a2))) == 0);
572  }
573 
574  PG_RETURN_BOOL(false);
575 }
#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:802
#define PG_GETARG_INET_PP(n)
Definition: inet.h:124
Definition: inet.h:52
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
static FormData_pg_attribute a1
Definition: heap.c:144
int bitncmp(const unsigned char *l, const unsigned char *r, int n)
Definition: network.c:980
static FormData_pg_attribute a2
Definition: heap.c:150

◆ network_recv()

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

Definition at line 151 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().

152 {
153  inet *addr;
154  char *addrptr;
155  int bits;
156  int nb,
157  i;
158 
159  /* make sure any unused bits in a CIDR value are zeroed */
160  addr = (inet *) palloc0(sizeof(inet));
161 
162  ip_family(addr) = pq_getmsgbyte(buf);
163  if (ip_family(addr) != PGSQL_AF_INET &&
164  ip_family(addr) != PGSQL_AF_INET6)
165  ereport(ERROR,
166  (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
167  /* translator: %s is inet or cidr */
168  errmsg("invalid address family in external \"%s\" value",
169  is_cidr ? "cidr" : "inet")));
170  bits = pq_getmsgbyte(buf);
171  if (bits < 0 || bits > ip_maxbits(addr))
172  ereport(ERROR,
173  (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
174  /* translator: %s is inet or cidr */
175  errmsg("invalid bits in external \"%s\" value",
176  is_cidr ? "cidr" : "inet")));
177  ip_bits(addr) = bits;
178  i = pq_getmsgbyte(buf); /* ignore is_cidr */
179  nb = pq_getmsgbyte(buf);
180  if (nb != ip_addrsize(addr))
181  ereport(ERROR,
182  (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
183  /* translator: %s is inet or cidr */
184  errmsg("invalid length in external \"%s\" value",
185  is_cidr ? "cidr" : "inet")));
186 
187  addrptr = (char *) ip_addr(addr);
188  for (i = 0; i < nb; i++)
189  addrptr[i] = pq_getmsgbyte(buf);
190 
191  /*
192  * Error check: CIDR values must not have any bits set beyond the masklen.
193  */
194  if (is_cidr)
195  {
196  if (!addressOK(ip_addr(addr), bits, ip_family(addr)))
197  ereport(ERROR,
198  (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
199  errmsg("invalid external \"cidr\" value"),
200  errdetail("Value has bits set to right of mask.")));
201  }
202 
203  SET_INET_VARSIZE(addr);
204 
205  return addr;
206 }
#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:1052
#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:575
#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:873
Definition: inet.h:52
#define ereport(elevel, rest)
Definition: elog.h:122
void * palloc0(Size size)
Definition: mcxt.c:877
int pq_getmsgbyte(StringInfo msg)
Definition: pqformat.c:401
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i

◆ network_scan_first()

Datum network_scan_first ( Datum  in)

Definition at line 1101 of file network.c.

References DirectFunctionCall1, and network_network().

Referenced by network_prefix_quals().

1102 {
1104 }
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:585
Datum network_network(PG_FUNCTION_ARGS)
Definition: network.c:738

◆ network_scan_last()

Datum network_scan_last ( Datum  in)

Definition at line 1115 of file network.c.

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

Referenced by network_prefix_quals().

1116 {
1119  Int32GetDatum(-1));
1120 }
Datum network_broadcast(PG_FUNCTION_ARGS)
Definition: network.c:693
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:585
Datum inet_set_masklen(PG_FUNCTION_ARGS)
Definition: network.c:283
#define Int32GetDatum(X)
Definition: postgres.h:485
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:587

◆ network_send()

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

Definition at line 229 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().

230 {
232  char *addrptr;
233  int nb,
234  i;
235 
236  pq_begintypsend(&buf);
237  pq_sendbyte(&buf, ip_family(addr));
238  pq_sendbyte(&buf, ip_bits(addr));
239  pq_sendbyte(&buf, is_cidr);
240  nb = ip_addrsize(addr);
241  if (nb < 0)
242  nb = 0;
243  pq_sendbyte(&buf, nb);
244  addrptr = (char *) ip_addr(addr);
245  for (i = 0; i < nb; i++)
246  pq_sendbyte(&buf, addrptr[i]);
247  return pq_endtypsend(&buf);
248 }
#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
static void pq_sendbyte(StringInfo buf, int8 byt)
Definition: pqformat.h:164
bytea * pq_endtypsend(StringInfo buf)
Definition: pqformat.c:348
static char * buf
Definition: pg_test_fsync.c:67
int i

◆ network_show()

Datum network_show ( PG_FUNCTION_ARGS  )

Definition at line 607 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().

608 {
609  inet *ip = PG_GETARG_INET_PP(0);
610  int len;
611  char tmp[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255/128")];
612 
613  if (inet_net_ntop(ip_family(ip), ip_addr(ip), ip_maxbits(ip),
614  tmp, sizeof(tmp)) == NULL)
615  ereport(ERROR,
616  (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
617  errmsg("could not format inet value: %m")));
618 
619  /* Add /n if not present (which it won't be) */
620  if (strchr(tmp, '/') == NULL)
621  {
622  len = strlen(tmp);
623  snprintf(tmp + len, sizeof(tmp) - len, "/%u", ip_bits(ip));
624  }
625 
627 }
#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:575
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
#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:122
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:331
text * cstring_to_text(const char *s)
Definition: varlena.c:149
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ network_smaller()

Datum network_smaller ( PG_FUNCTION_ARGS  )

Definition at line 453 of file network.c.

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

454 {
455  inet *a1 = PG_GETARG_INET_PP(0);
456  inet *a2 = PG_GETARG_INET_PP(1);
457 
458  if (network_cmp_internal(a1, a2) < 0)
459  PG_RETURN_INET_P(a1);
460  else
461  PG_RETURN_INET_P(a2);
462 }
#define PG_RETURN_INET_P(x)
Definition: inet.h:125
static int32 network_cmp_internal(inet *a1, inet *a2)
Definition: network.c:364
#define PG_GETARG_INET_PP(n)
Definition: inet.h:124
Definition: inet.h:52
static FormData_pg_attribute a1
Definition: heap.c:144
static FormData_pg_attribute a2
Definition: heap.c:150

◆ network_sub()

Datum network_sub ( PG_FUNCTION_ARGS  )

Definition at line 503 of file network.c.

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

504 {
505  inet *a1 = PG_GETARG_INET_PP(0);
506  inet *a2 = PG_GETARG_INET_PP(1);
507 
508  if (ip_family(a1) == ip_family(a2))
509  {
510  PG_RETURN_BOOL(ip_bits(a1) > ip_bits(a2) &&
511  bitncmp(ip_addr(a1), ip_addr(a2), ip_bits(a2)) == 0);
512  }
513 
514  PG_RETURN_BOOL(false);
515 }
#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 PG_GETARG_INET_PP(n)
Definition: inet.h:124
Definition: inet.h:52
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
static FormData_pg_attribute a1
Definition: heap.c:144
int bitncmp(const unsigned char *l, const unsigned char *r, int n)
Definition: network.c:980
static FormData_pg_attribute a2
Definition: heap.c:150

◆ network_subeq()

Datum network_subeq ( PG_FUNCTION_ARGS  )

Definition at line 518 of file network.c.

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

519 {
520  inet *a1 = PG_GETARG_INET_PP(0);
521  inet *a2 = PG_GETARG_INET_PP(1);
522 
523  if (ip_family(a1) == ip_family(a2))
524  {
525  PG_RETURN_BOOL(ip_bits(a1) >= ip_bits(a2) &&
526  bitncmp(ip_addr(a1), ip_addr(a2), ip_bits(a2)) == 0);
527  }
528 
529  PG_RETURN_BOOL(false);
530 }
#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 PG_GETARG_INET_PP(n)
Definition: inet.h:124
Definition: inet.h:52
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
static FormData_pg_attribute a1
Definition: heap.c:144
int bitncmp(const unsigned char *l, const unsigned char *r, int n)
Definition: network.c:980
static FormData_pg_attribute a2
Definition: heap.c:150

◆ network_sup()

Datum network_sup ( PG_FUNCTION_ARGS  )

Definition at line 533 of file network.c.

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

534 {
535  inet *a1 = PG_GETARG_INET_PP(0);
536  inet *a2 = PG_GETARG_INET_PP(1);
537 
538  if (ip_family(a1) == ip_family(a2))
539  {
540  PG_RETURN_BOOL(ip_bits(a1) < ip_bits(a2) &&
541  bitncmp(ip_addr(a1), ip_addr(a2), ip_bits(a1)) == 0);
542  }
543 
544  PG_RETURN_BOOL(false);
545 }
#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 PG_GETARG_INET_PP(n)
Definition: inet.h:124
Definition: inet.h:52
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
static FormData_pg_attribute a1
Definition: heap.c:144
int bitncmp(const unsigned char *l, const unsigned char *r, int n)
Definition: network.c:980
static FormData_pg_attribute a2
Definition: heap.c:150

◆ network_supeq()

Datum network_supeq ( PG_FUNCTION_ARGS  )

Definition at line 548 of file network.c.

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

549 {
550  inet *a1 = PG_GETARG_INET_PP(0);
551  inet *a2 = PG_GETARG_INET_PP(1);
552 
553  if (ip_family(a1) == ip_family(a2))
554  {
555  PG_RETURN_BOOL(ip_bits(a1) <= ip_bits(a2) &&
556  bitncmp(ip_addr(a1), ip_addr(a2), ip_bits(a1)) == 0);
557  }
558 
559  PG_RETURN_BOOL(false);
560 }
#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 PG_GETARG_INET_PP(n)
Definition: inet.h:124
Definition: inet.h:52
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
static FormData_pg_attribute a1
Definition: heap.c:144
int bitncmp(const unsigned char *l, const unsigned char *r, int n)
Definition: network.c:980
static FormData_pg_attribute a2
Definition: heap.c:150