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