РУС Site Map Contact Us Home
E-mail Newsletter
Subscribe to get informed about
Clever Components news.

Your Name:
Your Email:
 
SUBSCRIBE
 
Previous Newsletters
 




Products Articles Downloads Order Support
Customer Portal   

Simple DNS Server in Delphi

Abstract

This article describes how to write your own DNS server in Delphi by using Clever Internet Suite Delphi Internet components. It is a simple DNS server which can be a primary DNS server for domains in your local Network as well as a caching DNS server for the Internet.

About DNS components in Clever Internet Suite

The DNS server functionality in Clever Internet Suite is represented by two components: DnsServer and DnsFileHandler. The DnsServer component is the fully-functional DNS server that supports all DNS commands and the mostly used DNS records, including A, MX, NS, PTR, SOA and TXT records.

This component manages DNS records and zones in memory and uses events for notifying users about adding, deleting and searching of DNS records. You can use these events to override the standard component behavior and implement your own functionality.

The other component, DnsFileHandler, is used for managing of DNS zone files and storing of cached records on the disk. This component handles some events of DnsServer for its work.

The sources of this component are free and can be downloaded here

You can use this as starting point for developing your own functionality: storing DNS records in databases, XML files, implementing custom authentication procedures, etc.

How it works

DNS server components

For running simple DNS server, you need to put both the DnsServer and DnsFileHandler components on to the Delphi form, assign the Server property of the DnsFileHandler component with DnsServer instance and call the DnsServer.Start method.

This will work without having to write a single line of source code!

You can customize each step of receiving a DNS request, processing the request and sending a response DNS message to the client. Here are the most important component members:

RootNameServers. This property of DnsServer allows you to specify the names of primary NS servers for domains on the Internet. The DNS server names (or IP addresses of NS servers) from this property will be used by DnsServer as a source for obtaining information about domains on the Internet. Information, obtained from these name servers, will be cached by DnsServer. If you run your DNS server within your home Network, you can specify the DNS server address of your Internet provider. If you use your DnsServer as a secondary name server, you can specify the address of the primary DNS server here. By default, 13 root Internet name servers are defined.

The UseCaching property allows the DnsServer component to work as a caching DNS server.

The UseRecursiveQueries property specifies whether to request DNS information recursively from DNS servers on the Internet if a record is not found.

The Port specifies the port number at which the component listens to incoming connections. The LocalBinding property allows you to specify the binding interface in case you run your server on a multi-homed PC.

Both the HandedZonesPath and CachedZonesPath properties of the DnsFileHandler component specify the paths to store handed zone files and cached records.

The ZoneManager property of the DnsFileHandler component is an instance of the Zone Manager class that allows you to manage zone files on the disk. See below for more details.

How to customize DNS server and implement your own DNS record storage

If you want to extend the default behavior of DnsFileHandler, you can use the sources of this component and write new components.

Also, you can implement all this functionality without having to write a new component. All that you need to do is to handle the following events of the DnsServer component and write your code within the event handler methods:

TclDnsServer.OnGetHandedRecords;
TclDnsServer.OnGetCachedRecords;
TclDnsServer.OnAddCachedRecords;
TclDnsServer.OnDeleteCachedRecords.

Samples of implementing the events above can be found in the DnsFileHandler sources that can be downloaded here

Please note: if you use the DnsFileHandler component together with DnsServer, you cannot handle the events above in your code since DnsFileHandler internally handles it. All remaining events are fully available: OnReceiveQuery, OnSendResponse, OnStart, OnStop, OnServerError, etc.
Please check the DnsServer description to learn more about component members.

You can use these events for logging purposes or custom client authentication:

procedure TForm1.clDnsServer1ReceiveQuery(Sender: TObject;
   AConnection: TclUdpUserConnection; AQuery: TclDnsMessage);
begin
   if not CustomAuthentication(AConnection.PeerIP) then
   begin
      raise Exception.Create('Authentication failed');
   end;
   PutLogMessage(AConnection.PeerIP + ' - query received');
end;

All DnsServer events are not thread-safe. If you want to access VCL components within event handlers, you will need to add special synchronization code: use critical sections or TThread.Synchronize method.

Managing DNS zone files

There is special class, TclDnsZoneManager, that can be used for creating and modifying zone files on the disk. You can create an instance of this class and use it directly in your code as well as use the special ZoneManager property that is available in DnsFileHandler.

var
   aRecord: TclDnsARecord;
begin
   aRecord := TclDnsARecord.Create();
   aRecord.Name := 'mydomain.com';
   aRecord.IPAddress := '1.2.3.4';
   clDnsFileHandler1.ZoneManager.Records.Add(aRecord);

   clDnsFileHandler1.ZoneManager.ZonePath := 'c:\zonefiles\';
   clDnsFileHandler1.ZoneManager.Save('mydomain.com');
end;

Please note: for creating valid handed DNS zones, you must add the SOA record as well:

   soaRecord := TclDnsSOARecord.Create();
   soaRecord.Name := 'mydomain.com';
   soaRecord.PrimaryNameServer := 'nameserver.com';
   soaRecord.ResponsibleMailbox := 'admin.mydomain.com';
   clDnsFileHandler1.ZoneManager.Records.Add(soaRecord);

Download source code

The source code for Simple DNS Server can be downloaded here:

The program can be compiled in Delphi 5-7, 2005-2010, XE and XE2 since the Clever Internet Suite supports all these versions of Delphi and C++ Builder as well.

Please feel free to contact me at Customer Portal

 

Sergey Shirokov, Internet components developer,
Clever Components team
www.CleverComponents.com

    Copyright © 2000-2017