本文整理汇总了Golang中github.com/miekg/dns.Msg.SetEdns0方法的典型用法代码示例。如果您正苦于以下问题:Golang Msg.SetEdns0方法的具体用法?Golang Msg.SetEdns0怎么用?Golang Msg.SetEdns0使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/miekg/dns.Msg
的用法示例。
在下文中一共展示了Msg.SetEdns0方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestDNSTtlRR
func TestDNSTtlRR(t *testing.T) {
s := newTestServerDNSSEC(t, false)
defer s.Stop()
serv := &msg.Service{Host: "10.0.0.2", Key: "ttl.skydns.test.", Ttl: 360}
addService(t, s, serv.Key, time.Duration(serv.Ttl)*time.Second, serv)
defer delService(t, s, serv.Key)
c := new(dns.Client)
tc := dnsTestCases[9] // TTL Test
t.Logf("%v\n", tc)
m := new(dns.Msg)
m.SetQuestion(tc.Qname, tc.Qtype)
if tc.dnssec == true {
m.SetEdns0(4096, true)
}
resp, _, err := c.Exchange(m, "127.0.0.1:"+StrPort)
if err != nil {
t.Errorf("failing: %s: %s\n", m.String(), err.Error())
}
t.Logf("%s\n", resp)
for i, a := range resp.Answer {
if a.Header().Ttl != 360 {
t.Errorf("Answer %d should have a Header TTL of %d, but has %d", i, 360, a.Header().Ttl)
}
}
}
示例2: TestDNSTtlRRset
func TestDNSTtlRRset(t *testing.T) {
s := newTestServerDNSSEC(t, false)
defer s.Stop()
ttl := uint32(60)
for _, serv := range services {
addService(t, s, serv.Key, uint64(ttl), serv)
defer delService(t, s, serv.Key)
ttl += 60
}
c := new(dns.Client)
tc := dnsTestCases[9]
t.Logf("%v\n", tc)
m := new(dns.Msg)
m.SetQuestion(tc.Qname, tc.Qtype)
if tc.dnssec == true {
m.SetEdns0(4096, true)
}
resp, _, err := c.Exchange(m, "127.0.0.1:"+StrPort)
if err != nil {
t.Fatalf("failing: %s: %s\n", m.String(), err.Error())
}
t.Logf("%s\n", resp)
ttl = 360
for i, a := range resp.Answer {
if a.Header().Ttl != ttl {
t.Errorf("Answer %d should have a Header TTL of %d, but has %d", i, ttl, a.Header().Ttl)
}
}
}
示例3: assertExchange
// perform a DNS query and assert the reply code, number or answers, etc
func assertExchange(t *testing.T, z string, ty uint16, minAnswers int, maxAnswers int, expErr int) *dns.Msg {
c := new(dns.Client)
c.UDPSize = testUDPBufSize
m := new(dns.Msg)
m.RecursionDesired = true
m.SetQuestion(z, ty)
m.SetEdns0(testUDPBufSize, false) // we don't want to play with truncation here...
r, _, err := c.Exchange(m, fmt.Sprintf("127.0.0.1:%d", testPort))
t.Logf("Response:\n%+v\n", r)
wt.AssertNoErr(t, err)
if minAnswers == 0 && maxAnswers == 0 {
wt.AssertStatus(t, r.Rcode, expErr, "DNS response code")
} else {
wt.AssertStatus(t, r.Rcode, dns.RcodeSuccess, "DNS response code")
}
answers := len(r.Answer)
if minAnswers >= 0 && answers < minAnswers {
wt.Fatalf(t, "Number of answers >= %d", minAnswers)
}
if maxAnswers >= 0 && answers > maxAnswers {
wt.Fatalf(t, "Number of answers <= %d", maxAnswers)
}
return r
}
示例4: TestInFlightEDns0
func TestInFlightEDns0(t *T) {
m1 := new(dns.Msg)
m1.SetQuestion(testAnyDomain, dns.TypeA)
m1.SetEdns0(4096, false)
w1 := getWriter()
m2 := new(dns.Msg)
m2.SetQuestion(testAnyDomain, dns.TypeA)
w2 := getWriter()
go func() {
handleRequest(w1, m1)
}()
go func() {
handleRequest(w2, m2)
}()
var r1 *dns.Msg
var r2 *dns.Msg
for r1 == nil || r2 == nil {
select {
case r1 = <-w1.ReplyCh:
case r2 = <-w2.ReplyCh:
}
}
//note: this test could be flaky since we're relying on google to return
//edns0 response when we send one vs when we don't send one
assert.NotNil(t, r1.IsEdns0())
assert.Nil(t, r2.IsEdns0())
}
示例5: assertExchange
// Perform a DNS query and assert the reply code, number or answers, etc
func assertExchange(t *testing.T, z string, ty uint16, port int, minAnswers int, maxAnswers int, expErr int) (*dns.Msg, *dns.Msg) {
require.NotEqual(t, 0, port, "invalid DNS server port")
c := &dns.Client{
UDPSize: testUDPBufSize,
}
m := new(dns.Msg)
m.RecursionDesired = true
m.SetQuestion(z, ty)
m.SetEdns0(testUDPBufSize, false) // we don't want to play with truncation here...
lstAddr := fmt.Sprintf("127.0.0.1:%d", port)
r, _, err := c.Exchange(m, lstAddr)
t.Logf("Response from '%s':\n%+v\n", lstAddr, r)
if err != nil {
t.Errorf("Error when querying DNS server at %s: %s", lstAddr, err)
}
require.NoError(t, err)
if minAnswers == 0 && maxAnswers == 0 {
require.Equal(t, expErr, r.Rcode, "DNS response code")
} else {
require.Equal(t, dns.RcodeSuccess, r.Rcode, "DNS response code")
}
answers := len(r.Answer)
if minAnswers >= 0 && answers < minAnswers {
require.FailNow(t, fmt.Sprintf("Number of answers >= %d", minAnswers))
}
if maxAnswers >= 0 && answers > maxAnswers {
require.FailNow(t, fmt.Sprintf("Number of answers <= %d", maxAnswers))
}
return m, r
}
示例6: dnsQuery
// dnsQuery will query a nameserver, iterating through the supplied servers as it retries
// The nameserver should include a port, to facilitate testing where we talk to a mock dns server.
func dnsQuery(fqdn string, rtype uint16, nameservers []string, recursive bool) (in *dns.Msg, err error) {
m := new(dns.Msg)
m.SetQuestion(fqdn, rtype)
m.SetEdns0(4096, false)
if !recursive {
m.RecursionDesired = false
}
// Will retry the request based on the number of servers (n+1)
for i := 1; i <= len(nameservers)+1; i++ {
ns := nameservers[i%len(nameservers)]
udp := &dns.Client{Net: "udp", Timeout: DNSTimeout}
in, _, err = udp.Exchange(m, ns)
if err == dns.ErrTruncated {
tcp := &dns.Client{Net: "tcp", Timeout: DNSTimeout}
// If the TCP request suceeds, the err will reset to nil
in, _, err = tcp.Exchange(m, ns)
}
if err == nil {
break
}
}
return
}
示例7: SendQuery
// Send a DNS query via UDP, configured by a Request object. If successful,
// stores response details in Result object, otherwise, returns Result object
// with an error string.
func SendQuery(request *Request) (result Result, err error) {
log.Printf("Sending query: %s", request)
result.Request = *request
record_type, ok := dns.StringToType[request.RecordType]
if !ok {
result.Error = fmt.Sprintf("Invalid type: %s", request.RecordType)
return result, errors.New(result.Error)
}
m := new(dns.Msg)
if request.VerifySignature == true {
log.Printf("SetEdns0 for %s", request.RecordName)
m.SetEdns0(4096, true)
}
m.SetQuestion(request.RecordName, record_type)
c := new(dns.Client)
in, rtt, err := c.Exchange(m, request.Destination)
// log.Printf("Answer: %s [%d] %s", in, rtt, err)
result.Duration = rtt
if err != nil {
result.Error = err.Error()
} else {
for _, rr := range in.Answer {
answer := Answer{
Ttl: rr.Header().Ttl,
Name: rr.Header().Name,
String: rr.String(),
}
result.Answers = append(result.Answers, answer)
}
}
return result, nil
}
示例8: searchServerIP
func searchServerIP(domain string, version int, DNSservers []string) (answer *dns.Msg, err error) {
DNSserver := DNSservers[rand.Intn(len(DNSservers))]
for i := 1; i <= 3; i++ {
if DNSserver == "" {
DNSserver = DNSservers[rand.Intn(len(DNSservers))]
}
}
if DNSserver == "" {
return nil, errors.New("DNSserver is an empty string")
}
dnsRequest := new(dns.Msg)
if dnsRequest == nil {
return nil, errors.New("Can not new dnsRequest")
}
dnsClient := new(dns.Client)
if dnsClient == nil {
return nil, errors.New("Can not new dnsClient")
}
if version == 4 {
dnsRequest.SetQuestion(domain+".", dns.TypeA)
} else if version == 6 {
dnsRequest.SetQuestion(domain+".", dns.TypeAAAA)
} else {
return nil, errors.New("wrong parameter in version")
}
dnsRequest.SetEdns0(4096, true)
answer, _, err = dnsClient.Exchange(dnsRequest, DNSserver)
if err != nil {
return nil, err
}
return answer, nil
}
示例9: TestDNSExpire
func TestDNSExpire(t *testing.T) {
s := newTestServerDNSSEC(t)
defer s.Stop()
serv := services[0]
addService(t, s, serv.key, 1, &Service{Host: serv.Host, Port: serv.Port})
// defer delService(t, s, serv.key) // It will delete itself...magically
c := new(dns.Client)
tc := dnsTestCases[0]
m := new(dns.Msg)
m.SetQuestion(tc.Qname, tc.Qtype)
if tc.dnssec == true {
m.SetEdns0(4096, true)
}
resp, _, err := c.Exchange(m, "127.0.0.1:"+StrPort)
if err != nil {
t.Fatalf("failing: %s: %s\n", m.String(), err.Error())
}
if resp.Rcode != dns.RcodeSuccess {
t.Logf("%v\n", resp)
t.Fail()
}
// Sleep to let it expire.
time.Sleep(2 * time.Second)
resp, _, err = c.Exchange(m, "127.0.0.1:"+StrPort)
if err != nil {
t.Errorf("failing: %s\n", err.Error())
}
if resp.Rcode != dns.RcodeNameError {
t.Logf("%v\n", resp)
t.Fail()
}
}
示例10: 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
}
示例11: exchangeOne
// exchangeOne performs a single DNS exchange with a randomly chosen server
// out of the server list, returning the response, time, and error (if any).
// This method sets the DNSSEC OK bit on the message to true before sending
// it to the resolver in case validation isn't the resolvers default behaviour.
func (dnsResolver *DNSResolverImpl) exchangeOne(ctx context.Context, hostname string, qtype uint16, msgStats metrics.Scope) (*dns.Msg, error) {
m := new(dns.Msg)
// Set question type
m.SetQuestion(dns.Fqdn(hostname), qtype)
// Set DNSSEC OK bit for resolver
m.SetEdns0(4096, true)
if len(dnsResolver.servers) < 1 {
return nil, fmt.Errorf("Not configured with at least one DNS Server")
}
dnsResolver.stats.Inc("Rate", 1)
// Randomly pick a server
chosenServer := dnsResolver.servers[rand.Intn(len(dnsResolver.servers))]
client := dnsResolver.dnsClient
tries := 1
start := dnsResolver.clk.Now()
msgStats.Inc("Calls", 1)
defer func() {
msgStats.TimingDuration("Latency", dnsResolver.clk.Now().Sub(start))
}()
for {
msgStats.Inc("Tries", 1)
ch := make(chan dnsResp, 1)
go func() {
rsp, rtt, err := client.Exchange(m, chosenServer)
msgStats.TimingDuration("SingleTryLatency", rtt)
ch <- dnsResp{m: rsp, err: err}
}()
select {
case <-ctx.Done():
msgStats.Inc("Cancels", 1)
msgStats.Inc("Errors", 1)
return nil, ctx.Err()
case r := <-ch:
if r.err != nil {
msgStats.Inc("Errors", 1)
operr, ok := r.err.(*net.OpError)
isRetryable := ok && operr.Temporary()
hasRetriesLeft := tries < dnsResolver.maxTries
if isRetryable && hasRetriesLeft {
tries++
continue
} else if isRetryable && !hasRetriesLeft {
msgStats.Inc("RanOutOfTries", 1)
}
} else {
msgStats.Inc("Successes", 1)
}
return r.m, r.err
}
}
}
示例12: ednsFromRequest
// Create skeleton edns opt RR from the query and
// add it to the message m
func ednsFromRequest(req, m *dns.Msg) {
for _, r := range req.Extra {
if r.Header().Rrtype == dns.TypeOPT {
m.SetEdns0(4096, r.(*dns.OPT).Do())
return
}
}
return
}
示例13: refuseWithCode
func (self *TrivialDnsServer) refuseWithCode(w dns.ResponseWriter, req *dns.Msg, code int) {
m := new(dns.Msg)
for _, r := range req.Extra {
if r.Header().Rrtype == dns.TypeOPT {
m.SetEdns0(4096, r.(*dns.OPT).Do())
}
}
m.SetRcode(req, code)
w.WriteMsg(m)
}
示例14: testLookupSRV
func testLookupSRV(t *testing.T) {
if !checkKubernetesRunning() {
t.Skip("Skipping Kubernetes Integration tests. Kubernetes is not running")
}
// Note: Use different port to avoid conflict with servers used in other tests.
coreFile :=
`.:2054 {
kubernetes coredns.local {
endpoint http://localhost:8080
namespaces demo
}
`
server, _, udp, err := Server(t, coreFile)
if err != nil {
t.Fatal("Could not get server: %s", err)
}
defer server.Stop()
log.SetOutput(ioutil.Discard)
// TODO: Add checks for A records in additional section
for _, testData := range testdataLookupSRV {
t.Logf("[log] Testing query string: '%v'\n", testData.Query)
dnsClient := new(dns.Client)
dnsMessage := new(dns.Msg)
dnsMessage.SetQuestion(testData.Query, dns.TypeSRV)
dnsMessage.SetEdns0(4096, true)
res, _, err := dnsClient.Exchange(dnsMessage, udp)
if err != nil {
t.Fatal("Could not send query: %s", err)
}
// Count SRV records in the answer section
srvRecordCount := 0
for _, a := range res.Answer {
fmt.Printf("RR: %v\n", a)
if a.Header().Rrtype == dns.TypeSRV {
srvRecordCount++
}
}
if srvRecordCount != testData.SRVRecordCount {
t.Errorf("Expected '%v' SRV records in response. Instead got '%v' SRV records. Test query string: '%v'", testData.SRVRecordCount, srvRecordCount, testData.Query)
}
if len(res.Answer) != testData.TotalAnswerCount {
t.Errorf("Expected '%v' records in answer section. Instead got '%v' records in answer section. Test query string: '%v'", testData.TotalAnswerCount, len(res.Answer), testData.Query)
}
}
}
示例15: resolve
func resolve(req *dns.Msg, dnssec bool) (*dns.Msg, error) {
extra2 := []dns.RR{}
for _, extra := range req.Extra {
if extra.Header().Rrtype != dns.TypeOPT {
extra2 = append(extra2, extra)
}
}
req.Extra = extra2
req.SetEdns0(dns.DefaultMsgSize, dnssec)
resolved, _, err := resolveViaResolverThreads(req)
if err != nil {
return nil, err
}
resolved.Compress = true
return resolved, nil
}