Esempio n. 1
0
        public static void CreateRoute(string destination, string mask, string gateway, uint interfaceIndex, int metric)
        {
            MibIPForwardRow route = new MibIPForwardRow
            {
                DwForwardDest      = BitConverter.ToUInt32(IPAddress.Parse(destination).GetAddressBytes(), 0),
                DwForwardMask      = BitConverter.ToUInt32(IPAddress.Parse(mask).GetAddressBytes(), 0),
                DwForwardNextHop   = BitConverter.ToUInt32(IPAddress.Parse(gateway).GetAddressBytes(), 0),
                DwForwardMetric1   = Convert.ToUInt32(metric),
                DwForwardType      = MibIPRouteTypeDirect,
                DwForwardProto     = MibIPProtoNetmgmt,
                DwForwardAge       = 0,
                DwForwardPolicy    = 0,
                DwForwardNextHopAS = 0,
                DwForwardIfIndex   = Convert.ToUInt32(interfaceIndex)
            };

            IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(MibIPForwardRow)));

            try
            {
                Marshal.StructureToPtr(route, ptr, false);
                PInvoke.CreateIpForwardEntry(ptr);
            }
            finally
            {
                Marshal.FreeHGlobal(ptr);
            }
        }
Esempio n. 2
0
        public static IPForwardTable ReadIPForwardTable(IntPtr tablePtr)
        {
            IPForwardTable result = (IPForwardTable)Marshal.PtrToStructure(tablePtr, typeof(IPForwardTable));

            MibIPForwardRow[] table = new MibIPForwardRow[result.Size];
            IntPtr            p     = new IntPtr(tablePtr.ToInt64() + Marshal.SizeOf(result.Size));

            for (int i = 0; i < result.Size; ++i)
            {
                table[i] = (MibIPForwardRow)Marshal.PtrToStructure(p, typeof(MibIPForwardRow));
                p        = new IntPtr(p.ToInt64() + Marshal.SizeOf(typeof(MibIPForwardRow)));
            }
            result.Table = table;

            return(result);
        }