Building a Simple DNS Server in Delphi with TTL Support

CK1820 
Created at Mar 16, 2024 06:09:52 
122   0   0   0  

Below is a basic Delphi code example to implement a DNS server capable of handling A, AAAA, CNAME, and TXT records with Time-To-Live (TTL) support. This example uses the IdUDPServer component from Indy (Internet Direct) library for handling UDP packets.

unit DNS_Server;
interface
uses
 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
 Dialogs, IdUDPServer, IdSocketHandle, IdGlobal, IdStack, IdDNSCommon,
 IdDNSResolver;
type
 TForm1 = class(TForm)
   UDPServer: TIdUDPServer;
   procedure FormCreate(Sender: TObject);
   procedure UDPServerUDPRead(AThread: TIdUDPListenerThread;
     AData: TBytes; ABinding: TIdSocketHandle);
 private
   { Private declarations }
 public
   { Public declarations }
 end;
var
 Form1: TForm1;
implementation
{$R *.dfm}
// Sample DNS records
const
 DNSRecords: array [0..2] of TIdRR = (
   (Host: 'example.com'; RDATA: '192.168.1.1'; TTL: 3600; RClass: inClass; RType: ARecord),
   (Host: 'example.com'; RDATA: '2001:0db8:85a3:0000:0000:8a2e:0370:7334'; TTL: 3600; RClass: inClass; RType: AAAARecord),
   (Host: 'cname.example.com'; RDATA: 'example.com'; TTL: 3600; RClass: inClass; RType: CNameRecord)
 );
procedure TForm1.FormCreate(Sender: TObject);
begin
 UDPServer.DefaultPort := 53;
 UDPServer.Active := True;
end;
function LookupRecord(const Query: TIdDNSQuery; var Response: TIdDNSQuery): Boolean;
var
 i: Integer;
begin
 Result := False;
 for i := 0 to High(DNSRecords) do
 begin
   if (Query.QType = DNSRecords[i].RType) and
      (SameText(Query.QName, DNSRecords[i].Host)) then
   begin
     Response.QType := DNSRecords[i].RType;
     Response.QName := DNSRecords[i].Host;
     Response.QClass := DNSRecords[i].RClass;
     Response.TTL := DNSRecords[i].TTL;
     Response.RData := DNSRecords[i].RData;
     Result := True;
     Break;
   end;
 end;
end;
procedure TForm1.UDPServerUDPRead(AThread: TIdUDPListenerThread;
 AData: TBytes; ABinding: TIdSocketHandle);
var
 Query, Response: TIdDNSQuery;
 ResponseData: TBytes;
begin
 Query := TIdDNSQuery.Create;
 Response := TIdDNSQuery.Create;
 try
   Query.Decode(AData);
   if LookupRecord(Query, Response) then
   begin
     Response.Encode(ResponseData);
     UDPServer.SendBuffer(ABinding.PeerIP, ABinding.PeerPort, ResponseData);
   end;
 finally
   Query.Free;
   Response.Free;
 end;
end;
end.

This code sets up a basic DNS server using the Indy library. It listens on UDP port 53 and handles DNS queries. The LookupRecord function mimics a simple DNS lookup by checking against hardcoded DNS records and populating the response if a matching record is found. This example supports A, AAAA, and CNAME record types with TTL. You can extend it to support other record types like TXT by adding them to the DNSRecords array and updating the LookupRecord function accordingly.



Tags: A record AAAA record CNAME record DNS resolution DNS server Delphi programming Indy library Network programming Time-To-Live (TTL) UDP communication Share on Facebook Share on X

◀ PREVIOUS
Run PowerShell Command as Administrator in Delphi
  Comments 0
Login for comment
SIMILAR POSTS

Implementing a Versatile DNS Server in Python: Handling A, AAAA, CNAME, and TXT Records (created at Mar 16, 2024)


OTHER POSTS IN THE SAME CATEGORY

Run PowerShell Command as Administrator in Delphi (updated at Mar 09, 2024)

Run command with Administrator permission in Delphi on Microsoft Windows (updated at Mar 09, 2024)

Delphi code to send data to web server based on POST method (updated at Mar 09, 2024)

Good Delphi components (created at Apr 03, 2016)

How to download/grap web pages or XML document at internet website in Delphi XE? (created at Jul 20, 2015)

Delphi Program can command level execution program with hidden mode (updated at Jan 15, 2024)

How To Turn On/Off Monitor with Delphi (updated at Mar 09, 2024)

How to get application version number in Delphi (updated at Mar 09, 2024)

The Delphi function to get My Videos folder (created at Apr 29, 2011)

Get Application Data Path on Delphi (updated at Mar 09, 2024)

I want to select directory(folder) not file. How can I do that ? (created at Apr 29, 2011)

Changing the Title of a Print Dialog in Delphi (created at Apr 20, 2011)

Auto Logon Programming in Delphi (created at Dec 31, 2010)

Thread Application Implementation in TThread (created at Apr 14, 2010)

Is there directory selection VCL component in Delphi ? (created at Sep 08, 2009)

UPDATES

Creating a Pinterest-Style Card Layout with Bootstrap and Masonry (created at Apr 24, 2024)

Mastering Excel Data Importation in PHP (updated at Apr 24, 2024)

JSON format control in PHP (updated at Apr 24, 2024)

Equal Height Blocks in Bootstrap with JavaScript (created at Apr 22, 2024)

How to convert integer to text string ? (updated at Apr 22, 2024)

Checking similarity between two strings in PHP (updated at Apr 21, 2024)

Create Blob Image in HTML based on the given Text, Width and Height in the Center of the Image without saving file (updated at Apr 21, 2024)

How do I determine the client IP type (IPv4/IPv6) in PHP (updated at Apr 16, 2024)

How do I determine the client IP type in Python - IPv4 or IPv6 (updated at Apr 13, 2024)

Getting Started with PyTorch: A Beginner's Guide to Building Your First Neural Network (updated at Apr 09, 2024)

Predicting Buyer Preferences with PyTorch: A Deep Learning Approach (updated at Apr 09, 2024)

Forecasting the Weather with PyTorch: A Beginner's Guide to Temperature Prediction (created at Apr 09, 2024)

PyTorch example to Forcast Stock Price based on 10 days Dataset (created at Apr 09, 2024)

Mastering Model Persistence: Saving and Loading Trained Machine Learning Models in Python (created at Apr 08, 2024)

Harnessing the Power of Random Forest Algorithm in Python (created at Apr 08, 2024)

Understanding and Implementing K-Nearest Neighbors (KNN) Algorithm in Python (created at Apr 08, 2024)

Forecasting with Linear Regression and KNN Regression in Python (updated at Apr 07, 2024)

What is 302 Found Redirection in HTTP 1.1? (created at Apr 04, 2024)

Mastering Random Forest Regression: A Comprehensive Guide with Python Examples (updated at Apr 01, 2024)

Python Implementation of Linear Regression (updated at Apr 01, 2024)

Mastering Supervised Machine Learning with Python: A Comprehensive Guide (created at Apr 01, 2024)

Mastering AI: A Beginner's Guide to Python Programming and Beyond (created at Apr 01, 2024)

How do I create animated background for Google Meet? (updated at Mar 28, 2024)

How to force cookies, disable php sessid in URL ? (updated at Mar 16, 2024)

Implementing a Versatile DNS Server in PHP: Handling A, AAAA, CNAME, and TXT Records (updated at Mar 16, 2024)

Implementing a Versatile DNS Server in Python: Handling A, AAAA, CNAME, and TXT Records (created at Mar 16, 2024)

Building a Basic DNS Server in PHP/Python: A Beginner's Guide (updated at Mar 15, 2024)

Dynamic DNS Made Easy: Building a Python-Based Solution (created at Mar 15, 2024)

Exploring the Depths of Data Transfer: sendfile vs. kTLS (created at Mar 15, 2024)

How Netflix Ensures Smooth Streaming with Open Connect CDN (updated at Mar 15, 2024)