aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java
diff options
context:
space:
mode:
authorDaniel Gultsch <daniel@gultsch.de>2015-01-04 15:05:24 +0100
committerDaniel Gultsch <daniel@gultsch.de>2015-01-04 15:05:24 +0100
commitd64496eab6f50784ba7dd0c77fb633cb845d4285 (patch)
treea5f653ca019d55fde10773ce00c5d82f2747cd4b /src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java
parent00fdac42a9d2592c7c0fcbaa5b973f3a164bcf12 (diff)
parent88704ce5cd68047008375c2eba6751e2e3cdd8c0 (diff)
Merge pull request #823 from SamWhited/issue20
Verify IQ responses
Diffstat (limited to 'src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java')
-rw-r--r--src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java70
1 files changed, 42 insertions, 28 deletions
diff --git a/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java b/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java
index ff3210a4..56d3e560 100644
--- a/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java
+++ b/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java
@@ -9,6 +9,7 @@ import android.os.PowerManager.WakeLock;
import android.os.SystemClock;
import android.preference.PreferenceManager;
import android.util.Log;
+import android.util.Pair;
import android.util.SparseArray;
import org.apache.http.conn.ssl.StrictHostnameVerifier;
@@ -104,7 +105,7 @@ public class XmppConnection implements Runnable {
private long lastConnect = 0;
private long lastSessionStarted = 0;
private int attempt = 0;
- private final Map<String, OnIqPacketReceived> packetCallbacks = new Hashtable<>();
+ private final Map<String, Pair<IqPacket, OnIqPacketReceived>> packetCallbacks = new Hashtable<>();
private OnPresencePacketReceived presenceListener = null;
private OnJinglePacketReceived jingleListener = null;
private OnIqPacketReceived unregisteredIqListener = null;
@@ -372,7 +373,7 @@ public class XmppConnection implements Runnable {
private void sendInitialPing() {
Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": sending intial ping");
- final IqPacket iq = new IqPacket(IqPacket.TYPE_GET);
+ final IqPacket iq = new IqPacket(IqPacket.TYPE.GET);
iq.setFrom(account.getJid());
iq.addChild("ping", "urn:xmpp:ping");
this.sendIqPacket(iq, new OnIqPacketReceived() {
@@ -444,13 +445,26 @@ public class XmppConnection implements Runnable {
}
} else {
if (packetCallbacks.containsKey(packet.getId())) {
- packetCallbacks.get(packet.getId()).onIqPacketReceived(account, packet);
- packetCallbacks.remove(packet.getId());
- } else if ((packet.getType() == IqPacket.TYPE_GET || packet
- .getType() == IqPacket.TYPE_SET)
+ final Pair<IqPacket, OnIqPacketReceived> packetCallbackDuple = packetCallbacks.get(packet.getId());
+ // Packets to the server should have responses from the server
+ if (packetCallbackDuple.first.toServer(account)) {
+ if (packet.fromServer(account)) {
+ packetCallbackDuple.second
+ .onIqPacketReceived(account, packet);
+ packetCallbacks.remove(packet.getId());
+ }
+ } else {
+ if (packet.getFrom().equals(packetCallbackDuple.first.getTo())) {
+ packetCallbackDuple.second
+ .onIqPacketReceived(account, packet);
+ packetCallbacks.remove(packet.getId());
+ }
+ }
+ } else if ((packet.getType() == IqPacket.TYPE.GET || packet
+ .getType() == IqPacket.TYPE.SET)
&& this.unregisteredIqListener != null) {
this.unregisteredIqListener.onIqPacketReceived(account, packet);
- }
+ }
}
}
@@ -479,19 +493,19 @@ public class XmppConnection implements Runnable {
}
private void switchOverToTls(final Tag currentTag) throws XmlPullParserException, IOException {
- tagReader.readTag();
- try {
+ tagReader.readTag();
+ try {
final SSLContext sc = SSLContext.getInstance("TLS");
sc.init(null,new X509TrustManager[]{this.mXmppConnectionService.getMemorizingTrustManager()},mXmppConnectionService.getRNG());
final SSLSocketFactory factory = sc.getSocketFactory();
final HostnameVerifier verifier = this.mXmppConnectionService.getMemorizingTrustManager().wrapHostnameVerifier(new StrictHostnameVerifier());
- final InetAddress address = socket == null ? null : socket.getInetAddress();
+ final InetAddress address = socket == null ? null : socket.getInetAddress();
if (factory == null || address == null || verifier == null) {
throw new IOException("could not setup ssl");
}
- final SSLSocket sslSocket = (SSLSocket) factory.createSocket(socket,address.getHostAddress(), socket.getPort(),true);
+ final SSLSocket sslSocket = (SSLSocket) factory.createSocket(socket,address.getHostAddress(), socket.getPort(),true);
if (sslSocket == null) {
throw new IOException("could not initialize ssl socket");
@@ -506,13 +520,13 @@ public class XmppConnection implements Runnable {
supportedProtocols.remove("SSLv3");
supportProtocols = new String[supportedProtocols.size()];
supportedProtocols.toArray(supportProtocols);
- }
+ }
sslSocket.setEnabledProtocols(supportProtocols);
- if (!verifier.verify(account.getServer().getDomainpart(),sslSocket.getSession())) {
- Log.d(Config.LOGTAG,account.getJid().toBareJid()+": TLS certificate verification failed");
- disconnect(true);
- changeStatus(Account.State.SECURITY_ERROR);
+ if (!verifier.verify(account.getServer().getDomainpart(),sslSocket.getSession())) {
+ Log.d(Config.LOGTAG,account.getJid().toBareJid()+": TLS certificate verification failed");
+ disconnect(true);
+ changeStatus(Account.State.SECURITY_ERROR);
}
tagReader.setInputStream(sslSocket.getInputStream());
tagWriter.setOutputStream(sslSocket.getOutputStream());
@@ -521,11 +535,11 @@ public class XmppConnection implements Runnable {
enabledEncryption = true;
processStream(tagReader.readTag());
sslSocket.close();
- } catch (final NoSuchAlgorithmException | KeyManagementException e1) {
+ } catch (final NoSuchAlgorithmException | KeyManagementException e1) {
Log.d(Config.LOGTAG,account.getJid().toBareJid()+": TLS certificate verification failed");
disconnect(true);
changeStatus(Account.State.SECURITY_ERROR);
- }
+ }
}
private void processStreamFeatures(final Tag currentTag)
@@ -598,7 +612,7 @@ public class XmppConnection implements Runnable {
}
private void sendRegistryRequest() {
- final IqPacket register = new IqPacket(IqPacket.TYPE_GET);
+ final IqPacket register = new IqPacket(IqPacket.TYPE.GET);
register.query("jabber:iq:register");
register.setTo(account.getServer());
sendIqPacket(register, new OnIqPacketReceived() {
@@ -608,7 +622,7 @@ public class XmppConnection implements Runnable {
final Element instructions = packet.query().findChild("instructions");
if (packet.query().hasChild("username")
&& (packet.query().hasChild("password"))) {
- final IqPacket register = new IqPacket(IqPacket.TYPE_SET);
+ final IqPacket register = new IqPacket(IqPacket.TYPE.SET);
final Element username = new Element("username")
.setContent(account.getUsername());
final Element password = new Element("password")
@@ -619,7 +633,7 @@ public class XmppConnection implements Runnable {
@Override
public void onIqPacketReceived(final Account account, final IqPacket packet) {
- if (packet.getType() == IqPacket.TYPE_RESULT) {
+ if (packet.getType() == IqPacket.TYPE.RESULT) {
account.setOption(Account.OPTION_REGISTER,
false);
changeStatus(Account.State.REGISTRATION_SUCCESSFUL);
@@ -646,7 +660,7 @@ public class XmppConnection implements Runnable {
}
private void sendBindRequest() {
- final IqPacket iq = new IqPacket(IqPacket.TYPE_SET);
+ final IqPacket iq = new IqPacket(IqPacket.TYPE.SET);
iq.addChild("bind", "urn:ietf:params:xml:ns:xmpp-bind")
.addChild("resource").setContent(account.getResource());
this.sendUnmodifiedIqPacket(iq, new OnIqPacketReceived() {
@@ -693,7 +707,7 @@ public class XmppConnection implements Runnable {
});
if (this.streamFeatures.hasChild("session")) {
Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": sending deprecated session");
- final IqPacket startSession = new IqPacket(IqPacket.TYPE_SET);
+ final IqPacket startSession = new IqPacket(IqPacket.TYPE.SET);
startSession.addChild("session","urn:ietf:params:xml:ns:xmpp-session");
this.sendUnmodifiedIqPacket(startSession, null);
}
@@ -705,7 +719,7 @@ public class XmppConnection implements Runnable {
enableAdvancedStreamFeatures();
}
} else {
- final IqPacket iq = new IqPacket(IqPacket.TYPE_GET);
+ final IqPacket iq = new IqPacket(IqPacket.TYPE.GET);
iq.setTo(server.toDomainJid());
iq.query("http://jabber.org/protocol/disco#info");
this.sendIqPacket(iq, new OnIqPacketReceived() {
@@ -750,7 +764,7 @@ public class XmppConnection implements Runnable {
}
private void sendServiceDiscoveryItems(final Jid server) {
- final IqPacket iq = new IqPacket(IqPacket.TYPE_GET);
+ final IqPacket iq = new IqPacket(IqPacket.TYPE.GET);
iq.setTo(server.toDomainJid());
iq.query("http://jabber.org/protocol/disco#items");
this.sendIqPacket(iq, new OnIqPacketReceived() {
@@ -771,7 +785,7 @@ public class XmppConnection implements Runnable {
}
private void sendEnableCarbons() {
- final IqPacket iq = new IqPacket(IqPacket.TYPE_SET);
+ final IqPacket iq = new IqPacket(IqPacket.TYPE.SET);
iq.addChild("enable", "urn:xmpp:carbons:2");
this.sendIqPacket(iq, new OnIqPacketReceived() {
@@ -831,7 +845,7 @@ public class XmppConnection implements Runnable {
if (packet.getId() == null) {
packet.setId(nextRandomId());
}
- packetCallbacks.put(packet.getId(), callback);
+ packetCallbacks.put(packet.getId(), new Pair<>(packet, callback));
}
this.sendPacket(packet);
}
@@ -861,7 +875,7 @@ public class XmppConnection implements Runnable {
if (streamFeatures.hasChild("sm")) {
tagWriter.writeStanzaAsync(new RequestPacket(smVersion));
} else {
- final IqPacket iq = new IqPacket(IqPacket.TYPE_GET);
+ final IqPacket iq = new IqPacket(IqPacket.TYPE.GET);
iq.setFrom(account.getJid());
iq.addChild("ping", "urn:xmpp:ping");
this.sendIqPacket(iq, null);