-
Notifications
You must be signed in to change notification settings - Fork 1
/
ipLocator.cpp
253 lines (211 loc) · 5.76 KB
/
ipLocator.cpp
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include "ipLocator.h"
#define IP_FILENAME "ip.dat"
prefix_map c1;
IPSearch::IPSearch(): dataBuffer(NULL)
{}
IPSearch::~IPSearch()
{
free(dataBuffer);
}
IPSearch* IPSearch::instance()
{
IPSearch *ret = new IPSearch;
if (ret->init()) {
return ret;
}
if (ret) {
delete ret;
}
return NULL;
}
bool IPSearch::init()
{
long size = 0;
dataBuffer = readFile(IP_FILENAME, &size);
if (dataBuffer)
{
first_index = ReadInt32(dataBuffer, 0);
last_index = ReadInt32(dataBuffer, 4);
first_prefix_index = ReadInt32(dataBuffer, 8);
last_prefix_index = ReadInt32(dataBuffer, 12);
index_count = (last_index - first_index) / 12 + 1;
prefix_count = (last_prefix_index - first_prefix_index) / 9 + 1;
uint8_t *indexBuffer = dataBuffer + first_prefix_index;
for (uint32_t i = 0; i < prefix_count; indexBuffer += 9, i++)
{
Interval iv;
uint32_t prefix = (uint32_t)indexBuffer[0];
iv.start = ReadInt32(indexBuffer, 1);
iv.end = ReadInt32(indexBuffer, 5);
c1[prefix] = iv;
}
return true;
}
return false;
}
uint8_t* IPSearch::readFile(const string path, long* length) {
uint8_t *data;
FILE *file = fopen(path.data(), "rb");
int readBytes = 0;
if (!file) return 0;
fseek(file, 0, SEEK_END);
*length = ftell(file);
fseek(file, 0, SEEK_SET);
data = (uint8_t*)malloc(*length * sizeof(uint8_t));
readBytes = fread(data, 1, *length, file);
fclose(file);
if (readBytes != *length)
{
free(data);
data = NULL;
}
return data;
}
const string IPSearch::Query(const char*ip)
{
uint32_t ip_prefix_value;
uint32_t intIP = ipToLong(ip, ip_prefix_value);
uint32_t high = 0;
uint32_t low = 0;
uint32_t startIp = 0;
uint32_t endIp = 0;
uint32_t local_offset = 0;
uint32_t local_length = 0;
prefix_map::iterator it = c1.find(ip_prefix_value);
if (it != c1.end())
{
low = it->second.start;
high = it->second.end;
uint32_t my_index = low == high ? low : BinarySearch(low, high, intIP);
GetIndex(my_index, startIp, endIp, local_offset, local_length);
if ((startIp <= intIP) && (endIp >= intIP))
{
return GetLocal(local_offset, local_length);
}
}
return "";
}
uint32_t IPSearch::BinarySearch(uint32_t low, uint32_t high, uint32_t k)
{
uint32_t M = 0;
while (low <= high)
{
uint32_t mid = (low + high) / 2;
uint32_t endipNum = GetEndIp(mid);
if (endipNum >= k)
{
M = mid;
if (mid == 0)
{
break;
}
high = mid - 1;
}
else
low = mid + 1;
}
return M;
}
void IPSearch::GetIndex(uint32_t left, uint32_t &startip, uint32_t &endip, uint32_t &local_offset, uint32_t &local_length)
{
uint32_t left_offset = first_index + (left * 12);
startip = ReadInt32(dataBuffer, left_offset);
endip = ReadInt32(dataBuffer, left_offset + 4);
local_offset = ReadInt24(dataBuffer, left_offset + 8);
local_length = (uint32_t)dataBuffer[left_offset + 11];
}
uint32_t IPSearch::GetEndIp(uint32_t left)
{
uint32_t left_offset = first_index + (left * 12);
return ReadInt32(dataBuffer, left_offset + 4);
}
string IPSearch::GetLocal(uint32_t local_offset, uint32_t local_length)
{
string str("");
str.append((const char*)dataBuffer + local_offset, local_length);
return str;
}
string IPSearch::longToIp(uint32_t adr) {
char buf[256];
sprintf(buf, "%d.%d.%d.%d", adr >> 24, (adr >> 16) & 0xff, (adr >> 8) & 0xff, adr & 0xff);
string ipstr(buf);
return ipstr;
}
uint32_t IPSearch::ipToLong(const char * ip, uint32_t &prefix)
{
/*int a, b, c, d;
sscanf_s(ip, "%u.%u.%u.%u", &a, &b, &c, &d);
prefix = (BYTE)a;
return ((BYTE)a << 24) | ((BYTE)b << 16) | ((BYTE)c << 8) | (BYTE)d;
*/
int a, b, c, d;
int iLen;
int abcdIndex = 0;
iLen = strlen(ip);
char ips[3];
memset(ips, '\0', 3);
a = b = c = d = 0;
int ipsCnt = 0;
for (int i = 0; i < iLen; i++)
{
if ('.' == ip[i])
{
if (0 == abcdIndex)
{
abcdIndex = 1;
a = atoi(ips);
}
else if (1 == abcdIndex)
{
abcdIndex = 2;
b = atoi(ips);
}
else if (2 == abcdIndex)
{
abcdIndex = 3;
c = atoi(ips);
}
ipsCnt = 0;
memset(ips, '\0', 3);
}
else
{
ips[ipsCnt] = ip[i];
ipsCnt++;
}
}
d = atoi(ips);
prefix = (uint32_t)a;
return ((uint8_t)a << 24) | ((uint8_t)b << 16) | ((uint8_t)c << 8) | (uint8_t)d;
}
uint32_t IPSearch::ReadInt32(uint8_t *buf, int pos)
{
static uint32_t retInt = 0;
retInt = (uint32_t)((buf[pos + 3] << 24 & 0xFF000000) | (buf[pos + 2] << 16 & 0x00FF0000) | (buf[pos + 1] << 8 & 0x0000FF00) | (buf[pos] & 0x000000FF));
return retInt;
}
uint32_t IPSearch::ReadInt24(uint8_t *buf, int pos)
{
static uint32_t retInt = 0;
retInt = (uint32_t)((buf[pos + 2] << 16 & 0x00FF0000) | (buf[pos + 1] << 8 & 0x0000FF00) | (buf[pos] & 0x000000FF));
return retInt;
}
#if 0
int main(int argc, char **argv)
{
IPSearch *finder = IPSearch::instance();
if (!finder) {
printf("the IPSearch instance is null!");
getchar();
return -1;
}
const char *ip = "123.4.5.68";
const string local = finder->Query(ip);
std::cout << local << endl;
return 0;
}
#endif