Exemplo n.º 1
0
        public static bool RouteExists(string destinationIP)
        {
            List <Ip4RouteEntry> routeTable = Ip4RouteTable.GetRouteTable();
            Ip4RouteEntry        routeEntry = routeTable.Find(i => i.DestinationIP.ToString().Equals(destinationIP));

            return(routeEntry != null);
        }
Exemplo n.º 2
0
        public static List <Ip4RouteEntry> GetRouteTable()
        {
            var fwdTable = IntPtr.Zero;
            int size     = 0;
            var result   = NativeMethods.GetIpForwardTable(fwdTable, ref size, true);

            fwdTable = Marshal.AllocHGlobal(size);

            result = NativeMethods.GetIpForwardTable(fwdTable, ref size, true);

            var forwardTable = NativeMethods.ReadIPForwardTable(fwdTable);

            Marshal.FreeHGlobal(fwdTable);


            List <Ip4RouteEntry> routeTable = new List <Ip4RouteEntry>();

            for (int i = 0; i < forwardTable.Table.Length; ++i)
            {
                Ip4RouteEntry entry = new Ip4RouteEntry();
                entry.DestinationIP   = new IPAddress((long)forwardTable.Table[i].dwForwardDest);
                entry.SubnetMask      = new IPAddress((long)forwardTable.Table[i].dwForwardMask);
                entry.GatewayIP       = new IPAddress((long)forwardTable.Table[i].dwForwardNextHop);
                entry.InterfaceIndex  = Convert.ToInt32(forwardTable.Table[i].dwForwardIfIndex);
                entry.ForwardType     = Convert.ToInt32(forwardTable.Table[i].dwForwardType);
                entry.ForwardProtocol = Convert.ToInt32(forwardTable.Table[i].dwForwardProto);
                entry.ForwardAge      = Convert.ToInt32(forwardTable.Table[i].dwForwardAge);
                entry.Metric          = Convert.ToInt32(forwardTable.Table[i].dwForwardMetric1);
                routeTable.Add(entry);
            }
            return(routeTable);
        }
Exemplo n.º 3
0
        public static bool InterfaceIndexExists(int interfaceIndex)
        {
            List <Ip4RouteEntry> routeTable = Ip4RouteTable.GetRouteTable();
            Ip4RouteEntry        routeEntry = routeTable.Find(i => i.InterfaceIndex.Equals(interfaceIndex));

            return(routeEntry != null);
        }
Exemplo n.º 4
0
        public static void DeleteRoute(Ip4RouteEntry routeEntry)
        {
            var route = new NativeMethods.MIB_IPFORWARDROW
            {
                dwForwardDest    = BitConverter.ToUInt32(IPAddress.Parse(routeEntry.DestinationIP.ToString()).GetAddressBytes(), 0),
                dwForwardMask    = BitConverter.ToUInt32(IPAddress.Parse(routeEntry.SubnetMask.ToString()).GetAddressBytes(), 0),
                dwForwardNextHop = BitConverter.ToUInt32(IPAddress.Parse(routeEntry.GatewayIP.ToString()).GetAddressBytes(), 0),
                dwForwardMetric1 = 99,
                dwForwardType    = Convert.ToUInt32(3), //Default to 3
                dwForwardProto   = Convert.ToUInt32(3), //Default to 3
                dwForwardAge     = 0,
                dwForwardIfIndex = Convert.ToUInt32(routeEntry.InterfaceIndex)
            };

            IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(NativeMethods.MIB_IPFORWARDROW)));

            try
            {
                Marshal.StructureToPtr(route, ptr, false);
                var status = NativeMethods.DeleteIpForwardEntry(ptr);
            }
            finally
            {
                Marshal.FreeHGlobal(ptr);
            }
        }