I try to use the ping function over the CoAP.COAP_TCP_URI_SCHEME (coap+tcp) and CoAP.COAP_SECURE_TCP_URI_SCHEME (coaps+tcp) on the coap://californium.eclipse.org server. When I call the coapClient.ping() the function waits infinite. In the tcp dump the client sends an NON message. I think this is the wrong behavior. I also face this problem with the SECURE sheme.
I don't encounter this problem with tje CoAP.COAP_URI_SCHEME. With this sheme the client sends the correct CON with Code 0 Empty Message and the sever replies with the RST Message.
Is this not supported. bug?
Version:
'org.eclipse.californium:californium-core:2.0.0-M5'
'org.eclipse.californium:element-connector:2.0.0-M5'
import org.eclipse.californium.core.CoapClient;
import org.eclipse.californium.core.coap.CoAP;
import org.eclipse.californium.core.network.CoapEndpoint;
import org.eclipse.californium.core.network.Endpoint;
import org.eclipse.californium.core.network.config.NetworkConfig;
import org.eclipse.californium.elements.tcp.TlsClientConnector;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
public class TlsPingTest {
public static void main(String[] args) {
String host = "californium.eclipse.org";
CoapClient coapClient = buildClient(host, CoAP.COAP_TCP_URI_SCHEME, 5683);
boolean ping = coapClient.ping();
System.out.println("ping : " + ping);
CoapClient sslCoapClient = buildClient(host, CoAP.COAP_SECURE_TCP_URI_SCHEME, 5684);
boolean sslPing = sslCoapClient.ping();
System.out.println("sslping: " + sslPing);
}
static CoapClient buildClient(String host, String sheme, int port) {
CoapClient coapClient = new CoapClient.Builder(host, port)
.scheme(sheme)
.path(new String[0])
.query(new String[0])
.create();
if (sheme.equals(CoAP.COAP_SECURE_TCP_URI_SCHEME)) {
addSecureEndpoint(coapClient);
}
return coapClient;
}
private static void addSecureEndpoint(CoapClient coapClient) {
Endpoint secureEndpoint = buildSecureEndpoint(
true
);
coapClient.setEndpoint(secureEndpoint);
}
private static final String DEFAULT_TLS_PROTOCOL = "TLSv1.2";
public static Endpoint buildSecureEndpoint(boolean allowUntrusted) {
Endpoint endpoint = null;
try {
SSLContext sslContext = SSLContext.getInstance(DEFAULT_TLS_PROTOCOL);
if (allowUntrusted) {
TrustManager[] trustManagers = getInsecureTrustManagers();
sslContext.init(null, trustManagers, new SecureRandom());
} else {
sslContext.init(null, null, new SecureRandom());
}
NetworkConfig config = NetworkConfig.getStandard();
TlsClientConnector connector = new TlsClientConnector(
sslContext,
config.getInt(NetworkConfig.Keys.TCP_WORKER_THREADS),
config.getInt(NetworkConfig.Keys.TCP_CONNECT_TIMEOUT),
config.getInt(NetworkConfig.Keys.TCP_CONNECTION_IDLE_TIMEOUT));
endpoint = new CoapEndpoint(connector, config);
} catch (NoSuchAlgorithmException | KeyManagementException ex) {
//lululul
endpoint = null;
throw new IllegalArgumentException();
}
return endpoint;
}
private static TrustManager[] getInsecureTrustManagers() {
TrustManager[] trustAllCerts = new TrustManager[] {
new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
public void checkClientTrusted(X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(X509Certificate[] certs, String authType) {
}
}
};
return trustAllCerts;
}
}