示例#1
0
        private void HandleRData(NetBinaryReader nbr)
        {
            long startPos = nbr.BaseStream.Position;

            switch (Type)
            {
                case (QType.A):
                case (QType.AAAA):
                    recordValue = new IPAddress(nbr.ReadBytes(ByteCount));
                    break;
                    /*
                case (QType.NS):
                case (QType.CNAME):
                    recordValue = nbr.ReadLblOrPntString();
                    ReadPadOctets(nbr, startPos);
                    break;
                case (QType.SOA):
                    recordValue = new SOA(nbr);
                    ReadPadOctets(nbr, startPos);
                    break;
                    */
                default:
                    recordValue = nbr.ReadBytes(ByteCount);
                    break;
            }
        }
示例#2
0
        public DnsQuestion(NetBinaryReader nbr)
        {
            List<string> labels = new List<string>();
            byte nameLength;

            while ((nameLength = nbr.ReadByte()) != 0)
            {
                string label = string.Empty;

                for (int i = 0; i < nameLength; i++)
                {
                    label += (char)nbr.ReadByte();
                }

                labels.Add(label);
            }

            QueriedDomainName = string.Join(".", labels);
            Type = (QType)nbr.ReadUInt16();

            ushort rawClass = nbr.ReadUInt16();
            if (rawClass > 65279) rawClass = 0;
            else if (rawClass > 4 && rawClass < 252) rawClass = 2;
            else if (rawClass > 255 && rawClass < 65280) rawClass = 2;
            Class = (DnsClass)rawClass;
        }
示例#3
0
        public IPHeader(byte[] byBuffer, int nReceived)
        {
            NetBinaryReader nbr = new NetBinaryReader(byBuffer, 0, nReceived);

            Version = nbr.ReadNible();
            HeaderLength = nbr.ReadNible();

            Precedence = (Precedence)nbr.ReadCustomAmount(3);
            LowDelay = nbr.ReadBit();
            HighThroughput = nbr.ReadBit();
            HighRelibility = nbr.ReadBit();
            nbr.SkipPadBits();

            TotalLength = nbr.ReadUInt16();
            Identification = nbr.ReadUInt16();

            nbr.ReadBit();
            MayFragment = !nbr.ReadBit();
            LastFragment = !nbr.ReadBit();
            FragmentOffset = (nbr.ReadPadBits() << 3) + nbr.ReadByte();

            TTL = nbr.ReadByte();
            Protocol = (ProtocolType)nbr.ReadByte();
            HeaderChecksum = IPAddress.NetworkToHostOrder(nbr.ReadInt16());

            Source = new IPAddress(nbr.ReadBytes(4));
            Destination = new IPAddress(nbr.ReadBytes(4));
        }
示例#4
0
        public DnsPacket(Packet packet)
        {
            Header = (packet.ApplicationHeader as DnsHeader);
            if (Header == null) throw new ArgumentException("Non-Dns packet!");
            NetBinaryReader nbr = new NetBinaryReader(Header.data);

            Questions = new DnsQuestion[Header.QuestionCount];
            for (int i = 0; i < Questions.Length; i++)
            {
                Questions[i] = new DnsQuestion(nbr);
            }

            Answers = new DnsAnswer[Header.AnswerCount];
            for (int i = 0; i < Answers.Length; i++)
            {
                Answers[i] = new DnsAnswer(nbr);
            }

            Authority = new DnsAnswer[Header.AuthorityCount];
            for (int i = 0; i < Authority.Length; i++)
            {
                Authority[i] = new DnsAnswer(nbr);
            }

            Information = new DnsAnswer[Header.AdditionalCount];
            for (int i = 0; i < Information.Length; i++)
            {
                Information[i] = new DnsAnswer(nbr);
            }
        }
示例#5
0
文件: SOA.cs 项目: Arzana/Sniffles
 public SOA(NetBinaryReader nbr)
 {
     PrimaryNS = nbr.ReadLblOrPntString();
     AdminMB = nbr.ReadLblOrPntString();
     SerialNbr = nbr.ReadUInt32();
     RefreshInterval = nbr.ReadUInt32();
     RetryInterval = nbr.ReadUInt32();
     ExpirationLimit = nbr.ReadUInt32();
     MaxTTL = nbr.ReadUInt32();
 }
示例#6
0
        public TcpHeader(byte[] byIpData, int start)
        {
            NetBinaryReader nbr = new NetBinaryReader(byIpData, start);

            SourcePort = nbr.ReadUInt16();
            DestinationPort = nbr.ReadUInt16();

            SequenceNumber = nbr.ReadUInt32();
            AcknowledgmentNumber = nbr.ReadUInt32();

            DataOffset = nbr.ReadNible();
            nbr.SkipPadBits();

            nbr.ReadCustomAmount(2);
            URG = nbr.ReadBit();
            ACK = nbr.ReadBit();
            PSH = nbr.ReadBit();
            RST = nbr.ReadBit();
            SYN = nbr.ReadBit();
            FIN = nbr.ReadBit();

            Window = nbr.ReadUInt16();
            Checksum = nbr.ReadUInt16();
            UrgentPointer = nbr.ReadUInt16();

            //TODO: test
            byte option;
            long optionsStart = nbr.BaseStream.Position;
            Options = new byte[0][];

            while ((option = nbr.ReadByte()) != 0 || nbr.BaseStream.Position > start + DataOffset)    // 0 = End list
            {
                if (option == 1) continue;  // 1 = No Option
                if (option == 2)            // Max Segm size
                {
                    byte length = nbr.ReadByte();

                    byte[] cur = new byte[length];
                    for (byte i = 0; i < length; i++)
                    {
                        cur[i] = nbr.ReadByte();
                    }

                    Array.Resize(ref Options, Options.Length + 1);
                    Options[Options.Length - 1] = cur;
                }
            }

            long dataLength = DataOffset - optionsStart;
            Data = new byte[dataLength];
            for (int i = 0; i < dataLength; i++)
            {
                Data[i] = nbr.ReadByte();
            }
        }
示例#7
0
        public UdpHeader(byte[] byIpData, int start, int bytesReceived)
        {
            NetBinaryReader nbr = new NetBinaryReader(byIpData, start);

            SourcePort = nbr.ReadUInt16();
            DestinationPort = nbr.ReadUInt16();

            ByteCount = nbr.ReadUInt16();
            Checksum = nbr.ReadUInt16();

            data = new byte[ByteCount - OCTET_COUNT];
            Array.Copy(byIpData, start + OCTET_COUNT, data, 0, data.Length);
        }
示例#8
0
        public DnsAnswer(NetBinaryReader nbr)
        {
            Name = nbr.ReadLblOrPntString();
            Type = (QType)nbr.ReadUInt16();

            ushort rawClass = nbr.ReadUInt16();
            if (rawClass > 65279) rawClass = 0;
            else if (rawClass > 4 && rawClass < 252) rawClass = 2;
            else if (rawClass > 255 && rawClass < 65280) rawClass = 2;
            Class = (DnsClass)rawClass;

            TTL = nbr.ReadUInt32();
            ByteCount = nbr.ReadUInt16();
            HandleRData(nbr);
        }
示例#9
0
        public IcmpHeader(byte[] byIpData, int start, int bytesReceived)
        {
            NetBinaryReader nbr = new NetBinaryReader(byIpData, start);

            byte type = nbr.ReadByte();
            if (type == 2 || type == 7) type = 1;
            else if (type > 20 && type < 30) type = 20;
            else if (type > 41) type = 41;
            Type = (MessageType)type;

            Code = nbr.ReadByte();
            Checksum = nbr.ReadUInt16();

            data = new byte[bytesReceived - start - OCTET_COUNT];
            Array.Copy(byIpData, start + OCTET_COUNT, data, 0, data.Length);
        }
示例#10
0
        public DnsHeader(byte[] byIpData, int start, int bytesReceived)
        {
            NetBinaryReader nbr = new NetBinaryReader(byIpData, start);

            Indentifier = nbr.ReadUInt16();

            QueryOrResponseFlag = nbr.ReadBit();
            OperationCode = (OpCode)nbr.ReadNible();
            AuthoritativeAnswer = nbr.ReadBit();
            Turncation = nbr.ReadBit();
            RecursionDesired = nbr.ReadBit();

            RecursionAvailable = nbr.ReadBit();
            nbr.ReadCustomAmount(3);
            ResponseCode = (RCode)nbr.ReadNible();

            QuestionCount = nbr.ReadUInt16();
            AnswerCount = nbr.ReadUInt16();
            AuthorityCount = nbr.ReadUInt16();
            AdditionalCount = nbr.ReadUInt16();

            data = new byte[bytesReceived - start - OCTET_COUNT];
            Array.Copy(byIpData, start + OCTET_COUNT, data, 0, data.Length);
        }
示例#11
0
 private void ReadPadOctets(NetBinaryReader nbr, long startPos)
 {
     long padding = ByteCount - (nbr.BaseStream.Position - startPos);
     nbr.BaseStream.Position += padding;
 }