Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * [subnetCalculator.hpp]
- *
- * Subnet Calculator header file.
- *
- * by Rocco Castoro.
- */
- #ifndef __SUBNET_CALCULATOR_H__ // Include guard.
- #define __SUBNET_CALCULATOR_H__
- //#include "stdafx.h" // M$ PCH stuff.
- #include <iostream>
- #include <string>
- #include <vector>
- #include <sstream>
- #include <cstdio>
- #include <cstdlib>
- #include <cmath>
- using std::cin; // Beats the crap outta the general std namespace use convention...
- using std::cout;
- using std::endl;
- using std::string;
- using std::vector;
- using std::stringstream;
- using std::ostringstream; //...
- /**
- * The getOctetsIP() function:
- */
- int getOctetsIP(string ip, vector<int> &octetsIP) // Define vector<int> octets, using reference from main.
- {
- stringstream sip(ip); // Use stringstream named sip and populate with IP.
- string temp;
- octetsIP.clear(); // Clears the octectsIP vector, in case the main function re-runs this function.
- vector<bool> ipInRange;
- while(getline(sip, temp, '.')) // Every time getline() receives new stream element from sip, it saves it to temp, until it reaches the '.' delimiter...
- octetsIP.push_back(atoi(temp.c_str())); // ...then push_back octet with new element.
- if(octetsIP.size() == 4)
- {
- for(int i = 0; i < octetsIP.size(); i++)
- if(octetsIP[i] >= 0 && octetsIP[i] <= 255)
- ipInRange.push_back(true);
- else
- ipInRange.push_back(false);
- if(ipInRange[0] == true && ipInRange[1] == true && ipInRange[2] == true && ipInRange[3] == true)
- return 0;
- else
- {
- cout << endl << "There are only 255 bits per octet. Please re-enter IP." << endl << endl;
- return 1;
- }
- }
- else
- {
- cout << endl << "Please enter four octets in dot (.) notation." << endl << endl;
- return 1;
- }
- }
- /**
- * The getOctetsMask() function:
- */
- int getOctetsMask(string mask, vector<int> &octetsMask)
- {
- stringstream smask(mask);
- string temp;
- octetsMask.clear(); // Clears the octetsMask vector, in case the main function re-runs this function.
- vector<bool> maskInRange;
- while(getline(smask, temp, '.'))
- octetsMask.push_back(atoi(temp.c_str()));
- if(octetsMask.size() == 4)
- {
- for(int i = 0; i < octetsMask.size(); i++)
- switch(octetsMask[i])
- {
- case 0:
- case 128:
- case 192:
- case 224:
- case 240:
- case 248:
- case 252:
- case 254:
- case 255:
- maskInRange.push_back(true);
- break;
- default:
- maskInRange.push_back(false);
- break;
- }
- if(maskInRange[0] == true && maskInRange[1] == true && maskInRange[2] == true && maskInRange[3] == true)
- return 0;
- else
- {
- cout << endl << "Subnet masks only use 2^[0-7]. Please re-enter mask." << endl << endl;
- return 1;
- }
- }
- else
- {
- cout << endl << "Please enter four octets in dot (.) notation." << endl << endl;
- return 1;
- }
- }
- /**
- * The calcCLass() function:
- */
- int calcClass(vector<int> &octetsIP)
- {
- if(octetsIP[0] == 10)
- return 1; // Class A private address blocks.
- else if(octetsIP[0] == 172 && octetsIP[1] >= 16 && octetsIP[1] <= 31)
- return 2; // Class B private address blocks.
- else if(octetsIP[0] == 192 && octetsIP[1] == 168)
- return 3; // Class C private address blocks.
- else if(octetsIP[0] == 127)
- return 4; // Loopback Address reserved address blocks.
- else if(octetsIP[0] >= 0 && octetsIP[0] < 127)
- return 5;
- else if(octetsIP[0] > 127 && octetsIP[0] < 192)
- return 6;
- else if(octetsIP[0] > 191 && octetsIP[0] < 224)
- return 7;
- else if(octetsIP[0] > 223 && octetsIP[0] < 240)
- return 8;
- else if(octetsIP[0] > 239 && octetsIP[0] <= 255)
- return 9;
- else
- return 0; // Out of range.
- }
- /**
- * The getNHBits() function:
- *
- * Determine binary.
- */
- int getNHBits(vector<int> &octetsIP, vector<int> &octetsMask, vector<int> &octetsIPBits, vector<int> &octetsMaskBits)
- {
- /**
- * Get IP binary rep:
- */
- cout << "------------------------------------------" << endl;
- cout << "///////// Binary Representation //////////" << endl;
- cout << "------------------------------------------" << endl;
- for(int j = 0; j < octetsIP.size(); j++)
- {
- if(j > 0)
- cout << ".";
- for(int mask = 128; mask; mask >>= 1)
- {
- octetsIPBits.push_back((octetsIP[j] & mask) != 0);
- cout << ((octetsIP[j] & mask) != 0);
- }
- }
- cout << " : IP Address" << endl;
- /**
- * Get SUBNET binary rep:
- */
- for(int j = 0; j < octetsMask.size(); j++)
- {
- if(j > 0)
- cout << ".";
- for(int mask = 128; mask; mask >>= 1)
- {
- octetsIPBits.push_back((octetsMask[j] & mask) != 0);
- cout << ((octetsMask[j] & mask) != 0);
- }
- }
- cout << " : Subnet Mask" << endl;
- cout << "------------------------------------------";
- return 0;
- }
- /**
- * The getNetID() function:
- *
- * Perform ANDing of IP and Subnet Mask to generate Network ID range.
- */
- vector<int> getNetID(vector<int> &octetsIPBits, vector<int> &octetsMaskBits)
- {
- vector<int> netID;
- for(int j = 0; j < octetsIPBits.size(); j++)
- {
- if((j > 0) && (j % 8 == 0))
- cout << ".";
- netID.push_back(octetsIPBits[j] & octetsMaskBits[j]);
- }
- return netID;
- }
- /**
- * The toString() function:
- *
- * Turn Binary to String.
- */
- string toString(vector<int> octets)
- {
- ostringstream octStrm;
- for(int j = 0; j < octets.size(); j++)
- {
- if(j > 0)
- octStrm << '.';
- octStrm << octets[j];
- }
- return octStrm.str();
- }
- /**
- * The toDecimal() function:
- *
- * Turn String back to Decimal.
- */
- vector<int> toDecimal(vector<int> octets, vector<int> &decimals)
- {
- stringstream octStrm;
- decimals.clear();
- for(int j = 0; j < octets.size(); j++)
- {
- if(j > 0)
- octStrm << '.';
- octStrm << octets[j];
- }
- for(string temp = ""; getline(octStrm, temp, '.'); )
- decimals.push_back(atoi(temp.c_str()));
- return decimals;
- }
- /**
- * The getIncrement() function:
- *
- * Get the network increment.
- */
- int getIncrement(vector<int> decimalMask, vector<int> decimalNetID)
- {
- int increment = 0;
- for(int i = 0; i < decimalMask.size(); i++)
- {
- switch(decimalMask[i])
- {
- case 255:
- increment = 1;
- break;
- case 254:
- increment = 2;
- break;
- case 252:
- increment = 4;
- break;
- case 248:
- increment = 8;
- break;
- case 240:
- increment = 16;
- break;
- case 224:
- increment = 32;
- break;
- case 192:
- increment = 64;
- break;
- case 128:
- increment = 128;
- break;
- default:
- break;
- }
- }
- return increment;
- }
- /**
- * The getNetIDRange() function:
- *
- * Get network ID range.
- */
- vector<int> getNetIDRange(vector<int> &decimalNetID, int &netInc, vector<int> &decimalMask)
- {
- vector<int> netIDEnd;
- for(int i = 0; i < decimalNetID.size(); i++)
- if(decimalMask[i] == 255)
- netIDEnd.push_back(decimalNetID[i]);
- else if(decimalMask[i] < 255 && decimalMask[i] > 0)
- netIDEnd.push_back((decimalNetID[i] + netInc) - 1);
- else
- netIDEnd.push_back(255);
- return netIDEnd;
- }
- /**
- * The getSubnets() function:
- *
- * Get subnets.
- */
- int getSubnets(vector<int> &decimalMask, int &ipClass, vector<int> &subClassMask)
- {
- int netBits = 0;
- subClassMask.clear();
- switch(ipClass)
- {
- case 1:
- subClassMask.push_back(255);
- subClassMask.push_back(0);
- subClassMask.push_back(0);
- subClassMask.push_back(0);
- break;
- case 2:
- subClassMask.push_back(255);
- subClassMask.push_back(255);
- subClassMask.push_back(0);
- subClassMask.push_back(0);
- break;
- case 3:
- subClassMask.push_back(255);
- subClassMask.push_back(255);
- subClassMask.push_back(255);
- subClassMask.push_back(0);
- break;
- case 4:
- case 5:
- subClassMask.push_back(decimalMask[0]);
- subClassMask.push_back(decimalMask[1]);
- subClassMask.push_back(decimalMask[2]);
- subClassMask.push_back(decimalMask[3]);
- break;
- default:
- break;
- }
- for(int i = 0; i < decimalMask.size(); i++)
- if(decimalMask[i] != subClassMask[i])
- switch(decimalMask[i])
- {
- case 255:
- netBits += 8;
- continue;
- case 254:
- netBits += 7;
- continue;
- case 252:
- netBits += 6;
- continue;
- case 248:
- netBits += 5;
- continue;
- case 240:
- netBits += 4;
- continue;
- case 224:
- netBits += 3;
- continue;
- case 192:
- netBits += 2;
- continue;
- case 128:
- netBits += 1;
- continue;
- case 0:
- netBits += 0;
- continue;
- default:
- netBits += 0;
- break;
- }
- return (pow(2.0, netBits));
- }
- /**
- * The getHostsPerSubnet() function:
- *
- * Get hosts per subnet.
- */
- int getHostsPerSubnet(vector<int> &decimalMask)
- {
- int hostBits = 0;
- for(int i = 0; i < decimalMask.size(); i++)
- switch(decimalMask[i])
- {
- case 255:
- hostBits += 0;
- continue;
- case 254:
- hostBits += 1;
- continue;
- case 252:
- hostBits += 2;
- continue;
- case 248:
- hostBits += 3;
- continue;
- case 240:
- hostBits += 4;
- continue;
- case 224:
- hostBits += 5;
- continue;
- case 192:
- hostBits += 6;
- continue;
- case 128:
- hostBits += 7;
- continue;
- case 0:
- hostBits += 8;
- continue;
- default:
- hostBits += 0;
- break;
- }
- return (pow(2.0, hostBits) - 2);
- }
- #endif // __SUBNET_CALCULATOR_H__
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement