83 lines
1.3 KiB
Go
83 lines
1.3 KiB
Go
package server
|
|
|
|
|
|
import (
|
|
"errors"
|
|
"log"
|
|
"net"
|
|
|
|
"github.com/davecgh/go-spew/spew"
|
|
)
|
|
|
|
// github.com/insomniacslk/dhcp
|
|
|
|
type addr struct {
|
|
IP net.IP
|
|
ipn *net.IPNet
|
|
}
|
|
|
|
type Config struct {
|
|
Device string `yaml:"device"`
|
|
}
|
|
|
|
type Server struct {
|
|
addrs []net.IP
|
|
l net.Listener
|
|
}
|
|
|
|
func addrsForDevice(dev string) ([]addr, error) {
|
|
netInterface, err := net.InterfaceByName(dev)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
spew.Dump(netInterface)
|
|
|
|
var addrs []addr
|
|
devAddrs, err := netInterface.Addrs()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
for _, devAddr := range devAddrs {
|
|
log.Printf("consider %s", devAddr.String())
|
|
ip, ipn, err := net.ParseCIDR(devAddr.String())
|
|
if err != nil {
|
|
continue
|
|
}
|
|
|
|
if ip == nil {
|
|
continue // address isn't an IP address
|
|
}
|
|
|
|
log.Printf("found IP: %s", ip)
|
|
ip = ip.To4()
|
|
|
|
// DHCP should only listen on private addresses.
|
|
if !ip.IsPrivate() {
|
|
log.Println("skipping non-private")
|
|
continue
|
|
}
|
|
|
|
// only support IPv4 for now
|
|
if len(ip) != 4 {
|
|
log.Printf("%d IP, only supporting v4 right now", len(ip))
|
|
continue
|
|
}
|
|
|
|
addrs = append(addrs, addr{ip, ipn})
|
|
}
|
|
|
|
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")
|
|
}
|