-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathquery.go
62 lines (53 loc) · 1.61 KB
/
query.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
package main
import (
"fmt"
"github.com/holoplot/go-avahi"
"github.com/miekg/dns"
"github.com/sirupsen/logrus"
)
func createDNSReply(logger *logrus.Entry, aserver *avahi.Server, r *dns.Msg) *dns.Msg {
m := new(dns.Msg)
m.Compress = false
switch r.Opcode {
case dns.OpcodeQuery:
for _, q := range r.Question {
switch q.Qtype {
case dns.TypeA:
rr, err := avahiToRecord(logger, aserver, q.Name, avahi.ProtoInet, "A")
if err != nil {
logger.WithError(err).Error("avahi A lookup failed, skipping query...")
continue
}
m.Answer = append(m.Answer, rr)
case dns.TypeAAAA:
rr, err := avahiToRecord(logger, aserver, q.Name, avahi.ProtoInet6, "AAAA")
if err != nil {
logger.WithError(err).Error("avahi AAAA lookup failed, skipping query...")
continue
}
m.Answer = append(m.Answer, rr)
default:
logger.WithField("type", q.Qtype).Warning("unsupported question")
}
}
default:
logger.WithField("opcode", r.Opcode).Warning("unsupported opcode")
}
return m
}
func avahiToRecord(logger *logrus.Entry, aserver *avahi.Server, name string, proto int32, recordType string) (dns.RR, error) {
logger.WithFields(logrus.Fields{
"name": name,
"type": recordType,
"protocol": proto,
}).Info("forwarding query to avahi")
hn, err := aserver.ResolveHostName(avahi.InterfaceUnspec, proto, name, proto, 0)
if err != nil {
return nil, fmt.Errorf("avahi resolve failure: %w", err)
}
rr, err := dns.NewRR(fmt.Sprintf("%s %s %s", name, recordType, hn.Address))
if err != nil {
return nil, fmt.Errorf("failured to create record: %w", err)
}
return rr, err
}