From e85d792218765c0fe62e7affa81adf4a697da057 Mon Sep 17 00:00:00 2001 From: Mygod Date: Thu, 4 Oct 2018 16:34:40 +0800 Subject: [PATCH] Refine IpNeighbour parsing --- .../be/mygod/vpnhotspot/net/IpNeighbour.kt | 25 +++++++++---------- .../net/monitor/IpNeighbourMonitor.kt | 6 ++--- .../java/be/mygod/vpnhotspot/util/Utils.kt | 6 ----- 3 files changed, 14 insertions(+), 23 deletions(-) diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/net/IpNeighbour.kt b/mobile/src/main/java/be/mygod/vpnhotspot/net/IpNeighbour.kt index 57877e3d..15a48bb6 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/net/IpNeighbour.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/net/IpNeighbour.kt @@ -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() diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/net/monitor/IpNeighbourMonitor.kt b/mobile/src/main/java/be/mygod/vpnhotspot/net/monitor/IpNeighbourMonitor.kt index 427668fb..f284b640 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/net/monitor/IpNeighbourMonitor.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/net/monitor/IpNeighbourMonitor.kt @@ -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 }) diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/util/Utils.kt b/mobile/src/main/java/be/mygod/vpnhotspot/util/Utils.kt index d6634603..391f3faf 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/util/Utils.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/util/Utils.kt @@ -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.