Exemplo n.º 1
0
        static MIB_TCPROW_OWNER_PID[] GetAllTcpConnections()
        {
            MIB_TCPROW_OWNER_PID[] rows;
            int  AF_INET   = 2; // IP_v4
            uint buff_size = 0;

            uint   ret       = SafeNativeMethods.GetExtendedTcpTable(IntPtr.Zero, ref buff_size, true, AF_INET, SafeNativeMethods.TCP_TABLE_CLASS.TCP_TABLE_OWNER_PID_ALL);
            IntPtr buffTable = Marshal.AllocHGlobal((int)buff_size);

            try
            {
                ret = SafeNativeMethods.GetExtendedTcpTable(buffTable, ref buff_size, true, AF_INET, SafeNativeMethods.TCP_TABLE_CLASS.TCP_TABLE_OWNER_PID_ALL);

                if (ret != 0)
                {
                    return(null);
                }

                var    tab     = (MIB_TCPTABLE_OWNER_PID)Marshal.PtrToStructure(buffTable, typeof(MIB_TCPTABLE_OWNER_PID));
                IntPtr row_ptr = (IntPtr)((long)buffTable + Marshal.SizeOf(tab.dwNumEntries));
                rows = new MIB_TCPROW_OWNER_PID[tab.dwNumEntries];

                for (int i = 0; i < tab.dwNumEntries; i++)
                {
                    var row = (MIB_TCPROW_OWNER_PID)Marshal.PtrToStructure(row_ptr, typeof(MIB_TCPROW_OWNER_PID));
                    rows[i] = row;
                    row_ptr = (IntPtr)((long)row_ptr + Marshal.SizeOf(row));
                }
            }
            finally
            {
                Marshal.FreeHGlobal(buffTable);
            }
            return(rows);
        }
Exemplo n.º 2
0
            public static List <TcpProcessRecord> GetAllTcpConnections()
            {
                int bufferSize = 0;
                List <TcpProcessRecord> tcpTableRecords = new List <TcpProcessRecord>();

                // Getting the size of TCP table, that is returned in 'bufferSize' variable.
                uint result = GetExtendedTcpTable(IntPtr.Zero, ref bufferSize, true, AF_INET,
                                                  TcpTableClass.TCP_TABLE_OWNER_PID_ALL);

                // Allocating memory from the unmanaged memory of the process by using the
                // specified number of bytes in 'bufferSize' variable.
                IntPtr tcpTableRecordsPtr = Marshal.AllocHGlobal(bufferSize);

                try
                {
                    // The size of the table returned in 'bufferSize' variable in previous
                    // call must be used in this subsequent call to 'GetExtendedTcpTable'
                    // function in order to successfully retrieve the table.
                    result = GetExtendedTcpTable(tcpTableRecordsPtr, ref bufferSize, true,
                                                 AF_INET, TcpTableClass.TCP_TABLE_OWNER_PID_ALL);

                    // Non-zero value represent the function 'GetExtendedTcpTable' failed,
                    // hence empty list is returned to the caller function.
                    if (result != 0)
                    {
                        return(new List <TcpProcessRecord>());
                    }

                    // Marshals data from an unmanaged block of memory to a newly allocated
                    // managed object 'tcpRecordsTable' of type 'MIB_TCPTABLE_OWNER_PID'
                    // to get number of entries of the specified TCP table structure.
                    MIB_TCPTABLE_OWNER_PID tcpRecordsTable = (MIB_TCPTABLE_OWNER_PID)
                                                             Marshal.PtrToStructure(tcpTableRecordsPtr,
                                                                                    typeof(MIB_TCPTABLE_OWNER_PID));
                    IntPtr tableRowPtr = (IntPtr)((long)tcpTableRecordsPtr +
                                                  Marshal.SizeOf(tcpRecordsTable.dwNumEntries));

                    // Reading and parsing the TCP records one by one from the table and
                    // storing them in a list of 'TcpProcessRecord' structure type objects.
                    for (int row = 0; row < tcpRecordsTable.dwNumEntries; row++)
                    {
                        MIB_TCPROW_OWNER_PID tcpRow = (MIB_TCPROW_OWNER_PID)Marshal.
                                                      PtrToStructure(tableRowPtr, typeof(MIB_TCPROW_OWNER_PID));
                        tcpTableRecords.Add(new TcpProcessRecord(
                                                new IPAddress(tcpRow.localAddr),
                                                new IPAddress(tcpRow.remoteAddr),
                                                BitConverter.ToUInt16(new byte[2] {
                            tcpRow.localPort[1],
                            tcpRow.localPort[0]
                        }, 0),
                                                BitConverter.ToUInt16(new byte[2] {
                            tcpRow.remotePort[1],
                            tcpRow.remotePort[0]
                        }, 0),
                                                tcpRow.owningPid, tcpRow.state));
                        tableRowPtr = (IntPtr)((long)tableRowPtr + Marshal.SizeOf(tcpRow));
                    }
                }
                catch (OutOfMemoryException outOfMemoryException)
                {
                }
                catch (Exception exception)
                {
                }
                finally
                {
                    Marshal.FreeHGlobal(tcpTableRecordsPtr);
                }
                return(tcpTableRecords != null?tcpTableRecords.Distinct()
                       .ToList <TcpProcessRecord>() : new List <TcpProcessRecord>());
            }