Debian Codegraph @

xmpp-vala/src/module/xep/0166_jingle/session.vala

raw
1 using Gee;
2 using Xmpp;
3
4
5 public delegate void Xmpp.Xep.Jingle.SessionTerminate(Jid to, string sid, StanzaNode reason);
6
7 public class Xmpp.Xep.Jingle.Session : Object {
8
9 public signal void terminated(XmppStream stream, bool we_terminated, string? reason_name, string? reason_text);
10 public signal void additional_content_add_incoming(XmppStream stream, Content content);
11
12 // INITIATE_SENT/INITIATE_RECEIVED -> CONNECTING -> PENDING -> ACTIVE -> ENDED
13 public enum State {
14 INITIATE_SENT,
15 INITIATE_RECEIVED,
16 ACTIVE,
17 ENDED,
18 }
19
20 public XmppStream stream { get; set; }
21 public State state { get; set; }
22 public string sid { get; private set; }
23 public Jid local_full_jid { get; private set; }
24 public Jid peer_full_jid { get; private set; }
25 public bool we_initiated { get; private set; }
26
27 public HashMap<string, Content> contents_map = new HashMap<string, Content>();
28 public Gee.List<Content> contents = new ArrayList<Content>(); // Keep the order contents
29
30 public SecurityParameters? security { get { return contents.to_array()[0].security_params; } }
31
32 public Jid muji_room { get; set; }
33
34 public Session.initiate_sent(XmppStream stream, string sid, Jid local_full_jid, Jid peer_full_jid) {
35 this.stream = stream;
36 this.sid = sid;
37 this.local_full_jid = local_full_jid;
38 this.peer_full_jid = peer_full_jid;
39 this.state = State.INITIATE_SENT;
40 this.we_initiated = true;
41 }
42
43 public Session.initiate_received(XmppStream stream, string sid, Jid local_full_jid, Jid peer_full_jid) {
44 this.stream = stream;
45 this.sid = sid;
46 this.local_full_jid = local_full_jid;
47 this.peer_full_jid = peer_full_jid;
48 this.state = State.INITIATE_RECEIVED;
49 this.we_initiated = false;
50 }
51
52 public void handle_iq_set(string action, StanzaNode jingle, Iq.Stanza iq) throws IqError {
53
54 if (action.has_prefix("session-")) {
55 switch (action) {
56 case "session-accept":
57 Gee.List<ContentNode> content_nodes = get_content_nodes(jingle);
58
59 if (state != State.INITIATE_SENT) {
60 throw new IqError.OUT_OF_ORDER("got session-accept while not waiting for one");
61 }
62 handle_session_accept(content_nodes, jingle, iq);
63 break;
64 case "session-info":
65 handle_session_info.begin(jingle, iq);
66 break;
67 case "session-terminate":
68 handle_session_terminate(jingle, iq);
69 break;
70 default:
71 throw new IqError.BAD_REQUEST("invalid action");
72 }
73
74
75 } else if (action.has_prefix("content-")) {
76 switch (action) {
77 case "content-accept":
78 ContentNode content_node = get_single_content_node(jingle);
79 handle_content_accept(content_node);
80 stream.get_module(Iq.Module.IDENTITY).send_iq(stream, new Iq.Stanza.result(iq));
81 break;
82 case "content-add":
83 ContentNode content_node = get_single_content_node(jingle);
84 insert_content_node.begin(content_node, peer_full_jid);
85 stream.get_module(Iq.Module.IDENTITY).send_iq(stream, new Iq.Stanza.result(iq));
86 break;
87 case "content-modify":
88 handle_content_modify(stream, jingle, iq);
89 break;
90 case "content-reject":
91 case "content-remove":
92 throw new IqError.NOT_IMPLEMENTED(@"$(action) is not implemented");
93 default:
94 throw new IqError.BAD_REQUEST("invalid action");
95 }
96
97
98 } else if (action.has_prefix("transport-")) {
99 ContentNode content_node = get_single_content_node(jingle);
100 if (!contents_map.has_key(content_node.name)) {
101 throw new IqError.BAD_REQUEST("unknown content");
102 }
103
104 if (content_node.transport == null) {
105 throw new IqError.BAD_REQUEST("missing transport node");
106 }
107
108 Content content = contents_map[content_node.name];
109
110 if (content_node.creator != content.content_creator) warning("Received transport-* with unexpected content creator from %s", peer_full_jid.to_string());
111
112 switch (action) {
113 case "transport-accept":
114 content.handle_transport_accept(stream, content_node.transport, jingle, iq);
115 break;
116 case "transport-info":
117 content.handle_transport_info(stream, content_node.transport, jingle, iq);
118 break;
119 case "transport-reject":
120 content.handle_transport_reject(stream, jingle, iq);
121 break;
122 case "transport-replace":
123 content.handle_transport_replace(stream, content_node.transport, jingle, iq);
124 break;
125 default:
126 throw new IqError.BAD_REQUEST("invalid action");
127 }
128
129
130 } else if (action == "description-info") {
131 ContentNode content_node = get_single_content_node(jingle);
132 if (!contents_map.has_key(content_node.name)) {
133 throw new IqError.BAD_REQUEST("unknown content");
134 }
135
136 Content content = contents_map[content_node.name];
137
138 if (content_node.creator != content.content_creator) warning("Received description-info with unexpected content creator from %s", peer_full_jid.to_string());
139
140 content.on_description_info(stream, content_node.description, jingle, iq);
141 } else if (action == "security-info") {
142 throw new IqError.NOT_IMPLEMENTED(@"$(action) is not implemented");
143
144
145 } else {
146 throw new IqError.BAD_REQUEST("invalid action");
147 }
148 }
149
150 internal void insert_content(Content content) {
151 this.contents_map[content.content_name] = content;
152 this.contents.add(content);
153 content.set_session(this);
154 }
155
156 internal async void insert_content_node(ContentNode content_node, Jid peer_full_jid) throws IqError {
157 if (content_node.description == null || content_node.transport == null) {
158 throw new IqError.BAD_REQUEST("missing description or transport node");
159 }
160
161 Jid? my_jid = stream.get_flag(Bind.Flag.IDENTITY).my_jid;
162
163 Transport? transport = stream.get_module(Jingle.Module.IDENTITY).get_transport(content_node.transport.ns_uri);
164 ContentType? content_type = stream.get_module(Jingle.Module.IDENTITY).get_content_type(content_node.description.ns_uri);
165
166 if (content_type == null) {
167 // TODO(hrxi): how do we signal an unknown content type?
168 throw new IqError.NOT_IMPLEMENTED("unknown content type");
169 }
170
171 TransportParameters? transport_params = null;
172 if (transport != null) {
173 transport_params = transport.parse_transport_parameters(stream, content_type.required_components, my_jid, peer_full_jid, content_node.transport);
174 } else {
175 // terminate the session below
176 }
177
178 ContentParameters content_params = content_type.parse_content_parameters(content_node.description);
179
180 SecurityPrecondition? precondition = content_node.security != null ? stream.get_module(Jingle.Module.IDENTITY).get_security_precondition(content_node.security.ns_uri) : null;
181 SecurityParameters? security_params = null;
182 if (precondition != null) {
183 debug("Using precondition %s", precondition.security_ns_uri());
184 security_params = precondition.parse_security_parameters(stream, my_jid, peer_full_jid, content_node.security);
185 } else if (content_node.security != null) {
186 throw new IqError.NOT_IMPLEMENTED("unknown security precondition");
187 }
188
189 TransportType type = content_type.required_transport_type;
190
191 if (transport == null || transport.type_ != type) {
192 terminate(ReasonElement.UNSUPPORTED_TRANSPORTS, null, "unsupported transports");
193 throw new IqError.NOT_IMPLEMENTED("unsupported transports");
194 }
195
196 Content content = new Content.initiate_received(content_node.name, content_node.senders,
197 content_type, content_params,
198 transport, transport_params,
199 precondition, security_params,
200 my_jid, peer_full_jid);
201 insert_content(content);
202
203 yield content_params.handle_proposed_content(stream, this, content);
204
205 if (this.state == State.ACTIVE) {
206 additional_content_add_incoming(stream, content);
207 }
208 }
209
210 public async void add_content(Content content) {
211 insert_content(content);
212
213 StanzaNode content_add_node = new StanzaNode.build("jingle", NS_URI)
214 .add_self_xmlns()
215 .put_attribute("action", "content-add")
216 .put_attribute("sid", sid)
217 .put_node(new StanzaNode.build("content", NS_URI)
218 .put_attribute("creator", "initiator")
219 .put_attribute("name", content.content_name)
220 .put_attribute("senders", content.senders.to_string())
221 .put_node(content.content_params.get_description_node())
222 .put_node(content.transport_params.to_transport_stanza_node("content-add")));
223
224 Iq.Stanza iq = new Iq.Stanza.set(content_add_node) { to=peer_full_jid };
225 yield stream.get_module(Iq.Module.IDENTITY).send_iq_async(stream, iq);
226 }
227
228 private void handle_content_accept(ContentNode content_node) throws IqError {
229 if (content_node.description == null || content_node.transport == null) throw new IqError.BAD_REQUEST("missing description or transport node");
230 if (!contents_map.has_key(content_node.name)) throw new IqError.BAD_REQUEST("unknown content");
231
232 Content content = contents_map[content_node.name];
233
234 if (content_node.creator != content.content_creator) warning("Counterpart accepts content with an unexpected `creator`");
235 if (content_node.senders != content.senders) warning("Counterpart accepts content with an unexpected `senders`");
236 if (content_node.transport.ns_uri != content.transport_params.ns_uri) throw new IqError.BAD_REQUEST("session-accept with unnegotiated transport method");
237
238 content.handle_accept(stream, content_node);
239 }
240
241 private void handle_content_modify(XmppStream stream, StanzaNode jingle_node, Iq.Stanza iq) throws IqError {
242 ContentNode content_node = get_single_content_node(jingle_node);
243
244 Content? content = contents_map[content_node.name];
245
246 if (content == null) throw new IqError.BAD_REQUEST("no such content");
247 if (content_node.creator != content.content_creator) throw new IqError.BAD_REQUEST("mismatching creator");
248
249 Iq.Stanza result_iq = new Iq.Stanza.result(iq) { to=peer_full_jid };
250 stream.get_module(Iq.Module.IDENTITY).send_iq(stream, result_iq);
251
252 content.handle_content_modify(stream, content_node.senders);
253 }
254
255 private void handle_session_accept(Gee.List<ContentNode> content_nodes, StanzaNode jingle, Iq.Stanza iq) throws IqError {
256 string? responder_str = jingle.get_attribute("responder");
257 Jid responder = iq.from;
258 if (responder_str != null) {
259 try {
260 responder = new Jid(responder_str);
261 } catch (InvalidJidError e) {
262 warning("Received invalid session accept: %s", e.message);
263 }
264 }
265
266 foreach (ContentNode content_node in content_nodes) {
267 handle_content_accept(content_node);
268 }
269 stream.get_module(Iq.Module.IDENTITY).send_iq(stream, new Iq.Stanza.result(iq));
270
271 state = State.ACTIVE;
272 }
273
274 private void handle_session_terminate(StanzaNode jingle, Iq.Stanza iq) throws IqError {
275 string? reason_text = null;
276 string? reason_name = null;
277 StanzaNode? reason_node = iq.stanza.get_deep_subnode(NS_URI + ":jingle", NS_URI + ":reason");
278 if (reason_node != null) {
279 if (reason_node.sub_nodes.size > 2) warning("Jingle session-terminate reason node w/ >2 subnodes: %s", iq.stanza.to_string());
280
281 StanzaNode? specific_reason_node = null;
282 StanzaNode? text_node = null;
283 foreach (StanzaNode node in reason_node.sub_nodes) {
284 if (node.name == "text") {
285 text_node = node;
286 } else if (node.ns_uri == NS_URI) {
287 specific_reason_node = node;
288 }
289 }
290 reason_name = specific_reason_node != null ? specific_reason_node.name : null;
291 reason_text = text_node != null ? text_node.get_string_content() : null;
292
293 if (reason_name != null && !(specific_reason_node.name in ReasonElement.NORMAL_TERMINATE_REASONS)) {
294 warning("Jingle session terminated: %s : %s", reason_name ?? "", reason_text ?? "");
295 } else {
296 debug("Jingle session terminated: %s : %s", reason_name ?? "", reason_text ?? "");
297 }
298 }
299
300 foreach (Content content in contents) {
301 content.terminate(false, reason_name, reason_text);
302 }
303
304 stream.get_module(Iq.Module.IDENTITY).send_iq(stream, new Iq.Stanza.result(iq));
305 // TODO(hrxi): also handle presence type=unavailable
306
307 state = State.ENDED;
308 terminated(stream, false, reason_name, reason_text);
309 }
310
311 private async void handle_session_info(StanzaNode jingle, Iq.Stanza iq) throws IqError {
312 StanzaNode? info = get_single_node_anyns(jingle);
313 if (info == null) {
314 // Jingle session ping
315 stream.get_module(Iq.Module.IDENTITY).send_iq(stream, new Iq.Stanza.result(iq));
316 return;
317 }
318 SessionInfoNs? info_ns = stream.get_module(Module.IDENTITY).get_session_info_type(info.ns_uri);
319 if (info_ns == null) {
320 throw new IqError.UNSUPPORTED_INFO("unknown session-info namespace");
321 }
322 info_ns.handle_content_session_info(stream, this, info, iq);
323
324 Iq.Stanza result_iq = new Iq.Stanza.result(iq) { to=peer_full_jid };
325 stream.get_module(Iq.Module.IDENTITY).send_iq(stream, result_iq);
326 }
327
328 private void accept() {
329 if (state != State.INITIATE_RECEIVED) critical("Accepting a stream, but we're the initiator");
330
331 StanzaNode jingle = new StanzaNode.build("jingle", NS_URI)
332 .add_self_xmlns()
333 .put_attribute("action", "session-accept")
334 .put_attribute("sid", sid);
335 foreach (Content content in contents) {
336 StanzaNode content_node = new StanzaNode.build("content", NS_URI)
337 .put_attribute("creator", "initiator")
338 .put_attribute("name", content.content_name)
339 .put_attribute("senders", content.senders.to_string())
340 .put_node(content.content_params.get_description_node())
341 .put_node(content.transport_params.to_transport_stanza_node("session-accept"));
342 jingle.put_node(content_node);
343 }
344
345 Iq.Stanza iq = new Iq.Stanza.set(jingle) { to=peer_full_jid };
346 stream.get_module(Iq.Module.IDENTITY).send_iq(stream, iq);
347
348
349 foreach (Content content2 in contents) {
350 content2.on_accept(stream);
351 }
352
353 state = State.ACTIVE;
354 }
355
356 internal void accept_content(Content content) {
357 if (state == State.INITIATE_RECEIVED) {
358 bool all_accepted = true;
359 foreach (Content c in contents) {
360 if (c.state != Content.State.WANTS_TO_BE_ACCEPTED) {
361 all_accepted = false;
362 }
363 }
364 if (all_accepted) {
365 accept();
366 }
367 } else if (state == State.ACTIVE) {
368 StanzaNode content_accept_node = new StanzaNode.build("jingle", NS_URI)
369 .add_self_xmlns()
370 .put_attribute("action", "content-accept")
371 .put_attribute("sid", sid)
372 .put_node(new StanzaNode.build("content", NS_URI)
373 .put_attribute("creator", "initiator")
374 .put_attribute("name", content.content_name)
375 .put_attribute("senders", content.senders.to_string())
376 .put_node(content.content_params.get_description_node())
377 .put_node(content.transport_params.to_transport_stanza_node("content-accept")));
378
379 Iq.Stanza iq = new Iq.Stanza.set(content_accept_node) { to=peer_full_jid };
380 stream.get_module(Iq.Module.IDENTITY).send_iq(stream, iq);
381
382 content.on_accept(stream);
383 }
384 }
385
386 private void reject() {
387 if (state != State.INITIATE_RECEIVED) critical("Accepting a stream, but we're the initiator");
388 terminate(ReasonElement.DECLINE, null, "declined");
389 }
390
391 internal void reject_content(Content content) {
392 if (state == State.INITIATE_RECEIVED) {
393 reject();
394 } else {
395 warning("not really handeling content rejects");
396 }
397 }
398
399 public void set_application_error(StanzaNode? application_reason = null) {
400 terminate(ReasonElement.FAILED_APPLICATION, null, "application error");
401 }
402
403 public void terminate(string? reason_name, string? reason_text, string? local_reason) {
404 if (state == State.ENDED) return;
405 debug("Jingle session %s terminated: %s; %s; %s", this.sid, reason_name ?? "-", reason_text ?? "-", local_reason ?? "-");
406
407 if (state == State.ACTIVE) {
408 string reason_str;
409 if (local_reason != null) {
410 reason_str = @"local session-terminate: $(local_reason)";
411 } else {
412 reason_str = "local session-terminate";
413 }
414 foreach (Content content in contents) {
415 content.terminate(true, reason_name, reason_text);
416 }
417 }
418
419 StanzaNode terminate_iq = new StanzaNode.build("jingle", NS_URI)
420 .add_self_xmlns()
421 .put_attribute("action", "session-terminate")
422 .put_attribute("sid", sid);
423 if (reason_name != null || reason_text != null) {
424 StanzaNode reason_node = new StanzaNode.build("reason", NS_URI);
425 if (reason_name != null) {
426 reason_node.put_node(new StanzaNode.build(reason_name, NS_URI));
427 }
428 if (reason_text != null) {
429 reason_node.put_node(new StanzaNode.build("text", NS_URI).put_node(new StanzaNode.text(reason_text)));
430 }
431 terminate_iq.put_node(reason_node);
432 }
433 Iq.Stanza iq = new Iq.Stanza.set(terminate_iq) { to=peer_full_jid };
434 stream.get_module(Iq.Module.IDENTITY).send_iq(stream, iq);
435
436 state = State.ENDED;
437 terminated(stream, true, reason_name, reason_text);
438 }
439
440 internal void send_session_info(StanzaNode child_node) {
441 if (state == State.ENDED) return;
442
443 StanzaNode jingle_node = build_outer_session_node("session-info").put_node(child_node);
444 Iq.Stanza iq = new Iq.Stanza.set(jingle_node) { to=peer_full_jid };
445 stream.get_module(Iq.Module.IDENTITY).send_iq(stream, iq);
446 }
447
448 internal void send_content_modify(Content content, Senders senders) {
449 if (state == State.ENDED) return;
450
451 StanzaNode jingle_node = build_outer_session_node("content-modify")
452 .put_node(content.build_outer_content_node()
453 .put_attribute("senders", senders.to_string()));
454
455 Iq.Stanza iq = new Iq.Stanza.set(jingle_node) { to=peer_full_jid };
456 stream.get_module(Iq.Module.IDENTITY).send_iq(stream, iq);
457 }
458
459 internal void send_transport_accept(Content content, TransportParameters transport_params) {
460 if (state == State.ENDED) return;
461
462 StanzaNode jingle_node = build_outer_session_node("transport-accept")
463 .put_node(content.build_outer_content_node()
464 .put_node(transport_params.to_transport_stanza_node("transport-accept")));
465
466 Iq.Stanza iq_response = new Iq.Stanza.set(jingle_node) { to=peer_full_jid };
467 stream.get_module(Iq.Module.IDENTITY).send_iq(stream, iq_response);
468 }
469
470 internal void send_transport_replace(Content content, TransportParameters transport_params) {
471 if (state == State.ENDED) return;
472
473 StanzaNode jingle_node = build_outer_session_node("transport-replace")
474 .put_node(content.build_outer_content_node()
475 .put_node(transport_params.to_transport_stanza_node("transport-replace")));
476
477 Iq.Stanza iq = new Iq.Stanza.set(jingle_node) { to=peer_full_jid };
478 stream.get_module(Iq.Module.IDENTITY).send_iq(stream, iq);
479 }
480
481 internal void send_transport_reject(Content content, StanzaNode transport_node) {
482 if (state == State.ENDED) return;
483
484 StanzaNode jingle_node = build_outer_session_node("transport-reject")
485 .put_node(content.build_outer_content_node().put_node(transport_node));
486
487 Iq.Stanza iq_response = new Iq.Stanza.set(jingle_node) { to=peer_full_jid };
488 stream.get_module(Iq.Module.IDENTITY).send_iq(stream, iq_response);
489 }
490
491 internal void send_transport_info(Content content, StanzaNode transport) {
492 if (state == State.ENDED) return;
493
494 StanzaNode jingle_node = build_outer_session_node("transport-info")
495 .put_node(content.build_outer_content_node().put_node(transport));
496
497 Iq.Stanza iq = new Iq.Stanza.set(jingle_node) { to=peer_full_jid };
498 stream.get_module(Iq.Module.IDENTITY).send_iq(stream, iq);
499 }
500
501 private StanzaNode build_outer_session_node(string action) {
502 return new StanzaNode.build("jingle", NS_URI)
503 .add_self_xmlns()
504 .put_attribute("action", action)
505 .put_attribute("initiator", we_initiated ? local_full_jid.to_string() : peer_full_jid.to_string())
506 .put_attribute("sid", sid);
507 }
508
509 public bool senders_include_us(Senders senders) {
510 switch (senders) {
511 case Senders.BOTH:
512 return true;
513 case Senders.NONE:
514 return false;
515 case Senders.INITIATOR:
516 return we_initiated;
517 case Senders.RESPONDER:
518 return !we_initiated;
519 }
520 assert_not_reached();
521 }
522
523 public bool senders_include_counterpart(Senders senders) {
524 switch (senders) {
525 case Senders.BOTH:
526 return true;
527 case Senders.NONE:
528 return false;
529 case Senders.INITIATOR:
530 return !we_initiated;
531 case Senders.RESPONDER:
532 return we_initiated;
533 }
534 assert_not_reached();
535 }
536 }