Skip to content

Commit 288208c

Browse files
committed
Skip aliased networks by default
1 parent 1e6c614 commit 288208c

File tree

3 files changed

+16
-23
lines changed

3 files changed

+16
-23
lines changed

example_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func ExampleReader_Networks() {
6767
Domain string `maxminddb:"connection_type"`
6868
}{}
6969

70-
networks := db.Networks(maxminddb.SkipAliasedNetworks)
70+
networks := db.Networks()
7171
for networks.Next() {
7272
subnet, err := networks.Network(&record)
7373
if err != nil {
@@ -123,7 +123,7 @@ func ExampleReader_NetworksWithin() {
123123
log.Panic(err)
124124
}
125125

126-
networks := db.NetworksWithin(prefix, maxminddb.SkipAliasedNetworks)
126+
networks := db.NetworksWithin(prefix)
127127
for networks.Next() {
128128
subnet, err := networks.Network(&record)
129129
if err != nil {

traverse.go

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ type netNode struct {
1414

1515
// Networks represents a set of subnets that we are iterating over.
1616
type Networks struct {
17-
err error
18-
reader *Reader
19-
nodes []netNode
20-
lastNode netNode
21-
skipAliasedNetworks bool
17+
err error
18+
reader *Reader
19+
nodes []netNode
20+
lastNode netNode
21+
includeAliasedNetworks bool
2222
}
2323

2424
var (
@@ -29,23 +29,20 @@ var (
2929
// NetworksOption are options for Networks and NetworksWithin.
3030
type NetworksOption func(*Networks)
3131

32-
// SkipAliasedNetworks is an option for Networks and NetworksWithin that
33-
// makes them not iterate over aliases of the IPv4 subtree in an IPv6
32+
// IncludeAliasedNetworks is an option for Networks and NetworksWithin
33+
// that makes them iterate over aliases of the IPv4 subtree in an IPv6
3434
// database, e.g., ::ffff:0:0/96, 2001::/32, and 2002::/16.
35-
//
36-
// You most likely want to set this. The only reason it isn't the default
37-
// behavior is to provide backwards compatibility to existing users.
38-
func SkipAliasedNetworks(networks *Networks) {
39-
networks.skipAliasedNetworks = true
35+
func IncludeAliasedNetworks(networks *Networks) {
36+
networks.includeAliasedNetworks = true
4037
}
4138

4239
// Networks returns an iterator that can be used to traverse all networks in
4340
// the database.
4441
//
4542
// Please note that a MaxMind DB may map IPv4 networks into several locations
46-
// in an IPv6 database. This iterator will iterate over all of these locations
47-
// separately. To only iterate over the IPv4 networks once, use the
48-
// SkipAliasedNetworks option.
43+
// in an IPv6 database. This iterator will only iterate over these once by
44+
// default. To iterate over all the IPv4 network locations, use the
45+
// IncludeAliasedNetworks option.
4946
func (r *Reader) Networks(options ...NetworksOption) *Networks {
5047
var networks *Networks
5148
if r.Metadata.IPVersion == 6 {
@@ -122,7 +119,7 @@ func (n *Networks) Next() bool {
122119
for node.pointer != n.reader.Metadata.NodeCount {
123120
// This skips IPv4 aliases without hardcoding the networks that the writer
124121
// currently aliases.
125-
if n.skipAliasedNetworks && n.reader.ipv4Start != 0 &&
122+
if !n.includeAliasedNetworks && n.reader.ipv4Start != 0 &&
126123
node.pointer == n.reader.ipv4Start && !isInIPv4Subtree(node.ip) {
127124
break
128125
}

traverse_test.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,6 @@ var tests = []networkTest{
140140
Expected: []string{
141141
"::1:ffff:ffff/128",
142142
},
143-
Options: []NetworksOption{SkipAliasedNetworks},
144143
},
145144
{
146145
Network: "::/0",
@@ -152,7 +151,6 @@ var tests = []networkTest{
152151
"::2:0:50/125",
153152
"::2:0:58/127",
154153
},
155-
Options: []NetworksOption{SkipAliasedNetworks},
156154
},
157155
{
158156
Network: "::2:0:40/123",
@@ -162,7 +160,6 @@ var tests = []networkTest{
162160
"::2:0:50/125",
163161
"::2:0:58/127",
164162
},
165-
Options: []NetworksOption{SkipAliasedNetworks},
166163
},
167164
{
168165
Network: "0:0:0:0:0:ffff:ffff:ff00/120",
@@ -192,7 +189,6 @@ var tests = []networkTest{
192189
"1.1.1.16/28",
193190
"1.1.1.32/32",
194191
},
195-
Options: []NetworksOption{SkipAliasedNetworks},
196192
},
197193
{
198194
Network: "::/0",
@@ -228,6 +224,7 @@ var tests = []networkTest{
228224
"2002:101:110::/44",
229225
"2002:101:120::/48",
230226
},
227+
Options: []NetworksOption{IncludeAliasedNetworks},
231228
},
232229
{
233230
Network: "::/0",
@@ -245,7 +242,6 @@ var tests = []networkTest{
245242
"::2:0:50/125",
246243
"::2:0:58/127",
247244
},
248-
Options: []NetworksOption{SkipAliasedNetworks},
249245
},
250246
{
251247
Network: "1.1.1.16/28",

0 commit comments

Comments
 (0)