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