Merge pull request #841 from drautb/master

Add start/stop callbacks to packet proxy
This commit is contained in:
Simone Margaritelli 2021-03-10 17:58:00 +01:00 committed by GitHub
commit 08ae5e0920
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 45 additions and 5 deletions

View file

@ -161,6 +161,18 @@ func (mod *PacketProxy) Configure() (err error) {
return fmt.Errorf("Symbol OnPacket is not a valid callback function.")
}
if sym, err = mod.plugin.Lookup("OnStart"); err == nil {
var onStartCb func() int
if onStartCb, ok = sym.(func() int); !ok {
return fmt.Errorf("OnStart signature does not match expected signature: 'func() int'")
} else {
var result int
if result = onStartCb(); result != 0 {
return fmt.Errorf("OnStart returned non-zero result. result=%d", result)
}
}
}
mod.queue = new(nfqueue.Queue)
if err = mod.queue.SetCallback(dummyCallback); err != nil {
return
@ -206,10 +218,22 @@ func (mod *PacketProxy) Start() error {
})
}
func (mod *PacketProxy) Stop() error {
func (mod *PacketProxy) Stop() (err error) {
return mod.SetRunning(false, func() {
mod.queue.StopLoop()
mod.runRule(false)
var sym plugin.Symbol
if sym, err = mod.plugin.Lookup("OnStop"); err == nil {
var onStopCb func()
var ok bool
if onStopCb, ok = sym.(func()); !ok {
mod.Error("OnStop signature does not match expected signature: 'func()', unable to call OnStop.")
} else {
onStopCb()
}
}
<-mod.done
})
}

View file

@ -101,10 +101,10 @@ func ParseTargets(targets string, aliasMap *data.UnsortedKV) (ips []net.IP, macs
// first isolate MACs and parse them
for _, mac := range macParser.FindAllString(targets, -1) {
mac = NormalizeMac(mac)
hw, err := net.ParseMAC(mac)
normalizedMac := NormalizeMac(mac)
hw, err := net.ParseMAC(normalizedMac)
if err != nil {
return nil, nil, fmt.Errorf("error while parsing MAC '%s': %s", mac, err)
return nil, nil, fmt.Errorf("error while parsing MAC '%s': %s", normalizedMac, err)
}
macs = append(macs, hw)

View file

@ -37,6 +37,14 @@ func TestNormalizeMac(t *testing.T) {
// TODO: refactor to parse targets with an actual alias map
func TestParseTargets(t *testing.T) {
aliasMap, err := data.NewMemUnsortedKV()
if err != nil {
panic(err)
}
aliasMap.Set("5c:00:0b:90:a9:f0", "test_alias")
aliasMap.Set("5c:00:0b:90:a9:f1", "Home_Laptop")
cases := []struct {
Name string
InputTargets string
@ -57,9 +65,17 @@ func TestParseTargets(t *testing.T) {
},
{
"MACs are parsed",
"192.168.1.2, 192.168.1.3, 5c:00:0b:90:a9:f0, 6c:00:0b:90:a9:f0",
"192.168.1.2, 192.168.1.3, 5c:00:0b:90:a9:f0, 6c:00:0b:90:a9:f0, 6C:00:0B:90:A9:F0",
&data.UnsortedKV{},
2,
3,
false,
},
{
"Aliases are parsed",
"test_alias, Home_Laptop",
aliasMap,
0,
2,
false,
},