* Maintenant, on va pouvoir identifier formellement les machines v6-only et les autres, sans perdre
 la possibilité de passer de l'une à l'autre rapidement (avec les changements qui vont bien)
This commit is contained in:
Pierre-Elliott Bécue 2013-05-30 01:15:24 +02:00
parent 60ded9f830
commit cd75ed7bd4
5 changed files with 79 additions and 35 deletions

View file

@ -44,24 +44,57 @@ from unicodedata import normalize
DEVNULL = open(os.devnull, 'w')
def find_rid_plage(rid):
"""Trouve la plage du rid fourni"""
for (tp, plages) in config.rid_primaires.iteritems():
if isinstance(plages, list):
for begin, end in plages:
if begin <= rid <= end:
return tp, (begin, end)
else:
(begin, end) = plages
if begin <= rid <= end:
return tp, (begin, end)
else:
return 'Inconnu', (0, 0)
def find_ipv4_plage(ipv4):
"""Trouve la plage de l'ipv4 fournie"""
for (tp, plage) in config.NETs_primaires.iteritems():
for sousplage in map(netaddr.IPNetwork, plage):
if ipv4 in sousplage:
return tp, sousplage
def ip4_of_rid(rid):
"""Convertit un rid en son IP associée"""
# Au cas où
rid = int(rid)
for net, plage in config.rid.items():
if rid >= plage[0] and rid <= plage[1]:
break
else:
net, plage = find_rid_plage(rid)
if net == 'Inconnu':
raise ValueError("Rid dans aucune plage: %d" % rid)
if net == 'special':
try:
return netaddr.IPAddress(config.rid_machines_speciales[rid])
except KeyError:
return ValueError("Machine speciale inconnue: %d" % rid)
raise ValueError(u"Machine speciale inconnue: %d" % rid)
try:
return netaddr.IPAddress(netaddr.IPNetwork(config.NETs[net][0]).first + rid - plage[0])
except KeyError:
raise EnvironmentError("Les machines v6-only ne peuvent pas avoir d'ipv4 (%s)" % (net))
return netaddr.IPAddress(netaddr.IPNetwork(config.NETs[net][0]).first + rid - plage[0])
def rid_of_ip4(ipv4):
"""Convertit une ipv4 en rid, si possible"""
# Est-ce une machine spéciale ?
for (rid, ip) in config.rid_machines_speciales.iteritems():
if str(ipv4) == ip:
return rid
# Le cas non-échéant, on va devoir faire de la deep NETs inspection
realm, sousplage = find_ipv4_plage(ipv4)
return config.rid[realm][0][0] + int(ipv4 - sousplage.first)
def prefixev6_of_rid(rid):
"""
@ -73,10 +106,8 @@ def prefixev6_of_rid(rid):
# Au cas où
rid = int(rid)
for net, plage in config.rid.items():
if rid >= plage[0] and rid <= plage[1]:
break
else:
net, plage = find_rid_plage(rid)
if net == 'Inconnu':
raise ValueError("Rid dans aucune plage: %d" % rid)
# fil-v6 ou wifi-v6, we don't care
@ -89,10 +120,8 @@ def ip6_of_mac(mac, rid):
# Au cas où
rid = int(rid)
for net, plage in config.rid.items():
if rid >= plage[0] and rid <= plage[1]:
break
else:
net, plage = find_rid_plage(rid)
if net == 'Inconnu':
raise ValueError("Rid dans aucune plage: %d" % rid)
# En théorie, format_mac est inutile, car on ne devrait avoir