Skip to content

Commit

Permalink
add option to bind to inaddr_any for broadcasts
Browse files Browse the repository at this point in the history
  • Loading branch information
froks committed Aug 24, 2023
1 parent 847090b commit d8272a2
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ plugins {
apply<NexusReleasePlugin>()

group = "io.github.doip-sim-ecu"
version = "0.9.16"
version = "0.9.17"

repositories {
gradlePluginPortal()
Expand Down
7 changes: 7 additions & 0 deletions src/main/kotlin/SimGateway.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ public open class GatewayData(name: String) : RequestsData(name) {
*/
public var localAddress: String = "0.0.0.0"

/**
* Should udp be bound additionally on any?
* There's an issue when binding it to an network interface of not receiving 255.255.255.255 broadcasts
*/
public var bindOnAnyForUdpAdditional: Boolean = true

/**
* Network port this gateway should bind on (default: 13400)
*/
Expand Down Expand Up @@ -102,6 +108,7 @@ private fun GatewayData.toGatewayConfig(): DoipEntityConfig {
gid = this.gid,
eid = this.eid,
localAddress = this.localAddress,
bindOnAnyForUdpAdditional = this.bindOnAnyForUdpAdditional,
localPort = this.localPort,
logicalAddress = this.logicalAddress,
broadcastEnabled = this.broadcastEnable,
Expand Down
28 changes: 28 additions & 0 deletions src/main/kotlin/library/DoipEntity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import org.slf4j.LoggerFactory
import org.slf4j.MDC
import java.io.File
import java.io.OutputStream
import java.net.Inet4Address
import java.net.InetAddress
import java.net.SocketException
import java.nio.file.Paths
Expand Down Expand Up @@ -57,6 +58,7 @@ public open class DoipEntityConfig(
public val vin: VIN,
public val maxDataSize: Int = Int.MAX_VALUE,
public val localAddress: String = "0.0.0.0",
public val bindOnAnyForUdpAdditional: Boolean = true,
public val localPort: Int = 13400,
public val broadcastEnabled: Boolean = true,
public val broadcastAddress: String = "255.255.255.255",
Expand Down Expand Up @@ -321,6 +323,32 @@ public open class DoipEntity(
logger.info("Listening on udp: ${serverSocket.localAddress}")
startVamTimer(serverSocket)
val udpMessageHandler = createDoipUdpMessageHandler()

if (config.localAddress != "0.0.0.0") {
logger.info("Also listening on udp 0.0.0.0 for broadcasts")
val localAddress = InetSocketAddress("0.0.0.0", 13400)
val anyServerSocket =
aSocket(ActorSelectorManager(Dispatchers.IO))
.udp()
.bind(localAddress = localAddress) {
broadcast = true
reuseAddress = true
}
thread(start = true, isDaemon = true) {
runBlocking {
while (!anyServerSocket.isClosed) {
val datagram = anyServerSocket.receive()
if (datagram.address is InetSocketAddress) {
if (datagram.address == localAddress) {
continue
}
}
handleUdpMessage(udpMessageHandler, datagram, anyServerSocket)
}
}
}
}

while (!serverSocket.isClosed) {
val datagram = serverSocket.receive()
handleUdpMessage(udpMessageHandler, datagram, serverSocket)
Expand Down

0 comments on commit d8272a2

Please sign in to comment.