From 642dac9aa0b90dd2f17df5dddd0e7914a7d306d3 Mon Sep 17 00:00:00 2001 From: hrxi Date: Sat, 20 Jul 2019 23:14:40 +0200 Subject: Add support for Jingle SOCKS5 bytestreams (XEP-0260) --- .../module/xep/0260_jingle_socks5_bytestreams.vala | 505 +++++++++++++++++++++ 1 file changed, 505 insertions(+) create mode 100644 xmpp-vala/src/module/xep/0260_jingle_socks5_bytestreams.vala (limited to 'xmpp-vala/src/module/xep/0260_jingle_socks5_bytestreams.vala') diff --git a/xmpp-vala/src/module/xep/0260_jingle_socks5_bytestreams.vala b/xmpp-vala/src/module/xep/0260_jingle_socks5_bytestreams.vala new file mode 100644 index 00000000..abe5e0a9 --- /dev/null +++ b/xmpp-vala/src/module/xep/0260_jingle_socks5_bytestreams.vala @@ -0,0 +1,505 @@ +using Gee; +using Xmpp; +using Xmpp.Xep; + +namespace Xmpp.Xep.JingleSocks5Bytestreams { + +private const string NS_URI = "urn:xmpp:jingle:transports:s5b:1"; + +public class Module : Jingle.Transport, XmppStreamModule { + public static Xmpp.ModuleIdentity IDENTITY = new Xmpp.ModuleIdentity(NS_URI, "0260_jingle_socks5_bytestreams"); + + public override void attach(XmppStream stream) { + stream.get_module(Jingle.Module.IDENTITY).register_transport(this); + stream.get_module(ServiceDiscovery.Module.IDENTITY).add_feature(stream, NS_URI); + } + public override void detach(XmppStream stream) { } + + public override string get_ns() { return NS_URI; } + public override string get_id() { return IDENTITY.id; } + + public bool is_transport_available(XmppStream stream, Jid full_jid) { + bool? result = stream.get_flag(ServiceDiscovery.Flag.IDENTITY).has_entity_feature(full_jid, NS_URI); + return result != null && result; + } + + public string transport_ns_uri() { + return NS_URI; + } + public Jingle.TransportType transport_type() { + return Jingle.TransportType.STREAMING; + } + public int transport_priority() { + return 1; + } + private Gee.List get_local_candidates(XmppStream stream) { + Gee.List result = new ArrayList(); + int i = 1 << 15; + foreach (Socks5Bytestreams.Proxy proxy in stream.get_module(Socks5Bytestreams.Module.IDENTITY).get_proxies(stream)) { + result.add(new Candidate.proxy(random_uuid(), proxy, i)); + i -= 1; + } + return result; + } + public Jingle.TransportParameters create_transport_parameters(XmppStream stream, Jid local_full_jid, Jid peer_full_jid) { + Parameters result = new Parameters.create(local_full_jid, peer_full_jid, random_uuid()); + result.local_candidates.add_all(get_local_candidates(stream)); + return result; + } + public Jingle.TransportParameters parse_transport_parameters(XmppStream stream, Jid local_full_jid, Jid peer_full_jid, StanzaNode transport) throws Jingle.IqError { + Parameters result = Parameters.parse(local_full_jid, peer_full_jid, transport); + result.local_candidates.add_all(get_local_candidates(stream)); + return result; + } +} + +public enum CandidateType { + ASSISTED, + DIRECT, + PROXY, + TUNNEL; + + public static CandidateType parse(string type) throws Jingle.IqError { + switch (type) { + case "assisted": return CandidateType.ASSISTED; + case "direct": return CandidateType.DIRECT; + case "proxy": return CandidateType.PROXY; + case "tunnel": return CandidateType.TUNNEL; + } + throw new Jingle.IqError.BAD_REQUEST(@"unknown candidate type $(type)"); + } + + public string to_string() { + switch (this) { + case ASSISTED: return "assisted"; + case DIRECT: return "direct"; + case PROXY: return "proxy"; + case TUNNEL: return "tunnel"; + } + assert_not_reached(); + } + + private int type_preference_impl() { + switch (this) { + case ASSISTED: return 120; + case DIRECT: return 126; + case PROXY: return 10; + case TUNNEL: return 110; + } + assert_not_reached(); + } + public int type_preference() { + return type_preference_impl() << 16; + } +} + +public class Candidate : Socks5Bytestreams.Proxy { + public string cid { get; private set; } + public int priority { get; private set; } + public CandidateType type_ { get; private set; } + + private Candidate(string cid, string host, Jid jid, int port, int priority, CandidateType type) { + base(host, jid, port); + this.cid = cid; + this.priority = priority; + this.type_ = type; + } + + public Candidate.build(string cid, string host, Jid jid, int port, int local_priority, CandidateType type) { + this(cid, host, jid, port, type.type_preference() + local_priority, type); + } + public Candidate.proxy(string cid, Socks5Bytestreams.Proxy proxy, int local_priority) { + this.build(cid, proxy.host, proxy.jid, proxy.port, local_priority, CandidateType.PROXY); + } + + public static Candidate parse(StanzaNode candidate) throws Jingle.IqError { + string? cid = candidate.get_attribute("cid"); + string? host = candidate.get_attribute("host"); + string? jid_str = candidate.get_attribute("jid"); + Jid? jid = jid_str != null ? Jid.parse(jid_str) : null; + int port = candidate.get_attribute("port") != null ? candidate.get_attribute_int("port") : 1080; + int priority = candidate.get_attribute_int("priority"); + string? type_str = candidate.get_attribute("type"); + CandidateType type = type_str != null ? CandidateType.parse(type_str) : CandidateType.DIRECT; + + if (cid == null || host == null || jid == null || port <= 0 || priority <= 0) { + throw new Jingle.IqError.BAD_REQUEST("missing or invalid cid, host, jid or port"); + } + + return new Candidate(cid, host, jid, port, priority, type); + } + public StanzaNode to_xml() { + return new StanzaNode.build("candidate", NS_URI) + .put_attribute("cid", cid) + .put_attribute("host", host) + .put_attribute("jid", jid.to_string()) + .put_attribute("port", port.to_string()) + .put_attribute("priority", priority.to_string()) + .put_attribute("type", type_.to_string()); + } +} + +bool bytes_equal(uint8[] a, uint8[] b) { + if (a.length != b.length) { + return false; + } + for (int i = 0; i < a.length; i++) { + if (a[i] != b[i]) { + return false; + } + } + return true; +} + +class Parameters : Jingle.TransportParameters, Object { + public Jingle.Role role { get; private set; } + public string sid { get; private set; } + public string remote_dstaddr { get; private set; } + public string local_dstaddr { get; private set; } + public Gee.List local_candidates = new ArrayList(); + public Gee.List remote_candidates = new ArrayList(); + + Jid peer_full_jid; + + bool remote_sent_selected_candidate = false; + Candidate? remote_selected_candidate = null; + bool local_determined_selected_candidate = false; + Candidate? local_selected_candidate = null; + SocketConnection? local_selected_candidate_conn = null; + weak Jingle.Session? session = null; + XmppStream? hack = null; + + string? waiting_for_activation_cid = null; + SourceFunc waiting_for_activation_callback; + + private static string calculate_dstaddr(string sid, Jid first_jid, Jid second_jid) { + string hashed = sid + first_jid.to_string() + second_jid.to_string(); + return Checksum.compute_for_string(ChecksumType.SHA1, hashed); + } + private Parameters(Jingle.Role role, string sid, Jid local_full_jid, Jid peer_full_jid, string? remote_dstaddr) { + this.role = role; + this.sid = sid; + this.local_dstaddr = calculate_dstaddr(sid, local_full_jid, peer_full_jid); + this.remote_dstaddr = remote_dstaddr ?? calculate_dstaddr(sid, peer_full_jid, local_full_jid); + + this.peer_full_jid = peer_full_jid; + } + public Parameters.create(Jid local_full_jid, Jid peer_full_jid, string sid) { + this(Jingle.Role.INITIATOR, sid, local_full_jid, peer_full_jid, null); + } + public static Parameters parse(Jid local_full_jid, Jid peer_full_jid, StanzaNode transport) throws Jingle.IqError { + string? dstaddr = transport.get_attribute("dstaddr"); + string? mode = transport.get_attribute("mode"); + string? sid = transport.get_attribute("sid"); + if (mode != null && mode != "tcp") { + throw new Jingle.IqError.BAD_REQUEST(@"unknown transport method $(mode)"); + } + if (dstaddr != null && dstaddr.length > 255) { + throw new Jingle.IqError.BAD_REQUEST("too long dstaddr"); + } + Parameters result = new Parameters(Jingle.Role.RESPONDER, sid, local_full_jid, peer_full_jid, dstaddr); + //result.remote_candidates.add(new Candidate("b", "0.0.0.0", new Jid("a@b/c"), 1234, 2000000000, CandidateType.PROXY)); + foreach (StanzaNode candidate in transport.get_subnodes("candidate", NS_URI)) { + result.remote_candidates.add(Candidate.parse(candidate)); + } + return result; + } + public string transport_ns_uri() { + return NS_URI; + } + public StanzaNode to_transport_stanza_node() { + StanzaNode transport = new StanzaNode.build("transport", NS_URI) + .add_self_xmlns() + .put_attribute("dstaddr", local_dstaddr); + + if (role == Jingle.Role.INITIATOR) { + // Must not be included by the responder according to XEP-0260. + transport.put_attribute("mode", "tcp"); + } + + transport.put_attribute("sid", sid); + foreach (Candidate candidate in local_candidates) { + transport.put_node(candidate.to_xml()); + } + return transport; + } + public void on_transport_accept(StanzaNode transport) throws Jingle.IqError { + throw new Jingle.IqError.BAD_REQUEST("blurb"); + } + public void on_transport_info(StanzaNode transport) throws Jingle.IqError { + StanzaNode? candidate_error = transport.get_subnode("candidate-error", NS_URI); + StanzaNode? candidate_used = transport.get_subnode("candidate-used", NS_URI); + StanzaNode? activated = transport.get_subnode("activated", NS_URI); + int num_children = 0; + if (candidate_error != null) { num_children += 1; } + if (candidate_used != null) { num_children += 1; } + if (activated != null) { num_children += 1; } + if (num_children == 0) { + throw new Jingle.IqError.UNSUPPORTED_INFO("unknown transport-info"); + } else if (num_children > 1) { + throw new Jingle.IqError.BAD_REQUEST("transport-info with more than one child"); + } + if (candidate_error != null) { + handle_remote_candidate(null); + } + if (candidate_used != null) { + string? cid = candidate_used.get_attribute("cid"); + if (cid == null) { + throw new Jingle.IqError.BAD_REQUEST("missing cid"); + } + handle_remote_candidate(cid); + } + if (activated != null) { + string? cid = activated.get_attribute("cid"); + if (cid == null) { + throw new Jingle.IqError.BAD_REQUEST("missing cid"); + } + handle_activated(cid); + } + } + private void handle_remote_candidate(string? cid) throws Jingle.IqError { + if (remote_sent_selected_candidate) { + throw new Jingle.IqError.BAD_REQUEST("remote candidate already specified"); + } + Candidate? candidate = null; + if (cid != null) { + foreach (Candidate c in local_candidates) { + if (c.cid == cid) { + candidate = c; + break; + } + } + if (candidate == null) { + throw new Jingle.IqError.BAD_REQUEST("unknown cid"); + } + } + remote_sent_selected_candidate = true; + remote_selected_candidate = candidate; + try_completing_negotiation(); + } + private void handle_activated(string cid) throws Jingle.IqError { + if (waiting_for_activation_cid == null || cid != waiting_for_activation_cid) { + throw new Jingle.IqError.BAD_REQUEST("unexpected proxy activation message"); + } + Idle.add((owned)waiting_for_activation_callback); + waiting_for_activation_cid = null; + } + private void try_completing_negotiation() { + if (!remote_sent_selected_candidate || !local_determined_selected_candidate) { + return; + } + + Candidate? remote = remote_selected_candidate; + Candidate? local = local_selected_candidate; + + int num_candidates = 0; + if (remote != null) { num_candidates += 1; } + if (local != null) { num_candidates += 1; } + + if (num_candidates == 0) { + // Notify Jingle of the failed transport. + session.set_transport_connection(hack, null); + return; + } + + bool remote_wins; + if (num_candidates == 1) { + remote_wins = remote != null; + } else { + if (local.priority < remote.priority) { + remote_wins = true; + } else if (local.priority > remote.priority) { + remote_wins = false; + } else { + // equal priority -> XEP-0260 says that the initiator wins + remote_wins = role != Jingle.Role.INITIATOR; + } + } + + if (!remote_wins) { + if (local_selected_candidate.type_ != CandidateType.PROXY) { + Jingle.Session? strong = session; + if (strong == null) { + return; + } + strong.set_transport_connection(hack, local_selected_candidate_conn); + } else { + wait_for_remote_activation.begin(local_selected_candidate, local_selected_candidate_conn); + } + } else { + connect_to_local_candidate.begin(remote_selected_candidate); + } + } + public async void wait_for_remote_activation(Candidate candidate, SocketConnection conn) { + waiting_for_activation_cid = candidate.cid; + waiting_for_activation_callback = wait_for_remote_activation.callback; + yield; + + Jingle.Session? strong = session; + if (strong == null) { + return; + } + strong.set_transport_connection(hack, conn); + } + public async void connect_to_local_candidate(Candidate candidate) { + try { + SocketConnection conn = yield connect_to_socks5(candidate, local_dstaddr); + + bool activation_error = false; + SourceFunc callback = connect_to_local_candidate.callback; + StanzaNode query = new StanzaNode.build("query", Socks5Bytestreams.NS_URI) + .add_self_xmlns() + .put_attribute("sid", sid) + .put_node(new StanzaNode.build("activate", Socks5Bytestreams.NS_URI) + .put_node(new StanzaNode.text(peer_full_jid.to_string())) + ); + Iq.Stanza iq = new Iq.Stanza.set(query) { to=candidate.jid }; + hack.get_module(Iq.Module.IDENTITY).send_iq(hack, iq, (stream, iq) => { + activation_error = iq.is_error(); + Idle.add((owned)callback); + }); + yield; + + if (activation_error) { + throw new IOError.PROXY_FAILED("activation iq error"); + } + + Jingle.Session? strong = session; + if (strong == null) { + return; + } + strong.send_transport_info(hack, new StanzaNode.build("transport", NS_URI) + .add_self_xmlns() + .put_attribute("sid", sid) + .put_node(new StanzaNode.build("activated", NS_URI) + .put_attribute("cid", candidate.cid) + ) + ); + + strong.set_transport_connection(hack, conn); + } catch (Error e) { + Jingle.Session? strong = session; + if (strong == null) { + return; + } + strong.send_transport_info(hack, new StanzaNode.build("transport", NS_URI) + .add_self_xmlns() + .put_attribute("sid", sid) + .put_node(new StanzaNode.build("proxy-error", NS_URI)) + ); + strong.set_transport_connection(hack, null); + } + } + public async SocketConnection connect_to_socks5(Candidate candidate, string dstaddr) throws Error { + SocketClient socket_client = new SocketClient() { timeout=3 }; + + string address = @"[$(candidate.host)]:$(candidate.port)"; + + size_t written; + size_t read; + uint8[] read_buffer = new uint8[1024]; + ByteArray write_buffer = new ByteArray(); + + SocketConnection conn = yield socket_client.connect_to_host_async(address, 0); + + // 05 SOCKS version 5 + // 01 number of authentication methods: 1 + // 00 nop authentication + yield conn.output_stream.write_all_async({0x05, 0x01, 0x00}, GLib.Priority.DEFAULT, null, out written); + + yield conn.input_stream.read_all_async(read_buffer[0:2], GLib.Priority.DEFAULT, null, out read); + // 05 SOCKS version 5 + // 01 success + if (read_buffer[0] != 0x05 || read_buffer[1] != 0x00) { + throw new IOError.PROXY_FAILED("wanted 05 00, got %02x %02x".printf(read_buffer[0], read_buffer[1])); + } + + // 05 SOCKS version 5 + // 01 connect + // 00 reserved + // 03 address type: domain name + // ?? length of the domain + // .. domain + // 00 port 0 (upper half) + // 00 port 0 (lower half) + write_buffer.append({0x05, 0x01, 0x00, 0x03}); + write_buffer.append({(uint8)dstaddr.length}); + write_buffer.append(dstaddr.data); + write_buffer.append({0x00, 0x00}); + yield conn.output_stream.write_all_async(write_buffer.data, GLib.Priority.DEFAULT, null, out written); + + yield conn.input_stream.read_all_async(read_buffer[0:write_buffer.len], GLib.Priority.DEFAULT, null, out read); + // 05 SOCKS version 5 + // 00 success + // 00 reserved + // 03 address type: domain name + // ?? length of the domain + // .. domain + // 00 port 0 (upper half) + // 00 port 0 (lower half) + if (read_buffer[0] != 0x05 || read_buffer[1] != 0x00 || read_buffer[3] != 0x03) { + throw new IOError.PROXY_FAILED("wanted 05 00 ?? 03, got %02x %02x %02x %02x".printf(read_buffer[0], read_buffer[1], read_buffer[2], read_buffer[3])); + } + if (read_buffer[4] != (uint8)dstaddr.length) { + throw new IOError.PROXY_FAILED("wanted %02x for length, got %02x".printf(dstaddr.length, read_buffer[4])); + } + if (!bytes_equal(read_buffer[5:5+dstaddr.length], dstaddr.data)) { + string repr = ((string)read_buffer[5:5+dstaddr.length]).make_valid().escape(); + throw new IOError.PROXY_FAILED(@"wanted dstaddr $(dstaddr), got $(repr)"); + } + if (read_buffer[5+dstaddr.length] != 0x00 || read_buffer[5+dstaddr.length+1] != 0x00) { + throw new IOError.PROXY_FAILED("wanted port 00 00, got %02x %02x".printf(read_buffer[5+dstaddr.length], read_buffer[5+dstaddr.length+1])); + } + + return conn; + } + public async void try_connecting_to_candidates(XmppStream stream, Jingle.Session session) throws Error { + remote_candidates.sort((c1, c2) => { + // sort from priorities from high to low + if (c1.priority < c2.priority) { return 1; } + if (c1.priority > c2.priority) { return -1; } + return 0; + }); + foreach (Candidate candidate in remote_candidates) { + if (remote_selected_candidate != null && remote_selected_candidate.priority > candidate.priority) { + // Don't try candidates with lower priority than the one the + // peer already selected. + break; + } + try { + SocketConnection conn = yield connect_to_socks5(candidate, remote_dstaddr); + + local_determined_selected_candidate = true; + local_selected_candidate = candidate; + local_selected_candidate_conn = conn; + session.send_transport_info(stream, new StanzaNode.build("transport", NS_URI) + .add_self_xmlns() + .put_attribute("sid", sid) + .put_node(new StanzaNode.build("candidate-used", NS_URI) + .put_attribute("cid", candidate.cid) + ) + ); + try_completing_negotiation(); + return; + } catch (Error e) { + // An error in the connection establishment isn't fatal, just + // try the next candidate or respond that none of the + // candidates work. + } + } + local_determined_selected_candidate = true; + local_selected_candidate = null; + session.send_transport_info(stream, new StanzaNode.build("transport", NS_URI) + .add_self_xmlns() + .put_attribute("sid", sid) + .put_node(new StanzaNode.build("candidate-error", NS_URI)) + ); + } + public void create_transport_connection(XmppStream stream, Jingle.Session session) { + this.session = session; + this.hack = stream; + try_connecting_to_candidates.begin(stream, session); + } +} + +} -- cgit v1.2.3-54-g00ecf From e1c98a0fd94d0536c231ce79c2af63d891bf4c16 Mon Sep 17 00:00:00 2001 From: hrxi Date: Tue, 6 Aug 2019 16:45:48 +0200 Subject: Forgot to add outgoing Jingle SOCKS5 transfer --- xmpp-vala/src/module/xep/0260_jingle_socks5_bytestreams.vala | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'xmpp-vala/src/module/xep/0260_jingle_socks5_bytestreams.vala') diff --git a/xmpp-vala/src/module/xep/0260_jingle_socks5_bytestreams.vala b/xmpp-vala/src/module/xep/0260_jingle_socks5_bytestreams.vala index abe5e0a9..78f47cfb 100644 --- a/xmpp-vala/src/module/xep/0260_jingle_socks5_bytestreams.vala +++ b/xmpp-vala/src/module/xep/0260_jingle_socks5_bytestreams.vala @@ -159,6 +159,7 @@ class Parameters : Jingle.TransportParameters, Object { public Gee.List local_candidates = new ArrayList(); public Gee.List remote_candidates = new ArrayList(); + Jid local_full_jid; Jid peer_full_jid; bool remote_sent_selected_candidate = false; @@ -182,6 +183,7 @@ class Parameters : Jingle.TransportParameters, Object { this.local_dstaddr = calculate_dstaddr(sid, local_full_jid, peer_full_jid); this.remote_dstaddr = remote_dstaddr ?? calculate_dstaddr(sid, peer_full_jid, local_full_jid); + this.local_full_jid = local_full_jid; this.peer_full_jid = peer_full_jid; } public Parameters.create(Jid local_full_jid, Jid peer_full_jid, string sid) { @@ -224,7 +226,12 @@ class Parameters : Jingle.TransportParameters, Object { return transport; } public void on_transport_accept(StanzaNode transport) throws Jingle.IqError { - throw new Jingle.IqError.BAD_REQUEST("blurb"); + Parameters other = Parameters.parse(local_full_jid, peer_full_jid, transport); + if (other.sid != sid) { + throw new Jingle.IqError.BAD_REQUEST("invalid sid"); + } + remote_candidates = other.remote_candidates; + remote_dstaddr = other.remote_dstaddr; } public void on_transport_info(StanzaNode transport) throws Jingle.IqError { StanzaNode? candidate_error = transport.get_subnode("candidate-error", NS_URI); -- cgit v1.2.3-54-g00ecf From 9a1e9864d6ea5bc967a69b2245ead55ba7c05812 Mon Sep 17 00:00:00 2001 From: hrxi Date: Mon, 5 Aug 2019 20:54:45 +0200 Subject: Fall back to IBB if S5B does not work out This mostly happens if connectivity to the candidates cannot be established. --- xmpp-vala/src/module/xep/0166_jingle.vala | 205 +++++++++++++++++---- .../module/xep/0260_jingle_socks5_bytestreams.vala | 22 ++- 2 files changed, 188 insertions(+), 39 deletions(-) (limited to 'xmpp-vala/src/module/xep/0260_jingle_socks5_bytestreams.vala') diff --git a/xmpp-vala/src/module/xep/0166_jingle.vala b/xmpp-vala/src/module/xep/0166_jingle.vala index 34816fcd..f5d112aa 100644 --- a/xmpp-vala/src/module/xep/0166_jingle.vala +++ b/xmpp-vala/src/module/xep/0166_jingle.vala @@ -139,12 +139,15 @@ public class Module : XmppStreamModule, Iq.Handler { } return transports[ns_uri]; } - public Transport? select_transport(XmppStream stream, TransportType type, Jid receiver_full_jid) { + public Transport? select_transport(XmppStream stream, TransportType type, Jid receiver_full_jid, Set blacklist) { Transport? result = null; foreach (Transport transport in transports.values) { if (transport.transport_type() != type) { continue; } + if (transport.transport_ns_uri() in blacklist) { + continue; + } if (transport.is_transport_available(stream, receiver_full_jid)) { if (result != null) { if (result.transport_priority() >= transport.transport_priority()) { @@ -163,14 +166,14 @@ public class Module : XmppStreamModule, Iq.Handler { } public bool is_available(XmppStream stream, TransportType type, Jid full_jid) { - return is_jingle_available(stream, full_jid) && select_transport(stream, type, full_jid) != null; + return is_jingle_available(stream, full_jid) && select_transport(stream, type, full_jid, Set.empty()) != null; } public Session create_session(XmppStream stream, TransportType type, Jid receiver_full_jid, Senders senders, string content_name, StanzaNode description) throws Error { if (!is_jingle_available(stream, receiver_full_jid)) { throw new Error.NO_SHARED_PROTOCOLS("No Jingle support"); } - Transport? transport = select_transport(stream, type, receiver_full_jid); + Transport? transport = select_transport(stream, type, receiver_full_jid, Set.empty()); if (transport == null) { throw new Error.NO_SHARED_PROTOCOLS("No suitable transports"); } @@ -369,11 +372,13 @@ public interface ContentParameters : Object { public class Session { - // INITIATE_SENT -> CONNECTING -> ACTIVE -> ENDED - // INITIATE_RECEIVED -> CONNECTING -> ACTIVE -> ENDED + // INITIATE_SENT -> CONNECTING -> [REPLACING_TRANSPORT -> CONNECTING ->]... ACTIVE -> ENDED + // INITIATE_RECEIVED -> CONNECTING -> [WAITING_FOR_TRANSPORT_REPLACE -> CONNECTING ->].. ACTIVE -> ENDED public enum State { INITIATE_SENT, + REPLACING_TRANSPORT, INITIATE_RECEIVED, + WAITING_FOR_TRANSPORT_REPLACE, CONNECTING, ACTIVE, ENDED, @@ -381,8 +386,9 @@ public class Session { public State state { get; private set; } + public Role role { get; private set; } public string sid { get; private set; } - public Type type_ { get; private set; } + public TransportType type_ { get; private set; } public Jid local_full_jid { get; private set; } public Jid peer_full_jid { get; private set; } public Role content_creator { get; private set; } @@ -392,25 +398,30 @@ public class Session { public IOStream conn { get { return connection; } } // INITIATE_SENT | INITIATE_RECEIVED | CONNECTING + Set tried_transport_methods = new HashSet(); TransportParameters? transport = null; SessionTerminate session_terminate_handler; - public Session.initiate_sent(string sid, Type type, TransportParameters transport, Jid local_full_jid, Jid peer_full_jid, string content_name, owned SessionTerminate session_terminate_handler) { + public Session.initiate_sent(string sid, TransportType type, TransportParameters transport, Jid local_full_jid, Jid peer_full_jid, string content_name, owned SessionTerminate session_terminate_handler) { this.state = State.INITIATE_SENT; + this.role = Role.INITIATOR; this.sid = sid; this.type_ = type; this.local_full_jid = local_full_jid; this.peer_full_jid = peer_full_jid; this.content_creator = Role.INITIATOR; this.content_name = content_name; + this.tried_transport_methods = new HashSet(); + this.tried_transport_methods.add(transport.transport_ns_uri()); this.transport = transport; this.connection = new Connection(this); this.session_terminate_handler = (owned)session_terminate_handler; } - public Session.initiate_received(string sid, Type type, TransportParameters? transport, Jid local_full_jid, Jid peer_full_jid, string content_name, owned SessionTerminate session_terminate_handler) { + public Session.initiate_received(string sid, TransportType type, TransportParameters? transport, Jid local_full_jid, Jid peer_full_jid, string content_name, owned SessionTerminate session_terminate_handler) { this.state = State.INITIATE_RECEIVED; + this.role = Role.RESPONDER; this.sid = sid; this.type_ = type; this.local_full_jid = local_full_jid; @@ -418,39 +429,87 @@ public class Session { this.content_creator = Role.INITIATOR; this.content_name = content_name; this.transport = transport; + this.tried_transport_methods = new HashSet(); + if (transport != null) { + this.tried_transport_methods.add(transport.transport_ns_uri()); + } this.connection = new Connection(this); this.session_terminate_handler = (owned)session_terminate_handler; } public void handle_iq_set(XmppStream stream, string action, StanzaNode jingle, Iq.Stanza iq) throws IqError { + // Validate action. switch (action) { case "session-accept": - if (state != State.INITIATE_SENT) { - throw new IqError.OUT_OF_ORDER("got session-accept while not waiting for one"); - } - handle_session_accept(stream, jingle, iq); - break; case "session-terminate": - handle_session_terminate(stream, jingle, iq); - break; + case "transport-accept": + case "transport-reject": + case "transport-replace": case "transport-info": - handle_transport_info(stream, jingle, iq); - return; + break; case "content-accept": case "content-add": case "content-modify": case "content-reject": case "content-remove": case "security-info": + default: + throw new IqError.BAD_REQUEST("invalid action"); + } + ContentNode? content = null; + StanzaNode? transport = null; + // Do some pre-processing. + if (action != "session-terminate") { + content = get_single_content_node(jingle); + verify_content(content); + switch (action) { + case "transport-accept": + case "transport-reject": + case "transport-replace": + case "transport-info": + switch (state) { + case State.INITIATE_SENT: + case State.REPLACING_TRANSPORT: + case State.INITIATE_RECEIVED: + case State.WAITING_FOR_TRANSPORT_REPLACE: + case State.CONNECTING: + break; + default: + throw new IqError.OUT_OF_ORDER("transport-* unsupported after connection setup"); + } + // TODO(hrxi): What to do with description nodes? + if (content.transport == null) { + throw new IqError.BAD_REQUEST("missing transport node"); + } + transport = content.transport; + break; + } + } + switch (action) { + case "session-accept": + if (state != State.INITIATE_SENT) { + throw new IqError.OUT_OF_ORDER("got session-accept while not waiting for one"); + } + handle_session_accept(stream, content, jingle, iq); + break; + case "session-terminate": + handle_session_terminate(stream, jingle, iq); + break; case "transport-accept": + handle_transport_accept(stream, transport, jingle, iq); + break; case "transport-reject": + handle_transport_reject(stream, jingle, iq); + break; case "transport-replace": - throw new IqError.NOT_IMPLEMENTED(@"$(action) is not implemented"); - default: - throw new IqError.BAD_REQUEST("invalid action"); + handle_transport_replace(stream, transport, jingle, iq); + break; + case "transport-info": + handle_transport_info(stream, transport, jingle, iq); + break; } } - void handle_session_accept(XmppStream stream, StanzaNode jingle, Iq.Stanza iq) throws IqError { + void handle_session_accept(XmppStream stream, ContentNode content, StanzaNode jingle, Iq.Stanza iq) throws IqError { string? responder_str = jingle.get_attribute("responder"); Jid responder; if (responder_str != null) { @@ -462,8 +521,6 @@ public class Session { if (!responder.is_full()) { throw new IqError.BAD_REQUEST("invalid responder JID"); } - ContentNode content = get_single_content_node(jingle); - verify_content(content); if (content.description == null || content.transport == null) { throw new IqError.BAD_REQUEST("missing description or transport node"); } @@ -472,8 +529,9 @@ public class Session { } transport.on_transport_accept(content.transport); StanzaNode description = content.description; // TODO(hrxi): handle this :P - state = State.CONNECTING; stream.get_module(Iq.Module.IDENTITY).send_iq(stream, new Iq.Stanza.result(iq)); + + state = State.CONNECTING; transport.create_transport_connection(stream, this); } void connection_created(XmppStream stream, IOStream? conn) { @@ -483,12 +541,14 @@ public class Session { if (conn != null) { state = State.ACTIVE; transport = null; + tried_transport_methods.clear(); connection.set_inner(conn); } else { - // TODO(hrxi): try negotiating other transports… - StanzaNode reason = new StanzaNode.build("reason", NS_URI) - .put_node(new StanzaNode.build("failed-transport", NS_URI)); - terminate(reason, "failed transport"); + if (role == Role.INITIATOR) { + select_new_transport(stream); + } else { + state = State.WAITING_FOR_TRANSPORT_REPLACE; + } } } void handle_session_terminate(XmppStream stream, StanzaNode jingle, Iq.Stanza iq) throws IqError { @@ -499,17 +559,88 @@ public class Session { stream.get_module(Iq.Module.IDENTITY).send_iq(stream, new Iq.Stanza.result(iq)); // TODO(hrxi): also handle presence type=unavailable } - void handle_transport_info(XmppStream stream, StanzaNode jingle, Iq.Stanza iq) throws IqError { - if (state != State.INITIATE_RECEIVED && state != State.INITIATE_SENT && state != State.CONNECTING) { - stream.get_module(Iq.Module.IDENTITY).send_iq(stream, new Iq.Stanza.result(iq)); - throw new IqError.UNSUPPORTED_INFO("transport-info unsupported after connection setup"); + void select_new_transport(XmppStream stream) { + Transport? new_transport = stream.get_module(Module.IDENTITY).select_transport(stream, type_, peer_full_jid, tried_transport_methods); + if (new_transport == null) { + StanzaNode reason = new StanzaNode.build("reason", NS_URI) + .put_node(new StanzaNode.build("failed-transport", NS_URI)); + terminate(reason, "failed transport"); + return; } - ContentNode content = get_single_content_node(jingle); - verify_content(content); - if (content.description != null || content.transport == null) { - throw new IqError.BAD_REQUEST("unexpected description node or missing transport node"); + tried_transport_methods.add(new_transport.transport_ns_uri()); + transport = new_transport.create_transport_parameters(stream, local_full_jid, peer_full_jid); + StanzaNode jingle = new StanzaNode.build("jingle", NS_URI) + .add_self_xmlns() + .put_attribute("action", "transport-replace") + .put_attribute("sid", sid) + .put_node(new StanzaNode.build("content", NS_URI) + .put_attribute("creator", "initiator") + .put_attribute("name", content_name) + .put_node(transport.to_transport_stanza_node()) + ); + Iq.Stanza iq = new Iq.Stanza.set(jingle) { to=peer_full_jid }; + stream.get_module(Iq.Module.IDENTITY).send_iq(stream, iq); + state = State.REPLACING_TRANSPORT; + } + void handle_transport_accept(XmppStream stream, StanzaNode transport_node, StanzaNode jingle, Iq.Stanza iq) throws IqError { + if (state != State.REPLACING_TRANSPORT) { + throw new IqError.OUT_OF_ORDER("no outstanding transport-replace request"); + } + if (transport_node.ns_uri != transport.transport_ns_uri()) { + throw new IqError.BAD_REQUEST("transport-accept with unnegotiated transport method"); } - transport.on_transport_info(content.transport); + transport.on_transport_accept(transport_node); + state = State.CONNECTING; + stream.get_module(Iq.Module.IDENTITY).send_iq(stream, new Iq.Stanza.result(iq)); + transport.create_transport_connection(stream, this); + } + void handle_transport_reject(XmppStream stream, StanzaNode jingle, Iq.Stanza iq) throws IqError { + if (state != State.REPLACING_TRANSPORT) { + throw new IqError.OUT_OF_ORDER("no outstanding transport-replace request"); + } + stream.get_module(Iq.Module.IDENTITY).send_iq(stream, new Iq.Stanza.result(iq)); + select_new_transport(stream); + } + void handle_transport_replace(XmppStream stream, StanzaNode transport_node, StanzaNode jingle, Iq.Stanza iq) throws IqError { + Transport? transport = stream.get_module(Module.IDENTITY).get_transport(transport_node.ns_uri); + TransportParameters? parameters = null; + if (transport != null) { + // Just parse the transport info for the errors. + parameters = transport.parse_transport_parameters(stream, local_full_jid, peer_full_jid, transport_node); + } + stream.get_module(Iq.Module.IDENTITY).send_iq(stream, new Iq.Stanza.result(iq)); + if (state != State.WAITING_FOR_TRANSPORT_REPLACE || transport == null) { + StanzaNode jingle_response = new StanzaNode.build("jingle", NS_URI) + .add_self_xmlns() + .put_attribute("action", "transport-reject") + .put_attribute("sid", sid) + .put_node(new StanzaNode.build("content", NS_URI) + .put_attribute("creator", "initiator") + .put_attribute("name", content_name) + .put_node(transport_node) + ); + Iq.Stanza iq_response = new Iq.Stanza.set(jingle_response) { to=peer_full_jid }; + stream.get_module(Iq.Module.IDENTITY).send_iq(stream, iq_response); + return; + } + this.transport = parameters; + StanzaNode jingle_response = new StanzaNode.build("jingle", NS_URI) + .add_self_xmlns() + .put_attribute("action", "transport-accept") + .put_attribute("sid", sid) + .put_node(new StanzaNode.build("content", NS_URI) + .put_attribute("creator", "initiator") + .put_attribute("name", content_name) + .put_node(this.transport.to_transport_stanza_node()) + ); + Iq.Stanza iq_response = new Iq.Stanza.set(jingle_response) { to=peer_full_jid }; + stream.get_module(Iq.Module.IDENTITY).send_iq(stream, iq_response); + + state = State.CONNECTING; + this.transport.create_transport_connection(stream, this); + } + void handle_transport_info(XmppStream stream, StanzaNode transport, StanzaNode jingle, Iq.Stanza iq) throws IqError { + this.transport.on_transport_info(transport); stream.get_module(Iq.Module.IDENTITY).send_iq(stream, new Iq.Stanza.result(iq)); } void verify_content(ContentNode content) throws IqError { diff --git a/xmpp-vala/src/module/xep/0260_jingle_socks5_bytestreams.vala b/xmpp-vala/src/module/xep/0260_jingle_socks5_bytestreams.vala index 78f47cfb..79c62d68 100644 --- a/xmpp-vala/src/module/xep/0260_jingle_socks5_bytestreams.vala +++ b/xmpp-vala/src/module/xep/0260_jingle_socks5_bytestreams.vala @@ -172,6 +172,7 @@ class Parameters : Jingle.TransportParameters, Object { string? waiting_for_activation_cid = null; SourceFunc waiting_for_activation_callback; + bool waiting_for_activation_error = false; private static string calculate_dstaddr(string sid, Jid first_jid, Jid second_jid) { string hashed = sid + first_jid.to_string() + second_jid.to_string(); @@ -200,7 +201,6 @@ class Parameters : Jingle.TransportParameters, Object { throw new Jingle.IqError.BAD_REQUEST("too long dstaddr"); } Parameters result = new Parameters(Jingle.Role.RESPONDER, sid, local_full_jid, peer_full_jid, dstaddr); - //result.remote_candidates.add(new Candidate("b", "0.0.0.0", new Jid("a@b/c"), 1234, 2000000000, CandidateType.PROXY)); foreach (StanzaNode candidate in transport.get_subnodes("candidate", NS_URI)) { result.remote_candidates.add(Candidate.parse(candidate)); } @@ -237,10 +237,12 @@ class Parameters : Jingle.TransportParameters, Object { StanzaNode? candidate_error = transport.get_subnode("candidate-error", NS_URI); StanzaNode? candidate_used = transport.get_subnode("candidate-used", NS_URI); StanzaNode? activated = transport.get_subnode("activated", NS_URI); + StanzaNode? proxy_error = transport.get_subnode("proxy-error", NS_URI); int num_children = 0; if (candidate_error != null) { num_children += 1; } if (candidate_used != null) { num_children += 1; } if (activated != null) { num_children += 1; } + if (proxy_error != null) { num_children += 1; } if (num_children == 0) { throw new Jingle.IqError.UNSUPPORTED_INFO("unknown transport-info"); } else if (num_children > 1) { @@ -263,6 +265,9 @@ class Parameters : Jingle.TransportParameters, Object { } handle_activated(cid); } + if (proxy_error != null) { + handle_proxy_error(); + } } private void handle_remote_candidate(string? cid) throws Jingle.IqError { if (remote_sent_selected_candidate) { @@ -291,6 +296,15 @@ class Parameters : Jingle.TransportParameters, Object { Idle.add((owned)waiting_for_activation_callback); waiting_for_activation_cid = null; } + private void handle_proxy_error() throws Jingle.IqError { + if (waiting_for_activation_cid == null) { + throw new Jingle.IqError.BAD_REQUEST("unexpected proxy error message"); + } + Idle.add((owned)waiting_for_activation_callback); + waiting_for_activation_cid = null; + waiting_for_activation_error = true; + + } private void try_completing_negotiation() { if (!remote_sent_selected_candidate || !local_determined_selected_candidate) { return; @@ -346,7 +360,11 @@ class Parameters : Jingle.TransportParameters, Object { if (strong == null) { return; } - strong.set_transport_connection(hack, conn); + if (!waiting_for_activation_error) { + strong.set_transport_connection(hack, conn); + } else { + strong.set_transport_connection(hack, null); + } } public async void connect_to_local_candidate(Candidate candidate) { try { -- cgit v1.2.3-54-g00ecf From 6083f446b47e258e0381c0c22755dbfa881c7df7 Mon Sep 17 00:00:00 2001 From: hrxi Date: Sat, 24 Aug 2019 13:30:23 +0200 Subject: Fix candidate selection for equal priority XEP-0260 states that the candidate selected (offered) by the initiator wins, not the one that was chosen by the initiator (i.e. offered by the responder). --- xmpp-vala/src/module/xep/0166_jingle.vala | 2 +- xmpp-vala/src/module/xep/0260_jingle_socks5_bytestreams.vala | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) (limited to 'xmpp-vala/src/module/xep/0260_jingle_socks5_bytestreams.vala') diff --git a/xmpp-vala/src/module/xep/0166_jingle.vala b/xmpp-vala/src/module/xep/0166_jingle.vala index e0a96cc6..d6dbcd9e 100644 --- a/xmpp-vala/src/module/xep/0166_jingle.vala +++ b/xmpp-vala/src/module/xep/0166_jingle.vala @@ -720,7 +720,7 @@ public class Session { .put_node(new StanzaNode.build("text", NS_URI) .put_node(new StanzaNode.text(error.message)) ); - terminate(reason, "transport error: $(error.message)"); + terminate(reason, @"transport error: $(error.message)"); } public void on_connection_close() { if (terminate_on_connection_close) { diff --git a/xmpp-vala/src/module/xep/0260_jingle_socks5_bytestreams.vala b/xmpp-vala/src/module/xep/0260_jingle_socks5_bytestreams.vala index 79c62d68..c17dc0b3 100644 --- a/xmpp-vala/src/module/xep/0260_jingle_socks5_bytestreams.vala +++ b/xmpp-vala/src/module/xep/0260_jingle_socks5_bytestreams.vala @@ -332,8 +332,9 @@ class Parameters : Jingle.TransportParameters, Object { } else if (local.priority > remote.priority) { remote_wins = false; } else { - // equal priority -> XEP-0260 says that the initiator wins - remote_wins = role != Jingle.Role.INITIATOR; + // equal priority -> XEP-0260 says that the candidate offered + // by the initiator wins, so the one that the remote chose + remote_wins = role == Jingle.Role.INITIATOR; } } -- cgit v1.2.3-54-g00ecf From 6028fd15a81a084b63311bc61f7b48d9f3d00746 Mon Sep 17 00:00:00 2001 From: hrxi Date: Mon, 26 Aug 2019 17:30:47 +0200 Subject: Don't error on Jingle file transfer hash session-info --- xmpp-vala/src/module/xep/0166_jingle.vala | 34 ++++++++++++++++++---- .../src/module/xep/0234_jingle_file_transfer.vala | 13 +++++++++ .../module/xep/0260_jingle_socks5_bytestreams.vala | 2 ++ 3 files changed, 44 insertions(+), 5 deletions(-) (limited to 'xmpp-vala/src/module/xep/0260_jingle_socks5_bytestreams.vala') diff --git a/xmpp-vala/src/module/xep/0166_jingle.vala b/xmpp-vala/src/module/xep/0166_jingle.vala index d6dbcd9e..06e3d5c8 100644 --- a/xmpp-vala/src/module/xep/0166_jingle.vala +++ b/xmpp-vala/src/module/xep/0166_jingle.vala @@ -47,12 +47,16 @@ public errordomain Error { TRANSPORT_ERROR, } -StanzaNode? get_single_node_anyns(StanzaNode parent, string node_name) throws IqError { +StanzaNode? get_single_node_anyns(StanzaNode parent, string? node_name = null) throws IqError { StanzaNode? result = null; foreach (StanzaNode child in parent.get_all_subnodes()) { - if (child.name == node_name) { + if (node_name == null || child.name == node_name) { if (result != null) { - throw new IqError.BAD_REQUEST(@"multiple $(node_name) nodes"); + if (node_name != null) { + throw new IqError.BAD_REQUEST(@"multiple $(node_name) nodes"); + } else { + throw new IqError.BAD_REQUEST(@"expected single subnode"); + } } result = child; } @@ -364,6 +368,7 @@ public interface ContentType : Object { public abstract string content_type_ns_uri(); public abstract TransportType content_type_transport_type(); public abstract ContentParameters parse_content_parameters(StanzaNode description) throws IqError; + public abstract void handle_content_session_info(XmppStream stream, Session session, StanzaNode info, Iq.Stanza iq) throws IqError; } public interface ContentParameters : Object { @@ -445,25 +450,28 @@ public class Session { // Validate action. switch (action) { case "session-accept": + case "session-info": case "session-terminate": case "transport-accept": + case "transport-info": case "transport-reject": case "transport-replace": - case "transport-info": break; case "content-accept": case "content-add": case "content-modify": case "content-reject": case "content-remove": + case "description-info": case "security-info": + throw new IqError.NOT_IMPLEMENTED(@"$(action) is not implemented"); default: throw new IqError.BAD_REQUEST("invalid action"); } ContentNode? content = null; StanzaNode? transport = null; // Do some pre-processing. - if (action != "session-terminate") { + if (action != "session-info" && action != "session-terminate") { content = get_single_content_node(jingle); verify_content(content); switch (action) { @@ -496,6 +504,9 @@ public class Session { } handle_session_accept(stream, content, jingle, iq); break; + case "session-info": + handle_session_info(stream, jingle, iq); + break; case "session-terminate": handle_session_terminate(stream, jingle, iq); break; @@ -563,6 +574,19 @@ public class Session { stream.get_module(Iq.Module.IDENTITY).send_iq(stream, new Iq.Stanza.result(iq)); // TODO(hrxi): also handle presence type=unavailable } + void handle_session_info(XmppStream stream, StanzaNode jingle, Iq.Stanza iq) throws IqError { + StanzaNode? info = get_single_node_anyns(jingle); + if (info == null) { + // Jingle session ping + stream.get_module(Iq.Module.IDENTITY).send_iq(stream, new Iq.Stanza.result(iq)); + return; + } + ContentType? content_type = stream.get_module(Module.IDENTITY).get_content_type(info.ns_uri); + if (content_type == null) { + throw new IqError.UNSUPPORTED_INFO("unknown session-info namespace"); + } + content_type.handle_content_session_info(stream, this, info, iq); + } void select_new_transport(XmppStream stream) { Transport? new_transport = stream.get_module(Module.IDENTITY).select_transport(stream, type_, peer_full_jid, tried_transport_methods); if (new_transport == null) { diff --git a/xmpp-vala/src/module/xep/0234_jingle_file_transfer.vala b/xmpp-vala/src/module/xep/0234_jingle_file_transfer.vala index 43c212f5..25fe3ce4 100644 --- a/xmpp-vala/src/module/xep/0234_jingle_file_transfer.vala +++ b/xmpp-vala/src/module/xep/0234_jingle_file_transfer.vala @@ -24,6 +24,19 @@ public class Module : Jingle.ContentType, XmppStreamModule { public Jingle.ContentParameters parse_content_parameters(StanzaNode description) throws Jingle.IqError { return Parameters.parse(this, description); } + public void handle_content_session_info(XmppStream stream, Jingle.Session session, StanzaNode info, Iq.Stanza iq) throws Jingle.IqError { + switch (info.name) { + case "received": + stream.get_module(Iq.Module.IDENTITY).send_iq(stream, new Iq.Stanza.result(iq)); + break; + case "checksum": + // TODO(hrxi): handle hash + stream.get_module(Iq.Module.IDENTITY).send_iq(stream, new Iq.Stanza.result(iq)); + break; + default: + throw new Jingle.IqError.UNSUPPORTED_INFO(@"unsupported file transfer info $(info.name)"); + } + } public signal void file_incoming(XmppStream stream, FileTransfer file_transfer); diff --git a/xmpp-vala/src/module/xep/0260_jingle_socks5_bytestreams.vala b/xmpp-vala/src/module/xep/0260_jingle_socks5_bytestreams.vala index c17dc0b3..e0542207 100644 --- a/xmpp-vala/src/module/xep/0260_jingle_socks5_bytestreams.vala +++ b/xmpp-vala/src/module/xep/0260_jingle_socks5_bytestreams.vala @@ -477,6 +477,8 @@ class Parameters : Jingle.TransportParameters, Object { throw new IOError.PROXY_FAILED("wanted port 00 00, got %02x %02x".printf(read_buffer[5+dstaddr.length], read_buffer[5+dstaddr.length+1])); } + conn.get_socket().set_timeout(0); + return conn; } public async void try_connecting_to_candidates(XmppStream stream, Jingle.Session session) throws Error { -- cgit v1.2.3-54-g00ecf