mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-10-28 03:23:25 +08:00
netutils: isolate linux dependent parts
This commit is contained in:
@@ -24,8 +24,6 @@ import (
|
||||
"strings"
|
||||
"unicode"
|
||||
|
||||
"github.com/vishvananda/netlink"
|
||||
|
||||
"yunion.io/x/jsonutils"
|
||||
"yunion.io/x/log"
|
||||
"yunion.io/x/pkg/errors"
|
||||
@@ -33,7 +31,6 @@ import (
|
||||
"yunion.io/x/pkg/util/regutils"
|
||||
|
||||
"yunion.io/x/onecloud/pkg/cloudcommon/types"
|
||||
"yunion.io/x/onecloud/pkg/util/iproute2"
|
||||
"yunion.io/x/onecloud/pkg/util/procutils"
|
||||
"yunion.io/x/onecloud/pkg/util/regutils2"
|
||||
)
|
||||
@@ -85,38 +82,6 @@ func MyIP() (ip string, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func DefaultSrcIpDev() (srcIp net.IP, ifname string, err error) {
|
||||
destIp := net.ParseIP("114.114.114.114")
|
||||
routes, err := netlink.RouteGet(destIp)
|
||||
if err != nil {
|
||||
err = errors.Wrap(err, "get route")
|
||||
return
|
||||
}
|
||||
if len(routes) == 0 {
|
||||
err = fmt.Errorf("no route")
|
||||
return
|
||||
}
|
||||
var errs []error
|
||||
for i := range routes {
|
||||
route := &routes[i]
|
||||
ip4 := route.Src.To4()
|
||||
if len(ip4) != 4 || ip4.Equal(net.IPv4zero) {
|
||||
errs = append(errs, fmt.Errorf("bad src ipv4 address: %s", ip4))
|
||||
continue
|
||||
}
|
||||
link, err2 := netlink.LinkByIndex(route.LinkIndex)
|
||||
if err2 != nil {
|
||||
errs = append(errs, errors.Wrap(err2, "link by index"))
|
||||
continue
|
||||
}
|
||||
srcIp = ip4
|
||||
ifname = link.Attrs().Name
|
||||
return
|
||||
}
|
||||
err = errors.NewAggregate(errs)
|
||||
return
|
||||
}
|
||||
|
||||
func GetPrivatePrefixes(privatePrefixes []string) []string {
|
||||
if privatePrefixes != nil {
|
||||
return privatePrefixes
|
||||
@@ -427,24 +392,6 @@ func (n *SNetInterface) getRoutes(gwOnly bool, outputs []string) [][]string {
|
||||
return res
|
||||
}
|
||||
|
||||
func (n *SNetInterface) GetAddresses() [][]string {
|
||||
ipnets, err := iproute2.NewAddress(n.name).List4()
|
||||
if err != nil {
|
||||
log.Errorf("list address %s: %v", n.name, err)
|
||||
return nil
|
||||
}
|
||||
r := make([][]string, len(ipnets))
|
||||
for i, ipnet := range ipnets {
|
||||
ip := ipnet.IP
|
||||
masklen, _ := ipnet.Mask.Size()
|
||||
r[i] = []string{
|
||||
ip.String(),
|
||||
fmt.Sprintf("%d", masklen),
|
||||
}
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
func (n *SNetInterface) GetSlaveAddresses() [][]string {
|
||||
addrs := n.GetAddresses()
|
||||
var slaves = make([][]string, 0)
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
package netutils2
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
|
||||
"github.com/vishvananda/netlink"
|
||||
|
||||
"yunion.io/x/log"
|
||||
"yunion.io/x/pkg/errors"
|
||||
|
||||
"yunion.io/x/onecloud/pkg/util/iproute2"
|
||||
)
|
||||
|
||||
func (n *SNetInterface) GetAddresses() [][]string {
|
||||
ipnets, err := iproute2.NewAddress(n.name).List4()
|
||||
if err != nil {
|
||||
log.Errorf("list address %s: %v", n.name, err)
|
||||
return nil
|
||||
}
|
||||
r := make([][]string, len(ipnets))
|
||||
for i, ipnet := range ipnets {
|
||||
ip := ipnet.IP
|
||||
masklen, _ := ipnet.Mask.Size()
|
||||
r[i] = []string{
|
||||
ip.String(),
|
||||
fmt.Sprintf("%d", masklen),
|
||||
}
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
func DefaultSrcIpDev() (srcIp net.IP, ifname string, err error) {
|
||||
destIp := net.ParseIP("114.114.114.114")
|
||||
routes, err := netlink.RouteGet(destIp)
|
||||
if err != nil {
|
||||
err = errors.Wrap(err, "get route")
|
||||
return
|
||||
}
|
||||
if len(routes) == 0 {
|
||||
err = fmt.Errorf("no route")
|
||||
return
|
||||
}
|
||||
var errs []error
|
||||
for i := range routes {
|
||||
route := &routes[i]
|
||||
ip4 := route.Src.To4()
|
||||
if len(ip4) != 4 || ip4.Equal(net.IPv4zero) {
|
||||
errs = append(errs, fmt.Errorf("bad src ipv4 address: %s", ip4))
|
||||
continue
|
||||
}
|
||||
link, err2 := netlink.LinkByIndex(route.LinkIndex)
|
||||
if err2 != nil {
|
||||
errs = append(errs, errors.Wrap(err2, "link by index"))
|
||||
continue
|
||||
}
|
||||
srcIp = ip4
|
||||
ifname = link.Attrs().Name
|
||||
return
|
||||
}
|
||||
err = errors.NewAggregate(errs)
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
// +build !linux
|
||||
|
||||
package netutils2
|
||||
|
||||
func (n *SNetInterface) GetAddresses() [][]string {
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user