Skip to content

Commit

Permalink
#280 if scanning a local network, then ARP will be used in addition t…
Browse files Browse the repository at this point in the history
…o chosen Pinger to detect more hosts
  • Loading branch information
angryziber committed Jan 21, 2022
1 parent 9bcc4ea commit ace2f3d
Show file tree
Hide file tree
Showing 16 changed files with 108 additions and 66 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Unreleased:
- Windows 32-bit build has been removed
- Linux deb & rpm packages now correctly specify Java 11 dependency, not 8
- Mac: display a notification if java not in PATH #279
- If scanning a local network, then ARP will be used in addition to chosen Pinger to detect more hosts #280

Changes in 3.8.1:
- Make 32-bit Windows build still run under Oracle Java 8 - it seems to still be popular #324
Expand Down
5 changes: 3 additions & 2 deletions src/net/azib/ipscan/core/Scanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import net.azib.ipscan.core.values.NotAvailable;
import net.azib.ipscan.core.values.NotScanned;
import net.azib.ipscan.feeders.Feeder;
import net.azib.ipscan.fetchers.Fetcher;
import net.azib.ipscan.fetchers.FetcherRegistry;

Expand Down Expand Up @@ -72,9 +73,9 @@ public void interrupt(Thread thread) {
/**
* Init everything needed for scanning, including Fetchers
*/
public void init() {
public void init(Feeder feeder) {
for (Fetcher fetcher : fetcherRegistry.getSelectedFetchers()) {
fetcher.init();
fetcher.init(feeder);
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/net/azib/ipscan/core/ScannerDispatcherThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,8 @@ public ScannerDispatcherThread(Feeder feeder, Scanner scanner, StateMachine stat
this.scanningResultList = scanningResults;
try {
this.scanningResultList.initNewScan(feeder);

// initialize in the main thread in order to catch exceptions gracefully
scanner.init();
scanner.init(feeder);
}
catch (RuntimeException e) {
stateMachine.reset();
Expand Down
18 changes: 13 additions & 5 deletions src/net/azib/ipscan/core/net/ARPPinger.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,27 @@

public class ARPPinger implements Pinger {
private MACFetcher macFetcher;
private JavaPinger trigger;
private Pinger trigger;

public ARPPinger(MACFetcher macFetcher, JavaPinger trigger) {
this.macFetcher = macFetcher;
// WinMACFetcher sends an actual ARP request, so no previous UDP request is needed
this.trigger = macFetcher.getClass().getSimpleName().startsWith("Win") ? null : trigger;
this(macFetcher, macFetcher.getClass().getSimpleName().startsWith("Win") ? null : (Pinger) trigger);
}

public ARPPinger(MACFetcher macFetcher, Pinger trigger) {
this.macFetcher = macFetcher;
this.trigger = trigger;
}

@Override public PingResult ping(ScanningSubject subject, int count) throws IOException {
PingResult result = new PingResult(subject.getAddress(), 1);
PingResult result = new PingResult(subject.getAddress(), count);
if (trigger != null) count /= 2;
for (int i = 0; i < count; i++) {
long start = currentTimeMillis();
if (trigger != null) trigger.ping(subject, 1); // this should issue an ARP request for the IP
if (trigger != null) {
// this should issue an ARP request for the IP
result.merge(trigger.ping(subject, count / 2));
}
String mac = macFetcher.scan(subject);
if (mac != null) result.addReply(currentTimeMillis() - start);
}
Expand Down
7 changes: 5 additions & 2 deletions src/net/azib/ipscan/core/net/PingerRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import net.azib.ipscan.di.InjectException;
import net.azib.ipscan.di.Injector;
import net.azib.ipscan.fetchers.FetcherException;
import net.azib.ipscan.fetchers.MACFetcher;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
Expand Down Expand Up @@ -60,8 +61,10 @@ public String[] getRegisteredNames() {
/**
* Creates the configured pinger with configured timeout
*/
public Pinger createPinger() throws FetcherException {
return createPinger(scannerConfig.selectedPinger, scannerConfig.pingTimeout);
public Pinger createPinger(boolean isLAN) throws FetcherException {
Pinger mainPinger = createPinger(scannerConfig.selectedPinger, scannerConfig.pingTimeout);
if (isLAN) return new ARPPinger(injector.require(MACFetcher.class), mainPinger);
return mainPinger;
}

/**
Expand Down
30 changes: 26 additions & 4 deletions src/net/azib/ipscan/feeders/AbstractFeeder.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,42 @@
package net.azib.ipscan.feeders;

import net.azib.ipscan.config.Labels;
import net.azib.ipscan.core.ScanningSubject;

import java.net.InetAddress;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;

import static net.azib.ipscan.util.InetAddressUtils.getInterface;
import static net.azib.ipscan.util.InetAddressUtils.matchingAddress;

/**
* Helper base class for built-in Feeders
*
* @author Anton Keks
*/
public abstract class AbstractFeeder implements Feeder {

public String getName() {
private NetworkInterface netIf;
private InterfaceAddress ifAddr;

protected void initInterfaces(InetAddress ip) {
this.netIf = getInterface(ip);
this.ifAddr = matchingAddress(netIf, ip.getClass());
}

@Override public ScanningSubject subject(InetAddress ip) {
return new ScanningSubject(ip, netIf, ifAddr);
}

@Override public String getName() {
return Labels.getLabel(getId());
}

@Override
public String toString() {
@Override public boolean isLocalNetwork() {
return ifAddr != null;
}

@Override public String toString() {
return getName() + ": " + getInfo();
}
}
13 changes: 13 additions & 0 deletions src/net/azib/ipscan/feeders/Feeder.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import net.azib.ipscan.core.Plugin;
import net.azib.ipscan.core.ScanningSubject;

import java.net.InetAddress;

/**
* Interface of a Feeder, which is used to feed scanner with IP addresses.
* Basically, classes implementing Feeder must provide an algorithm of
Expand Down Expand Up @@ -43,4 +45,15 @@ public interface Feeder extends Plugin {
* Used for creation of Favorites, saving to file, etc.
*/
String getInfo();

/**
* @return true if scanning LAN addresses, so that ARP, etc can be used
*/
default boolean isLocalNetwork() {
return false;
}

default ScanningSubject subject(InetAddress ip) {
return new ScanningSubject(ip);
}
}
10 changes: 4 additions & 6 deletions src/net/azib/ipscan/feeders/RandomFeeder.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
* @author Anton Keks
*/
public class RandomFeeder extends AbstractFeeder {

SecureRandom random = new SecureRandom();
InetAddress currentAddress;

Expand All @@ -29,10 +28,7 @@ public class RandomFeeder extends AbstractFeeder {
int addressCount;
int currentNumber;

/**
* @see Feeder#getId()
*/
public String getId() {
@Override public String getId() {
return "feeder.random";
}

Expand All @@ -41,7 +37,9 @@ public RandomFeeder() {

public RandomFeeder(String prototypeIP, String mask, int count) {
try {
this.prototypeBytes = InetAddress.getByName(prototypeIP).getAddress();
InetAddress ip = InetAddress.getByName(prototypeIP);
initInterfaces(ip);
this.prototypeBytes = ip.getAddress();
}
catch (UnknownHostException e) {
throw new FeederException("malformedIP");
Expand Down
17 changes: 5 additions & 12 deletions src/net/azib/ipscan/feeders/RangeFeeder.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@
import org.savarese.vserv.tcpip.OctetConverter;

import java.net.InetAddress;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.net.UnknownHostException;

import static net.azib.ipscan.util.InetAddressUtils.*;
import static net.azib.ipscan.util.InetAddressUtils.decrement;
import static net.azib.ipscan.util.InetAddressUtils.increment;

/**
* IP Range Feeder.
Expand All @@ -24,8 +23,6 @@
* @author Anton Keks
*/
public class RangeFeeder extends AbstractFeeder {
private NetworkInterface netIf;
private InterfaceAddress ifAddr;
private InetAddress startIP;
private InetAddress endIP;
private InetAddress originalEndIP;
Expand All @@ -35,9 +32,6 @@ public class RangeFeeder extends AbstractFeeder {
double percentageComplete;
double percentageIncrement;

/**
* @see Feeder#getId()
*/
public String getId() {
return "feeder.range";
}
Expand All @@ -49,8 +43,7 @@ public RangeFeeder(String startIP, String endIP) {
try {
this.startIP = this.currentIP = InetAddress.getByName(startIP);
this.endIP = this.originalEndIP = InetAddress.getByName(endIP);
this.netIf = getInterface(this.startIP);
this.ifAddr = matchingAddress(netIf, this.startIP.getClass());
initInterfaces(this.startIP);
this.isReverse = false;
}
catch (UnknownHostException e) {
Expand All @@ -66,7 +59,7 @@ public RangeFeeder(String startIP, String endIP) {
initPercentageIncrement();
this.endIP = increment(this.endIP);
}

/**
* Initalizes fields, used for computation of percentage of completion.
*/
Expand Down Expand Up @@ -95,7 +88,7 @@ private void initPercentageIncrement() {
} else {
this.currentIP = increment(prevIP);
}
return new ScanningSubject(prevIP, netIf, ifAddr);
return subject(prevIP);
}

public int percentageComplete() {
Expand Down
28 changes: 13 additions & 15 deletions src/net/azib/ipscan/feeders/RescanFeeder.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,27 @@
* @author Anton Keks
*/
public class RescanFeeder extends AbstractFeeder {

private Feeder originalFeeder;
private List<InetAddress> addresses;

int current;

/**
* Initializes the RescanFeeder using the old feeder used for the real scan to delegate some calls to.
* @param oldFeeder
*/
public RescanFeeder(Feeder oldFeeder, String ... ips) {
this.originalFeeder = oldFeeder;
public RescanFeeder(Feeder originalFeeder, String ... ips) {
this.originalFeeder = originalFeeder;
initAddresses(ips);
}

/**
* @return the label of the "old" feeder
*/
public String getId() {
@Override public String getId() {
return originalFeeder.getId();
}

@Override
public String getName() {
@Override public String getName() {
return Labels.getLabel("feeder.rescan.of") + originalFeeder.getName();
}

Expand All @@ -66,22 +63,23 @@ private int initAddresses(String ... ips) {
return ips.length;
}

public boolean hasNext() {
@Override public boolean hasNext() {
return current < addresses.size();
}

public ScanningSubject next() {
return new ScanningSubject(addresses.get(current++));
@Override public ScanningSubject next() {
return originalFeeder.subject(addresses.get(current++));
}

public int percentageComplete() {
@Override public int percentageComplete() {
return current * 100 / addresses.size();
}

/**
* @return the info of the "old" feeder
*/
public String getInfo() {
@Override public String getInfo() {
return originalFeeder.getInfo();
}

@Override public boolean isLocalNetwork() {
return originalFeeder.isLocalNetwork();
}
}
13 changes: 9 additions & 4 deletions src/net/azib/ipscan/fetchers/Fetcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import net.azib.ipscan.core.ScanningSubject;
import net.azib.ipscan.core.values.NotAvailable;
import net.azib.ipscan.core.values.NotScanned;
import net.azib.ipscan.feeders.Feeder;

/**
* Interface of all IP Fetchers.
Expand Down Expand Up @@ -50,12 +51,16 @@ public interface Fetcher extends Cloneable, Plugin {
* Special values may also be returned, such as {@link NotAvailable} or {@link NotScanned}
*/
Object scan(ScanningSubject subject);

/**
* Called before scanning has started to do any intialization stuff
* Called before scanning has started to do any initialization stuff
*/
void init();

default void init(Feeder feeder) {
init();
}

default void init() {}

/**
* Called after the scanning has been completed to do any cleanup needed
*/
Expand Down
5 changes: 3 additions & 2 deletions src/net/azib/ipscan/fetchers/PingFetcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import net.azib.ipscan.core.net.Pinger;
import net.azib.ipscan.core.net.PingerRegistry;
import net.azib.ipscan.core.values.IntegerWithUnit;
import net.azib.ipscan.feeders.Feeder;
import net.azib.ipscan.gui.fetchers.PingFetcherPrefs;

import java.io.IOException;
Expand Down Expand Up @@ -86,9 +87,9 @@ public Object scan(ScanningSubject subject) {
return result.isAlive() ? new IntegerWithUnit(result.getAverageTime(), "ms") : null;
}

public void init() {
public void init(Feeder feeder) {
if (pinger == null) {
pinger = pingerRegistry.createPinger();
pinger = pingerRegistry.createPinger(feeder.isLocalNetwork());
pingerUsers.set(1);
}
else
Expand Down
4 changes: 1 addition & 3 deletions src/net/azib/ipscan/gui/actions/StartStopScanningAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,7 @@ private boolean preScanChecks() {
MessageBox box = new MessageBox(resultTable.getShell(), SWT.ICON_QUESTION | SWT.YES | SWT.NO | SWT.SHEET);
box.setText(Labels.getLabel("text.scan.new"));
box.setMessage(Labels.getLabel("text.scan.confirmation"));
if (box.open() != SWT.YES) {
return false;
}
return box.open() == SWT.YES;
}
return true;
}
Expand Down
Loading

0 comments on commit ace2f3d

Please sign in to comment.