File tree Expand file tree Collapse file tree 2 files changed +21
-10
lines changed Expand file tree Collapse file tree 2 files changed +21
-10
lines changed Original file line number Diff line number Diff line change 1
1
package maxminddb
2
2
3
3
import (
4
+ "fmt"
4
5
"net"
5
6
)
6
7
@@ -51,6 +52,15 @@ func (r *Reader) Networks() *Networks {
51
52
// If the provided network is contained within a network in the database, the
52
53
// iterator will iterate over exactly one network, the containing network.
53
54
func (r * Reader ) NetworksWithin (network * net.IPNet ) * Networks {
55
+ if r .Metadata .IPVersion == 4 && network .IP .To4 () == nil {
56
+ return & Networks {
57
+ err : fmt .Errorf (
58
+ "error getting networks with '%s': you attempted to use an IPv6 network in an IPv4-only database" ,
59
+ network .String (),
60
+ ),
61
+ }
62
+ }
63
+
54
64
ip := network .IP
55
65
prefixLength , _ := network .Mask .Size ()
56
66
@@ -76,6 +86,9 @@ func (r *Reader) NetworksWithin(network *net.IPNet) *Networks {
76
86
// returns true if there is another network to be processed and false if there
77
87
// are no more networks or if there is an error.
78
88
func (n * Networks ) Next () bool {
89
+ if n .err != nil {
90
+ return false
91
+ }
79
92
for len (n .nodes ) > 0 {
80
93
node := n .nodes [len (n .nodes )- 1 ]
81
94
n .nodes = n .nodes [:len (n .nodes )- 1 ]
@@ -115,6 +128,9 @@ func (n *Networks) Next() bool {
115
128
// decoding the data for the network. It takes a pointer to a result value to
116
129
// decode the network's data into.
117
130
func (n * Networks ) Network (result interface {}) (* net.IPNet , error ) {
131
+ if n .err != nil {
132
+ return nil , n .err
133
+ }
118
134
if err := n .reader .retrieveData (n .lastNode .pointer , result ); err != nil {
119
135
return nil , err
120
136
}
Original file line number Diff line number Diff line change @@ -152,22 +152,17 @@ var tests = []networkTest{
152
152
},
153
153
},
154
154
{
155
- Network : "::/0 " ,
156
- Database : "ipv4 " ,
155
+ Network : "1.1.1.16/28 " ,
156
+ Database : "mixed " ,
157
157
Expected : []string {
158
- "101:101::/32" ,
159
- "101:102::/31" ,
160
- "101:104::/30" ,
161
- "101:108::/29" ,
162
- "101:110::/28" ,
163
- "101:120::/32" ,
158
+ "1.1.1.16/28" ,
164
159
},
165
160
},
166
161
{
167
- Network : "101:104:: /30" ,
162
+ Network : "1.1.1.4 /30" ,
168
163
Database : "ipv4" ,
169
164
Expected : []string {
170
- "101:104:: /30" ,
165
+ "1.1.1.4 /30" ,
171
166
},
172
167
},
173
168
}
You can’t perform that action at this time.
0 commit comments