client
pub fn client(endpoint string) DnsClient用途
客户端。
参数
endpointstring- endpoint(string)
返回值
DnsClient — 该类型的值
#github.com/xulang-org/packages/dns扩展包 `dns #github.com/xulang-org/packages/dns`,由模块 github.com/xulang-org/packages 提供。
github.com/xulang-org/packages · 1.17.2 · 安装与使用
以下源码来自官方 examples 仓库,可复制或下载。请在完整工程中使用本地 Xu SDK 运行;网络、数据库和桌面示例需要对应的服务或资源。
49_standalone_dns/main.xu@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]")
}clientpub fn client(endpoint string) DnsClient客户端。
endpoint stringDnsClient — 该类型的值
poolpub fn pool(endpoint string, max_conns int) DnsPool执行 pool 处理。
endpoint stringmax_conns intDnsPool — 该类型的值
struct DnsClientpub struct DnsClient {
pub host string
pub port int
}Dns 客户端会话实例,维护与服务端的连接并提供操作接口。
host stringport intnew_clientpub fn new_client(endpoint string) DnsClient构造 client。
endpoint stringDnsClient — 该类型的值
(DnsClient) connectpub fn (c DnsClient) connect() int建立与目标服务端点的网络通信连接。失败时抛出错误,调用方需处理或继续传播。
c DnsClientDnsClient 实例;按值接收。int — 该类型的值
(DnsClient) resolve_a_on_fdpub fn (c DnsClient) resolve_a_on_fd(fd int, domain string) []stringDnsClient 上的操作:执行 resolve a on fd 操作。失败时抛出错误,调用方需处理或继续传播。
c DnsClientDnsClient 实例;按值接收。fd intdomain string[]string — 切片结果
(DnsClient) resolve_apub fn (c DnsClient) resolve_a(domain string) []stringDnsClient 上的操作:执行 resolve a 操作。失败时抛出错误,调用方需处理或继续传播。
c DnsClientDnsClient 实例;按值接收。domain string[]string — 切片结果
const TYPE_Apub const TYPE_A = 1常量 TYPE_A,定义了相关的协议或算法配置参数。
const TYPE_NSpub const TYPE_NS = 2常量 TYPE_NS,定义了相关的协议或算法配置参数。
const TYPE_CNAMEpub const TYPE_CNAME = 5常量 TYPE_CNAME,定义了相关的协议或算法配置参数。
const TYPE_SOApub const TYPE_SOA = 6常量 TYPE_SOA,定义了相关的协议或算法配置参数。
const TYPE_TXTpub const TYPE_TXT = 16常量 TYPE_TXT,定义了相关的协议或算法配置参数。
const CLASS_INpub const CLASS_IN = 1常量 CLASS_IN,定义了相关的协议或算法配置参数。
encode_domainpub fn encode_domain(domain string) string编码 domain 数据。
domain stringstring — 结果字符串
build_query_packetpub fn build_query_packet(tx_id int, domain string, qtype int) string执行 build query packet 操作。
tx_id intdomain stringqtype intstring — 结果字符串
struct DnsAnswerpub struct DnsAnswer {
pub name string
pub rtype int
pub rclass int
pub ttl int
pub data string
}承载 name、rtype、rclass、ttl、data 等属性的结构体。
name stringrtype intrclass intttl intdata stringparse_a_recordspub fn parse_a_records(pkt string) []string解析为 a records。
pkt string[]string — 切片结果
struct DnsPoolpub struct DnsPool {
pub client DnsClient
pub idle chan int
pub max_idle int
}Dns 连接池管理器,并发安全地维护和调度复用套接字连接。
client DnsClientidle chan intmax_idle intopen_poolpub fn open_pool(endpoint string, max_idle int) DnsPool执行 open pool 操作。
endpoint stringmax_idle intDnsPool — 该类型的值
(&DnsPool) get_connpub fn (p &DnsPool) get_conn() int&DnsPool 上的操作:读取 conn。
p &DnsPoolDnsPool 实例;以只读借用传入,调用方仍持有实例。int — 该类型的值
(&DnsPool) put_connpub fn (p &DnsPool) put_conn(fd int)&DnsPool 上的操作:执行 put conn 操作。
p &DnsPoolDnsPool 实例;以只读借用传入,调用方仍持有实例。fd int不返回值。
(&DnsPool) resolve_apub fn (p &DnsPool) resolve_a(domain string) []string&DnsPool 上的操作:执行 resolve a 操作。失败时抛出错误,调用方需处理或继续传播。
p &DnsPoolDnsPool 实例;以只读借用传入,调用方仍持有实例。domain string[]string — 切片结果
(&DnsPool) closepub fn (p &DnsPool) close()&DnsPool 上的操作:关闭资源。
p &DnsPoolDnsPool 实例;以只读借用传入,调用方仍持有实例。不返回值。
struct TlsClientpub struct TlsClient {
pub conn transport.Conn
pub host string
pub port int
}Dns 客户端会话实例,维护与服务端的连接并提供操作接口。
conn transport.Connhost stringport inttls_connectpub fn tls_connect(endpoint string, default_port int) TlsClient执行 tls connect 操作。失败时抛出错误,调用方需处理或继续传播。
endpoint stringdefault_port intTlsClient — 该类型的值
tls_connect_configpub fn tls_connect_config(endpoint string, default_port int, cfg tls.Config) TlsClient执行 tls connect config 操作。失败时抛出错误,调用方需处理或继续传播。
endpoint stringdefault_port intcfg tls.ConfigTlsClient — 该类型的值
($&TlsClient) write_allpub fn (c $&TlsClient) write_all(data string) int$&TlsClient 上的操作:执行 write all 操作。失败时抛出错误,调用方需处理或继续传播。
c $&TlsClientTlsClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。data stringint — 该类型的值
($&TlsClient) readpub fn (c $&TlsClient) read(max int) string$&TlsClient 上的操作:读出最多 max 字节。失败时抛出错误,调用方需处理或继续传播。
c $&TlsClientTlsClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。max intstring — 结果字符串
($&TlsClient) read_exactpub fn (c $&TlsClient) read_exact(need int) string$&TlsClient 上的操作:执行 read exact 操作。失败时抛出错误,调用方需处理或继续传播。
c $&TlsClientTlsClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。need intstring — 结果字符串
($&TlsClient) closepub fn (c $&TlsClient) close() int$&TlsClient 上的操作:关闭资源。失败时抛出错误,调用方需处理或继续传播。
c $&TlsClientTlsClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。int — 该类型的值
($&TlsClient) set_deadlinepub fn (c $&TlsClient) set_deadline(t_ns int) int$&TlsClient 上的操作:设置 deadline。失败时抛出错误,调用方需处理或继续传播。
c $&TlsClientTlsClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。t_ns intint — 该类型的值
($&TlsClient) set_read_deadlinepub fn (c $&TlsClient) set_read_deadline(t_ns int) int$&TlsClient 上的操作:设置 read deadline。失败时抛出错误,调用方需处理或继续传播。
c $&TlsClientTlsClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。t_ns intint — 该类型的值
($&TlsClient) set_write_deadlinepub fn (c $&TlsClient) set_write_deadline(t_ns int) int$&TlsClient 上的操作:设置 write deadline。失败时抛出错误,调用方需处理或继续传播。
c $&TlsClientTlsClient 实例;以独占借用传入,方法可修改它,调用方仍持有实例。t_ns intint — 该类型的值