1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
|
--
-- PostgreSQL code for IP addresses.
--
-- $Id: ip.sql,v 1.4 1998/06/16 04:34:30 momjian Exp $
-- Invoced from 1998/02/14 17:58:04 scrappy
--
-- New - INPUT/OUTPUT, functions, indexing by btree, test.
-- PART # 1 - ip.sql - load new type, functions and operators.
-- Then you should execute ipi.sql - add ipaddr_ops class to allow indexing.
load '/usr/local/pgsql/modules/ip.so';
--
-- Input and output functions and the type itself:
-- Note - we input 193.124.23.1 as /32, and 193.124.23.0 as /24.
-- We output /24 network withouth /24 suffix, and /32 hosts wothouth suffixes
-- if it is not '0' address of /24 network.
-- Just the same, we threat 0.0.0.0 as 0.0.0.0/0 == DEFAULT.
--
create function ipaddr_in(opaque)
returns opaque
as '/usr/local/pgsql/modules/ip.so'
language 'c';
create function ipaddr_out(opaque)
returns opaque
as '/usr/local/pgsql/modules/ip.so'
language 'c';
create type ipaddr (
internallength = 6,
externallength = variable,
input = ipaddr_in,
output = ipaddr_out
);
--
-- Print address by format
-- %A - address
-- %P - /Pref
-- %M - maska
-- %B - reversed maska
drop function ipaddr_print;
create function ipaddr_print(ipaddr, text)
returns text
as '/usr/local/pgsql/modules/ip.so'
language 'c';
);
--
-- The various boolean tests:
-- In case if addresseas are equal, we compare prefix length
-- It means 193.124.23.0/24 < 193.124.23.0/32
--
create function ipaddr_lt(ipaddr, ipaddr)
returns bool
as '/usr/local/pgsql/modules/ip.so'
language 'c';
create function ipaddr_le(ipaddr, ipaddr)
returns bool
as '/usr/local/pgsql/modules/ip.so'
language 'c';
create function ipaddr_eq(ipaddr, ipaddr)
returns bool
as '/usr/local/pgsql/modules/ip.so'
language 'c';
create function ipaddr_ge(ipaddr, ipaddr)
returns bool
as '/usr/local/pgsql/modules/ip.so'
language 'c';
create function ipaddr_gt(ipaddr, ipaddr)
returns bool
as '/usr/local/pgsql/modules/ip.so'
language 'c';
create function ipaddr_ne(ipaddr, ipaddr)
returns bool
as '/usr/local/pgsql/modules/ip.so'
language 'c';
--
-- Test if a1 is in net a2
-- Return TRUE if a1 is IN a2 subnet or if a1 == a2
--
create function ipaddr_in_net(ipaddr, ipaddr)
returns bool
as '/usr/local/pgsql/modules/ip.so'
language 'c';
--
-- Return the network from the host/network address. This means
-- 193.124.23.4/24 -> 193.124.23.0/24.
-- This allow to use interface address (with the real netmask) to create
-- network, and to link interfaces and addresses belongs to the same network.
--
create function ipaddr_net(ipaddr)
returns ipaddr
as '/usr/local/pgsql/modules/ip.so'
language 'c';
--
-- Return TRUE if addr describe NETWORK, not host in the network
-- It's equivalent to ipaddr_net(a) == a
--
create function ipaddr_is_net(ipaddr)
returns boolean
as '/usr/local/pgsql/modules/ip.so'
language 'c';
--
-- Return the number of the hosts in the network
--
create function ipaddr_len(ipaddr)
returns int4
as '/usr/local/pgsql/modules/ip.so'
language 'c';
--
-- Return the prefix length of the network
--
create function ipaddr_pref(ipaddr)
returns int4
as '/usr/local/pgsql/modules/ip.so'
language 'c';
--
-- Convert network into the integer.
-- Can be used for 'compose' function
--
create function ipaddr_integer(ipaddr)
returns int4
as '/usr/local/pgsql/modules/ip.so'
language 'c';
--
-- Compose ipaddr from the ADDRESS and PREF
-- ipaddr_compose(ipaddr_integer(a),ipaddr_pref(a)) == a
--
create function ipaddr_compose(int4,int4)
returns ipaddr
as '/usr/local/pgsql/modules/ip.so'
language 'c';
--
-- Return MASK for the network
--
create function ipaddr_mask(ipaddr)
returns ipaddr
as '/usr/local/pgsql/modules/ip.so'
language 'c';
--
-- Return BROADCAST address for the network
--
create function ipaddr_bcast(ipaddr)
returns ipaddr
as '/usr/local/pgsql/modules/ip.so'
language 'c';
--
-- Compare 2 addresses. First, compare addresses, then, compare prefixes (if the addresses
-- are the same).
--
create function ipaddr_cmp(ipaddr,ipaddr)
returns int4
as '/usr/local/pgsql/modules/ip.so'
language 'c';
--
-- Plus and Minus operators
--
create function ipaddr_plus(ipaddr,int4)
returns ipaddr
as '/usr/local/pgsql/modules/ip.so'
language 'c';
create function ipaddr_minus(ipaddr,int4)
returns ipaddr
as '/usr/local/pgsql/modules/ip.so'
language 'c';
--
-- Now the operators. Note how some of the parameters to some
-- of the 'create operator' commands are commented out. This
-- is because they reference as yet undefined operators, and
-- will be implicitly defined when those are, further down.
--
-- drop operator < ( ipaddr, ipaddr);
create operator < (
leftarg = ipaddr,
rightarg = ipaddr,
-- negator = >=,
procedure = ipaddr_lt,
restrict = intltsel,
join = intltjoinsel
);
-- drop operator <= (ipaddr,ipaddr);
create operator <= (
leftarg = ipaddr,
rightarg = ipaddr,
-- negator = >,
procedure = ipaddr_le,
restrict = intltsel,
join = intltjoinsel
);
-- drop operator = (ipaddr,ipaddr);
create operator = (
leftarg = ipaddr,
rightarg = ipaddr,
commutator = =,
-- negator = <>,
restrict = eqsel,
join = eqjoinsel,
procedure = ipaddr_eq
);
-- drop operator >= (ipaddr,ipaddr);
create operator >= (
leftarg = ipaddr,
rightarg = ipaddr,
negator = <,
procedure = ipaddr_ge,
restrict = intgtsel,
join = intgtjoinsel
);
-- drop operator > (ipaddr,ipaddr);
create operator > (
leftarg = ipaddr,
rightarg = ipaddr,
negator = <=,
procedure = ipaddr_gt,
restrict = intgtsel,
join = intgtjoinsel
);
-- drop operator <> (ipaddr,ipaddr);
create operator <> (
leftarg = ipaddr,
rightarg = ipaddr,
negator = =,
procedure = ipaddr_ne,
restrict = neqsel,
join = neqjoinsel
);
create operator @ (
leftarg = ipaddr,
rightarg = ipaddr,
procedure = ipaddr_in_net
);
create operator + (
leftarg = ipaddr,
rightarg = int4,
procedure = ipaddr_plus
);
create operator - (
leftarg = ipaddr,
rightarg = int4,
procedure = ipaddr_minus
);
-- *****************************************************************************************
-- * For now, you have: input/output (remember, '193.124.23.0' means /24 network, *
-- * '193.124.23.1' means /32 host) *
-- * <, <=, = <>, >=, > relational operations; host @ net (host is the part of the net) op *
-- * varchar ipaddr_print(addr, '%A/%P %M %B') - print by pattern function *
-- * ipaddr ipaddr_mask(a),ipaddr_bcast(a),ipaddr_net(a) functions (mask,bcast, start addr)*
-- * int4 ipaddr_len(a) - lenght of subnet; ipaddr_pref(a) - prefix length, *
-- * int4 ipaddr_integer(a) - integer value; ipaddr ipaddr_compose(integer_addr,pref_len) *
-- * compose ipaddr from addr and mask *
-- * '+' and '-' operators (ipaddr = ipaddr + integer),(ipaddr = ipaddr - integer) ops *
-- *****************************************************************************************
-- * R E A D T H I S T E X T B E F O R E E X I T I N G : *
-- * Now you should execute ipi.sql to allow BTREE indexing on this class. *
-- *****************************************************************************************
-- eof
|