Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GetRecordV6 #29

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions geoip.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,50 @@ func (gi *GeoIP) GetRecord(ip string) *GeoIPRecord {
return rec
}

// Returns the "City Record" for an IP address. Requires the GeoCity(Lite)
// database - http://www.maxmind.com/en/city
func (gi *GeoIP) GetRecordV6(ip string) *GeoIPRecord {
if gi.db == nil {
return nil
}

cip := C.CString(ip)
defer C.free(unsafe.Pointer(cip))

gi.mu.Lock()
record := C.GeoIP_record_by_addr_v6(gi.db, cip)
gi.mu.Unlock()

if record == nil {
return nil
}
// defer C.free(unsafe.Pointer(record))
defer C.GeoIPRecord_delete(record)
rec := new(GeoIPRecord)
rec.CountryCode = C.GoString(record.country_code)
rec.CountryCode3 = C.GoString(record.country_code3)
rec.CountryName = C.GoString(record.country_name)
rec.Region = C.GoString(record.region)
rec.City = C.GoString(record.city)
rec.PostalCode = C.GoString(record.postal_code)
rec.Latitude = float32(record.latitude)
rec.Longitude = float32(record.longitude)
rec.CharSet = int(record.charset)
rec.ContinentCode = C.GoString(record.continent_code)

if gi.db.databaseType != C.GEOIP_CITY_EDITION_REV0 {
/* DIRTY HACK BELOW:
The GeoIPRecord struct in GeoIPCity.h contains an int32 union of metro_code and dma_code.
The union is unnamed, so cgo names it anon0 and assumes it's a 4-byte array.
*/
union_int := (*int32)(unsafe.Pointer(&record.anon0))
rec.MetroCode = int(*union_int)
rec.AreaCode = int(record.area_code)
}

return rec
}

// Returns the country code and region code for an IP address. Requires
// the GeoIP Region database.
func (gi *GeoIP) GetRegion(ip string) (string, string) {
Expand Down
31 changes: 31 additions & 0 deletions geoip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,37 @@ func (s *GeoIPSuite) Testv4Record(c *C) {
)
}

func (s *GeoIPSuite) Testv6Record(c *C) {
gi, err := Open("test-db/GeoLiteCityv6.dat")
if gi == nil || err != nil {
fmt.Printf("Could not open GeoIP database: %s\n", err)
return
}

c.Check(gi, NotNil)

record := gi.GetRecordV6("2001:208::")
c.Assert(record, NotNil)
c.Check(
*record,
Equals,
GeoIPRecord{
CountryCode: "SG",
CountryCode3: "SGP",
CountryName: "Singapore",
Region: "",
City: "",
PostalCode: "",
Latitude: 1.3667,
Longitude: 103.8,
AreaCode: 0,
MetroCode: 0,
CharSet: 1,
ContinentCode: "AS",
},
)
}

func (s *GeoIPSuite) Benchmark_GetRecord(c *C) {

gi, err := Open("db/GeoLiteCity.dat")
Expand Down
Binary file added test-db/GeoLiteCityv6.dat
Binary file not shown.