示例#1
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);
        }
示例#2
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);
        }
        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;
        }
        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);
        }