[gestion/midtools.py] Corrections de structure, ajout de la conversion ipv4 -> mid

Ignore-this: c0729ff11f1e08228887913380c85506

darcs-hash:20100119172952-108b1-425e523824ac2166febb35af33adc75b7f1a5267.gz
This commit is contained in:
Olivier Iffrig 2010-01-19 18:29:52 +01:00
parent 5395f61d1b
commit af1bcdedbb

View file

@ -19,7 +19,7 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
from config import mid, prefix, NETs import config
import iptools import iptools
@ -28,10 +28,20 @@ class Mid(object):
Permet de décortiquer un mid et d'obtenir les IP correspondantes Permet de décortiquer un mid et d'obtenir les IP correspondantes
""" """
def __init__(self, mid): def __init__(self, mid=None):
if mid is not None:
self.parse(mid)
else:
self.mid = None
self.reste = False
self.type = None
self.ipv4_dispo = False
self.priv = False
def parse(self, mid):
self.mid = mid self.mid = mid
self.type = None self.type = None
for tp in mid: for tp in config.mid:
if mid <= tp[1] and mid >= tp[0]: if mid <= tp[1] and mid >= tp[0]:
self.type = tp self.type = tp
if self.type is None: if self.type is None:
@ -47,6 +57,25 @@ class Mid(object):
self.reste = mid & 0x7ff self.reste = mid & 0x7ff
# if self.type == 'ens': # Inutile pour l'instant # if self.type == 'ens': # Inutile pour l'instant
# self.reste &= 0xff
def from_ipv4(self, ip):
"""
Met à jour l' instance pour correspondre à l'IPv4 donnée
"""
self.ipv4_dispo = True
self.priv = ip.startswith('10.')
for tp in ['fil', 'wifi', 'adm', 'gratuit', 'ens']:
if iptools.AddrInNet(ip, config.NETs[tp]):
self.type = tp
break
if self.type is None:
raise ValueError("Impossible de convertir l'adresse IP")
self.mid = iptools.QuadToDec(ip)
self.mid -= iptools.param(config.NETs[self.type], True)['network']
self.reste = self.mid & 0x7ff
# if self.type == 'ens': # Inutile pour l'instant
# self.reste &= 0xff # self.reste &= 0xff
def to_ipv4(self): def to_ipv4(self):
@ -56,7 +85,7 @@ class Mid(object):
if not self.ipv4_dispo: if not self.ipv4_dispo:
raise ValueError("Pas d'adresse ipv4 disponible pour la machine") raise ValueError("Pas d'adresse ipv4 disponible pour la machine")
ip = iptools.param(NETs[self.type], True)['network'] ip = iptools.param(config.NETs[self.type], True)['network']
ip += self.reste ip += self.reste
return iptools.DecToQuad(ip) return iptools.DecToQuad(ip)
@ -70,7 +99,7 @@ class Mid(object):
tp = {'fil': 'fil', 'fil-v6': 'fil', tp = {'fil': 'fil', 'fil-v6': 'fil',
'wifi': 'wifi', 'wifi-v6': 'wifi', 'wifi': 'wifi', 'wifi-v6': 'wifi',
'adm': 'adm'}[self.type] 'adm': 'adm'}[self.type]
ip = prefix[tp][0].split(":/")[0] ip = config.prefix[tp][0].split(":/")[0]
ip += hex(self.mid)[2:] ip += hex(self.mid)[2:]
ip += "::/64" ip += "::/64"