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