Author Topic: [C++] Subnet Calculator  (Read 2900 times)

0 Members and 1 Guest are viewing this topic.

Offline flowjob

  • Knight
  • **
  • Posts: 327
  • Cookies: 46
  • Pastafarian
    • View Profile
[C++] Subnet Calculator
« on: July 29, 2014, 10:35:59 pm »
NetCalc Subnet Calculator

I wrote a little python script some months ago for school, as I was to lazy to calculate all that stuff by myself again and again...
It calculates the most important stuff like
  • netmask/cidr
  • maschine part (= inversed netmask)
  • max-number of possible hosts
  • broadcast addresse
  • default gateway

Usage:
Compile and execute as usual, enter an IP, then enter a netmask, if you don't enter anything as netmask then the program will ask you for the CIDR instead.

Source Code:
Code: (c++) [Select]
/*
 * Author: flowjob [from EvilZone.org]
 * Version: 0.2
 * Description:
 * C++ port of a python script
 * I wrote for an IT class, that
 * calculates all the values I needed
 * based on the IP and a CIDR/Subnet.
 */

#include <iostream>
#include <string>
#include <sstream>
#include <cstdlib>
using namespace std;


struct addr_t {
string notation;
unsigned int value;
};


int calcIPValue( string ipaddr ) {
/* Calculates the binary value
* of the IP Adresse */
string buf;
int values[4];
unsigned int binary = 0;
int i = 0;
ipaddr += '.';
for ( char c : ipaddr ) {
if ( c != '.' ) {
buf += c;
}
else {
stringstream(buf) >> values[i];
buf = "";
i++;
}
}
i = 0;
for ( int value : values ) {
binary |=  value << ( 8*(3-i) );
i++;
}
return binary;
}


string calcIPNotation( unsigned int binary ) {
/* Calculates the IP-Notation
* given its value */
string notation;
for (int i = 0; i < 4; i++) {
int shift = 255 << ( 8*(3-i) );
unsigned int part = binary & shift;
part >>= 8*(3-i);
ostringstream value;
if ( i != 0 ) {
value << '.';
}
value << part;
notation += value.str();
}
return notation;
}


addr_t calcSubnet( int cidr ) {
/* Calculate the binary value
* given the CIDR */
addr_t subnet;
subnet.value = 4294967295 << ( 32-cidr );
subnet.notation = calcIPNotation(subnet.value);
// 4294967295 sets all bits to 1, int should be 4 bytes!
return subnet;
}


int calcCIDR( addr_t subnet ) {
/* Calculate the CIDR given
* the binary value of the SUbnet */
int cidr = 0;
while ( subnet.value != 0 ) {
subnet.value = subnet.value << 1;
cidr += 1;
}
return cidr;
}


addr_t calcNetPart( addr_t *ipaddr, addr_t *subnet ) {
/* Calculates the network-part
* of the IP Addresse */
addr_t netpart;
netpart.value = ipaddr->value & subnet->value;
netpart.notation = calcIPNotation(netpart.value);
return netpart;
}


addr_t calcMacPart( addr_t *subnet ) {
/* Calculates the machine-part
* of the IP Addresse */
addr_t macpart;
macpart.value = ~subnet->value;
macpart.notation = calcIPNotation(macpart.value);
return macpart;
}


addr_t calcBroadCast( addr_t *netpart, addr_t *macpart ) {
/* Calculates the Broadcast
* Addresse of the network */
addr_t broadcast;
broadcast.value = netpart->value | macpart->value;
broadcast.notation = calcIPNotation(broadcast.value);
return broadcast;
}


addr_t calcDefaultGW( addr_t *netpart ) {
/* Calculates the Default-Gateway
* of the network */
addr_t defaultgw;
defaultgw.value = netpart->value + 1;
defaultgw.notation = calcIPNotation(defaultgw.value);
return defaultgw;
}


int calcHostNum( addr_t *macpart ) {
/* Calculates the number of
* available Addresses */
unsigned int hostnum = macpart->value + 1;
return hostnum;
}


int main( int argc, char **argv ) {
cout << "Enter an IP-Address and a Netmask/CIDR..." << endl;

addr_t ipaddr;
addr_t subnet;
int cidr;
addr_t macpart;
int hostnum;
addr_t netpart;
addr_t broadcast;
addr_t defaultgw;

cout << "IP-Address? ";
getline(cin,ipaddr.notation);
ipaddr.value = calcIPValue(ipaddr.notation);
cout << "Netmask?    ";
getline(cin,subnet.notation);
if ( subnet.notation == "" ) {
cout << "CIDR?       ";
cin >> cidr;
subnet = calcSubnet(cidr);
subnet.notation = calcIPNotation(subnet.value);
}
else {
subnet.value = calcIPValue(subnet.notation);
cidr = calcCIDR(subnet);
}
macpart = calcMacPart(&subnet);
hostnum = calcHostNum(&macpart);
netpart = calcNetPart(&ipaddr, &subnet);
broadcast = calcBroadCast(&netpart , &macpart);
defaultgw = calcDefaultGW(&netpart);

cout << "=================================" << endl;
cout << "IPv4:             " << ipaddr.notation << endl;
cout << "Netmask:          " << subnet.notation << endl;
cout << "CIDR:             " << cidr << endl;
cout << "Inversed Netmask: " << macpart.notation << endl;
cout << "Hosts:            " << hostnum << endl;
cout << "Network:          " << netpart.notation << endl;
cout << "Broadcast:        " << broadcast.notation << endl;
cout << "Default-Gateway:  " << defaultgw.notation << endl;
cout << "=================================" << endl;

return EXIT_SUCCESS;
}

That script was the first thing I wrote in C++ except "Hello, World" so it isn't that advanced.
The original python script is here >> netcalc.py
Quote
<phil> I'm gonna DDOS the washing machine with clothes packets.
<deviant_sheep> dont use too much soap or youll cause a bubble overflow