public void GetUdpConnections() { int AF_INET = 2; // IP_v4 int buffSize = 20000; byte[] buffer = new byte[buffSize]; int res = IPHlpAPI32Wrapper.GetExtendedUdpTable(buffer, out buffSize, true, AF_INET, UDP_TABLE_CLASS.UDP_TABLE_OWNER_PID, 0); if (res != Utils.NO_ERROR) { buffer = new byte[buffSize]; res = IPHlpAPI32Wrapper.GetExtendedUdpTable(buffer, out buffSize, true, AF_INET, UDP_TABLE_CLASS.UDP_TABLE_OWNER_PID, 0); if (res != Utils.NO_ERROR) { return; } } int nOffset = 0; int NumEntries = Convert.ToInt32(buffer[nOffset]); nOffset += 4; for (int i = 0; i < NumEntries; i++) { TCPUDPConnection row = new TCPUDPConnection(this); row.Protocol = Protocol.UDP; row.Local = Utils.BufferToIPEndPoint(buffer, ref nOffset, false); row.PID = Utils.BufferToInt(buffer, ref nOffset); this.Add(row); } }
//IPv6 Version public void GetUdp6Connections() { int AF_INET = (int)AddressFamily.InterNetworkV6; // IP_v6 int buffSize = 20000; byte[] buffer = new byte[buffSize]; int res = IPHlpAPI32Wrapper.GetExtendedUdpTable(buffer, out buffSize, true, AF_INET, UDP_TABLE_CLASS.UDP_TABLE_OWNER_PID, 0); if (res != Utils.NO_ERROR) { buffer = new byte[buffSize]; res = IPHlpAPI32Wrapper.GetExtendedUdpTable(buffer, out buffSize, true, AF_INET, UDP_TABLE_CLASS.UDP_TABLE_OWNER_PID, 0); if (res != Utils.NO_ERROR) { return; } } int nOffset = 0; int NumEntries = Convert.ToInt32(buffer[nOffset]); nOffset += 4; for (int i = 0; i < NumEntries; i++) { TCPUDPConnection row = new TCPUDPConnection(this); row.Protocol = Protocol.UDP; row.AddressFamily = AddressFamily.InterNetworkV6; // Read in local address information row.Local = Utils.BufferToIPv6EndPoint(buffer, ref nOffset); // PID row.PID = BitConverter.ToInt32(buffer, nOffset); nOffset += 4; this.Add(row); } }