11package com .stealthcopter .networktools ;
22
33import com .stealthcopter .networktools .portscanning .PortScanTCP ;
4+ import com .stealthcopter .networktools .portscanning .PortScanUDP ;
45
56import java .net .InetAddress ;
67import java .net .UnknownHostException ;
1516 */
1617public class PortScan {
1718
18- private int noThreads = 50 ;
19- private InetAddress address ;
20- private int timeOutMillis = 1000 ;
21- private boolean cancelled = false ;
22- private ArrayList <Integer > ports = new ArrayList <>();
23- private ArrayList <Integer > openPortsFound = new ArrayList <>();
24-
2519 private static final int TIMEOUT_LOCALHOST = 25 ;
2620 private static final int TIMEOUT_LOCALNETWORK = 1000 ;
2721 private static final int TIMEOUT_REMOTE = 2500 ;
@@ -30,6 +24,18 @@ public class PortScan {
3024 private static final int DEFAULT_THREADS_LOCALNETWORK = 50 ;
3125 private static final int DEFAULT_THREADS_REMOTE = 50 ;
3226
27+ private static final int METHOD_TCP = 0 ;
28+ private static final int METHOD_UDP = 1 ;
29+
30+ private int method = METHOD_TCP ;
31+ private int noThreads = 50 ;
32+ private InetAddress address ;
33+ private int timeOutMillis = 1000 ;
34+ private boolean cancelled = false ;
35+
36+ private ArrayList <Integer > ports = new ArrayList <>();
37+ private ArrayList <Integer > openPortsFound = new ArrayList <>();
38+
3339 private PortListener portListener ;
3440
3541 // This class is not to be instantiated
@@ -70,7 +76,7 @@ public static PortScan onAddress(InetAddress ia) {
7076
7177 /**
7278 * Sets the timeout for each port scanned
73- *
79+ * <p>
7480 * If you raise the timeout you may want to consider increasing the thread count {@link #setNoThreads(int)} to compensate.
7581 * We can afford to have quite a high thread count as most of the time the thread is just sitting
7682 * idle and waiting for the socket to timeout.
@@ -225,14 +231,55 @@ private void setDefaultThreadsAndTimeouts() {
225231 * @param noThreads set the number of threads to work with, note we default to a large number
226232 * as these requests are network heavy not cpu heavy.
227233 * @return self
228- * @throws IllegalAccessException - if no threads is less than 1
234+ * @throws IllegalArgumentException - if no threads is less than 1
229235 */
230236 public PortScan setNoThreads (int noThreads ) throws IllegalArgumentException {
231237 if (noThreads < 1 ) throw new IllegalArgumentException ("Cannot have less than 1 thread" );
232238 this .noThreads = noThreads ;
233239 return this ;
234240 }
235241
242+
243+ /**
244+ * Set scan method, either TCP or UDP
245+ *
246+ * @param method - the transport method to use to scan, either PortScan.METHOD_UDP or PortScan.METHOD_TCP
247+ * @return this object to allow chaining
248+ * @throws IllegalArgumentException - if invalid method
249+ */
250+ private PortScan setMethod (int method ) {
251+ switch (method ) {
252+ case METHOD_UDP :
253+ case METHOD_TCP :
254+ this .method = method ;
255+ break ;
256+ default :
257+ throw new IllegalArgumentException ("Invalid method type " + method );
258+ }
259+ return this ;
260+ }
261+
262+ /**
263+ * Set scan method to UDP
264+ *
265+ * @return this object to allow chaining
266+ */
267+ public PortScan setMethodUDP () {
268+ setMethod (METHOD_UDP );
269+ return this ;
270+ }
271+
272+ /**
273+ * Set scan method to TCP
274+ *
275+ * @return this object to allow chaining
276+ */
277+ public PortScan setMethodTCP () {
278+ setMethod (METHOD_TCP );
279+ return this ;
280+ }
281+
282+
236283 /**
237284 * Cancel a running ping
238285 */
@@ -253,7 +300,7 @@ public ArrayList<Integer> doScan() {
253300 ExecutorService executor = Executors .newFixedThreadPool (noThreads );
254301
255302 for (int portNo : ports ) {
256- Runnable worker = new PortScanRunnable (address , portNo , timeOutMillis );
303+ Runnable worker = new PortScanRunnable (address , portNo , timeOutMillis , method );
257304 executor .execute (worker );
258305 }
259306
@@ -291,7 +338,7 @@ public void run() {
291338 ExecutorService executor = Executors .newFixedThreadPool (noThreads );
292339
293340 for (int portNo : ports ) {
294- Runnable worker = new PortScanRunnable (address , portNo , timeOutMillis );
341+ Runnable worker = new PortScanRunnable (address , portNo , timeOutMillis , method );
295342 executor .execute (worker );
296343 }
297344
@@ -329,17 +376,29 @@ private class PortScanRunnable implements Runnable {
329376 private final InetAddress address ;
330377 private final int portNo ;
331378 private final int timeOutMillis ;
379+ private final int method ;
332380
333- PortScanRunnable (InetAddress address , int portNo , int timeOutMillis ) {
381+ PortScanRunnable (InetAddress address , int portNo , int timeOutMillis , int method ) {
334382 this .address = address ;
335383 this .portNo = portNo ;
336384 this .timeOutMillis = timeOutMillis ;
385+ this .method = method ;
337386 }
338387
339388 @ Override
340389 public void run () {
341390 if (cancelled ) return ;
342- portScanned (portNo , PortScanTCP .scanAddress (address , portNo , timeOutMillis ).open );
391+
392+ switch (method ) {
393+ case METHOD_UDP :
394+ portScanned (portNo , PortScanUDP .scanAddress (address , portNo , timeOutMillis ));
395+ break ;
396+ case METHOD_TCP :
397+ portScanned (portNo , PortScanTCP .scanAddress (address , portNo , timeOutMillis ));
398+ break ;
399+ default :
400+ throw new IllegalArgumentException ("Invalid method" );
401+ }
343402 }
344403 }
345404
0 commit comments