Skip to content

Commit

Permalink
Merge branch 'release/0.4.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
lparam committed Dec 5, 2015
2 parents 9aedf06 + bbc52ae commit 12c889a
Show file tree
Hide file tree
Showing 13 changed files with 534 additions and 269 deletions.
2 changes: 1 addition & 1 deletion 3rd/libsodium
Submodule libsodium updated 203 files
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
v0.4.0 (2015-12-05)
-----------
* Feature: Multiqueue support


v0.3.0 (2015-11-18)
-----------
* Feature: Android support
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
MAJOR = 0
MINOR = 3
MINOR = 4
PATCH = 0
NAME = xTun

Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Features
* Stateless
* CCA security
* Low cost (CPU, RAM and packet overhead)
* Cross-platform, including PC (Linux, Mobile ([Android](https://github.com/lparam/xTun-android)) and Router (OpenWRT)
* Parallelization


BUILD
Expand All @@ -32,7 +34,7 @@ Usage
### Server

```bash
xTun -i IFACE -I IP/MASK -c server -k PASSWORD
xTun -i IFACE -I IP/MASK -m server -k PASSWORD -P PARALLEL
scripts/server_up.sh
```

Expand All @@ -45,7 +47,7 @@ scripts/server_down.sh
### Client

```bash
xTun -i IFACE -I IP/MASK -c client -k PASSWORD -s SERVER
xTun -i IFACE -I IP/MASK -m client -k PASSWORD -s SERVER
scripts/client_up.sh
```

Expand Down
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.3.0
PKG_VERSION:=0.4.0
PKG_RELEASE=

PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.gz
Expand Down
106 changes: 55 additions & 51 deletions src/android.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,34 @@
#include "android.h"


#define DNS_ANSWER_SIZE 1024
#define TIMEOUT 60
#define HASHSIZE 256


struct dns_query {
int tunfd;
struct iphdr iphdr;
struct udphdr udphdr;
uv_udp_t handle;
uv_timer_t *timer;
int tunfd;
struct iphdr iphdr;
struct udphdr udphdr;
uv_udp_t handle;
uv_timer_t *timer;
};

struct query_cache {
struct dns_query *query;
struct query_cache *next;
struct dns_query *query;
struct query_cache *next;
};

#define DNS_ANSWER_SIZE 1024
#define TIMEOUT 60
#define HASHSIZE 256

static struct query_cache *caches[HASHSIZE];

static void dns_alloc_cb(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf);
static void dns_recv_cb(uv_udp_t *handle, ssize_t nread, const uv_buf_t *buf, const struct sockaddr *addr, unsigned flags);
static void dns_alloc_cb(uv_handle_t *handle, size_t suggested_size,
uv_buf_t *buf);
static void dns_recv_cb(uv_udp_t *handle, ssize_t nread, const uv_buf_t *buf,
const struct sockaddr *addr, unsigned flags);
static void close_query(struct dns_query *query);
static void handle_local_dns_answer(struct dns_query *query, uint8_t *buf, size_t len);
static void handle_local_dns_answer(struct dns_query *query, uint8_t *buf,
size_t len);
void network_to_tun(int tunfd, uint8_t *buf, ssize_t len);


static uint16_t
Expand All @@ -51,10 +56,12 @@ static struct query_cache *
cache_lookup(uint16_t port) {
int h = hash_query(port);
struct query_cache *cache = caches[h];
if (cache == NULL)
if (cache == NULL) {
return NULL;
if (cache->query->udphdr.source == port)
}
if (cache->query->udphdr.source == port) {
return cache;
}
struct query_cache *last = cache;
while (last->next) {
cache = last->next;
Expand All @@ -70,8 +77,9 @@ static void
cache_remove(uint16_t port) {
int h = hash_query(port);
struct query_cache *cache = caches[h];
if (cache == NULL)
if (cache == NULL) {
return;
}
if (cache->query->udphdr.source == port) {
caches[h] = cache->next;
return;
Expand Down Expand Up @@ -130,10 +138,10 @@ new_query(int tunfd, struct iphdr *iphdr, struct udphdr *udphdr) {
free(query->timer);
free(query);
return NULL;
} else {
protectSocket(fd);
}

protectSocket(fd);

return query;
}

Expand Down Expand Up @@ -174,33 +182,40 @@ dns_send_cb(uv_udp_send_t *req, int status) {
logger_log(LOG_ERR, "DNS query failed: %s", uv_strerror(status));
}
uv_buf_t *buf = (uv_buf_t *)(req + 1);
free(buf->base - sizeof(struct iphdr) - sizeof(struct udphdr) - PRIMITIVE_BYTES);
size_t offset = sizeof(struct iphdr) - sizeof(struct udphdr)
- PRIMITIVE_BYTES;
free(buf->base - offset);
free(req);
}

static void
dns_recv_cb(uv_udp_t *handle, ssize_t nread, const uv_buf_t *buf, const struct sockaddr *addr, unsigned flags) {
dns_recv_cb(uv_udp_t *handle, ssize_t nread, const uv_buf_t *buf,
const struct sockaddr *addr, unsigned flags) {
if (nread > 0) {
struct dns_query *query = container_of(handle, struct dns_query, handle);
struct dns_query *query =
container_of(handle, struct dns_query, handle);
reset_timer(query);
handle_local_dns_answer(query, (uint8_t*)buf->base, nread);
handle_local_dns_answer(query, (uint8_t *) buf->base, nread);
}
free(buf->base);
}

static void
cache_log(struct iphdr *iphdr, struct udphdr *udphdr, struct sockaddr *server, const char *hint) {
cache_log(struct iphdr *iphdr, struct udphdr *udphdr, struct sockaddr *server,
const char *hint) {
char saddr[24] = {0}, daddr[30] = {0};
char *addr = inet_ntoa(*(struct in_addr *) &iphdr->saddr);
strcpy(saddr, addr);
uv_ip4_name((const struct sockaddr_in *) server, daddr, sizeof(daddr));
logger_log(LOG_WARNING, "DNS Cache %s: %s:%d -> %s", hint, saddr, ntohs(udphdr->source), daddr);
logger_log(LOG_WARNING, "DNS Cache %s: %s:%d -> %s", hint, saddr,
ntohs(udphdr->source), daddr);
}

int
handle_local_dns_query(int tunfd, struct sockaddr *dns_server, uint8_t *buf, int buflen) {
struct iphdr *iphdr = (struct iphdr *)buf;
struct udphdr *udphdr = (struct udphdr *)(buf + sizeof(struct iphdr));
handle_local_dns_query(int tunfd, struct sockaddr *dns_server,
uint8_t *buf, int buflen) {
struct iphdr *iphdr = (struct iphdr *) buf;
struct udphdr *udphdr = (struct udphdr *) (buf + sizeof(struct iphdr));

buf += sizeof(struct iphdr) + sizeof(struct udphdr);
buflen -= sizeof(struct iphdr) + sizeof(struct udphdr);
Expand Down Expand Up @@ -232,17 +247,19 @@ handle_local_dns_query(int tunfd, struct sockaddr *dns_server, uint8_t *buf, int

uv_udp_send_t *write_req = malloc(sizeof(*write_req) + sizeof(uv_buf_t));
uv_buf_t *outbuf = (uv_buf_t *)(write_req + 1);
outbuf->base = (char *)buf;
outbuf->base = (char *) buf;
outbuf->len = buflen;
uv_udp_send(write_req, &query->handle, outbuf, 1, dns_server, dns_send_cb);

return 1;
}

static void
create_dns_packet(struct dns_query *query, uint8_t *answer, ssize_t answer_len, uint8_t *packet) {
create_dns_packet(struct dns_query *query, uint8_t *answer, ssize_t answer_len,
uint8_t *packet) {
struct iphdr iphdr;
struct udphdr udphdr;

memset(&iphdr, 0, sizeof(iphdr));
memset(&udphdr, 0, sizeof(udphdr));

Expand All @@ -257,7 +274,8 @@ create_dns_packet(struct dns_query *query, uint8_t *answer, ssize_t answer_len,
iphdr.ttl = query->iphdr.ttl;
iphdr.tos = query->iphdr.tos;

iphdr.tot_len = htons(sizeof(struct iphdr) + sizeof(struct udphdr) + answer_len);
iphdr.tot_len = htons(sizeof(struct iphdr) + sizeof(struct udphdr)
+ answer_len);
iphdr.check = checksum((uint16_t*)&iphdr, sizeof(struct iphdr));

udphdr.dest = query->udphdr.source;
Expand All @@ -271,24 +289,10 @@ create_dns_packet(struct dns_query *query, uint8_t *answer, ssize_t answer_len,
}

static void
handle_local_dns_answer(struct dns_query *query, uint8_t *answer, size_t answer_len) {
int packet_len = sizeof(struct iphdr) + sizeof(struct udphdr) + answer_len;
uint8_t packet[packet_len];

create_dns_packet(query, answer, answer_len, packet);

for (;;) {
int rc = write(query->tunfd, packet, packet_len);
if (rc < 0) {
if (errno == EINTR) {
continue;
} else {
logger_log(LOG_ERR, "Write tun: %s", strerror(errno));
exit(1);
}

} else {
break;
}
}
handle_local_dns_answer(struct dns_query *query, uint8_t *answer,
size_t answer_len) {
int pktsz = sizeof(struct iphdr) + sizeof(struct udphdr) + answer_len;
uint8_t pkt[pktsz];
create_dns_packet(query, answer, answer_len, pkt);
network_to_tun(query->tunfd, pkt, pktsz);
}
Loading

0 comments on commit 12c889a

Please sign in to comment.