JingleConnection.java

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