diff --git a/plugins/post_analysis/iptogeo.py b/plugins/post_analysis/iptogeo.py
index f887346..76a8d1c 100644
--- a/plugins/post_analysis/iptogeo.py
+++ b/plugins/post_analysis/iptogeo.py
@@ -1,6 +1,25 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
+#
+# Copyright 2016 Grégory Soutadé
+#
+# This file is part of iptogeo.
+#
+# iptogeo is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# iptogeo is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with iptogeo. If not, see .
+#
+
import socket
import struct
@@ -27,26 +46,44 @@ class IPToGeo(object):
5 : 'Unsupported IP version',
6 : 'IP not found'}
- def __init__(self, remote_addr='127.0.0.1', remote_port=53333, timeout=None):
+ 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()
def _create_socket(self):
- self._socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+ self._socket = socket.socket(self._family, socket.SOCK_STREAM)
if not self._timeout is None:
self._socket.settimeout(self._timeout)
self._socket.connect((self._remote_addr, self._remote_port))
- def _create_request(self, ip):
+ def _extend_ipv6(self, ipv6):
+ tmp = ''
+ for s in ipv6.split(':'):
+ if not s: break
+ while len(s) != 4:
+ s = '0' + s
+ tmp += s
+ while len(tmp) < 16*2:
+ tmp += '0'
+ res = ''
+ for i in range(0, 15*2, 2):
+ res += tmp[i] + tmp[i+1] + ':'
+ res += tmp[30] + tmp[31]
+
+ return res
+
+ def _create_request(self, ip, ip_type):
packet = ''
packet += struct.pack('= 0:
+ splitted_ip = [int(a) for a in ip.split('.')]
+ if len(splitted_ip) != 4:
+ raise Exception('Bad IP %s' % (ip))
+ elif ip.find(':') >= 0:
+ splitted_ip = [int(a, 16) for a in self._extend_ipv6(ip).split(':')]
+ if len(splitted_ip) != 16:
+ raise Exception('Bad IP %s' % (ip))
+ ip_type = IPToGeo.IPV6
+ else:
+ raise Exception('Bad IP %s' % (ip))
- packet = self._create_request(splitted_ip)
+ packet = self._create_request(splitted_ip, ip_type)
try:
self._socket.send(packet)
except IOError, e: