Debian Codegraph @

xmpp-vala/src/module/xep/0176_jingle_ice_udp/transport_parameters.vala

raw
1 using Gee;
2 using Xmpp.Xep;
3 using Xmpp;
4
5 public abstract class Xmpp.Xep.JingleIceUdp.IceUdpTransportParameters : Jingle.TransportParameters, Object {
6 public string ns_uri { get { return NS_URI; } }
7 public string remote_pwd { get; private set; }
8 public string remote_ufrag { get; private set; }
9 public string local_pwd { get; private set; }
10 public string local_ufrag { get; private set; }
11
12 public ConcurrentList<Candidate> local_candidates = new ConcurrentList<Candidate>(Candidate.equals_func);
13 public ConcurrentList<Candidate> unsent_local_candidates = new ConcurrentList<Candidate>(Candidate.equals_func);
14 public Gee.List<Candidate> remote_candidates = new ArrayList<Candidate>(Candidate.equals_func);
15
16 public uint8[]? own_fingerprint = null;
17 public string? own_setup = null;
18 public uint8[]? peer_fingerprint = null;
19 public string? peer_fp_algo = null;
20 public string? peer_setup = null;
21
22 public Jid local_full_jid { get; private set; }
23 public Jid peer_full_jid { get; private set; }
24 private uint8 components_;
25 public uint8 components { get { return components_; } }
26
27 public bool incoming { get; private set; default = false; }
28 private bool connection_created = false;
29
30 protected weak Jingle.Content? content = null;
31 protected bool use_raw = false;
32
33 protected IceUdpTransportParameters(uint8 components, Jid local_full_jid, Jid peer_full_jid, StanzaNode? node = null) {
34 this.components_ = components;
35 this.local_full_jid = local_full_jid;
36 this.peer_full_jid = peer_full_jid;
37 if (node != null) {
38 incoming = true;
39 remote_pwd = node.get_attribute("pwd");
40 remote_ufrag = node.get_attribute("ufrag");
41 foreach (StanzaNode candidateNode in node.get_subnodes("candidate")) {
42 remote_candidates.add(Candidate.parse(candidateNode));
43 }
44
45 StanzaNode? fingerprint_node = node.get_subnode("fingerprint", DTLS_NS_URI);
46 if (fingerprint_node != null) {
47 peer_fingerprint = fingerprint_to_bytes(fingerprint_node.get_string_content());
48 peer_fp_algo = fingerprint_node.get_attribute("hash");
49 peer_setup = fingerprint_node.get_attribute("setup");
50 }
51 }
52 }
53
54 public void init(string ufrag, string pwd) {
55 this.local_ufrag = ufrag;
56 this.local_pwd = pwd;
57 debug("Initialized for %s", pwd);
58 }
59
60 public void set_content(Jingle.Content content) {
61 this.content = content;
62 this.content.weak_ref(unset_content);
63 }
64
65 public void unset_content() {
66 this.content = null;
67 }
68
69 public StanzaNode to_transport_stanza_node(string action_type) {
70 var node = new StanzaNode.build("transport", NS_URI)
71 .add_self_xmlns()
72 .put_attribute("ufrag", local_ufrag)
73 .put_attribute("pwd", local_pwd);
74
75 if (own_fingerprint != null && action_type != "transport-info") {
76 var fingerprint_node = new StanzaNode.build("fingerprint", DTLS_NS_URI)
77 .add_self_xmlns()
78 .put_attribute("hash", "sha-256")
79 .put_node(new StanzaNode.text(format_fingerprint(own_fingerprint)));
80 fingerprint_node.put_attribute("setup", own_setup);
81 node.put_node(fingerprint_node);
82 }
83
84 if (action_type != "transport-info") {
85 foreach (Candidate candidate in unsent_local_candidates) {
86 node.put_node(candidate.to_xml());
87 }
88 unsent_local_candidates.clear();
89 } else if (!unsent_local_candidates.is_empty) {
90 Candidate candidate = unsent_local_candidates.first();
91 node.put_node(candidate.to_xml());
92 unsent_local_candidates.remove(candidate);
93 }
94 return node;
95 }
96
97 public virtual void handle_transport_accept(StanzaNode node) throws Jingle.IqError {
98 string? pwd = node.get_attribute("pwd");
99 string? ufrag = node.get_attribute("ufrag");
100 if (pwd != null) remote_pwd = pwd;
101 if (ufrag != null) remote_ufrag = ufrag;
102 foreach (StanzaNode candidateNode in node.get_subnodes("candidate")) {
103 remote_candidates.add(Candidate.parse(candidateNode));
104 }
105
106 StanzaNode? fingerprint_node = node.get_subnode("fingerprint", DTLS_NS_URI);
107 if (fingerprint_node != null) {
108 peer_fingerprint = fingerprint_to_bytes(fingerprint_node.get_string_content());
109 peer_fp_algo = fingerprint_node.get_attribute("hash");
110 peer_setup = fingerprint_node.get_attribute("setup");
111 }
112 }
113
114 public virtual void handle_transport_info(StanzaNode node) throws Jingle.IqError {
115 string? pwd = node.get_attribute("pwd");
116 string? ufrag = node.get_attribute("ufrag");
117 if (pwd != null) remote_pwd = pwd;
118 if (ufrag != null) remote_ufrag = ufrag;
119 foreach (StanzaNode candidateNode in node.get_subnodes("candidate")) {
120 remote_candidates.add(Candidate.parse(candidateNode));
121 }
122
123 StanzaNode? fingerprint_node = node.get_subnode("fingerprint", DTLS_NS_URI);
124 if (fingerprint_node != null) {
125 peer_fingerprint = fingerprint_to_bytes(fingerprint_node.get_string_content());
126 peer_fp_algo = fingerprint_node.get_attribute("hash");
127 peer_setup = fingerprint_node.get_attribute("setup");
128 }
129 }
130
131 public virtual void create_transport_connection(XmppStream stream, Jingle.Content content) {
132 connection_created = true;
133
134 check_send_transport_info();
135 }
136
137 public void add_local_candidate_threadsafe(Candidate candidate) {
138 if (local_candidates.contains(candidate)) return;
139
140 debug("New local candidate %u %s %s:%u", candidate.component, candidate.type_.to_string(), candidate.ip, candidate.port);
141 unsent_local_candidates.add(candidate);
142 local_candidates.add(candidate);
143
144 if (this.content != null && (this.connection_created || !this.incoming)) {
145 Idle.add( () => {
146 check_send_transport_info();
147 return Source.REMOVE;
148 });
149 }
150 }
151
152 private void check_send_transport_info() {
153 if (this.content != null && !unsent_local_candidates.is_empty) {
154 content.send_transport_info(to_transport_stanza_node("transport-info"));
155 }
156 }
157
158 private string format_fingerprint(uint8[] fingerprint) {
159 var sb = new StringBuilder();
160 for (int i = 0; i < fingerprint.length; i++) {
161 sb.append("%02X".printf(fingerprint[i]));
162 if (i < fingerprint.length - 1) {
163 sb.append(":");
164 }
165 }
166 return sb.str;
167 }
168
169 private uint8[]? fingerprint_to_bytes(string? fingerprint_) {
170 if (fingerprint_ == null) return null;
171
172 string fingerprint = fingerprint_.replace(":", "").up();
173
174 uint8[] bin = new uint8[fingerprint.length / 2];
175 const string HEX = "0123456789ABCDEF";
176 for (int i = 0; i < fingerprint.length / 2; i++) {
177 bin[i] = (uint8) (HEX.index_of_char(fingerprint[i*2]) << 4) | HEX.index_of_char(fingerprint[i*2+1]);
178 }
179 return bin;
180 }
181 }