Commit ab232120 authored by Ulysse Beaugnon's avatar Ulysse Beaugnon

Peers can now discover themselves without a central registry.

Currently, a peer broadcast its address to evrybody but this should change
parent 3b4883a8
......@@ -6,6 +6,13 @@ To be done :
Choose the number of peer we ask to the server
Test the package
remove some peers if their is too much in addpeer
check the ips attributed
remove populate
remove unused methodes from the registry
how do we make sure that none inject some false packets ?
how do we make sur that noone is declaring too much false nodes ?
Put more information in the token mail ( registry ), such as :
......@@ -8,7 +8,7 @@ class PeerManager:
def __init__(self, db_path, registry, key_path, refresh_time, address,
internal_ip, prefix, manual, pp, db_size):
self._refresh_time = refresh_time
self._address = address
self.address = address
self._internal_ip = internal_ip
self._prefix = prefix
self._db_size = db_size
......@@ -17,8 +17,6 @@ class PeerManager:
self._pp = pp
self._manual = manual
self.tunnel_manager = None
self._sock = None
self.socket_file = None'Connecting to peers database...')
self._db = sqlite3.connect(db_path, isolation_level=None)
......@@ -87,9 +85,9 @@ class PeerManager:
return False
def _declare(self):
if self._address != None:
if self.address != None:'Sending connection info to server...')
logging.debug('Info sent')
logging.warning("Warning : couldn't send ip, unknown external config")
......@@ -129,20 +127,14 @@ class PeerManager:
p = subprocess.Popen(('openssl', 'rsautl', '-decrypt', '-inkey', self._key_path),
stdin=subprocess.PIPE, stdout=subprocess.PIPE)
bootpeer = p.communicate(bootpeer)[0].split()
if bootpeer[0] != self._prefix:
if int(self._db.execute("""SELECT COUNT(*) FROM blacklist.flag WHERE prefix = ?""", (bootpeer[0],)).next()[0]) > 0:'BootPeer is blacklisted')
return False
self._db.execute("INSERT INTO peers (prefix, address) VALUES (?,?)", bootpeer)
logging.debug('Boot peer added')
return True
return self._addPeer(bootpeer)
except socket.error:
except sqlite3.IntegrityError, e:
if e.args[0] != 'column prefix is not unique':
except StopIteration:'No peer available for bootstrap')
except:'Unable to bootstrap')
return False
def usePeer(self, prefix):
......@@ -178,8 +170,8 @@ class PeerManager:
external_ip = arg
new_address = list([external_ip, port, proto]
for port, proto, _ in self._pp)
if self._address != new_address:
self._address = new_address
if self.address != new_address:
self.address = new_address'Received new external ip : %s'
% (external_ip,))
......@@ -188,18 +180,22 @@ class PeerManager:
logging.debug('socket.error : %s' % e)'''Connection to server failed while
declaring external infos''')
elif script_type == 'up':
if int(arg) != 0:'Server creation failed, terminating')
raise RuntimeError
logging.debug('Creating the socket for peer advertising')
self._sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
self._sock.bind((self._internal_ip, 326))
self._socket_file = self._sock.makefile()
logging.debug('Unknow message recieved from the openvpn pipe : %s'
% msg)
def readSocket(self):
print 'reading socket'
def readSocket(self, msg):
peer = msg.split(' ')
if len(peer) != 2:
logging.debug('Invalid package recieved : %s' % msg)
def _addPeer(self, peer):
logging.debug('Adding peer %s' % peer)
if int(self._db.execute("""SELECT COUNT(*) FROM blacklist.flag WHERE prefix = ?""", (peer[0],)).next()[0]) > 0:'Peer is blacklisted')
return False
self._db.execute("INSERT INTO peers (prefix, address) VALUES (?,?)", peer)
logging.debug('Peer added')
return True
......@@ -42,13 +42,11 @@ script_type = os.environ['script_type']
if script_type == 'up':
from subprocess import call
dev = os.environ['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)
if sys.argv[1] != 'none':
sys.exit(call(('ip', 'link', 'set', dev, 'up'))
or call(('ip', 'addr', 'add', sys.argv[1], 'dev', dev)))
out = call(('ip', 'link', 'set', dev, 'up'))
sys.exit(call(('ip', 'link', 'set', dev, 'up')))
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 %s/%u' % (ovpn_server, pipe_fd, server_ip, 64)
script_up = '%s %s/%u' % (ovpn_server, server_ip, 64)
script_up = '%s %s none' % (ovpn_server, pipe_fd)
script_up = '%s none' % (ovpn_server)
return openvpn(hello_interval, encrypt,
'--mode', 'server',
import os, traceback, time, subprocess, logging
import random
import socket
import plib
# Be carfull the refresh interval should let the routes be established
......@@ -163,6 +163,13 @@ class TunnelManager:
return True
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
if self._peer_db.address:
ip = '%s:%s:%s:%s:%s:%s:%s:%s' % (peerIp[0:4], peerIp[4:8], peerIp[8:12],
peerIp[12:16], peerIp[16:20], peerIp[20:24], peerIp[24:28], peerIp[28:32])
logging.debug('Notifying peer %s' % ip)
sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
sock.sendto('%s %s\n' % (self._prefix, self._peer_db.address), (ip, 326))
except socket.error, e:
logging.debug('Unable to notify %s' % ip)
logging.debug('socket.error : %s' % e)
#!/usr/bin/env python
import os, sys, select, time
import os, sys, select, time, socket
import argparse, subprocess, sqlite3, logging, traceback
from argparse import ArgumentParser
from re6st import plib, utils, db, upnpigd, tunnel
......@@ -144,6 +144,11 @@ def main():
config.iface_list, network, prefix, 2, config.encrypt)
peer_db.tunnel_manager = tunnel_manager
# Create the socket to listen on
sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
sock.bind(('::', 326))
socket_file = sock.makefile()
# Launch routing protocol. WARNING : you have to be root to start babeld
interface_list = list(tunnel_manager.free_interface_set) \
+ config.iface_list + list(iface
......@@ -175,10 +180,7 @@ def main():
if forwarder != None:
nextUpdate = min(nextUpdate, forwarder.next_refresh)
nextUpdate = max(0, nextUpdate - time.time())
select_list = [read_pipe]
if peer_db.socket_file:
ready, tmp1, tmp2 =, [], [], nextUpdate)
ready, tmp1, tmp2 =[read_pipe, socket_file], [], [], nextUpdate)
if read_pipe in ready:
if time.time() >= peer_db.next_refresh:
......@@ -187,8 +189,8 @@ def main():
if forwarder != None and time.time() > forwarder.next_refresh:
if peer_db.socket_file in ready:
if socket_file in ready:
for p in [router] + server_process:
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment