commit 666991a4f3624f9caa0c9e1ada6dc3b191f914a6
parent 8a76155151d767740572403ab9a94f85376d3d00
Author: Matthias Jaros <jaros@mailbox.org>
Date: Wed, 1 Jul 2026 13:45:41 +0200
Fixed port parsing logic
Diffstat:
1 file changed, 39 insertions(+), 24 deletions(-)
diff --git a/src/main/java/com/it_jaros/jscanner/PortRange.java b/src/main/java/com/it_jaros/jscanner/PortRange.java
@@ -10,37 +10,51 @@ public class PortRange {
private final BitSet specifiedPorts = new BitSet(MAX_PORT);
private final BitSet availablePorts = new BitSet(MAX_PORT);
- private int cursor;
+ private int currentPortCursor;
private int done = 0;
public PortRange(String ports) {
if (ports == null || "".equals(ports)) {
- // keep defaults
- specifiedPorts.set(1, 1024 + 1);
- return;
+ specifiedPorts.set(MIN_PORT, 1024);
+ } else if ("all".equalsIgnoreCase(ports)) {
+ specifiedPorts.set(MIN_PORT, MAX_PORT);
+ } else {
+ parsePortRange(ports);
}
+ availablePorts.or(specifiedPorts);
+ currentPortCursor = availablePorts.nextSetBit(MIN_PORT);
+ }
+
+ /**
+ * Possible values are 1,10 or 1-10 or a mix of 1,5-10
+ * So we treat , stronger and handle them first and then check for -
+ *
+ * @param ports
+ */
+ private void parsePortRange(String ports) {
String[] splitComma = ports.split(",");
for (String commaValue : splitComma) {
- if (commaValue.contains("-")) {
- String[] rangeValue = commaValue.split("-");
- if (rangeValue.length > 2) {
- throw new IllegalArgumentException(String.format("Argument contains too many '-'' %s", commaValue));
- }
-
- int start = Integer.parseInt(rangeValue[0]);
- int end = Integer.parseInt(rangeValue[1]);
- checkValues(start, end);
- specifiedPorts.set(start, end + 1);
- } else {
- int port = Integer.parseInt(commaValue);
+ String[] rangeValue = commaValue.split("-");
+
+ // more then two values are not possible
+ if (rangeValue.length > 2) {
+ throw new IllegalArgumentException(String.format("Argument contains too many '-'' %s", commaValue));
+ }
+
+ // no range given, only single port
+ int port = Integer.parseInt(rangeValue[0]);
+ if (rangeValue.length == 1) {
checkValue(port);
- specifiedPorts.set(Integer.parseInt(commaValue));
+ specifiedPorts.set(port);
+ continue;
}
- }
- availablePorts.or(specifiedPorts);
- cursor = availablePorts.nextSetBit(MIN_PORT);
+ // range given
+ int end = Integer.parseInt(rangeValue[1]);
+ checkValues(port, end);
+ specifiedPorts.set(port, end + 1);
+ }
}
private void checkValues(int start, int end) {
@@ -66,10 +80,10 @@ public class PortRange {
}
public int next() {
- int p = availablePorts.nextSetBit(cursor);
+ int p = availablePorts.nextSetBit(currentPortCursor);
if (p > 0) {
availablePorts.clear(p);
- cursor = p + 1;
+ currentPortCursor = p + 1;
done++;
return p;
}
@@ -78,7 +92,7 @@ public class PortRange {
}
public boolean hasNext() {
- return availablePorts.nextSetBit(cursor) > 0;
+ return availablePorts.nextSetBit(currentPortCursor) > 0;
}
public int getTotal() {
@@ -88,4 +102,4 @@ public class PortRange {
public int getDone() {
return done;
}
-}
+}
+\ No newline at end of file