Esempio n. 1
0
        internal DnsResponse(DnsReader br)
        {
            _header = new DnsHeader(br);

            _numQuestions         = br.ReadInt16();
            _numAnswers           = br.ReadInt16();
            _numNameServers       = br.ReadInt16();
            _numAdditionalRecords = br.ReadInt16();

#if (MF)
            _questions         = new Question[_numQuestions];
            _answers           = new Answer[_numAnswers];
            _authorities       = new Authority[_numNameServers];
            _additionalRecords = new Additional[_numAdditionalRecords];
#endif


#if (MF)
            for (int i = 0; i < _numQuestions; i++)
            {
                _questions[i] = new Question(br);
            }

            for (int i = 0; i < _numAnswers; i++)
            {
                _answers[i] = new Answer(br);
            }

            for (int i = 0; i < _numNameServers; i++)
            {
                _authorities[i] = new Authority(br);
            }

            for (int i = 0; i < _numAdditionalRecords; i++)
            {
                _additionalRecords[i] = new Additional(br);
            }
#else
            for (int i = 0; i < _numQuestions; i++)
            {
                _questions.Add(new Question(br));
            }

            for (int i = 0; i < _numAnswers; i++)
            {
                _answers.Add(new Answer(br));
            }

            for (int i = 0; i < _numNameServers; i++)
            {
                _authorities.Add(new Authority(br));
            }

            for (int i = 0; i < _numAdditionalRecords; i++)
            {
                _additionalRecords.Add(new Additional(br));
            }
#endif
        }
Esempio n. 2
0
 internal SOARecord(DnsReader br)
 {
     _mname      = br.ReadDomain();
     _rname      = br.ReadDomain();
     _serial     = br.ReadInt32();
     _refresh    = br.ReadInt32();
     _retry      = br.ReadInt32();
     _expire     = br.ReadInt32();
     _minimumTtl = br.ReadInt32();
 }
Esempio n. 3
0
        /// <summary>
        /// Reads a domain name from the byte array. (RFC 1035 - 4.1.4.)
        /// </summary>
        public string ReadDomain()
        {
            StringBuilder domain = new StringBuilder();
            int           length = 0;

            while ((length = ReadByte()) != 0)
            {
                // top 2 bits set denotes domain name compression and to reference elsewhere
                if ((length & 0xc0) == 0xc0)
                {
                    // work out the existing domain name, copy this pointer
                    DnsReader newPointer = Copy();

                    // and move it to where specified here
                    newPointer.Position = (length & 0x3f) << 8 | ReadByte();

                    // repeat call recursively
                    domain.Append(newPointer.ReadDomain());
                    return(domain.ToString());
                }

                // if not using compression, copy a char at a time to the domain name
                while (length > 0)
                {
                    domain.Append(ReadChar());
                    length--;
                }

                // if size of next label isn't null (end of domain name) add a period ready for next label
                if (Peek() != 0)
                {
                    domain.Append('.');
                }
            }

            return(domain.ToString());
        }
Esempio n. 4
0
        internal ResourceRecord(DnsReader br)
        {
            _domain = br.ReadDomain();
            _qtype  = (DnsType)br.ReadInt16();
            _qclass = (DnsClass)br.ReadInt16();
            _ttl    = br.ReadInt32();

            int recordLength = br.ReadInt16();

            if (recordLength != 0)
            {
                switch (_qtype)
                {
                case DnsType.A:     _record = new ARecord(br);      break;

                case DnsType.CNAME: _record = new CNAMERecord(br);  break;

                case DnsType.MX:    _record = new MXRecord(br);     break;

                case DnsType.NS:    _record = new NSRecord(br);     break;

                case DnsType.SOA:   _record = new SOARecord(br);    break;

                case DnsType.TXT:   _record = new TXTRecord(br);    break;

                case DnsType.PTR:       _record = new PTRERecord(br);   break;

                // NetBIOS related records
                case DnsType.NB:    _record = new NBRecord(br);     break;

                default:
                    br += recordLength;
                    break;
                }
            }
        }
Esempio n. 5
0
 internal ARecord(DnsReader br)
 {
     _ipAddress = new IPAddress(br.ReadBytes(4));
 }
Esempio n. 6
0
 internal Authority(DnsReader br) : base(br)
 {
 }
Esempio n. 7
0
 internal Question(DnsReader br)
 {
     _domain = br.ReadDomain();
     _qtype  = (DnsType)br.ReadInt16();
     _qclass = (DnsClass)br.ReadInt16();
 }
Esempio n. 8
0
 internal CNAMERecord(DnsReader br)
 {
     _domain = br.ReadDomain();
 }
Esempio n. 9
0
 internal NSRecord(DnsReader br)
 {
     _domainName = br.ReadDomain();
 }
Esempio n. 10
0
 internal MXRecord(DnsReader br)
 {
     _preference = br.ReadInt16();
     _domainName = br.ReadDomain();
 }
Esempio n. 11
0
        public DnsResponse Resolve(DnsRequest request)
        {
            int attempts = 0;

            if (request.Header.MessageID == 0)
            {
                request.Header.MessageID = _nextMessageID++;
            }

            while (attempts <= _maxRetryAttemps)
            {
                byte[] bytes = request.GetMessage();

                if (bytes.Length > 512)
                {
                    throw new ArgumentException("RFC 1035 2.3.4 states that the maximum size of a UDP datagram is 512 octets (bytes).");
                }

                Socket socket = new Socket(
#if (MF)
                    AddressFamily.InterNetwork
#else
                    _endPoint.AddressFamily
#endif
                    , SocketType.Dgram, ProtocolType.Udp);


                socket.ReceiveTimeout = 300;
                //socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 300);

                socket.SendTo(bytes, bytes.Length, SocketFlags.None, _endPoint);

                // Messages carried by UDP are restricted to 512 bytes (not counting the IP
                // or UDP headers).  Longer messages are truncated and the TC bit is set in
                // the header. (RFC 1035 4.2.1)
                byte[] responseMessage = new byte[512];

                try
                {
                    //int numBytes = socket.Receive(responseMessage);

                    EndPoint ep       = (EndPoint)_endPoint;
                    int      numBytes = socket.ReceiveFrom(responseMessage, ref ep);

                    if (numBytes == 0 || numBytes > 512)
                    {
                        throw new Exception("RFC 1035 2.3.4 states that the maximum size of a UDP datagram is 512 octets (bytes).");
                    }

                    DnsReader   br  = new DnsReader(responseMessage);
                    DnsResponse res = new DnsResponse(br);

                    if (request.Header.MessageID == res.Header.MessageID)
                    {
                        return(res);
                    }

                    _lastException = new Exception("Not the answer for the current query.");
                    attempts++;
                }
                catch (SocketException se)
                {
                    _lastException = se;
                    attempts++;
                }
                finally
                {
                    socket.Close();
                    socket = null;
                }
            }

            throw new Exception("Could not resolve the query (" + attempts + " attempts).");
        }
Esempio n. 12
0
 internal Additional(DnsReader br) : base(br)
 {
 }
Esempio n. 13
0
 internal TXTRecord(DnsReader br)
 {
     _txtData = br.ReadString();
 }
Esempio n. 14
0
        internal DnsResponse Invoke(DnsRequest request, bool isQuery)
        {
            int attempts = 0;

            if (request.Header.MessageID == 0)
            {
                request.Header.MessageID = _nextMessageID++;
            }

            while (attempts <= _maxRetryAttemps)
            {
                byte[] bytes = request.GetMessage();

                if (bytes.Length > 512)
                {
                    throw new ArgumentException("RFC 1035 2.3.4 states that the maximum size of a UDP datagram is 512 octets (bytes).");
                }

                Socket socket = null;

                try
                {
                    socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

                    socket.ReceiveTimeout = 300;
                    //socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 300);

#if (DEBUG && MF)
                    Microsoft.SPOT.Debug.Print(MFToolkit.IO.ByteUtil.PrintBytes(bytes));
#endif

                    socket.SendTo(bytes, bytes.Length, SocketFlags.None, new IPEndPoint(IPAddress.Parse("192.168.178.255"), 137));

                    if (!isQuery)
                    {
                        return(null);
                    }

                    // Messages carried by UDP are restricted to 512 bytes (not counting the IP
                    // or UDP headers).  Longer messages are truncated and the TC bit is set in
                    // the header. (RFC 1035 4.2.1)
                    byte[] responseMessage = new byte[512];


                    //int numBytes = socket.Receive(responseMessage);

                    EndPoint ep       = (EndPoint) new IPEndPoint(new IPAddress(4294967295), 137);
                    int      numBytes = socket.ReceiveFrom(responseMessage, ref ep);

                    if (numBytes == 0 || numBytes > 512)
                    {
                        throw new Exception("RFC 1035 2.3.4 states that the maximum size of a UDP datagram is 512 octets (bytes).");
                    }

                    DnsReader   br  = new DnsReader(responseMessage);
                    DnsResponse res = new DnsResponse(br);

                    if (request.Header.MessageID == res.Header.MessageID)
                    {
                        return(res);
                    }

                    _lastException = new Exception("Not the answer for the current query.");
                    attempts++;
                }
                catch (SocketException se)
                {
                    _lastException = se;
                    attempts++;
                }
                finally
                {
                    socket.Close();
                    socket = null;
                }
            }

            throw new Exception("Could not resolve the query (" + attempts + " attempts).");
        }
Esempio n. 15
0
 internal Answer(DnsReader br) : base(br)
 {
 }
Esempio n. 16
0
 internal NBRecord(DnsReader br)
 {
     _flags     = br.ReadUInt16();
     _ipAddress = new IPAddress(br.ReadBytes(4));
 }