forked from florianl/go-nfqueue
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample_test.go
54 lines (44 loc) · 1.13 KB
/
example_test.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
//+build linux
package nfqueue_test
import (
"context"
"fmt"
"time"
nfqueue "github.com/twistlock/go-nfqueue"
)
func ExampleNfqueue_Register() {
// Send outgoing pings to nfqueue queue 100
// # sudo iptables -I OUTPUT -p icmp -j NFQUEUE --queue-num 100
// Set configuration options for nfqueue
config := nfqueue.Config{
NfQueue: 100,
MaxPacketLen: 0xFFFF,
MaxQueueLen: 0xFF,
Copymode: nfqueue.NfQnlCopyPacket,
ReadTimeout: 10 * time.Millisecond,
WriteTimeout: 15 * time.Millisecond,
}
nf, err := nfqueue.Open(&config)
if err != nil {
fmt.Println("could not open nfqueue socket:", err)
return
}
defer nf.Close()
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
fn := func(a nfqueue.Attribute) int {
id := *a.PacketID
// Just print out the id and payload of the nfqueue packet
fmt.Printf("[%d]\t%v\n", id, *a.Payload)
nf.SetVerdict(id, nfqueue.NfAccept)
return 0
}
// Register your function to listen on nflqueue queue 100
err = nf.Register(ctx, fn)
if err != nil {
fmt.Println(err)
return
}
// Block till the context expires
<-ctx.Done()
}