0% found this document useful (0 votes)
45 views18 pages

IP Address Class & Conversion Lab

This code takes an IP address as input and provides information about its class including the number of possible hosts and networks, and the IP address range. It extracts the first octet, determines the class based on the octet's value, and outputs the number of possible hosts and networks for that class from predefined values, as well as the class's IP address range.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views18 pages

IP Address Class & Conversion Lab

This code takes an IP address as input and provides information about its class including the number of possible hosts and networks, and the IP address range. It extracts the first octet, determines the class based on the octet's value, and outputs the number of possible hosts and networks for that class from predefined values, as well as the class's IP address range.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Winter Semester 2021-22

Course Course
Class Group Course Title Class Id Slot
Code Type

General Network and Embedded


CSE1004 VL2021220502954 L51+L52
(Semester) Communication Lab

Student Details: Student Name: MEGHA BHATTACHARYA


Roll/Reg No: 20BCE0793
Email: megha.bhattacharya2020@vitstudent.ac.in
Mobile: 6382933694

Faculty Details: Faculty Name: VENKATA PHANIKRISHNA B


School: SCOPE
Email: venkata.phanikrishna@vit.ac.in
Assessment No. 5
Assessment Title. Network Layer IP Addressing
Date of Submission 16-April-2022

1
20BCE0793 MEGHA BHATTACHARYA LAB ASSESS 05

TABLE OF CONTENTS

Setup used for coding: Online GDB Compiler -


https://www.onlinegdb.com/online_c_compiler
https://www.onlinegdb.com/online_c++_compiler

SNO QUESTION PG NO
01 Implementation to find IP 03
address class
02 Conversation of 32 bit IP 06
address to Dotted Decimal
03 Finding each class info, 10
include how many hosts are
possible, how many networks
are possible and it range

Also for few codes some sites from the internet was
referred.

2
20BCE0793 MEGHA BHATTACHARYA LAB ASSESS 05

Finding IP address class


RELATED THEORY:
The range of class-A is 128, which is 0 to 127. Here a total of 0 to 127 (i.e., 128)
networks are possible. The range of class-B is from 128 to 191. The range of
class-C is 32, which is 191 to 223. Class A, B, and C are used for the hosts.
That’s why they have NID and HID parts. Class-D and Class-E didn’t divide into
NID and HID. All bits are just IP addresses; there is no NID and HID. The range
of class-D is 28, which is 224 to 239. The range of class-E is 28, which is 240 to
255.

CODE:
#include <stdio.h>
#include <string.h>
void extractIpAddress(unsigned char *sourceString,short *ipAddress)
{
unsigned short len=0;
unsigned char oct[4]={0},cnt=0,cnt1=0,i,buf[5];

len=strlen(sourceString);
for(i=0;i<len;i++)
{
if(sourceString[i]!='.'){
buf[cnt++] =sourceString[i];
}
if(sourceString[i]=='.' || i==len-1){
buf[cnt]='\0';
cnt=0;
oct[cnt1++]=atoi(buf);
}
}
ipAddress[0]=oct[0];
ipAddress[1]=oct[1];
ipAddress[2]=oct[2];
ipAddress[3]=oct[3];
}

int main()
{

3
20BCE0793 MEGHA BHATTACHARYA LAB ASSESS 05

printf ("NAME: MEGHA BHATTACHARYA\n");


printf ("REG NO: 20BCE0793\n");
printf ("SUB: CSE 1004 - NETWORKING AND COMMUNICATION\n");
printf ("LAB ASSESSMENT: 05\n");
printf ("QUESTION 1 CODE\n\n\n\n");

unsigned char ip[20]={0};


short ipAddress[4];

printf("Enter IP Address (xxx.xxx.xxx.xxx format): ");


scanf("%s",ip);

extractIpAddress(ip,&ipAddress[0]);

printf("\nIp Address: %03d. %03d. %03d.


%03d\n",ipAddress[0],ipAddress[1],ipAddress[2],ipAddress[3]);

if(ipAddress[0]>=0 && ipAddress[0]<=127)


printf("Class A Ip Address.\n");
if(ipAddress[0]>127 && ipAddress[0]<191)
printf("Class B Ip Address.\n");
if(ipAddress[0]>191 && ipAddress[0]<224)
printf("Class C Ip Address.\n");
if(ipAddress[0]>224 && ipAddress[0]<=239)
printf("Class D Ip Address.\n");
if(ipAddress[0]>239)
printf("Class E Ip Address.\n");

return 0;
}

SCREENSHOT OF CODE IN TERMINAL:

4
20BCE0793 MEGHA BHATTACHARYA LAB ASSESS 05

OUTPUT:

5
20BCE0793 MEGHA BHATTACHARYA LAB ASSESS 05

OBSERVATION:

This program takes the dotted decimal format and tells the class of the IP
Address. The observation is as follows.

CLASS OF IP ADDRESS START RANGE END RANGE


CLASS A 0 127
CLASS B 128 191
CLASS C 192 223
CLASS D 224 239
CLASS E 240 256

Converting 32 bit IP address to Dotted decimal


number
RELATED THEORY:
• IP addresses are defined using binary notation with a 32-bit long string.
• Dotted decimal notations are used to make strings easily, in which
periods or dots separate four decimal numbers from 0 to 255 (i.e., 8
bits), describing 32 bits.
• Break 32-bit long address into segments of 8-bit blocks.
• Write decimal similar to each structure.
• Divide the blocks with periods.

CODE:
6
20BCE0793 MEGHA BHATTACHARYA LAB ASSESS 05

#include <iostream>
#include <string>
#include <vector>
using namespace std;
int binaryToDecimal(int n)
{
int num = n;
int dec_value = 0;

// Initializing base value to 1, i.e 2^0


int base = 1;

int temp = num;


while (temp)
{
int last_digit = temp % 10;
temp = temp / 10;

dec_value += last_digit * base;

base = base * 2;
}

return dec_value;
}

int main()
{
cout<<"NAME: MEGHA BHATTACHARYA\n";
cout<<"REG NO: 20BCE0793\n";
cout<<"SUB: CSE 1004 - NETWORKING AND COMMUNICATION\n";
cout<<"LAB ASSESSMENT: 05\n";
cout<<"QUESTION 2 CODE\n\n\n";

cout << "Enter the IP address:" << endl;


string ip;
cin >> ip;
string temp;
vector<int> v;

7
20BCE0793 MEGHA BHATTACHARYA LAB ASSESS 05

for (int i = 0; i <= ip.length(); i++)


{
if (ip[i] == '.' || i == ip.length())
{
int in = stoi(temp);
v.push_back(binaryToDecimal(in));
temp.clear();
}
else
{
temp.push_back(ip[i]);
}
}
cout << "Dotted decimal representation is:" << endl;
for (int i = 0; i < v.size(); i++)
{
cout << v[i];
if (i != v.size() - 1)
cout << ".";
}
}
SCREENSHOT OF CODE IN TERMINAL:

8
20BCE0793 MEGHA BHATTACHARYA LAB ASSESS 05

OUTPUT:

9
20BCE0793 MEGHA BHATTACHARYA LAB ASSESS 05

OBSERVATION:
From this program we can find the dotted decimal representation of all IP
addresses.

Finding each class information, include how


many hosts are possible, how many networks
are possible and it range
RELATED THEORY:
CLASS A:
• The range of class-A is 128, which is 0 to 127
• Here a total of 0 to 127 (i.e., 128) networks are possible.
• But practically the 0 (i.e., all 8bits zeros) and 127 (i.e., 0 followed by all
seven ones) will not be used in networking.
• Therefore, originally the number of networks present in class-A is 128,
but practically it is 126.
CLASS B:
• In NID, 16 bits, the first two bits are fixed (1,0). So with the remaining 14
bits, we can get 2^14 network IDs (NID).
• Here each network has a 2^16 Host address (IP address).That is
2^(14)*2^(16)=2^30 IP addresses.

10
20BCE0793 MEGHA BHATTACHARYA LAB ASSESS 05

• By considering fist octet 8 bits. The practical Range of Class-B is 128 to


191
CLASS C:
• In NID, 24 bits, the first three bits are fixed (1,1,0).
• So with the remaining 21 bits, we can get 2^21 network IDs (NID).
• Each network has a 2^8 (256) Host address (IP address).
• That is 2^(21)*2^(8)=2^29 IP addresses.
CLASS D:
 In class-D, 2^28 IP addresses are possible.
 Rang of Class D: 224 to 239.
 Class-D reserved for multicasting.
CLASS E:
 In Class-E, 2^28 IP addresses are possible
 Rang of Class E: 240 to 255
 Class-E is reserved for Research

CODE:
#include <iostream>
#include <string>
using namespace std;

char findClass(char str[])


{
// storing first octet in arr[] variable
char arr[4];
int i = 0;
while (str[i] != '.')
{
arr[i] = str[i];
i++;
}
i--;

// converting str[] variable into number for


// comparison
int ip = 0, j = 1;
while (i >= 0)
{
ip = ip + (str[i] - '0') * j;
j = j * 10;

11
20BCE0793 MEGHA BHATTACHARYA LAB ASSESS 05

i--;
}

// Class A
if (ip >= 1 && ip <= 126)
return 'A';

// Class B
else if (ip >= 128 && ip <= 191)
return 'B';

// Class C
else if (ip >= 192 && ip <= 223)
return 'C';

// Class D
else if (ip >= 224 && ip <= 239)
return 'D';

// Class E
else
return 'E';
}
int no_of_hosts(char cl)
{
if (cl == 'A')
return (1 << 24) - 2;
else if (cl == 'B')
return 65534;
else if (cl == 'C')
return 254;
else if (cl == 'D')
return 0;
else
return 0;
}
int no_of_network(char cl)
{
if (cl == 'A')
return 126;

12
20BCE0793 MEGHA BHATTACHARYA LAB ASSESS 05

else if (cl == 'B')


return 16384;
else if (cl == 'C')
return (1 << 21);
else if (cl == 'D')
return 0;
else
return 0;
}
void range(char cl)
{
cout << "Ranges are:" << endl;
if (cl == 'A')
cout << "0 to 127" << endl;
else if (cl == 'B')
cout << "128 to 191" << endl;
else if (cl == 'C')
cout << "192 to 223" << endl;
else if (cl == 'D')
cout << "224 to 239" << endl;
else
cout << "240 to 255" << endl;
}
int main()
{
cout<<"NAME: MEGHA BHATTACHARYA\n";
cout<<"REG NO: 20BCE0793\n";
cout<<"SUB: CSE 1004 - NETWORKING AND COMMUNICATION\n";
cout<<"LAB ASSESSMENT: 05\n";
cout<<"QUESTION 3 CODE\n\n\n";

char str[] = "192.226.12.11";


char ipclass = findClass(str);
cout << "Class of the ip address is \n"
<< ipclass << endl;
int host = no_of_hosts(ipclass);
cout << "\nNo of hosts are:\n"
<< host << endl;
int net = no_of_network(ipclass);
cout << "\nNo of networks \n"

13
20BCE0793 MEGHA BHATTACHARYA LAB ASSESS 05

<< net << endl;


cout << endl;
range(ipclass);
}
SCREENSHOT OF CODE IN TERMINAL:

14
20BCE0793 MEGHA BHATTACHARYA LAB ASSESS 05

15
20BCE0793 MEGHA BHATTACHARYA LAB ASSESS 05

16
20BCE0793 MEGHA BHATTACHARYA LAB ASSESS 05

OUTPUT:

OBSERVATION:
In this program given a IP address we can find out the class of IP address, its
range, number of hosts and ranges. The observations are as follows –

How many HID How many Host Total Number of


(IP) possible. are possible IP address

Class-A 2^24 (2^24)-2 2^31


Class-B 2^16 (2^16)-2 2^30
Class-C (2^8) (2^8) -2 2^29
Class -D Reserved Reserved Reserved
Class-E Reserved Reserved Reserved

THE END

17
20BCE0793 MEGHA BHATTACHARYA LAB ASSESS 05

DONE BY:

Student Details:
Student Name: MEGHA BHATTACHARYA
Roll/Reg No: 20BCE0793
Email: megha.bhattacharya2020@vitstudent.ac.in
Mobile: 6382933694

18

You might also like