Move MAC utils to MacAddressCompat

This commit is contained in:
Mygod
2020-05-30 02:06:41 -04:00
parent d328215764
commit e8fb62a0b3
15 changed files with 97 additions and 61 deletions

View File

@@ -3,7 +3,6 @@ package be.mygod.vpnhotspot.net
import android.os.Build
import android.system.ErrnoException
import android.system.OsConstants
import be.mygod.vpnhotspot.room.macToLong
import be.mygod.vpnhotspot.util.parseNumericAddress
import timber.log.Timber
import java.io.File
@@ -13,7 +12,7 @@ import java.net.InetAddress
import java.net.NetworkInterface
import java.net.SocketException
data class IpNeighbour(val ip: InetAddress, val dev: String, val lladdr: Long, val state: State) {
data class IpNeighbour(val ip: InetAddress, val dev: String, val lladdr: MacAddressCompat, val state: State) {
enum class State {
INCOMPLETE, VALID, FAILED, DELETING
}
@@ -56,12 +55,12 @@ data class IpNeighbour(val ip: InetAddress, val dev: String, val lladdr: Long, v
else -> throw IllegalArgumentException("Unknown state encountered: ${match.groupValues[7]}")
}
val mac = try {
lladdr.macToLong()
} catch (e: NumberFormatException) {
MacAddressCompat.fromString(lladdr)
} catch (e: IllegalArgumentException) {
if (match.groups[4] == null) return emptyList()
// for DELETING, we only care about IP address and do not care if MAC is not present
if (state != State.DELETING) Timber.w(IOException("Failed to find MAC address for $line"))
0L
MacAddressCompat.ALL_ZEROS_ADDRESS
}
val result = IpNeighbour(ip, dev, mac, state)
val devParser = devFallback.matchEntire(dev)

View File

@@ -0,0 +1,45 @@
package be.mygod.vpnhotspot.net
import java.nio.ByteBuffer
import java.nio.ByteOrder
inline class MacAddressCompat(val addr: Long) {
companion object {
private const val ETHER_ADDR_LEN = 6
/**
* The MacAddress zero MAC address.
*
* Not publicly exposed or treated specially since the OUI 00:00:00 is registered.
* @hide
*/
val ALL_ZEROS_ADDRESS = MacAddressCompat(0)
fun bytesToString(addr: ByteArray): String {
require(addr.size == ETHER_ADDR_LEN) { addr.contentToString() + " was not a valid MAC address" }
return addr.joinToString(":") { "%02x".format(it) }
}
fun bytesToString(addr: Collection<Byte>): String {
require(addr.size == ETHER_ADDR_LEN) { addr.joinToString() + " was not a valid MAC address" }
return addr.joinToString(":") { "%02x".format(it) }
}
@Throws(IllegalArgumentException::class)
fun fromString(addr: String) = MacAddressCompat(ByteBuffer.allocate(8).run {
order(ByteOrder.LITTLE_ENDIAN)
mark()
try {
put(addr.split(':').map { Integer.parseInt(it, 16).toByte() }.toByteArray())
} catch (e: NumberFormatException) {
throw IllegalArgumentException(e)
}
reset()
long
})
}
override fun toString() = ByteBuffer.allocate(8).run {
order(ByteOrder.LITTLE_ENDIAN)
putLong(addr)
bytesToString(array().take(6))
}
}

View File

@@ -208,7 +208,7 @@ class Routing(private val caller: Any, private val downstream: String,
private val upstream = Upstream(RULE_PRIORITY_UPSTREAM)
private var disableSystem: RootSession.Transaction? = null
private inner class Client(private val ip: Inet4Address, mac: Long) : AutoCloseable {
private inner class Client(private val ip: Inet4Address, mac: MacAddressCompat) : AutoCloseable {
private val transaction = RootSession.beginTransaction().safeguard {
val address = ip.hostAddress
iptablesInsert("vpnhotspot_acl -i $downstream -s $address -j ACCEPT")

View File

@@ -1,6 +1,7 @@
package be.mygod.vpnhotspot.net.monitor
import android.util.LongSparseArray
import be.mygod.vpnhotspot.net.MacAddressCompat
import be.mygod.vpnhotspot.net.Routing.Companion.IPTABLES
import be.mygod.vpnhotspot.room.AppDatabase
import be.mygod.vpnhotspot.room.TrafficRecord
@@ -20,8 +21,8 @@ object TrafficRecorder {
private val records = mutableMapOf<Pair<InetAddress, String>, TrafficRecord>()
val foregroundListeners = Event2<Collection<TrafficRecord>, LongSparseArray<TrafficRecord>>()
fun register(ip: InetAddress, downstream: String, mac: Long) {
val record = TrafficRecord(mac = mac, ip = ip, downstream = downstream)
fun register(ip: InetAddress, downstream: String, mac: MacAddressCompat) {
val record = TrafficRecord(mac = mac.addr, ip = ip, downstream = downstream)
AppDatabase.instance.trafficRecordDao.insert(record)
synchronized(this) {
Timber.d("Registering $ip%$downstream")
@@ -149,5 +150,5 @@ object TrafficRecorder {
/**
* Possibly inefficient. Don't call this too often.
*/
fun isWorking(mac: Long) = records.values.any { it.mac == mac }
fun isWorking(mac: MacAddressCompat) = records.values.any { it.mac == mac.addr }
}