geoip2_compat 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +5 -0
- data/.travis.yml +9 -0
- data/Gemfile +3 -0
- data/LICENSE +21 -0
- data/README.md +19 -0
- data/Rakefile +34 -0
- data/ext/geoip2_compat/extconf.rb +11 -0
- data/ext/geoip2_compat/geoip2_compat.c +186 -0
- data/ext/geoip2_compat/maxminddb-compat-util.h +167 -0
- data/ext/geoip2_compat/maxminddb.c +1801 -0
- data/ext/geoip2_compat/maxminddb.h +223 -0
- data/ext/geoip2_compat/maxminddb_config.h +14 -0
- data/geoip2_compat.gemspec +21 -0
- data/lib/geoip2_compat.rb +1 -0
- data/test/test_geoip2_compat.rb +49 -0
- metadata +102 -0
@@ -0,0 +1,223 @@
|
|
1
|
+
#ifdef __cplusplus
|
2
|
+
extern "C" {
|
3
|
+
#endif
|
4
|
+
|
5
|
+
#ifndef MAXMINDDB_H
|
6
|
+
#define MAXMINDDB_H
|
7
|
+
|
8
|
+
#ifndef _POSIX_C_SOURCE
|
9
|
+
#define _POSIX_C_SOURCE 200112L
|
10
|
+
#endif
|
11
|
+
|
12
|
+
#include <maxminddb_config.h>
|
13
|
+
#include <stdarg.h>
|
14
|
+
#include <stdbool.h>
|
15
|
+
#include <stdint.h>
|
16
|
+
#include <stdio.h>
|
17
|
+
#include <sys/types.h>
|
18
|
+
|
19
|
+
#ifdef _WIN32
|
20
|
+
#include <winsock2.h>
|
21
|
+
#include <ws2tcpip.h>
|
22
|
+
/* libmaxminddb package version from configure */
|
23
|
+
#define PACKAGE_VERSION "1.0.3"
|
24
|
+
|
25
|
+
typedef ADDRESS_FAMILY sa_family_t;
|
26
|
+
|
27
|
+
#if defined(_MSC_VER)
|
28
|
+
/* MSVC doesn't define signed size_t, copy it from configure */
|
29
|
+
#define ssize_t int
|
30
|
+
|
31
|
+
/* MSVC doesn't support restricted pointers */
|
32
|
+
#define restrict
|
33
|
+
#endif
|
34
|
+
#else
|
35
|
+
#include <netdb.h>
|
36
|
+
#include <netinet/in.h>
|
37
|
+
#include <sys/socket.h>
|
38
|
+
#endif
|
39
|
+
|
40
|
+
#define MMDB_DATA_TYPE_EXTENDED (0)
|
41
|
+
#define MMDB_DATA_TYPE_POINTER (1)
|
42
|
+
#define MMDB_DATA_TYPE_UTF8_STRING (2)
|
43
|
+
#define MMDB_DATA_TYPE_DOUBLE (3)
|
44
|
+
#define MMDB_DATA_TYPE_BYTES (4)
|
45
|
+
#define MMDB_DATA_TYPE_UINT16 (5)
|
46
|
+
#define MMDB_DATA_TYPE_UINT32 (6)
|
47
|
+
#define MMDB_DATA_TYPE_MAP (7)
|
48
|
+
#define MMDB_DATA_TYPE_INT32 (8)
|
49
|
+
#define MMDB_DATA_TYPE_UINT64 (9)
|
50
|
+
#define MMDB_DATA_TYPE_UINT128 (10)
|
51
|
+
#define MMDB_DATA_TYPE_ARRAY (11)
|
52
|
+
#define MMDB_DATA_TYPE_CONTAINER (12)
|
53
|
+
#define MMDB_DATA_TYPE_END_MARKER (13)
|
54
|
+
#define MMDB_DATA_TYPE_BOOLEAN (14)
|
55
|
+
#define MMDB_DATA_TYPE_FLOAT (15)
|
56
|
+
|
57
|
+
/* flags for open */
|
58
|
+
#define MMDB_MODE_MMAP (1)
|
59
|
+
#define MMDB_MODE_MASK (7)
|
60
|
+
|
61
|
+
/* error codes */
|
62
|
+
#define MMDB_SUCCESS (0)
|
63
|
+
#define MMDB_FILE_OPEN_ERROR (1)
|
64
|
+
#define MMDB_CORRUPT_SEARCH_TREE_ERROR (2)
|
65
|
+
#define MMDB_INVALID_METADATA_ERROR (3)
|
66
|
+
#define MMDB_IO_ERROR (4)
|
67
|
+
#define MMDB_OUT_OF_MEMORY_ERROR (5)
|
68
|
+
#define MMDB_UNKNOWN_DATABASE_FORMAT_ERROR (6)
|
69
|
+
#define MMDB_INVALID_DATA_ERROR (7)
|
70
|
+
#define MMDB_INVALID_LOOKUP_PATH_ERROR (8)
|
71
|
+
#define MMDB_LOOKUP_PATH_DOES_NOT_MATCH_DATA_ERROR (9)
|
72
|
+
#define MMDB_INVALID_NODE_NUMBER_ERROR (10)
|
73
|
+
#define MMDB_IPV6_LOOKUP_IN_IPV4_DATABASE_ERROR (11)
|
74
|
+
|
75
|
+
#if !(MMDB_UINT128_IS_BYTE_ARRAY)
|
76
|
+
#if MMDB_UINT128_USING_MODE
|
77
|
+
typedef unsigned int mmdb_uint128_t __attribute__ ((__mode__(TI)));
|
78
|
+
#else
|
79
|
+
typedef unsigned __int128 mmdb_uint128_t;
|
80
|
+
#endif
|
81
|
+
#endif
|
82
|
+
|
83
|
+
/* This is a pointer into the data section for a given IP address lookup */
|
84
|
+
typedef struct MMDB_entry_s {
|
85
|
+
struct MMDB_s *mmdb;
|
86
|
+
uint32_t offset;
|
87
|
+
} MMDB_entry_s;
|
88
|
+
|
89
|
+
typedef struct MMDB_lookup_result_s {
|
90
|
+
bool found_entry;
|
91
|
+
MMDB_entry_s entry;
|
92
|
+
uint16_t netmask;
|
93
|
+
} MMDB_lookup_result_s;
|
94
|
+
|
95
|
+
typedef struct MMDB_entry_data_s {
|
96
|
+
bool has_data;
|
97
|
+
union {
|
98
|
+
uint32_t pointer;
|
99
|
+
const char *utf8_string;
|
100
|
+
double double_value;
|
101
|
+
const uint8_t *bytes;
|
102
|
+
uint16_t uint16;
|
103
|
+
uint32_t uint32;
|
104
|
+
int32_t int32;
|
105
|
+
uint64_t uint64;
|
106
|
+
#if MMDB_UINT128_IS_BYTE_ARRAY
|
107
|
+
uint8_t uint128[16];
|
108
|
+
#else
|
109
|
+
mmdb_uint128_t uint128;
|
110
|
+
#endif
|
111
|
+
bool boolean;
|
112
|
+
float float_value;
|
113
|
+
};
|
114
|
+
/* This is a 0 if a given entry cannot be found. This can only happen
|
115
|
+
* when a call to MMDB_(v)get_value() asks for hash keys or array
|
116
|
+
* indices that don't exist. */
|
117
|
+
uint32_t offset;
|
118
|
+
/* This is the next entry in the data section, but it's really only
|
119
|
+
* relevant for entries that part of a larger map or array
|
120
|
+
* struct. There's no good reason for an end user to look at this
|
121
|
+
* directly. */
|
122
|
+
uint32_t offset_to_next;
|
123
|
+
/* This is only valid for strings, utf8_strings or binary data */
|
124
|
+
uint32_t data_size;
|
125
|
+
/* This is an MMDB_DATA_TYPE_* constant */
|
126
|
+
uint32_t type;
|
127
|
+
} MMDB_entry_data_s;
|
128
|
+
|
129
|
+
/* This is the return type when someone asks for all the entry data in a map or array */
|
130
|
+
typedef struct MMDB_entry_data_list_s {
|
131
|
+
MMDB_entry_data_s entry_data;
|
132
|
+
struct MMDB_entry_data_list_s *next;
|
133
|
+
} MMDB_entry_data_list_s;
|
134
|
+
|
135
|
+
typedef struct MMDB_description_s {
|
136
|
+
const char *language;
|
137
|
+
const char *description;
|
138
|
+
} MMDB_description_s;
|
139
|
+
|
140
|
+
typedef struct MMDB_metadata_s {
|
141
|
+
uint32_t node_count;
|
142
|
+
uint16_t record_size;
|
143
|
+
uint16_t ip_version;
|
144
|
+
const char *database_type;
|
145
|
+
struct {
|
146
|
+
size_t count;
|
147
|
+
const char **names;
|
148
|
+
} languages;
|
149
|
+
uint16_t binary_format_major_version;
|
150
|
+
uint16_t binary_format_minor_version;
|
151
|
+
uint64_t build_epoch;
|
152
|
+
struct {
|
153
|
+
size_t count;
|
154
|
+
MMDB_description_s **descriptions;
|
155
|
+
} description;
|
156
|
+
} MMDB_metadata_s;
|
157
|
+
|
158
|
+
typedef struct MMDB_ipv4_start_node_s {
|
159
|
+
uint16_t netmask;
|
160
|
+
uint32_t node_value;
|
161
|
+
} MMDB_ipv4_start_node_s;
|
162
|
+
|
163
|
+
typedef struct MMDB_s {
|
164
|
+
uint32_t flags;
|
165
|
+
const char *filename;
|
166
|
+
ssize_t file_size;
|
167
|
+
const uint8_t *file_content;
|
168
|
+
const uint8_t *data_section;
|
169
|
+
uint32_t data_section_size;
|
170
|
+
const uint8_t *metadata_section;
|
171
|
+
uint32_t metadata_section_size;
|
172
|
+
uint16_t full_record_byte_size;
|
173
|
+
uint16_t depth;
|
174
|
+
MMDB_ipv4_start_node_s ipv4_start_node;
|
175
|
+
MMDB_metadata_s metadata;
|
176
|
+
} MMDB_s;
|
177
|
+
|
178
|
+
typedef struct MMDB_search_node_s {
|
179
|
+
uint64_t left_record;
|
180
|
+
uint64_t right_record;
|
181
|
+
} MMDB_search_node_s;
|
182
|
+
|
183
|
+
/* *INDENT-OFF* */
|
184
|
+
/* --prototypes automatically generated by dev-bin/regen-prototypes.pl - don't remove this comment */
|
185
|
+
extern int MMDB_open(const char *const filename, uint32_t flags, MMDB_s *const mmdb);
|
186
|
+
extern MMDB_lookup_result_s MMDB_lookup_string(MMDB_s *const mmdb,
|
187
|
+
const char *const ipstr,
|
188
|
+
int *const gai_error,
|
189
|
+
int *const mmdb_error);
|
190
|
+
extern MMDB_lookup_result_s MMDB_lookup_sockaddr(
|
191
|
+
MMDB_s *const mmdb,
|
192
|
+
const struct sockaddr *const sockaddr,
|
193
|
+
int *const mmdb_error);
|
194
|
+
extern int MMDB_read_node(MMDB_s *const mmdb, uint32_t node_number,
|
195
|
+
MMDB_search_node_s *const node);
|
196
|
+
extern int MMDB_get_value(MMDB_entry_s *const start,
|
197
|
+
MMDB_entry_data_s *const entry_data,
|
198
|
+
...);
|
199
|
+
extern int MMDB_vget_value(MMDB_entry_s *const start,
|
200
|
+
MMDB_entry_data_s *const entry_data,
|
201
|
+
va_list va_path);
|
202
|
+
extern int MMDB_aget_value(MMDB_entry_s *const start,
|
203
|
+
MMDB_entry_data_s *const entry_data,
|
204
|
+
const char *const *const path);
|
205
|
+
extern int MMDB_get_metadata_as_entry_data_list(
|
206
|
+
MMDB_s *const mmdb, MMDB_entry_data_list_s **const entry_data_list);
|
207
|
+
extern int MMDB_get_entry_data_list(
|
208
|
+
MMDB_entry_s *start, MMDB_entry_data_list_s **const entry_data_list);
|
209
|
+
extern void MMDB_free_entry_data_list(MMDB_entry_data_list_s *const entry_data_list);
|
210
|
+
extern void MMDB_close(MMDB_s *const mmdb);
|
211
|
+
extern const char *MMDB_lib_version(void);
|
212
|
+
extern int MMDB_dump_entry_data_list(FILE *const stream,
|
213
|
+
MMDB_entry_data_list_s *const entry_data_list,
|
214
|
+
int indent);
|
215
|
+
extern const char *MMDB_strerror(int error_code);
|
216
|
+
/* --prototypes end - don't remove this comment-- */
|
217
|
+
/* *INDENT-ON* */
|
218
|
+
|
219
|
+
#endif /* MAXMINDDB_H */
|
220
|
+
|
221
|
+
#ifdef __cplusplus
|
222
|
+
}
|
223
|
+
#endif
|
@@ -0,0 +1,14 @@
|
|
1
|
+
#ifndef MAXMINDDB_CONFIG_H
|
2
|
+
#define MAXMINDDB_CONFIG_H
|
3
|
+
|
4
|
+
#ifndef MMDB_UINT128_USING_MODE
|
5
|
+
/* Define as 1 if we we use unsigned int __atribute__ ((__mode__(TI))) for uint128 values */
|
6
|
+
#define MMDB_UINT128_USING_MODE 0
|
7
|
+
#endif
|
8
|
+
|
9
|
+
#ifndef MMDB_UINT128_IS_BYTE_ARRAY
|
10
|
+
/* Define as 1 if we don't have an unsigned __int128 type */
|
11
|
+
#undef MMDB_UINT128_IS_BYTE_ARRAY
|
12
|
+
#endif
|
13
|
+
|
14
|
+
#endif /* MAXMINDDB_CONFIG_H */
|
@@ -0,0 +1,21 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = "geoip2_compat"
|
3
|
+
s.version = "0.0.3"
|
4
|
+
|
5
|
+
s.licenses = ["MIT"]
|
6
|
+
|
7
|
+
s.authors = ["Dirkjan Bussink"]
|
8
|
+
s.email = ["d.bussink@gmail.com"]
|
9
|
+
|
10
|
+
s.summary = "Bindings for libmaxminddb to access GeoIP2 database files"
|
11
|
+
s.description = "Lookup IPv4 and IPv6 addresses in Maxmind GeoIP2 database files"
|
12
|
+
s.homepage = "http://github.com/dbussink/geoip2_compat"
|
13
|
+
|
14
|
+
s.files = `git ls-files`.split("\n")
|
15
|
+
s.extensions = ['ext/geoip2_compat/extconf.rb']
|
16
|
+
s.require_paths = ["lib"]
|
17
|
+
|
18
|
+
s.add_development_dependency 'rake', '~>10.0'
|
19
|
+
s.add_development_dependency 'rake-compiler', '~> 0'
|
20
|
+
s.add_development_dependency 'test-unit', '~> 0'
|
21
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require "geoip2_compat/geoip2_compat"
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'geoip2_compat'
|
2
|
+
require 'test/unit'
|
3
|
+
|
4
|
+
class GeoIP2Compat
|
5
|
+
class TestGeoIP2Compat < Test::Unit::TestCase
|
6
|
+
|
7
|
+
def setup
|
8
|
+
@db = GeoIP2Compat.new("test/GeoLite2-City.mmdb")
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_initialize_success
|
12
|
+
assert_equal "test/GeoLite2-City.mmdb", @db.path
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_initialize_failure
|
16
|
+
assert_raise ArgumentError do
|
17
|
+
GeoIP2Compat.new {}
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_lookup_ipv4
|
22
|
+
res = {
|
23
|
+
:city=>"San Francisco",
|
24
|
+
:country_code=>"US",
|
25
|
+
:country_name=>"United States",
|
26
|
+
:latitude=>37.7697,
|
27
|
+
:longitude=>-122.3933,
|
28
|
+
:postal_code=>"94107",
|
29
|
+
:region=>"CA",
|
30
|
+
:region_name=>"California",
|
31
|
+
}
|
32
|
+
assert_equal res, @db.lookup("192.30.252.128")
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_lookup_ipv6
|
36
|
+
res = {
|
37
|
+
:city=>"Grave",
|
38
|
+
:country_code=>"NL",
|
39
|
+
:country_name=>"Netherlands",
|
40
|
+
:latitude=>51.759,
|
41
|
+
:longitude=>5.7388,
|
42
|
+
:postal_code=>"5361",
|
43
|
+
:region=>"NB",
|
44
|
+
:region_name=>"North Brabant",
|
45
|
+
}
|
46
|
+
assert_equal res, @db.lookup("2001:888:0:1::888")
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
metadata
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: geoip2_compat
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.3
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Dirkjan Bussink
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-12-29 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rake
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '10.0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '10.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake-compiler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: test-unit
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Lookup IPv4 and IPv6 addresses in Maxmind GeoIP2 database files
|
56
|
+
email:
|
57
|
+
- d.bussink@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions:
|
60
|
+
- ext/geoip2_compat/extconf.rb
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- ".travis.yml"
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- ext/geoip2_compat/extconf.rb
|
70
|
+
- ext/geoip2_compat/geoip2_compat.c
|
71
|
+
- ext/geoip2_compat/maxminddb-compat-util.h
|
72
|
+
- ext/geoip2_compat/maxminddb.c
|
73
|
+
- ext/geoip2_compat/maxminddb.h
|
74
|
+
- ext/geoip2_compat/maxminddb_config.h
|
75
|
+
- geoip2_compat.gemspec
|
76
|
+
- lib/geoip2_compat.rb
|
77
|
+
- test/test_geoip2_compat.rb
|
78
|
+
homepage: http://github.com/dbussink/geoip2_compat
|
79
|
+
licenses:
|
80
|
+
- MIT
|
81
|
+
metadata: {}
|
82
|
+
post_install_message:
|
83
|
+
rdoc_options: []
|
84
|
+
require_paths:
|
85
|
+
- lib
|
86
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - ">="
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '0'
|
96
|
+
requirements: []
|
97
|
+
rubyforge_project:
|
98
|
+
rubygems_version: 2.2.2
|
99
|
+
signing_key:
|
100
|
+
specification_version: 4
|
101
|
+
summary: Bindings for libmaxminddb to access GeoIP2 database files
|
102
|
+
test_files: []
|