1package eu.siacs.conversations.xmpp;
2
3import android.content.Context;
4import android.content.SharedPreferences;
5import android.os.Bundle;
6import android.os.Parcelable;
7import android.os.PowerManager;
8import android.os.PowerManager.WakeLock;
9import android.os.SystemClock;
10import android.preference.PreferenceManager;
11import android.util.Log;
12import android.util.Pair;
13import android.util.SparseArray;
14
15import org.apache.http.conn.ssl.StrictHostnameVerifier;
16import org.json.JSONException;
17import org.json.JSONObject;
18import org.xmlpull.v1.XmlPullParserException;
19
20import java.io.IOException;
21import java.io.InputStream;
22import java.io.OutputStream;
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.security.KeyManagementException;
31import java.security.NoSuchAlgorithmException;
32import java.util.ArrayList;
33import java.util.Arrays;
34import java.util.Collection;
35import java.util.HashMap;
36import java.util.Hashtable;
37import java.util.LinkedList;
38import java.util.List;
39import java.util.Map;
40import java.util.Map.Entry;
41
42import javax.net.ssl.HostnameVerifier;
43import javax.net.ssl.SSLContext;
44import javax.net.ssl.SSLSocket;
45import javax.net.ssl.SSLSocketFactory;
46import javax.net.ssl.X509TrustManager;
47
48import eu.siacs.conversations.Config;
49import eu.siacs.conversations.crypto.sasl.DigestMd5;
50import eu.siacs.conversations.crypto.sasl.Plain;
51import eu.siacs.conversations.crypto.sasl.SaslMechanism;
52import eu.siacs.conversations.crypto.sasl.ScramSha1;
53import eu.siacs.conversations.entities.Account;
54import eu.siacs.conversations.generator.IqGenerator;
55import eu.siacs.conversations.services.XmppConnectionService;
56import eu.siacs.conversations.utils.DNSHelper;
57import eu.siacs.conversations.utils.Xmlns;
58import eu.siacs.conversations.xml.Element;
59import eu.siacs.conversations.xml.Tag;
60import eu.siacs.conversations.xml.TagWriter;
61import eu.siacs.conversations.xml.XmlReader;
62import eu.siacs.conversations.xmpp.jid.InvalidJidException;
63import eu.siacs.conversations.xmpp.jid.Jid;
64import eu.siacs.conversations.xmpp.jingle.OnJinglePacketReceived;
65import eu.siacs.conversations.xmpp.jingle.stanzas.JinglePacket;
66import eu.siacs.conversations.xmpp.stanzas.AbstractStanza;
67import eu.siacs.conversations.xmpp.stanzas.IqPacket;
68import eu.siacs.conversations.xmpp.stanzas.MessagePacket;
69import eu.siacs.conversations.xmpp.stanzas.PresencePacket;
70import eu.siacs.conversations.xmpp.stanzas.csi.ActivePacket;
71import eu.siacs.conversations.xmpp.stanzas.csi.InactivePacket;
72import eu.siacs.conversations.xmpp.stanzas.streammgmt.AckPacket;
73import eu.siacs.conversations.xmpp.stanzas.streammgmt.EnablePacket;
74import eu.siacs.conversations.xmpp.stanzas.streammgmt.RequestPacket;
75import eu.siacs.conversations.xmpp.stanzas.streammgmt.ResumePacket;
76
77public class XmppConnection implements Runnable {
78
79 private static final int PACKET_IQ = 0;
80 private static final int PACKET_MESSAGE = 1;
81 private static final int PACKET_PRESENCE = 2;
82 private final Context applicationContext;
83 protected Account account;
84 private final WakeLock wakeLock;
85 private Socket socket;
86 private XmlReader tagReader;
87 private TagWriter tagWriter;
88 private final Features features = new Features(this);
89 private boolean shouldBind = true;
90 private boolean shouldAuthenticate = true;
91 private Element streamFeatures;
92 private final HashMap<String, List<String>> disco = new HashMap<>();
93
94 private String streamId = null;
95 private int smVersion = 3;
96 private final SparseArray<String> messageReceipts = new SparseArray<>();
97
98 private boolean enabledEncryption = false;
99 private boolean enabledCarbons = false;
100
101 private int stanzasReceived = 0;
102 private int stanzasSent = 0;
103 private long lastPaketReceived = 0;
104 private long lastPingSent = 0;
105 private long lastConnect = 0;
106 private long lastSessionStarted = 0;
107 private int attempt = 0;
108 private final Map<String, Pair<IqPacket, OnIqPacketReceived>> packetCallbacks = new Hashtable<>();
109 private OnPresencePacketReceived presenceListener = null;
110 private OnJinglePacketReceived jingleListener = null;
111 private OnIqPacketReceived unregisteredIqListener = null;
112 private OnMessagePacketReceived messageListener = null;
113 private OnStatusChanged statusListener = null;
114 private OnBindListener bindListener = null;
115 private final ArrayList<OnAdvancedStreamFeaturesLoaded> advancedStreamFeaturesLoadedListeners = new ArrayList<>();
116 private OnMessageAcknowledged acknowledgedListener = null;
117 private XmppConnectionService mXmppConnectionService = null;
118
119 private SaslMechanism saslMechanism;
120
121 public XmppConnection(final Account account, final XmppConnectionService service) {
122 this.account = account;
123 this.wakeLock = service.getPowerManager().newWakeLock(
124 PowerManager.PARTIAL_WAKE_LOCK, account.getJid().toBareJid().toString());
125 tagWriter = new TagWriter();
126 mXmppConnectionService = service;
127 applicationContext = service.getApplicationContext();
128 }
129
130 protected void changeStatus(final Account.State nextStatus) {
131 if (account.getStatus() != nextStatus) {
132 if ((nextStatus == Account.State.OFFLINE)
133 && (account.getStatus() != Account.State.CONNECTING)
134 && (account.getStatus() != Account.State.ONLINE)
135 && (account.getStatus() != Account.State.DISABLED)) {
136 return;
137 }
138 if (nextStatus == Account.State.ONLINE) {
139 this.attempt = 0;
140 }
141 account.setStatus(nextStatus);
142 if (statusListener != null) {
143 statusListener.onStatusChanged(account);
144 }
145 }
146 }
147
148 protected void connect() {
149 Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": connecting");
150 enabledEncryption = false;
151 lastConnect = SystemClock.elapsedRealtime();
152 lastPingSent = SystemClock.elapsedRealtime();
153 this.attempt++;
154 try {
155 shouldAuthenticate = shouldBind = !account
156 .isOptionSet(Account.OPTION_REGISTER);
157 tagReader = new XmlReader(wakeLock);
158 tagWriter = new TagWriter();
159 packetCallbacks.clear();
160 this.changeStatus(Account.State.CONNECTING);
161 final Bundle result = DNSHelper.getSRVRecord(account.getServer());
162 final ArrayList<Parcelable> values = result.getParcelableArrayList("values");
163 if ("timeout".equals(result.getString("error"))) {
164 throw new IOException("timeout in dns");
165 } else if (values != null) {
166 int i = 0;
167 boolean socketError = true;
168 while (socketError && values.size() > i) {
169 final Bundle namePort = (Bundle) values.get(i);
170 try {
171 String srvRecordServer;
172 try {
173 srvRecordServer=IDN.toASCII(namePort.getString("name"));
174 } catch (final IllegalArgumentException e) {
175 // TODO: Handle me?`
176 srvRecordServer = "";
177 }
178 final int srvRecordPort = namePort.getInt("port");
179 final String srvIpServer = namePort.getString("ip");
180 final InetSocketAddress addr;
181 if (srvIpServer != null) {
182 addr = new InetSocketAddress(srvIpServer, srvRecordPort);
183 Log.d(Config.LOGTAG, account.getJid().toBareJid().toString()
184 + ": using values from dns " + srvRecordServer
185 + "[" + srvIpServer + "]:" + srvRecordPort);
186 } else {
187 addr = new InetSocketAddress(srvRecordServer, srvRecordPort);
188 Log.d(Config.LOGTAG, account.getJid().toBareJid().toString()
189 + ": using values from dns "
190 + srvRecordServer + ":" + srvRecordPort);
191 }
192 socket = new Socket();
193 socket.connect(addr, 20000);
194 socketError = false;
195 } catch (final UnknownHostException e) {
196 Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": " + e.getMessage());
197 i++;
198 } catch (final IOException e) {
199 Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": " + e.getMessage());
200 i++;
201 }
202 }
203 if (socketError) {
204 throw new UnknownHostException();
205 }
206 } else if (result.containsKey("error")
207 && "nosrv".equals(result.getString("error", null))) {
208 socket = new Socket(account.getServer().getDomainpart(), 5222);
209 } else {
210 throw new IOException("timeout in dns");
211 }
212 final OutputStream out = socket.getOutputStream();
213 tagWriter.setOutputStream(out);
214 final InputStream in = socket.getInputStream();
215 tagReader.setInputStream(in);
216 tagWriter.beginDocument();
217 sendStartStream();
218 Tag nextTag;
219 while ((nextTag = tagReader.readTag()) != null) {
220 if (nextTag.isStart("stream")) {
221 processStream(nextTag);
222 break;
223 } else {
224 throw new IOException("unknown tag on connect");
225 }
226 }
227 if (socket.isConnected()) {
228 socket.close();
229 }
230 } catch (final UnknownHostException | ConnectException e) {
231 this.changeStatus(Account.State.SERVER_NOT_FOUND);
232 } catch (final IOException | XmlPullParserException | NoSuchAlgorithmException e) {
233 Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": " + e.getMessage());
234 this.changeStatus(Account.State.OFFLINE);
235 } finally {
236 if (wakeLock.isHeld()) {
237 try {
238 wakeLock.release();
239 } catch (final RuntimeException ignored) {
240 }
241 }
242 }
243 }
244
245 @Override
246 public void run() {
247 connect();
248 }
249
250 private void processStream(final Tag currentTag) throws XmlPullParserException,
251 IOException, NoSuchAlgorithmException {
252 Tag nextTag = tagReader.readTag();
253
254 while ((nextTag != null) && (!nextTag.isEnd("stream"))) {
255 if (nextTag.isStart("error")) {
256 processStreamError(nextTag);
257 } else if (nextTag.isStart("features")) {
258 processStreamFeatures(nextTag);
259 } else if (nextTag.isStart("proceed")) {
260 switchOverToTls(nextTag);
261 } else if (nextTag.isStart("success")) {
262 final String challenge = tagReader.readElement(nextTag).getContent();
263 try {
264 saslMechanism.getResponse(challenge);
265 } catch (final SaslMechanism.AuthenticationException e) {
266 disconnect(true);
267 Log.e(Config.LOGTAG, String.valueOf(e));
268 }
269 Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": logged in");
270 account.setKey(Account.PINNED_MECHANISM_KEY,
271 String.valueOf(saslMechanism.getPriority()));
272 tagReader.reset();
273 sendStartStream();
274 processStream(tagReader.readTag());
275 break;
276 } else if (nextTag.isStart("failure")) {
277 tagReader.readElement(nextTag);
278 changeStatus(Account.State.UNAUTHORIZED);
279 } else if (nextTag.isStart("challenge")) {
280 final String challenge = tagReader.readElement(nextTag).getContent();
281 final Element response = new Element("response");
282 response.setAttribute("xmlns",
283 "urn:ietf:params:xml:ns:xmpp-sasl");
284 try {
285 response.setContent(saslMechanism.getResponse(challenge));
286 } catch (final SaslMechanism.AuthenticationException e) {
287 // TODO: Send auth abort tag.
288 Log.e(Config.LOGTAG, e.toString());
289 }
290 tagWriter.writeElement(response);
291 } else if (nextTag.isStart("enabled")) {
292 final Element enabled = tagReader.readElement(nextTag);
293 if ("true".equals(enabled.getAttribute("resume"))) {
294 this.streamId = enabled.getAttribute("id");
295 Log.d(Config.LOGTAG, account.getJid().toBareJid().toString()
296 + ": stream managment(" + smVersion
297 + ") enabled (resumable)");
298 } else {
299 Log.d(Config.LOGTAG, account.getJid().toBareJid().toString()
300 + ": stream managment(" + smVersion + ") enabled");
301 }
302 this.lastSessionStarted = SystemClock.elapsedRealtime();
303 this.stanzasReceived = 0;
304 final RequestPacket r = new RequestPacket(smVersion);
305 tagWriter.writeStanzaAsync(r);
306 } else if (nextTag.isStart("resumed")) {
307 lastPaketReceived = SystemClock.elapsedRealtime();
308 final Element resumed = tagReader.readElement(nextTag);
309 final String h = resumed.getAttribute("h");
310 try {
311 final int serverCount = Integer.parseInt(h);
312 if (serverCount != stanzasSent) {
313 Log.d(Config.LOGTAG, account.getJid().toBareJid().toString()
314 + ": session resumed with lost packages");
315 stanzasSent = serverCount;
316 } else {
317 Log.d(Config.LOGTAG, account.getJid().toBareJid().toString()
318 + ": session resumed");
319 }
320 if (acknowledgedListener != null) {
321 for (int i = 0; i < messageReceipts.size(); ++i) {
322 if (serverCount >= messageReceipts.keyAt(i)) {
323 acknowledgedListener.onMessageAcknowledged(
324 account, messageReceipts.valueAt(i));
325 }
326 }
327 }
328 messageReceipts.clear();
329 } catch (final NumberFormatException ignored) {
330 }
331 sendServiceDiscoveryInfo(account.getServer());
332 sendServiceDiscoveryItems(account.getServer());
333 sendInitialPing();
334 } else if (nextTag.isStart("r")) {
335 tagReader.readElement(nextTag);
336 final AckPacket ack = new AckPacket(this.stanzasReceived, smVersion);
337 tagWriter.writeStanzaAsync(ack);
338 } else if (nextTag.isStart("a")) {
339 final Element ack = tagReader.readElement(nextTag);
340 lastPaketReceived = SystemClock.elapsedRealtime();
341 final int serverSequence = Integer.parseInt(ack.getAttribute("h"));
342 final String msgId = this.messageReceipts.get(serverSequence);
343 if (msgId != null) {
344 if (this.acknowledgedListener != null) {
345 this.acknowledgedListener.onMessageAcknowledged(
346 account, msgId);
347 }
348 this.messageReceipts.remove(serverSequence);
349 }
350 } else if (nextTag.isStart("failed")) {
351 tagReader.readElement(nextTag);
352 Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": resumption failed");
353 streamId = null;
354 if (account.getStatus() != Account.State.ONLINE) {
355 sendBindRequest();
356 }
357 } else if (nextTag.isStart("iq")) {
358 processIq(nextTag);
359 } else if (nextTag.isStart("message")) {
360 processMessage(nextTag);
361 } else if (nextTag.isStart("presence")) {
362 processPresence(nextTag);
363 }
364 nextTag = tagReader.readTag();
365 }
366 if (account.getStatus() == Account.State.ONLINE) {
367 account. setStatus(Account.State.OFFLINE);
368 if (statusListener != null) {
369 statusListener.onStatusChanged(account);
370 }
371 }
372 }
373
374 private void sendInitialPing() {
375 Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": sending intial ping");
376 final IqPacket iq = new IqPacket(IqPacket.TYPE.GET);
377 iq.setFrom(account.getJid());
378 iq.addChild("ping", "urn:xmpp:ping");
379 this.sendIqPacket(iq, new OnIqPacketReceived() {
380 @Override
381 public void onIqPacketReceived(final Account account, final IqPacket packet) {
382 Log.d(Config.LOGTAG, account.getJid().toBareJid().toString()
383 + ": online with resource " + account.getResource());
384 changeStatus(Account.State.ONLINE);
385 }
386 });
387 }
388
389 private Element processPacket(final Tag currentTag, final int packetType)
390 throws XmlPullParserException, IOException {
391 Element element;
392 switch (packetType) {
393 case PACKET_IQ:
394 element = new IqPacket();
395 break;
396 case PACKET_MESSAGE:
397 element = new MessagePacket();
398 break;
399 case PACKET_PRESENCE:
400 element = new PresencePacket();
401 break;
402 default:
403 return null;
404 }
405 element.setAttributes(currentTag.getAttributes());
406 Tag nextTag = tagReader.readTag();
407 if (nextTag == null) {
408 throw new IOException("interrupted mid tag");
409 }
410 while (!nextTag.isEnd(element.getName())) {
411 if (!nextTag.isNo()) {
412 final Element child = tagReader.readElement(nextTag);
413 final String type = currentTag.getAttribute("type");
414 if (packetType == PACKET_IQ
415 && "jingle".equals(child.getName())
416 && ("set".equalsIgnoreCase(type) || "get"
417 .equalsIgnoreCase(type))) {
418 element = new JinglePacket();
419 element.setAttributes(currentTag.getAttributes());
420 }
421 element.addChild(child);
422 }
423 nextTag = tagReader.readTag();
424 if (nextTag == null) {
425 throw new IOException("interrupted mid tag");
426 }
427 }
428 ++stanzasReceived;
429 lastPaketReceived = SystemClock.elapsedRealtime();
430 return element;
431 }
432
433 private void processIq(final Tag currentTag) throws XmlPullParserException,
434 IOException {
435 final IqPacket packet = (IqPacket) processPacket(currentTag, PACKET_IQ);
436
437 if (packet.getId() == null) {
438 return; // an iq packet without id is definitely invalid
439 }
440
441 if (packet instanceof JinglePacket) {
442 if (this.jingleListener != null) {
443 this.jingleListener.onJinglePacketReceived(account,
444 (JinglePacket) packet);
445 }
446 } else {
447 if (packetCallbacks.containsKey(packet.getId())) {
448 final Pair<IqPacket, OnIqPacketReceived> packetCallbackDuple = packetCallbacks.get(packet.getId());
449 // Packets to the server should have responses from the server
450 if (packetCallbackDuple.first.toServer(account)) {
451 if (packet.fromServer(account)) {
452 packetCallbackDuple.second
453 .onIqPacketReceived(account, packet);
454 packetCallbacks.remove(packet.getId());
455 }
456 } else {
457 if (packet.getFrom().equals(packetCallbackDuple.first.getTo())) {
458 packetCallbackDuple.second
459 .onIqPacketReceived(account, packet);
460 packetCallbacks.remove(packet.getId());
461 }
462 }
463 } else if ((packet.getType() == IqPacket.TYPE.GET || packet
464 .getType() == IqPacket.TYPE.SET)
465 && this.unregisteredIqListener != null) {
466 this.unregisteredIqListener.onIqPacketReceived(account, packet);
467 }
468 }
469 }
470
471 private void processMessage(final Tag currentTag) throws XmlPullParserException, IOException {
472 final MessagePacket packet = (MessagePacket) processPacket(currentTag,PACKET_MESSAGE);
473 this.messageListener.onMessagePacketReceived(account, packet);
474 }
475
476 private void processPresence(final Tag currentTag) throws XmlPullParserException, IOException {
477 PresencePacket packet = (PresencePacket) processPacket(currentTag, PACKET_PRESENCE);
478 this.presenceListener.onPresencePacketReceived(account, packet);
479 }
480
481 private void sendStartTLS() throws IOException {
482 final Tag startTLS = Tag.empty("starttls");
483 startTLS.setAttribute("xmlns", "urn:ietf:params:xml:ns:xmpp-tls");
484 tagWriter.writeTag(startTLS);
485 }
486
487 private SharedPreferences getPreferences() {
488 return PreferenceManager.getDefaultSharedPreferences(applicationContext);
489 }
490
491 private boolean enableLegacySSL() {
492 return getPreferences().getBoolean("enable_legacy_ssl", false);
493 }
494
495 private void switchOverToTls(final Tag currentTag) throws XmlPullParserException, IOException {
496 tagReader.readTag();
497 try {
498 final SSLContext sc = SSLContext.getInstance("TLS");
499 sc.init(null,new X509TrustManager[]{this.mXmppConnectionService.getMemorizingTrustManager()},mXmppConnectionService.getRNG());
500 final SSLSocketFactory factory = sc.getSocketFactory();
501 final HostnameVerifier verifier = this.mXmppConnectionService.getMemorizingTrustManager().wrapHostnameVerifier(new StrictHostnameVerifier());
502 final InetAddress address = socket == null ? null : socket.getInetAddress();
503
504 if (factory == null || address == null || verifier == null) {
505 throw new IOException("could not setup ssl");
506 }
507
508 final SSLSocket sslSocket = (SSLSocket) factory.createSocket(socket,address.getHostAddress(), socket.getPort(),true);
509
510 if (sslSocket == null) {
511 throw new IOException("could not initialize ssl socket");
512 }
513
514 final String[] supportProtocols;
515 if (enableLegacySSL()) {
516 supportProtocols = sslSocket.getSupportedProtocols();
517 } else {
518 final Collection<String> supportedProtocols = new LinkedList<>(
519 Arrays.asList(sslSocket.getSupportedProtocols()));
520 supportedProtocols.remove("SSLv3");
521 supportProtocols = new String[supportedProtocols.size()];
522 supportedProtocols.toArray(supportProtocols);
523 }
524 sslSocket.setEnabledProtocols(supportProtocols);
525
526 if (!verifier.verify(account.getServer().getDomainpart(),sslSocket.getSession())) {
527 Log.d(Config.LOGTAG,account.getJid().toBareJid()+": TLS certificate verification failed");
528 disconnect(true);
529 changeStatus(Account.State.SECURITY_ERROR);
530 }
531 tagReader.setInputStream(sslSocket.getInputStream());
532 tagWriter.setOutputStream(sslSocket.getOutputStream());
533 sendStartStream();
534 Log.d(Config.LOGTAG, account.getJid().toBareJid()+ ": TLS connection established");
535 enabledEncryption = true;
536 processStream(tagReader.readTag());
537 sslSocket.close();
538 } catch (final NoSuchAlgorithmException | KeyManagementException e1) {
539 Log.d(Config.LOGTAG,account.getJid().toBareJid()+": TLS certificate verification failed");
540 disconnect(true);
541 changeStatus(Account.State.SECURITY_ERROR);
542 }
543 }
544
545 private void processStreamFeatures(final Tag currentTag)
546 throws XmlPullParserException, IOException {
547 this.streamFeatures = tagReader.readElement(currentTag);
548 if (this.streamFeatures.hasChild("starttls") && !enabledEncryption) {
549 sendStartTLS();
550 } else if (this.streamFeatures.hasChild("register")
551 && account.isOptionSet(Account.OPTION_REGISTER)
552 && enabledEncryption) {
553 sendRegistryRequest();
554 } else if (!this.streamFeatures.hasChild("register")
555 && account.isOptionSet(Account.OPTION_REGISTER)) {
556 changeStatus(Account.State.REGISTRATION_NOT_SUPPORTED);
557 disconnect(true);
558 } else if (this.streamFeatures.hasChild("mechanisms")
559 && shouldAuthenticate && enabledEncryption) {
560 final List<String> mechanisms = extractMechanisms(streamFeatures
561 .findChild("mechanisms"));
562 final Element auth = new Element("auth");
563 auth.setAttribute("xmlns", "urn:ietf:params:xml:ns:xmpp-sasl");
564 if (mechanisms.contains("SCRAM-SHA-1")) {
565 saslMechanism = new ScramSha1(tagWriter, account, mXmppConnectionService.getRNG());
566 } else if (mechanisms.contains("PLAIN")) {
567 saslMechanism = new Plain(tagWriter, account);
568 } else if (mechanisms.contains("DIGEST-MD5")) {
569 saslMechanism = new DigestMd5(tagWriter, account, mXmppConnectionService.getRNG());
570 }
571 final JSONObject keys = account.getKeys();
572 try {
573 if (keys.has(Account.PINNED_MECHANISM_KEY) &&
574 keys.getInt(Account.PINNED_MECHANISM_KEY) > saslMechanism.getPriority() ) {
575 Log.e(Config.LOGTAG, "Auth failed. Authentication mechanism " + saslMechanism.getMechanism() +
576 " has lower priority (" + String.valueOf(saslMechanism.getPriority()) +
577 ") than pinned priority (" + keys.getInt(Account.PINNED_MECHANISM_KEY) +
578 "). Possible downgrade attack?");
579 disconnect(true);
580 changeStatus(Account.State.SECURITY_ERROR);
581 }
582 } catch (final JSONException e) {
583 Log.d(Config.LOGTAG, "Parse error while checking pinned auth mechanism");
584 }
585 Log.d(Config.LOGTAG,account.getJid().toString()+": Authenticating with " + saslMechanism.getMechanism());
586 auth.setAttribute("mechanism", saslMechanism.getMechanism());
587 if (!saslMechanism.getClientFirstMessage().isEmpty()) {
588 auth.setContent(saslMechanism.getClientFirstMessage());
589 }
590 tagWriter.writeElement(auth);
591 } else if (this.streamFeatures.hasChild("sm", "urn:xmpp:sm:"
592 + smVersion)
593 && streamId != null) {
594 final ResumePacket resume = new ResumePacket(this.streamId,
595 stanzasReceived, smVersion);
596 this.tagWriter.writeStanzaAsync(resume);
597 } else if (this.streamFeatures.hasChild("bind") && shouldBind) {
598 sendBindRequest();
599 } else {
600 disconnect(true);
601 changeStatus(Account.State.INCOMPATIBLE_SERVER);
602 }
603 }
604
605 private List<String> extractMechanisms(final Element stream) {
606 final ArrayList<String> mechanisms = new ArrayList<>(stream
607 .getChildren().size());
608 for (final Element child : stream.getChildren()) {
609 mechanisms.add(child.getContent());
610 }
611 return mechanisms;
612 }
613
614 private void sendRegistryRequest() {
615 final IqPacket register = new IqPacket(IqPacket.TYPE.GET);
616 register.query("jabber:iq:register");
617 register.setTo(account.getServer());
618 sendIqPacket(register, new OnIqPacketReceived() {
619
620 @Override
621 public void onIqPacketReceived(final Account account, final IqPacket packet) {
622 final Element instructions = packet.query().findChild("instructions");
623 if (packet.query().hasChild("username")
624 && (packet.query().hasChild("password"))) {
625 final IqPacket register = new IqPacket(IqPacket.TYPE.SET);
626 final Element username = new Element("username")
627 .setContent(account.getUsername());
628 final Element password = new Element("password")
629 .setContent(account.getPassword());
630 register.query("jabber:iq:register").addChild(username);
631 register.query().addChild(password);
632 sendIqPacket(register, new OnIqPacketReceived() {
633
634 @Override
635 public void onIqPacketReceived(final Account account, final IqPacket packet) {
636 if (packet.getType() == IqPacket.TYPE.RESULT) {
637 account.setOption(Account.OPTION_REGISTER,
638 false);
639 changeStatus(Account.State.REGISTRATION_SUCCESSFUL);
640 } else if (packet.hasChild("error")
641 && (packet.findChild("error")
642 .hasChild("conflict"))) {
643 changeStatus(Account.State.REGISTRATION_CONFLICT);
644 } else {
645 changeStatus(Account.State.REGISTRATION_FAILED);
646 Log.d(Config.LOGTAG, packet.toString());
647 }
648 disconnect(true);
649 }
650 });
651 } else {
652 changeStatus(Account.State.REGISTRATION_FAILED);
653 disconnect(true);
654 Log.d(Config.LOGTAG, account.getJid().toBareJid()
655 + ": could not register. instructions are"
656 + instructions.getContent());
657 }
658 }
659 });
660 }
661
662 private void sendBindRequest() {
663 final IqPacket iq = new IqPacket(IqPacket.TYPE.SET);
664 iq.addChild("bind", "urn:ietf:params:xml:ns:xmpp-bind")
665 .addChild("resource").setContent(account.getResource());
666 this.sendUnmodifiedIqPacket(iq, new OnIqPacketReceived() {
667 @Override
668 public void onIqPacketReceived(final Account account, final IqPacket packet) {
669 final Element bind = packet.findChild("bind");
670 if (bind != null) {
671 final Element jid = bind.findChild("jid");
672 if (jid != null && jid.getContent() != null) {
673 try {
674 account.setResource(Jid.fromString(jid.getContent()).getResourcepart());
675 } catch (final InvalidJidException e) {
676 // TODO: Handle the case where an external JID is technically invalid?
677 }
678 if (streamFeatures.hasChild("sm", "urn:xmpp:sm:3")) {
679 smVersion = 3;
680 final EnablePacket enable = new EnablePacket(smVersion);
681 tagWriter.writeStanzaAsync(enable);
682 stanzasSent = 0;
683 messageReceipts.clear();
684 } else if (streamFeatures.hasChild("sm",
685 "urn:xmpp:sm:2")) {
686 smVersion = 2;
687 final EnablePacket enable = new EnablePacket(smVersion);
688 tagWriter.writeStanzaAsync(enable);
689 stanzasSent = 0;
690 messageReceipts.clear();
691 }
692 enabledCarbons = false;
693 disco.clear();
694 sendServiceDiscoveryInfo(account.getServer());
695 sendServiceDiscoveryItems(account.getServer());
696 if (bindListener != null) {
697 bindListener.onBind(account);
698 }
699 sendInitialPing();
700 } else {
701 disconnect(true);
702 }
703 } else {
704 disconnect(true);
705 }
706 }
707 });
708 if (this.streamFeatures.hasChild("session")) {
709 Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": sending deprecated session");
710 final IqPacket startSession = new IqPacket(IqPacket.TYPE.SET);
711 startSession.addChild("session","urn:ietf:params:xml:ns:xmpp-session");
712 this.sendUnmodifiedIqPacket(startSession, null);
713 }
714 }
715
716 private void sendServiceDiscoveryInfo(final Jid server) {
717 if (disco.containsKey(server.toDomainJid().toString())) {
718 if (account.getServer().equals(server.toDomainJid())) {
719 enableAdvancedStreamFeatures();
720 }
721 } else {
722 final IqPacket iq = new IqPacket(IqPacket.TYPE.GET);
723 iq.setTo(server.toDomainJid());
724 iq.query("http://jabber.org/protocol/disco#info");
725 this.sendIqPacket(iq, new OnIqPacketReceived() {
726
727 @Override
728 public void onIqPacketReceived(final Account account, final IqPacket packet) {
729 final List<Element> elements = packet.query().getChildren();
730 final List<String> features = new ArrayList<>();
731 for (final Element element : elements) {
732 if (element.getName().equals("identity")) {
733 if ("irc".equals(element.getAttribute("type"))) {
734 //add fake feature to not confuse irc and real muc
735 features.add("siacs:no:muc");
736 }
737 } else if (element.getName().equals("feature")) {
738 features.add(element.getAttribute("var"));
739 }
740 }
741 disco.put(server.toDomainJid().toString(), features);
742
743 if (account.getServer().equals(server.toDomainJid())) {
744 enableAdvancedStreamFeatures();
745 for (final OnAdvancedStreamFeaturesLoaded listener : advancedStreamFeaturesLoadedListeners) {
746 listener.onAdvancedStreamFeaturesAvailable(account);
747 }
748 }
749 }
750 });
751 }
752 }
753
754 private void enableAdvancedStreamFeatures() {
755 if (getFeatures().carbons()) {
756 if (!enabledCarbons) {
757 sendEnableCarbons();
758 }
759 }
760 if (getFeatures().blocking()) {
761 Log.d(Config.LOGTAG, "Requesting block list");
762 this.sendIqPacket(getIqGenerator().generateGetBlockList(), mXmppConnectionService.getIqParser());
763 }
764 }
765
766 private void sendServiceDiscoveryItems(final Jid server) {
767 final IqPacket iq = new IqPacket(IqPacket.TYPE.GET);
768 iq.setTo(server.toDomainJid());
769 iq.query("http://jabber.org/protocol/disco#items");
770 this.sendIqPacket(iq, new OnIqPacketReceived() {
771
772 @Override
773 public void onIqPacketReceived(final Account account, final IqPacket packet) {
774 final List<Element> elements = packet.query().getChildren();
775 for (final Element element : elements) {
776 if (element.getName().equals("item")) {
777 final Jid jid = element.getAttributeAsJid("jid");
778 if (jid != null && !jid.equals(account.getServer())) {
779 sendServiceDiscoveryInfo(jid);
780 }
781 }
782 }
783 }
784 });
785 }
786
787 private void sendEnableCarbons() {
788 final IqPacket iq = new IqPacket(IqPacket.TYPE.SET);
789 iq.addChild("enable", "urn:xmpp:carbons:2");
790 this.sendIqPacket(iq, new OnIqPacketReceived() {
791
792 @Override
793 public void onIqPacketReceived(final Account account, final IqPacket packet) {
794 if (!packet.hasChild("error")) {
795 Log.d(Config.LOGTAG, account.getJid().toBareJid()
796 + ": successfully enabled carbons");
797 enabledCarbons = true;
798 } else {
799 Log.d(Config.LOGTAG, account.getJid().toBareJid()
800 + ": error enableing carbons " + packet.toString());
801 }
802 }
803 });
804 }
805
806 private void processStreamError(final Tag currentTag)
807 throws XmlPullParserException, IOException {
808 final Element streamError = tagReader.readElement(currentTag);
809 if (streamError != null && streamError.hasChild("conflict")) {
810 final String resource = account.getResource().split("\\.")[0];
811 account.setResource(resource + "." + nextRandomId());
812 Log.d(Config.LOGTAG,
813 account.getJid().toBareJid() + ": switching resource due to conflict ("
814 + account.getResource() + ")");
815 }
816 }
817
818 private void sendStartStream() throws IOException {
819 final Tag stream = Tag.start("stream:stream");
820 stream.setAttribute("from", account.getJid().toBareJid().toString());
821 stream.setAttribute("to", account.getServer().toString());
822 stream.setAttribute("version", "1.0");
823 stream.setAttribute("xml:lang", "en");
824 stream.setAttribute("xmlns", "jabber:client");
825 stream.setAttribute("xmlns:stream", "http://etherx.jabber.org/streams");
826 tagWriter.writeTag(stream);
827 }
828
829 private String nextRandomId() {
830 return new BigInteger(50, mXmppConnectionService.getRNG()).toString(32);
831 }
832
833 public void sendIqPacket(final IqPacket packet, final OnIqPacketReceived callback) {
834 packet.setFrom(account.getJid());
835 this.sendUnmodifiedIqPacket(packet,callback);
836
837 }
838
839 private void sendUnmodifiedIqPacket(final IqPacket packet, final OnIqPacketReceived callback) {
840 if (packet.getId() == null) {
841 final String id = nextRandomId();
842 packet.setAttribute("id", id);
843 }
844 if (callback != null) {
845 if (packet.getId() == null) {
846 packet.setId(nextRandomId());
847 }
848 packetCallbacks.put(packet.getId(), new Pair<>(packet, callback));
849 }
850 this.sendPacket(packet);
851 }
852
853 public void sendMessagePacket(final MessagePacket packet) {
854 this.sendPacket(packet);
855 }
856
857 public void sendPresencePacket(final PresencePacket packet) {
858 this.sendPacket(packet);
859 }
860
861 private synchronized void sendPacket(final AbstractStanza packet) {
862 final String name = packet.getName();
863 if (name.equals("iq") || name.equals("message") || name.equals("presence")) {
864 ++stanzasSent;
865 }
866 tagWriter.writeStanzaAsync(packet);
867 if (packet instanceof MessagePacket && packet.getId() != null && this.streamId != null) {
868 Log.d(Config.LOGTAG, "request delivery report for stanza " + stanzasSent);
869 this.messageReceipts.put(stanzasSent, packet.getId());
870 tagWriter.writeStanzaAsync(new RequestPacket(this.smVersion));
871 }
872 }
873
874 public void sendPing() {
875 if (streamFeatures.hasChild("sm")) {
876 tagWriter.writeStanzaAsync(new RequestPacket(smVersion));
877 } else {
878 final IqPacket iq = new IqPacket(IqPacket.TYPE.GET);
879 iq.setFrom(account.getJid());
880 iq.addChild("ping", "urn:xmpp:ping");
881 this.sendIqPacket(iq, null);
882 }
883 this.lastPingSent = SystemClock.elapsedRealtime();
884 }
885
886 public void setOnMessagePacketReceivedListener(
887 final OnMessagePacketReceived listener) {
888 this.messageListener = listener;
889 }
890
891 public void setOnUnregisteredIqPacketReceivedListener(
892 final OnIqPacketReceived listener) {
893 this.unregisteredIqListener = listener;
894 }
895
896 public void setOnPresencePacketReceivedListener(
897 final OnPresencePacketReceived listener) {
898 this.presenceListener = listener;
899 }
900
901 public void setOnJinglePacketReceivedListener(
902 final OnJinglePacketReceived listener) {
903 this.jingleListener = listener;
904 }
905
906 public void setOnStatusChangedListener(final OnStatusChanged listener) {
907 this.statusListener = listener;
908 }
909
910 public void setOnBindListener(final OnBindListener listener) {
911 this.bindListener = listener;
912 }
913
914 public void setOnMessageAcknowledgeListener(final OnMessageAcknowledged listener) {
915 this.acknowledgedListener = listener;
916 }
917
918 public void addOnAdvancedStreamFeaturesAvailableListener(final OnAdvancedStreamFeaturesLoaded listener) {
919 if (!this.advancedStreamFeaturesLoadedListeners.contains(listener)) {
920 this.advancedStreamFeaturesLoadedListeners.add(listener);
921 }
922 }
923
924 public void disconnect(final boolean force) {
925 Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": disconnecting");
926 try {
927 if (force) {
928 socket.close();
929 return;
930 }
931 new Thread(new Runnable() {
932
933 @Override
934 public void run() {
935 if (tagWriter.isActive()) {
936 tagWriter.finish();
937 try {
938 while (!tagWriter.finished()) {
939 Log.d(Config.LOGTAG, "not yet finished");
940 Thread.sleep(100);
941 }
942 tagWriter.writeTag(Tag.end("stream:stream"));
943 socket.close();
944 } catch (final IOException e) {
945 Log.d(Config.LOGTAG,
946 "io exception during disconnect");
947 } catch (final InterruptedException e) {
948 Log.d(Config.LOGTAG, "interrupted");
949 }
950 }
951 }
952 }).start();
953 } catch (final IOException e) {
954 Log.d(Config.LOGTAG, "io exception during disconnect");
955 }
956 }
957
958 public List<String> findDiscoItemsByFeature(final String feature) {
959 final List<String> items = new ArrayList<>();
960 for (final Entry<String, List<String>> cursor : disco.entrySet()) {
961 if (cursor.getValue().contains(feature)) {
962 items.add(cursor.getKey());
963 }
964 }
965 return items;
966 }
967
968 public String findDiscoItemByFeature(final String feature) {
969 final List<String> items = findDiscoItemsByFeature(feature);
970 if (items.size() >= 1) {
971 return items.get(0);
972 }
973 return null;
974 }
975
976 public void r() {
977 this.tagWriter.writeStanzaAsync(new RequestPacket(smVersion));
978 }
979
980 public String getMucServer() {
981 for (final Entry<String, List<String>> cursor : disco.entrySet()) {
982 final List<String> value = cursor.getValue();
983 if (value.contains("http://jabber.org/protocol/muc") && !value.contains("jabber:iq:gateway") && !value.contains("siacs:no:muc")) {
984 return cursor.getKey();
985 }
986 }
987 return null;
988 }
989
990 public int getTimeToNextAttempt() {
991 final int interval = (int) (25 * Math.pow(1.5, attempt));
992 final int secondsSinceLast = (int) ((SystemClock.elapsedRealtime() - this.lastConnect) / 1000);
993 return interval - secondsSinceLast;
994 }
995
996 public int getAttempt() {
997 return this.attempt;
998 }
999
1000 public Features getFeatures() {
1001 return this.features;
1002 }
1003
1004 public long getLastSessionEstablished() {
1005 final long diff;
1006 if (this.lastSessionStarted == 0) {
1007 diff = SystemClock.elapsedRealtime() - this.lastConnect;
1008 } else {
1009 diff = SystemClock.elapsedRealtime() - this.lastSessionStarted;
1010 }
1011 return System.currentTimeMillis() - diff;
1012 }
1013
1014 public long getLastConnect() {
1015 return this.lastConnect;
1016 }
1017
1018 public long getLastPingSent() {
1019 return this.lastPingSent;
1020 }
1021
1022 public long getLastPacketReceived() {
1023 return this.lastPaketReceived;
1024 }
1025
1026 public void sendActive() {
1027 this.sendPacket(new ActivePacket());
1028 }
1029
1030 public void sendInactive() {
1031 this.sendPacket(new InactivePacket());
1032 }
1033
1034 public class Features {
1035 XmppConnection connection;
1036
1037 public Features(final XmppConnection connection) {
1038 this.connection = connection;
1039 }
1040
1041 private boolean hasDiscoFeature(final Jid server, final String feature) {
1042 return connection.disco.containsKey(server.toDomainJid().toString()) &&
1043 connection.disco.get(server.toDomainJid().toString()).contains(feature);
1044 }
1045
1046 public boolean carbons() {
1047 return hasDiscoFeature(account.getServer(), "urn:xmpp:carbons:2");
1048 }
1049
1050 public boolean blocking() {
1051 return hasDiscoFeature(account.getServer(), Xmlns.BLOCKING);
1052 }
1053
1054 public boolean register() {
1055 return hasDiscoFeature(account.getServer(), Xmlns.REGISTER);
1056 }
1057
1058 public boolean sm() {
1059 return streamId != null;
1060 }
1061
1062 public boolean csi() {
1063 return connection.streamFeatures != null && connection.streamFeatures.hasChild("csi", "urn:xmpp:csi:0");
1064 }
1065
1066 public boolean pubsub() {
1067 return hasDiscoFeature(account.getServer(),
1068 "http://jabber.org/protocol/pubsub#publish");
1069 }
1070
1071 public boolean mam() {
1072 return hasDiscoFeature(account.getServer(), "urn:xmpp:mam:0");
1073 }
1074
1075 public boolean advancedStreamFeaturesLoaded() {
1076 return disco.containsKey(account.getServer().toString());
1077 }
1078
1079 public boolean rosterVersioning() {
1080 return connection.streamFeatures != null && connection.streamFeatures.hasChild("ver");
1081 }
1082
1083 public boolean streamhost() {
1084 return connection
1085 .findDiscoItemByFeature("http://jabber.org/protocol/bytestreams") != null;
1086 }
1087 }
1088
1089 private IqGenerator getIqGenerator() {
1090 return mXmppConnectionService.getIqGenerator();
1091 }
1092}