mirror of
https://github.com/psviderski/uncloud.git
synced 2026-08-26 19:13:34 +00:00
add management IP to machine and peer configs, update daemon to listen on it
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user