Improve & refactor Jingle base implementation

Co-authored-by: Marvin W <git@larma.de>
This commit is contained in:
fiaxh 2021-03-19 22:46:39 +01:00
parent 148cf48d2b
commit 2b90fcc39a
19 changed files with 1671 additions and 1157 deletions

View file

@ -61,15 +61,18 @@ SOURCES
"src/module/xep/0048_conference.vala" "src/module/xep/0048_conference.vala"
"src/module/xep/0402_bookmarks2.vala" "src/module/xep/0402_bookmarks2.vala"
"src/module/xep/0004_data_forms.vala" "src/module/xep/0004_data_forms.vala"
"src/module/xep/0030_service_discovery/flag.vala" "src/module/xep/0030_service_discovery/flag.vala"
"src/module/xep/0030_service_discovery/identity.vala" "src/module/xep/0030_service_discovery/identity.vala"
"src/module/xep/0030_service_discovery/info_result.vala" "src/module/xep/0030_service_discovery/info_result.vala"
"src/module/xep/0030_service_discovery/item.vala" "src/module/xep/0030_service_discovery/item.vala"
"src/module/xep/0030_service_discovery/items_result.vala" "src/module/xep/0030_service_discovery/items_result.vala"
"src/module/xep/0030_service_discovery/module.vala" "src/module/xep/0030_service_discovery/module.vala"
"src/module/xep/0045_muc/flag.vala" "src/module/xep/0045_muc/flag.vala"
"src/module/xep/0045_muc/module.vala" "src/module/xep/0045_muc/module.vala"
"src/module/xep/0045_muc/status_code.vala" "src/module/xep/0045_muc/status_code.vala"
"src/module/xep/0047_in_band_bytestreams.vala" "src/module/xep/0047_in_band_bytestreams.vala"
"src/module/xep/0049_private_xml_storage.vala" "src/module/xep/0049_private_xml_storage.vala"
"src/module/xep/0054_vcard/module.vala" "src/module/xep/0054_vcard/module.vala"
@ -81,7 +84,31 @@ SOURCES
"src/module/xep/0084_user_avatars.vala" "src/module/xep/0084_user_avatars.vala"
"src/module/xep/0085_chat_state_notifications.vala" "src/module/xep/0085_chat_state_notifications.vala"
"src/module/xep/0115_entity_capabilities.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/0184_message_delivery_receipts.vala"
"src/module/xep/0191_blocking_command.vala" "src/module/xep/0191_blocking_command.vala"
"src/module/xep/0198_stream_management.vala" "src/module/xep/0198_stream_management.vala"

View file

@ -110,13 +110,13 @@ public class XmppLog {
public void node(string what, StanzaNode node, XmppStream stream) { public void node(string what, StanzaNode node, XmppStream stream) {
if (should_log_node(node)) { 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<Thread>(), 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) { public void str(string what, string str, XmppStream stream) {
if (should_log_str(str)) { 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<Thread>(), new DateTime.now_local().to_string(), use_ansi ? ANSI_COLOR_END : "", str);
} }
} }

View file

@ -10,6 +10,8 @@ namespace Xmpp.Iq {
private HashMap<string, ArrayList<Handler>> namespaceRegistrants = new HashMap<string, ArrayList<Handler>>(); private HashMap<string, ArrayList<Handler>> namespaceRegistrants = new HashMap<string, ArrayList<Handler>>();
public async Iq.Stanza send_iq_async(XmppStream stream, Iq.Stanza iq) { 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; Iq.Stanza? return_stanza = null;
send_iq(stream, iq, (_, result_iq) => { send_iq(stream, iq, (_, result_iq) => {
return_stanza = result_iq; return_stanza = result_iq;

File diff suppressed because it is too large Load diff

View file

@ -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<IOStream> stream { get { return promise.future; } }
protected Gee.Promise<IOStream> promise = new Gee.Promise<IOStream>();
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();
}
}
}
}
}

View file

@ -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<uint8, ComponentConnection> component_connections = new HashMap<uint8, ComponentConnection>(); // TODO private
// INITIATE_SENT | INITIATE_RECEIVED | CONNECTING
public Set<string> tried_transport_methods = new HashSet<string>();
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);
}
}

View file

@ -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();
}
}

View file

@ -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<StanzaNode> 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<ContentNode> get_content_nodes(StanzaNode jingle) throws IqError {
Gee.List<StanzaNode> contents = jingle.get_subnodes("content");
if (contents.size == 0) {
throw new IqError.BAD_REQUEST("missing content node");
}
Gee.List<ContentNode> list = new ArrayList<ContentNode>();
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;
}
}

View file

@ -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);
}
}

View file

@ -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);
}
}

View file

@ -0,0 +1,38 @@
using Gee;
using Xmpp;
public class Xmpp.Xep.Jingle.Flag : XmppStreamFlag {
public static FlagIdentity<Flag> IDENTITY = new FlagIdentity<Flag>(NS_URI, "jingle");
public HashMap<string, Session> sessions = new HashMap<string, Session>();
public HashMap<string, Promise<Session?>> promises = new HashMap<string, Promise<Session?>>();
// 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<Session?>();
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; }
}

View file

@ -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<Module> IDENTITY = new Xmpp.ModuleIdentity<Module>(NS_URI, "0166_jingle");
public signal void session_initiate_received(XmppStream stream, Session session);
private HashMap<string, ContentType> content_types = new HashMap<string, ContentType>();
private HashMap<string, SessionInfoNs> session_info_types = new HashMap<string, SessionInfoNs>();
private HashMap<string, Transport> transports = new HashMap<string, Transport>();
private HashMap<string, SecurityPrecondition> security_preconditions = new HashMap<string, SecurityPrecondition>();
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<string> 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<Content> 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 });
}
}

View file

@ -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)");
}
}
}

View file

@ -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
};
}

View file

@ -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<string, Content> contents = new HashMap<string, Content>();
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<ContentNode> 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<ContentNode> 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();
}
}

View file

@ -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;
}
}

View file

@ -7,50 +7,42 @@ namespace Xmpp.Xep.JingleFileTransfer {
private const string NS_URI = "urn:xmpp:jingle:apps:file-transfer:5"; private const string NS_URI = "urn:xmpp:jingle:apps:file-transfer:5";
public class Module : Jingle.ContentType, XmppStreamModule { public class Module : Jingle.ContentType, XmppStreamModule {
public signal void file_incoming(XmppStream stream, FileTransfer file_transfer);
public static Xmpp.ModuleIdentity<Module> IDENTITY = new Xmpp.ModuleIdentity<Module>(NS_URI, "0234_jingle_file_transfer"); public static Xmpp.ModuleIdentity<Module> IDENTITY = new Xmpp.ModuleIdentity<Module>(NS_URI, "0234_jingle_file_transfer");
public SessionInfoType session_info_type = new SessionInfoType();
public override void attach(XmppStream stream) { public override void attach(XmppStream stream) {
stream.get_module(ServiceDiscovery.Module.IDENTITY).add_feature(stream, NS_URI); 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_content_type(this);
stream.get_module(Jingle.Module.IDENTITY).register_session_info_type(session_info_type);
} }
public override void detach(XmppStream stream) { public override void detach(XmppStream stream) {
stream.get_module(ServiceDiscovery.Module.IDENTITY).remove_feature(stream, NS_URI); stream.get_module(ServiceDiscovery.Module.IDENTITY).remove_feature(stream, NS_URI);
} }
public string content_type_ns_uri() { public string ns_uri { get { return NS_URI; } }
return NS_URI; public Jingle.TransportType required_transport_type { get { return Jingle.TransportType.STREAMING; } }
} public uint8 required_components { get { return 1; } }
public Jingle.TransportType content_type_transport_type() {
return Jingle.TransportType.STREAMING;
}
public Jingle.ContentParameters parse_content_parameters(StanzaNode description) throws Jingle.IqError { public Jingle.ContentParameters parse_content_parameters(StanzaNode description) throws Jingle.IqError {
return Parameters.parse(this, description); 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) { 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); bool? has_feature = yield stream.get_module(ServiceDiscovery.Module.IDENTITY).has_entity_feature(stream, full_jid, NS_URI);
if (has_feature == null || !(!)has_feature) { if (has_feature == null || !(!)has_feature) {
return false; 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 file_node;
StanzaNode description = new StanzaNode.build("description", NS_URI) StanzaNode description = new StanzaNode.build("description", NS_URI)
.add_self_xmlns() .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); warning("Sending file %s without size, likely going to cause problems down the road...", basename);
} }
Jingle.Session session; Parameters parameters = Parameters.parse(this, description);
try {
session = yield stream.get_module(Jingle.Module.IDENTITY) Jingle.Module jingle_module = 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) { Jingle.Transport? transport = yield jingle_module.select_transport(stream, required_transport_type, required_components, receiver_full_jid, Set.empty());
throw new IOError.FAILED(@"couldn't create Jingle session: $(e.message)"); 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 ArrayList<Jingle.Content> contents = new ArrayList<Jingle.Content>();
yield session.conn.output_stream.splice_async(input_stream, OutputStreamSpliceFlags.CLOSE_SOURCE|OutputStreamSpliceFlags.CLOSE_TARGET); 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_ns() { return NS_URI; }
public override string get_id() { return IDENTITY.id; } 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 { public class Parameters : Jingle.ContentParameters, Object {
Module parent; Module parent;
@ -127,24 +177,42 @@ public class Parameters : Jingle.ContentParameters, Object {
return new Parameters(parent, description, media_type, name, size); return new Parameters(parent, description, media_type, name, size);
} }
public void on_session_initiate(XmppStream stream, Jingle.Session session) { public StanzaNode get_description_node() {
parent.file_incoming(stream, new FileTransfer(session, this)); 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 // Does nothing except wrapping an input stream to signal EOF after reading
// `max_size` bytes. // `max_size` bytes.
private class FileTransferInputStream : InputStream { private class FileTransferInputStream : InputStream {
public signal void closed();
InputStream inner; InputStream inner;
int64 remaining_size; int64 remaining_size;
public FileTransferInputStream(InputStream inner, int64 max_size) { public FileTransferInputStream(InputStream inner, int64 max_size) {
this.inner = inner; this.inner = inner;
this.remaining_size = max_size; this.remaining_size = max_size;
} }
private ssize_t update_remaining(ssize_t read) { private ssize_t update_remaining(ssize_t read) {
this.remaining_size -= read; this.remaining_size -= read;
return read; return read;
} }
public override ssize_t read(uint8[] buffer_, Cancellable? cancellable = null) throws IOError { public override ssize_t read(uint8[] buffer_, Cancellable? cancellable = null) throws IOError {
unowned uint8[] buffer = buffer_; unowned uint8[] buffer = buffer_;
if (remaining_size <= 0) { if (remaining_size <= 0) {
@ -155,6 +223,7 @@ private class FileTransferInputStream : InputStream {
} }
return update_remaining(inner.read(buffer, cancellable)); 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 { public override async ssize_t read_async(uint8[]? buffer_, int io_priority = GLib.Priority.DEFAULT, Cancellable? cancellable = null) throws IOError {
unowned uint8[] buffer = buffer_; unowned uint8[] buffer = buffer_;
if (remaining_size <= 0) { if (remaining_size <= 0) {
@ -165,16 +234,21 @@ private class FileTransferInputStream : InputStream {
} }
return update_remaining(yield inner.read_async(buffer, io_priority, cancellable)); return update_remaining(yield inner.read_async(buffer, io_priority, cancellable));
} }
public override bool close(Cancellable? cancellable = null) throws IOError { public override bool close(Cancellable? cancellable = null) throws IOError {
closed();
return inner.close(cancellable); return inner.close(cancellable);
} }
public override async bool close_async(int io_priority = GLib.Priority.DEFAULT, Cancellable? cancellable = null) throws IOError { 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); return yield inner.close_async(io_priority, cancellable);
} }
} }
public class FileTransfer : Object { public class FileTransfer : Object {
Jingle.Session session; Jingle.Session session;
Jingle.Content content;
Parameters parameters; Parameters parameters;
public Jid peer { get { return session.peer_full_jid; } } public Jid peer { get { return session.peer_full_jid; } }
@ -184,19 +258,27 @@ public class FileTransfer : Object {
public InputStream? stream { get; private set; } 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.session = session;
this.content = content;
this.parameters = parameters; this.parameters = parameters;
this.stream = new FileTransferInputStream(session.conn.input_stream, size);
} }
public void accept(XmppStream stream) throws IOError { public async void accept(XmppStream stream) throws IOError {
session.accept(stream, parameters.original_description); content.accept();
session.conn.output_stream.close();
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) { public void reject(XmppStream stream) {
session.reject(stream); content.reject();
} }
} }

View file

@ -21,19 +21,13 @@ public class Module : Jingle.Transport, XmppStreamModule {
public override string get_ns() { return NS_URI; } public override string get_ns() { return NS_URI; }
public override string get_id() { return IDENTITY.id; } public override string get_id() { return IDENTITY.id; }
public async bool is_transport_available(XmppStream stream, Jid full_jid) { public async bool is_transport_available(XmppStream stream, uint8 components, Jid full_jid) {
return yield stream.get_module(ServiceDiscovery.Module.IDENTITY).has_entity_feature(stream, full_jid, NS_URI); return components == 1 && yield stream.get_module(ServiceDiscovery.Module.IDENTITY).has_entity_feature(stream, full_jid, NS_URI);
} }
public string transport_ns_uri() { public string ns_uri { get { return NS_URI; } }
return NS_URI; public Jingle.TransportType type_ { get { return Jingle.TransportType.STREAMING; } }
} public int priority { get { return 1; } }
public Jingle.TransportType transport_type() {
return Jingle.TransportType.STREAMING;
}
public int transport_priority() {
return 1;
}
private Gee.List<Candidate> get_proxies(XmppStream stream) { private Gee.List<Candidate> get_proxies(XmppStream stream) {
Gee.List<Candidate> result = new ArrayList<Candidate>(); Gee.List<Candidate> result = new ArrayList<Candidate>();
int i = 1 << 15; 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) { 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(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) => { result.local_candidates.sort((c1, c2) => {
if (c1.priority < c2.priority) { return 1; } if (c1.priority < c2.priority) { return 1; }
if (c1.priority > c2.priority) { return -1; } if (c1.priority > c2.priority) { return -1; }
return 0; 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()); 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); string dstaddr = calculate_dstaddr(result.sid, local_full_jid, peer_full_jid);
select_candidates(stream, local_full_jid, dstaddr, result); select_candidates(stream, local_full_jid, dstaddr, result);
return 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); Parameters result = Parameters.parse(local_full_jid, peer_full_jid, transport);
string dstaddr = calculate_dstaddr(result.sid, local_full_jid, peer_full_jid); string dstaddr = calculate_dstaddr(result.sid, local_full_jid, peer_full_jid);
select_candidates(stream, local_full_jid, dstaddr, result); select_candidates(stream, local_full_jid, dstaddr, result);
@ -335,6 +330,8 @@ class LocalListener {
} }
class Parameters : Jingle.TransportParameters, Object { 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 Jingle.Role role { get; private set; }
public string sid { get; private set; } public string sid { get; private set; }
public string remote_dstaddr { get; private set; } public string remote_dstaddr { get; private set; }
@ -352,6 +349,7 @@ class Parameters : Jingle.TransportParameters, Object {
Candidate? local_selected_candidate = null; Candidate? local_selected_candidate = null;
SocketConnection? local_selected_candidate_conn = null; SocketConnection? local_selected_candidate_conn = null;
weak Jingle.Session? session = null; weak Jingle.Session? session = null;
weak Jingle.Content? content = null;
XmppStream? hack = null; XmppStream? hack = null;
string? waiting_for_activation_cid = null; string? waiting_for_activation_cid = null;
@ -367,9 +365,11 @@ class Parameters : Jingle.TransportParameters, Object {
this.local_full_jid = local_full_jid; this.local_full_jid = local_full_jid;
this.peer_full_jid = peer_full_jid; this.peer_full_jid = peer_full_jid;
} }
public Parameters.create(Jid local_full_jid, Jid peer_full_jid, string sid) { 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); 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 { public static Parameters parse(Jid local_full_jid, Jid peer_full_jid, StanzaNode transport) throws Jingle.IqError {
string? dstaddr = transport.get_attribute("dstaddr"); string? dstaddr = transport.get_attribute("dstaddr");
string? mode = transport.get_attribute("mode"); string? mode = transport.get_attribute("mode");
@ -386,9 +386,11 @@ class Parameters : Jingle.TransportParameters, Object {
} }
return result; return result;
} }
public string transport_ns_uri() {
return NS_URI; public void set_content(Jingle.Content content) {
} }
public StanzaNode to_transport_stanza_node() { public StanzaNode to_transport_stanza_node() {
StanzaNode transport = new StanzaNode.build("transport", NS_URI) StanzaNode transport = new StanzaNode.build("transport", NS_URI)
.add_self_xmlns() .add_self_xmlns()
@ -405,7 +407,8 @@ class Parameters : Jingle.TransportParameters, Object {
} }
return transport; 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); Parameters other = Parameters.parse(local_full_jid, peer_full_jid, transport);
if (other.sid != sid) { if (other.sid != sid) {
throw new Jingle.IqError.BAD_REQUEST("invalid sid"); throw new Jingle.IqError.BAD_REQUEST("invalid sid");
@ -413,7 +416,8 @@ class Parameters : Jingle.TransportParameters, Object {
remote_candidates = other.remote_candidates; remote_candidates = other.remote_candidates;
remote_dstaddr = other.remote_dstaddr; 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_error = transport.get_subnode("candidate-error", NS_URI);
StanzaNode? candidate_used = transport.get_subnode("candidate-used", NS_URI); StanzaNode? candidate_used = transport.get_subnode("candidate-used", NS_URI);
StanzaNode? activated = transport.get_subnode("activated", NS_URI); StanzaNode? activated = transport.get_subnode("activated", NS_URI);
@ -449,6 +453,7 @@ class Parameters : Jingle.TransportParameters, Object {
handle_proxy_error(); handle_proxy_error();
} }
} }
private void handle_remote_candidate(string? cid) throws Jingle.IqError { private void handle_remote_candidate(string? cid) throws Jingle.IqError {
if (remote_sent_selected_candidate) { if (remote_sent_selected_candidate) {
throw new Jingle.IqError.BAD_REQUEST("remote candidate already specified"); 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)"); debug("Remote selected candidate %s", candidate != null ? candidate.cid : "(null)");
try_completing_negotiation(); try_completing_negotiation();
} }
private void handle_activated(string cid) throws Jingle.IqError { private void handle_activated(string cid) throws Jingle.IqError {
if (waiting_for_activation_cid == null || cid != waiting_for_activation_cid) { if (waiting_for_activation_cid == null || cid != waiting_for_activation_cid) {
throw new Jingle.IqError.BAD_REQUEST("unexpected proxy activation message"); 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); Idle.add((owned)waiting_for_activation_callback);
waiting_for_activation_cid = null; waiting_for_activation_cid = null;
} }
private void handle_proxy_error() throws Jingle.IqError { private void handle_proxy_error() throws Jingle.IqError {
if (waiting_for_activation_cid == null) { if (waiting_for_activation_cid == null) {
throw new Jingle.IqError.BAD_REQUEST("unexpected proxy error message"); throw new Jingle.IqError.BAD_REQUEST("unexpected proxy error message");
@ -486,6 +493,7 @@ class Parameters : Jingle.TransportParameters, Object {
waiting_for_activation_error = true; waiting_for_activation_error = true;
} }
private void try_completing_negotiation() { private void try_completing_negotiation() {
if (!remote_sent_selected_candidate || !local_determined_selected_candidate) { if (!remote_sent_selected_candidate || !local_determined_selected_candidate) {
return; return;
@ -500,7 +508,7 @@ class Parameters : Jingle.TransportParameters, Object {
if (num_candidates == 0) { if (num_candidates == 0) {
// Notify Jingle of the failed transport. // Notify Jingle of the failed transport.
session.set_transport_connection(hack, null); content_set_transport_connection(null);
return; return;
} }
@ -525,7 +533,7 @@ class Parameters : Jingle.TransportParameters, Object {
if (strong == null) { if (strong == null) {
return; return;
} }
strong.set_transport_connection(hack, local_selected_candidate_conn); content_set_transport_connection(local_selected_candidate_conn);
} else { } else {
wait_for_remote_activation.begin(local_selected_candidate, local_selected_candidate_conn); 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); SocketConnection? conn = listener.get_connection(remote_selected_candidate.cid);
if (conn == null) { if (conn == null) {
// Remote hasn't actually connected to us?! // Remote hasn't actually connected to us?!
strong.set_transport_connection(hack, null); content_set_transport_connection(null);
return; return;
} }
strong.set_transport_connection(hack, conn); content_set_transport_connection(conn);
} else { } else {
connect_to_local_candidate.begin(remote_selected_candidate); connect_to_local_candidate.begin(remote_selected_candidate);
} }
} }
} }
public async void wait_for_remote_activation(Candidate candidate, SocketConnection conn) { public async void wait_for_remote_activation(Candidate candidate, SocketConnection conn) {
debug("Waiting for remote activation of %s", candidate.cid); debug("Waiting for remote activation of %s", candidate.cid);
waiting_for_activation_cid = candidate.cid; waiting_for_activation_cid = candidate.cid;
@ -558,11 +567,12 @@ class Parameters : Jingle.TransportParameters, Object {
return; return;
} }
if (!waiting_for_activation_error) { if (!waiting_for_activation_error) {
strong.set_transport_connection(hack, conn); content_set_transport_connection(conn);
} else { } else {
strong.set_transport_connection(hack, null); content_set_transport_connection(null);
} }
} }
public async void connect_to_local_candidate(Candidate candidate) { public async void connect_to_local_candidate(Candidate candidate) {
debug("Connecting to candidate %s", candidate.cid); debug("Connecting to candidate %s", candidate.cid);
try { try {
@ -587,11 +597,11 @@ class Parameters : Jingle.TransportParameters, Object {
throw new IOError.PROXY_FAILED("activation iq error"); throw new IOError.PROXY_FAILED("activation iq error");
} }
Jingle.Session? strong = session; Jingle.Content? strong_content = content;
if (strong == null) { if (strong_content == null) {
return; 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() .add_self_xmlns()
.put_attribute("sid", sid) .put_attribute("sid", sid)
.put_node(new StanzaNode.build("activated", NS_URI) .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) { } catch (Error e) {
Jingle.Session? strong = session; Jingle.Content? strong_content = content;
if (strong == null) { if (strong_content == null) {
return; 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() .add_self_xmlns()
.put_attribute("sid", sid) .put_attribute("sid", sid)
.put_node(new StanzaNode.build("proxy-error", NS_URI)) .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 { public async SocketConnection connect_to_socks5(Candidate candidate, string dstaddr) throws Error {
SocketClient socket_client = new SocketClient() { timeout=NEGOTIATION_TIMEOUT }; SocketClient socket_client = new SocketClient() { timeout=NEGOTIATION_TIMEOUT };
@ -685,6 +696,7 @@ class Parameters : Jingle.TransportParameters, Object {
return conn; return conn;
} }
public async void try_connecting_to_candidates(XmppStream stream, Jingle.Session session) throws Error { public async void try_connecting_to_candidates(XmppStream stream, Jingle.Session session) throws Error {
remote_candidates.sort((c1, c2) => { remote_candidates.sort((c1, c2) => {
// sort from priorities from high to low // sort from priorities from high to low
@ -705,7 +717,7 @@ class Parameters : Jingle.TransportParameters, Object {
local_selected_candidate = candidate; local_selected_candidate = candidate;
local_selected_candidate_conn = conn; local_selected_candidate_conn = conn;
debug("Selected candidate %s", candidate.cid); 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() .add_self_xmlns()
.put_attribute("sid", sid) .put_attribute("sid", sid)
.put_node(new StanzaNode.build("candidate-used", NS_URI) .put_node(new StanzaNode.build("candidate-used", NS_URI)
@ -722,7 +734,7 @@ class Parameters : Jingle.TransportParameters, Object {
} }
local_determined_selected_candidate = true; local_determined_selected_candidate = true;
local_selected_candidate = null; 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() .add_self_xmlns()
.put_attribute("sid", sid) .put_attribute("sid", sid)
.put_node(new StanzaNode.build("candidate-error", NS_URI)) .put_node(new StanzaNode.build("candidate-error", NS_URI))
@ -730,10 +742,26 @@ class Parameters : Jingle.TransportParameters, Object {
// Try remote candidates // Try remote candidates
try_completing_negotiation(); 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; this.hack = stream;
try_connecting_to_candidates.begin(stream, session); try_connecting_to_candidates.begin(stream, session);
this.content.set_transport_connection(connection, 1);
} }
} }

View file

@ -21,41 +21,41 @@ public class Module : Jingle.Transport, XmppStreamModule {
public override string get_ns() { return NS_URI; } public override string get_ns() { return NS_URI; }
public override string get_id() { return IDENTITY.id; } public override string get_id() { return IDENTITY.id; }
public async bool is_transport_available(XmppStream stream, Jid full_jid) { public async bool is_transport_available(XmppStream stream, uint8 components, Jid full_jid) {
return yield stream.get_module(ServiceDiscovery.Module.IDENTITY).has_entity_feature(stream, full_jid, NS_URI); return components == 1 && yield stream.get_module(ServiceDiscovery.Module.IDENTITY).has_entity_feature(stream, full_jid, NS_URI);
} }
public string transport_ns_uri() { public string ns_uri { get { return NS_URI; } }
return NS_URI; public Jingle.TransportType type_ { get { return Jingle.TransportType.STREAMING; } }
} public int priority { get { return 0; } }
public Jingle.TransportType transport_type() { public Jingle.TransportParameters create_transport_parameters(XmppStream stream, uint8 components, Jid local_full_jid, Jid peer_full_jid) {
return Jingle.TransportType.STREAMING; assert(components == 1);
}
public int transport_priority() {
return 0;
}
public Jingle.TransportParameters create_transport_parameters(XmppStream stream, Jid local_full_jid, Jid peer_full_jid) {
return new Parameters.create(peer_full_jid, random_uuid()); 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); return Parameters.parse(peer_full_jid, transport);
} }
} }
class Parameters : Jingle.TransportParameters, Object { 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 Jingle.Role role { get; private set; }
public Jid peer_full_jid { get; private set; } public Jid peer_full_jid { get; private set; }
public string sid { get; private set; } public string sid { get; private set; }
public int block_size { get; private set; } public int block_size { get; private set; }
private Parameters(Jingle.Role role, Jid peer_full_jid, string sid, int block_size) { private Parameters(Jingle.Role role, Jid peer_full_jid, string sid, int block_size) {
this.role = role; this.role = role;
this.peer_full_jid = peer_full_jid; this.peer_full_jid = peer_full_jid;
this.sid = sid; this.sid = sid;
this.block_size = block_size; this.block_size = block_size;
} }
public Parameters.create(Jid peer_full_jid, string sid) { public Parameters.create(Jid peer_full_jid, string sid) {
this(Jingle.Role.INITIATOR, peer_full_jid, sid, DEFAULT_BLOCKSIZE); this(Jingle.Role.INITIATOR, peer_full_jid, sid, DEFAULT_BLOCKSIZE);
} }
public static Parameters parse(Jid peer_full_jid, StanzaNode transport) throws Jingle.IqError { public static Parameters parse(Jid peer_full_jid, StanzaNode transport) throws Jingle.IqError {
string? sid = transport.get_attribute("sid"); string? sid = transport.get_attribute("sid");
int block_size = transport.get_attribute_int("block-size"); 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); return new Parameters(Jingle.Role.RESPONDER, peer_full_jid, sid, block_size);
} }
public string transport_ns_uri() { public string transport_ns_uri() {
return NS_URI; return NS_URI;
} }
public void set_content(Jingle.Content content) {
}
public StanzaNode to_transport_stanza_node() { public StanzaNode to_transport_stanza_node() {
return new StanzaNode.build("transport", NS_URI) return new StanzaNode.build("transport", NS_URI)
.add_self_xmlns() .add_self_xmlns()
.put_attribute("block-size", block_size.to_string()) .put_attribute("block-size", block_size.to_string())
.put_attribute("sid", sid); .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); Parameters other = Parameters.parse(peer_full_jid, transport);
if (other.sid != sid || other.block_size > block_size) { if (other.sid != sid || other.block_size > block_size) {
throw new Jingle.IqError.NOT_ACCEPTABLE("invalid IBB sid or block_size"); throw new Jingle.IqError.NOT_ACCEPTABLE("invalid IBB sid or block_size");
} }
block_size = other.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"); 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);
} }
} }