473,770 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(By Val 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.GetAddres sBytes
Dim DestIPArr As Byte() = DestIP.GetAddre ssBytes
Dim SubIPArr As Byte() = SubIP.GetAddres sBytes

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

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(By Val 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.GetAddres sBytes
Dim DestIPArr As Byte() = DestIP.GetAddre ssBytes
Dim SubIPArr As Byte() = SubIP.GetAddres sBytes

'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*******@hotm ail.com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.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(By Val 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.GetAddres sBytes
Dim DestIPArr As Byte() = DestIP.GetAddre ssBytes
Dim SubIPArr As Byte() = SubIP.GetAddres sBytes

'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*******@hotm ail.com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.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
1683
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 gateway. Under Win32, the Win32 IP helper API provides a GetIpAddrTable() function that returns information that contain the subnet mask, but that's Win32 level, and is not portable. Is there something available in the Python library for that...
0
1352
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 use a similar approach to get machines in each subnet?? JER
1
2420
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 behind it, but I have no idea on how to actually implement it! I have split my supplied 3 IP's into 4 numbers each, but past that I'm stuck - on paper I would convert them to binary and write down all the 1's and 0's then figure it out, but in...
3
7045
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 { public: int node_id;
2
3864
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 wrapper from the win32 C++ functions. using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices;
1
1787
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
5137
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 it only tells me that "instance" is used before it has been assigned a value. Can anyone tell me what to do to get my subnet location info? Public Property SubnetLocation() As String Get Dim instance As ActiveDirectorySubnet
3
9521
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
3105
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 second textbox. Please help me by providing links or c#.net code. Thanks in Advance. Regards, Nussu
0
9618
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10101
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9906
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7456
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6710
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2849
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.