public static DnsDatagram ReadFromJson(dynamic jsonResponse) { var data = new DnsDatagram { QR = 1, //Is Response OPCODE = 0, //StandardQuery TC = (byte)(jsonResponse.TC.Value ? 1 : 0), RD = (byte)(jsonResponse.RD.Value ? 1 : 0), RA = (byte)(jsonResponse.RA.Value ? 1 : 0), AD = (byte)(jsonResponse.AD.Value ? 1 : 0), CD = (byte)(jsonResponse.CD.Value ? 1 : 0), RCODE = (byte)jsonResponse.Status }; if (jsonResponse.Question == null) { data.Question = Array.Empty <DnsRecords.QuestionItem>(); } else { var question = new List <DnsRecords.QuestionItem>(Convert.ToUInt16(jsonResponse.Question.Count)); data.Question = question; foreach (var jsonQuestionRecord in jsonResponse.Question) { question.Add(new DnsRecords.QuestionItem(jsonQuestionRecord)); } } if (jsonResponse.Answer == null) { data.Answer = Array.Empty <DnsRecords.RecordItem>(); } else { var answer = new List <DnsRecords.RecordItem>(Convert.ToUInt16(jsonResponse.Answer.Count)); data.Answer = answer; foreach (var jsonAnswerRecord in jsonResponse.Answer) { answer.Add(new DnsRecords.RecordItem(jsonAnswerRecord)); } } if (jsonResponse.Authority == null) { data.Authority = Array.Empty <DnsRecords.RecordItem>(); } else { var authority = new List <DnsRecords.RecordItem>(Convert.ToUInt16(jsonResponse.Authority.Count)); data.Authority = authority; foreach (var jsonAuthorityRecord in jsonResponse.Authority) { authority.Add(new DnsRecords.RecordItem(jsonAuthorityRecord)); } } if (jsonResponse.Additional == null) { data.Additional = Array.Empty <DnsRecords.RecordItem>(); } else { var additional = new List <DnsRecords.RecordItem>(Convert.ToUInt16(jsonResponse.Additional.Count)); data.Additional = additional; foreach (var jsonAdditionalRecord in jsonResponse.Additional) { additional.Add(new DnsRecords.RecordItem(jsonAdditionalRecord)); } } return(data); }
public static DnsDatagram ReadFromDnsMessage(DnsMessage dnsResponse) { dnsResponse.IsEDnsEnabled = false; dnsResponse.EDnsOptions = null; dnsResponse.TSigOptions = null; var data = new DnsDatagram { QR = 1, OPCODE = 0, TC = (byte)(dnsResponse.IsTruncated ? 1 : 0), RD = (byte)(dnsResponse.IsRecursionDesired ? 1 : 0), RA = (byte)(dnsResponse.IsRecursionAllowed ? 1 : 0), AD = (byte)(dnsResponse.IsAuthenticData ? 1 : 0), CD = (byte)(dnsResponse.IsCheckingDisabled ? 1 : 0), RCODE = (byte)dnsResponse.ReturnCode }; if (dnsResponse.Questions == null) { data.Question = Array.Empty <DnsRecords.QuestionItem>(); } else { var question = new List <DnsRecords.QuestionItem>(Convert.ToUInt16(dnsResponse.Questions.Count)); data.Question = question; question.AddRange(from dynamic jsonQuestionRecord in dnsResponse.Questions select new DnsRecords.QuestionItem(jsonQuestionRecord)); } if (dnsResponse.AnswerRecords == null) { data.Answer = Array.Empty <DnsRecords.RecordItem>(); } else { var answer = new List <DnsRecords.RecordItem>(Convert.ToUInt16(dnsResponse.AnswerRecords.Count)); data.Answer = answer; answer.AddRange(from jsonAnswerRecord in dnsResponse.AnswerRecords where !jsonAnswerRecord.Name.IsSubDomainOf(DomainName.Parse("arashi-msg")) && !jsonAnswerRecord.Name.IsSubDomainOf(DomainName.Parse("nova-msg")) || jsonAnswerRecord.RecordType != RecordType.Txt select new DnsRecords.RecordItem(jsonAnswerRecord)); } if (dnsResponse.AuthorityRecords == null) { data.Authority = Array.Empty <DnsRecords.RecordItem>(); } else { var authority = new List <DnsRecords.RecordItem>(Convert.ToUInt16(dnsResponse.AuthorityRecords.Count)); data.Authority = authority; authority.AddRange(dnsResponse.AuthorityRecords.Select(jsonAuthorityRecord => new DnsRecords.RecordItem(jsonAuthorityRecord))); } if (dnsResponse.AdditionalRecords == null) { data.Additional = Array.Empty <DnsRecords.RecordItem>(); } else { var additional = new List <DnsRecords.RecordItem>(Convert.ToUInt16(dnsResponse.AdditionalRecords.Count)); data.Additional = additional; additional.AddRange(dnsResponse.AdditionalRecords.Select(jsonAdditionalRecord => new DnsRecords.RecordItem(jsonAdditionalRecord))); } return(data); }