forked from tylerdmace/subnetterjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
subnetter.js
150 lines (112 loc) · 4.04 KB
/
subnetter.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
138
139
140
141
142
143
144
145
146
147
148
149
150
'use strict';
var Subnetter = (function(obj) {
obj.addressToBinary = function(address) {
var decimalValues = [];
var convertedAddress = [];
var splitAddress = address.split('.');
for(var i = 0; i < splitAddress.length; i++) {
decimalValues.push(Number(splitAddress[i]));
}
for(i = 0; i < decimalValues.length; i++) {
convertedAddress.push(obj.octetToBinary(decimalValues[i]));
}
return convertedAddress;
};
obj.addressToDecimal = function(address) {
var joinedAddress = obj.addressToBinary(address).join('');
var bitPlaceValues = [2147483648, 1073741824, 536870912, 268435456, 134217728, 67108864, 33554432, 16777216, 8388608, 4194304, 2097152, 1048576, 524288, 262144, 131072, 65536, 32768, 16384, 8192, 4096, 2048, 1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1];
var bitValue = 0;
for(var bit = 0; bit < 32; bit++) {
if(joinedAddress.charAt(bit) === '1') {
bitValue = bitValue + bitPlaceValues[bit];
}
}
return bitValue;
};
obj.andOctets = function(octetOne, octetTwo){
var convertedOctet = [];
for(var bit = 0; bit < 8; bit++) {
if(octetOne.charAt(bit) === '1' && octetTwo.charAt(bit) === '1') {
convertedOctet.push(1);
} else { convertedOctet.push(0); }
}
return convertedOctet.join('');
};
obj.getBroadcast = function(address, netmask) {
var network = obj.addressToBinary(obj.getNetwork(address, netmask));
var binaryNetmask = obj.addressToBinary(netmask);
var broadcast = [];
for(var octet = 0; octet < 4; octet++) {
broadcast.push(obj.octetToDecimal(obj.xorOctets(network[octet], obj.invertOctet(binaryNetmask[octet]))));
}
return broadcast.join('.');
};
obj.getFirstUsable = function(address, netmask) {
var network = obj.getNetwork(address, netmask).split('.');
network[3] = Number(network[3] + 1);
return network.join('.');
};
obj.getHost = function(address, netmask) {
var binaryAddress = obj.addressToBinary(address);
var binaryNetmask = obj.addressToBinary(netmask);
var host = [];
for(var octet = 0; octet < 4; octet++) {
host.push(obj.octetToDecimal(obj.andOctets(binaryAddress[octet], obj.invertOctet(binaryNetmask[octet]))));
}
return host.join('.');
};
obj.getLastUsable = function(address, netmask) {
var broadcast = obj.getBroadcast(address, netmask).split('.');
broadcast[3] = Number(broadcast[3] - 1);
return broadcast.join('.');
};
obj.getNetwork = function(address, netmask) {
var binaryAddress = obj.addressToBinary(address);
var binaryNetmask = obj.addressToBinary(netmask);
var network = [];
for(var octet = 0; octet < 4; octet++) {
network.push(obj.octetToDecimal(obj.andOctets(binaryAddress[octet], binaryNetmask[octet])));
}
return network.join('.');
};
obj.invertOctet = function(octet) {
var invertedOctet = [];
for(var bit = 0; bit < 8; bit++) {
if(octet.charAt(bit) === '1') {
invertedOctet.push(0);
} else { invertedOctet.push(1); }
}
return invertedOctet.join('');
};
obj.octetToBinary = function(octet){
var bitPlaceValues = [128, 64, 32, 16, 8, 4, 2, 1];
var bitValues = [];
for(var i = 0; i < bitPlaceValues.length; i++) {
if(octet % bitPlaceValues[i] !== octet) {
octet = octet - bitPlaceValues[i];
bitValues.push(1);
} else { bitValues.push(0); }
}
return bitValues.join('');
};
obj.octetToDecimal = function(octet) {
var bitPlaceValues = [128, 64, 32, 16, 8, 4, 2, 1];
var bitValue = 0;
for(var bit = 0; bit < 8; bit++) {
if(octet.charAt(bit) === '1') {
bitValue = bitValue + bitPlaceValues[bit];
}
}
return bitValue;
};
obj.xorOctets = function(octetOne, octetTwo) {
var convertedOctet = [];
for(var bit = 0; bit < 8; bit++) {
if(octetOne.charAt(bit) ^ octetTwo.charAt(bit)) {
convertedOctet.push(1);
} else { convertedOctet.push(0); }
}
return convertedOctet.join('');
};
return obj;
})(Subnetter || {});