Code cleanup

Signed-off-by: Adrien Gallouët <adrien@gallouet.fr>
This commit is contained in:
Adrien Gallouët
2020-04-02 20:10:18 +00:00
parent 392a493c17
commit 6091853f46
15 changed files with 109 additions and 163 deletions

View File

@@ -1,11 +1,10 @@
#include "common.h"
#include <sodium.h>
#include <string.h>
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <inttypes.h>
#include <sodium.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#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;

View File

@@ -2,11 +2,9 @@
#include "ctl.h"
#include "iface.h"
#include "ip.h"
#include "str.h"
#include "tun.h"
#include <fcntl.h>
#include <stdio.h>
#include <sys/select.h>
#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;
}
@@ -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) {
@@ -321,8 +319,7 @@ gt_bind(int argc, char **argv)
res.status.bind = bind_addr;
res.status.peer = peer_addr;
break;
case CTL_PATH_STATUS:
{
case CTL_PATH_STATUS: {
unsigned count = 0;
struct mud_path *paths = mud_get_paths(mud, &count);
@@ -342,8 +339,7 @@ gt_bind(int argc, char **argv)
free(paths);
res.ret = 0;
}
break;
} break;
case CTL_BAD:
if (mud_get_bad(mud, &res.bad))
res.ret = errno;

View File

@@ -4,16 +4,17 @@
#define _GNU_SOURCE
#endif
#include <stdlib.h>
#include <errno.h>
#include <signal.h>
#include <stdint.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/socket.h>
#ifndef PACKAGE_NAME
#define PACKAGE_NAME "glorytun"
@@ -23,34 +24,26 @@
#define PACKAGE_VERSION "0.0.0"
#endif
#define COUNT(x) (sizeof(x)/sizeof(x[0]))
#define ALIGN_SIZE (1<<4)
#define ALIGN_MASK (ALIGN_SIZE-1)
#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 COUNT(x) (sizeof(x) / sizeof(x[0]))
#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 EMPTY(x) ({ __typeof__(x) X=(x); !X || !X[0]; })
#define GT_CIPHER(x) ((x) ? "chacha20poly1305" : "aegis256")
@@ -58,14 +51,15 @@ 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 **);
@@ -75,3 +69,4 @@ 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 **);

View File

@@ -1,13 +1,9 @@
#include "common.h"
#include "ctl.h"
#include "str.h"
#include "common.h"
#include <stdio.h>
#include <unistd.h>
#include <dirent.h>
#include <libgen.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/un.h>
@@ -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) &&

View File

@@ -1,8 +1,8 @@
#include "common.h"
#include "iface.h"
#include "common.h"
#include <stdio.h>
#include <net/if.h>
#include <stdio.h>
#include <sys/ioctl.h>
int

View File

@@ -1,3 +1,5 @@
#pragma once
#include <stddef.h>
int iface_set_mtu (const char *, size_t);

View File

@@ -3,10 +3,8 @@
#include "../argz/argz.h"
#include <stdio.h>
#include <dirent.h>
#include <unistd.h>
#include <sys/un.h>
#include <stdio.h>
int
gt_list(int argc, char **argv)

View File

@@ -1,7 +1,5 @@
#include "common.h"
#include "str.h"
#include <sodium.h>
#include <stdio.h>
#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,7 +76,7 @@ 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);

View File

@@ -1,10 +1,7 @@
#include "common.h"
#include "ctl.h"
#include "str.h"
#include <stdio.h>
#include <sys/socket.h>
#include <unistd.h>
#include "../argz/argz.h"

View File

@@ -1,9 +1,7 @@
#include "common.h"
#include "ctl.h"
#include "str.h"
#include <stdio.h>
#include <sys/socket.h>
#include "../argz/argz.h"

View File

@@ -1,15 +1,9 @@
#include "common.h"
#include "ctl.h"
#include "str.h"
#include "../argz/argz.h"
#include <stdio.h>
#include <sys/socket.h>
#include <dirent.h>
#include <sys/un.h>
#include <arpa/inet.h>
#include <unistd.h>
static void
gt_show_bad_line(int term, char *name, uint64_t count,

View File

@@ -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);
}

View File

@@ -1,14 +1,13 @@
#include "tun.h"
#include "common.h"
#include "ip.h"
#include "str.h"
#include "tun.h"
#include <fcntl.h>
#include <net/if.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/uio.h>
#include <net/if.h>
#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 {

View File

@@ -1,5 +1,7 @@
#pragma once
#include <stddef.h>
int tun_create (char *, size_t, const char *);
int tun_read (int, void *, size_t);
int tun_write (int, const void *, size_t);

27
src/version.c Normal file
View File

@@ -0,0 +1,27 @@
#include "common.h"
#include <sodium.h>
#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;
}