Compare commits
7 Commits
v0.0.85-mu
...
v0.0.90-mu
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
355040f576 | ||
|
|
bbf1c12f7a | ||
|
|
c54303da8f | ||
|
|
e3440cf1e9 | ||
|
|
86916f1999 | ||
|
|
9cebabfe01 | ||
|
|
0664fc3b21 |
2
mud
2
mud
Submodule mud updated: 762b4487bf...dfcc08feed
75
src/main.c
75
src/main.c
@@ -28,6 +28,7 @@
|
||||
|
||||
static struct {
|
||||
volatile sig_atomic_t quit;
|
||||
volatile sig_atomic_t reload;
|
||||
char *dev;
|
||||
char *keyfile;
|
||||
char *host;
|
||||
@@ -45,6 +46,10 @@ static struct {
|
||||
int mtu_auto;
|
||||
int chacha20;
|
||||
int version;
|
||||
struct {
|
||||
unsigned char *data;
|
||||
long size;
|
||||
} buf;
|
||||
} gt = {
|
||||
.port = 5000,
|
||||
.bind = {
|
||||
@@ -56,6 +61,9 @@ static struct {
|
||||
#ifdef __linux__
|
||||
.ipv6 = 1,
|
||||
#endif
|
||||
.buf = {
|
||||
.size = 64 * 1024,
|
||||
},
|
||||
};
|
||||
|
||||
static void
|
||||
@@ -80,6 +88,7 @@ fd_set_nonblock(int fd)
|
||||
static void
|
||||
gt_quit_handler(int sig)
|
||||
{
|
||||
gt.reload = (sig == SIGHUP);
|
||||
gt.quit = 1;
|
||||
}
|
||||
|
||||
@@ -96,9 +105,9 @@ gt_set_signal(void)
|
||||
sigaction(SIGINT, &sa, NULL);
|
||||
sigaction(SIGQUIT, &sa, NULL);
|
||||
sigaction(SIGTERM, &sa, NULL);
|
||||
sigaction(SIGHUP, &sa, NULL);
|
||||
|
||||
sa.sa_handler = SIG_IGN;
|
||||
sigaction(SIGHUP, &sa, NULL);
|
||||
sigaction(SIGPIPE, &sa, NULL);
|
||||
sigaction(SIGUSR1, &sa, NULL);
|
||||
sigaction(SIGUSR2, &sa, NULL);
|
||||
@@ -186,6 +195,7 @@ gt_setup_option(int argc, char **argv)
|
||||
{ "v4only", NULL, option_option },
|
||||
{ "v6only", NULL, option_option },
|
||||
{ "chacha20", NULL, option_option },
|
||||
{ "buf-size", >.buf.size, option_long },
|
||||
{ "version", NULL, option_option },
|
||||
{ NULL },
|
||||
};
|
||||
@@ -213,6 +223,11 @@ gt_setup_option(int argc, char **argv)
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (gt.buf.size <= 0) {
|
||||
gt_log("bad buf-size\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (v4only) {
|
||||
gt.ipv4 = 1;
|
||||
gt.ipv6 = 0;
|
||||
@@ -227,6 +242,8 @@ gt_setup_option(int argc, char **argv)
|
||||
gt.chacha20 = option_is_set(opts, "chacha20");
|
||||
gt.version = option_is_set(opts, "version");
|
||||
|
||||
gt.buf.data = malloc(gt.buf.size);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -277,6 +294,9 @@ main(int argc, char **argv)
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (tun_set_persist(tun_fd, 0) == -1)
|
||||
perror("tun_set_persist");
|
||||
|
||||
struct mud *mud = mud_create(gt.bind.port, gt.ipv4, gt.ipv6,
|
||||
!gt.chacha20, GT_MTU(gt.mtu));
|
||||
|
||||
@@ -345,8 +365,6 @@ main(int argc, char **argv)
|
||||
fd_set rfds;
|
||||
FD_ZERO(&rfds);
|
||||
|
||||
unsigned char buf[8 * 1024];
|
||||
|
||||
int last_fd = 1 + MAX(tun_fd, MAX(mud_fd, icmp_fd));
|
||||
|
||||
while (!gt.quit) {
|
||||
@@ -366,12 +384,12 @@ main(int argc, char **argv)
|
||||
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,
|
||||
ssize_t r = recvfrom(icmp_fd, gt.buf.data, gt.buf.size, 0,
|
||||
(struct sockaddr *)&ss, &sl);
|
||||
if (r >= 8) {
|
||||
struct ip_common ic;
|
||||
if (!ip_get_common(&ic, buf, r) && ic.proto == 1) {
|
||||
unsigned char *data = &buf[ic.hdr_size];
|
||||
if (!ip_get_common(&ic, gt.buf.data, r) && ic.proto == 1) {
|
||||
unsigned char *data = >.buf.data[ic.hdr_size];
|
||||
if (data[0] == 3) {
|
||||
int mtu = (data[6] << 8) | data[7];
|
||||
if (mtu) {
|
||||
@@ -386,15 +404,15 @@ main(int argc, char **argv)
|
||||
if (FD_ISSET(tun_fd, &rfds)) {
|
||||
size_t size = 0;
|
||||
|
||||
while (sizeof(buf) - size >= gt.mtu) {
|
||||
const int r = tun_read(tun_fd, &buf[size], sizeof(buf) - size);
|
||||
while (gt.buf.size - size >= gt.mtu) {
|
||||
const int r = tun_read(tun_fd, >.buf.data[size], gt.buf.size - size);
|
||||
|
||||
if (r <= 0 || r > gt.mtu)
|
||||
break;
|
||||
|
||||
struct ip_common ic;
|
||||
|
||||
if (ip_get_common(&ic, &buf[size], r) || ic.size != r)
|
||||
if (ip_get_common(&ic, >.buf.data[size], r) || ic.size != r)
|
||||
break;
|
||||
|
||||
size += r;
|
||||
@@ -409,7 +427,7 @@ main(int argc, char **argv)
|
||||
while (q < size) {
|
||||
struct ip_common ic;
|
||||
|
||||
if ((ip_get_common(&ic, &buf[q], size - q)) ||
|
||||
if ((ip_get_common(&ic, >.buf.data[q], size - q)) ||
|
||||
(ic.size > size - q))
|
||||
break;
|
||||
|
||||
@@ -425,7 +443,7 @@ main(int argc, char **argv)
|
||||
if (p >= q)
|
||||
break;
|
||||
|
||||
int r = mud_send(mud, &buf[p], q - p, tc);
|
||||
int r = mud_send(mud, >.buf.data[p], q - p, tc);
|
||||
|
||||
if (r == -1 && errno == EMSGSIZE) {
|
||||
gt_setup_mtu(mud, tun_name);
|
||||
@@ -439,31 +457,40 @@ main(int argc, char **argv)
|
||||
}
|
||||
|
||||
if (FD_ISSET(mud_fd, &rfds)) {
|
||||
while (1) {
|
||||
const int size = mud_recv(mud, buf, sizeof(buf));
|
||||
size_t size = 0;
|
||||
|
||||
if (size <= 0) {
|
||||
if (size == -1 && errno != EAGAIN)
|
||||
while (gt.buf.size - size >= gt.mtu) {
|
||||
const int r = mud_recv(mud, >.buf.data[size], gt.buf.size - size);
|
||||
|
||||
if (r <= 0) {
|
||||
if (r == -1 && errno != EAGAIN)
|
||||
perror("mud_recv");
|
||||
break;
|
||||
}
|
||||
|
||||
int p = 0;
|
||||
size += r;
|
||||
}
|
||||
|
||||
while (p < size) {
|
||||
struct ip_common ic;
|
||||
int p = 0;
|
||||
|
||||
if ((ip_get_common(&ic, &buf[p], size - p)) ||
|
||||
(ic.size > size - p))
|
||||
break;
|
||||
while (p < size) {
|
||||
struct ip_common ic;
|
||||
|
||||
tun_write(tun_fd, &buf[p], ic.size);
|
||||
if ((ip_get_common(&ic, >.buf.data[p], size - p)) ||
|
||||
(ic.size > size - p))
|
||||
break;
|
||||
|
||||
p += ic.size;
|
||||
}
|
||||
tun_write(tun_fd, >.buf.data[p], ic.size);
|
||||
|
||||
p += ic.size;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (gt.reload && tun_fd >= 0) {
|
||||
if (tun_set_persist(tun_fd, 1) == -1)
|
||||
perror("tun_set_persist");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
#define IFF_TUN 0x0001
|
||||
#define IFF_NO_PI 0x1000
|
||||
#define TUNSETIFF _IOW('T', 202, int)
|
||||
#define TUNSETPERSIST _IOW('T', 203, int)
|
||||
#endif
|
||||
|
||||
#ifdef __APPLE__
|
||||
@@ -260,3 +261,9 @@ tun_set_mtu(char *dev_name, int mtu)
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int
|
||||
tun_set_persist(int fd, int on)
|
||||
{
|
||||
return ioctl(fd, TUNSETPERSIST, on);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
int tun_create (char *, char **);
|
||||
int tun_read (int, void *, size_t);
|
||||
int tun_write (int, const void *, size_t);
|
||||
int tun_set_mtu (char *, int);
|
||||
int tun_create (char *, char **);
|
||||
int tun_read (int, void *, size_t);
|
||||
int tun_write (int, const void *, size_t);
|
||||
int tun_set_mtu (char *, int);
|
||||
int tun_set_persist (int, int);
|
||||
|
||||
Reference in New Issue
Block a user