2020-08-08 20:23:27 +02:00
|
|
|
package dorkbox.netUtil
|
|
|
|
|
|
|
|
import dorkbox.executor.Executor
|
|
|
|
import java.io.File
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
object Dhcp {
|
2021-04-06 23:42:45 +02:00
|
|
|
/**
|
|
|
|
* Gets the version number.
|
|
|
|
*/
|
2022-03-03 00:39:25 +01:00
|
|
|
const val version = "2.9.1"
|
2021-04-06 23:42:45 +02:00
|
|
|
|
2020-08-18 22:28:03 +02:00
|
|
|
fun start(id: String, interfaceName: String) {
|
2020-08-08 20:23:27 +02:00
|
|
|
if (Common.OS_LINUX) {
|
2020-08-18 22:28:03 +02:00
|
|
|
stop(id, interfaceName)
|
2020-08-08 20:23:27 +02:00
|
|
|
val dhcpPidFile = "/var/run/dhclient-$id.pid"
|
2020-08-18 22:28:03 +02:00
|
|
|
Executor().command("/sbin/dhclient", "-pf", dhcpPidFile, interfaceName).startBlocking()
|
2020-08-08 20:23:27 +02:00
|
|
|
} else {
|
|
|
|
throw RuntimeException("NOT IMPL.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-18 22:28:03 +02:00
|
|
|
fun stop(id: String, interfaceName: String) {
|
2020-08-08 20:23:27 +02:00
|
|
|
if (Common.OS_LINUX) {
|
|
|
|
val dhcpPidFile = "/var/run/dhclient-$id.pid"
|
|
|
|
|
|
|
|
// close the dhclient if it was already running (based on pid file), and delete the pid file
|
|
|
|
Executor().command("/sbin/dhclient", "-r -pf", dhcpPidFile, interfaceName).startBlocking()
|
|
|
|
|
|
|
|
// short break
|
|
|
|
try {
|
|
|
|
Thread.sleep(500)
|
|
|
|
} catch (e: InterruptedException) {
|
|
|
|
e.printStackTrace()
|
|
|
|
}
|
|
|
|
File(dhcpPidFile).delete()
|
|
|
|
} else {
|
|
|
|
throw RuntimeException("NOT IMPL.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|