Commit Graph

244 Commits

Author SHA1 Message Date
Florian Schmaus 525ee09ea1 [tcp] Do not send SM ack after we send a </stream:stream>
Do net put an ack to the queue if it has already been shutdown. Some
servers, like ejabberd, like to request an ack even after we have send
a stream close (and hance the queue was shutdown). If we would not
check here, then the ack would dangle around in the queue, and be send
on the next re-connection attempt even before the stream open.

See the following trace of the MUC bookmarks integration test. The
fact that it is a MUC test does not matter, but this test does
disconnect the connection and reconnect it. Not how the server,
ejabberd in this case, requests an SM ack by sending an <r/> even
though we already send the </stream:stream>:

22:22:05 SENT (4):
<iq id='MD4UC-61' type='set'>
  <query xmlns='jabber:iq:private'>
    <storage xmlns='storage:bookmarks'>
      <conference name='Smack Inttest: 7in7j' autojoin='true' jid='y9jcn5@conference.salem.geekplace.eu'>
        <nick>
          Nick-P2VXD7
        </nick>
      </conference>
    </storage>
  </query>
</iq>
22:22:05 RECV (4):
<r xmlns='urn:xmpp:sm:3'/>
22:22:05 SENT (4):
<a xmlns='urn:xmpp:sm:3' h='29'/>
22:22:05 RECV (4):
<message to='sinttest-7in7j-4@salem.geekplace.eu' from='sinttest-7in7j-4@salem.geekplace.eu' type='headline'>
  <event xmlns='http://jabber.org/protocol/pubsub#event'>
    <items node='storage:bookmarks'>
      <item id='current'>
        <storage xmlns='storage:bookmarks'>
          <conference name='Smack Inttest: 7in7j' autojoin='true' jid='y9jcn5@conference.salem.geekplace.eu'>
            <nick>
              Nick-P2VXD7
            </nick>
          </conference>
        </storage>
      </item>
    </items>
  </event>
  <addresses xmlns='http://jabber.org/protocol/address'>
    <address jid='sinttest-7in7j-4@salem.geekplace.eu/1415073683806426185213090' type='replyto'/>
  </addresses>
</message>
22:22:05 RECV (4):
<iq xml:lang='en-US' to='sinttest-7in7j-4@salem.geekplace.eu/1415073683806426185213090' from='sinttest-7in7j-4@salem.geekplace.eu' type='result' id='MD4UC-61'/>
22:22:05 SENT (4):
<presence id='6MS6J-20' type='unavailable'/>
<a xmlns='urn:xmpp:sm:3' h='31'/>
<!-- We have closed the stream -->
</stream:stream>
<!-- But the server still requests an SM ack -->
22:22:05 RECV (4):
<r xmlns='urn:xmpp:sm:3'/>
22:22:05 RECV (4):
</stream:stream>
22:22:05 XMPPConnection closed (XMPPTCPConnection[sinttest-7in7j-4@salem.geekplace.eu/1415073683806426185213090] (4))
22:22:05 SENT (4):
<a xmlns='urn:xmpp:sm:3' h='31'/>
22:22:05 SENT (4):
<stream:stream xmlns='jabber:client' to='salem.geekplace.eu' xmlns:stream='http://etherx.jabber.org/streams' version='1.0' from='sinttest-7in7j-4@salem.geekplace.eu' xml:lang='en-US'>
22:22:05 RECV (4): ?xml version='1.0'?>
<stream:stream id='3379123514446782311' ver
22:22:05 RECV (4): sion='1.0' xml:lang='en' xmlns:stream='http://etherx.jabber.org/streams' xmlns='jabber:client'>
<stream:error>
  <invalid-xml xmlns='urn:ietf:params:xml:ns:xmpp-streams'/>
</stream:error>
</stream:stream>
22:22:05 XMPPConnection closed due to an exception (XMPPTCPConnection[sinttest-7in7j-4@salem.geekplace.eu/1415073683806426185213090] (4))
org.jivesoftware.smack.XMPPException$StreamErrorException: invalid-xml You can read more about the meaning of this stream error at http://xmpp.org/rfcs/rfc6120.html#streams-error-conditions
<stream:error><invalid-xml xmlns='urn:ietf:params:xml:ns:xmpp-streams'/></stream:error>
	at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader.parsePackets(XMPPTCPConnection.java:981)
	at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader.access$700(XMPPTCPConnection.java:913)
	at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader$1.run(XMPPTCPConnection.java:936)
	at java.base/java.lang.Thread.run(Thread.java:834)
2020-09-18 14:36:26 +02:00
Florian Schmaus 49ebe8c587 [tcp] Drop Stream Management state on clean shutdown
We previously only set the SM session ID to zero, but that is not
enough. On a clean shutdown, i.e. where we send a </stream> close tag,
we also have to nullify the unacknowledgedStanzas queue.
2020-08-17 22:11:50 +02:00
Florian Schmaus 1a2a613112 Set 'connected' to 'true' as early as possible
We previously only set 'connected' after connectInternal()
returned. This could lead to notifyConnectionError() ignoring stream
error exceptions, e.g. when establishing TLS which happens also in
connectInternal(), because 'connected' was still 'false'.

2020-08-06 13:08:06.265 19830-20423/org.atalk.android D/SMACK: SENT (0):
    <stream:stream xmlns='jabber:client' to='atalk.sytes.net' xmlns:stream='http://etherx.jabber.org/streams' version='1.0' xml:lang='en'>
2020-08-06 13:08:06.333 19830-20424/org.atalk.android D/SMACK: RECV (0): ?xml version='1.0'?>
    <stream:stream id='16420577292739412012' version='1.0' xml:lang='en' xmlns:stream='http://etherx.jabber.org/streams' from='atalk.sytes.net' xmlns='jabber:client'>
    <stream:error>
      <policy-violation xmlns='urn:ietf:params:xml:ns:xmpp-streams'/>
      <text xml:lang='en' xmlns='urn:ietf:params:xml:ns:xmpp-streams'>
        Too many (20) failed authentications from this IP address (::ffff:42.60.7.13). The address will be unblocked at 05:15:34 06.08.2020 UTC
      </text>
    </stream:error>
    </stream:stream>
2020-08-06 13:08:06.346 19830-20424/org.atalk.android I/aTalk: [241896] org.jivesoftware.smack.AbstractXMPPConnection.notifyConnectionError() Connection was already disconnected when attempting to handle org.jivesoftware.smack.XMPPException$StreamErrorException: policy-violation You can read more about the meaning of this stream error at http://xmpp.org/rfcs/rfc6120.html#streams-error-conditions
    <stream:error><policy-violation xmlns='urn:ietf:params:xml:ns:xmpp-streams'/><text xml:lang='en'>Too many (20) failed authentications from this IP address (::ffff:42.60.7.13). The address will be unblocked at 05:15:34 06.08.2020 UTC</text></stream:error>
    org.jivesoftware.smack.XMPPException$StreamErrorException: policy-violation You can read more about the meaning of this stream error at http://xmpp.org/rfcs/rfc6120.html#streams-error-conditions
    <stream:error><policy-violation xmlns='urn:ietf:params:xml:ns:xmpp-streams'/><text xml:lang='en'>Too many (20) failed authentications from this IP address (::ffff:42.60.7.13). The address will be unblocked at 05:15:34 06.08.2020 UTC</text></stream:error>
        at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader.parsePackets(XMPPTCPConnection.java:966)
        at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader.access$700(XMPPTCPConnection.java:898)
        at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader$1.run(XMPPTCPConnection.java:921)
        at java.lang.Thread.run(Thread.java:919)

Which eventually leads to a NoResponseException

org.jivesoftware.smack.SmackException$NoResponseException: No response
received within reply timeout. Timeout was 30000ms (~30s). While
waiting for establishing TLS
[XMPPTCPConnection[not-authenticated] (4)]

We now set 'connected' to 'true' as soon as the transport (e.g. TCP,
BOSH, …) is connected. While this is in other ways also sensible, it
also allows notifyConnectionError() to handle exceptions in the early
connection stage.

Thanks to Eng Chong Meng for reporting this.
2020-08-06 16:32:26 +02:00
Florian Schmaus 1bd097ed9b
Merge pull request #411 from Flowdalic/sasl
SASL / getFeature()
2020-07-23 16:09:57 +02:00
Florian Schmaus 329948b442 Add XMPP.(get|has)Feature(Class|QName) and deprecate (String, String) 2020-07-23 14:32:14 +02:00
Florian Schmaus bcfe7b12a4 [tcp] Mark SM as disabled prior resource binding
Otherwise we may send a SM ack request with the bind IQ request,
causing a stream error:

D/SMACK: SENT (0):
    <iq id='SETVB-74' type='set'>
      <bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'>
      </bind>
    </iq>
    <r xmlns='urn:xmpp:sm:3'/>
D/SMACK: RECV (0):
    <iq id='SETVB-74' type='result'>
      <bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'>
        <jid>
          snakeman@wiuwiu.de/eHeTGlCq
        </jid>
      </bind>
    </iq>
    <stream:error>
      <unsupported-stanza-type xmlns='urn:ietf:params:xml:ns:xmpp-streams'/>
    </stream:error>
    </stream:stream>
2020-07-20 14:26:06 +02:00
Florian Schmaus 63f133e68b Set 'running' to true prior starting the reader/writer threads
To ensure the thread starting the reader/writer threads sees them
running and eventually waits until the 'running' boolean is reset to
'false' upon connection termination.
2020-06-17 21:56:45 +02:00
Florian Schmaus c384849532 Set 'running' to false before calling notifyConnectionError()
Since the current variant of notifyConnectionError() does not execute
most of its work in a new thread, especially since instantShutdown()
is called in the invoking thread, we have to mark the connections
reader or writer threads as no longer running prior them invoking
notifyConnectionError(). Otherwise they will end up waiting for
themselves to terminate.
2020-06-17 21:56:45 +02:00
Florian Schmaus 884ee327e1 Remove writerException in XMPPTCPConnection
This delay mechanism is no longer necessary.
2020-06-17 21:56:45 +02:00
Florian Schmaus a05b464032 Do not use waitForConditionOrConnectionException() in XMPPTCPConnection
Since at this point, there will potentially be an active connection
exception, which would cause the call to return immediately.
2020-06-17 21:56:45 +02:00
Florian Schmaus ddc39030d7 Rename waitForCondition() to waitForConditionOrConnectionException()
To make it clear that this will either return if the condition is
true *or* if a connection exception happened.

Also introduce waitFor(), which is deliberately not named
waitForCondition() because it carries a different semantic.
2020-06-17 21:56:45 +02:00
Florian Schmaus f9292a23fb [tcp] Add and improve log of reader/writer thread termination 2020-06-17 21:56:45 +02:00
Florian Schmaus 018cba7f4f [tcp] Log XmlStringBuilder NPEs and the causing class 2020-06-16 21:44:04 +02:00
Florian Schmaus cf1f533fff [tcp] Mark SM sync points as volatile 2020-06-12 21:16:05 +02:00
Florian Schmaus fd1b49ca8f [tcp] Set sync points before they are used, and not in init() 2020-06-12 18:15:40 +02:00
Florian Schmaus 843c8dd7fe [tcp] Add missing notifyWaitingThreads() after receiving SM <failed/> 2020-06-12 16:35:32 +02:00
Florian Schmaus 7d129d6f6c [tcp] Cleanup handling of stream errors in XMPPTCPConnection
There is no need to notify waiting threads, throwing the stream error
will also notify them. Also settings tlsHandled to true is no longer
necessary.
2020-05-31 19:49:42 +02:00
Florian Schmaus b7465e8200 [tcp] Do not needlessly wait for closing stream tag 2020-05-31 19:49:42 +02:00
Florian Schmaus 84b7adb764 [tcp] Remove javadoc throws annotation
This method does no longer throw.
2020-05-31 19:49:42 +02:00
Florian Schmaus 22baa74298 [tcp] Remove flush() in writer thread
We will flush the stream after the closing stream tag has been written
anyway. No need to do it here.
2020-05-31 19:49:42 +02:00
Florian Schmaus 57961a8cc1 Remove SynchronizationPoint
This continues the design started with e98d42790 ("SmackReactor/NIO,
Java8/Android19, Pretty print XML, FSM connections"), where the
exceptions that caused an operation to fail, are not recorded within
SynchronizationPoint but within the connection instance itself.
2020-05-31 19:48:47 +02:00
Florian Schmaus f5448c5faa [core] Rework TLS logic
This moves the logic in AbstractXMPPConnection.getSmackTlsContext()
into the ConnectionConfiguration constructor.

Also introduce SslContextFactory and use it in
ConnectionConfiguration.
2020-05-25 15:41:57 +02:00
Florian Schmaus ab2d3a2b79 [core] Deprecate AbstractConnectionListener 2020-05-13 22:14:43 +02:00
Florian Schmaus e2d393d00d tcp: add newline for better readability 2020-04-08 14:31:00 +02:00
Florian Schmaus 51b167c0d4 tcp: do not flush after writing the SM ack
There is no need to flush here, as writePackets() will eventually
flush the data out.
2020-04-08 14:31:00 +02:00
Florian Schmaus bfff412112 tcp: increase unack'ed stanza queue size, decrease ack request limit
To reduce the chances of a deadlock between read and writer if SM
unacked stanza queue is full. See SMACK-881.
2020-04-08 14:31:00 +02:00
Florian Schmaus cc636fff21 Introduce Smack's Modular Connection Architecture
This is a complete redesign of what was previously
XmppNioTcpConnection. The new architecture allows to extend an XMPP
client to server (c2s) connection with new transport bindings and
other extensions.
2020-04-04 13:03:31 +02:00
Florian Schmaus be2fc23f41 proxy: make it the caller's reponsibility to close the socket
This makes the code shorter as there is now a single place where the
socket should be closed.
2020-02-23 19:12:54 +01:00
Florian Schmaus eb4c2c5572 s/occured/occurred/ 2019-10-30 12:02:36 +01:00
Florian Schmaus eeb6c52f7e Move SASL logic into AbstractXMPPConnection
Besides the way the transport handles the stream after SASL
<success/>, the SASL logic is independend from the underlying
transport (BOSH, TCP, …). Hence move it up into
AbstractXMPPConnection.

This also has the benefit that we can make some more methods private
or package-private.

Also introduce XmlStringBuilder.optTextChild(), which causes some
associated changes.
2019-09-25 13:49:21 +02:00
Florian Schmaus bd4b91fc26 Introduce AbstractXMPPConnection.outgoingStreamXmlEnvironment 2019-09-23 16:12:48 +02:00
Florian Schmaus 1a3067c89b Enable werror for javadoc and fix javadoc issues 2019-08-30 12:08:30 +02:00
Florian Schmaus 4ca2c7cc69 Enable MissingJavadocPackage and UnnecessaryParentheses checkstyle checks 2019-07-24 09:18:39 +02:00
Florian Schmaus 7e25c3ada5 Merge branch '4.3' 2019-05-16 20:52:27 +02:00
Florian Schmaus 2ceadc0de1 Introduce verificationSuccessful boolean 2019-05-16 16:51:31 +02:00
Florian Schmaus f60e4055ec Use ACE representation of XMPP domain on certificate verification
This is based on the patch provided by Georg Lukas, modified to take
XMPP domain names that can not be represented as DNS names into
account. See c42d2eea24 (r269250137)

Fixes SMACK-870.
2019-05-16 14:14:10 +02:00
Florian Schmaus 4133eb175c Replace XPP3 by XmlPullParser interface wrapping StAX and XPP3
Introducing Smack's own XmlPullParser interface which tries to stay as
compatible as possible to XPP3. The interface is used to either wrap
StAX's XMLStreamReader if Smack is used on Java SE, and XPP3's
XmlPullParser if Smack is used on on Android.

Fixes SMACK-591.

Also introduce JUnit 5 and non-strict javadoc projects.
2019-05-06 22:10:50 +02:00
Florian Schmaus b3646abecd XMPPTCPConnection: Move openStream() call from writer into reader thread
and also call notifyConnectionError() on exception thrown by
openStream().

In hindsight I wonder why openStream() was ever called in the writer
thread, as it only caused unnecessary synchronization overhead, as can
be seen by the initialOpenStreamSend synchronization point.
2019-04-30 13:55:55 +02:00
Florian Schmaus d6b6fdca17 Add ConnectionConfiguration.setXmppAddressAndPassword() 2019-04-07 16:44:04 +02:00
Florian Schmaus d10319f1a0 Merge branch '4.3' 2019-04-05 10:22:12 +02:00
Florian Schmaus 25b3f35421 Ensure that shutdown() terminates reader/writer threads
In case an exception happens in connect()/login() the
'disconnectedButResumable' boolean may (still) be set. Which causes
only one of the reader and writer threads to exit, typically the
reader thread, because shutdown() will bail out very early. This
leaves a dangling (writer) thread causing memory leaks and deadlocks
on a subsequent connect()/login().
2019-03-28 17:58:25 +01:00
Florian Schmaus 5d46e281fc XMPPTCPConnection log when reader/writer threads start and exit 2019-03-26 17:23:27 +01:00
Florian Schmaus a23adfab6e Merge branch '4.3' 2019-03-24 00:16:24 +01:00
Georg Lukas 8e88cd2e31 Proxy mode: add failed address on error 2019-03-24 00:11:01 +01:00
Florian Schmaus 7059b60672 Wait for reader/writer thread termination at the end of shutdown()
This synchronizes the place with what the master branch currently
does.
2019-03-16 11:11:58 +01:00
Florian Schmaus 3ded023629 Remove 'synchronized' from notifyConnectionError()
as it is not neccessary and causes stalls and deadlocks with a
concurrent connect()/login() (which could be resolved interrupting the
connect()/login() calling thread):

"Smack Reader (0)" daemon prio=5 tid=21 Blocked
  | group="main" sCount=1 dsCount=0 obj=0x12c84670 self=0x7e072ca200
  | sysTid=14965 nice=0 cgrp=default sched=0/0 handle=0x7e06155450
  | state=S schedstat=( 63430626 3034269 21 ) utm=5 stm=0 core=2 HZ=100
  | stack=0x7e06053000-0x7e06055000 stackSize=1037KB
  | held mutexes=
  at org.jivesoftware.smack.tcp.XMPPTCPConnection.notifyConnectionError(XMPPTCPConnection.java:-1)
  - waiting to lock <0x0ec6e6bb> (a org.jivesoftware.smack.tcp.XMPPTCPConnection) held by thread 22
  at org.jivesoftware.smack.tcp.XMPPTCPConnection.access$3900(XMPPTCPConnection.java:154)
  at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader.parsePackets(XMPPTCPConnection.java:1330)
  at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader.access$900(XMPPTCPConnection.java:1064)
  at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader$1.run(XMPPTCPConnection.java:1081)
  at java.lang.Thread.run(Thread.java:761)

"connect" prio=5 tid=22 Waiting
  | group="main" sCount=1 dsCount=0 obj=0x12c8e820 self=0x7e19bcd600
  | sysTid=15047 nice=0 cgrp=default sched=0/0 handle=0x7e05ee4450
  | state=S schedstat=( 14067083 7475835 14 ) utm=1 stm=0 core=0 HZ=100
  | stack=0x7e05de2000-0x7e05de4000 stackSize=1037KB
  | held mutexes=
  at java.lang.Object.wait!(Native method)
  - waiting on <0x0c058b14> (a java.lang.Object)
  at java.lang.Thread.parkFor$(Thread.java:2127)
  - locked <0x0c058b14> (a java.lang.Object)
  at sun.misc.Unsafe.park(Unsafe.java:325)
  at java.util.concurrent.locks.LockSupport.park(LockSupport.java:161)
  at java.util.concurrent.locks.AbstractQueuedSynchronizer.parkAndCheckInterrupt(AbstractQueuedSynchronizer.java:840)
  at java.util.concurrent.locks.AbstractQueuedSynchronizer.doAcquireSharedInterruptibly(AbstractQueuedSynchronizer.java:994)
  at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireSharedInterruptibly(AbstractQueuedSynchronizer.java:1303)
  at java.util.concurrent.Semaphore.acquire(Semaphore.java:446)
  at org.jivesoftware.smack.tcp.XMPPTCPConnection.initConnection(XMPPTCPConnection.java:685)
  at org.jivesoftware.smack.tcp.XMPPTCPConnection.connectInternal(XMPPTCPConnection.java:942)
  at org.jivesoftware.smack.AbstractXMPPConnection.connect(AbstractXMPPConnection.java:417)
  - locked <0x0ec6e6bb> (a org.jivesoftware.smack.tcp.XMPPTCPConnection)
  at org.yaxim.androidclient.service.SmackableImp.connectAndLogin(SmackableImp.java:717)
  - locked <0x0ec6e6bb> (a org.jivesoftware.smack.tcp.XMPPTCPConnection)
  at org.yaxim.androidclient.service.SmackableImp.doConnect(SmackableImp.java:304)
  at org.yaxim.androidclient.service.SmackableImp$5.run(SmackableImp.java:391)
2019-03-16 10:42:36 +01:00
Florian Schmaus daab6039a1 Smack 4.3.3
-----BEGIN PGP SIGNATURE-----
 
 iQGTBAABCgB9FiEEl3UFnzoh3OFr5PuuIjmn6PWFIFIFAlyKV9tfFIAAAAAALgAo
 aXNzdWVyLWZwckBub3RhdGlvbnMub3BlbnBncC5maWZ0aGhvcnNlbWFuLm5ldDk3
 NzUwNTlGM0EyMURDRTE2QkU0RkJBRTIyMzlBN0U4RjU4NTIwNTIACgkQIjmn6PWF
 IFL4vQf/Qfg3VzNEnmk0+KjOtuvfAbhMfzE92gfo15vE0PPEIe9VA0Pzkvqhva4k
 Efw7BhD2zx8hWvo0d5FfkdII89hSYnOCiSmhiX1Ln9q/gUqFW0TDAKpsMfAl7jAK
 Fap8M7uUStP9T6fF/gq01djYCYoWA/4v1lGKv4J4b9gWCqzGIF2sK0M7of7VERnr
 pEXJSUM228rk6EYjVmX/9Ujo1Y+xaNMFFZZSbKYrirAqjP540v0OPBCRQyB8qFaI
 NTcNJ+qESe6Q80mw5V+y/kD6kX0LERSDB+pigzOOOlmfMtD1uADR84tAynuTKNAU
 7/5K/YSQxceRY6RTgD17Al5lHIH5nw==
 =ToR+
 -----END PGP SIGNATURE-----

Merge tag '4.3.3'

Smack 4.3.3
2019-03-14 18:23:33 +01:00
Georg Lukas e5bbd19ef1 StanzaDroppedListener for XEP-0198 resumption failures
If a stream resume fails, smack will re-send all queued stanzas after a
reconnect. However, it does not make sense to re-send them:

* IQs / IQ responses have probably timed out
* MUC messages and PMs will be rejected as you haven't rejoined yet
* regular messages should be amended with a <delay> element

This patch adds a StanzaDroppedListener interface to the
XMPPTCPConnection. If at least one StanzaDroppedListener is provided,
all queued messages will be drained into the StanzaDroppedListener(s).
Otherwise, the original behavior of attempting to transmit them will be
followed.

Discussion: https://discourse.igniterealtime.org/t/xep-0198-resume-failure-reconnect-resending-of-muc-messages/83510/3

Signed-off-by: Georg Lukas <georg@op-co.de>
2019-03-10 21:24:37 +01:00
Florian Schmaus 569f7417a8 Add AuthenticatedConnectionInitiallyEstablished timestamp 2019-03-10 21:24:37 +01:00
Florian Schmaus c4289b2c18 Add AbstractXMPPConnection.initState()
and init/reset the sychronization points there.

This method is called right at the beginning of connect() and at the
end of shutdown().
2019-03-10 21:24:37 +01:00