ezk_stun_types/attributes/
mod.rs

1use crate::builder::MessageBuilder;
2use crate::parse::{AttrSpan, Message};
3use crate::{Error, NE};
4use byteorder::ReadBytesExt;
5use bytes::BufMut;
6use std::convert::TryFrom;
7use std::str::from_utf8;
8
9mod addr;
10mod error_code;
11mod fingerprint;
12mod ice;
13mod integrity;
14mod password_algs;
15pub mod turn;
16mod user_hash;
17
18pub use addr::*;
19pub use error_code::ErrorCode;
20pub use fingerprint::Fingerprint;
21pub use ice::*;
22pub use integrity::*;
23pub use password_algs::*;
24pub use user_hash::*;
25
26pub(crate) const ATTRIBUTE_HEADER_LEN: usize = 4;
27
28pub trait Attribute<'s> {
29    type Context;
30    const TYPE: u16;
31
32    fn decode(ctx: Self::Context, msg: &'s mut Message, attr: AttrSpan) -> Result<Self, Error>
33    where
34        Self: Sized;
35
36    fn encode(&self, ctx: Self::Context, builder: &mut MessageBuilder);
37
38    fn encode_len(&self) -> Result<u16, Error>;
39}
40
41pub struct StringAttribute<'s, const TYPE: u16>(pub &'s str);
42
43impl<'s, const TYPE: u16> StringAttribute<'s, TYPE> {
44    pub fn new(s: &'s str) -> Self {
45        Self(s)
46    }
47}
48
49impl<'s, const TYPE: u16> Attribute<'s> for StringAttribute<'s, TYPE> {
50    type Context = ();
51    const TYPE: u16 = TYPE;
52
53    fn decode(_: Self::Context, msg: &'s mut Message, attr: AttrSpan) -> Result<Self, Error> {
54        Ok(Self(from_utf8(attr.get_value(msg.buffer()))?))
55    }
56
57    fn encode(&self, _: Self::Context, builder: &mut MessageBuilder) {
58        builder.buffer().extend_from_slice(self.0.as_ref());
59    }
60
61    fn encode_len(&self) -> Result<u16, Error> {
62        Ok(u16::try_from(self.0.len())?)
63    }
64}
65
66pub struct BytesAttribute<'s, const TYPE: u16>(pub &'s [u8]);
67
68impl<'s, const TYPE: u16> BytesAttribute<'s, TYPE> {
69    pub fn new(s: &'s [u8]) -> Self {
70        Self(s)
71    }
72}
73
74impl<'s, const TYPE: u16> Attribute<'s> for BytesAttribute<'s, TYPE> {
75    type Context = ();
76    const TYPE: u16 = TYPE;
77
78    fn decode(_: Self::Context, msg: &'s mut Message, attr: AttrSpan) -> Result<Self, Error> {
79        Ok(Self(attr.get_value(msg.buffer())))
80    }
81
82    fn encode(&self, _: Self::Context, builder: &mut MessageBuilder) {
83        builder.buffer().extend_from_slice(self.0);
84    }
85
86    fn encode_len(&self) -> Result<u16, Error> {
87        Ok(u16::try_from(self.0.len())?)
88    }
89}
90
91/// [RFC8489](https://datatracker.ietf.org/doc/html/rfc8489#section-14.3)
92pub type Username<'s> = StringAttribute<'s, 0x0006>;
93
94/// [RFC8489](https://datatracker.ietf.org/doc/html/rfc8489#section-14.9)
95pub type Realm<'s> = StringAttribute<'s, 0x0014>;
96
97/// [RFC8489](https://datatracker.ietf.org/doc/html/rfc8489#section-14.10)
98pub type Nonce<'s> = BytesAttribute<'s, 0x0015>;
99
100/// [RFC8489](https://datatracker.ietf.org/doc/html/rfc8489#section-14.13)
101pub struct UnknownAttributes(pub Vec<u16>);
102
103impl Attribute<'_> for UnknownAttributes {
104    type Context = ();
105    const TYPE: u16 = 0x000A;
106
107    fn decode(_: Self::Context, msg: &mut Message, attr: AttrSpan) -> Result<Self, Error> {
108        let mut value = attr.get_value(msg.buffer());
109
110        let mut attributes = vec![];
111
112        while !value.is_empty() {
113            attributes.push(value.read_u16::<NE>()?);
114        }
115
116        Ok(Self(attributes))
117    }
118
119    fn encode(&self, _: Self::Context, builder: &mut MessageBuilder) {
120        for &attr in &self.0 {
121            builder.buffer().put_u16(attr);
122        }
123    }
124
125    fn encode_len(&self) -> Result<u16, Error> {
126        Ok(u16::try_from(self.0.len() * 2)?)
127    }
128}
129
130/// [RFC8489](https://datatracker.ietf.org/doc/html/rfc8489#section-14.14)
131pub type Software<'s> = StringAttribute<'s, 0x8022>;
132
133/// [RFC8489](https://datatracker.ietf.org/doc/html/rfc8489#section-14.15)
134pub type AlternateDomain<'s> = BytesAttribute<'s, 0x8003>;