Exemplo n.º 1
0
            private void OnReceive(IAsyncResult ar)
            {                                 
                AsyncCaptureState so = (AsyncCaptureState)ar.AsyncState;
                Socket mainSocket = so.workSocket;

                try {
                    int nReceived = mainSocket.EndReceive(ar);
                   
                    //Analyze the bytes received...

                    ParseData(byteData, nReceived);

                    if (bContinueCapturing)
                    {
                        byteData = new byte[MAX_PACKET_SIZE];

                        //Another call to BeginReceive so that we continue to receive the incoming
                        //packets
                        mainSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None,
                            new AsyncCallback(OnReceive), so);
                    }
                }
                catch (ObjectDisposedException)
                {
                }
                //catch (Exception ex)
                //{
                //    // do something with error..
                //    // MessageBox.Show(ex.Message, "MJsniffer", MessageBoxButtons.OK, MessageBoxIcon.Error);
                //    PacketEvent("ONReceive Error: " + ex.ToString());
                //}
            }
Exemplo n.º 2
0
        public void StartCapture()
        {
           // try
           // {
                if (!bContinueCapturing)
                {
                    bContinueCapturing = true;

                    // start listening on every interface... so we don't have to ask the user to choose
                    // TODO: make a settings dialog to choose ANY or a specific interface?

                    IPHostEntry hostEntry = Dns.GetHostEntry((Dns.GetHostName()));
                    foreach (IPAddress ip in hostEntry.AddressList) {
                    
                        string interfaceName = ip.ToString();

                        //For sniffing the socket to capture the packets has to be a raw socket, with the
                        //address family being of type internetwork, and protocol being IP
                        
                        AddressFamily addressFamily = ip.AddressFamily;
                        ProtocolType protocolType;
                        SocketOptionLevel socketOptionLevel;

                        switch (ip.AddressFamily) {
                            case AddressFamily.InterNetwork:
                                protocolType = ProtocolType.IP;
                                socketOptionLevel = SocketOptionLevel.IP;
                                break;
                            case AddressFamily.InterNetworkV6:
                                protocolType = ProtocolType.IPv6;
                            socketOptionLevel = SocketOptionLevel.IPv6;
                            break;
                            default:
                                continue;
                        }

                        Socket mainSocket = new Socket(addressFamily, SocketType.Raw, protocolType);

                        this.listenSockets.Add(mainSocket); // it to our list of listen sockets...

                        //Bind the socket to the selected IP address
                        mainSocket.Bind(new IPEndPoint(IPAddress.Parse(interfaceName), 0));
                        
                        //Set the socket  options
                        mainSocket.SetSocketOption(socketOptionLevel,            //Applies only to IP packets
                                                   SocketOptionName.HeaderIncluded, //Set the include the header
                                                   true);                           //option to true

                        byte[] byTrue = new byte[4] { 1, 0, 0, 0 };
                        byte[] byOut = new byte[4] { 1, 0, 0, 0 }; //Capture outgoing packets

                        //Socket.IOControl is analogous to the WSAIoctl method of Winsock 2
                        mainSocket.IOControl(IOControlCode.ReceiveAll,              //Equivalent to SIO_RCVALL constant
                                                                                    //of Winsock 2
                                             byTrue,
                                             byOut);

                        //Start receiving the packets asynchronously
                        var aState = new AsyncCaptureState(mainSocket);
                        mainSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None,
                            new AsyncCallback(OnReceive), aState);                       
                    }

                    
                }
            //} catch (Exception ex) {                
            //    // something bad happened we should probably fix
            //    if (PacketEvent != null) PacketEvent("Error opening socket for packet sniffing");
            //}

        }