@@ -27,7 +27,9 @@ public interface PortListener{
27
27
* Set the address to ping
28
28
* @param address - Address to be pinged
29
29
* @return this object to allow chaining
30
- * @throws UnknownHostException
30
+ * @throws UnknownHostException - if no IP address for the
31
+ * <code>host</code> could be found, or if a scope_id was specified
32
+ * for a global IPv6 address.
31
33
*/
32
34
public static PortScan onAddress (@ NonNull String address ) throws UnknownHostException {
33
35
PortScan portScan = new PortScan ();
@@ -65,8 +67,7 @@ public PortScan setTimeOutMillis(int timeOutMillis){
65
67
*/
66
68
public PortScan setPort (int port ){
67
69
ports .clear ();
68
- if (port <1 ) throw new IllegalArgumentException ("Port cannot be less than 1" );
69
- else if (port >65535 ) throw new IllegalArgumentException ("Port cannot be greater than 65535" );
70
+ validatePort (port );
70
71
ports .add (port );
71
72
return this ;
72
73
}
@@ -77,8 +78,12 @@ public PortScan setPort(int port){
77
78
* @return this object to allow chaining
78
79
*/
79
80
public PortScan setPorts (ArrayList <Integer > ports ){
80
- ports .clear ();
81
- // TODO: Validate / sanitize
81
+
82
+ // Check all ports are valid
83
+ for (Integer port : ports ){
84
+ validatePort (port );
85
+ }
86
+
82
87
this .ports = ports ;
83
88
84
89
return this ;
@@ -106,9 +111,8 @@ public PortScan setPorts(String portString){
106
111
if (x .contains ("-" )){
107
112
int start = Integer .parseInt (x .split ("-" )[0 ]);
108
113
int end = Integer .parseInt (x .split ("-" )[1 ]);
109
- if (start <1 ) throw new IllegalArgumentException ("Start port cannot be less than 1" );
110
- if (start >65535 ) throw new IllegalArgumentException ("Start cannot be greater than 65535" );
111
- if (end >65535 ) throw new IllegalArgumentException ("Start cannot be greater than 65535" );
114
+ validatePort (start );
115
+ validatePort (end );
112
116
if (end <=start ) throw new IllegalArgumentException ("Start port cannot be greater than or equal to the end port" );
113
117
114
118
for (int j =start ; j <=end ;j ++){
@@ -117,8 +121,7 @@ public PortScan setPorts(String portString){
117
121
}
118
122
else {
119
123
int start = Integer .parseInt (x );
120
- if (start <1 ) throw new IllegalArgumentException ("Start port cannot be less than 1" );
121
- if (start >65535 ) throw new IllegalArgumentException ("Start cannot be greater than 65535" );
124
+ validatePort (start );
122
125
ports .add (start );
123
126
}
124
127
}
@@ -128,6 +131,15 @@ public PortScan setPorts(String portString){
128
131
return this ;
129
132
}
130
133
134
+ /**
135
+ * Checks and throws exception if port is not valid
136
+ * @param port - the port to validate
137
+ */
138
+ private void validatePort (int port ){
139
+ if (port <1 ) throw new IllegalArgumentException ("Start port cannot be less than 1" );
140
+ if (port >65535 ) throw new IllegalArgumentException ("Start cannot be greater than 65535" );
141
+ }
142
+
131
143
/**
132
144
* Scan all privileged ports
133
145
* @return this object to allow chaining
@@ -146,7 +158,7 @@ public PortScan setPortsPrivileged(){
146
158
*/
147
159
public PortScan setPortsAll (){
148
160
ports .clear ();
149
- for (int i = 1 ; i < 65535 ; i ++) {
161
+ for (int i = 1 ; i < 65536 ; i ++) {
150
162
ports .add (i );
151
163
}
152
164
return this ;
0 commit comments