Compare commits

2 Commits
v0.1 ... master

Author SHA1 Message Date
Gregory Soutade
395260e0eb Update build_c_array.py to Python3 2024-08-31 14:35:40 +02:00
ea4bfda214 Handle timeouts & max requests in Python test class 2020-05-11 10:07:10 +02:00
3 changed files with 54 additions and 39 deletions

View File

@@ -2,6 +2,7 @@
#-*- coding: utf-8
import sys
from functools import cmp_to_key
COUNTRY_CODE_INDEX=1
IP_TYPE_INDEX=2
@@ -75,15 +76,15 @@ class IP_ELEMENT(object):
self._level = level
def printme(self):
print 'static const ip_level %s = {' % (self.name())
print '\t.prev = %s,' % (self._prev and '&%s' % (self._prev.name()) or 'NULL')
print '\t.next = %s,' % (self._next and '&%s' % (self._next.name()) or 'NULL')
print '\t.childs = %s,' % (self._childs and '&%s' % (self._childs.name()) or 'NULL')
print '\t.start = %d,' % (self._splitted_start[self._level])
print '\t.end = %d,' % (self._splitted_end[self._level])
print '\t.average = %d,' % (self._average)
print '\t.code = %d,' % (self._country_code and self._country_code or 0)
print '};'
print('static const ip_level %s = {' % (self.name()))
print('\t.prev = %s,' % (self._prev and '&%s' % (self._prev.name()) or 'NULL'))
print('\t.next = %s,' % (self._next and '&%s' % (self._next.name()) or 'NULL'))
print('\t.childs = %s,' % (self._childs and '&%s' % (self._childs.name()) or 'NULL'))
print('\t.start = %d,' % (self._splitted_start[self._level]))
print('\t.end = %d,' % (self._splitted_end[self._level]))
print('\t.average = %d,' % (self._average))
print('\t.code = %d,' % (self._country_code and self._country_code or 0))
print('};')
def get_ip_len(self):
raise NotImplementedError()
@@ -178,8 +179,8 @@ while True:
else:
sys.stderr.write('Unknown IP type %s\n' % (information[IP_TYPE_INDEX]))
print '/* This file was automatically generated, do not edit it ! */'
print '#include <stdint.h>\n\n'
print('/* This file was automatically generated, do not edit it ! */')
print('#include <stdint.h>\n\n')
def ip_sort(a, b):
for i in range(0, a.get_ip_len()):
@@ -257,16 +258,16 @@ def print_ip(ip):
while cur_ip:
if cur_ip._childs:
print_ip(cur_ip._childs)
print 'static const ip_level %s;' % (cur_ip.name())
print('static const ip_level %s;' % (cur_ip.name()))
cur_ip = cur_ip._next
print ''
print('')
cur_ip = ip
while cur_ip:
cur_ip.printme()
cur_ip = cur_ip._next
def build_array(ip_list, array_name, max_depth):
ip_list.sort(ip_sort)
ip_list = sorted(ip_list, key=cmp_to_key(ip_sort))
start_idx = 0
end_idx = start_idx+1
cur_interval = [ip_list[start_idx]]
@@ -288,18 +289,18 @@ def build_array(ip_list, array_name, max_depth):
res = manage_root(root, cur_interval, 1, max_depth)
print_ip(res)
print '\nstatic const ip_level* %s[256] = {' % (array_name)
print('\nstatic const ip_level* %s[256] = {' % (array_name))
for i in range(0, 256):
if root_ips[i]:
print '\t&%s,' % (root_ips[i].name())
print('\t&%s,' % (root_ips[i].name()))
else:
print '\tNULL, // %d' % (i)
print '};\n'
print('\tNULL, // %d' % (i))
print('};\n')
build_array(array_vals_ipv4.values(), 's_root_ipv4', 3)
build_array(array_vals_ipv6.values(), 's_root_ipv6', 15)
build_array(list(array_vals_ipv4.values()), 's_root_ipv4', 3)
build_array(list(array_vals_ipv6.values()), 's_root_ipv6', 15)
print 'static const uint8_t country_codes[][3] = {'
print('static const uint8_t country_codes[][3] = {')
for cc in countries:
print '\t{"%s"},' % (cc)
print '};\n'
print('\t{"%s"},' % (cc))
print('};\n')

View File

@@ -62,8 +62,8 @@ typedef struct thread_ctx_s{
socket_ctx_t* sockets;
int nb_cur_sockets;
int nb_available_sockets;
int max_timeout;
int max_sockets; // in ms
int max_timeout; // in ms
int max_sockets;
int stop;
int quiet;
pthread_mutex_t mutex;
@@ -481,7 +481,7 @@ int daemonize(struct gengetopt_args_info* params)
return -2;
}
ret = listen(new_socket, 0);
ret = listen(new_socket, params->sockets_per_thread_arg);
if (ret)
{
if (!params->quiet_flag)

View File

@@ -45,16 +45,20 @@ class IPToGeo(object):
4 : 'Bad IP version',
5 : 'Unsupported IP version',
6 : 'IP not found'}
MAX_REQUESTS = 50
def __init__(self, remote_addr='127.0.0.1', remote_port=53333, timeout=None, family=socket.AF_INET):
self._remote_addr = remote_addr
self._remote_port = remote_port
self._timeout = timeout
self._family = family
self._create_socket()
self._nb_requests_sent = self.MAX_REQUESTS # Force socket creation
self._socket = None
def _create_socket(self):
if self._socket:
self._socket.close()
self._socket = socket.socket(self._family, socket.SOCK_STREAM)
if not self._timeout is None:
self._socket.settimeout(self._timeout)
@@ -108,7 +112,25 @@ class IPToGeo(object):
(cc0, cc1, cc2, cc3) = struct.unpack_from('BBBB', packet, 7*4)
return (ip_res, '%c%c%c%c' % (cc0, cc1, cc2, cc3))
def _send_request(self, packet, second_chance=True):
self._nb_requests_sent += 1
if self._nb_requests_sent >= self.MAX_REQUESTS:
self._create_socket()
self._nb_requests_sent = 0
try:
self._socket.send(packet)
packet = self._socket.recv(IPToGeo.PACKET_SIZE)
if not packet:
raise socket.timeout
return packet
except socket.timeout, e:
if second_chance:
self._nb_requests_sent = self.MAX_REQUESTS
return self._send_request(packet, False)
else:
raise e
def ip_to_geo(self, ip):
ip_type = IPToGeo.IPV4
if ip.find('.') >= 0:
@@ -124,15 +146,7 @@ class IPToGeo(object):
raise Exception('Bad IP %s' % (ip))
packet = self._create_request(splitted_ip, ip_type)
try:
self._socket.send(packet)
except IOError, e:
# Give another chance (we may have been disconnected due to timeout)
self._create_socket()
self._socket.send(packet)
packet = self._socket.recv(IPToGeo.PACKET_SIZE)
if not packet:
raise IPToGeoException('Error, empty packet')
packet = self._send_request(packet)
(ip, country_code) = self._check_request(packet)
if country_code:
# convert to string