use of org.eclipse.californium.core.network.config.NetworkConfig in project leshan by eclipse.
the class SecureIntegrationTestHelper method createRPKClient.
// TODO implement RPK support for client
public void createRPKClient() {
ObjectsInitializer initializer = new ObjectsInitializer();
initializer.setInstancesForObject(LwM2mId.SECURITY, Security.rpk("coaps://" + server.getSecuredAddress().getHostString() + ":" + server.getSecuredAddress().getPort(), 12345, clientPublicKey.getEncoded(), clientPrivateKey.getEncoded(), serverPublicKey.getEncoded()));
initializer.setInstancesForObject(LwM2mId.SERVER, new Server(12345, LIFETIME, BindingMode.U, false));
initializer.setInstancesForObject(LwM2mId.DEVICE, new Device("Eclipse Leshan", MODEL_NUMBER, "12345", "U"));
List<LwM2mObjectEnabler> objects = initializer.createMandatory();
objects.add(initializer.create(2));
InetSocketAddress clientAddress = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
DtlsConnectorConfig.Builder config = new DtlsConnectorConfig.Builder().setAddress(clientAddress);
// TODO we should read the config from the security object
// TODO no way to provide a dynamic config with the current scandium API
config.setIdentity(clientPrivateKey, clientPublicKey);
CoapServer coapServer = new CoapServer();
CoapEndpoint.CoapEndpointBuilder coapBuilder = new CoapEndpoint.CoapEndpointBuilder();
coapBuilder.setConnector(new DTLSConnector(config.build()));
coapBuilder.setNetworkConfig(new NetworkConfig());
coapServer.addEndpoint(coapBuilder.build());
LeshanClientBuilder builder = new LeshanClientBuilder(getCurrentEndpoint());
builder.setLocalAddress(clientAddress.getHostString(), clientAddress.getPort());
builder.setObjects(objects);
client = builder.build();
}
use of org.eclipse.californium.core.network.config.NetworkConfig in project hono by eclipse.
the class ConfigBasedCoapEndpointFactory method newDefaultNetworkConfig.
private NetworkConfig newDefaultNetworkConfig() {
final NetworkConfig networkConfig = new NetworkConfig();
networkConfig.setInt(Keys.PROTOCOL_STAGE_THREAD_COUNT, config.getCoapThreads());
networkConfig.setInt(Keys.NETWORK_STAGE_RECEIVER_THREAD_COUNT, config.getConnectorThreads());
networkConfig.setInt(Keys.NETWORK_STAGE_SENDER_THREAD_COUNT, config.getConnectorThreads());
networkConfig.setInt(Keys.MAX_RESOURCE_BODY_SIZE, config.getMaxPayloadSize());
networkConfig.setInt(Keys.EXCHANGE_LIFETIME, config.getExchangeLifetime());
networkConfig.setBoolean(Keys.USE_MESSAGE_OFFLOADING, config.isMessageOffloadingEnabled());
networkConfig.setString(Keys.DEDUPLICATOR, Keys.DEDUPLICATOR_PEERS_MARK_AND_SWEEP);
final int maxConnections = config.getMaxConnections();
if (maxConnections == 0) {
final MemoryBasedConnectionLimitStrategy limits = new MemoryBasedConnectionLimitStrategy(MINIMAL_MEMORY, MEMORY_PER_CONNECTION);
networkConfig.setInt(Keys.MAX_ACTIVE_PEERS, limits.getRecommendedLimit());
} else {
networkConfig.setInt(Keys.MAX_ACTIVE_PEERS, maxConnections);
}
return networkConfig;
}
use of org.eclipse.californium.core.network.config.NetworkConfig in project hono by eclipse.
the class ConfigBasedCoapEndpointFactory method createSecureEndpoint.
private Future<Endpoint> createSecureEndpoint(final int port, final NetworkConfig networkConfig) {
if (deviceResolver == null) {
return Future.failedFuture(new IllegalStateException("infoSupplier property must be set for secure endpoint"));
}
if (pskStore == null) {
return Future.failedFuture(new IllegalStateException("pskStore property must be set for secure endpoint"));
}
LOG.info("creating secure endpoint");
final DtlsConnectorConfig.Builder dtlsConfig = new DtlsConnectorConfig.Builder();
// prevent session resumption
dtlsConfig.setNoServerSessionId(true);
dtlsConfig.setServerOnly(true);
dtlsConfig.setRecommendedCipherSuitesOnly(true);
dtlsConfig.setClientAuthenticationRequired(true);
dtlsConfig.setAddress(new InetSocketAddress(config.getBindAddress(), port));
dtlsConfig.setApplicationLevelInfoSupplier(deviceResolver);
dtlsConfig.setAdvancedPskStore(pskStore);
dtlsConfig.setRetransmissionTimeout(config.getDtlsRetransmissionTimeout());
dtlsConfig.setMaxConnections(networkConfig.getInt(Keys.MAX_ACTIVE_PEERS));
dtlsConfig.setSniEnabled(true);
addIdentity(dtlsConfig);
try {
final DtlsConnectorConfig dtlsConnectorConfig = dtlsConfig.build();
if (LOG.isInfoEnabled()) {
final String ciphers = dtlsConnectorConfig.getSupportedCipherSuites().stream().map(cipher -> cipher.name()).collect(Collectors.joining(", "));
LOG.info("creating secure endpoint supporting ciphers: {}", ciphers);
}
final DTLSConnector dtlsConnector = new DTLSConnector(dtlsConnectorConfig);
final CoapEndpoint.Builder builder = new CoapEndpoint.Builder();
builder.setNetworkConfig(networkConfig);
builder.setConnector(dtlsConnector);
builder.setObservationStore(observationStore);
return Future.succeededFuture(builder.build());
} catch (final IllegalStateException ex) {
LOG.warn("failed to create secure endpoint", ex);
return Future.failedFuture(ex);
}
}
use of org.eclipse.californium.core.network.config.NetworkConfig in project hono by eclipse.
the class ConfigBasedCoapEndpointFactory method loadNetworkConfig.
/**
* Loads Californium configuration properties from a file.
*
* @param fileName The absolute path to the properties file.
* @param networkConfig The configuration to apply the properties to.
* @return The updated configuration.
*/
protected Future<NetworkConfig> loadNetworkConfig(final String fileName, final NetworkConfig networkConfig) {
final Promise<NetworkConfig> result = Promise.promise();
if (!Strings.isNullOrEmpty(fileName)) {
vertx.fileSystem().readFile(fileName, readAttempt -> {
if (readAttempt.succeeded()) {
try (InputStream is = new ByteArrayInputStream(readAttempt.result().getBytes())) {
networkConfig.load(is);
result.complete(networkConfig);
} catch (final IOException e) {
LOG.warn("error malformed NetworkConfig properties [{}]", fileName);
result.fail(e);
}
} else {
LOG.warn("error reading NetworkConfig file [{}]", fileName, readAttempt.cause());
result.fail(readAttempt.cause());
}
});
} else {
result.complete(networkConfig);
}
return result.future();
}
Aggregations