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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
|
package main
import (
"context"
"flag"
"fmt"
"github.com/cloudflare/dns"
dnstap "github.com/dnstap/golang-dnstap"
"google.golang.org/protobuf/proto"
"io"
"io/ioutil"
"log"
"net"
"os"
"os/exec"
"strings"
"time"
)
var (
kresdArgs = []string{
"-n",
"-q",
}
)
func qnameFromFrame(b []byte) (string, error) {
dt := &dnstap.Dnstap{}
var name string
if err := proto.Unmarshal(b, dt); err != nil {
return name, err
}
var msg_raw []byte
m := dt.Message
if *m.Type == dnstap.Message_CLIENT_QUERY {
msg_raw = m.QueryMessage
} else if *m.Type == dnstap.Message_CLIENT_RESPONSE {
msg_raw = m.ResponseMessage
} else {
return name, fmt.Errorf("incorrect message type: %v", *m.Type)
}
if msg_raw == nil {
return name, fmt.Errorf("no message payload")
}
if err := dns.IsMsg(msg_raw); err != nil {
return name, err
}
var msg dns.Msg
if err := msg.Unpack(msg_raw); err != nil {
return name, err
}
if len(msg.Question) < 1 {
return name, fmt.Errorf("question empty")
}
return msg.Question[0].Name, nil
}
func listenOn() (net.Addr, *os.File, error) {
udpConn, err := net.ListenUDP("udp", &net.UDPAddr{
IP: net.ParseIP("127.0.0.1"),
Port: 0,
})
if err != nil {
return nil, nil, err
}
file, err := udpConn.File()
if err != nil {
return nil, nil, err
}
return udpConn.LocalAddr(), file, nil
}
func runKresd(ctx context.Context, path, configFile string, grace time.Duration) (chan bool, error) {
ch := make(chan bool)
kresdArgs = append(kresdArgs, "-c"+configFile)
// we have 1 object in ExtraFiles with index 0
// child fd will be 3 + i = 3
kresdArgs = append(kresdArgs, "-S3")
file := ctx.Value("file").(*os.File)
debug := ctx.Value("debug").(bool)
cmd := exec.CommandContext(ctx, path, kresdArgs...)
cmd.ExtraFiles = []*os.File{file}
var stdout, stderr io.ReadCloser
var err error
if debug {
stdout, err = cmd.StdoutPipe()
if err != nil {
log.Printf("stdoutpipe: %v\n", err)
return ch, err
}
stderr, err = cmd.StderrPipe()
if err != nil {
log.Printf("stderrpipe: %v\n", err)
return ch, err
}
}
go func() {
status := false
defer func() {
ch <- status // kresd done
}()
if err := cmd.Start(); err != nil {
log.Printf("start: %v\n", err)
return
}
time.Sleep(grace)
ch <- true // Started kresd
if debug {
s, err := ioutil.ReadAll(stdout)
if err != nil {
log.Printf("readall: %v\n", err)
return
}
if len(s) > 0 {
fmt.Printf("stdout:\n%s\n", s)
}
s, err = ioutil.ReadAll(stderr)
if err != nil {
log.Printf("readall: %v\n", err)
return
}
if len(s) > 0 {
fmt.Printf("stderr:\n%s\n", s)
}
}
if err := cmd.Wait(); err != nil && err.Error() != "signal: killed" {
log.Printf("wait: %v\n", err)
return
}
status = true
}()
return ch, nil
}
func main() {
var (
unixSocket = flag.String("u", "dnstap.sock", "dnstap socket")
kresdPath = flag.String("cmd", "kresd", "kresd path")
configFile = flag.String("c", "config", "config file")
qnames = flag.String("q", ".", "list of comma separated zones")
grace = flag.String("g", "1s", "Time to wait for daemon start")
timeout = flag.String("t", "60s", "Test Timeout")
debug = flag.Bool("d", false, "Debug")
)
flag.Parse()
kresdStartGracePeriod, err := time.ParseDuration(*grace)
if err != nil {
panic(err)
}
testTimeout, err := time.ParseDuration(*timeout)
if err != nil {
panic(err)
}
input, err := dnstap.NewFrameStreamSockInputFromPath(*unixSocket)
if err != nil {
panic(err)
}
output := make(chan []byte)
go input.ReadInto(output)
ctx, cancel := context.WithTimeout(context.Background(), testTimeout)
// Create a UDP listening socket on random port
// FD will be passed on to kresd
addr, file, err := listenOn()
if err != nil {
panic(err)
}
if *debug {
log.Printf("listen addr:%v", addr)
}
ctx = context.WithValue(ctx, "file", file)
ctx = context.WithValue(ctx, "debug", *debug)
ch, err := runKresd(ctx, *kresdPath, *configFile, kresdStartGracePeriod)
if err != nil {
panic(err)
}
log.Printf("Waiting for kresd to start\n")
status := <-ch
if !status {
os.Exit(1) // error starting
}
go func() {
parts := strings.Split(*qnames, ",")
if len(parts) == 0 {
log.Printf("qname count is 0")
}
for _, name := range parts {
m := new(dns.Msg)
fqdn := dns.Fqdn(name)
m.SetQuestion(fqdn, dns.TypeA)
c := new(dns.Client)
resp, _, err := c.Exchange(m, fmt.Sprintf("%v", addr))
if err != nil {
log.Printf("%v\n", err)
os.Exit(1) // Test Failed
}
if *debug {
log.Printf("Response: %v", resp)
}
for range "QR" { // Checking Query and Response is the same ATM
o := <-output
if *debug {
log.Printf("raw dnstap:%v", o)
}
dtName, err := qnameFromFrame(o)
if err != nil {
log.Printf("%v\n", err)
os.Exit(1)
}
if fqdn != dtName {
log.Printf("expected %v got %v", fqdn, dtName)
os.Exit(1) // Test failed
}
log.Printf("matched qname: %v", dtName)
}
}
cancel() // Send signal to close daemon
}()
status = <-ch
if !status {
os.Exit(1) // error in wait
}
log.Printf("Tested OK\n")
}
|