/// <summary> /// Adds a question to the request to be sent to the DNS server. /// </summary> /// <param name="question">The question to add to the request</param> public void AddQuestion(Question question) { // abandon if null if (question == null) throw new ArgumentNullException("question"); // add this question to our collection _questions.Add(question); }
/// <summary> /// Do an QClass=IN Query on specified DNS servers /// </summary> /// <param name="name">Name to query</param> /// <param name="qtype">Question type</param> /// <returns>Response of the query</returns> public Response Query(string name, QType qtype) { var question = new Question(name, qtype, QClass.IN); var response = SearchInCache(question); if (response != null) return response; var request = new Request(); request.AddQuestion(question); return GetResponse(request); }
private Response SearchInCache(Question question) { if (!_mUseCache) return null; var strKey = question.QClass + "-" + question.QType + "-" + question.QName; Response response; lock (_mResponseCache) { if (!_mResponseCache.ContainsKey(strKey)) return null; response = _mResponseCache[strKey]; } var timeLived = (int) ((DateTime.Now.Ticks - response.TimeStamp.Ticks)/TimeSpan.TicksPerSecond); foreach (var rr in response.RecordsRR) { rr.TimeLived = timeLived; // The TTL property calculates its actual time to live if (rr.TTL == 0) return null; // out of date } return response; }
/// <summary> /// Construct a Response object from the supplied byte array /// </summary> /// <param name="message">a byte array returned from a DNS server query</param> internal Response(byte[] message) { // the bit flags are in bytes 2 and 3 var flags1 = message[2]; var flags2 = message[3]; // get return code from lowest 4 bits of byte 3 var returnCode = flags2 & 15; // if its in the reserved section, set to other if (returnCode > 6) returnCode = 6; _returnCode = (ReturnCode)returnCode; // other bit flags _authoritativeAnswer = ((flags1 & 4) != 0); _recursionAvailable = ((flags2 & 128) != 0); _truncated = ((flags1 & 2) != 0); // create the arrays of response objects _questions = new Question[GetShort(message, 4)]; _answers = new Answer[GetShort(message, 6)]; _nameServers = new NameServer[GetShort(message, 8)]; _additionalRecords = new AdditionalRecord[GetShort(message, 10)]; // need a pointer to do this, position just after the header var pointer = new Pointer(message, 12); // and now populate them, they always follow this order for (var index = 0; index < _questions.Length; index++) { try { // try to build a quesion from the response _questions[index] = new Question(pointer); } catch (Exception ex) { // something grim has happened, we can't continue throw new InvalidResponseException(ex); } } for (var index = 0; index < _answers.Length; index++) { _answers[index] = new Answer(pointer); } for (var index = 0; index < _nameServers.Length; index++) { _nameServers[index] = new NameServer(pointer); } for (var index = 0; index < _additionalRecords.Length; index++) { _additionalRecords[index] = new AdditionalRecord(pointer); } }