diff options
author | Toni Uhlig <matzeton@googlemail.com> | 2020-05-22 13:43:46 +0200 |
---|---|---|
committer | Toni Uhlig <matzeton@googlemail.com> | 2020-05-22 14:48:29 +0200 |
commit | c394c09330760985d282cb866a06dea6294012aa (patch) | |
tree | 5a120d309ef25552b719844474993184a8707608 /utils.h |
first public release
Signed-off-by: Toni Uhlig <matzeton@googlemail.com>
Diffstat (limited to 'utils.h')
-rw-r--r-- | utils.h | 70 |
1 files changed, 70 insertions, 0 deletions
@@ -0,0 +1,70 @@ +#ifndef UTILS_H +#define UTILS_H 1 + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +struct cmd_options { + /* server: private key + * client: server public key + */ + char * key_string; + size_t key_length; + /* server: listen host + * client: remote host + */ + char * host; + /* server: listen port + * client: remote port + */ + int port; + /* server: path to write to, received from client via PDU-type DATA + * client: path to read from, send it via PDU-type DATA + */ + char * filepath; +}; + +__attribute__((noreturn)) static inline void usage(const char * const arg0) +{ + fprintf(stderr, "usage: %s -k [SODIUM-KEY] -h [HOST] -p [PORT] -f [FILE]\n", arg0); + exit(EXIT_FAILURE); +} + +static inline void parse_cmdline(struct cmd_options * const opts, int argc, char ** const argv) +{ + int opt; + + while ((opt = getopt(argc, argv, "k:h:p:f:h")) != -1) { + switch (opt) { + case 'k': + opts->key_string = strdup(optarg); + memset(optarg, '*', strlen(optarg)); + break; + case 'h': + opts->host = strdup(optarg); + break; + case 'p': + opts->port = atoi(optarg); /* meh, strtol is king */ + break; + case 'f': + opts->filepath = strdup(optarg); + break; + default: + usage(argv[0]); + } + } + + if (opts->host == NULL) { + opts->host = strdup("127.0.0.1"); + } + if (opts->port == 0) { + opts->port = 5555; + } + if (opts->key_string != NULL) { + opts->key_length = strlen(opts->key_string); + } +} + +#endif |