Reformat
This commit is contained in:
178
src/main.c
178
src/main.c
@@ -1,19 +1,19 @@
|
||||
#include "common.h"
|
||||
|
||||
#include "buffer.h"
|
||||
#include "ip.h"
|
||||
#include "str.h"
|
||||
#include "option.h"
|
||||
#include "tun.h"
|
||||
#include "db.h"
|
||||
#include "ip.h"
|
||||
#include "option.h"
|
||||
#include "state.h"
|
||||
#include "str.h"
|
||||
#include "tun.h"
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <inttypes.h>
|
||||
#include <limits.h>
|
||||
#include <stdio.h>
|
||||
#include <signal.h>
|
||||
#include <poll.h>
|
||||
#include <fcntl.h>
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
@@ -33,25 +33,27 @@ static struct {
|
||||
int state_fd;
|
||||
} gt;
|
||||
|
||||
static void fd_set_nonblock (int fd)
|
||||
static void
|
||||
fd_set_nonblock(int fd)
|
||||
{
|
||||
int ret;
|
||||
|
||||
do {
|
||||
ret = fcntl(fd, F_GETFL, 0);
|
||||
} while (ret==-1 && errno==EINTR);
|
||||
} while (ret == -1 && errno == EINTR);
|
||||
|
||||
int flags = (ret==-1)?0:ret;
|
||||
int flags = (ret == -1) ? 0 : ret;
|
||||
|
||||
do {
|
||||
ret = fcntl(fd, F_SETFL, flags|O_NONBLOCK);
|
||||
} while (ret==-1 && errno==EINTR);
|
||||
ret = fcntl(fd, F_SETFL, flags | O_NONBLOCK);
|
||||
} while (ret == -1 && errno == EINTR);
|
||||
|
||||
if (ret==-1)
|
||||
if (ret == -1)
|
||||
perror("fcntl O_NONBLOCK");
|
||||
}
|
||||
|
||||
static void gt_sa_handler (int sig)
|
||||
static void
|
||||
gt_sa_handler(int sig)
|
||||
{
|
||||
switch (sig) {
|
||||
case SIGINT:
|
||||
@@ -65,7 +67,8 @@ static void gt_sa_handler (int sig)
|
||||
}
|
||||
}
|
||||
|
||||
static void gt_set_signal (void)
|
||||
static void
|
||||
gt_set_signal(void)
|
||||
{
|
||||
struct sigaction sa = {
|
||||
.sa_flags = 0,
|
||||
@@ -74,25 +77,26 @@ static void gt_set_signal (void)
|
||||
sigemptyset(&sa.sa_mask);
|
||||
|
||||
sa.sa_handler = gt_sa_handler;
|
||||
sigaction(SIGINT, &sa, NULL);
|
||||
sigaction(SIGINT, &sa, NULL);
|
||||
sigaction(SIGQUIT, &sa, NULL);
|
||||
sigaction(SIGTERM, &sa, NULL);
|
||||
sigaction(SIGUSR1, &sa, NULL);
|
||||
|
||||
sa.sa_handler = SIG_IGN;
|
||||
sigaction(SIGHUP, &sa, NULL);
|
||||
sigaction(SIGHUP, &sa, NULL);
|
||||
sigaction(SIGPIPE, &sa, NULL);
|
||||
}
|
||||
|
||||
static ssize_t fd_read (int fd, void *data, size_t size)
|
||||
static ssize_t
|
||||
fd_read(int fd, void *data, size_t size)
|
||||
{
|
||||
if ((fd==-1) || !size)
|
||||
if ((fd == -1) || !size)
|
||||
return -1;
|
||||
|
||||
ssize_t ret = read(fd, data, size);
|
||||
|
||||
if (ret==-1) {
|
||||
if (errno==EAGAIN || errno==EINTR)
|
||||
if (ret == -1) {
|
||||
if (errno == EAGAIN || errno == EINTR)
|
||||
return -1;
|
||||
|
||||
if (errno)
|
||||
@@ -104,18 +108,19 @@ static ssize_t fd_read (int fd, void *data, size_t size)
|
||||
return ret;
|
||||
}
|
||||
|
||||
static ssize_t fd_write (int fd, const void *data, size_t size)
|
||||
static ssize_t
|
||||
fd_write(int fd, const void *data, size_t size)
|
||||
{
|
||||
if ((fd==-1) || !size)
|
||||
if ((fd == -1) || !size)
|
||||
return -1;
|
||||
|
||||
ssize_t ret = write(fd, data, size);
|
||||
|
||||
if (ret==-1) {
|
||||
if (errno==EAGAIN || errno==EINTR)
|
||||
if (ret == -1) {
|
||||
if (errno == EAGAIN || errno == EINTR)
|
||||
return -1;
|
||||
|
||||
if (errno==EPIPE || errno==ECONNRESET)
|
||||
if (errno == EPIPE || errno == ECONNRESET)
|
||||
return 0;
|
||||
|
||||
if (errno)
|
||||
@@ -127,17 +132,18 @@ static ssize_t fd_write (int fd, const void *data, size_t size)
|
||||
return ret;
|
||||
}
|
||||
|
||||
static size_t fd_read_all (int fd, void *data, size_t size)
|
||||
static size_t
|
||||
fd_read_all(int fd, void *data, size_t size)
|
||||
{
|
||||
size_t done = 0;
|
||||
|
||||
while (done<size) {
|
||||
ssize_t ret = fd_read(fd, (uint8_t *)data+done, size-done);
|
||||
while (done < size) {
|
||||
ssize_t ret = fd_read(fd, (uint8_t *)data + done, size - done);
|
||||
|
||||
if (!ret)
|
||||
break;
|
||||
|
||||
if (ret<0) {
|
||||
if (ret < 0) {
|
||||
struct pollfd pollfd = {
|
||||
.fd = fd,
|
||||
.events = POLLIN,
|
||||
@@ -155,17 +161,18 @@ static size_t fd_read_all (int fd, void *data, size_t size)
|
||||
return done;
|
||||
}
|
||||
|
||||
static size_t fd_write_all (int fd, const void *data, size_t size)
|
||||
static size_t
|
||||
fd_write_all(int fd, const void *data, size_t size)
|
||||
{
|
||||
size_t done = 0;
|
||||
|
||||
while (done<size) {
|
||||
ssize_t ret = fd_write(fd, (const uint8_t *)data+done, size-done);
|
||||
while (done < size) {
|
||||
ssize_t ret = fd_write(fd, (const uint8_t *)data + done, size - done);
|
||||
|
||||
if (!ret)
|
||||
break;
|
||||
|
||||
if (ret<0) {
|
||||
if (ret < 0) {
|
||||
struct pollfd pollfd = {
|
||||
.fd = fd,
|
||||
.events = POLLOUT,
|
||||
@@ -183,12 +190,13 @@ static size_t fd_write_all (int fd, const void *data, size_t size)
|
||||
return done;
|
||||
}
|
||||
|
||||
static int gt_setup_secretkey (struct mud *mud, char *keyfile)
|
||||
static int
|
||||
gt_setup_secretkey(struct mud *mud, char *keyfile)
|
||||
{
|
||||
unsigned char key[32];
|
||||
|
||||
if (str_empty(keyfile)) {
|
||||
char buf[2*sizeof(key)+1];
|
||||
char buf[2 * sizeof(key) + 1];
|
||||
size_t size = sizeof(key);
|
||||
|
||||
if (mud_get_key(mud, key, &size))
|
||||
@@ -203,20 +211,20 @@ static int gt_setup_secretkey (struct mud *mud, char *keyfile)
|
||||
int fd;
|
||||
|
||||
do {
|
||||
fd = open(keyfile, O_RDONLY|O_CLOEXEC);
|
||||
} while (fd==-1 && errno==EINTR);
|
||||
fd = open(keyfile, O_RDONLY | O_CLOEXEC);
|
||||
} while (fd == -1 && errno == EINTR);
|
||||
|
||||
if (fd==-1) {
|
||||
if (fd == -1) {
|
||||
perror("open keyfile");
|
||||
return -1;
|
||||
}
|
||||
|
||||
char buf[2*sizeof(key)];
|
||||
char buf[2 * sizeof(key)];
|
||||
size_t r = fd_read_all(fd, buf, sizeof(buf));
|
||||
|
||||
close(fd);
|
||||
|
||||
if (r!=sizeof(buf)) {
|
||||
if (r != sizeof(buf)) {
|
||||
gt_log("unable to read secret key\n");
|
||||
return -1;
|
||||
}
|
||||
@@ -231,7 +239,8 @@ static int gt_setup_secretkey (struct mud *mud, char *keyfile)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main (int argc, char **argv)
|
||||
int
|
||||
main(int argc, char **argv)
|
||||
{
|
||||
gt_set_signal();
|
||||
|
||||
@@ -260,6 +269,7 @@ int main (int argc, char **argv)
|
||||
#endif
|
||||
|
||||
struct option opts[] = {
|
||||
// clang-format off
|
||||
{ "host", &host, option_str },
|
||||
{ "port", &port, option_long },
|
||||
{ "bind", &bind_list, option_str },
|
||||
@@ -275,14 +285,15 @@ int main (int argc, char **argv)
|
||||
{ "v6only", NULL, option_option },
|
||||
{ "chacha20", NULL, option_option },
|
||||
{ "version", NULL, option_option },
|
||||
{ NULL },
|
||||
{ NULL },
|
||||
// clang-format on
|
||||
};
|
||||
|
||||
if (option(opts, argc, argv))
|
||||
return 1;
|
||||
|
||||
if (option_is_set(opts, "version")) {
|
||||
gt_print(PACKAGE_STRING"\n");
|
||||
gt_print(PACKAGE_STRING "\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -307,7 +318,7 @@ int main (int argc, char **argv)
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (gt.timeout<=0 || gt.timeout>INT_MAX) {
|
||||
if (gt.timeout <= 0 || gt.timeout > INT_MAX) {
|
||||
gt_log("bad timeout\n");
|
||||
return 1;
|
||||
}
|
||||
@@ -317,25 +328,25 @@ int main (int argc, char **argv)
|
||||
if (v4) {
|
||||
icmp_fd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
|
||||
|
||||
if (icmp_fd==-1)
|
||||
if (icmp_fd == -1)
|
||||
gt_log("couldn't create ICMP socket\n");
|
||||
}
|
||||
|
||||
gt.state_fd = state_create(statefile);
|
||||
|
||||
if (statefile && gt.state_fd==-1)
|
||||
if (statefile && gt.state_fd == -1)
|
||||
return 1;
|
||||
|
||||
char *tun_name = NULL;
|
||||
|
||||
int tun_fd = tun_create(dev, &tun_name);
|
||||
|
||||
if (tun_fd==-1) {
|
||||
if (tun_fd == -1) {
|
||||
gt_log("couldn't create tun device\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (tun_set_mtu(tun_name, mtu)==-1) {
|
||||
if (tun_set_mtu(tun_name, mtu) == -1) {
|
||||
perror("tun_set_mtu");
|
||||
return 1;
|
||||
}
|
||||
@@ -371,12 +382,12 @@ int main (int argc, char **argv)
|
||||
char tmp[1024];
|
||||
char *name = &tmp[0];
|
||||
|
||||
str_cpy(tmp, bind_list, sizeof(tmp)-1);
|
||||
str_cpy(tmp, bind_list, sizeof(tmp) - 1);
|
||||
|
||||
while (*name) {
|
||||
char *p = name;
|
||||
|
||||
while (*p && *p!=',')
|
||||
while (*p && *p != ',')
|
||||
p++;
|
||||
|
||||
if (*p)
|
||||
@@ -401,35 +412,36 @@ int main (int argc, char **argv)
|
||||
fd_set rfds;
|
||||
FD_ZERO(&rfds);
|
||||
|
||||
unsigned char buf[8*1024];
|
||||
unsigned char buf[8 * 1024];
|
||||
|
||||
while (!gt.quit) {
|
||||
FD_SET(tun_fd, &rfds);
|
||||
FD_SET(mud_fd, &rfds);
|
||||
|
||||
if (icmp_fd!=-1)
|
||||
if (icmp_fd != -1)
|
||||
FD_SET(icmp_fd, &rfds);
|
||||
|
||||
if _0_(select(mud_fd+1, &rfds, NULL, NULL, NULL)==-1) {
|
||||
if (errno==EINTR)
|
||||
if (select(mud_fd + 1, &rfds, NULL, NULL, NULL) == -1) {
|
||||
if (errno == EINTR)
|
||||
continue;
|
||||
perror("select");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (icmp_fd!=-1 && FD_ISSET(icmp_fd, &rfds)) {
|
||||
if (icmp_fd != -1 && FD_ISSET(icmp_fd, &rfds)) {
|
||||
struct sockaddr_storage ss;
|
||||
socklen_t sl = sizeof(ss);
|
||||
ssize_t r = recvfrom(icmp_fd, buf, sizeof(buf), 0, (struct sockaddr *)&ss, &sl);
|
||||
if (r>=8) {
|
||||
ssize_t r = recvfrom(icmp_fd, buf, sizeof(buf), 0,
|
||||
(struct sockaddr *)&ss, &sl);
|
||||
if (r >= 8) {
|
||||
struct ip_common ic;
|
||||
if (!ip_get_common(&ic, buf, r) && ic.proto==1) {
|
||||
if (!ip_get_common(&ic, buf, r) && ic.proto == 1) {
|
||||
unsigned char *data = &buf[ic.hdr_size];
|
||||
if (data[0]==3) {
|
||||
int new_mtu = (data[6]<<8)|data[7];
|
||||
if (data[0] == 3) {
|
||||
int new_mtu = (data[6] << 8) | data[7];
|
||||
if (new_mtu) {
|
||||
gt_log("received MTU from ICMP: %i\n", new_mtu);
|
||||
mud_set_mtu(mud, new_mtu-50); // XXX
|
||||
mud_set_mtu(mud, new_mtu - 50); // XXX
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -439,15 +451,15 @@ int main (int argc, char **argv)
|
||||
if (FD_ISSET(tun_fd, &rfds)) {
|
||||
size_t size = 0;
|
||||
|
||||
while (sizeof(buf)-size>mtu) {
|
||||
const ssize_t r = tun_read(tun_fd, &buf[size], sizeof(buf)-size);
|
||||
|
||||
if (r<=0)
|
||||
while (sizeof(buf) - size > mtu) {
|
||||
const ssize_t r = tun_read(tun_fd, &buf[size],
|
||||
sizeof(buf) - size);
|
||||
if (r <= 0)
|
||||
break;
|
||||
|
||||
struct ip_common ic;
|
||||
|
||||
if (ip_get_common(&ic, &buf[size], r) || ic.size!=r)
|
||||
if (ip_get_common(&ic, &buf[size], r) || ic.size != r)
|
||||
break;
|
||||
|
||||
size += r;
|
||||
@@ -455,42 +467,43 @@ int main (int argc, char **argv)
|
||||
|
||||
int p = 0;
|
||||
|
||||
while (p<size) {
|
||||
while (p < size) {
|
||||
int tc = 0;
|
||||
int q = p;
|
||||
|
||||
while (q<size) {
|
||||
while (q < size) {
|
||||
struct ip_common ic;
|
||||
|
||||
if (ip_get_common(&ic, &buf[q], size-q) || ic.size>size-q) {
|
||||
if ((ip_get_common(&ic, &buf[q], size - q)) ||
|
||||
(ic.size > size - q)) {
|
||||
size = q;
|
||||
break;
|
||||
}
|
||||
|
||||
if (q+ic.size>p+mtu)
|
||||
if (q + ic.size > p + mtu)
|
||||
break;
|
||||
|
||||
q += ic.size;
|
||||
|
||||
if (tc<(ic.tc&0xFC))
|
||||
tc = ic.tc&0xFC;
|
||||
if (tc < (ic.tc & 0xFC))
|
||||
tc = ic.tc & 0xFC;
|
||||
}
|
||||
|
||||
int r = mud_send(mud, &buf[p], q-p, tc);
|
||||
int r = mud_send(mud, &buf[p], q - p, tc);
|
||||
|
||||
if (r==-1 && errno==EMSGSIZE) {
|
||||
if (r == -1 && errno == EMSGSIZE) {
|
||||
int new_mtu = mud_get_mtu(mud);
|
||||
|
||||
if (new_mtu!=mtu) {
|
||||
if (new_mtu != mtu) {
|
||||
mtu = new_mtu;
|
||||
|
||||
gt_log("MTU changed: %li\n", mtu);
|
||||
|
||||
if (tun_set_mtu(tun_name, mtu)==-1)
|
||||
if (tun_set_mtu(tun_name, mtu) == -1)
|
||||
perror("tun_set_mtu");
|
||||
}
|
||||
} else {
|
||||
if (r==-1 && errno!=EAGAIN)
|
||||
if (r == -1 && errno != EAGAIN)
|
||||
perror("mud_send");
|
||||
|
||||
p = q;
|
||||
@@ -502,18 +515,19 @@ int main (int argc, char **argv)
|
||||
while (1) {
|
||||
const int size = mud_recv(mud, buf, sizeof(buf));
|
||||
|
||||
if (size<=0) {
|
||||
if (size==-1 && errno!=EAGAIN)
|
||||
if (size <= 0) {
|
||||
if (size == -1 && errno != EAGAIN)
|
||||
perror("mud_recv");
|
||||
break;
|
||||
}
|
||||
|
||||
int p = 0;
|
||||
|
||||
while (p<size) {
|
||||
while (p < size) {
|
||||
struct ip_common ic;
|
||||
|
||||
if (ip_get_common(&ic, &buf[p], size-p) || ic.size>size-p)
|
||||
if ((ip_get_common(&ic, &buf[p], size - p)) ||
|
||||
(ic.size > size - p))
|
||||
break;
|
||||
|
||||
tun_write(tun_fd, &buf[p], ic.size);
|
||||
|
||||
Reference in New Issue
Block a user