Example #1
0
    def run(self):
        log.info('starting daemon')
        self.styx = Styx(config.styx_unix_socket_path)
        
        sleep_count = 0
        while True:
            try:
                self.ipop_addr = self.get_ip_address('tapipop')
                log.info('found our IPOP IPv4 address: %s' % self.ipop_addr)
                break
            except IOError:
                if sleep_count % 10 == 0:
                    log.critical('could not find IPOP IPv4 address. Sleeping.')
                time.sleep(0.5)
                sleep_count += 1
        
        try:
            self.ipop_listener = socket.socket(socket.AF_INET,
                                               socket.SOCK_DGRAM)
        except:
            log.critical('error creating IPOP socket')
            sys.exit(1)
        self.ipop_listener.bind((config.ipop_host, config.ipop_port))

        while True:
            addr = self.ipop_listener.recv(16).strip()
            log.info('received request for connection to %s' % addr)
            if addr in self.connections:
                log.info('discarding request for duplicate connection '
                         'to %s' % addr)
                continue

            self.connections.add(addr)
            conn_id = self.styx.message_id

            self.styx.addConfig(
                'conn_%d' % conn_id,
                self.ipop_addr,
                addr,
                'leftcert=myCert.pem',
                'rightid=%any',
                'leftid=%any',
                'keyexchange=ikev2'
            )
            self.styx.connect(
                False,
                'conn_%d' % conn_id
            )
Example #2
0
class Acheron(Daemon):
    def __init__(self, *args, **kwargs):
        Daemon.__init__(self, *args, **kwargs)
        self.connections = set()

    @staticmethod
    def get_ip_address(ifname):
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        return socket.inet_ntoa(fcntl.ioctl(
                s.fileno(),
                0x8915,  # SIOCGIFADDR
                struct.pack('256s', ifname[:15])
               )[20:24])

    def run(self):
        log.info('starting daemon')
        self.styx = Styx(config.styx_unix_socket_path)
        
        sleep_count = 0
        while True:
            try:
                self.ipop_addr = self.get_ip_address('tapipop')
                log.info('found our IPOP IPv4 address: %s' % self.ipop_addr)
                break
            except IOError:
                if sleep_count % 10 == 0:
                    log.critical('could not find IPOP IPv4 address. Sleeping.')
                time.sleep(0.5)
                sleep_count += 1
        
        try:
            self.ipop_listener = socket.socket(socket.AF_INET,
                                               socket.SOCK_DGRAM)
        except:
            log.critical('error creating IPOP socket')
            sys.exit(1)
        self.ipop_listener.bind((config.ipop_host, config.ipop_port))

        while True:
            addr = self.ipop_listener.recv(16).strip()
            log.info('received request for connection to %s' % addr)
            if addr in self.connections:
                log.info('discarding request for duplicate connection '
                         'to %s' % addr)
                continue

            self.connections.add(addr)
            conn_id = self.styx.message_id

            self.styx.addConfig(
                'conn_%d' % conn_id,
                self.ipop_addr,
                addr,
                'leftcert=myCert.pem',
                'rightid=%any',
                'leftid=%any',
                'keyexchange=ikev2'
            )
            self.styx.connect(
                False,
                'conn_%d' % conn_id
            )