Compare commits
4 Commits
v0.0.40-mu
...
v0.0.42-mu
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
286d6abf2d | ||
|
|
1f1464e90d | ||
|
|
55d9dd9277 | ||
|
|
2f290dbf85 |
2
mud
2
mud
Submodule mud updated: f0ad4bbdd3...2c4443a823
71
src/main.c
71
src/main.c
@@ -20,8 +20,6 @@
|
|||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
#include <netdb.h>
|
#include <netdb.h>
|
||||||
|
|
||||||
#include <sodium.h>
|
|
||||||
|
|
||||||
#include "mud.h"
|
#include "mud.h"
|
||||||
|
|
||||||
#ifndef O_CLOEXEC
|
#ifndef O_CLOEXEC
|
||||||
@@ -29,10 +27,10 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
static struct {
|
static struct {
|
||||||
int timeout;
|
|
||||||
volatile sig_atomic_t quit;
|
volatile sig_atomic_t quit;
|
||||||
volatile sig_atomic_t info;
|
volatile sig_atomic_t info;
|
||||||
uint8_t key[crypto_generichash_KEYBYTES];
|
int timeout;
|
||||||
|
int state_fd;
|
||||||
} gt;
|
} gt;
|
||||||
|
|
||||||
static void fd_set_nonblock (int fd)
|
static void fd_set_nonblock (int fd)
|
||||||
@@ -185,16 +183,19 @@ static size_t fd_write_all (int fd, const void *data, size_t size)
|
|||||||
return done;
|
return done;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int gt_setup_secretkey (char *keyfile)
|
static int gt_setup_secretkey (struct mud *mud, char *keyfile)
|
||||||
{
|
{
|
||||||
const size_t size = sizeof(gt.key);
|
unsigned char key[32];
|
||||||
|
|
||||||
if (str_empty(keyfile)) {
|
if (str_empty(keyfile)) {
|
||||||
char buf[2*size+1];
|
char buf[2*sizeof(key)+1];
|
||||||
|
size_t size = sizeof(key);
|
||||||
|
|
||||||
randombytes_buf(gt.key, size);
|
if (mud_get_key(mud, key, &size))
|
||||||
gt_tohex(buf, sizeof(buf), gt.key, size);
|
return -1;
|
||||||
state("SECRETKEY", buf);
|
|
||||||
|
gt_tohex(buf, sizeof(buf), key, size);
|
||||||
|
state_send(gt.state_fd, "SECRETKEY", buf);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -210,21 +211,23 @@ static int gt_setup_secretkey (char *keyfile)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
char key[2*size];
|
char buf[2*sizeof(key)];
|
||||||
size_t r = fd_read_all(fd, key, sizeof(key));
|
size_t r = fd_read_all(fd, buf, sizeof(buf));
|
||||||
|
|
||||||
close(fd);
|
close(fd);
|
||||||
|
|
||||||
if (r!=sizeof(key)) {
|
if (r!=sizeof(buf)) {
|
||||||
gt_log("unable to read secret key\n");
|
gt_log("unable to read secret key\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (gt_fromhex(gt.key, size, key, sizeof(key))) {
|
if (gt_fromhex(key, sizeof(key), buf, sizeof(buf))) {
|
||||||
gt_log("secret key is not valid\n");
|
gt_log("secret key is not valid\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mud_set_key(mud, key, sizeof(key));
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -233,9 +236,11 @@ int main (int argc, char **argv)
|
|||||||
gt_set_signal();
|
gt_set_signal();
|
||||||
|
|
||||||
char *host = NULL;
|
char *host = NULL;
|
||||||
char *port = "5000";
|
long port = 5000;
|
||||||
|
|
||||||
char *bind_list = NULL;
|
char *bind_list = NULL;
|
||||||
char *bind_port = "5000";
|
long bind_port = 5000;
|
||||||
|
|
||||||
char *dev = NULL;
|
char *dev = NULL;
|
||||||
char *keyfile = NULL;
|
char *keyfile = NULL;
|
||||||
char *statefile = NULL;
|
char *statefile = NULL;
|
||||||
@@ -249,9 +254,9 @@ int main (int argc, char **argv)
|
|||||||
|
|
||||||
struct option opts[] = {
|
struct option opts[] = {
|
||||||
{ "host", &host, option_str },
|
{ "host", &host, option_str },
|
||||||
{ "port", &port, option_str },
|
{ "port", &port, option_long },
|
||||||
{ "bind", &bind_list, option_str },
|
{ "bind", &bind_list, option_str },
|
||||||
{ "bind-port", &bind_port, option_str },
|
{ "bind-port", &bind_port, option_long },
|
||||||
{ "dev", &dev, option_str },
|
{ "dev", &dev, option_str },
|
||||||
{ "keyfile", &keyfile, option_str },
|
{ "keyfile", &keyfile, option_str },
|
||||||
{ "multiqueue", NULL, option_option },
|
{ "multiqueue", NULL, option_option },
|
||||||
@@ -283,7 +288,7 @@ int main (int argc, char **argv)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!option_is_set(opts, "keyfile")) {
|
if (host && !option_is_set(opts, "keyfile")) {
|
||||||
gt_log("keyfile option must be set\n");
|
gt_log("keyfile option must be set\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@@ -293,12 +298,9 @@ int main (int argc, char **argv)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (sodium_init()==-1) {
|
gt.state_fd = state_create(statefile);
|
||||||
gt_log("libsodium initialization has failed\n");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (state_init(statefile))
|
if (statefile && gt.state_fd==-1)
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
char *tun_name = NULL;
|
char *tun_name = NULL;
|
||||||
@@ -312,9 +314,6 @@ int main (int argc, char **argv)
|
|||||||
|
|
||||||
fd_set_nonblock(tun_fd);
|
fd_set_nonblock(tun_fd);
|
||||||
|
|
||||||
if (gt_setup_secretkey(keyfile))
|
|
||||||
return 1;
|
|
||||||
|
|
||||||
struct mud *mud = mud_create(bind_port, v4, v6);
|
struct mud *mud = mud_create(bind_port, v4, v6);
|
||||||
|
|
||||||
if (!mud) {
|
if (!mud) {
|
||||||
@@ -322,14 +321,15 @@ int main (int argc, char **argv)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
mud_set_key(mud, gt.key, sizeof(gt.key));
|
if (gt_setup_secretkey(mud, keyfile))
|
||||||
|
return 1;
|
||||||
|
|
||||||
mud_set_send_timeout_msec(mud, gt.timeout);
|
mud_set_send_timeout_msec(mud, gt.timeout);
|
||||||
|
|
||||||
if (time_tolerance > 0)
|
if (time_tolerance > 0)
|
||||||
mud_set_time_tolerance_sec(mud, time_tolerance);
|
mud_set_time_tolerance_sec(mud, time_tolerance);
|
||||||
|
|
||||||
if (bind_list) {
|
if (host && port && bind_list) {
|
||||||
char tmp[1024];
|
char tmp[1024];
|
||||||
char *name = &tmp[0];
|
char *name = &tmp[0];
|
||||||
|
|
||||||
@@ -341,22 +341,19 @@ int main (int argc, char **argv)
|
|||||||
|
|
||||||
tmp[i] = 0;
|
tmp[i] = 0;
|
||||||
|
|
||||||
if (mud_bind(mud, name))
|
if (mud_peer(mud, name, host, port))
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
name = &tmp[i+1];
|
name = &tmp[i+1];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (name[0] && mud_bind(mud, name))
|
if (name[0] && mud_peer(mud, name, host, port))
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (host && mud_peer(mud, host, port))
|
|
||||||
return 1;
|
|
||||||
|
|
||||||
int mud_fd = mud_get_fd(mud);
|
int mud_fd = mud_get_fd(mud);
|
||||||
|
|
||||||
state("INITIALIZED", tun_name);
|
state_send(gt.state_fd, "INITIALIZED", tun_name);
|
||||||
|
|
||||||
fd_set rfds;
|
fd_set rfds;
|
||||||
FD_ZERO(&rfds);
|
FD_ZERO(&rfds);
|
||||||
@@ -389,12 +386,12 @@ int main (int argc, char **argv)
|
|||||||
|
|
||||||
if (mud_is_up(mud)) {
|
if (mud_is_up(mud)) {
|
||||||
if (!started) {
|
if (!started) {
|
||||||
state("STARTED", tun_name);
|
state_send(gt.state_fd, "STARTED", tun_name);
|
||||||
started = 1;
|
started = 1;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (started) {
|
if (started) {
|
||||||
state("STOPPED", tun_name);
|
state_send(gt.state_fd, "STOPPED", tun_name);
|
||||||
started = 0;
|
started = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
41
src/state.c
41
src/state.c
@@ -7,16 +7,14 @@
|
|||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
|
||||||
static int state_fd = -1;
|
int state_create (const char *filename)
|
||||||
|
|
||||||
int state_init (const char *filename)
|
|
||||||
{
|
{
|
||||||
if (str_empty(filename))
|
if (str_empty(filename))
|
||||||
return 0;
|
return -1;
|
||||||
|
|
||||||
state_fd = open(filename, O_WRONLY);
|
int fd = open(filename, O_WRONLY);
|
||||||
|
|
||||||
if (state_fd==-1) {
|
if (fd==-1) {
|
||||||
if (errno!=EINTR)
|
if (errno!=EINTR)
|
||||||
perror("open");
|
perror("open");
|
||||||
return -1;
|
return -1;
|
||||||
@@ -24,38 +22,41 @@ int state_init (const char *filename)
|
|||||||
|
|
||||||
struct stat st = {0};
|
struct stat st = {0};
|
||||||
|
|
||||||
if (fstat(state_fd, &st)==-1) {
|
if (fstat(fd, &st)==-1) {
|
||||||
perror("fstat");
|
perror("fstat");
|
||||||
close(state_fd);
|
close(fd);
|
||||||
state_fd = -1;
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!S_ISFIFO(st.st_mode)) {
|
if (!S_ISFIFO(st.st_mode)) {
|
||||||
gt_log("`%s' is not a fifo\n", filename);
|
gt_log("`%s' is not a fifo\n", filename);
|
||||||
close(state_fd);
|
close(fd);
|
||||||
state_fd = -1;
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return fd;
|
||||||
}
|
}
|
||||||
|
|
||||||
void state (const char *state, const char *info)
|
void state_send (int fd, const char *state, const char *info)
|
||||||
{
|
{
|
||||||
if (str_empty(state))
|
if (str_empty(state))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
if (fd==-1) {
|
||||||
|
gt_print("%s %s\n", state, info);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const char *strs[] = { state, " ", info, "\n" };
|
const char *strs[] = { state, " ", info, "\n" };
|
||||||
char *str = str_cat(strs, COUNT(strs));
|
char *str = str_cat(strs, COUNT(strs));
|
||||||
|
|
||||||
if (!str)
|
if (!str) {
|
||||||
|
perror("str_cat");
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (state_fd==-1) {
|
|
||||||
gt_print("%s", str);
|
|
||||||
} else {
|
|
||||||
if (write(state_fd, str, str_len(str))==-1 && errno!=EINTR)
|
|
||||||
perror("write");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (write(fd, str, str_len(str))==-1 && errno!=EINTR)
|
||||||
|
perror("write");
|
||||||
|
|
||||||
|
free(str);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
int state_init (const char *);
|
int state_create (const char *);
|
||||||
void state (const char *, const char *);
|
void state_send (int, const char *, const char *);
|
||||||
|
|||||||
Reference in New Issue
Block a user