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