From 24826f851d2e36f8e56d03ff19f875c8f1685bbb Mon Sep 17 00:00:00 2001 From: zabbius Date: Mon, 12 Aug 2024 04:52:03 +0300 Subject: [PATCH 1/2] removed duplicated code in args.c --- args.c | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/args.c b/args.c index fd1b49e..db4a2df 100644 --- a/args.c +++ b/args.c @@ -163,7 +163,6 @@ int parse_args(int argc, char *argv[]) { } else if (strcmp(optarg, "none") == 0) { config.fragmentation_strategy = FRAG_STRAT_NONE; } else { - printf("Invalid option %s\n", long_opt[optIdx].name); goto error; } @@ -175,7 +174,6 @@ int parse_args(int argc, char *argv[]) { config.frag_sni_faked = 0; } else { errno = EINVAL; - printf("Invalid option %s\n", long_opt[optIdx].name); goto error; } @@ -187,7 +185,6 @@ int parse_args(int argc, char *argv[]) { config.frag_sni_reverse = 0; } else { errno = EINVAL; - printf("Invalid option %s\n", long_opt[optIdx].name); goto error; } @@ -199,7 +196,6 @@ int parse_args(int argc, char *argv[]) { config.faking_strategy = FAKE_STRAT_TTL; } else { errno = EINVAL; - printf("Invalid option %s\n", long_opt[optIdx].name); goto error; } @@ -207,7 +203,6 @@ int parse_args(int argc, char *argv[]) { case OPT_FAKING_TTL: num = parse_numeric_option(optarg); if (errno != 0 || num < 0 || num > 255) { - printf("Invalid option %s\n", long_opt[optIdx].name); goto error; } @@ -221,7 +216,6 @@ int parse_args(int argc, char *argv[]) { config.fake_sni = 0; } else { errno = EINVAL; - printf("Invalid option %s\n", long_opt[optIdx].name); goto error; } @@ -229,7 +223,6 @@ int parse_args(int argc, char *argv[]) { case OPT_FAKE_SNI_SEQ_LEN: num = parse_numeric_option(optarg); if (errno != 0 || num < 0 || num > 255) { - printf("Invalid option %s\n", long_opt[optIdx].name); goto error; } @@ -238,7 +231,6 @@ int parse_args(int argc, char *argv[]) { case OPT_FK_WINSIZE: num = parse_numeric_option(optarg); if (errno != 0 || num < 0) { - printf("Invalid option %s\n", long_opt[optIdx].name); goto error; } @@ -247,7 +239,6 @@ int parse_args(int argc, char *argv[]) { case OPT_SEG2DELAY: num = parse_numeric_option(optarg); if (errno != 0 || num < 0) { - printf("Invalid option %s\n", long_opt[optIdx].name); goto error; } @@ -256,7 +247,6 @@ int parse_args(int argc, char *argv[]) { case OPT_THREADS: num = parse_numeric_option(optarg); if (errno != 0 || num < 0 || num > MAX_THREADS) { - printf("Invalid option %s\n", long_opt[optIdx].name); goto error; } @@ -265,7 +255,6 @@ int parse_args(int argc, char *argv[]) { case OPT_QUEUE_NUM: num = parse_numeric_option(optarg); if (errno != 0 || num < 0) { - printf("Invalid option %s\n", long_opt[optIdx].name); goto error; } @@ -284,6 +273,7 @@ out: errno = 0; return 1; error: + printf("Invalid option %s\n", long_opt[optIdx].name); print_usage(argv[0]); errno = EINVAL; return -1; From 219062aae227cbd291dee5a4eaca189dde4cec71 Mon Sep 17 00:00:00 2001 From: Vadim Vetrov Date: Mon, 12 Aug 2024 15:22:04 +0300 Subject: [PATCH 2/2] Fix segfault bufs, update coding style --- args.c | 233 ++++++++++++++++++++++++++++----------------------------- 1 file changed, 115 insertions(+), 118 deletions(-) diff --git a/args.c b/args.c index db4a2df..61c9a8f 100644 --- a/args.c +++ b/args.c @@ -134,146 +134,143 @@ int parse_args(int argc, char *argv[]) { while ((opt = getopt_long(argc, argv, "hv", long_opt, &optIdx)) != -1) { switch (opt) { - case 'h': - print_usage(argv[0]); - goto out; - case 'v': - print_version(); - goto out; - case OPT_SILENT: - config.verbose = 0; - break; - case OPT_NO_GSO: - config.use_gso = 0; - break; - case OPT_SNI_DOMAINS: - if (!strcmp(optarg, "all")) { - config.all_domains = 1; - } + case 'h': + print_usage(argv[0]); + goto stop_exec; + case 'v': + print_version(); + goto stop_exec; + case OPT_SILENT: + config.verbose = 0; + break; + case OPT_NO_GSO: + config.use_gso = 0; + break; + case OPT_SNI_DOMAINS: + if (!strcmp(optarg, "all")) { + config.all_domains = 1; + } - config.domains_str = optarg; - config.domains_strlen = strlen(config.domains_str); + config.domains_str = optarg; + 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; - 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 error; - } + break; + case OPT_FRAG_SNI_FAKED: + if (strcmp(optarg, "1") == 0) { + config.frag_sni_faked = 1; + } else if (strcmp(optarg, "0") == 0) { + config.frag_sni_faked = 0; + } else { + goto invalid_opt; + } - break; - case OPT_FRAG_SNI_FAKED: - if (strcmp(optarg, "1") == 0) { - config.frag_sni_faked = 1; - } else if (strcmp(optarg, "0") == 0) { - config.frag_sni_faked = 0; - } else { - errno = EINVAL; - goto error; - } + break; + case OPT_FRAG_SNI_REVERSE: + if (strcmp(optarg, "1") == 0) { + config.frag_sni_reverse = 1; + } else if (strcmp(optarg, "0") == 0) { + config.frag_sni_reverse = 0; + } else { + goto invalid_opt; + } - break; - case OPT_FRAG_SNI_REVERSE: - if (strcmp(optarg, "1") == 0) { - config.frag_sni_reverse = 1; - } else if (strcmp(optarg, "0") == 0) { - config.frag_sni_reverse = 0; - } else { - errno = EINVAL; - goto error; - } + break; + case OPT_FAKING_STRATEGY: + if (strcmp(optarg, "ack") == 0) { + config.faking_strategy = FAKE_STRAT_ACK_SEQ; + } else if (strcmp(optarg, "ttl") == 0) { + config.faking_strategy = FAKE_STRAT_TTL; + } else { + goto invalid_opt; + } - break; - case OPT_FAKING_STRATEGY: - if (strcmp(optarg, "ack") == 0) { - config.faking_strategy = FAKE_STRAT_ACK_SEQ; - } else if (strcmp(optarg, "ttl") == 0) { - config.faking_strategy = FAKE_STRAT_TTL; - } else { - errno = EINVAL; - goto error; - } + break; + case OPT_FAKING_TTL: + num = parse_numeric_option(optarg); + if (errno != 0 || num < 0 || num > 255) { + goto invalid_opt; + } - break; - case OPT_FAKING_TTL: - num = parse_numeric_option(optarg); - if (errno != 0 || num < 0 || num > 255) { - goto error; - } + config.faking_ttl = num; + break; - config.faking_ttl = num; - break; + case OPT_FAKE_SNI: + 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: - if (strcmp(optarg, "1") == 0) { - config.fake_sni = 1; - } else if (strcmp(optarg, "0") == 0) { - config.fake_sni = 0; - } else { - errno = EINVAL; - goto error; - } + break; + case OPT_FAKE_SNI_SEQ_LEN: + num = parse_numeric_option(optarg); + if (errno != 0 || num < 0 || num > 255) { + goto invalid_opt; + } - break; - case OPT_FAKE_SNI_SEQ_LEN: - num = parse_numeric_option(optarg); - if (errno != 0 || num < 0 || num > 255) { - goto error; - } + config.fake_sni_seq_len = num; + break; + case OPT_FK_WINSIZE: + num = parse_numeric_option(optarg); + if (errno != 0 || num < 0) { + goto invalid_opt; + } - config.fake_sni_seq_len = num; - break; - case OPT_FK_WINSIZE: - num = parse_numeric_option(optarg); - if (errno != 0 || num < 0) { - goto error; - } + config.fk_winsize = num; + break; + case OPT_SEG2DELAY: + num = parse_numeric_option(optarg); + if (errno != 0 || num < 0) { + goto invalid_opt; + } - config.fk_winsize = num; - break; - case OPT_SEG2DELAY: - num = parse_numeric_option(optarg); - if (errno != 0 || num < 0) { - goto error; - } + config.seg2_delay = num; + break; + case OPT_THREADS: + num = parse_numeric_option(optarg); + if (errno != 0 || num < 0 || num > MAX_THREADS) { + goto invalid_opt; + } - config.seg2_delay = num; - break; - case OPT_THREADS: - num = parse_numeric_option(optarg); - if (errno != 0 || num < 0 || num > MAX_THREADS) { - goto error; - } + config.threads = num; + break; + case OPT_QUEUE_NUM: + num = parse_numeric_option(optarg); + if (errno != 0 || num < 0) { + goto invalid_opt; + } - config.threads = num; - break; - case OPT_QUEUE_NUM: - num = parse_numeric_option(optarg); - if (errno != 0 || num < 0) { - goto error; - } - - config.queue_start_num = num; - break; - default: - goto error; + config.queue_start_num = num; + break; + default: + goto error; } } - +// out: errno = 0; return 0; -out: +stop_exec: errno = 0; return 1; -error: + +invalid_opt: printf("Invalid option %s\n", long_opt[optIdx].name); +error: print_usage(argv[0]); errno = EINVAL; return -1;