Python socket.AI_CANONNAME Examples

The following are 20 code examples of socket.AI_CANONNAME(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module socket , or try the search function .
Example #1
Source File: resolver.py    From script.elementum.burst with Do What The F*ck You Want To Public License 6 votes vote down vote up
def _gethostbyaddr(ip):
    try:
        dns.ipv6.inet_aton(ip)
        sockaddr = (ip, 80, 0, 0)
        family = socket.AF_INET6
    except Exception:
        sockaddr = (ip, 80)
        family = socket.AF_INET
    (name, port) = _getnameinfo(sockaddr, socket.NI_NAMEREQD)
    aliases = []
    addresses = []
    tuples = _getaddrinfo(name, 0, family, socket.SOCK_STREAM, socket.SOL_TCP,
                          socket.AI_CANONNAME)
    canonical = tuples[0][3]
    for item in tuples:
        addresses.append(item[4][0])
    # XXX we just ignore aliases
    return (canonical, aliases, addresses) 
Example #2
Source File: __init__.py    From katprep with GNU General Public License v3.0 6 votes vote down vote up
def validate_hostname(hostname):
    """
    Validates using a FQDN rather than a short name as some
    APIs are very picky and SSL verification might fail.

    :param hostname: the hostname to validate
    :type hostname: str
    """
    try:
        if hostname == "localhost":
            #get real hostname
            hostname = socket.gethostname()
        if hostname.count('.') != 2:
            #get convert to FQDN if possible
            hostname = socket.getaddrinfo(
                socket.getfqdn(hostname), 0, 0, 0, 0,
                socket.AI_CANONNAME
            )[0][3]
    except socket.gaierror:
        raise InvalidHostnameFormatException(
            "Unable to find FQDN for host '{}'".format(hostname)
        )
    return hostname 
Example #3
Source File: network.py    From landscape-client with GNU General Public License v2.0 6 votes vote down vote up
def get_fqdn():
    """
    Return the current fqdn of the machine, trying hard to return a meaningful
    name.

    In particular, it means working against a NetworkManager bug which seems to
    make C{getfqdn} return localhost6.localdomain6 for machine without a domain
    since Maverick.
    """
    fqdn = socket.getfqdn()
    if "localhost" in fqdn:
        # Try the heavy artillery
        fqdn = socket.getaddrinfo(socket.gethostname(), None, socket.AF_INET,
                                  socket.SOCK_DGRAM, socket.IPPROTO_IP,
                                  socket.AI_CANONNAME)[0][3]
        if "localhost" in fqdn:
            # Another fallback
            fqdn = socket.gethostname()
    return fqdn 
Example #4
Source File: resolver.py    From luscan-devel with GNU General Public License v2.0 6 votes vote down vote up
def _gethostbyaddr(ip):
    try:
        addr = dns.ipv6.inet_aton(ip)
        sockaddr = (ip, 80, 0, 0)
        family = socket.AF_INET6
    except:
        sockaddr = (ip, 80)
        family = socket.AF_INET
    (name, port) = _getnameinfo(sockaddr, socket.NI_NAMEREQD)
    aliases = []
    addresses = []
    tuples = _getaddrinfo(name, 0, family, socket.SOCK_STREAM, socket.SOL_TCP,
                          socket.AI_CANONNAME)
    canonical = tuples[0][3]
    for item in tuples:
        addresses.append(item[4][0])
    # XXX we just ignore aliases
    return (canonical, aliases, addresses) 
Example #5
Source File: resolver.py    From Tautulli with GNU General Public License v3.0 6 votes vote down vote up
def _gethostbyaddr(ip):
    try:
        dns.ipv6.inet_aton(ip)
        sockaddr = (ip, 80, 0, 0)
        family = socket.AF_INET6
    except:
        sockaddr = (ip, 80)
        family = socket.AF_INET
    (name, port) = _getnameinfo(sockaddr, socket.NI_NAMEREQD)
    aliases = []
    addresses = []
    tuples = _getaddrinfo(name, 0, family, socket.SOCK_STREAM, socket.SOL_TCP,
                          socket.AI_CANONNAME)
    canonical = tuples[0][3]
    for item in tuples:
        addresses.append(item[4][0])
    # XXX we just ignore aliases
    return (canonical, aliases, addresses) 
Example #6
Source File: _remote_socket_stub.py    From python-compat-runtime with Apache License 2.0 6 votes vote down vote up
def _Dynamic_Resolve(self, request, response):
    addrs = {}
    for family, _, _, canonname, sa in socket.getaddrinfo(
        request.name(), 0, 0, socket.SOCK_STREAM, 0, socket.AI_CANONNAME):
      addrs.setdefault(self._AF_MAP.get(family), set()).add(
          _remote_socket_addr.inet_pton(self._TRANSLATED_AF_MAP[family], sa[0]))
      response.set_canonical_name(canonname)





      if canonname and canonname.lower() != request.name().lower():
        if not response.aliases_size():
          response.add_aliases(request.name())
    for af in request.address_families_list():
      for packed_addr in addrs.get(af, set()):
        response.add_packed_address(packed_addr) 
Example #7
Source File: resolver.py    From Cloudmare with GNU General Public License v3.0 6 votes vote down vote up
def _gethostbyaddr(ip):
    try:
        thirdparty.dns.ipv6.inet_aton(ip)
        sockaddr = (ip, 80, 0, 0)
        family = socket.AF_INET6
    except:
        sockaddr = (ip, 80)
        family = socket.AF_INET
    (name, port) = _getnameinfo(sockaddr, socket.NI_NAMEREQD)
    aliases = []
    addresses = []
    tuples = _getaddrinfo(name, 0, family, socket.SOCK_STREAM, socket.SOL_TCP,
                          socket.AI_CANONNAME)
    canonical = tuples[0][3]
    for item in tuples:
        addresses.append(item[4][0])
    # XXX we just ignore aliases
    return (canonical, aliases, addresses) 
Example #8
Source File: resolver.py    From arissploit with GNU General Public License v3.0 6 votes vote down vote up
def _gethostbyaddr(ip):
    try:
        dns.ipv6.inet_aton(ip)
        sockaddr = (ip, 80, 0, 0)
        family = socket.AF_INET6
    except:
        sockaddr = (ip, 80)
        family = socket.AF_INET
    (name, port) = _getnameinfo(sockaddr, socket.NI_NAMEREQD)
    aliases = []
    addresses = []
    tuples = _getaddrinfo(name, 0, family, socket.SOCK_STREAM, socket.SOL_TCP,
                          socket.AI_CANONNAME)
    canonical = tuples[0][3]
    for item in tuples:
        addresses.append(item[4][0])
    # XXX we just ignore aliases
    return (canonical, aliases, addresses) 
Example #9
Source File: resolver.py    From elasticintel with GNU General Public License v3.0 6 votes vote down vote up
def _gethostbyaddr(ip):
    try:
        dns.ipv6.inet_aton(ip)
        sockaddr = (ip, 80, 0, 0)
        family = socket.AF_INET6
    except Exception:
        sockaddr = (ip, 80)
        family = socket.AF_INET
    (name, port) = _getnameinfo(sockaddr, socket.NI_NAMEREQD)
    aliases = []
    addresses = []
    tuples = _getaddrinfo(name, 0, family, socket.SOCK_STREAM, socket.SOL_TCP,
                          socket.AI_CANONNAME)
    canonical = tuples[0][3]
    for item in tuples:
        addresses.append(item[4][0])
    # XXX we just ignore aliases
    return (canonical, aliases, addresses) 
Example #10
Source File: sysinfo.py    From treadmill with Apache License 2.0 6 votes vote down vote up
def hostname():
    """Hostname of the server."""
    treadmill_hostname = os.environ.get('TREADMILL_HOSTNAME')
    if treadmill_hostname:
        return treadmill_hostname.lower()

    host_name = socket.gethostname()
    port = 0
    family = 0
    socktype = 0
    proto = 0

    _family, _socktype, _proto, canonname, _sockaddr = socket.getaddrinfo(
        host_name,
        port,
        family,
        socktype,
        proto,
        socket.AI_CANONNAME)[0]

    return canonname.lower() 
Example #11
Source File: resolver.py    From bazarr with GNU General Public License v3.0 6 votes vote down vote up
def _gethostbyaddr(ip):
    try:
        dns.ipv6.inet_aton(ip)
        sockaddr = (ip, 80, 0, 0)
        family = socket.AF_INET6
    except Exception:
        sockaddr = (ip, 80)
        family = socket.AF_INET
    (name, port) = _getnameinfo(sockaddr, socket.NI_NAMEREQD)
    aliases = []
    addresses = []
    tuples = _getaddrinfo(name, 0, family, socket.SOCK_STREAM, socket.SOL_TCP,
                          socket.AI_CANONNAME)
    canonical = tuples[0][3]
    for item in tuples:
        addresses.append(item[4][0])
    # XXX we just ignore aliases
    return (canonical, aliases, addresses) 
Example #12
Source File: osdetect.py    From vulners-agent with MIT License 5 votes vote down vote up
def get_ip_mac_fqdn():

    interfaces = get_interface_list()

    primary_interface = sorted(interfaces, key=lambda k: k['ifaceName'])[0]
    ip_address = primary_interface['ipaddress']
    fqdn = socket.getaddrinfo(socket.gethostname(), 0, 0, 0, 0, socket.AI_CANONNAME)[0][3]
    mac_address = primary_interface['macaddress']
    return ip_address, mac_address, fqdn 
Example #13
Source File: resolver.py    From script.elementum.burst with Do What The F*ck You Want To Public License 5 votes vote down vote up
def _gethostbyname_ex(name):
    aliases = []
    addresses = []
    tuples = _getaddrinfo(name, 0, socket.AF_INET, socket.SOCK_STREAM,
                          socket.SOL_TCP, socket.AI_CANONNAME)
    canonical = tuples[0][3]
    for item in tuples:
        addresses.append(item[4][0])
    # XXX we just ignore aliases
    return (canonical, aliases, addresses) 
Example #14
Source File: resolver.py    From Tautulli with GNU General Public License v3.0 5 votes vote down vote up
def _gethostbyname_ex(name):
    aliases = []
    addresses = []
    tuples = _getaddrinfo(name, 0, socket.AF_INET, socket.SOCK_STREAM,
                          socket.SOL_TCP, socket.AI_CANONNAME)
    canonical = tuples[0][3]
    for item in tuples:
        addresses.append(item[4][0])
    # XXX we just ignore aliases
    return (canonical, aliases, addresses) 
Example #15
Source File: resolver.py    From arissploit with GNU General Public License v3.0 5 votes vote down vote up
def _gethostbyname_ex(name):
    aliases = []
    addresses = []
    tuples = _getaddrinfo(name, 0, socket.AF_INET, socket.SOCK_STREAM,
                          socket.SOL_TCP, socket.AI_CANONNAME)
    canonical = tuples[0][3]
    for item in tuples:
        addresses.append(item[4][0])
    # XXX we just ignore aliases
    return (canonical, aliases, addresses) 
Example #16
Source File: resolver.py    From bazarr with GNU General Public License v3.0 5 votes vote down vote up
def _gethostbyname_ex(name):
    aliases = []
    addresses = []
    tuples = _getaddrinfo(name, 0, socket.AF_INET, socket.SOCK_STREAM,
                          socket.SOL_TCP, socket.AI_CANONNAME)
    canonical = tuples[0][3]
    for item in tuples:
        addresses.append(item[4][0])
    # XXX we just ignore aliases
    return (canonical, aliases, addresses) 
Example #17
Source File: resolver.py    From elasticintel with GNU General Public License v3.0 5 votes vote down vote up
def _gethostbyname_ex(name):
    aliases = []
    addresses = []
    tuples = _getaddrinfo(name, 0, socket.AF_INET, socket.SOCK_STREAM,
                          socket.SOL_TCP, socket.AI_CANONNAME)
    canonical = tuples[0][3]
    for item in tuples:
        addresses.append(item[4][0])
    # XXX we just ignore aliases
    return (canonical, aliases, addresses) 
Example #18
Source File: resolver.py    From Cloudmare with GNU General Public License v3.0 5 votes vote down vote up
def _gethostbyname_ex(name):
    aliases = []
    addresses = []
    tuples = _getaddrinfo(name, 0, socket.AF_INET, socket.SOCK_STREAM,
                          socket.SOL_TCP, socket.AI_CANONNAME)
    canonical = tuples[0][3]
    for item in tuples:
        addresses.append(item[4][0])
    # XXX we just ignore aliases
    return (canonical, aliases, addresses) 
Example #19
Source File: resolver.py    From luscan-devel with GNU General Public License v2.0 5 votes vote down vote up
def _gethostbyname_ex(name):
    aliases = []
    addresses = []
    tuples = _getaddrinfo(name, 0, socket.AF_INET, socket.SOCK_STREAM,
                         socket.SOL_TCP, socket.AI_CANONNAME)
    canonical = tuples[0][3]
    for item in tuples:
        addresses.append(item[4][0])
    # XXX we just ignore aliases
    return (canonical, aliases, addresses) 
Example #20
Source File: config.py    From imoocc with GNU General Public License v2.0 4 votes vote down vote up
def __str__(self):
        if self.fqdn is None:
            #
            # If the SSH config contains AddressFamily, use that when
            # determining  the local host's FQDN. Using socket.getfqdn() from
            # the standard library is the most general solution, but can
            # result in noticeable delays on some platforms when IPv6 is
            # misconfigured or not available, as it calls getaddrinfo with no
            # address family specified, so both IPv4 and IPv6 are checked.
            #

            # Handle specific option
            fqdn = None
            address_family = self.config.get('addressfamily', 'any').lower()
            if address_family != 'any':
                try:
                    family = socket.AF_INET if address_family == 'inet' \
                        else socket.AF_INET6
                    results = socket.getaddrinfo(
                        self.host,
                        None,
                        family,
                        socket.SOCK_DGRAM,
                        socket.IPPROTO_IP,
                        socket.AI_CANONNAME
                    )
                    for res in results:
                        af, socktype, proto, canonname, sa = res
                        if canonname and '.' in canonname:
                            fqdn = canonname
                            break
                # giaerror -> socket.getaddrinfo() can't resolve self.host
                # (which is from socket.gethostname()). Fall back to the
                # getfqdn() call below.
                except socket.gaierror:
                    pass
            # Handle 'any' / unspecified
            if fqdn is None:
                fqdn = socket.getfqdn()
            # Cache
            self.fqdn = fqdn
        return self.fqdn