diff --git a/helper.go b/helper.go index 03e3234..8bc11d8 100644 --- a/helper.go +++ b/helper.go @@ -54,3 +54,11 @@ func endianSwapUint32(in uint32) uint32 { ((in & 0x00FF0000) >> 8) | ((in & 0xFF000000) >> 24) } + +// bytesToInt32 converts an array for four bytes in big endian format to an integer. +func bytesToInt32(b []byte) int32 { + if len(b) != 4 { + return 0 + } + return int32(b[0])<<24 | int32(b[1])<<16 | int32(b[2])<<8 | int32(b[3]) +} diff --git a/helper_test.go b/helper_test.go index 845ac0e..35998ac 100644 --- a/helper_test.go +++ b/helper_test.go @@ -140,3 +140,21 @@ func TestEndianSwapUint32(t *testing.T) { } } } + +func TestBytesToInt32(t *testing.T) { + tests := []struct { + in []byte + out int32 + }{ + {in: []byte{0x1}, out: 0}, + {in: []byte{0x0, 0x0, 0x0, 0x2a}, out: 42}, + } + + for _, test := range tests { + test := test + out := bytesToInt32(test.in) + if test.out != out { + t.Fatalf("%d != %d", test.out, out) + } + } +} diff --git a/tc.go b/tc.go index 017bdb9..b6ddcb2 100644 --- a/tc.go +++ b/tc.go @@ -3,7 +3,6 @@ package tc import ( "context" "fmt" - "log" "time" "github.com/florianl/go-tc/internal/unix" @@ -27,8 +26,6 @@ var _ tcConn = &netlink.Conn{} // Tc represents a RTNETLINK wrapper type Tc struct { con tcConn - - logger *log.Logger } var nativeEndian = native.Endian @@ -47,12 +44,6 @@ func Open(config *Config) (*Tc, error) { } tc.con = con - if config.Logger == nil { - tc.logger = setDummyLogger() - } else { - tc.logger = config.Logger - } - return &tc, nil } @@ -107,9 +98,15 @@ func (tc *Tc) action(action int, flags netlink.HeaderFlags, msg interface{}, opt } for _, msg := range msgs { - if msg.Header.Type == netlink.Error { - // see https://www.infradead.org/~tgr/libnl/doc/core.html#core_errmsg - tc.logger.Printf("received netlink.Error in action()\n") + switch msg.Header.Type { + case netlink.Error: + errCode := bytesToInt32(msg.Data[:4]) + // Check if the sucess message is embeded encoded as error code 0: + if errCode != 0 { + return fmt.Errorf("received error from netlink: %#v", msg) + } + case netlink.Overrun: + return fmt.Errorf("lost netlink data: %#v", msg) } } @@ -294,7 +291,6 @@ func (tc *Tc) Monitor(ctx context.Context, deadline time.Duration, fn HookFunc) return 0 } } - tc.logger.Printf("Could not receive message: %v\n", err) return 1 }) } @@ -362,12 +358,10 @@ func (tc *Tc) monitor(ctx context.Context, deadline time.Duration, for _, msg := range msgs { var monitored Object if err := unmarshalStruct(msg.Data[:20], &monitored.Msg); err != nil { - tc.logger.Printf("could not extract tc.Msg from %v\n", msg.Data[:20]) continue } if err := extractTcmsgAttributes(int(msg.Header.Type), msg.Data[20:], &monitored.Attribute); err != nil { - tc.logger.Printf("could not extract attributes from %v\n", msg.Data[20:36]) continue } if fn(uint16(msg.Header.Type), monitored) != 0 { diff --git a/tc_gteq_1.16.go b/tc_gteq_1.16.go deleted file mode 100644 index 73553f6..0000000 --- a/tc_gteq_1.16.go +++ /dev/null @@ -1,13 +0,0 @@ -//go:build go1.16 -// +build go1.16 - -package tc - -import ( - "io/ioutil" - "log" -) - -func setDummyLogger() *log.Logger { - return log.New(ioutil.Discard, "", 0) -} diff --git a/tc_linux_integration_test.go b/tc_linux_integration_test.go index b4198f1..85d9d94 100644 --- a/tc_linux_integration_test.go +++ b/tc_linux_integration_test.go @@ -6,7 +6,6 @@ package tc import ( "context" "fmt" - "log" "net" "os" "testing" @@ -161,17 +160,6 @@ func TestSocket(t *testing.T) { t.Fatalf("failed to close test socket: %v", err) } }) - t.Run("with logger", func(t *testing.T) { - tc, err := Open(&Config{ - Logger: log.Default(), - }) - if err != nil { - t.Fatalf("failed to open netlink socket: %v", err) - } - if err = tc.Close(); err != nil { - t.Fatalf("failed to close test socket: %v", err) - } - }) } func TestMonitorWithErrorFunc(t *testing.T) { diff --git a/tc_lt_1.16.go b/tc_lt_1.16.go deleted file mode 100644 index 46bf468..0000000 --- a/tc_lt_1.16.go +++ /dev/null @@ -1,17 +0,0 @@ -//go:build !go1.16 -// +build !go1.16 - -package tc - -import "log" - -// devNull satisfies io.Writer, in case *log.Logger is not provided -type devNull struct{} - -func (devNull) Write(p []byte) (int, error) { - return 0, nil -} - -func setDummyLogger() *log.Logger { - return log.New(new(devNull), "", 0) -}