Branch data Line data Source code
1 : : /*
2 : : * PostgreSQL type definitions for the INET and CIDR types.
3 : : *
4 : : * src/backend/utils/adt/network.c
5 : : *
6 : : * Jon Postel RIP 16 Oct 1998
7 : : */
8 : :
9 : : #include "postgres.h"
10 : :
11 : : #include <sys/socket.h>
12 : : #include <netinet/in.h>
13 : : #include <arpa/inet.h>
14 : :
15 : : #include "catalog/pg_type.h"
16 : : #include "common/hashfn.h"
17 : : #include "common/ip.h"
18 : : #include "lib/hyperloglog.h"
19 : : #include "libpq/libpq-be.h"
20 : : #include "libpq/pqformat.h"
21 : : #include "miscadmin.h"
22 : : #include "nodes/makefuncs.h"
23 : : #include "nodes/nodeFuncs.h"
24 : : #include "nodes/supportnodes.h"
25 : : #include "utils/builtins.h"
26 : : #include "utils/fmgroids.h"
27 : : #include "utils/guc.h"
28 : : #include "utils/inet.h"
29 : : #include "utils/lsyscache.h"
30 : : #include "utils/sortsupport.h"
31 : :
32 : :
33 : : /*
34 : : * An IPv4 netmask size is a value in the range of 0 - 32, which is
35 : : * represented with 6 bits in inet/cidr abbreviated keys where possible.
36 : : *
37 : : * An IPv4 inet/cidr abbreviated key can use up to 25 bits for subnet
38 : : * component.
39 : : */
40 : : #define ABBREV_BITS_INET4_NETMASK_SIZE 6
41 : : #define ABBREV_BITS_INET4_SUBNET 25
42 : :
43 : : /* sortsupport for inet/cidr */
44 : : typedef struct
45 : : {
46 : : int64 input_count; /* number of non-null values seen */
47 : : bool estimating; /* true if estimating cardinality */
48 : :
49 : : hyperLogLogState abbr_card; /* cardinality estimator */
50 : : } network_sortsupport_state;
51 : :
52 : : static int32 network_cmp_internal(inet *a1, inet *a2);
53 : : static int network_fast_cmp(Datum x, Datum y, SortSupport ssup);
54 : : static bool network_abbrev_abort(int memtupcount, SortSupport ssup);
55 : : static Datum network_abbrev_convert(Datum original, SortSupport ssup);
56 : : static List *match_network_function(Node *leftop,
57 : : Node *rightop,
58 : : int indexarg,
59 : : Oid funcid,
60 : : Oid opfamily);
61 : : static List *match_network_subset(Node *leftop,
62 : : Node *rightop,
63 : : bool is_eq,
64 : : Oid opfamily);
65 : : static bool addressOK(unsigned char *a, int bits, int family);
66 : : static inet *internal_inetpl(inet *ip, int64 addend);
67 : :
68 : :
69 : : /*
70 : : * Common INET/CIDR input routine
71 : : */
72 : : static inet *
73 : 3473 : network_in(char *src, bool is_cidr, Node *escontext)
74 : : {
75 : : int bits;
76 : : inet *dst;
77 : :
78 : 3473 : dst = (inet *) palloc0_object(inet);
79 : :
80 : : /*
81 : : * First, check to see if this is an IPv6 or IPv4 address. IPv6 addresses
82 : : * will have a : somewhere in them (several, in fact) so if there is one
83 : : * present, assume it's V6, otherwise assume it's V4.
84 : : */
85 : :
86 [ + + ]: 3473 : if (strchr(src, ':') != NULL)
87 : 726 : ip_family(dst) = PGSQL_AF_INET6;
88 : : else
89 : 2747 : ip_family(dst) = PGSQL_AF_INET;
90 : :
91 [ + + ]: 4658 : bits = pg_inet_net_pton(ip_family(dst), src, ip_addr(dst),
92 [ + + ]: 1185 : is_cidr ? ip_addrsize(dst) : -1);
93 [ + + + + : 3473 : if ((bits < 0) || (bits > ip_maxbits(dst)))
- + ]
94 [ + + + + ]: 20 : ereturn(escontext, NULL,
95 : : (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
96 : : /* translator: first %s is inet or cidr */
97 : : errmsg("invalid input syntax for type %s: \"%s\"",
98 : : is_cidr ? "cidr" : "inet", src)));
99 : :
100 : : /*
101 : : * Error check: CIDR values must not have any bits set beyond the masklen.
102 : : */
103 [ + + ]: 3453 : if (is_cidr)
104 : : {
105 [ + + ]: 1173 : if (!addressOK(ip_addr(dst), bits, ip_family(dst)))
106 [ + + ]: 20 : ereturn(escontext, NULL,
107 : : (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
108 : : errmsg("invalid cidr value: \"%s\"", src),
109 : : errdetail("Value has bits set to right of mask.")));
110 : : }
111 : :
112 : 3433 : ip_bits(dst) = bits;
113 [ + + ]: 3433 : SET_INET_VARSIZE(dst);
114 : :
115 : 3433 : return dst;
116 : : }
117 : :
118 : : Datum
119 : 2288 : inet_in(PG_FUNCTION_ARGS)
120 : : {
121 : 2288 : char *src = PG_GETARG_CSTRING(0);
122 : :
123 : 2288 : PG_RETURN_INET_P(network_in(src, false, fcinfo->context));
124 : : }
125 : :
126 : : Datum
127 : 1185 : cidr_in(PG_FUNCTION_ARGS)
128 : : {
129 : 1185 : char *src = PG_GETARG_CSTRING(0);
130 : :
131 : 1185 : PG_RETURN_INET_P(network_in(src, true, fcinfo->context));
132 : : }
133 : :
134 : :
135 : : /*
136 : : * Common INET/CIDR output routine
137 : : */
138 : : static char *
139 : 10066 : network_out(inet *src, bool is_cidr)
140 : : {
141 : : char tmp[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255/128")];
142 : : char *dst;
143 : : int len;
144 : :
145 : 10066 : dst = pg_inet_net_ntop(ip_family(src), ip_addr(src), ip_bits(src),
146 : : tmp, sizeof(tmp));
147 [ - + ]: 10066 : if (dst == NULL)
148 [ # # ]: 0 : ereport(ERROR,
149 : : (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
150 : : errmsg("could not format inet value: %m")));
151 : :
152 : : /* For CIDR, add /n if not present */
153 [ + + + + ]: 10066 : if (is_cidr && strchr(tmp, '/') == NULL)
154 : : {
155 : 859 : len = strlen(tmp);
156 : 859 : snprintf(tmp + len, sizeof(tmp) - len, "/%u", ip_bits(src));
157 : : }
158 : :
159 : 10066 : return pstrdup(tmp);
160 : : }
161 : :
162 : : Datum
163 : 6251 : inet_out(PG_FUNCTION_ARGS)
164 : : {
165 : 6251 : inet *src = PG_GETARG_INET_PP(0);
166 : :
167 : 6251 : PG_RETURN_CSTRING(network_out(src, false));
168 : : }
169 : :
170 : : Datum
171 : 3815 : cidr_out(PG_FUNCTION_ARGS)
172 : : {
173 : 3815 : inet *src = PG_GETARG_INET_PP(0);
174 : :
175 : 3815 : PG_RETURN_CSTRING(network_out(src, true));
176 : : }
177 : :
178 : :
179 : : /*
180 : : * network_recv - converts external binary format to inet
181 : : *
182 : : * The external representation is (one byte apiece for)
183 : : * family, bits, is_cidr, address length, address in network byte order.
184 : : *
185 : : * Presence of is_cidr is largely for historical reasons, though it might
186 : : * allow some code-sharing on the client side. We send it correctly on
187 : : * output, but ignore the value on input.
188 : : */
189 : : static inet *
190 : 0 : network_recv(StringInfo buf, bool is_cidr)
191 : : {
192 : : inet *addr;
193 : : char *addrptr;
194 : : int bits;
195 : : int nb,
196 : : i;
197 : :
198 : : /* make sure any unused bits in a CIDR value are zeroed */
199 : 0 : addr = palloc0_object(inet);
200 : :
201 : 0 : ip_family(addr) = pq_getmsgbyte(buf);
202 [ # # ]: 0 : if (ip_family(addr) != PGSQL_AF_INET &&
203 [ # # ]: 0 : ip_family(addr) != PGSQL_AF_INET6)
204 [ # # # # ]: 0 : ereport(ERROR,
205 : : (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
206 : : /* translator: %s is inet or cidr */
207 : : errmsg("invalid address family in external \"%s\" value",
208 : : is_cidr ? "cidr" : "inet")));
209 : 0 : bits = pq_getmsgbyte(buf);
210 [ # # # # : 0 : if (bits < 0 || bits > ip_maxbits(addr))
# # ]
211 [ # # # # ]: 0 : ereport(ERROR,
212 : : (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
213 : : /* translator: %s is inet or cidr */
214 : : errmsg("invalid bits in external \"%s\" value",
215 : : is_cidr ? "cidr" : "inet")));
216 : 0 : ip_bits(addr) = bits;
217 : 0 : i = pq_getmsgbyte(buf); /* ignore is_cidr */
218 : 0 : nb = pq_getmsgbyte(buf);
219 [ # # # # ]: 0 : if (nb != ip_addrsize(addr))
220 [ # # # # ]: 0 : ereport(ERROR,
221 : : (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
222 : : /* translator: %s is inet or cidr */
223 : : errmsg("invalid length in external \"%s\" value",
224 : : is_cidr ? "cidr" : "inet")));
225 : :
226 : 0 : addrptr = (char *) ip_addr(addr);
227 [ # # ]: 0 : for (i = 0; i < nb; i++)
228 : 0 : addrptr[i] = pq_getmsgbyte(buf);
229 : :
230 : : /*
231 : : * Error check: CIDR values must not have any bits set beyond the masklen.
232 : : */
233 [ # # ]: 0 : if (is_cidr)
234 : : {
235 [ # # ]: 0 : if (!addressOK(ip_addr(addr), bits, ip_family(addr)))
236 [ # # ]: 0 : ereport(ERROR,
237 : : (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
238 : : errmsg("invalid external \"cidr\" value"),
239 : : errdetail("Value has bits set to right of mask.")));
240 : : }
241 : :
242 [ # # ]: 0 : SET_INET_VARSIZE(addr);
243 : :
244 : 0 : return addr;
245 : : }
246 : :
247 : : Datum
248 : 0 : inet_recv(PG_FUNCTION_ARGS)
249 : : {
250 : 0 : StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
251 : :
252 : 0 : PG_RETURN_INET_P(network_recv(buf, false));
253 : : }
254 : :
255 : : Datum
256 : 0 : cidr_recv(PG_FUNCTION_ARGS)
257 : : {
258 : 0 : StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
259 : :
260 : 0 : PG_RETURN_INET_P(network_recv(buf, true));
261 : : }
262 : :
263 : :
264 : : /*
265 : : * network_send - converts inet to binary format
266 : : */
267 : : static bytea *
268 : 0 : network_send(inet *addr, bool is_cidr)
269 : : {
270 : : StringInfoData buf;
271 : : char *addrptr;
272 : : int nb,
273 : : i;
274 : :
275 : 0 : pq_begintypsend(&buf);
276 : 0 : pq_sendbyte(&buf, ip_family(addr));
277 : 0 : pq_sendbyte(&buf, ip_bits(addr));
278 : 0 : pq_sendbyte(&buf, is_cidr);
279 [ # # ]: 0 : nb = ip_addrsize(addr);
280 : 0 : pq_sendbyte(&buf, nb);
281 : 0 : addrptr = (char *) ip_addr(addr);
282 [ # # ]: 0 : for (i = 0; i < nb; i++)
283 : 0 : pq_sendbyte(&buf, addrptr[i]);
284 : 0 : return pq_endtypsend(&buf);
285 : : }
286 : :
287 : : Datum
288 : 0 : inet_send(PG_FUNCTION_ARGS)
289 : : {
290 : 0 : inet *addr = PG_GETARG_INET_PP(0);
291 : :
292 : 0 : PG_RETURN_BYTEA_P(network_send(addr, false));
293 : : }
294 : :
295 : : Datum
296 : 0 : cidr_send(PG_FUNCTION_ARGS)
297 : : {
298 : 0 : inet *addr = PG_GETARG_INET_PP(0);
299 : :
300 : 0 : PG_RETURN_BYTEA_P(network_send(addr, true));
301 : : }
302 : :
303 : :
304 : : Datum
305 : 2160 : inet_to_cidr(PG_FUNCTION_ARGS)
306 : : {
307 : 2160 : inet *src = PG_GETARG_INET_PP(0);
308 : : int bits;
309 : :
310 : 2160 : bits = ip_bits(src);
311 : :
312 : : /* safety check */
313 [ + - + + : 2160 : if ((bits < 0) || (bits > ip_maxbits(src)))
- + ]
314 [ # # ]: 0 : elog(ERROR, "invalid inet bit length: %d", bits);
315 : :
316 : 2160 : PG_RETURN_INET_P(cidr_set_masklen_internal(src, bits));
317 : : }
318 : :
319 : : Datum
320 : 180 : inet_set_masklen(PG_FUNCTION_ARGS)
321 : : {
322 : 180 : inet *src = PG_GETARG_INET_PP(0);
323 : 180 : int bits = PG_GETARG_INT32(1);
324 : : inet *dst;
325 : :
326 [ + + ]: 180 : if (bits == -1)
327 [ + + ]: 108 : bits = ip_maxbits(src);
328 : :
329 [ + - + + : 180 : if ((bits < 0) || (bits > ip_maxbits(src)))
+ + ]
330 [ + - ]: 4 : ereport(ERROR,
331 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
332 : : errmsg("invalid mask length: %d", bits)));
333 : :
334 : : /* clone the original data */
335 : 176 : dst = (inet *) palloc(VARSIZE_ANY(src));
336 : 176 : memcpy(dst, src, VARSIZE_ANY(src));
337 : :
338 : 176 : ip_bits(dst) = bits;
339 : :
340 : 176 : PG_RETURN_INET_P(dst);
341 : : }
342 : :
343 : : Datum
344 : 140 : cidr_set_masklen(PG_FUNCTION_ARGS)
345 : : {
346 : 140 : inet *src = PG_GETARG_INET_PP(0);
347 : 140 : int bits = PG_GETARG_INT32(1);
348 : :
349 [ + + ]: 140 : if (bits == -1)
350 [ + + ]: 68 : bits = ip_maxbits(src);
351 : :
352 [ + - + + : 140 : if ((bits < 0) || (bits > ip_maxbits(src)))
+ + ]
353 [ + - ]: 4 : ereport(ERROR,
354 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
355 : : errmsg("invalid mask length: %d", bits)));
356 : :
357 : 136 : PG_RETURN_INET_P(cidr_set_masklen_internal(src, bits));
358 : : }
359 : :
360 : : /*
361 : : * Copy src and set mask length to 'bits' (which must be valid for the family)
362 : : */
363 : : inet *
364 : 2448 : cidr_set_masklen_internal(const inet *src, int bits)
365 : : {
366 : 2448 : inet *dst = palloc0_object(inet);
367 : :
368 : 2448 : ip_family(dst) = ip_family(src);
369 : 2448 : ip_bits(dst) = bits;
370 : :
371 [ + - ]: 2448 : if (bits > 0)
372 : : {
373 : : Assert(bits <= ip_maxbits(dst));
374 : :
375 : : /* Clone appropriate bytes of the address, leaving the rest 0 */
376 : 2448 : memcpy(ip_addr(dst), ip_addr(src), (bits + 7) / 8);
377 : :
378 : : /* Clear any unwanted bits in the last partial byte */
379 [ + + ]: 2448 : if (bits % 8)
380 : 40 : ip_addr(dst)[bits / 8] &= ~(0xFF >> (bits % 8));
381 : : }
382 : :
383 : : /* Set varlena header correctly */
384 [ + + ]: 2448 : SET_INET_VARSIZE(dst);
385 : :
386 : 2448 : return dst;
387 : : }
388 : :
389 : : /*
390 : : * Basic comparison function for sorting and inet/cidr comparisons.
391 : : *
392 : : * Comparison is first on the common bits of the network part, then on
393 : : * the length of the network part, and then on the whole unmasked address.
394 : : * The effect is that the network part is the major sort key, and for
395 : : * equal network parts we sort on the host part. Note this is only sane
396 : : * for CIDR if address bits to the right of the mask are guaranteed zero;
397 : : * otherwise logically-equal CIDRs might compare different.
398 : : */
399 : :
400 : : static int32
401 : 105273 : network_cmp_internal(inet *a1, inet *a2)
402 : : {
403 [ + + ]: 105273 : if (ip_family(a1) == ip_family(a2))
404 : : {
405 : : int order;
406 : :
407 : 90845 : order = bitncmp(ip_addr(a1), ip_addr(a2),
408 [ + + ]: 90845 : Min(ip_bits(a1), ip_bits(a2)));
409 [ + + ]: 90845 : if (order != 0)
410 : 79637 : return order;
411 : 11208 : order = ((int) ip_bits(a1)) - ((int) ip_bits(a2));
412 [ + + ]: 11208 : if (order != 0)
413 : 668 : return order;
414 [ + + ]: 10540 : return bitncmp(ip_addr(a1), ip_addr(a2), ip_maxbits(a1));
415 : : }
416 : :
417 : 14428 : return ip_family(a1) - ip_family(a2);
418 : : }
419 : :
420 : : Datum
421 : 150 : network_cmp(PG_FUNCTION_ARGS)
422 : : {
423 : 150 : inet *a1 = PG_GETARG_INET_PP(0);
424 : 150 : inet *a2 = PG_GETARG_INET_PP(1);
425 : :
426 : 150 : PG_RETURN_INT32(network_cmp_internal(a1, a2));
427 : : }
428 : :
429 : : /*
430 : : * SortSupport strategy routine
431 : : */
432 : : Datum
433 : 221 : network_sortsupport(PG_FUNCTION_ARGS)
434 : : {
435 : 221 : SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
436 : :
437 : 221 : ssup->comparator = network_fast_cmp;
438 : 221 : ssup->ssup_extra = NULL;
439 : :
440 [ + + ]: 221 : if (ssup->abbreviate)
441 : : {
442 : : network_sortsupport_state *uss;
443 : : MemoryContext oldcontext;
444 : :
445 : 104 : oldcontext = MemoryContextSwitchTo(ssup->ssup_cxt);
446 : :
447 : 104 : uss = palloc_object(network_sortsupport_state);
448 : 104 : uss->input_count = 0;
449 : 104 : uss->estimating = true;
450 : 104 : initHyperLogLog(&uss->abbr_card, 10);
451 : :
452 : 104 : ssup->ssup_extra = uss;
453 : :
454 : 104 : ssup->comparator = ssup_datum_uint64_cmp;
455 : 104 : ssup->abbrev_converter = network_abbrev_convert;
456 : 104 : ssup->abbrev_abort = network_abbrev_abort;
457 : 104 : ssup->abbrev_full_comparator = network_fast_cmp;
458 : :
459 : 104 : MemoryContextSwitchTo(oldcontext);
460 : : }
461 : :
462 : 221 : PG_RETURN_VOID();
463 : : }
464 : :
465 : : /*
466 : : * SortSupport comparison func
467 : : */
468 : : static int
469 : 21217 : network_fast_cmp(Datum x, Datum y, SortSupport ssup)
470 : : {
471 : 21217 : inet *arg1 = DatumGetInetPP(x);
472 : 21217 : inet *arg2 = DatumGetInetPP(y);
473 : :
474 : 21217 : return network_cmp_internal(arg1, arg2);
475 : : }
476 : :
477 : : /*
478 : : * Callback for estimating effectiveness of abbreviated key optimization.
479 : : *
480 : : * We pay no attention to the cardinality of the non-abbreviated data, because
481 : : * there is no equality fast-path within authoritative inet comparator.
482 : : */
483 : : static bool
484 : 28 : network_abbrev_abort(int memtupcount, SortSupport ssup)
485 : : {
486 : 28 : network_sortsupport_state *uss = ssup->ssup_extra;
487 : : double abbr_card;
488 : :
489 [ - + - - : 28 : if (memtupcount < 10000 || uss->input_count < 10000 || !uss->estimating)
- - ]
490 : 28 : return false;
491 : :
492 : 0 : abbr_card = estimateHyperLogLog(&uss->abbr_card);
493 : :
494 : : /*
495 : : * If we have >100k distinct values, then even if we were sorting many
496 : : * billion rows we'd likely still break even, and the penalty of undoing
497 : : * that many rows of abbrevs would probably not be worth it. At this point
498 : : * we stop counting because we know that we're now fully committed.
499 : : */
500 [ # # ]: 0 : if (abbr_card > 100000.0)
501 : : {
502 [ # # ]: 0 : if (trace_sort)
503 [ # # ]: 0 : elog(LOG,
504 : : "network_abbrev: estimation ends at cardinality %f"
505 : : " after " INT64_FORMAT " values (%d rows)",
506 : : abbr_card, uss->input_count, memtupcount);
507 : 0 : uss->estimating = false;
508 : 0 : return false;
509 : : }
510 : :
511 : : /*
512 : : * Target minimum cardinality is 1 per ~2k of non-null inputs. 0.5 row
513 : : * fudge factor allows us to abort earlier on genuinely pathological data
514 : : * where we've had exactly one abbreviated value in the first 2k
515 : : * (non-null) rows.
516 : : */
517 [ # # ]: 0 : if (abbr_card < uss->input_count / 2000.0 + 0.5)
518 : : {
519 [ # # ]: 0 : if (trace_sort)
520 [ # # ]: 0 : elog(LOG,
521 : : "network_abbrev: aborting abbreviation at cardinality %f"
522 : : " below threshold %f after " INT64_FORMAT " values (%d rows)",
523 : : abbr_card, uss->input_count / 2000.0 + 0.5, uss->input_count,
524 : : memtupcount);
525 : 0 : return true;
526 : : }
527 : :
528 [ # # ]: 0 : if (trace_sort)
529 [ # # ]: 0 : elog(LOG,
530 : : "network_abbrev: cardinality %f after " INT64_FORMAT
531 : : " values (%d rows)", abbr_card, uss->input_count, memtupcount);
532 : :
533 : 0 : return false;
534 : : }
535 : :
536 : : /*
537 : : * SortSupport conversion routine. Converts original inet/cidr representation
538 : : * to abbreviated key representation that works with simple 3-way unsigned int
539 : : * comparisons. The network_cmp_internal() rules for sorting inet/cidr datums
540 : : * are followed by abbreviated comparisons by an encoding scheme that
541 : : * conditions keys through careful use of padding.
542 : : *
543 : : * Some background: inet values have three major components (take for example
544 : : * the address 1.2.3.4/24):
545 : : *
546 : : * * A network, or netmasked bits (1.2.3.0).
547 : : * * A netmask size (/24).
548 : : * * A subnet, or bits outside of the netmask (0.0.0.4).
549 : : *
550 : : * cidr values are the same except that with only the first two components --
551 : : * all their subnet bits *must* be zero (1.2.3.0/24).
552 : : *
553 : : * IPv4 and IPv6 are identical in this makeup, with the difference being that
554 : : * IPv4 addresses have a maximum of 32 bits compared to IPv6's 128 bits, so in
555 : : * IPv6 each part may be larger.
556 : : *
557 : : * inet/cidr types compare using these sorting rules. If inequality is detected
558 : : * at any step, comparison is finished. If any rule is a tie, the algorithm
559 : : * drops through to the next to break it:
560 : : *
561 : : * 1. IPv4 always appears before IPv6.
562 : : * 2. Network bits are compared.
563 : : * 3. Netmask size is compared.
564 : : * 4. All bits are compared (having made it here, we know that both
565 : : * netmasked bits and netmask size are equal, so we're in effect only
566 : : * comparing subnet bits).
567 : : *
568 : : * When generating abbreviated keys for SortSupport, we pack as much as we can
569 : : * into a datum while ensuring that when comparing those keys as integers,
570 : : * these rules will be respected. Exact contents depend on IP family:
571 : : *
572 : : * IPv4
573 : : * ----
574 : : *
575 : : * We have space to store all netmasked bits, followed by the netmask size,
576 : : * followed by 25 bits of the subnet (25 bits is usually more than enough in
577 : : * practice). cidr datums always have all-zero subnet bits.
578 : : *
579 : : * +----------+-----------------------+--------------+--------------------+
580 : : * | 1 bit IP | 32 bits network | 6 bits | 25 bits subnet |
581 : : * | family | (full) | network size | (truncated) |
582 : : * +----------+-----------------------+--------------+--------------------+
583 : : *
584 : : * IPv6
585 : : * ----
586 : : *
587 : : * +----------+---------------------------------+
588 : : * | 1 bit IP | 63 bits network | (up to 65 bits
589 : : * | family | (truncated) | network omitted)
590 : : * +----------+---------------------------------+
591 : : */
592 : : static Datum
593 : 1024 : network_abbrev_convert(Datum original, SortSupport ssup)
594 : : {
595 : 1024 : network_sortsupport_state *uss = ssup->ssup_extra;
596 : 1024 : inet *authoritative = DatumGetInetPP(original);
597 : : Datum res,
598 : : ipaddr_datum,
599 : : subnet_bitmask,
600 : : network;
601 : : int subnet_size;
602 : :
603 : : Assert(ip_family(authoritative) == PGSQL_AF_INET ||
604 : : ip_family(authoritative) == PGSQL_AF_INET6);
605 : :
606 : : /*
607 : : * Get an unsigned integer representation of the IP address by taking its
608 : : * first 4 or 8 bytes. Always take all 4 bytes of an IPv4 address. Take
609 : : * the first 8 bytes of an IPv6 address.
610 : : *
611 : : * We're consuming an array of unsigned char, so byteswap on little endian
612 : : * systems (an inet's ipaddr field stores the most significant byte
613 : : * first).
614 : : */
615 [ + + ]: 1024 : if (ip_family(authoritative) == PGSQL_AF_INET)
616 : : {
617 : : uint32 ipaddr_datum32;
618 : :
619 : 772 : memcpy(&ipaddr_datum32, ip_addr(authoritative), sizeof(uint32));
620 : :
621 : : /* Must byteswap on little-endian machines */
622 : : #ifndef WORDS_BIGENDIAN
623 : 772 : ipaddr_datum = pg_bswap32(ipaddr_datum32);
624 : : #else
625 : : ipaddr_datum = ipaddr_datum32;
626 : : #endif
627 : :
628 : : /* Initialize result without setting ipfamily bit */
629 : 772 : res = (Datum) 0;
630 : : }
631 : : else
632 : : {
633 : 252 : memcpy(&ipaddr_datum, ip_addr(authoritative), sizeof(Datum));
634 : :
635 : : /* Must byteswap on little-endian machines */
636 : 252 : ipaddr_datum = DatumBigEndianToNative(ipaddr_datum);
637 : :
638 : : /* Initialize result with ipfamily (most significant) bit set */
639 : 252 : res = ((Datum) 1) << (sizeof(Datum) * BITS_PER_BYTE - 1);
640 : : }
641 : :
642 : : /*
643 : : * ipaddr_datum must be "split": high order bits go in "network" component
644 : : * of abbreviated key (often with zeroed bits at the end due to masking),
645 : : * while low order bits go in "subnet" component when there is space for
646 : : * one. This is often accomplished by generating a temp datum subnet
647 : : * bitmask, which we may reuse later when generating the subnet bits
648 : : * themselves.
649 : : *
650 : : * The number of bits in subnet is used to generate a datum subnet
651 : : * bitmask. For example, with a /24 IPv4 datum there are 8 subnet bits
652 : : * (since 32 - 24 is 8), so the final subnet bitmask is B'1111 1111'. We
653 : : * need explicit handling for cases where the ipaddr bits cannot all fit
654 : : * in a datum, though (otherwise we'd incorrectly mask the network
655 : : * component with IPv6 values).
656 : : */
657 [ + + ]: 1024 : subnet_size = ip_maxbits(authoritative) - ip_bits(authoritative);
658 : : Assert(subnet_size >= 0);
659 : : /* subnet size must work with prefix ipaddr cases */
660 : 1024 : subnet_size %= sizeof(Datum) * BITS_PER_BYTE;
661 [ + + ]: 1024 : if (ip_bits(authoritative) == 0)
662 : : {
663 : : /* Fit as many ipaddr bits as possible into subnet */
664 : 112 : subnet_bitmask = ((Datum) 0) - 1;
665 : 112 : network = 0;
666 : : }
667 [ + + ]: 912 : else if (ip_bits(authoritative) < sizeof(Datum) * BITS_PER_BYTE)
668 : : {
669 : : /* Split ipaddr bits between network and subnet */
670 : 772 : subnet_bitmask = (((Datum) 1) << subnet_size) - 1;
671 : 772 : network = ipaddr_datum & ~subnet_bitmask;
672 : : }
673 : : else
674 : : {
675 : : /* Fit as many ipaddr bits as possible into network */
676 : 140 : subnet_bitmask = 0;
677 : 140 : network = ipaddr_datum;
678 : : }
679 : :
680 [ + + ]: 1024 : if (ip_family(authoritative) == PGSQL_AF_INET)
681 : : {
682 : : /*
683 : : * IPv4: keep all 32 netmasked bits, netmask size, and most
684 : : * significant 25 subnet bits
685 : : */
686 : 772 : Datum netmask_size = (Datum) ip_bits(authoritative);
687 : : Datum subnet;
688 : :
689 : : /*
690 : : * Shift left 31 bits: 6 bits netmask size + 25 subnet bits.
691 : : *
692 : : * We don't make any distinction between network bits that are zero
693 : : * due to masking and "true"/non-masked zero bits. An abbreviated
694 : : * comparison that is resolved by comparing a non-masked and non-zero
695 : : * bit to a masked/zeroed bit is effectively resolved based on
696 : : * ip_bits(), even though the comparison won't reach the netmask_size
697 : : * bits.
698 : : */
699 : 772 : network <<= (ABBREV_BITS_INET4_NETMASK_SIZE +
700 : : ABBREV_BITS_INET4_SUBNET);
701 : :
702 : : /* Shift size to make room for subnet bits at the end */
703 : 772 : netmask_size <<= ABBREV_BITS_INET4_SUBNET;
704 : :
705 : : /* Extract subnet bits without shifting them */
706 : 772 : subnet = ipaddr_datum & subnet_bitmask;
707 : :
708 : : /*
709 : : * If we have more than 25 subnet bits, we can't fit everything. Shift
710 : : * subnet down to avoid clobbering bits that are only supposed to be
711 : : * used for netmask_size.
712 : : *
713 : : * Discarding the least significant subnet bits like this is correct
714 : : * because abbreviated comparisons that are resolved at the subnet
715 : : * level must have had equal netmask_size/ip_bits() values in order to
716 : : * get that far.
717 : : */
718 [ + + ]: 772 : if (subnet_size > ABBREV_BITS_INET4_SUBNET)
719 : 96 : subnet >>= subnet_size - ABBREV_BITS_INET4_SUBNET;
720 : :
721 : : /*
722 : : * Assemble the final abbreviated key without clobbering the ipfamily
723 : : * bit that must remain a zero.
724 : : */
725 : 772 : res |= network | netmask_size | subnet;
726 : : }
727 : : else
728 : : {
729 : : /*
730 : : * IPv6: Use as many of the netmasked bits as will fit in final
731 : : * abbreviated key. Avoid clobbering the ipfamily bit that was set
732 : : * earlier.
733 : : */
734 : 252 : res |= network >> 1;
735 : : }
736 : :
737 : 1024 : uss->input_count += 1;
738 : :
739 : : /* Hash abbreviated key */
740 [ + - ]: 1024 : if (uss->estimating)
741 : 1024 : addHyperLogLog(&uss->abbr_card,
742 : 1024 : (uint32) murmurhash64(DatumGetUInt64(res)));
743 : :
744 : 1024 : return res;
745 : : }
746 : :
747 : : /*
748 : : * Boolean ordering tests.
749 : : */
750 : : Datum
751 : 29911 : network_lt(PG_FUNCTION_ARGS)
752 : : {
753 : 29911 : inet *a1 = PG_GETARG_INET_PP(0);
754 : 29911 : inet *a2 = PG_GETARG_INET_PP(1);
755 : :
756 : 29911 : PG_RETURN_BOOL(network_cmp_internal(a1, a2) < 0);
757 : : }
758 : :
759 : : Datum
760 : 12005 : network_le(PG_FUNCTION_ARGS)
761 : : {
762 : 12005 : inet *a1 = PG_GETARG_INET_PP(0);
763 : 12005 : inet *a2 = PG_GETARG_INET_PP(1);
764 : :
765 : 12005 : PG_RETURN_BOOL(network_cmp_internal(a1, a2) <= 0);
766 : : }
767 : :
768 : : Datum
769 : 15434 : network_eq(PG_FUNCTION_ARGS)
770 : : {
771 : 15434 : inet *a1 = PG_GETARG_INET_PP(0);
772 : 15434 : inet *a2 = PG_GETARG_INET_PP(1);
773 : :
774 : 15434 : PG_RETURN_BOOL(network_cmp_internal(a1, a2) == 0);
775 : : }
776 : :
777 : : Datum
778 : 12149 : network_ge(PG_FUNCTION_ARGS)
779 : : {
780 : 12149 : inet *a1 = PG_GETARG_INET_PP(0);
781 : 12149 : inet *a2 = PG_GETARG_INET_PP(1);
782 : :
783 : 12149 : PG_RETURN_BOOL(network_cmp_internal(a1, a2) >= 0);
784 : : }
785 : :
786 : : Datum
787 : 14083 : network_gt(PG_FUNCTION_ARGS)
788 : : {
789 : 14083 : inet *a1 = PG_GETARG_INET_PP(0);
790 : 14083 : inet *a2 = PG_GETARG_INET_PP(1);
791 : :
792 : 14083 : PG_RETURN_BOOL(network_cmp_internal(a1, a2) > 0);
793 : : }
794 : :
795 : : Datum
796 : 68 : network_ne(PG_FUNCTION_ARGS)
797 : : {
798 : 68 : inet *a1 = PG_GETARG_INET_PP(0);
799 : 68 : inet *a2 = PG_GETARG_INET_PP(1);
800 : :
801 : 68 : PG_RETURN_BOOL(network_cmp_internal(a1, a2) != 0);
802 : : }
803 : :
804 : : /*
805 : : * MIN/MAX support functions.
806 : : */
807 : : Datum
808 : 128 : network_smaller(PG_FUNCTION_ARGS)
809 : : {
810 : 128 : inet *a1 = PG_GETARG_INET_PP(0);
811 : 128 : inet *a2 = PG_GETARG_INET_PP(1);
812 : :
813 [ + + ]: 128 : if (network_cmp_internal(a1, a2) < 0)
814 : 76 : PG_RETURN_INET_P(a1);
815 : : else
816 : 52 : PG_RETURN_INET_P(a2);
817 : : }
818 : :
819 : : Datum
820 : 128 : network_larger(PG_FUNCTION_ARGS)
821 : : {
822 : 128 : inet *a1 = PG_GETARG_INET_PP(0);
823 : 128 : inet *a2 = PG_GETARG_INET_PP(1);
824 : :
825 [ + + ]: 128 : if (network_cmp_internal(a1, a2) > 0)
826 : 104 : PG_RETURN_INET_P(a1);
827 : : else
828 : 24 : PG_RETURN_INET_P(a2);
829 : : }
830 : :
831 : : /*
832 : : * Support function for hash indexes on inet/cidr.
833 : : */
834 : : Datum
835 : 4064 : hashinet(PG_FUNCTION_ARGS)
836 : : {
837 : 4064 : inet *addr = PG_GETARG_INET_PP(0);
838 [ + + ]: 4064 : int addrsize = ip_addrsize(addr);
839 : :
840 : : /* XXX this assumes there are no pad bytes in the data structure */
841 : 4064 : return hash_any((unsigned char *) VARDATA_ANY(addr), addrsize + 2);
842 : : }
843 : :
844 : : Datum
845 : 40 : hashinetextended(PG_FUNCTION_ARGS)
846 : : {
847 : 40 : inet *addr = PG_GETARG_INET_PP(0);
848 [ + - ]: 40 : int addrsize = ip_addrsize(addr);
849 : :
850 : 40 : return hash_any_extended((unsigned char *) VARDATA_ANY(addr), addrsize + 2,
851 : 40 : PG_GETARG_INT64(1));
852 : : }
853 : :
854 : : /*
855 : : * Boolean network-inclusion tests.
856 : : */
857 : : Datum
858 : 4088 : network_sub(PG_FUNCTION_ARGS)
859 : : {
860 : 4088 : inet *a1 = PG_GETARG_INET_PP(0);
861 : 4088 : inet *a2 = PG_GETARG_INET_PP(1);
862 : :
863 [ + + ]: 4088 : if (ip_family(a1) == ip_family(a2))
864 : : {
865 [ + + + + ]: 3288 : PG_RETURN_BOOL(ip_bits(a1) > ip_bits(a2) &&
866 : : bitncmp(ip_addr(a1), ip_addr(a2), ip_bits(a2)) == 0);
867 : : }
868 : :
869 : 800 : PG_RETURN_BOOL(false);
870 : : }
871 : :
872 : : Datum
873 : 6604 : network_subeq(PG_FUNCTION_ARGS)
874 : : {
875 : 6604 : inet *a1 = PG_GETARG_INET_PP(0);
876 : 6604 : inet *a2 = PG_GETARG_INET_PP(1);
877 : :
878 [ + + ]: 6604 : if (ip_family(a1) == ip_family(a2))
879 : : {
880 [ + + + + ]: 4092 : PG_RETURN_BOOL(ip_bits(a1) >= ip_bits(a2) &&
881 : : bitncmp(ip_addr(a1), ip_addr(a2), ip_bits(a2)) == 0);
882 : : }
883 : :
884 : 2512 : PG_RETURN_BOOL(false);
885 : : }
886 : :
887 : : Datum
888 : 4120 : network_sup(PG_FUNCTION_ARGS)
889 : : {
890 : 4120 : inet *a1 = PG_GETARG_INET_PP(0);
891 : 4120 : inet *a2 = PG_GETARG_INET_PP(1);
892 : :
893 [ + + ]: 4120 : if (ip_family(a1) == ip_family(a2))
894 : : {
895 [ + + + + ]: 3320 : PG_RETURN_BOOL(ip_bits(a1) < ip_bits(a2) &&
896 : : bitncmp(ip_addr(a1), ip_addr(a2), ip_bits(a1)) == 0);
897 : : }
898 : :
899 : 800 : PG_RETURN_BOOL(false);
900 : : }
901 : :
902 : : Datum
903 : 12392 : network_supeq(PG_FUNCTION_ARGS)
904 : : {
905 : 12392 : inet *a1 = PG_GETARG_INET_PP(0);
906 : 12392 : inet *a2 = PG_GETARG_INET_PP(1);
907 : :
908 [ + + ]: 12392 : if (ip_family(a1) == ip_family(a2))
909 : : {
910 [ + + + + ]: 6832 : PG_RETURN_BOOL(ip_bits(a1) <= ip_bits(a2) &&
911 : : bitncmp(ip_addr(a1), ip_addr(a2), ip_bits(a1)) == 0);
912 : : }
913 : :
914 : 5560 : PG_RETURN_BOOL(false);
915 : : }
916 : :
917 : : Datum
918 : 14020 : network_overlap(PG_FUNCTION_ARGS)
919 : : {
920 : 14020 : inet *a1 = PG_GETARG_INET_PP(0);
921 : 14020 : inet *a2 = PG_GETARG_INET_PP(1);
922 : :
923 [ + + ]: 14020 : if (ip_family(a1) == ip_family(a2))
924 : : {
925 [ + + ]: 8532 : PG_RETURN_BOOL(bitncmp(ip_addr(a1), ip_addr(a2),
926 : : Min(ip_bits(a1), ip_bits(a2))) == 0);
927 : : }
928 : :
929 : 5488 : PG_RETURN_BOOL(false);
930 : : }
931 : :
932 : : /*
933 : : * Planner support function for network subset/superset operators
934 : : */
935 : : Datum
936 : 1240 : network_subset_support(PG_FUNCTION_ARGS)
937 : : {
938 : 1240 : Node *rawreq = (Node *) PG_GETARG_POINTER(0);
939 : 1240 : Node *ret = NULL;
940 : :
941 [ + + ]: 1240 : if (IsA(rawreq, SupportRequestIndexCondition))
942 : : {
943 : : /* Try to convert operator/function call to index conditions */
944 : 40 : SupportRequestIndexCondition *req = (SupportRequestIndexCondition *) rawreq;
945 : :
946 [ + - ]: 40 : if (is_opclause(req->node))
947 : : {
948 : 40 : OpExpr *clause = (OpExpr *) req->node;
949 : :
950 : : Assert(list_length(clause->args) == 2);
951 : : ret = (Node *)
952 : 40 : match_network_function((Node *) linitial(clause->args),
953 : 40 : (Node *) lsecond(clause->args),
954 : : req->indexarg,
955 : : req->funcid,
956 : : req->opfamily);
957 : : }
958 [ # # ]: 0 : else if (is_funcclause(req->node)) /* be paranoid */
959 : : {
960 : 0 : FuncExpr *clause = (FuncExpr *) req->node;
961 : :
962 : : Assert(list_length(clause->args) == 2);
963 : : ret = (Node *)
964 : 0 : match_network_function((Node *) linitial(clause->args),
965 : 0 : (Node *) lsecond(clause->args),
966 : : req->indexarg,
967 : : req->funcid,
968 : : req->opfamily);
969 : : }
970 : : }
971 : :
972 : 1240 : PG_RETURN_POINTER(ret);
973 : : }
974 : :
975 : : /*
976 : : * match_network_function
977 : : * Try to generate an indexqual for a network subset/superset function.
978 : : *
979 : : * This layer is just concerned with identifying the function and swapping
980 : : * the arguments if necessary.
981 : : */
982 : : static List *
983 : 40 : match_network_function(Node *leftop,
984 : : Node *rightop,
985 : : int indexarg,
986 : : Oid funcid,
987 : : Oid opfamily)
988 : : {
989 [ + + + + : 40 : switch (funcid)
- ]
990 : : {
991 : 10 : case F_NETWORK_SUB:
992 : : /* indexkey must be on the left */
993 [ - + ]: 10 : if (indexarg != 0)
994 : 0 : return NIL;
995 : 10 : return match_network_subset(leftop, rightop, false, opfamily);
996 : :
997 : 10 : case F_NETWORK_SUBEQ:
998 : : /* indexkey must be on the left */
999 [ - + ]: 10 : if (indexarg != 0)
1000 : 0 : return NIL;
1001 : 10 : return match_network_subset(leftop, rightop, true, opfamily);
1002 : :
1003 : 10 : case F_NETWORK_SUP:
1004 : : /* indexkey must be on the right */
1005 [ - + ]: 10 : if (indexarg != 1)
1006 : 0 : return NIL;
1007 : 10 : return match_network_subset(rightop, leftop, false, opfamily);
1008 : :
1009 : 10 : case F_NETWORK_SUPEQ:
1010 : : /* indexkey must be on the right */
1011 [ - + ]: 10 : if (indexarg != 1)
1012 : 0 : return NIL;
1013 : 10 : return match_network_subset(rightop, leftop, true, opfamily);
1014 : :
1015 : 0 : default:
1016 : :
1017 : : /*
1018 : : * We'd only get here if somebody attached this support function
1019 : : * to an unexpected function. Maybe we should complain, but for
1020 : : * now, do nothing.
1021 : : */
1022 : 0 : return NIL;
1023 : : }
1024 : : }
1025 : :
1026 : : /*
1027 : : * match_network_subset
1028 : : * Try to generate an indexqual for a network subset function.
1029 : : */
1030 : : static List *
1031 : 40 : match_network_subset(Node *leftop,
1032 : : Node *rightop,
1033 : : bool is_eq,
1034 : : Oid opfamily)
1035 : : {
1036 : : List *result;
1037 : : Datum rightopval;
1038 : 40 : Oid datatype = INETOID;
1039 : : Oid opr1oid;
1040 : : Oid opr2oid;
1041 : : Datum opr1right;
1042 : : Datum opr2right;
1043 : : Expr *expr;
1044 : :
1045 : : /*
1046 : : * Can't do anything with a non-constant or NULL comparison value.
1047 : : *
1048 : : * Note that since we restrict ourselves to cases with a hard constant on
1049 : : * the RHS, it's a-fortiori a pseudoconstant, and we don't need to worry
1050 : : * about verifying that.
1051 : : */
1052 [ + - ]: 40 : if (!IsA(rightop, Const) ||
1053 [ - + ]: 40 : ((Const *) rightop)->constisnull)
1054 : 0 : return NIL;
1055 : 40 : rightopval = ((Const *) rightop)->constvalue;
1056 : :
1057 : : /*
1058 : : * create clause "key >= network_scan_first( rightopval )", or ">" if the
1059 : : * operator disallows equality.
1060 : : */
1061 [ + + ]: 40 : opr1oid = get_opfamily_member_for_cmptype(opfamily, datatype, datatype, is_eq ? COMPARE_GE : COMPARE_GT);
1062 [ - + ]: 40 : if (opr1oid == InvalidOid)
1063 : 0 : return NIL;
1064 : :
1065 : 40 : opr1right = network_scan_first(rightopval);
1066 : :
1067 : 40 : expr = make_opclause(opr1oid, BOOLOID, false,
1068 : : (Expr *) leftop,
1069 : 40 : (Expr *) makeConst(datatype, -1,
1070 : : InvalidOid, /* not collatable */
1071 : : -1, opr1right,
1072 : : false, false),
1073 : : InvalidOid, InvalidOid);
1074 : 40 : result = list_make1(expr);
1075 : :
1076 : : /* create clause "key <= network_scan_last( rightopval )" */
1077 : :
1078 : 40 : opr2oid = get_opfamily_member_for_cmptype(opfamily, datatype, datatype, COMPARE_LE);
1079 [ - + ]: 40 : if (opr2oid == InvalidOid)
1080 : 0 : return NIL;
1081 : :
1082 : 40 : opr2right = network_scan_last(rightopval);
1083 : :
1084 : 40 : expr = make_opclause(opr2oid, BOOLOID, false,
1085 : : (Expr *) leftop,
1086 : 40 : (Expr *) makeConst(datatype, -1,
1087 : : InvalidOid, /* not collatable */
1088 : : -1, opr2right,
1089 : : false, false),
1090 : : InvalidOid, InvalidOid);
1091 : 40 : result = lappend(result, expr);
1092 : :
1093 : 40 : return result;
1094 : : }
1095 : :
1096 : :
1097 : : /*
1098 : : * Extract data from a network datatype.
1099 : : */
1100 : : Datum
1101 : 68 : network_host(PG_FUNCTION_ARGS)
1102 : : {
1103 : 68 : inet *ip = PG_GETARG_INET_PP(0);
1104 : : char *ptr;
1105 : : char tmp[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255/128")];
1106 : :
1107 : : /* force display of max bits, regardless of masklen... */
1108 [ + + - + ]: 68 : if (pg_inet_net_ntop(ip_family(ip), ip_addr(ip), ip_maxbits(ip),
1109 : : tmp, sizeof(tmp)) == NULL)
1110 [ # # ]: 0 : ereport(ERROR,
1111 : : (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
1112 : : errmsg("could not format inet value: %m")));
1113 : :
1114 : : /* Suppress /n if present (shouldn't happen now) */
1115 [ - + ]: 68 : if ((ptr = strchr(tmp, '/')) != NULL)
1116 : 0 : *ptr = '\0';
1117 : :
1118 : 68 : PG_RETURN_TEXT_P(cstring_to_text(tmp));
1119 : : }
1120 : :
1121 : : /*
1122 : : * network_show implements the inet and cidr casts to text. This is not
1123 : : * quite the same behavior as network_out, hence we can't drop it in favor
1124 : : * of CoerceViaIO.
1125 : : */
1126 : : Datum
1127 : 352 : network_show(PG_FUNCTION_ARGS)
1128 : : {
1129 : 352 : inet *ip = PG_GETARG_INET_PP(0);
1130 : : int len;
1131 : : char tmp[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255/128")];
1132 : :
1133 [ + + - + ]: 352 : if (pg_inet_net_ntop(ip_family(ip), ip_addr(ip), ip_maxbits(ip),
1134 : : tmp, sizeof(tmp)) == NULL)
1135 [ # # ]: 0 : ereturn(fcinfo->context, (Datum) 0,
1136 : : (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
1137 : : errmsg("could not format inet value: %m")));
1138 : :
1139 : : /* Add /n if not present (which it won't be) */
1140 [ + - ]: 352 : if (strchr(tmp, '/') == NULL)
1141 : : {
1142 : 352 : len = strlen(tmp);
1143 : 352 : snprintf(tmp + len, sizeof(tmp) - len, "/%u", ip_bits(ip));
1144 : : }
1145 : :
1146 : 352 : PG_RETURN_TEXT_P(cstring_to_text(tmp));
1147 : : }
1148 : :
1149 : : Datum
1150 : 68 : inet_abbrev(PG_FUNCTION_ARGS)
1151 : : {
1152 : 68 : inet *ip = PG_GETARG_INET_PP(0);
1153 : : char *dst;
1154 : : char tmp[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255/128")];
1155 : :
1156 : 68 : dst = pg_inet_net_ntop(ip_family(ip), ip_addr(ip),
1157 : 68 : ip_bits(ip), tmp, sizeof(tmp));
1158 : :
1159 [ - + ]: 68 : if (dst == NULL)
1160 [ # # ]: 0 : ereport(ERROR,
1161 : : (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
1162 : : errmsg("could not format inet value: %m")));
1163 : :
1164 : 68 : PG_RETURN_TEXT_P(cstring_to_text(tmp));
1165 : : }
1166 : :
1167 : : Datum
1168 : 68 : cidr_abbrev(PG_FUNCTION_ARGS)
1169 : : {
1170 : 68 : inet *ip = PG_GETARG_INET_PP(0);
1171 : : char *dst;
1172 : : char tmp[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255/128")];
1173 : :
1174 : 68 : dst = pg_inet_cidr_ntop(ip_family(ip), ip_addr(ip),
1175 : 68 : ip_bits(ip), tmp, sizeof(tmp));
1176 : :
1177 [ - + ]: 68 : if (dst == NULL)
1178 [ # # ]: 0 : ereport(ERROR,
1179 : : (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
1180 : : errmsg("could not format cidr value: %m")));
1181 : :
1182 : 68 : PG_RETURN_TEXT_P(cstring_to_text(tmp));
1183 : : }
1184 : :
1185 : : Datum
1186 : 236 : network_masklen(PG_FUNCTION_ARGS)
1187 : : {
1188 : 236 : inet *ip = PG_GETARG_INET_PP(0);
1189 : :
1190 : 236 : PG_RETURN_INT32(ip_bits(ip));
1191 : : }
1192 : :
1193 : : Datum
1194 : 68 : network_family(PG_FUNCTION_ARGS)
1195 : : {
1196 : 68 : inet *ip = PG_GETARG_INET_PP(0);
1197 : :
1198 [ + + - ]: 68 : switch (ip_family(ip))
1199 : : {
1200 : 56 : case PGSQL_AF_INET:
1201 : 56 : PG_RETURN_INT32(4);
1202 : : break;
1203 : 12 : case PGSQL_AF_INET6:
1204 : 12 : PG_RETURN_INT32(6);
1205 : : break;
1206 : 0 : default:
1207 : 0 : PG_RETURN_INT32(0);
1208 : : break;
1209 : : }
1210 : : }
1211 : :
1212 : : Datum
1213 : 176 : network_broadcast(PG_FUNCTION_ARGS)
1214 : : {
1215 : 176 : inet *ip = PG_GETARG_INET_PP(0);
1216 : : inet *dst;
1217 : : int byte;
1218 : : int bits;
1219 : : int maxbytes;
1220 : : unsigned char mask;
1221 : : unsigned char *a,
1222 : : *b;
1223 : :
1224 : : /* make sure any unused bits are zeroed */
1225 : 176 : dst = palloc0_object(inet);
1226 : :
1227 [ + + ]: 176 : maxbytes = ip_addrsize(ip);
1228 : 176 : bits = ip_bits(ip);
1229 : 176 : a = ip_addr(ip);
1230 : 176 : b = ip_addr(dst);
1231 : :
1232 [ + + ]: 1168 : for (byte = 0; byte < maxbytes; byte++)
1233 : : {
1234 [ + + ]: 992 : if (bits >= 8)
1235 : : {
1236 : 684 : mask = 0x00;
1237 : 684 : bits -= 8;
1238 : : }
1239 [ + + ]: 308 : else if (bits == 0)
1240 : 292 : mask = 0xff;
1241 : : else
1242 : : {
1243 : 16 : mask = 0xff >> bits;
1244 : 16 : bits = 0;
1245 : : }
1246 : :
1247 : 992 : b[byte] = a[byte] | mask;
1248 : : }
1249 : :
1250 : 176 : ip_family(dst) = ip_family(ip);
1251 : 176 : ip_bits(dst) = ip_bits(ip);
1252 [ + + ]: 176 : SET_INET_VARSIZE(dst);
1253 : :
1254 : 176 : PG_RETURN_INET_P(dst);
1255 : : }
1256 : :
1257 : : Datum
1258 : 176 : network_network(PG_FUNCTION_ARGS)
1259 : : {
1260 : 176 : inet *ip = PG_GETARG_INET_PP(0);
1261 : : inet *dst;
1262 : : int byte;
1263 : : int bits;
1264 : : unsigned char mask;
1265 : : unsigned char *a,
1266 : : *b;
1267 : :
1268 : : /* make sure any unused bits are zeroed */
1269 : 176 : dst = palloc0_object(inet);
1270 : :
1271 : 176 : bits = ip_bits(ip);
1272 : 176 : a = ip_addr(ip);
1273 : 176 : b = ip_addr(dst);
1274 : :
1275 : 176 : byte = 0;
1276 : :
1277 [ + + ]: 876 : while (bits)
1278 : : {
1279 [ + + ]: 700 : if (bits >= 8)
1280 : : {
1281 : 684 : mask = 0xff;
1282 : 684 : bits -= 8;
1283 : : }
1284 : : else
1285 : : {
1286 : 16 : mask = 0xff << (8 - bits);
1287 : 16 : bits = 0;
1288 : : }
1289 : :
1290 : 700 : b[byte] = a[byte] & mask;
1291 : 700 : byte++;
1292 : : }
1293 : :
1294 : 176 : ip_family(dst) = ip_family(ip);
1295 : 176 : ip_bits(dst) = ip_bits(ip);
1296 [ + + ]: 176 : SET_INET_VARSIZE(dst);
1297 : :
1298 : 176 : PG_RETURN_INET_P(dst);
1299 : : }
1300 : :
1301 : : Datum
1302 : 68 : network_netmask(PG_FUNCTION_ARGS)
1303 : : {
1304 : 68 : inet *ip = PG_GETARG_INET_PP(0);
1305 : : inet *dst;
1306 : : int byte;
1307 : : int bits;
1308 : : unsigned char mask;
1309 : : unsigned char *b;
1310 : :
1311 : : /* make sure any unused bits are zeroed */
1312 : 68 : dst = palloc0_object(inet);
1313 : :
1314 : 68 : bits = ip_bits(ip);
1315 : 68 : b = ip_addr(dst);
1316 : :
1317 : 68 : byte = 0;
1318 : :
1319 [ + + ]: 316 : while (bits)
1320 : : {
1321 [ + + ]: 248 : if (bits >= 8)
1322 : : {
1323 : 240 : mask = 0xff;
1324 : 240 : bits -= 8;
1325 : : }
1326 : : else
1327 : : {
1328 : 8 : mask = 0xff << (8 - bits);
1329 : 8 : bits = 0;
1330 : : }
1331 : :
1332 : 248 : b[byte] = mask;
1333 : 248 : byte++;
1334 : : }
1335 : :
1336 : 68 : ip_family(dst) = ip_family(ip);
1337 [ + + ]: 68 : ip_bits(dst) = ip_maxbits(ip);
1338 [ + + ]: 68 : SET_INET_VARSIZE(dst);
1339 : :
1340 : 68 : PG_RETURN_INET_P(dst);
1341 : : }
1342 : :
1343 : : Datum
1344 : 68 : network_hostmask(PG_FUNCTION_ARGS)
1345 : : {
1346 : 68 : inet *ip = PG_GETARG_INET_PP(0);
1347 : : inet *dst;
1348 : : int byte;
1349 : : int bits;
1350 : : int maxbytes;
1351 : : unsigned char mask;
1352 : : unsigned char *b;
1353 : :
1354 : : /* make sure any unused bits are zeroed */
1355 : 68 : dst = palloc0_object(inet);
1356 : :
1357 [ + + ]: 68 : maxbytes = ip_addrsize(ip);
1358 [ + + ]: 68 : bits = ip_maxbits(ip) - ip_bits(ip);
1359 : 68 : b = ip_addr(dst);
1360 : :
1361 : 68 : byte = maxbytes - 1;
1362 : :
1363 [ + + ]: 244 : while (bits)
1364 : : {
1365 [ + + ]: 176 : if (bits >= 8)
1366 : : {
1367 : 168 : mask = 0xff;
1368 : 168 : bits -= 8;
1369 : : }
1370 : : else
1371 : : {
1372 : 8 : mask = 0xff >> (8 - bits);
1373 : 8 : bits = 0;
1374 : : }
1375 : :
1376 : 176 : b[byte] = mask;
1377 : 176 : byte--;
1378 : : }
1379 : :
1380 : 68 : ip_family(dst) = ip_family(ip);
1381 [ + + ]: 68 : ip_bits(dst) = ip_maxbits(ip);
1382 [ + + ]: 68 : SET_INET_VARSIZE(dst);
1383 : :
1384 : 68 : PG_RETURN_INET_P(dst);
1385 : : }
1386 : :
1387 : : /*
1388 : : * Returns true if the addresses are from the same family, or false. Used to
1389 : : * check that we can create a network which contains both of the networks.
1390 : : */
1391 : : Datum
1392 : 160 : inet_same_family(PG_FUNCTION_ARGS)
1393 : : {
1394 : 160 : inet *a1 = PG_GETARG_INET_PP(0);
1395 : 160 : inet *a2 = PG_GETARG_INET_PP(1);
1396 : :
1397 : 160 : PG_RETURN_BOOL(ip_family(a1) == ip_family(a2));
1398 : : }
1399 : :
1400 : : /*
1401 : : * Returns the smallest CIDR which contains both of the inputs.
1402 : : */
1403 : : Datum
1404 : 156 : inet_merge(PG_FUNCTION_ARGS)
1405 : : {
1406 : 156 : inet *a1 = PG_GETARG_INET_PP(0),
1407 : 156 : *a2 = PG_GETARG_INET_PP(1);
1408 : : int commonbits;
1409 : :
1410 [ + + ]: 156 : if (ip_family(a1) != ip_family(a2))
1411 [ + - ]: 4 : ereport(ERROR,
1412 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1413 : : errmsg("cannot merge addresses from different families")));
1414 : :
1415 : 152 : commonbits = bitncommon(ip_addr(a1), ip_addr(a2),
1416 [ + + ]: 152 : Min(ip_bits(a1), ip_bits(a2)));
1417 : :
1418 : 152 : PG_RETURN_INET_P(cidr_set_masklen_internal(a1, commonbits));
1419 : : }
1420 : :
1421 : : /*
1422 : : * Convert a value of a network datatype to an approximate scalar value.
1423 : : * This is used for estimating selectivities of inequality operators
1424 : : * involving network types.
1425 : : *
1426 : : * On failure (e.g., unsupported typid), set *failure to true;
1427 : : * otherwise, that variable is not changed.
1428 : : */
1429 : : double
1430 : 5454 : convert_network_to_scalar(Datum value, Oid typid, bool *failure)
1431 : : {
1432 [ + - - - ]: 5454 : switch (typid)
1433 : : {
1434 : 5454 : case INETOID:
1435 : : case CIDROID:
1436 : : {
1437 : 5454 : inet *ip = DatumGetInetPP(value);
1438 : : int len;
1439 : : double res;
1440 : : int i;
1441 : :
1442 : : /*
1443 : : * Note that we don't use the full address for IPv6.
1444 : : */
1445 [ + - ]: 5454 : if (ip_family(ip) == PGSQL_AF_INET)
1446 : 5454 : len = 4;
1447 : : else
1448 : 0 : len = 5;
1449 : :
1450 : 5454 : res = ip_family(ip);
1451 [ + + ]: 27270 : for (i = 0; i < len; i++)
1452 : : {
1453 : 21816 : res *= 256;
1454 : 21816 : res += ip_addr(ip)[i];
1455 : : }
1456 : 5454 : return res;
1457 : : }
1458 : 0 : case MACADDROID:
1459 : : {
1460 : 0 : macaddr *mac = DatumGetMacaddrP(value);
1461 : : double res;
1462 : :
1463 : 0 : res = (mac->a << 16) | (mac->b << 8) | (mac->c);
1464 : 0 : res *= 256 * 256 * 256;
1465 : 0 : res += (mac->d << 16) | (mac->e << 8) | (mac->f);
1466 : 0 : return res;
1467 : : }
1468 : 0 : case MACADDR8OID:
1469 : : {
1470 : 0 : macaddr8 *mac = DatumGetMacaddr8P(value);
1471 : : double res;
1472 : :
1473 : 0 : res = (mac->a << 24) | (mac->b << 16) | (mac->c << 8) | (mac->d);
1474 : 0 : res *= ((double) 256) * 256 * 256 * 256;
1475 : 0 : res += (mac->e << 24) | (mac->f << 16) | (mac->g << 8) | (mac->h);
1476 : 0 : return res;
1477 : : }
1478 : : }
1479 : :
1480 : 0 : *failure = true;
1481 : 0 : return 0;
1482 : : }
1483 : :
1484 : : /*
1485 : : * int
1486 : : * bitncmp(l, r, n)
1487 : : * compare bit masks l and r, for n bits.
1488 : : * return:
1489 : : * <0, >0, or 0 in the libc tradition.
1490 : : * note:
1491 : : * network byte order assumed. this means 192.5.5.240/28 has
1492 : : * 0x11110000 in its fourth octet.
1493 : : * author:
1494 : : * Paul Vixie (ISC), June 1996
1495 : : */
1496 : : int
1497 : 128425 : bitncmp(const unsigned char *l, const unsigned char *r, int n)
1498 : : {
1499 : : unsigned int lb,
1500 : : rb;
1501 : : int x,
1502 : : b;
1503 : :
1504 : 128425 : b = n / 8;
1505 : 128425 : x = memcmp(l, r, b);
1506 [ + + + + ]: 128425 : if (x || (n % 8) == 0)
1507 : 128264 : return x;
1508 : :
1509 : 161 : lb = l[b];
1510 : 161 : rb = r[b];
1511 [ + + ]: 274 : for (b = n % 8; b > 0; b--)
1512 : : {
1513 [ + + ]: 185 : if (IS_HIGHBIT_SET(lb) != IS_HIGHBIT_SET(rb))
1514 : : {
1515 [ + + ]: 72 : if (IS_HIGHBIT_SET(lb))
1516 : 40 : return 1;
1517 : 32 : return -1;
1518 : : }
1519 : 113 : lb <<= 1;
1520 : 113 : rb <<= 1;
1521 : : }
1522 : 89 : return 0;
1523 : : }
1524 : :
1525 : : /*
1526 : : * bitncommon: compare bit masks l and r, for up to n bits.
1527 : : *
1528 : : * Returns the number of leading bits that match (0 to n).
1529 : : */
1530 : : int
1531 : 1698 : bitncommon(const unsigned char *l, const unsigned char *r, int n)
1532 : : {
1533 : : int byte,
1534 : : nbits;
1535 : :
1536 : : /* number of bits to examine in last byte */
1537 : 1698 : nbits = n % 8;
1538 : :
1539 : : /* check whole bytes */
1540 [ + + ]: 2369 : for (byte = 0; byte < n / 8; byte++)
1541 : : {
1542 [ + + ]: 705 : if (l[byte] != r[byte])
1543 : : {
1544 : : /* at least one bit in the last byte is not common */
1545 : 34 : nbits = 7;
1546 : 34 : break;
1547 : : }
1548 : : }
1549 : :
1550 : : /* check bits in last partial byte */
1551 [ + + ]: 1698 : if (nbits != 0)
1552 : : {
1553 : : /* calculate diff of first non-matching bytes */
1554 : 1294 : unsigned int diff = l[byte] ^ r[byte];
1555 : :
1556 : : /* compare the bits from the most to the least */
1557 [ + + ]: 1540 : while ((diff >> (8 - nbits)) != 0)
1558 : 246 : nbits--;
1559 : : }
1560 : :
1561 : 1698 : return (8 * byte) + nbits;
1562 : : }
1563 : :
1564 : :
1565 : : /*
1566 : : * Verify a CIDR address is OK (doesn't have bits set past the masklen)
1567 : : */
1568 : : static bool
1569 : 1173 : addressOK(unsigned char *a, int bits, int family)
1570 : : {
1571 : : int byte;
1572 : : int nbits;
1573 : : int maxbits;
1574 : : int maxbytes;
1575 : : unsigned char mask;
1576 : :
1577 [ + + ]: 1173 : if (family == PGSQL_AF_INET)
1578 : : {
1579 : 911 : maxbits = 32;
1580 : 911 : maxbytes = 4;
1581 : : }
1582 : : else
1583 : : {
1584 : 262 : maxbits = 128;
1585 : 262 : maxbytes = 16;
1586 : : }
1587 : : Assert(bits <= maxbits);
1588 : :
1589 [ + + ]: 1173 : if (bits == maxbits)
1590 : 478 : return true;
1591 : :
1592 : 695 : byte = bits / 8;
1593 : :
1594 : 695 : nbits = bits % 8;
1595 : 695 : mask = 0xff;
1596 [ + + ]: 695 : if (bits != 0)
1597 : 663 : mask >>= nbits;
1598 : :
1599 [ + + ]: 2434 : while (byte < maxbytes)
1600 : : {
1601 [ + + ]: 1759 : if ((a[byte] & mask) != 0)
1602 : 20 : return false;
1603 : 1739 : mask = 0xff;
1604 : 1739 : byte++;
1605 : : }
1606 : :
1607 : 675 : return true;
1608 : : }
1609 : :
1610 : :
1611 : : /*
1612 : : * These functions are used by planner to generate indexscan limits
1613 : : * for clauses a << b and a <<= b
1614 : : */
1615 : :
1616 : : /* return the minimal value for an IP on a given network */
1617 : : Datum
1618 : 40 : network_scan_first(Datum in)
1619 : : {
1620 : 40 : return DirectFunctionCall1(network_network, in);
1621 : : }
1622 : :
1623 : : /*
1624 : : * return "last" IP on a given network. It's the broadcast address,
1625 : : * however, masklen has to be set to its max bits, since
1626 : : * 192.168.0.255/24 is considered less than 192.168.0.255/32
1627 : : *
1628 : : * inet_set_masklen() hacked to max out the masklength to 128 for IPv6
1629 : : * and 32 for IPv4 when given '-1' as argument.
1630 : : */
1631 : : Datum
1632 : 40 : network_scan_last(Datum in)
1633 : : {
1634 : 40 : return DirectFunctionCall2(inet_set_masklen,
1635 : : DirectFunctionCall1(network_broadcast, in),
1636 : : Int32GetDatum(-1));
1637 : : }
1638 : :
1639 : :
1640 : : /*
1641 : : * IP address that the client is connecting from (NULL if Unix socket)
1642 : : */
1643 : : Datum
1644 : 0 : inet_client_addr(PG_FUNCTION_ARGS)
1645 : : {
1646 : 0 : Port *port = MyProcPort;
1647 : : char remote_host[NI_MAXHOST];
1648 : : int ret;
1649 : :
1650 [ # # ]: 0 : if (port == NULL)
1651 : 0 : PG_RETURN_NULL();
1652 : :
1653 [ # # ]: 0 : switch (port->raddr.addr.ss_family)
1654 : : {
1655 : 0 : case AF_INET:
1656 : : case AF_INET6:
1657 : 0 : break;
1658 : 0 : default:
1659 : 0 : PG_RETURN_NULL();
1660 : : }
1661 : :
1662 : 0 : remote_host[0] = '\0';
1663 : :
1664 : 0 : ret = pg_getnameinfo_all(&port->raddr.addr, port->raddr.salen,
1665 : : remote_host, sizeof(remote_host),
1666 : : NULL, 0,
1667 : : NI_NUMERICHOST | NI_NUMERICSERV);
1668 [ # # ]: 0 : if (ret != 0)
1669 : 0 : PG_RETURN_NULL();
1670 : :
1671 : 0 : clean_ipv6_addr(port->raddr.addr.ss_family, remote_host);
1672 : :
1673 : 0 : PG_RETURN_INET_P(network_in(remote_host, false, NULL));
1674 : : }
1675 : :
1676 : :
1677 : : /*
1678 : : * port that the client is connecting from (NULL if Unix socket)
1679 : : */
1680 : : Datum
1681 : 0 : inet_client_port(PG_FUNCTION_ARGS)
1682 : : {
1683 : 0 : Port *port = MyProcPort;
1684 : : char remote_port[NI_MAXSERV];
1685 : : int ret;
1686 : :
1687 [ # # ]: 0 : if (port == NULL)
1688 : 0 : PG_RETURN_NULL();
1689 : :
1690 [ # # ]: 0 : switch (port->raddr.addr.ss_family)
1691 : : {
1692 : 0 : case AF_INET:
1693 : : case AF_INET6:
1694 : 0 : break;
1695 : 0 : default:
1696 : 0 : PG_RETURN_NULL();
1697 : : }
1698 : :
1699 : 0 : remote_port[0] = '\0';
1700 : :
1701 : 0 : ret = pg_getnameinfo_all(&port->raddr.addr, port->raddr.salen,
1702 : : NULL, 0,
1703 : : remote_port, sizeof(remote_port),
1704 : : NI_NUMERICHOST | NI_NUMERICSERV);
1705 [ # # ]: 0 : if (ret != 0)
1706 : 0 : PG_RETURN_NULL();
1707 : :
1708 : 0 : PG_RETURN_DATUM(DirectFunctionCall1(int4in, CStringGetDatum(remote_port)));
1709 : : }
1710 : :
1711 : :
1712 : : /*
1713 : : * IP address that the server accepted the connection on (NULL if Unix socket)
1714 : : */
1715 : : Datum
1716 : 0 : inet_server_addr(PG_FUNCTION_ARGS)
1717 : : {
1718 : 0 : Port *port = MyProcPort;
1719 : : char local_host[NI_MAXHOST];
1720 : : int ret;
1721 : :
1722 [ # # ]: 0 : if (port == NULL)
1723 : 0 : PG_RETURN_NULL();
1724 : :
1725 [ # # ]: 0 : switch (port->laddr.addr.ss_family)
1726 : : {
1727 : 0 : case AF_INET:
1728 : : case AF_INET6:
1729 : 0 : break;
1730 : 0 : default:
1731 : 0 : PG_RETURN_NULL();
1732 : : }
1733 : :
1734 : 0 : local_host[0] = '\0';
1735 : :
1736 : 0 : ret = pg_getnameinfo_all(&port->laddr.addr, port->laddr.salen,
1737 : : local_host, sizeof(local_host),
1738 : : NULL, 0,
1739 : : NI_NUMERICHOST | NI_NUMERICSERV);
1740 [ # # ]: 0 : if (ret != 0)
1741 : 0 : PG_RETURN_NULL();
1742 : :
1743 : 0 : clean_ipv6_addr(port->laddr.addr.ss_family, local_host);
1744 : :
1745 : 0 : PG_RETURN_INET_P(network_in(local_host, false, NULL));
1746 : : }
1747 : :
1748 : :
1749 : : /*
1750 : : * port that the server accepted the connection on (NULL if Unix socket)
1751 : : */
1752 : : Datum
1753 : 0 : inet_server_port(PG_FUNCTION_ARGS)
1754 : : {
1755 : 0 : Port *port = MyProcPort;
1756 : : char local_port[NI_MAXSERV];
1757 : : int ret;
1758 : :
1759 [ # # ]: 0 : if (port == NULL)
1760 : 0 : PG_RETURN_NULL();
1761 : :
1762 [ # # ]: 0 : switch (port->laddr.addr.ss_family)
1763 : : {
1764 : 0 : case AF_INET:
1765 : : case AF_INET6:
1766 : 0 : break;
1767 : 0 : default:
1768 : 0 : PG_RETURN_NULL();
1769 : : }
1770 : :
1771 : 0 : local_port[0] = '\0';
1772 : :
1773 : 0 : ret = pg_getnameinfo_all(&port->laddr.addr, port->laddr.salen,
1774 : : NULL, 0,
1775 : : local_port, sizeof(local_port),
1776 : : NI_NUMERICHOST | NI_NUMERICSERV);
1777 [ # # ]: 0 : if (ret != 0)
1778 : 0 : PG_RETURN_NULL();
1779 : :
1780 : 0 : PG_RETURN_DATUM(DirectFunctionCall1(int4in, CStringGetDatum(local_port)));
1781 : : }
1782 : :
1783 : :
1784 : : Datum
1785 : 68 : inetnot(PG_FUNCTION_ARGS)
1786 : : {
1787 : 68 : inet *ip = PG_GETARG_INET_PP(0);
1788 : : inet *dst;
1789 : :
1790 : 68 : dst = palloc0_object(inet);
1791 : :
1792 : : {
1793 [ + + ]: 68 : int nb = ip_addrsize(ip);
1794 : 68 : unsigned char *pip = ip_addr(ip);
1795 : 68 : unsigned char *pdst = ip_addr(dst);
1796 : :
1797 [ + + ]: 484 : while (--nb >= 0)
1798 : 416 : pdst[nb] = ~pip[nb];
1799 : : }
1800 : 68 : ip_bits(dst) = ip_bits(ip);
1801 : :
1802 : 68 : ip_family(dst) = ip_family(ip);
1803 [ + + ]: 68 : SET_INET_VARSIZE(dst);
1804 : :
1805 : 68 : PG_RETURN_INET_P(dst);
1806 : : }
1807 : :
1808 : :
1809 : : Datum
1810 : 68 : inetand(PG_FUNCTION_ARGS)
1811 : : {
1812 : 68 : inet *ip = PG_GETARG_INET_PP(0);
1813 : 68 : inet *ip2 = PG_GETARG_INET_PP(1);
1814 : : inet *dst;
1815 : :
1816 : 68 : dst = palloc0_object(inet);
1817 : :
1818 [ - + ]: 68 : if (ip_family(ip) != ip_family(ip2))
1819 [ # # ]: 0 : ereport(ERROR,
1820 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1821 : : errmsg("cannot AND inet values of different sizes")));
1822 : : else
1823 : : {
1824 [ + + ]: 68 : int nb = ip_addrsize(ip);
1825 : 68 : unsigned char *pip = ip_addr(ip);
1826 : 68 : unsigned char *pip2 = ip_addr(ip2);
1827 : 68 : unsigned char *pdst = ip_addr(dst);
1828 : :
1829 [ + + ]: 484 : while (--nb >= 0)
1830 : 416 : pdst[nb] = pip[nb] & pip2[nb];
1831 : : }
1832 [ + + ]: 68 : ip_bits(dst) = Max(ip_bits(ip), ip_bits(ip2));
1833 : :
1834 : 68 : ip_family(dst) = ip_family(ip);
1835 [ + + ]: 68 : SET_INET_VARSIZE(dst);
1836 : :
1837 : 68 : PG_RETURN_INET_P(dst);
1838 : : }
1839 : :
1840 : :
1841 : : Datum
1842 : 68 : inetor(PG_FUNCTION_ARGS)
1843 : : {
1844 : 68 : inet *ip = PG_GETARG_INET_PP(0);
1845 : 68 : inet *ip2 = PG_GETARG_INET_PP(1);
1846 : : inet *dst;
1847 : :
1848 : 68 : dst = palloc0_object(inet);
1849 : :
1850 [ - + ]: 68 : if (ip_family(ip) != ip_family(ip2))
1851 [ # # ]: 0 : ereport(ERROR,
1852 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1853 : : errmsg("cannot OR inet values of different sizes")));
1854 : : else
1855 : : {
1856 [ + + ]: 68 : int nb = ip_addrsize(ip);
1857 : 68 : unsigned char *pip = ip_addr(ip);
1858 : 68 : unsigned char *pip2 = ip_addr(ip2);
1859 : 68 : unsigned char *pdst = ip_addr(dst);
1860 : :
1861 [ + + ]: 484 : while (--nb >= 0)
1862 : 416 : pdst[nb] = pip[nb] | pip2[nb];
1863 : : }
1864 [ + + ]: 68 : ip_bits(dst) = Max(ip_bits(ip), ip_bits(ip2));
1865 : :
1866 : 68 : ip_family(dst) = ip_family(ip);
1867 [ + + ]: 68 : SET_INET_VARSIZE(dst);
1868 : :
1869 : 68 : PG_RETURN_INET_P(dst);
1870 : : }
1871 : :
1872 : :
1873 : : static inet *
1874 : 3439 : internal_inetpl(inet *ip, int64 addend)
1875 : : {
1876 : : inet *dst;
1877 : :
1878 : 3439 : dst = palloc0_object(inet);
1879 : :
1880 : : {
1881 [ + + ]: 3439 : int nb = ip_addrsize(ip);
1882 : 3439 : unsigned char *pip = ip_addr(ip);
1883 : 3439 : unsigned char *pdst = ip_addr(dst);
1884 : 3439 : int carry = 0;
1885 : :
1886 [ + + ]: 25043 : while (--nb >= 0)
1887 : : {
1888 : 21604 : carry = pip[nb] + (int) (addend & 0xFF) + carry;
1889 : 21604 : pdst[nb] = (unsigned char) (carry & 0xFF);
1890 : 21604 : carry >>= 8;
1891 : :
1892 : : /*
1893 : : * We have to be careful about right-shifting addend because
1894 : : * right-shift isn't portable for negative values, while simply
1895 : : * dividing by 256 doesn't work (the standard rounding is in the
1896 : : * wrong direction, besides which there may be machines out there
1897 : : * that round the wrong way). So, explicitly clear the low-order
1898 : : * byte to remove any doubt about the correct result of the
1899 : : * division, and then divide rather than shift.
1900 : : */
1901 : 21604 : addend &= ~((int64) 0xFF);
1902 : 21604 : addend /= 0x100;
1903 : : }
1904 : :
1905 : : /*
1906 : : * At this point we should have addend and carry both zero if original
1907 : : * addend was >= 0, or addend -1 and carry 1 if original addend was <
1908 : : * 0. Anything else means overflow.
1909 : : */
1910 [ + + - + : 3439 : if (!((addend == 0 && carry == 0) ||
+ + ]
1911 [ - + ]: 88 : (addend == -1 && carry == 1)))
1912 [ + - ]: 8 : ereport(ERROR,
1913 : : (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1914 : : errmsg("result is out of range")));
1915 : : }
1916 : :
1917 : 3431 : ip_bits(dst) = ip_bits(ip);
1918 : 3431 : ip_family(dst) = ip_family(ip);
1919 [ + + ]: 3431 : SET_INET_VARSIZE(dst);
1920 : :
1921 : 3431 : return dst;
1922 : : }
1923 : :
1924 : :
1925 : : Datum
1926 : 3347 : inetpl(PG_FUNCTION_ARGS)
1927 : : {
1928 : 3347 : inet *ip = PG_GETARG_INET_PP(0);
1929 : 3347 : int64 addend = PG_GETARG_INT64(1);
1930 : :
1931 : 3347 : PG_RETURN_INET_P(internal_inetpl(ip, addend));
1932 : : }
1933 : :
1934 : :
1935 : : Datum
1936 : 92 : inetmi_int8(PG_FUNCTION_ARGS)
1937 : : {
1938 : 92 : inet *ip = PG_GETARG_INET_PP(0);
1939 : 92 : int64 addend = PG_GETARG_INT64(1);
1940 : :
1941 : 92 : PG_RETURN_INET_P(internal_inetpl(ip, -addend));
1942 : : }
1943 : :
1944 : :
1945 : : Datum
1946 : 101 : inetmi(PG_FUNCTION_ARGS)
1947 : : {
1948 : 101 : inet *ip = PG_GETARG_INET_PP(0);
1949 : 101 : inet *ip2 = PG_GETARG_INET_PP(1);
1950 : 101 : int64 res = 0;
1951 : :
1952 [ - + ]: 101 : if (ip_family(ip) != ip_family(ip2))
1953 [ # # ]: 0 : ereport(ERROR,
1954 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1955 : : errmsg("cannot subtract inet values of different sizes")));
1956 : : else
1957 : : {
1958 : : /*
1959 : : * We form the difference using the traditional complement, increment,
1960 : : * and add rule, with the increment part being handled by starting the
1961 : : * carry off at 1. If you don't think integer arithmetic is done in
1962 : : * two's complement, too bad.
1963 : : */
1964 [ + + ]: 101 : int nb = ip_addrsize(ip);
1965 : 101 : int byte = 0;
1966 : 101 : unsigned char *pip = ip_addr(ip);
1967 : 101 : unsigned char *pip2 = ip_addr(ip2);
1968 : 101 : int carry = 1;
1969 : :
1970 [ + + ]: 909 : while (--nb >= 0)
1971 : : {
1972 : : int lobyte;
1973 : :
1974 : 816 : carry = pip[nb] + (~pip2[nb] & 0xFF) + carry;
1975 : 816 : lobyte = carry & 0xFF;
1976 [ + + ]: 816 : if (byte < sizeof(int64))
1977 : : {
1978 : 544 : res |= ((int64) lobyte) << (byte * 8);
1979 : : }
1980 : : else
1981 : : {
1982 : : /*
1983 : : * Input wider than int64: check for overflow. All bytes to
1984 : : * the left of what will fit should be 0 or 0xFF, depending on
1985 : : * sign of the now-complete result.
1986 : : */
1987 [ + + + + ]: 272 : if ((res < 0) ? (lobyte != 0xFF) : (lobyte != 0))
1988 [ + - ]: 8 : ereport(ERROR,
1989 : : (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1990 : : errmsg("result is out of range")));
1991 : : }
1992 : 808 : carry >>= 8;
1993 : 808 : byte++;
1994 : : }
1995 : :
1996 : : /*
1997 : : * If input is narrower than int64, overflow is not possible, but we
1998 : : * have to do proper sign extension.
1999 : : */
2000 [ + + + + ]: 93 : if (carry == 0 && byte < sizeof(int64))
2001 : 9 : res |= ((uint64) (int64) -1) << (byte * 8);
2002 : : }
2003 : :
2004 : 93 : PG_RETURN_INT64(res);
2005 : : }
2006 : :
2007 : :
2008 : : /*
2009 : : * clean_ipv6_addr --- remove any '%zone' part from an IPv6 address string
2010 : : *
2011 : : * XXX This should go away someday!
2012 : : *
2013 : : * This is a kluge needed because we don't yet support zones in stored inet
2014 : : * values. Since the result of getnameinfo() might include a zone spec,
2015 : : * call this to remove it anywhere we want to feed getnameinfo's output to
2016 : : * network_in. Beats failing entirely.
2017 : : *
2018 : : * An alternative approach would be to let network_in ignore %-parts for
2019 : : * itself, but that would mean we'd silently drop zone specs in user input,
2020 : : * which seems not such a good idea.
2021 : : */
2022 : : void
2023 : 93 : clean_ipv6_addr(int addr_family, char *addr)
2024 : : {
2025 [ + + ]: 93 : if (addr_family == AF_INET6)
2026 : : {
2027 : 24 : char *pct = strchr(addr, '%');
2028 : :
2029 [ - + ]: 24 : if (pct)
2030 : 0 : *pct = '\0';
2031 : : }
2032 : 93 : }
|