Exemplo n.º 1
0
 public void SetPorts()
 {
     if (this.Protocol == 6 || this.Protocol == 17)
     {
         SPort = HeaderParser.ToInt(this.Data, 0, 16);
         DPort = HeaderParser.ToInt(this.Data, 16, 16);
     }
 }
Exemplo n.º 2
0
        public IcmpPacket HandleIcmpPacket()
        {
            IcmpPacket packet = new IcmpPacket();

            packet.Source      = new IPEndPoint(this.Source, 0);
            packet.Destination = new IPEndPoint(this.Destination, 0);
            packet.Type        = HeaderParser.ToByte(this.Data, 0, 8);
            packet.Code        = HeaderParser.ToByte(this.Data, 8, 8);
            packet.Checksum    = HeaderParser.ToUShort(this.Data, 16, 16);
            packet.SetData(this.Data, 4, this.Data.Length - 4);
            return(packet);
        }
Exemplo n.º 3
0
        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);
        }
Exemplo n.º 4
0
        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);
            }
        }
Exemplo n.º 5
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);
            }
        }
Exemplo n.º 6
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);
                }
            }
        }
Exemplo n.º 7
0
        public int CheckNextFields(byte [] data)
        {
            int ret = 0;

            switch (this.Type)
            {
            case 0:
            case 8:
            case 13:
            case 14:
            case 15:
            case 16:
            case 17:
            case 18:
            case 37:
            case 38:
                FieldNames_  = new ArrayList();
                FieldLength_ = new ArrayList();
                FieldValues_ = new Hashtable();
                this.FieldLength_.Add(16);
                this.FieldNames_.Add("Identifier");
                this.FieldValues_.Add("Identifier", HeaderParser.ToUInt(data, 32, 16));
                this.FieldLength_.Add(16);
                this.FieldNames_.Add("Sequence Number");
                this.FieldValues_.Add("Sequence Number", HeaderParser.ToUInt(data, 48, 16));
                ret           = 8;
                this.dataIsIp = false;
                break;

            case 4:
            case 11:
                FieldNames_  = new ArrayList();
                FieldLength_ = new ArrayList();
                FieldValues_ = new Hashtable();
                this.FieldLength_.Add(32);
                this.FieldNames_.Add("Reserved");
                this.FieldValues_.Add("Reserved", HeaderParser.ToUInt(data, 32, 32));
                ret           = 8;
                this.dataIsIp = true;
                break;

            case 10:
                FieldNames_  = new ArrayList();
                FieldLength_ = new ArrayList();
                FieldValues_ = new Hashtable();
                this.FieldLength_.Add(32);
                this.FieldNames_.Add("Reserved");
                this.FieldValues_.Add("Reserved", HeaderParser.ToUInt(data, 32, 32));
                ret           = 8;
                this.dataIsIp = false;
                break;

            case 3:
                FieldNames_  = new ArrayList();
                FieldLength_ = new ArrayList();
                FieldValues_ = new Hashtable();
                this.FieldLength_.Add(16);
                this.FieldNames_.Add("Unused");
                this.FieldValues_.Add("Unused", HeaderParser.ToUInt(data, 32, 16));
                this.FieldLength_.Add(16);
                this.FieldNames_.Add("Next Hop MTU");
                this.FieldValues_.Add("Next Hop MTU", HeaderParser.ToUInt(data, 48, 16));
                ret           = 8;
                this.dataIsIp = true;
                break;

            case 5:
                FieldNames_  = new ArrayList();
                FieldLength_ = new ArrayList();
                FieldValues_ = new Hashtable();
                this.FieldLength_.Add(32);
                this.FieldNames_.Add("IP Address");
                this.FieldValues_.Add("IP Address", HeaderParser.ToUInt(data, 32, 32));
                ret           = 8;
                this.dataIsIp = true;
                break;

            case 31:
                FieldNames_  = new ArrayList();
                FieldLength_ = new ArrayList();
                FieldValues_ = new Hashtable();
                this.FieldLength_.Add(32);
                this.FieldNames_.Add("Offset");
                this.FieldValues_.Add("Offset", HeaderParser.ToUInt(data, 32, 32));
                ret           = 8;
                this.dataIsIp = false;
                break;

            case 12:
                FieldNames_  = new ArrayList();
                FieldLength_ = new ArrayList();
                FieldValues_ = new Hashtable();
                this.FieldLength_.Add(8);
                this.FieldNames_.Add("Pointer");
                this.FieldValues_.Add("Pointer", HeaderParser.ToUInt(data, 32, 8));
                this.FieldLength_.Add(24);
                this.FieldNames_.Add("Unused");
                this.FieldValues_.Add("Unused", HeaderParser.ToUInt(data, 40, 24));
                ret           = 8;
                this.dataIsIp = true;
                break;

            case 9:
                FieldNames_  = new ArrayList();
                FieldLength_ = new ArrayList();
                FieldValues_ = new Hashtable();
                this.FieldLength_.Add(8);
                this.FieldNames_.Add("Advertisement Count");
                this.FieldValues_.Add("Advertisement Count", HeaderParser.ToUInt(data, 32, 8));
                this.FieldLength_.Add(8);
                this.FieldNames_.Add("Address Entry Size");
                this.FieldValues_.Add("Address Entry Size", HeaderParser.ToUInt(data, 40, 8));
                this.FieldLength_.Add(16);
                this.FieldNames_.Add("Life Time");
                this.FieldValues_.Add("Life Time", HeaderParser.ToUInt(data, 48, 16));
                ret           = 8;
                this.dataIsIp = false;
                break;

            case 30:
                FieldNames_  = new ArrayList();
                FieldLength_ = new ArrayList();
                FieldValues_ = new Hashtable();
                this.FieldLength_.Add(16);
                this.FieldNames_.Add("Identifier");
                this.FieldValues_.Add("Identifier", HeaderParser.ToUInt(data, 32, 16));
                this.FieldLength_.Add(16);
                this.FieldNames_.Add("Unused");
                this.FieldValues_.Add("Unused", HeaderParser.ToUInt(data, 48, 16));
                this.FieldLength_.Add(16);
                this.FieldNames_.Add("Out Bount Hop Count");
                this.FieldValues_.Add("Out Bount Hop Count", HeaderParser.ToUInt(data, 64, 16));
                this.FieldLength_.Add(16);
                this.FieldNames_.Add("Return Hop Count");
                this.FieldValues_.Add("Return Hop Count", HeaderParser.ToUInt(data, 80, 16));
                this.FieldLength_.Add(32);
                this.FieldNames_.Add("Output Link Speed");
                this.FieldValues_.Add("Output Link Speed", HeaderParser.ToUInt(data, 96, 32));
                this.FieldLength_.Add(32);
                this.FieldNames_.Add("Output Link MTU");
                this.FieldValues_.Add("Output Link MTU", HeaderParser.ToUInt(data, 128, 32));
                ret           = 20;
                this.dataIsIp = false;
                break;

            case 40:
                FieldNames_  = new ArrayList();
                FieldLength_ = new ArrayList();
                FieldValues_ = new Hashtable();
                this.FieldLength_.Add(16);
                this.FieldNames_.Add("Reserved");
                this.FieldValues_.Add("Reserved", HeaderParser.ToUInt(data, 32, 16));
                this.FieldLength_.Add(16);
                this.FieldNames_.Add("Pointer");
                this.FieldValues_.Add("Pointer", HeaderParser.ToUInt(data, 48, 16));
                ret           = 8;
                this.dataIsIp = false;
                break;

            default: break;
            }
            return(ret);
        }