1
0
mirror of https://github.com/nmap/nmap.git synced 2025-12-07 21:21:31 +00:00

Use switch instead of chained if/else for -P* options

This commit is contained in:
dmiller
2023-05-09 14:16:45 +00:00
parent 6fdccee5ba
commit 8cd2130cd4

61
nmap.cc
View File

@@ -1124,37 +1124,42 @@ void parse_options(int argc, char **argv) {
delayed_options.normalfilename = logfilename(optarg, &local_time);
break;
case 'P':
if (!optarg) {
if (!optarg || *optarg == '\0') {
delayed_options.warn_deprecated("P", "PE");
o.pingtype |= PINGTYPE_ICMP_PING;
}
else if (*optarg == '\0' || *optarg == 'I' || *optarg == 'E') {
if (*optarg != 'E') {
else {
char buf[4];
Snprintf(buf, 3, "P%c", *optarg);
delayed_options.warn_deprecated(buf, "PE");
}
switch (*optarg) {
case 'I':
delayed_options.warn_deprecated("PI", "PE");
case 'E':
o.pingtype |= PINGTYPE_ICMP_PING;
}
else if (*optarg == 'M')
break;
case 'M':
o.pingtype |= PINGTYPE_ICMP_MASK;
else if (*optarg == 'P')
break;
case 'P':
o.pingtype |= PINGTYPE_ICMP_TS;
else if (*optarg == 'n' || *optarg == '0' || *optarg == 'N' || *optarg == 'D') {
if (*optarg != 'n') {
char buf[4];
break;
case '0':
case 'N':
case 'D':
Snprintf(buf, 3, "P%c", *optarg);
delayed_options.warn_deprecated(buf, "Pn");
}
case 'n':
if (o.verbose > 0)
error("Host discovery disabled (-Pn). All addresses will be marked 'up' and scan times may be slower.");
o.pingtype |= PINGTYPE_NONE;
}
else if (*optarg == 'R') {
break;
case 'R':
if (o.verbose > 0)
error("The -PR option is deprecated. ARP scan is always done when possible.");
}
else if (*optarg == 'S') {
break;
case 'S':
if (ports.syn_ping_count > 0)
fatal("Only one -PS option is allowed. Combine port ranges with commas.");
o.pingtype |= (PINGTYPE_TCP | PINGTYPE_TCP_USE_SYN);
@@ -1166,7 +1171,10 @@ void parse_options(int argc, char **argv) {
getpts_simple(DEFAULT_TCP_PROBE_PORT_SPEC, SCAN_TCP_PORT, &ports.syn_ping_ports, &ports.syn_ping_count);
assert(ports.syn_ping_count > 0);
}
} else if (*optarg == 'T' || *optarg == 'A') {
break;
case 'T':
case 'A':
if (ports.ack_ping_count > 0)
fatal("Only one -PB, -PA, or -PT option is allowed. Combine port ranges with commas.");
/* validate_scan_lists takes case of changing this to
@@ -1180,7 +1188,8 @@ void parse_options(int argc, char **argv) {
getpts_simple(DEFAULT_TCP_PROBE_PORT_SPEC, SCAN_TCP_PORT, &ports.ack_ping_ports, &ports.ack_ping_count);
assert(ports.ack_ping_count > 0);
}
} else if (*optarg == 'U') {
break;
case 'U':
if (ports.udp_ping_count > 0)
fatal("Only one -PU option is allowed. Combine port ranges with commas.");
o.pingtype |= (PINGTYPE_UDP);
@@ -1192,7 +1201,8 @@ void parse_options(int argc, char **argv) {
getpts_simple(DEFAULT_UDP_PROBE_PORT_SPEC, SCAN_UDP_PORT, &ports.udp_ping_ports, &ports.udp_ping_count);
assert(ports.udp_ping_count > 0);
}
} else if (*optarg == 'Y') {
break;
case 'Y':
if (ports.sctp_ping_count > 0)
fatal("Only one -PY option is allowed. Combine port ranges with commas.");
o.pingtype |= (PINGTYPE_SCTP_INIT);
@@ -1204,7 +1214,8 @@ void parse_options(int argc, char **argv) {
getpts_simple(DEFAULT_SCTP_PROBE_PORT_SPEC, SCAN_SCTP_PORT, &ports.sctp_ping_ports, &ports.sctp_ping_count);
assert(ports.sctp_ping_count > 0);
}
} else if (*optarg == 'B') {
break;
case 'B':
if (ports.ack_ping_count > 0)
fatal("Only one -PB, -PA, or -PT option is allowed. Combine port ranges with commas.");
o.pingtype = DEFAULT_IPV4_PING_TYPES;
@@ -1216,7 +1227,8 @@ void parse_options(int argc, char **argv) {
getpts_simple(DEFAULT_TCP_PROBE_PORT_SPEC, SCAN_TCP_PORT, &ports.ack_ping_ports, &ports.ack_ping_count);
assert(ports.ack_ping_count > 0);
}
} else if (*optarg == 'O') {
break;
case 'O':
if (ports.proto_ping_count > 0)
fatal("Only one -PO option is allowed. Combine protocol ranges with commas.");
o.pingtype |= PINGTYPE_PROTO;
@@ -1228,8 +1240,11 @@ void parse_options(int argc, char **argv) {
getpts_simple(DEFAULT_PROTO_PROBE_PORT_SPEC, SCAN_PROTOCOLS, &ports.proto_ping_ports, &ports.proto_ping_count);
assert(ports.proto_ping_count > 0);
}
} else {
break;
default:
fatal("Illegal Argument to -P, use -Pn, -PE, -PS, -PA, -PP, -PM, -PU, -PY, or -PO");
break;
}
}
break;
case 'p':