Search in sources :

Example 1 with Tag

use of de.pixart.messenger.xml.Tag in project Pix-Art-Messenger by kriztan.

the class XmppConnection method switchOverToTls.

private void switchOverToTls(final Tag currentTag) throws XmlPullParserException, IOException {
    tagReader.readTag();
    try {
        final TlsFactoryVerifier tlsFactoryVerifier = getTlsFactoryVerifier();
        final InetAddress address = socket == null ? null : socket.getInetAddress();
        if (address == null) {
            throw new IOException("could not setup ssl");
        }
        final SSLSocket sslSocket = (SSLSocket) tlsFactoryVerifier.factory.createSocket(socket, address.getHostAddress(), socket.getPort(), true);
        if (sslSocket == null) {
            throw new IOException("could not initialize ssl socket");
        }
        SSLSocketHelper.setSecurity(sslSocket);
        if (!tlsFactoryVerifier.verifier.verify(account.getServer().getDomainpart(), this.verifiedHostname, sslSocket.getSession())) {
            Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": TLS certificate verification failed");
            throw new StateChangingException(Account.State.TLS_ERROR);
        }
        tagReader.setInputStream(sslSocket.getInputStream());
        tagWriter.setOutputStream(sslSocket.getOutputStream());
        sendStartStream();
        Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": TLS connection established");
        features.encryptionEnabled = true;
        final Tag tag = tagReader.readTag();
        if (tag != null && tag.isStart("stream")) {
            processStream();
        } else {
            throw new IOException("server didn't restart stream after STARTTLS");
        }
        sslSocket.close();
    } catch (final NoSuchAlgorithmException | KeyManagementException e1) {
        Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": TLS certificate verification failed");
        throw new StateChangingException(Account.State.TLS_ERROR);
    }
}
Also used : SSLSocket(javax.net.ssl.SSLSocket) IOException(java.io.IOException) Tag(de.pixart.messenger.xml.Tag) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InetAddress(java.net.InetAddress) KeyManagementException(java.security.KeyManagementException)

Example 2 with Tag

use of de.pixart.messenger.xml.Tag in project Pix-Art-Messenger by kriztan.

the class XmppConnection method sendStartStream.

private void sendStartStream() throws IOException {
    final Tag stream = Tag.start("stream:stream");
    stream.setAttribute("to", account.getServer().toString());
    stream.setAttribute("version", "1.0");
    stream.setAttribute("xml:lang", "en");
    stream.setAttribute("xmlns", "jabber:client");
    stream.setAttribute("xmlns:stream", "http://etherx.jabber.org/streams");
    tagWriter.writeTag(stream);
}
Also used : Tag(de.pixart.messenger.xml.Tag)

Example 3 with Tag

use of de.pixart.messenger.xml.Tag in project Pix-Art-Messenger by kriztan.

the class XmppConnection method processPacket.

private Element processPacket(final Tag currentTag, final int packetType) throws XmlPullParserException, IOException {
    Element element;
    switch(packetType) {
        case PACKET_IQ:
            element = new IqPacket();
            break;
        case PACKET_MESSAGE:
            element = new MessagePacket();
            break;
        case PACKET_PRESENCE:
            element = new PresencePacket();
            break;
        default:
            return null;
    }
    element.setAttributes(currentTag.getAttributes());
    Tag nextTag = tagReader.readTag();
    if (nextTag == null) {
        throw new IOException("interrupted mid tag");
    }
    while (!nextTag.isEnd(element.getName())) {
        if (!nextTag.isNo()) {
            final Element child = tagReader.readElement(nextTag);
            final String type = currentTag.getAttribute("type");
            if (packetType == PACKET_IQ && "jingle".equals(child.getName()) && ("set".equalsIgnoreCase(type) || "get".equalsIgnoreCase(type))) {
                element = new JinglePacket();
                element.setAttributes(currentTag.getAttributes());
            }
            element.addChild(child);
        }
        nextTag = tagReader.readTag();
        if (nextTag == null) {
            throw new IOException("interrupted mid tag");
        }
    }
    if (stanzasReceived == Integer.MAX_VALUE) {
        resetStreamId();
        throw new IOException("time to restart the session. cant handle >2 billion pcks");
    }
    if (inSmacksSession) {
        ++stanzasReceived;
    } else if (features.sm()) {
        Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": not counting stanza(" + element.getClass().getSimpleName() + "). Not in smacks session.");
    }
    lastPacketReceived = SystemClock.elapsedRealtime();
    if (Config.BACKGROUND_STANZA_LOGGING && mXmppConnectionService.checkListeners()) {
        Log.d(Config.LOGTAG, "[background stanza] " + element);
    }
    return element;
}
Also used : MessagePacket(de.pixart.messenger.xmpp.stanzas.MessagePacket) JinglePacket(de.pixart.messenger.xmpp.jingle.stanzas.JinglePacket) Element(de.pixart.messenger.xml.Element) Tag(de.pixart.messenger.xml.Tag) IOException(java.io.IOException) PresencePacket(de.pixart.messenger.xmpp.stanzas.PresencePacket) IqPacket(de.pixart.messenger.xmpp.stanzas.IqPacket)

Example 4 with Tag

use of de.pixart.messenger.xml.Tag in project Pix-Art-Messenger by kriztan.

the class XmppConnection method startXmpp.

/**
 * Starts xmpp protocol, call after connecting to socket
 *
 * @return true if server returns with valid xmpp, false otherwise
 */
private boolean startXmpp(Socket socket) throws Exception {
    if (Thread.currentThread().isInterrupted()) {
        throw new InterruptedException();
    }
    this.socket = socket;
    tagReader = new XmlReader();
    if (tagWriter != null) {
        tagWriter.forceClose();
    }
    tagWriter = new TagWriter();
    tagWriter.setOutputStream(socket.getOutputStream());
    tagReader.setInputStream(socket.getInputStream());
    tagWriter.beginDocument();
    sendStartStream();
    final Tag tag = tagReader.readTag();
    return tag != null && tag.isStart("stream");
}
Also used : TagWriter(de.pixart.messenger.xml.TagWriter) XmlReader(de.pixart.messenger.xml.XmlReader) Tag(de.pixart.messenger.xml.Tag)

Example 5 with Tag

use of de.pixart.messenger.xml.Tag in project Pix-Art-Messenger by kriztan.

the class XmppConnection method processStream.

private void processStream() throws XmlPullParserException, IOException, NoSuchAlgorithmException {
    final CountDownLatch streamCountDownLatch = new CountDownLatch(1);
    this.mStreamCountDownLatch = streamCountDownLatch;
    Tag nextTag = tagReader.readTag();
    while (nextTag != null && !nextTag.isEnd("stream")) {
        if (nextTag.isStart("error")) {
            processStreamError(nextTag);
        } else if (nextTag.isStart("features")) {
            processStreamFeatures(nextTag);
        } else if (nextTag.isStart("proceed")) {
            switchOverToTls(nextTag);
        } else if (nextTag.isStart("success")) {
            final String challenge = tagReader.readElement(nextTag).getContent();
            try {
                saslMechanism.getResponse(challenge);
            } catch (final SaslMechanism.AuthenticationException e) {
                Log.e(Config.LOGTAG, String.valueOf(e));
                throw new StateChangingException(Account.State.UNAUTHORIZED);
            }
            Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": logged in");
            account.setKey(Account.PINNED_MECHANISM_KEY, String.valueOf(saslMechanism.getPriority()));
            tagReader.reset();
            sendStartStream();
            final Tag tag = tagReader.readTag();
            if (tag != null && tag.isStart("stream")) {
                processStream();
            } else {
                throw new IOException("server didn't restart stream after successful auth");
            }
            break;
        } else if (nextTag.isStart("failure")) {
            final Element failure = tagReader.readElement(nextTag);
            if (Namespace.SASL.equals(failure.getNamespace())) {
                final String text = failure.findChildContent("text");
                if (failure.hasChild("account-disabled") && text != null) {
                    Matcher matcher = Patterns.AUTOLINK_WEB_URL.matcher(text);
                    if (matcher.find()) {
                        try {
                            URL url = new URL(text.substring(matcher.start(), matcher.end()));
                            if (url.getProtocol().equals("https")) {
                                this.redirectionUrl = url;
                                throw new StateChangingException(Account.State.PAYMENT_REQUIRED);
                            }
                        } catch (MalformedURLException e) {
                            throw new StateChangingException(Account.State.UNAUTHORIZED);
                        }
                    }
                }
                throw new StateChangingException(Account.State.UNAUTHORIZED);
            } else if (Namespace.TLS.equals(failure.getNamespace())) {
                throw new StateChangingException(Account.State.TLS_ERROR);
            } else {
                throw new StateChangingException(Account.State.INCOMPATIBLE_SERVER);
            }
        } else if (nextTag.isStart("challenge")) {
            final String challenge = tagReader.readElement(nextTag).getContent();
            final Element response = new Element("response", Namespace.SASL);
            try {
                response.setContent(saslMechanism.getResponse(challenge));
            } catch (final SaslMechanism.AuthenticationException e) {
                // TODO: Send auth abort tag.
                Log.e(Config.LOGTAG, e.toString());
            }
            tagWriter.writeElement(response);
        } else if (nextTag.isStart("enabled")) {
            final Element enabled = tagReader.readElement(nextTag);
            if ("true".equals(enabled.getAttribute("resume"))) {
                this.streamId = enabled.getAttribute("id");
                Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": stream management(" + smVersion + ") enabled (resumable)");
            } else {
                Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": stream management(" + smVersion + ") enabled");
            }
            this.stanzasReceived = 0;
            this.inSmacksSession = true;
            final RequestPacket r = new RequestPacket(smVersion);
            tagWriter.writeStanzaAsync(r);
        } else if (nextTag.isStart("resumed")) {
            this.inSmacksSession = true;
            this.isBound = true;
            this.tagWriter.writeStanzaAsync(new RequestPacket(smVersion));
            lastPacketReceived = SystemClock.elapsedRealtime();
            final Element resumed = tagReader.readElement(nextTag);
            final String h = resumed.getAttribute("h");
            try {
                ArrayList<AbstractAcknowledgeableStanza> failedStanzas = new ArrayList<>();
                synchronized (this.mStanzaQueue) {
                    final int serverCount = Integer.parseInt(h);
                    if (serverCount < stanzasSent) {
                        Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": session resumed with lost packages");
                        stanzasSent = serverCount;
                    } else {
                        Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": session resumed");
                    }
                    acknowledgeStanzaUpTo(serverCount);
                    for (int i = 0; i < this.mStanzaQueue.size(); ++i) {
                        failedStanzas.add(mStanzaQueue.valueAt(i));
                    }
                    mStanzaQueue.clear();
                }
                Log.d(Config.LOGTAG, "resending " + failedStanzas.size() + " stanzas");
                for (AbstractAcknowledgeableStanza packet : failedStanzas) {
                    if (packet instanceof MessagePacket) {
                        MessagePacket message = (MessagePacket) packet;
                        mXmppConnectionService.markMessage(account, message.getTo().toBareJid(), message.getId(), Message.STATUS_UNSEND);
                    }
                    sendPacket(packet);
                }
            } catch (final NumberFormatException ignored) {
            }
            Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": online with resource " + account.getResource());
            changeStatus(Account.State.ONLINE);
        } else if (nextTag.isStart("r")) {
            tagReader.readElement(nextTag);
            if (Config.EXTENDED_SM_LOGGING) {
                Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": acknowledging stanza #" + this.stanzasReceived);
            }
            final AckPacket ack = new AckPacket(this.stanzasReceived, smVersion);
            tagWriter.writeStanzaAsync(ack);
        } else if (nextTag.isStart("a")) {
            boolean accountUiNeedsRefresh = false;
            synchronized (NotificationService.CATCHUP_LOCK) {
                if (mWaitingForSmCatchup.compareAndSet(true, false)) {
                    int count = mSmCatchupMessageCounter.get();
                    Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": SM catchup complete (" + count + ")");
                    accountUiNeedsRefresh = true;
                    if (count > 0) {
                        mXmppConnectionService.getNotificationService().finishBacklog(true, account);
                    }
                }
            }
            if (accountUiNeedsRefresh) {
                mXmppConnectionService.updateAccountUi();
            }
            final Element ack = tagReader.readElement(nextTag);
            lastPacketReceived = SystemClock.elapsedRealtime();
            try {
                synchronized (this.mStanzaQueue) {
                    final int serverSequence = Integer.parseInt(ack.getAttribute("h"));
                    acknowledgeStanzaUpTo(serverSequence);
                }
            } catch (NumberFormatException | NullPointerException e) {
                Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": server send ack without sequence number");
            }
        } else if (nextTag.isStart("failed")) {
            Element failed = tagReader.readElement(nextTag);
            try {
                final int serverCount = Integer.parseInt(failed.getAttribute("h"));
                Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": resumption failed but server acknowledged stanza #" + serverCount);
                synchronized (this.mStanzaQueue) {
                    acknowledgeStanzaUpTo(serverCount);
                }
            } catch (NumberFormatException | NullPointerException e) {
                Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": resumption failed");
            }
            resetStreamId();
            sendBindRequest();
        } else if (nextTag.isStart("iq")) {
            processIq(nextTag);
        } else if (nextTag.isStart("message")) {
            processMessage(nextTag);
        } else if (nextTag.isStart("presence")) {
            processPresence(nextTag);
        }
        nextTag = tagReader.readTag();
    }
    if (nextTag != null && nextTag.isEnd("stream")) {
        streamCountDownLatch.countDown();
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) RequestPacket(de.pixart.messenger.xmpp.stanzas.streammgmt.RequestPacket) Matcher(java.util.regex.Matcher) Element(de.pixart.messenger.xml.Element) ArrayList(java.util.ArrayList) AckPacket(de.pixart.messenger.xmpp.stanzas.streammgmt.AckPacket) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) URL(java.net.URL) MessagePacket(de.pixart.messenger.xmpp.stanzas.MessagePacket) AbstractAcknowledgeableStanza(de.pixart.messenger.xmpp.stanzas.AbstractAcknowledgeableStanza) Tag(de.pixart.messenger.xml.Tag)

Aggregations

Tag (de.pixart.messenger.xml.Tag)6 IOException (java.io.IOException)3 Element (de.pixart.messenger.xml.Element)2 MessagePacket (de.pixart.messenger.xmpp.stanzas.MessagePacket)2 TagWriter (de.pixart.messenger.xml.TagWriter)1 XmlReader (de.pixart.messenger.xml.XmlReader)1 JinglePacket (de.pixart.messenger.xmpp.jingle.stanzas.JinglePacket)1 AbstractAcknowledgeableStanza (de.pixart.messenger.xmpp.stanzas.AbstractAcknowledgeableStanza)1 IqPacket (de.pixart.messenger.xmpp.stanzas.IqPacket)1 PresencePacket (de.pixart.messenger.xmpp.stanzas.PresencePacket)1 AckPacket (de.pixart.messenger.xmpp.stanzas.streammgmt.AckPacket)1 RequestPacket (de.pixart.messenger.xmpp.stanzas.streammgmt.RequestPacket)1 InetAddress (java.net.InetAddress)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 KeyManagementException (java.security.KeyManagementException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 ArrayList (java.util.ArrayList)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Matcher (java.util.regex.Matcher)1