Skip to content

Commit

Permalink
Fix arp scanner for devices without /proc/net/arp
Browse files Browse the repository at this point in the history
fixes #1
  • Loading branch information
csicar committed Aug 13, 2020
1 parent 51a2c58 commit 88d6377
Showing 1 changed file with 29 additions and 16 deletions.
45 changes: 29 additions & 16 deletions app/src/main/java/de/csicar/ning/scanner/ArpScanner.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.withContext
import java.io.File
import java.io.FileNotFoundException
import java.io.IOException
import java.io.InputStream
import java.net.InetAddress
import java.util.regex.Pattern
Expand Down Expand Up @@ -46,24 +48,35 @@ object ArpScanner {

}

private suspend fun getArpTableFromFile() = withContext(Dispatchers.IO) {
File("/proc/net/arp").inputStream().readStreamAsTable()
.drop(1)
.filter { it.size == 6 }
.map {
ArpEntry.from(it[0], it[3])
}
private suspend fun getArpTableFromFile() : Sequence<ArpEntry> = withContext(Dispatchers.IO) {
try {
File("/proc/net/arp").inputStream().readStreamAsTable()
.drop(1)
.filter { it.size == 6 }
.map {
ArpEntry.from(it[0], it[3])
}
} catch (exception : FileNotFoundException) {
Log.e("arp-scanner", "arp file not found $exception")
emptySequence<ArpEntry>()
}
}

private suspend fun getArpTableFromIpCommand() = withContext(Dispatchers.IO) {
val execution = Runtime.getRuntime().exec("ip neigh")
execution.waitFor()
execution.inputStream.readStreamAsTable()
.filter { it.size >= 5 }
.map {
ArpEntry.from(it[0], it[4])
}
.onEach { Log.d("asd", "found entry in 'ip neight': $it") }
private suspend fun getArpTableFromIpCommand() : Sequence<ArpEntry> = withContext(Dispatchers.IO) {
try {

val execution = Runtime.getRuntime().exec("ip neigh")
execution.waitFor()
execution.inputStream.readStreamAsTable()
.filter { it.size >= 5 }
.map {
ArpEntry.from(it[0], it[4])
}
.onEach { Log.d("arp-scanner", "found entry in 'ip neight': $it") }
} catch (exception: IOException) {
Log.e("arp-scanner", "io error when running ip neigh $exception")
emptySequence<ArpEntry>()
}
}
}

Expand Down

0 comments on commit 88d6377

Please sign in to comment.