amt has asked for the wisdom of the Perl Monks concerning the following question:

Gentlemen,

I am using NetAddr::IP to break down a range of addresses given, the base address, the stop address, and the subnet mask, in bit format.
The problem that I am having is with getting the address that is 1 higher than the broadcast address for the previous subnet work. Below you will see code, with comments inline.
sub subnet_recurse { my $netaddr_ref = shift; my $endaddr_ref = shift; my $new_addmask;

The parameters are references to NetAddr::IP objects
print "<hr><b>base: " . $$netaddr_ref->network() "</b><br>"; my $hostnum = 0; foreach ($$netaddr_ref->hostenum()){ print "add$hostnum: " . $_ ."<br>"; $hostnum++; } print "<b>brdc:".$$netaddr_ref->broadcast()."</b><br><hr>";

Print out the stuff that is given to me from the objects.
my $net_broadcast = NetAddr::IP->new($$netaddr_ref->broadcast()->addr());

Create new NetAddr::IP ojbect with the broadcast ip address
my $new_netaddr=NetAddr::IP->new($net_broadcast->addr())+1;

Use overloaded "+" as defined by the specification
print "<b>brd: " . $net_broadcast->addr() ."</b><br>"; #print "<b>new: " . $new_netaddr->addr() . "</b><br>"; #if( $new_netaddr->within($$endaddr_ref) ){ # subnet_recurse( \$new_netaddr, $endaddr_ref ); #} else { return; } return; }


The resulting output is as follows:
host: 192.168.1.0/29 mask: 255.255.255.248 base: 192.168.1.0/29 add0: 192.168.1.1/32 add1: 192.168.1.2/32 add2: 192.168.1.3/32 add3: 192.168.1.4/32 add4: 192.168.1.5/32 add5: 192.168.1.6/32 brdc: 192.168.1.7/29 192.168.1.7/32brd: 192.168.1.7
This has held me up all day, and is starting to become frustrating. Thanks for any help in advance.
amt

Replies are listed 'Best First'.
Re: NetAddr::IP Automated Subnet Allocation
by ikegami (Patriarch) on Sep 02, 2004 at 22:49 UTC

    Like the documentation indicates says + and ++ will wrap around within the subnet, so new_netaddr won't hold what you want:

    (127.0.0.255/24)++ -> (127.0.0.0/24) (127.0.0.255/24)+1 -> (127.0.0.0/24)

    How about: (Tested)

    my ($addr, $mask) = $net_broadcast->numeric(); my $new_netaddr = NetAddr::IP->new($addr+1, $mask);

    I haven't looked at why you have /32's instead of /29's in your output.

      Your solution works, but I need to add 1 to the address of the broadcast address, so I can begin the next sub-network. Your solution returns the numberic() of the current address field, which is the base network address.
      amt
        Your solution returns the numberic() of the current address field, which is the base network address.

        Not so. Did you type in $net_addr where you should have typed $net_broadcast?

        use NetAddr::IP (); my $net_addr = NetAddr::IP->new('192.168.1.0/29'); my $net_broadcast = $net_addr->broadcast(); my $net_newaddr; { my ($addr, $mask) = $net_broadcast->numeric(); $net_newaddr = NetAddr::IP->new($addr+1, $mask); } print("$net_addr\n"); # prints 192.168.1.0/29 print("$net_broadcast\n"); # prints 192.168.1.7/29 print("$net_newaddr\n"); # prints 192.168.1.8/29
Re: NetAddr::IP Automated Subnet Allocation
by NetWallah (Canon) on Sep 03, 2004 at 01:48 UTC
    Bravo !     ikegami's code works (++). Here is my minor variation:
    my $IP2 = NetAddr::IP->new($IP1->broadcast->numeric()+1 , 24); # Usin +g 24-bit mask...
    What I'm curious about is why you pass Object REFEREENCES around, causing awkward syntax like
    $$netaddr_ref->hostenum())

    Why not pass the object itself (Which happens to be a reference already) ?

        Earth first! (We'll rob the other planets later)

      i was passing references because of a previous misconception mixed with laziness. haha. thanks for the suggestions you two, it has already made my otherwise miserable hungover day, just a little bit better. like a ray of sunshine cutting through my drunken stupor, but I digress.
      amt