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