XU XUv0.16.0

#github.com/xulang-org/packages/dns

import: dns #github.com/xulang-org/packages/dns

counts: 9 functions, 14 methods, 4 types, 6 constants, 0 method sets

External package `dns #github.com/xulang-org/packages/dns`, provided by github.com/xulang-org/packages.

github.com/xulang-org/packages · 1.17.2 · Install and use

Examples · API declarations

Examples

These sources come from the official examples repository and can be copied or downloaded. Run them with a local Xu SDK in the complete project; network, database, and desktop examples require the corresponding services or assets.

49_standalone_dns/main.xu

Required SDK: Xu 0.10.5 · Complete project · Download source

XuLocal SDK example
@main

dns #github.com/xulang-org/packages/dns
#errors
#strings

fn verify_dns_framing_and_codec() {
    println("--- 1. Testing DNS RFC 1035 Packet Encoding & Answer Parsing ---")

    // 1. Domain label encoding
    enc := dns.encode_domain("api.google.com")
    // "\x03api\x06google\x03com\x00" -> length 1 + 3 + 1 + 6 + 1 + 3 + 1 = 16 bytes
    if enc.len == 16 {
        println("   [PASS] Domain label encoded cleanly: length 16 bytes")
    } else {
        println("   [FAIL] Unexpected encoded domain length: " + str(enc.len))
    }

    // 2. Query packet generation
    pkt := dns.build_query_packet(0x1234, "example.com", dns.TYPE_A)
    // 12 bytes header + (1+7+1+3+1 = 13 bytes name) + 2 bytes qtype + 2 bytes qclass = 29 bytes
    if pkt.len == 29 {
        println("   [PASS] Query packet length verified: 29 bytes")
    } else {
        println("   [FAIL] Unexpected query packet length: " + str(pkt.len))
    }

    // 3. Mock DNS Answer parsing with compression pointer (0xC00C)
    // Header (12 bytes): ID 0x1234, Flags 0x8180 (response), QDCOUNT 1, ANCOUNT 1, NS 0, AR 0
    $hdr := string_from_byte(0x12) + string_from_byte(0x34) + string_from_byte(0x81) + string_from_byte(0x80)
    hdr = hdr + string_from_byte(0x00) + string_from_byte(0x01) + string_from_byte(0x00) + string_from_byte(0x01)
    hdr = hdr + string_from_byte(0x00) + string_from_byte(0x00) + string_from_byte(0x00) + string_from_byte(0x00)

    // Question: "\x07example\x03com\x00" + TYPE A (0x0001) + CLASS IN (0x0001)
    q := dns.encode_domain("example.com") + string_from_byte(0x00) + string_from_byte(0x01) + string_from_byte(0x00) + string_from_byte(0x01)

    // Answer: Name pointer 0xC00C (points to offset 12) + TYPE A (0x0001) + CLASS IN (0x0001) + TTL (0x0000003c) + RDLENGTH 4 + IP 93.184.216.34
    $ans := string_from_byte(0xc0) + string_from_byte(0x0c) + string_from_byte(0x00) + string_from_byte(0x01) + string_from_byte(0x00) + string_from_byte(0x01)
    ans = ans + string_from_byte(0x00) + string_from_byte(0x00) + string_from_byte(0x00) + string_from_byte(0x3c)
    ans = ans + string_from_byte(0x00) + string_from_byte(0x04)
    ans = ans + string_from_byte(93) + string_from_byte(184) + string_from_byte(216) + string_from_byte(34)

    mock_resp := hdr + q + ans
    ips := dns.parse_a_records(mock_resp)
    if ips.len == 1 && ips[0] == "93.184.216.34" {
        println("   [PASS] Compression pointer resolved correctly -> IP: " + ips[0])
    } else {
        println("   [FAIL] Mock answer parsing failed!")
    }
}

fn verify_dns_pool() {
    println("\n--- 2. Testing DNS Connection Pool ---")
    $p := dns.open_pool("8.8.8.8:53", 5)

    // Connection borrow & return test
    fd1 := p.get_conn()
    if fd1 >= 0 {
        println("   Borrow 1 fd: " + str(fd1))
        p.put_conn(fd1)

        fd2 := p.get_conn()
        println("   Borrow 2 (after return) fd: " + str(fd2))
        if fd1 == fd2 {
            println("   [PASS] Connection reused successfully (fd " + str(fd1) + " == " + str(fd2) + ")")
        }

        fd3 := p.get_conn()
        println("   Borrow 3 (concurrently) fd: " + str(fd3))
        if fd2 != fd3 {
            println("   [PASS] Concurrently allocated distinct fds (" + str(fd2) + " != " + str(fd3) + ")")
        }
        p.put_conn(fd2)
        p.put_conn(fd3)

        // Try TCP query
        ips := p.resolve_a("google.com") >>> (err) {
            println("   [WARN] External DNS query skipped: " + err.msg())
            p.close()
            <<
        }
        if ips.len > 0 {
            println("   [PASS] Live TCP DNS Query google.com resolved " + str(ips.len) + " IP(s): " + ips[0])
        }
    } else {
        println("   [INFO] Offline environment - pool mock passed.")
    }

    p.close()
    println("   [PASS] DNS Pool closed cleanly.")
}

fn main() {
    println("=== Xu DNS Native Driver Verification ===\n")
    verify_dns_framing_and_codec()
    verify_dns_pool()
    println("\n[DNS VERIFICATION 100% COMPLETED]")
}

API declarations

client

XuAPI
pub fn client(endpoint string) DnsClient

Purpose

Execute client.

Parameters

endpoint string
endpoint (string)

Returns

DnsClient — a value of this type

pool

XuAPI
pub fn pool(endpoint string, max_conns int) DnsPool

Purpose

Execute pool.

Parameters

endpoint string
endpoint (string)
max_conns int
max_conns (int)

Returns

DnsPool — a value of this type

struct DnsClient

XuAPI
pub struct DnsClient {
    pub host string
    pub port int
}

Purpose

Dns Client session maintaining connection and operations.

Public fields

host string
host
port int
port

new_client

XuAPI
pub fn new_client(endpoint string) DnsClient

Purpose

Construct client.

Parameters

endpoint string
endpoint (string)

Returns

DnsClient — a value of this type

(DnsClient) connect

XuAPI
pub fn (c DnsClient) connect() int

Purpose

Establish network connection to the target server endpoint. Throws on failure; handle the error or propagate it to the caller.

Parameters

c DnsClient
The DnsClient instance used by this call (value receiver).

Returns

int — a value of this type

(DnsClient) resolve_a_on_fd

XuAPI
pub fn (c DnsClient) resolve_a_on_fd(fd int, domain string) []string

Purpose

Queries A-records over TCP DNS (RFC 1035 section 4.2.2) Throws on failure; handle the error or propagate it to the caller.

Parameters

c DnsClient
The DnsClient instance used by this call (value receiver).
fd int
file descriptor (int)
domain string
domain (string)

Returns

[]string — slice result

(DnsClient) resolve_a

XuAPI
pub fn (c DnsClient) resolve_a(domain string) []string

Purpose

Operation on DnsClient: resolve a. Throws on failure; handle the error or propagate it to the caller.

Parameters

c DnsClient
The DnsClient instance used by this call (value receiver).
domain string
domain (string)

Returns

[]string — slice result

const TYPE_A

XuAPI
pub const TYPE_A = 1

Purpose

Constant TYPE_A, defining protocol or algorithm parameters.

See usage example

const TYPE_NS

XuAPI
pub const TYPE_NS = 2

Purpose

Constant TYPE_NS, defining protocol or algorithm parameters.

const TYPE_CNAME

XuAPI
pub const TYPE_CNAME = 5

Purpose

Constant TYPE_CNAME, defining protocol or algorithm parameters.

const TYPE_SOA

XuAPI
pub const TYPE_SOA = 6

Purpose

Constant TYPE_SOA, defining protocol or algorithm parameters.

const TYPE_TXT

XuAPI
pub const TYPE_TXT = 16

Purpose

Constant TYPE_TXT, defining protocol or algorithm parameters.

const CLASS_IN

XuAPI
pub const CLASS_IN = 1

Purpose

Constant CLASS_IN, defining protocol or algorithm parameters.

encode_domain

XuAPI
pub fn encode_domain(domain string) string

Purpose

Encodes "www.example.com" -> "\x03www\x07example\x03com\x00"

Parameters

domain string
domain (string)

Returns

string — result string

See usage example

build_query_packet

XuAPI
pub fn build_query_packet(tx_id int, domain string, qtype int) string

Purpose

Builds a standard 12-byte DNS Header + Question section

Parameters

tx_id int
tx_id (int)
domain string
domain (string)
qtype int
qtype (int)

Returns

string — result string

See usage example

struct DnsAnswer

XuAPI
pub struct DnsAnswer {
    pub name string
    pub rtype int
    pub rclass int
    pub ttl int
    pub data string
}

Purpose

Structure encapsulating name, rtype, rclass, ttl, data.

Public fields

name string
name
rtype int
field (int)
rclass int
field (int)
ttl int
field (int)
data string
data

parse_a_records

XuAPI
pub fn parse_a_records(pkt string) []string

Purpose

Parses DNS response and extracts IPv4 addresses (Type A)

Parameters

pkt string
pkt (string)

Returns

[]string — slice result

See usage example

struct DnsPool

XuAPI
pub struct DnsPool {
    pub client DnsClient
    pub idle chan int
    pub max_idle int
}

Purpose

Dns Connection pool manager scheduling reusable socket connections.

Public fields

client DnsClient
field (DnsClient)
idle chan int
field (channel; type chan int)
max_idle int
field (int)

open_pool

XuAPI
pub fn open_pool(endpoint string, max_idle int) DnsPool

Purpose

Open pool.

Parameters

endpoint string
endpoint (string)
max_idle int
max_idle (int)

Returns

DnsPool — a value of this type

See usage example

(&DnsPool) get_conn

XuAPI
pub fn (p &DnsPool) get_conn() int

Purpose

Operation on &DnsPool: read conn.

Parameters

p &DnsPool
The DnsPool instance used by this call (borrow).

Returns

int — a value of this type

(&DnsPool) put_conn

XuAPI
pub fn (p &DnsPool) put_conn(fd int)

Purpose

Operation on &DnsPool: put connection.

Parameters

p &DnsPool
The DnsPool instance used by this call (borrow).
fd int
file descriptor (int)

Returns

This call does not return a value.

(&DnsPool) resolve_a

XuAPI
pub fn (p &DnsPool) resolve_a(domain string) []string

Purpose

Operation on &DnsPool: resolve a. Throws on failure; handle the error or propagate it to the caller.

Parameters

p &DnsPool
The DnsPool instance used by this call (borrow).
domain string
domain (string)

Returns

[]string — slice result

(&DnsPool) close

XuAPI
pub fn (p &DnsPool) close()

Purpose

Operation on &DnsPool: close the resource.

Parameters

p &DnsPool
The DnsPool instance used by this call (borrow).

Returns

This call does not return a value.

struct TlsClient

XuAPI
pub struct TlsClient {
    pub conn transport.Conn
    pub host string
    pub port int
}

Purpose

Dns Client session maintaining connection and operations.

Public fields

conn transport.Conn
field (transport.Conn)
host string
host
port int
port

tls_connect

XuAPI
pub fn tls_connect(endpoint string, default_port int) TlsClient

Purpose

Tls connect. Throws on failure; handle the error or propagate it to the caller.

Parameters

endpoint string
endpoint (string)
default_port int
default_port (int)

Returns

TlsClient — a value of this type

tls_connect_config

XuAPI
pub fn tls_connect_config(endpoint string, default_port int, cfg tls.Config) TlsClient

Purpose

Tls connect config. Throws on failure; handle the error or propagate it to the caller.

Parameters

endpoint string
endpoint (string)
default_port int
default_port (int)
cfg tls.Config
cfg (tls.Config)

Returns

TlsClient — a value of this type

($&TlsClient) write_all

XuAPI
pub fn (c $&TlsClient) write_all(data string) int

Purpose

Operation on $&TlsClient: write all. Throws on failure; handle the error or propagate it to the caller.

Parameters

c $&TlsClient
The TlsClient instance used by this call (exclusive borrow).
data string
data (string)

Returns

int — a value of this type

($&TlsClient) read

XuAPI
pub fn (c $&TlsClient) read(max int) string

Purpose

Operation on $&TlsClient: read up to max bytes. Throws on failure; handle the error or propagate it to the caller.

Parameters

c $&TlsClient
The TlsClient instance used by this call (exclusive borrow).
max int
maximum (int)

Returns

string — result string

($&TlsClient) read_exact

XuAPI
pub fn (c $&TlsClient) read_exact(need int) string

Purpose

Operation on $&TlsClient: read exact. Throws on failure; handle the error or propagate it to the caller.

Parameters

c $&TlsClient
The TlsClient instance used by this call (exclusive borrow).
need int
need (int)

Returns

string — result string

($&TlsClient) close

XuAPI
pub fn (c $&TlsClient) close() int

Purpose

Operation on $&TlsClient: close the resource. Throws on failure; handle the error or propagate it to the caller.

Parameters

c $&TlsClient
The TlsClient instance used by this call (exclusive borrow).

Returns

int — a value of this type

($&TlsClient) set_deadline

XuAPI
pub fn (c $&TlsClient) set_deadline(t_ns int) int

Purpose

Operation on $&TlsClient: set deadline. Throws on failure; handle the error or propagate it to the caller.

Parameters

c $&TlsClient
The TlsClient instance used by this call (exclusive borrow).
t_ns int
t_ns (int)

Returns

int — a value of this type

($&TlsClient) set_read_deadline

XuAPI
pub fn (c $&TlsClient) set_read_deadline(t_ns int) int

Purpose

Operation on $&TlsClient: set read deadline. Throws on failure; handle the error or propagate it to the caller.

Parameters

c $&TlsClient
The TlsClient instance used by this call (exclusive borrow).
t_ns int
t_ns (int)

Returns

int — a value of this type

($&TlsClient) set_write_deadline

XuAPI
pub fn (c $&TlsClient) set_write_deadline(t_ns int) int

Purpose

Operation on $&TlsClient: set write deadline. Throws on failure; handle the error or propagate it to the caller.

Parameters

c $&TlsClient
The TlsClient instance used by this call (exclusive borrow).
t_ns int
t_ns (int)

Returns

int — a value of this type