Merge #57 by @zabbius to dev

minor - removed duplicated code in args.c
This commit is contained in:
Vadim Vetrov 2024-08-12 15:24:57 +03:00
commit aafa1a728a
No known key found for this signature in database
GPG Key ID: E8A308689D7A73A5

243
args.c
View File

@ -134,155 +134,142 @@ int parse_args(int argc, char *argv[]) {
while ((opt = getopt_long(argc, argv, "hv", long_opt, &optIdx)) != -1) { while ((opt = getopt_long(argc, argv, "hv", long_opt, &optIdx)) != -1) {
switch (opt) { switch (opt) {
case 'h': case 'h':
print_usage(argv[0]); print_usage(argv[0]);
goto out; goto stop_exec;
case 'v': case 'v':
print_version(); print_version();
goto out; goto stop_exec;
case OPT_SILENT: case OPT_SILENT:
config.verbose = 0; config.verbose = 0;
break; break;
case OPT_NO_GSO: case OPT_NO_GSO:
config.use_gso = 0; config.use_gso = 0;
break; break;
case OPT_SNI_DOMAINS: case OPT_SNI_DOMAINS:
if (!strcmp(optarg, "all")) { if (!strcmp(optarg, "all")) {
config.all_domains = 1; config.all_domains = 1;
} }
config.domains_str = optarg; config.domains_str = optarg;
config.domains_strlen = strlen(config.domains_str); config.domains_strlen = strlen(config.domains_str);
break;
case OPT_FRAG:
if (strcmp(optarg, "tcp") == 0) {
config.fragmentation_strategy = FRAG_STRAT_TCP;
} else if (strcmp(optarg, "ip") == 0) {
config.fragmentation_strategy = FRAG_STRAT_IP;
} else if (strcmp(optarg, "none") == 0) {
config.fragmentation_strategy = FRAG_STRAT_NONE;
} else {
goto invalid_opt;
}
break; break;
case OPT_FRAG: case OPT_FRAG_SNI_FAKED:
if (strcmp(optarg, "tcp") == 0) { if (strcmp(optarg, "1") == 0) {
config.fragmentation_strategy = FRAG_STRAT_TCP; config.frag_sni_faked = 1;
} else if (strcmp(optarg, "ip") == 0) { } else if (strcmp(optarg, "0") == 0) {
config.fragmentation_strategy = FRAG_STRAT_IP; config.frag_sni_faked = 0;
} else if (strcmp(optarg, "none") == 0) { } else {
config.fragmentation_strategy = FRAG_STRAT_NONE; goto invalid_opt;
} else { }
printf("Invalid option %s\n", long_opt[optIdx].name);
goto error;
}
break; break;
case OPT_FRAG_SNI_FAKED: case OPT_FRAG_SNI_REVERSE:
if (strcmp(optarg, "1") == 0) { if (strcmp(optarg, "1") == 0) {
config.frag_sni_faked = 1; config.frag_sni_reverse = 1;
} else if (strcmp(optarg, "0") == 0) { } else if (strcmp(optarg, "0") == 0) {
config.frag_sni_faked = 0; config.frag_sni_reverse = 0;
} else { } else {
errno = EINVAL; goto invalid_opt;
printf("Invalid option %s\n", long_opt[optIdx].name); }
goto error;
}
break; break;
case OPT_FRAG_SNI_REVERSE: case OPT_FAKING_STRATEGY:
if (strcmp(optarg, "1") == 0) { if (strcmp(optarg, "ack") == 0) {
config.frag_sni_reverse = 1; config.faking_strategy = FAKE_STRAT_ACK_SEQ;
} else if (strcmp(optarg, "0") == 0) { } else if (strcmp(optarg, "ttl") == 0) {
config.frag_sni_reverse = 0; config.faking_strategy = FAKE_STRAT_TTL;
} else { } else {
errno = EINVAL; goto invalid_opt;
printf("Invalid option %s\n", long_opt[optIdx].name); }
goto error;
}
break; break;
case OPT_FAKING_STRATEGY: case OPT_FAKING_TTL:
if (strcmp(optarg, "ack") == 0) { num = parse_numeric_option(optarg);
config.faking_strategy = FAKE_STRAT_ACK_SEQ; if (errno != 0 || num < 0 || num > 255) {
} else if (strcmp(optarg, "ttl") == 0) { goto invalid_opt;
config.faking_strategy = FAKE_STRAT_TTL; }
} else {
errno = EINVAL;
printf("Invalid option %s\n", long_opt[optIdx].name);
goto error;
}
break; config.faking_ttl = num;
case OPT_FAKING_TTL: break;
num = parse_numeric_option(optarg);
if (errno != 0 || num < 0 || num > 255) {
printf("Invalid option %s\n", long_opt[optIdx].name);
goto error;
}
config.faking_ttl = num; case OPT_FAKE_SNI:
break; if (strcmp(optarg, "1") == 0) {
config.fake_sni = 1;
} else if (strcmp(optarg, "0") == 0) {
config.fake_sni = 0;
} else {
goto invalid_opt;
}
case OPT_FAKE_SNI: break;
if (strcmp(optarg, "1") == 0) { case OPT_FAKE_SNI_SEQ_LEN:
config.fake_sni = 1; num = parse_numeric_option(optarg);
} else if (strcmp(optarg, "0") == 0) { if (errno != 0 || num < 0 || num > 255) {
config.fake_sni = 0; goto invalid_opt;
} else { }
errno = EINVAL;
printf("Invalid option %s\n", long_opt[optIdx].name);
goto error;
}
break; config.fake_sni_seq_len = num;
case OPT_FAKE_SNI_SEQ_LEN: break;
num = parse_numeric_option(optarg); case OPT_FK_WINSIZE:
if (errno != 0 || num < 0 || num > 255) { num = parse_numeric_option(optarg);
printf("Invalid option %s\n", long_opt[optIdx].name); if (errno != 0 || num < 0) {
goto error; goto invalid_opt;
} }
config.fake_sni_seq_len = num; config.fk_winsize = num;
break; break;
case OPT_FK_WINSIZE: case OPT_SEG2DELAY:
num = parse_numeric_option(optarg); num = parse_numeric_option(optarg);
if (errno != 0 || num < 0) { if (errno != 0 || num < 0) {
printf("Invalid option %s\n", long_opt[optIdx].name); goto invalid_opt;
goto error; }
}
config.fk_winsize = num; config.seg2_delay = num;
break; break;
case OPT_SEG2DELAY: case OPT_THREADS:
num = parse_numeric_option(optarg); num = parse_numeric_option(optarg);
if (errno != 0 || num < 0) { if (errno != 0 || num < 0 || num > MAX_THREADS) {
printf("Invalid option %s\n", long_opt[optIdx].name); goto invalid_opt;
goto error; }
}
config.seg2_delay = num; config.threads = num;
break; break;
case OPT_THREADS: case OPT_QUEUE_NUM:
num = parse_numeric_option(optarg); num = parse_numeric_option(optarg);
if (errno != 0 || num < 0 || num > MAX_THREADS) { if (errno != 0 || num < 0) {
printf("Invalid option %s\n", long_opt[optIdx].name); goto invalid_opt;
goto error; }
}
config.threads = num; config.queue_start_num = num;
break; break;
case OPT_QUEUE_NUM: default:
num = parse_numeric_option(optarg); goto error;
if (errno != 0 || num < 0) {
printf("Invalid option %s\n", long_opt[optIdx].name);
goto error;
}
config.queue_start_num = num;
break;
default:
goto error;
} }
} }
// out:
errno = 0; errno = 0;
return 0; return 0;
out: stop_exec:
errno = 0; errno = 0;
return 1; return 1;
invalid_opt:
printf("Invalid option %s\n", long_opt[optIdx].name);
error: error:
print_usage(argv[0]); print_usage(argv[0]);
errno = EINVAL; errno = EINVAL;