public static MIB_UDP6ROW_OWNER_PID[] GetUdp6Table() { MIB_UDP6ROW_OWNER_PID[] Entries = null; System.IntPtr Table = System.Runtime.InteropServices.Marshal.AllocHGlobal(5000); System.UInt32 Size = 5000; System.UInt32 ret = GetExtendedUdpTable(Table, ref Size, false, Address_Family.AF_INET6, UDP_TABLE_CLASS.UDP_TABLE_OWNER_PID, 0); if (ret == 0) { System.UInt32 NumEntries = Convert.ToUInt32(System.Runtime.InteropServices.Marshal.ReadInt32(Table)); System.IntPtr PtrEntries = AddToPointer(Table, 4); Entries = new MIB_UDP6ROW_OWNER_PID[NumEntries]; for (int i = 0; i < NumEntries; ++i) { Entries[i] = new MIB_UDP6ROW_OWNER_PID(); System.IntPtr PtrEntry = AddToPointer(PtrEntries, (i * System.Runtime.InteropServices.Marshal.SizeOf(Entries[i]))); System.Runtime.InteropServices.Marshal.PtrToStructure(PtrEntry, Entries[i]); } } System.Runtime.InteropServices.Marshal.FreeHGlobal(Table); return(Entries); }
public static MIB_UDP6ROW_OWNER_PID[] GetUdp6Table() { MIB_UDP6ROW_OWNER_PID[] Entries = null; System.IntPtr Table = System.Runtime.InteropServices.Marshal.AllocHGlobal(5000); System.UInt32 Size = 5000; System.UInt32 ret = GetExtendedUdpTable(Table, ref Size, false, Address_Family.AF_INET6, UDP_TABLE_CLASS.UDP_TABLE_OWNER_PID, 0); if (ret == 0) { System.UInt32 NumEntries = Convert.ToUInt32(System.Runtime.InteropServices.Marshal.ReadInt32(Table)); System.IntPtr PtrEntries = AddToPointer(Table, 4); Entries = new MIB_UDP6ROW_OWNER_PID[NumEntries]; for (int i = 0; i < NumEntries; ++i) { Entries[i] = new MIB_UDP6ROW_OWNER_PID(); System.IntPtr PtrEntry = AddToPointer(PtrEntries, (i * System.Runtime.InteropServices.Marshal.SizeOf(Entries[i]))); System.Runtime.InteropServices.Marshal.PtrToStructure(PtrEntry, Entries[i]); } } System.Runtime.InteropServices.Marshal.FreeHGlobal(Table); return Entries; }
public static IEnumerable <UdpConnectionInfo> GetUdpConnections(IPVersion ipVersion, Dictionary <int, Process> processesByPid = null) { int bufferSize = 0; List <UdpConnectionInfo> udpTableRecords = new List <UdpConnectionInfo>(); int ulAf = AF_INET; if (ipVersion == IPVersion.IPv6) { ulAf = AF_INET6; } // Getting the initial size of UDP table. uint result = Iphlpapi.GetExtendedUdpTable(IntPtr.Zero, ref bufferSize, true, ulAf, UdpTableClass.UDP_TABLE_OWNER_PID); // Allocating memory as an IntPtr with the bufferSize. IntPtr udpTableRecordsPtr = Marshal.AllocHGlobal(bufferSize); try { // The IntPtr from last call, udpTableRecoresPtr must be used in the subsequent // call and passed as the first parameter. result = Iphlpapi.GetExtendedUdpTable(udpTableRecordsPtr, ref bufferSize, true, ulAf, UdpTableClass.UDP_TABLE_OWNER_PID); // If not zero, call failed. if (result != 0) { return(new List <UdpConnectionInfo>()); } // Marshals data fron an unmanaged block of memory to the // newly allocated managed object 'udpRecordsTable' of type // 'MIB_UDPTABLE_OWNER_PID' to get number of entries of TCP // table structure. // Determine if IPv4 or IPv6. if (ipVersion == IPVersion.IPv4) { MIB_UDPTABLE_OWNER_PID udpRecordsTable = (MIB_UDPTABLE_OWNER_PID)Marshal.PtrToStructure(udpTableRecordsPtr, typeof(MIB_UDPTABLE_OWNER_PID)); IntPtr tableRowPtr = (IntPtr)((long)udpTableRecordsPtr + Marshal.SizeOf(udpRecordsTable.dwNumEntries)); // Read and parse the UDP records from the table and store them in list // 'UdpConnection' structure type objects. for (int i = 0; i < udpRecordsTable.dwNumEntries; i++) { MIB_UDPROW_OWNER_PID udpRow = (MIB_UDPROW_OWNER_PID)Marshal.PtrToStructure(tableRowPtr, typeof(MIB_UDPROW_OWNER_PID)); udpTableRecords.Add(new UdpConnectionInfo( Protocol.UDP, new IPAddress(udpRow.localAddr), BitConverter.ToUInt16(new byte[2] { udpRow.localPort[1], udpRow.localPort[0] }, 0), udpRow.owningPid, GetProcessNameByPid(udpRow.owningPid, processesByPid))); tableRowPtr = (IntPtr)((long)tableRowPtr + Marshal.SizeOf(udpRow)); } } else if (ipVersion == IPVersion.IPv6) { MIB_UDP6TABLE_OWNER_PID udpRecordsTable = (MIB_UDP6TABLE_OWNER_PID) Marshal.PtrToStructure(udpTableRecordsPtr, typeof(MIB_UDP6TABLE_OWNER_PID)); IntPtr tableRowPtr = (IntPtr)((long)udpTableRecordsPtr + Marshal.SizeOf(udpRecordsTable.dwNumEntries)); // Read and parse the UDP records from the table and store them in list // 'UdpConnection' structure type objects. for (int i = 0; i < udpRecordsTable.dwNumEntries; i++) { MIB_UDP6ROW_OWNER_PID udpRow = (MIB_UDP6ROW_OWNER_PID) Marshal.PtrToStructure(tableRowPtr, typeof(MIB_UDP6ROW_OWNER_PID)); udpTableRecords.Add(new UdpConnectionInfo( Protocol.UDP, new IPAddress(udpRow.localAddr, udpRow.localScopeId), BitConverter.ToUInt16(new byte[2] { udpRow.localPort[1], udpRow.localPort[0] }, 0), udpRow.owningPid, GetProcessNameByPid(udpRow.owningPid, processesByPid))); tableRowPtr = (IntPtr)((long)tableRowPtr + Marshal.SizeOf(udpRow)); } } } catch (OutOfMemoryException outOfMemoryException) { throw outOfMemoryException; } catch (Exception exception) { throw exception; } finally { Marshal.FreeHGlobal(udpTableRecordsPtr); } return(udpTableRecords != null?udpTableRecords.Distinct().ToList() : new List <UdpConnectionInfo>()); }