Skip to content

Commit

Permalink
Fixes for the major breaking API changes in the ipaddr module we use
Browse files Browse the repository at this point in the history
  • Loading branch information
oschwald committed Jan 14, 2016
1 parent 05e0f80 commit 7ba5b3f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ language: go
go:
- '1.2'
- '1.3'
- '1.4'
- '1.5'
- release
- tip
notifications:
email:
Expand Down
31 changes: 19 additions & 12 deletions convert/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

type headerFunc func([]string) []string
type lineFunc func(ipaddr.Prefix, []string) []string
type lineFunc func(*ipaddr.Prefix, []string) []string

// ConvertFile converts the MaxMind GeoIP2 or GeoLite2 CSV file `inputFile` to
// `outputFile` file using a different representation of the network. The
Expand Down Expand Up @@ -53,7 +53,7 @@ func Convert(
) error {

makeHeader := func(orig []string) []string { return orig }
makeLine := func(_ ipaddr.Prefix, orig []string) []string { return orig }
makeLine := func(_ *ipaddr.Prefix, orig []string) []string { return orig }

if intRange {
makeHeader = addHeaderFunc(makeHeader, intRangeHeader)
Expand All @@ -80,7 +80,7 @@ func addHeaderFunc(first headerFunc, second headerFunc) headerFunc {
}

func addLineFunc(first lineFunc, second lineFunc) lineFunc {
return func(network ipaddr.Prefix, line []string) []string {
return func(network *ipaddr.Prefix, line []string) []string {
return second(network, first(network, line))
}
}
Expand All @@ -89,17 +89,17 @@ func cidrHeader(orig []string) []string {
return append([]string{"network"}, orig...)
}

func cidrLine(network ipaddr.Prefix, orig []string) []string {
func cidrLine(network *ipaddr.Prefix, orig []string) []string {
return append([]string{network.String()}, orig...)
}

func rangeHeader(orig []string) []string {
return append([]string{"network_start_ip", "network_last_ip"}, orig...)
}

func rangeLine(network ipaddr.Prefix, orig []string) []string {
func rangeLine(network *ipaddr.Prefix, orig []string) []string {
return append(
[]string{network.Addr().String(), network.LastAddr().String()},
[]string{network.IP.String(), network.Last().String()},
orig...,
)
}
Expand All @@ -108,19 +108,27 @@ func intRangeHeader(orig []string) []string {
return append([]string{"network_start_integer", "network_last_integer"}, orig...)
}

func intRangeLine(network ipaddr.Prefix, orig []string) []string {
func intRangeLine(network *ipaddr.Prefix, orig []string) []string {
startInt := new(big.Int)
startInt.SetBytes(network.Addr())

startInt.SetBytes(canonicalizeIP(network.IP))

endInt := new(big.Int)
endInt.SetBytes(network.LastAddr())
endInt.SetBytes(canonicalizeIP(network.Last()))

return append(
[]string{startInt.String(), endInt.String()},
orig...,
)
}

func canonicalizeIP(ip net.IP) net.IP {
if v4 := ip.To4(); v4 != nil {
return v4
}
return ip
}

func convert(
input io.Reader,
output io.Writer,
Expand Down Expand Up @@ -163,11 +171,10 @@ func convert(
return nil
}

func makePrefix(network string) (ipaddr.Prefix, error) {
func makePrefix(network string) (*ipaddr.Prefix, error) {
_, ipn, err := net.ParseCIDR(network)
if err != nil {
return nil, err
}
nbits, _ := ipn.Mask.Size()
return ipaddr.NewPrefix(ipn.IP, nbits)
return ipaddr.NewPrefix(ipn), nil
}
3 changes: 2 additions & 1 deletion dev-bin/package.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ do
done

git tag -a $VERSION
git push -u --tags
git push --tags
git push -u

0 comments on commit 7ba5b3f

Please sign in to comment.