PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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 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

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

Definition at line 1032 of file network.c.

References Assert, byte, and PGSQL_AF_INET.

Referenced by network_in(), and network_recv().

1033 {
1034  int byte;
1035  int nbits;
1036  int maxbits;
1037  int maxbytes;
1038  unsigned char mask;
1039 
1040  if (family == PGSQL_AF_INET)
1041  {
1042  maxbits = 32;
1043  maxbytes = 4;
1044  }
1045  else
1046  {
1047  maxbits = 128;
1048  maxbytes = 16;
1049  }
1050  Assert(bits <= maxbits);
1051 
1052  if (bits == maxbits)
1053  return true;
1054 
1055  byte = bits / 8;
1056 
1057  nbits = bits % 8;
1058  mask = 0xff;
1059  if (bits != 0)
1060  mask >>= nbits;
1061 
1062  while (byte < maxbytes)
1063  {
1064  if ((a[byte] & mask) != 0)
1065  return false;
1066  mask = 0xff;
1067  byte++;
1068  }
1069 
1070  return true;
1071 }
#define PGSQL_AF_INET
Definition: inet.h:39
#define byte(x, n)
Definition: rijndael.c:68
#define Assert(condition)
Definition: c.h:667
int bitncmp ( const unsigned char *  l,
const unsigned char *  r,
int  n 
)

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

961 {
962  unsigned int lb,
963  rb;
964  int x,
965  b;
966 
967  b = n / 8;
968  x = memcmp(l, r, b);
969  if (x || (n % 8) == 0)
970  return x;
971 
972  lb = l[b];
973  rb = r[b];
974  for (b = n % 8; b > 0; b--)
975  {
976  if (IS_HIGHBIT_SET(lb) != IS_HIGHBIT_SET(rb))
977  {
978  if (IS_HIGHBIT_SET(lb))
979  return 1;
980  return -1;
981  }
982  lb <<= 1;
983  rb <<= 1;
984  }
985  return 0;
986 }
#define IS_HIGHBIT_SET(ch)
Definition: c.h:949
int bitncommon ( const unsigned char *  l,
const unsigned char *  r,
int  n 
)

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

995 {
996  int byte,
997  nbits;
998 
999  /* number of bits to examine in last byte */
1000  nbits = n % 8;
1001 
1002  /* check whole bytes */
1003  for (byte = 0; byte < n / 8; byte++)
1004  {
1005  if (l[byte] != r[byte])
1006  {
1007  /* at least one bit in the last byte is not common */
1008  nbits = 7;
1009  break;
1010  }
1011  }
1012 
1013  /* check bits in last partial byte */
1014  if (nbits != 0)
1015  {
1016  /* calculate diff of first non-matching bytes */
1017  unsigned int diff = l[byte] ^ r[byte];
1018 
1019  /* compare the bits from the most to the least */
1020  while ((diff >> (8 - nbits)) != 0)
1021  nbits--;
1022  }
1023 
1024  return (8 * byte) + nbits;
1025 }
#define byte(x, n)
Definition: rijndael.c:68
Datum cidr_abbrev ( PG_FUNCTION_ARGS  )

Definition at line 638 of file network.c.

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

639 {
640  inet *ip = PG_GETARG_INET_PP(0);
641  char *dst;
642  char tmp[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255/128")];
643 
644  dst = inet_cidr_ntop(ip_family(ip), ip_addr(ip),
645  ip_bits(ip), tmp, sizeof(tmp));
646 
647  if (dst == NULL)
648  ereport(ERROR,
649  (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
650  errmsg("could not format cidr value: %m")));
651 
653 }
#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:111
Definition: inet.h:52
#define ereport(elevel, rest)
Definition: elog.h:122
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:314
text * cstring_to_text(const char *s)
Definition: varlena.c:150
#define NULL
Definition: c.h:226
int errmsg(const char *fmt,...)
Definition: elog.c:797
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:112
static inet * network_in(char *src, bool is_cidr)
Definition: network.c:34
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:233
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:111
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:306
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:112
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:232
static char * buf
Definition: pg_test_fsync.c:65
static inet * network_recv(StringInfo buf, bool is_cidr)
Definition: network.c:151
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:313
#define PG_GETARG_INET_PP(n)
Definition: inet.h:111
Definition: inet.h:52
static bytea * network_send(inet *addr, bool is_cidr)
Definition: network.c:229
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:225
#define PG_RETURN_INET_P(x)
Definition: inet.h:112
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:111
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
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:920
#define Assert(condition)
Definition: c.h:667
void clean_ipv6_addr ( int  addr_family,
char *  addr 
)

Definition at line 1494 of file network.c.

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

1495 {
1496 #ifdef HAVE_IPV6
1497  if (addr_family == AF_INET6)
1498  {
1499  char *pct = strchr(addr, '%');
1500 
1501  if (pct)
1502  *pct = '\0';
1503  }
1504 #endif
1505 }
double convert_network_to_scalar ( Datum  value,
Oid  typid 
)

Definition at line 897 of file network.c.

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

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

898 {
899  switch (typid)
900  {
901  case INETOID:
902  case CIDROID:
903  {
904  inet *ip = DatumGetInetPP(value);
905  int len;
906  double res;
907  int i;
908 
909  /*
910  * Note that we don't use the full address for IPv6.
911  */
912  if (ip_family(ip) == PGSQL_AF_INET)
913  len = 4;
914  else
915  len = 5;
916 
917  res = ip_family(ip);
918  for (i = 0; i < len; i++)
919  {
920  res *= 256;
921  res += ip_addr(ip)[i];
922  }
923  return res;
924 
925  break;
926  }
927  case MACADDROID:
928  {
930  double res;
931 
932  res = (mac->a << 16) | (mac->b << 8) | (mac->c);
933  res *= 256 * 256 * 256;
934  res += (mac->d << 16) | (mac->e << 8) | (mac->f);
935  return res;
936  }
937  }
938 
939  /*
940  * Can't get here unless someone tries to use scalarltsel/scalargtsel on
941  * an operator with one network and one non-network operand.
942  */
943  elog(ERROR, "unsupported type: %u", typid);
944  return 0;
945 }
#define CIDROID
Definition: pg_type.h:443
static struct @77 value
#define DatumGetMacaddrP(X)
Definition: inet.h:114
#define ip_family(inetptr)
Definition: inet.h:71
#define PGSQL_AF_INET
Definition: inet.h:39
#define INETOID
Definition: pg_type.h:440
#define DatumGetInetPP(X)
Definition: inet.h:108
unsigned char f
Definition: inet.h:101
#define ip_addr(inetptr)
Definition: inet.h:77
unsigned char c
Definition: inet.h:98
#define ERROR
Definition: elog.h:43
unsigned char a
Definition: inet.h:96
unsigned char d
Definition: inet.h:99
Definition: inet.h:52
unsigned char b
Definition: inet.h:97
unsigned char e
Definition: inet.h:100
Definition: inet.h:94
int i
#define MACADDROID
Definition: pg_type.h:437
#define elog
Definition: elog.h:219
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:349
#define ip_addrsize(inetptr)
Definition: inet.h:80
#define PG_GETARG_INET_PP(n)
Definition: inet.h:111
Definition: inet.h:52
Datum hash_any(register const unsigned char *k, register int keylen)
Definition: hashfunc.c:308
Datum inet_abbrev ( PG_FUNCTION_ARGS  )

Definition at line 620 of file network.c.

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

621 {
622  inet *ip = PG_GETARG_INET_PP(0);
623  char *dst;
624  char tmp[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255/128")];
625 
626  dst = inet_net_ntop(ip_family(ip), ip_addr(ip),
627  ip_bits(ip), tmp, sizeof(tmp));
628 
629  if (dst == NULL)
630  ereport(ERROR,
631  (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
632  errmsg("could not format inet value: %m")));
633 
635 }
#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:111
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:78
#define ereport(elevel, rest)
Definition: elog.h:122
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:314
text * cstring_to_text(const char *s)
Definition: varlena.c:150
#define NULL
Definition: c.h:226
int errmsg(const char *fmt,...)
Definition: elog.c:797
Datum inet_client_addr ( PG_FUNCTION_ARGS  )

Definition at line 1107 of file network.c.

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

1108 {
1109  Port *port = MyProcPort;
1110  char remote_host[NI_MAXHOST];
1111  int ret;
1112 
1113  if (port == NULL)
1114  PG_RETURN_NULL();
1115 
1116  switch (port->raddr.addr.ss_family)
1117  {
1118  case AF_INET:
1119 #ifdef HAVE_IPV6
1120  case AF_INET6:
1121 #endif
1122  break;
1123  default:
1124  PG_RETURN_NULL();
1125  }
1126 
1127  remote_host[0] = '\0';
1128 
1129  ret = pg_getnameinfo_all(&port->raddr.addr, port->raddr.salen,
1130  remote_host, sizeof(remote_host),
1131  NULL, 0,
1133  if (ret != 0)
1134  PG_RETURN_NULL();
1135 
1136  clean_ipv6_addr(port->raddr.addr.ss_family, remote_host);
1137 
1138  PG_RETURN_INET_P(network_in(remote_host, false));
1139 }
#define NI_NUMERICHOST
Definition: getaddrinfo.h:80
struct Port * MyProcPort
Definition: globals.c:40
struct sockaddr_storage addr
Definition: pqcomm.h:64
#define PG_RETURN_INET_P(x)
Definition: inet.h:112
Definition: libpq-be.h:118
static inet * network_in(char *src, bool is_cidr)
Definition: network.c:34
SockAddr raddr
Definition: libpq-be.h:124
#define NI_MAXHOST
Definition: getaddrinfo.h:90
ACCEPT_TYPE_ARG3 salen
Definition: pqcomm.h:65
static int port
Definition: pg_regress.c:87
#define NI_NUMERICSERV
Definition: getaddrinfo.h:83
#define NULL
Definition: c.h:226
int pg_getnameinfo_all(const struct sockaddr_storage *addr, int salen, char *node, int nodelen, char *service, int servicelen, int flags)
Definition: ip.c:123
void clean_ipv6_addr(int addr_family, char *addr)
Definition: network.c:1494
#define PG_RETURN_NULL()
Definition: fmgr.h:289
Datum inet_client_port ( PG_FUNCTION_ARGS  )

Definition at line 1146 of file network.c.

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

1147 {
1148  Port *port = MyProcPort;
1149  char remote_port[NI_MAXSERV];
1150  int ret;
1151 
1152  if (port == NULL)
1153  PG_RETURN_NULL();
1154 
1155  switch (port->raddr.addr.ss_family)
1156  {
1157  case AF_INET:
1158 #ifdef HAVE_IPV6
1159  case AF_INET6:
1160 #endif
1161  break;
1162  default:
1163  PG_RETURN_NULL();
1164  }
1165 
1166  remote_port[0] = '\0';
1167 
1168  ret = pg_getnameinfo_all(&port->raddr.addr, port->raddr.salen,
1169  NULL, 0,
1170  remote_port, sizeof(remote_port),
1172  if (ret != 0)
1173  PG_RETURN_NULL();
1174 
1176 }
#define NI_NUMERICHOST
Definition: getaddrinfo.h:80
struct Port * MyProcPort
Definition: globals.c:40
Datum int4in(PG_FUNCTION_ARGS)
Definition: int.c:281
struct sockaddr_storage addr
Definition: pqcomm.h:64
Definition: libpq-be.h:118
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:548
SockAddr raddr
Definition: libpq-be.h:124
#define CStringGetDatum(X)
Definition: postgres.h:586
#define NI_MAXSERV
Definition: getaddrinfo.h:93
ACCEPT_TYPE_ARG3 salen
Definition: pqcomm.h:65
static int port
Definition: pg_regress.c:87
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:297
#define NI_NUMERICSERV
Definition: getaddrinfo.h:83
#define NULL
Definition: c.h:226
int pg_getnameinfo_all(const struct sockaddr_storage *addr, int salen, char *node, int nodelen, char *service, int servicelen, int flags)
Definition: ip.c:123
#define PG_RETURN_NULL()
Definition: fmgr.h:289
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:112
static inet * network_in(char *src, bool is_cidr)
Definition: network.c:34
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:233
Datum inet_merge ( PG_FUNCTION_ARGS  )

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

875 {
876  inet *a1 = PG_GETARG_INET_PP(0),
877  *a2 = PG_GETARG_INET_PP(1);
878  int commonbits;
879 
880  if (ip_family(a1) != ip_family(a2))
881  ereport(ERROR,
882  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
883  errmsg("cannot merge addresses from different families")));
884 
885  commonbits = bitncommon(ip_addr(a1), ip_addr(a2),
886  Min(ip_bits(a1), ip_bits(a2)));
887 
889 }
#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:798
#define PG_RETURN_INET_P(x)
Definition: inet.h:112
int bitncommon(const unsigned char *l, const unsigned char *r, int n)
Definition: network.c:994
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
#define PG_GETARG_INET_PP(n)
Definition: inet.h:111
Definition: inet.h:52
#define ereport(elevel, rest)
Definition: elog.h:122
static FormData_pg_attribute a1
Definition: heap.c:138
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:144
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:111
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:306
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:112
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:232
static char * buf
Definition: pg_test_fsync.c:65
static inet * network_recv(StringInfo buf, bool is_cidr)
Definition: network.c:151
Datum inet_same_family ( PG_FUNCTION_ARGS  )

Definition at line 862 of file network.c.

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

863 {
864  inet *a1 = PG_GETARG_INET_PP(0);
865  inet *a2 = PG_GETARG_INET_PP(1);
866 
867  PG_RETURN_BOOL(ip_family(a1) == ip_family(a2));
868 }
#define ip_family(inetptr)
Definition: inet.h:71
#define PG_GETARG_INET_PP(n)
Definition: inet.h:111
Definition: inet.h:52
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
static FormData_pg_attribute a1
Definition: heap.c:138
static FormData_pg_attribute a2
Definition: heap.c:144
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:313
#define PG_GETARG_INET_PP(n)
Definition: inet.h:111
Definition: inet.h:52
static bytea * network_send(inet *addr, bool is_cidr)
Definition: network.c:229
Datum inet_server_addr ( PG_FUNCTION_ARGS  )

Definition at line 1183 of file network.c.

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

1184 {
1185  Port *port = MyProcPort;
1186  char local_host[NI_MAXHOST];
1187  int ret;
1188 
1189  if (port == NULL)
1190  PG_RETURN_NULL();
1191 
1192  switch (port->laddr.addr.ss_family)
1193  {
1194  case AF_INET:
1195 #ifdef HAVE_IPV6
1196  case AF_INET6:
1197 #endif
1198  break;
1199  default:
1200  PG_RETURN_NULL();
1201  }
1202 
1203  local_host[0] = '\0';
1204 
1205  ret = pg_getnameinfo_all(&port->laddr.addr, port->laddr.salen,
1206  local_host, sizeof(local_host),
1207  NULL, 0,
1209  if (ret != 0)
1210  PG_RETURN_NULL();
1211 
1212  clean_ipv6_addr(port->laddr.addr.ss_family, local_host);
1213 
1214  PG_RETURN_INET_P(network_in(local_host, false));
1215 }
#define NI_NUMERICHOST
Definition: getaddrinfo.h:80
struct Port * MyProcPort
Definition: globals.c:40
struct sockaddr_storage addr
Definition: pqcomm.h:64
#define PG_RETURN_INET_P(x)
Definition: inet.h:112
Definition: libpq-be.h:118
static inet * network_in(char *src, bool is_cidr)
Definition: network.c:34
#define NI_MAXHOST
Definition: getaddrinfo.h:90
ACCEPT_TYPE_ARG3 salen
Definition: pqcomm.h:65
SockAddr laddr
Definition: libpq-be.h:123
static int port
Definition: pg_regress.c:87
#define NI_NUMERICSERV
Definition: getaddrinfo.h:83
#define NULL
Definition: c.h:226
int pg_getnameinfo_all(const struct sockaddr_storage *addr, int salen, char *node, int nodelen, char *service, int servicelen, int flags)
Definition: ip.c:123
void clean_ipv6_addr(int addr_family, char *addr)
Definition: network.c:1494
#define PG_RETURN_NULL()
Definition: fmgr.h:289
Datum inet_server_port ( PG_FUNCTION_ARGS  )

Definition at line 1222 of file network.c.

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

1223 {
1224  Port *port = MyProcPort;
1225  char local_port[NI_MAXSERV];
1226  int ret;
1227 
1228  if (port == NULL)
1229  PG_RETURN_NULL();
1230 
1231  switch (port->laddr.addr.ss_family)
1232  {
1233  case AF_INET:
1234 #ifdef HAVE_IPV6
1235  case AF_INET6:
1236 #endif
1237  break;
1238  default:
1239  PG_RETURN_NULL();
1240  }
1241 
1242  local_port[0] = '\0';
1243 
1244  ret = pg_getnameinfo_all(&port->laddr.addr, port->laddr.salen,
1245  NULL, 0,
1246  local_port, sizeof(local_port),
1248  if (ret != 0)
1249  PG_RETURN_NULL();
1250 
1252 }
#define NI_NUMERICHOST
Definition: getaddrinfo.h:80
struct Port * MyProcPort
Definition: globals.c:40
Datum int4in(PG_FUNCTION_ARGS)
Definition: int.c:281
struct sockaddr_storage addr
Definition: pqcomm.h:64
Definition: libpq-be.h:118
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:548
#define CStringGetDatum(X)
Definition: postgres.h:586
#define NI_MAXSERV
Definition: getaddrinfo.h:93
ACCEPT_TYPE_ARG3 salen
Definition: pqcomm.h:65
SockAddr laddr
Definition: libpq-be.h:123
static int port
Definition: pg_regress.c:87
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:297
#define NI_NUMERICSERV
Definition: getaddrinfo.h:83
#define NULL
Definition: c.h:226
int pg_getnameinfo_all(const struct sockaddr_storage *addr, int salen, char *node, int nodelen, char *service, int servicelen, int flags)
Definition: ip.c:123
#define PG_RETURN_NULL()
Definition: fmgr.h:289
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:225
#define ip_bits(inetptr)
Definition: inet.h:74
#define PG_RETURN_INET_P(x)
Definition: inet.h:112
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:111
Definition: inet.h:52
#define ereport(elevel, rest)
Definition: elog.h:122
#define VARSIZE_ANY(PTR)
Definition: postgres.h:336
void * palloc(Size size)
Definition: mcxt.c:891
int errmsg(const char *fmt,...)
Definition: elog.c:797
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:112
#define ip_maxbits(inetptr)
Definition: inet.h:83
#define ERROR
Definition: elog.h:43
#define PG_GETARG_INET_PP(n)
Definition: inet.h:111
Definition: inet.h:52
inet * cidr_set_masklen_internal(const inet *src, int bits)
Definition: network.c:327
#define elog
Definition: elog.h:219
Datum inetand ( PG_FUNCTION_ARGS  )

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

1282 {
1283  inet *ip = PG_GETARG_INET_PP(0);
1284  inet *ip2 = PG_GETARG_INET_PP(1);
1285  inet *dst;
1286 
1287  dst = (inet *) palloc0(sizeof(inet));
1288 
1289  if (ip_family(ip) != ip_family(ip2))
1290  ereport(ERROR,
1291  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1292  errmsg("cannot AND inet values of different sizes")));
1293  else
1294  {
1295  int nb = ip_addrsize(ip);
1296  unsigned char *pip = ip_addr(ip);
1297  unsigned char *pip2 = ip_addr(ip2);
1298  unsigned char *pdst = ip_addr(dst);
1299 
1300  while (nb-- > 0)
1301  pdst[nb] = pip[nb] & pip2[nb];
1302  }
1303  ip_bits(dst) = Max(ip_bits(ip), ip_bits(ip2));
1304 
1305  ip_family(dst) = ip_family(ip);
1306  SET_INET_VARSIZE(dst);
1307 
1308  PG_RETURN_INET_P(dst);
1309 }
#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:112
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:111
Definition: inet.h:52
#define ereport(elevel, rest)
Definition: elog.h:122
void * palloc0(Size size)
Definition: mcxt.c:920
#define Max(x, y)
Definition: c.h:792
int errmsg(const char *fmt,...)
Definition: elog.c:797
Datum inetmi ( PG_FUNCTION_ARGS  )

Definition at line 1417 of file network.c.

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

1418 {
1419  inet *ip = PG_GETARG_INET_PP(0);
1420  inet *ip2 = PG_GETARG_INET_PP(1);
1421  int64 res = 0;
1422 
1423  if (ip_family(ip) != ip_family(ip2))
1424  ereport(ERROR,
1425  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1426  errmsg("cannot subtract inet values of different sizes")));
1427  else
1428  {
1429  /*
1430  * We form the difference using the traditional complement, increment,
1431  * and add rule, with the increment part being handled by starting the
1432  * carry off at 1. If you don't think integer arithmetic is done in
1433  * two's complement, too bad.
1434  */
1435  int nb = ip_addrsize(ip);
1436  int byte = 0;
1437  unsigned char *pip = ip_addr(ip);
1438  unsigned char *pip2 = ip_addr(ip2);
1439  int carry = 1;
1440 
1441  while (nb-- > 0)
1442  {
1443  int lobyte;
1444 
1445  carry = pip[nb] + (~pip2[nb] & 0xFF) + carry;
1446  lobyte = carry & 0xFF;
1447  if (byte < sizeof(int64))
1448  {
1449  res |= ((int64) lobyte) << (byte * 8);
1450  }
1451  else
1452  {
1453  /*
1454  * Input wider than int64: check for overflow. All bytes to
1455  * the left of what will fit should be 0 or 0xFF, depending on
1456  * sign of the now-complete result.
1457  */
1458  if ((res < 0) ? (lobyte != 0xFF) : (lobyte != 0))
1459  ereport(ERROR,
1460  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1461  errmsg("result is out of range")));
1462  }
1463  carry >>= 8;
1464  byte++;
1465  }
1466 
1467  /*
1468  * If input is narrower than int64, overflow is not possible, but we
1469  * have to do proper sign extension.
1470  */
1471  if (carry == 0 && byte < sizeof(int64))
1472  res |= ((int64) -1) << (byte * 8);
1473  }
1474 
1475  PG_RETURN_INT64(res);
1476 }
#define ip_family(inetptr)
Definition: inet.h:71
#define PG_RETURN_INT64(x)
Definition: fmgr.h:311
#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:111
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
Datum inetmi_int8 ( PG_FUNCTION_ARGS  )

Definition at line 1407 of file network.c.

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

1408 {
1409  inet *ip = PG_GETARG_INET_PP(0);
1410  int64 addend = PG_GETARG_INT64(1);
1411 
1412  PG_RETURN_INET_P(internal_inetpl(ip, -addend));
1413 }
#define PG_RETURN_INET_P(x)
Definition: inet.h:112
#define PG_GETARG_INET_PP(n)
Definition: inet.h:111
Definition: inet.h:52
static inet * internal_inetpl(inet *ip, int64 addend)
Definition: network.c:1345
#define PG_GETARG_INT64(n)
Definition: fmgr.h:238
Datum inetnot ( PG_FUNCTION_ARGS  )

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

1257 {
1258  inet *ip = PG_GETARG_INET_PP(0);
1259  inet *dst;
1260 
1261  dst = (inet *) palloc0(sizeof(inet));
1262 
1263  {
1264  int nb = ip_addrsize(ip);
1265  unsigned char *pip = ip_addr(ip);
1266  unsigned char *pdst = ip_addr(dst);
1267 
1268  while (nb-- > 0)
1269  pdst[nb] = ~pip[nb];
1270  }
1271  ip_bits(dst) = ip_bits(ip);
1272 
1273  ip_family(dst) = ip_family(ip);
1274  SET_INET_VARSIZE(dst);
1275 
1276  PG_RETURN_INET_P(dst);
1277 }
#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:112
#define ip_addrsize(inetptr)
Definition: inet.h:80
#define PG_GETARG_INET_PP(n)
Definition: inet.h:111
Definition: inet.h:52
void * palloc0(Size size)
Definition: mcxt.c:920
Datum inetor ( PG_FUNCTION_ARGS  )

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

1314 {
1315  inet *ip = PG_GETARG_INET_PP(0);
1316  inet *ip2 = PG_GETARG_INET_PP(1);
1317  inet *dst;
1318 
1319  dst = (inet *) palloc0(sizeof(inet));
1320 
1321  if (ip_family(ip) != ip_family(ip2))
1322  ereport(ERROR,
1323  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1324  errmsg("cannot OR inet values of different sizes")));
1325  else
1326  {
1327  int nb = ip_addrsize(ip);
1328  unsigned char *pip = ip_addr(ip);
1329  unsigned char *pip2 = ip_addr(ip2);
1330  unsigned char *pdst = ip_addr(dst);
1331 
1332  while (nb-- > 0)
1333  pdst[nb] = pip[nb] | pip2[nb];
1334  }
1335  ip_bits(dst) = Max(ip_bits(ip), ip_bits(ip2));
1336 
1337  ip_family(dst) = ip_family(ip);
1338  SET_INET_VARSIZE(dst);
1339 
1340  PG_RETURN_INET_P(dst);
1341 }
#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:112
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:111
Definition: inet.h:52
#define ereport(elevel, rest)
Definition: elog.h:122
void * palloc0(Size size)
Definition: mcxt.c:920
#define Max(x, y)
Definition: c.h:792
int errmsg(const char *fmt,...)
Definition: elog.c:797
Datum inetpl ( PG_FUNCTION_ARGS  )

Definition at line 1397 of file network.c.

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

1398 {
1399  inet *ip = PG_GETARG_INET_PP(0);
1400  int64 addend = PG_GETARG_INT64(1);
1401 
1402  PG_RETURN_INET_P(internal_inetpl(ip, addend));
1403 }
#define PG_RETURN_INET_P(x)
Definition: inet.h:112
#define PG_GETARG_INET_PP(n)
Definition: inet.h:111
Definition: inet.h:52
static inet * internal_inetpl(inet *ip, int64 addend)
Definition: network.c:1345
#define PG_GETARG_INT64(n)
Definition: fmgr.h:238
static inet * internal_inetpl ( inet ip,
int64  addend 
)
static

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

1346 {
1347  inet *dst;
1348 
1349  dst = (inet *) palloc0(sizeof(inet));
1350 
1351  {
1352  int nb = ip_addrsize(ip);
1353  unsigned char *pip = ip_addr(ip);
1354  unsigned char *pdst = ip_addr(dst);
1355  int carry = 0;
1356 
1357  while (nb-- > 0)
1358  {
1359  carry = pip[nb] + (int) (addend & 0xFF) + carry;
1360  pdst[nb] = (unsigned char) (carry & 0xFF);
1361  carry >>= 8;
1362 
1363  /*
1364  * We have to be careful about right-shifting addend because
1365  * right-shift isn't portable for negative values, while simply
1366  * dividing by 256 doesn't work (the standard rounding is in the
1367  * wrong direction, besides which there may be machines out there
1368  * that round the wrong way). So, explicitly clear the low-order
1369  * byte to remove any doubt about the correct result of the
1370  * division, and then divide rather than shift.
1371  */
1372  addend &= ~((int64) 0xFF);
1373  addend /= 0x100;
1374  }
1375 
1376  /*
1377  * At this point we should have addend and carry both zero if original
1378  * addend was >= 0, or addend -1 and carry 1 if original addend was <
1379  * 0. Anything else means overflow.
1380  */
1381  if (!((addend == 0 && carry == 0) ||
1382  (addend == -1 && carry == 1)))
1383  ereport(ERROR,
1384  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1385  errmsg("result is out of range")));
1386  }
1387 
1388  ip_bits(dst) = ip_bits(ip);
1389  ip_family(dst) = ip_family(ip);
1390  SET_INET_VARSIZE(dst);
1391 
1392  return dst;
1393 }
#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:920
int errmsg(const char *fmt,...)
Definition: elog.c:797
Datum network_broadcast ( PG_FUNCTION_ARGS  )

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

684 {
685  inet *ip = PG_GETARG_INET_PP(0);
686  inet *dst;
687  int byte;
688  int bits;
689  int maxbytes;
690  unsigned char mask;
691  unsigned char *a,
692  *b;
693 
694  /* make sure any unused bits are zeroed */
695  dst = (inet *) palloc0(sizeof(inet));
696 
697  maxbytes = ip_addrsize(ip);
698  bits = ip_bits(ip);
699  a = ip_addr(ip);
700  b = ip_addr(dst);
701 
702  for (byte = 0; byte < maxbytes; byte++)
703  {
704  if (bits >= 8)
705  {
706  mask = 0x00;
707  bits -= 8;
708  }
709  else if (bits == 0)
710  mask = 0xff;
711  else
712  {
713  mask = 0xff >> bits;
714  bits = 0;
715  }
716 
717  b[byte] = a[byte] | mask;
718  }
719 
720  ip_family(dst) = ip_family(ip);
721  ip_bits(dst) = ip_bits(ip);
722  SET_INET_VARSIZE(dst);
723 
724  PG_RETURN_INET_P(dst);
725 }
#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:112
#define ip_addrsize(inetptr)
Definition: inet.h:80
#define PG_GETARG_INET_PP(n)
Definition: inet.h:111
Definition: inet.h:52
#define byte(x, n)
Definition: rijndael.c:68
void * palloc0(Size size)
Definition: mcxt.c:920
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.

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:298
static int32 network_cmp_internal(inet *a1, inet *a2)
Definition: network.c:364
#define PG_GETARG_INET_PP(n)
Definition: inet.h:111
Definition: inet.h:52
static FormData_pg_attribute a1
Definition: heap.c:138
static FormData_pg_attribute a2
Definition: heap.c:144
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:798
#define ip_maxbits(inetptr)
Definition: inet.h:83
int bitncmp(const unsigned char *l, const unsigned char *r, int n)
Definition: network.c:960
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:111
Definition: inet.h:52
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
static FormData_pg_attribute a1
Definition: heap.c:138
static FormData_pg_attribute a2
Definition: heap.c:144
Datum network_family ( PG_FUNCTION_ARGS  )

Definition at line 664 of file network.c.

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

665 {
666  inet *ip = PG_GETARG_INET_PP(0);
667 
668  switch (ip_family(ip))
669  {
670  case PGSQL_AF_INET:
671  PG_RETURN_INT32(4);
672  break;
673  case PGSQL_AF_INET6:
674  PG_RETURN_INT32(6);
675  break;
676  default:
677  PG_RETURN_INT32(0);
678  break;
679  }
680 }
#define ip_family(inetptr)
Definition: inet.h:71
#define PGSQL_AF_INET
Definition: inet.h:39
#define PG_RETURN_INT32(x)
Definition: fmgr.h:298
#define PGSQL_AF_INET6
Definition: inet.h:40
#define PG_GETARG_INET_PP(n)
Definition: inet.h:111
Definition: inet.h:52
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:111
Definition: inet.h:52
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
static FormData_pg_attribute a1
Definition: heap.c:138
static FormData_pg_attribute a2
Definition: heap.c:144
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:111
Definition: inet.h:52
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
static FormData_pg_attribute a1
Definition: heap.c:138
static FormData_pg_attribute a2
Definition: heap.c:144
Datum network_host ( PG_FUNCTION_ARGS  )

Definition at line 571 of file network.c.

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

572 {
573  inet *ip = PG_GETARG_INET_PP(0);
574  char *ptr;
575  char tmp[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255/128")];
576 
577  /* force display of max bits, regardless of masklen... */
578  if (inet_net_ntop(ip_family(ip), ip_addr(ip), ip_maxbits(ip),
579  tmp, sizeof(tmp)) == NULL)
580  ereport(ERROR,
581  (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
582  errmsg("could not format inet value: %m")));
583 
584  /* Suppress /n if present (shouldn't happen now) */
585  if ((ptr = strchr(tmp, '/')) != NULL)
586  *ptr = '\0';
587 
589 }
#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:111
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:78
#define ereport(elevel, rest)
Definition: elog.h:122
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:314
text * cstring_to_text(const char *s)
Definition: varlena.c:150
#define NULL
Definition: c.h:226
int errmsg(const char *fmt,...)
Definition: elog.c:797
Datum network_hostmask ( PG_FUNCTION_ARGS  )

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

815 {
816  inet *ip = PG_GETARG_INET_PP(0);
817  inet *dst;
818  int byte;
819  int bits;
820  int maxbytes;
821  unsigned char mask;
822  unsigned char *b;
823 
824  /* make sure any unused bits are zeroed */
825  dst = (inet *) palloc0(sizeof(inet));
826 
827  maxbytes = ip_addrsize(ip);
828  bits = ip_maxbits(ip) - ip_bits(ip);
829  b = ip_addr(dst);
830 
831  byte = maxbytes - 1;
832 
833  while (bits)
834  {
835  if (bits >= 8)
836  {
837  mask = 0xff;
838  bits -= 8;
839  }
840  else
841  {
842  mask = 0xff >> (8 - bits);
843  bits = 0;
844  }
845 
846  b[byte] = mask;
847  byte--;
848  }
849 
850  ip_family(dst) = ip_family(ip);
851  ip_bits(dst) = ip_maxbits(ip);
852  SET_INET_VARSIZE(dst);
853 
854  PG_RETURN_INET_P(dst);
855 }
#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:112
#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:111
Definition: inet.h:52
#define byte(x, n)
Definition: rijndael.c:68
void * palloc0(Size size)
Definition: mcxt.c:920
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, NULL, 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:1032
#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:63
#define ereport(elevel, rest)
Definition: elog.h:122
void * palloc0(Size size)
Definition: mcxt.c:920
#define NULL
Definition: c.h:226
int errmsg(const char *fmt,...)
Definition: elog.c:797
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:112
static int32 network_cmp_internal(inet *a1, inet *a2)
Definition: network.c:364
#define PG_GETARG_INET_PP(n)
Definition: inet.h:111
Definition: inet.h:52
static FormData_pg_attribute a1
Definition: heap.c:138
static FormData_pg_attribute a2
Definition: heap.c:144
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:111
Definition: inet.h:52
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
static FormData_pg_attribute a1
Definition: heap.c:138
static FormData_pg_attribute a2
Definition: heap.c:144
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:111
Definition: inet.h:52
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
static FormData_pg_attribute a1
Definition: heap.c:138
static FormData_pg_attribute a2
Definition: heap.c:144
Datum network_masklen ( PG_FUNCTION_ARGS  )

Definition at line 656 of file network.c.

References ip_bits, PG_GETARG_INET_PP, and PG_RETURN_INT32.

657 {
658  inet *ip = PG_GETARG_INET_PP(0);
659 
661 }
#define ip_bits(inetptr)
Definition: inet.h:74
#define PG_RETURN_INT32(x)
Definition: fmgr.h:298
#define PG_GETARG_INET_PP(n)
Definition: inet.h:111
Definition: inet.h:52
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:111
Definition: inet.h:52
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
static FormData_pg_attribute a1
Definition: heap.c:138
static FormData_pg_attribute a2
Definition: heap.c:144
Datum network_netmask ( PG_FUNCTION_ARGS  )

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

773 {
774  inet *ip = PG_GETARG_INET_PP(0);
775  inet *dst;
776  int byte;
777  int bits;
778  unsigned char mask;
779  unsigned char *b;
780 
781  /* make sure any unused bits are zeroed */
782  dst = (inet *) palloc0(sizeof(inet));
783 
784  bits = ip_bits(ip);
785  b = ip_addr(dst);
786 
787  byte = 0;
788 
789  while (bits)
790  {
791  if (bits >= 8)
792  {
793  mask = 0xff;
794  bits -= 8;
795  }
796  else
797  {
798  mask = 0xff << (8 - bits);
799  bits = 0;
800  }
801 
802  b[byte] = mask;
803  byte++;
804  }
805 
806  ip_family(dst) = ip_family(ip);
807  ip_bits(dst) = ip_maxbits(ip);
808  SET_INET_VARSIZE(dst);
809 
810  PG_RETURN_INET_P(dst);
811 }
#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:112
#define ip_maxbits(inetptr)
Definition: inet.h:83
#define PG_GETARG_INET_PP(n)
Definition: inet.h:111
Definition: inet.h:52
#define byte(x, n)
Definition: rijndael.c:68
void * palloc0(Size size)
Definition: mcxt.c:920
Datum network_network ( PG_FUNCTION_ARGS  )

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

729 {
730  inet *ip = PG_GETARG_INET_PP(0);
731  inet *dst;
732  int byte;
733  int bits;
734  unsigned char mask;
735  unsigned char *a,
736  *b;
737 
738  /* make sure any unused bits are zeroed */
739  dst = (inet *) palloc0(sizeof(inet));
740 
741  bits = ip_bits(ip);
742  a = ip_addr(ip);
743  b = ip_addr(dst);
744 
745  byte = 0;
746 
747  while (bits)
748  {
749  if (bits >= 8)
750  {
751  mask = 0xff;
752  bits -= 8;
753  }
754  else
755  {
756  mask = 0xff << (8 - bits);
757  bits = 0;
758  }
759 
760  b[byte] = a[byte] & mask;
761  byte++;
762  }
763 
764  ip_family(dst) = ip_family(ip);
765  ip_bits(dst) = ip_bits(ip);
766  SET_INET_VARSIZE(dst);
767 
768  PG_RETURN_INET_P(dst);
769 }
#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:112
#define PG_GETARG_INET_PP(n)
Definition: inet.h:111
Definition: inet.h:52
#define byte(x, n)
Definition: rijndael.c:68
void * palloc0(Size size)
Definition: mcxt.c:920
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, NULL, 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:1165
#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:78
#define ereport(elevel, rest)
Definition: elog.h:122
#define NULL
Definition: c.h:226
int errmsg(const char *fmt,...)
Definition: elog.c:797
Datum network_overlap ( PG_FUNCTION_ARGS  )

Definition at line 553 of file network.c.

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

554 {
555  inet *a1 = PG_GETARG_INET_PP(0);
556  inet *a2 = PG_GETARG_INET_PP(1);
557 
558  if (ip_family(a1) == ip_family(a2))
559  {
561  Min(ip_bits(a1), ip_bits(a2))) == 0);
562  }
563 
564  PG_RETURN_BOOL(false);
565 }
#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:798
#define PG_GETARG_INET_PP(n)
Definition: inet.h:111
Definition: inet.h:52
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
static FormData_pg_attribute a1
Definition: heap.c:138
int bitncmp(const unsigned char *l, const unsigned char *r, int n)
Definition: network.c:960
static FormData_pg_attribute a2
Definition: heap.c:144
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:1032
#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:920
int pq_getmsgbyte(StringInfo msg)
Definition: pqformat.c:432
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
Datum network_scan_first ( Datum  in)

Definition at line 1081 of file network.c.

References DirectFunctionCall1, and network_network().

Referenced by network_prefix_quals().

1082 {
1084 }
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:548
Datum network_network(PG_FUNCTION_ARGS)
Definition: network.c:728
Datum network_scan_last ( Datum  in)

Definition at line 1095 of file network.c.

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

Referenced by network_prefix_quals().

1096 {
1099  Int32GetDatum(-1));
1100 }
Datum network_broadcast(PG_FUNCTION_ARGS)
Definition: network.c:683
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:548
Datum inet_set_masklen(PG_FUNCTION_ARGS)
Definition: network.c:283
#define Int32GetDatum(X)
Definition: postgres.h:487
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:550
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
void pq_sendbyte(StringInfo buf, int byt)
Definition: pqformat.c:105
#define ip_family(inetptr)
Definition: inet.h:71
void pq_begintypsend(StringInfo buf)
Definition: pqformat.c:359
#define ip_addr(inetptr)
Definition: inet.h:77
#define ip_addrsize(inetptr)
Definition: inet.h:80
bytea * pq_endtypsend(StringInfo buf)
Definition: pqformat.c:379
static char * buf
Definition: pg_test_fsync.c:65
int i
Datum network_show ( PG_FUNCTION_ARGS  )

Definition at line 597 of file network.c.

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

598 {
599  inet *ip = PG_GETARG_INET_PP(0);
600  int len;
601  char tmp[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255/128")];
602 
603  if (inet_net_ntop(ip_family(ip), ip_addr(ip), ip_maxbits(ip),
604  tmp, sizeof(tmp)) == NULL)
605  ereport(ERROR,
606  (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
607  errmsg("could not format inet value: %m")));
608 
609  /* Add /n if not present (which it won't be) */
610  if (strchr(tmp, '/') == NULL)
611  {
612  len = strlen(tmp);
613  snprintf(tmp + len, sizeof(tmp) - len, "/%u", ip_bits(ip));
614  }
615 
617 }
#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:111
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:78
#define ereport(elevel, rest)
Definition: elog.h:122
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:314
text * cstring_to_text(const char *s)
Definition: varlena.c:150
#define NULL
Definition: c.h:226
int errmsg(const char *fmt,...)
Definition: elog.c:797
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:112
static int32 network_cmp_internal(inet *a1, inet *a2)
Definition: network.c:364
#define PG_GETARG_INET_PP(n)
Definition: inet.h:111
Definition: inet.h:52
static FormData_pg_attribute a1
Definition: heap.c:138
static FormData_pg_attribute a2
Definition: heap.c:144
Datum network_sub ( PG_FUNCTION_ARGS  )

Definition at line 493 of file network.c.

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

494 {
495  inet *a1 = PG_GETARG_INET_PP(0);
496  inet *a2 = PG_GETARG_INET_PP(1);
497 
498  if (ip_family(a1) == ip_family(a2))
499  {
500  PG_RETURN_BOOL(ip_bits(a1) > ip_bits(a2) &&
501  bitncmp(ip_addr(a1), ip_addr(a2), ip_bits(a2)) == 0);
502  }
503 
504  PG_RETURN_BOOL(false);
505 }
#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:111
Definition: inet.h:52
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
static FormData_pg_attribute a1
Definition: heap.c:138
int bitncmp(const unsigned char *l, const unsigned char *r, int n)
Definition: network.c:960
static FormData_pg_attribute a2
Definition: heap.c:144
Datum network_subeq ( PG_FUNCTION_ARGS  )

Definition at line 508 of file network.c.

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

509 {
510  inet *a1 = PG_GETARG_INET_PP(0);
511  inet *a2 = PG_GETARG_INET_PP(1);
512 
513  if (ip_family(a1) == ip_family(a2))
514  {
515  PG_RETURN_BOOL(ip_bits(a1) >= ip_bits(a2) &&
516  bitncmp(ip_addr(a1), ip_addr(a2), ip_bits(a2)) == 0);
517  }
518 
519  PG_RETURN_BOOL(false);
520 }
#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:111
Definition: inet.h:52
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
static FormData_pg_attribute a1
Definition: heap.c:138
int bitncmp(const unsigned char *l, const unsigned char *r, int n)
Definition: network.c:960
static FormData_pg_attribute a2
Definition: heap.c:144
Datum network_sup ( PG_FUNCTION_ARGS  )

Definition at line 523 of file network.c.

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

524 {
525  inet *a1 = PG_GETARG_INET_PP(0);
526  inet *a2 = PG_GETARG_INET_PP(1);
527 
528  if (ip_family(a1) == ip_family(a2))
529  {
530  PG_RETURN_BOOL(ip_bits(a1) < ip_bits(a2) &&
531  bitncmp(ip_addr(a1), ip_addr(a2), ip_bits(a1)) == 0);
532  }
533 
534  PG_RETURN_BOOL(false);
535 }
#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:111
Definition: inet.h:52
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
static FormData_pg_attribute a1
Definition: heap.c:138
int bitncmp(const unsigned char *l, const unsigned char *r, int n)
Definition: network.c:960
static FormData_pg_attribute a2
Definition: heap.c:144
Datum network_supeq ( PG_FUNCTION_ARGS  )

Definition at line 538 of file network.c.

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

539 {
540  inet *a1 = PG_GETARG_INET_PP(0);
541  inet *a2 = PG_GETARG_INET_PP(1);
542 
543  if (ip_family(a1) == ip_family(a2))
544  {
545  PG_RETURN_BOOL(ip_bits(a1) <= ip_bits(a2) &&
546  bitncmp(ip_addr(a1), ip_addr(a2), ip_bits(a1)) == 0);
547  }
548 
549  PG_RETURN_BOOL(false);
550 }
#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:111
Definition: inet.h:52
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
static FormData_pg_attribute a1
Definition: heap.c:138
int bitncmp(const unsigned char *l, const unsigned char *r, int n)
Definition: network.c:960
static FormData_pg_attribute a2
Definition: heap.c:144