001/** 002 * 003 * Copyright 2003-2007 Jive Software. 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.jivesoftware.smack.tcp; 018 019import org.jivesoftware.smack.AbstractConnectionListener; 020import org.jivesoftware.smack.AbstractXMPPConnection; 021import org.jivesoftware.smack.ConnectionConfiguration; 022import org.jivesoftware.smack.ConnectionConfiguration.SecurityMode; 023import org.jivesoftware.smack.ConnectionCreationListener; 024import org.jivesoftware.smack.StanzaListener; 025import org.jivesoftware.smack.SmackConfiguration; 026import org.jivesoftware.smack.SmackException; 027import org.jivesoftware.smack.SmackException.AlreadyConnectedException; 028import org.jivesoftware.smack.SmackException.AlreadyLoggedInException; 029import org.jivesoftware.smack.SmackException.NoResponseException; 030import org.jivesoftware.smack.SmackException.NotConnectedException; 031import org.jivesoftware.smack.SmackException.ConnectionException; 032import org.jivesoftware.smack.SmackException.SecurityRequiredByClientException; 033import org.jivesoftware.smack.SmackException.SecurityRequiredByServerException; 034import org.jivesoftware.smack.SmackException.SecurityRequiredException; 035import org.jivesoftware.smack.SynchronizationPoint; 036import org.jivesoftware.smack.XMPPException.StreamErrorException; 037import org.jivesoftware.smack.XMPPConnection; 038import org.jivesoftware.smack.XMPPException; 039import org.jivesoftware.smack.XMPPException.XMPPErrorException; 040import org.jivesoftware.smack.compress.packet.Compressed; 041import org.jivesoftware.smack.compression.XMPPInputOutputStream; 042import org.jivesoftware.smack.filter.StanzaFilter; 043import org.jivesoftware.smack.compress.packet.Compress; 044import org.jivesoftware.smack.packet.Element; 045import org.jivesoftware.smack.packet.IQ; 046import org.jivesoftware.smack.packet.Message; 047import org.jivesoftware.smack.packet.StreamOpen; 048import org.jivesoftware.smack.packet.Stanza; 049import org.jivesoftware.smack.packet.Presence; 050import org.jivesoftware.smack.packet.StartTls; 051import org.jivesoftware.smack.sasl.packet.SaslStreamElements; 052import org.jivesoftware.smack.sasl.packet.SaslStreamElements.Challenge; 053import org.jivesoftware.smack.sasl.packet.SaslStreamElements.SASLFailure; 054import org.jivesoftware.smack.sasl.packet.SaslStreamElements.Success; 055import org.jivesoftware.smack.sm.SMUtils; 056import org.jivesoftware.smack.sm.StreamManagementException; 057import org.jivesoftware.smack.sm.StreamManagementException.StreamIdDoesNotMatchException; 058import org.jivesoftware.smack.sm.StreamManagementException.StreamManagementCounterError; 059import org.jivesoftware.smack.sm.StreamManagementException.StreamManagementNotEnabledException; 060import org.jivesoftware.smack.sm.packet.StreamManagement; 061import org.jivesoftware.smack.sm.packet.StreamManagement.AckAnswer; 062import org.jivesoftware.smack.sm.packet.StreamManagement.AckRequest; 063import org.jivesoftware.smack.sm.packet.StreamManagement.Enable; 064import org.jivesoftware.smack.sm.packet.StreamManagement.Enabled; 065import org.jivesoftware.smack.sm.packet.StreamManagement.Failed; 066import org.jivesoftware.smack.sm.packet.StreamManagement.Resume; 067import org.jivesoftware.smack.sm.packet.StreamManagement.Resumed; 068import org.jivesoftware.smack.sm.packet.StreamManagement.StreamManagementFeature; 069import org.jivesoftware.smack.sm.predicates.Predicate; 070import org.jivesoftware.smack.sm.provider.ParseStreamManagement; 071import org.jivesoftware.smack.packet.PlainStreamElement; 072import org.jivesoftware.smack.packet.XMPPError; 073import org.jivesoftware.smack.util.ArrayBlockingQueueWithShutdown; 074import org.jivesoftware.smack.util.Async; 075import org.jivesoftware.smack.util.PacketParserUtils; 076import org.jivesoftware.smack.util.StringUtils; 077import org.jivesoftware.smack.util.TLSUtils; 078import org.jivesoftware.smack.util.dns.HostAddress; 079import org.jxmpp.util.XmppStringUtils; 080import org.xmlpull.v1.XmlPullParser; 081import org.xmlpull.v1.XmlPullParserException; 082 083import javax.net.SocketFactory; 084import javax.net.ssl.HostnameVerifier; 085import javax.net.ssl.KeyManager; 086import javax.net.ssl.KeyManagerFactory; 087import javax.net.ssl.SSLContext; 088import javax.net.ssl.SSLSocket; 089import javax.security.auth.callback.Callback; 090import javax.security.auth.callback.PasswordCallback; 091 092import java.io.BufferedReader; 093import java.io.ByteArrayInputStream; 094import java.io.FileInputStream; 095import java.io.IOException; 096import java.io.InputStream; 097import java.io.InputStreamReader; 098import java.io.OutputStream; 099import java.io.OutputStreamWriter; 100import java.io.Writer; 101import java.lang.reflect.Constructor; 102import java.net.InetAddress; 103import java.net.InetSocketAddress; 104import java.net.Socket; 105import java.net.UnknownHostException; 106import java.security.KeyManagementException; 107import java.security.KeyStore; 108import java.security.KeyStoreException; 109import java.security.NoSuchAlgorithmException; 110import java.security.NoSuchProviderException; 111import java.security.Provider; 112import java.security.Security; 113import java.security.UnrecoverableKeyException; 114import java.security.cert.CertificateException; 115import java.util.ArrayList; 116import java.util.Arrays; 117import java.util.Collection; 118import java.util.Iterator; 119import java.util.LinkedHashSet; 120import java.util.LinkedList; 121import java.util.List; 122import java.util.Map; 123import java.util.Set; 124import java.util.concurrent.ArrayBlockingQueue; 125import java.util.concurrent.BlockingQueue; 126import java.util.concurrent.ConcurrentHashMap; 127import java.util.concurrent.ConcurrentLinkedQueue; 128import java.util.concurrent.TimeUnit; 129import java.util.concurrent.atomic.AtomicBoolean; 130import java.util.logging.Level; 131import java.util.logging.Logger; 132 133/** 134 * Creates a socket connection to an XMPP server. This is the default connection 135 * to an XMPP server and is specified in the XMPP Core (RFC 6120). 136 * 137 * @see XMPPConnection 138 * @author Matt Tucker 139 */ 140public class XMPPTCPConnection extends AbstractXMPPConnection { 141 142 private static final int QUEUE_SIZE = 500; 143 private static final Logger LOGGER = Logger.getLogger(XMPPTCPConnection.class.getName()); 144 145 /** 146 * The socket which is used for this connection. 147 */ 148 private Socket socket; 149 150 /** 151 * 152 */ 153 private boolean disconnectedButResumeable = false; 154 155 /** 156 * Flag to indicate if the socket was closed intentionally by Smack. 157 * <p> 158 * This boolean flag is used concurrently, therefore it is marked volatile. 159 * </p> 160 */ 161 private volatile boolean socketClosed = false; 162 163 private boolean usingTLS = false; 164 165 /** 166 * Protected access level because of unit test purposes 167 */ 168 protected PacketWriter packetWriter; 169 170 /** 171 * Protected access level because of unit test purposes 172 */ 173 protected PacketReader packetReader; 174 175 private final SynchronizationPoint<Exception> initalOpenStreamSend = new SynchronizationPoint<Exception>(this); 176 177 /** 178 * 179 */ 180 private final SynchronizationPoint<XMPPException> maybeCompressFeaturesReceived = new SynchronizationPoint<XMPPException>( 181 this); 182 183 /** 184 * 185 */ 186 private final SynchronizationPoint<XMPPException> compressSyncPoint = new SynchronizationPoint<XMPPException>( 187 this); 188 189 /** 190 * The default bundle and defer callback, used for new connections. 191 * @see bundleAndDeferCallback 192 */ 193 private static BundleAndDeferCallback defaultBundleAndDeferCallback; 194 195 /** 196 * The used bundle and defer callback. 197 * <p> 198 * Although this field may be set concurrently, the 'volatile' keyword was deliberately not added, in order to avoid 199 * having a 'volatile' read within the writer threads loop. 200 * </p> 201 */ 202 private BundleAndDeferCallback bundleAndDeferCallback = defaultBundleAndDeferCallback; 203 204 private static boolean useSmDefault = false; 205 206 private static boolean useSmResumptionDefault = true; 207 208 /** 209 * The stream ID of the stream that is currently resumable, ie. the stream we hold the state 210 * for in {@link #clientHandledStanzasCount}, {@link #serverHandledStanzasCount} and 211 * {@link #unacknowledgedStanzas}. 212 */ 213 private String smSessionId; 214 215 private final SynchronizationPoint<XMPPException> smResumedSyncPoint = new SynchronizationPoint<XMPPException>( 216 this); 217 218 private final SynchronizationPoint<XMPPException> smEnabledSyncPoint = new SynchronizationPoint<XMPPException>( 219 this); 220 221 /** 222 * The client's preferred maximum resumption time in seconds. 223 */ 224 private int smClientMaxResumptionTime = -1; 225 226 /** 227 * The server's preferred maximum resumption time in seconds. 228 */ 229 private int smServerMaxResumptimTime = -1; 230 231 /** 232 * Indicates whether Stream Management (XEP-198) should be used if it's supported by the server. 233 */ 234 private boolean useSm = useSmDefault; 235 private boolean useSmResumption = useSmResumptionDefault; 236 237 /** 238 * The counter that the server sends the client about it's current height. For example, if the server sends 239 * {@code <a h='42'/>}, then this will be set to 42 (while also handling the {@link #unacknowledgedStanzas} queue). 240 */ 241 private long serverHandledStanzasCount = 0; 242 243 /** 244 * The counter for stanzas handled ("received") by the client. 245 * <p> 246 * Note that we don't need to synchronize this counter. Although JLS 17.7 states that reads and writes to longs are 247 * not atomic, it guarantees that there are at most 2 separate writes, one to each 32-bit half. And since 248 * {@link SMUtils#incrementHeight(long)} masks the lower 32 bit, we only operate on one half of the long and 249 * therefore have no concurrency problem because the read/write operations on one half are guaranteed to be atomic. 250 * </p> 251 */ 252 private long clientHandledStanzasCount = 0; 253 254 private BlockingQueue<Stanza> unacknowledgedStanzas; 255 256 /** 257 * Set to true if Stream Management was at least once enabled for this connection. 258 */ 259 private boolean smWasEnabledAtLeastOnce = false; 260 261 /** 262 * This listeners are invoked for every stanza that got acknowledged. 263 * <p> 264 * We use a {@link ConccurrentLinkedQueue} here in order to allow the listeners to remove 265 * themselves after they have been invoked. 266 * </p> 267 */ 268 private final Collection<StanzaListener> stanzaAcknowledgedListeners = new ConcurrentLinkedQueue<StanzaListener>(); 269 270 /** 271 * This listeners are invoked for a acknowledged stanza that has the given stanza ID. They will 272 * only be invoked once and automatically removed after that. 273 */ 274 private final Map<String, StanzaListener> stanzaIdAcknowledgedListeners = new ConcurrentHashMap<String, StanzaListener>(); 275 276 /** 277 * Predicates that determine if an stream management ack should be requested from the server. 278 * <p> 279 * We use a linked hash set here, so that the order how the predicates are added matches the 280 * order in which they are invoked in order to determine if an ack request should be send or not. 281 * </p> 282 */ 283 private final Set<StanzaFilter> requestAckPredicates = new LinkedHashSet<StanzaFilter>(); 284 285 private final XMPPTCPConnectionConfiguration config; 286 287 /** 288 * Creates a new XMPP connection over TCP (optionally using proxies). 289 * <p> 290 * Note that XMPPTCPConnection constructors do not establish a connection to the server 291 * and you must call {@link #connect()}. 292 * </p> 293 * 294 * @param config the connection configuration. 295 */ 296 public XMPPTCPConnection(XMPPTCPConnectionConfiguration config) { 297 super(config); 298 this.config = config; 299 addConnectionListener(new AbstractConnectionListener() { 300 @Override 301 public void connectionClosedOnError(Exception e) { 302 if (e instanceof XMPPException.StreamErrorException) { 303 dropSmState(); 304 } 305 } 306 }); 307 } 308 309 /** 310 * Creates a new XMPP connection over TCP. 311 * <p> 312 * Note that {@code jid} must be the bare JID, e.g. "user@example.org". More fine-grained control over the 313 * connection settings is available using the {@link #XMPPTCPConnection(XMPPTCPConnectionConfiguration)} 314 * constructor. 315 * </p> 316 * 317 * @param jid the bare JID used by the client. 318 * @param password the password or authentication token. 319 */ 320 public XMPPTCPConnection(CharSequence jid, String password) { 321 this(XmppStringUtils.parseLocalpart(jid.toString()), password, XmppStringUtils.parseDomain(jid.toString())); 322 } 323 324 /** 325 * Creates a new XMPP connection over TCP. 326 * <p> 327 * This is the simplest constructor for connecting to an XMPP server. Alternatively, 328 * you can get fine-grained control over connection settings using the 329 * {@link #XMPPTCPConnection(XMPPTCPConnectionConfiguration)} constructor. 330 * </p> 331 * @param username 332 * @param password 333 * @param serviceName 334 */ 335 public XMPPTCPConnection(CharSequence username, String password, String serviceName) { 336 this(XMPPTCPConnectionConfiguration.builder().setUsernameAndPassword(username, password).setServiceName( 337 serviceName).build()); 338 } 339 340 @Override 341 protected void throwNotConnectedExceptionIfAppropriate() throws NotConnectedException { 342 if (packetWriter == null) { 343 throw new NotConnectedException(); 344 } 345 packetWriter.throwNotConnectedExceptionIfDoneAndResumptionNotPossible(); 346 } 347 348 @Override 349 protected void throwAlreadyConnectedExceptionIfAppropriate() throws AlreadyConnectedException { 350 if (isConnected() && !disconnectedButResumeable) { 351 throw new AlreadyConnectedException(); 352 } 353 } 354 355 @Override 356 protected void throwAlreadyLoggedInExceptionIfAppropriate() throws AlreadyLoggedInException { 357 if (isAuthenticated() && !disconnectedButResumeable) { 358 throw new AlreadyLoggedInException(); 359 } 360 } 361 362 @Override 363 protected void afterSuccessfulLogin(final boolean resumed) throws NotConnectedException { 364 // Reset the flag in case it was set 365 disconnectedButResumeable = false; 366 super.afterSuccessfulLogin(resumed); 367 } 368 369 @Override 370 protected synchronized void loginNonAnonymously(String username, String password, String resource) throws XMPPException, SmackException, IOException { 371 if (saslAuthentication.hasNonAnonymousAuthentication()) { 372 // Authenticate using SASL 373 if (password != null) { 374 saslAuthentication.authenticate(username, password, resource); 375 } 376 else { 377 saslAuthentication.authenticate(resource, config.getCallbackHandler()); 378 } 379 } else { 380 throw new SmackException("No non-anonymous SASL authentication mechanism available"); 381 } 382 383 // If compression is enabled then request the server to use stream compression. XEP-170 384 // recommends to perform stream compression before resource binding. 385 if (config.isCompressionEnabled()) { 386 useCompression(); 387 } 388 389 if (isSmResumptionPossible()) { 390 smResumedSyncPoint.sendAndWaitForResponse(new Resume(clientHandledStanzasCount, smSessionId)); 391 if (smResumedSyncPoint.wasSuccessful()) { 392 // We successfully resumed the stream, be done here 393 afterSuccessfulLogin(true); 394 return; 395 } 396 // SM resumption failed, what Smack does here is to report success of 397 // lastFeaturesReceived in case of sm resumption was answered with 'failed' so that 398 // normal resource binding can be tried. 399 LOGGER.fine("Stream resumption failed, continuing with normal stream establishment process"); 400 } 401 402 List<Stanza> previouslyUnackedStanzas = new LinkedList<Stanza>(); 403 if (unacknowledgedStanzas != null) { 404 // There was a previous connection with SM enabled but that was either not resumable or 405 // failed to resume. Make sure that we (re-)send the unacknowledged stanzas. 406 unacknowledgedStanzas.drainTo(previouslyUnackedStanzas); 407 // Reset unacknowledged stanzas to 'null' to signal that we never send 'enable' in this 408 // XMPP session (There maybe was an enabled in a previous XMPP session of this 409 // connection instance though). This is used in writePackets to decide if stanzas should 410 // be added to the unacknowledged stanzas queue, because they have to be added right 411 // after the 'enable' stream element has been sent. 412 dropSmState(); 413 } 414 415 // Now bind the resource. It is important to do this *after* we dropped an eventually 416 // existing Stream Management state. As otherwise <bind/> and <session/> may end up in 417 // unacknowledgedStanzas and become duplicated on reconnect. See SMACK-706. 418 bindResourceAndEstablishSession(resource); 419 420 if (isSmAvailable() && useSm) { 421 // Remove what is maybe left from previously stream managed sessions 422 serverHandledStanzasCount = 0; 423 // XEP-198 3. Enabling Stream Management. If the server response to 'Enable' is 'Failed' 424 // then this is a non recoverable error and we therefore throw an exception. 425 smEnabledSyncPoint.sendAndWaitForResponseOrThrow(new Enable(useSmResumption, smClientMaxResumptionTime)); 426 synchronized (requestAckPredicates) { 427 if (requestAckPredicates.isEmpty()) { 428 // Assure that we have at lest one predicate set up that so that we request acks 429 // for the server and eventually flush some stanzas from the unacknowledged 430 // stanza queue 431 requestAckPredicates.add(Predicate.forMessagesOrAfter5Stanzas()); 432 } 433 } 434 } 435 // (Re-)send the stanzas *after* we tried to enable SM 436 for (Stanza stanza : previouslyUnackedStanzas) { 437 sendStanzaInternal(stanza); 438 } 439 440 afterSuccessfulLogin(false); 441 } 442 443 @Override 444 public synchronized void loginAnonymously() throws XMPPException, SmackException, IOException { 445 // Wait with SASL auth until the SASL mechanisms have been received 446 saslFeatureReceived.checkIfSuccessOrWaitOrThrow(); 447 448 if (saslAuthentication.hasAnonymousAuthentication()) { 449 saslAuthentication.authenticateAnonymously(); 450 } 451 else { 452 throw new SmackException("No anonymous SASL authentication mechanism available"); 453 } 454 455 // If compression is enabled then request the server to use stream compression 456 if (config.isCompressionEnabled()) { 457 useCompression(); 458 } 459 460 bindResourceAndEstablishSession(null); 461 462 afterSuccessfulLogin(false); 463 } 464 465 @Override 466 public boolean isSecureConnection() { 467 return usingTLS; 468 } 469 470 public boolean isSocketClosed() { 471 return socketClosed; 472 } 473 474 /** 475 * Shuts the current connection down. After this method returns, the connection must be ready 476 * for re-use by connect. 477 */ 478 @Override 479 protected void shutdown() { 480 if (isSmEnabled()) { 481 try { 482 // Try to send a last SM Acknowledgement. Most servers won't find this information helpful, as the SM 483 // state is dropped after a clean disconnect anyways. OTOH it doesn't hurt much either. 484 sendSmAcknowledgementInternal(); 485 } catch (NotConnectedException e) { 486 LOGGER.log(Level.FINE, "Can not send final SM ack as connection is not connected", e); 487 } 488 } 489 shutdown(false); 490 } 491 492 /** 493 * Performs an unclean disconnect and shutdown of the connection. Does not send a closing stream stanza. 494 */ 495 public synchronized void instantShutdown() { 496 shutdown(true); 497 } 498 499 private void shutdown(boolean instant) { 500 if (disconnectedButResumeable) { 501 return; 502 } 503 if (packetReader != null) { 504 packetReader.shutdown(); 505 } 506 if (packetWriter != null) { 507 packetWriter.shutdown(instant); 508 } 509 510 // Set socketClosed to true. This will cause the PacketReader 511 // and PacketWriter to ignore any Exceptions that are thrown 512 // because of a read/write from/to a closed stream. 513 // It is *important* that this is done before socket.close()! 514 socketClosed = true; 515 try { 516 socket.close(); 517 } catch (Exception e) { 518 LOGGER.log(Level.WARNING, "shutdown", e); 519 } 520 521 setWasAuthenticated(); 522 // If we are able to resume the stream, then don't set 523 // connected/authenticated/usingTLS to false since we like behave like we are still 524 // connected (e.g. sendStanza should not throw a NotConnectedException). 525 if (isSmResumptionPossible() && instant) { 526 disconnectedButResumeable = true; 527 } else { 528 disconnectedButResumeable = false; 529 // Reset the stream management session id to null, since if the stream is cleanly closed, i.e. sending a closing 530 // stream tag, there is no longer a stream to resume. 531 smSessionId = null; 532 } 533 authenticated = false; 534 connected = false; 535 usingTLS = false; 536 reader = null; 537 writer = null; 538 539 maybeCompressFeaturesReceived.init(); 540 compressSyncPoint.init(); 541 smResumedSyncPoint.init(); 542 smEnabledSyncPoint.init(); 543 initalOpenStreamSend.init(); 544 } 545 546 @Override 547 public void send(PlainStreamElement element) throws NotConnectedException { 548 packetWriter.sendStreamElement(element); 549 } 550 551 @Override 552 protected void sendStanzaInternal(Stanza packet) throws NotConnectedException { 553 packetWriter.sendStreamElement(packet); 554 if (isSmEnabled()) { 555 for (StanzaFilter requestAckPredicate : requestAckPredicates) { 556 if (requestAckPredicate.accept(packet)) { 557 requestSmAcknowledgementInternal(); 558 break; 559 } 560 } 561 } 562 } 563 564 private void connectUsingConfiguration() throws IOException, ConnectionException { 565 List<HostAddress> failedAddresses = populateHostAddresses(); 566 SocketFactory socketFactory = config.getSocketFactory(); 567 if (socketFactory == null) { 568 socketFactory = SocketFactory.getDefault(); 569 } 570 for (HostAddress hostAddress : hostAddresses) { 571 String host = hostAddress.getFQDN(); 572 int port = hostAddress.getPort(); 573 socket = socketFactory.createSocket(); 574 try { 575 Iterator<InetAddress> inetAddresses = Arrays.asList(InetAddress.getAllByName(host)).iterator(); 576 if (!inetAddresses.hasNext()) { 577 // This should not happen 578 LOGGER.warning("InetAddress.getAllByName() returned empty result array."); 579 throw new UnknownHostException(host); 580 } 581 innerloop: while (inetAddresses.hasNext()) { 582 final InetAddress inetAddress = inetAddresses.next(); 583 final String inetAddressAndPort = inetAddress + " at port " + port; 584 LOGGER.finer("Trying to establish TCP connection to " + inetAddressAndPort); 585 try { 586 socket.connect(new InetSocketAddress(inetAddress, port), config.getConnectTimeout()); 587 } catch (Exception e) { 588 if (inetAddresses.hasNext()) { 589 continue innerloop; 590 } else { 591 throw e; 592 } 593 } 594 LOGGER.finer("Established TCP connection to " + inetAddressAndPort); 595 // We found a host to connect to, return here 596 this.host = host; 597 this.port = port; 598 return; 599 } 600 } 601 catch (Exception e) { 602 hostAddress.setException(e); 603 failedAddresses.add(hostAddress); 604 } 605 } 606 // There are no more host addresses to try 607 // throw an exception and report all tried 608 // HostAddresses in the exception 609 throw ConnectionException.from(failedAddresses); 610 } 611 612 /** 613 * Initializes the connection by creating a stanza(/packet) reader and writer and opening a 614 * XMPP stream to the server. 615 * 616 * @throws XMPPException if establishing a connection to the server fails. 617 * @throws SmackException if the server failes to respond back or if there is anther error. 618 * @throws IOException 619 */ 620 private void initConnection() throws IOException { 621 boolean isFirstInitialization = packetReader == null || packetWriter == null; 622 compressionHandler = null; 623 624 // Set the reader and writer instance variables 625 initReaderAndWriter(); 626 627 if (isFirstInitialization) { 628 packetWriter = new PacketWriter(); 629 packetReader = new PacketReader(); 630 631 // If debugging is enabled, we should start the thread that will listen for 632 // all packets and then log them. 633 if (config.isDebuggerEnabled()) { 634 addAsyncStanzaListener(debugger.getReaderListener(), null); 635 if (debugger.getWriterListener() != null) { 636 addPacketSendingListener(debugger.getWriterListener(), null); 637 } 638 } 639 } 640 // Start the packet writer. This will open an XMPP stream to the server 641 packetWriter.init(); 642 // Start the packet reader. The startup() method will block until we 643 // get an opening stream packet back from server 644 packetReader.init(); 645 646 if (isFirstInitialization) { 647 // Notify listeners that a new connection has been established 648 for (ConnectionCreationListener listener : getConnectionCreationListeners()) { 649 listener.connectionCreated(this); 650 } 651 } 652 } 653 654 private void initReaderAndWriter() throws IOException { 655 InputStream is = socket.getInputStream(); 656 OutputStream os = socket.getOutputStream(); 657 if (compressionHandler != null) { 658 is = compressionHandler.getInputStream(is); 659 os = compressionHandler.getOutputStream(os); 660 } 661 // OutputStreamWriter is already buffered, no need to wrap it into a BufferedWriter 662 writer = new OutputStreamWriter(os, "UTF-8"); 663 reader = new BufferedReader(new InputStreamReader(is, "UTF-8")); 664 665 // If debugging is enabled, we open a window and write out all network traffic. 666 initDebugger(); 667 } 668 669 /** 670 * The server has indicated that TLS negotiation can start. We now need to secure the 671 * existing plain connection and perform a handshake. This method won't return until the 672 * connection has finished the handshake or an error occurred while securing the connection. 673 * @throws IOException 674 * @throws CertificateException 675 * @throws NoSuchAlgorithmException 676 * @throws NoSuchProviderException 677 * @throws KeyStoreException 678 * @throws UnrecoverableKeyException 679 * @throws KeyManagementException 680 * @throws SmackException 681 * @throws Exception if an exception occurs. 682 */ 683 private void proceedTLSReceived() throws NoSuchAlgorithmException, CertificateException, IOException, KeyStoreException, NoSuchProviderException, UnrecoverableKeyException, KeyManagementException, SmackException { 684 SSLContext context = this.config.getCustomSSLContext(); 685 KeyStore ks = null; 686 KeyManager[] kms = null; 687 PasswordCallback pcb = null; 688 689 if(config.getCallbackHandler() == null) { 690 ks = null; 691 } else if (context == null) { 692 if(config.getKeystoreType().equals("NONE")) { 693 ks = null; 694 pcb = null; 695 } 696 else if(config.getKeystoreType().equals("PKCS11")) { 697 try { 698 Constructor<?> c = Class.forName("sun.security.pkcs11.SunPKCS11").getConstructor(InputStream.class); 699 String pkcs11Config = "name = SmartCard\nlibrary = "+config.getPKCS11Library(); 700 ByteArrayInputStream config = new ByteArrayInputStream(pkcs11Config.getBytes()); 701 Provider p = (Provider)c.newInstance(config); 702 Security.addProvider(p); 703 ks = KeyStore.getInstance("PKCS11",p); 704 pcb = new PasswordCallback("PKCS11 Password: ",false); 705 this.config.getCallbackHandler().handle(new Callback[]{pcb}); 706 ks.load(null,pcb.getPassword()); 707 } 708 catch (Exception e) { 709 ks = null; 710 pcb = null; 711 } 712 } 713 else if(config.getKeystoreType().equals("Apple")) { 714 ks = KeyStore.getInstance("KeychainStore","Apple"); 715 ks.load(null,null); 716 //pcb = new PasswordCallback("Apple Keychain",false); 717 //pcb.setPassword(null); 718 } 719 else { 720 ks = KeyStore.getInstance(config.getKeystoreType()); 721 try { 722 pcb = new PasswordCallback("Keystore Password: ",false); 723 config.getCallbackHandler().handle(new Callback[]{pcb}); 724 ks.load(new FileInputStream(config.getKeystorePath()), pcb.getPassword()); 725 } 726 catch(Exception e) { 727 ks = null; 728 pcb = null; 729 } 730 } 731 KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509"); 732 try { 733 if(pcb == null) { 734 kmf.init(ks,null); 735 } else { 736 kmf.init(ks,pcb.getPassword()); 737 pcb.clearPassword(); 738 } 739 kms = kmf.getKeyManagers(); 740 } catch (NullPointerException npe) { 741 kms = null; 742 } 743 } 744 745 // If the user didn't specify a SSLContext, use the default one 746 if (context == null) { 747 context = SSLContext.getInstance("TLS"); 748 context.init(kms, null, new java.security.SecureRandom()); 749 } 750 Socket plain = socket; 751 // Secure the plain connection 752 socket = context.getSocketFactory().createSocket(plain, 753 host, plain.getPort(), true); 754 // Initialize the reader and writer with the new secured version 755 initReaderAndWriter(); 756 757 final SSLSocket sslSocket = (SSLSocket) socket; 758 TLSUtils.setEnabledProtocolsAndCiphers(sslSocket, config.getEnabledSSLProtocols(), config.getEnabledSSLCiphers()); 759 760 // Proceed to do the handshake 761 sslSocket.startHandshake(); 762 763 final HostnameVerifier verifier = getConfiguration().getHostnameVerifier(); 764 if (verifier == null) { 765 throw new IllegalStateException("No HostnameVerifier set. Use connectionConfiguration.setHostnameVerifier() to configure."); 766 } else if (!verifier.verify(getServiceName(), sslSocket.getSession())) { 767 throw new CertificateException("Hostname verification of certificate failed. Certificate does not authenticate " + getServiceName()); 768 } 769 770 // Set that TLS was successful 771 usingTLS = true; 772 } 773 774 /** 775 * Returns the compression handler that can be used for one compression methods offered by the server. 776 * 777 * @return a instance of XMPPInputOutputStream or null if no suitable instance was found 778 * 779 */ 780 private XMPPInputOutputStream maybeGetCompressionHandler() { 781 Compress.Feature compression = getFeature(Compress.Feature.ELEMENT, Compress.NAMESPACE); 782 if (compression == null) { 783 // Server does not support compression 784 return null; 785 } 786 for (XMPPInputOutputStream handler : SmackConfiguration.getCompresionHandlers()) { 787 String method = handler.getCompressionMethod(); 788 if (compression.getMethods().contains(method)) 789 return handler; 790 } 791 return null; 792 } 793 794 @Override 795 public boolean isUsingCompression() { 796 return compressionHandler != null && compressSyncPoint.wasSuccessful(); 797 } 798 799 /** 800 * <p> 801 * Starts using stream compression that will compress network traffic. Traffic can be 802 * reduced up to 90%. Therefore, stream compression is ideal when using a slow speed network 803 * connection. However, the server and the client will need to use more CPU time in order to 804 * un/compress network data so under high load the server performance might be affected. 805 * </p> 806 * <p> 807 * Stream compression has to have been previously offered by the server. Currently only the 808 * zlib method is supported by the client. Stream compression negotiation has to be done 809 * before authentication took place. 810 * </p> 811 * 812 * @throws NotConnectedException 813 * @throws XMPPException 814 * @throws NoResponseException 815 */ 816 private void useCompression() throws NotConnectedException, NoResponseException, XMPPException { 817 maybeCompressFeaturesReceived.checkIfSuccessOrWait(); 818 // If stream compression was offered by the server and we want to use 819 // compression then send compression request to the server 820 if ((compressionHandler = maybeGetCompressionHandler()) != null) { 821 compressSyncPoint.sendAndWaitForResponseOrThrow(new Compress(compressionHandler.getCompressionMethod())); 822 } else { 823 LOGGER.warning("Could not enable compression because no matching handler/method pair was found"); 824 } 825 } 826 827 /** 828 * Establishes a connection to the XMPP server and performs an automatic login 829 * only if the previous connection state was logged (authenticated). It basically 830 * creates and maintains a socket connection to the server.<p> 831 * <p/> 832 * Listeners will be preserved from a previous connection if the reconnection 833 * occurs after an abrupt termination. 834 * 835 * @throws XMPPException if an error occurs while trying to establish the connection. 836 * @throws SmackException 837 * @throws IOException 838 */ 839 @Override 840 protected void connectInternal() throws SmackException, IOException, XMPPException { 841 // Establishes the TCP connection to the server and does setup the reader and writer. Throws an exception if 842 // there is an error establishing the connection 843 connectUsingConfiguration(); 844 845 // We connected successfully to the servers TCP port 846 socketClosed = false; 847 initConnection(); 848 849 // Wait with SASL auth until the SASL mechanisms have been received 850 saslFeatureReceived.checkIfSuccessOrWaitOrThrow(); 851 852 // Make note of the fact that we're now connected. 853 connected = true; 854 callConnectionConnectedListener(); 855 856 // Automatically makes the login if the user was previously connected successfully 857 // to the server and the connection was terminated abruptly 858 if (wasAuthenticated) { 859 login(); 860 notifyReconnection(); 861 } 862 } 863 864 /** 865 * Sends out a notification that there was an error with the connection 866 * and closes the connection. Also prints the stack trace of the given exception 867 * 868 * @param e the exception that causes the connection close event. 869 */ 870 private synchronized void notifyConnectionError(Exception e) { 871 // Listeners were already notified of the exception, return right here. 872 if ((packetReader == null || packetReader.done) && 873 (packetWriter == null || packetWriter.done())) return; 874 875 // Closes the connection temporary. A reconnection is possible 876 instantShutdown(); 877 878 // Notify connection listeners of the error. 879 callConnectionClosedOnErrorListener(e); 880 } 881 882 /** 883 * For unit testing purposes 884 * 885 * @param writer 886 */ 887 protected void setWriter(Writer writer) { 888 this.writer = writer; 889 } 890 891 @Override 892 protected void afterFeaturesReceived() throws SecurityRequiredException, NotConnectedException { 893 StartTls startTlsFeature = getFeature(StartTls.ELEMENT, StartTls.NAMESPACE); 894 if (startTlsFeature != null) { 895 if (startTlsFeature.required() && config.getSecurityMode() == SecurityMode.disabled) { 896 notifyConnectionError(new SecurityRequiredByServerException()); 897 return; 898 } 899 900 if (config.getSecurityMode() != ConnectionConfiguration.SecurityMode.disabled) { 901 send(new StartTls()); 902 } 903 } 904 // If TLS is required but the server doesn't offer it, disconnect 905 // from the server and throw an error. First check if we've already negotiated TLS 906 // and are secure, however (features get parsed a second time after TLS is established). 907 if (!isSecureConnection() && startTlsFeature == null 908 && getConfiguration().getSecurityMode() == SecurityMode.required) { 909 throw new SecurityRequiredByClientException(); 910 } 911 912 if (getSASLAuthentication().authenticationSuccessful()) { 913 // If we have received features after the SASL has been successfully completed, then we 914 // have also *maybe* received, as it is an optional feature, the compression feature 915 // from the server. 916 maybeCompressFeaturesReceived.reportSuccess(); 917 } 918 } 919 920 /** 921 * Resets the parser using the latest connection's reader. Reseting the parser is necessary 922 * when the plain connection has been secured or when a new opening stream element is going 923 * to be sent by the server. 924 * 925 * @throws SmackException if the parser could not be reset. 926 */ 927 void openStream() throws SmackException { 928 // If possible, provide the receiving entity of the stream open tag, i.e. the server, as much information as 929 // possible. The 'to' attribute is *always* available. The 'from' attribute if set by the user and no external 930 // mechanism is used to determine the local entity (user). And the 'id' attribute is available after the first 931 // response from the server (see e.g. RFC 6120 ยง 9.1.1 Step 2.) 932 CharSequence to = getServiceName(); 933 CharSequence from = null; 934 CharSequence localpart = config.getUsername(); 935 if (localpart != null) { 936 from = XmppStringUtils.completeJidFrom(localpart, to); 937 } 938 String id = getStreamId(); 939 send(new StreamOpen(to, from, id)); 940 try { 941 packetReader.parser = PacketParserUtils.newXmppParser(reader); 942 } 943 catch (XmlPullParserException e) { 944 throw new SmackException(e); 945 } 946 } 947 948 protected class PacketReader { 949 950 XmlPullParser parser; 951 952 private volatile boolean done; 953 954 /** 955 * Initializes the reader in order to be used. The reader is initialized during the 956 * first connection and when reconnecting due to an abruptly disconnection. 957 */ 958 void init() { 959 done = false; 960 961 Async.go(new Runnable() { 962 public void run() { 963 parsePackets(); 964 } 965 }, "Smack Packet Reader (" + getConnectionCounter() + ")"); 966 } 967 968 /** 969 * Shuts the stanza(/packet) reader down. This method simply sets the 'done' flag to true. 970 */ 971 void shutdown() { 972 done = true; 973 } 974 975 /** 976 * Parse top-level packets in order to process them further. 977 * 978 * @param thread the thread that is being used by the reader to parse incoming packets. 979 */ 980 private void parsePackets() { 981 try { 982 initalOpenStreamSend.checkIfSuccessOrWait(); 983 int eventType = parser.getEventType(); 984 while (!done) { 985 switch (eventType) { 986 case XmlPullParser.START_TAG: 987 final String name = parser.getName(); 988 switch (name) { 989 case Message.ELEMENT: 990 case IQ.IQ_ELEMENT: 991 case Presence.ELEMENT: 992 try { 993 parseAndProcessStanza(parser); 994 } finally { 995 clientHandledStanzasCount = SMUtils.incrementHeight(clientHandledStanzasCount); 996 } 997 break; 998 case "stream": 999 // We found an opening stream. 1000 if ("jabber:client".equals(parser.getNamespace(null))) { 1001 streamId = parser.getAttributeValue("", "id"); 1002 String reportedServiceName = parser.getAttributeValue("", "from"); 1003 assert(reportedServiceName.equals(config.getServiceName())); 1004 } 1005 break; 1006 case "error": 1007 throw new StreamErrorException(PacketParserUtils.parseStreamError(parser)); 1008 case "features": 1009 parseFeatures(parser); 1010 break; 1011 case "proceed": 1012 try { 1013 // Secure the connection by negotiating TLS 1014 proceedTLSReceived(); 1015 // Send a new opening stream to the server 1016 openStream(); 1017 } 1018 catch (Exception e) { 1019 // We report any failure regarding TLS in the second stage of XMPP 1020 // connection establishment, namely the SASL authentication 1021 saslFeatureReceived.reportFailure(new SmackException(e)); 1022 throw e; 1023 } 1024 break; 1025 case "failure": 1026 String namespace = parser.getNamespace(null); 1027 switch (namespace) { 1028 case "urn:ietf:params:xml:ns:xmpp-tls": 1029 // TLS negotiation has failed. The server will close the connection 1030 // TODO Parse failure stanza 1031 throw new XMPPErrorException("TLS negotiation has failed", null); 1032 case "http://jabber.org/protocol/compress": 1033 // Stream compression has been denied. This is a recoverable 1034 // situation. It is still possible to authenticate and 1035 // use the connection but using an uncompressed connection 1036 // TODO Parse failure stanza 1037 compressSyncPoint.reportFailure(new XMPPErrorException( 1038 "Could not establish compression", null)); 1039 break; 1040 case SaslStreamElements.NAMESPACE: 1041 // SASL authentication has failed. The server may close the connection 1042 // depending on the number of retries 1043 final SASLFailure failure = PacketParserUtils.parseSASLFailure(parser); 1044 getSASLAuthentication().authenticationFailed(failure); 1045 break; 1046 } 1047 break; 1048 case Challenge.ELEMENT: 1049 // The server is challenging the SASL authentication made by the client 1050 String challengeData = parser.nextText(); 1051 getSASLAuthentication().challengeReceived(challengeData); 1052 break; 1053 case Success.ELEMENT: 1054 Success success = new Success(parser.nextText()); 1055 // We now need to bind a resource for the connection 1056 // Open a new stream and wait for the response 1057 openStream(); 1058 // The SASL authentication with the server was successful. The next step 1059 // will be to bind the resource 1060 getSASLAuthentication().authenticated(success); 1061 break; 1062 case Compressed.ELEMENT: 1063 // Server confirmed that it's possible to use stream compression. Start 1064 // stream compression 1065 // Initialize the reader and writer with the new compressed version 1066 initReaderAndWriter(); 1067 // Send a new opening stream to the server 1068 openStream(); 1069 // Notify that compression is being used 1070 compressSyncPoint.reportSuccess(); 1071 break; 1072 case Enabled.ELEMENT: 1073 Enabled enabled = ParseStreamManagement.enabled(parser); 1074 if (enabled.isResumeSet()) { 1075 smSessionId = enabled.getId(); 1076 if (StringUtils.isNullOrEmpty(smSessionId)) { 1077 XMPPErrorException xmppException = new XMPPErrorException( 1078 "Stream Management 'enabled' element with resume attribute but without session id received", 1079 new XMPPError( 1080 XMPPError.Condition.bad_request)); 1081 smEnabledSyncPoint.reportFailure(xmppException); 1082 throw xmppException; 1083 } 1084 smServerMaxResumptimTime = enabled.getMaxResumptionTime(); 1085 } else { 1086 // Mark this a non-resumable stream by setting smSessionId to null 1087 smSessionId = null; 1088 } 1089 clientHandledStanzasCount = 0; 1090 smWasEnabledAtLeastOnce = true; 1091 smEnabledSyncPoint.reportSuccess(); 1092 LOGGER.fine("Stream Management (XEP-198): succesfully enabled"); 1093 break; 1094 case Failed.ELEMENT: 1095 Failed failed = ParseStreamManagement.failed(parser); 1096 XMPPError xmppError = new XMPPError(failed.getXMPPErrorCondition()); 1097 XMPPException xmppException = new XMPPErrorException("Stream Management failed", xmppError); 1098 // If only XEP-198 would specify different failure elements for the SM 1099 // enable and SM resume failure case. But this is not the case, so we 1100 // need to determine if this is a 'Failed' response for either 'Enable' 1101 // or 'Resume'. 1102 if (smResumedSyncPoint.requestSent()) { 1103 smResumedSyncPoint.reportFailure(xmppException); 1104 } 1105 else { 1106 if (!smEnabledSyncPoint.requestSent()) { 1107 throw new IllegalStateException("Failed element received but SM was not previously enabled"); 1108 } 1109 smEnabledSyncPoint.reportFailure(xmppException); 1110 // Report success for last lastFeaturesReceived so that in case a 1111 // failed resumption, we can continue with normal resource binding. 1112 // See text of XEP-198 5. below Example 11. 1113 lastFeaturesReceived.reportSuccess(); 1114 } 1115 break; 1116 case Resumed.ELEMENT: 1117 Resumed resumed = ParseStreamManagement.resumed(parser); 1118 if (!smSessionId.equals(resumed.getPrevId())) { 1119 throw new StreamIdDoesNotMatchException(smSessionId, resumed.getPrevId()); 1120 } 1121 // Mark SM as enabled and resumption as successful. 1122 smResumedSyncPoint.reportSuccess(); 1123 smEnabledSyncPoint.reportSuccess(); 1124 // First, drop the stanzas already handled by the server 1125 processHandledCount(resumed.getHandledCount()); 1126 // Then re-send what is left in the unacknowledged queue 1127 List<Stanza> stanzasToResend = new ArrayList<>(unacknowledgedStanzas.size()); 1128 unacknowledgedStanzas.drainTo(stanzasToResend); 1129 for (Stanza stanza : stanzasToResend) { 1130 sendStanzaInternal(stanza); 1131 } 1132 // If there where stanzas resent, then request a SM ack for them. 1133 // Writer's sendStreamElement() won't do it automatically based on 1134 // predicates. 1135 if (!stanzasToResend.isEmpty()) { 1136 requestSmAcknowledgementInternal(); 1137 } 1138 LOGGER.fine("Stream Management (XEP-198): Stream resumed"); 1139 break; 1140 case AckAnswer.ELEMENT: 1141 AckAnswer ackAnswer = ParseStreamManagement.ackAnswer(parser); 1142 processHandledCount(ackAnswer.getHandledCount()); 1143 break; 1144 case AckRequest.ELEMENT: 1145 ParseStreamManagement.ackRequest(parser); 1146 if (smEnabledSyncPoint.wasSuccessful()) { 1147 sendSmAcknowledgementInternal(); 1148 } else { 1149 LOGGER.warning("SM Ack Request received while SM is not enabled"); 1150 } 1151 break; 1152 default: 1153 LOGGER.warning("Unknown top level stream element: " + name); 1154 break; 1155 } 1156 break; 1157 case XmlPullParser.END_TAG: 1158 if (parser.getName().equals("stream")) { 1159 // Disconnect the connection 1160 disconnect(); 1161 } 1162 break; 1163 case XmlPullParser.END_DOCUMENT: 1164 // END_DOCUMENT only happens in an error case, as otherwise we would see a 1165 // closing stream element before. 1166 throw new SmackException( 1167 "Parser got END_DOCUMENT event. This could happen e.g. if the server closed the connection without sending a closing stream element"); 1168 } 1169 eventType = parser.next(); 1170 } 1171 } 1172 catch (Exception e) { 1173 // The exception can be ignored if the the connection is 'done' 1174 // or if the it was caused because the socket got closed 1175 if (!(done || isSocketClosed())) { 1176 // Close the connection and notify connection listeners of the 1177 // error. 1178 notifyConnectionError(e); 1179 } 1180 } 1181 } 1182 } 1183 1184 protected class PacketWriter { 1185 public static final int QUEUE_SIZE = XMPPTCPConnection.QUEUE_SIZE; 1186 1187 private final ArrayBlockingQueueWithShutdown<Element> queue = new ArrayBlockingQueueWithShutdown<Element>( 1188 QUEUE_SIZE, true); 1189 1190 /** 1191 * Needs to be protected for unit testing purposes. 1192 */ 1193 protected SynchronizationPoint<NoResponseException> shutdownDone = new SynchronizationPoint<NoResponseException>( 1194 XMPPTCPConnection.this); 1195 1196 /** 1197 * If set, the stanza(/packet) writer is shut down 1198 */ 1199 protected volatile Long shutdownTimestamp = null; 1200 1201 private volatile boolean instantShutdown; 1202 1203 /** 1204 * True if some preconditions are given to start the bundle and defer mechanism. 1205 * <p> 1206 * This will likely get set to true right after the start of the writer thread, because 1207 * {@link #nextStreamElement()} will check if {@link queue} is empty, which is probably the case, and then set 1208 * this field to true. 1209 * </p> 1210 */ 1211 private boolean shouldBundleAndDefer; 1212 1213 /** 1214 * Initializes the writer in order to be used. It is called at the first connection and also 1215 * is invoked if the connection is disconnected by an error. 1216 */ 1217 void init() { 1218 shutdownDone.init(); 1219 shutdownTimestamp = null; 1220 1221 if (unacknowledgedStanzas != null) { 1222 // It's possible that there are new stanzas in the writer queue that 1223 // came in while we were disconnected but resumable, drain those into 1224 // the unacknowledged queue so that they get resent now 1225 drainWriterQueueToUnacknowledgedStanzas(); 1226 } 1227 1228 queue.start(); 1229 Async.go(new Runnable() { 1230 @Override 1231 public void run() { 1232 writePackets(); 1233 } 1234 }, "Smack Packet Writer (" + getConnectionCounter() + ")"); 1235 } 1236 1237 private boolean done() { 1238 return shutdownTimestamp != null; 1239 } 1240 1241 protected void throwNotConnectedExceptionIfDoneAndResumptionNotPossible() throws NotConnectedException { 1242 if (done() && !isSmResumptionPossible()) { 1243 // Don't throw a NotConnectedException is there is an resumable stream available 1244 throw new NotConnectedException(); 1245 } 1246 } 1247 1248 /** 1249 * Sends the specified element to the server. 1250 * 1251 * @param element the element to send. 1252 * @throws NotConnectedException 1253 */ 1254 protected void sendStreamElement(Element element) throws NotConnectedException { 1255 throwNotConnectedExceptionIfDoneAndResumptionNotPossible(); 1256 1257 boolean enqueued = false; 1258 while (!enqueued) { 1259 try { 1260 queue.put(element); 1261 enqueued = true; 1262 } 1263 catch (InterruptedException e) { 1264 throwNotConnectedExceptionIfDoneAndResumptionNotPossible(); 1265 // If the method above did not throw, then the sending thread was interrupted 1266 // TODO in a later version of Smack the InterruptedException should be thrown to 1267 // allow users to interrupt a sending thread that is currently blocking because 1268 // the queue is full. 1269 LOGGER.log(Level.WARNING, "Sending thread was interrupted", e); 1270 } 1271 } 1272 } 1273 1274 /** 1275 * Shuts down the stanza(/packet) writer. Once this method has been called, no further 1276 * packets will be written to the server. 1277 */ 1278 void shutdown(boolean instant) { 1279 instantShutdown = instant; 1280 shutdownTimestamp = System.currentTimeMillis(); 1281 queue.shutdown(); 1282 try { 1283 shutdownDone.checkIfSuccessOrWait(); 1284 } 1285 catch (NoResponseException e) { 1286 LOGGER.log(Level.WARNING, "shutdownDone was not marked as successful by the writer thread", e); 1287 } 1288 } 1289 1290 /** 1291 * Maybe return the next available element from the queue for writing. If the queue is shut down <b>or</b> a 1292 * spurious interrupt occurs, <code>null</code> is returned. So it is important to check the 'done' condition in 1293 * that case. 1294 * 1295 * @return the next element for writing or null. 1296 */ 1297 private Element nextStreamElement() { 1298 // It is important the we check if the queue is empty before removing an element from it 1299 if (queue.isEmpty()) { 1300 shouldBundleAndDefer = true; 1301 } 1302 Element packet = null; 1303 try { 1304 packet = queue.take(); 1305 } 1306 catch (InterruptedException e) { 1307 if (!queue.isShutdown()) { 1308 // Users shouldn't try to interrupt the packet writer thread 1309 LOGGER.log(Level.WARNING, "Packet writer thread was interrupted. Don't do that. Use disconnect() instead.", e); 1310 } 1311 } 1312 return packet; 1313 } 1314 1315 private void writePackets() { 1316 try { 1317 openStream(); 1318 initalOpenStreamSend.reportSuccess(); 1319 // Write out packets from the queue. 1320 while (!done()) { 1321 Element element = nextStreamElement(); 1322 if (element == null) { 1323 continue; 1324 } 1325 1326 // Get a local version of the bundle and defer callback, in case it's unset 1327 // between the null check and the method invocation 1328 final BundleAndDeferCallback localBundleAndDeferCallback = bundleAndDeferCallback; 1329 // If the preconditions are given (e.g. bundleAndDefer callback is set, queue is 1330 // empty), then we could wait a bit for further stanzas attempting to decrease 1331 // our energy consumption 1332 if (localBundleAndDeferCallback != null && isAuthenticated() && shouldBundleAndDefer) { 1333 // Reset shouldBundleAndDefer to false, nextStreamElement() will set it to true once the 1334 // queue is empty again. 1335 shouldBundleAndDefer = false; 1336 final AtomicBoolean bundlingAndDeferringStopped = new AtomicBoolean(); 1337 final int bundleAndDeferMillis = localBundleAndDeferCallback.getBundleAndDeferMillis(new BundleAndDefer( 1338 bundlingAndDeferringStopped)); 1339 if (bundleAndDeferMillis > 0) { 1340 long remainingWait = bundleAndDeferMillis; 1341 final long waitStart = System.currentTimeMillis(); 1342 synchronized (bundlingAndDeferringStopped) { 1343 while (!bundlingAndDeferringStopped.get() && remainingWait > 0) { 1344 bundlingAndDeferringStopped.wait(remainingWait); 1345 remainingWait = bundleAndDeferMillis 1346 - (System.currentTimeMillis() - waitStart); 1347 } 1348 } 1349 } 1350 } 1351 1352 Stanza packet = null; 1353 if (element instanceof Stanza) { 1354 packet = (Stanza) element; 1355 } 1356 else if (element instanceof Enable) { 1357 // The client needs to add messages to the unacknowledged stanzas queue 1358 // right after it sent 'enabled'. Stanza will be added once 1359 // unacknowledgedStanzas is not null. 1360 unacknowledgedStanzas = new ArrayBlockingQueue<>(QUEUE_SIZE); 1361 } 1362 // Check if the stream element should be put to the unacknowledgedStanza 1363 // queue. Note that we can not do the put() in sendStanzaInternal() and the 1364 // packet order is not stable at this point (sendStanzaInternal() can be 1365 // called concurrently). 1366 if (unacknowledgedStanzas != null && packet != null) { 1367 // If the unacknowledgedStanza queue is nearly full, request an new ack 1368 // from the server in order to drain it 1369 if (unacknowledgedStanzas.size() == 0.8 * XMPPTCPConnection.QUEUE_SIZE) { 1370 writer.write(AckRequest.INSTANCE.toXML().toString()); 1371 writer.flush(); 1372 } 1373 try { 1374 // It is important the we put the stanza in the unacknowledged stanza 1375 // queue before we put it on the wire 1376 unacknowledgedStanzas.put(packet); 1377 } 1378 catch (InterruptedException e) { 1379 throw new IllegalStateException(e); 1380 } 1381 } 1382 writer.write(element.toXML().toString()); 1383 if (queue.isEmpty()) { 1384 writer.flush(); 1385 } 1386 if (packet != null) { 1387 firePacketSendingListeners(packet); 1388 } 1389 } 1390 if (!instantShutdown) { 1391 // Flush out the rest of the queue. 1392 try { 1393 while (!queue.isEmpty()) { 1394 Element packet = queue.remove(); 1395 writer.write(packet.toXML().toString()); 1396 } 1397 writer.flush(); 1398 } 1399 catch (Exception e) { 1400 LOGGER.log(Level.WARNING, 1401 "Exception flushing queue during shutdown, ignore and continue", 1402 e); 1403 } 1404 1405 // Close the stream. 1406 try { 1407 writer.write("</stream:stream>"); 1408 writer.flush(); 1409 } 1410 catch (Exception e) { 1411 LOGGER.log(Level.WARNING, "Exception writing closing stream element", e); 1412 } 1413 // Delete the queue contents (hopefully nothing is left). 1414 queue.clear(); 1415 } else if (instantShutdown && isSmEnabled()) { 1416 // This was an instantShutdown and SM is enabled, drain all remaining stanzas 1417 // into the unacknowledgedStanzas queue 1418 drainWriterQueueToUnacknowledgedStanzas(); 1419 } 1420 1421 try { 1422 writer.close(); 1423 } 1424 catch (Exception e) { 1425 // Do nothing 1426 } 1427 1428 } 1429 catch (Exception e) { 1430 // The exception can be ignored if the the connection is 'done' 1431 // or if the it was caused because the socket got closed 1432 if (!(done() || isSocketClosed())) { 1433 notifyConnectionError(e); 1434 } else { 1435 LOGGER.log(Level.FINE, "Ignoring Exception in writePackets()", e); 1436 } 1437 } finally { 1438 LOGGER.fine("Reporting shutdownDone success in writer thread"); 1439 shutdownDone.reportSuccess(); 1440 } 1441 } 1442 1443 private void drainWriterQueueToUnacknowledgedStanzas() { 1444 List<Element> elements = new ArrayList<Element>(queue.size()); 1445 queue.drainTo(elements); 1446 for (Element element : elements) { 1447 if (element instanceof Stanza) { 1448 unacknowledgedStanzas.add((Stanza) element); 1449 } 1450 } 1451 } 1452 } 1453 1454 /** 1455 * Set if Stream Management should be used by default for new connections. 1456 * 1457 * @param useSmDefault true to use Stream Management for new connections. 1458 */ 1459 public static void setUseStreamManagementDefault(boolean useSmDefault) { 1460 XMPPTCPConnection.useSmDefault = useSmDefault; 1461 } 1462 1463 /** 1464 * Set if Stream Management resumption should be used by default for new connections. 1465 * 1466 * @param useSmResumptionDefault true to use Stream Management resumption for new connections. 1467 * @deprecated use {@link #setUseStreamManagementResumptionDefault(boolean)} instead. 1468 */ 1469 @Deprecated 1470 public static void setUseStreamManagementResumptiodDefault(boolean useSmResumptionDefault) { 1471 setUseStreamManagementResumptionDefault(useSmResumptionDefault); 1472 } 1473 1474 /** 1475 * Set if Stream Management resumption should be used by default for new connections. 1476 * 1477 * @param useSmResumptionDefault true to use Stream Management resumption for new connections. 1478 */ 1479 public static void setUseStreamManagementResumptionDefault(boolean useSmResumptionDefault) { 1480 if (useSmResumptionDefault) { 1481 // Also enable SM is resumption is enabled 1482 setUseStreamManagementDefault(useSmResumptionDefault); 1483 } 1484 XMPPTCPConnection.useSmResumptionDefault = useSmResumptionDefault; 1485 } 1486 1487 /** 1488 * Set if Stream Management should be used if supported by the server. 1489 * 1490 * @param useSm true to use Stream Management. 1491 */ 1492 public void setUseStreamManagement(boolean useSm) { 1493 this.useSm = useSm; 1494 } 1495 1496 /** 1497 * Set if Stream Management resumption should be used if supported by the server. 1498 * 1499 * @param useSmResumption true to use Stream Management resumption. 1500 */ 1501 public void setUseStreamManagementResumption(boolean useSmResumption) { 1502 if (useSmResumption) { 1503 // Also enable SM is resumption is enabled 1504 setUseStreamManagement(useSmResumption); 1505 } 1506 this.useSmResumption = useSmResumption; 1507 } 1508 1509 /** 1510 * Set the preferred resumption time in seconds. 1511 * @param resumptionTime the preferred resumption time in seconds 1512 */ 1513 public void setPreferredResumptionTime(int resumptionTime) { 1514 smClientMaxResumptionTime = resumptionTime; 1515 } 1516 1517 /** 1518 * Add a predicate for Stream Management acknowledgment requests. 1519 * <p> 1520 * Those predicates are used to determine when a Stream Management acknowledgement request is send to the server. 1521 * Some pre-defined predicates are found in the <code>org.jivesoftware.smack.sm.predicates</code> package. 1522 * </p> 1523 * <p> 1524 * If not predicate is configured, the {@link Predicate#forMessagesOrAfter5Stanzas()} will be used. 1525 * </p> 1526 * 1527 * @param predicate the predicate to add. 1528 * @return if the predicate was not already active. 1529 */ 1530 public boolean addRequestAckPredicate(StanzaFilter predicate) { 1531 synchronized (requestAckPredicates) { 1532 return requestAckPredicates.add(predicate); 1533 } 1534 } 1535 1536 /** 1537 * Remove the given predicate for Stream Management acknowledgment request. 1538 * @param predicate the predicate to remove. 1539 * @return true if the predicate was removed. 1540 */ 1541 public boolean removeRequestAckPredicate(StanzaFilter predicate) { 1542 synchronized (requestAckPredicates) { 1543 return requestAckPredicates.remove(predicate); 1544 } 1545 } 1546 1547 /** 1548 * Remove all predicates for Stream Management acknowledgment requests. 1549 */ 1550 public void removeAllRequestAckPredicates() { 1551 synchronized (requestAckPredicates) { 1552 requestAckPredicates.clear(); 1553 } 1554 } 1555 1556 /** 1557 * Send an unconditional Stream Management acknowledgement request to the server. 1558 * 1559 * @throws StreamManagementNotEnabledException if Stream Mangement is not enabled. 1560 * @throws NotConnectedException if the connection is not connected. 1561 */ 1562 public void requestSmAcknowledgement() throws StreamManagementNotEnabledException, NotConnectedException { 1563 if (!isSmEnabled()) { 1564 throw new StreamManagementException.StreamManagementNotEnabledException(); 1565 } 1566 requestSmAcknowledgementInternal(); 1567 } 1568 1569 private void requestSmAcknowledgementInternal() throws NotConnectedException { 1570 packetWriter.sendStreamElement(AckRequest.INSTANCE); 1571 } 1572 1573 /** 1574 * Send a unconditional Stream Management acknowledgment to the server. 1575 * <p> 1576 * See <a href="http://xmpp.org/extensions/xep-0198.html#acking">XEP-198: Stream Management ยง 4. Acks</a>: 1577 * "Either party MAY send an <a/> element at any time (e.g., after it has received a certain number of stanzas, 1578 * or after a certain period of time), even if it has not received an <r/> element from the other party." 1579 * </p> 1580 * 1581 * @throws StreamManagementNotEnabledException if Stream Management is not enabled. 1582 * @throws NotConnectedException if the connection is not connected. 1583 */ 1584 public void sendSmAcknowledgement() throws StreamManagementNotEnabledException, NotConnectedException { 1585 if (!isSmEnabled()) { 1586 throw new StreamManagementException.StreamManagementNotEnabledException(); 1587 } 1588 sendSmAcknowledgementInternal(); 1589 } 1590 1591 private void sendSmAcknowledgementInternal() throws NotConnectedException { 1592 packetWriter.sendStreamElement(new AckAnswer(clientHandledStanzasCount)); 1593 } 1594 1595 /** 1596 * Add a Stanza acknowledged listener. 1597 * <p> 1598 * Those listeners will be invoked every time a Stanza has been acknowledged by the server. The will not get 1599 * automatically removed. Consider using {@link #addStanzaIdAcknowledgedListener(String, StanzaListener)} when 1600 * possible. 1601 * </p> 1602 * 1603 * @param listener the listener to add. 1604 */ 1605 public void addStanzaAcknowledgedListener(StanzaListener listener) { 1606 stanzaAcknowledgedListeners.add(listener); 1607 } 1608 1609 /** 1610 * Remove the given Stanza acknowledged listener. 1611 * 1612 * @param listener the listener. 1613 * @return true if the listener was removed. 1614 */ 1615 public boolean removeStanzaAcknowledgedListener(StanzaListener listener) { 1616 return stanzaAcknowledgedListeners.remove(listener); 1617 } 1618 1619 /** 1620 * Remove all stanza acknowledged listeners. 1621 */ 1622 public void removeAllStanzaAcknowledgedListeners() { 1623 stanzaAcknowledgedListeners.clear(); 1624 } 1625 1626 /** 1627 * Add a new Stanza ID acknowledged listener for the given ID. 1628 * <p> 1629 * The listener will be invoked if the stanza with the given ID was acknowledged by the server. It will 1630 * automatically be removed after the listener was run. 1631 * </p> 1632 * 1633 * @param id the stanza ID. 1634 * @param listener the listener to invoke. 1635 * @return the previous listener for this stanza ID or null. 1636 * @throws StreamManagementNotEnabledException if Stream Management is not enabled. 1637 */ 1638 public StanzaListener addStanzaIdAcknowledgedListener(final String id, StanzaListener listener) throws StreamManagementNotEnabledException { 1639 // Prevent users from adding callbacks that will never get removed 1640 if (!smWasEnabledAtLeastOnce) { 1641 throw new StreamManagementException.StreamManagementNotEnabledException(); 1642 } 1643 // Remove the listener after max. 12 hours 1644 final int removeAfterSeconds = Math.min(getMaxSmResumptionTime(), 12 * 60 * 60); 1645 schedule(new Runnable() { 1646 @Override 1647 public void run() { 1648 stanzaIdAcknowledgedListeners.remove(id); 1649 } 1650 }, removeAfterSeconds, TimeUnit.SECONDS); 1651 return stanzaIdAcknowledgedListeners.put(id, listener); 1652 } 1653 1654 /** 1655 * Remove the Stanza ID acknowledged listener for the given ID. 1656 * 1657 * @param id the stanza ID. 1658 * @return true if the listener was found and removed, false otherwise. 1659 */ 1660 public StanzaListener removeStanzaIdAcknowledgedListener(String id) { 1661 return stanzaIdAcknowledgedListeners.remove(id); 1662 } 1663 1664 /** 1665 * Removes all Stanza ID acknowledged listeners. 1666 */ 1667 public void removeAllStanzaIdAcknowledgedListeners() { 1668 stanzaIdAcknowledgedListeners.clear(); 1669 } 1670 1671 /** 1672 * Returns true if Stream Management is supported by the server. 1673 * 1674 * @return true if Stream Management is supported by the server. 1675 */ 1676 public boolean isSmAvailable() { 1677 return hasFeature(StreamManagementFeature.ELEMENT, StreamManagement.NAMESPACE); 1678 } 1679 1680 /** 1681 * Returns true if Stream Management was successfully negotiated with the server. 1682 * 1683 * @return true if Stream Management was negotiated. 1684 */ 1685 public boolean isSmEnabled() { 1686 return smEnabledSyncPoint.wasSuccessful(); 1687 } 1688 1689 /** 1690 * Returns true if the stream was successfully resumed with help of Stream Management. 1691 * 1692 * @return true if the stream was resumed. 1693 */ 1694 public boolean streamWasResumed() { 1695 return smResumedSyncPoint.wasSuccessful(); 1696 } 1697 1698 /** 1699 * Returns true if the connection is disconnected by a Stream resumption via Stream Management is possible. 1700 * 1701 * @return true if disconnected but resumption possible. 1702 */ 1703 public boolean isDisconnectedButSmResumptionPossible() { 1704 return disconnectedButResumeable && isSmResumptionPossible(); 1705 } 1706 1707 /** 1708 * Returns true if the stream is resumable. 1709 * 1710 * @return true if the stream is resumable. 1711 */ 1712 public boolean isSmResumptionPossible() { 1713 // There is no resumable stream available 1714 if (smSessionId == null) 1715 return false; 1716 1717 final Long shutdownTimestamp = packetWriter.shutdownTimestamp; 1718 // Seems like we are already reconnected, report true 1719 if (shutdownTimestamp == null) { 1720 return true; 1721 } 1722 1723 // See if resumption time is over 1724 long current = System.currentTimeMillis(); 1725 long maxResumptionMillies = ((long) getMaxSmResumptionTime()) * 1000; 1726 if (current > shutdownTimestamp + maxResumptionMillies) { 1727 // Stream resumption is *not* possible if the current timestamp is greater then the greatest timestamp where 1728 // resumption is possible 1729 return false; 1730 } else { 1731 return true; 1732 } 1733 } 1734 1735 /** 1736 * Drop the stream management state. Sets {@link #smSessionId} and 1737 * {@link #unacknowledgedStanzas} to <code>null</code>. 1738 */ 1739 private void dropSmState() { 1740 // clientHandledCount and serverHandledCount will be reset on <enable/> and <enabled/> 1741 // respective. No need to reset them here. 1742 smSessionId = null; 1743 unacknowledgedStanzas = null; 1744 } 1745 1746 /** 1747 * Get the maximum resumption time in seconds after which a managed stream can be resumed. 1748 * <p> 1749 * This method will return {@link Integer#MAX_VALUE} if neither the client nor the server specify a maximum 1750 * resumption time. Be aware of integer overflows when using this value, e.g. do not add arbitrary values to it 1751 * without checking for overflows before. 1752 * </p> 1753 * 1754 * @return the maximum resumption time in seconds or {@link Integer#MAX_VALUE} if none set. 1755 */ 1756 public int getMaxSmResumptionTime() { 1757 int clientResumptionTime = smClientMaxResumptionTime > 0 ? smClientMaxResumptionTime : Integer.MAX_VALUE; 1758 int serverResumptionTime = smServerMaxResumptimTime > 0 ? smServerMaxResumptimTime : Integer.MAX_VALUE; 1759 return Math.min(clientResumptionTime, serverResumptionTime); 1760 } 1761 1762 private void processHandledCount(long handledCount) throws StreamManagementCounterError { 1763 long ackedStanzasCount = SMUtils.calculateDelta(handledCount, serverHandledStanzasCount); 1764 final List<Stanza> ackedStanzas = new ArrayList<Stanza>( 1765 ackedStanzasCount <= Integer.MAX_VALUE ? (int) ackedStanzasCount 1766 : Integer.MAX_VALUE); 1767 for (long i = 0; i < ackedStanzasCount; i++) { 1768 Stanza ackedStanza = unacknowledgedStanzas.poll(); 1769 // If the server ack'ed a stanza, then it must be in the 1770 // unacknowledged stanza queue. There can be no exception. 1771 if (ackedStanza == null) { 1772 throw new StreamManagementCounterError(handledCount, serverHandledStanzasCount, 1773 ackedStanzasCount, ackedStanzas); 1774 } 1775 ackedStanzas.add(ackedStanza); 1776 } 1777 1778 boolean atLeastOneStanzaAcknowledgedListener = false; 1779 if (!stanzaAcknowledgedListeners.isEmpty()) { 1780 // If stanzaAcknowledgedListeners is not empty, the we have at least one 1781 atLeastOneStanzaAcknowledgedListener = true; 1782 } 1783 else { 1784 // Otherwise we look for a matching id in the stanza *id* acknowledged listeners 1785 for (Stanza ackedStanza : ackedStanzas) { 1786 String id = ackedStanza.getStanzaId(); 1787 if (id != null && stanzaIdAcknowledgedListeners.containsKey(id)) { 1788 atLeastOneStanzaAcknowledgedListener = true; 1789 break; 1790 } 1791 } 1792 } 1793 1794 // Only spawn a new thread if there is a chance that some listener is invoked 1795 if (atLeastOneStanzaAcknowledgedListener) { 1796 asyncGo(new Runnable() { 1797 @Override 1798 public void run() { 1799 for (Stanza ackedStanza : ackedStanzas) { 1800 for (StanzaListener listener : stanzaAcknowledgedListeners) { 1801 try { 1802 listener.processPacket(ackedStanza); 1803 } 1804 catch (NotConnectedException e) { 1805 LOGGER.log(Level.FINER, "Received not connected exception", e); 1806 } 1807 } 1808 String id = ackedStanza.getStanzaId(); 1809 if (StringUtils.isNullOrEmpty(id)) { 1810 continue; 1811 } 1812 StanzaListener listener = stanzaIdAcknowledgedListeners.remove(id); 1813 if (listener != null) { 1814 try { 1815 listener.processPacket(ackedStanza); 1816 } 1817 catch (NotConnectedException e) { 1818 LOGGER.log(Level.FINER, "Received not connected exception", e); 1819 } 1820 } 1821 } 1822 } 1823 }); 1824 } 1825 1826 serverHandledStanzasCount = handledCount; 1827 } 1828 1829 /** 1830 * Set the default bundle and defer callback used for new connections. 1831 * 1832 * @param defaultBundleAndDeferCallback 1833 * @see BundleAndDeferCallback 1834 * @since 4.1 1835 */ 1836 public static void setDefaultBundleAndDeferCallback(BundleAndDeferCallback defaultBundleAndDeferCallback) { 1837 XMPPTCPConnection.defaultBundleAndDeferCallback = defaultBundleAndDeferCallback; 1838 } 1839 1840 /** 1841 * Set the bundle and defer callback used for this connection. 1842 * <p> 1843 * You can use <code>null</code> as argument to reset the callback. Outgoing stanzas will then 1844 * no longer get deferred. 1845 * </p> 1846 * 1847 * @param bundleAndDeferCallback the callback or <code>null</code>. 1848 * @see BundleAndDeferCallback 1849 * @since 4.1 1850 */ 1851 public void setBundleandDeferCallback(BundleAndDeferCallback bundleAndDeferCallback) { 1852 this.bundleAndDeferCallback = bundleAndDeferCallback; 1853 } 1854 1855}