From beb488f6fad992aaa8b5724377d6b500b83f7618 Mon Sep 17 00:00:00 2001 From: Michael Stapelberg Date: Thu, 28 Jun 2018 20:05:27 +0200 Subject: [PATCH] implement expr.Payload unmarshaling --- expr/payload.go | 21 ++++++++++++++++++--- nftables.go | 2 ++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/expr/payload.go b/expr/payload.go index d04b9fa..11727c3 100644 --- a/expr/payload.go +++ b/expr/payload.go @@ -15,8 +15,6 @@ package expr import ( - "fmt" - "github.com/google/nftables/binaryutil" "github.com/mdlayher/netlink" "golang.org/x/sys/unix" @@ -55,5 +53,22 @@ func (e *Payload) marshal() ([]byte, error) { } func (e *Payload) unmarshal(data []byte) error { - return fmt.Errorf("not yet implemented") + attrs, err := netlink.UnmarshalAttributes(data) + if err != nil { + return err + } + for _, attr := range attrs { + switch attr.Type { + case unix.NFTA_PAYLOAD_DREG: + e.DestRegister = binaryutil.BigEndian.Uint32(attr.Data) + case unix.NFTA_PAYLOAD_BASE: + e.Base = PayloadBase(binaryutil.BigEndian.Uint32(attr.Data)) + case unix.NFTA_PAYLOAD_OFFSET: + e.Offset = binaryutil.BigEndian.Uint32(attr.Data) + case unix.NFTA_PAYLOAD_LEN: + e.Len = binaryutil.BigEndian.Uint32(attr.Data) + } + } + + return nil } diff --git a/nftables.go b/nftables.go index b2d07a2..d6f54a9 100644 --- a/nftables.go +++ b/nftables.go @@ -213,6 +213,8 @@ func exprsFromMsg(b []byte) ([]expr.Any, error) { e = &expr.Cmp{} case "counter": e = &expr.Counter{} + case "payload": + e = &expr.Payload{} } if e == nil { // TODO: introduce an opaque expression type so that users know