@@ -11,7 +11,7 @@ import (
11
11
)
12
12
13
13
type headerFunc func ([]string ) []string
14
- type lineFunc func (ipaddr.Prefix , []string ) []string
14
+ type lineFunc func (* ipaddr.Prefix , []string ) []string
15
15
16
16
// ConvertFile converts the MaxMind GeoIP2 or GeoLite2 CSV file `inputFile` to
17
17
// `outputFile` file using a different representation of the network. The
@@ -53,7 +53,7 @@ func Convert(
53
53
) error {
54
54
55
55
makeHeader := func (orig []string ) []string { return orig }
56
- makeLine := func (_ ipaddr.Prefix , orig []string ) []string { return orig }
56
+ makeLine := func (_ * ipaddr.Prefix , orig []string ) []string { return orig }
57
57
58
58
if intRange {
59
59
makeHeader = addHeaderFunc (makeHeader , intRangeHeader )
@@ -80,7 +80,7 @@ func addHeaderFunc(first headerFunc, second headerFunc) headerFunc {
80
80
}
81
81
82
82
func addLineFunc (first lineFunc , second lineFunc ) lineFunc {
83
- return func (network ipaddr.Prefix , line []string ) []string {
83
+ return func (network * ipaddr.Prefix , line []string ) []string {
84
84
return second (network , first (network , line ))
85
85
}
86
86
}
@@ -89,17 +89,17 @@ func cidrHeader(orig []string) []string {
89
89
return append ([]string {"network" }, orig ... )
90
90
}
91
91
92
- func cidrLine (network ipaddr.Prefix , orig []string ) []string {
92
+ func cidrLine (network * ipaddr.Prefix , orig []string ) []string {
93
93
return append ([]string {network .String ()}, orig ... )
94
94
}
95
95
96
96
func rangeHeader (orig []string ) []string {
97
97
return append ([]string {"network_start_ip" , "network_last_ip" }, orig ... )
98
98
}
99
99
100
- func rangeLine (network ipaddr.Prefix , orig []string ) []string {
100
+ func rangeLine (network * ipaddr.Prefix , orig []string ) []string {
101
101
return append (
102
- []string {network .Addr () .String (), network .LastAddr ().String ()},
102
+ []string {network .IP .String (), network .Last ().String ()},
103
103
orig ... ,
104
104
)
105
105
}
@@ -108,19 +108,27 @@ func intRangeHeader(orig []string) []string {
108
108
return append ([]string {"network_start_integer" , "network_last_integer" }, orig ... )
109
109
}
110
110
111
- func intRangeLine (network ipaddr.Prefix , orig []string ) []string {
111
+ func intRangeLine (network * ipaddr.Prefix , orig []string ) []string {
112
112
startInt := new (big.Int )
113
- startInt .SetBytes (network .Addr ())
113
+
114
+ startInt .SetBytes (canonicalizeIP (network .IP ))
114
115
115
116
endInt := new (big.Int )
116
- endInt .SetBytes (network .LastAddr ( ))
117
+ endInt .SetBytes (canonicalizeIP ( network .Last () ))
117
118
118
119
return append (
119
120
[]string {startInt .String (), endInt .String ()},
120
121
orig ... ,
121
122
)
122
123
}
123
124
125
+ func canonicalizeIP (ip net.IP ) net.IP {
126
+ if v4 := ip .To4 (); v4 != nil {
127
+ return v4
128
+ }
129
+ return ip
130
+ }
131
+
124
132
func convert (
125
133
input io.Reader ,
126
134
output io.Writer ,
@@ -163,11 +171,10 @@ func convert(
163
171
return nil
164
172
}
165
173
166
- func makePrefix (network string ) (ipaddr.Prefix , error ) {
174
+ func makePrefix (network string ) (* ipaddr.Prefix , error ) {
167
175
_ , ipn , err := net .ParseCIDR (network )
168
176
if err != nil {
169
177
return nil , err
170
178
}
171
- nbits , _ := ipn .Mask .Size ()
172
- return ipaddr .NewPrefix (ipn .IP , nbits )
179
+ return ipaddr .NewPrefix (ipn ), nil
173
180
}
0 commit comments