Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rtnetlink: fix Link ListByKind response #233

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func unpackMessages(msgs []netlink.Message) ([]Message, error) {
var m Message
switch nm.Header.Type {
case unix.RTM_GETLINK, unix.RTM_NEWLINK, unix.RTM_DELLINK:
m = &LinkMessage{}
m = &LinkMessage{filtered: (nm.Header.Flags&netlink.DumpFiltered != 0)}
case unix.RTM_GETADDR, unix.RTM_NEWADDR, unix.RTM_DELADDR:
m = &AddressMessage{}
case unix.RTM_GETROUTE, unix.RTM_NEWROUTE, unix.RTM_DELROUTE:
Expand Down
27 changes: 21 additions & 6 deletions link.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ type LinkMessage struct {

// Attributes List
Attributes *LinkAttributes

// A response was filtered as requested, see NLM_F_DUMP_FILTERED
filtered bool
}

// MarshalBinary marshals a LinkMessage into a byte slice.
Expand Down Expand Up @@ -184,14 +187,26 @@ func (l *LinkService) Set(req *LinkMessage) error {

func (l *LinkService) list(kind string) ([]LinkMessage, error) {
req := &LinkMessage{}
if kind != "" {
req.Attributes = &LinkAttributes{
Info: &LinkInfo{Kind: kind},
}
flags := netlink.Request | netlink.Dump

if kind == "" {
return l.execute(req, unix.RTM_GETLINK, flags)
}

flags := netlink.Request | netlink.Dump
return l.execute(req, unix.RTM_GETLINK, flags)
req.Attributes = &LinkAttributes{
Info: &LinkInfo{Kind: kind},
}

msgs, err := l.execute(req, unix.RTM_GETLINK, flags)

// All filtered links are marked by a NLM_F_DUMP_FILTERED flag
// no other links present in a response, so just check the first one
if err == nil && len(msgs) > 0 && !msgs[0].filtered {
msgs = []LinkMessage{}
}

return msgs, err

}

// ListByKind retrieves all interfaces of a specific kind.
Expand Down