Commit 8c4a1fb4 authored by Julien Muchembled's avatar Julien Muchembled

re6stnet: new --disable-proto option

parent 620b9e98
...@@ -95,19 +95,28 @@ class PeerDB(object): ...@@ -95,19 +95,28 @@ class PeerDB(object):
return bootpeer return bootpeer
logging.warning('Buggy registry sent us our own address') logging.warning('Buggy registry sent us our own address')
def addPeer(self, prefix, address, force=False): def addPeer(self, prefix, address, set_preferred=False):
logging.debug('Adding peer %s: %s', prefix, address) logging.debug('Adding peer %s: %s', prefix, address)
with self._db: with self._db:
q = self._db.execute q = self._db.execute
try: try:
(a,), = q("SELECT address FROM peer WHERE prefix=?", (prefix,)) (a,), = q("SELECT address FROM peer WHERE prefix=?", (prefix,))
a = a != address if force else \ if set_preferred:
set(a.split(';')) != set(address.split(';')) preferred = address.split(';')
address = a
else:
preferred = a.split(';')
def key(a):
try:
return preferred.index(a)
except ValueError:
return len(preferred)
address = ';'.join(sorted(address.split(';'), key=key))
except ValueError: except ValueError:
q("DELETE FROM peer WHERE prefix IN (SELECT peer" q("DELETE FROM peer WHERE prefix IN (SELECT peer"
" FROM volatile.stat ORDER BY try, RANDOM() LIMIT ?,-1)", " FROM volatile.stat ORDER BY try, RANDOM() LIMIT ?,-1)",
(self._db_size,)) (self._db_size,))
a = True a = None
if a: if a != address:
q("INSERT OR REPLACE INTO peer VALUES (?,?)", (prefix, address)) q("INSERT OR REPLACE INTO peer VALUES (?,?)", (prefix, address))
q("INSERT OR REPLACE INTO volatile.stat VALUES (?,0)", (prefix,)) q("INSERT OR REPLACE INTO volatile.stat VALUES (?,0)", (prefix,))
...@@ -41,8 +41,8 @@ class MultiGatewayManager(dict): ...@@ -41,8 +41,8 @@ class MultiGatewayManager(dict):
class Connection(object): class Connection(object):
def __init__(self, address, iface, prefix): def __init__(self, address_list, iface, prefix):
self.address_list = list(utils.parse_address(address)) self.address_list = address_list
self.iface = iface self.iface = iface
self.routes = 0 self.routes = 0
self._prefix = prefix self._prefix = prefix
...@@ -78,8 +78,7 @@ class Connection(object): ...@@ -78,8 +78,7 @@ class Connection(object):
except TypeError: except TypeError:
i = len(self.address_list) - 1 i = len(self.address_list) - 1
if i: if i:
db.addPeer(self._prefix, utils.dump_address( db.addPeer(self._prefix, ','.join(self.address_list[i]), True)
self.address_list[i:] + self.address_list[:i]), True)
else: else:
db.connecting(self._prefix, 0) db.connecting(self._prefix, 0)
...@@ -106,7 +105,7 @@ class TunnelManager(object): ...@@ -106,7 +105,7 @@ class TunnelManager(object):
def __init__(self, write_pipe, peer_db, openvpn_args, timeout, def __init__(self, write_pipe, peer_db, openvpn_args, timeout,
refresh, client_count, iface_list, network, prefix, refresh, client_count, iface_list, network, prefix,
address, ip_changed, encrypt, remote_gateway): address, ip_changed, encrypt, remote_gateway, disable_proto):
self._write_pipe = write_pipe self._write_pipe = write_pipe
self._peer_db = peer_db self._peer_db = peer_db
self._connecting = set() self._connecting = set()
...@@ -125,6 +124,7 @@ class TunnelManager(object): ...@@ -125,6 +124,7 @@ class TunnelManager(object):
self._encrypt = encrypt self._encrypt = encrypt
self._gateway_manager = MultiGatewayManager(remote_gateway) \ self._gateway_manager = MultiGatewayManager(remote_gateway) \
if remote_gateway else None if remote_gateway else None
self._disable_proto = disable_proto
self._served = set() self._served = set()
self.sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM) self.sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
...@@ -216,6 +216,11 @@ class TunnelManager(object): ...@@ -216,6 +216,11 @@ class TunnelManager(object):
if prefix in self._served or prefix in self._connection_dict: if prefix in self._served or prefix in self._connection_dict:
return False return False
assert prefix != self._prefix, self.__dict__ assert prefix != self._prefix, self.__dict__
address = [x for x in utils.parse_address(address)
if x[2] not in self._disable_proto]
self._peer_db.connecting(prefix, 1)
if not address:
return False
logging.info('Establishing a connection with %u/%u', logging.info('Establishing a connection with %u/%u',
int(prefix, 2), len(prefix)) int(prefix, 2), len(prefix))
iface = self.getFreeInterface(prefix) iface = self.getFreeInterface(prefix)
...@@ -224,7 +229,6 @@ class TunnelManager(object): ...@@ -224,7 +229,6 @@ class TunnelManager(object):
for ip in c: for ip in c:
self._gateway_manager.add(ip, True) self._gateway_manager.add(ip, True)
c.open(self._write_pipe, self._timeout, self._encrypt, self._ovpn_args) c.open(self._write_pipe, self._timeout, self._encrypt, self._ovpn_args)
self._peer_db.connecting(prefix, 1)
return True return True
def _makeNewTunnels(self, route_counted): def _makeNewTunnels(self, route_counted):
......
...@@ -101,6 +101,8 @@ def getConfig(): ...@@ -101,6 +101,8 @@ def getConfig():
_('--remote-gateway', action='append', dest='gw_list', _('--remote-gateway', action='append', dest='gw_list',
help="Force each tunnel to be created through one the given gateways," help="Force each tunnel to be created through one the given gateways,"
" in a round-robin fashion.") " in a round-robin fashion.")
_('--disable-proto', action='append', choices=('udp', 'tcp'), default=[],
help="Do never try to create tunnels using given protocols.")
_('--client', metavar='HOST,PORT,PROTO[;...]', _('--client', metavar='HOST,PORT,PROTO[;...]',
help="Do not run any OpenVPN server, but only 1 OpenVPN client," help="Do not run any OpenVPN server, but only 1 OpenVPN client,"
" with specified remotes. Any other option not required in this" " with specified remotes. Any other option not required in this"
...@@ -229,7 +231,8 @@ def main(): ...@@ -229,7 +231,8 @@ def main():
tunnel_manager = tunnel.TunnelManager(write_pipe, peer_db, tunnel_manager = tunnel.TunnelManager(write_pipe, peer_db,
config.openvpn_args, timeout, config.tunnel_refresh, config.openvpn_args, timeout, config.tunnel_refresh,
config.client_count, config.iface_list, network, prefix, config.client_count, config.iface_list, network, prefix,
address, ip_changed, config.encrypt, remote_gateway) address, ip_changed, config.encrypt, remote_gateway,
config.disable_proto)
tunnel_interfaces += tunnel_manager.new_iface_list tunnel_interfaces += tunnel_manager.new_iface_list
else: else:
tunnel_manager = write_pipe = None tunnel_manager = write_pipe = None
......
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