From f43a1eec3f2c3cb31c15ac1c1905dc04205ad0bf Mon Sep 17 00:00:00 2001 From: Anton Keks Date: Fri, 21 Jan 2022 20:02:23 +0200 Subject: [PATCH] #311 store MAC explicitly into ScanningResult, so that it is always available after the scan; and can be accessed to read/set comments --- .../azib/ipscan/config/CommentsConfig.java | 25 +++++-------------- src/net/azib/ipscan/core/Scanner.java | 3 ++- src/net/azib/ipscan/core/ScanningResult.java | 17 ++++++++----- src/net/azib/ipscan/gui/DetailsWindow.java | 6 ++--- 4 files changed, 22 insertions(+), 29 deletions(-) diff --git a/src/net/azib/ipscan/config/CommentsConfig.java b/src/net/azib/ipscan/config/CommentsConfig.java index 1db69f0b..d9e123b5 100644 --- a/src/net/azib/ipscan/config/CommentsConfig.java +++ b/src/net/azib/ipscan/config/CommentsConfig.java @@ -7,8 +7,6 @@ package net.azib.ipscan.config; import net.azib.ipscan.core.ScanningResult; -import net.azib.ipscan.core.ScanningResultList; -import net.azib.ipscan.fetchers.MACFetcher; import java.net.InetAddress; import java.util.prefs.Preferences; @@ -33,32 +31,21 @@ public class CommentsConfig { return comment; } - public String getComment(ScanningResultList results, int resultIndex) { - ScanningResult result = results.getResult(resultIndex); - int macIndex = results.getFetcherIndex(MACFetcher.ID); - return getComment(result.getAddress(), getMac(macIndex, result)); + public String getComment(ScanningResult result) { + return getComment(result.getAddress(), result.getMac()); } - private String getMac(int macIndex, ScanningResult result) { - if (macIndex < 0) return null; - Object macValue = result.getValues().get(macIndex); - return macValue instanceof String ? (String) macValue : null; - } - - public void setComment(ScanningResultList results, int resultIndex, String comment) { - ScanningResult result = results.getResult(resultIndex); - int macIndex = results.getFetcherIndex(MACFetcher.ID); - + public void setComment(ScanningResult result, String comment) { String key = result.getAddress().getHostAddress(); - if (macIndex >= 0) { + if (result.getMac() != null) { // remove ip-based comment if we set a mac-based one preferences.remove(key); - String mac = getMac(macIndex, result); + String mac = result.getMac(); if (mac != null) key = mac; } - if (comment == null || comment.length() == 0) + if (comment == null || comment.isEmpty()) preferences.remove(key); else preferences.put(key, comment); diff --git a/src/net/azib/ipscan/core/Scanner.java b/src/net/azib/ipscan/core/Scanner.java index 5e5b6688..7c9f31bd 100644 --- a/src/net/azib/ipscan/core/Scanner.java +++ b/src/net/azib/ipscan/core/Scanner.java @@ -10,6 +10,7 @@ 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; +import net.azib.ipscan.fetchers.MACFetcher; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -37,7 +38,6 @@ public class Scanner { * @param result where the results are injected */ public void scan(ScanningSubject subject, ScanningResult result) { - // populate results int fetcherIndex = 0; boolean isScanningInterrupted = false; for (Fetcher fetcher : fetcherRegistry.getSelectedFetchers()) { @@ -60,6 +60,7 @@ public class Scanner { result.setValue(fetcherIndex, value); fetcherIndex++; } + result.setMac((String) subject.getParameter(MACFetcher.ID)); activeFetchers.remove(Thread.currentThread().getId()); result.setType(subject.getResultType()); diff --git a/src/net/azib/ipscan/core/ScanningResult.java b/src/net/azib/ipscan/core/ScanningResult.java index 4449572e..e80a1e06 100644 --- a/src/net/azib/ipscan/core/ScanningResult.java +++ b/src/net/azib/ipscan/core/ScanningResult.java @@ -18,7 +18,6 @@ import java.util.List; * @author Anton Keks */ public class ScanningResult { - public enum ResultType { UNKNOWN, DEAD, ALIVE, WITH_PORTS; @@ -29,8 +28,9 @@ public class ScanningResult { } } - /** The scanned IP address */ private InetAddress address; + private String mac; + /** Scanning results, result of each Fetcher is an element */ private Object[] values; /** Scanning result type */ @@ -42,7 +42,6 @@ public class ScanningResult { /** * Creates a new instance, initializing the first value to the * provided address - * @param address * @param numberOfFetchers the number of currently available fetchers */ public ScanningResult(InetAddress address, int numberOfFetchers) { @@ -100,13 +99,19 @@ public class ScanningResult { /** * Sets the value returned by the specified fetcher - * @param fetcherIndex - * @param value */ public void setValue(int fetcherIndex, Object value) { values[fetcherIndex] = value; } - + + public void setMac(String mac) { + this.mac = mac; + } + + public String getMac() { + return mac; + } + /** * Returns all results for this IP address as a String. * This is used in showing the IP Details dialog box. diff --git a/src/net/azib/ipscan/gui/DetailsWindow.java b/src/net/azib/ipscan/gui/DetailsWindow.java index f16f756d..a2fb1a54 100644 --- a/src/net/azib/ipscan/gui/DetailsWindow.java +++ b/src/net/azib/ipscan/gui/DetailsWindow.java @@ -52,14 +52,14 @@ public class DetailsWindow extends AbstractModalDialog { resultIndex = resultTable.getSelectionIndex(); ScanningResult result = resultTable.getSelectedResult(); - commentsText = new Text(shell, SWT.BORDER); // TODO: change to SWT.SEARCH in SWT 3.5 + commentsText = new Text(shell, SWT.SEARCH); commentsText.pack(); commentsText.setLayoutData(LayoutHelper.formData(new FormAttachment(0), new FormAttachment(100), null, new FormAttachment(100))); CommentsTextListener commentsTextListener = new CommentsTextListener(); commentsText.addFocusListener(commentsTextListener); commentsText.addModifyListener(commentsTextListener); - String comment = commentsConfig.getComment(scanningResults, resultIndex); + String comment = commentsConfig.getComment(result); if (comment != null) commentsText.setText(comment); else commentsTextListener.focusLost(null); @@ -100,7 +100,7 @@ public class DetailsWindow extends AbstractModalDialog { String newComment = commentsText.getText(); if (!defaultText.equals(newComment)) { // store the new comment - commentsConfig.setComment(scanningResults, resultIndex, newComment); + commentsConfig.setComment(scanningResults.getResult(resultIndex), newComment); // now update the result table for user to immediately see the change resultTable.updateResult(resultIndex, CommentFetcher.ID, newComment); }