当前位置: 首页>>代码示例>>Golang>>正文


Golang Msg.Unpack方法代码示例

本文整理汇总了Golang中github.com/miekg/dns.Msg.Unpack方法的典型用法代码示例。如果您正苦于以下问题:Golang Msg.Unpack方法的具体用法?Golang Msg.Unpack怎么用?Golang Msg.Unpack使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在github.com/miekg/dns.Msg的用法示例。


在下文中一共展示了Msg.Unpack方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: recv

// recv is used to receive until we get a shutdown
func (c *client) recv(l *net.UDPConn, msgCh chan *dns.Msg) {
	if l == nil {
		return
	}
	buf := make([]byte, 65536)

	var closed bool
	for {
		c.closeLock.RLock()
		closed = c.closed
		c.closeLock.RUnlock()
		if closed {
			break
		}
		n, err := l.Read(buf)
		if err != nil {
			Log.Printf("[ERR] mdns: Failed to read packet: %v", err)
			continue
		}
		msg := new(dns.Msg)
		if err := msg.Unpack(buf[:n]); err != nil {
			Log.Printf("[ERR] mdns: Failed to unpack packet: %v", err)
			continue
		}
		select {
		case msgCh <- msg:
		case <-c.closedCh:
			return
		}
	}
}
开发者ID:Gaboose,项目名称:mdns,代码行数:32,代码来源:client.go

示例2: ParseDnsFrame

func ParseDnsFrame(f Frame, req *dns.Msg) (addrs []net.IP, err error) {
	ft, ok := f.(*FrameDns)
	if !ok {
		return nil, ErrDnsMsgIllegal
	}

	res := new(dns.Msg)
	err = res.Unpack(ft.Data)
	if err != nil || !res.Response || res.Id != req.Id {
		return nil, ErrDnsMsgIllegal
	}

	if DEBUGDNS {
		DebugDNS(res, req.Question[0].Name)
	}
	for _, a := range res.Answer {
		switch ta := a.(type) {
		case *dns.A:
			addrs = append(addrs, ta.A)
		case *dns.AAAA:
			addrs = append(addrs, ta.AAAA)
		}
	}
	return
}
开发者ID:xyz12810,项目名称:goproxy-1,代码行数:25,代码来源:session.go

示例3: 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())
}
开发者ID:RunxiaWan,项目名称:oganization,代码行数:32,代码来源:main_test.go

示例4: parsePacket

// parsePacket is used to parse an incoming packet
func (s *Server) parsePacket(packet []byte, from net.Addr) error {
	var msg dns.Msg
	if err := msg.Unpack(packet); err != nil {
		log.Printf("[ERR] bonjour: Failed to unpack packet: %v", err)
		return err
	}
	return s.handleQuery(&msg, from)
}
开发者ID:edc1591,项目名称:gohome,代码行数:9,代码来源:server.go

示例5: dnsQueryRaw

func dnsQueryRaw(r []byte) ([]byte, error) {
	req := new(dns.Msg)
	req.Unpack(r)
	res, err := dnsQuery(req)
	if nil != err {
		return nil, err
	}
	return res.Pack()
}
开发者ID:yinqiwen,项目名称:gsnova,代码行数:9,代码来源:dns.go

示例6: safeParse

func safeParse(msg *dns.Msg, data []byte) (err error) {
	defer func() {
		if e := recover(); e != nil {
			fmt.Fprintf(os.Stderr, "Crashed dns: %v\nError: %v\n",
				hex.EncodeToString(data), e)
			err = fmt.Errorf("bad packet")
		}
	}()
	return msg.Unpack(data)
}
开发者ID:wheelcomplex,项目名称:golibs,代码行数:10,代码来源:readpcap.go

示例7: 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)
	}
}
开发者ID:saltsa,项目名称:lego,代码行数:54,代码来源:dns_challenge_rfc2136_test.go

示例8: safeParse

func safeParse(msg *dns.Msg, data []byte) (err error) {
	defer func() {
		if e := recover(); e != nil {
			hexs := hex.EncodeToString(data)
			fmt.Fprintf(os.Stderr, "Crashed dns: %v\nError: %v\n",
				hexs, e)
			panic("Unpacking dns crashed: " + hexs)
		}
	}()
	return msg.Unpack(data)
}
开发者ID:wheelcomplex,项目名称:golibs,代码行数:11,代码来源:topdns.go

示例9: readMessage

// consume an mdns packet from the wire and decode it
func (c *connector) readMessage() (*dns.Msg, *net.UDPAddr, error) {
	buf := make([]byte, 1500)
	read, addr, err := c.ReadFromUDP(buf)
	if err != nil {
		return nil, nil, err
	}
	var msg dns.Msg
	if err := msg.Unpack(buf[:read]); err != nil {
		return nil, nil, err
	}
	return &msg, addr, nil
}
开发者ID:davecheney,项目名称:mdns,代码行数:13,代码来源:mdns.go

示例10: ReadDNSFromUDP

func (conn *DNSUDPConn) ReadDNSFromUDP() (*dns.Msg, *net.UDPAddr, error) {
	buf := make([]byte, DNS_MAX_UDP_SIZE)
	rdlen, addr, err := conn.ReadFromUDP(buf)
	if rdlen == 0 || err != nil {
		return nil, nil, fmt.Errorf("Error reading from UDP: %v", err)
	}
	m := new(dns.Msg)
	if err = m.Unpack(buf[:rdlen]); err != nil {
		return nil, nil, err
	}
	return m, addr, nil
}
开发者ID:blahgeek,项目名称:justvpn,代码行数:12,代码来源:dns_common.go

示例11: readloop

// A loop that reads incoming mDNS questions from UDP connection
// and passes them into the main loop question channel
func (m *mdns) readloop(c *net.UDPConn, in chan msg) {
	buf := make([]byte, 1500)
	for {
		if n, addr, err := c.ReadFromUDP(buf); err != nil {
			return
		} else {
			var dnsmsg dns.Msg
			if err := dnsmsg.Unpack(buf[:n]); err == nil {
				in <- msg{&dnsmsg, addr, c}
			}
		}
	}
}
开发者ID:zserge,项目名称:mdns,代码行数:15,代码来源:mdns.go

示例12: readUDP

func (c *Client) readUDP() (*net.UDPAddr, *dns.Msg, error) {
	in := make([]byte, dns.DefaultMsgSize)
	read, addr, err := c.conn.ReadFromUDP(in)
	if err != nil {
		return nil, nil, err
	}

	var readMsg dns.Msg
	if err := readMsg.Unpack(in[:read]); err != nil {
		return nil, nil, err
	}

	return addr, &readMsg, nil
}
开发者ID:soh335,项目名称:go-mdns,代码行数:14,代码来源:main.go

示例13: 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)
	}
}
开发者ID:adriencarbonne,项目名称:lego,代码行数:48,代码来源:rfc2136_test.go

示例14: Get

func (this *cache) Get(message *dns.Msg) (bool, *dns.Msg, error) {
	cachedMessage := CacheRecord{}
	err := this.collection.GetObject(message.Question[0].Name, &cachedMessage)
	if err != nil {
		return false, nil, err
	}

	cachedMessageMsg := dns.Msg{}
	err = cachedMessageMsg.Unpack(cachedMessage.Record)
	if err != nil {
		return false, nil, err
	}

	if cachedMessageMsg.Id != 0 && cachedMessage.Expiry.After(time.Now()) {
		return true, &cachedMessageMsg, nil
	} else {
		return false, &dns.Msg{}, nil
	}
}
开发者ID:d2g,项目名称:cloudpathway,代码行数:19,代码来源:cache.go

示例15: 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)
}
开发者ID:yueyingjuesha,项目名称:v2ray-core,代码行数:43,代码来源:nameserver.go


注:本文中的github.com/miekg/dns.Msg.Unpack方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。