Commit 3b4883a8 authored by Ulysse Beaugnon's avatar Ulysse Beaugnon

Merge branch 'master' of https://git.erp5.org/repos/re6stnet

Conflicts:
	re6st/tunnel.py

Re6stnet now create a socket and listen on it. The socket is not used yet
parents 3ccc14d3 84127aa6
...@@ -28,7 +28,7 @@ running the re6st-registry must also have a client ( re6stnet ) running. ...@@ -28,7 +28,7 @@ running the re6st-registry must also have a client ( re6stnet ) running.
USAGE USAGE
===== =====
The re6st-registry will automatically listen on both ipv4 and ipv6 for incomming The re6st-registry will automatically listen on both ipv4 and ipv6 for incoming
request. request.
--port port --port port
......
...@@ -17,6 +17,8 @@ class PeerManager: ...@@ -17,6 +17,8 @@ class PeerManager:
self._pp = pp self._pp = pp
self._manual = manual self._manual = manual
self.tunnel_manager = None self.tunnel_manager = None
self._sock = None
self.socket_file = None
logging.info('Connecting to peers database...') logging.info('Connecting to peers database...')
self._db = sqlite3.connect(db_path, isolation_level=None) self._db = sqlite3.connect(db_path, isolation_level=None)
...@@ -164,9 +166,9 @@ class PeerManager: ...@@ -164,9 +166,9 @@ class PeerManager:
def handle_message(self, msg): def handle_message(self, msg):
script_type, arg = msg.split() script_type, arg = msg.split()
if script_type == 'client-connect': if script_type == 'client-connect':
logging.info('Incomming connection from %s' % (arg,)) logging.info('Incoming connection from %s' % (arg,))
prefix = utils.binFromSubnet(arg) prefix = utils.binFromSubnet(arg)
if self.tunnel_manager.checkIncommingTunnel(prefix): if self.tunnel_manager.checkIncomingTunnel(prefix):
self.blacklist(prefix, 2) self.blacklist(prefix, 2)
elif script_type == 'client-disconnect': elif script_type == 'client-disconnect':
self.whitelist(utils.binFromSubnet(arg)) self.whitelist(utils.binFromSubnet(arg))
...@@ -186,6 +188,18 @@ class PeerManager: ...@@ -186,6 +188,18 @@ class PeerManager:
logging.debug('socket.error : %s' % e) logging.debug('socket.error : %s' % e)
logging.info('''Connection to server failed while logging.info('''Connection to server failed while
declaring external infos''') declaring external infos''')
elif script_type == 'up':
if int(arg) != 0:
logging.info('Server creation failed, terminating')
raise RuntimeError
logging.debug('Creating the socket for peer advertising')
time.sleep(5)
self._sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
self._sock.bind((self._internal_ip, 326))
self._socket_file = self._sock.makefile()
else: else:
logging.debug('Unknow message recieved from the openvpn pipe : %s' logging.debug('Unknow message recieved from the openvpn pipe : %s'
% msg) % msg)
def readSocket(self):
print 'reading socket'
...@@ -42,11 +42,13 @@ script_type = os.environ['script_type'] ...@@ -42,11 +42,13 @@ script_type = os.environ['script_type']
if script_type == 'up': if script_type == 'up':
from subprocess import call from subprocess import call
dev = os.environ['dev'] dev = os.environ['dev']
if sys.argv[1] != 'none': if sys.argv[2] != 'none':
sys.exit(call(('ip', 'link', 'set', dev, 'up')) out = (call(('ip', 'link', 'set', dev, 'up'))
or call(('ip', 'addr', 'add', sys.argv[1], 'dev', dev))) or call(('ip', 'addr', 'add', sys.argv[2], 'dev', dev)))
os.write(int(sys.argv[1]), 'up %s\n' % out)
else: else:
sys.exit(call(('ip', 'link', 'set', dev, 'up'))) out = call(('ip', 'link', 'set', dev, 'up'))
sys.exit(out)
if script_type == 'client-connect': if script_type == 'client-connect':
# Send client its external ip address # Send client its external ip address
......
...@@ -28,9 +28,9 @@ def openvpn(hello_interval, encrypt, *args, **kw): ...@@ -28,9 +28,9 @@ def openvpn(hello_interval, encrypt, *args, **kw):
def server(server_ip, ip_length, max_clients, dh_path, pipe_fd, port, proto, hello_interval, encrypt, *args, **kw): def server(server_ip, ip_length, max_clients, dh_path, pipe_fd, port, proto, hello_interval, encrypt, *args, **kw):
logging.debug('Starting server...') logging.debug('Starting server...')
if server_ip != '': if server_ip != '':
script_up = '%s %s/%u' % (ovpn_server, server_ip, 64) script_up = '%s %s %s/%u' % (ovpn_server, pipe_fd, server_ip, 64)
else: else:
script_up = '%s none' % ovpn_server script_up = '%s %s none' % (ovpn_server, pipe_fd)
return openvpn(hello_interval, encrypt, return openvpn(hello_interval, encrypt,
'--tls-server', '--tls-server',
'--mode', 'server', '--mode', 'server',
......
...@@ -142,6 +142,7 @@ class TunnelManager: ...@@ -142,6 +142,7 @@ class TunnelManager:
logging.debug('A route to %s has been discovered on the LAN' logging.debug('A route to %s has been discovered on the LAN'
% (hex(int(prefix), 2)[2:])) % (hex(int(prefix), 2)[2:]))
self._peer_db.blacklist(prefix, 0) self._peer_db.blacklist(prefix, 0)
self._notifyPeer(line[0])
logging.debug("Routes have been counted") logging.debug("Routes have been counted")
for p in self._connection_dict.keys(): for p in self._connection_dict.keys():
...@@ -153,15 +154,15 @@ class TunnelManager: ...@@ -153,15 +154,15 @@ class TunnelManager:
for prefix in self._connection_dict.keys(): for prefix in self._connection_dict.keys():
self._kill(prefix) self._kill(prefix)
def checkIncommingTunnel(self, prefix): def checkIncomingTunnel(self, prefix):
if prefix in self._connection_dict: if prefix in self._connection_dict:
if prefix >= self._prefix: if prefix < self._prefix:
self._kill(prefix)
return True
else:
return False return False
else: else:
return True self._kill(prefix)
return True
def notifyPeer(self, peerIp): def _notifyPeer(self, peerIp):
pass ip = '%s:%s:%s:%s:%s:%s:%s:%s' % (peerIp[0:3], peerIp[4:7], peerIp[8:11],
peerIp[12:15], peerIp[16:19], peerIp[20:23], peerIp[24:27], peerIp[28:32])
print ip
...@@ -154,14 +154,17 @@ def main(): ...@@ -154,14 +154,17 @@ def main():
os.O_WRONLY | os.O_CREAT | os.O_TRUNC), stderr=subprocess.STDOUT) os.O_WRONLY | os.O_CREAT | os.O_TRUNC), stderr=subprocess.STDOUT)
# Establish connections # Establish connections
server_process = list(plib.server(internal_ip, len(network) + len(prefix), server_process = []
server_ip = internal_ip
for port, proto, iface in config.pp:
server_process.append(plib.server(server_ip, len(network) + len(prefix),
config.connection_count, config.dh, write_pipe, port, config.connection_count, config.dh, write_pipe, port,
proto, config.hello, config.encrypt, '--dev', iface, *openvpn_args, proto, config.hello, config.encrypt, '--dev', iface, *openvpn_args,
stdout=os.open(os.path.join(config.log, stdout=os.open(os.path.join(config.log,
're6stnet.server.%s.log' % (proto,)), 're6stnet.server.%s.log' % (proto,)),
os.O_WRONLY | os.O_CREAT | os.O_TRUNC), os.O_WRONLY | os.O_CREAT | os.O_TRUNC),
stderr=subprocess.STDOUT) stderr=subprocess.STDOUT))
for port, proto, iface in config.pp) server_ip = ''
# main loop # main loop
try: try:
...@@ -172,9 +175,11 @@ def main(): ...@@ -172,9 +175,11 @@ def main():
if forwarder != None: if forwarder != None:
nextUpdate = min(nextUpdate, forwarder.next_refresh) nextUpdate = min(nextUpdate, forwarder.next_refresh)
nextUpdate = max(0, nextUpdate - time.time()) nextUpdate = max(0, nextUpdate - time.time())
select_list = [read_pipe]
ready, tmp1, tmp2 = select.select([read_pipe], [], [], nextUpdate) if peer_db.socket_file:
if ready: select_list.append(peer_db.socket_file)
ready, tmp1, tmp2 = select.select(select_list, [], [], nextUpdate)
if read_pipe in ready:
peer_db.handle_message(read_pipe.readline()) peer_db.handle_message(read_pipe.readline())
if time.time() >= peer_db.next_refresh: if time.time() >= peer_db.next_refresh:
peer_db.refresh() peer_db.refresh()
...@@ -182,6 +187,8 @@ def main(): ...@@ -182,6 +187,8 @@ def main():
tunnel_manager.refresh() tunnel_manager.refresh()
if forwarder != None and time.time() > forwarder.next_refresh: if forwarder != None and time.time() > forwarder.next_refresh:
forwarder.refresh() forwarder.refresh()
if peer_db.socket_file in ready:
peer_db.readSocket()
finally: finally:
for p in [router] + server_process: for p in [router] + server_process:
try: try:
......
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