OSDN Git Service

New INET functions from D'Arcy J.M. Cain
authorBruce Momjian <bruce@momjian.us>
Mon, 12 Oct 1998 04:07:53 +0000 (04:07 +0000)
committerBruce Momjian <bruce@momjian.us>
Mon, 12 Oct 1998 04:07:53 +0000 (04:07 +0000)
src/backend/utils/adt/cash.c
src/backend/utils/adt/inet.c
src/include/catalog/pg_proc.h
src/include/utils/builtins.h
src/include/utils/cash.h

index 262d2d4..a6e9711 100644 (file)
@@ -9,7 +9,7 @@
  * workings can be found in the book "Software Solutions in C" by
  * Dale Schumacher, Academic Press, ISBN: 0-12-632360-7.
  *
- * $Header: /cvsroot/pgsql/src/backend/utils/adt/cash.c,v 1.26 1998/09/01 04:32:26 momjian Exp $
+ * $Header: /cvsroot/pgsql/src/backend/utils/adt/cash.c,v 1.27 1998/10/12 04:07:44 momjian Exp $
  */
 
 #include <stdio.h>
@@ -672,7 +672,7 @@ cashsmaller(Cash *c1, Cash *c2)
  * This converts a int4 as well but to a representation using words
  * Obviously way North American centric - sorry
  */
-const char *
+text *
 cash_words_out(Cash *value)
 {
        static char buf[128];
@@ -681,7 +681,8 @@ cash_words_out(Cash *value)
        Cash            m1;
        Cash            m2;
        Cash            m3;
-
+       text            *result;
+       
        /* work with positive numbers */
        if (*value < 0)
        {
@@ -718,8 +719,16 @@ cash_words_out(Cash *value)
        strcat(buf, (int) (*value / 100) == 1 ? " dollar and " : " dollars and ");
        strcat(buf, num_word(m0));
        strcat(buf, m0 == 1 ? " cent" : " cents");
+
+       /* capitalize output */
        *buf = toupper(*buf);
-       return buf;
+
+       /* make a text type for output */
+       result = (text *) palloc(strlen(buf) + VARHDRSZ);
+       VARSIZE(result) = strlen(buf) + VARHDRSZ;
+       StrNCpy(VARDATA(result), buf, strlen(buf));
+
+       return result;
 }      /* cash_words_out() */
 
 
index f98b36f..23b9158 100644 (file)
@@ -3,7 +3,7 @@
  *     is for IP V4 CIDR notation, but prepared for V6: just
  *     add the necessary bits where the comments indicate.
  *
- *     $Id: inet.c,v 1.2 1998/10/08 02:08:44 momjian Exp $
+ *     $Id: inet.c,v 1.3 1998/10/12 04:07:46 momjian Exp $
  */
 
 #include <sys/types.h>
@@ -57,7 +57,9 @@ inet_in(char *src)
        }
        /* First, try for an IP V4 address: */
        ip_family(dst) = AF_INET;
-       bits = inet_net_pton(ip_family(dst), src, &ip_v4addr(dst), ip_addrsize(dst));
+#ifdef BAD
+       bits = inet_net_pton(ip_family(dst), src, &ip_v4addr(dst), ip_addrsize(dst), NULL);
+#endif
        if ((bits < 0) || (bits > 32))
        {
                /* Go for an IPV6 address here, before faulting out: */
@@ -84,13 +86,15 @@ inet_out(inet *src)
 
        if (ip_family(src) == AF_INET)
        {
+#ifdef BAD
                /* It's an IP V4 address: */
-               if (inet_net_ntop(AF_INET, &ip_v4addr(src), ip_bits(src),
+               if (inet_net_ntop(AF_INET, &ip_v4addr(src), 4, ip_bits(src),
                                                  tmp, sizeof(tmp)) < 0)
                {
                        elog(ERROR, "unable to print address (%s)", strerror(errno));
                        return (NULL);
                }
+#endif
        }
        else
        {
@@ -265,6 +269,194 @@ inet_cmp(inet *a1, inet *a2)
        return 0;
 }
 
+text *
+inet_netmask(inet *ip)
+{
+       char       *dst,
+                               tmp[sizeof("255.255.255.255/32")];
+
+       if (ip_family(ip) == AF_INET)
+       {
+               /* It's an IP V4 address: */
+               int addr = -1 << (32 - ip_bits(ip));
+
+               /* a little wasteful by why reinvent the wheel? */
+#ifdef BAD
+               if (inet_cidr_ntop(AF_INET, &addr, 4, -1, tmp, sizeof(tmp)) < 0)
+               {
+                       elog(ERROR, "unable to print netmask (%s)", strerror(errno));
+                       return (NULL);
+               }
+#endif
+       }
+       else
+       {
+               /* Go for an IPV6 address here, before faulting out: */
+               elog(ERROR, "unknown address family (%d)", ip_family(ip));
+               return (NULL);
+       }
+       dst = palloc(strlen(tmp) + 1);
+       if (dst == NULL)
+       {
+               elog(ERROR, "unable to allocate memory in inet_netmask()");
+               return (NULL);
+       }
+       strcpy(dst, tmp);
+       return (dst);
+       
+}
+
+int4
+inet_masklen(inet *ip)
+{
+       return ip_bits(ip);
+}
+
+text *
+inet_host(inet *ip)
+{
+       char       *dst,
+                               tmp[sizeof("255.255.255.255/32")];
+
+       if (ip_family(ip) == AF_INET)
+       {
+#ifdef BAD
+               /* It's an IP V4 address: */
+               if (inet_cidr_ntop(AF_INET, &ip_v4addr(ip), 4, -1,
+                                                 tmp, sizeof(tmp)) < 0)
+               {
+                       elog(ERROR, "unable to print host (%s)", strerror(errno));
+                       return (NULL);
+               }
+#endif
+       }
+       else
+       {
+               /* Go for an IPV6 address here, before faulting out: */
+               elog(ERROR, "unknown address family (%d)", ip_family(ip));
+               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);
+       
+}
+
+text *
+inet_network_without_bits(inet *ip)
+{
+       char       *dst,
+                               tmp[sizeof("255.255.255.255/32")];
+
+       if (ip_family(ip) == AF_INET)
+       {
+               /* It's an IP V4 address: */
+               int addr = ip_v4addr(ip) & (-1 << (32 - ip_bits(ip)));
+#ifdef BAD
+
+               if (inet_cidr_ntop(AF_INET, &addr, (int)(ip_bits(ip)/8), -1,
+                                                 tmp, sizeof(tmp)) < 0)
+               {
+                       elog(ERROR, "unable to print address (%s)", strerror(errno));
+                       return (NULL);
+               }
+#endif
+       }
+       else
+       {
+               /* Go for an IPV6 address here, before faulting out: */
+               elog(ERROR, "unknown address family (%d)", ip_family(ip));
+               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);
+       
+}
+
+text *
+inet_network_with_bits(inet *ip)
+{
+       char       *dst,
+                               tmp[sizeof("255.255.255.255/32")];
+
+       if (ip_family(ip) == AF_INET)
+       {
+               /* It's an IP V4 address: */
+               int addr = ip_v4addr(ip) & (-1 << (32 - ip_bits(ip)));
+#ifdef BAD
+
+               if (inet_cidr_ntop(AF_INET, &addr, (int)(ip_bits(ip)/8),
+                                                 ip_bits(ip), tmp, sizeof(tmp)) < 0)
+               {
+                       elog(ERROR, "unable to print address (%s)", strerror(errno));
+                       return (NULL);
+               }
+#endif
+       }
+       else
+       {
+               /* Go for an IPV6 address here, before faulting out: */
+               elog(ERROR, "unknown address family (%d)", ip_family(ip));
+               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);
+       
+}
+
+text *
+inet_broadcast(inet *ip)
+{
+       char       *dst,
+                               tmp[sizeof("255.255.255.255/32")];
+
+       if (ip_family(ip) == AF_INET)
+       {
+               /* It's an IP V4 address: */
+               int addr = ip_v4addr(ip) | ~(-1 << (32 - ip_bits(ip)));
+#ifdef BAD
+
+               if (inet_cidr_ntop(AF_INET,&addr,4,ip_bits(ip),tmp,sizeof(tmp)) < 0)
+               {
+                       elog(ERROR, "unable to print address (%s)", strerror(errno));
+                       return (NULL);
+               }
+#endif
+       }
+       else
+       {
+               /* Go for an IPV6 address here, before faulting out: */
+               elog(ERROR, "unknown address family (%d)", ip_family(ip));
+               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);
+       
+}
+
 /*
  *     Bitwise comparison for V4 addresses.  Add V6 implementation!
  */
@@ -285,3 +477,4 @@ v4bitncmp(unsigned int a1, unsigned int a2, int bits)
                return (1);
        return (0);
 }
+
index 969470a..f454342 100644 (file)
@@ -6,7 +6,7 @@
  *
  * Copyright (c) 1994, Regents of the University of California
  *
- * $Id: pg_proc.h,v 1.72 1998/10/08 00:19:40 momjian Exp $
+ * $Id: pg_proc.h,v 1.73 1998/10/12 04:07:48 momjian Exp $
  *
  * NOTES
  *       The script catalog/genbki.sh reads this file and generates .bki
@@ -2097,6 +2097,19 @@ DESCR("is-supernet");
 DATA(insert OID = 930 (  inet_supeq       PGUID 11 f t f 2 f 16 "869 869" 100 0 0 100  foo bar ));
 DESCR("is-supernet-or-equal");
 
+DATA(insert OID = 940 (  inet_netmask          PGUID 11 f t f 1 f 25 "869" 100 0 0 100  foo bar ));
+DESCR("netmask of inet address");
+DATA(insert OID = 941 (  inet_masklen          PGUID 11 f t f 1 f 23 "869" 100 0 0 100  foo bar ));
+DESCR("netmask length");
+DATA(insert OID = 942 (  inet_host             PGUID 11 f t f 1 f 25 "869" 100 0 0 100  foo bar ));
+DESCR("host adress");
+DATA(insert OID = 943 (  inet_network_without_bits             PGUID 11 f t f 1 f 25 "869" 100 0 0 100  foo bar ));
+DESCR("netmask without bits");
+DATA(insert OID = 944 (  inet_network_with_bits                PGUID 11 f t f 1 f 25 "869" 100 0 0 100  foo bar ));
+DESCR("netmask with bits");
+DATA(insert OID = 945 (  inet_broadcast                PGUID 11 f t f 1 f 25 "869" 100 0 0 100  foo bar ));
+DESCR("broadcast address");
+
 
 /*
  * prototypes for functions pg_proc.c
index f3c57d4..49b849d 100644 (file)
@@ -6,7 +6,7 @@
  *
  * Copyright (c) 1994, Regents of the University of California
  *
- * $Id: builtins.h,v 1.60 1998/10/08 18:30:49 momjian Exp $
+ * $Id: builtins.h,v 1.61 1998/10/12 04:07:51 momjian Exp $
  *
  * NOTES
  *       This should normally only be included by fmgr.h.
@@ -515,6 +515,9 @@ char *inet_net_ntop(int af, const void *src, int bits, char *dst, size_t size);
 /* inet_net_pton.c */
 int inet_net_pton(int af, const char *src, void *dst, size_t size);
 
+char *inet_cidr_ntop(int af, const void *src, size_t len, int bits, char *dst, size_t size);
+int inet_cidr_pton(int af, const void *src, void *dst, size_t size, int *used);
+
 /* inet.c */
 inet      *inet_in(char *str);
 char      *inet_out(inet * addr);
@@ -530,6 +533,13 @@ bool               inet_sup(inet * a1, inet * a2);
 bool           inet_supeq(inet * a1, inet * a2);
 int4           inet_cmp(inet * a1, inet * a2);
 
+text      *inet_netmask(inet * addr);
+int4           inet_masklen(inet * addr);
+text      *inet_host(inet * addr);
+text      *inet_network_without_bits(inet * addr);
+text      *inet_network_with_bits(inet * addr);
+text      *inet_broadcast(inet * addr);
+
 
 /* mac.c */
 macaddr    *macaddr_in(char *str);
index b5e45fc..0530266 100644 (file)
@@ -44,6 +44,6 @@ extern Cash *int2_mul_cash(int2 s, Cash *c);
 extern Cash *cashlarger(Cash *c1, Cash *c2);
 extern Cash *cashsmaller(Cash *c1, Cash *c2);
 
-extern const char *cash_words_out(Cash *value);
+extern text *cash_words_out(Cash *value);
 
 #endif  /* CASH_H */