Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > subnets eating each other?

Reply
Thread Tools

subnets eating each other?

 
 
Matija Papec
Guest
Posts: n/a
 
      11-28-2003

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:umper; 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
 
Reply With Quote
 
 
 
 
James Willmore
Guest
Posts: n/a
 
      11-28-2003
On Fri, 28 Nov 2003 19:19:19 +0100
Matija Papec <(E-Mail Removed)> 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-Net...04/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.
 
Reply With Quote
 
 
 
 
Matija Papec
Guest
Posts: n/a
 
      11-28-2003
X-Ftn-To: James Willmore

James Willmore <(E-Mail Removed)> 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-Net...04/Netmask.pod)


Right now I'm looking at it, tnx!



--
Matija
 
Reply With Quote
 
Matija Papec
Guest
Posts: n/a
 
      11-28-2003
X-Ftn-To: Matija Papec

Matija Papec <(E-Mail Removed)> 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-Net...04/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
 
Reply With Quote
 
dominix
Guest
Posts: n/a
 
      11-30-2003
Matija Papec wrote:
> X-Ftn-To: Matija Papec
>
> Matija Papec <(E-Mail Removed)> 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-Net...04/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


 
Reply With Quote
 
dominix
Guest
Posts: n/a
 
      11-30-2003
dominix" <dominix"at wrote:
> Matija Papec wrote:
>> X-Ftn-To: Matija Papec
>>
>> Matija Papec <(E-Mail Removed)> 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-Net...04/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


 
Reply With Quote
 
Juha Laiho
Guest
Posts: n/a
 
      11-30-2003
Matija Papec <(E-Mail Removed)> 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)
 
Reply With Quote
 
Matija Papec
Guest
Posts: n/a
 
      11-30-2003
X-Ftn-To: Juha Laiho

Juha Laiho <(E-Mail Removed)> 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) == 36916428 ...

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
 
Reply With Quote
 
Matija Papec
Guest
Posts: n/a
 
      11-30-2003
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: What's better a few big subnets or several smaller subnets? jfalken@socket.net Cisco 4 08-29-2008 11:33 AM
Re: Lance & Reichen: Eating each other's asses again Reichen Lancestool Digital Photography 4 01-01-2007 11:55 AM
Fx 1.5.0.1 media player plug-in eating memory Faun Firefox 0 02-26-2006 04:32 PM
Picking your nose and eating it is good for the immune system Tony Spadaro Digital Photography 11 06-22-2004 08:45 AM
Air: Eating Sleeping Waiting and Playing BC DVD Video 3 07-02-2003 05:22 PM



Advertisments