stun_types/attribute/
fingerprint.rs1use core::convert::TryFrom;
10
11use crate::message::StunParseError;
12
13use super::{
14 Attribute, AttributeFromRaw, AttributeStaticType, AttributeType, AttributeWrite,
15 AttributeWriteExt, RawAttribute,
16};
17
18#[derive(Debug, Clone, PartialEq, Eq)]
20pub struct Fingerprint {
21 fingerprint: [u8; 4],
22}
23
24impl AttributeStaticType for Fingerprint {
25 const TYPE: AttributeType = AttributeType(0x8028);
26}
27
28impl Attribute for Fingerprint {
29 fn get_type(&self) -> AttributeType {
30 Self::TYPE
31 }
32
33 fn length(&self) -> u16 {
34 4
35 }
36}
37
38impl AttributeWrite for Fingerprint {
39 fn to_raw(&self) -> RawAttribute<'_> {
40 let buf = bytewise_xor!(4, self.fingerprint, Fingerprint::XOR_CONSTANT, 0);
41 RawAttribute::new(Fingerprint::TYPE, &buf).into_owned()
42 }
43
44 fn write_into_unchecked(&self, dest: &mut [u8]) {
45 let offset = self.write_header_unchecked(dest);
46 let buf = bytewise_xor!(4, self.fingerprint, Fingerprint::XOR_CONSTANT, 0);
47 dest[offset..offset + 4].copy_from_slice(&buf);
48 }
49}
50
51impl AttributeFromRaw<'_> for Fingerprint {
52 fn from_raw_ref(raw: &RawAttribute) -> Result<Self, StunParseError>
53 where
54 Self: Sized,
55 {
56 Self::try_from(raw)
57 }
58}
59
60impl TryFrom<&RawAttribute<'_>> for Fingerprint {
61 type Error = StunParseError;
62
63 fn try_from(raw: &RawAttribute) -> Result<Self, Self::Error> {
64 raw.check_type_and_len(Self::TYPE, 4..=4)?;
65 let boxed: [u8; 4] = (&*raw.value).try_into().unwrap();
67 let fingerprint = bytewise_xor!(4, boxed, Fingerprint::XOR_CONSTANT, 0);
68 Ok(Self { fingerprint })
69 }
70}
71
72impl Fingerprint {
73 const XOR_CONSTANT: [u8; 4] = [0x53, 0x54, 0x55, 0x4E];
74
75 pub fn new(fingerprint: [u8; 4]) -> Self {
86 Self { fingerprint }
87 }
88
89 pub fn fingerprint(&self) -> &[u8; 4] {
100 &self.fingerprint
101 }
102
103 pub fn compute(data: &[u8]) -> [u8; 4] {
112 use crc::{Crc, CRC_32_ISO_HDLC};
113 const CRC_ALGO: Crc<u32> = Crc::<u32>::new(&CRC_32_ISO_HDLC);
114 CRC_ALGO.checksum(data).to_be_bytes()
115 }
116}
117
118impl core::fmt::Display for Fingerprint {
119 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
120 write!(f, "{}: 0x", Self::TYPE)?;
121 for val in self.fingerprint.iter() {
122 write!(f, "{val:02x}")?;
123 }
124 Ok(())
125 }
126}
127
128#[cfg(test)]
129mod tests {
130 use crate::attribute::AttributeExt;
131
132 use super::*;
133 use alloc::vec;
134 use alloc::vec::Vec;
135 use byteorder::{BigEndian, ByteOrder};
136 use tracing::trace;
137
138 #[test]
139 fn fingerprint() {
140 let _log = crate::tests::test_init_log();
141 let val = [1; 4];
142 let attr = Fingerprint::new(val);
143 trace!("{attr}");
144 assert_eq!(attr.fingerprint(), &val);
145 assert_eq!(attr.length(), 4);
146 let raw = RawAttribute::from(&attr);
147 trace!("{raw}");
148 assert_eq!(raw.get_type(), Fingerprint::TYPE);
149 let mapped2 = Fingerprint::try_from(&raw).unwrap();
150 assert_eq!(mapped2.fingerprint(), &val);
151 let mut data: Vec<_> = raw.clone().into();
153 let len = data.len();
154 BigEndian::write_u16(&mut data[2..4], len as u16 - 4 - 1);
155 assert!(matches!(
156 Fingerprint::try_from(&RawAttribute::from_bytes(data[..len - 1].as_ref()).unwrap()),
157 Err(StunParseError::Truncated {
158 expected: 4,
159 actual: 3
160 })
161 ));
162 let mut data: Vec<_> = raw.clone().into();
164 BigEndian::write_u16(&mut data[0..2], 0);
165 assert!(matches!(
166 Fingerprint::try_from(&RawAttribute::from_bytes(data.as_ref()).unwrap()),
167 Err(StunParseError::WrongAttributeImplementation)
168 ));
169
170 let mut dest = vec![0; raw.padded_len()];
171 attr.write_into(&mut dest).unwrap();
172 let raw = RawAttribute::from_bytes(&dest).unwrap();
173 let attr2 = Fingerprint::try_from(&raw).unwrap();
174 assert_eq!(attr2.fingerprint(), &val);
175 }
176}