Re: [HACKERS] CIDR type and functions - Mailing list pgsql-hackers

From darcy@druid.net (D'Arcy J.M. Cain)
Subject Re: [HACKERS] CIDR type and functions
Date
Msg-id m0zRkRp-0000exC@druid.net
Whole thread Raw
In response to Re: [HACKERS] CIDR type and functions  (Bruce Momjian <maillist@candle.pha.pa.us>)
List pgsql-hackers
Thus spake Bruce Momjian
> > Here are the functions I had suggested.
> >
> >     netmask('192.3.4.5/24::cidr') == 255.255.255.0
> >     masklen('192.3.4.5/24::cidr') == 24
> >     host('192.3.4.5/24::cidr') == 192.3.4.5
> >     network('192.3.4.5/24::cidr') == 192.3.4.0

I forgot
        broadcast('192.3.4.5/24::cidr') == 192.3.4.255

> > and perhaps;
> >
> >     class('192.3.4.5/24::cidr') == C
> >     classnet('192.3.4.5/24::cidr') == 192.3.4

I'll leave these for the moment as I'm not sure what to do with invalid
classful addresses such as 192.3.4.5/16.  I'll bring it up again after
6.4 is released.

> > Can I help code up some of this stuff?
>
> Yes, we need those.  Code them up, and I will add them as standard
> types.

OK, I started but I could use a small change to inet_net_ntop.c which
I think impliments something we discussed anyway.  I just need to know
if this is going to affect anything else.  Basically it allows for
the number of bits to be -1 which is interpreted as a host with
unspecified netmask.  The changes cause all outputs to leave of
the netmask part if it is -1.  I realize that I will have to change
some of the other functions in inet.c but is there anything else
that might bite me?

If there is no problem I'll resubmit this to the patches list.

*** ../src.original/./backend/utils/adt/inet_net_ntop.c    Fri Oct  9 17:37:27 1998
--- ./backend/utils/adt/inet_net_ntop.c    Fri Oct  9 17:39:05 1998
***************
*** 85,90 ****
--- 85,97 ----
      char       *t;
      u_int        m;
      int            b;
+     int            print_bits = 1;
+
+     if (bits == -1)
+     {
+         bits = 32;
+         print_bits = 0;
+     }

      if (bits < 0 || bits > 32)
      {
***************
*** 129,137 ****
      }

      /* Format CIDR /width. */
!     if (size < sizeof "/32")
!         goto emsgsize;
!     dst += SPRINTF((dst, "/%u", bits));
      return (odst);

  emsgsize:
--- 136,147 ----
      }

      /* Format CIDR /width. */
!     if (print_bits)
!     {
!         if (size < sizeof "/32")
!             goto emsgsize;
!         dst += SPRINTF((dst, "/%u", bits));
!     }
      return (odst);

  emsgsize:


--
D'Arcy J.M. Cain <darcy@{druid|vex}.net>   |  Democracy is three wolves
http://www.druid.net/darcy/                |  and a sheep voting on
+1 416 424 2871     (DoD#0082)    (eNTP)   |  what's for dinner.

pgsql-hackers by date:

Previous
From: Bruce Momjian
Date:
Subject: Re: [HACKERS] dynamic libraries
Next
From: Bruce Momjian
Date:
Subject: Re: [HACKERS] PL compile warning messages