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.
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.
--port port
......
......@@ -17,6 +17,8 @@ class PeerManager:
self._pp = pp
self._manual = manual
self.tunnel_manager = None
self._sock = None
self.socket_file = None
logging.info('Connecting to peers database...')
self._db = sqlite3.connect(db_path, isolation_level=None)
......@@ -164,9 +166,9 @@ class PeerManager:
def handle_message(self, msg):
script_type, arg = msg.split()
if script_type == 'client-connect':
logging.info('Incomming connection from %s' % (arg,))
logging.info('Incoming connection from %s' % (arg,))
prefix = utils.binFromSubnet(arg)
if self.tunnel_manager.checkIncommingTunnel(prefix):
if self.tunnel_manager.checkIncomingTunnel(prefix):
self.blacklist(prefix, 2)
elif script_type == 'client-disconnect':
self.whitelist(utils.binFromSubnet(arg))
......@@ -186,6 +188,18 @@ class PeerManager:
logging.debug('socket.error : %s' % e)
logging.info('''Connection to server failed while
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:
logging.debug('Unknow message recieved from the openvpn pipe : %s'
% msg)
def readSocket(self):
print 'reading socket'
......@@ -42,11 +42,13 @@ script_type = os.environ['script_type']
if script_type == 'up':
from subprocess import call
dev = os.environ['dev']
if sys.argv[1] != 'none':
sys.exit(call(('ip', 'link', 'set', dev, 'up'))
or call(('ip', 'addr', 'add', sys.argv[1], 'dev', dev)))
if sys.argv[2] != 'none':
out = (call(('ip', 'link', 'set', dev, 'up'))
or call(('ip', 'addr', 'add', sys.argv[2], 'dev', dev)))
os.write(int(sys.argv[1]), 'up %s\n' % out)
else:
sys.exit(call(('ip', 'link', 'set', dev, 'up')))
out = call(('ip', 'link', 'set', dev, 'up'))
sys.exit(out)
if script_type == 'client-connect':
# Send client its external ip address
......
......@@ -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):
logging.debug('Starting server...')
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:
script_up = '%s none' % ovpn_server
script_up = '%s %s none' % (ovpn_server, pipe_fd)
return openvpn(hello_interval, encrypt,
'--tls-server',
'--mode', 'server',
......
......@@ -142,6 +142,7 @@ class TunnelManager:
logging.debug('A route to %s has been discovered on the LAN'
% (hex(int(prefix), 2)[2:]))
self._peer_db.blacklist(prefix, 0)
self._notifyPeer(line[0])
logging.debug("Routes have been counted")
for p in self._connection_dict.keys():
......@@ -153,15 +154,15 @@ class TunnelManager:
for prefix in self._connection_dict.keys():
self._kill(prefix)
def checkIncommingTunnel(self, prefix):
def checkIncomingTunnel(self, prefix):
if prefix in self._connection_dict:
if prefix >= self._prefix:
self._kill(prefix)
return True
else:
if prefix < self._prefix:
return False
else:
self._kill(prefix)
return True
def notifyPeer(self, peerIp):
pass
def _notifyPeer(self, peerIp):
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():
os.O_WRONLY | os.O_CREAT | os.O_TRUNC), stderr=subprocess.STDOUT)
# 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,
proto, config.hello, config.encrypt, '--dev', iface, *openvpn_args,
stdout=os.open(os.path.join(config.log,
're6stnet.server.%s.log' % (proto,)),
os.O_WRONLY | os.O_CREAT | os.O_TRUNC),
stderr=subprocess.STDOUT)
for port, proto, iface in config.pp)
stderr=subprocess.STDOUT))
server_ip = ''
# main loop
try:
......@@ -172,9 +175,11 @@ def main():
if forwarder != None:
nextUpdate = min(nextUpdate, forwarder.next_refresh)
nextUpdate = max(0, nextUpdate - time.time())
ready, tmp1, tmp2 = select.select([read_pipe], [], [], nextUpdate)
if ready:
select_list = [read_pipe]
if peer_db.socket_file:
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())
if time.time() >= peer_db.next_refresh:
peer_db.refresh()
......@@ -182,6 +187,8 @@ def main():
tunnel_manager.refresh()
if forwarder != None and time.time() > forwarder.next_refresh:
forwarder.refresh()
if peer_db.socket_file in ready:
peer_db.readSocket()
finally:
for p in [router] + server_process:
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