/
capture.go
785 lines (666 loc) · 21.1 KB
/
capture.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
/////////////////////////////////////////////////////////////////////////////////
//
// capture.go
//
// Written by Lorenz Breidenbach lob@open.ch, December 2015
// Copyright (c) 2015 Open Systems AG, Switzerland
// All Rights Reserved.
//
/////////////////////////////////////////////////////////////////////////////////
package goProbe
import (
"fmt"
"os"
"runtime/debug"
"sync"
"time"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcap"
"OSAG/goDB"
)
const (
CAPTURE_SNAPLEN = 86
CAPTURE_ERROR_THRESHOLD = 10000
// Our experiments show that you don't want to set this value lower
// than roughly 100 ms. Otherwise we flood the kernel with syscalls
// and our performance drops.
CAPTURE_TIMEOUT time.Duration = 500 * time.Millisecond
MIN_PCAP_BUF_SIZE = 1024 // require at least one KiB
MAX_PCAP_BUF_SIZE = 1024 * 1024 * 1024 // 1 GiB should be enough for anyone ;)
)
//////////////////////// Ancillary types ////////////////////////
type CaptureConfig struct {
BufSize int `json:"buf_size"` // in bytes
BPFFilter string `json:"bpf_filter"`
Promisc bool `json:"promisc"`
}
// Validate (partially) checks that the given CaptureConfig contains no bogus settings.
//
// Note that the BPFFilter field isn't checked.
func (cc CaptureConfig) Validate() error {
if !(MIN_PCAP_BUF_SIZE <= cc.BufSize && cc.BufSize <= MAX_PCAP_BUF_SIZE) {
return fmt.Errorf("Invalid configuration entry BufSize. Value must be in range [%d, %d].", MIN_PCAP_BUF_SIZE, MAX_PCAP_BUF_SIZE)
}
return nil
}
type CaptureState byte
const (
CAPTURE_STATE_UNINITIALIZED CaptureState = iota + 1
CAPTURE_STATE_INITIALIZED
CAPTURE_STATE_ACTIVE
CAPTURE_STATE_ERROR
)
func (cs CaptureState) String() string {
switch cs {
case CAPTURE_STATE_UNINITIALIZED:
return "CAPTURE_STATE_UNINITIALIZED"
case CAPTURE_STATE_INITIALIZED:
return "CAPTURE_STATE_INITIALIZED"
case CAPTURE_STATE_ACTIVE:
return "CAPTURE_STATE_ACTIVE"
case CAPTURE_STATE_ERROR:
return "CAPTURE_STATE_ERROR"
default:
return "Unknown"
}
}
type CaptureStats struct {
Pcap *pcap.Stats
PacketsLogged int
}
type CaptureStatus struct {
State CaptureState
Stats CaptureStats
}
type errorMap map[string]int
func (e errorMap) String() string {
var str string
for err, count := range e {
str += fmt.Sprintf(" %s(%d);", err, count)
}
return str
}
//////////////////////// capture commands ////////////////////////
// captureCommand is an interface implemented by (you guessed it...)
// all capture commands. A capture command is sent to the process() of
// a Capture over the Capture's cmdChan. The captureCommand's execute()
// method is then executed by process() (and in process()'s goroutine).
// As a result we don't have to worry about synchronization of the
// Capture's pcap handle inside the execute() methods.
type captureCommand interface {
// executes the command on the provided capture instance.
// This will always be called from the process() goroutine.
execute(c *Capture)
}
type captureCommandStatus struct {
returnChan chan<- CaptureStatus
}
type captureCommandErrors struct {
returnChan chan<- errorMap
}
func (cmd captureCommandStatus) execute(c *Capture) {
var result CaptureStatus
result.State = c.state
pcapStats := c.tryGetPcapStats()
result.Stats = CaptureStats{
Pcap: subPcapStats(pcapStats, c.lastRotationStats.Pcap),
PacketsLogged: c.packetsLogged - c.lastRotationStats.PacketsLogged,
}
cmd.returnChan <- result
}
func (cmd captureCommandErrors) execute(c *Capture) {
cmd.returnChan <- c.errMap
}
type captureCommandUpdate struct {
config CaptureConfig
returnChan chan<- struct{}
}
func (cmd captureCommandUpdate) execute(c *Capture) {
if c.state == CAPTURE_STATE_ACTIVE {
if c.needReinitialization(cmd.config) {
c.deactivate()
} else {
cmd.returnChan <- struct{}{}
return
}
}
// Can no longer be in CAPTURE_STATE_ACTIVE at this point
// Now try to make Capture initialized with new config.
switch c.state {
case CAPTURE_STATE_UNINITIALIZED:
c.config = cmd.config
c.initialize()
case CAPTURE_STATE_INITIALIZED:
if c.needReinitialization(cmd.config) {
c.uninitialize()
c.config = cmd.config
c.initialize()
}
case CAPTURE_STATE_ERROR:
c.recoverError()
c.config = cmd.config
c.initialize()
}
SysLog.Debug(fmt.Sprintf("Interface '%s': (re)initialized for configuration update", c.iface))
// If initialization in last step succeeded, activate
if c.state == CAPTURE_STATE_INITIALIZED {
c.activate()
}
cmd.returnChan <- struct{}{}
}
// helper struct to bundle up the multiple return values
// of Rotate
type rotateResult struct {
agg goDB.AggFlowMap
stats CaptureStats
}
type captureCommandRotate struct {
returnChan chan<- rotateResult
}
func (cmd captureCommandRotate) execute(c *Capture) {
var result rotateResult
result.agg = c.flowLog.Rotate()
pcapStats := c.tryGetPcapStats()
result.stats = CaptureStats{
Pcap: subPcapStats(pcapStats, c.lastRotationStats.Pcap),
PacketsLogged: c.packetsLogged - c.lastRotationStats.PacketsLogged,
}
c.lastRotationStats = CaptureStats{
Pcap: pcapStats,
PacketsLogged: c.packetsLogged,
}
cmd.returnChan <- result
}
type captureCommandEnable struct {
returnChan chan<- struct{}
}
func (cmd captureCommandEnable) execute(c *Capture) {
update := captureCommandUpdate{
c.config,
cmd.returnChan,
}
update.execute(c)
}
type captureCommandDisable struct {
returnChan chan<- struct{}
}
func (cmd captureCommandDisable) execute(c *Capture) {
switch c.state {
case CAPTURE_STATE_UNINITIALIZED:
case CAPTURE_STATE_INITIALIZED:
c.uninitialize()
case CAPTURE_STATE_ACTIVE:
c.deactivate()
c.uninitialize()
case CAPTURE_STATE_ERROR:
c.recoverError()
}
cmd.returnChan <- struct{}{}
}
// BUG(pcap): There is a pcap bug? that causes mysterious panics
// when we try to call Activate on more than one pcap.InactiveHandle
// at the same time.
// We have also observed (much rarer) panics triggered by calls to
// SetBPFFilter on activated pcap handles.
// Hence we use PcapMutex to make sure that
// there can only be on call to Activate and SetBPFFilter at any given
// moment.
// This mutex linearizes all pcap.InactiveHandle.Activate and
// pcap.Handle.SetBPFFilter calls. Don't touch it unless you know what you're
// doing.
var PcapMutex sync.Mutex
//////////////////////// Capture definition ////////////////////////
// A Capture captures and logs flow data for all traffic on a
// given network interface. For each Capture, a goroutine is
// spawned at creation time. To avoid leaking this goroutine,
// be sure to call Close() when you're done with a Capture.
//
// Each Capture is a finite state machine.
// Here is a diagram of the possible state transitions:
//
// +---------------+
// | |
// | |
// | +---------------------+
// | | |
// | UNINITIALIZED <-------------------+ |
// | | recoverError() | |
// +----^-+--------+ | |initialize()
// | | | |fails
// | |initialize() is | |
// | |successful | |
// | | | |
// uninitialize()| | | |
// | | | |
// +---+-v-------+ | |
// | | +---+-v---+
// | | | |
// | | | |
// | | | ERROR |
// | INITIALIZED | | |
// | | +----^----+
// +---^-+-------+ |
// | | |
// | |activate() |
// | | |
// deactivate()| | |
// | | |
// +-+-v----+ |
// | | |
// | +------------------------+
// | | capturePacket()
// | | (called by process())
// | ACTIVE | fails
// | |
// +--------+
//
// Enable() and Update() try to put the capture into the ACTIVE state, Disable() puts the capture
// into the UNINITIALIZED state.
//
// Each capture is associated with a network interface when created. This interface
// can never be changed.
//
// All public methods of Capture are threadsafe.
type Capture struct {
iface string
// synchronizes all access to the Capture's public methods
mutex sync.Mutex
// has Close been called on the Capture?
closed bool
state CaptureState
config CaptureConfig
// channel over which commands are passed to process()
// close(cmdChan) is used to tell process() to stop
cmdChan chan captureCommand
// stats from the last rotation or reset (needed for Status)
lastRotationStats CaptureStats
// Counts the total number of logged packets (since the creation of the
// Capture)
packetsLogged int
// Logged flows since creation of the capture (note that some
// flows are retained even after Rotate has been called)
flowLog *FlowLog
pcapHandle *pcap.Handle
packetSource *gopacket.PacketSource
// error map for logging errors more properly
errMap errorMap
}
// NewCapture creates a new Capture associated with the given iface.
func NewCapture(iface string, config CaptureConfig) *Capture {
c := &Capture{
iface,
sync.Mutex{},
false, // closed
CAPTURE_STATE_UNINITIALIZED,
config,
make(chan captureCommand, 1),
CaptureStats{
Pcap: &pcap.Stats{},
PacketsLogged: 0,
},
0, // packetsLogged
NewFlowLog(),
nil, // pcapHandle
nil, // packetSource
make(map[string]int),
}
go c.process()
return c
}
// setState provides write access to the state field of
// a Capture. It also logs the state change.
func (c *Capture) setState(s CaptureState) {
c.state = s
SysLog.Debug(fmt.Sprintf("Interface '%s': entered capture state %s", c.iface, s))
}
// process is the heart of the Capture. It listens for network traffic on the
// network interface and logs the corresponding flows.
//
// As long as the Capture is in CAPTURE_STATE_ACTIVE process() is capturing
// packets from the network. In any other state, process() only awaits
// further commands.
//
// process keeps running its own goroutine until Close is called on its Capture.
func (c *Capture) process() {
errcount := 0
gppacket := GPPacket{}
capturePacket := func() (err error) {
defer func() {
if r := recover(); r != nil {
trace := string(debug.Stack())
fmt.Fprintf(os.Stderr, "Interface '%s': panic returned %v. Stacktrace:\n%s\n", c.iface, r, trace)
err = fmt.Errorf("Panic during capture")
return
}
}()
packet, err := c.packetSource.NextPacket()
if err != nil {
if err == pcap.NextErrorTimeoutExpired { // CAPTURE_TIMEOUT expired
return nil
} else {
return fmt.Errorf("Capture error: %s", err)
}
}
if err := gppacket.Populate(packet); err == nil {
c.flowLog.Add(&gppacket)
errcount = 0
c.packetsLogged++
} else {
errcount++
// collect the error. The errors value is the key here. Otherwise, the address
// of the error would be taken, which results in a non-minimal set of errors
if _, exists := c.errMap[err.Error()]; !exists {
// log the packet to the pcap error logs
if logerr := PacketLog.Log(c.iface, packet, CAPTURE_SNAPLEN); logerr != nil {
SysLog.Info("failed to log faulty packet: " + logerr.Error())
}
}
c.errMap[err.Error()]++
// shut down the interface thread if too many consecutive decoding failures
// have been encountered
if errcount > CAPTURE_ERROR_THRESHOLD {
return fmt.Errorf("The last %d packets could not be decoded: [%s ]",
CAPTURE_ERROR_THRESHOLD,
c.errMap.String(),
)
}
}
return nil
}
for {
if c.state == CAPTURE_STATE_ACTIVE {
if err := capturePacket(); err != nil {
c.setState(CAPTURE_STATE_ERROR)
SysLog.Err(fmt.Sprintf("Interface '%s': %s", c.iface, err.Error()))
}
select {
case cmd, ok := <-c.cmdChan:
if ok {
cmd.execute(c)
} else {
return
}
default:
// keep going
}
} else {
cmd, ok := <-c.cmdChan
if ok {
cmd.execute(c)
} else {
return
}
}
}
}
//////////////////////// state transisition functions ////////////////////////
// initialize attempts to transition from CAPTURE_STATE_UNINITIALIZED
// into CAPTURE_STATE_INITIALIZED. If an error occurrs, it instead
// transitions into state CAPTURE_STATE_ERROR.
func (c *Capture) initialize() {
initializationErr := func(msg string, args ...interface{}) {
SysLog.Err(fmt.Sprintf(msg, args...))
c.setState(CAPTURE_STATE_ERROR)
return
}
if c.state != CAPTURE_STATE_UNINITIALIZED {
panic("Need state CAPTURE_STATE_UNINITIALIZED")
}
var err error
inactiveHandle, err := setupInactiveHandle(c.iface, c.config.BufSize, c.config.Promisc)
if err != nil {
initializationErr("Interface '%s': failed to create inactive handle: %s", c.iface, err)
return
}
defer inactiveHandle.CleanUp()
PcapMutex.Lock()
c.pcapHandle, err = inactiveHandle.Activate()
PcapMutex.Unlock()
if err != nil {
initializationErr("Interface '%s': failed to activate handle: %s", c.iface, err)
return
}
// link type might be null if the
// specified interface does not exist (anymore)
if c.pcapHandle.LinkType() == layers.LinkTypeNull {
initializationErr("Interface '%s': has link type null", c.iface)
return
}
PcapMutex.Lock()
err = c.pcapHandle.SetBPFFilter(c.config.BPFFilter)
PcapMutex.Unlock()
if err != nil {
initializationErr("Interface '%s': failed to set bpf filter to %s: %s", c.iface, c.config.BPFFilter, err)
return
}
c.packetSource = gopacket.NewPacketSource(c.pcapHandle, c.pcapHandle.LinkType())
// set the decoding options to lazy decoding in order to ensure that the packet
// layers are only decoded once they are needed. Additionally, this is imperative
// when GRE-encapsulated packets are decoded because otherwise the layers cannot
// be detected correctly.
// In addition to lazy decoding, the zeroCopy feature is enabled to avoid allocation
// of a full copy of each gopacket, just to copy over a few elements into a GPPacket
// structure afterwards.
c.packetSource.DecodeOptions = gopacket.DecodeOptions{Lazy: true, NoCopy: true}
c.setState(CAPTURE_STATE_INITIALIZED)
}
// uninitialize moves from CAPTURE_STATE_INITIALIZED to CAPTURE_STATE_UNINITIALIZED.
func (c *Capture) uninitialize() {
if c.state != CAPTURE_STATE_INITIALIZED {
panic("Need state CAPTURE_STATE_INITIALIZED")
}
c.reset()
}
// activate transitions from CAPTURE_STATE_INITIALIZED
// into CAPTURE_STATE_ACTIVE.
func (c *Capture) activate() {
if c.state != CAPTURE_STATE_INITIALIZED {
panic("Need state CAPTURE_STATE_INITIALIZED")
}
c.setState(CAPTURE_STATE_ACTIVE)
SysLog.Debug(fmt.Sprintf("Interface '%s': capture active. Link type: %s", c.iface, c.pcapHandle.LinkType()))
}
// deactivate transitions from CAPTURE_STATE_ACTIVE
// into CAPTURE_STATE_INITIALIZED.
func (c *Capture) deactivate() {
if c.state != CAPTURE_STATE_ACTIVE {
panic("Need state CAPTURE_STATE_ACTIVE")
}
c.setState(CAPTURE_STATE_INITIALIZED)
SysLog.Debug(fmt.Sprintf("Interface '%s': deactivated", c.iface))
}
// recoverError transitions from CAPTURE_STATE_ERROR
// into CAPTURE_STATE_UNINITIALIZED
func (c *Capture) recoverError() {
if c.state != CAPTURE_STATE_ERROR {
panic("Need state CAPTURE_STATE_ERROR")
}
c.reset()
}
//////////////////////// utilities ////////////////////////
// reset unites logic used in both recoverError and uninitialize
// in a single method.
func (c *Capture) reset() {
if c.pcapHandle != nil {
c.pcapHandle.Close()
}
// We reset the Pcap part of the stats because we will create
// a new pcap handle with new counts when the Capture is next
// initialized. We don't reset the PacketsLogged field because
// it corresponds to the number of packets in the (untouched)
// flowLog.
c.lastRotationStats.Pcap = &pcap.Stats{}
c.pcapHandle = nil
c.packetSource = nil
c.setState(CAPTURE_STATE_UNINITIALIZED)
// reset the error map. The GC will take care of the previous
// one
c.errMap = make(map[string]int)
}
// needReinitialization checks whether we need to reinitialize the capture
// to apply the given config.
func (c *Capture) needReinitialization(config CaptureConfig) bool {
return c.config != config
}
func (c *Capture) tryGetPcapStats() *pcap.Stats {
var (
pcapStats *pcap.Stats
err error
)
if c.pcapHandle != nil {
pcapStats, err = c.pcapHandle.Stats()
if err != nil {
SysLog.Err(fmt.Sprintf("Interface '%s': error while requesting pcap stats: %s", err.Error()))
}
}
return pcapStats
}
// subPcapStats computes a - b (fieldwise) if both a and b
// are not nil. Otherwise, it returns nil.
func subPcapStats(a, b *pcap.Stats) *pcap.Stats {
if a == nil || b == nil {
return nil
} else {
return &pcap.Stats{
PacketsReceived: a.PacketsReceived - b.PacketsReceived,
PacketsDropped: a.PacketsDropped - b.PacketsDropped,
PacketsIfDropped: a.PacketsIfDropped - b.PacketsIfDropped,
}
}
}
// setupInactiveHandle sets up a pcap InactiveHandle with the given settings.
func setupInactiveHandle(iface string, bufSize int, promisc bool) (*pcap.InactiveHandle, error) {
// new inactive handle
inactive, err := pcap.NewInactiveHandle(iface)
if err != nil {
inactive.CleanUp()
return nil, err
}
// set up buffer size
if err := inactive.SetBufferSize(bufSize); err != nil {
inactive.CleanUp()
return nil, err
}
// set snaplength
if err := inactive.SetSnapLen(int(CAPTURE_SNAPLEN)); err != nil {
inactive.CleanUp()
return nil, err
}
// set promisc mode
if err := inactive.SetPromisc(promisc); err != nil {
inactive.CleanUp()
return nil, err
}
// set timeout
if err := inactive.SetTimeout(CAPTURE_TIMEOUT); err != nil {
inactive.CleanUp()
return nil, err
}
// return the inactive handle for activation
return inactive, err
}
//////////////////////// public functions ////////////////////////
// Status returns the current CaptureState as well as the statistics
// collected since the last call to Rotate()
//
// Note: If the Capture was reinitialized since the last rotation,
// result.Stats.Pcap will be inaccurate.
//
// Note: result.Stats.Pcap may be null if there was an error fetching the
// stats of the underlying pcap handle.
func (c *Capture) Status() (result CaptureStatus) {
c.mutex.Lock()
defer c.mutex.Unlock()
if c.closed {
panic("Capture is closed")
}
ch := make(chan CaptureStatus, 1)
c.cmdChan <- captureCommandStatus{ch}
return <-ch
}
// Error map status call
func (c *Capture) Errors() (result errorMap) {
c.mutex.Lock()
defer c.mutex.Unlock()
if c.closed {
panic("Capture is closed")
}
ch := make(chan errorMap, 1)
c.cmdChan <- captureCommandErrors{ch}
return <-ch
}
// Update will attempt to put the Capture instance into
// CAPTURE_STATE_ACTIVE with the given config.
// If the Capture is already active with the given config
// Update will detect this and do no work.
func (c *Capture) Update(config CaptureConfig) {
c.mutex.Lock()
defer c.mutex.Unlock()
if c.closed {
panic("Capture is closed")
}
ch := make(chan struct{}, 1)
c.cmdChan <- captureCommandUpdate{config, ch}
<-ch
}
// Enable will attempt to put the Capture instance into
// CAPTURE_STATE_ACTIVE.
// Enable will have no effect if the Capture is already
// in CAPTURE_STATE_ACTIVE.
func (c *Capture) Enable() {
c.mutex.Lock()
defer c.mutex.Unlock()
if c.closed {
panic("Capture is closed")
}
ch := make(chan struct{}, 1)
c.cmdChan <- captureCommandEnable{ch}
<-ch
}
// Disable will bring the Capture instance into CAPTURE_STATE_UNINITIALIZED
// Disable will have no effect if the Capture is already
// in CAPTURE_STATE_UNINITIALIZED.
func (c *Capture) Disable() {
c.mutex.Lock()
defer c.mutex.Unlock()
if c.closed {
panic("Capture is closed")
}
ch := make(chan struct{}, 1)
c.cmdChan <- captureCommandDisable{ch}
<-ch
}
// Rotate performs a rotation of the underlying flow log and
// returns an AggFlowMap with all flows that have been collected
// since the last call to Rotate(). It also returns capture statistics
// collected since the last call to Rotate().
//
// Note: stats.Pcap may be null if there was an error fetching the
// stats of the underlying pcap handle.
func (c *Capture) Rotate() (agg goDB.AggFlowMap, stats CaptureStats) {
c.mutex.Lock()
defer c.mutex.Unlock()
if c.closed {
panic("Capture is closed")
}
ch := make(chan rotateResult, 1)
c.cmdChan <- captureCommandRotate{ch}
result := <-ch
return result.agg, result.stats
}
// Close closes the Capture and releases all underlying resources.
// Close is idempotent. Once you have closed a Capture, you can no
// longer call any of its methods (apart from Close).
func (c *Capture) Close() {
c.mutex.Lock()
defer c.mutex.Unlock()
if c.closed {
return
}
ch := make(chan struct{}, 1)
c.cmdChan <- captureCommandDisable{ch}
<-ch
close(c.cmdChan)
c.closed = true
}