1
0
Fork 0
mirror of https://github.com/anyproto/anytype-heart.git synced 2025-06-08 05:47:07 +09:00

support wlp0s20f3 like iface naming style

This commit is contained in:
Roman Khafizianov 2023-04-25 18:17:59 +02:00 committed by Mikhail Iudin
parent 54af31a2af
commit 17b6c7289c
No known key found for this signature in database
GPG key ID: FAAAA8BAABDFF1C0
2 changed files with 66 additions and 8 deletions

View file

@ -37,24 +37,43 @@ func (i InterfacesAddrs) Equal(other InterfacesAddrs) bool {
return slices.Equal(myStr, otherStr)
}
var re = regexp.MustCompile(`^(.*?)([0-9]*)$`)
func (i InterfacesAddrs) SortWithPriority(priority []string) {
f := func(ifName string) (prefix string, num int) {
res := re.FindStringSubmatch(ifName)
var (
ifaceRe = regexp.MustCompile(`^([a-z]*?)([0-9]+)$`)
// ifaceReBusSlot used for prefixBusSlot naming schema used in newer linux distros https://cgit.freedesktop.org/systemd/systemd/tree/src/udev/udev-builtin-net_id.c#n20
ifaceReBusSlot = regexp.MustCompile(`^([a-z]*?)p([0-9]+)s([0-9a-f]+)$`)
)
func parseInterfaceName(name string) (prefix string, bus int, num int64) {
// try new-style naming schema first (enp0s3, wlp2s0, ...)
res := ifaceReBusSlot.FindStringSubmatch(name)
if len(res) > 0 {
if len(res) > 1 {
prefix = res[1]
}
if len(res) > 2 {
num, _ = strconv.Atoi(res[2])
bus, _ = strconv.Atoi(res[2])
}
if len(res) > 3 {
numHex := res[3]
num, _ = strconv.ParseInt(numHex, 16, 32)
}
return
}
// try old-style naming schema (eth0, wlan0, ...)
res = ifaceRe.FindStringSubmatch(name)
if len(res) > 1 {
prefix = res[1]
}
if len(res) > 2 {
num, _ = strconv.ParseInt(res[2], 10, 32)
}
return
}
func (i InterfacesAddrs) SortWithPriority(priority []string) {
slices.SortFunc(i.Interfaces, func(a, b net.Interface) bool {
aPrefix, aNum := f(a.Name)
bPrefix, bNum := f(b.Name)
aPrefix, aBus, aNum := parseInterfaceName(a.Name)
bPrefix, bBus, bNum := parseInterfaceName(b.Name)
aPrioirity := slice.FindPos(priority, aPrefix)
bPrioirity := slice.FindPos(priority, bPrefix)
@ -71,6 +90,9 @@ func (i InterfacesAddrs) SortWithPriority(priority []string) {
return aPrioirity < bPrioirity
}
// prioritise wlan1 over eth8
if aBus != bBus {
return aBus < bBus
}
return aNum < bNum
} else if aPrioirity != -1 {
return true

36
net/addrs/common_test.go Normal file
View file

@ -0,0 +1,36 @@
package addrs
import "testing"
func Test_parseInterfaceName(t *testing.T) {
type args struct {
name string
}
tests := []struct {
name string
args args
wantPrefix string
wantBus int
wantNum int64
}{
{"eth0", args{"eth0"}, "eth", 0, 0},
{"eth1", args{"eth1"}, "eth", 0, 1},
{"eth10", args{"eth10"}, "eth", 0, 10},
{"enp0s10", args{"enp0s10"}, "en", 0, 16},
{"wlp0s20f3", args{"wlp0s20f3"}, "wl", 0, 8435},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotPrefix, gotBus, gotNum := parseInterfaceName(tt.args.name)
if gotPrefix != tt.wantPrefix {
t.Errorf("parseInterfaceName() gotPrefix = %v, want %v", gotPrefix, tt.wantPrefix)
}
if gotBus != tt.wantBus {
t.Errorf("parseInterfaceName() gotBus = %v, want %v", gotBus, tt.wantBus)
}
if gotNum != tt.wantNum {
t.Errorf("parseInterfaceName() gotNum = %v, want %v", gotNum, tt.wantNum)
}
})
}
}