Skip to content
This repository was archived by the owner on Jul 22, 2024. It is now read-only.

Commit cfefa3a

Browse files
smikulcikHenryGessau
authored andcommitted
Expose ContainsNet Add GetNetworks (#13)
* Expose ContainsNet Add GetNetworks * Replace containsNet with ContainsNet * fix wording
1 parent 6553693 commit cfefa3a

File tree

4 files changed

+34
-12
lines changed

4 files changed

+34
-12
lines changed

ipset.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,15 @@ func (s *IPSet) GetIPs(limit int) (ips []net.IP) {
114114
return
115115
}
116116

117+
// GetNetworks retrieves a list of all networks included in the ipTree
118+
func (s *IPSet) GetNetworks() []*net.IPNet {
119+
networks := []*net.IPNet{}
120+
s.tree.walk(func(node *ipTree) {
121+
networks = append(networks, node.net)
122+
})
123+
return networks
124+
}
125+
117126
// Intersection computes the set intersect between this IPSet and another one
118127
// It returns a new set which is the intersection.
119128
func (s *IPSet) Intersection(set1 *IPSet) (interSect *IPSet) {

ipset_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package netaddr
22

33
import (
4+
"fmt"
45
"math/big"
56
"math/rand"
67
"net"
@@ -418,3 +419,15 @@ func TestIPSetAllocateDeallocate(t *testing.T) {
418419
assert.Equal(t, 51036, len(ips))
419420
assert.Equal(t, []error{}, available.tree.validate())
420421
}
422+
423+
func TestGetNetworks(t *testing.T) {
424+
s := &IPSet{}
425+
assert.Equal(t, []*net.IPNet{}, s.GetNetworks())
426+
s.InsertNet(Ten24)
427+
assert.Equal(t, "[10.0.0.0/24]", fmt.Sprintf("%s", s.GetNetworks()))
428+
ten25, _ := ParseNet("10.0.0.0/25")
429+
s.RemoveNet(ten25)
430+
assert.Equal(t, "[10.0.0.128/25]", fmt.Sprintf("%s", s.GetNetworks()))
431+
s.Remove(ParseIP("10.0.0.129"))
432+
assert.Equal(t, "[10.0.0.128/32 10.0.0.130/31 10.0.0.132/30 10.0.0.136/29 10.0.0.144/28 10.0.0.160/27 10.0.0.192/26]", fmt.Sprintf("%s", s.GetNetworks()))
433+
}

iptree.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func (t *ipTree) trimLeft(top *ipTree) *ipTree {
4242
return nil
4343
}
4444

45-
if containsNet(top.net, t.net) {
45+
if ContainsNet(top.net, t.net) {
4646
return t.left.trimLeft(top)
4747
}
4848
t.setRight(t.right.trimLeft(top))
@@ -55,7 +55,7 @@ func (t *ipTree) trimRight(top *ipTree) *ipTree {
5555
return nil
5656
}
5757

58-
if containsNet(top.net, t.net) {
58+
if ContainsNet(top.net, t.net) {
5959
return t.right.trimRight(top)
6060
}
6161
t.setLeft(t.left.trimRight(top))
@@ -71,11 +71,11 @@ func (t *ipTree) insert(newNode *ipTree) *ipTree {
7171
return newNode
7272
}
7373

74-
if containsNet(t.net, newNode.net) {
74+
if ContainsNet(t.net, newNode.net) {
7575
return t
7676
}
7777

78-
if containsNet(newNode.net, t.net) {
78+
if ContainsNet(newNode.net, t.net) {
7979
// Replace the current top node and trim the tree
8080
newNode.setLeft(t.left.trimLeft(newNode))
8181
newNode.setRight(t.right.trimRight(newNode))
@@ -98,10 +98,10 @@ func (t *ipTree) contains(newNode *ipTree) bool {
9898
return false
9999
}
100100

101-
if containsNet(t.net, newNode.net) {
101+
if ContainsNet(t.net, newNode.net) {
102102
return true
103103
}
104-
if containsNet(newNode.net, t.net) {
104+
if ContainsNet(newNode.net, t.net) {
105105
return false
106106
}
107107
if bytes.Compare(newNode.net.IP, t.net.IP) < 0 {
@@ -161,10 +161,10 @@ func (t *ipTree) removeNet(net *net.IPNet) (top *ipTree) {
161161
}
162162

163163
top = t
164-
if containsNet(net, t.net) {
164+
if ContainsNet(net, t.net) {
165165
// Remove the current node
166166
top = t.remove()
167-
} else if containsNet(t.net, net) {
167+
} else if ContainsNet(t.net, net) {
168168
diff = netDifference(t.net, net)
169169
t.net = diff[0]
170170
for _, n := range diff[1:] {

net_utils.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,9 @@ func BroadcastAddr(n *net.IPNet) net.IP {
9292
return broadcast
9393
}
9494

95-
// containsNet returns true if net2 is a subset of net1. To be clear, it
95+
// ContainsNet returns true if net2 is a subset of net1. To be clear, it
9696
// returns true if net1 == net2 also.
97-
func containsNet(net1, net2 *net.IPNet) bool {
97+
func ContainsNet(net1, net2 *net.IPNet) bool {
9898
// If the two networks are different IP versions, return false
9999
if len(net1.IP) != len(net2.IP) {
100100
return false
@@ -117,11 +117,11 @@ func netDifference(a, b *net.IPNet) (result []*net.IPNet) {
117117
}
118118

119119
// If b contains a then the difference is empty
120-
if containsNet(b, a) {
120+
if ContainsNet(b, a) {
121121
return
122122
}
123123
// If a doesn't contain b then the difference is equal to a
124-
if !containsNet(a, b) {
124+
if !ContainsNet(a, b) {
125125
return []*net.IPNet{a}
126126
}
127127

0 commit comments

Comments
 (0)