|
@@ -60,10 +60,10 @@ class PcapAddressOperations():
|
|
|
print("Warning: .pcap does not contain any private ips.")
|
|
|
return []
|
|
|
|
|
|
- if count > len(self.priv_ips):
|
|
|
- print("Warning: There are no {} priv IPs in the .pcap file. Returning all existing priv IPs.".format(count))
|
|
|
+ if count > len(self.remaining_priv_ips):
|
|
|
+ print("Warning: There are no more {} private IPs in the .pcap file. Returning all remaining private IPs.".format(count))
|
|
|
|
|
|
- total = min(len(self.priv_ips), count)
|
|
|
+ total = min(len(self.remaining_priv_ips), count)
|
|
|
|
|
|
retr_priv_ips = []
|
|
|
priv_ips = self.remaining_priv_ips
|
|
@@ -72,6 +72,9 @@ class PcapAddressOperations():
|
|
|
retr_priv_ips.append(str(random_priv_ip))
|
|
|
priv_ips.remove(random_priv_ip)
|
|
|
|
|
|
+ if count == 1:
|
|
|
+ return retr_priv_ips[0]
|
|
|
+
|
|
|
return retr_priv_ips
|
|
|
|
|
|
# also use IPs below minimum observed IP?
|
|
@@ -143,8 +146,41 @@ class PcapAddressOperations():
|
|
|
retr_priv_ips.append(str(random_priv_ip))
|
|
|
uncertain_priv_ips.remove(random_priv_ip)
|
|
|
|
|
|
+ if count == 1:
|
|
|
+ return retr_public_ips[0]
|
|
|
+
|
|
|
return retr_priv_ips
|
|
|
|
|
|
+ def get_existing_public_ips(self, count: int=1):
|
|
|
+ """
|
|
|
+ Returns the given number of private IPs that are existent in the pcap file.
|
|
|
+
|
|
|
+ :param count: the number of IPs to return
|
|
|
+ :return: the chosen private IPs
|
|
|
+ """
|
|
|
+
|
|
|
+ # reasonable to include this?
|
|
|
+ if not (len(self.public_ips) > 0):
|
|
|
+ print("Warning: .pcap does not contain any public ips.")
|
|
|
+ return []
|
|
|
+
|
|
|
+ if count > len(self.remaining_public_ips):
|
|
|
+ print("Warning: There are no {} public IPs in the .pcap file. Returning all existing public IPs.".format(count))
|
|
|
+
|
|
|
+ total = min(len(self.remaining_public_ips), count)
|
|
|
+
|
|
|
+ retr_public_ips = []
|
|
|
+ public_ips = self.remaining_public_ips
|
|
|
+ for _ in range(0, total):
|
|
|
+ random_public_ip = choice(tuple(public_ips))
|
|
|
+ retr_public_ips.append(str(random_public_ip))
|
|
|
+ public_ips.remove(random_public_ip)
|
|
|
+
|
|
|
+ if count == 1:
|
|
|
+ return retr_public_ips[0]
|
|
|
+
|
|
|
+ return retr_public_ips
|
|
|
+
|
|
|
def _init_ipaddress_ops(self):
|
|
|
"""
|
|
|
Load and process data needed to perform functions on the IP addresses contained in the statistics
|
|
@@ -152,7 +188,9 @@ class PcapAddressOperations():
|
|
|
|
|
|
all_ips = self.statistics.process_db_query("all(ipAddress)", print_results=False)
|
|
|
|
|
|
+ # Prepare private address operations and on the side save all external IPs
|
|
|
# find the private IP segment in use
|
|
|
+ public_ips = set()
|
|
|
priv_ip_segment = None
|
|
|
self.contains_priv_ips = False
|
|
|
first_priv_ip = None
|
|
@@ -170,6 +208,9 @@ class PcapAddressOperations():
|
|
|
first_priv_ip = ip
|
|
|
self.contains_priv_ips = True
|
|
|
break
|
|
|
+ # new function in IPv4 to shorten this?
|
|
|
+ elif (not ip.is_localhost()) and (not ip.is_localhost()) and (not ip.is_multicast()) and (not ip.is_reserved()) and (not ip.is_zero_conf()):
|
|
|
+ public_ips.add(ip)
|
|
|
|
|
|
if not self.contains_priv_ips:
|
|
|
#print("The Pcap File does not contain any private IPs")
|
|
@@ -190,6 +231,9 @@ class PcapAddressOperations():
|
|
|
max_priv_ip = ip
|
|
|
elif ip < min_priv_ip:
|
|
|
min_priv_ip = ip
|
|
|
+ # new function in IPv4 to shorten this?
|
|
|
+ elif (not ip.is_private()) and (not ip.is_localhost()) and (not ip.is_localhost()) and (not ip.is_multicast()) and (not ip.is_reserved()) and (not ip.is_zero_conf()):
|
|
|
+ public_ips.add(ip)
|
|
|
|
|
|
# save the certain unused priv IPs of the network
|
|
|
unused_priv_ips = set()
|
|
@@ -199,6 +243,8 @@ class PcapAddressOperations():
|
|
|
unused_priv_ips.add(ip)
|
|
|
|
|
|
# save the gathered information for efficient later use
|
|
|
+ self.public_ips = frozenset(public_ips)
|
|
|
+ self.remaining_public_ips = public_ips
|
|
|
self.min_priv_ip, self.max_priv_ip = min_priv_ip, max_priv_ip
|
|
|
self.max_uncertain_priv_ip = max_priv_ip
|
|
|
self.priv_ips = frozenset(priv_ips)
|
|
@@ -207,5 +253,3 @@ class PcapAddressOperations():
|
|
|
self.generated_uncertain_ips = set()
|
|
|
self.uncertain_priv_ips = set()
|
|
|
self.priv_ip_segment = priv_ip_segment
|
|
|
-
|
|
|
-
|