Esempio n. 1
0
    def __init__(self, server, ssid, radio, mac_address):
        """ constructor:
        """
        # if not isinstance(ap, AP):
        #   raise ValueError("Parameter is must be a AP class")

        self.__intf_name = radio.wiphy
        log.info("Creating a VAP in %s interface", self.__intf_name)
        super(VAP, self).__init__(server, self.__intf_name)

        self.__server = server  #: saves the reference to server of ap
        self.__mac_address = mac_address  #: virtual ap's mac address
        self.__radio = radio  #: physical radio to which the vap is attached

        log.debug("Registering_functions: %s", self.__mac_address)
        # register the association process for this ap
        register_functions(self.__mac_address, self)

        """ list of stations connected to this vap """
        self.__list_of_stations = []

        self.__ssid = ssid  #: setting ssid will configure VAP
        self.__enabled = False
        self.__mgmtFrame = dict()  # keep a list of listeners for each type of mgmt frame received
        log.info("Created VAP with id:%s in interface %s", self.id, self.__intf_name)
Esempio n. 2
0
def add_station(client_address):
    '''
      Create (and return) possibly several objects,
      one for each wireless connections identified by (client_address,
      interface name).
      This function updates a list of these objects.

      client_address = (ip, port) used by the Hello message's process
    '''
    ip = client_address[0]

    if ip not in list_of_stations:
        log.info("Starting Station object with IP %s", ip)
        msg, intfs = get_interfaces(server=client_address, m_id=0)
        ''' select only wireless interfaces '''
        intfs = [intf.intf_name for intf in intfs if intf.is_wifi is True]
        log.info("Found %d wireless interface in the device: %s", len(intfs),
                 ",".join(intfs))
        if len(intfs) > 0:
            list_of_stations[ip] = {}
            for intf_name in intfs:
                log.info("Station interface: %s", intf_name)
                station = Station(socket=client_address, intf_name=intf_name)
                list_of_stations[ip][intf_name] = station
    else:
        log.debug("Station with IP %s exists", ip)
Esempio n. 3
0
def add_ap(client_address):
    """
        Create (and return) an AP object for the the device represented
        by the tuple client_address.
        This function updates a list of these objects.

        used by the Hello message's process

        @param client_address: tuple with (ip, port) used to make a socket
        connection to the AP
        @type client_address: tuple or list
    """
    ip = client_address[0]
    port = client_address[1]

    if not(ip in __list_of_aps):
        # create a new ap

        # comentado porque nao tem todas as funcoes do AP
        __list_of_aps[ip] = AP(ip, port)
        log.info("Adding AP with IP %s to the list of connected aps (size %d)"
                 % (ip, len(__list_of_aps)))
    else:
        log.debug('AP %s exists' % ip)
        pass
Esempio n. 4
0
 def _802_11b_Preamble(self):
     """ connect to ap requesting which type of preamble is set"""
     server = self.__get_connection()
     msg, value = get_preamble(server,
                               id=self.msg_id,
                               intf_name=self.__wiphy_name)
     log.debug("preamble message received with value %d", value)
     return value
Esempio n. 5
0
 def cpu(self):
     """ physical device's CPU usage """
     if self.__intf_name is None:
         return -1
     server = self.get_connection
     msg, value = get_cpu_usage(server, id=self.msg_id)
     log.debug('CPU device %f', value)
     return value
Esempio n. 6
0
 def memory(self):
     """ physical device's memory usage """
     if self.__intf_name is None:
         return -1
     server = self.get_connection
     msg, value = get_memory_usage(server, id=self.msg_id)
     log.debug('Memory device %f', value)
     return value
Esempio n. 7
0
 def beaconInterval(self):
     """connect to ap requesting beacon interval value"""
     server = self.__get_connection()
     msg, value = get_beacon_interval(server,
                                      id=self.msg_id,
                                      intf_name=self.__wiphy_name)
     log.debug("beacon interval: %d", value)
     return value
Esempio n. 8
0
 def __linkando(self):
     self.__vap = get_vap_by_mac_address(self.__mac_address)
     if self.__vap is None:
         self.__radio = None
         log.debug("VAP <<nao encontrada>> na criacao da Station")
     else:
         self.__radio = self.__vap.radio
         self.__vap.register_station(station=self)
         log.debug("VAP <<encontrada>> na criacao da Station")
Esempio n. 9
0
 def _handle_ConnectionUp(self, event):
     """ when a connection is up, inserts this openflow device in a mapping list
         if this devices sends an Ethanol Hello then we know that it has openflow and ethanol capabilities
     """
     log.debug("Connection %s" % (event.connection, ))
     # registra um novo AP
     sock = event.connection.sock
     ip, port = sock.getpeername()
     add_ap_openflow(ip)
Esempio n. 10
0
 def _802_11b_Preamble(self, value):
     """
       set new preamble, returns nothing
     """
     server = self.__get_connection()
     set_preamble(server,
                  id=self.msg_id,
                  intf_name=self.__wiphy_name,
                  preamble=value)
     log.debug("preamble set to %d", value)
Esempio n. 11
0
 def packetsLost(self):
     """ number of packets lost on this interface (cumulative value) """
     if self.__intf_name is None:
         return -1
     server = self.get_connection
     msg, value = send_msg_get_packetslost(server,
                                           id=self.msg_id,
                                           intf_name=self.__intf_name)
     log.debug('packetsLost device %f', value)
     return value
Esempio n. 12
0
 def txpower(self, new_value):
     """set current tx power"""
     if self.__intf_name is None:
         return -1
     server = self.get_connection
     set_txpower(server,
                 id=self.msg_id,
                 intf_name=self.__intf_name,
                 txpower=new_value)
     log.debug('set TXPower device to %f', new_value)
Esempio n. 13
0
 def txpower(self):
     """ retrieve the TX power """
     if self.__intf_name is None:
         return -1
     server = self.get_connection
     msg, value = get_txpower(server,
                              id=self.msg_id,
                              intf_name=self.__intf_name)
     log.debug('get TXPower device %f', value)
     return value
Esempio n. 14
0
 def SNR(self):
     """ retrieve current SNR """
     if self.__intf_name is None:
         return -1
     server = self.get_connection
     msg, value = get_snr(server,
                          id=self.msg_id,
                          intf_name=self.__intf_name)
     log.debug('SNR %f', value)
     return value
Esempio n. 15
0
 def getACS(self, num_tests=1):
     """ request that the AP computes the ACS factor for each frequency
     in the intf_name interface
     """
     server = self.__get_connection()
     msg, num_chan, acs = get_acs(server,
                                  id=self.msg_id,
                                  intf_name=self.__wiphy_name,
                                  num_tests=num_tests)
     log.debug("ACS message received with %d channels", num_chan)
     return num_chan, acs
Esempio n. 16
0
 def beaconInterval(self, value=100):
     """ connect to AP to set beacon interval value
         returns nothing
     """
     server = self.__get_connection()
     set_beacon_interval(server,
                         value,
                         id=self.msg_id,
                         intf_name=self.__wiphy_name,
                         beacon_interval=value)
     log.debug("beacon interval set to %s", value)
Esempio n. 17
0
 def currentChannel(self, new_channel):
     """
       tries to set the ap channel.
       @note: to confim that the channel was changed, issue currentChannel()
     """
     server = self.__get_connection()  # allows to send message to the AP
     set_currentchannel(server,
                        id=self.msg_id,
                        channel=new_channel,
                        intf_name=self.__wiphy_name)
     log.debug("canal: %d interface: %s tcp: %s:%d", new_channel,
               self.__wiphy_name, server[0], server[1])
Esempio n. 18
0
 def getAPsInRange(self):
     """get aps that are in range.
       @note: this method is not precise, because it relies on the spare
       time the device has to scan all the channels
     """
     if self.__intf_name is None:
         return -1, None
     server = self.get_connection
     msg, num_aps, aps = get_ap_in_range(server,
                                         id=self.msg_id,
                                         intf_name=self.__intf_name)
     log.debug('get ap_in_range device %d, %f', num_aps, aps)
     return num_aps, aps
Esempio n. 19
0
 def __init__(self, socket, intf_name):
     """ creates a device object (used by VAP and STATION)
     @param socket: tuple (ip, port_num)
     @param intf_name: name of the wireless interface that this device uses
     """
     log.debug("starting DEVICE constructor")
     self.__id = uuid.uuid4()  # UUID
     self.__socket = socket
     # socket (ip, port) that will be used to connect to this station
     self.__ip, self.__port = socket
     self.__intf_name = intf_name
     self.__mac_address = None
     self.__msg_id = 0
     log.debug("DEVICE id:%s created", self.__id)
Esempio n. 20
0
def process_hello(received_msg, fromaddr):
    """returns the message to the ssl server process
      @param received_msg:
      @param fromaddr: ip address of the device that sent this message
      @func_hello: event
    """
    msg = msg_hello.parse(received_msg)
    client_port = msg['tcp_port']
    client_socket = (fromaddr[0], client_port)

    log.debug("Hello msg received.")
    if msg['device_type'] == 1:
        # create ap object
        log.debug("\tConnect to AP @ %s:%d" % client_socket)
        ap = add_ap(client_socket)  # returns ap
        log.debug("AP %s" % ap)
    elif msg['device_type'] == 2:
        log.info("Connect to STA @ %s:%d" % client_socket)
        station = add_station(client_socket)
        log.debug("Station %s" % station)

    events_hello.on_change(msg=msg, fromaddr=fromaddr)  # call all registered functions

    # only send back the same message
    return received_msg
Esempio n. 21
0
def send_msg_hello(server, m_id=0):
    """
      @param server: tuple (ip, port_num)
      @param m_id: message id

      @return: msg - received message
    """
    ssl_sock, sckt = connect_ssl_socket(server)

    # print "send_msg_hello id:", m_id
    # 1) create message
    msg_struct = Container(m_type=MSG_TYPE.MSG_HELLO_TYPE,
                           m_id=m_id,
                           p_version_length=len_of_string(VERSION),
                           p_version=VERSION,
                           m_size=0,
                           device_type=0,
                           tcp_port=SERVER_PORT,
                           rtt=0
                           )
    msg = msg_hello.build(msg_struct)
    # print "hello enviado para servidor > ", hexadecimal(msg)
    # 2) sending message
    t0 = datetime.now()
    log.debug(hexadecimal(msg))
    num_bytes = ssl_sock.write(msg)
    log.debug("num bytes enviados: %d" % num_bytes)

    # 3) retrieve server's response
    received_msg = ssl_sock.read(BUFFER_SIZE)
    if received_msg != '':
        t1 = datetime.now()
        # print "msg recebida > ", hexadecimal(received_msg)

        if is_error_msg(received_msg):
            msg = get_error_msg(received_msg)
        else:
            # print "msg recebida > ", hexadecimal(received_msg)
            msg = msg_hello.parse(received_msg)
            msg.rtt = t1 - t0
    else:
        msg = None
    ssl_sock.close()
    sckt.close()
    return msg
Esempio n. 22
0
    def __init__(self, ssid):
        """
          create a network with ESSID = ssid
        """
        for i in list_of_networks():
            if i[0] == ssid:
                raise ValueError("SSID %s already exists!" % self.__SSID)

        if ssid in list_of_networks():
            log.debug('ssid %s already exists in ')

        self.__id = uuid4()  # random UUID

        self.__SSID = ssid
        self.__listVAP = []
        self.__msg_id = 0  # message id used to identify the msg to the device
        log.info('SSID: %s', self.__SSID)
        add_network(self.__SSID, self)
        log.info('constructor Network %s ended', self.__SSID)
Esempio n. 23
0
def process_hello(received_msg, fromaddr):
    """returns the message to the ssl server process
      @param received_msg:
      @param fromaddr: ip address of the device that sent this message
      @func_hello: event
    """
    msg = msg_hello.parse(received_msg)
    client_port = msg['tcp_port']
    client_socket = (fromaddr[0], client_port)

    events_hello.on_change(msg=msg,
                           fromaddr=fromaddr)  # call all registered functions

    if msg['device_type'] == 1:
        # Creates and returns ap object if it doesn't already exist
        ap = add_ap(client_socket)
        if ap is not None:
            log.debug("\tConnected to AP @ %s:%d" % client_socket)
            log.debug("List of connected APs: %s", connected_aps().keys())
    elif msg['device_type'] == 2:
        log.info("Connect to STA @ %s:%d" % client_socket)
        station = add_station(client_socket)
        log.debug("Station %s" % station)

    # only send back the same message
    return received_msg
Esempio n. 24
0
    def __init__(self, ssid):
        """
            create a network with ESSID = ssid
            add the ssid to the list __list_of_networks, if does not exist
            if exists triggers an error
        """
        if ssid in list_of_networks():
            # don't allow to create two networks with the same SSID
            log.debug('ssid %s already exists', ssid)
            raise ValueError("SSID %s already exists!" % ssid)
        else:
            # create the network
            self.__id = uuid4()  # random UUID
            # set the name of the SSID
            self.__SSID = ssid
            self.__listVAP = []
            self.__msg_id = 0  # message id used to identify the msg to the device
            # if the Netword does not exists, insert it in the list
            add_network(ssid, self)

            log.info('SSID: %s', self.__SSID)
            log.info('Constructor Network %s ended', self.__SSID)
Esempio n. 25
0
def bogus_hello_on_change(**kwargs):
    log.debug("hello message received: %s" % kwargs['fromaddr'][0] if 'fromaddr' in kwargs else "")
Esempio n. 26
0
    def __init__(self, ip, port=SERVER_PORT):
        """
         constructor
         @param ip: socket IP address to connect to the physical AP
         @param port: socket port to connect to the physical AP
        """
        # import placed here to avoid 'import loop'
        from pox.ethanol.ethanol.radio import Radio
        from pox.ethanol.ethanol.network import Network
        from pox.ethanol.ethanol.network import add_network, get_or_create_network_by_ssid

        self.__id = uuid.uuid4()  #
        # client_address tuple
        self.__ip = ip
        self.__port = port
        self.__msg_id = 0
        self.__radios = {}
        self.__listVAP = []
        server = self.__get_connection()
        self.___wiphys = set()
        map_openflow_vs_ethanol_ip[ip] = self
        self.__stats_msec = -1  # disabled
        self.__stats_alpha = 0.1
        """ retrieve and create radios (represented by the physical
        wifi interfaces)
        """
        msg, wlans = get_radio_wlans(server)
        intf_x_mac = {}

        log.info('wireless interfaces: [%s]' % ",".join([
            _w['intf_name']
            for _w in wlans if _w is not None and _w.intf_name is not None
        ]))
        if wlans is not None:
            # identify distinct set of phy interfaces
            for wlan in wlans:
                wiphy_idx = wlan.wiphy
                wiphy_name = wlan.intf_name
                intf_x_mac[wlan.intf_name] = wlan.mac_addr
                self.___wiphys.add(wiphy_name)
                # create radio objects belonging to this AP
                radio = Radio(self, wiphy_name, ip, port)
                self.__radios[wiphy_name] = radio

        msg, list_ssids = get_ap_ssids(server)

        if list_ssids is not None and len(list_ssids) > 0:
            # TODO: tratar quando o ssid vem nulo
            log.info("SSIDs in Radio: %s" % ",".join([
                _ssid['ssid'] for _ssid in list_ssids
                if _ssid is not None and _ssid.ssid is not None
            ]))
            for ssid in list_ssids:
                if ssid is None or ssid.ssid is None:
                    log.info("Detected a invalid SSID!!!")
                else:
                    try:
                        net = Network(
                            ssid.ssid
                        )  # create the object. This init also inserts the ssid to a control list
                        log.info('[%s] added to network (SSID) list',
                                 ssid.ssid)
                    except ValueError:
                        # exception if network exists
                        log.debug('Network SSID %s already exists', ssid.ssid)
                        net = get_or_create_network_by_ssid(
                            ssid.ssid)  # retrieve the network

            log.info('Creating and association the VAP objects')
            #
            # retrieve configured vaps
            # and create vap objects
            #
            for i in range(len(list_ssids)):
                intf_name = list_ssids[i]['intf_name']
                ssid = list_ssids[i]['ssid']
                if intf_name in self.__radios:
                    # if there is no such ssid in list_of_networks
                    # (network.py) add it
                    vap = \
                        self.createvirtualap_and_insert_listvap(ssid,
                                                                self.__radios[intf_name],
                                                                intf_x_mac[intf_name])
            log.info("Num# of VAPs: %d" % len(self.__listVAP))
        else:
            log.debug("AP returned no SSIDs")

        log.info('New AP created - id: %s', self.id)