kdhcp/server/server.go

83 lines
1.3 KiB
Go
Raw Normal View History

2023-04-22 08:25:13 +00:00
package server
2023-04-30 06:46:44 +00:00
2023-04-22 08:25:13 +00:00
import (
"errors"
"log"
"net"
2023-04-30 06:46:44 +00:00
"github.com/davecgh/go-spew/spew"
2023-04-22 08:25:13 +00:00
)
// github.com/insomniacslk/dhcp
2023-04-30 06:46:44 +00:00
type addr struct {
IP net.IP
ipn *net.IPNet
}
2023-04-22 08:25:13 +00:00
type Config struct {
Device string `yaml:"device"`
}
type Server struct {
addrs []net.IP
l net.Listener
}
2023-04-30 06:46:44 +00:00
func addrsForDevice(dev string) ([]addr, error) {
2023-04-22 08:25:13 +00:00
netInterface, err := net.InterfaceByName(dev)
if err != nil {
return nil, err
}
2023-04-30 06:46:44 +00:00
spew.Dump(netInterface)
var addrs []addr
2023-04-22 08:25:13 +00:00
devAddrs, err := netInterface.Addrs()
if err != nil {
return nil, err
}
2023-04-30 06:46:44 +00:00
for _, devAddr := range devAddrs {
log.Printf("consider %s", devAddr.String())
ip, ipn, err := net.ParseCIDR(devAddr.String())
if err != nil {
continue
}
2023-04-22 08:25:13 +00:00
if ip == nil {
continue // address isn't an IP address
}
2023-04-30 06:46:44 +00:00
log.Printf("found IP: %s", ip)
ip = ip.To4()
2023-04-22 08:25:13 +00:00
// DHCP should only listen on private addresses.
if !ip.IsPrivate() {
2023-04-30 06:46:44 +00:00
log.Println("skipping non-private")
2023-04-22 08:25:13 +00:00
continue
}
// only support IPv4 for now
if len(ip) != 4 {
2023-04-30 06:46:44 +00:00
log.Printf("%d IP, only supporting v4 right now", len(ip))
2023-04-22 08:25:13 +00:00
continue
}
2023-04-30 06:46:44 +00:00
addrs = append(addrs, addr{ip, ipn})
2023-04-22 08:25:13 +00:00
}
return addrs, nil
}
func NewServer(cfg *Config) (*Server, error) {
addrs, err := addrsForDevice(cfg.Device)
if err != nil {
return nil, err
}
log.Println("server IP list: ", addrs)
return nil, errors.New("not implemented")
}