#311 store MAC explicitly into ScanningResult, so that it is always available after the scan; and can be accessed to read/set comments

This commit is contained in:
Anton Keks 2022-01-21 20:02:23 +02:00
parent 78e18f9b74
commit f43a1eec3f
4 changed files with 22 additions and 29 deletions

View File

@ -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);

View File

@ -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());

View File

@ -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.

View File

@ -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);
}