summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/event/manager.rs12
-rw-r--r--src/packet/packet.rs21
-rw-r--r--src/protocol/dns.rs135
-rw-r--r--src/protocol/ipv4.rs104
-rw-r--r--src/protocol/mpls.rs2
-rw-r--r--src/protocol/tcp.rs2
6 files changed, 209 insertions, 67 deletions
diff --git a/src/event/manager.rs b/src/event/manager.rs
index c28b34d..0af352b 100644
--- a/src/event/manager.rs
+++ b/src/event/manager.rs
@@ -229,16 +229,12 @@ mod tests {
let mut packet = Packet::new(&bytes, bytes.len() as u32);
let result = packet.handle();
match result {
- Ok(_v) => {
- // println!("SUCCESS: {:?}, {:?}", packet, _v);
- // println!("SUCCESS: {:#?}, {:?}", packet, _v);
- // dbg!(packet);
+ Ok(_) => {
+ println!("Ok Packet: {:?}", packet);
}
Err(e) => {
- println!("ERROR Data: {:?}", packet);
- println!("ERROR Code: {:?}", e);
- // println!("ERROR Desc: {}", e);
- assert_eq!(0, 1);
+ println!("Unexpected Packet: {:?} {:?}", e, packet);
+ assert_eq!(1, 0);
}
}
diff --git a/src/packet/packet.rs b/src/packet/packet.rs
index 6a81c4d..7b9768a 100644
--- a/src/packet/packet.rs
+++ b/src/packet/packet.rs
@@ -802,6 +802,7 @@ mod tests {
checksum: 0xc352,
source_address: Ipv4Addr::new(192, 168, 0, 101),
dest_address: Ipv4Addr::new(121, 14, 154, 93),
+ options: None,
};
let ipv6_hdr = IPv6Header {
version: 6,
@@ -1060,6 +1061,7 @@ mod tests {
checksum: 0x9293,
source_address: Ipv4Addr::new(10, 118, 10, 1),
dest_address: Ipv4Addr::new(10, 118, 10, 2),
+ options: None,
},
&bytes[42..]
)
@@ -1259,6 +1261,7 @@ mod tests {
checksum: 0x0000,
source_address: Ipv4Addr::new(18, 3, 2, 3),
dest_address: Ipv4Addr::new(18, 3, 1, 100),
+ options: None,
},
&bytes[42..]
)
@@ -1506,6 +1509,7 @@ mod tests {
checksum: 0xd4ea,
source_address: Ipv4Addr::new(1, 1, 1, 1),
dest_address: Ipv4Addr::new(2, 2, 2, 2),
+ options: None,
},
&bytes[74..]
)
@@ -1691,6 +1695,7 @@ mod tests {
checksum: 0x09ce,
source_address: Ipv4Addr::new(210, 77, 88, 163),
dest_address: Ipv4Addr::new(59, 66, 4, 50),
+ options: None,
},
&bytes[34..]
)
@@ -2070,6 +2075,7 @@ mod tests {
checksum: 0x1148,
source_address: Ipv4Addr::new(69, 67, 35, 146),
dest_address: Ipv4Addr::new(41, 202, 46, 110),
+ options: None,
},
&bytes[42..]
)
@@ -2090,6 +2096,7 @@ mod tests {
checksum: 0xde84,
source_address: Ipv4Addr::new(10, 10, 100, 25),
dest_address: Ipv4Addr::new(10, 10, 101, 2),
+ options: None,
},
&bytes[62..]
)
@@ -2277,6 +2284,7 @@ mod tests {
checksum: 0xa6d9,
source_address: Ipv4Addr::new(10, 31, 0, 1),
dest_address: Ipv4Addr::new(10, 34, 0, 1),
+ options: None,
},
&bytes[42..]
)
@@ -2484,6 +2492,7 @@ mod tests {
checksum: 0xecc9,
source_address: Ipv4Addr::new(192, 168, 0, 10),
dest_address: Ipv4Addr::new(192, 168, 0, 20),
+ options: None,
},
&bytes[60..]
)
@@ -2751,6 +2760,7 @@ mod tests {
checksum: 0xcc48,
source_address: Ipv4Addr::new(10, 16, 131, 196),
dest_address: Ipv4Addr::new(112, 49, 26, 208),
+ options: None,
},
&bytes[102..]
)
@@ -2956,6 +2966,7 @@ mod tests {
checksum: 0x0127,
source_address: Ipv4Addr::new(72, 205, 54, 70),
dest_address: Ipv4Addr::new(86, 106, 164, 150),
+ options: None,
},
&bytes[34..]
)
@@ -2998,6 +3009,7 @@ mod tests {
checksum: 0x685b,
source_address: Ipv4Addr::new(10, 10, 11, 2),
dest_address: Ipv4Addr::new(10, 10, 13, 2),
+ options: None,
},
&bytes[58..]
)
@@ -3040,6 +3052,7 @@ mod tests {
checksum: 0xb32b,
source_address: Ipv4Addr::new(10, 10, 25, 1),
dest_address: Ipv4Addr::new(192, 168, 1, 2),
+ options: None,
},
&bytes[82..]
)
@@ -3204,6 +3217,7 @@ mod tests {
checksum: 0x970d,
source_address: Ipv4Addr::new(172, 16, 0, 100),
dest_address: Ipv4Addr::new(172, 16, 0, 254),
+ options: None,
},
&bytes[34..]
)
@@ -3400,6 +3414,7 @@ mod tests {
checksum: 0x397d,
source_address: Ipv4Addr::new(20, 0, 0, 2),
dest_address: Ipv4Addr::new(20, 0, 0, 1),
+ options: None,
},
&bytes[34..]
)
@@ -3453,6 +3468,7 @@ mod tests {
checksum: 0x2f2c,
source_address: Ipv4Addr::new(17, 1, 1, 122),
dest_address: Ipv4Addr::new(40, 0, 0, 2),
+ options: None,
},
&bytes[70..]
)
@@ -3670,6 +3686,7 @@ mod tests {
checksum: 0xd916,
source_address: Ipv4Addr::new(172, 16, 0, 100),
dest_address: Ipv4Addr::new(172, 16, 0, 254),
+ options: None,
},
&bytes[34..]
)
@@ -3736,6 +3753,7 @@ mod tests {
checksum: 0x3a6f,
source_address: Ipv4Addr::new(172, 16, 2, 100),
dest_address: Ipv4Addr::new(10, 0, 6, 229),
+ options: None,
},
&bytes[74..]
)
@@ -4061,7 +4079,8 @@ mod tests {
protocol: IPProtocol::TCP,
checksum: 0xc91f,
source_address: Ipv4Addr::new(91, 185, 14, 33),
- dest_address: Ipv4Addr::new(100, 65, 55, 0)
+ dest_address: Ipv4Addr::new(100, 65, 55, 0),
+ options: None,
},
&bytes[46..]
)
diff --git a/src/protocol/dns.rs b/src/protocol/dns.rs
index c2d2411..991dedc 100644
--- a/src/protocol/dns.rs
+++ b/src/protocol/dns.rs
@@ -1474,15 +1474,18 @@ mod tests {
assert_eq!(DNSMessage::decode(&bytes), Ok((LAST_SLICE, expectation)));
- match DNSMessage::decode(&bytes) {
- Ok((left, message)) => {
- println!("{:?}; left {:?}", message, left);
+ // example
+ let result = DNSMessage::decode(&bytes);
+ match result {
+ Ok((payload, header)) => {
+ println!("OK: {:?}, payload: {}", header, payload.len());
}
- Err(err) => {
- println!("{:?}\n", err);
+ Err(e) => {
+ println!("ERR: {:?}", e);
}
}
- // assert_eq!(0, 1);
+
+ // assert_eq!(1, 0);
}
#[test]
@@ -1595,15 +1598,18 @@ mod tests {
assert_eq!(DNSMessage::decode(&bytes), Ok((LAST_SLICE, expectation)));
- match DNSMessage::decode(&bytes) {
- Ok((left, message)) => {
- println!("{:?}; left {:?}", message, left);
+ // example
+ let result = DNSMessage::decode(&bytes);
+ match result {
+ Ok((payload, header)) => {
+ println!("OK: {:?}, payload: {}", header, payload.len());
}
- Err(err) => {
- println!("{:?}\n", err);
+ Err(e) => {
+ println!("ERR: {:?}", e);
}
}
- // assert_eq!(0, 1);
+
+ // assert_eq!(1, 0);
}
#[test]
@@ -1716,15 +1722,18 @@ mod tests {
assert_eq!(DNSMessage::decode(&bytes), Ok((LAST_SLICE, expectation)));
- match DNSMessage::decode(&bytes) {
- Ok((left, message)) => {
- println!("{:?}; left {:?}", message, left);
+ // example
+ let result = DNSMessage::decode(&bytes);
+ match result {
+ Ok((payload, header)) => {
+ println!("OK: {:?}, payload: {}", header, payload.len());
}
- Err(err) => {
- println!("{:?}\n", err);
+ Err(e) => {
+ println!("ERR: {:?}", e);
}
}
- // assert_eq!(0, 1);
+
+ // assert_eq!(1, 0);
}
// TODO
@@ -1840,15 +1849,18 @@ mod tests {
assert_eq!(DNSMessage::decode(&bytes), Ok((LAST_SLICE, expectation)));
- match DNSMessage::decode(&bytes) {
- Ok((left, message)) => {
- println!("{:?}; left {:?}", message, left);
+ // example
+ let result = DNSMessage::decode(&bytes);
+ match result {
+ Ok((payload, header)) => {
+ println!("OK: {:?}, payload: {}", header, payload.len());
}
- Err(err) => {
- println!("{:?}\n", err);
+ Err(e) => {
+ println!("ERR: {:?}", e);
}
}
- // assert_eq!(0, 1);
+
+ // assert_eq!(1, 0);
}
// TODO
@@ -1969,15 +1981,18 @@ mod tests {
assert_eq!(DNSMessage::decode(&bytes), Ok((LAST_SLICE, expectation)));
- match DNSMessage::decode(&bytes) {
- Ok((left, message)) => {
- println!("{:?}; left {:?}", message, left);
+ // example
+ let result = DNSMessage::decode(&bytes);
+ match result {
+ Ok((payload, header)) => {
+ println!("OK: {:?}, payload: {}", header, payload.len());
}
- Err(err) => {
- println!("{:?}\n", err);
+ Err(e) => {
+ println!("ERR: {:?}", e);
}
}
- // assert_eq!(0, 1);
+
+ // assert_eq!(1, 0);
}
#[test]
@@ -2091,15 +2106,18 @@ mod tests {
assert_eq!(DNSMessage::decode(&bytes), Ok((LAST_SLICE, expectation)));
- match DNSMessage::decode(&bytes) {
- Ok((left, message)) => {
- println!("{:?}; left {:?}", message, left);
+ // example
+ let result = DNSMessage::decode(&bytes);
+ match result {
+ Ok((payload, header)) => {
+ println!("OK: {:?}, payload: {}", header, payload.len());
}
- Err(err) => {
- println!("{:?}\n", err);
+ Err(e) => {
+ println!("ERR: {:?}", e);
}
}
- // assert_eq!(0, 1);
+
+ // assert_eq!(1, 0);
}
// TODO
@@ -2764,15 +2782,18 @@ mod tests {
assert_eq!(DNSMessage::decode(&bytes), Ok((LAST_SLICE, expectation)));
- match DNSMessage::decode(&bytes) {
- Ok((left, message)) => {
- println!("{:?}; left {:?}", message, left);
+ // example
+ let result = DNSMessage::decode(&bytes);
+ match result {
+ Ok((payload, header)) => {
+ println!("OK: {:?}, payload: {}", header, payload.len());
}
- Err(err) => {
- println!("{:?}\n", err);
+ Err(e) => {
+ println!("ERR: {:?}", e);
}
}
- // assert_eq!(0, 1);
+
+ // assert_eq!(1, 0);
}
// TODO
@@ -2987,15 +3008,18 @@ mod tests {
assert_eq!(DNSMessage::decode(&bytes), Ok((LAST_SLICE, expectation)));
- match DNSMessage::decode(&bytes) {
- Ok((left, message)) => {
- println!("{:?}; left {:?}", message, left);
+ // example
+ let result = DNSMessage::decode(&bytes);
+ match result {
+ Ok((payload, header)) => {
+ println!("OK: {:?}, payload: {}", header, payload.len());
}
- Err(err) => {
- println!("{:?}\n", err);
+ Err(e) => {
+ println!("ERR: {:?}", e);
}
}
- // assert_eq!(0, 1);
+
+ // assert_eq!(1, 0);
}
#[test]
@@ -3478,15 +3502,18 @@ mod tests {
assert_eq!(DNSMessage::decode(&bytes), Ok((LAST_SLICE, expectation)));
- match DNSMessage::decode(&bytes) {
- Ok((left, message)) => {
- println!("{:?}; left {:?}", message, left);
+ // example
+ let result = DNSMessage::decode(&bytes);
+ match result {
+ Ok((payload, header)) => {
+ println!("OK: {:?}, payload: {}", header, payload.len());
}
- Err(err) => {
- println!("{:?}\n", err);
+ Err(e) => {
+ println!("ERR: {:?}", e);
}
}
- // assert_eq!(0, 1);
+
+ // assert_eq!(1, 0);
}
// TODO
diff --git a/src/protocol/ipv4.rs b/src/protocol/ipv4.rs
index 2297196..f2fc19b 100644
--- a/src/protocol/ipv4.rs
+++ b/src/protocol/ipv4.rs
@@ -45,6 +45,7 @@ pub struct IPv4Header {
pub checksum: u16,
pub source_address: Ipv4Addr,
pub dest_address: Ipv4Addr,
+ pub options: Option<Vec<u8>>,
}
/******************************************************************************
@@ -84,8 +85,11 @@ impl Decode for IPv4Header {
let (input, checksum) = number::streaming::be_u16(input)?;
let (input, source_address) = address_v4_decode(input)?;
let (input, dest_address) = address_v4_decode(input)?;
-
- // TODO IPv4 Options Decode
+ let (input, options) = match verihl.1 > 5 {
+ true => nom::bytes::streaming::take((verihl.1 - 5) * 4)(input)
+ .map(|(i, l)| (i, Some(l.to_vec())))?,
+ false => (input, None),
+ };
Ok((
input,
@@ -102,6 +106,7 @@ impl Decode for IPv4Header {
checksum,
source_address,
dest_address,
+ options,
},
))
}
@@ -121,7 +126,8 @@ mod tests {
const LAST_SLICE: &'static [u8] = &[0xff];
#[test]
- fn ipv4_header_decode() {
+ // Without Options
+ fn ipv4_header_decode1() {
/*
* Internet Protocol Version 4, Src: 192.168.0.101, Dst: 121.14.154.93
* 0100 .... = Version: 4
@@ -173,6 +179,98 @@ mod tests {
checksum: 0xc352,
source_address: Ipv4Addr::new(192, 168, 0, 101),
dest_address: Ipv4Addr::new(121, 14, 154, 93),
+ options: None,
+ };
+
+ assert_eq!(IPv4Header::decode(&bytes), Ok((LAST_SLICE, expectation)));
+
+ // example
+ let result = IPv4Header::decode(&bytes);
+ match result {
+ Ok((payload, header)) => {
+ println!("OK: {:?}, payload: {}", header, payload.len());
+ }
+ Err(e) => {
+ println!("ERR: {:?}", e);
+ }
+ }
+
+ // assert_eq!(1, 0);
+ }
+
+ #[test]
+ // With Options
+ fn ipv4_header_decode2() {
+ /*
+ * Internet Protocol Version 4, Src: 127.0.0.1, Dst: 127.0.0.1
+ * 0100 .... = Version: 4
+ * .... 1111 = Header Length: 60 bytes (15)
+ * Differentiated Services Field: 0x00 (DSCP: CS0, ECN: Not-ECT)
+ * 0000 00.. = Differentiated Services Codepoint: Default (0)
+ * .... ..00 = Explicit Congestion Notification: Not ECN-Capable Transport (0)
+ * Total Length: 124
+ * Identification: 0x0000 (0)
+ * 010. .... = Flags: 0x2, Don't fragment
+ * 0... .... = Reserved bit: Not set
+ * .1.. .... = Don't fragment: Set
+ * ..0. .... = More fragments: Not set
+ * ...0 0000 0000 0000 = Fragment Offset: 0
+ * Time to Live: 64
+ * Protocol: ICMP (1)
+ * Header Checksum: 0xfd30 [correct]
+ * [Header checksum status: Good]
+ * [Calculated Checksum: 0xfd30]
+ * Source Address: 127.0.0.1
+ * Destination Address: 127.0.0.1
+ * Options: (40 bytes), Commercial Security
+ * IP Option - Commercial Security (40 bytes)
+ * Type: 134
+ * 1... .... = Copy on fragmentation: Yes
+ * .00. .... = Class: Control (0)
+ * ...0 0110 = Number: Commercial IP security option (6)
+ * Length: 40
+ * DOI: 1
+ * Tag Type: Restrictive Category Bitmap (1)
+ * Sensitivity Level: 1
+ * Categories: 0,2,4,5,6,239
+ */
+
+ let bytes = [
+ 0x4f, /* Version and Header length */
+ 0x00, /* Differentiated Services Field */
+ 0x00, 0x7c, /* Total Length */
+ 0x00, 0x00, /* Identification */
+ 0x40, 0x00, /* Flags and Fragment Offset */
+ 0x40, /* Time to Live */
+ 0x01, /* Protocol */
+ 0xfd, 0x30, /* Header Checksum */
+ 0x7f, 0x00, 0x00, 0x01, /* Source Address */
+ 0x7f, 0x00, 0x00, 0x01, /* Destination Address */
+ 0x86, 0x28, 0x00, 0x00, 0x00, 0x01, 0x01, 0x22, 0x00, 0x01, 0xae, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x01, /* Options */
+ 0xff, /* Payload */
+ ];
+
+ let expectation = IPv4Header {
+ version: 4,
+ ihl: 60,
+ tos: 0,
+ length: 124,
+ id: 0x0000,
+ flags: 0x2,
+ frag_offset: 0,
+ ttl: 64,
+ protocol: IPProtocol::ICMP,
+ checksum: 0xfd30,
+ source_address: Ipv4Addr::new(127, 0, 0, 1),
+ dest_address: Ipv4Addr::new(127, 0, 0, 1),
+ options: Some(vec![
+ 0x86, 0x28, 0x00, 0x00, 0x00, 0x01, 0x01, 0x22, 0x00, 0x01, 0xae, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
+ ]),
};
assert_eq!(IPv4Header::decode(&bytes), Ok((LAST_SLICE, expectation)));
diff --git a/src/protocol/mpls.rs b/src/protocol/mpls.rs
index 2f441f9..fc3ee1d 100644
--- a/src/protocol/mpls.rs
+++ b/src/protocol/mpls.rs
@@ -111,7 +111,7 @@ mod tests {
// example
let mut payload = &bytes[..];
while let Ok((remain, header)) = MPLSHeader::decode(payload) {
- println!("return: {:?}, payload: {}", header, remain.len());
+ println!("OK: {:?}, payload: {}", header, remain.len());
payload = remain;
if header.bottom_of_stack {
break;
diff --git a/src/protocol/tcp.rs b/src/protocol/tcp.rs
index 09a5690..687ccea 100644
--- a/src/protocol/tcp.rs
+++ b/src/protocol/tcp.rs
@@ -241,10 +241,12 @@ impl Decode for TCPHeader {
header.options = Some(options);
Ok((&left[options_length..], header))
} else {
+ // TODO
println!("tcp header options parser error, skip tcp options");
Ok((&left[options_length..], header))
}
} else {
+ // TODO
println!("tcp header options data not enough");
Err(Err::Incomplete(Needed::new(options_length - left.len())))
}