diff options
Diffstat (limited to 'src/main/java/com/it_jaros')
| -rw-r--r-- | src/main/java/com/it_jaros/jns/scan/engine/ScanPortTask.java | 57 |
1 files changed, 31 insertions, 26 deletions
diff --git a/src/main/java/com/it_jaros/jns/scan/engine/ScanPortTask.java b/src/main/java/com/it_jaros/jns/scan/engine/ScanPortTask.java index 2203db6..1e709d5 100644 --- a/src/main/java/com/it_jaros/jns/scan/engine/ScanPortTask.java +++ b/src/main/java/com/it_jaros/jns/scan/engine/ScanPortTask.java @@ -61,31 +61,10 @@ public class ScanPortTask implements Callable<PortResult> { result.setPort(port); result.setState(PortState.UNKNOWN); try(SocketChannel socketChannel = SocketChannel.open()) { - socketChannel.configureBlocking(false); - socketChannel.connect(new InetSocketAddress(inetAddress, port)); - final long deadlineNanos = System.nanoTime() + timeoutInNanos; - final long waitInNanos = TimeUnit.MILLISECONDS.toNanos(1000); - boolean isConnected = socketChannel.finishConnect(); - while (!cancelledToken.isCancelled() && !isConnected) { - long remainingNanos = deadlineNanos - System.nanoTime(); - if (remainingNanos <= 0) { - break; - } - LockSupport.parkNanos(Math.min(waitInNanos, remainingNanos)); - isConnected = socketChannel.finishConnect(); - } - - if (cancelledToken.isCancelled()) { - return result; - } - - if (isConnected) { - result.setState(PortState.OPEN); - if (bannerRecognition) { - result.setBanner(getBanner(socketChannel)); - } - } else { - result.setState(PortState.FILTERED); + PortState state = getPortState(socketChannel); + result.setState(state); + if (PortState.OPEN.equals(state) && bannerRecognition) { + result.setBanner(getBanner(socketChannel)); } } catch (ConnectException ignored) { result.setState(PortState.CLOSED); @@ -96,6 +75,32 @@ public class ScanPortTask implements Callable<PortResult> { return result; } + private PortState getPortState(SocketChannel socketChannel) throws IOException { + socketChannel.configureBlocking(false); + socketChannel.connect(new InetSocketAddress(inetAddress, port)); + final long deadlineNanos = System.nanoTime() + timeoutInNanos; + final long waitInNanos = TimeUnit.MILLISECONDS.toNanos(1000); + boolean isConnected = socketChannel.finishConnect(); + while (!cancelledToken.isCancelled() && !isConnected) { + long remainingNanos = deadlineNanos - System.nanoTime(); + if (remainingNanos <= 0) { + break; + } + LockSupport.parkNanos(Math.min(waitInNanos, remainingNanos)); + isConnected = socketChannel.finishConnect(); + } + + if (isConnected) { + return PortState.OPEN; + } + + if (cancelledToken.isCancelled()) { + return PortState.UNKNOWN; + } + + return PortState.FILTERED; + } + private byte[] getBanner(SocketChannel socketChannel) { try { return tryReadFrom(socketChannel); @@ -103,7 +108,7 @@ public class ScanPortTask implements Callable<PortResult> { // ignore } - return null; + return new byte[0]; } private byte[] tryReadFrom(SocketChannel socketChannel) throws IOException { |
