Starting DHCP packets.

This commit is contained in:
2023-05-02 23:33:14 +00:00
parent 3ec9af1fde
commit ac41c7a480
17 changed files with 11743 additions and 32 deletions

View File

@@ -5,7 +5,6 @@ import (
"fmt"
"io/ioutil"
"net"
"strconv"
"git.wntrmute.dev/kyle/kdhcp/log"
"gopkg.in/yaml.v2"
@@ -42,6 +41,7 @@ func (r *IPRange) ensureV4() (err error) {
}
type Network struct {
IP net.IP `yaml:"address"`
Gateway net.IP `yaml:"gateway"`
Mask net.IP `yaml:"mask"`
Broadcast net.IP `yaml:"broadcast"`
@@ -50,6 +50,11 @@ type Network struct {
}
func (n *Network) ensureV4() (err error) {
n.IP, err = ensureV4(n.IP)
if err != nil {
return fmt.Errorf("config: IP %w", err)
}
n.Gateway, err = ensureV4(n.Gateway)
if err != nil {
return fmt.Errorf("config: gateway %w", err)
@@ -80,11 +85,10 @@ type ConfigFile struct {
}
type Config struct {
Version int `yaml:"version"`
Interface string `yaml:"interface"`
Address string `yaml:"address"`
IP net.IP
Port int
Version int `yaml:"version"`
Interface string `yaml:"interface"`
Address string `yaml:"address"`
Port int `yaml:"port"`
LeaseFile string `yaml:"lease_file"`
Network *Network `yaml:"network"`
Pools map[string]*IPRange `yaml:"pools"`
@@ -104,26 +108,6 @@ func (cfg *Config) process() (err error) {
return fmt.Errorf("config: while looking up interface %s: %w", cfg.Interface, err)
}
ip, port, err := net.SplitHostPort(cfg.Address)
if err != nil {
return err
}
cfg.IP = net.ParseIP(ip)
if cfg.IP == nil {
return fmt.Errorf("config: parsing IP from address %s: %w", cfg.Address, err)
}
cfg.IP, err = ensureV4(cfg.IP)
if err != nil {
return fmt.Errorf("config: address %w", err)
}
cfg.Port, err = strconv.Atoi(port)
if err != nil {
return fmt.Errorf("config: invalid port %s: %w", port, err)
}
err = cfg.Network.ensureV4()
if err != nil {
return err

View File

@@ -13,6 +13,7 @@ func FindConfigPath() string {
user, err := user.Current()
if err == nil {
if homedir := user.HomeDir; homedir != "" {
paths = append(paths, filepath.Join(homedir, "code", "kdhcp", "kdhcpd.yaml"))
paths = append(paths, filepath.Join(homedir, ".config", "kdhcp", "kdhcpd.yaml"))
}
}
@@ -20,6 +21,7 @@ func FindConfigPath() string {
paths = append(paths, "/etc/kdhcp/kdhcpd.yaml")
for _, path := range paths {
log.Debugf("config: looking for config file at %s", path)
_, err = os.Stat(path)
if os.IsNotExist(err) {
continue