fix and test #20
2 changed files with 59 additions and 0 deletions
f-15: code: add generate dhcp file
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
commit
5d980514b8
47
internal/dhcp/generate.go
Normal file
47
internal/dhcp/generate.go
Normal file
|
|
@ -0,0 +1,47 @@
|
||||||
|
package dhcp
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func GenerateConfig(c Config) (string, error) {
|
||||||
|
mask := fmt.Sprintf("%d.%d.%d.%d", c.Network.Mask[0], c.Network.Mask[1], c.Network.Mask[2], c.Network.Mask[3])
|
||||||
|
|
||||||
|
var sb strings.Builder
|
||||||
|
fmt.Fprintf(&sb, "no-resolv\n")
|
||||||
|
fmt.Fprintf(&sb, "dhcp-range=%s,static,%s,12h\n", c.Network.IP.String(), mask)
|
||||||
|
fmt.Fprintf(&sb, "dhcp-option=3,%s\n", c.Gateway.String())
|
||||||
|
fmt.Fprintf(&sb, "dhcp-option=6,1.1.1.1,8.8.8.8\n\n")
|
||||||
|
|
||||||
|
i := 0
|
||||||
|
for ip := cloneIP(c.Network.IP); c.Network.Contains(ip); incrementIP(ip) {
|
||||||
|
fmt.Fprintf(&sb, "dhcp-host=00:22:33:%02X:%02X:%02X,%s\n",
|
||||||
|
(i>>16)&0xFF, (i>>8)&0xFF, i&0xFF, ip)
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
|
||||||
|
outPath := filepath.Join(c.ConfDir, c.Name+".conf")
|
||||||
|
if err := os.MkdirAll(c.ConfDir, 0755); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return outPath, os.WriteFile(outPath, []byte(sb.String()), 0644)
|
||||||
|
}
|
||||||
|
|
||||||
|
func incrementIP(ip net.IP) {
|
||||||
|
for j := len(ip) - 1; j >= 0; j-- {
|
||||||
|
ip[j]++
|
||||||
|
if ip[j] != 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func cloneIP(ip net.IP) net.IP {
|
||||||
|
clone := make(net.IP, len(ip))
|
||||||
|
copy(clone, ip)
|
||||||
|
return clone
|
||||||
|
}
|
||||||
12
internal/dhcp/struct.go
Normal file
12
internal/dhcp/struct.go
Normal file
|
|
@ -0,0 +1,12 @@
|
||||||
|
package dhcp
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Config struct {
|
||||||
|
Network *net.IPNet
|
||||||
|
Gateway net.IP
|
||||||
|
Name string
|
||||||
|
ConfDir string
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue