473,320 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

A little help with my subnet comparison function please

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
Dec 1 '05 #1
3 1843

JamesB wrote:
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?

--
Larry Lard
Replies to group pleas

Dec 1 '05 #2

"Larry Lard" <la*******@hotmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...

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


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.
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 :)

<cough> Erm, indeed...

'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.

Hmm, possibly?
For some reason I thought arrays were 1 based not 0 based in .net - I dont
get an error in any case.
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.

Good tip.
End If
Next

End Function


Principle looks ok; what goes wrong?

See beginning!
Thanks
James
Dec 1 '05 #3

"Larry Lard" <la*******@hotmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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.


You were right. Dunno where I got 1 from!
James.
Dec 2 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Pierre Rouleau | last post by:
Hi all, socket.gethostbyname_ex(socket.gethostname()) will return the list of IP addresses on a the local host. I did not find any function that can be used to get the subnet mask and default...
0
by: Jerry Negrelli | last post by:
Does anyone know how to get the subnet of a machine in C# (without using WMI's NetworkAdapterConfiguration object)? I've queried the Active Directory to get the visible subnets -- perhaps I can...
1
by: JamesB | last post by:
I'm looking for a function to return true/false depending on if two supplied IP's (as strings) are in the same subnet (also supplied). Im getting nowhere fast unfortunately - I know the theory...
3
by: eric.boissard | last post by:
Hello, I managed to implement the AStar algorithm, however I have some trouble using the priority queue, and the 'compare' function. Here is my 'Waypoint.h' header file: class Waypoint {...
2
by: Mattias | last post by:
I'm trying to get the number of clients on every subnet using DhcpEnumSubnetClients on every subnet returned by DhcpEnumSubnets. The problem is that I don't understand how to declare the .Net...
1
by: Terry Olsen | last post by:
Is there an "elegant" way to determine if an IP address is on my subnet? Or do I just compare the first 2 or 3 octets?
4
by: Morten Fagermoen | last post by:
Hi! I try to get the subnet location info using the code below found at http://msdn2.microsoft.com/en-us/library/system.directoryservices.activedirectory.activedirectorysubnet.location.aspx. But...
3
usafshah
by: usafshah | last post by:
is it possible to have default gateway of different subnet from IP? Like IP: 192.168.0.154 G/W: 192.168.16.1 will it work? please explain in both cases Thanks in advance.
2
by: nussu | last post by:
Hi , I need to find a subnet mask number for a given IP Address number. I have two textbox's once enter the ip address in first textbox i need to get subnet mask number for that ip address in...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.