2 * PostgreSQL type definitions for the INET type. This
3 * is for IP V4 CIDR notation, but prepared for V6: just
4 * add the necessary bits where the comments indicate.
6 * $Id: network.c,v 1.12 1999/07/15 15:20:18 momjian Exp $
7 * Jon Postel RIP 16 Oct 1998
10 #include <sys/types.h>
11 #include <sys/socket.h>
17 #include <netinet/in.h>
18 #include <arpa/inet.h>
21 #include <utils/builtins.h>
22 #include <utils/inet.h>
24 static int v4bitncmp(unsigned int a1, unsigned int a2, int bits);
27 * Access macros. Add IPV6 support.
30 #define ip_addrsize(inetptr) \
31 (((inet_struct *)VARDATA(inetptr))->family == AF_INET ? 4 : -1)
33 #define ip_family(inetptr) \
34 (((inet_struct *)VARDATA(inetptr))->family)
36 #define ip_bits(inetptr) \
37 (((inet_struct *)VARDATA(inetptr))->bits)
39 #define ip_type(inetptr) \
40 (((inet_struct *)VARDATA(inetptr))->type)
42 #define ip_v4addr(inetptr) \
43 (((inet_struct *)VARDATA(inetptr))->addr.ipv4_addr)
45 /* Common input routine */
47 network_in(char *src, int type)
55 dst = palloc(VARHDRSZ + sizeof(inet_struct));
57 elog(ERROR, "unable to allocate memory in network_in()");
59 /* First, try for an IP V4 address: */
60 ip_family(dst) = AF_INET;
61 bits = inet_net_pton(ip_family(dst), src, &ip_v4addr(dst),
62 type ? ip_addrsize(dst) : -1);
63 if ((bits < 0) || (bits > 32))
64 /* Go for an IPV6 address here, before faulting out: */
65 elog(ERROR, "could not parse \"%s\"", src);
67 VARSIZE(dst) = VARHDRSZ
68 + ((char *) &ip_v4addr(dst) - (char *) VARDATA(dst))
75 /* INET address reader. */
79 return network_in(src, 0);
82 /* CIDR address reader. */
86 return network_in(src, 1);
90 * INET address output function.
97 tmp[sizeof("255.255.255.255/32")];
99 if (ip_family(src) == AF_INET)
101 /* It's an IP V4 address: */
103 dst = inet_cidr_ntop(AF_INET, &ip_v4addr(src), ip_bits(src),
106 dst = inet_net_ntop(AF_INET, &ip_v4addr(src), ip_bits(src),
110 elog(ERROR, "unable to print address (%s)", strerror(errno));
113 /* Go for an IPV6 address here, before faulting out: */
114 elog(ERROR, "unknown address family (%d)", ip_family(src));
116 dst = palloc(strlen(tmp) + 1);
118 elog(ERROR, "unable to allocate memory in inet_out()");
129 return inet_out(src);
133 * Boolean tests for magnitude. Add V4/V6 testing!
137 network_lt(inet *a1, inet *a2)
139 if (!PointerIsValid(a1) || !PointerIsValid(a2))
141 if ((ip_family(a1) == AF_INET) && (ip_family(a2) == AF_INET))
143 int order = v4bitncmp(ip_v4addr(a1), ip_v4addr(a2), ip_bits(a2));
145 return ((order < 0) || ((order == 0) && (ip_bits(a1) < ip_bits(a2))));
149 /* Go for an IPV6 address here, before faulting out: */
150 elog(ERROR, "cannot compare address families %d and %d",
151 ip_family(a1), ip_family(a2));
157 network_le(inet *a1, inet *a2)
159 if (!PointerIsValid(a1) || !PointerIsValid(a2))
161 return (network_lt(a1, a2) || network_eq(a1, a2));
165 network_eq(inet *a1, inet *a2)
167 if (!PointerIsValid(a1) || !PointerIsValid(a2))
169 if ((ip_family(a1) == AF_INET) && (ip_family(a2) == AF_INET))
171 return ((ip_bits(a1) == ip_bits(a2))
172 && (v4bitncmp(ip_v4addr(a1), ip_v4addr(a2), ip_bits(a1)) == 0));
176 /* Go for an IPV6 address here, before faulting out: */
177 elog(ERROR, "cannot compare address families %d and %d",
178 ip_family(a1), ip_family(a2));
184 network_ge(inet *a1, inet *a2)
186 if (!PointerIsValid(a1) || !PointerIsValid(a2))
188 return (network_gt(a1, a2) || network_eq(a1, a2));
192 network_gt(inet *a1, inet *a2)
194 if (!PointerIsValid(a1) || !PointerIsValid(a2))
196 if ((ip_family(a1) == AF_INET) && (ip_family(a2) == AF_INET))
198 int order = v4bitncmp(ip_v4addr(a1), ip_v4addr(a2), ip_bits(a2));
200 return ((order > 0) || ((order == 0) && (ip_bits(a1) > ip_bits(a2))));
204 /* Go for an IPV6 address here, before faulting out: */
205 elog(ERROR, "cannot compare address families %d and %d",
206 ip_family(a1), ip_family(a2));
212 network_ne(inet *a1, inet *a2)
214 if (!PointerIsValid(a1) || !PointerIsValid(a2))
216 return (!network_eq(a1, a2));
220 network_sub(inet *a1, inet *a2)
222 if (!PointerIsValid(a1) || !PointerIsValid(a2))
225 if ((ip_family(a1) == AF_INET) && (ip_family(a2) == AF_INET))
227 return ((ip_bits(a1) > ip_bits(a2))
228 && (v4bitncmp(ip_v4addr(a1), ip_v4addr(a2), ip_bits(a2)) == 0));
232 /* Go for an IPV6 address here, before faulting out: */
233 elog(ERROR, "cannot compare address families %d and %d",
234 ip_family(a1), ip_family(a2));
240 network_subeq(inet *a1, inet *a2)
242 if (!PointerIsValid(a1) || !PointerIsValid(a2))
245 if ((ip_family(a1) == AF_INET) && (ip_family(a2) == AF_INET))
247 return ((ip_bits(a1) >= ip_bits(a2))
248 && (v4bitncmp(ip_v4addr(a1), ip_v4addr(a2), ip_bits(a2)) == 0));
252 /* Go for an IPV6 address here, before faulting out: */
253 elog(ERROR, "cannot compare address families %d and %d",
254 ip_family(a1), ip_family(a2));
260 network_sup(inet *a1, inet *a2)
262 if (!PointerIsValid(a1) || !PointerIsValid(a2))
265 if ((ip_family(a1) == AF_INET) && (ip_family(a2) == AF_INET))
267 return ((ip_bits(a1) < ip_bits(a2))
268 && (v4bitncmp(ip_v4addr(a1), ip_v4addr(a2), ip_bits(a1)) == 0));
272 /* Go for an IPV6 address here, before faulting out: */
273 elog(ERROR, "cannot compare address families %d and %d",
274 ip_family(a1), ip_family(a2));
280 network_supeq(inet *a1, inet *a2)
282 if (!PointerIsValid(a1) || !PointerIsValid(a2))
285 if ((ip_family(a1) == AF_INET) && (ip_family(a2) == AF_INET))
287 return ((ip_bits(a1) <= ip_bits(a2))
288 && (v4bitncmp(ip_v4addr(a1), ip_v4addr(a2), ip_bits(a1)) == 0));
292 /* Go for an IPV6 address here, before faulting out: */
293 elog(ERROR, "cannot compare address families %d and %d",
294 ip_family(a1), ip_family(a2));
300 * Comparison function for sorting. Add V4/V6 testing!
304 network_cmp(inet *a1, inet *a2)
306 if (ntohl(ip_v4addr(a1)) < ntohl(ip_v4addr(a2)))
309 if (ntohl(ip_v4addr(a1)) > ntohl(ip_v4addr(a2)))
312 if (ip_bits(a1) < ip_bits(a2))
315 if (ip_bits(a1) > ip_bits(a2))
322 network_host(inet *ip)
327 tmp[sizeof("255.255.255.255/32")];
329 if (!PointerIsValid(ip))
333 elog(ERROR, "CIDR type has no host part");
335 if (ip_family(ip) == AF_INET)
337 /* It's an IP V4 address: */
338 if (inet_net_ntop(AF_INET, &ip_v4addr(ip), 32, tmp, sizeof(tmp)) == NULL)
339 elog(ERROR, "unable to print host (%s)", strerror(errno));
342 /* Go for an IPV6 address here, before faulting out: */
343 elog(ERROR, "unknown address family (%d)", ip_family(ip));
345 if ((ptr = strchr(tmp, '/')) != NULL)
347 len = VARHDRSZ + strlen(tmp) + 1;
350 elog(ERROR, "unable to allocate memory in network_host()");
353 strcpy(VARDATA(ret), tmp);
358 network_masklen(inet *ip)
360 if (!PointerIsValid(ip))
367 network_broadcast(inet *ip)
372 tmp[sizeof("255.255.255.255/32")];
374 if (!PointerIsValid(ip))
377 if (ip_family(ip) == AF_INET)
379 /* It's an IP V4 address: */
381 unsigned long mask = 0xffffffff;
383 if (ip_bits(ip) < 32)
384 mask >>= ip_bits(ip);
385 addr = htonl(ntohl(ip_v4addr(ip)) | mask);
387 if (inet_net_ntop(AF_INET, &addr, 32, tmp, sizeof(tmp)) == NULL)
388 elog(ERROR, "unable to print address (%s)", strerror(errno));
392 /* Go for an IPV6 address here, before faulting out: */
393 elog(ERROR, "unknown address family (%d)", ip_family(ip));
395 if ((ptr = strchr(tmp, '/')) != NULL)
397 len = VARHDRSZ + strlen(tmp) + 1;
400 elog(ERROR, "unable to allocate memory in network_broadcast()");
403 strcpy(VARDATA(ret), tmp);
408 network_network(inet *ip)
412 char tmp[sizeof("255.255.255.255/32")];
414 if (!PointerIsValid(ip))
417 if (ip_family(ip) == AF_INET)
419 /* It's an IP V4 address: */
420 int addr = htonl(ntohl(ip_v4addr(ip)) & (0xffffffff << (32 - ip_bits(ip))));
422 if (inet_cidr_ntop(AF_INET, &addr, ip_bits(ip), tmp, sizeof(tmp)) == NULL)
423 elog(ERROR, "unable to print network (%s)", strerror(errno));
427 /* Go for an IPV6 address here, before faulting out: */
428 elog(ERROR, "unknown address family (%d)", ip_family(ip));
430 len = VARHDRSZ + strlen(tmp) + 1;
433 elog(ERROR, "unable to allocate memory in network_network()");
436 strcpy(VARDATA(ret), tmp);
441 network_netmask(inet *ip)
446 tmp[sizeof("255.255.255.255/32")];
448 if (!PointerIsValid(ip))
451 if (ip_family(ip) == AF_INET)
453 /* It's an IP V4 address: */
454 int addr = htonl((-1 << (32 - ip_bits(ip))) & 0xffffffff);
456 if (inet_net_ntop(AF_INET, &addr, 32, tmp, sizeof(tmp)) == NULL)
457 elog(ERROR, "unable to print netmask (%s)", strerror(errno));
461 /* Go for an IPV6 address here, before faulting out: */
462 elog(ERROR, "unknown address family (%d)", ip_family(ip));
464 if ((ptr = strchr(tmp, '/')) != NULL)
466 len = VARHDRSZ + strlen(tmp) + 1;
469 elog(ERROR, "unable to allocate memory in network_netmask()");
472 strcpy(VARDATA(ret), tmp);
477 * Bitwise comparison for V4 addresses. Add V6 implementation!
481 v4bitncmp(unsigned int a1, unsigned int a2, int bits)
483 unsigned long mask = 0;
486 for (i = 0; i < bits; i++)
487 mask = (mask >> 1) | 0x80000000;
490 if ((a1 & mask) < (a2 & mask))
492 else if ((a1 & mask) > (a2 & mask))