private static List <Connection> GetExtendedUdpTable4() { IpHelperApi.GetExtendedUdpTable(null, out int size, true, AfInet.AF_INET, UdpTableClass.UDP_TABLE_OWNER_PID, 0); byte[] udpTable = new byte[size]; IpHelperApi.GetExtendedUdpTable(udpTable, out size, true, AfInet.AF_INET, UdpTableClass.UDP_TABLE_OWNER_PID, 0); int index = 0; int entries = BitConverter.ToInt32(udpTable, index); index += 4; List <Connection> table = new List <Connection>(entries); for (int i = 0; i < entries; ++i) { Connection udp = new Connection { ConnectionType = "UDP", State = "Listen" }; uint localAddr = BitConverter.ToUInt32(udpTable, index); index += 4; uint localPort = BitConverter.ToUInt32(udpTable, index); index += 4; udp.LocalEndPoint = new IPEndPoint(localAddr, (int)ConvertPort(localPort)); udp.RemoteEndPoint = new IPEndPoint(0, 0); udp.Pid = BitConverter.ToInt32(udpTable, index); index += 4; table.Add(udp); } return(table); }
private static List <Connection> GetExtendedUdpTable6() { IpHelperApi.GetExtendedUdpTable(null, out int size, true, AfInet.AF_INET6, UdpTableClass.UDP_TABLE_OWNER_PID, 0); byte[] udpTable = new byte[size]; IpHelperApi.GetExtendedUdpTable(udpTable, out size, true, AfInet.AF_INET6, UdpTableClass.UDP_TABLE_OWNER_PID, 0); int index = 0; int entries = BitConverter.ToInt32(udpTable, index); index += 4; List <Connection> table = new List <Connection>(entries); byte[] localAddr = new byte[16]; byte[] remoteAddr = new byte[16]; for (int i = 0; i < entries; ++i) { Connection udp = new Connection { ConnectionType = "UDPv6" }; Array.Copy(udpTable, index, localAddr, 0, 16); index += 16; uint localScope = BitConverter.ToUInt32(udpTable, index); index += 4; uint localPort = BitConverter.ToUInt32(udpTable, index); index += 4; udp.LocalEndPoint = new IPEndPoint(new IPAddress(localAddr, localScope), (int)ConvertPort(localPort)); udp.RemoteEndPoint = new IPEndPoint(new IPAddress(remoteAddr, 0), 0); udp.State = "Listen"; udp.Pid = BitConverter.ToInt32(udpTable, index); index += 4; table.Add(udp); } return(table); }