Search in sources :

Example 96 with IqPacket

use of eu.siacs.conversations.xmpp.stanzas.IqPacket in project Conversations by siacs.

the class JingleConnectionManager method getPrimaryCandidate.

void getPrimaryCandidate(final Account account, final boolean initiator, final OnPrimaryCandidateFound listener) {
    if (Config.DISABLE_PROXY_LOOKUP) {
        listener.onPrimaryCandidateFound(false, null);
        return;
    }
    if (!this.primaryCandidates.containsKey(account.getJid().asBareJid())) {
        final Jid proxy = account.getXmppConnection().findDiscoItemByFeature(Namespace.BYTE_STREAMS);
        if (proxy != null) {
            IqPacket iq = new IqPacket(IqPacket.TYPE.GET);
            iq.setTo(proxy);
            iq.query(Namespace.BYTE_STREAMS);
            account.getXmppConnection().sendIqPacket(iq, new OnIqPacketReceived() {

                @Override
                public void onIqPacketReceived(Account account, IqPacket packet) {
                    final Element streamhost = packet.query().findChild("streamhost", Namespace.BYTE_STREAMS);
                    final String host = streamhost == null ? null : streamhost.getAttribute("host");
                    final String port = streamhost == null ? null : streamhost.getAttribute("port");
                    if (host != null && port != null) {
                        try {
                            JingleCandidate candidate = new JingleCandidate(nextRandomId(), true);
                            candidate.setHost(host);
                            candidate.setPort(Integer.parseInt(port));
                            candidate.setType(JingleCandidate.TYPE_PROXY);
                            candidate.setJid(proxy);
                            candidate.setPriority(655360 + (initiator ? 30 : 0));
                            primaryCandidates.put(account.getJid().asBareJid(), candidate);
                            listener.onPrimaryCandidateFound(true, candidate);
                        } catch (final NumberFormatException e) {
                            listener.onPrimaryCandidateFound(false, null);
                        }
                    } else {
                        listener.onPrimaryCandidateFound(false, null);
                    }
                }
            });
        } else {
            listener.onPrimaryCandidateFound(false, null);
        }
    } else {
        listener.onPrimaryCandidateFound(true, this.primaryCandidates.get(account.getJid().asBareJid()));
    }
}
Also used : Account(eu.siacs.conversations.entities.Account) Jid(eu.siacs.conversations.xmpp.Jid) OnIqPacketReceived(eu.siacs.conversations.xmpp.OnIqPacketReceived) Element(eu.siacs.conversations.xml.Element) IqPacket(eu.siacs.conversations.xmpp.stanzas.IqPacket)

Example 97 with IqPacket

use of eu.siacs.conversations.xmpp.stanzas.IqPacket in project Conversations by siacs.

the class JingleFileTransferConnection method respondToIq.

private void respondToIq(final IqPacket packet, final boolean result) {
    final IqPacket response;
    if (result) {
        response = packet.generateResponse(IqPacket.TYPE.RESULT);
    } else {
        response = packet.generateResponse(IqPacket.TYPE.ERROR);
        final Element error = response.addChild("error").setAttribute("type", "cancel");
        error.addChild("not-acceptable", "urn:ietf:params:xml:ns:xmpp-stanzas");
    }
    xmppConnectionService.sendIqPacket(id.account, response, null);
}
Also used : Element(eu.siacs.conversations.xml.Element) IqPacket(eu.siacs.conversations.xmpp.stanzas.IqPacket)

Example 98 with IqPacket

use of eu.siacs.conversations.xmpp.stanzas.IqPacket in project Conversations by siacs.

the class JingleFileTransferConnection method respondToIqWithOutOfOrder.

private void respondToIqWithOutOfOrder(final IqPacket packet) {
    final IqPacket response = packet.generateResponse(IqPacket.TYPE.ERROR);
    final Element error = response.addChild("error").setAttribute("type", "wait");
    error.addChild("unexpected-request", "urn:ietf:params:xml:ns:xmpp-stanzas");
    error.addChild("out-of-order", "urn:xmpp:jingle:errors:1");
    xmppConnectionService.sendIqPacket(id.account, response, null);
}
Also used : Element(eu.siacs.conversations.xml.Element) IqPacket(eu.siacs.conversations.xmpp.stanzas.IqPacket)

Example 99 with IqPacket

use of eu.siacs.conversations.xmpp.stanzas.IqPacket in project Conversations by siacs.

the class JingleInBandTransport method sendNextBlock.

private void sendNextBlock() {
    byte[] buffer = new byte[this.blockSize];
    try {
        int count = innerInputStream.read(buffer);
        if (count == -1) {
            sendClose();
            file.setSha1Sum(digest.digest());
            Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": sendNextBlock() count was -1");
            this.onFileTransmissionStatusChanged.onFileTransmitted(file);
            fileInputStream.close();
            return;
        } else if (count != buffer.length) {
            int rem = innerInputStream.read(buffer, count, buffer.length - count);
            if (rem > 0) {
                count += rem;
            }
        }
        this.remainingSize -= count;
        this.digest.update(buffer, 0, count);
        String base64 = Base64.encodeToString(buffer, 0, count, Base64.NO_WRAP);
        IqPacket iq = new IqPacket(IqPacket.TYPE.SET);
        iq.setTo(this.counterpart);
        Element data = iq.addChild("data", "http://jabber.org/protocol/ibb");
        data.setAttribute("seq", Integer.toString(this.seq));
        data.setAttribute("block-size", Integer.toString(this.blockSize));
        data.setAttribute("sid", this.sessionId);
        data.setContent(base64);
        this.account.getXmppConnection().sendIqPacket(iq, this.onAckReceived);
        // don't fill up stanza queue too much
        this.account.getXmppConnection().r();
        this.seq++;
        connection.updateProgress((int) ((((double) (this.fileSize - this.remainingSize)) / this.fileSize) * 100));
        if (this.remainingSize <= 0) {
            file.setSha1Sum(digest.digest());
            this.onFileTransmissionStatusChanged.onFileTransmitted(file);
            sendClose();
            fileInputStream.close();
        }
    } catch (IOException e) {
        Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": io exception during sendNextBlock() " + e.getMessage());
        FileBackend.close(fileInputStream);
        this.onFileTransmissionStatusChanged.onFileTransferAborted();
    }
}
Also used : Element(eu.siacs.conversations.xml.Element) IOException(java.io.IOException) IqPacket(eu.siacs.conversations.xmpp.stanzas.IqPacket)

Example 100 with IqPacket

use of eu.siacs.conversations.xmpp.stanzas.IqPacket in project Conversations by siacs.

the class JingleRtpConnection method discoverIceServers.

private void discoverIceServers(final OnIceServersDiscovered onIceServersDiscovered) {
    if (id.account.getXmppConnection().getFeatures().externalServiceDiscovery()) {
        final IqPacket request = new IqPacket(IqPacket.TYPE.GET);
        request.setTo(id.account.getDomain());
        request.addChild("services", Namespace.EXTERNAL_SERVICE_DISCOVERY);
        xmppConnectionService.sendIqPacket(id.account, request, (account, response) -> {
            ImmutableList.Builder<PeerConnection.IceServer> listBuilder = new ImmutableList.Builder<>();
            if (response.getType() == IqPacket.TYPE.RESULT) {
                final Element services = response.findChild("services", Namespace.EXTERNAL_SERVICE_DISCOVERY);
                final List<Element> children = services == null ? Collections.emptyList() : services.getChildren();
                for (final Element child : children) {
                    if ("service".equals(child.getName())) {
                        final String type = child.getAttribute("type");
                        final String host = child.getAttribute("host");
                        final String sport = child.getAttribute("port");
                        final Integer port = sport == null ? null : Ints.tryParse(sport);
                        final String transport = child.getAttribute("transport");
                        final String username = child.getAttribute("username");
                        final String password = child.getAttribute("password");
                        if (Strings.isNullOrEmpty(host) || port == null) {
                            continue;
                        }
                        if (port < 0 || port > 65535) {
                            continue;
                        }
                        if (Arrays.asList("stun", "stuns", "turn", "turns").contains(type) && Arrays.asList("udp", "tcp").contains(transport)) {
                            if (Arrays.asList("stuns", "turns").contains(type) && "udp".equals(transport)) {
                                Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": skipping invalid combination of udp/tls in external services");
                                continue;
                            }
                            final PeerConnection.IceServer.Builder iceServerBuilder = PeerConnection.IceServer.builder(String.format("%s:%s:%s?transport=%s", type, IP.wrapIPv6(host), port, transport));
                            iceServerBuilder.setTlsCertPolicy(PeerConnection.TlsCertPolicy.TLS_CERT_POLICY_INSECURE_NO_CHECK);
                            if (username != null && password != null) {
                                iceServerBuilder.setUsername(username);
                                iceServerBuilder.setPassword(password);
                            } else if (Arrays.asList("turn", "turns").contains(type)) {
                                // The WebRTC spec requires throwing an
                                // InvalidAccessError when username (from libwebrtc
                                // source coder)
                                // https://chromium.googlesource.com/external/webrtc/+/master/pc/ice_server_parsing.cc
                                Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": skipping " + type + "/" + transport + " without username and password");
                                continue;
                            }
                            final PeerConnection.IceServer iceServer = iceServerBuilder.createIceServer();
                            Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": discovered ICE Server: " + iceServer);
                            listBuilder.add(iceServer);
                        }
                    }
                }
            }
            final List<PeerConnection.IceServer> iceServers = listBuilder.build();
            if (iceServers.size() == 0) {
                Log.w(Config.LOGTAG, id.account.getJid().asBareJid() + ": no ICE server found " + response);
            }
            onIceServersDiscovered.onIceServersDiscovered(iceServers);
        });
    } else {
        Log.w(Config.LOGTAG, id.account.getJid().asBareJid() + ": has no external service discovery");
        onIceServersDiscovered.onIceServersDiscovered(Collections.emptyList());
    }
}
Also used : PeerConnection(org.webrtc.PeerConnection) ImmutableList(com.google.common.collect.ImmutableList) Element(eu.siacs.conversations.xml.Element) IqPacket(eu.siacs.conversations.xmpp.stanzas.IqPacket)

Aggregations

IqPacket (eu.siacs.conversations.xmpp.stanzas.IqPacket)319 Element (eu.siacs.conversations.xml.Element)195 Account (eu.siacs.conversations.entities.Account)83 OnIqPacketReceived (eu.siacs.conversations.xmpp.OnIqPacketReceived)70 Jid (eu.siacs.conversations.xmpp.Jid)45 Data (eu.siacs.conversations.xmpp.forms.Data)30 ArrayList (java.util.ArrayList)22 Contact (eu.siacs.conversations.entities.Contact)18 JabberIdContact (eu.siacs.conversations.android.JabberIdContact)15 PreKeyBundle (org.whispersystems.libsignal.state.PreKeyBundle)15 HashSet (java.util.HashSet)14 Bundle (android.os.Bundle)12 ServiceDiscoveryResult (eu.siacs.conversations.entities.ServiceDiscoveryResult)12 XmppConnection (eu.siacs.conversations.xmpp.XmppConnection)12 Map (java.util.Map)12 Bookmark (eu.siacs.conversations.entities.Bookmark)10 Conversation (eu.siacs.conversations.entities.Conversation)10 MucOptions (eu.siacs.conversations.entities.MucOptions)10 IOException (java.io.IOException)10 Pair (android.util.Pair)9