示例#1
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).");
        }
示例#2
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).");
        }
示例#3
0
 internal DnsResponse Invoke(DnsRequest request)
 {
     return(Invoke(request, true));
 }