mirror of
https://github.com/nmap/nmap.git
synced 2025-12-10 17:59:04 +00:00
Reduce Zenmap's Topology noise by collapsing equivalent 'anonymous' nodes
This commit is contained in:
@@ -176,11 +176,21 @@ class TracerouteHostInfo(object):
|
|||||||
hostnames = property(lambda self: self.hostname and [self.hostname] or [])
|
hostnames = property(lambda self: self.hostname and [self.hostname] or [])
|
||||||
|
|
||||||
|
|
||||||
|
def find_hop_by_ttl(hops, ttl):
|
||||||
|
assert ttl >= 0, "ttl must be non-negative"
|
||||||
|
if ttl == 0: # Same machine (i.e. localhost)
|
||||||
|
return {"ipaddr": "127.0.0.1/8"}
|
||||||
|
for h in hops:
|
||||||
|
if ttl == int(h["ttl"]):
|
||||||
|
return h
|
||||||
|
return None
|
||||||
|
|
||||||
def make_graph_from_hosts(hosts):
|
def make_graph_from_hosts(hosts):
|
||||||
#hosts = parser.get_root().search_children('host', deep=True)
|
#hosts = parser.get_root().search_children('host', deep=True)
|
||||||
graph = Graph()
|
graph = Graph()
|
||||||
nodes = list()
|
nodes = list()
|
||||||
node_cache = {}
|
node_cache = {}
|
||||||
|
invalid_node_cache = {}
|
||||||
|
|
||||||
# Setting initial reference host
|
# Setting initial reference host
|
||||||
main_node = NetNode()
|
main_node = NetNode()
|
||||||
@@ -210,13 +220,7 @@ def make_graph_from_hosts(hosts):
|
|||||||
# Getting nodes of host by ttl
|
# Getting nodes of host by ttl
|
||||||
for ttl in range(1, max(ttls) + 1):
|
for ttl in range(1, max(ttls) + 1):
|
||||||
if ttl in ttls:
|
if ttl in ttls:
|
||||||
# Find a hop by ttl
|
hop = find_hop_by_ttl(hops, ttl)
|
||||||
hop = None
|
|
||||||
for h in hops:
|
|
||||||
if ttl == int(h["ttl"]):
|
|
||||||
hop = h
|
|
||||||
break
|
|
||||||
|
|
||||||
node = node_cache.get(hop["ipaddr"])
|
node = node_cache.get(hop["ipaddr"])
|
||||||
if node is None:
|
if node is None:
|
||||||
node = NetNode()
|
node = NetNode()
|
||||||
@@ -245,14 +249,34 @@ def make_graph_from_hosts(hosts):
|
|||||||
else:
|
else:
|
||||||
graph.set_connection(node, prev_node)
|
graph.set_connection(node, prev_node)
|
||||||
else:
|
else:
|
||||||
node = NetNode()
|
# Add an "anonymous" node only if there isn't already a node
|
||||||
nodes.append(node)
|
# equivalent to it (i.e. at same distance from the previous
|
||||||
|
# "real" node)
|
||||||
|
|
||||||
node.set_draw_info({"valid": False})
|
pre_hop = None
|
||||||
node.set_draw_info(
|
pre_hop_distance = 0
|
||||||
{"color": (1, 1, 1), "radius": NONE_RADIUS})
|
for i in range(1, ttl + 1):
|
||||||
|
pre_hop = find_hop_by_ttl(hops, ttl-i)
|
||||||
|
if pre_hop is not None:
|
||||||
|
pre_hop_distance = i
|
||||||
|
break
|
||||||
|
|
||||||
graph.set_connection(node, prev_node)
|
assert pre_hop is not None, "pre_hop should have become localhost if nothing else"
|
||||||
|
|
||||||
|
key = (pre_hop["ipaddr"], pre_hop_distance)
|
||||||
|
|
||||||
|
if key not in invalid_node_cache:
|
||||||
|
node = NetNode()
|
||||||
|
nodes.append(node)
|
||||||
|
|
||||||
|
node.set_draw_info({"valid":False})
|
||||||
|
node.set_draw_info({"color":(1,1,1), "radius":NONE_RADIUS})
|
||||||
|
|
||||||
|
graph.set_connection(node, prev_node)
|
||||||
|
|
||||||
|
invalid_node_cache[key] = node
|
||||||
|
|
||||||
|
node = invalid_node_cache[key]
|
||||||
|
|
||||||
prev_node = node
|
prev_node = node
|
||||||
endpoints[host] = node
|
endpoints[host] = node
|
||||||
|
|||||||
Reference in New Issue
Block a user