Skip to content

Commit 23bf240

Browse files
committed
Update GetRandomCidrBlock to return /18 ranges.
1 parent 17c1a7a commit 23bf240

2 files changed

Lines changed: 32 additions & 18 deletions

File tree

util/network.go

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,7 @@ func GetRandomPrivateCidrBlock(routingPrefix int) string {
3333
o4 = Random(0, 255)
3434
}
3535

36-
case 31:
37-
fallthrough
38-
case 30:
39-
fallthrough
40-
case 29:
41-
fallthrough
42-
case 28:
43-
fallthrough
44-
case 27:
45-
fallthrough
46-
case 26:
47-
fallthrough
48-
case 25:
36+
case 31, 30, 29, 28, 27, 26, 25:
4937
fallthrough
5038
case 24:
5139
o1 = RandomInt([]int{10, 172, 192})
@@ -56,17 +44,34 @@ func GetRandomPrivateCidrBlock(routingPrefix int) string {
5644
o3 = Random(0, 255)
5745
o4 = 0
5846
case 172:
59-
o2 = Random(16, 31)
60-
o3 = Random(0, 255)
47+
o2 = 16
48+
o3 = 0
6149
o4 = 0
6250
case 192:
6351
o2 = 168
64-
o3 = Random(0, 255)
52+
o3 = 0
6553
o4 = 0
6654
}
55+
case 23, 22, 21, 20, 19:
56+
fallthrough
57+
case 18:
58+
o1 = RandomInt([]int{10, 172, 192})
6759

60+
switch o1 {
61+
case 10:
62+
o2 = 0
63+
o3 = 0
64+
o4 = 0
65+
case 172:
66+
o2 = 16
67+
o3 = 0
68+
o4 = 0
69+
case 192:
70+
o2 = 168
71+
o3 = 0
72+
o4 = 0
73+
}
6874
}
69-
7075
return fmt.Sprintf("%d.%d.%d.%d/%d", o1, o2, o3, o4, routingPrefix)
7176
}
7277

util/network_test.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,13 @@ func TestGetFirstTwoOctets(t *testing.T) {
1111
if firstTwo != "10.100" {
1212
t.Errorf("Received: %s, Expected: 10.100", firstTwo)
1313
}
14-
}
14+
}
15+
16+
// Deferred to save time
17+
//func TestGetRandomPrivateCidrBlock(t *testing.T) {
18+
// t.Parallel()
19+
//
20+
// for i := 0; i < 10; i++ {
21+
// fmt.Println(GetRandomPrivateCidrBlock(18))
22+
// }
23+
//}

0 commit comments

Comments
 (0)