Example #1
0
        public static bool GetProcessTCPConns(ref MIB_TCPTABLE_OWNER_PID ExConns)
        {
            UInt32* ptable = (UInt32*)IntPtr.Zero;
            UInt32 dwSize = 0;
            GetExtendedTcpTable((IntPtr)ptable, ref dwSize, true, AFType.AF_INET, TCP_TABLE_CLASS.TCP_TABLE_OWNER_PID_ALL, 0);

            char* tmp = stackalloc char[(int)dwSize];
            ptable = (UInt32*)tmp;

            if (GetExtendedTcpTable((IntPtr)ptable, ref dwSize, true, AFType.AF_INET, TCP_TABLE_CLASS.TCP_TABLE_OWNER_PID_ALL, 0) != NO_ERROR)
                return false;

            ExConns.dwNumEntries = (UInt32)ptable[0];
            ExConns.table = new MIB_TCPROW_OWNER_PID[ExConns.dwNumEntries];
            MIB_TCPROW_OWNER_PID * row = (MIB_TCPROW_OWNER_PID*)&ptable[1];
            UInt32 j = 0;
            for (int i = 0; i < ExConns.dwNumEntries; i++)
            {
                if (!IP.specialIP(row[i].dwLocalAddr) && !IP.specialIP(row[i].dwRemoteAddr))
                    ExConns.table[j++] = row[i];
            }

            ExConns.dwNumEntries = j;

            return true;
        }
Example #2
0
        public static HashSet<UInt32> netActiveProcesses()
        {
            HashSet<UInt32> processes = new HashSet<UInt32>();
            MIB_UDPTABLE_OWNER_PID utable = new MIB_UDPTABLE_OWNER_PID();
            bool res = GetProcessUDPConns(ref utable);
            if(res)
            {
                for(int i = 0; i < utable.dwNumEntries; i++)
                {
                    MIB_UDPROW_OWNER_PID urow = utable.table[i];
                    if (!processes.Contains(urow.dwOwningPid))
                        processes.Add(urow.dwOwningPid);
                }
            }

            MIB_TCPTABLE_OWNER_PID ttable = new MIB_TCPTABLE_OWNER_PID();
            res = GetProcessTCPConns(ref ttable);
            if(res)
            {
                for(int i = 0; i < ttable.dwNumEntries; i++)
                {
                    MIB_TCPROW_OWNER_PID trow = ttable.table[i];
                    if (!processes.Contains(trow.dwOwningPid) && tcpActive(trow))
                        processes.Add(trow.dwOwningPid);
                }
            }

            return processes;
        }