def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin=None): address = ipv4.inet_ntoa(wire[current:current + 4]) protocol, = struct.unpack('!B', wire[current + 4:current + 5]) current += 5 rdlen -= 5 bitmap = wire[current:current + rdlen].unwrap() return cls(rdclass, rdtype, address, protocol, bitmap)
def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin = None): address = ipv4.inet_ntoa(wire[current : current + 4]) protocol, = struct.unpack('!B', wire[current + 4 : current + 5]) current += 5 rdlen -= 5 bitmap = wire[current : current + rdlen].unwrap() return cls(rdclass, rdtype, address, protocol, bitmap)
def to_address(name): """Convert a reverse map domain name into textual address form. @param name: an IPv4 or IPv6 address in reverse-map form. @type name: name.Name object @rtype: str """ if name.is_subdomain(ipv4_reverse_domain): name = name.relativize(ipv4_reverse_domain) labels = list(name.labels) labels.reverse() text = '.'.join(labels) # run through inet_aton() to check syntax and make pretty. return ipv4.inet_ntoa(ipv4.inet_aton(text)) elif name.is_subdomain(ipv6_reverse_domain): name = name.relativize(ipv6_reverse_domain) labels = list(name.labels) labels.reverse() parts = [] i = 0 l = len(labels) while i < l: parts.append(''.join(labels[i:i+4])) i += 4 text = ':'.join(parts) # run through inet_aton() to check syntax and make pretty. return ipv6.inet_ntoa(ipv6.inet_aton(text)) else: raise exception.SyntaxError('unknown reverse-map address family')
def inet_ntop(family, address): """Convert the binary form of a network address into its textual form. @param family: the address family @type family: int @param address: the binary address @type address: string @raises NotImplementedError: the address family specified is not implemented. @rtype: string """ if family == AF_INET: return ipv4.inet_ntoa(address) elif family == AF_INET6: return ipv6.inet_ntoa(address) else: raise NotImplementedError
def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin = None): address = ipv4.inet_ntoa(wire[current : current + rdlen]) return cls(rdclass, rdtype, address)
def inet_ntoa(address): """Convert a network format IPv6 address into text. @param address: the binary address @type address: string @rtype: string @raises ValueError: the address isn't 16 bytes long """ if len(address) != 16: raise ValueError("IPv6 addresses are 16 bytes long") hex = address.encode('hex_codec') chunks = [] i = 0 l = len(hex) while i < l: chunk = hex[i : i + 4] # strip leading zeros. we do this with an re instead of # with lstrip() because lstrip() didn't support chars until # python 2.2.2 m = _leading_zero.match(chunk) if not m is None: chunk = m.group(1) chunks.append(chunk) i += 4 # # Compress the longest subsequence of 0-value chunks to :: # best_start = 0 best_len = 0 start = -1 last_was_zero = False for i in xrange(8): if chunks[i] != '0': if last_was_zero: end = i current_len = end - start if current_len > best_len: best_start = start best_len = current_len last_was_zero = False elif not last_was_zero: start = i last_was_zero = True if last_was_zero: end = 8 current_len = end - start if current_len > best_len: best_start = start best_len = current_len if best_len > 1: if best_start == 0 and \ (best_len == 6 or best_len == 5 and chunks[5] == 'ffff'): # We have an embedded IPv4 address if best_len == 6: prefix = '::' else: prefix = '::ffff:' hex = prefix + ipv4.inet_ntoa(address[12:]) else: hex = ':'.join(chunks[:best_start]) + '::' + \ ':'.join(chunks[best_start + best_len:]) else: hex = ':'.join(chunks) return hex
def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin=None): address = ipv4.inet_ntoa(wire[current:current + rdlen]) return cls(rdclass, rdtype, address)
def inet_ntoa(address): """Convert a network format IPv6 address into text. @param address: the binary address @type address: string @rtype: string @raises ValueError: the address isn't 16 bytes long """ if len(address) != 16: raise ValueError("IPv6 addresses are 16 bytes long") hex = address.encode('hex_codec') chunks = [] i = 0 l = len(hex) while i < l: chunk = hex[i:i + 4] # strip leading zeros. we do this with an re instead of # with lstrip() because lstrip() didn't support chars until # python 2.2.2 m = _leading_zero.match(chunk) if not m is None: chunk = m.group(1) chunks.append(chunk) i += 4 # # Compress the longest subsequence of 0-value chunks to :: # best_start = 0 best_len = 0 start = -1 last_was_zero = False for i in xrange(8): if chunks[i] != '0': if last_was_zero: end = i current_len = end - start if current_len > best_len: best_start = start best_len = current_len last_was_zero = False elif not last_was_zero: start = i last_was_zero = True if last_was_zero: end = 8 current_len = end - start if current_len > best_len: best_start = start best_len = current_len if best_len > 1: if best_start == 0 and \ (best_len == 6 or best_len == 5 and chunks[5] == 'ffff'): # We have an embedded IPv4 address if best_len == 6: prefix = '::' else: prefix = '::ffff:' hex = prefix + ipv4.inet_ntoa(address[12:]) else: hex = ':'.join(chunks[:best_start]) + '::' + \ ':'.join(chunks[best_start + best_len:]) else: hex = ':'.join(chunks) return hex