ARSoft.Tools.Net - C# DNS client/server, SPF and SenderID Library
DNS Server
Really simple dns server
Copy Code
class Program
{
    static void Main(string[] args)
    {
        using (DnsServer server = new DnsServer(10, 10))
        {
            server.QueryReceived += OnQueryReceived;

            server.Start();

            Console.WriteLine("Press any key to stop server");
            Console.ReadLine();
        }
    }

    static async Task OnQueryReceived(object sender, QueryReceivedEventArgs e)
    {
        DnsMessage query = e.Query as DnsMessage;

        if (query == null)
            return;

        DnsMessage response = query.CreateResponseInstance();

        // check for valid query
        if ((query.Questions.Count == 1)
            && (query.Questions[0].RecordType == RecordType.Txt)
            && (query.Questions[0].Name.Equals(DomainName.Parse("example.com"))))
        {
            response.ReturnCode = ReturnCode.NoError;
            response.AnswerRecords.Add(new TxtRecord(DomainName.Parse("example.com"), 3600, "Hello world"));
        }
        else
        {
            response.ReturnCode = ReturnCode.ServerFailure;
        }

        // set the response
        e.Response = response;
    }

 

Really simple forwarding server
Copy Code
class Program
{
    static void Main(string[] args)
    {
        using (DnsServer server = new DnsServer(IPAddress.Any, 10, 10))
        {
            server.QueryReceived += OnQueryReceived;

            server.Start();

            Console.WriteLine("Press any key to stop server");
            Console.ReadLine();
        }
    }

    static async void OnQueryReceived(object sender, QueryReceivedEventArgs e)
    {
        DnsMessage message = e.Query as DnsMessage;

        if (query == null)
            return;

        DnsMessage response = query.CreateResponseInstance();

        if ((message.Questions.Count == 1))
        {
            // send query to upstream server
            DnsQuestion question = message.Questions[0];
            DnsMessage upstreamResponse = await DnsClient.Default.ResolveAsync(question.Name, question.RecordType, question.RecordClass);

            // if got an answer, copy it to the message sent to the client
            if (answer != null)
            {
                foreach (DnsRecordBase record in (upstreamResponse.AnswerRecords))
                {
                    response.AnswerRecords.Add(record);
                }
                foreach (DnsRecordBase record in (upstreamResponse.AdditionalRecords))
                {
                    response.AdditionalRecords.Add(record);
                }

                response.ReturnCode = ReturnCode.NoError;

                // set the response
                e.Response = response;
            }
        }
    }
}

 

Only allow local requests
Copy Code
class Program
{
    static void Main(string[] args)
    {
        using (DnsServer server = new DnsServer(IPAddress.Any, 10, 10))
        {
            server.ClientConnected += OnClientConnected;
            server.QueryReceived += OnQueryReceived;

            server.Start();

            Console.WriteLine("Press any key to stop server");
            Console.ReadLine();
        }
    }

    static async void OnClientConnected(object sender, ClientConnectedEventArgs e)
    {
        if (!IPAddress.IsLoopback(e.RemoteEndpoint.Address))
            e.RefuseConnect = true;
    }

    static async void OnQueryReceived(object sender, QueryReceivedEventArgs e)
    {
        // process query as you like
    }
}

 

 


© 2010-2015 Alexander Reinert. All Rights Reserved.

Send Feedback


This documentation was created using Document! X