JingleFileTransferConnection.java

   1package eu.siacs.conversations.xmpp.jingle;
   2
   3import android.util.Base64;
   4import android.util.Log;
   5
   6import com.google.common.base.Preconditions;
   7import com.google.common.base.Strings;
   8import com.google.common.collect.Collections2;
   9import com.google.common.collect.FluentIterable;
  10import com.google.common.collect.Iterables;
  11
  12import java.io.File;
  13import java.io.FileInputStream;
  14import java.io.FileNotFoundException;
  15import java.io.IOException;
  16import java.io.InputStream;
  17import java.io.OutputStream;
  18import java.util.ArrayList;
  19import java.util.Arrays;
  20import java.util.Collection;
  21import java.util.Collections;
  22import java.util.Iterator;
  23import java.util.List;
  24import java.util.Map.Entry;
  25import java.util.concurrent.ConcurrentHashMap;
  26
  27import eu.siacs.conversations.Config;
  28import eu.siacs.conversations.crypto.axolotl.AxolotlService;
  29import eu.siacs.conversations.crypto.axolotl.XmppAxolotlMessage;
  30import eu.siacs.conversations.entities.Account;
  31import eu.siacs.conversations.entities.Conversation;
  32import eu.siacs.conversations.entities.DownloadableFile;
  33import eu.siacs.conversations.entities.Message;
  34import eu.siacs.conversations.entities.Presence;
  35import eu.siacs.conversations.entities.ServiceDiscoveryResult;
  36import eu.siacs.conversations.entities.Transferable;
  37import eu.siacs.conversations.entities.TransferablePlaceholder;
  38import eu.siacs.conversations.parser.IqParser;
  39import eu.siacs.conversations.persistance.FileBackend;
  40import eu.siacs.conversations.services.AbstractConnectionManager;
  41import eu.siacs.conversations.services.XmppConnectionService;
  42import eu.siacs.conversations.utils.CryptoHelper;
  43import eu.siacs.conversations.utils.MimeUtils;
  44import eu.siacs.conversations.xml.Element;
  45import eu.siacs.conversations.xml.Namespace;
  46import eu.siacs.conversations.xmpp.Jid;
  47import eu.siacs.conversations.xmpp.OnIqPacketReceived;
  48import eu.siacs.conversations.xmpp.jingle.stanzas.Content;
  49import eu.siacs.conversations.xmpp.jingle.stanzas.FileTransferDescription;
  50import eu.siacs.conversations.xmpp.jingle.stanzas.GenericTransportInfo;
  51import eu.siacs.conversations.xmpp.jingle.stanzas.IbbTransportInfo;
  52import eu.siacs.conversations.xmpp.jingle.stanzas.JinglePacket;
  53import eu.siacs.conversations.xmpp.jingle.stanzas.Reason;
  54import eu.siacs.conversations.xmpp.jingle.stanzas.S5BTransportInfo;
  55import eu.siacs.conversations.xmpp.stanzas.IqPacket;
  56
  57public class JingleFileTransferConnection extends AbstractJingleConnection implements Transferable {
  58
  59    private static final int JINGLE_STATUS_TRANSMITTING = 5;
  60    private static final String JET_OMEMO_CIPHER = "urn:xmpp:ciphers:aes-128-gcm-nopadding";
  61    private static final int JINGLE_STATUS_INITIATED = 0;
  62    private static final int JINGLE_STATUS_ACCEPTED = 1;
  63    private static final int JINGLE_STATUS_FINISHED = 4;
  64    private static final int JINGLE_STATUS_FAILED = 99;
  65    private static final int JINGLE_STATUS_OFFERED = -1;
  66
  67    private static final int MAX_IBB_BLOCK_SIZE = 8192;
  68
  69    private int ibbBlockSize = MAX_IBB_BLOCK_SIZE;
  70
  71    private int mJingleStatus = JINGLE_STATUS_OFFERED; //migrate to enum
  72    private int mStatus = Transferable.STATUS_UNKNOWN;
  73    private Message message;
  74    private Jid responder;
  75    private final List<JingleCandidate> candidates = new ArrayList<>();
  76    private final ConcurrentHashMap<String, JingleSocks5Transport> connections = new ConcurrentHashMap<>();
  77
  78    private String transportId;
  79    private FileTransferDescription description;
  80    private DownloadableFile file = null;
  81
  82    private boolean proxyActivationFailed = false;
  83
  84    private String contentName;
  85    private Content.Creator contentCreator;
  86    private Content.Senders contentSenders;
  87    private Class<? extends GenericTransportInfo> initialTransport;
  88    private boolean remoteSupportsOmemoJet;
  89
  90    private int mProgress = 0;
  91
  92    private boolean receivedCandidate = false;
  93    private boolean sentCandidate = false;
  94
  95    private boolean acceptedAutomatically = false;
  96    private boolean cancelled = false;
  97
  98    private XmppAxolotlMessage mXmppAxolotlMessage;
  99
 100    private JingleTransport transport = null;
 101
 102    private OutputStream mFileOutputStream;
 103    private InputStream mFileInputStream;
 104
 105    private final OnIqPacketReceived responseListener = (account, packet) -> {
 106        if (packet.getType() != IqPacket.TYPE.RESULT) {
 107            if (mJingleStatus != JINGLE_STATUS_FAILED && mJingleStatus != JINGLE_STATUS_FINISHED) {
 108                fail(IqParser.extractErrorMessage(packet));
 109            } else {
 110                Log.d(Config.LOGTAG, "ignoring late delivery of jingle packet to jingle session with status=" + mJingleStatus + ": " + packet.toString());
 111            }
 112        }
 113    };
 114    private byte[] expectedHash = new byte[0];
 115    private final OnFileTransmissionStatusChanged onFileTransmissionStatusChanged = new OnFileTransmissionStatusChanged() {
 116
 117        @Override
 118        public void onFileTransmitted(DownloadableFile file) {
 119            DownloadableFile finalFile;
 120
 121            if (responding()) {
 122                if (expectedHash.length > 0) {
 123                    if (Arrays.equals(expectedHash, file.getSha1Sum())) {
 124                        Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": received file matched the expected hash");
 125                    } else {
 126                        Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": hashes did not match");
 127                    }
 128                } else {
 129                    Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": other party did not include file hash in file transfer");
 130                }
 131                sendSuccess();
 132
 133                final String extension = MimeUtils.extractRelevantExtension(file.getName());
 134                try {
 135                    xmppConnectionService.getFileBackend().setupRelativeFilePath(message, new FileInputStream(file), extension);
 136                    finalFile = xmppConnectionService.getFileBackend().getFile(message);
 137                    boolean didRename = file.renameTo(finalFile);
 138                    if (!didRename) throw new IOException("rename failed");
 139                } catch (final IOException e) {
 140                    finalFile = file;
 141                    message.setRelativeFilePath(finalFile.getAbsolutePath());
 142                } catch (final XmppConnectionService.BlockedMediaException e) {
 143                    finalFile = file;
 144                    file.delete();
 145                    message.setRelativeFilePath(null);
 146                    message.setDeleted(true);
 147                }
 148
 149                xmppConnectionService.getFileBackend().updateFileParams(message, null, false);
 150                xmppConnectionService.databaseBackend.createMessage(message);
 151                xmppConnectionService.markMessage(message, Message.STATUS_RECEIVED);
 152                if (acceptedAutomatically) {
 153                    message.markUnread();
 154                    if (message.getEncryption() == Message.ENCRYPTION_PGP) {
 155                        id.account.getPgpDecryptionService().decrypt(message, true);
 156                    } else {
 157                        xmppConnectionService.getFileBackend().updateMediaScanner(file, () -> JingleFileTransferConnection.this.xmppConnectionService.getNotificationService().push(message));
 158
 159                    }
 160                    Log.d(Config.LOGTAG, "successfully transmitted file:" + file.getAbsolutePath() + " (" + CryptoHelper.bytesToHex(file.getSha1Sum()) + ")");
 161                    return;
 162                } else if (message.getEncryption() == Message.ENCRYPTION_PGP) {
 163                    id.account.getPgpDecryptionService().decrypt(message, true);
 164                }
 165            } else {
 166                finalFile = file;
 167
 168                if (description.getVersion() == FileTransferDescription.Version.FT_5) { //older Conversations will break when receiving a session-info
 169                    sendHash();
 170                }
 171                if (message.getEncryption() == Message.ENCRYPTION_PGP) {
 172                    id.account.getPgpDecryptionService().decrypt(message, false);
 173                }
 174                if (message.getEncryption() == Message.ENCRYPTION_PGP || message.getEncryption() == Message.ENCRYPTION_DECRYPTED) {
 175                    finalFile.delete();
 176                }
 177                disconnectSocks5Connections();
 178            }
 179            Log.d(Config.LOGTAG, "successfully transmitted file:" + finalFile.getAbsolutePath() + " (" + CryptoHelper.bytesToHex(file.getSha1Sum()) + ")");
 180            if (message.getEncryption() != Message.ENCRYPTION_PGP) {
 181                xmppConnectionService.getFileBackend().updateMediaScanner(finalFile);
 182            }
 183        }
 184
 185        @Override
 186        public void onFileTransferAborted() {
 187            JingleFileTransferConnection.this.sendSessionTerminate(Reason.CONNECTIVITY_ERROR);
 188            JingleFileTransferConnection.this.fail();
 189        }
 190    };
 191    private final OnTransportConnected onIbbTransportConnected = new OnTransportConnected() {
 192        @Override
 193        public void failed() {
 194            Log.d(Config.LOGTAG, "ibb open failed");
 195        }
 196
 197        @Override
 198        public void established() {
 199            Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": ibb transport connected. sending file");
 200            mJingleStatus = JINGLE_STATUS_TRANSMITTING;
 201            JingleFileTransferConnection.this.transport.send(file, onFileTransmissionStatusChanged);
 202        }
 203    };
 204    private final OnProxyActivated onProxyActivated = new OnProxyActivated() {
 205
 206        @Override
 207        public void success() {
 208            if (isInitiator()) {
 209                Log.d(Config.LOGTAG, "we were initiating. sending file");
 210                transport.send(file, onFileTransmissionStatusChanged);
 211            } else {
 212                transport.receive(file, onFileTransmissionStatusChanged);
 213                Log.d(Config.LOGTAG, "we were responding. receiving file");
 214            }
 215        }
 216
 217        @Override
 218        public void failed() {
 219            Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": proxy activation failed");
 220            proxyActivationFailed = true;
 221            if (isInitiator()) {
 222                sendFallbackToIbb();
 223            }
 224        }
 225    };
 226
 227    JingleFileTransferConnection(JingleConnectionManager jingleConnectionManager, Id id, Jid initiator) {
 228        super(jingleConnectionManager, id, initiator);
 229    }
 230
 231    private static long parseLong(final Element element, final long l) {
 232        final String input = element == null ? null : element.getContent();
 233        if (input == null) {
 234            return l;
 235        }
 236        try {
 237            return Long.parseLong(input);
 238        } catch (Exception e) {
 239            return l;
 240        }
 241    }
 242
 243    //TODO get rid and use isInitiator() instead
 244    private boolean responding() {
 245        return responder != null && responder.equals(id.account.getJid());
 246    }
 247
 248
 249    InputStream getFileInputStream() {
 250        return this.mFileInputStream;
 251    }
 252
 253    OutputStream getFileOutputStream() throws IOException {
 254        if (this.file == null) {
 255            Log.d(Config.LOGTAG, "file object was not assigned");
 256            return null;
 257        }
 258        final File parent = this.file.getParentFile();
 259        if (parent != null && parent.mkdirs()) {
 260            Log.d(Config.LOGTAG, "created parent directories for file " + file.getAbsolutePath());
 261        }
 262        if (this.file.createNewFile()) {
 263            Log.d(Config.LOGTAG, "created output file " + file.getAbsolutePath());
 264        }
 265        this.mFileOutputStream = AbstractConnectionManager.createOutputStream(this.file, false, true);
 266        return this.mFileOutputStream;
 267    }
 268
 269    @Override
 270    void deliverPacket(final JinglePacket packet) {
 271        final JinglePacket.Action action = packet.getAction();
 272        //TODO switch case
 273        if (action == JinglePacket.Action.SESSION_INITIATE) {
 274            init(packet);
 275        } else if (action == JinglePacket.Action.SESSION_TERMINATE) {
 276            final Reason reason = packet.getReason().reason;
 277            switch (reason) {
 278                case CANCEL:
 279                    this.cancelled = true;
 280                    this.fail();
 281                    break;
 282                case SUCCESS:
 283                    this.receiveSuccess();
 284                    break;
 285                default:
 286                    Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": received session-terminate with reason " + reason);
 287                    this.fail();
 288                    break;
 289
 290            }
 291        } else if (action == JinglePacket.Action.SESSION_ACCEPT) {
 292            receiveAccept(packet);
 293        } else if (action == JinglePacket.Action.SESSION_INFO) {
 294            final Element checksum = packet.getJingleChild("checksum");
 295            final Element file = checksum == null ? null : checksum.findChild("file");
 296            final Element hash = file == null ? null : file.findChild("hash", "urn:xmpp:hashes:2");
 297            if (hash != null && "sha-1".equalsIgnoreCase(hash.getAttribute("algo"))) {
 298                try {
 299                    this.expectedHash = Base64.decode(hash.getContent(), Base64.DEFAULT);
 300                } catch (Exception e) {
 301                    this.expectedHash = new byte[0];
 302                }
 303            }
 304            respondToIq(packet, true);
 305        } else if (action == JinglePacket.Action.TRANSPORT_INFO) {
 306            receiveTransportInfo(packet);
 307        } else if (action == JinglePacket.Action.TRANSPORT_REPLACE) {
 308            final Content content = packet.getJingleContent();
 309            final GenericTransportInfo transportInfo = content == null ? null : content.getTransport();
 310            if (transportInfo instanceof IbbTransportInfo) {
 311                receiveFallbackToIbb(packet, (IbbTransportInfo) transportInfo);
 312            } else {
 313                Log.d(Config.LOGTAG, "trying to fallback to something unknown" + packet.toString());
 314                respondToIq(packet, false);
 315            }
 316        } else if (action == JinglePacket.Action.TRANSPORT_ACCEPT) {
 317            receiveTransportAccept(packet);
 318        } else {
 319            Log.d(Config.LOGTAG, "packet arrived in connection. action was " + packet.getAction());
 320            respondToIq(packet, false);
 321        }
 322    }
 323
 324    @Override
 325    void notifyRebound() {
 326        if (getJingleStatus() == JINGLE_STATUS_TRANSMITTING) {
 327            abort(Reason.CONNECTIVITY_ERROR);
 328        }
 329    }
 330
 331    private void respondToIq(final IqPacket packet, final boolean result) {
 332        final IqPacket response;
 333        if (result) {
 334            response = packet.generateResponse(IqPacket.TYPE.RESULT);
 335        } else {
 336            response = packet.generateResponse(IqPacket.TYPE.ERROR);
 337            final Element error = response.addChild("error").setAttribute("type", "cancel");
 338            error.addChild("not-acceptable", "urn:ietf:params:xml:ns:xmpp-stanzas");
 339        }
 340        xmppConnectionService.sendIqPacket(id.account, response, null);
 341    }
 342
 343    private void respondToIqWithOutOfOrder(final IqPacket packet) {
 344        final IqPacket response = packet.generateResponse(IqPacket.TYPE.ERROR);
 345        final Element error = response.addChild("error").setAttribute("type", "wait");
 346        error.addChild("unexpected-request", "urn:ietf:params:xml:ns:xmpp-stanzas");
 347        error.addChild("out-of-order", "urn:xmpp:jingle:errors:1");
 348        xmppConnectionService.sendIqPacket(id.account, response, null);
 349    }
 350
 351    public void init(final Message message) {
 352        Preconditions.checkArgument(message.isFileOrImage());
 353        if (message.getEncryption() == Message.ENCRYPTION_AXOLOTL) {
 354            Conversation conversation = (Conversation) message.getConversation();
 355            conversation.getAccount().getAxolotlService().prepareKeyTransportMessage(conversation, xmppAxolotlMessage -> {
 356                if (xmppAxolotlMessage != null) {
 357                    init(message, xmppAxolotlMessage);
 358                } else {
 359                    fail();
 360                }
 361            });
 362        } else {
 363            init(message, null);
 364        }
 365    }
 366
 367    private void init(final Message message, final XmppAxolotlMessage xmppAxolotlMessage) {
 368        this.mXmppAxolotlMessage = xmppAxolotlMessage;
 369        this.contentCreator = Content.Creator.INITIATOR;
 370        this.contentSenders = Content.Senders.INITIATOR;
 371        this.contentName = JingleConnectionManager.nextRandomId();
 372        this.message = message;
 373        final List<String> remoteFeatures = getRemoteFeatures();
 374        final FileTransferDescription.Version remoteVersion = getAvailableFileTransferVersion(remoteFeatures);
 375        this.initialTransport = remoteFeatures.contains(Namespace.JINGLE_TRANSPORTS_S5B) ? S5BTransportInfo.class : IbbTransportInfo.class;
 376        this.remoteSupportsOmemoJet = remoteFeatures.contains(Namespace.JINGLE_ENCRYPTED_TRANSPORT_OMEMO);
 377        this.message.setTransferable(this);
 378        this.mStatus = Transferable.STATUS_UPLOADING;
 379        this.responder = this.id.with;
 380        this.transportId = JingleConnectionManager.nextRandomId();
 381        this.setupDescription(remoteVersion);
 382        if (this.initialTransport == IbbTransportInfo.class) {
 383            this.sendInitRequest();
 384        } else {
 385            gatherAndConnectDirectCandidates();
 386            this.jingleConnectionManager.getPrimaryCandidate(id.account, isInitiator(), (success, candidate) -> {
 387                if (success) {
 388                    final JingleSocks5Transport socksConnection = new JingleSocks5Transport(this, candidate);
 389                    connections.put(candidate.getCid(), socksConnection);
 390                    socksConnection.connect(new OnTransportConnected() {
 391
 392                        @Override
 393                        public void failed() {
 394                            Log.d(Config.LOGTAG, String.format("connection to our own proxy65 candidate failed (%s:%d)", candidate.getHost(), candidate.getPort()));
 395                            sendInitRequest();
 396                        }
 397
 398                        @Override
 399                        public void established() {
 400                            Log.d(Config.LOGTAG, "successfully connected to our own proxy65 candidate");
 401                            mergeCandidate(candidate);
 402                            sendInitRequest();
 403                        }
 404                    });
 405                    mergeCandidate(candidate);
 406                } else {
 407                    Log.d(Config.LOGTAG, "no proxy65 candidate of our own was found");
 408                    sendInitRequest();
 409                }
 410            });
 411        }
 412
 413    }
 414
 415    private void gatherAndConnectDirectCandidates() {
 416        final List<JingleCandidate> directCandidates;
 417        if (Config.USE_DIRECT_JINGLE_CANDIDATES) {
 418            if (id.account.isOnion() || xmppConnectionService.useTorToConnect()) {
 419                directCandidates = Collections.emptyList();
 420            } else {
 421                directCandidates = DirectConnectionUtils.getLocalCandidates(id.account.getJid());
 422            }
 423        } else {
 424            directCandidates = Collections.emptyList();
 425        }
 426        for (JingleCandidate directCandidate : directCandidates) {
 427            final JingleSocks5Transport socksConnection = new JingleSocks5Transport(this, directCandidate);
 428            connections.put(directCandidate.getCid(), socksConnection);
 429            candidates.add(directCandidate);
 430        }
 431    }
 432
 433    private FileTransferDescription.Version getAvailableFileTransferVersion(List<String> remoteFeatures) {
 434        if (remoteFeatures.contains(FileTransferDescription.Version.FT_5.getNamespace())) {
 435            return FileTransferDescription.Version.FT_5;
 436        } else if (remoteFeatures.contains(FileTransferDescription.Version.FT_4.getNamespace())) {
 437            return FileTransferDescription.Version.FT_4;
 438        } else {
 439            return FileTransferDescription.Version.FT_3;
 440        }
 441    }
 442
 443    private List<String> getRemoteFeatures() {
 444        final String resource = Strings.nullToEmpty(this.id.with.getResource());
 445        final Presence presence = this.id.account.getRoster().getContact(id.with).getPresences().get(resource);
 446        final ServiceDiscoveryResult result = presence != null ? presence.getServiceDiscoveryResult() : null;
 447        return result == null ? Collections.emptyList() : result.getFeatures();
 448    }
 449
 450    private void init(JinglePacket packet) { //should move to deliverPacket
 451        //TODO if not 'OFFERED' reply with out-of-order
 452        this.mJingleStatus = JINGLE_STATUS_INITIATED;
 453        final Conversation conversation = this.xmppConnectionService.findOrCreateConversation(id.account, id.with.asBareJid(), false, false);
 454        this.message = new Message(conversation, "", Message.ENCRYPTION_NONE);
 455        this.message.setStatus(Message.STATUS_RECEIVED);
 456        this.mStatus = Transferable.STATUS_OFFER;
 457        this.message.setTransferable(this);
 458        this.message.setCounterpart(this.id.with);
 459        this.responder = this.id.account.getJid();
 460        final Content content = packet.getJingleContent();
 461        final GenericTransportInfo transportInfo = content.getTransport();
 462        this.contentCreator = content.getCreator();
 463        Content.Senders senders;
 464        try {
 465            senders = content.getSenders();
 466        } catch (final Exception e) {
 467            senders = Content.Senders.INITIATOR;
 468        }
 469        this.contentSenders = senders;
 470        this.contentName = content.getAttribute("name");
 471
 472        if (transportInfo instanceof S5BTransportInfo) {
 473            final S5BTransportInfo s5BTransportInfo = (S5BTransportInfo) transportInfo;
 474            this.transportId = s5BTransportInfo.getTransportId();
 475            this.initialTransport = s5BTransportInfo.getClass();
 476            this.mergeCandidates(s5BTransportInfo.getCandidates());
 477        } else if (transportInfo instanceof IbbTransportInfo) {
 478            final IbbTransportInfo ibbTransportInfo = (IbbTransportInfo) transportInfo;
 479            this.initialTransport = ibbTransportInfo.getClass();
 480            this.transportId = ibbTransportInfo.getTransportId();
 481            final int remoteBlockSize = ibbTransportInfo.getBlockSize();
 482            if (remoteBlockSize <= 0) {
 483                Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": remote party requested invalid ibb block size");
 484                respondToIq(packet, false);
 485                this.fail();
 486            }
 487            this.ibbBlockSize = Math.min(MAX_IBB_BLOCK_SIZE, ibbTransportInfo.getBlockSize());
 488        } else {
 489            Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": remote tried to use unknown transport " + transportInfo.getNamespace());
 490            respondToIq(packet, false);
 491            this.fail();
 492            return;
 493        }
 494
 495        this.description = (FileTransferDescription) content.getDescription();
 496
 497        final Element fileOffer = this.description.getFileOffer();
 498
 499        if (fileOffer != null) {
 500            boolean remoteIsUsingJet = false;
 501            Element encrypted = fileOffer.findChild("encrypted", AxolotlService.PEP_PREFIX);
 502            if (encrypted == null) {
 503                final Element security = content.findChild("security", Namespace.JINGLE_ENCRYPTED_TRANSPORT);
 504                if (security != null && AxolotlService.PEP_PREFIX.equals(security.getAttribute("type"))) {
 505                    Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": received jingle file offer with JET");
 506                    encrypted = security.findChild("encrypted", AxolotlService.PEP_PREFIX);
 507                    remoteIsUsingJet = true;
 508                }
 509            }
 510            if (encrypted != null) {
 511                this.mXmppAxolotlMessage = XmppAxolotlMessage.fromElement(encrypted, packet.getFrom().asBareJid());
 512            }
 513            Element fileSize = fileOffer.findChild("size");
 514            final String path = fileOffer.findChildContent("name");
 515            if (path != null) {
 516                AbstractConnectionManager.Extension extension = AbstractConnectionManager.Extension.of(path);
 517                if (VALID_IMAGE_EXTENSIONS.contains(extension.main)) {
 518                    message.setType(Message.TYPE_IMAGE);
 519                    xmppConnectionService.getFileBackend().setupRelativeFilePath(message, message.getUuid() + "." + extension.main);
 520                } else if (VALID_CRYPTO_EXTENSIONS.contains(extension.main)) {
 521                    if (VALID_IMAGE_EXTENSIONS.contains(extension.secondary)) {
 522                        message.setType(Message.TYPE_IMAGE);
 523                        xmppConnectionService.getFileBackend().setupRelativeFilePath(message,message.getUuid() + "." + extension.secondary);
 524                    } else {
 525                        message.setType(Message.TYPE_FILE);
 526                        xmppConnectionService.getFileBackend().setupRelativeFilePath(message,message.getUuid() + (extension.secondary != null ? ("." + extension.secondary) : ""));
 527                    }
 528                    message.setEncryption(Message.ENCRYPTION_PGP);
 529                } else {
 530                    message.setType(Message.TYPE_FILE);
 531                    xmppConnectionService.getFileBackend().setupRelativeFilePath(message,message.getUuid() + (extension.main != null ? ("." + extension.main) : ""));
 532                }
 533                long size = parseLong(fileSize, 0);
 534                Message.FileParams fp = new Message.FileParams();
 535                fp.size = new Long(size);
 536                message.setFileParams(fp);
 537                conversation.add(message);
 538                jingleConnectionManager.updateConversationUi(true);
 539                this.file = this.xmppConnectionService.getFileBackend().getFile(message, false);
 540                if (mXmppAxolotlMessage != null) {
 541                    XmppAxolotlMessage.XmppAxolotlKeyTransportMessage transportMessage = id.account.getAxolotlService().processReceivingKeyTransportMessage(mXmppAxolotlMessage, false);
 542                    if (transportMessage != null) {
 543                        message.setEncryption(Message.ENCRYPTION_AXOLOTL);
 544                        this.file.setKey(transportMessage.getKey());
 545                        this.file.setIv(transportMessage.getIv());
 546                        message.setFingerprint(transportMessage.getFingerprint());
 547                    } else {
 548                        Log.d(Config.LOGTAG, "could not process KeyTransportMessage");
 549                    }
 550                }
 551                //legacy OMEMO encrypted file transfers reported the file size after encryption
 552                //JET reports the plain text size. however lower levels of our receiving code still
 553                //expect the cipher text size. so we just + 16 bytes (auth tag size) here
 554                this.file.setExpectedSize(size + (remoteIsUsingJet ? 16 : 0));
 555
 556                respondToIq(packet, true);
 557
 558                if (id.account.getRoster().getContact(id.with).showInContactList()
 559                        && jingleConnectionManager.hasStoragePermission()
 560                        && size < this.jingleConnectionManager.getAutoAcceptFileSize()
 561                        && xmppConnectionService.isDataSaverDisabled()) {
 562                    Log.d(Config.LOGTAG, "auto accepting file from " + id.with);
 563                    this.acceptedAutomatically = true;
 564                    this.sendAccept();
 565                } else {
 566                    message.markUnread();
 567                    Log.d(Config.LOGTAG,
 568                            "not auto accepting new file offer with size: "
 569                                    + size
 570                                    + " allowed size:"
 571                                    + this.jingleConnectionManager
 572                                    .getAutoAcceptFileSize());
 573                    this.xmppConnectionService.getNotificationService().push(message);
 574                }
 575                Log.d(Config.LOGTAG, "receiving file: expecting size of " + this.file.getExpectedSize());
 576                return;
 577            }
 578            respondToIq(packet, false);
 579        }
 580    }
 581
 582    private void setupDescription(final FileTransferDescription.Version version) {
 583        this.file = this.xmppConnectionService.getFileBackend().getFile(message, false);
 584        final FileTransferDescription description;
 585        if (message.getEncryption() == Message.ENCRYPTION_AXOLOTL) {
 586            this.file.setKey(mXmppAxolotlMessage.getInnerKey());
 587            this.file.setIv(mXmppAxolotlMessage.getIV());
 588            //legacy OMEMO encrypted file transfer reported file size of the encrypted file
 589            //JET uses the file size of the plain text file. The difference is only 16 bytes (auth tag)
 590            this.file.setExpectedSize(file.getSize() + (this.remoteSupportsOmemoJet ? 0 : 16));
 591            if (remoteSupportsOmemoJet) {
 592                description = FileTransferDescription.of(this.file, version, null);
 593            } else {
 594                description = FileTransferDescription.of(this.file, version, this.mXmppAxolotlMessage);
 595            }
 596        } else {
 597            this.file.setExpectedSize(file.getSize());
 598            description = FileTransferDescription.of(this.file, version, null);
 599        }
 600        this.description = description;
 601    }
 602
 603    private void sendInitRequest() {
 604        final JinglePacket packet = this.bootstrapPacket(JinglePacket.Action.SESSION_INITIATE);
 605        final Content content = new Content(this.contentCreator, this.contentSenders, this.contentName);
 606        if (message.getEncryption() == Message.ENCRYPTION_AXOLOTL && remoteSupportsOmemoJet) {
 607            Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": remote announced support for JET");
 608            final Element security = new Element("security", Namespace.JINGLE_ENCRYPTED_TRANSPORT);
 609            security.setAttribute("name", this.contentName);
 610            security.setAttribute("cipher", JET_OMEMO_CIPHER);
 611            security.setAttribute("type", AxolotlService.PEP_PREFIX);
 612            security.addChild(mXmppAxolotlMessage.toElement());
 613            content.addChild(security);
 614        }
 615        content.setDescription(this.description);
 616        try {
 617            this.mFileInputStream = new FileInputStream(file);
 618        } catch (FileNotFoundException e) {
 619            fail(e.getMessage());
 620            return;
 621        }
 622        if (this.initialTransport == IbbTransportInfo.class) {
 623            content.setTransport(new IbbTransportInfo(this.transportId, this.ibbBlockSize));
 624            Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": sending IBB offer");
 625        } else {
 626            final Collection<JingleCandidate> candidates = getOurCandidates();
 627            content.setTransport(new S5BTransportInfo(this.transportId, candidates));
 628            Log.d(Config.LOGTAG, String.format("%s: sending S5B offer with %d candidates", id.account.getJid().asBareJid(), candidates.size()));
 629        }
 630        packet.addJingleContent(content);
 631        this.sendJinglePacket(packet, (account, response) -> {
 632            if (response.getType() == IqPacket.TYPE.RESULT) {
 633                Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": other party received offer");
 634                if (mJingleStatus == JINGLE_STATUS_OFFERED) {
 635                    mJingleStatus = JINGLE_STATUS_INITIATED;
 636                    xmppConnectionService.markMessage(message, Message.STATUS_OFFERED);
 637                } else {
 638                    Log.d(Config.LOGTAG, "received ack for offer when status was " + mJingleStatus);
 639                }
 640            } else {
 641                fail(IqParser.extractErrorMessage(response));
 642            }
 643        });
 644
 645    }
 646
 647    private void sendHash() {
 648        final Element checksum = new Element("checksum", description.getVersion().getNamespace());
 649        checksum.setAttribute("creator", "initiator");
 650        checksum.setAttribute("name", "a-file-offer");
 651        Element hash = checksum.addChild("file").addChild("hash", "urn:xmpp:hashes:2");
 652        hash.setAttribute("algo", "sha-1").setContent(Base64.encodeToString(file.getSha1Sum(), Base64.NO_WRAP));
 653
 654        final JinglePacket packet = this.bootstrapPacket(JinglePacket.Action.SESSION_INFO);
 655        packet.addJingleChild(checksum);
 656        xmppConnectionService.sendIqPacket(id.account, packet, (account, response) -> {
 657            if (response.getType() == IqPacket.TYPE.ERROR) {
 658                Log.d(Config.LOGTAG,account.getJid().asBareJid()+": ignoring error response to our session-info (hash transmission)");
 659            }
 660        });
 661    }
 662
 663    private Collection<JingleCandidate> getOurCandidates() {
 664        return Collections2.filter(this.candidates, c -> c != null && c.isOurs());
 665    }
 666
 667    private void sendAccept() {
 668        mJingleStatus = JINGLE_STATUS_ACCEPTED;
 669        this.mStatus = Transferable.STATUS_DOWNLOADING;
 670        this.jingleConnectionManager.updateConversationUi(true);
 671        if (initialTransport == S5BTransportInfo.class) {
 672            sendAcceptSocks();
 673        } else {
 674            sendAcceptIbb();
 675        }
 676    }
 677
 678    private void sendAcceptSocks() {
 679        gatherAndConnectDirectCandidates();
 680        this.jingleConnectionManager.getPrimaryCandidate(this.id.account, isInitiator(), (success, candidate) -> {
 681            final JinglePacket packet = bootstrapPacket(JinglePacket.Action.SESSION_ACCEPT);
 682            final Content content = new Content(contentCreator, contentSenders, contentName);
 683            content.setDescription(this.description);
 684            if (success && candidate != null && !equalCandidateExists(candidate)) {
 685                final JingleSocks5Transport socksConnection = new JingleSocks5Transport(this, candidate);
 686                connections.put(candidate.getCid(), socksConnection);
 687                socksConnection.connect(new OnTransportConnected() {
 688
 689                    @Override
 690                    public void failed() {
 691                        Log.d(Config.LOGTAG, "connection to our own proxy65 candidate failed");
 692                        content.setTransport(new S5BTransportInfo(transportId, getOurCandidates()));
 693                        packet.addJingleContent(content);
 694                        sendJinglePacket(packet);
 695                        connectNextCandidate();
 696                    }
 697
 698                    @Override
 699                    public void established() {
 700                        Log.d(Config.LOGTAG, "connected to proxy65 candidate");
 701                        mergeCandidate(candidate);
 702                        content.setTransport(new S5BTransportInfo(transportId, getOurCandidates()));
 703                        packet.addJingleContent(content);
 704                        sendJinglePacket(packet);
 705                        connectNextCandidate();
 706                    }
 707                });
 708            } else {
 709                Log.d(Config.LOGTAG, "did not find a proxy65 candidate for ourselves");
 710                content.setTransport(new S5BTransportInfo(transportId, getOurCandidates()));
 711                packet.addJingleContent(content);
 712                sendJinglePacket(packet);
 713                connectNextCandidate();
 714            }
 715        });
 716    }
 717
 718    private void sendAcceptIbb() {
 719        this.transport = new JingleInBandTransport(this, this.transportId, this.ibbBlockSize);
 720        final JinglePacket packet = bootstrapPacket(JinglePacket.Action.SESSION_ACCEPT);
 721        final Content content = new Content(contentCreator, contentSenders, contentName);
 722        content.setDescription(this.description);
 723        content.setTransport(new IbbTransportInfo(this.transportId, this.ibbBlockSize));
 724        packet.addJingleContent(content);
 725        this.transport.receive(file, onFileTransmissionStatusChanged);
 726        this.sendJinglePacket(packet);
 727    }
 728
 729    private JinglePacket bootstrapPacket(JinglePacket.Action action) {
 730        final JinglePacket packet = new JinglePacket(action, this.id.sessionId);
 731        packet.setTo(id.with);
 732        return packet;
 733    }
 734
 735    private void sendJinglePacket(JinglePacket packet) {
 736        xmppConnectionService.sendIqPacket(id.account, packet, responseListener);
 737    }
 738
 739    private void sendJinglePacket(JinglePacket packet, OnIqPacketReceived callback) {
 740        xmppConnectionService.sendIqPacket(id.account, packet, callback);
 741    }
 742
 743    private void receiveAccept(JinglePacket packet) {
 744        if (responding()) {
 745            Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": received out of order session-accept (we were responding)");
 746            respondToIqWithOutOfOrder(packet);
 747            return;
 748        }
 749        if (this.mJingleStatus != JINGLE_STATUS_INITIATED) {
 750            Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": received out of order session-accept");
 751            respondToIqWithOutOfOrder(packet);
 752            return;
 753        }
 754        this.mJingleStatus = JINGLE_STATUS_ACCEPTED;
 755        xmppConnectionService.markMessage(message, Message.STATUS_UNSEND);
 756        final Content content = packet.getJingleContent();
 757        final GenericTransportInfo transportInfo = content.getTransport();
 758        //TODO we want to fail if transportInfo doesn’t match our intialTransport and/or our id
 759        if (transportInfo instanceof S5BTransportInfo) {
 760            final S5BTransportInfo s5BTransportInfo = (S5BTransportInfo) transportInfo;
 761            respondToIq(packet, true);
 762            //TODO calling merge is probably a bug because that might eliminate candidates of the other party and lead to us not sending accept/deny
 763            //TODO: we probably just want to call add
 764            mergeCandidates(s5BTransportInfo.getCandidates());
 765            this.connectNextCandidate();
 766        } else if (transportInfo instanceof IbbTransportInfo) {
 767            final IbbTransportInfo ibbTransportInfo = (IbbTransportInfo) transportInfo;
 768            final int remoteBlockSize = ibbTransportInfo.getBlockSize();
 769            if (remoteBlockSize > 0) {
 770                this.ibbBlockSize = Math.min(ibbBlockSize, remoteBlockSize);
 771            }
 772            respondToIq(packet, true);
 773            this.transport = new JingleInBandTransport(this, this.transportId, this.ibbBlockSize);
 774            this.transport.connect(onIbbTransportConnected);
 775        } else {
 776            respondToIq(packet, false);
 777        }
 778    }
 779
 780    private void receiveTransportInfo(JinglePacket packet) {
 781        final Content content = packet.getJingleContent();
 782        final GenericTransportInfo transportInfo = content.getTransport();
 783        if (transportInfo instanceof S5BTransportInfo) {
 784            final S5BTransportInfo s5BTransportInfo = (S5BTransportInfo) transportInfo;
 785            if (s5BTransportInfo.hasChild("activated")) {
 786                respondToIq(packet, true);
 787                if ((this.transport != null) && (this.transport instanceof JingleSocks5Transport)) {
 788                    onProxyActivated.success();
 789                } else {
 790                    String cid = s5BTransportInfo.findChild("activated").getAttribute("cid");
 791                    Log.d(Config.LOGTAG, "received proxy activated (" + cid
 792                            + ")prior to choosing our own transport");
 793                    JingleSocks5Transport connection = this.connections.get(cid);
 794                    if (connection != null) {
 795                        connection.setActivated(true);
 796                    } else {
 797                        Log.d(Config.LOGTAG, "activated connection not found");
 798                        sendSessionTerminate(Reason.FAILED_TRANSPORT);
 799                        this.fail();
 800                    }
 801                }
 802            } else if (s5BTransportInfo.hasChild("proxy-error")) {
 803                respondToIq(packet, true);
 804                onProxyActivated.failed();
 805            } else if (s5BTransportInfo.hasChild("candidate-error")) {
 806                Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": received candidate error");
 807                respondToIq(packet, true);
 808                this.receivedCandidate = true;
 809                if (mJingleStatus == JINGLE_STATUS_ACCEPTED && this.sentCandidate) {
 810                    this.connect();
 811                }
 812            } else if (s5BTransportInfo.hasChild("candidate-used")) {
 813                String cid = s5BTransportInfo.findChild("candidate-used").getAttribute("cid");
 814                if (cid != null) {
 815                    Log.d(Config.LOGTAG, "candidate used by counterpart:" + cid);
 816                    JingleCandidate candidate = getCandidate(cid);
 817                    if (candidate == null) {
 818                        Log.d(Config.LOGTAG, "could not find candidate with cid=" + cid);
 819                        respondToIq(packet, false);
 820                        return;
 821                    }
 822                    respondToIq(packet, true);
 823                    candidate.flagAsUsedByCounterpart();
 824                    this.receivedCandidate = true;
 825                    if (mJingleStatus == JINGLE_STATUS_ACCEPTED && this.sentCandidate) {
 826                        this.connect();
 827                    } else {
 828                        Log.d(Config.LOGTAG, "ignoring because file is already in transmission or we haven't sent our candidate yet status=" + mJingleStatus + " sentCandidate=" + sentCandidate);
 829                    }
 830                } else {
 831                    respondToIq(packet, false);
 832                }
 833            } else {
 834                respondToIq(packet, false);
 835            }
 836        } else {
 837            respondToIq(packet, true);
 838        }
 839    }
 840
 841    private void connect() {
 842        final JingleSocks5Transport connection = chooseConnection();
 843        this.transport = connection;
 844        if (connection == null) {
 845            Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": could not find suitable candidate");
 846            this.disconnectSocks5Connections();
 847            if (isInitiator()) {
 848                this.sendFallbackToIbb();
 849            }
 850        } else {
 851            //TODO at this point we can already close other connections to free some resources
 852            final JingleCandidate candidate = connection.getCandidate();
 853            Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": elected candidate " + candidate.toString());
 854            this.mJingleStatus = JINGLE_STATUS_TRANSMITTING;
 855            if (connection.needsActivation()) {
 856                if (connection.getCandidate().isOurs()) {
 857                    final String sid;
 858                    if (description.getVersion() == FileTransferDescription.Version.FT_3) {
 859                        Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": use session ID instead of transport ID to activate proxy");
 860                        sid = id.sessionId;
 861                    } else {
 862                        sid = getTransportId();
 863                    }
 864                    Log.d(Config.LOGTAG, "candidate "
 865                            + connection.getCandidate().getCid()
 866                            + " was our proxy. going to activate");
 867                    IqPacket activation = new IqPacket(IqPacket.TYPE.SET);
 868                    activation.setTo(connection.getCandidate().getJid());
 869                    activation.query("http://jabber.org/protocol/bytestreams")
 870                            .setAttribute("sid", sid);
 871                    activation.query().addChild("activate")
 872                            .setContent(this.id.with.toEscapedString());
 873                    xmppConnectionService.sendIqPacket(this.id.account, activation, (account, response) -> {
 874                        if (response.getType() != IqPacket.TYPE.RESULT) {
 875                            Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": " + response.toString());
 876                            sendProxyError();
 877                            onProxyActivated.failed();
 878                        } else {
 879                            sendProxyActivated(connection.getCandidate().getCid());
 880                            onProxyActivated.success();
 881                        }
 882                    });
 883                } else {
 884                    Log.d(Config.LOGTAG,
 885                            "candidate "
 886                                    + connection.getCandidate().getCid()
 887                                    + " was a proxy. waiting for other party to activate");
 888                }
 889            } else {
 890                if (isInitiator()) {
 891                    Log.d(Config.LOGTAG, "we were initiating. sending file");
 892                    connection.send(file, onFileTransmissionStatusChanged);
 893                } else {
 894                    Log.d(Config.LOGTAG, "we were responding. receiving file");
 895                    connection.receive(file, onFileTransmissionStatusChanged);
 896                }
 897            }
 898        }
 899    }
 900
 901    private JingleSocks5Transport chooseConnection() {
 902        final List<JingleSocks5Transport> establishedConnections = FluentIterable.from(connections.entrySet())
 903                .transform(Entry::getValue)
 904                .filter(c -> (c != null && c.isEstablished() && (c.getCandidate().isUsedByCounterpart() || !c.getCandidate().isOurs())))
 905                .toSortedList((a, b) -> {
 906                    final int compare = Integer.compare(b.getCandidate().getPriority(), a.getCandidate().getPriority());
 907                    if (compare == 0) {
 908                        if (isInitiator()) {
 909                            //pick the one we sent a candidate-used for (meaning not ours)
 910                            return a.getCandidate().isOurs() ? 1 : -1;
 911                        } else {
 912                            //pick the one they sent a candidate-used for (meaning ours)
 913                            return a.getCandidate().isOurs() ? -1 : 1;
 914                        }
 915                    }
 916                    return compare;
 917                });
 918        return Iterables.getFirst(establishedConnections, null);
 919    }
 920
 921    private void sendSuccess() {
 922        sendSessionTerminate(Reason.SUCCESS);
 923        this.disconnectSocks5Connections();
 924        this.mJingleStatus = JINGLE_STATUS_FINISHED;
 925        this.message.setStatus(Message.STATUS_RECEIVED);
 926        this.message.setTransferable(null);
 927        this.xmppConnectionService.updateMessage(message, false);
 928        this.jingleConnectionManager.finishConnection(this);
 929    }
 930
 931    private void sendFallbackToIbb() {
 932        Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": sending fallback to ibb");
 933        final JinglePacket packet = this.bootstrapPacket(JinglePacket.Action.TRANSPORT_REPLACE);
 934        final Content content = new Content(this.contentCreator, this.contentSenders, this.contentName);
 935        this.transportId = JingleConnectionManager.nextRandomId();
 936        content.setTransport(new IbbTransportInfo(this.transportId, this.ibbBlockSize));
 937        packet.addJingleContent(content);
 938        this.sendJinglePacket(packet);
 939    }
 940
 941
 942    private void receiveFallbackToIbb(final JinglePacket packet, final IbbTransportInfo transportInfo) {
 943        if (isInitiator()) {
 944            Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": received out of order transport-replace (we were initiating)");
 945            respondToIqWithOutOfOrder(packet);
 946            return;
 947        }
 948        final boolean validState = mJingleStatus == JINGLE_STATUS_ACCEPTED || (proxyActivationFailed && mJingleStatus == JINGLE_STATUS_TRANSMITTING);
 949        if (!validState) {
 950            Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": received out of order transport-replace");
 951            respondToIqWithOutOfOrder(packet);
 952            return;
 953        }
 954        this.proxyActivationFailed = false; //fallback received; now we no longer need to accept another one;
 955        Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": receiving fallback to ibb");
 956        final int remoteBlockSize = transportInfo.getBlockSize();
 957        if (remoteBlockSize > 0) {
 958            this.ibbBlockSize = Math.min(MAX_IBB_BLOCK_SIZE, remoteBlockSize);
 959        } else {
 960            Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": unable to parse block size in transport-replace");
 961        }
 962        this.transportId = transportInfo.getTransportId(); //TODO: handle the case where this is null by the remote party
 963        this.transport = new JingleInBandTransport(this, this.transportId, this.ibbBlockSize);
 964
 965        final JinglePacket answer = bootstrapPacket(JinglePacket.Action.TRANSPORT_ACCEPT);
 966
 967        final Content content = new Content(contentCreator, contentSenders, contentName);
 968        content.setTransport(new IbbTransportInfo(this.transportId, this.ibbBlockSize));
 969        answer.addJingleContent(content);
 970
 971        respondToIq(packet, true);
 972
 973        if (isInitiator()) {
 974            this.sendJinglePacket(answer, (account, response) -> {
 975                if (response.getType() == IqPacket.TYPE.RESULT) {
 976                    Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + " recipient ACKed our transport-accept. creating ibb");
 977                    transport.connect(onIbbTransportConnected);
 978                }
 979            });
 980        } else {
 981            this.transport.receive(file, onFileTransmissionStatusChanged);
 982            this.sendJinglePacket(answer);
 983        }
 984    }
 985
 986    private void receiveTransportAccept(JinglePacket packet) {
 987        if (responding()) {
 988            Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": received out of order transport-accept (we were responding)");
 989            respondToIqWithOutOfOrder(packet);
 990            return;
 991        }
 992        final boolean validState = mJingleStatus == JINGLE_STATUS_ACCEPTED || (proxyActivationFailed && mJingleStatus == JINGLE_STATUS_TRANSMITTING);
 993        if (!validState) {
 994            Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": received out of order transport-accept");
 995            respondToIqWithOutOfOrder(packet);
 996            return;
 997        }
 998        this.proxyActivationFailed = false; //fallback accepted; now we no longer need to accept another one;
 999        final Content content = packet.getJingleContent();
1000        final GenericTransportInfo transportInfo = content == null ? null : content.getTransport();
1001        if (transportInfo instanceof IbbTransportInfo) {
1002            final IbbTransportInfo ibbTransportInfo = (IbbTransportInfo) transportInfo;
1003            final int remoteBlockSize = ibbTransportInfo.getBlockSize();
1004            if (remoteBlockSize > 0) {
1005                this.ibbBlockSize = Math.min(MAX_IBB_BLOCK_SIZE, remoteBlockSize);
1006            }
1007            final String sid = ibbTransportInfo.getTransportId();
1008            this.transport = new JingleInBandTransport(this, this.transportId, this.ibbBlockSize);
1009
1010            if (sid == null || !sid.equals(this.transportId)) {
1011                Log.w(Config.LOGTAG, String.format("%s: sid in transport-accept (%s) did not match our sid (%s) ", id.account.getJid().asBareJid(), sid, transportId));
1012            }
1013            respondToIq(packet, true);
1014            //might be receive instead if we are not initiating
1015            if (isInitiator()) {
1016                this.transport.connect(onIbbTransportConnected);
1017            }
1018        } else {
1019            Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": received invalid transport-accept");
1020            respondToIq(packet, false);
1021        }
1022    }
1023
1024    private void receiveSuccess() {
1025        if (isInitiator()) {
1026            this.mJingleStatus = JINGLE_STATUS_FINISHED;
1027            this.xmppConnectionService.markMessage(this.message, Message.STATUS_SEND_RECEIVED);
1028            this.disconnectSocks5Connections();
1029            if (this.transport instanceof JingleInBandTransport) {
1030                this.transport.disconnect();
1031            }
1032            this.message.setTransferable(null);
1033            this.jingleConnectionManager.finishConnection(this);
1034        } else {
1035            Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": received session-terminate/success while responding");
1036        }
1037    }
1038
1039    @Override
1040    public void cancel() {
1041        this.cancelled = true;
1042        abort(Reason.CANCEL);
1043    }
1044
1045    private void abort(final Reason reason) {
1046        this.disconnectSocks5Connections();
1047        if (this.transport instanceof JingleInBandTransport) {
1048            this.transport.disconnect();
1049        }
1050        sendSessionTerminate(reason);
1051        this.jingleConnectionManager.finishConnection(this);
1052        if (responding()) {
1053            this.message.setTransferable(new TransferablePlaceholder(cancelled ? Transferable.STATUS_CANCELLED : Transferable.STATUS_FAILED));
1054            if (this.file != null) {
1055                file.delete();
1056            }
1057            this.jingleConnectionManager.updateConversationUi(true);
1058        } else {
1059            this.xmppConnectionService.markMessage(this.message, Message.STATUS_SEND_FAILED, cancelled ? Message.ERROR_MESSAGE_CANCELLED : null);
1060            this.message.setTransferable(null);
1061        }
1062    }
1063
1064    private void fail() {
1065        fail(null);
1066    }
1067
1068    private void fail(String errorMessage) {
1069        this.mJingleStatus = JINGLE_STATUS_FAILED;
1070        this.disconnectSocks5Connections();
1071        if (this.transport instanceof JingleInBandTransport) {
1072            this.transport.disconnect();
1073        }
1074        FileBackend.close(mFileInputStream);
1075        FileBackend.close(mFileOutputStream);
1076        if (this.message != null) {
1077            if (responding()) {
1078                this.message.setTransferable(new TransferablePlaceholder(cancelled ? Transferable.STATUS_CANCELLED : Transferable.STATUS_FAILED));
1079                if (this.file != null) {
1080                    file.delete();
1081                }
1082                this.jingleConnectionManager.updateConversationUi(true);
1083            } else {
1084                this.xmppConnectionService.markMessage(this.message,
1085                        Message.STATUS_SEND_FAILED,
1086                        cancelled ? Message.ERROR_MESSAGE_CANCELLED : errorMessage);
1087                this.message.setTransferable(null);
1088            }
1089        }
1090        this.jingleConnectionManager.finishConnection(this);
1091    }
1092
1093    private void sendSessionTerminate(Reason reason) {
1094        final JinglePacket packet = bootstrapPacket(JinglePacket.Action.SESSION_TERMINATE);
1095        packet.setReason(reason, null);
1096        this.sendJinglePacket(packet);
1097    }
1098
1099    private void connectNextCandidate() {
1100        for (JingleCandidate candidate : this.candidates) {
1101            if ((!connections.containsKey(candidate.getCid()) && (!candidate
1102                    .isOurs()))) {
1103                this.connectWithCandidate(candidate);
1104                return;
1105            }
1106        }
1107        this.sendCandidateError();
1108    }
1109
1110    private void connectWithCandidate(final JingleCandidate candidate) {
1111        final JingleSocks5Transport socksConnection = new JingleSocks5Transport(
1112                this, candidate);
1113        connections.put(candidate.getCid(), socksConnection);
1114        socksConnection.connect(new OnTransportConnected() {
1115
1116            @Override
1117            public void failed() {
1118                Log.d(Config.LOGTAG,
1119                        "connection failed with " + candidate.getHost() + ":"
1120                                + candidate.getPort());
1121                connectNextCandidate();
1122            }
1123
1124            @Override
1125            public void established() {
1126                Log.d(Config.LOGTAG,
1127                        "established connection with " + candidate.getHost()
1128                                + ":" + candidate.getPort());
1129                sendCandidateUsed(candidate.getCid());
1130            }
1131        });
1132    }
1133
1134    private void disconnectSocks5Connections() {
1135        Iterator<Entry<String, JingleSocks5Transport>> it = this.connections
1136                .entrySet().iterator();
1137        while (it.hasNext()) {
1138            Entry<String, JingleSocks5Transport> pairs = it.next();
1139            pairs.getValue().disconnect();
1140            it.remove();
1141        }
1142    }
1143
1144    private void sendProxyActivated(String cid) {
1145        final JinglePacket packet = bootstrapPacket(JinglePacket.Action.TRANSPORT_INFO);
1146        final Content content = new Content(this.contentCreator, this.contentSenders, this.contentName);
1147        content.setTransport(new S5BTransportInfo(this.transportId, new Element("activated").setAttribute("cid", cid)));
1148        packet.addJingleContent(content);
1149        this.sendJinglePacket(packet);
1150    }
1151
1152    private void sendProxyError() {
1153        final JinglePacket packet = bootstrapPacket(JinglePacket.Action.TRANSPORT_INFO);
1154        final Content content = new Content(this.contentCreator, this.contentSenders, this.contentName);
1155        content.setTransport(new S5BTransportInfo(this.transportId, new Element("proxy-error")));
1156        packet.addJingleContent(content);
1157        this.sendJinglePacket(packet);
1158    }
1159
1160    private void sendCandidateUsed(final String cid) {
1161        JinglePacket packet = bootstrapPacket(JinglePacket.Action.TRANSPORT_INFO);
1162        final Content content = new Content(this.contentCreator, this.contentSenders, this.contentName);
1163        content.setTransport(new S5BTransportInfo(this.transportId, new Element("candidate-used").setAttribute("cid", cid)));
1164        packet.addJingleContent(content);
1165        this.sentCandidate = true;
1166        if ((receivedCandidate) && (mJingleStatus == JINGLE_STATUS_ACCEPTED)) {
1167            connect();
1168        }
1169        this.sendJinglePacket(packet);
1170    }
1171
1172    private void sendCandidateError() {
1173        Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": sending candidate error");
1174        JinglePacket packet = bootstrapPacket(JinglePacket.Action.TRANSPORT_INFO);
1175        Content content = new Content(this.contentCreator, this.contentSenders, this.contentName);
1176        content.setTransport(new S5BTransportInfo(this.transportId, new Element("candidate-error")));
1177        packet.addJingleContent(content);
1178        this.sentCandidate = true;
1179        this.sendJinglePacket(packet);
1180        if (receivedCandidate && mJingleStatus == JINGLE_STATUS_ACCEPTED) {
1181            connect();
1182        }
1183    }
1184
1185    private int getJingleStatus() {
1186        return this.mJingleStatus;
1187    }
1188
1189    private boolean equalCandidateExists(JingleCandidate candidate) {
1190        for (JingleCandidate c : this.candidates) {
1191            if (c.equalValues(candidate)) {
1192                return true;
1193            }
1194        }
1195        return false;
1196    }
1197
1198    private void mergeCandidate(JingleCandidate candidate) {
1199        for (JingleCandidate c : this.candidates) {
1200            if (c.equals(candidate)) {
1201                return;
1202            }
1203        }
1204        this.candidates.add(candidate);
1205    }
1206
1207    private void mergeCandidates(List<JingleCandidate> candidates) {
1208        Collections.sort(candidates, (a, b) -> Integer.compare(b.getPriority(), a.getPriority()));
1209        for (JingleCandidate c : candidates) {
1210            mergeCandidate(c);
1211        }
1212    }
1213
1214    private JingleCandidate getCandidate(String cid) {
1215        for (JingleCandidate c : this.candidates) {
1216            if (c.getCid().equals(cid)) {
1217                return c;
1218            }
1219        }
1220        return null;
1221    }
1222
1223    void updateProgress(int i) {
1224        this.mProgress = i;
1225        jingleConnectionManager.updateConversationUi(false);
1226    }
1227
1228    String getTransportId() {
1229        return this.transportId;
1230    }
1231
1232    FileTransferDescription.Version getFtVersion() {
1233        return this.description.getVersion();
1234    }
1235
1236    public JingleTransport getTransport() {
1237        return this.transport;
1238    }
1239
1240    public boolean start() {
1241        if (id.account.getStatus() == Account.State.ONLINE) {
1242            if (mJingleStatus == JINGLE_STATUS_INITIATED) {
1243                new Thread(this::sendAccept).start();
1244            }
1245            return true;
1246        } else {
1247            return false;
1248        }
1249    }
1250
1251    @Override
1252    public int getStatus() {
1253        return this.mStatus;
1254    }
1255
1256    @Override
1257    public Long getFileSize() {
1258        if (this.file != null) {
1259            return this.file.getExpectedSize();
1260        } else {
1261            return null;
1262        }
1263    }
1264
1265    @Override
1266    public int getProgress() {
1267        return this.mProgress;
1268    }
1269
1270    AbstractConnectionManager getConnectionManager() {
1271        return this.jingleConnectionManager;
1272    }
1273
1274    interface OnProxyActivated {
1275        void success();
1276
1277        void failed();
1278    }
1279}