Example #1
0
        /// <summary>
        ///     尝试搜寻目前局域网内的所有设备。
        /// </summary>
        /// <exception cref="TimeoutException">等待线程结束超时。</exception>
        public void ScanForTarget()
        {
            // 获取当前设备
            var device = DeviceList[CurDevName];

            // 创建分析线程
            const int analyzeThreadsCount = 4;
            var       analyzeThreads      = new Thread[analyzeThreadsCount];

            for (var i = 0; i < analyzeThreadsCount; i++)
            {
                analyzeThreads[i] = new Thread(ScanPacketAnalyzeThread);
            }

            // 创建发包线程队列
            var sendThreads = new List <Thread>();

            try {
                // arp头起始位置向后偏移6字节后,取2字节内容即为arp包类型
                device = StartCapture("arp [6:2] = 2");

                // 启动分析线程
                for (var i = 0; i < analyzeThreadsCount; i++)
                {
                    analyzeThreads[i].Start();
                }

                // 去除网络号和广播地址,产生地址集合
                byte[] minAddress = NetworkNumber.GetAddressBytes(),
                maxAddress  = BroadcastAddress.GetAddressBytes(),
                tempAddress = minAddress;
                var ipAddresses = new List <IPAddress>();
                tempAddress[3]++;
                while (!(tempAddress[0] == maxAddress[0] &&
                         tempAddress[1] == maxAddress[1] &&
                         tempAddress[2] == maxAddress[2] &&
                         tempAddress[3] == maxAddress[3]))
                {
                    ipAddresses.Add(new IPAddress(tempAddress));
                    if (ipAddresses.Count >= (AddressCount / 8 >= 254 ? 254 : AddressCount / 8))
                    {
                        // 创建发包线程
                        var sendThread = new Thread(ScanPacketSendThread);
                        sendThread.Start(ipAddresses);
                        sendThreads.Add(sendThread);
                        ipAddresses = new List <IPAddress>();
                    }
                    var i = 3;
                    while (i >= 0 && tempAddress[i] == 255)
                    {
                        tempAddress[i] = 0;
                        i--;
                    }
                    tempAddress[i]++;
                }

                // 最后一个发送线程
                var lastSendThread = new Thread(ScanPacketSendThread);
                lastSendThread.Start(ipAddresses);
                sendThreads.Add(lastSendThread);

                // 等待数据包发送完成
                new WaitTimeoutChecker((int)(60 * 1000 * Math.Log(AddressCount, 254))).ThreadSleep(500, () => sendThreads.Any(item => item.IsAlive));

                // 等待接收目标机反馈消息
                Thread.Sleep((int)(8 * 1000 * Math.Log(AddressCount, 254)));
            }
            finally {
                // 终止发包线程
                foreach (var sendThread in sendThreads)
                {
                    if (sendThread.IsAlive)
                    {
                        sendThread.Abort();
                    }
                }

                // 终止分析线程
                foreach (var analyzeThread in analyzeThreads)
                {
                    if (analyzeThread.IsAlive)
                    {
                        analyzeThread.Abort();
                    }
                }

                // 清理缓冲区及其他内容
                lock (_hostList) {
                    _hostList.Sort(Host.SortMethod);
                }
                StopCapture(device);
                ClearCaptures();
            }
        }
Example #2
0
 public override void WriteTo(System.IO.Stream stream)
 {
     stream.WriteByte(4);
     BinaryHelper.Write(stream, BroadcastAddress.GetAddressBytes());
 }