File: ipadress.cpp

package info (click to toggle)
ipqalc 1.5.3%2Bgit20200816.523b207-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 324 kB
  • sloc: cpp: 348; xml: 20; makefile: 4
file content (202 lines) | stat: -rw-r--r-- 4,319 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include <math.h>
#include <QStringList>
#include <qregexp.h>

#include "ipadress.h"


IpAdress::IpAdress(QString ipAdress,QString NetMask){
    QString newmask;
    QString tempIP;
    bool ok;


    QStringList tempList = NetMask.split("");
    if (tempList[1] == "/" or NetMask.size()<=2){
        NetMask.replace("/","");
        int prefix = NetMask.toInt(&ok,10);
        for (int i=0;i<prefix;i++) tempIP.append("1");
        for (int i=0;i<(32-prefix);i++) tempIP.append("0");

        for (int x=0;x<4;x++){
            for (int i=(x*8);i<(x*8)+8;i++) newmask.append(tempIP[i]);
            newmask.append(".");
        }        
        this->NetMask = Conv(newmask,2,10);
    }else{
        this->NetMask = NetMask;
    }
    
    this->ipAdress = ipAdress;
    
}

IpAdress::~IpAdress(){

}


QString IpAdress::GetIpAdress(int Base){
    return Conv(ipAdress,10,Base);
}

QString IpAdress::GetNetMask(int Base){
    return Conv(NetMask,10,Base);
}

QString IpAdress::GetInversMask(int Base){
    QStringList mask = this->NetMask.split(".");
    QStringList invmask;
    QString str;

    for (int i=0; i<4; i++){
        invmask.append(str.setNum(((mask[i].toInt()) ^ 255), Base));
    }
    return Conv(invmask.join("."),Base,Base);
    
}

QString IpAdress::GetNetAdress(int Base){



    QStringList adr = this->ipAdress.split(".");
    QStringList mask = this->NetMask.split(".");
    QStringList net;
    QString str;

    for (int i=0; i<4; i++){
        net.append(str.setNum(((adr[i].toInt()) & (mask[i].toInt())), Base));
    }
    return Conv(net.join("."),Base,Base);
}


QString IpAdress::GetMaskPrefix(){
    QStringList binmask = GetNetMask(2).split("");
    int prefix=0;
    
    for (int i=0;i<binmask.count();i++){
	if (binmask[i] == "1") prefix++;
    }

    QString str;
    str.setNum(prefix, 10);
	
    return str;
}

QString IpAdress::GetBroadCast(int Base){
    int prefix = GetMaskPrefix().toInt();
    QString temp=GetIpAdress(2);
    QString broadcast;
    
    temp.replace(".","");
    temp.truncate(prefix);
    for (int i=0;i<(32-prefix);i++){
        temp.append("1");
    }
        
    QStringList tempList = temp.split("");
    for (int x=0;x<4;x++){
	for (int i=(x*8);i<(x*8)+8;i++) broadcast.append(tempList[1+i]);
	broadcast.append(".");
    }
            
    return Conv(broadcast,2,Base);
}

QString IpAdress::GetMinIP(int Base){
    int prefix = GetMaskPrefix().toInt();
    QString temp=GetIpAdress(2);
    QString minip;
    
    temp.replace(".","");
    temp.truncate(prefix);
    for (int i=0;i<(32-prefix-1);i++) temp.append("0");
    temp.append("1");
    
    QStringList tempList = temp.split("");
    for (int x=0;x<4;x++){
	for (int i=(x*8);i<(x*8)+8;i++) minip.append(tempList[1+i]);
	minip.append(".");
    }
            
    return Conv(minip,2,Base);
}

QString IpAdress::GetMaxIP(int Base){
    int prefix = GetMaskPrefix().toInt();
    QString temp=GetIpAdress(2);
    QString maxip;
    
    temp.replace(".","");
    temp.truncate(prefix);
    for (int i=0;i<(32-prefix-1);i++) temp.append("1");
    temp.append("0");
        
    QStringList tempList = temp.split("");
    for (int x=0;x<4;x++){
	for (int i=(x*8);i<(x*8)+8;i++) maxip.append(tempList[1+i]);
	maxip.append(".");
    }
            
    return Conv(maxip,2,Base);
}

QString IpAdress::GetHosts(){
    int hosts;
    QString temp;
    bool ok;
    int prefix = GetMaskPrefix().toInt();

    for (int i=0;i<32-(32-prefix);i++) temp.append("0");
    for (int i=0;i<(32-prefix);i++) temp.append("1");

    hosts=temp.toInt(&ok,2)+1;

    QString str;
    str.setNum(hosts, 10);

    return str;
    
}



QString IpAdress::Conv(QString IpAddr,int FromBase, int ToBase){
    QString str = "";
    QStringList strList;
    QByteArray text;
    int len;
    int MaxLen=0;
    bool ok;

    QStringList adr = IpAddr.split(".");

    for (int i=0;i<4;i++){
        text.setNum(adr[i].toInt(&ok,FromBase),ToBase);
        strList.append(text);
    }
    
    for (int i=0;i<4;i++){
	if (ToBase == 2) MaxLen=8;
	if (ToBase == 16) MaxLen=2;
	//if (ToBase == 10) MaxLen=3;
	
	len=strList[i].length();
	if (len < MaxLen){
	    for (int x=0;x<(MaxLen-len);x++){
	        strList[i].insert(0,"0");
	    }
	     
	}
    }

    str = strList.join(".");
    str = str.toUpper();

    return str;
}