19bce0752 VL2020210504331 Ast03

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

CSE 1004: Network and

Communication (L23+L24)
Logical Addressing
NAME: ANSH SHARMA
REG NO:19BCE0752
08-04-2021

1) Write a program to check whether the given


address is IPV4 or IPV6.
Ans)

C++ program:

#include <string>
#include<iostream>
#include<conio.h>
#include <vector>
#include <bits/stdc++.h>
#include <stdio.h>
#include <stdlib.h>

using namespace std;


vector <string> split(string s, char c) {
vector <string> parts;
string part;
istringstream in (s);
while (getline( in , part, c)) {
parts.push_back(part);
}
return parts;
}

bool validIPv4(string IP) {


if (count(IP.begin(), IP.end(), '.') != 3) {
return false;
}
vector < string > parts = split(IP, '.');
if (parts.size() != 4) {
return false;
}
for (string part: parts) {
if (part.empty() || part.size() > 3 || part.size() > 1 &&
part[0] == '0') {
return false;
}
for (char c: part) {
if (!isdigit(c)) {
return false;
}
}
if (stoi(part) > 255) {
return false;
}
}
return true;
}

bool validIPv6(string IP) {


if (count(IP.begin(), IP.end(), ':') != 7) {
return false;
}
vector<string> parts = split(IP, ':');
if (parts.size() != 8) {
return false;
}
for (string part : parts) {
if (part.empty() || part.size() > 4) {
return false;
}
for (char c : part) {
if (!isdigit(c) && (!isalpha(c) || toupper(c) > 'F')) {
return false;
}
}
}

return true;
}

string validIPAddress(string IP) {


return validIPv4(IP) ? "IPv4" : (validIPv6(IP) ? "IPv6" :
"Neither");
}
int main()
{

string str;

cout<<str<<endl;

cin>>str;

string ans=validIPAddress(str);

cout<<ans<<endl;

return 0;

}
Output:
2) Write a program to determine the class,network
id,host id, default mask.
Ans)

C program:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

char determineClass(char string1[])


{
char charArray[4];
int i = 0;
while (string1[i] != '.')
{
charArray[i] = string1[i];
i++;
}

i--;
int ipAddress = 0, j = 1;

while (i >= 0)
{
ipAddress = ipAddress + (string1[i] - '0') * j;
j = j * 10;
i--;
}

if (ipAddress >=1 && ipAddress <= 126)


return 'A';

else if (ipAddress >= 128 && ipAddress <= 191)


return 'B';

else if (ipAddress >= 192 && ipAddress <= 223)


return 'C';

else if (ipAddress >= 224 && ipAddress <= 239)


return 'D';

else
return 'E';
}

void IDFinder(char string1[], char ipClass)


{
int k;
char network[12], host[12];
for (k = 0; k < 12; k++)
network[k] = host[k] = '\0';

if (ipClass == 'A')
{
int i = 0, j = 0;
while (string1[j] != '.')
network[i++] = string1[j++];
i = 0;
j++;
while (string1[j] != '\0')
host[i++] = string1[j++];
printf("Network ID is %s\n", network);
printf("Host ID is %s\n", host);
}
else if (ipClass == 'B')
{
int i = 0, j = 0, dotCount = 0;

while (dotCount < 2)


{
network[i++] = string1[j++];
if (string1[j] == '.')
dotCount++;
}
i = 0;
j++;

while (string1[j] != '\0')


host[i++] = string1[j++];

printf("Network ID is %s\n", network);


printf("Host ID is %s\n", host);
}

else if (ipClass == 'C')


{
int i = 0, j = 0, dotCount = 0;
while (dotCount < 3)
{
network[i++] = string1[j++];
if (string1[j] == '.')
dotCount++;
}

i = 0;
j++;

while (string1[j] != '\0')


host[i++] = string1[j++];

printf("Network ID is %s\n", network);


printf("Host ID is %s\n", host);
}

else
printf("In this Class, IP address is not divided into
Network and Host ID\n");
}

int main()
{
char string1[50];
scanf("%s",string1);
char ipClass = determineClass(string1);
printf("Given IP address belongs to Class %c\n",
ipClass);
IDFinder(string1, ipClass);

if(ipClass=='A')
{
printf("Default Mask is 255.0.0.0\n");
}
else if(ipClass=='B')
{
printf("Default Mask is 255.255.0.0\n");
}
else if(ipClass=='C')
{
printf("Default Mask is 255.255.255.0\n");
}

return 0;
}

Output:

You might also like