1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
// 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/.
use jid::Jid;
use std::net::IpAddr;
use crate::core::{FromXml, IntoXml};
use crate::ns::JINGLE_S5B;
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
);
/// A candidate for a connection.
#[derive(FromXml, IntoXml, Debug, Clone, PartialEq)]
#[xml(namespace = JINGLE_S5B, name = "candidate")]
pub struct Candidate {
/// The identifier for this candidate.
#[xml(attribute)]
pub cid: CandidateId,
/// The host to connect to.
#[xml(attribute)]
pub host: IpAddr,
/// The JID to request at the given end.
#[xml(attribute)]
pub jid: Jid,
/// The port to connect to.
#[xml(attribute)]
pub port: Option<u16>,
/// The priority of this candidate, computed using this formula:
/// priority = (2^16)*(type preference) + (local preference)
#[xml(attribute)]
pub priority: u32,
/// The type of the connection being proposed by this candidate.
#[xml(attribute(name = "type", default))]
pub type_: Type,
}
impl Candidate {
/// Creates a new candidate with the given parameters.
pub fn new(cid: CandidateId, host: IpAddr, jid: Jid, priority: u32) -> Candidate {
Candidate {
cid,
host,
jid,
priority,
port: Default::default(),
type_: Default::default(),
}
}
/// Sets the port of this candidate.
pub fn with_port(mut self, port: u16) -> Candidate {
self.port = Some(port);
self
}
/// Sets the type of this candidate.
pub fn with_type(mut self, type_: Type) -> Candidate {
self.type_ = type_;
self
}
}
/// The state of a transport, after candidate selection.
#[derive(FromXml, IntoXml, Debug, Clone, PartialEq)]
#[xml(namespace = JINGLE_S5B)]
pub enum TransportState {
/// The responder informs the initiator that the bytestream pointed by this
/// candidate has been activated.
#[xml(name = "activated")]
Activated(#[xml(attribute = "cid")] CandidateId),
/// Both parties failed to use a candidate, they should fallback to another
/// transport.
#[xml(name = "candidate-error")]
CandidateError,
/// The candidate pointed here should be used by both parties.
#[xml(name = "candidate-used")]
CandidateUsed(#[xml(attribute = "cid")] CandidateId),
/// This entity can’t connect to the SOCKS5 proxy.
#[xml(name = "proxy-error")]
ProxyError,
}
/// Describes a Jingle transport using a direct or proxied connection.
#[derive(FromXml, IntoXml, Debug, Clone, PartialEq)]
#[xml(namespace = JINGLE_S5B, name = "transport")]
pub struct Transport {
/// The stream identifier for this transport.
#[xml(attribute)]
pub sid: StreamId,
/// The destination address.
#[xml(attribute)]
pub dstaddr: Option<String>,
/// The mode to be used for the transfer.
#[xml(attribute(default))]
pub mode: Mode,
/// Offered candidates
///
/// This must be empty if `state` is not None.
#[xml(children)]
pub candidates: Vec<Candidate>,
/// A substate in relation to this transport.
///
/// This must be None if there is more than zero `candidates`.
#[xml(child)]
pub state: Option<TransportState>,
}
impl Transport {
/// Creates a new transport element.
pub fn new(sid: StreamId) -> Transport {
Transport {
sid,
dstaddr: None,
mode: Default::default(),
candidates: Vec::new(),
state: None,
}
}
/// Sets the destination address of this transport.
pub fn with_dstaddr(mut self, dstaddr: String) -> Transport {
self.dstaddr = Some(dstaddr);
self
}
/// Sets the mode of this transport.
pub fn with_mode(mut self, mode: Mode) -> Transport {
self.mode = mode;
self
}
/// Sets the payload of this transport.
///
/// This clears the `candidates` field.
pub fn with_state(mut self, state: TransportState) -> Transport {
self.state = Some(state);
self.candidates = Vec::new();
self
}
/// Set the candidates of this transport.
///
/// This clears the `state` field.
pub fn with_candidates(mut self, candidates: Vec<Candidate>) -> Transport {
self.candidates = candidates;
self.state = None;
self
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::Element;
use std::str::FromStr;
#[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, 56);
assert_size!(TransportState, 16);
assert_size!(Transport, 56);
}
#[cfg(target_pointer_width = "64")]
#[test]
fn test_size() {
assert_size!(Type, 1);
assert_size!(Mode, 1);
assert_size!(CandidateId, 24);
assert_size!(StreamId, 24);
assert_size!(Candidate, 88);
assert_size!(TransportState, 32);
assert_size!(Transport, 112);
}
#[test]
fn test_simple() {
let elem: Element = "<transport xmlns='urn:xmpp:jingle:transports:s5b:1' sid='coucou'/>"
.parse()
.unwrap();
let transport = Transport::try_from(elem).unwrap();
assert_eq!(transport.sid, StreamId(String::from("coucou")));
assert_eq!(transport.dstaddr, None);
assert_eq!(transport.mode, Mode::Tcp);
match transport.state {
None => (),
_ => 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 {
sid: StreamId(String::from("coucou")),
dstaddr: None,
mode: Mode::Tcp,
candidates: Vec::new(),
state: Some(TransportState::Activated(CandidateId(String::from(
"coucou",
)))),
};
let elem2: Element = transport.into();
assert_eq!(elem, elem2);
}
#[test]
fn test_serialise_candidate() {
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();
let transport = Transport {
sid: StreamId(String::from("coucou")),
dstaddr: None,
mode: Mode::Tcp,
candidates: vec![Candidate {
cid: CandidateId(String::from("coucou")),
host: IpAddr::from_str("127.0.0.1").unwrap(),
jid: Jid::new("coucou@coucou").unwrap(),
port: None,
priority: 0u32,
type_: Type::Direct,
}],
state: None,
};
let elem2: Element = transport.into();
assert_eq!(elem, elem2);
}
}