]> granicus.if.org Git - postgresql/blobdiff - src/backend/utils/adt/network.c
Move some system includes into c.h, and remove duplicates.
[postgresql] / src / backend / utils / adt / network.c
index 4e09af623d5425a1be8bbf13bec246821d6ed619..92580cfe965fc77fea35637a7977ee344e064f0d 100644 (file)
@@ -3,24 +3,20 @@
  *     is for IP V4 CIDR notation, but prepared for V6: just
  *     add the necessary bits where the comments indicate.
  *
- *     $Id: network.c,v 1.2 1998/10/26 01:03:24 tgl Exp $
+ *     $Id: network.c,v 1.15 1999/07/17 20:17:58 momjian Exp $
  *     Jon Postel RIP 16 Oct 1998
  */
 
 #include <sys/types.h>
 #include <sys/socket.h>
 
-#include <stdio.h>
-#include <string.h>
 #include <errno.h>
 
 #include <netinet/in.h>
 #include <arpa/inet.h>
 
-#include <postgres.h>
-#include <utils/palloc.h>
-#include <utils/builtins.h>
-#include <utils/inet.h>
+#include "postgres.h"
+#include "utils/builtins.h"
 
 static int     v4bitncmp(unsigned int a1, unsigned int a2, int bits);
 
@@ -50,23 +46,21 @@ network_in(char *src, int type)
        int                     bits;
        inet       *dst;
 
+       if (!src)
+               return NULL;
+
        dst = palloc(VARHDRSZ + sizeof(inet_struct));
        if (dst == NULL)
-       {
                elog(ERROR, "unable to allocate memory in network_in()");
-               return NULL;
-       }
+
        /* 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);
+                                                type ? ip_addrsize(dst) : -1);
        if ((bits < 0) || (bits > 32))
-       {
                /* Go for an IPV6 address here, before faulting out: */
                elog(ERROR, "could not parse \"%s\"", src);
-               pfree(dst);
-               return NULL;
-       }
+
        VARSIZE(dst) = VARHDRSZ
                + ((char *) &ip_v4addr(dst) - (char *) VARDATA(dst))
                + ip_addrsize(dst);
@@ -104,29 +98,22 @@ inet_out(inet *src)
                /* It's an IP V4 address: */
                if (ip_type(src))
                        dst = inet_cidr_ntop(AF_INET, &ip_v4addr(src), ip_bits(src),
-                                                 tmp, sizeof(tmp));
+                                                                tmp, sizeof(tmp));
                else
                        dst = inet_net_ntop(AF_INET, &ip_v4addr(src), ip_bits(src),
-                                                 tmp, sizeof(tmp));
+                                                               tmp, sizeof(tmp));
 
                if (dst == NULL)
-               {
                        elog(ERROR, "unable to print address (%s)", strerror(errno));
-                       return (NULL);
-               }
        }
        else
-       {
                /* Go for an IPV6 address here, before faulting out: */
                elog(ERROR, "unknown address family (%d)", ip_family(src));
-               return NULL;
-       }
+
        dst = palloc(strlen(tmp) + 1);
        if (dst == NULL)
-       {
                elog(ERROR, "unable to allocate memory in inet_out()");
-               return NULL;
-       }
+
        strcpy(dst, tmp);
        return dst;
 }
@@ -146,6 +133,8 @@ cidr_out(inet *src)
 bool
 network_lt(inet *a1, inet *a2)
 {
+       if (!PointerIsValid(a1) || !PointerIsValid(a2))
+               return FALSE;
        if ((ip_family(a1) == AF_INET) && (ip_family(a2) == AF_INET))
        {
                int                     order = v4bitncmp(ip_v4addr(a1), ip_v4addr(a2), ip_bits(a2));
@@ -157,19 +146,23 @@ network_lt(inet *a1, inet *a2)
                /* 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 (FALSE);
+               return FALSE;
        }
 }
 
 bool
 network_le(inet *a1, inet *a2)
 {
+       if (!PointerIsValid(a1) || !PointerIsValid(a2))
+               return FALSE;
        return (network_lt(a1, a2) || network_eq(a1, a2));
 }
 
 bool
 network_eq(inet *a1, inet *a2)
 {
+       if (!PointerIsValid(a1) || !PointerIsValid(a2))
+               return FALSE;
        if ((ip_family(a1) == AF_INET) && (ip_family(a2) == AF_INET))
        {
                return ((ip_bits(a1) == ip_bits(a2))
@@ -180,19 +173,23 @@ network_eq(inet *a1, inet *a2)
                /* 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 (FALSE);
+               return FALSE;
        }
 }
 
 bool
 network_ge(inet *a1, inet *a2)
 {
+       if (!PointerIsValid(a1) || !PointerIsValid(a2))
+               return FALSE;
        return (network_gt(a1, a2) || network_eq(a1, a2));
 }
 
 bool
 network_gt(inet *a1, inet *a2)
 {
+       if (!PointerIsValid(a1) || !PointerIsValid(a2))
+               return FALSE;
        if ((ip_family(a1) == AF_INET) && (ip_family(a2) == AF_INET))
        {
                int                     order = v4bitncmp(ip_v4addr(a1), ip_v4addr(a2), ip_bits(a2));
@@ -204,19 +201,24 @@ network_gt(inet *a1, inet *a2)
                /* 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 (FALSE);
+               return FALSE;
        }
 }
 
 bool
 network_ne(inet *a1, inet *a2)
 {
+       if (!PointerIsValid(a1) || !PointerIsValid(a2))
+               return FALSE;
        return (!network_eq(a1, a2));
 }
 
 bool
 network_sub(inet *a1, inet *a2)
 {
+       if (!PointerIsValid(a1) || !PointerIsValid(a2))
+               return FALSE;
+
        if ((ip_family(a1) == AF_INET) && (ip_family(a2) == AF_INET))
        {
                return ((ip_bits(a1) > ip_bits(a2))
@@ -227,13 +229,16 @@ network_sub(inet *a1, inet *a2)
                /* 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 (FALSE);
+               return FALSE;
        }
 }
 
 bool
 network_subeq(inet *a1, inet *a2)
 {
+       if (!PointerIsValid(a1) || !PointerIsValid(a2))
+               return FALSE;
+
        if ((ip_family(a1) == AF_INET) && (ip_family(a2) == AF_INET))
        {
                return ((ip_bits(a1) >= ip_bits(a2))
@@ -244,13 +249,16 @@ network_subeq(inet *a1, inet *a2)
                /* 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 (FALSE);
+               return FALSE;
        }
 }
 
 bool
 network_sup(inet *a1, inet *a2)
 {
+       if (!PointerIsValid(a1) || !PointerIsValid(a2))
+               return FALSE;
+
        if ((ip_family(a1) == AF_INET) && (ip_family(a2) == AF_INET))
        {
                return ((ip_bits(a1) < ip_bits(a2))
@@ -261,13 +269,16 @@ network_sup(inet *a1, inet *a2)
                /* 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 (FALSE);
+               return FALSE;
        }
 }
 
 bool
 network_supeq(inet *a1, inet *a2)
 {
+       if (!PointerIsValid(a1) || !PointerIsValid(a2))
+               return FALSE;
+
        if ((ip_family(a1) == AF_INET) && (ip_family(a2) == AF_INET))
        {
                return ((ip_bits(a1) <= ip_bits(a2))
@@ -278,7 +289,7 @@ network_supeq(inet *a1, inet *a2)
                /* 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 (FALSE);
+               return FALSE;
        }
 }
 
@@ -291,8 +302,16 @@ network_cmp(inet *a1, inet *a2)
 {
        if (ntohl(ip_v4addr(a1)) < ntohl(ip_v4addr(a2)))
                return (-1);
-       else if (ntohl(ip_v4addr(a1)) > ntohl(ip_v4addr(a2)))
+
+       if (ntohl(ip_v4addr(a1)) > ntohl(ip_v4addr(a2)))
+               return (1);
+
+       if (ip_bits(a1) < ip_bits(a2))
+               return (-1);
+
+       if (ip_bits(a1) > ip_bits(a2))
                return (1);
+
        return 0;
 }
 
@@ -304,36 +323,29 @@ network_host(inet *ip)
        char       *ptr,
                                tmp[sizeof("255.255.255.255/32")];
 
+       if (!PointerIsValid(ip))
+               return NULL;
+
        if (ip_type(ip))
-       {
                elog(ERROR, "CIDR type has no host part");
-               return NULL;
-       }
 
        if (ip_family(ip) == AF_INET)
        {
                /* It's an IP V4 address: */
                if (inet_net_ntop(AF_INET, &ip_v4addr(ip), 32, tmp, sizeof(tmp)) == NULL)
-               {
                        elog(ERROR, "unable to print host (%s)", strerror(errno));
-                       return (NULL);
-               }
        }
        else
-       {
                /* Go for an IPV6 address here, before faulting out: */
                elog(ERROR, "unknown address family (%d)", ip_family(ip));
-               return (NULL);
-       }
+
        if ((ptr = strchr(tmp, '/')) != NULL)
                *ptr = 0;
        len = VARHDRSZ + strlen(tmp) + 1;
        ret = palloc(len);
        if (ret == NULL)
-       {
                elog(ERROR, "unable to allocate memory in network_host()");
-               return (NULL);
-       }
+
        VARSIZE(ret) = len;
        strcpy(VARDATA(ret), tmp);
        return (ret);
@@ -342,6 +354,9 @@ network_host(inet *ip)
 int4
 network_masklen(inet *ip)
 {
+       if (!PointerIsValid(ip))
+               return 0;
+
        return ip_bits(ip);
 }
 
@@ -353,32 +368,34 @@ network_broadcast(inet *ip)
        char       *ptr,
                                tmp[sizeof("255.255.255.255/32")];
 
+       if (!PointerIsValid(ip))
+               return NULL;
+
        if (ip_family(ip) == AF_INET)
        {
                /* It's an IP V4 address: */
-               int     addr = htonl(ntohl(ip_v4addr(ip)) | (0xffffffff >> ip_bits(ip)));
+               int                     addr;
+               unsigned long mask = 0xffffffff;
+
+               if (ip_bits(ip) < 32)
+                       mask >>= ip_bits(ip);
+               addr = htonl(ntohl(ip_v4addr(ip)) | mask);
 
                if (inet_net_ntop(AF_INET, &addr, 32, tmp, sizeof(tmp)) == NULL)
-               {
                        elog(ERROR, "unable to print address (%s)", strerror(errno));
-                       return (NULL);
-               }
+
        }
        else
-       {
                /* Go for an IPV6 address here, before faulting out: */
                elog(ERROR, "unknown address family (%d)", ip_family(ip));
-               return (NULL);
-       }
+
        if ((ptr = strchr(tmp, '/')) != NULL)
                *ptr = 0;
        len = VARHDRSZ + strlen(tmp) + 1;
        ret = palloc(len);
        if (ret == NULL)
-       {
                elog(ERROR, "unable to allocate memory in network_broadcast()");
-               return (NULL);
-       }
+
        VARSIZE(ret) = len;
        strcpy(VARDATA(ret), tmp);
        return (ret);
@@ -389,35 +406,29 @@ network_network(inet *ip)
 {
        text       *ret;
        int                     len;
-       char       *ptr,
-                               tmp[sizeof("255.255.255.255/32")];
+       char            tmp[sizeof("255.255.255.255/32")];
+
+       if (!PointerIsValid(ip))
+               return NULL;
 
        if (ip_family(ip) == AF_INET)
        {
                /* It's an IP V4 address: */
-               int     addr = ntohl(ip_v4addr(ip)) & (0xffffffff << (32 - ip_bits(ip)));
+               int                     addr = htonl(ntohl(ip_v4addr(ip)) & (0xffffffff << (32 - ip_bits(ip))));
 
-               if (inet_cidr_ntop(AF_INET, &addr, 32, tmp, sizeof(tmp)) == NULL)
-               {
+               if (inet_cidr_ntop(AF_INET, &addr, ip_bits(ip), tmp, sizeof(tmp)) == NULL)
                        elog(ERROR, "unable to print network (%s)", strerror(errno));
-                       return (NULL);
-               }
+
        }
        else
-       {
                /* Go for an IPV6 address here, before faulting out: */
                elog(ERROR, "unknown address family (%d)", ip_family(ip));
-               return (NULL);
-       }
-       if ((ptr = strchr(tmp, '/')) != NULL)
-               *ptr = 0;
+
        len = VARHDRSZ + strlen(tmp) + 1;
        ret = palloc(len);
        if (ret == NULL)
-       {
                elog(ERROR, "unable to allocate memory in network_network()");
-               return (NULL);
-       }
+
        VARSIZE(ret) = len;
        strcpy(VARDATA(ret), tmp);
        return (ret);
@@ -431,32 +442,29 @@ network_netmask(inet *ip)
        char       *ptr,
                                tmp[sizeof("255.255.255.255/32")];
 
+       if (!PointerIsValid(ip))
+               return NULL;
+
        if (ip_family(ip) == AF_INET)
        {
                /* It's an IP V4 address: */
                int                     addr = htonl((-1 << (32 - ip_bits(ip))) & 0xffffffff);
 
                if (inet_net_ntop(AF_INET, &addr, 32, tmp, sizeof(tmp)) == NULL)
-               {
                        elog(ERROR, "unable to print netmask (%s)", strerror(errno));
-                       return (NULL);
-               }
+
        }
        else
-       {
                /* Go for an IPV6 address here, before faulting out: */
                elog(ERROR, "unknown address family (%d)", ip_family(ip));
-               return (NULL);
-       }
+
        if ((ptr = strchr(tmp, '/')) != NULL)
                *ptr = 0;
        len = VARHDRSZ + strlen(tmp) + 1;
        ret = palloc(len);
        if (ret == NULL)
-       {
                elog(ERROR, "unable to allocate memory in network_netmask()");
-               return (NULL);
-       }
+
        VARSIZE(ret) = len;
        strcpy(VARDATA(ret), tmp);
        return (ret);