chore(netlink): separate linux only and OS independent code

- Move `Addr` and its `String` method to `types.go`
- Move `IsWireguardSupported` to `wireguard.go` to have `family.go` OS independant
- Remove dependency on vishvananda/netlink in `ipv6.go`
- Move `Link` to `types.go`
- Move `Route` to `types.go`
- Move `Rule` and its `String` method to `types.go`
This commit is contained in:
Quentin McGaw
2023-05-29 06:55:54 +00:00
parent c26476a2fd
commit d6924597dd
9 changed files with 79 additions and 77 deletions

View File

@@ -1,19 +1,9 @@
package netlink
import (
"net/netip"
"github.com/vishvananda/netlink"
)
type Addr struct {
Network netip.Prefix
}
func (a Addr) String() string {
return a.Network.String()
}
func (n *NetLink) AddrList(link Link, family int) (
addresses []Addr, err error) {
netlinkLink := linkToNetlinkLink(&link)

View File

@@ -2,8 +2,6 @@ package netlink
import (
"fmt"
"github.com/vishvananda/netlink"
)
const (
@@ -24,16 +22,3 @@ func FamilyToString(family int) string {
return fmt.Sprint(family)
}
}
func (n *NetLink) IsWireguardSupported() (ok bool, err error) {
families, err := netlink.GenlFamilyList()
if err != nil {
return false, fmt.Errorf("listing gen 1 families: %w", err)
}
for _, family := range families {
if family.Name == "wireguard" {
return true, nil
}
}
return false, nil
}

View File

@@ -2,8 +2,6 @@ package netlink
import (
"fmt"
"github.com/vishvananda/netlink"
)
func (n *NetLink) IsIPv6Supported() (supported bool, err error) {
@@ -15,7 +13,7 @@ func (n *NetLink) IsIPv6Supported() (supported bool, err error) {
var totalRoutes uint
for _, link := range links {
link := link
routes, err := n.RouteList(&link, netlink.FAMILY_V6)
routes, err := n.RouteList(&link, FamilyV6)
if err != nil {
return false, fmt.Errorf("listing IPv6 routes for link %s: %w",
link.Name, err)

View File

@@ -2,14 +2,6 @@ package netlink
import "github.com/vishvananda/netlink"
type Link struct {
Type string
Name string
Index int
EncapType string
MTU uint16
}
func (n *NetLink) LinkList() (links []Link, err error) {
netlinkLinks, err := netlink.LinkList()
if err != nil {

View File

@@ -1,22 +1,9 @@
package netlink
import (
"net/netip"
"github.com/vishvananda/netlink"
)
type Route struct {
LinkIndex int
Dst netip.Prefix
Src netip.Addr
Gw netip.Addr
Priority int
Family int
Table int
Type int
}
func (n *NetLink) RouteList(link *Link, family int) (
routes []Route, err error) {
netlinkLink := linkToNetlinkLink(link)

View File

@@ -1,37 +1,9 @@
package netlink
import (
"fmt"
"net/netip"
"github.com/vishvananda/netlink"
)
type Rule struct {
Priority int
Family int
Table int
Mark int
Src netip.Prefix
Dst netip.Prefix
Invert bool
}
func (r Rule) String() string {
from := "all"
if r.Src.IsValid() {
from = r.Src.String()
}
to := "all"
if r.Dst.IsValid() {
to = r.Dst.String()
}
return fmt.Sprintf("ip rule %d: from %s to %s table %d",
r.Priority, from, to, r.Table)
}
func NewRule() Rule {
// defaults found from netlink.NewRule() for fields we use,
// the rest of the defaults is set when converting from a `Rule`

58
internal/netlink/types.go Normal file
View File

@@ -0,0 +1,58 @@
package netlink
import (
"fmt"
"net/netip"
)
type Addr struct {
Network netip.Prefix
}
func (a Addr) String() string {
return a.Network.String()
}
type Link struct {
Type string
Name string
Index int
EncapType string
MTU uint16
}
type Route struct {
LinkIndex int
Dst netip.Prefix
Src netip.Addr
Gw netip.Addr
Priority int
Family int
Table int
Type int
}
type Rule struct {
Priority int
Family int
Table int
Mark int
Src netip.Prefix
Dst netip.Prefix
Invert bool
}
func (r Rule) String() string {
from := "all"
if r.Src.IsValid() {
from = r.Src.String()
}
to := "all"
if r.Dst.IsValid() {
to = r.Dst.String()
}
return fmt.Sprintf("ip rule %d: from %s to %s table %d",
r.Priority, from, to, r.Table)
}

View File

@@ -0,0 +1,20 @@
package netlink
import (
"fmt"
"github.com/vishvananda/netlink"
)
func (n *NetLink) IsWireguardSupported() (ok bool, err error) {
families, err := netlink.GenlFamilyList()
if err != nil {
return false, fmt.Errorf("listing gen 1 families: %w", err)
}
for _, family := range families {
if family.Name == "wireguard" {
return true, nil
}
}
return false, nil
}