Compare commits

...

34 Commits

Author SHA1 Message Date
wangyu-
1628c15fcb new option hb-len 2017-11-11 01:15:50 -06:00
wangyu-
79855b445d Merge branch 'master' of https://github.com/wangyu-/udp2raw-tunnel 2017-11-11 01:07:57 -06:00
wangyu-
9e010c7f97 improve heartbeat 2017-11-11 01:07:28 -06:00
wangyu-
dce81b3fbf Update README.md 2017-11-09 17:02:02 -08:00
wangyu-
24310edd35 tuned parameter 2017-11-09 02:37:26 -06:00
wangyu-
2e1d4f001d Merge branch 'master' of https://github.com/wangyu-/udp2raw-tunnel 2017-11-09 02:20:44 -06:00
wangyu-
ae82925a74 tune parameter 2017-11-09 02:18:22 -06:00
wangyu-
f953d6d702 Update README.zh-cn.md 2017-11-08 16:03:34 -08:00
wangyu-
9e522f79b8 Update README.zh-cn.md 2017-11-08 15:59:04 -08:00
wangyu-
72c6240716 Update README.md 2017-11-08 15:55:41 -08:00
wangyu-
aac9453518 new option hb-mode 2017-11-07 01:07:32 -06:00
wangyu-
b24784b26c changed parameter 2017-11-06 03:15:05 -06:00
wangyu-
5660eed366 increase heart beat length 2017-11-06 03:00:16 -06:00
wangyu-
4c92c29948 added version.txt into makefile 2017-11-03 01:04:59 -05:00
wangyu-
cadb142455 added missing files 2017-11-02 19:49:04 -05:00
wangyu-
fcb418f5ea Merge branch 'master' of https://github.com/wangyu-/udp2raw-tunnel 2017-11-01 23:01:23 -05:00
wangyu-
63eb7f1f7f fixed bug of last few commit, and fixed a bug of bind error 2017-11-01 08:25:50 -05:00
wangyu-
66b2bb87c5 Update README.md 2017-10-30 20:25:17 -07:00
wangyu-
6b27f8624d Update README.md 2017-10-30 20:24:53 -07:00
wangyu-
ce7b89f6b6 Update README.md 2017-10-30 20:22:42 -07:00
wangyu-
a0bfa86699 Update README.zh-cn.md 2017-10-30 08:42:38 -07:00
wangyu-
e332cabfed Update README.zh-cn.md 2017-10-30 08:42:21 -07:00
wangyu-
9798bf496a Update README.zh-cn.md 2017-10-30 08:41:54 -07:00
wangyu-
7aa4b8745e trival 2017-10-30 08:59:47 -05:00
wangyu-
ea6a41ebb3 more fix 2017-10-30 08:37:53 -05:00
wangyu-
77ba1161f5 fix timer of fd64 2017-10-30 08:19:21 -05:00
wangyu-
7858c4d832 fixed some bug of last commit 2017-10-30 08:02:23 -05:00
wangyu-
71152ffbe3 added an assert 2017-10-30 07:29:40 -05:00
wangyu-
b06e979dc8 Merge branch 'master' of https://github.com/wangyu-/udp2raw-tunnel 2017-10-30 07:24:26 -05:00
wangyu-
5300f98b0a Update README.md 2017-10-25 21:10:26 -07:00
wangyu-
063d133463 Update README.md 2017-10-25 21:09:59 -07:00
wangyu-
4c88df9c0e Add files via upload 2017-10-26 12:09:09 +08:00
wangyu-
52cb20ec20 Update README.md 2017-10-25 21:04:18 -07:00
wangyu-
7aede9edd0 Update README.md 2017-10-25 21:00:10 -07:00
14 changed files with 236 additions and 74 deletions

View File

@@ -1,16 +1,23 @@
# Udp2raw-tunnel # Udp2raw-tunnel
![image0](images/image0.PNG)
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. 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.
![image0](images/image0.PNG)
or
![image_vpn](images/udp2rawopenvpn.PNG)
[简体中文](/doc/README.zh-cn.md) [简体中文](/doc/README.zh-cn.md)
# Support Platforms # Support Platforms
Linux host (including desktop Linux,Android phone/tablet,OpenWRT router,or Raspberry PI) with root access. Linux host (including desktop Linux,Android phone/tablet,OpenWRT router,or Raspberry PI) with root access.
For Winodws/MacOS,the 4.4mb virtual image with udp2raw pre-installed has been released,you can load it with Vmware/VirtualBox.The virtual image has been set to auto obtain ip,udp2raw can be run imidiately after boot finished(make sure network mode of virtual machine has been set to bridged)(only udp2raw has to be run under virtual machine,all other programs runs under Windows/MacOS as usual). For Windows and MacOS You can run udp2raw inside [this](https://github.com/wangyu-/udp2raw-tunnel/releases/download/20171108.0/lede-17.01.2-x86_virtual_machine_image.zip) 7.5mb virtual machine image(make sure network adapter runs at bridged mode).
# Features # Features
@@ -226,7 +233,7 @@ raw_mode: faketcp cipher_mode: aes128cbc  auth_mode: md5
# Application # Application
## Tunneling any traffic via raw traffic by using udp2raw +openvpn ## Tunneling any traffic via raw traffic by using udp2raw +openvpn
![image_vpn](images/openvpn.PNG) ![image_vpn](images/udp2rawopenvpn.PNG)
1. Bypasses UDP block/UDP QOS 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 ) 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 )

View File

@@ -80,10 +80,7 @@ struct ip_port_t
char * to_s(); char * to_s();
}; };
struct fd_info_t
{
ip_port_t ip_port;
};
typedef u64_t fd64_t; typedef u64_t fd64_t;

View File

@@ -297,6 +297,7 @@ conv_manager_t::~conv_manager_t()
assert(oppsite_const_id==0); assert(oppsite_const_id==0);
} }
} }
assert(timer_fd64==0);
//if(oppsite_const_id!=0) //do this at conn_manager 's deconstuction function //if(oppsite_const_id!=0) //do this at conn_manager 's deconstuction function
//conn_manager.const_id_mp.erase(oppsite_const_id); //conn_manager.const_id_mp.erase(oppsite_const_id);
if(blob!=0) if(blob!=0)
@@ -388,6 +389,7 @@ conv_manager_t::~conv_manager_t()
fd_manager.fd64_close(erase_it->second->timer_fd64); fd_manager.fd64_close(erase_it->second->timer_fd64);
erase_it->second->timer_fd64=0;
//timer_fd_mp.erase(erase_it->second->timer_fd); //timer_fd_mp.erase(erase_it->second->timer_fd);
//close(erase_it->second->timer_fd);// close will auto delte it from epoll //close(erase_it->second->timer_fd);// close will auto delte it from epoll
delete(erase_it->second); delete(erase_it->second);
@@ -709,7 +711,17 @@ int parse_safer(conn_info_t &conn_info,const char * input,int input_len,char &ty
conn_info.oppsite_roller=roller; conn_info.oppsite_roller=roller;
conn_info.last_oppsite_roller_time=get_current_time(); conn_info.last_oppsite_roller_time=get_current_time();
} }
conn_info.my_roller++;//increase on a successful recv if(hb_mode==0)
conn_info.my_roller++;//increase on a successful recv
else if(hb_mode==1)
{
if(type=='h')
conn_info.my_roller++;
}
else
{
assert(0==1);
}
if(after_recv_raw0(conn_info.raw_info)!=0) return -1; if(after_recv_raw0(conn_info.raw_info)!=0) return -1;
@@ -732,9 +744,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), 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 //so we have to close the fd when conv expires
{ {
int fd=int(u64); //int fd=int(u64);
int ret; // int ret;
assert(fd!=0); //assert(fd!=0);
/* /*
epoll_event ev; epoll_event ev;
@@ -747,16 +759,18 @@ 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); mylog(log_fatal,"fd:%d epoll delete failed!!!!\n",fd);
myexit(-1); //this shouldnt happen myexit(-1); //this shouldnt happen
}*/ //no need }*/ //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) if (ret!=0)
{ {
mylog(log_fatal,"close fd %d failed !!!!\n",fd); mylog(log_fatal,"close fd %d failed !!!!\n",fd);
myexit(-1); //this shouldnt happen myexit(-1); //this shouldnt happen
} }*/
//mylog(log_fatal,"size:%d !!!!\n",conn_manager.udp_fd_mp.size()); //mylog(log_fatal,"size:%d !!!!\n",conn_manager.udp_fd_mp.size());
assert(fd_manager.exist(u64)); fd64_t fd64=u64;
fd_manager.fd64_close(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()); //assert(conn_manager.udp_fd_mp.find(fd)!=conn_manager.udp_fd_mp.end());
//conn_manager.udp_fd_mp.erase(fd); //conn_manager.udp_fd_mp.erase(fd);

View File

@@ -95,7 +95,7 @@ struct conn_info_t //stores info for a raw connection.for client ,there is o
uint8_t oppsite_roller; uint8_t oppsite_roller;
u64_t last_oppsite_roller_time; u64_t last_oppsite_roller_time;
ip_port_t ip_port; // ip_port_t ip_port;
/* /*
const uint32_t &ip=raw_info.recv_info.src_ip; const uint32_t &ip=raw_info.recv_info.src_ip;

View File

@@ -4,22 +4,23 @@ udp2raw tunnel通过raw socket给UDP包加上TCP或ICMP header进而绕过
支持心跳保活、自动重连,重连后会恢复上次连接,在底层掉线的情况下可以保持上层不掉线。同时有加密、防重放攻击、信道复用的功能。 支持心跳保活、自动重连,重连后会恢复上次连接,在底层掉线的情况下可以保持上层不掉线。同时有加密、防重放攻击、信道复用的功能。
**欢迎任何形式的转载**
[English](/README.md) [English](/README.md)
[udp2raw+kcptun step_by_step教程](kcptun_step_by_step.md) [udp2raw+kcptun step_by_step教程](kcptun_step_by_step.md)
[udp2raw+finalspeed step_by_step教程](finalspeed_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 https://github.com/wangyu-/UDPspeeder
# 支持的平台 # 支持的平台
Linux主机有root权限。可以是PC、android手机/平板、openwrt路由器、树莓派。主机上最好安装了iptables命令(apt/yum很容易安装)。 Linux主机有root权限。可以是PC、android手机/平板、openwrt路由器、树莓派。主机上最好安装了iptables命令(apt/yum很容易安装)。
windows和mac上预装了udp2raw的虚拟机镜像已发布可以用Vmware或VirtualBox加载容量4.4mb已经配置好了自动获取网卡ip开机即用稳定性能很好 对于windows和mac用户在虚拟机中可以稳定使用udp2raw跑在Linux里其他应用照常跑在window里确保虚拟机网卡工作在桥接模式。可以使用[这个](https://github.com/wangyu-/udp2raw-tunnel/releases/download/20171108.0/lede-17.01.2-x86_virtual_machine_image.zip)虚拟机镜像大小只有7.5mb免去在虚拟机里装系统的麻烦虚拟机自带ssh server可以scp拷贝文件可以ssh进去可以复制粘贴root密码123456
udp2raw跑在虚拟机里其他应用照常跑在windows上确保虚拟机网卡工作在桥接模式Vmware player 75mb,VirtualBox 118mb,很容易安装)。
# 功能特性 # 功能特性
### 把udp流量伪装成tcp /icmp ### 把udp流量伪装成tcp /icmp

63
fd_manager.cpp Normal file
View 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
View 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

Binary file not shown.

After

Width:  |  Height:  |  Size: 44 KiB

View File

@@ -9,6 +9,10 @@
int mtu_warn=1375;//if a packet larger than mtu warn is receviced,there will be a warning int mtu_warn=1375;//if a packet larger than mtu warn is receviced,there will be a warning
char hb_buf[buf_len];
int server_on_raw_recv_pre_ready(conn_info_t &conn_info,char * ip_port,u32_t tmp_oppsite_const_id); int server_on_raw_recv_pre_ready(conn_info_t &conn_info,char * ip_port,u32_t tmp_oppsite_const_id);
int server_on_raw_recv_ready(conn_info_t &conn_info,char * ip_port,char type,char *data,int data_len); int server_on_raw_recv_ready(conn_info_t &conn_info,char * ip_port,char type,char *data,int data_len);
int server_on_raw_recv_handshake1(conn_info_t &conn_info,char * ip_port,char * data, int data_len); int server_on_raw_recv_handshake1(conn_info_t &conn_info,char * ip_port,char * data, int data_len);
@@ -27,8 +31,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); 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) if(conn_info.state.client_current_state==client_idle)
{ {
fail_time_counter++; fail_time_counter++;
@@ -241,8 +243,10 @@ int client_on_timer(conn_info_t &conn_info) //for client. called when a timer is
mylog(log_debug,"heartbeat sent <%x,%x>\n",conn_info.oppsite_id,conn_info.my_id); mylog(log_debug,"heartbeat sent <%x,%x>\n",conn_info.oppsite_id,conn_info.my_id);
send_safer(conn_info,'h',"",0);/////////////send if(hb_mode==0)
send_safer(conn_info,'h',hb_buf,0);/////////////send
else
send_safer(conn_info,'h',hb_buf,hb_len);
conn_info.last_hb_sent_time=get_current_time(); conn_info.last_hb_sent_time=get_current_time();
return 0; return 0;
} }
@@ -284,8 +288,10 @@ int server_on_timer_multi(conn_info_t &conn_info,char * ip_port) //for server.
return 0; return 0;
} }
send_safer(conn_info,'h',"",0); /////////////send if(hb_mode==0)
send_safer(conn_info,'h',hb_buf,0); /////////////send
else
send_safer(conn_info,'h',hb_buf,hb_len);
conn_info.last_hb_sent_time=get_current_time(); conn_info.last_hb_sent_time=get_current_time();
mylog(log_debug,"heart beat sent<%x,%x>\n",conn_info.my_id,conn_info.oppsite_id); mylog(log_debug,"heart beat sent<%x,%x>\n",conn_info.my_id,conn_info.oppsite_id);
@@ -431,9 +437,9 @@ int client_on_raw_recv(conn_info_t &conn_info) //called when raw fd received a p
conn_info.last_oppsite_roller_time=conn_info.last_hb_recv_time; conn_info.last_oppsite_roller_time=conn_info.last_hb_recv_time;
client_on_timer(conn_info); client_on_timer(conn_info);
} }
if(data_len==0&&type=='h') if(data_len>=0&&type=='h')
{ {
mylog(log_debug,"[hb]heart beat received\n"); mylog(log_debug,"[hb]heart beat received,oppsite_roller=%d\n",int(conn_info.oppsite_roller));
conn_info.last_hb_recv_time=get_current_time(); conn_info.last_hb_recv_time=get_current_time();
return 0; return 0;
} }
@@ -441,7 +447,8 @@ int client_on_raw_recv(conn_info_t &conn_info) //called when raw fd received a p
{ {
mylog(log_trace,"received a data from fake tcp,len:%d\n",data_len); mylog(log_trace,"received a data from fake tcp,len:%d\n",data_len);
conn_info.last_hb_recv_time=get_current_time(); if(hb_mode==0)
conn_info.last_hb_recv_time=get_current_time();
//u32_t tmp_conv_id= ntohl(* ((u32_t *)&data[0])); //u32_t tmp_conv_id= ntohl(* ((u32_t *)&data[0]));
u32_t tmp_conv_id; u32_t tmp_conv_id;
@@ -603,8 +610,8 @@ 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_t &conn_info=conn_manager.find_insert(ip,port);
conn_info.raw_info=tmp_raw_info; conn_info.raw_info=tmp_raw_info;
conn_info.ip_port.ip=ip; //conn_info.ip_port.ip=ip;
conn_info.ip_port.port=port; //conn_info.ip_port.port=port;
packet_info_t &send_info=conn_info.raw_info.send_info; packet_info_t &send_info=conn_info.raw_info.send_info;
packet_info_t &recv_info=conn_info.raw_info.recv_info; packet_info_t &recv_info=conn_info.raw_info.recv_info;
@@ -775,7 +782,7 @@ int server_on_raw_recv_ready(conn_info_t &conn_info,char * ip_port,char type,cha
return 0; return 0;
}*/ }*/
if (type == 'h' && data_len == 0) { if (type == 'h' && data_len >= 0) {
//u32_t tmp = ntohl(*((u32_t *) &data[sizeof(u32_t)])); //u32_t tmp = ntohl(*((u32_t *) &data[sizeof(u32_t)]));
mylog(log_debug,"[%s][hb]received hb \n",ip_port); mylog(log_debug,"[%s][hb]received hb \n",ip_port);
conn_info.last_hb_recv_time = get_current_time(); conn_info.last_hb_recv_time = get_current_time();
@@ -789,7 +796,8 @@ int server_on_raw_recv_ready(conn_info_t &conn_info,char * ip_port,char type,cha
tmp_conv_id=ntohl(tmp_conv_id); tmp_conv_id=ntohl(tmp_conv_id);
conn_info.last_hb_recv_time = get_current_time(); if(hb_mode==0)
conn_info.last_hb_recv_time = get_current_time();
mylog(log_trace, "conv:%u\n", tmp_conv_id); mylog(log_trace, "conv:%u\n", tmp_conv_id);
if (!conn_info.blob->conv_manager.is_conv_used(tmp_conv_id)) { if (!conn_info.blob->conv_manager.is_conv_used(tmp_conv_id)) {
@@ -826,6 +834,7 @@ int server_on_raw_recv_ready(conn_info_t &conn_info,char * ip_port,char type,cha
struct epoll_event ev; struct epoll_event ev;
fd64_t new_udp_fd64 = fd_manager.create(new_udp_fd); 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); mylog(log_trace, "[%s]u64: %lld\n",ip_port, new_udp_fd64);
ev.events = EPOLLIN; ev.events = EPOLLIN;
@@ -842,7 +851,7 @@ int server_on_raw_recv_ready(conn_info_t &conn_info,char * ip_port,char type,cha
conn_info.blob->conv_manager.insert_conv(tmp_conv_id, new_udp_fd64); conn_info.blob->conv_manager.insert_conv(tmp_conv_id, new_udp_fd64);
fd_manager.get_info(new_udp_fd64).ip_port=conn_info.ip_port;
//assert(conn_manager.udp_fd_mp.find(new_udp_fd)==conn_manager.udp_fd_mp.end()); //assert(conn_manager.udp_fd_mp.find(new_udp_fd)==conn_manager.udp_fd_mp.end());
@@ -857,11 +866,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); 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); 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), int ret = send(fd, data + sizeof(u32_t),
@@ -914,21 +923,23 @@ int server_on_raw_recv_pre_ready(conn_info_t &conn_info,char * ip_port,u32_t tmp
//my_id=conn_info.my_id; //my_id=conn_info.my_id;
//oppsite_id=conn_info.oppsite_id; //oppsite_id=conn_info.oppsite_id;
conn_info.last_hb_recv_time = get_current_time(); conn_info.last_hb_recv_time = get_current_time();
conn_info.last_hb_sent_time = conn_info.last_hb_recv_time;//=get_current_time() conn_info.last_hb_sent_time = conn_info.last_hb_recv_time;//=get_current_time()
send_safer(conn_info, 'h',"", 0); /////////////send if(hb_mode==0)
send_safer(conn_info,'h',hb_buf,0);/////////////send
else
send_safer(conn_info,'h',hb_buf,hb_len);
mylog(log_info, "[%s]changed state to server_ready\n",ip_port); mylog(log_info, "[%s]changed state to server_ready\n",ip_port);
conn_info.blob->anti_replay.re_init(); conn_info.blob->anti_replay.re_init();
//g_conn_info=conn_info; //g_conn_info=conn_info;
int new_timer_fd; int new_timer_fd;
set_timer_server(epollfd, new_timer_fd); set_timer_server(epollfd, new_timer_fd,conn_info.timer_fd64);
conn_info.timer_fd64=fd_manager.create(new_timer_fd);
fd_manager.get_info(conn_info.timer_fd64).ip_port=conn_info.ip_port; 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()); //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); //conn_manager.timer_fd_mp[new_timer_fd] = &conn_info;//pack_u64(ip,port);
@@ -979,8 +990,14 @@ int server_on_raw_recv_pre_ready(conn_info_t &conn_info,char * ip_port,u32_t tmp
//ori_conn_info.state.server_current_state=server_ready; //ori_conn_info.state.server_current_state=server_ready;
ori_conn_info.recover(conn_info); ori_conn_info.recover(conn_info);
send_safer(ori_conn_info, 'h',"", 0); //send_safer(ori_conn_info, 'h',hb_buf, hb_len);
//ori_conn_info.blob->anti_replay.re_init(); //ori_conn_info.blob->anti_replay.re_init();
if(hb_mode==0)
send_safer(ori_conn_info,'h',hb_buf,0);/////////////send
else
send_safer(ori_conn_info,'h',hb_buf,hb_len);
ori_conn_info.last_hb_recv_time=get_current_time();
@@ -1484,7 +1501,7 @@ int server_event_loop()
fd64_t fd64=events[idx].data.u64; fd64_t fd64=events[idx].data.u64;
if(fd_manager.exist(fd64)) if(!fd_manager.exist(fd64))
{ {
mylog(log_trace ,"fd64 no longer exist\n"); mylog(log_trace ,"fd64 no longer exist\n");
continue; continue;
@@ -1492,23 +1509,21 @@ int server_event_loop()
assert(fd_manager.exist_info(fd64)); assert(fd_manager.exist_info(fd64));
ip_port_t ip_port=fd_manager.get_info(fd64).ip_port; conn_info_t* p_conn_info=fd_manager.get_info(fd64).p_conn_info;
u32_t ip=ip_port.ip; u32_t ip=p_conn_info->raw_info.send_info.dst_ip;
u32_t port=ip_port.port; 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);
//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(fd64==p_conn_info->timer_fd64)//////////timer_fd64
{ {
if(debug_flag)begin_time=get_current_time(); 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; u64_t dummy;
read(fd, &dummy, 8); read(fd, &dummy, 8);

View File

@@ -15,7 +15,7 @@ SOURCES_AES_ACC=$(COMMON) $(wildcard lib/aes_acc/aes*.c)
NAME=udp2raw 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 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 all:git_version
rm -f ${NAME} rm -f ${NAME}
@@ -69,6 +69,7 @@ cross3:git_version
${cc_cross} -o ${NAME}_cross -I. ${SOURCES} ${FLAGS} -lrt -static -O3 ${cc_cross} -o ${NAME}_cross -I. ${SOURCES} ${FLAGS} -lrt -static -O3
release: ${TARGETS} release: ${TARGETS}
cp git_version.h version.txt
tar -zcvf ${TAR} tar -zcvf ${TAR}
clean: clean:

View File

@@ -13,6 +13,9 @@
#include "fd_manager.h" #include "fd_manager.h"
int hb_mode=1;
int hb_len=1200;
fd_manager_t fd_manager; 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 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
@@ -149,6 +152,7 @@ void print_help()
printf(" the parameter automatically,specify it manually if 'auto' failed\n"); printf(" the parameter automatically,specify it manually if 'auto' failed\n");
printf(" --gen-add generate iptables rule and add it permanently,then exit.overrides -g\n"); printf(" --gen-add generate iptables rule and add it permanently,then exit.overrides -g\n");
printf(" --keep-rule monitor iptables and auto re-add if necessary.implys -a\n"); printf(" --keep-rule monitor iptables and auto re-add if necessary.implys -a\n");
printf(" --hb-len <number> length of heart-beat packet\n");
printf(" --clear clear any iptables rules added by this program.overrides everything\n"); printf(" --clear clear any iptables rules added by this program.overrides everything\n");
printf(" -h,--help print this help message\n"); printf(" -h,--help print this help message\n");
@@ -247,6 +251,8 @@ void process_arg(int argc, char *argv[]) //process all options
{"force-sock-buf", no_argument, 0, 1}, {"force-sock-buf", no_argument, 0, 1},
{"random-drop", required_argument, 0, 1}, {"random-drop", required_argument, 0, 1},
{"fifo", required_argument, 0, 1}, {"fifo", required_argument, 0, 1},
{"hb-mode", required_argument, 0, 1},
{"hb-len", required_argument, 0, 1},
{NULL, 0, 0, 0} {NULL, 0, 0, 0}
}; };
@@ -589,6 +595,18 @@ void process_arg(int argc, char *argv[]) //process all options
{ {
mylog(log_info,"configuration loaded from %s\n",optarg); mylog(log_info,"configuration loaded from %s\n",optarg);
} }
else if(strcmp(long_options[option_index].name,"hb-mode")==0)
{
sscanf(optarg,"%d",&hb_mode);
assert(hb_mode==0||hb_mode==1);
mylog(log_info,"hb_mode =%d \n",hb_mode);
}
else if(strcmp(long_options[option_index].name,"hb-len")==0)
{
sscanf(optarg,"%d",&hb_len);
assert(hb_len>=0&&hb_len<=1500);
mylog(log_info,"hb_len =%d \n",hb_len);
}
else else
{ {
@@ -951,7 +969,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; int ret;
epoll_event ev; epoll_event ev;
@@ -969,9 +987,11 @@ int set_timer_server(int epollfd,int &timer_fd)//only for server
its.it_value.tv_nsec=1; //imidiately its.it_value.tv_nsec=1; //imidiately
timerfd_settime(timer_fd,0,&its,0); timerfd_settime(timer_fd,0,&its,0);
fd64=fd_manager.create(timer_fd);
ev.events = EPOLLIN; 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); ret=epoll_ctl(epollfd, EPOLL_CTL_ADD, timer_fd, &ev);
if (ret < 0) { if (ret < 0) {

15
misc.h
View File

@@ -13,7 +13,8 @@
#include "log.h" #include "log.h"
#include "network.h" #include "network.h"
extern int hb_mode;
extern int hb_len;
const u32_t max_handshake_conn_num=10000; const u32_t max_handshake_conn_num=10000;
const u32_t max_ready_conn_num=1000; const u32_t max_ready_conn_num=1000;
@@ -25,18 +26,18 @@ const u32_t client_retry_interval=1000;//ms
const u32_t server_handshake_timeout=client_handshake_timeout+5000;// this should be longer than clients. client retry initially ,server retry passtively const u32_t server_handshake_timeout=client_handshake_timeout+5000;// this should be longer than clients. client retry initially ,server retry passtively
const int conv_clear_ratio=10; //conv grabage collecter check 1/10 of all conv one time const int conv_clear_ratio=30; //conv grabage collecter check 1/30 of all conv one time
const int conn_clear_ratio=30; const int conn_clear_ratio=50;
const int conv_clear_min=1; const int conv_clear_min=1;
const int conn_clear_min=1; const int conn_clear_min=1;
const u32_t conv_clear_interval=3000;//ms const u32_t conv_clear_interval=1000;//ms
const u32_t conn_clear_interval=3000;//ms const u32_t conn_clear_interval=1000;//ms
const i32_t max_fail_time=0;//disable const i32_t max_fail_time=0;//disable
const u32_t heartbeat_interval=1000;//ms const u32_t heartbeat_interval=600;//ms
const u32_t timer_interval=400;//ms. this should be smaller than heartbeat_interval and retry interval; const u32_t timer_interval=400;//ms. this should be smaller than heartbeat_interval and retry interval;
@@ -112,7 +113,7 @@ void iptables_rule();
void pre_process_arg(int argc, char *argv[]);//mainly for load conf file; void pre_process_arg(int argc, char *argv[]);//mainly for load conf file;
int unit_test(); int unit_test();
int set_timer(int epollfd,int &timer_fd); 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 handle_lower_level(raw_info_t &raw_info);
int add_iptables_rule(const char *); int add_iptables_rule(const char *);

View File

@@ -1820,17 +1820,17 @@ int get_src_adress(u32_t &ip,u32_t remote_ip_uint32,int remote_port) //a trick
return 0; 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) 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) 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) 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_port = htons(port);
temp_bind_addr.sin_addr.s_addr = local_ip_uint32; 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"); mylog(log_debug,"bind fail\n");
return -1; 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(raw_mode==mode_faketcp)
{ {
if (listen(bind_fd, SOMAXCONN) != 0) { if (listen(fd, SOMAXCONN) != 0) {
mylog(log_warn,"listen fail\n"); mylog(log_warn,"listen fail\n");
return -1; return -1;
} }
} }
return 0; 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); 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 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; return raw_send_port;
} }

View File

@@ -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 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); int send_raw_ip(raw_info_t &raw_info,const char * payload,int payloadlen);