本文整理汇总了Golang中github.com/miekg/dns.Msg.Authoritative方法的典型用法代码示例。如果您正苦于以下问题:Golang Msg.Authoritative方法的具体用法?Golang Msg.Authoritative怎么用?Golang Msg.Authoritative使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/miekg/dns.Msg
的用法示例。
在下文中一共展示了Msg.Authoritative方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: answerForRequest
// Instantiate a new answer as a reply of the passed message.
func (h *ENUMHandler) answerForRequest(message *dns.Msg) *dns.Msg {
answer := new(dns.Msg)
answer.SetReply(message)
answer.Authoritative = true
answer.RecursionAvailable = false
return answer
}
示例2: handleDomain
// handleDomain handles DNS queries that come to the cluster
func (d *DnsServer) handleDomain(w dns.ResponseWriter, r *dns.Msg) {
dom, qType := parseQuestion(r)
q := dns.TypeToString[qType] + " " + dom
log.Printf("--> Internal: %s", q)
m := new(dns.Msg)
m.SetReply(r)
m.Authoritative = true
supported, found, recs := d.queryRR(qType, dom)
if !supported {
log.Printf("<-x %s: NOTIMP", q)
m.SetRcode(r, dns.RcodeNotImplemented) // NOTIMP
} else if !found {
log.Printf("<-x %s: NXDOMAIN", q)
m.SetRcode(r, dns.RcodeNameError) // NXDOMAIN
} else {
for _, rec := range recs {
rr, err := rrtype.ToRR(qType, dom, rec)
if err != nil {
log.Printf("<-x %s SERVFAIL: record conv err: %v", q, err)
m.SetRcode(r, dns.RcodeServerFailure)
break
} else {
log.Printf("<-- %s: %s", q, rr.String())
m.Answer = append(m.Answer, rr)
}
}
}
w.WriteMsg(m)
}
示例3: handleQuery
// handleQUery is used to handle DNS queries in the configured domain
func (d *DNSServer) handleQuery(resp dns.ResponseWriter, req *dns.Msg) {
q := req.Question[0]
defer func(s time.Time) {
d.logger.Printf("[DEBUG] dns: request for %v (%v)", q, time.Now().Sub(s))
}(time.Now())
// Switch to TCP if the client is
network := "udp"
if _, ok := resp.RemoteAddr().(*net.TCPAddr); ok {
network = "tcp"
}
// Setup the message response
m := new(dns.Msg)
m.SetReply(req)
m.Authoritative = true
m.RecursionAvailable = (len(d.recursors) > 0)
// Only add the SOA if requested
if req.Question[0].Qtype == dns.TypeSOA {
d.addSOA(d.domain, m)
}
// Dispatch the correct handler
d.dispatch(network, req, m)
// Write out the complete response
if err := resp.WriteMsg(m); err != nil {
d.logger.Printf("[WARN] dns: failed to respond: %v", err)
}
}
示例4: handleRequest
func (s *jujuNameServer) handleRequest(w dns.ResponseWriter, r *dns.Msg) {
m := new(dns.Msg)
m.SetReply(r)
for _, q := range r.Question {
rr, err := s.answer(q)
if err != nil {
m.SetRcodeFormatError(r)
t := new(dns.TXT)
t.Hdr = dns.RR_Header{
Name: q.Name,
Rrtype: dns.TypeTXT,
Class: dns.ClassNONE,
}
t.Txt = []string{err.Error()}
m.Extra = append(m.Extra, t)
continue
} else if rr != nil {
m.Answer = append(m.Answer, rr)
}
}
m.Authoritative = true
// recursion isn't really available, but it's apparently
// necessary to set this to make nslookup happy.
m.RecursionAvailable = true
w.WriteMsg(m)
}
示例5: 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)
}
}
示例6: GenerateServeDNS
func (c *Config) GenerateServeDNS(hostname string) func(dns.ResponseWriter, *dns.Msg) {
if len(hostname) > 0 {
if hostname[len(hostname)-1] != '.' {
hostname += "."
}
}
return func(w dns.ResponseWriter, r *dns.Msg) {
m := new(dns.Msg)
m.SetReply(r)
m.Authoritative = true
ip, err := c.GetOrLaunchInstance()
if err != nil {
// TODO: error handling
log.Println("Error in GetOrLaunchInstance:", err)
w.Close()
return
}
rr := new(dns.A)
rr.Hdr = dns.RR_Header{Name: hostname, Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: uint32(c.MaxIdleTime.Seconds())}
rr.A = ip
m.Answer = []dns.RR{rr}
w.WriteMsg(m)
}
}
示例7: handleTest
// handleTest is used to handle DNS queries in the ".consul." domain
func (d *DNSServer) handleTest(resp dns.ResponseWriter, req *dns.Msg) {
q := req.Question[0]
defer func(s time.Time) {
d.logger.Printf("[DEBUG] dns: request for %v (%v)", q, time.Now().Sub(s))
}(time.Now())
if !(q.Qtype == dns.TypeANY || q.Qtype == dns.TypeTXT) {
return
}
if q.Name != testQuery {
return
}
// Always respond with TXT "ok"
m := new(dns.Msg)
m.SetReply(req)
m.Authoritative = true
m.RecursionAvailable = true
header := dns.RR_Header{Name: q.Name, Rrtype: dns.TypeTXT, Class: dns.ClassINET, Ttl: 0}
txt := &dns.TXT{header, []string{"ok"}}
m.Answer = append(m.Answer, txt)
d.addSOA(consulDomain, m)
if err := resp.WriteMsg(m); err != nil {
d.logger.Printf("[WARN] dns: failed to respond: %v", err)
}
}
示例8: main
func main() {
runtime.GOMAXPROCS(runtime.NumCPU() * 4)
for z, rr := range zones {
rrx := rr.(*dns.SOA) // Needed to create the actual RR, and not an reference.
dns.HandleFunc(z, func(w dns.ResponseWriter, r *dns.Msg) {
m := new(dns.Msg)
m.SetReply(r)
m.Authoritative = true
m.Ns = []dns.RR{rrx}
w.WriteMsg(m)
})
}
go func() {
err := dns.ListenAndServe(":8053", "tcp", nil)
if err != nil {
log.Fatal("Failed to set tcp listener %s\n", err.Error())
}
}()
go func() {
err := dns.ListenAndServe(":8053", "udp", nil)
if err != nil {
log.Fatal("Failed to set udp listener %s\n", err.Error())
}
}()
sig := make(chan os.Signal)
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
for {
select {
case s := <-sig:
log.Fatalf("Signal (%d) received, stopping\n", s)
}
}
}
示例9: handleExternal
// handleExternal handles DNS queries that are outside the cluster's domain such
// as the Public Internet.
func (d *DnsServer) handleExternal(w dns.ResponseWriter, r *dns.Msg) {
dom, qType := parseQuestion(r)
q := dns.TypeToString[qType] + " " + dom
log.Printf("--> External: %s", q)
if !d.recurse {
log.Printf("<-x %s: SERVFAIL: recursion disabled", q)
m := new(dns.Msg)
m.SetReply(r)
m.SetRcode(r, dns.RcodeServerFailure)
m.Authoritative = false
m.RecursionAvailable = false
w.WriteMsg(m)
} else {
in, ns, err := d.queryExternal(r)
if err != nil {
log.Printf("<-x %s (@%s): SERVFAIL: %v", q, ns, err)
m := new(dns.Msg)
m.SetReply(r)
m.SetRcode(r, dns.RcodeServerFailure)
w.WriteMsg(m)
} else {
log.Printf("<-- %s (@%s): %d answers, %d extra, %d ns", q, ns, len(in.Answer), len(in.Extra), len(in.Ns))
in.Compress = true
w.WriteMsg(in)
}
}
}
示例10: handlePtr
// handlePtr is used to handle "reverse" DNS queries
func (d *DNSServer) handlePtr(resp dns.ResponseWriter, req *dns.Msg) {
q := req.Question[0]
defer func(s time.Time) {
d.logger.Printf("[DEBUG] dns: request for %v (%v) from client %s (%s)",
q, time.Now().Sub(s), resp.RemoteAddr().String(),
resp.RemoteAddr().Network())
}(time.Now())
// Setup the message response
m := new(dns.Msg)
m.SetReply(req)
m.Authoritative = true
m.RecursionAvailable = (len(d.recursors) > 0)
// Only add the SOA if requested
if req.Question[0].Qtype == dns.TypeSOA {
d.addSOA(d.domain, m)
}
datacenter := d.agent.config.Datacenter
// Get the QName without the domain suffix
qName := strings.ToLower(dns.Fqdn(req.Question[0].Name))
args := structs.DCSpecificRequest{
Datacenter: datacenter,
QueryOptions: structs.QueryOptions{
Token: d.agent.config.ACLToken,
AllowStale: d.config.AllowStale,
},
}
var out structs.IndexedNodes
// TODO: Replace ListNodes with an internal RPC that can do the filter
// server side to avoid transferring the entire node list.
if err := d.agent.RPC("Catalog.ListNodes", &args, &out); err == nil {
for _, n := range out.Nodes {
arpa, _ := dns.ReverseAddr(n.Address)
if arpa == qName {
ptr := &dns.PTR{
Hdr: dns.RR_Header{Name: q.Name, Rrtype: dns.TypePTR, Class: dns.ClassINET, Ttl: 0},
Ptr: fmt.Sprintf("%s.node.%s.%s", n.Node, datacenter, d.domain),
}
m.Answer = append(m.Answer, ptr)
break
}
}
}
// nothing found locally, recurse
if len(m.Answer) == 0 {
d.handleRecurse(resp, req)
return
}
// Write out the complete response
if err := resp.WriteMsg(m); err != nil {
d.logger.Printf("[WARN] dns: failed to respond: %v", err)
}
}
示例11: prepareFailureMsg
func prepareFailureMsg(req *dns.Msg) *dns.Msg {
failMsg := new(dns.Msg)
failMsg.Id = req.Id
failMsg.Response = true
failMsg.Authoritative = true
failMsg.Question = req.Question
failMsg.Rcode = dns.RcodeNameError
return failMsg
}
示例12: ServeDNS
func (ds *DNSServer) ServeDNS(rw dns.ResponseWriter, mes *dns.Msg) {
resp := new(dns.Msg)
for _, q := range mes.Question {
log.Printf("DNS request from %s: %s", rw.RemoteAddr(), &q)
switch q.Qtype {
case dns.TypeA, dns.TypeAAAA:
val, err := ds.HandleLookup(q.Name)
if err != nil {
log.Println(err)
continue
}
if q.Qclass != dns.ClassINET {
log.Printf("error: got invalid DNS question class %d\n", q.Qclass)
continue
}
header := dns.RR_Header{
Name: q.Name,
Rrtype: q.Qtype,
Class: dns.ClassINET,
Ttl: DefaultResponseTTL,
}
var rr dns.RR
// not really super sure why these have to be different types
if q.Qtype == dns.TypeA {
rr = &dns.A{
A: net.ParseIP(val),
Hdr: header,
}
} else if q.Qtype == dns.TypeAAAA {
rr = &dns.AAAA{
AAAA: net.ParseIP(val),
Hdr: header,
}
} else {
panic("unreachable")
}
resp.Answer = append(resp.Answer, rr)
default:
log.Printf("unhandled qtype: %d\n", q.Qtype)
}
}
resp.Authoritative = true
resp.Id = mes.Id
resp.Response = true
if err := rw.WriteMsg(resp); err != nil {
log.Printf("error responding to DNS query: %s", err)
}
}
示例13: cacheMsg
func cacheMsg(m *dns.Msg, tc cacheTestCase) *dns.Msg {
m.RecursionAvailable = tc.RecursionAvailable
m.AuthenticatedData = tc.AuthenticatedData
m.Authoritative = tc.Authoritative
m.Truncated = tc.Truncated
m.Answer = tc.in.Answer
m.Ns = tc.in.Ns
// m.Extra = tc.in.Extra , not the OPT record!
return m
}
示例14: prepareAnswerMsg
func prepareAnswerMsg(req *dns.Msg, answers []dns.RR) *dns.Msg {
answerMsg := new(dns.Msg)
answerMsg.Id = req.Id
answerMsg.Response = true
answerMsg.Authoritative = true
answerMsg.Question = req.Question
answerMsg.Answer = answers
answerMsg.Rcode = dns.RcodeSuccess
answerMsg.Extra = []dns.RR{}
return answerMsg
}
示例15: 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)
}