public SocketSniffer(SocketAdapter adapter)
        {
            this.basePacketType = adapter.BasePacketType;

            this.snifferActive = false;
            buffer             = new byte[65535];

            //this does not seem to work for IPv6 traffic.
            //Others seem to have similar problems: http://social.technet.microsoft.com/Forums/en-US/netfxnetcom/thread/95fba78d-aa40-44df-9575-dc98138455f3
            //I would like to do somthing like this:
            if (adapter.IP.AddressFamily == AddressFamily.InterNetworkV6)
            {
                socket = new Socket(AddressFamily.InterNetworkV6, SocketType.Raw, ProtocolType.Raw);
                socket.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.HeaderIncluded, true);
            }
            else
            {
                socket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP);
            }

            IPEndPoint endPoint = new IPEndPoint(adapter.IP, 0);

            socket.Bind(endPoint);
            byte[] optionInValue = { 1, 0, 0, 0 };
            socket.IOControl(IOControlCode.ReceiveAll, optionInValue, null);
        }
Exemplo n.º 2
0
        /*SocketAdapter(IPAddress ip) {
         *  this.ip=ip;
         * }*/
        SocketAdapter(NetworkInterface nic, IPAddress ip)
        {
            this.nic = nic;
            this.ip  = ip;
            if (nic.Supports(NetworkInterfaceComponent.IPv4) && ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
            {
                this.basePacketType = PacketReceivedEventArgs.PacketTypes.IPv4Packet;
            }
            else if (nic.Supports(NetworkInterfaceComponent.IPv6) && ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6)
            {
                this.basePacketType = PacketReceivedEventArgs.PacketTypes.IPv6Packet;
            }
            else//use IPv4 as default
            {
                this.basePacketType = PacketReceivedEventArgs.PacketTypes.IPv4Packet;
            }

            /*
             * UnicastIPAddressInformationCollection ipAddressCollection=nic.GetIPProperties().UnicastAddresses;
             * if (ipAddressCollection.Count > 0)
             *  this.ip = ipAddressCollection[0].Address;
             * else this.ip = IPAddress.None;
             * */
        }
Exemplo n.º 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;
            }
        }