Commit 7152bb0d authored by Ulysse Beaugnon's avatar Ulysse Beaugnon

using set instead of sets

using the same socket for sending and listenning
parent a00f6201
......@@ -22,6 +22,10 @@ class PeerManager:
self._manual = manual
self.tunnel_manager = None
self.sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
self.sock.bind((self._internal_ip, 326))
self.socket_file = self._sock.makefile()
logging.info('Connecting to peers database...')
self._db = sqlite3.connect(db_path, isolation_level=None)
logging.debug('Database opened')
......@@ -165,7 +169,8 @@ class PeerManager:
logging.debug('Unknow message recieved from the openvpn pipe : %s'
% msg)
def readSocket(self, msg):
def readSocket(self):
msg = self.socket_file.readline()
peer = msg.replace('\n', '').split(' ')
if len(peer) != 2:
logging.debug('Invalid package recieved : %s' % msg)
......
import os, traceback, time, subprocess, logging
import socket
import sets
import random
import plib
import utils
......@@ -127,7 +126,7 @@ class TunnelManager:
def _countRoutes(self):
logging.debug('Starting to count the routes on each interface...')
self._peer_db.clear_blacklist(0)
possiblePeers = sets.Set([])
possiblePeers = set()
for iface in self._iface_to_prefix.keys():
self._connection_dict[self._iface_to_prefix[iface]].routes = 0
for line in open('/proc/net/ipv6_route'):
......@@ -180,8 +179,7 @@ class TunnelManager:
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.trace('Notifying peer %s' % ip)
sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
sock.sendto('%s %s\n' % (self._prefix, utils.address_str(self._peer_db.address)), (ip, 326))
self.peer_db.sock.sendto('%s %s\n' % (self._prefix, utils.address_str(self._peer_db.address)), (ip, 326))
except socket.error, e:
logging.debug('Unable to notify %s' % ip)
logging.debug('socket.error : %s' % e)
......@@ -144,11 +144,6 @@ 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
......@@ -181,7 +176,10 @@ def main():
if forwarder != None:
nextUpdate = min(nextUpdate, forwarder.next_refresh)
nextUpdate = max(0, nextUpdate - time.time())
ready, tmp1, tmp2 = select.select([read_pipe, socket_file], [], [], nextUpdate)
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:
......@@ -190,8 +188,8 @@ def main():
tunnel_manager.refresh()
if forwarder != None and time.time() > forwarder.next_refresh:
forwarder.refresh()
if socket_file in ready:
peer_db.readSocket(socket_file.readline())
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