client
pub fn client(endpoint string) DnsClientPurpose
Execute client.
Parameters
endpointstring- endpoint (string)
Returns
DnsClient — a value of this type
#github.com/xulang-org/packages/dnsExternal 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
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@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) DnsClientExecute client.
endpoint stringDnsClient — a value of this type
poolpub fn pool(endpoint string, max_conns int) DnsPoolExecute pool.
endpoint stringmax_conns intDnsPool — a value of this type
struct DnsClientpub struct DnsClient {
pub host string
pub port int
}Dns Client session maintaining connection and operations.
host stringport intnew_clientpub fn new_client(endpoint string) DnsClientConstruct client.
endpoint stringDnsClient — a value of this type
(DnsClient) connectpub fn (c DnsClient) connect() intEstablish network connection to the target server endpoint. Throws on failure; handle the error or propagate it to the caller.
c DnsClientDnsClient instance used by this call (value receiver).int — a value of this type
(DnsClient) resolve_a_on_fdpub fn (c DnsClient) resolve_a_on_fd(fd int, domain string) []stringQueries A-records over TCP DNS (RFC 1035 section 4.2.2) Throws on failure; handle the error or propagate it to the caller.
c DnsClientDnsClient instance used by this call (value receiver).fd intdomain string[]string — slice result
(DnsClient) resolve_apub fn (c DnsClient) resolve_a(domain string) []stringOperation on DnsClient: resolve a. Throws on failure; handle the error or propagate it to the caller.
c DnsClientDnsClient instance used by this call (value receiver).domain string[]string — slice result
const TYPE_Apub const TYPE_A = 1Constant TYPE_A, defining protocol or algorithm parameters.
const TYPE_NSpub const TYPE_NS = 2Constant TYPE_NS, defining protocol or algorithm parameters.
const TYPE_CNAMEpub const TYPE_CNAME = 5Constant TYPE_CNAME, defining protocol or algorithm parameters.
const TYPE_SOApub const TYPE_SOA = 6Constant TYPE_SOA, defining protocol or algorithm parameters.
const TYPE_TXTpub const TYPE_TXT = 16Constant TYPE_TXT, defining protocol or algorithm parameters.
const CLASS_INpub const CLASS_IN = 1Constant CLASS_IN, defining protocol or algorithm parameters.
encode_domainpub fn encode_domain(domain string) stringEncodes "www.example.com" -> "\x03www\x07example\x03com\x00"
domain stringstring — result string
build_query_packetpub fn build_query_packet(tx_id int, domain string, qtype int) stringBuilds a standard 12-byte DNS Header + Question section
tx_id intdomain stringqtype intstring — result string
struct DnsAnswerpub struct DnsAnswer {
pub name string
pub rtype int
pub rclass int
pub ttl int
pub data string
}Structure encapsulating name, rtype, rclass, ttl, data.
name stringrtype intrclass intttl intdata stringparse_a_recordspub fn parse_a_records(pkt string) []stringParses DNS response and extracts IPv4 addresses (Type A)
pkt string[]string — slice result
struct DnsPoolpub struct DnsPool {
pub client DnsClient
pub idle chan int
pub max_idle int
}Dns Connection pool manager scheduling reusable socket connections.
client DnsClientidle chan intmax_idle intopen_poolpub fn open_pool(endpoint string, max_idle int) DnsPoolOpen pool.
endpoint stringmax_idle intDnsPool — a value of this type
(&DnsPool) get_connpub fn (p &DnsPool) get_conn() intOperation on &DnsPool: read conn.
p &DnsPoolDnsPool instance used by this call (borrow).int — a value of this type
(&DnsPool) put_connpub fn (p &DnsPool) put_conn(fd int)Operation on &DnsPool: put connection.
p &DnsPoolDnsPool instance used by this call (borrow).fd intThis call does not return a value.
(&DnsPool) resolve_apub fn (p &DnsPool) resolve_a(domain string) []stringOperation on &DnsPool: resolve a. Throws on failure; handle the error or propagate it to the caller.
p &DnsPoolDnsPool instance used by this call (borrow).domain string[]string — slice result
(&DnsPool) closepub fn (p &DnsPool) close()Operation on &DnsPool: close the resource.
p &DnsPoolDnsPool instance used by this call (borrow).This call does not return a value.
struct TlsClientpub struct TlsClient {
pub conn transport.Conn
pub host string
pub port int
}Dns Client session maintaining connection and operations.
conn transport.Connhost stringport inttls_connectpub fn tls_connect(endpoint string, default_port int) TlsClientTls connect. Throws on failure; handle the error or propagate it to the caller.
endpoint stringdefault_port intTlsClient — a value of this type
tls_connect_configpub fn tls_connect_config(endpoint string, default_port int, cfg tls.Config) TlsClientTls connect config. Throws on failure; handle the error or propagate it to the caller.
endpoint stringdefault_port intcfg tls.ConfigTlsClient — a value of this type
($&TlsClient) write_allpub fn (c $&TlsClient) write_all(data string) intOperation on $&TlsClient: write all. Throws on failure; handle the error or propagate it to the caller.
c $&TlsClientTlsClient instance used by this call (exclusive borrow).data stringint — a value of this type
($&TlsClient) readpub fn (c $&TlsClient) read(max int) stringOperation on $&TlsClient: read up to max bytes. Throws on failure; handle the error or propagate it to the caller.
c $&TlsClientTlsClient instance used by this call (exclusive borrow).max intstring — result string
($&TlsClient) read_exactpub fn (c $&TlsClient) read_exact(need int) stringOperation on $&TlsClient: read exact. Throws on failure; handle the error or propagate it to the caller.
c $&TlsClientTlsClient instance used by this call (exclusive borrow).need intstring — result string
($&TlsClient) closepub fn (c $&TlsClient) close() intOperation on $&TlsClient: close the resource. Throws on failure; handle the error or propagate it to the caller.
c $&TlsClientTlsClient instance used by this call (exclusive borrow).int — a value of this type
($&TlsClient) set_deadlinepub fn (c $&TlsClient) set_deadline(t_ns int) intOperation on $&TlsClient: set deadline. Throws on failure; handle the error or propagate it to the caller.
c $&TlsClientTlsClient instance used by this call (exclusive borrow).t_ns intint — a value of this type
($&TlsClient) set_read_deadlinepub fn (c $&TlsClient) set_read_deadline(t_ns int) intOperation on $&TlsClient: set read deadline. Throws on failure; handle the error or propagate it to the caller.
c $&TlsClientTlsClient instance used by this call (exclusive borrow).t_ns intint — a value of this type
($&TlsClient) set_write_deadlinepub fn (c $&TlsClient) set_write_deadline(t_ns int) intOperation on $&TlsClient: set write deadline. Throws on failure; handle the error or propagate it to the caller.
c $&TlsClientTlsClient instance used by this call (exclusive borrow).t_ns intint — a value of this type