Beispiel #1
0
        internal static List <ITcpConnectionInfo> GetTcp6Table()
        {
            int tableSize = 0;

            var result = GetTcp6Table2(IntPtr.Zero, ref tableSize, false);

            IntPtr tcpTableRecordsPtr = IntPtr.Zero;

            List <ITcpConnectionInfo> fTable = new List <ITcpConnectionInfo>();

            try
            {
                tcpTableRecordsPtr = Marshal.AllocHGlobal(tableSize);

                result = GetTcp6Table2(tcpTableRecordsPtr, ref tableSize, false);

                if (result != 0)
                {
                    return(fTable);
                }

                var table = (MIB_TCP6TABLE2)Marshal.PtrToStructure(tcpTableRecordsPtr, typeof(MIB_TCP6TABLE2));

                IntPtr tableRowPtr = (IntPtr)((long)tcpTableRecordsPtr + Marshal.SizeOf(table.dwNumEntries));

                for (int i = 0; i < table.dwNumEntries; ++i)
                {
                    MIB_TCP6ROW2 tcpRow = (MIB_TCP6ROW2)Marshal.PtrToStructure(tableRowPtr, typeof(MIB_TCP6ROW2));

                    fTable.Add(new Tcp6ConnectionInfo(tcpRow));

                    tableRowPtr = (IntPtr)((long)tableRowPtr + Marshal.SizeOf(tcpRow));
                }

                return(fTable);
            }
            catch (OutOfMemoryException me)
            {
                LoggerProxy.Default.Error(me);
            }
            catch (Exception e)
            {
                LoggerProxy.Default.Error(e);
            }
            finally
            {
                Marshal.FreeHGlobal(tcpTableRecordsPtr);
            }

            return(fTable);
        }
Beispiel #2
0
        public Tcp6ConnectionInfo(MIB_TCP6ROW2 tcpRow)
        {
            // We mask the ports in this struct because according to the documentation, the upper
            // bits can be populated arbitrarily, aka undefined state.
            LocalPort = (ushort)(tcpRow.dwLocalPort & 0xFFFF);

            RemotePort = (ushort)(tcpRow.dwRemotePort & 0xFFFF);

            LocalAddress = new IPAddress(tcpRow.LocalAddr.u.Byte);

            RemoteAddress = new IPAddress(tcpRow.RemoteAddr.u.Byte);

            State = tcpRow.State;

            OffloadState = tcpRow.dwOffloadState;

            LocalScopeId = tcpRow.dwLocalScopeId;

            OwnerPid = tcpRow.dwOwningPid;
        }