Advertisement
Guest User

Sieciowe stuffy

a guest
Feb 24th, 2020
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.83 KB | None | 0 0
  1. #include <iostream>
  2. #include <math.h>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. class IP
  8. {
  9. public:
  10.     string FullIP;
  11.     int adress[4];
  12.     int IPadress[4];
  13.     int submask[4];
  14.     int netPrefix[4];
  15.     int broadcastAdr[4];
  16.  
  17.  
  18.     IP()
  19.     {
  20.         cout << "Podaj adres IP: ";
  21.         cin >> FullIP;
  22.         splitIP();
  23.         IPadress[0] = adress[0];
  24.         IPadress[1] = adress[1];
  25.         IPadress[2] = adress[2];
  26.         IPadress[3] = adress[3];
  27.         cout << "Podaj maske podsieci: ";
  28.         cin >> FullIP;
  29.         splitIP();
  30.         submask[0] = adress[0];
  31.         submask[1] = adress[1];
  32.         submask[2] = adress[2];
  33.         submask[3] = adress[3];
  34.         networkPrefix();
  35.         cout<<"Adres calej podsieci: "<< niceFormat(netPrefix)<<endl;
  36.         broadcastAdress();
  37.         cout<<"Adres rozgloszeniowy: "<< niceFormat(broadcastAdr)<<endl;
  38.         cout<<"Ilosc hostow w sieci: " << hostAmount(connect4(submask));
  39.     }
  40.  
  41.     void splitIP()
  42.     {
  43.  
  44.         char temp[3];
  45.         temp[0] = '0';
  46.         temp[1] = '0';
  47.         temp[2] = '0';
  48.         int counter=0;
  49.         int counter2=0;
  50.         for (unsigned int i = 0; i <= FullIP.size(); i++)
  51.         {
  52.             if(FullIP[i]!='.'&&i<FullIP.size())
  53.             {
  54.                 temp[counter] = FullIP[i];
  55.                 counter++;
  56.             }
  57.             else
  58.             {
  59.                 if(temp==0)
  60.                     adress[counter2] = 0;
  61.                 else if(counter==3)
  62.                     adress[counter2] = asInt(temp);
  63.                 else if(counter==2)
  64.                 {
  65.                     temp[2] = temp[1];
  66.                     temp[1] = temp[0];
  67.                     temp[0] = '0';
  68.                     adress[counter2] = asInt(temp);
  69.                 }
  70.                 else
  71.                 {
  72.                     temp[2] = temp[0];
  73.                     temp[1] = '0';
  74.                     temp[0] = '0';
  75.                     adress[counter2] = asInt(temp);
  76.                 }
  77.                 counter = 0;
  78.                 counter2++;
  79.                 temp[0] = '0';
  80.                 temp[1] = '0';
  81.                 temp[2] = '0';
  82.             }
  83.         }
  84.     }
  85.     int asInt(char n[3])  //konwersja string na int
  86.     {
  87.         int temp = 0;
  88.         temp += (n[0] - '0') * 100; //od kodu ascii znaku cyfry adresu ip odejmujemy kod znaku '0' otrzymując w ten sposób liczby.
  89.         temp += (n[1] - '0') * 10;
  90.         temp += (n[2] - '0');
  91.         return temp;
  92.     }
  93.     void networkPrefix()
  94.     {
  95.         string temp;
  96.  
  97.         netPrefix[0] = (IPadress[0]&submask[0]); //operacja logiczna AND | &
  98.         netPrefix[1] = (IPadress[1]&submask[1]);
  99.         netPrefix[2] = (IPadress[2]&submask[2]);
  100.         netPrefix[3] = (IPadress[3]&submask[3]);
  101.  
  102.     }
  103.     void broadcastAdress()
  104.     {
  105.         int temp[4];
  106.  
  107.         temp[0] = toDecimal(toBinaryWithInverse(submask[0]));
  108.         temp[1] = toDecimal(toBinaryWithInverse(submask[1]));
  109.         temp[2] = toDecimal(toBinaryWithInverse(submask[2]));
  110.         temp[3] = toDecimal(toBinaryWithInverse(submask[3]));
  111.  
  112.         broadcastAdr[0] = temp[0] + netPrefix[0];
  113.         broadcastAdr[1] = temp[1] + netPrefix[1];
  114.         broadcastAdr[2] = temp[2] + netPrefix[2];
  115.         broadcastAdr[3] = temp[3] + netPrefix[3];
  116.     }
  117.     string niceFormat(int *tab)
  118.     {
  119.         string temp;
  120.         temp+=to_string(tab[0]);
  121.         temp+='.';
  122.         temp+=to_string(tab[1]);
  123.         temp+='.';
  124.         temp+=to_string(tab[2]);
  125.         temp+='.';
  126.         temp+=to_string(tab[3]);
  127.         return temp;
  128.     }
  129.     string toBinaryWithInverse(int n)
  130.     {
  131.         string r;
  132.         while(n!=0)
  133.         {
  134.             r=(n%2==0 ?"1":"0")+r; //tutaj odbywa się negacja, zero i jeden są na odwrót co daje nam negacje podczas konwersji | sama konwersja -> r=(n%2==0 ?"0":"1")+r;
  135.             n/=2;
  136.         }
  137.         return r;
  138.     }
  139.     string toBinary(int n)
  140.     {
  141.         string r;
  142.         while(n!=0)
  143.         {
  144.             r=(n%2==0 ?"0":"1")+r; //tutaj odbywa się negacja, zero i jeden są na odwrót co daje nam negacje podczas konwersji | sama konwersja -> r=(n%2==0 ?"0":"1")+r;
  145.             n/=2;
  146.         }
  147.         return r;
  148.     }
  149.     int toDecimal(string a) {
  150.         int temp=0;
  151.         for (unsigned int i = 0; i<a.size(); i++) {
  152.             temp+=(a[i]-'0') * pow(2, a.size()-i-1);
  153.         }
  154.         return temp;
  155.     }
  156.  
  157.     string connect4(int *tab){
  158.         string temp;
  159.         temp+=toBinary(tab[0]);
  160.         temp+=toBinary(tab[1]);
  161.         temp+=toBinary(tab[2]);
  162.         temp+=toBinary(tab[3]);
  163.  
  164.         return temp;
  165.     }
  166.     int hostAmount(string a) {
  167.         int n=0;
  168.         for (unsigned int i = 0; i< a.size(); i++) {
  169.             if(a[i] == '0') n++;
  170.         }
  171.         return pow(2, n)-2;
  172.     }
  173.  
  174. };
  175.  
  176. int main()
  177. {
  178.     IP test;
  179.     return 0;
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement