Example #1
0
        public List <ArpIpAddress> GetListArp()
        {
            List <NativeWLanAPI.MIB_IPNETROW> ArpList = new List <NativeWLanAPI.MIB_IPNETROW>();
            List <ArpIpAddress> addresses             = new List <ArpIpAddress>();
            int size = 0;

            NativeWLanAPI.GetIpNetTable(IntPtr.Zero, ref size, true);
            IntPtr ipList = Marshal.AllocHGlobal(size);

            if ((int)NativeWLanAPI.GetIpNetTable(ipList, ref size, true) == 0)
            {
                var num = Marshal.ReadInt32(ipList);
                var ptr = IntPtr.Add(ipList, 4);
                for (int i = 0; i < num; i++)
                {
                    ArpList.Add((NativeWLanAPI.MIB_IPNETROW)Marshal.PtrToStructure(ptr, typeof(NativeWLanAPI.MIB_IPNETROW)));
                    ptr = IntPtr.Add(ptr, Marshal.SizeOf(typeof(NativeWLanAPI.MIB_IPNETROW)));
                }
                Marshal.FreeHGlobal(ipList);
            }
            foreach (var item in ArpList)
            {
                addresses.Add(new ArpIpAddress()
                {
                    IpAddress = ArpHelper.ConvertIpToStr(item.Addr), MacAddress = ArpHelper.CovnertMacToStr(item.PhysAddr)
                });
            }
            return(addresses);
        }
        /// <summary>
        /// Busca todo e qualquer equipamento da lista ARP na LAN.
        /// </summary>
        private void Search_AllDevices()
        {
            // Classe usada para listar o ARP (Todos os IP's registrados no gateway)
            ArpHelper arp = new ArpHelper();

            // Gera a lista ARP com IPs, MACs e Types dos Equipamentos Registrados na Rede.
            listArp = arp.GetArpResult();
            //
            lstListDevices.Items.Clear();
            //
            foreach (var device in listArp)
            {
                lstListDevices.Items.Add(GetListViewItem(device));
            }
        }
Example #3
0
        /// <summary>
        /// 开始抓包
        /// </summary>
        /// <param name="mac"></param>
        public void StartPacp(string mac)
        {
            _Mac = mac;
            foreach (var item in CaptureDeviceList.Instance)
            {
                if (string.Equals(ArpHelper.ConvertMacStrToStr((item as dynamic).Interface.MacAddress.ToString()), _Mac, StringComparison.CurrentCultureIgnoreCase))
                {
                    _Device = item;
                    break;
                }
            }
            if (_Device != null)
            {
                _Device.OnPacketArrival -= _Device_OnPacketArrival;

                _Device.OnPacketArrival += _Device_OnPacketArrival;
                int readTimeoutMilliseconds = 1000;
                _Device.Open(DeviceMode.Normal, readTimeoutMilliseconds);
                _Device.StartCapture();
            }
        }
Example #4
0
        static IPAddress FindMyNanoLeaf()
        {
            Ping        pingSender = new Ping();
            PingOptions options    = new PingOptions();

            // Use the default Ttl value which is 128,
            // but change the fragmentation behavior.
            options.DontFragment = true;

            // Create a buffer of 32 bytes of data to be transmitted.
            string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";

            byte[] buffer  = Encoding.ASCII.GetBytes(data);
            int    timeout = 400;

            for (int i = 0; i < 200; i++)
            {
                byte[]    b     = { 192, 168, 68, (byte)(i) };
                var       addr  = new IPAddress(b);
                PingReply reply = pingSender.Send(addr, timeout, buffer, options);
                if (reply.Status == IPStatus.Success)
                {
                    Console.WriteLine("Address: {0}", reply.Address.ToString());
                    Console.WriteLine("RoundTrip time: {0}", reply.RoundtripTime);
                    Console.WriteLine("Time to live: {0}", reply.Options.Ttl);
                    Console.WriteLine("Don't fragment: {0}", reply.Options.DontFragment);
                    Console.WriteLine("Buffer size: {0}", reply.Buffer.Length);
                }
            }

            var arpEntities = new ArpHelper().GetArpResult();
            //return arpEntities.First().ToString();
            //Console.WriteLine();
            var macAddress = PhysicalAddress.Parse("00-55-da-57-44-31");

            return(arpEntities.FirstOrDefault(a => a.MacAddress.Equals(macAddress))?.Ip);
        }
        private async void Search_TCP232()
        {
            // Classe usada para listar o ARP (Todos os IP's registrados no gateway)
            ArpHelper arp = new ArpHelper();

            // Gera a lista ARP com IPs, MACs e Types dos Equipamentos Registrados na Rede.
            listArp = arp.GetArpResult();
            //
            byte[] searchCommand = { 0xFF, 0x01, 0x01, 0x02 };
            //
            byte[] basicCommmand = { 0x55, 0xC6 };

            //
            lstListDevices.Items.Clear();
            // Roda a lista ARP enviando o comando de "Search" na porta 1901.
            foreach (var device in listArp)
            {
                lstListDevices.Items.Add(GetListViewItem(device));

                // Conecta via UDP ao Device
                UDP.Connect(device.Ip, 1901, 1901);
                //
                UDP.DataReceivedInBackGround += UDP_DataReceivedInBackGround;
                // Envia um comando de busca, caso o device responda corretamente, então trata-se de um módulo USR-TCP232
                UDP.Send(searchCommand);
                //
                await Task.Delay(800);

                //
                UDP.DataReceivedInBackGround -= UDP_DataReceivedInBackGround;
                //
                UDP.Disconnect();
                //
                await Task.Delay(50);
            }
        }