Skip to content

Instantly share code, notes, and snippets.

@luxuia
Created May 21, 2020 02:45
Show Gist options
  • Save luxuia/619e7bb583e9553ab2d979ee3445233f to your computer and use it in GitHub Desktop.
Save luxuia/619e7bb583e9553ab2d979ee3445233f to your computer and use it in GitHub Desktop.
get all process listenr and port like netstat but use winapi iphelper
static void GetTCPPort() {
//UnityEngine.Debug.Log("Active Connections");
// UnityEngine.Debug.Log(" Proto Local Address Foreign Address State PID");
var curid = Process.GetCurrentProcess().Id;
var ports = new List<int>();
foreach (TcpRow tcpRow in ManagedIpHelper.GetExtendedTcpTable(true)) {
if (tcpRow.State == TcpState.Listen && tcpRow.ProcessId == curid && tcpRow.LocalEndPoint.Address.ToString().StartsWith("127.0.0.1")) {
try {
Process process = Process.GetProcessById(tcpRow.ProcessId);
if (process.ProcessName == "Unity" ) {
ports.Add(tcpRow.LocalEndPoint.Port);
//UnityEngine.Debug.Log(string.Format(" {0,-7}{1,-23}{2, -23}{3,-14}{4}", "TCP", tcpRow.LocalEndPoint, tcpRow.RemoteEndPoint, tcpRow.State, tcpRow.ProcessId));
//foreach (ProcessModule processModule in process.Modules) {
// UnityEngine.Debug.Log( processModule.FileName);
//}
//
//UnityEngine.Debug.Log(Path.GetFileName(process.MainModule.FileName));
}
}
catch {
}
}
}
ports.Sort((a, b) => { return b-a; });
port = string.Join(",", ports);
}
#region Managed IP Helper API
public class TcpTable : IEnumerable<TcpRow> {
#region Private Fields
private IEnumerable<TcpRow> tcpRows;
#endregion
#region Constructors
public TcpTable(IEnumerable<TcpRow> tcpRows) {
this.tcpRows = tcpRows;
}
#endregion
#region Public Properties
public IEnumerable<TcpRow> Rows {
get { return this.tcpRows; }
}
#endregion
#region IEnumerable<TcpRow> Members
public IEnumerator<TcpRow> GetEnumerator() {
return this.tcpRows.GetEnumerator();
}
#endregion
#region IEnumerable Members
IEnumerator IEnumerable.GetEnumerator() {
return this.tcpRows.GetEnumerator();
}
#endregion
}
public class TcpRow {
#region Private Fields
private IPEndPoint localEndPoint;
private IPEndPoint remoteEndPoint;
private TcpState state;
private int processId;
#endregion
#region Constructors
public TcpRow(IpHelper.TcpRow tcpRow) {
this.state = tcpRow.state;
this.processId = tcpRow.owningPid;
int localPort = (tcpRow.localPort1 << 8) + (tcpRow.localPort2) + (tcpRow.localPort3 << 24) + (tcpRow.localPort4 << 16);
long localAddress = tcpRow.localAddr;
this.localEndPoint = new IPEndPoint(localAddress, localPort);
int remotePort = (tcpRow.remotePort1 << 8) + (tcpRow.remotePort2) + (tcpRow.remotePort3 << 24) + (tcpRow.remotePort4 << 16);
long remoteAddress = tcpRow.remoteAddr;
this.remoteEndPoint = new IPEndPoint(remoteAddress, remotePort);
}
#endregion
#region Public Properties
public IPEndPoint LocalEndPoint {
get { return this.localEndPoint; }
}
public IPEndPoint RemoteEndPoint {
get { return this.remoteEndPoint; }
}
public TcpState State {
get { return this.state; }
}
public int ProcessId {
get { return this.processId; }
}
#endregion
}
public static class ManagedIpHelper {
#region Public Methods
public static TcpTable GetExtendedTcpTable(bool sorted) {
List<TcpRow> tcpRows = new List<TcpRow>();
IntPtr tcpTable = IntPtr.Zero;
int tcpTableLength = 0;
if (IpHelper.GetExtendedTcpTable(tcpTable, ref tcpTableLength, sorted, IpHelper.AfInet, IpHelper.TcpTableType.OwnerPidAll, 0) != 0) {
try {
tcpTable = Marshal.AllocHGlobal(tcpTableLength);
if (IpHelper.GetExtendedTcpTable(tcpTable, ref tcpTableLength, true, IpHelper.AfInet, IpHelper.TcpTableType.OwnerPidAll, 0) == 0) {
IpHelper.TcpTable table = (IpHelper.TcpTable)Marshal.PtrToStructure(tcpTable, typeof(IpHelper.TcpTable));
IntPtr rowPtr = (IntPtr)((long)tcpTable + Marshal.SizeOf(table.length));
for (int i = 0; i < table.length; ++i) {
tcpRows.Add(new TcpRow((IpHelper.TcpRow)Marshal.PtrToStructure(rowPtr, typeof(IpHelper.TcpRow))));
rowPtr = (IntPtr)((long)rowPtr + Marshal.SizeOf(typeof(IpHelper.TcpRow)));
}
}
}
finally {
if (tcpTable != IntPtr.Zero) {
Marshal.FreeHGlobal(tcpTable);
}
}
}
return new TcpTable(tcpRows);
}
#endregion
}
#endregion
#region P/Invoke IP Helper API
/// <summary>
/// <see cref="http://msdn2.microsoft.com/en-us/library/aa366073.aspx"/>
/// </summary>
public static class IpHelper {
#region Public Fields
public const string DllName = "iphlpapi.dll";
public const int AfInet = 2;
#endregion
#region Public Methods
/// <summary>
/// <see cref="http://msdn2.microsoft.com/en-us/library/aa365928.aspx"/>
/// </summary>
[DllImport(IpHelper.DllName, SetLastError = true)]
public static extern uint GetExtendedTcpTable(IntPtr tcpTable, ref int tcpTableLength, bool sort, int ipVersion, TcpTableType tcpTableType, int reserved);
#endregion
#region Public Enums
/// <summary>
/// <see cref="http://msdn2.microsoft.com/en-us/library/aa366386.aspx"/>
/// </summary>
public enum TcpTableType {
BasicListener,
BasicConnections,
BasicAll,
OwnerPidListener,
OwnerPidConnections,
OwnerPidAll,
OwnerModuleListener,
OwnerModuleConnections,
OwnerModuleAll,
}
#endregion
#region Public Structs
/// <summary>
/// <see cref="http://msdn2.microsoft.com/en-us/library/aa366921.aspx"/>
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct TcpTable {
public uint length;
public TcpRow row;
}
/// <summary>
/// <see cref="http://msdn2.microsoft.com/en-us/library/aa366913.aspx"/>
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct TcpRow {
public TcpState state;
public uint localAddr;
public byte localPort1;
public byte localPort2;
public byte localPort3;
public byte localPort4;
public uint remoteAddr;
public byte remotePort1;
public byte remotePort2;
public byte remotePort3;
public byte remotePort4;
public int owningPid;
}
#endregion
}
#endregion
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment