Ejemplo n.º 1
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)
        {
            var res = GetBestRoute(IpToUint(destAddr), IpToUint(sourceAddr), out IPFORWARDROW pBestRoute);

            bestRoute = new IPForwardRow(pBestRoute);
            return(res);
        }
Ejemplo n.º 2
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)
            {
                int res = GetIpInterfaceEntry(ifIndex, out MIB_IPINTERFACE_ROW row);
                if (res != 0)
                {
                    return(res);
                }
                route.Metric = (int)row.Metric;
            }

            return(CreateIpForwardEntry(route));
        }
Ejemplo n.º 3
0
        /// <summary>取得路由表。</summary>
        /// <param name="ipForwardTable">[OUT]傳回的路由表。</param>
        /// <returns></returns>
        public static int GetIpForwardTable(out IPForwardRow[] ipForwardTable)
        {
            var fwdTable = IntPtr.Zero;
            int size     = 0;
            var result   = GetIpForwardTable(fwdTable, ref size, true);

            fwdTable = Marshal.AllocHGlobal(size);
            result   = GetIpForwardTable(fwdTable, ref size, true);
            if (result == 0)
            {
                var res = (IPForwardTable)Marshal.PtrToStructure(fwdTable, typeof(IPForwardTable));
                ipForwardTable = new IPForwardRow[res.Size];
                IntPtr p = new IntPtr(fwdTable.ToInt64() + Marshal.SizeOf(res.Size));
                for (int i = 0; i < res.Size; i++)
                {
                    ipForwardTable[i] = new IPForwardRow((IPFORWARDROW)Marshal.PtrToStructure(p, typeof(IPFORWARDROW)));
                    p = new IntPtr(p.ToInt64() + Marshal.SizeOf(typeof(IPFORWARDROW)));
                }
            }
            else
            {
                ipForwardTable = null;
            }

            Marshal.FreeHGlobal(fwdTable);

            return(result);
        }
Ejemplo n.º 4
0
 /// <summary>刪除路由</summary>
 /// <param name="route"></param>
 /// <returns></returns>
 public static int DeleteIpForwardEntry(IPForwardRow route)
 {
     return(DeleteIpForwardEntry(route.GetBaseStruct()));
 }