Is this explanation of how to determine, mathematically, if a destination IP address is in the same subnet as the sender, correct or not?
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