Refine IpNeighbour parsing
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
package be.mygod.vpnhotspot.net
|
package be.mygod.vpnhotspot.net
|
||||||
|
|
||||||
import be.mygod.vpnhotspot.util.parseNumericAddressNoThrow
|
import be.mygod.vpnhotspot.util.parseNumericAddress
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
@@ -18,23 +18,19 @@ data class IpNeighbour(val ip: InetAddress, val dev: String, val lladdr: String,
|
|||||||
* https://people.cs.clemson.edu/~westall/853/notes/arpstate.pdf
|
* https://people.cs.clemson.edu/~westall/853/notes/arpstate.pdf
|
||||||
* Assumptions: IP addr (key) always present, RTM_GETNEIGH is never used and show_stats = 0
|
* Assumptions: IP addr (key) always present, RTM_GETNEIGH is never used and show_stats = 0
|
||||||
*/
|
*/
|
||||||
private val parser = ("^(Deleted )?(.+?) (dev (.+?) )?(lladdr (.+?))?( router)?( proxy)?" +
|
private val parser = ("^(Deleted )?([^ ]+?) (dev ([^ ]+?) )?(lladdr (.[^ ]+?))?( router)?( proxy)?" +
|
||||||
"( ([INCOMPLET,RAHBSDYF]+))?\$").toRegex()
|
"( ([INCOMPLET,RAHBSDYF]+))?\$").toRegex()
|
||||||
private fun checkLladdrNotLoopback(lladdr: String) = if (lladdr == "00:00:00:00:00:00") "" else lladdr
|
private fun checkLladdrNotLoopback(lladdr: String) = if (lladdr == "00:00:00:00:00:00") "" else lladdr
|
||||||
|
|
||||||
fun parse(line: String): IpNeighbour? {
|
fun parse(line: String): IpNeighbour? {
|
||||||
val match = parser.matchEntire(line)
|
val match = parser.matchEntire(line)!!
|
||||||
if (match == null) {
|
val ip = parseNumericAddress(match.groupValues[2])
|
||||||
if (line.isNotEmpty()) Timber.w(line)
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
val ip = parseNumericAddressNoThrow(match.groupValues[2]) ?: return null
|
|
||||||
val dev = match.groupValues[4]
|
val dev = match.groupValues[4]
|
||||||
var lladdr = checkLladdrNotLoopback(match.groupValues[6])
|
var lladdr = checkLladdrNotLoopback(match.groupValues[6])
|
||||||
// use ARP as fallback
|
// use ARP as fallback
|
||||||
if (dev.isNotEmpty() && lladdr.isEmpty()) lladdr = checkLladdrNotLoopback(arp()
|
if (dev.isNotEmpty() && lladdr.isEmpty()) lladdr = checkLladdrNotLoopback(arp()
|
||||||
.asSequence()
|
.asSequence()
|
||||||
.filter { parseNumericAddressNoThrow(it[ARP_IP_ADDRESS]) == ip && it[ARP_DEVICE] == dev }
|
.filter { parseNumericAddress(it[ARP_IP_ADDRESS]) == ip && it[ARP_DEVICE] == dev }
|
||||||
.map { it[ARP_HW_ADDRESS] }
|
.map { it[ARP_HW_ADDRESS] }
|
||||||
.singleOrNull() ?: "")
|
.singleOrNull() ?: "")
|
||||||
val state = if (match.groupValues[1].isNotEmpty() || lladdr.isEmpty()) State.DELETING else
|
val state = if (match.groupValues[1].isNotEmpty() || lladdr.isEmpty()) State.DELETING else
|
||||||
@@ -43,13 +39,16 @@ data class IpNeighbour(val ip: InetAddress, val dev: String, val lladdr: String,
|
|||||||
"REACHABLE", "DELAY", "STALE", "PROBE", "PERMANENT" -> State.VALID
|
"REACHABLE", "DELAY", "STALE", "PROBE", "PERMANENT" -> State.VALID
|
||||||
"FAILED" -> State.FAILED
|
"FAILED" -> State.FAILED
|
||||||
"NOARP" -> return null // skip
|
"NOARP" -> return null // skip
|
||||||
else -> {
|
else -> throw IllegalArgumentException("Unknown state encountered: ${match.groupValues[10]}")
|
||||||
Timber.w("Unknown state encountered: ${match.groupValues[10]}")
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return IpNeighbour(ip, dev, lladdr, state)
|
return IpNeighbour(ip, dev, lladdr, state)
|
||||||
}
|
}
|
||||||
|
fun parseNoThrow(line: String): IpNeighbour? = try {
|
||||||
|
parse(line)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
Timber.w(IllegalArgumentException("Unable to parse line: $line", e))
|
||||||
|
null
|
||||||
|
}
|
||||||
|
|
||||||
private val spaces = " +".toPattern()
|
private val spaces = " +".toPattern()
|
||||||
private val mac = "^([0-9a-f]{2}:){5}[0-9a-f]{2}$".toPattern()
|
private val mac = "^([0-9a-f]{2}:){5}[0-9a-f]{2}$".toPattern()
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package be.mygod.vpnhotspot.net.monitor
|
package be.mygod.vpnhotspot.net.monitor
|
||||||
|
|
||||||
import be.mygod.vpnhotspot.App.Companion.app
|
import be.mygod.vpnhotspot.App.Companion.app
|
||||||
import be.mygod.vpnhotspot.debugLog
|
|
||||||
import be.mygod.vpnhotspot.net.IpNeighbour
|
import be.mygod.vpnhotspot.net.IpNeighbour
|
||||||
import java.net.InetAddress
|
import java.net.InetAddress
|
||||||
|
|
||||||
@@ -39,8 +38,7 @@ class IpNeighbourMonitor private constructor() : IpMonitor() {
|
|||||||
|
|
||||||
override fun processLine(line: String) {
|
override fun processLine(line: String) {
|
||||||
synchronized(neighbours) {
|
synchronized(neighbours) {
|
||||||
val neighbour = IpNeighbour.parse(line) ?: return
|
val neighbour = IpNeighbour.parseNoThrow(line) ?: return
|
||||||
debugLog(javaClass.simpleName, line)
|
|
||||||
val changed = if (neighbour.state == IpNeighbour.State.DELETING)
|
val changed = if (neighbour.state == IpNeighbour.State.DELETING)
|
||||||
neighbours.remove(neighbour.ip) != null
|
neighbours.remove(neighbour.ip) != null
|
||||||
else neighbours.put(neighbour.ip, neighbour) != neighbour
|
else neighbours.put(neighbour.ip, neighbour) != neighbour
|
||||||
@@ -52,7 +50,7 @@ class IpNeighbourMonitor private constructor() : IpMonitor() {
|
|||||||
synchronized(neighbours) {
|
synchronized(neighbours) {
|
||||||
neighbours.clear()
|
neighbours.clear()
|
||||||
neighbours.putAll(lines
|
neighbours.putAll(lines
|
||||||
.map(IpNeighbour.Companion::parse)
|
.map(IpNeighbour.Companion::parseNoThrow)
|
||||||
.filterNotNull()
|
.filterNotNull()
|
||||||
.filter { it.state != IpNeighbour.State.DELETING } // skip entries without lladdr
|
.filter { it.state != IpNeighbour.State.DELETING } // skip entries without lladdr
|
||||||
.associateBy { it.ip })
|
.associateBy { it.ip })
|
||||||
|
|||||||
@@ -57,17 +57,11 @@ fun NetworkInterface.formatAddresses() =
|
|||||||
.joinToString("\n")
|
.joinToString("\n")
|
||||||
|
|
||||||
private val parseNumericAddress by lazy {
|
private val parseNumericAddress by lazy {
|
||||||
// parseNumericAddressNoThrow is in dark grey list unfortunately
|
|
||||||
InetAddress::class.java.getDeclaredMethod("parseNumericAddress", String::class.java).apply {
|
InetAddress::class.java.getDeclaredMethod("parseNumericAddress", String::class.java).apply {
|
||||||
isAccessible = true
|
isAccessible = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fun parseNumericAddress(address: String) = parseNumericAddress.invoke(null, address) as InetAddress
|
fun parseNumericAddress(address: String) = parseNumericAddress.invoke(null, address) as InetAddress
|
||||||
fun parseNumericAddressNoThrow(address: String): InetAddress? = try {
|
|
||||||
parseNumericAddress(address)
|
|
||||||
} catch (_: IllegalArgumentException) {
|
|
||||||
null
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Wrapper for kotlin.concurrent.thread that silences uncaught exceptions.
|
* Wrapper for kotlin.concurrent.thread that silences uncaught exceptions.
|
||||||
|
|||||||
Reference in New Issue
Block a user