diff --git a/app/src/main/java/de/csicar/ning/scanner/ArpScanner.kt b/app/src/main/java/de/csicar/ning/scanner/ArpScanner.kt index fa539dc..015cc45 100644 --- a/app/src/main/java/de/csicar/ning/scanner/ArpScanner.kt +++ b/app/src/main/java/de/csicar/ning/scanner/ArpScanner.kt @@ -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 @@ -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 = 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() + } } - 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 = 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() + } } }