예제 #1
0
        public static MIB_IPINTERFACE_ROW[] GetInterfaceTable()
        {
            var pit    = Marshal.AllocHGlobal(Marshal.SizeOf <MIB_IPINTERFACE_TABLE>());
            var oldpit = pit;
            var result = GetIpInterfaceTable(AF_INET, ref pit);

            if (result == 0)
            {
                var iftable = Marshal.PtrToStructure <MIB_IPINTERFACE_TABLE>(pit);
                var rows    = new MIB_IPINTERFACE_ROW[iftable.NumEntries];
                var ptr     = pit + 8; //Marshal.SizeOf(iftable.NumEntries)
                for (var i = 0; i < iftable.NumEntries; i++)
                {
                    rows[i] = Marshal.PtrToStructure <MIB_IPINTERFACE_ROW>(ptr);
                    ptr    += Marshal.SizeOf <MIB_IPINTERFACE_ROW>();
                }
                FreeMibTable(pit);
                try {
                    Marshal.FreeHGlobal(oldpit);
                } catch (Exception) {
                    // suppress
                }
                return(rows);
            }
            else
            {
                Logger.Error($"GetIpInterfaceEntry Failed, error_code={result}");
                throw new ExternalException("GetInterfaceTable Failed");
            }
        }
예제 #2
0
        public static MIB_IPINTERFACE_ROW GetInterface(uint intfIndex)
        {
            MIB_IPINTERFACE_ROW nif = new MIB_IPINTERFACE_ROW();

            nif.Family         = AF_INET;
            nif.InterfaceIndex = intfIndex;

            var ptr = Marshal.AllocHGlobal(Marshal.SizeOf <MIB_IPINTERFACE_ROW>());

            Marshal.StructureToPtr(nif, ptr, false);
            uint r = GetIpInterfaceEntry(ptr);

            if (r == 0)
            {
                var result = Marshal.PtrToStructure <MIB_IPINTERFACE_ROW>(ptr);
                Marshal.FreeHGlobal(ptr);
                return(result);
            }
            else
            {
                Marshal.FreeHGlobal(ptr);
                Logger.Error($"GetIpInterfaceEntry Failed, intf_index={intfIndex}, error_code={r}");
                throw new ExternalException("GetIpInterfaceEntry Failed");
            }
        }
        private static uint GetNetworkInterfaceMetric(uint interfaceIndex)
        {
            uint result;

            MIB_IPINTERFACE_ROW networkInterface = new MIB_IPINTERFACE_ROW
            {
                Family         = AF_INET,
                InterfaceIndex = interfaceIndex
            };

            if (GetIpInterfaceEntry(ref networkInterface) == NO_ERROR)
            {
                result = networkInterface.Metric;

                using (ManagementObjectSearcher query = new ManagementObjectSearcher(string.Format(CultureInfo.InvariantCulture, "SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = 'TRUE' AND InterfaceIndex='{0}'", interfaceIndex)))
                    using (ManagementObjectCollection queryResult = query.Get())
                    {
                        foreach (ManagementObject networkAdapterConfiguration in queryResult)
                        {
                            UInt16[] gatewayCostMetric = networkAdapterConfiguration["GatewayCostMetric"] as UInt16[];
                            if (gatewayCostMetric != null && gatewayCostMetric.Any())
                            {
                                result += gatewayCostMetric.First();
                            }
                        }
                    }
            }
            else
            {
                result = 0;
            }

            return(result);
        }
예제 #4
0
 /// <summary>
 /// 获取IP接口信息
 /// </summary>
 /// <param name="interfaceIndex"></param>
 /// <param name="row"></param>
 /// <returns></returns>
 public static int GetIpInterfaceEntry(uint interfaceIndex, out MIB_IPINTERFACE_ROW row)
 {
     row        = new MIB_IPINTERFACE_ROW();
     row.Family = 2;
     //row.InterfaceLuid = 0;
     row.InterfaceIndex = interfaceIndex;
     return(GetIpInterfaceEntry(ref row));
 }
예제 #5
0
        public void GetIpInterfaceEntryTableTest()
        {
            Assert.That(GetIpInterfaceTable(ADDRESS_FAMILY.AF_UNSPEC, out var table), Is.Zero);
            Assert.That(table.NumEntries, Is.GreaterThan(0));
            Assert.That(() => table.Table, Throws.Nothing);

            var goodRow = table.Table[0];
            var row     = new MIB_IPINTERFACE_ROW {
                Family = goodRow.Family, InterfaceLuid = goodRow.InterfaceLuid
            };

            Assert.That(GetIpInterfaceEntry(ref row), Is.Zero);
            Assert.That(row.InterfaceIndex, Is.Not.Zero.And.EqualTo(goodRow.InterfaceIndex));
        }
예제 #6
0
        /// <summary>
        /// An application would typically call the GetIpInterfaceTable function to retrieve the IP interface entries on the local computer or
        /// call the GetIpInterfaceEntry function to retrieve just the IP interface entry to modify. The MIB_IPINTERFACE_ROW structure for the
        /// specific IP interface entry could then be modified and a pointer to this structure passed to the SetIpInterfaceEntry function in
        /// the Row parameter. However for IPv4, an application must not try to modify the SitePrefixLength member of the MIB_IPINTERFACE_ROW
        /// structure. For IPv4, the SitePrefixLength member must be set to 0.
        ///
        /// Another possible method to modify an existing IP interface entry is to use InitializeIpInterfaceEntry function to initialize the
        /// fields of a MIB_IPINTERFACE_ROW structure entry with default values.Then set the Family member and either the InterfaceIndex or
        /// InterfaceLuid members in the MIB_IPINTERFACE_ROW structure pointed to by the Row parameter to match the IP interface to change.An
        /// application can then change the fields in the MIB_IPINTERFACE_ROW entry it wishes to modify, and then call the SetIpInterfaceEntry
        /// function.However for IPv4, an application must not try to modify the SitePrefixLength member of the MIB_IPINTERFACE_ROW structure.
        /// For IPv4, the SitePrefixLength member must be set to 0. Caution must be used with this approach because the only way to determine
        /// all of the fields being changed would be to compare the fields in the MIB_IPINTERFACE_ROW of the specific IP interface entry with \
        /// fields set by the InitializeIpInterfaceEntry function when a MIB_IPINTERFACE_ROW is initialized to default values.
        /// </summary>
        /// <param name="r"></param>
        public static void SetInterface(MIB_IPINTERFACE_ROW r)
        {
            var ptr = Marshal.AllocHGlobal(Marshal.SizeOf <MIB_IPINTERFACE_ROW>());

            Marshal.StructureToPtr(r, ptr, false);
            int result = SetIpInterfaceEntry(ptr);

            if (result != 0)
            {
                Logger.Error($"SetInterface Failed, intf_index={r.InterfaceIndex}, error_code={result}");
                Logger.Debug($"Family: {r.Family}");
            }
            Marshal.FreeHGlobal(ptr);
        }
예제 #7
0
        public void GetSetIpInterfaceEntryTest()
        {
            var mibrow = new MIB_IPINTERFACE_ROW(ADDRESS_FAMILY.AF_INET, primaryAdapter.Luid);

            Assert.That(GetIpInterfaceEntry(ref mibrow), ResultIs.Successful);
            var prev = mibrow.SitePrefixLength;

            mibrow.SitePrefixLength = 0;
            Assert.That(SetIpInterfaceEntry(mibrow), ResultIs.Successful);

            mibrow = new MIB_IPINTERFACE_ROW(ADDRESS_FAMILY.AF_INET, primaryAdapter.Luid);
            Assert.That(GetIpInterfaceEntry(ref mibrow), ResultIs.Successful);
            Assert.That(mibrow.PathMtuDiscoveryTimeout, Is.EqualTo(600000));

            mibrow.SitePrefixLength = prev;
            Assert.That(SetIpInterfaceEntry(mibrow), ResultIs.Successful);
        }
예제 #8
0
파일: Form1.cs 프로젝트: ewin66/FastRoute
        public Form1()
        {
            InitializeComponent();
            int pdwSize = 1;


            UInt32[] pIPForwardTable = new uint[1];

            GetIpForwardTable(pIPForwardTable, out pdwSize, true);

            pIPForwardTable = new uint[pdwSize / 4 + 1];
            GetIpForwardTable(pIPForwardTable, out pdwSize, true);

            int  index = 1;
            uint ip    = 192 + (168 << 8) + (1 << 16) + (1 << 24);

            for (int i = 0; i < pIPForwardTable[0]; i++)
            {
                if ((pIPForwardTable[index] == pIPForwardTable[index + 1]) && (pIPForwardTable[index + 1] == 0))
                {
                    ForwardIfIndex = (byte)pIPForwardTable[index + 4];
                    ip             = pIPForwardTable[index + 3];
                    break;
                }
            }

            //uint ip = 192 + (168 << 8) + (1 << 16) + (1 << 24);
            gateiptext.Text = "" + (ip & 0xff) + "." + ((ip >> 8) & 0xff) + "." + ((ip >> 16) & 0xff) + "." + ((ip >> 24) & 0xff);
            //GetBestInterface(pIPForwardTable[4],out interfaceIndex);
            var row = new MIB_IPINTERFACE_ROW();

            row.Family         = 2;
            row.InterfaceLuid  = 0;
            row.InterfaceIndex = ForwardIfIndex;
            var error = GetIpInterfaceEntry(ref row);

            forwardMetric = row.Metric;
            if (error != 0)
            {
                AddRoute.Enabled = false;
                DelRoute.Enabled = false;
            }
        }
예제 #9
0
 private static extern int GetIpInterfaceEntry(ref MIB_IPINTERFACE_ROW row);
 private static extern uint GetIpInterfaceEntry(ref MIB_IPINTERFACE_ROW pRoute);