IP's are scanned reversly when the end IP address is lower than the start IP address.

This commit is contained in:
horvist 2014-04-05 12:09:08 +02:00 committed by Anton Keks
parent f4102d3a79
commit 7f3c4d0472
2 changed files with 41 additions and 5 deletions

View File

@ -26,6 +26,7 @@ public class RangeFeeder extends AbstractFeeder {
private InetAddress endIP;
private InetAddress originalEndIP;
private InetAddress currentIP;
private boolean isReverse;
double percentageComplete;
double percentageIncrement;
@ -44,12 +45,15 @@ public class RangeFeeder extends AbstractFeeder {
try {
this.startIP = this.currentIP = InetAddress.getByName(startIP);
this.endIP = this.originalEndIP = InetAddress.getByName(endIP);
this.isReverse = false;
}
catch (UnknownHostException e) {
throw new FeederException("malformedIP");
}
if (InetAddressUtils.greaterThan(this.startIP, this.endIP)) {
throw new FeederException("range.greaterThan");
this.isReverse = true;
this.endIP = InetAddressUtils.decrement(InetAddressUtils
.decrement(this.endIP));
}
initPercentageIncrement();
this.endIP = InetAddressUtils.increment(this.endIP);
@ -66,7 +70,7 @@ public class RangeFeeder extends AbstractFeeder {
rawEndIP = rawEndIP >= 0 ? rawEndIP : rawEndIP + Integer.MAX_VALUE;
rawStartIP = rawStartIP >= 0 ? rawStartIP : rawStartIP + Integer.MAX_VALUE;
// compute 1% of the whole range
percentageIncrement = 100.0/(rawEndIP - rawStartIP + 1);
percentageIncrement = Math.abs(100.0 / (rawEndIP - rawStartIP + 1));
percentageComplete = 0;
}
@ -78,7 +82,11 @@ public class RangeFeeder extends AbstractFeeder {
public ScanningSubject next() {
percentageComplete += percentageIncrement;
InetAddress prevIP = this.currentIP;
this.currentIP = InetAddressUtils.increment(prevIP);
if (this.isReverse) {
this.currentIP = InetAddressUtils.decrement(prevIP);
} else {
this.currentIP = InetAddressUtils.increment(prevIP);
}
return new ScanningSubject(prevIP);
}

View File

@ -82,11 +82,39 @@ public class InetAddressUtils {
* Increments an IP address by 1.
*/
public static InetAddress increment(InetAddress address) {
return modifyInetAddress(address, true);
}
/**
* Decrements an IP address by 1.
*/
public static InetAddress decrement(InetAddress address) {
return modifyInetAddress(address, false);
}
/**
* Increments or decrements an IP address by 1.
*
* @param address
* the IP address
* @param isIncrement
* @return incremented/decremented IP address
*/
private static InetAddress modifyInetAddress(InetAddress address,
boolean isIncrement) {
try {
byte[] newAddress = address.getAddress();
for (int i = newAddress.length-1; i >= 0; i--) {
if (++newAddress[i] != 0x00)
break;
if (isIncrement) {
if (++newAddress[i] != 0x00) {
break;
}
} else {
if (--newAddress[i] != 0x00) {
break;
}
}
}
return InetAddress.getByAddress(newAddress);
}