PostgreSQL Source Code  git master
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
 
struct  macaddr8
 

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 DatumGetInetPP(X)   ((inet *) PG_DETOAST_DATUM_PACKED(X))
 
#define InetPGetDatum(X)   PointerGetDatum(X)
 
#define PG_GETARG_INET_PP(n)   DatumGetInetPP(PG_GETARG_DATUM(n))
 
#define PG_RETURN_INET_P(x)   return InetPGetDatum(x)
 
#define DatumGetInetP(X)   ((inet *) PG_DETOAST_DATUM(X))
 
#define PG_GETARG_INET_P(n)   DatumGetInetP(PG_GETARG_DATUM(n))
 
#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)
 
#define DatumGetMacaddr8P(X)   ((macaddr8 *) DatumGetPointer(X))
 
#define Macaddr8PGetDatum(X)   PointerGetDatum(X)
 
#define PG_GETARG_MACADDR8_P(n)   DatumGetMacaddr8P(PG_GETARG_DATUM(n))
 
#define PG_RETURN_MACADDR8_P(x)   return Macaddr8PGetDatum(x)
 

Typedefs

typedef struct macaddr macaddr
 
typedef struct macaddr8 macaddr8
 

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

◆ DatumGetInetP

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

Definition at line 127 of file inet.h.

◆ DatumGetInetPP

#define DatumGetInetPP (   X)    ((inet *) PG_DETOAST_DATUM_PACKED(X))

Definition at line 122 of file inet.h.

◆ DatumGetMacaddr8P

#define DatumGetMacaddr8P (   X)    ((macaddr8 *) DatumGetPointer(X))

Definition at line 137 of file inet.h.

◆ DatumGetMacaddrP

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

Definition at line 131 of file inet.h.

◆ InetPGetDatum

#define InetPGetDatum (   X)    PointerGetDatum(X)

Definition at line 123 of file inet.h.

◆ ip_addr

#define ip_addr (   inetptr)     (((inet_struct *) VARDATA_ANY(inetptr))->ipaddr)

Definition at line 77 of file inet.h.

◆ ip_addrsize

#define ip_addrsize (   inetptr)     (ip_family(inetptr) == PGSQL_AF_INET ? 4 : 16)

Definition at line 80 of file inet.h.

◆ ip_bits

#define ip_bits (   inetptr)     (((inet_struct *) VARDATA_ANY(inetptr))->bits)

Definition at line 74 of file inet.h.

◆ ip_family

#define ip_family (   inetptr)     (((inet_struct *) VARDATA_ANY(inetptr))->family)

Definition at line 71 of file inet.h.

◆ ip_maxbits

#define ip_maxbits (   inetptr)     (ip_family(inetptr) == PGSQL_AF_INET ? 32 : 128)

Definition at line 83 of file inet.h.

◆ Macaddr8PGetDatum

#define Macaddr8PGetDatum (   X)    PointerGetDatum(X)

Definition at line 138 of file inet.h.

◆ MacaddrPGetDatum

#define MacaddrPGetDatum (   X)    PointerGetDatum(X)

Definition at line 132 of file inet.h.

◆ PG_GETARG_INET_P

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

Definition at line 128 of file inet.h.

◆ PG_GETARG_INET_PP

#define PG_GETARG_INET_PP (   n)    DatumGetInetPP(PG_GETARG_DATUM(n))

Definition at line 124 of file inet.h.

◆ PG_GETARG_MACADDR8_P

#define PG_GETARG_MACADDR8_P (   n)    DatumGetMacaddr8P(PG_GETARG_DATUM(n))

Definition at line 139 of file inet.h.

◆ PG_GETARG_MACADDR_P

#define PG_GETARG_MACADDR_P (   n)    DatumGetMacaddrP(PG_GETARG_DATUM(n))

Definition at line 133 of file inet.h.

◆ PG_RETURN_INET_P

#define PG_RETURN_INET_P (   x)    return InetPGetDatum(x)

Definition at line 125 of file inet.h.

◆ PG_RETURN_MACADDR8_P

#define PG_RETURN_MACADDR8_P (   x)    return Macaddr8PGetDatum(x)

Definition at line 140 of file inet.h.

◆ PG_RETURN_MACADDR_P

#define PG_RETURN_MACADDR_P (   x)    return MacaddrPGetDatum(x)

Definition at line 134 of file inet.h.

◆ PGSQL_AF_INET

#define PGSQL_AF_INET   (AF_INET + 0)

Definition at line 39 of file inet.h.

◆ PGSQL_AF_INET6

#define PGSQL_AF_INET6   (AF_INET + 1)

Definition at line 40 of file inet.h.

◆ SET_INET_VARSIZE

#define SET_INET_VARSIZE (   dst)
Value:
#define offsetof(type, field)
Definition: c.h:727
#define VARHDRSZ
Definition: c.h:627
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:342
#define ip_addrsize(inetptr)
Definition: inet.h:80

Definition at line 86 of file inet.h.

Typedef Documentation

◆ macaddr

typedef struct macaddr macaddr

◆ macaddr8

typedef struct macaddr8 macaddr8

Function Documentation

◆ bitncmp()

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

Definition at line 1569 of file network.c.

1570 {
1571  unsigned int lb,
1572  rb;
1573  int x,
1574  b;
1575 
1576  b = n / 8;
1577  x = memcmp(l, r, b);
1578  if (x || (n % 8) == 0)
1579  return x;
1580 
1581  lb = l[b];
1582  rb = r[b];
1583  for (b = n % 8; b > 0; b--)
1584  {
1585  if (IS_HIGHBIT_SET(lb) != IS_HIGHBIT_SET(rb))
1586  {
1587  if (IS_HIGHBIT_SET(lb))
1588  return 1;
1589  return -1;
1590  }
1591  lb <<= 1;
1592  rb <<= 1;
1593  }
1594  return 0;
1595 }
#define IS_HIGHBIT_SET(ch)
Definition: c.h:1153
int b
Definition: isn.c:70
int x
Definition: isn.c:71

References b, IS_HIGHBIT_SET, and x.

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

◆ bitncommon()

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

Definition at line 1603 of file network.c.

1604 {
1605  int byte,
1606  nbits;
1607 
1608  /* number of bits to examine in last byte */
1609  nbits = n % 8;
1610 
1611  /* check whole bytes */
1612  for (byte = 0; byte < n / 8; byte++)
1613  {
1614  if (l[byte] != r[byte])
1615  {
1616  /* at least one bit in the last byte is not common */
1617  nbits = 7;
1618  break;
1619  }
1620  }
1621 
1622  /* check bits in last partial byte */
1623  if (nbits != 0)
1624  {
1625  /* calculate diff of first non-matching bytes */
1626  unsigned int diff = l[byte] ^ r[byte];
1627 
1628  /* compare the bits from the most to the least */
1629  while ((diff >> (8 - nbits)) != 0)
1630  nbits--;
1631  }
1632 
1633  return (8 * byte) + nbits;
1634 }

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

◆ cidr_set_masklen_internal()

inet* cidr_set_masklen_internal ( const inet src,
int  bits 
)

Definition at line 368 of file network.c.

369 {
370  inet *dst = (inet *) palloc0(sizeof(inet));
371 
372  ip_family(dst) = ip_family(src);
373  ip_bits(dst) = bits;
374 
375  if (bits > 0)
376  {
377  Assert(bits <= ip_maxbits(dst));
378 
379  /* Clone appropriate bytes of the address, leaving the rest 0 */
380  memcpy(ip_addr(dst), ip_addr(src), (bits + 7) / 8);
381 
382  /* Clear any unwanted bits in the last partial byte */
383  if (bits % 8)
384  ip_addr(dst)[bits / 8] &= ~(0xFF >> (bits % 8));
385  }
386 
387  /* Set varlena header correctly */
388  SET_INET_VARSIZE(dst);
389 
390  return dst;
391 }
Assert(fmt[strlen(fmt) - 1] !='\n')
void * palloc0(Size size)
Definition: mcxt.c:1099
Definition: inet.h:53
#define ip_addr(inetptr)
Definition: inet.h:77
#define SET_INET_VARSIZE(dst)
Definition: inet.h:86
#define ip_family(inetptr)
Definition: inet.h:71
#define ip_maxbits(inetptr)
Definition: inet.h:83
#define ip_bits(inetptr)
Definition: inet.h:74

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().