-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiscoverWithinRange.java
More file actions
107 lines (81 loc) · 3.59 KB
/
DiscoverWithinRange.java
File metadata and controls
107 lines (81 loc) · 3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package ToolKit;
import icmp.IcmpBasicOperation;
import java.util.ArrayList;
/**
* Created by yuwang on 12/13/15.
*/
public class DiscoverWithinRange {
private String addressStart;
private String addressEnd;
private int retries;
private int timeout;
private ArrayList<String> allAddresses;
public DiscoverWithinRange(String addressStart, String addressEnd, int retries, int timeout) {
if (retries <= 0) {
throw new IllegalArgumentException("retries must be higher than zero");
}
if (timeout < 1000 || timeout > 10000) {
throw new IllegalArgumentException("timeout should be between 1000 to 10000 ms");
}
this.timeout = timeout;
this.addressStart = addressStart;
this.addressEnd = addressEnd;
this.retries = retries;
allAddresses = parseAddressList();
}
public ArrayList<String> obtainReachables() {
ArrayList<String> resultList = new ArrayList<>();
for (String entry : this.allAddresses) {
IcmpBasicOperation icmpBasicOperation = new IcmpBasicOperation(entry, this.timeout, this.retries);
if (icmpBasicOperation.ifReachable()) {
resultList.add(entry);
}
}
return resultList;
}
public ArrayList<String> parseAddressList() {
ArrayList<String> resultList = new ArrayList<>();
if (this.addressStart.equalsIgnoreCase(this.addressEnd)) {
resultList.add(this.addressEnd);
return resultList;
}
String[] start = this.addressStart.split("\\.");
String[] end = this.addressEnd.split("\\.");
if (start.length != 4 || end.length != 4) {
throw new IllegalArgumentException("IP address range is illegal");
}
for (int i = 0; i < 4; i++) {
try {
int temp = Integer.parseInt(start[i]);
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException("There should not be any letter in IP address");
}
try {
int temp = Integer.parseInt(end[i]);
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException("There should not be any letter in IP address");
}
}
if (Integer.parseInt(start[0]) <= 0 || Integer.parseInt(start[0]) > 255
|| Integer.parseInt(end[0]) <= 0 || Integer.parseInt(end[0]) > 255
|| Integer.parseInt(start[3]) < 0 || Integer.parseInt(start[3]) > 255
|| Integer.parseInt(end[3]) < 0 || Integer.parseInt(end[3]) > 255) {
throw new IllegalArgumentException("The IP address is illegal");
}
if (start[0] == end[0] && start[1] == end[1] && start[2] == end[2]
&& Integer.parseInt(start[3]) > Integer.parseInt(end[3])) {
throw new IllegalArgumentException("Starting IP cannot be after ending IP");
}
if (start[0].equals(end[0]) && start[1].equals(end[1]) && start[2].equals(end[2])
&& Integer.parseInt(start[3]) < Integer.parseInt(end[3])) {
int diff = Integer.parseInt(end[3]) - Integer.parseInt(start[3]);
int i = 0;
while (i <= diff) {
resultList.add(start[0] + "." + start[1] + "." + start[2] + "." + String.valueOf(Integer.parseInt(start[3]) + i));
//System.out.println(start[0] + "." + start[1] + "." + start[2] + "." + String.valueOf(Integer.parseInt(start[3]) + i));
i++;
}
}
return resultList;
}
}