Skip to content

Commit

Permalink
event: export Unmarshal method
Browse files Browse the repository at this point in the history
This is useful for callers that bring their own netlink receiver (e.g. rcvmmsg)
but still want to use the unmarshaling infrastructure.

Signed-off-by: Nadia Pinaeva <n.m.pinaeva@gmail.com>
  • Loading branch information
npinaeva authored and ti-mo committed May 17, 2024
1 parent fd4a300 commit 9b3ab11
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func (c *Conn) eventWorker(workerID uint8, evChan chan<- Event, errChan chan<- e

// Decode event and send on channel
ev = *new(Event)
err := ev.unmarshal(recv[0])
err := ev.Unmarshal(recv[0])
if err != nil {
errChan <- err
return
Expand Down
4 changes: 2 additions & 2 deletions event.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ func (et *eventType) unmarshal(h netfilter.Header) error {
return nil
}

// unmarshal unmarshals a Netlink message into an Event structure.
func (e *Event) unmarshal(nlmsg netlink.Message) error {
// Unmarshal unmarshals a Netlink message into an Event structure.
func (e *Event) Unmarshal(nlmsg netlink.Message) error {
// Make sure we don't re-use an Event structure
if e.Expect != nil || e.Flow != nil {
return errReusedEvent
Expand Down
8 changes: 4 additions & 4 deletions event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func TestEventUnmarshal(t *testing.T) {
require.NoError(t, err)

var e Event
assert.NoError(t, e.unmarshal(nlm))
assert.NoError(t, e.Unmarshal(nlm))
assert.Equal(t, tt.e, e, "unexpected unmarshal")
})
}
Expand All @@ -144,17 +144,17 @@ func TestEventUnmarshal(t *testing.T) {
func TestEventUnmarshalError(t *testing.T) {
// Unmarshal into event with existing Flow
eventExistingFlow := Event{Flow: &Flow{}}
assert.ErrorIs(t, eventExistingFlow.unmarshal(netlink.Message{}), errReusedEvent)
assert.ErrorIs(t, eventExistingFlow.Unmarshal(netlink.Message{}), errReusedEvent)

// EventType unmarshal error, blank SubsystemID
var emptyEvent Event
assert.ErrorIs(t, emptyEvent.unmarshal(netlink.Message{
assert.ErrorIs(t, emptyEvent.Unmarshal(netlink.Message{
Header: netlink.Header{},
Data: []byte{1, 2, 3, 4},
}), errNotConntrack)

// Cause a random error during Flow unmarshal
assert.ErrorIs(t, emptyEvent.unmarshal(netlink.Message{
assert.ErrorIs(t, emptyEvent.Unmarshal(netlink.Message{
Header: netlink.Header{Type: netlink.HeaderType(netfilter.NFSubsysCTNetlink) << 8},
Data: []byte{
1, 2, 3, 4, // random 4-byte nfgenmsg
Expand Down

0 comments on commit 9b3ab11

Please sign in to comment.