From 31328e916ea204913000d0089fb328ca037e4bc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ask=20Bj=C3=B8rn=20Hansen?= Date: Thu, 17 Oct 2013 22:40:38 -0700 Subject: [PATCH 01/13] Ignore *~ --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 66e60fc..6954e46 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ geoip-demo +*~ *.dat # Compiled Object files, Static and Dynamic libs (Shared Objects) From d15be817163cb8cf5d7fbb574c751bdb6afbfceb Mon Sep 17 00:00:00 2001 From: Dave Marchevsky Date: Wed, 23 Oct 2013 17:17:52 +0000 Subject: [PATCH 02/13] Add GeoIPOptions const section and GeoIPOptions flag param to Open(). --- const.go | 10 ++++++++++ geoip.go | 25 +++++++++++++++---------- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/const.go b/const.go index 3eaef3f..9c8d0be 100644 --- a/const.go +++ b/const.go @@ -1,5 +1,6 @@ package geoip +// GeoIPDBTypes enum in GeoIP.h const ( GEOIP_COUNTRY_EDITION = 1 GEOIP_REGION_EDITION_REV0 = 7 @@ -33,3 +34,12 @@ const ( GEOIP_NETSPEED_EDITION_REV1 = 32 GEOIP_NETSPEED_EDITION_REV1_V6 = 33 ) + +// GeoIPOptions enum in GeoIP.h +const ( + GEOIP_STANDARD = 0 + GEOIP_MEMORY_CACHE = 1 + GEOIP_CHECK_CACHE = 2 + GEOIP_INDEX_CACHE = 4 + GEOIP_MMAP_CACHE = 8 +) diff --git a/geoip.go b/geoip.go index 22eee0e..4549265 100644 --- a/geoip.go +++ b/geoip.go @@ -46,13 +46,17 @@ func (gi *GeoIP) free() { return } -// Opens a GeoIP database by filename, all formats supported by libgeoip are -// supported though there are only functions to access some of the databases in this API. -// The database is opened in MEMORY_CACHE mode, if you need to optimize for memory -// instead of performance you should change this. +// Default convenience wrapper around OpenDb +func Open(files ...string) (*GeoIP, error) { + return OpenDb(files, GEOIP_MEMORY_CACHE) +} + +// Opens a GeoIP database by filename with specified GeoIPOptions flag. +// All formats supported by libgeoip are supported though there are only +// functions to access some of the databases in this API. // If you don't pass a filename, it will try opening the database from // a list of common paths. -func Open(files ...string) (*GeoIP, error) { +func OpenDb(files []string, flag int) (*GeoIP, error) { if len(files) == 0 { files = []string{ "/usr/share/GeoIP/GeoIP.dat", // Linux default @@ -81,7 +85,7 @@ func Open(files ...string) (*GeoIP, error) { cbase := C.CString(file) defer C.free(unsafe.Pointer(cbase)) - g.db, err = C.GeoIP_open(cbase, C.GEOIP_MEMORY_CACHE) + g.db, err = C.GeoIP_open(cbase, C.int(flag)) if g.db != nil && err != nil { break } @@ -107,15 +111,16 @@ func SetCustomDirectory(dir string) { C.GeoIP_setup_custom_directory(cdir) } -// OpenType opens a specified GeoIP database type in the default location. Constants -// are defined for each database type (for example GEOIP_COUNTRY_EDITION). -func OpenType(dbType int) (*GeoIP, error) { +// OpenType opens a specified GeoIP database type in the default location with the +// specified GeoIPOptions flag. Constants are defined for each database type +// (for example GEOIP_COUNTRY_EDITION). +func OpenType(dbType int, flag int) (*GeoIP, error) { g := &GeoIP{} runtime.SetFinalizer(g, (*GeoIP).free) var err error - g.db, err = C.GeoIP_open_type(C.int(dbType), C.GEOIP_MEMORY_CACHE) + g.db, err = C.GeoIP_open_type(C.int(dbType), C.int(flag)) if err != nil { return nil, fmt.Errorf("Error opening GeoIP database (%d): %s", dbType, err) } From 86a22a60741caffaffb3fa0e11aa3e5ac0ab45e9 Mon Sep 17 00:00:00 2001 From: Dave Marchevsky Date: Wed, 23 Oct 2013 21:44:38 +0000 Subject: [PATCH 03/13] Fix test that I broke --- geoip_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/geoip_test.go b/geoip_test.go index e23f84e..01a19f4 100644 --- a/geoip_test.go +++ b/geoip_test.go @@ -37,7 +37,7 @@ func (s *GeoIPSuite) TestOpenType(c *C) { // SetCustomDirectory("/Users/ask/go/src/geoip/db") // Open Country database - gi, err := OpenType(GEOIP_COUNTRY_EDITION) + gi, err := OpenType(GEOIP_COUNTRY_EDITION, GEOIP_MEMORY_CACHE) c.Check(err, IsNil) c.Assert(gi, NotNil) country, _ := gi.GetCountry("207.171.7.51") From 7be32187cd78298b388859d675a5c435914c6d00 Mon Sep 17 00:00:00 2001 From: Dave Marchevsky Date: Tue, 22 Oct 2013 22:42:44 +0000 Subject: [PATCH 04/13] Add DMACode and MetroCode fields to GeoIPRecord and only get AreaCode if db version > 0 --- geoip.go | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/geoip.go b/geoip.go index 22eee0e..2d77d11 100644 --- a/geoip.go +++ b/geoip.go @@ -161,15 +161,15 @@ func (gi *GeoIP) GetName(ip string) (name string, netmask int) { } type GeoIPRecord struct { - CountryCode string - CountryCode3 string - CountryName string - Region string - City string - PostalCode string - Latitude float32 - Longitude float32 - // DMACode int + CountryCode string + CountryCode3 string + CountryName string + Region string + City string + PostalCode string + Latitude float32 + Longitude float32 + MetroCode int AreaCode int CharSet int ContinentCode string @@ -203,10 +203,19 @@ func (gi *GeoIP) GetRecord(ip string) *GeoIPRecord { rec.PostalCode = C.GoString(record.postal_code) rec.Latitude = float32(record.latitude) rec.Longitude = float32(record.longitude) - rec.AreaCode = int(record.area_code) 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 } From ac9f3e718723ecdb8a1954ef06879391f007caeb Mon Sep 17 00:00:00 2001 From: Christian Muehlhaeuser Date: Thu, 24 Jul 2014 04:46:04 +0200 Subject: [PATCH 05/13] Use pkg-config to retrieve LDFLAGS and CFLAGS for geoip. Tested on Linux and OSX. This gets rid of warnings about non-existing directories (like /opt on OSX). --- geoip.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/geoip.go b/geoip.go index 22eee0e..6e0f760 100644 --- a/geoip.go +++ b/geoip.go @@ -2,8 +2,7 @@ package geoip /* -#cgo CFLAGS: -I/opt/local/include -I/usr/local/include -I/usr/include -#cgo LDFLAGS: -lGeoIP -L/opt/local/lib -L/usr/local/lib -L/usr/lib +#cgo pkg-config: geoip #include #include #include From ffcbdd8b2e038b85a8d976af668b218dde5a6336 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ask=20Bj=C3=B8rn=20Hansen?= Date: Sun, 4 Jan 2015 20:03:51 +0100 Subject: [PATCH 06/13] Update gocheck url --- geoip_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/geoip_test.go b/geoip_test.go index e23f84e..1c27362 100644 --- a/geoip_test.go +++ b/geoip_test.go @@ -2,8 +2,9 @@ package geoip import ( "fmt" - . "launchpad.net/gocheck" "testing" + + . "gopkg.in/check.v1" ) // Hook up gocheck into the gotest runner. From 55d051f928cee26f9389c1faf0ab628244a9c8dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ask=20Bj=C3=B8rn=20Hansen?= Date: Sun, 4 Jan 2015 19:00:49 -0800 Subject: [PATCH 07/13] Fix gocheck url in travis configuration --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index f13abf6..f92ace7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,7 +4,7 @@ before_install: install: - mkdir -p $TRAVIS_BUILD_DIR/db - curl http://geodns.bitnames.com/geoip/GeoLiteCity.dat.gz | gzip -cd > $TRAVIS_BUILD_DIR/db/GeoLiteCity.dat - - go get launchpad.net/gocheck + - go get gopkg.in/check.v1 script: - cd $TRAVIS_BUILD_DIR && go test -gocheck.v - go test -gocheck.v -gocheck.b -gocheck.btime=2s From 99efcb2de3721f85617d32ea7ef8165b74926f83 Mon Sep 17 00:00:00 2001 From: Gregory Oschwald Date: Wed, 4 Feb 2015 20:10:26 -0800 Subject: [PATCH 08/13] Use fixed test databases. Test on 1.3, 1.4, and tip --- .gitignore | 2 +- .travis.yml | 8 ++++++++ geoip_test.go | 40 +++++++++++++++++++++++++++------------- test-db/GeoIP.dat | Bin 0 -> 3315 bytes test-db/GeoIPCity.dat | Bin 0 -> 4065 bytes test-db/GeoIPRegion.dat | Bin 0 -> 184 bytes 6 files changed, 36 insertions(+), 14 deletions(-) create mode 100644 test-db/GeoIP.dat create mode 100644 test-db/GeoIPCity.dat create mode 100644 test-db/GeoIPRegion.dat diff --git a/.gitignore b/.gitignore index 6954e46..0fa9e49 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,6 @@ geoip-demo *~ -*.dat +/db/GeoLiteCity.dat # Compiled Object files, Static and Dynamic libs (Shared Objects) *.o diff --git a/.travis.yml b/.travis.yml index f92ace7..4c1fd43 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,10 +1,18 @@ language: go + +go: + - 1.3 + - 1.4 + - tip + before_install: - sudo apt-get install libgeoip-dev bzr + install: - mkdir -p $TRAVIS_BUILD_DIR/db - curl http://geodns.bitnames.com/geoip/GeoLiteCity.dat.gz | gzip -cd > $TRAVIS_BUILD_DIR/db/GeoLiteCity.dat - go get gopkg.in/check.v1 + script: - cd $TRAVIS_BUILD_DIR && go test -gocheck.v - go test -gocheck.v -gocheck.b -gocheck.btime=2s diff --git a/geoip_test.go b/geoip_test.go index 1c27362..9b99cff 100644 --- a/geoip_test.go +++ b/geoip_test.go @@ -24,13 +24,13 @@ func (s *GeoIPSuite) Testv4(c *C) { c.Check(gi, NotNil) - country, netmask := gi.GetCountry("207.171.7.51") + country, netmask := gi.GetCountry("64.17.254.216") c.Check(country, Equals, "US") - c.Check(netmask, Equals, 15) + c.Check(netmask, Equals, 17) - country, netmask = gi.GetCountry("149.20.64.42") - c.Check(country, Equals, "US") - c.Check(netmask, Equals, 13) + country, netmask = gi.GetCountry("222.230.136.0") + c.Check(country, Equals, "JP") + c.Check(netmask, Equals, 16) } func (s *GeoIPSuite) TestOpenType(c *C) { @@ -58,7 +58,7 @@ func (s *GeoIPSuite) Benchmark_GetCountry(c *C) { } func (s *GeoIPSuite) Testv4Record(c *C) { - gi, err := Open("db/GeoLiteCity.dat") + gi, err := Open("test-db/GeoIPCity.dat") if gi == nil || err != nil { fmt.Printf("Could not open GeoIP database: %s\n", err) return @@ -66,16 +66,30 @@ func (s *GeoIPSuite) Testv4Record(c *C) { c.Check(gi, NotNil) - record := gi.GetRecord("207.171.7.51") + record := gi.GetRecord("66.92.181.240") c.Assert(record, NotNil) - c.Check(record.CountryCode, Equals, "US") - fmt.Printf("Record: %#v\n", record) - + c.Check( + *record, + Equals, + GeoIPRecord{ + CountryCode: "US", + CountryCode3: "USA", + CountryName: "United States", + Region: "CA", + City: "Fremont", + PostalCode: "94538", + Latitude: 37.5079, + Longitude: -121.96, + AreaCode: 510, + CharSet: 1, + ContinentCode: "NA", + }, + ) } func (s *GeoIPSuite) Benchmark_GetRecord(c *C) { - gi, err := Open("db/GeoIPCity.dat") + gi, err := Open("db/GeoLiteCity.dat") if gi == nil || err != nil { fmt.Printf("Could not open GeoIP database: %s\n", err) return @@ -90,13 +104,13 @@ func (s *GeoIPSuite) Benchmark_GetRecord(c *C) { } func (s *GeoIPSuite) Testv4Region(c *C) { - gi, err := Open("db/GeoIPRegion.dat") + gi, err := Open("test-db/GeoIPRegion.dat") if gi == nil || err != nil { fmt.Printf("Could not open GeoIP database: %s\n", err) return } - country, region := gi.GetRegion("207.171.7.51") + country, region := gi.GetRegion("64.17.254.223") c.Check(country, Equals, "US") c.Check(region, Equals, "CA") } diff --git a/test-db/GeoIP.dat b/test-db/GeoIP.dat new file mode 100644 index 0000000000000000000000000000000000000000..5374dff90b4f6cc59cbfed9cbed624254389ccb2 GIT binary patch literal 3315 zcmXw*1+*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dVn^bhBhQ5B_@tSyK7vZzy`uFHC| zzHA^H%0{wGSg}Im^d=RW>di7~uC%DoQfZZWYo*+E+f3Ri?PZ4w9hFX5T}kO&p-U!R YD|FMl%O08ctk6sEEk{`A9hSV+yDRo literal 0 HcmV?d00001 diff --git a/test-db/GeoIPCity.dat b/test-db/GeoIPCity.dat new file mode 100644 index 0000000000000000000000000000000000000000..230526c65dde42c5dd55a8dfca27acd77c30f449 GIT binary patch literal 4065 zcmY+G2~<>d8^)jaf(nY@z9BL&DsJSk3=VCGD5zm7klJRAFv93KgTtUvT3Bhe+MZ^X znpSGIX;!wVrRI`rWtyd-nQ3m7nOpMx=jpuX`@VDT`JLZ=-v52?JNMq1xdK=~1P}>) zDELUw0O)C*+i=`KvVTtAWpp*r8(IGXsLNCN^7zW&{p$ylwKA( z0P#RaAOUDCXd~zZBm&!k9e@o;0v-h(1Cjwd-~q-0oq4#6>_T=W&HO1qH+pxnhvq#g zy~tGZDzZ1}Ak)ZnAcJDMWm23#mL`2D11w|%R|D5*K8$iLaGfUCQ*y|@K(6NfD0yUm zasZG|DIm>x2T}%+gMk|~FQg11Zv=*FUTTGJ<#3=#lVYHReiJ!@97&EMZzgXcZzWBK z+bFjKqcyn$xKrH?jG>p3(=3z$hUUO>CU@(~3c#yAjxwI~0TU>GtpFc z!mH}5fko6^&Sns1?OCBGuK0VjDo%oW?oq#oD> z>;}H(?s4)P@>_Bb`5n2J+(&*-nm+r1ALu`l2Q)tj{6w$Q~Y@(X#C{FO94 zkC|W5i11@$w%>pg%uj0d6y-Es503j_hf;3?yrGcQKCXED9^k_{Q3!2bl$fladQsM;7G-*z0A!w;dD?wk*&{oh+ z;A7ri&_OUt*Tf4Pf;2%#CJAIGGLf{ANu-%MSzxDk)})J|D?Nqmrg?Wk5A~juUV>Cj zt`hX77jeRL&HGa_$V`Egl126*%?vI|w%}^YHRQEgy-sjFJ%{Y8d9I+JdLE^Wryd~4 z=Wc;k2T}%+g9SHG3dtd)nc+skQ2H=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0 HcmV?d00001 From 512713db984e1e98e684b10984eccf0ee8efc6c2 Mon Sep 17 00:00:00 2001 From: Gregory Oschwald Date: Wed, 4 Feb 2015 20:22:29 -0800 Subject: [PATCH 09/13] Fix OpenType test --- geoip_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/geoip_test.go b/geoip_test.go index 9b99cff..279395f 100644 --- a/geoip_test.go +++ b/geoip_test.go @@ -35,14 +35,14 @@ func (s *GeoIPSuite) Testv4(c *C) { func (s *GeoIPSuite) TestOpenType(c *C) { - // SetCustomDirectory("/Users/ask/go/src/geoip/db") + SetCustomDirectory("test-db") // Open Country database gi, err := OpenType(GEOIP_COUNTRY_EDITION) c.Check(err, IsNil) c.Assert(gi, NotNil) - country, _ := gi.GetCountry("207.171.7.51") - c.Check(country, Equals, "US") + country, _ := gi.GetCountry("81.2.69.160") + c.Check(country, Equals, "GB") } func (s *GeoIPSuite) Benchmark_GetCountry(c *C) { From 7c948f3c9221d4d1492ffb48292f0e7b0f3671c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ask=20Bj=C3=B8rn=20Hansen?= Date: Sat, 7 Feb 2015 16:15:35 -0800 Subject: [PATCH 10/13] Fix tests (multiple merges got out of sync) --- geoip_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/geoip_test.go b/geoip_test.go index 737089d..a82e444 100644 --- a/geoip_test.go +++ b/geoip_test.go @@ -81,6 +81,7 @@ func (s *GeoIPSuite) Testv4Record(c *C) { Latitude: 37.5079, Longitude: -121.96, AreaCode: 510, + MetroCode: 807, CharSet: 1, ContinentCode: "NA", }, From c4fcb311fd2291421fbe177c47c61d61d5d1e137 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ask=20Bj=C3=B8rn=20Hansen?= Date: Sat, 7 Feb 2015 16:16:16 -0800 Subject: [PATCH 11/13] Whitespace cleanup from 'go fmt' --- geoip.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/geoip.go b/geoip.go index b1032e3..cd44a35 100644 --- a/geoip.go +++ b/geoip.go @@ -51,8 +51,8 @@ func Open(files ...string) (*GeoIP, error) { return OpenDb(files, GEOIP_MEMORY_CACHE) } -// Opens a GeoIP database by filename with specified GeoIPOptions flag. -// All formats supported by libgeoip are supported though there are only +// Opens a GeoIP database by filename with specified GeoIPOptions flag. +// All formats supported by libgeoip are supported though there are only // functions to access some of the databases in this API. // If you don't pass a filename, it will try opening the database from // a list of common paths. @@ -111,8 +111,8 @@ func SetCustomDirectory(dir string) { C.GeoIP_setup_custom_directory(cdir) } -// OpenType opens a specified GeoIP database type in the default location with the -// specified GeoIPOptions flag. Constants are defined for each database type +// OpenType opens a specified GeoIP database type in the default location with the +// specified GeoIPOptions flag. Constants are defined for each database type // (for example GEOIP_COUNTRY_EDITION). func OpenType(dbType int, flag int) (*GeoIP, error) { g := &GeoIP{} @@ -212,7 +212,7 @@ func (gi *GeoIP) GetRecord(ip string) *GeoIPRecord { rec.ContinentCode = C.GoString(record.continent_code) if gi.db.databaseType != C.GEOIP_CITY_EDITION_REV0 { - /* DIRTY HACK BELOW: + /* 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. */ From 917745c7ac5c2f92c815ed169cc1680474700951 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ask=20Bj=C3=B8rn=20Hansen?= Date: Thu, 19 Feb 2015 00:10:15 -0800 Subject: [PATCH 12/13] Preserve OpenType() API until we bump the package version --- geoip.go | 8 +++++++- geoip_test.go | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/geoip.go b/geoip.go index cd44a35..92e2954 100644 --- a/geoip.go +++ b/geoip.go @@ -114,7 +114,7 @@ func SetCustomDirectory(dir string) { // OpenType opens a specified GeoIP database type in the default location with the // specified GeoIPOptions flag. Constants are defined for each database type // (for example GEOIP_COUNTRY_EDITION). -func OpenType(dbType int, flag int) (*GeoIP, error) { +func OpenTypeFlag(dbType int, flag int) (*GeoIP, error) { g := &GeoIP{} runtime.SetFinalizer(g, (*GeoIP).free) @@ -134,6 +134,12 @@ func OpenType(dbType int, flag int) (*GeoIP, error) { return g, nil } +// OpenType opens a specified GeoIP database type in the default location +// and the 'memory cache' flag. Use OpenTypeFlag() to specify flag. +func OpenType(dbType int) (*GeoIP, error) { + return OpenTypeFlag(dbType, GEOIP_MEMORY_CACHE) +} + // Takes an IPv4 address string and returns the organization name for that IP. // Requires the GeoIP organization database. func (gi *GeoIP) GetOrg(ip string) string { diff --git a/geoip_test.go b/geoip_test.go index a82e444..c8695c1 100644 --- a/geoip_test.go +++ b/geoip_test.go @@ -38,7 +38,7 @@ func (s *GeoIPSuite) TestOpenType(c *C) { SetCustomDirectory("test-db") // Open Country database - gi, err := OpenType(GEOIP_COUNTRY_EDITION, GEOIP_MEMORY_CACHE) + gi, err := OpenType(GEOIP_COUNTRY_EDITION) c.Check(err, IsNil) c.Assert(gi, NotNil) country, _ := gi.GetCountry("81.2.69.160") From ddf946996c061238c06d6924f0a31ff04a88b742 Mon Sep 17 00:00:00 2001 From: Gregory McCue Date: Tue, 10 May 2016 16:00:59 +0200 Subject: [PATCH 13/13] Implement `go vet` fix. --- ex/geoip-demo.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ex/geoip-demo.go b/ex/geoip-demo.go index bbafd1e..e2cde8d 100644 --- a/ex/geoip-demo.go +++ b/ex/geoip-demo.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "github.com/abh/geoip" ) @@ -54,7 +55,7 @@ func main() { } -func test4(g geoip.GeoIP, ip string) { +func test4(g *geoip.GeoIP, ip string) { test(func(s string) (string, int) { return g.GetCountry(s) }, ip) }