Code in C programming only please and same output thanks, 1 IP Addressing Intern
ID: 3809768 • Letter: C
Question
Code in C programming only please and same output thanks, 1 IP Addressing Internet addresses follow the Internet Protocol (P), commonly referred to as P addresses, are 32-bit binary numbers consisting of four bit octets. For convenience, IP addresses are expressed in a "dot-decimal" notation, whereby every octet of binary numbers is converted to a decimal number from 0 to 255 The decimal numbers are separated by periods, in the form r As en example 11000000.1010 1000.01 100 101 00000010 -t 192.168.101.2 P addresses consist of two par the network pa and the host part. The first n most significant bits of the IP address represent the network part of the address, while the remaining m represent the host part (see Figure A network with m allocated to the host address can accommodate up to 2'" hosts (network devices). The IP addresses of hosts that belong to the same network have the same network part, but different host part Network Part Host Part 192.168.101 Figure 1 The network and host parts of an IP address with 24 bits devoted to the network part. All IP addresses with the same network part (first bits) belong to the same network The set of IP addresses assigned to a network are written using the first address of that followed by network, a slash character (l) and the length n of the network part of the address. For instance, network 192.168.1.0/24 is assigned IP addresses from 192.168.1.0 to 192.168.1.255 (a total of 256 The /24 denotes the length of the network part of the address, which corresponds to the 24 most significant bits of the binary representation of an IP address. In this assignment, you are to develop a program that finds all hosts that belong to the same network from a list of hosts stored in a file hosts.txt. In your program, use the following structure to store the details of a host typedef struct f int char os [8] hostExplanation / Answer
//loading packages
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
//declaring structer host_struct for storing ip address
typedef struct host_struct{
int x,y,z,w;
char os[8];
}host;
void decToBin(int decimal, char *str){
int i, binary = 0;
char strNum[8];
for(i = 0; decimal != 0 ; i++) {
binary = binary + pow(10,i) *(decimal%2);
decimal = decimal/2;
}
sprintf(strNum, "%d",binary);
int l = i;
for(i = l; i<8 ; i++) {
strcat(str,"0");
}
strcat(str,strNum);
}
host convertIP(char s[]){
host ip;
char str[8];
int length = strlength(s);
int c=1;
int j = 0;
int flagLoop= 0;
int flagNo = 0;
for(int i = 0; i < length && flagLoop == 0; i++){
if(c<4){
if(s[i] != '.'){
str[j]=s[i];
j++;
}
else{
flagNo = 1;
}
}
else if(c==4){
if(s[i] != ' '){
str[j]=s[i];
j++;
}
else {
ip.w=atoi(str);
c++;
}
}
else {
if(s[i] == 'o'){
strcpy(ip.os,"osx");
flagLoop = 1;
}
else if (s[i] == 'w'){
strcpy(ip.os,"windows");
flagLoop = 1;
}
else if (s[i] == 'l'){
strcpy(ip.os,"linux");
flagLoop = 1;
}
}
if(flagNo == 1){
switch (c){
case 1:
ip.x= atoi(str);
break;
case 2:
ip.y=atoi(str);
break;
case 3:
ip.z=atoi(str);
break;
default:
break;
}
strcpy(str," ");
c++;
j=0;
flagNo = 0;
}
}
return ip;
}
void printHost(host* ip){
if (ip == NULL) return;
printf("%d.%d.%d.%d %s ",(*ip).x,(*ip).y,(*ip).z,(*ip).w,(*ip).os);
}
void findHosts (host *net, int size, host target, int length){
host* ptr = NULL;
ptr = (host*)malloc(sizeof(host));
int num1, num2;
int i = 0;
int flag = 0;
char stringx[8];
char stringy[8];
char stringz[8];
char stringw[8];
char stringx1[8];
char stringy1[8];
char stringz1[8];
char stringw1[8];
decToBin(target.x, stringx1);
decToBin(target.y, stringy1);
decToBin(target.z, stringz1);
decToBin(target.w, stringw1);
strcat(stringx1,stringy1);
strcat(stringx1,stringz1);
strcat(stringx1,stringw1);
for(ptr = net; ptr < net + size; ptr++){
decToBin((*ptr).x, stringx);
decToBin((*ptr).y, stringy);
decToBin((*ptr).z, stringz);
decToBin((*ptr).w, stringw);
strcat(stringx,stringy);
strcat(stringx,stringz);
strcat(stringx,stringw);
for(i = 0; i <length; i++){
if(stringx[i] != stringx1[i])
flag = 1;
}
if (strcmp((*ptr).os, target.os) != 0)
flag = 1;
if (flag == 0)
printHost(ptr);
}
}
int main(void) {
host ip;
char operating_system[8];
char target[50];
int length;
int size;
char s[8];
//FILE *inp;
//inp = fopen ("host.txt", "r");
//fscanf(inp, "%s", s);
host net[50];
ip = convertIP("192.223.11.1 linux");
printHost(&ip);
printf("Enter the target IP address: ");
scanf("%s", target);
printf("Enter the oerating system, or enter “?” for any os: ? ");
scanf("%s", operating_system);
printf("Enter the lengthgth of the network part: ");
scanf("%d", &length);
// calling the function findhosts
return 0;
}