Skip to content

Commit 7aa57c0

Browse files
test5
1 parent 703e0a3 commit 7aa57c0

20 files changed

+3467
-12
lines changed

tun_test4/Cargo.toml

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ edition = "2018"
77
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
88

99
[dependencies]
10-
#tokio-tun = "0.3.5"
11-
tokio-tun = { git = "https://github.com/hankbao/tokio-tun.git" }
10+
tokio-tun = "0.3.5"
11+
#tokio-tun = { git = "https://github.com/hankbao/tokio-tun.git" }
1212
tokio = { version = "1.1", features = ["net"] }
13-
mio = "0.6"
13+
#mio = "0.6"
1414

1515
libc = "0.2"
1616
nix = "0.19"

tun_test4/MEMO.txt

+3
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
sudo ./target/debug/tun_rust
2+
3+
sudo tshark -i tun0
4+

tun_test4/src/main.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use std::net::Ipv4Addr;
44
use std::os::unix::io::AsRawFd;
55
use tokio::io::AsyncReadExt;
6+
use tokio::io::AsyncWriteExt;
67
use tokio_tun::result::Result;
78
use tokio_tun::TunBuilder;
89

@@ -95,12 +96,10 @@ async fn main() -> Result<()> {
9596
let mut buf = [0u8; 1024];
9697
loop {
9798
let n = reader.read(&mut buf).await?;
98-
//println!("reading {} bytes: {:?}", n, &buf[..n]);
99-
//let mut fake_ethernet_frame = MutableEthernetPacket::new(&mut buf[..n]).unwrap();
100-
101-
//let fake_ethernet_frame = EthernetPacket::new(&mut buf[..n]).unwrap();
102-
//capture_packet(&fake_ethernet_frame);
103-
//
99+
100+
//_writer.write_all(&buf).await?;
101+
_writer.write(&buf).await?;
102+
104103
let header = Ipv4Packet::new(&mut buf[..n]).unwrap();
105104
println!("{} -> {}",header.get_source(), header.get_destination());
106105
let ihl = usize::from(header.get_header_length());
@@ -117,9 +116,12 @@ async fn main() -> Result<()> {
117116
header. get_next_level_protocol(),
118117
//&buf[20..n],
119118
&buf[hlen..n],
119+
&buf,
120120
&mut _writer,
121+
//tun,
121122
);
122123

124+
123125
//if let Some(header) = header {
124126
// // println!("{?}",header.get_source());
125127
// //header.get_destination()

tun_test4/src/packet_handler.rs

+33-3
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ use pnet::util::MacAddr;
1616

1717
use tokio_tun::Tun;
1818
use tokio::io::WriteHalf;
19+
use tokio::io::AsyncReadExt;
1920
use tokio::io::AsyncWriteExt;
2021
//use tokio::io::Write;
22+
use tokio_tun::TunBuilder;
2123

2224
//use std::env;
2325
//use std::io::{self, Write};
@@ -52,6 +54,7 @@ fn send_ipv4_packet(
5254
//tx: &TxSender,
5355
writer: &mut tokio::io::WriteHalf<Tun>,
5456
) {
57+
println!("send");
5558
let buf_size = MutableIpv4Packet::minimum_packet_size() + payload.len();
5659
let mut ip_packet = MutableIpv4Packet::owned(vec![0u8; buf_size]).unwrap();
5760

@@ -75,7 +78,7 @@ fn send_ipv4_packet(
7578
//writer.write(ip_packet.packet());
7679
//let r = writer.write(ip_packet.packet());
7780
//writer.flush();
78-
writer.write(ip_packet.packet());
81+
writer.write_all(ip_packet.packet());
7982
writer.flush();
8083
//println!("send:{:?}", ret);
8184

@@ -105,7 +108,13 @@ fn send_ipv4_packet(
105108
//};
106109
}
107110

108-
fn handle_icmp_packet(interface_name: &str, source: IpAddr, destination: IpAddr, packet: &[u8], writer: &mut tokio::io::WriteHalf<Tun>) {
111+
fn handle_icmp_packet(interface_name: &str, source: IpAddr, destination: IpAddr,
112+
packet: &[u8],
113+
recv_packet: &[u8],
114+
writer: &mut tokio::io::WriteHalf<Tun>) {
115+
116+
writer.write_all(&recv_packet);
117+
109118
let icmp_packet = IcmpPacket::new(packet);
110119
if let Some(icmp_packet) = icmp_packet {
111120
match icmp_packet.get_icmp_type() {
@@ -121,6 +130,8 @@ fn handle_icmp_packet(interface_name: &str, source: IpAddr, destination: IpAddr,
121130
);
122131
}
123132
IcmpTypes::EchoRequest => {
133+
println!("recv");
134+
writer.write_all(&recv_packet);
124135
let echo_request_packet = echo_request::EchoRequestPacket::new(packet).unwrap();
125136
println!(
126137
"[{}]: ICMP echo request {} -> {} (seq={:?}, id={:?})",
@@ -156,6 +167,7 @@ fn handle_icmp_packet(interface_name: &str, source: IpAddr, destination: IpAddr,
156167
//////////////////////////////////////////
157168
*/
158169
let mut echo_reply_packet =
170+
159171
echo_reply::MutableEchoReplyPacket::owned(packet.to_vec()).unwrap();
160172
echo_reply_packet.set_icmp_type(IcmpTypes::EchoReply);
161173
let icmp_packet = IcmpPacket::new(echo_reply_packet.packet()).unwrap();
@@ -229,6 +241,7 @@ pub fn handle_transport_protocol(
229241
destination: IpAddr,
230242
protocol: IpNextHeaderProtocol,
231243
packet: &[u8],
244+
recv_packet: &[u8],
232245
writer: &mut tokio::io::WriteHalf<Tun>,
233246
) {
234247
match protocol {
@@ -239,7 +252,7 @@ pub fn handle_transport_protocol(
239252
handle_tcp_packet(interface_name, source, destination, packet, writer)
240253
}
241254
IpNextHeaderProtocols::Icmp => {
242-
handle_icmp_packet(interface_name, source, destination, packet, writer)
255+
handle_icmp_packet(interface_name, source, destination, packet, recv_packet, writer)
243256
}
244257
IpNextHeaderProtocols::Icmpv6 => {
245258
handle_icmpv6_packet(interface_name, source, destination, packet, writer)
@@ -259,6 +272,21 @@ pub fn handle_transport_protocol(
259272
}
260273
}
261274

275+
pub async fn handle_transport_protocol2(
276+
interface_name: &str,
277+
source: IpAddr,
278+
destination: IpAddr,
279+
protocol: IpNextHeaderProtocol,
280+
packet: &[u8],
281+
recv_packet: &[u8],
282+
//writer: &mut tokio::io::WriteHalf<Tun>,
283+
tun: Tun,
284+
) {
285+
let (mut reader, mut _writer) = tokio::io::split(tun);
286+
287+
//_writer.write(&recv_packet);
288+
}
289+
262290
pub fn handle_ipv4_packet(interface_name: &str, ethernet: &EthernetPacket, writer: &mut tokio::io::WriteHalf<Tun>) {
263291
let header = Ipv4Packet::new(ethernet.payload());
264292
if let Some(header) = header {
@@ -268,6 +296,7 @@ pub fn handle_ipv4_packet(interface_name: &str, ethernet: &EthernetPacket, write
268296
IpAddr::V4(header.get_destination()),
269297
header.get_next_level_protocol(),
270298
header.payload(),
299+
header.payload(),
271300
writer,
272301
);
273302
} else {
@@ -284,6 +313,7 @@ pub fn handle_ipv6_packet(interface_name: &str, ethernet: &EthernetPacket, write
284313
IpAddr::V6(header.get_destination()),
285314
header.get_next_header(),
286315
header.payload(),
316+
header.payload(),
287317
writer,
288318
);
289319
} else {

0 commit comments

Comments
 (0)