def listen(self): """ Listen for incoming service requests and provide as necessary. """ self.init_sockets() self.dnsmasq = pwifi_dnsmasq.DnsmasqCTRL() self.dnsmasq.start() self.webserver = sofi_web.sofi_web(self.database,self.stateIndex) while True: logging.info("Waiting for messages...") data, addr = self.receive_msg() s = addr if s is None: s = "hostapd" logging.info("Received message from %s: %s" % (s, data)) split_data = data.split() ssid = split_data[-1] request = split_data[0] source_address = sofi_iftools.sanitizeMac(split_data[1]) #source_address MAC # Requests from legacy clients only contain the request keyword # and SSID. Requests from hostapd provide the IP address which # will be used for the BSS as well. ip = None if len(split_data) == 4: ip = split_data[2] if(sofi_ssid.isSofiSSID(ssid)): ssidObject = sofi_ssid.ssid(SSID=ssid) if request == IPC.PWIFI_IPC_REQUEST: reply = self.handle_request(ssidObject,source_address, ip=ip) if len(reply) != 0: if addr is None: logging.info("Sending reply to hostapd: %s" %reply) logging.info("Hex encoded reply: %s" %(sofi_crypt.hex(reply))) self.sock_hostapd.sendto(reply, IPC.PWIFI_HOSTAPD_INTERFACE) else: logging.info("Sending reply to legacy client " + addr) self.sock_legacy.sendto(reply, addr) else: logging.info("Empty reply. Not sending a reply.") elif request == IPC.PWIFI_IPC_DISBAND: reply = self.handle_disband(ssidObject,source_address) # hostapd does not expect a reply on disband messages -> reply is empty String else: logging.error("Invalid message format. Ignored: %s" %data) continue else: logging.error("Not a valid SSID! Ignored!") continue
def _remove_old_nets(self): """ Removing old networks in order to clean up the network list. Speeds up the scanning and looks nicer. """ networks = self.wpa.wpa_supplicant_list_networks() netlist = networks.split('\n') for item in netlist: item = item.split() if (len(item) < 2): break if (sofi_ssid.isSofiSSID(item[1])): self.wpa.wpa_supplicant_remove_net(item[0])
def _remove_old_nets(self): """ Removing old networks in order to clean up the network list. Speeds up the scanning and looks nicer. """ networks = self.wpa.wpa_supplicant_list_networks() netlist = networks.split('\n') for item in netlist: item = item.split() if(len(item)<2): break if(sofi_ssid.isSofiSSID(item[1])): self.wpa.wpa_supplicant_remove_net(item[0])
def _eval_events(self, event): event = event[3:].split() if (event[0] == "CTRL-EVENT-BSS-ADDED"): apList = self.wpa.wpa_supplicant_get_scan() apList = apList.split('\n') apList = apList[1:-1] for line in apList: item = line.split() if (item[0] == event[2]): if (len(item) < 5): logging.error("Strange AP added: %s" % item) return logging.info("AP added: %s with SSID: %s" % (item[0], item[4])) if (sofi_ssid.isSofiSSID(item[4]) and (self.state == WAITING_FOR_RESPONSE or self.state == ANSWERED_PUZZLE)): ssid = sofi_ssid.ssid(SSID=item[4]) if (ssid.getID() == self.ComId): logging.info( "Received matching response with ComID: %s" % self.ComId) #This is a reply to a request we sent. if (self.state == WAITING_FOR_RESPONSE): logging.info( "Waited for response and response received!" ) self._stop_timeout() self.endScanDiscoverTime = time.time() solution = self._start_puzzle(ssid) logging.info( "Puzzle solved and setting up answer!") self.wpa.wpa_supplicant_remove_net(self.nId) self._start_connect(solution, service=ssid.getService(), private=ssid.isPrivate(), password=self.request[0], bitSize=ssid.getBitSize()) if (self.state == ANSWERED_PUZZLE): logging.info( "Answered puzzle and response received: connecting to Network!" ) elif (event[0] == "CTRL-EVENT-CONNECTED"): logging.debug("Connected event received: %s" % event) if (event[7][4:] == self.nId): if (self.state != CONNECTED): logging.info("Connected to network!") self._stop_timeout() self.connectedBSSID = event[4] self.ie = self._get_ie(self.connectedBSSID, ForceSSID=True) self.end_time = time.time() self.endConnectDiscover = self.end_time if (IS_EVAL): self.state = CONNECTED return while (self.ie == None): time.sleep(DELAY) self.lastScan = self.wpa.wpa_supplicant_get_scan() self.ie = self._get_ie(self.connectedBSSID, ForceSSID=True) logging.info("Received IE: %s" % self.ie) ie_ssid, ie_sofi = self._parse_ie(self.ie) logging.info("Parsed IE: SSID: %s So-Fi: %s" % (ie_ssid, ie_sofi)) self._eval_sofi_ie(ie_sofi) self.state = CONNECTED else: logging.info("Reauth to Network with Id: %s" % self.nId) else: self.ie = self._get_ie(event[4], ForceSSID=False) logging.info("Received IE: %s" % self.ie) elif (event[0] == "CTRL-EVENT-DISCONNECTED"): logging.debug("Disconnect event received: %s" % event) if (self.state == CONNECTED): bssid = event[1][6:] logging.info("Disconnected from Network with BBSID: %s" % bssid) if (bssid == self.connectedBSSID): logging.info("Disconnected from So-Fi Network!") self.wpa.wpa_supplicant_remove_net(self.nId) elif (event[0] == "CTRL-EVENT-SCAN-RESULTS"): self.lastScan = self.wpa.wpa_supplicant_get_scan() else: logging.debug(event)
def _eval_events(self,event): event = event[3:].split() if(event[0]=="CTRL-EVENT-BSS-ADDED"): apList = self.wpa.wpa_supplicant_get_scan() apList = apList.split('\n') apList = apList[1:-1] for line in apList: item = line.split() if(item[0]==event[2]): if(len(item)<5): logging.error("Strange AP added: %s" %item) return logging.info("AP added: %s with SSID: %s" %(item[0],item[4])) if(sofi_ssid.isSofiSSID(item[4]) and (self.state == WAITING_FOR_RESPONSE or self.state == ANSWERED_PUZZLE)): ssid = sofi_ssid.ssid(SSID=item[4]) if(ssid.getID()==self.ComId): logging.info("Received matching response with ComID: %s" %self.ComId) #This is a reply to a request we sent. if(self.state == WAITING_FOR_RESPONSE): logging.info("Waited for response and response received!") self._stop_timeout() self.endScanDiscoverTime = time.time() solution = self._start_puzzle(ssid) logging.info("Puzzle solved and setting up answer!") self.wpa.wpa_supplicant_remove_net(self.nId) self._start_connect(solution, service=ssid.getService(), private=ssid.isPrivate(), password=self.request[0], bitSize=ssid.getBitSize()) if(self.state == ANSWERED_PUZZLE): logging.info("Answered puzzle and response received: connecting to Network!") elif(event[0]=="CTRL-EVENT-CONNECTED"): logging.debug("Connected event received: %s" %event) if(event[7][4:]==self.nId): if(self.state != CONNECTED): logging.info("Connected to network!") self._stop_timeout() self.connectedBSSID = event[4] self.ie = self._get_ie(self.connectedBSSID,ForceSSID=True) self.end_time = time.time() self.endConnectDiscover = self.end_time if(IS_EVAL): self.state = CONNECTED return while(self.ie == None): time.sleep(DELAY) self.lastScan = self.wpa.wpa_supplicant_get_scan() self.ie = self._get_ie(self.connectedBSSID,ForceSSID=True) logging.info("Received IE: %s" %self.ie) ie_ssid,ie_sofi = self._parse_ie(self.ie) logging.info("Parsed IE: SSID: %s So-Fi: %s" %(ie_ssid,ie_sofi)) self._eval_sofi_ie(ie_sofi) self.state = CONNECTED else: logging.info("Reauth to Network with Id: %s" %self.nId) else: self.ie = self._get_ie(event[4],ForceSSID=False) logging.info("Received IE: %s" %self.ie) elif(event[0]=="CTRL-EVENT-DISCONNECTED"): logging.debug("Disconnect event received: %s" %event) if(self.state == CONNECTED): bssid = event[1][6:] logging.info("Disconnected from Network with BBSID: %s" %bssid) if(bssid == self.connectedBSSID): logging.info("Disconnected from So-Fi Network!") self.wpa.wpa_supplicant_remove_net(self.nId) elif(event[0]=="CTRL-EVENT-SCAN-RESULTS"): self.lastScan = self.wpa.wpa_supplicant_get_scan() else: logging.debug(event)