Skip to content

Commit

Permalink
Add coaps+tcp support based on java-coap to server-demo and client-demo.
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernard31 committed Apr 23, 2024
1 parent 8d208a0 commit 99b94e7
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
import org.eclipse.leshan.core.request.ContentFormat;
import org.eclipse.leshan.core.response.BootstrapWriteResponse;
import org.eclipse.leshan.transport.javacoap.client.coaptcp.endpoint.JavaCoapTcpClientEndpointsProvider;
import org.eclipse.leshan.transport.javacoap.client.coaptcp.endpoint.JavaCoapsTcpClientEndpointsProvider;
import org.eclipse.leshan.transport.javacoap.client.endpoint.JavaCoapClientEndpointsProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -319,6 +320,7 @@ protected DtlsConnectorConfig.Builder createRootDtlsConnectorConfigBuilder(
endpointsProvider.add(new JavaCoapClientEndpointsProvider());
}
endpointsProvider.add(new JavaCoapTcpClientEndpointsProvider());
endpointsProvider.add(new JavaCoapsTcpClientEndpointsProvider());

// Create client
LeshanClientBuilder builder = new LeshanClientBuilder(cli.main.endpoint);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ public void run() {
// check URI scheme is supported
List<String> supportedUnsecuredProtocol = Arrays.asList(Protocol.COAP, Protocol.COAP_TCP) //
.stream().map(Protocol::getUriScheme).collect(Collectors.toList());
List<String> supportedTlsBasedProtocol = Arrays.asList(Protocol.COAPS) //
List<String> supportedTlsBasedProtocol = Arrays.asList(Protocol.COAPS, Protocol.COAPS_TCP) //
.stream().map(Protocol::getUriScheme).collect(Collectors.toList());
List<String> allSupportedProtocol = Stream
.concat(supportedUnsecuredProtocol.stream(), supportedTlsBasedProtocol.stream())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
import org.eclipse.leshan.server.security.EditableSecurityStore;
import org.eclipse.leshan.server.security.FileSecurityStore;
import org.eclipse.leshan.transport.javacoap.server.coaptcp.endpoint.JavaCoapTcpServerEndpointsProvider;
import org.eclipse.leshan.transport.javacoap.server.coaptcp.endpoint.JavaCoapsTcpServerEndpointsProvider;
import org.eclipse.leshan.transport.javacoap.server.endpoint.JavaCoapServerEndpointsProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -265,9 +266,16 @@ public static LeshanServer createLeshanServer(LeshanServerDemoCLI cli) throws Ex
JavaCoapTcpServerEndpointsProvider javacoapTcpEndpointsProvider = new JavaCoapTcpServerEndpointsProvider(
coapTcpAddr);

// Create CoAP over TLS endpoint based on java-coap
int coapsTcpPort = cli.main.jTlsLocalPort;
InetSocketAddress coapsTcpAddr = cli.main.jTlsLocalAddress == null ? new InetSocketAddress(coapsTcpPort)
: new InetSocketAddress(cli.main.jTlsLocalAddress, coapTcpPort);
JavaCoapsTcpServerEndpointsProvider javacoapsTcpEndpointsProvider = new JavaCoapsTcpServerEndpointsProvider(
coapsTcpAddr);

// Create LWM2M server
builder.setEndpointsProviders(endpointsBuilder.build(), javacoapEndpointsProvider,
javacoapTcpEndpointsProvider);
builder.setEndpointsProviders(endpointsBuilder.build(), javacoapEndpointsProvider, javacoapTcpEndpointsProvider,
javacoapsTcpEndpointsProvider);
return builder.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,19 @@ public static class ServerGeneralSection extends GeneralSection {
converter = PortConverter.class)
public Integer jTcpLocalPort = 5683;

@Option(names = { "-tsh", "--java-coaps-tcp-host" },
description = { //
"Set the local CoAP over TLS address of endpoint based on java-coap library.", //
"Default: any local address." })
public String jTlsLocalAddress;

@Option(names = { "-tsp", "--java-coaps-tcp-port" },
description = { //
"Set the local CoAP over TLS port of endpoint based on java-coap library.", //
"Default: ${DEFAULT-VALUE}" },
converter = PortConverter.class)
public Integer jTlsLocalPort = 5684;

@Option(names = { "-r", "--redis" },
description = { //
"Use redis to store registration and securityInfo.", //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*******************************************************************************/
package org.eclipse.leshan.transport.javacoap.identity;

import java.net.InetSocketAddress;
import java.security.Principal;

import javax.security.auth.x500.X500Principal;
Expand All @@ -24,25 +25,24 @@
import org.eclipse.leshan.core.peer.X509Identity;
import org.eclipse.leshan.core.security.certificate.util.X509CertUtil;

import com.mbed.coap.packet.CoapRequest;
import com.mbed.coap.transport.TransportContext;

public class DefaultTlsIdentityHandler extends DefaultCoapIdentityHandler {

@Override
protected LwM2mPeer getIdentity(CoapRequest receivedRequest) {
Principal principal = receivedRequest.getTransContext().get(TlsTransportContextKeys.PRINCIPAL);
protected LwM2mPeer getIdentity(InetSocketAddress address, TransportContext context) {
Principal principal = context.get(TlsTransportContextKeys.PRINCIPAL);
if (principal != null) {
if (principal instanceof X500Principal) {
// Extract common name
String x509CommonName = X509CertUtil.extractCN(principal.getName());
return new IpPeer(receivedRequest.getPeerAddress(), new X509Identity(x509CommonName));
return new IpPeer(address, new X509Identity(x509CommonName));
}
throw new IllegalStateException(
String.format("Unable to extract sender identity : unexpected type of Principal %s [%s]",
principal.getClass(), principal.toString()));
} else {
return new IpPeer(receivedRequest.getPeerAddress());
return new IpPeer(address);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,10 @@ public void channelActive(ChannelHandlerContext ctx) throws Exception {
// if (tansportContext == null)
// throw new IllegalStateException("transport context should not be null");

if (listener != null)
if (listener != null
// Not clear what is the consequence but it seems that remote addresse can be null :
// https://github.com/netty/netty/issues/8501
&& ctx.channel().remoteAddress() != null)
listener.onConnected((InetSocketAddress) ctx.channel().remoteAddress());

super.channelActive(ctx);
Expand All @@ -187,7 +190,10 @@ public void channelInactive(ChannelHandlerContext ctx) throws Exception {
// if (tansportContext == null)
// throw new IllegalStateException("transport context should not be null");

if (listener != null)
if (listener != null
// Not clear what is the consequence but it seems that remote addresse can be null :
// https://github.com/netty/netty/issues/8501
&& ctx.channel().remoteAddress() != null)
listener.onDisconnected((InetSocketAddress) ctx.channel().remoteAddress());

super.channelInactive(ctx);
Expand Down

0 comments on commit 99b94e7

Please sign in to comment.