perl and network addresses

Python python at venix.com
Mon Mar 27 15:23:01 EST 2006


On Mon, 2006-03-27 at 14:25 -0500, Paul Lussier wrote:
> Hi all,
> 
> I'm stumped.  I've got a network address space of 10.0.32/19.  How
> ever, this space is carved up using a /16 netmask.  In otherwords, the
> /19 netmask was simply used to *allocate* the space from 10.0.32.0 to
> 10.0.63.255, but we actually *use* the space with a /16 netmask (yes,
> this means that 10.0.8.10 is in the *same* physical network as
> 10.0.33.93!).
> 
> Given an address, say 10.0.33.189, I want to get the "network" and
> "host" portion of the address.  given that I'm really on a /16, it's
> fairly easy to split the IP at 10.0 and 33.189.  However, I want to be
> able to do the same thing on the /19 as well.  Once I have the network
> and host portions of the address, I need to increment the network
> portion by 1 block, then re-apply the host portion.
> 
> For example, given the address 10.0.33.189/16:
> 
>     Network = 10.0   Host = 33.189
>                + 1
>              -----
>               10.1 + $HOST = 10.1.33.189
Would it help to convert to 32-bit integers?  

When you add 1 here, you are really adding 2 ** 17.  Your host mask is 
	int('1' * 16, 2)
So newIP = oldIP + 2 ** 17
newhost = newIP & hostmask

> 
> And, given the address 10.0.33.189/19:

With a /19, you are adding 2**14
	hostmask = int('1' * 13, 2)

Converting a.b.c.d (base 256) to integer is left as an exercise for the
reader.  Perhaps you do not really care about the host mask.  You can
simply convert the newIP back to base-256 notation.

I think I understand the arithmetic.  I do not really understand what
you are trying to do.

> 
>     Network = 10.0.32   Host = 1.189
>                   + 1
>                 -----
>               10.0.64 + $HOST = 10.1.65.189
> 
> And, given the address 10.0.35.189/19:
>     Network = 10.0.32   Host = 3.189
>                   + 1
>                 -----
>               10.0.64 + $HOST = 10.1.67.189
> 
> Getting the "next" network block isn't too tough, Net::Netmask does
> that for me.  What I can't quite figure out is how to get the host
> portion, and then add it to the new network portion.  For a classful
> address it's fairly simple, for CIDR address, not so much...
> 
> All ideas are welcome and appreciated :)
> 
> Thanks,
-- 
Lloyd Kvam
Venix Corp




More information about the gnhlug-discuss mailing list