コード例 #1
0
        private void ReceivePacket(IAsyncResult ar)
        {
            int        num        = 0;
            SocketPair asyncState = ar.AsyncState as SocketPair;
            Socket     iPSocket   = asyncState.IPSocket;
            int        num2       = 0;

            try
            {
                num = iPSocket.EndReceive(ar);
            }
            catch (SocketException exception)
            {
                this.FireSnifferError(new SnifferException("Error Receiving Packet", exception));
            }
            num2 = Sniffer.HeaderParser.ToInt(asyncState.Buffer, 0, 4);
            try
            {
                switch (num2)
                {
                case 4:
                {
                    this.HandleIPv4Datagram(asyncState.Buffer);
                }
                break;
                }
            }
            catch (Exception exception2)
            {
                this.FireSnifferError(new SnifferException("Unknown Exception", exception2));
            }
            iPSocket.BeginReceive(asyncState.Buffer, 0, asyncState.Buffer.Length, SocketFlags.None, new AsyncCallback(this.ReceivePacket), asyncState);
        }
コード例 #2
0
        public void resumeSocket()
        {
            SocketPair      p       = SocketMap_[IP] as SocketPair;
            Socket          socket  = p.IPSocket;
            bool            ret_val = true;
            SocketException e       = null;

            try
            {
                socket.BeginReceive(p.Buffer, 0, p.Buffer.Length, SocketFlags.None, new AsyncCallback(this.ReceivePacket), p);
                this.paused = false;
            }
            catch (SocketException ex)
            {
                e       = ex;
                ret_val = false;
            }
            finally
            {
                if (!ret_val)
                {
                    throw new SnifferException("Could not resume the socket", e);
                }
            }
        }
コード例 #3
0
        // Callback function for the Asynchronous Receive on a Socket.
        private void ReceivePacket(IAsyncResult ar)
        {
            bool       fired  = false;
            int        len    = 0;
            SocketPair p      = ar.AsyncState as SocketPair;
            Socket     socket = p.IPSocket;
            int        type   = 0;

            try
            {
                len = socket.EndReceive(ar);
            }
            catch (SocketException e)
            {
                fired = true;
                FireSnifferError(new SnifferException("Error Receiving Packet", e));
            }

            if (!fired)
            {
                type = HeaderParser.ToInt(p.Buffer, 0, 4);
                try
                {
                    switch (type)
                    {
                    case 4:
                        HandleIPv4Datagram(p.Buffer);
                        break;
                    }
                }
                catch (Exception e)
                {
                    FireSnifferError(new SnifferException(e.Message.ToString(), e));
                }
            }
            if (!this.paused)
            {
                socket.BeginReceive(p.Buffer, 0, p.Buffer.Length, SocketFlags.None, new AsyncCallback(this.ReceivePacket), p);
            }
        }
コード例 #4
0
        // This will start the Sniffer so that it will listen the IP passed in.
        // All of the Sniffing is done asynchronously so this method will return immediately.
        public void Sniff(String ip)
        {
            IP = ip;
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP);

            byte[]     buffer     = new byte[2048];
            SocketPair socketpair = new SocketPair(socket, buffer);

            socket.Blocking = true;
            try
            {
                socket.Bind(new IPEndPoint(IPAddress.Parse(ip), 0));
            }
            catch (SocketException e)
            {
                throw new SnifferException("Cannot assign requested address.The requested address is not valid in its context.", e);
            }
            this.SetupSocket(socket);

            if (SocketMap_.Contains(ip))
            {
                throw new SnifferException("Socket already bound on that IP");
            }
            else
            {
                SocketMap_.Add(ip, socketpair);
            }
            try
            {
                socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(this.ReceivePacket), socketpair);
                paused = false;
            }
            catch (Exception e)
            {
                throw new SnifferException("Could not start the Receive", e);
            }
        }
コード例 #5
0
        public void Sniff(string ip)
        {
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP);

            byte[]     buffer = new byte[0x800];
            SocketPair pair   = new SocketPair(socket, buffer);

            socket.Blocking = false;
            socket.Bind(new IPEndPoint(IPAddress.Parse(ip), 0));
            this.SetupSocket(socket);
            if (this.m_SocketMap.Contains(ip))
            {
                throw new SnifferException("Socket already bound on that IP");
            }
            this.m_SocketMap.Add(ip, pair);
            try
            {
                socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(this.ReceivePacket), pair);
            }
            catch (Exception exception)
            {
                throw new SnifferException("Could not start the Receive", exception);
            }
        }