Added dhcp to namespace, cleaned up "normal" dhcp

This commit is contained in:
nathan 2020-08-18 22:28:03 +02:00
parent 32dc98470b
commit f8b4485cf2
2 changed files with 37 additions and 5 deletions

View File

@ -7,18 +7,17 @@ import java.io.File
* *
*/ */
object Dhcp { object Dhcp {
fun start(id: String, interfaceName: String) {
fun start(nameSpace: String, id: String, interfaceName: String) {
if (Common.OS_LINUX) { if (Common.OS_LINUX) {
stop(nameSpace, id, interfaceName) stop(id, interfaceName)
val dhcpPidFile = "/var/run/dhclient-$id.pid" val dhcpPidFile = "/var/run/dhclient-$id.pid"
Executor().command("/sbin/dhclient", "-pf", dhcpPidFile, interfaceName).startBlocking(); Executor().command("/sbin/dhclient", "-pf", dhcpPidFile, interfaceName).startBlocking()
} else { } else {
throw RuntimeException("NOT IMPL.") throw RuntimeException("NOT IMPL.")
} }
} }
fun stop(nameSpace: String, id: String, interfaceName: String) { fun stop(id: String, interfaceName: String) {
if (Common.OS_LINUX) { if (Common.OS_LINUX) {
val dhcpPidFile = "/var/run/dhclient-$id.pid" val dhcpPidFile = "/var/run/dhclient-$id.pid"

View File

@ -19,6 +19,39 @@ object NameSpace {
} }
} }
object Dhcp {
object Dhcp {
fun start(nameSpace: String, id: String, interfaceName: String) {
if (Common.OS_LINUX) {
stop(nameSpace, id, interfaceName)
val dhcpPidFile = "/var/run/dhclient-$id.pid"
run(nameSpace, "/sbin/dhclient", "-pf", dhcpPidFile, interfaceName)
} else {
throw RuntimeException("NOT IMPL.")
}
}
fun stop(nameSpace: String, id: String, interfaceName: String) {
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
run(nameSpace, "/sbin/dhclient", "-r -pf", dhcpPidFile, interfaceName)
// short break
try {
Thread.sleep(500)
} catch (e: InterruptedException) {
e.printStackTrace()
}
File(dhcpPidFile).delete()
} else {
throw RuntimeException("NOT IMPL.")
}
}
}
}
private val nameSpaceToIifToIp: MutableMap<String, MutableMap<String, String>> = HashMap() private val nameSpaceToIifToIp: MutableMap<String, MutableMap<String, String>> = HashMap()
fun add(nameSpace: String) { fun add(nameSpace: String) {