Esempio n. 1
0
        /// <summary>
        /// 创建路由
        /// </summary>
        /// <param name="dest"></param>
        /// <param name="mask"></param>
        /// <param name="nextHop"></param>
        /// <param name="ifIndex"></param>
        /// <param name="metric"></param>
        /// <returns></returns>
        public static int CreateIpForwardEntry(IPAddress dest, IPAddress mask, IPAddress nextHop, uint ifIndex, int metric = 1)
        {
            IPForwardRow route = new IPForwardRow()
            {
                Dest      = dest,
                Mask      = mask,
                NextHop   = nextHop,
                IfIndex   = ifIndex,
                Metric    = metric,
                Policy    = 0,
                Type      = MIB_IPFORWARD_TYPE.MIB_IPROUTE_TYPE_DIRECT,
                Proto     = MIB_IPPROTO.MIB_IPPROTO_NETMGMT,
                Age       = 0,
                NextHopAS = 0
            };

            OperatingSystem os = Environment.OSVersion;

            if (os.Platform == PlatformID.Win32NT && os.Version.Major >= 6)
            {
                MIB_IPINTERFACE_ROW row;
                int res = GetIpInterfaceEntry(ifIndex, out row);
                if (res != 0)
                {
                    return(res);
                }
                route.Metric = (int)row.Metric;
            }

            return(CreateIpForwardEntry(route));
        }
Esempio n. 2
0
        /// <summary>
        /// 获取路由表
        /// </summary>
        /// <param name="ipForwardTable"></param>
        /// <returns></returns>
        public unsafe static int GetIpForwardTable(out IPForwardRow[] ipForwardTable)
        {
            int res = 0, size = 0;

            size = Marshal.SizeOf(typeof(MIB_IPFORWARDROW));
            MIB_IPFORWARDTABLE *table = (MIB_IPFORWARDTABLE *)Marshal.AllocHGlobal(size);

            res = GetIpForwardTable(table, ref size, true);
            if (res == 0x7A)
            {
                table = (MIB_IPFORWARDTABLE *)Marshal.ReAllocHGlobal((IntPtr)table, (IntPtr)size);
                res   = GetIpForwardTable(table, ref size, true);
            }

            if (res == 0)
            {
                ipForwardTable = new IPForwardRow[(*table).dwNumEntries];
                for (int i = 0; i < ipForwardTable.Length; i++)
                {
                    ipForwardTable[i] = new IPForwardRow((&(*table).table)[i]);
                }
            }
            else
            {
                ipForwardTable = null;
            }

            Marshal.FreeHGlobal((IntPtr)table);


            return(res);
        }
Esempio n. 3
0
        /// <summary>
        /// 获取基础路由
        /// </summary>
        /// <param name="destAddr"></param>
        /// <param name="sourceAddr"></param>
        /// <param name="bestRoute"></param>
        /// <returns></returns>
        public static int GetBestRoute(IPAddress destAddr, IPAddress sourceAddr, out IPForwardRow bestRoute)
        {
            MIB_IPFORWARDROW pBestRoute;
            var res = GetBestRoute(IpToUint(destAddr), IpToUint(sourceAddr), out pBestRoute);

            bestRoute = new IPForwardRow(pBestRoute);
            return(res);
        }
Esempio n. 4
0
        /// <summary>
        /// 获取单条路由信息
        /// </summary>
        /// <param name="destAddr"></param>
        /// <param name="route"></param>
        /// <returns></returns>
        public unsafe static int GetIpForwardEntry(IPAddress destAddr, out IPForwardRow route)
        {
            route = null;

            IPForwardRow[] ipForwardTable;
            var            res = GetIpForwardTable(out ipForwardTable);

            if (res == 0)
            {
                for (int i = 0; i < ipForwardTable.Length; i++)
                {
                    if (ipForwardTable[i].Dest.Equals(destAddr))
                    {
                        route = ipForwardTable[i];
                        break;
                    }
                }
            }

            return(res);
        }
Esempio n. 5
0
 /// <summary>
 /// 删除路由
 /// </summary>
 /// <param name="route"></param>
 /// <returns></returns>
 public static int DeleteIpForwardEntry(IPForwardRow route)
 {
     return(DeleteIpForwardEntry(route.GetBaseStruct()));
 }