-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
232 lines (178 loc) · 5.97 KB
/
Copy pathscript.js
File metadata and controls
232 lines (178 loc) · 5.97 KB
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
const subnetMasks = [
[128, 0, 0, 0], // /1
[192, 0, 0, 0], // /2
[224, 0, 0, 0], // /3
[240, 0, 0, 0], // /4
[248, 0, 0, 0], // /5
[252, 0, 0, 0], // /6
[254, 0, 0, 0], // /7
[255, 0, 0, 0], // /8
[255, 128, 0, 0], // /9
[255, 192, 0, 0], // /10
[255, 224, 0, 0], // /11
[255, 240, 0, 0], // /12
[255, 248, 0, 0], // /13
[255, 252, 0, 0], // /14
[255, 254, 0, 0], // /15
[255, 255, 0, 0], // /16
[255, 255, 128, 0], // /17
[255, 255, 192, 0], // /18
[255, 255, 224, 0], // /19
[255, 255, 240, 0], // /20
[255, 255, 248, 0], // /21
[255, 255, 252, 0], // /22
[255, 255, 254, 0], // /23
[255, 255, 255, 0], // /24
[255, 255, 255, 128], // /25
[255, 255, 255, 192], // /26
[255, 255, 255, 224], // /27
[255, 255, 255, 240], // /28
[255, 255, 255, 248], // /29
[255, 255, 255, 252], // /30
[255, 255, 255, 254], // /31
[255, 255, 255, 255] // /32
];
function createTr(){
return document.createElement('tr');
}
function createTd(){
return document.createElement('td');
}
function createTh() {
return document.createElement('th');
}
function createTableHeader() {
const tr = createTr();
const headers = [
'N° SUBNET',
'INDIRIZZO DI RETE',
'NETMASK',
'INDIRIZZO DI BROADCAST',
'Default Gateway',
'RANGE INDIRIZZI'
];
headers.forEach(headerText => {
const th = createTh();
th.textContent = headerText;
tr.appendChild(th);
});
return tr;
}
function getIp(){
return document.getElementById('ip').value;
}
function createTable(){
return document.createElement('table');
}
function getContainer(){
return document.getElementById('container');
}
function dividedOctets(value){
return value.split('.');
}
function necessaryNetmask(){
let defaultNetmask = defNetmask();
return defaultNetmask + numOfSubnet();
}
function defNetmask(){
let firstOctect = parseInt(dividedOctets(getIp())[0]);
return firstOctect >= 0 && firstOctect <= 127 ? 8 : firstOctect >= 128 && firstOctect <= 191 ? 16 : firstOctect >= 192 && firstOctect <= 223 ? 24 : console.log("l'indirizzo non è valido");
}
function numOfSubnet(){
let requestedSubnet = Number(document.getElementById('subnets').value);
let bitForSub = Math.log2(requestedSubnet);
return Math.ceil(bitForSub); // Number.isInteger(bitForSub) ? bitForSub + 1 : Math.ceil(bitForSub);
}
function rangeIp(numOfBit){
return rebuildIp(calculateFirstAndUltimateHost(numOfBit, '1')) + " - " + rebuildIp(calculateFirstAndUltimateHost(numOfBit, '0'));
}
function calculateFirstAndUltimateHost(numOfBit, index){
let host = index === '0' ? calculateBroadIp(numOfBit).split('') : calculateNetIp(numOfBit).split('');
host[31] = index;
return host.join('');
}
function calculateBroadIp(numOfBit){
let netIpOfActualSubnet = calculateNetIp(numOfBit);
let broadIp = netIpOfActualSubnet.split('');
const startBit = necessaryNetmask();
for(let i = startBit; i < 32; i++){
broadIp[i] = '1';
}
return broadIp.join('');
}
//funzione che resitituisce l'ip di rete
function calculateNetIp(numOfBit) {
const Ip = getIp();
const partsIp = dividedOctets(Ip);
const partsMask = subnetMasks[necessaryNetmask() - 1];
const numSubnetBits = numOfSubnet();
const ipBin = partsIp.map(octet => parseInt(octet).toString(2).padStart(8, '0')).join('');
const maskBin = partsMask.map(octet => parseInt(octet).toString(2).padStart(8, '0')).join('');
// Calcola i bit della subnet
const subnetBits = numOfBit.toString(2).padStart(numSubnetBits, '0');
const networkBin = ipBin.substr(0, defNetmask()) + subnetBits + '0'.repeat(32 - defNetmask() - numSubnetBits);
return networkBin;
}
function rebuildIp(ip){
const networkParts = [];
for (let i = 0; i < 4; i++) {
networkParts.push(parseInt(ip.substr(i * 8, 8), 2));
}
return networkParts.join('.');
}
function createSubnetTable(){
const table = createTable();
const numSubnet = 2 ** numOfSubnet();
table.appendChild(createTableHeader());
for (let i = 0; i < numSubnet; i++) {
const tr = createTr();
for (let j = 0; j <= 5; j++) {
const td = createTd();
td.textContent = j == 0 ? `${i}` : j == 1 ? rebuildIp(calculateNetIp(i)) : j == 2 ? `${subnetMasks[necessaryNetmask() - 1].join('.')}`: j == 3 ? rebuildIp(calculateBroadIp(i)) : j == 4 ? rebuildIp(calculateFirstAndUltimateHost(i, '0')) : j == 5 ? rangeIp(i): console.log("niente oh!!!");
tr.appendChild(td);
}
table.appendChild(tr);
}
return table;
}
function checkIp(){
ip = getIp();
const formatIp = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/;
if (!formatIp.test(ip)) {
return false;
}
const octets = ip.split('.');
for(let i = 0; i < octets.length; i++){
if(octets[i] < 0 || octets[i] > 255){
return false;
}
}
return true;
}
function checkSubnet() {
let host = Number(document.getElementById('hosts').value);
let netmaskBits = necessaryNetmask();
let totalBits = 32;
let hostBits = totalBits - netmaskBits;
let bitHosts = Math.ceil(Math.log2(host + 2));
return bitHosts <= hostBits;
}
function createSubnetInfo(){
let ipCheck = checkIp()
let subnetCheck = checkSubnet()
const existingTable = document.querySelector('table');
if (existingTable){
existingTable.remove();
}
if(ipCheck){
if(subnetCheck){
const container = getContainer();
const table = createSubnetTable();
container.after(table);
}else{
alert('Configurazione subnet non valida')
}
}else{
alert('Indirizzo IP non valido');
}
}