def load_info(): """ Parse and load internal IANA data lookups with the latest information from data files. """ ipv4 = IPv4Parser( _importlib_resources.open_binary(__package__, 'ipv4-address-space.xml')) ipv4.attach(DictUpdater(IANA_INFO['IPv4'], 'IPv4', 'prefix')) ipv4.parse() ipv6 = IPv6Parser( _importlib_resources.open_binary(__package__, 'ipv6-address-space.xml')) ipv6.attach(DictUpdater(IANA_INFO['IPv6'], 'IPv6', 'prefix')) ipv6.parse() ipv6ua = IPv6UnicastParser( _importlib_resources.open_binary( __package__, 'ipv6-unicast-address-assignments.xml'), ) ipv6ua.attach( DictUpdater(IANA_INFO['IPv6_unicast'], 'IPv6_unicast', 'prefix')) ipv6ua.parse() mcast = MulticastParser( _importlib_resources.open_binary(__package__, 'multicast-addresses.xml')) mcast.attach(DictUpdater(IANA_INFO['multicast'], 'multicast', 'address')) mcast.parse()
def test_iab_parser_py3(): from io import StringIO outfile = StringIO() with contextlib.closing( _importlib_resources.open_binary(__package__, 'sample_iab.txt')) as infile: iab_parser = IABIndexParser(infile) iab_parser.attach(FileIndexer(outfile)) iab_parser.parse() assert outfile.getvalue() == '84683452,1,181\n'
def test_oui_parser_py2(): from cStringIO import StringIO outfile = StringIO() with contextlib.closing( _importlib_resources.open_binary(__package__, 'sample_oui.txt')) as infile: iab_parser = OUIIndexParser(infile) iab_parser.attach(FileIndexer(outfile)) iab_parser.parse() assert outfile.getvalue() == '51966,1,138\n'
def __init__(self, iab, strict=False): """ Constructor :param iab: an IAB string ``00-50-C2-XX-X0-00`` or an unsigned \ integer. This address looks like an EUI-48 but it should not \ have any non-zero bits in the last 3 bytes. :param strict: If True, raises a ValueError if the last 12 bits \ of IAB MAC/EUI-48 address are non-zero, ignores them otherwise. \ (Default: False) """ super(IAB, self).__init__() # Lazy loading of IEEE data structures. from netaddr.eui import ieee self.record = { 'idx': 0, 'iab': '', 'org': '', 'address': [], 'offset': 0, 'size': 0, } if isinstance(iab, str): #TODO: Improve string parsing here. #TODO: '00-50-C2' is actually invalid. #TODO: Should be '00-50-C2-00-00-00' (i.e. a full MAC/EUI-48) int_val = int(iab.replace('-', ''), 16) iab_int, user_int = self.split_iab_mac(int_val, strict=strict) self._value = iab_int elif _is_int(iab): iab_int, user_int = self.split_iab_mac(iab, strict=strict) self._value = iab_int else: raise TypeError('unexpected IAB format: %r!' % (iab, )) # Discover offsets. if self._value in ieee.IAB_INDEX: fh = _importlib_resources.open_binary(__package__, 'iab.txt') (offset, size) = ieee.IAB_INDEX[self._value][0] self.record['offset'] = offset self.record['size'] = size fh.seek(offset) data = fh.read(size).decode('UTF-8') self._parse_data(data, offset, size) fh.close() else: raise NotRegisteredError('IAB %r not unregistered!' % (iab, ))
def __init__(self, oui): """ Constructor :param oui: an OUI string ``XX-XX-XX`` or an unsigned integer. \ Also accepts and parses full MAC/EUI-48 address strings (but not \ MAC/EUI-48 integers)! """ super(OUI, self).__init__() # Lazy loading of IEEE data structures. from netaddr.eui import ieee self.records = [] if isinstance(oui, str): #TODO: Improve string parsing here. #TODO: Accept full MAC/EUI-48 addressses as well as XX-XX-XX #TODO: and just take /16 (see IAB for details) self._value = int(oui.replace('-', ''), 16) elif _is_int(oui): if 0 <= oui <= 0xffffff: self._value = oui else: raise ValueError('OUI int outside expected range: %r' % (oui, )) else: raise TypeError('unexpected OUI format: %r' % (oui, )) # Discover offsets. if self._value in ieee.OUI_INDEX: fh = _importlib_resources.open_binary(__package__, 'oui.txt') for (offset, size) in ieee.OUI_INDEX[self._value]: fh.seek(offset) data = fh.read(size).decode('UTF-8') self._parse_data(data, offset, size) fh.close() else: raise NotRegisteredError('OUI %r not registered!' % (oui, ))
def load_indices(): """Load OUI and IAB lookup indices into memory""" load_index(OUI_INDEX, _importlib_resources.open_binary(__package__, 'oui.idx')) load_index(IAB_INDEX, _importlib_resources.open_binary(__package__, 'iab.idx'))