scripts/gestion/midtools.py
Olivier Iffrig 5395f61d1b [gestion/midtools.py]
Ignore-this: b56182a078791b705a84376e3bbb952c
Création du fichier, ajout des méthodes de conversion mid -> ipv4 et mid -> vlan ipv6

darcs-hash:20100118234032-108b1-3f2417a852e9885d62f0e8b8628146695337870e.gz
2010-01-19 00:40:32 +01:00

76 lines
2.3 KiB
Python
Executable file

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# MIDTOOLS.PY -- Gestion de la conversion mid <-> IP
#
# Copyright (C) 2010 Olivier Iffrig
# Authors: Olivier Iffrig <iffrig@crans.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from config import mid, prefix, NETs
import iptools
class Mid(object):
"""
Permet de décortiquer un mid et d'obtenir les IP correspondantes
"""
def __init__(self, mid):
self.mid = mid
self.type = None
for tp in mid:
if mid <= tp[1] and mid >= tp[0]:
self.type = tp
if self.type is None:
raise ValueError("mid inconnu : %d" % mid)
self.ipv4_dispo = False
if (mid & (1 << 15)) == 0 and self.type != 'special':
self.ipv4_dispo = True
self.priv = True
if (mid & (1 << 14)) == 0:
self.priv = False
self.reste = mid & 0x7ff
# if self.type == 'ens': # Inutile pour l'instant
# self.reste &= 0xff
def to_ipv4(self):
"""
Génère l'IPv4 associée à la machine
"""
if not self.ipv4_dispo:
raise ValueError("Pas d'adresse ipv4 disponible pour la machine")
ip = iptools.param(NETs[self.type], True)['network']
ip += self.reste
return iptools.DecToQuad(ip)
def to_ipv6_vlan(self):
"""
Génère le VLAN IPv6 associé à la machine
"""
if priv:
raise ValueError("Pas d'adresse ipv6 disponible pour la machine")
tp = {'fil': 'fil', 'fil-v6': 'fil',
'wifi': 'wifi', 'wifi-v6': 'wifi',
'adm': 'adm'}[self.type]
ip = prefix[tp][0].split(":/")[0]
ip += hex(self.mid)[2:]
ip += "::/64"