Пример #1
0
        public async Task CapturePacketsAsync(string deviceName)
        {
            Task task = Task.Run(() =>
            {
                dev = SharpPcap.CaptureDeviceList.Instance[2];
                // Instance[2] for wifi
                // Instance[5] for LAN
                int readTimeoutMilliseconds = 1000;

                dev.OnPacketArrival +=
                    new PacketArrivalEventHandler(dev_OnPacketArrival);

                if (dev is AirPcapDevice)
                {
                    // NOTE: AirPcap devices cannot disable local capture
                    var airPcap = dev as AirPcapDevice;

                    airPcap.Open(SharpPcap.WinPcap.OpenFlags.DataTransferUdp, readTimeoutMilliseconds);
                }
                else if (dev is WinPcapDevice)
                {
                    var winPcap = dev as WinPcapDevice;
                    winPcap.Open(SharpPcap.WinPcap.OpenFlags.DataTransferUdp | SharpPcap.WinPcap.OpenFlags.NoCaptureLocal, readTimeoutMilliseconds);
                }
                else if (dev is LibPcapLiveDevice)
                {
                    var livePcapDevice = dev as LibPcapLiveDevice;
                    livePcapDevice.Open(DeviceMode.Promiscuous, readTimeoutMilliseconds);
                }

                dev.Capture();
            });
            await task;
        }
        public static DCP.IpInfo GetPcapIp(SharpPcap.ICaptureDevice pcapDevice)
        {
            foreach (System.Net.NetworkInformation.NetworkInterface nic in System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces())
            {
                PhysicalAddress mac = null;
                try
                {
                    mac = nic.GetPhysicalAddress();
                }
                catch (Exception)
                {
                    //interface have no mac address
                }

                if (mac != null && mac.Equals(pcapDevice.MacAddress))
                {
                    IPInterfaceProperties ipp = nic.GetIPProperties();
                    foreach (var entry in ipp.UnicastAddresses)
                    {
                        if (entry.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                        {
                            byte[] gw = new byte[] { 0, 0, 0, 0 };
                            if (ipp.GatewayAddresses.Count > 0)
                            {
                                gw = ipp.GatewayAddresses[0].Address.GetAddressBytes();
                            }
                            return(new DCP.IpInfo(DCP.BlockInfo.IpSet, entry.Address.GetAddressBytes(), entry.IPv4Mask.GetAddressBytes(), gw));
                        }
                    }
                }
            }
            return(null);
        }
Пример #3
0
        /// <summary>
        /// Creates a HardwareDevice object based on its SharpPcap representation.
        /// </summary>
        /// <param name="dev"></param>
        public HardwareDevice(ICaptureDevice dev)
        {
            this.Device = dev;
            string input = dev.ToString();

            IpAddresses = new List <String>();

            List <String> detectedAddrs = new List <String>();

            IPAddress[] localIps = Dns.GetHostAddresses(Dns.GetHostName());

            foreach (IPAddress ip in localIps)
            {
                string addr = ip.ToString();

                // scrub out the %xxx at the end (if present)
                int percentSignPos = addr.IndexOf("%");
                if (percentSignPos > 0)
                {
                    addr = addr.Substring(0, percentSignPos);
                }

                detectedAddrs.Add(addr);
            }

            // break everything up so we can parse line-by-line
            string[] deviceFields = input.Split(new string[] { "\n" }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string line in deviceFields)
            {
                if (line.StartsWith("FriendlyName"))
                {
                    FriendlyName = line.Replace("FriendlyName: ", "");
                }
                else if (line.StartsWith("Description"))
                {
                    Description = line.Replace("Description: ", "");
                }

                /*	IP or MAC address
                 *      Only add if the IP address here exists in the detected list
                 *      (that is, the list returned by Dns.GetHostAddresses() )
                 */
                else if (line.StartsWith("Addr:"))
                {
                    string detectedIp = line.Replace("Addr:      ", "");

                    // MAC address
                    if (detectedIp.Contains("HW addr: "))
                    {
                        MacAddress = detectedIp.Replace("HW addr: ", "");
                    }

                    // IP address
                    if (detectedAddrs.Contains(detectedIp))
                    {
                        IpAddresses.Add(detectedIp);
                    }
                }
            }
        }