示例#1
0
            /// <summary>
            /// Get permanent mac address from soft mac bytes
            /// </summary>
            /// <param name="softMacBytes">soft mac byte arrays to be searched</param>
            /// <returns>the permanent mac address bytes according to soft mac, null if not found</returns>
            public static byte[] getPermanentMacFromSoftMac(byte[] softMacBytes)
            {
                MIB_IF_TABLE2 mibTable = MibIFSingleton.Instance.getMIB2Interface();

                MIB_IF_ROW2[] mibRows = (from mibIfRow in mibTable.Table where matchByteArray(softMacBytes, mibIfRow.PhysicalAddress) select mibIfRow).ToArray <MIB_IF_ROW2>();
                try
                {
                    if (null == mibRows || 0 == mibRows.Length)
                    {
                        return(null); // Does not find the permanent mac
                    }
                    else
                    {
                        byte[] matchBytes = mibRows[0].PermanentPhysicalAddress;
                        if (mibRows[0].PhysicalAddressLength != 6)
                        {
                            Debug.Print("found non-ethernet physical address, taken as not found");
                            return(null);
                        }
                        return(matchBytes);
                    }
                }
                catch (Exception e)
                {
                    Debug.Print("get exception: {0}", e);
                    return(null);
                }
            }
示例#2
0
        public static List <NetworkAdapter> GetNetworkAdapters()
        {
            List <NetworkAdapter> resList       = new List <NetworkAdapter>();
            MIB_IF_TABLE2         MIB_IF_TABLE2 = GetMIB2Interface();

            for (int i = 0; i < MIB_IF_TABLE2.NumEntries; i++)
            {
                if (MIB_IF_TABLE2.Table[i].OutOctets == 0 && MIB_IF_TABLE2.Table[i].InOctets == 0)
                {
                    continue;
                }
                var ret = resList.FindLast(x => x.Recv.Equals(MIB_IF_TABLE2.Table[i].InOctets) && x.Send.Equals(MIB_IF_TABLE2.Table[i].OutOctets));
                if (ret != null)
                {
                    continue;
                }
                NetworkAdapter n = new NetworkAdapter();
                n.Send = MIB_IF_TABLE2.Table[i].OutOctets;
                n.Recv = MIB_IF_TABLE2.Table[i].InOctets;
                StringBuilder builder = new StringBuilder();
                for (int j = 0; j < MIB_IF_TABLE2.Table[i].PhysicalAddressLength; j++)
                {
                    builder.Append(string.Format("-{0:X2}", MIB_IF_TABLE2.Table[i].PhysicalAddress[j]));
                }

                n.MAC = builder.ToString().Substring(1);


                resList.Add(n);
            }

            return(resList);
        }
示例#3
0
 /// <summary>
 /// Get single instance
 /// </summary>
 private MibIFSingleton()
 {
     mibTable2 = new MIB_IF_TABLE2();
     bDirty    = true;
 }
示例#4
0
        private static MIB_IF_TABLE2 GetMIB2Interface()
        {
            int pTable = 0;

            byte[]        bin           = null;
            MIB_IF_TABLE2 MIB_IF_TABLE2 = new MIB_IF_TABLE2();
            int           ret           = GetIfTable2(ref pTable);

            if (ret != 0)
            {
                return(MIB_IF_TABLE2);
            }

            int leng = 1352;

            RtlMoveMemory(ref MIB_IF_TABLE2.NumEntries, pTable, 4);
            MIB_IF_TABLE2.Table = new MIB_IF_ROW2[MIB_IF_TABLE2.NumEntries];
            int Address = pTable + 8;

            for (int i = 0; i < MIB_IF_TABLE2.NumEntries; i++)
            {
                RtlMoveMemory(ref MIB_IF_TABLE2.Table[i].InterfaceLuid, Address + (i) * leng, 8);
                RtlMoveMemory(ref MIB_IF_TABLE2.Table[i].InterfaceIndex, Address + (i) * leng + 8, 4);
                MIB_IF_TABLE2.Table[i].GUID = new byte[16];
                RtlMoveMemory(SetHandleCount(MIB_IF_TABLE2.Table[i].GUID), Address + (i) * leng + 12, 16);
                bin = new byte[514];
                RtlMoveMemory(SetHandleCount(bin), Address + (i) * leng + 28, 514);
                MIB_IF_TABLE2.Table[i].Alias = Encoding.Unicode.GetString(bin);
                bin = new byte[514];
                RtlMoveMemory(SetHandleCount(bin), Address + (i) * leng + 542, 514);
                MIB_IF_TABLE2.Table[i].Description = Encoding.Unicode.GetString(bin);
                RtlMoveMemory(ref MIB_IF_TABLE2.Table[i].PhysicalAddressLength, Address + (i) * leng + 1056, 4);
                MIB_IF_TABLE2.Table[i].PhysicalAddress = new byte[32];
                RtlMoveMemory(SetHandleCount(MIB_IF_TABLE2.Table[i].PhysicalAddress), Address + (i) * leng + 1060, 32);
                MIB_IF_TABLE2.Table[i].PermanentPhysicalAddress = new byte[32];
                RtlMoveMemory(SetHandleCount(MIB_IF_TABLE2.Table[i].PermanentPhysicalAddress),
                              Address + (i) * leng + 1092, 32);
                RtlMoveMemory(ref MIB_IF_TABLE2.Table[i].Mtu, Address + (i) * leng + 1124, 4);
                RtlMoveMemory(ref MIB_IF_TABLE2.Table[i].Type, Address + (i) * leng + 1128, 4);
                RtlMoveMemory(ref MIB_IF_TABLE2.Table[i].TunnelType, Address + (i) * leng + 1132, 4);
                RtlMoveMemory(ref MIB_IF_TABLE2.Table[i].MediaType, Address + (i) * leng + 1136, 4);
                RtlMoveMemory(ref MIB_IF_TABLE2.Table[i].PhysicalMediumType, Address + (i) * leng + 1140, 4);
                RtlMoveMemory(ref MIB_IF_TABLE2.Table[i].AccessType, Address + (i) * leng + 1144, 4);
                RtlMoveMemory(ref MIB_IF_TABLE2.Table[i].DirectionType, Address + (i) * leng + 1148, 4);
                MIB_IF_TABLE2.Table[i].InterfaceAndOperStatusFlags = new byte[8];
                RtlMoveMemory(SetHandleCount(MIB_IF_TABLE2.Table[i].InterfaceAndOperStatusFlags),
                              Address + (i) * leng + 1152, 8);
                RtlMoveMemory(ref MIB_IF_TABLE2.Table[i].OperStatus, Address + (i) * leng + 1160, 4);
                RtlMoveMemory(ref MIB_IF_TABLE2.Table[i].AdminStatus, Address + (i) * leng + 1164, 4);
                RtlMoveMemory(ref MIB_IF_TABLE2.Table[i].MediaConnectState, Address + (i) * leng + 1168, 4);
                MIB_IF_TABLE2.Table[i].NetworkGuid = new byte[16];
                RtlMoveMemory(SetHandleCount(MIB_IF_TABLE2.Table[i].NetworkGuid), Address + (i) * leng + 1172, 16);
                RtlMoveMemory(ref MIB_IF_TABLE2.Table[i].ConnectionType, Address + (i) * leng + 1188, 4);
                RtlMoveMemory(ref MIB_IF_TABLE2.Table[i].TransmitLinkSpeed, Address + (i) * leng + 1192, 8);
                RtlMoveMemory(ref MIB_IF_TABLE2.Table[i].ReceiveLinkSpeed, Address + (i) * leng + 1200, 8);
                RtlMoveMemory(ref MIB_IF_TABLE2.Table[i].InOctets, Address + (i) * leng + 1208, 8);
                RtlMoveMemory(ref MIB_IF_TABLE2.Table[i].InUcastPkts, Address + (i) * leng + 1216, 8);
                RtlMoveMemory(ref MIB_IF_TABLE2.Table[i].InNUcastPkts, Address + (i) * leng + 1224, 8);
                RtlMoveMemory(ref MIB_IF_TABLE2.Table[i].InDiscards, Address + (i) * leng + 1232, 8);
                RtlMoveMemory(ref MIB_IF_TABLE2.Table[i].InErrors, Address + (i) * leng + 1240, 8);
                RtlMoveMemory(ref MIB_IF_TABLE2.Table[i].InUnknownProtos, Address + (i) * leng + 1248, 8);
                RtlMoveMemory(ref MIB_IF_TABLE2.Table[i].InUcastOctets, Address + (i) * leng + 1256, 8);
                RtlMoveMemory(ref MIB_IF_TABLE2.Table[i].InMulticastOctets, Address + (i) * leng + 1264, 8);
                RtlMoveMemory(ref MIB_IF_TABLE2.Table[i].InBroadcastOctets, Address + (i) * leng + 1272, 8);
                RtlMoveMemory(ref MIB_IF_TABLE2.Table[i].OutOctets, Address + (i) * leng + 1280, 8);
                RtlMoveMemory(ref MIB_IF_TABLE2.Table[i].OutUcastPkts, Address + (i) * leng + 1288, 8);
                RtlMoveMemory(ref MIB_IF_TABLE2.Table[i].OutNUcastPkts, Address + (i) * leng + 1296, 8);
                RtlMoveMemory(ref MIB_IF_TABLE2.Table[i].OutDiscards, Address + (i) * leng + 1304, 8);
                RtlMoveMemory(ref MIB_IF_TABLE2.Table[i].OutErrors, Address + (i) * leng + 1312, 8);
                RtlMoveMemory(ref MIB_IF_TABLE2.Table[i].OutUcastOctets, Address + (i) * leng + 1320, 8);
                RtlMoveMemory(ref MIB_IF_TABLE2.Table[i].OutMulticastOctets, Address + (i) * leng + 1328, 8);
                RtlMoveMemory(ref MIB_IF_TABLE2.Table[i].OutBroadcastOctets, Address + (i) * leng + 1336, 8);
                RtlMoveMemory(ref MIB_IF_TABLE2.Table[i].OutQLen, Address + (i) * leng + 1344, 8);
            }

            FreeMibTable(pTable);
            return(MIB_IF_TABLE2);
        }