From 2b90fcc39a1079346d6c5e2bfff8987104da737a Mon Sep 17 00:00:00 2001 From: fiaxh Date: Fri, 19 Mar 2021 22:46:39 +0100 Subject: [PATCH] Improve & refactor Jingle base implementation Co-authored-by: Marvin W --- xmpp-vala/CMakeLists.txt | 29 +- xmpp-vala/src/core/xmpp_log.vala | 4 +- xmpp-vala/src/module/iq/module.vala | 2 + xmpp-vala/src/module/xep/0166_jingle.vala | 1061 ----------------- .../src/module/xep/0166_jingle/component.vala | 52 + .../src/module/xep/0166_jingle/content.vala | 236 ++++ .../xep/0166_jingle/content_description.vala | 27 + .../module/xep/0166_jingle/content_node.vala | 112 ++ .../xep/0166_jingle/content_security.vala | 18 + .../xep/0166_jingle/content_transport.vala | 29 + .../module/xep/0166_jingle/jingle_flag.vala | 38 + .../module/xep/0166_jingle/jingle_module.vala | 235 ++++ .../xep/0166_jingle/jingle_structs.vala | 73 ++ .../xep/0166_jingle/reason_element.vala | 29 + .../src/module/xep/0166_jingle/session.vala | 559 +++++++++ .../module/xep/0166_jingle/session_info.vala | 12 + .../module/xep/0234_jingle_file_transfer.vala | 162 ++- .../xep/0260_jingle_socks5_bytestreams.vala | 100 +- .../xep/0261_jingle_in_band_bytestreams.vala | 50 +- 19 files changed, 1671 insertions(+), 1157 deletions(-) delete mode 100644 xmpp-vala/src/module/xep/0166_jingle.vala create mode 100644 xmpp-vala/src/module/xep/0166_jingle/component.vala create mode 100644 xmpp-vala/src/module/xep/0166_jingle/content.vala create mode 100644 xmpp-vala/src/module/xep/0166_jingle/content_description.vala create mode 100644 xmpp-vala/src/module/xep/0166_jingle/content_node.vala create mode 100644 xmpp-vala/src/module/xep/0166_jingle/content_security.vala create mode 100644 xmpp-vala/src/module/xep/0166_jingle/content_transport.vala create mode 100644 xmpp-vala/src/module/xep/0166_jingle/jingle_flag.vala create mode 100644 xmpp-vala/src/module/xep/0166_jingle/jingle_module.vala create mode 100644 xmpp-vala/src/module/xep/0166_jingle/jingle_structs.vala create mode 100644 xmpp-vala/src/module/xep/0166_jingle/reason_element.vala create mode 100644 xmpp-vala/src/module/xep/0166_jingle/session.vala create mode 100644 xmpp-vala/src/module/xep/0166_jingle/session_info.vala diff --git a/xmpp-vala/CMakeLists.txt b/xmpp-vala/CMakeLists.txt index fcc74fdc..48f411c7 100644 --- a/xmpp-vala/CMakeLists.txt +++ b/xmpp-vala/CMakeLists.txt @@ -61,15 +61,18 @@ SOURCES "src/module/xep/0048_conference.vala" "src/module/xep/0402_bookmarks2.vala" "src/module/xep/0004_data_forms.vala" + "src/module/xep/0030_service_discovery/flag.vala" "src/module/xep/0030_service_discovery/identity.vala" "src/module/xep/0030_service_discovery/info_result.vala" "src/module/xep/0030_service_discovery/item.vala" "src/module/xep/0030_service_discovery/items_result.vala" "src/module/xep/0030_service_discovery/module.vala" + "src/module/xep/0045_muc/flag.vala" "src/module/xep/0045_muc/module.vala" "src/module/xep/0045_muc/status_code.vala" + "src/module/xep/0047_in_band_bytestreams.vala" "src/module/xep/0049_private_xml_storage.vala" "src/module/xep/0054_vcard/module.vala" @@ -81,7 +84,31 @@ SOURCES "src/module/xep/0084_user_avatars.vala" "src/module/xep/0085_chat_state_notifications.vala" "src/module/xep/0115_entity_capabilities.vala" - "src/module/xep/0166_jingle.vala" + + "src/module/xep/0166_jingle/content.vala" + "src/module/xep/0166_jingle/content_description.vala" + "src/module/xep/0166_jingle/content_node.vala" + "src/module/xep/0166_jingle/content_security.vala" + "src/module/xep/0166_jingle/content_transport.vala" + "src/module/xep/0166_jingle/component.vala" + "src/module/xep/0166_jingle/jingle_flag.vala" + "src/module/xep/0166_jingle/jingle_module.vala" + "src/module/xep/0166_jingle/jingle_structs.vala" + "src/module/xep/0166_jingle/reason_element.vala" + "src/module/xep/0166_jingle/session.vala" + "src/module/xep/0166_jingle/session_info.vala" + + "src/module/xep/0167_jingle_rtp/content_parameters.vala" + "src/module/xep/0167_jingle_rtp/content_type.vala" + "src/module/xep/0167_jingle_rtp/jingle_rtp_module.vala" + "src/module/xep/0167_jingle_rtp/payload_type.vala" + "src/module/xep/0167_jingle_rtp/session_info_type.vala" + "src/module/xep/0167_jingle_rtp/stream.vala" + + "src/module/xep/0176_jingle_ice_udp/candidate.vala" + "src/module/xep/0176_jingle_ice_udp/jingle_ice_udp_module.vala" + "src/module/xep/0176_jingle_ice_udp/transport_parameters.vala" + "src/module/xep/0184_message_delivery_receipts.vala" "src/module/xep/0191_blocking_command.vala" "src/module/xep/0198_stream_management.vala" diff --git a/xmpp-vala/src/core/xmpp_log.vala b/xmpp-vala/src/core/xmpp_log.vala index 4790a8ab..3d5693ef 100644 --- a/xmpp-vala/src/core/xmpp_log.vala +++ b/xmpp-vala/src/core/xmpp_log.vala @@ -110,13 +110,13 @@ public class XmppLog { public void node(string what, StanzaNode node, XmppStream stream) { if (should_log_node(node)) { - stderr.printf("%sXMPP %s [%s %p %s]%s\n%s\n", use_ansi ? ANSI_COLOR_WHITE : "", what, ident, stream, new DateTime.now_local().to_string(), use_ansi ? ANSI_COLOR_END : "", use_ansi ? node.to_ansi_string(hide_ns) : node.to_string()); + stderr.printf("%sXMPP %s [%s stream:%p thread:%p %s]%s\n%s\n", use_ansi ? ANSI_COLOR_WHITE : "", what, ident, stream, Thread.self(), new DateTime.now_local().to_string(), use_ansi ? ANSI_COLOR_END : "", use_ansi ? node.to_ansi_string(hide_ns) : node.to_string()); } } public void str(string what, string str, XmppStream stream) { if (should_log_str(str)) { - stderr.printf("%sXMPP %s [%s %p %s]%s\n%s\n", use_ansi ? ANSI_COLOR_WHITE : "", what, ident, stream, new DateTime.now_local().to_string(), use_ansi ? ANSI_COLOR_END : "", str); + stderr.printf("%sXMPP %s [%s stream:%p thread:%p %s]%s\n%s\n", use_ansi ? ANSI_COLOR_WHITE : "", what, ident, stream, Thread.self(), new DateTime.now_local().to_string(), use_ansi ? ANSI_COLOR_END : "", str); } } diff --git a/xmpp-vala/src/module/iq/module.vala b/xmpp-vala/src/module/iq/module.vala index 9deb0422..56605d01 100644 --- a/xmpp-vala/src/module/iq/module.vala +++ b/xmpp-vala/src/module/iq/module.vala @@ -10,6 +10,8 @@ namespace Xmpp.Iq { private HashMap> namespaceRegistrants = new HashMap>(); public async Iq.Stanza send_iq_async(XmppStream stream, Iq.Stanza iq) { + assert(iq.type_ == Iq.Stanza.TYPE_GET || iq.type_ == Iq.Stanza.TYPE_SET); + Iq.Stanza? return_stanza = null; send_iq(stream, iq, (_, result_iq) => { return_stanza = result_iq; diff --git a/xmpp-vala/src/module/xep/0166_jingle.vala b/xmpp-vala/src/module/xep/0166_jingle.vala deleted file mode 100644 index 3a634222..00000000 --- a/xmpp-vala/src/module/xep/0166_jingle.vala +++ /dev/null @@ -1,1061 +0,0 @@ -using Gee; -using Xmpp.Xep; -using Xmpp; - -namespace Xmpp.Xep.Jingle { - -private const string NS_URI = "urn:xmpp:jingle:1"; -private const string ERROR_NS_URI = "urn:xmpp:jingle:errors:1"; - -public errordomain IqError { - BAD_REQUEST, - NOT_ACCEPTABLE, - NOT_IMPLEMENTED, - UNSUPPORTED_INFO, - OUT_OF_ORDER, - RESOURCE_CONSTRAINT, -} - -void send_iq_error(IqError iq_error, XmppStream stream, Iq.Stanza iq) { - ErrorStanza error; - if (iq_error is IqError.BAD_REQUEST) { - error = new ErrorStanza.bad_request(iq_error.message); - } else if (iq_error is IqError.NOT_ACCEPTABLE) { - error = new ErrorStanza.not_acceptable(iq_error.message); - } else if (iq_error is IqError.NOT_IMPLEMENTED) { - error = new ErrorStanza.feature_not_implemented(iq_error.message); - } else if (iq_error is IqError.UNSUPPORTED_INFO) { - StanzaNode unsupported_info = new StanzaNode.build("unsupported-info", ERROR_NS_URI).add_self_xmlns(); - error = new ErrorStanza.build(ErrorStanza.TYPE_CANCEL, ErrorStanza.CONDITION_FEATURE_NOT_IMPLEMENTED, iq_error.message, unsupported_info); - } else if (iq_error is IqError.OUT_OF_ORDER) { - StanzaNode out_of_order = new StanzaNode.build("out-of-order", ERROR_NS_URI).add_self_xmlns(); - error = new ErrorStanza.build(ErrorStanza.TYPE_MODIFY, ErrorStanza.CONDITION_UNEXPECTED_REQUEST, iq_error.message, out_of_order); - } else if (iq_error is IqError.RESOURCE_CONSTRAINT) { - error = new ErrorStanza.resource_constraint(iq_error.message); - } else { - assert_not_reached(); - } - stream.get_module(Iq.Module.IDENTITY).send_iq(stream, new Iq.Stanza.error(iq, error) { to=iq.from }); -} - -public errordomain Error { - GENERAL, - BAD_REQUEST, - INVALID_PARAMETERS, - UNSUPPORTED_TRANSPORT, - UNSUPPORTED_SECURITY, - NO_SHARED_PROTOCOLS, - TRANSPORT_ERROR, -} - -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 (node_name == null || child.name == node_name) { - if (result != null) { - if (node_name != null) { - throw new IqError.BAD_REQUEST(@"multiple $(node_name) nodes"); - } else { - throw new IqError.BAD_REQUEST(@"expected single subnode"); - } - } - result = child; - } - } - return result; -} - -class ContentNode { - public Role creator; - public string name; - public StanzaNode? description; - public StanzaNode? transport; - public StanzaNode? security; -} - -ContentNode get_single_content_node(StanzaNode jingle) throws IqError { - Gee.List contents = jingle.get_subnodes("content"); - if (contents.size == 0) { - throw new IqError.BAD_REQUEST("missing content node"); - } - if (contents.size > 1) { - throw new IqError.NOT_IMPLEMENTED("can't process multiple content nodes"); - } - StanzaNode content = contents[0]; - string? creator_str = content.get_attribute("creator"); - // Vala can't typecheck the ternary operator here. - Role? creator = null; - if (creator_str != null) { - creator = Role.parse(creator_str); - } else { - // TODO(hrxi): now, is the creator attribute optional or not (XEP-0166 - // Jingle)? - creator = Role.INITIATOR; - } - - string? name = content.get_attribute("name"); - StanzaNode? description = get_single_node_anyns(content, "description"); - StanzaNode? transport = get_single_node_anyns(content, "transport"); - StanzaNode? security = get_single_node_anyns(content, "security"); - if (name == null || creator == null) { - throw new IqError.BAD_REQUEST("missing name or creator"); - } - - return new ContentNode() { - creator=creator, - name=name, - description=description, - transport=transport, - security=security - }; -} - -// This module can only be attached to one stream at a time. -public class Module : XmppStreamModule, Iq.Handler { - public static Xmpp.ModuleIdentity IDENTITY = new Xmpp.ModuleIdentity(NS_URI, "0166_jingle"); - - private HashMap content_types = new HashMap(); - private HashMap transports = new HashMap(); - private HashMap security_preconditions = new HashMap(); - - private XmppStream? current_stream = null; - - public override void attach(XmppStream stream) { - stream.add_flag(new Flag()); - stream.get_module(ServiceDiscovery.Module.IDENTITY).add_feature(stream, NS_URI); - stream.get_module(Iq.Module.IDENTITY).register_for_namespace(NS_URI, this); - current_stream = stream; - } - public override void detach(XmppStream stream) { - stream.get_module(ServiceDiscovery.Module.IDENTITY).remove_feature(stream, NS_URI); - stream.get_module(Iq.Module.IDENTITY).unregister_from_namespace(NS_URI, this); - } - - public void register_content_type(ContentType content_type) { - content_types[content_type.content_type_ns_uri()] = content_type; - } - public ContentType? get_content_type(string ns_uri) { - if (!content_types.has_key(ns_uri)) { - return null; - } - return content_types[ns_uri]; - } - public void register_transport(Transport transport) { - transports[transport.transport_ns_uri()] = transport; - } - public Transport? get_transport(string ns_uri) { - if (!transports.has_key(ns_uri)) { - return null; - } - return transports[ns_uri]; - } - public async 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 (yield transport.is_transport_available(stream, receiver_full_jid)) { - if (result != null) { - if (result.transport_priority() >= transport.transport_priority()) { - continue; - } - } - result = transport; - } - } - return result; - } - public void register_security_precondition(SecurityPrecondition precondition) { - security_preconditions[precondition.security_ns_uri()] = precondition; - } - public SecurityPrecondition? get_security_precondition(string? ns_uri) { - if (ns_uri == null) return null; - if (!security_preconditions.has_key(ns_uri)) { - return null; - } - return security_preconditions[ns_uri]; - } - - private async bool is_jingle_available(XmppStream stream, Jid full_jid) { - bool? has_jingle = yield stream.get_module(ServiceDiscovery.Module.IDENTITY).has_entity_feature(stream, full_jid, NS_URI); - return has_jingle != null && has_jingle; - } - - public async bool is_available(XmppStream stream, TransportType type, Jid full_jid) { - return (yield is_jingle_available(stream, full_jid)) && (yield select_transport(stream, type, full_jid, Set.empty())) != null; - } - - public async Session create_session(XmppStream stream, TransportType type, Jid receiver_full_jid, Senders senders, string content_name, StanzaNode description, string? precondition_name = null, Object? precondation_options = null) throws Error { - if (!yield is_jingle_available(stream, receiver_full_jid)) { - throw new Error.NO_SHARED_PROTOCOLS("No Jingle support"); - } - Transport? transport = yield select_transport(stream, type, receiver_full_jid, Set.empty()); - if (transport == null) { - throw new Error.NO_SHARED_PROTOCOLS("No suitable transports"); - } - SecurityPrecondition? precondition = get_security_precondition(precondition_name); - if (precondition_name != null && precondition == null) { - throw new Error.UNSUPPORTED_SECURITY("No suitable security precondiiton found"); - } - Jid? my_jid = stream.get_flag(Bind.Flag.IDENTITY).my_jid; - if (my_jid == null) { - throw new Error.GENERAL("Couldn't determine own JID"); - } - TransportParameters transport_params = transport.create_transport_parameters(stream, my_jid, receiver_full_jid); - SecurityParameters? security_params = precondition != null ? precondition.create_security_parameters(stream, my_jid, receiver_full_jid, precondation_options) : null; - Session session = new Session.initiate_sent(random_uuid(), type, transport_params, security_params, my_jid, receiver_full_jid, content_name, send_terminate_and_remove_session); - StanzaNode content = new StanzaNode.build("content", NS_URI) - .put_attribute("creator", "initiator") - .put_attribute("name", content_name) - .put_attribute("senders", senders.to_string()) - .put_node(description) - .put_node(transport_params.to_transport_stanza_node()); - if (security_params != null) { - content.put_node(security_params.to_security_stanza_node(stream, my_jid, receiver_full_jid)); - } - StanzaNode jingle = new StanzaNode.build("jingle", NS_URI) - .add_self_xmlns() - .put_attribute("action", "session-initiate") - .put_attribute("initiator", my_jid.to_string()) - .put_attribute("sid", session.sid) - .put_node(content); - Iq.Stanza iq = new Iq.Stanza.set(jingle) { to=receiver_full_jid }; - - stream.get_module(Iq.Module.IDENTITY).send_iq(stream, iq, (stream, iq) => { - // TODO(hrxi): handle errors - stream.get_flag(Flag.IDENTITY).add_session(session); - }); - - return session; - } - - public void handle_session_initiate(XmppStream stream, string sid, StanzaNode jingle, Iq.Stanza iq) throws IqError { - ContentNode content = get_single_content_node(jingle); - if (content.description == null || content.transport == null) { - throw new IqError.BAD_REQUEST("missing description or transport node"); - } - Jid? my_jid = stream.get_flag(Bind.Flag.IDENTITY).my_jid; - if (my_jid == null) { - throw new IqError.RESOURCE_CONSTRAINT("Couldn't determine own JID"); - } - Transport? transport = get_transport(content.transport.ns_uri); - TransportParameters? transport_params = null; - if (transport != null) { - transport_params = transport.parse_transport_parameters(stream, my_jid, iq.from, content.transport); - } else { - // terminate the session below - } - - ContentType? content_type = get_content_type(content.description.ns_uri); - if (content_type == null) { - // TODO(hrxi): how do we signal an unknown content type? - throw new IqError.NOT_IMPLEMENTED("unknown content type"); - } - ContentParameters content_params = content_type.parse_content_parameters(content.description); - - SecurityPrecondition? precondition = content.security != null ? get_security_precondition(content.security.ns_uri) : null; - SecurityParameters? security_params = null; - if (precondition != null) { - debug("Using precondition %s", precondition.security_ns_uri()); - security_params = precondition.parse_security_parameters(stream, my_jid, iq.from, content.security); - } else if (content.security != null) { - throw new IqError.NOT_IMPLEMENTED("unknown security precondition"); - } - - TransportType type = content_type.content_type_transport_type(); - Session session = new Session.initiate_received(sid, type, transport_params, security_params, my_jid, iq.from, content.name, send_terminate_and_remove_session); - stream.get_flag(Flag.IDENTITY).add_session(session); - stream.get_module(Iq.Module.IDENTITY).send_iq(stream, new Iq.Stanza.result(iq)); - - if (transport == null || transport.transport_type() != type) { - StanzaNode reason = new StanzaNode.build("reason", NS_URI) - .put_node(new StanzaNode.build("unsupported-transports", NS_URI)); - session.terminate(reason, "unsupported transports"); - return; - } - - content_params.on_session_initiate(stream, session); - } - - private void send_terminate_and_remove_session(Jid to, string sid, StanzaNode reason) { - StanzaNode jingle = new StanzaNode.build("jingle", NS_URI) - .add_self_xmlns() - .put_attribute("action", "session-terminate") - .put_attribute("sid", sid) - .put_node(reason); - Iq.Stanza iq = new Iq.Stanza.set(jingle) { to=to }; - current_stream.get_module(Iq.Module.IDENTITY).send_iq(current_stream, iq); - - // Immediately remove the session from the open sessions as per the - // XEP, don't wait for confirmation. - current_stream.get_flag(Flag.IDENTITY).remove_session(sid); - } - - public async void on_iq_set(XmppStream stream, Iq.Stanza iq) { - try { - handle_iq_set(stream, iq); - } catch (IqError e) { - send_iq_error(e, stream, iq); - } - } - - public void handle_iq_set(XmppStream stream, Iq.Stanza iq) throws IqError { - StanzaNode? jingle = iq.stanza.get_subnode("jingle", NS_URI); - string? sid = jingle != null ? jingle.get_attribute("sid") : null; - string? action = jingle != null ? jingle.get_attribute("action") : null; - if (jingle == null || sid == null || action == null) { - throw new IqError.BAD_REQUEST("missing jingle node, sid or action"); - } - Session? session = stream.get_flag(Flag.IDENTITY).get_session(sid); - if (action == "session-initiate") { - if (session != null) { - // TODO(hrxi): Info leak if other clients use predictable session IDs? - stream.get_module(Iq.Module.IDENTITY).send_iq(stream, new Iq.Stanza.error(iq, new ErrorStanza.build(ErrorStanza.TYPE_MODIFY, ErrorStanza.CONDITION_CONFLICT, "session ID already in use", null)) { to=iq.from }); - return; - } - handle_session_initiate(stream, sid, jingle, iq); - return; - } - if (session == null) { - StanzaNode unknown_session = new StanzaNode.build("unknown-session", ERROR_NS_URI).add_self_xmlns(); - stream.get_module(Iq.Module.IDENTITY).send_iq(stream, new Iq.Stanza.error(iq, new ErrorStanza.item_not_found(unknown_session)) { to=iq.from }); - return; - } - session.handle_iq_set(stream, action, jingle, iq); - } - - public override string get_ns() { return NS_URI; } - public override string get_id() { return IDENTITY.id; } -} - -public enum TransportType { - DATAGRAM, - STREAMING, -} - -public enum Senders { - BOTH, - INITIATOR, - NONE, - RESPONDER; - - public string to_string() { - switch (this) { - case BOTH: return "both"; - case INITIATOR: return "initiator"; - case NONE: return "none"; - case RESPONDER: return "responder"; - } - assert_not_reached(); - } -} - -public delegate void SessionTerminate(Jid to, string sid, StanzaNode reason); - -public interface Transport : Object { - public abstract string transport_ns_uri(); - public async abstract bool is_transport_available(XmppStream stream, Jid full_jid); - public abstract TransportType transport_type(); - public abstract int transport_priority(); - public abstract TransportParameters create_transport_parameters(XmppStream stream, Jid local_full_jid, Jid peer_full_jid) throws Error; - public abstract TransportParameters parse_transport_parameters(XmppStream stream, Jid local_full_jid, Jid peer_full_jid, StanzaNode transport) throws IqError; -} - - -// Gets a null `stream` if connection setup was unsuccessful and another -// transport method should be tried. -public interface TransportParameters : Object { - public abstract string transport_ns_uri(); - public abstract StanzaNode to_transport_stanza_node(); - public abstract void on_transport_accept(StanzaNode transport) throws IqError; - public abstract void on_transport_info(StanzaNode transport) throws IqError; - public abstract void create_transport_connection(XmppStream stream, Session session); -} - -public enum Role { - INITIATOR, - RESPONDER; - - public string to_string() { - switch (this) { - case INITIATOR: return "initiator"; - case RESPONDER: return "responder"; - } - assert_not_reached(); - } - - public static Role parse(string role) throws IqError { - switch (role) { - case "initiator": return INITIATOR; - case "responder": return RESPONDER; - } - throw new IqError.BAD_REQUEST(@"invalid role $(role)"); - } -} - -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 { - public abstract void on_session_initiate(XmppStream stream, Session session); -} - -public interface SecurityPrecondition : Object { - public abstract string security_ns_uri(); - public abstract SecurityParameters? create_security_parameters(XmppStream stream, Jid local_full_jid, Jid peer_full_jid, Object options) throws Jingle.Error; - public abstract SecurityParameters? parse_security_parameters(XmppStream stream, Jid local_full_jid, Jid peer_full_jid, StanzaNode security) throws IqError; -} - -public interface SecurityParameters : Object { - public abstract string security_ns_uri(); - public abstract StanzaNode to_security_stanza_node(XmppStream stream, Jid local_full_jid, Jid peer_full_jid); - public abstract IOStream wrap_stream(IOStream stream); -} - -public class Session { - // 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, - } - - public State state { get; private set; } - - public Role role { get; private set; } - public string sid { 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; } - public string content_name { get; private set; } - public SecurityParameters? security { get; private set; } - - private Connection connection; - public IOStream conn { get { return connection; } } - - public bool terminate_on_connection_close { get; set; } - - // INITIATE_SENT | INITIATE_RECEIVED | CONNECTING - Set tried_transport_methods = new HashSet(); - TransportParameters? transport = null; - - SessionTerminate session_terminate_handler; - - public Session.initiate_sent(string sid, TransportType type, TransportParameters transport, SecurityParameters? security, 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.security = security; - this.connection = new Connection(this); - this.session_terminate_handler = (owned)session_terminate_handler; - this.terminate_on_connection_close = true; - } - - public Session.initiate_received(string sid, TransportType type, TransportParameters? transport, SecurityParameters? security, 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; - this.peer_full_jid = peer_full_jid; - this.content_creator = Role.INITIATOR; - this.content_name = content_name; - this.transport = transport; - this.security = security; - 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; - this.terminate_on_connection_close = true; - } - - public void handle_iq_set(XmppStream stream, string action, StanzaNode jingle, Iq.Stanza iq) throws IqError { - // 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": - 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-info" && 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-info": - handle_session_info(stream, 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": - 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, ContentNode content, StanzaNode jingle, Iq.Stanza iq) throws IqError { - string? responder_str = jingle.get_attribute("responder"); - Jid responder = iq.from; - if (responder_str != null) { - try { - responder = new Jid(responder_str); - } catch (InvalidJidError e) { - warning("Received invalid session accept: %s", e.message); - } - } - // TODO(hrxi): more sanity checking, perhaps replace who we're talking to - if (!responder.is_full()) { - throw new IqError.BAD_REQUEST("invalid responder JID"); - } - if (content.description == null || content.transport == null) { - throw new IqError.BAD_REQUEST("missing description or transport node"); - } - if (content.transport.ns_uri != transport.transport_ns_uri()) { - throw new IqError.BAD_REQUEST("session-accept with unnegotiated transport method"); - } - transport.on_transport_accept(content.transport); - // TODO(hrxi): handle content.description :) - 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) { - if (state != State.CONNECTING) { - return; - } - if (conn != null) { - state = State.ACTIVE; - tried_transport_methods.clear(); - if (security != null) { - connection.set_inner(security.wrap_stream(conn)); - } else { - connection.set_inner(conn); - } - transport = null; - } else { - if (role == Role.INITIATOR) { - select_new_transport.begin(stream); - } else { - state = State.WAITING_FOR_TRANSPORT_REPLACE; - } - } - } - void handle_session_terminate(XmppStream stream, StanzaNode jingle, Iq.Stanza iq) throws IqError { - connection.on_terminated_by_jingle("remote terminated jingle session"); - state = State.ENDED; - stream.get_flag(Flag.IDENTITY).remove_session(sid); - - 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); - } - async void select_new_transport(XmppStream stream) { - Transport? new_transport = yield 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; - } - 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_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.begin(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 { - if (content.name != content_name || content.creator != content_creator) { - throw new IqError.BAD_REQUEST("unknown content"); - } - } - public void set_transport_connection(XmppStream stream, IOStream? conn) { - if (state != State.CONNECTING) { - return; - } - connection_created(stream, conn); - } - public void send_transport_info(XmppStream stream, StanzaNode transport) { - if (state != State.CONNECTING) { - return; - } - StanzaNode jingle = new StanzaNode.build("jingle", NS_URI) - .add_self_xmlns() - .put_attribute("action", "transport-info") - .put_attribute("sid", sid) - .put_node(new StanzaNode.build("content", NS_URI) - .put_attribute("creator", "initiator") - .put_attribute("name", content_name) - .put_node(transport) - ); - Iq.Stanza iq = new Iq.Stanza.set(jingle) { to=peer_full_jid }; - stream.get_module(Iq.Module.IDENTITY).send_iq(stream, iq); - } - public void accept(XmppStream stream, StanzaNode description) { - if (state != State.INITIATE_RECEIVED) { - return; // TODO(hrxi): what to do? - } - StanzaNode jingle = new StanzaNode.build("jingle", NS_URI) - .add_self_xmlns() - .put_attribute("action", "session-accept") - .put_attribute("sid", sid) - .put_node(new StanzaNode.build("content", NS_URI) - .put_attribute("creator", "initiator") - .put_attribute("name", content_name) - .put_node(description) - .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.CONNECTING; - transport.create_transport_connection(stream, this); - } - - public void reject(XmppStream stream) { - if (state != State.INITIATE_RECEIVED) { - return; // TODO(hrxi): what to do? - } - StanzaNode reason = new StanzaNode.build("reason", NS_URI) - .put_node(new StanzaNode.build("decline", NS_URI)); - terminate(reason, "declined"); - } - - public void set_application_error(XmppStream stream, StanzaNode? application_reason = null) { - StanzaNode reason = new StanzaNode.build("reason", NS_URI) - .put_node(new StanzaNode.build("failed-application", NS_URI)); - if (application_reason != null) { - reason.put_node(application_reason); - } - terminate(reason, "application error"); - } - - public void on_connection_error(IOError error) { - // TODO(hrxi): where can we get an XmppStream from? - StanzaNode reason = new StanzaNode.build("reason", NS_URI) - .put_node(new StanzaNode.build("failed-transport", NS_URI)) - .put_node(new StanzaNode.build("text", NS_URI) - .put_node(new StanzaNode.text(error.message)) - ); - terminate(reason, @"transport error: $(error.message)"); - } - public void on_connection_close() { - if (terminate_on_connection_close) { - StanzaNode reason = new StanzaNode.build("reason", NS_URI) - .put_node(new StanzaNode.build("success", NS_URI)); - terminate(reason, "success"); - } - } - - public void terminate(StanzaNode reason, string? local_reason) { - if (state == State.ENDED) { - return; - } - if (state == State.ACTIVE) { - if (local_reason != null) { - connection.on_terminated_by_jingle(@"local session-terminate: $(local_reason)"); - } else { - connection.on_terminated_by_jingle("local session-terminate"); - } - } - - session_terminate_handler(peer_full_jid, sid, reason); - state = State.ENDED; - } -} - -public class Connection : IOStream { - public class Input : InputStream { - private weak Connection connection; - public Input(Connection connection) { - this.connection = connection; - } - public override ssize_t read(uint8[] buffer, Cancellable? cancellable = null) throws IOError { - throw new IOError.NOT_SUPPORTED("can't do non-async reads on jingle connections"); - } - public override async ssize_t read_async(uint8[]? buffer, int io_priority = GLib.Priority.DEFAULT, Cancellable? cancellable = null) throws IOError { - return yield connection.read_async(buffer, io_priority, cancellable); - } - public override bool close(Cancellable? cancellable = null) throws IOError { - return connection.close_read(cancellable); - } - public override async bool close_async(int io_priority = GLib.Priority.DEFAULT, Cancellable? cancellable = null) throws IOError { - return yield connection.close_read_async(io_priority, cancellable); - } - } - public class Output : OutputStream { - private weak Connection connection; - public Output(Connection connection) { - this.connection = connection; - } - public override ssize_t write(uint8[] buffer, Cancellable? cancellable = null) throws IOError { - throw new IOError.NOT_SUPPORTED("can't do non-async writes on jingle connections"); - } - public override async ssize_t write_async(uint8[]? buffer, int io_priority = GLib.Priority.DEFAULT, Cancellable? cancellable = null) throws IOError { - return yield connection.write_async(buffer, io_priority, cancellable); - } - public override bool close(Cancellable? cancellable = null) throws IOError { - return connection.close_write(cancellable); - } - public override async bool close_async(int io_priority = GLib.Priority.DEFAULT, Cancellable? cancellable = null) throws IOError { - return yield connection.close_write_async(io_priority, cancellable); - } - } - - private Input input; - private Output output; - public override InputStream input_stream { get { return input; } } - public override OutputStream output_stream { get { return output; } } - - private weak Session session; - private IOStream? inner = null; - private string? error = null; - - private bool read_closed = false; - private bool write_closed = false; - - private class OnSetInnerCallback { - public SourceFunc callback; - public int io_priority; - } - - Gee.List callbacks = new ArrayList(); - - public Connection(Session session) { - this.input = new Input(this); - this.output = new Output(this); - this.session = session; - } - - public void set_inner(IOStream inner) { - assert(this.inner == null); - this.inner = inner; - foreach (OnSetInnerCallback c in callbacks) { - Idle.add((owned) c.callback, c.io_priority); - } - callbacks = null; - } - - public void on_terminated_by_jingle(string reason) { - if (error == null) { - close_async.begin(); - error = reason; - } - } - - private void check_for_errors() throws IOError { - if (error != null) { - throw new IOError.CLOSED(error); - } - } - private async void wait_and_check_for_errors(int io_priority, Cancellable? cancellable = null) throws IOError { - while (true) { - check_for_errors(); - if (inner != null) { - return; - } - SourceFunc callback = wait_and_check_for_errors.callback; - ulong id = 0; - if (cancellable != null) { - id = cancellable.connect(() => callback()); - } - callbacks.add(new OnSetInnerCallback() { callback=(owned)callback, io_priority=io_priority}); - yield; - if (cancellable != null) { - cancellable.disconnect(id); - } - } - } - private void handle_connection_error(IOError error) { - Session? strong = session; - if (strong != null) { - strong.on_connection_error(error); - } - } - private void handle_connection_close() { - Session? strong = session; - if (strong != null) { - strong.on_connection_close(); - } - } - - public async ssize_t read_async(uint8[]? buffer, int io_priority = GLib.Priority.DEFAULT, Cancellable? cancellable = null) throws IOError { - yield wait_and_check_for_errors(io_priority, cancellable); - try { - return yield inner.input_stream.read_async(buffer, io_priority, cancellable); - } catch (IOError e) { - handle_connection_error(e); - throw e; - } - } - public async ssize_t write_async(uint8[]? buffer, int io_priority = GLib.Priority.DEFAULT, Cancellable? cancellable = null) throws IOError { - yield wait_and_check_for_errors(io_priority, cancellable); - try { - return yield inner.output_stream.write_async(buffer, io_priority, cancellable); - } catch (IOError e) { - handle_connection_error(e); - throw e; - } - } - public bool close_read(Cancellable? cancellable = null) throws IOError { - check_for_errors(); - if (read_closed) { - return true; - } - close_read_async.begin(GLib.Priority.DEFAULT, cancellable); - return true; - } - public async bool close_read_async(int io_priority = GLib.Priority.DEFAULT, Cancellable? cancellable = null) throws IOError { - debug("Closing Jingle input stream"); - yield wait_and_check_for_errors(io_priority, cancellable); - if (read_closed) { - return true; - } - read_closed = true; - IOError error = null; - bool result = true; - try { - result = yield inner.input_stream.close_async(io_priority, cancellable); - } catch (IOError e) { - if (error == null) { - error = e; - } - } - try { - result = (yield close_if_both_closed(io_priority, cancellable)) && result; - } catch (IOError e) { - if (error == null) { - error = e; - } - } - if (error != null) { - handle_connection_error(error); - throw error; - } - return result; - } - public bool close_write(Cancellable? cancellable = null) throws IOError { - check_for_errors(); - if (write_closed) { - return true; - } - close_write_async.begin(GLib.Priority.DEFAULT, cancellable); - return true; - } - public async bool close_write_async(int io_priority = GLib.Priority.DEFAULT, Cancellable? cancellable = null) throws IOError { - yield wait_and_check_for_errors(io_priority, cancellable); - if (write_closed) { - return true; - } - write_closed = true; - IOError error = null; - bool result = true; - try { - result = yield inner.output_stream.close_async(io_priority, cancellable); - } catch (IOError e) { - if (error == null) { - error = e; - } - } - try { - result = (yield close_if_both_closed(io_priority, cancellable)) && result; - } catch (IOError e) { - if (error == null) { - error = e; - } - } - if (error != null) { - handle_connection_error(error); - throw error; - } - return result; - } - private async bool close_if_both_closed(int io_priority, Cancellable? cancellable = null) throws IOError { - if (read_closed && write_closed) { - handle_connection_close(); - //return yield inner.close_async(io_priority, cancellable); - } - return true; - } -} - -public class Flag : XmppStreamFlag { - public static FlagIdentity IDENTITY = new FlagIdentity(NS_URI, "jingle"); - - private HashMap sessions = new HashMap(); - - public void add_session(Session session) { - sessions[session.sid] = session; - } - public Session? get_session(string sid) { - return sessions.has_key(sid) ? sessions[sid] : null; - } - public void remove_session(string sid) { - sessions.unset(sid); - } - - public override string get_ns() { return NS_URI; } - public override string get_id() { return IDENTITY.id; } -} - -} diff --git a/xmpp-vala/src/module/xep/0166_jingle/component.vala b/xmpp-vala/src/module/xep/0166_jingle/component.vala new file mode 100644 index 00000000..544bcd69 --- /dev/null +++ b/xmpp-vala/src/module/xep/0166_jingle/component.vala @@ -0,0 +1,52 @@ +namespace Xmpp.Xep.Jingle { + + public abstract class ComponentConnection : Object { + public uint8 component_id { get; set; default = 0; } + public abstract async void terminate(bool we_terminated, string? reason_name = null, string? reason_text = null); + public signal void connection_closed(); + public signal void connection_error(IOError e); + } + + public abstract class DatagramConnection : ComponentConnection { + public bool ready { get; set; default = false; } + private string? terminate_reason_name = null; + private string? terminate_reason_text = null; + private bool terminated = false; + + public override async void terminate(bool we_terminated, string? reason_string = null, string? reason_text = null) { + if (!terminated) { + terminated = true; + terminate_reason_name = reason_string; + terminate_reason_text = reason_text; + connection_closed(); + } + } + + public signal void datagram_received(Bytes datagram); + public abstract void send_datagram(Bytes datagram); + } + + public class StreamingConnection : ComponentConnection { + public Gee.Future stream { get { return promise.future; } } + protected Gee.Promise promise = new Gee.Promise(); + private string? terminated = null; + + public async void init(IOStream stream) { + assert(!this.stream.ready); + promise.set_value(stream); + if (terminated != null) { + yield stream.close_async(); + } + } + + public override async void terminate(bool we_terminated, string? reason_name = null, string? reason_text = null) { + if (terminated == null) { + terminated = (reason_name ?? "") + " - " + (reason_text ?? "") + @"we terminated? $we_terminated"; + if (stream.ready) { + yield stream.value.close_async(); + } + } + } + } +} + diff --git a/xmpp-vala/src/module/xep/0166_jingle/content.vala b/xmpp-vala/src/module/xep/0166_jingle/content.vala new file mode 100644 index 00000000..67c13dd8 --- /dev/null +++ b/xmpp-vala/src/module/xep/0166_jingle/content.vala @@ -0,0 +1,236 @@ +using Gee; +using Xmpp; + +public class Xmpp.Xep.Jingle.Content : Object { + + public signal void senders_modify_incoming(Senders proposed_senders); + + // INITIATE_SENT -> CONNECTING -> [REPLACING_TRANSPORT -> CONNECTING ->]... ACTIVE -> ENDED + // INITIATE_RECEIVED -> CONNECTING -> [WAITING_FOR_TRANSPORT_REPLACE -> CONNECTING ->].. ACTIVE -> ENDED + public enum State { + PENDING, + WANTS_TO_BE_ACCEPTED, + ACCEPTED, + REPLACING_TRANSPORT, + WAITING_FOR_TRANSPORT_REPLACE + } + + public State state { get; set; } + + public Role role { 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; } + public string content_name { get; private set; } + public Senders senders { get; private set; } + + public ContentType content_type; + public ContentParameters content_params; + public Transport transport; + public TransportParameters? transport_params; + public SecurityPrecondition security_precondition; + public SecurityParameters? security_params; + + public weak Session session; + public Map component_connections = new HashMap(); // TODO private + + // INITIATE_SENT | INITIATE_RECEIVED | CONNECTING + public Set tried_transport_methods = new HashSet(); + + + public Content.initiate_sent(string content_name, Senders senders, + ContentType content_type, ContentParameters content_params, + Transport transport, TransportParameters? transport_params, + SecurityPrecondition? security_precondition, SecurityParameters? security_params, + Jid local_full_jid, Jid peer_full_jid) { + this.content_name = content_name; + this.senders = senders; + this.role = Role.INITIATOR; + this.local_full_jid = local_full_jid; + this.peer_full_jid = peer_full_jid; + this.content_creator = Role.INITIATOR; + + this.content_type = content_type; + this.content_params = content_params; + this.transport = transport; + this.transport_params = transport_params; + this.security_precondition = security_precondition; + this.security_params = security_params; + + this.tried_transport_methods.add(transport.ns_uri); + + state = State.PENDING; + } + + public Content.initiate_received(string content_name, Senders senders, + ContentType content_type, ContentParameters content_params, + Transport transport, TransportParameters? transport_params, + SecurityPrecondition? security_precondition, SecurityParameters? security_params, + Jid local_full_jid, Jid peer_full_jid) throws Error { + this.content_name = content_name; + this.senders = senders; + this.role = Role.RESPONDER; + this.local_full_jid = local_full_jid; + this.peer_full_jid = peer_full_jid; + this.content_creator = Role.INITIATOR; + + this.content_type = content_type; + this.content_params = content_params; + this.transport = transport; + this.transport_params = transport_params; + this.security_precondition = security_precondition; + this.security_params = security_params; + + if (transport != null) { + this.tried_transport_methods.add(transport.ns_uri); + } + + state = State.PENDING; + } + + public void set_session(Session session) { + this.session = session; + this.transport_params.set_content(this); + } + + public void accept() { + state = State.WANTS_TO_BE_ACCEPTED; + + session.accept_content(this); + } + + public void reject() { + session.reject_content(this); + } + + public void terminate(bool we_terminated, string? reason_name, string? reason_text) { + content_params.terminate(we_terminated, reason_name, reason_text); + + foreach (ComponentConnection connection in component_connections.values) { + connection.terminate(we_terminated, reason_name, reason_text); + } + } + + public void modify(Senders new_sender) { + session.send_content_modify(this, new_sender); + this.senders = new_sender; + } + + public void accept_content_modify(Senders senders) { + this.senders = senders; + } + + internal void handle_content_modify(XmppStream stream, Senders proposed_senders) { + senders_modify_incoming(proposed_senders); + } + + internal void on_accept(XmppStream stream) { + this.transport_params.create_transport_connection(stream, this); + this.content_params.accept(stream, session, this); + } + + internal void handle_accept(XmppStream stream, ContentNode content_node) { + this.transport_params.handle_transport_accept(content_node.transport); + this.transport_params.create_transport_connection(stream, this); + this.content_params.handle_accept(stream, this.session, this, content_node.description); + } + + private async void select_new_transport() { + XmppStream stream = session.stream; + Transport? new_transport = yield stream.get_module(Module.IDENTITY).select_transport(stream, transport.type_, transport_params.components, peer_full_jid, tried_transport_methods); + if (new_transport == null) { + session.terminate(ReasonElement.FAILED_TRANSPORT, null, "failed transport"); + // TODO should we only terminate this content or really the whole session? + return; + } + tried_transport_methods.add(new_transport.ns_uri); + transport_params = new_transport.create_transport_parameters(stream, transport_params.components, local_full_jid, peer_full_jid); + set_transport_params(transport_params); + session.send_transport_replace(this, transport_params); + state = State.REPLACING_TRANSPORT; + } + + public 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.ns_uri) { + throw new IqError.BAD_REQUEST("transport-accept with unnegotiated transport method"); + } + transport_params.handle_transport_accept(transport_node); + stream.get_module(Iq.Module.IDENTITY).send_iq(stream, new Iq.Stanza.result(iq)); + transport_params.create_transport_connection(stream, this); + } + + public 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.begin(); + } + + public 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, content_type.required_components, 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) { + session.send_transport_reject(this, transport_node); + return; + } + set_transport_params(parameters); + session.send_transport_accept(this, parameters); + + this.transport_params.create_transport_connection(stream, this); + } + + public void handle_transport_info(XmppStream stream, StanzaNode transport, StanzaNode jingle, Iq.Stanza iq) throws IqError { + this.transport_params.handle_transport_info(transport); + stream.get_module(Iq.Module.IDENTITY).send_iq(stream, new Iq.Stanza.result(iq)); + } + + public void on_description_info(XmppStream stream, StanzaNode description, StanzaNode jinglq, Iq.Stanza iq) throws IqError { + // TODO: do something. + stream.get_module(Iq.Module.IDENTITY).send_iq(stream, new Iq.Stanza.result(iq)); + } + + void verify_content(ContentNode content) throws IqError { + if (content.name != content_name || content.creator != content_creator) { + throw new IqError.BAD_REQUEST("unknown content"); + } + } + + public void set_transport_connection(ComponentConnection? conn, uint8 component = 1) { + debug(@"set_transport_connection: %s, %s, %i, %s, overwrites: %s", this.content_name, this.state.to_string(), component, (conn != null).to_string(), component_connections.has_key(component).to_string()); + + if (conn != null) { + component_connections[component] = conn; + if (transport_params.components == component) { + state = State.ACCEPTED; + tried_transport_methods.clear(); + } + } else { + if (role == Role.INITIATOR) { + select_new_transport.begin(); + } else { + state = State.WAITING_FOR_TRANSPORT_REPLACE; + } + } + } + + private void set_transport_params(TransportParameters transport_params) { + this.transport_params = transport_params; + } + + public ComponentConnection? get_transport_connection(uint8 component = 1) { + return component_connections[component]; + } + + public void send_transport_info(StanzaNode transport) { + session.send_transport_info(this, transport); + } +} \ No newline at end of file diff --git a/xmpp-vala/src/module/xep/0166_jingle/content_description.vala b/xmpp-vala/src/module/xep/0166_jingle/content_description.vala new file mode 100644 index 00000000..1a24e52e --- /dev/null +++ b/xmpp-vala/src/module/xep/0166_jingle/content_description.vala @@ -0,0 +1,27 @@ +using Gee; +using Xmpp.Xep; +using Xmpp; + +namespace Xmpp.Xep.Jingle { + + public interface ContentType : Object { + public abstract string ns_uri { get; } + public abstract TransportType required_transport_type { get; } + public abstract uint8 required_components { get; } + public abstract ContentParameters parse_content_parameters(StanzaNode description) throws IqError; + } + + public interface ContentParameters : Object { + /** Called when the counterpart proposes the content */ + public abstract async void handle_proposed_content(XmppStream stream, Jingle.Session session, Content content); + + /** Called when we accept the content that was proposed by the counterpart */ + public abstract void accept(XmppStream stream, Jingle.Session session, Jingle.Content content); + /** Called when the counterpart accepts the content that was proposed by us*/ + public abstract void handle_accept(XmppStream stream, Jingle.Session session, Jingle.Content content, StanzaNode description_node); + + public abstract void terminate(bool we_terminated, string? reason_name, string? reason_text); + + public abstract StanzaNode get_description_node(); + } +} \ No newline at end of file diff --git a/xmpp-vala/src/module/xep/0166_jingle/content_node.vala b/xmpp-vala/src/module/xep/0166_jingle/content_node.vala new file mode 100644 index 00000000..7d8d56c8 --- /dev/null +++ b/xmpp-vala/src/module/xep/0166_jingle/content_node.vala @@ -0,0 +1,112 @@ +using Gee; +using Xmpp.Xep; +using Xmpp; + +namespace Xmpp.Xep.Jingle { + + class ContentNode { + public Role creator; + public string name; + public Senders senders; + public StanzaNode? description; + public StanzaNode? transport; + public StanzaNode? security; + } + + [Version(deprecated = true)] + ContentNode get_single_content_node(StanzaNode jingle) throws IqError { + Gee.List contents = jingle.get_subnodes("content"); + if (contents.size == 0) { + throw new IqError.BAD_REQUEST("missing content node"); + } + if (contents.size > 1) { + throw new IqError.NOT_IMPLEMENTED("can't process multiple content nodes"); + } + StanzaNode content = contents[0]; + string? creator_str = content.get_attribute("creator"); + // Vala can't typecheck the ternary operator here. + Role? creator = null; + if (creator_str != null) { + creator = Role.parse(creator_str); + } else { + // TODO(hrxi): now, is the creator attribute optional or not (XEP-0166 + // Jingle)? + creator = Role.INITIATOR; + } + + string? name = content.get_attribute("name"); + + Senders senders = Senders.parse(content.get_attribute("senders")); + + StanzaNode? description = get_single_node_anyns(content, "description"); + StanzaNode? transport = get_single_node_anyns(content, "transport"); + StanzaNode? security = get_single_node_anyns(content, "security"); + if (name == null || creator == null) { + throw new IqError.BAD_REQUEST("missing name or creator"); + } + + return new ContentNode() { + creator=creator, + name=name, + senders=senders, + description=description, + transport=transport, + security=security + }; + } + + Gee.List get_content_nodes(StanzaNode jingle) throws IqError { + Gee.List contents = jingle.get_subnodes("content"); + if (contents.size == 0) { + throw new IqError.BAD_REQUEST("missing content node"); + } + Gee.List list = new ArrayList(); + foreach (StanzaNode content in contents) { + string? creator_str = content.get_attribute("creator"); + // Vala can't typecheck the ternary operator here. + Role? creator = null; + if (creator_str != null) { + creator = Role.parse(creator_str); + } else { + // TODO(hrxi): now, is the creator attribute optional or not (XEP-0166 + // Jingle)? + creator = Role.INITIATOR; + } + + string? name = content.get_attribute("name"); + Senders senders = Senders.parse(content.get_attribute("senders")); + StanzaNode? description = get_single_node_anyns(content, "description"); + StanzaNode? transport = get_single_node_anyns(content, "transport"); + StanzaNode? security = get_single_node_anyns(content, "security"); + if (name == null || creator == null) { + throw new IqError.BAD_REQUEST("missing name or creator"); + } + list.add(new ContentNode() { + creator=creator, + name=name, + senders=senders, + description=description, + transport=transport, + security=security + }); + } + return list; + } + + 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 (node_name == null || child.name == node_name) { + if (result != null) { + if (node_name != null) { + throw new IqError.BAD_REQUEST(@"multiple $(node_name) nodes"); + } else { + throw new IqError.BAD_REQUEST(@"expected single subnode"); + } + } + result = child; + } + } + return result; + } +} \ No newline at end of file diff --git a/xmpp-vala/src/module/xep/0166_jingle/content_security.vala b/xmpp-vala/src/module/xep/0166_jingle/content_security.vala new file mode 100644 index 00000000..0e10311d --- /dev/null +++ b/xmpp-vala/src/module/xep/0166_jingle/content_security.vala @@ -0,0 +1,18 @@ +using Gee; +using Xmpp.Xep; +using Xmpp; + +namespace Xmpp.Xep.Jingle { + + public interface SecurityPrecondition : Object { + public abstract string security_ns_uri(); + public abstract SecurityParameters? create_security_parameters(XmppStream stream, Jid local_full_jid, Jid peer_full_jid, Object options) throws Jingle.Error; + public abstract SecurityParameters? parse_security_parameters(XmppStream stream, Jid local_full_jid, Jid peer_full_jid, StanzaNode security) throws IqError; + } + + public interface SecurityParameters : Object { + public abstract string security_ns_uri(); + public abstract StanzaNode to_security_stanza_node(XmppStream stream, Jid local_full_jid, Jid peer_full_jid); + public abstract IOStream wrap_stream(IOStream stream); + } +} \ No newline at end of file diff --git a/xmpp-vala/src/module/xep/0166_jingle/content_transport.vala b/xmpp-vala/src/module/xep/0166_jingle/content_transport.vala new file mode 100644 index 00000000..cd74c836 --- /dev/null +++ b/xmpp-vala/src/module/xep/0166_jingle/content_transport.vala @@ -0,0 +1,29 @@ +namespace Xmpp.Xep.Jingle { + + public interface Transport : Object { + public abstract string ns_uri { get; } + public async abstract bool is_transport_available(XmppStream stream, uint8 components, Jid full_jid); + public abstract TransportType type_ { get; } + public abstract int priority { get; } + public abstract TransportParameters create_transport_parameters(XmppStream stream, uint8 components, Jid local_full_jid, Jid peer_full_jid) throws Error; + public abstract TransportParameters parse_transport_parameters(XmppStream stream, uint8 components, Jid local_full_jid, Jid peer_full_jid, StanzaNode transport) throws IqError; + } + + public enum TransportType { + DATAGRAM, + STREAMING, + } + + // Gets a null `stream` if connection setup was unsuccessful and another + // transport method should be tried. + public interface TransportParameters : Object { + public abstract string ns_uri { get; } + public abstract uint8 components { get; } + + public abstract void set_content(Content content); + public abstract StanzaNode to_transport_stanza_node(); + public abstract void handle_transport_accept(StanzaNode transport) throws IqError; + public abstract void handle_transport_info(StanzaNode transport) throws IqError; + public abstract void create_transport_connection(XmppStream stream, Content content); + } +} \ No newline at end of file diff --git a/xmpp-vala/src/module/xep/0166_jingle/jingle_flag.vala b/xmpp-vala/src/module/xep/0166_jingle/jingle_flag.vala new file mode 100644 index 00000000..9f0acd27 --- /dev/null +++ b/xmpp-vala/src/module/xep/0166_jingle/jingle_flag.vala @@ -0,0 +1,38 @@ +using Gee; +using Xmpp; + +public class Xmpp.Xep.Jingle.Flag : XmppStreamFlag { + public static FlagIdentity IDENTITY = new FlagIdentity(NS_URI, "jingle"); + + public HashMap sessions = new HashMap(); + public HashMap> promises = new HashMap>(); + + // We might get transport-infos about a session before we finished fully creating the session. (e.g. telepathy outgoing calls) + // Thus, we "pre add" the session as soon as possible and can then await it. + public void pre_add_session(string sid) { + var promise = new Promise(); + promises[sid] = promise; + } + + public void add_session(Session session) { + if (promises.has_key(session.sid)) { + promises[session.sid].set_value(session); + promises.unset(session.sid); + } + sessions[session.sid] = session; + } + + public async Session? get_session(string sid) { + if (promises.has_key(sid)) { + return yield promises[sid].future.wait_async(); + } + return sessions[sid]; + } + + public void remove_session(string sid) { + sessions.unset(sid); + } + + public override string get_ns() { return NS_URI; } + public override string get_id() { return IDENTITY.id; } +} \ No newline at end of file diff --git a/xmpp-vala/src/module/xep/0166_jingle/jingle_module.vala b/xmpp-vala/src/module/xep/0166_jingle/jingle_module.vala new file mode 100644 index 00000000..1e8a36d1 --- /dev/null +++ b/xmpp-vala/src/module/xep/0166_jingle/jingle_module.vala @@ -0,0 +1,235 @@ +using Gee; +using Xmpp; + +namespace Xmpp.Xep.Jingle { + + internal const string NS_URI = "urn:xmpp:jingle:1"; + private const string ERROR_NS_URI = "urn:xmpp:jingle:errors:1"; + + // This module can only be attached to one stream at a time. + public class Module : XmppStreamModule, Iq.Handler { + public static Xmpp.ModuleIdentity IDENTITY = new Xmpp.ModuleIdentity(NS_URI, "0166_jingle"); + + public signal void session_initiate_received(XmppStream stream, Session session); + + private HashMap content_types = new HashMap(); + private HashMap session_info_types = new HashMap(); + private HashMap transports = new HashMap(); + private HashMap security_preconditions = new HashMap(); + + public override void attach(XmppStream stream) { + stream.add_flag(new Flag()); + stream.get_module(ServiceDiscovery.Module.IDENTITY).add_feature(stream, NS_URI); + stream.get_module(Iq.Module.IDENTITY).register_for_namespace(NS_URI, this); + + // TODO update stream in all sessions + } + + public override void detach(XmppStream stream) { + stream.get_module(ServiceDiscovery.Module.IDENTITY).remove_feature(stream, NS_URI); + stream.get_module(Iq.Module.IDENTITY).unregister_from_namespace(NS_URI, this); + } + + public void register_content_type(ContentType content_type) { + content_types[content_type.ns_uri] = content_type; + } + + public void register_session_info_type(SessionInfoNs info_ns) { + session_info_types[info_ns.ns_uri] = info_ns; + } + + public ContentType? get_content_type(string ns_uri) { + if (!content_types.has_key(ns_uri)) { + return null; + } + return content_types[ns_uri]; + } + + public SessionInfoNs? get_session_info_type(string ns_uri) { + return session_info_types[ns_uri]; + } + + public void register_transport(Transport transport) { + transports[transport.ns_uri] = transport; + } + + public Transport? get_transport(string ns_uri) { + if (!transports.has_key(ns_uri)) { + return null; + } + return transports[ns_uri]; + } + + public async Transport? select_transport(XmppStream stream, TransportType type, uint8 components, Jid receiver_full_jid, Set blacklist) { + Transport? result = null; + foreach (Transport transport in transports.values) { + if (transport.type_ != type) { + continue; + } + if (transport.ns_uri in blacklist) { + continue; + } + if (yield transport.is_transport_available(stream, components, receiver_full_jid)) { + if (result != null) { + if (result.priority >= transport.priority) { + continue; + } + } + result = transport; + } + } + return result; + } + + public void register_security_precondition(SecurityPrecondition precondition) { + security_preconditions[precondition.security_ns_uri()] = precondition; + } + + public SecurityPrecondition? get_security_precondition(string? ns_uri) { + if (ns_uri == null) return null; + if (!security_preconditions.has_key(ns_uri)) { + return null; + } + return security_preconditions[ns_uri]; + } + + private async bool is_jingle_available(XmppStream stream, Jid full_jid) { + bool? has_jingle = yield stream.get_module(ServiceDiscovery.Module.IDENTITY).has_entity_feature(stream, full_jid, NS_URI); + return has_jingle != null && has_jingle; + } + + public async bool is_available(XmppStream stream, TransportType type, uint8 components, Jid full_jid) { + return (yield is_jingle_available(stream, full_jid)) && (yield select_transport(stream, type, components, full_jid, Set.empty())) != null; + } + + public async Session create_session(XmppStream stream, Gee.List contents, Jid receiver_full_jid, string sid = random_uuid()) throws Error { + if (!yield is_jingle_available(stream, receiver_full_jid)) { + throw new Error.NO_SHARED_PROTOCOLS("No Jingle support"); + } + Jid? my_jid = stream.get_flag(Bind.Flag.IDENTITY).my_jid; + if (my_jid == null) { + throw new Error.GENERAL("Couldn't determine own JID"); + } + + Session session = new Session.initiate_sent(stream, sid, my_jid, receiver_full_jid); + session.terminated.connect((session, stream, _1, _2, _3) => { stream.get_flag(Flag.IDENTITY).remove_session(session.sid); }); + + foreach (Content content in contents) { + session.insert_content(content); + } + + // Build & send session-initiate iq stanza + StanzaNode initiate_jingle_iq = new StanzaNode.build("jingle", NS_URI) + .add_self_xmlns() + .put_attribute("action", "session-initiate") + .put_attribute("initiator", my_jid.to_string()) + .put_attribute("sid", session.sid); + + foreach (Content content in contents) { + StanzaNode content_node = new StanzaNode.build("content", NS_URI) + .put_attribute("creator", "initiator") + .put_attribute("name", content.content_name) + .put_attribute("senders", content.senders.to_string()) + .put_node(content.content_params.get_description_node()) + .put_node(content.transport_params.to_transport_stanza_node()); + if (content.security_params != null) { + content_node.put_node(content.security_params.to_security_stanza_node(stream, my_jid, receiver_full_jid)); + } + initiate_jingle_iq.put_node(content_node); + } + + Iq.Stanza iq = new Iq.Stanza.set(initiate_jingle_iq) { to=receiver_full_jid }; + + stream.get_flag(Flag.IDENTITY).add_session(session); + // We might get a follow-up before the ack => add_session before send_iq returns + stream.get_module(Iq.Module.IDENTITY).send_iq(stream, iq, (stream, iq) => { + if (iq.is_error()) warning("Jingle session-initiate got error: %s", iq.stanza.to_string()); + }); + + return session; + } + + public async void handle_session_initiate(XmppStream stream, string sid, StanzaNode jingle, Iq.Stanza iq) throws IqError { + Jid? my_jid = stream.get_flag(Bind.Flag.IDENTITY).my_jid; + if (my_jid == null) { + throw new IqError.RESOURCE_CONSTRAINT("Couldn't determine own JID"); + } + + Session session = new Session.initiate_received(stream, sid, my_jid, iq.from); + session.terminated.connect((stream) => { stream.get_flag(Flag.IDENTITY).remove_session(sid); }); + + stream.get_flag(Flag.IDENTITY).pre_add_session(session.sid); + + foreach (ContentNode content_node in get_content_nodes(jingle)) { + yield session.insert_content_node(content_node, iq.from); + } + + stream.get_flag(Flag.IDENTITY).add_session(session); + + stream.get_module(Iq.Module.IDENTITY).send_iq(stream, new Iq.Stanza.result(iq)); + + session_initiate_received(stream, session); + } + + public async void on_iq_set(XmppStream stream, Iq.Stanza iq) { + try { + yield handle_iq_set(stream, iq); + } catch (IqError e) { + send_iq_error(e, stream, iq); + } + } + + public async void handle_iq_set(XmppStream stream, Iq.Stanza iq) throws IqError { + StanzaNode? jingle_node = iq.stanza.get_subnode("jingle", NS_URI); + if (jingle_node == null) { + throw new IqError.BAD_REQUEST("missing jingle node"); + } + string? sid = jingle_node.get_attribute("sid"); + string? action = jingle_node.get_attribute("action"); + if (sid == null || action == null) { + throw new IqError.BAD_REQUEST("missing jingle node, sid or action"); + } + + Session? session = yield stream.get_flag(Flag.IDENTITY).get_session(sid); + if (action == "session-initiate") { + if (session != null) { + stream.get_module(Iq.Module.IDENTITY).send_iq(stream, new Iq.Stanza.error(iq, new ErrorStanza.build(ErrorStanza.TYPE_MODIFY, ErrorStanza.CONDITION_CONFLICT, "session ID already in use", null)) { to=iq.from }); + return; + } + yield handle_session_initiate(stream, sid, jingle_node, iq); + return; + } + if (session == null) { + StanzaNode unknown_session = new StanzaNode.build("unknown-session", ERROR_NS_URI).add_self_xmlns(); + stream.get_module(Iq.Module.IDENTITY).send_iq(stream, new Iq.Stanza.error(iq, new ErrorStanza.item_not_found(unknown_session)) { to=iq.from }); + return; + } + session.handle_iq_set(action, jingle_node, iq); + } + + public override string get_ns() { return NS_URI; } + public override string get_id() { return IDENTITY.id; } + } + + void send_iq_error(IqError iq_error, XmppStream stream, Iq.Stanza iq) { + ErrorStanza error; + if (iq_error is IqError.BAD_REQUEST) { + error = new ErrorStanza.bad_request(iq_error.message); + } else if (iq_error is IqError.NOT_ACCEPTABLE) { + error = new ErrorStanza.not_acceptable(iq_error.message); + } else if (iq_error is IqError.NOT_IMPLEMENTED) { + error = new ErrorStanza.feature_not_implemented(iq_error.message); + } else if (iq_error is IqError.UNSUPPORTED_INFO) { + StanzaNode unsupported_info = new StanzaNode.build("unsupported-info", ERROR_NS_URI).add_self_xmlns(); + error = new ErrorStanza.build(ErrorStanza.TYPE_CANCEL, ErrorStanza.CONDITION_FEATURE_NOT_IMPLEMENTED, iq_error.message, unsupported_info); + } else if (iq_error is IqError.OUT_OF_ORDER) { + StanzaNode out_of_order = new StanzaNode.build("out-of-order", ERROR_NS_URI).add_self_xmlns(); + error = new ErrorStanza.build(ErrorStanza.TYPE_MODIFY, ErrorStanza.CONDITION_UNEXPECTED_REQUEST, iq_error.message, out_of_order); + } else if (iq_error is IqError.RESOURCE_CONSTRAINT) { + error = new ErrorStanza.resource_constraint(iq_error.message); + } else { + assert_not_reached(); + } + stream.get_module(Iq.Module.IDENTITY).send_iq(stream, new Iq.Stanza.error(iq, error) { to=iq.from }); + } +} \ No newline at end of file diff --git a/xmpp-vala/src/module/xep/0166_jingle/jingle_structs.vala b/xmpp-vala/src/module/xep/0166_jingle/jingle_structs.vala new file mode 100644 index 00000000..0f283e0e --- /dev/null +++ b/xmpp-vala/src/module/xep/0166_jingle/jingle_structs.vala @@ -0,0 +1,73 @@ +using Gee; +using Xmpp; + +namespace Xmpp.Xep.Jingle { + + public errordomain IqError { + BAD_REQUEST, + NOT_ACCEPTABLE, + NOT_IMPLEMENTED, + UNSUPPORTED_INFO, + OUT_OF_ORDER, + RESOURCE_CONSTRAINT, + } + + public errordomain Error { + GENERAL, + BAD_REQUEST, + INVALID_PARAMETERS, + UNSUPPORTED_TRANSPORT, + UNSUPPORTED_SECURITY, + NO_SHARED_PROTOCOLS, + TRANSPORT_ERROR, + } + + public enum Senders { + BOTH, + INITIATOR, + NONE, + RESPONDER; + + public string to_string() { + switch (this) { + case BOTH: return "both"; + case INITIATOR: return "initiator"; + case NONE: return "none"; + case RESPONDER: return "responder"; + } + assert_not_reached(); + } + + public static Senders parse(string? senders) throws IqError { + if (senders == null) return Senders.BOTH; + switch (senders) { + case "initiator": return Senders.INITIATOR; + case "responder": return Senders.RESPONDER; + case "both": return Senders.BOTH; + } + throw new IqError.BAD_REQUEST(@"invalid role $(senders)"); + } + } + + public enum Role { + INITIATOR, + RESPONDER; + + public string to_string() { + switch (this) { + case INITIATOR: return "initiator"; + case RESPONDER: return "responder"; + } + assert_not_reached(); + } + + public static Role parse(string role) throws IqError { + switch (role) { + case "initiator": return INITIATOR; + case "responder": return RESPONDER; + } + throw new IqError.BAD_REQUEST(@"invalid role $(role)"); + } + } + +} \ No newline at end of file diff --git a/xmpp-vala/src/module/xep/0166_jingle/reason_element.vala b/xmpp-vala/src/module/xep/0166_jingle/reason_element.vala new file mode 100644 index 00000000..1cbdf936 --- /dev/null +++ b/xmpp-vala/src/module/xep/0166_jingle/reason_element.vala @@ -0,0 +1,29 @@ +using Gee; +using Xmpp; + +namespace Xmpp.Xep.Jingle.ReasonElement { + public const string ALTERNATIVE_SESSION = "alternative-session"; + public const string BUSY = "busy"; + public const string CANCEL = "cancel"; + public const string CONNECTIVITY_ERROR = "connectivity-error"; + public const string DECLINE = "decline"; + public const string EXPIRED = "expired"; + public const string FAILED_APPLICATION = "failed_application"; + public const string FAILED_TRANSPORT = "failed_transport"; + public const string GENERAL_ERROR = "general-error"; + public const string GONE = "gone"; + public const string INCOMPATIBLE_PARAMETERS = "incompatible-parameters"; + public const string MEDIA_ERROR = "media-error"; + public const string SECURITY_ERROR = "security-error"; + public const string SUCCESS = "success"; + public const string TIMEOUT = "timeout"; + public const string UNSUPPORTED_APPLICATIONS = "unsupported-applications"; + public const string UNSUPPORTED_TRANSPORTS = "unsupported-transports"; + + public const string[] NORMAL_TERMINATE_REASONS = { + BUSY, + CANCEL, + DECLINE, + SUCCESS + }; +} \ No newline at end of file diff --git a/xmpp-vala/src/module/xep/0166_jingle/session.vala b/xmpp-vala/src/module/xep/0166_jingle/session.vala new file mode 100644 index 00000000..e9ad9169 --- /dev/null +++ b/xmpp-vala/src/module/xep/0166_jingle/session.vala @@ -0,0 +1,559 @@ +using Gee; +using Xmpp; + + +public delegate void Xmpp.Xep.Jingle.SessionTerminate(Jid to, string sid, StanzaNode reason); + +public class Xmpp.Xep.Jingle.Session : Object { + + public signal void terminated(XmppStream stream, bool we_terminated, string? reason_name, string? reason_text); + public signal void additional_content_add_incoming(XmppStream stream, Content content); + + // INITIATE_SENT/INITIATE_RECEIVED -> CONNECTING -> PENDING -> ACTIVE -> ENDED + public enum State { + INITIATE_SENT, + INITIATE_RECEIVED, + ACTIVE, + ENDED, + } + + public XmppStream stream { get; set; } + public State state { get; set; } + public string sid { get; private set; } + public Jid local_full_jid { get; private set; } + public Jid peer_full_jid { get; private set; } + public bool we_initiated { get; private set; } + + public HashMap contents = new HashMap(); + + public SecurityParameters? security { get { return contents.values.to_array()[0].security_params; } } + + public Session.initiate_sent(XmppStream stream, string sid, Jid local_full_jid, Jid peer_full_jid) { + this.stream = stream; + this.sid = sid; + this.local_full_jid = local_full_jid; + this.peer_full_jid = peer_full_jid; + this.state = State.INITIATE_SENT; + this.we_initiated = true; + } + + public Session.initiate_received(XmppStream stream, string sid, Jid local_full_jid, Jid peer_full_jid) { + this.stream = stream; + this.sid = sid; + this.local_full_jid = local_full_jid; + this.peer_full_jid = peer_full_jid; + this.state = State.INITIATE_RECEIVED; + this.we_initiated = false; + } + + public void handle_iq_set(string action, StanzaNode jingle, Iq.Stanza iq) throws IqError { + + if (action.has_prefix("session-")) { + switch (action) { + case "session-accept": + Gee.List content_nodes = get_content_nodes(jingle); + + if (state != State.INITIATE_SENT) { + throw new IqError.OUT_OF_ORDER("got session-accept while not waiting for one"); + } + handle_session_accept(content_nodes, jingle, iq); + break; + case "session-info": + handle_session_info.begin(jingle, iq); + break; + case "session-terminate": + handle_session_terminate(jingle, iq); + break; + default: + throw new IqError.BAD_REQUEST("invalid action"); + } + + + } else if (action.has_prefix("content-")) { + switch (action) { + case "content-accept": + ContentNode content_node = get_single_content_node(jingle); + handle_content_accept(content_node); + stream.get_module(Iq.Module.IDENTITY).send_iq(stream, new Iq.Stanza.result(iq)); + break; + case "content-add": + ContentNode content_node = get_single_content_node(jingle); + insert_content_node.begin(content_node, peer_full_jid); + stream.get_module(Iq.Module.IDENTITY).send_iq(stream, new Iq.Stanza.result(iq)); + break; + case "content-modify": + handle_content_modify(stream, jingle, iq); + break; + case "content-reject": + case "content-remove": + throw new IqError.NOT_IMPLEMENTED(@"$(action) is not implemented"); + default: + throw new IqError.BAD_REQUEST("invalid action"); + } + + + } else if (action.has_prefix("transport-")) { + ContentNode content_node = get_single_content_node(jingle); + if (!contents.has_key(content_node.name)) { + throw new IqError.BAD_REQUEST("unknown content"); + } + + if (content_node.transport == null) { + throw new IqError.BAD_REQUEST("missing transport node"); + } + + Content content = contents[content_node.name]; + + if (content_node.creator != content.content_creator) { + throw new IqError.BAD_REQUEST("unknown content; creator"); + } + + switch (action) { + case "transport-accept": + content.handle_transport_accept(stream, content_node.transport, jingle, iq); + break; + case "transport-info": + content.handle_transport_info(stream, content_node.transport, jingle, iq); + break; + case "transport-reject": + content.handle_transport_reject(stream, jingle, iq); + break; + case "transport-replace": + content.handle_transport_replace(stream, content_node.transport, jingle, iq); + break; + default: + throw new IqError.BAD_REQUEST("invalid action"); + } + + + } else if (action == "description-info") { + ContentNode content_node = get_single_content_node(jingle); + if (!contents.has_key(content_node.name)) { + throw new IqError.BAD_REQUEST("unknown content"); + } + + Content content = contents[content_node.name]; + + if (content_node.creator != content.content_creator) { + throw new IqError.BAD_REQUEST("unknown content; creator"); + } + + content.on_description_info(stream, content_node.description, jingle, iq); + } else if (action == "security-info") { + throw new IqError.NOT_IMPLEMENTED(@"$(action) is not implemented"); + + + } else { + throw new IqError.BAD_REQUEST("invalid action"); + } + } + + internal void insert_content(Content content) { + this.contents[content.content_name] = content; + content.set_session(this); + } + + internal async void insert_content_node(ContentNode content_node, Jid peer_full_jid) throws IqError { + if (content_node.description == null || content_node.transport == null) { + throw new IqError.BAD_REQUEST("missing description or transport node"); + } + + Jid? my_jid = stream.get_flag(Bind.Flag.IDENTITY).my_jid; + + Transport? transport = stream.get_module(Jingle.Module.IDENTITY).get_transport(content_node.transport.ns_uri); + ContentType? content_type = stream.get_module(Jingle.Module.IDENTITY).get_content_type(content_node.description.ns_uri); + + if (content_type == null) { + // TODO(hrxi): how do we signal an unknown content type? + throw new IqError.NOT_IMPLEMENTED("unknown content type"); + } + + TransportParameters? transport_params = null; + if (transport != null) { + transport_params = transport.parse_transport_parameters(stream, content_type.required_components, my_jid, peer_full_jid, content_node.transport); + } else { + // terminate the session below + } + + ContentParameters content_params = content_type.parse_content_parameters(content_node.description); + + SecurityPrecondition? precondition = content_node.security != null ? stream.get_module(Jingle.Module.IDENTITY).get_security_precondition(content_node.security.ns_uri) : null; + SecurityParameters? security_params = null; + if (precondition != null) { + debug("Using precondition %s", precondition.security_ns_uri()); + security_params = precondition.parse_security_parameters(stream, my_jid, peer_full_jid, content_node.security); + } else if (content_node.security != null) { + throw new IqError.NOT_IMPLEMENTED("unknown security precondition"); + } + + TransportType type = content_type.required_transport_type; + + if (transport == null || transport.type_ != type) { + terminate(ReasonElement.UNSUPPORTED_TRANSPORTS, null, "unsupported transports"); + throw new IqError.NOT_IMPLEMENTED("unsupported transports"); + } + + Content content = new Content.initiate_received(content_node.name, content_node.senders, + content_type, content_params, + transport, transport_params, + precondition, security_params, + my_jid, peer_full_jid); + insert_content(content); + + yield content_params.handle_proposed_content(stream, this, content); + + if (this.state == State.ACTIVE) { + additional_content_add_incoming(stream, content); + } + } + + public async void add_content(Content content) { + content.session = this; + this.contents[content.content_name] = content; + + StanzaNode content_add_node = new StanzaNode.build("jingle", NS_URI) + .add_self_xmlns() + .put_attribute("action", "content-add") + .put_attribute("sid", sid) + .put_node(new StanzaNode.build("content", NS_URI) + .put_attribute("creator", "initiator") + .put_attribute("name", content.content_name) + .put_attribute("senders", content.senders.to_string()) + .put_node(content.content_params.get_description_node()) + .put_node(content.transport_params.to_transport_stanza_node())); + + Iq.Stanza iq = new Iq.Stanza.set(content_add_node) { to=peer_full_jid }; + yield stream.get_module(Iq.Module.IDENTITY).send_iq_async(stream, iq); + } + + private void handle_content_accept(ContentNode content_node) throws IqError { + if (content_node.description == null || content_node.transport == null) throw new IqError.BAD_REQUEST("missing description or transport node"); + if (!contents.has_key(content_node.name)) throw new IqError.BAD_REQUEST("unknown content"); + + Content content = contents[content_node.name]; + + if (content_node.creator != content.content_creator) warning("Counterpart accepts content with an unexpected `creator`"); + if (content_node.senders != content.senders) warning("Counterpart accepts content with an unexpected `senders`"); + if (content_node.transport.ns_uri != content.transport_params.ns_uri) throw new IqError.BAD_REQUEST("session-accept with unnegotiated transport method"); + + content.handle_accept(stream, content_node); + } + + private void handle_content_modify(XmppStream stream, StanzaNode jingle_node, Iq.Stanza iq) throws IqError { + ContentNode content_node = get_single_content_node(jingle_node); + + Content? content = contents[content_node.name]; + + if (content == null) throw new IqError.BAD_REQUEST("no such content"); + if (content_node.creator != content.content_creator) throw new IqError.BAD_REQUEST("mismatching creator"); + + Iq.Stanza result_iq = new Iq.Stanza.result(iq) { to=peer_full_jid }; + stream.get_module(Iq.Module.IDENTITY).send_iq(stream, result_iq); + + content.handle_content_modify(stream, content_node.senders); + } + + private void handle_session_accept(Gee.List content_nodes, StanzaNode jingle, Iq.Stanza iq) throws IqError { + string? responder_str = jingle.get_attribute("responder"); + Jid responder = iq.from; + if (responder_str != null) { + try { + responder = new Jid(responder_str); + } catch (InvalidJidError e) { + warning("Received invalid session accept: %s", e.message); + } + } + // TODO(hrxi): more sanity checking, perhaps replace who we're talking to + if (!responder.is_full()) { + throw new IqError.BAD_REQUEST("invalid responder JID"); + } + foreach (ContentNode content_node in content_nodes) { + handle_content_accept(content_node); + } + stream.get_module(Iq.Module.IDENTITY).send_iq(stream, new Iq.Stanza.result(iq)); + + state = State.ACTIVE; + } + + private void handle_session_terminate(StanzaNode jingle, Iq.Stanza iq) throws IqError { + string? reason_text = null; + string? reason_name = null; + StanzaNode? reason_node = iq.stanza.get_deep_subnode(NS_URI + ":jingle", NS_URI + ":reason"); + if (reason_node != null) { + if (reason_node.sub_nodes.size > 2) warning("Jingle session-terminate reason node w/ >2 subnodes: %s", iq.stanza.to_string()); + + StanzaNode? specific_reason_node = null; + StanzaNode? text_node = null; + foreach (StanzaNode node in reason_node.sub_nodes) { + if (node.name == "text") { + text_node = node; + } else if (node.ns_uri == NS_URI) { + specific_reason_node = node; + } + } + reason_name = specific_reason_node != null ? specific_reason_node.name : null; + reason_text = text_node != null ? text_node.get_string_content() : null; + + if (reason_name != null && !(specific_reason_node.name in ReasonElement.NORMAL_TERMINATE_REASONS)) { + warning("Jingle session terminated: %s : %s", reason_name ?? "", reason_text ?? ""); + } else { + debug("Jingle session terminated: %s : %s", reason_name ?? "", reason_text ?? ""); + } + } + + foreach (Content content in contents.values) { + content.terminate(false, reason_name, reason_text); + } + + stream.get_module(Iq.Module.IDENTITY).send_iq(stream, new Iq.Stanza.result(iq)); + // TODO(hrxi): also handle presence type=unavailable + + state = State.ENDED; + terminated(stream, false, reason_name, reason_text); + } + + private async void handle_session_info(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; + } + SessionInfoNs? info_ns = stream.get_module(Module.IDENTITY).get_session_info_type(info.ns_uri); + if (info_ns == null) { + throw new IqError.UNSUPPORTED_INFO("unknown session-info namespace"); + } + info_ns.handle_content_session_info(stream, this, info, iq); + + Iq.Stanza result_iq = new Iq.Stanza.result(iq) { to=peer_full_jid }; + stream.get_module(Iq.Module.IDENTITY).send_iq(stream, result_iq); + } + + private void accept() { + if (state != State.INITIATE_RECEIVED) critical("Accepting a stream, but we're the initiator"); + + StanzaNode jingle = new StanzaNode.build("jingle", NS_URI) + .add_self_xmlns() + .put_attribute("action", "session-accept") + .put_attribute("sid", sid); + foreach (Content content in contents.values) { + StanzaNode content_node = new StanzaNode.build("content", NS_URI) + .put_attribute("creator", "initiator") + .put_attribute("name", content.content_name) + .put_attribute("senders", content.senders.to_string()) + .put_node(content.content_params.get_description_node()) + .put_node(content.transport_params.to_transport_stanza_node()); + jingle.put_node(content_node); + } + Iq.Stanza iq = new Iq.Stanza.set(jingle) { to=peer_full_jid }; + stream.get_module(Iq.Module.IDENTITY).send_iq(stream, iq); + + + foreach (Content content in contents.values) { + content.on_accept(stream); + } + + state = State.ACTIVE; + } + + internal void accept_content(Content content) { + if (state == State.INITIATE_RECEIVED) { + bool all_accepted = true; + foreach (Content c in contents.values) { + if (c.state != Content.State.WANTS_TO_BE_ACCEPTED) { + all_accepted = false; + } + } + if (all_accepted) { + accept(); + } + } else if (state == State.ACTIVE) { + StanzaNode content_accept_node = new StanzaNode.build("jingle", NS_URI) + .add_self_xmlns() + .put_attribute("action", "content-accept") + .put_attribute("sid", sid) + .put_node(new StanzaNode.build("content", NS_URI) + .put_attribute("creator", "initiator") + .put_attribute("name", content.content_name) + .put_attribute("senders", content.senders.to_string()) + .put_node(content.content_params.get_description_node()) + .put_node(content.transport_params.to_transport_stanza_node())); + + Iq.Stanza iq = new Iq.Stanza.set(content_accept_node) { to=peer_full_jid }; + stream.get_module(Iq.Module.IDENTITY).send_iq(stream, iq); + + content.on_accept(stream); + } + } + + private void reject() { + if (state != State.INITIATE_RECEIVED) critical("Accepting a stream, but we're the initiator"); + terminate(ReasonElement.DECLINE, null, "declined"); + } + + internal void reject_content(Content content) { + if (state == State.INITIATE_RECEIVED) { + reject(); + } else { + warning("not really handeling content rejects"); + } + } + + public void set_application_error(StanzaNode? application_reason = null) { + terminate(ReasonElement.FAILED_APPLICATION, null, "application error"); + } + + public void terminate(string? reason_name, string? reason_text, string? local_reason) { + if (state == State.ENDED) return; + + if (state == State.ACTIVE) { + string reason_str; + if (local_reason != null) { + reason_str = @"local session-terminate: $(local_reason)"; + } else { + reason_str = "local session-terminate"; + } + foreach (Content content in contents.values) { + content.terminate(true, reason_name, reason_text); + } + } + + StanzaNode terminate_iq = new StanzaNode.build("jingle", NS_URI) + .add_self_xmlns() + .put_attribute("action", "session-terminate") + .put_attribute("sid", sid); + if (reason_name != null || reason_text != null) { + StanzaNode reason_node = new StanzaNode.build("reason", NS_URI); + if (reason_name != null) { + reason_node.put_node(new StanzaNode.build(reason_name, NS_URI)); + } + if (reason_text != null) { + reason_node.put_node(new StanzaNode.text(reason_text)); + } + terminate_iq.put_node(reason_node); + } + Iq.Stanza iq = new Iq.Stanza.set(terminate_iq) { to=peer_full_jid }; + stream.get_module(Iq.Module.IDENTITY).send_iq(stream, iq); + + state = State.ENDED; + terminated(stream, true, reason_name, reason_text); + } + + internal void send_session_info(StanzaNode child_node) { + if (state == State.ENDED) return; + + StanzaNode node = new StanzaNode.build("jingle", NS_URI).add_self_xmlns() + .put_attribute("action", "session-info") + .put_attribute("sid", sid) + // TODO put `initiator`? + .put_node(child_node); + Iq.Stanza iq = new Iq.Stanza.set(node) { to=peer_full_jid }; + stream.get_module(Iq.Module.IDENTITY).send_iq(stream, iq); + } + + internal void send_content_modify(Content content, Senders senders) { + if (state == State.ENDED) return; + + StanzaNode node = new StanzaNode.build("jingle", NS_URI).add_self_xmlns() + .put_attribute("action", "content-modify") + .put_attribute("sid", sid) + .put_node(new StanzaNode.build("content", NS_URI) + .put_attribute("creator", content.content_creator.to_string()) + .put_attribute("name", content.content_name) + .put_attribute("senders", senders.to_string())); + Iq.Stanza iq = new Iq.Stanza.set(node) { to=peer_full_jid }; + stream.get_module(Iq.Module.IDENTITY).send_iq(stream, iq); + } + + internal void send_transport_accept(Content content, TransportParameters transport_params) { + if (state == State.ENDED) return; + + 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.content_name) + .put_node(transport_params.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); + } + + internal void send_transport_replace(Content content, TransportParameters transport_params) { + if (state == State.ENDED) return; + + 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.content_name) + .put_node(transport_params.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); + } + + internal void send_transport_reject(Content content, StanzaNode transport_node) { + if (state == State.ENDED) return; + + 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.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); + } + + internal void send_transport_info(Content content, StanzaNode transport) { + if (state == State.ENDED) return; + + StanzaNode jingle = new StanzaNode.build("jingle", NS_URI) + .add_self_xmlns() + .put_attribute("action", "transport-info") + .put_attribute("sid", sid) + .put_node(new StanzaNode.build("content", NS_URI) + .put_attribute("creator", "initiator") + .put_attribute("name", content.content_name) + .put_node(transport) + ); + Iq.Stanza iq = new Iq.Stanza.set(jingle) { to=peer_full_jid }; + stream.get_module(Iq.Module.IDENTITY).send_iq(stream, iq); + } + + public bool senders_include_us(Senders senders) { + switch (senders) { + case Senders.BOTH: + return true; + case Senders.NONE: + return false; + case Senders.INITIATOR: + return we_initiated; + case Senders.RESPONDER: + return !we_initiated; + } + assert_not_reached(); + } + + public bool senders_include_counterpart(Senders senders) { + switch (senders) { + case Senders.BOTH: + return true; + case Senders.NONE: + return false; + case Senders.INITIATOR: + return !we_initiated; + case Senders.RESPONDER: + return we_initiated; + } + assert_not_reached(); + } +} \ No newline at end of file diff --git a/xmpp-vala/src/module/xep/0166_jingle/session_info.vala b/xmpp-vala/src/module/xep/0166_jingle/session_info.vala new file mode 100644 index 00000000..fcf7584f --- /dev/null +++ b/xmpp-vala/src/module/xep/0166_jingle/session_info.vala @@ -0,0 +1,12 @@ +using Gee; +using Xmpp.Xep; +using Xmpp; + +namespace Xmpp.Xep.Jingle { + + public interface SessionInfoNs : Object { + public abstract string ns_uri { get; } + + public abstract void handle_content_session_info(XmppStream stream, Session session, StanzaNode info, Iq.Stanza iq) throws IqError; + } +} \ No newline at end of file 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 1c0323be..07b158bc 100644 --- a/xmpp-vala/src/module/xep/0234_jingle_file_transfer.vala +++ b/xmpp-vala/src/module/xep/0234_jingle_file_transfer.vala @@ -7,50 +7,42 @@ namespace Xmpp.Xep.JingleFileTransfer { private const string NS_URI = "urn:xmpp:jingle:apps:file-transfer:5"; public class Module : Jingle.ContentType, XmppStreamModule { + + public signal void file_incoming(XmppStream stream, FileTransfer file_transfer); + public static Xmpp.ModuleIdentity IDENTITY = new Xmpp.ModuleIdentity(NS_URI, "0234_jingle_file_transfer"); + public SessionInfoType session_info_type = new SessionInfoType(); public override void attach(XmppStream stream) { stream.get_module(ServiceDiscovery.Module.IDENTITY).add_feature(stream, NS_URI); stream.get_module(Jingle.Module.IDENTITY).register_content_type(this); + stream.get_module(Jingle.Module.IDENTITY).register_session_info_type(session_info_type); } public override void detach(XmppStream stream) { stream.get_module(ServiceDiscovery.Module.IDENTITY).remove_feature(stream, NS_URI); } - public string content_type_ns_uri() { - return NS_URI; - } - public Jingle.TransportType content_type_transport_type() { - return Jingle.TransportType.STREAMING; - } + public string ns_uri { get { return NS_URI; } } + public Jingle.TransportType required_transport_type { get { return Jingle.TransportType.STREAMING; } } + public uint8 required_components { get { return 1; } } + 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); + public Jingle.ContentParameters create_content_parameters(Object object) throws Jingle.IqError { + assert_not_reached(); + } public async bool is_available(XmppStream stream, Jid full_jid) { bool? has_feature = yield stream.get_module(ServiceDiscovery.Module.IDENTITY).has_entity_feature(stream, full_jid, NS_URI); if (has_feature == null || !(!)has_feature) { return false; } - return yield stream.get_module(Jingle.Module.IDENTITY).is_available(stream, Jingle.TransportType.STREAMING, full_jid); + return yield stream.get_module(Jingle.Module.IDENTITY).is_available(stream, required_transport_type, required_components, full_jid); } - public async void offer_file_stream(XmppStream stream, Jid receiver_full_jid, InputStream input_stream, string basename, int64 size, string? precondition_name = null, Object? precondition_options = null) throws IOError { + public async void offer_file_stream(XmppStream stream, Jid receiver_full_jid, InputStream input_stream, string basename, int64 size, string? precondition_name = null, Object? precondition_options = null) throws Jingle.Error { StanzaNode file_node; StanzaNode description = new StanzaNode.build("description", NS_URI) .add_self_xmlns() @@ -64,25 +56,83 @@ public class Module : Jingle.ContentType, XmppStreamModule { warning("Sending file %s without size, likely going to cause problems down the road...", basename); } - Jingle.Session session; - try { - session = yield stream.get_module(Jingle.Module.IDENTITY) - .create_session(stream, Jingle.TransportType.STREAMING, receiver_full_jid, Jingle.Senders.INITIATOR, "a-file-offer", description, precondition_name, precondition_options); // TODO(hrxi): Why "a-file-offer"? - } catch (Jingle.Error e) { - throw new IOError.FAILED(@"couldn't create Jingle session: $(e.message)"); + Parameters parameters = Parameters.parse(this, description); + + Jingle.Module jingle_module = stream.get_module(Jingle.Module.IDENTITY); + + Jingle.Transport? transport = yield jingle_module.select_transport(stream, required_transport_type, required_components, receiver_full_jid, Set.empty()); + if (transport == null) { + throw new Jingle.Error.NO_SHARED_PROTOCOLS("No suitable transports"); } - session.terminate_on_connection_close = false; + Jingle.SecurityPrecondition? precondition = jingle_module.get_security_precondition(precondition_name); + if (precondition_name != null && precondition == null) { + throw new Jingle.Error.UNSUPPORTED_SECURITY("No suitable security precondiiton found"); + } + Jid? my_jid = stream.get_flag(Bind.Flag.IDENTITY).my_jid; + if (my_jid == null) { + throw new Jingle.Error.GENERAL("Couldn't determine own JID"); + } + Jingle.TransportParameters transport_params = transport.create_transport_parameters(stream, required_components, my_jid, receiver_full_jid); + Jingle.SecurityParameters? security_params = precondition != null ? precondition.create_security_parameters(stream, my_jid, receiver_full_jid, precondition_options) : null; - yield session.conn.input_stream.close_async(); + Jingle.Content content = new Jingle.Content.initiate_sent("a-file-offer", Jingle.Senders.INITIATOR, + this, parameters, + transport, transport_params, + precondition, security_params, + my_jid, receiver_full_jid); - // TODO(hrxi): catch errors - yield session.conn.output_stream.splice_async(input_stream, OutputStreamSpliceFlags.CLOSE_SOURCE|OutputStreamSpliceFlags.CLOSE_TARGET); + ArrayList contents = new ArrayList(); + contents.add(content); + + + Jingle.Session? session = null; + try { + session = yield jingle_module.create_session(stream, contents, receiver_full_jid); + + // Wait for the counterpart to accept our offer + ulong content_notify_id = 0; + content_notify_id = content.notify["state"].connect(() => { + if (content.state == Jingle.Content.State.ACCEPTED) { + Idle.add(offer_file_stream.callback); + content.disconnect(content_notify_id); + } + }); + yield; + + // Send the file data + Jingle.StreamingConnection connection = content.component_connections.values.to_array()[0] as Jingle.StreamingConnection; + IOStream io_stream = yield connection.stream.wait_async(); + yield io_stream.input_stream.close_async(); + yield io_stream.output_stream.splice_async(input_stream, OutputStreamSpliceFlags.CLOSE_SOURCE|OutputStreamSpliceFlags.CLOSE_TARGET); + yield connection.terminate(true); + } catch (Jingle.Error e) { + session.terminate(Jingle.ReasonElement.FAILED_TRANSPORT, e.message, e.message); + throw new Jingle.Error.GENERAL(@"couldn't create Jingle session: $(e.message)"); + } } public override string get_ns() { return NS_URI; } public override string get_id() { return IDENTITY.id; } } +public class SessionInfoType : Jingle.SessionInfoNs, Object { + + public string ns_uri { get { return NS_URI; } } + + public void handle_content_session_info(XmppStream stream, Jingle.Session session, StanzaNode info, Iq.Stanza iq) throws Jingle.IqError { + switch (info.name) { + case "received": + break; + case "checksum": + // TODO(hrxi): handle hash + break; + default: + throw new Jingle.IqError.UNSUPPORTED_INFO(@"unsupported file transfer info $(info.name)"); + } + } + +} + public class Parameters : Jingle.ContentParameters, Object { Module parent; @@ -127,24 +177,42 @@ public class Parameters : Jingle.ContentParameters, Object { return new Parameters(parent, description, media_type, name, size); } - public void on_session_initiate(XmppStream stream, Jingle.Session session) { - parent.file_incoming(stream, new FileTransfer(session, this)); + public StanzaNode get_description_node() { + return original_description; } + + public async void handle_proposed_content(XmppStream stream, Jingle.Session session, Jingle.Content content) { + parent.file_incoming(stream, new FileTransfer(session, content, this)); + } + + public void modify(XmppStream stream, Jingle.Session session, Jingle.Content content, Jingle.Senders senders) { } + + public void accept(XmppStream stream, Jingle.Session session, Jingle.Content content) { } + + public void handle_accept(XmppStream stream, Jingle.Session session, Jingle.Content content, StanzaNode description_node) { } + + public void terminate(bool we_terminated, string? reason_name, string? reason_text) { } } // Does nothing except wrapping an input stream to signal EOF after reading // `max_size` bytes. private class FileTransferInputStream : InputStream { + + public signal void closed(); + InputStream inner; int64 remaining_size; + public FileTransferInputStream(InputStream inner, int64 max_size) { this.inner = inner; this.remaining_size = max_size; } + private ssize_t update_remaining(ssize_t read) { this.remaining_size -= read; return read; } + public override ssize_t read(uint8[] buffer_, Cancellable? cancellable = null) throws IOError { unowned uint8[] buffer = buffer_; if (remaining_size <= 0) { @@ -155,6 +223,7 @@ private class FileTransferInputStream : InputStream { } return update_remaining(inner.read(buffer, cancellable)); } + public override async ssize_t read_async(uint8[]? buffer_, int io_priority = GLib.Priority.DEFAULT, Cancellable? cancellable = null) throws IOError { unowned uint8[] buffer = buffer_; if (remaining_size <= 0) { @@ -165,16 +234,21 @@ private class FileTransferInputStream : InputStream { } return update_remaining(yield inner.read_async(buffer, io_priority, cancellable)); } + public override bool close(Cancellable? cancellable = null) throws IOError { + closed(); return inner.close(cancellable); } + public override async bool close_async(int io_priority = GLib.Priority.DEFAULT, Cancellable? cancellable = null) throws IOError { + closed(); return yield inner.close_async(io_priority, cancellable); } } public class FileTransfer : Object { Jingle.Session session; + Jingle.Content content; Parameters parameters; public Jid peer { get { return session.peer_full_jid; } } @@ -184,19 +258,27 @@ public class FileTransfer : Object { public InputStream? stream { get; private set; } - public FileTransfer(Jingle.Session session, Parameters parameters) { + public FileTransfer(Jingle.Session session, Jingle.Content content, Parameters parameters) { this.session = session; + this.content = content; this.parameters = parameters; - this.stream = new FileTransferInputStream(session.conn.input_stream, size); } - public void accept(XmppStream stream) throws IOError { - session.accept(stream, parameters.original_description); - session.conn.output_stream.close(); + public async void accept(XmppStream stream) throws IOError { + content.accept(); + + Jingle.StreamingConnection connection = content.component_connections.values.to_array()[0] as Jingle.StreamingConnection; + IOStream? io_stream = yield connection.stream.wait_async(); + FileTransferInputStream ft_stream = new FileTransferInputStream(io_stream.input_stream, size); + io_stream.output_stream.close(); + ft_stream.closed.connect(() => { + session.terminate(Jingle.ReasonElement.SUCCESS, null, null); + }); + this.stream = ft_stream; } public void reject(XmppStream stream) { - session.reject(stream); + content.reject(); } } 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 bb31cadc..1c4e0d38 100644 --- a/xmpp-vala/src/module/xep/0260_jingle_socks5_bytestreams.vala +++ b/xmpp-vala/src/module/xep/0260_jingle_socks5_bytestreams.vala @@ -21,19 +21,13 @@ public class Module : Jingle.Transport, XmppStreamModule { public override string get_ns() { return NS_URI; } public override string get_id() { return IDENTITY.id; } - public async bool is_transport_available(XmppStream stream, Jid full_jid) { - return yield stream.get_module(ServiceDiscovery.Module.IDENTITY).has_entity_feature(stream, full_jid, NS_URI); + public async bool is_transport_available(XmppStream stream, uint8 components, Jid full_jid) { + return components == 1 && yield stream.get_module(ServiceDiscovery.Module.IDENTITY).has_entity_feature(stream, full_jid, NS_URI); } - public string transport_ns_uri() { - return NS_URI; - } - public Jingle.TransportType transport_type() { - return Jingle.TransportType.STREAMING; - } - public int transport_priority() { - return 1; - } + public string ns_uri { get { return NS_URI; } } + public Jingle.TransportType type_ { get { return Jingle.TransportType.STREAMING; } } + public int priority { get { return 1; } } private Gee.List get_proxies(XmppStream stream) { Gee.List result = new ArrayList(); int i = 1 << 15; @@ -70,20 +64,21 @@ public class Module : Jingle.Transport, XmppStreamModule { } private void select_candidates(XmppStream stream, Jid local_full_jid, string dstaddr, Parameters result) { result.local_candidates.add_all(get_proxies(stream)); - result.local_candidates.add_all(start_local_listeners(stream, local_full_jid, dstaddr, out result.listener)); + //result.local_candidates.add_all(start_local_listeners(stream, local_full_jid, dstaddr, out result.listener)); result.local_candidates.sort((c1, c2) => { if (c1.priority < c2.priority) { return 1; } if (c1.priority > c2.priority) { return -1; } return 0; }); } - public Jingle.TransportParameters create_transport_parameters(XmppStream stream, Jid local_full_jid, Jid peer_full_jid) { + public Jingle.TransportParameters create_transport_parameters(XmppStream stream, uint8 components, Jid local_full_jid, Jid peer_full_jid) { + assert(components == 1); Parameters result = new Parameters.create(local_full_jid, peer_full_jid, random_uuid()); string dstaddr = calculate_dstaddr(result.sid, local_full_jid, peer_full_jid); select_candidates(stream, local_full_jid, dstaddr, result); return result; } - public Jingle.TransportParameters parse_transport_parameters(XmppStream stream, Jid local_full_jid, Jid peer_full_jid, StanzaNode transport) throws Jingle.IqError { + public Jingle.TransportParameters parse_transport_parameters(XmppStream stream, uint8 components, Jid local_full_jid, Jid peer_full_jid, StanzaNode transport) throws Jingle.IqError { Parameters result = Parameters.parse(local_full_jid, peer_full_jid, transport); string dstaddr = calculate_dstaddr(result.sid, local_full_jid, peer_full_jid); select_candidates(stream, local_full_jid, dstaddr, result); @@ -335,6 +330,8 @@ class LocalListener { } class Parameters : Jingle.TransportParameters, Object { + public string ns_uri { get { return NS_URI; } } + public uint8 components { get { return 1; } } public Jingle.Role role { get; private set; } public string sid { get; private set; } public string remote_dstaddr { get; private set; } @@ -352,6 +349,7 @@ class Parameters : Jingle.TransportParameters, Object { Candidate? local_selected_candidate = null; SocketConnection? local_selected_candidate_conn = null; weak Jingle.Session? session = null; + weak Jingle.Content? content = null; XmppStream? hack = null; string? waiting_for_activation_cid = null; @@ -367,9 +365,11 @@ class Parameters : Jingle.TransportParameters, Object { 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) { 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"); @@ -386,9 +386,11 @@ class Parameters : Jingle.TransportParameters, Object { } return result; } - public string transport_ns_uri() { - return NS_URI; + + public void set_content(Jingle.Content content) { + } + public StanzaNode to_transport_stanza_node() { StanzaNode transport = new StanzaNode.build("transport", NS_URI) .add_self_xmlns() @@ -405,7 +407,8 @@ class Parameters : Jingle.TransportParameters, Object { } return transport; } - public void on_transport_accept(StanzaNode transport) throws Jingle.IqError { + + public void handle_transport_accept(StanzaNode transport) throws Jingle.IqError { Parameters other = Parameters.parse(local_full_jid, peer_full_jid, transport); if (other.sid != sid) { throw new Jingle.IqError.BAD_REQUEST("invalid sid"); @@ -413,7 +416,8 @@ class Parameters : Jingle.TransportParameters, Object { remote_candidates = other.remote_candidates; remote_dstaddr = other.remote_dstaddr; } - public void on_transport_info(StanzaNode transport) throws Jingle.IqError { + + public void handle_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); @@ -449,6 +453,7 @@ class Parameters : Jingle.TransportParameters, Object { handle_proxy_error(); } } + 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"); @@ -470,6 +475,7 @@ class Parameters : Jingle.TransportParameters, Object { debug("Remote selected candidate %s", candidate != null ? candidate.cid : "(null)"); 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"); @@ -477,6 +483,7 @@ 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"); @@ -486,6 +493,7 @@ class Parameters : Jingle.TransportParameters, Object { waiting_for_activation_error = true; } + private void try_completing_negotiation() { if (!remote_sent_selected_candidate || !local_determined_selected_candidate) { return; @@ -500,7 +508,7 @@ class Parameters : Jingle.TransportParameters, Object { if (num_candidates == 0) { // Notify Jingle of the failed transport. - session.set_transport_connection(hack, null); + content_set_transport_connection(null); return; } @@ -525,7 +533,7 @@ class Parameters : Jingle.TransportParameters, Object { if (strong == null) { return; } - strong.set_transport_connection(hack, local_selected_candidate_conn); + content_set_transport_connection(local_selected_candidate_conn); } else { wait_for_remote_activation.begin(local_selected_candidate, local_selected_candidate_conn); } @@ -538,15 +546,16 @@ class Parameters : Jingle.TransportParameters, Object { SocketConnection? conn = listener.get_connection(remote_selected_candidate.cid); if (conn == null) { // Remote hasn't actually connected to us?! - strong.set_transport_connection(hack, null); + content_set_transport_connection(null); return; } - strong.set_transport_connection(hack, conn); + content_set_transport_connection(conn); } else { connect_to_local_candidate.begin(remote_selected_candidate); } } } + public async void wait_for_remote_activation(Candidate candidate, SocketConnection conn) { debug("Waiting for remote activation of %s", candidate.cid); waiting_for_activation_cid = candidate.cid; @@ -558,11 +567,12 @@ class Parameters : Jingle.TransportParameters, Object { return; } if (!waiting_for_activation_error) { - strong.set_transport_connection(hack, conn); + content_set_transport_connection(conn); } else { - strong.set_transport_connection(hack, null); + content_set_transport_connection(null); } } + public async void connect_to_local_candidate(Candidate candidate) { debug("Connecting to candidate %s", candidate.cid); try { @@ -587,11 +597,11 @@ class Parameters : Jingle.TransportParameters, Object { throw new IOError.PROXY_FAILED("activation iq error"); } - Jingle.Session? strong = session; - if (strong == null) { + Jingle.Content? strong_content = content; + if (strong_content == null) { return; } - strong.send_transport_info(hack, new StanzaNode.build("transport", NS_URI) + strong_content.send_transport_info(new StanzaNode.build("transport", NS_URI) .add_self_xmlns() .put_attribute("sid", sid) .put_node(new StanzaNode.build("activated", NS_URI) @@ -599,20 +609,21 @@ class Parameters : Jingle.TransportParameters, Object { ) ); - strong.set_transport_connection(hack, conn); + content_set_transport_connection(conn); } catch (Error e) { - Jingle.Session? strong = session; - if (strong == null) { + Jingle.Content? strong_content = content; + if (strong_content == null) { return; } - strong.send_transport_info(hack, new StanzaNode.build("transport", NS_URI) + strong_content.send_transport_info(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); + content_set_transport_connection(null); } } + public async SocketConnection connect_to_socks5(Candidate candidate, string dstaddr) throws Error { SocketClient socket_client = new SocketClient() { timeout=NEGOTIATION_TIMEOUT }; @@ -685,6 +696,7 @@ class Parameters : Jingle.TransportParameters, Object { 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 @@ -705,7 +717,7 @@ class Parameters : Jingle.TransportParameters, Object { local_selected_candidate = candidate; local_selected_candidate_conn = conn; debug("Selected candidate %s", candidate.cid); - session.send_transport_info(stream, new StanzaNode.build("transport", NS_URI) + content.send_transport_info(new StanzaNode.build("transport", NS_URI) .add_self_xmlns() .put_attribute("sid", sid) .put_node(new StanzaNode.build("candidate-used", NS_URI) @@ -722,7 +734,7 @@ class Parameters : Jingle.TransportParameters, Object { } local_determined_selected_candidate = true; local_selected_candidate = null; - session.send_transport_info(stream, new StanzaNode.build("transport", NS_URI) + content.send_transport_info(new StanzaNode.build("transport", NS_URI) .add_self_xmlns() .put_attribute("sid", sid) .put_node(new StanzaNode.build("candidate-error", NS_URI)) @@ -730,10 +742,26 @@ class Parameters : Jingle.TransportParameters, Object { // Try remote candidates try_completing_negotiation(); } - public void create_transport_connection(XmppStream stream, Jingle.Session session) { - this.session = session; + + private Jingle.StreamingConnection connection = new Jingle.StreamingConnection(); + + private void content_set_transport_connection(IOStream? ios) { + IOStream? iostream = ios; + Jingle.Content? strong_content = content; + if (strong_content == null) return; + + if (strong_content.security_params != null) { + iostream = strong_content.security_params.wrap_stream(iostream); + } + connection.init.begin(iostream); + } + + public void create_transport_connection(XmppStream stream, Jingle.Content content) { + this.session = content.session; + this.content = content; this.hack = stream; try_connecting_to_candidates.begin(stream, session); + this.content.set_transport_connection(connection, 1); } } diff --git a/xmpp-vala/src/module/xep/0261_jingle_in_band_bytestreams.vala b/xmpp-vala/src/module/xep/0261_jingle_in_band_bytestreams.vala index e26d63b7..5bb71831 100644 --- a/xmpp-vala/src/module/xep/0261_jingle_in_band_bytestreams.vala +++ b/xmpp-vala/src/module/xep/0261_jingle_in_band_bytestreams.vala @@ -21,41 +21,41 @@ public class Module : Jingle.Transport, XmppStreamModule { public override string get_ns() { return NS_URI; } public override string get_id() { return IDENTITY.id; } - public async bool is_transport_available(XmppStream stream, Jid full_jid) { - return yield stream.get_module(ServiceDiscovery.Module.IDENTITY).has_entity_feature(stream, full_jid, NS_URI); + public async bool is_transport_available(XmppStream stream, uint8 components, Jid full_jid) { + return components == 1 && yield stream.get_module(ServiceDiscovery.Module.IDENTITY).has_entity_feature(stream, full_jid, NS_URI); } - public string transport_ns_uri() { - return NS_URI; - } - public Jingle.TransportType transport_type() { - return Jingle.TransportType.STREAMING; - } - public int transport_priority() { - return 0; - } - public Jingle.TransportParameters create_transport_parameters(XmppStream stream, Jid local_full_jid, Jid peer_full_jid) { + public string ns_uri { get { return NS_URI; } } + public Jingle.TransportType type_ { get { return Jingle.TransportType.STREAMING; } } + public int priority { get { return 0; } } + public Jingle.TransportParameters create_transport_parameters(XmppStream stream, uint8 components, Jid local_full_jid, Jid peer_full_jid) { + assert(components == 1); return new Parameters.create(peer_full_jid, random_uuid()); } - public Jingle.TransportParameters parse_transport_parameters(XmppStream stream, Jid local_full_jid, Jid peer_full_jid, StanzaNode transport) throws Jingle.IqError { + public Jingle.TransportParameters parse_transport_parameters(XmppStream stream, uint8 components, Jid local_full_jid, Jid peer_full_jid, StanzaNode transport) throws Jingle.IqError { return Parameters.parse(peer_full_jid, transport); } } class Parameters : Jingle.TransportParameters, Object { + public string ns_uri { get { return NS_URI; } } + public uint8 components { get { return 1; } } public Jingle.Role role { get; private set; } public Jid peer_full_jid { get; private set; } public string sid { get; private set; } public int block_size { get; private set; } + private Parameters(Jingle.Role role, Jid peer_full_jid, string sid, int block_size) { this.role = role; this.peer_full_jid = peer_full_jid; this.sid = sid; this.block_size = block_size; } + public Parameters.create(Jid peer_full_jid, string sid) { this(Jingle.Role.INITIATOR, peer_full_jid, sid, DEFAULT_BLOCKSIZE); } + public static Parameters parse(Jid peer_full_jid, StanzaNode transport) throws Jingle.IqError { string? sid = transport.get_attribute("sid"); int block_size = transport.get_attribute_int("block-size"); @@ -64,27 +64,43 @@ class Parameters : Jingle.TransportParameters, Object { } return new Parameters(Jingle.Role.RESPONDER, peer_full_jid, sid, block_size); } + public string transport_ns_uri() { return NS_URI; } + + public void set_content(Jingle.Content content) { + + } + public StanzaNode to_transport_stanza_node() { return new StanzaNode.build("transport", NS_URI) .add_self_xmlns() .put_attribute("block-size", block_size.to_string()) .put_attribute("sid", sid); } - public void on_transport_accept(StanzaNode transport) throws Jingle.IqError { + + public void handle_transport_accept(StanzaNode transport) throws Jingle.IqError { Parameters other = Parameters.parse(peer_full_jid, transport); if (other.sid != sid || other.block_size > block_size) { throw new Jingle.IqError.NOT_ACCEPTABLE("invalid IBB sid or block_size"); } block_size = other.block_size; } - public void on_transport_info(StanzaNode transport) throws Jingle.IqError { + + public void handle_transport_info(StanzaNode transport) throws Jingle.IqError { throw new Jingle.IqError.UNSUPPORTED_INFO("transport-info not supported for IBBs"); } - public void create_transport_connection(XmppStream stream, Jingle.Session session) { - session.set_transport_connection(stream, InBandBytestreams.Connection.create(stream, peer_full_jid, sid, block_size, role == Jingle.Role.INITIATOR)); + + public void create_transport_connection(XmppStream stream, Jingle.Content content) { + IOStream iostream = InBandBytestreams.Connection.create(stream, peer_full_jid, sid, block_size, role == Jingle.Role.INITIATOR); + Jingle.StreamingConnection connection = new Jingle.StreamingConnection(); + if (content.security_params != null) { + iostream = content.security_params.wrap_stream(iostream); + } + connection.init.begin(iostream); + debug("set transport conn ibb"); + content.set_transport_connection(connection, 1); } }