本文整理汇总了Golang中github.com/miekg/dns.Msg.String方法的典型用法代码示例。如果您正苦于以下问题:Golang Msg.String方法的具体用法?Golang Msg.String怎么用?Golang Msg.String使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/miekg/dns.Msg
的用法示例。
在下文中一共展示了Msg.String方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestTCP
func TestTCP(t *testing.T) {
handle, err := pcap.OpenOffline("tcptest.pcap")
if err != nil {
panic(err)
}
packetSource := gopacket.NewPacketSource(handle, handle.LinkType())
defer handle.Close()
tcpPack := make(chan gopacket.Packet, 10)
nomalPack := make(chan gopacket.Packet, 5)
for input_pack := range packetSource.Packets() { // send tcp package for channel
tcpLayer := input_pack.Layer(layers.LayerTypeTCP)
if tcpLayer != nil {
tcpPack <- input_pack
// send packet to tcp ASSEMBLER
}
}
streamFactory := &DNSStreamFactory{normal: nomalPack}
streamPool := tcpassembly.NewStreamPool(streamFactory)
assembler := tcpassembly.NewAssembler(streamPool)
go tcpAssemble(tcpPack, assembler)
pack := <-nomalPack
udpLayer := pack.Layer(layers.LayerTypeUDP)
if udpLayer == nil {
t.Errorf("can not fine udp Layer in result")
}
dns_message := new(dns.Msg)
err = dns_message.Unpack(udpLayer.LayerPayload())
if err != nil {
t.Errorf("can not parse dns message")
}
fmt.Printf(dns_message.String())
}
示例2: setNS
func setNS(rrs []string, r *dns.Msg, qns string) (string, string) {
if debug {
log.Printf("RRS %v,DNS Message %v Qns %v\n", rrs, r.String(), qns)
}
var ns string
typ := "A"
if strings.Contains(rrs[4], rrs[0]) || strings.Compare(qns, root) == 0 || fool {
for _, rr := range r.Extra {
rrss := splitRR(rr)
if strings.Compare(rrss[3], "AAAA") == 0 {
continue
}
if strings.Compare(rrss[0], rrs[4]) == 0 {
ns = rrss[4]
typ = rrss[3]
break
}
}
} else {
ns = rrs[4]
}
if strings.Compare(ns, "") == 0 {
ns = rrs[4]
}
return ns, typ
}
示例3: 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)
}
}
}
示例4: 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)
}
}
}
示例5: 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()
}
}
示例6: ServeDNS
// ServeDNS implements the handler interface
func (s *Server) ServeDNS(w mdns.ResponseWriter, r *mdns.Msg) {
defer trace.End(trace.Begin(r.String()))
if r == nil || len(r.Question) == 0 {
return
}
// Reject multi-question query
if len(r.Question) != 1 {
log.Errorf("Rejected multi-question query")
respServerFailure(w, r)
return
}
q := r.Question[0]
// Reject non-INET type query
if q.Qclass != mdns.ClassINET {
log.Errorf("Rejected non-inet query")
respNotImplemented(w, r)
return
}
// Reject ANY type query
if q.Qtype == mdns.TypeANY {
log.Errorf("Rejected ANY query")
respNotImplemented(w, r)
return
}
// Do we have the response in our cache?
ok, err := s.SeenBefore(w, r)
if ok {
if err != nil {
log.Errorf("SeenBefore returned: %q", err)
}
return
}
// Check VIC first
ok, err = s.HandleVIC(w, r)
if ok {
if err != nil {
log.Errorf("HandleVIC returned: %q", err)
}
return
}
// Then forward
ok, err = s.HandleForwarding(w, r)
if ok {
if err != nil {
log.Errorf("HandleForwarding returned: %q", err)
}
return
}
}
示例7: dorequest
func dorequest(c *C, msg *dns.Msg) *dns.Msg {
cli := new(dns.Client)
// cli.ReadTimeout = 2 * time.Second
r, _, err := cli.Exchange(msg, "127.0.0.1"+PORT)
if err != nil {
c.Logf("request err '%s': %s", msg.String(), err)
c.Fail()
}
return r
}
示例8: TestRFC2136ValidUpdatePacket
func TestRFC2136ValidUpdatePacket(t *testing.T) {
dns.HandleFunc(rfc2136TestZone, serverHandlerPassBackRequest)
defer dns.HandleRemove(rfc2136TestZone)
server, addrstr, err := runLocalDNSTestServer("127.0.0.1:0", false)
if err != nil {
t.Fatalf("Failed to start test server: %v", err)
}
defer server.Shutdown()
rr := new(dns.TXT)
rr.Hdr = dns.RR_Header{
Name: rfc2136TestFqdn,
Rrtype: dns.TypeTXT,
Class: dns.ClassINET,
Ttl: uint32(rfc2136TestTTL),
}
rr.Txt = []string{rfc2136TestValue}
rrs := make([]dns.RR, 1)
rrs[0] = rr
m := new(dns.Msg)
m.SetUpdate(dns.Fqdn(rfc2136TestZone))
m.Insert(rrs)
expectstr := m.String()
expect, err := m.Pack()
if err != nil {
t.Fatalf("Error packing expect msg: %v", err)
}
provider, err := NewDNSProviderRFC2136(addrstr, rfc2136TestZone, "", "")
if err != nil {
t.Fatalf("Expected NewDNSProviderRFC2136() to return no error but the error was -> %v", err)
}
if err := provider.Present(rfc2136TestDomain, "", "1234d=="); err != nil {
t.Errorf("Expected Present() to return no error but the error was -> %v", err)
}
rcvMsg := <-reqChan
rcvMsg.Id = m.Id
actual, err := rcvMsg.Pack()
if err != nil {
t.Fatalf("Error packing actual msg: %v", err)
}
if !bytes.Equal(actual, expect) {
tmp := new(dns.Msg)
if err := tmp.Unpack(actual); err != nil {
t.Fatalf("Error unpacking actual msg: %v", err)
}
t.Errorf("Expected msg:\n%s", expectstr)
t.Errorf("Actual msg:\n%v", tmp)
}
}
示例9: HandleForwarding
// HandleForwarding forwards a request to the nameservers and returns the response
func (s *Server) HandleForwarding(w mdns.ResponseWriter, r *mdns.Msg) (bool, error) {
defer trace.End(trace.Begin(r.String()))
var m *mdns.Msg
var err error
var try int
if len(s.Nameservers) == 0 {
log.Errorf("No nameservers defined, can not forward")
return false, respServerFailure(w, r)
}
// which protocol are they talking
tcp := false
if _, ok := w.RemoteAddr().(*net.TCPAddr); ok {
tcp = true
}
// Use request ID for "random" nameserver selection.
nsid := int(r.Id) % len(s.Nameservers)
Redo:
nameserver := s.Nameservers[nsid]
if i := strings.Index(nameserver, ":"); i < 0 {
nameserver += ":53"
}
if tcp {
m, _, err = s.tcpclient.Exchange(r, nameserver)
} else {
m, _, err = s.udpclient.Exchange(r, nameserver)
}
if err != nil {
// Seen an error, this can only mean, "server not reached", try again but only if we have not exausted our nameservers.
if try < len(s.Nameservers) {
try++
nsid = (nsid + 1) % len(s.Nameservers)
goto Redo
}
log.Errorf("Failure to forward request: %q", err)
return false, respServerFailure(w, r)
}
// We have a response so cache it
s.cache.Add(m)
m.Compress = true
if err := w.WriteMsg(m); err != nil {
log.Errorf("Error writing response: %q", err)
return true, err
}
return true, nil
}
示例10: TestRFC2136ValidUpdatePacket
func TestRFC2136ValidUpdatePacket(t *testing.T) {
acme.ClearFqdnCache()
dns.HandleFunc(rfc2136TestZone, serverHandlerPassBackRequest)
defer dns.HandleRemove(rfc2136TestZone)
server, addrstr, err := runLocalDNSTestServer("127.0.0.1:0", false)
if err != nil {
t.Fatalf("Failed to start test server: %v", err)
}
defer server.Shutdown()
txtRR, _ := dns.NewRR(fmt.Sprintf("%s %d IN TXT %s", rfc2136TestFqdn, rfc2136TestTTL, rfc2136TestValue))
rrs := []dns.RR{txtRR}
m := new(dns.Msg)
m.SetUpdate(rfc2136TestZone)
m.RemoveRRset(rrs)
m.Insert(rrs)
expectstr := m.String()
expect, err := m.Pack()
if err != nil {
t.Fatalf("Error packing expect msg: %v", err)
}
provider, err := NewDNSProvider(addrstr, "", "", "")
if err != nil {
t.Fatalf("Expected NewDNSProvider() to return no error but the error was -> %v", err)
}
if err := provider.Present(rfc2136TestDomain, "", "1234d=="); err != nil {
t.Errorf("Expected Present() to return no error but the error was -> %v", err)
}
rcvMsg := <-reqChan
rcvMsg.Id = m.Id
actual, err := rcvMsg.Pack()
if err != nil {
t.Fatalf("Error packing actual msg: %v", err)
}
if !bytes.Equal(actual, expect) {
tmp := new(dns.Msg)
if err := tmp.Unpack(actual); err != nil {
t.Fatalf("Error unpacking actual msg: %v", err)
}
t.Errorf("Expected msg:\n%s", expectstr)
t.Errorf("Actual msg:\n%v", tmp)
}
}
示例11: HandleResponse
// @Private
func (this *UDPNameServer) HandleResponse(dest v2net.Destination, payload *alloc.Buffer) {
msg := new(dns.Msg)
err := msg.Unpack(payload.Value)
if err != nil {
log.Warning("DNS: Failed to parse DNS response: ", err)
return
}
record := &ARecord{
IPs: make([]net.IP, 0, 16),
}
id := msg.Id
ttl := DefaultTTL
log.Debug("DNS: Handling response for id ", id, " content: ", msg.String())
this.Lock()
request, found := this.requests[id]
if !found {
this.Unlock()
return
}
delete(this.requests, id)
this.Unlock()
for _, rr := range msg.Answer {
switch rr := rr.(type) {
case *dns.A:
record.IPs = append(record.IPs, rr.A)
if rr.Hdr.Ttl < ttl {
ttl = rr.Hdr.Ttl
}
case *dns.AAAA:
record.IPs = append(record.IPs, rr.AAAA)
if rr.Hdr.Ttl < ttl {
ttl = rr.Hdr.Ttl
}
}
}
record.Expire = time.Now().Add(time.Second * time.Duration(ttl))
request.response <- record
close(request.response)
}
示例12: SeenBefore
// SeenBefore returns the cached response
func (s *Server) SeenBefore(w mdns.ResponseWriter, r *mdns.Msg) (bool, error) {
defer trace.End(trace.Begin(r.String()))
// Do we have it in the cache
if m := s.cache.Get(r); m != nil {
log.Debugf("Cache hit for %q", r.String())
// Overwrite the ID with the request's ID
m.Id = r.Id
m.Compress = true
m.Truncated = false
if err := w.WriteMsg(m); err != nil {
log.Errorf("Error writing response: %q", err)
return true, err
}
return true, nil
}
return false, nil
}
示例13: handleRequest
func handleRequest(w dns.ResponseWriter, r *dns.Msg) {
var rr dns.RR
fmt.Println(r.Question[0].Name)
m := new(dns.Msg)
m.SetReply(r)
m.Compress = *compress
rrstr, err := getRRStr(r.Question[0])
if err {
m.SetRcode(r, dns.RcodeNameError)
} else {
rr, _ = dns.NewRR(rrstr)
m.Answer = append(m.Answer, rr)
if *printf {
fmt.Printf("%v\n", m.String())
}
}
w.WriteMsg(m)
}
示例14: updateAddressCache
// updateAddressCache updates the dns map of real names to ip addresses
// used to relay traffic to the intended destination.
func updateAddressCache(r *dns.Msg) {
u := r.Copy()
u, err := upstreamLookup(u)
if err != nil {
fmt.Println("Network error: Cannnot lookup upstream!", err.Error())
return
}
// find all A records, save the name and address, replace the address and ttl
names := []string{}
ip := ""
// grab all names (and IP from A record), for updating (sans dot at end)
for _, answer := range u.Answer {
names = append(names, answer.Header().Name)
if dns.TypeToString[answer.Header().Rrtype] == "A" {
found := addressRegex.FindString(answer.String())
if found != "" {
ip = found
} else {
panic(r.String())
}
}
}
if ip == "" {
fmt.Println("Cannot updated map, no IP found")
fmt.Println(u.String())
return
}
updateMutex.Lock()
// Update both with . at the end and without
for _, name := range names {
addressMap[strings.ToLower(name)] = ip
addressMap[strings.ToLower(name[:len(name)-1])] = ip
fmt.Println("Updated address map with ", name, ip)
}
updateMutex.Unlock()
}
示例15: handleRequest
func handleRequest(w dns.ResponseWriter, r *dns.Msg) {
r.Question[0].Qtype
m := new(dns.Msg)
fmt.Printf("get query %s", r.Question[0].Name)
fmt.Printf("Query: %s", r.String())
// remote request client ip...
m.SetReply(r)
m.Answer = []dns.RR{
&dns.CNAME{
Hdr: dns.RR_Header{Name: m.Question[0].Name, Rrtype: dns.TypeCNAME, Class: dns.ClassINET, Ttl: 120},
Target: "cname.domain.com.", // target must end with .!!!!!!
},
newCNAME("backend.in.skydns.test. IN CNAME ipaddr.skydns.test."),
&dns.A{
//Hdr:dns.RR_Header{Name: "cname.domain.com", Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: 120},
Hdr: dns.RR_Header{Name: m.Question[0].Name, Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: 120},
A: net.ParseIP("1.1.1.1"),
},
&dns.A{
//Hdr:dns.RR_Header{Name: "cname.domain.com", Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: 120},
Hdr: dns.RR_Header{Name: m.Question[0].Name, Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: 120},
A: net.ParseIP("1.1.1.1"),
},
}
/*m.Answer = []dns.RR{
newCNAME("backend.in.skydns.test. IN CNAME ipaddr.skydns.test."),
newA("ipaddr.skydns.test. IN A 172.16.1.1"),
newA("ipaddr.skydns.test. IN A 172.16.1.2"),
}*/
m.Ns = make([]dns.RR, 2)
m.Ns[0] = newSOA("qiniudns.com. 60 IN SOA ns3.dnsv5.com. enterprise3dnsadmin.dnspod.com. 1457923437 3600 180 1209600 180")
m.Extra = make([]dns.RR, 1)
m.Extra[0] = &dns.TXT{Hdr: dns.RR_Header{Name: m.Question[0].Name, Rrtype: dns.TypeTXT, Class: dns.ClassINET, Ttl: 0}, Txt: []string{"Hello example"}}
w.WriteMsg(m)
}