diff --git a/src/bench.c b/src/bench.c index c60cddf..4f52c1a 100644 --- a/src/bench.c +++ b/src/bench.c @@ -1,11 +1,10 @@ #include "common.h" -#include -#include -#include -#include -#include #include +#include +#include +#include +#include #include "../argz/argz.h" #include "../mud/aegis256/aegis256.h" @@ -60,7 +59,7 @@ gt_bench(int argc, char **argv) for (int i = 0; !gt_quit && size <= 1450; i++) { struct { int64_t min, mean, max, n; - } mbps = { .n = 0 }; + } mbps = {.n = 0}; int64_t bytes_max = (int64_t)1 << 24; @@ -71,7 +70,7 @@ gt_bench(int argc, char **argv) while (!gt_quit && bytes <= bytes_max) { if (chacha) { crypto_aead_chacha20poly1305_encrypt( - buf, NULL, buf, size, NULL, 0, NULL, npub, key); + buf, NULL, buf, size, NULL, 0, NULL, npub, key); } else { aegis256_encrypt(buf, NULL, buf, size, NULL, 0, npub, key); } diff --git a/src/bind.c b/src/bind.c index 013184b..c3b0c74 100644 --- a/src/bind.c +++ b/src/bind.c @@ -2,11 +2,9 @@ #include "ctl.h" #include "iface.h" #include "ip.h" -#include "str.h" #include "tun.h" #include -#include #include #include "../argz/argz.h" @@ -103,8 +101,8 @@ gt_setup_mtu(struct mud *mud, size_t old, const char *tun_name) int gt_bind(int argc, char **argv) { - struct sockaddr_storage bind_addr = { .ss_family = AF_INET }; - struct sockaddr_storage peer_addr = { 0 }; + struct sockaddr_storage bind_addr = {.ss_family = AF_INET}; + struct sockaddr_storage peer_addr = {0}; unsigned short bind_port = 5000; unsigned short peer_port = bind_port; const char *dev = NULL; @@ -128,7 +126,7 @@ gt_bind(int argc, char **argv) if (argz(bindz, argc, argv)) return 1; - if (str_empty(keyfile)) { + if (EMPTY(keyfile)) { gt_log("a keyfile is needed!\n"); return 1; } @@ -175,7 +173,7 @@ gt_bind(int argc, char **argv) if (tun_set_persist(tun_fd, persist) == -1) { gt_log("couldn't %sable persist mode on device %s\n", - persist ? "en" : "dis", tun_name); + persist ? "en" : "dis", tun_name); } if (peer_addr.ss_family) { @@ -191,7 +189,7 @@ gt_bind(int argc, char **argv) char dir[64]; if (ctl_rundir(dir, sizeof(dir))) { gt_log("couldn't create %s/%s: %s\n", - dir, tun_name, strerror(errno)); + dir, tun_name, strerror(errno)); } else { gt_log("couldn't find a writable run/tmp directory\n"); } @@ -232,7 +230,7 @@ gt_bind(int argc, char **argv) FD_SET(ctl_fd, &rfds); - struct timeval tv = { 0 }; + struct timeval tv = {0}; int update = mud_update(mud); if (update >= 0) { @@ -274,7 +272,7 @@ gt_bind(int argc, char **argv) tun_can_read = 0; } - if (mud_can_read && tun_can_write) { + if (mud_can_read && tun_can_write) { int r = mud_recv(mud, buf, sizeof(buf)); if (r > 0 && ip_is_valid(buf, r)) { @@ -321,29 +319,27 @@ gt_bind(int argc, char **argv) res.status.bind = bind_addr; res.status.peer = peer_addr; break; - case CTL_PATH_STATUS: - { - unsigned count = 0; - struct mud_path *paths = mud_get_paths(mud, &count); + case CTL_PATH_STATUS: { + unsigned count = 0; + struct mud_path *paths = mud_get_paths(mud, &count); - if (!paths) { - res.ret = errno; - break; - } - - res.ret = EAGAIN; - - for (unsigned i = 0; i < count; i++) { - memcpy(&res.path_status, &paths[i], sizeof(struct mud_path)); - if (sendto(ctl_fd, &res, sizeof(res), 0, - (const struct sockaddr *)&ss, sl) == -1) - perror("sendto(ctl)"); - } - - free(paths); - res.ret = 0; + if (!paths) { + res.ret = errno; + break; } - break; + + res.ret = EAGAIN; + + for (unsigned i = 0; i < count; i++) { + memcpy(&res.path_status, &paths[i], sizeof(struct mud_path)); + if (sendto(ctl_fd, &res, sizeof(res), 0, + (const struct sockaddr *)&ss, sl) == -1) + perror("sendto(ctl)"); + } + + free(paths); + res.ret = 0; + } break; case CTL_BAD: if (mud_get_bad(mud, &res.bad)) res.ret = errno; diff --git a/src/common.h b/src/common.h index a1518f9..0eefc95 100644 --- a/src/common.h +++ b/src/common.h @@ -4,16 +4,17 @@ #define _GNU_SOURCE #endif -#include +#include +#include #include +#include +#include #include #include -#include -#include #include -#include #include +#include #ifndef PACKAGE_NAME #define PACKAGE_NAME "glorytun" @@ -23,55 +24,49 @@ #define PACKAGE_VERSION "0.0.0" #endif -#define COUNT(x) (sizeof(x)/sizeof(x[0])) +#define COUNT(x) (sizeof(x) / sizeof(x[0])) -#define ALIGN_SIZE (1<<4) -#define ALIGN_MASK (ALIGN_SIZE-1) +#define _1_(x) (__builtin_expect((x), 1)) +#define _0_(x) (__builtin_expect((x), 0)) +#define CLZ(x) (__builtin_clz(x)) -#define ALIGN(x) (((x)+ALIGN_MASK)&~ALIGN_MASK) -#define ALIGN_DOWN(x) ((x)&~ALIGN_MASK) - -#define PALIGN(x) ((void *)ALIGN((size_t)(x))) -#define PALIGN_DOWN(x) ((void *)ALIGN_DOWN((size_t)(x))) - -#define _1_(x) (__builtin_expect((x), 1)) -#define _0_(x) (__builtin_expect((x), 0)) - -#define CLZ(x) (__builtin_clz(x)) - -#define _printf_(A,B) __attribute__ ((format(printf,A,B))) -#define _noreturn_ __attribute__ ((noreturn)) -#define _unused_ __attribute__ ((unused)) -#define _pure_ __attribute__ ((pure)) -#define _const_ __attribute__ ((const)) -#define _align_(...) __attribute__ ((aligned(__VA_ARGS__))) +#define _printf_(A, B) __attribute__((format(printf, A, B))) +#define _noreturn_ __attribute__((noreturn)) +#define _unused_ __attribute__((unused)) +#define _pure_ __attribute__((pure)) +#define _const_ __attribute__((const)) +#define _align_(...) __attribute__((aligned(__VA_ARGS__))) #undef MAX -#define MAX(x,y) ({ __typeof__(x) X=(x); __typeof__(y) Y=(y); X > Y ? X : Y; }) +#define MAX(x, y) ({ __typeof__(x) X=(x); __typeof__(y) Y=(y); X > Y ? X : Y; }) #undef MIN -#define MIN(x,y) ({ __typeof__(x) X=(x); __typeof__(y) Y=(y); X < Y ? X : Y; }) +#define MIN(x, y) ({ __typeof__(x) X=(x); __typeof__(y) Y=(y); X < Y ? X : Y; }) -#define GT_CIPHER(x) ((x) ? "chacha20poly1305" : "aegis256") +#define EMPTY(x) ({ __typeof__(x) X=(x); !X || !X[0]; }) + +#define GT_CIPHER(x) ((x) ? "chacha20poly1305" : "aegis256") extern volatile sig_atomic_t gt_alarm; extern volatile sig_atomic_t gt_reload; extern volatile sig_atomic_t gt_quit; -int gt_print (const char *, ...) _printf_(1,2); -void gt_log (const char *, ...) _printf_(1,2); +int gt_print (const char *, ...) _printf_(1, 2); +void gt_log (const char *, ...) _printf_(1, 2); int gt_tohex (char *, size_t, const uint8_t *, size_t); int gt_fromhex (uint8_t *, size_t, const char *, size_t); void gt_set_port (struct sockaddr *, uint16_t); uint16_t gt_get_port (struct sockaddr *); -int gt_toaddr (char *, size_t, struct sockaddr *); -int gt_list (int, char **); -int gt_show (int, char **); -int gt_bind (int, char **); -int gt_path (int, char **); -int gt_keygen (int, char **); -int gt_bench (int, char **); -int gt_set (int, char **); +int gt_toaddr (char *, size_t, struct sockaddr *); + +int gt_list (int, char **); +int gt_show (int, char **); +int gt_bind (int, char **); +int gt_path (int, char **); +int gt_keygen (int, char **); +int gt_bench (int, char **); +int gt_set (int, char **); +int gt_version (int, char **); diff --git a/src/ctl.c b/src/ctl.c index ee01ccc..f11c92b 100644 --- a/src/ctl.c +++ b/src/ctl.c @@ -1,13 +1,9 @@ -#include "common.h" #include "ctl.h" -#include "str.h" +#include "common.h" -#include -#include #include #include -#include -#include +#include #include #include @@ -87,10 +83,10 @@ ctl_setsun(struct sockaddr_un *dst, const char *dir, const char *file) static int ctl_bind(int fd, const char *dir, const char *file) { - char name[10] = { [0] = '.' }; + char name[10] = {[0] = '.'}; struct sockaddr_un sun; - if (str_empty(file)) { + if (EMPTY(file)) { unsigned pid = (unsigned)getpid(); for (size_t i = 1; i < sizeof(name) - 1; i++, pid >>= 4) @@ -111,7 +107,7 @@ ctl_bind(int fd, const char *dir, const char *file) void ctl_delete(int fd) { - struct sockaddr_storage ss = { 0 }; + struct sockaddr_storage ss = {0}; socklen_t sslen = sizeof(ss); if ((getsockname(fd, (struct sockaddr *)&ss, &sslen) == 0) && diff --git a/src/iface.c b/src/iface.c index a0644d9..8862877 100644 --- a/src/iface.c +++ b/src/iface.c @@ -1,8 +1,8 @@ -#include "common.h" #include "iface.h" +#include "common.h" -#include #include +#include #include int diff --git a/src/iface.h b/src/iface.h index 6066d24..0d64c09 100644 --- a/src/iface.h +++ b/src/iface.h @@ -1,3 +1,5 @@ #pragma once +#include + int iface_set_mtu (const char *, size_t); diff --git a/src/list.c b/src/list.c index 688a7b6..6fcae48 100644 --- a/src/list.c +++ b/src/list.c @@ -3,10 +3,8 @@ #include "../argz/argz.h" -#include #include -#include -#include +#include int gt_list(int argc, char **argv) diff --git a/src/main.c b/src/main.c index a3b44a8..ae56505 100644 --- a/src/main.c +++ b/src/main.c @@ -1,7 +1,5 @@ #include "common.h" -#include "str.h" -#include #include #include "../argz/argz.h" @@ -46,28 +44,6 @@ gt_set_signal(void) sigaction(SIGUSR2, &sa, NULL); } -static int -gt_version(int argc, char **argv) -{ - struct argz version_argz[] = { - {"libsodium", NULL, NULL, argz_option}, - {NULL}}; - - if (argz(version_argz, argc, argv)) - return 1; - - if (argz_is_set(version_argz, "libsodium")) { - printf("%i.%i (%s)\n", - sodium_library_version_major(), - sodium_library_version_minor(), - sodium_version_string()); - } else { - printf("%s\n", PACKAGE_VERSION); - } - - return 0; -} - int main(int argc, char **argv) { @@ -90,7 +66,7 @@ main(int argc, char **argv) if (argv[1]) { for (int k = 0; cmd[k].name; k++) { - if (!str_cmp(cmd[k].name, argv[1])) + if (!strcmp(cmd[k].name, argv[1])) return cmd[k].call(argc - 1, argv + 1); } } @@ -100,10 +76,10 @@ main(int argc, char **argv) int len = 0; for (int k = 0; cmd[k].name; k++) - len = MAX(len, (int)str_len(cmd[k].name, 32)); + len = MAX(len, (int)strlen(cmd[k].name)); for (int k = 0; cmd[k].name; k++) - printf(" %-*s %s\n", len, cmd[k].name, cmd[k].help); + printf(" %-*s %s\n", len, cmd[k].name, cmd[k].help); printf("\n"); diff --git a/src/path.c b/src/path.c index 6e79f03..8cb098f 100644 --- a/src/path.c +++ b/src/path.c @@ -1,10 +1,7 @@ #include "common.h" #include "ctl.h" -#include "str.h" #include -#include -#include #include "../argz/argz.h" diff --git a/src/set.c b/src/set.c index e99a8e8..b886245 100644 --- a/src/set.c +++ b/src/set.c @@ -1,9 +1,7 @@ #include "common.h" #include "ctl.h" -#include "str.h" #include -#include #include "../argz/argz.h" diff --git a/src/show.c b/src/show.c index 7e1a21b..3c76984 100644 --- a/src/show.c +++ b/src/show.c @@ -1,15 +1,9 @@ #include "common.h" #include "ctl.h" -#include "str.h" #include "../argz/argz.h" #include -#include -#include -#include -#include -#include static void gt_show_bad_line(int term, char *name, uint64_t count, diff --git a/src/str.h b/src/str.h deleted file mode 100644 index b012277..0000000 --- a/src/str.h +++ /dev/null @@ -1,33 +0,0 @@ -#pragma once - -#include "common.h" - -_pure_ static inline int -str_empty(const char *restrict str) -{ - return !str || !str[0]; -} - -_pure_ static inline size_t -str_cmp(const char *restrict sa, const char *restrict sb) -{ - if (!sa || !sb) - return 1; - - size_t i = 0; - - while (sa[i] == sb[i]) - if (!sa[i++]) - return 0; - - return i + 1; -} - -_pure_ static inline size_t -str_len(const char *restrict str, size_t len) -{ - if (!str) - return 0; - - return strnlen(str, len); -} diff --git a/src/tun.c b/src/tun.c index 6b9a7de..0b25709 100644 --- a/src/tun.c +++ b/src/tun.c @@ -1,14 +1,13 @@ +#include "tun.h" #include "common.h" #include "ip.h" -#include "str.h" -#include "tun.h" #include +#include #include #include #include #include -#include #ifdef __linux__ #define IFF_TUN 0x0001 @@ -172,7 +171,7 @@ tun_create(char *name, size_t len, const char *dev_name) { int fd = -1; - if (str_empty(dev_name)) { + if (EMPTY(dev_name)) { for (unsigned id = 0; id < 32 && fd == -1; id++) fd = tun_create_by_id(name, len, id); } else { diff --git a/src/tun.h b/src/tun.h index 2b4aa70..d020098 100644 --- a/src/tun.h +++ b/src/tun.h @@ -1,5 +1,7 @@ #pragma once +#include + int tun_create (char *, size_t, const char *); int tun_read (int, void *, size_t); int tun_write (int, const void *, size_t); diff --git a/src/version.c b/src/version.c new file mode 100644 index 0000000..523942c --- /dev/null +++ b/src/version.c @@ -0,0 +1,27 @@ +#include "common.h" + +#include + +#include "../argz/argz.h" + +int +gt_version(int argc, char **argv) +{ + struct argz version_argz[] = { + {"libsodium", NULL, NULL, argz_option}, + {NULL}}; + + if (argz(version_argz, argc, argv)) + return 1; + + if (argz_is_set(version_argz, "libsodium")) { + printf("%i.%i (%s)\n", + sodium_library_version_major(), + sodium_library_version_minor(), + sodium_version_string()); + } else { + printf("%s\n", PACKAGE_VERSION); + } + + return 0; +}