forked from ipipdotnet/ipdb-cplusplus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathipdb.h
225 lines (140 loc) · 4.36 KB
/
ipdb.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
//
// Created by root on 9/4/18.
//
#ifndef IPDB_IPDB_H
#define IPDB_IPDB_H
#include <memory>
#include <string>
#include <vector>
#include <map>
namespace ipdb {
#define IPv4 0x01
#define IPv6 0x02
#define ErrFileSize "IP Database file size error."
#define ErrMetaData "IP Database metadata error."
//#define ErrReadFull "IP Database ReadFull error."
#define ErrDatabaseError "database error"
#define ErrIPFormat "Query IP Format error."
#define ErrNoSupportLanguage "language not support"
#define ErrNoSupportIPv4 "IPv4 not support"
#define ErrNoSupportIPv6 "IPv6 not support"
#define ErrDataNotExists "data is not exists"
using namespace std;
class MetaData {
public:
uint64_t Build{}; //`json:"build"`
uint16_t IPVersion{}; //`json:"ip_version"`
map<string, int> Languages; //`json:"languages"`
int NodeCount{}; //`json:"node_count"`
int TotalSize{}; //`json:"total_size"`
vector<string> Fields; //`json:"fields"`
void Parse(const string &json);
};
class Reader {
MetaData meta;
int v4offset = 0;
int fileSize = 0;
int dataSize = 0;
shared_ptr<u_char> data = nullptr;
int readNode(int node, int index) const;
string resolve(int node);
int search(const u_char *ip, int bitCount) const;
string find0(const string &addr);
vector<string> find1(const string &addr, const string &language);
public:
~Reader();
explicit Reader(const string &file);
vector<string> Find(const string &addr, const string &language);
map<string, string> FindMap(const string &addr, const string &language);
bool IsIPv4Support();
bool IsIPv6Support();
uint64_t BuildTime();
vector<string> Languages();
vector<string> Fields();
};
class CityInfo {
vector<string> _data;
public:
explicit CityInfo(const vector<string> &data);
string CountryName();
string RegionName();
string CityName();
string OwnerDomain();
string IspDomain();
string Latitude();
string Longitude();
string Timezone();
string UtcOffset();
string ChinaAdminCode();
string IddCode();
string CountryCode();
string ContinentCode();
string IDC();
string BaseStation();
string CountryCode3();
string EuropeanUnion();
string CurrencyCode();
string CurrencyName();
string Anycast();
string str();
};
class City : public Reader {
public:
explicit City(const string &file);
CityInfo FindInfo(const string &addr, const string &language);
};
class BaseStationInfo {
vector<string> _data;
public:
explicit BaseStationInfo(const vector<string> &data);
string CountryName();
string RegionName();
string CityName();
string OwnerDomain();
string IspDomain();
string BaseStation();
string str();
};
class BaseStation : public Reader {
public:
explicit BaseStation(const string &file);
BaseStationInfo FindInfo(const string &addr, const string &language);
};
class DistrictInfo {
vector<string> _data;
public:
explicit DistrictInfo(const vector<string> &data);
string CountryName();
string RegionName();
string CityName();
string DistrictName();
string ChinaAdminCode();
string CoveringRadius();
string Latitude();
string Longitude();
string str();
};
class District : public Reader {
public:
explicit District(const string &file);
DistrictInfo FindInfo(const string &addr, const string &language);
};
class IDCInfo {
vector<string> _data;
public:
explicit IDCInfo(const vector<string> &data);
string CountryName();
string RegionName();
string CityName();
string OwnerDomain();
string IspDomain();
string IDC();
string str();
};
class IDC : public Reader {
public:
explicit IDC(const string &file);
IDCInfo FindInfo(const string &addr, const string &language);
};
}
#endif //IPDB_IPDB_H