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
 
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

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

Definition at line 127 of file inet.h.

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

Definition at line 137 of file inet.h.

Referenced by convert_network_to_scalar().

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

Definition at line 131 of file inet.h.

Referenced by convert_network_to_scalar(), macaddr_abbrev_convert(), and macaddr_fast_cmp().

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

Definition at line 138 of file inet.h.

Referenced by gbt_macad8key_cmp(), and leftmostvalue_macaddr8().

#define MacaddrPGetDatum (   X)    PointerGetDatum(X)

Definition at line 132 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 128 of file inet.h.

#define PG_RETURN_MACADDR8_P (   x)    return Macaddr8PGetDatum(x)
#define PG_RETURN_MACADDR_P (   x)    return MacaddrPGetDatum(x)
#define PGSQL_AF_INET6   (AF_INET + 1)
#define SET_INET_VARSIZE (   dst)
Value:
#define VARHDRSZ
Definition: c.h:439
#define ip_addrsize(inetptr)
Definition: inet.h:80
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:328
#define offsetof(type, field)
Definition: c.h:549

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 980 of file network.c.

References IS_HIGHBIT_SET.

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

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

Definition at line 1014 of file network.c.

References byte.

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

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

Definition at line 327 of file network.c.

References Assert, ip_addr, ip_bits, ip_family, ip_maxbits, palloc0(), and SET_INET_VARSIZE.

Referenced by cidr_set_masklen(), inet_merge(), inet_spg_choose(), inet_spg_picksplit(), and inet_to_cidr().

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