| 1 |
# Converting from v1 to v2 |
| 2 |
|
| 3 |
## What changed? |
| 4 |
|
| 5 |
A lot. See below. |
| 6 |
|
| 7 |
## General tips |
| 8 |
|
| 9 |
None of these are required but experience has proven them to be good ideas. |
| 10 |
|
| 11 |
1. Convert to netip.Addr first. |
| 12 |
Before you convert to dns v2, convert from `net.IP` to `netip.Addr`. DNS v2 |
| 13 |
uses `netip.Addr` to represent IP addresses. Converting two modules at the same |
| 14 |
time is much more difficult than converting them in sequence. |
| 15 |
|
| 16 |
2. Make v1 and v2 coexist, then work to eliminate v1 |
| 17 |
If you make v1 and v2 coexist your conversion will be easier. You can work incrementally |
| 18 |
instead of one big "big bang" change. You can identify code that needs to be upgraded |
| 19 |
by searching for `dnsv1.` |
| 20 |
|
| 21 |
Code that uses both will have imports that look like: |
| 22 |
|
| 23 |
```go |
| 24 |
import ( |
| 25 |
dnsv1 "github.com/miekg/dns" |
| 26 |
"codeberg.org/miekg/dns" |
| 27 |
) |
| 28 |
``` |
| 29 |
|
| 30 |
Here's how to do it: |
| 31 |
|
| 32 |
Rename v1 to dnsv1 |
| 33 |
|
| 34 |
Change all imports from |
| 35 |
|
| 36 |
"github.com/miekg/dns" |
| 37 |
|
| 38 |
to |
| 39 |
|
| 40 |
dnsv1 "github.com/miekg/dns" |
| 41 |
|
| 42 |
Now v1 and v2 can co-exist. `dnsv1.RR` is the old code and `dns.RR` is the new code. |
| 43 |
|
| 44 |
You might consider shipping a release that has this one change. This should be a "no op" change |
| 45 |
and therefore no tests should fail, no features should break. Now you have a baseline |
| 46 |
before other changes are made. |
| 47 |
|
| 48 |
ProTip: VSCode makes this easy. Find code that mentions `dns.` and use "Rename Symbol" (F2) to change it |
| 49 |
to `dnsv1.`. VSCode does all the work of finding all instances. It even updates the imports line. You'll |
| 50 |
need to do this once for each file. VSCode is smart enough to know that imports are per-file, even though |
| 51 |
Rename Symbol can work across multiple files. |
| 52 |
|
| 53 |
Find the files that need this change: |
| 54 |
|
| 55 |
grep -l -R -r --include='*.go' github.com/miekg/dns |
| 56 |
|
| 57 |
3. Do this for _dnsutil_ and other packages |
| 58 |
|
| 59 |
Do something similar for _dnsutil_ (_dnsutilv1_) also. Again, this isn't required, just useful. |
| 60 |
|
| 61 |
4. Work incrementally |
| 62 |
|
| 63 |
It's best to convert a little, test, convert a little more, test, etc. Trying to convert |
| 64 |
everything makes testing difficult. |
| 65 |
|
| 66 |
Doing this requires you to be able to convert between v1's `RR` and v2's `RR`. |
| 67 |
|
| 68 |
Here are some conversion functions. The functions are slow and ugly, but accuracy is more important. (The |
| 69 |
functions being slow is OK because they are temporary. The slowness should give you incentive to finish |
| 70 |
porting to v2! |
| 71 |
|
| 72 |
```go |
| 73 |
package dnsrr |
| 74 |
|
| 75 |
import ( |
| 76 |
dnsv2 "codeberg.org/miekg/dns" |
| 77 |
dnsv1 "github.com/miekg/dns" |
| 78 |
) |
| 79 |
|
| 80 |
// RRv1tov2 converts github.com/miekg/dns (v1) RR to codeberg.org/miekg/dns (v2) RR. |
| 81 |
// Typically used in providers that still use v1. |
| 82 |
// Note: this function is not efficient. It converts via string representation. |
| 83 |
// Use it until you are able to convert to v2 fully. |
| 84 |
// Note: Panics on error. |
| 85 |
func RRv1tov2(rrv1 dnsv1.RR) dnsv2.RR { |
| 86 |
rrv2, err := dnsv2.New(rrv1.String()) |
| 87 |
if err != nil { |
| 88 |
panic("Failed to convert RR to v2: " + err.Error()) |
| 89 |
} |
| 90 |
return rrv2 |
| 91 |
} |
| 92 |
|
| 93 |
// RRv2tov1 converts codeberg.org/miekg/dns (v2) RR to github.com/miekg/dns (v1) RR. |
| 94 |
// Typically used in providers that still use v1. |
| 95 |
// Note: this function is not efficient. It converts via string representation. |
| 96 |
// Use it until you are able to convert to v1 fully. |
| 97 |
// Note: Panics on error. |
| 98 |
func RRv2tov1(rrv2 dnsv2.RR) dnsv1.RR { |
| 99 |
rrv1, err := dnsv1.NewRR(rrv2.String()) |
| 100 |
if err != nil { |
| 101 |
panic("Failed to convert RR to v1: " + err.Error()) |
| 102 |
} |
| 103 |
return rrv1 |
| 104 |
} |
| 105 |
``` |
| 106 |
|
| 107 |
# Difference with github.com/miekg/dns |
| 108 |
|
| 109 |
I have ported a few utilities from dnsv1 to dnsv2, and dnsv2 is mostly a drop-in replacement. Of course YMMV. |
| 110 |
|
| 111 |
- Many functions (and new ones) are moved into _dnsutil_, and _dnstest_. This copied a lot of stuff from CoreDNS. |
| 112 |
- _dnshttp_ was added for help with DOH - DNS over HTTPs. |
| 113 |
- `RR` lost the `Type` and `Rdlength` fields, type is derived from the Go type, `Rdlength` served no function at all. |
| 114 |
The `Header` is thus 4 bytes smaller than in v1. The RFC3597 (unknown RRs) has gained a `Type` field because of this. |
| 115 |
- The rdata of each `RR` is split out in to a _rdata_ package. This makes it much more memory efficient to |
| 116 |
store RRSets - as the RR's header isn't duplicated. This saves a minimal of 24 bytes (empty string, ttl, and |
| 117 |
class) per RR stored. |
| 118 |
- `context.Context` is used in the correct places. `ServeDNS` now has a context.Context, with `Zone(ctx)` you |
| 119 |
retrieve the pattern zone that lead to invocation of this Handler. |
| 120 |
- _internal/..._ packages that hold code that used to be private, but was cluttering the top level directory; also allowed for better |
| 121 |
naming. |
| 122 |
- builtin perf testing with _internal/dnsperf_. Need `dnsperf`, on deb-based systems `apt-get install dnsperf`. |
| 123 |
- Interfaces do not have private methods. |
| 124 |
- No more `dns.Conn`. |
| 125 |
- `Msg` contains a buffer named `Data` that holds the binary data for this message. This pulls TSIG/SIG(0) |
| 126 |
handling out of the client and server, simplifying it enormously as we can get rid of `dns.Conn`, and just |
| 127 |
use io.Writer and io.Reader interfaces. |
| 128 |
- `Msg` includes `Options` that control on how you want it packed/unpacked. |
| 129 |
- `Msg` includes all the ENDS0 OPT RR bits, as this almost was a real message header; in this package it now is. |
| 130 |
- `Msg` has a pseudo section that holds all EDNS0 Options as (faked) resource records. |
| 131 |
- Everything is a resource record: |
| 132 |
- question section: holds `[]RR` |
| 133 |
- pseudo section: holds `[]RR` |
| 134 |
- stateful section: holds `[]RR` |
| 135 |
|
| 136 |
Pseudo section RR (EDNS0 OPT) can also be parsed from their (also unique to this library) presentation format. |
| 137 |
|
| 138 |
The `Stateful` section in the message that holds DNS Stateful Operation (DSO) records, these records are |
| 139 |
also `RR`s. (The Stateful section was unused - this has been removed from Msg for the time being). |
| 140 |
|
| 141 |
- `New` will return an `RR`, `NewRR` is gone, `dnstest/New` will do the same, but panic on errors. |
| 142 |
- `Client` has a `dns.Transport` just like `http.Client`, so _all_ connection management is now external. |
| 143 |
- More: |
| 144 |
- `Msg` is a io.Writer. |
| 145 |
- `msg.Data` can be re-used between request and reply in Exchange. |
| 146 |
- `msg.Data` can be returned to a server buffer pool, for reuse in new messages, this is done automatically, |
| 147 |
see `msg.Hijack()` for hijacking the buffer. |
| 148 |
- private RRs are easier. |
| 149 |
- private EDNS0 are implementable and hopefully easier. |
| 150 |
- SVCB record got its own package _svcb_ where all the key-values (called `svcb.Pair`) now reside. |
| 151 |
- DELEG record also got its own package _deleg_, where its key-values (called `deleg.Info`) live. |
| 152 |
- IsDuplicate is gone in favor of Compare and a full support for the `sort.Interface`, so you can just |
| 153 |
sort RRs in an RRset. This also simplified the DNSSEC signing and make wireformat even less important. |
| 154 |
- Copied, sanitized and removed tests that accumulated over 16 years of development. |
| 155 |
- Escapes in domain names is not supported. This added 50-100% overhead in low-level functions that are often |
| 156 |
used in the hot path. In rdata (TXT records) it still is. |
| 157 |
- The less used ClientConfig now lives in _dnsconf_. |
| 158 |
|
| 159 |
# Converting idioms |
| 160 |
|
| 161 |
Here are some v1 vs v2 changes. |
| 162 |
|
| 163 |
Please add to this list! The conversions you find will be useful to others. Please submit a PR to this file! |
| 164 |
|
| 165 |
## RRs |
| 166 |
|
| 167 |
Create an RR. |
| 168 |
|
| 169 |
``` |
| 170 |
OLD | NEW |
| 171 |
| |
| 172 |
r := &MX{ Header{Name:"miek.nl.", Class: dns.ClassINET, TTL: 3600}, | r := &MX{ |
| 173 |
Preference: 10, Mx: "mx.miek.nl."} | Header{Name:"miek.nl.", Class: dns.ClassINET, TTL: 3600}, |
| 174 |
| MX: rdata.MX{Preference: 10, Mx: "mx.miek.nl."}, |
| 175 |
| } |
| 176 |
``` |
| 177 |
|
| 178 |
Print RR without header. |
| 179 |
|
| 180 |
``` |
| 181 |
OLD | NEW |
| 182 |
| |
| 183 |
mx, _ := dns.NewRR("miek.nl. 3600 IN MX 10 mx.miek.nl.") | mx := dnstest.New("miek.nl. 3600 IN MX 10 mx.miek.nl.") |
| 184 |
hdr := mx.Header().String() | hdr := mx.Header().String() |
| 185 |
flds := mx.String()[len(hdr)+1:] | fmt.Printf("Fields: %q\n", mx.MX.String()) |
| 186 |
fmt.Printf("Fields: %q\n", flds) | |
| 187 |
``` |
| 188 |
|
| 189 |
Access RR's rdata. |
| 190 |
|
| 191 |
``` |
| 192 |
OLD | NEW |
| 193 |
| |
| 194 |
mx, _ := dns.NewRR("miek.nl. 3600 IN MX 10 mx.miek.nl.") | mx := dnstest.New("miek.nl. 3600 IN MX 10 mx.miek.nl.") |
| 195 |
num := dns.NumField(mx) | rdata := mx.MX |
| 196 |
for i := range num { | rdata.Preference = 10 |
| 197 |
fmt.Printf("%q", dns.Field(i)) | |
| 198 |
} | |
| 199 |
``` |
| 200 |
|
| 201 |
What type is this RR and as string. |
| 202 |
|
| 203 |
``` |
| 204 |
OLD | NEW |
| 205 |
| |
| 206 |
hdr := rr.Header() | rrtype := dns.RRToType(rr) |
| 207 |
rrtype := hdr.Rrtype | str := dns.TypeToString[rrtype] |
| 208 |
str := dns.TypeToString[rrtype] | // or |
| 209 |
| str = dnsutil.TypeToString(rrtype) // gives TYPEXXX for unknown types |
| 210 |
``` |
| 211 |
|
| 212 |
Find the TTL: |
| 213 |
|
| 214 |
``` |
| 215 |
OLD | NEW |
| 216 |
| |
| 217 |
rr.Header().Ttl | rr.Header().TTL |
| 218 |
``` |
| 219 |
|
| 220 |
## Setting EDNS0 |
| 221 |
|
| 222 |
``` |
| 223 |
OLD | NEW |
| 224 |
| |
| 225 |
m := new(dns.Msg) | m := dns.NewMsg("miek.nl.", dns.TypeDNSKEY) |
| 226 |
m.SetQuestion("miek.nl.", dns.TypeDNSKEY) | m.UDPSize, m.Security = 4096, true |
| 227 |
m.SetEdns0(4096, true) | |
| 228 |
| // or |
| 229 |
| |
| 230 |
| m := new(dns.Msg) |
| 231 |
| dnsutil.SetQuestion(m, "miek.nl.", dns.TypeDNSKEY") |
| 232 |
| m.UDPSize, m.Security = 4096, true |
| 233 |
``` |
| 234 |
|
| 235 |
Setting the UDP buffer size. |
| 236 |
|
| 237 |
``` |
| 238 |
OLD | NEW |
| 239 |
| |
| 240 |
bufsize := 0 | bufsize := m.UDPSize |
| 241 |
for i := len(m.Extra) - 1; i >= 0; i-- { | |
| 242 |
if m.Extra[i].Header().Rrtype == dns.TypeOPT { | |
| 243 |
bufsize = m.Extra[i].(*dns.OPT).UDPSize() | |
| 244 |
} | |
| 245 |
} | |
| 246 |
``` |
| 247 |
|
| 248 |
Accessing ENDS0 options. |
| 249 |
|
| 250 |
``` |
| 251 |
OLD | NEW |
| 252 |
| |
| 253 |
opt := 0 | for i, options := range m.Pseudo { |
| 254 |
for i := len(m.Extra) - 1; i >= 0; i-- { | // ... |
| 255 |
if m.Extra[i].Header().Rrtype == dns.TypeOPT { | } |
| 256 |
opt = m.Extra[i].(*dns.OPT)| | |
| 257 |
} | |
| 258 |
} | |
| 259 |
for i, options := range opt.Options { | |
| 260 |
// ... | |
| 261 |
} | |
| 262 |
``` |
| 263 |
|
| 264 |
Checking if there _is_ an EDNS0 option added. |
| 265 |
|
| 266 |
``` |
| 267 |
OLD | NEW |
| 268 |
| |
| 269 |
x := m.IsEdns0() | x := len(m.Pseudo) > 0 |
| 270 |
| // The OPT RR itself is incorperated into Msg. |
| 271 |
``` |
| 272 |
|
| 273 |
Adding an EDNS0 option is just as easy, assign to the pseudo section. |
| 274 |
|
| 275 |
``` |
| 276 |
OLD | NEW |
| 277 |
| |
| 278 |
o := &dns.OPT{Hdr: dns.RR_Header{Name: ".", Rrtype: dns.TypeOPT}} | |
| 279 |
o.SetDo() | m.Security = true |
| 280 |
o.SetUDPSize(dns.DefaultMsgSize) | m.UDPSize = dns.DefaultMsgSize |
| 281 |
e := &dns.EDNS0_NSID{Code: dns.EDNS0NSID} | m.Pseudo = append(m.Pseudo, &dns.NSID{}) |
| 282 |
o.Option = append(o.Option, e) | |
| 283 |
m.Extra = append(m.Extra, o) | |
| 284 |
``` |
| 285 |
|
| 286 |
## Msg |
| 287 |
|
| 288 |
Ranging over an entire `Msg`: |
| 289 |
|
| 290 |
``` |
| 291 |
OLD | NEW |
| 292 |
| |
| 293 |
// N/A | for rr := range m.RRs() { ... } |
| 294 |
``` |
| 295 |
|
| 296 |
Set the EDNS0 UDP buffer size: |
| 297 |
|
| 298 |
``` |
| 299 |
OLD | NEW |
| 300 |
| |
| 301 |
m := new(dns.Msg) | m := dns.NewMsg("miek.nl.", dns.TypeDNSKEY) |
| 302 |
m.SetQuestion("miek.nl.", dns.TypeDNSKEY) | m.UDPSize, m.Security = 4096, true |
| 303 |
o.SetEdns0(4096, true) | |
| 304 |
``` |
| 305 |
|
| 306 |
## Text Output |
| 307 |
|
| 308 |
Note the `do` flag now being shown as if it was set in the message header, OPT options are displayed as RRs |
| 309 |
and can also be created with `dns.New`. |
| 310 |
|
| 311 |
``` |
| 312 |
OLD | NEW |
| 313 |
| |
| 314 |
;; opcode: QUERY, status: NOERROR, id: 62167 | ;; QUERY, rcode: NOERROR, id: 3, flags: rd do |
| 315 |
;; flags: qr rd ra; QUERY: 1, ANSWER: 5, AUTHORITY: 0, ADDITIONAL: 0 | ;; EDNS, version: 0, udp: 1024 |
| 316 |
| ;; QUESTION: 1, PSEUDO: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 0, DATA SIZE: 25 |
| 317 |
;; OPT PSEUDOSECTION: | |
| 318 |
; EDNS: version 0; flags:; udp: 512 | ;; PSEUDO SECTION: |
| 319 |
; NSID: 6770646e732d616d73 (g)(p)(d)(n)(s)(-)(a)(m)(s) | . CLASS0 NSID 6770; "gpdns-ams" |
| 320 |
| |
| 321 |
;; QUESTION SECTION: | |
| 322 |
;miek.nl. IN MX | ;; QUESTION SECTION: |
| 323 |
| miek.nl. IN A |
| 324 |
``` |
| 325 |
|
| 326 |
## Functions and Methods |
| 327 |
|
| 328 |
``` |
| 329 |
OLD | NEW |
| 330 |
| |
| 331 |
r := m.Copy() | r := m.Copy() // Shallow copy! |
| 332 |
``` |
| 333 |
|
| 334 |
Fqdn has moved. |
| 335 |
|
| 336 |
``` |
| 337 |
OLD | NEW |
| 338 |
| |
| 339 |
s := dns.Fqdn(s) | s := dnsutil.Fqdn(s) |
| 340 |
``` |
| 341 |
|
| 342 |
SetQuestion has moved. |
| 343 |
|
| 344 |
``` |
| 345 |
OLD | NEW |
| 346 |
| |
| 347 |
m.SetQuestion("miek.nl.", dns.TypeDNSKEY) | dnsutil.SetQuestion(m, "miek.nl.", dns.TypeDNSKEY) |
| 348 |
``` |
| 349 |
|
| 350 |
CanonicalName has moved and been renamed. |
| 351 |
|
| 352 |
``` |
| 353 |
OLD | NEW |
| 354 |
| |
| 355 |
canon := dns.CanonicalName(name) | canon := dnsutil.Canonical(name) |
| 356 |
``` |
| 357 |
|
| 358 |
Validating hostnames and counting labels |
| 359 |
``` |
| 360 |
OLD | NEW |
| 361 |
| |
| 362 |
labels, ok := dns.IsDomainName(hostname) | ok := dnsutil.IsName(hostname) |
| 363 |
| labels := dnsutil.Labels(hostname) |
| 364 |
``` |
| 365 |
|
| 366 |
Is a host a subdomain? |
| 367 |
|
| 368 |
``` |
| 369 |
OLD | NEW |
| 370 |
| |
| 371 |
dns.IsSubDomain(domain, hostname) | dnsutil.IsBelow(domain, hostname) |
| 372 |
``` |
| 373 |
|
| 374 |
|
| 375 |
## Server |
| 376 |
|
| 377 |
Because `Msg` now carries its binary data too there is no need to do TSIG in the server it self, it can now be |
| 378 |
done in a handler. This, again, removes a little of internal code that slowed things down. |
| 379 |
|
| 380 |
The default implementation of `dns.ResponseWriter` is thread safe and this for TCP pipe lining, which is thusly |
| 381 |
implemented in `dns.Server`. Writing or reading data is now done with `io.Copy` no more `ReadMsg` or `WriteMsg`. |
| 382 |
|
| 383 |
A handler for instance. |
| 384 |
|
| 385 |
``` |
| 386 |
OLD | NEW |
| 387 |
| |
| 388 |
func HelloServer(w dns.ResponseWriter, req *dns.Msg) { | func HelloServer(ctx contect.Context, w ResponseWriter, req *Msg) { |
| 389 |
m := new(dns.Msg) | m := req.Copy() |
| 390 |
m.SetReply(req) | dnsutil.SetReply(m, req) |
| 391 |
| |
| 392 |
m.Extra = make([]dns.RR, 1) | m.Extra = []dns.RR{ |
| 393 |
m.Extra[0] = &TXT{ | &TXT{Hdr: dns.Header{Name: m.Question[0].Name, Class: dns.ClassINET}, |
| 394 |
Hdr: dns.RR_Header{Name: m.Question[0].Name, | Txt: []string{"Hello world"}} |
| 395 |
Rrtype: dns.TypeTXT, Class: dns.ClassINET}, | } |
| 396 |
Txt: []string{"Hello world"} | |
| 397 |
} | m.Pack() |
| 398 |
w.WriteMsg(m) | io.Copy(w, m) |
| 399 |
} | } |
| 400 |
``` |
| 401 |
|
| 402 |
## Zone Parser |
| 403 |
|
| 404 |
Allow includes. |
| 405 |
|
| 406 |
``` |
| 407 |
OLD | NEW |
| 408 |
| |
| 409 |
zp := dns.NewZoneParser(...) | zp := dns.NewZoneParser(...) |
| 410 |
zp.SetIncludeAllowed(true) | // now the default |
| 411 |
``` |
| 412 |
|
| 413 |
Disallow includes. |
| 414 |
|
| 415 |
``` |
| 416 |
OLD | NEW |
| 417 |
| |
| 418 |
zp := dns.NewZoneParser(...) | zp := dns.NewZoneParser(...) |
| 419 |
zp.SetIncludeAllowed(false) | zp.IncludeAllowFunc = func() bool { return false } |
| 420 |
``` |