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)
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
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
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
def __del__(self): """ Called when the instance is about to be destroyed. Removes this ap from the mapping """ if self.__ip in map_openflow_vs_ethanol_ip: del map_openflow_vs_ethanol_ip[self.__ip] log.info('Removing AP %s from list' % self.__ip) remove_ap_byIP(self.__ip)
def launch(): """ registra a classe que trata as conexões dos Aps """ log.info("Registering ethanol_ap_server") core.registerNew(ethanol_ap_server) """ ativa parte wireless do servidor ethanol """ log.info("Starting server thread") thread = Thread(target=run_server) thread.daemon = True thread.start()
def run_server(server_address='0.0.0.0', server_port=SERVER_PORT): """ creates an Ethanol server at SERVER_PORT and activates it """ server = (server_address, server_port) # socket provided by the server log.info("Listening @ %s:%i" % server) log.info("Ethanol version %s" % VERSION) if run(server) == -1: log.info("Server error. Not receiving messages!") log.info("Server finished!")
def get_vap_by_mac_address(mac_address): """ get a VAP object by its MAC address (BSSID) @return: a VAP object that matches the mac_address or None if doesn't match @param mac_address: MAC address in dotted format of the Virtual AP (SSID) """ vap = None if (len(__list_of_aps)) > 0: for ap_ip in __list_of_aps: ap = __list_of_aps[ap_ip] fvap = [vp for vp in ap.vaps if vp.mac_address == mac_address] if len(fvap) > 0: vap = fvap[0] log.info(" VAP %s encontrada e anexada" % vap) break return vap
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)
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)
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)
def run_server(server_address=SERVER_ADDR, server_port=SERVER_PORT): """ creates an Ethanol server at SERVER_PORT and activates it @param server_address: bind the server to an interface. if this parameters is '0.0.0.0', then binds to all interfaces. if you want an specific interface you can inform it here, e.g., server_address='localhost'. @type server_address: str @param server_port: server port to bind this python server @type server_port: int """ server = (server_address, server_port) # socket provided by the server log.info("Listening @ %s:%i" % server) log.info("Ethanol version %s" % VERSION) if run(server) == -1: log.info("Server error. Not receiving messages!") log.info("Server finished!")
def __init__(self, socket, intf_name='wlan0'): ''' constructor: creates an object that represents the user connection receives an ip/port pair from the hello message uses this info to connect to the station and retrieve the radio it is connected to ''' log.info('constructor Station (%s,%s)' % socket) super(Station, self).__init__(socket, intf_name) msg, mac_addr, ssid, freq, intf = \ get_sta_link_info(socket, id=self.msg_id, intf_name=intf_name) log.info("get_sta_link_info - mac:%s ssid:%s freq:%d intf:%s" % (mac_addr, ssid, freq, intf)) self.__mac_address = mac_addr self.__linkando() log.info('Station created')
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 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 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 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])) for ssid in list_ssids: if ssid.ssid is None: log.info("Detected a invalid SSID!!!") else: net = Network(ssid.ssid) log.info('[%s] added to network (SSID) list', ssid.ssid) 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)) log.info('New AP created - id: %s', self.id)
def launch(server_address='0.0.0.0', server_port='22223', num_acs_tests=1, intf_name='wlan0', mac_sta='0c:84:dc:d4:7a:73'): """ launch is a default method used by pox to load and run this module """ if server_address == '0.0.0.0': return server_port = int(server_port) """ registra a classe que trata as conexões dos Aps (server_address, server_port) tuple with the AP's ip address and TCP port """ from pox.ethanol.ssl_message.msg_common import VERSION log.info("Running message(s) - Ethanol version %s", VERSION) ''' send a message ''' if True: server = (server_address, server_port) print "Sending messages to ", server # MSG_HELLO from pox.ethanol.ssl_message.msg_hello import send_msg_hello print "sending msg_hello" print send_msg_hello(server=server) # PING # from pox.ethanol.ssl_message.msg_ping import send_msg_ping # from random import randint # num_tries=randint(1,10)+1 # print "sending msg_ping - num_tries=",num_tries # print send_msg_ping(server=server, num_tries=num_tries) # MSG_GET_RADIO_WLANS from pox.ethanol.ssl_message.msg_radio_wlans import get_radio_wlans msg, wlans = get_radio_wlans(server=server, intf_name = intf_name) print wlans # MSG_GET_ACS # msg_acs(connect=server, intf_name = intf_name, num_acs_tests = num_acs_tests) # MSG_GET_TXPOWER # from pox.ethanol.ssl_message.msg_snr_power import get_txpower, set_txpower # msg, value = get_txpower(server=(server_address, server_port), intf_name = intf_name) # print "tx power =", value # value = ( value + 1 ) % 20 # #MSG_SET_TXPOWER # print "setting tx power to",value # set_txpower(server=server, intf_name=intf_name, txpower=value) # #MSG_GET_TXPOWER # msg, value = get_txpower(server=(server_address, server_port), intf_name = intf_name) # print "tx power =", value # MSG_GET_SNR # from pox.ethanol.ssl_message.msg_snr_power import get_snr # msg, value = get_snr(server=server, intf_name = intf_name) # print "SNR =", value # MSG_GET_AP_SSID # from pox.ethanol.ssl_message.msg_ap_ssid import get_ap_ssids # msg, value = get_ap_ssids(server=server, intf_names = [intf_name]) # print value # MSG_GET_MEMORY # from pox.ethanol.ssl_message.msg_memcpu import get_memory_usage # msg, value = get_memory_usage(server=server) # print "memory", value # MSG_GET_CPU # from pox.ethanol.ssl_message.msg_memcpu import get_cpu_usage # msg, value = get_cpu_usage(server=server) # print "cpu", value # MSG_WLAN_INFO # from pox.ethanol.ssl_message.msg_wlan_info import req_wlan_info # msg, info = req_wlan_info(server=server, intf_name_list = [intf_name]) # print info # MSG_GET_AP_IN_RANGE_TYPE # from pox.ethanol.ssl_message.msg_ap_in_range import get_ap_in_range # msg, num_aps, aps = get_ap_in_range(server=server, intf_name = intf_name) # print aps # MSG_GET_STATISTICS # from pox.ethanol.ssl_message.msg_statistics import send_msg_get_statistics # msg, stats = send_msg_get_statistics(server=server, intf_name = intf_name) # print stats # MSG_GET_STA_STATISTICS # MSG_GET_CURRENTCHANNEL # from pox.ethanol.ssl_message.msg_channels import get_currentchannel # msg, value = get_currentchannel(server=server, intf_name=intf_name) # print "current chan = ", value # value = (value + 1) % 11 + 1 # #MSG_SET_CURRENTCHANNEL # from pox.ethanol.ssl_message.msg_channels import set_currentchannel # print "setting new channel to ", value # set_currentchannel(server=server, intf_name=intf_name, channel=value) # msg, value = get_currentchannel(server=server, intf_name=intf_name) # print "current chan = ", value # MSG_GET_TX_BITRATE # from pox.ethanol.ssl_message.msg_bitrates import get_tx_bitrate # msg, value = get_tx_bitrate(server=server, # intf_name = intf_name, # sta_mac=mac_sta) # print value # MSG_GET_TX_BITRATES # from pox.ethanol.ssl_message.msg_bitrates import get_tx_bitrates # msg, value = get_tx_bitrates(server=server, intf_name = intf_name) # print value # MSG_GET_CHANNELINFO # from pox.ethanol.ssl_message.msg_channelinfo import get_channelinfo # msg, value = get_channelinfo(server=server, intf_name = intf_name) # print value # msg, value = get_channelinfo(server=server, intf_name = intf_name, only_channel_in_use=True) # print "MSG_GET_CHANNELINFO\n",value import time time.sleep(1) if True: # quit import sys sys.exit(0)