diff options
Diffstat (limited to 'src/protocol/tcp.rs')
| -rw-r--r-- | src/protocol/tcp.rs | 98 |
1 files changed, 49 insertions, 49 deletions
diff --git a/src/protocol/tcp.rs b/src/protocol/tcp.rs index 5a88b25..5ab7dde 100644 --- a/src/protocol/tcp.rs +++ b/src/protocol/tcp.rs @@ -45,7 +45,7 @@ const TCP_OPTION_SACK: u8 = 5; // Selective acknowledgment const TCP_OPTION_TIMESTAMPS: u8 = 8; // Timestamps #[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub enum TcpOption { +pub enum TCPOption { EOL, NOP, MSS { @@ -70,7 +70,7 @@ pub enum TcpOption { } #[derive(Clone, Debug, PartialEq, Eq)] -pub struct TcpHeader { +pub struct TCPHeader { pub source_port: u16, pub dest_port: u16, @@ -90,7 +90,7 @@ pub struct TcpHeader { pub window: u16, pub checksum: u16, pub urgent_ptr: u16, - pub options: Option<Vec<TcpOption>>, + pub options: Option<Vec<TCPOption>>, } /****************************************************************************** @@ -105,37 +105,37 @@ fn offset_res_flags_decode(input: &[u8]) -> IResult<&[u8], (u8, u8, u8)> { )))(input) } -impl TcpOption { - fn mss_decode(input: &[u8]) -> IResult<&[u8], TcpOption> { +impl TCPOption { + fn mss_decode(input: &[u8]) -> IResult<&[u8], TCPOption> { let (input, length) = number::streaming::be_u8(input)?; let (input, mss) = number::streaming::be_u16(input)?; - Ok((input, TcpOption::MSS { length, mss })) + Ok((input, TCPOption::MSS { length, mss })) } - fn wscale_decode(input: &[u8]) -> IResult<&[u8], TcpOption> { + fn wscale_decode(input: &[u8]) -> IResult<&[u8], TCPOption> { let (input, length) = number::streaming::be_u8(input)?; let (input, shift_count) = number::streaming::be_u8(input)?; Ok(( input, - TcpOption::WSCALE { + TCPOption::WSCALE { length, shift_count, }, )) } - fn sack_permitted_decode(input: &[u8]) -> IResult<&[u8], TcpOption> { + fn sack_permitted_decode(input: &[u8]) -> IResult<&[u8], TCPOption> { let (input, _length) = number::streaming::be_u8(input)?; - Ok((input, TcpOption::SACKPERMITTED)) + Ok((input, TCPOption::SACKPERMITTED)) } - fn sack_decode(input: &[u8]) -> IResult<&[u8], TcpOption> { + fn sack_decode(input: &[u8]) -> IResult<&[u8], TCPOption> { let (input, length) = number::streaming::be_u8(input)?; let (input, left_edge) = number::streaming::be_u32(input)?; let (input, right_edge) = number::streaming::be_u32(input)?; Ok(( input, - TcpOption::SACK { + TCPOption::SACK { length, left_edge, right_edge, @@ -143,13 +143,13 @@ impl TcpOption { )) } - fn timestamp_decode(input: &[u8]) -> IResult<&[u8], TcpOption> { + fn timestamp_decode(input: &[u8]) -> IResult<&[u8], TCPOption> { let (input, length) = number::streaming::be_u8(input)?; let (input, ts_value) = number::streaming::be_u32(input)?; let (input, ts_reply) = number::streaming::be_u32(input)?; Ok(( input, - TcpOption::TIMESTAMPS { + TCPOption::TIMESTAMPS { length, ts_value, ts_reply, @@ -157,22 +157,22 @@ impl TcpOption { )) } - fn decode(input: &[u8]) -> IResult<&[u8], TcpOption> { + fn decode(input: &[u8]) -> IResult<&[u8], TCPOption> { match number::streaming::be_u8(input)? { - (input, TCP_OPTION_EOL) => Ok((input, TcpOption::EOL)), - (input, TCP_OPTION_NOP) => Ok((input, TcpOption::NOP)), - (input, TCP_OPTION_MSS) => TcpOption::mss_decode(input), - (input, TCP_OPTION_WSCALE) => TcpOption::wscale_decode(input), - (input, TCP_OPTION_SACK_PERMITTED) => TcpOption::sack_permitted_decode(input), - (input, TCP_OPTION_SACK) => TcpOption::sack_decode(input), - (input, TCP_OPTION_TIMESTAMPS) => TcpOption::timestamp_decode(input), + (input, TCP_OPTION_EOL) => Ok((input, TCPOption::EOL)), + (input, TCP_OPTION_NOP) => Ok((input, TCPOption::NOP)), + (input, TCP_OPTION_MSS) => TCPOption::mss_decode(input), + (input, TCP_OPTION_WSCALE) => TCPOption::wscale_decode(input), + (input, TCP_OPTION_SACK_PERMITTED) => TCPOption::sack_permitted_decode(input), + (input, TCP_OPTION_SACK) => TCPOption::sack_decode(input), + (input, TCP_OPTION_TIMESTAMPS) => TCPOption::timestamp_decode(input), (input, _other) => Err(Err::Failure(Error::new(input, ErrorKind::Switch))), } } } -impl TcpHeader { - fn fixed_header_decode(input: &[u8]) -> IResult<&[u8], TcpHeader> { +impl TCPHeader { + fn fixed_header_decode(input: &[u8]) -> IResult<&[u8], TCPHeader> { let (input, source_port) = number::streaming::be_u16(input)?; let (input, dest_port) = number::streaming::be_u16(input)?; let (input, seq_num) = number::streaming::be_u32(input)?; @@ -184,7 +184,7 @@ impl TcpHeader { Ok(( input, - TcpHeader { + TCPHeader { source_port, dest_port, seq_num, @@ -205,11 +205,11 @@ impl TcpHeader { )) } - fn options_decode(input: &[u8]) -> IResult<&[u8], Vec<TcpOption>> { + fn options_decode(input: &[u8]) -> IResult<&[u8], Vec<TCPOption>> { let mut left = input; - let mut options: Vec<TcpOption> = vec![]; + let mut options: Vec<TCPOption> = vec![]; loop { - match TcpOption::decode(left) { + match TCPOption::decode(left) { Ok((l, opt)) => { left = l; options.push(opt); @@ -217,7 +217,7 @@ impl TcpHeader { if left.len() <= 0 { break; } - if let TcpOption::EOL = opt { + if let TCPOption::EOL = opt { break; } } @@ -229,16 +229,16 @@ impl TcpHeader { } } -impl Decode for TcpHeader { - type Iterm = TcpHeader; - fn decode(input: &[u8]) -> IResult<&[u8], TcpHeader> { - match TcpHeader::fixed_header_decode(input) { +impl Decode for TCPHeader { + type Iterm = TCPHeader; + fn decode(input: &[u8]) -> IResult<&[u8], TCPHeader> { + match TCPHeader::fixed_header_decode(input) { Ok((left, mut header)) => { if header.data_offset > 20 { let options_length = (header.data_offset - 20) as usize; if options_length <= left.len() { if let Ok((__left, options)) = - TcpHeader::options_decode(&left[0..options_length]) + TCPHeader::options_decode(&left[0..options_length]) { header.options = Some(options); Ok((&left[options_length..], header)) @@ -267,12 +267,12 @@ impl Decode for TcpHeader { mod tests { use super::*; use crate::protocol::codec::Decode; - use crate::protocol::tcp::TcpOption::MSS; - use crate::protocol::tcp::TcpOption::NOP; - use crate::protocol::tcp::TcpOption::SACK; - use crate::protocol::tcp::TcpOption::SACKPERMITTED; - use crate::protocol::tcp::TcpOption::TIMESTAMPS; - use crate::protocol::tcp::TcpOption::WSCALE; + use crate::protocol::tcp::TCPOption::MSS; + use crate::protocol::tcp::TCPOption::NOP; + use crate::protocol::tcp::TCPOption::SACK; + use crate::protocol::tcp::TCPOption::SACKPERMITTED; + use crate::protocol::tcp::TCPOption::TIMESTAMPS; + use crate::protocol::tcp::TCPOption::WSCALE; const LAST_SLICE: &'static [u8] = &[0xff]; @@ -341,7 +341,7 @@ mod tests { 0xff, /* Payload */ ]; - let expect_options: Vec<TcpOption> = vec![ + let expect_options: Vec<TCPOption> = vec![ NOP, NOP, TIMESTAMPS { @@ -351,7 +351,7 @@ mod tests { }, ]; - let expectation = TcpHeader { + let expectation = TCPHeader { source_port: 50081, dest_port: 443, seq_num: 1522577104, @@ -370,7 +370,7 @@ mod tests { options: Some(expect_options), }; - assert_eq!(TcpHeader::decode(&bytes), Ok((LAST_SLICE, expectation))); + assert_eq!(TCPHeader::decode(&bytes), Ok((LAST_SLICE, expectation))); } #[test] @@ -449,7 +449,7 @@ mod tests { 0xff, /* Payload */ ]; - let expect_options: Vec<TcpOption> = vec![ + let expect_options: Vec<TCPOption> = vec![ MSS { length: 4, mss: 1460, @@ -467,7 +467,7 @@ mod tests { }, ]; - let expectation = TcpHeader { + let expectation = TCPHeader { source_port: 58816, dest_port: 80, seq_num: 3851697578, @@ -486,7 +486,7 @@ mod tests { options: Some(expect_options), }; - assert_eq!(TcpHeader::decode(&bytes), Ok((LAST_SLICE, expectation))); + assert_eq!(TCPHeader::decode(&bytes), Ok((LAST_SLICE, expectation))); } #[test] @@ -568,7 +568,7 @@ mod tests { 0xff, /* Payload */ ]; - let opts: Vec<TcpOption> = vec![ + let opts: Vec<TCPOption> = vec![ NOP, NOP, TIMESTAMPS { @@ -585,7 +585,7 @@ mod tests { }, ]; - let expectation = TcpHeader { + let expectation = TCPHeader { source_port: 58816, dest_port: 80, seq_num: 3851698039, @@ -604,6 +604,6 @@ mod tests { options: Some(opts), }; - assert_eq!(TcpHeader::decode(&bytes), Ok((LAST_SLICE, expectation))); + assert_eq!(TCPHeader::decode(&bytes), Ok((LAST_SLICE, expectation))); } } |
