On May 22, 2023, at 8:08 PM, Greg Wooledge <g...@wooledge.org> 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. The network address is 10.0.252.0, the first address. The broadcast address is 10.0.252.255, the last address. The host addresses are 10.0.252.1 - 254. >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.