| 1 |
using Gee; |
| 2 |
using Xmpp; |
| 3 |
|
| 4 |
public class Xmpp.Xep.Jingle.Content : Object { |
| 5 |
|
| 6 |
public signal void senders_modify_incoming(Senders proposed_senders); |
| 7 |
|
| 8 |
// INITIATE_SENT -> CONNECTING -> [REPLACING_TRANSPORT -> CONNECTING ->]... ACTIVE -> ENDED |
| 9 |
// INITIATE_RECEIVED -> CONNECTING -> [WAITING_FOR_TRANSPORT_REPLACE -> CONNECTING ->].. ACTIVE -> ENDED |
| 10 |
public enum State { |
| 11 |
PENDING, |
| 12 |
WANTS_TO_BE_ACCEPTED, |
| 13 |
ACCEPTED, |
| 14 |
REPLACING_TRANSPORT, |
| 15 |
WAITING_FOR_TRANSPORT_REPLACE |
| 16 |
} |
| 17 |
|
| 18 |
public State state { get; set; } |
| 19 |
|
| 20 |
public Role role { get; private set; } |
| 21 |
public Jid local_full_jid { get; private set; } |
| 22 |
public Jid peer_full_jid { get; private set; } |
| 23 |
public Role content_creator { get; private set; } |
| 24 |
public string content_name { get; private set; } |
| 25 |
public Senders senders { get; private set; } |
| 26 |
|
| 27 |
public ContentType content_type; |
| 28 |
public ContentParameters content_params; |
| 29 |
public Transport transport; |
| 30 |
public TransportParameters? transport_params; |
| 31 |
public SecurityPrecondition security_precondition; |
| 32 |
public SecurityParameters? security_params; |
| 33 |
|
| 34 |
public weak Session session; |
| 35 |
public Map<uint8, ComponentConnection> component_connections = new HashMap<uint8, ComponentConnection>(); // TODO private |
| 36 |
|
| 37 |
public HashMap<string, ContentEncryption> encryptions = new HashMap<string, ContentEncryption>(); |
| 38 |
|
| 39 |
private Set<string> tried_transport_methods = new HashSet<string>(); |
| 40 |
|
| 41 |
|
| 42 |
public Content.initiate_sent(string content_name, Senders senders, |
| 43 |
ContentType content_type, ContentParameters content_params, |
| 44 |
Transport transport, TransportParameters? transport_params, |
| 45 |
SecurityPrecondition? security_precondition, SecurityParameters? security_params, |
| 46 |
Jid local_full_jid, Jid peer_full_jid) { |
| 47 |
this.content_name = content_name; |
| 48 |
this.senders = senders; |
| 49 |
this.role = Role.INITIATOR; |
| 50 |
this.local_full_jid = local_full_jid; |
| 51 |
this.peer_full_jid = peer_full_jid; |
| 52 |
this.content_creator = Role.INITIATOR; |
| 53 |
|
| 54 |
this.content_type = content_type; |
| 55 |
this.content_params = content_params; |
| 56 |
this.transport = transport; |
| 57 |
this.transport_params = transport_params; |
| 58 |
this.security_precondition = security_precondition; |
| 59 |
this.security_params = security_params; |
| 60 |
|
| 61 |
this.tried_transport_methods.add(transport.ns_uri); |
| 62 |
|
| 63 |
state = State.PENDING; |
| 64 |
} |
| 65 |
|
| 66 |
public Content.initiate_received(string content_name, Senders senders, |
| 67 |
ContentType content_type, ContentParameters content_params, |
| 68 |
Transport transport, TransportParameters? transport_params, |
| 69 |
SecurityPrecondition? security_precondition, SecurityParameters? security_params, |
| 70 |
Jid local_full_jid, Jid peer_full_jid) throws Error { |
| 71 |
this.content_name = content_name; |
| 72 |
this.senders = senders; |
| 73 |
this.role = Role.RESPONDER; |
| 74 |
this.local_full_jid = local_full_jid; |
| 75 |
this.peer_full_jid = peer_full_jid; |
| 76 |
this.content_creator = Role.INITIATOR; |
| 77 |
|
| 78 |
this.content_type = content_type; |
| 79 |
this.content_params = content_params; |
| 80 |
this.transport = transport; |
| 81 |
this.transport_params = transport_params; |
| 82 |
this.security_precondition = security_precondition; |
| 83 |
this.security_params = security_params; |
| 84 |
|
| 85 |
if (transport != null) { |
| 86 |
this.tried_transport_methods.add(transport.ns_uri); |
| 87 |
} |
| 88 |
|
| 89 |
state = State.PENDING; |
| 90 |
} |
| 91 |
|
| 92 |
public void set_session(Session session) { |
| 93 |
this.session = session; |
| 94 |
this.transport_params.set_content(this); |
| 95 |
} |
| 96 |
|
| 97 |
public void accept() { |
| 98 |
if (state != State.PENDING) { |
| 99 |
warning("accepting a non-pending content"); |
| 100 |
return; |
| 101 |
} |
| 102 |
state = State.WANTS_TO_BE_ACCEPTED; |
| 103 |
session.accept_content(this); |
| 104 |
} |
| 105 |
|
| 106 |
public void reject() { |
| 107 |
if (state != State.PENDING) { |
| 108 |
warning("rejecting a non-pending content"); |
| 109 |
return; |
| 110 |
} |
| 111 |
session.reject_content(this); |
| 112 |
} |
| 113 |
|
| 114 |
public void terminate(bool we_terminated, string? reason_name, string? reason_text) { |
| 115 |
if (state == State.PENDING) { |
| 116 |
warning("terminating a pending call"); |
| 117 |
return; |
| 118 |
} |
| 119 |
content_params.terminate(we_terminated, reason_name, reason_text); |
| 120 |
transport_params.dispose(); |
| 121 |
|
| 122 |
foreach (ComponentConnection connection in component_connections.values) { |
| 123 |
connection.terminate.begin(we_terminated, reason_name, reason_text); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
public void modify(Senders new_sender) { |
| 128 |
session.send_content_modify(this, new_sender); |
| 129 |
this.senders = new_sender; |
| 130 |
} |
| 131 |
|
| 132 |
public void accept_content_modify(Senders senders) { |
| 133 |
this.senders = senders; |
| 134 |
} |
| 135 |
|
| 136 |
internal void handle_content_modify(XmppStream stream, Senders proposed_senders) { |
| 137 |
senders_modify_incoming(proposed_senders); |
| 138 |
} |
| 139 |
|
| 140 |
internal void on_accept(XmppStream stream) { |
| 141 |
this.transport_params.create_transport_connection(stream, this); |
| 142 |
this.content_params.accept(stream, session, this); |
| 143 |
} |
| 144 |
|
| 145 |
internal void handle_accept(XmppStream stream, ContentNode content_node) { |
| 146 |
this.transport_params.handle_transport_accept(content_node.transport); |
| 147 |
this.transport_params.create_transport_connection(stream, this); |
| 148 |
this.content_params.handle_accept(stream, this.session, this, content_node.description); |
| 149 |
} |
| 150 |
|
| 151 |
public async void select_new_transport() { |
| 152 |
XmppStream stream = session.stream; |
| 153 |
Transport? new_transport = yield stream.get_module(Module.IDENTITY).select_transport(stream, transport.type_, transport_params.components, peer_full_jid, tried_transport_methods); |
| 154 |
if (new_transport == null) { |
| 155 |
session.terminate(ReasonElement.FAILED_TRANSPORT, null, "failed transport"); |
| 156 |
// TODO should we only terminate this content or really the whole session? |
| 157 |
return; |
| 158 |
} |
| 159 |
tried_transport_methods.add(new_transport.ns_uri); |
| 160 |
transport_params = new_transport.create_transport_parameters(stream, transport_params.components, local_full_jid, peer_full_jid); |
| 161 |
set_transport_params(transport_params); |
| 162 |
session.send_transport_replace(this, transport_params); |
| 163 |
state = State.REPLACING_TRANSPORT; |
| 164 |
} |
| 165 |
|
| 166 |
public void handle_transport_accept(XmppStream stream, StanzaNode transport_node, StanzaNode jingle, Iq.Stanza iq) throws IqError { |
| 167 |
if (state != State.REPLACING_TRANSPORT) { |
| 168 |
throw new IqError.OUT_OF_ORDER("no outstanding transport-replace request"); |
| 169 |
} |
| 170 |
if (transport_node.ns_uri != transport.ns_uri) { |
| 171 |
throw new IqError.BAD_REQUEST("transport-accept with unnegotiated transport method"); |
| 172 |
} |
| 173 |
transport_params.handle_transport_accept(transport_node); |
| 174 |
stream.get_module(Iq.Module.IDENTITY).send_iq(stream, new Iq.Stanza.result(iq)); |
| 175 |
transport_params.create_transport_connection(stream, this); |
| 176 |
} |
| 177 |
|
| 178 |
public void handle_transport_reject(XmppStream stream, StanzaNode jingle, Iq.Stanza iq) throws IqError { |
| 179 |
if (state != State.REPLACING_TRANSPORT) { |
| 180 |
throw new IqError.OUT_OF_ORDER("no outstanding transport-replace request"); |
| 181 |
} |
| 182 |
stream.get_module(Iq.Module.IDENTITY).send_iq(stream, new Iq.Stanza.result(iq)); |
| 183 |
select_new_transport.begin(); |
| 184 |
} |
| 185 |
|
| 186 |
public void handle_transport_replace(XmppStream stream, StanzaNode transport_node, StanzaNode jingle, Iq.Stanza iq) throws IqError { |
| 187 |
Transport? transport = stream.get_module(Module.IDENTITY).get_transport(transport_node.ns_uri); |
| 188 |
TransportParameters? parameters = null; |
| 189 |
if (transport != null) { |
| 190 |
// Just parse the transport info for the errors. |
| 191 |
parameters = transport.parse_transport_parameters(stream, content_type.required_components, local_full_jid, peer_full_jid, transport_node); |
| 192 |
} |
| 193 |
stream.get_module(Iq.Module.IDENTITY).send_iq(stream, new Iq.Stanza.result(iq)); |
| 194 |
if (state != State.WAITING_FOR_TRANSPORT_REPLACE || transport == null) { |
| 195 |
session.send_transport_reject(this, transport_node); |
| 196 |
return; |
| 197 |
} |
| 198 |
set_transport_params(parameters); |
| 199 |
session.send_transport_accept(this, parameters); |
| 200 |
|
| 201 |
this.transport_params.create_transport_connection(stream, this); |
| 202 |
} |
| 203 |
|
| 204 |
public void handle_transport_info(XmppStream stream, StanzaNode transport, StanzaNode jingle, Iq.Stanza iq) throws IqError { |
| 205 |
this.transport_params.handle_transport_info(transport); |
| 206 |
stream.get_module(Iq.Module.IDENTITY).send_iq(stream, new Iq.Stanza.result(iq)); |
| 207 |
} |
| 208 |
|
| 209 |
public void on_description_info(XmppStream stream, StanzaNode description, StanzaNode jinglq, Iq.Stanza iq) throws IqError { |
| 210 |
// TODO: do something. |
| 211 |
stream.get_module(Iq.Module.IDENTITY).send_iq(stream, new Iq.Stanza.result(iq)); |
| 212 |
} |
| 213 |
|
| 214 |
public void set_transport_connection(ComponentConnection? conn, uint8 component = 1) { |
| 215 |
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()); |
| 216 |
|
| 217 |
if (conn != null) { |
| 218 |
component_connections[component] = conn; |
| 219 |
if (transport_params.components == component) { |
| 220 |
state = State.ACCEPTED; |
| 221 |
tried_transport_methods.clear(); |
| 222 |
} |
| 223 |
} else { |
| 224 |
if (role == Role.INITIATOR) { |
| 225 |
select_new_transport.begin(); |
| 226 |
} else { |
| 227 |
state = State.WAITING_FOR_TRANSPORT_REPLACE; |
| 228 |
} |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
private void set_transport_params(TransportParameters transport_params) { |
| 233 |
this.transport_params = transport_params; |
| 234 |
} |
| 235 |
|
| 236 |
public ComponentConnection? get_transport_connection(uint8 component = 1) { |
| 237 |
return component_connections[component]; |
| 238 |
} |
| 239 |
|
| 240 |
public void send_transport_info(StanzaNode transport) { |
| 241 |
session.send_transport_info(this, transport); |
| 242 |
} |
| 243 |
|
| 244 |
internal StanzaNode build_outer_content_node() { |
| 245 |
return new StanzaNode.build("content", NS_URI) |
| 246 |
.put_attribute("creator", content_creator.to_string()) |
| 247 |
.put_attribute("name", content_name); |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
public class Xmpp.Xep.Jingle.ContentEncryption : Object { |
| 252 |
public string encryption_ns; |
| 253 |
public string encryption_name; |
| 254 |
public uint8[] our_key; |
| 255 |
public uint8[] peer_key; |
| 256 |
|
| 257 |
public ContentEncryption(string encryption_ns, string encryption_name, uint8[] our_key = new uint8[]{}, uint8[] peer_key = new uint8[]{}) { |
| 258 |
this.encryption_ns = encryption_ns; |
| 259 |
this.encryption_name = encryption_name; |
| 260 |
this.our_key = our_key; |
| 261 |
this.peer_key = peer_key; |
| 262 |
} |
| 263 |
} |