Monday, April 22, 2013

Subnet Calculator (C++)

Berikut ini saya bagi source code subnet calculator, mohon koreksi nya ya barang kali ada yang salah hehehehe.... ^_^



//////////////////////////////
//Program Hitung Subnet
//by : Ayrbyte
//blog : ayrbyte.blogspot.nl <<-- 
//kunjungi blog q ya... 
//and jangan lupa klik iklannya... hehehe... ^_^
//////////////////////////////
#include <iostream>
#include <string>
#include <stdio.h>
using namespace std;

int temp, cidr;
int hps, jnet;
string tempip, no, no1, no2, no3, no4, m2, m3, m4, kelas, kelasa, scidr;
//s 0, bc 1, fh 2, lh 3
short int alldata[4][4];

void baner(){
    cout << "\n-----------------------------------------\n"
            "| Program Hitung Subnet\n"
            "| By : Ayrbyte (orang ganteng sedunia)\n"
            "| blog : ayrbyte.blogspot.nl <<--\n"
            "| ---------------------------------------\n" 
            "| kunjungi blog q ya... \n"
            "|    and jangan lupa klik iklannya...\n" 
            "|          hehehe... ^_^\n"
            "-----------------------------------------\n\n";
}

void invalid(){
    void baner();
    cout << "Input tidak valid... atau program ini gak bisa nemuin jawabannya...\n" 
            "Hehehehe... maklum... yang buat masih NEWBIE pool... ^_^\n" << endl;  
}

short int cekip(string scip){
    short int count, cx;
    temp = 0;
    cx = 0;
    count = 0;
    tempip.clear();
    short int titik = -2;
    temp = scip[0] - 48;
    if ((scip.length() < 16)&&(temp != titik)){
        for(short int x=0; x < scip.length();x++)
        {
            temp = scip[x] - 48;
            if(temp == titik){
              count = count + 1;  
            }
        }
        if(count != 3){
            invalid();
            return 0;
        } 
        temp = 0;
        for(short int i=0; i < scip.length();i++)
        {
             temp = scip[i] - 48;
             if((temp >= 0)and(temp <= 9)or(temp == titik)){
                    if(temp != titik){
                    tempip = temp + 48;
                    no = no + tempip;
                    }else{
                        if(cx == 0){
                            no1 = no;
                        }if(cx == 1){
                            no2 = no;
                        }
                        if(cx == 2){
                            no3 = no;
                        }
                    cx = cx +1;
                    no.clear();
                    }
                    no4 = no;
                }else{ invalid(); return 0; }
              temp = 0;
        }
        temp = atoi(no1.c_str());
        if((temp > 223) or (temp < 1)){ invalid(); return 0; }
        temp = atoi(no2.c_str());
        if((temp > 255) or (temp < 0)){ invalid(); return 0; }
        temp = atoi(no3.c_str());
        if((temp > 255) or (temp < 0)){ invalid(); return 0; }
        temp = atoi(no4.c_str());
        if((temp > 255) or (temp < 0)){ invalid(); return 0; }
    }else{ invalid(); return 0;}
    return 0;
}

void getclass(){
        temp = atoi(no1.c_str());
        if((temp <= 126) and (temp >= 1)){ kelas = "Class A"; }
        temp = atoi(no1.c_str());
        if((temp <= 191) and (temp >= 128)){ kelas = "Class B"; }
        temp = atoi(no1.c_str());
        if((temp <= 223) and (temp >= 192)){ kelas = "Class C"; }
}

void privatorpublic(){
    short int temp1, temp2;
    if(kelas == "Class A"){
        temp1 = atoi(no1.c_str());
        if((temp1 == 10)){
            kelasa = "(Private Addressing)";
        }else{ kelasa = "(Public Addressing)"; }
    }
    if(kelas == "Class B"){
        temp1 = atoi(no1.c_str());
        temp2 = atoi(no2.c_str());
        if((temp1 == 172)&&(temp2 >= 16)&&(temp2 <= 31)){
            kelasa = "(Private Addressing)";
        }else{ kelasa = "(Public Addressing)"; }
    }
    if(kelas == "Class C"){
        temp1 = atoi(no1.c_str());
        temp2 = atoi(no2.c_str());
        if((temp1 == 192)&&(temp2 == 168)){
            kelasa = "(Private Addressing)";
        }else{ kelasa = "(Public Addressing)"; }
    }
}

void nemask(){
    if(kelas == "Class A"){
        switch(cidr){
            case 8: m2 = "0"; m3 = "0"; m4 = "0"; break;
            case 9: m2 = "128"; m3 = "0"; m4 = "0"; break;
            case 10: m2 = "192"; m3 = "0"; m4 = "0"; break;
            case 11: m2 = "224"; m3 = "0"; m4 = "0"; break;
            case 12: m2 = "240"; m3 = "0"; m4 = "0"; break;
            case 13: m2 = "248"; m3 = "0"; m4 = "0"; break;
            case 14: m2 = "252"; m3 = "0"; m4 = "0"; break;
            case 15: m2 = "254"; m3 = "0"; m4 = "0"; break;
            case 16: m2 = "255"; m3 = "0"; m4 = "0"; break;
            case 17: m2 = "255"; m3 = "128"; m4 = "0"; break;
            case 18: m2 = "255"; m3 = "192"; m4 = "0"; break;
            case 19: m2 = "255"; m3 = "224"; m4 = "0"; break;
            case 20: m2 = "255"; m3 = "240"; m4 = "0"; break;
            case 21: m2 = "255"; m3 = "248"; m4 = "0"; break;
            case 22: m2 = "255"; m3 = "252"; m4 = "0"; break;
            case 23: m2 = "255"; m3 = "254"; m4 = "0"; break;
            case 24: m2 = "255"; m3 = "255"; m4 = "0"; break;
            case 25: m2 = "255"; m3 = "255"; m4 = "128"; break;
            case 26: m2 = "255"; m3 = "255"; m4 = "192"; break;
            case 27: m2 = "255"; m3 = "255"; m4 = "224"; break;
            case 28: m2 = "255"; m3 = "255"; m4 = "240"; break;
            case 29: m2 = "255"; m3 = "255"; m4 = "248"; break;
            case 30: m2 = "255"; m3 = "255"; m4 = "252"; break;
        }
    }
    if(kelas == "Class B"){
        switch(cidr){
            case 16: m2 = "255"; m3 = "0"; m4 = "0"; break;
            case 17: m2 = "255"; m3 = "128"; m4 = "0"; break;
            case 18: m2 = "255"; m3 = "192"; m4 = "0"; break;
            case 19: m2 = "255"; m3 = "224"; m4 = "0"; break;
            case 20: m2 = "255"; m3 = "240"; m4 = "0"; break;
            case 21: m2 = "255"; m3 = "248"; m4 = "0"; break;
            case 22: m2 = "255"; m3 = "252"; m4 = "0"; break;
            case 23: m2 = "255"; m3 = "254"; m4 = "0"; break;
            case 24: m2 = "255"; m3 = "255"; m4 = "0"; break;
            case 25: m2 = "255"; m3 = "255"; m4 = "128"; break;
            case 26: m2 = "255"; m3 = "255"; m4 = "192"; break;
            case 27: m2 = "255"; m3 = "255"; m4 = "224"; break;
            case 28: m2 = "255"; m3 = "255"; m4 = "240"; break;
            case 29: m2 = "255"; m3 = "255"; m4 = "248"; break;
            case 30: m2 = "255"; m3 = "255"; m4 = "252"; break;
        }
    }
    if(kelas == "Class C"){
        switch(cidr){
            case 24: m2 = "255"; m3 = "255"; m4 = "0"; break;
            case 25: m2 = "255"; m3 = "255"; m4 = "128"; break;
            case 26: m2 = "255"; m3 = "255"; m4 = "192"; break;
            case 27: m2 = "255"; m3 = "255"; m4 = "224"; break;
            case 28: m2 = "255"; m3 = "255"; m4 = "240"; break;
            case 29: m2 = "255"; m3 = "255"; m4 = "248"; break;
            case 30: m2 = "255"; m3 = "255"; m4 = "252"; break;
        }
    }
}

void jumlahsubnet(){
    int temp1 = 1;
    if(kelas == "Class A"){
        temp = cidr - 8;
        for(int i = 1; i <= temp;i++){
            temp1 = temp1 * 2;
        }
    }
    if(kelas == "Class B"){
        temp = cidr - 16;
        for(int i = 1; i <= temp;i++){
            temp1 = temp1 * 2;
        }
    }
    if(kelas == "Class C"){
        temp = cidr - 24;
        for(int i = 1; i <= temp;i++){
            temp1 = temp1 * 2;
        }
    }
    jnet = temp1;
}

void hostpersubnet(){
    int temp1 = 1;
    if(kelas == "Class A"){
        temp = cidr - 8;
        temp = 24 - temp;
        for(int i = 1; i <= temp;i++){
            temp1 = temp1 * 2;
        }
    }
    if(kelas == "Class B"){
        temp = cidr - 16;
        temp = 16 - temp;
        for(int i = 1; i <= temp;i++){
            temp1 = temp1 * 2;
        }
    }
    if(kelas == "Class C"){
        temp = cidr - 24;
        temp = 8 - temp;
        for(int i = 1; i <= temp;i++){
            temp1 = temp1 * 2;
        }
    }
    temp1 = temp1-2;
    hps = temp1;
}

void network(){
    short int temp1, temp2;
    if(kelas == "Class A"){
        alldata[0][0] = atoi(no1.c_str());
        
        temp1 = atoi(no2.c_str());
        temp2 = atoi(m2.c_str());
        temp = temp1 & temp2;
        alldata[0][1] = temp;
        
        temp1 = atoi(no3.c_str());
        temp2 = atoi(m3.c_str());
        temp = temp1 & temp2;
        alldata[0][2] = temp;
        
        temp1 = atoi(no4.c_str());
        temp2 = atoi(m4.c_str());
        temp = temp1 & temp2;
        alldata[0][3] = temp;
    }
    if(kelas == "Class B"){
        alldata[0][0] = atoi(no1.c_str());
        
        temp1 = atoi(no2.c_str());
        temp2 = atoi(m2.c_str());
        temp = temp1 & temp2;
        alldata[0][1] = temp;
        
        temp1 = atoi(no3.c_str());
        temp2 = atoi(m3.c_str());
        temp = temp1 & temp2;
        alldata[0][2] = temp;
        
        temp1 = atoi(no4.c_str());
        temp2 = atoi(m4.c_str());
        temp = temp1 & temp2;
        alldata[0][3] = temp;
    }
    if(kelas == "Class C"){
        alldata[0][0] = atoi(no1.c_str());
        
        temp1 = atoi(no2.c_str());
        temp2 = atoi(m2.c_str());
        temp = temp1 & temp2;
        alldata[0][1] = temp;
        
        temp1 = atoi(no3.c_str());
        temp2 = atoi(m3.c_str());
        temp = temp1 & temp2;
        alldata[0][2] = temp;
        
        temp1 = atoi(no4.c_str());
        temp2 = atoi(m4.c_str());
        temp = temp1 & temp2;
        alldata[0][3] = temp;
    }
}

void conbc(){
   bool tempbool[8];
   tempbool[0] = 1;
   tempbool[1] = 1;
   cout<<tempbool; 
}

void broadcast(){
    short int temp1, temp2;
    if(kelas == "Class A"){
        alldata[1][0] = atoi(no1.c_str());
        
        temp1 = atoi(no2.c_str());
        temp2 = atoi(m2.c_str());
        temp2 = 256 + ~temp2;
        temp = temp1 | temp2;
        alldata[1][1] = temp;
        
        temp1 = atoi(no3.c_str());
        temp2 = atoi(m3.c_str());
        temp2 = 256 + ~temp2;
        temp = temp1 | temp2;
        alldata[1][2] = temp;
        
        temp1 = atoi(no4.c_str());
        temp2 = atoi(m4.c_str());
        temp2 = 256 + ~temp2;
        temp = temp1 | temp2;
        alldata[1][3] = temp;
    }
    if(kelas == "Class B"){
        alldata[1][0] = atoi(no1.c_str());
        
        temp1 = atoi(no2.c_str());
        temp2 = atoi(m2.c_str());
        temp2 = 256 + ~temp2;
        temp = temp1 | temp2;
        alldata[1][1] = temp;
        
        temp1 = atoi(no3.c_str());
        temp2 = atoi(m3.c_str());
        temp2 = 256 + ~temp2;
        temp = temp1 | temp2;
        alldata[1][2] = temp;
        
        temp1 = atoi(no4.c_str());
        temp2 = atoi(m4.c_str());
        temp2 = 256 + ~temp2;
        temp = temp1 | temp2;
        alldata[1][3] = temp;
    }
    if(kelas == "Class C"){
        alldata[1][0] = atoi(no1.c_str());
        
        temp1 = atoi(no2.c_str());
        temp2 = atoi(m2.c_str());
        temp2 = 256 + ~temp2;
        temp = temp1 | temp2;
        alldata[1][1] = temp;
        
        temp1 = atoi(no3.c_str());
        temp2 = atoi(m3.c_str());
        temp2 = 256 + ~temp2;
        temp = temp1 | temp2;
        alldata[1][2] = temp;
        
        temp1 = atoi(no4.c_str());
        temp2 = atoi(m4.c_str());
        temp2 = 256 + ~temp2;
        temp = temp1 | temp2;
        alldata[1][3] = temp;
    }
}

void fhlh(){
    alldata[2][0] = alldata[0][0];
    alldata[2][1] = alldata[0][1];
    alldata[2][2] = alldata[0][2];
    alldata[2][3] = alldata[0][3] + 1;
    
    alldata[3][0] = alldata[1][0];
    alldata[3][1] = alldata[1][1];
    alldata[3][2] = alldata[1][2];
    alldata[3][3] = alldata[1][3] - 1;
}

int main(int argc, char *argv[]){
    baner();
    string cip;
    cidr = 0;
    if(argc == 3){
        scidr = argv[2];
        temp = scidr[0];
        if((scidr.length() > 3)or(temp != 47)or(scidr.length() == 1)){
            invalid(); return 0;
        }
        for(short int i =1; i < scidr.length();i++)
        {
            temp = scidr[i];
            if((temp < 48)or(temp > 57)){
                invalid(); return 0;
            }
            if(scidr[1] == 0){
                invalid(); return 0;
            }
            tempip = temp;
            no = no + tempip;
            tempip.clear();
        }
        cidr = atoi(no.c_str());
        no.clear();
    }
    if(argc > 3){
        invalid(); return 0;
    }
    if(argc == 1){
        baner();
        cout<<"| berikut syntax untuk menggunakan program ini...\n"
              "| hitung.exe no_ip /cidr\n"
              "| contoh : hitung.exe 192.168.100.1 /26\n\n"
              "| catatan : apabila cidr tidak di isi berarti menggunakan default\n"
              "| cidrnya nya masing2... :*\n\n";
              return 0;
    }
    cip = argv[1];
    cekip(cip);
    getclass();
    if(kelas == "Class A"){
        if(cidr == 0){ cidr = 8; }
        if((8 > cidr)or(cidr > 30)){ invalid(); return 0; }
    }
    if(kelas == "Class B"){
        if(cidr == 0){ cidr = 16; }
        if((16 > cidr)or(cidr > 30)){ invalid(); return 0; }
    }
    if(kelas == "Class C"){
        if(cidr == 0){ cidr = 24; }
        if((24 > cidr)or(cidr > 30)){ invalid(); return 0; }
    }
    privatorpublic();
    nemask();
    jumlahsubnet();
    hostpersubnet();
    network();
    broadcast();
    fhlh();
    cout<<  "| ip : "<< no1 << "." << no2 << "." << no3 << "." << no4<<" \n" 
            "| Address Type : "<<kelas<<" "<<kelasa<<"\n"
            "| Sub Netmask : "<<"255"<<"."<<m2<<"."<<m3<<"."<<m4<<" \n"
            "| Jumlah Subnet : "<<jnet<<"\n"
            "| Host Per Subnet : "<<hps<<"\n"
            "| Subnet : "<<alldata[0][0]<<"."<<alldata[0][1]<<"."<<alldata[0][2]<<"."<<alldata[0][3]<<"\n"
            "| Range : "<<alldata[2][0]<<"."<<alldata[2][1]<<"."<<alldata[2][2]<<"."<<alldata[2][3]<<" - "
            <<alldata[3][0]<<"."<<alldata[3][1]<<"."<<alldata[3][2]<<"."<<alldata[3][3]<<"\n"
            "| Broadcast : "<<alldata[1][0]<<"."<<alldata[1][1]<<"."<<alldata[1][2]<<"."<<alldata[1][3]<<"\n";
    //short int x;
    //x = 13 & 24;
    //cout<<x;
    //string tet = "223";
    //short int test = atoi(tet.c_str());
    //cout << tet;
    return 0;
}
Baca Lebih Lanjut >>