Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature] Support build tag to disable mocks and refactor code #61

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 5 additions & 9 deletions capture/afpacket/afpacket/afpacket.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ func NewSourceFromLink(link *link.Link, options ...Option) (*Source, error) {
snapLen: DefaultSnapLen,
ipLayerOffset: link.Type.IPHeaderOffset(),
link: link,
Mutex: sync.Mutex{},
}

// Apply functional options, if any
Expand Down Expand Up @@ -226,10 +225,11 @@ retry:

// Stats returns (and clears) the packet counters of the underlying source
func (s *Source) Stats() (capture.Stats, error) {
s.Lock()
defer s.Unlock()

s.Lock()
ss, err := s.eventHandler.GetSocketStats()
s.Unlock()

if err != nil {
return capture.Stats{}, err
}
Expand Down Expand Up @@ -264,11 +264,7 @@ func (s *Source) Close() error {
return err
}

if err := s.eventHandler.Fd.Close(); err != nil {
return err
}

return nil
return s.eventHandler.Fd.Close()
}

func (s *Source) nextPacketInto(data capture.Packet) (int, error) {
Expand Down Expand Up @@ -309,7 +305,7 @@ retry:
}

data[1] = s.ipLayerOffset
*(*uint32)(unsafe.Pointer(&data[2])) = uint32(totalLen)
*(*uint32)(unsafe.Pointer(&data[2])) = uint32(totalLen) // #nosec: G103

return n, nil
}
Expand Down
5 changes: 4 additions & 1 deletion capture/afpacket/afpacket/afpacket_mock.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//go:build !slimcap_nomock
// +build !slimcap_nomock

package afpacket

import (
Expand Down Expand Up @@ -66,7 +69,7 @@ func (m *MockSource) AddPacketFromSource(src capture.Source) error {
}

// NewMockSource instantiates a new mock direct AF_PACKET source, wrapping a regular Source
func NewMockSource(iface string, options ...Option) (*MockSource, error) {
func NewMockSource(_ string, options ...Option) (*MockSource, error) {

mockHandler, mockFd, err := event.NewMockHandler()
if err != nil {
Expand Down
Loading