From dda3e846d2c18fbf74ad1c7accc403d9195f3f31 Mon Sep 17 00:00:00 2001 From: Masayoshi Honda Date: Sat, 13 Apr 2019 01:28:01 +0900 Subject: [PATCH 1/2] Modified the way to exclude the network and broadcast addresses. --- tello_manager.py | 39 ++++++++++++++------------------------- 1 file changed, 14 insertions(+), 25 deletions(-) diff --git a/tello_manager.py b/tello_manager.py index a560ed0..9dba8db 100644 --- a/tello_manager.py +++ b/tello_manager.py @@ -48,15 +48,13 @@ def find_avaliable_tello(self, num): """ print '[Start_Searching]Searching for %s available Tello...\n' % num - subnets, address = self.get_subnets() + networks = self.get_ipv4_networks() possible_addr = [] - for subnet, netmask in subnets: - for ip in IPNetwork('%s/%s' % (subnet, netmask)): - # skip local and broadcast - if str(ip).split('.')[3] == '0' or str(ip).split('.')[3] == '255': - continue - possible_addr.append(str(ip)) + for network in networks: + for ip in network.iter_hosts(): + if str(network.ip) != str(ip): + possible_addr.append(str(ip)) while len(self.tello_ip_list) < num: print '[Still_Searching]Trying to find Tello in subnets...\n' @@ -65,11 +63,8 @@ def find_avaliable_tello(self, num): for tello_ip in self.tello_ip_list: if tello_ip in possible_addr: possible_addr.remove(tello_ip) - # skip server itself + # send each of the addresses a command. for ip in possible_addr: - if ip in address: - continue - # record this command self.log[ip].append(Stats('command', len(self.log[ip]))) self.socket.sendto(b'command', (ip, 8889)) @@ -81,23 +76,20 @@ def find_avaliable_tello(self, num): temp[ip] = self.log[ip] self.log = temp - - - def get_subnets(self): + def get_ipv4_networks(self): """ Look through the server's internet connection and - returns subnet addresses and server ip - :return: list[str]: subnets - list[str]: addr_list + returns ipv4 networks + :return: list[IPNetwork]: networks """ - subnets = [] ifaces = netifaces.interfaces() - addr_list = [] + networks = [] for myiface in ifaces: addrs = netifaces.ifaddresses(myiface) if socket.AF_INET not in addrs: continue + # Get ipv4 stuff ipinfo = addrs[socket.AF_INET][0] address = ipinfo['addr'] @@ -107,12 +99,9 @@ def get_subnets(self): if netmask != '255.255.255.0': continue - # Create ip object and get - cidr = netaddr.IPNetwork('%s/%s' % (address, netmask)) - network = cidr.network - subnets.append((network, netmask)) - addr_list.append(address) - return subnets, addr_list + network = IPNetwork('%s/%s' % (address, netmask)) + networks.append(network) + return networks def get_tello_list(self): return self.tello_list From 1316ff95a2e45f219bc588f1473be5a627d3f8e7 Mon Sep 17 00:00:00 2001 From: Masayoshi Honda Date: Sat, 13 Apr 2019 01:39:55 +0900 Subject: [PATCH 2/2] Allowed the network part which more than 24 bits. --- tello_manager.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tello_manager.py b/tello_manager.py index 9dba8db..7be70a2 100644 --- a/tello_manager.py +++ b/tello_manager.py @@ -96,7 +96,7 @@ def get_ipv4_networks(self): netmask = ipinfo['netmask'] # limit range of search. This will work for router subnets - if netmask != '255.255.255.0': + if not netmask.startswith('255.255.255'): continue network = IPNetwork('%s/%s' % (address, netmask))