1package eu.siacs.conversations.xmpp;
2
3import android.graphics.Bitmap;
4import android.graphics.BitmapFactory;
5import android.os.Bundle;
6import android.os.Parcelable;
7import android.os.PowerManager;
8import android.os.PowerManager.WakeLock;
9import android.os.SystemClock;
10import android.security.KeyChain;
11import android.util.Base64;
12import android.util.Log;
13import android.util.Pair;
14import android.util.SparseArray;
15
16import org.json.JSONException;
17import org.json.JSONObject;
18import org.xmlpull.v1.XmlPullParserException;
19
20import java.io.ByteArrayInputStream;
21import java.io.IOException;
22import java.io.InputStream;
23import java.lang.reflect.Method;
24import java.math.BigInteger;
25import java.net.ConnectException;
26import java.net.IDN;
27import java.net.InetAddress;
28import java.net.InetSocketAddress;
29import java.net.Socket;
30import java.net.UnknownHostException;
31import java.net.URL;
32import java.security.KeyManagementException;
33import java.security.NoSuchAlgorithmException;
34import java.security.Principal;
35import java.security.PrivateKey;
36import java.security.cert.X509Certificate;
37import java.util.ArrayList;
38import java.util.Arrays;
39import java.util.Collection;
40import java.util.HashMap;
41import java.util.Hashtable;
42import java.util.Iterator;
43import java.util.LinkedList;
44import java.util.List;
45import java.util.Map.Entry;
46
47import javax.net.ssl.HostnameVerifier;
48import javax.net.ssl.KeyManager;
49import javax.net.ssl.SSLContext;
50import javax.net.ssl.SSLSocket;
51import javax.net.ssl.SSLSocketFactory;
52import javax.net.ssl.X509KeyManager;
53import javax.net.ssl.X509TrustManager;
54
55import de.duenndns.ssl.MemorizingTrustManager;
56import eu.siacs.conversations.Config;
57import eu.siacs.conversations.crypto.XmppDomainVerifier;
58import eu.siacs.conversations.crypto.sasl.DigestMd5;
59import eu.siacs.conversations.crypto.sasl.External;
60import eu.siacs.conversations.crypto.sasl.Plain;
61import eu.siacs.conversations.crypto.sasl.SaslMechanism;
62import eu.siacs.conversations.crypto.sasl.ScramSha1;
63import eu.siacs.conversations.entities.Account;
64import eu.siacs.conversations.entities.Message;
65import eu.siacs.conversations.generator.IqGenerator;
66import eu.siacs.conversations.services.XmppConnectionService;
67import eu.siacs.conversations.utils.CryptoHelper;
68import eu.siacs.conversations.utils.DNSHelper;
69import eu.siacs.conversations.utils.SocksSocketFactory;
70import eu.siacs.conversations.utils.Xmlns;
71import eu.siacs.conversations.xml.Element;
72import eu.siacs.conversations.xml.Tag;
73import eu.siacs.conversations.xml.TagWriter;
74import eu.siacs.conversations.xml.XmlReader;
75import eu.siacs.conversations.xmpp.forms.Data;
76import eu.siacs.conversations.xmpp.forms.Field;
77import eu.siacs.conversations.xmpp.jid.InvalidJidException;
78import eu.siacs.conversations.xmpp.jid.Jid;
79import eu.siacs.conversations.xmpp.jingle.OnJinglePacketReceived;
80import eu.siacs.conversations.xmpp.jingle.stanzas.JinglePacket;
81import eu.siacs.conversations.xmpp.stanzas.AbstractAcknowledgeableStanza;
82import eu.siacs.conversations.xmpp.stanzas.AbstractStanza;
83import eu.siacs.conversations.xmpp.stanzas.IqPacket;
84import eu.siacs.conversations.xmpp.stanzas.MessagePacket;
85import eu.siacs.conversations.xmpp.stanzas.PresencePacket;
86import eu.siacs.conversations.xmpp.stanzas.csi.ActivePacket;
87import eu.siacs.conversations.xmpp.stanzas.csi.InactivePacket;
88import eu.siacs.conversations.xmpp.stanzas.streammgmt.AckPacket;
89import eu.siacs.conversations.xmpp.stanzas.streammgmt.EnablePacket;
90import eu.siacs.conversations.xmpp.stanzas.streammgmt.RequestPacket;
91import eu.siacs.conversations.xmpp.stanzas.streammgmt.ResumePacket;
92
93public class XmppConnection implements Runnable {
94
95 private static final int PACKET_IQ = 0;
96 private static final int PACKET_MESSAGE = 1;
97 private static final int PACKET_PRESENCE = 2;
98 protected Account account;
99 private final WakeLock wakeLock;
100 private Socket socket;
101 private XmlReader tagReader;
102 private TagWriter tagWriter;
103 private final Features features = new Features(this);
104 private boolean needsBinding = true;
105 private boolean shouldAuthenticate = true;
106 private Element streamFeatures;
107 private final HashMap<Jid, Info> disco = new HashMap<>();
108
109 private String streamId = null;
110 private int smVersion = 3;
111 private final SparseArray<AbstractAcknowledgeableStanza> mStanzaQueue = new SparseArray<>();
112
113 private int stanzasReceived = 0;
114 private int stanzasSent = 0;
115 private long lastPacketReceived = 0;
116 private long lastPingSent = 0;
117 private long lastConnect = 0;
118 private long lastSessionStarted = 0;
119 private long lastDiscoStarted = 0;
120 private int mPendingServiceDiscoveries = 0;
121 private final ArrayList<String> mPendingServiceDiscoveriesIds = new ArrayList<>();
122 private boolean mInteractive = false;
123 private int attempt = 0;
124 private final Hashtable<String, Pair<IqPacket, OnIqPacketReceived>> packetCallbacks = new Hashtable<>();
125 private OnPresencePacketReceived presenceListener = null;
126 private OnJinglePacketReceived jingleListener = null;
127 private OnIqPacketReceived unregisteredIqListener = null;
128 private OnMessagePacketReceived messageListener = null;
129 private OnStatusChanged statusListener = null;
130 private OnBindListener bindListener = null;
131 private final ArrayList<OnAdvancedStreamFeaturesLoaded> advancedStreamFeaturesLoadedListeners = new ArrayList<>();
132 private OnMessageAcknowledged acknowledgedListener = null;
133 private XmppConnectionService mXmppConnectionService = null;
134
135 private SaslMechanism saslMechanism;
136
137 private X509KeyManager mKeyManager = new X509KeyManager() {
138 @Override
139 public String chooseClientAlias(String[] strings, Principal[] principals, Socket socket) {
140 return account.getPrivateKeyAlias();
141 }
142
143 @Override
144 public String chooseServerAlias(String s, Principal[] principals, Socket socket) {
145 return null;
146 }
147
148 @Override
149 public X509Certificate[] getCertificateChain(String alias) {
150 try {
151 return KeyChain.getCertificateChain(mXmppConnectionService, alias);
152 } catch (Exception e) {
153 return new X509Certificate[0];
154 }
155 }
156
157 @Override
158 public String[] getClientAliases(String s, Principal[] principals) {
159 return new String[0];
160 }
161
162 @Override
163 public String[] getServerAliases(String s, Principal[] principals) {
164 return new String[0];
165 }
166
167 @Override
168 public PrivateKey getPrivateKey(String alias) {
169 try {
170 return KeyChain.getPrivateKey(mXmppConnectionService, alias);
171 } catch (Exception e) {
172 return null;
173 }
174 }
175 };
176 private Identity mServerIdentity = Identity.UNKNOWN;
177
178 private OnIqPacketReceived createPacketReceiveHandler() {
179 return new OnIqPacketReceived() {
180 @Override
181 public void onIqPacketReceived(Account account, IqPacket packet) {
182 if (packet.getType() == IqPacket.TYPE.RESULT) {
183 account.setOption(Account.OPTION_REGISTER,
184 false);
185 changeStatus(Account.State.REGISTRATION_SUCCESSFUL);
186 } else if (packet.hasChild("error")
187 && (packet.findChild("error")
188 .hasChild("conflict"))) {
189 changeStatus(Account.State.REGISTRATION_CONFLICT);
190 } else {
191 changeStatus(Account.State.REGISTRATION_FAILED);
192 Log.d(Config.LOGTAG, packet.toString());
193 }
194 disconnect(true);
195 }
196 };
197 }
198
199 public XmppConnection(final Account account, final XmppConnectionService service) {
200 this.account = account;
201 this.wakeLock = service.getPowerManager().newWakeLock(
202 PowerManager.PARTIAL_WAKE_LOCK, account.getJid().toBareJid().toString());
203 tagWriter = new TagWriter();
204 mXmppConnectionService = service;
205 }
206
207 protected void changeStatus(final Account.State nextStatus) {
208 if (account.getStatus() != nextStatus) {
209 if ((nextStatus == Account.State.OFFLINE)
210 && (account.getStatus() != Account.State.CONNECTING)
211 && (account.getStatus() != Account.State.ONLINE)
212 && (account.getStatus() != Account.State.DISABLED)) {
213 return;
214 }
215 if (nextStatus == Account.State.ONLINE) {
216 this.attempt = 0;
217 }
218 account.setStatus(nextStatus);
219 if (statusListener != null) {
220 statusListener.onStatusChanged(account);
221 }
222 }
223 }
224
225 protected void connect() {
226 Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": connecting");
227 features.encryptionEnabled = false;
228 lastConnect = SystemClock.elapsedRealtime();
229 lastPingSent = SystemClock.elapsedRealtime();
230 lastDiscoStarted = Long.MAX_VALUE;
231 this.attempt++;
232 if (account.getJid().getDomainpart().equals("chat.facebook.com")) {
233 mServerIdentity = Identity.FACEBOOK;
234 }
235 try {
236 shouldAuthenticate = needsBinding = !account.isOptionSet(Account.OPTION_REGISTER);
237 tagReader = new XmlReader(wakeLock);
238 tagWriter = new TagWriter();
239 this.changeStatus(Account.State.CONNECTING);
240 final boolean useTor = mXmppConnectionService.useTorToConnect() || account.isOnion();
241 if (useTor) {
242 String destination;
243 if (account.getHostname() == null || account.getHostname().isEmpty()) {
244 destination = account.getServer().toString();
245 } else {
246 destination = account.getHostname();
247 }
248 Log.d(Config.LOGTAG,account.getJid().toBareJid()+": connect to "+destination+" via TOR");
249 socket = SocksSocketFactory.createSocketOverTor(destination,account.getPort());
250 startXmpp();
251 } else if (DNSHelper.isIp(account.getServer().toString())) {
252 socket = new Socket();
253 try {
254 socket.connect(new InetSocketAddress(account.getServer().toString(), 5222), Config.SOCKET_TIMEOUT * 1000);
255 } catch (IOException e) {
256 throw new UnknownHostException();
257 }
258 startXmpp();
259 } else {
260 final Bundle result = DNSHelper.getSRVRecord(account.getServer(), mXmppConnectionService);
261 final ArrayList<Parcelable>values = result.getParcelableArrayList("values");
262 for(Iterator<Parcelable> iterator = values.iterator(); iterator.hasNext();) {
263 final Bundle namePort = (Bundle) iterator.next();
264 try {
265 String srvRecordServer;
266 try {
267 srvRecordServer = IDN.toASCII(namePort.getString("name"));
268 } catch (final IllegalArgumentException e) {
269 // TODO: Handle me?`
270 srvRecordServer = "";
271 }
272 final int srvRecordPort = namePort.getInt("port");
273 final String srvIpServer = namePort.getString("ip");
274 // if tls is true, encryption is implied and must not be started
275 features.encryptionEnabled = namePort.getBoolean("tls");
276 final InetSocketAddress addr;
277 if (srvIpServer != null) {
278 addr = new InetSocketAddress(srvIpServer, srvRecordPort);
279 Log.d(Config.LOGTAG, account.getJid().toBareJid().toString()
280 + ": using values from dns " + srvRecordServer
281 + "[" + srvIpServer + "]:" + srvRecordPort + " tls: " + features.encryptionEnabled);
282 } else {
283 addr = new InetSocketAddress(srvRecordServer, srvRecordPort);
284 Log.d(Config.LOGTAG, account.getJid().toBareJid().toString()
285 + ": using values from dns "
286 + srvRecordServer + ":" + srvRecordPort + " tls: " + features.encryptionEnabled);
287 }
288
289 if (!features.encryptionEnabled) {
290 socket = new Socket();
291 socket.connect(addr, Config.SOCKET_TIMEOUT * 1000);
292 } else {
293 final TlsFactoryVerifier tlsFactoryVerifier = getTlsFactoryVerifier();
294 socket = tlsFactoryVerifier.factory.createSocket();
295
296 if (socket == null) {
297 throw new IOException("could not initialize ssl socket");
298 }
299
300 setSSLSocketSecurity((SSLSocket) socket);
301 this.setSNIHost(tlsFactoryVerifier.factory, (SSLSocket) socket, account.getServer().getDomainpart());
302 this.setAlpnProtocol(tlsFactoryVerifier.factory, (SSLSocket) socket, "xmpp-client");
303
304 socket.connect(addr, Config.SOCKET_TIMEOUT * 1000);
305
306 if (!tlsFactoryVerifier.verifier.verify(account.getServer().getDomainpart(), ((SSLSocket) socket).getSession())) {
307 Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": TLS certificate verification failed");
308 throw new SecurityException();
309 }
310 }
311
312 if(startXmpp())
313 break; // successfully connected to server that speaks xmpp
314 } catch (final Throwable e) {
315 Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": " + e.getMessage() +"("+e.getClass().getName()+")");
316 if (!iterator.hasNext()) {
317 throw new UnknownHostException();
318 }
319 }
320 }
321 }
322 processStream();
323 } catch (final IncompatibleServerException e) {
324 this.changeStatus(Account.State.INCOMPATIBLE_SERVER);
325 } catch (final SecurityException e) {
326 this.changeStatus(Account.State.SECURITY_ERROR);
327 } catch (final UnauthorizedException e) {
328 this.changeStatus(Account.State.UNAUTHORIZED);
329 } catch (final UnknownHostException | ConnectException e) {
330 this.changeStatus(Account.State.SERVER_NOT_FOUND);
331 } catch (final SocksSocketFactory.SocksProxyNotFoundException e) {
332 this.changeStatus(Account.State.TOR_NOT_AVAILABLE);
333 } catch (final IOException | XmlPullParserException | NoSuchAlgorithmException e) {
334 Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": " + e.getMessage());
335 this.changeStatus(Account.State.OFFLINE);
336 this.attempt--; //don't count attempt when reconnecting instantly anyway
337 } finally {
338 if (socket != null) {
339 try {
340 socket.close();
341 } catch (IOException e) {
342
343 }
344 }
345 if (wakeLock.isHeld()) {
346 try {
347 wakeLock.release();
348 } catch (final RuntimeException ignored) {
349 }
350 }
351 }
352 }
353
354 /**
355 * Starts xmpp protocol, call after connecting to socket
356 * @return true if server returns with valid xmpp, false otherwise
357 * @throws IOException Unknown tag on connect
358 * @throws XmlPullParserException Bad Xml
359 * @throws NoSuchAlgorithmException Other error
360 */
361 private boolean startXmpp() throws IOException, XmlPullParserException, NoSuchAlgorithmException {
362 tagWriter.setOutputStream(socket.getOutputStream());
363 tagReader.setInputStream(socket.getInputStream());
364 tagWriter.beginDocument();
365 sendStartStream();
366 Tag nextTag;
367 while ((nextTag = tagReader.readTag()) != null) {
368 if (nextTag.isStart("stream")) {
369 return true;
370 } else {
371 throw new IOException("unknown tag on connect");
372 }
373 }
374 if (socket.isConnected()) {
375 socket.close();
376 }
377 return false;
378 }
379
380 private void setSNIHost(final SSLSocketFactory factory, final SSLSocket socket, final String hostname) {
381 if (factory instanceof android.net.SSLCertificateSocketFactory && android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) {
382 ((android.net.SSLCertificateSocketFactory) factory).setHostname(socket, hostname);
383 } else {
384 try {
385 socket.getClass().getMethod("setHostname", String.class).invoke(socket, hostname);
386 } catch (Throwable e) {
387 // ignore any error, we just can't set the hostname...
388 }
389 }
390 }
391
392 private void setAlpnProtocol(final SSLSocketFactory factory, final SSLSocket socket, final String protocol) {
393 try {
394 if (factory instanceof android.net.SSLCertificateSocketFactory && android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
395 // can't call directly because of @hide?
396 //((android.net.SSLCertificateSocketFactory)factory).setAlpnProtocols(new byte[][]{protocol.getBytes("UTF-8")});
397 android.net.SSLCertificateSocketFactory.class.getMethod("setAlpnProtocols", byte[][].class).invoke(socket, new Object[]{new byte[][]{protocol.getBytes("UTF-8")}});
398 } else {
399 final Method method = socket.getClass().getMethod("setAlpnProtocols", byte[].class);
400 // the concatenation of 8-bit, length prefixed protocol names, just one in our case...
401 // http://tools.ietf.org/html/draft-agl-tls-nextprotoneg-04#page-4
402 final byte[] protocolUTF8Bytes = protocol.getBytes("UTF-8");
403 final byte[] lengthPrefixedProtocols = new byte[protocolUTF8Bytes.length + 1];
404 lengthPrefixedProtocols[0] = (byte) protocol.length(); // cannot be over 255 anyhow
405 System.arraycopy(protocolUTF8Bytes, 0, lengthPrefixedProtocols, 1, protocolUTF8Bytes.length);
406 method.invoke(socket, new Object[]{lengthPrefixedProtocols});
407 }
408 } catch (Throwable e) {
409 // ignore any error, we just can't set the alpn protocol...
410 }
411 }
412
413 private static class TlsFactoryVerifier {
414 private final SSLSocketFactory factory;
415 private final HostnameVerifier verifier;
416
417 public TlsFactoryVerifier(final SSLSocketFactory factory, final HostnameVerifier verifier) throws IOException {
418 this.factory = factory;
419 this.verifier = verifier;
420 if (factory == null || verifier == null) {
421 throw new IOException("could not setup ssl");
422 }
423 }
424 }
425
426 private TlsFactoryVerifier getTlsFactoryVerifier() throws NoSuchAlgorithmException, KeyManagementException, IOException {
427 final SSLContext sc = SSLContext.getInstance("TLS");
428 MemorizingTrustManager trustManager = this.mXmppConnectionService.getMemorizingTrustManager();
429 KeyManager[] keyManager;
430 if (account.getPrivateKeyAlias() != null && account.getPassword().isEmpty()) {
431 keyManager = new KeyManager[]{mKeyManager};
432 } else {
433 keyManager = null;
434 }
435 sc.init(keyManager, new X509TrustManager[]{mInteractive ? trustManager : trustManager.getNonInteractive()}, mXmppConnectionService.getRNG());
436 final SSLSocketFactory factory = sc.getSocketFactory();
437 final HostnameVerifier verifier;
438 if (mInteractive) {
439 verifier = trustManager.wrapHostnameVerifier(new XmppDomainVerifier());
440 } else {
441 verifier = trustManager.wrapHostnameVerifierNonInteractive(new XmppDomainVerifier());
442 }
443
444 return new TlsFactoryVerifier(factory, verifier);
445 }
446
447 @Override
448 public void run() {
449 try {
450 if (socket != null) {
451 socket.close();
452 }
453 } catch (final IOException ignored) {
454
455 }
456 connect();
457 }
458
459 private void processStream() throws XmlPullParserException, IOException, NoSuchAlgorithmException {
460 Tag nextTag = tagReader.readTag();
461 while (nextTag != null && !nextTag.isEnd("stream")) {
462 if (nextTag.isStart("error")) {
463 processStreamError(nextTag);
464 } else if (nextTag.isStart("features")) {
465 processStreamFeatures(nextTag);
466 } else if (nextTag.isStart("proceed")) {
467 switchOverToTls(nextTag);
468 } else if (nextTag.isStart("success")) {
469 final String challenge = tagReader.readElement(nextTag).getContent();
470 try {
471 saslMechanism.getResponse(challenge);
472 } catch (final SaslMechanism.AuthenticationException e) {
473 disconnect(true);
474 Log.e(Config.LOGTAG, String.valueOf(e));
475 }
476 Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": logged in");
477 account.setKey(Account.PINNED_MECHANISM_KEY,
478 String.valueOf(saslMechanism.getPriority()));
479 tagReader.reset();
480 sendStartStream();
481 final Tag tag = tagReader.readTag();
482 if (tag != null && tag.isStart("stream")) {
483 processStream();
484 } else {
485 throw new IOException("server didn't restart stream after successful auth");
486 }
487 break;
488 } else if (nextTag.isStart("failure")) {
489 throw new UnauthorizedException();
490 } else if (nextTag.isStart("challenge")) {
491 final String challenge = tagReader.readElement(nextTag).getContent();
492 final Element response = new Element("response");
493 response.setAttribute("xmlns",
494 "urn:ietf:params:xml:ns:xmpp-sasl");
495 try {
496 response.setContent(saslMechanism.getResponse(challenge));
497 } catch (final SaslMechanism.AuthenticationException e) {
498 // TODO: Send auth abort tag.
499 Log.e(Config.LOGTAG, e.toString());
500 }
501 tagWriter.writeElement(response);
502 } else if (nextTag.isStart("enabled")) {
503 final Element enabled = tagReader.readElement(nextTag);
504 if ("true".equals(enabled.getAttribute("resume"))) {
505 this.streamId = enabled.getAttribute("id");
506 Log.d(Config.LOGTAG, account.getJid().toBareJid().toString()
507 + ": stream managment(" + smVersion
508 + ") enabled (resumable)");
509 } else {
510 Log.d(Config.LOGTAG, account.getJid().toBareJid().toString()
511 + ": stream management(" + smVersion + ") enabled");
512 }
513 this.stanzasReceived = 0;
514 final RequestPacket r = new RequestPacket(smVersion);
515 tagWriter.writeStanzaAsync(r);
516 } else if (nextTag.isStart("resumed")) {
517 lastPacketReceived = SystemClock.elapsedRealtime();
518 final Element resumed = tagReader.readElement(nextTag);
519 final String h = resumed.getAttribute("h");
520 try {
521 final int serverCount = Integer.parseInt(h);
522 if (serverCount != stanzasSent) {
523 Log.d(Config.LOGTAG, account.getJid().toBareJid().toString()
524 + ": session resumed with lost packages");
525 stanzasSent = serverCount;
526 } else {
527 Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": session resumed");
528 }
529 acknowledgeStanzaUpTo(serverCount);
530 ArrayList<AbstractAcknowledgeableStanza> failedStanzas = new ArrayList<>();
531 for(int i = 0; i < this.mStanzaQueue.size(); ++i) {
532 failedStanzas.add(mStanzaQueue.valueAt(i));
533 }
534 mStanzaQueue.clear();
535 Log.d(Config.LOGTAG,"resending "+failedStanzas.size()+" stanzas");
536 for(AbstractAcknowledgeableStanza packet : failedStanzas) {
537 if (packet instanceof MessagePacket) {
538 MessagePacket message = (MessagePacket) packet;
539 mXmppConnectionService.markMessage(account,
540 message.getTo().toBareJid(),
541 message.getId(),
542 Message.STATUS_UNSEND);
543 }
544 sendPacket(packet);
545 }
546 } catch (final NumberFormatException ignored) {
547 }
548 Log.d(Config.LOGTAG, account.getJid().toBareJid()+ ": online with resource " + account.getResource());
549 changeStatus(Account.State.ONLINE);
550 } else if (nextTag.isStart("r")) {
551 tagReader.readElement(nextTag);
552 if (Config.EXTENDED_SM_LOGGING) {
553 Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": acknowledging stanza #" + this.stanzasReceived);
554 }
555 final AckPacket ack = new AckPacket(this.stanzasReceived, smVersion);
556 tagWriter.writeStanzaAsync(ack);
557 } else if (nextTag.isStart("a")) {
558 final Element ack = tagReader.readElement(nextTag);
559 lastPacketReceived = SystemClock.elapsedRealtime();
560 try {
561 final int serverSequence = Integer.parseInt(ack.getAttribute("h"));
562 acknowledgeStanzaUpTo(serverSequence);
563 } catch (NumberFormatException e) {
564 Log.d(Config.LOGTAG,account.getJid().toBareJid()+": server send ack without sequence number");
565 }
566 } else if (nextTag.isStart("failed")) {
567 tagReader.readElement(nextTag);
568 Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": resumption failed");
569 streamId = null;
570 if (account.getStatus() != Account.State.ONLINE) {
571 sendBindRequest();
572 }
573 } else if (nextTag.isStart("iq")) {
574 processIq(nextTag);
575 } else if (nextTag.isStart("message")) {
576 processMessage(nextTag);
577 } else if (nextTag.isStart("presence")) {
578 processPresence(nextTag);
579 }
580 nextTag = tagReader.readTag();
581 }
582 throw new IOException("reached end of stream. last tag was "+nextTag);
583 }
584
585 private void acknowledgeStanzaUpTo(int serverCount) {
586 for (int i = 0; i < mStanzaQueue.size(); ++i) {
587 if (serverCount >= mStanzaQueue.keyAt(i)) {
588 if (Config.EXTENDED_SM_LOGGING) {
589 Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": server acknowledged stanza #" + mStanzaQueue.keyAt(i));
590 }
591 AbstractAcknowledgeableStanza stanza = mStanzaQueue.valueAt(i);
592 if (stanza instanceof MessagePacket && acknowledgedListener != null) {
593 MessagePacket packet = (MessagePacket) stanza;
594 acknowledgedListener.onMessageAcknowledged(account, packet.getId());
595 }
596 mStanzaQueue.removeAt(i);
597 i--;
598 }
599 }
600 }
601
602 private Element processPacket(final Tag currentTag, final int packetType)
603 throws XmlPullParserException, IOException {
604 Element element;
605 switch (packetType) {
606 case PACKET_IQ:
607 element = new IqPacket();
608 break;
609 case PACKET_MESSAGE:
610 element = new MessagePacket();
611 break;
612 case PACKET_PRESENCE:
613 element = new PresencePacket();
614 break;
615 default:
616 return null;
617 }
618 element.setAttributes(currentTag.getAttributes());
619 Tag nextTag = tagReader.readTag();
620 if (nextTag == null) {
621 throw new IOException("interrupted mid tag");
622 }
623 while (!nextTag.isEnd(element.getName())) {
624 if (!nextTag.isNo()) {
625 final Element child = tagReader.readElement(nextTag);
626 final String type = currentTag.getAttribute("type");
627 if (packetType == PACKET_IQ
628 && "jingle".equals(child.getName())
629 && ("set".equalsIgnoreCase(type) || "get"
630 .equalsIgnoreCase(type))) {
631 element = new JinglePacket();
632 element.setAttributes(currentTag.getAttributes());
633 }
634 element.addChild(child);
635 }
636 nextTag = tagReader.readTag();
637 if (nextTag == null) {
638 throw new IOException("interrupted mid tag");
639 }
640 }
641 if (stanzasReceived == Integer.MAX_VALUE) {
642 resetStreamId();
643 throw new IOException("time to restart the session. cant handle >2 billion pcks");
644 }
645 ++stanzasReceived;
646 lastPacketReceived = SystemClock.elapsedRealtime();
647 return element;
648 }
649
650 private void processIq(final Tag currentTag) throws XmlPullParserException, IOException {
651 final IqPacket packet = (IqPacket) processPacket(currentTag, PACKET_IQ);
652
653 if (packet.getId() == null) {
654 return; // an iq packet without id is definitely invalid
655 }
656
657 if (packet instanceof JinglePacket) {
658 if (this.jingleListener != null) {
659 this.jingleListener.onJinglePacketReceived(account,(JinglePacket) packet);
660 }
661 } else {
662 OnIqPacketReceived callback = null;
663 synchronized (this.packetCallbacks) {
664 if (packetCallbacks.containsKey(packet.getId())) {
665 final Pair<IqPacket, OnIqPacketReceived> packetCallbackDuple = packetCallbacks.get(packet.getId());
666 // Packets to the server should have responses from the server
667 if (packetCallbackDuple.first.toServer(account)) {
668 if (packet.fromServer(account) || mServerIdentity == Identity.FACEBOOK) {
669 callback = packetCallbackDuple.second;
670 packetCallbacks.remove(packet.getId());
671 } else {
672 Log.e(Config.LOGTAG, account.getJid().toBareJid().toString() + ": ignoring spoofed iq packet");
673 }
674 } else {
675 if (packet.getFrom().equals(packetCallbackDuple.first.getTo())) {
676 callback = packetCallbackDuple.second;
677 packetCallbacks.remove(packet.getId());
678 } else {
679 Log.e(Config.LOGTAG, account.getJid().toBareJid().toString() + ": ignoring spoofed iq packet");
680 }
681 }
682 } else if (packet.getType() == IqPacket.TYPE.GET || packet.getType() == IqPacket.TYPE.SET) {
683 callback = this.unregisteredIqListener;
684 }
685 }
686 if (callback != null) {
687 callback.onIqPacketReceived(account,packet);
688 }
689 }
690 }
691
692 private void processMessage(final Tag currentTag) throws XmlPullParserException, IOException {
693 final MessagePacket packet = (MessagePacket) processPacket(currentTag,PACKET_MESSAGE);
694 this.messageListener.onMessagePacketReceived(account, packet);
695 }
696
697 private void processPresence(final Tag currentTag) throws XmlPullParserException, IOException {
698 PresencePacket packet = (PresencePacket) processPacket(currentTag, PACKET_PRESENCE);
699 this.presenceListener.onPresencePacketReceived(account, packet);
700 }
701
702 private void sendStartTLS() throws IOException {
703 final Tag startTLS = Tag.empty("starttls");
704 startTLS.setAttribute("xmlns", "urn:ietf:params:xml:ns:xmpp-tls");
705 tagWriter.writeTag(startTLS);
706 }
707
708 private void setSSLSocketSecurity(final SSLSocket sslSocket) throws NoSuchAlgorithmException {
709 final String[] supportProtocols;
710 final Collection<String> supportedProtocols = new LinkedList<>(
711 Arrays.asList(sslSocket.getSupportedProtocols()));
712 supportedProtocols.remove("SSLv3");
713 supportProtocols = supportedProtocols.toArray(new String[supportedProtocols.size()]);
714
715 sslSocket.setEnabledProtocols(supportProtocols);
716
717 final String[] cipherSuites = CryptoHelper.getOrderedCipherSuites(
718 sslSocket.getSupportedCipherSuites());
719 //Log.d(Config.LOGTAG, "Using ciphers: " + Arrays.toString(cipherSuites));
720 if (cipherSuites.length > 0) {
721 sslSocket.setEnabledCipherSuites(cipherSuites);
722 }
723 }
724
725 private void switchOverToTls(final Tag currentTag) throws XmlPullParserException, IOException {
726 tagReader.readTag();
727 try {
728 final TlsFactoryVerifier tlsFactoryVerifier = getTlsFactoryVerifier();
729 final InetAddress address = socket == null ? null : socket.getInetAddress();
730
731 if (address == null) {
732 throw new IOException("could not setup ssl");
733 }
734
735 final SSLSocket sslSocket = (SSLSocket) tlsFactoryVerifier.factory.createSocket(socket, address.getHostAddress(), socket.getPort(), true);
736
737 if (sslSocket == null) {
738 throw new IOException("could not initialize ssl socket");
739 }
740
741 setSSLSocketSecurity(sslSocket);
742
743 if (!tlsFactoryVerifier.verifier.verify(account.getServer().getDomainpart(), sslSocket.getSession())) {
744 Log.d(Config.LOGTAG,account.getJid().toBareJid()+": TLS certificate verification failed");
745 throw new SecurityException();
746 }
747 tagReader.setInputStream(sslSocket.getInputStream());
748 tagWriter.setOutputStream(sslSocket.getOutputStream());
749 sendStartStream();
750 Log.d(Config.LOGTAG, account.getJid().toBareJid()+ ": TLS connection established");
751 features.encryptionEnabled = true;
752 final Tag tag = tagReader.readTag();
753 if (tag != null && tag.isStart("stream")) {
754 processStream();
755 } else {
756 throw new IOException("server didn't restart stream after STARTTLS");
757 }
758 sslSocket.close();
759 } catch (final NoSuchAlgorithmException | KeyManagementException e1) {
760 Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": TLS certificate verification failed");
761 throw new SecurityException();
762 }
763 }
764
765 private void processStreamFeatures(final Tag currentTag)
766 throws XmlPullParserException, IOException {
767 this.streamFeatures = tagReader.readElement(currentTag);
768 if (this.streamFeatures.hasChild("starttls") && !features.encryptionEnabled) {
769 sendStartTLS();
770 } else if (this.streamFeatures.hasChild("register")
771 && account.isOptionSet(Account.OPTION_REGISTER)
772 && features.encryptionEnabled) {
773 sendRegistryRequest();
774 } else if (!this.streamFeatures.hasChild("register")
775 && account.isOptionSet(Account.OPTION_REGISTER)) {
776 changeStatus(Account.State.REGISTRATION_NOT_SUPPORTED);
777 disconnect(true);
778 } else if (this.streamFeatures.hasChild("mechanisms")
779 && shouldAuthenticate && features.encryptionEnabled) {
780 final List<String> mechanisms = extractMechanisms(streamFeatures
781 .findChild("mechanisms"));
782 final Element auth = new Element("auth");
783 auth.setAttribute("xmlns", "urn:ietf:params:xml:ns:xmpp-sasl");
784 if (mechanisms.contains("EXTERNAL") && account.getPrivateKeyAlias() != null) {
785 saslMechanism = new External(tagWriter, account, mXmppConnectionService.getRNG());
786 } else if (mechanisms.contains("SCRAM-SHA-1")) {
787 saslMechanism = new ScramSha1(tagWriter, account, mXmppConnectionService.getRNG());
788 } else if (mechanisms.contains("PLAIN")) {
789 saslMechanism = new Plain(tagWriter, account);
790 } else if (mechanisms.contains("DIGEST-MD5")) {
791 saslMechanism = new DigestMd5(tagWriter, account, mXmppConnectionService.getRNG());
792 }
793 if (saslMechanism != null) {
794 final JSONObject keys = account.getKeys();
795 try {
796 if (keys.has(Account.PINNED_MECHANISM_KEY) &&
797 keys.getInt(Account.PINNED_MECHANISM_KEY) > saslMechanism.getPriority()) {
798 Log.e(Config.LOGTAG, "Auth failed. Authentication mechanism " + saslMechanism.getMechanism() +
799 " has lower priority (" + String.valueOf(saslMechanism.getPriority()) +
800 ") than pinned priority (" + keys.getInt(Account.PINNED_MECHANISM_KEY) +
801 "). Possible downgrade attack?");
802 throw new SecurityException();
803 }
804 } catch (final JSONException e) {
805 Log.d(Config.LOGTAG, "Parse error while checking pinned auth mechanism");
806 }
807 Log.d(Config.LOGTAG, account.getJid().toString() + ": Authenticating with " + saslMechanism.getMechanism());
808 auth.setAttribute("mechanism", saslMechanism.getMechanism());
809 if (!saslMechanism.getClientFirstMessage().isEmpty()) {
810 auth.setContent(saslMechanism.getClientFirstMessage());
811 }
812 tagWriter.writeElement(auth);
813 } else {
814 throw new IncompatibleServerException();
815 }
816 } else if (this.streamFeatures.hasChild("sm", "urn:xmpp:sm:" + smVersion) && streamId != null) {
817 if (Config.EXTENDED_SM_LOGGING) {
818 Log.d(Config.LOGTAG,account.getJid().toBareJid()+": resuming after stanza #"+stanzasReceived);
819 }
820 final ResumePacket resume = new ResumePacket(this.streamId, stanzasReceived, smVersion);
821 this.tagWriter.writeStanzaAsync(resume);
822 } else if (needsBinding) {
823 if (this.streamFeatures.hasChild("bind")) {
824 sendBindRequest();
825 } else {
826 throw new IncompatibleServerException();
827 }
828 }
829 }
830
831 private List<String> extractMechanisms(final Element stream) {
832 final ArrayList<String> mechanisms = new ArrayList<>(stream
833 .getChildren().size());
834 for (final Element child : stream.getChildren()) {
835 mechanisms.add(child.getContent());
836 }
837 return mechanisms;
838 }
839
840 public void sendCaptchaRegistryRequest(String id, Data data) {
841 if (data == null) {
842 setAccountCreationFailed("");
843 } else {
844 IqPacket request = getIqGenerator().generateCreateAccountWithCaptcha(account, id, data);
845 sendIqPacket(request, createPacketReceiveHandler());
846 }
847 }
848
849 private void sendRegistryRequest() {
850 final IqPacket register = new IqPacket(IqPacket.TYPE.GET);
851 register.query("jabber:iq:register");
852 register.setTo(account.getServer());
853 sendIqPacket(register, new OnIqPacketReceived() {
854
855 @Override
856 public void onIqPacketReceived(final Account account, final IqPacket packet) {
857 boolean failed = false;
858 if (packet.getType() == IqPacket.TYPE.RESULT
859 && packet.query().hasChild("username")
860 && (packet.query().hasChild("password"))) {
861 final IqPacket register = new IqPacket(IqPacket.TYPE.SET);
862 final Element username = new Element("username").setContent(account.getUsername());
863 final Element password = new Element("password").setContent(account.getPassword());
864 register.query("jabber:iq:register").addChild(username);
865 register.query().addChild(password);
866 sendIqPacket(register, createPacketReceiveHandler());
867 } else if (packet.getType() == IqPacket.TYPE.RESULT
868 && (packet.query().hasChild("x", "jabber:x:data"))) {
869 final Data data = Data.parse(packet.query().findChild("x", "jabber:x:data"));
870 final Element blob = packet.query().findChild("data", "urn:xmpp:bob");
871 final String id = packet.getId();
872
873 Bitmap captcha = null;
874 if (blob != null) {
875 try {
876 final String base64Blob = blob.getContent();
877 final byte[] strBlob = Base64.decode(base64Blob, Base64.DEFAULT);
878 InputStream stream = new ByteArrayInputStream(strBlob);
879 captcha = BitmapFactory.decodeStream(stream);
880 } catch (Exception e) {
881 //ignored
882 }
883 } else {
884 try {
885 Field url = data.getFieldByName("url");
886 String urlString = url.findChildContent("value");
887 URL uri = new URL(urlString);
888 captcha = BitmapFactory.decodeStream(uri.openConnection().getInputStream());
889 } catch (IOException e) {
890 Log.e(Config.LOGTAG, e.toString());
891 }
892 }
893
894 if (captcha != null) {
895 failed = !mXmppConnectionService.displayCaptchaRequest(account, id, data, captcha);
896 }
897 } else {
898 failed = true;
899 }
900
901 if (failed) {
902 final Element instructions = packet.query().findChild("instructions");
903 setAccountCreationFailed((instructions != null) ? instructions.getContent() : "");
904 }
905 }
906 });
907 }
908
909 private void setAccountCreationFailed(String instructions) {
910 changeStatus(Account.State.REGISTRATION_FAILED);
911 disconnect(true);
912 Log.d(Config.LOGTAG, account.getJid().toBareJid()
913 + ": could not register. instructions are"
914 + instructions);
915 }
916
917 private void sendBindRequest() {
918 while(!mXmppConnectionService.areMessagesInitialized() && socket != null && !socket.isClosed()) {
919 try {
920 Thread.sleep(500);
921 } catch (final InterruptedException ignored) {
922 }
923 }
924 needsBinding = false;
925 clearIqCallbacks();
926 final IqPacket iq = new IqPacket(IqPacket.TYPE.SET);
927 iq.addChild("bind", "urn:ietf:params:xml:ns:xmpp-bind")
928 .addChild("resource").setContent(account.getResource());
929 this.sendUnmodifiedIqPacket(iq, new OnIqPacketReceived() {
930 @Override
931 public void onIqPacketReceived(final Account account, final IqPacket packet) {
932 if (packet.getType() == IqPacket.TYPE.TIMEOUT) {
933 return;
934 }
935 final Element bind = packet.findChild("bind");
936 if (bind != null && packet.getType() == IqPacket.TYPE.RESULT) {
937 final Element jid = bind.findChild("jid");
938 if (jid != null && jid.getContent() != null) {
939 try {
940 account.setResource(Jid.fromString(jid.getContent()).getResourcepart());
941 } catch (final InvalidJidException e) {
942 // TODO: Handle the case where an external JID is technically invalid?
943 }
944 if (streamFeatures.hasChild("session")) {
945 sendStartSession();
946 } else {
947 sendPostBindInitialization();
948 }
949 } else {
950 Log.d(Config.LOGTAG, account.getJid() + ": disconnecting because of bind failure");
951 disconnect(true);
952 }
953 } else {
954 Log.d(Config.LOGTAG, account.getJid() + ": disconnecting because of bind failure");
955 disconnect(true);
956 }
957 }
958 });
959 }
960
961 private void clearIqCallbacks() {
962 final IqPacket failurePacket = new IqPacket(IqPacket.TYPE.TIMEOUT);
963 final ArrayList<OnIqPacketReceived> callbacks = new ArrayList<>();
964 synchronized (this.packetCallbacks) {
965 if (this.packetCallbacks.size() == 0) {
966 return;
967 }
968 Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": clearing "+this.packetCallbacks.size()+" iq callbacks");
969 final Iterator<Pair<IqPacket, OnIqPacketReceived>> iterator = this.packetCallbacks.values().iterator();
970 while (iterator.hasNext()) {
971 Pair<IqPacket, OnIqPacketReceived> entry = iterator.next();
972 callbacks.add(entry.second);
973 iterator.remove();
974 }
975 }
976 for(OnIqPacketReceived callback : callbacks) {
977 callback.onIqPacketReceived(account,failurePacket);
978 }
979 Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": done clearing iq callbacks. " + this.packetCallbacks.size() + " left");
980 }
981
982 public void sendDiscoTimeout() {
983 final IqPacket failurePacket = new IqPacket(IqPacket.TYPE.ERROR); //don't use timeout
984 final ArrayList<OnIqPacketReceived> callbacks = new ArrayList<>();
985 synchronized (this.mPendingServiceDiscoveriesIds) {
986 for(String id : mPendingServiceDiscoveriesIds) {
987 synchronized (this.packetCallbacks) {
988 Pair<IqPacket, OnIqPacketReceived> pair = this.packetCallbacks.remove(id);
989 if (pair != null) {
990 callbacks.add(pair.second);
991 }
992 }
993 }
994 this.mPendingServiceDiscoveriesIds.clear();
995 }
996 if (callbacks.size() > 0) {
997 Log.d(Config.LOGTAG,account.getJid().toBareJid()+": sending disco timeout");
998 resetStreamId(); //we don't want to live with this for ever
999 }
1000 for(OnIqPacketReceived callback : callbacks) {
1001 callback.onIqPacketReceived(account,failurePacket);
1002 }
1003 }
1004
1005 private void sendStartSession() {
1006 final IqPacket startSession = new IqPacket(IqPacket.TYPE.SET);
1007 startSession.addChild("session", "urn:ietf:params:xml:ns:xmpp-session");
1008 this.sendUnmodifiedIqPacket(startSession, new OnIqPacketReceived() {
1009 @Override
1010 public void onIqPacketReceived(Account account, IqPacket packet) {
1011 if (packet.getType() == IqPacket.TYPE.RESULT) {
1012 sendPostBindInitialization();
1013 } else if (packet.getType() != IqPacket.TYPE.TIMEOUT) {
1014 Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": could not init sessions");
1015 disconnect(true);
1016 }
1017 }
1018 });
1019 }
1020
1021 private void sendPostBindInitialization() {
1022 smVersion = 0;
1023 if (streamFeatures.hasChild("sm", "urn:xmpp:sm:3")) {
1024 smVersion = 3;
1025 } else if (streamFeatures.hasChild("sm", "urn:xmpp:sm:2")) {
1026 smVersion = 2;
1027 }
1028 if (smVersion != 0) {
1029 final EnablePacket enable = new EnablePacket(smVersion);
1030 tagWriter.writeStanzaAsync(enable);
1031 stanzasSent = 0;
1032 mStanzaQueue.clear();
1033 }
1034 features.carbonsEnabled = false;
1035 features.blockListRequested = false;
1036 synchronized (this.disco) {
1037 this.disco.clear();
1038 }
1039 mPendingServiceDiscoveries = 0;
1040 lastDiscoStarted = SystemClock.elapsedRealtime();
1041 Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": starting service discovery");
1042 mXmppConnectionService.scheduleWakeUpCall(Config.CONNECT_DISCO_TIMEOUT, account.getUuid().hashCode());
1043 sendServiceDiscoveryItems(account.getServer());
1044 sendServiceDiscoveryInfo(account.getServer());
1045 sendServiceDiscoveryInfo(account.getJid().toBareJid());
1046 this.lastSessionStarted = SystemClock.elapsedRealtime();
1047 }
1048
1049 private void sendServiceDiscoveryInfo(final Jid jid) {
1050 mPendingServiceDiscoveries++;
1051 final IqPacket iq = new IqPacket(IqPacket.TYPE.GET);
1052 iq.setTo(jid);
1053 iq.query("http://jabber.org/protocol/disco#info");
1054 String id = this.sendIqPacket(iq, new OnIqPacketReceived() {
1055
1056 @Override
1057 public void onIqPacketReceived(final Account account, final IqPacket packet) {
1058 if (packet.getType() == IqPacket.TYPE.RESULT) {
1059 boolean advancedStreamFeaturesLoaded = false;
1060 synchronized (XmppConnection.this.disco) {
1061 final List<Element> elements = packet.query().getChildren();
1062 final Info info = new Info();
1063 for (final Element element : elements) {
1064 if (element.getName().equals("identity")) {
1065 String type = element.getAttribute("type");
1066 String category = element.getAttribute("category");
1067 String name = element.getAttribute("name");
1068 if (type != null && category != null) {
1069 info.identities.add(new Pair<>(category, type));
1070 if (type.equals("im") && category.equals("server")) {
1071 if (name != null && jid.equals(account.getServer())) {
1072 switch (name) {
1073 case "Prosody":
1074 mServerIdentity = Identity.PROSODY;
1075 break;
1076 case "ejabberd":
1077 mServerIdentity = Identity.EJABBERD;
1078 break;
1079 case "Slack-XMPP":
1080 mServerIdentity = Identity.SLACK;
1081 break;
1082 }
1083 Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": server name: " + name);
1084 }
1085 }
1086 }
1087 } else if (element.getName().equals("feature")) {
1088 info.features.add(element.getAttribute("var"));
1089 }
1090 }
1091 disco.put(jid, info);
1092 advancedStreamFeaturesLoaded = disco.containsKey(account.getServer())
1093 && disco.containsKey(account.getJid().toBareJid());
1094 }
1095 if (advancedStreamFeaturesLoaded && (jid.equals(account.getServer()) || jid.equals(account.getJid().toBareJid()))) {
1096 enableAdvancedStreamFeatures();
1097 }
1098 } else {
1099 Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": could not query disco info for " + jid.toString());
1100 }
1101 if (packet.getType() != IqPacket.TYPE.TIMEOUT) {
1102 mPendingServiceDiscoveries--;
1103 if (mPendingServiceDiscoveries <= 0) {
1104 Log.d(Config.LOGTAG,account.getJid().toBareJid()+": done with service discovery");
1105 Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": online with resource " + account.getResource());
1106 changeStatus(Account.State.ONLINE);
1107 if (bindListener != null) {
1108 bindListener.onBind(account);
1109 }
1110 }
1111 }
1112 }
1113 });
1114 synchronized (this.mPendingServiceDiscoveriesIds) {
1115 this.mPendingServiceDiscoveriesIds.add(id);
1116 }
1117 }
1118
1119 private void enableAdvancedStreamFeatures() {
1120 if (getFeatures().carbons() && !features.carbonsEnabled) {
1121 sendEnableCarbons();
1122 }
1123 if (getFeatures().blocking() && !features.blockListRequested) {
1124 Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": Requesting block list");
1125 this.sendIqPacket(getIqGenerator().generateGetBlockList(), mXmppConnectionService.getIqParser());
1126 }
1127 for (final OnAdvancedStreamFeaturesLoaded listener : advancedStreamFeaturesLoadedListeners) {
1128 listener.onAdvancedStreamFeaturesAvailable(account);
1129 }
1130 }
1131
1132 private void sendServiceDiscoveryItems(final Jid server) {
1133 final IqPacket iq = new IqPacket(IqPacket.TYPE.GET);
1134 iq.setTo(server.toDomainJid());
1135 iq.query("http://jabber.org/protocol/disco#items");
1136 this.sendIqPacket(iq, new OnIqPacketReceived() {
1137
1138 @Override
1139 public void onIqPacketReceived(final Account account, final IqPacket packet) {
1140 if (packet.getType() == IqPacket.TYPE.RESULT) {
1141 final List<Element> elements = packet.query().getChildren();
1142 for (final Element element : elements) {
1143 if (element.getName().equals("item")) {
1144 final Jid jid = element.getAttributeAsJid("jid");
1145 if (jid != null && !jid.equals(account.getServer())) {
1146 sendServiceDiscoveryInfo(jid);
1147 }
1148 }
1149 }
1150 } else {
1151 Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": could not query disco items of " + server);
1152 }
1153 }
1154 });
1155 }
1156
1157 private void sendEnableCarbons() {
1158 final IqPacket iq = new IqPacket(IqPacket.TYPE.SET);
1159 iq.addChild("enable", "urn:xmpp:carbons:2");
1160 this.sendIqPacket(iq, new OnIqPacketReceived() {
1161
1162 @Override
1163 public void onIqPacketReceived(final Account account, final IqPacket packet) {
1164 if (!packet.hasChild("error")) {
1165 Log.d(Config.LOGTAG, account.getJid().toBareJid()
1166 + ": successfully enabled carbons");
1167 features.carbonsEnabled = true;
1168 } else {
1169 Log.d(Config.LOGTAG, account.getJid().toBareJid()
1170 + ": error enableing carbons " + packet.toString());
1171 }
1172 }
1173 });
1174 }
1175
1176 private void processStreamError(final Tag currentTag)
1177 throws XmlPullParserException, IOException {
1178 final Element streamError = tagReader.readElement(currentTag);
1179 if (streamError != null && streamError.hasChild("conflict")) {
1180 final String resource = account.getResource().split("\\.")[0];
1181 account.setResource(resource + "." + nextRandomId());
1182 Log.d(Config.LOGTAG,
1183 account.getJid().toBareJid() + ": switching resource due to conflict ("
1184 + account.getResource() + ")");
1185 } else if (streamError != null) {
1186 Log.d(Config.LOGTAG,account.getJid().toBareJid()+": stream error "+streamError.toString());
1187 }
1188 }
1189
1190 private void sendStartStream() throws IOException {
1191 final Tag stream = Tag.start("stream:stream");
1192 stream.setAttribute("to", account.getServer().toString());
1193 stream.setAttribute("version", "1.0");
1194 stream.setAttribute("xml:lang", "en");
1195 stream.setAttribute("xmlns", "jabber:client");
1196 stream.setAttribute("xmlns:stream", "http://etherx.jabber.org/streams");
1197 tagWriter.writeTag(stream);
1198 }
1199
1200 private String nextRandomId() {
1201 return new BigInteger(50, mXmppConnectionService.getRNG()).toString(32);
1202 }
1203
1204 public String sendIqPacket(final IqPacket packet, final OnIqPacketReceived callback) {
1205 packet.setFrom(account.getJid());
1206 return this.sendUnmodifiedIqPacket(packet, callback);
1207 }
1208
1209 private synchronized String sendUnmodifiedIqPacket(final IqPacket packet, final OnIqPacketReceived callback) {
1210 if (packet.getId() == null) {
1211 final String id = nextRandomId();
1212 packet.setAttribute("id", id);
1213 }
1214 if (callback != null) {
1215 synchronized (this.packetCallbacks) {
1216 packetCallbacks.put(packet.getId(), new Pair<>(packet, callback));
1217 }
1218 }
1219 this.sendPacket(packet);
1220 return packet.getId();
1221 }
1222
1223 public void sendMessagePacket(final MessagePacket packet) {
1224 this.sendPacket(packet);
1225 }
1226
1227 public void sendPresencePacket(final PresencePacket packet) {
1228 this.sendPacket(packet);
1229 }
1230
1231 private synchronized void sendPacket(final AbstractStanza packet) {
1232 if (stanzasSent == Integer.MAX_VALUE) {
1233 resetStreamId();
1234 disconnect(true);
1235 return;
1236 }
1237 tagWriter.writeStanzaAsync(packet);
1238 if (packet instanceof AbstractAcknowledgeableStanza) {
1239 AbstractAcknowledgeableStanza stanza = (AbstractAcknowledgeableStanza) packet;
1240 ++stanzasSent;
1241 this.mStanzaQueue.put(stanzasSent, stanza);
1242 if (stanza instanceof MessagePacket && stanza.getId() != null && getFeatures().sm()) {
1243 if (Config.EXTENDED_SM_LOGGING) {
1244 Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": requesting ack for message stanza #" + stanzasSent);
1245 }
1246 tagWriter.writeStanzaAsync(new RequestPacket(this.smVersion));
1247 }
1248 }
1249 }
1250
1251 public void sendPing() {
1252 if (!r()) {
1253 final IqPacket iq = new IqPacket(IqPacket.TYPE.GET);
1254 iq.setFrom(account.getJid());
1255 iq.addChild("ping", "urn:xmpp:ping");
1256 this.sendIqPacket(iq, null);
1257 }
1258 this.lastPingSent = SystemClock.elapsedRealtime();
1259 }
1260
1261 public void setOnMessagePacketReceivedListener(
1262 final OnMessagePacketReceived listener) {
1263 this.messageListener = listener;
1264 }
1265
1266 public void setOnUnregisteredIqPacketReceivedListener(
1267 final OnIqPacketReceived listener) {
1268 this.unregisteredIqListener = listener;
1269 }
1270
1271 public void setOnPresencePacketReceivedListener(
1272 final OnPresencePacketReceived listener) {
1273 this.presenceListener = listener;
1274 }
1275
1276 public void setOnJinglePacketReceivedListener(
1277 final OnJinglePacketReceived listener) {
1278 this.jingleListener = listener;
1279 }
1280
1281 public void setOnStatusChangedListener(final OnStatusChanged listener) {
1282 this.statusListener = listener;
1283 }
1284
1285 public void setOnBindListener(final OnBindListener listener) {
1286 this.bindListener = listener;
1287 }
1288
1289 public void setOnMessageAcknowledgeListener(final OnMessageAcknowledged listener) {
1290 this.acknowledgedListener = listener;
1291 }
1292
1293 public void addOnAdvancedStreamFeaturesAvailableListener(final OnAdvancedStreamFeaturesLoaded listener) {
1294 if (!this.advancedStreamFeaturesLoadedListeners.contains(listener)) {
1295 this.advancedStreamFeaturesLoadedListeners.add(listener);
1296 }
1297 }
1298
1299 public void disconnect(final boolean force) {
1300 Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": disconnecting force="+Boolean.valueOf(force));
1301 if (force) {
1302 try {
1303 socket.close();
1304 } catch(Exception e) {
1305 Log.d(Config.LOGTAG,account.getJid().toBareJid().toString()+": exception during force close ("+e.getMessage()+")");
1306 }
1307 return;
1308 } else {
1309 resetStreamId();
1310 if (tagWriter.isActive()) {
1311 tagWriter.finish();
1312 try {
1313 int i = 0;
1314 boolean warned = false;
1315 while (!tagWriter.finished() && socket.isConnected() && i <= 10) {
1316 if (!warned) {
1317 Log.d(Config.LOGTAG, account.getJid().toBareJid()+": waiting for tag writer to finish");
1318 warned = true;
1319 }
1320 Thread.sleep(200);
1321 i++;
1322 }
1323 if (warned) {
1324 Log.d(Config.LOGTAG,account.getJid().toBareJid()+": tag writer has finished");
1325 }
1326 Log.d(Config.LOGTAG,account.getJid().toBareJid()+": closing stream");
1327 tagWriter.writeTag(Tag.end("stream:stream"));
1328 } catch (final IOException e) {
1329 Log.d(Config.LOGTAG,account.getJid().toBareJid()+": io exception during disconnect ("+e.getMessage()+")");
1330 } catch (final InterruptedException e) {
1331 Log.d(Config.LOGTAG, "interrupted");
1332 }
1333 }
1334 }
1335 }
1336
1337 public void resetStreamId() {
1338 this.streamId = null;
1339 }
1340
1341 public List<Jid> findDiscoItemsByFeature(final String feature) {
1342 synchronized (this.disco) {
1343 final List<Jid> items = new ArrayList<>();
1344 for (final Entry<Jid, Info> cursor : this.disco.entrySet()) {
1345 if (cursor.getValue().features.contains(feature)) {
1346 items.add(cursor.getKey());
1347 }
1348 }
1349 return items;
1350 }
1351 }
1352
1353 public Jid findDiscoItemByFeature(final String feature) {
1354 final List<Jid> items = findDiscoItemsByFeature(feature);
1355 if (items.size() >= 1) {
1356 return items.get(0);
1357 }
1358 return null;
1359 }
1360
1361 public boolean r() {
1362 if (getFeatures().sm()) {
1363 this.tagWriter.writeStanzaAsync(new RequestPacket(smVersion));
1364 return true;
1365 } else {
1366 return false;
1367 }
1368 }
1369
1370 public String getMucServer() {
1371 synchronized (this.disco) {
1372 for (final Entry<Jid, Info> cursor : disco.entrySet()) {
1373 final Info value = cursor.getValue();
1374 if (value.features.contains("http://jabber.org/protocol/muc")
1375 && !value.features.contains("jabber:iq:gateway")
1376 && !value.identities.contains(new Pair<>("conference", "irc"))) {
1377 return cursor.getKey().toString();
1378 }
1379 }
1380 }
1381 return null;
1382 }
1383
1384 public int getTimeToNextAttempt() {
1385 final int interval = (int) (25 * Math.pow(1.5, attempt));
1386 final int secondsSinceLast = (int) ((SystemClock.elapsedRealtime() - this.lastConnect) / 1000);
1387 return interval - secondsSinceLast;
1388 }
1389
1390 public int getAttempt() {
1391 return this.attempt;
1392 }
1393
1394 public Features getFeatures() {
1395 return this.features;
1396 }
1397
1398 public long getLastSessionEstablished() {
1399 final long diff = SystemClock.elapsedRealtime() - this.lastSessionStarted;
1400 return System.currentTimeMillis() - diff;
1401 }
1402
1403 public long getLastConnect() {
1404 return this.lastConnect;
1405 }
1406
1407 public long getLastPingSent() {
1408 return this.lastPingSent;
1409 }
1410
1411 public long getLastDiscoStarted() {
1412 return this.lastDiscoStarted;
1413 }
1414 public long getLastPacketReceived() {
1415 return this.lastPacketReceived;
1416 }
1417
1418 public void sendActive() {
1419 this.sendPacket(new ActivePacket());
1420 }
1421
1422 public void sendInactive() {
1423 this.sendPacket(new InactivePacket());
1424 }
1425
1426 public void resetAttemptCount() {
1427 this.attempt = 0;
1428 this.lastConnect = 0;
1429 }
1430
1431 public void setInteractive(boolean interactive) {
1432 this.mInteractive = interactive;
1433 }
1434
1435 public Identity getServerIdentity() {
1436 return mServerIdentity;
1437 }
1438
1439 private class Info {
1440 public final ArrayList<String> features = new ArrayList<>();
1441 public final ArrayList<Pair<String,String>> identities = new ArrayList<>();
1442 }
1443
1444 private class UnauthorizedException extends IOException {
1445
1446 }
1447
1448 private class SecurityException extends IOException {
1449
1450 }
1451
1452 private class IncompatibleServerException extends IOException {
1453
1454 }
1455
1456 public enum Identity {
1457 FACEBOOK,
1458 SLACK,
1459 EJABBERD,
1460 PROSODY,
1461 UNKNOWN
1462 }
1463
1464 public class Features {
1465 XmppConnection connection;
1466 private boolean carbonsEnabled = false;
1467 private boolean encryptionEnabled = false;
1468 private boolean blockListRequested = false;
1469
1470 public Features(final XmppConnection connection) {
1471 this.connection = connection;
1472 }
1473
1474 private boolean hasDiscoFeature(final Jid server, final String feature) {
1475 synchronized (XmppConnection.this.disco) {
1476 return connection.disco.containsKey(server) &&
1477 connection.disco.get(server).features.contains(feature);
1478 }
1479 }
1480
1481 public boolean carbons() {
1482 return hasDiscoFeature(account.getServer(), "urn:xmpp:carbons:2");
1483 }
1484
1485 public boolean blocking() {
1486 return hasDiscoFeature(account.getServer(), Xmlns.BLOCKING);
1487 }
1488
1489 public boolean register() {
1490 return hasDiscoFeature(account.getServer(), Xmlns.REGISTER);
1491 }
1492
1493 public boolean sm() {
1494 return streamId != null
1495 || (connection.streamFeatures != null && connection.streamFeatures.hasChild("sm"));
1496 }
1497
1498 public boolean csi() {
1499 return connection.streamFeatures != null && connection.streamFeatures.hasChild("csi", "urn:xmpp:csi:0");
1500 }
1501
1502 public boolean pep() {
1503 synchronized (XmppConnection.this.disco) {
1504 final Pair<String, String> needle = new Pair<>("pubsub", "pep");
1505 Info info = disco.get(account.getServer());
1506 if (info != null && info.identities.contains(needle)) {
1507 return true;
1508 } else {
1509 info = disco.get(account.getJid().toBareJid());
1510 return info != null && info.identities.contains(needle);
1511 }
1512 }
1513 }
1514
1515 public boolean mam() {
1516 if (hasDiscoFeature(account.getJid().toBareJid(), "urn:xmpp:mam:0")) {
1517 return true;
1518 } else {
1519 return hasDiscoFeature(account.getServer(), "urn:xmpp:mam:0");
1520 }
1521 }
1522
1523 public boolean advancedStreamFeaturesLoaded() {
1524 synchronized (XmppConnection.this.disco) {
1525 return disco.containsKey(account.getServer());
1526 }
1527 }
1528
1529 public boolean rosterVersioning() {
1530 return connection.streamFeatures != null && connection.streamFeatures.hasChild("ver");
1531 }
1532
1533 public void setBlockListRequested(boolean value) {
1534 this.blockListRequested = value;
1535 }
1536
1537 public boolean httpUpload() {
1538 return !Config.DISABLE_HTTP_UPLOAD && findDiscoItemsByFeature(Xmlns.HTTP_UPLOAD).size() > 0;
1539 }
1540 }
1541
1542 private IqGenerator getIqGenerator() {
1543 return mXmppConnectionService.getIqGenerator();
1544 }
1545}