subnets eating each other?

Discussion in 'Perl Misc' started by Matija Papec, Nov 28, 2003.

  1. Matija Papec

    Matija Papec Guest

    I would like to discover situations when one ip/mask is able to cover
    another one, eg. does 22.122.0.0/16 cover 122.0.0.0/24?
    I made integer conversion for ip/mask but don't know what to do further..


    my $r1 = {
    source => '22.122.0.0/16',
    };
    my $r2 = {
    source => '122.0.0.0/24',
    };
    for my $r ($r1, $r2) {
    $r->{_source} = CalcIP($r->{source}) if !$r->{_source};
    }

    use Data::Dumper; print Dumper $r1, $r2;

    sub CalcIP {
    #
    # calculate ip/mask
    #
    my ($source) = @_;
    my %adr;

    if ($source) {
    my ($ip, $mask) = split /\//, $source;
    $ip = unpack 'N', pack 'C4', split /\./, $ip;

    if (!$mask) { $mask = 0xffffffff }
    elsif ($mask =~ /^\d+$/) { $mask = 0xffffffff << (32-$mask) }
    else { $mask = unpack 'N', pack 'C4', split /\./, $mask }

    $ip = $ip & $mask;
    %adr = ( ip => $ip, mask => $mask );
    }
    return \%adr;
    }


    --
    Matija
    Matija Papec, Nov 28, 2003
    #1
    1. Advertising

  2. On Fri, 28 Nov 2003 19:19:19 +0100
    Matija Papec <> wrote:

    >
    > I would like to discover situations when one ip/mask is able to
    > cover another one, eg. does 22.122.0.0/16 cover 122.0.0.0/24?
    > I made integer conversion for ip/mask but don't know what to do
    > further..


    *If* you wanted to use a module, you could use the Net::Netmask module
    (http://search.cpan.org/~muir/Net-Netmask-1.9004/Netmask.pod)

    HTH

    --
    Jim

    Copyright notice: all code written by the author in this post is
    released under the GPL. http://www.gnu.org/licenses/gpl.txt
    for more information.

    a fortune quote ...
    Confidence is the feeling you have before you understand the
    situation.
    James Willmore, Nov 28, 2003
    #2
    1. Advertising

  3. Matija Papec

    Matija Papec Guest

    X-Ftn-To: James Willmore

    James Willmore <> wrote:
    >> I would like to discover situations when one ip/mask is able to
    >> cover another one, eg. does 22.122.0.0/16 cover 122.0.0.0/24?
    >> I made integer conversion for ip/mask but don't know what to do
    >> further..

    >
    >*If* you wanted to use a module, you could use the Net::Netmask module
    >(http://search.cpan.org/~muir/Net-Netmask-1.9004/Netmask.pod)


    Right now I'm looking at it, tnx! :)



    --
    Matija
    Matija Papec, Nov 28, 2003
    #3
  4. Matija Papec

    Matija Papec Guest

    X-Ftn-To: Matija Papec

    Matija Papec <> wrote:
    >>> I would like to discover situations when one ip/mask is able to
    >>> cover another one, eg. does 22.122.0.0/16 cover 122.0.0.0/24?
    >>> I made integer conversion for ip/mask but don't know what to do
    >>> further..

    >>
    >>*If* you wanted to use a module, you could use the Net::Netmask module
    >>(http://search.cpan.org/~muir/Net-Netmask-1.9004/Netmask.pod)

    >
    >Right now I'm looking at it, tnx! :)


    Unfortunately Netmask.pm does not what I want, it can't tell if
    22.3.0.0/16 can "eat" 22.3.1.0/24

    cmpblocks does just simple check,
    $_[0]->{IBASE} <=> $_[1]->{IBASE} || $_[0]->{BITS} <=> $_[1]->{BITS}



    --
    Matija
    Matija Papec, Nov 28, 2003
    #4
  5. Matija Papec

    dominix Guest

    Matija Papec wrote:
    > X-Ftn-To: Matija Papec
    >
    > Matija Papec <> wrote:
    >>>> I would like to discover situations when one ip/mask is able to
    >>>> cover another one, eg. does 22.122.0.0/16 cover 122.0.0.0/24?
    >>>
    >>> *If* you wanted to use a module, you could use the Net::Netmask
    >>> module (http://search.cpan.org/~muir/Net-Netmask-1.9004/Netmask.pod)

    >>

    >
    > Unfortunately Netmask.pm does not what I want, it can't tell if
    > 22.3.0.0/16 can "eat" 22.3.1.0/24
    >


    I suggest you look at NetAddr::IP
    it has a contain method ...

    --
    dominix
    dominix, Nov 30, 2003
    #5
  6. Matija Papec

    dominix Guest

    dominix" <dominix"at wrote:
    > Matija Papec wrote:
    >> X-Ftn-To: Matija Papec
    >>
    >> Matija Papec <> wrote:
    >>>>> I would like to discover situations when one ip/mask is able to
    >>>>> cover another one, eg. does 22.122.0.0/16 cover 122.0.0.0/24?
    >>>>
    >>>> *If* you wanted to use a module, you could use the Net::Netmask
    >>>> module
    >>>> (http://search.cpan.org/~muir/Net-Netmask-1.9004/Netmask.pod)
    >>>

    >>
    >> Unfortunately Netmask.pm does not what I want, it can't tell if
    >> 22.3.0.0/16 can "eat" 22.3.1.0/24
    >>

    >
    > I suggest you look at NetAddr::IP
    > it has a contain method ...


    and off course Net::Subnet


    -- dominix
    dominix, Nov 30, 2003
    #6
  7. Matija Papec

    Juha Laiho Guest

    Matija Papec <> said:
    >I would like to discover situations when one ip/mask is able to cover
    >another one, eg. does 22.122.0.0/16 cover 122.0.0.0/24?
    >I made integer conversion for ip/mask but don't know what to do further..


    Can't think this in proper code just now, but what should be correct
    approach is that you take the "wider" mask and apply that to the net
    address of the network with the "tigter" mask. If this brings you the
    net address of the network with the "wider" mask, then the smaller
    network is completely within the larger one.
    --
    Wolf a.k.a. Juha Laiho Espoo, Finland
    (GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V
    PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
    "...cancel my subscription to the resurrection!" (Jim Morrison)
    Juha Laiho, Nov 30, 2003
    #7
  8. Matija Papec

    Matija Papec Guest

    X-Ftn-To: Juha Laiho

    Juha Laiho <> wrote:
    >>I would like to discover situations when one ip/mask is able to cover
    >>another one, eg. does 22.122.0.0/16 cover 122.0.0.0/24?
    >>I made integer conversion for ip/mask but don't know what to do further..

    >
    >Can't think this in proper code just now, but what should be correct
    >approach is that you take the "wider" mask and apply that to the net
    >address of the network with the "tigter" mask. If this brings you the
    >net address of the network with the "wider" mask, then the smaller
    >network is completely within the larger one.


    Yes! That would be,
    if ((4294901760 & 369165056) == 369164288) ...

    for 22.1.3.0/24 and 22.1.0.0/16 subnets.


    $VAR1 = {
    '_source' => {
    'ip' => 369165056,
    'pr' => 0,
    'mask' => 4294967040
    },
    'source' => '22.1.3.0/24'
    };
    $VAR2 = {
    '_source' => {
    'ip' => 369164288,
    'pr' => 0,
    'mask' => 4294901760
    },
    'source' => '22.1.0.0/16'
    };


    --
    Matija
    Matija Papec, Nov 30, 2003
    #8
  9. Matija Papec

    Matija Papec Guest

    X-Ftn-To: "dominix" <dominix"at"despammed.com>

    >>> Unfortunately Netmask.pm does not what I want, it can't tell if
    >>> 22.3.0.0/16 can "eat" 22.3.1.0/24

    >>
    >> I suggest you look at NetAddr::IP
    >> it has a contain method ...

    >
    >and off course Net::Subnet


    Tnx, I'll take a look.


    --
    Matija
    Matija Papec, Nov 30, 2003
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. DartmanX
    Replies:
    1
    Views:
    2,058
    Roedy Green
    Apr 7, 2006
  2. Java and Swing
    Replies:
    7
    Views:
    8,377
    Gordon Beaton
    Nov 30, 2006
  3. Java and Swing
    Replies:
    0
    Views:
    366
    Java and Swing
    Nov 28, 2006
  4. Prabhu Gurumurthy

    help parsing ipv6 addresses and subnets

    Prabhu Gurumurthy, Nov 8, 2007, in forum: Python
    Replies:
    2
    Views:
    361
    Hyuga
    Nov 8, 2007
  5. Christopher Aldridge
    Replies:
    2
    Views:
    101
    Christopher Aldridge
    Aug 16, 2005
Loading...

Share This Page