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