Fix MAC not showing for failures

This commit is contained in:
Mygod
2018-01-20 23:39:07 -08:00
parent 8655562518
commit 03ccaf2a98
4 changed files with 54 additions and 35 deletions

View File

@@ -73,15 +73,14 @@ class RepeaterFragment : Fragment(), ServiceConnection, Toolbar.OnMenuItemClickL
val statusListener = broadcastReceiver { _, _ -> onStatusChanged() } val statusListener = broadcastReceiver { _, _ -> onStatusChanged() }
} }
inner class Client(p2p: WifiP2pDevice? = null, inner class Client(p2p: WifiP2pDevice? = null, private val neighbour: IpNeighbour? = null) {
private val pair: Map.Entry<IpNeighbour, IpNeighbour.State>? = null) { private val iface = neighbour?.dev ?: p2pInterface!!
val iface = pair?.key?.dev ?: p2pInterface!! val mac = neighbour?.lladdr ?: p2p!!.deviceAddress!!
val mac = pair?.key?.lladdr ?: p2p!!.deviceAddress val ip = neighbour?.ip
val ip = pair?.key?.ip
val icon get() = TetherType.ofInterface(iface, p2pInterface).icon val icon get() = TetherType.ofInterface(iface, p2pInterface).icon
val title: CharSequence get() = listOf(ip, mac).filter { !it.isNullOrEmpty() }.joinToString(", ") val title: CharSequence get() = listOf(ip, mac).filter { !it.isNullOrEmpty() }.joinToString(", ")
val description: CharSequence get() = when (pair?.value) { val description: CharSequence get() = when (neighbour?.state) {
IpNeighbour.State.INCOMPLETE, null -> "Connecting to $iface" IpNeighbour.State.INCOMPLETE, null -> "Connecting to $iface"
IpNeighbour.State.VALID -> "Connected to $iface" IpNeighbour.State.VALID -> "Connected to $iface"
IpNeighbour.State.VALID_DELAY -> "Connected to $iface (losing)" IpNeighbour.State.VALID_DELAY -> "Connected to $iface (losing)"
@@ -93,16 +92,16 @@ class RepeaterFragment : Fragment(), ServiceConnection, Toolbar.OnMenuItemClickL
private inner class ClientAdapter : RecyclerView.Adapter<ClientViewHolder>() { private inner class ClientAdapter : RecyclerView.Adapter<ClientViewHolder>() {
private val clients = ArrayList<Client>() private val clients = ArrayList<Client>()
var p2p: Collection<WifiP2pDevice> = emptyList() var p2p: Collection<WifiP2pDevice> = emptyList()
var neighbours = emptyMap<IpNeighbour, IpNeighbour.State>() var neighbours = emptyList<IpNeighbour>()
fun recreate() { fun recreate() {
clients.clear() clients.clear()
val map = HashMap(p2p.associateBy { it.deviceAddress }) val map = HashMap(p2p.associateBy { it.deviceAddress })
val tethered = (tetheredInterfaces + p2pInterface).filterNotNull() val tethered = (tetheredInterfaces + p2pInterface).filterNotNull()
for (pair in neighbours) { for (neighbour in neighbours) {
val client = map.remove(pair.key.lladdr) val client = map.remove(neighbour.lladdr)
if (client != null) clients.add(Client(client, pair)) if (client != null) clients.add(Client(client, neighbour))
else if (tethered.contains(pair.key.dev)) clients.add(Client(pair = pair)) else if (tethered.contains(neighbour.dev)) clients.add(Client(neighbour = neighbour))
} }
clients.addAll(map.map { Client(it.value) }) clients.addAll(map.map { Client(it.value) })
clients.sortWith(compareBy<Client> { it.ip }.thenBy { it.mac }) clients.sortWith(compareBy<Client> { it.ip }.thenBy { it.mac })
@@ -210,8 +209,8 @@ class RepeaterFragment : Fragment(), ServiceConnection, Toolbar.OnMenuItemClickL
else -> false else -> false
} }
override fun onIpNeighbourAvailable(neighbours: Map<IpNeighbour, IpNeighbour.State>) { override fun onIpNeighbourAvailable(neighbours: Map<String, IpNeighbour>) {
adapter.neighbours = neighbours.toMap() adapter.neighbours = neighbours.values.toList()
} }
override fun postIpNeighbourAvailable() = adapter.recreate() override fun postIpNeighbourAvailable() = adapter.recreate()
} }

View File

@@ -1,9 +1,8 @@
package be.mygod.vpnhotspot.net package be.mygod.vpnhotspot.net
import android.util.Log import android.util.Log
import be.mygod.vpnhotspot.debugLog
data class IpNeighbour(val ip: String, val dev: String, val lladdr: String) { data class IpNeighbour(val ip: String, val dev: String, val lladdr: String, val state: State) {
enum class State { enum class State {
INCOMPLETE, VALID, VALID_DELAY, FAILED, DELETING INCOMPLETE, VALID, VALID_DELAY, FAILED, DELETING
} }
@@ -15,29 +14,36 @@ data class IpNeighbour(val ip: String, val dev: String, val lladdr: String) {
* Parser based on: * Parser based on:
* https://android.googlesource.com/platform/external/iproute2/+/ad0a6a2/ip/ipneigh.c#194 * https://android.googlesource.com/platform/external/iproute2/+/ad0a6a2/ip/ipneigh.c#194
* https://people.cs.clemson.edu/~westall/853/notes/arpstate.pdf * https://people.cs.clemson.edu/~westall/853/notes/arpstate.pdf
* Assumptions: IPv4 only, RTM_GETNEIGH is never used and show_stats = 0 * Assumptions: IP addr (key) always present, IPv4 only, RTM_GETNEIGH is never used and show_stats = 0
*/ */
private val parser = private val parser =
"^(Deleted )?((.+?) )?(dev (.+?) )?(lladdr (.+?))?( proxy)?( ([INCOMPLET,RAHBSDYF]+))?\$".toRegex() "^(Deleted )?(.+?) (dev (.+?) )?(lladdr (.+?))?( proxy)?( ([INCOMPLET,RAHBSDYF]+))?\$".toRegex()
fun parse(line: String): Pair<IpNeighbour, State>? { fun parse(line: String): IpNeighbour? {
val match = parser.matchEntire(line) val match = parser.matchEntire(line)
if (match == null) { if (match == null) {
if (!line.isBlank()) Log.w(TAG, line) if (!line.isBlank()) Log.w(TAG, line)
return null return null
} }
val neighbour = IpNeighbour(match.groupValues[3], match.groupValues[5], match.groupValues[7]) val ip = match.groupValues[2]
val state = if (match.groupValues[1].isNotEmpty()) State.DELETING else when (match.groupValues[10]) { val dev = match.groupValues[4]
var lladdr = match.groupValues[6]
// use ARP as fallback
if (dev.isNotBlank() && lladdr.isBlank()) lladdr = (NetUtils.arp()
.filter { it[NetUtils.ARP_IP_ADDRESS] == ip && it[NetUtils.ARP_DEVICE] == dev }
.map { it[NetUtils.ARP_HW_ADDRESS] }
.singleOrNull() ?: "")
val state = if (match.groupValues[1].isNotEmpty()) State.DELETING else when (match.groupValues[9]) {
"", "INCOMPLETE" -> State.INCOMPLETE "", "INCOMPLETE" -> State.INCOMPLETE
"REACHABLE", "STALE", "PROBE", "PERMANENT" -> State.VALID "REACHABLE", "STALE", "PROBE", "PERMANENT" -> State.VALID
"DELAY" -> State.VALID_DELAY "DELAY" -> State.VALID_DELAY
"FAILED" -> State.FAILED "FAILED" -> State.FAILED
"NOARP" -> return null // skip "NOARP" -> return null // skip
else -> { else -> {
Log.w(TAG, "Unknown state encountered: ${match.groupValues[10]}") Log.w(TAG, "Unknown state encountered: ${match.groupValues[9]}")
return null return null
} }
} }
return Pair(neighbour, state) return IpNeighbour(ip, dev, lladdr, state)
} }
} }
} }

View File

@@ -41,13 +41,13 @@ class IpNeighbourMonitor private constructor() {
} }
interface Callback { interface Callback {
fun onIpNeighbourAvailable(neighbours: Map<IpNeighbour, IpNeighbour.State>) fun onIpNeighbourAvailable(neighbours: Map<String, IpNeighbour>)
fun postIpNeighbourAvailable() { } fun postIpNeighbourAvailable() { }
} }
private val handler = Handler() private val handler = Handler()
private var updatePosted = false private var updatePosted = false
val neighbours = HashMap<IpNeighbour, IpNeighbour.State>() val neighbours = HashMap<String, IpNeighbour>()
/** /**
* Using monitor requires using /proc/self/ns/net which would be problematic on Android 6.0+. * Using monitor requires using /proc/self/ns/net which would be problematic on Android 6.0+.
* *
@@ -72,9 +72,10 @@ class IpNeighbourMonitor private constructor() {
monitor.inputStream.bufferedReader().forEachLine { monitor.inputStream.bufferedReader().forEachLine {
debugLog(TAG, it) debugLog(TAG, it)
synchronized(neighbours) { synchronized(neighbours) {
val (neighbour, state) = IpNeighbour.parse(it) ?: return@forEachLine val neighbour = IpNeighbour.parse(it) ?: return@forEachLine
val changed = if (state == IpNeighbour.State.DELETING) neighbours.remove(neighbour) != null else val changed = if (neighbour.state == IpNeighbour.State.DELETING)
neighbours.put(neighbour, state) != state neighbours.remove(neighbour.ip) != null
else neighbours.put(neighbour.ip, neighbour) != neighbour
if (changed) postUpdateLocked() if (changed) postUpdateLocked()
} }
} }
@@ -94,7 +95,7 @@ class IpNeighbourMonitor private constructor() {
process.inputStream.bufferedReader().useLines { process.inputStream.bufferedReader().useLines {
synchronized(neighbours) { synchronized(neighbours) {
neighbours.clear() neighbours.clear()
neighbours.putAll(it.map(IpNeighbour.Companion::parse).filterNotNull().toMap()) neighbours.putAll(it.map(IpNeighbour.Companion::parse).filterNotNull().associateBy { it.ip })
postUpdateLocked() postUpdateLocked()
} }
} }

View File

@@ -4,6 +4,7 @@ import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.support.annotation.RequiresApi import android.support.annotation.RequiresApi
import java.io.File import java.io.File
import java.io.IOException
object NetUtils { object NetUtils {
// hidden constants from ConnectivityManager // hidden constants from ConnectivityManager
@@ -27,12 +28,24 @@ object NetUtils {
extras.getStringArrayList(EXTRA_ACTIVE_TETHER).toSet() + extras.getStringArrayList(EXTRA_ACTIVE_LOCAL_ONLY) extras.getStringArrayList(EXTRA_ACTIVE_TETHER).toSet() + extras.getStringArrayList(EXTRA_ACTIVE_LOCAL_ONLY)
else extras.getStringArrayList(EXTRA_ACTIVE_TETHER_LEGACY).toSet() else extras.getStringArrayList(EXTRA_ACTIVE_TETHER_LEGACY).toSet()
fun arp(iface: String? = null) = File("/proc/net/arp").bufferedReader().useLines { // IP address HW type Flags HW address Mask Device
// IP address HW type Flags HW address Mask Device const val ARP_IP_ADDRESS = 0
it.map { it.split(spaces) } const val ARP_HW_ADDRESS = 3
.drop(1) const val ARP_DEVICE = 5
.filter { it.size >= 4 && (iface == null || it.getOrNull(5) == iface) && private const val ARP_CACHE_EXPIRE = 1L * 1000 * 1000 * 1000
mac.matcher(it[3]).matches() } private var arpCache = emptyList<List<String>>()
.associateBy({ it[3] }, { it[0] }) private var arpCacheTime = -ARP_CACHE_EXPIRE
fun arp(): List<List<String>> {
if (System.nanoTime() - arpCacheTime >= ARP_CACHE_EXPIRE) try {
arpCache = File("/proc/net/arp").bufferedReader().useLines {
it.map { it.split(spaces) }
.drop(1)
.filter { it.size >= 6 && mac.matcher(it[ARP_HW_ADDRESS]).matches() }
.toList()
}
} catch (e: IOException) {
e.printStackTrace()
}
return arpCache
} }
} }