2018-10-25 10:24:45 +02:00
|
|
|
package owrt
|
|
|
|
|
|
|
|
import "fmt"
|
|
|
|
|
|
|
|
// UCIDHCPConf describes uci dhcp server configuration for an network inteface (uci show dhcp)
|
|
|
|
type UCIDHCPConf struct {
|
|
|
|
Name string
|
|
|
|
IFName string
|
|
|
|
LeaseTime string
|
|
|
|
RangeLimit string
|
|
|
|
StartIP string
|
2018-10-30 09:55:54 +01:00
|
|
|
UciClient *UCI
|
2018-10-25 10:24:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewUCIDHCPConf builds a new UCIDHCPConf instance
|
|
|
|
func NewUCIDHCPConf(uci *UCI) *UCIDHCPConf {
|
|
|
|
return &UCIDHCPConf{
|
2018-10-30 09:55:54 +01:00
|
|
|
UciClient: uci,
|
2018-10-25 10:24:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create add a new entry for wifi interface in UCI Configuration
|
|
|
|
func (dh *UCIDHCPConf) Create() *Action {
|
2018-10-30 09:55:54 +01:00
|
|
|
uci := dh.UciClient
|
2018-10-25 10:24:45 +02:00
|
|
|
conf := make(map[string]string)
|
|
|
|
|
|
|
|
conf["name"] = dh.Name
|
|
|
|
conf["interface"] = dh.IFName
|
|
|
|
conf["leasetime"] = dh.LeaseTime
|
|
|
|
conf["limit"] = dh.RangeLimit
|
|
|
|
conf["ra_management"] = "1"
|
|
|
|
conf["start"] = dh.StartIP
|
|
|
|
|
|
|
|
result := uci.Set(fmt.Sprintf("dhcp.%s", dh.Name), "dhcp")
|
|
|
|
if result.ReturnCode != 0 {
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
for key, value := range conf {
|
2018-10-25 12:03:54 +02:00
|
|
|
if value == "" {
|
|
|
|
return &Action{
|
|
|
|
CommandResult: &CommandResult{
|
|
|
|
Stdout: "",
|
|
|
|
Stderr: fmt.Sprintf("dhcp.%s can't be empty", key),
|
|
|
|
ReturnCode: 100,
|
|
|
|
},
|
|
|
|
Command: "Empty value error",
|
|
|
|
}
|
|
|
|
}
|
2018-10-25 10:24:45 +02:00
|
|
|
result := uci.Set(fmt.Sprintf("dhcp.%s.%s", dh.Name, key), value)
|
|
|
|
if result.ReturnCode != 0 {
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Action{
|
|
|
|
CommandResult: &CommandResult{
|
|
|
|
Stdout: "",
|
|
|
|
Stderr: "",
|
|
|
|
ReturnCode: 0,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save commit and relaod configuration (writes it to files !)
|
|
|
|
func (dh *UCIDHCPConf) Save() *Action {
|
2018-10-30 09:55:54 +01:00
|
|
|
uci := dh.UciClient
|
2018-10-25 10:24:45 +02:00
|
|
|
commitRes := uci.Commit()
|
|
|
|
if commitRes.ReturnCode != 0 {
|
|
|
|
return commitRes
|
|
|
|
}
|
|
|
|
|
|
|
|
reload := uci.Reload()
|
|
|
|
return reload
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete remove wifi interface from UCI Configuration
|
|
|
|
func (dh *UCIDHCPConf) Delete() *Action {
|
2018-10-30 09:55:54 +01:00
|
|
|
uci := dh.UciClient
|
2018-10-25 10:24:45 +02:00
|
|
|
toDelete := fmt.Sprintf("network.%s", dh.Name)
|
|
|
|
del := uci.Delete(toDelete)
|
|
|
|
if del.ReturnCode != 0 {
|
|
|
|
return del
|
|
|
|
}
|
|
|
|
return uci.Commit()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update add a new entry for wifi interface in UCI Configuration
|
|
|
|
func (dh *UCIDHCPConf) Update() *Action {
|
2018-10-30 09:55:54 +01:00
|
|
|
uci := dh.UciClient
|
2018-10-25 10:24:45 +02:00
|
|
|
dh.Delete()
|
|
|
|
create := dh.Create()
|
|
|
|
if create.ReturnCode != 0 {
|
|
|
|
return create
|
|
|
|
}
|
|
|
|
return uci.Commit()
|
|
|
|
}
|