public static List <Dictionary <string, string> > GetNetCardInfo()
        {
            List <Dictionary <string, string> >            results  = new List <Dictionary <string, string> >();
            Dictionary <int, Dictionary <string, string> > adapters = new Dictionary <int, Dictionary <string, string> >();

            try
            {
                foreach (NetworkInterface netElement in NetworkInterface.GetAllNetworkInterfaces())
                {
                    Dictionary <string, string> card = new Dictionary <string, string>()
                    {
                        { "Index", netElement.GetIPProperties().GetIPv4Properties().Index.ToString() },
                        { "Name", netElement.Name },
                        { "PysicalAddr", "" },
                        { "DNSs", String.Join(", ", netElement.GetIPProperties().DnsAddresses) },
                        { "Gateways", "" },
                        { "IPs", "" },
                        { "Netmasks", "" },
                        { "arp", "" }
                    };
                    card["PysicalAddrIni"] = netElement.GetPhysicalAddress().ToString();
                    for (int i = 0; i < card["PysicalAddrIni"].Length; i += 2)
                    {
                        card["PysicalAddr"] += card["PysicalAddrIni"].Substring(i, 2) + ":";
                    }

                    foreach (GatewayIPAddressInformation address in netElement.GetIPProperties().GatewayAddresses.Reverse()) //Reverse so first IPv4
                    {
                        card["Gateways"] += address.Address + ", ";
                    }

                    foreach (UnicastIPAddressInformation ip in netElement.GetIPProperties().UnicastAddresses.Reverse())
                    { //Reverse so first IPv4
                        card["IPs"]      += ip.Address.ToString() + ", ";
                        card["Netmasks"] += ip.IPv4Mask.ToString() + ", ";
                    }

                    //Delete last separator
                    if (card["PysicalAddr"].Length > 0)
                    {
                        card["PysicalAddr"] = card["PysicalAddr"].Remove(card["PysicalAddr"].Length - 1);
                    }

                    if (card["Gateways"].Length > 0)
                    {
                        card["Gateways"] = card["Gateways"].Remove(card["Gateways"].Length - 2);
                    }

                    if (card["IPs"].Length > 0)
                    {
                        card["IPs"] = card["IPs"].Remove(card["IPs"].Length - 2);
                    }

                    if (card["Netmasks"].Length > 0)
                    {
                        card["Netmasks"] = card["Netmasks"].Remove(card["Netmasks"].Length - 2);
                    }

                    adapters[netElement.GetIPProperties().GetIPv4Properties().Index] = card;
                }
                //return results;

                // GET ARP values

                int bytesNeeded = 0;

                int result = GetIpNetTable(IntPtr.Zero, ref bytesNeeded, false);

                // call the function, expecting an insufficient buffer.
                if (result != ERROR_INSUFFICIENT_BUFFER)
                {
                    Console.WriteLine("  [X] Exception: {0}", result);
                }

                IntPtr buffer = IntPtr.Zero;

                // allocate sufficient memory for the result structure
                buffer = Marshal.AllocCoTaskMem(bytesNeeded);

                result = GetIpNetTable(buffer, ref bytesNeeded, false);

                if (result != 0)
                {
                    Console.WriteLine("  [X] Exception allocating buffer: {0}", result);
                }

                // now we have the buffer, we have to marshal it. We can read the first 4 bytes to get the length of the buffer
                int entries = Marshal.ReadInt32(buffer);

                // increment the memory pointer by the size of the int
                IntPtr currentBuffer = new IntPtr(buffer.ToInt64() + Marshal.SizeOf(typeof(int)));

                // allocate a list of entries
                List <MIB_IPNETROW> arpEntries = new List <MIB_IPNETROW>();

                // cycle through the entries
                for (int index = 0; index < entries; index++)
                {
                    arpEntries.Add((MIB_IPNETROW)Marshal.PtrToStructure(new IntPtr(currentBuffer.ToInt64() + (index * Marshal.SizeOf(typeof(MIB_IPNETROW)))), typeof(MIB_IPNETROW)));
                }

                // sort the list by interface index
                List <MIB_IPNETROW> sortedARPEntries = arpEntries.OrderBy(o => o.dwIndex).ToList();
                int currentIndexAdaper = -1;

                foreach (MIB_IPNETROW arpEntry in sortedARPEntries)
                {
                    int indexAdapter = arpEntry.dwIndex;
                    if (!adapters.ContainsKey(indexAdapter))
                    {
                        Console.WriteLine("Error: No interface found with Index " + arpEntry.dwIndex.ToString());
                        continue;
                    }
                    currentIndexAdaper = indexAdapter;

                    IPAddress    ipAddr    = new IPAddress(BitConverter.GetBytes(arpEntry.dwAddr));
                    byte[]       macBytes  = new byte[] { arpEntry.mac0, arpEntry.mac1, arpEntry.mac2, arpEntry.mac3, arpEntry.mac4, arpEntry.mac5 };
                    string       physAddr  = BitConverter.ToString(macBytes);
                    ArpEntryType entryType = (ArpEntryType)arpEntry.dwType;
                    adapters[arpEntry.dwIndex]["arp"] += String.Format("\t  {0,-22}{1,-22}{2}\n", ipAddr, physAddr, entryType);
                }

                FreeMibTable(buffer);
            }
            catch (Exception ex)
            {
                Console.WriteLine("  [X] Exception: {0}", ex);
            }
            results = adapters.Values.ToList();
            return(results);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creates an instance of an ArpEntry
        /// </summary>
        /// <param name="adapter">NetworkInterface on the local machine with which this entry will be associated</param>
        /// <param name="address">IP Address of the entry</param>
        /// <param name="mac">Physical (MAC) address of the entry</param>
        /// <param name="entryType">Type of the entry</param>
        public ArpEntry(NetworkInterface adapter, IPAddress address, PhysicalAddress mac, ArpEntryType entryType)
        {
            m_adapter = adapter;
            m_data    = new byte[SIZE];

            this.IPAddress       = address;
            this.PhysicalAddress = mac;
            this.ArpEntryType    = entryType;
            this.AdapterIndex    = adapter.Index;
        }