A
A
Anton2014-05-13 13:29:18
Algorithms
Anton, 2014-05-13 13:29:18

How to determine the occurrence of one subnet in another? (algorithm in PHP)?

Hello, there are 2 subnets with masks, let's say

$subnet1 = '10.5.1.0/20';
$subnet2 = '10.5.1.0/24';

How to check that one subnet is part of another?
I found such a solution - get all the addresses of each subnet, take the first and last, convert to integers (ip2long) and stupidly check the occurrence of one range in another.
But something tells me that there is a more beautiful solution - for example, somehow make it XOR, but I don’t understand how to do it right.
Can you tell me?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrew, 2014-05-13
@zeromodule

Take a network with a smaller mask (/24),
convert its IP (first) to long,
check if this IP enters a network with a wider mask (/20).
PS Network 10.5.1.0/20 is not possible because its lower 12 bits must be zeros.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question