Exemplo n.º 1
0
 def _cmp(self, other):
     sa = ipv4.inet_aton(self.address)
     oa = ipv4.inet_aton(other.address)
     v = cmp(sa, oa)
     if v == 0:
         sp = struct.pack('!B', self.protocol)
         op = struct.pack('!B', other.protocol)
         v = cmp(sp, op)
         if v == 0:
             v = cmp(self.bitmap, other.bitmap)
     return v
Exemplo n.º 2
0
 def _cmp(self, other):
     sa = ipv4.inet_aton(self.address)
     oa = ipv4.inet_aton(other.address)
     v = cmp(sa, oa)
     if v == 0:
         sp = struct.pack('!B', self.protocol)
         op = struct.pack('!B', other.protocol)
         v = cmp(sp, op)
         if v == 0:
             v = cmp(self.bitmap, other.bitmap)
     return v
Exemplo n.º 3
0
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')
Exemplo n.º 4
0
def is_multicast(text):
    """Is the textual-form network address a multicast address?

    @param text: the textual address
    @raises ValueError: the address family cannot be determined from the input.
    @rtype: bool
    """
    try:
        first = ord(ipv4.inet_aton(text)[0])
        return (first >= 224 and first <= 239)
    except:
        try:
            first = ord(ipv6.inet_aton(text)[0])
            return (first == 255)
        except:
            raise ValueError
Exemplo n.º 5
0
def is_multicast(text):
    """Is the textual-form network address a multicast address?

    @param text: the textual address
    @raises ValueError: the address family cannot be determined from the input.
    @rtype: bool
    """
    try:
        first = ord(ipv4.inet_aton(text)[0])
        return (first >= 224 and first <= 239)
    except:
        try:
            first = ord(ipv6.inet_aton(text)[0])
            return (first == 255)
        except:
            raise ValueError
Exemplo n.º 6
0
def af_for_address(text):
    """Determine the address family of a textual-form network address.

    @param text: the textual address
    @type text: string
    @raises ValueError: the address family cannot be determined from the input.
    @rtype: int
    """
    try:
        junk = ipv4.inet_aton(text)
        return AF_INET
    except:
        try:
            junk = ipv6.inet_aton(text)
            return AF_INET6
        except:
            raise ValueError
Exemplo n.º 7
0
def af_for_address(text):
    """Determine the address family of a textual-form network address.

    @param text: the textual address
    @type text: string
    @raises ValueError: the address family cannot be determined from the input.
    @rtype: int
    """
    try:
        junk = ipv4.inet_aton(text)
        return AF_INET
    except:
        try:
            junk = ipv6.inet_aton(text)
            return AF_INET6
        except:
            raise ValueError
Exemplo n.º 8
0
def inet_pton(family, text):
    """Convert the textual form of a network address into its binary form.

    @param family: the address family
    @type family: int
    @param text: the textual address
    @type text: string
    @raises NotImplementedError: the address family specified is not
    implemented.
    @rtype: string
    """

    if family == AF_INET:
        return ipv4.inet_aton(text)
    elif family == AF_INET6:
        return ipv6.inet_aton(text)
    else:
        raise NotImplementedError
Exemplo n.º 9
0
def inet_pton(family, text):
    """Convert the textual form of a network address into its binary form.

    @param family: the address family
    @type family: int
    @param text: the textual address
    @type text: string
    @raises NotImplementedError: the address family specified is not
    implemented.
    @rtype: string
    """
    
    if family == AF_INET:
        return ipv4.inet_aton(text)
    elif family == AF_INET6:
        return ipv6.inet_aton(text)
    else:
        raise NotImplementedError
Exemplo n.º 10
0
def from_address(text):
    """Convert an IPv4 or IPv6 address in textual form into a Name object whose
    value is the reverse-map domain name of the address.
    @param text: an IPv4 or IPv6 address in textual form (e.g. '127.0.0.1',
    '::1')
    @type text: str
    @rtype: name.Name object
    """
    try:
        v6 = ipv6.inet_aton(text)
        if ipv6.is_mapped(v6):
            parts = ['%d' % ord(byte) for byte in v6[12:]]
            origin = ipv4_reverse_domain
        else:
            parts = list(v6.encode('hex_codec'))
            origin = ipv6_reverse_domain
    except:
        parts = ['%d' % ord(byte) for byte in ipv4.inet_aton(text)]
        origin = ipv4_reverse_domain
    parts.reverse()
    return name.from_text('.'.join(parts), origin=origin)
Exemplo n.º 11
0
def inet_aton(text):
    """Convert a text format IPv6 address into network format.

    @param text: the textual address
    @type text: string
    @rtype: string
    @raises exception.SyntaxError: the text was not properly formatted
    """

    #
    # Our aim here is not something fast; we just want something that works.
    #

    if text == '::':
        text = '0::'
    #
    # Get rid of the icky dot-quad syntax if we have it.
    #
    m = _v4_ending.match(text)
    if not m is None:
        b = ipv4.inet_aton(m.group(2))
        text = "%s:%02x%02x:%02x%02x" % (m.group(1), ord(b[0]), ord(
            b[1]), ord(b[2]), ord(b[3]))
    #
    # Try to turn '::<whatever>' into ':<whatever>'; if no match try to
    # turn '<whatever>::' into '<whatever>:'
    #
    m = _colon_colon_start.match(text)
    if not m is None:
        text = text[1:]
    else:
        m = _colon_colon_end.match(text)
        if not m is None:
            text = text[:-1]
    #
    # Now canonicalize into 8 chunks of 4 hex digits each
    #
    chunks = text.split(':')
    l = len(chunks)
    if l > 8:
        raise exception.SyntaxError
    seen_empty = False
    canonical = []
    for c in chunks:
        if c == '':
            if seen_empty:
                raise exception.SyntaxError
            seen_empty = True
            for i in xrange(0, 8 - l + 1):
                canonical.append('0000')
        else:
            lc = len(c)
            if lc > 4:
                raise exception.SyntaxError
            if lc != 4:
                c = ('0' * (4 - lc)) + c
            canonical.append(c)
    if l < 8 and not seen_empty:
        raise exception.SyntaxError
    text = ''.join(canonical)

    #
    # Finally we can go to binary.
    #
    try:
        return text.decode('hex_codec')
    except TypeError:
        raise exception.SyntaxError
Exemplo n.º 12
0
Arquivo: A.py Projeto: v1cker/wiper
 def __init__(self, rdclass, rdtype, address):
     super(A, self).__init__(rdclass, rdtype)
     # check that it's OK
     junk = ipv4.inet_aton(address)
     self.address = address
Exemplo n.º 13
0
def _getaddrinfo(host=None, service=None, family=socket.AF_UNSPEC, socktype=0,
                 proto=0, flags=0):
    if flags & (socket.AI_ADDRCONFIG|socket.AI_V4MAPPED) != 0:
        raise NotImplementedError
    if host is None and service is None:
        raise socket.gaierror(socket.EAI_NONAME)
    v6addrs = []
    v4addrs = []
    canonical_name = None
    try:
        # Is host None or a V6 address literal?
        if host is None:
            canonical_name = 'localhost'
            if flags & socket.AI_PASSIVE != 0:
                v6addrs.append('::')
                v4addrs.append('0.0.0.0')
            else:
                v6addrs.append('::1')
                v4addrs.append('127.0.0.1')
        else:
            parts = host.split('%')
            if len(parts) == 2:
                ahost = parts[0]
            else:
                ahost = host
            addr = ipv6.inet_aton(ahost)
            v6addrs.append(host)
            canonical_name = host
    except:
        try:
            # Is it a V4 address literal?
            addr = ipv4.inet_aton(host)
            v4addrs.append(host)
            canonical_name = host
        except:
            if flags & socket.AI_NUMERICHOST == 0:
                try:
                    qname = None
                    if family == socket.AF_INET6 or family == socket.AF_UNSPEC:
                        v6 = _resolver.query(host, rdatatype.AAAA,
                                             raise_on_no_answer=False)
                        # Note that setting host ensures we query the same name
                        # for A as we did for AAAA.
                        host = v6.qname
                        canonical_name = v6.canonical_name.to_text(True)
                        if v6.rrset is not None:
                            for rdata in v6.rrset:
                                v6addrs.append(rdata.address)
                    if family == socket.AF_INET or family == socket.AF_UNSPEC:
                        v4 = _resolver.query(host, rdatatype.A,
                                             raise_on_no_answer=False)
                        host = v4.qname
                        canonical_name = v4.canonical_name.to_text(True)
                        if v4.rrset is not None:
                            for rdata in v4.rrset:
                                v4addrs.append(rdata.address)
                except resolver.NXDOMAIN:
                    raise socket.gaierror(socket.EAI_NONAME)
                except:
                    raise socket.gaierror(socket.EAI_SYSTEM)
    port = None
    try:
        # Is it a port literal?
        if service is None:
            port = 0
        else:
            port = int(service)
    except:
        if flags & socket.AI_NUMERICSERV == 0:
            try:
                port = socket.getservbyname(service)
            except:
                pass
    if port is None:
        raise socket.gaierror(socket.EAI_NONAME)
    tuples = []
    if socktype == 0:
        socktypes = [socket.SOCK_DGRAM, socket.SOCK_STREAM]
    else:
        socktypes = [socktype]
    if flags & socket.AI_CANONNAME != 0:
        cname = canonical_name
    else:
        cname = ''
    if family == socket.AF_INET6 or family == socket.AF_UNSPEC:
        for addr in v6addrs:
            for socktype in socktypes:
                for proto in _protocols_for_socktype[socktype]:
                    tuples.append((socket.AF_INET6, socktype, proto,
                                   cname, (addr, port, 0, 0)))
    if family == socket.AF_INET or family == socket.AF_UNSPEC:
        for addr in v4addrs:
            for socktype in socktypes:
                for proto in _protocols_for_socktype[socktype]:
                    tuples.append((socket.AF_INET, socktype, proto,
                                   cname, (addr, port)))
    if len(tuples) == 0:
        raise socket.gaierror(socket.EAI_NONAME)
    return tuples
Exemplo n.º 14
0
 def to_wire(self, file, compress = None, origin = None):
     file.write(ipv4.inet_aton(self.address))
     protocol = struct.pack('!B', self.protocol)
     file.write(protocol)
     file.write(self.bitmap)
Exemplo n.º 15
0
 def to_wire(self, file, compress=None, origin=None):
     file.write(ipv4.inet_aton(self.address))
     protocol = struct.pack('!B', self.protocol)
     file.write(protocol)
     file.write(self.bitmap)
Exemplo n.º 16
0
def inet_aton(text):
    """Convert a text format IPv6 address into network format.

    @param text: the textual address
    @type text: string
    @rtype: string
    @raises exception.SyntaxError: the text was not properly formatted
    """

    #
    # Our aim here is not something fast; we just want something that works.
    #

    if text == '::':
        text = '0::'
    #
    # Get rid of the icky dot-quad syntax if we have it.
    #
    m = _v4_ending.match(text)
    if not m is None:
        b = ipv4.inet_aton(m.group(2))
        text = "%s:%02x%02x:%02x%02x" % (m.group(1), ord(b[0]), ord(b[1]),
                                         ord(b[2]), ord(b[3]))
    #
    # Try to turn '::<whatever>' into ':<whatever>'; if no match try to
    # turn '<whatever>::' into '<whatever>:'
    #
    m = _colon_colon_start.match(text)
    if not m is None:
        text = text[1:]
    else:
        m = _colon_colon_end.match(text)
        if not m is None:
            text = text[:-1]
    #
    # Now canonicalize into 8 chunks of 4 hex digits each
    #
    chunks = text.split(':')
    l = len(chunks)
    if l > 8:
        raise exception.SyntaxError
    seen_empty = False
    canonical = []
    for c in chunks:
        if c == '':
            if seen_empty:
                raise exception.SyntaxError
            seen_empty = True
            for i in xrange(0, 8 - l + 1):
                canonical.append('0000')
        else:
            lc = len(c)
            if lc > 4:
                raise exception.SyntaxError
            if lc != 4:
                c = ('0' * (4 - lc)) + c
            canonical.append(c)
    if l < 8 and not seen_empty:
        raise exception.SyntaxError
    text = ''.join(canonical)

    #
    # Finally we can go to binary.
    #
    try:
        return text.decode('hex_codec')
    except TypeError:
        raise exception.SyntaxError
Exemplo n.º 17
0
Arquivo: A.py Projeto: alpha1e0/wiper
 def to_wire(self, file, compress = None, origin = None):
     file.write(ipv4.inet_aton(self.address))
Exemplo n.º 18
0
Arquivo: A.py Projeto: alpha1e0/wiper
 def __init__(self, rdclass, rdtype, address):
     super(A, self).__init__(rdclass, rdtype)
     # check that it's OK
     junk = ipv4.inet_aton(address)
     self.address = address
Exemplo n.º 19
0
Arquivo: A.py Projeto: v1cker/wiper
 def to_wire(self, file, compress=None, origin=None):
     file.write(ipv4.inet_aton(self.address))
Exemplo n.º 20
0
Arquivo: A.py Projeto: alpha1e0/wiper
 def _cmp(self, other):
     sa = ipv4.inet_aton(self.address)
     oa = ipv4.inet_aton(other.address)
     return cmp(sa, oa)
Exemplo n.º 21
0
Arquivo: A.py Projeto: v1cker/wiper
 def _cmp(self, other):
     sa = ipv4.inet_aton(self.address)
     oa = ipv4.inet_aton(other.address)
     return cmp(sa, oa)