Subnetting


SUBNETTING


// Subnetting

/*
Note:
     Class A addresses range from 1-126
          prefix value from 8 to 15
     Class B addresses range from 128-191
          prefix value from 16 to 23
     Class C addresses range from 192-223
          prefix value from 24 to 32
         
     Sample input
          Class A :     92.25.12.123/9      
          Class B :     132.45.67.34/20
          Class C :     192.0.2.130/24
                        192.0.2.130/26
*/

import java.io.*;
import java.util.*;
import java.net.*;


class subnetting
{
     public static void main(String as[]) throws UnknownHostException
     {
          Scanner s = new Scanner(System.in);
                  
          System.out.print("Enter IP Address : ");
          String str = s.next().trim();
          String []part = str.split("/");
         
          // get IP Address and subnet mask
          InetAddress ia = InetAddress.getByName(part[0]);
          int prefix = 0;
          if(part.length==2)
              prefix = Integer.parseInt(part[1]);
         
          if(ia.isMulticastAddress())
          {
              System.out.println("Multicast address");
              System.exit(0);
          }
          else if(ia.isAnyLocalAddress())
          {
              System.out.println("Unspecified address");
              System.exit(0);
          }
          else
          {
              System.out.println("Given address = " + str);
             
              // Calculating the subnet mask
              int mask = 0xffffffff << (32 - prefix);
              int value = mask;
              byte[] bytes = new byte[]{    (byte)(value >>> 24),
                                           (byte)(value >> 16 & 0xff),
                                           (byte)(value >> 8 & 0xff),
                                           (byte)(value & 0xff)
                                           };
             
              InetAddress netAddr = InetAddress.getByAddress(bytes);
              System.out.println("Mask = " + netAddr.getHostAddress());
             
             
              // Calculating the network component
              String []adr1 = ia.getHostAddress().split("\\.");
              String []adr2 = netAddr.getHostAddress().split("\\.");
              int []nadr = new int[4];
             

              for(int i=0;i<4;i++)
                   nadr[i] = Integer.parseInt(adr1[i])&Integer.parseInt(adr2[i]);
             
              String nkadr = nadr[0]+"."+nadr[1]+"."+nadr[2]+"."+nadr[3]; 
              System.out.println("network address = " + nkadr);
             
              // Calculating the host component
              int []hadr = new int[4];
              for(int i=0;i<4;i++)
                   hadr[i] = Integer.parseInt(adr1[i])^nadr[i];
             
              String hostadr = hadr[0]+"."+hadr[1]+"."+hadr[2]+"."+hadr[3];        
              System.out.println("host address = " + hostadr);
                            
          }            
     }
}


// Sample Output


D:\cnlab\Subnetting>javac subnetting.java
D:\cnlab\Subnetting>java subnetting
Enter IP Address : 192.0.2.130/26
Given address = 192.0.2.130/26
Mask = 255.255.255.192
network address = 192.0.2.128
host address = 0.0.0.2

D:\cnlab\Subnetting>java subnetting
Enter IP Address : 192.0.2.130/24
Given address = 192.0.2.130/24
Mask = 255.255.255.0
network address = 192.0.2.0
host address = 0.0.0.130

D:\cnlab\Subnetting>java subnetting
Enter IP Address : 0.0.0.0/7
Unspecified address

D:\cnlab\Subnetting>java subnetting
Enter IP Address : 225.23.34.45
Multicast address

D:\cnlab\Subnetting>java subnetting
Enter IP Address : 92.25.12.123/9
Given address = 92.25.12.123/9
Mask = 255.128.0.0
network address = 92.0.0.0
host address = 0.25.12.123

D:\cnlab\Subnetting>java subnetting
Enter IP Address : 132.45.67.34/20
Given address = 132.45.67.34/20
Mask = 255.255.240.0
network address = 132.45.64.0
host address = 0.0.3.34


D:\cnlab\Subnetting>


Subnetting
Subnetting is a process of dividing large network into the smaller networks based on layer 3 IP address. Every computer in network has an IP address which represents its location in network. There are two versions of IP address – IPv4 and IPv6.

IPv4
IP addresses are displayed in dotted decimal notation, and appear as four numbers separated by dots. Each number of an IP address is made from eight individual bits known as octet. Each octet can create number value from 0 to 255. An IP address would be 32 bits long in binary divided into the two components – network component and host component. Network component is used to identify the network that the packet is intended for, and host component is used to identify the individual host on network. IP Classes in decimal notation


  • Class A addresses range from 1-126
  • Class B addresses range from 128-191
  • Class C addresses range from 192-223
  • Class D addresses range from 224-239
  • Class E addresses range from 240-254


Special IP address

  • Loopback Address – 0 [Zero] is reserved and represents all IP addresses.
  • 127 is a reserved address and is used for testing, like a loop back on an interface.
  • 255 is a reserved address and is used for broadcasting purposes.
  • Multicast address – An identifier for a set of interfaces (typically belonging to different nodes). A packet sent to a multicast address is delivered to all interfaces identified by that address.
  • Unspecified Address -- Also called wildcard address. It indicates the absence of an address. It must never be assigned to any node.
  • Class D and Class E addresses are used for broadcasting and research purposes only.


Subnet mask
An IPv4 subnet mask consists of 32 bits, a sequence of ones (1) followed by a block of zeros (0). The trailing block of zeros designates that part as being the host identifier. The following example shows the separation of the network prefix and the host identifier from an address (192.0.2.130) and its associated /24 subnet mask (255.255.255.0). The operation is visualized in a table using binary address formats.

Binary form
Dot-decimal    notation
IP address
11000000.00000000.00000010.10000010
192.0.2.130
Subnet mask
11111111.11111111.11111111.00000000
255.255.255.0
Network prefix
11000000.00000000.00000010.00000000
192.0.2.0
Host part
00000000.00000000.00000000.10000010
0.0.0.130

The result of the bitwise AND operation of IP address and the subnet mask is the network prefix 192.0.2.0. The host part, which is 130, is derived by the bitwise AND operation of the address and the one's complement of the subnet mask.

Subnetting
Subnetting is the process of designating some high-order bits from the host part as part of the network prefix and adjusting the subnet mask appropriately. This divides a network into smaller subnets. The following diagram modifies the example by moving 2 bits from the host part to the network prefix to form four smaller subnets one quarter the previous size:

Binary form
Dot-decimal   notation
IP address
11000000.00000000.00000010.10000010
192.0.2.130
Subnet mask
11111111.11111111.11111111.11000000
255.255.255.192
Network prefix
11000000.00000000.00000010.10000000
192.0.2.128
Host part
00000000.00000000.00000000.00000010
0.0.0.2



No comments:

Post a Comment

Don't be a silent reader...
Leave your comments...

Anu