diff --git a/kotlin-mbedtls-netty/src/main/kotlin/org/opencoap/ssl/netty/DtlsClientHandshakeChannelHandler.kt b/kotlin-mbedtls-netty/src/main/kotlin/org/opencoap/ssl/netty/DtlsClientHandshakeChannelHandler.kt index 8008e3e..021def0 100644 --- a/kotlin-mbedtls-netty/src/main/kotlin/org/opencoap/ssl/netty/DtlsClientHandshakeChannelHandler.kt +++ b/kotlin-mbedtls-netty/src/main/kotlin/org/opencoap/ssl/netty/DtlsClientHandshakeChannelHandler.kt @@ -21,6 +21,7 @@ import io.netty.channel.ChannelHandlerContext import io.netty.channel.ChannelPromise import io.netty.channel.socket.DatagramPacket import io.netty.util.concurrent.ScheduledFuture +import org.opencoap.ssl.SslConfig import org.opencoap.ssl.SslException import org.opencoap.ssl.SslHandshakeContext import org.opencoap.ssl.SslSession @@ -33,8 +34,7 @@ import java.nio.channels.ClosedChannelException import java.util.concurrent.TimeUnit class DtlsClientHandshakeChannelHandler( - private val sslHandshakeContext: SslHandshakeContext, - private val peerAddress: InetSocketAddress, + sslConfig: SslConfig, private val sessionWriter: SessionWriter ) : ChannelDuplexHandler() { private val logger = LoggerFactory.getLogger(javaClass) @@ -42,6 +42,9 @@ class DtlsClientHandshakeChannelHandler( private val outboundMessages: MutableList> = mutableListOf() private var scheduledRetransmission: ScheduledFuture<*>? = null + private val peerAddress: InetSocketAddress get() = ctx.channel().remoteAddress() as InetSocketAddress + private val sslHandshakeContext: SslHandshakeContext by lazy { sslConfig.newContext(peerAddress) } + private fun write(packet: ByteBuffer) { val dtlsPacket = DatagramPacket(packet.toByteBuf(), peerAddress) ctx.writeAndFlush(dtlsPacket) diff --git a/kotlin-mbedtls-netty/src/main/kotlin/org/opencoap/ssl/netty/NettyTransportAdapter.kt b/kotlin-mbedtls-netty/src/main/kotlin/org/opencoap/ssl/netty/NettyTransportAdapter.kt index 90f5873..6bea347 100644 --- a/kotlin-mbedtls-netty/src/main/kotlin/org/opencoap/ssl/netty/NettyTransportAdapter.kt +++ b/kotlin-mbedtls-netty/src/main/kotlin/org/opencoap/ssl/netty/NettyTransportAdapter.kt @@ -82,9 +82,9 @@ class NettyTransportAdapter( return Bootstrap() .group(group) .channel(NioDatagramChannel::class.java) - .handler(DtlsClientHandshakeChannelHandler(sslConfig.newContext(destinationAddress), destinationAddress, sessionWriter)) + .handler(DtlsClientHandshakeChannelHandler(sslConfig, sessionWriter)) .also(bootstrapConfig) - .bind(0) + .connect(destinationAddress) .sync() .channel() .let { NettyTransportAdapter(it as DatagramChannel, destinationAddress) }