2022-08-18 18:22:15 +02:00
|
|
|
package route
|
|
|
|
|
|
|
|
import (
|
2022-11-11 20:36:45 +01:00
|
|
|
"github.com/netbirdio/netbird/management/server/status"
|
2022-08-18 18:22:15 +02:00
|
|
|
"net/netip"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Windows has some limitation regarding metric size that differ from Unix-like systems.
|
|
|
|
// Because of that we are limiting the min and max metric size based on Windows limits:
|
|
|
|
// see based on info from https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/route_ws2008
|
|
|
|
const (
|
|
|
|
// MinMetric max metric input
|
|
|
|
MinMetric = 1
|
|
|
|
// MaxMetric max metric input
|
|
|
|
MaxMetric = 9999
|
2022-08-22 14:10:24 +02:00
|
|
|
// MaxNetIDChar Max Network Identifier
|
|
|
|
MaxNetIDChar = 40
|
2022-08-18 18:22:15 +02:00
|
|
|
)
|
2022-08-22 14:10:24 +02:00
|
|
|
|
2022-08-18 18:22:15 +02:00
|
|
|
const (
|
2022-08-22 14:10:24 +02:00
|
|
|
// InvalidNetworkString invalid network type string
|
|
|
|
InvalidNetworkString = "Invalid"
|
|
|
|
// IPv4NetworkString IPv4 network type string
|
|
|
|
IPv4NetworkString = "IPv4"
|
|
|
|
// IPv6NetworkString IPv6 network type string
|
|
|
|
IPv6NetworkString = "IPv6"
|
2022-08-18 18:22:15 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2022-08-22 14:10:24 +02:00
|
|
|
// InvalidNetwork invalid network type
|
|
|
|
InvalidNetwork NetworkType = iota
|
|
|
|
// IPv4Network IPv4 network type
|
|
|
|
IPv4Network
|
|
|
|
// IPv6Network IPv6 network type
|
|
|
|
IPv6Network
|
2022-08-18 18:22:15 +02:00
|
|
|
)
|
|
|
|
|
2022-08-22 14:10:24 +02:00
|
|
|
// NetworkType route network type
|
|
|
|
type NetworkType int
|
2022-08-18 18:22:15 +02:00
|
|
|
|
|
|
|
// String returns prefix type string
|
2022-08-22 14:10:24 +02:00
|
|
|
func (p NetworkType) String() string {
|
2022-08-18 18:22:15 +02:00
|
|
|
switch p {
|
2022-08-22 14:10:24 +02:00
|
|
|
case IPv4Network:
|
|
|
|
return IPv4NetworkString
|
|
|
|
case IPv6Network:
|
|
|
|
return IPv6NetworkString
|
2022-08-18 18:22:15 +02:00
|
|
|
default:
|
2022-08-22 14:10:24 +02:00
|
|
|
return InvalidNetworkString
|
2022-08-18 18:22:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToPrefixType returns a prefix type
|
2022-08-22 14:10:24 +02:00
|
|
|
func ToPrefixType(prefix string) NetworkType {
|
2022-08-18 18:22:15 +02:00
|
|
|
switch prefix {
|
2022-08-22 14:10:24 +02:00
|
|
|
case IPv4NetworkString:
|
|
|
|
return IPv4Network
|
|
|
|
case IPv6NetworkString:
|
|
|
|
return IPv6Network
|
2022-08-18 18:22:15 +02:00
|
|
|
default:
|
2022-08-22 14:10:24 +02:00
|
|
|
return InvalidNetwork
|
2022-08-18 18:22:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Route represents a route
|
|
|
|
type Route struct {
|
|
|
|
ID string
|
2022-08-22 14:10:24 +02:00
|
|
|
Network netip.Prefix
|
|
|
|
NetID string
|
2022-08-18 18:22:15 +02:00
|
|
|
Description string
|
|
|
|
Peer string
|
2022-08-22 14:10:24 +02:00
|
|
|
NetworkType NetworkType
|
2022-08-18 18:22:15 +02:00
|
|
|
Masquerade bool
|
|
|
|
Metric int
|
|
|
|
Enabled bool
|
2022-12-06 10:11:57 +01:00
|
|
|
Groups []string
|
2022-08-18 18:22:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Copy copies a route object
|
|
|
|
func (r *Route) Copy() *Route {
|
|
|
|
return &Route{
|
|
|
|
ID: r.ID,
|
|
|
|
Description: r.Description,
|
2022-08-22 14:10:24 +02:00
|
|
|
NetID: r.NetID,
|
|
|
|
Network: r.Network,
|
|
|
|
NetworkType: r.NetworkType,
|
2022-08-18 18:22:15 +02:00
|
|
|
Peer: r.Peer,
|
|
|
|
Metric: r.Metric,
|
|
|
|
Masquerade: r.Masquerade,
|
|
|
|
Enabled: r.Enabled,
|
2022-12-06 10:11:57 +01:00
|
|
|
Groups: r.Groups,
|
2022-08-18 18:22:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsEqual compares one route with the other
|
|
|
|
func (r *Route) IsEqual(other *Route) bool {
|
|
|
|
return other.ID == r.ID &&
|
|
|
|
other.Description == r.Description &&
|
2022-08-22 14:10:24 +02:00
|
|
|
other.NetID == r.NetID &&
|
|
|
|
other.Network == r.Network &&
|
|
|
|
other.NetworkType == r.NetworkType &&
|
2022-08-18 18:22:15 +02:00
|
|
|
other.Peer == r.Peer &&
|
|
|
|
other.Metric == r.Metric &&
|
|
|
|
other.Masquerade == r.Masquerade &&
|
2022-12-06 10:11:57 +01:00
|
|
|
other.Enabled == r.Enabled &&
|
|
|
|
compareGroupsList(r.Groups, other.Groups)
|
2022-08-18 18:22:15 +02:00
|
|
|
}
|
|
|
|
|
2022-08-22 14:10:24 +02:00
|
|
|
// ParseNetwork Parses a network prefix string and returns a netip.Prefix object and if is invalid, IPv4 or IPv6
|
|
|
|
func ParseNetwork(networkString string) (NetworkType, netip.Prefix, error) {
|
|
|
|
prefix, err := netip.ParsePrefix(networkString)
|
2022-08-18 18:22:15 +02:00
|
|
|
if err != nil {
|
2022-11-11 20:36:45 +01:00
|
|
|
return InvalidNetwork, netip.Prefix{}, status.Errorf(status.InvalidArgument, "invalid network %s", networkString)
|
2022-08-18 18:22:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
masked := prefix.Masked()
|
|
|
|
|
|
|
|
if !masked.IsValid() {
|
2022-11-11 20:36:45 +01:00
|
|
|
return InvalidNetwork, netip.Prefix{}, status.Errorf(status.InvalidArgument, "invalid range %s", networkString)
|
2022-08-18 18:22:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if masked.Addr().Is6() {
|
2022-08-22 14:10:24 +02:00
|
|
|
return IPv6Network, masked, nil
|
2022-08-18 18:22:15 +02:00
|
|
|
}
|
|
|
|
|
2022-08-22 14:10:24 +02:00
|
|
|
return IPv4Network, masked, nil
|
2022-08-18 18:22:15 +02:00
|
|
|
}
|
2022-12-06 10:11:57 +01:00
|
|
|
|
|
|
|
func compareGroupsList(list, other []string) bool {
|
|
|
|
if len(list) != len(other) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for _, id := range list {
|
|
|
|
match := false
|
|
|
|
for _, otherID := range other {
|
|
|
|
if id == otherID {
|
|
|
|
match = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !match {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|