internal static TcpConnectionInformation[] ParseActiveTcpConnectionsFromFiles(string tcp4ConnectionsFile, string tcp6ConnectionsFile)
        {
            if (!File.Exists(tcp4ConnectionsFile) || !File.Exists(tcp6ConnectionsFile))
            {
                throw new PlatformNotSupportedException(SR.net_InformationUnavailableOnPlatform);
            }

            string tcp4FileContents = File.ReadAllText(tcp4ConnectionsFile);
            string[] v4connections = tcp4FileContents.Split(s_newLineSeparator, StringSplitOptions.RemoveEmptyEntries);

            string tcp6FileContents = File.ReadAllText(tcp6ConnectionsFile);
            string[] v6connections = tcp6FileContents.Split(s_newLineSeparator, StringSplitOptions.RemoveEmptyEntries);

            // First line is header in each file.
            TcpConnectionInformation[] connections = new TcpConnectionInformation[v4connections.Length + v6connections.Length - 2];
            int index = 0;

            // TCP Connections
            for (int i = 1; i < v4connections.Length; i++) // Skip first line header.
            {
                string line = v4connections[i];
                connections[index++] = ParseTcpConnectionInformationFromLine(line);
            }

            // TCP6 Connections
            for (int i = 1; i < v6connections.Length; i++) // Skip first line header.
            {
                string line = v6connections[i];
                connections[index++] = ParseTcpConnectionInformationFromLine(line);
            }

            return connections;
        }
        internal static TcpConnectionInformation[] ParseActiveTcpConnectionsFromFiles(string tcp4ConnectionsFile, string tcp6ConnectionsFile)
        {
            string tcp4FileContents = File.ReadAllText(tcp4ConnectionsFile);

            string[] v4connections = tcp4FileContents.Split(s_newLineSeparator, StringSplitOptions.RemoveEmptyEntries);

            string tcp6FileContents = File.ReadAllText(tcp6ConnectionsFile);

            string[] v6connections = tcp6FileContents.Split(s_newLineSeparator, StringSplitOptions.RemoveEmptyEntries);

            TcpConnectionInformation[] connections = new TcpConnectionInformation[v4connections.Length + v6connections.Length - 2]; // First line is header in each file
            int index = 0;

            // TCP Connections
            for (int i = 1; i < v4connections.Length; i++) // Skip first line header
            {
                string line = v4connections[i];
                connections[index++] = ParseTcpConnectionInformationFromLine(line);
            }

            // TCP6 Connections
            for (int i = 1; i < v6connections.Length; i++) // Skip first line header
            {
                string line = v6connections[i];
                connections[index++] = ParseTcpConnectionInformationFromLine(line);
            }

            return(connections);
        }
        internal static TcpConnectionInformation[] ParseActiveTcpConnectionsFromFiles(string tcp4ConnectionsFile, string tcp6ConnectionsFile)
        {
            string tcp4FileContents = File.ReadAllText(tcp4ConnectionsFile);
            string[] v4connections = tcp4FileContents.Split(s_newLineSeparator, StringSplitOptions.RemoveEmptyEntries);

            string tcp6FileContents = File.ReadAllText(tcp6ConnectionsFile);
            string[] v6connections = tcp6FileContents.Split(s_newLineSeparator, StringSplitOptions.RemoveEmptyEntries);

            TcpConnectionInformation[] connections = new TcpConnectionInformation[v4connections.Length + v6connections.Length - 2]; // First line is header in each file
            int index = 0;

            // TCP Connections
            for (int i = 1; i < v4connections.Length; i++) // Skip first line header
            {
                string line = v4connections[i];
                connections[index++] = ParseTcpConnectionInformationFromLine(line);
            }

            // TCP6 Connections
            for (int i = 1; i < v6connections.Length; i++) // Skip first line header
            {
                string line = v6connections[i];
                connections[index++] = ParseTcpConnectionInformationFromLine(line);
            }

            return connections;
        }
Exemplo n.º 4
0
        private unsafe TcpConnectionInformation[] GetTcpConnections(bool listeners)
        {
            int realCount = Interop.Sys.GetEstimatedTcpConnectionCount();
            int infoCount = realCount * 2;

            Interop.Sys.NativeTcpConnectionInformation *infos = stackalloc Interop.Sys.NativeTcpConnectionInformation[infoCount];
            if (Interop.Sys.GetActiveTcpConnectionInfos(infos, &infoCount) == -1)
            {
                throw new NetworkInformationException(SR.net_PInvokeError);
            }

            TcpConnectionInformation[] connectionInformations = new TcpConnectionInformation[infoCount];
            int nextResultIndex = 0;

            for (int i = 0; i < infoCount; i++)
            {
                Interop.Sys.NativeTcpConnectionInformation nativeInfo = infos[i];
                TcpState state = nativeInfo.State;

                if (listeners != (state == TcpState.Listen))
                {
                    continue;
                }

                byte[] localBytes = new byte[nativeInfo.LocalEndPoint.NumAddressBytes];
                fixed(byte *localBytesPtr = localBytes)
                {
                    Buffer.MemoryCopy(nativeInfo.LocalEndPoint.AddressBytes, localBytesPtr, localBytes.Length, localBytes.Length);
                }

                IPAddress  localIPAddress = new IPAddress(localBytes);
                IPEndPoint local          = new IPEndPoint(localIPAddress, (int)nativeInfo.LocalEndPoint.Port);

                IPAddress remoteIPAddress;
                if (nativeInfo.RemoteEndPoint.NumAddressBytes == 0)
                {
                    remoteIPAddress = IPAddress.Any;
                }
                else
                {
                    byte[] remoteBytes = new byte[nativeInfo.RemoteEndPoint.NumAddressBytes];
                    fixed(byte *remoteBytesPtr = &remoteBytes[0])
                    {
                        Buffer.MemoryCopy(nativeInfo.RemoteEndPoint.AddressBytes, remoteBytesPtr, remoteBytes.Length, remoteBytes.Length);
                    }

                    remoteIPAddress = new IPAddress(remoteBytes);
                }

                IPEndPoint remote = new IPEndPoint(remoteIPAddress, (int)nativeInfo.RemoteEndPoint.Port);
                connectionInformations[nextResultIndex++] = new SimpleTcpConnectionInformation(local, remote, state);
            }

            if (nextResultIndex != connectionInformations.Length)
            {
                Array.Resize(ref connectionInformations, nextResultIndex);
            }

            return(connectionInformations);
        }
        internal static TcpConnectionInformation[] ParseActiveTcpConnectionsFromFiles(string tcp4ConnectionsFile, string tcp6ConnectionsFile)
        {
            if (!File.Exists(tcp4ConnectionsFile) || !File.Exists(tcp6ConnectionsFile))
            {
                throw new PlatformNotSupportedException(SR.net_InformationUnavailableOnPlatform);
            }

            string tcp4FileContents = File.ReadAllText(tcp4ConnectionsFile);

            string[] v4connections = tcp4FileContents.Split(s_newLineSeparator, StringSplitOptions.RemoveEmptyEntries);

            string tcp6FileContents = File.ReadAllText(tcp6ConnectionsFile);

            string[] v6connections = tcp6FileContents.Split(s_newLineSeparator, StringSplitOptions.RemoveEmptyEntries);

            // First line is header in each file.
            TcpConnectionInformation[] connections = new TcpConnectionInformation[v4connections.Length + v6connections.Length - 2];
            int index = 0;

            // TCP Connections
            for (int i = 1; i < v4connections.Length; i++) // Skip first line header.
            {
                string line = v4connections[i];
                connections[index++] = ParseTcpConnectionInformationFromLine(line);
            }

            // TCP6 Connections
            for (int i = 1; i < v6connections.Length; i++) // Skip first line header.
            {
                string line = v6connections[i];
                connections[index++] = ParseTcpConnectionInformationFromLine(line);
            }

            return(connections);
        }
 public NetConnection(TcpConnectionInformation tcp)
 {
     Protocol = "TCP";
     RemoteAddress = tcp.RemoteEndPoint.Address.ToString();
     RemotePort = tcp.RemoteEndPoint.Port;
     LocalAddress = tcp.LocalEndPoint.Address.ToString();
     LocalPort = tcp.LocalEndPoint.Port;
     State = tcp.State.ToString();
 }
Exemplo n.º 7
0
        public static bool IsConnected(this System.Net.Sockets.TcpClient client)
        {
            System.Net.NetworkInformation.TcpConnectionInformation tcpConnectionInformation = System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties()
                                                                                              .GetActiveTcpConnections()
                                                                                              .SingleOrDefault(x => x.RemoteEndPoint.Equals(client.Client.RemoteEndPoint));


            return(tcpConnectionInformation == null ? false : tcpConnectionInformation.State == System.Net.NetworkInformation.TcpState.Established ? true : false);
        }
Exemplo n.º 8
0
        internal static IPEndPoint[] ParseActiveTcpListenersFromFiles(string tcp4ConnectionsFile, string tcp6ConnectionsFile)
        {
            if (!File.Exists(tcp4ConnectionsFile) || !File.Exists(tcp6ConnectionsFile))
            {
                throw new PlatformNotSupportedException(SR.net_InformationUnavailableOnPlatform);
            }

            string tcp4FileContents = File.ReadAllText(tcp4ConnectionsFile);

            string[] v4connections = tcp4FileContents.Split(s_newLineSeparator, StringSplitOptions.RemoveEmptyEntries);

            string tcp6FileContents = File.ReadAllText(tcp6ConnectionsFile);

            string[] v6connections = tcp6FileContents.Split(s_newLineSeparator, StringSplitOptions.RemoveEmptyEntries);

            // First line is header in each file.
            IPEndPoint[] endPoints = new IPEndPoint[v4connections.Length + v6connections.Length - 2];
            int          index     = 0;
            int          skip      = 0;

            // TCP Connections
            for (int i = 1; i < v4connections.Length; i++) // Skip first line header.
            {
                TcpConnectionInformation ti = ParseTcpConnectionInformationFromLine(v4connections[i]);
                if (ti.State == TcpState.Listen)
                {
                    endPoints[index] = ti.LocalEndPoint;
                    index++;
                }
                else
                {
                    skip++;
                }
            }

            // TCP6 Connections
            for (int i = 1; i < v6connections.Length; i++) // Skip first line header.
            {
                TcpConnectionInformation ti = ParseTcpConnectionInformationFromLine(v6connections[i]);
                if (ti.State == TcpState.Listen)
                {
                    endPoints[index] = ti.LocalEndPoint;
                    index++;
                }
                else
                {
                    skip++;
                }
            }

            if (skip != 0)
            {
                Array.Resize(ref endPoints, endPoints.Length - skip);
            }

            return(endPoints);
        }
Exemplo n.º 9
0
 private static bool IsPortAvailable(int port, TcpConnectionInformation[] tcpConnInfoArray)
 {
     bool isAvailable = true;
     foreach (TcpConnectionInformation tcpi in tcpConnInfoArray)
     {
         if (port > 0 && tcpi.LocalEndPoint.Port == port)
         {
             isAvailable = false;
             break;
         }
     }
     return isAvailable;
 }
Exemplo n.º 10
0
        public unsafe override TcpConnectionInformation[] GetActiveTcpConnections()
        {
            int realCount = Interop.Sys.GetEstimatedTcpConnectionCount();
            int infoCount = (int)(realCount * 1.5f);

            Interop.Sys.NativeTcpConnectionInformation *infos = stackalloc Interop.Sys.NativeTcpConnectionInformation[infoCount];
            while (Interop.Sys.GetActiveTcpConnectionInfos(infos, &infoCount) == -1)
            {
                var newAlloc = stackalloc Interop.Sys.NativeTcpConnectionInformation[infoCount];
                infos = newAlloc;
            }

            TcpConnectionInformation[] connectionInformations = new TcpConnectionInformation[infoCount];
            for (int i = 0; i < infoCount; i++)
            {
                Interop.Sys.NativeTcpConnectionInformation nativeInfo = infos[i];
                TcpState state = nativeInfo.State;

                byte[] localBytes = new byte[nativeInfo.LocalEndPoint.NumAddressBytes];
                fixed(byte *localBytesPtr = localBytes)
                {
                    Buffer.MemoryCopy(nativeInfo.LocalEndPoint.AddressBytes, localBytesPtr, localBytes.Length, localBytes.Length);
                }

                IPAddress  localIPAddress = new IPAddress(localBytes);
                IPEndPoint local          = new IPEndPoint(localIPAddress, (int)nativeInfo.LocalEndPoint.Port);

                IPAddress remoteIPAddress;
                if (nativeInfo.RemoteEndPoint.NumAddressBytes == 0)
                {
                    remoteIPAddress = IPAddress.Any;
                }
                else
                {
                    byte[] remoteBytes = new byte[nativeInfo.RemoteEndPoint.NumAddressBytes];
                    fixed(byte *remoteBytesPtr = remoteBytes)
                    {
                        Buffer.MemoryCopy(nativeInfo.RemoteEndPoint.AddressBytes, remoteBytesPtr, remoteBytes.Length, remoteBytes.Length);
                    }

                    remoteIPAddress = new IPAddress(remoteBytes);
                }

                IPEndPoint remote = new IPEndPoint(remoteIPAddress, (int)nativeInfo.RemoteEndPoint.Port);
                connectionInformations[i] = new SimpleTcpConnectionInformation(local, remote, state);
            }

            return(connectionInformations);
        }
Exemplo n.º 11
0
        public override TcpConnectionInformation[] GetActiveTcpConnections()
        {
            List <string[]> list = new List <string[]>();

            this.GetRows(this.TcpFile, list);
            this.GetRows(this.Tcp6File, list);
            TcpConnectionInformation[] array = new TcpConnectionInformation[list.Count];
            for (int i = 0; i < array.Length; i++)
            {
                IPEndPoint local  = this.ToEndpoint(list[i][1]);
                IPEndPoint remote = this.ToEndpoint(list[i][2]);
                TcpState   state  = (TcpState)int.Parse(list[i][3], NumberStyles.HexNumber);
                array[i] = new TcpConnectionInformationImpl(local, remote, state);
            }
            return(array);
        }
Exemplo n.º 12
0
 private string GetLocalEndPoint(System.Net.Sockets.Socket s)
 {
     System.Net.NetworkInformation.IPGlobalProperties         iPGlobalProperties   = System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties();
     System.Net.NetworkInformation.TcpConnectionInformation[] activeTcpConnections = iPGlobalProperties.GetActiveTcpConnections();
     System.Net.IPEndPoint iPEndPoint = (System.Net.IPEndPoint)s.RemoteEndPoint;
     System.Net.NetworkInformation.TcpConnectionInformation[] array = activeTcpConnections;
     for (int i = 0; i < array.Length; i++)
     {
         System.Net.NetworkInformation.TcpConnectionInformation tcpConnectionInformation = array[i];
         if (iPEndPoint.ToString() == tcpConnectionInformation.RemoteEndPoint.ToString())
         {
             return(new System.Net.IPEndPoint(tcpConnectionInformation.LocalEndPoint.Address, ((System.Net.IPEndPoint)s.LocalEndPoint).Port).ToString());
         }
     }
     return("127.0.0.1:0");
 }
Exemplo n.º 13
0
        public unsafe override TcpConnectionInformation[] GetActiveTcpConnections()
        {
            int realCount = Interop.Sys.GetEstimatedTcpConnectionCount();
            int infoCount = realCount * 2;
            Interop.Sys.NativeTcpConnectionInformation* infos = stackalloc Interop.Sys.NativeTcpConnectionInformation[infoCount];
            while (Interop.Sys.GetActiveTcpConnectionInfos(infos, &infoCount) == -1)
            {
                Interop.Sys.NativeTcpConnectionInformation* newAlloc = stackalloc Interop.Sys.NativeTcpConnectionInformation[infoCount];
                infos = newAlloc;
            }

            TcpConnectionInformation[] connectionInformations = new TcpConnectionInformation[infoCount];
            for (int i = 0; i < infoCount; i++)
            {
                Interop.Sys.NativeTcpConnectionInformation nativeInfo = infos[i];
                TcpState state = nativeInfo.State;

                byte[] localBytes = new byte[nativeInfo.LocalEndPoint.NumAddressBytes];
                fixed (byte* localBytesPtr = localBytes)
                {
                    Buffer.MemoryCopy(nativeInfo.LocalEndPoint.AddressBytes, localBytesPtr, localBytes.Length, localBytes.Length);
                }
                IPAddress localIPAddress = new IPAddress(localBytes);
                IPEndPoint local = new IPEndPoint(localIPAddress, (int)nativeInfo.LocalEndPoint.Port);

                IPAddress remoteIPAddress;
                if (nativeInfo.RemoteEndPoint.NumAddressBytes == 0)
                {
                    remoteIPAddress = IPAddress.Any;
                }
                else
                {
                    byte[] remoteBytes = new byte[nativeInfo.RemoteEndPoint.NumAddressBytes];
                    fixed (byte* remoteBytesPtr = remoteBytes)
                    {
                        Buffer.MemoryCopy(nativeInfo.RemoteEndPoint.AddressBytes, remoteBytesPtr, remoteBytes.Length, remoteBytes.Length);
                    }
                    remoteIPAddress = new IPAddress(remoteBytes);
                }

                IPEndPoint remote = new IPEndPoint(remoteIPAddress, (int)nativeInfo.RemoteEndPoint.Port);
                connectionInformations[i] = new SimpleTcpConnectionInformation(local, remote, state);
            }

            return connectionInformations;
        }
Exemplo n.º 14
0
        private unsafe TcpConnectionInformation[] GetTcpConnections(bool listeners)
        {
            int realCount = Interop.Sys.GetEstimatedTcpConnectionCount();
            int infoCount = realCount * 2;

            Interop.Sys.NativeTcpConnectionInformation[] infos = new Interop.Sys.NativeTcpConnectionInformation[infoCount];
            fixed(Interop.Sys.NativeTcpConnectionInformation *infosPtr = infos)
            {
                if (Interop.Sys.GetActiveTcpConnectionInfos(infosPtr, &infoCount) == -1)
                {
                    throw new NetworkInformationException(SR.net_PInvokeError);
                }
            }

            TcpConnectionInformation[] connectionInformations = new TcpConnectionInformation[infoCount];
            int nextResultIndex = 0;

            for (int i = 0; i < infoCount; i++)
            {
                Interop.Sys.NativeTcpConnectionInformation nativeInfo = infos[i];
                TcpState state = nativeInfo.State;

                if (listeners != (state == TcpState.Listen))
                {
                    continue;
                }

                IPAddress  localIPAddress = new IPAddress(new ReadOnlySpan <byte>(nativeInfo.LocalEndPoint.AddressBytes, checked ((int)nativeInfo.LocalEndPoint.NumAddressBytes)));
                IPEndPoint local          = new IPEndPoint(localIPAddress, (int)nativeInfo.LocalEndPoint.Port);

                IPAddress remoteIPAddress = nativeInfo.RemoteEndPoint.NumAddressBytes == 0 ?
                                            IPAddress.Any :
                                            new IPAddress(new ReadOnlySpan <byte>(nativeInfo.RemoteEndPoint.AddressBytes, checked ((int)nativeInfo.RemoteEndPoint.NumAddressBytes)));

                IPEndPoint remote = new IPEndPoint(remoteIPAddress, (int)nativeInfo.RemoteEndPoint.Port);
                connectionInformations[nextResultIndex++] = new SimpleTcpConnectionInformation(local, remote, state);
            }

            if (nextResultIndex != connectionInformations.Length)
            {
                Array.Resize(ref connectionInformations, nextResultIndex);
            }

            return(connectionInformations);
        }
Exemplo n.º 15
0
        public override TcpConnectionInformation [] GetActiveTcpConnections()
        {
            List <string []> list = new List <string []> ();

            GetRows(TcpFile, list);
            GetRows(Tcp6File, list);

            TcpConnectionInformation [] ret = new TcpConnectionInformation [list.Count];
            for (int i = 0; i < ret.Length; i++)
            {
                // sl  local_address rem_address   st tx_queue rx_queue tr tm->when retrnsmt   uid  timeout inode
                IPEndPoint local  = ToEndpoint(list [i] [1]);
                IPEndPoint remote = ToEndpoint(list [i] [2]);
                TcpState   state  = (TcpState)int.Parse(list [i] [3], NumberStyles.HexNumber);
                ret [i] = new TcpConnectionInformationImpl(local, remote, state);
            }
            return(ret);
        }
Exemplo n.º 16
0
        public override TcpConnectionInformation[] GetActiveTcpConnections()
        {
            List <Win32IPGlobalProperties.Win32_MIB_TCPROW>  list  = null;
            List <Win32IPGlobalProperties.Win32_MIB_TCP6ROW> list2 = null;

            this.FillTcpTable(out list, out list2);
            int count = list.Count;

            TcpConnectionInformation[] array = new TcpConnectionInformation[count + list2.Count];
            for (int i = 0; i < count; i++)
            {
                array[i] = list[i].TcpInfo;
            }
            for (int j = 0; j < list2.Count; j++)
            {
                array[count + j] = list2[j].TcpInfo;
            }
            return(array);
        }
Exemplo n.º 17
0
        public override TcpConnectionInformation [] GetActiveTcpConnections()
        {
            List <Win32_MIB_TCPROW>  tab4 = null;
            List <Win32_MIB_TCP6ROW> tab6 = null;

            FillTcpTable(out tab4, out tab6);
            int size4 = tab4.Count;

            TcpConnectionInformation [] ret = new TcpConnectionInformation [size4 + tab6.Count];
            for (int i = 0; i < size4; i++)
            {
                ret [i] = tab4 [i].TcpInfo;
            }
            for (int i = 0; i < tab6.Count; i++)
            {
                ret [size4 + i] = tab6 [i].TcpInfo;
            }
            return(ret);
        }
Exemplo n.º 18
0
        public override TcpConnectionInformation[] GetActiveTcpConnections()
        {
            List <Win32_MIB_TCPROW>  tab  = null;
            List <Win32_MIB_TCP6ROW> tab2 = null;

            FillTcpTable(out tab, out tab2);
            int count = tab.Count;

            TcpConnectionInformation[] array = new TcpConnectionInformation[count + tab2.Count];
            for (int i = 0; i < count; i++)
            {
                array[i] = tab[i].TcpInfo;
            }
            for (int j = 0; j < tab2.Count; j++)
            {
                array[count + j] = tab2[j].TcpInfo;
            }
            return(array);
        }
        internal static TcpConnectionInformation[] ParseActiveTcpConnectionsFromFiles(string?tcp4ConnectionsFile, string?tcp6ConnectionsFile)
        {
            string[] v4connections;
            string[] v6connections;

            if (tcp4ConnectionsFile != null)
            {
                string tcp4FileContents = ReadAllText(tcp4ConnectionsFile);
                v4connections = tcp4FileContents.Split(s_newLineSeparator, StringSplitOptions.RemoveEmptyEntries);
            }
            else
            {
                v4connections = Array.Empty <string>();
            }

            if (tcp6ConnectionsFile != null)
            {
                string tcp6FileContents = ReadAllText(tcp6ConnectionsFile);
                v6connections = tcp6FileContents.Split(s_newLineSeparator, StringSplitOptions.RemoveEmptyEntries);
            }
            else
            {
                v6connections = Array.Empty <string>();
            }

            // First line is header in each file. On WSL, this file may be empty.
            int count = 0;

            if (v4connections.Length > 0)
            {
                count += v4connections.Length - 1;
            }

            if (v6connections.Length > 0)
            {
                count += v6connections.Length - 1;
            }

            // First line is header in each file.
            TcpConnectionInformation[] connections = new TcpConnectionInformation[count];
            int index = 0;
            int skip  = 0;

            // TCP Connections
            for (int i = 1; i < v4connections.Length; i++) // Skip first line header.
            {
                string line = v4connections[i];
                connections[index] = ParseTcpConnectionInformationFromLine(line);
                if (connections[index].State == TcpState.Listen)
                {
                    skip++;
                }
                else
                {
                    index++;
                }
            }

            // TCP6 Connections
            for (int i = 1; i < v6connections.Length; i++) // Skip first line header.
            {
                string line = v6connections[i];
                connections[index] = ParseTcpConnectionInformationFromLine(line);
                if (connections[index].State == TcpState.Listen)
                {
                    skip++;
                }
                else
                {
                    index++;
                }
            }

            if (skip != 0)
            {
                Array.Resize(ref connections, connections.Length - skip);
            }

            return(connections);
        }
Exemplo n.º 20
0
		public override TcpConnectionInformation [] GetActiveTcpConnections ()
		{
			List<Win32_MIB_TCPROW> tab4 = null;
			List<Win32_MIB_TCP6ROW> tab6 = null;
			FillTcpTable (out tab4, out tab6);
			int size4 = tab4.Count;

			TcpConnectionInformation [] ret = new TcpConnectionInformation [size4 + tab6.Count];
			for (int i = 0; i < size4; i++)
				ret [i] = tab4 [i].TcpInfo;
			for (int i = 0; i < tab6.Count; i++)
				ret [size4 + i] = tab6 [i].TcpInfo;
			return ret;
		}
Exemplo n.º 21
0
		public override TcpConnectionInformation [] GetActiveTcpConnections ()
		{
			List<string []> list = new List<string []> ();
			GetRows (TcpFile, list);
			GetRows (Tcp6File, list);

			TcpConnectionInformation [] ret = new TcpConnectionInformation [list.Count];
			for (int i = 0; i < ret.Length; i++) {
				// sl  local_address rem_address   st tx_queue rx_queue tr tm->when retrnsmt   uid  timeout inode
				IPEndPoint local = ToEndpoint (list [i] [1]);
				IPEndPoint remote = ToEndpoint (list [i] [2]);
				TcpState state = (TcpState) int.Parse (list [i] [3], NumberStyles.HexNumber);
				ret [i] = new TcpConnectionInformationImpl (local, remote, state);
			}
			return ret;
		}
        internal static TcpConnectionInformation[] ParseActiveTcpConnectionsFromFiles(string tcp4ConnectionsFile, string tcp6ConnectionsFile)
        {
            if (!File.Exists(tcp4ConnectionsFile) || !File.Exists(tcp6ConnectionsFile))
            {
                throw new PlatformNotSupportedException(SR.net_InformationUnavailableOnPlatform);
            }

            string tcp4FileContents = File.ReadAllText(tcp4ConnectionsFile);

            string[] v4connections = tcp4FileContents.Split(s_newLineSeparator, StringSplitOptions.RemoveEmptyEntries);

            string tcp6FileContents = File.ReadAllText(tcp6ConnectionsFile);

            string[] v6connections = tcp6FileContents.Split(s_newLineSeparator, StringSplitOptions.RemoveEmptyEntries);

            // First line is header in each file. On WSL, this file may be empty.
            int count = 0;

            if (v4connections.Length > 0)
            {
                count += v4connections.Length - 1;
            }

            if (v6connections.Length > 0)
            {
                count += v6connections.Length - 1;
            }

            // First line is header in each file.
            TcpConnectionInformation[] connections = new TcpConnectionInformation[count];
            int index = 0;
            int skip  = 0;

            // TCP Connections
            for (int i = 1; i < v4connections.Length; i++) // Skip first line header.
            {
                string line = v4connections[i];
                connections[index] = ParseTcpConnectionInformationFromLine(line);
                if (connections[index].State == TcpState.Listen)
                {
                    skip++;
                }
                else
                {
                    index++;
                }
            }

            // TCP6 Connections
            for (int i = 1; i < v6connections.Length; i++) // Skip first line header.
            {
                string line = v6connections[i];
                connections[index] = ParseTcpConnectionInformationFromLine(line);
                if (connections[index].State == TcpState.Listen)
                {
                    skip++;
                }
                else
                {
                    index++;
                }
            }

            if (skip != 0)
            {
                Array.Resize(ref connections, connections.Length - skip);
            }

            return(connections);
        }
        internal static IPEndPoint[] ParseActiveTcpListenersFromFiles(string tcp4ConnectionsFile, string tcp6ConnectionsFile)
        {
            string tcp4FileContents = ReadAllText(tcp4ConnectionsFile);

            string[] v4connections = tcp4FileContents.Split(s_newLineSeparator, StringSplitOptions.RemoveEmptyEntries);

            string tcp6FileContents = ReadAllText(tcp6ConnectionsFile);

            string[] v6connections = tcp6FileContents.Split(s_newLineSeparator, StringSplitOptions.RemoveEmptyEntries);

            // First line is header in each file. On WSL, this file may be empty.
            int count = 0;

            if (v4connections.Length > 0)
            {
                count += v4connections.Length - 1;
            }

            if (v6connections.Length > 0)
            {
                count += v6connections.Length - 1;
            }

            // First line is header in each file.
            IPEndPoint[] endPoints = new IPEndPoint[count];
            int          index     = 0;
            int          skip      = 0;

            // TCP Connections
            for (int i = 1; i < v4connections.Length; i++) // Skip first line header.
            {
                TcpConnectionInformation ti = ParseTcpConnectionInformationFromLine(v4connections[i]);
                if (ti.State == TcpState.Listen)
                {
                    endPoints[index] = ti.LocalEndPoint;
                    index++;
                }
                else
                {
                    skip++;
                }
            }

            // TCP6 Connections
            for (int i = 1; i < v6connections.Length; i++) // Skip first line header.
            {
                TcpConnectionInformation ti = ParseTcpConnectionInformationFromLine(v6connections[i]);
                if (ti.State == TcpState.Listen)
                {
                    endPoints[index] = ti.LocalEndPoint;
                    index++;
                }
                else
                {
                    skip++;
                }
            }

            if (skip != 0)
            {
                Array.Resize(ref endPoints, endPoints.Length - skip);
            }

            return(endPoints);
        }
Exemplo n.º 24
0
 public bool PlayerInList(TcpConnectionInformation info)
 {
     foreach (AccessListEntry e in accessList)
     {
         if (e.Matches(info.RemoteEndPoint.Address))
         {
             return true;
         }
     }
     return false;
 }