1package eu.siacs.conversations.xmpp.jingle;
2
3import android.util.Base64;
4import android.util.Log;
5
6import com.google.common.base.Function;
7import com.google.common.base.Objects;
8import com.google.common.base.Preconditions;
9import com.google.common.collect.Collections2;
10import com.google.common.collect.ImmutableSet;
11
12import org.checkerframework.checker.nullness.compatqual.NullableDecl;
13
14import java.lang.ref.WeakReference;
15import java.security.SecureRandom;
16import java.util.Collection;
17import java.util.Collections;
18import java.util.HashMap;
19import java.util.List;
20import java.util.Map;
21import java.util.Set;
22import java.util.concurrent.ConcurrentHashMap;
23
24import eu.siacs.conversations.Config;
25import eu.siacs.conversations.entities.Account;
26import eu.siacs.conversations.entities.Conversation;
27import eu.siacs.conversations.entities.Conversational;
28import eu.siacs.conversations.entities.Message;
29import eu.siacs.conversations.entities.Transferable;
30import eu.siacs.conversations.services.AbstractConnectionManager;
31import eu.siacs.conversations.services.XmppConnectionService;
32import eu.siacs.conversations.xml.Element;
33import eu.siacs.conversations.xml.Namespace;
34import eu.siacs.conversations.xmpp.OnIqPacketReceived;
35import eu.siacs.conversations.xmpp.jingle.stanzas.Content;
36import eu.siacs.conversations.xmpp.jingle.stanzas.FileTransferDescription;
37import eu.siacs.conversations.xmpp.jingle.stanzas.GenericDescription;
38import eu.siacs.conversations.xmpp.jingle.stanzas.JinglePacket;
39import eu.siacs.conversations.xmpp.jingle.stanzas.Propose;
40import eu.siacs.conversations.xmpp.jingle.stanzas.Reason;
41import eu.siacs.conversations.xmpp.jingle.stanzas.RtpDescription;
42import eu.siacs.conversations.xmpp.stanzas.IqPacket;
43import eu.siacs.conversations.xmpp.stanzas.MessagePacket;
44import rocks.xmpp.addr.Jid;
45
46public class JingleConnectionManager extends AbstractConnectionManager {
47 private final HashMap<RtpSessionProposal, DeviceDiscoveryState> rtpSessionProposals = new HashMap<>();
48 private final Map<AbstractJingleConnection.Id, AbstractJingleConnection> connections = new ConcurrentHashMap<>();
49
50 private HashMap<Jid, JingleCandidate> primaryCandidates = new HashMap<>();
51
52 public JingleConnectionManager(XmppConnectionService service) {
53 super(service);
54 }
55
56 static String nextRandomId() {
57 final byte[] id = new byte[16];
58 new SecureRandom().nextBytes(id);
59 return Base64.encodeToString(id, Base64.NO_WRAP | Base64.NO_PADDING);
60 }
61
62 public void deliverPacket(final Account account, final JinglePacket packet) {
63 final String sessionId = packet.getSessionId();
64 if (sessionId == null) {
65 respondWithJingleError(account, packet, "unknown-session", "item-not-found", "cancel");
66 return;
67 }
68 final AbstractJingleConnection.Id id = AbstractJingleConnection.Id.of(account, packet);
69 final AbstractJingleConnection existingJingleConnection = connections.get(id);
70 if (existingJingleConnection != null) {
71 existingJingleConnection.deliverPacket(packet);
72 } else if (packet.getAction() == JinglePacket.Action.SESSION_INITIATE) {
73 final Jid from = packet.getFrom();
74 final Content content = packet.getJingleContent();
75 final String descriptionNamespace = content == null ? null : content.getDescriptionNamespace();
76 final AbstractJingleConnection connection;
77 if (FileTransferDescription.NAMESPACES.contains(descriptionNamespace)) {
78 connection = new JingleFileTransferConnection(this, id, from);
79 } else if (Namespace.JINGLE_APPS_RTP.equals(descriptionNamespace) && !usesTor(account)) {
80 if (isBusy()) {
81 mXmppConnectionService.sendIqPacket(account, packet.generateResponse(IqPacket.TYPE.RESULT), null);
82 final JinglePacket sessionTermination = new JinglePacket(JinglePacket.Action.SESSION_TERMINATE, id.sessionId);
83 sessionTermination.setTo(id.with);
84 sessionTermination.setReason(Reason.BUSY, null);
85 mXmppConnectionService.sendIqPacket(account, sessionTermination, null);
86 return;
87 }
88 connection = new JingleRtpConnection(this, id, from);
89 } else {
90 respondWithJingleError(account, packet, "unsupported-info", "feature-not-implemented", "cancel");
91 return;
92 }
93 connections.put(id, connection);
94 connection.deliverPacket(packet);
95 } else {
96 Log.d(Config.LOGTAG, "unable to route jingle packet: " + packet);
97 respondWithJingleError(account, packet, "unknown-session", "item-not-found", "cancel");
98 }
99 }
100
101 private boolean usesTor(final Account account) {
102 return account.isOnion() || mXmppConnectionService.useTorToConnect();
103 }
104
105 private boolean isBusy() {
106 for (AbstractJingleConnection connection : this.connections.values()) {
107 if (connection instanceof JingleRtpConnection) {
108 return true;
109 }
110 }
111 synchronized (this.rtpSessionProposals) {
112 return this.rtpSessionProposals.containsValue(DeviceDiscoveryState.DISCOVERED) || this.rtpSessionProposals.containsValue(DeviceDiscoveryState.SEARCHING);
113 }
114 }
115
116 public void respondWithJingleError(final Account account, final IqPacket original, String jingleCondition, String condition, String conditionType) {
117 final IqPacket response = original.generateResponse(IqPacket.TYPE.ERROR);
118 final Element error = response.addChild("error");
119 error.setAttribute("type", conditionType);
120 error.addChild(condition, "urn:ietf:params:xml:ns:xmpp-stanzas");
121 error.addChild(jingleCondition, "urn:xmpp:jingle:errors:1");
122 account.getXmppConnection().sendIqPacket(response, null);
123 }
124
125 public void deliverMessage(final Account account, final Jid to, final Jid from, final Element message, String serverMsgId, long timestamp) {
126 Preconditions.checkArgument(Namespace.JINGLE_MESSAGE.equals(message.getNamespace()));
127 final String sessionId = message.getAttribute("id");
128 if (sessionId == null) {
129 return;
130 }
131 if ("accept".equals(message.getName())) {
132 for (AbstractJingleConnection connection : connections.values()) {
133 if (connection instanceof JingleRtpConnection) {
134 final JingleRtpConnection rtpConnection = (JingleRtpConnection) connection;
135 final AbstractJingleConnection.Id id = connection.getId();
136 if (id.account == account && id.sessionId.equals(sessionId)) {
137 rtpConnection.deliveryMessage(from, message, serverMsgId, timestamp);
138 return;
139 }
140 }
141 }
142 return;
143 }
144 final boolean fromSelf = from.asBareJid().equals(account.getJid().asBareJid());
145 final AbstractJingleConnection.Id id;
146 if (fromSelf) {
147 if (to.isFullJid()) {
148 id = AbstractJingleConnection.Id.of(account, to, sessionId);
149 } else {
150 return;
151 }
152 } else {
153 id = AbstractJingleConnection.Id.of(account, from, sessionId);
154 }
155 final AbstractJingleConnection existingJingleConnection = connections.get(id);
156 if (existingJingleConnection != null) {
157 if (existingJingleConnection instanceof JingleRtpConnection) {
158 ((JingleRtpConnection) existingJingleConnection).deliveryMessage(from, message, serverMsgId, timestamp);
159 } else {
160 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": " + existingJingleConnection.getClass().getName() + " does not support jingle messages");
161 }
162 return;
163 }
164
165 if (fromSelf) {
166 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": ignore jingle message from self");
167 //TODO proceed from self should maybe dedup/change the busy that we set earlier
168 return;
169 }
170
171 if ("propose".equals(message.getName())) {
172 final Propose propose = Propose.upgrade(message);
173 final List<GenericDescription> descriptions = propose.getDescriptions();
174 final Collection<RtpDescription> rtpDescriptions = Collections2.transform(
175 Collections2.filter(descriptions, d -> d instanceof RtpDescription),
176 input -> (RtpDescription) input
177 );
178 if (rtpDescriptions.size() > 0 && rtpDescriptions.size() == descriptions.size() && !usesTor(account)) {
179 final Collection<Media> media = Collections2.transform(rtpDescriptions, RtpDescription::getMedia);
180 if (media.contains(Media.UNKNOWN)) {
181 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": encountered unknown media in session proposal. " + propose);
182 return;
183 }
184 if (isBusy()) { //TODO only if no other devices are active
185 //TODO create busy
186 final MessagePacket reject = mXmppConnectionService.getMessageGenerator().sessionReject(from, sessionId);
187 mXmppConnectionService.sendMessagePacket(account, reject);
188 } else {
189 final JingleRtpConnection rtpConnection = new JingleRtpConnection(this, id, from);
190 this.connections.put(id, rtpConnection);
191 rtpConnection.setProposedMedia(ImmutableSet.copyOf(media));
192 rtpConnection.deliveryMessage(from, message, serverMsgId, timestamp);
193 }
194 } else {
195 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": unable to react to proposed session with " + rtpDescriptions.size() + " rtp descriptions of " + descriptions.size() + " total descriptions");
196 }
197 } else if ("proceed".equals(message.getName())) {
198 synchronized (rtpSessionProposals) {
199 final RtpSessionProposal proposal = getRtpSessionProposal(account, from.asBareJid(), sessionId);
200 if (proposal != null) {
201 rtpSessionProposals.remove(proposal);
202 final JingleRtpConnection rtpConnection = new JingleRtpConnection(this, id, account.getJid());
203 rtpConnection.setProposedMedia(proposal.media);
204 this.connections.put(id, rtpConnection);
205 rtpConnection.transitionOrThrow(AbstractJingleConnection.State.PROPOSED);
206 rtpConnection.deliveryMessage(from, message, serverMsgId, timestamp);
207 } else {
208 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": no rtp session proposal found for " + from + " to deliver proceed");
209 }
210 }
211 } else if ("reject".equals(message.getName())) {
212 final RtpSessionProposal proposal = new RtpSessionProposal(account, from.asBareJid(), sessionId);
213 synchronized (rtpSessionProposals) {
214 if (rtpSessionProposals.remove(proposal) != null) {
215 writeLogMissedOutgoing(account, proposal.with, proposal.sessionId, serverMsgId, timestamp);
216 mXmppConnectionService.notifyJingleRtpConnectionUpdate(account, proposal.with, proposal.sessionId, RtpEndUserState.DECLINED_OR_BUSY);
217 } else {
218 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": no rtp session proposal found for " + from + " to deliver reject");
219 }
220 }
221 } else {
222 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": retrieved out of order jingle message");
223 }
224
225 }
226
227 private RtpSessionProposal getRtpSessionProposal(final Account account, Jid from, String sessionId) {
228 for (RtpSessionProposal rtpSessionProposal : rtpSessionProposals.keySet()) {
229 if (rtpSessionProposal.sessionId.equals(sessionId) && rtpSessionProposal.with.equals(from) && rtpSessionProposal.account.getJid().equals(account.getJid())) {
230 return rtpSessionProposal;
231 }
232 }
233 return null;
234 }
235
236 private void writeLogMissedOutgoing(final Account account, Jid with, final String sessionId, String serverMsgId, long timestamp) {
237 final Conversation conversation = mXmppConnectionService.findOrCreateConversation(
238 account,
239 with.asBareJid(),
240 false,
241 false
242 );
243 final Message message = new Message(
244 conversation,
245 Message.STATUS_SEND,
246 Message.TYPE_RTP_SESSION,
247 sessionId
248 );
249 message.setServerMsgId(serverMsgId);
250 message.setTime(timestamp);
251 writeMessage(message);
252 }
253
254 private void writeMessage(final Message message) {
255 final Conversational conversational = message.getConversation();
256 if (conversational instanceof Conversation) {
257 ((Conversation) conversational).add(message);
258 mXmppConnectionService.databaseBackend.createMessage(message);
259 mXmppConnectionService.updateConversationUi();
260 } else {
261 throw new IllegalStateException("Somehow the conversation in a message was a stub");
262 }
263 }
264
265 public void startJingleFileTransfer(final Message message) {
266 Preconditions.checkArgument(message.isFileOrImage(), "Message is not of type file or image");
267 final Transferable old = message.getTransferable();
268 if (old != null) {
269 old.cancel();
270 }
271 final Account account = message.getConversation().getAccount();
272 final AbstractJingleConnection.Id id = AbstractJingleConnection.Id.of(message);
273 final JingleFileTransferConnection connection = new JingleFileTransferConnection(this, id, account.getJid());
274 mXmppConnectionService.markMessage(message, Message.STATUS_WAITING);
275 this.connections.put(id, connection);
276 connection.init(message);
277 }
278
279 void finishConnection(final AbstractJingleConnection connection) {
280 this.connections.remove(connection.getId());
281 }
282
283 void getPrimaryCandidate(final Account account, final boolean initiator, final OnPrimaryCandidateFound listener) {
284 if (Config.DISABLE_PROXY_LOOKUP) {
285 listener.onPrimaryCandidateFound(false, null);
286 return;
287 }
288 if (!this.primaryCandidates.containsKey(account.getJid().asBareJid())) {
289 final Jid proxy = account.getXmppConnection().findDiscoItemByFeature(Namespace.BYTE_STREAMS);
290 if (proxy != null) {
291 IqPacket iq = new IqPacket(IqPacket.TYPE.GET);
292 iq.setTo(proxy);
293 iq.query(Namespace.BYTE_STREAMS);
294 account.getXmppConnection().sendIqPacket(iq, new OnIqPacketReceived() {
295
296 @Override
297 public void onIqPacketReceived(Account account, IqPacket packet) {
298 final Element streamhost = packet.query().findChild("streamhost", Namespace.BYTE_STREAMS);
299 final String host = streamhost == null ? null : streamhost.getAttribute("host");
300 final String port = streamhost == null ? null : streamhost.getAttribute("port");
301 if (host != null && port != null) {
302 try {
303 JingleCandidate candidate = new JingleCandidate(nextRandomId(), true);
304 candidate.setHost(host);
305 candidate.setPort(Integer.parseInt(port));
306 candidate.setType(JingleCandidate.TYPE_PROXY);
307 candidate.setJid(proxy);
308 candidate.setPriority(655360 + (initiator ? 30 : 0));
309 primaryCandidates.put(account.getJid().asBareJid(), candidate);
310 listener.onPrimaryCandidateFound(true, candidate);
311 } catch (final NumberFormatException e) {
312 listener.onPrimaryCandidateFound(false, null);
313 }
314 } else {
315 listener.onPrimaryCandidateFound(false, null);
316 }
317 }
318 });
319 } else {
320 listener.onPrimaryCandidateFound(false, null);
321 }
322
323 } else {
324 listener.onPrimaryCandidateFound(true,
325 this.primaryCandidates.get(account.getJid().asBareJid()));
326 }
327 }
328
329 public void retractSessionProposal(final Account account, final Jid with) {
330 synchronized (this.rtpSessionProposals) {
331 RtpSessionProposal matchingProposal = null;
332 for (RtpSessionProposal proposal : this.rtpSessionProposals.keySet()) {
333 if (proposal.account == account && with.asBareJid().equals(proposal.with)) {
334 matchingProposal = proposal;
335 break;
336 }
337 }
338 if (matchingProposal != null) {
339 this.rtpSessionProposals.remove(matchingProposal);
340 final MessagePacket messagePacket = mXmppConnectionService.getMessageGenerator().sessionRetract(matchingProposal);
341 writeLogMissedOutgoing(account, matchingProposal.with, matchingProposal.sessionId, null, System.currentTimeMillis());
342 mXmppConnectionService.sendMessagePacket(account, messagePacket);
343
344 }
345 }
346 }
347
348 public void proposeJingleRtpSession(final Account account, final Jid with, final Set<Media> media) {
349 synchronized (this.rtpSessionProposals) {
350 for (Map.Entry<RtpSessionProposal, DeviceDiscoveryState> entry : this.rtpSessionProposals.entrySet()) {
351 RtpSessionProposal proposal = entry.getKey();
352 if (proposal.account == account && with.asBareJid().equals(proposal.with)) {
353 final DeviceDiscoveryState preexistingState = entry.getValue();
354 if (preexistingState != null && preexistingState != DeviceDiscoveryState.FAILED) {
355 mXmppConnectionService.notifyJingleRtpConnectionUpdate(
356 account,
357 with,
358 proposal.sessionId,
359 preexistingState.toEndUserState()
360 );
361 return;
362 }
363 }
364 }
365 final RtpSessionProposal proposal = RtpSessionProposal.of(account, with.asBareJid(), media);
366 this.rtpSessionProposals.put(proposal, DeviceDiscoveryState.SEARCHING);
367 mXmppConnectionService.notifyJingleRtpConnectionUpdate(
368 account,
369 proposal.with,
370 proposal.sessionId,
371 RtpEndUserState.FINDING_DEVICE
372 );
373 final MessagePacket messagePacket = mXmppConnectionService.getMessageGenerator().sessionProposal(proposal);
374 Log.d(Config.LOGTAG, messagePacket.toString());
375 mXmppConnectionService.sendMessagePacket(account, messagePacket);
376 }
377 }
378
379 public void deliverIbbPacket(Account account, IqPacket packet) {
380 final String sid;
381 final Element payload;
382 if (packet.hasChild("open", Namespace.IBB)) {
383 payload = packet.findChild("open", Namespace.IBB);
384 sid = payload.getAttribute("sid");
385 } else if (packet.hasChild("data", Namespace.IBB)) {
386 payload = packet.findChild("data", Namespace.IBB);
387 sid = payload.getAttribute("sid");
388 } else if (packet.hasChild("close", Namespace.IBB)) {
389 payload = packet.findChild("close", Namespace.IBB);
390 sid = payload.getAttribute("sid");
391 } else {
392 payload = null;
393 sid = null;
394 }
395 if (sid != null) {
396 for (final AbstractJingleConnection connection : this.connections.values()) {
397 if (connection instanceof JingleFileTransferConnection) {
398 final JingleFileTransferConnection fileTransfer = (JingleFileTransferConnection) connection;
399 final JingleTransport transport = fileTransfer.getTransport();
400 if (transport instanceof JingleInBandTransport) {
401 final JingleInBandTransport inBandTransport = (JingleInBandTransport) transport;
402 if (inBandTransport.matches(account, sid)) {
403 inBandTransport.deliverPayload(packet, payload);
404 }
405 return;
406 }
407 }
408 }
409 }
410 Log.d(Config.LOGTAG, "unable to deliver ibb packet: " + packet.toString());
411 account.getXmppConnection().sendIqPacket(packet.generateResponse(IqPacket.TYPE.ERROR), null);
412 }
413
414 public void notifyRebound() {
415 for (final AbstractJingleConnection connection : this.connections.values()) {
416 connection.notifyRebound();
417 }
418 }
419
420 public WeakReference<JingleRtpConnection> findJingleRtpConnection(Account account, Jid with, String sessionId) {
421 final AbstractJingleConnection.Id id = AbstractJingleConnection.Id.of(account, Jid.ofEscaped(with), sessionId);
422 final AbstractJingleConnection connection = connections.get(id);
423 if (connection instanceof JingleRtpConnection) {
424 return new WeakReference<>((JingleRtpConnection) connection);
425 }
426 return null;
427 }
428
429 public void updateProposedSessionDiscovered(Account account, Jid from, String sessionId, final DeviceDiscoveryState target) {
430 synchronized (this.rtpSessionProposals) {
431 final RtpSessionProposal sessionProposal = getRtpSessionProposal(account, from.asBareJid(), sessionId);
432 final DeviceDiscoveryState currentState = sessionProposal == null ? null : rtpSessionProposals.get(sessionProposal);
433 if (currentState == null) {
434 Log.d(Config.LOGTAG, "unable to find session proposal for session id " + sessionId);
435 return;
436 }
437 if (currentState == DeviceDiscoveryState.DISCOVERED) {
438 Log.d(Config.LOGTAG, "session proposal already at discovered. not going to fall back");
439 return;
440 }
441 this.rtpSessionProposals.put(sessionProposal, target);
442 mXmppConnectionService.notifyJingleRtpConnectionUpdate(account, sessionProposal.with, sessionProposal.sessionId, target.toEndUserState());
443 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": flagging session " + sessionId + " as " + target);
444 }
445 }
446
447 public void rejectRtpSession(final String sessionId) {
448 for (final AbstractJingleConnection connection : this.connections.values()) {
449 if (connection.getId().sessionId.equals(sessionId)) {
450 if (connection instanceof JingleRtpConnection) {
451 ((JingleRtpConnection) connection).rejectCall();
452 }
453 }
454 }
455 }
456
457 public void endRtpSession(final String sessionId) {
458 for (final AbstractJingleConnection connection : this.connections.values()) {
459 if (connection.getId().sessionId.equals(sessionId)) {
460 if (connection instanceof JingleRtpConnection) {
461 ((JingleRtpConnection) connection).endCall();
462 }
463 }
464 }
465 }
466
467 public void failProceed(Account account, final Jid with, String sessionId) {
468 final AbstractJingleConnection.Id id = AbstractJingleConnection.Id.of(account, with, sessionId);
469 final AbstractJingleConnection existingJingleConnection = connections.get(id);
470 if (existingJingleConnection instanceof JingleRtpConnection) {
471 ((JingleRtpConnection) existingJingleConnection).deliverFailedProceed();
472 }
473 }
474
475 public enum DeviceDiscoveryState {
476 SEARCHING, DISCOVERED, FAILED;
477
478 public RtpEndUserState toEndUserState() {
479 switch (this) {
480 case SEARCHING:
481 return RtpEndUserState.FINDING_DEVICE;
482 case DISCOVERED:
483 return RtpEndUserState.RINGING;
484 default:
485 return RtpEndUserState.CONNECTIVITY_ERROR;
486 }
487 }
488 }
489
490 public static class RtpSessionProposal {
491 public final Jid with;
492 public final String sessionId;
493 private final Account account;
494 public final Set<Media> media;
495
496 private RtpSessionProposal(Account account, Jid with, String sessionId) {
497 this(account, with, sessionId, Collections.emptySet());
498 }
499
500 private RtpSessionProposal(Account account, Jid with, String sessionId, Set<Media> media) {
501 this.account = account;
502 this.with = with;
503 this.sessionId = sessionId;
504 this.media = media;
505 }
506
507 public static RtpSessionProposal of(Account account, Jid with, Set<Media> media) {
508 return new RtpSessionProposal(account, with, nextRandomId(), media);
509 }
510
511 @Override
512 public boolean equals(Object o) {
513 if (this == o) return true;
514 if (o == null || getClass() != o.getClass()) return false;
515 RtpSessionProposal proposal = (RtpSessionProposal) o;
516 return Objects.equal(account.getJid(), proposal.account.getJid()) &&
517 Objects.equal(with, proposal.with) &&
518 Objects.equal(sessionId, proposal.sessionId);
519 }
520
521 @Override
522 public int hashCode() {
523 return Objects.hashCode(account.getJid(), with, sessionId);
524 }
525 }
526}