Fix coding style of args parser

This commit is contained in:
Vadim Vetrov 2024-08-09 15:58:52 +03:00
parent 4963258c0b
commit 8d4fb1f7ad
No known key found for this signature in database
GPG Key ID: E8A308689D7A73A5

View File

@ -80,7 +80,9 @@ static void print_version() {
} }
static void print_usage(const char *argv0) { static void print_usage(const char *argv0) {
printf("Usage: %s [ OPTIONS ] <queue_num> \n", argv0); print_version();
printf("Usage: %s <queue_num> [ OPTIONS ] \n", argv0);
printf("Options:\n"); printf("Options:\n");
printf("\t--sni-domains=<comma separated domain list>|all\n"); printf("\t--sni-domains=<comma separated domain list>|all\n");
printf("\t--fake-sni={ack,ttl,none}\n"); printf("\t--fake-sni={ack,ttl,none}\n");
@ -90,30 +92,31 @@ static void print_usage(const char *argv0) {
printf("\t--threads=<threads number>\n"); printf("\t--threads=<threads number>\n");
printf("\t--silent\n"); printf("\t--silent\n");
printf("\t--no-gso\n"); printf("\t--no-gso\n");
printf("\n");
} }
#define OPT_SNI_DOMAINS 1 #define OPT_SNI_DOMAINS 1
#define OPT_FAKE_SNI 2 #define OPT_FAKE_SNI 2
#define OPT_FAKE_SNI_TTL 3 #define OPT_FAKE_SNI_TTL 3
#define OPT_FRAG 4 #define OPT_FRAG 4
#define OPT_SEG2DELAY 5 #define OPT_SEG2DELAY 5
#define OPT_THREADS 6 #define OPT_THREADS 6
#define OPT_SILENT 7 #define OPT_SILENT 7
#define OPT_NO_GSO 8 #define OPT_NO_GSO 8
static struct option long_opt[] = { static struct option long_opt[] = {
{"help", 0, 0, 'h'}, {"help", 0, 0, 'h'},
{"version", 0, 0, 'v'}, {"version", 0, 0, 'v'},
{"sni-domains", 1, 0, OPT_SNI_DOMAINS}, {"sni-domains", 1, 0, OPT_SNI_DOMAINS},
{"fake-sni", 1, 0, OPT_FAKE_SNI}, {"fake-sni", 1, 0, OPT_FAKE_SNI},
{"fake-sni-ttl", 1, 0, OPT_FAKE_SNI_TTL}, {"fake-sni-ttl", 1, 0, OPT_FAKE_SNI_TTL},
{"frag", 1, 0, OPT_FRAG}, {"frag", 1, 0, OPT_FRAG},
{"seg2delay", 1, 0, OPT_SEG2DELAY}, {"seg2delay", 1, 0, OPT_SEG2DELAY},
{"threads", 1, 0, OPT_THREADS}, {"threads", 1, 0, OPT_THREADS},
{"silent", 0, 0, OPT_SILENT}, {"silent", 0, 0, OPT_SILENT},
{"no-gso", 0, 0, OPT_NO_GSO}, {"no-gso", 0, 0, OPT_NO_GSO},
{0,0,0,0} {0,0,0,0}
}; };
static int parse_args(int argc, char *argv[]) { static int parse_args(int argc, char *argv[]) {
int opt; int opt;
@ -121,21 +124,19 @@ static int parse_args(int argc, char *argv[]) {
long num; long num;
if (argc < 2) { if (argc < 2) {
print_usage(argv[0]); print_usage(argv[0]);
errno = EINVAL; errno = EINVAL;
return -1; return -1;
} }
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]);
errno = 0; goto out;
return 1; case 'v':
case 'v': print_version();
print_version(); goto out;
errno = 0;
return 1;
case OPT_SILENT: case OPT_SILENT:
config.verbose = 0; config.verbose = 0;
break; break;
@ -143,29 +144,29 @@ static int parse_args(int argc, char *argv[]) {
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);
printf("asdffdsa\n");
break; break;
case OPT_FRAG: case OPT_FRAG:
if (strcmp(optarg, "tcp") == 0) { if (strcmp(optarg, "tcp") == 0) {
config.fragmentation_strategy = FRAG_STRAT_TCP; config.fragmentation_strategy = FRAG_STRAT_TCP;
} else if (strcmp(optarg, "ip") == 0) { } else if (strcmp(optarg, "ip") == 0) {
config.fragmentation_strategy = FRAG_STRAT_IP; config.fragmentation_strategy = FRAG_STRAT_IP;
} else if (strcmp(optarg, "none") == 0) { } else if (strcmp(optarg, "none") == 0) {
config.fragmentation_strategy = FRAG_STRAT_NONE; config.fragmentation_strategy = FRAG_STRAT_NONE;
} else { } else {
errno = EINVAL; printf("Invalid option %s\n", long_opt[optIdx].name);
printf("Invalid option %s\n", long_opt[optIdx].name); goto error;
return -1;
} }
break; break;
case OPT_FAKE_SNI: case OPT_FAKE_SNI:
if (strcmp(optarg, "ack") == 0) { if (strcmp(optarg, "ack") == 0) {
config.fake_sni_strategy = FKSN_STRAT_ACK_SEQ; config.fake_sni_strategy = FKSN_STRAT_ACK_SEQ;
} else if (strcmp(optarg, "ttl") == 0) { } else if (strcmp(optarg, "ttl") == 0) {
config.fake_sni_strategy = FKSN_STRAT_TTL; config.fake_sni_strategy = FKSN_STRAT_TTL;
@ -173,48 +174,59 @@ static int parse_args(int argc, char *argv[]) {
config.fake_sni_strategy = FKSN_STRAT_NONE; config.fake_sni_strategy = FKSN_STRAT_NONE;
} else { } else {
errno = EINVAL; errno = EINVAL;
printf("Invalid option %s\n", long_opt[optIdx].name); printf("Invalid option %s\n", long_opt[optIdx].name);
return -1; goto error;
} }
break; break;
case OPT_SEG2DELAY: 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); printf("Invalid option %s\n", long_opt[optIdx].name);
return -1; goto error;
} }
config.seg2_delay = num; config.seg2_delay = num;
break; break;
case OPT_THREADS: case OPT_THREADS:
num = parse_numeric_option(optarg); num = parse_numeric_option(optarg);
if (errno != 0 || num < 0 || num > MAX_THREADS) { if (errno != 0 || num < 0 || num > MAX_THREADS) {
printf("Invalid option %s\n", long_opt[optIdx].name); printf("Invalid option %s\n", long_opt[optIdx].name);
return -1; goto error;
} }
config.threads = num; config.threads = num;
break; break;
case OPT_FAKE_SNI_TTL: case OPT_FAKE_SNI_TTL:
num = parse_numeric_option(optarg); num = parse_numeric_option(optarg);
if (errno != 0 || num < 0 || num > 255) { if (errno != 0 || num < 0 || num > 255) {
printf("Invalid option %s\n", long_opt[optIdx].name); printf("Invalid option %s\n", long_opt[optIdx].name);
return -1; goto error;
} }
config.fake_sni_ttl = num; config.fake_sni_ttl = num;
break; break;
default: default:
printf("Invalid option %s\n", long_opt[optIdx].name); printf("Invalid option %s\n", long_opt[optIdx].name);
errno = EINVAL; goto error;
return -1; }
}
} }
config.queue_start_num = parse_numeric_option(argv[optind]); config.queue_start_num = parse_numeric_option(argv[optind]);
if (errno != 0) { if (errno != 0) {
printf("Invalid queue number\n"); printf("Invalid queue number\n");
return -1; goto error;
} }
errno = 0;
return 0; return 0;
out:
errno = 0;
return 1;
error:
print_usage(argv[0]);
errno = EINVAL;
return -1;
} }
static int open_socket(struct mnl_socket **_nl) { static int open_socket(struct mnl_socket **_nl) {