예제 #1
0
파일: DnsQuery.cs 프로젝트: yslib/minimvc
        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);

                    _reqSocket.ReceiveTimeout = timeout;

                    _reqSocket.SendTo(_query, _query.Length, SocketFlags.None, _dnsServer);
                    _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
                }

            }
            return res;
        }
예제 #2
0
파일: DnsQuery.cs 프로젝트: dumpvn/minimvc
        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);

                    _reqSocket.ReceiveTimeout = timeout;

                    _reqSocket.SendTo(_query, _query.Length, SocketFlags.None, _dnsServer);
                    _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
                }
            }
            return(res);
        }