Refine IpNeighbour parsing

This commit is contained in:
Mygod
2018-10-04 16:34:40 +08:00
parent ed1e1a581e
commit e85d792218
3 changed files with 14 additions and 23 deletions

View File

@@ -1,6 +1,6 @@
package be.mygod.vpnhotspot.net
import be.mygod.vpnhotspot.util.parseNumericAddressNoThrow
import be.mygod.vpnhotspot.util.parseNumericAddress
import timber.log.Timber
import java.io.File
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
* 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()
private fun checkLladdrNotLoopback(lladdr: String) = if (lladdr == "00:00:00:00:00:00") "" else lladdr
fun parse(line: String): IpNeighbour? {
val match = parser.matchEntire(line)
if (match == null) {
if (line.isNotEmpty()) Timber.w(line)
return null
}
val ip = parseNumericAddressNoThrow(match.groupValues[2]) ?: return null
val match = parser.matchEntire(line)!!
val ip = parseNumericAddress(match.groupValues[2])
val dev = match.groupValues[4]
var lladdr = checkLladdrNotLoopback(match.groupValues[6])
// use ARP as fallback
if (dev.isNotEmpty() && lladdr.isEmpty()) lladdr = checkLladdrNotLoopback(arp()
.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] }
.singleOrNull() ?: "")
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
"FAILED" -> State.FAILED
"NOARP" -> return null // skip
else -> {
Timber.w("Unknown state encountered: ${match.groupValues[10]}")
return null
}
else -> throw IllegalArgumentException("Unknown state encountered: ${match.groupValues[10]}")
}
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 mac = "^([0-9a-f]{2}:){5}[0-9a-f]{2}$".toPattern()

View File

@@ -1,7 +1,6 @@
package be.mygod.vpnhotspot.net.monitor
import be.mygod.vpnhotspot.App.Companion.app
import be.mygod.vpnhotspot.debugLog
import be.mygod.vpnhotspot.net.IpNeighbour
import java.net.InetAddress
@@ -39,8 +38,7 @@ class IpNeighbourMonitor private constructor() : IpMonitor() {
override fun processLine(line: String) {
synchronized(neighbours) {
val neighbour = IpNeighbour.parse(line) ?: return
debugLog(javaClass.simpleName, line)
val neighbour = IpNeighbour.parseNoThrow(line) ?: return
val changed = if (neighbour.state == IpNeighbour.State.DELETING)
neighbours.remove(neighbour.ip) != null
else neighbours.put(neighbour.ip, neighbour) != neighbour
@@ -52,7 +50,7 @@ class IpNeighbourMonitor private constructor() : IpMonitor() {
synchronized(neighbours) {
neighbours.clear()
neighbours.putAll(lines
.map(IpNeighbour.Companion::parse)
.map(IpNeighbour.Companion::parseNoThrow)
.filterNotNull()
.filter { it.state != IpNeighbour.State.DELETING } // skip entries without lladdr
.associateBy { it.ip })

View File

@@ -57,17 +57,11 @@ fun NetworkInterface.formatAddresses() =
.joinToString("\n")
private val parseNumericAddress by lazy {
// parseNumericAddressNoThrow is in dark grey list unfortunately
InetAddress::class.java.getDeclaredMethod("parseNumericAddress", String::class.java).apply {
isAccessible = true
}
}
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.