powered by Jive Software

"The following addresses failed" on localhost

Greetings.

I am evaluating the Smack library for a XMPP-based project, and I am trying to log from my prototype into a local XMPP server. I had already tried prototypes in node.js and Python and didn’t meet any problem to reach the server with the localhost address. Here is my Smack-based implementation (only the relevant snippet):

XMPPTCPConnectionConfiguration config = XMPPTCPConnectionConfiguration.builder()
	    .setUsernameAndPassword(user, password)
	    .setXmppDomain(xmppDomain)
	    .setHost(hostname)
            .setSecurityMode(securityMode)
            .setPort(Integer.parseInt(portArg)
	    .build();
	
	AbstractXMPPConnection connection = new XMPPTCPConnection(config);

	if (connection == null) {
	    System.err.println("Failed to instantiate a connection");
	    System.exit(1);
	}
	
	connection.connect();
	System.out.println("Connected to " + hostname);
	connection.login();
	System.out.println("Logged as " + user);

When testing this code, I get the following error:

$ java org.mydomain.hellosmack.HelloSmack localhost

Smack version 4.3.4 (4.3.4-4.3 2019-05-27)

user: me@localhost

password: <hidden>

Exception in thread "main" org.jivesoftware.smack.SmackException$ConnectionException: *The following addresses failed*

at org.jivesoftware.smack.SmackException$ConnectionException.from(SmackException.java:292)

at org.jivesoftware.smack.tcp.XMPPTCPConnection.connectUsingConfiguration(XMPPTCPConnection.java:668)

at org.jivesoftware.smack.tcp.XMPPTCPConnection.connectInternal(XMPPTCPConnection.java:971)

at org.jivesoftware.smack.AbstractXMPPConnection.connect(AbstractXMPPConnection.java:435)

at org.mydomain.hellosmack.HelloSmack.main(HelloSmack.java:66)

Am I missing anything (either in the code or in the configuration)? While it is in my plans, I would like to avoid setting up a heavy environment (e.g. a domain name, a name server, a fixed IP address and a remote XMPP server) at this stage of the project, just for a local evaluation. Is it possible with Smack? Thanks.

What’s the value of ‘hostname’?

Exception in thread “main” org.jivesoftware.smack.SmackException$ConnectionException: The following addresses failed

It also looks like this message is truncated.

Thank you for your quick reply.
The ‘hostname’ variable contains “localhost”.
I confirm that the message that I get contains only:

Exception in thread “main” org.jivesoftware.smack.SmackException$ConnectionException: The following addresses failed
at org.jivesoftware.smack.SmackException$ConnectionException.from(SmackException.java:292)

and that no addresses appear after “failed”.

Here is my configuration of development: MacOSX 10.14.6 (Mojave), XMPP server: Prosody 0.10.2 (planning to test with OpenFire and ejabberd too later). Everything runs locally.

PS. I can connect when I give the actual hostname of my workstation, instead of simply “localhost”. That makes the prototype less portable (I also need to edit the local server’s configuration file accordingly). (once I am connected, I receive error messages related with security, but it’s unrelated with the current issue). Is it not possible to use “localhost” with Smack?

If I had to guess then I would assume that you may “added” Smack to your project by throwing in the jar files. Is that correct?

I use Maven, and I added Smack to my pom.xml file.

I included the following packages:
smack-java7
smack-tcp
smack-im
smack-extensions
smack-sasl-provided
smack-resolver-javax
smack-resolver-minidns

and also some smack dependencies:
org.jxmpp/jxmpp-jid
xpp3/xpp3

What is the right thing to do?

No, Maven is fine. But note that you don’t need to include transitive dependencies like jxmpp or xpp3. It is the job of the build system, like Maven, to resolve these afterall.
You also don’t need to depend explicitly on smack-sasl-provided. And you need to depend on smack-resolver-minidns only if you want to use MiniDNS.

Back to your problem: I am not sure why there is no output. At this point I would simply attach an debugger and place a breakpoint at populateHostAddresses() so see what is going on. I have some ideas what it could be, but it is hard to diagnose it remotely with the information at hand. Oh, and maybe try setHost("localhost.local") and see changes the behavior?

Dear Flow,

thank you for the useful hint. With localhost.local, the message is a bit more informative:

Exception in thread “main” org.jivesoftware.smack.SmackException$ConnectionException: The following addresses failed: ‘localhost.local:5222’ failed because: org.minidns.hla.ResolutionUnsuccessfulException: Asking for localhost.local. IN A yielded an error response NX_DOMAIN, ‘localhost.local:5222’ failed because: org.minidns.hla.ResolutionUnsuccessfulException: Asking for localhost.local. IN AAAA yielded an error response NX_DOMAIN
at org.jivesoftware.smack.SmackException$ConnectionException.from(SmackException.java:292)
at org.jivesoftware.smack.tcp.XMPPTCPConnection.connectUsingConfiguration(XMPPTCPConnection.java:668)
at org.jivesoftware.smack.tcp.XMPPTCPConnection.connectInternal(XMPPTCPConnection.java:971)
at org.jivesoftware.smack.AbstractXMPPConnection.connect(AbstractXMPPConnection.java:435)
at org.mydomain.hellosmack.HelloSmack.main(HelloSmack.java:168)

So it seems to be related with the minidns. At that stage, I don’t think I need this (at least not directly), and I didn’t include it initially. But I was led to add the minidns packages because of the following error when they are missing:

Exception in thread “main” java.lang.NoClassDefFoundError: org/minidns/dnsname/InvalidDnsNameException

at org.mydomain.hellosmack.HelloSmack.main(HelloSmack.java:153)

Caused by: java.lang.ClassNotFoundException: org.minidns.dnsname.InvalidDnsNameException

line 153 (see above):
XMPPTCPConnectionConfiguration config = XMPPTCPConnectionConfiguration.builder() …

When I include only minidns-core in my class path, it solves the NoClassDefFoundError but then the smack connect() fails with a NullPointerException:

Exception in thread “main” java.lang.NullPointerException
at org.jivesoftware.smack.AbstractXMPPConnection.populateHostAddresses(AbstractXMPPConnection.java:675)
at org.jivesoftware.smack.tcp.XMPPTCPConnection.connectUsingConfiguration(XMPPTCPConnection.java:604)
at org.jivesoftware.smack.tcp.XMPPTCPConnection.connectInternal(XMPPTCPConnection.java:971)
at org.jivesoftware.smack.AbstractXMPPConnection.connect(AbstractXMPPConnection.java:435)
at org.mydomain.hellosmack.HelloSmack.main(HelloSmack.java:168)

line 168 (see above): connection.connect()

Eventually, I was led to add all the minidns packages after a lot of tries and errors. It’s only when they are all in place that I manage to get a start of connection handshake (but not with localhost).

Obviously I didn’t follow the “right” path.

Hmm, it still feels like you are including Smack wrong. Could you share your whole code? I’d be able to look into the root cause why the ConnectinException does not hold any failed addresses if you explicitly specific the XMPP service host.

Thank you very much for your help and patience, I really appreciate it!

HelloSmack.tar.gz (10,0 Ko)

I rewrote my pom.xml from scratch, without unnecessary dependencies, and now it “almost” works. Now I think I can log in (with the security disabled), even with localhost, but I still get some weird errors:

  • with the security disabled:
20:23:16 User logged (0): me@localhost:5222/ad54aeff-96d9-4e10-9bb3-d235021071aa

20:23:16 SENT (0): <iq id='6t4b8-5' type='get'><query xmlns='jabber:iq:roster'></query></iq>

20:23:16 RECV (0): <iq type='result' id='6t4b8-5' to='me@localhost/ad54aeff-96d9-4e10-9bb3-d235021071aa'><query xmlns='jabber:iq:roster' ver='1'/></iq>

20:23:16 XMPPConnection authenticated (XMPPTCPConnection[me@localhost/ad54aeff-96d9-4e10-9bb3-d235021071aa] (0))

20:23:16 SENT (0): <presence id='6t4b8-7'><c xmlns='http://jabber.org/protocol/caps' hash='sha-1' node='http://www.igniterealtime.org/projects/smack' ver='NfJ3flI83zSdUDzCEICtbypursw='/></presence>

Logged as me

20:23:16 RECV (0): <presence id='6t4b8-7' from='me@localhost/ad54aeff-96d9-4e10-9bb3-d235021071aa'><c ver='NfJ3flI83zSdUDzCEICtbypursw=' hash='sha-1' xmlns='http://jabber.org/protocol/caps' node='http://www.igniterealtime.org/projects/smack'/></presence>

[ **WARNING** ]

**org.jxmpp.stringprep.XmppStringprepException** : **XmppStringprepException caused by 'me': org.jxmpp.stringprep.XmppStringprepException: Argument can't be the empty string**

**at** org.jxmpp.jid.impl.JidCreate.entityBareFrom ( **JidCreate.java:675** )

**at** org.mydomain.hellosmack.HelloSmack.main ( **HelloSmack.java:180** )
  • with the security enabled:
20:02:56 RECV (0): <?xml version='1.0'?><stream:stream id='73f3a21e-8033-4686-9c4f-2375dbb4569e' xml:lang='en' xmlns:stream='http://etherx.jabber.org/streams' from='localhost' xmlns='jabber:client' version='1.0'><stream:features><starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/><mechanisms xmlns='urn:ietf:params:xml:ns:xmpp-sasl'><mechanism>SCRAM-SHA-1</mechanism></mechanisms></stream:features>
20:02:56 SENT (0): <starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'></starttls>
20:02:56 RECV (0): <proceed xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>
[WARNING]
org.jivesoftware.smack.SmackException: javax.net.ssl.SSLHandshakeException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
    at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader.parsePackets (XMPPTCPConnection.java:1176)
    at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader.access$1000 (XMPPTCPConnection.java:1092)
    at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader$1.run (XMPPTCPConnection.java:1112)
    at java.lang.Thread.run (Thread.java:835)

The code is in hellosmack/src/main/java/org/mydomain/hellosmack/HelloSmack.java
(sorry it is quick and dirty, I am just learning the API for the moment. It’s a throwaway prototype. No function outside main is called for the moment).

If you can have a (very) quick look and let me know if I am doing obvious mistakes…

To build the code, just type: make (it will call maven)

If you are interested in launching the code:

./run_hellosmack.sh localhost
(on a Linux or OSX system. If you are on Windows, the script is a just a one-line call to maven, but with flags for the truststore in case of a SSL/TLS negotiation)

Thanks! (I will not bother you anymore after this!)

PS. I put .log files with the output that I obtain, with the security enabled or disabled.
With the security enabled, I suspect that it fails because the client can’t validate the server’s certificate, but I wouldn’t swear it (I added the server’s certificate in the client’s truststore).

You are trying to create an EntityBareJid from the String ‘me’, which does not represent a valid entity bare JID and thus fails. JIDs always have a domainpart, e.g. me@example.org

Your SSLContext is unable to establish a trust path to the certificate presented by the XMPP service.

You are welcome.

I am always happy about user feedback, in order to improve the API and make it more user firendly, so feel free to keep it coming. Please note that there is also a MUC smack@conference.igniterealtime.org where you can ask questions. I am usually around, and if I have time, I am glad to help (But I do not provide unpaid 24/7 support obviously ;)).

Thank you!!

I fixed the Jid issue and the connection works now, yay! The code still exits wrongly (I call disconnect() on the connection instance, but it’s apparently not sufficient), I have to figure out why, as well as the problem with the certificates (if I did understand, by trust path, you mean that the CA used to sign the certificate is not recognised by the client, right?), but at least I am not blocked anymore.

Best regards

[WARNING] thread Thread[Smack Scheduled Executor Service,5,org.mydomain.hellosmack.HelloSmack] was interrupted but is still alive after waiting at least 14999msecs
[WARNING] thread Thread[Smack Scheduled Executor Service,5,org.mydomain.hellosmack.HelloSmack] will linger despite being asked to die via interruption
[WARNING] thread Thread[Smack Cached Executor,5,org.mydomain.hellosmack.HelloSmack] will linger despite being asked to die via interruption
[WARNING] thread Thread[Smack Cached Executor,5,org.mydomain.hellosmack.HelloSmack] will linger despite being asked to die via interruption
[WARNING] thread Thread[Smack Cached Executor,5,org.mydomain.hellosmack.HelloSmack] will linger despite being asked to die via interruption
[WARNING] NOTE: 4 thread(s) did not finish despite being asked to  via interruption. This is not a problem with exec:java, it is a problem with the running code. Although not serious, it should be remedied.
[WARNING] Couldn't destroy threadgroup org.codehaus.mojo.exec.ExecJavaMojo$IsolatedThreadGroup[name=org.mydomain.hellosmack.HelloSmack,maxpri=10]
java.lang.IllegalThreadStateException

Well, it should be.

You may want to look into

Thank you, very interesting! I will look at it.

I solved my problem with certificates anyway, it was just some JVM options passed wrongly through Maven. Now everything works but the issue with disconnection. I will look at it another day.

Thank you again for your help and time!

All the best

Just for your information,

the issue with IllegalThreadStateException could be fixed by adding the cleanupDaemonThreads element in the Maven pom.xml below.

<plugin>
          <groupId>org.codehaus.mojo</groupId>
          <artifactId>exec-maven-plugin</artifactId>
          <version>1.6.0</version>
          <configuration>
            <mainClass>org.mydomain.hellosmack.HelloSmack</mainClass>
            <cleanupDaemonThreads>false</cleanupDaemonThreads>
          </configuration>
</plugin>

Bye!