From cf4c382998d3eea4da4b029c29aa130c76ddf545 Mon Sep 17 00:00:00 2001 From: Florian Roks Date: Thu, 5 Oct 2023 07:21:49 +0200 Subject: [PATCH] improve registeredSourceAddress handling --- ...ltDoipEntityTcpConnectionMessageHandler.kt | 19 +++++++------------ src/main/kotlin/library/DoipTcpMessages.kt | 9 +++++++++ 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/main/kotlin/library/DefaultDoipEntityTcpConnectionMessageHandler.kt b/src/main/kotlin/library/DefaultDoipEntityTcpConnectionMessageHandler.kt index 8ca046f..e89d3f6 100644 --- a/src/main/kotlin/library/DefaultDoipEntityTcpConnectionMessageHandler.kt +++ b/src/main/kotlin/library/DefaultDoipEntityTcpConnectionMessageHandler.kt @@ -19,11 +19,6 @@ public open class DefaultDoipEntityTcpConnectionMessageHandler( ) : DoipTcpConnectionMessageHandler(maxPayloadLength) { private val logger: Logger = LoggerFactory.getLogger(DefaultDoipEntityTcpConnectionMessageHandler::class.java) - private var _registeredSourceAddress: Short? = null - - public fun getRegisteredSourceAddress(): Short? = - _registeredSourceAddress - override suspend fun handleTcpMessage(message: DoipTcpMessage, output: OutputStream) { runBlocking { MDC.put("ecu", doipEntity.name) @@ -58,11 +53,11 @@ public open class DefaultDoipEntityTcpConnectionMessageHandler( return } - if (_registeredSourceAddress == null) { - _registeredSourceAddress = message.sourceAddress + if (registeredSourceAddress == null) { + registeredSourceAddress = message.sourceAddress } - if (_registeredSourceAddress != message.sourceAddress) { + if (registeredSourceAddress != message.sourceAddress) { logger.error("Routing activation for ${message.sourceAddress} denied (Different source address already registered)") output.writeFully( DoipTcpRoutingActivationResponse( @@ -72,8 +67,8 @@ public open class DefaultDoipEntityTcpConnectionMessageHandler( ).asByteArray ) } else if ( - doipEntity.hasAlreadyActiveConnection(message.sourceAddress, this) && - doipEntity.ecus.all { it.config.additionalVam == null } + doipEntity.ecus.all { it.config.additionalVam == null } && + doipEntity.hasAlreadyActiveConnection(message.sourceAddress, this) ) { logger.error("Routing activation for ${message.sourceAddress} denied (Has already an active connection)") output.writeFully( @@ -102,7 +97,7 @@ public open class DefaultDoipEntityTcpConnectionMessageHandler( } override suspend fun handleTcpDiagMessage(message: DoipTcpDiagMessage, output: OutputStream) { - if (_registeredSourceAddress != message.sourceAddress) { + if (registeredSourceAddress != message.sourceAddress) { val reject = DoipTcpDiagMessageNegAck( message.targetAddress, message.sourceAddress, @@ -151,7 +146,7 @@ public interface DiagnosticMessageHandler { public fun DoipEntity.hasAlreadyActiveConnection(sourceAddress: Short, exclude: DoipTcpConnectionMessageHandler?): Boolean = this.connectionHandlers.any { - (it as DefaultDoipEntityTcpConnectionMessageHandler).getRegisteredSourceAddress() == sourceAddress + it.registeredSourceAddress == sourceAddress && it != exclude } diff --git a/src/main/kotlin/library/DoipTcpMessages.kt b/src/main/kotlin/library/DoipTcpMessages.kt index f1de173..e8727e8 100644 --- a/src/main/kotlin/library/DoipTcpMessages.kt +++ b/src/main/kotlin/library/DoipTcpMessages.kt @@ -12,6 +12,14 @@ public abstract class DoipTcpMessage : DoipMessage() public open class DoipTcpConnectionMessageHandler( public val maxPayloadLength: Int = Int.MAX_VALUE ) { + private var _registeredSourceAddress: Short? = null + + public var registeredSourceAddress: Short? + get() = _registeredSourceAddress + protected set(value) { + _registeredSourceAddress = value + } + public open suspend fun receiveTcpData(brc: ByteReadChannel): DoipTcpMessage { logger.traceIf { "# receiveTcpData" } @@ -122,6 +130,7 @@ public open class DoipTcpConnectionMessageHandler( output: OutputStream ) { logger.traceIf { "# handleTcpRoutingActivationRequest $message" } + registeredSourceAddress = message.sourceAddress } protected open suspend fun handleTcpRoutingActivationResponse(