Skip to content

Commit cea7a9a

Browse files
committed
Add OpenType function
1 parent c758c3b commit cea7a9a

File tree

3 files changed

+66
-2
lines changed

3 files changed

+66
-2
lines changed

const.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package geoip
2+
3+
const (
4+
GEOIP_COUNTRY_EDITION = 1
5+
GEOIP_REGION_EDITION_REV0 = 7
6+
GEOIP_CITY_EDITION_REV0 = 6
7+
GEOIP_ORG_EDITION = 5
8+
GEOIP_ISP_EDITION = 4
9+
GEOIP_CITY_EDITION_REV1 = 2
10+
GEOIP_REGION_EDITION_REV1 = 3
11+
GEOIP_PROXY_EDITION = 8
12+
GEOIP_ASNUM_EDITION = 9
13+
GEOIP_NETSPEED_EDITION = 10
14+
GEOIP_DOMAIN_EDITION = 11
15+
GEOIP_COUNTRY_EDITION_V6 = 12
16+
GEOIP_LOCATIONA_EDITION = 13
17+
GEOIP_ACCURACYRADIUS_EDITION = 14
18+
GEOIP_CITYCONFIDENCE_EDITION = 15
19+
GEOIP_CITYCONFIDENCEDIST_EDITION = 16
20+
GEOIP_LARGE_COUNTRY_EDITION = 17
21+
GEOIP_LARGE_COUNTRY_EDITION_V6 = 18
22+
GEOIP_ASNUM_EDITION_V6 = 21
23+
GEOIP_ISP_EDITION_V6 = 22
24+
GEOIP_ORG_EDITION_V6 = 23
25+
GEOIP_DOMAIN_EDITION_V6 = 24
26+
GEOIP_LOCATIONA_EDITION_V6 = 25
27+
GEOIP_REGISTRAR_EDITION = 26
28+
GEOIP_REGISTRAR_EDITION_V6 = 27
29+
GEOIP_USERTYPE_EDITION = 28
30+
GEOIP_USERTYPE_EDITION_V6 = 29
31+
GEOIP_CITY_EDITION_REV1_V6 = 30
32+
GEOIP_CITY_EDITION_REV0_V6 = 31
33+
GEOIP_NETSPEED_EDITION_REV1 = 32
34+
GEOIP_NETSPEED_EDITION_REV1_V6 = 33
35+
)

geoip.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ func (gi *GeoIP) free() {
4040
return
4141
}
4242

43-
// Opens a GeoIP database, all formats supported by libgeoip are supported though
44-
// there are only functions to access some of the databases in this API.
43+
// Opens a GeoIP database by filename, all formats supported by libgeoip are
44+
// supported though there are only functions to access some of the databases in this API.
4545
// The database is opened in MEMORY_CACHE mode, if you need to optimize for memory
4646
// instead of performance you should change this.
4747
// If you don't pass a filename, it will try opening the database from
@@ -92,6 +92,26 @@ func Open(files ...string) (*GeoIP, error) {
9292
return g, nil
9393
}
9494

95+
// OpenType opens a specified GeoIP database type in the default location. Constants
96+
// are defined for each database type (for example GEOIP_COUNTRY_EDITION).
97+
func OpenType(dbType int) (*GeoIP, error) {
98+
g := &GeoIP{}
99+
runtime.SetFinalizer(g, (*GeoIP).free)
100+
101+
var err error
102+
103+
g.db, err = C.GeoIP_open_type(C.int(dbType), C.GEOIP_MEMORY_CACHE)
104+
if err != nil {
105+
return nil, fmt.Errorf("Error opening GeoIP database (%d): %s", dbType, err)
106+
}
107+
108+
if g.db == nil {
109+
return nil, fmt.Errorf("Didn't open GeoIP database (%d)", dbType)
110+
}
111+
112+
return g, nil
113+
}
114+
95115
// Takes an IPv4 address string and returns the organization name for that IP.
96116
// Requires the GeoIP organization database.
97117
func (gi *GeoIP) GetOrg(ip string) string {

geoip_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ func (s *GeoIPSuite) Testv4(c *C) {
3232
c.Check(netmask, Equals, 13)
3333
}
3434

35+
func (s *GeoIPSuite) TestOpenType(c *C) {
36+
// Open Country database
37+
gi, err := OpenType(GEOIP_COUNTRY_EDITION)
38+
c.Check(err, IsNil)
39+
c.Assert(gi, NotNil)
40+
country, _ := gi.GetCountry("207.171.7.51")
41+
c.Check(country, Equals, "US")
42+
}
43+
3544
func (s *GeoIPSuite) Benchmark_GetCountry(c *C) {
3645
gi, err := Open()
3746
if gi == nil || err != nil {

0 commit comments

Comments
 (0)