mirror of
https://github.com/wangyu-/udp2raw.git
synced 2025-09-15 19:54:28 +08:00
Compare commits
28 Commits
20171024.0
...
20171103.0
Author | SHA1 | Date | |
---|---|---|---|
|
4c92c29948 | ||
|
cadb142455 | ||
|
fcb418f5ea | ||
|
63eb7f1f7f | ||
|
66b2bb87c5 | ||
|
6b27f8624d | ||
|
ce7b89f6b6 | ||
|
a0bfa86699 | ||
|
e332cabfed | ||
|
9798bf496a | ||
|
7aa4b8745e | ||
|
ea6a41ebb3 | ||
|
77ba1161f5 | ||
|
7858c4d832 | ||
|
71152ffbe3 | ||
|
b06e979dc8 | ||
|
7a23486533 | ||
|
6515d428e9 | ||
|
5300f98b0a | ||
|
063d133463 | ||
|
4c88df9c0e | ||
|
52cb20ec20 | ||
|
7aede9edd0 | ||
|
c3e1dab838 | ||
|
00edb620be | ||
|
95ee6e64dc | ||
|
deeb7395a4 | ||
|
537f8a6311 |
16
README.md
16
README.md
@@ -1,9 +1,15 @@
|
||||
# Udp2raw-tunnel
|
||||

|
||||
|
||||
|
||||
A Tunnel which turns UDP Traffic into Encrypted FakeTCP/UDP/ICMP Traffic by using Raw Socket, helps you Bypass UDP FireWalls(or Unstable UDP Environment). It can defend Replay-Attack and supports Multiplexing. It also acts as a Connection Stabilizer.
|
||||
|
||||
It can tunnel any traffic when used together with a UDP-based VPN(such as OpenVPN).Check [this link](https://github.com/wangyu-/udp2raw-tunnel#tunneling-any-traffic-via-raw-traffic-by-using-udp2raw-openvpn) for more info.
|
||||
When used alone,udp2raw tunnels only UDP traffic. Nevertheless,if you used udp2raw + any UDP-based VPN together,you can tunnel any traffic(include TCP/UDP/ICMP),currently OpenVPN/L2TP/ShadowVPN and [tinyFecVPN](https://github.com/wangyu-/tinyFecVPN) are confirmed to be supported。
|
||||
|
||||

|
||||
|
||||
or
|
||||
|
||||

|
||||
|
||||
[简体中文](/doc/README.zh-cn.md)
|
||||
|
||||
@@ -191,6 +197,10 @@ Then start the server with
|
||||
./udp2raw_amd64 --conf-file server.conf
|
||||
```
|
||||
|
||||
### `--fifo`
|
||||
Use a fifo(named pipe) for sending commands to the running program. For example `--fifo fifo.file`.
|
||||
|
||||
At client side,you can use `echo reconnect >fifo.file` to force client to reconnect.Currently no command has been implemented for server.
|
||||
|
||||
# Peformance Test
|
||||
#### Test method:
|
||||
@@ -222,7 +232,7 @@ raw_mode: faketcp cipher_mode: aes128cbc auth_mode: md5
|
||||
|
||||
# Application
|
||||
## Tunneling any traffic via raw traffic by using udp2raw +openvpn
|
||||

|
||||

|
||||
1. Bypasses UDP block/UDP QOS
|
||||
|
||||
2. No TCP over TCP problem (TCP over TCP problem http://sites.inka.de/bigred/devel/tcp-tcp.html ,https://community.openvpn.net/openvpn/ticket/2 )
|
||||
|
16
common.cpp
16
common.cpp
@@ -593,7 +593,21 @@ int create_fifo(char * file)
|
||||
return fifo_fd;
|
||||
}
|
||||
|
||||
|
||||
void ip_port_t::from_u64(u64_t u64)
|
||||
{
|
||||
ip=get_u64_h(u64);
|
||||
port=get_u64_l(u64);
|
||||
}
|
||||
u64_t ip_port_t::to_u64()
|
||||
{
|
||||
return pack_u64(ip,port);
|
||||
}
|
||||
char * ip_port_t::to_s()
|
||||
{
|
||||
static char res[40];
|
||||
sprintf(res,"%s:%d",my_ntoa(ip),port);
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
15
common.h
15
common.h
@@ -70,6 +70,21 @@ typedef u64_t padding_t;
|
||||
|
||||
typedef u64_t anti_replay_seq_t;
|
||||
|
||||
|
||||
struct ip_port_t
|
||||
{
|
||||
u32_t ip;
|
||||
int port;
|
||||
void from_u64(u64_t u64);
|
||||
u64_t to_u64();
|
||||
char * to_s();
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
typedef u64_t fd64_t;
|
||||
|
||||
const int max_data_len=1600;
|
||||
const int buf_len=max_data_len+400;
|
||||
|
||||
|
@@ -7,6 +7,7 @@
|
||||
|
||||
#include "connection.h"
|
||||
#include "encrypt.h"
|
||||
#include "fd_manager.h"
|
||||
|
||||
|
||||
int disable_anti_replay=0;//if anti_replay windows is diabled
|
||||
@@ -249,7 +250,7 @@ conv_manager_t::~conv_manager_t()
|
||||
last_state_time=0;
|
||||
oppsite_const_id=0;
|
||||
|
||||
timer_fd=0;
|
||||
timer_fd64=0;
|
||||
|
||||
my_roller=0;
|
||||
oppsite_roller=0;
|
||||
@@ -296,6 +297,7 @@ conv_manager_t::~conv_manager_t()
|
||||
assert(oppsite_const_id==0);
|
||||
}
|
||||
}
|
||||
assert(timer_fd64==0);
|
||||
//if(oppsite_const_id!=0) //do this at conn_manager 's deconstuction function
|
||||
//conn_manager.const_id_mp.erase(oppsite_const_id);
|
||||
if(blob!=0)
|
||||
@@ -310,9 +312,9 @@ conv_manager_t::~conv_manager_t()
|
||||
ready_num=0;
|
||||
mp.reserve(10007);
|
||||
clear_it=mp.begin();
|
||||
timer_fd_mp.reserve(10007);
|
||||
// timer_fd_mp.reserve(10007);
|
||||
const_id_mp.reserve(10007);
|
||||
udp_fd_mp.reserve(100007);
|
||||
// udp_fd_mp.reserve(100007);
|
||||
last_clear_time=0;
|
||||
//current_ready_ip=0;
|
||||
// current_ready_port=0;
|
||||
@@ -372,21 +374,33 @@ conv_manager_t::~conv_manager_t()
|
||||
ready_num--;
|
||||
assert(i32_t(ready_num)!=-1);
|
||||
assert(erase_it->second!=0);
|
||||
assert(erase_it->second->timer_fd !=0);
|
||||
|
||||
assert(erase_it->second->timer_fd64 !=0);
|
||||
|
||||
assert(fd_manager.exist(erase_it->second->timer_fd64));
|
||||
|
||||
assert(erase_it->second->oppsite_const_id!=0);
|
||||
assert(const_id_mp.find(erase_it->second->oppsite_const_id)!=const_id_mp.end());
|
||||
assert(timer_fd_mp.find(erase_it->second->timer_fd)!=timer_fd_mp.end());
|
||||
|
||||
|
||||
//assert(timer_fd_mp.find(erase_it->second->timer_fd)!=timer_fd_mp.end());
|
||||
|
||||
const_id_mp.erase(erase_it->second->oppsite_const_id);
|
||||
timer_fd_mp.erase(erase_it->second->timer_fd);
|
||||
close(erase_it->second->timer_fd);// close will auto delte it from epoll
|
||||
|
||||
fd_manager.fd64_close(erase_it->second->timer_fd64);
|
||||
|
||||
erase_it->second->timer_fd64=0;
|
||||
//timer_fd_mp.erase(erase_it->second->timer_fd);
|
||||
//close(erase_it->second->timer_fd);// close will auto delte it from epoll
|
||||
delete(erase_it->second);
|
||||
mp.erase(erase_it->first);
|
||||
}
|
||||
else
|
||||
{
|
||||
assert(erase_it->second->blob==0);
|
||||
assert(erase_it->second->timer_fd ==0);
|
||||
assert(erase_it->second->timer_fd64 ==0);
|
||||
|
||||
|
||||
assert(erase_it->second->oppsite_const_id==0);
|
||||
delete(erase_it->second);
|
||||
mp.erase(erase_it->first);
|
||||
@@ -720,9 +734,9 @@ int recv_safer(conn_info_t &conn_info,char &type,char* &data,int &len)///safer t
|
||||
void server_clear_function(u64_t u64)//used in conv_manager in server mode.for server we have to use one udp fd for one conv(udp connection),
|
||||
//so we have to close the fd when conv expires
|
||||
{
|
||||
int fd=int(u64);
|
||||
int ret;
|
||||
assert(fd!=0);
|
||||
//int fd=int(u64);
|
||||
// int ret;
|
||||
//assert(fd!=0);
|
||||
/*
|
||||
epoll_event ev;
|
||||
|
||||
@@ -735,14 +749,19 @@ void server_clear_function(u64_t u64)//used in conv_manager in server mode.for s
|
||||
mylog(log_fatal,"fd:%d epoll delete failed!!!!\n",fd);
|
||||
myexit(-1); //this shouldnt happen
|
||||
}*/ //no need
|
||||
ret= close(fd); //closed fd should be auto removed from epoll
|
||||
|
||||
/*ret= close(fd); //closed fd should be auto removed from epoll
|
||||
|
||||
if (ret!=0)
|
||||
{
|
||||
mylog(log_fatal,"close fd %d failed !!!!\n",fd);
|
||||
myexit(-1); //this shouldnt happen
|
||||
}
|
||||
}*/
|
||||
//mylog(log_fatal,"size:%d !!!!\n",conn_manager.udp_fd_mp.size());
|
||||
assert(conn_manager.udp_fd_mp.find(fd)!=conn_manager.udp_fd_mp.end());
|
||||
conn_manager.udp_fd_mp.erase(fd);
|
||||
fd64_t fd64=u64;
|
||||
assert(fd_manager.exist(fd64));
|
||||
fd_manager.fd64_close(fd64);
|
||||
|
||||
//assert(conn_manager.udp_fd_mp.find(fd)!=conn_manager.udp_fd_mp.end());
|
||||
//conn_manager.udp_fd_mp.erase(fd);
|
||||
}
|
||||
|
@@ -85,7 +85,8 @@ struct conn_info_t //stores info for a raw connection.for client ,there is o
|
||||
id_t oppsite_id;
|
||||
|
||||
|
||||
int timer_fd;
|
||||
fd64_t timer_fd64;
|
||||
|
||||
id_t oppsite_const_id;
|
||||
|
||||
blob_t *blob;
|
||||
@@ -94,6 +95,8 @@ struct conn_info_t //stores info for a raw connection.for client ,there is o
|
||||
uint8_t oppsite_roller;
|
||||
u64_t last_oppsite_roller_time;
|
||||
|
||||
// ip_port_t ip_port;
|
||||
|
||||
/*
|
||||
const uint32_t &ip=raw_info.recv_info.src_ip;
|
||||
const uint16_t &port=raw_info.recv_info.src_port;
|
||||
@@ -113,8 +116,8 @@ struct conn_manager_t //manager for connections. for client,we dont need conn_m
|
||||
|
||||
u32_t ready_num;
|
||||
|
||||
unordered_map<int,conn_info_t *> udp_fd_mp; //a bit dirty to used pointer,but can void unordered_map search
|
||||
unordered_map<int,conn_info_t *> timer_fd_mp;//we can use pointer here since unordered_map.rehash() uses shallow copy
|
||||
//unordered_map<int,conn_info_t *> udp_fd_mp; //a bit dirty to used pointer,but can void unordered_map search
|
||||
//unordered_map<int,conn_info_t *> timer_fd_mp;//we can use pointer here since unordered_map.rehash() uses shallow copy
|
||||
|
||||
unordered_map<id_t,conn_info_t *> const_id_mp;
|
||||
|
||||
|
@@ -4,15 +4,17 @@ udp2raw tunnel,通过raw socket给UDP包加上TCP或ICMP header,进而绕过
|
||||
|
||||
支持心跳保活、自动重连,重连后会恢复上次连接,在底层掉线的情况下可以保持上层不掉线。同时有加密、防重放攻击、信道复用的功能。
|
||||
|
||||
**欢迎任何形式的转载**
|
||||
|
||||
[English](/README.md)
|
||||
|
||||
[udp2raw+kcptun step_by_step教程](kcptun_step_by_step.md)
|
||||
|
||||
[udp2raw+finalspeed step_by_step教程](finalspeed_step_by_step.md)
|
||||
|
||||
如果你需要加速跨国网游、网页浏览,解决方案在另一个repo:
|
||||
**提示:**
|
||||
|
||||
udp2raw不是加速器,只是一个帮助你绕过UDP限制的工具。如果你需要UDP加速器,请看UDPspeeder。
|
||||
|
||||
UDPspeeder的repo:
|
||||
|
||||
https://github.com/wangyu-/UDPspeeder
|
||||
# 支持的平台
|
||||
@@ -98,7 +100,7 @@ udp2raw可以用非root账号运行,这样更安全。具体方法见:[#26](
|
||||
### 命令选项
|
||||
```
|
||||
udp2raw-tunnel
|
||||
git version:adbe7d110f build date:Sep 6 2017 05:37:45
|
||||
git version:6e1df4b39f build date:Oct 24 2017 09:21:15
|
||||
repository: https://github.com/wangyu-/udp2raw-tunnel
|
||||
|
||||
usage:
|
||||
@@ -121,6 +123,8 @@ client options:
|
||||
other options:
|
||||
--conf-file <string> read options from a configuration file instead of command line.
|
||||
check example.conf in repo for format
|
||||
--fifo <string> use a fifo(named pipe) for sending commands to the running program,
|
||||
check readme.md in repository for supported commands.
|
||||
--log-level <number> 0:never 1:fatal 2:error 3:warn
|
||||
4:info (default) 5:debug 6:trace
|
||||
--log-position enable file name,function name,line number in log
|
||||
@@ -162,6 +166,11 @@ facktcp模式并没有模拟tcp的全部。所以理论上有办法把faketcp和
|
||||
### `--lower-level`
|
||||
大部分udp2raw不能连通的情况都是设置了不兼容的iptables造成的。--lower-level选项允许绕过本地iptables。在一些iptables不好改动的情况下尤其有效(比如你用的是梅林固件,iptables全是固件自己生成的)。
|
||||
|
||||
### `--fifo`
|
||||
指定一个fifo(named pipe)来向运行中的程序发送命令,例如`--fifo fifo.file`:
|
||||
|
||||
在client端,可以用`echo reconnect >fifo.file`来强制client换端口重连(上层不断线).对Server,目前没有效果。
|
||||
|
||||
##### 格式
|
||||
`if_name#dest_mac_adress`,例如 `eth0#00:23:45:67:89:b9` 。`eth0`换成你的出口网卡名。`00:23:45:67:89:b9`换成网关的mac地址(如果client和server在同一个局域网内,可能不需要网关,这时候直接用对方主机的mac地址,这个属于罕见的应用场景,可以忽略)。
|
||||
|
||||
|
63
fd_manager.cpp
Normal file
63
fd_manager.cpp
Normal file
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* fd_manager.cpp
|
||||
*
|
||||
* Created on: Sep 25, 2017
|
||||
* Author: root
|
||||
*/
|
||||
|
||||
|
||||
#include "fd_manager.h"
|
||||
int fd_manager_t::fd_exist(int fd)
|
||||
{
|
||||
return fd_to_fd64_mp.find(fd)!=fd_to_fd64_mp.end();
|
||||
}
|
||||
int fd_manager_t::exist(fd64_t fd64)
|
||||
{
|
||||
return fd64_to_fd_mp.find(fd64)!=fd64_to_fd_mp.end();
|
||||
}
|
||||
int fd_manager_t::to_fd(fd64_t fd64)
|
||||
{
|
||||
assert(exist(fd64));
|
||||
return fd64_to_fd_mp[fd64];
|
||||
}
|
||||
void fd_manager_t::fd64_close(fd64_t fd64)
|
||||
{
|
||||
assert(exist(fd64));
|
||||
int fd=fd64_to_fd_mp[fd64];
|
||||
fd64_to_fd_mp.erase(fd64);
|
||||
fd_to_fd64_mp.erase(fd);
|
||||
if(exist_info(fd64))
|
||||
{
|
||||
fd_info_mp.erase(fd64);
|
||||
}
|
||||
assert(close(fd)==0);
|
||||
}
|
||||
void fd_manager_t::reserve(int n)
|
||||
{
|
||||
fd_to_fd64_mp.reserve(n);
|
||||
fd64_to_fd_mp.reserve(n);
|
||||
fd_info_mp.reserve(n);
|
||||
}
|
||||
u64_t fd_manager_t::create(int fd)
|
||||
{
|
||||
assert(!fd_exist(fd));
|
||||
fd64_t fd64=counter++;
|
||||
fd_to_fd64_mp[fd]=fd64;
|
||||
fd64_to_fd_mp[fd64]=fd;
|
||||
return fd64;
|
||||
}
|
||||
fd_manager_t::fd_manager_t()
|
||||
{
|
||||
counter=u32_t(-1);
|
||||
counter+=100;
|
||||
reserve(10007);
|
||||
}
|
||||
fd_info_t & fd_manager_t::get_info(fd64_t fd64)
|
||||
{
|
||||
assert(exist(fd64));
|
||||
return fd_info_mp[fd64];
|
||||
}
|
||||
int fd_manager_t::exist_info(fd64_t fd64)
|
||||
{
|
||||
return fd_info_mp.find(fd64)!=fd_info_mp.end();
|
||||
}
|
43
fd_manager.h
Normal file
43
fd_manager.h
Normal file
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* fd_manager.h
|
||||
*
|
||||
* Created on: Sep 25, 2017
|
||||
* Author: root
|
||||
*/
|
||||
|
||||
#ifndef FD_MANAGER_H_
|
||||
#define FD_MANAGER_H_
|
||||
|
||||
#include "common.h"
|
||||
//#include "packet.h"
|
||||
#include "connection.h"
|
||||
|
||||
struct fd_info_t
|
||||
{
|
||||
//ip_port_t ip_port;
|
||||
conn_info_t *p_conn_info;
|
||||
};
|
||||
|
||||
struct fd_manager_t //conver fd to a uniq 64bit number,avoid fd value conflict caused by close and re-create
|
||||
//this class is not strictly necessary,it just makes epoll fd handling easier
|
||||
{
|
||||
fd_info_t & get_info(fd64_t fd64);
|
||||
int exist_info(fd64_t);
|
||||
int exist(fd64_t fd64);
|
||||
int to_fd(fd64_t);
|
||||
void fd64_close(fd64_t fd64);
|
||||
void reserve(int n);
|
||||
u64_t create(int fd);
|
||||
fd_manager_t();
|
||||
private:
|
||||
u64_t counter;
|
||||
unordered_map<int,fd64_t> fd_to_fd64_mp;
|
||||
unordered_map<fd64_t,int> fd64_to_fd_mp;
|
||||
unordered_map<fd64_t,fd_info_t> fd_info_mp;
|
||||
int fd_exist(int fd);
|
||||
//void remove_fd(int fd);
|
||||
//fd64_t fd_to_fd64(int fd);
|
||||
};
|
||||
|
||||
extern fd_manager_t fd_manager;
|
||||
#endif /* FD_MANAGER_H_ */
|
BIN
images/udp2rawopenvpn.PNG
Normal file
BIN
images/udp2rawopenvpn.PNG
Normal file
Binary file not shown.
After Width: | Height: | Size: 44 KiB |
126
main.cpp
126
main.cpp
@@ -5,6 +5,7 @@
|
||||
#include "log.h"
|
||||
#include "lib/md5.h"
|
||||
#include "encrypt.h"
|
||||
#include "fd_manager.h"
|
||||
|
||||
int mtu_warn=1375;//if a packet larger than mtu warn is receviced,there will be a warning
|
||||
|
||||
@@ -26,8 +27,6 @@ int client_on_timer(conn_info_t &conn_info) //for client. called when a timer is
|
||||
mylog(log_trace,"<client_on_timer,send_info.ts_ack= %u>\n",send_info.ts_ack);
|
||||
|
||||
|
||||
|
||||
|
||||
if(conn_info.state.client_current_state==client_idle)
|
||||
{
|
||||
fail_time_counter++;
|
||||
@@ -602,6 +601,9 @@ int server_on_raw_recv_multi() //called when server received an raw packet
|
||||
conn_info_t &conn_info=conn_manager.find_insert(ip,port);
|
||||
conn_info.raw_info=tmp_raw_info;
|
||||
|
||||
//conn_info.ip_port.ip=ip;
|
||||
//conn_info.ip_port.port=port;
|
||||
|
||||
packet_info_t &send_info=conn_info.raw_info.send_info;
|
||||
packet_info_t &recv_info=conn_info.raw_info.recv_info;
|
||||
raw_info_t &raw_info=conn_info.raw_info;
|
||||
@@ -821,11 +823,13 @@ int server_on_raw_recv_ready(conn_info_t &conn_info,char * ip_port,char type,cha
|
||||
}
|
||||
struct epoll_event ev;
|
||||
|
||||
u64_t u64 = (u32_t(new_udp_fd))+(1llu<<32u);
|
||||
mylog(log_trace, "[%s]u64: %lld\n",ip_port, u64);
|
||||
fd64_t new_udp_fd64 = fd_manager.create(new_udp_fd);
|
||||
fd_manager.get_info(new_udp_fd64).p_conn_info=&conn_info;
|
||||
|
||||
mylog(log_trace, "[%s]u64: %lld\n",ip_port, new_udp_fd64);
|
||||
ev.events = EPOLLIN;
|
||||
|
||||
ev.data.u64 = u64;
|
||||
ev.data.u64 = new_udp_fd64;
|
||||
|
||||
ret = epoll_ctl(epollfd, EPOLL_CTL_ADD, new_udp_fd, &ev);
|
||||
|
||||
@@ -835,10 +839,13 @@ int server_on_raw_recv_ready(conn_info_t &conn_info,char * ip_port,char type,cha
|
||||
return -1;
|
||||
}
|
||||
|
||||
conn_info.blob->conv_manager.insert_conv(tmp_conv_id, new_udp_fd);
|
||||
assert(conn_manager.udp_fd_mp.find(new_udp_fd)==conn_manager.udp_fd_mp.end());
|
||||
conn_info.blob->conv_manager.insert_conv(tmp_conv_id, new_udp_fd64);
|
||||
|
||||
conn_manager.udp_fd_mp[new_udp_fd] = &conn_info;
|
||||
|
||||
|
||||
//assert(conn_manager.udp_fd_mp.find(new_udp_fd)==conn_manager.udp_fd_mp.end());
|
||||
|
||||
//conn_manager.udp_fd_mp[new_udp_fd] = &conn_info;
|
||||
|
||||
//pack_u64(conn_info.raw_info.recv_info.src_ip,conn_info.raw_info.recv_info.src_port);
|
||||
|
||||
@@ -849,11 +856,11 @@ int server_on_raw_recv_ready(conn_info_t &conn_info,char * ip_port,char type,cha
|
||||
|
||||
}
|
||||
|
||||
u64_t u64 = conn_info.blob->conv_manager.find_u64_by_conv(tmp_conv_id);
|
||||
fd64_t fd64 = conn_info.blob->conv_manager.find_u64_by_conv(tmp_conv_id);
|
||||
|
||||
conn_info.blob->conv_manager.update_active_time(tmp_conv_id);
|
||||
|
||||
int fd = int((u64 << 32u) >> 32u);
|
||||
int fd = fd_manager.to_fd(fd64);
|
||||
|
||||
mylog(log_trace, "[%s]received a data from fake tcp,len:%d\n",ip_port, data_len);
|
||||
int ret = send(fd, data + sizeof(u32_t),
|
||||
@@ -917,10 +924,11 @@ int server_on_raw_recv_pre_ready(conn_info_t &conn_info,char * ip_port,u32_t tmp
|
||||
|
||||
//g_conn_info=conn_info;
|
||||
int new_timer_fd;
|
||||
set_timer_server(epollfd, new_timer_fd);
|
||||
conn_info.timer_fd=new_timer_fd;
|
||||
assert(conn_manager.timer_fd_mp.find(new_timer_fd)==conn_manager.timer_fd_mp.end());
|
||||
conn_manager.timer_fd_mp[new_timer_fd] = &conn_info;//pack_u64(ip,port);
|
||||
set_timer_server(epollfd, new_timer_fd,conn_info.timer_fd64);
|
||||
|
||||
fd_manager.get_info(conn_info.timer_fd64).p_conn_info=&conn_info;
|
||||
//assert(conn_manager.timer_fd_mp.find(new_timer_fd)==conn_manager.timer_fd_mp.end());
|
||||
//conn_manager.timer_fd_mp[new_timer_fd] = &conn_info;//pack_u64(ip,port);
|
||||
|
||||
|
||||
//timer_fd_mp[new_timer_fd]
|
||||
@@ -1196,7 +1204,12 @@ int client_event_loop()
|
||||
else if (events[idx].data.u64 == (u64_t)fifo_fd)
|
||||
{
|
||||
int len=read (fifo_fd, buf, sizeof (buf));
|
||||
assert(len>=0);
|
||||
//assert(len>=0);
|
||||
if(len<0)
|
||||
{
|
||||
mylog(log_warn,"fifo read failed len=%d,errno=%s\n",len,strerror(errno));
|
||||
continue;
|
||||
}
|
||||
buf[len]=0;
|
||||
while(len>=1&&buf[len-1]=='\n')
|
||||
buf[len-1]=0;
|
||||
@@ -1452,29 +1465,58 @@ int server_event_loop()
|
||||
else if (events[idx].data.u64 == (u64_t)fifo_fd)
|
||||
{
|
||||
int len=read (fifo_fd, buf, sizeof (buf));
|
||||
assert(len>=0);
|
||||
if(len<0)
|
||||
{
|
||||
mylog(log_warn,"fifo read failed len=%d,errno=%s\n",len,strerror(errno));
|
||||
continue;
|
||||
}
|
||||
//assert(len>=0);
|
||||
buf[len]=0;
|
||||
while(len>=1&&buf[len-1]=='\n')
|
||||
buf[len-1]=0;
|
||||
mylog(log_info,"got data from fifo,len=%d,s=[%s]\n",len,buf);
|
||||
mylog(log_info,"unknown command\n");
|
||||
}
|
||||
else if ((events[idx].data.u64 >>32u) == 2u)
|
||||
else if (events[idx].data.u64>u32_t(-1) )
|
||||
{
|
||||
|
||||
fd64_t fd64=events[idx].data.u64;
|
||||
|
||||
if(!fd_manager.exist(fd64))
|
||||
{
|
||||
mylog(log_trace ,"fd64 no longer exist\n");
|
||||
continue;
|
||||
}
|
||||
|
||||
assert(fd_manager.exist_info(fd64));
|
||||
|
||||
conn_info_t* p_conn_info=fd_manager.get_info(fd64).p_conn_info;
|
||||
u32_t ip=p_conn_info->raw_info.send_info.dst_ip;
|
||||
u32_t port=p_conn_info->raw_info.send_info.dst_port;
|
||||
|
||||
//assert(conn_manager.exist(ip,port));
|
||||
|
||||
///conn_info_t* p_conn_info=conn_manager.find_insert_p(ip,port);
|
||||
|
||||
|
||||
if(fd64==p_conn_info->timer_fd64)//////////timer_fd64
|
||||
{
|
||||
|
||||
if(debug_flag)begin_time=get_current_time();
|
||||
int fd=get_u64_l(events[idx].data.u64);
|
||||
//int fd=get_u64_l(events[idx].data.u64);
|
||||
int fd=fd_manager.to_fd(fd64);
|
||||
u64_t dummy;
|
||||
read(fd, &dummy, 8);
|
||||
|
||||
if(conn_manager.timer_fd_mp.find(fd)==conn_manager.timer_fd_mp.end()) //this can happen,when fd is a just closed fd
|
||||
/*if(conn_manager.timer_fd_mp.find(fd)==conn_manager.timer_fd_mp.end()) //this can happen,when fd is a just closed fd
|
||||
{
|
||||
mylog(log_info,"timer_fd no longer exits\n");
|
||||
continue;
|
||||
}
|
||||
conn_info_t* p_conn_info=conn_manager.timer_fd_mp[fd];
|
||||
u32_t ip=p_conn_info->raw_info.recv_info.src_ip;
|
||||
u32_t port=p_conn_info->raw_info.recv_info.src_port;
|
||||
assert(conn_manager.exist(ip,port));//TODO remove this for peformance
|
||||
}*/
|
||||
//conn_info_t* p_conn_info=conn_manager.timer_fd_mp[fd];
|
||||
//u32_t ip=p_conn_info->raw_info.recv_info.src_ip;
|
||||
//u32_t port=p_conn_info->raw_info.recv_info.src_port;
|
||||
//assert(conn_manager.exist(ip,port));//TODO remove this for peformance
|
||||
|
||||
assert(p_conn_info->state.server_current_state == server_ready); //TODO remove this for peformance
|
||||
|
||||
@@ -1490,30 +1532,36 @@ int server_event_loop()
|
||||
end_time=get_current_time();
|
||||
mylog(log_debug,"(events[idx].data.u64 >>32u) == 2u ,%llu,%llu,%llu \n",begin_time,end_time,end_time-begin_time);
|
||||
}
|
||||
|
||||
}
|
||||
else if ((events[idx].data.u64 >>32u) == 1u)
|
||||
else//udp_fd64
|
||||
{
|
||||
//}
|
||||
//else if ((events[idx].data.u64 >>32u) == 1u)
|
||||
//{
|
||||
//uint32_t conv_id=events[n].data.u64>>32u;
|
||||
|
||||
if(debug_flag)begin_time=get_current_time();
|
||||
|
||||
int fd=int((events[idx].data.u64<<32u)>>32u);
|
||||
//int fd=int((events[idx].data.u64<<32u)>>32u);
|
||||
|
||||
/*
|
||||
if(conn_manager.udp_fd_mp.find(fd)==conn_manager.udp_fd_mp.end()) //this can happen,when fd is a just closed fd
|
||||
{
|
||||
mylog(log_debug,"fd no longer exists in udp_fd_mp,udp fd %d\n",fd);
|
||||
recv(fd,0,0,0);
|
||||
continue;
|
||||
}
|
||||
conn_info_t* p_conn_info=conn_manager.udp_fd_mp[fd];
|
||||
}*/
|
||||
//conn_info_t* p_conn_info=conn_manager.udp_fd_mp[fd];
|
||||
|
||||
u32_t ip=p_conn_info->raw_info.recv_info.src_ip;
|
||||
u32_t port=p_conn_info->raw_info.recv_info.src_port;
|
||||
if(!conn_manager.exist(ip,port))//TODO remove this for peformance
|
||||
//u32_t ip=p_conn_info->raw_info.recv_info.src_ip;
|
||||
//u32_t port=p_conn_info->raw_info.recv_info.src_port;
|
||||
|
||||
/*if(!conn_manager.exist(ip,port))//TODO remove this for peformance
|
||||
{
|
||||
mylog(log_fatal,"ip port no longer exits 2!!!this shouldnt happen\n");
|
||||
myexit(-1);
|
||||
}
|
||||
}*/
|
||||
|
||||
if(p_conn_info->state.server_current_state!=server_ready)//TODO remove this for peformance
|
||||
{
|
||||
@@ -1523,14 +1571,11 @@ int server_event_loop()
|
||||
|
||||
conn_info_t &conn_info=*p_conn_info;
|
||||
|
||||
if(!conn_info.blob->conv_manager.is_u64_used(fd))
|
||||
{
|
||||
mylog(log_debug,"conv no longer exists,udp fd %d\n",fd);
|
||||
int recv_len=recv(fd,0,0,0); ///////////TODO ,delete this
|
||||
continue;
|
||||
}
|
||||
assert(conn_info.blob->conv_manager.is_u64_used(fd64));
|
||||
|
||||
u32_t conv_id=conn_info.blob->conv_manager.find_conv_by_u64(fd);
|
||||
u32_t conv_id=conn_info.blob->conv_manager.find_conv_by_u64(fd64);
|
||||
|
||||
int fd=fd_manager.to_fd(fd64);
|
||||
|
||||
int recv_len=recv(fd,buf,max_data_len,0);
|
||||
|
||||
@@ -1562,6 +1607,9 @@ int server_event_loop()
|
||||
end_time=get_current_time();
|
||||
mylog(log_debug,"(events[idx].data.u64 >>32u) == 1u,%lld,%lld,%lld \n",begin_time,end_time,end_time-begin_time);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
|
5
makefile
5
makefile
@@ -8,14 +8,14 @@ cc_arm= /toolchains/arm-2014.05/bin/arm-none-linux-gnueabi-g++
|
||||
#cc_bcm2708=/home/wangyu/raspberry/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/arm-linux-gnueabihf-g++
|
||||
FLAGS= -std=c++11 -Wall -Wextra -Wno-unused-variable -Wno-unused-parameter -Wno-missing-field-initializers
|
||||
|
||||
COMMON=main.cpp lib/md5.c encrypt.cpp log.cpp network.cpp common.cpp connection.cpp misc.cpp -lpthread
|
||||
COMMON=main.cpp lib/md5.c encrypt.cpp log.cpp network.cpp common.cpp connection.cpp misc.cpp fd_manager.cpp -lpthread
|
||||
SOURCES= $(COMMON) lib/aes_faster_c/aes.c lib/aes_faster_c/wrapper.c
|
||||
SOURCES_TINY_AES= $(COMMON) lib/aes.c
|
||||
SOURCES_AES_ACC=$(COMMON) $(wildcard lib/aes_acc/aes*.c)
|
||||
|
||||
NAME=udp2raw
|
||||
TARGETS=amd64 arm amd64_hw_aes arm_asm_aes mips24kc_be mips24kc_be_asm_aes x86 x86_asm_aes mips24kc_le mips24kc_le_asm_aes
|
||||
TAR=${NAME}_binaries.tar.gz `echo ${TARGETS}|sed -r 's/([^ ]+)/udp2raw_\1/g'`
|
||||
TAR=${NAME}_binaries.tar.gz `echo ${TARGETS}|sed -r 's/([^ ]+)/udp2raw_\1/g'` version.txt
|
||||
|
||||
all:git_version
|
||||
rm -f ${NAME}
|
||||
@@ -69,6 +69,7 @@ cross3:git_version
|
||||
${cc_cross} -o ${NAME}_cross -I. ${SOURCES} ${FLAGS} -lrt -static -O3
|
||||
|
||||
release: ${TARGETS}
|
||||
cp git_version.h version.txt
|
||||
tar -zcvf ${TAR}
|
||||
|
||||
clean:
|
||||
|
9
misc.cpp
9
misc.cpp
@@ -10,8 +10,11 @@
|
||||
#include "misc.h"
|
||||
#include "network.h"
|
||||
#include "connection.h"
|
||||
#include "fd_manager.h"
|
||||
|
||||
|
||||
fd_manager_t fd_manager;
|
||||
|
||||
char local_ip[100]="0.0.0.0", remote_ip[100]="255.255.255.255",source_ip[100]="0.0.0.0";//local_ip is for -l option,remote_ip for -r option,source for --source-ip
|
||||
u32_t local_ip_uint32,remote_ip_uint32,source_ip_uint32;//convert from last line.
|
||||
int local_port = -1, remote_port=-1,source_port=0;//similiar to local_ip remote_ip,buf for port.source_port=0 indicates --source-port is not enabled
|
||||
@@ -948,7 +951,7 @@ int set_timer(int epollfd,int &timer_fd)//put a timer_fd into epoll,general func
|
||||
}
|
||||
|
||||
|
||||
int set_timer_server(int epollfd,int &timer_fd)//only for server
|
||||
int set_timer_server(int epollfd,int &timer_fd,fd64_t &fd64)//only for server
|
||||
{
|
||||
int ret;
|
||||
epoll_event ev;
|
||||
@@ -966,9 +969,11 @@ int set_timer_server(int epollfd,int &timer_fd)//only for server
|
||||
its.it_value.tv_nsec=1; //imidiately
|
||||
timerfd_settime(timer_fd,0,&its,0);
|
||||
|
||||
fd64=fd_manager.create(timer_fd);
|
||||
|
||||
|
||||
ev.events = EPOLLIN;
|
||||
ev.data.u64 = pack_u64(2,timer_fd);////difference
|
||||
ev.data.u64 = fd64;////difference
|
||||
|
||||
ret=epoll_ctl(epollfd, EPOLL_CTL_ADD, timer_fd, &ev);
|
||||
if (ret < 0) {
|
||||
|
2
misc.h
2
misc.h
@@ -112,7 +112,7 @@ void iptables_rule();
|
||||
void pre_process_arg(int argc, char *argv[]);//mainly for load conf file;
|
||||
int unit_test();
|
||||
int set_timer(int epollfd,int &timer_fd);
|
||||
int set_timer_server(int epollfd,int &timer_fd);
|
||||
int set_timer_server(int epollfd,int &timer_fd,fd64_t &fd64);
|
||||
int handle_lower_level(raw_info_t &raw_info);
|
||||
|
||||
int add_iptables_rule(const char *);
|
||||
|
16
network.cpp
16
network.cpp
@@ -1820,17 +1820,17 @@ int get_src_adress(u32_t &ip,u32_t remote_ip_uint32,int remote_port) //a trick
|
||||
return 0;
|
||||
}
|
||||
|
||||
int try_to_list_and_bind(int bind_fd,u32_t local_ip_uint32,int port) //try to bind to a port,may fail.
|
||||
int try_to_list_and_bind(int &fd,u32_t local_ip_uint32,int port) //try to bind to a port,may fail.
|
||||
{
|
||||
int old_bind_fd=bind_fd;
|
||||
int old_bind_fd=fd;
|
||||
|
||||
if(raw_mode==mode_faketcp)
|
||||
{
|
||||
bind_fd=socket(AF_INET,SOCK_STREAM,0);
|
||||
fd=socket(AF_INET,SOCK_STREAM,0);
|
||||
}
|
||||
else if(raw_mode==mode_udp||raw_mode==mode_icmp)
|
||||
{
|
||||
bind_fd=socket(AF_INET,SOCK_DGRAM,0);
|
||||
fd=socket(AF_INET,SOCK_DGRAM,0);
|
||||
}
|
||||
if(old_bind_fd!=-1)
|
||||
{
|
||||
@@ -1844,7 +1844,7 @@ int try_to_list_and_bind(int bind_fd,u32_t local_ip_uint32,int port) //try to b
|
||||
temp_bind_addr.sin_port = htons(port);
|
||||
temp_bind_addr.sin_addr.s_addr = local_ip_uint32;
|
||||
|
||||
if (bind(bind_fd, (struct sockaddr*)&temp_bind_addr, sizeof(temp_bind_addr)) !=0)
|
||||
if (bind(fd, (struct sockaddr*)&temp_bind_addr, sizeof(temp_bind_addr)) !=0)
|
||||
{
|
||||
mylog(log_debug,"bind fail\n");
|
||||
return -1;
|
||||
@@ -1852,19 +1852,19 @@ int try_to_list_and_bind(int bind_fd,u32_t local_ip_uint32,int port) //try to b
|
||||
if(raw_mode==mode_faketcp)
|
||||
{
|
||||
|
||||
if (listen(bind_fd, SOMAXCONN) != 0) {
|
||||
if (listen(fd, SOMAXCONN) != 0) {
|
||||
mylog(log_warn,"listen fail\n");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
int client_bind_to_a_new_port(int bind_fd,u32_t local_ip_uint32)//find a free port and bind to it.
|
||||
int client_bind_to_a_new_port(int &fd,u32_t local_ip_uint32)//find a free port and bind to it.
|
||||
{
|
||||
int raw_send_port=10000+get_true_random_number()%(65535-10000);
|
||||
for(int i=0;i<1000;i++)//try 1000 times at max,this should be enough
|
||||
{
|
||||
if (try_to_list_and_bind(bind_fd,local_ip_uint32,raw_send_port)==0)
|
||||
if (try_to_list_and_bind(fd,local_ip_uint32,raw_send_port)==0)
|
||||
{
|
||||
return raw_send_port;
|
||||
}
|
||||
|
@@ -99,9 +99,9 @@ int find_lower_level_info(u32_t ip,u32_t &dest_ip,string &if_name,string &hw);
|
||||
|
||||
int get_src_adress(u32_t &ip,u32_t remote_ip_uint32,int remote_port); //a trick to get src adress for a dest adress,so that we can use the src address in raw socket as source ip
|
||||
|
||||
int try_to_list_and_bind(int bind_fd,u32_t local_ip_uint32,int port); //try to bind to a port,may fail.
|
||||
int try_to_list_and_bind(int & bind_fd,u32_t local_ip_uint32,int port); //try to bind to a port,may fail.
|
||||
|
||||
int client_bind_to_a_new_port(int bind_fd,u32_t local_ip_uint32);//find a free port and bind to it.
|
||||
int client_bind_to_a_new_port(int & bind_fd,u32_t local_ip_uint32);//find a free port and bind to it.
|
||||
|
||||
int send_raw_ip(raw_info_t &raw_info,const char * payload,int payloadlen);
|
||||
|
||||
|
Reference in New Issue
Block a user