[Mimedefang] Stupid question? - subnet

jmiller at purifieddata.net jmiller at purifieddata.net
Tue Feb 11 18:27:01 EST 2003


On Tue, 11 Feb 2003, Richard Cooper wrote:
>
> > On Mon, 10 Feb 2003, David F. Skoll wrote:
> >
> > > On Mon, 10 Feb 2003, Jim McCullars wrote:
> > >
> > > >    It's a string comparison, so you may be looking at a
> > fairly complicated
> > > > regex to do the test you want (I say "may be" because I
> > don't know how to
> > > > decode a CIDR address string to a range of addresses).
> > >
> > > You might want to look at a Perl module to do CIDR
> > comparisons; I think
> > > Net::CIDR::Lite might be just the ticket:
> > >
> > > http://search.cpan.org/author/DOUGW/Net-CIDR-Lite-0.14/Lite.pm
> > >
> > > Or perhaps NetAddr::IP is more to your liking:
> > >
> > > http://search.cpan.org/author/LUISMUNOZ/NetAddr-IP-3.14/IP.pm
> > >
> > > Or Net::CIDR:
> > >
> > > http://search.cpan.org/author/MRSAM/Net-CIDR-0.04/CIDR.pm
> > >
> > > What was that horrible Perl slogan again?  "TIMTOWTDI"?
> > >
> >
>
> Thanks for these - I'm having some problems installing Perl modules
> though so I'm just using Josh's code. I'm getting an error on this line
> though in the ip2bin sub -
>
> > 			my $bin = &dec2bin($_);
>

Sorry... I was hoping it wouldn't miss any of those. Here's dec2bin:
sub dec2bin {
    my $str = unpack("B32", pack("N", shift));
    return $str;
}

> because there is no dec2bin sub. Is there something I'm missing (or
> something missing from the code) or is this something I can remove? I
> hate to admit I can't follow it 100% but I can't :). This is in the sub
> ip2bin - to get ip to dec it goes ip to bin then bin to dec - is there
> any reason why it needs a dec to bin sub?

Well, that code does. I mainly did it because I'm not too great with
binary math stuff, so I go from ip (4 8bit unsigned ints) to it's binary
representation to the decimal of that 32bit binary. With the
decimals, it's easy to understand an IP being less than or greater than
some other and stuff (and also allowed me to store IP's in the database as
INT's, and select based on ranges of decimals).
So, it could probably be worked around by using just dqtobin and bintodq
and some binary calculations, but if you just stick the above dec2bin in
your code, it should work.

--
Josh I.




More information about the MIMEDefang mailing list