geoipdb 0.5.5-java

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.
@@ -0,0 +1,84 @@
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);
@@ -0,0 +1,120 @@
1
+ public class City implements Comparable<City>
2
+ {
3
+ int cityCode;
4
+ String name;
5
+ double lat;
6
+ double lng;
7
+ String countryISO3;
8
+ String countryISO2;
9
+
10
+
11
+ public static final String[] country_iso2_codes = {
12
+ "--", "ap", "eu", "ad", "ae", "af", "ag", "ai", "al", "am", "an", "ao",
13
+ "aq", "ar", "as", "at", "au", "aw", "az", "ba", "bb", "bd", "be", "bf",
14
+ "bg", "bh", "bi", "bj", "bm", "bn", "bo", "br", "bs", "bt", "bv", "bw",
15
+ "by", "bz", "ca", "cc", "cd", "cf", "cg", "ch", "ci", "ck", "cl", "cm",
16
+ "cn", "co", "cr", "cu", "cv", "cx", "cy", "cz", "de", "dj", "dk", "dm",
17
+ "do", "dz", "ec", "ee", "eg", "eh", "er", "es", "et", "fi", "fj", "fk",
18
+ "fm", "fo", "fr", "fx", "ga", "gb", "gd", "ge", "gf", "gh", "gi", "gl",
19
+ "gm", "gn", "gp", "gq", "gr", "gs", "gt", "gu", "gw", "gy", "hk", "hm",
20
+ "hn", "hr", "ht", "hu", "id", "ie", "il", "in", "io", "iq", "ir", "is",
21
+ "it", "jm", "jo", "jp", "ke", "kg", "kh", "ki", "km", "kn", "kp", "kr",
22
+ "kw", "ky", "kz", "la", "lb", "lc", "li", "lk", "lr", "ls", "lt", "lu",
23
+ "lv", "ly", "ma", "mc", "md", "mg", "mh", "mk", "ml", "mm", "mn", "mo",
24
+ "mp", "mq", "mr", "ms", "mt", "mu", "mv", "mw", "mx", "my", "mz", "na",
25
+ "nc", "ne", "nf", "ng", "ni", "nl", "no", "np", "nr", "nu", "nz", "om",
26
+ "pa", "pe", "pf", "pg", "ph", "pk", "pl", "pm", "pn", "pr", "ps", "pt",
27
+ "pw", "py", "qa", "re", "ro", "ru", "rw", "sa", "sb", "sc", "sd", "se",
28
+ "sg", "sh", "si", "sj", "sk", "sl", "sm", "sn", "so", "sr", "st", "sv",
29
+ "sy", "sz", "tc", "td", "tf", "tg", "th", "tj", "tk", "tm", "tn", "to",
30
+ "tl", "tr", "tt", "tv", "tw", "tz", "ua", "ug", "um", "us", "uy", "uz",
31
+ "va", "vc", "ve", "vg", "vi", "vn", "vu", "wf", "ws", "ye", "yt", "rs",
32
+ "za", "zm", "me", "zw", "a1", "a2", "o1", "ax", "gg", "im", "je", "bl",
33
+ "mf"
34
+ };
35
+
36
+ public static final String[] country_iso3_codes = {
37
+ "--", "ap", "eu", "and", "are", "afg", "atg", "aia", "alb", "arm",
38
+ "ant", "ago", "aq", "arg", "asm", "aut", "aus", "abw", "aze", "bih",
39
+ "brb", "bgd", "bel", "bfa", "bgr", "bhr", "bdi", "ben", "bmu", "brn",
40
+ "bol", "bra", "bhs", "btn", "bv", "bwa", "blr", "blz", "can", "cc",
41
+ "cod", "caf", "cog", "che", "civ", "cok", "chl", "cmr", "chn", "col",
42
+ "cri", "cub", "cpv", "cx", "cyp", "cze", "deu", "dji", "dnk", "dma",
43
+ "dom", "dza", "ecu", "est", "egy", "esh", "eri", "esp", "eth", "fin",
44
+ "fji", "flk", "fsm", "fro", "fra", "fx", "gab", "gbr", "grd", "geo",
45
+ "guf", "gha", "gib", "grl", "gmb", "gin", "glp", "gnq", "grc", "gs",
46
+ "gtm", "gum", "gnb", "guy", "hkg", "hm", "hnd", "hrv", "hti", "hun",
47
+ "idn", "irl", "isr", "ind", "io", "irq", "irn", "isl", "ita", "jam",
48
+ "jor", "jpn", "ken", "kgz", "khm", "kir", "com", "kna", "prk", "kor",
49
+ "kwt", "cym", "kaz", "lao", "lbn", "lca", "lie", "lka", "lbr", "lso",
50
+ "ltu", "lux", "lva", "lby", "mar", "mco", "mda", "mdg", "mhl", "mkd",
51
+ "mli", "mmr", "mng", "mac", "mnp", "mtq", "mrt", "msr", "mlt", "mus",
52
+ "mdv", "mwi", "mex", "mys", "moz", "nam", "ncl", "ner", "nfk", "nga",
53
+ "nic", "nld", "nor", "npl", "nru", "niu", "nzl", "omn", "pan", "per",
54
+ "pyf", "png", "phl", "pak", "pol", "spm", "pcn", "pri", "pse", "prt",
55
+ "plw", "pry", "qat", "reu", "rou", "rus", "rwa", "sau", "slb", "syc",
56
+ "sdn", "swe", "sgp", "shn", "svn", "sjm", "svk", "sle", "smr", "sen",
57
+ "som", "sur", "stp", "slv", "syr", "swz", "tca", "tcd", "tf", "tgo",
58
+ "tha", "tjk", "tkl", "tkm", "tun", "ton", "tls", "tur", "tto", "tuv",
59
+ "twn", "tza", "ukr", "uga", "um", "usa", "ury", "uzb", "vat", "vct",
60
+ "ven", "vgb", "vir", "vnm", "vut", "wlf", "wsm", "yem", "yt", "srb",
61
+ "zaf", "zmb", "mne", "zwe", "a1", "a2", "o1", "ala", "ggy", "imn",
62
+ "jey", "blm", "maf"
63
+ };
64
+
65
+ public City(String[] cityValues)
66
+ {
67
+ this.cityCode = Integer.parseInt(cityValues[4]);
68
+ this.name = cityValues[2];
69
+ this.countryISO2 = iso2_code(cityValues[0]);
70
+ this.countryISO3 = cityValues[0];
71
+ this.lat = Double.parseDouble(cityValues[5]);
72
+ this.lng = Double.parseDouble(cityValues[6]);
73
+ }
74
+
75
+ private String iso2_code(String iso3_code)
76
+ {
77
+ int index = 0;
78
+ for (index = 0; index < country_iso3_codes.length; index++) {
79
+ if (country_iso3_codes[index].equals(iso3_code)) {
80
+ return country_iso2_codes[index];
81
+ }
82
+ }
83
+ return country_iso2_codes[0];
84
+ }
85
+
86
+ @Override
87
+ public int compareTo(City other)
88
+ {
89
+ return this.cityCode - other.cityCode;
90
+ }
91
+
92
+ public int getCityCode()
93
+ {
94
+ return cityCode;
95
+ }
96
+
97
+ public String getName()
98
+ {
99
+ return name;
100
+ }
101
+
102
+ public double getLat()
103
+ {
104
+ return lat;
105
+ }
106
+
107
+ public double getLng() {
108
+ return lng;
109
+ }
110
+
111
+ public String getCountryISO3()
112
+ {
113
+ return countryISO3;
114
+ }
115
+
116
+ public String getCountryISO2()
117
+ {
118
+ return countryISO2;
119
+ }
120
+ }
@@ -0,0 +1,29 @@
1
+ import java.io.BufferedReader;
2
+ import java.io.FileNotFoundException;
3
+ import java.io.FileReader;
4
+ import java.io.IOException;
5
+
6
+ public class CsvReader
7
+ {
8
+ private BufferedReader reader = null;
9
+
10
+ public CsvReader(String file_name) throws FileNotFoundException {
11
+ reader = new BufferedReader(new FileReader(file_name));
12
+ }
13
+
14
+ public String[] readLine()
15
+ {
16
+ try {
17
+ String line = "";
18
+ String[] token = null;
19
+
20
+ if ((line = reader.readLine()) != null) {
21
+ token = line.split(",");
22
+ }
23
+
24
+ return token;
25
+ } catch (IOException e) {
26
+ return null;
27
+ }
28
+ }
29
+ }
@@ -0,0 +1,101 @@
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
+ }
@@ -0,0 +1,110 @@
1
+ public class IpRange implements Comparable<IpRange>
2
+ {
3
+ long from;
4
+ long to;
5
+ boolean isMobile;
6
+ int cityCode;
7
+ String ispName;
8
+
9
+ public IpRange(String[] rangeValues)
10
+ {
11
+ this.from = ipToLong(rangeValues[0]);
12
+ this.to = ipToLong(rangeValues[1]);
13
+ this.isMobile = conTypeToBool(rangeValues[2]);
14
+ this.cityCode = Integer.parseInt(rangeValues[3]);
15
+
16
+ if (!rangeValues[4].equals("?")) {
17
+ // Use only the first 100 chars to be compliant with the c implementation
18
+ this.ispName = rangeValues[4].length() > 100 ? rangeValues[4].substring(0, 100) : rangeValues[4];
19
+ }
20
+ }
21
+
22
+ public IpRange(String from, String to)
23
+ {
24
+ this.from = ipToLong(from);
25
+ this.to = ipToLong(to);
26
+ }
27
+
28
+ public int getCityCode()
29
+ {
30
+ return cityCode;
31
+ }
32
+
33
+ public void setCityCode(int cityCode)
34
+ {
35
+ this.cityCode = cityCode;
36
+ }
37
+
38
+ public long getFrom()
39
+ {
40
+ return from;
41
+ }
42
+
43
+ public long getTo()
44
+ {
45
+ return to;
46
+ }
47
+
48
+ public boolean getIsMobile()
49
+ {
50
+ return isMobile;
51
+ }
52
+
53
+ public String getIspName()
54
+ {
55
+ return ispName;
56
+ }
57
+
58
+ private boolean conTypeToBool(String conType)
59
+ {
60
+ return (conType.length() > 0) && (conType.charAt(0) == 'm');
61
+ }
62
+
63
+ private long ipToLong(String ip)
64
+ {
65
+ long result = 0;
66
+
67
+ if (!(ip == null || ip.equals(""))) {
68
+ for (String octet : ip.split("\\.")) {
69
+ result = (result << 8) | Integer.parseInt(octet);
70
+ }
71
+ }
72
+
73
+ return result;
74
+ }
75
+
76
+ @Override
77
+ public int compareTo(IpRange other)
78
+ {
79
+ if (other == null) {
80
+ return 0;
81
+ }
82
+
83
+ if (other.from > 0 && other.to > 0 && this.from > 0 && this.to > 0) {
84
+ if (other.from < this.from)
85
+ return 1;
86
+ else if (other.from > this.to)
87
+ return -1;
88
+ else
89
+ return 0;
90
+ } else if (other.to == 0 && this.to > 0) {
91
+ if (other.from < this.from)
92
+ return 1;
93
+ else if (other.from > this.to)
94
+ return -1;
95
+ else
96
+ return 0;
97
+ } else if (this.to == 0 && other.to > 0) {
98
+ if (this.from < other.from)
99
+ return 1;
100
+ else if (this.from > other.from)
101
+ return -1;
102
+ else
103
+ return 0;
104
+ } else if (other.to == 0 && this.to == 0) {
105
+ return (int)(other.from - this.from);
106
+ }
107
+
108
+ return 0;
109
+ }
110
+ }