예제 #1
0
        public static List <IAdapter> GetAdapters()
        {
            //To use Nicolas .NET wrapper for WinPcap:

            List <IAdapter> deviceList = new List <IAdapter>();

            foreach (Device d in WinPCapWrapper.FindAllDevs())
            {
                deviceList.Add(new WinPCapAdapter((Device)d));
            }
            return(deviceList);


            //To use the old dotNetPcap dll file:

            /*
             * System.Collections.ArrayList tmpList;
             * tmpList=dotnetWinpCap.FindAllDevs();
             *
             * List<IAdapter> devices=new List<IAdapter>(tmpList.Count);
             *
             * foreach(object d in tmpList) {
             *  devices.Add(new WinPCapAdapter((Device)d));
             * }
             * return devices;
             */
        }
        public static List <IAdapter> GetAdapters(int millisecondsTimeout = 1000)
        {
            if (millisecondsTimeout > 0)
            {
                //Let's wrap this peice of unmanaged code in a task in order to handle timeouts better
                var getAdapterTask = System.Threading.Tasks.Task.Factory.StartNew <List <IAdapter> >(() => {
                    //To use Nicolas .NET wrapper for WinPcap:
                    List <IAdapter> deviceList = new List <IAdapter>();
                    try
                    {
                        foreach (Device d in WinPCapWrapper.FindAllDevs())
                        {
                            deviceList.Add(new WinPCapAdapter((Device)d));
                        }
                    }
                    catch { }
                    return(deviceList);
                });

                //getAdapterTask.ContinueWith(t => { /* error handling */ }, TaskContinuationOptions.OnlyOnFaulted | TaskContinuationOptions.ExecuteSynchronously);

                //problem: the task might throw an exception
                if (getAdapterTask.Wait(millisecondsTimeout))
                {
                    return(getAdapterTask.Result);
                }
                else
                {
                    throw new TimeoutException("Timeout while getting WinPCap Adapters");
                }
            }
            else
            {
                List <IAdapter> deviceList = new List <IAdapter>();
                foreach (Device d in WinPCapWrapper.FindAllDevs())
                {
                    deviceList.Add(new WinPCapAdapter((Device)d));
                }
                return(deviceList);
            }


            //To use the old dotNetPcap dll file:

            /*
             * System.Collections.ArrayList tmpList;
             * tmpList=dotnetWinpCap.FindAllDevs();
             *
             * List<IAdapter> devices=new List<IAdapter>(tmpList.Count);
             *
             * foreach(object d in tmpList) {
             *  devices.Add(new WinPCapAdapter((Device)d));
             * }
             * return devices;
             */
        }
예제 #3
0
        //constructor
        public WinPCapSniffer(WinPCapAdapter adapter)
        {
            //this.adapter=adapter;
            nPacketsReceived = 0;
            //wpcap=new dotnetWinpCap();

            wpcap = new WinPCapWrapper();
            if (wpcap.IsOpen)
            {
                wpcap.Close();
            }

            //Opens an Ethernet interface with
            //source as the name of the interface obtained from a Device object,
            //snaplen is the max number of bytes to be captured from each packet,
            //flags=1 means promiscuous mode,
            //read_timeout is the blocking time of ReadNext before it returns.
            if (!wpcap.Open(adapter.NPFName, 65536, 1, 0))
            {
                throw new Exception(wpcap.LastError);
            }

            //sets the minimum number of bytes required to be received by the driver before
            //OnReceivePacket fires. Lowering this can increase response time,
            //but increases system calls which lowers program efficiency.
            wpcap.SetMinToCopy(100);

            WinPCapWrapper.PacketArrivalEventHandler rcvPack = new WinPCapNative.PacketArrivalEventHandler(this.ReceivePacketListener);
            //dotnetWinpCap.ReceivePacket rcvPack=new dotnetWinpCap.ReceivePacket(this.ReceivePacketListener);
            //wpcap.OnReceivePacket+=rcvPack;
            wpcap.PacketArrival += rcvPack;

            if (wpcap.DataLink == (int)DataLinkType.WTAP_ENCAP_IEEE_802_11)
            {
                this.basePacketType = PacketReceivedEventArgs.PacketTypes.IEEE_802_11Packet;
            }
            else if (wpcap.DataLink == (int)DataLinkType.WTAP_ENCAP_ETHERNET)
            {
                this.basePacketType = PacketReceivedEventArgs.PacketTypes.Ethernet2Packet;
            }
            else if (wpcap.DataLink == (int)DataLinkType.WTAP_ENCAP_IEEE_802_11_WLAN_RADIOTAP)
            {
                this.basePacketType = PacketReceivedEventArgs.PacketTypes.IEEE_802_11RadiotapPacket;
            }
            else if (wpcap.DataLink == (int)DataLinkType.WTAP_ENCAP_RAW_IP)
            {
                this.basePacketType = PacketReceivedEventArgs.PacketTypes.IPv4Packet;
            }
            else if (wpcap.DataLink == (int)DataLinkType.WTAP_ENCAP_RAW_IP_2)
            {
                this.basePacketType = PacketReceivedEventArgs.PacketTypes.IPv4Packet;
            }
            else if (wpcap.DataLink == (int)DataLinkType.WTAP_ENCAP_RAW_IP_3)
            {
                this.basePacketType = PacketReceivedEventArgs.PacketTypes.IPv4Packet;
            }
            else if (wpcap.DataLink == (int)DataLinkType.WTAP_ENCAP_CHDLC)
            {
                this.basePacketType = PacketReceivedEventArgs.PacketTypes.CiscoHDLC;
            }
            else if (wpcap.DataLink == (int)DataLinkType.WTAP_ENCAP_SLL)
            {
                this.basePacketType = PacketReceivedEventArgs.PacketTypes.LinuxCookedCapture;
            }
            else if (adapter.ToString().ToLower().Contains("airpcap"))
            {
                this.basePacketType = PacketReceivedEventArgs.PacketTypes.IEEE_802_11Packet;//could also be radiotap
            }
            else
            {
                this.basePacketType = PacketReceivedEventArgs.PacketTypes.Ethernet2Packet;
            }
        }