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.
77 lines
1.4 KiB
77 lines
1.4 KiB
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 |
|
} |
|
|
|
// PrefixToRange returns the beginning and end of a |
|
// [netip.Prefix] (aka CIDR or subnet). |
|
func PrefixToRange(subnet netip.Prefix) (from, to netip.Addr, ok bool) { |
|
var u uint32 |
|
|
|
addr := subnet.Addr() |
|
if u, ok = AddrToU32(addr); ok { |
|
bits := subnet.Bits() |
|
|
|
switch { |
|
case bits > 32, bits < 0: |
|
// bad |
|
case bits == 32: |
|
// single |
|
from, to, ok = addr, addr, true |
|
default: |
|
// subnet |
|
shift := 32 - bits |
|
|
|
m1 := uint32((1 << shift) - 1) |
|
m0 := uint32(0xffffffff) & ^m1 |
|
|
|
u0 := u & m0 |
|
u1 := u0 + m1 |
|
|
|
ok = true |
|
from = AddrFromU32(u0) |
|
to = AddrFromU32(u1) |
|
} |
|
} |
|
|
|
return from, to, ok |
|
}
|
|
|