70
70
71
71
#include <generated/blog_channel_tun2socks.h>
72
72
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
+
73
128
#define LOGGER_STDOUT 1
74
129
#define LOGGER_SYSLOG 2
75
130
@@ -1199,8 +1254,6 @@ int process_device_dns_packet (uint8_t *data, int data_len)
1199
1254
goto fail ;
1200
1255
}
1201
1256
1202
- static BAddr local_addr ;
1203
- static BAddr remote_addr ;
1204
1257
static int init = 0 ;
1205
1258
1206
1259
int to_dns ;
@@ -1257,9 +1310,13 @@ int process_device_dns_packet (uint8_t *data, int data_len)
1257
1310
// construct addresses
1258
1311
if (!init ) {
1259
1312
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 );
1262
1314
}
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 );
1263
1320
1264
1321
// build IP header
1265
1322
ipv4_header .destination_address = dnsgw .ipv4 .ip ;
@@ -1277,13 +1334,23 @@ int process_device_dns_packet (uint8_t *data, int data_len)
1277
1334
1278
1335
BLog (BLOG_INFO , "UDP: from DNS %d bytes" , data_len );
1279
1336
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 ;
1283
1343
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 );
1286
1348
1349
+ }
1350
+ else
1351
+ {
1352
+ goto fail ;
1353
+ }
1287
1354
}
1288
1355
1289
1356
// update IPv4 header's checksum
0 commit comments