Пример #1
0
        // New IPv6 version
        public void GetTcp6Connections()
        {
            int AF_INET  = (int)AddressFamily.InterNetworkV6;   // AF_INET6
            int buffSize = 20000;

            byte[] buffer = new byte[buffSize];
            int    res    = IPHlpAPI32Wrapper.GetExtendedTcpTable(buffer, out buffSize, true, AF_INET, TCP_TABLE_CLASS.TCP_TABLE_OWNER_PID_ALL, 0);

            if (res != Utils.NO_ERROR) //If there is no enough memory to execute function
            {
                buffer = new byte[buffSize];
                res    = IPHlpAPI32Wrapper.GetExtendedTcpTable(buffer, out buffSize, true, AF_INET, TCP_TABLE_CLASS.TCP_TABLE_OWNER_PID_ALL, 0);
                if (res != Utils.NO_ERROR)
                {
                    return;
                }
            }
            int nOffset = 0;
            // number of entries in the table
            int NumEntries = Convert.ToInt32(buffer[nOffset]);

            nOffset += 4;
            for (int i = 0; i < NumEntries; i++)
            {
                TCPUDPConnection row = new TCPUDPConnection(this);
                row.Protocol      = Protocol.TCP;
                row.AddressFamily = AddressFamily.InterNetworkV6;

                // Read in local address information
                row.Local = Utils.BufferToIPv6EndPoint(buffer, ref nOffset);

                // Read in remote address information
                row.Remote = Utils.BufferToIPv6EndPoint(buffer, ref nOffset);

                // State
                int state = BitConverter.ToInt32(buffer, nOffset);
                row.iState = state;
                nOffset   += 4;

                // PID
                row.PID  = BitConverter.ToInt32(buffer, nOffset);
                nOffset += 4;

                // Add row to list of connections
                Add(row);
            }
        }
Пример #2
0
        public static List <TCPUDPConnection> GetTcpConnections()
        {
            int AF_INET  = 2;   // IP_v4
            int buffSize = 20000;

            byte[] buffer = new byte[buffSize];
            int    res    = IPHlpAPI32Wrapper.GetExtendedTcpTable(buffer, out buffSize, true, AF_INET, TCP_TABLE_CLASS.TCP_TABLE_OWNER_PID_ALL, 0);

            if (res != Utils.NO_ERROR) //If there is no enouth memory to execute function
            {
                buffer = new byte[buffSize];
                res    = IPHlpAPI32Wrapper.GetExtendedTcpTable(buffer, out buffSize, true, AF_INET, TCP_TABLE_CLASS.TCP_TABLE_OWNER_PID_ALL, 0);
                if (res != Utils.NO_ERROR)
                {
                    throw new Exception("Couldn't call win API, because we couldn't build a large enough buffer");
                }
            }
            var ourReturn = new List <TCPUDPConnection>();
            int nOffset   = 0;
            // number of entry in the
            int NumEntries = Convert.ToInt32(buffer[nOffset]);

            Console.WriteLine("there are {0} entries", NumEntries);
            nOffset += 4;
            for (int i = 0; i < NumEntries; i++)
            {
                // state
                int st  = Convert.ToInt32(buffer[nOffset]);
                var row = new TCPUDPConnection(null);
                row.iState   = st;
                nOffset     += 4;
                row.Protocol = Protocol.TCP;
                row.Local    = Utils.BufferToIPEndPoint(buffer, ref nOffset, false);
                row.Remote   = Utils.BufferToIPEndPoint(buffer, ref nOffset, true);
                row.PID      = Utils.BufferToInt(buffer, ref nOffset);
                ourReturn.Add(row);
            }
            return(ourReturn);
        }
Пример #3
0
        public void GetTcpConnections()
        {
            int AF_INET  = 2;   // IP_v4
            int buffSize = 20000;

            byte[] buffer = new byte[buffSize];
            int    res    = IPHlpAPI32Wrapper.GetExtendedTcpTable(buffer, out buffSize, true, AF_INET, TCP_TABLE_CLASS.TCP_TABLE_OWNER_PID_ALL, 0);

            if (res != Utils.NO_ERROR) //If there is no enouth memory to execute function
            {
                buffer = new byte[buffSize];
                res    = IPHlpAPI32Wrapper.GetExtendedTcpTable(buffer, out buffSize, true, AF_INET, TCP_TABLE_CLASS.TCP_TABLE_OWNER_PID_ALL, 0);
                if (res != Utils.NO_ERROR)
                {
                    return;
                }
            }
            int nOffset = 0;
            // number of entry in the
            int NumEntries = Convert.ToInt32(buffer[nOffset]);

            Console.WriteLine("there are {0} entries", NumEntries);
            nOffset += 4;
            for (int i = 0; i < NumEntries; i++)
            {
                // state
                int st            = Convert.ToInt32(buffer[nOffset]);
                var nextPosOffset = nOffset + 4;
                var row           = new TCPUDPConnection(this)
                {
                    iState   = st,
                    Protocol = Protocol.TCP,
                    Local    = Utils.BufferToIPEndPoint(buffer, ref nextPosOffset, false),
                    Remote   = Utils.BufferToIPEndPoint(buffer, ref nextPosOffset, true),
                    PID      = Utils.BufferToInt(buffer, ref nextPosOffset)
                };
                this.Add(row);
            }
        }
Пример #4
0
        public void GetTcpConnections()
        {
            int AF_INET  = 2;   // IP_v4
            int buffSize = 20000;

            byte[] buffer = new byte[buffSize];
            int    res    = IPHlpAPI32Wrapper.GetExtendedTcpTable(buffer, out buffSize, true, AF_INET, TCP_TABLE_CLASS.TCP_TABLE_OWNER_PID_ALL, 0);

            if (res != Utils.NO_ERROR) //If there is no enouth memory to execute function
            {
                buffer = new byte[buffSize];
                res    = IPHlpAPI32Wrapper.GetExtendedTcpTable(buffer, out buffSize, true, AF_INET, TCP_TABLE_CLASS.TCP_TABLE_OWNER_PID_ALL, 0);
                if (res != Utils.NO_ERROR)
                {
                    return;
                }
            }
            int nOffset = 0;
            // number of entry in the
            int NumEntries = Convert.ToInt32(buffer[nOffset]);

            nOffset += 4;
            for (int i = 0; i < NumEntries; i++)
            {
                TCPUDPConnection row = new TCPUDPConnection(this);
                // state
                int st = Convert.ToInt32(buffer[nOffset]);
                // state  by ID
                row.iState        = st;
                nOffset          += 4;
                row.Protocol      = Protocol.TCP;
                row.AddressFamily = AddressFamily.InterNetwork;
                row.Local         = Utils.BufferToIPEndPoint(buffer, ref nOffset, false);
                row.Remote        = Utils.BufferToIPEndPoint(buffer, ref nOffset, true);
                row.PID           = Utils.BufferToInt(buffer, ref nOffset);
                this.Add(row);
            }
        }