コード例 #1
0
def get_local_ip():
    """
    Returns a deferred which will be called with a
    2-uple (lan_flag, ip_address) :
        - lan_flag:
            - True if it's a local network (RFC1918)
            - False if it's a WAN address
        
        - ip_address is the actual ip address
    
    @return: A deferred called with the above defined tuple
    @rtype: L{twisted.internet.defer.Deferred}
    """
    # first we try a connected udp socket, then via multicast
    logging.debug("Resolving dns to get udp ip")
    try:
        ipaddr = yield reactor.resolve('A.ROOT-SERVERS.NET')
    except:
        pass
    else:
        udpprot = DatagramProtocol()
        port = reactor.listenUDP(0, udpprot)
        udpprot.transport.connect(ipaddr, 7)
        localip = udpprot.transport.getHost().host
        port.stopListening()

        if is_bogus_ip(localip):
            raise RuntimeError, "Invalid IP address returned"
        else:
            defer.returnValue((is_rfc1918_ip(localip), localip))

    logging.debug("Multicast ping to retrieve local IP")
    ipaddr = yield _discover_multicast()
    defer.returnValue((is_rfc1918_ip(ipaddr), ipaddr))
コード例 #2
0
ファイル: ipdiscover.py プロジェクト: llazzaro/p2pool
def get_local_ip():
    """
    Returns a deferred which will be called with a
    2-uple (lan_flag, ip_address) :
        - lan_flag:
            - True if it's a local network (RFC1918)
            - False if it's a WAN address

        - ip_address is the actual ip address

    @return: A deferred called with the above defined tuple
    @rtype: L{twisted.internet.defer.Deferred}
    """
    # first we try a connected udp socket, then via multicast
    logging.debug("Resolving dns to get udp ip")
    try:
        ipaddr = yield reactor.resolve('A.ROOT-SERVERS.NET')
    except:
        pass
    else:
        udpprot = DatagramProtocol()
        port = reactor.listenUDP(0, udpprot)
        udpprot.transport.connect(ipaddr, 7)
        localip = udpprot.transport.getHost().host
        port.stopListening()

        if is_bogus_ip(localip):
            raise RuntimeError("Invalid IP address returned")
        else:
            defer.returnValue((is_rfc1918_ip(localip), localip))

    logging.debug("Multicast ping to retrieve local IP")
    ipaddr = yield _discover_multicast()
    defer.returnValue((is_rfc1918_ip(ipaddr), ipaddr))
コード例 #3
0
def _get_via_connected_udp(ipaddr):
    """
	Init a UDP socket ip discovery. We do a dns query, and retreive our
	ip address from the connected udp socket.
	
	@param ipaddr: The ip address of a dns server
	@type ipaddr: a string "x.x.x.x"
	@raise RuntimeError: When the ip is a bogus ip (0.0.0.0 or alike)
	"""
    udpprot = DatagramProtocol()
    port = reactor.listenUDP(0, udpprot)
    udpprot.transport.connect(ipaddr, 7)
    localip = udpprot.transport.getHost().host
    port.stopListening()

    if is_bogus_ip(localip):
        raise RuntimeError, "Invalid IP addres returned"
    else:
        return (is_rfc1918_ip(localip), localip)
コード例 #4
0
ファイル: ipdiscover.py プロジェクト: gyver/p2pool
def _get_via_connected_udp(ipaddr):
	"""
	Init a UDP socket ip discovery. We do a dns query, and retreive our
	ip address from the connected udp socket.
	
	@param ipaddr: The ip address of a dns server
	@type ipaddr: a string "x.x.x.x"
	@raise RuntimeError: When the ip is a bogus ip (0.0.0.0 or alike)
	"""
	udpprot = DatagramProtocol()
	port = reactor.listenUDP(0, udpprot)
	udpprot.transport.connect(ipaddr, 7)
	localip = udpprot.transport.getHost().host
	port.stopListening()
	
	if is_bogus_ip(localip):
		raise RuntimeError, "Invalid IP addres returned"
	else:
		return (is_rfc1918_ip(localip), localip)