Skip to content

Commit

Permalink
Link: Fix LinkByName with long (>15 char) name by using altnames
Browse files Browse the repository at this point in the history
  • Loading branch information
steiler committed Mar 1, 2024
1 parent 124bb8e commit 8fd1b63
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 40 deletions.
3 changes: 3 additions & 0 deletions link_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -1867,6 +1867,9 @@ func (h *Handle) LinkByName(name string) (Link, error) {
req.AddData(attr)

nameData := nl.NewRtAttr(unix.IFLA_IFNAME, nl.ZeroTerminated(name))
if len(name) > 15 {
nameData = nl.NewRtAttr(unix.IFLA_ALT_IFNAME, nl.ZeroTerminated(name))
}
req.AddData(nameData)

link, err := execGetLink(req)
Expand Down
74 changes: 34 additions & 40 deletions link_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"net"
"os"
"os/exec"
"sort"
"strings"
"syscall"
"testing"
"time"
Expand Down Expand Up @@ -1899,64 +1901,56 @@ func TestLinkAltName(t *testing.T) {
t.Fatal(err)
}

err = LinkAddAltName(link, "altname")
if err != nil {
t.Fatalf("Could not add altname: %v", err)
}
altNames := []string{"altname", "altname2", "some_longer_altname"}
sort.Strings(altNames)
altNamesStr := strings.Join(altNames, ",")

err = LinkAddAltName(link, "altname2")
if err != nil {
t.Fatalf("Could not add altname: %v", err)
for _, altname := range altNames {
err = LinkAddAltName(link, altname)
if err != nil {
t.Fatalf("Could not add %s: %v", altname, err)
}
}

link, err = LinkByName("bar")
if err != nil {
t.Fatal(err)
}

altNameExist := false
altName2Exist := false
for _, altName := range link.Attrs().AltNames {
if altName == "altname" {
altNameExist = true
} else if altName == "altname2" {
altName2Exist = true
}
sort.Strings(link.Attrs().AltNames)
linkAltNamesStr := strings.Join(link.Attrs().AltNames, ",")

}
if !altNameExist {
t.Fatal("Could not find altname")
if altNamesStr != linkAltNamesStr {
t.Fatalf("Expected %s AltNames, got %s", altNamesStr, linkAltNamesStr)
}

if !altName2Exist {
t.Fatal("Could not find altname2")
for _, altname := range altNames {
link, err = LinkByName(altname)
if err != nil {
t.Fatal(err)
}
}

link, err = LinkByName("altname")
if err != nil {
t.Fatal(err)
}
for idx, altName := range altNames {
err = LinkDelAltName(link, altName)
if err != nil {
t.Fatalf("Could not delete %s: %v", altName, err)
}

err = LinkDelAltName(link, "altname")
if err != nil {
t.Fatalf("Could not delete altname: %v", err)
}
link, err = LinkByName("bar")
if err != nil {
t.Fatal(err)
}

link, err = LinkByName("bar")
if err != nil {
t.Fatal(err)
}
sort.Strings(link.Attrs().AltNames)
linkAltNamesStr := strings.Join(link.Attrs().AltNames, ",")
altNamesStr := strings.Join(altNames[idx+1:], ",")

altNameExist = false
for _, altName := range link.Attrs().AltNames {
if altName == "altname" {
altNameExist = true
break
if linkAltNamesStr != altNamesStr {
t.Fatalf("Expected %s AltNames, got %s", altNamesStr, linkAltNamesStr)
}
}
if altNameExist {
t.Fatal("altname still exist")
}

}

func TestLinkSetARP(t *testing.T) {
Expand Down

0 comments on commit 8fd1b63

Please sign in to comment.