Skip to content

Commit 4528ed6

Browse files
authored
refact: migrate net.IP -> net/netip: part 1 (#3680)
1 parent 854e727 commit 4528ed6

File tree

7 files changed

+441
-192
lines changed

7 files changed

+441
-192
lines changed

pkg/csnet/ip.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package csnet
2+
3+
import (
4+
"github.com/crowdsecurity/crowdsec/pkg/types"
5+
)
6+
7+
type IPAddrSize int
8+
9+
const (
10+
IPv4Size IPAddrSize = 4
11+
IPv6Size IPAddrSize = 16
12+
)
13+
14+
type IP struct {
15+
size IPAddrSize
16+
Addr int64
17+
Sfx int64
18+
}
19+
20+
type Range struct {
21+
Start IP
22+
End IP
23+
}
24+
25+
func (r Range) Size() int {
26+
return int(r.Start.size)
27+
}
28+
29+
func NewRange(anyIP string) (Range, error) {
30+
size, start_ip, start_sfx, end_ip, end_sfx, err := types.Addr2Ints(anyIP)
31+
if err != nil {
32+
return Range{}, err
33+
}
34+
35+
return Range{
36+
Start: IP{
37+
size: IPAddrSize(size),
38+
Addr: start_ip,
39+
Sfx: start_sfx,
40+
},
41+
End: IP{
42+
size: IPAddrSize(size),
43+
Addr: end_ip,
44+
Sfx: end_sfx,
45+
},
46+
}, nil
47+
}

pkg/csnet/ip_test.go

Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
package csnet
2+
3+
import (
4+
"math"
5+
"strings"
6+
"testing"
7+
)
8+
9+
func TestAdd2Int(t *testing.T) {
10+
tests := []struct {
11+
in_addr string
12+
exp_sz int
13+
exp_start_ip int64
14+
exp_start_sfx int64
15+
exp_end_ip int64
16+
exp_end_sfx int64
17+
exp_error string
18+
}{
19+
{
20+
in_addr: "7FFF:FFFF:FFFF:FFFF:aaaa:aaaa:aaaa:fff7",
21+
22+
exp_sz: 16,
23+
exp_start_ip: -math.MaxInt64 + 0x7FFFFFFFFFFFFFFF,
24+
exp_start_sfx: -math.MaxInt64 + 0xaaaaaaaaaaaafff7,
25+
exp_end_ip: -math.MaxInt64 + 0x7FFFFFFFFFFFFFFF,
26+
exp_end_sfx: -math.MaxInt64 + 0xaaaaaaaaaaaafff7,
27+
},
28+
{
29+
in_addr: "aaaa:aaaa:aaaa:aaaa:aaaa:aaaa:aaaa:fff7",
30+
31+
exp_sz: 16,
32+
exp_start_ip: -math.MaxInt64 + 0xaaaaaaaaaaaaaaaa,
33+
exp_start_sfx: -math.MaxInt64 + 0xaaaaaaaaaaaafff7,
34+
exp_end_ip: -math.MaxInt64 + 0xaaaaaaaaaaaaaaaa,
35+
exp_end_sfx: -math.MaxInt64 + 0xaaaaaaaaaaaafff7,
36+
},
37+
{
38+
in_addr: "ffff:ffff:ffff:ffff:ffff:ffff:ffff:fff7",
39+
/*ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff*/
40+
41+
exp_sz: 16,
42+
exp_start_ip: math.MaxInt64,
43+
exp_start_sfx: -math.MaxInt64 + 0xfffffffffffffff7,
44+
exp_end_ip: math.MaxInt64,
45+
exp_end_sfx: -math.MaxInt64 + 0xfffffffffffffff7,
46+
},
47+
{
48+
in_addr: "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
49+
/*ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff*/
50+
51+
exp_sz: 16,
52+
exp_start_ip: math.MaxInt64,
53+
exp_start_sfx: math.MaxInt64,
54+
exp_end_ip: math.MaxInt64,
55+
exp_end_sfx: math.MaxInt64,
56+
},
57+
{
58+
in_addr: "::",
59+
/*::*/
60+
61+
exp_sz: 16,
62+
exp_start_ip: -math.MaxInt64,
63+
exp_start_sfx: -math.MaxInt64,
64+
exp_end_ip: -math.MaxInt64,
65+
exp_end_sfx: -math.MaxInt64,
66+
},
67+
{
68+
in_addr: "2001:db8::",
69+
/*2001:db8:: -> 2001:db8::*/
70+
exp_sz: 16,
71+
exp_start_ip: -math.MaxInt64 + 0x20010DB800000000,
72+
exp_start_sfx: -math.MaxInt64,
73+
exp_end_ip: -math.MaxInt64 + 0x20010DB800000000,
74+
exp_end_sfx: -math.MaxInt64,
75+
},
76+
{
77+
in_addr: "2001:db8:0000:0000:0000:0000:0000:00ff",
78+
/*2001:db8:0000:0000:0000:0000:0000:00ff*/
79+
exp_sz: 16,
80+
exp_start_ip: -math.MaxInt64 + 0x20010DB800000000,
81+
exp_start_sfx: -math.MaxInt64 + 0xFF,
82+
exp_end_ip: -math.MaxInt64 + 0x20010DB800000000,
83+
exp_end_sfx: -math.MaxInt64 + 0xFF,
84+
},
85+
{
86+
in_addr: "1.2.3.4",
87+
/*1.2.3.4*/
88+
exp_sz: 4,
89+
exp_start_ip: -math.MaxInt64 + 0x01020304,
90+
exp_start_sfx: 0,
91+
exp_end_ip: -math.MaxInt64 + 0x01020304,
92+
exp_end_sfx: 0,
93+
},
94+
{
95+
in_addr: "::/0",
96+
/*:: -> ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff*/
97+
98+
exp_sz: 16,
99+
exp_start_ip: -math.MaxInt64,
100+
exp_start_sfx: -math.MaxInt64,
101+
exp_end_ip: math.MaxInt64,
102+
exp_end_sfx: math.MaxInt64,
103+
},
104+
{
105+
in_addr: "::/64",
106+
/*:: -> 0000:0000:0000:0000:ffff:ffff:ffff:ffff*/
107+
exp_sz: 16,
108+
exp_start_ip: -math.MaxInt64,
109+
exp_start_sfx: -math.MaxInt64,
110+
exp_end_ip: -math.MaxInt64,
111+
exp_end_sfx: math.MaxInt64,
112+
},
113+
{
114+
in_addr: "2001:db8::/109",
115+
/*2001:db8:: -> 2001:db8:0000:0000:0000:0000:0007:ffff*/
116+
exp_sz: 16,
117+
exp_start_ip: -math.MaxInt64 + 0x20010DB800000000,
118+
exp_start_sfx: -math.MaxInt64,
119+
exp_end_ip: -math.MaxInt64 + 0x20010DB800000000,
120+
exp_end_sfx: -math.MaxInt64 + 0x7FFFF,
121+
},
122+
{
123+
in_addr: "0.0.0.0/0",
124+
/*0.0.0.0 -> 255.255.255.255*/
125+
exp_sz: 4,
126+
exp_start_ip: -math.MaxInt64,
127+
exp_start_sfx: 0,
128+
exp_end_ip: -math.MaxInt64 + 0xFFFFFFFF,
129+
exp_end_sfx: 0,
130+
},
131+
{
132+
in_addr: "0.0.0.0/16",
133+
/*0.0.0.0 -> 0.0.255.255*/
134+
exp_sz: 4,
135+
exp_start_ip: -math.MaxInt64,
136+
exp_start_sfx: 0,
137+
exp_end_ip: -math.MaxInt64 + 0x0000FFFF,
138+
exp_end_sfx: 0,
139+
},
140+
{
141+
in_addr: "255.255.0.0/16",
142+
/*255.255.0.0 -> 255.255.255.255*/
143+
exp_sz: 4,
144+
exp_start_ip: -math.MaxInt64 + 0xFFFF0000,
145+
exp_start_sfx: 0,
146+
exp_end_ip: -math.MaxInt64 + 0xFFFFFFFF,
147+
exp_end_sfx: 0,
148+
},
149+
{
150+
in_addr: "1.2.3.0/24",
151+
/*1.2.3.0 -> 1.2.3.255*/
152+
exp_sz: 4,
153+
exp_start_ip: -math.MaxInt64 + 0x01020300,
154+
exp_start_sfx: 0,
155+
exp_end_ip: -math.MaxInt64 + 0x010203FF,
156+
exp_end_sfx: 0,
157+
},
158+
/*errors*/
159+
{
160+
in_addr: "xxx/24",
161+
exp_error: "invalid ip range 'xxx/24': invalid CIDR address: xxx/24",
162+
},
163+
{
164+
in_addr: "xxx2",
165+
exp_error: "invalid ip address 'xxx2'",
166+
},
167+
}
168+
169+
for idx, test := range tests {
170+
rng, err := NewRange(test.in_addr)
171+
if err != nil && test.exp_error == "" {
172+
t.Fatalf("%d unexpected error : %s", idx, err)
173+
}
174+
175+
if test.exp_error != "" {
176+
if !strings.Contains(err.Error(), test.exp_error) {
177+
t.Fatalf("%d unmatched error : %s != %s", idx, err, test.exp_error)
178+
}
179+
180+
continue // we can skip this one
181+
}
182+
183+
if rng.Size() != test.exp_sz {
184+
t.Fatalf("%d unexpected size %d != %d", idx, rng.Size(), test.exp_sz)
185+
}
186+
187+
if rng.Start.Addr != test.exp_start_ip {
188+
t.Fatalf("%d unexpected start_ip %d != %d", idx, rng.Start.Addr, test.exp_start_ip)
189+
}
190+
191+
if rng.Size() == 16 {
192+
if rng.Start.Sfx != test.exp_start_sfx {
193+
t.Fatalf("%d unexpected start sfx %d != %d", idx, rng.Start.Sfx, test.exp_start_sfx)
194+
}
195+
}
196+
197+
if rng.End.Addr != test.exp_end_ip {
198+
t.Fatalf("%d unexpected end ip %d != %d", idx, rng.End.Addr, test.exp_end_ip)
199+
}
200+
201+
if rng.Size() == 16 {
202+
if rng.End.Sfx != test.exp_end_sfx {
203+
t.Fatalf("%d unexpected end sfx %d != %d", idx, rng.End.Sfx, test.exp_end_sfx)
204+
}
205+
}
206+
}
207+
}

0 commit comments

Comments
 (0)