IPv4 Classes and Subnetting


Aim: Write a program to find out class of a given IP address, subnet mask & first & last IP address of that block.

Apparatus (Software): Eclipse/ Netbeans

Procedure: Following should be studied to understand this practical.



IPv4 Addressing and Subnetting

Hardware Addressing

A hardware address is used to uniquely identify a host within a local network. Hardware addressing is a function of the Data-Link layer of the OSI model (Layer-2). Ethernet utilizes the 48-bit MAC address as its hardware address.
MAC address is most often represented in hexadecimal, using one of two accepted formats:
00:43:AB:F2:32:13
The first six hexadecimal digits of a MAC address identify the manufacturer of the physical network interface. This is referred to as the OUI (Organizational Unique Identifier). The last six digits uniquely identify the host itself, and are referred to as the host ID.

The MAC address has one shortcoming – it contains no hierarchy. MAC addresses provide no mechanism to create boundaries between networks. There is no method to distinguish one network from another.

Internet Protocol (IP)

In the 1970’s, the Department of Defense developed the Transmission Control Protocol (TCP), to provide both Network and Transport layer functions. When this proved to be an inflexible solution, those functions were separated - with the Internet Protocol (IP) providing Network layer services, and TCP providing Transport layer services. Together, TCP and IP provide the core functionality for the TCP/IP or Internet protocol suite.

IP provides two fundamental Network layer services:

Logical addressing – provides a unique address that identifies both the host, and the network that host exists on.
Routing – determines the best path to a particular destination network, and then routes data accordingly.


IPv4 Addressing

A core function of IP is to provide logical addressing for hosts. An IP address provides a hierarchical structure to both uniquely identify a host, and what network that host exists on.
An IP address is most often represented in decimal, in the following format:

158.80.164.3

An IP address is comprised of four octets, separated by periods:
First Octet Second Octet Third Octet Fourth Octet
158 80 164 3

Each octet is an 8-bit number, resulting in a 32-bit IP address. The smallest
possible value of an octet is 0, or 00000000 in binary. The largest possible
value of an octet is 255, or 11111111 in binary.
The above IP address represented in binary would look as follows:
First Octet Second Octet Third Octet Fourth Octet
10011110 01010000 10100100 00000011

The Subnet Mask

Part of an IP address identifies the network. The other part of the address cidentifies the host. A subnet mask is required to provide this distinction:

158.80.164.3 255.255.0.0

The above IP address has a subnet mask of 255.255.0.0. The subnet mask follows two rules:
• If a binary bit is set to a 1 (or on) in a subnet mask, the corresponding bit in the address identifies the network.
• If a binary bit is set to a 0 (or off) in a subnet mask, the corresponding bit in the address identifies the host.

Looking at the above address and subnet mask in binary:

IP Address: 10011110.01010000.10100100.00000011
Subnet Mask: 11111111.11111111.00000000.00000000


The first 16 bits of the subnet mask are set to 1. Thus, the first 16 bits of the address (158.80) identify the network. The last 16 bits of the subnet mask are set to 0. Thus, the last 16 bits of the address (164.3) identify the unique host on that network. The network portion of the subnet mask must be contiguous. For example, a subnet mask of 255.0.0.255 is not valid.

IP Address Classes

The IPv4 address space has been structured into several classes. The value of the first octet of an address determines the class of the network. The figure shows how the ip addresses are classified:

Class A networks range from 1 to 127. The default subnet mask is 255.0.0.0. Thus, by default, the first octet defines the network, and the last three octets define the host. This results in a maximum of 127 Class A networks, with 16,777,214 hosts per network!

Example of a Class A address:
Address: 64.32.254.100
Subnet Mask: 255.0.0.0

Class B networks range from 128 to 191. The default subnet mask is 255.255.0.0. Thus, by default, the first two octets define the network, and the last two octets define the host. This results in a maximum of 16,384 Class B networks, with 65,534 hosts per network.

Example of a Class B address:
Address: 152.41.12.195
Subnet Mask: 255.255.0.0

Class C networks range from 192 to 223. The default subnet mask is 255.255.255.0. Thus, by default, the first three octets define the network, and the last octet defines the host. This results in a maximum of 2,097,152 Class C networks, with 254 hosts per network.

Example of a Class C address:
Address: 207.79.233.6
Subnet Mask: 255.255.255.0


Class D networks are reserved for multicast traffic. Class D addresses do not use a subnet mask.

Subnetting

Subnetting is the process of creating new networks (or subnets) by stealing bits from the host portion of a subnet mask. There is one caveat: stealing bits from hosts creates more networks but fewer hosts per network.

Consider the following Class C network:
192.168.254.0
The default subnet mask for this network is 255.255.255.0. This single network can be segmented, or subnetted, into multiple networks.

For example, assume a minimum of 10 new networks are required. Resolving
this is possible using the following magical formula:

2 power n

The exponent ‘n’ identifies the number of bits to steal from the host portion of the subnet mask. The default Class C mask (255.255.255.0) looks as follows in binary:

11111111.1111111.1111111.00000000

There are a total of 24 bits set to 1, which are used to identify the network. There are a total of 8 bits set to 0, which are used to identify the host, and these host bits can be stolen.

Consider the result if three bits are stolen. Using the above formula:
2 power n


=
2 power 3
=
8
=
8 new networks created However, a total of 8 new networks does not meet the original requirement
of at least 10 networks. Consider the result if four bits are stolen:
2 power n
=
2 power 4
=
16
=
16 new networks created A total of 16 new networks does meet the original requirement. Stealing four host bits results in the following new subnet mask:

11111111.11111111.11111111.11110000 = 255.255.255.240

Program:

package ipclass;

import java.util.Arrays;
import java.util.Scanner;
import java.util.regex.Pattern;

public class main {


        private static int bits;

        public static void main(String[] args){

     
        Scanner Scr = new Scanner(System.in);
        System.out.println("Enter a valid network IP:");
       
        String pattern = "^([01]?\\d\\d?|2[0-5]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-5]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-5]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-5]\\d|25[0-5])$";
        //String pattern = "[0-255][.][0-255][.][0-255][.][0-255]";
      
        boolean matches = false;

            String IP = Scr.nextLine();
            matches = Pattern.matches(pattern, IP);
          
            if(matches==false)
                System.out.println("wrong range");
           
            else{
               
                String split[] =IP.split("\\.");
                String splitbinary[]= new String[4];
                String binaryip[]= new String[4];  
                String bip=" ";
              
                for(int i = 0; i < split.length; i++)
                {
                    System.out.println("\n "+split[i]);
                  
                    int ip =Integer.parseInt(split[i]);  
                    splitbinary[i]= Integer.toBinaryString(ip);
                    System.out.println("Binary: "+splitbinary[i]);
                   
                    binaryip[i]=String.format("%8s",splitbinary[i]).replace(" ","0");
                    System.out.println("Appended form: "+binaryip[i]);
                    bip += binaryip[i];
                   
                }
               
                System.out.println("\n Binary Equivalent of IP: " +bip);
               
                int number=Integer.parseInt(split[0]);
               
                if (number>=0 && number < 127){
                    System.out.println("\n IP Belongs to Class A");
                    subnet();
                   
                    int a[]=new int[32];
                    String s="";
                    int n1=bits+8;
                    int n2=32-n1;
                   
                    for(int i=0;i<n2;i++)
                    {
                        a[i]=0;
                        s+=a[i];
                    }
                   
                    String masking= String.format("%32s",s).replace(" ","1");
                    System.out.println("The Masking String: "+masking);
                   
                    System.out.println("\n The number of address in each subnet:"+Math.pow(2, n2));   
                }
                   
                else if (number>=128 && number<191)
                     System.out.println("\n IP Belongs to Class B");
                else if (number>=192 && number<223)
                    System.out.println("\n IP Belongs to Class C");
                else if (number>=224 && number<239)
                       System.out.println("\n IP Belongs to Class D");
                else
                    System.out.println("\n IP Belongs to Class E");
                  
                }
                
    
        Scr.close();
    }

        private static int subnet() {
            // TODO Auto-generated method stub
             Scanner Scr = new Scanner(System.in);
             System.out.println("Enter the number of equal subnets required");
             int n=Scr.nextInt();
            
             bits=(int)Math.ceil(Math.log(n)/Math.log(2));
             System.out.println("\n Number of bits required: "+bits);
            
             return bits;
            
        }
}

Output

Enter a valid network IP:
123.45.65.53

 123
Binary: 1111011
Appended form: 01111011

 45
Binary: 101101
Appended form: 00101101

 65
Binary: 1000001
Appended form: 01000001

 53
Binary: 110101
Appended form: 00110101

 Binary Equivalent of IP:  01111011001011010100000100110101

 IP Belongs to Class A
Enter the number of equal subnets required
7

 Number of bits required: 3
The Masking String: 11111111111000000000000000000000

 The number of address in each subnet:2097152.0



Comments

Post a Comment

Popular posts from this blog

Study of Differenet Network Types and Different Types of Network Cables and Practically Implement the Cross-Wired cable using Clamping Tool.

Error Detection and Correction Techniques

Challenges in e-governance