rings: DecodeAddress() [WIP]

Signed-off-by: Alejandro Mery <amery@jpi.io>
This commit is contained in:
2024-05-28 02:29:34 +00:00
parent 16f6f1141c
commit ac091dcca9
2 changed files with 76 additions and 0 deletions
+62
View File
@@ -0,0 +1,62 @@
package rings
import (
"net/netip"
"darvaza.org/core"
)
// DecodeAddress ... TODO
//
// revive:disable:function-result-limit
func DecodeAddress(addr netip.Addr) (Ring, RegionID, ZoneID, uint) {
// revive:enable:function-result-limit
if addr.IsValid() {
if addr.Is4In6() {
addr = addr.Unmap()
}
if addr.Is4() {
a4 := addr.As4()
return unsafeDecodeAddress(a4[0], a4[1], a4[2], a4[3])
}
}
return InvalidRing, 0, 0, 0
}
// revive:disable:function-result-limit
func unsafeDecodeAddress(a, b, c, d byte) (Ring, RegionID, ZoneID, uint) {
// revive:enable:function-result-limit
switch {
case a != 10:
return InvalidRing, 0, 0, 0
case b&0xf0 != 0:
k := RingThree
r := RegionID(b >> 4)
n2 := uint(b & 0x0f)
n1 := uint(c)
n0 := uint(d)
n := n0 + n1<<8 + n2<<16
return k, r, 0, n
case b&0x0f != 0:
r := RegionID(b)
z := ZoneID(c >> 4)
k := core.IIf(z == 0, RingTwo, RingOne)
n1 := uint(c & 0x0f)
n0 := uint(d)
n := n0 + n1<<8
return k, r, z, n
default: // b == 0
k := RingZero
r := RegionID(c >> 4)
z := ZoneID(c & 0xf)
n := uint(d)
return k, r, z, n
}
}
+14
View File
@@ -9,7 +9,21 @@ import (
"darvaza.org/core"
)
// Ring ... TODO
type Ring int
const (
// InvalidRing ... TODO
InvalidRing Ring = iota - 1
// RingZero ... TODO
RingZero
// RingOne ... TODO
RingOne
// RingTwo ... TODO
RingTwo
// RingThree ... TODO
RingThree
// RegionMax indicates the highest number that can be used for a [RegionID].
RegionMax = (1 << 4) - 1
// ZoneMax indicates the highest number that can be used for a [ZoneID].