kdhcp/server/addr.go

62 lines
1.1 KiB
Go
Raw Normal View History

2023-05-01 14:37:52 +00:00
package server
import (
"fmt"
"net"
"git.wntrmute.dev/kyle/kdhcp/bazel-kdhcp/external/com_github_davecgh_go_spew/spew"
"git.wntrmute.dev/kyle/kdhcp/log"
)
type addr struct {
ifi *net.Interface
IP net.IP
ipn *net.IPNet
}
func addrsForDevice(dev string) ([]addr, error) {
netInterface, err := net.InterfaceByName(dev)
if err != nil {
return nil, fmt.Errorf("while selecting interface %s: %w", dev, err)
}
spew.Dump(netInterface)
var addrs []addr
devAddrs, err := netInterface.Addrs()
if err != nil {
return nil, err
}
for _, devAddr := range devAddrs {
log.Debugf("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.Debugf("found IP: %s", ip)
ip = ip.To4()
// DHCP should only listen on private addresses.
if !ip.IsPrivate() {
log.Debugln("skipping non-private")
continue
}
// only support IPv4 for now
if len(ip) != 4 {
log.Debugf("%d IP, only supporting v4 right now", len(ip))
continue
}
addrs = append(addrs, addr{netInterface, ip, ipn})
}
return addrs, nil
}