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 } else {
274 response = packet.generateResponse(IqPacket.TYPE.ERROR);
275 final Element error = response.addChild("error").setAttribute("type", "cancel");
276 error.addChild("not-acceptable", "urn:ietf:params:xml:ns:xmpp-stanzas");
277 }
278 mXmppConnectionService.sendIqPacket(account, response, null);
279 }
280
281 public void init(final Message message) {
282 if (message.getEncryption() == Message.ENCRYPTION_AXOLOTL) {
283 Conversation conversation = (Conversation) message.getConversation();
284 conversation.getAccount().getAxolotlService().prepareKeyTransportMessage(conversation, xmppAxolotlMessage -> {
285 if (xmppAxolotlMessage != null) {
286 init(message, xmppAxolotlMessage);
287 } else {
288 fail();
289 }
290 });
291 } else {
292 init(message, null);
293 }
294 }
295
296 private void init(Message message, XmppAxolotlMessage xmppAxolotlMessage) {
297 this.mXmppAxolotlMessage = xmppAxolotlMessage;
298 this.contentCreator = "initiator";
299 this.contentName = this.mJingleConnectionManager.nextRandomId();
300 this.message = message;
301 this.account = message.getConversation().getAccount();
302 final List<String> remoteFeatures = getRemoteFeatures();
303 upgradeNamespace(remoteFeatures);
304 this.initialTransport = remoteFeatures.contains(Namespace.JINGLE_TRANSPORTS_S5B) ? Transport.SOCKS : Transport.IBB;
305 this.remoteSupportsOmemoJet = remoteFeatures.contains(Namespace.JINGLE_ENCRYPTED_TRANSPORT_OMEMO);
306 this.message.setTransferable(this);
307 this.mStatus = Transferable.STATUS_UPLOADING;
308 this.initiator = this.account.getJid();
309 this.responder = this.message.getCounterpart();
310 this.sessionId = this.mJingleConnectionManager.nextRandomId();
311 this.transportId = this.mJingleConnectionManager.nextRandomId();
312 if (this.initialTransport == Transport.IBB) {
313 this.sendInitRequest();
314 } else {
315 gatherAndConnectDirectCandidates();
316 this.mJingleConnectionManager.getPrimaryCandidate(account, initiating(), (success, candidate) -> {
317 if (success) {
318 final JingleSocks5Transport socksConnection = new JingleSocks5Transport(this, candidate);
319 connections.put(candidate.getCid(), socksConnection);
320 socksConnection.connect(new OnTransportConnected() {
321
322 @Override
323 public void failed() {
324 Log.d(Config.LOGTAG, String.format("connection to our own proxy65 candidate failed (%s:%d)", candidate.getHost(), candidate.getPort()));
325 sendInitRequest();
326 }
327
328 @Override
329 public void established() {
330 Log.d(Config.LOGTAG, "successfully connected to our own proxy65 candidate");
331 mergeCandidate(candidate);
332 sendInitRequest();
333 }
334 });
335 mergeCandidate(candidate);
336 } else {
337 Log.d(Config.LOGTAG, "no proxy65 candidate of our own was found");
338 sendInitRequest();
339 }
340 });
341 }
342
343 }
344
345 private void gatherAndConnectDirectCandidates() {
346 final List<JingleCandidate> directCandidates;
347 if (Config.USE_DIRECT_JINGLE_CANDIDATES) {
348 if (account.isOnion() || mXmppConnectionService.useTorToConnect()) {
349 directCandidates = Collections.emptyList();
350 } else {
351 directCandidates = DirectConnectionUtils.getLocalCandidates(account.getJid());
352 }
353 } else {
354 directCandidates = Collections.emptyList();
355 }
356 for (JingleCandidate directCandidate : directCandidates) {
357 final JingleSocks5Transport socksConnection = new JingleSocks5Transport(this, directCandidate);
358 connections.put(directCandidate.getCid(), socksConnection);
359 candidates.add(directCandidate);
360 }
361 }
362
363 private void upgradeNamespace(List<String> remoteFeatures) {
364 if (remoteFeatures.contains(Content.Version.FT_5.getNamespace())) {
365 this.ftVersion = Content.Version.FT_5;
366 } else if (remoteFeatures.contains(Content.Version.FT_4.getNamespace())) {
367 this.ftVersion = Content.Version.FT_4;
368 }
369 }
370
371 private List<String> getRemoteFeatures() {
372 Jid jid = this.message.getCounterpart();
373 String resource = jid != null ? jid.getResource() : null;
374 if (resource != null) {
375 Presence presence = this.account.getRoster().getContact(jid).getPresences().getPresences().get(resource);
376 ServiceDiscoveryResult result = presence != null ? presence.getServiceDiscoveryResult() : null;
377 return result == null ? Collections.emptyList() : result.getFeatures();
378 } else {
379 return Collections.emptyList();
380 }
381 }
382
383 public void init(Account account, JinglePacket packet) {
384 this.mJingleStatus = JINGLE_STATUS_INITIATED;
385 Conversation conversation = this.mXmppConnectionService
386 .findOrCreateConversation(account,
387 packet.getFrom().asBareJid(), false, false);
388 this.message = new Message(conversation, "", Message.ENCRYPTION_NONE);
389 this.message.setStatus(Message.STATUS_RECEIVED);
390 this.mStatus = Transferable.STATUS_OFFER;
391 this.message.setTransferable(this);
392 final Jid from = packet.getFrom();
393 this.message.setCounterpart(from);
394 this.account = account;
395 this.initiator = packet.getFrom();
396 this.responder = this.account.getJid();
397 this.sessionId = packet.getSessionId();
398 Content content = packet.getJingleContent();
399 this.contentCreator = content.getAttribute("creator");
400 this.initialTransport = content.hasSocks5Transport() ? Transport.SOCKS : Transport.IBB;
401 this.contentName = content.getAttribute("name");
402 this.transportId = content.getTransportId();
403
404 mXmppConnectionService.sendIqPacket(account, packet.generateResponse(IqPacket.TYPE.RESULT), null);
405
406 if (this.initialTransport == Transport.SOCKS) {
407 this.mergeCandidates(JingleCandidate.parse(content.socks5transport().getChildren()));
408 } else if (this.initialTransport == Transport.IBB) {
409 final String receivedBlockSize = content.ibbTransport().getAttribute("block-size");
410 if (receivedBlockSize != null) {
411 try {
412 this.ibbBlockSize = Math.min(Integer.parseInt(receivedBlockSize), this.ibbBlockSize);
413 } catch (NumberFormatException e) {
414 Log.d(Config.LOGTAG, "number format exception " + e.getMessage());
415 this.sendCancel();
416 this.fail();
417 return;
418 }
419 } else {
420 Log.d(Config.LOGTAG, "received block size was null");
421 this.sendCancel();
422 this.fail();
423 return;
424 }
425 }
426 this.ftVersion = content.getVersion();
427 if (ftVersion == null) {
428 this.sendCancel();
429 this.fail();
430 return;
431 }
432 this.fileOffer = content.getFileOffer(this.ftVersion);
433
434
435 if (fileOffer != null) {
436 boolean remoteIsUsingJet = false;
437 Element encrypted = fileOffer.findChild("encrypted", AxolotlService.PEP_PREFIX);
438 if (encrypted == null) {
439 final Element security = content.findChild("security", Namespace.JINGLE_ENCRYPTED_TRANSPORT);
440 if (security != null && AxolotlService.PEP_PREFIX.equals(security.getAttribute("type"))) {
441 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": received jingle file offer with JET");
442 encrypted = security.findChild("encrypted", AxolotlService.PEP_PREFIX);
443 remoteIsUsingJet = true;
444 }
445 }
446 if (encrypted != null) {
447 this.mXmppAxolotlMessage = XmppAxolotlMessage.fromElement(encrypted, packet.getFrom().asBareJid());
448 }
449 Element fileSize = fileOffer.findChild("size");
450 final String path = fileOffer.findChildContent("name");
451 if (path != null) {
452 AbstractConnectionManager.Extension extension = AbstractConnectionManager.Extension.of(path);
453 if (VALID_IMAGE_EXTENSIONS.contains(extension.main)) {
454 message.setType(Message.TYPE_IMAGE);
455 message.setRelativeFilePath(message.getUuid() + "." + extension.main);
456 } else if (VALID_CRYPTO_EXTENSIONS.contains(extension.main)) {
457 if (VALID_IMAGE_EXTENSIONS.contains(extension.secondary)) {
458 message.setType(Message.TYPE_IMAGE);
459 message.setRelativeFilePath(message.getUuid() + "." + extension.secondary);
460 } else {
461 message.setType(Message.TYPE_FILE);
462 message.setRelativeFilePath(message.getUuid() + (extension.secondary != null ? ("." + extension.secondary) : ""));
463 }
464 message.setEncryption(Message.ENCRYPTION_PGP);
465 } else {
466 message.setType(Message.TYPE_FILE);
467 message.setRelativeFilePath(message.getUuid() + (extension.main != null ? ("." + extension.main) : ""));
468 }
469 long size = parseLong(fileSize, 0);
470 message.setBody(Long.toString(size));
471 conversation.add(message);
472 mJingleConnectionManager.updateConversationUi(true);
473 this.file = this.mXmppConnectionService.getFileBackend().getFile(message, false);
474 if (mXmppAxolotlMessage != null) {
475 XmppAxolotlMessage.XmppAxolotlKeyTransportMessage transportMessage = account.getAxolotlService().processReceivingKeyTransportMessage(mXmppAxolotlMessage, false);
476 if (transportMessage != null) {
477 message.setEncryption(Message.ENCRYPTION_AXOLOTL);
478 this.file.setKey(transportMessage.getKey());
479 this.file.setIv(transportMessage.getIv());
480 message.setFingerprint(transportMessage.getFingerprint());
481 } else {
482 Log.d(Config.LOGTAG, "could not process KeyTransportMessage");
483 }
484 }
485 message.resetFileParams();
486 //legacy OMEMO encrypted file transfers reported the file size after encryption
487 //JET reports the plain text size. however lower levels of our receiving code still
488 //expect the cipher text size. so we just + 16 bytes (auth tag size) here
489 this.file.setExpectedSize(size + (remoteIsUsingJet ? 16 : 0));
490 if (mJingleConnectionManager.hasStoragePermission()
491 && size < this.mJingleConnectionManager.getAutoAcceptFileSize()
492 && mXmppConnectionService.isDataSaverDisabled()) {
493 Log.d(Config.LOGTAG, "auto accepting file from " + packet.getFrom());
494 this.acceptedAutomatically = true;
495 this.sendAccept();
496 } else {
497 message.markUnread();
498 Log.d(Config.LOGTAG,
499 "not auto accepting new file offer with size: "
500 + size
501 + " allowed size:"
502 + this.mJingleConnectionManager
503 .getAutoAcceptFileSize());
504 this.mXmppConnectionService.getNotificationService().push(message);
505 }
506 Log.d(Config.LOGTAG, "receiving file: expecting size of " + this.file.getExpectedSize());
507 } else {
508 this.sendCancel();
509 this.fail();
510 }
511 } else {
512 this.sendCancel();
513 this.fail();
514 }
515 }
516
517 private static long parseLong(final Element element, final long l) {
518 final String input = element == null ? null : element.getContent();
519 if (input == null) {
520 return l;
521 }
522 try {
523 return Long.parseLong(input);
524 } catch (Exception e) {
525 return l;
526 }
527 }
528
529 private void sendInitRequest() {
530 JinglePacket packet = this.bootstrapPacket("session-initiate");
531 Content content = new Content(this.contentCreator, this.contentName);
532 if (message.isFileOrImage()) {
533 content.setTransportId(this.transportId);
534 this.file = this.mXmppConnectionService.getFileBackend().getFile(message, false);
535 if (message.getEncryption() == Message.ENCRYPTION_AXOLOTL) {
536 this.file.setKey(mXmppAxolotlMessage.getInnerKey());
537 this.file.setIv(mXmppAxolotlMessage.getIV());
538 //legacy OMEMO encrypted file transfer reported file size of the encrypted file
539 //JET uses the file size of the plain text file. The difference is only 16 bytes (auth tag)
540 this.file.setExpectedSize(file.getSize() + (this.remoteSupportsOmemoJet ? 0 : 16));
541 final Element file = content.setFileOffer(this.file, false, this.ftVersion);
542 if (remoteSupportsOmemoJet) {
543 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": remote announced support for JET");
544 final Element security = new Element("security", Namespace.JINGLE_ENCRYPTED_TRANSPORT);
545 security.setAttribute("name", this.contentName);
546 security.setAttribute("cipher", JET_OMEMO_CIPHER);
547 security.setAttribute("type", AxolotlService.PEP_PREFIX);
548 security.addChild(mXmppAxolotlMessage.toElement());
549 content.addChild(security);
550 } else {
551 file.addChild(mXmppAxolotlMessage.toElement());
552 }
553 } else {
554 this.file.setExpectedSize(file.getSize());
555 content.setFileOffer(this.file, false, this.ftVersion);
556 }
557 message.resetFileParams();
558 try {
559 this.mFileInputStream = new FileInputStream(file);
560 } catch (FileNotFoundException e) {
561 abort();
562 return;
563 }
564 content.setTransportId(this.transportId);
565 if (this.initialTransport == Transport.IBB) {
566 content.ibbTransport().setAttribute("block-size", Integer.toString(this.ibbBlockSize));
567 } else {
568 content.socks5transport().setChildren(getCandidatesAsElements());
569 }
570 packet.setContent(content);
571 this.sendJinglePacket(packet, (account, response) -> {
572 if (response.getType() == IqPacket.TYPE.RESULT) {
573 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": other party received offer");
574 if (mJingleStatus == JINGLE_STATUS_OFFERED) {
575 mJingleStatus = JINGLE_STATUS_INITIATED;
576 mXmppConnectionService.markMessage(message, Message.STATUS_OFFERED);
577 } else {
578 Log.d(Config.LOGTAG, "received ack for offer when status was " + mJingleStatus);
579 }
580 } else {
581 fail(IqParser.extractErrorMessage(response));
582 }
583 });
584
585 }
586 }
587
588 private void sendHash() {
589 JinglePacket packet = this.bootstrapPacket("session-info");
590 packet.addChecksum(file.getSha1Sum(), ftVersion.getNamespace());
591 this.sendJinglePacket(packet);
592 }
593
594 private List<Element> getCandidatesAsElements() {
595 List<Element> elements = new ArrayList<>();
596 for (JingleCandidate c : this.candidates) {
597 if (c.isOurs()) {
598 elements.add(c.toElement());
599 }
600 }
601 return elements;
602 }
603
604 private void sendAccept() {
605 mJingleStatus = JINGLE_STATUS_ACCEPTED;
606 this.mStatus = Transferable.STATUS_DOWNLOADING;
607 this.mJingleConnectionManager.updateConversationUi(true);
608 if (initialTransport == Transport.SOCKS) {
609 sendAcceptSocks();
610 } else {
611 sendAcceptIbb();
612 }
613 }
614
615 private void sendAcceptSocks() {
616 gatherAndConnectDirectCandidates();
617 this.mJingleConnectionManager.getPrimaryCandidate(this.account, initiating(), (success, candidate) -> {
618 final JinglePacket packet = bootstrapPacket("session-accept");
619 final Content content = new Content(contentCreator, contentName);
620 content.setFileOffer(fileOffer, ftVersion);
621 content.setTransportId(transportId);
622 if (success && candidate != null && !equalCandidateExists(candidate)) {
623 final JingleSocks5Transport socksConnection = new JingleSocks5Transport(this, candidate);
624 connections.put(candidate.getCid(), socksConnection);
625 socksConnection.connect(new OnTransportConnected() {
626
627 @Override
628 public void failed() {
629 Log.d(Config.LOGTAG, "connection to our own proxy65 candidate failed");
630 content.socks5transport().setChildren(getCandidatesAsElements());
631 packet.setContent(content);
632 sendJinglePacket(packet);
633 connectNextCandidate();
634 }
635
636 @Override
637 public void established() {
638 Log.d(Config.LOGTAG, "connected to proxy65 candidate");
639 mergeCandidate(candidate);
640 content.socks5transport().setChildren(getCandidatesAsElements());
641 packet.setContent(content);
642 sendJinglePacket(packet);
643 connectNextCandidate();
644 }
645 });
646 } else {
647 Log.d(Config.LOGTAG, "did not find a proxy65 candidate for ourselves");
648 content.socks5transport().setChildren(getCandidatesAsElements());
649 packet.setContent(content);
650 sendJinglePacket(packet);
651 connectNextCandidate();
652 }
653 });
654 }
655
656 private void sendAcceptIbb() {
657 this.transport = new JingleInbandTransport(this, this.transportId, this.ibbBlockSize);
658 final JinglePacket packet = bootstrapPacket("session-accept");
659 final Content content = new Content(contentCreator, contentName);
660 content.setFileOffer(fileOffer, ftVersion);
661 content.setTransportId(transportId);
662 content.ibbTransport().setAttribute("block-size", this.ibbBlockSize);
663 packet.setContent(content);
664 this.transport.receive(file, onFileTransmissionStatusChanged);
665 this.sendJinglePacket(packet);
666 }
667
668 private JinglePacket bootstrapPacket(String action) {
669 JinglePacket packet = new JinglePacket();
670 packet.setAction(action);
671 packet.setFrom(account.getJid());
672 packet.setTo(this.message.getCounterpart());
673 packet.setSessionId(this.sessionId);
674 packet.setInitiator(this.initiator);
675 return packet;
676 }
677
678 private void sendJinglePacket(JinglePacket packet) {
679 mXmppConnectionService.sendIqPacket(account, packet, responseListener);
680 }
681
682 private void sendJinglePacket(JinglePacket packet, OnIqPacketReceived callback) {
683 mXmppConnectionService.sendIqPacket(account, packet, callback);
684 }
685
686 private boolean receiveAccept(JinglePacket packet) {
687 if (this.mJingleStatus != JINGLE_STATUS_INITIATED) {
688 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": received out of order session-accept");
689 return false;
690 }
691 this.mJingleStatus = JINGLE_STATUS_ACCEPTED;
692 mXmppConnectionService.markMessage(message, Message.STATUS_UNSEND);
693 Content content = packet.getJingleContent();
694 if (content.hasSocks5Transport()) {
695 mergeCandidates(JingleCandidate.parse(content.socks5transport().getChildren()));
696 this.connectNextCandidate();
697 return true;
698 } else if (content.hasIbbTransport()) {
699 String receivedBlockSize = packet.getJingleContent().ibbTransport().getAttribute("block-size");
700 if (receivedBlockSize != null) {
701 try {
702 int bs = Integer.parseInt(receivedBlockSize);
703 if (bs > this.ibbBlockSize) {
704 this.ibbBlockSize = bs;
705 }
706 } catch (Exception e) {
707 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": unable to parse block size in session-accept");
708 }
709 }
710 this.transport = new JingleInbandTransport(this, this.transportId, this.ibbBlockSize);
711 this.transport.connect(onIbbTransportConnected);
712 return true;
713 } else {
714 return false;
715 }
716 }
717
718 private boolean receiveTransportInfo(JinglePacket packet) {
719 Content content = packet.getJingleContent();
720 if (content.hasSocks5Transport()) {
721 if (content.socks5transport().hasChild("activated")) {
722 if ((this.transport != null) && (this.transport instanceof JingleSocks5Transport)) {
723 onProxyActivated.success();
724 } else {
725 String cid = content.socks5transport().findChild("activated").getAttribute("cid");
726 Log.d(Config.LOGTAG, "received proxy activated (" + cid
727 + ")prior to choosing our own transport");
728 JingleSocks5Transport connection = this.connections.get(cid);
729 if (connection != null) {
730 connection.setActivated(true);
731 } else {
732 Log.d(Config.LOGTAG, "activated connection not found");
733 this.sendCancel();
734 this.fail();
735 }
736 }
737 return true;
738 } else if (content.socks5transport().hasChild("proxy-error")) {
739 onProxyActivated.failed();
740 return true;
741 } else if (content.socks5transport().hasChild("candidate-error")) {
742 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": received candidate error");
743 this.receivedCandidate = true;
744 if (mJingleStatus == JINGLE_STATUS_ACCEPTED && this.sentCandidate) {
745 this.connect();
746 }
747 return true;
748 } else if (content.socks5transport().hasChild("candidate-used")) {
749 String cid = content.socks5transport().findChild("candidate-used").getAttribute("cid");
750 if (cid != null) {
751 Log.d(Config.LOGTAG, "candidate used by counterpart:" + cid);
752 JingleCandidate candidate = getCandidate(cid);
753 if (candidate == null) {
754 Log.d(Config.LOGTAG, "could not find candidate with cid=" + cid);
755 return false;
756 }
757 candidate.flagAsUsedByCounterpart();
758 this.receivedCandidate = true;
759 if (mJingleStatus == JINGLE_STATUS_ACCEPTED && this.sentCandidate) {
760 this.connect();
761 } else {
762 Log.d(Config.LOGTAG, "ignoring because file is already in transmission or we haven't sent our candidate yet status=" + mJingleStatus + " sentCandidate=" + sentCandidate);
763 }
764 return true;
765 } else {
766 return false;
767 }
768 } else {
769 return false;
770 }
771 } else {
772 return true;
773 }
774 }
775
776 private void connect() {
777 final JingleSocks5Transport connection = chooseConnection();
778 this.transport = connection;
779 if (connection == null) {
780 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": could not find suitable candidate");
781 this.disconnectSocks5Connections();
782 if (initiating()) {
783 this.sendFallbackToIbb();
784 }
785 } else {
786 final JingleCandidate candidate = connection.getCandidate();
787 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": elected candidate " + candidate.getHost() + ":" + candidate.getPort());
788 this.mJingleStatus = JINGLE_STATUS_TRANSMITTING;
789 if (connection.needsActivation()) {
790 if (connection.getCandidate().isOurs()) {
791 final String sid;
792 if (ftVersion == Content.Version.FT_3) {
793 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": use session ID instead of transport ID to activate proxy");
794 sid = getSessionId();
795 } else {
796 sid = getTransportId();
797 }
798 Log.d(Config.LOGTAG, "candidate "
799 + connection.getCandidate().getCid()
800 + " was our proxy. going to activate");
801 IqPacket activation = new IqPacket(IqPacket.TYPE.SET);
802 activation.setTo(connection.getCandidate().getJid());
803 activation.query("http://jabber.org/protocol/bytestreams")
804 .setAttribute("sid", sid);
805 activation.query().addChild("activate")
806 .setContent(this.getCounterPart().toString());
807 mXmppConnectionService.sendIqPacket(account, activation, (account, response) -> {
808 if (response.getType() != IqPacket.TYPE.RESULT) {
809 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": " + response.toString());
810 sendProxyError();
811 onProxyActivated.failed();
812 } else {
813 sendProxyActivated(connection.getCandidate().getCid());
814 onProxyActivated.success();
815 }
816 });
817 } else {
818 Log.d(Config.LOGTAG,
819 "candidate "
820 + connection.getCandidate().getCid()
821 + " was a proxy. waiting for other party to activate");
822 }
823 } else {
824 if (initiating()) {
825 Log.d(Config.LOGTAG, "we were initiating. sending file");
826 connection.send(file, onFileTransmissionStatusChanged);
827 } else {
828 Log.d(Config.LOGTAG, "we were responding. receiving file");
829 connection.receive(file, onFileTransmissionStatusChanged);
830 }
831 }
832 }
833 }
834
835 private JingleSocks5Transport chooseConnection() {
836 JingleSocks5Transport connection = null;
837 for (Entry<String, JingleSocks5Transport> cursor : connections
838 .entrySet()) {
839 JingleSocks5Transport currentConnection = cursor.getValue();
840 // Log.d(Config.LOGTAG,"comparing candidate: "+currentConnection.getCandidate().toString());
841 if (currentConnection.isEstablished()
842 && (currentConnection.getCandidate().isUsedByCounterpart() || (!currentConnection
843 .getCandidate().isOurs()))) {
844 // Log.d(Config.LOGTAG,"is usable");
845 if (connection == null) {
846 connection = currentConnection;
847 } else {
848 if (connection.getCandidate().getPriority() < currentConnection
849 .getCandidate().getPriority()) {
850 connection = currentConnection;
851 } else if (connection.getCandidate().getPriority() == currentConnection
852 .getCandidate().getPriority()) {
853 // Log.d(Config.LOGTAG,"found two candidates with same priority");
854 if (initiating()) {
855 if (currentConnection.getCandidate().isOurs()) {
856 connection = currentConnection;
857 }
858 } else {
859 if (!currentConnection.getCandidate().isOurs()) {
860 connection = currentConnection;
861 }
862 }
863 }
864 }
865 }
866 }
867 return connection;
868 }
869
870 private void sendSuccess() {
871 JinglePacket packet = bootstrapPacket("session-terminate");
872 Reason reason = new Reason();
873 reason.addChild("success");
874 packet.setReason(reason);
875 this.sendJinglePacket(packet);
876 this.disconnectSocks5Connections();
877 this.mJingleStatus = JINGLE_STATUS_FINISHED;
878 this.message.setStatus(Message.STATUS_RECEIVED);
879 this.message.setTransferable(null);
880 this.mXmppConnectionService.updateMessage(message, false);
881 this.mJingleConnectionManager.finishConnection(this);
882 }
883
884 private void sendFallbackToIbb() {
885 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": sending fallback to ibb");
886 JinglePacket packet = this.bootstrapPacket("transport-replace");
887 Content content = new Content(this.contentCreator, this.contentName);
888 this.transportId = this.mJingleConnectionManager.nextRandomId();
889 content.setTransportId(this.transportId);
890 content.ibbTransport().setAttribute("block-size",
891 Integer.toString(this.ibbBlockSize));
892 packet.setContent(content);
893 this.sendJinglePacket(packet);
894 }
895
896
897 private boolean receiveFallbackToIbb(JinglePacket packet) {
898 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": receiving fallback to ibb");
899 final String receivedBlockSize = packet.getJingleContent().ibbTransport().getAttribute("block-size");
900 if (receivedBlockSize != null) {
901 try {
902 final int bs = Integer.parseInt(receivedBlockSize);
903 if (bs < this.ibbBlockSize) {
904 this.ibbBlockSize = bs;
905 }
906 } catch (NumberFormatException e) {
907 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": unable to parse block size in transport-replace");
908 }
909 }
910 this.transportId = packet.getJingleContent().getTransportId();
911 this.transport = new JingleInbandTransport(this, this.transportId, this.ibbBlockSize);
912
913 final JinglePacket answer = bootstrapPacket("transport-accept");
914
915 final Content content = new Content(contentCreator, contentName);
916 content.ibbTransport().setAttribute("block-size", this.ibbBlockSize);
917 content.ibbTransport().setAttribute("sid", this.transportId);
918 answer.setContent(content);
919
920
921 if (initiating()) {
922 this.sendJinglePacket(answer, (account, response) -> {
923 if (response.getType() == IqPacket.TYPE.RESULT) {
924 Log.d(Config.LOGTAG, account.getJid().asBareJid() + " recipient ACKed our transport-accept. creating ibb");
925 transport.connect(onIbbTransportConnected);
926 }
927 });
928 } else {
929 this.transport.receive(file, onFileTransmissionStatusChanged);
930 this.sendJinglePacket(answer);
931 }
932 return true;
933 }
934
935 private boolean receiveTransportAccept(JinglePacket packet) {
936 if (packet.getJingleContent().hasIbbTransport()) {
937 final Element ibbTransport = packet.getJingleContent().ibbTransport();
938 final String receivedBlockSize = ibbTransport.getAttribute("block-size");
939 final String sid = ibbTransport.getAttribute("sid");
940 if (receivedBlockSize != null) {
941 try {
942 int bs = Integer.parseInt(receivedBlockSize);
943 if (bs < this.ibbBlockSize) {
944 this.ibbBlockSize = bs;
945 }
946 } catch (NumberFormatException e) {
947 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": unable to parse block size in transport-accept");
948 }
949 }
950 this.transport = new JingleInbandTransport(this, this.transportId, this.ibbBlockSize);
951
952 if (sid == null || !sid.equals(this.transportId)) {
953 Log.w(Config.LOGTAG,String.format("%s: sid in transport-accept (%s) did not match our sid (%s) ", account.getJid().asBareJid(), sid, transportId));
954 }
955
956 //might be receive instead if we are not initiating
957 if (initiating()) {
958 this.transport.connect(onIbbTransportConnected);
959 } else {
960 this.transport.receive(file, onFileTransmissionStatusChanged);
961 }
962 return true;
963 } else {
964 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": received invalid transport-accept");
965 return false;
966 }
967 }
968
969 private void receiveSuccess() {
970 if (initiating()) {
971 this.mJingleStatus = JINGLE_STATUS_FINISHED;
972 this.mXmppConnectionService.markMessage(this.message, Message.STATUS_SEND_RECEIVED);
973 this.disconnectSocks5Connections();
974 if (this.transport instanceof JingleInbandTransport) {
975 this.transport.disconnect();
976 }
977 this.message.setTransferable(null);
978 this.mJingleConnectionManager.finishConnection(this);
979 } else {
980 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": received session-terminate/success while responding");
981 }
982 }
983
984 @Override
985 public void cancel() {
986 this.cancelled = true;
987 abort();
988 }
989
990 public void abort() {
991 this.disconnectSocks5Connections();
992 if (this.transport instanceof JingleInbandTransport) {
993 this.transport.disconnect();
994 }
995 this.sendCancel();
996 this.mJingleConnectionManager.finishConnection(this);
997 if (responding()) {
998 this.message.setTransferable(new TransferablePlaceholder(Transferable.STATUS_FAILED));
999 if (this.file != null) {
1000 file.delete();
1001 }
1002 this.mJingleConnectionManager.updateConversationUi(true);
1003 } else {
1004 this.mXmppConnectionService.markMessage(this.message, Message.STATUS_SEND_FAILED, cancelled ? Message.ERROR_MESSAGE_CANCELLED : null);
1005 this.message.setTransferable(null);
1006 }
1007 }
1008
1009 private void fail() {
1010 fail(null);
1011 }
1012
1013 private void fail(String errorMessage) {
1014 this.mJingleStatus = JINGLE_STATUS_FAILED;
1015 this.disconnectSocks5Connections();
1016 if (this.transport instanceof JingleInbandTransport) {
1017 this.transport.disconnect();
1018 }
1019 FileBackend.close(mFileInputStream);
1020 FileBackend.close(mFileOutputStream);
1021 if (this.message != null) {
1022 if (responding()) {
1023 this.message.setTransferable(new TransferablePlaceholder(Transferable.STATUS_FAILED));
1024 if (this.file != null) {
1025 file.delete();
1026 }
1027 this.mJingleConnectionManager.updateConversationUi(true);
1028 } else {
1029 this.mXmppConnectionService.markMessage(this.message,
1030 Message.STATUS_SEND_FAILED,
1031 cancelled ? Message.ERROR_MESSAGE_CANCELLED : errorMessage);
1032 this.message.setTransferable(null);
1033 }
1034 }
1035 this.mJingleConnectionManager.finishConnection(this);
1036 }
1037
1038 private void sendCancel() {
1039 JinglePacket packet = bootstrapPacket("session-terminate");
1040 Reason reason = new Reason();
1041 reason.addChild("cancel");
1042 packet.setReason(reason);
1043 this.sendJinglePacket(packet);
1044 }
1045
1046 private void connectNextCandidate() {
1047 for (JingleCandidate candidate : this.candidates) {
1048 if ((!connections.containsKey(candidate.getCid()) && (!candidate
1049 .isOurs()))) {
1050 this.connectWithCandidate(candidate);
1051 return;
1052 }
1053 }
1054 this.sendCandidateError();
1055 }
1056
1057 private void connectWithCandidate(final JingleCandidate candidate) {
1058 final JingleSocks5Transport socksConnection = new JingleSocks5Transport(
1059 this, candidate);
1060 connections.put(candidate.getCid(), socksConnection);
1061 socksConnection.connect(new OnTransportConnected() {
1062
1063 @Override
1064 public void failed() {
1065 Log.d(Config.LOGTAG,
1066 "connection failed with " + candidate.getHost() + ":"
1067 + candidate.getPort());
1068 connectNextCandidate();
1069 }
1070
1071 @Override
1072 public void established() {
1073 Log.d(Config.LOGTAG,
1074 "established connection with " + candidate.getHost()
1075 + ":" + candidate.getPort());
1076 sendCandidateUsed(candidate.getCid());
1077 }
1078 });
1079 }
1080
1081 private void disconnectSocks5Connections() {
1082 Iterator<Entry<String, JingleSocks5Transport>> it = this.connections
1083 .entrySet().iterator();
1084 while (it.hasNext()) {
1085 Entry<String, JingleSocks5Transport> pairs = it.next();
1086 pairs.getValue().disconnect();
1087 it.remove();
1088 }
1089 }
1090
1091 private void sendProxyActivated(String cid) {
1092 final JinglePacket packet = bootstrapPacket("transport-info");
1093 final Content content = new Content(this.contentCreator, this.contentName);
1094 content.setTransportId(this.transportId);
1095 content.socks5transport().addChild("activated").setAttribute("cid", cid);
1096 packet.setContent(content);
1097 this.sendJinglePacket(packet);
1098 }
1099
1100 private void sendProxyError() {
1101 final JinglePacket packet = bootstrapPacket("transport-info");
1102 final Content content = new Content(this.contentCreator, this.contentName);
1103 content.setTransportId(this.transportId);
1104 content.socks5transport().addChild("proxy-error");
1105 packet.setContent(content);
1106 this.sendJinglePacket(packet);
1107 }
1108
1109 private void sendCandidateUsed(final String cid) {
1110 JinglePacket packet = bootstrapPacket("transport-info");
1111 Content content = new Content(this.contentCreator, this.contentName);
1112 content.setTransportId(this.transportId);
1113 content.socks5transport().addChild("candidate-used").setAttribute("cid", cid);
1114 packet.setContent(content);
1115 this.sentCandidate = true;
1116 if ((receivedCandidate) && (mJingleStatus == JINGLE_STATUS_ACCEPTED)) {
1117 connect();
1118 }
1119 this.sendJinglePacket(packet);
1120 }
1121
1122 private void sendCandidateError() {
1123 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": sending candidate error");
1124 JinglePacket packet = bootstrapPacket("transport-info");
1125 Content content = new Content(this.contentCreator, this.contentName);
1126 content.setTransportId(this.transportId);
1127 content.socks5transport().addChild("candidate-error");
1128 packet.setContent(content);
1129 this.sentCandidate = true;
1130 this.sendJinglePacket(packet);
1131 if (receivedCandidate && mJingleStatus == JINGLE_STATUS_ACCEPTED) {
1132 connect();
1133 }
1134 }
1135
1136 public int getJingleStatus() {
1137 return this.mJingleStatus;
1138 }
1139
1140 private boolean equalCandidateExists(JingleCandidate candidate) {
1141 for (JingleCandidate c : this.candidates) {
1142 if (c.equalValues(candidate)) {
1143 return true;
1144 }
1145 }
1146 return false;
1147 }
1148
1149 private void mergeCandidate(JingleCandidate candidate) {
1150 for (JingleCandidate c : this.candidates) {
1151 if (c.equals(candidate)) {
1152 return;
1153 }
1154 }
1155 this.candidates.add(candidate);
1156 }
1157
1158 private void mergeCandidates(List<JingleCandidate> candidates) {
1159 Collections.sort(candidates, (a, b) -> Integer.compare(b.getPriority(), a.getPriority()));
1160 for (JingleCandidate c : candidates) {
1161 mergeCandidate(c);
1162 }
1163 }
1164
1165 private JingleCandidate getCandidate(String cid) {
1166 for (JingleCandidate c : this.candidates) {
1167 if (c.getCid().equals(cid)) {
1168 return c;
1169 }
1170 }
1171 return null;
1172 }
1173
1174 void updateProgress(int i) {
1175 this.mProgress = i;
1176 mJingleConnectionManager.updateConversationUi(false);
1177 }
1178
1179 public String getTransportId() {
1180 return this.transportId;
1181 }
1182
1183 public Content.Version getFtVersion() {
1184 return this.ftVersion;
1185 }
1186
1187 public boolean hasTransportId(String sid) {
1188 return sid.equals(this.transportId);
1189 }
1190
1191 public JingleTransport getTransport() {
1192 return this.transport;
1193 }
1194
1195 public boolean start() {
1196 if (account.getStatus() == Account.State.ONLINE) {
1197 if (mJingleStatus == JINGLE_STATUS_INITIATED) {
1198 new Thread(this::sendAccept).start();
1199 }
1200 return true;
1201 } else {
1202 return false;
1203 }
1204 }
1205
1206 @Override
1207 public int getStatus() {
1208 return this.mStatus;
1209 }
1210
1211 @Override
1212 public long getFileSize() {
1213 if (this.file != null) {
1214 return this.file.getExpectedSize();
1215 } else {
1216 return 0;
1217 }
1218 }
1219
1220 @Override
1221 public int getProgress() {
1222 return this.mProgress;
1223 }
1224
1225 public AbstractConnectionManager getConnectionManager() {
1226 return this.mJingleConnectionManager;
1227 }
1228
1229 interface OnProxyActivated {
1230 void success();
1231
1232 void failed();
1233 }
1234}