示例#1
0
 def do_get_socket_info(self):
     s = self._socket
     if not s:
         return None
     info = {
         "family": FAMILY_STR.get(s.family, int(s.family)),
         "proto": s.proto,
         "type": PROTOCOL_STR.get(s.type, int(s.type)),
     }
     try:
         info["timeout"] = int(1000 * (s.gettimeout() or 0))
     except:
         pass
     try:
         fd = s.fileno()
         info["fileno"] = fd
         from xpra.platform.netdev_query import get_interface_speed
         #ie: self.local = ("192.168.1.7", "14500")
         if self.local and len(self.local) == 2:
             from xpra.net.net_util import get_interface
             iface = get_interface(self.local[0])
             #ie: iface = "eth0"
             if iface and iface != "lo":
                 s = get_interface_speed(fd, iface)
                 if s > 0:
                     info["speed"] = s
     except:
         pass
     return info
示例#2
0
 def do_get_socket_info(self, s):
     if not s:
         return None
     info = {}
     try:
         info.update({
             "proto": s.proto,
             "family": FAMILY_STR.get(s.family, int(s.family)),
             "type": PROTOCOL_STR.get(s.type, int(s.type)),
         })
     except Exception:
         log("do_get_socket_info()", exc_info=True)
     if self.nodelay is not None:
         info["nodelay"] = self.nodelay
     try:
         info["timeout"] = int(1000 * (s.gettimeout() or 0))
     except:
         pass
     try:
         if POSIX:
             fd = s.fileno()
         else:
             fd = 0
         if fd:
             info["fileno"] = fd
         from xpra.platform.netdev_query import get_interface_info
         #ie: self.local = ("192.168.1.7", "14500")
         if self.local and len(self.local) == 2:
             from xpra.net.net_util import get_interface
             iface = get_interface(self.local[0])
             #ie: iface = "eth0"
             if iface and iface != "lo":
                 i = get_interface_info(fd, iface)
                 if i:
                     info["device"] = i
     except OSError as e:
         log("do_get_socket_info() error querying socket speed",
             exc_info=True)
         log.error("Error querying socket speed:")
         log.error(" %s", e)
     else:
         opts = {
             "SOCKET": get_socket_options(s, socket.SOL_SOCKET,
                                          SOCKET_OPTIONS),
         }
         if self.socktype in ("tcp", "udp", "ws", "wss", "ssl"):
             opts["IP"] = get_socket_options(s, socket.SOL_IP, IP_OPTIONS)
         if self.socktype in ("tcp", "ws", "wss", "ssl"):
             opts["TCP"] = get_socket_options(s, socket.IPPROTO_TCP,
                                              TCP_OPTIONS)
         #ipv6:  IPV6_ADDR_PREFERENCES, IPV6_CHECKSUM, IPV6_DONTFRAG, IPV6_DSTOPTS, IPV6_HOPOPTS,
         # IPV6_MULTICAST_HOPS, IPV6_MULTICAST_IF, IPV6_MULTICAST_LOOP, IPV6_NEXTHOP, IPV6_PATHMTU,
         # IPV6_PKTINFO, IPV6_PREFER_TEMPADDR, IPV6_RECVDSTOPTS, IPV6_RECVHOPLIMIT, IPV6_RECVHOPOPTS,
         # IPV6_RECVPATHMTU, IPV6_RECVPKTINFO, IPV6_RECVRTHDR, IPV6_RECVTCLASS, IPV6_RTHDR,
         # IPV6_RTHDRDSTOPTS, IPV6_TCLASS, IPV6_UNICAST_HOPS, IPV6_USE_MIN_MTU, IPV6_V6ONLY
         info["options"] = opts
     return info
示例#3
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
示例#4
0
 def do_get_socket_info(self, s) -> dict:
     if not s:
         return None
     info = {}
     try:
         info.update({
             "proto"         : s.proto,
             "family"        : FAMILY_STR.get(s.family, int(s.family)),
             "type"          : PROTOCOL_STR.get(s.type, int(s.type)),
             "cork"          : self.cork,
             })
     except AttributeError:
         log("do_get_socket_info()", exc_info=True)
     if self.nodelay is not None:
         info["nodelay"] = self.nodelay
     try:
         info["timeout"] = int(1000*(s.gettimeout() or 0))
     except socket.error:
         pass
     try:
         if POSIX:
             fd = s.fileno()
         else:
             fd = 0
         if fd:
             info["fileno"] = fd
         #ie: self.local = ("192.168.1.7", "14500")
         log("do_get_socket_info(%s) fd=%s, local=%s", s, fd, self.local)
         if self.local and len(self.local)==2:
             from xpra.net.net_util import get_interface
             iface = get_interface(self.local[0])
             #ie: iface = "eth0"
             device_info = {}
             if iface:
                 device_info["name"] = iface
             if iface and iface!="lo":
                 try:
                     from xpra.platform.netdev_query import get_interface_info
                 except ImportError as e:
                     log("do_get_socket_info() no netdev_query: %s", e)
                 else:
                     device_info.update(get_interface_info(fd, iface))
             if device_info:
                 info["device"] = device_info
     except (OSError, ValueError) as e:
         log("do_get_socket_info() error querying socket speed", exc_info=True)
         log.error("Error querying socket speed:")
         log.error(" %s", e)
     else:
         opts = {
                 "SOCKET" : get_socket_options(s, socket.SOL_SOCKET, SOCKET_OPTIONS),
                 }
         if self.socktype_wrapped in IP_SOCKTYPES:
             opts["IP"] = get_socket_options(s, socket.SOL_IP, IP_OPTIONS)
         if self.socktype_wrapped in TCP_SOCKTYPES:
             opts["TCP"] = get_socket_options(s, socket.IPPROTO_TCP, TCP_OPTIONS)
             from xpra.platform.netdev_query import get_tcp_info
             opts["TCP_INFO"] = get_tcp_info(s)
         #ipv6:  IPV6_ADDR_PREFERENCES, IPV6_CHECKSUM, IPV6_DONTFRAG, IPV6_DSTOPTS, IPV6_HOPOPTS,
         # IPV6_MULTICAST_HOPS, IPV6_MULTICAST_IF, IPV6_MULTICAST_LOOP, IPV6_NEXTHOP, IPV6_PATHMTU,
         # IPV6_PKTINFO, IPV6_PREFER_TEMPADDR, IPV6_RECVDSTOPTS, IPV6_RECVHOPLIMIT, IPV6_RECVHOPOPTS,
         # IPV6_RECVPATHMTU, IPV6_RECVPKTINFO, IPV6_RECVRTHDR, IPV6_RECVTCLASS, IPV6_RTHDR,
         # IPV6_RTHDRDSTOPTS, IPV6_TCLASS, IPV6_UNICAST_HOPS, IPV6_USE_MIN_MTU, IPV6_V6ONLY
         info["options"] = opts
     return info