Skip to main content

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    #[test]
97    fn test_from_bytes() {
98        init_logger(true);
99
100        let bytes = Socket::receive_mock(&[
101            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,
102            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,
103            48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48,
104            48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48,
105            48, 48, 48, 48, 48, 48, 48, 48, 48, 0, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
106            64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
107            64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
108            64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
109            64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
110            64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
111            64, 64, 64, 64, 64, 0, 99, 130, 83, 99, 0, 53, 1, 1, 255,
112        ]);
113        let test: Message = Message::from_bytes(&bytes);
114        let expected = Message {
115            op: Ops::Request,
116            htype: HTypes::Ethernet,
117            hlen: 6,
118            hops: 1,
119            xid: 4,
120            secs: 2,
121            flags: Flags { broadcast: true },
122            ciaddr: Ipv4Addr::new(1, 1, 1, 1),
123            yiaddr: Ipv4Addr::new(1, 1, 1, 1),
124            siaddr: Ipv4Addr::new(1, 1, 1, 1),
125            giaddr: Ipv4Addr::new(1, 1, 1, 1),
126            chaddr: [3; 16],
127            sname: SName::new(c"000000000000000000000000000000000000000000000000000000000000000")
128                .unwrap(),
129            file: File::new(c"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@").unwrap(),
130            magic: [99, 130, 83, 99],
131            options: vec![
132                MessageOptions::MessageType(MessageTypes::Discover),
133            ].into(),
134        };
135
136        assert_eq!(expected, test);
137    }
138}