public async Task <bool> GetHost(string url, DNSState state) { try { IPHostEntry ret = await Dns.GetHostEntryAsync(url).ConfigureAwait(false); Log_Info("Success Lookup of " + url); state.AddAnswer(url, ret.AddressList[0].GetAddressBytes()); } catch (SocketException ex) { Log_Error("Failed Lookup of " + url); if (ex.NativeErrorCode != 11001 & //Host not found (Authoritative) ex.NativeErrorCode != 11002) //Host not found (Non-Authoritative) { Log_Error("Unexpected error of " + ex.NativeErrorCode); lock (errSentry) lastTaskError = ex; return(false); } } if (state.DecCounter() == 0) { FinDNS(state); } return(true); }
public void FinDNS(DNSState state) { Log_Info("DNS Packet Sent From CLR_DEV9 DNS server"); DNS retPay = state.DNS; string[] reqs = state.Questions; ConcurrentDictionary <string, byte[]> answers = state.GetAnswers(); foreach (string req in reqs) { if (answers.ContainsKey(req)) { DNSResponseEntry ans = new DNSResponseEntry(req, 1, 1, answers[req], 10800); retPay.Answers.Add(ans); } else { //Log_Error("Missing an Answer"); retPay.RCode = 2; //ServerFailure } } byte[] udpPayload = retPay.GetBytes(); if (udpPayload.Length > 512) { throw new NotImplementedException("DNS packet larger than 512bytes"); } UDP retudp = new UDP(udpPayload); retudp.SourcePort = 53; retudp.DestinationPort = state.ClientPort; _recvBuff.Enqueue(retudp); //Interlocked.Decrement(ref open); }
public bool CheckHost(string url, DNSState state) { if (hosts.ContainsKey(url.ToLower())) { state.AddAnswer(url, hosts[url]); Log_Info(url + " found in hosts"); //Add entry to DNS state if (state.DecCounter() == 0) { FinDNS(state); } return(true); } return(false); }
public override bool Send(IPPayload payload) { Log_Info("DNS Packet Sent To CLR_DEV9 DNS server"); Log_Info("Contents"); DNS dns = new DNS(payload.GetPayload()); if (dns.OPCode == (byte)DNSOPCode.Query & dns.QuestionCount > 0 & dns.QR == false) { List <string> reqs = new List <string>(); foreach (DNSQuestionEntry q in dns.Questions) { if (q.Type == 1 & q.Class == 1) { reqs.Add(q.Name); } else { Log_Error("Unexpected question type of class, T:" + q.Type.ToString() + " C:" + q.Class.ToString()); } } if (reqs.Count == 0) { return(true); } if (dns.TC == true) { throw new NotImplementedException("Truncated DNS packet"); } //Interlocked.Increment(ref open); DNS ret = new DNS { ID = dns.ID, //TODO, drop duplicate requests based on ID QR = true, OPCode = (byte)DNSOPCode.Query, AA = false, TC = false, RD = true, RA = true, AD = false, CD = false, RCode = (byte)DNSRCode.NoError, //Counts Questions = dns.Questions }; DNSState state = new DNSState(reqs.Count, reqs.ToArray(), ret, ((UDP)payload).SourcePort); foreach (string req in reqs) { if (CheckHost(req, state)) { continue; } Task <bool> res = GetHost(req, state); } return(true); } else { Log_Error("Unexpected OPCode, Code:" + dns.OPCode); return(true); } }