aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorToni Uhlig <matzeton@googlemail.com>2020-07-21 15:55:16 +0200
committerToni Uhlig <matzeton@googlemail.com>2020-07-21 15:55:16 +0200
commit50a65c5358beef1cc2859e4bf6e9ae7805c29087 (patch)
tree63477d23e29c5bd2ecaccae40ac717934a4d21fc
parentf7721bf87405c2cc81ba8a67c79c08b72af7ccaa (diff)
moved inlined functions from utils.h to non-inlined versions in utils.c
Signed-off-by: Toni Uhlig <matzeton@googlemail.com>
-rw-r--r--Makefile4
-rw-r--r--utils.c84
-rw-r--r--utils.h84
3 files changed, 90 insertions, 82 deletions
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 <netdb.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <unistd.h>
+
+#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 <netdb.h>
-#include <stdio.h>
#include <stdlib.h>
-#include <string.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <unistd.h>
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