Skip to content

Commit

Permalink
Merge pull request #61 from fako1024/60-provide-slimcap_nomock-build-…
Browse files Browse the repository at this point in the history
…tag-and-allow-feature-toggle

[feature] Support build tag to disable mocks and refactor code
  • Loading branch information
fako1024 authored Sep 3, 2023
2 parents 1a70846 + ae0b217 commit a32220d
Show file tree
Hide file tree
Showing 24 changed files with 1,235 additions and 1,086 deletions.
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

0 comments on commit a32220d

Please sign in to comment.