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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
|
/*
* PostgreSQL type definitions for the INET type. This
* is for IP V4 CIDR notation, but prepared for V6: just
* add the necessary bits where the comments indicate.
*
* $Header: /cvsroot/pgsql/src/backend/utils/adt/network.c,v 1.29 2001/03/22 03:59:52 momjian Exp $
*
* Jon Postel RIP 16 Oct 1998
*/
#include "postgres.h"
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "utils/builtins.h"
#include "utils/inet.h"
static int32 network_cmp_internal(inet *a1, inet *a2);
static int v4bitncmp(unsigned long a1, unsigned long a2, int bits);
static bool v4addressOK(unsigned long a1, int bits);
/*
* Access macros. Add IPV6 support.
*/
#define ip_addrsize(inetptr) \
(((inet_struct *)VARDATA(inetptr))->family == AF_INET ? 4 : -1)
#define ip_family(inetptr) \
(((inet_struct *)VARDATA(inetptr))->family)
#define ip_bits(inetptr) \
(((inet_struct *)VARDATA(inetptr))->bits)
#define ip_type(inetptr) \
(((inet_struct *)VARDATA(inetptr))->type)
#define ip_v4addr(inetptr) \
(((inet_struct *)VARDATA(inetptr))->addr.ipv4_addr)
/* Common input routine */
static inet *
network_in(char *src, int type)
{
int bits;
inet *dst;
dst = (inet *) palloc(VARHDRSZ + sizeof(inet_struct));
/* make sure any unused bits in a CIDR value are zeroed */
MemSet(dst, 0, VARHDRSZ + sizeof(inet_struct));
/* First, try for an IP V4 address: */
ip_family(dst) = AF_INET;
bits = inet_net_pton(ip_family(dst), src, &ip_v4addr(dst),
type ? ip_addrsize(dst) : -1);
if ((bits < 0) || (bits > 32))
{
/* Go for an IPV6 address here, before faulting out: */
elog(ERROR, "invalid %s value '%s'",
type ? "CIDR" : "INET", src);
}
/*
* Error check: CIDR values must not have any bits set beyond the
* masklen. XXX this code is not IPV6 ready.
*/
if (type)
{
if (!v4addressOK(ip_v4addr(dst), bits))
elog(ERROR, "invalid CIDR value '%s': has bits set to right of mask", src);
}
VARATT_SIZEP(dst) = VARHDRSZ
+ ((char *) &ip_v4addr(dst) - (char *) VARDATA(dst))
+ ip_addrsize(dst);
ip_bits(dst) = bits;
ip_type(dst) = type;
return dst;
}
/* INET address reader. */
Datum
inet_in(PG_FUNCTION_ARGS)
{
char *src = PG_GETARG_CSTRING(0);
PG_RETURN_INET_P(network_in(src, 0));
}
/* CIDR address reader. */
Datum
cidr_in(PG_FUNCTION_ARGS)
{
char *src = PG_GETARG_CSTRING(0);
PG_RETURN_INET_P(network_in(src, 1));
}
/*
* INET address output function.
*/
Datum
inet_out(PG_FUNCTION_ARGS)
{
inet *src = PG_GETARG_INET_P(0);
char tmp[sizeof("255.255.255.255/32")];
char *dst;
int len;
if (ip_family(src) == AF_INET)
{
/* It's an IP V4 address: */
/*
* Use inet style for both inet and cidr, since we don't want
* abbreviated CIDR style here.
*/
dst = inet_net_ntop(AF_INET, &ip_v4addr(src), ip_bits(src),
tmp, sizeof(tmp));
if (dst == NULL)
elog(ERROR, "unable to print address (%s)", strerror(errno));
/* For CIDR, add /n if not present */
if (ip_type(src) && strchr(tmp, '/') == NULL)
{
len = strlen(tmp);
snprintf(tmp + len, sizeof(tmp) - len, "/%u", ip_bits(src));
}
}
else
/* Go for an IPV6 address here, before faulting out: */
elog(ERROR, "unknown address family (%d)", ip_family(src));
PG_RETURN_CSTRING(pstrdup(tmp));
}
/* share code with INET case */
Datum
cidr_out(PG_FUNCTION_ARGS)
{
return inet_out(fcinfo);
}
/*
* Basic comparison function for sorting and inet/cidr comparisons.
*
* Comparison is first on the common bits of the network part, then on
* the length of the network part, and then on the whole unmasked address.
* The effect is that the network part is the major sort key, and for
* equal network parts we sort on the host part. Note this is only sane
* for CIDR if address bits to the right of the mask are guaranteed zero;
* otherwise logically-equal CIDRs might compare different.
*/
static int32
network_cmp_internal(inet *a1, inet *a2)
{
if (ip_family(a1) == AF_INET && ip_family(a2) == AF_INET)
{
int order;
order = v4bitncmp(ip_v4addr(a1), ip_v4addr(a2),
Min(ip_bits(a1), ip_bits(a2)));
if (order != 0)
return order;
order = ((int) ip_bits(a1)) - ((int) ip_bits(a2));
if (order != 0)
return order;
return v4bitncmp(ip_v4addr(a1), ip_v4addr(a2), 32);
}
else
{
/* Go for an IPV6 address here, before faulting out: */
elog(ERROR, "cannot compare address families %d and %d",
ip_family(a1), ip_family(a2));
return 0; /* keep compiler quiet */
}
}
Datum
network_cmp(PG_FUNCTION_ARGS)
{
inet *a1 = PG_GETARG_INET_P(0);
inet *a2 = PG_GETARG_INET_P(1);
PG_RETURN_INT32(network_cmp_internal(a1, a2));
}
/*
* Boolean ordering tests.
*/
Datum
network_lt(PG_FUNCTION_ARGS)
{
inet *a1 = PG_GETARG_INET_P(0);
inet *a2 = PG_GETARG_INET_P(1);
PG_RETURN_BOOL(network_cmp_internal(a1, a2) < 0);
}
Datum
network_le(PG_FUNCTION_ARGS)
{
inet *a1 = PG_GETARG_INET_P(0);
inet *a2 = PG_GETARG_INET_P(1);
PG_RETURN_BOOL(network_cmp_internal(a1, a2) <= 0);
}
Datum
network_eq(PG_FUNCTION_ARGS)
{
inet *a1 = PG_GETARG_INET_P(0);
inet *a2 = PG_GETARG_INET_P(1);
PG_RETURN_BOOL(network_cmp_internal(a1, a2) == 0);
}
Datum
network_ge(PG_FUNCTION_ARGS)
{
inet *a1 = PG_GETARG_INET_P(0);
inet *a2 = PG_GETARG_INET_P(1);
PG_RETURN_BOOL(network_cmp_internal(a1, a2) >= 0);
}
Datum
network_gt(PG_FUNCTION_ARGS)
{
inet *a1 = PG_GETARG_INET_P(0);
inet *a2 = PG_GETARG_INET_P(1);
PG_RETURN_BOOL(network_cmp_internal(a1, a2) > 0);
}
Datum
network_ne(PG_FUNCTION_ARGS)
{
inet *a1 = PG_GETARG_INET_P(0);
inet *a2 = PG_GETARG_INET_P(1);
PG_RETURN_BOOL(network_cmp_internal(a1, a2) != 0);
}
/*
* Boolean network-inclusion tests.
*/
Datum
network_sub(PG_FUNCTION_ARGS)
{
inet *a1 = PG_GETARG_INET_P(0);
inet *a2 = PG_GETARG_INET_P(1);
if ((ip_family(a1) == AF_INET) && (ip_family(a2) == AF_INET))
{
PG_RETURN_BOOL(ip_bits(a1) > ip_bits(a2)
&& v4bitncmp(ip_v4addr(a1), ip_v4addr(a2), ip_bits(a2)) == 0);
}
else
{
/* Go for an IPV6 address here, before faulting out: */
elog(ERROR, "cannot compare address families %d and %d",
ip_family(a1), ip_family(a2));
PG_RETURN_BOOL(false);
}
}
Datum
network_subeq(PG_FUNCTION_ARGS)
{
inet *a1 = PG_GETARG_INET_P(0);
inet *a2 = PG_GETARG_INET_P(1);
if ((ip_family(a1) == AF_INET) && (ip_family(a2) == AF_INET))
{
PG_RETURN_BOOL(ip_bits(a1) >= ip_bits(a2)
&& v4bitncmp(ip_v4addr(a1), ip_v4addr(a2), ip_bits(a2)) == 0);
}
else
{
/* Go for an IPV6 address here, before faulting out: */
elog(ERROR, "cannot compare address families %d and %d",
ip_family(a1), ip_family(a2));
PG_RETURN_BOOL(false);
}
}
Datum
network_sup(PG_FUNCTION_ARGS)
{
inet *a1 = PG_GETARG_INET_P(0);
inet *a2 = PG_GETARG_INET_P(1);
if ((ip_family(a1) == AF_INET) && (ip_family(a2) == AF_INET))
{
PG_RETURN_BOOL(ip_bits(a1) < ip_bits(a2)
&& v4bitncmp(ip_v4addr(a1), ip_v4addr(a2), ip_bits(a1)) == 0);
}
else
{
/* Go for an IPV6 address here, before faulting out: */
elog(ERROR, "cannot compare address families %d and %d",
ip_family(a1), ip_family(a2));
PG_RETURN_BOOL(false);
}
}
Datum
network_supeq(PG_FUNCTION_ARGS)
{
inet *a1 = PG_GETARG_INET_P(0);
inet *a2 = PG_GETARG_INET_P(1);
if ((ip_family(a1) == AF_INET) && (ip_family(a2) == AF_INET))
{
PG_RETURN_BOOL(ip_bits(a1) <= ip_bits(a2)
&& v4bitncmp(ip_v4addr(a1), ip_v4addr(a2), ip_bits(a1)) == 0);
}
else
{
/* Go for an IPV6 address here, before faulting out: */
elog(ERROR, "cannot compare address families %d and %d",
ip_family(a1), ip_family(a2));
PG_RETURN_BOOL(false);
}
}
/*
* Extract data from a network datatype.
*/
Datum
network_host(PG_FUNCTION_ARGS)
{
inet *ip = PG_GETARG_INET_P(0);
text *ret;
int len;
char *ptr,
tmp[sizeof("255.255.255.255/32")];
if (ip_family(ip) == AF_INET)
{
/* It's an IP V4 address: */
/* force display of 32 bits, regardless of masklen... */
if (inet_net_ntop(AF_INET, &ip_v4addr(ip), 32, tmp, sizeof(tmp)) == NULL)
elog(ERROR, "unable to print host (%s)", strerror(errno));
}
else
/* Go for an IPV6 address here, before faulting out: */
elog(ERROR, "unknown address family (%d)", ip_family(ip));
/* Suppress /n if present (shouldn't happen now) */
if ((ptr = strchr(tmp, '/')) != NULL)
*ptr = '\0';
/* Return string as a text datum */
len = strlen(tmp);
ret = (text *) palloc(len + VARHDRSZ);
VARATT_SIZEP(ret) = len + VARHDRSZ;
memcpy(VARDATA(ret), tmp, len);
PG_RETURN_TEXT_P(ret);
}
Datum
network_show(PG_FUNCTION_ARGS)
{
inet *ip = PG_GETARG_INET_P(0);
text *ret;
int len;
char tmp[sizeof("255.255.255.255/32")];
if (ip_family(ip) == AF_INET)
{
/* It's an IP V4 address: */
/* force display of 32 bits, regardless of masklen... */
if (inet_net_ntop(AF_INET, &ip_v4addr(ip), 32, tmp, sizeof(tmp)) == NULL)
elog(ERROR, "unable to print host (%s)", strerror(errno));
/* Add /n if not present (which it won't be) */
if (strchr(tmp, '/') == NULL)
{
len = strlen(tmp);
snprintf(tmp + len, sizeof(tmp) - len, "/%u", ip_bits(ip));
}
}
else
/* Go for an IPV6 address here, before faulting out: */
elog(ERROR, "unknown address family (%d)", ip_family(ip));
/* Return string as a text datum */
len = strlen(tmp);
ret = (text *) palloc(len + VARHDRSZ);
VARATT_SIZEP(ret) = len + VARHDRSZ;
memcpy(VARDATA(ret), tmp, len);
PG_RETURN_TEXT_P(ret);
}
Datum
network_abbrev(PG_FUNCTION_ARGS)
{
inet *ip = PG_GETARG_INET_P(0);
text *ret;
char *dst;
int len;
char tmp[sizeof("255.255.255.255/32")];
if (ip_family(ip) == AF_INET)
{
/* It's an IP V4 address: */
if (ip_type(ip))
dst = inet_cidr_ntop(AF_INET, &ip_v4addr(ip), ip_bits(ip),
tmp, sizeof(tmp));
else
dst = inet_net_ntop(AF_INET, &ip_v4addr(ip), ip_bits(ip),
tmp, sizeof(tmp));
if (dst == NULL)
elog(ERROR, "unable to print address (%s)", strerror(errno));
}
else
/* Go for an IPV6 address here, before faulting out: */
elog(ERROR, "unknown address family (%d)", ip_family(ip));
/* Return string as a text datum */
len = strlen(tmp);
ret = (text *) palloc(len + VARHDRSZ);
VARATT_SIZEP(ret) = len + VARHDRSZ;
memcpy(VARDATA(ret), tmp, len);
PG_RETURN_TEXT_P(ret);
}
Datum
network_masklen(PG_FUNCTION_ARGS)
{
inet *ip = PG_GETARG_INET_P(0);
PG_RETURN_INT32(ip_bits(ip));
}
Datum
network_broadcast(PG_FUNCTION_ARGS)
{
inet *ip = PG_GETARG_INET_P(0);
inet *dst;
dst = (inet *) palloc(VARHDRSZ + sizeof(inet_struct));
/* make sure any unused bits are zeroed */
MemSet(dst, 0, VARHDRSZ + sizeof(inet_struct));
if (ip_family(ip) == AF_INET)
{
/* It's an IP V4 address: */
unsigned long mask = 0xffffffff;
/*
* Shifting by 32 or more bits does not yield portable results, so
* don't try it.
*/
if (ip_bits(ip) < 32)
mask >>= ip_bits(ip);
else
mask = 0;
ip_v4addr(dst) = htonl(ntohl(ip_v4addr(ip)) | mask);
}
else
/* Go for an IPV6 address here, before faulting out: */
elog(ERROR, "unknown address family (%d)", ip_family(ip));
ip_family(dst) = ip_family(ip);
ip_bits(dst) = ip_bits(ip);
ip_type(dst) = 0;
VARATT_SIZEP(dst) = VARHDRSZ
+ ((char *) &ip_v4addr(dst) - (char *) VARDATA(dst))
+ ip_addrsize(dst);
PG_RETURN_INET_P(dst);
}
Datum
network_network(PG_FUNCTION_ARGS)
{
inet *ip = PG_GETARG_INET_P(0);
inet *dst;
dst = (inet *) palloc(VARHDRSZ + sizeof(inet_struct));
/* make sure any unused bits are zeroed */
MemSet(dst, 0, VARHDRSZ + sizeof(inet_struct));
if (ip_family(ip) == AF_INET)
{
/* It's an IP V4 address: */
unsigned long mask = 0xffffffff;
/*
* Shifting by 32 or more bits does not yield portable results, so
* don't try it.
*/
if (ip_bits(ip) > 0)
mask <<= (32 - ip_bits(ip));
else
mask = 0;
ip_v4addr(dst) = htonl(ntohl(ip_v4addr(ip)) & mask);
}
else
/* Go for an IPV6 address here, before faulting out: */
elog(ERROR, "unknown address family (%d)", ip_family(ip));
ip_family(dst) = ip_family(ip);
ip_bits(dst) = ip_bits(ip);
ip_type(dst) = 1;
VARATT_SIZEP(dst) = VARHDRSZ
+ ((char *) &ip_v4addr(dst) - (char *) VARDATA(dst))
+ ip_addrsize(dst);
PG_RETURN_INET_P(dst);
}
Datum
network_netmask(PG_FUNCTION_ARGS)
{
inet *ip = PG_GETARG_INET_P(0);
inet *dst;
dst = (inet *) palloc(VARHDRSZ + sizeof(inet_struct));
/* make sure any unused bits are zeroed */
MemSet(dst, 0, VARHDRSZ + sizeof(inet_struct));
if (ip_family(ip) == AF_INET)
{
/* It's an IP V4 address: */
unsigned long mask = 0xffffffff;
/*
* Shifting by 32 or more bits does not yield portable results, so
* don't try it.
*/
if (ip_bits(ip) > 0)
mask <<= (32 - ip_bits(ip));
else
mask = 0;
ip_v4addr(dst) = htonl(mask);
ip_bits(dst) = 32;
}
else
/* Go for an IPV6 address here, before faulting out: */
elog(ERROR, "unknown address family (%d)", ip_family(ip));
ip_family(dst) = ip_family(ip);
ip_type(dst) = 0;
VARATT_SIZEP(dst) = VARHDRSZ
+ ((char *) &ip_v4addr(dst) - (char *) VARDATA(dst))
+ ip_addrsize(dst);
PG_RETURN_INET_P(dst);
}
/*
* Bitwise comparison for V4 addresses. Add V6 implementation!
*/
static int
v4bitncmp(unsigned long a1, unsigned long a2, int bits)
{
unsigned long mask;
/*
* Shifting by 32 or more bits does not yield portable results, so
* don't try it.
*/
if (bits > 0)
mask = (0xFFFFFFFFL << (32 - bits)) & 0xFFFFFFFFL;
else
mask = 0;
a1 = ntohl(a1);
a2 = ntohl(a2);
if ((a1 & mask) < (a2 & mask))
return (-1);
else if ((a1 & mask) > (a2 & mask))
return (1);
return (0);
}
/*
* Returns true if given address fits fully within the specified bit width.
*/
static bool
v4addressOK(unsigned long a1, int bits)
{
unsigned long mask;
/*
* Shifting by 32 or more bits does not yield portable results, so
* don't try it.
*/
if (bits > 0)
mask = (0xFFFFFFFFL << (32 - bits)) & 0xFFFFFFFFL;
else
mask = 0;
a1 = ntohl(a1);
if ((a1 & mask) == a1)
return true;
return false;
}
|