Skip to main content

rtci_sdp/attribute/
sctp.rs

1//! The SCTP attributes of RFC 8841: `sctp-port`, `max-message-size`, and the `sctpmap`
2//! they replaced.
3
4use std::fmt;
5
6use shared::error::Result;
7
8use super::TypedAttribute;
9use crate::grammar::{self, syntax};
10
11/// `a=sctp-port:<port>` (RFC 8841 section 5): `1*5(DIGIT)`, 0 to 65535, "leading zeroes
12/// MUST NOT be used".
13#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
14pub struct SctpPort(pub u16);
15
16impl TypedAttribute for SctpPort {
17    const NAME: &'static str = "sctp-port";
18
19    fn parse(value: &str) -> Result<Self> {
20        grammar::digits_no_leading_zero("a=sctp-port", value).map(SctpPort)
21    }
22}
23
24impl fmt::Display for SctpPort {
25    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26        write!(f, "{}", self.0)
27    }
28}
29
30/// `a=max-message-size:<octets>` (RFC 8841 section 6): `1*DIGIT` without leading zeroes.
31/// 0 means the endpoint handles messages of any size; an absent attribute means 64
32/// kilobytes.
33#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
34pub struct MaxMessageSize(pub u64);
35
36impl TypedAttribute for MaxMessageSize {
37    const NAME: &'static str = "max-message-size";
38
39    fn parse(value: &str) -> Result<Self> {
40        grammar::digits_no_leading_zero("a=max-message-size", value).map(MaxMessageSize)
41    }
42}
43
44impl fmt::Display for MaxMessageSize {
45    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46        write!(f, "{}", self.0)
47    }
48}
49
50/// `a=sctpmap:<port> <app> [<streams>]` (draft-ietf-mmusic-sctp-sdp-05), what data sections
51/// carried before `a=sctp-port`: the SCTP port, the application protocol
52/// (`webrtc-datachannel`) and the number of streams.
53#[derive(Debug, Clone, PartialEq, Eq, Hash)]
54pub struct SctpMap {
55    /// The SCTP port: the `<fmt>` of the old-style m= line.
56    pub port: u16,
57    /// The application protocol, a token.
58    pub app: String,
59    /// The number of streams, when given.
60    pub streams: Option<u32>,
61}
62
63impl TypedAttribute for SctpMap {
64    const NAME: &'static str = "sctpmap";
65
66    fn parse(value: &str) -> Result<Self> {
67        let fields = grammar::fields("a=sctpmap", value)?;
68        let (port, app, streams) = match fields[..] {
69            [port, app] => (port, app, None),
70            [port, app, streams] => (port, app, Some(streams)),
71            _ => return Err(syntax("a=sctpmap", value)),
72        };
73        if !grammar::is_token(app) {
74            return Err(syntax("a=sctpmap", value));
75        }
76        Ok(SctpMap {
77            port: grammar::digits("a=sctpmap port", port)?,
78            app: app.to_owned(),
79            streams: streams
80                .map(|s| grammar::digits("a=sctpmap streams", s))
81                .transpose()?,
82        })
83    }
84}
85
86impl fmt::Display for SctpMap {
87    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
88        write!(f, "{} {}", self.port, self.app)?;
89        if let Some(streams) = self.streams {
90            write!(f, " {streams}")?;
91        }
92        Ok(())
93    }
94}