perl and network addresses

Stephen Ryan stephen.p.ryan at Dartmouth.EDU
Fri Mar 31 10:16:01 EST 2006


On Thu, 2006-03-30 at 20:35 -0500, Jason Stephenson wrote:
> Paul Lussier wrote:
> 
> > Yes, more or less.  Between you and Jason I've been able to come up
> > with exactly what I need.  Thanks a lot for all your help.  Why I
> > couldn't see this for myself is beyond me.  Of course, this week has
> > been full of me "missing the details" to the point where I somehow
> > managed to mail my taxes to myself from work the other day rather than
> > to my accountant :) So, just in case you wondered, the USPS system is
> > working at peak efficiency !
> 
> You're very welcome to the help, and we all have those weeks. It took me 
> a while to realize what your real question was.
> 
> Once I figured out your question, it was actually rather  interesting: 
> adding network addresses to interpolate between different networks. 
> Trying to answer it allowed me to discover some facts about IPv4 
> addresses and masks, so I got to learn something, too.
> 
> The thing that I found most interesting is if you use the one or two 
> digit kind of "mask," i.e. /19, you can determine how many addresses are 
> on the network via the following C code: addresses = 1 << (32 - n). 
> Where n is the part of the mask after the /.
> 
> I wish I could find a faster way to blit the bits to make the "real" 
> mask from the /N style than using a for loop. Only alternative I can 
> think of is to use a switch on the 33 possibilities (0-32).--Of course, 
> anything < /8 and > /30 doesn't make a "real" network.
> 
> Can anyone think of a better way to blit an arbitrary number of bits 
> from 0 to 1?

Well, let's see....

Taking advantage of the fact that all of the '1' bits are at the end of
the hostmask, you've actually almost gotten it already.

hostmask = (1 << (32 - n)) - 1
netmask = ~ hostmask

1 << (32 - n) in binary is (n-1) '0' bits, a '1', then (32 - n) '0'
bits.  Subtracting 1 from that gives n '0' bits followed by (32 - n) '1'
bits.  The 'not' operator flips all the bits for the netmask.

This works for /1 through /32 networks, even though some of those are
nonsensical.  A /0 might break this because of overflow (1 << (32 -n)
overflows a 32-bit integer); theoretically, it should work even for /0
so long as 1 << (32-n) returns 0 (32-bit gcc 4.0 on my Athlon64 desktop
computes this correctly, but complains 'warning: left shift count >=
width of type' while compiling.  Anyway, if you're running a /0, you've
got other, bigger problems.

> Now, I'm working on a network calculator application that will support 
> IPv6 as well. I should probably do it in JavaScript, uh, sorry, AJAX, so 
> that the "Web 2.0" people will notice. ;)
> 
> Cheers,
> Jason
> _______________________________________________
> gnhlug-discuss mailing list
> gnhlug-discuss at mail.gnhlug.org
> http://mail.gnhlug.org/mailman/listinfo/gnhlug-discuss
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test.c
Type: text/x-csrc
Size: 259 bytes
Desc: not available
Url : http://mail.gnhlug.org/mailman/private/gnhlug-discuss/attachments/20060331/b25c829a/test.bin


More information about the gnhlug-discuss mailing list