Пример #1
0
func ExamplePrivateHandle() {
	dns.PrivateHandle("APAIR", TypeAPAIR, NewAPAIR)
	defer dns.PrivateHandleRemove(TypeAPAIR)

	rr, err := dns.NewRR("miek.nl. APAIR (1.2.3.4    1.2.3.5)")
	if err != nil {
		log.Fatal("could not parse APAIR record: ", err)
	}
	fmt.Println(rr)
	// Output: miek.nl.	3600	IN	APAIR	1.2.3.4 1.2.3.5

	m := new(dns.Msg)
	m.Id = 12345
	m.SetQuestion("miek.nl.", TypeAPAIR)
	m.Answer = append(m.Answer, rr)

	fmt.Println(m)
	// ;; opcode: QUERY, status: NOERROR, id: 12345
	// ;; flags: rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0
	//
	// ;; QUESTION SECTION:
	// ;miek.nl.	IN	 APAIR
	//
	// ;; ANSWER SECTION:
	// miek.nl.	3600	IN	APAIR	1.2.3.4 1.2.3.5
}
func (wb *workbench) dnsHandler(w dns.ResponseWriter, r *dns.Msg) {
	wb.mu.RLock()
	defer wb.mu.RUnlock()
	defer w.Close()
	m := new(dns.Msg)
	m.SetReply(r)
	m.Compress = wb.compression

	if len(r.Question) > 1 || r.Rcode != dns.OpcodeQuery {
		m.Rcode = dns.RcodeNotImplemented
	} else if len(r.Question) == 0 {
		m.Rcode = dns.RcodeFormatError
	}
	if m.Rcode == dns.RcodeFormatError || m.Rcode == dns.RcodeNotImplemented {
		w.WriteMsg(m)
		return
	}

	q := &r.Question[0]

	wb.l.Printf("Received query for [%s] %s\n", dns.TypeToString[q.Qtype], q.Name)
	allRecords, present := wb.z[q.Name]
	if !present {
		m.Rcode = dns.RcodeNameError
		w.WriteMsg(m)
		return
	}

	if auth, present := wb.a[q.Name]; present {
		m.Authoritative = true
		m.Ns = append(m.Ns, *auth)
	}

	qRecords, present := allRecords[q.Qtype]
	if !present {
		m.Rcode = dns.RcodeNXRrset
		w.WriteMsg(m)
		return
	}

	m.Answer = append(m.Answer, qRecords...)
	w.WriteMsg(m)
	return
}