Example #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
Example #2
0
 def get_uri(self, password, interface, protocol, name, stype, domain, host, address, port, text):
     dstr = ""
     tt = typedict(text)
     display = tt.strget("display", "")
     username = tt.strget("username", "")
     mode = tt.strget("mode", "")
     if display.startswith(":"):
         dstr = display[1:]
     #append interface to IPv6 host URI for link local addresses ("fe80:"):
     if interface and if_indextoname and address.lower().startswith("fe80:"):
         #ie: "fe80::c1:ac45:7351:ea69%eth1"
         address += "%%%s" % if_indextoname(interface)
     if username:
         if password:
             uri = "%s://%s:%s@%s" % (mode, username, password, address)
         else:
             uri = "%s://%s@%s" % (mode, username, address)
     else:
         uri = "%s://%s" % (mode, address)
     if port>0:
         uri += ":%s" % port
     if protocol not in ("socket", "namedpipe"):
         uri += "/"
         if dstr:
             uri += "%s" % dstr
     return uri
Example #3
0
    def __init__(self, listen_on, service_name, service_type=XPRA_MDNS_TYPE, text_dict={}):
        self.publishers = []
        for host, port in listen_on:
            iface_index = get_interface_index(host)
            log("iface_index(%s)=%s", host, iface_index)
            td = text_dict
            if SHOW_INTERFACE and if_indextoname and iface_index is not None:
                td = text_dict.copy()
                td["iface"] = if_indextoname(iface_index)
            txt = []
            if text_dict:
                for k, v in text_dict.items():
                    txt.append("%s=%s" % (k, v))
            if host == "0.0.0.0":
                host = ""
            else:
                try:
                    import socket

                    host = socket.gethostbyaddr(host)[0]
                except:
                    pass
            self.publishers.append(
                AvahiPublisher(service_name, port, service_type, domain="", host=host, text=txt, interface=iface_index)
            )
Example #4
0
 def __init__(self,
              listen_on,
              service_name,
              service_type=XPRA_MDNS_TYPE,
              text_dict={}):
     self.publishers = []
     for host, port in listen_on:
         iface_index = get_interface_index(host)
         log("iface_index(%s)=%s", host, iface_index)
         td = text_dict
         if SHOW_INTERFACE and if_indextoname and iface_index is not None:
             td = text_dict.copy()
             td["iface"] = if_indextoname(iface_index)
         txt = []
         if text_dict:
             for k, v in text_dict.items():
                 txt.append("%s=%s" % (k, v))
         if host == "0.0.0.0":
             host = ""
         else:
             try:
                 import socket
                 host = socket.gethostbyaddr(host)[0]
             except:
                 pass
         self.publishers.append(
             AvahiPublisher(service_name,
                            port,
                            service_type,
                            domain="",
                            host=host,
                            text=txt,
                            interface=iface_index))
Example #5
0
	def __init__(self, listen_on, service_name, service_type=XPRA_MDNS_TYPE, text_dict={}):
		self.publishers = []
		try:
			bus = init_system_bus()
		except Exception as e:
			log.warn("failed to connect to the system dbus: %s", e)
			log.warn(" either start a dbus session or disable mdns support")
			return
		for host, port in listen_on:
			iface_index = get_interface_index(host)
			log("iface_index(%s)=%s", host, iface_index)
			td = text_dict
			if SHOW_INTERFACE and if_indextoname and iface_index is not None:
				td = text_dict.copy()
				td["iface"] = if_indextoname(iface_index)
			txt = []
			if text_dict:
				for k,v in text_dict.items():
					txt.append("%s=%s" % (k,v))
			if host=="0.0.0.0":
				host = ""
			else:
				try:
					import socket
					host = socket.gethostbyaddr(host)[0]
				except:
					pass
			self.publishers.append(AvahiPublisher(bus, service_name, port, service_type, domain="", host=host, text=txt, interface=iface_index))
Example #6
0
 def __init__(self, listen_on, service_name, service_type=XPRA_MDNS_TYPE, text_dict={}):
     self.publishers = []
     try:
         bus = init_system_bus()
     except Exception as e:
         log.warn("failed to connect to the system dbus: %s", e)
         log.warn(" either start a dbus session or disable mdns support")
         return
     for host, port in listen_on:
         iface_index = get_interface_index(host)
         log("iface_index(%s)=%s", host, iface_index)
         td = text_dict
         if SHOW_INTERFACE and if_indextoname and iface_index is not None:
             td = text_dict.copy()
             td["iface"] = if_indextoname(iface_index)
         txt = []
         if text_dict:
             for k,v in text_dict.items():
                 txt.append("%s=%s" % (k,v))
         if host=="0.0.0.0":
             host = ""
         else:
             try:
                 import socket
                 host = socket.gethostbyaddr(host)[0]
             except:
                 pass
         self.publishers.append(AvahiPublisher(bus, service_name, port, service_type, domain="", host=host, text=txt, interface=iface_index))
Example #7
0
 def __init__(self,
              listen_on,
              service_name,
              service_type=XPRA_MDNS_TYPE,
              text_dict=None):
     log("AvahiPublishers%s",
         (listen_on, service_name, service_type, text_dict))
     self.publishers = []
     try:
         bus = init_system_bus()
     except Exception as e:
         log.warn("failed to connect to the system dbus: %s", e)
         log.warn(" either start a dbus session or disable mdns support")
         return
     for host, port in listen_on:
         iface_index = get_interface_index(host)
         log("iface_index(%s)=%s", host, iface_index)
         td = text_dict or {}
         if SHOW_INTERFACE and if_indextoname and iface_index is not None:
             td = text_dict.copy()
             td["iface"] = if_indextoname(iface_index)
         txt = []
         if text_dict:
             for k, v in text_dict.items():
                 txt.append("%s=%s" % (k, v))
         fqdn = host
         if host == "0.0.0.0":
             fqdn = ""
         elif host:
             try:
                 import socket
                 fqdn = socket.gethostbyaddr(host)[0]
                 log("gethostbyaddr(%s)=%s", host, fqdn)
                 if fqdn.find(".") < 0:
                     fqdn = socket.getfqdn(host)
                     log("getfqdn(%s)=%s", host, fqdn)
                 if fqdn.find(".") < 0:
                     if fqdn:
                         fqdn += ".local"
                     log(
                         "cannot find a fully qualified domain name for '%s', using: %s",
                         host, fqdn)
             except (OSError, IOError, IndexError):
                 log("failed to get hostbyaddr for '%s'",
                     host,
                     exc_info=True)
         self.publishers.append(
             AvahiPublisher(bus,
                            service_name,
                            port,
                            service_type,
                            domain="",
                            host=fqdn,
                            text=txt,
                            interface=iface_index))
Example #8
0
 def __init__(self, listen_on, service_name, service_type=XPRA_MDNS_TYPE, text_dict={}):
     self.publishers = []
     for host, port in listen_on:
         iface_index = get_interface_index(host)
         log("iface_index(%s)=%s", host, iface_index)
         td = text_dict
         if SHOW_INTERFACE and if_indextoname:
             td = text_dict.copy()
             td["iface"] = if_indextoname(iface_index)
         txt = pybonjour.TXTRecord(td)
         self.publishers.append(BonjourPublisher(iface_index, port, service_name, service_type, txt))
Example #9
0
 def __init__(self, listen_on, service_name, service_type=XPRA_MDNS_TYPE, text_dict={}):
     log("BonjourPublishers%s", (listen_on, service_name, service_type, text_dict))
     self.publishers = []
     for host, port in listen_on:
         iface_index = get_interface_index(host)
         log("iface_index(%s)=%s", host, iface_index)
         td = text_dict
         if iface_index is not None and SHOW_INTERFACE and if_indextoname:
             td = text_dict.copy()
             td["iface"] = if_indextoname(iface_index)
         txt = pybonjour.TXTRecord(td)
         self.publishers.append(BonjourPublisher(iface_index, port, service_name, service_type, txt))
Example #10
0
 def get_uri(self, password, interface, protocol, name, stype, domain, host,
             address, port, text):
     dstr = ""
     tt = typedict(text)
     display = tt.strget("display", "")
     username = tt.strget("username", "")
     mode = tt.strget("mode", "")
     if not mode:
         #guess the mode from the service name,
         #ie: "localhost.localdomain :2 (wss)" -> "wss"
         #ie: "localhost.localdomain :2 (ssh-2)" -> "ssh"
         pos = name.rfind("(")
         if name.endswith(")") and pos > 0:
             mode = name[pos + 1:-1].split("-")[0]
             if mode not in ("tcp", "ws", "wss", "ssl", "ssh"):
                 return ""
         else:
             mode = "tcp"
     if display and display.startswith(":"):
         dstr = display[1:]
     #append interface to IPv6 host URI for link local addresses ("fe80:"):
     if interface and if_indextoname and address.lower().startswith(
             "fe80:"):
         #ie: "fe80::c1:ac45:7351:ea69%eth1"
         address += "%%%s" % if_indextoname(interface)
     if username:
         if password:
             uri = "%s://%s:%s@%s" % (mode, username, password, address)
         else:
             uri = "%s://%s@%s" % (mode, username, address)
     else:
         uri = "%s://%s" % (mode, address)
     if port > 0 and DEFAULT_PORTS.get(mode, 0) != port:
         uri += ":%s" % port
     if protocol not in ("socket", "namedpipe"):
         uri += "/"
         if dstr:
             uri += "%s" % dstr
     return uri