@@ -27,7 +27,9 @@ public interface PortListener{
2727 * Set the address to ping
2828 * @param address - Address to be pinged
2929 * @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.
3133 */
3234 public static PortScan onAddress (@ NonNull String address ) throws UnknownHostException {
3335 PortScan portScan = new PortScan ();
@@ -65,8 +67,7 @@ public PortScan setTimeOutMillis(int timeOutMillis){
6567 */
6668 public PortScan setPort (int port ){
6769 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 );
7071 ports .add (port );
7172 return this ;
7273 }
@@ -77,8 +78,12 @@ public PortScan setPort(int port){
7778 * @return this object to allow chaining
7879 */
7980 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+
8287 this .ports = ports ;
8388
8489 return this ;
@@ -106,9 +111,8 @@ public PortScan setPorts(String portString){
106111 if (x .contains ("-" )){
107112 int start = Integer .parseInt (x .split ("-" )[0 ]);
108113 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 );
112116 if (end <=start ) throw new IllegalArgumentException ("Start port cannot be greater than or equal to the end port" );
113117
114118 for (int j =start ; j <=end ;j ++){
@@ -117,8 +121,7 @@ public PortScan setPorts(String portString){
117121 }
118122 else {
119123 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 );
122125 ports .add (start );
123126 }
124127 }
@@ -128,6 +131,15 @@ public PortScan setPorts(String portString){
128131 return this ;
129132 }
130133
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+
131143 /**
132144 * Scan all privileged ports
133145 * @return this object to allow chaining
@@ -146,7 +158,7 @@ public PortScan setPortsPrivileged(){
146158 */
147159 public PortScan setPortsAll (){
148160 ports .clear ();
149- for (int i = 1 ; i < 65535 ; i ++) {
161+ for (int i = 1 ; i < 65536 ; i ++) {
150162 ports .add (i );
151163 }
152164 return this ;
0 commit comments