Exemplo n.º 1
0
        static unsafe int Main(string[] args)
        {
            uint status;

            // Print the table of connections
            // Call for the size
            uint size = 0;

            status = IphlpApi.GetTcpTable2(IntPtr.Zero, ref size, order: true);

            fixed(byte *buffer = new byte[size])
            {
                var bufferIntPtr = (IntPtr)buffer;

                status = IphlpApi.GetTcpTable2(bufferIntPtr, ref size, order: true);

                switch (status)
                {
                case StatusCode.NoError:
                    var numConnections = *(uint *)bufferIntPtr;
                    Console.WriteLine($"Found {numConnections} connections:");

                    bufferIntPtr += sizeof(uint);
                    for (int i = 0; i < numConnections; i++)
                    {
                        var row = Marshal.PtrToStructure <TcpRow2>(bufferIntPtr);
                        Console.WriteLine(FormatTcpConnectionInfo(row.localAddr, row.localPort, row.remoteAddr, row.remotePort, row.state.ToString()));
                        bufferIntPtr += sizeof(TcpRow2);
                    }
                    break;

                case StatusCode.ErrorInsufficientBuffer:
                    Console.Error.WriteLine($"Insufficient buffer: required {size} bytes.");
                    break;

                case StatusCode.ErrorInvalidParameter:
                    Console.Error.WriteLine("Could not write to the buffer. Is it null?");
                    break;

                case StatusCode.ErrorNotSupported:
                    Console.Error.WriteLine("Not supported on this OS.");
                    break;

                default:
                    Console.Error.WriteLine("An unknown error occurred.");
                    break;
                }
            }

            if (status != StatusCode.NoError)
            {
                return(1);
            }

            // Print the table of bound ports
            var boundPortTable = new HeapAllocHandle();

            status = IphlpApi.InternalGetBoundTcpEndpointTable(ref boundPortTable, HeapAllocHandle.DangerousGetProcessHeap(), flags: 0);

            if (status == StatusCode.NoError)
            {
                var numConnections = *(uint *)boundPortTable.DangerousGetHandle();
                Console.WriteLine($"Found {numConnections} bound ports:");

                // We need to keep a pointer to the start of the memory block so we can free it at the end
                var currentRow = boundPortTable.DangerousGetHandle();
                currentRow += sizeof(uint);
                for (int i = 0; i < numConnections; i++)
                {
                    var row = Marshal.PtrToStructure <TcpRow2>(currentRow);
                    Console.WriteLine(FormatTcpConnectionInfo(row.localAddr, row.localPort, row.remoteAddr, row.remotePort, "BOUND"));
                    currentRow += sizeof(TcpRow2);
                }
            }
            else
            {
                Console.Error.WriteLine("An unknown error occurred.");
            }

            if (status != StatusCode.NoError)
            {
                return(1);
            }

            return(0);
        }
Exemplo n.º 2
0
 public static extern uint InternalGetBoundTcpEndpointTable(ref HeapAllocHandle tcpTable, IntPtr heapHandle, uint flags);