func handleDnsRequest(w dns.ResponseWriter, r *dns.Msg) { m := new(dns.Msg) m.SetReply(r) m.Compress = false switch r.Opcode { case dns.OpcodeQuery: parseQuery(m) case dns.OpcodeUpdate: for _, question := range r.Question { for _, rr := range r.Ns { updateRecord(rr, &question) } } } if r.IsTsig() != nil { if w.TsigStatus() == nil { m.SetTsig(r.Extra[len(r.Extra)-1].(*dns.TSIG).Hdr.Name, dns.HmacMD5, 300, time.Now().Unix()) } else { log.Println("Status", w.TsigStatus().Error()) } } w.WriteMsg(m) }
func parseQuery(m *dns.Msg) { var rr dns.RR for _, q := range m.Question { if read_rr, e := getRecord(q.Name, q.Qtype); e == nil { rr = read_rr.(dns.RR) if rr.Header().Name == q.Name { m.Answer = append(m.Answer, rr) } } else { m2 := new(dns.Msg) if q.Qtype == dns.TypeA { m2.SetQuestion(q.Name, dns.TypeA) } else { m2.Question = make([]dns.Question, 1) m2.Question[0] = q } ret := queryUpStream(m2) if ret != nil { if q.Qtype == dns.TypeA { for _, r := range ret.Answer { // fmt.Printf("%s\n", r) if *cache { storeRecord(r) //FIXME: only be able to save one record } tmp := strings.Split(r.String(), " ") if len(tmp) > 2 && tmp[len(tmp)-2] == "A" { fmt.Printf("%s\n", tmp[len(tmp)-1]) } } } m.Answer = append(m.Answer, ret.Answer...) } } } }