UdpTable.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using System;
  2. using System.Buffers.Binary;
  3. using System.Diagnostics;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using System.Runtime.InteropServices;
  7. using System.Runtime.Versioning;
  8. namespace FastGithub.Dns
  9. {
  10. /// <summary>
  11. /// windows iphlpapi
  12. /// </summary>
  13. [SupportedOSPlatform("windows")]
  14. unsafe static class UdpTable
  15. {
  16. private const int ERROR_INSUFFICIENT_BUFFER = 122;
  17. [DllImport("iphlpapi.dll", SetLastError = true)]
  18. private static extern uint GetExtendedUdpTable(void* pUdpTable, ref int pdwSize, bool bOrder, AddressFamily ulAf, UDP_TABLE_CLASS tableClass, uint reserved = 0);
  19. /// <summary>
  20. /// 杀死占用进程
  21. /// </summary>
  22. /// <param name="port"></param>
  23. /// <returns></returns>
  24. public static bool KillPortOwner(int port)
  25. {
  26. if (TryGetOwnerProcessId(port, out var pid) == false)
  27. {
  28. return true;
  29. }
  30. try
  31. {
  32. var proess = Process.GetProcessById(pid);
  33. proess.Kill();
  34. proess.WaitForExit();
  35. return true;
  36. }
  37. catch (ArgumentException)
  38. {
  39. return true;
  40. }
  41. catch (Exception)
  42. {
  43. return false;
  44. }
  45. }
  46. /// <summary>
  47. /// 获取udp端口的占用进程id
  48. /// </summary>
  49. /// <param name="port"></param>
  50. /// <param name="processId"></param>
  51. /// <returns></returns>
  52. public static bool TryGetOwnerProcessId(int port, out int processId)
  53. {
  54. processId = 0;
  55. var pdwSize = 0;
  56. var result = GetExtendedUdpTable(null, ref pdwSize, false, AddressFamily.InterNetwork, UDP_TABLE_CLASS.UDP_TABLE_OWNER_PID);
  57. if (result != ERROR_INSUFFICIENT_BUFFER)
  58. {
  59. return false;
  60. }
  61. var buffer = new byte[pdwSize];
  62. fixed (byte* pUdpTable = &buffer[0])
  63. {
  64. result = GetExtendedUdpTable(pUdpTable, ref pdwSize, false, AddressFamily.InterNetwork, UDP_TABLE_CLASS.UDP_TABLE_OWNER_PID);
  65. if (result != 0)
  66. {
  67. return false;
  68. }
  69. var prt = new IntPtr(pUdpTable);
  70. var table = Marshal.PtrToStructure<MIB_UDPTABLE_OWNER_PID>(prt);
  71. prt += sizeof(int);
  72. for (var i = 0; i < table.dwNumEntries; i++)
  73. {
  74. var row = Marshal.PtrToStructure<MIB_UDPROW_OWNER_PID>(prt);
  75. if (row.LocalPort == port)
  76. {
  77. processId = row.ProcessId;
  78. return true;
  79. }
  80. prt += Marshal.SizeOf<MIB_UDPROW_OWNER_PID>();
  81. }
  82. }
  83. return false;
  84. }
  85. private enum UDP_TABLE_CLASS
  86. {
  87. UDP_TABLE_BASIC,
  88. UDP_TABLE_OWNER_PID,
  89. UDP_TABLE_OWNER_MODULE
  90. }
  91. [StructLayout(LayoutKind.Sequential)]
  92. private struct MIB_UDPTABLE_OWNER_PID
  93. {
  94. public uint dwNumEntries;
  95. }
  96. [StructLayout(LayoutKind.Sequential)]
  97. private struct MIB_UDPROW_OWNER_PID
  98. {
  99. public uint localAddr;
  100. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
  101. public byte[] localPort;
  102. public int owningPid;
  103. public int ProcessId => owningPid;
  104. public IPAddress LocalAddress => new(localAddr);
  105. public ushort LocalPort => BinaryPrimitives.ReadUInt16BigEndian(this.localPort);
  106. }
  107. }
  108. }