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


Golang Msg.Pack方法代码示例

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


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

示例1: SendQuery

// Async
func (c *MDNSClient) SendQuery(name string, querytype uint16, insistent bool, responseCh chan<- *Response) {
	c.actionChan <- func() {
		query, found := c.inflight[name]
		if !found {
			m := new(dns.Msg)
			m.SetQuestion(name, querytype)
			m.RecursionDesired = false
			buf, err := m.Pack()
			if err != nil {
				responseCh <- &Response{err: err}
				close(responseCh)
				return
			}
			query = &inflightQuery{
				name: name,
				id:   m.Id,
			}
			if _, err = c.conn.WriteTo(buf, c.addr); err != nil {
				responseCh <- &Response{err: err}
				close(responseCh)
				return
			}
			c.inflight[name] = query
		}
		info := &responseInfo{
			ch:        responseCh,
			timeout:   time.Now().Add(mDNSTimeout),
			insistent: insistent,
		}
		// Invariant on responseInfos: they are in ascending order of
		// timeout.  Since we use a fixed interval from Now(), this
		// must be after all existing timeouts.
		query.responseInfos = append(query.responseInfos, info)
	}
}
开发者ID:grkvlt,项目名称:weave,代码行数:36,代码来源:mdns_client.go

示例2: Insert

func (c *Cache) Insert(d *dns.Msg) {
	if *flaglog {
		log.Printf("fsk-shield: inserting " + toRadixKey(d))
	}
	buf, _ := d.Pack() // Should always work
	c.Radix.Insert(toRadixKey(d), &Packet{d: buf[2:], ttl: time.Now().UTC()})
}
开发者ID:valm0unt,项目名称:fks,代码行数:7,代码来源:cache.go

示例3: Add

func (this *cache) Add(message *dns.Msg) error {
	if message.Question[0].Qtype == dns.TypeA && message.Question[0].Qclass == dns.ClassINET {
		byteMessage, err := message.Pack()
		if err != nil {
			return err
		}

		err = this.collection.SetObject(message.Question[0].Name, CacheRecord{Expiry: time.Now().Add(24 * time.Hour), Record: byteMessage})
		for _, part := range message.Answer {
			switch part.(type) {
			case *dns.A:
				cacheErr := this.ipToHostname.SetObject(part.(*dns.A).A.String(), strings.TrimSuffix(message.Question[0].Name, "."))
				if cacheErr != nil {
					log.Println("Warning: Error Adding/Updating Cache IP:" + part.(*dns.A).A.String() + " Hostname:" + strings.TrimSuffix(message.Question[0].Name, ".") + " Error:" + cacheErr.Error())
				}
			case *dns.CNAME:
				//CNAME Don't contain the IP for reverse lookups.
				//log.Printf("CNAME: Type:%s Value:%v", reflect.TypeOf(part).Name(), part)
				//TODO: We should probably Add the CNAME as a hostname??
			default:
				log.Printf("Type:%s Value:%v", reflect.TypeOf(part).Name(), part)
			}
		}
		return err
	}
	return nil
}
开发者ID:d2g,项目名称:cloudpathway,代码行数:27,代码来源:cache.go

示例4: Discover

func (c *Client) Discover(domain string, cb func(*dns.Msg)) {
	m := new(dns.Msg)
	m.SetQuestion(dns.Fqdn(domain), dns.TypePTR)
	m.RecursionDesired = true

	addr := &net.UDPAddr{
		IP:   net.ParseIP("224.0.0.251"),
		Port: 5353,
	}

	conn, err := net.ListenMulticastUDP("udp4", nil, addr)

	if err != nil {
		panic(err)
	}

	defer conn.Close()
	c.conn = conn

	out, err := m.Pack()

	if err != nil {
		panic(err)
	}

	_, err = conn.WriteToUDP(out, addr)
	if err != nil {
		panic(err)
	}

	c.handleReceiveMsg(domain, cb)
}
开发者ID:soh335,项目名称:go-mdns,代码行数:32,代码来源:main.go

示例5: sendResponse

func (s *MDNSServer) sendResponse(m *dns.Msg) error {
	buf, err := m.Pack()
	if err != nil {
		return err
	}
	_, err = s.sendconn.WriteTo(buf, ipv4Addr)
	return err
}
开发者ID:rahulxkrishna,项目名称:weave,代码行数:8,代码来源:mdns_server.go

示例6: WriteDNSToUDP

func (conn *DNSUDPConn) WriteDNSToUDP(m *dns.Msg, addr *net.UDPAddr) error {
	out, err := m.Pack()
	if err != nil {
		return err
	}
	_, err = conn.WriteToUDP(out, addr)
	return err
}
开发者ID:blahgeek,项目名称:justvpn,代码行数:8,代码来源:dns_common.go

示例7: writeMessage

// encode an mdns msg and broadcast it on the wire
func (c *connector) writeMessage(msg *dns.Msg, addr *net.UDPAddr) error {
	buf, err := msg.Pack()
	if err != nil {
		return err
	}
	_, err = c.WriteToUDP(buf, addr)
	return err
}
开发者ID:davecheney,项目名称:mdns,代码行数:9,代码来源:mdns.go

示例8: CompressIfLarge

func CompressIfLarge(m *dns.Msg) {
	bytes, err := m.Pack()
	if err != nil {
		return
	}
	if len(bytes) > 512 { // may not fit into UDP packet
		m.Compress = true // will be compressed in WriteMsg
	}
}
开发者ID:a-kr,项目名称:trivialdns,代码行数:9,代码来源:trivialdns.go

示例9: 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

示例10: sendQuery

// sendQuery is used to multicast a query out
func (c *client) sendQuery(q *dns.Msg) error {
	buf, err := q.Pack()
	if err != nil {
		return err
	}
	if c.ipv4List != nil {
		c.ipv4List.WriteTo(buf, ipv4Addr)
	}
	if c.ipv6List != nil {
		c.ipv6List.WriteTo(buf, ipv6Addr)
	}
	return nil
}
开发者ID:KensakuKOMATSU,项目名称:mdns,代码行数:14,代码来源:client.go

示例11: sendQuery

// Pack the dns.Msg and write to available connections (multicast)
func (c *client) sendQuery(msg *dns.Msg) error {
	buf, err := msg.Pack()
	if err != nil {
		return err
	}
	if c.ipv4conn != nil {
		c.ipv4conn.WriteTo(buf, ipv4Addr)
	}
	if c.ipv6conn != nil {
		c.ipv6conn.WriteTo(buf, ipv6Addr)
	}
	return nil
}
开发者ID:brutella,项目名称:hklifx,代码行数:14,代码来源:client.go

示例12: sendQuery

// sendQuery is used to multicast a query out
func (c *client) sendQuery(q *dns.Msg, params *QueryParam) error {
	buf, err := q.Pack()
	if err != nil {
		return err
	}
	if c.ipv4List != nil {
		c.ipv4List.WriteTo(buf, params.IPv4mdns)
	}
	if c.ipv6List != nil {
		c.ipv6List.WriteTo(buf, params.IPv6mdns)
	}
	return nil
}
开发者ID:richtr,项目名称:mdns,代码行数:14,代码来源:client.go

示例13: sendQuery

// sendQuery is used to multicast a query out
func (c *client) sendQuery(q *dns.Msg) error {
	buf, err := q.Pack()
	if err != nil {
		return err
	}
	if c.ipv4UnicastConn != nil {
		c.ipv4UnicastConn.WriteToUDP(buf, ipv4Addr)
	}
	if c.ipv6UnicastConn != nil {
		c.ipv6UnicastConn.WriteToUDP(buf, ipv6Addr)
	}
	return nil
}
开发者ID:dgageot,项目名称:docker-machine-dns,代码行数:14,代码来源:client.go

示例14: multicastResponse

// multicastResponse us used to send a multicast response packet
func (c *Server) multicastResponse(msg *dns.Msg) error {
	buf, err := msg.Pack()
	if err != nil {
		log.Println("Failed to pack message!")
		return err
	}
	if c.ipv4conn != nil {
		c.ipv4conn.WriteTo(buf, ipv4Addr)
	}
	if c.ipv6conn != nil {
		c.ipv6conn.WriteTo(buf, ipv6Addr)
	}
	return nil
}
开发者ID:edc1591,项目名称:gohome,代码行数:15,代码来源:server.go

示例15: unicastResponse

// unicastResponse is used to send a unicast response packet
func (s *Server) unicastResponse(resp *dns.Msg, from net.Addr) error {
	buf, err := resp.Pack()
	if err != nil {
		return err
	}
	addr := from.(*net.UDPAddr)
	if addr.IP.To4() != nil {
		_, err = s.ipv4conn.WriteToUDP(buf, addr)
		return err
	} else {
		_, err = s.ipv6conn.WriteToUDP(buf, addr)
		return err
	}
}
开发者ID:edc1591,项目名称:gohome,代码行数:15,代码来源:server.go


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