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.

44 lines
822 B

package rings
import "net/netip"
// AddrFromU32 converts a 32bit value into an IPv4
// address.
func AddrFromU32(v uint32) netip.Addr {
return AddrFrom4(
uint(v>>24),
uint(v>>16),
uint(v>>8),
uint(v),
)
}
// AddrFrom4 assembles an IPv4 address for 4 numbers.
// each number is truncated to 8-bits.
func AddrFrom4(a, b, c, d uint) netip.Addr {
return netip.AddrFrom4([4]byte{
byte(a & 0xff),
byte(b & 0xff),
byte(c & 0xff),
byte(d & 0xff),
})
}
// AddrToU32 converts a valid IPv4 address into it's
// 32bit numeric representation.
func AddrToU32(addr netip.Addr) (v uint32, ok bool) {
if addr.IsValid() {
if addr.Is4() || addr.Is4In6() {
a4 := addr.As4()
v = uint32(a4[0])<<24 +
uint32(a4[1])<<16 +
uint32(a4[2])<<8 +
uint32(a4[3])
return v, true
}
}
return 0, false
}