예제 #1
0
def toggle_device(mac, ip, enabled):
    from apate_redis import ApateRedis
    if not check_ip(ip) or not check_mac(mac):
        return 27

    try:
        with open(CONFIG_FILE) as config:
            data = json.load(config)
    except ValueError as ve:
        print "Could not parse the configuration file"
        print str(ve)
        return 28
    except IOError as ioe:
        print "An error occurred while trying to open the configuration file"
        print str(ioe)
        return 29

    if 'interface' not in data:
        print "The configuration file does not include all necessary options"
        return 30

    network = get_network(data['interface'], check_ip(ip))
    if not network:
        return 31

    try:
        redis = ApateRedis(network, logging.getLogger('config'))
        if enabled:
            redis.enable_device(mac, ip, network)
        else:
            redis.disable_device(mac, ip, network)
    except:
        return 32

    return 0
예제 #2
0
    def __init__(self, logger, interface, pidfile, stdout, stderr):
        """Initialises several things needed to define the daemons behaviour.

        Args:
            logger (logging.Logger): Used for logging messages.
            interface (str): The network interface which should be used. (e.g. eth0)
            pidfile (str): Path of the pidfile, used by the daemon.
            stdout (str): Path of stdout, used by the daemon.
            stderr (str): Path of stderr, used by the daemon.

        Raises:
            DaemonError: Signalises the failure of the daemon.
        """
        super(self.__class__, self).__init__(logger, interface, pidfile, stdout, stderr)
        self.redis = ApateRedis(str(self.network.network), logger)

        # Initialise threads
        self.sniffthread = SelectiveSniffThread(self.interface, self.gateway, self.mac, self.gate_mac, self.redis)
        self.sniffthread.daemon = True
        self.psthread = PubSubThread(self.redis, self.logger)
        self.psthread.daemon = True
        self.arpthread = ARPDiscoveryThread(self.gateway, str(self.network.network))
        self.arpthread.daemon = True
        self.igmpthread = IGMPDiscoveryThread(self.gateway, str(self.network.network), self.ip, self.mac)
        self.igmpthread.daemon = True
예제 #3
0
    def __init__(self, logger, interface, ipv6):
        """Initialises several things needed to define the daemons behaviour.

        Args:
            logger (logging.Logger): Used for logging messages.
            interface (str): The network interface which should be used. (e.g. eth0)
            pidfile (str): Path of the pidfile, used by the daemon.
            stdout (str): Path of stdout, used by the daemon.
            stderr (str): Path of stderr, used by the daemon.
            dns_file (str): Path of file containing the nameservers.

        Raises:
            DaemonError: Signalises the failure of the daemon.
        """
        super(self.__class__, self).__init__()

        self.exit = mp.Event()

        self.interface = interface
        self.logger = logger

        # add redis objects to the ip tuples
        self.ipv6 = ipv6._replace(
            redis=ApateRedis(str(ipv6.network.network), logger))

        # used for thread synchronisation (for waking this thread)
        self.sleeper = threading.Condition()

        self.threads = {}
        # Initialise threads
        self.threads['sniffthread'] = SelectiveIPv6SniffThread(
            self.interface, self.ipv6, self.sleeper, self.logger)
        self.threads['icmpv6thread'] = MulticastPingDiscoveryThread(
            self.interface)
        self.threads['mldv2thread'] = MulticastListenerDiscoveryThread(
            self.interface)
        self.threads['psthread6'] = PubSubThread(self.ipv6, self.logger,
                                                 self.spoof_devices)

        # declare all threads as deamons
        for worker in self.threads:
            self.threads[worker].daemon = True