Open In App

Subnet Calculator in C++

Last Updated : 10 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

A Subnet Calculator in C++ is a program that allows users to calculate subnet masks, network addresses, broadcast addresses, and the range of the valid host addresses within a given IP address range and subnet mask and Subnetting is the technique used in computer networking to divide a large IP address space into smaller or more manageable subnetworks and The purpose of subnetting is to allocate IP addresses and optimize network resources efficiently. It enables the creation of multiple smaller networks within the larger network.

Important Points Subnet Calculator in C++

Below are some key functionalities that such a calculator should provide:

  • IP Address Input: The calculator should allow users to input an IP address typically in the format of the four octets separated by dots.
  • Subnet Mask Input: The Users should be able to input a subnet mask in either decimal format or CIDR notation.
  • Subnet Information: The calculator should provide essential information about the subnet, including:
    1. Network Address: The first address in the subnet.
    2. Broadcast Address: The last address in the subnet.
  • Usable IP Range: The range of IP addresses that can be assigned to devices on the subnet, excluding network and broadcast addresses.
  • CIDR Notation: If a subnet mask is entered in the CIDR notation the calculator should be able to convert it to decimal subnet mask and vice versa.
  • Validation: The calculator should validate user inputs to ensure they are in the correct format and within valid ranges.
  • Error Handling: Provide appropriate error messages or responses if the user inputs are invalid.
  • Subnetting: Users should have the option to subnet further within a given subnet and calculate sub-subnet information.
  • Reverse DNS Lookup: The calculator can perform the reverse DNS lookups to provide domain names for the IP addresses.

Key Features of Subnet Calculator

  • Calculates network and broadcast addresses for the each subnet.
  • Determines the range of usable IP addresses in each subnet.
  • Supports both IPv4 and IPv6 addresses.
  • Displays subnet information in user-friendly format.

C++ Program for Subnet Calculator

Below is the implementation of the Subnet Calculator in C++ programming language.

C++




// C++ Program to implement
// Subnet Calculator
#include <bitset>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
  
using namespace std;
  
// function
void GFG(const string& ipAddress, const string& subnetMask)
{
    // Parse IP address and subnet mask
    vector<int> ipParts;
    vector<int> maskParts;
  
    istringstream ipStream(ipAddress);
    istringstream maskStream(subnetMask);
    string ipPart;
  
    while (getline(ipStream, ipPart, '.')) {
        ipParts.push_back(stoi(ipPart));
    }
  
    string maskPart;
    while (getline(maskStream, maskPart, '.')) {
        maskParts.push_back(stoi(maskPart));
    }
  
    // Calculate network address
    vector<int> networkAddress;
    for (size_t i = 0; i < ipParts.size(); i++) {
        networkAddress.push_back(ipParts[i] & maskParts[i]);
    }
    vector<int> broadcastAddress;
    for (size_t i = 0; i < ipParts.size(); i++) {
        broadcastAddress.push_back(
            ipParts[i] | (~maskParts[i] & 0xFF));
    }
  
    // Calculate usable IP range
    networkAddress[3]++;
    broadcastAddress[3]--;
  
    // Display subnet details
    cout << "Subnet Details:" << endl;
  
    cout << "Network Address: ";
    for (int i = 0; i < 4; i++) {
        cout << networkAddress[i];
        if (i < 3) {
            cout << ".";
        }
    }
    cout << endl;
  
    cout << "Broadcast Address: ";
    for (int i = 0; i < 4; i++) {
        cout << broadcastAddress[i];
        if (i < 3) {
            cout << ".";
        }
    }
    cout << endl;
  
    cout << "Subnet Mask: " << subnetMask << endl;
    cout << "Usable IP Range: ";
    for (int i = 0; i < 4; i++) {
        cout << networkAddress[i];
        if (i < 3) {
            cout << ".";
        }
    }
    cout << " - ";
    for (int i = 0; i < 4; i++) {
        cout << broadcastAddress[i];
        if (i < 3) {
            cout << ".";
        }
    }
    cout << endl;
}
  
// main function
int main()
{
    string ipAddress, subnetMask;
    cout << "Enter IP Address: ";
    cin >> ipAddress;
  
    cout << "Enter Subnet Mask: ";
    cin >> subnetMask;
    GFG(ipAddress, subnetMask);
  
    return 0;
}


Output

Enter IP Address: 192.168.1.10
Enter Subnet Mask: 255.255.255.0
Subnet Details:
Network Address: 192.168.1.1
Broadcast Address: 192.168.1.254
Subnet Mask: 255.255.255.0
Usable IP Range: 192.168.1.1 - 192.168.1.254


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads