subnets eating each other?

M

Matija Papec

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;
}
 
J

James Willmore

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.
 
M

Matija Papec

X-Ftn-To: Matija Papec

Matija Papec said:
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}
 
D

dominix

Matija said:
X-Ftn-To: Matija Papec



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 ...
 
J

Juha Laiho

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.
 
M

Matija Papec

X-Ftn-To: Juha Laiho

Juha Laiho said:
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'
};
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top