package rings import ( "net/netip" "darvaza.org/core" ) // DecodeAddress ... TODO // // revive:disable:function-result-limit func DecodeAddress(addr netip.Addr) (RingID, 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) (RingID, 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 } }