From eab7ce7e956785aca36076d0e5434a7aa244ade4 Mon Sep 17 00:00:00 2001 From: angryziber Date: Tue, 2 Jan 2007 00:11:25 +0000 Subject: [PATCH] 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 --- .../azib/ipscan/config/ComponentRegistry.java | 4 + ipscan/src/net/azib/ipscan/core/Scanner.java | 2 + .../net/azib/ipscan/core/ScannerThread.java | 16 ++-- .../ipscan/core/ScannerThreadFactory.java | 7 +- .../core/{ => values}/IntegerWithUnit.java | 5 +- .../core/{ => values}/NotAvailableValue.java | 2 +- .../core/{ => values}/NotScannedValue.java | 2 +- .../ipscan/core/values/NumericListValue.java | 76 ++++++++++++++++ .../src/net/azib/ipscan/fetchers/Fetcher.java | 4 +- .../ipscan/fetchers/FilteredPortsFetcher.java | 13 +-- .../ipscan/fetchers/PacketLossFetcher.java | 7 +- .../net/azib/ipscan/fetchers/PingFetcher.java | 15 ++-- .../azib/ipscan/fetchers/PingTTLFetcher.java | 5 +- .../azib/ipscan/fetchers/PortsFetcher.java | 86 +++++-------------- 14 files changed, 149 insertions(+), 95 deletions(-) rename ipscan/src/net/azib/ipscan/core/{ => values}/IntegerWithUnit.java (92%) rename ipscan/src/net/azib/ipscan/core/{ => values}/NotAvailableValue.java (90%) rename ipscan/src/net/azib/ipscan/core/{ => values}/NotScannedValue.java (90%) create mode 100755 ipscan/src/net/azib/ipscan/core/values/NumericListValue.java diff --git a/ipscan/src/net/azib/ipscan/config/ComponentRegistry.java b/ipscan/src/net/azib/ipscan/config/ComponentRegistry.java index 4d8f5ec7..ddfa30a3 100755 --- a/ipscan/src/net/azib/ipscan/config/ComponentRegistry.java +++ b/ipscan/src/net/azib/ipscan/config/ComponentRegistry.java @@ -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); diff --git a/ipscan/src/net/azib/ipscan/core/Scanner.java b/ipscan/src/net/azib/ipscan/core/Scanner.java index 808078c7..a56f1242 100755 --- a/ipscan/src/net/azib/ipscan/core/Scanner.java +++ b/ipscan/src/net/azib/ipscan/core/Scanner.java @@ -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; diff --git a/ipscan/src/net/azib/ipscan/core/ScannerThread.java b/ipscan/src/net/azib/ipscan/core/ScannerThread.java index ad46fae2..cf60b32f 100755 --- a/ipscan/src/net/azib/ipscan/core/ScannerThread.java +++ b/ipscan/src/net/azib/ipscan/core/ScannerThread.java @@ -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; } diff --git a/ipscan/src/net/azib/ipscan/core/ScannerThreadFactory.java b/ipscan/src/net/azib/ipscan/core/ScannerThreadFactory.java index f153fd4b..437784a8 100755 --- a/ipscan/src/net/azib/ipscan/core/ScannerThreadFactory.java +++ b/ipscan/src/net/azib/ipscan/core/ScannerThreadFactory.java @@ -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); } } diff --git a/ipscan/src/net/azib/ipscan/core/IntegerWithUnit.java b/ipscan/src/net/azib/ipscan/core/values/IntegerWithUnit.java similarity index 92% rename from ipscan/src/net/azib/ipscan/core/IntegerWithUnit.java rename to ipscan/src/net/azib/ipscan/core/values/IntegerWithUnit.java index 444048f1..8f213c7b 100755 --- a/ipscan/src/net/azib/ipscan/core/IntegerWithUnit.java +++ b/ipscan/src/net/azib/ipscan/core/values/IntegerWithUnit.java @@ -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 */ diff --git a/ipscan/src/net/azib/ipscan/core/NotAvailableValue.java b/ipscan/src/net/azib/ipscan/core/values/NotAvailableValue.java similarity index 90% rename from ipscan/src/net/azib/ipscan/core/NotAvailableValue.java rename to ipscan/src/net/azib/ipscan/core/values/NotAvailableValue.java index 5a733e8a..74c4ae8d 100755 --- a/ipscan/src/net/azib/ipscan/core/NotAvailableValue.java +++ b/ipscan/src/net/azib/ipscan/core/values/NotAvailableValue.java @@ -1,7 +1,7 @@ /** * */ -package net.azib.ipscan.core; +package net.azib.ipscan.core.values; import net.azib.ipscan.config.Labels; diff --git a/ipscan/src/net/azib/ipscan/core/NotScannedValue.java b/ipscan/src/net/azib/ipscan/core/values/NotScannedValue.java similarity index 90% rename from ipscan/src/net/azib/ipscan/core/NotScannedValue.java rename to ipscan/src/net/azib/ipscan/core/values/NotScannedValue.java index c9b14003..0bb732e3 100755 --- a/ipscan/src/net/azib/ipscan/core/NotScannedValue.java +++ b/ipscan/src/net/azib/ipscan/core/values/NotScannedValue.java @@ -1,7 +1,7 @@ /** * */ -package net.azib.ipscan.core; +package net.azib.ipscan.core.values; import net.azib.ipscan.config.Labels; diff --git a/ipscan/src/net/azib/ipscan/core/values/NumericListValue.java b/ipscan/src/net/azib/ipscan/core/values/NumericListValue.java new file mode 100755 index 00000000..ca31d9c8 --- /dev/null +++ b/ipscan/src/net/azib/ipscan/core/values/NumericListValue.java @@ -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(); + } +} diff --git a/ipscan/src/net/azib/ipscan/fetchers/Fetcher.java b/ipscan/src/net/azib/ipscan/fetchers/Fetcher.java index 919e906e..5d338ef2 100755 --- a/ipscan/src/net/azib/ipscan/fetchers/Fetcher.java +++ b/ipscan/src/net/azib/ipscan/fetchers/Fetcher.java @@ -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. diff --git a/ipscan/src/net/azib/ipscan/fetchers/FilteredPortsFetcher.java b/ipscan/src/net/azib/ipscan/fetchers/FilteredPortsFetcher.java index 24ec3fdf..e5cfdda1 100755 --- a/ipscan/src/net/azib/ipscan/fetchers/FilteredPortsFetcher.java +++ b/ipscan/src/net/azib/ipscan/fetchers/FilteredPortsFetcher.java @@ -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; } - } diff --git a/ipscan/src/net/azib/ipscan/fetchers/PacketLossFetcher.java b/ipscan/src/net/azib/ipscan/fetchers/PacketLossFetcher.java index e17d53fc..2183b310 100755 --- a/ipscan/src/net/azib/ipscan/fetchers/PacketLossFetcher.java +++ b/ipscan/src/net/azib/ipscan/fetchers/PacketLossFetcher.java @@ -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); } } diff --git a/ipscan/src/net/azib/ipscan/fetchers/PingFetcher.java b/ipscan/src/net/azib/ipscan/fetchers/PingFetcher.java index 083e24a1..90533e67 100755 --- a/ipscan/src/net/azib/ipscan/fetchers/PingFetcher.java +++ b/ipscan/src/net/azib/ipscan/fetchers/PingFetcher.java @@ -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) { diff --git a/ipscan/src/net/azib/ipscan/fetchers/PingTTLFetcher.java b/ipscan/src/net/azib/ipscan/fetchers/PingTTLFetcher.java index 41ac5307..6bd7dec7 100755 --- a/ipscan/src/net/azib/ipscan/fetchers/PingTTLFetcher.java +++ b/ipscan/src/net/azib/ipscan/fetchers/PingTTLFetcher.java @@ -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() { diff --git a/ipscan/src/net/azib/ipscan/fetchers/PortsFetcher.java b/ipscan/src/net/azib/ipscan/fetchers/PortsFetcher.java index 1e6bbc9b..18f1a59f 100755 --- a/ipscan/src/net/azib/ipscan/fetchers/PortsFetcher.java +++ b/ipscan/src/net/azib/ipscan/fetchers/PortsFetcher.java @@ -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() {