Exemple #1
0
    def test_netifaces(self):
        ifaces = get_interfaces()
        if not ifaces:
            return
        ip_ifaces = defaultdict(list)
        for iface in ifaces:
            if if_nametoindex:
                try:
                    i = if_nametoindex(iface)
                except Exception:
                    pass
                else:
                    if if_indextoname:
                        assert if_indextoname(
                            i
                        ) == iface, "expected interface %s for index %i but got %s" % (
                            iface, i, if_indextoname(i))
            ipmasks = do_get_bind_ifacemask(iface)
            for ip, _ in ipmasks:
                ip_ifaces[ip].append(iface)
        for ip, ifaces in ip_ifaces.items():
            assert get_iface(
                ip
            ) in ifaces, "expected interface for ip %s to be one of %s but got %s" % (
                ip, ifaces, get_iface(ip))
        ia = get_interfaces_addresses()
        assert ia
        #for iface, address in ia.items():
        #    iface2 = get_interface(address)
        #    assert iface2==iface, "expected %s but got %s" % (iface, iface2)
        get_gateways()
        get_bind_IPs()
        get_ssl_info()
        get_info()

        if if_indextoname:
            assert if_indextoname(-1) is None

        def invalid_iface(s):
            v = get_iface(s)
            if v:
                raise Exception(
                    "invalid IP '%s' should not return interface '%s'" %
                    (s, v))

        invalid_iface(None)
        invalid_iface("")
        invalid_iface("%")
        invalid_iface(":")
        with silence_error(net_util):
            invalid_iface("INVALIDHOSTNAME")
        invalid_iface("10.0.0")
        get_iface("localhost")

        assert get_interface("invalid") is None
Exemple #2
0
def main():
    import sys
    import socket

    from xpra.os_util import POSIX
    from xpra.util import print_nested_dict
    from xpra.net.net_util import import_netifaces, get_interfaces, if_nametoindex
    from xpra.platform import program_context
    from xpra.log import Logger, enable_color, add_debug_category, enable_debug_for
    log = Logger("network")
    with program_context("Network-Device-Info", "Network Device Info"):
        enable_color()
        verbose = "-v" in sys.argv or "--verbose" in sys.argv
        if verbose:
            enable_debug_for("network")
            add_debug_category("network")
            log.enable_debug()

        print("Network interfaces found:")
        netifaces = import_netifaces()
        for iface in get_interfaces():
            if if_nametoindex:
                print("* %s (index=%s)" %
                      (iface.ljust(20), if_nametoindex(iface)))
            else:
                print("* %s" % iface)
            addresses = netifaces.ifaddresses(iface)  #@UndefinedVariable
            for addr, defs in addresses.items():
                if addr in (socket.AF_INET, socket.AF_INET6):
                    for d in defs:
                        ip = d.get("addr")
                        if ip:
                            stype = {
                                socket.AF_INET: "IPv4",
                                socket.AF_INET6: "IPv6",
                            }[addr]
                            print(" * %s:     %s" % (stype, ip))
                            if POSIX:
                                from xpra.net.socket_util import create_tcp_socket
                                try:
                                    sock = create_tcp_socket(ip, 0)
                                    sockfd = sock.fileno()
                                    info = get_interface_info(sockfd, iface)
                                    if info:
                                        print_nested_dict(info,
                                                          prefix="    ",
                                                          lchar="-")
                                finally:
                                    sock.close()
            if not POSIX:
                info = get_interface_info(0, iface)
                if info:
                    print("  %s" % info)
Exemple #3
0
def get_interface_index(host):
    if host == "0.0.0.0" or host =="" or host=="*":
        return    pybonjour.kDNSServiceInterfaceIndexAny
        #kDNSServiceInterfaceIndexLocalOnly
    if not if_nametoindex:
        log.error("Error: cannot convert interface to index (if_nametoindex is missing)")
        log.error(" pybonjour will publish on ALL interfaces")
        return pybonjour.kDNSServiceInterfaceIndexAny
    iface = get_iface(host)
    if not iface:
        return None
    return if_nametoindex(iface)
Exemple #4
0
def get_interface_index(host):
    if host == "0.0.0.0" or host =="" or host=="*":
        return pybonjour.kDNSServiceInterfaceIndexAny
    if host=="127.0.0.1" or host=="::1":
        return pybonjour.kDNSServiceInterfaceIndexLocalOnly
    if not if_nametoindex:
        if not WIN32:
            log.error("Error: cannot convert interface to index (if_nametoindex is missing)")
            log.error(" pybonjour will publish on ALL interfaces")
        return pybonjour.kDNSServiceInterfaceIndexAny
    iface = get_iface(host)
    if not iface:
        return None
    return if_nametoindex(iface)
Exemple #5
0
def get_interface_index(host):
	log("get_interface_index(%s)", host)
	if host == "0.0.0.0" or host =="" or host=="*":
		return	avahi.IF_UNSPEC

	if not if_nametoindex:
		log.error("cannot convert interface to index (if_nametoindex is missing), so returning 'IF_UNSPEC', avahi will publish on ALL interfaces")
		return	avahi.IF_UNSPEC

	iface = get_iface(host)
	log("get_iface(%s)=%s", host, iface)
	if iface is None:
		return	avahi.IF_UNSPEC

	index = if_nametoindex(iface)
	log("if_nametoindex(%s)=%s", iface, index)
	if iface is None:
		return	avahi.IF_UNSPEC
	return index
Exemple #6
0
def get_interface_index(host):
	log("get_interface_index(%s)", host)
	if host == "0.0.0.0" or host =="" or host=="*":
		return	avahi.IF_UNSPEC

	if not if_nametoindex:
		log.error("cannot convert interface to index (if_nametoindex is missing), so returning 'IF_UNSPEC', avahi will publish on ALL interfaces")
		return	avahi.IF_UNSPEC

	iface = get_iface(host)
	log("get_iface(%s)=%s", host, iface)
	if iface is None:
		return	avahi.IF_UNSPEC

	index = if_nametoindex(iface)
	log("if_nametoindex(%s)=%s", iface, index)
	if iface is None:
		return	avahi.IF_UNSPEC
	return index
Exemple #7
0
def get_interface_index(host):
    log("get_interface_index(%s)", host)
    if host in ("0.0.0.0", "", "*", "::"):
        return avahi.IF_UNSPEC

    if not if_nametoindex:
        log.warn("Warning: cannot convert interface to index (if_nametoindex is missing)")
        log.warn(" so returning 'IF_UNSPEC', avahi will publish on ALL interfaces")
        return avahi.IF_UNSPEC

    iface = get_iface(host)
    log("get_iface(%s)=%s", host, iface)
    if iface is None:
        return avahi.IF_UNSPEC

    index = if_nametoindex(iface)
    log("if_nametoindex(%s)=%s", iface, index)
    if iface is None:
        return avahi.IF_UNSPEC
    return index
Exemple #8
0
def main():
    import sys
    from xpra.net.net_util import get_interfaces, if_nametoindex
    from xpra.platform import program_context
    from xpra.log import Logger, enable_color, add_debug_category, enable_debug_for
    log = Logger("network")
    with program_context("Network-Device-Info", "Network Device Info"):
        enable_color()
        verbose = "-v" in sys.argv or "--verbose" in sys.argv
        if verbose:
            enable_debug_for("network")
            add_debug_category("network")
            log.enable_debug()

        print("Network interfaces found:")
        for iface in get_interfaces():
            if if_nametoindex:
                print("* %s (index=%s)" %
                      (iface.ljust(20), if_nametoindex(iface)))
            else:
                print("* %s" % iface)
            print_iface(iface)