Ejemplo n.º 1
0
        public UdpConnectInfo GetUdpConnexions()
        {
            byte[] buffer  = new byte[20000]; // Start with 20.000 bytes left for information about tcp table
            int    pdwSize = 20000;
            int    res     = ConInfosHlpAPI32Wrapper.GetUdpTable(buffer, out pdwSize, true);

            if (res != NO_ERROR)
            {
                buffer = new byte[pdwSize];
                res    = ConInfosHlpAPI32Wrapper.GetUdpTable(buffer, out pdwSize, true);
                if (res != 0)
                {
                    return(UdpConnexion);     // Error. You should handle it
                }
            }

            UdpConnexion = new UdpConnectInfo();

            int nOffset = 0;

            // number of entry in the
            UdpConnexion.dwNumEntries = Convert.ToInt32(buffer[nOffset]);
            nOffset           += 4;
            UdpConnexion.table = new UdpConnectTable[UdpConnexion.dwNumEntries];
            for (int i = 0; i < UdpConnexion.dwNumEntries; i++)
            {
                string LocalAdrr = buffer[nOffset].ToString() + "." + buffer[nOffset + 1].ToString() + "." + buffer[nOffset + 2].ToString() + "." + buffer[nOffset + 3].ToString();
                nOffset += 4;

                int LocalPort = (((int)buffer[nOffset]) << 8) + (((int)buffer[nOffset + 1])) +
                                (((int)buffer[nOffset + 2]) << 24) + (((int)buffer[nOffset + 3]) << 16);
                nOffset += 4;
                UdpConnexion.table[i].Local = new IPEndPoint(IPAddress.Parse(LocalAdrr), LocalPort);
            }
            return(UdpConnexion);
        }
Ejemplo n.º 2
0
        public TcpConnectExInfo GetExTcpConnexions()
        {
            // the size of the MIB_EXTCPROW struct =  6*DWORD
            int rowsize    = 24;
            int BufferSize = 100000;
            // allocate a dumb memory space in order to retrieve  nb of connexion
            IntPtr lpTable = Marshal.AllocHGlobal(BufferSize);
            //getting infos
            int res = ConInfosHlpAPI32Wrapper.AllocateAndGetTcpExTableFromStack(ref lpTable, true, ConInfosHlpAPI32Wrapper.GetProcessHeap(), 0, 2);

            if (res != NO_ERROR)
            {
                Debug.WriteLine("Erreur : " + ConInfosHlpAPI32Wrapper.GetAPIErrorMessageDescription(res) + " " + res);
                return(TcpExConnexions); // Error. You should handle it
            }
            int CurrentIndex = 0;
            //get the number of entries in the table
            int NumEntries = (int)Marshal.ReadIntPtr(lpTable);

            lpTable = IntPtr.Zero;
            // free allocated space in memory
            Marshal.FreeHGlobal(lpTable);



            ///////////////////
            // calculate the real buffer size nb of entrie * size of the struct for each entrie(24) + the dwNumEntries
            BufferSize = (NumEntries * rowsize) + 4;
            // make the struct to hold the resullts
            TcpExConnexions = new TcpConnectExInfo();
            // Allocate memory
            lpTable = Marshal.AllocHGlobal(BufferSize);
            res     = ConInfosHlpAPI32Wrapper.AllocateAndGetTcpExTableFromStack(ref lpTable, true, ConInfosHlpAPI32Wrapper.GetProcessHeap(), 0, 2);
            if (res != NO_ERROR)
            {
                Debug.WriteLine("Erreur : " + ConInfosHlpAPI32Wrapper.GetAPIErrorMessageDescription(res) + " " + res);
                return(TcpExConnexions); // Error. You should handle it
            }
            // New pointer of iterating throught the data
            IntPtr current = lpTable;

            CurrentIndex = 0;
            // get the (again) the number of entries
            NumEntries = (int)Marshal.ReadIntPtr(current);
            TcpExConnexions.dwNumEntries = NumEntries;
            // Make the array of entries
            TcpExConnexions.table = new TcpConnectExTable[NumEntries];
            // iterate the pointer of 4 (the size of the DWORD dwNumEntries)
            CurrentIndex += 4;
            current       = (IntPtr)((int)current + CurrentIndex);
            // for each entries
            for (int i = 0; i < NumEntries; i++)
            {
                // The state of the connexion (in string)
                TcpExConnexions.table[i].StrgState = this.convert_state((int)Marshal.ReadIntPtr(current));
                // The state of the connexion (in ID)
                TcpExConnexions.table[i].iState = (int)Marshal.ReadIntPtr(current);
                // iterate the pointer of 4
                current = (IntPtr)((int)current + 4);
                // get the local address of the connexion
                UInt32 localAddr = (UInt32)Marshal.ReadIntPtr(current);
                // iterate the pointer of 4
                current = (IntPtr)((int)current + 4);
                // get the local port of the connexion
                UInt32 localPort = (UInt32)Marshal.ReadIntPtr(current);
                // iterate the pointer of 4
                current = (IntPtr)((int)current + 4);
                // Store the local endpoint in the struct and convertthe port in decimal (ie convert_Port())
                TcpExConnexions.table[i].Local = new IPEndPoint(localAddr, (int)convert_Port(localPort));
                // get the remote address of the connexion
                UInt32 RemoteAddr = (UInt32)Marshal.ReadIntPtr(current);
                // iterate the pointer of 4
                current = (IntPtr)((int)current + 4);
                UInt32 RemotePort = 0;
                // if the remote address = 0 (0.0.0.0) the remote port is always 0
                // else get the remote port
                if (RemoteAddr != 0)
                {
                    RemotePort = (UInt32)Marshal.ReadIntPtr(current);
                    RemotePort = convert_Port(RemotePort);
                }
                current = (IntPtr)((int)current + 4);
                // store the remote endpoint in the struct  and convertthe port in decimal (ie convert_Port())
                TcpExConnexions.table[i].Remote = new IPEndPoint(RemoteAddr, (int)RemotePort);
                // store the process ID
                TcpExConnexions.table[i].dwProcessId = (int)Marshal.ReadIntPtr(current);
                // Store and get the process name in the struct
                TcpExConnexions.table[i].ProcessName = this.get_process_name(TcpExConnexions.table[i].dwProcessId);
                current = (IntPtr)((int)current + 4);
            }
            // free the buffer
            Marshal.FreeHGlobal(lpTable);
            // re init the pointer
            current = IntPtr.Zero;
            return(TcpExConnexions);
        }
Ejemplo n.º 3
0
 public TcpConnectStats GetTcpStats()
 {
     TcpStats = new TcpConnectStats();
     ConInfosHlpAPI32Wrapper.GetTcpStatistics(ref TcpStats);
     return(TcpStats);
 }
Ejemplo n.º 4
0
 public UdpConnectStats GetUdpStats()
 {
     UdpStats = new UdpConnectStats();
     ConInfosHlpAPI32Wrapper.GetUdpStatistics(ref UdpStats);
     return(UdpStats);
 }
Ejemplo n.º 5
0
        public TcpConnectInfo GetTcpConnexions()
        {
            byte[] buffer  = new byte[20000]; // Start with 20.000 bytes left for information about tcp table
            int    pdwSize = 20000;
            int    res     = ConInfosHlpAPI32Wrapper.GetTcpTable(buffer, out pdwSize, true);

            if (res != NO_ERROR)
            {
                buffer = new byte[pdwSize];
                res    = ConInfosHlpAPI32Wrapper.GetTcpTable(buffer, out pdwSize, true);
                if (res != 0)
                {
                    return(TcpConnexion);     // Error. You should handle it
                }
            }

            TcpConnexion = new TcpConnectInfo();

            int nOffset = 0;

            // number of entry in the
            TcpConnexion.dwNumEntries = Convert.ToInt32(buffer[nOffset]);
            nOffset           += 4;
            TcpConnexion.table = new TcpConnectTable[TcpConnexion.dwNumEntries];

            for (int i = 0; i < TcpConnexion.dwNumEntries; i++)
            {
                // state
                int st = Convert.ToInt32(buffer[nOffset]);
                // state in string
                TcpConnexion.table[i].StrgState = convert_state(st);
                // state  by ID
                TcpConnexion.table[i].iState = st;
                nOffset += 4;
                // local address
                string LocalAdrr = buffer[nOffset].ToString() + "." + buffer[nOffset + 1].ToString() + "." + buffer[nOffset + 2].ToString() + "." + buffer[nOffset + 3].ToString();
                nOffset += 4;
                //local port in decimal
                int LocalPort = (((int)buffer[nOffset]) << 8) + (((int)buffer[nOffset + 1])) +
                                (((int)buffer[nOffset + 2]) << 24) + (((int)buffer[nOffset + 3]) << 16);

                nOffset += 4;
                // store the remote endpoint
                TcpConnexion.table[i].Local = new IPEndPoint(IPAddress.Parse(LocalAdrr), LocalPort);

                // remote address
                string RemoteAdrr = buffer[nOffset].ToString() + "." + buffer[nOffset + 1].ToString() + "." + buffer[nOffset + 2].ToString() + "." + buffer[nOffset + 3].ToString();
                nOffset += 4;
                // if the remote address = 0 (0.0.0.0) the remote port is always 0
                // else get the remote port in decimal
                int RemotePort;
                //
                if (RemoteAdrr == "0.0.0.0")
                {
                    RemotePort = 0;
                }
                else
                {
                    RemotePort = (((int)buffer[nOffset]) << 8) + (((int)buffer[nOffset + 1])) +
                                 (((int)buffer[nOffset + 2]) << 24) + (((int)buffer[nOffset + 3]) << 16);
                }
                nOffset += 4;
                TcpConnexion.table[i].Remote = new IPEndPoint(IPAddress.Parse(RemoteAdrr), RemotePort);
            }
            return(TcpConnexion);
        }