Răsfoiți Sursa

fixed merge conflict

wangyu- 8 ani în urmă
părinte
comite
f64108867c
11 a modificat fișierele cu 137 adăugiri și 83 ștergeri
  1. 13 12
      README.md
  2. 1 1
      common.cpp
  3. 2 2
      common.h
  4. 5 1
      delay_manager.cpp
  5. 53 27
      doc/README.zh-cn.md
  6. 2 2
      doc/README.zh-cn.v1.md
  7. 4 4
      doc/udpspeeder_openvpn.md
  8. 4 0
      lib/rs.c
  9. 1 1
      main.cpp
  10. 30 30
      misc.cpp
  11. 22 3
      tun_dev.cpp

+ 13 - 12
README.md

@@ -1,7 +1,8 @@
 # UDPspeeder
 Network Speed-Up Tool. Boost your Connection on a High Lantency High Packet-Loss Link by using Forward Error Correction.
 
-When used alone,UDPspeeder speeds-up only UDP connection.Nevertheless,if you used UDPspeeder + any UDP-based VPN together,you can speed-up any traffic(include TCP/UDP/ICMP),currently OpenVPN/L2TP/ShadowVPN are confirmed to be supported。
+When used alone, UDPspeeder speeds-up only UDP connection. Nevertheless, if you used UDPspeeder + any UDP-based VPN together,
+you can speed-up any traffic(include TCP/UDP/ICMP), currently OpenVPN/L2TP/ShadowVPN are confirmed to be supported。
 
 ![](/images/en/udpspeeder.PNG)
 
@@ -26,13 +27,13 @@ tested on a link with 100ms latency and 10% packet loss at both direction
 ![](/images/en/scp_compare2.PNG)
 
 # Supported Platforms
-Linux host (including desktop Linux,Android phone/tablet,OpenWRT router,or Raspberry PI).
+Linux host (including desktop Linux,Android phone/tablet, OpenWRT router, or Raspberry PI).
 
 For Windows and MacOS You can run UDPspeeder inside [this](https://github.com/wangyu-/udp2raw-tunnel/releases/download/20170918.0/lede-17.01.2-x86_virtual_machine_image_with_udp2raw_pre_installed.zip) 7.5mb virtual machine image.
 
 # How does it work
 
-UDPspeeder uses FEC(Forward Error Correction) to reduce packet loss rate,at the cost of addtional bandwidth.The algorithm for FEC is called Reed-Solomon.
+UDPspeeder uses FEC(Forward Error Correction) to reduce packet loss rate, at the cost of addtional bandwidth. The algorithm for FEC is called Reed-Solomon.
 
 ![image0](/images/en/fec.PNG)
 
@@ -66,7 +67,7 @@ Assume your server ip is 44.55.66.77, you have a service listening on udp port 7
 ./speederv2 -c -l0.0.0.0:3333  -r44.55.66.77:4096 -f20:10 -k "passwd"
 ```
 
-Now connecting to UDP port 3333 at the client side is equivalent to connecting to port 7777 at the server side,and the connection is boosted by UDPspeeder.
+Now connecting to UDP port 3333 at the client side is equivalent to connecting to port 7777 at the server side, and the connection is boosted by UDPspeeder.
 
 ##### Note
 
@@ -74,7 +75,7 @@ Now connecting to UDP port 3333 at the client side is equivalent to connecting t
 
 `-k` enables simple XOR encryption to confuse DPI(Deep Packet Inspection)
 
-To run stably,pay attention to MTU.
+To run stably, pay attention to MTU.
 
 # Advanced Topic
 ### Full Options
@@ -125,14 +126,14 @@ log and help options:
 
 ```
 #### `--fifo` option
-Use a fifo(named pipe) for sending commands to the running program. For example `--fifo fifo.file`,you can use following commands to change parameters dynamically:
+Use a fifo(named pipe) for sending commands to the running program. For example `--fifo fifo.file`, you can use following commands to change parameters dynamically:
 ```
-echo fec 19:9 >fifo.file
-echo mtu 1100 >fifo.file
-echo timeout 5 >fifo.file
-echo queue-len 100 >fifo.file
-echo mode 0 >fifo.file
+echo fec 19:9 > fifo.file
+echo mtu 1100 > fifo.file
+echo timeout 5 > fifo.file
+echo queue-len 100 > fifo.file
+echo mode 0 > fifo.file
 ```
-### Speed-Up any traffic with OpenVPN+UDPspeeder
+### Speed-Up any traffic with OpenVPN + UDPspeeder
 
 Check [UDPspeeder + openvpn config guide](/doc/udpspeeder_openvpn.md).

+ 1 - 1
common.cpp

@@ -22,7 +22,7 @@ char iptables_rule[200]="";
 
 program_mode_t client_or_server=unset_mode;//0 unset; 1client 2server
 
-working_mode_t working_mode=unset_mode;
+working_mode_t working_mode=unset_working_mode;
 
 struct random_fd_t
 {

+ 2 - 2
common.h

@@ -44,7 +44,7 @@
 #include <stdarg.h>
 #include <assert.h>
 #include <linux/if_packet.h>
-
+#include <linux/if_tun.h>
 
 #include<unordered_map>
 #include<unordered_set>
@@ -118,7 +118,7 @@ enum program_mode_t {unset_mode=0,client_mode,server_mode};
 extern program_mode_t client_or_server;
 extern unordered_map<int, const char*> raw_mode_tostring ;
 
-enum working_mode_t {unset_mode=0,tunnel_mode,tun_dev_mode};
+enum working_mode_t {unset_working_mode=0,tunnel_mode,tun_dev_mode};
 extern working_mode_t working_mode;
 
 extern int delay_capacity;

+ 5 - 1
delay_manager.cpp

@@ -66,7 +66,11 @@ int delay_manager_t::add(my_time_t delay,const dest_t &dest,char *data,int len)
 
 	delay_data_t tmp=delay_data;
 	tmp.data=(char *)malloc(delay_data.len+100);
-
+    if(!tmp.data)
+    {
+        mylog(log_warn, "malloc() returned null in delay_manager_t::add()");
+        return -1;
+    }
 	memcpy(tmp.data,data,delay_data.len);
 
 	my_time_t tmp_time=get_current_time_us();

+ 53 - 27
doc/README.zh-cn.md

@@ -17,7 +17,7 @@
 
 ![image0](/images/cn/scp_compare.PNG)
 #### 原理简介
-主要原理是通过冗余数据来对抗网络的丢包,发送冗余数据的方式支持FEC(Forward Error Correction)和多倍发包,其中FEC算法是Reed-Solomon。
+主要原理是通过冗余数据来对抗网络的丢包,发送冗余数据的方式支持FEC(Forward Error Correction)和多倍发包其中FEC算法是Reed-Solomon。
 
 FEC方式的原理图:
 
@@ -28,7 +28,7 @@ FEC方式的原理图:
 
 在多个冗余包之间引入延迟(时间可配)来对抗突发性的丢包,避开中间路由器因为瞬时buffer长度过长而连续丢掉所有副本。
 
-模拟一定的延迟抖动(时间可配),这样上层应用计算出来的RTT方差会更大,以等待后续冗余包的到达,不至于发生在冗余包到达之前就触发重传的尴尬。
+模拟一定的延迟抖动(时间可配)这样上层应用计算出来的RTT方差会更大,以等待后续冗余包的到达,不至于发生在冗余包到达之前就触发重传的尴尬。
 
 输出UDP收发情况报告,可以看出丢包率。
 
@@ -39,7 +39,7 @@ client支持多个udp连接,server也支持多个client
 # 简明操作说明
 
 ### 环境要求
-Linux主机,可以是桌面版,可以是android手机/平板,可以是openwrt路由器,也可以是树莓派。在windows和mac上配合虚拟机可以稳定使用(speeder跑在Linux里,其他应用照常跑在window里,桥接模式测试可用),可以使用[这个](https://github.com/wangyu-/udp2raw-tunnel/releases/download/20170918.0/lede-17.01.2-x86_virtual_machine_image_with_udp2raw_pre_installed.zip)虚拟机镜像,大小只有7.5mb,免去在虚拟机里装系统的麻烦。
+Linux主机,可以是桌面版,可以是android手机/平板,可以是openwrt路由器,也可以是树莓派。在windows和mac上配合虚拟机可以稳定使用(speeder跑在Linux里,其他应用照常跑在window里,桥接模式测试可用),可以使用[这个](https://github.com/wangyu-/udp2raw-tunnel/releases/download/20170918.0/lede-17.01.2-x86_virtual_machine_image_with_udp2raw_pre_installed.zip)虚拟机镜像,大小只有7.5mb,免去在虚拟机里装系统的麻烦;虚拟机自带ssh server,可以scp拷贝文件,可以ssh进去,可以复制粘贴,root密码123456
 
 android版需要通过terminal运行。
 
@@ -65,9 +65,10 @@ https://github.com/wangyu-/UDPspeeder/releases
 
 ###### 备注:
 
--f20:10 表示对每20个原始数据发送10个冗余包。
+`-f20:10` 表示对每20个原始数据发送10个冗余包。`-f20:10` 和`-f 20:10`都是可以的,空格可以省略,对于所有的单字节option都是如此。对于双字节option,例如后面的`--mode 0`和`--mtu 1200`,空格不可以省略。
+
+`-k` 指定一个字符串,server/client间所有收发的包都会被异或,改变协议特征,防止UDPspeeder的协议被运营商针对。
 
--k 指定一个字符串,server/client间所有收发的包都会被异或,改变协议特征,防止UDPspeeder的协议被运营商针对。
 
 ###### 注意
 
@@ -131,7 +132,7 @@ log and help options:
 指定fec编码器在编码时候最多可以引入多大的延迟。越高fec越有效率,加速游戏时调低可以降低延迟。
 
 #####  `--mode` 选项 和 `--mtu`选项
-fec编码器的工作模式。对于mode 0,编码器会积攒一定数量的packet,然后把他们合并再切成等长的片段(切分长度由--mtu指定)。对于mode 1,编码器不会做任何切分,而是会把packet按最大长度对齐,fec冗余包的长度为对齐后的长度(最大长度)。
+fec编码器的工作模式。对于mode 0,编码器会积攒一定数量的packet,然后把他们合并再切成等长的片段(切分长度由--mtu指定)。对于mode 1,编码器不会做任何切分而是会把packet按最大长度对齐,fec冗余包的长度为对齐后的长度(最大长度)。
 
 mode 0更省流量,在丢包率正常的情况下效果和mode 1是一样的;mode 1延迟更低,在极高丢包的情况下表现更好。
 
@@ -141,17 +142,22 @@ mode 0模式的流量消耗基本完全透明。mode 1因为涉及到数据按
 
 mode 0模式数据包一般不会乱序,除非网络本身有严重乱序;mode 1模式被恢复的数据包可能会乱序,不过UDP本来就允许乱序,对绝大多数应用没有影响。mode 0模式反而可以纠正一些乱序情况。
 
+mode 0模式允许你发送的数据包大小超过物理接口的MTU而几乎不引起性能损失(而普通的ip分片做不到这点),目前最高支持到2000字节,2000字节已经可以应对任何应用了,因为一般网络的MTU只有1400多。之所以支持到2000字节是为了省程序内部开的静态buff(静态buff避免malloc提高性能),如果你是开发者,通过重新编译,支持到UDP协议的极限(
+65507字节)也没问题。
+
 ##### `--report`  选项
 数据发送和接受报告。开启后可以根据此数据推测出包速和丢包率等特征。
 
 ##### `-i` 选项
-指定一个时间窗口,长度为n毫秒。同一个fec分组的数据在发送时候会被均匀分散到这n毫秒中。可以对抗突发性的丢包。默认值是0,因为这个功能需要用到时钟,在某些虚拟机里时钟不稳定,可能会导致个别包出现非常大的延迟,所以默认关掉了。这个功能很有用,默认参数效果不理想时可以尝试打开。
+指定一个时间窗口,长度为n毫秒。同一个fec分组的数据在发送时候会被均匀分散到这n毫秒中。可以对抗突发性的丢包。默认值是0,因为这个功能需要用到时钟,在某些虚拟机里时钟不稳定,可能会导致个别包出现非常大的延迟,所以默认关掉了。这个功能很有用,默认参数效果不理想时可以尝试打开。这个选项的跟通信原理上常说的`交错fec` `交织fec`的原理是差不多的。
 
 ##### `-j` 选项
 为原始数据的发送,增加一个延迟抖动值。这样上层应用计算出来的RTT方差会更大,以等待后续冗余包的到达,不至于发生在冗余包到达之前就触发重传的尴尬。配合-t选项使用。正常情况下跨国网络本身的延迟抖动就很大,可以不用设-j。这个功能也需要时钟,默认关掉了,不过一般情况应该不需要这个功能。
 
+-j选项不但可以模拟延迟抖动,也可以模拟延迟。
+
 ##### `--random-drop` 选项
-随机丢包。模拟恶劣的网络环境时使用。如果你的网络现在没有多大丢包,但是你想测试一下高丢包环境下各种FEC参数的表现,可以开这个选项。
+随机丢包。模拟高丢包的网络环境时使用。 `--random-drop`和`-j`选项一起用,可以模拟高延迟(或者高延迟抖动)高丢包的网络,可用于测试FEC参数在各种网络环境下的表现
 
 ##### `-k`选项
 指定一个字符串,server/client间所有收发的包都会被异或,改变协议特征,防止UDPspeeder的协议被运营商针对。
@@ -162,21 +168,21 @@ UDPspeeder默认情况下会对每个发出的数据包随机填充和异或一
 ##### `-q,--queue-len`
 编码器在做FEC前最多积攒多少个数据包,只对mode 0有效。除非是使用下文`V2版如何多倍发包`里面提到的用法,不建议改动。
 #### `--fifo` option
-用fifo(命名管道)向运行中的程序发送command. 例如`--fifo fifo.file`,可用的command有:
+用fifo(命名管道)向运行中的程序发送command。例如`--fifo fifo.file`,可用的command有:
 ```
-echo fec 19:9 >fifo.file
-echo mtu 1100 >fifo.file
-echo timeout 5 >fifo.file
-echo queue-len 100 >fifo.file
-echo mode 0 >fifo.file
+echo fec 19:9 > fifo.file
+echo mtu 1100 > fifo.file
+echo timeout 5 > fifo.file
+echo queue-len 100 > fifo.file
+echo mode 0 > fifo.file
 ```
-可以动态改变fec编码器参数。
+可以动态改变fec编码器参数。可以从程序的log里看到command是否发送成功。
 
 # 使用经验
 
 ### 在FEC和多倍发包之间如何选择
 
-对于游戏,游戏的流量本身不大,延迟很重要,多倍发包是最佳解决方案,多倍发包不会引入额外的延迟。FEC编码器需要先积攒一些数据,才可以做FEC,延迟无法避免;对于多倍发包,没有这个问题,所以没有延迟。
+对于游戏,游戏的流量本身不大,延迟很重要,多倍发包是最佳解决方案,多倍发包不会引入额外的延迟。FEC编码器需要先积攒一些数据,才可以做FEC延迟无法避免;对于多倍发包,没有这个问题,所以没有延迟。
 
 对于其他日常应用(延迟要求一般),在合理配置的情况下,FEC的效果肯定好过多倍发包。不过需要根据网络的最大丢包来配置FEC参数,才能有稳定的效果。如果配置不当,对于--mode 1可能会完全没有效果;对于--mode 0,可能效果会比不用UDPspeeder还差。
 
@@ -184,7 +190,7 @@ echo mode 0 >fifo.file
 
 ### V2版如何多倍发包
 
-只要在设置-f参数时把x设置为1,fec算法就退化为多倍发包了。例如-f1:1,表示2倍发包,-f1:2表示3倍发包,以此类推。另外可以加上`--mode 0 -q1`参数,防止fec编码器试图积攒和合并数据,获得最低的延迟。
+只要在设置-f参数时把x设置为1,fec算法就退化为多倍发包了。例如-f1:1表示2倍发包,-f1:2表示3倍发包,以此类推。另外可以加上`--mode 0 -q1`参数,防止fec编码器试图积攒和合并数据,获得最低的延迟。
 
 2倍发包的完整参数:
 
@@ -209,15 +215,15 @@ echo mode 0 >fifo.file
 
 ### 根据CPU处理能力来调整FEC参数
 
-FEC算法很吃CPU,初次使用建议关注UDPspeeder的CPU占用。如果CPU被打满,可以在冗余度不变的情况下把FEC分组大小调小,否则的话效果可能很差。
+FEC算法很吃CPU初次使用建议关注UDPspeeder的CPU占用。如果CPU被打满,可以在冗余度不变的情况下把FEC分组大小调小,否则的话效果可能很差。
 
-比如-f20:10和-f10:5,都是1.5倍的冗余度,而-f20:10的FEC分组大小是30个包,-f10:5的FEC分组大小是15个包。-f20:10更费CPU,但是在一般情况下效果更稳定。把分组调小可以节省CPU。
+比如-f20:10和-f10:5,都是1.5倍的冗余度,而-f20:10的FEC分组大小是30个包,-f10:5的FEC分组大小是15个包。-f20:10更费CPU但是在一般情况下效果更稳定。把分组调小可以节省CPU。
 
-另外,fec分组大小不宜过大,否则不但很耗CPU,还有其他副作用,建议x+y<50。
+另外,fec分组大小不宜过大,否则不但很耗CPU还有其他副作用,建议x+y<50。
 
 ### 改变FEC参数而不断线
 
-`--fifo`选项可以在运行时无缝改变FEC参数,无需重启程序,也不会断线。
+`--fifo`选项可以在运行时改变FEC参数,无需重启程序,也不会断线。如果你在使用过程中发现网络丢包突然变高,可以动态地把冗余度调大;反之也一样,如果网络变好了,把冗余度调小节省流量。一切都是无缝进行,不会断线,也不会因为改FEC参数导致额外的丢包。
 
 ### 为什么使用之后效果反而变差了?
 
@@ -234,19 +240,19 @@ UDPspeeder和BBR/锐速可以配合使用,UDPspeeder工作在IP层负责降低
 
 ### UDPspeeder和Kcptun配合
 
-UDPspeeder和Kcptun配合,UDPspeeder和Kcptun可以并联也可以串联。
+UDPspeeder和Kcptun配合UDPspeeder和Kcptun可以并联也可以串联。
 
-并联的情况下,让kcptun负责加速TCP,UDPspeeder负责加速UDP。见下文的`UDPspeeder + kcptun + $*** 同时加速tcp和udp流量`。
+并联的情况下,让kcptun负责加速TCPUDPspeeder负责加速UDP。见下文的`UDPspeeder + kcptun + $*** 同时加速tcp和udp流量`。
 
 串联的情况。UDPspeeder的FEC跟Kcptun自带的相比:可以对两个方向设置不同的FEC参数、有一个更省流量的mode 0模式、可以动态改变FEC参数;但是UDPspeeder本身不优化拥塞和重传算法。所以UDPspeeder和Kcptun也可以配合使用,结合两者的优点。
 
-串联时可以关掉Kcptun的FEC,让UDPspeeder接管FEC功能。这样UDPspeeder工作在UDP层负责降低丢包率,Kcptun工作在应用层用kcp算法负责优化拥塞和重传,能起到和`UDPspeeder+BBR/锐速`类似的效果。
+串联时可以关掉Kcptun的FEC让UDPspeeder接管FEC功能。这样UDPspeeder工作在UDP层负责降低丢包率,Kcptun工作在应用层用kcp算法负责优化拥塞和重传,能起到和`UDPspeeder+BBR/锐速`类似的效果。
 
 如果发Issue问Kcptun+UDPspeeder相关的问题,一定要说明是并联还是串联。
 
 # 应用
 
-#### UDPspeeder + openvpn加速任何流量
+#### UDPspeeder + OpenVPN加速任何流量,也适用于其他VPN
 ![image0](/images/Capture2.PNG)
 
 具体配置见,[UDPspeeder + openvpn config guide](/doc/udpspeeder_openvpn.md).
@@ -256,10 +262,30 @@ UDPspeeder和Kcptun配合,UDPspeeder和Kcptun可以并联也可以串联。
 需要在服务端开启ipforward和NAT。在客户端改路由表(可以手动修改,也可以由OpenVPN的redirect-gateway选项自动加好)。
 
 #### UDPspeeder + kcptun/finalspeed + $*** 同时加速tcp和udp流量
-如果你需要用加速的tcp看视频和下载文件,这样效果比UDPspeeder+vpn方案更好(在没有BBR的情况下)
+如果你需要用加速的tcp看视频和下载文件,这样效果可能比没有BBR的UDPspeeder+vpn方案更好。另外,如果你需要玩游戏,但是嫌配VPN麻烦,也可以用这种方案
 ![image0](/images/cn/speeder_kcptun.PNG)
 
-#### UDPspeeder + openvpn + $*** 混合方案
+具体配置方法简介:
+
+假设$\*\*\*  server监听在在44.55.66.77的443端口(tcp和udp同时)。用kcptun把tcp 443映射到本地的tcp 1234;用UDPspeeder把udp 443的映射到本地的udp 1234。
+然后让$\*\*\* client 去连127.0.0.1:1234就可以了,tcp和udp都被加速了。完整命令:
+```
+run at server side:
+./kcp_server  -l ":4000" -t "127.0.0.1:443" -mode fast2
+./speederv2 -s -l0.0.0.0:4001 -r127.0.0.1:443  -f20:10 -k "passwd"
+
+run at client side:
+./kcp_client  -l ":1234" -r "44.55.66.77:4000" -mode fast2
+./speederv2 -c -l0.0.0.0:1234 -r44.55.66.77:4001 -f20:10 -k "passwd"
+```
+
+这就是全部的命令了。issue里有很多人困惑于怎么把tcp和udp流量"分开",其实很简单就可以做到。
+
+如果只需要加速UDP,不需要加速TCP,可以把kcptun换成其他的任意端口转发方式,比如ncat/socat/ssh tunnel/iptables。
+
+另外,如果没有kcptun只有BBR/锐速的话,也可以把kcptun换成ncat/socat/ssh tunnel/iptables。这样,TCP流量由锐速/BBR加速,UDP由UDPspeeder加速。
+
+#### UDPspeeder + openvpn + $*** 混合方案,也适用于其他VPN
 也是我正在用的方案。优点是可以随时在vpn和$\*\*\*方案间快速切换。
 实际部署起来比图中看起来的还要简单。不需要改路由表,不需要写iptables规则和开启NAT,需要做的只是用openvpn分配的ip访问$*** server。
 

+ 2 - 2
doc/README.zh-cn.v1.md

@@ -15,7 +15,7 @@ UDP双边加速工具,降低丢包率,配合vpn可以加速任何协议,
 
 每个冗余数据包都是间隔数毫秒(可配置)以后延迟发出的,可以避开中间路由器因为瞬时buffer长度过长而连续丢掉所有副本。
 
-模拟一定的延迟抖动,这样上层应用计算出来的RTT方差会更大,以等待后续冗余包的到达,不至于发生在冗余包到达之前就触发重传的尴尬。
+模拟一定的延迟抖动这样上层应用计算出来的RTT方差会更大,以等待后续冗余包的到达,不至于发生在冗余包到达之前就触发重传的尴尬。
 
 #### 适用场景
 绝大部分流量不高的情况。程序本身加速udp,但是配合openvpn可以加速任何流量。网络状况不好时,游戏卡得没法玩,或者网页卡得没法打开,使用起来效果最好。对于解决语音通话的断断续续效果也不错。不适合大流量的场景,比如BT下载和在线看视频。 无论从自己使用效果的角度,还是从国际出口带宽占用的角度,都建议不要在大流量环境使用。
@@ -134,7 +134,7 @@ other options:
 
 跟openvpn via kcptun方式的对比:
 
-kcptun在udp层有RS code,也是一种冗余传输,通过openvpn把流量转成tcp,再通过kcptun加速是有一定效果的。但是tcp只支持按序到达。按序到达的意思是,如果你发了1 2 3 4 5 6 ,6个包,如果第一个包丢了,那么必须等第一个包重传成功以后 2 3 4 5 6 才能到达;只要有一个包不到,后续数据包就要一直等待。用tcp承载udp流量会破坏udp的实时性。会造成游戏卡顿更严重。
+kcptun在udp层有RS code,也是一种冗余传输,通过openvpn把流量转成tcp,再通过kcptun加速是有一定效果的。但是tcp只支持按序到达。按序到达的意思是,如果你发了1 2 3 4 5 6,6个包,如果第一个包丢了,那么必须等第一个包重传成功以后 2 3 4 5 6 才能到达;只要有一个包不到,后续数据包就要一直等待。用tcp承载udp流量会破坏udp的实时性。会造成游戏卡顿更严重。
 
 udp协议本身是ip协议加上了端口之后的直接封装,udp继承了ip协议的实时/乱序到达特性,更适合中转vpn。
 

+ 4 - 4
doc/udpspeeder_openvpn.md

@@ -39,7 +39,7 @@ mute 20
 
 comp-lzo no
 
-fragment 1200       ##### very important    you can turn it up a bit. but,the lower the safer
+fragment 1200       ##### very important    you can turn it up a bit. but, the lower the safer
 mssfix 1200         ##### very important
 
 sndbuf 2000000      ##### important
@@ -79,7 +79,7 @@ mute 20
 
 comp-lzo no
 
-fragment 1200       ##### very important    you can turn it up a bit. but,the lower the safer
+fragment 1200       ##### very important    you can turn it up a bit. but, the lower the safer
 mssfix 1200         ##### very important
 
 sndbuf 2000000      ##### important
@@ -88,9 +88,9 @@ txqueuelen 4000     ##### suggested
 ```
 
 ##### Note:
-If you use the `redirect-gateway` option of OpenVPN,you may need to add a route exception for your remote server ip at client side.Otherwise OpenVPN may hijack UDPspeeder 's traffic.
+If you use the `redirect-gateway` option of OpenVPN, you may need to add a route exception for your remote server ip at client side.Otherwise OpenVPN may hijack UDPspeeder 's traffic.
 
-For example,depend on your network environment,the command may looks like:
+For example, depend on your network environment, the command may looks like:
 ```
 ip route add 44.55.66.77 via 44.55.66.1
 ```

+ 4 - 0
lib/rs.c

@@ -51,6 +51,10 @@ void* get_code(int k,int n)
 	if (table==0)
 	{
 		table=(void* (*)[256]) malloc(sizeof(void*)*256*256);
+		if(!table)
+		{
+		    return table;
+		}
 		memset(table,0,sizeof(void*)*256*256);
 	}
 	if(table[k][n]==0)

+ 1 - 1
main.cpp

@@ -14,7 +14,7 @@ using namespace std;
 
 int main(int argc, char *argv[])
 {
-	working_mode=tun_dev_mode;
+	working_mode=tunnel_mode;
 
 
 

+ 30 - 30
misc.cpp

@@ -915,51 +915,51 @@ void print_help()
 	strncpy(git_version_buf,gitversion,10);
 
 	printf("UDPspeeder V2\n");
-	printf("git version:%s    ",git_version_buf);
-	printf("build date:%s %s\n",__DATE__,__TIME__);
+	printf("git version: %s    ",git_version_buf);
+	printf("build date: %s %s\n",__DATE__,__TIME__);
 	printf("repository: https://github.com/wangyu-/UDPspeeder\n");
 	printf("\n");
 	printf("usage:\n");
-	printf("    run as client : ./this_program -c -l local_listen_ip:local_port -r server_ip:server_port  [options]\n");
-	printf("    run as server : ./this_program -s -l server_listen_ip:server_port -r remote_ip:remote_port  [options]\n");
+	printf("    run as client: ./this_program -c -l local_listen_ip:local_port -r server_ip:server_port  [options]\n");
+	printf("    run as server: ./this_program -s -l server_listen_ip:server_port -r remote_ip:remote_port  [options]\n");
 	printf("\n");
-	printf("common option,must be same on both sides:\n");
-	printf("    -k,--key              <string>        key for simple xor encryption. if not set,xor is disabled\n");
+	printf("common options, must be same on both sides:\n");
+	printf("    -k,--key              <string>        key for simple xor encryption. if not set, xor is disabled\n");
 
 	printf("main options:\n");
-	printf("    -f,--fec              x:y             forward error correction,send y redundant packets for every x packets\n");
-	printf("    --timeout             <number>        how long could a packet be held in queue before doing fec,unit: ms,default :8ms\n");
-	printf("    --mode                <number>        fec-mode,available values: 0,1 ; 0 cost less bandwidth,1 cost less latency(default)\n");
-	printf("    --report              <number>        turn on send/recv report,and set a period for reporting,unit:s\n");
+	printf("    -f,--fec              x:y             forward error correction, send y redundant packets for every x packets\n");
+	printf("    --timeout             <number>        how long could a packet be held in queue before doing fec, unit: ms, default: 8ms\n");
+	printf("    --mode                <number>        fec-mode,available values: 0, 1; 0 cost less bandwidth, 1 cost less latency(default)\n");
+	printf("    --report              <number>        turn on send/recv report, and set a period for reporting, unit: s\n");
 
 	printf("advanced options:\n");
-	printf("    --mtu                 <number>        mtu. for mode 0,the program will split packet to segment smaller than mtu_value.\n");
-	printf("                                          for mode 1,no packet will be split,the program just check if the mtu is exceed.\n");
-	printf("                                          default value:1250 \n");
-	printf("    -j,--jitter           <number>        simulated jitter.randomly delay first packet for 0~<number> ms,default value:0.\n");
+	printf("    --mtu                 <number>        mtu. for mode 0, the program will split packet to segment smaller than mtu_value.\n");
+	printf("                                          for mode 1, no packet will be split, the program just check if the mtu is exceed.\n");
+	printf("                                          default value: 1250\n");
+	printf("    -j,--jitter           <number>        simulated jitter. randomly delay first packet for 0~<number> ms, default value: 0.\n");
 	printf("                                          do not use if you dont know what it means.\n");
-	printf("    -i,--interval         <number>        scatter each fec group to a interval of <number> ms,to protect burst packet loss.\n");
-	printf("                                          default value:0.do not use if you dont know what it means.\n");
-	printf("    --random-drop         <number>        simulate packet loss ,unit:0.01%%. default value: 0\n");
-	printf("    --disable-obscure     <number>        disable obscure,to save a bit bandwidth and cpu\n");
+	printf("    -i,--interval         <number>        scatter each fec group to a interval of <number> ms, to protect burst packet loss.\n");
+	printf("                                          default value: 0. do not use if you dont know what it means.\n");
+	printf("    --random-drop         <number>        simulate packet loss, unit: 0.01%%. default value: 0\n");
+	printf("    --disable-obscure     <number>        disable obscure, to save a bit bandwidth and cpu\n");
 //	printf("    --disable-xor         <number>        disable xor\n");
 
 	printf("developer options:\n");
-	printf("    --fifo                <string>        use a fifo(named pipe) for sending commands to the running program,so that you\n");
-	printf("                                          can change fec encode parameters dynamically,check readme.md in repository for\n");
+	printf("    --fifo                <string>        use a fifo(named pipe) for sending commands to the running program, so that you\n");
+	printf("                                          can change fec encode parameters dynamically, check readme.md in repository for\n");
 	printf("                                          supported commands.\n");
-	printf("    -j ,--jitter          jmin:jmax       similiar to -j above,but create jitter randomly between jmin and jmax\n");
-	printf("    -i,--interval         imin:imax       similiar to -i above,but scatter randomly between imin and imax\n");
-    printf("    -q,--queue-len        <number>        max fec queue len,only for mode 0\n");
-    printf("    --decode-buf          <number>        size of buffer of fec decoder,unit:packet,default:2000\n");
-    printf("    --fix-latency         <number>        try to stabilize latency,only for mode 0\n");
+	printf("    -j ,--jitter          jmin:jmax       similiar to -j above, but create jitter randomly between jmin and jmax\n");
+	printf("    -i,--interval         imin:imax       similiar to -i above, but scatter randomly between imin and imax\n");
+    printf("    -q,--queue-len        <number>        max fec queue len, only for mode 0\n");
+    printf("    --decode-buf          <number>        size of buffer of fec decoder,u nit: packet, default: 2000\n");
+    printf("    --fix-latency         <number>        try to stabilize latency, only for mode 0\n");
     printf("    --delay-capacity      <number>        max number of delayed packets\n");
-	printf("    --disable-fec         <number>        completely disable fec,turn the program into a normal udp tunnel\n");
-	printf("    --sock-buf            <number>        buf size for socket,>=10 and <=10240,unit:kbyte,default:1024\n");
+	printf("    --disable-fec         <number>        completely disable fec, turn the program into a normal udp tunnel\n");
+	printf("    --sock-buf            <number>        buf size for socket, >=10 and <=10240, unit: kbyte, default: 1024\n");
 	printf("log and help options:\n");
-	printf("    --log-level           <number>        0:never    1:fatal   2:error   3:warn \n");
-	printf("                                          4:info (default)     5:debug   6:trace\n");
-	printf("    --log-position                        enable file name,function name,line number in log\n");
+	printf("    --log-level           <number>        0: never    1: fatal   2: error   3: warn \n");
+	printf("                                          4: info (default)      5: debug   6: trace\n");
+	printf("    --log-position                        enable file name, function name, line number in log\n");
 	printf("    --disable-color                       disable log color\n");
 	printf("    -h,--help                             print this help message\n");
 

+ 22 - 3
tun_dev.cpp

@@ -5,11 +5,30 @@
  *      Author: root
  */
 
-#ifndef TUN_CPP_
-#define TUN_CPP_
 
+#include "common.h"
+#include "log.h"
 
+int get_tun_fd(char * dev_name)
+{
+	int tun_fd=open("/dev/net/tun",O_RDWR);
 
+	if(tun_fd <0)
+	{
+		mylog(log_fatal,"open /dev/net/tun failed");
+		myexit(-1);
+	}
+	struct ifreq ifr;
+	memset(&ifr, 0, sizeof(ifr));
+	ifr.ifr_flags = IFF_TUN|IFF_NO_PI;
 
+	strncpy(ifr.ifr_name, dev_name, IFNAMSIZ);
+
+	if(ioctl(tun_fd, TUNSETIFF, (void *)&ifr) != 0)
+	{
+		mylog(log_fatal,"open /dev/net/tun failed");
+		myexit(-1);
+	}
+	return tun_fd;
+}
 
-#endif /* TUN_CPP_ */