Search in sources :

Example 6 with CoapEndpoint

use of org.eclipse.californium.core.network.CoapEndpoint in project leshan by eclipse.

the class SecurityTest method dont_sent_request_if_identity_change.

@Test
public void dont_sent_request_if_identity_change() throws NonUniqueSecurityInfoException, InterruptedException, IOException {
    // Create PSK server & start it
    // default server support PSK
    helper.createServer();
    helper.server.start();
    // Create PSK Client
    helper.createPSKClient();
    // Add client credentials to the server
    helper.getSecurityStore().add(SecurityInfo.newPreSharedKeyInfo(helper.getCurrentEndpoint(), GOOD_PSK_ID, GOOD_PSK_KEY));
    // Check client is not registered
    helper.assertClientNotRegisterered();
    // Start it and wait for registration
    helper.client.start();
    helper.waitForRegistration(1);
    // Check client is well registered
    helper.assertClientRegisterered();
    // Ensure we can send a read request
    helper.server.send(helper.getCurrentRegistration(), new ReadRequest(3, 0, 1));
    // Pause the client
    // helper.client.stop(false);
    // Add new credential to the server
    helper.getSecurityStore().add(SecurityInfo.newPreSharedKeyInfo(GOOD_ENDPOINT, "anotherPSK", GOOD_PSK_KEY));
    // Get connector
    Endpoint endpoint = helper.client.getCoapServer().getEndpoint(helper.client.getSecuredAddress());
    DTLSConnector connector = (DTLSConnector) ((CoapEndpoint) endpoint).getConnector();
    // Clear DTLS session to force new handshake
    connector.clearConnectionState();
    // Change PSK idea
    helper.setNewPsk(helper.client, "anotherPSK");
    // restart connector
    connector.start();
    // send and empty message to force a new handshake with new credentials
    SimpleMessageCallback callback = new SimpleMessageCallback();
    connector.send(RawData.outbound(new byte[0], new AddressEndpointContext(helper.server.getSecuredAddress()), callback, false));
    // Wait until new handshake DTLS is done
    EndpointContext endpointContext = callback.getEndpointContext(1000);
    assertEquals(endpointContext.getPeerIdentity().getName(), "anotherPSK");
    // Try to send a read request this should failed with an SendFailedException.
    try {
        helper.server.send(helper.getCurrentRegistration(), new ReadRequest(3, 0, 1), 1000);
        fail("send must failed");
    } catch (SendFailedException e) {
        assertTrue("must be caused by an EndpointMismatchException", e.getCause() instanceof EndpointMismatchException);
    } finally {
        connector.stop();
        helper.client.destroy(false);
        helper.client = null;
    }
}
Also used : SendFailedException(org.eclipse.leshan.core.request.exception.SendFailedException) EndpointMismatchException(org.eclipse.californium.elements.EndpointMismatchException) CoapEndpoint(org.eclipse.californium.core.network.CoapEndpoint) Endpoint(org.eclipse.californium.core.network.Endpoint) AddressEndpointContext(org.eclipse.californium.elements.AddressEndpointContext) EndpointContext(org.eclipse.californium.elements.EndpointContext) AddressEndpointContext(org.eclipse.californium.elements.AddressEndpointContext) SimpleMessageCallback(org.eclipse.californium.elements.util.SimpleMessageCallback) DTLSConnector(org.eclipse.californium.scandium.DTLSConnector) ReadRequest(org.eclipse.leshan.core.request.ReadRequest) Test(org.junit.Test)

Example 7 with CoapEndpoint

use of org.eclipse.californium.core.network.CoapEndpoint in project thingsboard by thingsboard.

the class CoapTransportService method init.

@PostConstruct
public void init() throws UnknownHostException {
    log.info("Starting CoAP transport...");
    log.info("Lookup CoAP transport adaptor {}", adaptorName);
    this.adaptor = (CoapTransportAdaptor) appContext.getBean(adaptorName);
    log.info("Starting CoAP transport server");
    this.server = new CoapServer();
    createResources();
    InetAddress addr = InetAddress.getByName(host);
    InetSocketAddress sockAddr = new InetSocketAddress(addr, port);
    server.addEndpoint(new CoapEndpoint(sockAddr));
    server.start();
    log.info("CoAP transport started!");
}
Also used : InetSocketAddress(java.net.InetSocketAddress) CoapServer(org.eclipse.californium.core.CoapServer) InetAddress(java.net.InetAddress) CoapEndpoint(org.eclipse.californium.core.network.CoapEndpoint) PostConstruct(javax.annotation.PostConstruct)

Aggregations

CoapEndpoint (org.eclipse.californium.core.network.CoapEndpoint)7 InetSocketAddress (java.net.InetSocketAddress)6 DTLSConnector (org.eclipse.californium.scandium.DTLSConnector)5 DtlsConnectorConfig (org.eclipse.californium.scandium.config.DtlsConnectorConfig)4 AddressEndpointContext (org.eclipse.californium.elements.AddressEndpointContext)2 Builder (org.eclipse.californium.scandium.config.DtlsConnectorConfig.Builder)2 Lwm2mEndpointContextMatcher (org.eclipse.leshan.core.californium.Lwm2mEndpointContextMatcher)2 ReadRequest (org.eclipse.leshan.core.request.ReadRequest)2 Test (org.junit.Test)2 JsonObject (com.google.gson.JsonObject)1 JsonParseException (com.google.gson.JsonParseException)1 JsonParser (com.google.gson.JsonParser)1 InetAddress (java.net.InetAddress)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 PostConstruct (javax.annotation.PostConstruct)1 CoapClient (org.eclipse.californium.core.CoapClient)1 CoapResponse (org.eclipse.californium.core.CoapResponse)1 CoapServer (org.eclipse.californium.core.CoapServer)1 Request (org.eclipse.californium.core.coap.Request)1