DNS Forwarding

Feb 13, 2011 at 9:10 PM

What a great library, it's really useful - thank you. I'm trying to create a DNS forwarder but it seems a little hit and miss, any ideas?

Regards, Paul.

Coordinator
Feb 19, 2011 at 2:40 PM

Sorry for my late answer to your post and your email.

I've just added an example for a really simple DNS forwarding server.

At the moment it is just a hint, how to start, but there is to do something more like it is described in chapter 7 of RFC 1035. I look forward to expand this example in the next few months, but I hope the short example helps you for now.

Regards, Alex.

Feb 19, 2011 at 6:53 PM

Thanks Alex. I managed to figure it out but my codes a little different and probably mostly useless but I thought I'd share it anyway as it seems to work:

private DnsMessageBase ProcessDnsQuery(DnsMessageBase _Query, IPAddress Client, System.Net.Sockets.ProtocolType Protocol)
{
    DnsMessage Query = (DnsMessage)_Query;
    Query.IsQuery = false;

    if ((Query != null) && (Query.Questions.Count == 1))
    {
        foreach (DnsQuestion Question in Query.Questions)
        {
            DnsMessage Record = DnsClient.Default.Resolve(Question.Name, Question.RecordType, Question.RecordClass);

            if (Record != null && Record.ReturnCode == ReturnCode.NoError && Record.AnswerRecords.Count >= 1)
            {
                foreach (DnsRecordBase Answer in Record.AnswerRecords)
                {
                    if (Answer != null)
                    {
                        string ResolvedTo;

                        if (Answer.RecordType == RecordType.A)
                        {
                            ARecord AnswerRecord = (ARecord)Answer;
                            Query.AnswerRecords.Add(new ARecord(Answer.Name, Answer.TimeToLive, AnswerRecord.Address));
                            ResolvedTo = AnswerRecord.Address.ToString();
                        }
                        else if (Answer.RecordType == RecordType.CName)
                        {
                            CNameRecord AnswerRecord = (CNameRecord)Answer;
                            Query.AnswerRecords.Add(new CNameRecord(Answer.Name, Answer.TimeToLive, AnswerRecord.CanonicalName));
                            ResolvedTo = AnswerRecord.CanonicalName;
                        }
                        else if (Answer.RecordType == RecordType.Ptr)
                        {
                            PtrRecord AnswerRecord = (PtrRecord)Answer;
                            Query.AnswerRecords.Add(new PtrRecord(Answer.Name, Answer.TimeToLive, AnswerRecord.PointerDomainName));
                            ResolvedTo = AnswerRecord.PointerDomainName;
                        }
                        else
                        {
                            Query.ReturnCode = ReturnCode.ServerFailure;
                            return Query;
                        }

                        Log("[DNS]\t {0} resolved to {1} (request from {2})", Question.Name, ResolvedTo, Client.ToString());
                    }
                }

                Query.ReturnCode = ReturnCode.NoError;
            }
            else
            {
                Query.ReturnCode = ReturnCode.NxDomain;
            }
        }
    }

    return Query;
}

Regards, Paul.