kdhcp/server/server.go

63 lines
995 B
Go

package server
import (
"errors"
"log"
"net"
)
// github.com/insomniacslk/dhcp
type Config struct {
Device string `yaml:"device"`
}
type Server struct {
addrs []net.IP
l net.Listener
}
func addrsForDevice(dev string) ([]net.IP, error) {
netInterface, err := net.InterfaceByName(dev)
if err != nil {
return nil, err
}
var addrs []net.IP
devAddrs, err := netInterface.Addrs()
if err != nil {
return nil, err
}
for _, addr := range devAddrs {
ip := net.ParseIP(addr.String())
if ip == nil {
continue // address isn't an IP address
}
// DHCP should only listen on private addresses.
if !ip.IsPrivate() {
continue
}
// only support IPv4 for now
if len(ip) != 4 {
continue
}
addrs = append(addrs, ip)
}
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")
}