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