toe_beans/v4/message/
decode.rs

1use crate::v4::UndecodedMessage;
2use std::fmt::Debug;
3
4/// A supertrait that defines everything a custom message
5/// needs to be decoded after being received by a socket.
6pub trait Decodable: DecodeMessage + Debug {}
7
8/// Define how the bytes of a DHCP message's fields
9/// decode into your custom Message's types.
10///
11/// ...or don't and use our homemade [Message](crate::v4::message::Message)
12/// with this already implemented.
13pub trait DecodeMessage {
14    /// The type your custom Message uses to represent the op field.
15    type Op;
16    /// The type your custom Message uses to represent the htype field.
17    type Htype;
18    /// The type your custom Message uses to represent the hlen field.
19    type Hlen;
20    /// The type your custom Message uses to represent the hops field.
21    type Hops;
22    /// The type your custom Message uses to represent the xid field.
23    type Xid;
24    /// The type your custom Message uses to represent the secs field.
25    type Secs;
26    /// The type your custom Message uses to represent the flags field.
27    type Flags;
28    /// The type your custom Message uses to represent the ciaddr field.
29    type Ciaddr;
30    /// The type your custom Message uses to represent the yiaddr field.
31    type Yiaddr;
32    /// The type your custom Message uses to represent the siaddr field.
33    type Siaddr;
34    /// The type your custom Message uses to represent the giaddr field.
35    type Giaddr;
36    /// The type your custom Message uses to represent the chaddr field.
37    type Chaddr;
38    /// The type your custom Message uses to represent the sname field.
39    type Sname;
40    /// The type your custom Message uses to represent the file field.
41    type File;
42    /// The type your custom Message uses to represent the magic field.
43    type Magic;
44    /// The type your custom Message uses to represent the options field.
45    type Options;
46
47    /// Your custom Message.
48    type Output;
49
50    /// Convert an op field's byte into your custom Message's type.
51    fn decode_op(op: &UndecodedMessage) -> Self::Op;
52    /// Convert an htype field's byte into your custom Message's type.
53    fn decode_htype(htype: &UndecodedMessage) -> Self::Htype;
54    /// Convert an hlen field's byte into your custom Message's type.
55    fn decode_hlen(hlen: &UndecodedMessage) -> Self::Hlen;
56    /// Convert an hops field's byte into your custom Message's type.
57    fn decode_hops(hops: &UndecodedMessage) -> Self::Hops;
58    /// Convert an xid field's bytes into your custom Message's type.
59    fn decode_xid(xid: &UndecodedMessage) -> Self::Xid;
60    /// Convert an secs field's bytes into your custom Message's type.
61    fn decode_secs(secs: &UndecodedMessage) -> Self::Secs;
62    /// Convert an flags field's bytes into your custom Message's type.
63    fn decode_flags(flags: &UndecodedMessage) -> Self::Flags;
64    /// Convert an ciaddr field's bytes into your custom Message's type.
65    fn decode_ciaddr(ciaddr: &UndecodedMessage) -> Self::Ciaddr;
66    /// Convert an yiaddr field's bytes into your custom Message's type.
67    fn decode_yiaddr(yiaddr: &UndecodedMessage) -> Self::Yiaddr;
68    /// Convert an siaddr field's bytes into your custom Message's type.
69    fn decode_siaddr(siaddr: &UndecodedMessage) -> Self::Siaddr;
70    /// Convert an giaddr field's bytes into your custom Message's type.
71    fn decode_giaddr(giaddr: &UndecodedMessage) -> Self::Giaddr;
72    /// Convert an chaddr field's bytes into your custom Message's type.
73    fn decode_chaddr(chaddr: &UndecodedMessage) -> Self::Chaddr;
74    /// Convert an sname field's bytes into your custom Message's type.
75    fn decode_sname(sname: &UndecodedMessage) -> Self::Sname;
76    /// Convert an file field's bytes into your custom Message's type.
77    fn decode_file(file: &UndecodedMessage) -> Self::File;
78    /// Convert an magic field's bytes into your custom Message's type.
79    fn decode_magic(magic: &UndecodedMessage) -> Self::Magic;
80    /// Convert an options field's bytes into your custom Message's type.
81    fn decode_options(options: &UndecodedMessage) -> Self::Options;
82
83    /// decodes a byte array into a [Message](crate::v4::Message) or other type that implements decodeMessage.
84    fn from_bytes(undecoded: &UndecodedMessage) -> Self::Output;
85}
86
87// ------------------------------------------------
88
89#[cfg(test)]
90mod tests {
91    use std::net::Ipv4Addr;
92
93    use super::*;
94    use crate::v4::*;
95
96    // Output additional debug info with
97    // RUST_LOG=debug cargo test test_from_bytes -- --show-output
98    fn init_logger() {
99        let _ = env_logger::builder().is_test(true).try_init();
100    }
101
102    #[test]
103    fn test_from_bytes() {
104        init_logger();
105
106        let bytes = Socket::receive_mock(&[
107            1, 1, 6, 1, 0, 0, 0, 4, 0, 2, 128, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
108            3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48,
109            48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48,
110            48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48,
111            48, 48, 48, 48, 48, 48, 48, 48, 48, 0, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
112            64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
113            64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
114            64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
115            64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
116            64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
117            64, 64, 64, 64, 64, 0, 99, 130, 83, 99, 0, 53, 1, 1, 255,
118        ]);
119        let test: Message = Message::from_bytes(&bytes);
120        let expected = Message {
121            op: Ops::Request,
122            htype: HTypes::Ethernet,
123            hlen: 6,
124            hops: 1,
125            xid: 4,
126            secs: 2,
127            flags: Flags { broadcast: true },
128            ciaddr: Ipv4Addr::new(1, 1, 1, 1),
129            yiaddr: Ipv4Addr::new(1, 1, 1, 1),
130            siaddr: Ipv4Addr::new(1, 1, 1, 1),
131            giaddr: Ipv4Addr::new(1, 1, 1, 1),
132            chaddr: [3; 16],
133            sname: SName::new(c"000000000000000000000000000000000000000000000000000000000000000")
134                .unwrap(),
135            file: File::new(c"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@").unwrap(),
136            magic: [99, 130, 83, 99],
137            options: vec![
138                MessageOptions::MessageType(MessageTypes::Discover),
139            ],
140        };
141
142        assert_eq!(expected, test);
143    }
144}