From 62e3bcc9d6a0066975fee797dad95ce2f28d8abc Mon Sep 17 00:00:00 2001 From: Florian Lehner Date: Sat, 29 Jun 2024 11:26:51 +0200 Subject: [PATCH] add SkipOptional Signed-off-by: Florian Lehner --- diag.go | 13 ++++++++++--- protocols.go | 2 +- types.go | 5 +++++ unix.go | 2 +- 4 files changed, 17 insertions(+), 5 deletions(-) diff --git a/diag.go b/diag.go index cf17494..4a98307 100644 --- a/diag.go +++ b/diag.go @@ -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 @@ -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{}) @@ -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 } @@ -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{}) @@ -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 } diff --git a/protocols.go b/protocols.go index 4fd138a..e323aa6 100644 --- a/protocols.go +++ b/protocols.go @@ -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. diff --git a/types.go b/types.go index 6bbcc3a..bc1f83c 100644 --- a/types.go +++ b/types.go @@ -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 ( diff --git a/unix.go b/unix.go index abb29c9..64c4f7a 100644 --- a/unix.go +++ b/unix.go @@ -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 }