Commit 3a498f20 authored by Guillaume Bury's avatar Guillaume Bury

Fixed some typos

parent 33822341
Bugs :
When no peer is avalaible without the --no-boot option, it crash => see below
Once in a while, when exiting vifibnet ( not very properly, via Ctrl+C ), processes ( openvpn and babel )
still remain and disturb further attemps ( must be killed for vifibnet to work again )
To be done :
Replace comments at the beginning of functions with docstrings & give all fn docstrings
......
......@@ -4,39 +4,40 @@ import utils
class PeerManager:
def __init__(self, dbPath):
def __init__(self, dbPath, server, port):
utils.log('Connectiong to peers database', 4)
self.db = sqlite3.connect(dbPath, isolation_level=None)
self._db = sqlite3.connect(dbPath, isolation_level=None)
self._server = server
self._port = port
utils.log('Preparing peers database', 4)
try:
self.db.execute("UPDATE peers SET used = 0")
self._db.execute("UPDATE peers SET used = 0")
except sqlite3.OperationalError, e:
if e.args[0] == 'no such table: peers':
raise RuntimeError
def populate(self, n):
def populate(self, n, address):
# address = (internal_ip, external_ip, port, proto)
# TODO: don't reconnect to server each time ?
utils.log('Connecting to remote server', 3)
self.proxy = xmlrpclib.ServerProxy('http://%s:%u' % (utils.config.server, utils.config.server_port))
self._proxy = xmlrpclib.ServerProxy('http://%s:%u' % (self._server, self._port))
utils.log('Updating peers database : populating', 2)
# TODO: determine port and proto
port = 1194
proto = 'udp'
new_peer_list = self.proxy.getPeerList(n, (utils.config.internal_ip, utils.config.external_ip, port, proto))
self.db.executemany("INSERT OR IGNORE INTO peers (ip, port, proto, used) VALUES (?,?,?,0)", new_peer_list)
self.db.execute("DELETE FROM peers WHERE ip = ?", (utils.config.external_ip,))
_, external_ip, _, _ = address
new_peer_list = self._proxy.getPeerList(n, address)
self._db.executemany("INSERT OR IGNORE INTO peers (ip, port, proto, used) VALUES (?,?,?,0)", new_peer_list)
self._db.execute("DELETE FROM peers WHERE ip = ?", (external_ip,))
def getUnusedPeers(self, nPeers):
return self.db.execute("SELECT id, ip, port, proto FROM peers WHERE used = 0 "
return self._db.execute("SELECT id, ip, port, proto FROM peers WHERE used = 0 "
"ORDER BY RANDOM() LIMIT ?", (nPeers,))
def usePeer(self, id):
utils.log('Updating peers database : using peer ' + str(id), 5)
self.db.execute("UPDATE peers SET used = 1 WHERE id = ?", (id,))
self._db.execute("UPDATE peers SET used = 1 WHERE id = ?", (id,))
def unusePeer(self, id):
utils.log('Updating peers database : unusing peer ' + str(id), 5)
self.db.execute("UPDATE peers SET used = 0 WHERE id = ?", (id,))
self._db.execute("UPDATE peers SET used = 0 WHERE id = ?", (id,))
def handle_message(self, msg):
script_type, arg = msg.split()
......
......@@ -179,7 +179,7 @@ class main(object):
def declare(self, handler, address):
client_address, ip, port, proto = address
#client_address, _ = handler.client_address
client_ip = binFromIp(client_address)
client_ip = utils.binFromIp(client_address)
if client_ip.startswith(self.network):
prefix = client_ip[len(self.network):]
prefix, = self.db.execute("SELECT prefix FROM vifib WHERE prefix <= ? ORDER BY prefix DESC LIMIT 1", (prefix,)).next()
......
......@@ -33,7 +33,7 @@ class TunnelManager:
def _removeSomeTunnels(self):
for i in range(0, max(0, len(self._connection_dict) - self._client_count + self._refresh_count)):
peer_id = random.choice(self._connection_dict.keys())
kill(peer_id)
self._kill(peer_id)
def _kill(self, peer_id):
utils.log('Killing the connection with id ' + str(peer_id), 2)
......
......@@ -10,7 +10,7 @@ def log(message, verbose_level):
def binFromIp(ip):
ip1, ip2 = struct.unpack('>QQ', socket.inet_pton(socket.AF_INET6, ip))
return bin(client_ip1)[2:].rjust(64, '0') + bin(client_ip2)[2:].rjust(64, '0')
return bin(ip1)[2:].rjust(64, '0') + bin(ip2)[2:].rjust(64, '0')
def ipFromBin(prefix):
prefix = hex(int(prefix, 2))[2:]
......
......@@ -48,6 +48,11 @@ def main():
network = utils.networkFromCa(config.ca)
internal_ip = utils.ipFromCert(network, config.cert)
openvpn_args = utils.ovpnArgs(config.openvpn_args, config.ca, config.cert)
# Get real port and proto ?
port = 1194
proto = 'udp'
# Set global variables
tunnel.log = config.log
utils.verbose = plib.verbose = config.verbose
......@@ -58,7 +63,7 @@ def main():
read_pipe = os.fdopen(r_pipe)
# Init db and tunnels
peer_db = db.PeerManager(config.db)
peer_db = db.PeerManager(config.db, config.server, config.server_port)
tunnel_manager = tunnel.TunnelManager(write_pipe, peer_db, config.client_count, config.refresh_count, openvpn_args)
# Launch babel on all interfaces. WARNING : you have to be root to start babeld
......@@ -84,7 +89,7 @@ def main():
if ready:
peer_db.handle_message(read_pipe.readline())
if time.time() >= next_refresh:
peer_db.populate(100)
peer_db.populate(100, (internal_ip, config.external_ip, port, proto))
tunnel_manager.refresh()
next_refresh = time.time() + config.refresh_time
except KeyboardInterrupt:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment