IP to Geo

IP to Geo Commit Details

Date:2016-02-24 19:17:40 (4 years 8 months ago)
Author:Grégory Soutadé
Branch:master
Commit:d9c104c731142c0347c6c476c3c8599410710ef0
Parents: 0973b18da9ba67e1ae9e2a1dc04a5e061421e1f5
Message:Bad subgroup linking in build_c_array and recursion was stopped too early for ipv6 addresses

Changes:
Mdata/build_c_array.py (14 diffs)

File differences

data/build_c_array.py
99
1010
1111
12
12
1313
1414
1515
......
1919
2020
2121
22
2223
2324
2425
......
5051
5152
5253
53
54
55
56
57
5458
5559
5660
......
8690
8791
8892
89
93
9094
9195
9296
93
97
9498
9599
96100
......
104108
105109
106110
107
108111
109112
110113
111
114
112115
113116
114117
115
118
116119
117120
118121
......
179182
180183
181184
182
185
183186
184187
185188
......
201204
202205
203206
204
207
205208
206209
207210
......
213216
214217
215218
219
216220
217221
218222
......
220224
221225
222226
223
227
224228
225229
226230
227231
228
232
229233
230234
231235
......
238242
239243
240244
241
245
242246
243
244
247
248
245249
246250
247251
......
268272
269273
270274
271
272
275
276
273277
274278
275279
......
280284
281285
282286
283
287
284288
285289
286290
......
288292
289293
290294
291
295
292296
293297
294298
......
299303
300304
301305
302
303
306
307
304308
305309
306310
IP_SIZE_INDEX=4
class IP_ELEMENT(object):
def __init__(self, start, end=None, size=0, country_code=None, level=0):
def __init__(self, start, end=None, size=0, country_code=None, level=0, is_group=False):
self._start = start
self._end = end
self._size = size
self._childs = None
self._average = 0
self._level = level
self._is_group = is_group
if not self._end: self._compute_last_ip()
return self._separator.join([self._format % x for x in ip_val])
def name(self):
return 'ip__%s__%s' %(self._start.replace(self._separator, '_'), self._end.replace(self._separator, '_'))
name = 'ip__'
if self._is_group:
name += 'g%d_' % (self._level)
return name + '%s__%s' %(self._start.replace(self._separator, '_'), self._end.replace(self._separator, '_'))
def _compute_last_ip(self):
raise NotImplementedError()
class IP_ELEMENT4(IP_ELEMENT):
def __init__(self, start, end=None, size=0, country_code=None, level=0):
def __init__(self, start, end=None, size=0, country_code=None, level=0, is_group=False):
self._separator = '.'
self._base = 10
self._format = '%d'
super(IP_ELEMENT4, self).__init__(start, end, size, country_code, level)
super(IP_ELEMENT4, self).__init__(start, end, size, country_code, level, is_group)
def get_ip_len(self):
return 4
size = int(size/256)
i += 1
self._end = self.ip_to_str(end_ip)
# print '%s + %d -> %s' % (self._start, self._size, self._end)
class IP_ELEMENT6(IP_ELEMENT):
def __init__(self, start, end=None, size=0, country_code=None, level=0):
def __init__(self, start, end=None, size=0, country_code=None, level=0, is_group=False):
self._separator = ':'
self._base = 16
self._format = '%02x'
super(IP_ELEMENT6, self).__init__(start, end, size, country_code, level)
super(IP_ELEMENT6, self).__init__(start, end, size, country_code, level, is_group)
def get_ip_len(self):
return 16
print '#include <stdint.h>\n\n'
def ip_sort(a, b):
for i in range(0, len(a._splitted_start)):
for i in range(0, a.get_ip_len()):
if a._splitted_start[i] != b._splitted_start[i]:
return a._splitted_start[i] - b._splitted_start[i]
return 0
def print_interval(interval):
p = '['
for i in interval:
p += '%s, ' % (i.name())
p += '%s,\n' % (i.name())
p += ']'
return p
total += 1
count += (child._splitted_end[child._level] - child._splitted_start[child._level] + 1)
child = child._next
if not total: return
average = int(count/total)
# Find highest power of 2 < average
for i in range(0, 9):
root.set_average(i-1)
break
def manage_root(root, intervals, level):
def manage_root(root, intervals, level, max_depth):
cur_start = 0
prev = None
first = None
cur_len = 0
if level >= 3: return (0, None)
if level >= max_depth: return None
# print 'manage_root(%d, %s, %d)' %\
# (root, print_interval(intervals), level)
while True:
cur_ip.set_level(level+1)
for ip in sub_interval:
ip.set_level(level+1)
new_group = cur_ip.__class__(cur_ip.make_group(), level=level)
new_group = cur_ip.__class__(cur_ip.make_group(), level=level, is_group=True)
sub_interval.insert(0, cur_ip)
child = manage_root(cur_ip._splitted_start[level+1], sub_interval, level+1)
new_group.set_childs(cur_ip)
child = manage_root(cur_ip._splitted_start[level+1], sub_interval, level+1, max_depth)
new_group.set_childs(child)
compute_average(new_group)
cur_ip = new_group
cur_start += len(sub_interval)
cur_ip.printme()
cur_ip = cur_ip._next
def build_array(ip_list, array_name):
ip_list.sort(ip_sort)
def build_array(ip_list, array_name, max_depth):
ip_list.sort(ip_sort)
start_idx = 0
end_idx = start_idx+1
cur_interval = [ip_list[start_idx]]
if end_idx >= len(ip_list): break
if ip_list[end_idx]._splitted_start[0] != root:
start_idx = end_idx
res = manage_root(root, cur_interval, 1)
res = manage_root(root, cur_interval, 1, max_depth)
print_ip(res)
root_ips[res._splitted_start[0]] = res
cur_interval = [ip_list[end_idx]]
else:
cur_interval.append(ip_list[end_idx])
end_idx += 1
res = manage_root(root, cur_interval, 1)
res = manage_root(root, cur_interval, 1, max_depth)
print_ip(res)
print '\nstatic const ip_level* %s[256] = {' % (array_name)
print '\tNULL, // %d' % (i)
print '};\n'
build_array(array_vals_ipv4.values(), 's_root_ipv4')
build_array(array_vals_ipv6.values(), 's_root_ipv6')
build_array(array_vals_ipv4.values(), 's_root_ipv4', 3)
build_array(array_vals_ipv6.values(), 's_root_ipv6', 15)
print 'static const uint8_t country_codes[][3] = {'
for cc in countries:

Archive Download the corresponding diff file

Branches

Tags