Subnet Calculator

I wanted to test my knowledge - and create a useful tool for me and others in IT, so I developed over the past 3 days this subnet calculator, it works good. If you find anything bugs with what the program outputs let me know. I'm not interested in criticism about my syntax or use of the language, I know now that every project over 100 lines needs a planning phase (which I will do from now on). This looks good when it runs at least! Give a compile and test

Sorry for the extremely uneasy to read and unnecessarily complex code - I'm only 2 1/2 weeks into learning C++. Thanks!

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189`` ``````// Subnet Calculator, by rocco castoro #include "stdafx.h" #include #include #include #include #include #include using namespace std; int getOctetsIP(string ip, vector &octetsIP) { // Define vector octets, using reference from main stringstream sip(ip); // use stringstream named ss and populate with ip string temp; octetsIP.clear(); // Clears the octetsMask vector, in case main function re-runs this function vector ipInRange; while (getline(sip,temp,'.')) // Every time getline recieves new stream element from ss, save to temp octetsIP.push_back(atoi(temp.c_str())); //... until reaches '.' delimiter, 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; } } int getOctetsMask(string mask, vector &octetsMask) { stringstream smask(mask); string temp; octetsMask.clear(); // Clears the octetsMask vector, in case main function re-runs this function vector maskInRange; while (getline(smask,temp,'.')) octetsMask.push_back(atoi(temp.c_str())); if (octetsMask.size() == 4){ for(int i = 0; i < octetsMask.size(); i++){ if (octetsMask[i] == 0 || octetsMask[i] == 128 || octetsMask[i] == 192 || octetsMask[i] == 224 || octetsMask[i] == 240 || octetsMask[i] == 248 || octetsMask[i] == 252 || octetsMask[i] == 254 || octetsMask[i] == 255) maskInRange.push_back(true); else maskInRange.push_back(false); } if(maskInRange[0]==true&&maskInRange[1]==true&&maskInRange[2]==true&&maskInRange[3]==true){ return 0; }else{ cout << endl << "Subnet masks only use 2^1-8. Please re-enter mask." << endl << endl; return 1; } }else{ cout << endl << "Please enter four octets in dot notation." << endl << endl; return 1; } } int calcClass(vector &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 // } } // Determine Binary /-- int getNHBits(vector &octetsIP, vector &octetsMask, vector &octetsIPBits, vector &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 << "."; int mask = 128; while (mask) { octetsIPBits.push_back((octetsIP[j] & mask) != 0); cout << ((octetsIP[j] & mask) != 0); mask >>= 1; } } cout << " : IP Address" << endl; // Get SUBNET binary rep. // for (int j=0; j < octetsMask.size(); j++) { if (j>0) cout << "."; int mask = 128; while (mask) { octetsMaskBits.push_back((octetsMask[j] & mask) != 0); cout << ((octetsMask[j] & mask) != 0); mask >>= 1; } } cout << " : Subnet Mask" << endl; cout << "-----------------------------------------" << endl; return 0; } // Perform ANDing of IP and Subnet Mask to generate Network ID range // vector getNetID(vector &octetsIPBits, vector &octetsMaskBits){ vector 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; } // Turn Binary back to Decimal string toString(vector octets){ ostringstream octStrm; for(int j = 0; j < octets.size(); j++) { if (j>0) octStrm << '.'; octStrm << octets[j]; } return octStrm.str(); } // Turn Binary back to Decimal vector toDecimal(vector octets, vector &decimals){ stringstream octStrm; decimals.clear(); for(int j = 0; j < octets.size(); j++) { if (j>0) octStrm << '.'; octStrm << octets[j]; } string temp; while (getline(octStrm, temp, '.')) decimals.push_back(atoi(temp.c_str())); return decimals; }``````

Last edited on
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274`` `````` // Get the network increment // int getIncrement(vector decimalMask, vector decimalNetID){ int increment = 0; for (int i=0; i getNetIDRange(vector &decimalNetID, int &netInc, vector &decimalMask) { vector netIDEnd; for (int i=0; i 0){ netIDEnd.push_back( (decimalNetID[i] + netInc) - 1); }else{ netIDEnd.push_back(255); } } return netIDEnd; } // Get subnets int getSubnets(vector &decimalMask, int &ipClass, vector &subClassMask){ int netBits = 0; subClassMask.clear(); if (ipClass==1){ subClassMask.push_back(255); subClassMask.push_back(0); subClassMask.push_back(0); subClassMask.push_back(0); }else if(ipClass==2){ subClassMask.push_back(255); subClassMask.push_back(255); subClassMask.push_back(0); subClassMask.push_back(0); }else if(ipClass==3){ subClassMask.push_back(255); subClassMask.push_back(255); subClassMask.push_back(255); subClassMask.push_back(0); }else if(ipClass==4 || ipClass==5){ subClassMask.push_back(decimalMask[0]); subClassMask.push_back(decimalMask[1]); subClassMask.push_back(decimalMask[2]); subClassMask.push_back(decimalMask[3]); } for (int i=0; i &decimalMask){ int hostBits = 0; for (int i=0; i octetsIP; while (getOctetsIP(ip, octetsIP) == 1) { cout << "Enter IPv4 Address -> "; (getline(cin, ip)); // Accept user input for IP Address // } // Get subnet mask octets // string mask; vector octetsMask; while (getOctetsMask(mask, octetsMask) == 1) { cout << endl << "Enter subnet mask for " << ip << " -> "; (getline(cin, mask)); // Accept user input for subnet mask // } cout << endl << endl << endl << endl << endl; // Print Initial User IP and Subnet Mask // vector decimals; cout << "//////////////////////////////////////////" << endl; cout << "/// IP Address: " << toString(octetsIP) << endl; vector decimalMask = toDecimal(octetsMask, decimals); cout << "/// Subnet Mask: " << toString(octetsMask) << endl; cout << "//////////////////////////////////////////" << endl << endl; // Print Binary Representation // vector octetsIPBits; vector octetsMaskBits; getNHBits(octetsIP, octetsMask, octetsIPBits, octetsMaskBits); vector netID = getNetID(octetsIP, octetsMask); vector decimalNetID = toDecimal(netID, decimals); int netInc = getIncrement(decimalMask, decimalNetID); cout << endl; // Print IP Class // Run function to determine and print IP class cout << "------------------------------------------" << endl; cout << "//////////// Class Information ///////////" << endl; cout << "------------------------------------------" << endl; int classResult = calcClass(octetsIP); int ipClass = 0; switch (classResult){ case 1: cout << "IP Class: Private block, Class 'A' " << endl; ipClass = 1; break; case 2: cout << "IP Class: Private block, Class 'B'" << endl; ipClass = 2; break; case 3: cout << "IP Class: Private block, Class 'C'" << endl; ipClass = 3; break; case 4: cout << "IP Class: Reserved block, System Loopback Address" << endl; ipClass = 1; break; case 5: cout << "IP Class: A" << endl; ipClass = 1; break; case 6: cout << "IP Class: B" << endl; ipClass = 2; break; case 7: cout << "IP Class: C" << endl; ipClass = 3; break; case 8: cout << "IP Class: D" << endl; ipClass = 4; cout << "!! This is a reserved Class D Multicast IP Address Block" << endl; break; case 9: cout << "IP Class: E" << endl; ipClass = 5; cout << "!! This is a reserved Class E Multicast IP Address Block" << endl; break; default : cout << "Not in Range" << endl; break; } vector subClassMask; getSubnets(decimalMask, ipClass, subClassMask); cout << "Default Class Subnet Mask: " << toString(subClassMask) << endl; cout << "-----------------------------------------" << endl << endl; // Print Subnetting Details // cout << "------------------------------------------" << endl; cout << "///////////// Subnet Details /////////////" << endl; cout << "------------------------------------------" << endl; vector netIDRange = getNetIDRange(decimalNetID, netInc, decimalMask); cout << "Network ID: " << toString(netID) << " - "; cout << toString(netIDRange) << endl; cout << "Network Increment: " << getIncrement(decimalMask, decimalNetID) << endl; cout << "Number of Subnets: " << getSubnets(decimalMask, ipClass, subClassMask) << endl; cout << "Usable hosts per subnet: " << getHostsPerSubnet(decimalMask) << endl; cout << "-----------------------------------------" << endl << endl; cout << "Would you like to enter another IP Address to subnet? (y or n): "; cin >> resp; cout << endl << endl << endl << endl; } return 0; }``````

Thank you Chervil and everybody else that assisted me in learning process.
Last edited on
Topic archived. No new replies allowed.