Question

Implement the following in Classless IP Addressing in C programming (b) Implement Subnetting Concepts

Implement the following in Classless IP Addressing in C programming
(b) Implement Subnetting Concepts

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Subnetting concept with C can be acheived using below code. Please execute and feel free to comment if you have any doubts.

I have attached the screenshot for the same at the end.

==========================================================================

#include <stdio.h>
#include <math.h>


int main(int argc, char *argv[]) {
  
  
printf(" Simple IP Subnet Calculator By dumiku ");

int IP1, IP2, IP3, IP4, CIDR, bitborrowed, bitremained, subnet, host, mask, blocksize;
char class;
  
//Request User input

printf("Please enter IP address in the form of "0.0.0.0": ");
  
scanf("%i.%i.%i.%i", &IP1, &IP2, &IP3, &IP4);
  
//Check if IP is valide
do
{
if((IP1 >255 || IP2 >255 || IP3 >255 || IP4 >255) || (IP1 == 127 || IP1 == 191 || IP1 == 255) || (IP1 == 0 && IP2 == 0 && IP3 == 0 && IP4 == 0))
{
  
printf(" Warning!! IP address invalid, please enter a valid IP address: ");
  
scanf("%d.%d.%d.%d", &IP1, &IP2, &IP3, &IP4);
}
}while((IP1 >255 || IP2 >255 || IP3 >255 || IP4 >255) || (IP1 == 127 || IP1 == 191 || IP1 == 255) || (IP1 == 0 && IP2 == 0 && IP3 == 0 && IP4 == 0));

//If IP valid ask for CIDR
  
printf("Enter CIDR in the format of decimal CIDR: ");
  
scanf("%d", &CIDR);
do{
//CHeck if CIDR is valid
if(CIDR > 32 || CIDR == 0)
{
  
printf("Invalid CIDR, please enter a valid CIDR: ");
  
scanf("%d", &CIDR);
}
}while(CIDR > 32);

//Let's determine IP class
if(IP1 <=170) {
class = 'A';
}else if(IP1 <=190) {
class = 'B';
}else {
class = 'C';
}
  
// Let find the number of bits borrowed
switch (class) {
case 'A':
if(CIDR >= 8) {
bitborrowed = CIDR - 8;
}else {
bitborrowed = 8 - CIDR;
}
break;
case 'B':
if(CIDR >= 16) {
bitborrowed = CIDR - 16;
}else {
bitborrowed = 16 - CIDR;
}
break;
case 'C':
if(CIDR >= 24) {
bitborrowed = CIDR - 24;
}else {
bitborrowed = 24 -CIDR;
}
}
  
//Let Find the number of bits that remained
switch (class) {
case 'A':
bitremained = 32 - CIDR;
break;
case 'B':
bitremained = 32 -CIDR;
break;
case 'C':
bitremained = 32 -CIDR;
break;
}
  
//Now Let Calculate the number of Subnets
subnet = pow(2,bitborrowed);
  
//And then we calculate the number of usable hosts
host = pow(2, bitremained)-2;
  
//Here we determine the block size
switch(bitborrowed) {
case 1:
blocksize = 128;
break;
case 2:
blocksize = 64;
break;
case 3:
blocksize = 32;
break;
case 4:
blocksize = 16;
break;
case 5:
blocksize = 8;
break;
case 6:
blocksize = 4;
break;
case 7:
blocksize = 2;
break;
case 8:
blocksize = 1;
break;
}
  
printf("Here is your Result: ");
  
  
printf("your IP %i.%i.%i.%i/%i is of class %c ", IP1,IP2,IP3,IP4,CIDR,class);

printf("Number of bit borrowed is: %d ", bitborrowed);
  
printf("Number of bits remained is: %d ", bitremained);
  
printf("Subnet: %d ", subnet);
  
printf("host: %d ", host);
  
printf("blocksize: %d ", blocksize);
return (0);
}

===============================================================================================

Add a comment
Know the answer?
Add Answer to:
Implement the following in Classless IP Addressing in C programming (b) Implement Subnetting Concepts
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT