本文整理汇总了Golang中github.com/miekg/dns.Msg.Question方法的典型用法代码示例。如果您正苦于以下问题:Golang Msg.Question方法的具体用法?Golang Msg.Question怎么用?Golang Msg.Question使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/miekg/dns.Msg
的用法示例。
在下文中一共展示了Msg.Question方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Handle
// Construct a response for a single DNS request.
func (ds *DjdnsServer) Handle(query *dns.Msg) (*dns.Msg, error) {
response := new(dns.Msg)
response.MsgHdr.Id = query.MsgHdr.Id
response.Question = query.Question
if len(query.Question) > 0 {
// Ignore secondary questions
question := query.Question[0]
records, err := ds.GetRecords(question.Name)
if err != nil {
return nil, err
}
response.Answer = make([]dns.RR, len(records))
for i, record := range records {
answer, err := record.ToDns()
if err != nil {
return nil, err
}
response.Answer[i] = answer
}
response.Ns = make([]dns.RR, 0)
response.Extra = make([]dns.RR, 0)
}
return response, nil
}
示例2: RenewDnsMsg
func RenewDnsMsg(m *dns.Msg) {
m.Extra = nil
m.Answer = nil
m.AuthenticatedData = false
m.CheckingDisabled = false
m.Question = nil
}
示例3: main
func main() {
if len(os.Args) != 2 {
fmt.Printf("%s NAMESERVER\n", os.Args[0])
os.Exit(1)
}
conf, _ := dns.ClientConfigFromFile("/etc/resolv.conf")
m := new(dns.Msg)
m.Question = make([]dns.Question, 1)
c := new(dns.Client)
addr := addresses(conf, c, os.Args[1])
if len(addr) == 0 {
fmt.Printf("No address found for %s\n", os.Args[1])
os.Exit(1)
}
for _, a := range addr {
m.Question[0] = dns.Question{"version.bind.", dns.TypeTXT, dns.ClassCHAOS}
in, rtt, _ := c.ExchangeRtt(m, a)
if in != nil && len(in.Answer) > 0 {
fmt.Printf("(time %.3d µs) %v\n", rtt/1e3, in.Answer[0])
}
m.Question[0] = dns.Question{"hostname.bind.", dns.TypeTXT, dns.ClassCHAOS}
in, rtt, _ = c.ExchangeRtt(m, a)
if in != nil && len(in.Answer) > 0 {
fmt.Printf("(time %.3d µs) %v\n", rtt/1e3, in.Answer[0])
}
}
}
示例4: query
// Performs the actual query by service name (browse) or service instance name (lookup),
// start response listeners goroutines and loops over the entries channel.
func (c *client) query(params *LookupParams) error {
var serviceName, serviceInstanceName string
serviceName = fmt.Sprintf("%s.%s.", trimDot(params.Service), trimDot(params.Domain))
if params.Instance != "" {
serviceInstanceName = fmt.Sprintf("%s.%s", params.Instance, serviceName)
}
// send the query
m := new(dns.Msg)
if serviceInstanceName != "" {
m.Question = []dns.Question{
dns.Question{serviceInstanceName, dns.TypeSRV, dns.ClassINET},
dns.Question{serviceInstanceName, dns.TypeTXT, dns.ClassINET},
}
m.RecursionDesired = false
} else {
m.SetQuestion(serviceName, dns.TypePTR)
m.RecursionDesired = false
}
if err := c.sendQuery(m); err != nil {
return err
}
return nil
}
示例5: lookupHost
func (self *DnsResolver) lookupHost(host string, triesLeft int) ([]net.IP, error) {
m1 := new(dns.Msg)
m1.Id = dns.Id()
m1.RecursionDesired = true
m1.Question = make([]dns.Question, 1)
m1.Question[0] = dns.Question{dns.Fqdn(host), dns.TypeA, dns.ClassINET}
in, err := dns.Exchange(m1, self.Servers[self.r.Intn(len(self.Servers))])
result := []net.IP{}
if err != nil {
if strings.HasSuffix(err.Error(), "i/o timeout") && triesLeft > 0 {
triesLeft -= 1
return self.lookupHost(host, triesLeft)
} else {
return result, err
}
}
if in != nil && in.Rcode != dns.RcodeSuccess {
return result, errors.New(dns.RcodeToString[in.Rcode])
}
for _, record := range in.Answer {
if t, ok := record.(*dns.A); ok {
result = append(result, t.A)
}
}
return result, err
}
示例6: newMsg
func newMsg(host string, qClass uint16) *dns.Msg {
m1 := new(dns.Msg)
m1.Id = dns.Id()
m1.RecursionDesired = true
m1.Question = make([]dns.Question, 1)
m1.Question[0] = dns.Question{host, qClass, dns.ClassINET}
return m1
}
示例7: localQuery
func localQuery(mychan chan DNSreply, qname string, qtype uint16) {
var result DNSreply
var trials uint
result.qname = qname
result.qtype = qtype
result.r = nil
result.err = errors.New("No name server to answer the question")
localm := new(dns.Msg)
localm.Id = dns.Id()
localm.RecursionDesired = true
localm.Question = make([]dns.Question, 1)
localm.SetEdns0(EDNSBUFFERSIZE, false) // Even if no EDNS requested, see #9 May be we should retry without it if timeout?
localc := new(dns.Client)
localc.ReadTimeout = timeout
localm.Question[0] = dns.Question{qname, qtype, dns.ClassINET}
Tests:
for trials = 0; trials < uint(*maxTrials); trials++ {
Resolvers:
for serverIndex := range conf.Servers {
server := conf.Servers[serverIndex]
result.nameserver = server
// Brackets around the server address are necessary for IPv6 name servers
r, rtt, err := localc.Exchange(localm, "["+server+"]:"+conf.Port) // Do not use net.JoinHostPort, see https://github.com/bortzmeyer/check-soa/commit/3e4edb13855d8c4016768796b2892aa83eda1933#commitcomment-2355543
if r == nil {
result.r = nil
result.err = err
if strings.Contains(err.Error(), "timeout") {
// Try another resolver
break Resolvers
} else { // We give in
break Tests
}
} else {
result.rtt = rtt
if r.Rcode == dns.RcodeSuccess {
// TODO: as a result, NODATA (NOERROR/ANSWER=0) are silently ignored (try "foo", for instance, the name exists but no IP address)
// TODO: for rcodes like SERVFAIL, trying another resolver could make sense
result.r = r
result.err = nil
break Tests
} else {
// All the other codes are errors. Yes, it may
// happens that one resolver returns REFUSED
// and the others work but we do not handle
// this case. TODO: delete the resolver from
// the list and try another one
result.r = r
result.err = errors.New(dns.RcodeToString[r.Rcode])
break Tests
}
}
}
}
if *debug {
fmt.Printf("DEBUG: end of DNS request \"%s\" / %d\n", qname, qtype)
}
mychan <- result
}
示例8: 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
}
示例9: dnsHandle
// DNS requests go to this function
func dnsHandle(w dns.ResponseWriter, r *dns.Msg) {
name := r.Question[0].Name
if !namePattern.MatchString(name) {
kilog.Debug("%v does not match pattern, forwarding", name)
dnsForward(w, r)
return
}
// otherwise
kilog.Debug("%v matches pattern, handling", name)
dnsLock.Lock()
defer dnsLock.Unlock()
// check in table first
fakeIP, ok := nameToIP[name]
if !ok {
// place in table
var nwIP string
for {
haha := ipAlloc().String()
_, exists := ipToName[haha]
if exists {
continue
}
nwIP = haha
break
}
fakeIP = nwIP
nameToIP[name] = fakeIP
ipToName[fakeIP] = name
// remove in 30 minutes
go func() {
time.Sleep(time.Minute * 30)
dnsLock.Lock()
defer dnsLock.Unlock()
delete(nameToIP, name)
delete(ipToName, fakeIP)
}()
}
// return the fake IP to the user
resp := new(dns.A)
resp.Hdr.Name = name
resp.Hdr.Ttl = 1 // very short
resp.Hdr.Class = dns.ClassINET
resp.Hdr.Rrtype = dns.TypeA
resp.A = net.ParseIP(fakeIP)
towrite := new(dns.Msg)
towrite.Id = r.Id
towrite.RecursionAvailable = true
towrite.RecursionDesired = true
towrite.Response = true
towrite.Question = r.Question
towrite.Answer = make([]dns.RR, 1)
towrite.Answer[0] = resp
w.WriteMsg(towrite)
kilog.Debug("returning mapping %v -> %v", name, fakeIP)
}
示例10: Test
func (d *DnsDomain) Test() bool {
if !(*Domain)(d).Test() {
return false
}
fqdn := d.Name
if strings.HasPrefix(fqdn, "*.") {
fqdn = "a" + fqdn[1:]
}
if !strings.HasSuffix(fqdn, ".") {
fqdn = fqdn + "."
}
any_ok := false
d.DNS = make([]*DnsRecords, 0, len(DNS_servers))
for name, addr := range DNS_servers {
records := new(DnsRecords)
records.Server = name
records.NS = addr
d.DNS = append(d.DNS, records)
req := new(dns.Msg)
req.Id = dns.Id()
req.RecursionDesired = true
req.Question = []dns.Question{
dns.Question{fqdn, dns.TypeA, dns.ClassINET},
}
resp, err := dns_client.Exchange(req, addr)
if err != nil {
records.Status = 900
records.Message = err.Error()
continue
}
records.IPs = make([]string, 0, len(resp.Answer))
for _, rr := range resp.Answer {
switch a := rr.(type) {
case *dns.RR_A:
records.IPs = append(records.IPs, a.A.String())
}
}
if len(records.IPs) > 0 {
any_ok = true
} else {
records.Status = 900
records.Message = "No records"
}
}
return any_ok
}
示例11: 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
}
示例12: TestServeDNSOverrideIPv6
func TestServeDNSOverrideIPv6(t *testing.T) {
t.Parallel()
const hostname = "foo.com."
var a App
a.Overrides.Store(Overrides{hostname: net.ParseIP("1.2.3.4")})
req := new(dns.Msg)
req.Opcode = dns.OpcodeQuery
req.Question = []dns.Question{{Name: hostname, Qtype: qtypeIPv6}}
var w fDNSResponseWriter
a.ServeDNS(&w, req)
ensure.DeepEqual(t, len(w.msg.Answer), 0)
}
示例13: findSoaNs
func findSoaNs(domain string) (string, string, string) {
var cname string
var soa string
var ns string
add := func(c, s, n string) {
cname += c
soa += s
ns += n
return
}
cname += domain + ","
m1 := new(dns.Msg)
m1.Id = dns.Id()
m1.RecursionDesired = true
m1.Question = make([]dns.Question, 1)
m1.Question[0] = dns.Question{domain, dns.TypeSOA, dns.ClassINET}
in, _ := dns.Exchange(m1, (cf.Servers[1] + ":53"))
rrList := [...][]dns.RR{in.Answer, in.Ns, in.Extra}
for _, rr := range rrList {
for i := len(rr) - 1; i >= 0; i-- {
switch rr[i].Header().Rrtype {
case dns.TypeCNAME:
temp_cname := rr[i].(*dns.CNAME)
add(findSoaNs(temp_cname.Target))
// fmt.Println( "temp_cname:" , temp_cname )
return cname, soa, ns
break
case dns.TypeNS:
temp_ns := rr[i].(*dns.NS)
ns += temp_ns.Ns + "," // + "|" + fmt.Sprint( temp_ns.Hdr.Ttl ) + ","
// fmt.Println( "temp_ns:" , temp_ns )
break
case dns.TypeSOA:
temp_soa := rr[i].(*dns.SOA)
soa += temp_soa.Ns + "," // + "|" + fmt.Sprint( temp_soa.Hdr.Ttl ) + ","
// fmt.Println( "temp_soa:" , temp_soa )
break
}
}
}
return cname, soa, ns
}
示例14: BuildQueryA
func (this *UDPNameServer) BuildQueryA(domain string, id uint16) *alloc.Buffer {
buffer := alloc.NewBuffer()
msg := new(dns.Msg)
msg.Id = id
msg.RecursionDesired = true
msg.Question = []dns.Question{
dns.Question{
Name: dns.Fqdn(domain),
Qtype: dns.TypeA,
Qclass: dns.ClassINET,
}}
writtenBuffer, _ := msg.PackBuffer(buffer.Value)
buffer.Slice(0, len(writtenBuffer))
return buffer
}
示例15: fakeMsg
func fakeMsg(dom string, rrHeader uint16, proto string, serverPort int) (*dns.Msg, error) {
qc := uint16(dns.ClassINET)
c := new(dns.Client)
c.Net = proto
m := new(dns.Msg)
m.Question = make([]dns.Question, 1)
m.Question[0] = dns.Question{
Name: dns.Fqdn(dom),
Qtype: rrHeader,
Qclass: qc,
}
m.RecursionDesired = true
in, _, err := c.Exchange(m, "127.0.0.1:"+strconv.Itoa(serverPort))
return in, err
}