On Mon, May 22, 2023 at 08:08:26PM -0400, Greg Wooledge wrote:
> On Tue, May 23, 2023 at 07:39:21AM +0800, Tom Reed wrote:
> > For a given ipv4, if I know net addr and broadcast addr, how will I
> > calculate the netmask?
> 
> I hope this is a theoretical question, because this is backwards.
> Normally you would specify the IP address and the netmask, and the
> software would calculate a broadcast address for you.
> 
> The question you asked has no unique solution in the general case.
> Consider that you have the IPv4 address 10.0.255.42 and the broadcast
> address 10.0.255.255.
> 
> Now, the netmask *could* be /24.  That would make 10.0.255.* the network
> address, and setting all the non-network bits to 1 gives you the
> broadcast address 10.0.255.255.
> 
> But the netmask could also be /22.  That would make the network
> address range 10.0.252.0 through 10.0.255.255.
> 
> In fact, the netmask could be anything from /17 to /24 inclusive.  You
> can't deduce it from the available information.
> 
> That's why you specify the netmask up front.  You have to know it.

Unless... Tom's "net addr" was meant to be "network address", which
(first by convention, now by RFC) has all zeros for the host part.

In that case, it's easy: from the network address to the broadcast
address, exactly the host bit parts change (from 0 to 1), so

  <netmask> = NOT ( <network address> XOR <broadcast address>)

with the customary bitwise operators NOT and XOR.

Only Tom knows now :)

Cheers
-- 
t

Attachment: signature.asc
Description: PGP signature

Reply via email to