Refine arp parsing

This commit is contained in:
Mygod
2020-06-26 01:20:24 +08:00
parent 288fed2391
commit 0c53fa61c7

View File

@@ -76,12 +76,17 @@ data class IpNeighbour(val ip: InetAddress, val dev: String, val lladdr: MacAddr
// or for DELETING, which we do not care about MAC not present // or for DELETING, which we do not care about MAC not present
if (ip is Inet4Address && lladdr == MacAddressCompat.ALL_ZEROS_ADDRESS && state != State.INCOMPLETE && if (ip is Inet4Address && lladdr == MacAddressCompat.ALL_ZEROS_ADDRESS && state != State.INCOMPLETE &&
state != State.DELETING) try { state != State.DELETING) try {
lladdr = MacAddressCompat.fromString(arp() val list = arp()
.asSequence() .asSequence()
.filter { parseNumericAddress(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] }
.distinct() .distinct()
.singleOrNull() ?: throw IllegalArgumentException("singleOrNull")) .toList()
when (list.size) {
1 -> lladdr = MacAddressCompat.fromString(list.single())
0 -> { }
else -> throw IllegalArgumentException("Unexpected output in arp: ${list.joinToString()}")
}
} catch (e: IllegalArgumentException) { } catch (e: IllegalArgumentException) {
Timber.w(e) Timber.w(e)
} }