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> GetExtendedTcpTable6() { IpHelperApi.GetExtendedTcpTable(null, out int size, true, AfInet.AF_INET6, TcpTableClass.TCP_TABLE_OWNER_PID_ALL, 0); byte[] tcpTable = new byte[size]; IpHelperApi.GetExtendedTcpTable(tcpTable, out size, true, AfInet.AF_INET6, TcpTableClass.TCP_TABLE_OWNER_PID_ALL, 0); int index = 0; int entries = BitConverter.ToInt32(tcpTable, 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 tcp = new Connection { ConnectionType = "TCPv6" }; Array.Copy(tcpTable, index, localAddr, 0, 16); index += 16; uint localScope = BitConverter.ToUInt32(tcpTable, index); index += 4; uint localPort = BitConverter.ToUInt32(tcpTable, index); index += 4; tcp.LocalEndPoint = new IPEndPoint(new IPAddress(localAddr, localScope), (int)ConvertPort(localPort)); Array.Copy(tcpTable, index, remoteAddr, 0, 16); index += 16; uint remoteScope = BitConverter.ToUInt32(tcpTable, index); index += 4; uint remotePort = BitConverter.ToUInt32(tcpTable, index); index += 4; tcp.RemoteEndPoint = new IPEndPoint(new IPAddress(remoteAddr, remoteScope), (int)ConvertPort(remotePort)); tcp.State = ConvertState(BitConverter.ToInt32(tcpTable, index)); index += 4; tcp.Pid = BitConverter.ToInt32(tcpTable, index); index += 4; table.Add(tcp); } return(table); }
private static List <Connection> GetExtendedTcpTable4() { IpHelperApi.GetExtendedTcpTable(null, out int size, true, AfInet.AF_INET, TcpTableClass.TCP_TABLE_OWNER_PID_ALL, 0); byte[] tcpTable = new byte[size]; IpHelperApi.GetExtendedTcpTable(tcpTable, out size, true, AfInet.AF_INET, TcpTableClass.TCP_TABLE_OWNER_PID_ALL, 0); int index = 0; int entries = BitConverter.ToInt32(tcpTable, index); index += 4; List <Connection> table = new List <Connection>(entries); for (int i = 0; i < entries; ++i) { Connection tcp = new Connection { ConnectionType = "TCP", State = ConvertState(BitConverter.ToInt32(tcpTable, index)) }; index += 4; uint localAddr = BitConverter.ToUInt32(tcpTable, index); index += 4; uint localPort = BitConverter.ToUInt32(tcpTable, index); index += 4; tcp.LocalEndPoint = new IPEndPoint(localAddr, (int)ConvertPort(localPort)); uint remoteAddr = BitConverter.ToUInt32(tcpTable, index); index += 4; uint remotePort = BitConverter.ToUInt32(tcpTable, index); index += 4; tcp.RemoteEndPoint = new IPEndPoint(remoteAddr, (int)ConvertPort(remotePort)); tcp.Pid = BitConverter.ToInt32(tcpTable, index); index += 4; table.Add(tcp); } return(table); }