예제 #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
예제 #2
0
    def __init__(self,
                 listen_on,
                 service_name,
                 service_type=XPRA_MDNS_TYPE,
                 text_dict=None):
        log("ZeroconfPublishers%s",
            (listen_on, service_name, service_type, text_dict))
        self.services = []
        self.ports = {}

        def add_address(host, port, af=socket.AF_INET):
            try:
                if af == socket.AF_INET6 and host.find("%"):
                    host = host.split("%")[0]
                address = inet_ton(af, host)
                sn = service_name
                ports = set(self.ports.get(service_name, ()))
                log("add_address(%s, %s, %s) ports=%s", host, port, af, ports)
                ports.add(port)
                if len(ports) > 1:
                    sn += "-%i" % len(ports)
                zp = ZeroconfPublisher(address, host, port, sn, service_type,
                                       text_dict)
            except Exception as e:
                log("inet_aton(%s)", host, exc_info=True)
                log.warn("Warning: cannot publish records on %s:", host)
                log.warn(" %s", e)
            else:
                self.services.append(zp)
                self.ports[service_name] = ports

        for host, port in listen_on:
            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):
                        if af == socket.AF_INET6 and not IPV6:
                            continue
                        log("%s: %s", iface, addresses.get(socket.AF_INET, {}))
                        for defs in addresses.get(af, {}):
                            addr = defs.get("addr")
                            if addr:
                                add_address(addr, port, af)
                continue
            if host == "":
                host = "127.0.0.1"
            af = socket.AF_INET
            if host.find(":") >= 0:
                if IPV6:
                    af = socket.AF_INET6
                else:
                    host = "127.0.0.1"
            add_address(host, port, af)
예제 #3
0
 def test_netifaces(self):
     ifaces = get_interfaces()
     if not ifaces:
         return
     for iface in ifaces:
         do_get_bind_ifacemask(iface)
     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()
예제 #4
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