Skip to content

Commit

Permalink
Merge pull request #14 from nranchev/ip_parse
Browse files Browse the repository at this point in the history
Use the IP address parser that Go provides
  • Loading branch information
nranchev committed Jun 29, 2017
2 parents c78e8bd + da13713 commit d6d4a9a
Showing 1 changed file with 11 additions and 31 deletions.
42 changes: 11 additions & 31 deletions libgeo.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ package libgeo

// Dependencies
import (
"encoding/binary"
"errors"
"net"
"os"
)

Expand Down Expand Up @@ -202,7 +204,7 @@ func Load(filename string) (gi *GeoIP, err error) {

// Lookup by IP address and return location
func (gi *GeoIP) GetLocationByIP(ip string) *Location {
return gi.GetLocationByIPNum(AddrToNum(ip))
return gi.GetLocationByIPNum(addrToNum(ip))
}

// Lookup by IP number and return location
Expand Down Expand Up @@ -319,36 +321,14 @@ func (gi *GeoIP) lookupByIPNum(ip uint32) int {
return 0
}

// Convert ip address to an int representation
func AddrToNum(ip string) uint32 {
octet := uint32(0)
ipnum := uint32(0)
i := 3
for j := 0; j < len(ip); j++ {
c := byte(ip[j])
if c == '.' {
if octet > 255 {
return 0
}
ipnum <<= 8
ipnum += octet
i--
octet = 0
} else {
t := octet
octet <<= 3
octet += t
octet += t
c -= '0'
if c > 9 {
return 0
}
octet += uint32(c)
}
func addrToNum(ip string) uint32 {
i := net.ParseIP(ip)
if i == nil {
return uint32(0)
}
if (octet > 255) || (i != 0) {
return 0
i = i.To4()
if i == nil {
return uint32(0)
}
ipnum <<= 8
return uint32(ipnum + octet)
return binary.BigEndian.Uint32(i)
}

0 comments on commit d6d4a9a

Please sign in to comment.