Computer Networking Assignment
Course title: Computer Networking.
Course code: CSE4011.2.
Date of Submission:
24 May, 2024
Submitted to-
Hon. Mohammad Ashraful Hoque
Lecturer
Department of Computer Science and Engineering
Sl Class Roll Name
01 2018200010041 Jahidul Islam
Problem Statement
Writing a program in C that will calculate VLSM from a given IP or a predetermined IP
address.
Input:
1. Number of networks.
2. For each network: number of hosts.
3. Root IP address (optional).
Output:
For each network:
1. Network address.
2. Subnet mask.
3. Default gateway.
4. Broadcast address.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <limits.h>
// Define the structure to store an IP address
typedef struct {
unsigned char bytes[4];
} IPAddress;
// Function to print an IP address
void printIPAddress(IPAddress ip) {
printf("%d.%d.%d.%d", ip.bytes[0], ip.bytes[1], ip.bytes[2],
ip.bytes[3]);
}
// Function to validate and parse an IP address from a string
int validateIPAddress(const char *ipStr, IPAddress *ip) {
return sscanf(ipStr, "%hhu.%hhu.%hhu.%hhu", &ip->bytes[0],
&ip->bytes[1], &ip->bytes[2], &ip->bytes[3]) == 4;
}
// Function to calculate the subnet mask for a given number of
hosts
IPAddress calculateSubnetMask(int hostCount) {
int bits = (int)ceil(log2(hostCount + 2)); // +2 for network
and broadcast addresses
int subnetBits = 32 - bits;
IPAddress subnetMask = {0, 0, 0, 0};
for (int i = 0; i < subnetBits; i++) {
subnetMask.bytes[i / 8] |= (1 << (7 - (i % 8)));
}
return subnetMask;
}
// Function to calculate the broadcast address given a network
address and subnet mask
IPAddress calculateBroadcastAddress(IPAddress networkAddress,
IPAddress subnetMask) {
IPAddress broadcastAddress;
for (int i = 0; i < 4; i++) {
broadcastAddress.bytes[i] = networkAddress.bytes[i] |
(~subnetMask.bytes[i] & 0xFF);
}
return broadcastAddress;
}
// Function to calculate the next network address given a network
address and subnet mask
IPAddress calculateNextNetwork(IPAddress networkAddress,
IPAddress subnetMask) {
IPAddress nextNetwork = networkAddress;
int carry = 1;
for (int i = 3; i >= 0; i--) {
int maskByte = ~subnetMask.bytes[i] & 0xFF;
int increment = maskByte + 1;
nextNetwork.bytes[i] += increment * carry;
carry = (nextNetwork.bytes[i] > 255);
if (nextNetwork.bytes[i] > 255) {
nextNetwork.bytes[i] -= 256;
}
}
return nextNetwork;
}
int main() {
int numNetworks;
// Prompt the user for the number of networks
printf("Enter the number of networks: ");
if (scanf("%d", &numNetworks) != 1 || numNetworks <= 0) {
fprintf(stderr, "Invalid number of networks.\n");
return 1;
}
int hosts[numNetworks];
for (int i = 0; i < numNetworks; i++) {
// Prompt the user for the number of hosts for each
network
printf("Enter the number of hosts for network %d: ", i +
1);
if (scanf("%d", &hosts[i]) != 1 || hosts[i] <= 0) {
fprintf(stderr, "Invalid number of hosts for network
%d.\n", i + 1);
return 1;
}
}
IPAddress rootIP = {192, 168, 0, 0}; // Default root IP
address
char userInput[16];
// Prompt the user for the root IP address
printf("Enter the root IP address (press Enter to use default
192.168.0.0): ");
getchar(); // Consume newline left in buffer
fgets(userInput, 16, stdin);
if (userInput[0] != '\n') {
if (!validateIPAddress(userInput, &rootIP)) {
fprintf(stderr, "Invalid IP address format.\n");
return 1;
}
}
IPAddress networkAddress = rootIP;
for (int i = 0; i < numNetworks; i++) {
int hostCount = hosts[i];
int requiredIPs = (int)pow(2, ceil(log2(hostCount + 2)));
IPAddress subnetMask = calculateSubnetMask(requiredIPs -
2);
IPAddress broadcastAddress =
calculateBroadcastAddress(networkAddress, subnetMask);
IPAddress defaultGateway = networkAddress;
defaultGateway.bytes[3] += 1;
// Display the calculated network details with
explanations
printf("\nNetwork %d:\n", i + 1);
printf("Network Address: ");
printIPAddress(networkAddress);
printf("\nSubnet Mask: ");
printIPAddress(subnetMask);
printf("\nDefault Gateway: ");
printIPAddress(defaultGateway);
printf("\nBroadcast Address: ");
printIPAddress(broadcastAddress);
printf("\nExplanation:\n");
printf("- For %d hosts, we need at least %d IP addresses,
which means we allocate a block of %d IP addresses.\n",
hostCount, requiredIPs, requiredIPs);
printf("- Network address %d.%d.%d.%d to %d.%d.%d.%d is
assigned to network %d.\n",
networkAddress.bytes[0], networkAddress.bytes[1],
networkAddress.bytes[2], networkAddress.bytes[3],
broadcastAddress.bytes[0],
broadcastAddress.bytes[1], broadcastAddress.bytes[2],
broadcastAddress.bytes[3],
i + 1);
networkAddress = calculateNextNetwork(networkAddress,
subnetMask);
}
return 0;
}
Explanation
1. Structure Definition:
● IPAddress: A struct to store an IP address using four bytes.
2. Functions:
● printIPAddress(IPAddress ip): Prints an IP address in the dotted-decimal
format.
● validateIPAddress(const char *ipStr, IPAddress *ip): Validates and
parses a string into an IPAddress struct.
● calculateSubnetMask(int hostCount): Calculates the subnet mask based
on the number of required hosts.
● calculateBroadcastAddress(IPAddress networkAddress, IPAddress
subnetMask): Calculates the broadcast address for a given network.
● calculateNextNetwork(IPAddress networkAddress, IPAddress
subnetMask): Calculates the next network address based on the current
network and subnet mask.
3. Main Function:
● Prompts the user for the number of networks and the number of hosts
for each network.
● Validates the input and provides error messages if the input is invalid.
● Prompts the user for the root IP address and validates it.
● Calculates and displays the network address, subnet mask, default
gateway, and broadcast address for each subnet.