PostgreSQL Source Code git master
Loading...
Searching...
No Matches
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 PG_GETARG_INET_PP(n)   DatumGetInetPP(PG_GETARG_DATUM(n))
 
#define PG_RETURN_INET_P(x)   return InetPGetDatum(x)
 
#define PG_GETARG_INET_P(n)   DatumGetInetP(PG_GETARG_DATUM(n))
 
#define PG_GETARG_MACADDR_P(n)   DatumGetMacaddrP(PG_GETARG_DATUM(n))
 
#define PG_RETURN_MACADDR_P(x)   return MacaddrPGetDatum(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

static inetDatumGetInetPP (Datum X)
 
static Datum InetPGetDatum (const inet *X)
 
static inetDatumGetInetP (Datum X)
 
static macaddrDatumGetMacaddrP (Datum X)
 
static Datum MacaddrPGetDatum (const macaddr *X)
 
static macaddr8DatumGetMacaddr8P (Datum X)
 
static Datum Macaddr8PGetDatum (const macaddr8 *X)
 
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

◆ ip_addr

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

Definition at line 77 of file inet.h.

80 : 16)
81
83 (ip_family(inetptr) == PGSQL_AF_INET ? 32 : 128)
84
88
89
90/*
91 * This is the internal storage format for MAC addresses:
92 */
93typedef struct macaddr
94{
95 unsigned char a;
96 unsigned char b;
97 unsigned char c;
98 unsigned char d;
99 unsigned char e;
100 unsigned char f;
101} macaddr;
102
103/*
104 * This is the internal storage format for MAC8 addresses:
105 */
106typedef struct macaddr8
107{
108 unsigned char a;
109 unsigned char b;
110 unsigned char c;
111 unsigned char d;
112 unsigned char e;
113 unsigned char f;
114 unsigned char g;
115 unsigned char h;
116} macaddr8;
117
118/*
119 * fmgr interface macros
120 */
121static inline inet *
123{
124 return (inet *) PG_DETOAST_DATUM_PACKED(X);
125}
126
127static inline Datum
128InetPGetDatum(const inet *X)
129{
130 return PointerGetDatum(X);
131}
132
133#define PG_GETARG_INET_PP(n) DatumGetInetPP(PG_GETARG_DATUM(n))
134#define PG_RETURN_INET_P(x) return InetPGetDatum(x)
135
136/* obsolescent variants */
137static inline inet *
139{
140 return (inet *) PG_DETOAST_DATUM(X);
141}
142#define PG_GETARG_INET_P(n) DatumGetInetP(PG_GETARG_DATUM(n))
143
144/* macaddr is a fixed-length pass-by-reference datatype */
145static inline macaddr *
147{
148 return (macaddr *) DatumGetPointer(X);
149}
150
151static inline Datum
153{
154 return PointerGetDatum(X);
155}
156
157#define PG_GETARG_MACADDR_P(n) DatumGetMacaddrP(PG_GETARG_DATUM(n))
158#define PG_RETURN_MACADDR_P(x) return MacaddrPGetDatum(x)
159
160/* macaddr8 is a fixed-length pass-by-reference datatype */
161static inline macaddr8 *
163{
164 return (macaddr8 *) DatumGetPointer(X);
165}
166
167static inline Datum
169{
170 return PointerGetDatum(X);
171}
172
173#define PG_GETARG_MACADDR8_P(n) DatumGetMacaddr8P(PG_GETARG_DATUM(n))
174#define PG_RETURN_MACADDR8_P(x) return Macaddr8PGetDatum(x)
175
176/*
177 * Support functions in network.c
178 */
179extern inet *cidr_set_masklen_internal(const inet *src, int bits);
180extern int bitncmp(const unsigned char *l, const unsigned char *r, int n);
181extern int bitncommon(const unsigned char *l, const unsigned char *r, int n);
182
183#endif /* INET_H */
#define VARHDRSZ
Definition c.h:721
#define PG_DETOAST_DATUM_PACKED(datum)
Definition fmgr.h:248
#define PG_DETOAST_DATUM(datum)
Definition fmgr.h:240
static Datum PointerGetDatum(const void *X)
Definition postgres.h:352
uint64_t Datum
Definition postgres.h:70
static Pointer DatumGetPointer(Datum X)
Definition postgres.h:342
static int fb(int x)
Definition inet.h:53
unsigned char c
Definition inet.h:111
unsigned char b
Definition inet.h:110
unsigned char d
Definition inet.h:112
unsigned char e
Definition inet.h:113
unsigned char g
Definition inet.h:115
unsigned char h
Definition inet.h:116
unsigned char a
Definition inet.h:109
unsigned char f
Definition inet.h:114
Definition inet.h:95
unsigned char e
Definition inet.h:100
unsigned char b
Definition inet.h:97
unsigned char f
Definition inet.h:101
unsigned char c
Definition inet.h:98
unsigned char a
Definition inet.h:96
unsigned char d
Definition inet.h:99
static Datum MacaddrPGetDatum(const macaddr *X)
Definition inet.h:153
static Datum InetPGetDatum(const inet *X)
Definition inet.h:129
int bitncommon(const unsigned char *l, const unsigned char *r, int n)
Definition network.c:1536
inet * cidr_set_masklen_internal(const inet *src, int bits)
Definition network.c:364
static macaddr8 * DatumGetMacaddr8P(Datum X)
Definition inet.h:163
static inet * DatumGetInetPP(Datum X)
Definition inet.h:123
static inet * DatumGetInetP(Datum X)
Definition inet.h:139
#define SET_INET_VARSIZE(dst)
Definition inet.h:86
int bitncmp(const unsigned char *l, const unsigned char *r, int n)
Definition network.c:1502
#define ip_family(inetptr)
Definition inet.h:71
static macaddr * DatumGetMacaddrP(Datum X)
Definition inet.h:147
#define PGSQL_AF_INET
Definition inet.h:39
#define ip_addrsize(inetptr)
Definition inet.h:80
#define ip_maxbits(inetptr)
Definition inet.h:83
static Datum Macaddr8PGetDatum(const macaddr8 *X)
Definition inet.h:169

◆ ip_addrsize

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

Definition at line 80 of file inet.h.

81 : 16)

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

84 : 128)

◆ PG_GETARG_INET_P

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

Definition at line 143 of file inet.h.

◆ PG_GETARG_INET_PP

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

Definition at line 134 of file inet.h.

◆ PG_GETARG_MACADDR8_P

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

Definition at line 174 of file inet.h.

◆ PG_GETARG_MACADDR_P

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

Definition at line 158 of file inet.h.

◆ PG_RETURN_INET_P

#define PG_RETURN_INET_P (   x)    return InetPGetDatum(x)

Definition at line 135 of file inet.h.

◆ PG_RETURN_MACADDR8_P

#define PG_RETURN_MACADDR8_P (   x)    return Macaddr8PGetDatum(x)

Definition at line 175 of file inet.h.

◆ PG_RETURN_MACADDR_P

#define PG_RETURN_MACADDR_P (   x)    return MacaddrPGetDatum(x)

Definition at line 159 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:
static void SET_VARSIZE(void *PTR, Size len)
Definition varatt.h:432

Definition at line 86 of file inet.h.

Typedef Documentation

◆ macaddr

◆ macaddr8

Function Documentation

◆ bitncmp()

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

Definition at line 1502 of file network.c.

1503{
1504 unsigned int lb,
1505 rb;
1506 int x,
1507 b;
1508
1509 b = n / 8;
1510 x = memcmp(l, r, b);
1511 if (x || (n % 8) == 0)
1512 return x;
1513
1514 lb = l[b];
1515 rb = r[b];
1516 for (b = n % 8; b > 0; b--)
1517 {
1518 if (IS_HIGHBIT_SET(lb) != IS_HIGHBIT_SET(rb))
1519 {
1520 if (IS_HIGHBIT_SET(lb))
1521 return 1;
1522 return -1;
1523 }
1524 lb <<= 1;
1525 rb <<= 1;
1526 }
1527 return 0;
1528}
#define IS_HIGHBIT_SET(ch)
Definition c.h:1144
int b
Definition isn.c:74
int x
Definition isn.c:75

References b, fb(), 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 
)
extern

Definition at line 1536 of file network.c.

1537{
1538 int byte,
1539 nbits;
1540
1541 /* number of bits to examine in last byte */
1542 nbits = n % 8;
1543
1544 /* check whole bytes */
1545 for (byte = 0; byte < n / 8; byte++)
1546 {
1547 if (l[byte] != r[byte])
1548 {
1549 /* at least one bit in the last byte is not common */
1550 nbits = 7;
1551 break;
1552 }
1553 }
1554
1555 /* check bits in last partial byte */
1556 if (nbits != 0)
1557 {
1558 /* calculate diff of first non-matching bytes */
1559 unsigned int diff = l[byte] ^ r[byte];
1560
1561 /* compare the bits from the most to the least */
1562 while ((diff >> (8 - nbits)) != 0)
1563 nbits--;
1564 }
1565
1566 return (8 * byte) + nbits;
1567}

References fb().

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

Definition at line 364 of file network.c.

365{
367
368 ip_family(dst) = ip_family(src);
369 ip_bits(dst) = bits;
370
371 if (bits > 0)
372 {
373 Assert(bits <= ip_maxbits(dst));
374
375 /* Clone appropriate bytes of the address, leaving the rest 0 */
376 memcpy(ip_addr(dst), ip_addr(src), (bits + 7) / 8);
377
378 /* Clear any unwanted bits in the last partial byte */
379 if (bits % 8)
380 ip_addr(dst)[bits / 8] &= ~(0xFF >> (bits % 8));
381 }
382
383 /* Set varlena header correctly */
385
386 return dst;
387}
#define Assert(condition)
Definition c.h:883
#define palloc0_object(type)
Definition fe_memutils.h:75
#define ip_addr(inetptr)
Definition inet.h:77
#define ip_bits(inetptr)
Definition inet.h:74

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

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

◆ DatumGetInetP()

static inet * DatumGetInetP ( Datum  X)
inlinestatic

Definition at line 139 of file inet.h.

140{
141 return (inet *) PG_DETOAST_DATUM(X);
142}

References fb(), and PG_DETOAST_DATUM.

◆ DatumGetInetPP()

◆ DatumGetMacaddr8P()

static macaddr8 * DatumGetMacaddr8P ( Datum  X)
inlinestatic

Definition at line 163 of file inet.h.

164{
165 return (macaddr8 *) DatumGetPointer(X);
166}

References DatumGetPointer(), and fb().

Referenced by convert_network_to_scalar().

◆ DatumGetMacaddrP()

static macaddr * DatumGetMacaddrP ( Datum  X)
inlinestatic

Definition at line 147 of file inet.h.

148{
149 return (macaddr *) DatumGetPointer(X);
150}

References DatumGetPointer(), and fb().

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

◆ InetPGetDatum()

static Datum InetPGetDatum ( const inet X)
inlinestatic

Definition at line 129 of file inet.h.

130{
131 return PointerGetDatum(X);
132}

References fb(), and PointerGetDatum().

Referenced by inet_gist_fetch(), inet_spg_choose(), inet_spg_leaf_consistent(), and inet_spg_picksplit().

◆ Macaddr8PGetDatum()

static Datum Macaddr8PGetDatum ( const macaddr8 X)
inlinestatic

Definition at line 169 of file inet.h.

170{
171 return PointerGetDatum(X);
172}

References fb(), and PointerGetDatum().

Referenced by gbt_macad8key_cmp(), gbt_macaddr8_ssup_cmp(), and leftmostvalue_macaddr8().

◆ MacaddrPGetDatum()

static Datum MacaddrPGetDatum ( const macaddr X)
inlinestatic

Definition at line 153 of file inet.h.

154{
155 return PointerGetDatum(X);
156}

References fb(), and PointerGetDatum().

Referenced by gbt_macaddr_ssup_cmp(), gbt_macadkey_cmp(), and leftmostvalue_macaddr().