mirror of
https://github.com/netbirdio/netbird.git
synced 2024-12-04 22:10:56 +01:00
30 lines
571 B
Go
30 lines
571 B
Go
|
package util
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"net"
|
||
|
"net/netip"
|
||
|
)
|
||
|
|
||
|
// GetPrefixFromIP returns a netip.Prefix from a net.IP address.
|
||
|
func GetPrefixFromIP(ip net.IP) (netip.Prefix, error) {
|
||
|
addr, ok := netip.AddrFromSlice(ip)
|
||
|
if !ok {
|
||
|
return netip.Prefix{}, fmt.Errorf("parse IP address: %s", ip)
|
||
|
}
|
||
|
addr = addr.Unmap()
|
||
|
|
||
|
var prefixLength int
|
||
|
switch {
|
||
|
case addr.Is4():
|
||
|
prefixLength = 32
|
||
|
case addr.Is6():
|
||
|
prefixLength = 128
|
||
|
default:
|
||
|
return netip.Prefix{}, fmt.Errorf("invalid IP address: %s", addr)
|
||
|
}
|
||
|
|
||
|
prefix := netip.PrefixFrom(addr, prefixLength)
|
||
|
return prefix, nil
|
||
|
}
|