PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
inet.h File Reference
#include "fmgr.h"
Include dependency graph for inet.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  inet_struct
 
struct  inet
 
struct  macaddr
 

Macros

#define PGSQL_AF_INET   (AF_INET + 0)
 
#define PGSQL_AF_INET6   (AF_INET + 1)
 
#define ip_family(inetptr)   (((inet_struct *) VARDATA_ANY(inetptr))->family)
 
#define ip_bits(inetptr)   (((inet_struct *) VARDATA_ANY(inetptr))->bits)
 
#define ip_addr(inetptr)   (((inet_struct *) VARDATA_ANY(inetptr))->ipaddr)
 
#define ip_addrsize(inetptr)   (ip_family(inetptr) == PGSQL_AF_INET ? 4 : 16)
 
#define ip_maxbits(inetptr)   (ip_family(inetptr) == PGSQL_AF_INET ? 32 : 128)
 
#define SET_INET_VARSIZE(dst)
 
#define DatumGetInetP(X)   ((inet *) PG_DETOAST_DATUM(X))
 
#define DatumGetInetPP(X)   ((inet *) PG_DETOAST_DATUM_PACKED(X))
 
#define InetPGetDatum(X)   PointerGetDatum(X)
 
#define PG_GETARG_INET_P(n)   DatumGetInetP(PG_GETARG_DATUM(n))
 
#define PG_GETARG_INET_PP(n)   DatumGetInetPP(PG_GETARG_DATUM(n))
 
#define PG_RETURN_INET_P(x)   return InetPGetDatum(x)
 
#define DatumGetMacaddrP(X)   ((macaddr *) DatumGetPointer(X))
 
#define MacaddrPGetDatum(X)   PointerGetDatum(X)
 
#define PG_GETARG_MACADDR_P(n)   DatumGetMacaddrP(PG_GETARG_DATUM(n))
 
#define PG_RETURN_MACADDR_P(x)   return MacaddrPGetDatum(x)
 

Typedefs

typedef struct macaddr macaddr
 

Functions

inetcidr_set_masklen_internal (const inet *src, int bits)
 
int bitncmp (const unsigned char *l, const unsigned char *r, int n)
 
int bitncommon (const unsigned char *l, const unsigned char *r, int n)
 

Macro Definition Documentation

#define DatumGetInetP (   X)    ((inet *) PG_DETOAST_DATUM(X))

Definition at line 107 of file inet.h.

#define DatumGetMacaddrP (   X)    ((macaddr *) DatumGetPointer(X))

Definition at line 114 of file inet.h.

Referenced by convert_network_to_scalar().

#define InetPGetDatum (   X)    PointerGetDatum(X)
#define ip_addrsize (   inetptr)    (ip_family(inetptr) == PGSQL_AF_INET ? 4 : 16)
#define MacaddrPGetDatum (   X)    PointerGetDatum(X)

Definition at line 115 of file inet.h.

Referenced by gbt_macadkey_cmp(), and leftmostvalue_macaddr().

#define PG_GETARG_INET_P (   n)    DatumGetInetP(PG_GETARG_DATUM(n))

Definition at line 110 of file inet.h.

#define PG_RETURN_MACADDR_P (   x)    return MacaddrPGetDatum(x)

Definition at line 117 of file inet.h.

Referenced by macaddr_and(), macaddr_in(), macaddr_not(), macaddr_or(), macaddr_recv(), and macaddr_trunc().

#define PGSQL_AF_INET6   (AF_INET + 1)
#define SET_INET_VARSIZE (   dst)
Value:
#define VARHDRSZ
Definition: c.h:441
#define ip_addrsize(inetptr)
Definition: inet.h:80
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:330
#define offsetof(type, field)
Definition: c.h:551

Definition at line 86 of file inet.h.

Referenced by cidr_set_masklen_internal(), inet_gist_fetch(), inetand(), inetnot(), inetor(), internal_inetpl(), network_broadcast(), network_hostmask(), network_in(), network_netmask(), network_network(), and network_recv().

Typedef Documentation

Function Documentation

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:969
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
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:671