add management IP to machine and peer configs, update daemon to listen on it

This commit is contained in:
Pavel Sviderski
2024-08-30 18:33:37 +10:00
parent c93206e6d1
commit fd396dd91f
10 changed files with 124 additions and 39 deletions
+33
View File
@@ -1,6 +1,7 @@
package network
import (
"fmt"
"net"
"net/netip"
"uncloud/internal/secret"
@@ -24,3 +25,35 @@ func prefixToIPNet(prefix netip.Prefix) net.IPNet {
Mask: net.CIDRMask(prefix.Bits(), prefix.Addr().BitLen()),
}
}
// ipNetToPrefix returns a netip.Prefix from the net.IPNet type. If ipNet is invalid, ok is false.
// Based on https://github.com/tailscale/tailscale/blob/main/net/netaddr/netaddr.go
func ipNetToPrefix(ipNet net.IPNet) (netip.Prefix, error) {
ip, ok := netip.AddrFromSlice(ipNet.IP)
if !ok {
return netip.Prefix{}, fmt.Errorf("invalid IP network")
}
ip = ip.Unmap()
if l := len(ipNet.Mask); l != net.IPv4len && l != net.IPv6len {
return netip.Prefix{}, fmt.Errorf("invalid IP network mask length: %d", l)
}
ones, bits := ipNet.Mask.Size()
if ones == 0 && bits == 0 {
return netip.Prefix{}, fmt.Errorf("non-contiguous IP network mask")
}
return netip.PrefixFrom(ip, ones), nil
}
func addrToSingleIPPrefix(addr netip.Addr) (netip.Prefix, error) {
if !addr.IsValid() {
return netip.Prefix{}, fmt.Errorf("invalid IP address")
}
bits := 32
if addr.Is6() {
bits = 128
}
return addr.Prefix(bits)
}