Hi there,

Is there a control that I can use in C# which will help me work with IP
addresses conveniently? And how can I add and subbtract IP addresses?

Re: IP address handling by Carsten

Carsten
Wed Feb 11 04:13:20 CST 2004

Ahmed Shafi wrote:
> Hi there,
>
> Is there a control that I can use in C# which will help me work with
> IP addresses conveniently? And how can I add and subbtract IP
> addresses?

IP(v4) addresses are just 32 bit unsigned integers. What most people
refer to when talking about an "IP address" is the so-called "dotted
quad" notation (e.g. "192.168.0.1"). To convert it to a "real" address,
you'd have to split the dotted quad (what would be a string) into the
four pieces, convert the pieces into "byte"s and then

real_ip = (quad[3] << 24) | (quad[2] << 16) | (quad[1] << 8) |
(quad[0]);

I don't know what "adding" and "substracting" IPs could mean, but if
you're refering to dealing with netmasks, it's just as simple as:

match1 = real_ip1 & real_netmask;
match2 = real_ip2 & real_netmask;

if (match1 == match2) // same subnet
{
// do something
}
else // different subnet
{
// do something else
}

HTH,
Carsten



Re: IP address handling by Carsten

Carsten
Wed Feb 11 04:15:30 CST 2004

Carsten Posingies wrote:
> quad" notation (e.g. "192.168.0.1"). To convert it to a "real"
> address, you'd have to split the dotted quad (what would be a string)
> into the four pieces, convert the pieces into "byte"s and then

Rubbish. They have, of course, to be converted into uint32s. Pardon!



Re: IP address handling by Carsten

Carsten
Wed Feb 11 05:38:23 CST 2004

For the .NET support, look for "System.Net" in the .NET framework.

For "educational purposes" here an example in C# that does everything
"bare bones":

(Note that some lines are different from what I've posted before, that
was theory, this is practise <g>)

using System;
using System.Text;

namespace IPDemo
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
// dotted-quad notated IPs...
string dotted_quad_IP1;
string dotted_quad_IP2;
string dotted_quad_netmask1;
string dotted_quad_netmask2;

// ... become unsigned ints (32 bit)
// (long here for convenience)
long real_IP1;
long real_IP2;
long real_netmask1;
long real_netmask2;

// the results of some binary math go here
long maskIP1withnetmask1;
long maskIP2withnetmask1;
long maskIP1withnetmask2;
long maskIP2withnetmask2;

// 192.168.000.001 is 192.168.0.1,
// this crude format just for a nice output
dotted_quad_IP1 = "192.168.000.001";
dotted_quad_IP2 = "192.168.017.002";
dotted_quad_netmask1 = "255.255.255.000";
dotted_quad_netmask2 = "255.255.000.000";

// see method below
real_IP1 = DottedQuadToInt(dotted_quad_IP1);
real_IP2 = DottedQuadToInt(dotted_quad_IP2);
real_netmask1 = DottedQuadToInt(dotted_quad_netmask1);
real_netmask2 = DottedQuadToInt(dotted_quad_netmask2);

Console.WriteLine("IP1: " + dotted_quad_IP1 + " is binary: " \
+ Int2Bin(real_IP1));
Console.WriteLine("IP2: " + dotted_quad_IP2 + " is binary: " \
+ Int2Bin(real_IP2));
Console.WriteLine("Netmask1: " + dotted_quad_netmask1 + \
" is binary: " + Int2Bin(real_netmask1));
Console.WriteLine("Netmask2: " + dotted_quad_netmask2 + \
" is binary: " + Int2Bin(real_netmask2));

maskIP1withnetmask1 = real_IP1 & real_netmask1;
maskIP2withnetmask1 = real_IP2 & real_netmask1;
maskIP1withnetmask2 = real_IP1 & real_netmask2;
maskIP2withnetmask2 = real_IP2 & real_netmask2;

Console.WriteLine("IP1 masked with netmask1 results in binary: " \
+ Int2Bin(maskIP1withnetmask1));
Console.WriteLine("IP2 masked with netmask1 results in binary: " \
+ Int2Bin(maskIP2withnetmask1));

if (IPsAreInSameSubnet(real_IP1, real_IP2, real_netmask1)) {
Console.WriteLine("==> same subnet");
}
else {
Console.WriteLine("==> different subnets");
}

Console.WriteLine("IP1 masked with netmask2 results in binary: " \
+ Int2Bin(maskIP1withnetmask2));
Console.WriteLine("IP2 masked with netmask2 results in binary: " \
+ Int2Bin(maskIP2withnetmask2));

if (IPsAreInSameSubnet(real_IP1, real_IP2, real_netmask2)) {
Console.WriteLine("==> same subnet");
}
else {
Console.WriteLine("==> different subnets");
}

Console.ReadLine();
}

// convert an int into a string representing the binary format
// of the int
static string Int2Bin(long number)
{
long k;
string binaryForm = "";

for (int i = 0; i < 32; i++) {
k = number % 2;
if (k == 1) {
binaryForm = "1" + binaryForm;
}
else {
binaryForm = "0" + binaryForm;
}

// uncomment next 2 lines to have "binary dotted quad"s
// if ((i + 1) % 8 == 0 && i < 31)
// binaryForm = "." + binaryForm;

number = number / 2;
}
return binaryForm;
}

// build the int representation of a string that is a dotted
// quad IP string
static long DottedQuadToInt(string DottedQuad)
{
string[] IP_blocks = DottedQuad.Split('.');
return (Convert.ToInt64(IP_blocks[0]) << 24) | \
(Convert.ToInt64(IP_blocks[1]) << 16) | \
(Convert.ToInt64(IP_blocks[2]) << 8) | \
(Convert.ToInt64(IP_blocks[3]));
}

// check if 2 IPv4 addresses are in the same subnet
static bool IPsAreInSameSubnet(long ip1, long ip2, long netmask)
{
return ((ip1 & netmask) == (ip2 & netmask));
}
}
}