packet/icmp/mod.rs
1// DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
2// Version 2, December 2004
3//
4// Copyleft (ↄ) meh. <meh@schizofreni.co> | http://meh.schizofreni.co
5//
6// Everyone is permitted to copy and distribute verbatim or modified
7// copies of this license document, and changing it is allowed as long
8// as the name is changed.
9//
10// DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
11// TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
12//
13// 0. You just DO WHAT THE FUCK YOU WANT TO.
14
15mod kind;
16pub use self::kind::Kind;
17
18/// ICMP codes.
19pub mod code;
20
21mod packet;
22pub use self::packet::Packet;
23
24mod builder;
25pub use self::builder::Builder;
26
27/// Echo Request/Reply.
28pub mod echo;
29
30/// Information Request/Reply.
31pub mod information;
32
33/// Parmeter Problem.
34pub mod parameter_problem;
35
36/// Source Quench, Destination Unreachable and Time Exceeded.
37pub mod previous;
38
39/// Redirect Message.
40pub mod redirect_message;
41
42/// Timestamp Request/Reply.
43pub mod timestamp;
44
45/// Calculate the checksum for an ICMP packet.
46pub fn checksum(buffer: &[u8]) -> u16 {
47 use std::io::Cursor;
48 use byteorder::{ReadBytesExt, BigEndian};
49
50 let mut result = 0xffffu32;
51 let mut buffer = Cursor::new(buffer);
52
53 while let Ok(value) = buffer.read_u16::<BigEndian>() {
54 // Skip checksum field.
55 if buffer.position() == 4 {
56 continue;
57 }
58
59 result += u32::from(value);
60
61 if result > 0xffff {
62 result -= 0xffff;
63 }
64 }
65
66 !result as u16
67}