From 698b00698ab45a7e6067aa019a8070b6f88f69d2 Mon Sep 17 00:00:00 2001 From: Damien Szczyt Date: Wed, 19 Jul 2017 15:24:04 +0200 Subject: [PATCH] Add method to get database infos --- geoip.go | 18 +++++++++++++++++- geoip_test.go | 11 +++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/geoip.go b/geoip.go index 4d264fd..49495ed 100644 --- a/geoip.go +++ b/geoip.go @@ -2,7 +2,7 @@ package geoip /* -#cgo pkg-config: geoip +#cgo pkg-config: geoip #include #include #include @@ -338,3 +338,19 @@ func (gi *GeoIP) GetCountry_v6(ip string) (cc string, netmask int) { } return } + +func (gi *GeoIP) GetDatabaseInfo() string { + if gi.db == nil { + return "" + } + + gi.mu.Lock() + defer gi.mu.Unlock() + + cdbinfo := C.GeoIP_database_info(gi.db) + defer C.free(unsafe.Pointer(cdbinfo)) + if cdbinfo != nil { + return C.GoString(cdbinfo) + } + return "" +} diff --git a/geoip_test.go b/geoip_test.go index c8695c1..894225e 100644 --- a/geoip_test.go +++ b/geoip_test.go @@ -122,3 +122,14 @@ func (s *GeoIPSuite) TestRegionName(c *C) { regionName = GetRegionName("CA", "ON") c.Check(regionName, Equals, "Ontario") } + +func (s *GeoIPSuite) TestDatabaseInfo(c *C) { + gi, err := Open("test-db/GeoIP.dat") + if err != nil { + fmt.Printf("Could not open GeoIP database: %s\n", err) + return + } + + databaseInfo := gi.GetDatabaseInfo() + c.Check(databaseInfo, Equals, "") +}