NetProcessTool.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Runtime.InteropServices;
  7. using System.Text;
  8. namespace Azylee.Core.NetUtils
  9. {
  10. public class NetProcessTool
  11. {
  12. #region 枚举 读取连接表选项
  13. enum TCP_TABLE_CLASS
  14. {
  15. TCP_TABLE_BASIC_LISTENER,
  16. TCP_TABLE_BASIC_CONNECTIONS,
  17. TCP_TABLE_BASIC_ALL,
  18. TCP_TABLE_OWNER_PID_LISTENER,
  19. TCP_TABLE_OWNER_PID_CONNECTIONS,
  20. TCP_TABLE_OWNER_PID_ALL,
  21. TCP_TABLE_OWNER_MODULE_LISTENER,
  22. TCP_TABLE_OWNER_MODULE_CONNECTIONS,
  23. TCP_TABLE_OWNER_MODULE_ALL
  24. }
  25. enum UDP_TABLE_CLASS
  26. {
  27. UDP_TABLE_BASIC,
  28. UDP_TABLE_OWNER_PID,
  29. UDP_TABLE_OWNER_MODULE
  30. }
  31. #endregion
  32. #region UDP 和 TCP 列表 结构
  33. struct UdpTable
  34. {
  35. public uint dwNumEntries;
  36. private UdpRow table;
  37. }
  38. struct TcpTable
  39. {
  40. public uint dwNumEntries;
  41. private TcpRow table;
  42. }
  43. #endregion
  44. #region UDP 和 TCP 行记录 结构
  45. public struct UdpRow
  46. {
  47. private uint localAddr;
  48. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
  49. private byte[] localPort;
  50. public IPAddress LocalIP
  51. {
  52. get
  53. {
  54. return new IPAddress((long)((ulong)this.localAddr));
  55. }
  56. }
  57. public ushort LocalPort
  58. {
  59. get
  60. {
  61. return BitConverter.ToUInt16(new byte[]
  62. {
  63. this.localPort[1],
  64. this.localPort[0]
  65. }, 0);
  66. }
  67. }
  68. public int ProcessId { get; }
  69. }
  70. public struct TcpRow
  71. {
  72. private ConnectionState state;
  73. private uint localAddr;
  74. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
  75. private byte[] localPort;
  76. private uint remoteAddr;
  77. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
  78. private byte[] remotePort;
  79. public ConnectionState State
  80. {
  81. get { return state; }
  82. }
  83. public IPAddress LocalIP
  84. {
  85. get
  86. {
  87. return new IPAddress((long)((ulong)this.localAddr));
  88. }
  89. }
  90. public ushort LocalPort
  91. {
  92. get
  93. {
  94. return BitConverter.ToUInt16(new byte[]
  95. {
  96. this.localPort[1],
  97. this.localPort[0]
  98. }, 0);
  99. }
  100. }
  101. public IPAddress RemoteIP
  102. {
  103. get
  104. {
  105. return new IPAddress((long)((ulong)this.remoteAddr));
  106. }
  107. }
  108. public ushort RemotePort
  109. {
  110. get
  111. {
  112. return BitConverter.ToUInt16(new byte[]
  113. {
  114. this.remotePort[1],
  115. this.remotePort[0]
  116. }, 0);
  117. }
  118. }
  119. public int ProcessId { get; }
  120. }
  121. #endregion
  122. [DllImport("iphlpapi.dll", SetLastError = true)]
  123. static extern uint GetExtendedTcpTable(IntPtr pTcpTable, ref int dwOutBufLen, bool sort, int ipVersion, TCP_TABLE_CLASS tblClass, uint reserved = 0u);
  124. [DllImport("iphlpapi.dll", SetLastError = true)]
  125. static extern uint GetExtendedUdpTable(IntPtr pUdpTable, ref int dwOutBufLen, bool sort, int ipVersion, UDP_TABLE_CLASS tblClass, uint reserved = 0u);
  126. public static TcpRow[] GetTcpConnection()
  127. {
  128. TcpRow[] array = null;
  129. int ipVersion = 2;
  130. int cb = 0;
  131. uint extendedTcpTable = GetExtendedTcpTable(IntPtr.Zero, ref cb, true, ipVersion, TCP_TABLE_CLASS.TCP_TABLE_OWNER_PID_ALL, 0u);
  132. IntPtr intPtr = Marshal.AllocHGlobal(cb);
  133. try
  134. {
  135. extendedTcpTable = GetExtendedTcpTable(intPtr, ref cb, true, ipVersion, TCP_TABLE_CLASS.TCP_TABLE_OWNER_PID_ALL, 0u);
  136. bool flag = extendedTcpTable > 0u;
  137. if (flag) return null;
  138. TcpTable tcpTable = (TcpTable)Marshal.PtrToStructure(intPtr, typeof(TcpTable));
  139. IntPtr intPtr2 = (IntPtr)((long)intPtr + Marshal.SizeOf(tcpTable.dwNumEntries));
  140. array = new TcpRow[tcpTable.dwNumEntries];
  141. int num = 0;
  142. while (num < (long)((ulong)tcpTable.dwNumEntries))
  143. {
  144. TcpRow tcpRow = (TcpRow)Marshal.PtrToStructure(intPtr2, typeof(TcpRow));
  145. array[num] = tcpRow;
  146. intPtr2 = (IntPtr)((long)intPtr2 + Marshal.SizeOf(tcpRow));
  147. num++;
  148. }
  149. }
  150. catch (Exception)
  151. { }
  152. finally
  153. {
  154. Marshal.FreeHGlobal(intPtr);
  155. }
  156. return array;
  157. }
  158. public static UdpRow[] GetUdpConnection()
  159. {
  160. UdpRow[] array = null;
  161. int ipVersion = 2;
  162. int cb = 0;
  163. uint extendedUdpTable = GetExtendedUdpTable(IntPtr.Zero, ref cb, true, ipVersion, UDP_TABLE_CLASS.UDP_TABLE_OWNER_PID, 0u);
  164. IntPtr intPtr = Marshal.AllocHGlobal(cb);
  165. try
  166. {
  167. extendedUdpTable = GetExtendedUdpTable(intPtr, ref cb, true, ipVersion, UDP_TABLE_CLASS.UDP_TABLE_OWNER_PID, 0u);
  168. bool flag = extendedUdpTable > 0u;
  169. if (flag) return null;
  170. UdpTable udpTable = (UdpTable)Marshal.PtrToStructure(intPtr, typeof(UdpTable));
  171. IntPtr intPtr2 = (IntPtr)((long)intPtr + Marshal.SizeOf(udpTable.dwNumEntries));
  172. array = new UdpRow[udpTable.dwNumEntries];
  173. int num = 0;
  174. while (num < (long)((ulong)udpTable.dwNumEntries))
  175. {
  176. UdpRow udpRow = (UdpRow)Marshal.PtrToStructure(intPtr2, typeof(UdpRow));
  177. array[num] = udpRow;
  178. intPtr2 = (IntPtr)((long)intPtr2 + Marshal.SizeOf(udpRow));
  179. num++;
  180. }
  181. }
  182. catch (Exception)
  183. { }
  184. finally
  185. {
  186. Marshal.FreeHGlobal(intPtr);
  187. }
  188. return array;
  189. }
  190. }
  191. }