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.utils.Xmlns;
  72import eu.siacs.conversations.xml.Element;
  73import eu.siacs.conversations.xml.Tag;
  74import eu.siacs.conversations.xml.TagWriter;
  75import eu.siacs.conversations.xml.XmlReader;
  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		mPendingServiceDiscoveries.set(0);
1092		mWaitForDisco.set(smVersion != 0 && !account.getJid().getDomainpart().equalsIgnoreCase("nimbuzz.com"));
1093		lastDiscoStarted = SystemClock.elapsedRealtime();
1094		Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": starting service discovery");
1095		mXmppConnectionService.scheduleWakeUpCall(Config.CONNECT_DISCO_TIMEOUT, account.getUuid().hashCode());
1096		Element caps = streamFeatures.findChild("c");
1097		final String hash = caps == null ? null : caps.getAttribute("hash");
1098		final String ver = caps == null ? null : caps.getAttribute("ver");
1099		ServiceDiscoveryResult discoveryResult = null;
1100		if (hash != null && ver != null) {
1101			discoveryResult = mXmppConnectionService.getCachedServiceDiscoveryResult(new Pair<>(hash, ver));
1102		}
1103		if (discoveryResult == null) {
1104			sendServiceDiscoveryInfo(account.getServer());
1105		} else {
1106			Log.d(Config.LOGTAG,account.getJid().toBareJid()+": server caps came from cache");
1107			disco.put(account.getServer(), discoveryResult);
1108		}
1109		sendServiceDiscoveryInfo(account.getJid().toBareJid());
1110		sendServiceDiscoveryItems(account.getServer());
1111
1112		if (!mWaitForDisco.get()) {
1113			finalizeBind();
1114		}
1115		this.lastSessionStarted = SystemClock.elapsedRealtime();
1116	}
1117
1118	private void sendServiceDiscoveryInfo(final Jid jid) {
1119		mPendingServiceDiscoveries.incrementAndGet();
1120		final IqPacket iq = new IqPacket(IqPacket.TYPE.GET);
1121		iq.setTo(jid);
1122		iq.query("http://jabber.org/protocol/disco#info");
1123		this.sendIqPacket(iq, new OnIqPacketReceived() {
1124
1125			@Override
1126			public void onIqPacketReceived(final Account account, final IqPacket packet) {
1127				if (packet.getType() == IqPacket.TYPE.RESULT) {
1128					boolean advancedStreamFeaturesLoaded;
1129					synchronized (XmppConnection.this.disco) {
1130						ServiceDiscoveryResult result = new ServiceDiscoveryResult(packet);
1131						if (jid.equals(account.getServer())) {
1132							mXmppConnectionService.databaseBackend.insertDiscoveryResult(result);
1133						}
1134						disco.put(jid, result);
1135						advancedStreamFeaturesLoaded = disco.containsKey(account.getServer())
1136								&& disco.containsKey(account.getJid().toBareJid());
1137					}
1138					if (advancedStreamFeaturesLoaded && (jid.equals(account.getServer()) || jid.equals(account.getJid().toBareJid()))) {
1139						enableAdvancedStreamFeatures();
1140					}
1141				} else {
1142					Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": could not query disco info for " + jid.toString());
1143				}
1144				if (packet.getType() != IqPacket.TYPE.TIMEOUT) {
1145					if (mPendingServiceDiscoveries.decrementAndGet() == 0
1146							&& mWaitForDisco.compareAndSet(true, false)) {
1147						finalizeBind();
1148					}
1149				}
1150			}
1151		});
1152	}
1153
1154	private void finalizeBind() {
1155		Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": online with resource " + account.getResource());
1156		if (bindListener != null) {
1157			bindListener.onBind(account);
1158		}
1159		changeStatus(Account.State.ONLINE);
1160	}
1161
1162	private void enableAdvancedStreamFeatures() {
1163		if (getFeatures().carbons() && !features.carbonsEnabled) {
1164			sendEnableCarbons();
1165		}
1166		if (getFeatures().blocking() && !features.blockListRequested) {
1167			Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": Requesting block list");
1168			this.sendIqPacket(getIqGenerator().generateGetBlockList(), mXmppConnectionService.getIqParser());
1169		}
1170		for (final OnAdvancedStreamFeaturesLoaded listener : advancedStreamFeaturesLoadedListeners) {
1171			listener.onAdvancedStreamFeaturesAvailable(account);
1172		}
1173	}
1174
1175	private void sendServiceDiscoveryItems(final Jid server) {
1176		mPendingServiceDiscoveries.incrementAndGet();
1177		final IqPacket iq = new IqPacket(IqPacket.TYPE.GET);
1178		iq.setTo(server.toDomainJid());
1179		iq.query("http://jabber.org/protocol/disco#items");
1180		this.sendIqPacket(iq, new OnIqPacketReceived() {
1181
1182			@Override
1183			public void onIqPacketReceived(final Account account, final IqPacket packet) {
1184				if (packet.getType() == IqPacket.TYPE.RESULT) {
1185					final List<Element> elements = packet.query().getChildren();
1186					for (final Element element : elements) {
1187						if (element.getName().equals("item")) {
1188							final Jid jid = element.getAttributeAsJid("jid");
1189							if (jid != null && !jid.equals(account.getServer())) {
1190								sendServiceDiscoveryInfo(jid);
1191							}
1192						}
1193					}
1194				} else {
1195					Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": could not query disco items of " + server);
1196				}
1197				if (packet.getType() != IqPacket.TYPE.TIMEOUT) {
1198					if (mPendingServiceDiscoveries.decrementAndGet() == 0
1199							&& mWaitForDisco.compareAndSet(true, false)) {
1200						finalizeBind();
1201					}
1202				}
1203			}
1204		});
1205	}
1206
1207	private void sendEnableCarbons() {
1208		final IqPacket iq = new IqPacket(IqPacket.TYPE.SET);
1209		iq.addChild("enable", "urn:xmpp:carbons:2");
1210		this.sendIqPacket(iq, new OnIqPacketReceived() {
1211
1212			@Override
1213			public void onIqPacketReceived(final Account account, final IqPacket packet) {
1214				if (!packet.hasChild("error")) {
1215					Log.d(Config.LOGTAG, account.getJid().toBareJid()
1216							+ ": successfully enabled carbons");
1217					features.carbonsEnabled = true;
1218				} else {
1219					Log.d(Config.LOGTAG, account.getJid().toBareJid()
1220							+ ": error enableing carbons " + packet.toString());
1221				}
1222			}
1223		});
1224	}
1225
1226	private void processStreamError(final Tag currentTag)
1227		throws XmlPullParserException, IOException {
1228		final Element streamError = tagReader.readElement(currentTag);
1229		if (streamError == null) {
1230			return;
1231		}
1232		if (streamError.hasChild("conflict")) {
1233			final String resource = account.getResource().split("\\.")[0];
1234			account.setResource(resource + "." + nextRandomId());
1235			Log.d(Config.LOGTAG,
1236					account.getJid().toBareJid() + ": switching resource due to conflict ("
1237					+ account.getResource() + ")");
1238			throw new IOException();
1239		} else if (streamError.hasChild("host-unknown")) {
1240			throw new StreamErrorHostUnknown();
1241		} else if (streamError.hasChild("policy-violation")) {
1242			throw new StreamErrorPolicyViolation();
1243		} else {
1244			Log.d(Config.LOGTAG,account.getJid().toBareJid()+": stream error "+streamError.toString());
1245			throw new StreamError();
1246		}
1247	}
1248
1249	private void sendStartStream() throws IOException {
1250		final Tag stream = Tag.start("stream:stream");
1251		stream.setAttribute("to", account.getServer().toString());
1252		stream.setAttribute("version", "1.0");
1253		stream.setAttribute("xml:lang", "en");
1254		stream.setAttribute("xmlns", "jabber:client");
1255		stream.setAttribute("xmlns:stream", "http://etherx.jabber.org/streams");
1256		tagWriter.writeTag(stream);
1257	}
1258
1259	private String nextRandomId() {
1260		return new BigInteger(50, mXmppConnectionService.getRNG()).toString(36);
1261	}
1262
1263	public String sendIqPacket(final IqPacket packet, final OnIqPacketReceived callback) {
1264		packet.setFrom(account.getJid());
1265		return this.sendUnmodifiedIqPacket(packet, callback);
1266	}
1267
1268	public synchronized String sendUnmodifiedIqPacket(final IqPacket packet, final OnIqPacketReceived callback) {
1269		if (packet.getId() == null) {
1270			final String id = nextRandomId();
1271			packet.setAttribute("id", id);
1272		}
1273		if (callback != null) {
1274			synchronized (this.packetCallbacks) {
1275				packetCallbacks.put(packet.getId(), new Pair<>(packet, callback));
1276			}
1277		}
1278		this.sendPacket(packet);
1279		return packet.getId();
1280	}
1281
1282	public void sendMessagePacket(final MessagePacket packet) {
1283		this.sendPacket(packet);
1284	}
1285
1286	public void sendPresencePacket(final PresencePacket packet) {
1287		this.sendPacket(packet);
1288	}
1289
1290	private synchronized void sendPacket(final AbstractStanza packet) {
1291		if (stanzasSent == Integer.MAX_VALUE) {
1292			resetStreamId();
1293			disconnect(true);
1294			return;
1295		}
1296		synchronized (this.mStanzaQueue) {
1297			tagWriter.writeStanzaAsync(packet);
1298			if (packet instanceof AbstractAcknowledgeableStanza) {
1299				AbstractAcknowledgeableStanza stanza = (AbstractAcknowledgeableStanza) packet;
1300				++stanzasSent;
1301				this.mStanzaQueue.append(stanzasSent, stanza);
1302				if (stanza instanceof MessagePacket && stanza.getId() != null && getFeatures().sm()) {
1303					if (Config.EXTENDED_SM_LOGGING) {
1304						Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": requesting ack for message stanza #" + stanzasSent);
1305					}
1306					tagWriter.writeStanzaAsync(new RequestPacket(this.smVersion));
1307				}
1308			}
1309		}
1310	}
1311
1312	public void sendPing() {
1313		if (!r()) {
1314			final IqPacket iq = new IqPacket(IqPacket.TYPE.GET);
1315			iq.setFrom(account.getJid());
1316			iq.addChild("ping", "urn:xmpp:ping");
1317			this.sendIqPacket(iq, null);
1318		}
1319		this.lastPingSent = SystemClock.elapsedRealtime();
1320	}
1321
1322	public void setOnMessagePacketReceivedListener(
1323			final OnMessagePacketReceived listener) {
1324		this.messageListener = listener;
1325			}
1326
1327	public void setOnUnregisteredIqPacketReceivedListener(
1328			final OnIqPacketReceived listener) {
1329		this.unregisteredIqListener = listener;
1330			}
1331
1332	public void setOnPresencePacketReceivedListener(
1333			final OnPresencePacketReceived listener) {
1334		this.presenceListener = listener;
1335			}
1336
1337	public void setOnJinglePacketReceivedListener(
1338			final OnJinglePacketReceived listener) {
1339		this.jingleListener = listener;
1340			}
1341
1342	public void setOnStatusChangedListener(final OnStatusChanged listener) {
1343		this.statusListener = listener;
1344	}
1345
1346	public void setOnBindListener(final OnBindListener listener) {
1347		this.bindListener = listener;
1348	}
1349
1350	public void setOnMessageAcknowledgeListener(final OnMessageAcknowledged listener) {
1351		this.acknowledgedListener = listener;
1352	}
1353
1354	public void addOnAdvancedStreamFeaturesAvailableListener(final OnAdvancedStreamFeaturesLoaded listener) {
1355		if (!this.advancedStreamFeaturesLoadedListeners.contains(listener)) {
1356			this.advancedStreamFeaturesLoadedListeners.add(listener);
1357		}
1358	}
1359
1360	private void forceCloseSocket() {
1361		if (socket != null) {
1362			try {
1363				socket.close();
1364			} catch (IOException e) {
1365				Log.d(Config.LOGTAG,account.getJid().toBareJid()+": io exception "+e.getMessage()+" during force close");
1366			}
1367		} else {
1368			Log.d(Config.LOGTAG,account.getJid().toBareJid()+": socket was null during force close");
1369		}
1370	}
1371
1372	public void interrupt() {
1373		Thread.currentThread().interrupt();
1374	}
1375
1376	public void disconnect(final boolean force) {
1377		interrupt();
1378		Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": disconnecting force="+Boolean.valueOf(force));
1379		if (force) {
1380			forceCloseSocket();
1381		} else {
1382			if (tagWriter.isActive()) {
1383				tagWriter.finish();
1384				try {
1385					int i = 0;
1386					boolean warned = false;
1387					while (!tagWriter.finished() && socket.isConnected() && i <= 10) {
1388						if (!warned) {
1389							Log.d(Config.LOGTAG, account.getJid().toBareJid()+": waiting for tag writer to finish");
1390							warned = true;
1391						}
1392						try {
1393							Thread.sleep(200);
1394						} catch(InterruptedException e) {
1395							Log.d(Config.LOGTAG,account.getJid().toBareJid()+": sleep interrupted");
1396						}
1397						i++;
1398					}
1399					if (warned) {
1400						Log.d(Config.LOGTAG,account.getJid().toBareJid()+": tag writer has finished");
1401					}
1402					Log.d(Config.LOGTAG,account.getJid().toBareJid()+": closing stream");
1403					tagWriter.writeTag(Tag.end("stream:stream"));
1404				} catch (final IOException e) {
1405					Log.d(Config.LOGTAG,account.getJid().toBareJid()+": io exception during disconnect ("+e.getMessage()+")");
1406				} finally {
1407					forceCloseSocket();
1408				}
1409			}
1410		}
1411	}
1412
1413	public void resetStreamId() {
1414		this.streamId = null;
1415	}
1416
1417	private List<Entry<Jid, ServiceDiscoveryResult>> findDiscoItemsByFeature(final String feature) {
1418		synchronized (this.disco) {
1419			final List<Entry<Jid, ServiceDiscoveryResult>> items = new ArrayList<>();
1420			for (final Entry<Jid, ServiceDiscoveryResult> cursor : this.disco.entrySet()) {
1421				if (cursor.getValue().getFeatures().contains(feature)) {
1422					items.add(cursor);
1423				}
1424			}
1425			return items;
1426		}
1427	}
1428
1429	public Jid findDiscoItemByFeature(final String feature) {
1430		final List<Entry<Jid, ServiceDiscoveryResult>> items = findDiscoItemsByFeature(feature);
1431		if (items.size() >= 1) {
1432			return items.get(0).getKey();
1433		}
1434		return null;
1435	}
1436
1437	public boolean r() {
1438		if (getFeatures().sm()) {
1439			this.tagWriter.writeStanzaAsync(new RequestPacket(smVersion));
1440			return true;
1441		} else {
1442			return false;
1443		}
1444	}
1445
1446	public String getMucServer() {
1447		synchronized (this.disco) {
1448			for (final Entry<Jid, ServiceDiscoveryResult> cursor : disco.entrySet()) {
1449				final ServiceDiscoveryResult value = cursor.getValue();
1450				if (value.getFeatures().contains("http://jabber.org/protocol/muc")
1451						&& !value.getFeatures().contains("jabber:iq:gateway")
1452						&& !value.hasIdentity("conference", "irc")) {
1453					return cursor.getKey().toString();
1454				}
1455			}
1456		}
1457		return null;
1458	}
1459
1460	public int getTimeToNextAttempt() {
1461		final int interval = Math.min((int) (25 * Math.pow(1.3, attempt)), 300);
1462		final int secondsSinceLast = (int) ((SystemClock.elapsedRealtime() - this.lastConnect) / 1000);
1463		return interval - secondsSinceLast;
1464	}
1465
1466	public int getAttempt() {
1467		return this.attempt;
1468	}
1469
1470	public Features getFeatures() {
1471		return this.features;
1472	}
1473
1474	public long getLastSessionEstablished() {
1475		final long diff = SystemClock.elapsedRealtime() - this.lastSessionStarted;
1476		return System.currentTimeMillis() - diff;
1477	}
1478
1479	public long getLastConnect() {
1480		return this.lastConnect;
1481	}
1482
1483	public long getLastPingSent() {
1484		return this.lastPingSent;
1485	}
1486
1487	public long getLastDiscoStarted() {
1488		return this.lastDiscoStarted;
1489	}
1490	public long getLastPacketReceived() {
1491		return this.lastPacketReceived;
1492	}
1493
1494	public void sendActive() {
1495		this.sendPacket(new ActivePacket());
1496	}
1497
1498	public void sendInactive() {
1499		this.sendPacket(new InactivePacket());
1500	}
1501
1502	public void resetAttemptCount(boolean resetConnectTime) {
1503		this.attempt = 0;
1504		if (resetConnectTime) {
1505			this.lastConnect = 0;
1506		}
1507	}
1508
1509	public void setInteractive(boolean interactive) {
1510		this.mInteractive = interactive;
1511	}
1512
1513	public Identity getServerIdentity() {
1514		synchronized (this.disco) {
1515			ServiceDiscoveryResult result = disco.get(account.getJid().toDomainJid());
1516			if (result == null) {
1517				return Identity.UNKNOWN;
1518			}
1519			for (final ServiceDiscoveryResult.Identity id : result.getIdentities()) {
1520				if (id.getType().equals("im") && id.getCategory().equals("server") && id.getName() != null) {
1521					switch (id.getName()) {
1522						case "Prosody":
1523							return Identity.PROSODY;
1524						case "ejabberd":
1525							return Identity.EJABBERD;
1526						case "Slack-XMPP":
1527							return Identity.SLACK;
1528					}
1529				}
1530			}
1531		}
1532		return Identity.UNKNOWN;
1533	}
1534
1535	private class UnauthorizedException extends IOException {
1536
1537	}
1538
1539	private class SecurityException extends IOException {
1540
1541	}
1542
1543	private class IncompatibleServerException extends IOException {
1544
1545	}
1546
1547	private class StreamErrorHostUnknown extends StreamError {
1548
1549	}
1550
1551	private class StreamErrorPolicyViolation extends StreamError {
1552
1553	}
1554
1555	private class StreamError extends IOException {
1556
1557	}
1558
1559	private class PaymentRequiredException extends IOException {
1560
1561	}
1562
1563	private class RegistrationNotSupportedException extends IOException {
1564
1565	}
1566
1567	public enum Identity {
1568		FACEBOOK,
1569		SLACK,
1570		EJABBERD,
1571		PROSODY,
1572		NIMBUZZ,
1573		UNKNOWN
1574	}
1575
1576	public class Features {
1577		XmppConnection connection;
1578		private boolean carbonsEnabled = false;
1579		private boolean encryptionEnabled = false;
1580		private boolean blockListRequested = false;
1581
1582		public Features(final XmppConnection connection) {
1583			this.connection = connection;
1584		}
1585
1586		private boolean hasDiscoFeature(final Jid server, final String feature) {
1587			synchronized (XmppConnection.this.disco) {
1588				return connection.disco.containsKey(server) &&
1589						connection.disco.get(server).getFeatures().contains(feature);
1590			}
1591		}
1592
1593		public boolean carbons() {
1594			return hasDiscoFeature(account.getServer(), "urn:xmpp:carbons:2");
1595		}
1596
1597		public boolean blocking() {
1598			return hasDiscoFeature(account.getServer(), Xmlns.BLOCKING);
1599		}
1600
1601		public boolean spamReporting() {
1602			return hasDiscoFeature(account.getServer(), "urn:xmpp:reporting:reason:spam:0");
1603		}
1604
1605		public boolean register() {
1606			return hasDiscoFeature(account.getServer(), Xmlns.REGISTER);
1607		}
1608
1609		public boolean sm() {
1610			return streamId != null
1611					|| (connection.streamFeatures != null && connection.streamFeatures.hasChild("sm"));
1612		}
1613
1614		public boolean csi() {
1615			return connection.streamFeatures != null && connection.streamFeatures.hasChild("csi", "urn:xmpp:csi:0");
1616		}
1617
1618		public boolean pep() {
1619			synchronized (XmppConnection.this.disco) {
1620				ServiceDiscoveryResult info = disco.get(account.getJid().toBareJid());
1621				return info != null && info.hasIdentity("pubsub", "pep");
1622			}
1623		}
1624
1625		public boolean pepPersistent() {
1626			synchronized (XmppConnection.this.disco) {
1627				ServiceDiscoveryResult info = disco.get(account.getJid().toBareJid());
1628				return info != null && info.getFeatures().contains("http://jabber.org/protocol/pubsub#persistent-items");
1629			}
1630		}
1631
1632		public boolean mam() {
1633			return hasDiscoFeature(account.getJid().toBareJid(), Xmlns.MAM)
1634					|| hasDiscoFeature(account.getJid().toBareJid(), Xmlns.MAM_LAGECY);
1635		}
1636
1637		public boolean mamLegacy() {
1638			return !hasDiscoFeature(account.getJid().toBareJid(),Xmlns.MAM)
1639					&& hasDiscoFeature(account.getJid().toBareJid(),Xmlns.MAM_LAGECY);
1640		}
1641
1642		public boolean push() {
1643			return hasDiscoFeature(account.getJid().toBareJid(), "urn:xmpp:push:0")
1644					|| hasDiscoFeature(account.getServer(), "urn:xmpp:push:0");
1645		}
1646
1647		public boolean rosterVersioning() {
1648			return connection.streamFeatures != null && connection.streamFeatures.hasChild("ver");
1649		}
1650
1651		public void setBlockListRequested(boolean value) {
1652			this.blockListRequested = value;
1653		}
1654
1655		public boolean httpUpload(long filesize) {
1656			if (Config.DISABLE_HTTP_UPLOAD) {
1657				return false;
1658			} else {
1659				List<Entry<Jid, ServiceDiscoveryResult>> items = findDiscoItemsByFeature(Xmlns.HTTP_UPLOAD);
1660				if (items.size() > 0) {
1661					try {
1662						long maxsize = Long.parseLong(items.get(0).getValue().getExtendedDiscoInformation(Xmlns.HTTP_UPLOAD, "max-file-size"));
1663						if(filesize <= maxsize) {
1664							return true;
1665						} else {
1666							Log.d(Config.LOGTAG,account.getJid().toBareJid()+": http upload is not available for files with size "+filesize+" (max is "+maxsize+")");
1667							return false;
1668						}
1669					} catch (Exception e) {
1670						return true;
1671					}
1672				} else {
1673					return false;
1674				}
1675			}
1676		}
1677
1678		public long getMaxHttpUploadSize() {
1679			List<Entry<Jid, ServiceDiscoveryResult>> items = findDiscoItemsByFeature(Xmlns.HTTP_UPLOAD);
1680				if (items.size() > 0) {
1681					try {
1682						return Long.parseLong(items.get(0).getValue().getExtendedDiscoInformation(Xmlns.HTTP_UPLOAD, "max-file-size"));
1683					} catch (Exception e) {
1684						return -1;
1685					}
1686				} else {
1687					return -1;
1688				}
1689		}
1690
1691		public boolean stanzaIds() {
1692			return hasDiscoFeature(account.getJid().toBareJid(),Xmlns.STANZA_IDS);
1693		}
1694	}
1695
1696	private IqGenerator getIqGenerator() {
1697		return mXmppConnectionService.getIqGenerator();
1698	}
1699}