/// <summary> /// Retrieves the IPv4 routing table /// </summary> /// <param name="forwardTable">The IPv4 routing table</param> /// <exception cref="OutOfMemoryException">Could not allocate a buffer that can store the routing table</exception> /// <exception cref="EmptyRouteTableException">There are no routes in the routing table</exception> /// <exception cref="NotSupportedException">There is no IP stack installed</exception> /// <exception cref="Win32Exception">Unexpected error</exception> private static void GetForwardTable(out IpForwardRow[] forwardTable) { IntPtr buffer = IntPtr.Zero; IntPtr bufferSize = IntPtr.Zero; try { var status = NativeLibrary.IPHelper.GetIpForwardTable(buffer, ref bufferSize, false); if (status == Error.InsufficientBuffer) { buffer = Marshal.AllocHGlobal(bufferSize); status = NativeLibrary.IPHelper.GetIpForwardTable(buffer, ref bufferSize, false); } if (status != Error.None) { if (buffer != IntPtr.Zero) { Marshal.FreeHGlobal(buffer); } buffer = IntPtr.Zero; if (status == Error.NoData) { throw new EmptyRouteTableException(); } if (status == Error.NotSupported) { throw new NotSupportedException("There is no IP stack installed on the local computer."); } throw new Win32Exception(status); } var size = (uint)Marshal.ReadInt32(buffer); forwardTable = new IpForwardRow[size]; IntPtr currentPointer = buffer + sizeof(uint); for (int i = 0; i < size; i++) { forwardTable[i] = Marshal.PtrToStructure <IpForwardRow>(currentPointer); currentPointer += Marshal.SizeOf <IpForwardRow>(); } } finally { if (buffer != IntPtr.Zero) { Marshal.FreeHGlobal(buffer); } } }
/// <summary> /// Retrieves the interface to IP address mapping table. /// </summary> /// <returns>A table that describes the mapping between IP addresses and adapters.</returns> internal static IpForwardRow[] GetIpForwardTable() { IpForwardRow[] rows = null; // figure out how big of a buffer we need uint size = 0; int ret = _GetIpForwardTable(IntPtr.Zero, ref size, true); // Check to see if the call returned a valid result if (ret != (int)IPErrorCodes.ERROR_SUCCESS && ret != (int)IPErrorCodes.ERROR_INSUFFICIENT_BUFFER) { Debug.Assert(false, @"Error calling GetIpForwardTable(): " + ret); return(null); } // allocate the ip address table buffer IntPtr buf = Marshal.AllocHGlobal((int)size); try { // Get the ip address table ret = _GetIpForwardTable(buf, ref size, true); // Check to see if the call returned a valid result if (ret != (int)IPErrorCodes.ERROR_SUCCESS) { Debug.Assert(false, @"Error calling GetIpForwardTable(): " + ret); return(null); } // Copy the data into a managed array of IpAddressRow structures int numEntries = Marshal.ReadInt32(buf); int pRows = 4 + (int)buf; rows = new IpForwardRow[(int)numEntries]; for (int i = 0; i < numEntries; i++) { rows[i] = (IpForwardRow)Marshal.PtrToStructure((IntPtr)pRows, typeof(IpForwardRow)); pRows += Marshal.SizeOf(typeof(IpForwardRow)); } } finally { // free the buffer Marshal.FreeHGlobal(buf); } return(rows); }
internal static extern int DeleteIpForwardEntry(ref IpForwardRow ipForwardRow);
internal static extern int CreateIpForwardEntry(ref IpForwardRow ipForwardRow);