예제 #1
0
        /// <summary>
        /// Quey a Dns Seerver for records of a specific type and timeout.
        /// </summary>
        /// <param name="recType"></param>
        /// <param name="timeout"></param>
        /// <returns></returns>
        public DnsAnswer QueryServer(RecordType recType, int timeout)
        {
            if (dnsServer == null)
            {
                throw new DnsQueryException("There is no Dns server set in Dns Query Component", null);
            }
            if (!ValidRecordType(recType))
            {
                throw new DnsQueryException("Invalid Record Type submitted to Dns Query Component", null);
            }

            //Result object
            DnsAnswer res = null;

            //UDP being unreliable we may need to try multiple times
            //therefore we count how many times we have tried
            numTries = 0;

            //as per RFC 1035  there is a max size on the dns response
            byte[]      dnsResponse = new byte[512];
            Exception[] exceptions  = new Exception[MAX_TRIES];

            while (numTries < MAX_TRIES)
            {
                try
                {
                    CreateDnsQuery(recType);
                    reqSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
#if !PocketPC
                    reqSocket.ReceiveTimeout = timeout;
#else
                    PPCSleep();
#endif
                    reqSocket.SendTo(query, query.Length, SocketFlags.None, dnsServer);
//#if !PocketPC
//                  reqSocket.ReceiveTimeout = timeout;
//#else
//                  PPCSleep();
//#endif
                    reqSocket.Receive(dnsResponse);

                    if (dnsResponse[0] == query[0] && dnsResponse[1] == query[1])
                    {
                        //this is our response so format and return the answer to the query
                        res = new DnsAnswer(dnsResponse);
                    }
                    numTries++;
                    if (res.ReturnCode == ReturnCode.Success)
                    {
                        return(res);
                    }
                }
                catch (SocketException ex)
                {
                    exceptions[numTries] = ex;
                    numTries++;
                    reqId++;
                    if (numTries > MAX_TRIES)
                    {
                        throw new DnsQueryException("Failure Querying DNS Server", exceptions);
                    }
                }
                finally
                {
                    reqId++;
                    reqSocket.Close();
                    Query = null; //Force a new message be built
                }
#if PocketPC
                //PocketPC CF .NET's Socket does require a latency to Connect.
                //.NET Sockets in 3.5 is improved, this will be taken out there.
                PPCSleep();
#endif
            }
            return(res);
        }
예제 #2
0
        /// <summary>
        /// Quey a Dns Seerver for records of a specific type and timeout.
        /// </summary>
        /// <param name="recType"></param>
        /// <param name="timeout"></param>
        /// <returns></returns>
        public DnsAnswer QueryServer(RecordType recType, int timeout)
        {
            if (dnsServer == null)
                throw new DnsQueryException("There is no Dns server set in Dns Query Component", null);
            if (!ValidRecordType(recType))
                throw new DnsQueryException("Invalid Record Type submitted to Dns Query Component", null);

            //Result object
            DnsAnswer res = null;

            //UDP being unreliable we may need to try multiple times 
            //therefore we count how many times we have tried
            numTries = 0;

            //as per RFC 1035  there is a max size on the dns response
            byte[] dnsResponse = new byte[512];
            Exception[] exceptions = new Exception[MAX_TRIES];

            while (numTries < MAX_TRIES)
            {
                try
                {
                    CreateDnsQuery(recType);
                    reqSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
#if !PocketPC
                    reqSocket.ReceiveTimeout = timeout;
#else
                    PPCSleep();
#endif
                    reqSocket.SendTo(query, query.Length, SocketFlags.None, dnsServer);
//#if !PocketPC
//                  reqSocket.ReceiveTimeout = timeout;
//#else
//                  PPCSleep();
//#endif
                    reqSocket.Receive(dnsResponse);

                    if (dnsResponse[0] == query[0] && dnsResponse[1] == query[1])
                        //this is our response so format and return the answer to the query
                        res = new DnsAnswer(dnsResponse);
                    numTries++;
                    if (res.ReturnCode == ReturnCode.Success)
                        return res;
                }
                catch (SocketException ex)
                {
                    exceptions[numTries] = ex;
                    numTries++;
                    reqId++;
                    if (numTries > MAX_TRIES)
                        throw new DnsQueryException("Failure Querying DNS Server", exceptions);
                }
                finally
                {
                    reqId++;
                    reqSocket.Close();
                    Query = null; //Force a new message be built
                }
#if PocketPC
                //PocketPC CF .NET's Socket does require a latency to Connect.
                //.NET Sockets in 3.5 is improved, this will be taken out there.
                PPCSleep();
#endif
            }
            return res;
        }