annotate libervia/backend/plugins/plugin_xep_0176.py @ 4340:ea72364131d5 default tip @

doc (components): Update Email Gateway documentation: A section has been added to explain how attachments are handled. fix 453
author Goffi <goffi@goffi.org>
date Tue, 03 Dec 2024 00:53:18 +0100
parents 4837ec911c43
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4045
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/env python3
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
2
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
3 # Libervia plugin for Jingle (XEP-0176)
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # Copyright (C) 2009-2023 Jérôme Poisson (goffi@goffi.org)
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
5
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
6 # This program is free software: you can redistribute it and/or modify
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # it under the terms of the GNU Affero General Public License as published by
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # the Free Software Foundation, either version 3 of the License, or
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # (at your option) any later version.
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
10
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
11 # This program is distributed in the hope that it will be useful,
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # GNU Affero General Public License for more details.
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
15
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
16 # You should have received a copy of the GNU Affero General Public License
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
18
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
19 from typing import Dict, List, Optional
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
20 import uuid
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
21
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
22 from twisted.internet import defer
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from twisted.words.protocols.jabber.xmlstream import XMPPHandler
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from twisted.words.xish import domish
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from wokkel import disco, iwokkel
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from zope.interface import implementer
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
27
4071
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4054
diff changeset
28 from libervia.backend.core import exceptions
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4054
diff changeset
29 from libervia.backend.core.constants import Const as C
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4054
diff changeset
30 from libervia.backend.core.core_types import SatXMPPEntity
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4054
diff changeset
31 from libervia.backend.core.i18n import _
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4054
diff changeset
32 from libervia.backend.core.log import getLogger
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4054
diff changeset
33 from libervia.backend.tools.common import data_format
4045
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
34
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
35 from .plugin_xep_0166 import BaseTransportHandler
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
36
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
37 log = getLogger(__name__)
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
38
4116
23fa52acf72c plugin XEP-0167, XEP-0176: transport-info and ICE candidate sending are delayed if session is not active yet
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
39 NS_JINGLE_ICE_UDP = "urn:xmpp:jingle:transports:ice-udp:1"
4045
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
40
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
41 PLUGIN_INFO = {
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
42 C.PI_NAME: "Jingle ICE-UDP Transport Method",
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
43 C.PI_IMPORT_NAME: "XEP-0176",
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
44 C.PI_TYPE: "XEP",
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
45 C.PI_MODES: C.PLUG_MODE_BOTH,
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
46 C.PI_PROTOCOLS: ["XEP-0176"],
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
47 C.PI_DEPENDENCIES: ["XEP-0166"],
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
48 C.PI_RECOMMENDATIONS: [],
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
49 C.PI_MAIN: "XEP_0176",
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
50 C.PI_HANDLER: "yes",
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
51 C.PI_DESCRIPTION: _("""Implementation of Jingle ICE-UDP transport"""),
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
52 }
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
53
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
54
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
55 class XEP_0176(BaseTransportHandler):
4231
e11b13418ba6 plugin XEP-0353, XEP-0234, jingle: WebRTC data channel signaling implementation:
Goffi <goffi@goffi.org>
parents: 4116
diff changeset
56 namespace = NS_JINGLE_ICE_UDP
e11b13418ba6 plugin XEP-0353, XEP-0234, jingle: WebRTC data channel signaling implementation:
Goffi <goffi@goffi.org>
parents: 4116
diff changeset
57
4045
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
58 def __init__(self, host):
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
59 log.info(f"plugin {PLUGIN_INFO[C.PI_NAME]!r} initialization")
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
60 self.host = host
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
61 self._j = host.plugins["XEP-0166"] # shortcut to access jingle
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
62 self._j.register_transport(
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
63 NS_JINGLE_ICE_UDP, self._j.TRANSPORT_DATAGRAM, self, 100
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
64 )
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
65 host.bridge.add_method(
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
66 "ice_candidates_add",
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
67 ".plugin",
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
68 in_sign="sss",
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
69 out_sign="",
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
70 method=self._ice_candidates_add,
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 async_=True,
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
72 )
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
73 host.bridge.add_signal(
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
74 "ice_candidates_new", ".plugin", signature="sss"
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
75 ) # args: jingle_sid, candidates_serialised, profile
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
76 host.bridge.add_signal(
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
77 "ice_restart", ".plugin", signature="sss"
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
78 ) # args: jingle_sid, side ("local" or "peer"), profile
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
79
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
80 def get_handler(self, client):
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
81 return XEP_0176_handler()
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
82
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
83 def _ice_candidates_add(
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
84 self,
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
85 session_id: str,
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
86 media_ice_data_s: str,
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
87 profile_key: str,
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
88 ):
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
89 client = self.host.get_client(profile_key)
4116
23fa52acf72c plugin XEP-0167, XEP-0176: transport-info and ICE candidate sending are delayed if session is not active yet
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
90 return defer.ensureDeferred(
23fa52acf72c plugin XEP-0167, XEP-0176: transport-info and ICE candidate sending are delayed if session is not active yet
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
91 self.ice_candidates_add(
23fa52acf72c plugin XEP-0167, XEP-0176: transport-info and ICE candidate sending are delayed if session is not active yet
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
92 client,
23fa52acf72c plugin XEP-0167, XEP-0176: transport-info and ICE candidate sending are delayed if session is not active yet
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
93 session_id,
23fa52acf72c plugin XEP-0167, XEP-0176: transport-info and ICE candidate sending are delayed if session is not active yet
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
94 data_format.deserialise(media_ice_data_s),
23fa52acf72c plugin XEP-0167, XEP-0176: transport-info and ICE candidate sending are delayed if session is not active yet
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
95 )
23fa52acf72c plugin XEP-0167, XEP-0176: transport-info and ICE candidate sending are delayed if session is not active yet
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
96 )
4045
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
97
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
98 def build_transport(self, ice_data: dict) -> domish.Element:
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
99 """Generate <transport> element from ICE data
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
100
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
101 @param ice_data: a dict containing the following keys:
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
102 - "ufrag" (str): The ICE username fragment.
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
103 - "pwd" (str): The ICE password.
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
104 - "candidates" (List[dict]): A list of ICE candidate dictionaries, each
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
105 containing:
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
106 - "component_id" (int): The component ID.
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
107 - "foundation" (str): The candidate foundation.
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
108 - "address" (str): The candidate IP address.
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
109 - "port" (int): The candidate port.
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
110 - "priority" (int): The candidate priority.
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
111 - "transport" (str): The candidate transport protocol, e.g., "udp".
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
112 - "type" (str): The candidate type, e.g., "host", "srflx", "prflx", or
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
113 "relay".
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
114 - "generation" (str, optional): The candidate generation. Defaults to "0".
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
115 - "network" (str, optional): The candidate network. Defaults to "0".
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
116 - "rel_addr" (str, optional): The related address for the candidate, if
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
117 any.
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
118 - "rel_port" (int, optional): The related port for the candidate, if any.
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
119
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
120 @return: A <transport> element.
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
121 """
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
122 try:
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
123 ufrag: str = ice_data["ufrag"]
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
124 pwd: str = ice_data["pwd"]
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
125 except KeyError as e:
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
126 raise exceptions.DataError(f"ICE {e} must be provided")
4116
23fa52acf72c plugin XEP-0167, XEP-0176: transport-info and ICE candidate sending are delayed if session is not active yet
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
127 candidates: List[dict] = ice_data.get("candidates", [])
4045
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
128
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
129 candidates.sort(key=lambda c: int(c.get("priority", 0)), reverse=True)
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
130 transport_elt = domish.Element(
4116
23fa52acf72c plugin XEP-0167, XEP-0176: transport-info and ICE candidate sending are delayed if session is not active yet
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
131 (NS_JINGLE_ICE_UDP, "transport"), attribs={"ufrag": ufrag, "pwd": pwd}
4045
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
132 )
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
133
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
134 for candidate in candidates:
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
135 try:
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
136 candidate_elt = transport_elt.addElement("candidate")
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
137 candidate_elt["component"] = str(candidate["component_id"])
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
138 candidate_elt["foundation"] = candidate["foundation"]
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
139 candidate_elt["generation"] = str(candidate.get("generation", "0"))
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
140 candidate_elt["id"] = candidate.get("id") or str(uuid.uuid4())
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
141 candidate_elt["ip"] = candidate["address"]
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
142 candidate_elt["network"] = str(candidate.get("network", "0"))
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
143 candidate_elt["port"] = str(candidate["port"])
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
144 candidate_elt["priority"] = str(candidate["priority"])
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
145 candidate_elt["protocol"] = candidate["transport"]
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
146 candidate_elt["type"] = candidate["type"]
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
147 except KeyError as e:
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
148 raise exceptions.DataError(
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
149 f"Mandatory ICE candidate attribute {e} is missing"
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
150 )
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
151
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
152 if "rel_addr" in candidate and "rel_port" in candidate:
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
153 candidate_elt["rel-addr"] = candidate["rel_addr"]
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
154 candidate_elt["rel-port"] = str(candidate["rel_port"])
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
155
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
156 self.host.trigger.point("XEP-0176_build_transport", transport_elt, ice_data)
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
157
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
158 return transport_elt
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
159
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
160 def parse_transport(self, transport_elt: domish.Element) -> dict:
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
161 """Parse <transport> to a dict
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
162
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
163 @param transport_elt: <transport> element
4054
4c8bf67bfbeb plugin XEP-0176: fix candidates data format when calling Bridge's `ice_candidates_new`
Goffi <goffi@goffi.org>
parents: 4052
diff changeset
164 @return: ICE data (as in [build_transport])
4045
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
165 """
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
166 try:
4116
23fa52acf72c plugin XEP-0167, XEP-0176: transport-info and ICE candidate sending are delayed if session is not active yet
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
167 ice_data = {"ufrag": transport_elt["ufrag"], "pwd": transport_elt["pwd"]}
4045
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
168 except KeyError as e:
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
169 raise exceptions.DataError(
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
170 f"<transport> is missing mandatory attribute {e}: {transport_elt.toXml()}"
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
171 )
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
172 ice_data["candidates"] = ice_candidates = []
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
173
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
174 for candidate_elt in transport_elt.elements(NS_JINGLE_ICE_UDP, "candidate"):
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
175 try:
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
176 candidate = {
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
177 "component_id": int(candidate_elt["component"]),
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
178 "foundation": candidate_elt["foundation"],
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
179 "address": candidate_elt["ip"],
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
180 "port": int(candidate_elt["port"]),
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
181 "priority": int(candidate_elt["priority"]),
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
182 "transport": candidate_elt["protocol"],
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
183 "type": candidate_elt["type"],
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
184 }
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
185 except KeyError as e:
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
186 raise exceptions.DataError(
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
187 f"Mandatory attribute {e} is missing in candidate element"
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
188 )
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
189
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
190 if candidate_elt.hasAttribute("generation"):
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
191 candidate["generation"] = candidate_elt["generation"]
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
192
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
193 if candidate_elt.hasAttribute("network"):
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
194 candidate["network"] = candidate_elt["network"]
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
195
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
196 if candidate_elt.hasAttribute("rel-addr"):
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
197 candidate["rel_addr"] = candidate_elt["rel-addr"]
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
198
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
199 if candidate_elt.hasAttribute("rel-port"):
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
200 candidate["rel_port"] = int(candidate_elt["rel-port"])
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
201
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
202 ice_candidates.append(candidate)
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
203
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
204 self.host.trigger.point("XEP-0176_parse_transport", transport_elt, ice_data)
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
205
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
206 return ice_data
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
207
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
208 async def jingle_session_init(
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
209 self,
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
210 client: SatXMPPEntity,
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
211 session: dict,
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
212 content_name: str,
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
213 ) -> domish.Element:
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
214 """Create a Jingle session initiation transport element with ICE candidates.
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
215
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
216 @param client: SatXMPPEntity object representing the client.
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
217 @param session: Dictionary containing session data.
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
218 @param content_name: Name of the content.
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
219 @param ufrag: ICE username fragment.
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
220 @param pwd: ICE password.
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
221 @param candidates: List of ICE candidate dictionaries parsed from the
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
222 parse_ice_candidate method.
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
223
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
224 @return: domish.Element representing the Jingle transport element.
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
225
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
226 @raise exceptions.DataError: If mandatory data is missing from the candidates.
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
227 """
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
228 content_data = session["contents"][content_name]
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
229 transport_data = content_data["transport_data"]
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
230 ice_data = transport_data["local_ice_data"]
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
231 return self.build_transport(ice_data)
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
232
4275
29fda1340078 plugin XEP-0176: Send ICE candidates buffer for initiator:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
233 async def _send_ice_buffer(
29fda1340078 plugin XEP-0176: Send ICE candidates buffer for initiator:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
234 self,
29fda1340078 plugin XEP-0176: Send ICE candidates buffer for initiator:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
235 client: SatXMPPEntity,
29fda1340078 plugin XEP-0176: Send ICE candidates buffer for initiator:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
236 session: dict,
29fda1340078 plugin XEP-0176: Send ICE candidates buffer for initiator:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
237 content_name: str,
29fda1340078 plugin XEP-0176: Send ICE candidates buffer for initiator:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
238 content_data: dict,
29fda1340078 plugin XEP-0176: Send ICE candidates buffer for initiator:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
239 ) -> None:
29fda1340078 plugin XEP-0176: Send ICE candidates buffer for initiator:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
240 """Send ``transport-info`` actions with buffered ICE candidates.
29fda1340078 plugin XEP-0176: Send ICE candidates buffer for initiator:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
241
29fda1340078 plugin XEP-0176: Send ICE candidates buffer for initiator:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
242 @param client: Libervia client instance.
29fda1340078 plugin XEP-0176: Send ICE candidates buffer for initiator:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
243 @param session: Jingle session.
29fda1340078 plugin XEP-0176: Send ICE candidates buffer for initiator:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
244 @param content_name: Name (ID) of the content.
29fda1340078 plugin XEP-0176: Send ICE candidates buffer for initiator:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
245 @param content_data: Data of the content.
29fda1340078 plugin XEP-0176: Send ICE candidates buffer for initiator:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
246 @param media_type: Type of media of the content.
29fda1340078 plugin XEP-0176: Send ICE candidates buffer for initiator:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
247 """
29fda1340078 plugin XEP-0176: Send ICE candidates buffer for initiator:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
248 media_type = content_data["application_data"].get("media")
29fda1340078 plugin XEP-0176: Send ICE candidates buffer for initiator:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
249 # we check if we have any buffered ICE candidates, and send them if it's the case
29fda1340078 plugin XEP-0176: Send ICE candidates buffer for initiator:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
250 try:
29fda1340078 plugin XEP-0176: Send ICE candidates buffer for initiator:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
251 buffer = session["XEP-0176_ice_buffer"]
29fda1340078 plugin XEP-0176: Send ICE candidates buffer for initiator:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
252 buffered_ice_data = buffer.pop(media_type)
29fda1340078 plugin XEP-0176: Send ICE candidates buffer for initiator:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
253 except KeyError:
29fda1340078 plugin XEP-0176: Send ICE candidates buffer for initiator:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
254 pass
29fda1340078 plugin XEP-0176: Send ICE candidates buffer for initiator:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
255 else:
29fda1340078 plugin XEP-0176: Send ICE candidates buffer for initiator:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
256 if not buffer:
29fda1340078 plugin XEP-0176: Send ICE candidates buffer for initiator:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
257 del session["XEP-0176_ice_buffer"]
4290
4837ec911c43 plugin XEP-0176: add debug logs:
Goffi <goffi@goffi.org>
parents: 4275
diff changeset
258 local_jid = session["local_jid"]
4837ec911c43 plugin XEP-0176: add debug logs:
Goffi <goffi@goffi.org>
parents: 4275
diff changeset
259 peer_jid = session["peer_jid"]
4837ec911c43 plugin XEP-0176: add debug logs:
Goffi <goffi@goffi.org>
parents: 4275
diff changeset
260 log.debug(f"Replaying ICE buffer from {local_jid} to {peer_jid}")
4275
29fda1340078 plugin XEP-0176: Send ICE candidates buffer for initiator:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
261 transport_elt = self.build_transport(buffered_ice_data)
29fda1340078 plugin XEP-0176: Send ICE candidates buffer for initiator:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
262 iq_elt, __ = self._j.build_action(
29fda1340078 plugin XEP-0176: Send ICE candidates buffer for initiator:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
263 client,
29fda1340078 plugin XEP-0176: Send ICE candidates buffer for initiator:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
264 self._j.A_TRANSPORT_INFO,
29fda1340078 plugin XEP-0176: Send ICE candidates buffer for initiator:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
265 session,
29fda1340078 plugin XEP-0176: Send ICE candidates buffer for initiator:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
266 content_name,
29fda1340078 plugin XEP-0176: Send ICE candidates buffer for initiator:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
267 context_elt=transport_elt,
29fda1340078 plugin XEP-0176: Send ICE candidates buffer for initiator:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
268 )
29fda1340078 plugin XEP-0176: Send ICE candidates buffer for initiator:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
269 self.host.trigger.point(
29fda1340078 plugin XEP-0176: Send ICE candidates buffer for initiator:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
270 "XEP-0176_jingle_handler_send_buffer",
29fda1340078 plugin XEP-0176: Send ICE candidates buffer for initiator:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
271 client,
29fda1340078 plugin XEP-0176: Send ICE candidates buffer for initiator:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
272 session,
29fda1340078 plugin XEP-0176: Send ICE candidates buffer for initiator:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
273 content_name,
29fda1340078 plugin XEP-0176: Send ICE candidates buffer for initiator:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
274 content_data,
29fda1340078 plugin XEP-0176: Send ICE candidates buffer for initiator:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
275 transport_elt,
29fda1340078 plugin XEP-0176: Send ICE candidates buffer for initiator:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
276 iq_elt,
29fda1340078 plugin XEP-0176: Send ICE candidates buffer for initiator:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
277 )
29fda1340078 plugin XEP-0176: Send ICE candidates buffer for initiator:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
278 await iq_elt.send()
29fda1340078 plugin XEP-0176: Send ICE candidates buffer for initiator:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
279
4045
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
280 async def jingle_handler(
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
281 self,
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
282 client: SatXMPPEntity,
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
283 action: str,
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
284 session: dict,
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
285 content_name: str,
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
286 transport_elt: domish.Element,
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
287 ) -> domish.Element:
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
288 """Handle Jingle requests
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
289
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
290 @param client: The SatXMPPEntity instance.
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
291 @param action: The action to be performed with the session.
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
292 @param session: A dictionary containing the session information.
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
293 @param content_name: The name of the content.
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
294 @param transport_elt: The domish.Element instance representing the transport
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
295 element.
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
296
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
297 @return: <transport> element
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
298 """
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
299 content_data = session["contents"][content_name]
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
300 transport_data = content_data["transport_data"]
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
301 if action in (self._j.A_PREPARE_CONFIRMATION, self._j.A_PREPARE_INITIATOR):
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
302 peer_ice_data = self.parse_transport(transport_elt)
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
303 transport_data["peer_ice_data"] = peer_ice_data
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
304
4116
23fa52acf72c plugin XEP-0167, XEP-0176: transport-info and ICE candidate sending are delayed if session is not active yet
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
305 elif action == self._j.A_ACCEPTED_ACK:
23fa52acf72c plugin XEP-0167, XEP-0176: transport-info and ICE candidate sending are delayed if session is not active yet
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
306 buffer = session.pop("XEP-0176_handler_buffer", None)
23fa52acf72c plugin XEP-0167, XEP-0176: transport-info and ICE candidate sending are delayed if session is not active yet
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
307 if buffer:
23fa52acf72c plugin XEP-0167, XEP-0176: transport-info and ICE candidate sending are delayed if session is not active yet
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
308 log.debug("replaying buffered events")
23fa52acf72c plugin XEP-0167, XEP-0176: transport-info and ICE candidate sending are delayed if session is not active yet
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
309 for args in buffer:
23fa52acf72c plugin XEP-0167, XEP-0176: transport-info and ICE candidate sending are delayed if session is not active yet
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
310 await self.jingle_handler(*args)
4275
29fda1340078 plugin XEP-0176: Send ICE candidates buffer for initiator:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
311 await self._send_ice_buffer(client, session, content_name, content_data)
4116
23fa52acf72c plugin XEP-0167, XEP-0176: transport-info and ICE candidate sending are delayed if session is not active yet
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
312 elif action == self._j.A_PREPARE_RESPONDER:
4045
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
313 pass
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
314
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
315 elif action == self._j.A_SESSION_ACCEPT:
4275
29fda1340078 plugin XEP-0176: Send ICE candidates buffer for initiator:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
316 await self._send_ice_buffer(client, session, content_name, content_data)
4045
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
317
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
318 elif action == self._j.A_START:
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
319 pass
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
320
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
321 elif action == self._j.A_SESSION_INITIATE:
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
322 # responder side, we give our candidates
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
323 transport_elt = self.build_transport(transport_data["local_ice_data"])
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
324 elif action == self._j.A_TRANSPORT_INFO:
4116
23fa52acf72c plugin XEP-0167, XEP-0176: transport-info and ICE candidate sending are delayed if session is not active yet
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
325 if session["state"] == self._j.STATE_PENDING:
23fa52acf72c plugin XEP-0167, XEP-0176: transport-info and ICE candidate sending are delayed if session is not active yet
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
326 # Session is not yet active; we buffer the arguments to replay them
23fa52acf72c plugin XEP-0167, XEP-0176: transport-info and ICE candidate sending are delayed if session is not active yet
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
327 # when the session becomes active. This makes the frontend's life easier.
23fa52acf72c plugin XEP-0167, XEP-0176: transport-info and ICE candidate sending are delayed if session is not active yet
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
328 log.debug("session is not active yet, buffering transport-info element")
23fa52acf72c plugin XEP-0167, XEP-0176: transport-info and ICE candidate sending are delayed if session is not active yet
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
329 buffer = session.setdefault("XEP-0176_handler_buffer", [])
23fa52acf72c plugin XEP-0167, XEP-0176: transport-info and ICE candidate sending are delayed if session is not active yet
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
330 buffer.append([client, action, session, content_name, transport_elt])
23fa52acf72c plugin XEP-0167, XEP-0176: transport-info and ICE candidate sending are delayed if session is not active yet
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
331 return transport_elt
4054
4c8bf67bfbeb plugin XEP-0176: fix candidates data format when calling Bridge's `ice_candidates_new`
Goffi <goffi@goffi.org>
parents: 4052
diff changeset
332
4c8bf67bfbeb plugin XEP-0176: fix candidates data format when calling Bridge's `ice_candidates_new`
Goffi <goffi@goffi.org>
parents: 4052
diff changeset
333 media_type = content_data["application_data"].get("media")
4045
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
334 new_ice_data = self.parse_transport(transport_elt)
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
335 restart = self.update_candidates(transport_data, new_ice_data, local=False)
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
336 if restart:
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
337 log.debug(
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
338 f"Peer ICE restart detected on session {session['id']} "
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
339 f"[{client.profile}]"
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
340 )
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
341 self.host.bridge.ice_restart(session["id"], "peer", client.profile)
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
342
4275
29fda1340078 plugin XEP-0176: Send ICE candidates buffer for initiator:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
343 ice_candidates_new_cb = session.get("ice_candidates_new_cb")
29fda1340078 plugin XEP-0176: Send ICE candidates buffer for initiator:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
344 ice_candidates_data = {media_type: new_ice_data}
29fda1340078 plugin XEP-0176: Send ICE candidates buffer for initiator:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
345 if ice_candidates_new_cb is None:
29fda1340078 plugin XEP-0176: Send ICE candidates buffer for initiator:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
346 self.host.bridge.ice_candidates_new(
29fda1340078 plugin XEP-0176: Send ICE candidates buffer for initiator:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
347 session["id"],
29fda1340078 plugin XEP-0176: Send ICE candidates buffer for initiator:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
348 data_format.serialise(ice_candidates_data),
29fda1340078 plugin XEP-0176: Send ICE candidates buffer for initiator:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
349 client.profile,
29fda1340078 plugin XEP-0176: Send ICE candidates buffer for initiator:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
350 )
29fda1340078 plugin XEP-0176: Send ICE candidates buffer for initiator:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
351 else:
29fda1340078 plugin XEP-0176: Send ICE candidates buffer for initiator:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
352 ice_candidates_new_cb(client, session, ice_candidates_data)
4045
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
353 elif action == self._j.A_DESTROY:
4116
23fa52acf72c plugin XEP-0167, XEP-0176: transport-info and ICE candidate sending are delayed if session is not active yet
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
354 pass
4045
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
355 else:
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
356 log.warning("FIXME: unmanaged action {}".format(action))
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
357
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
358 return transport_elt
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
359
4052
2ced30f6d5de plugin XEP-0166, 0176, 0234: minor renaming + type hints
Goffi <goffi@goffi.org>
parents: 4045
diff changeset
360 def jingle_terminate(
2ced30f6d5de plugin XEP-0166, 0176, 0234: minor renaming + type hints
Goffi <goffi@goffi.org>
parents: 4045
diff changeset
361 self,
2ced30f6d5de plugin XEP-0166, 0176, 0234: minor renaming + type hints
Goffi <goffi@goffi.org>
parents: 4045
diff changeset
362 client: SatXMPPEntity,
2ced30f6d5de plugin XEP-0166, 0176, 0234: minor renaming + type hints
Goffi <goffi@goffi.org>
parents: 4045
diff changeset
363 action: str,
2ced30f6d5de plugin XEP-0166, 0176, 0234: minor renaming + type hints
Goffi <goffi@goffi.org>
parents: 4045
diff changeset
364 session: dict,
2ced30f6d5de plugin XEP-0166, 0176, 0234: minor renaming + type hints
Goffi <goffi@goffi.org>
parents: 4045
diff changeset
365 content_name: str,
2ced30f6d5de plugin XEP-0166, 0176, 0234: minor renaming + type hints
Goffi <goffi@goffi.org>
parents: 4045
diff changeset
366 reason_elt: domish.Element,
2ced30f6d5de plugin XEP-0166, 0176, 0234: minor renaming + type hints
Goffi <goffi@goffi.org>
parents: 4045
diff changeset
367 ) -> None:
4045
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
368 log.debug("ICE-UDP session terminated")
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
369
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
370 def update_candidates(
4116
23fa52acf72c plugin XEP-0167, XEP-0176: transport-info and ICE candidate sending are delayed if session is not active yet
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
371 self, transport_data: dict, new_ice_data: dict, local: bool
4045
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
372 ) -> bool:
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
373 """Update ICE candidates when new one are received
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
374
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
375 @param transport_data: transport_data of the content linked to the candidates
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
376 @param new_ice_data: new ICE data, in the same format as returned
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
377 by [self.parse_transport]
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
378 @param local: True if it's our candidates, False if it's peer ones
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
379 @return: True if there is a ICE restart
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
380 """
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
381 key = "local_ice_data" if local else "peer_ice_data"
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
382 try:
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
383 ice_data = transport_data[key]
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
384 except KeyError:
4116
23fa52acf72c plugin XEP-0167, XEP-0176: transport-info and ICE candidate sending are delayed if session is not active yet
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
385 log.warning(f"no {key} available")
4045
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
386 transport_data[key] = new_ice_data
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
387 else:
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
388 if (
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
389 new_ice_data["ufrag"] != ice_data["ufrag"]
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
390 or new_ice_data["pwd"] != ice_data["pwd"]
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
391 ):
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
392 ice_data["ufrag"] = new_ice_data["ufrag"]
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
393 ice_data["pwd"] = new_ice_data["pwd"]
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
394 ice_data["candidates"] = new_ice_data["candidates"]
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
395 return True
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
396 return False
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
397
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
398 async def ice_candidates_add(
4275
29fda1340078 plugin XEP-0176: Send ICE candidates buffer for initiator:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
399 self, client: SatXMPPEntity, session_id: str, media_ice_data: dict[str, dict]
4045
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
400 ) -> None:
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
401 """Called when a new ICE candidates are available for a session
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
402
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
403 @param session_id: Session ID
4116
23fa52acf72c plugin XEP-0167, XEP-0176: transport-info and ICE candidate sending are delayed if session is not active yet
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
404 @param media_ice_data: a map from media type (audio, video) to ICE data
4045
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
405 ICE data must be in the same format as in [self.parse_transport]
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
406 """
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
407 session = self._j.get_session(client, session_id)
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
408 iq_elt: Optional[domish.Element] = None
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4231
diff changeset
409 content_name: str | None = None
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4231
diff changeset
410 content_data: dict | None = None
4045
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
411
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
412 for media_type, new_ice_data in media_ice_data.items():
4116
23fa52acf72c plugin XEP-0167, XEP-0176: transport-info and ICE candidate sending are delayed if session is not active yet
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
413 if session["state"] == self._j.STATE_PENDING:
23fa52acf72c plugin XEP-0167, XEP-0176: transport-info and ICE candidate sending are delayed if session is not active yet
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
414 log.debug(f"session not active, buffering")
4275
29fda1340078 plugin XEP-0176: Send ICE candidates buffer for initiator:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
415 buffer = session.setdefault("XEP-0176_ice_buffer", {})
4116
23fa52acf72c plugin XEP-0167, XEP-0176: transport-info and ICE candidate sending are delayed if session is not active yet
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
416 media_buffer = buffer.setdefault(media_type, {})
23fa52acf72c plugin XEP-0167, XEP-0176: transport-info and ICE candidate sending are delayed if session is not active yet
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
417
23fa52acf72c plugin XEP-0167, XEP-0176: transport-info and ICE candidate sending are delayed if session is not active yet
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
418 for key in ["ufrag", "pwd"]:
23fa52acf72c plugin XEP-0167, XEP-0176: transport-info and ICE candidate sending are delayed if session is not active yet
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
419 if key not in media_buffer:
23fa52acf72c plugin XEP-0167, XEP-0176: transport-info and ICE candidate sending are delayed if session is not active yet
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
420 media_buffer[key] = new_ice_data[key]
23fa52acf72c plugin XEP-0167, XEP-0176: transport-info and ICE candidate sending are delayed if session is not active yet
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
421 else:
23fa52acf72c plugin XEP-0167, XEP-0176: transport-info and ICE candidate sending are delayed if session is not active yet
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
422 if media_buffer[key] != new_ice_data[key]:
23fa52acf72c plugin XEP-0167, XEP-0176: transport-info and ICE candidate sending are delayed if session is not active yet
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
423 log.warning(
23fa52acf72c plugin XEP-0167, XEP-0176: transport-info and ICE candidate sending are delayed if session is not active yet
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
424 f"{key} conflict, new value will replace old one\n"
23fa52acf72c plugin XEP-0167, XEP-0176: transport-info and ICE candidate sending are delayed if session is not active yet
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
425 f"buffer={media_buffer[key]!r}\n"
23fa52acf72c plugin XEP-0167, XEP-0176: transport-info and ICE candidate sending are delayed if session is not active yet
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
426 f"new={new_ice_data[key]!r}"
23fa52acf72c plugin XEP-0167, XEP-0176: transport-info and ICE candidate sending are delayed if session is not active yet
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
427 )
23fa52acf72c plugin XEP-0167, XEP-0176: transport-info and ICE candidate sending are delayed if session is not active yet
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
428 media_buffer[key] = new_ice_data[key]
23fa52acf72c plugin XEP-0167, XEP-0176: transport-info and ICE candidate sending are delayed if session is not active yet
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
429
23fa52acf72c plugin XEP-0167, XEP-0176: transport-info and ICE candidate sending are delayed if session is not active yet
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
430 media_buffer.setdefault("candidates", []).extend(
23fa52acf72c plugin XEP-0167, XEP-0176: transport-info and ICE candidate sending are delayed if session is not active yet
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
431 new_ice_data["candidates"]
23fa52acf72c plugin XEP-0167, XEP-0176: transport-info and ICE candidate sending are delayed if session is not active yet
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
432 )
23fa52acf72c plugin XEP-0167, XEP-0176: transport-info and ICE candidate sending are delayed if session is not active yet
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
433 continue
23fa52acf72c plugin XEP-0167, XEP-0176: transport-info and ICE candidate sending are delayed if session is not active yet
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
434
4045
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
435 for content_name, content_data in session["contents"].items():
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
436 if content_data["application_data"].get("media") == media_type:
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
437 break
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
438 else:
4116
23fa52acf72c plugin XEP-0167, XEP-0176: transport-info and ICE candidate sending are delayed if session is not active yet
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
439 log.warning(f"no media of type {media_type} has been found")
4045
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
440 continue
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
441 restart = self.update_candidates(
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
442 content_data["transport_data"], new_ice_data, True
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
443 )
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
444 if restart:
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
445 log.debug(
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
446 f"Local ICE restart detected on session {session['id']} "
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
447 f"[{client.profile}]"
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
448 )
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
449 self.host.bridge.ice_restart(session["id"], "local", client.profile)
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
450 transport_elt = self.build_transport(new_ice_data)
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
451 iq_elt, __ = self._j.build_action(
4116
23fa52acf72c plugin XEP-0167, XEP-0176: transport-info and ICE candidate sending are delayed if session is not active yet
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
452 client,
23fa52acf72c plugin XEP-0167, XEP-0176: transport-info and ICE candidate sending are delayed if session is not active yet
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
453 self._j.A_TRANSPORT_INFO,
23fa52acf72c plugin XEP-0167, XEP-0176: transport-info and ICE candidate sending are delayed if session is not active yet
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
454 session,
23fa52acf72c plugin XEP-0167, XEP-0176: transport-info and ICE candidate sending are delayed if session is not active yet
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
455 content_name,
23fa52acf72c plugin XEP-0167, XEP-0176: transport-info and ICE candidate sending are delayed if session is not active yet
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
456 iq_elt=iq_elt,
23fa52acf72c plugin XEP-0167, XEP-0176: transport-info and ICE candidate sending are delayed if session is not active yet
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
457 context_elt=transport_elt,
4045
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
458 )
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
459
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
460 if iq_elt is not None:
4231
e11b13418ba6 plugin XEP-0353, XEP-0234, jingle: WebRTC data channel signaling implementation:
Goffi <goffi@goffi.org>
parents: 4116
diff changeset
461 assert content_name is not None and content_data is not None
4045
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
462 try:
4231
e11b13418ba6 plugin XEP-0353, XEP-0234, jingle: WebRTC data channel signaling implementation:
Goffi <goffi@goffi.org>
parents: 4116
diff changeset
463 self.host.trigger.point(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4231
diff changeset
464 "XEP-0176_ice_candidate_send",
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4231
diff changeset
465 client,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4231
diff changeset
466 session,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4231
diff changeset
467 media_ice_data,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4231
diff changeset
468 content_name,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4231
diff changeset
469 content_data,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4231
diff changeset
470 iq_elt,
4231
e11b13418ba6 plugin XEP-0353, XEP-0234, jingle: WebRTC data channel signaling implementation:
Goffi <goffi@goffi.org>
parents: 4116
diff changeset
471 )
4045
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
472 await iq_elt.send()
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
473 except Exception as e:
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
474 log.warning(f"Could not send new ICE candidates: {e}")
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
475
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
476
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
477 @implementer(iwokkel.IDisco)
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
478 class XEP_0176_handler(XMPPHandler):
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
479 def getDiscoInfo(self, requestor, target, nodeIdentifier=""):
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
480 return [disco.DiscoFeature(NS_JINGLE_ICE_UDP)]
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
481
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
482 def getDiscoItems(self, requestor, target, nodeIdentifier=""):
ae756bf7c3e8 plugin XEP-0176: Jingle ICE-UDP Transport Method implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
483 return []