A little help with my subnet comparison function please

J

JamesB

Hmm, this doesn't seem to work. I am probably tackling it completely the
wrong way, but basically I want a function to receive two IP's and a subnet
mask and return True if IP1 and 2 are on the same subnet, otherwise false.

I have got this so far:



Private Function IsSameSubnet(ByVal IPSource As String, ByVal IPDest As
String, ByVal IPSubnet As String) As Boolean

'return TRUE if the two IP's share the subnet specified, else FALSE

'Get IP Addresses from the strings
Dim SrcIP As IPAddress = IPAddress.Parse(IPSource)
Dim DestIP As IPAddress = IPAddress.Parse(IPDest)
Dim SubIP As IPAddress = IPAddress.Parse(IPSubnet)

'Convert to byte arrays
Dim SrcIPArr As Byte() = SrcIP.GetAddressBytes
Dim DestIPArr As Byte() = DestIP.GetAddressBytes
Dim SubIPArr As Byte() = SubIP.GetAddressBytes

'Default retval True
IsSameSubnet = True

Dim i as integer

'Loop around each - if the byte "anded" with the subnet ever differs between
source
'and dest then they must be different subnets
For i = 1 To 4
If (SrcIPArr(i) And SubIPArr(i)) <> (DestIPArr(i) And SubIPArr(i)) Then
IsSameSubnet = False
End If
Next

End Function
 
L

Larry Lard

JamesB said:
Hmm, this doesn't seem to work.

In what way does it fail?
I am probably tackling it completely the
wrong way, but basically I want a function to receive two IP's and a subnet
mask and return True if IP1 and 2 are on the same subnet, otherwise false.

I have got this so far:

Private Function IsSameSubnet(ByVal IPSource As String, ByVal IPDest As
String, ByVal IPSubnet As String) As Boolean

'return TRUE if the two IP's share the subnet specified, else FALSE

'Get IP Addresses from the strings
Dim SrcIP As IPAddress = IPAddress.Parse(IPSource)
Dim DestIP As IPAddress = IPAddress.Parse(IPDest)
Dim SubIP As IPAddress = IPAddress.Parse(IPSubnet)

Exception handling here would be nice, but this is sample code I
suppose :)
'Convert to byte arrays
Dim SrcIPArr As Byte() = SrcIP.GetAddressBytes
Dim DestIPArr As Byte() = DestIP.GetAddressBytes
Dim SubIPArr As Byte() = SubIP.GetAddressBytes

'Default retval True
IsSameSubnet = True

Dim i as integer

'Loop around each - if the byte "anded" with the subnet ever differs between
source
'and dest then they must be different subnets
For i = 1 To 4

Shouldn't this be 0 to 3? I would have thought trying to access (4) of
these arrays would give you an array bounds exception.
If (SrcIPArr(i) And SubIPArr(i)) <> (DestIPArr(i) And SubIPArr(i)) Then
IsSameSubnet = False

You might as well Exit Function here; it's never going to be true if
it's false once.
End If
Next

End Function

Principle looks ok; what goes wrong?
 
J

JamesB

Larry Lard said:
In what way does it fail?
Yeah, slight clarit problem on my part!
Basically it doesnt seem to return true or false - I tried stepping through
the code at runtime but so many packets are being picked up that it seems to
keep going back to the beginning of the packet received event before I get
to this function... so instead I tried displaying the value on screen and it
just never appears - could argue it's not getting called, but if I put a
breakpoint in my function it does get to there and stop, but then I have the
same problem stepping through.
Exception handling here would be nice, but this is sample code I
suppose :)

Shouldn't this be 0 to 3? I would have thought trying to access (4) of
these arrays would give you an array bounds exception.
Hmm, possibly?
For some reason I thought arrays were 1 based not 0 based in .net - I dont
get an error in any case.
You might as well Exit Function here; it's never going to be true if
it's false once.
Good tip.
Principle looks ok; what goes wrong?
See beginning!
Thanks
James
 
J

JamesB

Larry Lard said:
Shouldn't this be 0 to 3? I would have thought trying to access (4) of
these arrays would give you an array bounds exception.

You were right. Dunno where I got 1 from!
James.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top