From 50a65c5358beef1cc2859e4bf6e9ae7805c29087 Mon Sep 17 00:00:00 2001 From: Toni Uhlig Date: Tue, 21 Jul 2020 15:55:16 +0200 Subject: moved inlined functions from utils.h to non-inlined versions in utils.c Signed-off-by: Toni Uhlig --- Makefile | 4 ++-- utils.c | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ utils.h | 84 ++++------------------------------------------------------------ 3 files changed, 90 insertions(+), 82 deletions(-) create mode 100644 utils.c diff --git a/Makefile b/Makefile index c204ca9..766e9dc 100644 --- a/Makefile +++ b/Makefile @@ -23,8 +23,8 @@ CFLAGS += -Wall -Wextra -Wstrict-prototypes -std=gnu11 $(EXTRA_CFLAGS) -D_GNU_SO $(shell $(PKG_CONFIG_BIN) --cflags libsodium) \ $(shell $(PKG_CONFIG_BIN) --cflags libevent) -HEADER_TARGETS = common-event2.h common-sodium.h logging.h protocol.h -BUILD_TARGETS = common-event2.o common-sodium.o logging.o protocol.o +HEADER_TARGETS = utils.h common-event2.h common-sodium.h logging.h protocol.h +BUILD_TARGETS = utils.o common-event2.o common-sodium.o logging.o protocol.o SO_NAME=libsodium-tcp.so APP_HEADER_TARGETS = $(HEADER_TARGETS) diff --git a/utils.c b/utils.c new file mode 100644 index 0000000..6f13027 --- /dev/null +++ b/utils.c @@ -0,0 +1,84 @@ +#include +#include +#include +#include +#include +#include + +#include "utils.h" + +__attribute__((noreturn)) void usage(const char * const arg0) +{ + fprintf(stderr, "usage: %s -k [SODIUM-KEY] -h [HOST] -p [PORT] -f [FILE]\n", arg0); + exit(EXIT_FAILURE); +} + +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 = strdup(optarg); + 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 == NULL) { + opts->port = strdup("5555"); + } + if (opts->key_string != NULL) { + opts->key_length = strlen(opts->key_string); + } +} + +char * prettify_bytes_with_units(char * const out, size_t out_size, unsigned long long bytes) +{ + static char const * const unit_prefixes[] = {"", "Kilo", "Mega", "Giga", "Tera"}; + size_t const unit_prefixes_length = sizeof(unit_prefixes) / sizeof(unit_prefixes[0]); + unsigned char unit_prefixes_index = 0; + size_t const convert_bytes_every = 1024; + + while (bytes / convert_bytes_every > 0 && unit_prefixes_index < unit_prefixes_length) { + bytes /= convert_bytes_every; + unit_prefixes_index++; + } + + snprintf(out, out_size, "%llu %sBytes", bytes, unit_prefixes[unit_prefixes_index]); + + return out; +} + +int hostname_to_address(char const * const host, char const * const port, struct addrinfo ** const result) +{ + int s; + struct addrinfo hints; + + memset(&hints, 0, sizeof(struct addrinfo)); + hints.ai_family = AF_UNSPEC; + hints.ai_socktype = SOCK_STREAM; + hints.ai_flags = AI_PASSIVE; + + s = getaddrinfo(host, port, &hints, result); + if (s != 0) { + return s; + } + + return 0; +} diff --git a/utils.h b/utils.h index 0774007..0f8e07d 100644 --- a/utils.h +++ b/utils.h @@ -1,13 +1,7 @@ #ifndef UTILS_H #define UTILS_H 1 -#include -#include #include -#include -#include -#include -#include struct cmd_options { /* server: private key @@ -29,82 +23,12 @@ struct cmd_options { 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); -} +__attribute__((noreturn)) void usage(const char * const arg0); -static inline void parse_cmdline(struct cmd_options * const opts, int argc, char ** const argv) -{ - int opt; +void parse_cmdline(struct cmd_options * const opts, int argc, char ** const argv); - 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 = strdup(optarg); - break; - case 'f': - opts->filepath = strdup(optarg); - break; - default: - usage(argv[0]); - } - } +char * prettify_bytes_with_units(char * const out, size_t out_size, unsigned long long bytes); - if (opts->host == NULL) { - opts->host = strdup("127.0.0.1"); - } - if (opts->port == NULL) { - opts->port = strdup("5555"); - } - if (opts->key_string != NULL) { - opts->key_length = strlen(opts->key_string); - } -} +int hostname_to_address(char const * const host, char const * const port, struct addrinfo ** const result); -static inline char * prettify_bytes_with_units(char * const out, size_t out_size, - unsigned long long bytes) -{ - static char const * const unit_prefixes[] = {"","Kilo","Mega","Giga","Tera"}; - size_t const unit_prefixes_length = sizeof(unit_prefixes)/sizeof(unit_prefixes[0]); - unsigned char unit_prefixes_index = 0; - size_t const convert_bytes_every = 1024; - - while (bytes / convert_bytes_every > 0 && unit_prefixes_index < unit_prefixes_length) - { - bytes /= convert_bytes_every; - unit_prefixes_index++; - } - - snprintf(out, out_size, "%llu %sBytes", bytes, unit_prefixes[unit_prefixes_index]); - - return out; -} - -static inline int hostname_to_address(char const * const host, char const * const port, - struct addrinfo ** const result) -{ - int s; - struct addrinfo hints; - - memset(&hints, 0, sizeof(struct addrinfo)); - hints.ai_family = AF_UNSPEC; - hints.ai_socktype = SOCK_STREAM; - hints.ai_flags = AI_PASSIVE; - - s = getaddrinfo(host, port, &hints, result); - if (s != 0) { - return s; - } - - return 0; -} #endif -- cgit v1.2.3