Skip to content

Commit f215cb9

Browse files
committed
separate cname and A record lookups
Signed-off-by: Nino Kodabande <[email protected]>
1 parent 75ba2de commit f215cb9

File tree

1 file changed

+45
-45
lines changed

1 file changed

+45
-45
lines changed

pkg/hostagent/dns/dns.go

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ type Handler struct {
3636
clientConfig *dns.ClientConfig
3737
clients []*dns.Client
3838
IPv6 bool
39-
cname map[string]string
40-
ip map[string]net.IP
39+
cnameToHost map[string]string
40+
hostToIP map[string]net.IP
4141
}
4242

4343
type Server struct {
@@ -99,14 +99,14 @@ func NewHandler(opts HandlerOptions) (dns.Handler, error) {
9999
clientConfig: cc,
100100
clients: clients,
101101
IPv6: opts.IPv6,
102-
cname: make(map[string]string),
103-
ip: make(map[string]net.IP),
102+
cnameToHost: make(map[string]string),
103+
hostToIP: make(map[string]net.IP),
104104
}
105105
for host, address := range opts.StaticHosts {
106106
if ip := net.ParseIP(address); ip != nil {
107-
h.ip[host] = ip
107+
h.hostToIP[host] = ip
108108
} else {
109-
h.cname[host] = limayaml.Cname(address)
109+
h.cnameToHost[host] = limayaml.Cname(address)
110110
}
111111
}
112112
return h, nil
@@ -140,26 +140,60 @@ func (h *Handler) handleQuery(w dns.ResponseWriter, req *dns.Msg) {
140140
qtype = dns.TypeA
141141
}
142142
fallthrough
143-
case dns.TypeCNAME, dns.TypeA:
143+
case dns.TypeA:
144+
var err error
145+
var addrs []net.IP
146+
if _, ok := h.hostToIP[q.Name]; ok {
147+
addrs = []net.IP{h.hostToIP[q.Name]}
148+
} else {
149+
addrs, err = net.LookupIP(q.Name)
150+
if err != nil {
151+
logrus.Debugf("handleQuery lookup IP failed: %v", err)
152+
continue
153+
}
154+
}
155+
for _, ip := range addrs {
156+
var a dns.RR
157+
ipv6 := ip.To4() == nil
158+
if qtype == dns.TypeA && !ipv6 {
159+
hdr.Rrtype = dns.TypeA
160+
a = &dns.A{
161+
Hdr: hdr,
162+
A: ip.To4(),
163+
}
164+
} else if qtype == dns.TypeAAAA && ipv6 {
165+
hdr.Rrtype = dns.TypeAAAA
166+
a = &dns.AAAA{
167+
Hdr: hdr,
168+
AAAA: ip.To16(),
169+
}
170+
} else {
171+
continue
172+
}
173+
reply.Answer = append(reply.Answer, a)
174+
handled = true
175+
}
176+
case dns.TypeCNAME:
144177
cname := q.Name
145178
seen := make(map[string]bool)
146179
for {
147180
// break cyclic definition
148181
if seen[cname] {
149182
break
150183
}
151-
if _, ok := h.cname[cname]; ok {
184+
if _, ok := h.cnameToHost[cname]; ok {
152185
seen[cname] = true
153-
cname = h.cname[cname]
186+
cname = h.cnameToHost[cname]
154187
continue
155188
}
156189
break
157190
}
158191
var err error
159-
if _, ok := h.ip[cname]; !ok {
192+
if _, ok := h.hostToIP[cname]; !ok {
160193
cname, err = net.LookupCNAME(cname)
161194
if err != nil {
162-
break
195+
logrus.Debugf("handleQuery lookup CNAME failed: %v", err)
196+
continue
163197
}
164198
}
165199
if cname != "" && cname != q.Name {
@@ -171,40 +205,6 @@ func (h *Handler) handleQuery(w dns.ResponseWriter, req *dns.Msg) {
171205
reply.Answer = append(reply.Answer, a)
172206
handled = true
173207
}
174-
if qtype == dns.TypeCNAME {
175-
break
176-
}
177-
hdr.Name = cname
178-
var addrs []net.IP
179-
if _, ok := h.ip[cname]; ok {
180-
addrs = []net.IP{h.ip[cname]}
181-
err = nil
182-
} else {
183-
addrs, err = net.LookupIP(cname)
184-
}
185-
if err == nil && len(addrs) > 0 {
186-
for _, ip := range addrs {
187-
var a dns.RR
188-
ipv6 := ip.To4() == nil
189-
if qtype == dns.TypeA && !ipv6 {
190-
hdr.Rrtype = dns.TypeA
191-
a = &dns.A{
192-
Hdr: hdr,
193-
A: ip.To4(),
194-
}
195-
} else if qtype == dns.TypeAAAA && ipv6 {
196-
hdr.Rrtype = dns.TypeAAAA
197-
a = &dns.AAAA{
198-
Hdr: hdr,
199-
AAAA: ip.To16(),
200-
}
201-
} else {
202-
continue
203-
}
204-
reply.Answer = append(reply.Answer, a)
205-
handled = true
206-
}
207-
}
208208
case dns.TypeTXT:
209209
txt, err := net.LookupTXT(q.Name)
210210
if err == nil && len(txt) > 0 {

0 commit comments

Comments
 (0)