Skip to content

Commit

Permalink
avoid code duplication in InetAddressUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
angryziber committed Dec 18, 2022
1 parent 9294f9a commit f4b1113
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 27 deletions.
32 changes: 16 additions & 16 deletions src/net/azib/ipscan/feeders/FileFeeder.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public class FileFeeder extends AbstractFeeder {
/** Found IP address Strings are put here */
private Map<String, ScanningSubject> foundHosts;
private Iterator<ScanningSubject> foundIPAddressesIterator;
private Stream<NetworkInterface> networkInterfaces;

private int currentIndex;

Expand All @@ -49,23 +50,32 @@ public String getId() {
}

public FileFeeder() {
try {
networkInterfaces = NetworkInterface.networkInterfaces();
}
catch (SocketException e) {
LOG.log(WARNING, "", e);
}
}

public FileFeeder(String fileName) {
this();
try {
findHosts(new FileReader(fileName));
}
catch (FileNotFoundException e) {
throw new FeederException("file.notExists");
}
}
// for reading 10 lines, everytime

public FileFeeder(Reader reader) {
this();
findHosts(reader);
}

private String readMultiLine(BufferedReader fileReader) throws IOException {
int index = 1;
StringBuffer sb = new StringBuffer();
StringBuilder sb = new StringBuilder();
String fileLine;
while ((fileLine = fileReader.readLine()) != null) {
sb.append(fileLine);
Expand All @@ -82,31 +92,22 @@ private String readMultiLine(BufferedReader fileReader) throws IOException {
private void findHosts(Reader reader) {
currentIndex = 0;
foundHosts = new LinkedHashMap<>();
Long startTime = System.currentTimeMillis();
long startTime = System.currentTimeMillis();

// Import the collection flow of the local LAN port, first time , and just one time
Stream<NetworkInterface> interfaceStream = null;
try {
interfaceStream = NetworkInterface.networkInterfaces();
}
catch (SocketException e) {
e.printStackTrace();
}
System.out.println("Start to deal with the file, time used:" + startTime);
try (BufferedReader fileReader = new BufferedReader(reader)) {
String fileLine;
while (!(fileLine = readMultiLine(fileReader)).equals("")) {
Long lineTime = System.currentTimeMillis();
long lineTime = System.currentTimeMillis();
Matcher matcher = HOSTNAME_REGEX.matcher(fileLine);
while (matcher.find()) {
try {
String host = matcher.group();
if (host.equals(Version.OWN_HOST)) continue;
ScanningSubject subject = foundHosts.get(host);
if (subject == null){
if (subject == null) {
InetAddress address = InetAddress.getByName(host);
//Call the new constructor( ScanningSubject) and pass in the LAN port obtained in advance above
subject = new ScanningSubject(address, InetAddressUtils.getInterface(address, interfaceStream));
subject = new ScanningSubject(address, InetAddressUtils.getInterface(address, networkInterfaces));
}

if (!matcher.hitEnd() && fileLine.charAt(matcher.end()) == ':') {
Expand Down Expand Up @@ -154,5 +155,4 @@ public String getInfo() {
// let's return the number of found addresses
return Integer.toString(foundHosts.size());
}

}
14 changes: 3 additions & 11 deletions src/net/azib/ipscan/util/InetAddressUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -210,11 +210,6 @@ public static NetworkInterface getInterface(InterfaceAddress address) {
}
}


//Change the UTIL method to pass in the LAN port for specified host filtering(Stream<NetworkInterface> interfaceStream),
// instead of re-obtaining the LAN port every time, I have just
//"Stream<NetworkInterface> interfaceStream" is for the collection Stream of the obtained local LAN port
//"InetAddress address" is the list waiting to be scanned
public static NetworkInterface getInterface(InetAddress address, Stream<NetworkInterface> interfaceStream) {
try {
if (address == null) return null;
Expand All @@ -227,15 +222,12 @@ public static NetworkInterface getInterface(InetAddress address, Stream<NetworkI
return null;
}
}

public static NetworkInterface getInterface(InetAddress address) {
try {
if (address == null) return null;
return NetworkInterface.networkInterfaces().filter(i -> i.getInterfaceAddresses().stream().anyMatch(ifAddr -> {
InetAddress netmask = parseNetmask(ifAddr.getNetworkPrefixLength());
return startRangeByNetmask(address, netmask).equals(startRangeByNetmask(ifAddr.getAddress(), netmask));
})).findFirst().orElse(null);
return getInterface(address, NetworkInterface.networkInterfaces());
}
catch (SocketException e) {
catch (Exception e) {
return null;
}
}
Expand Down

0 comments on commit f4b1113

Please sign in to comment.