forked from ti-mo/conntrack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conn_test.go
203 lines (167 loc) · 4.46 KB
/
conn_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
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
package conntrack_test
import (
"fmt"
"log"
"net"
"testing"
"github.com/mdlayher/netlink"
"github.com/stretchr/testify/assert"
"github.com/ti-mo/conntrack"
"github.com/ti-mo/netfilter"
)
func TestConnDialError(t *testing.T) {
// Attempt to open a Netlink socket into a netns that is highly unlikely
// to exist, so we can catch an error from Dial.
_, err := conntrack.Dial(&netlink.Config{NetNS: 1337})
assert.EqualError(t, err, "setns: bad file descriptor")
}
func ExampleConn_createUpdateFlow() {
// Open a Conntrack connection.
c, err := conntrack.Dial(nil)
if err != nil {
log.Fatal(err)
}
// Set up a new Flow object using a given set of attributes.
f := conntrack.NewFlow(
17, 0,
net.ParseIP("2a00:1450:400e:804::200e"),
net.ParseIP("2a00:1450:400e:804::200f"),
1234, 80, 120, 0,
)
// Send the Flow to the kernel.
err = c.Create(f)
if err != nil {
log.Fatal(err)
}
f.Timeout = 240
// Update the Flow's timeout to 240 seconds.
err = c.Update(f)
if err != nil {
log.Fatal(err)
}
// Query the kernel based on the Flow's source/destination tuples.
// Returns a new Flow object with its connection ID assigned by the kernel.
qf, err := c.Get(f)
if err != nil {
log.Fatal(err)
}
// Print the result. The Flow has a timeout greater than 120 seconds.
log.Print(qf)
}
func ExampleConn_dumpFilter() {
// Open a Conntrack connection.
c, err := conntrack.Dial(nil)
if err != nil {
log.Fatal(err)
}
f1 := conntrack.NewFlow(
6, 0, net.IPv4(1, 2, 3, 4), net.IPv4(5, 6, 7, 8),
1234, 80, 120, 0x00ff, // Set a connection mark
)
f2 := conntrack.NewFlow(
17, 0, net.ParseIP("2a00:1450:400e:804::200e"), net.ParseIP("2a00:1450:400e:804::200f"),
1234, 80, 120, 0xff00, // Set a connection mark
)
_ = c.Create(f1)
_ = c.Create(f2)
// Dump all records in the Conntrack table that match the filter's mark/mask.
df, err := c.DumpFilter(conntrack.Filter{Mark: 0xff00, Mask: 0xff00})
if err != nil {
log.Fatal(err)
}
// Print the result. Only f2 is displayed.
log.Print(df)
}
func ExampleConn_flush() {
// Open a Conntrack connection.
c, err := conntrack.Dial(nil)
if err != nil {
log.Fatal(err)
}
// Evict all entries from the conntrack table in the current network namespace.
err = c.Flush()
if err != nil {
log.Fatal(err)
}
}
func ExampleConn_flushFilter() {
// Open a Conntrack connection.
c, err := conntrack.Dial(nil)
if err != nil {
log.Fatal(err)
}
f1 := conntrack.NewFlow(
6, 0, net.IPv4(1, 2, 3, 4), net.IPv4(5, 6, 7, 8),
1234, 80, 120, 0x00ff, // Set a connection mark
)
f2 := conntrack.NewFlow(
17, 0, net.ParseIP("2a00:1450:400e:804::200e"), net.ParseIP("2a00:1450:400e:804::200f"),
1234, 80, 120, 0xff00, // Set a connection mark
)
_ = c.Create(f1)
_ = c.Create(f2)
// Flush only the second flow matching the filter's mark/mask.
err = c.FlushFilter(conntrack.Filter{Mark: 0xff00, Mask: 0xff00})
if err != nil {
log.Fatal(err)
}
// Getting f1 succeeds.
_, err = c.Get(f1)
if err != nil {
log.Fatal(err)
}
// Getting f2 will fail, since it was flushed.
_, err = c.Get(f2)
if err != nil {
log.Println("Flow f2 missing, as expected", err)
}
}
func ExampleConn_delete() {
// Open a Conntrack connection.
c, err := conntrack.Dial(nil)
if err != nil {
log.Fatal(err)
}
f := conntrack.NewFlow(
6, 0, net.IPv4(1, 2, 3, 4), net.IPv4(5, 6, 7, 8),
1234, 80, 120, 0,
)
// Create the Flow, will return err if unsuccessful.
err = c.Create(f)
if err != nil {
log.Fatal(err)
}
// Delete the Flow based on its IP/port tuple, will return err if unsuccessful.
err = c.Delete(f)
if err != nil {
log.Fatal(err)
}
}
func ExampleConn_listen() {
// Open a Conntrack connection.
c, err := conntrack.Dial(nil)
if err != nil {
log.Fatal(err)
}
// Make a buffered channel to receive event updates on.
evCh := make(chan conntrack.Event, 1024)
// Listen for all Conntrack and Conntrack-Expect events with 4 decoder goroutines.
// All errors caught in the decoders are passed on channel errCh.
errCh, err := c.Listen(evCh, 4, append(netfilter.GroupsCT, netfilter.GroupsCTExp...))
if err != nil {
log.Fatal(err)
}
// Listen to Conntrack events from all network namespaces on the system.
err = c.SetOption(netlink.ListenAllNSID, true)
if err != nil {
log.Fatal(err)
}
// Start a goroutine to print all incoming messages on the event channel.
go func() {
for {
fmt.Println(<-evCh)
}
}()
// Stop the program as soon as an error is caught in a decoder goroutine.
log.Print(<-errCh)
}