Exemplo n.º 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
Exemplo n.º 2
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)
Exemplo n.º 3
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)
Exemplo n.º 4
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
Exemplo n.º 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
Exemplo n.º 6
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
Exemplo n.º 7
0
 def invalid_iface(s):
     v = get_iface(s)
     if v:
         raise Exception(
             "invalid IP '%s' should not return interface '%s'" %
             (s, v))
Exemplo n.º 8
0
def get_interface_index(host):
    #we don't use interface numbers with zeroconf,
    #so just return the interface name,
    #which is also unique
    return get_iface(host)
Exemplo n.º 9
0
 def __init__(self,
              listen_on,
              service_name,
              service_type=XPRA_MDNS_TYPE,
              text_dict={}):
     log("ZeroconfPublishers%s",
         (listen_on, service_name, service_type, text_dict))
     self.zeroconf = None
     self.services = []
     self.registered = []
     errs = 0
     hostname = socket.gethostname() + ".local."
     all_listen_on = []
     for host_str, port in listen_on:
         if host_str == "":
             hosts = ["127.0.0.1", "::"]
         else:
             hosts = [host_str]
         for host in hosts:
             if host in ("0.0.0.0", "::", ""):
                 #annoying: we have to enumerate all interfaces
                 for iface, addresses in get_interfaces_addresses().items():
                     for af in (socket.AF_INET, socket.AF_INET6):
                         for defs in addresses.get(af, []):
                             addr = defs.get("addr")
                             if addr:
                                 try:
                                     addr_str = addr.split("%", 1)[0]
                                     address = socket.inet_pton(
                                         af, addr_str)
                                 except OSError as e:
                                     log("socket.inet_pton '%s'",
                                         addr_str,
                                         exc_info=True)
                                     log.error(
                                         "Error: cannot parse IP address '%s'",
                                         addr_str)
                                     log.error(" %s", e)
                                     continue
                                 all_listen_on.append(
                                     (addr_str, port, address))
                 continue
             try:
                 if host.find(":") >= 0:
                     address = socket.inet_pton(socket.AF_INET6, host)
                 else:
                     address = socket.inet_pton(socket.AF_INET, host)
             except OSError as e:
                 log("socket.inet_pton '%s'", host, exc_info=True)
                 log.error("Error: cannot parse IP address '%s'", host)
                 log.error(" %s", e)
                 continue
             all_listen_on.append((host, port, address))
     log("will listen on: %s", all_listen_on)
     for host, port, address in all_listen_on:
         td = text_dict
         iface = get_iface(host)
         if iface is not None and SHOW_INTERFACE:
             td = text_dict.copy()
             td["iface"] = iface
         try:
             service = ServiceInfo(
                 service_type + "local.",
                 service_name + "." + service_type + "local.", address,
                 port, 0, 0, td, hostname)
             self.services.append(service)
         except Exception as e:
             log("zeroconf ServiceInfo", exc_info=True)
             if errs == 0:
                 log.error("Error: zeroconf failed to create service")
             log.error(" for host '%s' and port %i", host, port)
             log.error(" %s", e)
             errs += 1