-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathextcap_nif.go
96 lines (82 loc) · 3.42 KB
/
extcap_nif.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
// (c) Siemens AG 2023
//
// SPDX-License-Identifier: MIT
// Provides a general handling infrastructure for external capture network
// interfaces: registration, lookup, and extcap actions interface.
package cshargextcap
import (
"io"
"github.com/thediveo/go-plugger/v3"
"golang.org/x/exp/maps"
)
// ExtcapNifActions represents the actions that can be carried out on external
// capture network interfaces. These actions reflect the actions Wireshark
// signals to external capture plugins when invoking them. The only additional
// "internal" action here is returning the name of a specific external capture
// interface, which is used for looking up --ext-interface arguments.
type ExtcapNifActions interface {
Name() string // name of extcap network interface
Description() string // short description
DLTs() []ExtcapNifDLT // data-link layer encapsulation data type
Configure(w io.Writer) int // dump configuration options
ReloadOption(w io.Writer) // dump up-to-date value(s) of specific configuration options
Capture() int // start packet capture
}
// ExtcapNif represents an individual, named external capture network
// interface.
type ExtcapNif struct {
name string // name of external capture network interface
description string // short description of this extcap network interface
}
func NewExtcapNif(name string, description string) ExtcapNif {
return ExtcapNif{name: name, description: description}
}
// ExtcapNifDLT describes a single DLT in terms of its number and
// description.
type ExtcapNifDLT struct {
Number int // DLT number
Display string // display string for this DLT
}
// Name returns the (guess what!) name of an external capture network
// interface. Surprise, surprise. Go for petty GoDoc rules.
func (e *ExtcapNif) Name() string {
return e.name
}
// Description returns the short description of an external capture network
// interface.
func (e *ExtcapNif) Description() string {
return e.description
}
// extcapNifs maps external capture network interfaces names to their specific
// implementation. To be more precise: the interfaces of the network interfaces,
// indexed by the network interface names.
var extcapNifs map[string]ExtcapNifActions
// populateDictionary ensures that the dictionary of extcap network interfaces
// is correctly populated based on the plugin registration. We can't do it in an
// init func because we cannot control order of all init's in this package, so
// that extcap plugins might not have yet allowed to register.
func populateDictionary() {
if extcapNifs != nil {
return
}
extcapNifs = map[string]ExtcapNifActions{}
for _, nif := range plugger.Group[ExtcapNifActions]().Symbols() {
extcapNifs[nif.Name()] = nif
}
}
// ExtcapNifByName returns the external capture network interface with the
// specified name. Please note that capture network interfaces need to register
// their ExtcapNifActions implementation in the plugger group typed as
// ExtcapNifActions.
func ExtcapNifByName(name string) (ExtcapNifActions, bool) {
populateDictionary()
nif, ok := extcapNifs[name]
return nif, ok
}
// ExtcapNifNames returns the list of names of the registered external capture
// network interfaces. The names in the returned list are in no particular
// order.
func ExtcapNifNames() []string {
populateDictionary()
return maps.Keys(extcapNifs)
}