subnet calculations

  • Thread starter Thread starter ohynes
  • Start date Start date
O

ohynes

hey newbie here
wondering if there is anyone out there who can help me
im trying to make a c# subnet calulator using the

IPAddress class

i cant seem to figure out how to do all the subnet calculations
now i can do them on paper but thats about it
just starting to learn c sharp

well if any one could help me
it would be great

cheers
 
Hi ohynes,

First, do it on paper.
Then write it again as pseudo code along the lines of

if this happens
do that
otherwise
don't do that

When you have pseudo code start thinking about what variables you need and
how to form if sentences etc.
Write your code based on pseudo code.
 
tryed that but dont no enough
to do it.. like AND function
and binary in c#

any other advice?
 
Shaggyh,

Give us some of your pseudo code and we can help you translate it to the
equivalent C# code.
 
ok pseudo code is

variables
IPAddress ipaddress
int networkbits
int borrowedbits

class subnetinfo(string ipaddress, int netbits, int borbits)
{
this.ipaddress = IPaddress.parse(ipaddress)
networkbits = netbits
borrowedbits = borbits
}

class void calsubnet()
{
this is where i get stuck
trying to figure out this part
}
 
Ok Shaggyh,

Are you trying to calculate the subnetmask from the knowledge of an ip
address or calculate ip addresses based on a given subnet mask? I don't
think you can do the former without checking variues ip addresses against
some dns servers or similar.

If you want to calculate ip addresses based on subnet, what information do
you have?
Subnet masks are given as xxx.xxx.xxx.xxx. If you are given a mask and an
ip address and want to calculate possible ip addresses using that address
and mask there are several ways to do it.

Internally the subnet mask and ip addresses are treated as four bytes.
168.192.20.44 with a subnet mask of 255.255.255.192 would mean
10101000.11000000.00010100.00101100 for the ip address and
11111111.11111111.11111111.11000000 for the subnet mask meaning
00000000.00000000.00000000.00111111 possible addresses in your range

I'm not the one to ask for calculating ip addresses based on subnet masks
as I only know the basics like the first and two last addresses in a range
are reserved (I think). I can only help you translate your pseudo code
into C#.
 
this is what im trying to code a



simple Subnet Calculator for Class C IPv4 addresses.



The calculator should calculate subnet information for specified IPv4 Class
C network addresses only (ignore Class A and B). The user should input the
IP address for the Class C network, the number of network bits in the
address (since it could be a subnetted address already), and the number of
bits to borrow for subnetting.



e.g.

192.168.10.0 - Class C IP network address

24 - number of network bits in address

2 - number of bits to borrow





dont no if this will help anyone..

but all help is welcome
 
this is what im trying to code a



simple Subnet Calculator for Class C IPv4 addresses.



The calculator should calculate subnet information for specified IPv4
Class
C network addresses only (ignore Class A and B). The user should input
the
IP address for the Class C network, the number of network bits in the
address (since it could be a subnetted address already), and the number
of
bits to borrow for subnetting.



e.g.

192.168.10.0 - Class C IP network address

24 - number of network bits in address

2 - number of bits to borrow





dont no if this will help anyone..

but all help is welcome

Well, if I understand you right, you get a 255.255.255.0 subnet (24 bits).
What do you mean by 2 bits to borrow? 2 bits out of the last 8-bit range?
In that case you will get
11111111.11111111.11111111.11000000 = 255.255.255.192

Consider the ip address as a 32 bit long number. 24+2 is 26 bits
If you put -1 in an integer you will get the number

11111111111111111111111111111111

Push this number left as many positions as you lack to get full 32 bits

int n = -1;
n <<= (32 - 26);

n is now

11111111111111111111111111000000

You can convert this number back to a subnet string by splitting it in
chunks of 8 bytes
We can do that by using AND (&) with 11111111 (255)

11111111111111111111111111000000
& 00000000000000000000000011111111
= 00000000000000000000000011000000 = 192

n & 255

Push the number right 8 bits and we get

n >>= 8;

00000000111111111111111111111111 (->11000000)
& 00000000000000000000000011111111
= 00000000000000000000000011111111 = 255

In the end you will have something like

int n = -1;
n <<= (32 - networkbits - borrowedbits);
string subnet = (n & 255).ToString();
n >>= 8;
subnet = (n & 255) + "." + subnet;
n >>= 8;
subnet = (n & 255) + "." + subnet;
n >>= 8;
subnet = (n & 255) + "." + subnet;

subnet is now "255.255.255.192"
 
Back
Top