uncloud-mravi/uncloud/hack/hackcloud/mac-gen.py

118 lines
3.1 KiB
Python
Raw Normal View History

2020-01-12 12:41:54 +00:00
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# 2012 Nico Schottelius (nico-cinv at schottelius.org)
#
# This file is part of cinv.
#
# cinv 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.
#
# cinv 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 cinv. If not, see <http://www.gnu.org/licenses/>.
#
#
import argparse
import logging
import os.path
import os
import re
log = logging.getLogger(__name__)
2020-01-12 13:03:04 +00:00
class Error(Exception):
2020-01-12 12:41:54 +00:00
pass
class Mac(object):
def __init__(self):
2020-01-12 13:03:04 +00:00
self.base_dir = "."
2020-01-12 13:35:59 +00:00
self.prefix = 0x002000000000
#self.prefix = "{:012x}".format(self._prefix)
2020-01-12 12:41:54 +00:00
2020-01-12 13:03:04 +00:00
self.free = self.read_file("mac-free")
self.last = self.read_file("mac-last")
def read_file(self, filename):
fname = os.path.join(self.base_dir, filename)
ret = []
2020-01-12 12:41:54 +00:00
try:
2020-01-12 13:03:04 +00:00
with open(fname, "r") as fd:
ret = fd.readlines()
except Exception as e:
pass
return ret
2020-01-12 12:41:54 +00:00
2020-01-12 13:35:59 +00:00
def append_to_file(self, text, filename):
fname = os.path.join(self.base_dir, filename)
with open(fname, "a+") as fd:
fd.write("{}\n".format(text))
2020-01-12 12:41:54 +00:00
@staticmethod
def validate_mac(mac):
if not re.match(r'([0-9A-F]{2}[-:]){5}[0-9A-F]{2}$', mac, re.I):
raise Error("Not a valid mac address: %s" % mac)
def free_append(self, mac):
if mac in self.free:
raise Error("Mac already in free database: %s" % mac)
2020-01-12 13:35:59 +00:00
self.append_to_file(mac, "mac-free")
self.free = self.read_file("mac-free")
2020-01-12 12:41:54 +00:00
2020-01-12 13:35:59 +00:00
@staticmethod
def int_to_mac(number):
b = number.to_bytes(6, byteorder="big")
return ':'.join(format(s, '02x') for s in b)
2020-01-12 12:41:54 +00:00
2020-01-12 13:35:59 +00:00
def getnext(self):
# if self.free:
# return self.free.pop()
2020-01-12 12:41:54 +00:00
2020-01-12 13:35:59 +00:00
# if not self.prefix:
# raise Error("Cannot generate address without prefix - use prefix-set")
2020-01-12 12:41:54 +00:00
if self.last:
2020-01-12 13:35:59 +00:00
last_number = int(self.last[0], 16)
2020-01-12 12:41:54 +00:00
if last_number == int('0xffffff', 16):
raise Error("Exhausted all possible mac addresses - try to free some")
next_number = last_number + 1
else:
next_number = 0
2020-01-12 13:35:59 +00:00
next_number_string = "{:012x}".format(next_number)
2020-01-12 12:41:54 +00:00
2020-01-12 13:35:59 +00:00
next_mac_number = self.prefix + next_number
next_mac = self.int_to_mac(next_mac_number)
2020-01-12 12:41:54 +00:00
2020-01-12 13:35:59 +00:00
with open(os.path.join(self.base_dir, "mac-last"), "w+") as fd:
fd.write("{}\n".format(next_number_string))
2020-01-12 12:41:54 +00:00
return next_mac
@classmethod
2020-01-12 13:03:04 +00:00
def commandline(cls):
pass
2020-01-12 12:41:54 +00:00
2020-01-12 13:03:04 +00:00
if __name__ == '__main__':
m = Mac()
m.commandline()
2020-01-12 13:35:59 +00:00
# print(m.free)
#print(m.last)
print(m.getnext())