geoipdb 0.5.8 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +0 -8
- data/.ruby-version +1 -0
- data/Gemfile +1 -3
- data/Makefile +10 -0
- data/README.md +2 -5
- data/Rakefile +0 -13
- data/geoipdb.gemspec +5 -12
- data/lib/geoipdb.jar +0 -0
- data/lib/geoipdb.rb +224 -5
- data/sample_data/ip_ranges.csv +2 -2
- data/spec/geoipdb_spec.rb +8 -43
- data/spec/spec_helper.rb +10 -7
- data/{ext/geoipdb/src → src}/City.java +0 -0
- data/{ext/geoipdb/src → src}/CsvReader.java +0 -0
- data/src/IPDB.java +66 -0
- data/{ext/geoipdb/src → src}/IpRange.java +21 -7
- metadata +30 -38
- data/ext/geoipdb/extconf.rb +0 -3
- data/ext/geoipdb/geoipdb.c +0 -107
- data/ext/geoipdb/ipdb.c +0 -668
- data/ext/geoipdb/ipdb.h +0 -84
- data/ext/geoipdb/src/GeoIpDb.java +0 -101
- data/lib/cgeoipdb.rb +0 -1
- data/lib/ip_information.rb +0 -27
- data/lib/jgeoipdb.rb +0 -41
- data/sample_data/citiess_corrupt.csv +0 -8
- data/sample_data/ip_ranges_corrupt.csv +0 -20
data/ext/geoipdb/ipdb.h
DELETED
@@ -1,84 +0,0 @@
|
|
1
|
-
#ifdef INT_2_BYTES
|
2
|
-
typedef char int8;
|
3
|
-
typedef int int16;
|
4
|
-
typedef unsigned int uint16;
|
5
|
-
typedef long int32;
|
6
|
-
#else
|
7
|
-
typedef char int8;
|
8
|
-
typedef short int16;
|
9
|
-
typedef unsigned short uint16;
|
10
|
-
typedef int int32;
|
11
|
-
#endif
|
12
|
-
|
13
|
-
#ifdef RSTRING_PTR
|
14
|
-
#else
|
15
|
-
# define RSTRING_LEN(x) (RSTRING(x)->len)
|
16
|
-
# define RSTRING_PTR(x) (RSTRING(x)->ptr)
|
17
|
-
#endif
|
18
|
-
|
19
|
-
#define RANGES_DELIM ",\n"
|
20
|
-
#define CITIES_DELIM ",\n"
|
21
|
-
|
22
|
-
#define MAX_CITIES_COUNT 1000000 //Usually we have about 120 000 Cities
|
23
|
-
#define MAX_RANGES_COUNT 10000000 //Usually we have about 6 Mio IP-Ranges
|
24
|
-
|
25
|
-
#define MAX_ISPS_COUNT 65535
|
26
|
-
#define MAX_ISP_NAME_LENGTH 100
|
27
|
-
|
28
|
-
#define USE_CACHE 1
|
29
|
-
#define DEBUG 0
|
30
|
-
|
31
|
-
typedef struct{
|
32
|
-
unsigned long from;
|
33
|
-
unsigned long to;
|
34
|
-
unsigned char is_mobile;
|
35
|
-
int city_index; //index of the city in the cities-array
|
36
|
-
int16 isp_index; //index of the isp in the isps-array
|
37
|
-
} IpRange;
|
38
|
-
|
39
|
-
typedef struct{
|
40
|
-
int city_code;
|
41
|
-
char name[32];
|
42
|
-
double lat;
|
43
|
-
double lng;
|
44
|
-
|
45
|
-
char country_iso3[4];
|
46
|
-
char country_iso2[3];
|
47
|
-
} City;
|
48
|
-
|
49
|
-
typedef struct{
|
50
|
-
char *ranges_csv_file; //the CSV-file with ip-ranges-to-city-code-mappings
|
51
|
-
unsigned int max_ranges_count;
|
52
|
-
unsigned int ranges_count;
|
53
|
-
|
54
|
-
char *cities_csv_file;
|
55
|
-
unsigned int cities_count;
|
56
|
-
unsigned int max_cities_count;
|
57
|
-
|
58
|
-
char *cache_file_name; // a binary file to store the whole db.....
|
59
|
-
IpRange * ranges;
|
60
|
-
City * cities;
|
61
|
-
|
62
|
-
char isps[MAX_ISPS_COUNT][MAX_ISP_NAME_LENGTH]; // a fixed size array of strings should be enough here...do not expect the isps to grow dramatically..
|
63
|
-
uint16 isps_count;
|
64
|
-
|
65
|
-
|
66
|
-
} IPDB;
|
67
|
-
|
68
|
-
IPDB *
|
69
|
-
init_db(char * cities_csv_file, char * ranges_csv_file, char * cache_file_name);
|
70
|
-
|
71
|
-
void
|
72
|
-
print_city(const City * e);
|
73
|
-
|
74
|
-
void
|
75
|
-
benchmark_search(IPDB * db,int count);
|
76
|
-
|
77
|
-
IpRange*
|
78
|
-
find_range_for_ip(IPDB *db, char *ip);
|
79
|
-
|
80
|
-
City*
|
81
|
-
find_city_for_ip_range(IPDB * db, IpRange* range);
|
82
|
-
|
83
|
-
char*
|
84
|
-
find_isp_for_ip_range(IPDB * db, IpRange* range);
|
@@ -1,101 +0,0 @@
|
|
1
|
-
import java.io.FileNotFoundException;
|
2
|
-
import java.util.ArrayList;
|
3
|
-
import java.util.Collections;
|
4
|
-
import java.util.HashMap;
|
5
|
-
|
6
|
-
public class GeoIpDb
|
7
|
-
{
|
8
|
-
private static final int MAX_CITY_COUNT = 1000000;
|
9
|
-
private static final int MAX_RANGE_COUNT = 10000000;
|
10
|
-
|
11
|
-
HashMap<Integer, City> cities;
|
12
|
-
ArrayList<String> isps;
|
13
|
-
ArrayList<IpRange> ranges;
|
14
|
-
|
15
|
-
public GeoIpDb(String citiesFileName, String rangesFileName) throws FileNotFoundException
|
16
|
-
{
|
17
|
-
cities = new HashMap<Integer, City>();
|
18
|
-
isps = new ArrayList<String>();
|
19
|
-
ranges = new ArrayList<IpRange>();
|
20
|
-
|
21
|
-
readCitiesCSV(citiesFileName);
|
22
|
-
readRangesCSV(rangesFileName);
|
23
|
-
}
|
24
|
-
|
25
|
-
public IpRange findRangeForIp(String ip)
|
26
|
-
{
|
27
|
-
if (ranges.isEmpty()) {
|
28
|
-
System.out.println("ERROR: DB has no ranges data. Can not search!");
|
29
|
-
return null;
|
30
|
-
}
|
31
|
-
|
32
|
-
int index = 0;
|
33
|
-
IpRange search = new IpRange(ip, "0");
|
34
|
-
index = Collections.binarySearch(ranges, search);
|
35
|
-
if (index < 0)
|
36
|
-
return null;
|
37
|
-
|
38
|
-
return ranges.get(index);
|
39
|
-
}
|
40
|
-
|
41
|
-
public City findCityForIpRange(IpRange range)
|
42
|
-
{
|
43
|
-
if (range == null) {
|
44
|
-
System.out.println("Cannot find city for no given range, right?");
|
45
|
-
return null;
|
46
|
-
}
|
47
|
-
if (cities.isEmpty()) {
|
48
|
-
System.out.println("ERROR: DB has no city data. Can not search!");
|
49
|
-
return null;
|
50
|
-
}
|
51
|
-
|
52
|
-
if (range.cityCode == 0) {
|
53
|
-
System.out.format("ERROR: Could not find city with index: %d\n", range.cityCode);
|
54
|
-
}
|
55
|
-
|
56
|
-
return cities.get(range.cityCode);
|
57
|
-
}
|
58
|
-
|
59
|
-
public ArrayList<IpRange> get_ranges()
|
60
|
-
{
|
61
|
-
return ranges;
|
62
|
-
}
|
63
|
-
|
64
|
-
private void readCitiesCSV(String file_name) throws FileNotFoundException
|
65
|
-
{
|
66
|
-
CsvReader reader = new CsvReader(file_name);
|
67
|
-
String[] line = null;
|
68
|
-
City city = null;
|
69
|
-
|
70
|
-
reader.readLine(); // skip first line
|
71
|
-
|
72
|
-
while ((line = reader.readLine()) != null) {
|
73
|
-
if (cities.size() >= MAX_CITY_COUNT){
|
74
|
-
System.out.format("ERROR: MAX_CITY_COUNT = %d limit reached - mek it bigger :-(\n", MAX_CITY_COUNT);
|
75
|
-
return;
|
76
|
-
}
|
77
|
-
city = new City(line);
|
78
|
-
cities.put(city.cityCode, city);
|
79
|
-
}
|
80
|
-
}
|
81
|
-
|
82
|
-
private void readRangesCSV(String file_name) throws FileNotFoundException
|
83
|
-
{
|
84
|
-
CsvReader reader = new CsvReader(file_name);
|
85
|
-
String[] line = null;
|
86
|
-
|
87
|
-
reader.readLine(); // skip first line
|
88
|
-
|
89
|
-
while ((line = reader.readLine()) != null) {
|
90
|
-
if (line.length < 5)
|
91
|
-
continue;
|
92
|
-
|
93
|
-
if (ranges.size() >= MAX_RANGE_COUNT){
|
94
|
-
System.out.format("ERROR: MAX_RANGE_COUNT = %d limit reached - mek it bigger :-(\n", MAX_RANGE_COUNT);
|
95
|
-
return;
|
96
|
-
}
|
97
|
-
|
98
|
-
ranges.add(new IpRange(line));
|
99
|
-
}
|
100
|
-
}
|
101
|
-
}
|
data/lib/cgeoipdb.rb
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
require File.expand_path('../geoipdb.so', __FILE__)
|
data/lib/ip_information.rb
DELETED
@@ -1,27 +0,0 @@
|
|
1
|
-
class IpInformation
|
2
|
-
|
3
|
-
ATTRIBS = [
|
4
|
-
:country_iso_code,
|
5
|
-
:city_code,
|
6
|
-
:city_name,
|
7
|
-
:lat,
|
8
|
-
:lng,
|
9
|
-
:is_mobile,
|
10
|
-
:isp_name,
|
11
|
-
]
|
12
|
-
|
13
|
-
ATTRIBS.each do |attrib|
|
14
|
-
attr_accessor attrib
|
15
|
-
end
|
16
|
-
|
17
|
-
def mobile?
|
18
|
-
@is_mobile
|
19
|
-
end
|
20
|
-
|
21
|
-
def to_h
|
22
|
-
Hash[ATTRIBS.map do |attrib|
|
23
|
-
[attrib, send(attrib)]
|
24
|
-
end]
|
25
|
-
end
|
26
|
-
|
27
|
-
end
|
data/lib/jgeoipdb.rb
DELETED
@@ -1,41 +0,0 @@
|
|
1
|
-
require 'java'
|
2
|
-
require File.expand_path('../geoipdb.jar', __FILE__)
|
3
|
-
|
4
|
-
class GeoIpDb
|
5
|
-
|
6
|
-
def self.init(cities_file, ranges_file, cache_file)
|
7
|
-
self.new(cities_file, ranges_file, cache_file)
|
8
|
-
rescue java.io.FileNotFoundException
|
9
|
-
return nil
|
10
|
-
end
|
11
|
-
|
12
|
-
def initialize(cities_file, ranges_file, cache_file)
|
13
|
-
# the Java implementation does not support cache files,
|
14
|
-
# since the java serialisation is slower than the actual csv parsing
|
15
|
-
@jdb = Java::GeoIpDb.new(cities_file, ranges_file)
|
16
|
-
end
|
17
|
-
|
18
|
-
def information_for_ip(ip)
|
19
|
-
range = @jdb.find_range_for_ip(ip)
|
20
|
-
return nil unless range
|
21
|
-
|
22
|
-
city = @jdb.find_city_for_ip_range(range)
|
23
|
-
return nil unless city
|
24
|
-
|
25
|
-
isp = range.isp_name
|
26
|
-
build_ip_information_object(range, city, isp)
|
27
|
-
end
|
28
|
-
|
29
|
-
def build_ip_information_object(range, city, isp)
|
30
|
-
info = IpInformation.new
|
31
|
-
info.country_iso_code = city.country_iso2
|
32
|
-
info.city_name = city.name
|
33
|
-
info.city_code = city.city_code
|
34
|
-
info.lng = city.lng
|
35
|
-
info.lat = city.lat
|
36
|
-
info.is_mobile = range.is_mobile
|
37
|
-
info.isp_name = isp && isp.to_sym
|
38
|
-
info
|
39
|
-
end
|
40
|
-
|
41
|
-
end
|
@@ -1,8 +0,0 @@
|
|
1
|
-
COUNTRY,REGION,CITY-NAME,METRO-CODE,CITY-CODE,LATITUDE,LONGITUDE
|
2
|
-
***,***,?,0,7,0,0
|
3
|
-
afg,?,?,0,1,33,1
|
4
|
-
afg,no region,herat,-1,2,34.3452,62.
|
5
|
-
|
6
|
-
afg,no , , , , region,kabul,-1,3,34.5167,69.1833
|
7
|
-
afg,no region,, , kandahar,-1,4,31.6111,65.702
|
8
|
-
afg,no region,maza , ,r-e sharif,-1,5,36.7041,67.1096
|
@@ -1,20 +0,0 @@
|
|
1
|
-
asdf,as,dsa,g,asdf,g,as,d,ga,sd,f,as,dg,
|
2
|
-
|
3
|
-
|
4
|
-
start_ip,end_ip,field 13,
|
5
|
-
0.0.0.0,0.0.0.255,0,
|
6
|
-
|
7
|
-
0.0.1.0,0.255.255.255,1,
|
8
|
-
|
9
|
-
1.0.0.0,1.0.0.255,2,
|
10
|
-
|
11
|
-
1.0.1.0,1.1.0.255,3,
|
12
|
-
|
13
|
-
asfdasdf asdf asfasdfasdf§$%&/
|
14
|
-
|
15
|
-
1, , , 7 ,.1.1.0,1.1.1.255,4,,asdf,f,as,df,ag,as,df,asd,
|
16
|
-
1.1.2.0,1.2.2.255,5,
|
17
|
-
1.2.3.0,1.2.3.255,5,
|
18
|
-
1.2.4.0,1.3.255.255,2,
|
19
|
-
1.4.0.0,1.4.0.255,5,
|
20
|
-
1.4.1.0,1.8.255.255,3,
|