Ejemplo n.º 1
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="tablePtr">A pointer to the unmanaged memory where the table details reside.</param>
        /// <param name="tableType">The type of table to Get (TCP/UDP).</param>
        /// <returns>An array of MIB_UDPROW_OWNER_PID or MIB_TCPROW_OWNER_PID depending on the tableType parameter.</returns>
        public static dynamic GetExtendedTable(out IntPtr tablePtr, TableType tableType = TableType.TCP)
        {
            //Create an empty delegate for the TCP, or UDP, query.
            GetTableMethod GetTable;
            dynamic tabelRowArray;
            dynamic tableInfo;

            //If the tableType is UDP, initialize the dynamic values and delegate to UDP values.
            if (tableType == TableType.UDP)
            {
                GetTable = new GetTableMethod(ConnectionTables.GetExtendedUdpTable);
                tableInfo = new Udpmib.MIB_UDPTABLE_OWNER_PID();
                tabelRowArray = new Udpmib.MIB_UDPROW_OWNER_PID();
            }
            else
            {
                GetTable = new GetTableMethod(ConnectionTables.GetExtendedTcpTable);
                tableInfo = new Tcpmib.MIB_TCPTABLE_OWNER_PID();
                tabelRowArray = new Tcpmib.MIB_TCPROW_OWNER_PID();
            }

            WinError.SeverityCode status = GetTable(out tablePtr);
            if (status != WinError.SeverityCode.NO_ERROR)
                throw new Exception($"Failed on GetExtendedTable ({tableType}). Error Code: {status}.");

            //Marshals the table pointer to either a TCP or UDP table struct.
            tableInfo = Convert.ChangeType(Marshal.PtrToStructure(tablePtr, (Type)tableInfo.GetType()), tableInfo.GetType());

            //Initialize a new array of either a TCP or UDP row array.
            tabelRowArray = Array.CreateInstance(tabelRowArray.GetType(), tableInfo.dwNumEntries);

            //Gets a pointer to the first row in the table.
            IntPtr rowPtr = (IntPtr)(tablePtr + Marshal.SizeOf(tableInfo.dwNumEntries));


            for (int i = 0; i < tabelRowArray.Length; i++)
            {
                //Marshals the index of the table row array to the content of row struct.
                tabelRowArray[i] = Convert.ChangeType(Marshal.PtrToStructure(rowPtr, (Type)tabelRowArray[i].GetType()), tabelRowArray[i].GetType());

                //Increments the pointer to the row by the size of the struct.
                rowPtr = (IntPtr)((long)rowPtr + Marshal.SizeOf(tabelRowArray[i]));
            }
            return tabelRowArray;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="tcpTableRow">The TCP Row which contains the connection to close.</param>
        /// <returns>An NTSTATUS code.</returns>
        public static WinError.SeverityCode CloseConnection(Tcpmib.MIB_TCPROW_OWNER_PID tcpTableRow)
        {
            //Sets the state value to delete.
            tcpTableRow.dwState = Tcpmib.TCPState.MIB_TCP_STATE_DELETE_TCB;

            //Allocate a region for the row details to reside.
            IntPtr tcpRowPtr = Marshal.AllocHGlobal(Marshal.SizeOf(tcpTableRow));

            //Marshal the array into memory.
            Marshal.StructureToPtr(tcpTableRow, tcpRowPtr, false);

            //Overwrite the connection data.
            WinError.SeverityCode ntStatus = Iphlpapi.SetTcpEntry(tcpRowPtr);

            //Free the memory region.
            Marshal.FreeHGlobal(tcpRowPtr);

            
            return ntStatus;
        }