public static TCPTable GetExtendedTCPTable(bool sorted) { var tcpRows = new List <TCPRow>(); var tcpTable = IntPtr.Zero; var tcpTableLength = 0; if (UnsafeNativeMethods.GetExtendedTcpTable(tcpTable, ref tcpTableLength, sorted, 2, UnsafeNativeMethods.TCP_TABLE_CLASS.OWNER_PID_ALL) == 0) { return(new TCPTable(tcpRows)); } try { tcpTable = Marshal.AllocHGlobal(tcpTableLength); if (UnsafeNativeMethods.GetExtendedTcpTable(tcpTable, ref tcpTableLength, true, 2, UnsafeNativeMethods.TCP_TABLE_CLASS.OWNER_PID_ALL) == 0) { var table = (UnsafeNativeMethods.TCPTable)Marshal.PtrToStructure(tcpTable, typeof(UnsafeNativeMethods.TCPTable)); var rowPtr = (IntPtr)((long)tcpTable + Marshal.SizeOf(table.Length)); for (var i = 0; i < table.Length; ++i) { tcpRows.Add(new TCPRow((UnsafeNativeMethods.TCPRow)Marshal.PtrToStructure(rowPtr, typeof(UnsafeNativeMethods.TCPRow)))); rowPtr = (IntPtr)((long)rowPtr + Marshal.SizeOf(typeof(UnsafeNativeMethods.TCPRow))); } } } finally { if (tcpTable != IntPtr.Zero) { Marshal.FreeHGlobal(tcpTable); } } return(new TCPTable(tcpRows)); }
private static List <ServerConnection> GetXIVServerIPList() { var list1 = new List <ServerConnection>(); var id = Constants.ProcessModel.ProcessID; var num1 = IntPtr.Zero; var dwOutBufLen = 0; var error = 0; var num2 = IntPtr.Zero; for (var index = 0; index < 5; ++index) { error = (int)UnsafeNativeMethods.GetExtendedTcpTable(num1, ref dwOutBufLen, false, 2U, UnsafeNativeMethods.TCP_TABLE_CLASS.OWNER_PID_ALL, 0U); if (error != 0) { if (num1 != IntPtr.Zero) { Marshal.FreeHGlobal(num1); var num3 = IntPtr.Zero; } num1 = Marshal.AllocHGlobal(dwOutBufLen); } else { break; } } try { if (error != 0) { throw new Win32Exception(error); } var num3 = Marshal.ReadInt32(num1); var num4 = IntPtr.Add(num1, 4); for (var index = 0; index <= num3 - 1; ++index) { var mibTcprowEx = (UnsafeNativeMethods.MIB_TCPROW_EX)Marshal.PtrToStructure(num4, typeof(UnsafeNativeMethods.MIB_TCPROW_EX)); if (mibTcprowEx.dwProcessId == id) { var list2 = list1; var ffxivConnection1 = new ServerConnection(); ffxivConnection1.SourceAddress = mibTcprowEx.dwRemoteAddr; ffxivConnection1.SourcePort = (ushort)mibTcprowEx.dwRemotePort; ffxivConnection1.DestinationAddress = mibTcprowEx.dwLocalAddr; ffxivConnection1.DestinationPort = (ushort)mibTcprowEx.dwLocalPort; list2.Add(ffxivConnection1); } num4 = IntPtr.Add(num4, Marshal.SizeOf(typeof(UnsafeNativeMethods.MIB_TCPROW_EX))); } } catch { throw new Win32Exception(error); } finally { Marshal.FreeHGlobal(num1); } return(list1); /* * var tables = IPHelper.GetExtendedTCPTable(true); * return (tables.Cast<TCPRow>() * .Where(table => table.ProcessId == Constants.ProcessModel.ProcessID) * .Select(table => new ServerConnection * { * SourceAddress = BitConverter.ToUInt32(table.RemoteEndPoint.Address.GetAddressBytes(), 0), * SourcePort = (ushort)table.RemoteEndPoint.Port, * DestinationAddress = BitConverter.ToUInt32(table.LocalEndPoint.Address.GetAddressBytes(), 0), * DestinationPort = (ushort)table.LocalEndPoint.Port * })).ToList(); */ }