Compare commits

...

4 Commits

Author SHA1 Message Date
angt
7db50de8df Version 0.0.9 2015-11-30 16:08:36 +01:00
angt
22a6b511f7 Little opt 2015-11-30 16:07:13 +01:00
angt
2f2e5e6f99 Use SIGUSR1 to show tcp_info 2015-11-29 18:10:15 +01:00
angt
3472771a6f Try to close nicely on tun error 2015-11-28 14:23:45 +01:00
3 changed files with 74 additions and 70 deletions

View File

@@ -1,5 +1,5 @@
AC_PREREQ([2.65])
AC_INIT([glorytun], [0.0.8], [https://github.com/angt/glorytun/issues],
AC_INIT([glorytun], [0.0.9], [https://github.com/angt/glorytun/issues],
[glorytun], [https://github.com/angt/glorytun])
AC_CONFIG_SRCDIR([src/common.h])
AC_CONFIG_AUX_DIR([build-aux])

View File

@@ -48,6 +48,7 @@ struct crypto_ctx {
};
volatile sig_atomic_t gt_close = 0;
volatile sig_atomic_t gt_info = 0;
static int64_t dt_ms (struct timeval *ta, struct timeval *tb)
{
@@ -319,20 +320,29 @@ static void gt_sa_stop (int sig)
{
switch (sig) {
case SIGINT:
case SIGQUIT:
case SIGTERM:
gt_close = 1;
break;
case SIGUSR1:
gt_info = 1;
break;
}
}
static void gt_set_signal (void)
{
struct sigaction sa;
struct sigaction sa = {
.sa_flags = 0,
};
byte_set(&sa, 0, sizeof(sa));
sigemptyset(&sa.sa_mask);
sa.sa_handler = gt_sa_stop;
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);
@@ -342,7 +352,7 @@ static void gt_set_signal (void)
static ssize_t fd_read (int fd, void *data, size_t size)
{
if (!size)
return -2;
return -1;
ssize_t ret = read(fd, data, size);
@@ -362,7 +372,7 @@ static ssize_t fd_read (int fd, void *data, size_t size)
static ssize_t fd_write (int fd, const void *data, size_t size)
{
if (!size)
return -2;
return -1;
ssize_t ret = write(fd, data, size);
@@ -644,13 +654,6 @@ int main (int argc, char **argv)
long ka_idle = -1;
long ka_interval = -1;
#ifdef TCP_INFO
struct {
struct timeval time;
struct tcp_info info;
} tcpinfo = {0};
#endif
struct option ka_opts[] = {
{ "count", &ka_count, option_long },
{ "idle", &ka_idle, option_long },
@@ -671,7 +674,6 @@ int main (int argc, char **argv)
{ "buffer-size", &buffer_size, option_long },
{ "noquickack", NULL, option_option },
{ "daemon", NULL, option_option },
{ "debug", NULL, option_option },
{ "version", NULL, option_option },
{ NULL },
};
@@ -686,7 +688,6 @@ int main (int argc, char **argv)
int listener = option_is_set(opts, "listener");
int delay = option_is_set(opts, "delay");
int debug = option_is_set(opts, "debug");
int keepalive = option_is_set(opts, "keepalive");
int noquickack = option_is_set(opts, "noquickack");
@@ -827,12 +828,6 @@ int main (int argc, char **argv)
FD_SET(sock.fd, &rfds);
if (buffer_read_size(&tun.read) || blk_count)
FD_SET(sock.fd, &wfds);
if (buffer_read_size(&sock.read))
FD_SET(tun.fd, &wfds);
if (select(sock.fd+1, &rfds, &wfds, NULL, NULL)==-1) {
if (errno==EINTR)
continue;
@@ -843,14 +838,18 @@ int main (int argc, char **argv)
FD_CLR(sock.fd, &wfds);
FD_CLR(tun.fd, &wfds);
#ifdef TCP_INFO
struct timeval now;
gettimeofday(&now, NULL);
// TODO
// struct timeval now;
// gettimeofday(&now, NULL);
if (debug && dt_ms(&now, &tcpinfo.time)>1000LL) {
tcpinfo.time = now;
if (sk_get_info(sock.fd, &tcpinfo.info))
print_tcp_info(sockname, &tcpinfo.info);
#ifdef TCP_INFO
if (gt_info) {
struct tcp_info ti;
if (sk_get_info(sock.fd, &ti))
print_tcp_info(sockname, &ti);
gt_info = 0;
}
#endif
@@ -859,11 +858,10 @@ int main (int argc, char **argv)
uint8_t *data = blks[blk_write].data;
ssize_t r = tun_read(tun.fd, data, GT_MTU_MAX);
if (!r)
return 2;
if (r<0)
if (r<=0) {
gt_close |= !r;
break;
}
ssize_t ip_size = ip_get_size(data, GT_MTU_MAX);
@@ -885,13 +883,13 @@ int main (int argc, char **argv)
}
}
while (1) {
buffer_shift(&tun.read);
while (blk_count) {
if (!blks[blk_read].size) {
blk_read++;
continue;
}
if (!stop_loop) {
for (; blk_count; blk_read++) {
if (!blks[blk_read].size)
break;
if (buffer_write_size(&tun.read)<blks[blk_read].size)
break;
@@ -899,29 +897,36 @@ int main (int argc, char **argv)
byte_cpy(tun.read.write, blks[blk_read].data, blks[blk_read].size);
tun.read.write += blks[blk_read].size;
blks[blk_read++].size = 0;
blks[blk_read].size = 0;
blk_count--;
}
gt_encrypt(&ctx, &sock.write, &tun.read);
}
if (!buffer_read_size(&sock.write))
break;
if (buffer_read_size(&sock.write)) {
ssize_t r = fd_write(sock.fd, sock.write.read,
buffer_read_size(&sock.write));
if (r==-1)
FD_SET(sock.fd, &wfds);
if (!r)
stop_loop |= (1<<2);
if (r>0)
if (r>0) {
sock.write.read += r;
} else {
if (stop_loop && !(stop_loop>>2)) {
if (!r) {
stop_loop |= (1<<2);
} else {
FD_SET(sock.fd, &wfds);
}
break;
}
}
if (stop_loop && !buffer_read_size(&sock.write)) {
if (!(stop_loop&(1<<2))) {
stop_loop |= (1<<2);
gt_log("%s: shutdown\n", sockname);
shutdown(sock.fd, SHUT_WR);
gt_log("%s: shutdown\n", sockname);
}
}
@@ -935,11 +940,11 @@ int main (int argc, char **argv)
ssize_t r = fd_read(sock.fd, sock.read.write,
buffer_write_size(&sock.read));
if (!r)
stop_loop |= (1<<1);
if (r>0)
if (r>0) {
sock.read.write += r;
} else if (!r) {
stop_loop |= (1<<1);
}
}
while (1) {
@@ -961,14 +966,13 @@ int main (int argc, char **argv)
ssize_t r = tun_write(tun.fd, tun.write.read, ip_size);
if (!r)
return 2;
if (r==-1)
FD_SET(tun.fd, &wfds);
if (r>0)
if (r>0) {
tun.write.read += r;
} else {
gt_close |= !r;
FD_SET(tun.fd, &wfds);
break;
}
}
buffer_shift(&tun.write);

View File

@@ -124,7 +124,7 @@ int tun_create (_unused_ char *name, _unused_ int mq)
ssize_t tun_read (int fd, void *data, size_t size)
{
if (!size)
return -2;
return -1;
#ifdef GT_BSD_TUN
uint32_t family;
@@ -162,7 +162,7 @@ ssize_t tun_read (int fd, void *data, size_t size)
ssize_t tun_write (int fd, const void *data, size_t size)
{
if (!size)
return -2;
return -1;
#ifdef GT_BSD_TUN
uint32_t family;