Skip to content

Commit

Permalink
Merge branch 'release/0.5.1'
Browse files Browse the repository at this point in the history
lparam committed Jul 23, 2016
2 parents 3c0b54a + 1840357 commit eaf61e8
Showing 19 changed files with 158 additions and 114 deletions.
2 changes: 1 addition & 1 deletion 3rd/libsodium
Submodule libsodium updated 240 files
2 changes: 1 addition & 1 deletion 3rd/libuv
Submodule libuv updated 88 files
+3 −0 .gitignore
+1 −0 .mailmap
+17 −0 AUTHORS
+6 −3 CONTRIBUTING.md
+229 −1 ChangeLog
+1 −0 MAINTAINERS.md
+18 −6 Makefile.am
+12 −1 README.md
+2 −2 android-configure
+1 −1 appveyor.yml
+1 −1 autogen.sh
+2 −0 checksparse.sh
+0 −7 common.gypi
+4 −1 configure.ac
+11 −6 docs/Makefile
+4 −3 docs/src/loop.rst
+52 −2 docs/src/misc.rst
+18 −5 docs/src/poll.rst
+6 −2 docs/src/stream.rst
+12 −4 docs/src/tty.rst
+3 −2 gyp_uv.py
+39 −0 include/pthread-barrier.h
+0 −72 include/pthread-fixes.h
+4 −17 include/uv-unix.h
+4 −4 include/uv-version.h
+2 −6 include/uv-win.h
+14 −1 include/uv.h
+3 −2 src/fs-poll.c
+70 −75 src/unix/aix.c
+2 −2 src/unix/async.c
+169 −27 src/unix/core.c
+4 −4 src/unix/freebsd.c
+23 −9 src/unix/fs.c
+24 −31 src/unix/internal.h
+48 −11 src/unix/kqueue.c
+101 −25 src/unix/linux-core.c
+1 −1 src/unix/linux-inotify.c
+0 −7 src/unix/linux-syscalls.h
+2 −2 src/unix/openbsd.c
+8 −7 src/unix/pipe.c
+18 −8 src/unix/poll.c
+3 −3 src/unix/process.c
+100 −0 src/unix/pthread-barrier.c
+11 −59 src/unix/pthread-fixes.c
+1 −1 src/unix/signal.c
+55 −37 src/unix/stream.c
+29 −2 src/unix/sunos.c
+2 −2 src/unix/tcp.c
+65 −69 src/unix/thread.c
+34 −2 src/unix/tty.c
+10 −10 src/unix/udp.c
+28 −6 src/uv-common.c
+83 −47 src/win/core.c
+6 −1 src/win/dl.c
+39 −64 src/win/fs-event.c
+29 −14 src/win/fs.c
+44 −17 src/win/getaddrinfo.c
+3 −1 src/win/internal.h
+17 −6 src/win/pipe.c
+13 −2 src/win/poll.c
+2 −7 src/win/timer.c
+133 −50 src/win/tty.c
+231 −99 src/win/util.c
+1 −1 test/runner-unix.c
+5 −5 test/task.h
+67 −33 test/test-condvar.c
+94 −0 test/test-eintr-handling.c
+7 −0 test/test-emfile.c
+34 −5 test/test-fs-event.c
+1 −0 test/test-fs-poll.c
+4 −0 test/test-fs.c
+86 −0 test/test-get-passwd.c
+29 −6 test/test-homedir.c
+35 −0 test/test-list.h
+3 −0 test/test-pipe-close-stdout-read-stdin.c
+1 −0 test/test-pipe-getsockname.c
+16 −0 test/test-platform-output.c
+50 −7 test/test-poll.c
+1 −1 test/test-tcp-close-while-connecting.c
+3 −0 test/test-tcp-create-socket-early.c
+2 −0 test/test-tcp-write-queue-order.c
+21 −0 test/test-thread.c
+27 −0 test/test-timer.c
+71 −0 test/test-tmpdir.c
+108 −0 test/test-tty.c
+3 −0 test/test-udp-create-socket-early.c
+19 −9 uv.gyp
+10 −5 vcbuild.bat
8 changes: 8 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
v0.5.1 (2016-7-23)
-----------
* Fix: Free buffer
* Fix: Verify cipher length
* Fix: Running instance
* Feature: Reuse socket


v0.5.0 (2016-3-29)
-----------
* Feature: TCP support
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MAJOR = 0
MINOR = 5
PATCH = 0
PATCH = 1
NAME = xTun

ifdef O
@@ -46,7 +46,7 @@ ifdef CROSS_COMPILE
CPPFLAGS = -DCROSS_COMPILE
endif

CFLAGS = \
CFLAGS += \
-O2 \
-g \
-std=gnu99 \
@@ -103,6 +103,7 @@ android: libuv libsodium $(XTUN_STATIC)
$(OBJTREE)/3rd/libuv/Makefile: | 3rd/libuv/autogen.sh
$(Q)mkdir -p $(OBJTREE)/3rd/libuv
$(Q)cd 3rd/libuv && ./autogen.sh
$(Q)cd 3rd/libuv &&autoreconf --force -ivf
$(Q)cd $(OBJTREE)/3rd/libuv && $(SRCTREE)/3rd/libuv/configure --host=$(HOST) LDFLAGS= && $(MAKE)

libuv: $(OBJTREE)/3rd/libuv/Makefile
2 changes: 1 addition & 1 deletion openwrt/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
include $(TOPDIR)/rules.mk

PKG_NAME:=xTun
PKG_VERSION:=0.5.0
PKG_VERSION:=0.5.1
PKG_RELEASE=

PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.gz
2 changes: 1 addition & 1 deletion openwrt/files/xTun.init
Original file line number Diff line number Diff line change
@@ -117,7 +117,7 @@ net_stop() {

acl() {
if [ ! -f $BLACK_LIST ]; then
exit 1
return
fi

while read line;do
14 changes: 7 additions & 7 deletions src/android.c
Original file line number Diff line number Diff line change
@@ -128,20 +128,20 @@ new_query(int tunfd, struct iphdr *iphdr, struct udphdr *udphdr) {
query->timer = malloc(sizeof(uv_timer_t));

uv_udp_init(uv_default_loop(), &query->handle);
uv_timer_init(uv_default_loop(), query->timer);
uv_udp_recv_start(&query->handle, dns_alloc_cb, dns_recv_cb);

uv_os_fd_t fd = 0;
int rc = uv_fileno((uv_handle_t*) &query->handle, &fd);
if (rc) {
logger_log(LOG_ERR, "Get fileno error: %s", uv_strerror(rc));
int rc;
int fd = create_socket(SOCK_DGRAM, 0);
if ((rc = uv_udp_open(&query->handle, fd))) {
logger_log(LOG_ERR, "UDP open error: %s", uv_strerror(rc));
free(query->timer);
free(query);
return NULL;
}

protect_socket(fd);

uv_timer_init(uv_default_loop(), query->timer);
uv_udp_recv_start(&query->handle, dns_alloc_cb, dns_recv_cb);

return query;
}

23 changes: 16 additions & 7 deletions src/crypto.c
Original file line number Diff line number Diff line change
@@ -16,14 +16,15 @@
#define COB crypto_onetimeauth_BYTES // 16U
#define COKB crypto_onetimeauth_KEYBYTES // 32U
#define CSSNB crypto_stream_salsa20_NONCEBYTES // 8U
#define CSSKB crypto_stream_salsa20_KEYBYTES //32U
#define CSSKB crypto_stream_salsa20_KEYBYTES // 32U

static uint8_t secret_key[crypto_generichash_BYTES];
static uint8_t secret_key[crypto_generichash_BYTES]; // 32U


static int
salsa208poly1305_encrypt(uint8_t *c, const uint8_t *m, const uint32_t mlen,
const uint8_t *n, const uint8_t *k) {
const uint8_t *n, const uint8_t *k)
{
uint8_t cok[COKB];

crypto_stream_salsa208(cok, COKB, n, k);
@@ -35,7 +36,8 @@ salsa208poly1305_encrypt(uint8_t *c, const uint8_t *m, const uint32_t mlen,

static int
salsa208poly1305_decrypt(uint8_t *m, const uint8_t *c, const uint32_t clen,
const uint8_t *n, const uint8_t *k) {
const uint8_t *n, const uint8_t *k)
{
uint8_t cok[COKB];

if (clen < COB) {
@@ -61,11 +63,14 @@ crypto_init(const char *password) {
randombytes_set_implementation(&randombytes_salsa20_implementation);
randombytes_stir();

return crypto_generichash(secret_key, sizeof secret_key, (uint8_t*)password, strlen(password), NULL, 0);
return crypto_generichash(secret_key, sizeof secret_key,
(uint8_t*)password, strlen(password), NULL, 0);
}

int
crypto_generickey(uint8_t *out, size_t outlen, uint8_t *in, size_t inlen, uint8_t *key, size_t keylen) {
crypto_generickey(uint8_t *out, size_t outlen, uint8_t *in, size_t inlen,
uint8_t *key, size_t keylen)
{
return crypto_generichash(out, outlen, in, inlen, key, keylen);
}

@@ -80,6 +85,10 @@ crypto_encrypt(uint8_t *c, const uint8_t *m, const uint32_t mlen) {
int
crypto_decrypt(uint8_t *m, const uint8_t *c, const uint32_t clen) {
uint8_t nonce[CSSNB];
if (clen <= CSSNB + COB) {
return -1;
}
memcpy(nonce, c, CSSNB);
return salsa208poly1305_decrypt(m, c + CSSNB, clen - CSSNB, nonce, secret_key);
return salsa208poly1305_decrypt(m, c + CSSNB, clen - CSSNB, nonce,
secret_key);
}
2 changes: 0 additions & 2 deletions src/daemon.c
Original file line number Diff line number Diff line change
@@ -56,8 +56,6 @@ already_running(const char *pidfile) {
exit(1);
}

close(fd);

return 0;
}

2 changes: 1 addition & 1 deletion src/main.c
Original file line number Diff line number Diff line change
@@ -64,7 +64,7 @@ print_usage(const char *prog) {
" [-p --port <port>]\t server port to listen on/connect to (default: 1082)\n"
" [-i <iface>]\t\t interface name (default: tun0)\n"
" [-b --bind <host>]\t bind to a specific interface (only available on server mode, default: 0.0.0.0)\n"
" [-P <parallel>]\t number of parallel tun queues (only available on server mode)\n"
" [-P <parallel>]\t number of parallel tun queues (only available on server mode & UDP)\n"
" [--pid <pid>]\t\t PID file of daemon (default: /var/run/xTun.pid)\n"
" [--mtu <mtu>]\t\t MTU size (default: 1426)\n"
" [--signal <signal>]\t send signal to xTun: quit, stop\n"
46 changes: 21 additions & 25 deletions src/packet.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#include <string.h>
#include <assert.h>

#include "uv.h"

#include "util.h"
#include "packet.h"

@@ -20,45 +18,42 @@ packet_alloc(struct packet *packet, uv_buf_t *buf) {

int
packet_filter(struct packet *packet, const char *buf, ssize_t buflen) {
int rc;
int rc = PACKET_INVALID;

if (packet->size == 0) {
assert(buflen <= HEADER_BYTES);

if (packet->read == 1) {
packet->size = read_size((uint8_t *) packet->buf);
if (packet->size <= packet->max) {
rc = PACKET_UNCOMPLETE;
} else {
rc = PACKET_INVALID;
}

} else {
if (buflen == 1) {
packet->read = 1;
rc = PACKET_UNCOMPLETE;

} else if (buflen == HEADER_BYTES) {
if (packet->read == 0) {
if (buflen == HEADER_BYTES) {
packet->size = read_size((uint8_t *) buf);
if (packet->size > 0 && packet->size <= packet->max) {
if (packet->size > PRIMITIVE_BYTES && packet->size <= packet->max) {
rc = PACKET_UNCOMPLETE;
} else {
rc = PACKET_INVALID;
}

} else {
assert(buflen == 1);
packet->read = 1;
rc = PACKET_UNCOMPLETE;
}

} else {
assert(packet->read == 1);
packet->size = read_size((uint8_t *) packet->buf);
if (packet->size > PRIMITIVE_BYTES && packet->size <= packet->max) {
rc = PACKET_UNCOMPLETE;
} else {
rc = PACKET_INVALID;
}
}

} else {
if (buflen + packet->offset < packet->size) {
packet->offset += buflen;
rc = PACKET_UNCOMPLETE;
if (buflen + packet->offset == packet->size) {
rc = PACKET_COMPLETED;

} else {
assert(buflen + packet->offset == packet->size);
rc = PACKET_COMPLETED;
assert(buflen + packet->offset < packet->size);
packet->offset += buflen;
rc = PACKET_UNCOMPLETE;
}
}

@@ -70,4 +65,5 @@ packet_reset(struct packet *packet) {
packet->read = 0;
packet->offset = 0;
packet->size = 0;
memset(packet->buf, 0, packet->max);
}
8 changes: 3 additions & 5 deletions src/peer.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#ifndef CLIENT_H
#define CLIENT_H

#ifndef _PEER_H
#define _PEER_H
#include "uv.h"

struct peer {
@@ -16,5 +15,4 @@ void destroy_peers(struct peer **peers);
struct peer * lookup_peer(uint32_t addr, struct peer **peers);
struct peer * save_peer(uint32_t tun_addr, struct sockaddr *remote_addr,
struct peer **peers);

#endif // for #ifndef CLIENT_H
#endif // for #ifndef _PEER_H
10 changes: 8 additions & 2 deletions src/tcp_client.c
Original file line number Diff line number Diff line change
@@ -63,6 +63,8 @@ recv_cb(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf) {
int mlen = packet->size - PRIMITIVE_BYTES;
uint8_t *c = packet->buf, *m = packet->buf;

assert(mlen > 0 && mlen <= ctx->tun->mtu);

int err = crypto_decrypt(m, c, clen);
if (err) {
goto err;
@@ -84,6 +86,9 @@ recv_cb(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf) {

err:
logger_log(LOG_ERR, "Invalid tcp packet");
if (verbose) {
dump_hex(buf->base, nread, "Invalid tcp Packet");
}
disconnect(ctx);
}

@@ -147,8 +152,9 @@ connect_to_server(struct tundev_context *ctx) {
int
tcp_client_start(struct tundev_context *ctx, uv_loop_t *loop) {
ctx->interval = 5;
ctx->packet.buf = malloc(ctx->tun->mtu + OVERHEAD_BYTES);
ctx->packet.max = ctx->tun->mtu + PRIMITIVE_BYTES;
ctx->packet.buf = malloc(PRIMITIVE_BYTES + ctx->tun->mtu);
ctx->packet.max = PRIMITIVE_BYTES + ctx->tun->mtu;
packet_reset(&ctx->packet);
uv_timer_init(loop, &ctx->timer);
connect_to_server(ctx);
return 0;
19 changes: 13 additions & 6 deletions src/tcp_server.c
Original file line number Diff line number Diff line change
@@ -27,9 +27,9 @@ static struct client_context *
new_client(int mtu) {
struct client_context *client = malloc(sizeof(*client));
memset(client, 0, sizeof(*client));
client->packet.buf = malloc(mtu + OVERHEAD_BYTES);
client->packet.buf = malloc(PRIMITIVE_BYTES + mtu);
client->packet.max = PRIMITIVE_BYTES + mtu;
packet_reset(&client->packet);
client->packet.max = mtu + PRIMITIVE_BYTES;
return client;
}

@@ -61,6 +61,7 @@ handle_invalid_packet(struct client_context *client) {
char remote[INET_ADDRSTRLEN + 1];
port = ip_name(&client->addr, remote, sizeof(remote));
logger_log(LOG_ERR, "Invalid tcp packet from %s:%d", remote, port);
packet_reset(&client->packet);
close_client(client);
}

@@ -78,9 +79,9 @@ recv_cb(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf) {

ctx = stream->data;
client = container_of(stream, struct client_context, handle.stream);
struct packet *packet = &client->packet;

if (nread > 0) {
struct packet *packet = &client->packet;
int rc = packet_filter(packet, buf->base, nread);
if (rc == PACKET_UNCOMPLETE) {
return;
@@ -92,6 +93,8 @@ recv_cb(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf) {
int mlen = packet->size - PRIMITIVE_BYTES;
uint8_t *c = packet->buf, *m = packet->buf;

assert(mlen > 0 && mlen <= ctx->tun->mtu);

int err = crypto_decrypt(m, c, clen);
if (err) {
goto error;
@@ -148,6 +151,9 @@ recv_cb(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf) {
return;

error:
if (verbose) {
dump_hex(buf->base, nread, "Invalid tcp Packet");
}
handle_invalid_packet(client);
}

@@ -162,6 +168,8 @@ accept_cb(uv_stream_t *stream, int status) {
int len = sizeof(struct sockaddr);
uv_tcp_getpeername(&client->handle.tcp, &client->addr, &len);
client->handle.stream.data = ctx;
uv_tcp_nodelay(&client->handle.tcp, 1);
uv_tcp_keepalive(&client->handle.tcp, 1, 60);
uv_read_start(&client->handle.stream, alloc_cb, recv_cb);

} else {
@@ -188,9 +196,6 @@ tcp_server_start(struct tundev_context *ctx, uv_loop_t *loop) {
exit(1);
}

rc = uv_tcp_nodelay(&ctx->inet_tcp.tcp, 1);
rc = uv_tcp_keepalive(&ctx->inet_tcp.tcp, 1, 60);

ctx->inet_tcp.tcp.data = ctx;
rc = uv_listen(&ctx->inet_tcp.stream, 128, accept_cb);
if (rc) {
@@ -205,5 +210,7 @@ tun_to_tcp_client(struct peer *peer, uint8_t *buf, int len) {
struct client_context *client = peer->data;
if (client) {
tun_to_tcp(buf, len, &client->handle.stream);
} else {
free(buf);
}
}
16 changes: 8 additions & 8 deletions src/tun.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <unistd.h>
#include <string.h>
#include <assert.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <linux/if.h>
@@ -60,7 +61,9 @@ close_tunfd(int fd) {
static void
close_socket_handle(struct tundev_context *ctx) {
if (mode == xTUN_SERVER) {
uv_close(&ctx->inet_tcp.handle, NULL);
if (uv_is_active(&ctx->inet_tcp.handle)) {
uv_close(&ctx->inet_tcp.handle, NULL);
}
uv_close((uv_handle_t *) &ctx->inet_udp, NULL);

} else {
@@ -102,6 +105,7 @@ poll_cb(uv_poll_t *watcher, int status, int events) {
struct peer *peer = lookup_peer(iphdr->daddr, peers);
uv_rwlock_rdunlock(&rwlock);
if (peer) {
assert(peer->protocol == xTUN_TCP || peer->protocol == xTUN_UDP);
crypto_encrypt(tunbuf, m, mlen);
if (peer->protocol == xTUN_TCP) {
tun_to_tcp_client(peer, tunbuf, PRIMITIVE_BYTES + mlen);
@@ -421,11 +425,7 @@ queue_start(void *arg) {
uv_async_init(&loop, &ctx->async_handle, queue_close);

udp_start(ctx, &loop);
if (mode == xTUN_SERVER) {
tcp_server_start(ctx, &loop);
} else {
tcp_client_start(ctx, &loop);
}
/* tcp_server_start(ctx, &loop); */

uv_poll_init(&loop, &ctx->watcher, ctx->tunfd);
uv_poll_start(&ctx->watcher, UV_READABLE, poll_cb);
@@ -453,7 +453,7 @@ loop_close(uv_loop_t *loop) {

static void
signal_close() {
for (int i = 0; i <= 2; i++) {
for (int i = 0; i < 2; i++) {
uv_signal_stop(&signals[i].sig);
}
}
@@ -475,7 +475,7 @@ static void
signal_install(uv_loop_t *loop, uv_signal_cb cb, void *data) {
signals[0].signum = SIGINT;
signals[1].signum = SIGQUIT;
for (int i = 0; i <= 2; i++) {
for (int i = 0; i < 2; i++) {
signals[i].sig.data = data;
uv_signal_init(loop, &signals[i].sig);
uv_signal_start(&signals[i].sig, cb, signals[i].signum);
101 changes: 58 additions & 43 deletions src/udp.c
Original file line number Diff line number Diff line change
@@ -35,53 +35,68 @@ static void
inet_recv_cb(uv_udp_t *handle, ssize_t nread, const uv_buf_t *buf,
const struct sockaddr *addr, unsigned flags)
{
if (nread <= 0) {
return;
}

struct tundev_context *ctx = container_of(handle, struct tundev_context,
inet_udp);

if (nread > 0) {
uint8_t *m = (uint8_t *)buf->base;
ssize_t mlen = nread - PRIMITIVE_BYTES;
uint8_t *m = (uint8_t *)buf->base;
ssize_t mlen = nread - PRIMITIVE_BYTES;

int rc = crypto_decrypt(m, (uint8_t *)buf->base, nread);
if (rc) {
int port = 0;
char remote[INET_ADDRSTRLEN + 1];
port = ip_name(addr, remote, sizeof(remote));
logger_log(LOG_ERR, "Invalid udp packet from %s:%d", remote, port);
return;
}
int valid = mlen > 0 && mlen <= ctx->tun->mtu;
if (!valid) {
goto error;
}

if (mode == xTUN_SERVER) {
struct iphdr *iphdr = (struct iphdr *) m;
int rc = crypto_decrypt(m, (uint8_t *)buf->base, nread);
if (rc) {
goto error;
}

in_addr_t client_network = iphdr->saddr & htonl(ctx->tun->netmask);
if (client_network != ctx->tun->network) {
char *a = inet_ntoa(*(struct in_addr *) &iphdr->saddr);
logger_log(LOG_ERR, "Invalid client: %s", a);
return;
}
if (mode == xTUN_SERVER) {
struct iphdr *iphdr = (struct iphdr *) m;

in_addr_t client_network = iphdr->saddr & htonl(ctx->tun->netmask);
if (client_network != ctx->tun->network) {
char *a = inet_ntoa(*(struct in_addr *) &iphdr->saddr);
logger_log(LOG_ERR, "Invalid client: %s", a);
return;
}

// TODO: Compare source address
uv_rwlock_rdlock(&rwlock);
struct peer *peer = lookup_peer(iphdr->saddr, peers);
uv_rwlock_rdunlock(&rwlock);
if (peer == NULL) {
char saddr[24] = {0}, daddr[24] = {0};
parse_addr(iphdr, saddr, daddr);
logger_log(LOG_WARNING, "[UDP] Cache miss: %s -> %s", saddr, daddr);
uv_rwlock_wrlock(&rwlock);
peer = save_peer(iphdr->saddr, (struct sockaddr *) addr, peers);
uv_rwlock_wrunlock(&rwlock);

} else {
if (memcmp(&peer->remote_addr, addr, sizeof(*addr))) {
peer->remote_addr = *addr;
}
// TODO: Compare source address
uv_rwlock_rdlock(&rwlock);
struct peer *peer = lookup_peer(iphdr->saddr, peers);
uv_rwlock_rdunlock(&rwlock);
if (peer == NULL) {
char saddr[24] = {0}, daddr[24] = {0};
parse_addr(iphdr, saddr, daddr);
logger_log(LOG_WARNING, "[UDP] Cache miss: %s -> %s",
saddr, daddr);
uv_rwlock_wrlock(&rwlock);
peer = save_peer(iphdr->saddr, (struct sockaddr *) addr, peers);
uv_rwlock_wrunlock(&rwlock);

} else {
if (memcmp(&peer->remote_addr, addr, sizeof(*addr))) {
peer->remote_addr = *addr;
}
peer->protocol = xTUN_UDP;
}
peer->protocol = xTUN_UDP;
}

network_to_tun(ctx->tunfd, m, mlen);

return;

network_to_tun(ctx->tunfd, m, mlen);
int port = 0;
char remote[INET_ADDRSTRLEN + 1];
error:
port = ip_name(addr, remote, sizeof(remote));
logger_log(LOG_ERR, "Invalid udp packet from %s:%d", remote, port);
if (verbose) {
dump_hex(buf->base, nread, "Invalid udp Packet");
}
}

@@ -103,10 +118,10 @@ tun_to_udp(struct tundev_context *ctx, uint8_t *buf, int len,
uv_buf_t *outbuf = (uv_buf_t *) (write_req + 1);
outbuf->base = (char *) buf;
outbuf->len = len;
if (write_req) {
write_req->data = ctx;
uv_udp_send(write_req, &ctx->inet_udp, outbuf, 1, addr, inet_send_cb);
} else {
int rc = uv_udp_send(write_req, &ctx->inet_udp, outbuf, 1, addr,
inet_send_cb);
if (rc) {
logger_log(LOG_ERR, "UDP Write error: %s", uv_strerror(rc));
free(buf);
}
}
@@ -121,7 +136,7 @@ udp_start(struct tundev_context *ctx, uv_loop_t *loop) {

ctx->inet_udp_fd = create_socket(SOCK_DGRAM, mode == xTUN_SERVER ? 1 : 0);
if ((rc = uv_udp_open(&ctx->inet_udp, ctx->inet_udp_fd))) {
logger_log(LOG_ERR, "udp open error: %s", uv_strerror(rc));
logger_log(LOG_ERR, "UDP open error: %s", uv_strerror(rc));
exit(1);
}

@@ -134,7 +149,7 @@ udp_start(struct tundev_context *ctx, uv_loop_t *loop) {
if (mode == xTUN_SERVER) {
rc = uv_udp_bind(&ctx->inet_udp, &ctx->tun->addr, UV_UDP_REUSEADDR);
if (rc) {
logger_stderr("udp bind error: %s", uv_strerror(rc));
logger_stderr("UDP bind error: %s", uv_strerror(rc));
exit(1);
}
}
6 changes: 5 additions & 1 deletion src/util.c
Original file line number Diff line number Diff line change
@@ -197,11 +197,15 @@ create_socket(int type, int reuse) {
return -1;
}
if (reuse) {
#ifdef SO_REUSEPORT
int yes = 1;
#ifdef SO_REUSEPORT
if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &yes, sizeof(yes))) {
logger_stderr("setsockopt SO_REUSEPORT error: %s", strerror(errno));
}
#else
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes))) {
logger_stderr("setsockopt SO_REUSEADDR error: %s", strerror(errno));
}
#endif
}
return sock;
2 changes: 1 addition & 1 deletion src/xTun.h
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@
#include <stdint.h>
#include <netinet/in.h>

#define xTun_VERSION "0.5.0"
#define xTun_VERSION "0.5.1"
#define xTun_VER "xTun/" xTun_VERSION

struct tundev;
2 changes: 2 additions & 0 deletions valgrind.supp
Original file line number Diff line number Diff line change
@@ -30,7 +30,9 @@
Memcheck:Leak
match-leak-kinds: possible
fun:calloc
fun:allocate_dtv
fun:_dl_allocate_tls
fun:allocate_stack
fun:pthread_create@@GLIBC_2.2.5
fun:uv_thread_create
fun:tun_start

0 comments on commit eaf61e8

Please sign in to comment.