Value classes moved to their own package: net.azib.ipscan.core.values.

NumericListValue introduced (for port ranges and stuff), PortFetcher now returns them instead of Strings.
Config classes are now registered with pico container, so can be injected.


git-svn-id: https://ipscan.svn.sourceforge.net/svnroot/ipscan/trunk@101 375186e5-ef17-0410-b0b6-91563547dcda
This commit is contained in:
angryziber 2007-01-02 00:11:25 +00:00
parent 80552fa97f
commit eab7ce7e95
14 changed files with 149 additions and 95 deletions

View File

@ -67,6 +67,10 @@ public class ComponentRegistry {
// non-GUI
container.registerComponentInstance(Config.getPreferences());
container.registerComponentInstance(Config.getGlobal());
container.registerComponentInstance(Config.getDimensionsConfig());
container.registerComponentInstance(Config.getOpenersConfig());
container.registerComponentInstance(Config.getFavoritesConfig());
container.registerComponentImplementation(ExporterRegistry.class);
container.registerComponentImplementation(TXTExporter.class);

View File

@ -6,6 +6,8 @@ package net.azib.ipscan.core;
import java.net.InetAddress;
import java.util.Iterator;
import net.azib.ipscan.core.values.NotAvailableValue;
import net.azib.ipscan.core.values.NotScannedValue;
import net.azib.ipscan.fetchers.Fetcher;
import net.azib.ipscan.fetchers.FetcherRegistry;

View File

@ -5,7 +5,7 @@ package net.azib.ipscan.core;
import java.net.InetAddress;
import net.azib.ipscan.config.Config;
import net.azib.ipscan.config.GlobalConfig;
import net.azib.ipscan.feeders.Feeder;
/**
@ -22,11 +22,13 @@ public class ScannerThread extends Thread {
private ScanningResultsCallback resultsCallback;
private int state;
private int runningThreads;
private int maxThreads = Config.getGlobal().maxThreads;
private int threadDelay = Config.getGlobal().threadDelay;
public ScannerThread(Feeder feeder, Scanner scanner, ScanningResultList scanningResults) {
private GlobalConfig config;
public ScannerThread(Feeder feeder, Scanner scanner, ScanningResultList scanningResults, GlobalConfig globalConfig) {
super("Scanner Thread");
this.config = globalConfig;
// this thread is daemon because we want JVM to terminate it
// automatically if user closes the program (Main thread, that is)
setDaemon(true);
@ -44,9 +46,9 @@ public class ScannerThread extends Thread {
try {
// make a small delay between thread creation
Thread.sleep(threadDelay);
Thread.sleep(config.threadDelay);
if (runningThreads >= maxThreads) {
if (runningThreads >= config.maxThreads) {
// skip this iteration until more threads can be created
continue;
}
@ -55,7 +57,7 @@ public class ScannerThread extends Thread {
final InetAddress address = feeder.next();
// check if this is a likely broadcast address and needs to be skipped
if (Config.getGlobal().skipBroadcastAddresses && InetAddressUtils.isLikelyBroadcast(address)) {
if (config.skipBroadcastAddresses && InetAddressUtils.isLikelyBroadcast(address)) {
continue;
}

View File

@ -3,6 +3,7 @@
*/
package net.azib.ipscan.core;
import net.azib.ipscan.config.GlobalConfig;
import net.azib.ipscan.feeders.Feeder;
/**
@ -14,14 +15,16 @@ public class ScannerThreadFactory {
private ScanningResultList scanningResults;
private Scanner scanner;
private GlobalConfig globalConfig;
public ScannerThreadFactory(ScanningResultList scanningResults, Scanner scanner) {
public ScannerThreadFactory(ScanningResultList scanningResults, Scanner scanner, GlobalConfig globalConfig) {
this.scanningResults = scanningResults;
this.scanner = scanner;
this.globalConfig = globalConfig;
}
public ScannerThread createScannerThread(Feeder feeder) {
return new ScannerThread(feeder, scanner, scanningResults);
return new ScannerThread(feeder, scanner, scanningResults, globalConfig);
}
}

View File

@ -1,12 +1,13 @@
/**
*
*/
package net.azib.ipscan.core;
package net.azib.ipscan.core.values;
import net.azib.ipscan.config.Labels;
/**
* IntegerWithUnit - an Integer value together with a unit, e.g. "10 ms"
* IntegerWithUnit - an Integer value together with a unit, e.g. "10 ms".
* TODO: IntegerWithUnitTest
*
* @author anton
*/

View File

@ -1,7 +1,7 @@
/**
*
*/
package net.azib.ipscan.core;
package net.azib.ipscan.core.values;
import net.azib.ipscan.config.Labels;

View File

@ -1,7 +1,7 @@
/**
*
*/
package net.azib.ipscan.core;
package net.azib.ipscan.core.values;
import net.azib.ipscan.config.Labels;

View File

@ -0,0 +1,76 @@
/**
*
*/
package net.azib.ipscan.core.values;
import java.util.Collection;
import java.util.Iterator;
import java.util.TreeSet;
/**
* NumericListValue - a value object containing a list of numbers.
*
* TODO: add parsing functionality here.
*
* @author Anton Keks
*/
public class NumericListValue extends TreeSet {
private static final long serialVersionUID = 1L;
private boolean displayAsRanges; // TODO: make configurable
/**
* Creates a new empty instance
* @param displayAsRanges whether toString() outputs all number or their ranges
*/
public NumericListValue(boolean displayAsRanges) {
super();
this.displayAsRanges = displayAsRanges;
}
/**
* Creates a new instance initialized with the following numbers.
* @param numbers Set of numbers
* @param displayAsRanges whether toString() outputs all number or their ranges
*/
public NumericListValue(Collection numbers, boolean displayAsRanges) {
super(numbers);
this.displayAsRanges = displayAsRanges;
}
public String toString() {
StringBuffer sb = new StringBuffer();
Iterator i = iterator();
Integer prevPort = new Integer(Integer.MAX_VALUE);
boolean isRange = false;
if (i.hasNext()) {
prevPort = (Integer) i.next();
sb.append(prevPort);
}
while (i.hasNext()) {
Integer port = (Integer) i.next();
if (displayAsRanges && prevPort.intValue() + 1 == port.intValue()) {
isRange = true;
}
else {
if (isRange) {
sb.append('-').append(prevPort);
isRange = false;
}
sb.append(',').append(port);
}
prevPort = port;
}
if (isRange) {
sb.append('-').append(prevPort);
}
return sb.toString();
}
}

View File

@ -4,9 +4,9 @@
*/
package net.azib.ipscan.fetchers;
import net.azib.ipscan.core.NotAvailableValue;
import net.azib.ipscan.core.NotScannedValue;
import net.azib.ipscan.core.ScanningSubject;
import net.azib.ipscan.core.values.NotAvailableValue;
import net.azib.ipscan.core.values.NotScannedValue;
/**
* Interface of all IP Fetchers.

View File

@ -3,9 +3,9 @@
*/
package net.azib.ipscan.fetchers;
import java.util.Set;
import net.azib.ipscan.config.GlobalConfig;
import net.azib.ipscan.core.ScanningSubject;
import net.azib.ipscan.core.values.NumericListValue;
/**
* FilteredPortsFetcher uses the scanning results of PortsFetcher to display filtered ports.
@ -14,14 +14,17 @@ import net.azib.ipscan.core.ScanningSubject;
*/
public class FilteredPortsFetcher extends PortsFetcher {
public FilteredPortsFetcher(GlobalConfig globalConfig) {
super(globalConfig);
}
public String getLabel() {
return "fetcher.ports.filtered";
}
public Object scan(ScanningSubject subject) {
scanPorts(subject);
Set filteredPorts = getFilteredPorts(subject);
return filteredPorts.size() > 0 ? portListToRange(filteredPorts, displayAsRanges) : null;
NumericListValue filteredPorts = getFilteredPorts(subject);
return filteredPorts.size() > 0 ? filteredPorts.toString() : null;
}
}

View File

@ -3,17 +3,18 @@
*/
package net.azib.ipscan.fetchers;
import net.azib.ipscan.config.GlobalConfig;
import net.azib.ipscan.core.net.PingerRegistry;
/**
* TODO PacketLossFetcher
* TODO: write PacketLossFetcher
*
* @author anton
*/
public class PacketLossFetcher extends PingFetcher {
public PacketLossFetcher(PingerRegistry pingerRegistry) {
super(pingerRegistry);
public PacketLossFetcher(PingerRegistry pingerRegistry, GlobalConfig globalConfig) {
super(pingerRegistry, globalConfig);
}
}

View File

@ -8,13 +8,13 @@ import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.azib.ipscan.config.Config;
import net.azib.ipscan.config.GlobalConfig;
import net.azib.ipscan.config.LoggerFactory;
import net.azib.ipscan.core.IntegerWithUnit;
import net.azib.ipscan.core.ScanningSubject;
import net.azib.ipscan.core.net.PingResult;
import net.azib.ipscan.core.net.Pinger;
import net.azib.ipscan.core.net.PingerRegistry;
import net.azib.ipscan.core.values.IntegerWithUnit;
/**
* PingFetcher is able to ping IP addresses.
@ -27,6 +27,8 @@ public class PingFetcher implements Fetcher {
private static final Logger LOG = LoggerFactory.getLogger();
public static final String PARAMETER_PINGER = "pinger";
private GlobalConfig config;
/** The shared pinger - this one must be static, because PingTTLFetcher will use it as well */
private static Pinger pinger;
@ -34,8 +36,9 @@ public class PingFetcher implements Fetcher {
/** The registry used for creation of Pinger instances */
private PingerRegistry pingerRegistry;
public PingFetcher(PingerRegistry pingerRegistry) {
public PingFetcher(PingerRegistry pingerRegistry, GlobalConfig globalConfig) {
this.pingerRegistry = pingerRegistry;
this.config = globalConfig;
}
public String getLabel() {
@ -51,7 +54,7 @@ public class PingFetcher implements Fetcher {
}
else {
try {
result = pinger.ping(subject.getIPAddress(), Config.getGlobal().pingCount);
result = pinger.ping(subject.getIPAddress(), config.pingCount);
}
catch (IOException e) {
// if this is not a timeout
@ -69,7 +72,7 @@ public class PingFetcher implements Fetcher {
PingResult result = executePing(subject);
subject.setResultType(result.isAlive() ? ScanningSubject.RESULT_TYPE_ALIVE : ScanningSubject.RESULT_TYPE_DEAD);
if (!result.isAlive() && !Config.getGlobal().scanDeadHosts) {
if (!result.isAlive() && !config.scanDeadHosts) {
// the host is dead, we are not going to continue...
subject.abortScanning();
}
@ -80,7 +83,7 @@ public class PingFetcher implements Fetcher {
public void init() {
try {
if (pinger == null) {
pinger = pingerRegistry.createPinger(Config.getGlobal().selectedPinger, Config.getGlobal().pingTimeout);
pinger = pingerRegistry.createPinger(config.selectedPinger, config.pingTimeout);
}
}
catch (Exception e) {

View File

@ -4,6 +4,7 @@
*/
package net.azib.ipscan.fetchers;
import net.azib.ipscan.config.GlobalConfig;
import net.azib.ipscan.core.ScanningSubject;
import net.azib.ipscan.core.net.PingResult;
import net.azib.ipscan.core.net.PingerRegistry;
@ -16,8 +17,8 @@ import net.azib.ipscan.core.net.PingerRegistry;
*/
public class PingTTLFetcher extends PingFetcher {
public PingTTLFetcher(PingerRegistry pingerRegistry) {
super(pingerRegistry);
public PingTTLFetcher(PingerRegistry pingerRegistry, GlobalConfig globalConfig) {
super(pingerRegistry, globalConfig);
}
public String getLabel() {

View File

@ -8,21 +8,17 @@ import java.net.ConnectException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.util.Collection;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
import net.azib.ipscan.config.Config;
import net.azib.ipscan.config.GlobalConfig;
import net.azib.ipscan.core.PortIterator;
import net.azib.ipscan.core.ScanningSubject;
import net.azib.ipscan.core.net.PingResult;
import net.azib.ipscan.core.values.NumericListValue;
/**
* PortsFetcher scans TCP ports.
* Port list is obtained using the {@link net.azib.ipscan.core.PortIterator}.
*
* TODO: return a specialized "list" object instead of a String
*
* @author anton
*/
@ -31,15 +27,17 @@ public class PortsFetcher implements Fetcher {
private static final String PARAMETER_OPEN_PORTS = "openPorts";
private static final String PARAMETER_FILTERED_PORTS = "filteredPorts";
private GlobalConfig config;
// initialize options for this scan
private int timeout = Config.getGlobal().portTimeout;
private boolean adaptTimeout = Config.getGlobal().adaptPortTimeout;
private PortIterator portIteratorPrototype = new PortIterator(Config.getGlobal().portString);
private PortIterator portIteratorPrototype;
protected boolean displayAsRanges = true; // TODO: make configurable
public PortsFetcher(GlobalConfig globalConfig) {
this.config = globalConfig;
this.portIteratorPrototype = new PortIterator(config.portString);
}
/**
* @see net.azib.ipscan.fetchers.Fetcher#getLabel()
*/
public String getLabel() {
return "fetcher.ports";
}
@ -50,22 +48,22 @@ public class PortsFetcher implements Fetcher {
* @param subject
*/
protected void scanPorts(ScanningSubject subject) {
Set openPorts = getOpenPorts(subject);
NumericListValue openPorts = getOpenPorts(subject);
if (openPorts == null) {
// no results are available yet, let's proceed with the scanning
openPorts = new TreeSet();
Set filteredPorts = new TreeSet();
openPorts = new NumericListValue(displayAsRanges);
Set filteredPorts = new NumericListValue(displayAsRanges);
subject.setParameter(PARAMETER_OPEN_PORTS, openPorts);
subject.setParameter(PARAMETER_FILTERED_PORTS, filteredPorts);
int adaptedTimeout = timeout;
int adaptedTimeout = config.portTimeout;
// now try to adapt timeout if it is enabled and pinging results are availbale
PingResult pingResult = (PingResult) subject.getParameter(PingFetcher.PARAMETER_PINGER);
if (adaptTimeout && pingResult.isAlive()) {
if (config.adaptPortTimeout && pingResult.isAlive()) {
// TODO: use longest time istead of the average one for adapting
adaptedTimeout = Math.min(Math.max(pingResult.getAverageTime() * 4, 30), timeout);
adaptedTimeout = Math.min(Math.max(pingResult.getAverageTime() * 4, 30), config.portTimeout);
}
Socket socket = null;
@ -104,69 +102,29 @@ public class PortsFetcher implements Fetcher {
* @param subject
* @return
*/
protected Set getFilteredPorts(ScanningSubject subject) {
return (Set) subject.getParameter(PARAMETER_FILTERED_PORTS);
protected NumericListValue getFilteredPorts(ScanningSubject subject) {
return (NumericListValue) subject.getParameter(PARAMETER_FILTERED_PORTS);
}
/**
* @param subject
* @return
*/
protected Set getOpenPorts(ScanningSubject subject) {
return (Set) subject.getParameter(PARAMETER_OPEN_PORTS);
protected NumericListValue getOpenPorts(ScanningSubject subject) {
return (NumericListValue) subject.getParameter(PARAMETER_OPEN_PORTS);
}
/**
* Utility method to convert the resulting port List to String.
* @param portList source List of Integers
* @return a String
*/
protected static String portListToRange(Collection portList, boolean asRanges) {
StringBuffer sb = new StringBuffer();
Iterator i = portList.iterator();
Integer prevPort = new Integer(Integer.MAX_VALUE);
boolean isRange = false;
if (i.hasNext()) {
prevPort = (Integer) i.next();
sb.append(prevPort);
}
while (i.hasNext()) {
Integer port = (Integer) i.next();
if (asRanges && prevPort.intValue() + 1 == port.intValue()) {
isRange = true;
}
else {
if (isRange) {
sb.append('-').append(prevPort);
isRange = false;
}
sb.append(',').append(port);
}
prevPort = port;
}
if (isRange) {
sb.append('-').append(prevPort);
}
return sb.toString();
}
/*
* @see net.azib.ipscan.fetchers.Fetcher#scan(net.azib.ipscan.core.ScanningSubject)
*/
public Object scan(ScanningSubject subject) {
scanPorts(subject);
Set openPorts = getOpenPorts(subject);
NumericListValue openPorts = getOpenPorts(subject);
boolean portsFound = openPorts.size() > 0;
if (portsFound) {
subject.setResultType(ScanningSubject.RESULT_TYPE_ADDITIONAL_INFO);
}
return portsFound ? portListToRange(openPorts, displayAsRanges) : null;
return portsFound ? openPorts : null;
}
public void init() {