• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Golang ethernet.Frame类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中github.com/mdlayher/ethernet.Frame的典型用法代码示例。如果您正苦于以下问题:Golang Frame类的具体用法?Golang Frame怎么用?Golang Frame使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



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

示例1: ethernetFrame

func ethernetFrame(t *testing.T) []byte {
	f := new(ethernet.Frame)
	fb, err := f.MarshalFCS()
	if err != nil {
		t.Fatalf("failed to marshal Ethernet frame: %v", err)
	}

	return fb
}
开发者ID:mdlayher,项目名称:vxlan,代码行数:9,代码来源:vxlan_test.go


示例2: handlePacketIPv4

// Handles a IPv4 packet and sends it to it's destination
func (p *PeerToPeer) handlePacketIPv4(contents []byte, proto int) {
	Log(Trace, "Handling IPv4 Packet")
	f := new(ethernet.Frame)
	if err := f.UnmarshalBinary(contents); err != nil {
		Log(Error, "Failed to unmarshal IPv4 packet")
	}

	if f.EtherType != ethernet.EtherTypeIPv4 {
		return
	}
	msg := CreateNencP2PMessage(p.Crypter, contents, uint16(proto), 1, 1, 1)
	p.SendTo(f.Destination, msg)
}
开发者ID:subutai-io,项目名称:p2p,代码行数:14,代码来源:packet.go


示例3: parsePacket

func parsePacket(buf []byte) (*Packet, *ethernet.Frame, error) {
	f := new(ethernet.Frame)
	if err := f.UnmarshalBinary(buf); err != nil {
		return nil, nil, err
	}

	// Ignore frames which do not have ARP EtherType
	if f.EtherType != ethernet.EtherTypeARP {
		return nil, nil, errInvalidARPPacket
	}

	p := new(Packet)
	if err := p.UnmarshalBinary(f.Payload); err != nil {
		return nil, nil, err
	}
	return p, f, nil
}
开发者ID:xytis,项目名称:polyp,代码行数:17,代码来源:packet.go


示例4: testRequest

// testRequest performs a single AoE request using the input request
// header and returns a single AoE response.
func testRequest(t *testing.T, req *aoe.Header) *aoe.Header {
	recv, send, run, done := testAoEServer(t)
	defer done()

	reqb, err := req.MarshalBinary()
	if err != nil {
		t.Fatalf("failed to marshal request AoE header: %v", err)
	}

	reqf := &ethernet.Frame{
		Payload: reqb,
	}

	reqfb, err := reqf.MarshalBinary()
	if err != nil {
		t.Fatalf("failed to marshal request Ethernet frame: %v", err)
	}

	if _, err := send.Write(reqfb); err != nil {
		t.Fatalf("failed to write request to server: %v", err)
	}

	if err := run(); err != nil {
		t.Fatalf("failed to run server: %v", err)
	}

	b, err := ioutil.ReadAll(recv)
	if err != nil {
		t.Fatalf("failed to read server response: %v", err)
	}

	resf := new(ethernet.Frame)
	if err := resf.UnmarshalBinary(b); err != nil {
		t.Fatalf("failed to unmarshal response Ethernet frame: %v", err)
	}

	resh := new(aoe.Header)
	if err := resh.UnmarshalBinary(resf.Payload); err != nil {
		t.Fatalf("failed to unmarshal response AoE header: %v", err)
	}

	return resh
}
开发者ID:ConfusedReality,项目名称:server_distributed-storage_torus,代码行数:45,代码来源:aoe_test.go


示例5: UnmarshalBinary

// UnmarshalBinary allocates a byte slice and marshals a Frame into binary form.
//
// If a VNI value is invalid, ErrInvalidVNI will be returned.
func (f *Frame) UnmarshalBinary(b []byte) error {
	// Need at least VXLAN frame and empty Ethernet frame
	if len(b) < 18 {
		return io.ErrUnexpectedEOF
	}

	// I flag must be set to 1.
	if (b[0] >> 3) != 1 {
		return ErrInvalidFrame
	}

	f.VNI = VNI(binary.BigEndian.Uint32(b[3:]))

	ef := new(ethernet.Frame)
	if err := ef.UnmarshalFCS(b[8:]); err != nil {
		return err
	}
	f.Ethernet = ef

	return nil
}
开发者ID:mdlayher,项目名称:vxlan,代码行数:24,代码来源:vxlan.go


示例6: parseRequest

// parseRequest unmarshals a raw ethernet frame and an ARP packet into a Request.
func parseRequest(buf []byte) (*Request, error) {
	f := new(ethernet.Frame)
	if err := f.UnmarshalBinary(buf); err != nil {
		return nil, err
	}

	// Ignore frames which do not have ARP EtherType
	if f.EtherType != ethernet.EtherTypeARP {
		return nil, errInvalidARPPacket
	}

	p := new(Packet)
	if err := p.UnmarshalBinary(f.Payload); err != nil {
		return nil, err
	}

	return &Request{
		Operation:          p.Operation,
		SenderHardwareAddr: p.SenderHardwareAddr,
		SenderIP:           p.SenderIP,
		TargetHardwareAddr: p.TargetHardwareAddr,
		TargetIP:           p.TargetIP,
	}, nil
}
开发者ID:Tsingson1988,项目名称:arp,代码行数:25,代码来源:request.go


示例7: handlePacketARP

func (p *PeerToPeer) handlePacketARP(contents []byte, proto int) {
	// Prepare new ethernet frame and fill it with
	// contents of the packet
	f := new(ethernet.Frame)
	if err := f.UnmarshalBinary(contents); err != nil {
		Log(Error, "Failed to Unmarshal ARP Binary")
		return
	}

	if f.EtherType != ethernet.EtherTypeARP {
		Log(Error, "Not ARP")
		return
	}

	packet := new(ARPPacket)
	if err := packet.UnmarshalARP(f.Payload); err != nil {
		Log(Error, "Failed to unmarshal arp")
		return
	}
	Log(Trace, "Peers: %v, Target IP: %s", p.NetworkPeers, packet.TargetIP.String())
	var hwAddr net.HardwareAddr
	id, exists := p.IPIDTable[packet.TargetIP.String()]
	if !exists {
		Log(Debug, "Unknown IP requested")
		return
	}
	peer, exists := p.NetworkPeers[id]
	if !exists {
		Log(Debug, "Specified ID was not found in peer list")
		return
	}
	hwAddr = peer.PeerHW
	// TODO: Put there normal IP from list of ips
	// Send a reply
	if hwAddr == nil {
		Log(Error, "Cannot find hardware address for requested IP")
		_, hwAddr = GenerateMAC()
		peer.PeerHW = hwAddr
		p.NetworkPeers[id] = peer
	}
	if hwAddr.String() == "00:00:00:00:00:00" {
		_, hwAddr = GenerateMAC()
		peer.PeerHW = hwAddr
		p.NetworkPeers[id] = peer
	}
	var reply ARPPacket
	ip := net.ParseIP(packet.TargetIP.String())
	response, err := reply.NewPacket(OperationReply, hwAddr, ip, packet.SenderHardwareAddr, packet.SenderIP)
	if err != nil {
		Log(Error, "Failed to create ARP reply")
		return
	}
	rp, err := response.MarshalBinary()
	if err != nil {
		Log(Error, "Failed to marshal ARP response packet")
		return
	}

	fr := &ethernet.Frame{
		Destination: response.TargetHardwareAddr,
		Source:      response.SenderHardwareAddr,
		EtherType:   ethernet.EtherTypeARP,
		Payload:     rp,
	}

	fb, err := fr.MarshalBinary()
	if err != nil {
		Log(Error, "Failed to marshal ARP Ethernet Frame")
	}
	Log(Debug, "%v", packet.String())
	p.WriteToDevice(fb, uint16(proto), false)
}
开发者ID:subutai-io,项目名称:p2p,代码行数:72,代码来源:packet.go


示例8: TestServeOK

func TestServeOK(t *testing.T) {
	b := append([]byte{
		// Ethernet frame
		0xde, 0xad, 0xbe, 0xef, 0xde, 0xad,
		0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
		0x08, 0x06,
		// ARP Packet
		0, 1,
		0x08, 0x06,
		6,
		4,
		0, 1,
		0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
		192, 168, 1, 10,
		0xde, 0xad, 0xbe, 0xef, 0xde, 0xad,
		192, 168, 1, 1,
	}, make([]byte, 40)...)

	// Values to be sent in ARP reply and checked later
	wantsHW := net.HardwareAddr{0xde, 0xad, 0xbe, 0xef, 0xde, 0xad}
	wantsIP := net.IP{192, 168, 1, 1}

	wanttHW := net.HardwareAddr{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}
	wanttIP := net.IP{192, 168, 1, 10}

	// Count bytes sent
	var n int
	p, err := testServe(b, true, func(w ResponseSender, r *Request) {
		// Build an ARP reply for request
		pkt, err := NewPacket(
			OperationReply,
			r.TargetHardwareAddr,
			r.TargetIP,
			r.SenderHardwareAddr,
			r.SenderIP,
		)
		if err != nil {
			t.Fatal(err)
		}

		n, err = w.Send(pkt)
		if err != nil {
			t.Fatal(err)
		}
		return
	})
	if err != nil {
		t.Fatal(err)
	}

	if want, got := n, p.wb.Len(); want != got {
		t.Fatalf("unexpected response length: %v != %v", want, got)
	}

	// Unmarshal ethernet frame and verify fields
	f := new(ethernet.Frame)
	if err := f.UnmarshalBinary(p.wb.Bytes()); err != nil {
		log.Println(len(p.wb.Bytes()))
		t.Fatal(err)
	}

	if want, got := wanttHW, f.Destination; !bytes.Equal(want, got) {
		t.Fatalf("unexpected ethernet frame destination:\n- want: %v\n-  got: %v",
			want, got)
	}
	if want, got := wantsHW, f.Source; !bytes.Equal(want, got) {
		t.Fatalf("unexpected ethernet frame source:\n- want: %v\n-  got: %v",
			want, got)
	}
	if want, got := ethernet.EtherTypeARP, f.EtherType; want != got {
		t.Fatalf("unexpected ethernet frame EtherType: %v != %v", want, got)
	}

	// Unmarshal ARP packet and verify fields
	pkt := new(Packet)
	if err := pkt.UnmarshalBinary(f.Payload); err != nil {
		t.Fatal(err)
	}

	// Hardware type is hardcoded
	if want, got := uint16(1), pkt.HardwareType; want != got {
		t.Fatalf("unexpected ARP packet hardware type: %v != %v", want, got)
	}
	// Protocol type is hardcoded
	if want, got := uint16(ethernet.EtherTypeIPv4), pkt.ProtocolType; want != got {
		t.Fatalf("unexpected ARP packet protocol type: %v != %v", want, got)
	}

	if want, got := uint8(len(wantsHW)), pkt.HardwareAddrLength; want != got {
		t.Fatalf("unexpected ARP packet hardware address length: %v != %v", want, got)
	}
	if want, got := uint8(len(wantsIP)), pkt.IPLength; want != got {
		t.Fatalf("unexpected ARP packet IP length: %v != %v", want, got)
	}

	if want, got := OperationReply, pkt.Operation; want != got {
		t.Fatalf("unexpected ARP packet operation: %v != %v", want, got)
	}

	if want, got := wantsHW, pkt.SenderHardwareAddr; !bytes.Equal(want, got) {
//.........这里部分代码省略.........
开发者ID:Tsingson1988,项目名称:arp,代码行数:101,代码来源:server_test.go



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang common.LoadConfig函数代码示例发布时间:2022-05-23
下一篇:
Golang aoe.Header类代码示例发布时间:2022-05-23
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap