Coverage Report

Created: 2025-09-27 06:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/postgres/src/include/utils/inet.h
Line
Count
Source
1
/*-------------------------------------------------------------------------
2
 *
3
 * inet.h
4
 *    Declarations for operations on INET datatypes.
5
 *
6
 *
7
 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
8
 * Portions Copyright (c) 1994, Regents of the University of California
9
 *
10
 * src/include/utils/inet.h
11
 *
12
 *-------------------------------------------------------------------------
13
 */
14
#ifndef INET_H
15
#define INET_H
16
17
#include "fmgr.h"
18
19
/*
20
 *  This is the internal storage format for IP addresses
21
 *  (both INET and CIDR datatypes):
22
 */
23
typedef struct
24
{
25
  unsigned char family;   /* PGSQL_AF_INET or PGSQL_AF_INET6 */
26
  unsigned char bits;     /* number of bits in netmask */
27
  unsigned char ipaddr[16]; /* up to 128 bits of address */
28
} inet_struct;
29
30
/*
31
 * We use these values for the "family" field.
32
 *
33
 * Referencing all of the non-AF_INET types to AF_INET lets us work on
34
 * machines which did not have the appropriate address family (like
35
 * inet6 addresses when AF_INET6 wasn't present) but didn't cause a
36
 * dump/reload requirement.  Pre-7.4 databases used AF_INET for the family
37
 * type on disk.
38
 */
39
0
#define PGSQL_AF_INET (AF_INET + 0)
40
0
#define PGSQL_AF_INET6  (AF_INET + 1)
41
42
/*
43
 * Both INET and CIDR addresses are represented within Postgres as varlena
44
 * objects, ie, there is a varlena header in front of the struct type
45
 * depicted above.  This struct depicts what we actually have in memory
46
 * in "uncompressed" cases.  Note that since the maximum data size is only
47
 * 18 bytes, INET/CIDR will invariably be stored into tuples using the
48
 * 1-byte-header varlena format.  However, we have to be prepared to cope
49
 * with the 4-byte-header format too, because various code may helpfully
50
 * try to "decompress" 1-byte-header datums.
51
 */
52
typedef struct
53
{
54
  char    vl_len_[4];   /* Do not touch this field directly! */
55
  inet_struct inet_data;
56
} inet;
57
58
/*
59
 *  Access macros.  We use VARDATA_ANY so that we can process short-header
60
 *  varlena values without detoasting them.  This requires a trick:
61
 *  VARDATA_ANY assumes the varlena header is already filled in, which is
62
 *  not the case when constructing a new value (until SET_INET_VARSIZE is
63
 *  called, which we typically can't do till the end).  Therefore, we
64
 *  always initialize the newly-allocated value to zeroes (using palloc0).
65
 *  A zero length word will look like the not-1-byte case to VARDATA_ANY,
66
 *  and so we correctly construct an uncompressed value.
67
 *
68
 *  Note that ip_addrsize(), ip_maxbits(), and SET_INET_VARSIZE() require
69
 *  the family field to be set correctly.
70
 */
71
#define ip_family(inetptr) \
72
0
  (((inet_struct *) VARDATA_ANY(inetptr))->family)
73
74
#define ip_bits(inetptr) \
75
0
  (((inet_struct *) VARDATA_ANY(inetptr))->bits)
76
77
#define ip_addr(inetptr) \
78
0
  (((inet_struct *) VARDATA_ANY(inetptr))->ipaddr)
79
80
#define ip_addrsize(inetptr) \
81
0
  (ip_family(inetptr) == PGSQL_AF_INET ? 4 : 16)
82
83
#define ip_maxbits(inetptr) \
84
0
  (ip_family(inetptr) == PGSQL_AF_INET ? 32 : 128)
85
86
#define SET_INET_VARSIZE(dst) \
87
0
  SET_VARSIZE(dst, VARHDRSZ + offsetof(inet_struct, ipaddr) + \
88
0
        ip_addrsize(dst))
89
90
91
/*
92
 *  This is the internal storage format for MAC addresses:
93
 */
94
typedef struct macaddr
95
{
96
  unsigned char a;
97
  unsigned char b;
98
  unsigned char c;
99
  unsigned char d;
100
  unsigned char e;
101
  unsigned char f;
102
} macaddr;
103
104
/*
105
 *  This is the internal storage format for MAC8 addresses:
106
 */
107
typedef struct macaddr8
108
{
109
  unsigned char a;
110
  unsigned char b;
111
  unsigned char c;
112
  unsigned char d;
113
  unsigned char e;
114
  unsigned char f;
115
  unsigned char g;
116
  unsigned char h;
117
} macaddr8;
118
119
/*
120
 * fmgr interface macros
121
 */
122
static inline inet *
123
DatumGetInetPP(Datum X)
124
0
{
125
0
  return (inet *) PG_DETOAST_DATUM_PACKED(X);
126
0
}
Unexecuted instantiation: brin_minmax_multi.c:DatumGetInetPP
Unexecuted instantiation: inet_cidr_ntop.c:DatumGetInetPP
Unexecuted instantiation: inet_net_pton.c:DatumGetInetPP
Unexecuted instantiation: mac.c:DatumGetInetPP
Unexecuted instantiation: mac8.c:DatumGetInetPP
Unexecuted instantiation: network.c:DatumGetInetPP
Unexecuted instantiation: network_gist.c:DatumGetInetPP
Unexecuted instantiation: network_selfuncs.c:DatumGetInetPP
Unexecuted instantiation: network_spgist.c:DatumGetInetPP
Unexecuted instantiation: inet_net_ntop.c:DatumGetInetPP
127
128
static inline Datum
129
InetPGetDatum(const inet *X)
130
0
{
131
0
  return PointerGetDatum(X);
132
0
}
Unexecuted instantiation: brin_minmax_multi.c:InetPGetDatum
Unexecuted instantiation: inet_cidr_ntop.c:InetPGetDatum
Unexecuted instantiation: inet_net_pton.c:InetPGetDatum
Unexecuted instantiation: mac.c:InetPGetDatum
Unexecuted instantiation: mac8.c:InetPGetDatum
Unexecuted instantiation: network.c:InetPGetDatum
Unexecuted instantiation: network_gist.c:InetPGetDatum
Unexecuted instantiation: network_selfuncs.c:InetPGetDatum
Unexecuted instantiation: network_spgist.c:InetPGetDatum
Unexecuted instantiation: inet_net_ntop.c:InetPGetDatum
133
134
0
#define PG_GETARG_INET_PP(n) DatumGetInetPP(PG_GETARG_DATUM(n))
135
0
#define PG_RETURN_INET_P(x) return InetPGetDatum(x)
136
137
/* obsolescent variants */
138
static inline inet *
139
DatumGetInetP(Datum X)
140
0
{
141
0
  return (inet *) PG_DETOAST_DATUM(X);
142
0
}
Unexecuted instantiation: brin_minmax_multi.c:DatumGetInetP
Unexecuted instantiation: inet_cidr_ntop.c:DatumGetInetP
Unexecuted instantiation: inet_net_pton.c:DatumGetInetP
Unexecuted instantiation: mac.c:DatumGetInetP
Unexecuted instantiation: mac8.c:DatumGetInetP
Unexecuted instantiation: network.c:DatumGetInetP
Unexecuted instantiation: network_gist.c:DatumGetInetP
Unexecuted instantiation: network_selfuncs.c:DatumGetInetP
Unexecuted instantiation: network_spgist.c:DatumGetInetP
Unexecuted instantiation: inet_net_ntop.c:DatumGetInetP
143
#define PG_GETARG_INET_P(n) DatumGetInetP(PG_GETARG_DATUM(n))
144
145
/* macaddr is a fixed-length pass-by-reference datatype */
146
static inline macaddr *
147
DatumGetMacaddrP(Datum X)
148
0
{
149
0
  return (macaddr *) DatumGetPointer(X);
150
0
}
Unexecuted instantiation: brin_minmax_multi.c:DatumGetMacaddrP
Unexecuted instantiation: inet_cidr_ntop.c:DatumGetMacaddrP
Unexecuted instantiation: inet_net_pton.c:DatumGetMacaddrP
Unexecuted instantiation: mac.c:DatumGetMacaddrP
Unexecuted instantiation: mac8.c:DatumGetMacaddrP
Unexecuted instantiation: network.c:DatumGetMacaddrP
Unexecuted instantiation: network_gist.c:DatumGetMacaddrP
Unexecuted instantiation: network_selfuncs.c:DatumGetMacaddrP
Unexecuted instantiation: network_spgist.c:DatumGetMacaddrP
Unexecuted instantiation: inet_net_ntop.c:DatumGetMacaddrP
151
152
static inline Datum
153
MacaddrPGetDatum(const macaddr *X)
154
0
{
155
0
  return PointerGetDatum(X);
156
0
}
Unexecuted instantiation: brin_minmax_multi.c:MacaddrPGetDatum
Unexecuted instantiation: inet_cidr_ntop.c:MacaddrPGetDatum
Unexecuted instantiation: inet_net_pton.c:MacaddrPGetDatum
Unexecuted instantiation: mac.c:MacaddrPGetDatum
Unexecuted instantiation: mac8.c:MacaddrPGetDatum
Unexecuted instantiation: network.c:MacaddrPGetDatum
Unexecuted instantiation: network_gist.c:MacaddrPGetDatum
Unexecuted instantiation: network_selfuncs.c:MacaddrPGetDatum
Unexecuted instantiation: network_spgist.c:MacaddrPGetDatum
Unexecuted instantiation: inet_net_ntop.c:MacaddrPGetDatum
157
158
0
#define PG_GETARG_MACADDR_P(n) DatumGetMacaddrP(PG_GETARG_DATUM(n))
159
0
#define PG_RETURN_MACADDR_P(x) return MacaddrPGetDatum(x)
160
161
/* macaddr8 is a fixed-length pass-by-reference datatype */
162
static inline macaddr8 *
163
DatumGetMacaddr8P(Datum X)
164
0
{
165
0
  return (macaddr8 *) DatumGetPointer(X);
166
0
}
Unexecuted instantiation: brin_minmax_multi.c:DatumGetMacaddr8P
Unexecuted instantiation: inet_cidr_ntop.c:DatumGetMacaddr8P
Unexecuted instantiation: inet_net_pton.c:DatumGetMacaddr8P
Unexecuted instantiation: mac.c:DatumGetMacaddr8P
Unexecuted instantiation: mac8.c:DatumGetMacaddr8P
Unexecuted instantiation: network.c:DatumGetMacaddr8P
Unexecuted instantiation: network_gist.c:DatumGetMacaddr8P
Unexecuted instantiation: network_selfuncs.c:DatumGetMacaddr8P
Unexecuted instantiation: network_spgist.c:DatumGetMacaddr8P
Unexecuted instantiation: inet_net_ntop.c:DatumGetMacaddr8P
167
168
static inline Datum
169
Macaddr8PGetDatum(const macaddr8 *X)
170
0
{
171
0
  return PointerGetDatum(X);
172
0
}
Unexecuted instantiation: brin_minmax_multi.c:Macaddr8PGetDatum
Unexecuted instantiation: inet_cidr_ntop.c:Macaddr8PGetDatum
Unexecuted instantiation: inet_net_pton.c:Macaddr8PGetDatum
Unexecuted instantiation: mac.c:Macaddr8PGetDatum
Unexecuted instantiation: mac8.c:Macaddr8PGetDatum
Unexecuted instantiation: network.c:Macaddr8PGetDatum
Unexecuted instantiation: network_gist.c:Macaddr8PGetDatum
Unexecuted instantiation: network_selfuncs.c:Macaddr8PGetDatum
Unexecuted instantiation: network_spgist.c:Macaddr8PGetDatum
Unexecuted instantiation: inet_net_ntop.c:Macaddr8PGetDatum
173
174
0
#define PG_GETARG_MACADDR8_P(n) DatumGetMacaddr8P(PG_GETARG_DATUM(n))
175
0
#define PG_RETURN_MACADDR8_P(x) return Macaddr8PGetDatum(x)
176
177
/*
178
 * Support functions in network.c
179
 */
180
extern inet *cidr_set_masklen_internal(const inet *src, int bits);
181
extern int  bitncmp(const unsigned char *l, const unsigned char *r, int n);
182
extern int  bitncommon(const unsigned char *l, const unsigned char *r, int n);
183
184
#endif              /* INET_H */