You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.0 KiB

package zones
import "net/netip"
// ParseRingZeroAddress extracts zone and node ID from a wg0 [netip.Addr]
func ParseRingZeroAddress(addr netip.Addr) (zoneID int, nodeID int, ok bool) {
if addr.IsValid() {
a4 := addr.As4()
if a4[0] == 10 && a4[1] == 0 {
return int(a4[2]), int(a4[3]), true
}
}
return 0, 0, false
}
// RingZeroAddress returns a wg0 IP address
func RingZeroAddress(zoneID, nodeID int) netip.Addr {
c := zoneID
d := nodeID
return netip.AddrFrom4([4]byte{
10, 0, uint8(c), uint8(d),
})
}
// ParseRingOneAddress extracts zone and node ID from a wg1 [netip.Addr]
func ParseRingOneAddress(addr netip.Addr) (zoneID int, nodeID int, ok bool) {
if addr.IsValid() {
a4 := addr.As4()
if a4[0] == 10 && a4[2] == 0 {
zoneID = int(a4[1] >> 4)
nodeID = int(a4[3])
return zoneID, nodeID, true
}
}
return 0, 0, false
}
// RingOneAddress returns a wg1 IP address
func RingOneAddress(zoneID, nodeID int) netip.Addr {
b := zoneID << 4
d := nodeID
return netip.AddrFrom4([4]byte{
10, uint8(b), 0, uint8(d),
})
}