Skip to content

Commit 7ac9c2a

Browse files
antoninbasti-mo
authored andcommitted
Use netip.Addr instead of net.IP
Using the net/netip package instead of the net package can help reduce the memory footprint of the library and help reduce the number of heap allocations. This is a breaking change for consumers of the library as exported types are updated to use fields of type netip.Addr instead of net.IP. We also remove the depedency on go-cmp and use assert.Equal instead in tests. Fixes #35 Signed-off-by: Antonin Bas <[email protected]>
1 parent 59e136f commit 7ac9c2a

15 files changed

+141
-209
lines changed

conn_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package conntrack_test
33
import (
44
"fmt"
55
"log"
6-
"net"
6+
"net/netip"
77
"testing"
88

99
"github.com/mdlayher/netlink"
@@ -34,8 +34,8 @@ func ExampleConn_createUpdateFlow() {
3434
// Set up a new Flow object using a given set of attributes.
3535
f := conntrack.NewFlow(
3636
17, 0,
37-
net.ParseIP("2a00:1450:400e:804::200e"),
38-
net.ParseIP("2a00:1450:400e:804::200f"),
37+
netip.MustParseAddr("2a00:1450:400e:804::200e"),
38+
netip.MustParseAddr("2a00:1450:400e:804::200f"),
3939
1234, 80, 120, 0,
4040
)
4141

@@ -72,12 +72,12 @@ func ExampleConn_dumpFilter() {
7272
}
7373

7474
f1 := conntrack.NewFlow(
75-
6, 0, net.IPv4(1, 2, 3, 4), net.IPv4(5, 6, 7, 8),
75+
6, 0, netip.MustParseAddr("1.2.3.4"), netip.MustParseAddr("5.6.7.8"),
7676
1234, 80, 120, 0x00ff, // Set a connection mark
7777
)
7878

7979
f2 := conntrack.NewFlow(
80-
17, 0, net.ParseIP("2a00:1450:400e:804::200e"), net.ParseIP("2a00:1450:400e:804::200f"),
80+
17, 0, netip.MustParseAddr("2a00:1450:400e:804::200e"), netip.MustParseAddr("2a00:1450:400e:804::200f"),
8181
1234, 80, 120, 0xff00, // Set a connection mark
8282
)
8383

@@ -116,12 +116,12 @@ func ExampleConn_flushFilter() {
116116
}
117117

118118
f1 := conntrack.NewFlow(
119-
6, 0, net.IPv4(1, 2, 3, 4), net.IPv4(5, 6, 7, 8),
119+
6, 0, netip.MustParseAddr("1.2.3.4"), netip.MustParseAddr("5.6.7.8"),
120120
1234, 80, 120, 0x00ff, // Set a connection mark
121121
)
122122

123123
f2 := conntrack.NewFlow(
124-
17, 0, net.ParseIP("2a00:1450:400e:804::200e"), net.ParseIP("2a00:1450:400e:804::200f"),
124+
17, 0, netip.MustParseAddr("2a00:1450:400e:804::200e"), netip.MustParseAddr("2a00:1450:400e:804::200f"),
125125
1234, 80, 120, 0xff00, // Set a connection mark
126126
)
127127

@@ -155,7 +155,7 @@ func ExampleConn_delete() {
155155
}
156156

157157
f := conntrack.NewFlow(
158-
6, 0, net.IPv4(1, 2, 3, 4), net.IPv4(5, 6, 7, 8),
158+
6, 0, netip.MustParseAddr("1.2.3.4"), netip.MustParseAddr("5.6.7.8"),
159159
1234, 80, 120, 0,
160160
)
161161

event_integration_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
package conntrack
44

55
import (
6-
"net"
6+
"net/netip"
77
"testing"
88

99
"github.com/mdlayher/netlink"
@@ -43,7 +43,7 @@ func TestConnListen(t *testing.T) {
4343

4444
var warn bool
4545

46-
ip := net.ParseIP("::f00")
46+
ip := netip.MustParseAddr("::f00")
4747
for _, proto := range []uint8{unix.IPPROTO_TCP, unix.IPPROTO_UDP, unix.IPPROTO_DCCP, unix.IPPROTO_SCTP} {
4848
// Create the Flow.
4949
f := NewFlow(

expect_integration_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
package conntrack
44

55
import (
6-
"net"
6+
"net/netip"
77
"testing"
88

99
"golang.org/x/sys/unix"
@@ -27,7 +27,7 @@ func TestConnCreateExpect(t *testing.T) {
2727
c, _, err := makeNSConn()
2828
require.NoError(t, err)
2929

30-
f := NewFlow(6, 0, net.IPv4(1, 2, 3, 4), net.IPv4(5, 6, 7, 8), 42000, 21, 120, 0)
30+
f := NewFlow(6, 0, netip.MustParseAddr("1.2.3.4"), netip.MustParseAddr("5.6.7.8"), 42000, 21, 120, 0)
3131

3232
err = c.Create(f)
3333
require.NoError(t, err, "unexpected error creating flow", f)
@@ -37,8 +37,8 @@ func TestConnCreateExpect(t *testing.T) {
3737
TupleMaster: f.TupleOrig,
3838
Tuple: Tuple{
3939
IP: IPTuple{
40-
SourceAddress: net.IPv4(1, 2, 3, 4),
41-
DestinationAddress: net.IPv4(5, 6, 7, 8),
40+
SourceAddress: netip.MustParseAddr("1.2.3.4"),
41+
DestinationAddress: netip.MustParseAddr("5.6.7.8"),
4242
},
4343
Proto: ProtoTuple{
4444
Protocol: 6,
@@ -48,8 +48,8 @@ func TestConnCreateExpect(t *testing.T) {
4848
},
4949
Mask: Tuple{
5050
IP: IPTuple{
51-
SourceAddress: net.IPv4(255, 255, 255, 255),
52-
DestinationAddress: net.IPv4(255, 255, 255, 255),
51+
SourceAddress: netip.MustParseAddr("255.255.255.255"),
52+
DestinationAddress: netip.MustParseAddr("255.255.255.255"),
5353
},
5454
Proto: ProtoTuple{
5555
Protocol: 6,

expect_test.go

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
package conntrack
22

33
import (
4-
"net"
4+
"net/netip"
55
"testing"
66

7-
"github.com/google/go-cmp/cmp"
87
"github.com/stretchr/testify/assert"
98
"github.com/stretchr/testify/require"
109
"github.com/ti-mo/netfilter"
@@ -183,8 +182,8 @@ var corpusExpect = []struct {
183182
exp: Expect{
184183
TupleMaster: Tuple{
185184
IP: IPTuple{
186-
SourceAddress: []byte{127, 0, 0, 1},
187-
DestinationAddress: []byte{127, 0, 0, 2},
185+
SourceAddress: netip.MustParseAddr("127.0.0.1"),
186+
DestinationAddress: netip.MustParseAddr("127.0.0.2"),
188187
},
189188
Proto: ProtoTuple{
190189
Protocol: 6,
@@ -194,8 +193,8 @@ var corpusExpect = []struct {
194193
},
195194
Tuple: Tuple{
196195
IP: IPTuple{
197-
SourceAddress: []byte{127, 0, 0, 1},
198-
DestinationAddress: []byte{127, 0, 0, 2},
196+
SourceAddress: netip.MustParseAddr("127.0.0.1"),
197+
DestinationAddress: netip.MustParseAddr("127.0.0.2"),
199198
},
200199
Proto: ProtoTuple{
201200
Protocol: 6,
@@ -204,8 +203,8 @@ var corpusExpect = []struct {
204203
},
205204
Mask: Tuple{
206205
IP: IPTuple{
207-
SourceAddress: []byte{255, 255, 255, 255},
208-
DestinationAddress: []byte{255, 255, 255, 255},
206+
SourceAddress: netip.MustParseAddr("255.255.255.255"),
207+
DestinationAddress: netip.MustParseAddr("255.255.255.255"),
209208
},
210209
Proto: ProtoTuple{
211210
Protocol: 6,
@@ -263,11 +262,8 @@ func TestExpectUnmarshal(t *testing.T) {
263262
for _, tt := range corpusExpect {
264263
t.Run(tt.name, func(t *testing.T) {
265264
var ex Expect
266-
assert.NoError(t, ex.unmarshal(mustDecodeAttributes(tt.attrs)))
267-
268-
if diff := cmp.Diff(tt.exp, ex); diff != "" {
269-
t.Fatalf("unexpected unmarshal (-want +got):\n%s", diff)
270-
}
265+
require.NoError(t, ex.unmarshal(mustDecodeAttributes(tt.attrs)))
266+
assert.Equal(t, tt.exp, ex, "unexpected unmarshal")
271267
})
272268
}
273269

@@ -355,9 +351,7 @@ func TestExpectMarshal(t *testing.T) {
355351
},
356352
}
357353

358-
if diff := cmp.Diff(want, exm); diff != "" {
359-
t.Fatalf("unexpected Expect marshal (-want +got):\n%s", diff)
360-
}
354+
assert.Equal(t, want, exm, "unexpected Expect marshal")
361355

362356
// Cannot marshal without tuple/mask/master Tuples
363357
_, err = Expect{}.marshal()
@@ -424,10 +418,7 @@ func TestExpectNATUnmarshal(t *testing.T) {
424418
}
425419

426420
require.NoError(t, err)
427-
428-
if diff := cmp.Diff(tt.enat, enat); diff != "" {
429-
t.Fatalf("unexpected unmarshal (-want +got):\n%s", diff)
430-
}
421+
assert.Equal(t, tt.enat, enat, "unexpected unmarshal")
431422
})
432423
}
433424
}
@@ -439,8 +430,8 @@ func TestExpectNATMarshal(t *testing.T) {
439430
Direction: true,
440431
Tuple: Tuple{
441432
IP: IPTuple{
442-
SourceAddress: net.ParseIP("baa:baa::b"),
443-
DestinationAddress: net.ParseIP("ef00:3f00::ba13"),
433+
SourceAddress: netip.MustParseAddr("baa:baa::b"),
434+
DestinationAddress: netip.MustParseAddr("ef00:3f00::ba13"),
444435
},
445436
Proto: ProtoTuple{
446437
Protocol: 13,
@@ -458,9 +449,7 @@ func TestExpectNATMarshal(t *testing.T) {
458449

459450
// Only verify first attribute (direction); Tuple marshal has its own tests
460451
want := netfilter.Attribute{Type: uint16(ctaExpectNATDir), Data: []byte{0, 0, 0, 1}}
461-
if diff := cmp.Diff(want, enm.Children[0]); diff != "" {
462-
t.Fatalf("unexpected ExpectNAT marshal (-want +got):\n%s", diff)
463-
}
452+
assert.Equal(t, want, enm.Children[0], "unexpected ExpectNAT marshal")
464453
}
465454

466455
func TestExpectTypeString(t *testing.T) {

filter_test.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ package conntrack
33
import (
44
"testing"
55

6-
"github.com/ti-mo/netfilter"
6+
"github.com/stretchr/testify/assert"
77

8-
"github.com/google/go-cmp/cmp"
8+
"github.com/ti-mo/netfilter"
99
)
1010

1111
func TestFilterMarshal(t *testing.T) {
@@ -22,7 +22,5 @@ func TestFilterMarshal(t *testing.T) {
2222
},
2323
}
2424

25-
if diff := cmp.Diff(fm, f.marshal()); diff != "" {
26-
t.Fatalf("unexpected Filter marshal (-want +got):\n%s", diff)
27-
}
25+
assert.Equal(t, fm, f.marshal(), "unexpected Filter marshal")
2826
}

flow.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package conntrack
22

33
import (
44
"fmt"
5-
"net"
5+
"net/netip"
66

77
"github.com/mdlayher/netlink"
88
"github.com/ti-mo/netfilter"
@@ -44,7 +44,7 @@ type Flow struct {
4444
// source and destination addresses. srcPort and dstPort are the source and
4545
// destination ports. timeout is the non-zero time-to-live of a connection in
4646
// seconds.
47-
func NewFlow(proto uint8, status StatusFlag, srcAddr, destAddr net.IP,
47+
func NewFlow(proto uint8, status StatusFlag, srcAddr, destAddr netip.Addr,
4848
srcPort, destPort uint16, timeout, mark uint32) Flow {
4949

5050
var f Flow

0 commit comments

Comments
 (0)