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/.
|
|
|
|
|
|
2019-01-13 11:39:51 +00:00
|
|
|
|
use crate::util::error::Error;
|
2018-12-18 14:27:30 +00:00
|
|
|
|
use crate::ns;
|
2018-12-18 14:32:05 +00:00
|
|
|
|
use jid::Jid;
|
|
|
|
|
use minidom::Element;
|
|
|
|
|
use std::net::IpAddr;
|
2019-04-12 08:58:42 +00:00
|
|
|
|
use std::convert::TryFrom;
|
2017-05-06 11:49:30 +00:00
|
|
|
|
|
2018-08-08 19:07:22 +00:00
|
|
|
|
generate_attribute!(
|
|
|
|
|
/// The type of the connection being proposed by this candidate.
|
|
|
|
|
Type, "type", {
|
|
|
|
|
/// Direct connection using NAT assisting technologies like NAT-PMP or
|
|
|
|
|
/// UPnP-IGD.
|
|
|
|
|
Assisted => "assisted",
|
|
|
|
|
|
|
|
|
|
/// Direct connection using the given interface.
|
|
|
|
|
Direct => "direct",
|
|
|
|
|
|
|
|
|
|
/// SOCKS5 relay.
|
|
|
|
|
Proxy => "proxy",
|
|
|
|
|
|
|
|
|
|
/// Tunnel protocol such as Teredo.
|
|
|
|
|
Tunnel => "tunnel",
|
|
|
|
|
}, Default = Direct
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
generate_attribute!(
|
|
|
|
|
/// Which mode to use for the connection.
|
|
|
|
|
Mode, "mode", {
|
|
|
|
|
/// Use TCP, which is the default.
|
|
|
|
|
Tcp => "tcp",
|
|
|
|
|
|
|
|
|
|
/// Use UDP.
|
|
|
|
|
Udp => "udp",
|
|
|
|
|
}, Default = Tcp
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
generate_id!(
|
|
|
|
|
/// An identifier for a candidate.
|
|
|
|
|
CandidateId
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
generate_id!(
|
|
|
|
|
/// An identifier for a stream.
|
|
|
|
|
StreamId
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
generate_element!(
|
|
|
|
|
/// A candidate for a connection.
|
|
|
|
|
Candidate, "candidate", JINGLE_S5B,
|
|
|
|
|
attributes: [
|
|
|
|
|
/// The identifier for this candidate.
|
2019-02-24 19:26:40 +00:00
|
|
|
|
cid: Required<CandidateId> = "cid",
|
2018-08-08 19:07:22 +00:00
|
|
|
|
|
|
|
|
|
/// The host to connect to.
|
2019-02-24 19:26:40 +00:00
|
|
|
|
host: Required<IpAddr> = "host",
|
2018-08-08 19:07:22 +00:00
|
|
|
|
|
|
|
|
|
/// The JID to request at the given end.
|
2019-02-24 19:26:40 +00:00
|
|
|
|
jid: Required<Jid> = "jid",
|
2018-08-08 19:07:22 +00:00
|
|
|
|
|
|
|
|
|
/// The port to connect to.
|
2019-02-24 19:26:40 +00:00
|
|
|
|
port: Option<u16> = "port",
|
2018-08-08 19:07:22 +00:00
|
|
|
|
|
|
|
|
|
/// The priority of this candidate, computed using this formula:
|
|
|
|
|
/// priority = (2^16)*(type preference) + (local preference)
|
2019-02-24 19:26:40 +00:00
|
|
|
|
priority: Required<u32> = "priority",
|
2018-08-08 19:07:22 +00:00
|
|
|
|
|
|
|
|
|
/// The type of the connection being proposed by this candidate.
|
2019-02-24 19:26:40 +00:00
|
|
|
|
type_: Default<Type> = "type",
|
2018-08-08 19:07:22 +00:00
|
|
|
|
]
|
|
|
|
|
);
|
2017-05-06 11:49:30 +00:00
|
|
|
|
|
2018-05-04 17:11:03 +00:00
|
|
|
|
impl Candidate {
|
2018-08-08 19:07:22 +00:00
|
|
|
|
/// Creates a new candidate with the given parameters.
|
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(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-08 19:07:22 +00:00
|
|
|
|
/// Sets the port of this candidate.
|
2018-05-04 17:11:03 +00:00
|
|
|
|
pub fn with_port(mut self, port: u16) -> Candidate {
|
|
|
|
|
self.port = Some(port);
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-08 19:07:22 +00:00
|
|
|
|
/// Sets the type of this candidate.
|
2018-05-04 17:11:03 +00:00
|
|
|
|
pub fn with_type(mut self, type_: Type) -> Candidate {
|
|
|
|
|
self.type_ = type_;
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-08 19:07:22 +00:00
|
|
|
|
/// The payload of a transport.
|
2017-05-06 11:49:30 +00:00
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
|
pub enum TransportPayload {
|
2018-08-08 19:07:22 +00:00
|
|
|
|
/// The responder informs the initiator that the bytestream pointed by this
|
|
|
|
|
/// candidate has been activated.
|
2018-05-28 15:27:21 +00:00
|
|
|
|
Activated(CandidateId),
|
2018-08-08 19:07:22 +00:00
|
|
|
|
|
|
|
|
|
/// A list of suggested candidates.
|
2017-05-06 11:49:30 +00:00
|
|
|
|
Candidates(Vec<Candidate>),
|
2018-08-08 19:07:22 +00:00
|
|
|
|
|
|
|
|
|
/// Both parties failed to use a candidate, they should fallback to another
|
|
|
|
|
/// transport.
|
2017-05-06 11:49:30 +00:00
|
|
|
|
CandidateError,
|
2018-08-08 19:07:22 +00:00
|
|
|
|
|
|
|
|
|
/// The candidate pointed here should be used by both parties.
|
2018-05-28 15:27:21 +00:00
|
|
|
|
CandidateUsed(CandidateId),
|
2018-08-08 19:07:22 +00:00
|
|
|
|
|
|
|
|
|
/// This entity can’t connect to the SOCKS5 proxy.
|
2017-05-06 11:49:30 +00:00
|
|
|
|
ProxyError,
|
2018-08-08 19:07:22 +00:00
|
|
|
|
|
|
|
|
|
/// XXX: Invalid, should not be found in the wild.
|
2017-05-06 12:54:16 +00:00
|
|
|
|
None,
|
2017-05-06 11:49:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-08-08 19:07:22 +00:00
|
|
|
|
/// Describes a Jingle transport using a direct or proxied connection.
|
2017-05-06 11:49:30 +00:00
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
|
pub struct Transport {
|
2018-08-08 19:07:22 +00:00
|
|
|
|
/// The stream identifier for this transport.
|
2017-06-25 20:38:58 +00:00
|
|
|
|
pub sid: StreamId,
|
2018-08-08 19:07:22 +00:00
|
|
|
|
|
|
|
|
|
/// The destination address.
|
2017-05-06 11:49:30 +00:00
|
|
|
|
pub dstaddr: Option<String>,
|
2018-08-08 19:07:22 +00:00
|
|
|
|
|
|
|
|
|
/// The mode to be used for the transfer.
|
2017-05-06 11:49:30 +00:00
|
|
|
|
pub mode: Mode,
|
2018-08-08 19:07:22 +00:00
|
|
|
|
|
|
|
|
|
/// The payload of this transport.
|
2017-05-06 11:49:30 +00:00
|
|
|
|
pub payload: TransportPayload,
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-04 17:11:03 +00:00
|
|
|
|
impl Transport {
|
2018-08-08 19:07:22 +00:00
|
|
|
|
/// Creates a new transport element.
|
2018-05-04 17:11:03 +00:00
|
|
|
|
pub fn new(sid: StreamId) -> Transport {
|
|
|
|
|
Transport {
|
|
|
|
|
sid,
|
|
|
|
|
dstaddr: None,
|
|
|
|
|
mode: Default::default(),
|
|
|
|
|
payload: TransportPayload::None,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-08 19:07:22 +00:00
|
|
|
|
/// Sets the destination address of this transport.
|
2018-05-04 17:11:03 +00:00
|
|
|
|
pub fn with_dstaddr(mut self, dstaddr: String) -> Transport {
|
|
|
|
|
self.dstaddr = Some(dstaddr);
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-08 19:07:22 +00:00
|
|
|
|
/// Sets the mode of this transport.
|
2018-05-04 17:11:03 +00:00
|
|
|
|
pub fn with_mode(mut self, mode: Mode) -> Transport {
|
|
|
|
|
self.mode = mode;
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-08 19:07:22 +00:00
|
|
|
|
/// Sets the payload of this transport.
|
2018-05-04 17:11:03 +00:00
|
|
|
|
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 {
|
2019-04-12 08:58:42 +00:00
|
|
|
|
type Error = 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"]);
|
2019-02-24 19:48:19 +00:00
|
|
|
|
let sid = get_attr!(elem, "sid", Required);
|
|
|
|
|
let dstaddr = get_attr!(elem, "dstaddr", Option);
|
|
|
|
|
let mode = get_attr!(elem, "mode", Default);
|
2018-05-12 16:31:11 +00:00
|
|
|
|
|
|
|
|
|
let mut payload = None;
|
|
|
|
|
for child in elem.children() {
|
|
|
|
|
payload = Some(if child.is("candidate", ns::JINGLE_S5B) {
|
2018-12-18 14:32:05 +00:00
|
|
|
|
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![],
|
|
|
|
|
};
|
2018-05-12 16:31:11 +00:00
|
|
|
|
candidates.push(Candidate::try_from(child.clone())?);
|
|
|
|
|
TransportPayload::Candidates(candidates)
|
|
|
|
|
} else if child.is("activated", ns::JINGLE_S5B) {
|
|
|
|
|
if payload.is_some() {
|
2018-12-18 14:32:05 +00:00
|
|
|
|
return Err(Error::ParseError(
|
|
|
|
|
"Non-activated child already present in JingleS5B transport element.",
|
|
|
|
|
));
|
2018-05-12 16:31:11 +00:00
|
|
|
|
}
|
2019-02-24 19:48:19 +00:00
|
|
|
|
let cid = get_attr!(child, "cid", Required);
|
2018-05-12 16:31:11 +00:00
|
|
|
|
TransportPayload::Activated(cid)
|
|
|
|
|
} else if child.is("candidate-error", ns::JINGLE_S5B) {
|
|
|
|
|
if payload.is_some() {
|
2018-12-18 14:32:05 +00:00
|
|
|
|
return Err(Error::ParseError(
|
|
|
|
|
"Non-candidate-error child already present in JingleS5B transport element.",
|
|
|
|
|
));
|
2018-05-12 16:31:11 +00:00
|
|
|
|
}
|
|
|
|
|
TransportPayload::CandidateError
|
|
|
|
|
} else if child.is("candidate-used", ns::JINGLE_S5B) {
|
|
|
|
|
if payload.is_some() {
|
2018-12-18 14:32:05 +00:00
|
|
|
|
return Err(Error::ParseError(
|
|
|
|
|
"Non-candidate-used child already present in JingleS5B transport element.",
|
|
|
|
|
));
|
2018-05-12 16:31:11 +00:00
|
|
|
|
}
|
2019-02-24 19:48:19 +00:00
|
|
|
|
let cid = get_attr!(child, "cid", Required);
|
2018-05-12 16:31:11 +00:00
|
|
|
|
TransportPayload::CandidateUsed(cid)
|
|
|
|
|
} else if child.is("proxy-error", ns::JINGLE_S5B) {
|
|
|
|
|
if payload.is_some() {
|
2018-12-18 14:32:05 +00:00
|
|
|
|
return Err(Error::ParseError(
|
|
|
|
|
"Non-proxy-error child already present in JingleS5B transport element.",
|
|
|
|
|
));
|
2018-05-12 16:31:11 +00:00
|
|
|
|
}
|
|
|
|
|
TransportPayload::ProxyError
|
|
|
|
|
} else {
|
2018-12-18 14:32:05 +00:00
|
|
|
|
return Err(Error::ParseError(
|
|
|
|
|
"Unknown child in JingleS5B transport element.",
|
|
|
|
|
));
|
2018-05-12 16:31:11 +00:00
|
|
|
|
});
|
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 {
|
2019-02-21 20:00:58 +00:00
|
|
|
|
sid,
|
|
|
|
|
dstaddr,
|
|
|
|
|
mode,
|
|
|
|
|
payload,
|
2018-05-12 16:31:11 +00:00
|
|
|
|
})
|
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")
|
2018-12-18 14:32:05 +00:00
|
|
|
|
.ns(ns::JINGLE_S5B)
|
|
|
|
|
.attr("sid", transport.sid)
|
|
|
|
|
.attr("dstaddr", transport.dstaddr)
|
|
|
|
|
.attr("mode", transport.mode)
|
|
|
|
|
.append(match transport.payload {
|
|
|
|
|
TransportPayload::Candidates(candidates) => candidates
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(Element::from)
|
|
|
|
|
.collect::<Vec<_>>(),
|
|
|
|
|
TransportPayload::Activated(cid) => vec![Element::builder("activated")
|
|
|
|
|
.ns(ns::JINGLE_S5B)
|
|
|
|
|
.attr("cid", cid)
|
|
|
|
|
.build()],
|
|
|
|
|
TransportPayload::CandidateError => vec![Element::builder("candidate-error")
|
|
|
|
|
.ns(ns::JINGLE_S5B)
|
|
|
|
|
.build()],
|
|
|
|
|
TransportPayload::CandidateUsed(cid) => vec![Element::builder("candidate-used")
|
|
|
|
|
.ns(ns::JINGLE_S5B)
|
|
|
|
|
.attr("cid", cid)
|
|
|
|
|
.build()],
|
|
|
|
|
TransportPayload::ProxyError => {
|
|
|
|
|
vec![Element::builder("proxy-error").ns(ns::JINGLE_S5B).build()]
|
|
|
|
|
}
|
|
|
|
|
TransportPayload::None => vec![],
|
|
|
|
|
})
|
|
|
|
|
.build()
|
2017-05-06 11:49:30 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use super::*;
|
2019-01-13 11:39:51 +00:00
|
|
|
|
use crate::util::compare_elements::NamespaceAwareCompare;
|
2018-12-18 14:32:05 +00:00
|
|
|
|
use std::str::FromStr;
|
2017-05-06 11:49:30 +00:00
|
|
|
|
|
2018-10-28 12:10:48 +00:00
|
|
|
|
#[cfg(target_pointer_width = "32")]
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_size() {
|
|
|
|
|
assert_size!(Type, 1);
|
|
|
|
|
assert_size!(Mode, 1);
|
|
|
|
|
assert_size!(CandidateId, 12);
|
|
|
|
|
assert_size!(StreamId, 12);
|
|
|
|
|
assert_size!(Candidate, 80);
|
|
|
|
|
assert_size!(TransportPayload, 16);
|
|
|
|
|
assert_size!(Transport, 44);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(target_pointer_width = "64")]
|
2018-10-26 12:26:16 +00:00
|
|
|
|
#[test]
|
|
|
|
|
fn test_size() {
|
|
|
|
|
assert_size!(Type, 1);
|
|
|
|
|
assert_size!(Mode, 1);
|
|
|
|
|
assert_size!(CandidateId, 24);
|
|
|
|
|
assert_size!(StreamId, 24);
|
|
|
|
|
assert_size!(Candidate, 128);
|
|
|
|
|
assert_size!(TransportPayload, 32);
|
|
|
|
|
assert_size!(Transport, 88);
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-06 11:49:30 +00:00
|
|
|
|
#[test]
|
|
|
|
|
fn test_simple() {
|
2018-12-18 14:32:05 +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,
|
2018-05-28 15:27:21 +00:00
|
|
|
|
payload: TransportPayload::Activated(CandidateId(String::from("coucou"))),
|
2017-05-06 11:49:30 +00:00
|
|
|
|
};
|
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,
|
2018-12-18 14:32:05 +00:00
|
|
|
|
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,
|
2018-12-18 14:32:05 +00:00
|
|
|
|
}]),
|
2017-05-06 11:49:30 +00:00
|
|
|
|
};
|
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
|
|
|
|
}
|
|
|
|
|
}
|