refactor mac similarity check into its own function

This commit is contained in:
Nils Schneider 2012-07-07 23:11:37 +02:00
parent f7f8225d1f
commit a8a9ed9c8d

View file

@ -64,28 +64,16 @@ class NodeDB:
# MACs # MACs
if 'gateway' in x and x['label'] == "TT": if 'gateway' in x and x['label'] == "TT":
try: try:
router = list(int(i, 16) for i in x['router'].split(":")) mac_a = list(int(i, 16) for i in x['router'].split(":"))
gateway = list(int(i, 16) for i in x['gateway'].split(":")) mac_b = list(int(i, 16) for i in x['gateway'].split(":"))
except ValueError: except ValueError:
continue continue
# first byte must only differ in bit 2 if is_similar(mac_a, mac_b):
if router[0] == gateway[0] | 2: node.add_mac(x['gateway'])
# count different bytes
a = [x for x in zip(router[1:], gateway[1:]) if x[0] != x[1]]
# no more than two additional bytes must differ # skip processing as regular link
if len(a) <= 2: continue
delta = 0
if len(a) > 0:
delta = sum(abs(i[0] -i[1]) for i in a)
if delta < 8:
# This TT link looks like a mac of the router!
node.add_mac(x['gateway'])
# skip processing as regular link
continue
try: try:
if 'neighbor' in x: if 'neighbor' in x:
@ -292,3 +280,23 @@ class NodeDB:
if data[2]: if data[2]:
node.name = data[2] node.name = data[2]
# compares two MACs and decides whether they are
# similar and could be from the same node
def is_similar(a, b):
# first byte must only differ in bit 2
if a[0] | 2 == b[0] | 2:
# count different bytes
a = [x for x in zip(a[1:], b[1:]) if x[0] != x[1]]
else:
return False
# no more than two additional bytes must differ
if len(a) <= 2:
delta = 0
if len(a) > 0:
delta = sum(abs(i[0] -i[1]) for i in a)
# This TT link looks like a mac of the router!
return delta < 8