public static Boolean CheckData(ref Settings settings)
        {
            List <GridItem>  results   = new List <GridItem>();
            List <TcpRecord> tcpList   = NetworkHelper.GetTcpConnections(settings.ExcludeAppFromTCPConnections);
            List <UdpRecord> udpList   = NetworkHelper.GetUdpConnections();
            Boolean          wasChange = false;

            if (GridSource.Count > 0)
            {
                results = GridSource.ToList();
                //remove non existing ones from old list
                var nonExisting = results.Where(fn => fn.Protocol == "TCP").ToList().Where(fn => !tcpList.Select(t => t.ID).ToList().Contains(fn.ID)).ToList();
                nonExisting.ForEach(it =>
                {
                    results.Remove(it);
                    mapItems.Remove(mapItems.Where(fn => fn.ID == it.ID).FirstOrDefault());
                });

                nonExisting = results.Where(fn => fn.Protocol == "UDP").ToList().Where(fn => !udpList.Select(t => t.ID).ToList().Contains(fn.ID)).ToList();
                nonExisting.ForEach(it =>
                {
                    results.Remove(it);
                });
            }
            else
            {
                wasChange = true;
            }
            tcpList.ForEach(t =>
            {
                if (results.Where(fn => fn.Protocol == t.Protocol && fn.ID == t.ID).Count() == 0)
                {
                    t.UpdateDetails();
                    if (t.GeoData.IsOK())
                    {
                        mapItems.Add(t);
                    }
                    results.Add(new GridItem(t.ID, t.Protocol, t.LocalPort, t.LocalAddress.ToString(), t.RemoteAddress.ToString(), t.RemotePort, t.Country, t.City, t.StateText, t.ProcessName, t.PID, t.ProcessIcon, t.ProcessPath));
                    wasChange = true;
                }
            });
            udpList.ForEach(t =>
            {
                if (results.Where(fn => fn.Protocol == t.Protocol && fn.ID == t.ID).Count() == 0)
                {
                    results.Add(new GridItem(t.ID, t.Protocol, (int)t.LocalPort, t.LocalAddress.ToString(), String.Empty, -1, String.Empty, String.Empty, String.Empty, t.ProcessName, t.PID, t.ProcessIcon, t.ProcessPath));
                    wasChange = true;
                }
            });
            if (wasChange)
            {
                GridSource = new SortableBindingList <GridItem>(results);
            }
            return(wasChange);
        }
Esempio n. 2
0
        public static string GetProcessNameByTcpConnection(IPAddress sourceAddress, IPAddress destinationAddress, ushort sourcePort, ushort destinationPort, IPAddress localIP)
        {
            List <TcpRecord> tcpRecords = null;
            ushort           port;
            IPAddress        address;

            if (localIP == sourceAddress)
            {
                port    = sourcePort;
                address = sourceAddress;
            }
            else
            {
                port    = destinationPort;
                address = destinationAddress;
            }
            if ((tcpRecords = NetworkHelper.GetTcpConnections()) != null && tcpRecords.Count > 0)
            {
                TcpRecord record = tcpRecords.Where(r => r.LocalPort == port).SingleOrDefault();
                if (record != null)
                {
                    return(record.PID.ToString());

                    //if (record.PID == 0)
                    //    return "System";
                    //else
                    //{
                    //    Process proc = null;
                    //    if ((proc = GetProcessByID(record.PID)) != null)
                    //        return proc.ProcessName;
                    //    else
                    //        return "N/A";
                    //}
                }
            }

            return(String.Empty);
        }