Python socket.NI_NAMEREQD() Examples

The following are 20 code examples for showing how to use socket.NI_NAMEREQD(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

You may also check out all available functions/classes of the module socket , or try the search function .

Example 1
Project: script.elementum.burst   Author: elgatito   File: resolver.py    License: 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
Project: luscan-devel   Author: blackye   File: resolver.py    License: 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 3
Project: Cloudmare   Author: MrH0wl   File: resolver.py    License: 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 4
Project: elasticintel   Author: securityclippy   File: resolver.py    License: 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 5
Project: bazarr   Author: morpheus65535   File: resolver.py    License: 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 6
Project: arissploit   Author: entynetproject   File: resolver.py    License: 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 7
Project: Tautulli   Author: Tautulli   File: resolver.py    License: 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 8
Project: script.elementum.burst   Author: elgatito   File: resolver.py    License: Do What The F*ck You Want To Public License 5 votes vote down vote up
def _getnameinfo(sockaddr, flags=0):
    host = sockaddr[0]
    port = sockaddr[1]
    if len(sockaddr) == 4:
        scope = sockaddr[3]
        family = socket.AF_INET6
    else:
        scope = None
        family = socket.AF_INET
    tuples = _getaddrinfo(host, port, family, socket.SOCK_STREAM,
                          socket.SOL_TCP, 0)
    if len(tuples) > 1:
        raise socket.error('sockaddr resolved to multiple addresses')
    addr = tuples[0][4][0]
    if flags & socket.NI_DGRAM:
        pname = 'udp'
    else:
        pname = 'tcp'
    qname = dns.reversename.from_address(addr)
    if flags & socket.NI_NUMERICHOST == 0:
        try:
            answer = _resolver.query(qname, 'PTR')
            hostname = answer.rrset[0].target.to_text(True)
        except (dns.resolver.NXDOMAIN, dns.resolver.NoAnswer):
            if flags & socket.NI_NAMEREQD:
                raise socket.gaierror(socket.EAI_NONAME)
            hostname = addr
            if scope is not None:
                hostname += '%' + str(scope)
    else:
        hostname = addr
        if scope is not None:
            hostname += '%' + str(scope)
    if flags & socket.NI_NUMERICSERV:
        service = str(port)
    else:
        service = socket.getservbyport(port, pname)
    return (hostname, service) 
Example 9
Project: luscan-devel   Author: blackye   File: resolver.py    License: GNU General Public License v2.0 5 votes vote down vote up
def _getnameinfo(sockaddr, flags=0):
    host = sockaddr[0]
    port = sockaddr[1]
    if len(sockaddr) == 4:
        scope = sockaddr[3]
        family = socket.AF_INET6
    else:
        scope = None
        family = socket.AF_INET
    tuples = _getaddrinfo(host, port, family, socket.SOCK_STREAM,
                          socket.SOL_TCP, 0)
    if len(tuples) > 1:
        raise socket.error('sockaddr resolved to multiple addresses')
    addr = tuples[0][4][0]
    if flags & socket.NI_DGRAM:
        pname = 'udp'
    else:
        pname = 'tcp'
    qname = dns.reversename.from_address(addr)
    if flags & socket.NI_NUMERICHOST == 0:
        try:
            answer = _resolver.query(qname, 'PTR')
            hostname = answer.rrset[0].target.to_text(True)
        except (dns.resolver.NXDOMAIN, dns.resolver.NoAnswer):
            if flags & socket.NI_NAMEREQD:
                raise socket.gaierror(socket.EAI_NONAME)
            hostname = addr
            if scope is not None:
                hostname += '%' + str(scope)
    else:
        hostname = addr
        if scope is not None:
            hostname += '%' + str(scope)
    if flags & socket.NI_NUMERICSERV:
        service = str(port)
    else:
        service = socket.getservbyport(port, pname)
    return (hostname, service) 
Example 10
Project: medicare-demo   Author: ofermend   File: test_socket.py    License: Apache License 2.0 5 votes vote down vote up
def testHost(self):
        for address, flags, expected in [
            ( ("www.python.org", 80),  0,                     "dinsdale.python.org"),
            ( ("www.python.org", 80),  socket.NI_NUMERICHOST, "82.94.164.162"      ),
            ( ("www.python.org", 80),  socket.NI_NAMEREQD,    "dinsdale.python.org"),
            ( ("82.94.164.162",  80),  socket.NI_NAMEREQD,    "dinsdale.python.org"),
        ]:
            result = socket.getnameinfo(address, flags)
            self.failUnlessEqual(result[0], expected) 
Example 11
Project: medicare-demo   Author: ofermend   File: test_socket.py    License: Apache License 2.0 5 votes vote down vote up
def testNI_NAMEREQD(self):
        # This test may delay for some seconds
        unreversible_address = "198.51.100.1"
        try:
            socket.getnameinfo( (unreversible_address, 80), socket.NI_NAMEREQD)
        except socket.gaierror, ge:
            self.failUnlessEqual(ge[0], socket.EAI_NONAME) 
Example 12
Project: Cloudmare   Author: MrH0wl   File: resolver.py    License: GNU General Public License v3.0 5 votes vote down vote up
def _getnameinfo(sockaddr, flags=0):
    host = sockaddr[0]
    port = sockaddr[1]
    if len(sockaddr) == 4:
        scope = sockaddr[3]
        family = socket.AF_INET6
    else:
        scope = None
        family = socket.AF_INET
    tuples = _getaddrinfo(host, port, family, socket.SOCK_STREAM,
                          socket.SOL_TCP, 0)
    if len(tuples) > 1:
        raise socket.error('sockaddr resolved to multiple addresses')
    addr = tuples[0][4][0]
    if flags & socket.NI_DGRAM:
        pname = 'udp'
    else:
        pname = 'tcp'
    qname = thirdparty.dns.reversename.from_address(addr)
    if flags & socket.NI_NUMERICHOST == 0:
        try:
            answer = _resolver.query(qname, 'PTR')
            hostname = answer.rrset[0].target.to_text(True)
        except (thirdparty.dns.resolver.NXDOMAIN, thirdparty.dns.resolver.NoAnswer):
            if flags & socket.NI_NAMEREQD:
                raise socket.gaierror(socket.EAI_NONAME)
            hostname = addr
            if scope is not None:
                hostname += '%' + str(scope)
    else:
        hostname = addr
        if scope is not None:
            hostname += '%' + str(scope)
    if flags & socket.NI_NUMERICSERV:
        service = str(port)
    else:
        service = socket.getservbyport(port, pname)
    return (hostname, service) 
Example 13
Project: elasticintel   Author: securityclippy   File: resolver.py    License: GNU General Public License v3.0 5 votes vote down vote up
def _getnameinfo(sockaddr, flags=0):
    host = sockaddr[0]
    port = sockaddr[1]
    if len(sockaddr) == 4:
        scope = sockaddr[3]
        family = socket.AF_INET6
    else:
        scope = None
        family = socket.AF_INET
    tuples = _getaddrinfo(host, port, family, socket.SOCK_STREAM,
                          socket.SOL_TCP, 0)
    if len(tuples) > 1:
        raise socket.error('sockaddr resolved to multiple addresses')
    addr = tuples[0][4][0]
    if flags & socket.NI_DGRAM:
        pname = 'udp'
    else:
        pname = 'tcp'
    qname = dns.reversename.from_address(addr)
    if flags & socket.NI_NUMERICHOST == 0:
        try:
            answer = _resolver.query(qname, 'PTR')
            hostname = answer.rrset[0].target.to_text(True)
        except (dns.resolver.NXDOMAIN, dns.resolver.NoAnswer):
            if flags & socket.NI_NAMEREQD:
                raise socket.gaierror(socket.EAI_NONAME)
            hostname = addr
            if scope is not None:
                hostname += '%' + str(scope)
    else:
        hostname = addr
        if scope is not None:
            hostname += '%' + str(scope)
    if flags & socket.NI_NUMERICSERV:
        service = str(port)
    else:
        service = socket.getservbyport(port, pname)
    return (hostname, service) 
Example 14
Project: CTFCrackTools-V2   Author: Acmesec   File: test_socket.py    License: GNU General Public License v3.0 5 votes vote down vote up
def testPort(self):
        for address, flags, expected in [
            ( ("127.0.0.1", 25),  0,                     "smtp" ),
            ( ("127.0.0.1", 25),  socket.NI_NUMERICSERV, 25     ),

            # This portion of the test does not suceed on OS X;
            # the above entries probably suffice
            # ( ("127.0.0.1", 513), socket.NI_DGRAM,       "who"  ),
            # ( ("127.0.0.1", 513), 0,                     "login"),
        ]:
            result = socket.getnameinfo(address, flags)
            self.failUnlessEqual(result[1], expected)


    # This test currently fails due to the recent changes (as of March 2014) at python.org:
    # TBD perhaps there are well-known addresses that guarantee stable resolution

    # def testHost(self):
    #     for address, flags, expected in [
    #         ( ("www.python.org", 80),  0,                     "dinsdale.python.org"),
    #         ( ("www.python.org", 80),  socket.NI_NUMERICHOST, "82.94.164.162"      ),
    #         ( ("www.python.org", 80),  socket.NI_NAMEREQD,    "dinsdale.python.org"),
    #         ( ("82.94.164.162",  80),  socket.NI_NAMEREQD,    "dinsdale.python.org"),
    #     ]:
    #         result = socket.getnameinfo(address, flags)
    #         self.failUnlessEqual(result[0], expected) 
Example 15
Project: CTFCrackTools-V2   Author: Acmesec   File: test_socket.py    License: GNU General Public License v3.0 5 votes vote down vote up
def testNI_NAMEREQD(self):
        # This test may delay for some seconds
        unreversible_address = "198.51.100.1"
        try:
            socket.getnameinfo( (unreversible_address, 80), socket.NI_NAMEREQD)
        except socket.gaierror, ge:
            self.failUnlessEqual(ge[0], socket.EAI_NONAME) 
Example 16
Project: bazarr   Author: morpheus65535   File: resolver.py    License: GNU General Public License v3.0 5 votes vote down vote up
def _getnameinfo(sockaddr, flags=0):
    host = sockaddr[0]
    port = sockaddr[1]
    if len(sockaddr) == 4:
        scope = sockaddr[3]
        family = socket.AF_INET6
    else:
        scope = None
        family = socket.AF_INET
    tuples = _getaddrinfo(host, port, family, socket.SOCK_STREAM,
                          socket.SOL_TCP, 0)
    if len(tuples) > 1:
        raise socket.error('sockaddr resolved to multiple addresses')
    addr = tuples[0][4][0]
    if flags & socket.NI_DGRAM:
        pname = 'udp'
    else:
        pname = 'tcp'
    qname = dns.reversename.from_address(addr)
    if flags & socket.NI_NUMERICHOST == 0:
        try:
            answer = _resolver.query(qname, 'PTR')
            hostname = answer.rrset[0].target.to_text(True)
        except (dns.resolver.NXDOMAIN, dns.resolver.NoAnswer):
            if flags & socket.NI_NAMEREQD:
                raise socket.gaierror(socket.EAI_NONAME)
            hostname = addr
            if scope is not None:
                hostname += '%' + str(scope)
    else:
        hostname = addr
        if scope is not None:
            hostname += '%' + str(scope)
    if flags & socket.NI_NUMERICSERV:
        service = str(port)
    else:
        service = socket.getservbyport(port, pname)
    return (hostname, service) 
Example 17
Project: arissploit   Author: entynetproject   File: resolver.py    License: GNU General Public License v3.0 5 votes vote down vote up
def _getnameinfo(sockaddr, flags=0):
    host = sockaddr[0]
    port = sockaddr[1]
    if len(sockaddr) == 4:
        scope = sockaddr[3]
        family = socket.AF_INET6
    else:
        scope = None
        family = socket.AF_INET
    tuples = _getaddrinfo(host, port, family, socket.SOCK_STREAM,
                          socket.SOL_TCP, 0)
    if len(tuples) > 1:
        raise socket.error('sockaddr resolved to multiple addresses')
    addr = tuples[0][4][0]
    if flags & socket.NI_DGRAM:
        pname = 'udp'
    else:
        pname = 'tcp'
    qname = dns.reversename.from_address(addr)
    if flags & socket.NI_NUMERICHOST == 0:
        try:
            answer = _resolver.query(qname, 'PTR')
            hostname = answer.rrset[0].target.to_text(True)
        except (dns.resolver.NXDOMAIN, dns.resolver.NoAnswer):
            if flags & socket.NI_NAMEREQD:
                raise socket.gaierror(socket.EAI_NONAME)
            hostname = addr
            if scope is not None:
                hostname += '%' + str(scope)
    else:
        hostname = addr
        if scope is not None:
            hostname += '%' + str(scope)
    if flags & socket.NI_NUMERICSERV:
        service = str(port)
    else:
        service = socket.getservbyport(port, pname)
    return (hostname, service) 
Example 18
Project: Tautulli   Author: Tautulli   File: resolver.py    License: GNU General Public License v3.0 5 votes vote down vote up
def _getnameinfo(sockaddr, flags=0):
    host = sockaddr[0]
    port = sockaddr[1]
    if len(sockaddr) == 4:
        scope = sockaddr[3]
        family = socket.AF_INET6
    else:
        scope = None
        family = socket.AF_INET
    tuples = _getaddrinfo(host, port, family, socket.SOCK_STREAM,
                          socket.SOL_TCP, 0)
    if len(tuples) > 1:
        raise socket.error('sockaddr resolved to multiple addresses')
    addr = tuples[0][4][0]
    if flags & socket.NI_DGRAM:
        pname = 'udp'
    else:
        pname = 'tcp'
    qname = dns.reversename.from_address(addr)
    if flags & socket.NI_NUMERICHOST == 0:
        try:
            answer = _resolver.query(qname, 'PTR')
            hostname = answer.rrset[0].target.to_text(True)
        except (dns.resolver.NXDOMAIN, dns.resolver.NoAnswer):
            if flags & socket.NI_NAMEREQD:
                raise socket.gaierror(socket.EAI_NONAME)
            hostname = addr
            if scope is not None:
                hostname += '%' + str(scope)
    else:
        hostname = addr
        if scope is not None:
            hostname += '%' + str(scope)
    if flags & socket.NI_NUMERICSERV:
        service = str(port)
    else:
        service = socket.getservbyport(port, pname)
    return (hostname, service) 
Example 19
Project: CTFCrackTools   Author: Acmesec   File: test_socket.py    License: GNU General Public License v3.0 5 votes vote down vote up
def testPort(self):
        for address, flags, expected in [
            ( ("127.0.0.1", 25),  0,                     "smtp" ),
            ( ("127.0.0.1", 25),  socket.NI_NUMERICSERV, 25     ),

            # This portion of the test does not suceed on OS X;
            # the above entries probably suffice
            # ( ("127.0.0.1", 513), socket.NI_DGRAM,       "who"  ),
            # ( ("127.0.0.1", 513), 0,                     "login"),
        ]:
            result = socket.getnameinfo(address, flags)
            self.failUnlessEqual(result[1], expected)


    # This test currently fails due to the recent changes (as of March 2014) at python.org:
    # TBD perhaps there are well-known addresses that guarantee stable resolution

    # def testHost(self):
    #     for address, flags, expected in [
    #         ( ("www.python.org", 80),  0,                     "dinsdale.python.org"),
    #         ( ("www.python.org", 80),  socket.NI_NUMERICHOST, "82.94.164.162"      ),
    #         ( ("www.python.org", 80),  socket.NI_NAMEREQD,    "dinsdale.python.org"),
    #         ( ("82.94.164.162",  80),  socket.NI_NAMEREQD,    "dinsdale.python.org"),
    #     ]:
    #         result = socket.getnameinfo(address, flags)
    #         self.failUnlessEqual(result[0], expected) 
Example 20
Project: CTFCrackTools   Author: Acmesec   File: test_socket.py    License: GNU General Public License v3.0 5 votes vote down vote up
def testNI_NAMEREQD(self):
        # This test may delay for some seconds
        unreversible_address = "198.51.100.1"
        try:
            socket.getnameinfo( (unreversible_address, 80), socket.NI_NAMEREQD)
        except socket.gaierror, ge:
            self.failUnlessEqual(ge[0], socket.EAI_NONAME)