kdhcp/iptools/tools.go

46 lines
747 B
Go

package iptools
import (
"fmt"
"net"
"net/netip"
"git.wntrmute.dev/kyle/kdhcp/leases"
)
func enumerateRange(name string, r *Range, startFromOne bool) []*leases.Info {
start := r.Start
cur := start
lenfmt := fmt.Sprintf("%%s%%0%dd", len(fmt.Sprintf("%d", r.numHosts())))
i := 0
if startFromOne {
i++
}
lrange := []*leases.Info{}
for r.End.Compare(cur) >= 0 {
hostName := fmt.Sprintf(lenfmt, name, i)
lrange = append(lrange, &leases.Info{
HostName: hostName,
Addr: cur,
})
i++
cur = cur.Next()
}
return lrange
}
func NetIPtoAddr(ip net.IP) netip.Addr {
addr, ok := netip.AddrFromSlice(ip)
if !ok {
return netip.IPv4Unspecified()
}
if !addr.Is4() {
return netip.IPv4Unspecified()
}
return addr
}