/
pcap.go
56 lines (45 loc) 路 1.08 KB
/
pcap.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package strategies
import (
"log"
"github.com/google/gopacket/pcap"
"github.com/netmoth/netmoth/internal/config"
)
// PCAPStrategy is ...
type PCAPStrategy struct {
handle *pcap.Handle
}
// New is ...
func (s *PCAPStrategy) New(c *config.Config) ([]PacketDataSource, error) {
if c.NumberOfRings > 1 {
log.Println("WARNING: pcap not support cluster mode, ignoring number of rings parameter")
}
var err error
s.handle, err = pcap.OpenLive(c.Interface, int32(c.SnapLen), c.Promiscuous, pcap.BlockForever)
if err != nil {
return nil, err
}
if len(c.Bpf) != 0 {
if err = s.handle.SetBPFFilter(c.Bpf); err != nil {
return nil, err
}
}
return []PacketDataSource{s.handle}, nil
}
// Destroy is ...
func (s *PCAPStrategy) Destroy() {
s.handle.Close()
}
// PacketStats is ...
func (s *PCAPStrategy) PacketStats() (received uint64, dropped uint64) {
stats, err := s.handle.Stats()
if err != nil {
return
}
if stats.PacketsReceived > 0 {
received += uint64(stats.PacketsReceived)
}
if stats.PacketsDropped > 0 {
dropped += uint64(stats.PacketsDropped)
}
return
}