XU XUv0.16.0

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

导入方式: dns #github.com/xulang-org/packages/dns

公开 API: 9 函数, 14 方法, 4 类型, 6 常量, 0 方法集

扩展包 `dns #github.com/xulang-org/packages/dns`,由模块 github.com/xulang-org/packages 提供。

github.com/xulang-org/packages · 1.17.2 · 安装与使用

示例代码 · API 声明

示例代码

以下源码来自官方 examples 仓库,可复制或下载。请在完整工程中使用本地 Xu SDK 运行;网络、数据库和桌面示例需要对应的服务或资源。

49_standalone_dns/main.xu

所需 SDK:Xu 0.10.5 · 完整工程 · 下载源码

Xu本地 SDK 示例
@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 声明

client

XuAPI
pub fn client(endpoint string) DnsClient

用途

客户端。

参数

endpoint string
endpoint(string)

返回值

DnsClient — 该类型的值

pool

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

用途

执行 pool 处理。

参数

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

返回值

DnsPool — 该类型的值

struct DnsClient

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

用途

Dns 客户端会话实例,维护与服务端的连接并提供操作接口。

公开字段

host string
主机
port int
端口

new_client

XuAPI
pub fn new_client(endpoint string) DnsClient

用途

构造 client。

参数

endpoint string
endpoint(string)

返回值

DnsClient — 该类型的值

(DnsClient) connect

XuAPI
pub fn (c DnsClient) connect() int

用途

建立与目标服务端点的网络通信连接。失败时抛出错误,调用方需处理或继续传播。

参数

c DnsClient
本次调用使用的 DnsClient 实例;按值接收。

返回值

int — 该类型的值

(DnsClient) resolve_a_on_fd

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

用途

DnsClient 上的操作:执行 resolve a on fd 操作。失败时抛出错误,调用方需处理或继续传播。

参数

c DnsClient
本次调用使用的 DnsClient 实例;按值接收。
fd int
文件描述符(int)
domain string
domain(string)

返回值

[]string — 切片结果

(DnsClient) resolve_a

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

用途

DnsClient 上的操作:执行 resolve a 操作。失败时抛出错误,调用方需处理或继续传播。

参数

c DnsClient
本次调用使用的 DnsClient 实例;按值接收。
domain string
domain(string)

返回值

[]string — 切片结果

const TYPE_A

XuAPI
pub const TYPE_A = 1

用途

常量 TYPE_A,定义了相关的协议或算法配置参数。

查看用法示例

const TYPE_NS

XuAPI
pub const TYPE_NS = 2

用途

常量 TYPE_NS,定义了相关的协议或算法配置参数。

const TYPE_CNAME

XuAPI
pub const TYPE_CNAME = 5

用途

常量 TYPE_CNAME,定义了相关的协议或算法配置参数。

const TYPE_SOA

XuAPI
pub const TYPE_SOA = 6

用途

常量 TYPE_SOA,定义了相关的协议或算法配置参数。

const TYPE_TXT

XuAPI
pub const TYPE_TXT = 16

用途

常量 TYPE_TXT,定义了相关的协议或算法配置参数。

const CLASS_IN

XuAPI
pub const CLASS_IN = 1

用途

常量 CLASS_IN,定义了相关的协议或算法配置参数。

encode_domain

XuAPI
pub fn encode_domain(domain string) string

用途

编码 domain 数据。

参数

domain string
domain(string)

返回值

string — 结果字符串

查看用法示例

build_query_packet

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

用途

执行 build query packet 操作。

参数

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

返回值

string — 结果字符串

查看用法示例

struct DnsAnswer

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

用途

承载 name、rtype、rclass、ttl、data 等属性的结构体。

公开字段

name string
名字
rtype int
字段(int)
rclass int
字段(int)
ttl int
字段(int)
data string
数据

parse_a_records

XuAPI
pub fn parse_a_records(pkt string) []string

用途

解析为 a records。

参数

pkt string
pkt(string)

返回值

[]string — 切片结果

查看用法示例

struct DnsPool

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

用途

Dns 连接池管理器,并发安全地维护和调度复用套接字连接。

公开字段

client DnsClient
字段(DnsClient)
idle chan int
字段(通道;类型 chan int)
max_idle int
字段(int)

open_pool

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

用途

执行 open pool 操作。

参数

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

返回值

DnsPool — 该类型的值

查看用法示例

(&DnsPool) get_conn

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

用途

&DnsPool 上的操作:读取 conn。

参数

p &DnsPool
本次调用使用的 DnsPool 实例;以只读借用传入,调用方仍持有实例。

返回值

int — 该类型的值

(&DnsPool) put_conn

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

用途

&DnsPool 上的操作:执行 put conn 操作。

参数

p &DnsPool
本次调用使用的 DnsPool 实例;以只读借用传入,调用方仍持有实例。
fd int
文件描述符(int)

返回值

不返回值。

(&DnsPool) resolve_a

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

用途

&DnsPool 上的操作:执行 resolve a 操作。失败时抛出错误,调用方需处理或继续传播。

参数

p &DnsPool
本次调用使用的 DnsPool 实例;以只读借用传入,调用方仍持有实例。
domain string
domain(string)

返回值

[]string — 切片结果

(&DnsPool) close

XuAPI
pub fn (p &DnsPool) close()

用途

&DnsPool 上的操作:关闭资源。

参数

p &DnsPool
本次调用使用的 DnsPool 实例;以只读借用传入,调用方仍持有实例。

返回值

不返回值。

struct TlsClient

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

用途

Dns 客户端会话实例,维护与服务端的连接并提供操作接口。

公开字段

conn transport.Conn
字段(transport.Conn)
host string
主机
port int
端口

tls_connect

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

用途

执行 tls connect 操作。失败时抛出错误,调用方需处理或继续传播。

参数

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

返回值

TlsClient — 该类型的值

tls_connect_config

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

用途

执行 tls connect config 操作。失败时抛出错误,调用方需处理或继续传播。

参数

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

返回值

TlsClient — 该类型的值

($&TlsClient) write_all

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

用途

$&TlsClient 上的操作:执行 write all 操作。失败时抛出错误,调用方需处理或继续传播。

参数

c $&TlsClient
本次调用使用的 TlsClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
data string
数据(string)

返回值

int — 该类型的值

($&TlsClient) read

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

用途

$&TlsClient 上的操作:读出最多 max 字节。失败时抛出错误,调用方需处理或继续传播。

参数

c $&TlsClient
本次调用使用的 TlsClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
max int
上限(int)

返回值

string — 结果字符串

($&TlsClient) read_exact

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

用途

$&TlsClient 上的操作:执行 read exact 操作。失败时抛出错误,调用方需处理或继续传播。

参数

c $&TlsClient
本次调用使用的 TlsClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
need int
need(int)

返回值

string — 结果字符串

($&TlsClient) close

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

用途

$&TlsClient 上的操作:关闭资源。失败时抛出错误,调用方需处理或继续传播。

参数

c $&TlsClient
本次调用使用的 TlsClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。

返回值

int — 该类型的值

($&TlsClient) set_deadline

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

用途

$&TlsClient 上的操作:设置 deadline。失败时抛出错误,调用方需处理或继续传播。

参数

c $&TlsClient
本次调用使用的 TlsClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
t_ns int
t_ns(int)

返回值

int — 该类型的值

($&TlsClient) set_read_deadline

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

用途

$&TlsClient 上的操作:设置 read deadline。失败时抛出错误,调用方需处理或继续传播。

参数

c $&TlsClient
本次调用使用的 TlsClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
t_ns int
t_ns(int)

返回值

int — 该类型的值

($&TlsClient) set_write_deadline

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

用途

$&TlsClient 上的操作:设置 write deadline。失败时抛出错误,调用方需处理或继续传播。

参数

c $&TlsClient
本次调用使用的 TlsClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。
t_ns int
t_ns(int)

返回值

int — 该类型的值