Skip to content

Commit

Permalink
add SkipOptional
Browse files Browse the repository at this point in the history
Signed-off-by: Florian Lehner <[email protected]>
  • Loading branch information
florianl committed Jun 29, 2024
1 parent 4d3c934 commit 62e3bcc
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 5 deletions.
13 changes: 10 additions & 3 deletions diag.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ var _ diagConn = &netlink.Conn{}

// Diag represents a netlink wrapper
type Diag struct {
con diagConn
con diagConn
skipOptional bool
}

// Open establishes a netlink socket for traffic control
Expand Down Expand Up @@ -188,7 +189,7 @@ func (d *Diag) dumpQuery(header interface{}) ([]netlink.Message, error) {
return d.query(req)
}

func handleNetResponse(msgs []netlink.Message) ([]NetObject, error) {
func handleNetResponse(msgs []netlink.Message, skipOptional bool) ([]NetObject, error) {
var results []NetObject
sizeOfRecvMsg := binary.Size(DiagMsg{})

Expand All @@ -197,6 +198,9 @@ func handleNetResponse(msgs []netlink.Message) ([]NetObject, error) {
if err := unmarshalStruct(msg.Data[:sizeOfRecvMsg], &result.DiagMsg); err != nil {
return nil, err
}
if skipOptional {
continue
}
if err := extractAttributes(msg.Data[sizeOfRecvMsg:], &result.NetAttribute); err != nil {
return nil, err
}
Expand All @@ -205,7 +209,7 @@ func handleNetResponse(msgs []netlink.Message) ([]NetObject, error) {
return results, nil
}

func handleUnixResponse(msgs []netlink.Message) ([]UnixObject, error) {
func handleUnixResponse(msgs []netlink.Message, skipOptional bool) ([]UnixObject, error) {
var results []UnixObject
sizeOfRecvMsg := binary.Size(UnixDiagMsg{})

Expand All @@ -214,6 +218,9 @@ func handleUnixResponse(msgs []netlink.Message) ([]UnixObject, error) {
if err := unmarshalStruct(msg.Data[:sizeOfRecvMsg], &result.UnixDiagMsg); err != nil {
return nil, err
}
if skipOptional {
continue
}
if err := extractUnixAttributes(msg.Data[sizeOfRecvMsg:], &result.UnixAttribute); err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion protocols.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func (d *Diag) NetDump(opt *NetOption) ([]NetObject, error) {
if err != nil {
return nil, err
}
return handleNetResponse(respMsgs)
return handleNetResponse(respMsgs, d.skipOptional)
}

// Dump returns all TCP connections.
Expand Down
5 changes: 5 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ import (
type Config struct {
// NetNS defines the network namespace
NetNS int

// SkipOptional allows to skip decoding of
// optional values likes NetAttribute and
// UnixAttribute.
SkipOptional bool
}

const (
Expand Down
2 changes: 1 addition & 1 deletion unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (d *Diag) UnixDump(opt *UnixOption) ([]UnixObject, error) {
if err != nil {
return nil, err
}
objs, err := handleUnixResponse(respMsgs)
objs, err := handleUnixResponse(respMsgs, d.skipOptional)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 62e3bcc

Please sign in to comment.