public static string GetProcessNameByUDpConnection(IPAddress sourceAddress, IPAddress destinationAddress, ushort sourcePort, ushort destinationPort, IPAddress localIP) { List <UdpRecord> udpRecords = null; ushort port; IPAddress address; if (localIP == sourceAddress) { port = sourcePort; address = sourceAddress; } else { port = destinationPort; address = destinationAddress; } if ((udpRecords = NetworkHelper.GetUdpConnections()) != null) { UdpRecord record = udpRecords.Where(r => r.LocalPort == port).SingleOrDefault(); if (record != null) { return(record.PID.ToString()); } } return(String.Empty); }
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); }