Exemplo n.º 1
0
        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);
            }
        }
Exemplo n.º 2
0
        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);
            }
        }