コード例 #1
0
ファイル: IPv4Datagram.cs プロジェクト: LiveMirror/mirror
        public TcpPacket HandleTcpPacket()
        {
            TcpPacket packet      = new TcpPacket();
            int       source_port = HeaderParser.ToInt(this.Data, 0, 16);
            int       dest_port   = HeaderParser.ToInt(this.Data, 16, 16);
            int       offset      = HeaderParser.ToInt(this.Data, 96, 4) * 4;

            packet.Source          = new IPEndPoint(this.Source, source_port);
            packet.Destination     = new IPEndPoint(this.Destination, dest_port);
            packet.Sequence        = HeaderParser.ToUInt(this.Data, 32, 32);
            packet.Acknowledgement = HeaderParser.ToUInt(this.Data, 64, 32);
            packet.DataOffset      = offset;
            packet.Urgent          = HeaderParser.ToByte(this.Data, 106, 1) != 0;
            packet.Ack             = HeaderParser.ToByte(this.Data, 107, 1) != 0;
            packet.Push            = HeaderParser.ToByte(this.Data, 108, 1) != 0;
            packet.Reset           = HeaderParser.ToByte(this.Data, 109, 1) != 0;
            packet.Syn             = HeaderParser.ToByte(this.Data, 110, 1) != 0;
            packet.Fin             = HeaderParser.ToByte(this.Data, 111, 1) != 0;


            packet.WindowSize    = HeaderParser.ToInt(this.Data, 112, 16);
            packet.Checksum      = HeaderParser.ToInt(this.Data, 128, 16);
            packet.UrgentPointer = HeaderParser.ToInt(this.Data, 144, 16);

            if (offset > 20)
            {
                packet.SetData(this.Data, 20, offset - 20);
            }

            packet.SetData(this.Data, offset, this.Data.Length - offset);
            return(packet);
        }
コード例 #2
0
ファイル: ProtocolTemplate.cs プロジェクト: LiveMirror/mirror
 public bool SetFields(Byte[] buffer)
 {
     if (buffer.Length >= this.HeaderLength_)
     {
         int [] x = new int[2];
         for (int i = 0; i < Keys_.Count; i++)
         {
             x = (int[])FieldLengths_[Keys_[i]];
             if (x[1] <= 8)
             {
                 Fields_.Add(Keys_[i], HeaderParser.ToByte(buffer, x[0], x[1]));
             }
             else if (x[1] <= 16)
             {
                 Fields_.Add(Keys_[i], HeaderParser.ToInt(buffer, x[0], x[1]));
             }
             else if (x[1] <= 32)
             {
                 Fields_.Add(Keys_[i], HeaderParser.ToUInt(buffer, x[0], x[1]));
             }
         }
         this.SetData(buffer, this.HeaderLength_, buffer.Length - this.HeaderLength_);
         Length_ = buffer.Length;
         return(true);
     }
     return(false);
 }
コード例 #3
0
ファイル: IPv4Datagram.cs プロジェクト: LiveMirror/mirror
 public void SetPorts()
 {
     if (this.Protocol == 6 || this.Protocol == 17)
     {
         SPort = HeaderParser.ToInt(this.Data, 0, 16);
         DPort = HeaderParser.ToInt(this.Data, 16, 16);
     }
 }
コード例 #4
0
ファイル: IPv4Datagram.cs プロジェクト: LiveMirror/mirror
        public UdpDatagram HandleUdpDatagram()
        {
            UdpDatagram packet      = new UdpDatagram();
            int         source_port = HeaderParser.ToInt(this.Data, 0, 16);
            int         dest_port   = HeaderParser.ToInt(this.Data, 16, 16);
            int         length      = HeaderParser.ToInt(this.Data, 32, 16) - 8;

            packet.Source      = new IPEndPoint(this.Source, source_port);
            packet.Destination = new IPEndPoint(this.Destination, dest_port);
            packet.SetData(this.Data, 8, length);
            return(packet);
        }
コード例 #5
0
ファイル: IPv4Datagram.cs プロジェクト: LiveMirror/mirror
        public int GetInnerDataLength()
        {
            switch (this.Protocol)
            {
            case 1: return(this.Data.Length - 8);

            case 6: return(this.Data.Length - HeaderParser.ToInt(this.Data, 96, 4) * 4);

            case 17: return(HeaderParser.ToInt(this.Data, 32, 16) - 8);

            default: return(0);
            }
        }
コード例 #6
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);
            }
        }
コード例 #7
0
        // Parses out an IPv4 Datagram.
        private void HandleIPv4Datagram(byte[] buffer)
        {
            int          identification = 0;
            int          protocol       = 0;
            uint         source         = 0;
            uint         dest           = 0;
            int          header_length  = 0;
            IPv4Datagram datagram       = null;
            IPAddress    source_ip;
            IPAddress    dest_ip;


            source    = HeaderParser.ToUInt(buffer, 96, 32);
            dest      = HeaderParser.ToUInt(buffer, 128, 32);
            source_ip = IPAddress.Parse(IPv4Datagram.GetIPString(source));
            dest_ip   = IPAddress.Parse(IPv4Datagram.GetIPString(dest));
            if (isRelatedToThisCom(source_ip.ToString(), dest_ip.ToString()))
            {
                IPv4Fragment fragment = new IPv4Fragment();

                fragment.MoreFlag = (HeaderParser.ToByte(buffer, 50, 1) > 0) ? true : false;
                fragment.Offset   = HeaderParser.ToInt(buffer, 51, 13) * 8;
                fragment.TTL      = HeaderParser.ToInt(buffer, 64, 8);
                fragment.Length   = HeaderParser.ToUShort(buffer, 16, 16);
                header_length     = HeaderParser.ToInt(buffer, 4, 4) * 4;
                fragment.SetData(buffer, header_length, fragment.Length - header_length);

                identification = HeaderParser.ToInt(buffer, 32, 16);
                protocol       = HeaderParser.ToByte(buffer, 72, 8);
//				source = HeaderParser.ToUInt(buffer,96,32);
//				dest = HeaderParser.ToUInt(buffer,128,32);
//				source_ip = IPAddress.Parse(IPv4Datagram.GetIPString(source));
//				dest_ip = IPAddress.Parse(IPv4Datagram.GetIPString(dest));

                datagram = Data_.GetIPv4Datagram(identification, source_ip, dest_ip);

                if (datagram == null)
                {
                    datagram                  = new IPv4Datagram();
                    datagram.IHL              = HeaderParser.ToInt(buffer, 4, 4) * 4;
                    datagram.TypeOfService    = HeaderParser.ToInt(buffer, 8, 8);
                    datagram.ReservedFlag     = HeaderParser.ToInt(buffer, 48, 1);
                    datagram.DontFragmentFlag = HeaderParser.ToInt(buffer, 49, 1);
                    datagram.Identification   = identification;
                    datagram.TTL              = HeaderParser.ToInt(buffer, 64, 8);
                    datagram.Checksum         = HeaderParser.ToInt(buffer, 80, 16);
                    datagram.Source           = source_ip;

                    datagram.SourceName = DnsTable.GetName(source_ip.ToString());

                    datagram.DestinationName = DnsTable.GetName(dest_ip.ToString());

                    datagram.Destination = dest_ip;
                    datagram.Protocol    = protocol;
                    if (datagram.IHL == 24)
                    {
                        datagram.Options = HeaderParser.ToInt(buffer, 160, 32);
                    }
                }

                datagram.AddFragment(fragment);
                if (datagram.Complete)
                {
                    datagram.SetPorts();
                    if (FilterManager.isAllowed(datagram.GetUpperProtocol(), datagram.SourceIP, datagram.DestinationIP, datagram.SPort, datagram.DPort))
                    {
                        FireIPv4DatagramReceived(datagram);

                        datagram.SetHeader(buffer);
                        if (datagram.WasFragmented())
                        {
                            Data_.RemoveIPv4Datagram(datagram);
                        }
                    }
                }
                else
                {
                    Data_.AddIPv4Datagram(datagram);
                    this.FireIPv4FragmentReceived(fragment);
                }
            }
        }