-
Notifications
You must be signed in to change notification settings - Fork 1
/
example_tcp_test.go
68 lines (60 loc) · 1.41 KB
/
example_tcp_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// SPDX-License-Identifier: GPL-3.0-or-later
package dnscore_test
import (
"context"
"fmt"
"log"
"slices"
"strings"
"time"
"github.com/miekg/dns"
"github.com/rbmk-project/common/runtimex"
"github.com/rbmk-project/dnscore"
)
func ExampleTransport_dnsOverTCP() {
// create transport, server addr, and query
txp := &dnscore.Transport{}
serverAddr := &dnscore.ServerAddr{
Protocol: dnscore.ProtocolTCP,
Address: "8.8.8.8:53",
}
options := []dnscore.QueryOption{
dnscore.QueryOptionEDNS0(
dnscore.EDNS0SuggestedMaxResponseSizeOtherwise,
0,
),
}
query, err := dnscore.NewQuery("dns.google", dns.TypeA, options...)
if err != nil {
log.Fatal(err)
}
// issue the query and get the response
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
resp, err := txp.Query(ctx, serverAddr, query)
if err != nil {
log.Fatal(err)
}
// validate the response
if err := dnscore.ValidateResponse(query, resp); err != nil {
log.Fatal(err)
}
runtimex.Assert(len(query.Question) > 0, "expected at least one question")
rrs, err := dnscore.ValidAnswers(query.Question[0], resp)
if err != nil {
log.Fatal(err)
}
// print the results
var addrs []string
for _, rr := range rrs {
switch rr := rr.(type) {
case *dns.A:
addrs = append(addrs, rr.A.String())
}
}
slices.Sort(addrs)
fmt.Printf("%s\n", strings.Join(addrs, "\n"))
// Output:
// 8.8.4.4
// 8.8.8.8
}