public class DTLSConnector extends Object implements org.eclipse.californium.elements.Connector, RecordLayer
Connector using Datagram TLS (DTLS) as specified in
RFC 6347 for securing data
exchanged between networked clients and a server application.
Note: using IPv6 interfaces with multiple addresses including permanent and
temporary (with potentially several different prefixes) currently causes
issues on the server side. The outgoing traffic in response to incoming may
select a different source address than the incoming destination address. To
overcome this, please ensure that the 'any address' is not used on the server
side and a separate Connector is created for each address to receive incoming
traffic.| Modifier and Type | Class and Description |
|---|---|
protected class |
DTLSConnector.Worker
A worker thread for continuously doing repetitive tasks.
|
| Modifier and Type | Field and Description |
|---|---|
protected DtlsConnectorConfig |
config
all the configuration options for the DTLS connector
|
protected ConnectionIdGenerator |
connectionIdGenerator
Configure connection id generator.
|
protected DtlsHealth |
health |
protected int |
inboundDatagramBufferSize |
static String |
KEY_TLS_SERVER_HOST_NAME
The
EndpointContext key used to store the host name indicated by a
client in an SNI hello extension. |
protected ScheduledExecutorService |
timer
The timer daemon to schedule retransmissions.
|
DEFAULT_ETH_MTU, DEFAULT_IPV4_MTU, DEFAULT_IPV6_MTU, IPV4_HEADER_LENGTH, IPV6_HEADER_LENGTH, MAX_MTU| Modifier | Constructor and Description |
|---|---|
|
DTLSConnector(DtlsConnectorConfig configuration)
Creates a DTLS connector from a given configuration object using the
standard in-memory
ConnectionStore. |
protected |
DTLSConnector(DtlsConnectorConfig configuration,
ResumptionSupportingConnectionStore connectionStore)
Creates a DTLS connector for a given set of configuration options.
|
|
DTLSConnector(DtlsConnectorConfig configuration,
SessionCache sessionCache)
Creates a DTLS connector for a given set of configuration options.
|
| Modifier and Type | Method and Description |
|---|---|
void |
clearConnectionState()
Clears all connection state this connector maintains for peers.
|
void |
close(InetSocketAddress peerAddress)
Closes a connection with a given peer.
|
protected static ResumptionSupportingConnectionStore |
createConnectionStore(DtlsConnectorConfig configuration,
SessionCache sessionCache)
Create and initialize default connection store.
|
protected DtlsHealth |
createDefaultHealthHandler(DtlsConnectorConfig configuration)
Create default health handler.
|
void |
destroy()
Destroys the connector.
|
void |
dropReceivedRecord(Record record)
Report dropped record
|
void |
forceResumeAllSessions()
Marks all established sessions currently maintained by this connector to be resumed by means
of an abbreviated handshake the
next time a message is being sent to the corresponding peer using
send(RawData). |
void |
forceResumeSessionFor(InetSocketAddress peer)
Force connector to an abbreviated handshake.
|
InetSocketAddress |
getAddress()
Gets the address this connector is bound to.
|
int |
getMaxDatagramSize(boolean ipv6)
Gets the maximum size of a UDP datagram that can be sent to this
session's peer without IP fragmentation.
|
int |
getMaximumFragmentLength(InetSocketAddress peer)
Gets the maximum amount of unencrypted payload data that can be sent to a given
peer in a single DTLS record.
|
int |
getMaximumTransmissionUnit()
Deprecated.
use
getMaxDatagramSize(boolean) instead |
String |
getProtocol() |
DTLSSession |
getSessionByAddress(InetSocketAddress address)
Returns the
DTLSSession related to the given peer address. |
protected void |
init(InetSocketAddress bindAddress,
DatagramSocket socket,
Integer mtu)
Initialize socket ad start connector.
|
boolean |
isRunning()
Checks if this connector is running.
|
protected void |
onInitializeHandshaker(Handshaker handshaker)
Called after initialization of new create handshaker.
|
protected void |
processDatagram(DatagramPacket packet)
Deprecated.
|
protected void |
processDatagram(DatagramPacket packet,
InetSocketAddress router)
Process received datagram.
|
void |
processRecord(Record record,
Connection connection)
Process received record.
|
protected void |
receiveNextDatagramFromNetwork(DatagramPacket packet)
Receive the next datagram from network.
|
void |
send(org.eclipse.californium.elements.RawData message) |
void |
sendFlight(List<DatagramPacket> datagrams)
Sends a set of UDP datagrams containing DTLS records with handshake
messages to a peer.
|
protected void |
sendNextDatagramOverNetwork(DatagramPacket datagramPacket) |
protected void |
sendRecord(Record record) |
void |
setAlertHandler(AlertHandler handler)
Sets a handler to call back if an alert message is received from a peer.
|
void |
setEndpointContextMatcher(org.eclipse.californium.elements.EndpointContextMatcher endpointContextMatcher) |
void |
setExecutor(ExecutorService executor)
Sets the executor to be used for processing records.
|
void |
setRawDataReceiver(org.eclipse.californium.elements.RawDataChannel messageHandler) |
void |
start() |
protected void |
start(InetSocketAddress bindAddress)
Start connector.
|
Future<Void> |
startDropConnectionsForPrincipal(Principal principal)
Start to terminate connections related to the provided principals.
|
Future<Void> |
startForEach(org.eclipse.californium.elements.util.LeastRecentlyUsedCache.Predicate<Connection> handler)
Start applying provided handler to all connections.
|
Future<Void> |
startTerminateConnectionsForPrincipal(org.eclipse.californium.elements.util.LeastRecentlyUsedCache.Predicate<Principal> principalHandler)
Start to terminate connections applying the provided handler to the
principals of all connections.
|
Future<Void> |
startTerminateConnectionsForPrincipal(org.eclipse.californium.elements.util.LeastRecentlyUsedCache.Predicate<Principal> principalHandler,
boolean removeFromSessionCache)
Start to terminate connections applying the provided handler to the
principals of all connections.
|
void |
stop() |
String |
toString() |
public static final String KEY_TLS_SERVER_HOST_NAME
EndpointContext key used to store the host name indicated by a
client in an SNI hello extension.protected final DtlsConnectorConfig config
protected final DtlsHealth health
protected final ConnectionIdGenerator connectionIdGenerator
null, if connection id
should not be supported.protected int inboundDatagramBufferSize
protected ScheduledExecutorService timer
public DTLSConnector(DtlsConnectorConfig configuration)
ConnectionStore.configuration - the configuration optionsNullPointerException - if the configuration is nullpublic DTLSConnector(DtlsConnectorConfig configuration, SessionCache sessionCache)
configuration - The configuration options.sessionCache - An (optional) cache for DTLSSession objects
that can be used for persisting and/or sharing of session
state among multiple instances of DTLSConnector.
Whenever a handshake with a client is finished the negotiated
session is put to this cache. Similarly, whenever a client
wants to perform an abbreviated handshake based on an existing
session the connection store will try to retrieve the session
from this cache if it is not available from the connection
store's in-memory (first-level) cache.NullPointerException - if the configuration is null.protected DTLSConnector(DtlsConnectorConfig configuration, ResumptionSupportingConnectionStore connectionStore)
The connection store must use the same connection id generator as configured in the provided configuration. The current implementation synchronize on the connection store, therefore it is important not to use the connection store within a different synchronization scope.
configuration - The configuration options.connectionStore - The registry to use for managing connections to
peers.NullPointerException - if any of the parameters is null.IllegalArgumentException - if the connection store uses a different
cid generator than the configuration.protected static ResumptionSupportingConnectionStore createConnectionStore(DtlsConnectorConfig configuration, SessionCache sessionCache)
configuration - configuration for initializationsessionCache - An (optional) cache for DTLSSession objects
that can be used for persisting and/or sharing of session
state among multiple instances of DTLSConnector.
Whenever a handshake with a client is finished the negotiated
session is put to this cache. Similarly, whenever a client
wants to perform an abbreviated handshake based on an existing
session the connection store will try to retrieve the session
from this cache if it is not available from the connection
store's in-memory (first-level) cache.protected DtlsHealth createDefaultHealthHandler(DtlsConnectorConfig configuration)
configuration - configurationprotected void onInitializeHandshaker(Handshaker handshaker)
handshaker - new create handshakerpublic final void setExecutor(ExecutorService executor)
If this property is not set before invoking the start method, a new ExecutorService is created with a thread
pool of size.
This helps with performing multiple handshakes in parallel, in particular if the key exchange
requires a look up of identities, e.g. in a database or using a web service.
If this method is used to set an executor, the executor will not be shut down by the stop method.
executor - The executor.IllegalStateException - if a new executor is set and this connector is already running.public final void close(InetSocketAddress peerAddress)
peerAddress - the address of the peer to close the connection toIllegalStateException - if executor cache is exceeded.public final void start()
throws IOException
start in interface org.eclipse.californium.elements.ConnectorIOExceptionprotected void start(InetSocketAddress bindAddress) throws IOException
bindAddress - address to bind socket.IOException - I/O errorprotected void init(InetSocketAddress bindAddress, DatagramSocket socket, Integer mtu) throws IOException
bindAddress - address to bind socketsocket - socketmtu - mtu of socket, or null, if socket implementation
doesn't use a special mtu.IOException - I/O errorpublic final void forceResumeSessionFor(InetSocketAddress peer)
send(RawData).peer - the peer for which we will force to do an abbreviated handshakepublic final void forceResumeAllSessions()
send(RawData).
This method's execution time is proportional to the number of connections this connector maintains.
public final void clearConnectionState()
After invoking this method a new connection needs to be established with a peer using a full handshake in order to exchange messages with it again.
public void stop()
stop in interface org.eclipse.californium.elements.Connectorpublic void destroy()
This method invokes stop() and clears the ConnectionStore
used to manage connections to peers. Thus, contrary to the behavior specified
for Connector.destroy(), this connector can be re-started using the
start() method but subsequent invocations of the send(RawData)
method will trigger the establishment of a new connection to the corresponding peer.
destroy in interface org.eclipse.californium.elements.Connectorpublic Future<Void> startDropConnectionsForPrincipal(Principal principal)
SessionCache is used, it's not possible to remove a
cache entry, if no related connection is in the connection store.principal - principal, which connections are to terminatepublic Future<Void> startTerminateConnectionsForPrincipal(org.eclipse.californium.elements.util.LeastRecentlyUsedCache.Predicate<Principal> principalHandler)
SessionCache is used, it's not possible to remove a
cache entry, if no related connection is in the connection store. All
available connections will be removed from that session cache as well.principalHandler - handler to be called within the serial execution
of the related connection. If true is returned, the
related connection is terminated and the session is removed
from the session cache.startTerminateConnectionsForPrincipal(org.eclipse.californium.elements.util.LeastRecentlyUsedCache.Predicate,
boolean)public Future<Void> startTerminateConnectionsForPrincipal(org.eclipse.californium.elements.util.LeastRecentlyUsedCache.Predicate<Principal> principalHandler, boolean removeFromSessionCache)
SessionCache is used, it's not possible to remove a
cache entry, if no related connection is in the connection store.principalHandler - handler to be called within the serial execution
of the related connection. If true is returned, the
related connection is terminatedremoveFromSessionCache - true if the session of the
connection should be removed from the session cache,
false, otherwisestartTerminateConnectionsForPrincipal(org.eclipse.californium.elements.util.LeastRecentlyUsedCache.Predicate)public Future<Void> startForEach(org.eclipse.californium.elements.util.LeastRecentlyUsedCache.Predicate<Connection> handler)
handler - handler to be called within the serial execution of the
passed in connection. If true is returned, iterating
is stopped.protected void receiveNextDatagramFromNetwork(DatagramPacket packet) throws IOException
packet - datagram the be read from networkIOException - if anio- error occurredprocessDatagram(DatagramPacket)@Deprecated protected void processDatagram(DatagramPacket packet)
processDatagram(DatagramPacket, InetSocketAddress)packet - datagram filled with the received data and source address.protected void processDatagram(DatagramPacket packet, InetSocketAddress router)
packet - received messagerouter - router address, null, if no router is used.public void processRecord(Record record, Connection connection)
processRecord in interface RecordLayerrecord - received record.connection - connection to process record.public void send(org.eclipse.californium.elements.RawData message)
send in interface org.eclipse.californium.elements.Connectorpublic final DTLSSession getSessionByAddress(InetSocketAddress address)
DTLSSession related to the given peer address.address - the peer addressDTLSSession or null if no session found.public void dropReceivedRecord(Record record)
RecordLayerdropReceivedRecord in interface RecordLayerrecord - dropped recordpublic int getMaxDatagramSize(boolean ipv6)
RecordLayergetMaxDatagramSize in interface RecordLayeripv6 - true, IPv6 destination, false, IPv4
destinationpublic void sendFlight(List<DatagramPacket> datagrams) throws IOException
RecordLayerThe set is sent as a whole. In particular this means that all datagrams will be re-transmitted in case of a missing acknowledgement from the peer.
sendFlight in interface RecordLayerdatagrams - list of UDP datagrams containing DTLS records to send.IOException - if an io error occursprotected void sendRecord(Record record) throws IOException
IOExceptionprotected void sendNextDatagramOverNetwork(DatagramPacket datagramPacket) throws IOException
IOException@Deprecated public final int getMaximumTransmissionUnit()
getMaxDatagramSize(boolean) insteadApplications may use this property to determine the maximum length of application layer data that can be sent using this connector without requiring IP fragmentation.
The value returned will be 0 if this connector is not running or the network interface this connector is bound to does not provide an MTU value.
public final int getMaximumFragmentLength(InetSocketAddress peer)
The value of this property serves as an upper boundary for the DTLSPlaintext.length field defined in DTLS 1.2 spec, Section 4.3.1. This means that an application can assume that any message containing at most as many bytes as indicated by this method, will be delivered to the peer in a single unfragmented datagram.
The value returned by this method considers the current write state of the connection to the peer and any potential ciphertext expansion introduced by this cipher suite used to secure the connection. However, if no connection exists to the peer, the value returned is determined as follows:
maxFragmentLength = network interface's Maximum Transmission Unit
- IP header length (20 bytes IPv4, 120 IPv6)
- UDP header length (8 bytes)
- DTLS record header length (13 bytes)
- DTLS message header length (12 bytes)
peer - the address of the remote endpointpublic final InetSocketAddress getAddress()
getAddress in interface org.eclipse.californium.elements.Connectorpublic final boolean isRunning()
isRunning in interface RecordLayertrue if running.public void setRawDataReceiver(org.eclipse.californium.elements.RawDataChannel messageHandler)
setRawDataReceiver in interface org.eclipse.californium.elements.Connectorpublic void setEndpointContextMatcher(org.eclipse.californium.elements.EndpointContextMatcher endpointContextMatcher)
setEndpointContextMatcher in interface org.eclipse.californium.elements.Connectorpublic final void setAlertHandler(AlertHandler handler)
Setting a handler using this method is useful to be notified when a peer closes an existing connection, i.e. when the alert message has not been received during a handshake but after the connection has been established.
The handler can be set (and changed) at any time, either before the connector has been started or when the connector is already running.
Application code interested in being notified when a particular message cannot be sent,
e.g. due to a failing DTLS handshake that has been triggered as part of sending
the message, should instead register a
org.eclipse.californium.core.coap.MessageObserver on the message and
implement its onSendError method accordingly.
handler - The handler to notify.public String getProtocol()
getProtocol in interface org.eclipse.californium.elements.ConnectorCopyright © 2023 Eclipse Foundation. All rights reserved.