2017-05-06 11:49:30 +00:00
|
|
|
// Copyright (c) 2017 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
2017-07-20 19:03:15 +00:00
|
|
|
use try_from::TryFrom;
|
2018-05-04 19:19:40 +00:00
|
|
|
use std::net::IpAddr;
|
2017-05-06 11:49:30 +00:00
|
|
|
|
2018-05-14 14:12:56 +00:00
|
|
|
use minidom::Element;
|
2017-06-25 21:20:24 +00:00
|
|
|
use jid::Jid;
|
2017-05-06 11:49:30 +00:00
|
|
|
|
|
|
|
use error::Error;
|
|
|
|
|
|
|
|
use ns;
|
|
|
|
|
2017-06-13 23:50:57 +00:00
|
|
|
generate_attribute!(Type, "type", {
|
|
|
|
Assisted => "assisted",
|
|
|
|
Direct => "direct",
|
|
|
|
Proxy => "proxy",
|
|
|
|
Tunnel => "tunnel",
|
|
|
|
}, Default = Direct);
|
2017-05-06 11:49:30 +00:00
|
|
|
|
2017-06-25 20:38:58 +00:00
|
|
|
generate_attribute!(Mode, "mode", {
|
|
|
|
Tcp => "tcp",
|
|
|
|
Udp => "udp",
|
|
|
|
}, Default = Tcp);
|
|
|
|
|
2017-06-25 21:14:51 +00:00
|
|
|
generate_id!(CandidateId);
|
2017-06-25 20:38:58 +00:00
|
|
|
|
2017-06-25 21:14:51 +00:00
|
|
|
generate_id!(StreamId);
|
2017-06-25 20:38:58 +00:00
|
|
|
|
2018-05-28 14:42:35 +00:00
|
|
|
generate_element!(Candidate, "candidate", JINGLE_S5B,
|
|
|
|
attributes: [
|
2017-10-31 17:58:11 +00:00
|
|
|
cid: CandidateId = "cid" => required,
|
2018-05-04 19:19:40 +00:00
|
|
|
host: IpAddr = "host" => required,
|
2017-10-31 17:58:11 +00:00
|
|
|
jid: Jid = "jid" => required,
|
|
|
|
port: Option<u16> = "port" => optional,
|
|
|
|
priority: u32 = "priority" => required,
|
|
|
|
type_: Type = "type" => default,
|
|
|
|
]);
|
2017-05-06 11:49:30 +00:00
|
|
|
|
2018-05-04 17:11:03 +00:00
|
|
|
impl Candidate {
|
2018-05-04 19:19:40 +00:00
|
|
|
pub fn new(cid: CandidateId, host: IpAddr, jid: Jid, priority: u32) -> Candidate {
|
2018-05-04 17:11:03 +00:00
|
|
|
Candidate {
|
|
|
|
cid,
|
|
|
|
host,
|
|
|
|
jid,
|
|
|
|
priority,
|
|
|
|
port: Default::default(),
|
|
|
|
type_: Default::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with_port(mut self, port: u16) -> Candidate {
|
|
|
|
self.port = Some(port);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with_type(mut self, type_: Type) -> Candidate {
|
|
|
|
self.type_ = type_;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-06 11:49:30 +00:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub enum TransportPayload {
|
|
|
|
Activated(String),
|
|
|
|
Candidates(Vec<Candidate>),
|
|
|
|
CandidateError,
|
|
|
|
CandidateUsed(String),
|
|
|
|
ProxyError,
|
2017-05-06 12:54:16 +00:00
|
|
|
None,
|
2017-05-06 11:49:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct Transport {
|
2017-06-25 20:38:58 +00:00
|
|
|
pub sid: StreamId,
|
2017-05-06 11:49:30 +00:00
|
|
|
pub dstaddr: Option<String>,
|
|
|
|
pub mode: Mode,
|
|
|
|
pub payload: TransportPayload,
|
|
|
|
}
|
|
|
|
|
2018-05-04 17:11:03 +00:00
|
|
|
impl Transport {
|
|
|
|
pub fn new(sid: StreamId) -> Transport {
|
|
|
|
Transport {
|
|
|
|
sid,
|
|
|
|
dstaddr: None,
|
|
|
|
mode: Default::default(),
|
|
|
|
payload: TransportPayload::None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with_dstaddr(mut self, dstaddr: String) -> Transport {
|
|
|
|
self.dstaddr = Some(dstaddr);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with_mode(mut self, mode: Mode) -> Transport {
|
|
|
|
self.mode = mode;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with_payload(mut self, payload: TransportPayload) -> Transport {
|
|
|
|
self.payload = payload;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-23 22:31:33 +00:00
|
|
|
impl TryFrom<Element> for Transport {
|
2017-07-20 19:03:15 +00:00
|
|
|
type Err = Error;
|
2017-05-06 11:49:30 +00:00
|
|
|
|
2017-05-23 22:31:33 +00:00
|
|
|
fn try_from(elem: Element) -> Result<Transport, Error> {
|
2018-05-14 14:30:28 +00:00
|
|
|
check_self!(elem, "transport", JINGLE_S5B);
|
2018-05-14 23:47:12 +00:00
|
|
|
check_no_unknown_attributes!(elem, "transport", ["sid", "dstaddr", "mode"]);
|
2018-05-12 16:31:11 +00:00
|
|
|
let sid = get_attr!(elem, "sid", required);
|
|
|
|
let dstaddr = get_attr!(elem, "dstaddr", optional);
|
|
|
|
let mode = get_attr!(elem, "mode", default);
|
|
|
|
|
|
|
|
let mut payload = None;
|
|
|
|
for child in elem.children() {
|
|
|
|
payload = Some(if child.is("candidate", ns::JINGLE_S5B) {
|
|
|
|
let mut candidates = match payload {
|
|
|
|
Some(TransportPayload::Candidates(candidates)) => candidates,
|
|
|
|
Some(_) => return Err(Error::ParseError("Non-candidate child already present in JingleS5B transport element.")),
|
|
|
|
None => vec!(),
|
|
|
|
};
|
|
|
|
candidates.push(Candidate::try_from(child.clone())?);
|
|
|
|
TransportPayload::Candidates(candidates)
|
|
|
|
} else if child.is("activated", ns::JINGLE_S5B) {
|
|
|
|
if payload.is_some() {
|
|
|
|
return Err(Error::ParseError("Non-activated child already present in JingleS5B transport element."));
|
|
|
|
}
|
|
|
|
let cid = get_attr!(child, "cid", required);
|
|
|
|
TransportPayload::Activated(cid)
|
|
|
|
} else if child.is("candidate-error", ns::JINGLE_S5B) {
|
|
|
|
if payload.is_some() {
|
|
|
|
return Err(Error::ParseError("Non-candidate-error child already present in JingleS5B transport element."));
|
|
|
|
}
|
|
|
|
TransportPayload::CandidateError
|
|
|
|
} else if child.is("candidate-used", ns::JINGLE_S5B) {
|
|
|
|
if payload.is_some() {
|
|
|
|
return Err(Error::ParseError("Non-candidate-used child already present in JingleS5B transport element."));
|
|
|
|
}
|
|
|
|
let cid = get_attr!(child, "cid", required);
|
|
|
|
TransportPayload::CandidateUsed(cid)
|
|
|
|
} else if child.is("proxy-error", ns::JINGLE_S5B) {
|
|
|
|
if payload.is_some() {
|
|
|
|
return Err(Error::ParseError("Non-proxy-error child already present in JingleS5B transport element."));
|
|
|
|
}
|
|
|
|
TransportPayload::ProxyError
|
|
|
|
} else {
|
|
|
|
return Err(Error::ParseError("Unknown child in JingleS5B transport element."));
|
|
|
|
});
|
2017-05-06 11:49:30 +00:00
|
|
|
}
|
2018-05-12 16:31:11 +00:00
|
|
|
let payload = payload.unwrap_or(TransportPayload::None);
|
|
|
|
Ok(Transport {
|
|
|
|
sid: sid,
|
|
|
|
dstaddr: dstaddr,
|
|
|
|
mode: mode,
|
|
|
|
payload: payload,
|
|
|
|
})
|
2017-05-06 11:49:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-20 19:36:13 +00:00
|
|
|
impl From<Transport> for Element {
|
|
|
|
fn from(transport: Transport) -> Element {
|
2017-05-06 11:49:30 +00:00
|
|
|
Element::builder("transport")
|
|
|
|
.ns(ns::JINGLE_S5B)
|
2017-07-20 19:36:13 +00:00
|
|
|
.attr("sid", transport.sid)
|
|
|
|
.attr("dstaddr", transport.dstaddr)
|
|
|
|
.attr("mode", transport.mode)
|
|
|
|
.append(match transport.payload {
|
2017-07-25 23:04:20 +00:00
|
|
|
TransportPayload::Candidates(candidates) => {
|
|
|
|
candidates.into_iter()
|
2017-07-20 19:52:25 +00:00
|
|
|
.map(Element::from)
|
|
|
|
.collect::<Vec<_>>()
|
2017-05-06 11:49:30 +00:00
|
|
|
},
|
2017-05-23 22:31:33 +00:00
|
|
|
TransportPayload::Activated(cid) => {
|
2017-05-06 11:49:30 +00:00
|
|
|
vec!(Element::builder("activated")
|
|
|
|
.ns(ns::JINGLE_S5B)
|
2017-05-23 22:31:33 +00:00
|
|
|
.attr("cid", cid)
|
2017-05-06 11:49:30 +00:00
|
|
|
.build())
|
|
|
|
},
|
|
|
|
TransportPayload::CandidateError => {
|
|
|
|
vec!(Element::builder("candidate-error")
|
|
|
|
.ns(ns::JINGLE_S5B)
|
|
|
|
.build())
|
|
|
|
},
|
2017-07-25 23:04:20 +00:00
|
|
|
TransportPayload::CandidateUsed(cid) => {
|
2017-05-06 11:49:30 +00:00
|
|
|
vec!(Element::builder("candidate-used")
|
|
|
|
.ns(ns::JINGLE_S5B)
|
2017-05-28 00:47:12 +00:00
|
|
|
.attr("cid", cid)
|
2017-05-06 11:49:30 +00:00
|
|
|
.build())
|
|
|
|
},
|
|
|
|
TransportPayload::ProxyError => {
|
|
|
|
vec!(Element::builder("proxy-error")
|
|
|
|
.ns(ns::JINGLE_S5B)
|
|
|
|
.build())
|
|
|
|
},
|
2017-05-06 12:54:16 +00:00
|
|
|
TransportPayload::None => vec!(),
|
2017-05-06 11:49:30 +00:00
|
|
|
})
|
|
|
|
.build()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2018-05-14 14:11:22 +00:00
|
|
|
use std::str::FromStr;
|
2017-08-18 23:04:18 +00:00
|
|
|
use compare_elements::NamespaceAwareCompare;
|
2017-05-06 11:49:30 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_simple() {
|
2017-05-06 12:54:16 +00:00
|
|
|
let elem: Element = "<transport xmlns='urn:xmpp:jingle:transports:s5b:1' sid='coucou'/>".parse().unwrap();
|
2017-05-23 22:31:33 +00:00
|
|
|
let transport = Transport::try_from(elem).unwrap();
|
2017-06-25 20:38:58 +00:00
|
|
|
assert_eq!(transport.sid, StreamId(String::from("coucou")));
|
2017-05-06 11:49:30 +00:00
|
|
|
assert_eq!(transport.dstaddr, None);
|
|
|
|
assert_eq!(transport.mode, Mode::Tcp);
|
|
|
|
match transport.payload {
|
2017-05-06 12:54:16 +00:00
|
|
|
TransportPayload::None => (),
|
2017-05-06 11:49:30 +00:00
|
|
|
_ => panic!("Wrong element inside transport!"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_serialise_activated() {
|
|
|
|
let elem: Element = "<transport xmlns='urn:xmpp:jingle:transports:s5b:1' sid='coucou'><activated cid='coucou'/></transport>".parse().unwrap();
|
|
|
|
let transport = Transport {
|
2017-06-25 20:38:58 +00:00
|
|
|
sid: StreamId(String::from("coucou")),
|
2017-05-06 11:49:30 +00:00
|
|
|
dstaddr: None,
|
|
|
|
mode: Mode::Tcp,
|
|
|
|
payload: TransportPayload::Activated(String::from("coucou")),
|
|
|
|
};
|
2017-05-23 22:31:33 +00:00
|
|
|
let elem2: Element = transport.into();
|
2017-08-18 23:04:18 +00:00
|
|
|
assert!(elem.compare_to(&elem2));
|
2017-05-06 11:49:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_serialise_candidate() {
|
2018-05-04 19:19:40 +00:00
|
|
|
let elem: Element = "<transport xmlns='urn:xmpp:jingle:transports:s5b:1' sid='coucou'><candidate cid='coucou' host='127.0.0.1' jid='coucou@coucou' priority='0'/></transport>".parse().unwrap();
|
2017-05-06 11:49:30 +00:00
|
|
|
let transport = Transport {
|
2017-06-25 20:38:58 +00:00
|
|
|
sid: StreamId(String::from("coucou")),
|
2017-05-06 11:49:30 +00:00
|
|
|
dstaddr: None,
|
|
|
|
mode: Mode::Tcp,
|
|
|
|
payload: TransportPayload::Candidates(vec!(Candidate {
|
2017-06-25 20:38:58 +00:00
|
|
|
cid: CandidateId(String::from("coucou")),
|
2018-05-04 19:19:40 +00:00
|
|
|
host: IpAddr::from_str("127.0.0.1").unwrap(),
|
2017-06-25 21:20:24 +00:00
|
|
|
jid: Jid::from_str("coucou@coucou").unwrap(),
|
2017-05-06 11:49:30 +00:00
|
|
|
port: None,
|
|
|
|
priority: 0u32,
|
|
|
|
type_: Type::Direct,
|
|
|
|
})),
|
|
|
|
};
|
2017-05-23 22:31:33 +00:00
|
|
|
let elem2: Element = transport.into();
|
2017-08-18 23:04:18 +00:00
|
|
|
assert!(elem.compare_to(&elem2));
|
2017-05-06 11:49:30 +00:00
|
|
|
}
|
|
|
|
}
|