public void GetUdpConnections() { byte[] buffer = new byte[20000]; // Start with 20.000 bytes left for information about tcp table int pdwSize = 20000; int res = IPHlpAPI32Wrapper.GetUdpTable(buffer, out pdwSize, true); if (res != Utils.NO_ERROR) { buffer = new byte[pdwSize]; res = IPHlpAPI32Wrapper.GetUdpTable(buffer, out pdwSize, true); if (res != Utils.NO_ERROR) { return; // Error. You should handle it } } UdpConnections = new IpHlpApidotnet.MIB_UDPTABLE(); int nOffset = 0; // number of entry in the UdpConnections.dwNumEntries = Convert.ToInt32(buffer[nOffset]); nOffset += 4; UdpConnections.table = new MIB_UDPROW[UdpConnections.dwNumEntries]; for (int i = 0; i < UdpConnections.dwNumEntries; i++) { ((MIB_UDPROW)(UdpConnections.table[i])).Local = Utils.BufferToIPEndPoint(buffer, ref nOffset, false);//new IPEndPoint(IPAddress.Parse(LocalAdrr), LocalPort); } }
public void GetUdpConnexions() { byte[] buffer = new byte[20000]; // Start with 20.000 bytes left for information about tcp table int pdwSize = 20000; int res = IPHlpAPI32Wrapper.GetUdpTable(buffer, out pdwSize, true); if (res != NO_ERROR) { buffer = new byte[pdwSize]; res = IPHlpAPI32Wrapper.GetUdpTable(buffer, out pdwSize, true); if (res != 0) { return; // Error. You should handle it } } UdpConnexion = new IpHlpApidotnet.MIB_UDPTABLE(); int nOffset = 0; // number of entry in the UdpConnexion.dwNumEntries = Convert.ToInt32(buffer[nOffset]); nOffset += 4; UdpConnexion.table = new MIB_UDPROW[UdpConnexion.dwNumEntries]; for (int i = 0; i < UdpConnexion.dwNumEntries; i++) { string LocalAdrr = buffer[nOffset].ToString() + "." + buffer[nOffset + 1].ToString() + "." + buffer[nOffset + 2].ToString() + "." + buffer[nOffset + 3].ToString(); nOffset += 4; int LocalPort = (((int)buffer[nOffset]) << 8) + (((int)buffer[nOffset + 1])) + (((int)buffer[nOffset + 2]) << 24) + (((int)buffer[nOffset + 3]) << 16); nOffset += 4; ((MIB_UDPROW)(UdpConnexion.table[i])).Local = new IPEndPoint(IPAddress.Parse(LocalAdrr), LocalPort); } }
public void GetUdpConnections() { int AF_INET = 2; // IP_v4 int buffSize = 20000; byte[] buffer = new byte[buffSize]; int res = IPHlpAPI32Wrapper.GetExtendedUdpTable(buffer, out buffSize, true, AF_INET, UDP_TABLE_CLASS.UDP_TABLE_OWNER_PID, 0); if (res != Utils.NO_ERROR) { buffer = new byte[buffSize]; res = IPHlpAPI32Wrapper.GetExtendedUdpTable(buffer, out buffSize, true, AF_INET, UDP_TABLE_CLASS.UDP_TABLE_OWNER_PID, 0); if (res != Utils.NO_ERROR) { return; } } int nOffset = 0; int NumEntries = Convert.ToInt32(buffer[nOffset]); nOffset += 4; for (int i = 0; i < NumEntries; i++) { TCPUDPConnection row = new TCPUDPConnection(this); row.Protocol = Protocol.UDP; row.Local = Utils.BufferToIPEndPoint(buffer, ref nOffset, false); row.PID = Utils.BufferToInt(buffer, ref nOffset); this.Add(row); } }
// New IPv6 version public void GetTcp6Connections() { int AF_INET = (int)AddressFamily.InterNetworkV6; // AF_INET6 int buffSize = 20000; byte[] buffer = new byte[buffSize]; int res = IPHlpAPI32Wrapper.GetExtendedTcpTable(buffer, out buffSize, true, AF_INET, TCP_TABLE_CLASS.TCP_TABLE_OWNER_PID_ALL, 0); if (res != Utils.NO_ERROR) //If there is no enough memory to execute function { buffer = new byte[buffSize]; res = IPHlpAPI32Wrapper.GetExtendedTcpTable(buffer, out buffSize, true, AF_INET, TCP_TABLE_CLASS.TCP_TABLE_OWNER_PID_ALL, 0); if (res != Utils.NO_ERROR) { return; } } int nOffset = 0; // number of entries in the table int NumEntries = Convert.ToInt32(buffer[nOffset]); nOffset += 4; for (int i = 0; i < NumEntries; i++) { TCPUDPConnection row = new TCPUDPConnection(this); row.Protocol = Protocol.TCP; row.AddressFamily = AddressFamily.InterNetworkV6; // Read in local address information row.Local = Utils.BufferToIPv6EndPoint(buffer, ref nOffset); // Read in remote address information row.Remote = Utils.BufferToIPv6EndPoint(buffer, ref nOffset); // State int state = BitConverter.ToInt32(buffer, nOffset); row.iState = state; nOffset += 4; // PID row.PID = BitConverter.ToInt32(buffer, nOffset); nOffset += 4; // Add row to list of connections Add(row); } }
public void GetTcpConnections() { byte[] buffer = new byte[20000]; // Start with 20.000 bytes left for information about tcp table int pdwSize = 20000; int res = IPHlpAPI32Wrapper.GetTcpTable(buffer, out pdwSize, true); if (res != Utils.NO_ERROR) { buffer = new byte[pdwSize]; res = IPHlpAPI32Wrapper.GetTcpTable(buffer, out pdwSize, true); if (res != 0) { return; // Error. You should handle it } } TcpConnections = new IpHlpApidotnet.MIB_TCPTABLE(); int nOffset = 0; // number of entry in the TcpConnections.dwNumEntries = Convert.ToInt32(buffer[nOffset]); nOffset += 4; TcpConnections.table = new MIB_TCPROW[TcpConnections.dwNumEntries]; for (int i = 0; i < TcpConnections.dwNumEntries; i++) { // state int st = Convert.ToInt32(buffer[nOffset]); // state in string //((MIB_TCPROW)(TcpConnections.table[i])).StrgState = Utils.StateToStr(st); (TcpConnections.table[i]).StrgState = Utils.StateToStr(st); // state by ID //((MIB_TCPROW)(TcpConnections.table[i])).iState = st; (TcpConnections.table[i]).iState = st; nOffset += 4; // local address //((MIB_TCPROW)(TcpConnections.table[i])).Local = Utils.BufferToIPEndPoint(buffer, ref nOffset, false); (TcpConnections.table[i]).Local = Utils.BufferToIPEndPoint(buffer, ref nOffset, false); // remote address //((MIB_TCPROW)(TcpConnections.table[i])).Remote = Utils.BufferToIPEndPoint(buffer, ref nOffset, true); (TcpConnections.table[i]).Remote = Utils.BufferToIPEndPoint(buffer, ref nOffset, true); } }
public static List <TCPUDPConnection> GetTcpConnections() { int AF_INET = 2; // IP_v4 int buffSize = 20000; byte[] buffer = new byte[buffSize]; int res = IPHlpAPI32Wrapper.GetExtendedTcpTable(buffer, out buffSize, true, AF_INET, TCP_TABLE_CLASS.TCP_TABLE_OWNER_PID_ALL, 0); if (res != Utils.NO_ERROR) //If there is no enouth memory to execute function { buffer = new byte[buffSize]; res = IPHlpAPI32Wrapper.GetExtendedTcpTable(buffer, out buffSize, true, AF_INET, TCP_TABLE_CLASS.TCP_TABLE_OWNER_PID_ALL, 0); if (res != Utils.NO_ERROR) { throw new Exception("Couldn't call win API, because we couldn't build a large enough buffer"); } } var ourReturn = new List <TCPUDPConnection>(); int nOffset = 0; // number of entry in the int NumEntries = Convert.ToInt32(buffer[nOffset]); Console.WriteLine("there are {0} entries", NumEntries); nOffset += 4; for (int i = 0; i < NumEntries; i++) { // state int st = Convert.ToInt32(buffer[nOffset]); var row = new TCPUDPConnection(null); row.iState = st; nOffset += 4; row.Protocol = Protocol.TCP; row.Local = Utils.BufferToIPEndPoint(buffer, ref nOffset, false); row.Remote = Utils.BufferToIPEndPoint(buffer, ref nOffset, true); row.PID = Utils.BufferToInt(buffer, ref nOffset); ourReturn.Add(row); } return(ourReturn); }
public void GetTcpConnections() { int AF_INET = 2; // IP_v4 int buffSize = 20000; byte[] buffer = new byte[buffSize]; int res = IPHlpAPI32Wrapper.GetExtendedTcpTable(buffer, out buffSize, true, AF_INET, TCP_TABLE_CLASS.TCP_TABLE_OWNER_PID_ALL, 0); if (res != Utils.NO_ERROR) //If there is no enouth memory to execute function { buffer = new byte[buffSize]; res = IPHlpAPI32Wrapper.GetExtendedTcpTable(buffer, out buffSize, true, AF_INET, TCP_TABLE_CLASS.TCP_TABLE_OWNER_PID_ALL, 0); if (res != Utils.NO_ERROR) { return; } } int nOffset = 0; // number of entry in the int NumEntries = Convert.ToInt32(buffer[nOffset]); Console.WriteLine("there are {0} entries", NumEntries); nOffset += 4; for (int i = 0; i < NumEntries; i++) { // state int st = Convert.ToInt32(buffer[nOffset]); var nextPosOffset = nOffset + 4; var row = new TCPUDPConnection(this) { iState = st, Protocol = Protocol.TCP, Local = Utils.BufferToIPEndPoint(buffer, ref nextPosOffset, false), Remote = Utils.BufferToIPEndPoint(buffer, ref nextPosOffset, true), PID = Utils.BufferToInt(buffer, ref nextPosOffset) }; this.Add(row); } }
public void GetTcpConnections() { int AF_INET = 2; // IP_v4 int buffSize = 20000; byte[] buffer = new byte[buffSize]; int res = IPHlpAPI32Wrapper.GetExtendedTcpTable(buffer, out buffSize, true, AF_INET, TCP_TABLE_CLASS.TCP_TABLE_OWNER_PID_ALL, 0); if (res != Utils.NO_ERROR) //If there is no enouth memory to execute function { buffer = new byte[buffSize]; res = IPHlpAPI32Wrapper.GetExtendedTcpTable(buffer, out buffSize, true, AF_INET, TCP_TABLE_CLASS.TCP_TABLE_OWNER_PID_ALL, 0); if (res != Utils.NO_ERROR) { return; } } int nOffset = 0; // number of entry in the int NumEntries = Convert.ToInt32(buffer[nOffset]); nOffset += 4; for (int i = 0; i < NumEntries; i++) { TCPUDPConnection row = new TCPUDPConnection(this); // state int st = Convert.ToInt32(buffer[nOffset]); // state by ID row.iState = st; nOffset += 4; row.Protocol = Protocol.TCP; row.AddressFamily = AddressFamily.InterNetwork; row.Local = Utils.BufferToIPEndPoint(buffer, ref nOffset, false); row.Remote = Utils.BufferToIPEndPoint(buffer, ref nOffset, true); row.PID = Utils.BufferToInt(buffer, ref nOffset); this.Add(row); } }
//IPv6 Version public void GetUdp6Connections() { int AF_INET = (int)AddressFamily.InterNetworkV6; // IP_v6 int buffSize = 20000; byte[] buffer = new byte[buffSize]; int res = IPHlpAPI32Wrapper.GetExtendedUdpTable(buffer, out buffSize, true, AF_INET, UDP_TABLE_CLASS.UDP_TABLE_OWNER_PID, 0); if (res != Utils.NO_ERROR) { buffer = new byte[buffSize]; res = IPHlpAPI32Wrapper.GetExtendedUdpTable(buffer, out buffSize, true, AF_INET, UDP_TABLE_CLASS.UDP_TABLE_OWNER_PID, 0); if (res != Utils.NO_ERROR) { return; } } int nOffset = 0; int NumEntries = Convert.ToInt32(buffer[nOffset]); nOffset += 4; for (int i = 0; i < NumEntries; i++) { TCPUDPConnection row = new TCPUDPConnection(this); row.Protocol = Protocol.UDP; row.AddressFamily = AddressFamily.InterNetworkV6; // Read in local address information row.Local = Utils.BufferToIPv6EndPoint(buffer, ref nOffset); // PID row.PID = BitConverter.ToInt32(buffer, nOffset); nOffset += 4; this.Add(row); } }
public void GetExUdpConnections() { // the size of the MIB_EXTCPROW struct = 4*DWORD int rowsize = 12; int BufferSize = 100000; // allocate a dumb memory space in order to retrieve nb of connection IntPtr lpTable = Marshal.AllocHGlobal(BufferSize); //getting infos int res = IPHlpAPI32Wrapper.AllocateAndGetUdpExTableFromStack(ref lpTable, true, IPHlpAPI32Wrapper.GetProcessHeap(), 0, 2); if (res != Utils.NO_ERROR) { Debug.WriteLine("Error : " + IPHlpAPI32Wrapper.GetAPIErrorMessageDescription(res) + " " + res); return; // Error. You should handle it } int CurrentIndex = 0; //get the number of entries in the table int NumEntries = (int)Marshal.ReadIntPtr(lpTable); lpTable = IntPtr.Zero; // free allocated space in memory Marshal.FreeHGlobal(lpTable); /////////////////// // calculate the real buffer size nb of entrie * size of the struct for each entrie(24) + the dwNumEntries BufferSize = (NumEntries * rowsize) + 4; // make the struct to hold the resullts UdpExConnections = new IpHlpApidotnet.MIB_EXUDPTABLE(); // Allocate memory lpTable = Marshal.AllocHGlobal(BufferSize); res = IPHlpAPI32Wrapper.AllocateAndGetUdpExTableFromStack(ref lpTable, true, IPHlpAPI32Wrapper.GetProcessHeap(), 0, 2); if (res != Utils.NO_ERROR) { Debug.WriteLine("Error : " + IPHlpAPI32Wrapper.GetAPIErrorMessageDescription(res) + " " + res); return; // Error. You should handle it } // New pointer of iterating throught the data IntPtr current = lpTable; CurrentIndex = 0; // get the (again) the number of entries NumEntries = (int)Marshal.ReadIntPtr(current); UdpExConnections.dwNumEntries = NumEntries; // Make the array of entries UdpExConnections.table = new MIB_EXUDPROW[NumEntries]; // iterate the pointer of 4 (the size of the DWORD dwNumEntries) CurrentIndex += 4; current = (IntPtr)((int)current + CurrentIndex); // for each entries for (int i = 0; i < NumEntries; i++) { // get the local address of the connection UInt32 localAddr = (UInt32)Marshal.ReadIntPtr(current); // iterate the pointer of 4 current = (IntPtr)((int)current + 4); // get the local port of the connection UInt32 localPort = (UInt32)Marshal.ReadIntPtr(current); // iterate the pointer of 4 current = (IntPtr)((int)current + 4); // Store the local endpoint in the struct and convertthe port in decimal (ie convert_Port()) UdpExConnections.table[i].Local = new IPEndPoint(localAddr, Utils.ConvertPort(localPort)); // store the process ID UdpExConnections.table[i].dwProcessId = (int)Marshal.ReadIntPtr(current); // Store and get the process name in the struct UdpExConnections.table[i].ProcessName = Utils.GetProcessNameByPID(UdpExConnections.table[i].dwProcessId); current = (IntPtr)((int)current + 4); } // free the buffer Marshal.FreeHGlobal(lpTable); // re init the pointer current = IntPtr.Zero; }
public void GetUdpStats() { UdpStats = new MIB_UDPSTATS(); IPHlpAPI32Wrapper.GetUdpStatistics(ref UdpStats); }
public void GetTcpStats() { TcpStats = new MIB_TCPSTATS(); IPHlpAPI32Wrapper.GetTcpStatistics(ref TcpStats); }
public string dumpRouteEntry(IPHlpAPI32Wrapper.MIB_IPFORWARDROW row){ adapterIDs idList = new adapterIDs(); //Active Routes: // 1 2 3 4 5 6 7 8 //1234567890123456789012345678901234567890123456789012345678901234567890123456789012 //Network Destination Netmask Gateway Interface Metric string s = ""; s += (new IPAddress(row.dwForwardDest)).ToString().PadRight(27); s += (new IPAddress(row.dwForwardMask)).ToString().PadRight(17); s += (new IPAddress(row.dwForwardNextHop)).ToString().PadRight(17); //interface //s += "".PadLeft(6) + row.dwForwardIfIndex.ToString("000") + " "; s += "".PadLeft(2) + idList.getIPstrForID((int)row.dwForwardIfIndex).PadLeft(17) ; //metric s += row.dwForwardMetric1.ToString().PadLeft(5); return s; }
public void GetTcpConnexions() { byte[] buffer = new byte[20000]; // Start with 20.000 bytes left for information about tcp table int pdwSize = 20000; int res = IPHlpAPI32Wrapper.GetTcpTable(buffer, out pdwSize, true); if (res != NO_ERROR) { buffer = new byte[pdwSize]; res = IPHlpAPI32Wrapper.GetTcpTable(buffer, out pdwSize, true); if (res != 0) { return; // Error. You should handle it } } TcpConnexion = new IpHlpApidotnet.MIB_TCPTABLE(); int nOffset = 0; // number of entry in the TcpConnexion.dwNumEntries = Convert.ToInt32(buffer[nOffset]); nOffset += 4; TcpConnexion.table = new MIB_TCPROW[TcpConnexion.dwNumEntries]; for (int i = 0; i < TcpConnexion.dwNumEntries; i++) { // state int st = Convert.ToInt32(buffer[nOffset]); // state in string ((MIB_TCPROW)(TcpConnexion.table[i])).StrgState = convert_state(st); // state by ID ((MIB_TCPROW)(TcpConnexion.table[i])).iState = st; nOffset += 4; // local address string LocalAdrr = buffer[nOffset].ToString() + "." + buffer[nOffset + 1].ToString() + "." + buffer[nOffset + 2].ToString() + "." + buffer[nOffset + 3].ToString(); nOffset += 4; //local port in decimal int LocalPort = (((int)buffer[nOffset]) << 8) + (((int)buffer[nOffset + 1])) + (((int)buffer[nOffset + 2]) << 24) + (((int)buffer[nOffset + 3]) << 16); nOffset += 4; // store the remote endpoint ((MIB_TCPROW)(TcpConnexion.table[i])).Local = new IPEndPoint(IPAddress.Parse(LocalAdrr), LocalPort); // remote address string RemoteAdrr = buffer[nOffset].ToString() + "." + buffer[nOffset + 1].ToString() + "." + buffer[nOffset + 2].ToString() + "." + buffer[nOffset + 3].ToString(); nOffset += 4; // if the remote address = 0 (0.0.0.0) the remote port is always 0 // else get the remote port in decimal int RemotePort; // if (RemoteAdrr == "0.0.0.0") { RemotePort = 0; } else { RemotePort = (((int)buffer[nOffset]) << 8) + (((int)buffer[nOffset + 1])) + (((int)buffer[nOffset + 2]) << 24) + (((int)buffer[nOffset + 3]) << 16); } nOffset += 4; ((MIB_TCPROW)(TcpConnexion.table[i])).Remote = new IPEndPoint(IPAddress.Parse(RemoteAdrr), RemotePort); } }
public void getRoutingTable() { _routeEntry.Clear(); IPHlpAPI32Wrapper.MIB_IPFORWARDTABLE fTable = new IPHlpAPI32Wrapper.MIB_IPFORWARDTABLE(); fTable.table = new IPHlpAPI32Wrapper.MIB_IPFORWARDROW[1]; int MIB_SIZE = Marshal.SizeOf(new IPHlpAPI32Wrapper.MIB_IPFORWARDROW()) + Marshal.SizeOf(new UInt32()); // Initialize unmanged memory to hold one struct entry. IntPtr pTable = Marshal.AllocHGlobal(MIB_SIZE); try { // Copy the struct to unmanaged memory. // Marshal.StructureToPtr(fTable, pTable, false); //StructureToPtr does not like array inside struct byte[] buffer = new byte[MIB_SIZE]; int dwSize = MIB_SIZE; int iRes = IPHlpAPI32Wrapper.GetIpForwardTable(buffer, ref dwSize, true); if (iRes == ERROR_INSUFFICIENT_BUFFER) { //need to get dwSize memory allocated buffer = new byte[dwSize]; iRes = IPHlpAPI32Wrapper.GetIpForwardTable(buffer, ref dwSize, true); //now we have a list of structs //see also http://www.pcreview.co.uk/forums/changing-route-table-iphlpapi-dll-and-p-invoke-t2891061.html //now we have the buffer filled, create an empty entry IPHlpAPI32Wrapper.MIB_IPFORWARDTABLE ipForwardTable = new IPHlpAPI32Wrapper.MIB_IPFORWARDTABLE(); // ...and fill the MIB_IPFORWARDTABLE with the bytes from the buffer int nOffset = 0; // number of rows in the table ipForwardTable.dwNumEntries = Convert.ToUInt32(buffer[nOffset]); nOffset += 4; ipForwardTable.table = new IPHlpAPI32Wrapper.MIB_IPFORWARDROW[ipForwardTable.dwNumEntries]; for (int i = 0; i < ipForwardTable.dwNumEntries; i++) { //dwForwardDest //((MIB_IPFORWARDROW)(ipForwardTable.table[i])).dwForwardDest = Convert.ToUInt32(buffer[nOffset]); ((ipForwardTable.table[i])).dwForwardDest = toIPInt(buffer, nOffset); nOffset += 4; //dwForwardMask ((ipForwardTable.table[i])).dwForwardMask = toIPInt(buffer, nOffset); nOffset += 4; //dwForwardPolicy ((ipForwardTable.table[i])).dwForwardPolicy = Convert.ToUInt32(buffer[nOffset]); nOffset += 4; //dwForwardNextHop ((ipForwardTable.table[i])).dwForwardNextHop = toIPInt(buffer, nOffset); nOffset += 4; //dwForwardIfIndex ((ipForwardTable.table[i])).dwForwardIfIndex = Convert.ToUInt32(buffer[nOffset]); nOffset += 8; //8 since we're skipping the next item (dwForwardType) //dwForwardProto ((ipForwardTable.table[i])).dwForwardProto = Convert.ToUInt32(buffer[nOffset]); nOffset += 4; _routeEntry.Add(ipForwardTable.table[i]); } } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("Exception: " + ex.Message); } finally { Marshal.FreeHGlobal(pTable); } }