Debian Codegraph @

cookbook_test.go

raw
1 package dns_test
2
3 // This files has (testable) snippets to document how to do common tasks.
4 // Feel free to add ones you encountered when using this library.
5
6 import (
7 "context"
8 "fmt"
9
10 "codeberg.org/miekg/dns"
11 "codeberg.org/miekg/dns/dnsutil"
12 "codeberg.org/miekg/dns/rdata"
13 )
14
15 // ExampleHeader_replace shows how to overwrite a header.
16 func ExampleHeader_replace() {
17 rr := &dns.MX{}
18 *(rr.Header()) = dns.Header{Name: dnsutil.Fqdn("example.org"), Class: dns.ClassINET}
19 // Or
20 hdr := rr.Header()
21 *hdr = dns.Header{Name: dnsutil.Fqdn("example.org"), Class: dns.ClassINET}
22 // Or
23 rr.Header().Name = "example.org."
24 rr.Header().Class = dns.ClassINET
25 }
26
27 // ExampleRDATA shows how to access the various elements in a [dns.RR].
28 func ExampleRDATA() {
29 rr := &dns.MX{Hdr: dns.Header{Name: "miek.nl.", Class: dns.ClassINET, TTL: 3600}, Preference: 10, Mx: "mx.miek.nl."}
30 rh := rr.Header()
31 rd := rr.Data()
32 fmt.Println("Split RR")
33 fmt.Printf("Record = %v\n", rr)
34 fmt.Printf("Header = %v\n", rh)
35 fmt.Printf("Data = %v\n", rd)
36
37 // Updating rr.Preference does not affect rd, rd is a copy.
38 rr.Preference = 20
39 fmt.Printf("Data = %v\n", rd) // Still 10 mx.miek.nl.
40
41 // Updating rd.Preference does not affect rr.
42 rdmx := rd.(rdata.MX) // Need this otherwise: cannot assign to rd.(rdata.MX).Preference (neither addressable nor a map index expression)
43 rdmx.Preference = 20
44
45 // Update the RR with the new rdata.
46 fn := dns.TypeToRDATA[dns.TypeMX]
47 fn(rr, rdmx)
48 fmt.Printf("Record = %v\n", rr) // .... 20 mx.miek.nl.
49 }
50
51 // ExampleMsg_packUnpack shows how to serialize and deserialize DNS messages.
52 //
53 // In v1, Pack returned ([]byte, error) and Unpack took a []byte argument.
54 // In v2, Pack stores the wire format in msg.Data and Unpack reads from msg.Data.
55 // Always copy msg.Data if you need to hold on to the bytes after the message
56 // is reused or the pool reclaims it.
57 func ExampleMsg_packUnpack() {
58 m := dns.NewMsg("example.com.", dns.TypeA)
59
60 // Pack encodes the message into wire format stored in m.Data.
61 // It does not return the bytes; retrieve them from m.Data after the call.
62 if err := m.Pack(); err != nil {
63 panic(err)
64 }
65
66 // Copy before handing off — m.Data may be reused by the pool.
67 wire := make([]byte, len(m.Data))
68 copy(wire, m.Data)
69
70 // Unpack: place the raw bytes into m.Data, then call Unpack with no args.
71 m2 := new(dns.Msg)
72 m2.Data = wire
73 if err := m2.Unpack(); err != nil {
74 panic(err)
75 }
76
77 fmt.Println(m2.Question[0].Header().Name)
78 // Output:
79 // example.com.
80 }
81
82 // ExampleMsg_edns shows how to set and inspect EDNS0 fields.
83 //
84 // In v1 you built an OPT record by hand and appended it to msg.Extra, then
85 // retrieved it with msg.IsEdns0(). In v2 the DO bit and UDP payload size are
86 // first-class fields on Msg. Pack creates the OPT record automatically;
87 // Unpack populates the fields back from the wire.
88 func ExampleMsg_edns() {
89 m := dnsutil.SetQuestion(new(dns.Msg), "example.com.", dns.TypeDNSKEY)
90
91 // Set EDNS0 directly — no explicit OPT record needed.
92 m.Security = true // DO (DNSSEC OK) bit
93 m.UDPSize = 4096 // advertised UDP payload size
94
95 if err := m.Pack(); err != nil {
96 panic(err)
97 }
98
99 // Simulate receiving the message and inspecting EDNS0 on the far end.
100 r := new(dns.Msg)
101 r.Data = make([]byte, len(m.Data))
102 copy(r.Data, m.Data)
103 if err := r.Unpack(); err != nil {
104 panic(err)
105 }
106
107 fmt.Println("DO bit:", r.Security)
108 fmt.Println("UDP size:", r.UDPSize)
109 // Output:
110 // DO bit: true
111 // UDP size: 4096
112 }
113
114 // ExampleClient_transferIn shows how to perform an AXFR zone transfer.
115 //
116 // In v1 you created a dns.Transfer struct, called .In(), and received records
117 // in env.RR. In v2 use Client.TransferIn; records arrive in env.Answer.
118 // Use a context to cancel early (e.g. once you have seen enough records).
119 func ExampleClient_transferIn() {
120 ctx, cancel := context.WithCancel(context.Background())
121 defer cancel()
122
123 msg := dnsutil.SetQuestion(new(dns.Msg), "example.com.", dns.TypeAXFR)
124 msg.RecursionDesired = false
125
126 client := dns.NewClient()
127
128 ch, err := client.TransferIn(ctx, msg, "tcp", "192.0.2.1:53")
129 if err != nil {
130 // Connection or send error before the transfer started.
131 return
132 }
133
134 for env := range ch {
135 if env.Error != nil {
136 fmt.Println("transfer error:", env.Error)
137 return
138 }
139 for _, rr := range env.Answer {
140 fmt.Println(rr)
141 }
142 }
143 }