1. imalse
  2. Untitled project
  3. imalse


imalse / core / ns3 / experiment / experiment.py

this file describe the netns3 experiment for imalse
import ns3
from util.util import abstract_method, get_availabe_scenarios, load_scenario

# for test, just select the first node as server, the second node as BotMaster, all other nodes
# are clients
class ImalseExperiment(object):
    """ Base class for all ImalseExperiment
    server_id_set = [0]
    botmaster_id_set = [1]
    client_id_set = [2, 3, 4]

    def id_set(self):
        return self.server_id_set + self.botmaster_id_set + self.client_id_set

    def initparser(self, parser):
        scenario_ops = get_availabe_scenarios()
        parser.add_option('-s', '--scenario', dest="scenario",
                help='specify the scenario you want to execute. Scenarios available are: %s'%(scenario_ops),

    def _init(self): pass
    def setup(self): pass

    def get_node(self, i):
        """get ith nodes"""

    def node_num(self):

    def node_run(self, node, *args, **kwargs):
        """run a command on a specific node"""
        node.cmd_set.trigger(*args, **kwargs)

    def start_nodes(self):
        """specify when each node should start"""
        # start servers
        for i in self.server_id_set:
            print 'node [%i] type [%s] start at [%f]s'%(i, 'server', 0)
            self.event(0, self.node_run, self.get_node(i), 'start')

        # start clients
        t = 0
        for i in self.client_id_set:
            t += 2
            print 'node [%i] type [%s] start at [%f]s'%(i, 'client', t)
            self.event(t, self.node_run, self.get_node(i), 'start')

        # start BotMaster
        for i in self.botmaster_id_set:
            print 'node [%i] type [%s] start at [%f]s'%(i, 'BotMaster', t+2)
            self.event(t+2, self.node_run, self.get_node(i), 'start')

    def install_cmds(self, srv_addr=None):
        """install different command set to nodes according to their type
        can manually set the server address
        scen = load_scenario(self.options.scenario)
        BOTMASTER_DESC = scen.config.Botmaster()
        SERVER_DESC = scen.config.Server()
        CLIENT_DESC = scen.config.Client()
        #When in netns3 mode set a node in the network as the target of the attack        
            if self.target_id is not None:
                if self.get_node(self.target_id).NODE_TYPE == 'sim_ns3':
                    target_ip = self.get_node(self.target_id).GetObject(ns3.TypeId.LookupByName("ns3::Ipv4")).GetAddress(1, 0).GetLocal()            
                    BOTMASTER_DESC.command.hostname = str(target_ip)
                elif self.get_node(self.target_id).NODE_TYPE == 'real_ns3':
                    BOTMASTER_DESC.command.hostname = BOTMASTER_DESC.server_address
        except AttributeError:
                BOTMASTER_DESC.command.hostname = BOTMASTER_DESC.server_address
            except AttributeError:
        if srv_addr is not None:
            BOTMASTER_DESC.server_address = srv_addr
            SERVER_DESC.server_address = srv_addr
            CLIENT_DESC.server_address = srv_addr     
        for i in xrange(self.node_num):
            if i in self.botmaster_id_set:
                cmd = scen.BotMaster(BOTMASTER_DESC)
            elif i in self.server_id_set:
                cmd = scen.ServerCMD(SERVER_DESC)
            elif i in self.client_id_set:
                cmd = scen.ClientCMD(CLIENT_DESC)

    def get_server_addr(self):
        """The all the server address"""
        addr_set = []
        for i in self.server_id_set:
            ipv4Addr = self.get_node(i).GetObject(ns3.TypeId.LookupByName("ns3::Ipv4")).GetAddress(1, 0)
        return addr_set

    def get_server_nodes(self):
        return [self.get_node(i) for i in self.server_id_set]

    def set_server_info(self):
        """Only Set Server information for simulated nodes"""
        server_addr_set = self.get_server_addr()
        server_set = self.get_server_nodes()
        for i in xrange(self.node_num):
            # if i in self.server_id_set or self.get_node(i).NODE_TYPE.startswith('real'):
                # continue
            if self.get_node(i).NODE_TYPE.startswith('real'):
            self.get_node(i).server_addr_set = server_addr_set
            self.get_node(i).server_set = server_set

    def print_srv_addr(self):
        """print out the address of the server"""
        for i in self.server_id_set:
            ipv4Addr = self.get_node(i).GetObject(ns3.TypeId.LookupByName("ns3::Ipv4")).GetAddress(1, 0)
            print 'The server addr of server [%i ]: '%(i), ipv4Addr