Skip to content

Commit 3154e97

Browse files
committed
added IP subnet calculator
1 parent 18f881e commit 3154e97

File tree

1 file changed

+43
-5
lines changed

1 file changed

+43
-5
lines changed

sonar.js

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,16 +111,54 @@ var sonar = {
111111
sonar.fingerprints = fingerprints;
112112
},
113113

114-
'ip_to_range': function( ip ) {
114+
'ip_to_range': function(ip, range) {
115115
var ip_parts = ip.split( '.' );
116116
if( ip_parts.length !== 4 ) {
117117
return false;
118118
}
119119

120-
for( var i = 1; i < 255; i++ ) {
121-
var tmp_ip = ip_parts[0] + '.' + ip_parts[1] + '.' + ip_parts[2] + '.' + i;
122-
sonar.ip_queue.push( tmp_ip );
120+
var ip_min = [0, 0, 0, 0];
121+
var ip_max = [0, 0, 0, 0];
122+
var r = 0;
123+
124+
for( var tmp = 0; tmp < 4; tmp++ ) {
125+
126+
// Calculate the number of bits of each part
127+
if ( range > 8 + 8 * tmp){
128+
r = 0;
129+
} else {
130+
r = 8 - (range - tmp * 8);
131+
if ( r < 0 ){
132+
r = 8;
133+
}
134+
}
135+
136+
// Calculate minimum and maximum of IP range for the current part
137+
ip_min[tmp] = ip_parts[tmp] & (255 << r);
138+
ip_max[tmp] = ip_parts[tmp] | (255 & ~(255 << r));
123139
}
140+
141+
if( sonar.debug ) {
142+
alert( '[DEBUG][The samallest IP adress to be scaned is:]' + ip_min[0] + '.' + ip_min[1] + '.' + ip_min[2] + '.' + ip_min[3]);
143+
alert( '[DEBUG][The largest IP adress to be scaned is:]' + ip_max[0] + '.' + ip_max[1] + '.' + ip_max[2] + '.' + ip_max[3]);
144+
}
145+
146+
// Queue IP address range
147+
var ip_parts = ip_min.slice();
148+
for( var a = ip_min[0]; a <= ip_max[0]; a++ ) {
149+
ip_parts[0] = a;
150+
for( var b = ip_min[1]; b <= ip_max[1]; b++ ) {
151+
ip_parts[1] = b;
152+
for( var c = ip_min[2]; c <= ip_max[2]; c++ ) {
153+
ip_parts[2] = c;
154+
for( var d = ip_min[3]; d <= ip_max[3]; d++ ) {
155+
ip_parts[3] = d;
156+
var tmp_ip = ip_parts[0] + '.' + ip_parts[1] + '.' + ip_parts[2] + '.' + ip_parts[3];
157+
sonar.ip_queue.push( tmp_ip );
158+
}
159+
}
160+
}
161+
}
124162
},
125163

126164
/*
@@ -183,7 +221,7 @@ var sonar = {
183221
function addAddress(newAddr) {
184222
if (newAddr in addrs) return;
185223
addrs[newAddr] = true;
186-
sonar.ip_to_range(newAddr);
224+
sonar.ip_to_range(newAddr, 24); // We assume that we are on a /24
187225
}
188226
function grepSDP(sdp) {
189227
var hosts = [];

0 commit comments

Comments
 (0)