本文整理匯總了Golang中github.com/miekg/dns.Msg.SetRcode方法的典型用法代碼示例。如果您正苦於以下問題:Golang Msg.SetRcode方法的具體用法?Golang Msg.SetRcode怎麽用?Golang Msg.SetRcode使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/miekg/dns.Msg
的用法示例。
在下文中一共展示了Msg.SetRcode方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: ServeDNS
// ServeDNS resolution.
func (h *RandomUpstream) ServeDNS(w dns.ResponseWriter, r *dns.Msg) {
ns := h.upstream[rand.Intn(len(h.upstream))]
ns = defaultPort(ns)
for _, q := range r.Question {
log.Printf("[info] [%v] <== %s %s %v (ns %s)\n", r.Id,
dns.ClassToString[q.Qclass],
dns.TypeToString[q.Qtype],
q.Name,
ns)
}
client := &dns.Client{
Net: w.RemoteAddr().Network(),
}
res, rtt, err := client.Exchange(r, ns)
if err != nil {
msg := new(dns.Msg)
msg.SetRcode(r, dns.RcodeServerFailure)
w.WriteMsg(msg)
return
}
log.Printf("[info] [%v] ==> %s:", r.Id, rtt)
for _, a := range res.Answer {
log.Printf("[info] [%v] ----> %s\n", r.Id, a)
}
err = w.WriteMsg(res)
if err != nil {
log.Printf("[error] [%v] failed to respond – %s", r.Id, err)
}
}
示例2: 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)
}
}
}
示例3: NameError
func (s *server) NameError(req *dns.Msg) *dns.Msg {
m := new(dns.Msg)
m.SetRcode(req, dns.RcodeNameError)
m.Ns = []dns.RR{s.NewSOA()}
m.Ns[0].Header().Ttl = s.config.MinTtl
return m
}
示例4: nodeLookup
// nodeLookup is used to handle a node query
func (d *DNSServer) nodeLookup(network, datacenter, node string, req, resp *dns.Msg) {
// Only handle ANY and A type requests
qType := req.Question[0].Qtype
if qType != dns.TypeANY && qType != dns.TypeA {
return
}
// Make an RPC request
args := structs.NodeSpecificRequest{
Datacenter: datacenter,
Node: node,
}
var out structs.IndexedNodeServices
if err := d.agent.RPC("Catalog.NodeServices", &args, &out); err != nil {
d.logger.Printf("[ERR] dns: rpc error: %v", err)
resp.SetRcode(req, dns.RcodeServerFailure)
return
}
// If we have no address, return not found!
if out.NodeServices == nil {
resp.SetRcode(req, dns.RcodeNameError)
return
}
// Add the node record
records := d.formatNodeRecord(&out.NodeServices.Node, req.Question[0].Name, qType)
if records != nil {
resp.Answer = append(resp.Answer, records...)
}
}
示例5: setupRootZone
func (srv *Server) setupRootZone() {
dns.HandleFunc(".", func(w dns.ResponseWriter, r *dns.Msg) {
m := new(dns.Msg)
m.SetRcode(r, dns.RcodeRefused)
w.WriteMsg(m)
})
}
示例6: ServeDNS
func (l Logger) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
state := middleware.State{W: w, Req: r}
for _, rule := range l.Rules {
if middleware.Name(rule.NameScope).Matches(state.Name()) {
responseRecorder := middleware.NewResponseRecorder(w)
rcode, err := l.Next.ServeDNS(ctx, responseRecorder, r)
if rcode > 0 {
// There was an error up the chain, but no response has been written yet.
// The error must be handled here so the log entry will record the response size.
if l.ErrorFunc != nil {
l.ErrorFunc(responseRecorder, r, rcode)
} else {
rc := middleware.RcodeToString(rcode)
answer := new(dns.Msg)
answer.SetRcode(r, rcode)
state.SizeAndDo(answer)
metrics.Report(state, metrics.Dropped, rc, answer.Len(), time.Now())
w.WriteMsg(answer)
}
rcode = 0
}
rep := middleware.NewReplacer(r, responseRecorder, CommonLogEmptyValue)
rule.Log.Println(rep.Replace(rule.Format))
return rcode, err
}
}
return l.Next.ServeDNS(ctx, w, r)
}
示例7: serve
func serve(w dns.ResponseWriter, r *dns.Msg, c *Cache) {
switch {
case r.IsNotify():
if *flaglog {
log.Printf("fks-shield: notify/update")
}
fallthrough
case r.IsUpdate():
client := new(dns.Client)
if p, e := client.Exchange(r, *server); e == nil {
w.WriteMsg(p)
}
return
}
if p := c.Find(r); p != nil {
b := []byte{byte(r.MsgHdr.Id >> 8), byte(r.MsgHdr.Id)}
p = append(b, p...)
w.Write(p)
return
}
// Cache miss
client := new(dns.Client)
if p, e := client.Exchange(r, *server); e == nil {
// TODO(mg): If r has edns0 and p has not we create a mismatch here
w.WriteMsg(p)
c.Insert(p)
return
} else {
log.Printf("fks-shield: failed to get answer " + e.Error())
m := new(dns.Msg)
m.SetRcode(r, dns.RcodeServerFailure)
w.WriteMsg(m)
}
}
示例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: doHandle
func (s *DNSServer) doHandle(w dns.ResponseWriter, r *dns.Msg) *dns.Msg {
m := new(dns.Msg)
m.SetReply(r)
// Send empty response for empty requests
if len(r.Question) == 0 {
m.Ns = s.createSOA()
return m
}
switch r.Question[0].Qtype {
case dns.TypePTR:
s.handlePTRRequest(r, m)
case dns.TypeMX:
s.handleMXRequest(r, m)
case dns.TypeA:
s.handleARequest(r, m)
case dns.TypeSOA:
m.Answer = s.createSOA()
default:
m.Ns = s.createSOA()
m.SetRcode(r, dns.RcodeNotImplemented)
}
return m
}
示例10: handle
func handle(writer dns.ResponseWriter, request *dns.Msg) {
message := new(dns.Msg)
message.SetReply(request)
message.SetRcode(message, dns.RcodeSuccess)
question := request.Question[0]
switch request.Opcode {
case dns.OpcodeNotify:
log.Println(fmt.Sprintf("Recieved NOTIFY for %s", question.Name))
message = handle_notify(question, message, writer)
case dns.OpcodeQuery:
log.Println(fmt.Sprintf("Recieved QUERY for %s", question.Name))
message = handle_query(question, message, writer)
default:
message = handle_error(message, writer, "REFUSED")
}
// Apparently this dns library takes the question out on
// certain RCodes, like REFUSED, which is not right. So we reinsert it.
message.Question[0].Name = question.Name
message.Question[0].Qtype = question.Qtype
message.Question[0].Qclass = question.Qclass
message.MsgHdr.Opcode = request.Opcode
// Send an authoritative answer
message.MsgHdr.Authoritative = true
writer.WriteMsg(message)
}
示例11: serviceLookup
// serviceLookup is used to handle a service query
func (d *DNSServer) serviceLookup(network, datacenter, service, tag string, req, resp *dns.Msg) {
// Make an RPC request
args := structs.ServiceSpecificRequest{
Datacenter: datacenter,
ServiceName: service,
ServiceTag: tag,
TagFilter: tag != "",
QueryOptions: structs.QueryOptions{AllowStale: d.config.AllowStale},
}
var out structs.IndexedCheckServiceNodes
RPC:
if err := d.agent.RPC("Health.ServiceNodes", &args, &out); err != nil {
d.logger.Printf("[ERR] dns: rpc error: %v", err)
resp.SetRcode(req, dns.RcodeServerFailure)
return
}
// Verify that request is not too stale, redo the request
if args.AllowStale && out.LastContact > d.config.MaxStale {
args.AllowStale = false
d.logger.Printf("[WARN] dns: Query results too stale, re-requesting")
goto RPC
}
// If we have no nodes, return not found!
if len(out.Nodes) == 0 {
resp.SetRcode(req, dns.RcodeNameError)
return
}
// Determine the TTL
var ttl time.Duration
if d.config.ServiceTTL != nil {
var ok bool
ttl, ok = d.config.ServiceTTL[service]
if !ok {
ttl = d.config.ServiceTTL["*"]
}
}
// Filter out any service nodes due to health checks
out.Nodes = d.filterServiceNodes(out.Nodes)
// Perform a random shuffle
shuffleServiceNodes(out.Nodes)
// If the network is not TCP, restrict the number of responses
if network != "tcp" && len(out.Nodes) > maxServiceResponses {
out.Nodes = out.Nodes[:maxServiceResponses]
}
// Add various responses depending on the request
qType := req.Question[0].Qtype
d.serviceNodeRecords(out.Nodes, req, resp, ttl)
if qType == dns.TypeSRV {
d.serviceSRVRecords(datacenter, out.Nodes, req, resp, ttl)
}
}
示例12: formerr
func formerr(w dns.ResponseWriter, req *dns.Msg) {
m := new(dns.Msg)
m.MsgHdr.Opcode = dns.OpcodeUpdate
if req.IsTsig() != nil {
m.SetTsig(userFromTsig(req), dns.HmacMD5, 300, time.Now().Unix())
}
w.WriteMsg(m.SetRcode(req, dns.RcodeFormatError))
}
示例13: Err
// NoData write a nodata response to the client.
func (k Kubernetes) Err(zone string, rcode int, state middleware.State) (int, error) {
m := new(dns.Msg)
m.SetRcode(state.Req, rcode)
m.Ns = []dns.RR{k.SOA(zone, state)}
state.SizeAndDo(m)
state.W.WriteMsg(m)
return rcode, nil
}
示例14: NoDataError
func (s *server) NoDataError(m, req *dns.Msg) {
m.SetRcode(req, dns.RcodeSuccess)
m.Ns = []dns.RR{s.NewSOA()}
m.Ns[0].Header().Ttl = s.config.MinTtl
StatsNoDataCount.Inc(1)
promErrorCount.WithLabelValues("nodata")
}
示例15: NameError
func (s *server) NameError(m, req *dns.Msg) {
m.SetRcode(req, dns.RcodeNameError)
m.Ns = []dns.RR{s.NewSOA()}
m.Ns[0].Header().Ttl = s.config.MinTtl
StatsNameErrorCount.Inc(1)
promErrorCount.WithLabelValues("nxdomain")
}