| 1 | #include <math.h> |
| 2 | #include <QStringList> |
| 3 | #include <qregexp.h> |
| 4 | |
| 5 | #include "ipadress.h" |
| 6 | |
| 7 | |
| 8 | IpAdress::IpAdress(QString ipAdress,QString NetMask){ |
| 9 | QString newmask; |
| 10 | QString tempIP; |
| 11 | bool ok; |
| 12 | |
| 13 | |
| 14 | QStringList tempList = NetMask.split(""); |
| 15 | if (tempList[1] == "/" or NetMask.size()<=2){ |
| 16 | NetMask.replace("/",""); |
| 17 | int prefix = NetMask.toInt(&ok,10); |
| 18 | for (int i=0;i<prefix;i++) tempIP.append("1"); |
| 19 | for (int i=0;i<(32-prefix);i++) tempIP.append("0"); |
| 20 | |
| 21 | for (int x=0;x<4;x++){ |
| 22 | for (int i=(x*8);i<(x*8)+8;i++) newmask.append(tempIP[i]); |
| 23 | newmask.append("."); |
| 24 | } |
| 25 | this->NetMask = Conv(newmask,2,10); |
| 26 | }else{ |
| 27 | this->NetMask = NetMask; |
| 28 | } |
| 29 | |
| 30 | this->ipAdress = ipAdress; |
| 31 | |
| 32 | } |
| 33 | |
| 34 | IpAdress::~IpAdress(){ |
| 35 | |
| 36 | } |
| 37 | |
| 38 | |
| 39 | QString IpAdress::GetIpAdress(int Base){ |
| 40 | return Conv(ipAdress,10,Base); |
| 41 | } |
| 42 | |
| 43 | QString IpAdress::GetNetMask(int Base){ |
| 44 | return Conv(NetMask,10,Base); |
| 45 | } |
| 46 | |
| 47 | QString IpAdress::GetInversMask(int Base){ |
| 48 | QStringList mask = this->NetMask.split("."); |
| 49 | QStringList invmask; |
| 50 | QString str; |
| 51 | |
| 52 | for (int i=0; i<4; i++){ |
| 53 | invmask.append(str.setNum(((mask[i].toInt()) ^ 255), Base)); |
| 54 | } |
| 55 | return Conv(invmask.join("."),Base,Base); |
| 56 | |
| 57 | } |
| 58 | |
| 59 | QString IpAdress::GetNetAdress(int Base){ |
| 60 | |
| 61 | |
| 62 | |
| 63 | QStringList adr = this->ipAdress.split("."); |
| 64 | QStringList mask = this->NetMask.split("."); |
| 65 | QStringList net; |
| 66 | QString str; |
| 67 | |
| 68 | for (int i=0; i<4; i++){ |
| 69 | net.append(str.setNum(((adr[i].toInt()) & (mask[i].toInt())), Base)); |
| 70 | } |
| 71 | return Conv(net.join("."),Base,Base); |
| 72 | } |
| 73 | |
| 74 | |
| 75 | QString IpAdress::GetMaskPrefix(){ |
| 76 | QStringList binmask = GetNetMask(2).split(""); |
| 77 | int prefix=0; |
| 78 | |
| 79 | for (int i=0;i<binmask.count();i++){ |
| 80 | if (binmask[i] == "1") prefix++; |
| 81 | } |
| 82 | |
| 83 | QString str; |
| 84 | str.setNum(prefix, 10); |
| 85 | |
| 86 | return str; |
| 87 | } |
| 88 | |
| 89 | QString IpAdress::GetBroadCast(int Base){ |
| 90 | int prefix = GetMaskPrefix().toInt(); |
| 91 | QString temp=GetIpAdress(2); |
| 92 | QString broadcast; |
| 93 | |
| 94 | temp.replace(".",""); |
| 95 | temp.truncate(prefix); |
| 96 | for (int i=0;i<(32-prefix);i++){ |
| 97 | temp.append("1"); |
| 98 | } |
| 99 | |
| 100 | QStringList tempList = temp.split(""); |
| 101 | for (int x=0;x<4;x++){ |
| 102 | for (int i=(x*8);i<(x*8)+8;i++) broadcast.append(tempList[1+i]); |
| 103 | broadcast.append("."); |
| 104 | } |
| 105 | |
| 106 | return Conv(broadcast,2,Base); |
| 107 | } |
| 108 | |
| 109 | QString IpAdress::GetMinIP(int Base){ |
| 110 | int prefix = GetMaskPrefix().toInt(); |
| 111 | QString temp=GetIpAdress(2); |
| 112 | QString minip; |
| 113 | |
| 114 | temp.replace(".",""); |
| 115 | temp.truncate(prefix); |
| 116 | for (int i=0;i<(32-prefix-1);i++) temp.append("0"); |
| 117 | temp.append("1"); |
| 118 | |
| 119 | QStringList tempList = temp.split(""); |
| 120 | for (int x=0;x<4;x++){ |
| 121 | for (int i=(x*8);i<(x*8)+8;i++) minip.append(tempList[1+i]); |
| 122 | minip.append("."); |
| 123 | } |
| 124 | |
| 125 | return Conv(minip,2,Base); |
| 126 | } |
| 127 | |
| 128 | QString IpAdress::GetMaxIP(int Base){ |
| 129 | int prefix = GetMaskPrefix().toInt(); |
| 130 | QString temp=GetIpAdress(2); |
| 131 | QString maxip; |
| 132 | |
| 133 | temp.replace(".",""); |
| 134 | temp.truncate(prefix); |
| 135 | for (int i=0;i<(32-prefix-1);i++) temp.append("1"); |
| 136 | temp.append("0"); |
| 137 | |
| 138 | QStringList tempList = temp.split(""); |
| 139 | for (int x=0;x<4;x++){ |
| 140 | for (int i=(x*8);i<(x*8)+8;i++) maxip.append(tempList[1+i]); |
| 141 | maxip.append("."); |
| 142 | } |
| 143 | |
| 144 | return Conv(maxip,2,Base); |
| 145 | } |
| 146 | |
| 147 | QString IpAdress::GetHosts(){ |
| 148 | int hosts; |
| 149 | QString temp; |
| 150 | bool ok; |
| 151 | int prefix = GetMaskPrefix().toInt(); |
| 152 | |
| 153 | for (int i=0;i<32-(32-prefix);i++) temp.append("0"); |
| 154 | for (int i=0;i<(32-prefix);i++) temp.append("1"); |
| 155 | |
| 156 | hosts=temp.toInt(&ok,2)+1; |
| 157 | |
| 158 | QString str; |
| 159 | str.setNum(hosts, 10); |
| 160 | |
| 161 | return str; |
| 162 | |
| 163 | } |
| 164 | |
| 165 | |
| 166 | |
| 167 | QString IpAdress::Conv(QString IpAddr,int FromBase, int ToBase){ |
| 168 | QString str = ""; |
| 169 | QStringList strList; |
| 170 | QByteArray text; |
| 171 | int len; |
| 172 | int MaxLen=0; |
| 173 | bool ok; |
| 174 | |
| 175 | QStringList adr = IpAddr.split("."); |
| 176 | |
| 177 | for (int i=0;i<4;i++){ |
| 178 | text.setNum(adr[i].toInt(&ok,FromBase),ToBase); |
| 179 | strList.append(text); |
| 180 | } |
| 181 | |
| 182 | for (int i=0;i<4;i++){ |
| 183 | if (ToBase == 2) MaxLen=8; |
| 184 | if (ToBase == 16) MaxLen=2; |
| 185 | //if (ToBase == 10) MaxLen=3; |
| 186 | |
| 187 | len=strList[i].length(); |
| 188 | if (len < MaxLen){ |
| 189 | for (int x=0;x<(MaxLen-len);x++){ |
| 190 | strList[i].insert(0,"0"); |
| 191 | } |
| 192 | |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | str = strList.join("."); |
| 197 | str = str.toUpper(); |
| 198 | |
| 199 | return str; |
| 200 | } |
| 201 | |
| 202 |