Implementing a Versatile DNS Server in Python: Handling A, AAAA, CNAME, and TXT Records

CK1820 
Created at Mar 16, 2024 05:55:37 
80   0   0   0  

Supporting AAAA records, which are used for IPv6 addresses, is a straightforward extension of the previous examples. The AAAA record type is essential for modern applications that operate over IPv6 networks. Here's how you can modify the existing Python DNS server example to handle AAAA records in addition to A, CNAME, and TXT records:

from socket import socket, AF_INET, SOCK_DGRAM
from dnslib import DNSRecord, RR, QTYPE, TXT

# Define DNS records (for demonstration purposes)
dns_records = {
    'example.com.': ('A', '192.168.1.100'),
    'ipv6.example.com.': ('AAAA', '2001:0db8:85a3:0000:0000:8a2e:0370:7334'),
    'sub.example.com.': ('A', '192.168.1.101'),
    'another.example.com.': ('A', '192.168.1.102'),
    'alias.example.com.': ('CNAME', 'example.com.'),
    'info.example.com.': ('TXT', 'This is a test TXT record'),
}

# Create UDP socket
sock = socket(AF_INET, SOCK_DGRAM)
sock.bind(('0.0.0.0', 53))  # Bind to port 53

# Handle DNS queries
while True:
    data, addr = sock.recvfrom(512)  # Buffer size is 512 bytes
    dns_request = DNSRecord.parse(data)

    # Prepare DNS response
    dns_response = DNSRecord(DNSRecord.HEADER.ID(dns_request.header.id))
    dns_response.add_question(dns_request.q.qname)

    qname = str(dns_request.q.qname)

    # Check if the query name exists in the records
    if qname in dns_records:
        record_type, value = dns_records[qname]
        if record_type == 'A':
            dns_response.add_answer(RR(qname, QTYPE.A, rdata=value, ttl=60))
        elif record_type == 'AAAA':
            dns_response.add_answer(RR(qname, QTYPE.AAAA, rdata=value, ttl=60))
        elif record_type == 'CNAME':
            dns_response.add_answer(RR(qname, QTYPE.CNAME, rdata=value, ttl=60))
            # Optionally, add the corresponding A or AAAA record for the CNAME
            if value in dns_records and (dns_records[value][0] == 'A' or dns_records[value][0] == 'AAAA'):
                dns_response.add_answer(RR(value, QTYPE[dns_records[value][0]], rdata=dns_records[value][1], ttl=60))
        elif record_type == 'TXT':
            dns_response.add_answer(RR(qname, QTYPE.TXT, rdata=TXT(value), ttl=60))

    # Send DNS response
    sock.sendto(dns_response.pack(), addr)

In this version, an example AAAA record for ipv6.example.com. with an IPv6 address (2001:0db8:85a3:0000:0000:8a2e:0370:7334) is added to the dns_records dictionary. This dictionary maps domain names to tuples that specify the record type ('A', 'AAAA', 'CNAME', 'TXT') and the record's value (IP address or text).

When constructing the DNS response, the code now checks for 'AAAA' record types alongside the other types and adds the corresponding RR object with QTYPE.AAAA and the IPv6 address as the rdata if a match is found.

This enhanced server is capable of responding to DNS queries for IPv4 (A), IPv6 (AAAA), canonical names (CNAME), and text records (TXT), covering a broader range of DNS record types.



Tags: DNS Records DNS Server IPv6 Internet Protocols Networking Programming Tutorial Python Socket Programming Web Development Share on Facebook Share on X

◀ PREVIOUS
Dynamic DNS Made Easy: Building a Python-Based Solution
▶ NEXT
Mastering AI: A Beginner's Guide to Python Programming and Beyond
  Comments 0
Login for comment
SIMILAR POSTS

Building a Simple DNS Server in Delphi with TTL Support (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)

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

Enable/Disable IPv6 on Windows (updated at Feb 24, 2024)

Public DNS (Domain Name Service) based on IPv4, IPv6 widely used (updated at Feb 23, 2024)

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

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

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

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

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

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

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

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

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

PHP socket programming to get content with post method (created at Oct 19, 2009)

OTHER POSTS IN THE SAME CATEGORY

How do I determine the client IP type in Python - IPv4 or IPv6 (updated at Apr 13, 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)

Getting Started with PyTorch: A Beginner's Guide to Building Your First Neural Network (updated 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)

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)

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

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)

Building a Simple DNS Server in Delphi with TTL Support (created at Mar 16, 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)

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)