本文整理匯總了Golang中github.com/ghedo/go/pkt/packet.Buffer.Init方法的典型用法代碼示例。如果您正苦於以下問題:Golang Buffer.Init方法的具體用法?Golang Buffer.Init怎麽用?Golang Buffer.Init使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/ghedo/go/pkt/packet.Buffer
的用法示例。
在下文中一共展示了Buffer.Init方法的12個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: Pack
// Pack packets into their binary form. This will stack the packets before
// encoding them (see the Compose() method) and also calculate the checksums.
func Pack(pkts ...packet.Packet) ([]byte, error) {
var buf packet.Buffer
base_pkt, err := Compose(pkts...)
if err != nil {
return nil, err
}
tot_len := int(base_pkt.GetLength())
buf.Init(make([]byte, tot_len))
for i := len(pkts) - 1; i >= 0; i-- {
cur_pkt := pkts[i]
cur_len := int(cur_pkt.GetLength())
buf.SetOffset(tot_len - cur_len)
buf.NewLayer()
err := cur_pkt.Pack(&buf)
if err != nil {
return nil, err
}
}
buf.SetOffset(0)
return buf.Bytes(), nil
}
示例2: 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
}
示例3: TestUnpackOptions
func TestUnpackOptions(t *testing.T) {
var p tcp.Packet
var b packet.Buffer
b.Init(test_options)
err := p.Unpack(&b)
if err != nil {
t.Fatalf("Error unpacking: %s", err)
}
if len(p.Options) != 4 {
t.Fatalf("Options number mismatch: %d", len(p.Options))
}
if p.Options[0].Type != tcp.MSS {
t.Fatalf("Option MSS mismatch: %x", p.Options[0].Data)
}
if p.Options[1].Type != tcp.SAckOk {
t.Fatalf("Option SAckOk mismatch: %x", p.Options[1].Data)
}
if p.Options[2].Type != tcp.Timestamp {
t.Fatalf("Option Timestamp mismatch: %x", p.Options[2].Data)
}
if p.Options[3].Type != tcp.WindowScale {
t.Fatalf("Option WindowScale mismatch: %x", p.Options[3].Data)
}
}
示例4: TestPackWithIPv4
func TestPackWithIPv4(t *testing.T) {
var b packet.Buffer
b.Init(make([]byte, len(test_with_ipv4)))
ip4 := ipv4.Make()
ip4.SrcAddr = net.ParseIP(ipsrc_str)
ip4.DstAddr = net.ParseIP(ipdst_str)
udp := &udp.Packet{
SrcPort: 52134,
DstPort: 80,
Length: 18,
}
ip4.SetPayload(udp)
err := udp.Pack(&b)
if err != nil {
t.Fatalf("Error packing: %s", err)
}
if !bytes.Equal(test_with_ipv4, b.Buffer()) {
t.Fatalf("Raw packet mismatch: %x", b.Buffer())
}
}
示例5: BenchmarkUnpack
func BenchmarkUnpack(bn *testing.B) {
var p eth.Packet
var b packet.Buffer
for n := 0; n < bn.N; n++ {
b.Init(test_simple)
p.Unpack(&b)
}
}
示例6: BenchmarkPack
func BenchmarkPack(bn *testing.B) {
var b packet.Buffer
b.Init(make([]byte, len(test_simple)))
p := MakeTestSimple()
for n := 0; n < bn.N; n++ {
p.Pack(&b)
}
}
示例7: TestPackOptions
func TestPackOptions(t *testing.T) {
var b packet.Buffer
b.Init(make([]byte, len(test_options)))
p := MakeTestSimple()
p.DataOff = 10
p.Options = append(p.Options,
tcp.Option{
Type: tcp.MSS,
Len: 4,
Data: []byte{0x05, 0x78},
},
)
p.Options = append(p.Options,
tcp.Option{
Type: tcp.SAckOk,
Len: 2,
},
)
p.Options = append(p.Options,
tcp.Option{
Type: tcp.Timestamp,
Len: 10,
Data: []byte{0x61, 0x25, 0xE5, 0xB2, 0x00, 0x13, 0x15, 0x66},
},
)
p.Options = append(p.Options,
tcp.Option{
Type: tcp.WindowScale,
Len: 3,
Data: []byte{0x0A},
},
)
p.Options = append(p.Options,
tcp.Option{
Type: tcp.End,
},
)
err := p.Pack(&b)
if err != nil {
t.Fatalf("Error packing: %s", err)
}
if !bytes.Equal(test_options, b.Buffer()) {
t.Fatalf("Raw packet mismatch: %x", b.Buffer())
}
}
示例8: TestPack
func TestPack(t *testing.T) {
var b packet.Buffer
b.Init(make([]byte, len(test_simple)))
p := MakeTestSimple()
err := p.Pack(&b)
if err != nil {
t.Fatalf("Error packing: %s", err)
}
if !bytes.Equal(test_simple, b.Buffer()) {
t.Fatalf("Raw packet mismatch: %x", b.Buffer())
}
}
示例9: TestUnpack
func TestUnpack(t *testing.T) {
var p eth.Packet
cmp := MakeTestSimple()
var b packet.Buffer
b.Init(test_simple)
err := p.Unpack(&b)
if err != nil {
t.Fatalf("Error unpacking: %s", err)
}
if !p.Equals(cmp) {
t.Fatalf("Packet mismatch:\n%s\n%s", &p, cmp)
}
}
示例10: TestUnpackWithIPv6
func TestUnpackWithIPv6(t *testing.T) {
var p icmpv6.Packet
cmp := MakeTestSimple()
cmp.Checksum = 0x5bed
var b packet.Buffer
b.Init(test_with_ipv6)
err := p.Unpack(&b)
if err != nil {
t.Fatalf("Error unpacking: %s", err)
}
if !p.Equals(cmp) {
t.Fatalf("Packet mismatch:\n%s\n%s", &p, cmp)
}
}
示例11: TestPackWithIPv6
func TestPackWithIPv6(t *testing.T) {
var b packet.Buffer
b.Init(make([]byte, len(test_with_ipv6)))
ip6 := ipv6.Make()
ip6.SrcAddr = net.ParseIP(ipsrc_str)
ip6.DstAddr = net.ParseIP(ipdst_str)
icmp6 := MakeTestSimple()
ip6.SetPayload(icmp6)
err := icmp6.Pack(&b)
if err != nil {
t.Fatalf("Error packing: %s", err)
}
if !bytes.Equal(test_with_ipv6, b.Buffer()) {
t.Fatalf("Raw packet mismatch: %x", b.Buffer())
}
}
示例12: 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 = ð.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
}