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 "libpq/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)
 
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 1083 of file network.c.

References Assert, byte, and PGSQL_AF_INET.

Referenced by network_in(), and network_recv().

1084 {
1085  int byte;
1086  int nbits;
1087  int maxbits;
1088  int maxbytes;
1089  unsigned char mask;
1090 
1091  if (family == PGSQL_AF_INET)
1092  {
1093  maxbits = 32;
1094  maxbytes = 4;
1095  }
1096  else
1097  {
1098  maxbits = 128;
1099  maxbytes = 16;
1100  }
1101  Assert(bits <= maxbits);
1102 
1103  if (bits == maxbits)
1104  return true;
1105 
1106  byte = bits / 8;
1107 
1108  nbits = bits % 8;
1109  mask = 0xff;
1110  if (bits != 0)
1111  mask >>= nbits;
1112 
1113  while (byte < maxbytes)
1114  {
1115  if ((a[byte] & mask) != 0)
1116  return false;
1117  mask = 0xff;
1118  byte++;
1119  }
1120 
1121  return true;
1122 }
#define PGSQL_AF_INET
Definition: inet.h:37
#define byte(x, n)
Definition: rijndael.c:68
#define Assert(condition)
Definition: c.h:648
int bitncmp ( const unsigned char *  l,
const unsigned char *  r,
int  n 
)

Definition at line 1011 of file network.c.

References IS_HIGHBIT_SET.

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

1012 {
1013  unsigned int lb,
1014  rb;
1015  int x,
1016  b;
1017 
1018  b = n / 8;
1019  x = memcmp(l, r, b);
1020  if (x || (n % 8) == 0)
1021  return x;
1022 
1023  lb = l[b];
1024  rb = r[b];
1025  for (b = n % 8; b > 0; b--)
1026  {
1027  if (IS_HIGHBIT_SET(lb) != IS_HIGHBIT_SET(rb))
1028  {
1029  if (IS_HIGHBIT_SET(lb))
1030  return 1;
1031  return -1;
1032  }
1033  lb <<= 1;
1034  rb <<= 1;
1035  }
1036  return 0;
1037 }
#define IS_HIGHBIT_SET(ch)
Definition: c.h:958
int bitncommon ( const unsigned char *  l,
const unsigned char *  r,
int  n 
)

Definition at line 1045 of file network.c.

References byte.

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

1046 {
1047  int byte,
1048  nbits;
1049 
1050  /* number of bits to examine in last byte */
1051  nbits = n % 8;
1052 
1053  /* check whole bytes */
1054  for (byte = 0; byte < n / 8; byte++)
1055  {
1056  if (l[byte] != r[byte])
1057  {
1058  /* at least one bit in the last byte is not common */
1059  nbits = 7;
1060  break;
1061  }
1062  }
1063 
1064  /* check bits in last partial byte */
1065  if (nbits != 0)
1066  {
1067  /* calculate diff of first non-matching bytes */
1068  unsigned int diff = l[byte] ^ r[byte];
1069 
1070  /* compare the bits from the most to the least */
1071  while ((diff >> (8 - nbits)) != 0)
1072  nbits--;
1073  }
1074 
1075  return (8 * byte) + nbits;
1076 }
#define byte(x, n)
Definition: rijndael.c:68
Datum cidr_abbrev ( PG_FUNCTION_ARGS  )

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

664 {
665  inet *ip = PG_GETARG_INET_PP(0);
666  char *dst;
667  char tmp[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255/128")];
668 
669  dst = inet_cidr_ntop(ip_family(ip), ip_addr(ip),
670  ip_bits(ip), tmp, sizeof(tmp));
671 
672  if (dst == NULL)
673  ereport(ERROR,
674  (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
675  errmsg("could not format cidr value: %m")));
676 
678 }
#define ip_bits(inetptr)
Definition: inet.h:72
#define ip_family(inetptr)
Definition: inet.h:69
#define ip_addr(inetptr)
Definition: inet.h:75
int errcode(int sqlerrcode)
Definition: elog.c:569
char * inet_cidr_ntop(int af, const void *src, int bits, char *dst, size_t size)
#define ERROR
Definition: elog.h:41
#define PG_GETARG_INET_PP(n)
Definition: inet.h:109
Definition: inet.h:50
#define ereport(elevel, rest)
Definition: elog.h:132
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:314
text * cstring_to_text(const char *s)
Definition: varlena.c:139
#define NULL
Definition: c.h:202
int errmsg(const char *fmt,...)
Definition: elog.c:791
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:110
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:109
Definition: inet.h:50
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:110
#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:109
Definition: inet.h:50
static bytea * network_send(inet *addr, bool is_cidr)
Definition: network.c:229
Datum cidr_set_masklen ( PG_FUNCTION_ARGS  )

Definition at line 333 of file network.c.

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

334 {
335  inet *src = PG_GETARG_INET_PP(0);
336  int bits = PG_GETARG_INT32(1);
337  inet *dst;
338  int byte;
339  int nbits;
340  int maxbytes;
341 
342  if (bits == -1)
343  bits = ip_maxbits(src);
344 
345  if ((bits < 0) || (bits > ip_maxbits(src)))
346  ereport(ERROR,
347  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
348  errmsg("invalid mask length: %d", bits)));
349 
350  /* clone the original data */
351  dst = (inet *) palloc(VARSIZE_ANY(src));
352  memcpy(dst, src, VARSIZE_ANY(src));
353 
354  ip_bits(dst) = bits;
355 
356  /* zero out any bits to the right of the new netmask */
357  byte = bits / 8;
358 
359  nbits = bits % 8;
360  /* clear the first byte, this might be a partial byte */
361  if (nbits != 0)
362  {
363  ip_addr(dst)[byte] &= ~(0xFF >> nbits);
364  byte++;
365  }
366  /* clear remaining bytes */
367  maxbytes = ip_addrsize(dst);
368  while (byte < maxbytes)
369  {
370  ip_addr(dst)[byte] = 0;
371  byte++;
372  }
373 
374  PG_RETURN_INET_P(dst);
375 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:225
#define ip_bits(inetptr)
Definition: inet.h:72
#define ip_addr(inetptr)
Definition: inet.h:75
#define PG_RETURN_INET_P(x)
Definition: inet.h:110
int errcode(int sqlerrcode)
Definition: elog.c:569
#define ip_addrsize(inetptr)
Definition: inet.h:78
#define ip_maxbits(inetptr)
Definition: inet.h:81
#define ERROR
Definition: elog.h:41
#define PG_GETARG_INET_PP(n)
Definition: inet.h:109
Definition: inet.h:50
#define ereport(elevel, rest)
Definition: elog.h:132
#define byte(x, n)
Definition: rijndael.c:68
#define VARSIZE_ANY(PTR)
Definition: postgres.h:336
void * palloc(Size size)
Definition: mcxt.c:812
int errmsg(const char *fmt,...)
Definition: elog.c:791
void clean_ipv6_addr ( int  addr_family,
char *  addr 
)

Definition at line 1545 of file network.c.

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

1546 {
1547 #ifdef HAVE_IPV6
1548  if (addr_family == AF_INET6)
1549  {
1550  char *pct = strchr(addr, '%');
1551 
1552  if (pct)
1553  *pct = '\0';
1554  }
1555 #endif
1556 }
double convert_network_to_scalar ( Datum  value,
Oid  typid 
)

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

949 {
950  switch (typid)
951  {
952  case INETOID:
953  case CIDROID:
954  {
955  inet *ip = DatumGetInetPP(value);
956  int len;
957  double res;
958  int i;
959 
960  /*
961  * Note that we don't use the full address for IPv6.
962  */
963  if (ip_family(ip) == PGSQL_AF_INET)
964  len = 4;
965  else
966  len = 5;
967 
968  res = ip_family(ip);
969  for (i = 0; i < len; i++)
970  {
971  res *= 256;
972  res += ip_addr(ip)[i];
973  }
974  return res;
975 
976  break;
977  }
978  case MACADDROID:
979  {
981  double res;
982 
983  res = (mac->a << 16) | (mac->b << 8) | (mac->c);
984  res *= 256 * 256 * 256;
985  res += (mac->d << 16) | (mac->e << 8) | (mac->f);
986  return res;
987  }
988  }
989 
990  /*
991  * Can't get here unless someone tries to use scalarltsel/scalargtsel on
992  * an operator with one network and one non-network operand.
993  */
994  elog(ERROR, "unsupported type: %u", typid);
995  return 0;
996 }
#define CIDROID
Definition: pg_type.h:443
#define DatumGetMacaddrP(X)
Definition: inet.h:112
#define ip_family(inetptr)
Definition: inet.h:69
#define PGSQL_AF_INET
Definition: inet.h:37
#define INETOID
Definition: pg_type.h:440
#define DatumGetInetPP(X)
Definition: inet.h:106
unsigned char f
Definition: inet.h:99
#define ip_addr(inetptr)
Definition: inet.h:75
unsigned char c
Definition: inet.h:96
#define ERROR
Definition: elog.h:41
unsigned char a
Definition: inet.h:94
static struct @72 value
unsigned char d
Definition: inet.h:97
Definition: inet.h:50
unsigned char b
Definition: inet.h:95
unsigned char e
Definition: inet.h:98
Definition: inet.h:92
int i
#define MACADDROID
Definition: pg_type.h:437
#define elog
Definition: elog.h:228
Datum hashinet ( PG_FUNCTION_ARGS  )

Definition at line 505 of file network.c.

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

506 {
507  inet *addr = PG_GETARG_INET_PP(0);
508  int addrsize = ip_addrsize(addr);
509 
510  /* XXX this assumes there are no pad bytes in the data structure */
511  return hash_any((unsigned char *) VARDATA_ANY(addr), addrsize + 2);
512 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:349
#define ip_addrsize(inetptr)
Definition: inet.h:78
#define PG_GETARG_INET_PP(n)
Definition: inet.h:109
Definition: inet.h:50
Datum hash_any(register const unsigned char *k, register int keylen)
Definition: hashfunc.c:305
Datum inet_abbrev ( PG_FUNCTION_ARGS  )

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

646 {
647  inet *ip = PG_GETARG_INET_PP(0);
648  char *dst;
649  char tmp[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255/128")];
650 
651  dst = inet_net_ntop(ip_family(ip), ip_addr(ip),
652  ip_bits(ip), tmp, sizeof(tmp));
653 
654  if (dst == NULL)
655  ereport(ERROR,
656  (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
657  errmsg("could not format inet value: %m")));
658 
660 }
#define ip_bits(inetptr)
Definition: inet.h:72
#define ip_family(inetptr)
Definition: inet.h:69
#define ip_addr(inetptr)
Definition: inet.h:75
int errcode(int sqlerrcode)
Definition: elog.c:569
#define ERROR
Definition: elog.h:41
#define PG_GETARG_INET_PP(n)
Definition: inet.h:109
Definition: inet.h:50
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:132
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:314
text * cstring_to_text(const char *s)
Definition: varlena.c:139
#define NULL
Definition: c.h:202
int errmsg(const char *fmt,...)
Definition: elog.c:791
Datum inet_client_addr ( PG_FUNCTION_ARGS  )

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

1159 {
1160  Port *port = MyProcPort;
1161  char remote_host[NI_MAXHOST];
1162  int ret;
1163 
1164  if (port == NULL)
1165  PG_RETURN_NULL();
1166 
1167  switch (port->raddr.addr.ss_family)
1168  {
1169  case AF_INET:
1170 #ifdef HAVE_IPV6
1171  case AF_INET6:
1172 #endif
1173  break;
1174  default:
1175  PG_RETURN_NULL();
1176  }
1177 
1178  remote_host[0] = '\0';
1179 
1180  ret = pg_getnameinfo_all(&port->raddr.addr, port->raddr.salen,
1181  remote_host, sizeof(remote_host),
1182  NULL, 0,
1184  if (ret != 0)
1185  PG_RETURN_NULL();
1186 
1187  clean_ipv6_addr(port->raddr.addr.ss_family, remote_host);
1188 
1189  PG_RETURN_INET_P(network_in(remote_host, false));
1190 }
#define NI_NUMERICHOST
Definition: getaddrinfo.h:80
struct Port * MyProcPort
Definition: globals.c:39
struct sockaddr_storage addr
Definition: pqcomm.h:64
#define PG_RETURN_INET_P(x)
Definition: inet.h:110
Definition: libpq-be.h:123
static inet * network_in(char *src, bool is_cidr)
Definition: network.c:34
SockAddr raddr
Definition: libpq-be.h:129
#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:202
int pg_getnameinfo_all(const struct sockaddr_storage *addr, int salen, char *node, int nodelen, char *service, int servicelen, int flags)
Definition: ip.c:129
void clean_ipv6_addr(int addr_family, char *addr)
Definition: network.c:1545
#define PG_RETURN_NULL()
Definition: fmgr.h:289
Datum inet_client_port ( PG_FUNCTION_ARGS  )

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

1198 {
1199  Port *port = MyProcPort;
1200  char remote_port[NI_MAXSERV];
1201  int ret;
1202 
1203  if (port == NULL)
1204  PG_RETURN_NULL();
1205 
1206  switch (port->raddr.addr.ss_family)
1207  {
1208  case AF_INET:
1209 #ifdef HAVE_IPV6
1210  case AF_INET6:
1211 #endif
1212  break;
1213  default:
1214  PG_RETURN_NULL();
1215  }
1216 
1217  remote_port[0] = '\0';
1218 
1219  ret = pg_getnameinfo_all(&port->raddr.addr, port->raddr.salen,
1220  NULL, 0,
1221  remote_port, sizeof(remote_port),
1223  if (ret != 0)
1224  PG_RETURN_NULL();
1225 
1227 }
#define NI_NUMERICHOST
Definition: getaddrinfo.h:80
struct Port * MyProcPort
Definition: globals.c:39
Datum int4in(PG_FUNCTION_ARGS)
Definition: int.c:281
struct sockaddr_storage addr
Definition: pqcomm.h:64
Definition: libpq-be.h:123
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:548
SockAddr raddr
Definition: libpq-be.h:129
#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:202
int pg_getnameinfo_all(const struct sockaddr_storage *addr, int salen, char *node, int nodelen, char *service, int servicelen, int flags)
Definition: ip.c:129
#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:110
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 907 of file network.c.

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

908 {
909  inet *a1 = PG_GETARG_INET_PP(0),
910  *a2 = PG_GETARG_INET_PP(1),
911  *result;
912  int commonbits;
913 
914  if (ip_family(a1) != ip_family(a2))
915  ereport(ERROR,
916  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
917  errmsg("cannot merge addresses from different families")));
918 
919  commonbits = bitncommon(ip_addr(a1), ip_addr(a2),
920  Min(ip_bits(a1), ip_bits(a2)));
921 
922  /* Make sure any unused bits are zeroed. */
923  result = (inet *) palloc0(sizeof(inet));
924 
925  ip_family(result) = ip_family(a1);
926  ip_bits(result) = commonbits;
927 
928  /* Clone appropriate bytes of the address. */
929  if (commonbits > 0)
930  memcpy(ip_addr(result), ip_addr(a1), (commonbits + 7) / 8);
931 
932  /* Clean any unwanted bits in the last partial byte. */
933  if (commonbits % 8 != 0)
934  ip_addr(result)[commonbits / 8] &= ~(0xFF >> (commonbits % 8));
935 
936  /* Set varlena header correctly. */
937  SET_INET_VARSIZE(result);
938 
939  PG_RETURN_INET_P(result);
940 }
#define ip_bits(inetptr)
Definition: inet.h:72
#define ip_family(inetptr)
Definition: inet.h:69
#define ip_addr(inetptr)
Definition: inet.h:75
#define Min(x, y)
Definition: c.h:779
#define SET_INET_VARSIZE(dst)
Definition: inet.h:84
#define PG_RETURN_INET_P(x)
Definition: inet.h:110
int bitncommon(const unsigned char *l, const unsigned char *r, int n)
Definition: network.c:1045
int errcode(int sqlerrcode)
Definition: elog.c:569
#define ERROR
Definition: elog.h:41
#define PG_GETARG_INET_PP(n)
Definition: inet.h:109
Definition: inet.h:50
#define ereport(elevel, rest)
Definition: elog.h:132
void * palloc0(Size size)
Definition: mcxt.c:841
static FormData_pg_attribute a1
Definition: heap.c:137
int errmsg(const char *fmt,...)
Definition: elog.c:791
static FormData_pg_attribute a2
Definition: heap.c:143
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:109
Definition: inet.h:50
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:110
#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 895 of file network.c.

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

896 {
897  inet *a1 = PG_GETARG_INET_PP(0);
898  inet *a2 = PG_GETARG_INET_PP(1);
899 
900  PG_RETURN_BOOL(ip_family(a1) == ip_family(a2));
901 }
#define ip_family(inetptr)
Definition: inet.h:69
#define PG_GETARG_INET_PP(n)
Definition: inet.h:109
Definition: inet.h:50
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
static FormData_pg_attribute a1
Definition: heap.c:137
static FormData_pg_attribute a2
Definition: heap.c:143
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:109
Definition: inet.h:50
static bytea * network_send(inet *addr, bool is_cidr)
Definition: network.c:229
Datum inet_server_addr ( PG_FUNCTION_ARGS  )

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

1235 {
1236  Port *port = MyProcPort;
1237  char local_host[NI_MAXHOST];
1238  int ret;
1239 
1240  if (port == NULL)
1241  PG_RETURN_NULL();
1242 
1243  switch (port->laddr.addr.ss_family)
1244  {
1245  case AF_INET:
1246 #ifdef HAVE_IPV6
1247  case AF_INET6:
1248 #endif
1249  break;
1250  default:
1251  PG_RETURN_NULL();
1252  }
1253 
1254  local_host[0] = '\0';
1255 
1256  ret = pg_getnameinfo_all(&port->laddr.addr, port->laddr.salen,
1257  local_host, sizeof(local_host),
1258  NULL, 0,
1260  if (ret != 0)
1261  PG_RETURN_NULL();
1262 
1263  clean_ipv6_addr(port->laddr.addr.ss_family, local_host);
1264 
1265  PG_RETURN_INET_P(network_in(local_host, false));
1266 }
#define NI_NUMERICHOST
Definition: getaddrinfo.h:80
struct Port * MyProcPort
Definition: globals.c:39
struct sockaddr_storage addr
Definition: pqcomm.h:64
#define PG_RETURN_INET_P(x)
Definition: inet.h:110
Definition: libpq-be.h:123
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:128
static int port
Definition: pg_regress.c:87
#define NI_NUMERICSERV
Definition: getaddrinfo.h:83
#define NULL
Definition: c.h:202
int pg_getnameinfo_all(const struct sockaddr_storage *addr, int salen, char *node, int nodelen, char *service, int servicelen, int flags)
Definition: ip.c:129
void clean_ipv6_addr(int addr_family, char *addr)
Definition: network.c:1545
#define PG_RETURN_NULL()
Definition: fmgr.h:289
Datum inet_server_port ( PG_FUNCTION_ARGS  )

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

1274 {
1275  Port *port = MyProcPort;
1276  char local_port[NI_MAXSERV];
1277  int ret;
1278 
1279  if (port == NULL)
1280  PG_RETURN_NULL();
1281 
1282  switch (port->laddr.addr.ss_family)
1283  {
1284  case AF_INET:
1285 #ifdef HAVE_IPV6
1286  case AF_INET6:
1287 #endif
1288  break;
1289  default:
1290  PG_RETURN_NULL();
1291  }
1292 
1293  local_port[0] = '\0';
1294 
1295  ret = pg_getnameinfo_all(&port->laddr.addr, port->laddr.salen,
1296  NULL, 0,
1297  local_port, sizeof(local_port),
1299  if (ret != 0)
1300  PG_RETURN_NULL();
1301 
1303 }
#define NI_NUMERICHOST
Definition: getaddrinfo.h:80
struct Port * MyProcPort
Definition: globals.c:39
Datum int4in(PG_FUNCTION_ARGS)
Definition: int.c:281
struct sockaddr_storage addr
Definition: pqcomm.h:64
Definition: libpq-be.h:123
#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:128
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:202
int pg_getnameinfo_all(const struct sockaddr_storage *addr, int salen, char *node, int nodelen, char *service, int servicelen, int flags)
Definition: ip.c:129
#define PG_RETURN_NULL()
Definition: fmgr.h:289
Datum inet_set_masklen ( PG_FUNCTION_ARGS  )

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

310 {
311  inet *src = PG_GETARG_INET_PP(0);
312  int bits = PG_GETARG_INT32(1);
313  inet *dst;
314 
315  if (bits == -1)
316  bits = ip_maxbits(src);
317 
318  if ((bits < 0) || (bits > ip_maxbits(src)))
319  ereport(ERROR,
320  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
321  errmsg("invalid mask length: %d", bits)));
322 
323  /* clone the original data */
324  dst = (inet *) palloc(VARSIZE_ANY(src));
325  memcpy(dst, src, VARSIZE_ANY(src));
326 
327  ip_bits(dst) = bits;
328 
329  PG_RETURN_INET_P(dst);
330 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:225
#define ip_bits(inetptr)
Definition: inet.h:72
#define PG_RETURN_INET_P(x)
Definition: inet.h:110
int errcode(int sqlerrcode)
Definition: elog.c:569
#define ip_maxbits(inetptr)
Definition: inet.h:81
#define ERROR
Definition: elog.h:41
#define PG_GETARG_INET_PP(n)
Definition: inet.h:109
Definition: inet.h:50
#define ereport(elevel, rest)
Definition: elog.h:132
#define VARSIZE_ANY(PTR)
Definition: postgres.h:336
void * palloc(Size size)
Definition: mcxt.c:812
int errmsg(const char *fmt,...)
Definition: elog.c:791
Datum inet_to_cidr ( PG_FUNCTION_ARGS  )

Definition at line 268 of file network.c.

References byte, elog, ERROR, ip_addr, ip_addrsize, ip_bits, ip_maxbits, palloc(), PG_GETARG_INET_PP, PG_RETURN_INET_P, and VARSIZE_ANY.

269 {
270  inet *src = PG_GETARG_INET_PP(0);
271  inet *dst;
272  int bits;
273  int byte;
274  int nbits;
275  int maxbytes;
276 
277  bits = ip_bits(src);
278 
279  /* safety check */
280  if ((bits < 0) || (bits > ip_maxbits(src)))
281  elog(ERROR, "invalid inet bit length: %d", bits);
282 
283  /* clone the original data */
284  dst = (inet *) palloc(VARSIZE_ANY(src));
285  memcpy(dst, src, VARSIZE_ANY(src));
286 
287  /* zero out any bits to the right of the netmask */
288  byte = bits / 8;
289 
290  nbits = bits % 8;
291  /* clear the first byte, this might be a partial byte */
292  if (nbits != 0)
293  {
294  ip_addr(dst)[byte] &= ~(0xFF >> nbits);
295  byte++;
296  }
297  /* clear remaining bytes */
298  maxbytes = ip_addrsize(dst);
299  while (byte < maxbytes)
300  {
301  ip_addr(dst)[byte] = 0;
302  byte++;
303  }
304 
305  PG_RETURN_INET_P(dst);
306 }
#define ip_bits(inetptr)
Definition: inet.h:72
#define ip_addr(inetptr)
Definition: inet.h:75
#define PG_RETURN_INET_P(x)
Definition: inet.h:110
#define ip_addrsize(inetptr)
Definition: inet.h:78
#define ip_maxbits(inetptr)
Definition: inet.h:81
#define ERROR
Definition: elog.h:41
#define PG_GETARG_INET_PP(n)
Definition: inet.h:109
Definition: inet.h:50
#define byte(x, n)
Definition: rijndael.c:68
#define VARSIZE_ANY(PTR)
Definition: postgres.h:336
void * palloc(Size size)
Definition: mcxt.c:812
#define elog
Definition: elog.h:228
Datum inetand ( PG_FUNCTION_ARGS  )

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

1333 {
1334  inet *ip = PG_GETARG_INET_PP(0);
1335  inet *ip2 = PG_GETARG_INET_PP(1);
1336  inet *dst;
1337 
1338  dst = (inet *) palloc0(sizeof(inet));
1339 
1340  if (ip_family(ip) != ip_family(ip2))
1341  ereport(ERROR,
1342  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1343  errmsg("cannot AND inet values of different sizes")));
1344  else
1345  {
1346  int nb = ip_addrsize(ip);
1347  unsigned char *pip = ip_addr(ip);
1348  unsigned char *pip2 = ip_addr(ip2);
1349  unsigned char *pdst = ip_addr(dst);
1350 
1351  while (nb-- > 0)
1352  pdst[nb] = pip[nb] & pip2[nb];
1353  }
1354  ip_bits(dst) = Max(ip_bits(ip), ip_bits(ip2));
1355 
1356  ip_family(dst) = ip_family(ip);
1357  SET_INET_VARSIZE(dst);
1358 
1359  PG_RETURN_INET_P(dst);
1360 }
#define ip_bits(inetptr)
Definition: inet.h:72
#define ip_family(inetptr)
Definition: inet.h:69
#define ip_addr(inetptr)
Definition: inet.h:75
#define SET_INET_VARSIZE(dst)
Definition: inet.h:84
#define PG_RETURN_INET_P(x)
Definition: inet.h:110
int errcode(int sqlerrcode)
Definition: elog.c:569
#define ip_addrsize(inetptr)
Definition: inet.h:78
#define ERROR
Definition: elog.h:41
#define PG_GETARG_INET_PP(n)
Definition: inet.h:109
Definition: inet.h:50
#define ereport(elevel, rest)
Definition: elog.h:132
void * palloc0(Size size)
Definition: mcxt.c:841
#define Max(x, y)
Definition: c.h:773
int errmsg(const char *fmt,...)
Definition: elog.c:791
Datum inetmi ( PG_FUNCTION_ARGS  )

Definition at line 1468 of file network.c.

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

1469 {
1470  inet *ip = PG_GETARG_INET_PP(0);
1471  inet *ip2 = PG_GETARG_INET_PP(1);
1472  int64 res = 0;
1473 
1474  if (ip_family(ip) != ip_family(ip2))
1475  ereport(ERROR,
1476  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1477  errmsg("cannot subtract inet values of different sizes")));
1478  else
1479  {
1480  /*
1481  * We form the difference using the traditional complement, increment,
1482  * and add rule, with the increment part being handled by starting the
1483  * carry off at 1. If you don't think integer arithmetic is done in
1484  * two's complement, too bad.
1485  */
1486  int nb = ip_addrsize(ip);
1487  int byte = 0;
1488  unsigned char *pip = ip_addr(ip);
1489  unsigned char *pip2 = ip_addr(ip2);
1490  int carry = 1;
1491 
1492  while (nb-- > 0)
1493  {
1494  int lobyte;
1495 
1496  carry = pip[nb] + (~pip2[nb] & 0xFF) + carry;
1497  lobyte = carry & 0xFF;
1498  if (byte < sizeof(int64))
1499  {
1500  res |= ((int64) lobyte) << (byte * 8);
1501  }
1502  else
1503  {
1504  /*
1505  * Input wider than int64: check for overflow. All bytes to
1506  * the left of what will fit should be 0 or 0xFF, depending on
1507  * sign of the now-complete result.
1508  */
1509  if ((res < 0) ? (lobyte != 0xFF) : (lobyte != 0))
1510  ereport(ERROR,
1511  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1512  errmsg("result is out of range")));
1513  }
1514  carry >>= 8;
1515  byte++;
1516  }
1517 
1518  /*
1519  * If input is narrower than int64, overflow is not possible, but we
1520  * have to do proper sign extension.
1521  */
1522  if (carry == 0 && byte < sizeof(int64))
1523  res |= ((int64) -1) << (byte * 8);
1524  }
1525 
1526  PG_RETURN_INT64(res);
1527 }
#define ip_family(inetptr)
Definition: inet.h:69
#define PG_RETURN_INT64(x)
Definition: fmgr.h:311
#define ip_addr(inetptr)
Definition: inet.h:75
int errcode(int sqlerrcode)
Definition: elog.c:569
#define ip_addrsize(inetptr)
Definition: inet.h:78
#define ERROR
Definition: elog.h:41
#define PG_GETARG_INET_PP(n)
Definition: inet.h:109
Definition: inet.h:50
#define ereport(elevel, rest)
Definition: elog.h:132
#define byte(x, n)
Definition: rijndael.c:68
int errmsg(const char *fmt,...)
Definition: elog.c:791
Datum inetmi_int8 ( PG_FUNCTION_ARGS  )

Definition at line 1458 of file network.c.

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

1459 {
1460  inet *ip = PG_GETARG_INET_PP(0);
1461  int64 addend = PG_GETARG_INT64(1);
1462 
1463  PG_RETURN_INET_P(internal_inetpl(ip, -addend));
1464 }
#define PG_RETURN_INET_P(x)
Definition: inet.h:110
#define PG_GETARG_INET_PP(n)
Definition: inet.h:109
Definition: inet.h:50
static inet * internal_inetpl(inet *ip, int64 addend)
Definition: network.c:1396
#define PG_GETARG_INT64(n)
Definition: fmgr.h:238
Datum inetnot ( PG_FUNCTION_ARGS  )

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

1308 {
1309  inet *ip = PG_GETARG_INET_PP(0);
1310  inet *dst;
1311 
1312  dst = (inet *) palloc0(sizeof(inet));
1313 
1314  {
1315  int nb = ip_addrsize(ip);
1316  unsigned char *pip = ip_addr(ip);
1317  unsigned char *pdst = ip_addr(dst);
1318 
1319  while (nb-- > 0)
1320  pdst[nb] = ~pip[nb];
1321  }
1322  ip_bits(dst) = ip_bits(ip);
1323 
1324  ip_family(dst) = ip_family(ip);
1325  SET_INET_VARSIZE(dst);
1326 
1327  PG_RETURN_INET_P(dst);
1328 }
#define ip_bits(inetptr)
Definition: inet.h:72
#define ip_family(inetptr)
Definition: inet.h:69
#define ip_addr(inetptr)
Definition: inet.h:75
#define SET_INET_VARSIZE(dst)
Definition: inet.h:84
#define PG_RETURN_INET_P(x)
Definition: inet.h:110
#define ip_addrsize(inetptr)
Definition: inet.h:78
#define PG_GETARG_INET_PP(n)
Definition: inet.h:109
Definition: inet.h:50
void * palloc0(Size size)
Definition: mcxt.c:841
Datum inetor ( PG_FUNCTION_ARGS  )

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

1365 {
1366  inet *ip = PG_GETARG_INET_PP(0);
1367  inet *ip2 = PG_GETARG_INET_PP(1);
1368  inet *dst;
1369 
1370  dst = (inet *) palloc0(sizeof(inet));
1371 
1372  if (ip_family(ip) != ip_family(ip2))
1373  ereport(ERROR,
1374  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1375  errmsg("cannot OR inet values of different sizes")));
1376  else
1377  {
1378  int nb = ip_addrsize(ip);
1379  unsigned char *pip = ip_addr(ip);
1380  unsigned char *pip2 = ip_addr(ip2);
1381  unsigned char *pdst = ip_addr(dst);
1382 
1383  while (nb-- > 0)
1384  pdst[nb] = pip[nb] | pip2[nb];
1385  }
1386  ip_bits(dst) = Max(ip_bits(ip), ip_bits(ip2));
1387 
1388  ip_family(dst) = ip_family(ip);
1389  SET_INET_VARSIZE(dst);
1390 
1391  PG_RETURN_INET_P(dst);
1392 }
#define ip_bits(inetptr)
Definition: inet.h:72
#define ip_family(inetptr)
Definition: inet.h:69
#define ip_addr(inetptr)
Definition: inet.h:75
#define SET_INET_VARSIZE(dst)
Definition: inet.h:84
#define PG_RETURN_INET_P(x)
Definition: inet.h:110
int errcode(int sqlerrcode)
Definition: elog.c:569
#define ip_addrsize(inetptr)
Definition: inet.h:78
#define ERROR
Definition: elog.h:41
#define PG_GETARG_INET_PP(n)
Definition: inet.h:109
Definition: inet.h:50
#define ereport(elevel, rest)
Definition: elog.h:132
void * palloc0(Size size)
Definition: mcxt.c:841
#define Max(x, y)
Definition: c.h:773
int errmsg(const char *fmt,...)
Definition: elog.c:791
Datum inetpl ( PG_FUNCTION_ARGS  )

Definition at line 1448 of file network.c.

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

1449 {
1450  inet *ip = PG_GETARG_INET_PP(0);
1451  int64 addend = PG_GETARG_INT64(1);
1452 
1453  PG_RETURN_INET_P(internal_inetpl(ip, addend));
1454 }
#define PG_RETURN_INET_P(x)
Definition: inet.h:110
#define PG_GETARG_INET_PP(n)
Definition: inet.h:109
Definition: inet.h:50
static inet * internal_inetpl(inet *ip, int64 addend)
Definition: network.c:1396
#define PG_GETARG_INT64(n)
Definition: fmgr.h:238
static inet * internal_inetpl ( inet ip,
int64  addend 
)
static

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

1397 {
1398  inet *dst;
1399 
1400  dst = (inet *) palloc0(sizeof(inet));
1401 
1402  {
1403  int nb = ip_addrsize(ip);
1404  unsigned char *pip = ip_addr(ip);
1405  unsigned char *pdst = ip_addr(dst);
1406  int carry = 0;
1407 
1408  while (nb-- > 0)
1409  {
1410  carry = pip[nb] + (int) (addend & 0xFF) + carry;
1411  pdst[nb] = (unsigned char) (carry & 0xFF);
1412  carry >>= 8;
1413 
1414  /*
1415  * We have to be careful about right-shifting addend because
1416  * right-shift isn't portable for negative values, while simply
1417  * dividing by 256 doesn't work (the standard rounding is in the
1418  * wrong direction, besides which there may be machines out there
1419  * that round the wrong way). So, explicitly clear the low-order
1420  * byte to remove any doubt about the correct result of the
1421  * division, and then divide rather than shift.
1422  */
1423  addend &= ~((int64) 0xFF);
1424  addend /= 0x100;
1425  }
1426 
1427  /*
1428  * At this point we should have addend and carry both zero if original
1429  * addend was >= 0, or addend -1 and carry 1 if original addend was <
1430  * 0. Anything else means overflow.
1431  */
1432  if (!((addend == 0 && carry == 0) ||
1433  (addend == -1 && carry == 1)))
1434  ereport(ERROR,
1435  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1436  errmsg("result is out of range")));
1437  }
1438 
1439  ip_bits(dst) = ip_bits(ip);
1440  ip_family(dst) = ip_family(ip);
1441  SET_INET_VARSIZE(dst);
1442 
1443  return dst;
1444 }
#define ip_bits(inetptr)
Definition: inet.h:72
#define ip_family(inetptr)
Definition: inet.h:69
#define ip_addr(inetptr)
Definition: inet.h:75
#define SET_INET_VARSIZE(dst)
Definition: inet.h:84
int errcode(int sqlerrcode)
Definition: elog.c:569
#define ip_addrsize(inetptr)
Definition: inet.h:78
#define ERROR
Definition: elog.h:41
Definition: inet.h:50
#define ereport(elevel, rest)
Definition: elog.h:132
void * palloc0(Size size)
Definition: mcxt.c:841
int errmsg(const char *fmt,...)
Definition: elog.c:791
Datum network_broadcast ( PG_FUNCTION_ARGS  )

Definition at line 708 of file network.c.

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

Referenced by network_scan_last().

709 {
710  inet *ip = PG_GETARG_INET_PP(0);
711  inet *dst;
712  int byte;
713  int bits;
714  int maxbytes;
715  unsigned char mask;
716  unsigned char *a,
717  *b;
718 
719  /* make sure any unused bits are zeroed */
720  dst = (inet *) palloc0(sizeof(inet));
721 
722  if (ip_family(ip) == PGSQL_AF_INET)
723  maxbytes = 4;
724  else
725  maxbytes = 16;
726 
727  bits = ip_bits(ip);
728  a = ip_addr(ip);
729  b = ip_addr(dst);
730 
731  for (byte = 0; byte < maxbytes; byte++)
732  {
733  if (bits >= 8)
734  {
735  mask = 0x00;
736  bits -= 8;
737  }
738  else if (bits == 0)
739  mask = 0xff;
740  else
741  {
742  mask = 0xff >> bits;
743  bits = 0;
744  }
745 
746  b[byte] = a[byte] | mask;
747  }
748 
749  ip_family(dst) = ip_family(ip);
750  ip_bits(dst) = ip_bits(ip);
751  SET_INET_VARSIZE(dst);
752 
753  PG_RETURN_INET_P(dst);
754 }
#define ip_bits(inetptr)
Definition: inet.h:72
#define ip_family(inetptr)
Definition: inet.h:69
#define PGSQL_AF_INET
Definition: inet.h:37
#define ip_addr(inetptr)
Definition: inet.h:75
#define SET_INET_VARSIZE(dst)
Definition: inet.h:84
#define PG_RETURN_INET_P(x)
Definition: inet.h:110
#define PG_GETARG_INET_PP(n)
Definition: inet.h:109
Definition: inet.h:50
#define byte(x, n)
Definition: rijndael.c:68
void * palloc0(Size size)
Definition: mcxt.c:841
Datum network_cmp ( PG_FUNCTION_ARGS  )

Definition at line 409 of file network.c.

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

410 {
411  inet *a1 = PG_GETARG_INET_PP(0);
412  inet *a2 = PG_GETARG_INET_PP(1);
413 
415 }
#define PG_RETURN_INT32(x)
Definition: fmgr.h:298
static int32 network_cmp_internal(inet *a1, inet *a2)
Definition: network.c:389
#define PG_GETARG_INET_PP(n)
Definition: inet.h:109
Definition: inet.h:50
static FormData_pg_attribute a1
Definition: heap.c:137
static FormData_pg_attribute a2
Definition: heap.c:143
static int32 network_cmp_internal ( inet a1,
inet a2 
)
static

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

390 {
391  if (ip_family(a1) == ip_family(a2))
392  {
393  int order;
394 
395  order = bitncmp(ip_addr(a1), ip_addr(a2),
396  Min(ip_bits(a1), ip_bits(a2)));
397  if (order != 0)
398  return order;
399  order = ((int) ip_bits(a1)) - ((int) ip_bits(a2));
400  if (order != 0)
401  return order;
402  return bitncmp(ip_addr(a1), ip_addr(a2), ip_maxbits(a1));
403  }
404 
405  return ip_family(a1) - ip_family(a2);
406 }
#define ip_bits(inetptr)
Definition: inet.h:72
#define ip_family(inetptr)
Definition: inet.h:69
#define ip_addr(inetptr)
Definition: inet.h:75
#define Min(x, y)
Definition: c.h:779
#define ip_maxbits(inetptr)
Definition: inet.h:81
int bitncmp(const unsigned char *l, const unsigned char *r, int n)
Definition: network.c:1011
Datum network_eq ( PG_FUNCTION_ARGS  )

Definition at line 439 of file network.c.

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

440 {
441  inet *a1 = PG_GETARG_INET_PP(0);
442  inet *a2 = PG_GETARG_INET_PP(1);
443 
444  PG_RETURN_BOOL(network_cmp_internal(a1, a2) == 0);
445 }
static int32 network_cmp_internal(inet *a1, inet *a2)
Definition: network.c:389
#define PG_GETARG_INET_PP(n)
Definition: inet.h:109
Definition: inet.h:50
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
static FormData_pg_attribute a1
Definition: heap.c:137
static FormData_pg_attribute a2
Definition: heap.c:143
Datum network_family ( PG_FUNCTION_ARGS  )

Definition at line 689 of file network.c.

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

690 {
691  inet *ip = PG_GETARG_INET_PP(0);
692 
693  switch (ip_family(ip))
694  {
695  case PGSQL_AF_INET:
696  PG_RETURN_INT32(4);
697  break;
698  case PGSQL_AF_INET6:
699  PG_RETURN_INT32(6);
700  break;
701  default:
702  PG_RETURN_INT32(0);
703  break;
704  }
705 }
#define ip_family(inetptr)
Definition: inet.h:69
#define PGSQL_AF_INET
Definition: inet.h:37
#define PG_RETURN_INT32(x)
Definition: fmgr.h:298
#define PGSQL_AF_INET6
Definition: inet.h:38
#define PG_GETARG_INET_PP(n)
Definition: inet.h:109
Definition: inet.h:50
Datum network_ge ( PG_FUNCTION_ARGS  )

Definition at line 448 of file network.c.

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

449 {
450  inet *a1 = PG_GETARG_INET_PP(0);
451  inet *a2 = PG_GETARG_INET_PP(1);
452 
453  PG_RETURN_BOOL(network_cmp_internal(a1, a2) >= 0);
454 }
static int32 network_cmp_internal(inet *a1, inet *a2)
Definition: network.c:389
#define PG_GETARG_INET_PP(n)
Definition: inet.h:109
Definition: inet.h:50
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
static FormData_pg_attribute a1
Definition: heap.c:137
static FormData_pg_attribute a2
Definition: heap.c:143
Datum network_gt ( PG_FUNCTION_ARGS  )

Definition at line 457 of file network.c.

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

458 {
459  inet *a1 = PG_GETARG_INET_PP(0);
460  inet *a2 = PG_GETARG_INET_PP(1);
461 
463 }
static int32 network_cmp_internal(inet *a1, inet *a2)
Definition: network.c:389
#define PG_GETARG_INET_PP(n)
Definition: inet.h:109
Definition: inet.h:50
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
static FormData_pg_attribute a1
Definition: heap.c:137
static FormData_pg_attribute a2
Definition: heap.c:143
Datum network_host ( PG_FUNCTION_ARGS  )

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

597 {
598  inet *ip = PG_GETARG_INET_PP(0);
599  char *ptr;
600  char tmp[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255/128")];
601 
602  /* force display of max bits, regardless of masklen... */
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  /* Suppress /n if present (shouldn't happen now) */
610  if ((ptr = strchr(tmp, '/')) != NULL)
611  *ptr = '\0';
612 
614 }
#define ip_family(inetptr)
Definition: inet.h:69
#define ip_addr(inetptr)
Definition: inet.h:75
int errcode(int sqlerrcode)
Definition: elog.c:569
#define ip_maxbits(inetptr)
Definition: inet.h:81
#define ERROR
Definition: elog.h:41
#define PG_GETARG_INET_PP(n)
Definition: inet.h:109
Definition: inet.h:50
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:132
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:314
text * cstring_to_text(const char *s)
Definition: varlena.c:139
#define NULL
Definition: c.h:202
int errmsg(const char *fmt,...)
Definition: elog.c:791
Datum network_hostmask ( PG_FUNCTION_ARGS  )

Definition at line 843 of file network.c.

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

844 {
845  inet *ip = PG_GETARG_INET_PP(0);
846  inet *dst;
847  int byte;
848  int bits;
849  int maxbytes;
850  unsigned char mask;
851  unsigned char *b;
852 
853  /* make sure any unused bits are zeroed */
854  dst = (inet *) palloc0(sizeof(inet));
855 
856  if (ip_family(ip) == PGSQL_AF_INET)
857  maxbytes = 4;
858  else
859  maxbytes = 16;
860 
861  bits = ip_maxbits(ip) - ip_bits(ip);
862  b = ip_addr(dst);
863 
864  byte = maxbytes - 1;
865 
866  while (bits)
867  {
868  if (bits >= 8)
869  {
870  mask = 0xff;
871  bits -= 8;
872  }
873  else
874  {
875  mask = 0xff >> (8 - bits);
876  bits = 0;
877  }
878 
879  b[byte] = mask;
880  byte--;
881  }
882 
883  ip_family(dst) = ip_family(ip);
884  ip_bits(dst) = ip_maxbits(ip);
885  SET_INET_VARSIZE(dst);
886 
887  PG_RETURN_INET_P(dst);
888 }
#define ip_bits(inetptr)
Definition: inet.h:72
#define ip_family(inetptr)
Definition: inet.h:69
#define PGSQL_AF_INET
Definition: inet.h:37
#define ip_addr(inetptr)
Definition: inet.h:75
#define SET_INET_VARSIZE(dst)
Definition: inet.h:84
#define PG_RETURN_INET_P(x)
Definition: inet.h:110
#define ip_maxbits(inetptr)
Definition: inet.h:81
#define PG_GETARG_INET_PP(n)
Definition: inet.h:109
Definition: inet.h:50
#define byte(x, n)
Definition: rijndael.c:68
void * palloc0(Size size)
Definition: mcxt.c:841
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:72
#define ip_family(inetptr)
Definition: inet.h:69
static bool addressOK(unsigned char *a, int bits, int family)
Definition: network.c:1083
#define PGSQL_AF_INET
Definition: inet.h:37
#define ip_addr(inetptr)
Definition: inet.h:75
#define SET_INET_VARSIZE(dst)
Definition: inet.h:84
int errcode(int sqlerrcode)
Definition: elog.c:569
#define ip_addrsize(inetptr)
Definition: inet.h:78
#define PGSQL_AF_INET6
Definition: inet.h:38
#define ip_maxbits(inetptr)
Definition: inet.h:81
#define ERROR
Definition: elog.h:41
int errdetail(const char *fmt,...)
Definition: elog.c:864
Definition: inet.h:50
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:132
void * palloc0(Size size)
Definition: mcxt.c:841
#define NULL
Definition: c.h:202
int errmsg(const char *fmt,...)
Definition: elog.c:791
Datum network_larger ( PG_FUNCTION_ARGS  )

Definition at line 490 of file network.c.

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

491 {
492  inet *a1 = PG_GETARG_INET_PP(0);
493  inet *a2 = PG_GETARG_INET_PP(1);
494 
495  if (network_cmp_internal(a1, a2) > 0)
496  PG_RETURN_INET_P(a1);
497  else
498  PG_RETURN_INET_P(a2);
499 }
#define PG_RETURN_INET_P(x)
Definition: inet.h:110
static int32 network_cmp_internal(inet *a1, inet *a2)
Definition: network.c:389
#define PG_GETARG_INET_PP(n)
Definition: inet.h:109
Definition: inet.h:50
static FormData_pg_attribute a1
Definition: heap.c:137
static FormData_pg_attribute a2
Definition: heap.c:143
Datum network_le ( PG_FUNCTION_ARGS  )

Definition at line 430 of file network.c.

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

431 {
432  inet *a1 = PG_GETARG_INET_PP(0);
433  inet *a2 = PG_GETARG_INET_PP(1);
434 
435  PG_RETURN_BOOL(network_cmp_internal(a1, a2) <= 0);
436 }
static int32 network_cmp_internal(inet *a1, inet *a2)
Definition: network.c:389
#define PG_GETARG_INET_PP(n)
Definition: inet.h:109
Definition: inet.h:50
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
static FormData_pg_attribute a1
Definition: heap.c:137
static FormData_pg_attribute a2
Definition: heap.c:143
Datum network_lt ( PG_FUNCTION_ARGS  )

Definition at line 421 of file network.c.

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

422 {
423  inet *a1 = PG_GETARG_INET_PP(0);
424  inet *a2 = PG_GETARG_INET_PP(1);
425 
427 }
static int32 network_cmp_internal(inet *a1, inet *a2)
Definition: network.c:389
#define PG_GETARG_INET_PP(n)
Definition: inet.h:109
Definition: inet.h:50
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
static FormData_pg_attribute a1
Definition: heap.c:137
static FormData_pg_attribute a2
Definition: heap.c:143
Datum network_masklen ( PG_FUNCTION_ARGS  )

Definition at line 681 of file network.c.

References ip_bits, PG_GETARG_INET_PP, and PG_RETURN_INT32.

682 {
683  inet *ip = PG_GETARG_INET_PP(0);
684 
686 }
#define ip_bits(inetptr)
Definition: inet.h:72
#define PG_RETURN_INT32(x)
Definition: fmgr.h:298
#define PG_GETARG_INET_PP(n)
Definition: inet.h:109
Definition: inet.h:50
Datum network_ne ( PG_FUNCTION_ARGS  )

Definition at line 466 of file network.c.

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

467 {
468  inet *a1 = PG_GETARG_INET_PP(0);
469  inet *a2 = PG_GETARG_INET_PP(1);
470 
471  PG_RETURN_BOOL(network_cmp_internal(a1, a2) != 0);
472 }
static int32 network_cmp_internal(inet *a1, inet *a2)
Definition: network.c:389
#define PG_GETARG_INET_PP(n)
Definition: inet.h:109
Definition: inet.h:50
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
static FormData_pg_attribute a1
Definition: heap.c:137
static FormData_pg_attribute a2
Definition: heap.c:143
Datum network_netmask ( PG_FUNCTION_ARGS  )

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

802 {
803  inet *ip = PG_GETARG_INET_PP(0);
804  inet *dst;
805  int byte;
806  int bits;
807  unsigned char mask;
808  unsigned char *b;
809 
810  /* make sure any unused bits are zeroed */
811  dst = (inet *) palloc0(sizeof(inet));
812 
813  bits = ip_bits(ip);
814  b = ip_addr(dst);
815 
816  byte = 0;
817 
818  while (bits)
819  {
820  if (bits >= 8)
821  {
822  mask = 0xff;
823  bits -= 8;
824  }
825  else
826  {
827  mask = 0xff << (8 - bits);
828  bits = 0;
829  }
830 
831  b[byte] = mask;
832  byte++;
833  }
834 
835  ip_family(dst) = ip_family(ip);
836  ip_bits(dst) = ip_maxbits(ip);
837  SET_INET_VARSIZE(dst);
838 
839  PG_RETURN_INET_P(dst);
840 }
#define ip_bits(inetptr)
Definition: inet.h:72
#define ip_family(inetptr)
Definition: inet.h:69
#define ip_addr(inetptr)
Definition: inet.h:75
#define SET_INET_VARSIZE(dst)
Definition: inet.h:84
#define PG_RETURN_INET_P(x)
Definition: inet.h:110
#define ip_maxbits(inetptr)
Definition: inet.h:81
#define PG_GETARG_INET_PP(n)
Definition: inet.h:109
Definition: inet.h:50
#define byte(x, n)
Definition: rijndael.c:68
void * palloc0(Size size)
Definition: mcxt.c:841
Datum network_network ( PG_FUNCTION_ARGS  )

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

758 {
759  inet *ip = PG_GETARG_INET_PP(0);
760  inet *dst;
761  int byte;
762  int bits;
763  unsigned char mask;
764  unsigned char *a,
765  *b;
766 
767  /* make sure any unused bits are zeroed */
768  dst = (inet *) palloc0(sizeof(inet));
769 
770  bits = ip_bits(ip);
771  a = ip_addr(ip);
772  b = ip_addr(dst);
773 
774  byte = 0;
775 
776  while (bits)
777  {
778  if (bits >= 8)
779  {
780  mask = 0xff;
781  bits -= 8;
782  }
783  else
784  {
785  mask = 0xff << (8 - bits);
786  bits = 0;
787  }
788 
789  b[byte] = a[byte] & mask;
790  byte++;
791  }
792 
793  ip_family(dst) = ip_family(ip);
794  ip_bits(dst) = ip_bits(ip);
795  SET_INET_VARSIZE(dst);
796 
797  PG_RETURN_INET_P(dst);
798 }
#define ip_bits(inetptr)
Definition: inet.h:72
#define ip_family(inetptr)
Definition: inet.h:69
#define ip_addr(inetptr)
Definition: inet.h:75
#define SET_INET_VARSIZE(dst)
Definition: inet.h:84
#define PG_RETURN_INET_P(x)
Definition: inet.h:110
#define PG_GETARG_INET_PP(n)
Definition: inet.h:109
Definition: inet.h:50
#define byte(x, n)
Definition: rijndael.c:68
void * palloc0(Size size)
Definition: mcxt.c:841
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:72
#define ip_family(inetptr)
Definition: inet.h:69
char * pstrdup(const char *in)
Definition: mcxt.c:1080
#define ip_addr(inetptr)
Definition: inet.h:75
int errcode(int sqlerrcode)
Definition: elog.c:569
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
#define ERROR
Definition: elog.h:41
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:132
#define NULL
Definition: c.h:202
int errmsg(const char *fmt,...)
Definition: elog.c:791
Datum network_overlap ( PG_FUNCTION_ARGS  )

Definition at line 578 of file network.c.

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

579 {
580  inet *a1 = PG_GETARG_INET_PP(0);
581  inet *a2 = PG_GETARG_INET_PP(1);
582 
583  if (ip_family(a1) == ip_family(a2))
584  {
586  Min(ip_bits(a1), ip_bits(a2))) == 0);
587  }
588 
589  PG_RETURN_BOOL(false);
590 }
#define ip_bits(inetptr)
Definition: inet.h:72
#define ip_family(inetptr)
Definition: inet.h:69
#define ip_addr(inetptr)
Definition: inet.h:75
#define Min(x, y)
Definition: c.h:779
#define PG_GETARG_INET_PP(n)
Definition: inet.h:109
Definition: inet.h:50
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
static FormData_pg_attribute a1
Definition: heap.c:137
int bitncmp(const unsigned char *l, const unsigned char *r, int n)
Definition: network.c:1011
static FormData_pg_attribute a2
Definition: heap.c:143
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:72
#define ip_family(inetptr)
Definition: inet.h:69
static bool addressOK(unsigned char *a, int bits, int family)
Definition: network.c:1083
#define PGSQL_AF_INET
Definition: inet.h:37
#define ip_addr(inetptr)
Definition: inet.h:75
#define SET_INET_VARSIZE(dst)
Definition: inet.h:84
int errcode(int sqlerrcode)
Definition: elog.c:569
#define ip_addrsize(inetptr)
Definition: inet.h:78
#define PGSQL_AF_INET6
Definition: inet.h:38
#define ip_maxbits(inetptr)
Definition: inet.h:81
#define ERROR
Definition: elog.h:41
int errdetail(const char *fmt,...)
Definition: elog.c:864
Definition: inet.h:50
#define ereport(elevel, rest)
Definition: elog.h:132
void * palloc0(Size size)
Definition: mcxt.c:841
int pq_getmsgbyte(StringInfo msg)
Definition: pqformat.c:431
int errmsg(const char *fmt,...)
Definition: elog.c:791
int i
Datum network_scan_first ( Datum  in)

Definition at line 1132 of file network.c.

References DirectFunctionCall1, and network_network().

Referenced by network_prefix_quals().

1133 {
1135 }
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:548
Datum network_network(PG_FUNCTION_ARGS)
Definition: network.c:757
Datum network_scan_last ( Datum  in)

Definition at line 1146 of file network.c.

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

Referenced by network_prefix_quals().

1147 {
1150  Int32GetDatum(-1));
1151 }
Datum network_broadcast(PG_FUNCTION_ARGS)
Definition: network.c:708
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:548
Datum inet_set_masklen(PG_FUNCTION_ARGS)
Definition: network.c:309
#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:72
void pq_sendbyte(StringInfo buf, int byt)
Definition: pqformat.c:104
#define ip_family(inetptr)
Definition: inet.h:69
void pq_begintypsend(StringInfo buf)
Definition: pqformat.c:358
#define ip_addr(inetptr)
Definition: inet.h:75
#define ip_addrsize(inetptr)
Definition: inet.h:78
bytea * pq_endtypsend(StringInfo buf)
Definition: pqformat.c:378
static char * buf
Definition: pg_test_fsync.c:65
int i
Datum network_show ( PG_FUNCTION_ARGS  )

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

623 {
624  inet *ip = PG_GETARG_INET_PP(0);
625  int len;
626  char tmp[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255/128")];
627 
628  if (inet_net_ntop(ip_family(ip), ip_addr(ip), ip_maxbits(ip),
629  tmp, sizeof(tmp)) == NULL)
630  ereport(ERROR,
631  (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
632  errmsg("could not format inet value: %m")));
633 
634  /* Add /n if not present (which it won't be) */
635  if (strchr(tmp, '/') == NULL)
636  {
637  len = strlen(tmp);
638  snprintf(tmp + len, sizeof(tmp) - len, "/%u", ip_bits(ip));
639  }
640 
642 }
#define ip_bits(inetptr)
Definition: inet.h:72
#define ip_family(inetptr)
Definition: inet.h:69
#define ip_addr(inetptr)
Definition: inet.h:75
int errcode(int sqlerrcode)
Definition: elog.c:569
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
#define ip_maxbits(inetptr)
Definition: inet.h:81
#define ERROR
Definition: elog.h:41
#define PG_GETARG_INET_PP(n)
Definition: inet.h:109
Definition: inet.h:50
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:132
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:314
text * cstring_to_text(const char *s)
Definition: varlena.c:139
#define NULL
Definition: c.h:202
int errmsg(const char *fmt,...)
Definition: elog.c:791
Datum network_smaller ( PG_FUNCTION_ARGS  )

Definition at line 478 of file network.c.

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

479 {
480  inet *a1 = PG_GETARG_INET_PP(0);
481  inet *a2 = PG_GETARG_INET_PP(1);
482 
483  if (network_cmp_internal(a1, a2) < 0)
484  PG_RETURN_INET_P(a1);
485  else
486  PG_RETURN_INET_P(a2);
487 }
#define PG_RETURN_INET_P(x)
Definition: inet.h:110
static int32 network_cmp_internal(inet *a1, inet *a2)
Definition: network.c:389
#define PG_GETARG_INET_PP(n)
Definition: inet.h:109
Definition: inet.h:50
static FormData_pg_attribute a1
Definition: heap.c:137
static FormData_pg_attribute a2
Definition: heap.c:143
Datum network_sub ( 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:72
#define ip_family(inetptr)
Definition: inet.h:69
#define ip_addr(inetptr)
Definition: inet.h:75
#define PG_GETARG_INET_PP(n)
Definition: inet.h:109
Definition: inet.h:50
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
static FormData_pg_attribute a1
Definition: heap.c:137
int bitncmp(const unsigned char *l, const unsigned char *r, int n)
Definition: network.c:1011
static FormData_pg_attribute a2
Definition: heap.c:143
Datum network_subeq ( 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(a2)) == 0);
542  }
543 
544  PG_RETURN_BOOL(false);
545 }
#define ip_bits(inetptr)
Definition: inet.h:72
#define ip_family(inetptr)
Definition: inet.h:69
#define ip_addr(inetptr)
Definition: inet.h:75
#define PG_GETARG_INET_PP(n)
Definition: inet.h:109
Definition: inet.h:50
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
static FormData_pg_attribute a1
Definition: heap.c:137
int bitncmp(const unsigned char *l, const unsigned char *r, int n)
Definition: network.c:1011
static FormData_pg_attribute a2
Definition: heap.c:143
Datum network_sup ( 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:72
#define ip_family(inetptr)
Definition: inet.h:69
#define ip_addr(inetptr)
Definition: inet.h:75
#define PG_GETARG_INET_PP(n)
Definition: inet.h:109
Definition: inet.h:50
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
static FormData_pg_attribute a1
Definition: heap.c:137
int bitncmp(const unsigned char *l, const unsigned char *r, int n)
Definition: network.c:1011
static FormData_pg_attribute a2
Definition: heap.c:143
Datum network_supeq ( PG_FUNCTION_ARGS  )

Definition at line 563 of file network.c.

References a1, a2, bitncmp(), ip_addr, ip_bits, ip_family, 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  {
570  PG_RETURN_BOOL(ip_bits(a1) <= ip_bits(a2) &&
571  bitncmp(ip_addr(a1), ip_addr(a2), ip_bits(a1)) == 0);
572  }
573 
574  PG_RETURN_BOOL(false);
575 }
#define ip_bits(inetptr)
Definition: inet.h:72
#define ip_family(inetptr)
Definition: inet.h:69
#define ip_addr(inetptr)
Definition: inet.h:75
#define PG_GETARG_INET_PP(n)
Definition: inet.h:109
Definition: inet.h:50
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
static FormData_pg_attribute a1
Definition: heap.c:137
int bitncmp(const unsigned char *l, const unsigned char *r, int n)
Definition: network.c:1011
static FormData_pg_attribute a2
Definition: heap.c:143