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.13 1999/07/15 22:40:00 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>
23 static int v4bitncmp(unsigned int a1, unsigned int a2, int bits);
26 * Access macros. Add IPV6 support.
29 #define ip_addrsize(inetptr) \
30 (((inet_struct *)VARDATA(inetptr))->family == AF_INET ? 4 : -1)
32 #define ip_family(inetptr) \
33 (((inet_struct *)VARDATA(inetptr))->family)
35 #define ip_bits(inetptr) \
36 (((inet_struct *)VARDATA(inetptr))->bits)
38 #define ip_type(inetptr) \
39 (((inet_struct *)VARDATA(inetptr))->type)
41 #define ip_v4addr(inetptr) \
42 (((inet_struct *)VARDATA(inetptr))->addr.ipv4_addr)
44 /* Common input routine */
46 network_in(char *src, int type)
54 dst = palloc(VARHDRSZ + sizeof(inet_struct));
56 elog(ERROR, "unable to allocate memory in network_in()");
58 /* First, try for an IP V4 address: */
59 ip_family(dst) = AF_INET;
60 bits = inet_net_pton(ip_family(dst), src, &ip_v4addr(dst),
61 type ? ip_addrsize(dst) : -1);
62 if ((bits < 0) || (bits > 32))
63 /* Go for an IPV6 address here, before faulting out: */
64 elog(ERROR, "could not parse \"%s\"", src);
66 VARSIZE(dst) = VARHDRSZ
67 + ((char *) &ip_v4addr(dst) - (char *) VARDATA(dst))
74 /* INET address reader. */
78 return network_in(src, 0);
81 /* CIDR address reader. */
85 return network_in(src, 1);
89 * INET address output function.
96 tmp[sizeof("255.255.255.255/32")];
98 if (ip_family(src) == AF_INET)
100 /* It's an IP V4 address: */
102 dst = inet_cidr_ntop(AF_INET, &ip_v4addr(src), ip_bits(src),
105 dst = inet_net_ntop(AF_INET, &ip_v4addr(src), ip_bits(src),
109 elog(ERROR, "unable to print address (%s)", strerror(errno));
112 /* Go for an IPV6 address here, before faulting out: */
113 elog(ERROR, "unknown address family (%d)", ip_family(src));
115 dst = palloc(strlen(tmp) + 1);
117 elog(ERROR, "unable to allocate memory in inet_out()");
128 return inet_out(src);
132 * Boolean tests for magnitude. Add V4/V6 testing!
136 network_lt(inet *a1, inet *a2)
138 if (!PointerIsValid(a1) || !PointerIsValid(a2))
140 if ((ip_family(a1) == AF_INET) && (ip_family(a2) == AF_INET))
142 int order = v4bitncmp(ip_v4addr(a1), ip_v4addr(a2), ip_bits(a2));
144 return ((order < 0) || ((order == 0) && (ip_bits(a1) < ip_bits(a2))));
148 /* Go for an IPV6 address here, before faulting out: */
149 elog(ERROR, "cannot compare address families %d and %d",
150 ip_family(a1), ip_family(a2));
156 network_le(inet *a1, inet *a2)
158 if (!PointerIsValid(a1) || !PointerIsValid(a2))
160 return (network_lt(a1, a2) || network_eq(a1, a2));
164 network_eq(inet *a1, inet *a2)
166 if (!PointerIsValid(a1) || !PointerIsValid(a2))
168 if ((ip_family(a1) == AF_INET) && (ip_family(a2) == AF_INET))
170 return ((ip_bits(a1) == ip_bits(a2))
171 && (v4bitncmp(ip_v4addr(a1), ip_v4addr(a2), ip_bits(a1)) == 0));
175 /* Go for an IPV6 address here, before faulting out: */
176 elog(ERROR, "cannot compare address families %d and %d",
177 ip_family(a1), ip_family(a2));
183 network_ge(inet *a1, inet *a2)
185 if (!PointerIsValid(a1) || !PointerIsValid(a2))
187 return (network_gt(a1, a2) || network_eq(a1, a2));
191 network_gt(inet *a1, inet *a2)
193 if (!PointerIsValid(a1) || !PointerIsValid(a2))
195 if ((ip_family(a1) == AF_INET) && (ip_family(a2) == AF_INET))
197 int order = v4bitncmp(ip_v4addr(a1), ip_v4addr(a2), ip_bits(a2));
199 return ((order > 0) || ((order == 0) && (ip_bits(a1) > ip_bits(a2))));
203 /* Go for an IPV6 address here, before faulting out: */
204 elog(ERROR, "cannot compare address families %d and %d",
205 ip_family(a1), ip_family(a2));
211 network_ne(inet *a1, inet *a2)
213 if (!PointerIsValid(a1) || !PointerIsValid(a2))
215 return (!network_eq(a1, a2));
219 network_sub(inet *a1, inet *a2)
221 if (!PointerIsValid(a1) || !PointerIsValid(a2))
224 if ((ip_family(a1) == AF_INET) && (ip_family(a2) == AF_INET))
226 return ((ip_bits(a1) > ip_bits(a2))
227 && (v4bitncmp(ip_v4addr(a1), ip_v4addr(a2), ip_bits(a2)) == 0));
231 /* Go for an IPV6 address here, before faulting out: */
232 elog(ERROR, "cannot compare address families %d and %d",
233 ip_family(a1), ip_family(a2));
239 network_subeq(inet *a1, inet *a2)
241 if (!PointerIsValid(a1) || !PointerIsValid(a2))
244 if ((ip_family(a1) == AF_INET) && (ip_family(a2) == AF_INET))
246 return ((ip_bits(a1) >= ip_bits(a2))
247 && (v4bitncmp(ip_v4addr(a1), ip_v4addr(a2), ip_bits(a2)) == 0));
251 /* Go for an IPV6 address here, before faulting out: */
252 elog(ERROR, "cannot compare address families %d and %d",
253 ip_family(a1), ip_family(a2));
259 network_sup(inet *a1, inet *a2)
261 if (!PointerIsValid(a1) || !PointerIsValid(a2))
264 if ((ip_family(a1) == AF_INET) && (ip_family(a2) == AF_INET))
266 return ((ip_bits(a1) < ip_bits(a2))
267 && (v4bitncmp(ip_v4addr(a1), ip_v4addr(a2), ip_bits(a1)) == 0));
271 /* Go for an IPV6 address here, before faulting out: */
272 elog(ERROR, "cannot compare address families %d and %d",
273 ip_family(a1), ip_family(a2));
279 network_supeq(inet *a1, inet *a2)
281 if (!PointerIsValid(a1) || !PointerIsValid(a2))
284 if ((ip_family(a1) == AF_INET) && (ip_family(a2) == AF_INET))
286 return ((ip_bits(a1) <= ip_bits(a2))
287 && (v4bitncmp(ip_v4addr(a1), ip_v4addr(a2), ip_bits(a1)) == 0));
291 /* Go for an IPV6 address here, before faulting out: */
292 elog(ERROR, "cannot compare address families %d and %d",
293 ip_family(a1), ip_family(a2));
299 * Comparison function for sorting. Add V4/V6 testing!
303 network_cmp(inet *a1, inet *a2)
305 if (ntohl(ip_v4addr(a1)) < ntohl(ip_v4addr(a2)))
308 if (ntohl(ip_v4addr(a1)) > ntohl(ip_v4addr(a2)))
311 if (ip_bits(a1) < ip_bits(a2))
314 if (ip_bits(a1) > ip_bits(a2))
321 network_host(inet *ip)
326 tmp[sizeof("255.255.255.255/32")];
328 if (!PointerIsValid(ip))
332 elog(ERROR, "CIDR type has no host part");
334 if (ip_family(ip) == AF_INET)
336 /* It's an IP V4 address: */
337 if (inet_net_ntop(AF_INET, &ip_v4addr(ip), 32, tmp, sizeof(tmp)) == NULL)
338 elog(ERROR, "unable to print host (%s)", strerror(errno));
341 /* Go for an IPV6 address here, before faulting out: */
342 elog(ERROR, "unknown address family (%d)", ip_family(ip));
344 if ((ptr = strchr(tmp, '/')) != NULL)
346 len = VARHDRSZ + strlen(tmp) + 1;
349 elog(ERROR, "unable to allocate memory in network_host()");
352 strcpy(VARDATA(ret), tmp);
357 network_masklen(inet *ip)
359 if (!PointerIsValid(ip))
366 network_broadcast(inet *ip)
371 tmp[sizeof("255.255.255.255/32")];
373 if (!PointerIsValid(ip))
376 if (ip_family(ip) == AF_INET)
378 /* It's an IP V4 address: */
380 unsigned long mask = 0xffffffff;
382 if (ip_bits(ip) < 32)
383 mask >>= ip_bits(ip);
384 addr = htonl(ntohl(ip_v4addr(ip)) | mask);
386 if (inet_net_ntop(AF_INET, &addr, 32, tmp, sizeof(tmp)) == NULL)
387 elog(ERROR, "unable to print address (%s)", strerror(errno));
391 /* Go for an IPV6 address here, before faulting out: */
392 elog(ERROR, "unknown address family (%d)", ip_family(ip));
394 if ((ptr = strchr(tmp, '/')) != NULL)
396 len = VARHDRSZ + strlen(tmp) + 1;
399 elog(ERROR, "unable to allocate memory in network_broadcast()");
402 strcpy(VARDATA(ret), tmp);
407 network_network(inet *ip)
411 char tmp[sizeof("255.255.255.255/32")];
413 if (!PointerIsValid(ip))
416 if (ip_family(ip) == AF_INET)
418 /* It's an IP V4 address: */
419 int addr = htonl(ntohl(ip_v4addr(ip)) & (0xffffffff << (32 - ip_bits(ip))));
421 if (inet_cidr_ntop(AF_INET, &addr, ip_bits(ip), tmp, sizeof(tmp)) == NULL)
422 elog(ERROR, "unable to print network (%s)", strerror(errno));
426 /* Go for an IPV6 address here, before faulting out: */
427 elog(ERROR, "unknown address family (%d)", ip_family(ip));
429 len = VARHDRSZ + strlen(tmp) + 1;
432 elog(ERROR, "unable to allocate memory in network_network()");
435 strcpy(VARDATA(ret), tmp);
440 network_netmask(inet *ip)
445 tmp[sizeof("255.255.255.255/32")];
447 if (!PointerIsValid(ip))
450 if (ip_family(ip) == AF_INET)
452 /* It's an IP V4 address: */
453 int addr = htonl((-1 << (32 - ip_bits(ip))) & 0xffffffff);
455 if (inet_net_ntop(AF_INET, &addr, 32, tmp, sizeof(tmp)) == NULL)
456 elog(ERROR, "unable to print netmask (%s)", strerror(errno));
460 /* Go for an IPV6 address here, before faulting out: */
461 elog(ERROR, "unknown address family (%d)", ip_family(ip));
463 if ((ptr = strchr(tmp, '/')) != NULL)
465 len = VARHDRSZ + strlen(tmp) + 1;
468 elog(ERROR, "unable to allocate memory in network_netmask()");
471 strcpy(VARDATA(ret), tmp);
476 * Bitwise comparison for V4 addresses. Add V6 implementation!
480 v4bitncmp(unsigned int a1, unsigned int a2, int bits)
482 unsigned long mask = 0;
485 for (i = 0; i < bits; i++)
486 mask = (mask >> 1) | 0x80000000;
489 if ((a1 & mask) < (a2 & mask))
491 else if ((a1 & mask) > (a2 & mask))