1package eu.siacs.conversations.xmpp.jingle;
2
3import android.util.Base64;
4import android.util.Log;
5
6import java.io.FileInputStream;
7import java.io.FileNotFoundException;
8import java.io.IOException;
9import java.io.InputStream;
10import java.io.OutputStream;
11import java.util.ArrayList;
12import java.util.Arrays;
13import java.util.Collections;
14import java.util.Iterator;
15import java.util.List;
16import java.util.Map.Entry;
17import java.util.concurrent.ConcurrentHashMap;
18
19import eu.siacs.conversations.Config;
20import eu.siacs.conversations.crypto.axolotl.AxolotlService;
21import eu.siacs.conversations.crypto.axolotl.XmppAxolotlMessage;
22import eu.siacs.conversations.entities.Account;
23import eu.siacs.conversations.entities.Conversation;
24import eu.siacs.conversations.entities.DownloadableFile;
25import eu.siacs.conversations.entities.Message;
26import eu.siacs.conversations.entities.Presence;
27import eu.siacs.conversations.entities.ServiceDiscoveryResult;
28import eu.siacs.conversations.entities.Transferable;
29import eu.siacs.conversations.entities.TransferablePlaceholder;
30import eu.siacs.conversations.parser.IqParser;
31import eu.siacs.conversations.persistance.FileBackend;
32import eu.siacs.conversations.services.AbstractConnectionManager;
33import eu.siacs.conversations.services.XmppConnectionService;
34import eu.siacs.conversations.utils.CryptoHelper;
35import eu.siacs.conversations.xml.Element;
36import eu.siacs.conversations.xml.Namespace;
37import eu.siacs.conversations.xmpp.OnIqPacketReceived;
38import eu.siacs.conversations.xmpp.jingle.stanzas.Content;
39import eu.siacs.conversations.xmpp.jingle.stanzas.JinglePacket;
40import eu.siacs.conversations.xmpp.jingle.stanzas.Reason;
41import eu.siacs.conversations.xmpp.stanzas.IqPacket;
42import rocks.xmpp.addr.Jid;
43
44public class JingleConnection implements Transferable {
45
46 private static final String JET_OMEMO_CIPHER = "urn:xmpp:ciphers:aes-128-gcm-nopadding";
47
48 private static final int JINGLE_STATUS_INITIATED = 0;
49 private static final int JINGLE_STATUS_ACCEPTED = 1;
50 private static final int JINGLE_STATUS_FINISHED = 4;
51 static final int JINGLE_STATUS_TRANSMITTING = 5;
52 private static final int JINGLE_STATUS_FAILED = 99;
53 private static final int JINGLE_STATUS_OFFERED = -1;
54 private JingleConnectionManager mJingleConnectionManager;
55 private XmppConnectionService mXmppConnectionService;
56 private Content.Version ftVersion = Content.Version.FT_3;
57
58 private int ibbBlockSize = 8192;
59
60 private int mJingleStatus = JINGLE_STATUS_OFFERED;
61 private int mStatus = Transferable.STATUS_UNKNOWN;
62 private Message message;
63 private String sessionId;
64 private Account account;
65 private Jid initiator;
66 private Jid responder;
67 private List<JingleCandidate> candidates = new ArrayList<>();
68 private ConcurrentHashMap<String, JingleSocks5Transport> connections = new ConcurrentHashMap<>();
69
70 private String transportId;
71 private Element fileOffer;
72 private DownloadableFile file = null;
73
74 private String contentName;
75 private String contentCreator;
76 private Transport initialTransport;
77 private boolean remoteSupportsOmemoJet;
78
79 private int mProgress = 0;
80
81 private boolean receivedCandidate = false;
82 private boolean sentCandidate = false;
83
84 private boolean acceptedAutomatically = false;
85 private boolean cancelled = false;
86
87 private XmppAxolotlMessage mXmppAxolotlMessage;
88
89 private JingleTransport transport = null;
90
91 private OutputStream mFileOutputStream;
92 private InputStream mFileInputStream;
93
94 private OnIqPacketReceived responseListener = (account, packet) -> {
95 if (packet.getType() != IqPacket.TYPE.RESULT) {
96 fail(IqParser.extractErrorMessage(packet));
97 }
98 };
99 private byte[] expectedHash = new byte[0];
100 private final OnFileTransmissionStatusChanged onFileTransmissionStatusChanged = new OnFileTransmissionStatusChanged() {
101
102 @Override
103 public void onFileTransmitted(DownloadableFile file) {
104 if (responding()) {
105 if (expectedHash.length > 0 && !Arrays.equals(expectedHash, file.getSha1Sum())) {
106 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": hashes did not match");
107 }
108 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": file transmitted(). we are responding");
109 sendSuccess();
110 mXmppConnectionService.getFileBackend().updateFileParams(message);
111 mXmppConnectionService.databaseBackend.createMessage(message);
112 mXmppConnectionService.markMessage(message, Message.STATUS_RECEIVED);
113 if (acceptedAutomatically) {
114 message.markUnread();
115 if (message.getEncryption() == Message.ENCRYPTION_PGP) {
116 account.getPgpDecryptionService().decrypt(message, true);
117 } else {
118 mXmppConnectionService.getFileBackend().updateMediaScanner(file, () -> JingleConnection.this.mXmppConnectionService.getNotificationService().push(message));
119
120 }
121 Log.d(Config.LOGTAG, "successfully transmitted file:" + file.getAbsolutePath() + " (" + CryptoHelper.bytesToHex(file.getSha1Sum()) + ")");
122 return;
123 } else if (message.getEncryption() == Message.ENCRYPTION_PGP) {
124 account.getPgpDecryptionService().decrypt(message, true);
125 }
126 } else {
127 if (ftVersion == Content.Version.FT_5) { //older Conversations will break when receiving a session-info
128 sendHash();
129 }
130 if (message.getEncryption() == Message.ENCRYPTION_PGP) {
131 account.getPgpDecryptionService().decrypt(message, false);
132 }
133 if (message.getEncryption() == Message.ENCRYPTION_PGP || message.getEncryption() == Message.ENCRYPTION_DECRYPTED) {
134 file.delete();
135 }
136 }
137 Log.d(Config.LOGTAG, "successfully transmitted file:" + file.getAbsolutePath() + " (" + CryptoHelper.bytesToHex(file.getSha1Sum()) + ")");
138 if (message.getEncryption() != Message.ENCRYPTION_PGP) {
139 mXmppConnectionService.getFileBackend().updateMediaScanner(file);
140 }
141 }
142
143 @Override
144 public void onFileTransferAborted() {
145 JingleConnection.this.sendCancel();
146 JingleConnection.this.fail();
147 }
148 };
149 private OnTransportConnected onIbbTransportConnected = new OnTransportConnected() {
150 @Override
151 public void failed() {
152 Log.d(Config.LOGTAG, "ibb open failed");
153 }
154
155 @Override
156 public void established() {
157 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": ibb transport connected. sending file");
158 mJingleStatus = JINGLE_STATUS_TRANSMITTING;
159 JingleConnection.this.transport.send(file, onFileTransmissionStatusChanged);
160 }
161 };
162 private OnProxyActivated onProxyActivated = new OnProxyActivated() {
163
164 @Override
165 public void success() {
166 if (initiator.equals(account.getJid())) {
167 Log.d(Config.LOGTAG, "we were initiating. sending file");
168 transport.send(file, onFileTransmissionStatusChanged);
169 } else {
170 transport.receive(file, onFileTransmissionStatusChanged);
171 Log.d(Config.LOGTAG, "we were responding. receiving file");
172 }
173 }
174
175 @Override
176 public void failed() {
177 Log.d(Config.LOGTAG, account.getJid().asBareJid()+": proxy activation failed");
178 if (initiating()) {
179 sendFallbackToIbb();
180 }
181 }
182 };
183
184 public JingleConnection(JingleConnectionManager mJingleConnectionManager) {
185 this.mJingleConnectionManager = mJingleConnectionManager;
186 this.mXmppConnectionService = mJingleConnectionManager
187 .getXmppConnectionService();
188 }
189
190 private boolean responding() {
191 return responder != null && responder.equals(account.getJid());
192 }
193
194 private boolean initiating() {
195 return initiator.equals(account.getJid());
196 }
197
198 InputStream getFileInputStream() {
199 return this.mFileInputStream;
200 }
201
202 OutputStream getFileOutputStream() throws IOException {
203 if (this.file == null) {
204 Log.d(Config.LOGTAG, "file object was not assigned");
205 return null;
206 }
207 this.file.getParentFile().mkdirs();
208 this.file.createNewFile();
209 this.mFileOutputStream = AbstractConnectionManager.createOutputStream(this.file);
210 return this.mFileOutputStream;
211 }
212
213 public String getSessionId() {
214 return this.sessionId;
215 }
216
217 public Account getAccount() {
218 return this.account;
219 }
220
221 public Jid getCounterPart() {
222 return this.message.getCounterpart();
223 }
224
225 public void deliverPacket(JinglePacket packet) {
226 boolean returnResult = true;
227 if (packet.isAction("session-terminate")) {
228 Reason reason = packet.getReason();
229 if (reason != null) {
230 if (reason.hasChild("cancel")) {
231 this.fail();
232 } else if (reason.hasChild("success")) {
233 this.receiveSuccess();
234 } else {
235 this.fail();
236 }
237 } else {
238 this.fail();
239 }
240 } else if (packet.isAction("session-accept")) {
241 returnResult = receiveAccept(packet);
242 } else if (packet.isAction("session-info")) {
243 Element checksum = packet.getChecksum();
244 Element file = checksum == null ? null : checksum.findChild("file");
245 Element hash = file == null ? null : file.findChild("hash", "urn:xmpp:hashes:2");
246 if (hash != null && "sha-1".equalsIgnoreCase(hash.getAttribute("algo"))) {
247 try {
248 this.expectedHash = Base64.decode(hash.getContent(), Base64.DEFAULT);
249 } catch (Exception e) {
250 this.expectedHash = new byte[0];
251 }
252 }
253 } else if (packet.isAction("transport-info")) {
254 returnResult = receiveTransportInfo(packet);
255 } else if (packet.isAction("transport-replace")) {
256 if (packet.getJingleContent().hasIbbTransport()) {
257 returnResult = this.receiveFallbackToIbb(packet);
258 } else {
259 returnResult = false;
260 Log.d(Config.LOGTAG, "trying to fallback to something unknown"
261 + packet.toString());
262 }
263 } else if (packet.isAction("transport-accept")) {
264 returnResult = this.receiveTransportAccept(packet);
265 } else {
266 Log.d(Config.LOGTAG, "packet arrived in connection. action was "
267 + packet.getAction());
268 returnResult = false;
269 }
270 IqPacket response;
271 if (returnResult) {
272 response = packet.generateResponse(IqPacket.TYPE.RESULT);
273
274 } else {
275 response = packet.generateResponse(IqPacket.TYPE.ERROR);
276 }
277 mXmppConnectionService.sendIqPacket(account, response, null);
278 }
279
280 public void init(final Message message) {
281 if (message.getEncryption() == Message.ENCRYPTION_AXOLOTL) {
282 Conversation conversation = (Conversation) message.getConversation();
283 conversation.getAccount().getAxolotlService().prepareKeyTransportMessage(conversation, xmppAxolotlMessage -> {
284 if (xmppAxolotlMessage != null) {
285 init(message, xmppAxolotlMessage);
286 } else {
287 fail();
288 }
289 });
290 } else {
291 init(message, null);
292 }
293 }
294
295 private void init(Message message, XmppAxolotlMessage xmppAxolotlMessage) {
296 this.mXmppAxolotlMessage = xmppAxolotlMessage;
297 this.contentCreator = "initiator";
298 this.contentName = this.mJingleConnectionManager.nextRandomId();
299 this.message = message;
300 this.account = message.getConversation().getAccount();
301 final List<String> remoteFeatures = getRemoteFeatures();
302 upgradeNamespace(remoteFeatures);
303 this.initialTransport = remoteFeatures.contains(Namespace.JINGLE_TRANSPORTS_S5B) ? Transport.SOCKS : Transport.IBB;
304 this.remoteSupportsOmemoJet = remoteFeatures.contains(Namespace.JINGLE_ENCRYPTED_TRANSPORT_OMEMO);
305 this.message.setTransferable(this);
306 this.mStatus = Transferable.STATUS_UPLOADING;
307 this.initiator = this.account.getJid();
308 this.responder = this.message.getCounterpart();
309 this.sessionId = this.mJingleConnectionManager.nextRandomId();
310 this.transportId = this.mJingleConnectionManager.nextRandomId();
311 if (this.initialTransport == Transport.IBB) {
312 this.sendInitRequest();
313 } else {
314 gatherAndConnectDirectCandidates();
315 this.mJingleConnectionManager.getPrimaryCandidate(account, initiating(), (success, candidate) -> {
316 if (success) {
317 final JingleSocks5Transport socksConnection = new JingleSocks5Transport(this, candidate);
318 connections.put(candidate.getCid(), socksConnection);
319 socksConnection.connect(new OnTransportConnected() {
320
321 @Override
322 public void failed() {
323 Log.d(Config.LOGTAG,
324 "connection to our own primary candidete failed");
325 sendInitRequest();
326 }
327
328 @Override
329 public void established() {
330 Log.d(Config.LOGTAG,
331 "successfully connected to our own primary candidate");
332 mergeCandidate(candidate);
333 sendInitRequest();
334 }
335 });
336 mergeCandidate(candidate);
337 } else {
338 Log.d(Config.LOGTAG, "no primary candidate of our own was found");
339 sendInitRequest();
340 }
341 });
342 }
343
344 }
345
346 private void gatherAndConnectDirectCandidates() {
347 final List<JingleCandidate> directCandidates;
348 if (Config.USE_DIRECT_JINGLE_CANDIDATES) {
349 if (account.isOnion() || mXmppConnectionService.useTorToConnect()) {
350 directCandidates = Collections.emptyList();
351 } else {
352 directCandidates = DirectConnectionUtils.getLocalCandidates(account.getJid());
353 }
354 } else {
355 directCandidates = Collections.emptyList();
356 }
357 for (JingleCandidate directCandidate : directCandidates) {
358 final JingleSocks5Transport socksConnection = new JingleSocks5Transport(this, directCandidate);
359 connections.put(directCandidate.getCid(), socksConnection);
360 candidates.add(directCandidate);
361 }
362 }
363
364 private void upgradeNamespace(List<String> remoteFeatures) {
365 if (remoteFeatures.contains(Content.Version.FT_5.getNamespace())) {
366 this.ftVersion = Content.Version.FT_5;
367 } else if (remoteFeatures.contains(Content.Version.FT_4.getNamespace())) {
368 this.ftVersion = Content.Version.FT_4;
369 }
370 }
371
372 private List<String> getRemoteFeatures() {
373 Jid jid = this.message.getCounterpart();
374 String resource = jid != null ? jid.getResource() : null;
375 if (resource != null) {
376 Presence presence = this.account.getRoster().getContact(jid).getPresences().getPresences().get(resource);
377 ServiceDiscoveryResult result = presence != null ? presence.getServiceDiscoveryResult() : null;
378 return result == null ? Collections.emptyList() : result.getFeatures();
379 } else {
380 return Collections.emptyList();
381 }
382 }
383
384 public void init(Account account, JinglePacket packet) {
385 this.mJingleStatus = JINGLE_STATUS_INITIATED;
386 Conversation conversation = this.mXmppConnectionService
387 .findOrCreateConversation(account,
388 packet.getFrom().asBareJid(), false, false);
389 this.message = new Message(conversation, "", Message.ENCRYPTION_NONE);
390 this.message.setStatus(Message.STATUS_RECEIVED);
391 this.mStatus = Transferable.STATUS_OFFER;
392 this.message.setTransferable(this);
393 final Jid from = packet.getFrom();
394 this.message.setCounterpart(from);
395 this.account = account;
396 this.initiator = packet.getFrom();
397 this.responder = this.account.getJid();
398 this.sessionId = packet.getSessionId();
399 Content content = packet.getJingleContent();
400 this.contentCreator = content.getAttribute("creator");
401 this.initialTransport = content.hasSocks5Transport() ? Transport.SOCKS : Transport.IBB;
402 this.contentName = content.getAttribute("name");
403 this.transportId = content.getTransportId();
404
405 mXmppConnectionService.sendIqPacket(account, packet.generateResponse(IqPacket.TYPE.RESULT), null);
406
407 if (this.initialTransport == Transport.SOCKS) {
408 this.mergeCandidates(JingleCandidate.parse(content.socks5transport().getChildren()));
409 } else if (this.initialTransport == Transport.IBB) {
410 final String receivedBlockSize = content.ibbTransport().getAttribute("block-size");
411 if (receivedBlockSize != null) {
412 try {
413 this.ibbBlockSize = Math.min(Integer.parseInt(receivedBlockSize), this.ibbBlockSize);
414 } catch (NumberFormatException e) {
415 Log.d(Config.LOGTAG, "number format exception " + e.getMessage());
416 this.sendCancel();
417 this.fail();
418 return;
419 }
420 } else {
421 Log.d(Config.LOGTAG, "received block size was null");
422 this.sendCancel();
423 this.fail();
424 return;
425 }
426 }
427 this.ftVersion = content.getVersion();
428 if (ftVersion == null) {
429 this.sendCancel();
430 this.fail();
431 return;
432 }
433 this.fileOffer = content.getFileOffer(this.ftVersion);
434
435 if (fileOffer != null) {
436 Element encrypted = fileOffer.findChild("encrypted", AxolotlService.PEP_PREFIX);
437 if (encrypted == null) {
438 final Element security = content.findChild("security", Namespace.JINGLE_ENCRYPTED_TRANSPORT);
439 if (security != null && AxolotlService.PEP_PREFIX.equals(security.getAttribute("type"))) {
440 Log.d(Config.LOGTAG, account.getJid().asBareJid()+": received jingle file offer with JET");
441 encrypted = security.findChild("encrypted", AxolotlService.PEP_PREFIX);
442 }
443 }
444 if (encrypted != null) {
445 this.mXmppAxolotlMessage = XmppAxolotlMessage.fromElement(encrypted, packet.getFrom().asBareJid());
446 }
447 Element fileSize = fileOffer.findChild("size");
448 final String path = fileOffer.findChildContent("name");
449 if (path != null) {
450 AbstractConnectionManager.Extension extension = AbstractConnectionManager.Extension.of(path);
451 if (VALID_IMAGE_EXTENSIONS.contains(extension.main)) {
452 message.setType(Message.TYPE_IMAGE);
453 message.setRelativeFilePath(message.getUuid() + "." + extension.main);
454 } else if (VALID_CRYPTO_EXTENSIONS.contains(extension.main)) {
455 if (VALID_IMAGE_EXTENSIONS.contains(extension.secondary)) {
456 message.setType(Message.TYPE_IMAGE);
457 message.setRelativeFilePath(message.getUuid() + "." + extension.secondary);
458 } else {
459 message.setType(Message.TYPE_FILE);
460 message.setRelativeFilePath(message.getUuid() + (extension.secondary != null ? ("." + extension.secondary) : ""));
461 }
462 message.setEncryption(Message.ENCRYPTION_PGP);
463 } else {
464 message.setType(Message.TYPE_FILE);
465 message.setRelativeFilePath(message.getUuid() + (extension.main != null ? ("." + extension.main) : ""));
466 }
467 long size = parseLong(fileSize, 0);
468 message.setBody(Long.toString(size));
469 conversation.add(message);
470 mJingleConnectionManager.updateConversationUi(true);
471 this.file = this.mXmppConnectionService.getFileBackend().getFile(message, false);
472 if (mXmppAxolotlMessage != null) {
473 XmppAxolotlMessage.XmppAxolotlKeyTransportMessage transportMessage = account.getAxolotlService().processReceivingKeyTransportMessage(mXmppAxolotlMessage, false);
474 if (transportMessage != null) {
475 message.setEncryption(Message.ENCRYPTION_AXOLOTL);
476 this.file.setKey(transportMessage.getKey());
477 this.file.setIv(transportMessage.getIv());
478 message.setFingerprint(transportMessage.getFingerprint());
479 } else {
480 Log.d(Config.LOGTAG, "could not process KeyTransportMessage");
481 }
482 }
483 message.resetFileParams();
484 this.file.setExpectedSize(size);
485 if (mJingleConnectionManager.hasStoragePermission()
486 && size < this.mJingleConnectionManager.getAutoAcceptFileSize()
487 && mXmppConnectionService.isDataSaverDisabled()) {
488 Log.d(Config.LOGTAG, "auto accepting file from " + packet.getFrom());
489 this.acceptedAutomatically = true;
490 this.sendAccept();
491 } else {
492 message.markUnread();
493 Log.d(Config.LOGTAG,
494 "not auto accepting new file offer with size: "
495 + size
496 + " allowed size:"
497 + this.mJingleConnectionManager
498 .getAutoAcceptFileSize());
499 this.mXmppConnectionService.getNotificationService().push(message);
500 }
501 Log.d(Config.LOGTAG, "receiving file: expecting size of " + this.file.getExpectedSize());
502 } else {
503 this.sendCancel();
504 this.fail();
505 }
506 } else {
507 this.sendCancel();
508 this.fail();
509 }
510 }
511
512 private static long parseLong(final Element element, final long l) {
513 final String input = element == null ? null : element.getContent();
514 if (input == null) {
515 return l;
516 }
517 try {
518 return Long.parseLong(input);
519 } catch (Exception e) {
520 return l;
521 }
522 }
523
524 private void sendInitRequest() {
525 JinglePacket packet = this.bootstrapPacket("session-initiate");
526 Content content = new Content(this.contentCreator, this.contentName);
527 if (message.isFileOrImage()) {
528 content.setTransportId(this.transportId);
529 this.file = this.mXmppConnectionService.getFileBackend().getFile(message, false);
530 if (message.getEncryption() == Message.ENCRYPTION_AXOLOTL) {
531 this.file.setKey(mXmppAxolotlMessage.getInnerKey());
532 this.file.setIv(mXmppAxolotlMessage.getIV());
533 this.file.setExpectedSize(file.getSize() + 16);
534 final Element file = content.setFileOffer(this.file, false, this.ftVersion);
535 if (remoteSupportsOmemoJet) {
536 Log.d(Config.LOGTAG, account.getJid().asBareJid()+": remote announced support for JET");
537 final Element security = new Element("security", Namespace.JINGLE_ENCRYPTED_TRANSPORT);
538 security.setAttribute("name", this.contentName);
539 security.setAttribute("cipher", JET_OMEMO_CIPHER);
540 security.setAttribute("type", AxolotlService.PEP_PREFIX);
541 security.addChild(mXmppAxolotlMessage.toElement());
542 content.addChild(security);
543 } else {
544 file.addChild(mXmppAxolotlMessage.toElement());
545 }
546 } else {
547 this.file.setExpectedSize(file.getSize());
548 content.setFileOffer(this.file, false, this.ftVersion);
549 }
550 message.resetFileParams();
551 try {
552 this.mFileInputStream = new FileInputStream(file);
553 } catch (FileNotFoundException e) {
554 abort();
555 return;
556 }
557 content.setTransportId(this.transportId);
558 if (this.initialTransport == Transport.IBB) {
559 content.ibbTransport().setAttribute("block-size", Integer.toString(this.ibbBlockSize));
560 } else {
561 content.socks5transport().setChildren(getCandidatesAsElements());
562 }
563 packet.setContent(content);
564 this.sendJinglePacket(packet, (account, response) -> {
565 if (response.getType() == IqPacket.TYPE.RESULT) {
566 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": other party received offer");
567 if (mJingleStatus == JINGLE_STATUS_OFFERED) {
568 mJingleStatus = JINGLE_STATUS_INITIATED;
569 mXmppConnectionService.markMessage(message, Message.STATUS_OFFERED);
570 } else {
571 Log.d(Config.LOGTAG, "received ack for offer when status was " + mJingleStatus);
572 }
573 } else {
574 fail(IqParser.extractErrorMessage(response));
575 }
576 });
577
578 }
579 }
580
581 private void sendHash() {
582 JinglePacket packet = this.bootstrapPacket("session-info");
583 packet.addChecksum(file.getSha1Sum(), ftVersion.getNamespace());
584 this.sendJinglePacket(packet);
585 }
586
587 private List<Element> getCandidatesAsElements() {
588 List<Element> elements = new ArrayList<>();
589 for (JingleCandidate c : this.candidates) {
590 if (c.isOurs()) {
591 elements.add(c.toElement());
592 }
593 }
594 return elements;
595 }
596
597 private void sendAccept() {
598 mJingleStatus = JINGLE_STATUS_ACCEPTED;
599 this.mStatus = Transferable.STATUS_DOWNLOADING;
600 this.mJingleConnectionManager.updateConversationUi(true);
601 if (initialTransport == Transport.SOCKS) {
602 sendAcceptSocks();
603 } else {
604 sendAcceptIbb();
605 }
606 }
607
608 private void sendAcceptSocks() {
609 gatherAndConnectDirectCandidates();
610 this.mJingleConnectionManager.getPrimaryCandidate(this.account, initiating(), (success, candidate) -> {
611 final JinglePacket packet = bootstrapPacket("session-accept");
612 final Content content = new Content(contentCreator, contentName);
613 content.setFileOffer(fileOffer, ftVersion);
614 content.setTransportId(transportId);
615 if (success && candidate != null && !equalCandidateExists(candidate)) {
616 final JingleSocks5Transport socksConnection = new JingleSocks5Transport(this, candidate);
617 connections.put(candidate.getCid(), socksConnection);
618 socksConnection.connect(new OnTransportConnected() {
619
620 @Override
621 public void failed() {
622 Log.d(Config.LOGTAG, "connection to our own primary candidate failed");
623 content.socks5transport().setChildren(getCandidatesAsElements());
624 packet.setContent(content);
625 sendJinglePacket(packet);
626 connectNextCandidate();
627 }
628
629 @Override
630 public void established() {
631 Log.d(Config.LOGTAG, "connected to primary candidate");
632 mergeCandidate(candidate);
633 content.socks5transport().setChildren(getCandidatesAsElements());
634 packet.setContent(content);
635 sendJinglePacket(packet);
636 connectNextCandidate();
637 }
638 });
639 } else {
640 Log.d(Config.LOGTAG, "did not find a primary candidate for ourselves");
641 content.socks5transport().setChildren(getCandidatesAsElements());
642 packet.setContent(content);
643 sendJinglePacket(packet);
644 connectNextCandidate();
645 }
646 });
647 }
648
649 private void sendAcceptIbb() {
650 this.transport = new JingleInbandTransport(this, this.transportId, this.ibbBlockSize);
651 final JinglePacket packet = bootstrapPacket("session-accept");
652 final Content content = new Content(contentCreator, contentName);
653 content.setFileOffer(fileOffer, ftVersion);
654 content.setTransportId(transportId);
655 content.ibbTransport().setAttribute("block-size", this.ibbBlockSize);
656 packet.setContent(content);
657 this.transport.receive(file, onFileTransmissionStatusChanged);
658 this.sendJinglePacket(packet);
659 }
660
661 private JinglePacket bootstrapPacket(String action) {
662 JinglePacket packet = new JinglePacket();
663 packet.setAction(action);
664 packet.setFrom(account.getJid());
665 packet.setTo(this.message.getCounterpart());
666 packet.setSessionId(this.sessionId);
667 packet.setInitiator(this.initiator);
668 return packet;
669 }
670
671 private void sendJinglePacket(JinglePacket packet) {
672 mXmppConnectionService.sendIqPacket(account, packet, responseListener);
673 }
674
675 private void sendJinglePacket(JinglePacket packet, OnIqPacketReceived callback) {
676 mXmppConnectionService.sendIqPacket(account, packet, callback);
677 }
678
679 private boolean receiveAccept(JinglePacket packet) {
680 if (this.mJingleStatus != JINGLE_STATUS_INITIATED) {
681 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": received out of order session-accept");
682 return false;
683 }
684 this.mJingleStatus = JINGLE_STATUS_ACCEPTED;
685 mXmppConnectionService.markMessage(message, Message.STATUS_UNSEND);
686 Content content = packet.getJingleContent();
687 if (content.hasSocks5Transport()) {
688 mergeCandidates(JingleCandidate.parse(content.socks5transport().getChildren()));
689 this.connectNextCandidate();
690 return true;
691 } else if (content.hasIbbTransport()) {
692 String receivedBlockSize = packet.getJingleContent().ibbTransport().getAttribute("block-size");
693 if (receivedBlockSize != null) {
694 try {
695 int bs = Integer.parseInt(receivedBlockSize);
696 if (bs > this.ibbBlockSize) {
697 this.ibbBlockSize = bs;
698 }
699 } catch (Exception e) {
700 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": unable to parse block size in session-accept");
701 }
702 }
703 this.transport = new JingleInbandTransport(this, this.transportId, this.ibbBlockSize);
704 this.transport.connect(onIbbTransportConnected);
705 return true;
706 } else {
707 return false;
708 }
709 }
710
711 private boolean receiveTransportInfo(JinglePacket packet) {
712 Content content = packet.getJingleContent();
713 if (content.hasSocks5Transport()) {
714 if (content.socks5transport().hasChild("activated")) {
715 if ((this.transport != null) && (this.transport instanceof JingleSocks5Transport)) {
716 onProxyActivated.success();
717 } else {
718 String cid = content.socks5transport().findChild("activated").getAttribute("cid");
719 Log.d(Config.LOGTAG, "received proxy activated (" + cid
720 + ")prior to choosing our own transport");
721 JingleSocks5Transport connection = this.connections.get(cid);
722 if (connection != null) {
723 connection.setActivated(true);
724 } else {
725 Log.d(Config.LOGTAG, "activated connection not found");
726 this.sendCancel();
727 this.fail();
728 }
729 }
730 return true;
731 } else if (content.socks5transport().hasChild("proxy-error")) {
732 onProxyActivated.failed();
733 return true;
734 } else if (content.socks5transport().hasChild("candidate-error")) {
735 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": received candidate error");
736 this.receivedCandidate = true;
737 if (mJingleStatus == JINGLE_STATUS_ACCEPTED && this.sentCandidate) {
738 this.connect();
739 }
740 return true;
741 } else if (content.socks5transport().hasChild("candidate-used")) {
742 String cid = content.socks5transport().findChild("candidate-used").getAttribute("cid");
743 if (cid != null) {
744 Log.d(Config.LOGTAG, "candidate used by counterpart:" + cid);
745 JingleCandidate candidate = getCandidate(cid);
746 if (candidate == null) {
747 Log.d(Config.LOGTAG, "could not find candidate with cid=" + cid);
748 return false;
749 }
750 candidate.flagAsUsedByCounterpart();
751 this.receivedCandidate = true;
752 if (mJingleStatus == JINGLE_STATUS_ACCEPTED && this.sentCandidate) {
753 this.connect();
754 } else {
755 Log.d(Config.LOGTAG, "ignoring because file is already in transmission or we haven't sent our candidate yet status=" + mJingleStatus + " sentCandidate=" + sentCandidate);
756 }
757 return true;
758 } else {
759 return false;
760 }
761 } else {
762 return false;
763 }
764 } else {
765 return true;
766 }
767 }
768
769 private void connect() {
770 final JingleSocks5Transport connection = chooseConnection();
771 this.transport = connection;
772 if (connection == null) {
773 Log.d(Config.LOGTAG, account.getJid().asBareJid()+": could not find suitable candidate");
774 this.disconnectSocks5Connections();
775 if (initiating()) {
776 this.sendFallbackToIbb();
777 }
778 } else {
779 final JingleCandidate candidate = connection.getCandidate();
780 Log.d(Config.LOGTAG, account.getJid().asBareJid()+": elected candidate "+candidate.getHost()+":"+candidate.getPort());
781 this.mJingleStatus = JINGLE_STATUS_TRANSMITTING;
782 if (connection.needsActivation()) {
783 if (connection.getCandidate().isOurs()) {
784 final String sid;
785 if (ftVersion == Content.Version.FT_3) {
786 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": use session ID instead of transport ID to activate proxy");
787 sid = getSessionId();
788 } else {
789 sid = getTransportId();
790 }
791 Log.d(Config.LOGTAG, "candidate "
792 + connection.getCandidate().getCid()
793 + " was our proxy. going to activate");
794 IqPacket activation = new IqPacket(IqPacket.TYPE.SET);
795 activation.setTo(connection.getCandidate().getJid());
796 activation.query("http://jabber.org/protocol/bytestreams")
797 .setAttribute("sid", sid);
798 activation.query().addChild("activate")
799 .setContent(this.getCounterPart().toString());
800 mXmppConnectionService.sendIqPacket(account, activation, (account, response) -> {
801 if (response.getType() != IqPacket.TYPE.RESULT) {
802 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": " + response.toString());
803 sendProxyError();
804 onProxyActivated.failed();
805 } else {
806 sendProxyActivated(connection.getCandidate().getCid());
807 onProxyActivated.success();
808 }
809 });
810 } else {
811 Log.d(Config.LOGTAG,
812 "candidate "
813 + connection.getCandidate().getCid()
814 + " was a proxy. waiting for other party to activate");
815 }
816 } else {
817 if (initiating()) {
818 Log.d(Config.LOGTAG, "we were initiating. sending file");
819 connection.send(file, onFileTransmissionStatusChanged);
820 } else {
821 Log.d(Config.LOGTAG, "we were responding. receiving file");
822 connection.receive(file, onFileTransmissionStatusChanged);
823 }
824 }
825 }
826 }
827
828 private JingleSocks5Transport chooseConnection() {
829 JingleSocks5Transport connection = null;
830 for (Entry<String, JingleSocks5Transport> cursor : connections
831 .entrySet()) {
832 JingleSocks5Transport currentConnection = cursor.getValue();
833 // Log.d(Config.LOGTAG,"comparing candidate: "+currentConnection.getCandidate().toString());
834 if (currentConnection.isEstablished()
835 && (currentConnection.getCandidate().isUsedByCounterpart() || (!currentConnection
836 .getCandidate().isOurs()))) {
837 // Log.d(Config.LOGTAG,"is usable");
838 if (connection == null) {
839 connection = currentConnection;
840 } else {
841 if (connection.getCandidate().getPriority() < currentConnection
842 .getCandidate().getPriority()) {
843 connection = currentConnection;
844 } else if (connection.getCandidate().getPriority() == currentConnection
845 .getCandidate().getPriority()) {
846 // Log.d(Config.LOGTAG,"found two candidates with same priority");
847 if (initiating()) {
848 if (currentConnection.getCandidate().isOurs()) {
849 connection = currentConnection;
850 }
851 } else {
852 if (!currentConnection.getCandidate().isOurs()) {
853 connection = currentConnection;
854 }
855 }
856 }
857 }
858 }
859 }
860 return connection;
861 }
862
863 private void sendSuccess() {
864 JinglePacket packet = bootstrapPacket("session-terminate");
865 Reason reason = new Reason();
866 reason.addChild("success");
867 packet.setReason(reason);
868 this.sendJinglePacket(packet);
869 this.disconnectSocks5Connections();
870 this.mJingleStatus = JINGLE_STATUS_FINISHED;
871 this.message.setStatus(Message.STATUS_RECEIVED);
872 this.message.setTransferable(null);
873 this.mXmppConnectionService.updateMessage(message, false);
874 this.mJingleConnectionManager.finishConnection(this);
875 }
876
877 private void sendFallbackToIbb() {
878 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": sending fallback to ibb");
879 JinglePacket packet = this.bootstrapPacket("transport-replace");
880 Content content = new Content(this.contentCreator, this.contentName);
881 this.transportId = this.mJingleConnectionManager.nextRandomId();
882 content.setTransportId(this.transportId);
883 content.ibbTransport().setAttribute("block-size",
884 Integer.toString(this.ibbBlockSize));
885 packet.setContent(content);
886 this.sendJinglePacket(packet);
887 }
888
889
890 private boolean receiveFallbackToIbb(JinglePacket packet) {
891 Log.d(Config.LOGTAG, account.getJid().asBareJid()+": receiving fallback to ibb");
892 final String receivedBlockSize = packet.getJingleContent().ibbTransport().getAttribute("block-size");
893 if (receivedBlockSize != null) {
894 try {
895 final int bs = Integer.parseInt(receivedBlockSize);
896 if (bs < this.ibbBlockSize) {
897 this.ibbBlockSize = bs;
898 }
899 } catch (NumberFormatException e) {
900 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": unable to parse block size in transport-replace");
901 }
902 }
903 this.transportId = packet.getJingleContent().getTransportId();
904 this.transport = new JingleInbandTransport(this, this.transportId, this.ibbBlockSize);
905
906 final JinglePacket answer = bootstrapPacket("transport-accept");
907
908 final Content content = new Content(contentCreator, contentName);
909 content.ibbTransport().setAttribute("block-size", this.ibbBlockSize);
910 content.ibbTransport().setAttribute("sid", this.transportId);
911 answer.setContent(content);
912
913
914 if (initiating()) {
915 this.sendJinglePacket(answer, (account, response) -> {
916 if (response.getType() == IqPacket.TYPE.RESULT) {
917 Log.d(Config.LOGTAG, account.getJid().asBareJid() + " recipient ACKed our transport-accept. creating ibb");
918 transport.connect(onIbbTransportConnected);
919 }
920 });
921 } else {
922 this.transport.receive(file, onFileTransmissionStatusChanged);
923 this.sendJinglePacket(answer);
924 }
925 return true;
926 }
927
928 private boolean receiveTransportAccept(JinglePacket packet) {
929 if (packet.getJingleContent().hasIbbTransport()) {
930 String receivedBlockSize = packet.getJingleContent().ibbTransport()
931 .getAttribute("block-size");
932 if (receivedBlockSize != null) {
933 try {
934 int bs = Integer.parseInt(receivedBlockSize);
935 if (bs < this.ibbBlockSize) {
936 this.ibbBlockSize = bs;
937 }
938 } catch (NumberFormatException e) {
939 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": unable to parse block size in transport-accept");
940 }
941 }
942 this.transport = new JingleInbandTransport(this, this.transportId, this.ibbBlockSize);
943
944 //might be receive instead if we are not initiating
945 if (initiating()) {
946 this.transport.connect(onIbbTransportConnected);
947 } else {
948 this.transport.receive(file, onFileTransmissionStatusChanged);
949 }
950 return true;
951 } else {
952 return false;
953 }
954 }
955
956 private void receiveSuccess() {
957 if (initiating()) {
958 this.mJingleStatus = JINGLE_STATUS_FINISHED;
959 this.mXmppConnectionService.markMessage(this.message, Message.STATUS_SEND_RECEIVED);
960 this.disconnectSocks5Connections();
961 if (this.transport instanceof JingleInbandTransport) {
962 this.transport.disconnect();
963 }
964 this.message.setTransferable(null);
965 this.mJingleConnectionManager.finishConnection(this);
966 } else {
967 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": received session-terminate/success while responding");
968 }
969 }
970
971 @Override
972 public void cancel() {
973 this.cancelled = true;
974 abort();
975 }
976
977 public void abort() {
978 this.disconnectSocks5Connections();
979 if (this.transport instanceof JingleInbandTransport) {
980 this.transport.disconnect();
981 }
982 this.sendCancel();
983 this.mJingleConnectionManager.finishConnection(this);
984 if (responding()) {
985 this.message.setTransferable(new TransferablePlaceholder(Transferable.STATUS_FAILED));
986 if (this.file != null) {
987 file.delete();
988 }
989 this.mJingleConnectionManager.updateConversationUi(true);
990 } else {
991 this.mXmppConnectionService.markMessage(this.message, Message.STATUS_SEND_FAILED, cancelled ? Message.ERROR_MESSAGE_CANCELLED : null);
992 this.message.setTransferable(null);
993 }
994 }
995
996 private void fail() {
997 fail(null);
998 }
999
1000 private void fail(String errorMessage) {
1001 this.mJingleStatus = JINGLE_STATUS_FAILED;
1002 this.disconnectSocks5Connections();
1003 if (this.transport instanceof JingleInbandTransport) {
1004 this.transport.disconnect();
1005 }
1006 FileBackend.close(mFileInputStream);
1007 FileBackend.close(mFileOutputStream);
1008 if (this.message != null) {
1009 if (responding()) {
1010 this.message.setTransferable(new TransferablePlaceholder(Transferable.STATUS_FAILED));
1011 if (this.file != null) {
1012 file.delete();
1013 }
1014 this.mJingleConnectionManager.updateConversationUi(true);
1015 } else {
1016 this.mXmppConnectionService.markMessage(this.message,
1017 Message.STATUS_SEND_FAILED,
1018 cancelled ? Message.ERROR_MESSAGE_CANCELLED : errorMessage);
1019 this.message.setTransferable(null);
1020 }
1021 }
1022 this.mJingleConnectionManager.finishConnection(this);
1023 }
1024
1025 private void sendCancel() {
1026 JinglePacket packet = bootstrapPacket("session-terminate");
1027 Reason reason = new Reason();
1028 reason.addChild("cancel");
1029 packet.setReason(reason);
1030 this.sendJinglePacket(packet);
1031 }
1032
1033 private void connectNextCandidate() {
1034 for (JingleCandidate candidate : this.candidates) {
1035 if ((!connections.containsKey(candidate.getCid()) && (!candidate
1036 .isOurs()))) {
1037 this.connectWithCandidate(candidate);
1038 return;
1039 }
1040 }
1041 this.sendCandidateError();
1042 }
1043
1044 private void connectWithCandidate(final JingleCandidate candidate) {
1045 final JingleSocks5Transport socksConnection = new JingleSocks5Transport(
1046 this, candidate);
1047 connections.put(candidate.getCid(), socksConnection);
1048 socksConnection.connect(new OnTransportConnected() {
1049
1050 @Override
1051 public void failed() {
1052 Log.d(Config.LOGTAG,
1053 "connection failed with " + candidate.getHost() + ":"
1054 + candidate.getPort());
1055 connectNextCandidate();
1056 }
1057
1058 @Override
1059 public void established() {
1060 Log.d(Config.LOGTAG,
1061 "established connection with " + candidate.getHost()
1062 + ":" + candidate.getPort());
1063 sendCandidateUsed(candidate.getCid());
1064 }
1065 });
1066 }
1067
1068 private void disconnectSocks5Connections() {
1069 Iterator<Entry<String, JingleSocks5Transport>> it = this.connections
1070 .entrySet().iterator();
1071 while (it.hasNext()) {
1072 Entry<String, JingleSocks5Transport> pairs = it.next();
1073 pairs.getValue().disconnect();
1074 it.remove();
1075 }
1076 }
1077
1078 private void sendProxyActivated(String cid) {
1079 final JinglePacket packet = bootstrapPacket("transport-info");
1080 final Content content = new Content(this.contentCreator, this.contentName);
1081 content.setTransportId(this.transportId);
1082 content.socks5transport().addChild("activated").setAttribute("cid", cid);
1083 packet.setContent(content);
1084 this.sendJinglePacket(packet);
1085 }
1086
1087 private void sendProxyError() {
1088 final JinglePacket packet = bootstrapPacket("transport-info");
1089 final Content content = new Content(this.contentCreator, this.contentName);
1090 content.setTransportId(this.transportId);
1091 content.socks5transport().addChild("proxy-error");
1092 packet.setContent(content);
1093 this.sendJinglePacket(packet);
1094 }
1095
1096 private void sendCandidateUsed(final String cid) {
1097 JinglePacket packet = bootstrapPacket("transport-info");
1098 Content content = new Content(this.contentCreator, this.contentName);
1099 content.setTransportId(this.transportId);
1100 content.socks5transport().addChild("candidate-used").setAttribute("cid", cid);
1101 packet.setContent(content);
1102 this.sentCandidate = true;
1103 if ((receivedCandidate) && (mJingleStatus == JINGLE_STATUS_ACCEPTED)) {
1104 connect();
1105 }
1106 this.sendJinglePacket(packet);
1107 }
1108
1109 private void sendCandidateError() {
1110 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": sending candidate error");
1111 JinglePacket packet = bootstrapPacket("transport-info");
1112 Content content = new Content(this.contentCreator, this.contentName);
1113 content.setTransportId(this.transportId);
1114 content.socks5transport().addChild("candidate-error");
1115 packet.setContent(content);
1116 this.sentCandidate = true;
1117 this.sendJinglePacket(packet);
1118 if (receivedCandidate && mJingleStatus == JINGLE_STATUS_ACCEPTED) {
1119 connect();
1120 }
1121 }
1122
1123 public int getJingleStatus() {
1124 return this.mJingleStatus;
1125 }
1126
1127 private boolean equalCandidateExists(JingleCandidate candidate) {
1128 for (JingleCandidate c : this.candidates) {
1129 if (c.equalValues(candidate)) {
1130 return true;
1131 }
1132 }
1133 return false;
1134 }
1135
1136 private void mergeCandidate(JingleCandidate candidate) {
1137 for (JingleCandidate c : this.candidates) {
1138 if (c.equals(candidate)) {
1139 return;
1140 }
1141 }
1142 this.candidates.add(candidate);
1143 }
1144
1145 private void mergeCandidates(List<JingleCandidate> candidates) {
1146 Collections.sort(candidates, (a, b) -> Integer.compare(b.getPriority(), a.getPriority()));
1147 for (JingleCandidate c : candidates) {
1148 mergeCandidate(c);
1149 }
1150 }
1151
1152 private JingleCandidate getCandidate(String cid) {
1153 for (JingleCandidate c : this.candidates) {
1154 if (c.getCid().equals(cid)) {
1155 return c;
1156 }
1157 }
1158 return null;
1159 }
1160
1161 void updateProgress(int i) {
1162 this.mProgress = i;
1163 mJingleConnectionManager.updateConversationUi(false);
1164 }
1165
1166 public String getTransportId() {
1167 return this.transportId;
1168 }
1169
1170 public Content.Version getFtVersion() {
1171 return this.ftVersion;
1172 }
1173
1174 public boolean hasTransportId(String sid) {
1175 return sid.equals(this.transportId);
1176 }
1177
1178 public JingleTransport getTransport() {
1179 return this.transport;
1180 }
1181
1182 public boolean start() {
1183 if (account.getStatus() == Account.State.ONLINE) {
1184 if (mJingleStatus == JINGLE_STATUS_INITIATED) {
1185 new Thread(this::sendAccept).start();
1186 }
1187 return true;
1188 } else {
1189 return false;
1190 }
1191 }
1192
1193 @Override
1194 public int getStatus() {
1195 return this.mStatus;
1196 }
1197
1198 @Override
1199 public long getFileSize() {
1200 if (this.file != null) {
1201 return this.file.getExpectedSize();
1202 } else {
1203 return 0;
1204 }
1205 }
1206
1207 @Override
1208 public int getProgress() {
1209 return this.mProgress;
1210 }
1211
1212 public AbstractConnectionManager getConnectionManager() {
1213 return this.mJingleConnectionManager;
1214 }
1215
1216 interface OnProxyActivated {
1217 void success();
1218
1219 void failed();
1220 }
1221}