Skip to content

Commit

Permalink
Allow link argument to AddrAdd, AddReplace and AddrDel to be nil
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Worm Mortensen committed Mar 4, 2024
1 parent 5e915e0 commit 674ec21
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 49 deletions.
18 changes: 10 additions & 8 deletions addr_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,19 @@ func (h *Handle) AddrDel(link Link, addr *Addr) error {
}

func (h *Handle) addrHandle(link Link, addr *Addr, req *nl.NetlinkRequest) error {
base := link.Attrs()
if addr.Label != "" && !strings.HasPrefix(addr.Label, base.Name) {
return fmt.Errorf("label must begin with interface name")
}
h.ensureIndex(base)

family := nl.GetIPFamily(addr.IP)

msg := nl.NewIfAddrmsg(family)
msg.Index = uint32(base.Index)
msg.Scope = uint8(addr.Scope)
if link == nil {
msg.Index = uint32(addr.LinkIndex)
} else {
base := link.Attrs()
if addr.Label != "" && !strings.HasPrefix(addr.Label, base.Name) {
return fmt.Errorf("label must begin with interface name")
}
h.ensureIndex(base)
msg.Index = uint32(base.Index)
}
mask := addr.Mask
if addr.Peer != nil {
mask = addr.Peer.Mask
Expand Down
89 changes: 48 additions & 41 deletions addr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,58 +145,65 @@ func TestAddrAddReplace(t *testing.T) {
tearDown := setUpNetlinkTest(t)
defer tearDown()

var address = &net.IPNet{IP: net.IPv4(127, 0, 0, 2), Mask: net.CIDRMask(24, 32)}
var addr = &Addr{IPNet: address}
for _, nilLink := range []bool{false, true} {
var address = &net.IPNet{IP: net.IPv4(127, 0, 0, 2), Mask: net.CIDRMask(24, 32)}
var addr = &Addr{IPNet: address}

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

err = AddrAdd(link, addr)
if err != nil {
t.Fatal(err)
}
if nilLink {
addr.LinkIndex = link.Attrs().Index
link = nil
}

addrs, err := AddrList(link, FAMILY_ALL)
if err != nil {
t.Fatal(err)
}
err = AddrAdd(link, addr)
if err != nil {
t.Fatal(err)
}

if len(addrs) != 1 {
t.Fatal("Address not added properly")
}
addrs, err := AddrList(link, FAMILY_ALL)
if err != nil {
t.Fatal(err)
}

err = AddrAdd(link, addr)
if err == nil {
t.Fatal("Re-adding address should fail (but succeeded unexpectedly).")
}
if len(addrs) != 1 {
t.Fatal("Address not added properly")
}

err = AddrReplace(link, addr)
if err != nil {
t.Fatal("Replacing address failed.")
}
err = AddrAdd(link, addr)
if err == nil {
t.Fatal("Re-adding address should fail (but succeeded unexpectedly).")
}

addrs, err = AddrList(link, FAMILY_ALL)
if err != nil {
t.Fatal(err)
}
err = AddrReplace(link, addr)
if err != nil {
t.Fatal("Replacing address failed.")
}

if len(addrs) != 1 {
t.Fatal("Address not added properly")
}
addrs, err = AddrList(link, FAMILY_ALL)
if err != nil {
t.Fatal(err)
}

if err = AddrDel(link, addr); err != nil {
t.Fatal(err)
}
if len(addrs) != 1 {
t.Fatal("Address not added properly")
}

addrs, err = AddrList(link, FAMILY_ALL)
if err != nil {
t.Fatal(err)
}
if err = AddrDel(link, addr); err != nil {
t.Fatal(err)
}

addrs, err = AddrList(link, FAMILY_ALL)
if err != nil {
t.Fatal(err)
}

if len(addrs) != 0 {
t.Fatal("Address not removed properly")
if len(addrs) != 0 {
t.Fatal("Address not removed properly")
}
}
}

Expand Down

0 comments on commit 674ec21

Please sign in to comment.