-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
Merge pull request #17 from narrowlink/UnknownPackets
Implement UnknownTransport and UnknownNetwork
Showing
7 changed files
with
228 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,46 @@ | ||
use std::net::SocketAddr; | ||
use std::net::{SocketAddr, SocketAddrV4, SocketAddrV6}; | ||
|
||
pub use self::tcp::IpStackTcpStream; | ||
pub use self::udp::IpStackUdpStream; | ||
pub use self::unknown::IpStackUnknownTransport; | ||
|
||
mod tcb; | ||
mod tcp; | ||
mod udp; | ||
mod unknown; | ||
|
||
pub enum IpStackStream { | ||
Tcp(IpStackTcpStream), | ||
Udp(IpStackUdpStream), | ||
UnknownTransport(IpStackUnknownTransport), | ||
UnknownNetwork(Vec<u8>), | ||
} | ||
|
||
impl IpStackStream { | ||
pub fn local_addr(&self) -> SocketAddr { | ||
match self { | ||
IpStackStream::Tcp(tcp) => tcp.local_addr(), | ||
IpStackStream::Udp(udp) => udp.local_addr(), | ||
IpStackStream::UnknownNetwork(_) => { | ||
SocketAddr::V4(SocketAddrV4::new(std::net::Ipv4Addr::new(0, 0, 0, 0), 0)) | ||
} | ||
IpStackStream::UnknownTransport(unknown) => match unknown.src_addr() { | ||
std::net::IpAddr::V4(addr) => SocketAddr::V4(SocketAddrV4::new(addr, 0)), | ||
std::net::IpAddr::V6(addr) => SocketAddr::V6(SocketAddrV6::new(addr, 0, 0, 0)), | ||
}, | ||
} | ||
} | ||
pub fn peer_addr(&self) -> SocketAddr { | ||
match self { | ||
IpStackStream::Tcp(tcp) => tcp.peer_addr(), | ||
IpStackStream::Udp(udp) => udp.peer_addr(), | ||
IpStackStream::UnknownNetwork(_) => { | ||
SocketAddr::V4(SocketAddrV4::new(std::net::Ipv4Addr::new(0, 0, 0, 0), 0)) | ||
} | ||
IpStackStream::UnknownTransport(unknown) => match unknown.dst_addr() { | ||
std::net::IpAddr::V4(addr) => SocketAddr::V4(SocketAddrV4::new(addr, 0)), | ||
std::net::IpAddr::V6(addr) => SocketAddr::V6(SocketAddrV6::new(addr, 0, 0, 0)), | ||
}, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
use std::{io::Error, mem, net::IpAddr}; | ||
|
||
use etherparse::{IpHeader, Ipv4Extensions, Ipv4Header, Ipv6Extensions, Ipv6Header}; | ||
use tokio::sync::mpsc::UnboundedSender; | ||
|
||
use crate::{ | ||
packet::{NetworkPacket, TransportHeader}, | ||
TTL, | ||
}; | ||
|
||
pub struct IpStackUnknownTransport { | ||
src_addr: IpAddr, | ||
dst_addr: IpAddr, | ||
payload: Vec<u8>, | ||
protocol: u8, | ||
mtu: u16, | ||
packet_sender: UnboundedSender<NetworkPacket>, | ||
} | ||
|
||
impl IpStackUnknownTransport { | ||
pub fn new( | ||
src_addr: IpAddr, | ||
dst_addr: IpAddr, | ||
payload: Vec<u8>, | ||
ip: &IpHeader, | ||
mtu: u16, | ||
packet_sender: UnboundedSender<NetworkPacket>, | ||
) -> Self { | ||
let protocol = match ip { | ||
IpHeader::Version4(ip, _) => ip.protocol, | ||
IpHeader::Version6(ip, _) => ip.next_header, | ||
}; | ||
IpStackUnknownTransport { | ||
src_addr, | ||
dst_addr, | ||
payload, | ||
protocol, | ||
mtu, | ||
packet_sender, | ||
} | ||
} | ||
pub fn src_addr(&self) -> IpAddr { | ||
self.src_addr | ||
} | ||
pub fn dst_addr(&self) -> IpAddr { | ||
self.dst_addr | ||
} | ||
pub fn payload(&self) -> &[u8] { | ||
&self.payload | ||
} | ||
pub fn ip_protocol(&self) -> u8 { | ||
self.protocol | ||
} | ||
pub async fn send(&self, mut payload: Vec<u8>) -> Result<(), Error> { | ||
loop { | ||
let packet = self.create_rev_packet(&mut payload)?; | ||
self.packet_sender | ||
.send(packet) | ||
.map_err(|_| Error::new(std::io::ErrorKind::Other, "send error"))?; | ||
if payload.is_empty() { | ||
return Ok(()); | ||
} | ||
} | ||
} | ||
|
||
pub fn create_rev_packet(&self, payload: &mut Vec<u8>) -> Result<NetworkPacket, Error> { | ||
match (self.dst_addr, self.src_addr) { | ||
(std::net::IpAddr::V4(dst), std::net::IpAddr::V4(src)) => { | ||
let mut ip_h = Ipv4Header::new(0, TTL, self.protocol, dst.octets(), src.octets()); | ||
let line_buffer = self.mtu.saturating_sub(ip_h.header_len() as u16); | ||
|
||
let p = if payload.len() > line_buffer as usize { | ||
payload.drain(0..line_buffer as usize).collect::<Vec<u8>>() | ||
} else { | ||
mem::take(payload) | ||
}; | ||
ip_h.payload_len = p.len() as u16; | ||
Ok(NetworkPacket { | ||
ip: etherparse::IpHeader::Version4(ip_h, Ipv4Extensions::default()), | ||
transport: TransportHeader::Unknown, | ||
payload: p, | ||
}) | ||
} | ||
(std::net::IpAddr::V6(dst), std::net::IpAddr::V6(src)) => { | ||
let mut ip_h = Ipv6Header { | ||
traffic_class: 0, | ||
flow_label: 0, | ||
payload_length: 0, | ||
next_header: 17, | ||
hop_limit: TTL, | ||
source: dst.octets(), | ||
destination: src.octets(), | ||
}; | ||
let line_buffer = self.mtu.saturating_sub(ip_h.header_len() as u16); | ||
payload.truncate(line_buffer as usize); | ||
ip_h.payload_length = payload.len() as u16; | ||
let p = if payload.len() > line_buffer as usize { | ||
payload.drain(0..line_buffer as usize).collect::<Vec<u8>>() | ||
} else { | ||
mem::take(payload) | ||
}; | ||
Ok(NetworkPacket { | ||
ip: etherparse::IpHeader::Version6(ip_h, Ipv6Extensions::default()), | ||
transport: TransportHeader::Unknown, | ||
payload: p, | ||
}) | ||
} | ||
_ => unreachable!(), | ||
} | ||
} | ||
} |