Skip to content

Commit 800e85d

Browse files
committed
fix #146
1 parent e778a31 commit 800e85d

File tree

1 file changed

+76
-9
lines changed

1 file changed

+76
-9
lines changed

src/main/jni/badvpn/tun2socks/tun2socks.c

Lines changed: 76 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,61 @@
7070

7171
#include <generated/blog_channel_tun2socks.h>
7272

73+
#ifdef ANDROID
74+
#include <structure/BAVL.h>
75+
BAVL connections_tree;
76+
typedef struct {
77+
BAddr local_addr;
78+
BAddr remote_addr;
79+
uint16_t port;
80+
int count;
81+
BAVLNode connections_tree_node;
82+
} Connection;
83+
84+
static int conaddr_comparator (void *unused, uint16_t *v1, uint16_t *v2)
85+
{
86+
if (*v1 == *v2) return 0;
87+
else if (*v1 > *v2) return 1;
88+
else return -1;
89+
}
90+
91+
static Connection * find_connection (uint16_t port)
92+
{
93+
BAVLNode *tree_node = BAVL_LookupExact(&connections_tree, &port);
94+
if (!tree_node) {
95+
return NULL;
96+
}
97+
98+
return UPPER_OBJECT(tree_node, Connection, connections_tree_node);
99+
}
100+
101+
static void remove_connection (Connection *con)
102+
{
103+
con->count -= 1;
104+
if (con->count <= 0)
105+
{
106+
BAVL_Remove(&connections_tree, &con->connections_tree_node);
107+
free(con);
108+
}
109+
}
110+
111+
static void insert_connection (BAddr local_addr, BAddr remote_addr, uint16_t port)
112+
{
113+
Connection * con = find_connection(port);
114+
if (con != NULL)
115+
con->count += 1;
116+
else
117+
{
118+
Connection * tmp = (Connection *)malloc(sizeof(Connection));
119+
tmp->local_addr = local_addr;
120+
tmp->remote_addr = remote_addr;
121+
tmp->port = port;
122+
tmp->count = 1;
123+
BAVL_Insert(&connections_tree, &tmp->connections_tree_node, NULL);
124+
}
125+
}
126+
#endif
127+
73128
#define LOGGER_STDOUT 1
74129
#define LOGGER_SYSLOG 2
75130

@@ -1199,8 +1254,6 @@ int process_device_dns_packet (uint8_t *data, int data_len)
11991254
goto fail;
12001255
}
12011256

1202-
static BAddr local_addr;
1203-
static BAddr remote_addr;
12041257
static int init = 0;
12051258

12061259
int to_dns;
@@ -1257,9 +1310,13 @@ int process_device_dns_packet (uint8_t *data, int data_len)
12571310
// construct addresses
12581311
if (!init) {
12591312
init = 1;
1260-
BAddr_InitIPv4(&local_addr, ipv4_header.source_address, udp_header.source_port);
1261-
BAddr_InitIPv4(&remote_addr, ipv4_header.destination_address, udp_header.dest_port);
1313+
BAVL_Init(&connections_tree, OFFSET_DIFF(Connection, port, connections_tree_node), (BAVL_comparator)conaddr_comparator, NULL);
12621314
}
1315+
BAddr local_addr;
1316+
BAddr remote_addr;
1317+
BAddr_InitIPv4(&local_addr, ipv4_header.source_address, udp_header.source_port);
1318+
BAddr_InitIPv4(&remote_addr, ipv4_header.destination_address, udp_header.dest_port);
1319+
insert_connection(local_addr, remote_addr, udp_header.source_port);
12631320

12641321
// build IP header
12651322
ipv4_header.destination_address = dnsgw.ipv4.ip;
@@ -1277,13 +1334,23 @@ int process_device_dns_packet (uint8_t *data, int data_len)
12771334

12781335
BLog(BLOG_INFO, "UDP: from DNS %d bytes", data_len);
12791336

1280-
// build IP header
1281-
ipv4_header.source_address = remote_addr.ipv4.ip;
1282-
ipv4_header.destination_address = local_addr.ipv4.ip;
1337+
Connection * con = find_connection(udp_header.dest_port);
1338+
if (con != NULL)
1339+
{
1340+
// build IP header
1341+
ipv4_header.source_address = con->remote_addr.ipv4.ip;
1342+
ipv4_header.destination_address = con->local_addr.ipv4.ip;
12831343

1284-
// build UDP header
1285-
udp_header.source_port = remote_addr.ipv4.port;
1344+
// build UDP header
1345+
udp_header.source_port = con->remote_addr.ipv4.port;
1346+
1347+
remove_connection(con);
12861348

1349+
}
1350+
else
1351+
{
1352+
goto fail;
1353+
}
12871354
}
12881355

12891356
// update IPv4 header's checksum

0 commit comments

Comments
 (0)