diff options
Diffstat (limited to 'client.c')
-rw-r--r-- | client.c | 63 |
1 files changed, 56 insertions, 7 deletions
@@ -1,9 +1,16 @@ /* SPDX-License-Identifier: Unlicense */ +#include "debug.h" #include "proto.h" #include "proto_io.h" #include "timespec/timespec.h" +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wunused-function" +#define OPTPARSE_IMPLEMENTATION +#define OPTPARSE_API static +#include "optparse/optparse.h" +#pragma GCC diagnostic pop #include <assert.h> #include <errno.h> @@ -46,7 +53,9 @@ static void HandleFrame( self->connection_id = frame->connection_id; self->connected = true; } else if (frame->type == FT_SYN && self->connected) { - printf("client_rt %5" PRIu32 "\n", self->client_roundtrip_prev); + if (g_log_level >= LOG_LEVEL_DEBUG) { + fprintf(stderr, "client_rt %5" PRIu32 "\n", self->client_roundtrip_prev); + } struct frame outgoing = *frame; outgoing.type = FT_SYNACK, outgoing.syn = (struct frame_data_syn){ @@ -123,17 +132,57 @@ static void sigintHandler(int a) (void) a; } -int main(int const argc, char *const argv[]) +static void PrintUsage(FILE *s, const char *argv0) { - if (argc < 3) { - fprintf(stderr, "Usage: %s <address> <port>\n", argv[0]); - return 1; + // Please, keep all lines in 80 columns range when printed. + fprintf(s, + "Usage: %s [options] <address> <port>\n" + "Options:\n" + " -h, --help, Show this message.\n" + " -d, --debug-level=<level>,\n" + " Set id of debug log level: 0 means no logs, \n" + " 1 means info, 2 means some debug,\n" + " 3 is for maximum trace\n" + , argv0); +} + +int main(int argc, char *argv[]) +{ + (void) argc; + struct optparse_long longopts[] = { + {"help", 'h', OPTPARSE_NONE}, + {"debug-level", 'd', OPTPARSE_REQUIRED}, + {0}, + }; + struct optparse options; + optparse_init(&options, argv); + // Parse opts + int option; + while ((option = optparse_long(&options, longopts, NULL)) != -1) { + switch (option) { + case 'h': + PrintUsage(stdout, argv[0]); + return EXIT_SUCCESS; + break; + case 'd': + g_log_level = atoi(options.optarg); + break; + case '?': + fprintf(stderr, "main: optparse_long: Error: \"%s\"\n", options.errmsg); + return EXIT_FAILURE; + } + } + char const *const addr_arg = optparse_arg(&options); + char const *const port_arg = optparse_arg(&options); + if (addr_arg == NULL || port_arg == NULL) { + PrintUsage(stdout, argv[0]); + return EXIT_SUCCESS; } - unsigned const addr = ParseAddress(argv[1], stderr); + unsigned const addr = ParseAddress(addr_arg, stderr); if (addr == INADDR_NONE) { return 1; } - int const port = atoi(argv[2]); + int const port = atoi(port_arg); if (port <= 0 || port > UINT16_MAX) { fprintf(stderr, "Invalid port specified\n"); return 1; |