-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
137 lines (108 loc) · 2.83 KB
/
index.js
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
const ayb = require('all-your-base');
const toBinary = require('to-binary');
class SubnetInfo {
constructor(cidr) {
this.ip = cidr.split('/')[0];
this.subnetBits = parseInt(cidr.split('/')[1], 10);
this.octets = this.ip.split('.');
};
_size() {
return Math.pow(2, 32 - this.subnetBits);
};
_netmask() {
var i = 0;
var octets = [];
var octet = [];
for (i = 0; i < 32; i++) {
octet.push(i < this.subnetBits ? 1 : 0);
if (octet.length === 8 ) {
octets.push(ayb.binToDec(octet.join('')));
octet = [];
}
}
return octets.join('.');
};
_startAddress() {
var netAddr = this._networkAddress().split('.');
var startAddr = [];
startAddr[0] = netAddr[0];
startAddr[1] = netAddr[1];
startAddr[2] = netAddr[2];
startAddr[3] = parseInt(netAddr[3], 10) + 1;
return startAddr.join('.');
};
_endAddress() {
var broadcastAddr = this._broadcastAddress().split('.');
var endAddr = [];
endAddr[0] = broadcastAddr[0];
endAddr[1] = broadcastAddr[1];
endAddr[2] = broadcastAddr[2];
endAddr[3] = parseInt(broadcastAddr[3], 10) - 1;
return endAddr.join('.');
};
_networkAddress() {
var i = 0;
var binaryNetMask = '';
var binaryIP = '';
var netAddr = [];
var octet = [];
for (i = 0; i < 32; i++) {
binaryNetMask += i < this.subnetBits ? '1' : '0';
}
var ip = this.ip.split('.');
for (i = 0; i < ip.length; i++) {
ip[i] = toBinary(ip[i]);
}
binaryIP = ip.join('');
for (i = 0; i < 32; i++) {
if (parseInt(binaryNetMask.charAt(i)) === 1) {
octet.push(binaryIP.charAt(i));
} else {
octet.push(0);
}
if (octet.length === 8) {
netAddr.push(ayb.binToDec(octet.join('')));
octet = [];
}
}
return netAddr.join('.');
};
_broadcastAddress() {
var i = 0;
var binaryNetMask = '';
var binaryIP = '';
var broadcastAddr = [];
var octet = [];
for (i = 0; i < 32; i++) {
binaryNetMask += i < this.subnetBits ? '1' : '0';
}
var ip = this.ip.split('.');
for (i = 0; i < ip.length; i++) {
ip[i] = toBinary(ip[i]);
}
binaryIP = ip.join('');
for (i = 0; i < 32; i++) {
if (parseInt(binaryNetMask.charAt(i)) === 1) {
octet.push(binaryIP.charAt(i));
} else {
octet.push(1);
}
if (octet.length === 8) {
broadcastAddr.push(ayb.binToDec(octet.join('')));
octet = [];
}
}
return broadcastAddr.join('.');
};
info() {
return {
size: this._size(),
netmask: this._netmask(),
startAddress: this._startAddress(),
endAddress: this._endAddress(),
netAddress: this._networkAddress(),
broadcastAddress: this._broadcastAddress()
};
};
};
module.exports = SubnetInfo;