Cleaner handling of our own multicast queries

This commit is contained in:
Bryan Boreham committed 2014-10-03 16:42:02 +01:00
1 parent eedb6a1c30
commit c3f4c1a85f
2 files changed
+21 -2

No files matched your search

+4 -1
View File
@@ -26,7 +26,6 @@ var mdnsClient *weavedns.MDNSClient
func handleMDNS(w dns.ResponseWriter, r *dns.Msg) {
// Here we have to handle both requests incoming, responses to queries
// we sent out, and responses to queries that other nodes sent
// And we also get to see our own queries!
if len(r.Answer) > 0 {
mdnsClient.HandleResponse(r)
} else if len(r.Question) > 0 {
@@ -35,6 +34,10 @@ func handleMDNS(w dns.ResponseWriter, r *dns.Msg) {
if err == nil {
m := makeDNSReply(r, q.Name, net.ParseIP(ip))
mdnsClient.SendResponse(m)
} else if mdnsClient.IsInflightQuery(r) {
// ignore this - it's our own query received via multicast
} else {
log.Printf("Failed MDNS lookup for %s", q.Name)
}
}
}
+17 -1
View File
@@ -73,7 +73,7 @@ func (c *MDNSClient) SendQuery(name string, querytype uint16, responseCh chan<-
c.Lock()
query, found := c.inflight[name]
if !found {
query = &inflightQuery{name: name}
query = &inflightQuery{name: name, Id: m.Id}
c.inflight[name] = query
}
info := &responseInfo{ch: responseCh}
@@ -97,6 +97,22 @@ func (c *MDNSClient) SendResponse(m *dns.Msg) error {
return err
}
func (c *MDNSClient) IsInflightQuery(m *dns.Msg) bool {
if len(m.Question) == 1 {
q := m.Question[0]
if q.Qtype == dns.TypeA {
c.Lock()
defer c.Unlock()
if query, found := c.inflight[q.Name]; found {
if query.Id == m.Id {
return true
}
}
}
}
return false
}
func (c *MDNSClient) HandleResponse(r *dns.Msg) {
for _, answer := range r.Answer {
switch rr := answer.(type) {