internal SystemTcpConnectionInformation(MibTcpRow row)
 {
     this.state = row.state;
     int port = (row.localPort1 << 8) | row.localPort2;
     int num2 = (this.state == TcpState.Listen) ? 0 : ((row.remotePort1 << 8) | row.remotePort2);
     this.localEndPoint = new IPEndPoint((long) row.localAddr, port);
     this.remoteEndPoint = new IPEndPoint((long) row.remoteAddr, num2);
 }
        internal SystemTcpConnectionInformation(MibTcpRow row)
        {
            this.state = row.state;
            int port = (row.localPort1 << 8) | row.localPort2;
            int num2 = (this.state == TcpState.Listen) ? 0 : ((row.remotePort1 << 8) | row.remotePort2);

            this.localEndPoint  = new IPEndPoint((long)row.localAddr, port);
            this.remoteEndPoint = new IPEndPoint((long)row.remoteAddr, num2);
        }
Пример #3
0
        internal SystemTcpConnectionInformation(MibTcpRow row) {
            state = row.state;

            //port is returned in Big-Endian - most significant bit on left
            //unfortunately, its done at the word level and not the dword level.

            int localPort = row.localPort1<<8|row.localPort2;
            int remotePort = ((state == TcpState.Listen)?0:row.remotePort1<<8|row.remotePort2);

            localEndPoint = new IPEndPoint(row.localAddr,(int)localPort);
            remoteEndPoint= new IPEndPoint(row.remoteAddr,(int)remotePort);
        }
Пример #4
0
        internal SystemTcpConnectionInformation(MibTcpRow row)
        {
            state = row.state;

            //port is returned in Big-Endian - most significant bit on left
            //unfortunately, its done at the word level and not the dword level.

            int localPort  = row.localPort1 << 8 | row.localPort2;
            int remotePort = ((state == TcpState.Listen)?0:row.remotePort1 << 8 | row.remotePort2);

            localEndPoint  = new IPEndPoint(row.localAddr, (int)localPort);
            remoteEndPoint = new IPEndPoint(row.remoteAddr, (int)remotePort);
        }
        /// <summary>
        /// Gets the active tcp connections. Uses the native GetTcpTable api.</summary>
        private List <SystemTcpConnectionInformation> GetAllTcpConnections()
        {
            uint          size   = 0;
            uint          result = 0;
            SafeLocalFree buffer = null;
            List <SystemTcpConnectionInformation> tcpConnections = new List <SystemTcpConnectionInformation>();

            // Check if it supports IPv4 for IPv6 only modes.
            if (Socket.OSSupportsIPv4)
            {
                // Get the size of buffer needed
                result = UnsafeNetInfoNativeMethods.GetTcpTable(SafeLocalFree.Zero, ref size, true);

                while (result == IpHelperErrors.ErrorInsufficientBuffer)
                {
                    try {
                        //allocate the buffer and get the tcptable
                        buffer = SafeLocalFree.LocalAlloc((int)size);
                        result = UnsafeNetInfoNativeMethods.GetTcpTable(buffer, ref size, true);

                        if (result == IpHelperErrors.Success)
                        {
                            //the table info just gives us the number of rows.
                            IntPtr      newPtr       = buffer.DangerousGetHandle();
                            MibTcpTable tcpTableInfo = (MibTcpTable)Marshal.PtrToStructure(newPtr, typeof(MibTcpTable));

                            if (tcpTableInfo.numberOfEntries > 0)
                            {
                                //we need to skip over the tableinfo to get the inline rows
                                newPtr = (IntPtr)((long)newPtr + Marshal.SizeOf(tcpTableInfo.numberOfEntries));

                                for (int i = 0; i < tcpTableInfo.numberOfEntries; i++)
                                {
                                    MibTcpRow tcpRow = (MibTcpRow)Marshal.PtrToStructure(newPtr, typeof(MibTcpRow));
                                    tcpConnections.Add(new SystemTcpConnectionInformation(tcpRow));

                                    //we increment the pointer to the next row
                                    newPtr = (IntPtr)((long)newPtr + Marshal.SizeOf(tcpRow));
                                }
                            }
                        }
                    }
                    finally {
                        if (buffer != null)
                        {
                            buffer.Close();
                        }
                    }
                }

                // if we don't have any ipv4 interfaces detected, just continue
                if (result != IpHelperErrors.Success && result != IpHelperErrors.ErrorNoData)
                {
                    throw new NetworkInformationException((int)result);
                }
            }

            if (Socket.OSSupportsIPv6)
            {
                // IPv6 tcp connections
                // Get the size of buffer needed
                size   = 0;
                result = UnsafeNetInfoNativeMethods.GetExtendedTcpTable(SafeLocalFree.Zero, ref size, true,
                                                                        (uint)AddressFamily.InterNetworkV6,
                                                                        TcpTableClass.TcpTableOwnerPidAll, 0);

                while (result == IpHelperErrors.ErrorInsufficientBuffer)
                {
                    try {
                        // Allocate the buffer and get the tcptable
                        buffer = SafeLocalFree.LocalAlloc((int)size);
                        result = UnsafeNetInfoNativeMethods.GetExtendedTcpTable(buffer, ref size, true,
                                                                                (uint)AddressFamily.InterNetworkV6,
                                                                                TcpTableClass.TcpTableOwnerPidAll, 0);
                        if (result == IpHelperErrors.Success)
                        {
                            // The table info just gives us the number of rows.
                            IntPtr newPtr = buffer.DangerousGetHandle();

                            MibTcp6TableOwnerPid tcpTable6OwnerPid
                                = (MibTcp6TableOwnerPid)Marshal.PtrToStructure(newPtr, typeof(MibTcp6TableOwnerPid));

                            if (tcpTable6OwnerPid.numberOfEntries > 0)
                            {
                                // We need to skip over the tableinfo to get the inline rows
                                newPtr = (IntPtr)((long)newPtr + Marshal.SizeOf(tcpTable6OwnerPid.numberOfEntries));

                                for (int i = 0; i < tcpTable6OwnerPid.numberOfEntries; i++)
                                {
                                    MibTcp6RowOwnerPid tcp6RowOwnerPid
                                        = (MibTcp6RowOwnerPid)Marshal.PtrToStructure(newPtr,
                                                                                     typeof(MibTcp6RowOwnerPid));
                                    tcpConnections.Add(new SystemTcpConnectionInformation(tcp6RowOwnerPid));

                                    // We increment the pointer to the next row
                                    newPtr = (IntPtr)((long)newPtr + Marshal.SizeOf(tcp6RowOwnerPid));
                                }
                            }
                        }
                    }
                    finally {
                        if (buffer != null)
                        {
                            buffer.Close();
                        }
                    }
                }

                // If we don't have any ipv6 interfaces detected, just continue
                if (result != IpHelperErrors.Success && result != IpHelperErrors.ErrorNoData)
                {
                    throw new NetworkInformationException((int)result);
                }
            }

            return(tcpConnections);
        }
        private List <SystemTcpConnectionInformation> GetAllTcpConnections()
        {
            uint          dwOutBufLen = 0;
            uint          num2        = 0;
            SafeLocalFree pTcpTable   = null;
            List <SystemTcpConnectionInformation> list = new List <SystemTcpConnectionInformation>();

            if (Socket.OSSupportsIPv4)
            {
                num2 = UnsafeNetInfoNativeMethods.GetTcpTable(SafeLocalFree.Zero, ref dwOutBufLen, true);
                while (num2 == 0x7a)
                {
                    try
                    {
                        pTcpTable = SafeLocalFree.LocalAlloc((int)dwOutBufLen);
                        num2      = UnsafeNetInfoNativeMethods.GetTcpTable(pTcpTable, ref dwOutBufLen, true);
                        if (num2 == 0)
                        {
                            IntPtr      handle = pTcpTable.DangerousGetHandle();
                            MibTcpTable table  = (MibTcpTable)Marshal.PtrToStructure(handle, typeof(MibTcpTable));
                            if (table.numberOfEntries > 0)
                            {
                                handle = (IntPtr)(((long)handle) + Marshal.SizeOf(table.numberOfEntries));
                                for (int i = 0; i < table.numberOfEntries; i++)
                                {
                                    MibTcpRow row = (MibTcpRow)Marshal.PtrToStructure(handle, typeof(MibTcpRow));
                                    list.Add(new SystemTcpConnectionInformation(row));
                                    handle = (IntPtr)(((long)handle) + Marshal.SizeOf(row));
                                }
                            }
                        }
                        continue;
                    }
                    finally
                    {
                        if (pTcpTable != null)
                        {
                            pTcpTable.Close();
                        }
                    }
                }
                if ((num2 != 0) && (num2 != 0xe8))
                {
                    throw new NetworkInformationException((int)num2);
                }
            }
            if (Socket.OSSupportsIPv6)
            {
                dwOutBufLen = 0;
                num2        = UnsafeNetInfoNativeMethods.GetExtendedTcpTable(SafeLocalFree.Zero, ref dwOutBufLen, true, 0x17, TcpTableClass.TcpTableOwnerPidAll, 0);
                while (num2 == 0x7a)
                {
                    try
                    {
                        pTcpTable = SafeLocalFree.LocalAlloc((int)dwOutBufLen);
                        num2      = UnsafeNetInfoNativeMethods.GetExtendedTcpTable(pTcpTable, ref dwOutBufLen, true, 0x17, TcpTableClass.TcpTableOwnerPidAll, 0);
                        if (num2 == 0)
                        {
                            IntPtr ptr = pTcpTable.DangerousGetHandle();
                            MibTcp6TableOwnerPid pid = (MibTcp6TableOwnerPid)Marshal.PtrToStructure(ptr, typeof(MibTcp6TableOwnerPid));
                            if (pid.numberOfEntries > 0)
                            {
                                ptr = (IntPtr)(((long)ptr) + Marshal.SizeOf(pid.numberOfEntries));
                                for (int j = 0; j < pid.numberOfEntries; j++)
                                {
                                    MibTcp6RowOwnerPid pid2 = (MibTcp6RowOwnerPid)Marshal.PtrToStructure(ptr, typeof(MibTcp6RowOwnerPid));
                                    list.Add(new SystemTcpConnectionInformation(pid2));
                                    ptr = (IntPtr)(((long)ptr) + Marshal.SizeOf(pid2));
                                }
                            }
                        }
                        continue;
                    }
                    finally
                    {
                        if (pTcpTable != null)
                        {
                            pTcpTable.Close();
                        }
                    }
                }
                if ((num2 != 0) && (num2 != 0xe8))
                {
                    throw new NetworkInformationException((int)num2);
                }
            }
            return(list);
        }