2023-05-03 02:57:04 +00:00
|
|
|
package iptools
|
|
|
|
|
2023-05-06 05:21:27 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
2023-05-15 13:09:12 +00:00
|
|
|
"net"
|
|
|
|
"net/netip"
|
|
|
|
|
|
|
|
"git.wntrmute.dev/kyle/kdhcp/leases"
|
2023-05-06 05:21:27 +00:00
|
|
|
)
|
2023-05-03 02:57:04 +00:00
|
|
|
|
2023-05-15 13:09:12 +00:00
|
|
|
func enumerateRange(name string, r *Range, startFromOne bool) []*leases.Info {
|
2023-05-03 02:57:04 +00:00
|
|
|
start := r.Start
|
|
|
|
cur := start
|
|
|
|
lenfmt := fmt.Sprintf("%%s%%0%dd", len(fmt.Sprintf("%d", r.numHosts())))
|
|
|
|
i := 0
|
|
|
|
if startFromOne {
|
|
|
|
i++
|
|
|
|
}
|
2023-05-15 13:09:12 +00:00
|
|
|
lrange := []*leases.Info{}
|
2023-05-03 02:57:04 +00:00
|
|
|
|
|
|
|
for r.End.Compare(cur) >= 0 {
|
2023-05-06 05:21:27 +00:00
|
|
|
hostName := fmt.Sprintf(lenfmt, name, i)
|
2023-05-15 13:09:12 +00:00
|
|
|
lrange = append(lrange, &leases.Info{
|
2023-05-06 05:21:27 +00:00
|
|
|
HostName: hostName,
|
2023-05-03 02:57:04 +00:00
|
|
|
Addr: cur,
|
|
|
|
})
|
|
|
|
i++
|
|
|
|
cur = cur.Next()
|
|
|
|
}
|
|
|
|
|
2023-05-15 13:09:12 +00:00
|
|
|
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
|
2023-05-03 02:57:04 +00:00
|
|
|
}
|