Skip to content

Commit

Permalink
优化包解析
Browse files Browse the repository at this point in the history
  • Loading branch information
vnt-dev committed Jul 26, 2023
1 parent 31a6361 commit 4a603f4
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 38 deletions.
25 changes: 0 additions & 25 deletions packet/src/error.rs

This file was deleted.

10 changes: 5 additions & 5 deletions packet/src/ip/ipv4/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ impl<B: AsRef<[u8]>> IpV4Packet<B> {
Self { buffer }
}
pub fn new(buffer: B) -> io::Result<Self> {
if buffer.as_ref()[0] >> 4 != 4 {
Err(io::Error::from(io::ErrorKind::InvalidData))?;
}
if buffer.as_ref().len() < 20 {
Err(io::Error::from(io::ErrorKind::InvalidData))?;
Err(io::Error::new(io::ErrorKind::InvalidData, "len < 20"))?;
}
if buffer.as_ref()[0] >> 4 != 4 {
Err(io::Error::new(io::ErrorKind::InvalidData, "not ipv4"))?;
}
let packet = Self::unchecked(buffer);
if packet.buffer.as_ref().len() < packet.header_len() as usize * 4 {
Err(io::Error::from(io::ErrorKind::InvalidData))?;
Err(io::Error::new(io::ErrorKind::InvalidData, "head_len err"))?;
}
Ok(packet)
}
Expand Down
2 changes: 1 addition & 1 deletion packet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ pub fn ipv4_cal_checksum(
src_ip: &Ipv4Addr,
dest_ip: &Ipv4Addr,
protocol: u8,
length: u16,
) -> u16 {
use std::io::Cursor;
let length = buffer.len();
let mut sum = 0;
let src_ip = src_ip.octets();
sum += u32c(src_ip[0], src_ip[1]);
Expand Down
1 change: 0 additions & 1 deletion packet/src/tcp/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ impl<B: AsRef<[u8]>> TcpPacket<B> {
&self.source_ip,
&self.destination_ip,
6,
self.buffer.as_ref().len() as u16,
)
}
pub fn urgent_pointer(&self) -> u16 {
Expand Down
11 changes: 5 additions & 6 deletions packet/src/udp/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,15 @@ impl<B: AsRef<[u8]>> UdpPacket<B> {
&self.source_ip,
&self.destination_ip,
17,
self.length(),
)
}
}

impl<B: AsRef<[u8]> + AsMut<[u8]>> UdpPacket<B> {
fn header_mut(&mut self) -> &mut [u8] {
&mut self.buffer.as_mut()[..8]
}
}
// impl<B: AsRef<[u8]> + AsMut<[u8]>> UdpPacket<B> {
// fn header_mut(&mut self) -> &mut [u8] {
// &mut self.buffer.as_mut()[..8]
// }
// }

impl<B: AsRef<[u8]> + AsMut<[u8]>> UdpPacket<B> {
/// 设置源端口
Expand Down

0 comments on commit 4a603f4

Please sign in to comment.