473,395 Members | 1,471 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,395 software developers and data experts.

Comparison of IPAddresses

Hello,

I'd like to verify if an IP address (as an IPAddress parameter) is in a
range defined by two other IP addresses (also passed as IPAddress
parameters), something like IsLoopback() but not only for 127.x.y.z and not
only for x,y,z in the 0-255 range.

There is an Equals member function but it only compares if two IP addresses
are equal which is not enough in my case what I'd need is a Compare()
function which returns <0, 0 or >0 like String.Compare().

Currently I target IPv4 but I'd like it to be compatible with IPv6 too if
the IPAddress and the range are IPv6. In the Framework V1.0 I was using the
Address property like below, but in the V1.1 it is marked as obsolete.

Here is what I was doing:

public static bool IsAddressInRange(IPAddress addrSrc, IPAddressRange range)
{
long addr = IPAddress.NetworkToHostOrder(addrSrc.Address);
return (addr >= IPAddress.NetworkToHostOrder(range.lo.Address) &&
addr <= IPAddress.NetworkToHostOrder(range.hi.Address));
}

Could you recommend a method to do that for both address families? Thank
you.

--
Noël
http://noeld.com

Nov 15 '05 #1
2 5600
Hi,

I have written a function that will do the similar work to calling the
Address property. So you may use it instead of using Addess property to
avoid the "obsolete" message.

public static bool IsAddressInRange(IPAddress addrSrc, IPAddressRange range)
{
//long addr = IPAddress.NetworkToHostOrder(addrSrc.Address);
//return (addr >= IPAddress.NetworkToHostOrder(range.lo.Address) &&
//addr <= IPAddress.NetworkToHostOrder(range.hi.Address));

long addr = IPAddress.NetworkToHostOrder(getLongFromAddress(ad drSrc));
return (addr >= IPAddress.NetworkToHostOrder(getLongFromAddress(ra nge.lo))
&&
addr <= IPAddress.NetworkToHostOrder(getLongFromAddress(ra nge.hi)));
}

private static long getLongFromAddress (IPAddress ipa)
{
byte[] b = ipa.GetAddressBytes();
long l = 0;
for (int i = 0; i < b.Length ; i++)
{
l += (long) (b[i] * Math.Pow(256,i));
}
return l;
}

public struct IPAddressRange
{
public IPAddress hi;
public IPAddress lo;
}

Have a look and see whether it works for you.

If you have any concerns, please feel free to let me know.

Felix Wang
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 15 '05 #2
Hi Felix,

Thank you very much for your replies and your sample code. I tried your last
sample with sucess so I will use it. I just added an AddressFamily test at
the beginning to avoid a possible out-of-range index in case the
AddressFamily of the address and the range differs.

Thanks again for your help.
--
Noël
Nov 15 '05 #3

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

Similar topics

0
by: bettervssremoting | last post by:
To view the full article, please visit http://www.BetterVssRemoting.com Better VSS Remote Access Tool This article makes a detailed comparison among SourceAnyWhere, SourceOffSite, VSS...
29
by: Steven D'Aprano | last post by:
Playing around with comparisons of functions (don't ask), I discovered an interesting bit of unintuitive behaviour: >>> (lambda y: y) < (lambda y: y) False Do the comparison again and things...
5
by: mayamorning123 | last post by:
A comparison among six VSS remote tools including SourceOffSite , SourceAnyWhere, VSS Connect, SourceXT, VSS Remoting, VSS.NET To view the full article, please visit...
46
by: yadurajj | last post by:
Hello i am newbie trying to learn C..I need to know about string comparisons in C, without using a library function,...recently I was asked this in an interview..I can write a small program but I...
0
by: bettervssremoting | last post by:
To view the full article, please visit http://www.BetterVssRemoting.com Better VSS Remote Access Tool This article makes a detailed comparison among SourceAnyWhere, SourceOffSite, VSS...
37
by: spam.noam | last post by:
Hello, Guido has decided, in python-dev, that in Py3K the id-based order comparisons will be dropped. This means that, for example, "{} < " will raise a TypeError instead of the current...
7
by: bcutting | last post by:
I am looking for a way to take a large number of images and find matches among them. These images may not be exact replicas. Images may have been resized, cropped, faded, color corrected, etc. ...
0
by: SvenMathijssen | last post by:
Hi, I've been wrestling with a problem for some time that ought to be fairly simple, but turns out to be very difficult for me to solve. Maybe someone here knows the answer. What I try to do is...
1
by: Lars B | last post by:
Hey guys, I have written a C++ program that passes data from a file to an FPGA board and back again using software and DMA buffers. In my program I need to compare the size of a given file against...
11
by: Andrus | last post by:
I created dynamic extension methods for <= and < SQL comparison operators: public static IQueryable<TLessThanOrEqual<T>(this IQueryable<Tsource, string property, object value); public static...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.