Check if an IP address is in a range.

  • Thread starter Thread starter Smokey Grindel
  • Start date Start date
S

Smokey Grindel

Is there a way to check if an IP address is in a subnet range? say I have
the IP

10.10.0.0 and a subnet of 255.255.0.0 and am given the IP 10.10.50.1 and
want to verify that it is in that range.. thanks!
 
Is there a way to check if an IP address is in a subnet range? say I have
the IP

10.10.0.0 and a subnet of 255.255.0.0 and am given the IP 10.10.50.1 and
want to verify that it is in that range.. thanks!

If (Subnet And SubnetMask) = (IpAddress And SubnetMask)
' Ip Address is in the subnet
End If

I don't know if there is a .NET class method that will do this.
 
Is there a way to check if an IP address is in a subnet range? say I have
the IP

10.10.0.0 and a subnet of 255.255.0.0 and am given the IP 10.10.50.1 and
want to verify that it is in that range.. thanks!

If you have the network address (10.10.0.0) and you have the subnet
mask (255.255.0.0) of the IP and the IP (10.10.50.1) itself you can
use the subnet to determine the network address of the IP and then
compare it to your real value to ensure it is correct.

dim ip as UInt32 = 168440321 'uint32 value for 10.10.50.1
dim subnet as UInt32 = 4294901760 'uint32 value for 255.255.0.0
dim network as UInt32 = 168427520 'uint32 value for 10.10.0.0

if ((ip And subnetMask) = networkAddress) then
'in range
else
'out of range
end if

Please be aware that Jack Jacksons reply is inaccurate. There is no
need to do the:

If (Subnet and SubnetMask) part of the IF, because the operation
(subnet And SubnetMask) will always equal subnet. It's a redundant
statement.
 
If you have the network address (10.10.0.0) and you have the subnet
mask (255.255.0.0) of the IP and the IP (10.10.50.1) itself you can
use the subnet to determine the network address of the IP and then
compare it to your real value to ensure it is correct.

dim ip as UInt32 = 168440321 'uint32 value for 10.10.50.1
dim subnetMask as UInt32 = 4294901760 'uint32 value for 255.255.0.0 ***********
dim networkAddress as UInt32 = 168427520 'uint32 value for 10.10.0.0 ***********

if ((ip And subnetMask) = networkAddress) then
'in range
else
'out of range
end if

Please be aware that Jack Jacksons reply is inaccurate. There is no
need to do the:

If (Subnet and SubnetMask) part of the IF, because the operation
(subnet And SubnetMask) will always equal subnet. It's a redundant
statement.

Sorry, just fixed the variable names which were wrong.
 
Back
Top