Fall back to IBB if S5B does not work out

This mostly happens if connectivity to the candidates cannot be
established.
This commit is contained in:
hrxi 2019-08-05 20:54:45 +02:00
parent 2327dc783c
commit 9a1e9864d6
2 changed files with 189 additions and 40 deletions

View file

@ -139,12 +139,15 @@ public class Module : XmppStreamModule, Iq.Handler {
} }
return transports[ns_uri]; return transports[ns_uri];
} }
public Transport? select_transport(XmppStream stream, TransportType type, Jid receiver_full_jid) { public Transport? select_transport(XmppStream stream, TransportType type, Jid receiver_full_jid, Set<string> blacklist) {
Transport? result = null; Transport? result = null;
foreach (Transport transport in transports.values) { foreach (Transport transport in transports.values) {
if (transport.transport_type() != type) { if (transport.transport_type() != type) {
continue; continue;
} }
if (transport.transport_ns_uri() in blacklist) {
continue;
}
if (transport.is_transport_available(stream, receiver_full_jid)) { if (transport.is_transport_available(stream, receiver_full_jid)) {
if (result != null) { if (result != null) {
if (result.transport_priority() >= transport.transport_priority()) { if (result.transport_priority() >= transport.transport_priority()) {
@ -163,14 +166,14 @@ public class Module : XmppStreamModule, Iq.Handler {
} }
public bool is_available(XmppStream stream, TransportType type, Jid full_jid) { public bool is_available(XmppStream stream, TransportType type, Jid full_jid) {
return is_jingle_available(stream, full_jid) && select_transport(stream, type, full_jid) != null; return is_jingle_available(stream, full_jid) && select_transport(stream, type, full_jid, Set.empty()) != null;
} }
public Session create_session(XmppStream stream, TransportType type, Jid receiver_full_jid, Senders senders, string content_name, StanzaNode description) throws Error { public Session create_session(XmppStream stream, TransportType type, Jid receiver_full_jid, Senders senders, string content_name, StanzaNode description) throws Error {
if (!is_jingle_available(stream, receiver_full_jid)) { if (!is_jingle_available(stream, receiver_full_jid)) {
throw new Error.NO_SHARED_PROTOCOLS("No Jingle support"); throw new Error.NO_SHARED_PROTOCOLS("No Jingle support");
} }
Transport? transport = select_transport(stream, type, receiver_full_jid); Transport? transport = select_transport(stream, type, receiver_full_jid, Set.empty());
if (transport == null) { if (transport == null) {
throw new Error.NO_SHARED_PROTOCOLS("No suitable transports"); throw new Error.NO_SHARED_PROTOCOLS("No suitable transports");
} }
@ -369,11 +372,13 @@ public interface ContentParameters : Object {
public class Session { public class Session {
// INITIATE_SENT -> CONNECTING -> ACTIVE -> ENDED // INITIATE_SENT -> CONNECTING -> [REPLACING_TRANSPORT -> CONNECTING ->]... ACTIVE -> ENDED
// INITIATE_RECEIVED -> CONNECTING -> ACTIVE -> ENDED // INITIATE_RECEIVED -> CONNECTING -> [WAITING_FOR_TRANSPORT_REPLACE -> CONNECTING ->].. ACTIVE -> ENDED
public enum State { public enum State {
INITIATE_SENT, INITIATE_SENT,
REPLACING_TRANSPORT,
INITIATE_RECEIVED, INITIATE_RECEIVED,
WAITING_FOR_TRANSPORT_REPLACE,
CONNECTING, CONNECTING,
ACTIVE, ACTIVE,
ENDED, ENDED,
@ -381,8 +386,9 @@ public class Session {
public State state { get; private set; } public State state { get; private set; }
public Role role { get; private set; }
public string sid { get; private set; } public string sid { get; private set; }
public Type type_ { get; private set; } public TransportType type_ { get; private set; }
public Jid local_full_jid { get; private set; } public Jid local_full_jid { get; private set; }
public Jid peer_full_jid { get; private set; } public Jid peer_full_jid { get; private set; }
public Role content_creator { get; private set; } public Role content_creator { get; private set; }
@ -392,25 +398,30 @@ public class Session {
public IOStream conn { get { return connection; } } public IOStream conn { get { return connection; } }
// INITIATE_SENT | INITIATE_RECEIVED | CONNECTING // INITIATE_SENT | INITIATE_RECEIVED | CONNECTING
Set<string> tried_transport_methods = new HashSet<string>();
TransportParameters? transport = null; TransportParameters? transport = null;
SessionTerminate session_terminate_handler; SessionTerminate session_terminate_handler;
public Session.initiate_sent(string sid, Type type, TransportParameters transport, Jid local_full_jid, Jid peer_full_jid, string content_name, owned SessionTerminate session_terminate_handler) { public Session.initiate_sent(string sid, TransportType type, TransportParameters transport, Jid local_full_jid, Jid peer_full_jid, string content_name, owned SessionTerminate session_terminate_handler) {
this.state = State.INITIATE_SENT; this.state = State.INITIATE_SENT;
this.role = Role.INITIATOR;
this.sid = sid; this.sid = sid;
this.type_ = type; this.type_ = type;
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;
this.content_creator = Role.INITIATOR; this.content_creator = Role.INITIATOR;
this.content_name = content_name; this.content_name = content_name;
this.tried_transport_methods = new HashSet<string>();
this.tried_transport_methods.add(transport.transport_ns_uri());
this.transport = transport; this.transport = transport;
this.connection = new Connection(this); this.connection = new Connection(this);
this.session_terminate_handler = (owned)session_terminate_handler; this.session_terminate_handler = (owned)session_terminate_handler;
} }
public Session.initiate_received(string sid, Type type, TransportParameters? transport, Jid local_full_jid, Jid peer_full_jid, string content_name, owned SessionTerminate session_terminate_handler) { public Session.initiate_received(string sid, TransportType type, TransportParameters? transport, Jid local_full_jid, Jid peer_full_jid, string content_name, owned SessionTerminate session_terminate_handler) {
this.state = State.INITIATE_RECEIVED; this.state = State.INITIATE_RECEIVED;
this.role = Role.RESPONDER;
this.sid = sid; this.sid = sid;
this.type_ = type; this.type_ = type;
this.local_full_jid = local_full_jid; this.local_full_jid = local_full_jid;
@ -418,39 +429,87 @@ public class Session {
this.content_creator = Role.INITIATOR; this.content_creator = Role.INITIATOR;
this.content_name = content_name; this.content_name = content_name;
this.transport = transport; this.transport = transport;
this.tried_transport_methods = new HashSet<string>();
if (transport != null) {
this.tried_transport_methods.add(transport.transport_ns_uri());
}
this.connection = new Connection(this); this.connection = new Connection(this);
this.session_terminate_handler = (owned)session_terminate_handler; this.session_terminate_handler = (owned)session_terminate_handler;
} }
public void handle_iq_set(XmppStream stream, string action, StanzaNode jingle, Iq.Stanza iq) throws IqError { public void handle_iq_set(XmppStream stream, string action, StanzaNode jingle, Iq.Stanza iq) throws IqError {
// Validate action.
switch (action) { switch (action) {
case "session-accept": case "session-accept":
if (state != State.INITIATE_SENT) {
throw new IqError.OUT_OF_ORDER("got session-accept while not waiting for one");
}
handle_session_accept(stream, jingle, iq);
break;
case "session-terminate": case "session-terminate":
handle_session_terminate(stream, jingle, iq); case "transport-accept":
break; case "transport-reject":
case "transport-replace":
case "transport-info": case "transport-info":
handle_transport_info(stream, jingle, iq); break;
return;
case "content-accept": case "content-accept":
case "content-add": case "content-add":
case "content-modify": case "content-modify":
case "content-reject": case "content-reject":
case "content-remove": case "content-remove":
case "security-info": case "security-info":
case "transport-accept":
case "transport-reject":
case "transport-replace":
throw new IqError.NOT_IMPLEMENTED(@"$(action) is not implemented");
default: default:
throw new IqError.BAD_REQUEST("invalid action"); throw new IqError.BAD_REQUEST("invalid action");
} }
ContentNode? content = null;
StanzaNode? transport = null;
// Do some pre-processing.
if (action != "session-terminate") {
content = get_single_content_node(jingle);
verify_content(content);
switch (action) {
case "transport-accept":
case "transport-reject":
case "transport-replace":
case "transport-info":
switch (state) {
case State.INITIATE_SENT:
case State.REPLACING_TRANSPORT:
case State.INITIATE_RECEIVED:
case State.WAITING_FOR_TRANSPORT_REPLACE:
case State.CONNECTING:
break;
default:
throw new IqError.OUT_OF_ORDER("transport-* unsupported after connection setup");
}
// TODO(hrxi): What to do with description nodes?
if (content.transport == null) {
throw new IqError.BAD_REQUEST("missing transport node");
}
transport = content.transport;
break;
}
}
switch (action) {
case "session-accept":
if (state != State.INITIATE_SENT) {
throw new IqError.OUT_OF_ORDER("got session-accept while not waiting for one");
}
handle_session_accept(stream, content, jingle, iq);
break;
case "session-terminate":
handle_session_terminate(stream, jingle, iq);
break;
case "transport-accept":
handle_transport_accept(stream, transport, jingle, iq);
break;
case "transport-reject":
handle_transport_reject(stream, jingle, iq);
break;
case "transport-replace":
handle_transport_replace(stream, transport, jingle, iq);
break;
case "transport-info":
handle_transport_info(stream, transport, jingle, iq);
break;
}
} }
void handle_session_accept(XmppStream stream, StanzaNode jingle, Iq.Stanza iq) throws IqError { void handle_session_accept(XmppStream stream, ContentNode content, StanzaNode jingle, Iq.Stanza iq) throws IqError {
string? responder_str = jingle.get_attribute("responder"); string? responder_str = jingle.get_attribute("responder");
Jid responder; Jid responder;
if (responder_str != null) { if (responder_str != null) {
@ -462,8 +521,6 @@ public class Session {
if (!responder.is_full()) { if (!responder.is_full()) {
throw new IqError.BAD_REQUEST("invalid responder JID"); throw new IqError.BAD_REQUEST("invalid responder JID");
} }
ContentNode content = get_single_content_node(jingle);
verify_content(content);
if (content.description == null || content.transport == null) { if (content.description == null || content.transport == null) {
throw new IqError.BAD_REQUEST("missing description or transport node"); throw new IqError.BAD_REQUEST("missing description or transport node");
} }
@ -472,8 +529,9 @@ public class Session {
} }
transport.on_transport_accept(content.transport); transport.on_transport_accept(content.transport);
StanzaNode description = content.description; // TODO(hrxi): handle this :P StanzaNode description = content.description; // TODO(hrxi): handle this :P
state = State.CONNECTING;
stream.get_module(Iq.Module.IDENTITY).send_iq(stream, new Iq.Stanza.result(iq)); stream.get_module(Iq.Module.IDENTITY).send_iq(stream, new Iq.Stanza.result(iq));
state = State.CONNECTING;
transport.create_transport_connection(stream, this); transport.create_transport_connection(stream, this);
} }
void connection_created(XmppStream stream, IOStream? conn) { void connection_created(XmppStream stream, IOStream? conn) {
@ -483,12 +541,14 @@ public class Session {
if (conn != null) { if (conn != null) {
state = State.ACTIVE; state = State.ACTIVE;
transport = null; transport = null;
tried_transport_methods.clear();
connection.set_inner(conn); connection.set_inner(conn);
} else { } else {
// TODO(hrxi): try negotiating other transports if (role == Role.INITIATOR) {
StanzaNode reason = new StanzaNode.build("reason", NS_URI) select_new_transport(stream);
.put_node(new StanzaNode.build("failed-transport", NS_URI)); } else {
terminate(reason, "failed transport"); state = State.WAITING_FOR_TRANSPORT_REPLACE;
}
} }
} }
void handle_session_terminate(XmppStream stream, StanzaNode jingle, Iq.Stanza iq) throws IqError { void handle_session_terminate(XmppStream stream, StanzaNode jingle, Iq.Stanza iq) throws IqError {
@ -499,17 +559,88 @@ public class Session {
stream.get_module(Iq.Module.IDENTITY).send_iq(stream, new Iq.Stanza.result(iq)); stream.get_module(Iq.Module.IDENTITY).send_iq(stream, new Iq.Stanza.result(iq));
// TODO(hrxi): also handle presence type=unavailable // TODO(hrxi): also handle presence type=unavailable
} }
void handle_transport_info(XmppStream stream, StanzaNode jingle, Iq.Stanza iq) throws IqError { void select_new_transport(XmppStream stream) {
if (state != State.INITIATE_RECEIVED && state != State.INITIATE_SENT && state != State.CONNECTING) { Transport? new_transport = stream.get_module(Module.IDENTITY).select_transport(stream, type_, peer_full_jid, tried_transport_methods);
stream.get_module(Iq.Module.IDENTITY).send_iq(stream, new Iq.Stanza.result(iq)); if (new_transport == null) {
throw new IqError.UNSUPPORTED_INFO("transport-info unsupported after connection setup"); StanzaNode reason = new StanzaNode.build("reason", NS_URI)
.put_node(new StanzaNode.build("failed-transport", NS_URI));
terminate(reason, "failed transport");
return;
} }
ContentNode content = get_single_content_node(jingle); tried_transport_methods.add(new_transport.transport_ns_uri());
verify_content(content); transport = new_transport.create_transport_parameters(stream, local_full_jid, peer_full_jid);
if (content.description != null || content.transport == null) { StanzaNode jingle = new StanzaNode.build("jingle", NS_URI)
throw new IqError.BAD_REQUEST("unexpected description node or missing transport node"); .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");
} }
transport.on_transport_info(content.transport); 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(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)); stream.get_module(Iq.Module.IDENTITY).send_iq(stream, new Iq.Stanza.result(iq));
} }
void verify_content(ContentNode content) throws IqError { void verify_content(ContentNode content) throws IqError {

View file

@ -172,6 +172,7 @@ class Parameters : Jingle.TransportParameters, Object {
string? waiting_for_activation_cid = null; string? waiting_for_activation_cid = null;
SourceFunc waiting_for_activation_callback; SourceFunc waiting_for_activation_callback;
bool waiting_for_activation_error = false;
private static string calculate_dstaddr(string sid, Jid first_jid, Jid second_jid) { private static string calculate_dstaddr(string sid, Jid first_jid, Jid second_jid) {
string hashed = sid + first_jid.to_string() + second_jid.to_string(); string hashed = sid + first_jid.to_string() + second_jid.to_string();
@ -200,7 +201,6 @@ class Parameters : Jingle.TransportParameters, Object {
throw new Jingle.IqError.BAD_REQUEST("too long dstaddr"); throw new Jingle.IqError.BAD_REQUEST("too long dstaddr");
} }
Parameters result = new Parameters(Jingle.Role.RESPONDER, sid, local_full_jid, peer_full_jid, dstaddr); Parameters result = new Parameters(Jingle.Role.RESPONDER, sid, local_full_jid, peer_full_jid, dstaddr);
//result.remote_candidates.add(new Candidate("b", "0.0.0.0", new Jid("a@b/c"), 1234, 2000000000, CandidateType.PROXY));
foreach (StanzaNode candidate in transport.get_subnodes("candidate", NS_URI)) { foreach (StanzaNode candidate in transport.get_subnodes("candidate", NS_URI)) {
result.remote_candidates.add(Candidate.parse(candidate)); result.remote_candidates.add(Candidate.parse(candidate));
} }
@ -237,10 +237,12 @@ class Parameters : Jingle.TransportParameters, Object {
StanzaNode? candidate_error = transport.get_subnode("candidate-error", NS_URI); StanzaNode? candidate_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);
StanzaNode? proxy_error = transport.get_subnode("proxy-error", NS_URI);
int num_children = 0; int num_children = 0;
if (candidate_error != null) { num_children += 1; } if (candidate_error != null) { num_children += 1; }
if (candidate_used != null) { num_children += 1; } if (candidate_used != null) { num_children += 1; }
if (activated != null) { num_children += 1; } if (activated != null) { num_children += 1; }
if (proxy_error != null) { num_children += 1; }
if (num_children == 0) { if (num_children == 0) {
throw new Jingle.IqError.UNSUPPORTED_INFO("unknown transport-info"); throw new Jingle.IqError.UNSUPPORTED_INFO("unknown transport-info");
} else if (num_children > 1) { } else if (num_children > 1) {
@ -263,6 +265,9 @@ class Parameters : Jingle.TransportParameters, Object {
} }
handle_activated(cid); handle_activated(cid);
} }
if (proxy_error != null) {
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) {
@ -291,6 +296,15 @@ 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 {
if (waiting_for_activation_cid == null) {
throw new Jingle.IqError.BAD_REQUEST("unexpected proxy error message");
}
Idle.add((owned)waiting_for_activation_callback);
waiting_for_activation_cid = null;
waiting_for_activation_error = true;
}
private void try_completing_negotiation() { 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;
@ -346,7 +360,11 @@ class Parameters : Jingle.TransportParameters, Object {
if (strong == null) { if (strong == null) {
return; return;
} }
strong.set_transport_connection(hack, conn); if (!waiting_for_activation_error) {
strong.set_transport_connection(hack, conn);
} else {
strong.set_transport_connection(hack, null);
}
} }
public async void connect_to_local_candidate(Candidate candidate) { public async void connect_to_local_candidate(Candidate candidate) {
try { try {