XmppConnection.java

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