Compare commits
145 Commits
mp
...
df4c9f1b3f
Author | SHA1 | Date | |
---|---|---|---|
|
df4c9f1b3f | ||
|
f49e6adedf | ||
|
d1a9bcc4fb | ||
|
bc8bd8c2f8 | ||
|
ca16c3a5e6 | ||
|
7abe19c7d9 | ||
|
f3f528e866 | ||
|
ec6fad552b | ||
|
87b878a09e | ||
|
e66eddd1d5 | ||
|
ec416515f3 | ||
|
d12e540830 | ||
|
e7eecc8ef2 | ||
|
9d1aebde66 | ||
|
dc311f3b92 | ||
|
82ba4f7d1b | ||
|
e5ecd33ec4 | ||
|
9217e0e9e6 | ||
|
87b0db8862 | ||
|
8ceaf27eda | ||
|
9f9e8caff6 | ||
|
74f3eb90a7 | ||
|
38286d5c5b | ||
|
ec849322d7 | ||
|
b98a467eed | ||
|
25d3323427 | ||
|
b8e9095135 | ||
|
026f97687a | ||
|
f2f90a9a15 | ||
|
cf23f4d656 | ||
|
59819db2dd | ||
|
cc6ea766c4 | ||
|
509156fc14 | ||
|
cb9059bf3b | ||
|
07e2e695a6 | ||
|
e8daf7c263 | ||
|
5f907e32d7 | ||
|
0e37c1fea4 | ||
|
606bbec351 | ||
|
3fc23f5cf6 | ||
|
f3c8f70f47 | ||
|
6c27502757 | ||
|
5a51248cb0 | ||
|
79bb28fd12 | ||
|
b3e06de4cb | ||
|
b03ae53df6 | ||
|
15c15d5bcb | ||
|
2f0328a41a | ||
|
779ebdd37a | ||
|
5340f0726e | ||
|
e95ee70351 | ||
|
5cc304a261 | ||
|
7636225414 | ||
|
f68c6e211d | ||
|
8c81f7673b | ||
|
c1dfd4e928 | ||
|
7e55b1e132 | ||
|
ee787e0d4a | ||
|
7d481d26b9 | ||
|
168ae1e2ae | ||
|
6230569bbb | ||
|
238e85a5e4 | ||
|
0137dba1fd | ||
|
b6f76827b0 | ||
|
66eb002528 | ||
|
b1f0498472 | ||
|
e5584c73be | ||
|
c855a14ae8 | ||
|
d77271540f | ||
|
6b8852f269 | ||
|
f0e36d7d7c | ||
|
6153aca5d8 | ||
|
92fcdbb31a | ||
|
c72480110f | ||
|
8331610e7a | ||
|
4a1e01c5a5 | ||
|
6ccf6ce3dc | ||
|
a3535364fa | ||
|
4e9000c6b5 | ||
|
a4aba62656 | ||
|
7de2f800f9 | ||
|
233fab4fac | ||
|
6718627e9d | ||
|
b9d4264225 | ||
|
0095f1da5c | ||
|
44fef508a1 | ||
|
d6c0df17f5 | ||
|
8ade602be1 | ||
|
0980d89072 | ||
|
278ade5d36 | ||
|
10869eb197 | ||
|
1bc20c1cdb | ||
|
4bb7367cfa | ||
|
c0c9cc0411 | ||
|
b78dc6673f | ||
|
f52385ccd5 | ||
|
7ad9a6987c | ||
|
184b198470 | ||
|
2dc0670266 | ||
|
6ccd056d41 | ||
|
73daa12db1 | ||
|
80d21e56c7 | ||
|
b0d96a3c47 | ||
|
0682757631 | ||
|
52d540df90 | ||
|
256d1eee69 | ||
|
9e9ad56890 | ||
|
1e98ae10c2 | ||
|
fabe2b3558 | ||
|
565034dbae | ||
|
a7849b3634 | ||
|
3a372b9eee | ||
|
2e8294ab88 | ||
|
30eb96608d | ||
|
822a807e58 | ||
|
f90d1abe05 | ||
|
2f59ce490e | ||
|
4cb60d5997 | ||
|
1155f0547f | ||
|
cac9c56afb | ||
|
f050946ac5 | ||
|
2f0c02def2 | ||
|
c71d256006 | ||
|
946b615acf | ||
|
fef76af3a5 | ||
|
736c3f46b6 | ||
|
bec9c95999 | ||
|
16de522196 | ||
|
232ec4f339 | ||
|
d3cbbe8085 | ||
|
e51c236c20 | ||
|
876649fd74 | ||
|
8a6f89be88 | ||
|
45fca0bbfe | ||
|
cd92410cfe | ||
|
04eec19ab2 | ||
|
423157dbba | ||
|
94e12e6cf5 | ||
|
61512a15ec | ||
|
65943053ff | ||
|
52a911b109 | ||
|
3ab2543715 | ||
|
b9d0375a72 | ||
|
0fc24b6233 | ||
|
4463e21d9e |
4
.clang-format
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
SortIncludes: false
|
||||||
|
BasedOnStyle: Google
|
||||||
|
ColumnLimit: 0
|
||||||
|
IndentWidth: 4
|
2
.gitattributes
vendored
@@ -1 +1 @@
|
|||||||
libev/* linguist-vendored
|
lib/aes_acc/asm/* linguist-vendored
|
||||||
|
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
/udp2raw*
|
35
CMakeLists.txt
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
#note: experimental
|
||||||
|
# currently only used for generating `compile_commands.json` for clangd.
|
||||||
|
# to build this project, it's suggested to use `makefile` instead
|
||||||
|
|
||||||
|
cmake_minimum_required(VERSION 3.7)
|
||||||
|
project(udp2raw)
|
||||||
|
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
||||||
|
|
||||||
|
set(CMAKE_CXX_STANDARD 11)
|
||||||
|
|
||||||
|
set(SOURCE_FILES
|
||||||
|
main.cpp
|
||||||
|
lib/md5.cpp
|
||||||
|
lib/pbkdf2-sha1.cpp
|
||||||
|
lib/pbkdf2-sha256.cpp
|
||||||
|
encrypt.cpp
|
||||||
|
log.cpp
|
||||||
|
network.cpp
|
||||||
|
common.cpp
|
||||||
|
connection.cpp
|
||||||
|
misc.cpp
|
||||||
|
fd_manager.cpp
|
||||||
|
client.cpp
|
||||||
|
server.cpp
|
||||||
|
lib/aes_faster_c/aes.cpp
|
||||||
|
lib/aes_faster_c/wrapper.cpp
|
||||||
|
my_ev.cpp
|
||||||
|
)
|
||||||
|
set(CMAKE_CXX_FLAGS "-Wall -Wextra -Wno-unused-variable -Wno-unused-parameter -Wno-missing-field-initializers -O2 -g -fsanitize=address,undefined")
|
||||||
|
|
||||||
|
add_executable(udp2raw ${SOURCE_FILES})
|
||||||
|
target_link_libraries(udp2raw rt)
|
||||||
|
target_link_libraries(udp2raw pthread)
|
||||||
|
include_directories(SYSTEM "libev")
|
||||||
|
include_directories(".")
|
13
Dockerfile
@@ -1,13 +0,0 @@
|
|||||||
FROM alpine:3.6 as builder
|
|
||||||
|
|
||||||
WORKDIR /
|
|
||||||
|
|
||||||
RUN apk add --no-cache git build-base linux-headers && \
|
|
||||||
git clone https://github.com/wangyu-/udp2raw-tunnel.git && \
|
|
||||||
cd udp2raw-tunnel && \
|
|
||||||
make dynamic
|
|
||||||
|
|
||||||
FROM alpine:3.6
|
|
||||||
RUN apk add --no-cache libstdc++ iptables
|
|
||||||
COPY --from=builder /udp2raw-tunnel/udp2raw_dynamic /bin/
|
|
||||||
ENTRYPOINT [ "/bin/udp2raw_dynamic" ]
|
|
@@ -1,6 +1 @@
|
|||||||
For English speaking user:
|
English Only.
|
||||||
https://github.com/wangyu-/UDPspeeder/wiki/Issue-Guide
|
|
||||||
|
|
||||||
中文用户请看:
|
|
||||||
https://github.com/wangyu-/UDPspeeder/wiki/发Issue前请看
|
|
||||||
(否则Issue可能被忽略,或被直接关掉)
|
|
||||||
|
226
README.md
@@ -1,11 +1,227 @@
|
|||||||
# udp2raw-multiplatform
|
# 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).
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
multi-platform(cross-platform) version of [udp2raw](https://github.com/wangyu-/udp2raw-tunnel), which supports Windows/Mac/BSD natively.
|
or
|
||||||
|
|
||||||
udp2raw的跨平台版,协议兼容[linux版的udpraw](https://github.com/wangyu-/udp2raw-tunnel),可以直接运行在Windows、Mac、BSD上。
|

|
||||||
|
|
||||||
Check [Wiki](https://github.com/wangyu-/udp2raw-multiplatform/wiki) for details.
|
[udp2raw wiki](https://github.com/wangyu-/udp2raw-tunnel/wiki)
|
||||||
|
|
||||||
更多信息,见 [Wiki](https://github.com/wangyu-/udp2raw-multiplatform/wiki)。
|
[简体中文](/doc/README.zh-cn.md)
|
||||||
|
|
||||||
|
|
||||||
|
# Support Platforms
|
||||||
|
Linux host (including desktop Linux,Android phone/tablet,OpenWRT router,or Raspberry PI) with root account or cap_net_raw capability.
|
||||||
|
|
||||||
|
For Windows and MacOS users, use the udp2raw in [this repo](https://github.com/wangyu-/udp2raw-multiplatform).
|
||||||
|
|
||||||
|
# Features
|
||||||
|
### Send/Receive UDP Packets with ICMP/FakeTCP/UDP headers
|
||||||
|
ICMP/FakeTCP headers help you bypass UDP blocking, UDP QOS or improper UDP NAT behavior on some ISPs. In ICMP header mode,udp2raw works like an ICMP tunnel.
|
||||||
|
|
||||||
|
UDP headers are also supported. In UDP header mode, it behaves just like a normal UDP tunnel, and you can just make use of the other features (such as encryption, anti-replay, or connection stabilization).
|
||||||
|
|
||||||
|
### Simulated TCP with Real-time/Out-of-Order Delivery
|
||||||
|
In FakeTCP header mode,udp2raw simulates 3-way handshake while establishing a connection,simulates seq and ack_seq while data transferring. It also simulates a few TCP options such as: `MSS`, `sackOk`, `TS`, `TS_ack`, `wscale`. Firewalls will regard FakeTCP as a TCP connection, but its essentially UDP: it supports real-time/out-of-order delivery(just as normal UDP does), no congestion control or re-transmission. So there wont be any TCP over TCP problem when using OpenVPN.
|
||||||
|
|
||||||
|
### Encryption, Anti-Replay
|
||||||
|
* Encrypt your traffic with AES-128-CBC.
|
||||||
|
* Protect data integrity by HMAC-SHA1 (or weaker MD5/CRC32).
|
||||||
|
* Defense replay attack with anti-replay window.
|
||||||
|
|
||||||
|
[Notes on encryption](https://github.com/wangyu-/udp2raw-tunnel/wiki/Notes-on-encryption)
|
||||||
|
|
||||||
|
### Failure Dectection & Stabilization (Connection Recovery)
|
||||||
|
Conection failures are detected by heartbeats. If timed-out, client will automatically change port number and reconnect. If reconnection is successful, the previous connection will be recovered, and all existing UDP conversations will stay vaild.
|
||||||
|
|
||||||
|
For example, if you use udp2raw + OpenVPN, OpenVPN won't lose connection after any reconnect, **even if network cable is re-plugged or WiFi access point is changed**.
|
||||||
|
|
||||||
|
### Other Features
|
||||||
|
* **Multiplexing** One client can handle multiple UDP connections, all of which share the same raw connection.
|
||||||
|
|
||||||
|
* **Multiple Clients** One server can have multiple clients.
|
||||||
|
|
||||||
|
* **NAT Support** All of the 3 modes work in NAT environments.
|
||||||
|
|
||||||
|
* **OpenVZ Support** Tested on BandwagonHost VPS.
|
||||||
|
|
||||||
|
* **Easy to Build** No dependencies.To cross-compile udp2raw,all you need to do is just to download a toolchain,modify makefile to point at the toolchain,run `make cross` then everything is done.(Note:Pre-compiled binaries for Desktop,RaspberryPi,Android,some Openwrt Routers are already included in [Releases](https://github.com/wangyu-/udp2raw-tunnel/releases))
|
||||||
|
|
||||||
|
### Keywords
|
||||||
|
`Bypass UDP QoS` `Bypass UDP Blocking` `Bypass OpenVPN TCP over TCP problem` `OpenVPN over ICMP` `UDP to ICMP tunnel` `UDP to TCP tunnel` `UDP over ICMP` `UDP over TCP`
|
||||||
|
|
||||||
|
# Getting Started
|
||||||
|
### Installing
|
||||||
|
Download binary release from https://github.com/wangyu-/udp2raw-tunnel/releases
|
||||||
|
|
||||||
|
### Running
|
||||||
|
Assume your UDP is blocked or being QOS-ed or just poorly supported. Assume your server ip is 44.55.66.77, you have a service listening on udp port 7777.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Run at server side:
|
||||||
|
./udp2raw_amd64 -s -l0.0.0.0:4096 -r 127.0.0.1:7777 -k "passwd" --raw-mode faketcp -a
|
||||||
|
|
||||||
|
# Run at client side
|
||||||
|
./udp2raw_amd64 -c -l0.0.0.0:3333 -r44.55.66.77:4096 -k "passwd" --raw-mode faketcp -a
|
||||||
|
```
|
||||||
|
(The above commands need to be run as root. For better security, with some extra steps, you can run udp2raw as non-root. Check [this link](https://github.com/wangyu-/udp2raw-tunnel/wiki/run-udp2raw-as-non-root) for more info )
|
||||||
|
|
||||||
|
###### Server Output:
|
||||||
|

|
||||||
|
###### Client Output:
|
||||||
|

|
||||||
|
|
||||||
|
Now,an encrypted raw tunnel has been established between client and server through TCP port 4096. Connecting to UDP port 3333 at the client side is equivalent to connecting to port 7777 at the server side. No UDP traffic will be exposed.
|
||||||
|
|
||||||
|
### Note
|
||||||
|
To run on Android, check [Android_Guide](/doc/android_guide.md)
|
||||||
|
|
||||||
|
`-a` option automatically adds an iptables rule (or a few iptables rules) for you, udp2raw relies on this iptables rule to work stably. Be aware you dont forget `-a` (its a common mistake). If you dont want udp2raw to add iptables rule automatically, you can add it manually(take a look at `-g` option) and omit `-a`.
|
||||||
|
|
||||||
|
|
||||||
|
# Advanced Topic
|
||||||
|
### Usage
|
||||||
|
```
|
||||||
|
udp2raw-tunnel
|
||||||
|
git version:6e1df4b39f build date:Oct 24 2017 09:21:15
|
||||||
|
repository: https://github.com/wangyu-/udp2raw-tunnel
|
||||||
|
|
||||||
|
usage:
|
||||||
|
run as client : ./this_program -c -l local_listen_ip:local_port -r server_address:server_port [options]
|
||||||
|
run as server : ./this_program -s -l server_listen_ip:server_port -r remote_address:remote_port [options]
|
||||||
|
|
||||||
|
common options,these options must be same on both side:
|
||||||
|
--raw-mode <string> avaliable values:faketcp(default),udp,icmp
|
||||||
|
-k,--key <string> password to gen symetric key,default:"secret key"
|
||||||
|
--cipher-mode <string> avaliable values:aes128cbc(default),xor,none
|
||||||
|
--auth-mode <string> avaliable values:hmac_sha1,md5(default),crc32,simple,none
|
||||||
|
-a,--auto-rule auto add (and delete) iptables rule
|
||||||
|
-g,--gen-rule generate iptables rule then exit,so that you can copy and
|
||||||
|
add it manually.overrides -a
|
||||||
|
--disable-anti-replay disable anti-replay,not suggested
|
||||||
|
client options:
|
||||||
|
--source-ip <ip> force source-ip for raw socket
|
||||||
|
--source-port <port> force source-port for raw socket,tcp/udp only
|
||||||
|
this option disables port changing while re-connecting
|
||||||
|
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
|
||||||
|
--disable-color disable log color
|
||||||
|
--disable-bpf disable the kernel space filter,most time its not necessary
|
||||||
|
unless you suspect there is a bug
|
||||||
|
--sock-buf <number> buf size for socket,>=10 and <=10240,unit:kbyte,default:1024
|
||||||
|
--force-sock-buf bypass system limitation while setting sock-buf
|
||||||
|
--seq-mode <number> seq increase mode for faketcp:
|
||||||
|
0:static header,do not increase seq and ack_seq
|
||||||
|
1:increase seq for every packet,simply ack last seq
|
||||||
|
2:increase seq randomly, about every 3 packets,simply ack last seq
|
||||||
|
3:simulate an almost real seq/ack procedure(default)
|
||||||
|
4:similiar to 3,but do not consider TCP Option Window_Scale,
|
||||||
|
maybe useful when firewall doesnt support TCP Option
|
||||||
|
--lower-level <string> send packets at OSI level 2, format:'if_name#dest_mac_adress'
|
||||||
|
ie:'eth0#00:23:45:67:89:b9'.or try '--lower-level auto' to obtain
|
||||||
|
the parameter automatically,specify it manually if 'auto' failed
|
||||||
|
--gen-add generate iptables rule and add it permanently,then exit.overrides -g
|
||||||
|
--keep-rule monitor iptables and auto re-add if necessary.implys -a
|
||||||
|
--clear clear any iptables rules added by this program.overrides everything
|
||||||
|
-h,--help print this help message
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
### Iptables rules,`-a` and `-g`
|
||||||
|
This program sends packets via raw socket. In FakeTCP mode, Linux kernel TCP packet processing has to be blocked by a iptables rule on both sides, otherwise the kernel will automatically send RST for an unrecongized TCP packet and you will sustain from stability / peformance problems. You can use `-a` option to let the program automatically add / delete iptables rule on start / exit. You can also use the `-g` option to generate iptables rule and add it manually.
|
||||||
|
|
||||||
|
### `--cipher-mode` and `--auth-mode`
|
||||||
|
It is suggested to use `aes128cbc` + `hmac_sha1` to obtain maximum security. If you want to run the program on a router, you can try `xor` + `simple`, which can fool packet inspection by firewalls the most of time, but it cannot protect you from serious attacks. Mode none is only for debugging purpose. It is not recommended to set the cipher-mode or auth-mode to none.
|
||||||
|
|
||||||
|
### `--seq-mode`
|
||||||
|
The FakeTCP mode does not behave 100% like a real tcp connection. ISPs may be able to distinguish the simulated tcp traffic from the real TCP traffic (though it's costly). seq-mode can help you change the seq increase behavior slightly. If you experience connection problems, try to change the value.
|
||||||
|
|
||||||
|
### `--lower-level`
|
||||||
|
`--lower-level` allows you to send packet at OSI level 2(link level),so that you can bypass any local iptables rules. If you have a complicated iptables rules which conflicts with udp2raw and you cant(or too lazy to) edit the iptables rules,`--lower-level` can be very useful. Try `--lower-level auto` to auto detect the parameters,you can specify it manually if `auto` fails.
|
||||||
|
|
||||||
|
Manual format `if_name#dest_mac_adress`,ie:`eth0#00:23:45:67:89:b9`.
|
||||||
|
|
||||||
|
### `--keep-rule`
|
||||||
|
Monitor iptables and auto re-add iptables rules(for blocking kernel tcp processing) if necessary.Especially useful when iptables rules may be cleared by other programs(for example,if you are using openwrt,everytime you changed and commited a setting,iptables rule may be cleared and re-constructed).
|
||||||
|
|
||||||
|
### `--conf-file`
|
||||||
|
|
||||||
|
You can also load options from a configuration file in order to keep secrets away from `ps` command.
|
||||||
|
|
||||||
|
For example, rewrite the options for the above `server` example (in Getting Started section) into configuration file:
|
||||||
|
|
||||||
|
`server.conf`
|
||||||
|
|
||||||
|
```
|
||||||
|
-s
|
||||||
|
# You can add comments like this
|
||||||
|
# Comments MUST occupy an entire line
|
||||||
|
# Or they will not work as expected
|
||||||
|
# Listen address
|
||||||
|
-l 0.0.0.0:4096
|
||||||
|
# Remote address
|
||||||
|
-r 127.0.0.1:7777
|
||||||
|
-a
|
||||||
|
-k passwd
|
||||||
|
--raw-mode faketcp
|
||||||
|
```
|
||||||
|
|
||||||
|
Pay attention to the `-k` parameter: In command line mode the quotes around the password will be removed by shell. In configuration files we do not remove quotes.
|
||||||
|
|
||||||
|
Then start the server with
|
||||||
|
|
||||||
|
```bash
|
||||||
|
./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:
|
||||||
|
iperf3 TCP via OpenVPN + udp2raw
|
||||||
|
(iperf3 UDP mode is not used because of a bug mentioned in this issue: https://github.com/esnet/iperf/issues/296 . Instead, we package the TCP traffic into UDP by OpenVPN to test the performance. Read [Application](https://github.com/wangyu-/udp2raw-tunnel#application) for details.
|
||||||
|
|
||||||
|
#### iperf3 command:
|
||||||
|
```
|
||||||
|
iperf3 -c 10.222.2.1 -P40
|
||||||
|
iperf3 -c 10.222.2.1 -P40 -R
|
||||||
|
```
|
||||||
|
#### Environments
|
||||||
|
* **Client** Vultr $2.5/monthly plan (single core 2.4GHz cpu, 512MB RAM, Tokyo, Japan)
|
||||||
|
* **Server** BandwagonHost $3.99/annually plan (single core 2.0GHz cpu, 128MB RAM, Los Angeles, USA)
|
||||||
|
|
||||||
|
### Test1
|
||||||
|
raw_mode: faketcp cipher_mode: xor auth_mode: simple
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
(reverse speed was simliar and not uploaded)
|
||||||
|
|
||||||
|
### Test2
|
||||||
|
raw_mode: faketcp cipher_mode: aes128cbc auth_mode: md5
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
(reverse speed was simliar and not uploaded)
|
||||||
|
|
||||||
|
# wiki
|
||||||
|
|
||||||
|
Check wiki for more info:
|
||||||
|
|
||||||
|
https://github.com/wangyu-/udp2raw-tunnel/wiki
|
||||||
|
1653
client.cpp
1977
common.cpp
673
common.h
@@ -10,16 +10,16 @@
|
|||||||
#define __STDC_FORMAT_MACROS 1
|
#define __STDC_FORMAT_MACROS 1
|
||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
|
|
||||||
#include<stdio.h>
|
#include <stdio.h>
|
||||||
#include<string.h>
|
#include <string.h>
|
||||||
#include<stdlib.h>
|
#include <stdlib.h>
|
||||||
#include<getopt.h>
|
#include <getopt.h>
|
||||||
|
|
||||||
#include<unistd.h>
|
#include <unistd.h>
|
||||||
#include<errno.h>
|
#include <errno.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <stdlib.h> //for exit(0);
|
#include <stdlib.h> //for exit(0);
|
||||||
#include <errno.h> //For errno - the error number
|
#include <errno.h> //For errno - the error number
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
@@ -27,6 +27,12 @@
|
|||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
|
|
||||||
|
#ifndef USE_LIBNET
|
||||||
|
#define NO_LIBNET
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(UDP2RAW_MP)
|
||||||
|
const int is_udp2raw_mp = 1;
|
||||||
#if !defined(__CYGWIN__) && !defined(__MINGW32__)
|
#if !defined(__CYGWIN__) && !defined(__MINGW32__)
|
||||||
#include <pcap.h>
|
#include <pcap.h>
|
||||||
#else
|
#else
|
||||||
@@ -38,6 +44,20 @@
|
|||||||
#include <libnet.h>
|
#include <libnet.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#else
|
||||||
|
#define UDP2RAW_LINUX
|
||||||
|
const int is_udp2raw_mp = 0;
|
||||||
|
//#include <linux/if_ether.h>
|
||||||
|
#include <linux/filter.h>
|
||||||
|
#include <linux/if_packet.h>
|
||||||
|
#include <sys/epoll.h>
|
||||||
|
//#include <sys/wait.h> //signal
|
||||||
|
#include <netinet/if_ether.h>
|
||||||
|
#include <net/if.h>
|
||||||
|
#include <sys/timerfd.h>
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
#if !defined(NO_LIBEV_EMBED)
|
#if !defined(NO_LIBEV_EMBED)
|
||||||
#include <my_ev.h>
|
#include <my_ev.h>
|
||||||
#else
|
#else
|
||||||
@@ -59,51 +79,47 @@ typedef int socklen_t;
|
|||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include <unordered_map>
|
||||||
#include<unordered_map>
|
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <set>
|
#include <set>
|
||||||
#include <list>
|
#include <list>
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
#if defined(__BYTE_ORDER) && __BYTE_ORDER == __BIG_ENDIAN || \
|
#if defined(__BYTE_ORDER) && __BYTE_ORDER == __BIG_ENDIAN || \
|
||||||
defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ || \
|
defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ || \
|
||||||
defined(__BIG_ENDIAN__) || \
|
defined(__BIG_ENDIAN__) || \
|
||||||
defined(__ARMEB__) || \
|
defined(__ARMEB__) || \
|
||||||
defined(__THUMBEB__) || \
|
defined(__THUMBEB__) || \
|
||||||
defined(__AARCH64EB__) || \
|
defined(__AARCH64EB__) || \
|
||||||
defined(_MIBSEB) || defined(__MIBSEB) || defined(__MIBSEB__)
|
defined(_MIBSEB) || defined(__MIBSEB) || defined(__MIBSEB__)
|
||||||
#define UDP2RAW_BIG_ENDIAN 1
|
#define UDP2RAW_BIG_ENDIAN 1
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if defined(__BYTE_ORDER) && __BYTE_ORDER == __LITTLE_ENDIAN || \
|
||||||
#if defined(__BYTE_ORDER) && __BYTE_ORDER == __LITTLE_ENDIAN || \
|
|
||||||
defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ || \
|
defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ || \
|
||||||
defined(__LITTLE_ENDIAN__) || \
|
defined(__LITTLE_ENDIAN__) || \
|
||||||
defined(__ARMEL__) || \
|
defined(__ARMEL__) || \
|
||||||
defined(__THUMBEL__) || \
|
defined(__THUMBEL__) || \
|
||||||
defined(__AARCH64EL__) || \
|
defined(__AARCH64EL__) || \
|
||||||
defined(_MIPSEL) || defined(__MIPSEL) || defined(__MIPSEL__)
|
defined(_MIPSEL) || defined(__MIPSEL) || defined(__MIPSEL__)
|
||||||
#define UDP2RAW_LITTLE_ENDIAN 1
|
#define UDP2RAW_LITTLE_ENDIAN 1
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(UDP2RAW_BIG_ENDIAN) &&defined(UDP2RAW_LITTLE_ENDIAN)
|
#if defined(UDP2RAW_BIG_ENDIAN) && defined(UDP2RAW_LITTLE_ENDIAN)
|
||||||
#error "endian detection conflicts"
|
#error "endian detection conflicts"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
#if !defined(UDP2RAW_BIG_ENDIAN) && !defined(UDP2RAW_LITTLE_ENDIAN)
|
#if !defined(UDP2RAW_BIG_ENDIAN) && !defined(UDP2RAW_LITTLE_ENDIAN)
|
||||||
#error "endian detection failed"
|
#error "endian detection failed"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
#if defined(__MINGW32__)
|
#if defined(__MINGW32__)
|
||||||
int inet_pton(int af, const char *src, void *dst);
|
int inet_pton(int af, const char *src, void *dst);
|
||||||
const char *inet_ntop(int af, const void *src, char *dst, socklen_t size);
|
const char *inet_ntop(int af, const void *src, char *dst, socklen_t size);
|
||||||
#define setsockopt(a,b,c,d,e) setsockopt(a,b,c,(const char *)(d),e)
|
#define setsockopt(a, b, c, d, e) setsockopt(a, b, c, (const char *)(d), e)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
char *get_sock_error();
|
char *get_sock_error();
|
||||||
@@ -111,21 +127,18 @@ int get_sock_errno();
|
|||||||
|
|
||||||
#if defined(__MINGW32__)
|
#if defined(__MINGW32__)
|
||||||
typedef SOCKET my_fd_t;
|
typedef SOCKET my_fd_t;
|
||||||
inline int sock_close(my_fd_t fd)
|
inline int sock_close(my_fd_t fd) {
|
||||||
{
|
return closesocket(fd);
|
||||||
return closesocket(fd);
|
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
typedef int my_fd_t;
|
typedef int my_fd_t;
|
||||||
inline int sock_close(my_fd_t fd)
|
inline int sock_close(my_fd_t fd) {
|
||||||
{
|
return close(fd);
|
||||||
return close(fd);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
typedef unsigned long long u64_t; // this works on most platform,avoid using the PRId64
|
||||||
typedef unsigned long long u64_t; //this works on most platform,avoid using the PRId64
|
|
||||||
typedef long long i64_t;
|
typedef long long i64_t;
|
||||||
|
|
||||||
typedef unsigned int u32_t;
|
typedef unsigned int u32_t;
|
||||||
@@ -144,258 +157,233 @@ typedef u64_t anti_replay_seq_t;
|
|||||||
|
|
||||||
typedef u64_t my_time_t;
|
typedef u64_t my_time_t;
|
||||||
|
|
||||||
const int max_addr_len=100;
|
const int max_addr_len = 100;
|
||||||
|
|
||||||
extern int force_socket_buf;
|
extern int force_socket_buf;
|
||||||
|
|
||||||
extern int g_fix_gro;
|
extern int g_fix_gro;
|
||||||
|
|
||||||
|
/*
|
||||||
struct ip_port_t
|
struct ip_port_t
|
||||||
{
|
{
|
||||||
u32_t ip;
|
u32_t ip;
|
||||||
int port;
|
int port;
|
||||||
void from_u64(u64_t u64);
|
void from_u64(u64_t u64);
|
||||||
u64_t to_u64();
|
u64_t to_u64();
|
||||||
char * to_s();
|
char * to_s();
|
||||||
};
|
};*/
|
||||||
|
|
||||||
|
|
||||||
typedef u64_t fd64_t;
|
typedef u64_t fd64_t;
|
||||||
|
|
||||||
u32_t djb2(unsigned char *str,int len);
|
u32_t djb2(unsigned char *str, int len);
|
||||||
u32_t sdbm(unsigned char *str,int len);
|
u32_t sdbm(unsigned char *str, int len);
|
||||||
|
|
||||||
struct address_t //TODO scope id
|
struct address_t // TODO scope id
|
||||||
{
|
{
|
||||||
struct hash_function
|
struct hash_function {
|
||||||
{
|
u32_t operator()(const address_t &key) const {
|
||||||
u32_t operator()(const address_t &key) const
|
return sdbm((unsigned char *)&key.inner, sizeof(key.inner));
|
||||||
{
|
}
|
||||||
return sdbm((unsigned char*)&key.inner,sizeof(key.inner));
|
};
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
union storage_t //sockaddr_storage is too huge, we dont use it.
|
union storage_t // sockaddr_storage is too huge, we dont use it.
|
||||||
{
|
|
||||||
sockaddr_in ipv4;
|
|
||||||
sockaddr_in6 ipv6;
|
|
||||||
};
|
|
||||||
storage_t inner;
|
|
||||||
|
|
||||||
address_t()
|
|
||||||
{
|
|
||||||
clear();
|
|
||||||
}
|
|
||||||
void clear()
|
|
||||||
{
|
|
||||||
memset(&inner,0,sizeof(inner));
|
|
||||||
}
|
|
||||||
int from_ip_port(u32_t ip, int port)
|
|
||||||
{
|
|
||||||
clear();
|
|
||||||
inner.ipv4.sin_family=AF_INET;
|
|
||||||
inner.ipv4.sin_port=htons(port);
|
|
||||||
inner.ipv4.sin_addr.s_addr=ip;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int from_ip_port_new(int type, void * ip, int port)
|
|
||||||
{
|
|
||||||
clear();
|
|
||||||
if(type==AF_INET)
|
|
||||||
{
|
|
||||||
inner.ipv4.sin_family=AF_INET;
|
|
||||||
inner.ipv4.sin_port=htons(port);
|
|
||||||
inner.ipv4.sin_addr.s_addr=*((u32_t *)ip);
|
|
||||||
}
|
|
||||||
else if(type==AF_INET6)
|
|
||||||
{
|
|
||||||
inner.ipv6.sin6_family=AF_INET6;
|
|
||||||
inner.ipv6.sin6_port=htons(port);
|
|
||||||
inner.ipv6.sin6_addr=*((in6_addr*)ip);
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int from_str(char * str);
|
|
||||||
|
|
||||||
int from_str_ip_only(char * str);
|
|
||||||
|
|
||||||
int from_sockaddr(sockaddr *,socklen_t);
|
|
||||||
|
|
||||||
char* get_str();
|
|
||||||
void to_str(char *);
|
|
||||||
|
|
||||||
inline u32_t get_type()
|
|
||||||
{
|
|
||||||
u32_t ret=((sockaddr*)&inner)->sa_family;
|
|
||||||
assert(ret==AF_INET||ret==AF_INET6);
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline u32_t get_len()
|
|
||||||
{
|
|
||||||
u32_t type=get_type();
|
|
||||||
switch(type)
|
|
||||||
{
|
|
||||||
case AF_INET:
|
|
||||||
return sizeof(sockaddr_in);
|
|
||||||
case AF_INET6:
|
|
||||||
return sizeof(sockaddr_in6);
|
|
||||||
default:
|
|
||||||
assert(0==1);
|
|
||||||
}
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline u32_t get_port()
|
|
||||||
{
|
|
||||||
u32_t type=get_type();
|
|
||||||
switch(type)
|
|
||||||
{
|
|
||||||
case AF_INET:
|
|
||||||
return ntohs(inner.ipv4.sin_port);
|
|
||||||
case AF_INET6:
|
|
||||||
return ntohs(inner.ipv6.sin6_port);
|
|
||||||
default:
|
|
||||||
assert(0==1);
|
|
||||||
}
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline void set_port(int port)
|
|
||||||
{
|
|
||||||
u32_t type=get_type();
|
|
||||||
switch(type)
|
|
||||||
{
|
|
||||||
case AF_INET:
|
|
||||||
inner.ipv4.sin_port=htons(port);
|
|
||||||
break;
|
|
||||||
case AF_INET6:
|
|
||||||
inner.ipv6.sin6_port=htons(port);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
assert(0==1);
|
|
||||||
}
|
|
||||||
return ;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool operator == (const address_t &b) const
|
|
||||||
{
|
{
|
||||||
//return this->data==b.data;
|
sockaddr_in ipv4;
|
||||||
return memcmp(&this->inner,&b.inner,sizeof(this->inner))==0;
|
sockaddr_in6 ipv6;
|
||||||
|
};
|
||||||
|
storage_t inner;
|
||||||
|
|
||||||
|
address_t() {
|
||||||
|
clear();
|
||||||
|
}
|
||||||
|
void clear() {
|
||||||
|
memset(&inner, 0, sizeof(inner));
|
||||||
|
}
|
||||||
|
int from_ip_port(u32_t ip, int port) {
|
||||||
|
clear();
|
||||||
|
inner.ipv4.sin_family = AF_INET;
|
||||||
|
inner.ipv4.sin_port = htons(port);
|
||||||
|
inner.ipv4.sin_addr.s_addr = ip;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int from_ip_port_new(int type, void *ip, int port) {
|
||||||
|
clear();
|
||||||
|
if (type == AF_INET) {
|
||||||
|
inner.ipv4.sin_family = AF_INET;
|
||||||
|
inner.ipv4.sin_port = htons(port);
|
||||||
|
inner.ipv4.sin_addr.s_addr = *((u32_t *)ip);
|
||||||
|
} else if (type == AF_INET6) {
|
||||||
|
inner.ipv6.sin6_family = AF_INET6;
|
||||||
|
inner.ipv6.sin6_port = htons(port);
|
||||||
|
inner.ipv6.sin6_addr = *((in6_addr *)ip);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int from_str(char *str);
|
||||||
|
|
||||||
|
int from_str_ip_only(char *str);
|
||||||
|
|
||||||
|
int from_sockaddr(sockaddr *, socklen_t);
|
||||||
|
|
||||||
|
char *get_str();
|
||||||
|
void to_str(char *);
|
||||||
|
|
||||||
|
inline u32_t get_type() {
|
||||||
|
u32_t ret = ((sockaddr *)&inner)->sa_family;
|
||||||
|
assert(ret == AF_INET || ret == AF_INET6);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline u32_t get_len() {
|
||||||
|
u32_t type = get_type();
|
||||||
|
switch (type) {
|
||||||
|
case AF_INET:
|
||||||
|
return sizeof(sockaddr_in);
|
||||||
|
case AF_INET6:
|
||||||
|
return sizeof(sockaddr_in6);
|
||||||
|
default:
|
||||||
|
assert(0 == 1);
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline u32_t get_port() {
|
||||||
|
u32_t type = get_type();
|
||||||
|
switch (type) {
|
||||||
|
case AF_INET:
|
||||||
|
return ntohs(inner.ipv4.sin_port);
|
||||||
|
case AF_INET6:
|
||||||
|
return ntohs(inner.ipv6.sin6_port);
|
||||||
|
default:
|
||||||
|
assert(0 == 1);
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void set_port(int port) {
|
||||||
|
u32_t type = get_type();
|
||||||
|
switch (type) {
|
||||||
|
case AF_INET:
|
||||||
|
inner.ipv4.sin_port = htons(port);
|
||||||
|
break;
|
||||||
|
case AF_INET6:
|
||||||
|
inner.ipv6.sin6_port = htons(port);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
assert(0 == 1);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool operator==(const address_t &b) const {
|
||||||
|
// return this->data==b.data;
|
||||||
|
return memcmp(&this->inner, &b.inner, sizeof(this->inner)) == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int new_connected_udp_fd();
|
int new_connected_udp_fd();
|
||||||
|
|
||||||
char* get_ip();
|
char *get_ip();
|
||||||
};
|
};
|
||||||
|
|
||||||
namespace std {
|
namespace std {
|
||||||
template <>
|
template <>
|
||||||
struct hash<address_t>
|
struct hash<address_t> {
|
||||||
{
|
std::size_t operator()(const address_t &key) const {
|
||||||
std::size_t operator()(const address_t& key) const
|
// return address_t::hash_function(k);
|
||||||
{
|
return sdbm((unsigned char *)&key.inner, sizeof(key.inner));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
} // namespace std
|
||||||
|
|
||||||
//return address_t::hash_function(k);
|
union my_ip_t // just a simple version of address_t,stores ip only
|
||||||
return sdbm((unsigned char*)&key.inner,sizeof(key.inner));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
union my_ip_t //just a simple version of address_t,stores ip only
|
|
||||||
{
|
{
|
||||||
u32_t v4;
|
u32_t v4;
|
||||||
in6_addr v6;
|
in6_addr v6;
|
||||||
|
|
||||||
bool equal (const my_ip_t &b) const;
|
bool equal(const my_ip_t &b) const;
|
||||||
|
|
||||||
//int from_str(char * str);
|
// int from_str(char * str);
|
||||||
char * get_str1() const;
|
char *get_str1() const;
|
||||||
char * get_str2() const;
|
char *get_str2() const;
|
||||||
|
|
||||||
int from_address_t(address_t a);
|
int from_address_t(address_t a);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct not_copy_able_t
|
struct not_copy_able_t {
|
||||||
{
|
not_copy_able_t() {
|
||||||
not_copy_able_t()
|
}
|
||||||
{
|
not_copy_able_t(const not_copy_able_t &other) {
|
||||||
|
assert(0 == 1);
|
||||||
}
|
}
|
||||||
not_copy_able_t(const not_copy_able_t &other)
|
const not_copy_able_t &operator=(const not_copy_able_t &other) {
|
||||||
{
|
assert(0 == 1);
|
||||||
assert(0==1);
|
return other;
|
||||||
}
|
}
|
||||||
const not_copy_able_t & operator=(const not_copy_able_t &other)
|
|
||||||
{
|
|
||||||
assert(0==1);
|
|
||||||
return other;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const int huge_data_len=65535+100; //a packet with link level header might be larger than 65535
|
const int huge_data_len = 65535 + 100; // a packet with link level header might be larger than 65535
|
||||||
const int huge_buf_len=huge_data_len+100;
|
const int huge_buf_len = huge_data_len + 100;
|
||||||
|
|
||||||
const int max_data_len=1800;
|
const int max_data_len = 1800;
|
||||||
const int buf_len=max_data_len+400;
|
const int buf_len = max_data_len + 400;
|
||||||
|
|
||||||
//const int max_address_len=512;
|
// const int max_address_len=512;
|
||||||
|
|
||||||
const int queue_len=200;
|
#ifdef UDP2RAW_MP
|
||||||
|
const int queue_len = 200;
|
||||||
|
|
||||||
struct queue_t
|
struct queue_t {
|
||||||
{
|
char data[queue_len][huge_buf_len];
|
||||||
char data[queue_len][huge_buf_len];
|
int data_len[queue_len];
|
||||||
int data_len[queue_len];
|
|
||||||
|
|
||||||
int head=0;
|
int head = 0;
|
||||||
int tail=0;
|
int tail = 0;
|
||||||
void clear()
|
void clear() {
|
||||||
{
|
head = tail = 0;
|
||||||
head=tail=0;
|
}
|
||||||
}
|
int empty() {
|
||||||
int empty()
|
if (head == tail)
|
||||||
{
|
return 1;
|
||||||
if(head==tail) return 1;
|
else
|
||||||
else return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
int full()
|
int full() {
|
||||||
{
|
if ((tail + 1) % queue_len == head)
|
||||||
if( (tail+1)%queue_len==head ) return 1;
|
return 1;
|
||||||
else return 0;
|
else
|
||||||
}
|
return 0;
|
||||||
void peek_front(char * & p,int &len)
|
}
|
||||||
{
|
void peek_front(char *&p, int &len) {
|
||||||
assert(!empty());
|
assert(!empty());
|
||||||
p=data[head];
|
p = data[head];
|
||||||
len=data_len[head];
|
len = data_len[head];
|
||||||
}
|
}
|
||||||
void pop_front()
|
void pop_front() {
|
||||||
{
|
assert(!empty());
|
||||||
assert(!empty());
|
head++;
|
||||||
head++;head%=queue_len;
|
head %= queue_len;
|
||||||
}
|
}
|
||||||
void push_back(char * p,int len)
|
void push_back(char *p, int len) {
|
||||||
{
|
assert(!full());
|
||||||
assert(!full());
|
memcpy(data[tail], p, len);
|
||||||
memcpy(data[tail],p,len);
|
data_len[tail] = len;
|
||||||
data_len[tail]=len;
|
tail++;
|
||||||
tail++;tail%=queue_len;
|
tail %= queue_len;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
int init_ws();
|
int init_ws();
|
||||||
|
#endif
|
||||||
u64_t get_current_time();
|
u64_t get_current_time();
|
||||||
u64_t pack_u64(u32_t a,u32_t b);
|
u64_t pack_u64(u32_t a, u32_t b);
|
||||||
|
|
||||||
u32_t get_u64_h(u64_t a);
|
u32_t get_u64_h(u64_t a);
|
||||||
|
|
||||||
u32_t get_u64_l(u64_t a);
|
u32_t get_u64_l(u64_t a);
|
||||||
|
|
||||||
char * my_ntoa(u32_t ip);
|
char *my_ntoa(u32_t ip);
|
||||||
|
|
||||||
void init_random_number_fd();
|
void init_random_number_fd();
|
||||||
u64_t get_true_random_number_64();
|
u64_t get_true_random_number_64();
|
||||||
@@ -404,138 +392,131 @@ u32_t get_true_random_number_nz();
|
|||||||
u64_t ntoh64(u64_t a);
|
u64_t ntoh64(u64_t a);
|
||||||
u64_t hton64(u64_t a);
|
u64_t hton64(u64_t a);
|
||||||
|
|
||||||
void write_u16(char *,u16_t a);// network order
|
void write_u16(char *, u16_t a); // network order
|
||||||
u16_t read_u16(char *);
|
u16_t read_u16(char *);
|
||||||
void write_u32(char *,u32_t a);// network order
|
void write_u32(char *, u32_t a); // network order
|
||||||
u32_t read_u32(char *);
|
u32_t read_u32(char *);
|
||||||
void write_u64(char *,u64_t a);
|
void write_u64(char *, u64_t a);
|
||||||
u64_t read_u64(char *);
|
u64_t read_u64(char *);
|
||||||
|
|
||||||
bool larger_than_u16(uint16_t a,uint16_t b);
|
bool larger_than_u16(uint16_t a, uint16_t b);
|
||||||
bool larger_than_u32(u32_t a,u32_t b);
|
bool larger_than_u32(u32_t a, u32_t b);
|
||||||
void setnonblocking(int sock);
|
void setnonblocking(int sock);
|
||||||
int set_buf_size(int fd,int socket_buf_size);
|
int set_buf_size(int fd, int socket_buf_size);
|
||||||
|
|
||||||
void myexit(int a);
|
void myexit(int a);
|
||||||
|
|
||||||
unsigned short csum(const unsigned short *ptr,int nbytes);
|
unsigned short csum(const unsigned short *ptr, int nbytes);
|
||||||
unsigned short csum_with_header(char* header,int hlen,const unsigned short *ptr,int nbytes);
|
unsigned short csum_with_header(char *header, int hlen, const unsigned short *ptr, int nbytes);
|
||||||
|
|
||||||
int numbers_to_char(my_id_t id1,my_id_t id2,my_id_t id3,char * &data,int &len);
|
int numbers_to_char(my_id_t id1, my_id_t id2, my_id_t id3, char *&data, int &len);
|
||||||
int char_to_numbers(const char * data,int len,my_id_t &id1,my_id_t &id2,my_id_t &id3);
|
int char_to_numbers(const char *data, int len, my_id_t &id1, my_id_t &id2, my_id_t &id3);
|
||||||
|
|
||||||
const int show_none=0;
|
const int show_none = 0;
|
||||||
const int show_command=0x1;
|
const int show_command = 0x1;
|
||||||
const int show_log=0x2;
|
const int show_log = 0x2;
|
||||||
const int show_all=show_command|show_log;
|
const int show_all = show_command | show_log;
|
||||||
|
|
||||||
int run_command(string command,char * &output,int flag=show_all);
|
int run_command(string command, char *&output, int flag = show_all);
|
||||||
//int run_command_no_log(string command,char * &output);
|
// int run_command_no_log(string command,char * &output);
|
||||||
int read_file(const char * file,string &output);
|
int read_file(const char *file, string &output);
|
||||||
|
|
||||||
vector<string> string_to_vec(const char * s,const char * sp);
|
vector<string> string_to_vec(const char *s, const char *sp);
|
||||||
vector< vector <string> > string_to_vec2(const char * s);
|
vector<vector<string> > string_to_vec2(const char *s);
|
||||||
|
|
||||||
string trim(const string& str, char c);
|
string trim(const string &str, char c);
|
||||||
|
|
||||||
string trim_conf_line(const string& str);
|
string trim_conf_line(const string &str);
|
||||||
|
|
||||||
vector<string> parse_conf_line(const string& s);
|
vector<string> parse_conf_line(const string &s);
|
||||||
|
|
||||||
int hex_to_u32_with_endian(const string & a,u32_t &output);
|
int hex_to_u32_with_endian(const string &a, u32_t &output);
|
||||||
int hex_to_u32(const string & a,u32_t &output);
|
int hex_to_u32(const string &a, u32_t &output);
|
||||||
//extern string iptables_pattern;
|
// extern string iptables_pattern;
|
||||||
|
|
||||||
int create_fifo(char * file);
|
int create_fifo(char *file);
|
||||||
|
|
||||||
void print_binary_chars(const char * a,int len);
|
void print_binary_chars(const char *a, int len);
|
||||||
|
|
||||||
template <class key_t>
|
template <class key_t>
|
||||||
struct lru_collector_t:not_copy_able_t
|
struct lru_collector_t : not_copy_able_t {
|
||||||
{
|
// typedef void* key_t;
|
||||||
//typedef void* key_t;
|
//#define key_t void*
|
||||||
//#define key_t void*
|
struct lru_pair_t {
|
||||||
struct lru_pair_t
|
key_t key;
|
||||||
{
|
my_time_t ts;
|
||||||
key_t key;
|
};
|
||||||
my_time_t ts;
|
|
||||||
};
|
|
||||||
|
|
||||||
unordered_map<key_t,typename list<lru_pair_t>::iterator> mp;
|
unordered_map<key_t, typename list<lru_pair_t>::iterator> mp;
|
||||||
|
|
||||||
list<lru_pair_t> q;
|
list<lru_pair_t> q;
|
||||||
int update(key_t key)
|
int update(key_t key) {
|
||||||
{
|
assert(mp.find(key) != mp.end());
|
||||||
assert(mp.find(key)!=mp.end());
|
auto it = mp[key];
|
||||||
auto it=mp[key];
|
q.erase(it);
|
||||||
q.erase(it);
|
|
||||||
|
|
||||||
my_time_t value=get_current_time();
|
my_time_t value = get_current_time();
|
||||||
if(!q.empty())
|
if (!q.empty()) {
|
||||||
{
|
assert(value >= q.front().ts);
|
||||||
assert(value >=q.front().ts);
|
}
|
||||||
}
|
lru_pair_t tmp;
|
||||||
lru_pair_t tmp; tmp.key=key; tmp.ts=value;
|
tmp.key = key;
|
||||||
q.push_front( tmp);
|
tmp.ts = value;
|
||||||
mp[key]=q.begin();
|
q.push_front(tmp);
|
||||||
|
mp[key] = q.begin();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
int new_key(key_t key)
|
int new_key(key_t key) {
|
||||||
{
|
assert(mp.find(key) == mp.end());
|
||||||
assert(mp.find(key)==mp.end());
|
|
||||||
|
|
||||||
my_time_t value=get_current_time();
|
my_time_t value = get_current_time();
|
||||||
if(!q.empty())
|
if (!q.empty()) {
|
||||||
{
|
assert(value >= q.front().ts);
|
||||||
assert(value >=q.front().ts);
|
}
|
||||||
}
|
lru_pair_t tmp;
|
||||||
lru_pair_t tmp; tmp.key=key; tmp.ts=value;
|
tmp.key = key;
|
||||||
q.push_front( tmp);
|
tmp.ts = value;
|
||||||
mp[key]=q.begin();
|
q.push_front(tmp);
|
||||||
|
mp[key] = q.begin();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
int size()
|
int size() {
|
||||||
{
|
return q.size();
|
||||||
return q.size();
|
}
|
||||||
}
|
int empty() {
|
||||||
int empty()
|
return q.empty();
|
||||||
{
|
}
|
||||||
return q.empty();
|
void clear() {
|
||||||
}
|
mp.clear();
|
||||||
void clear()
|
q.clear();
|
||||||
{
|
}
|
||||||
mp.clear(); q.clear();
|
my_time_t ts_of(key_t key) {
|
||||||
}
|
assert(mp.find(key) != mp.end());
|
||||||
my_time_t ts_of(key_t key)
|
return mp[key]->ts;
|
||||||
{
|
}
|
||||||
assert(mp.find(key)!=mp.end());
|
|
||||||
return mp[key]->ts;
|
|
||||||
}
|
|
||||||
|
|
||||||
my_time_t peek_back(key_t &key)
|
my_time_t peek_back(key_t &key) {
|
||||||
{
|
assert(!q.empty());
|
||||||
assert(!q.empty());
|
auto it = q.end();
|
||||||
auto it=q.end(); it--;
|
it--;
|
||||||
key=it->key;
|
key = it->key;
|
||||||
return it->ts;
|
return it->ts;
|
||||||
}
|
}
|
||||||
void erase(key_t key)
|
void erase(key_t key) {
|
||||||
{
|
assert(mp.find(key) != mp.end());
|
||||||
assert(mp.find(key)!=mp.end());
|
q.erase(mp[key]);
|
||||||
q.erase(mp[key]);
|
mp.erase(key);
|
||||||
mp.erase(key);
|
}
|
||||||
}
|
/*
|
||||||
/*
|
void erase_back()
|
||||||
void erase_back()
|
{
|
||||||
{
|
assert(!q.empty());
|
||||||
assert(!q.empty());
|
auto it=q.end(); it--;
|
||||||
auto it=q.end(); it--;
|
key_t key=it->key;
|
||||||
key_t key=it->key;
|
erase(key);
|
||||||
erase(key);
|
}*/
|
||||||
}*/
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
#endif /* COMMON_H_ */
|
#endif /* COMMON_H_ */
|
||||||
|
1201
connection.cpp
510
connection.h
@@ -16,336 +16,298 @@ extern int disable_anti_replay;
|
|||||||
#include "network.h"
|
#include "network.h"
|
||||||
#include "misc.h"
|
#include "misc.h"
|
||||||
|
|
||||||
const int disable_conv_clear=0;//a udp connection in the multiplexer is called conversation in this program,conv for short.
|
const int disable_conv_clear = 0; // a udp connection in the multiplexer is called conversation in this program,conv for short.
|
||||||
|
|
||||||
|
struct anti_replay_t // its for anti replay attack,similar to openvpn/ipsec 's anti replay window
|
||||||
struct anti_replay_t //its for anti replay attack,similar to openvpn/ipsec 's anti replay window
|
|
||||||
{
|
{
|
||||||
u64_t max_packet_received;
|
u64_t max_packet_received;
|
||||||
char window[anti_replay_window_size];
|
char window[anti_replay_window_size];
|
||||||
anti_replay_seq_t anti_replay_seq;
|
anti_replay_seq_t anti_replay_seq;
|
||||||
anti_replay_seq_t get_new_seq_for_send();
|
anti_replay_seq_t get_new_seq_for_send();
|
||||||
anti_replay_t();
|
anti_replay_t();
|
||||||
void re_init();
|
void re_init();
|
||||||
|
|
||||||
int is_vaild(u64_t seq);
|
int is_vaild(u64_t seq);
|
||||||
};//anti_replay;
|
}; // anti_replay;
|
||||||
|
|
||||||
void server_clear_function(u64_t u64);
|
void server_clear_function(u64_t u64);
|
||||||
|
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
|
|
||||||
template <class T>
|
template <class T>
|
||||||
struct conv_manager_t // manage the udp connections
|
struct conv_manager_t // manage the udp connections
|
||||||
{
|
{
|
||||||
//typedef hash_map map;
|
// typedef hash_map map;
|
||||||
unordered_map<T,u32_t> data_to_conv; //conv and u64 are both supposed to be uniq
|
unordered_map<T, u32_t> data_to_conv; // conv and u64 are both supposed to be uniq
|
||||||
unordered_map<u32_t,T> conv_to_data;
|
unordered_map<u32_t, T> conv_to_data;
|
||||||
|
|
||||||
lru_collector_t<u32_t> lru;
|
lru_collector_t<u32_t> lru;
|
||||||
//unordered_map<u32_t,u64_t> conv_last_active_time;
|
// unordered_map<u32_t,u64_t> conv_last_active_time;
|
||||||
|
|
||||||
//unordered_map<u32_t,u64_t>::iterator clear_it;
|
// unordered_map<u32_t,u64_t>::iterator clear_it;
|
||||||
|
|
||||||
void (*additional_clear_function)(T data) =0;
|
void (*additional_clear_function)(T data) = 0;
|
||||||
|
|
||||||
long long last_clear_time;
|
long long last_clear_time;
|
||||||
|
|
||||||
conv_manager_t()
|
conv_manager_t() {
|
||||||
{
|
// clear_it=conv_last_active_time.begin();
|
||||||
//clear_it=conv_last_active_time.begin();
|
long long last_clear_time = 0;
|
||||||
long long last_clear_time=0;
|
additional_clear_function = 0;
|
||||||
additional_clear_function=0;
|
}
|
||||||
}
|
~conv_manager_t() {
|
||||||
~conv_manager_t()
|
clear();
|
||||||
{
|
}
|
||||||
clear();
|
int get_size() {
|
||||||
}
|
return conv_to_data.size();
|
||||||
int get_size()
|
}
|
||||||
{
|
void reserve() {
|
||||||
return conv_to_data.size();
|
data_to_conv.reserve(10007);
|
||||||
}
|
conv_to_data.reserve(10007);
|
||||||
void reserve()
|
// conv_last_active_time.reserve(10007);
|
||||||
{
|
|
||||||
data_to_conv.reserve(10007);
|
|
||||||
conv_to_data.reserve(10007);
|
|
||||||
//conv_last_active_time.reserve(10007);
|
|
||||||
|
|
||||||
lru.mp.reserve(10007);
|
lru.mp.reserve(10007);
|
||||||
}
|
}
|
||||||
void clear()
|
void clear() {
|
||||||
{
|
if (disable_conv_clear) return;
|
||||||
if(disable_conv_clear) return ;
|
|
||||||
|
|
||||||
if(additional_clear_function!=0)
|
if (additional_clear_function != 0) {
|
||||||
{
|
for (auto it = conv_to_data.begin(); it != conv_to_data.end(); it++) {
|
||||||
for(auto it=conv_to_data.begin();it!=conv_to_data.end();it++)
|
// int fd=int((it->second<<32u)>>32u);
|
||||||
{
|
additional_clear_function(it->second);
|
||||||
//int fd=int((it->second<<32u)>>32u);
|
}
|
||||||
additional_clear_function( it->second);
|
}
|
||||||
}
|
data_to_conv.clear();
|
||||||
}
|
conv_to_data.clear();
|
||||||
data_to_conv.clear();
|
|
||||||
conv_to_data.clear();
|
|
||||||
|
|
||||||
lru.clear();
|
lru.clear();
|
||||||
//conv_last_active_time.clear();
|
// conv_last_active_time.clear();
|
||||||
|
|
||||||
//clear_it=conv_last_active_time.begin();
|
// clear_it=conv_last_active_time.begin();
|
||||||
|
}
|
||||||
|
u32_t get_new_conv() {
|
||||||
|
u32_t conv = get_true_random_number_nz();
|
||||||
|
while (conv_to_data.find(conv) != conv_to_data.end()) {
|
||||||
|
conv = get_true_random_number_nz();
|
||||||
|
}
|
||||||
|
return conv;
|
||||||
|
}
|
||||||
|
int is_conv_used(u32_t conv) {
|
||||||
|
return conv_to_data.find(conv) != conv_to_data.end();
|
||||||
|
}
|
||||||
|
int is_data_used(T data) {
|
||||||
|
return data_to_conv.find(data) != data_to_conv.end();
|
||||||
|
}
|
||||||
|
u32_t find_conv_by_data(T data) {
|
||||||
|
return data_to_conv[data];
|
||||||
|
}
|
||||||
|
T find_data_by_conv(u32_t conv) {
|
||||||
|
return conv_to_data[conv];
|
||||||
|
}
|
||||||
|
int update_active_time(u32_t conv) {
|
||||||
|
// return conv_last_active_time[conv]=get_current_time();
|
||||||
|
lru.update(conv);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
int insert_conv(u32_t conv, T data) {
|
||||||
|
data_to_conv[data] = conv;
|
||||||
|
conv_to_data[conv] = data;
|
||||||
|
// conv_last_active_time[conv]=get_current_time();
|
||||||
|
lru.new_key(conv);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
int erase_conv(u32_t conv) {
|
||||||
|
if (disable_conv_clear) return 0;
|
||||||
|
T data = conv_to_data[conv];
|
||||||
|
if (additional_clear_function != 0) {
|
||||||
|
additional_clear_function(data);
|
||||||
|
}
|
||||||
|
conv_to_data.erase(conv);
|
||||||
|
data_to_conv.erase(data);
|
||||||
|
// conv_last_active_time.erase(conv);
|
||||||
|
lru.erase(conv);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
int clear_inactive(char *info = 0) {
|
||||||
|
if (get_current_time() - last_clear_time > conv_clear_interval) {
|
||||||
|
last_clear_time = get_current_time();
|
||||||
|
return clear_inactive0(info);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
int clear_inactive0(char *info) {
|
||||||
|
if (disable_conv_clear) return 0;
|
||||||
|
|
||||||
}
|
unordered_map<u32_t, u64_t>::iterator it;
|
||||||
u32_t get_new_conv()
|
unordered_map<u32_t, u64_t>::iterator old_it;
|
||||||
{
|
|
||||||
u32_t conv=get_true_random_number_nz();
|
|
||||||
while(conv_to_data.find(conv)!=conv_to_data.end())
|
|
||||||
{
|
|
||||||
conv=get_true_random_number_nz();
|
|
||||||
}
|
|
||||||
return conv;
|
|
||||||
}
|
|
||||||
int is_conv_used(u32_t conv)
|
|
||||||
{
|
|
||||||
return conv_to_data.find(conv)!=conv_to_data.end();
|
|
||||||
}
|
|
||||||
int is_data_used(T data)
|
|
||||||
{
|
|
||||||
return data_to_conv.find(data)!=data_to_conv.end();
|
|
||||||
}
|
|
||||||
u32_t find_conv_by_data(T data)
|
|
||||||
{
|
|
||||||
return data_to_conv[data];
|
|
||||||
}
|
|
||||||
T find_data_by_conv(u32_t conv)
|
|
||||||
{
|
|
||||||
return conv_to_data[conv];
|
|
||||||
}
|
|
||||||
int update_active_time(u32_t conv)
|
|
||||||
{
|
|
||||||
//return conv_last_active_time[conv]=get_current_time();
|
|
||||||
lru.update(conv);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
int insert_conv(u32_t conv,T data)
|
|
||||||
{
|
|
||||||
data_to_conv[data]=conv;
|
|
||||||
conv_to_data[conv]=data;
|
|
||||||
//conv_last_active_time[conv]=get_current_time();
|
|
||||||
lru.new_key(conv);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
int erase_conv(u32_t conv)
|
|
||||||
{
|
|
||||||
if(disable_conv_clear) return 0;
|
|
||||||
T data=conv_to_data[conv];
|
|
||||||
if(additional_clear_function!=0)
|
|
||||||
{
|
|
||||||
additional_clear_function(data);
|
|
||||||
}
|
|
||||||
conv_to_data.erase(conv);
|
|
||||||
data_to_conv.erase(data);
|
|
||||||
//conv_last_active_time.erase(conv);
|
|
||||||
lru.erase(conv);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
int clear_inactive(char * info=0)
|
|
||||||
{
|
|
||||||
if(get_current_time()-last_clear_time>conv_clear_interval)
|
|
||||||
{
|
|
||||||
last_clear_time=get_current_time();
|
|
||||||
return clear_inactive0(info);
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
int clear_inactive0(char * info)
|
|
||||||
{
|
|
||||||
if(disable_conv_clear) return 0;
|
|
||||||
|
|
||||||
|
// map<uint32_t,uint64_t>::iterator it;
|
||||||
|
int cnt = 0;
|
||||||
|
// it=clear_it;
|
||||||
|
int size = lru.size();
|
||||||
|
int num_to_clean = size / conv_clear_ratio + conv_clear_min; // clear 1/10 each time,to avoid latency glitch
|
||||||
|
|
||||||
unordered_map<u32_t,u64_t>::iterator it;
|
num_to_clean = min(num_to_clean, size);
|
||||||
unordered_map<u32_t,u64_t>::iterator old_it;
|
|
||||||
|
|
||||||
//map<uint32_t,uint64_t>::iterator it;
|
my_time_t current_time = get_current_time();
|
||||||
int cnt=0;
|
for (;;) {
|
||||||
//it=clear_it;
|
if (cnt >= num_to_clean) break;
|
||||||
int size=lru.size();
|
if (lru.empty()) break;
|
||||||
int num_to_clean=size/conv_clear_ratio+conv_clear_min; //clear 1/10 each time,to avoid latency glitch
|
|
||||||
|
|
||||||
num_to_clean=min(num_to_clean,size);
|
u32_t conv;
|
||||||
|
my_time_t ts = lru.peek_back(conv);
|
||||||
|
|
||||||
my_time_t current_time=get_current_time();
|
if (current_time - ts < conv_timeout) break;
|
||||||
for(;;)
|
|
||||||
{
|
|
||||||
if(cnt>=num_to_clean) break;
|
|
||||||
if(lru.empty()) break;
|
|
||||||
|
|
||||||
u32_t conv;
|
erase_conv(conv);
|
||||||
my_time_t ts=lru.peek_back(conv);
|
if (info == 0) {
|
||||||
|
mylog(log_info, "conv %x cleared\n", conv);
|
||||||
|
} else {
|
||||||
|
mylog(log_info, "[%s]conv %x cleared\n", info, conv);
|
||||||
|
}
|
||||||
|
cnt++;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
if(current_time- ts < conv_timeout) break;
|
/*
|
||||||
|
conv_manager_t();
|
||||||
|
~conv_manager_t();
|
||||||
|
int get_size();
|
||||||
|
void reserve();
|
||||||
|
void clear();
|
||||||
|
u32_t get_new_conv();
|
||||||
|
int is_conv_used(u32_t conv);
|
||||||
|
int is_u64_used(T u64);
|
||||||
|
u32_t find_conv_by_u64(T u64);
|
||||||
|
T find_u64_by_conv(u32_t conv);
|
||||||
|
int update_active_time(u32_t conv);
|
||||||
|
int insert_conv(u32_t conv,T u64);
|
||||||
|
int erase_conv(u32_t conv);
|
||||||
|
int clear_inactive(char * ip_port=0);
|
||||||
|
int clear_inactive0(char * ip_port);*/
|
||||||
|
}; // g_conv_manager;
|
||||||
|
|
||||||
erase_conv(conv);
|
struct blob_t : not_copy_able_t // used in conn_info_t.
|
||||||
if(info==0)
|
|
||||||
{
|
|
||||||
mylog(log_info,"conv %x cleared\n",conv);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
mylog(log_info,"[%s]conv %x cleared\n",info,conv);
|
|
||||||
}
|
|
||||||
cnt++;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
conv_manager_t();
|
|
||||||
~conv_manager_t();
|
|
||||||
int get_size();
|
|
||||||
void reserve();
|
|
||||||
void clear();
|
|
||||||
u32_t get_new_conv();
|
|
||||||
int is_conv_used(u32_t conv);
|
|
||||||
int is_u64_used(T u64);
|
|
||||||
u32_t find_conv_by_u64(T u64);
|
|
||||||
T find_u64_by_conv(u32_t conv);
|
|
||||||
int update_active_time(u32_t conv);
|
|
||||||
int insert_conv(u32_t conv,T u64);
|
|
||||||
int erase_conv(u32_t conv);
|
|
||||||
int clear_inactive(char * ip_port=0);
|
|
||||||
int clear_inactive0(char * ip_port);*/
|
|
||||||
};//g_conv_manager;
|
|
||||||
|
|
||||||
struct blob_t:not_copy_able_t //used in conn_info_t.
|
|
||||||
{
|
{
|
||||||
union tmp_union_t//conv_manager_t is here to avoid copying when a connection is recovered
|
union tmp_union_t // conv_manager_t is here to avoid copying when a connection is recovered
|
||||||
{
|
{
|
||||||
conv_manager_t<address_t> c;
|
conv_manager_t<address_t> c;
|
||||||
conv_manager_t<u64_t> s;
|
conv_manager_t<u64_t> s;
|
||||||
//avoid templates here and there, avoid pointer and type cast
|
// avoid templates here and there, avoid pointer and type cast
|
||||||
tmp_union_t()
|
tmp_union_t() {
|
||||||
{
|
if (program_mode == client_mode) {
|
||||||
if(program_mode==client_mode)
|
new (&c) conv_manager_t<address_t>();
|
||||||
{
|
} else {
|
||||||
new( &c ) conv_manager_t<address_t>();
|
assert(program_mode == server_mode);
|
||||||
}
|
new (&s) conv_manager_t<u64_t>();
|
||||||
else
|
}
|
||||||
{
|
}
|
||||||
assert(program_mode==server_mode);
|
~tmp_union_t() {
|
||||||
new( &s ) conv_manager_t<u64_t>();
|
if (program_mode == client_mode) {
|
||||||
}
|
c.~conv_manager_t<address_t>();
|
||||||
}
|
} else {
|
||||||
~tmp_union_t()
|
assert(program_mode == server_mode);
|
||||||
{
|
s.~conv_manager_t<u64_t>();
|
||||||
if(program_mode==client_mode)
|
}
|
||||||
{
|
}
|
||||||
c.~conv_manager_t<address_t>();
|
} conv_manager;
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
assert(program_mode==server_mode);
|
|
||||||
s.~conv_manager_t<u64_t>();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}conv_manager;
|
|
||||||
|
|
||||||
anti_replay_t anti_replay;//anti_replay_t is here bc its huge,its allocation is delayed.
|
anti_replay_t anti_replay; // anti_replay_t is here bc its huge,its allocation is delayed.
|
||||||
};
|
};
|
||||||
struct conn_info_t //stores info for a raw connection.for client ,there is only one connection,for server there can be thousand of connection since server can
|
struct conn_info_t // stores info for a raw connection.for client ,there is only one connection,for server there can be thousand of connection since server can
|
||||||
//handle multiple clients
|
// handle multiple clients
|
||||||
{
|
{
|
||||||
current_state_t state;
|
current_state_t state;
|
||||||
|
|
||||||
raw_info_t raw_info;
|
raw_info_t raw_info;
|
||||||
u64_t last_state_time;
|
u64_t last_state_time;
|
||||||
u64_t last_hb_sent_time; //client re-use this for retry
|
u64_t last_hb_sent_time; // client re-use this for retry
|
||||||
u64_t last_hb_recv_time;
|
u64_t last_hb_recv_time;
|
||||||
//long long last_resent_time;
|
// long long last_resent_time;
|
||||||
|
|
||||||
my_id_t my_id;
|
my_id_t my_id;
|
||||||
my_id_t oppsite_id;
|
my_id_t oppsite_id;
|
||||||
|
|
||||||
|
fd64_t timer_fd64;
|
||||||
|
fd64_t udp_fd64;
|
||||||
|
|
||||||
fd64_t timer_fd64;
|
my_id_t oppsite_const_id;
|
||||||
fd64_t udp_fd64;
|
|
||||||
|
|
||||||
my_id_t oppsite_const_id;
|
blob_t *blob;
|
||||||
|
|
||||||
blob_t *blob;
|
uint8_t my_roller;
|
||||||
|
uint8_t oppsite_roller;
|
||||||
|
u64_t last_oppsite_roller_time;
|
||||||
|
|
||||||
uint8_t my_roller;
|
// ip_port_t ip_port;
|
||||||
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;
|
||||||
|
|
||||||
/*
|
*/
|
||||||
const uint32_t &ip=raw_info.recv_info.src_ip;
|
void recover(const conn_info_t &conn_info);
|
||||||
const uint16_t &port=raw_info.recv_info.src_port;
|
void re_init();
|
||||||
*/
|
conn_info_t();
|
||||||
void recover(const conn_info_t &conn_info);
|
void prepare();
|
||||||
void re_init();
|
conn_info_t(const conn_info_t &b);
|
||||||
conn_info_t();
|
conn_info_t &operator=(const conn_info_t &b);
|
||||||
void prepare();
|
~conn_info_t();
|
||||||
conn_info_t(const conn_info_t&b);
|
}; // g_conn_info;
|
||||||
conn_info_t& operator=(const conn_info_t& b);
|
|
||||||
~conn_info_t();
|
|
||||||
};//g_conn_info;
|
|
||||||
|
|
||||||
struct conn_manager_t //manager for connections. for client,we dont need conn_manager since there is only one connection.for server we use one conn_manager for all connections
|
struct conn_manager_t // manager for connections. for client,we dont need conn_manager since there is only one connection.for server we use one conn_manager for all connections
|
||||||
{
|
{
|
||||||
|
u32_t ready_num;
|
||||||
|
|
||||||
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<my_id_t, conn_info_t *> const_id_mp;
|
||||||
//unordered_map<int,conn_info_t *> timer_fd_mp;//we can use pointer here since unordered_map.rehash() uses shallow copy
|
|
||||||
|
|
||||||
unordered_map<my_id_t,conn_info_t *> const_id_mp;
|
unordered_map<address_t, conn_info_t *> mp; // put it at end so that it de-consturcts first
|
||||||
|
|
||||||
unordered_map<address_t,conn_info_t*> mp; //put it at end so that it de-consturcts first
|
// lru_collector_t<address_t> lru;
|
||||||
|
|
||||||
//lru_collector_t<address_t> lru;
|
unordered_map<address_t, conn_info_t *>::iterator clear_it;
|
||||||
|
|
||||||
unordered_map<address_t,conn_info_t*>::iterator clear_it;
|
long long last_clear_time;
|
||||||
|
|
||||||
long long last_clear_time;
|
conn_manager_t();
|
||||||
|
int exist(address_t addr);
|
||||||
conn_manager_t();
|
/*
|
||||||
int exist(address_t addr);
|
int insert(uint32_t ip,uint16_t port)
|
||||||
/*
|
{
|
||||||
int insert(uint32_t ip,uint16_t port)
|
uint64_t u64=0;
|
||||||
{
|
u64=ip;
|
||||||
uint64_t u64=0;
|
u64<<=32u;
|
||||||
u64=ip;
|
u64|=port;
|
||||||
u64<<=32u;
|
mp[u64];
|
||||||
u64|=port;
|
return 0;
|
||||||
mp[u64];
|
}*/
|
||||||
return 0;
|
conn_info_t *&find_insert_p(address_t addr); // be aware,the adress may change after rehash //not true?
|
||||||
}*/
|
conn_info_t &find_insert(address_t addr); // be aware,the adress may change after rehash
|
||||||
conn_info_t *& find_insert_p(address_t addr); //be aware,the adress may change after rehash //not true?
|
|
||||||
conn_info_t & find_insert(address_t addr) ; //be aware,the adress may change after rehash
|
|
||||||
|
|
||||||
int erase(unordered_map<address_t,conn_info_t*>::iterator erase_it);
|
|
||||||
int clear_inactive();
|
|
||||||
int clear_inactive0();
|
|
||||||
|
|
||||||
|
int erase(unordered_map<address_t, conn_info_t *>::iterator erase_it);
|
||||||
|
int clear_inactive();
|
||||||
|
int clear_inactive0();
|
||||||
};
|
};
|
||||||
|
|
||||||
extern conn_manager_t conn_manager;
|
extern conn_manager_t conn_manager;
|
||||||
|
|
||||||
void server_clear_function(u64_t u64);
|
void server_clear_function(u64_t u64);
|
||||||
|
|
||||||
int send_bare(raw_info_t &raw_info,const char* data,int len);//send function with encryption but no anti replay,this is used when client and server verifys each other
|
int send_bare(raw_info_t &raw_info, const char *data, int len); // send function with encryption but no anti replay,this is used when client and server verifys each other
|
||||||
//you have to design the protocol carefully, so that you wont be affect by relay attack
|
// you have to design the protocol carefully, so that you wont be affect by relay attack
|
||||||
//int reserved_parse_bare(const char *input,int input_len,char* & data,int & len); // a sub function used in recv_bare
|
// int reserved_parse_bare(const char *input,int input_len,char* & data,int & len); // a sub function used in recv_bare
|
||||||
int recv_bare(raw_info_t &raw_info,char* & data,int & len);//recv function with encryption but no anti replay,this is used when client and server verifys each other
|
int recv_bare(raw_info_t &raw_info, char *&data, int &len); // recv function with encryption but no anti replay,this is used when client and server verifys each other
|
||||||
//you have to design the protocol carefully, so that you wont be affect by relay attack
|
// you have to design the protocol carefully, so that you wont be affect by relay attack
|
||||||
int send_handshake(raw_info_t &raw_info,my_id_t id1,my_id_t id2,my_id_t id3);// a warp for send_bare for sending handshake(this is not tcp handshake) easily
|
int send_handshake(raw_info_t &raw_info, my_id_t id1, my_id_t id2, my_id_t id3); // a warp for send_bare for sending handshake(this is not tcp handshake) easily
|
||||||
int send_safer(conn_info_t &conn_info,char type,const char* data,int len); //safer transfer function with anti-replay,when mutually verification is done.
|
int send_safer(conn_info_t &conn_info, char type, const char *data, int len); // safer transfer function with anti-replay,when mutually verification is done.
|
||||||
int send_data_safer(conn_info_t &conn_info,const char* data,int len,u32_t conv_num);//a wrap for send_safer for transfer data.
|
int send_data_safer(conn_info_t &conn_info, const char *data, int len, u32_t conv_num); // a wrap for send_safer for transfer data.
|
||||||
//int reserved_parse_safer(conn_info_t &conn_info,const char * input,int input_len,char &type,char* &data,int &len);//subfunction for recv_safer,allow overlap
|
// int reserved_parse_safer(conn_info_t &conn_info,const char * input,int input_len,char &type,char* &data,int &len);//subfunction for recv_safer,allow overlap
|
||||||
|
|
||||||
//int recv_safer(conn_info_t &conn_info,char &type,char* &data,int &len);///safer transfer function with anti-replay,when mutually verification is done.
|
// int recv_safer(conn_info_t &conn_info,char &type,char* &data,int &len);///safer transfer function with anti-replay,when mutually verification is done.
|
||||||
|
|
||||||
int recv_safer_multi(conn_info_t &conn_info,vector<char> &type_arr,vector<string> &data_arr);//new api for handle gro
|
int recv_safer_multi(conn_info_t &conn_info, vector<char> &type_arr, vector<string> &data_arr); // new api for handle gro
|
||||||
#endif /* CONNECTION_H_ */
|
#endif /* CONNECTION_H_ */
|
||||||
|
@@ -14,23 +14,19 @@ udp2raw tunnel,通过raw socket给UDP包加上TCP或ICMP header,进而绕过
|
|||||||
|
|
||||||
**提示:**
|
**提示:**
|
||||||
|
|
||||||
udp2raw不是加速器,只是一个帮助你绕过UDP限制的工具。如果你需要UDP加速器,请看UDPspeeder。
|
udp2raw不是加速器,只是一个帮助你绕过UDP限制的工具。如果你需要UDP“加速器” (改善UDP丢包),请看UDPspeeder。
|
||||||
|
|
||||||
UDPspeeder的repo:
|
UDPspeeder的repo:
|
||||||
|
|
||||||
https://github.com/wangyu-/UDPspeeder
|
https://github.com/wangyu-/UDPspeeder
|
||||||
# 支持的平台
|
# 支持的平台
|
||||||
Linux主机,有root权限。可以是PC、android手机/平板、openwrt路由器、树莓派。主机上最好安装了iptables命令(apt/yum很容易安装)。
|
Linux主机,有root权限或cap_net_raw capability.。可以是PC、android手机/平板、openwrt路由器、树莓派。主机上最好安装了iptables命令(apt/yum很容易安装)。
|
||||||
|
|
||||||
Release中提供了`amd64`、`x86`、`arm`、`mips_be`、`mips_le`的预编译binary.
|
Release中提供了`amd64`、`x86`、`arm`、`mips_be`、`mips_le`的预编译binary.
|
||||||
|
|
||||||
##### 对于windows和mac用户:
|
##### 对于windows和mac用户:
|
||||||
|
|
||||||
可以把udp2raw运行在虚拟机上(网络必须是桥接模式)。
|
可以用[这个repo](https://github.com/wangyu-/udp2raw-multiplatform)里的udp2raw。
|
||||||
|
|
||||||
另外可以参考:
|
|
||||||
|
|
||||||
https://github.com/wangyu-/udp2raw-tunnel/wiki/在windows-mac上运行udp2raw客户端,带图形界面
|
|
||||||
|
|
||||||
##### 对于ios和游戏主机用户:
|
##### 对于ios和游戏主机用户:
|
||||||
|
|
||||||
@@ -43,17 +39,13 @@ https://github.com/wangyu-/udp2raw-tunnel/wiki/在windows-mac上运行udp2raw客
|
|||||||
### 模拟TCP3次握手
|
### 模拟TCP3次握手
|
||||||
模拟TCP3次握手,模拟seq ack过程。另外还模拟了一些tcp option:MSS,sackOk,TS,TS_ack,wscale,用来使流量看起来更像是由普通的linux tcp协议栈发送的。
|
模拟TCP3次握手,模拟seq ack过程。另外还模拟了一些tcp option:MSS,sackOk,TS,TS_ack,wscale,用来使流量看起来更像是由普通的linux tcp协议栈发送的。
|
||||||
|
|
||||||
### 心跳保活、自动重连,连接快速恢复,单向链路失效检测
|
### 心跳保活、自动重连,连接恢复
|
||||||
心跳保活、自动重连,udp2raw重连可以恢复上次的连接,重连后上层连接继续有效,底层掉线上层不掉线。有效解决上层连接断开的问题。 (功能借鉴自[kcptun-raw](https://github.com/Chion82/kcptun-raw))(**就算你拔掉网线重插,或者重新拨号获得新ip,上层应用也不会断线**)
|
心跳保活、自动重连,udp2raw重连可以恢复上次的连接,重连后上层连接继续有效,底层掉线上层不掉线。有效解决上层连接断开的问题。 (功能借鉴自[kcptun-raw](https://github.com/Chion82/kcptun-raw))(**就算你拔掉网线重插,或者重新拨号获得新ip,上层应用也不会断线**)
|
||||||
|
|
||||||
Client能用单倍的超时时间检测到单向链路的失效,不管是上行还是下行,只要有一个方向失效就能被client检测到。重连只需要client发起,就可以立即被server处理,不需要等到server端的连接超时后。
|
### 加密、防重放攻击
|
||||||
|
用aes128cbc加密(或更弱的xor),hmac-sha1(或更弱的md5/crc32/simple)做数据完整校验。用类似ipsec/openvpn的replay window机制来防止重放攻击。
|
||||||
|
|
||||||
对于有大量client的情况,对于不同client,server发送的心跳是错开时间发送的,不会因为短时间发送大量的心跳而造成拥塞和延迟抖动。
|
[Notes on encryption](https://github.com/wangyu-/udp2raw-tunnel/wiki/Notes-on-encryption)
|
||||||
|
|
||||||
### 加密 防重放攻击
|
|
||||||
用aes128cbc加密,md5/crc32做数据完整校验。用类似ipsec/openvpn的 replay window机制来防止重放攻击。
|
|
||||||
|
|
||||||
设计目标是,即使攻击者可以监听到tunnel的所有包,可以选择性丢弃tunnel的任意包,可以重放任意包;攻击者也没办法获得tunnel承载的任何数据,也没办法向tunnel的数据流中通过包构造/包重放插入任何数据。
|
|
||||||
|
|
||||||
### 其他特性
|
### 其他特性
|
||||||
信道复用,client的udp端支持多个连接。
|
信道复用,client的udp端支持多个连接。
|
||||||
@@ -62,12 +54,10 @@ server支持多个client,也能正确处理多个连接的重连和连接恢
|
|||||||
|
|
||||||
NAT 穿透 ,tcp icmp udp模式都支持nat穿透。
|
NAT 穿透 ,tcp icmp udp模式都支持nat穿透。
|
||||||
|
|
||||||
支持Openvz,配合finalspeed使用,可以在openvz上用tcp模式的finalspeed
|
支持Openvz,配合finalspeed使用,可以在openvz上用tcp模式的finalspeed.
|
||||||
|
|
||||||
支持Openwrt,没有编译依赖,容易编译到任何平台上。
|
支持Openwrt,没有编译依赖,容易编译到任何平台上。
|
||||||
|
|
||||||
epoll实现,高并发,除了回收过期连接外,所有操作的时间复杂度都跟连接数无关。回收过期连接的操做也是柔和进行的,不会因为消耗太多cpu时间造成延迟抖动。
|
|
||||||
|
|
||||||
### 关键词
|
### 关键词
|
||||||
突破udp qos,突破udp屏蔽,openvpn tcp over tcp problem,openvpn over icmp,udp to icmp tunnel,udp to tcp tunnel,udp via icmp,udp via tcp
|
突破udp qos,突破udp屏蔽,openvpn tcp over tcp problem,openvpn over icmp,udp to icmp tunnel,udp to tcp tunnel,udp via icmp,udp via tcp
|
||||||
|
|
||||||
@@ -83,10 +73,10 @@ https://github.com/wangyu-/udp2raw-tunnel/releases
|
|||||||
|
|
||||||
```
|
```
|
||||||
在server端运行:
|
在server端运行:
|
||||||
./udp2raw_amd64 -s -l0.0.0.0:4096 -r127.0.0.1:7777 -a -k "passwd" --raw-mode faketcp --cipher-mode xor
|
./udp2raw_amd64 -s -l0.0.0.0:4096 -r127.0.0.1:7777 -k "passwd" --raw-mode faketcp --cipher-mode xor -a
|
||||||
|
|
||||||
在client端运行:
|
在client端运行:
|
||||||
./udp2raw_amd64 -c -l0.0.0.0:3333 -r44.55.66.77:4096 -a -k "passwd" --raw-mode faketcp --cipher-mode xor
|
./udp2raw_amd64 -c -l0.0.0.0:3333 -r44.55.66.77:4096 -k "passwd" --raw-mode faketcp --cipher-mode xor -a
|
||||||
```
|
```
|
||||||
(以上例子需要用root账号运行。 用非root运行udp2raw需要一些额外的步骤,具体方法请看 [这个](https://github.com/wangyu-/udp2raw-tunnel/wiki/run-udp2raw-as-non-root) 链接。用非root运行更安全)
|
(以上例子需要用root账号运行。 用非root运行udp2raw需要一些额外的步骤,具体方法请看 [这个](https://github.com/wangyu-/udp2raw-tunnel/wiki/run-udp2raw-as-non-root) 链接。用非root运行更安全)
|
||||||
|
|
||||||
@@ -106,9 +96,7 @@ https://github.com/wangyu-/udp2raw-tunnel/releases
|
|||||||
|
|
||||||
如果要在anroid上运行,请看[Android简明教程](/doc/android_guide.md)
|
如果要在anroid上运行,请看[Android简明教程](/doc/android_guide.md)
|
||||||
|
|
||||||
如果要在梅林固件的路由器上使用,添加`--lower-level auto` `--keep-rule`
|
`-a`选项会自动添加一条/几条iptables规则,udp2raw必须和相应的iptables规则配合才能稳定工作,一定要注意不要忘了`-a`(这是个常见错误)。 如果你不想让udp2raw自动添加iptables规则,可以自己手动添加相应的iptables规则(看一下`-g`选项),然后以不带`-a`的方式运行udp2raw。
|
||||||
|
|
||||||
如果client和server无法连接,或者连接经常断开,请看一下`--seq-mode`的用法,尝试不同的seq-mode。
|
|
||||||
|
|
||||||
# 进阶操作说明
|
# 进阶操作说明
|
||||||
|
|
||||||
@@ -119,14 +107,14 @@ git version:6e1df4b39f build date:Oct 24 2017 09:21:15
|
|||||||
repository: https://github.com/wangyu-/udp2raw-tunnel
|
repository: https://github.com/wangyu-/udp2raw-tunnel
|
||||||
|
|
||||||
usage:
|
usage:
|
||||||
run as client : ./this_program -c -l local_listen_ip:local_port -r server_address:server_port [options]
|
run as client : ./this_program -c -l local_listen_ip:local_port -r server_address:server_port [options]
|
||||||
run as server : ./this_program -s -l server_listen_ip:server_port -r remote_address:remote_port [options]
|
run as server : ./this_program -s -l server_listen_ip:server_port -r remote_address:remote_port [options]
|
||||||
|
|
||||||
common options,these options must be same on both side:
|
common options,these options must be same on both side:
|
||||||
--raw-mode <string> avaliable values:faketcp(default),udp,icmp
|
--raw-mode <string> available values:faketcp(default),udp,icmp
|
||||||
-k,--key <string> password to gen symetric key,default:"secret key"
|
-k,--key <string> password to gen symetric key,default:"secret key"
|
||||||
--cipher-mode <string> avaliable values:aes128cbc(default),xor,none
|
--cipher-mode <string> available values:aes128cbc(default),xor,none
|
||||||
--auth-mode <string> avaliable values:md5(default),crc32,simple,none
|
--auth-mode <string> available values:hmac_sha1,md5(default),crc32,simple,none
|
||||||
-a,--auto-rule auto add (and delete) iptables rule
|
-a,--auto-rule auto add (and delete) iptables rule
|
||||||
-g,--gen-rule generate iptables rule then exit,so that you can copy and
|
-g,--gen-rule generate iptables rule then exit,so that you can copy and
|
||||||
add it manually.overrides -a
|
add it manually.overrides -a
|
||||||
@@ -170,10 +158,10 @@ other options:
|
|||||||
|
|
||||||
用raw收发udp包也类似,只是内核回复的是icmp unreachable。而用raw 收发icmp,内核会自动回复icmp echo。都需要相应的iptables规则。
|
用raw收发udp包也类似,只是内核回复的是icmp unreachable。而用raw 收发icmp,内核会自动回复icmp echo。都需要相应的iptables规则。
|
||||||
### `--cipher-mode` 和 `--auth-mode`
|
### `--cipher-mode` 和 `--auth-mode`
|
||||||
如果要最大的安全性建议用aes128cbc+md5。如果要运行在路由器上,建议用xor+simple,可以节省CPU。但是注意xor+simple只能骗过防火墙的包检测,不能防止真正的攻击者。
|
如果要最大的安全性建议用aes128cbc+hmac_sha1。如果要运行在路由器上,建议用xor+simple,可以节省CPU。但是注意xor+simple只能骗过防火墙的包检测,不能防止真正的攻击者。
|
||||||
|
|
||||||
### `--seq-mode`
|
### `--seq-mode`
|
||||||
facktcp模式并没有模拟tcp的全部。所以理论上有办法把faketcp和真正的tcp流量区分开来(虽然大部分ISP不太可能做这种程度的包检测)。seq-mode可以改变一些seq ack的行为。如果遇到了连接问题,可以尝试更改。在我这边的移动线路用3种模式都没问题。
|
faketcp模式并没有模拟tcp的全部。所以理论上有办法把faketcp和真正的tcp流量区分开来(虽然大部分ISP不太可能做这种程度的包检测)。seq-mode可以改变一些seq ack的行为。如果遇到了连接问题,可以尝试更改。在我这边的移动线路用3种模式都没问题。
|
||||||
|
|
||||||
### `--keep-rule`
|
### `--keep-rule`
|
||||||
定期主动检查iptables,如果udp2raw添加的iptables规则丢了,就重新添加。在一些iptables可能会被其他程序清空的情况下(比如梅林固件和openwrt的路由器)格外有用。
|
定期主动检查iptables,如果udp2raw添加的iptables规则丢了,就重新添加。在一些iptables可能会被其他程序清空的情况下(比如梅林固件和openwrt的路由器)格外有用。
|
||||||
@@ -274,25 +262,6 @@ raw_mode: faketcp cipher_mode: aes128cbc auth_mode: md5
|
|||||||
[udp2raw+kcptun step_by_step教程](kcptun_step_by_step.md)
|
[udp2raw+kcptun step_by_step教程](kcptun_step_by_step.md)
|
||||||
### 中转 finalspeed
|
### 中转 finalspeed
|
||||||
[udp2raw+finalspeed step_by_step教程](finalspeed_step_by_step.md)
|
[udp2raw+finalspeed step_by_step教程](finalspeed_step_by_step.md)
|
||||||
# 如何自己编译
|
|
||||||
[编译教程](build_guide.zh-cn.md)
|
|
||||||
# 相关repo
|
|
||||||
### kcptun-raw
|
|
||||||
udp2raw was inspired by kcptun-raw,which modified kcptun to support tcp mode.
|
|
||||||
|
|
||||||
https://github.com/Chion82/kcptun-raw
|
|
||||||
### relayRawSocket
|
|
||||||
kcptun-raw was inspired by relayRawSocket. A simple udp to raw tunnel,wrote in python
|
|
||||||
|
|
||||||
https://github.com/linhua55/some_kcptun_tools/tree/master/relayRawSocket
|
|
||||||
### kcpraw
|
|
||||||
another project of kcptun with tcp mode
|
|
||||||
|
|
||||||
https://github.com/ccsexyz/kcpraw
|
|
||||||
### icmptunnel
|
|
||||||
Transparently tunnel your IP traffic through ICMP echo and reply packets.
|
|
||||||
|
|
||||||
https://github.com/DhavalKapil/icmptunnel
|
|
||||||
|
|
||||||
# wiki
|
# wiki
|
||||||
|
|
||||||
|
912
encrypt.cpp
Executable file → Normal file
44
encrypt.h
Executable file → Normal file
@@ -1,33 +1,35 @@
|
|||||||
#ifndef UDP2RAW_ENCRYPTION_H_
|
#ifndef UDP2RAW_ENCRYPTION_H_
|
||||||
#define UDP2RAW_ENCRYPTION_H_
|
#define UDP2RAW_ENCRYPTION_H_
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//#include "aes.h"
|
//#include "aes.h"
|
||||||
//#include "md5.h"
|
//#include "md5.h"
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
|
|
||||||
|
// using namespace std;
|
||||||
|
// extern char key[16];
|
||||||
|
|
||||||
//using namespace std;
|
const int aes_key_optimize = 1; // if enabled,once you used a key for aes,you cant change it anymore
|
||||||
//extern char key[16];
|
|
||||||
|
|
||||||
const int aes_key_optimize=1; //if enabled,once you used a key for aes,you cant change it anymore
|
|
||||||
extern int aes128cfb_old;
|
extern int aes128cfb_old;
|
||||||
|
|
||||||
int my_init_keys(const char *,int);
|
int my_init_keys(const char *, int);
|
||||||
|
|
||||||
int my_encrypt(const char *data,char *output,int &len);
|
int my_encrypt(const char *data, char *output, int &len);
|
||||||
int my_decrypt(const char *data,char *output,int &len);
|
int my_decrypt(const char *data, char *output, int &len);
|
||||||
|
|
||||||
|
unsigned short csum(const unsigned short *ptr, int nbytes);
|
||||||
|
|
||||||
unsigned short csum(const unsigned short *ptr,int nbytes) ;
|
enum auth_mode_t { auth_none = 0,
|
||||||
|
auth_md5,
|
||||||
|
auth_crc32,
|
||||||
enum auth_mode_t {auth_none=0,auth_md5,auth_crc32,auth_simple,auth_hmac_sha1,auth_end};
|
auth_simple,
|
||||||
|
auth_hmac_sha1,
|
||||||
|
auth_end };
|
||||||
enum cipher_mode_t {cipher_none=0,cipher_aes128cbc,cipher_xor,cipher_aes128cfb,cipher_end};
|
|
||||||
|
|
||||||
|
enum cipher_mode_t { cipher_none = 0,
|
||||||
|
cipher_aes128cbc,
|
||||||
|
cipher_xor,
|
||||||
|
cipher_aes128cfb,
|
||||||
|
cipher_end };
|
||||||
|
|
||||||
extern auth_mode_t auth_mode;
|
extern auth_mode_t auth_mode;
|
||||||
extern cipher_mode_t cipher_mode;
|
extern cipher_mode_t cipher_mode;
|
||||||
@@ -35,13 +37,13 @@ extern cipher_mode_t cipher_mode;
|
|||||||
extern unordered_map<int, const char *> auth_mode_tostring;
|
extern unordered_map<int, const char *> auth_mode_tostring;
|
||||||
extern unordered_map<int, const char *> cipher_mode_tostring;
|
extern unordered_map<int, const char *> cipher_mode_tostring;
|
||||||
|
|
||||||
extern char gro_xor[256+100];
|
extern char gro_xor[256 + 100];
|
||||||
|
|
||||||
int cipher_decrypt(const char *data,char *output,int &len,char * key);//internal interface ,exposed for test only
|
int cipher_decrypt(const char *data, char *output, int &len, char *key); // internal interface ,exposed for test only
|
||||||
int cipher_encrypt(const char *data,char *output,int &len,char * key);//internal interface ,exposed for test only
|
int cipher_encrypt(const char *data, char *output, int &len, char *key); // internal interface ,exposed for test only
|
||||||
|
|
||||||
void aes_ecb_encrypt(const char *data,char *output);
|
void aes_ecb_encrypt(const char *data, char *output);
|
||||||
void aes_ecb_decrypt(const char *data,char *output);
|
void aes_ecb_decrypt(const char *data, char *output);
|
||||||
|
|
||||||
void aes_ecb_encrypt1(char *data);
|
void aes_ecb_encrypt1(char *data);
|
||||||
void aes_ecb_decrypt1(char *data);
|
void aes_ecb_decrypt1(char *data);
|
||||||
|
@@ -5,60 +5,49 @@
|
|||||||
* Author: root
|
* Author: root
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#include "fd_manager.h"
|
#include "fd_manager.h"
|
||||||
int fd_manager_t::fd_exist(int fd)
|
int fd_manager_t::fd_exist(int fd) {
|
||||||
{
|
return fd_to_fd64_mp.find(fd) != fd_to_fd64_mp.end();
|
||||||
return fd_to_fd64_mp.find(fd)!=fd_to_fd64_mp.end();
|
|
||||||
}
|
}
|
||||||
int fd_manager_t::exist(fd64_t fd64)
|
int fd_manager_t::exist(fd64_t fd64) {
|
||||||
{
|
return fd64_to_fd_mp.find(fd64) != fd64_to_fd_mp.end();
|
||||||
return fd64_to_fd_mp.find(fd64)!=fd64_to_fd_mp.end();
|
|
||||||
}
|
}
|
||||||
int fd_manager_t::to_fd(fd64_t fd64)
|
int fd_manager_t::to_fd(fd64_t fd64) {
|
||||||
{
|
assert(exist(fd64));
|
||||||
assert(exist(fd64));
|
return fd64_to_fd_mp[fd64];
|
||||||
return fd64_to_fd_mp[fd64];
|
|
||||||
}
|
}
|
||||||
void fd_manager_t::fd64_close(fd64_t fd64)
|
void fd_manager_t::fd64_close(fd64_t fd64) {
|
||||||
{
|
assert(exist(fd64));
|
||||||
assert(exist(fd64));
|
int fd = fd64_to_fd_mp[fd64];
|
||||||
int fd=fd64_to_fd_mp[fd64];
|
fd64_to_fd_mp.erase(fd64);
|
||||||
fd64_to_fd_mp.erase(fd64);
|
fd_to_fd64_mp.erase(fd);
|
||||||
fd_to_fd64_mp.erase(fd);
|
if (exist_info(fd64)) {
|
||||||
if(exist_info(fd64))
|
fd_info_mp.erase(fd64);
|
||||||
{
|
}
|
||||||
fd_info_mp.erase(fd64);
|
// assert(close(fd)==0);
|
||||||
}
|
sock_close(fd);
|
||||||
//assert(close(fd)==0);
|
|
||||||
sock_close(fd);
|
|
||||||
}
|
}
|
||||||
void fd_manager_t::reserve(int n)
|
void fd_manager_t::reserve(int n) {
|
||||||
{
|
fd_to_fd64_mp.reserve(n);
|
||||||
fd_to_fd64_mp.reserve(n);
|
fd64_to_fd_mp.reserve(n);
|
||||||
fd64_to_fd_mp.reserve(n);
|
fd_info_mp.reserve(n);
|
||||||
fd_info_mp.reserve(n);
|
|
||||||
}
|
}
|
||||||
u64_t fd_manager_t::create(int fd)
|
u64_t fd_manager_t::create(int fd) {
|
||||||
{
|
assert(!fd_exist(fd));
|
||||||
assert(!fd_exist(fd));
|
fd64_t fd64 = counter++;
|
||||||
fd64_t fd64=counter++;
|
fd_to_fd64_mp[fd] = fd64;
|
||||||
fd_to_fd64_mp[fd]=fd64;
|
fd64_to_fd_mp[fd64] = fd;
|
||||||
fd64_to_fd_mp[fd64]=fd;
|
return fd64;
|
||||||
return fd64;
|
|
||||||
}
|
}
|
||||||
fd_manager_t::fd_manager_t()
|
fd_manager_t::fd_manager_t() {
|
||||||
{
|
counter = u32_t(-1);
|
||||||
counter=u32_t(-1);
|
counter += 100;
|
||||||
counter+=100;
|
reserve(10007);
|
||||||
reserve(10007);
|
|
||||||
}
|
}
|
||||||
fd_info_t & fd_manager_t::get_info(fd64_t fd64)
|
fd_info_t& fd_manager_t::get_info(fd64_t fd64) {
|
||||||
{
|
assert(exist(fd64));
|
||||||
assert(exist(fd64));
|
return fd_info_mp[fd64];
|
||||||
return fd_info_mp[fd64];
|
|
||||||
}
|
}
|
||||||
int fd_manager_t::exist_info(fd64_t fd64)
|
int fd_manager_t::exist_info(fd64_t fd64) {
|
||||||
{
|
return fd_info_mp.find(fd64) != fd_info_mp.end();
|
||||||
return fd_info_mp.find(fd64)!=fd_info_mp.end();
|
|
||||||
}
|
}
|
||||||
|
44
fd_manager.h
@@ -12,31 +12,31 @@
|
|||||||
//#include "packet.h"
|
//#include "packet.h"
|
||||||
#include "connection.h"
|
#include "connection.h"
|
||||||
|
|
||||||
struct fd_info_t
|
struct fd_info_t {
|
||||||
{
|
// ip_port_t ip_port;
|
||||||
//ip_port_t ip_port;
|
conn_info_t *p_conn_info;
|
||||||
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
|
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
|
// this class is not strictly necessary,it just makes epoll fd handling easier
|
||||||
{
|
{
|
||||||
fd_info_t & get_info(fd64_t fd64);
|
fd_info_t &get_info(fd64_t fd64);
|
||||||
int exist_info(fd64_t);
|
int exist_info(fd64_t);
|
||||||
int exist(fd64_t fd64);
|
int exist(fd64_t fd64);
|
||||||
int to_fd(fd64_t);
|
int to_fd(fd64_t);
|
||||||
void fd64_close(fd64_t fd64);
|
void fd64_close(fd64_t fd64);
|
||||||
void reserve(int n);
|
void reserve(int n);
|
||||||
u64_t create(int fd);
|
u64_t create(int fd);
|
||||||
fd_manager_t();
|
fd_manager_t();
|
||||||
private:
|
|
||||||
u64_t counter;
|
private:
|
||||||
unordered_map<int,fd64_t> fd_to_fd64_mp;
|
u64_t counter;
|
||||||
unordered_map<fd64_t,int> fd64_to_fd_mp;
|
unordered_map<int, fd64_t> fd_to_fd64_mp;
|
||||||
unordered_map<fd64_t,fd_info_t> fd_info_mp;
|
unordered_map<fd64_t, int> fd64_to_fd_mp;
|
||||||
int fd_exist(int fd);
|
unordered_map<fd64_t, fd_info_t> fd_info_mp;
|
||||||
//void remove_fd(int fd);
|
int fd_exist(int fd);
|
||||||
//fd64_t fd_to_fd64(int fd);
|
// void remove_fd(int fd);
|
||||||
|
// fd64_t fd_to_fd64(int fd);
|
||||||
};
|
};
|
||||||
|
|
||||||
extern fd_manager_t fd_manager;
|
extern fd_manager_t fd_manager;
|
||||||
|
@@ -1 +0,0 @@
|
|||||||
|
|
Before Width: | Height: | Size: 54 KiB |
Before Width: | Height: | Size: 67 KiB |
Before Width: | Height: | Size: 31 KiB |
Before Width: | Height: | Size: 46 KiB |
Before Width: | Height: | Size: 46 KiB |
Before Width: | Height: | Size: 56 KiB |
Before Width: | Height: | Size: 48 KiB |
Before Width: | Height: | Size: 17 KiB |
87
log.cpp
Executable file → Normal file
@@ -1,62 +1,57 @@
|
|||||||
#include "log.h"
|
#include "log.h"
|
||||||
#include "misc.h"
|
#include "misc.h"
|
||||||
|
|
||||||
int log_level=log_info;
|
int log_level = log_info;
|
||||||
|
|
||||||
int enable_log_position=0;
|
int enable_log_position = 0;
|
||||||
int enable_log_color=1;
|
int enable_log_color = 1;
|
||||||
|
|
||||||
void log0(const char * file,const char * function,int line,int level,const char* str, ...) {
|
void log0(const char* file, const char* function, int line, int level, const char* str, ...) {
|
||||||
|
if (level > log_level) return;
|
||||||
|
if (level > log_trace || level < 0) return;
|
||||||
|
|
||||||
if(level>log_level) return ;
|
time_t timer;
|
||||||
if(level>log_trace||level<0) return ;
|
char buffer[100];
|
||||||
|
struct tm* tm_info;
|
||||||
|
|
||||||
|
time(&timer);
|
||||||
|
tm_info = localtime(&timer);
|
||||||
|
|
||||||
time_t timer;
|
if (enable_log_color)
|
||||||
char buffer[100];
|
printf("%s", log_color[level]);
|
||||||
struct tm* tm_info;
|
|
||||||
|
|
||||||
time(&timer);
|
strftime(buffer, 100, "%Y-%m-%d %H:%M:%S", tm_info);
|
||||||
tm_info = localtime(&timer);
|
printf("[%s][%s]", buffer, log_text[level]);
|
||||||
|
|
||||||
if(enable_log_color)
|
if (enable_log_position) printf("[%s,func:%s,line:%d]", file, function, line);
|
||||||
printf("%s",log_color[level]);
|
|
||||||
|
|
||||||
strftime(buffer, 100, "%Y-%m-%d %H:%M:%S", tm_info);
|
va_list vlist;
|
||||||
printf("[%s][%s]",buffer,log_text[level]);
|
va_start(vlist, str);
|
||||||
|
vfprintf(stdout, str, vlist);
|
||||||
|
va_end(vlist);
|
||||||
|
if (enable_log_color)
|
||||||
|
printf("%s", RESET);
|
||||||
|
|
||||||
if(enable_log_position)printf("[%s,func:%s,line:%d]",file,function,line);
|
// printf("\n");
|
||||||
|
// if(enable_log_color)
|
||||||
|
// printf(log_color[level]);
|
||||||
|
fflush(stdout);
|
||||||
|
|
||||||
va_list vlist;
|
if (log_level == log_fatal) {
|
||||||
va_start(vlist, str);
|
about_to_exit = 1;
|
||||||
vfprintf(stdout, str, vlist);
|
}
|
||||||
va_end(vlist);
|
|
||||||
if(enable_log_color)
|
|
||||||
printf("%s",RESET);
|
|
||||||
|
|
||||||
//printf("\n");
|
|
||||||
//if(enable_log_color)
|
|
||||||
//printf(log_color[level]);
|
|
||||||
fflush(stdout);
|
|
||||||
|
|
||||||
if(log_level==log_fatal)
|
|
||||||
{
|
|
||||||
about_to_exit=1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void log_bare(int level,const char* str, ...)
|
void log_bare(int level, const char* str, ...) {
|
||||||
{
|
if (level > log_level) return;
|
||||||
if(level>log_level) return ;
|
if (level > log_trace || level < 0) return;
|
||||||
if(level>log_trace||level<0) return ;
|
if (enable_log_color)
|
||||||
if(enable_log_color)
|
printf("%s", log_color[level]);
|
||||||
printf("%s",log_color[level]);
|
va_list vlist;
|
||||||
va_list vlist;
|
va_start(vlist, str);
|
||||||
va_start(vlist, str);
|
vfprintf(stdout, str, vlist);
|
||||||
vfprintf(stdout, str, vlist);
|
va_end(vlist);
|
||||||
va_end(vlist);
|
if (enable_log_color)
|
||||||
if(enable_log_color)
|
printf("%s", RESET);
|
||||||
printf("%s",RESET);
|
fflush(stdout);
|
||||||
fflush(stdout);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
49
log.h
Executable file → Normal file
@@ -2,53 +2,46 @@
|
|||||||
#ifndef UDP2RAW_LOG_MYLOG_H_
|
#ifndef UDP2RAW_LOG_MYLOG_H_
|
||||||
#define UDP2RAW_LOG_MYLOG_H_
|
#define UDP2RAW_LOG_MYLOG_H_
|
||||||
|
|
||||||
|
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
|
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
|
#define RED "\x1B[31m"
|
||||||
#define RED "\x1B[31m"
|
#define GRN "\x1B[32m"
|
||||||
#define GRN "\x1B[32m"
|
#define YEL "\x1B[33m"
|
||||||
#define YEL "\x1B[33m"
|
#define BLU "\x1B[34m"
|
||||||
#define BLU "\x1B[34m"
|
#define MAG "\x1B[35m"
|
||||||
#define MAG "\x1B[35m"
|
#define CYN "\x1B[36m"
|
||||||
#define CYN "\x1B[36m"
|
#define WHT "\x1B[37m"
|
||||||
#define WHT "\x1B[37m"
|
|
||||||
#define RESET "\x1B[0m"
|
#define RESET "\x1B[0m"
|
||||||
|
|
||||||
|
const int log_never = 0;
|
||||||
|
const int log_fatal = 1;
|
||||||
|
const int log_error = 2;
|
||||||
|
const int log_warn = 3;
|
||||||
|
const int log_info = 4;
|
||||||
|
const int log_debug = 5;
|
||||||
|
const int log_trace = 6;
|
||||||
|
const int log_end = 7;
|
||||||
|
|
||||||
const int log_never=0;
|
const char log_text[][20] = {"NEVER", "FATAL", "ERROR", "WARN", "INFO", "DEBUG", "TRACE", ""};
|
||||||
const int log_fatal=1;
|
const char log_color[][20] = {RED, RED, RED, YEL, GRN, MAG, ""};
|
||||||
const int log_error=2;
|
|
||||||
const int log_warn=3;
|
|
||||||
const int log_info=4;
|
|
||||||
const int log_debug=5;
|
|
||||||
const int log_trace=6;
|
|
||||||
const int log_end=7;
|
|
||||||
|
|
||||||
const char log_text[][20]={"NEVER","FATAL","ERROR","WARN","INFO","DEBUG","TRACE",""};
|
|
||||||
const char log_color[][20]={RED,RED,RED,YEL,GRN,MAG,""};
|
|
||||||
|
|
||||||
extern int log_level;
|
extern int log_level;
|
||||||
extern int enable_log_position;
|
extern int enable_log_position;
|
||||||
extern int enable_log_color;
|
extern int enable_log_color;
|
||||||
|
|
||||||
|
|
||||||
#ifdef MY_DEBUG
|
#ifdef MY_DEBUG
|
||||||
#define mylog(__first_argu__dummy_abcde__,...) printf(__VA_ARGS__)
|
#define mylog(__first_argu__dummy_abcde__, ...) printf(__VA_ARGS__)
|
||||||
|
|
||||||
#else
|
#else
|
||||||
#define mylog(...) log0(__FILE__,__FUNCTION__,__LINE__,__VA_ARGS__)
|
#define mylog(...) log0(__FILE__, __FUNCTION__, __LINE__, __VA_ARGS__)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
//#define mylog(__first_argu__dummy_abcde__,...) {;}
|
//#define mylog(__first_argu__dummy_abcde__,...) {;}
|
||||||
|
|
||||||
void log0(const char * file,const char * function,int line,int level,const char* str, ...);
|
void log0(const char* file, const char* function, int line, int level, const char* str, ...);
|
||||||
|
|
||||||
void log_bare(int level,const char* str, ...);
|
|
||||||
|
|
||||||
|
void log_bare(int level, const char* str, ...);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
145
main.cpp
Executable file → Normal file
@@ -7,105 +7,100 @@
|
|||||||
#include "encrypt.h"
|
#include "encrypt.h"
|
||||||
#include "fd_manager.h"
|
#include "fd_manager.h"
|
||||||
|
|
||||||
void sigpipe_cb(struct ev_loop *l, ev_signal *w, int revents)
|
void sigpipe_cb(struct ev_loop *l, ev_signal *w, int revents) {
|
||||||
{
|
mylog(log_info, "got sigpipe, ignored");
|
||||||
mylog(log_info, "got sigpipe, ignored");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void sigterm_cb(struct ev_loop *l, ev_signal *w, int revents)
|
void sigterm_cb(struct ev_loop *l, ev_signal *w, int revents) {
|
||||||
{
|
mylog(log_info, "got sigterm, exit");
|
||||||
mylog(log_info, "got sigterm, exit");
|
myexit(0);
|
||||||
myexit(0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void sigint_cb(struct ev_loop *l, ev_signal *w, int revents)
|
void sigint_cb(struct ev_loop *l, ev_signal *w, int revents) {
|
||||||
{
|
mylog(log_info, "got sigint, exit");
|
||||||
mylog(log_info, "got sigint, exit");
|
myexit(0);
|
||||||
myexit(0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int client_event_loop();
|
int client_event_loop();
|
||||||
|
int server_event_loop();
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[]) {
|
||||||
{
|
assert(sizeof(unsigned short) == 2);
|
||||||
assert(sizeof(unsigned short)==2);
|
assert(sizeof(unsigned int) == 4);
|
||||||
assert(sizeof(unsigned int)==4);
|
assert(sizeof(unsigned long long) == 8);
|
||||||
assert(sizeof(unsigned long long)==8);
|
|
||||||
|
|
||||||
init_ws();
|
#ifdef UDP2RAW_MP
|
||||||
|
init_ws();
|
||||||
|
#endif
|
||||||
|
|
||||||
dup2(1, 2);//redirect stderr to stdout
|
dup2(1, 2); // redirect stderr to stdout
|
||||||
#if defined(__MINGW32__)
|
#if defined(__MINGW32__)
|
||||||
enable_log_color=0;
|
enable_log_color = 0;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
pre_process_arg(argc,argv);
|
pre_process_arg(argc, argv);
|
||||||
|
|
||||||
if(program_mode==client_mode)
|
ev_signal signal_watcher_sigpipe;
|
||||||
{
|
ev_signal signal_watcher_sigterm;
|
||||||
struct ev_loop* loop=ev_default_loop(0);
|
ev_signal signal_watcher_sigint;
|
||||||
|
|
||||||
|
if (program_mode == client_mode) {
|
||||||
|
struct ev_loop *loop = ev_default_loop(0);
|
||||||
#if !defined(__MINGW32__)
|
#if !defined(__MINGW32__)
|
||||||
ev_signal signal_watcher_sigpipe;
|
ev_signal_init(&signal_watcher_sigpipe, sigpipe_cb, SIGPIPE);
|
||||||
ev_signal_init(&signal_watcher_sigpipe, sigpipe_cb, SIGPIPE);
|
ev_signal_start(loop, &signal_watcher_sigpipe);
|
||||||
ev_signal_start(loop, &signal_watcher_sigpipe);
|
|
||||||
#endif
|
#endif
|
||||||
ev_signal signal_watcher_sigterm;
|
ev_signal_init(&signal_watcher_sigterm, sigterm_cb, SIGTERM);
|
||||||
ev_signal_init(&signal_watcher_sigterm, sigterm_cb, SIGTERM);
|
ev_signal_start(loop, &signal_watcher_sigterm);
|
||||||
ev_signal_start(loop, &signal_watcher_sigterm);
|
|
||||||
|
|
||||||
ev_signal signal_watcher_sigint;
|
ev_signal_init(&signal_watcher_sigint, sigint_cb, SIGINT);
|
||||||
ev_signal_init(&signal_watcher_sigint, sigint_cb, SIGINT);
|
ev_signal_start(loop, &signal_watcher_sigint);
|
||||||
ev_signal_start(loop, &signal_watcher_sigint);
|
} else {
|
||||||
}
|
#ifdef UDP2RAW_LINUX
|
||||||
else
|
signal(SIGINT, signal_handler);
|
||||||
{
|
signal(SIGHUP, signal_handler);
|
||||||
mylog(log_fatal,"server mode not supported in multi-platform version\n");
|
signal(SIGKILL, signal_handler);
|
||||||
myexit(-1);
|
signal(SIGTERM, signal_handler);
|
||||||
/*
|
signal(SIGQUIT, signal_handler);
|
||||||
signal(SIGINT, signal_handler);
|
#else
|
||||||
signal(SIGHUP, signal_handler);
|
mylog(log_fatal, "server mode not supported in multi-platform version\n");
|
||||||
signal(SIGKILL, signal_handler);
|
myexit(-1);
|
||||||
signal(SIGTERM, signal_handler);
|
#endif
|
||||||
signal(SIGQUIT, signal_handler);
|
}
|
||||||
*/
|
|
||||||
}
|
|
||||||
#if !defined(__MINGW32__)
|
#if !defined(__MINGW32__)
|
||||||
if(geteuid() != 0)
|
if (geteuid() != 0) {
|
||||||
{
|
mylog(log_warn, "root check failed, it seems like you are using a non-root account. we can try to continue, but it may fail. If you want to run udp2raw as non-root, you have to add iptables rule manually, and grant udp2raw CAP_NET_RAW capability, check README.md in repo for more info.\n");
|
||||||
mylog(log_warn,"root check failed, it seems like you are using a non-root account. we can try to continue, but it may fail. If you want to run udp2raw as non-root, you have to add iptables rule manually, and grant udp2raw CAP_NET_RAW capability, check README.md in repo for more info.\n");
|
} else {
|
||||||
}
|
mylog(log_warn, "you can run udp2raw with non-root account for better security. check README.md in repo for more info.\n");
|
||||||
else
|
}
|
||||||
{
|
|
||||||
mylog(log_warn,"you can run udp2raw with non-root account for better security. check README.md in repo for more info.\n");
|
|
||||||
}
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
mylog(log_info,"remote_ip=[%s], make sure this is a vaild IP address\n",remote_addr.get_ip());
|
mylog(log_info, "remote_ip=[%s], make sure this is a vaild IP address\n", remote_addr.get_ip());
|
||||||
|
|
||||||
//init_random_number_fd();
|
// init_random_number_fd();
|
||||||
srand(get_true_random_number_nz());
|
srand(get_true_random_number_nz());
|
||||||
const_id=get_true_random_number_nz();
|
const_id = get_true_random_number_nz();
|
||||||
|
|
||||||
mylog(log_info,"const_id:%x\n",const_id);
|
mylog(log_info, "const_id:%x\n", const_id);
|
||||||
|
|
||||||
my_init_keys(key_string,program_mode==client_mode?1:0);
|
my_init_keys(key_string, program_mode == client_mode ? 1 : 0);
|
||||||
|
|
||||||
iptables_rule();
|
iptables_rule();
|
||||||
//init_raw_socket();
|
|
||||||
//init_raw_socket() has to be done after dev dectection in mp version
|
|
||||||
|
|
||||||
if(program_mode==client_mode)
|
#ifdef UDP2RAW_LINUX
|
||||||
{
|
init_raw_socket();
|
||||||
client_event_loop();
|
#endif
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
mylog(log_fatal,"server mode not supported in multi-platform version\n");
|
|
||||||
myexit(-1);
|
|
||||||
/*
|
|
||||||
server_event_loop();
|
|
||||||
*/
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
if (program_mode == client_mode) {
|
||||||
|
client_event_loop();
|
||||||
|
} else {
|
||||||
|
#ifdef UDP2RAW_LINUX
|
||||||
|
server_event_loop();
|
||||||
|
#else
|
||||||
|
mylog(log_fatal, "server mode not supported in multi-platform version\n");
|
||||||
|
myexit(-1);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
166
makefile
@@ -1,97 +1,133 @@
|
|||||||
cc_cross=/home/wangyu/Desktop/arm-2014.05/bin/arm-none-linux-gnueabi-g++
|
cc_cross=/home/wangyu/Desktop/arm-2014.05/bin/arm-none-linux-gnueabi-g++
|
||||||
cc_local=g++
|
cc_local=g++
|
||||||
#cc_local=/opt/cross/x86_64-linux-musl/bin/x86_64-linux-musl-g++
|
cc_mips24kc_be=$(shell which g++ || /toolchains/lede-sdk-17.01.2-ar71xx-generic_gcc-5.4.0_musl-1.1.16.Linux-x86_64/staging_dir/toolchain-mips_24kc_gcc-5.4.0_musl-1.1.16/bin/mips-openwrt-linux-musl-g++)
|
||||||
#cc_mips34kc=/toolchains/OpenWrt-SDK-ar71xx-for-linux-x86_64-gcc-4.8-linaro_uClibc-0.9.33.2/staging_dir/toolchain-mips_34kc_gcc-4.8-linaro_uClibc-0.9.33.2/bin/mips-openwrt-linux-g++
|
cc_mips24kc_le=$(shell which g++ || /toolchains/lede-sdk-17.01.2-ramips-mt7621_gcc-5.4.0_musl-1.1.16.Linux-x86_64/staging_dir/toolchain-mipsel_24kc_gcc-5.4.0_musl-1.1.16/bin/mipsel-openwrt-linux-musl-g++)
|
||||||
cc_mips24kc_be=/toolchains/lede-sdk-17.01.2-ar71xx-generic_gcc-5.4.0_musl-1.1.16.Linux-x86_64/staging_dir/toolchain-mips_24kc_gcc-5.4.0_musl-1.1.16/bin/mips-openwrt-linux-musl-g++
|
cc_arm= $(shell which c++ || /toolchains/lede-sdk-17.01.2-bcm53xx_gcc-5.4.0_musl-1.1.16_eabi.Linux-x86_64/staging_dir/toolchain-arm_cortex-a9_gcc-5.4.0_musl-1.1.16_eabi/bin/arm-openwrt-linux-c++)
|
||||||
cc_mips24kc_le=/toolchains/lede-sdk-17.01.2-ramips-mt7621_gcc-5.4.0_musl-1.1.16.Linux-x86_64/staging_dir/toolchain-mipsel_24kc_gcc-5.4.0_musl-1.1.16/bin/mipsel-openwrt-linux-musl-g++
|
|
||||||
#cc_arm= /toolchains/gcc-linaro-4.9.4-2017.01-x86_64_arm-linux-gnueabi/bin/arm-linux-gnueabi-g++ -march=armv6 -marm
|
|
||||||
cc_arm= /toolchains/arm-2014.05/bin/arm-none-linux-gnueabi-g++
|
|
||||||
#cc_arm=/toolchains/lede-sdk-17.01.2-brcm2708-bcm2708_gcc-5.4.0_musl-1.1.16_eabi.Linux-x86_64/staging_dir/toolchain-arm_arm1176jzf-s+vfp_gcc-5.4.0_musl-1.1.16_eabi/bin/arm-openwrt-linux-muslgnueabi-g++
|
|
||||||
#cc_bcm2708=/home/wangyu/raspberry/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/arm-linux-gnueabihf-g++
|
|
||||||
cc_tmp= /home/wangyu/OpenWrt-SDK-15.05-x86-64_gcc-4.8-linaro_uClibc-0.9.33.2.Linux-x86_64/staging_dir/toolchain-x86_64_gcc-4.8-linaro_uClibc-0.9.33.2/bin/x86_64-openwrt-linux-uclibc-g++
|
|
||||||
cc_mingw_cross=i686-w64-mingw32-g++-posix
|
cc_mingw_cross=i686-w64-mingw32-g++-posix
|
||||||
|
cc_mac_cross=o64-clang++ -stdlib=libc++
|
||||||
|
cc_x86=$(shell which c++ || /toolchains/lede-sdk-17.01.2-x86-generic_gcc-5.4.0_musl-1.1.16.Linux-x86_64/staging_dir/toolchain-i386_pentium4_gcc-5.4.0_musl-1.1.16/bin/i486-openwrt-linux-c++)
|
||||||
|
cc_amd64=$(shell which c++ || /toolchains/lede-sdk-17.01.2-x86-64_gcc-5.4.0_musl-1.1.16.Linux-x86_64/staging_dir/toolchain-x86_64_gcc-5.4.0_musl-1.1.16/bin/x86_64-openwrt-linux-c++)
|
||||||
|
#cc_bcm2708=$(shell which g++ || /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 ${OPT}
|
|
||||||
|
|
||||||
COMMON=main.cpp lib/md5.cpp encrypt.cpp log.cpp network.cpp common.cpp connection.cpp misc.cpp fd_manager.cpp client.cpp -lpthread
|
FLAGS= -std=c++11 -Wall -Wextra -Wno-unused-variable -Wno-unused-parameter -Wno-missing-field-initializers ${OPT}
|
||||||
|
|
||||||
|
COMMON=main.cpp lib/md5.cpp lib/pbkdf2-sha1.cpp lib/pbkdf2-sha256.cpp encrypt.cpp log.cpp network.cpp common.cpp connection.cpp misc.cpp fd_manager.cpp client.cpp server.cpp -lpthread
|
||||||
|
|
||||||
|
SOURCES0= $(COMMON) lib/aes_faster_c/aes.cpp lib/aes_faster_c/wrapper.cpp
|
||||||
|
SOURCES= ${SOURCES0} my_ev.cpp -isystem libev
|
||||||
|
SOURCES_AES_ACC= $(COMMON) $(wildcard lib/aes_acc/aes*.c) my_ev.cpp -isystem libev
|
||||||
PCAP="-lpcap"
|
PCAP="-lpcap"
|
||||||
|
MP="-DUDP2RAW_MP"
|
||||||
LIBNET=-D_DEFAULT_SOURCE `libnet-config --defines` `libnet-config --libs`
|
|
||||||
|
|
||||||
|
|
||||||
SOURCES0= $(COMMON) lib/aes_faster_c/aes.cpp lib/aes_faster_c/wrapper.cpp lib/pbkdf2-sha1.cpp lib/pbkdf2-sha256.cpp
|
NAME=udp2raw
|
||||||
SOURCES=${SOURCES0} my_ev.cpp -isystem libev
|
|
||||||
SOURCES_TINY_AES= $(COMMON) lib/aes.cpp
|
|
||||||
SOURCES_AES_ACC=$(COMMON) $(wildcard lib/aes_acc/aes*.c)
|
|
||||||
|
|
||||||
NAME=udp2raw_mp
|
|
||||||
OUTPUTS=${NAME} ${NAME}_nolibnet ${NAME}.exe ${NAME}_nolibnet.exe
|
|
||||||
|
|
||||||
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'` version.txt
|
|
||||||
|
|
||||||
|
TAR=${NAME}_binaries.tar.gz `echo ${TARGETS}|sed -r 's/([^ ]+)/${NAME}_\1/g'` version.txt
|
||||||
|
|
||||||
|
TARGETS_MP= mingw_cross mingw_cross_wepoll mac_cross
|
||||||
|
|
||||||
|
export STAGING_DIR=/tmp/ #just for supress warning of staging_dir not define
|
||||||
|
|
||||||
|
# targets for nativei (non-cross) compile
|
||||||
all:git_version
|
all:git_version
|
||||||
echo "\ndo not use 'make all', instead, use 'make linux' 'make mac' 'make freebsd' 'make cygwin' \nyou can also try 'make linux_nolibnet' 'make mac_nolibnet' 'make freebsd_nolibnet' "
|
rm -f ${NAME}
|
||||||
|
${cc_local} -o ${NAME} -I. ${SOURCES} ${FLAGS} -lrt -ggdb -static -O2
|
||||||
|
|
||||||
cygwin:git_version
|
#dynamic link
|
||||||
rm -f ${OUTPUTS}
|
dynamic: git_version
|
||||||
${cc_local} -o ${NAME}_nolibnet -I. ${SOURCES} pcap_wrapper.cpp ${FLAGS} -lrt -ggdb -static -O2 -D_GNU_SOURCE
|
${cc_local} -o ${NAME}_$@ -I. ${SOURCES} ${FLAGS} -lrt -O2
|
||||||
|
|
||||||
mingw:git_version
|
#targes for general cross compile
|
||||||
rm -f ${OUTPUTS}
|
|
||||||
${cc_local} -o ${NAME}_nolibnet -I. ${SOURCES} pcap_wrapper.cpp ${FLAGS} -ggdb -static -O2 -lws2_32
|
|
||||||
|
|
||||||
mingw_cross:git_version
|
cross:git_version
|
||||||
${cc_mingw_cross} -o ${NAME}_nolibnet.exe -I. ${SOURCES} pcap_wrapper.cpp ${FLAGS} -ggdb -static -O2 -lws2_32
|
${cc_cross} -o ${NAME}_cross -I. ${SOURCES} ${FLAGS} -lrt -O2
|
||||||
|
|
||||||
mingw_cross_wepoll:git_version
|
cross2:git_version
|
||||||
${cc_mingw_cross} -o ${NAME}_nolibnet_wepoll.exe -I. ${SOURCES0} pcap_wrapper.cpp ${FLAGS} -ggdb -static -O2 -DNO_LIBEV_EMBED -D_WIN32 -lev -lws2_32
|
${cc_cross} -o ${NAME}_cross -I. ${SOURCES} ${FLAGS} -lrt -static -lgcc_eh -O2
|
||||||
|
|
||||||
linux:git_version
|
cross3:git_version
|
||||||
rm -f ${OUTPUTS}
|
${cc_cross} -o ${NAME}_cross -I. ${SOURCES} ${FLAGS} -lrt -static -O2
|
||||||
${cc_local} -o ${NAME} -I. ${SOURCES} ${PCAP} ${LIBNET} ${FLAGS} -lrt -ggdb -static -O2
|
|
||||||
|
|
||||||
linux_nolibnet:git_version
|
|
||||||
rm -f ${OUTPUTS}
|
|
||||||
${cc_local} -o ${NAME}_nolibnet -I. ${SOURCES} ${PCAP} ${FLAGS} -lrt -ggdb -static -O2 -DNO_LIBNET
|
|
||||||
|
|
||||||
freebsd:git_version
|
|
||||||
rm -f ${OUTPUTS}
|
|
||||||
${cc_local} -o ${NAME} -I. ${SOURCES} ${PCAP} ${LIBNET} ${FLAGS} -lrt -ggdb -static -O2
|
|
||||||
|
|
||||||
freebsd_nolibnet:git_version
|
|
||||||
rm -f ${OUTPUTS}
|
|
||||||
${cc_local} -o ${NAME}_nolibnet -I. ${SOURCES} ${PCAP} ${FLAGS} -lrt -ggdb -static -O2 -DNO_LIBNET
|
|
||||||
|
|
||||||
mac:git_version
|
|
||||||
rm -f ${OUTPUTS}
|
|
||||||
${cc_local} -o ${NAME} -I. ${SOURCES} ${PCAP} ${LIBNET} ${FLAGS} -ggdb -O2
|
|
||||||
|
|
||||||
mac_nolibnet:git_version
|
|
||||||
rm -f ${OUTPUTS}
|
|
||||||
${cc_local} -o ${NAME}_nolibnet -I. ${SOURCES} ${PCAP} ${FLAGS} -ggdb -O2 -DNO_LIBNET
|
|
||||||
|
|
||||||
mac_nolibnet_static:git_version #it doesnt work
|
|
||||||
rm -f ${OUTPUTS}
|
|
||||||
${cc_local} -o ${NAME} -I. ${SOURCES} -static-libstdc++ /usr/local/Cellar/libpcap/1.8.1/lib/libpcap.a ${FLAGS} -ggdb -O2 -DNO_LIBNET
|
|
||||||
|
|
||||||
|
#targets only for debug purpose
|
||||||
fast: git_version
|
fast: git_version
|
||||||
rm -f ${NAME}
|
rm -f ${NAME}
|
||||||
${cc_local} -o ${NAME} -I. ${SOURCES} ${FLAGS} -lrt -ggdb
|
${cc_local} -o ${NAME} -I. ${SOURCES} ${FLAGS} -lrt -ggdb
|
||||||
debug: git_version
|
debug: git_version
|
||||||
rm -f ${NAME}
|
rm -f ${NAME}
|
||||||
${cc_local} -o ${NAME} -I. ${SOURCES} ${PCAP} ${LIBNET} ${FLAGS} -lrt -ggdb -static -O2 -Wformat-nonliteral -D MY_DEBUG
|
${cc_local} -o ${NAME} -I. ${SOURCES} ${FLAGS} -lrt -Wformat-nonliteral -D MY_DEBUG -ggdb
|
||||||
debug2: git_version
|
debug2: git_version
|
||||||
rm -f ${NAME}
|
rm -f ${NAME}
|
||||||
${cc_local} -o ${NAME} -I. ${SOURCES} ${FLAGS} -lrt -Wformat-nonliteral -ggdb
|
${cc_local} -o ${NAME} -I. ${SOURCES} ${FLAGS} -lrt -Wformat-nonliteral -ggdb -fsanitize=address
|
||||||
|
|
||||||
|
#targets only for 'make release'
|
||||||
|
|
||||||
|
mips24kc_be: git_version
|
||||||
|
${cc_mips24kc_be} -o ${NAME}_$@ -I. ${SOURCES} ${FLAGS} -lrt -lgcc_eh -static -O2
|
||||||
|
mips24kc_be_asm_aes: git_version
|
||||||
|
${cc_mips24kc_be} -o ${NAME}_$@ -I. ${SOURCES_AES_ACC} ${FLAGS} -lrt -lgcc_eh -static -O2 lib/aes_acc/asm/mips_be.S
|
||||||
|
mips24kc_le: git_version
|
||||||
|
${cc_mips24kc_le} -o ${NAME}_$@ -I. ${SOURCES} ${FLAGS} -lrt -lgcc_eh -static -O2
|
||||||
|
mips24kc_le_asm_aes: git_version
|
||||||
|
${cc_mips24kc_le} -o ${NAME}_$@ -I. ${SOURCES_AES_ACC} ${FLAGS} -lrt -lgcc_eh -static -O2 lib/aes_acc/asm/mips.S
|
||||||
|
amd64:git_version
|
||||||
|
${cc_amd64} -o ${NAME}_$@ -I. ${SOURCES} ${FLAGS} -lrt -static -O2 -lgcc_eh -ggdb
|
||||||
|
amd64_hw_aes:git_version
|
||||||
|
${cc_amd64} -o ${NAME}_$@ -I. ${SOURCES_AES_ACC} ${FLAGS} -lrt -static -O2 lib/aes_acc/asm/x64.S -lgcc_eh -ggdb
|
||||||
|
x86:git_version
|
||||||
|
${cc_x86} -o ${NAME}_$@ -I. ${SOURCES} ${FLAGS} -lrt -static -O2 -lgcc_eh -ggdb
|
||||||
|
x86_asm_aes:git_version
|
||||||
|
${cc_x86} -o ${NAME}_$@ -I. ${SOURCES_AES_ACC} ${FLAGS} -lrt -static -O2 lib/aes_acc/asm/x86.S -lgcc_eh -ggdb
|
||||||
|
arm:git_version
|
||||||
|
${cc_arm} -o ${NAME}_$@ -I. ${SOURCES} ${FLAGS} -lrt -static -O2 -lgcc_eh
|
||||||
|
arm_asm_aes:git_version
|
||||||
|
${cc_arm} -o ${NAME}_$@ -I. ${SOURCES_AES_ACC} ${FLAGS} -lrt -static -O2 lib/aes_acc/asm/arm.S -lgcc_eh
|
||||||
|
|
||||||
|
release: ${TARGETS}
|
||||||
|
cp git_version.h version.txt
|
||||||
|
tar -zcvf ${TAR}
|
||||||
|
|
||||||
|
#targets for multi-platform version (native compile)
|
||||||
|
cygwin:git_version
|
||||||
|
${cc_local} -o ${NAME}_$@ -I. ${SOURCES} pcap_wrapper.cpp ${FLAGS} -lrt -ggdb -static -O2 -D_GNU_SOURCE ${MP}
|
||||||
|
|
||||||
|
mingw:git_version
|
||||||
|
${cc_local} -o ${NAME}_$@ -I. ${SOURCES} pcap_wrapper.cpp ${FLAGS} -ggdb -static -O2 -lws2_32 ${MP}
|
||||||
|
|
||||||
|
mingw_wepoll:git_version
|
||||||
|
${cc_local} -o ${NAME}_$@ -I. ${SOURCES0} pcap_wrapper.cpp ${FLAGS} -ggdb -static -O2 -DNO_LIBEV_EMBED -D_WIN32 -lev -lws2_32 ${MP}
|
||||||
|
|
||||||
|
linux:git_version
|
||||||
|
${cc_local} -o ${NAME}_$@ -I. ${SOURCES} ${PCAP} ${FLAGS} -lrt -ggdb -static -O2 ${MP}
|
||||||
|
|
||||||
|
freebsd:git_version
|
||||||
|
${cc_local} -o ${NAME}_$@ -I. ${SOURCES} ${PCAP} ${FLAGS} -lrt -ggdb -static -libverbs -O2 ${MP}
|
||||||
|
|
||||||
|
mac:git_version
|
||||||
|
${cc_local} -o ${NAME}_$@ -I. ${SOURCES} ${PCAP} ${FLAGS} -ggdb -O2 ${MP}
|
||||||
|
|
||||||
|
#targets for multi-platform version (cross compile)
|
||||||
|
|
||||||
|
mingw_cross:git_version
|
||||||
|
${cc_mingw_cross} -o ${NAME}_mp.exe -I. ${SOURCES} pcap_wrapper.cpp ${FLAGS} -ggdb -static -O2 -lws2_32 ${MP}
|
||||||
|
|
||||||
|
mingw_cross_wepoll:git_version
|
||||||
|
${cc_mingw_cross} -o ${NAME}_mp_wepoll.exe -I. ${SOURCES0} pcap_wrapper.cpp ${FLAGS} -ggdb -static -O2 -DNO_LIBEV_EMBED -D_WIN32 -lev -lws2_32 ${MP}
|
||||||
|
|
||||||
|
mac_cross:git_version
|
||||||
|
${cc_mac_cross} -o ${NAME}_mp_mac -I. ${SOURCES} ${PCAP} ${FLAGS} -ggdb -O2 ${MP}
|
||||||
|
|
||||||
|
release_mp:${TARGETS_MP}
|
||||||
|
cp git_version.h version.txt
|
||||||
|
tar -zcvf ${NAME}_mp_binaries.tar.gz ${NAME}_mp.exe ${NAME}_mp_wepoll.exe ${NAME}_mp_mac version.txt
|
||||||
|
|
||||||
#dynamic: git_version
|
|
||||||
# ${cc_local} -o ${NAME}_$@ -I. ${SOURCES} ${FLAGS} -lrt -O3
|
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -f udp2raw.exe ${OUTPUTS}
|
rm -f ${TAR}
|
||||||
rm -f udp2raw udp2raw_cross udp2raw_cmake udp2raw_dynamic
|
rm -f ${NAME} ${NAME}_cross ${NAME}.exe ${NAME}_wepoll.exe ${NAME}_mac
|
||||||
|
rm -f ${NAME}_mp_binaries.tar.gz ${NAME}_mp.exe ${NAME}_mp_wepoll.exe ${NAME}_mp_mac
|
||||||
rm -f git_version.h
|
rm -f git_version.h
|
||||||
|
|
||||||
git_version:
|
git_version:
|
||||||
|
143
misc.h
@@ -8,7 +8,6 @@
|
|||||||
#ifndef MISC_H_
|
#ifndef MISC_H_
|
||||||
#define MISC_H_
|
#define MISC_H_
|
||||||
|
|
||||||
|
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
#include "network.h"
|
#include "network.h"
|
||||||
@@ -26,101 +25,106 @@ extern int enable_dns_resolve;
|
|||||||
|
|
||||||
extern int ttl_value;
|
extern int ttl_value;
|
||||||
|
|
||||||
|
const u32_t max_handshake_conn_num = 10000;
|
||||||
|
const u32_t max_ready_conn_num = 1000;
|
||||||
|
const u32_t anti_replay_window_size = 4000;
|
||||||
|
const int max_conv_num = 10000;
|
||||||
|
|
||||||
const u32_t max_handshake_conn_num=10000;
|
const u32_t client_handshake_timeout = 5000; // unit ms
|
||||||
const u32_t max_ready_conn_num=1000;
|
const u32_t client_retry_interval = 1000; // ms
|
||||||
const u32_t anti_replay_window_size=4000;
|
|
||||||
const int max_conv_num=10000;
|
|
||||||
|
|
||||||
const u32_t client_handshake_timeout=5000;//unit 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 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 int conv_clear_ratio = 30; // conv grabage collecter check 1/30 of all conv one time
|
||||||
|
const int conn_clear_ratio = 50;
|
||||||
|
const int conv_clear_min = 1;
|
||||||
|
const int conn_clear_min = 1;
|
||||||
|
|
||||||
const int conv_clear_ratio=30; //conv grabage collecter check 1/30 of all conv one time
|
const u32_t conv_clear_interval = 1000; // ms
|
||||||
const int conn_clear_ratio=50;
|
const u32_t conn_clear_interval = 1000; // ms
|
||||||
const int conv_clear_min=1;
|
|
||||||
const int conn_clear_min=1;
|
|
||||||
|
|
||||||
const u32_t conv_clear_interval=1000;//ms
|
const i32_t max_fail_time = 0; // disable
|
||||||
const u32_t conn_clear_interval=1000;//ms
|
|
||||||
|
|
||||||
|
const u32_t heartbeat_interval = 600; // ms
|
||||||
|
|
||||||
const i32_t max_fail_time=0;//disable
|
const u32_t timer_interval = 400; // ms. this should be smaller than heartbeat_interval and retry interval;
|
||||||
|
|
||||||
const u32_t heartbeat_interval=600;//ms
|
const uint32_t conv_timeout = 180000; // ms. 120 second
|
||||||
|
// const u32_t conv_timeout=30000; //for test
|
||||||
|
|
||||||
const u32_t timer_interval=400;//ms. this should be smaller than heartbeat_interval and retry interval;
|
const u32_t client_conn_timeout = 10000; // ms.
|
||||||
|
const u32_t client_conn_uplink_timeout = client_conn_timeout + 2000; // ms
|
||||||
|
|
||||||
const uint32_t conv_timeout=180000; //ms. 120 second
|
const uint32_t server_conn_timeout = conv_timeout + 60000; // ms. this should be 60s+ longer than conv_timeout,so that conv_manager can destruct convs gradually,to avoid latency glicth
|
||||||
//const u32_t conv_timeout=30000; //for test
|
// const u32_t server_conn_timeout=conv_timeout+10000;//for test
|
||||||
|
|
||||||
const u32_t client_conn_timeout=10000;//ms.
|
const u32_t iptables_rule_keep_interval = 20; // unit: second;
|
||||||
const u32_t client_conn_uplink_timeout=client_conn_timeout+2000;//ms
|
|
||||||
|
|
||||||
const uint32_t server_conn_timeout=conv_timeout+60000;//ms. this should be 60s+ longer than conv_timeout,so that conv_manager can destruct convs gradually,to avoid latency glicth
|
enum server_current_state_t { server_idle = 0,
|
||||||
//const u32_t server_conn_timeout=conv_timeout+10000;//for test
|
server_handshake1,
|
||||||
|
server_ready }; // server state machine
|
||||||
|
enum client_current_state_t { client_idle = 0,
|
||||||
|
client_tcp_handshake,
|
||||||
|
client_handshake1,
|
||||||
|
client_handshake2,
|
||||||
|
client_ready,
|
||||||
|
client_tcp_handshake_dummy }; // client state machine
|
||||||
|
|
||||||
const u32_t iptables_rule_keep_interval=20;//unit: second;
|
enum raw_mode_t { mode_faketcp = 0,
|
||||||
|
mode_udp,
|
||||||
|
mode_icmp,
|
||||||
|
mode_end };
|
||||||
|
enum program_mode_t { unset_mode = 0,
|
||||||
|
client_mode,
|
||||||
|
server_mode };
|
||||||
|
|
||||||
enum server_current_state_t {server_idle=0,server_handshake1,server_ready}; //server state machine
|
union current_state_t {
|
||||||
enum client_current_state_t {client_idle=0,client_tcp_handshake,client_handshake1,client_handshake2,client_ready,client_tcp_handshake_dummy};//client state machine
|
server_current_state_t server_current_state;
|
||||||
|
client_current_state_t client_current_state;
|
||||||
enum raw_mode_t{mode_faketcp=0,mode_udp,mode_icmp,mode_end};
|
|
||||||
enum program_mode_t {unset_mode=0,client_mode,server_mode};
|
|
||||||
|
|
||||||
union current_state_t
|
|
||||||
{
|
|
||||||
server_current_state_t server_current_state;
|
|
||||||
client_current_state_t client_current_state;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
extern char remote_address[max_addr_len];
|
// extern char remote_address[max_address_len];
|
||||||
//extern char remote_address[max_address_len];
|
// extern char local_ip[100], remote_ip[100],source_ip[100];//local_ip is for -l option,remote_ip for -r option,source for --source-ip
|
||||||
extern char local_ip[100], remote_ip[100],source_ip[100];//local_ip is for -l option,remote_ip for -r option,source for --source-ip
|
// extern u32_t local_ip_uint32,remote_ip_uint32,source_ip_uint32;//convert from last line.
|
||||||
extern u32_t local_ip_uint32,remote_ip_uint32,source_ip_uint32;//convert from last line.
|
// extern int local_port , remote_port,source_port;//similiar to local_ip remote_ip,buf for port.source_port=0 indicates --source-port is not enabled
|
||||||
extern int local_port , remote_port,source_port;//similiar to local_ip remote_ip,buf for port.source_port=0 indicates --source-port is not enabled
|
|
||||||
|
|
||||||
|
extern address_t local_addr, remote_addr, source_addr;
|
||||||
extern address_t local_addr,remote_addr,source_addr;
|
|
||||||
|
|
||||||
extern my_ip_t bind_addr;
|
extern my_ip_t bind_addr;
|
||||||
|
|
||||||
extern int bind_addr_used;
|
extern int bind_addr_used;
|
||||||
extern int force_source_ip; //if --source-ip is enabled
|
extern int force_source_ip; // if --source-ip is enabled
|
||||||
extern int force_source_port;
|
extern int force_source_port;
|
||||||
extern int source_port;
|
extern int source_port;
|
||||||
|
|
||||||
extern my_id_t const_id;//an id used for connection recovery,its generated randomly,it never change since its generated
|
extern my_id_t const_id; // an id used for connection recovery,its generated randomly,it never change since its generated
|
||||||
|
|
||||||
extern int udp_fd; //for client only. client use this fd to listen and handle udp connection
|
extern int udp_fd; // for client only. client use this fd to listen and handle udp connection
|
||||||
extern int bind_fd; //bind only,never send or recv. its just a dummy fd for bind,so that other program wont occupy the same port
|
extern int bind_fd; // bind only,never send or recv. its just a dummy fd for bind,so that other program wont occupy the same port
|
||||||
//extern int epollfd; //fd for epoll
|
extern int epollfd; // fd for epoll
|
||||||
//extern int timer_fd; //the general timer fd for client and server.for server this is not the only timer find,every connection has a timer fd.
|
extern int timer_fd; // the general timer fd for client and server.for server this is not the only timer find,every connection has a timer fd.
|
||||||
extern int fail_time_counter;//determine if the max_fail_time is reached
|
extern int fail_time_counter; // determine if the max_fail_time is reached
|
||||||
extern int epoll_trigger_counter;//for debug only
|
extern int epoll_trigger_counter; // for debug only
|
||||||
extern int debug_flag;//for debug only
|
extern int debug_flag; // for debug only
|
||||||
|
|
||||||
|
extern int simple_rule; // deprecated.
|
||||||
extern int simple_rule; //deprecated.
|
extern int keep_rule; // whether to monitor the iptables rule periodly,re-add if losted
|
||||||
extern int keep_rule; //whether to monitor the iptables rule periodly,re-add if losted
|
extern int auto_add_iptables_rule; // if -a is set
|
||||||
extern int auto_add_iptables_rule;//if -a is set
|
extern int generate_iptables_rule; // if -g is set
|
||||||
extern int generate_iptables_rule;//if -g is set
|
extern int generate_iptables_rule_add; // if --gen-add is set
|
||||||
extern int generate_iptables_rule_add;// if --gen-add is set
|
|
||||||
extern int retry_on_error;
|
extern int retry_on_error;
|
||||||
const int retry_on_error_interval=10;
|
const int retry_on_error_interval = 10;
|
||||||
|
|
||||||
extern int debug_resend; // debug only
|
extern int debug_resend; // debug only
|
||||||
|
|
||||||
extern char key_string[1000];// -k option
|
extern char key_string[1000]; // -k option
|
||||||
extern char fifo_file[1000];
|
extern char fifo_file[1000];
|
||||||
|
|
||||||
|
|
||||||
extern raw_mode_t raw_mode;
|
extern raw_mode_t raw_mode;
|
||||||
extern u32_t raw_ip_version;
|
extern u32_t raw_ip_version;
|
||||||
|
|
||||||
extern program_mode_t program_mode;
|
extern program_mode_t program_mode;
|
||||||
extern unordered_map<int, const char*> raw_mode_tostring ;
|
extern unordered_map<int, const char *> raw_mode_tostring;
|
||||||
|
|
||||||
extern int about_to_exit;
|
extern int about_to_exit;
|
||||||
|
|
||||||
@@ -129,28 +133,23 @@ extern int socket_buf_size;
|
|||||||
extern pthread_t keep_thread;
|
extern pthread_t keep_thread;
|
||||||
extern int keep_thread_running;
|
extern int keep_thread_running;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int process_lower_level_arg();
|
int process_lower_level_arg();
|
||||||
void print_help();
|
void print_help();
|
||||||
void iptables_rule();
|
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,fd64_t &fd64);
|
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 *);
|
||||||
|
|
||||||
int clear_iptables_rule();
|
int clear_iptables_rule();
|
||||||
|
|
||||||
int iptables_gen_add(const char * s,u32_t const_id);
|
int iptables_gen_add(const char *s, u32_t const_id);
|
||||||
int iptables_rule_init(const char * s,u32_t const_id,int keep);
|
int iptables_rule_init(const char *s, u32_t const_id, int keep);
|
||||||
int keep_iptables_rule();
|
int keep_iptables_rule();
|
||||||
|
|
||||||
|
void signal_handler(int sig);
|
||||||
|
|
||||||
void signal_handler(int sig);
|
|
||||||
|
|
||||||
#endif /* MISC_H_ */
|
#endif /* MISC_H_ */
|
||||||
|
1
my_ev.h
@@ -2,4 +2,3 @@
|
|||||||
|
|
||||||
#include "my_ev_common.h"
|
#include "my_ev_common.h"
|
||||||
#include "ev.h"
|
#include "ev.h"
|
||||||
|
|
||||||
|
@@ -1,6 +1,8 @@
|
|||||||
|
|
||||||
#define EV_STANDALONE 1
|
#define EV_STANDALONE 1
|
||||||
#define EV_COMMON void *data; unsigned long long u64;
|
#define EV_COMMON \
|
||||||
|
void *data; \
|
||||||
|
unsigned long long u64;
|
||||||
#define EV_COMPAT3 0
|
#define EV_COMPAT3 0
|
||||||
|
|
||||||
//#include <wepoll.h>
|
//#include <wepoll.h>
|
||||||
@@ -8,11 +10,10 @@
|
|||||||
//#define EV_USE_SELECT 1
|
//#define EV_USE_SELECT 1
|
||||||
//#define EV_SELECT_IS_WINSOCKET 1
|
//#define EV_SELECT_IS_WINSOCKET 1
|
||||||
|
|
||||||
# define EV_FD_TO_WIN32_HANDLE(fd) (fd)
|
#define EV_FD_TO_WIN32_HANDLE(fd) (fd)
|
||||||
# define EV_WIN32_HANDLE_TO_FD(handle) (handle)
|
#define EV_WIN32_HANDLE_TO_FD(handle) (handle)
|
||||||
# define EV_WIN32_CLOSE_FD(fd) closesocket (fd)
|
#define EV_WIN32_CLOSE_FD(fd) closesocket(fd)
|
||||||
# define FD_SETSIZE 4096
|
#define FD_SETSIZE 4096
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
//#define EV_VERIFY 2
|
//#define EV_VERIFY 2
|
||||||
|
|
||||||
|
4414
network.cpp
346
network.h
@@ -14,11 +14,11 @@ extern int use_tcp_dummy_socket;
|
|||||||
extern int seq_mode;
|
extern int seq_mode;
|
||||||
extern int max_seq_mode;
|
extern int max_seq_mode;
|
||||||
extern int filter_port;
|
extern int filter_port;
|
||||||
extern u32_t bind_address_uint32;
|
// extern u32_t bind_address_uint32;
|
||||||
extern int disable_bpf_filter;
|
extern int disable_bpf_filter;
|
||||||
|
|
||||||
//extern int lower_level;
|
extern int lower_level;
|
||||||
//extern int lower_level_manual;
|
extern int lower_level_manual;
|
||||||
extern char if_name[100];
|
extern char if_name[100];
|
||||||
extern char dev[100];
|
extern char dev[100];
|
||||||
extern unsigned char dest_hw_addr[];
|
extern unsigned char dest_hw_addr[];
|
||||||
@@ -30,11 +30,11 @@ extern int ifindex;
|
|||||||
extern char g_packet_buf[huge_buf_len];
|
extern char g_packet_buf[huge_buf_len];
|
||||||
extern int g_packet_buf_len;
|
extern int g_packet_buf_len;
|
||||||
extern int g_packet_buf_cnt;
|
extern int g_packet_buf_cnt;
|
||||||
|
#ifdef UDP2RAW_MP
|
||||||
extern queue_t my_queue;
|
extern queue_t my_queue;
|
||||||
|
|
||||||
extern ev_async async_watcher;
|
extern ev_async async_watcher;
|
||||||
extern struct ev_loop* g_default_loop;
|
extern struct ev_loop *g_default_loop;
|
||||||
|
|
||||||
extern pthread_mutex_t queue_mutex;
|
extern pthread_mutex_t queue_mutex;
|
||||||
extern int use_pcap_mutex;
|
extern int use_pcap_mutex;
|
||||||
@@ -47,23 +47,22 @@ extern int send_with_pcap;
|
|||||||
extern int pcap_header_captured;
|
extern int pcap_header_captured;
|
||||||
extern int pcap_header_buf[buf_len];
|
extern int pcap_header_buf[buf_len];
|
||||||
|
|
||||||
struct icmphdr
|
struct icmphdr {
|
||||||
{
|
uint8_t type;
|
||||||
uint8_t type;
|
uint8_t code;
|
||||||
uint8_t code;
|
uint16_t check_sum;
|
||||||
uint16_t check_sum;
|
uint16_t id;
|
||||||
uint16_t id;
|
uint16_t seq;
|
||||||
uint16_t seq;
|
|
||||||
};
|
};
|
||||||
|
#endif
|
||||||
|
|
||||||
struct my_iphdr
|
struct my_iphdr {
|
||||||
{
|
|
||||||
#ifdef UDP2RAW_LITTLE_ENDIAN
|
#ifdef UDP2RAW_LITTLE_ENDIAN
|
||||||
unsigned char ihl:4;
|
unsigned char ihl : 4;
|
||||||
unsigned char version:4;
|
unsigned char version : 4;
|
||||||
#else
|
#else
|
||||||
unsigned char version:4;
|
unsigned char version : 4;
|
||||||
unsigned char ihl:4;
|
unsigned char ihl : 4;
|
||||||
#endif
|
#endif
|
||||||
u_int8_t tos;
|
u_int8_t tos;
|
||||||
u_int16_t tot_len;
|
u_int16_t tot_len;
|
||||||
@@ -75,106 +74,98 @@ struct my_iphdr
|
|||||||
u_int32_t saddr;
|
u_int32_t saddr;
|
||||||
u_int32_t daddr;
|
u_int32_t daddr;
|
||||||
/*The options start here. */
|
/*The options start here. */
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
struct my_udphdr
|
|
||||||
{
|
|
||||||
/*__extension__*/ union
|
|
||||||
{
|
|
||||||
struct
|
|
||||||
{
|
|
||||||
u_int16_t uh_sport; /* source port */
|
|
||||||
u_int16_t uh_dport; /* destination port */
|
|
||||||
u_int16_t uh_ulen; /* udp length */
|
|
||||||
u_int16_t uh_sum; /* udp checksum */
|
|
||||||
};
|
|
||||||
struct
|
|
||||||
{
|
|
||||||
u_int16_t source;
|
|
||||||
u_int16_t dest;
|
|
||||||
u_int16_t len;
|
|
||||||
u_int16_t check;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct my_udphdr {
|
||||||
struct my_tcphdr
|
/*__extension__*/ union {
|
||||||
{
|
struct
|
||||||
/*__extension__*/ union
|
{
|
||||||
{
|
u_int16_t uh_sport; /* source port */
|
||||||
struct
|
u_int16_t uh_dport; /* destination port */
|
||||||
{
|
u_int16_t uh_ulen; /* udp length */
|
||||||
u_int16_t th_sport; /* source port */
|
u_int16_t uh_sum; /* udp checksum */
|
||||||
u_int16_t th_dport; /* destination port */
|
};
|
||||||
u_int32_t th_seq; /* sequence number */
|
struct
|
||||||
u_int32_t th_ack; /* acknowledgement number */
|
{
|
||||||
# ifdef UDP2RAW_LITTLE_ENDIAN
|
u_int16_t source;
|
||||||
u_int8_t th_x2:4; /* (unused) */
|
u_int16_t dest;
|
||||||
u_int8_t tc_off:4; /* data offset */
|
u_int16_t len;
|
||||||
# else
|
u_int16_t check;
|
||||||
u_int8_t th_off:4; /* data offset */
|
};
|
||||||
u_int8_t th_x2:4; /* (unused) */
|
|
||||||
# endif
|
|
||||||
u_int8_t th_flags;
|
|
||||||
# define TH_FIN 0x01
|
|
||||||
# define TH_SYN 0x02
|
|
||||||
# define TH_RST 0x04
|
|
||||||
# define TH_PUSH 0x08
|
|
||||||
# define TH_ACK 0x10
|
|
||||||
# define TH_URG 0x20
|
|
||||||
u_int16_t th_win; /* window */
|
|
||||||
u_int16_t th_sum; /* checksum */
|
|
||||||
u_int16_t th_urp; /* urgent pointer */
|
|
||||||
};
|
|
||||||
struct
|
|
||||||
{
|
|
||||||
u_int16_t source;
|
|
||||||
u_int16_t dest;
|
|
||||||
u_int32_t seq;
|
|
||||||
u_int32_t ack_seq;
|
|
||||||
# ifdef UDP2RAW_LITTLE_ENDIAN
|
|
||||||
u_int16_t res1:4;
|
|
||||||
u_int16_t doff:4;
|
|
||||||
u_int16_t fin:1;
|
|
||||||
u_int16_t syn:1;
|
|
||||||
u_int16_t rst:1;
|
|
||||||
u_int16_t psh:1;
|
|
||||||
u_int16_t ack:1;
|
|
||||||
u_int16_t urg:1;
|
|
||||||
u_int16_t res2:2;
|
|
||||||
# else
|
|
||||||
u_int16_t doff:4;
|
|
||||||
u_int16_t res1:4;
|
|
||||||
u_int16_t res2:2;
|
|
||||||
u_int16_t urg:1;
|
|
||||||
u_int16_t ack:1;
|
|
||||||
u_int16_t psh:1;
|
|
||||||
u_int16_t rst:1;
|
|
||||||
u_int16_t syn:1;
|
|
||||||
u_int16_t fin:1;
|
|
||||||
# endif
|
|
||||||
u_int16_t window;
|
|
||||||
u_int16_t check;
|
|
||||||
u_int16_t urg_ptr;
|
|
||||||
};
|
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct my_tcphdr {
|
||||||
struct my_ip6hdr
|
/*__extension__*/ union {
|
||||||
{
|
struct
|
||||||
# ifdef UDP2RAW_LITTLE_ENDIAN
|
{
|
||||||
uint8_t traffic_class_high:4;
|
u_int16_t th_sport; /* source port */
|
||||||
uint8_t version:4;
|
u_int16_t th_dport; /* destination port */
|
||||||
uint8_t flow_label_high:4;
|
u_int32_t th_seq; /* sequence number */
|
||||||
uint8_t traffic_class_low:4;
|
u_int32_t th_ack; /* acknowledgement number */
|
||||||
|
#ifdef UDP2RAW_LITTLE_ENDIAN
|
||||||
|
u_int8_t th_x2 : 4; /* (unused) */
|
||||||
|
u_int8_t tc_off : 4; /* data offset */
|
||||||
#else
|
#else
|
||||||
uint8_t version:4;
|
u_int8_t th_off : 4; /* data offset */
|
||||||
uint8_t traffic_class_high:4;
|
u_int8_t th_x2 : 4; /* (unused) */
|
||||||
uint8_t traffic_class_low:4;
|
#endif
|
||||||
uint8_t flow_label_high:4;
|
u_int8_t th_flags;
|
||||||
|
#define TH_FIN 0x01
|
||||||
|
#define TH_SYN 0x02
|
||||||
|
#define TH_RST 0x04
|
||||||
|
#define TH_PUSH 0x08
|
||||||
|
#define TH_ACK 0x10
|
||||||
|
#define TH_URG 0x20
|
||||||
|
u_int16_t th_win; /* window */
|
||||||
|
u_int16_t th_sum; /* checksum */
|
||||||
|
u_int16_t th_urp; /* urgent pointer */
|
||||||
|
};
|
||||||
|
struct
|
||||||
|
{
|
||||||
|
u_int16_t source;
|
||||||
|
u_int16_t dest;
|
||||||
|
u_int32_t seq;
|
||||||
|
u_int32_t ack_seq;
|
||||||
|
#ifdef UDP2RAW_LITTLE_ENDIAN
|
||||||
|
u_int16_t res1 : 4;
|
||||||
|
u_int16_t doff : 4;
|
||||||
|
u_int16_t fin : 1;
|
||||||
|
u_int16_t syn : 1;
|
||||||
|
u_int16_t rst : 1;
|
||||||
|
u_int16_t psh : 1;
|
||||||
|
u_int16_t ack : 1;
|
||||||
|
u_int16_t urg : 1;
|
||||||
|
u_int16_t res2 : 2;
|
||||||
|
#else
|
||||||
|
u_int16_t doff : 4;
|
||||||
|
u_int16_t res1 : 4;
|
||||||
|
u_int16_t res2 : 2;
|
||||||
|
u_int16_t urg : 1;
|
||||||
|
u_int16_t ack : 1;
|
||||||
|
u_int16_t psh : 1;
|
||||||
|
u_int16_t rst : 1;
|
||||||
|
u_int16_t syn : 1;
|
||||||
|
u_int16_t fin : 1;
|
||||||
|
#endif
|
||||||
|
u_int16_t window;
|
||||||
|
u_int16_t check;
|
||||||
|
u_int16_t urg_ptr;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
struct my_ip6hdr {
|
||||||
|
#ifdef UDP2RAW_LITTLE_ENDIAN
|
||||||
|
uint8_t traffic_class_high : 4;
|
||||||
|
uint8_t version : 4;
|
||||||
|
uint8_t flow_label_high : 4;
|
||||||
|
uint8_t traffic_class_low : 4;
|
||||||
|
#else
|
||||||
|
uint8_t version : 4;
|
||||||
|
uint8_t traffic_class_high : 4;
|
||||||
|
uint8_t traffic_class_low : 4;
|
||||||
|
uint8_t flow_label_high : 4;
|
||||||
#endif
|
#endif
|
||||||
u_int16_t flow_label_low;
|
u_int16_t flow_label_low;
|
||||||
u_int16_t payload_len;
|
u_int16_t payload_len;
|
||||||
@@ -183,15 +174,14 @@ struct my_ip6hdr
|
|||||||
|
|
||||||
struct in6_addr src;
|
struct in6_addr src;
|
||||||
struct in6_addr dst;
|
struct in6_addr dst;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct my_icmphdr
|
struct my_icmphdr {
|
||||||
{
|
uint8_t type;
|
||||||
uint8_t type;
|
uint8_t code;
|
||||||
uint8_t code;
|
uint16_t check_sum;
|
||||||
uint16_t check_sum;
|
uint16_t id;
|
||||||
uint16_t id;
|
uint16_t seq;
|
||||||
uint16_t seq;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct pseudo_header {
|
struct pseudo_header {
|
||||||
@@ -211,60 +201,53 @@ struct pseudo_header6 {
|
|||||||
u_int8_t next_header;
|
u_int8_t next_header;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct packet_info_t //todo change this to union
|
struct packet_info_t // todo change this to union
|
||||||
{
|
{
|
||||||
uint8_t protocol;
|
uint8_t protocol;
|
||||||
|
|
||||||
//u32_t src_ip;
|
// u32_t src_ip;
|
||||||
//u32_t dst_ip;
|
// u32_t dst_ip;
|
||||||
u32_t src_ip;
|
my_ip_t new_src_ip;
|
||||||
u32_t dst_ip;
|
my_ip_t new_dst_ip;
|
||||||
|
|
||||||
my_ip_t new_src_ip;
|
uint16_t src_port;
|
||||||
my_ip_t new_dst_ip;
|
uint16_t dst_port;
|
||||||
|
|
||||||
uint16_t src_port;
|
// tcp_part:
|
||||||
uint16_t dst_port;
|
bool syn, ack, psh, rst;
|
||||||
|
|
||||||
//tcp_part:
|
u32_t seq, ack_seq;
|
||||||
bool syn,ack,psh,rst;
|
|
||||||
|
|
||||||
u32_t seq,ack_seq;
|
u32_t ack_seq_counter;
|
||||||
|
|
||||||
u32_t ack_seq_counter;
|
u32_t ts, ts_ack;
|
||||||
|
|
||||||
u32_t ts,ts_ack;
|
uint16_t my_icmp_seq;
|
||||||
|
|
||||||
|
bool has_ts;
|
||||||
|
|
||||||
uint16_t my_icmp_seq;
|
i32_t data_len;
|
||||||
|
|
||||||
bool has_ts;
|
#ifdef UDP2RAW_LINUX
|
||||||
|
sockaddr_ll addr_ll;
|
||||||
|
#endif
|
||||||
|
|
||||||
//sockaddr_ll addr_ll;
|
packet_info_t();
|
||||||
|
|
||||||
i32_t data_len;
|
|
||||||
|
|
||||||
packet_info_t();
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct raw_info_t
|
struct raw_info_t {
|
||||||
{
|
packet_info_t send_info;
|
||||||
packet_info_t send_info;
|
packet_info_t recv_info;
|
||||||
packet_info_t recv_info;
|
// int last_send_len;
|
||||||
//int last_send_len;
|
// int last_recv_len;
|
||||||
//int last_recv_len;
|
bool peek = 0;
|
||||||
|
// bool csum=1;
|
||||||
bool peek=0;
|
u32_t reserved_send_seq;
|
||||||
//bool csum=1;
|
// uint32_t first_seq,first_ack_seq;
|
||||||
|
int rst_received = 0;
|
||||||
u32_t reserved_send_seq;
|
bool disabled = 0;
|
||||||
//uint32_t first_seq,first_ack_seq;
|
|
||||||
int rst_received=0;
|
|
||||||
bool disabled=0;
|
|
||||||
|
|
||||||
};//g_raw_info;
|
|
||||||
|
|
||||||
|
|
||||||
|
}; // g_raw_info;
|
||||||
|
|
||||||
int init_raw_socket();
|
int init_raw_socket();
|
||||||
|
|
||||||
@@ -272,51 +255,56 @@ void init_filter(int port);
|
|||||||
|
|
||||||
void remove_filter();
|
void remove_filter();
|
||||||
|
|
||||||
//int init_ifindex(const char * if_name,int fd,int &index);
|
#ifdef UDP2RAW_LINUX
|
||||||
int init_ifindex(const char * if_name,int &index);
|
int init_ifindex(const char *if_name, int fd, int &index);
|
||||||
|
#endif
|
||||||
|
|
||||||
int find_lower_level_info(u32_t ip,u32_t &dest_ip,string &if_name,string &hw);
|
#ifdef UDP2RAW_MP
|
||||||
|
int init_ifindex(const char *if_name, int &index);
|
||||||
|
#endif
|
||||||
|
|
||||||
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 find_lower_level_info(u32_t ip, u32_t &dest_ip, string &if_name, string &hw);
|
||||||
int get_src_adress2(address_t &output_addr,address_t remote_addr);
|
|
||||||
|
|
||||||
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 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_bind2(int &fd,address_t address);
|
int get_src_adress2(address_t &output_addr, address_t remote_addr);
|
||||||
|
|
||||||
int client_bind_to_a_new_port(int & bind_fd,u32_t local_ip_uint32);//find a free port and bind to it.
|
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_port2(int &fd,const address_t& address);
|
int try_to_list_and_bind2(int &fd, address_t address);
|
||||||
|
|
||||||
|
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_port2(int &fd, const address_t &address);
|
||||||
|
|
||||||
int discard_raw_packet();
|
int discard_raw_packet();
|
||||||
|
int pre_recv_raw_packet();
|
||||||
|
|
||||||
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);
|
||||||
|
|
||||||
int peek_raw(raw_info_t &peek_info);
|
int peek_raw(raw_info_t &peek_info);
|
||||||
|
|
||||||
int recv_raw_ip(raw_info_t &raw_info,char * &payload,int &payloadlen);
|
int recv_raw_ip(raw_info_t &raw_info, char *&payload, int &payloadlen);
|
||||||
|
|
||||||
int send_raw_icmp(raw_info_t &raw_info, const char * payload, int payloadlen);
|
int send_raw_icmp(raw_info_t &raw_info, const char *payload, int payloadlen);
|
||||||
|
|
||||||
int send_raw_udp(raw_info_t &raw_info, const char * payload, int payloadlen);
|
int send_raw_udp(raw_info_t &raw_info, const char *payload, int payloadlen);
|
||||||
|
|
||||||
int send_raw_tcp(raw_info_t &raw_info,const char * payload, int payloadlen);
|
int send_raw_tcp(raw_info_t &raw_info, const char *payload, int payloadlen);
|
||||||
|
|
||||||
int recv_raw_icmp(raw_info_t &raw_info, char *&payload, int &payloadlen);
|
int recv_raw_icmp(raw_info_t &raw_info, char *&payload, int &payloadlen);
|
||||||
|
|
||||||
int recv_raw_udp(raw_info_t &raw_info, char *&payload, int &payloadlen);
|
int recv_raw_udp(raw_info_t &raw_info, char *&payload, int &payloadlen);
|
||||||
|
|
||||||
int recv_raw_tcp(raw_info_t &raw_info,char * &payload,int &payloadlen);
|
int recv_raw_tcp(raw_info_t &raw_info, char *&payload, int &payloadlen);
|
||||||
|
|
||||||
//int send_raw(raw_info_t &raw_info,const char * payload,int payloadlen);
|
// int send_raw(raw_info_t &raw_info,const char * payload,int payloadlen);
|
||||||
|
|
||||||
//int recv_raw(raw_info_t &raw_info,char * &payload,int &payloadlen);
|
// int recv_raw(raw_info_t &raw_info,char * &payload,int &payloadlen);
|
||||||
|
|
||||||
int send_raw0(raw_info_t &raw_info,const char * payload,int payloadlen);
|
int send_raw0(raw_info_t &raw_info, const char *payload, int payloadlen);
|
||||||
|
|
||||||
int recv_raw0(raw_info_t &raw_info,char * &payload,int &payloadlen);
|
int recv_raw0(raw_info_t &raw_info, char *&payload, int &payloadlen);
|
||||||
|
|
||||||
int after_send_raw0(raw_info_t &raw_info);
|
int after_send_raw0(raw_info_t &raw_info);
|
||||||
|
|
||||||
int after_recv_raw0(raw_info_t &raw_info);
|
int after_recv_raw0(raw_info_t &raw_info);
|
||||||
|
|
||||||
|
|
||||||
#endif /* NETWORK_H_ */
|
#endif /* NETWORK_H_ */
|
||||||
|
204
pcap_wrapper.cpp
@@ -2,124 +2,118 @@
|
|||||||
#include <pcap_wrapper.h>
|
#include <pcap_wrapper.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
int (*pcap_loop )(pcap_t *, int, pcap_handler, u_char *);
|
int (*pcap_loop)(pcap_t *, int, pcap_handler, u_char *);
|
||||||
int (*pcap_breakloop )(pcap_t *);
|
int (*pcap_breakloop)(pcap_t *);
|
||||||
|
|
||||||
pcap_t* (*pcap_create)(const char *, char *);
|
pcap_t *(*pcap_create)(const char *, char *);
|
||||||
|
|
||||||
int (*pcap_set_snaplen) (pcap_t *, int)=0;
|
int (*pcap_set_snaplen)(pcap_t *, int) = 0;
|
||||||
int (*pcap_set_promisc) (pcap_t *, int)=0;
|
int (*pcap_set_promisc)(pcap_t *, int) = 0;
|
||||||
int (*pcap_can_set_rfmon) (pcap_t *)=0;
|
int (*pcap_can_set_rfmon)(pcap_t *) = 0;
|
||||||
int (*pcap_set_rfmon )(pcap_t *, int)=0;
|
int (*pcap_set_rfmon)(pcap_t *, int) = 0;
|
||||||
int (*pcap_set_timeout)(pcap_t *, int)=0;
|
int (*pcap_set_timeout)(pcap_t *, int) = 0;
|
||||||
int (*pcap_set_buffer_size)(pcap_t *, int)=0;
|
int (*pcap_set_buffer_size)(pcap_t *, int) = 0;
|
||||||
int (*pcap_activate)(pcap_t *)=0;
|
int (*pcap_activate)(pcap_t *) = 0;
|
||||||
|
|
||||||
int (*pcap_setfilter)(pcap_t *, struct bpf_program *)=0;
|
int (*pcap_setfilter)(pcap_t *, struct bpf_program *) = 0;
|
||||||
int (*pcap_setdirection)(pcap_t *, pcap_direction_t)=0;
|
int (*pcap_setdirection)(pcap_t *, pcap_direction_t) = 0;
|
||||||
|
|
||||||
int (*pcap_datalink)(pcap_t *)=0;
|
int (*pcap_datalink)(pcap_t *) = 0;
|
||||||
|
|
||||||
void (*pcap_freecode)(struct bpf_program *)=0;
|
void (*pcap_freecode)(struct bpf_program *) = 0;
|
||||||
|
|
||||||
int (*pcap_compile)(pcap_t *, struct bpf_program *, const char *, int,
|
int (*pcap_compile)(pcap_t *, struct bpf_program *, const char *, int,
|
||||||
bpf_u_int32)=0;
|
bpf_u_int32) = 0;
|
||||||
|
|
||||||
char* (*pcap_geterr)(pcap_t *)=0;
|
char *(*pcap_geterr)(pcap_t *) = 0;
|
||||||
int (*pcap_sendpacket)(pcap_t *, const u_char *, int)=0;
|
int (*pcap_sendpacket)(pcap_t *, const u_char *, int) = 0;
|
||||||
|
|
||||||
char* (*pcap_lookupdev)(char *)=0;
|
char *(*pcap_lookupdev)(char *) = 0;
|
||||||
|
|
||||||
int (*pcap_findalldevs)(pcap_if_t **, char *)=0;
|
int (*pcap_findalldevs)(pcap_if_t **, char *) = 0;
|
||||||
|
|
||||||
struct init_pcap_t
|
struct init_pcap_t {
|
||||||
{
|
init_pcap_t() {
|
||||||
init_pcap_t()
|
init_pcap();
|
||||||
{
|
}
|
||||||
init_pcap();
|
|
||||||
}
|
|
||||||
|
|
||||||
}do_it;
|
|
||||||
|
|
||||||
static void init_npcap_dll_path()
|
} do_it;
|
||||||
{
|
|
||||||
BOOL(WINAPI *SetDllDirectory)(LPCTSTR);
|
|
||||||
char sysdir_name[512];
|
|
||||||
int len;
|
|
||||||
|
|
||||||
SetDllDirectory = (BOOL(WINAPI *)(LPCTSTR)) GetProcAddress(GetModuleHandle("kernel32.dll"), "SetDllDirectoryA");
|
static void init_npcap_dll_path() {
|
||||||
if (SetDllDirectory == NULL) {
|
BOOL(WINAPI * SetDllDirectory)
|
||||||
printf("Error in SetDllDirectory\n");
|
(LPCTSTR);
|
||||||
}
|
char sysdir_name[512];
|
||||||
else {
|
int len;
|
||||||
len = GetSystemDirectory(sysdir_name, 480); // be safe
|
|
||||||
if (!len)
|
SetDllDirectory = (BOOL(WINAPI *)(LPCTSTR))GetProcAddress(GetModuleHandle("kernel32.dll"), "SetDllDirectoryA");
|
||||||
printf("Error in GetSystemDirectory (%d)\n", (int)GetLastError());
|
if (SetDllDirectory == NULL) {
|
||||||
strcat(sysdir_name, "\\Npcap");
|
printf("Error in SetDllDirectory\n");
|
||||||
if (SetDllDirectory(sysdir_name) == 0)
|
} else {
|
||||||
printf("Error in SetDllDirectory(\"System32\\Npcap\")\n");
|
len = GetSystemDirectory(sysdir_name, 480); // be safe
|
||||||
}
|
if (!len)
|
||||||
|
printf("Error in GetSystemDirectory (%d)\n", (int)GetLastError());
|
||||||
|
strcat(sysdir_name, "\\Npcap");
|
||||||
|
if (SetDllDirectory(sysdir_name) == 0)
|
||||||
|
printf("Error in SetDllDirectory(\"System32\\Npcap\")\n");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#define EXPORT_FUN(XXX) do{ XXX= (__typeof__(XXX)) GetProcAddress(wpcap, #XXX); }while(0)
|
#define EXPORT_FUN(XXX) \
|
||||||
int init_pcap()
|
do { \
|
||||||
{
|
XXX = (__typeof__(XXX))GetProcAddress(wpcap, #XXX); \
|
||||||
HMODULE wpcap=LoadLibrary("wpcap.dll");
|
} while (0)
|
||||||
if(wpcap!=0)
|
int init_pcap() {
|
||||||
{
|
HMODULE wpcap = LoadLibrary("wpcap.dll");
|
||||||
printf("using system32/wpcap.dll\n");
|
if (wpcap != 0) {
|
||||||
}
|
printf("using system32/wpcap.dll\n");
|
||||||
else
|
} else {
|
||||||
{
|
init_npcap_dll_path();
|
||||||
init_npcap_dll_path();
|
// SetDllDirectory("C:\\Windows\\System32\\Npcap\\");
|
||||||
//SetDllDirectory("C:\\Windows\\System32\\Npcap\\");
|
wpcap = LoadLibrary("wpcap.dll");
|
||||||
wpcap=LoadLibrary("wpcap.dll");
|
if (wpcap != 0)
|
||||||
if(wpcap!=0)
|
printf("using system32/npcap/wpcap.dll\n");
|
||||||
printf("using system32/npcap/wpcap.dll\n");
|
}
|
||||||
}
|
if (wpcap == 0) {
|
||||||
if(wpcap==0)
|
printf("cant not open wpcap.dll, make sure winpcap/npcap is installed\n");
|
||||||
{
|
exit(-1);
|
||||||
printf("cant not open wpcap.dll, make sure winpcap/npcap is installed\n");
|
}
|
||||||
exit(-1);
|
assert(wpcap != 0);
|
||||||
}
|
|
||||||
assert(wpcap!=0);
|
|
||||||
|
|
||||||
EXPORT_FUN(pcap_loop);
|
|
||||||
EXPORT_FUN(pcap_breakloop);
|
|
||||||
EXPORT_FUN(pcap_create);
|
|
||||||
EXPORT_FUN(pcap_set_snaplen);
|
|
||||||
EXPORT_FUN(pcap_set_promisc);
|
|
||||||
EXPORT_FUN(pcap_set_timeout);
|
|
||||||
EXPORT_FUN(pcap_activate);
|
|
||||||
EXPORT_FUN(pcap_setfilter);
|
|
||||||
EXPORT_FUN(pcap_setdirection);
|
|
||||||
EXPORT_FUN(pcap_datalink);
|
|
||||||
EXPORT_FUN(pcap_freecode);
|
|
||||||
EXPORT_FUN(pcap_compile);
|
|
||||||
EXPORT_FUN(pcap_geterr);
|
|
||||||
EXPORT_FUN(pcap_sendpacket);
|
|
||||||
EXPORT_FUN(pcap_lookupdev);
|
|
||||||
EXPORT_FUN(pcap_findalldevs);
|
|
||||||
/*
|
|
||||||
pcap_loop = (__typeof__(pcap_loop))GetProcAddress(wpcap, "pcap_loop");
|
|
||||||
pcap_create = (__typeof__(pcap_create))GetProcAddress(wpcap, "pcap_create");
|
|
||||||
pcap_set_snaplen = (__typeof__(pcap_set_snaplen))GetProcAddress(wpcap, "pcap_set_snaplen");
|
|
||||||
pcap_set_promisc = (__typeof__(pcap_set_promisc))GetProcAddress(wpcap, "pcap_set_promisc");
|
|
||||||
pcap_set_timeout = (__typeof__(pcap_set_timeout))GetProcAddress(wpcap, "pcap_set_timeout");
|
|
||||||
pcap_activate = (__typeof__(pcap_activate))GetProcAddress(wpcap, "pcap_activate");
|
|
||||||
pcap_setfilter = (__typeof__(pcap_setfilter))GetProcAddress(wpcap, "pcap_setfilter");
|
|
||||||
pcap_setdirection = (__typeof__(pcap_setdirection))GetProcAddress(wpcap, "pcap_setdirection");
|
|
||||||
pcap_datalink = (__typeof__(pcap_datalink))GetProcAddress(wpcap, "pcap_datalink");
|
|
||||||
pcap_freecode = (__typeof__(pcap_freecode))GetProcAddress(wpcap, "pcap_freecode");
|
|
||||||
pcap_compile = (__typeof__(pcap_compile))GetProcAddress(wpcap, "pcap_compile");
|
|
||||||
pcap_geterr = (__typeof__(pcap_geterr))GetProcAddress(wpcap, "pcap_geterr");
|
|
||||||
pcap_sendpacket = (__typeof__(pcap_sendpacket))GetProcAddress(wpcap, "pcap_sendpacket");
|
|
||||||
pcap_lookupdev = (__typeof__(pcap_lookupdev))GetProcAddress(wpcap, "pcap_lookupdev");
|
|
||||||
pcap_findalldevs = (__typeof__(pcap_findalldevs))GetProcAddress(wpcap, "pcap_findalldevs");
|
|
||||||
//pcap_loop = (__typeof__(pcap_loop))GetProcAddress(wpcap, "pcap_loop");
|
|
||||||
//pcap_loop = (__typeof__(pcap_loop))GetProcAddress(wpcap, "pcap_loop");
|
|
||||||
//pcap_loop = (__typeof__(pcap_loop))GetProcAddress(wpcap, "pcap_loop");
|
|
||||||
*/
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
|
EXPORT_FUN(pcap_loop);
|
||||||
|
EXPORT_FUN(pcap_breakloop);
|
||||||
|
EXPORT_FUN(pcap_create);
|
||||||
|
EXPORT_FUN(pcap_set_snaplen);
|
||||||
|
EXPORT_FUN(pcap_set_promisc);
|
||||||
|
EXPORT_FUN(pcap_set_timeout);
|
||||||
|
EXPORT_FUN(pcap_activate);
|
||||||
|
EXPORT_FUN(pcap_setfilter);
|
||||||
|
EXPORT_FUN(pcap_setdirection);
|
||||||
|
EXPORT_FUN(pcap_datalink);
|
||||||
|
EXPORT_FUN(pcap_freecode);
|
||||||
|
EXPORT_FUN(pcap_compile);
|
||||||
|
EXPORT_FUN(pcap_geterr);
|
||||||
|
EXPORT_FUN(pcap_sendpacket);
|
||||||
|
EXPORT_FUN(pcap_lookupdev);
|
||||||
|
EXPORT_FUN(pcap_findalldevs);
|
||||||
|
/*
|
||||||
|
pcap_loop = (__typeof__(pcap_loop))GetProcAddress(wpcap, "pcap_loop");
|
||||||
|
pcap_create = (__typeof__(pcap_create))GetProcAddress(wpcap, "pcap_create");
|
||||||
|
pcap_set_snaplen = (__typeof__(pcap_set_snaplen))GetProcAddress(wpcap, "pcap_set_snaplen");
|
||||||
|
pcap_set_promisc = (__typeof__(pcap_set_promisc))GetProcAddress(wpcap, "pcap_set_promisc");
|
||||||
|
pcap_set_timeout = (__typeof__(pcap_set_timeout))GetProcAddress(wpcap, "pcap_set_timeout");
|
||||||
|
pcap_activate = (__typeof__(pcap_activate))GetProcAddress(wpcap, "pcap_activate");
|
||||||
|
pcap_setfilter = (__typeof__(pcap_setfilter))GetProcAddress(wpcap, "pcap_setfilter");
|
||||||
|
pcap_setdirection = (__typeof__(pcap_setdirection))GetProcAddress(wpcap, "pcap_setdirection");
|
||||||
|
pcap_datalink = (__typeof__(pcap_datalink))GetProcAddress(wpcap, "pcap_datalink");
|
||||||
|
pcap_freecode = (__typeof__(pcap_freecode))GetProcAddress(wpcap, "pcap_freecode");
|
||||||
|
pcap_compile = (__typeof__(pcap_compile))GetProcAddress(wpcap, "pcap_compile");
|
||||||
|
pcap_geterr = (__typeof__(pcap_geterr))GetProcAddress(wpcap, "pcap_geterr");
|
||||||
|
pcap_sendpacket = (__typeof__(pcap_sendpacket))GetProcAddress(wpcap, "pcap_sendpacket");
|
||||||
|
pcap_lookupdev = (__typeof__(pcap_lookupdev))GetProcAddress(wpcap, "pcap_lookupdev");
|
||||||
|
pcap_findalldevs = (__typeof__(pcap_findalldevs))GetProcAddress(wpcap, "pcap_findalldevs");
|
||||||
|
//pcap_loop = (__typeof__(pcap_loop))GetProcAddress(wpcap, "pcap_loop");
|
||||||
|
//pcap_loop = (__typeof__(pcap_loop))GetProcAddress(wpcap, "pcap_loop");
|
||||||
|
//pcap_loop = (__typeof__(pcap_loop))GetProcAddress(wpcap, "pcap_loop");
|
||||||
|
*/
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
128
pcap_wrapper.h
@@ -1,127 +1,117 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
//#ifdef __cplusplus
|
//#ifdef __cplusplus
|
||||||
//extern "C" {
|
// extern "C" {
|
||||||
//#endif
|
//#endif
|
||||||
|
|
||||||
//#include <sys/time.h>
|
//#include <sys/time.h>
|
||||||
//#include <stdint.h>
|
//#include <stdint.h>
|
||||||
|
|
||||||
struct bpf_program
|
struct bpf_program {
|
||||||
{
|
char a[4096];
|
||||||
char a[2000];
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct pcap_t
|
struct pcap_t {
|
||||||
{
|
char a[4096];
|
||||||
char a[2000];
|
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef unsigned int bpf_u_int32;
|
typedef unsigned int bpf_u_int32;
|
||||||
|
|
||||||
typedef struct my_timeval {
|
typedef struct my_timeval {
|
||||||
int tv_sec;
|
int tv_sec;
|
||||||
int tv_usec;
|
int tv_usec;
|
||||||
} my_timeval;
|
} my_timeval;
|
||||||
|
|
||||||
struct pcap_pkthdr {
|
struct pcap_pkthdr {
|
||||||
struct my_timeval ts; /* time stamp */
|
struct my_timeval ts; /* time stamp */
|
||||||
bpf_u_int32 caplen; /* length of portion present */
|
bpf_u_int32 caplen; /* length of portion present */
|
||||||
bpf_u_int32 len; /* length this packet (off wire) */
|
bpf_u_int32 len; /* length this packet (off wire) */
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
PCAP_D_INOUT = 0,
|
PCAP_D_INOUT = 0,
|
||||||
PCAP_D_IN,
|
PCAP_D_IN,
|
||||||
PCAP_D_OUT
|
PCAP_D_OUT
|
||||||
} pcap_direction_t;
|
} pcap_direction_t;
|
||||||
|
|
||||||
|
|
||||||
struct pcap_addr {
|
struct pcap_addr {
|
||||||
struct pcap_addr *next;
|
struct pcap_addr *next;
|
||||||
struct sockaddr *addr; /* address */
|
struct sockaddr *addr; /* address */
|
||||||
struct sockaddr *netmask; /* netmask for that address */
|
struct sockaddr *netmask; /* netmask for that address */
|
||||||
struct sockaddr *broadaddr; /* broadcast address for that address */
|
struct sockaddr *broadaddr; /* broadcast address for that address */
|
||||||
struct sockaddr *dstaddr; /* P2P destination address for that address */
|
struct sockaddr *dstaddr; /* P2P destination address for that address */
|
||||||
};
|
};
|
||||||
|
|
||||||
struct pcap_if {
|
struct pcap_if {
|
||||||
struct pcap_if *next;
|
struct pcap_if *next;
|
||||||
char *name; /* name to hand to "pcap_open_live()" */
|
char *name; /* name to hand to "pcap_open_live()" */
|
||||||
char *description; /* textual description of interface, or NULL */
|
char *description; /* textual description of interface, or NULL */
|
||||||
struct pcap_addr *addresses;
|
struct pcap_addr *addresses;
|
||||||
bpf_u_int32 flags; /* PCAP_IF_ interface flags */
|
bpf_u_int32 flags; /* PCAP_IF_ interface flags */
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef struct pcap_if pcap_if_t;
|
typedef struct pcap_if pcap_if_t;
|
||||||
typedef struct pcap_addr pcap_addr_t;
|
typedef struct pcap_addr pcap_addr_t;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
typedef unsigned char u_char;
|
typedef unsigned char u_char;
|
||||||
|
|
||||||
|
|
||||||
#define PCAP_ERRBUF_SIZE 256
|
#define PCAP_ERRBUF_SIZE 256
|
||||||
|
|
||||||
#define DLT_NULL 0 /* BSD loopback encapsulation */
|
#define DLT_NULL 0 /* BSD loopback encapsulation */
|
||||||
#define DLT_EN10MB 1 /* Ethernet (10Mb) */
|
#define DLT_EN10MB 1 /* Ethernet (10Mb) */
|
||||||
#define DLT_EN3MB 2 /* Experimental Ethernet (3Mb) */
|
#define DLT_EN3MB 2 /* Experimental Ethernet (3Mb) */
|
||||||
#define DLT_AX25 3 /* Amateur Radio AX.25 */
|
#define DLT_AX25 3 /* Amateur Radio AX.25 */
|
||||||
#define DLT_PRONET 4 /* Proteon ProNET Token Ring */
|
#define DLT_PRONET 4 /* Proteon ProNET Token Ring */
|
||||||
#define DLT_CHAOS 5 /* Chaos */
|
#define DLT_CHAOS 5 /* Chaos */
|
||||||
#define DLT_IEEE802 6 /* 802.5 Token Ring */
|
#define DLT_IEEE802 6 /* 802.5 Token Ring */
|
||||||
#define DLT_ARCNET 7 /* ARCNET, with BSD-style header */
|
#define DLT_ARCNET 7 /* ARCNET, with BSD-style header */
|
||||||
#define DLT_SLIP 8 /* Serial Line IP */
|
#define DLT_SLIP 8 /* Serial Line IP */
|
||||||
#define DLT_PPP 9 /* Point-to-point Protocol */
|
#define DLT_PPP 9 /* Point-to-point Protocol */
|
||||||
#define DLT_FDDI 10 /* FDDI */
|
#define DLT_FDDI 10 /* FDDI */
|
||||||
#define DLT_LINUX_SLL 113
|
#define DLT_LINUX_SLL 113
|
||||||
|
|
||||||
#define PCAP_NETMASK_UNKNOWN 0xffffffff
|
#define PCAP_NETMASK_UNKNOWN 0xffffffff
|
||||||
|
|
||||||
typedef void (*pcap_handler)(u_char *, const struct pcap_pkthdr *,
|
typedef void (*pcap_handler)(u_char *, const struct pcap_pkthdr *,
|
||||||
const u_char *);
|
const u_char *);
|
||||||
|
|
||||||
extern int (*pcap_loop )(pcap_t *, int, pcap_handler, u_char *);
|
extern int (*pcap_loop)(pcap_t *, int, pcap_handler, u_char *);
|
||||||
|
|
||||||
extern int (*pcap_breakloop )(pcap_t *);
|
extern int (*pcap_breakloop)(pcap_t *);
|
||||||
|
|
||||||
extern pcap_t* (*pcap_create)(const char *, char *);
|
extern pcap_t *(*pcap_create)(const char *, char *);
|
||||||
|
|
||||||
extern int (*pcap_set_snaplen) (pcap_t *, int);
|
extern int (*pcap_set_snaplen)(pcap_t *, int);
|
||||||
extern int (*pcap_set_promisc) (pcap_t *, int);
|
extern int (*pcap_set_promisc)(pcap_t *, int);
|
||||||
extern int (*pcap_can_set_rfmon) (pcap_t *);
|
extern int (*pcap_can_set_rfmon)(pcap_t *);
|
||||||
extern int (*pcap_set_rfmon )(pcap_t *, int);
|
extern int (*pcap_set_rfmon)(pcap_t *, int);
|
||||||
extern int (*pcap_set_timeout)(pcap_t *, int);
|
extern int (*pcap_set_timeout)(pcap_t *, int);
|
||||||
extern int (*pcap_set_buffer_size)(pcap_t *, int);
|
extern int (*pcap_set_buffer_size)(pcap_t *, int);
|
||||||
extern int (*pcap_activate)(pcap_t *);
|
extern int (*pcap_activate)(pcap_t *);
|
||||||
|
|
||||||
extern int (*pcap_setfilter)(pcap_t *, struct bpf_program *);
|
extern int (*pcap_setfilter)(pcap_t *, struct bpf_program *);
|
||||||
extern int (*pcap_setdirection)(pcap_t *, pcap_direction_t);
|
extern int (*pcap_setdirection)(pcap_t *, pcap_direction_t);
|
||||||
|
|
||||||
extern int (*pcap_datalink)(pcap_t *);
|
extern int (*pcap_datalink)(pcap_t *);
|
||||||
|
|
||||||
extern void (*pcap_freecode)(struct bpf_program *);
|
extern void (*pcap_freecode)(struct bpf_program *);
|
||||||
|
|
||||||
extern int (*pcap_compile)(pcap_t *, struct bpf_program *, const char *, int,
|
extern int (*pcap_compile)(pcap_t *, struct bpf_program *, const char *, int,
|
||||||
bpf_u_int32);
|
bpf_u_int32);
|
||||||
|
|
||||||
extern char* (*pcap_geterr)(pcap_t *);
|
extern char *(*pcap_geterr)(pcap_t *);
|
||||||
extern int (*pcap_sendpacket)(pcap_t *, const u_char *, int);
|
extern int (*pcap_sendpacket)(pcap_t *, const u_char *, int);
|
||||||
|
|
||||||
extern char* (*pcap_lookupdev)(char *);
|
extern char *(*pcap_lookupdev)(char *);
|
||||||
|
|
||||||
extern int (*pcap_findalldevs)(pcap_if_t **, char *);
|
extern int (*pcap_findalldevs)(pcap_if_t **, char *);
|
||||||
|
|
||||||
inline int pcap_set_immediate_mode(pcap_t *,int)
|
inline int pcap_set_immediate_mode(pcap_t *, int) {
|
||||||
{
|
return 0;
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//#ifdef __cplusplus
|
//#ifdef __cplusplus
|
||||||
//}
|
//}
|
||||||
//#endif
|
//#endif
|
||||||
|
|
||||||
int init_pcap();
|
int init_pcap();
|
||||||
|
|
||||||
|
800
server.cpp
Normal file
@@ -0,0 +1,800 @@
|
|||||||
|
/*
|
||||||
|
* server.cpp
|
||||||
|
*
|
||||||
|
* Created on: Aug 29, 2018
|
||||||
|
* Author: root
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef UDP2RAW_MP
|
||||||
|
|
||||||
|
#include "common.h"
|
||||||
|
#include "network.h"
|
||||||
|
#include "connection.h"
|
||||||
|
#include "misc.h"
|
||||||
|
#include "log.h"
|
||||||
|
#include "lib/md5.h"
|
||||||
|
#include "encrypt.h"
|
||||||
|
#include "fd_manager.h"
|
||||||
|
|
||||||
|
int server_on_timer_multi(conn_info_t &conn_info) // for server. called when a timer is ready in epoll.for server,there will be one timer for every connection
|
||||||
|
// there is also a global timer for server,but its not handled here
|
||||||
|
{
|
||||||
|
char ip_port[max_addr_len];
|
||||||
|
// u32_t ip=conn_info.raw_info.send_info.dst_ip;
|
||||||
|
// u32_t port=conn_info.raw_info.send_info.dst_port;
|
||||||
|
|
||||||
|
address_t tmp_addr;
|
||||||
|
tmp_addr.from_ip_port_new(raw_ip_version, &conn_info.raw_info.send_info.new_dst_ip, conn_info.raw_info.send_info.dst_port);
|
||||||
|
// sprintf(ip_port,"%s:%d",my_ntoa(ip),port);
|
||||||
|
tmp_addr.to_str(ip_port);
|
||||||
|
|
||||||
|
// keep_iptables_rule();
|
||||||
|
mylog(log_trace, "server timer!\n");
|
||||||
|
raw_info_t &raw_info = conn_info.raw_info;
|
||||||
|
|
||||||
|
assert(conn_info.state.server_current_state == server_ready);
|
||||||
|
|
||||||
|
if (conn_info.state.server_current_state == server_ready) {
|
||||||
|
conn_info.blob->conv_manager.s.clear_inactive(ip_port);
|
||||||
|
/*
|
||||||
|
if( get_current_time()-conn_info.last_hb_recv_time>heartbeat_timeout )
|
||||||
|
{
|
||||||
|
mylog(log_trace,"%lld %lld\n",get_current_time(),conn_info.last_state_time);
|
||||||
|
conn_info.server_current_state=server_nothing;
|
||||||
|
|
||||||
|
//conn_manager.current_ready_ip=0;
|
||||||
|
//conn_manager.current_ready_port=0;
|
||||||
|
|
||||||
|
mylog(log_info,"changed state to server_nothing\n");
|
||||||
|
return 0;
|
||||||
|
}*/
|
||||||
|
// dont need to do this at server,conn_manger will clear expired connections
|
||||||
|
|
||||||
|
if (get_current_time() - conn_info.last_hb_sent_time < heartbeat_interval) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
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();
|
||||||
|
|
||||||
|
mylog(log_debug, "heart beat sent<%x,%x>\n", conn_info.my_id, conn_info.oppsite_id);
|
||||||
|
} else {
|
||||||
|
mylog(log_fatal, "this shouldnt happen!\n");
|
||||||
|
myexit(-1);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
int server_on_raw_recv_ready(conn_info_t &conn_info, char *ip_port, char type, char *data, int data_len) // called while the state for a connection is server_ready
|
||||||
|
// receives data and heart beat by recv_safer.
|
||||||
|
{
|
||||||
|
raw_info_t &raw_info = conn_info.raw_info;
|
||||||
|
packet_info_t &send_info = conn_info.raw_info.send_info;
|
||||||
|
packet_info_t &recv_info = conn_info.raw_info.recv_info;
|
||||||
|
// char ip_port[40];
|
||||||
|
|
||||||
|
// sprintf(ip_port,"%s:%d",my_ntoa(recv_info.src_ip),recv_info.src_port);
|
||||||
|
|
||||||
|
/*
|
||||||
|
if (recv_info.src_ip != send_info.dst_ip
|
||||||
|
|| recv_info.src_port != send_info.dst_port) {
|
||||||
|
mylog(log_debug, "unexpected adress\n");
|
||||||
|
return 0;
|
||||||
|
}*/
|
||||||
|
|
||||||
|
if (type == 'h' && data_len >= 0) {
|
||||||
|
// u32_t tmp = ntohl(*((u32_t *) &data[sizeof(u32_t)]));
|
||||||
|
mylog(log_debug, "[%s][hb]received hb \n", ip_port);
|
||||||
|
conn_info.last_hb_recv_time = get_current_time();
|
||||||
|
return 0;
|
||||||
|
} else if (type == 'd' && data_len >= int(sizeof(u32_t))) {
|
||||||
|
// u32_t tmp_conv_id = ntohl(*((u32_t *) &data[0]));
|
||||||
|
my_id_t tmp_conv_id;
|
||||||
|
memcpy(&tmp_conv_id, &data[0], sizeof(tmp_conv_id));
|
||||||
|
tmp_conv_id = ntohl(tmp_conv_id);
|
||||||
|
|
||||||
|
if (hb_mode == 0)
|
||||||
|
conn_info.last_hb_recv_time = get_current_time();
|
||||||
|
|
||||||
|
mylog(log_trace, "conv:%u\n", tmp_conv_id);
|
||||||
|
if (!conn_info.blob->conv_manager.s.is_conv_used(tmp_conv_id)) {
|
||||||
|
if (conn_info.blob->conv_manager.s.get_size() >= max_conv_num) {
|
||||||
|
mylog(log_warn,
|
||||||
|
"[%s]ignored new conv %x connect bc max_conv_num exceed\n", ip_port,
|
||||||
|
tmp_conv_id);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
struct sockaddr_in remote_addr_in={0};
|
||||||
|
|
||||||
|
socklen_t slen = sizeof(sockaddr_in);
|
||||||
|
//memset(&remote_addr_in, 0, sizeof(remote_addr_in));
|
||||||
|
remote_addr_in.sin_family = AF_INET;
|
||||||
|
remote_addr_in.sin_port = htons(remote_port);
|
||||||
|
remote_addr_in.sin_addr.s_addr = remote_ip_uint32;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
int new_udp_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
||||||
|
|
||||||
|
|
||||||
|
if (new_udp_fd < 0) {
|
||||||
|
mylog(log_warn, "[%s]create udp_fd error\n",ip_port);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
setnonblocking(new_udp_fd);
|
||||||
|
set_buf_size(new_udp_fd,socket_buf_size);
|
||||||
|
|
||||||
|
mylog(log_debug, "[%s]created new udp_fd %d\n",ip_port, new_udp_fd);
|
||||||
|
int ret = connect(new_udp_fd, (struct sockaddr *) &remote_addr_in,
|
||||||
|
slen);
|
||||||
|
if (ret != 0) {
|
||||||
|
mylog(log_warn, "udp fd connect fail\n");
|
||||||
|
close(new_udp_fd);
|
||||||
|
return -1;
|
||||||
|
}*/
|
||||||
|
|
||||||
|
int new_udp_fd = remote_addr.new_connected_udp_fd();
|
||||||
|
if (new_udp_fd < 0) {
|
||||||
|
mylog(log_warn, "[%s]new_connected_udp_fd() failed\n", ip_port);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct epoll_event ev;
|
||||||
|
|
||||||
|
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 = new_udp_fd64;
|
||||||
|
|
||||||
|
int ret = epoll_ctl(epollfd, EPOLL_CTL_ADD, new_udp_fd, &ev);
|
||||||
|
|
||||||
|
if (ret != 0) {
|
||||||
|
mylog(log_warn, "[%s]add udp_fd error\n", ip_port);
|
||||||
|
close(new_udp_fd);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
conn_info.blob->conv_manager.s.insert_conv(tmp_conv_id, new_udp_fd64);
|
||||||
|
|
||||||
|
// 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);
|
||||||
|
|
||||||
|
mylog(log_info, "[%s]new conv conv_id=%x, assigned fd=%d\n", ip_port,
|
||||||
|
tmp_conv_id, new_udp_fd);
|
||||||
|
}
|
||||||
|
|
||||||
|
fd64_t fd64 = conn_info.blob->conv_manager.s.find_data_by_conv(tmp_conv_id);
|
||||||
|
|
||||||
|
conn_info.blob->conv_manager.s.update_active_time(tmp_conv_id);
|
||||||
|
|
||||||
|
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),
|
||||||
|
data_len - (sizeof(u32_t)), 0);
|
||||||
|
|
||||||
|
mylog(log_trace, "[%s]%d byte sent ,fd :%d\n ", ip_port, ret, fd);
|
||||||
|
if (ret < 0) {
|
||||||
|
mylog(log_warn, "send returned %d\n", ret);
|
||||||
|
// perror("what happened????");
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int server_on_raw_recv_pre_ready(conn_info_t &conn_info, char *ip_port, u32_t tmp_oppsite_const_id) // do prepare work before state change to server ready for a specifc connection
|
||||||
|
// connection recovery is also handle here
|
||||||
|
{
|
||||||
|
// u32_t ip;uint16_t port;
|
||||||
|
// ip=conn_info.raw_info.recv_info.src_ip;
|
||||||
|
// port=conn_info.raw_info.recv_info.src_port;
|
||||||
|
// char ip_port[40];
|
||||||
|
// sprintf(ip_port,"%s:%d",my_ntoa(ip),port);
|
||||||
|
|
||||||
|
mylog(log_info, "[%s]received handshake oppsite_id:%x my_id:%x\n", ip_port, conn_info.oppsite_id, conn_info.my_id);
|
||||||
|
|
||||||
|
mylog(log_info, "[%s]oppsite const_id:%x \n", ip_port, tmp_oppsite_const_id);
|
||||||
|
if (conn_manager.const_id_mp.find(tmp_oppsite_const_id) == conn_manager.const_id_mp.end()) {
|
||||||
|
// conn_manager.const_id_mp=
|
||||||
|
|
||||||
|
if (conn_manager.ready_num >= max_ready_conn_num) {
|
||||||
|
mylog(log_info, "[%s]max_ready_conn_num,cant turn to ready\n", ip_port);
|
||||||
|
conn_info.state.server_current_state = server_idle;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
conn_info.prepare();
|
||||||
|
conn_info.state.server_current_state = server_ready;
|
||||||
|
conn_info.oppsite_const_id = tmp_oppsite_const_id;
|
||||||
|
conn_manager.ready_num++;
|
||||||
|
conn_manager.const_id_mp[tmp_oppsite_const_id] = &conn_info;
|
||||||
|
|
||||||
|
// conn_info.last_state_time=get_current_time(); //dont change this!!!!!!!!!!!!!!!!!!!!!!!!!
|
||||||
|
|
||||||
|
// conn_manager.current_ready_ip=ip;
|
||||||
|
// conn_manager.current_ready_port=port;
|
||||||
|
|
||||||
|
// my_id=conn_info.my_id;
|
||||||
|
// oppsite_id=conn_info.oppsite_id;
|
||||||
|
conn_info.last_hb_recv_time = get_current_time();
|
||||||
|
|
||||||
|
conn_info.last_hb_sent_time = conn_info.last_hb_recv_time; //=get_current_time()
|
||||||
|
|
||||||
|
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);
|
||||||
|
conn_info.blob->anti_replay.re_init();
|
||||||
|
|
||||||
|
// g_conn_info=conn_info;
|
||||||
|
int new_timer_fd;
|
||||||
|
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]
|
||||||
|
/*
|
||||||
|
if(oppsite_const_id!=0&&tmp_oppsite_const_id!=oppsite_const_id) //TODO MOVE TO READY
|
||||||
|
{
|
||||||
|
mylog(log_info,"cleared all conv bc of const id doesnt match\n");
|
||||||
|
conv_manager.clear();
|
||||||
|
}*/
|
||||||
|
// oppsite_const_id=tmp_oppsite_const_id;
|
||||||
|
} else {
|
||||||
|
conn_info_t &ori_conn_info = *conn_manager.const_id_mp[tmp_oppsite_const_id];
|
||||||
|
|
||||||
|
if (ori_conn_info.state.server_current_state == server_ready) {
|
||||||
|
if (conn_info.last_state_time < ori_conn_info.last_state_time) {
|
||||||
|
mylog(log_info, "[%s]conn_info.last_state_time<ori_conn_info.last_state_time. ignored new handshake\n", ip_port);
|
||||||
|
conn_info.state.server_current_state = server_idle;
|
||||||
|
conn_info.oppsite_const_id = 0;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
address_t addr1;
|
||||||
|
addr1.from_ip_port_new(raw_ip_version, &ori_conn_info.raw_info.recv_info.new_src_ip, ori_conn_info.raw_info.recv_info.src_port);
|
||||||
|
if (!conn_manager.exist(addr1)) // TODO remove this
|
||||||
|
{
|
||||||
|
mylog(log_fatal, "[%s]this shouldnt happen\n", ip_port);
|
||||||
|
myexit(-1);
|
||||||
|
}
|
||||||
|
address_t addr2;
|
||||||
|
addr2.from_ip_port_new(raw_ip_version, &conn_info.raw_info.recv_info.new_src_ip, conn_info.raw_info.recv_info.src_port);
|
||||||
|
if (!conn_manager.exist(addr2)) // TODO remove this
|
||||||
|
{
|
||||||
|
mylog(log_fatal, "[%s]this shouldnt happen2\n", ip_port);
|
||||||
|
myexit(-1);
|
||||||
|
}
|
||||||
|
conn_info_t *&p_ori = conn_manager.find_insert_p(addr1);
|
||||||
|
conn_info_t *&p = conn_manager.find_insert_p(addr2);
|
||||||
|
conn_info_t *tmp = p;
|
||||||
|
p = p_ori;
|
||||||
|
p_ori = tmp;
|
||||||
|
|
||||||
|
mylog(log_info, "[%s]grabbed a connection\n", ip_port);
|
||||||
|
|
||||||
|
// ori_conn_info.state.server_current_state=server_ready;
|
||||||
|
ori_conn_info.recover(conn_info);
|
||||||
|
|
||||||
|
// send_safer(ori_conn_info, 'h',hb_buf, hb_len);
|
||||||
|
// 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();
|
||||||
|
|
||||||
|
conn_info.state.server_current_state = server_idle;
|
||||||
|
conn_info.oppsite_const_id = 0;
|
||||||
|
|
||||||
|
} else {
|
||||||
|
mylog(log_fatal, "[%s]this should never happen\n", ip_port);
|
||||||
|
myexit(-1);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
int server_on_raw_recv_handshake1(conn_info_t &conn_info, char *ip_port, char *data, int data_len) // called when server received a handshake1 packet from client
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
|
||||||
|
// u32_t ip=conn_info.raw_info.recv_info.src_ip;
|
||||||
|
// uint16_t port=conn_info.raw_info.recv_info.src_port;
|
||||||
|
|
||||||
|
// char ip_port[40];
|
||||||
|
// sprintf(ip_port,"%s:%d",my_ntoa(ip),port);
|
||||||
|
|
||||||
|
if (data_len < int(3 * sizeof(my_id_t))) {
|
||||||
|
mylog(log_debug, "[%s] data_len=%d too short to be a handshake\n", ip_port, data_len);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
// id_t tmp_oppsite_id= ntohl(* ((u32_t *)&data[0]));
|
||||||
|
my_id_t tmp_oppsite_id;
|
||||||
|
memcpy(&tmp_oppsite_id, (u32_t *)&data[0], sizeof(tmp_oppsite_id));
|
||||||
|
tmp_oppsite_id = ntohl(tmp_oppsite_id);
|
||||||
|
|
||||||
|
// id_t tmp_my_id=ntohl(* ((u32_t *)&data[sizeof(id_t)]));
|
||||||
|
my_id_t tmp_my_id;
|
||||||
|
memcpy(&tmp_my_id, &data[sizeof(my_id_t)], sizeof(tmp_my_id));
|
||||||
|
tmp_my_id = ntohl(tmp_my_id);
|
||||||
|
|
||||||
|
if (tmp_my_id == 0) // received init handshake again
|
||||||
|
{
|
||||||
|
if (raw_mode == mode_faketcp) {
|
||||||
|
send_info.seq = recv_info.ack_seq;
|
||||||
|
send_info.ack_seq = recv_info.seq + raw_info.recv_info.data_len;
|
||||||
|
send_info.ts_ack = recv_info.ts;
|
||||||
|
}
|
||||||
|
if (raw_mode == mode_icmp) {
|
||||||
|
send_info.my_icmp_seq = recv_info.my_icmp_seq;
|
||||||
|
}
|
||||||
|
send_handshake(raw_info, conn_info.my_id, tmp_oppsite_id, const_id); //////////////send
|
||||||
|
|
||||||
|
mylog(log_info, "[%s]changed state to server_handshake1,my_id is %x\n", ip_port, conn_info.my_id);
|
||||||
|
} else if (tmp_my_id == conn_info.my_id) {
|
||||||
|
conn_info.oppsite_id = tmp_oppsite_id;
|
||||||
|
// id_t tmp_oppsite_const_id=ntohl(* ((u32_t *)&data[sizeof(id_t)*2]));
|
||||||
|
|
||||||
|
my_id_t tmp_oppsite_const_id;
|
||||||
|
memcpy(&tmp_oppsite_const_id, &data[sizeof(my_id_t) * 2], sizeof(tmp_oppsite_const_id));
|
||||||
|
tmp_oppsite_const_id = ntohl(tmp_oppsite_const_id);
|
||||||
|
|
||||||
|
if (raw_mode == mode_faketcp) {
|
||||||
|
send_info.seq = recv_info.ack_seq;
|
||||||
|
send_info.ack_seq = recv_info.seq + raw_info.recv_info.data_len;
|
||||||
|
send_info.ts_ack = recv_info.ts;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (raw_mode == mode_icmp) {
|
||||||
|
send_info.my_icmp_seq = recv_info.my_icmp_seq;
|
||||||
|
}
|
||||||
|
|
||||||
|
server_on_raw_recv_pre_ready(conn_info, ip_port, tmp_oppsite_const_id);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
mylog(log_debug, "[%s]invalid my_id %x,my_id is %x\n", ip_port, tmp_my_id, conn_info.my_id);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
int server_on_recv_safer_multi(conn_info_t &conn_info, char type, char *data, int data_len) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
int server_on_raw_recv_multi() // called when server received an raw packet
|
||||||
|
{
|
||||||
|
char dummy_buf[buf_len];
|
||||||
|
raw_info_t peek_raw_info;
|
||||||
|
peek_raw_info.peek = 1;
|
||||||
|
packet_info_t &peek_info = peek_raw_info.recv_info;
|
||||||
|
mylog(log_trace, "got a packet\n");
|
||||||
|
if (pre_recv_raw_packet() < 0) return -1;
|
||||||
|
if (peek_raw(peek_raw_info) < 0) {
|
||||||
|
discard_raw_packet();
|
||||||
|
// recv(raw_recv_fd, 0,0, 0 );//
|
||||||
|
// struct sockaddr saddr;
|
||||||
|
// socklen_t saddr_size=sizeof(saddr);
|
||||||
|
/// recvfrom(raw_recv_fd, 0,0, 0 ,&saddr , &saddr_size);//
|
||||||
|
mylog(log_trace, "peek_raw failed\n");
|
||||||
|
return -1;
|
||||||
|
} else {
|
||||||
|
mylog(log_trace, "peek_raw success\n");
|
||||||
|
}
|
||||||
|
// u32_t ip=peek_info.src_ip;uint16_t port=peek_info.src_port;
|
||||||
|
|
||||||
|
int data_len;
|
||||||
|
char *data;
|
||||||
|
|
||||||
|
address_t addr;
|
||||||
|
addr.from_ip_port_new(raw_ip_version, &peek_info.new_src_ip, peek_info.src_port);
|
||||||
|
|
||||||
|
char ip_port[max_addr_len];
|
||||||
|
addr.to_str(ip_port);
|
||||||
|
// sprintf(ip_port,"%s:%d",my_ntoa(ip),port);
|
||||||
|
mylog(log_trace, "[%s]peek_raw\n", ip_port);
|
||||||
|
|
||||||
|
if (raw_mode == mode_faketcp && peek_info.syn == 1) {
|
||||||
|
if (!conn_manager.exist(addr) || conn_manager.find_insert(addr).state.server_current_state != server_ready) { // reply any syn ,before state become ready
|
||||||
|
|
||||||
|
raw_info_t tmp_raw_info;
|
||||||
|
if (recv_raw0(tmp_raw_info, data, data_len) < 0) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if (data_len >= max_data_len + 1) {
|
||||||
|
mylog(log_debug, "data_len=%d >= max_data_len+1,ignored", data_len);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (use_tcp_dummy_socket != 0)
|
||||||
|
return 0;
|
||||||
|
raw_info_t &raw_info = tmp_raw_info;
|
||||||
|
packet_info_t &send_info = raw_info.send_info;
|
||||||
|
packet_info_t &recv_info = raw_info.recv_info;
|
||||||
|
|
||||||
|
send_info.new_src_ip = recv_info.new_dst_ip;
|
||||||
|
send_info.src_port = recv_info.dst_port;
|
||||||
|
|
||||||
|
send_info.dst_port = recv_info.src_port;
|
||||||
|
send_info.new_dst_ip = recv_info.new_src_ip;
|
||||||
|
|
||||||
|
if (lower_level) {
|
||||||
|
handle_lower_level(raw_info);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (data_len == 0 && raw_info.recv_info.syn == 1 && raw_info.recv_info.ack == 0) {
|
||||||
|
send_info.ack_seq = recv_info.seq + 1;
|
||||||
|
|
||||||
|
send_info.psh = 0;
|
||||||
|
send_info.syn = 1;
|
||||||
|
send_info.ack = 1;
|
||||||
|
send_info.ts_ack = recv_info.ts;
|
||||||
|
|
||||||
|
mylog(log_info, "[%s]received syn,sent syn ack back\n", ip_port);
|
||||||
|
send_raw0(raw_info, 0, 0);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
discard_raw_packet();
|
||||||
|
// recv(raw_recv_fd, 0,0,0);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if (!conn_manager.exist(addr)) {
|
||||||
|
if (conn_manager.mp.size() >= max_handshake_conn_num) {
|
||||||
|
mylog(log_info, "[%s]reached max_handshake_conn_num,ignored new handshake\n", ip_port);
|
||||||
|
discard_raw_packet();
|
||||||
|
// recv(raw_recv_fd, 0,0, 0 );//
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
raw_info_t tmp_raw_info;
|
||||||
|
|
||||||
|
if (raw_mode == mode_icmp) {
|
||||||
|
tmp_raw_info.send_info.dst_port = tmp_raw_info.send_info.src_port = addr.get_port();
|
||||||
|
}
|
||||||
|
if (recv_bare(tmp_raw_info, data, data_len) < 0) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if (data_len < int(3 * sizeof(my_id_t))) {
|
||||||
|
mylog(log_debug, "[%s]too short to be a handshake\n", ip_port);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// id_t zero=ntohl(* ((u32_t *)&data[sizeof(id_t)]));
|
||||||
|
my_id_t zero;
|
||||||
|
memcpy(&zero, &data[sizeof(my_id_t)], sizeof(zero));
|
||||||
|
zero = ntohl(zero);
|
||||||
|
|
||||||
|
if (zero != 0) {
|
||||||
|
mylog(log_debug, "[%s]not a invalid initial handshake\n", ip_port);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
mylog(log_info, "[%s]got packet from a new ip\n", ip_port);
|
||||||
|
|
||||||
|
conn_info_t &conn_info = conn_manager.find_insert(addr);
|
||||||
|
conn_info.raw_info = tmp_raw_info;
|
||||||
|
raw_info_t &raw_info = conn_info.raw_info;
|
||||||
|
|
||||||
|
packet_info_t &send_info = conn_info.raw_info.send_info;
|
||||||
|
packet_info_t &recv_info = conn_info.raw_info.recv_info;
|
||||||
|
|
||||||
|
// conn_info.ip_port.ip=ip;
|
||||||
|
// conn_info.ip_port.port=port;
|
||||||
|
|
||||||
|
send_info.new_src_ip = recv_info.new_dst_ip;
|
||||||
|
send_info.src_port = recv_info.dst_port;
|
||||||
|
|
||||||
|
send_info.dst_port = recv_info.src_port;
|
||||||
|
send_info.new_dst_ip = recv_info.new_src_ip;
|
||||||
|
|
||||||
|
if (lower_level) {
|
||||||
|
handle_lower_level(raw_info);
|
||||||
|
}
|
||||||
|
|
||||||
|
// id_t tmp_oppsite_id= ntohl(* ((u32_t *)&data[0]));
|
||||||
|
// mylog(log_info,"[%s]handshake1 received %x\n",ip_port,tmp_oppsite_id);
|
||||||
|
|
||||||
|
conn_info.my_id = get_true_random_number_nz();
|
||||||
|
|
||||||
|
mylog(log_info, "[%s]created new conn,state: server_handshake1,my_id is %x\n", ip_port, conn_info.my_id);
|
||||||
|
|
||||||
|
conn_info.state.server_current_state = server_handshake1;
|
||||||
|
conn_info.last_state_time = get_current_time();
|
||||||
|
|
||||||
|
server_on_raw_recv_handshake1(conn_info, ip_port, data, data_len);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
conn_info_t &conn_info = conn_manager.find_insert(addr); // insert if not exist
|
||||||
|
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;
|
||||||
|
|
||||||
|
if (conn_info.state.server_current_state == server_handshake1) {
|
||||||
|
if (recv_bare(raw_info, data, data_len) != 0) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
return server_on_raw_recv_handshake1(conn_info, ip_port, data, data_len);
|
||||||
|
}
|
||||||
|
if (conn_info.state.server_current_state == server_ready) {
|
||||||
|
vector<char> type_vec;
|
||||||
|
vector<string> data_vec;
|
||||||
|
recv_safer_multi(conn_info, type_vec, data_vec);
|
||||||
|
if (data_vec.empty()) {
|
||||||
|
mylog(log_debug, "recv_safer failed!\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < (int)type_vec.size(); i++) {
|
||||||
|
char type = type_vec[i];
|
||||||
|
char *data = (char *)data_vec[i].c_str(); // be careful, do not append data to it
|
||||||
|
int data_len = data_vec[i].length();
|
||||||
|
server_on_raw_recv_ready(conn_info, ip_port, type, data, data_len);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (conn_info.state.server_current_state == server_idle) {
|
||||||
|
discard_raw_packet();
|
||||||
|
// recv(raw_recv_fd, 0,0, 0 );//
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
mylog(log_fatal, "we should never run to here\n");
|
||||||
|
myexit(-1);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int server_on_udp_recv(conn_info_t &conn_info, fd64_t fd64) {
|
||||||
|
char buf[buf_len];
|
||||||
|
|
||||||
|
if (conn_info.state.server_current_state != server_ready) // TODO remove this for peformance
|
||||||
|
{
|
||||||
|
mylog(log_fatal, "p_conn_info->state.server_current_state!=server_ready!!!this shouldnt happen\n");
|
||||||
|
myexit(-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// conn_info_t &conn_info=*p_conn_info;
|
||||||
|
|
||||||
|
assert(conn_info.blob->conv_manager.s.is_data_used(fd64));
|
||||||
|
|
||||||
|
u32_t conv_id = conn_info.blob->conv_manager.s.find_conv_by_data(fd64);
|
||||||
|
|
||||||
|
int fd = fd_manager.to_fd(fd64);
|
||||||
|
|
||||||
|
int recv_len = recv(fd, buf, max_data_len + 1, 0);
|
||||||
|
|
||||||
|
mylog(log_trace, "received a packet from udp_fd,len:%d\n", recv_len);
|
||||||
|
|
||||||
|
if (recv_len == max_data_len + 1) {
|
||||||
|
mylog(log_warn, "huge packet, data_len > %d,dropped\n", max_data_len);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (recv_len < 0) {
|
||||||
|
mylog(log_debug, "udp fd,recv_len<0 continue,%s\n", strerror(errno));
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (recv_len >= mtu_warn) {
|
||||||
|
mylog(log_warn, "huge packet,data len=%d (>=%d).strongly suggested to set a smaller mtu at upper level,to get rid of this warn\n ", recv_len, mtu_warn);
|
||||||
|
}
|
||||||
|
|
||||||
|
// conn_info.conv_manager->update_active_time(conv_id); server dosnt update from upd side,only update from raw side. (client updates at both side)
|
||||||
|
|
||||||
|
if (conn_info.state.server_current_state == server_ready) {
|
||||||
|
send_data_safer(conn_info, buf, recv_len, conv_id);
|
||||||
|
// send_data(g_packet_info_send,buf,recv_len,my_id,oppsite_id,conv_id);
|
||||||
|
mylog(log_trace, "send_data_safer ,sent !!\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int server_event_loop() {
|
||||||
|
char buf[buf_len];
|
||||||
|
|
||||||
|
int i, j, k;
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
if (raw_ip_version == AF_INET) {
|
||||||
|
if (local_addr.inner.ipv4.sin_addr.s_addr != 0) {
|
||||||
|
bind_addr_used = 1;
|
||||||
|
bind_addr.v4 = local_addr.inner.ipv4.sin_addr.s_addr;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
assert(raw_ip_version == AF_INET6);
|
||||||
|
char zero_arr[16] = {0};
|
||||||
|
if (memcmp(&local_addr.inner.ipv6.sin6_addr, zero_arr, 16) != 0) {
|
||||||
|
bind_addr_used = 1;
|
||||||
|
bind_addr.v6 = local_addr.inner.ipv6.sin6_addr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// bind_address_uint32=local_ip_uint32;//only server has bind adress,client sets it to zero
|
||||||
|
|
||||||
|
if (lower_level) {
|
||||||
|
if (lower_level_manual) {
|
||||||
|
init_ifindex(if_name, raw_send_fd, ifindex);
|
||||||
|
mylog(log_info, "we are running at lower-level (manual) mode\n");
|
||||||
|
} else {
|
||||||
|
mylog(log_info, "we are running at lower-level (auto) mode\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (raw_mode == mode_faketcp) {
|
||||||
|
bind_fd = socket(local_addr.get_type(), SOCK_STREAM, 0);
|
||||||
|
} else if (raw_mode == mode_udp || raw_mode == mode_icmp) // bind an adress to avoid collision,for icmp,there is no port,just bind a udp port
|
||||||
|
{
|
||||||
|
bind_fd = socket(local_addr.get_type(), SOCK_DGRAM, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// struct sockaddr_in temp_bind_addr={0};
|
||||||
|
// bzero(&temp_bind_addr, sizeof(temp_bind_addr));
|
||||||
|
|
||||||
|
// temp_bind_addr.sin_family = AF_INET;
|
||||||
|
// temp_bind_addr.sin_port = local_addr.get_port();
|
||||||
|
// temp_bind_addr.sin_addr.s_addr = local_addr.inner.ipv4.sin_addr.s_addr;
|
||||||
|
|
||||||
|
if (bind(bind_fd, (struct sockaddr *)&local_addr.inner, local_addr.get_len()) != 0) {
|
||||||
|
mylog(log_fatal, "bind fail\n");
|
||||||
|
myexit(-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (raw_mode == mode_faketcp) {
|
||||||
|
if (listen(bind_fd, SOMAXCONN) != 0) {
|
||||||
|
mylog(log_fatal, "listen fail\n");
|
||||||
|
myexit(-1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// init_raw_socket();
|
||||||
|
init_filter(local_addr.get_port()); // bpf filter
|
||||||
|
|
||||||
|
epollfd = epoll_create1(0);
|
||||||
|
const int max_events = 4096;
|
||||||
|
|
||||||
|
struct epoll_event ev, events[max_events];
|
||||||
|
if (epollfd < 0) {
|
||||||
|
mylog(log_fatal, "epoll return %d\n", epollfd);
|
||||||
|
myexit(-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
ev.events = EPOLLIN;
|
||||||
|
ev.data.u64 = raw_recv_fd;
|
||||||
|
|
||||||
|
ret = epoll_ctl(epollfd, EPOLL_CTL_ADD, raw_recv_fd, &ev);
|
||||||
|
if (ret != 0) {
|
||||||
|
mylog(log_fatal, "add raw_fd error\n");
|
||||||
|
myexit(-1);
|
||||||
|
}
|
||||||
|
int timer_fd;
|
||||||
|
|
||||||
|
set_timer(epollfd, timer_fd);
|
||||||
|
|
||||||
|
u64_t begin_time = 0;
|
||||||
|
u64_t end_time = 0;
|
||||||
|
|
||||||
|
mylog(log_info, "now listening at %s\n", local_addr.get_str());
|
||||||
|
|
||||||
|
int fifo_fd = -1;
|
||||||
|
|
||||||
|
if (fifo_file[0] != 0) {
|
||||||
|
fifo_fd = create_fifo(fifo_file);
|
||||||
|
ev.events = EPOLLIN;
|
||||||
|
ev.data.u64 = fifo_fd;
|
||||||
|
|
||||||
|
ret = epoll_ctl(epollfd, EPOLL_CTL_ADD, fifo_fd, &ev);
|
||||||
|
if (ret != 0) {
|
||||||
|
mylog(log_fatal, "add fifo_fd to epoll error %s\n", strerror(errno));
|
||||||
|
myexit(-1);
|
||||||
|
}
|
||||||
|
mylog(log_info, "fifo_file=%s\n", fifo_file);
|
||||||
|
}
|
||||||
|
|
||||||
|
while (1) ////////////////////////
|
||||||
|
{
|
||||||
|
if (about_to_exit) myexit(0);
|
||||||
|
|
||||||
|
int nfds = epoll_wait(epollfd, events, max_events, 180 * 1000);
|
||||||
|
if (nfds < 0) { // allow zero
|
||||||
|
if (errno == EINTR) {
|
||||||
|
mylog(log_info, "epoll interrupted by signal,continue\n");
|
||||||
|
// myexit(0);
|
||||||
|
} else {
|
||||||
|
mylog(log_fatal, "epoll_wait return %d,%s\n", nfds, strerror(errno));
|
||||||
|
myexit(-1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
int idx;
|
||||||
|
for (idx = 0; idx < nfds; ++idx) {
|
||||||
|
// mylog(log_debug,"ndfs: %d \n",nfds);
|
||||||
|
epoll_trigger_counter++;
|
||||||
|
// printf("%d %d %d %d\n",timer_fd,raw_recv_fd,raw_send_fd,n);
|
||||||
|
if ((events[idx].data.u64) == (u64_t)timer_fd) {
|
||||||
|
if (debug_flag) begin_time = get_current_time();
|
||||||
|
conn_manager.clear_inactive();
|
||||||
|
u64_t dummy;
|
||||||
|
int unused = read(timer_fd, &dummy, 8);
|
||||||
|
// current_time_rough=get_current_time();
|
||||||
|
if (debug_flag) {
|
||||||
|
end_time = get_current_time();
|
||||||
|
mylog(log_debug, "timer_fd,%llu,%llu,%llu\n", begin_time, end_time, end_time - begin_time);
|
||||||
|
}
|
||||||
|
|
||||||
|
mylog(log_trace, "epoll_trigger_counter: %d \n", epoll_trigger_counter);
|
||||||
|
epoll_trigger_counter = 0;
|
||||||
|
|
||||||
|
} else if (events[idx].data.u64 == (u64_t)raw_recv_fd) {
|
||||||
|
if (debug_flag) begin_time = get_current_time();
|
||||||
|
server_on_raw_recv_multi();
|
||||||
|
if (debug_flag) {
|
||||||
|
end_time = get_current_time();
|
||||||
|
mylog(log_debug, "raw_recv_fd,%llu,%llu,%llu \n", begin_time, end_time, end_time - begin_time);
|
||||||
|
}
|
||||||
|
} else if (events[idx].data.u64 == (u64_t)fifo_fd) {
|
||||||
|
int len = read(fifo_fd, buf, sizeof(buf));
|
||||||
|
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 > u32_t(-1)) {
|
||||||
|
fd64_t fd64 = events[idx].data.u64;
|
||||||
|
if (!fd_manager.exist(fd64)) {
|
||||||
|
mylog(log_trace, "fd64 no longer exist\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
assert(fd_manager.exist_info(fd64));
|
||||||
|
conn_info_t *p_conn_info = fd_manager.get_info(fd64).p_conn_info;
|
||||||
|
conn_info_t &conn_info = *p_conn_info;
|
||||||
|
if (fd64 == conn_info.timer_fd64) //////////timer_fd64
|
||||||
|
{
|
||||||
|
if (debug_flag) begin_time = get_current_time();
|
||||||
|
int fd = fd_manager.to_fd(fd64);
|
||||||
|
u64_t dummy;
|
||||||
|
int unused = read(fd, &dummy, 8);
|
||||||
|
assert(conn_info.state.server_current_state == server_ready); // TODO remove this for peformance
|
||||||
|
server_on_timer_multi(conn_info);
|
||||||
|
if (debug_flag) {
|
||||||
|
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 // udp_fd64
|
||||||
|
{
|
||||||
|
if (debug_flag) begin_time = get_current_time();
|
||||||
|
server_on_udp_recv(conn_info, fd64);
|
||||||
|
if (debug_flag) {
|
||||||
|
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 {
|
||||||
|
mylog(log_fatal, "unknown fd,this should never happen\n");
|
||||||
|
myexit(-1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|