Building a Simple DNS Server in Delphi with TTL Support

CK CK 3788 
Created at  
687 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 Indy library Network programming Time-To-Live (TTL) UDP communication programming Facebook X
Comments 0
Similar posts
  1. Implementing a Versatile DNS Server in Python: Handling A, AAAA, CNAME, and TXT Records
    1,078
  2. Building a Basic DNS Server in PHP/Python: A Beginner's Guide
    746
  3. Mastering Excel Data Importation in PHP
    558
  1. Run PowerShell Command as Administrator in Delphi
    659
  2. Run command with Administrator permission in Delphi on Microsoft Windows
    767
  3. Delphi code to send data to web server based on POST method
    1,036
  4. Good Delphi components
    931
  5. How to download/grap web pages or XML document at internet website in Delphi XE?
    880
  6. Delphi Program can command level execution program with hidden mode
    703
  7. How To Turn On/Off Monitor with Delphi
    857
  8. How to get application version number in Delphi
    1,559
  9. The Delphi function to get My Videos folder
    2,337
  10. Get Application Data Path on Delphi
    839
  11. I want to select directory(folder) not file. How can I do that ?
    624
  12. Changing the Title of a Print Dialog in Delphi
    1,098
  13. Auto Logon Programming in Delphi
    1,034
  14. Thread Application Implementation in TThread
    670
  15. Is there directory selection VCL component in Delphi ?
    523
Recently updated
  1. Google OAuth Token Expiration: Causes and Solutions
    29
  2. Why 100% SEO Guarantees Better User Engagement and How to Achieve It in Your Service
    55
  3. Why SPF and DKIM Are Essential for Linux Email Servers?
    49
  4. How do I create animated background for Google Meet?
    22,910
  5. MaxMind GeoLite2 vs. DB-IP Lite vs. ip-api.com: Choosing the Right Free IP Geolocation Solution
    271
  6. Meet Valkey: Why Redis Changed Its License, Why Valkey is Better, and How to Use It as a Drop-in Replacement
    103
  7. How Rogue Web Scrapers Are Stealing My Content and Destroying My Server
    168
  8. Array of arguments passed to script in PHP
    97
  9. Invalid Auction Storms in Ad Tech
    106
  10. Building MRC-Compliant Instrumentation in Ad Tech: Implementation Steps and Key Challenges
    207
  11. The Two Clocks of Ad Server Architecture
    77
  12. Frequency Cap Architecture Overview
    526
  13. Collecting Proof-of-Play from 100,000 Screens
    152
  14. Vector Search in Relational Databases (RDBs): Trends & Usage Environments
    302
  15. What is RedisVL?
    114
  16. Designing a Skill Registry Architecture: Minimize Token Usage and Maximize AI Performance
    246
  17. TPU & GPU Cluster Management Tools
    257
  18. The Concept of Scaffolding in AI Agents
    244
  19. Beyond Static Pipelines: Architecting LLM Multi-Agent Systems for Autonomous Workflow Orchestration
    596
  20. AI-DLC vs. AI-SDLC: Model-Centric vs. System-Centric AI Development
    267
  21. Fine-Tuning Open-Weight LLMs: Training, Licensing, and Deploying Custom Models with Ollama
    353
  22. Inside the Next-Generation Streaming Architecture: Ultra-Low Latency, SGAI, and AI Codecs
    206
  23. Machine Learning: Training vs. Ranking
    229
  24. The Continuous Learning Machine: Designing a Loop That Actually Turns
    228
  25. The Next-Gen Low-Latency Standards: LL-HLS & MoQ
    202
  26. Designing a Data Curation System Around a Vector Database
    230
  27. Extracting Context from Live Video: The Architecture Got Simpler When We Stopped Building Features
    263
  28. The New Ad Stack Behind Live Sports Streaming: Inside SGAI, EABN, and the Standards Race to Fix Live Monetization
    672
  29. Designing MLOps Architectures to Handle Seasonal Data Drift in EV Fleet Models
    201
  30. From 15 to 40+ FPS: Accelerating PyTorch Object Detection on Automotive ECUs
    232