本文整理汇总了Golang中github.com/miekg/dns.Msg.Answer方法的典型用法代码示例。如果您正苦于以下问题:Golang Msg.Answer方法的具体用法?Golang Msg.Answer怎么用?Golang Msg.Answer使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/miekg/dns.Msg
的用法示例。
在下文中一共展示了Msg.Answer方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: dnsHandler
func (manager *DnsManager) dnsHandler(w dns.ResponseWriter, req *dns.Msg) {
m := new(dns.Msg)
m.SetReply(req)
name := req.Question[0].Name
name = strings.TrimSuffix(name, "."+manager.BaseName)
manager.dbMutex.Lock()
entry, ok := manager.db[name]
manager.dbMutex.Unlock()
if ok {
switch req.Question[0].Qtype {
case dns.TypeSRV:
m.Answer = manager.makeAllSRV(entry)
case dns.TypeA:
m.Answer = manager.makeAllA(entry)
if manager.PushSRV {
m.Extra = manager.makeAllSRV(entry)
}
}
}
w.WriteMsg(m)
}
示例2: TestShuffleAnswers
func TestShuffleAnswers(t *testing.T) {
var res Resolver
m := new(dns.Msg)
for i := 0; i < 10; i++ {
name := "10.0.0." + strconv.Itoa(i)
rr, err := res.formatA("blah.com", name)
if err != nil {
t.Error(err)
}
m.Answer = append(m.Answer, rr)
}
n := new(dns.Msg)
c := make([]dns.RR, len(m.Answer))
copy(c, m.Answer)
n.Answer = c
_ = shuffleAnswers(m.Answer)
sflag := false
// 10! chance of failing here
for i := 0; i < 10; i++ {
if n.Answer[i] != m.Answer[i] {
sflag = true
break
}
}
if !sflag {
t.Error("not shuffling")
}
}
示例3: handleSpecialNames
func handleSpecialNames(w dns.ResponseWriter, req *dns.Msg) bool {
question := req.Question[0]
nameLC := strings.ToLower(question.Name)
for _, localRR := range localRRS {
if nameLC == localRR.Name {
m := new(dns.Msg)
m.Id = req.Id
m.Answer = []dns.RR{*localRR.RR}
m.Response = true
w.WriteMsg(m)
return true
}
}
if question.Qtype != dns.TypeANY {
return false
}
m := new(dns.Msg)
m.Id = req.Id
hinfo := new(dns.HINFO)
hinfo.Hdr = dns.RR_Header{Name: question.Name, Rrtype: dns.TypeHINFO,
Class: dns.ClassINET, Ttl: 86400}
hinfo.Cpu = "ANY is not supported any more"
hinfo.Os = "See draft-jabley-dnsop-refuse-any"
m.Answer = []dns.RR{hinfo}
m.Response = true
w.WriteMsg(m)
return true
}
示例4: handleARequest
func (s *DNSServer) handleARequest(r *dns.Msg, m *dns.Msg) {
m.Answer = make([]dns.RR, 0, 2)
query := r.Question[0].Name
if query[len(query)-1] == '.' {
query = query[:len(query)-1]
}
for service := range s.queryServices(query) {
rr := new(dns.A)
var ttl int
if service.Ttl != -1 {
ttl = service.Ttl
} else {
ttl = s.config.ttl
}
rr.Hdr = dns.RR_Header{
Name: r.Question[0].Name,
Rrtype: dns.TypeA,
Class: dns.ClassINET,
Ttl: uint32(ttl),
}
rr.A = service.Ip
m.Answer = append(m.Answer, rr)
}
}
示例5: handlePTRRequest
func (s *DNSServer) handlePTRRequest(r *dns.Msg, m *dns.Msg) {
m.Answer = make([]dns.RR, 0, 2)
query := r.Question[0].Name
if query[len(query)-1] == '.' {
query = query[:len(query)-1]
}
for service := range s.queryIp(query) {
var ttl int
if service.Ttl != -1 {
ttl = service.Ttl
} else {
ttl = s.config.ttl
}
for domain := range s.listDomains(service) {
rr := new(dns.PTR)
rr.Hdr = dns.RR_Header{
Name: r.Question[0].Name,
Rrtype: dns.TypePTR,
Class: dns.ClassINET,
Ttl: uint32(ttl),
}
rr.Ptr = domain
m.Answer = append(m.Answer, rr)
}
}
}
示例6: ServeDNS
func (h dnsHandler) ServeDNS(w dns.ResponseWriter, r *dns.Msg) {
if len(r.Question) != 1 {
h.t.Fatalf("bad: %#v", r.Question)
}
name := "join.service.consul."
question := r.Question[0]
if question.Name != name || question.Qtype != dns.TypeANY {
h.t.Fatalf("bad: %#v", question)
}
m := new(dns.Msg)
m.SetReply(r)
m.Authoritative = true
m.RecursionAvailable = false
m.Answer = append(m.Answer, &dns.A{
Hdr: dns.RR_Header{
Name: name,
Rrtype: dns.TypeA,
Class: dns.ClassINET},
A: net.ParseIP("127.0.0.1"),
})
m.Answer = append(m.Answer, &dns.AAAA{
Hdr: dns.RR_Header{
Name: name,
Rrtype: dns.TypeAAAA,
Class: dns.ClassINET},
AAAA: net.ParseIP("2001:db8:a0b:12f0::1"),
})
if err := w.WriteMsg(m); err != nil {
h.t.Fatalf("err: %v", err)
}
}
示例7: handleIPQuery
func (r *resolver) handleIPQuery(name string, query *dns.Msg, ipType int) (*dns.Msg, error) {
addr := r.sb.ResolveName(name, ipType)
if addr == nil {
return nil, nil
}
log.Debugf("Lookup for %s: IP %v", name, addr)
resp := new(dns.Msg)
resp.SetReply(query)
setCommonFlags(resp)
if len(addr) > 1 {
addr = shuffleAddr(addr)
}
if ipType == netutils.IPv4 {
for _, ip := range addr {
rr := new(dns.A)
rr.Hdr = dns.RR_Header{Name: name, Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: respTTL}
rr.A = ip
resp.Answer = append(resp.Answer, rr)
}
} else {
for _, ip := range addr {
rr := new(dns.AAAA)
rr.Hdr = dns.RR_Header{Name: name, Rrtype: dns.TypeAAAA, Class: dns.ClassINET, Ttl: respTTL}
rr.AAAA = ip
resp.Answer = append(resp.Answer, rr)
}
}
return resp, nil
}
示例8: handlerFunc
// handlerFunc receives requests, looks up the result and returns what is found.
func handlerFunc(res dns.ResponseWriter, req *dns.Msg) {
message := new(dns.Msg)
switch req.Opcode {
case dns.OpcodeQuery:
message.SetReply(req)
message.Compress = false
message.Answer = make([]dns.RR, 0)
for _, question := range message.Question {
answers := answerQuestion(strings.ToLower(question.Name), question.Qtype)
if len(answers) > 0 {
for i := range answers {
message.Answer = append(message.Answer, answers[i])
}
} else {
// If there are no records, go back through and search for SOA records
for _, question := range message.Question {
answers := answerQuestion(strings.ToLower(question.Name), dns.TypeSOA)
for i := range answers {
message.Ns = append(message.Ns, answers[i])
}
}
}
}
if len(message.Answer) == 0 && len(message.Ns) == 0 {
message.Rcode = dns.RcodeNameError
}
default:
message = message.SetRcode(req, dns.RcodeNotImplemented)
}
res.WriteMsg(message)
}
示例9: ServeDNS
func (c Chaos) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
state := middleware.State{W: w, Req: r}
if state.QClass() != dns.ClassCHAOS || state.QType() != dns.TypeTXT {
return c.Next.ServeDNS(ctx, w, r)
}
m := new(dns.Msg)
m.SetReply(r)
hdr := dns.RR_Header{Name: state.QName(), Rrtype: dns.TypeTXT, Class: dns.ClassCHAOS, Ttl: 0}
switch state.Name() {
default:
return c.Next.ServeDNS(ctx, w, r)
case "authors.bind.":
for a, _ := range c.Authors {
m.Answer = append(m.Answer, &dns.TXT{Hdr: hdr, Txt: []string{trim(a)}})
}
case "version.bind.", "version.server.":
m.Answer = []dns.RR{&dns.TXT{Hdr: hdr, Txt: []string{trim(c.Version)}}}
case "hostname.bind.", "id.server.":
hostname, err := os.Hostname()
if err != nil {
hostname = "localhost"
}
m.Answer = []dns.RR{&dns.TXT{Hdr: hdr, Txt: []string{trim(hostname)}}}
}
state.SizeAndDo(m)
w.WriteMsg(m)
return 0, nil
}
示例10: Handler
func (s HelixServer) Handler(w dns.ResponseWriter, req *dns.Msg) {
m := new(dns.Msg)
m.SetReply(req)
qType := req.Question[0].Qtype
qClass := req.Question[0].Qclass
header := dns.RR_Header{Name: m.Question[0].Name, Rrtype: qType, Class: qClass, Ttl: 5}
resp, err := s.getResponse(req.Question[0])
if err != nil {
if s.DNSClient != nil {
log.Printf("Could not get record for %s, forwarding to %s", req.Question[0].Name, s.DNSClient.GetAddress())
in, _ := s.DNSClient.Lookup(req)
w.WriteMsg(in)
} else {
log.Printf("Could not get record for %s", req.Question[0].Name)
w.WriteMsg(m)
}
return
}
switch qType {
case dns.TypeA:
m.Answer = make([]dns.RR, 1)
m.Answer[0] = &dns.A{Hdr: header, A: net.ParseIP(resp.Value())}
case dns.TypeAAAA:
m.Answer = make([]dns.RR, 1)
m.Answer[0] = &dns.AAAA{Hdr: header, AAAA: net.ParseIP(resp.Value())}
case dns.TypeSRV:
var records []SrvRecord
err := json.Unmarshal([]byte(resp.Value()), &records)
if err != nil {
log.Printf("Could not unmarshal SRV record from etcd: %s", resp.Value())
} else {
m.Answer = make([]dns.RR, len(records))
for i := range records {
m.Answer[i] = &dns.SRV{
Hdr: header,
Priority: records[i].Priority,
Weight: records[i].Weight,
Port: records[i].Port,
Target: records[i].Target,
}
}
}
case dns.TypePTR:
m.Answer = make([]dns.RR, 1)
m.Answer[0] = &dns.PTR{Hdr: header, Ptr: resp.Value()}
case dns.TypeCNAME:
m.Answer = make([]dns.RR, 1)
m.Answer[0] = &dns.CNAME{Hdr: header, Target: resp.Value()}
default:
log.Printf("Unrecognised record type: %d", qType)
}
w.WriteMsg(m)
}
示例11: handleReverseRequest
func (s *DNSServer) handleReverseRequest(w dns.ResponseWriter, r *dns.Msg) {
m := new(dns.Msg)
m.SetReply(r)
// Send empty response for empty requests
if len(r.Question) == 0 {
m.Ns = s.createSOA()
w.WriteMsg(m)
return
}
m.Answer = make([]dns.RR, 0, 2)
query := r.Question[0].Name
// trim off any trailing dot
if query[len(query)-1] == '.' {
query = query[:len(query)-1]
}
for service := range s.queryIp(query) {
if r.Question[0].Qtype != dns.TypePTR {
m.Ns = s.createSOA()
w.WriteMsg(m)
return
}
var ttl int
if service.Ttl != -1 {
ttl = service.Ttl
} else {
ttl = s.config.ttl
}
for domain := range s.listDomains(service) {
rr := new(dns.PTR)
rr.Hdr = dns.RR_Header{
Name: r.Question[0].Name,
Rrtype: dns.TypePTR,
Class: dns.ClassINET,
Ttl: uint32(ttl),
}
rr.Ptr = domain
m.Answer = append(m.Answer, rr)
}
}
if len(m.Answer) != 0 {
w.WriteMsg(m)
} else {
// We didn't find a record corresponding to the query,
// try forwarding
s.handleForward(w, r)
}
}
示例12: handleRequest
func (s *DNSServer) handleRequest(w dns.ResponseWriter, r *dns.Msg) {
m := new(dns.Msg)
m.SetReply(r)
// Send empty response for empty requests
if len(r.Question) == 0 {
m.Ns = s.createSOA()
w.WriteMsg(m)
return
}
// respond to SOA requests
if r.Question[0].Qtype == dns.TypeSOA {
m.Answer = s.createSOA()
w.WriteMsg(m)
return
}
m.Answer = make([]dns.RR, 0, 2)
query := r.Question[0].Name
// trim off any trailing dot
if query[len(query)-1] == '.' {
query = query[:len(query)-1]
}
for service := range s.queryServices(query) {
var rr dns.RR
switch r.Question[0].Qtype {
case dns.TypeA:
rr = s.makeServiceA(r.Question[0].Name, service)
case dns.TypeMX:
rr = s.makeServiceMX(r.Question[0].Name, service)
default:
// this query type isn't supported, but we do have
// a record with this name. Per RFC 4074 sec. 3, we
// immediately return an empty NOERROR reply.
m.Ns = s.createSOA()
m.MsgHdr.Authoritative = true
w.WriteMsg(m)
return
}
m.Answer = append(m.Answer, rr)
}
// We didn't find a record corresponding to the query
if len(m.Answer) == 0 {
m.Ns = s.createSOA()
m.SetRcode(r, dns.RcodeNameError) // NXDOMAIN
}
w.WriteMsg(m)
}
示例13: zoneHandler
func (z *zone) zoneHandler(c *config, w dns.ResponseWriter, req *dns.Msg) {
c.stats.Incr("query.request", 1)
m := new(dns.Msg)
m.SetReply(req)
m.Authoritative = true
m.Answer = []dns.RR{}
questions := []string{}
answers := []string{}
if len(req.Question) != 1 {
c.stats.Incr("query.error", 1)
log.Printf("Warning: len(req.Question) != 1")
return
}
q := req.Question[0]
questions = append(questions, fmt.Sprintf("%s[%s]", q.Name, dns.TypeToString[q.Qtype]))
if q.Qclass != uint16(dns.ClassINET) {
c.stats.Incr("query.error", 1)
log.Printf("Warning: skipping unhandled class: %s", dns.ClassToString[q.Qclass])
return
}
for _, record := range z.rrs {
h := record.Header()
if q.Name != h.Name {
continue
}
txt := record.String()
if q.Qtype == dns.TypeA && h.Rrtype == dns.TypeCNAME { // special handling for A queries w/CNAME results
if q.Name == dns.Fqdn(z.name) { // flatten root CNAME
flat, err := c.flattenCNAME(record.(*dns.CNAME))
if err != nil || flat == nil {
log.Printf("flattenCNAME error: %s", err.Error())
} else {
for _, record := range flat {
m.Answer = append(m.Answer, record)
answers = append(answers, "(FLAT)"+record.String())
}
}
continue
} // don't flatten other CNAMEs for now
} else if q.Qtype != h.Rrtype && q.Qtype != dns.TypeANY { // skip RRs that don't match
continue
}
m.Answer = append(m.Answer, record)
answers = append(answers, txt)
}
//m.Extra = []dns.RR{}
//m.Extra = append(m.Extra, &dns.TXT{Hdr: dns.RR_Header{Name: m.Question[0].Name, Rrtype: dns.TypeTXT, Class: dns.ClassINET, Ttl: 0}, Txt: []string{"DNS rocks"}})
c.debug(fmt.Sprintf("Query [%s] %s -> %s ", w.RemoteAddr().String(), strings.Join(questions, ","), strings.Join(answers, ",")))
c.stats.Incr("query.answer", 1)
w.WriteMsg(m)
}
示例14: registerVersionHandler
func (c *config) registerVersionHandler() { // special handler for reporting version: dig . @host TXT
dns.HandleFunc(".", func(w dns.ResponseWriter, req *dns.Msg) {
m := new(dns.Msg)
m.SetReply(req)
if req.Question[0].Name == "." && req.Question[0].Qtype == dns.TypeTXT {
m.Authoritative = true
m.Answer = []dns.RR{}
m.Answer = append(m.Answer, &dns.TXT{Hdr: dns.RR_Header{Name: m.Question[0].Name, Rrtype: dns.TypeTXT, Class: dns.ClassINET, Ttl: 0}, Txt: []string{"v" + version}})
m.Extra = []dns.RR{}
m.Extra = append(m.Extra, &dns.TXT{Hdr: dns.RR_Header{Name: m.Question[0].Name, Rrtype: dns.TypeTXT, Class: dns.ClassINET, Ttl: 0}, Txt: []string{"NedDNS"}})
}
w.WriteMsg(m)
})
}
示例15: Respond
func Respond(w dns.ResponseWriter, req *dns.Msg, records []dns.RR) {
m := new(dns.Msg)
m.SetReply(req)
m.Authoritative = true
m.RecursionAvailable = true
m.Compress = true
m.Answer = records
// Figure out the max response size
bufsize := uint16(512)
tcp := isTcp(w)
if o := req.IsEdns0(); o != nil {
bufsize = o.UDPSize()
}
if tcp {
bufsize = dns.MaxMsgSize - 1
} else if bufsize < 512 {
bufsize = 512
}
if m.Len() > dns.MaxMsgSize {
fqdn := dns.Fqdn(req.Question[0].Name)
log.WithFields(log.Fields{"fqdn": fqdn}).Debug("Response too big, dropping Extra")
m.Extra = nil
if m.Len() > dns.MaxMsgSize {
log.WithFields(log.Fields{"fqdn": fqdn}).Debug("Response still too big")
m := new(dns.Msg)
m.SetRcode(m, dns.RcodeServerFailure)
}
}
if m.Len() > int(bufsize) && !tcp {
log.Debug("Too big 1")
m.Extra = nil
if m.Len() > int(bufsize) {
log.Debug("Too big 2")
m.Answer = nil
m.Truncated = true
}
}
err := w.WriteMsg(m)
if err != nil {
log.Warn("Failed to return reply: ", err, m.Len())
}
}