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


Golang Buffer.Len方法代码示例

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


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

示例1: Unpack

// Unpack the given byte slice into the packet list supplied. Note that this
// will not check whether the packet types provided match the raw data. If the
// packet types to be decoded are unknown, UnpackAll() should be used instead.
//
// Note that unpacking is done without copying the input slice, which means that
// if the slice is modifed, it may affect the packets that where unpacked from
// it. If you can't guarantee that the data slice won't change, you'll need to
// copy it and pass the copy to Unpack().
func Unpack(buf []byte, pkts ...packet.Packet) (packet.Packet, error) {
	var b packet.Buffer
	b.Init(buf)

	prev_pkt := packet.Packet(nil)

	for _, p := range pkts {
		if b.Len() <= 0 {
			break
		}

		b.NewLayer()

		err := p.Unpack(&b)
		if err != nil {
			return nil, err
		}

		if prev_pkt != nil {
			prev_pkt.SetPayload(p)
		}

		if p.GuessPayloadType() == packet.None {
			break
		}

		prev_pkt = p
	}

	return pkts[0], nil
}
开发者ID:kelixin,项目名称:go.pkt,代码行数:39,代码来源:layers.go

示例2: UnpackAll

// Recursively unpack the given byte slice into a packet. The link_type argument
// must specify the type of the first layer in the input data, successive layers
// will be detected automatically.
//
// Note that unpacking is done without copying the input slice, which means that
// if the slice is modifed, it may affect the packets that where unpacked from
// it. If you can't guarantee that the data slice won't change, you'll need to
// copy it and pass the copy to UnpackAll().
func UnpackAll(buf []byte, link_type packet.Type) (packet.Packet, error) {
	var b packet.Buffer
	b.Init(buf)

	first_pkt := packet.Packet(nil)
	prev_pkt := packet.Packet(nil)

	for link_type != packet.None {
		var p packet.Packet

		if b.Len() <= 0 {
			break
		}

		switch link_type {
		case packet.ARP:
			p = &arp.Packet{}
		case packet.Eth:
			p = &eth.Packet{}
		case packet.ICMPv4:
			p = &icmpv4.Packet{}
		case packet.ICMPv6:
			p = &icmpv6.Packet{}
		case packet.IPv4:
			p = &ipv4.Packet{}
		case packet.IPv6:
			p = &ipv6.Packet{}
		case packet.LLC:
			p = &llc.Packet{}
		case packet.RadioTap:
			p = &radiotap.Packet{}
		case packet.SLL:
			p = &sll.Packet{}
		case packet.SNAP:
			p = &snap.Packet{}
		case packet.TCP:
			p = &tcp.Packet{}
		case packet.UDP:
			p = &udp.Packet{}
		case packet.VLAN:
			p = &vlan.Packet{}
		default:
			p = &raw.Packet{}
		}

		if p == nil {
			break
		}

		b.NewLayer()

		err := p.Unpack(&b)
		if err != nil {
			return nil, err
		}

		if prev_pkt != nil {
			prev_pkt.SetPayload(p)
		} else {
			first_pkt = p
		}

		prev_pkt = p
		link_type = p.GuessPayloadType()
	}

	return first_pkt, nil
}
开发者ID:kelixin,项目名称:go.pkt,代码行数:76,代码来源:layers.go

示例3: Unpack

func (p *Packet) Unpack(buf *packet.Buffer) error {
	p.Data = buf.Next(buf.Len())

	return nil
}
开发者ID:kelixin,项目名称:go.pkt,代码行数:5,代码来源:pkt.go


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