Compare commits
10 Commits
v0.0.41-mu
...
v0.0.49-mu
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b232a101d2 | ||
|
|
a01dc81500 | ||
|
|
1db628d84a | ||
|
|
f11cd34dc4 | ||
|
|
d0376e3aa5 | ||
|
|
a7518c0e5a | ||
|
|
378316bd68 | ||
|
|
286d6abf2d | ||
|
|
1f1464e90d | ||
|
|
55d9dd9277 |
2
mud
2
mud
Submodule mud updated: 7e5c487951...5c77233093
81
src/main.c
81
src/main.c
@@ -189,9 +189,12 @@ static int gt_setup_secretkey (struct mud *mud, char *keyfile)
|
||||
|
||||
if (str_empty(keyfile)) {
|
||||
char buf[2*sizeof(key)+1];
|
||||
size_t size = sizeof(key);
|
||||
|
||||
mud_get_key(mud, key, sizeof(key));
|
||||
gt_tohex(buf, sizeof(buf), key, sizeof(key));
|
||||
if (mud_get_key(mud, key, &size))
|
||||
return -1;
|
||||
|
||||
gt_tohex(buf, sizeof(buf), key, size);
|
||||
state_send(gt.state_fd, "SECRETKEY", buf);
|
||||
|
||||
return 0;
|
||||
@@ -233,13 +236,17 @@ int main (int argc, char **argv)
|
||||
gt_set_signal();
|
||||
|
||||
char *host = NULL;
|
||||
char *port = "5000";
|
||||
long port = 5000;
|
||||
|
||||
char *bind_list = NULL;
|
||||
char *bind_port = "5000";
|
||||
long bind_port = 5000;
|
||||
|
||||
char *dev = NULL;
|
||||
char *keyfile = NULL;
|
||||
char *statefile = NULL;
|
||||
|
||||
long mtu = 1450;
|
||||
|
||||
gt.timeout = 5000;
|
||||
|
||||
long time_tolerance = 0;
|
||||
@@ -249,10 +256,11 @@ int main (int argc, char **argv)
|
||||
|
||||
struct option opts[] = {
|
||||
{ "host", &host, option_str },
|
||||
{ "port", &port, option_str },
|
||||
{ "port", &port, option_long },
|
||||
{ "bind", &bind_list, option_str },
|
||||
{ "bind-port", &bind_port, option_str },
|
||||
{ "bind-port", &bind_port, option_long },
|
||||
{ "dev", &dev, option_str },
|
||||
{ "mtu", &mtu, option_long },
|
||||
{ "keyfile", &keyfile, option_str },
|
||||
{ "multiqueue", NULL, option_option },
|
||||
{ "statefile", &statefile, option_str },
|
||||
@@ -324,7 +332,7 @@ int main (int argc, char **argv)
|
||||
if (time_tolerance > 0)
|
||||
mud_set_time_tolerance_sec(mud, time_tolerance);
|
||||
|
||||
if (bind_list) {
|
||||
if (host && port && bind_list) {
|
||||
char tmp[1024];
|
||||
char *name = &tmp[0];
|
||||
|
||||
@@ -336,19 +344,16 @@ int main (int argc, char **argv)
|
||||
|
||||
tmp[i] = 0;
|
||||
|
||||
if (mud_bind(mud, name))
|
||||
if (mud_peer(mud, name, host, port))
|
||||
return 1;
|
||||
|
||||
name = &tmp[i+1];
|
||||
}
|
||||
|
||||
if (name[0] && mud_bind(mud, name))
|
||||
if (name[0] && mud_peer(mud, name, host, port))
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (host && mud_peer(mud, host, port))
|
||||
return 1;
|
||||
|
||||
int mud_fd = mud_get_fd(mud);
|
||||
|
||||
state_send(gt.state_fd, "INITIALIZED", tun_name);
|
||||
@@ -357,7 +362,16 @@ int main (int argc, char **argv)
|
||||
FD_ZERO(&rfds);
|
||||
|
||||
int started = 0;
|
||||
unsigned char buf[2048];
|
||||
|
||||
struct {
|
||||
unsigned char *buf;
|
||||
} send, recv;
|
||||
|
||||
send.buf = malloc(2*mtu);
|
||||
recv.buf = malloc(mtu);
|
||||
|
||||
size_t send_size = 0;
|
||||
size_t send_limit = 0;
|
||||
|
||||
while (!gt.quit) {
|
||||
FD_SET(tun_fd, &rfds);
|
||||
@@ -372,7 +386,7 @@ int main (int argc, char **argv)
|
||||
.tv_usec = 100000,
|
||||
};
|
||||
|
||||
if (mud_can_push(mud))
|
||||
if (mud_can_push(mud) || send_size)
|
||||
timeout.tv_usec = 1000;
|
||||
|
||||
if _0_(select(mud_fd+1, &rfds, NULL, NULL, &timeout)==-1) {
|
||||
@@ -395,8 +409,8 @@ int main (int argc, char **argv)
|
||||
}
|
||||
|
||||
if (FD_ISSET(tun_fd, &rfds)) {
|
||||
while (1) {
|
||||
const ssize_t r = tun_read(tun_fd, buf, sizeof(buf));
|
||||
while (send_size<mtu) {
|
||||
const ssize_t r = tun_read(tun_fd, send.buf+send_size, mtu);
|
||||
|
||||
if (r<=0) {
|
||||
gt.quit |= !r;
|
||||
@@ -405,9 +419,23 @@ int main (int argc, char **argv)
|
||||
|
||||
struct ip_common ic;
|
||||
|
||||
if (!ip_get_common(&ic, buf, sizeof(buf)) && ic.size==r)
|
||||
mud_send(mud, buf, r);
|
||||
if (ip_get_common(&ic, send.buf+send_size, mtu) || ic.size!=r) {
|
||||
gt_log("packet dropped: malformed\n");
|
||||
continue;
|
||||
}
|
||||
|
||||
send_size += r;
|
||||
|
||||
if (send_size<=mtu)
|
||||
send_limit = send_size;
|
||||
}
|
||||
}
|
||||
|
||||
if (send_limit && mud_send(mud, send.buf, send_limit)==send_limit) {
|
||||
if (send_size>send_limit)
|
||||
memmove(&send.buf[0], &send.buf[send_limit], send_size-send_limit);
|
||||
send_size -= send_limit;
|
||||
send_limit = send_size;
|
||||
}
|
||||
|
||||
mud_push(mud);
|
||||
@@ -415,18 +443,29 @@ int main (int argc, char **argv)
|
||||
if (FD_ISSET(mud_fd, &rfds))
|
||||
mud_pull(mud);
|
||||
|
||||
while (1) {
|
||||
const int size = mud_recv(mud, buf, sizeof(buf));
|
||||
while (!gt.quit) {
|
||||
const int size = mud_recv(mud, recv.buf, mtu);
|
||||
|
||||
if (size<=0)
|
||||
break;
|
||||
|
||||
const ssize_t r = tun_write(tun_fd, buf, size);
|
||||
int p = 0;
|
||||
|
||||
while (p<size) {
|
||||
struct ip_common ic;
|
||||
|
||||
if (ip_get_common(&ic, recv.buf+p, size-p) || ic.size>size-p)
|
||||
break;
|
||||
|
||||
const ssize_t r = tun_write(tun_fd, recv.buf+p, ic.size);
|
||||
|
||||
if (r<=0) {
|
||||
gt.quit |= !r;
|
||||
break;
|
||||
}
|
||||
|
||||
p += ic.size;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user