1
  1. Is this explanation of how to determine, mathematically, if a destination IP address is in the same subnet as the sender, correct or not?

  2. Is this what the computer does or does it do the math differently?

How do you calculate the prefix, network, subnet, and host numbers? is a great entry and covers a lot of ground, but to answer my question, I have to extrapolate and so I am left with the question as to whether or not my extrapolation is correct.


To determine if a packet is to be handed off to the switch to deal with, or forwarded to the router (gateway), we need to determine if the destination address is within the same network or not.

To do that, we can simply apply the netmask to the destination address so as to extract the network prefix. If we get the same value as the prefix for the computer, then it is on the same network.

For this example, let's assume the user is doing a ping to a computer with the IP address of 192.168.22.45. Let's apply the netmask of our computer against this IP address:

11000000.10101000.00010110.00101101 (192.168.22.45 - IP address) AND 11111111.11111111.11111111.00000000 (255.255.255.0 - Netmask) ----------------------------------- = 11000000.10101000.00010110.00000000 (192.168.22.0 - Prefix)

Finally, let's compare the prefix gotten to the prefix we got with the IP address of our computer earlier: 192.168.22.0 is not equal to 192.168.1.0.

Or in binary with Boolean logic, we compare the two prefix using the Exclusive-OR operator. If we get all 0s, then the two prefix are equal and therefore, are in the same network.

11000000.10101000.00010110.00000000 (Destination prefix - 192.168.22.0) XOR 11000000.10101000.00000001.00000000 (Local computer prefix - 192.168.1.0) ----------------------------------- = 00000000.00000000.00010111.00000000

ETL
  • 135
  • 1
  • 6
  • @RonMaupin - I definitely read this post, but it does not explain how to determine if an IP address is part of a subnet or not, mathematically. I can derive an answer but I am trying to confirm that the above is correct. – ETL Apr 12 '16 at 23:43
  • It explains how to get the network of an address if you have the mask. If you have two addresses, you mask them, and and the results are equal, what does that tell you? – Ron Maupin Apr 12 '16 at 23:45
  • 2
    Slight correction. Discovering whether the target is on your network or not will determine whether ARP will try to resolve the target's MAC address or the Default Gateway's MAC address. Either way, the packet will probably go through the switch. – Eddie Apr 13 '16 at 07:49
  • ...let's try keeping this Q around. It is, basically a dupe, but maybe the students will find it and stop re-asking this same question. :/ – Craig Constantine Apr 13 '16 at 13:12
  • @CraigConstantine - I guess this type of question comes up often! I can see how it's almost a duplicate but I edited it to explain why I felt I didn't get an answer from the other question. – ETL Apr 13 '16 at 13:54

1 Answers1

1

Yes, there is a mathematical way (and that is how the computer does it unless I'm much mistaken).

Your computer is X netmask M

The other computer is Y.

The computers are on the same network if (X & M) = (Y & M) where & is bitwise AND.

A better answer than mine would go into the source code of the network stack :)

Law29
  • 406
  • 1
  • 5
  • 10
  • Thank you! This confirms that the math explained in the question is the right way of calculating whether a destination host is in the same subnet. – ETL Apr 13 '16 at 14:09
  • There is no other way... you could calculate N=(X & M), B=(N | (not M), and check if N <= Y <= B, but there is no reason to do so. Either you calculate the network (once) and you compare that to (Y&M) or you convert the netmask to a network length S and compare (X>>S) to (Y>>S), the end result is the same. – Law29 Apr 13 '16 at 18:44