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