1 // Copyright 2012, Google, Inc. All rights reserved.
2 // Copyright 2009-2011 Andreas Krennmair. All rights reserved.
4 // Use of this source code is governed by a BSD-style license
5 // that can be found in the LICENSE file in the root of the source
19 "github.com/google/gopacket"
20 "github.com/google/gopacket/bytediff"
23 var testSimpleTCPPacket = []byte{
24 0x00, 0x00, 0x0c, 0x9f, 0xf0, 0x20, 0xbc, 0x30, 0x5b, 0xe8, 0xd3, 0x49,
25 0x08, 0x00, 0x45, 0x00, 0x01, 0xa4, 0x39, 0xdf, 0x40, 0x00, 0x40, 0x06,
26 0x55, 0x5a, 0xac, 0x11, 0x51, 0x49, 0xad, 0xde, 0xfe, 0xe1, 0xc5, 0xf7,
27 0x00, 0x50, 0xc5, 0x7e, 0x0e, 0x48, 0x49, 0x07, 0x42, 0x32, 0x80, 0x18,
28 0x00, 0x73, 0x9a, 0x8f, 0x00, 0x00, 0x01, 0x01, 0x08, 0x0a, 0x03, 0x77,
29 0x37, 0x9c, 0x42, 0x77, 0x5e, 0x3a, 0x47, 0x45, 0x54, 0x20, 0x2f, 0x20,
30 0x48, 0x54, 0x54, 0x50, 0x2f, 0x31, 0x2e, 0x31, 0x0d, 0x0a, 0x48, 0x6f,
31 0x73, 0x74, 0x3a, 0x20, 0x77, 0x77, 0x77, 0x2e, 0x66, 0x69, 0x73, 0x68,
32 0x2e, 0x63, 0x6f, 0x6d, 0x0d, 0x0a, 0x43, 0x6f, 0x6e, 0x6e, 0x65, 0x63,
33 0x74, 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x6b, 0x65, 0x65, 0x70, 0x2d, 0x61,
34 0x6c, 0x69, 0x76, 0x65, 0x0d, 0x0a, 0x55, 0x73, 0x65, 0x72, 0x2d, 0x41,
35 0x67, 0x65, 0x6e, 0x74, 0x3a, 0x20, 0x4d, 0x6f, 0x7a, 0x69, 0x6c, 0x6c,
36 0x61, 0x2f, 0x35, 0x2e, 0x30, 0x20, 0x28, 0x58, 0x31, 0x31, 0x3b, 0x20,
37 0x4c, 0x69, 0x6e, 0x75, 0x78, 0x20, 0x78, 0x38, 0x36, 0x5f, 0x36, 0x34,
38 0x29, 0x20, 0x41, 0x70, 0x70, 0x6c, 0x65, 0x57, 0x65, 0x62, 0x4b, 0x69,
39 0x74, 0x2f, 0x35, 0x33, 0x35, 0x2e, 0x32, 0x20, 0x28, 0x4b, 0x48, 0x54,
40 0x4d, 0x4c, 0x2c, 0x20, 0x6c, 0x69, 0x6b, 0x65, 0x20, 0x47, 0x65, 0x63,
41 0x6b, 0x6f, 0x29, 0x20, 0x43, 0x68, 0x72, 0x6f, 0x6d, 0x65, 0x2f, 0x31,
42 0x35, 0x2e, 0x30, 0x2e, 0x38, 0x37, 0x34, 0x2e, 0x31, 0x32, 0x31, 0x20,
43 0x53, 0x61, 0x66, 0x61, 0x72, 0x69, 0x2f, 0x35, 0x33, 0x35, 0x2e, 0x32,
44 0x0d, 0x0a, 0x41, 0x63, 0x63, 0x65, 0x70, 0x74, 0x3a, 0x20, 0x74, 0x65,
45 0x78, 0x74, 0x2f, 0x68, 0x74, 0x6d, 0x6c, 0x2c, 0x61, 0x70, 0x70, 0x6c,
46 0x69, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x2f, 0x78, 0x68, 0x74, 0x6d,
47 0x6c, 0x2b, 0x78, 0x6d, 0x6c, 0x2c, 0x61, 0x70, 0x70, 0x6c, 0x69, 0x63,
48 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x2f, 0x78, 0x6d, 0x6c, 0x3b, 0x71, 0x3d,
49 0x30, 0x2e, 0x39, 0x2c, 0x2a, 0x2f, 0x2a, 0x3b, 0x71, 0x3d, 0x30, 0x2e,
50 0x38, 0x0d, 0x0a, 0x41, 0x63, 0x63, 0x65, 0x70, 0x74, 0x2d, 0x45, 0x6e,
51 0x63, 0x6f, 0x64, 0x69, 0x6e, 0x67, 0x3a, 0x20, 0x67, 0x7a, 0x69, 0x70,
52 0x2c, 0x64, 0x65, 0x66, 0x6c, 0x61, 0x74, 0x65, 0x2c, 0x73, 0x64, 0x63,
53 0x68, 0x0d, 0x0a, 0x41, 0x63, 0x63, 0x65, 0x70, 0x74, 0x2d, 0x4c, 0x61,
54 0x6e, 0x67, 0x75, 0x61, 0x67, 0x65, 0x3a, 0x20, 0x65, 0x6e, 0x2d, 0x55,
55 0x53, 0x2c, 0x65, 0x6e, 0x3b, 0x71, 0x3d, 0x30, 0x2e, 0x38, 0x0d, 0x0a,
56 0x41, 0x63, 0x63, 0x65, 0x70, 0x74, 0x2d, 0x43, 0x68, 0x61, 0x72, 0x73,
57 0x65, 0x74, 0x3a, 0x20, 0x49, 0x53, 0x4f, 0x2d, 0x38, 0x38, 0x35, 0x39,
58 0x2d, 0x31, 0x2c, 0x75, 0x74, 0x66, 0x2d, 0x38, 0x3b, 0x71, 0x3d, 0x30,
59 0x2e, 0x37, 0x2c, 0x2a, 0x3b, 0x71, 0x3d, 0x30, 0x2e, 0x33, 0x0d, 0x0a,
63 var testDecodeOptions = gopacket.DecodeOptions{
64 SkipDecodeRecovery: true,
67 // Benchmarks for actual gopacket code
69 func BenchmarkLayerClassSliceContains(b *testing.B) {
70 lc := gopacket.NewLayerClassSlice([]gopacket.LayerType{LayerTypeTCP, LayerTypeEthernet})
71 for i := 0; i < b.N; i++ {
72 _ = lc.Contains(LayerTypeTCP)
76 func BenchmarkLayerClassMapContains(b *testing.B) {
77 lc := gopacket.NewLayerClassMap([]gopacket.LayerType{LayerTypeTCP, LayerTypeEthernet})
78 for i := 0; i < b.N; i++ {
79 _ = lc.Contains(LayerTypeTCP)
83 func BenchmarkLazyNoCopyEthLayer(b *testing.B) {
84 for i := 0; i < b.N; i++ {
85 gopacket.NewPacket(testSimpleTCPPacket, LinkTypeEthernet, gopacket.DecodeOptions{Lazy: true, NoCopy: true}).Layer(LayerTypeEthernet)
89 func BenchmarkLazyNoCopyIPLayer(b *testing.B) {
90 for i := 0; i < b.N; i++ {
91 gopacket.NewPacket(testSimpleTCPPacket, LinkTypeEthernet, gopacket.DecodeOptions{Lazy: true, NoCopy: true}).Layer(LayerTypeIPv4)
95 func BenchmarkLazyNoCopyTCPLayer(b *testing.B) {
96 for i := 0; i < b.N; i++ {
97 gopacket.NewPacket(testSimpleTCPPacket, LinkTypeEthernet, gopacket.DecodeOptions{Lazy: true, NoCopy: true}).Layer(LayerTypeTCP)
101 func BenchmarkLazyNoCopyAllLayers(b *testing.B) {
102 for i := 0; i < b.N; i++ {
103 gopacket.NewPacket(testSimpleTCPPacket, LinkTypeEthernet, gopacket.DecodeOptions{Lazy: true, NoCopy: true}).Layers()
107 func BenchmarkDefault(b *testing.B) {
108 for i := 0; i < b.N; i++ {
109 gopacket.NewPacket(testSimpleTCPPacket, LinkTypeEthernet, gopacket.Default)
113 func getSerializeLayers() []gopacket.SerializableLayer {
114 p := gopacket.NewPacket(testSimpleTCPPacket, LinkTypeEthernet, testDecodeOptions)
115 slayers := []gopacket.SerializableLayer{}
116 for _, l := range p.Layers() {
117 slayers = append(slayers, l.(gopacket.SerializableLayer))
119 p.Layer(LayerTypeTCP).(*TCP).SetNetworkLayerForChecksum(
124 func BenchmarkSerializeTcpNoOptions(b *testing.B) {
125 slayers := getSerializeLayers()
126 buf := gopacket.NewSerializeBuffer()
127 opts := gopacket.SerializeOptions{}
128 for i := 0; i < b.N; i++ {
129 gopacket.SerializeLayers(buf, opts, slayers...)
133 func BenchmarkSerializeTcpFixLengths(b *testing.B) {
134 slayers := getSerializeLayers()
135 buf := gopacket.NewSerializeBuffer()
136 opts := gopacket.SerializeOptions{FixLengths: true}
137 for i := 0; i < b.N; i++ {
138 gopacket.SerializeLayers(buf, opts, slayers...)
142 func BenchmarkSerializeTcpComputeChecksums(b *testing.B) {
143 slayers := getSerializeLayers()
144 buf := gopacket.NewSerializeBuffer()
145 opts := gopacket.SerializeOptions{ComputeChecksums: true}
146 for i := 0; i < b.N; i++ {
147 gopacket.SerializeLayers(buf, opts, slayers...)
151 func BenchmarkSerializeTcpFixLengthsComputeChecksums(b *testing.B) {
152 slayers := getSerializeLayers()
153 buf := gopacket.NewSerializeBuffer()
154 opts := gopacket.SerializeOptions{FixLengths: true, ComputeChecksums: true}
155 for i := 0; i < b.N; i++ {
156 gopacket.SerializeLayers(buf, opts, slayers...)
160 func BenchmarkLazy(b *testing.B) {
161 for i := 0; i < b.N; i++ {
162 gopacket.NewPacket(testSimpleTCPPacket, LinkTypeEthernet, gopacket.Lazy)
166 func BenchmarkNoCopy(b *testing.B) {
167 for i := 0; i < b.N; i++ {
168 gopacket.NewPacket(testSimpleTCPPacket, LinkTypeEthernet, gopacket.NoCopy)
172 func BenchmarkLazyNoCopy(b *testing.B) {
173 for i := 0; i < b.N; i++ {
174 gopacket.NewPacket(testSimpleTCPPacket, LinkTypeEthernet, gopacket.DecodeOptions{Lazy: true, NoCopy: true})
178 func BenchmarkKnownStack(b *testing.B) {
179 stack := []gopacket.DecodingLayer{&Ethernet{}, &IPv4{}, &TCP{}, &gopacket.Payload{}}
180 nf := gopacket.NilDecodeFeedback
181 for i := 0; i < b.N; i++ {
182 data := testSimpleTCPPacket[:]
183 for _, d := range stack {
184 _ = d.DecodeFromBytes(data, nf)
185 data = d.LayerPayload()
190 func BenchmarkDecodingLayerParserIgnorePanic(b *testing.B) {
191 decoded := make([]gopacket.LayerType, 0, 20)
192 dlp := gopacket.NewDecodingLayerParser(LayerTypeEthernet, &Ethernet{}, &IPv4{}, &TCP{}, &gopacket.Payload{})
193 dlp.IgnorePanic = true
194 for i := 0; i < b.N; i++ {
195 dlp.DecodeLayers(testSimpleTCPPacket, &decoded)
199 func BenchmarkDecodingLayerParserHandlePanic(b *testing.B) {
200 decoded := make([]gopacket.LayerType, 0, 20)
201 dlp := gopacket.NewDecodingLayerParser(LayerTypeEthernet, &Ethernet{}, &IPv4{}, &TCP{}, &gopacket.Payload{})
202 dlp.IgnorePanic = false
203 for i := 0; i < b.N; i++ {
204 dlp.DecodeLayers(testSimpleTCPPacket, &decoded)
208 func BenchmarkAlloc(b *testing.B) {
209 for i := 0; i < b.N; i++ {
214 func BenchmarkFlow(b *testing.B) {
215 p := gopacket.NewPacket(testSimpleTCPPacket, LinkTypeEthernet, gopacket.DecodeOptions{Lazy: true, NoCopy: true})
216 net := p.NetworkLayer()
217 for i := 0; i < b.N; i++ {
222 func BenchmarkEndpoints(b *testing.B) {
223 p := gopacket.NewPacket(testSimpleTCPPacket, LinkTypeEthernet, gopacket.DecodeOptions{Lazy: true, NoCopy: true})
224 flow := p.NetworkLayer().NetworkFlow()
225 for i := 0; i < b.N; i++ {
230 func BenchmarkTCPLayerFromDecodedPacket(b *testing.B) {
232 p := gopacket.NewPacket(testSimpleTCPPacket, LinkTypeEthernet, testDecodeOptions)
234 for i := 0; i < b.N; i++ {
235 _ = p.Layer(LayerTypeTCP)
239 func BenchmarkTCPLayerClassFromDecodedPacket(b *testing.B) {
241 p := gopacket.NewPacket(testSimpleTCPPacket, LinkTypeEthernet, testDecodeOptions)
242 lc := gopacket.NewLayerClass([]gopacket.LayerType{LayerTypeTCP})
244 for i := 0; i < b.N; i++ {
249 func BenchmarkTCPTransportLayerFromDecodedPacket(b *testing.B) {
251 p := gopacket.NewPacket(testSimpleTCPPacket, LinkTypeEthernet, testDecodeOptions)
253 for i := 0; i < b.N; i++ {
254 _ = p.TransportLayer()
258 func testDecoder([]byte, gopacket.PacketBuilder) error {
262 func BenchmarkDecodeFuncCallOverheadDirectCall(b *testing.B) {
264 var pb gopacket.PacketBuilder
265 for i := 0; i < b.N; i++ {
266 _ = testDecoder(data, pb)
270 func BenchmarkDecodeFuncCallOverheadDecoderCall(b *testing.B) {
271 d := gopacket.DecodeFunc(testDecoder)
273 var pb gopacket.PacketBuilder
274 for i := 0; i < b.N; i++ {
275 _ = d.Decode(data, pb)
279 func BenchmarkDecodeFuncCallOverheadArrayCall(b *testing.B) {
280 EthernetTypeMetadata[1] = EnumMetadata{DecodeWith: gopacket.DecodeFunc(testDecoder)}
283 var pb gopacket.PacketBuilder
284 for i := 0; i < b.N; i++ {
285 _ = d.Decode(data, pb)
289 func BenchmarkFmtVerboseString(b *testing.B) {
291 p := gopacket.NewPacket(testSimpleTCPPacket, LinkTypeEthernet, testDecodeOptions)
293 for i := 0; i < b.N; i++ {
294 _ = fmt.Sprintf("%#v", p)
298 func BenchmarkPacketString(b *testing.B) {
300 p := gopacket.NewPacket(testSimpleTCPPacket, LinkTypeEthernet, testDecodeOptions)
302 for i := 0; i < b.N; i++ {
307 func BenchmarkPacketDumpString(b *testing.B) {
309 p := gopacket.NewPacket(testSimpleTCPPacket, LinkTypeEthernet, testDecodeOptions)
311 for i := 0; i < b.N; i++ {
316 // TestFlowMapKey makes sure a flow and an endpoint can be used as map keys.
317 func TestFlowMapKey(t *testing.T) {
318 _ = map[gopacket.Flow]bool{}
319 _ = map[gopacket.Endpoint]bool{}
320 _ = map[[2]gopacket.Flow]bool{}
323 func TestDecodeSimpleTCPPacket(t *testing.T) {
324 equal := func(desc, want string, got fmt.Stringer) {
325 if want != got.String() {
326 t.Errorf("%s: got %q want %q", desc, got.String(), want)
329 p := gopacket.NewPacket(testSimpleTCPPacket, LinkTypeEthernet, gopacket.DecodeOptions{Lazy: true, NoCopy: true})
330 if eth := p.LinkLayer(); eth == nil {
331 t.Error("No ethernet layer found")
333 equal("Eth Src", "bc:30:5b:e8:d3:49", eth.LinkFlow().Src())
334 equal("Eth Dst", "00:00:0c:9f:f0:20", eth.LinkFlow().Dst())
336 if net := p.NetworkLayer(); net == nil {
337 t.Error("No net layer found")
338 } else if ip, ok := net.(*IPv4); !ok {
339 t.Error("Net layer is not IP layer")
341 equal("IP Src", "172.17.81.73", net.NetworkFlow().Src())
342 equal("IP Dst", "173.222.254.225", net.NetworkFlow().Dst())
344 BaseLayer: BaseLayer{testSimpleTCPPacket[14:34], testSimpleTCPPacket[34:]},
355 SrcIP: []byte{172, 17, 81, 73},
356 DstIP: []byte{173, 222, 254, 225},
358 if !reflect.DeepEqual(ip, want) {
359 t.Errorf("IP layer mismatch, \ngot %#v\nwant %#v\n", ip, want)
362 if trans := p.TransportLayer(); trans == nil {
363 t.Error("No transport layer found")
364 } else if tcp, ok := trans.(*TCP); !ok {
365 t.Error("Transport layer is not TCP layer")
367 equal("TCP Src", "50679", trans.TransportFlow().Src())
368 equal("TCP Dst", "80", trans.TransportFlow().Dst())
370 BaseLayer: BaseLayer{testSimpleTCPPacket[34:66], testSimpleTCPPacket[66:]},
381 sPort: []byte{0xc5, 0xf7},
382 dPort: []byte{0x0, 0x50},
383 Options: []TCPOption{
395 OptionData: []byte{0x3, 0x77, 0x37, 0x9c, 0x42, 0x77, 0x5e, 0x3a},
410 OptionData: []byte{0x3, 0x77, 0x37, 0x9c, 0x42, 0x77, 0x5e, 0x3a},
415 if !reflect.DeepEqual(tcp, want) {
416 t.Errorf("TCP layer mismatch\ngot %#v\nwant %#v", tcp, want)
419 if payload, ok := p.Layer(gopacket.LayerTypePayload).(*gopacket.Payload); payload == nil || !ok {
420 t.Error("No payload layer found")
422 if string(payload.Payload()) != "GET / HTTP/1.1\r\nHost: www.fish.com\r\nConnection: keep-alive\r\nUser-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2\r\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\nAccept-Encoding: gzip,deflate,sdch\r\nAccept-Language: en-US,en;q=0.8\r\nAccept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3\r\n\r\n" {
423 t.Error("--- Payload STRING ---\n", string(payload.Payload()), "\n--- Payload BYTES ---\n", payload.Payload())
427 // Test re-serialization.
428 testSerialization(t, p, testSimpleTCPPacket)
431 type canSetNetLayer interface {
432 SetNetworkLayerForChecksum(gopacket.NetworkLayer) error
435 func testSerialization(t *testing.T, p gopacket.Packet, data []byte) {
436 for _, opts := range []gopacket.SerializeOptions{
437 gopacket.SerializeOptions{},
438 gopacket.SerializeOptions{FixLengths: true},
439 gopacket.SerializeOptions{ComputeChecksums: true},
440 gopacket.SerializeOptions{FixLengths: true, ComputeChecksums: true},
442 testSerializationWithOpts(t, p, data, opts)
446 func testSerializationWithOpts(t *testing.T, p gopacket.Packet, data []byte, opts gopacket.SerializeOptions) {
447 // Test re-serialization.
448 slayers := []gopacket.SerializableLayer{}
449 for _, l := range p.Layers() {
450 slayers = append(slayers, l.(gopacket.SerializableLayer))
451 if h, ok := l.(canSetNetLayer); ok {
452 if err := h.SetNetworkLayerForChecksum(p.NetworkLayer()); err != nil {
453 t.Fatal("can't set network layer:", err)
457 buf := gopacket.NewSerializeBuffer()
458 err := gopacket.SerializeLayers(buf, opts, slayers...)
460 t.Errorf("unable to reserialize layers with opts %#v: %v", opts, err)
461 } else if !bytes.Equal(buf.Bytes(), data) {
462 t.Errorf("serialization failure with opts %#v:\n---want---\n%v\n---got---\n%v\nBASH-colorized diff, want->got:\n%v\n\n---PACKET---\n%v",
463 opts, hex.Dump(data), hex.Dump(buf.Bytes()), bytediff.BashOutput.String(bytediff.Diff(data, buf.Bytes())), p)
467 // Makes sure packet payload doesn't display the 6 trailing null of this packet
468 // as part of the payload. They're actually the ethernet trailer.
469 func TestDecodeSmallTCPPacketHasEmptyPayload(t *testing.T) {
470 smallPacket := []byte{
471 0xbc, 0x30, 0x5b, 0xe8, 0xd3, 0x49, 0xb8, 0xac, 0x6f, 0x92, 0xd5, 0xbf,
472 0x08, 0x00, 0x45, 0x00, 0x00, 0x28, 0x00, 0x00, 0x40, 0x00, 0x40, 0x06,
473 0x3f, 0x9f, 0xac, 0x11, 0x51, 0xc5, 0xac, 0x11, 0x51, 0x49, 0x00, 0x63,
474 0x9a, 0xef, 0x00, 0x00, 0x00, 0x00, 0x2e, 0xc1, 0x27, 0x83, 0x50, 0x14,
475 0x00, 0x00, 0xc3, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
477 p := gopacket.NewPacket(smallPacket, LinkTypeEthernet, testDecodeOptions)
479 if payload := p.Layer(gopacket.LayerTypePayload); payload != nil {
480 t.Error("Payload found for empty TCP packet")
483 testSerialization(t, p, smallPacket)
486 func TestDecodeVLANPacket(t *testing.T) {
487 p := gopacket.NewPacket(
489 0x00, 0x10, 0xdb, 0xff, 0x10, 0x00, 0x00, 0x15, 0x2c, 0x9d, 0xcc, 0x00,
490 0x81, 0x00, 0x01, 0xf7, 0x08, 0x00, 0x45, 0x00, 0x00, 0x28, 0x29, 0x8d,
491 0x40, 0x00, 0x7d, 0x06, 0x83, 0xa0, 0xac, 0x1b, 0xca, 0x8e, 0x45, 0x16,
492 0x94, 0xe2, 0xd4, 0x0a, 0x00, 0x50, 0xdf, 0xab, 0x9c, 0xc6, 0xcd, 0x1e,
493 0xe5, 0xd1, 0x50, 0x10, 0x01, 0x00, 0x5a, 0x74, 0x00, 0x00, 0x00, 0x00,
494 0x00, 0x00, 0x00, 0x00,
495 }, LinkTypeEthernet, testDecodeOptions)
496 if err := p.ErrorLayer(); err != nil {
497 t.Error("Error while parsing vlan packet:", err)
499 if vlan := p.Layer(LayerTypeDot1Q); vlan == nil {
500 t.Error("Didn't detect vlan")
501 } else if _, ok := vlan.(*Dot1Q); !ok {
502 t.Error("LayerTypeDot1Q layer is not a Dot1Q object")
504 for i, l := range p.Layers() {
505 t.Logf("Layer %d: %#v", i, l)
507 want := []gopacket.LayerType{LayerTypeEthernet, LayerTypeDot1Q, LayerTypeIPv4, LayerTypeTCP}
508 checkLayers(p, want, t)
511 func TestDecodeSCTPPackets(t *testing.T) {
512 sctpPackets := [][]byte{
514 0x00, 0x00, 0x0c, 0x9f, 0xf0, 0x1f, 0x24, 0xbe, 0x05, 0x27, 0x0b, 0x17, 0x08, 0x00, 0x45, 0x02,
515 0x00, 0x44, 0x00, 0x00, 0x40, 0x00, 0x40, 0x84, 0xc4, 0x22, 0xac, 0x1d, 0x14, 0x0f, 0xac, 0x19,
516 0x09, 0xcc, 0x27, 0x0f, 0x22, 0xb8, 0x00, 0x00, 0x00, 0x00, 0x19, 0x6b, 0x0b, 0x40, 0x01, 0x00,
517 0x00, 0x24, 0xb6, 0x96, 0xb0, 0x9e, 0x00, 0x01, 0xc0, 0x00, 0x00, 0x0a, 0xff, 0xff, 0xdb, 0x85,
518 0x60, 0x23, 0x00, 0x0c, 0x00, 0x06, 0x00, 0x05, 0x00, 0x00, 0x80, 0x00, 0x00, 0x04, 0xc0, 0x00,
520 }, []byte{ // INIT ACK
521 0x24, 0xbe, 0x05, 0x27, 0x0b, 0x17, 0x00, 0x1f, 0xca, 0xb3, 0x76, 0x40, 0x08, 0x00, 0x45, 0x20,
522 0x01, 0x24, 0x00, 0x00, 0x40, 0x00, 0x36, 0x84, 0xcd, 0x24, 0xac, 0x19, 0x09, 0xcc, 0xac, 0x1d,
523 0x14, 0x0f, 0x22, 0xb8, 0x27, 0x0f, 0xb6, 0x96, 0xb0, 0x9e, 0x4b, 0xab, 0x40, 0x9a, 0x02, 0x00,
524 0x01, 0x04, 0x32, 0x80, 0xfb, 0x42, 0x00, 0x00, 0xf4, 0x00, 0x00, 0x0a, 0x00, 0x0a, 0x85, 0x98,
525 0xb1, 0x26, 0x00, 0x07, 0x00, 0xe8, 0xd3, 0x08, 0xce, 0xe2, 0x52, 0x95, 0xcc, 0x09, 0xa1, 0x4c,
526 0x6f, 0xa7, 0x9e, 0xba, 0x03, 0xa1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
527 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x42, 0xfb, 0x80, 0x32, 0x9e, 0xb0,
528 0x96, 0xb6, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x46, 0xc2, 0x50, 0x00, 0x00,
529 0x00, 0x00, 0x5e, 0x25, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x0a, 0x00, 0x26, 0xb1,
530 0x98, 0x85, 0x02, 0x00, 0x27, 0x0f, 0xac, 0x1d, 0x14, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
531 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xb8, 0x22,
532 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x24, 0x6a, 0x72, 0x5c, 0x1c, 0x3c, 0xaa,
533 0x7a, 0xcd, 0xd3, 0x8f, 0x52, 0x78, 0x7c, 0x77, 0xfd, 0x46, 0xbd, 0x72, 0x82, 0xc1, 0x1f, 0x70,
534 0x44, 0xcc, 0xc7, 0x9b, 0x9b, 0x7b, 0x13, 0x54, 0x3f, 0x89, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
535 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
536 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x24, 0xb6, 0x96,
537 0xb0, 0x9e, 0x00, 0x01, 0xc0, 0x00, 0x00, 0x0a, 0xff, 0xff, 0xdb, 0x85, 0x60, 0x23, 0x00, 0x0c,
538 0x00, 0x06, 0x00, 0x05, 0x00, 0x00, 0x80, 0x00, 0x00, 0x04, 0xc0, 0x00, 0x00, 0x04, 0x00, 0x00,
539 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x04, 0xc0, 0x00,
541 }, []byte{ // COOKIE ECHO, DATA
542 0x00, 0x00, 0x0c, 0x9f, 0xf0, 0x1f, 0x24, 0xbe, 0x05, 0x27, 0x0b, 0x17, 0x08, 0x00, 0x45, 0x02,
543 0x01, 0x20, 0x00, 0x00, 0x40, 0x00, 0x40, 0x84, 0xc3, 0x46, 0xac, 0x1d, 0x14, 0x0f, 0xac, 0x19,
544 0x09, 0xcc, 0x27, 0x0f, 0x22, 0xb8, 0x32, 0x80, 0xfb, 0x42, 0x01, 0xf9, 0xf3, 0xa9, 0x0a, 0x00,
545 0x00, 0xe8, 0xd3, 0x08, 0xce, 0xe2, 0x52, 0x95, 0xcc, 0x09, 0xa1, 0x4c, 0x6f, 0xa7, 0x9e, 0xba,
546 0x03, 0xa1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
547 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x42, 0xfb, 0x80, 0x32, 0x9e, 0xb0, 0x96, 0xb6, 0x00, 0x00,
548 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x46, 0xc2, 0x50, 0x00, 0x00, 0x00, 0x00, 0x5e, 0x25,
549 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x0a, 0x00, 0x26, 0xb1, 0x98, 0x85, 0x02, 0x00,
550 0x27, 0x0f, 0xac, 0x1d, 0x14, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
551 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xb8, 0x22, 0x01, 0x00, 0x00, 0x00,
552 0x00, 0x00, 0x80, 0x02, 0x00, 0x24, 0x6a, 0x72, 0x5c, 0x1c, 0x3c, 0xaa, 0x7a, 0xcd, 0xd3, 0x8f,
553 0x52, 0x78, 0x7c, 0x77, 0xfd, 0x46, 0xbd, 0x72, 0x82, 0xc1, 0x1f, 0x70, 0x44, 0xcc, 0xc7, 0x9b,
554 0x9b, 0x7b, 0x13, 0x54, 0x3f, 0x89, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
555 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
556 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x24, 0xb6, 0x96, 0xb0, 0x9e, 0x00, 0x01,
557 0xc0, 0x00, 0x00, 0x0a, 0xff, 0xff, 0xdb, 0x85, 0x60, 0x23, 0x00, 0x0c, 0x00, 0x06, 0x00, 0x05,
558 0x00, 0x00, 0x80, 0x00, 0x00, 0x04, 0xc0, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
559 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x16, 0xdb, 0x85, 0x60, 0x23, 0x00, 0x00,
560 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x6f, 0x6f, 0x21, 0x0a, 0x00, 0x00, 0x00,
561 }, []byte{ // COOKIE ACK, SACK
562 0x24, 0xbe, 0x05, 0x27, 0x0b, 0x17, 0x00, 0x1f, 0xca, 0xb3, 0x76, 0x40, 0x08, 0x00, 0x45, 0x20,
563 0x00, 0x34, 0x00, 0x00, 0x40, 0x00, 0x36, 0x84, 0xce, 0x14, 0xac, 0x19, 0x09, 0xcc, 0xac, 0x1d,
564 0x14, 0x0f, 0x22, 0xb8, 0x27, 0x0f, 0xb6, 0x96, 0xb0, 0x9e, 0xed, 0x64, 0x30, 0x98, 0x0b, 0x00,
565 0x00, 0x04, 0x03, 0x00, 0x00, 0x10, 0xdb, 0x85, 0x60, 0x23, 0x00, 0x00, 0xf3, 0xfa, 0x00, 0x00,
568 0x00, 0x00, 0x0c, 0x9f, 0xf0, 0x1f, 0x24, 0xbe, 0x05, 0x27, 0x0b, 0x17, 0x08, 0x00, 0x45, 0x02,
569 0x00, 0x3c, 0x00, 0x00, 0x40, 0x00, 0x40, 0x84, 0xc4, 0x2a, 0xac, 0x1d, 0x14, 0x0f, 0xac, 0x19,
570 0x09, 0xcc, 0x27, 0x0f, 0x22, 0xb8, 0x32, 0x80, 0xfb, 0x42, 0xa1, 0xe3, 0xb2, 0x31, 0x00, 0x03,
571 0x00, 0x19, 0xdb, 0x85, 0x60, 0x24, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x62, 0x69,
572 0x7a, 0x7a, 0x6c, 0x65, 0x21, 0x0a, 0x00, 0x00, 0x00, 0x00,
574 0x24, 0xbe, 0x05, 0x27, 0x0b, 0x17, 0x00, 0x1f, 0xca, 0xb3, 0x76, 0x40, 0x08, 0x00, 0x45, 0x20,
575 0x00, 0x30, 0x00, 0x00, 0x40, 0x00, 0x36, 0x84, 0xce, 0x18, 0xac, 0x19, 0x09, 0xcc, 0xac, 0x1d,
576 0x14, 0x0f, 0x22, 0xb8, 0x27, 0x0f, 0xb6, 0x96, 0xb0, 0x9e, 0xfa, 0x49, 0x94, 0x3a, 0x03, 0x00,
577 0x00, 0x10, 0xdb, 0x85, 0x60, 0x24, 0x00, 0x00, 0xf4, 0x00, 0x00, 0x00, 0x00, 0x00,
578 }, []byte{ // SHUTDOWN
579 0x00, 0x00, 0x0c, 0x9f, 0xf0, 0x1f, 0x24, 0xbe, 0x05, 0x27, 0x0b, 0x17, 0x08, 0x00, 0x45, 0x02,
580 0x00, 0x28, 0x00, 0x00, 0x40, 0x00, 0x40, 0x84, 0xc4, 0x3e, 0xac, 0x1d, 0x14, 0x0f, 0xac, 0x19,
581 0x09, 0xcc, 0x27, 0x0f, 0x22, 0xb8, 0x32, 0x80, 0xfb, 0x42, 0x3f, 0x29, 0x59, 0x23, 0x07, 0x00,
582 0x00, 0x08, 0x85, 0x98, 0xb1, 0x25, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
583 }, []byte{ // SHUTDOWN ACK
584 0x24, 0xbe, 0x05, 0x27, 0x0b, 0x17, 0x00, 0x1f, 0xca, 0xb3, 0x76, 0x40, 0x08, 0x00, 0x45, 0x20,
585 0x00, 0x24, 0x00, 0x00, 0x40, 0x00, 0x36, 0x84, 0xce, 0x24, 0xac, 0x19, 0x09, 0xcc, 0xac, 0x1d,
586 0x14, 0x0f, 0x22, 0xb8, 0x27, 0x0f, 0xb6, 0x96, 0xb0, 0x9e, 0xb2, 0xc8, 0x99, 0x24, 0x08, 0x00,
587 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
588 }, []byte{ // SHUTDOWN COMPLETE
589 0x00, 0x00, 0x0c, 0x9f, 0xf0, 0x1f, 0x24, 0xbe, 0x05, 0x27, 0x0b, 0x17, 0x08, 0x00, 0x45, 0x02,
590 0x00, 0x24, 0x00, 0x00, 0x40, 0x00, 0x40, 0x84, 0xc4, 0x42, 0xac, 0x1d, 0x14, 0x0f, 0xac, 0x19,
591 0x09, 0xcc, 0x27, 0x0f, 0x22, 0xb8, 0x32, 0x80, 0xfb, 0x42, 0xa8, 0xd1, 0x86, 0x85, 0x0e, 0x00,
592 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
594 wantLayers := [][]gopacket.LayerType{
595 []gopacket.LayerType{LayerTypeSCTPInit},
596 []gopacket.LayerType{LayerTypeSCTPInitAck},
597 []gopacket.LayerType{LayerTypeSCTPCookieEcho, LayerTypeSCTPData, gopacket.LayerTypePayload},
598 []gopacket.LayerType{LayerTypeSCTPCookieAck, LayerTypeSCTPSack},
599 []gopacket.LayerType{LayerTypeSCTPData, gopacket.LayerTypePayload},
600 []gopacket.LayerType{LayerTypeSCTPSack},
601 []gopacket.LayerType{LayerTypeSCTPShutdown},
602 []gopacket.LayerType{LayerTypeSCTPShutdownAck},
603 []gopacket.LayerType{LayerTypeSCTPShutdownComplete},
605 for i, data := range sctpPackets {
606 p := gopacket.NewPacket(data, LinkTypeEthernet, testDecodeOptions)
607 for _, typ := range wantLayers[i] {
608 if p.Layer(typ) == nil {
609 t.Errorf("Packet %d missing layer type %v, got:", i, typ)
610 for _, layer := range p.Layers() {
611 t.Errorf("\t%v", layer.LayerType())
613 if p.ErrorLayer() != nil {
614 t.Error("\tPacket layer error:", p.ErrorLayer().Error())
618 // Test re-serialization.
619 testSerializationWithOpts(t, p, data, gopacket.SerializeOptions{FixLengths: true, ComputeChecksums: true})
623 func TestDecodeCiscoDiscovery(t *testing.T) {
624 // http://wiki.wireshark.org/SampleCaptures?action=AttachFile&do=get&target=cdp_v2.pcap
626 0x01, 0x00, 0x0c, 0xcc, 0xcc, 0xcc, 0x00, 0x0b, 0xbe, 0x18, 0x9a, 0x41, 0x01, 0xc3, 0xaa, 0xaa,
627 0x03, 0x00, 0x00, 0x0c, 0x20, 0x00, 0x02, 0xb4, 0x09, 0xa0, 0x00, 0x01, 0x00, 0x0c, 0x6d, 0x79,
628 0x73, 0x77, 0x69, 0x74, 0x63, 0x68, 0x00, 0x02, 0x00, 0x11, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01,
629 0xcc, 0x00, 0x04, 0xc0, 0xa8, 0x00, 0xfd, 0x00, 0x03, 0x00, 0x13, 0x46, 0x61, 0x73, 0x74, 0x45,
630 0x74, 0x68, 0x65, 0x72, 0x6e, 0x65, 0x74, 0x30, 0x2f, 0x31, 0x00, 0x04, 0x00, 0x08, 0x00, 0x00,
631 0x00, 0x28, 0x00, 0x05, 0x01, 0x14, 0x43, 0x69, 0x73, 0x63, 0x6f, 0x20, 0x49, 0x6e, 0x74, 0x65,
632 0x72, 0x6e, 0x65, 0x74, 0x77, 0x6f, 0x72, 0x6b, 0x20, 0x4f, 0x70, 0x65, 0x72, 0x61, 0x74, 0x69,
633 0x6e, 0x67, 0x20, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x20, 0x53, 0x6f, 0x66, 0x74, 0x77, 0x61,
634 0x72, 0x65, 0x20, 0x0a, 0x49, 0x4f, 0x53, 0x20, 0x28, 0x74, 0x6d, 0x29, 0x20, 0x43, 0x32, 0x39,
635 0x35, 0x30, 0x20, 0x53, 0x6f, 0x66, 0x74, 0x77, 0x61, 0x72, 0x65, 0x20, 0x28, 0x43, 0x32, 0x39,
636 0x35, 0x30, 0x2d, 0x49, 0x36, 0x4b, 0x32, 0x4c, 0x32, 0x51, 0x34, 0x2d, 0x4d, 0x29, 0x2c, 0x20,
637 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x20, 0x31, 0x32, 0x2e, 0x31, 0x28, 0x32, 0x32, 0x29,
638 0x45, 0x41, 0x31, 0x34, 0x2c, 0x20, 0x52, 0x45, 0x4c, 0x45, 0x41, 0x53, 0x45, 0x20, 0x53, 0x4f,
639 0x46, 0x54, 0x57, 0x41, 0x52, 0x45, 0x20, 0x28, 0x66, 0x63, 0x31, 0x29, 0x0a, 0x54, 0x65, 0x63,
640 0x68, 0x6e, 0x69, 0x63, 0x61, 0x6c, 0x20, 0x53, 0x75, 0x70, 0x70, 0x6f, 0x72, 0x74, 0x3a, 0x20,
641 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f, 0x77, 0x77, 0x77, 0x2e, 0x63, 0x69, 0x73, 0x63, 0x6f,
642 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x74, 0x65, 0x63, 0x68, 0x73, 0x75, 0x70, 0x70, 0x6f, 0x72, 0x74,
643 0x0a, 0x43, 0x6f, 0x70, 0x79, 0x72, 0x69, 0x67, 0x68, 0x74, 0x20, 0x28, 0x63, 0x29, 0x20, 0x31,
644 0x39, 0x38, 0x36, 0x2d, 0x32, 0x30, 0x31, 0x30, 0x20, 0x62, 0x79, 0x20, 0x63, 0x69, 0x73, 0x63,
645 0x6f, 0x20, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x73, 0x2c, 0x20, 0x49, 0x6e, 0x63, 0x2e, 0x0a,
646 0x43, 0x6f, 0x6d, 0x70, 0x69, 0x6c, 0x65, 0x64, 0x20, 0x54, 0x75, 0x65, 0x20, 0x32, 0x36, 0x2d,
647 0x4f, 0x63, 0x74, 0x2d, 0x31, 0x30, 0x20, 0x31, 0x30, 0x3a, 0x33, 0x35, 0x20, 0x62, 0x79, 0x20,
648 0x6e, 0x62, 0x75, 0x72, 0x72, 0x61, 0x00, 0x06, 0x00, 0x15, 0x63, 0x69, 0x73, 0x63, 0x6f, 0x20,
649 0x57, 0x53, 0x2d, 0x43, 0x32, 0x39, 0x35, 0x30, 0x2d, 0x31, 0x32, 0x00, 0x08, 0x00, 0x24, 0x00,
650 0x00, 0x0c, 0x01, 0x12, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x01, 0x02, 0x20, 0xff,
651 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0b, 0xbe, 0x18, 0x9a, 0x40, 0xff, 0x00, 0x00, 0x00,
652 0x09, 0x00, 0x0c, 0x4d, 0x59, 0x44, 0x4f, 0x4d, 0x41, 0x49, 0x4e, 0x00, 0x0a, 0x00, 0x06, 0x00,
653 0x01, 0x00, 0x0b, 0x00, 0x05, 0x01, 0x00, 0x12, 0x00, 0x05, 0x00, 0x00, 0x13, 0x00, 0x05, 0x00,
654 0x00, 0x16, 0x00, 0x11, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0xcc, 0x00, 0x04, 0xc0, 0xa8, 0x00,
657 p := gopacket.NewPacket(data, LinkTypeEthernet, testDecodeOptions)
658 wantLayers := []gopacket.LayerType{LayerTypeEthernet, LayerTypeLLC, LayerTypeSNAP, LayerTypeCiscoDiscovery, LayerTypeCiscoDiscoveryInfo}
659 checkLayers(p, wantLayers, t)
661 want := &CiscoDiscoveryInfo{
663 OUI: []byte{0, 0, 12},
665 ClusterMaster: []byte{0, 0, 0, 0},
666 Unknown1: []byte{255, 255, 255, 255},
671 ClusterCommander: net.HardwareAddr{0, 0, 0, 0, 0, 0},
672 SwitchMAC: net.HardwareAddr{0, 0x0b, 0xbe, 0x18, 0x9a, 0x40},
676 DeviceID: "myswitch",
677 Addresses: []net.IP{net.IPv4(192, 168, 0, 253)},
678 PortID: "FastEthernet0/1",
679 Capabilities: CDPCapabilities{false, false, false, true, false, true, false, false, false},
680 Version: "Cisco Internetwork Operating System Software \nIOS (tm) C2950 Software (C2950-I6K2L2Q4-M), Version 12.1(22)EA14, RELEASE SOFTWARE (fc1)\nTechnical Support: http://www.cisco.com/techsupport\nCopyright (c) 1986-2010 by cisco Systems, Inc.\nCompiled Tue 26-Oct-10 10:35 by nburra",
681 Platform: "cisco WS-C2950-12",
682 VTPDomain: "MYDOMAIN",
685 MgmtAddresses: []net.IP{net.IPv4(192, 168, 0, 253)},
686 BaseLayer: BaseLayer{Contents: data[26:]},
688 cdpL := p.Layer(LayerTypeCiscoDiscoveryInfo)
689 info, _ := cdpL.(*CiscoDiscoveryInfo)
690 if !reflect.DeepEqual(info, want) {
691 t.Errorf("Values mismatch, \ngot %#v\nwant %#v\n", info, want)
695 func TestDecodeLinkLayerDiscovery(t *testing.T) {
696 // http://wiki.wireshark.org/SampleCaptures?action=AttachFile&do=get&target=lldp.detailed.pcap
698 0x01, 0x80, 0xc2, 0x00, 0x00, 0x0e, 0x00, 0x01, 0x30, 0xf9, 0xad, 0xa0,
699 0x88, 0xcc, 0x02, 0x07, 0x04, 0x00, 0x01, 0x30, 0xf9, 0xad, 0xa0, 0x04,
700 0x04, 0x05, 0x31, 0x2f, 0x31, 0x06, 0x02, 0x00, 0x78, 0x08, 0x17, 0x53,
701 0x75, 0x6d, 0x6d, 0x69, 0x74, 0x33, 0x30, 0x30, 0x2d, 0x34, 0x38, 0x2d,
702 0x50, 0x6f, 0x72, 0x74, 0x20, 0x31, 0x30, 0x30, 0x31, 0x00, 0x0a, 0x0d,
703 0x53, 0x75, 0x6d, 0x6d, 0x69, 0x74, 0x33, 0x30, 0x30, 0x2d, 0x34, 0x38,
704 0x00, 0x0c, 0x4c, 0x53, 0x75, 0x6d, 0x6d, 0x69, 0x74, 0x33, 0x30, 0x30,
705 0x2d, 0x34, 0x38, 0x20, 0x2d, 0x20, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f,
706 0x6e, 0x20, 0x37, 0x2e, 0x34, 0x65, 0x2e, 0x31, 0x20, 0x28, 0x42, 0x75,
707 0x69, 0x6c, 0x64, 0x20, 0x35, 0x29, 0x20, 0x62, 0x79, 0x20, 0x52, 0x65,
708 0x6c, 0x65, 0x61, 0x73, 0x65, 0x5f, 0x4d, 0x61, 0x73, 0x74, 0x65, 0x72,
709 0x20, 0x30, 0x35, 0x2f, 0x32, 0x37, 0x2f, 0x30, 0x35, 0x20, 0x30, 0x34,
710 0x3a, 0x35, 0x33, 0x3a, 0x31, 0x31, 0x00, 0x0e, 0x04, 0x00, 0x14, 0x00,
711 0x14, 0x10, 0x0e, 0x07, 0x06, 0x00, 0x01, 0x30, 0xf9, 0xad, 0xa0, 0x02,
712 0x00, 0x00, 0x03, 0xe9, 0x00, 0xfe, 0x07, 0x00, 0x12, 0x0f, 0x02, 0x07,
713 0x01, 0x00, 0xfe, 0x09, 0x00, 0x12, 0x0f, 0x01, 0x03, 0x6c, 0x00, 0x00,
714 0x10, 0xfe, 0x09, 0x00, 0x12, 0x0f, 0x03, 0x01, 0x00, 0x00, 0x00, 0x00,
715 0xfe, 0x06, 0x00, 0x12, 0x0f, 0x04, 0x05, 0xf2, 0xfe, 0x06, 0x00, 0x80,
716 0xc2, 0x01, 0x01, 0xe8, 0xfe, 0x07, 0x00, 0x80, 0xc2, 0x02, 0x01, 0x00,
717 0x00, 0xfe, 0x17, 0x00, 0x80, 0xc2, 0x03, 0x01, 0xe8, 0x10, 0x76, 0x32,
718 0x2d, 0x30, 0x34, 0x38, 0x38, 0x2d, 0x30, 0x33, 0x2d, 0x30, 0x35, 0x30,
719 0x35, 0x00, 0xfe, 0x05, 0x00, 0x80, 0xc2, 0x04, 0x00, 0x00, 0x00,
722 p := gopacket.NewPacket(data, LinkTypeEthernet, testDecodeOptions)
723 wantLayers := []gopacket.LayerType{LayerTypeEthernet, LayerTypeLinkLayerDiscovery, LayerTypeLinkLayerDiscoveryInfo}
724 checkLayers(p, wantLayers, t)
725 lldpL := p.Layer(LayerTypeLinkLayerDiscovery)
726 lldp := lldpL.(*LinkLayerDiscovery)
727 want := &LinkLayerDiscovery{
728 ChassisID: LLDPChassisID{LLDPChassisIDSubTypeMACAddr, []byte{0x00, 0x01, 0x30, 0xf9, 0xad, 0xa0}},
729 PortID: LLDPPortID{LLDPPortIDSubtypeIfaceName, []byte("1/1")},
731 BaseLayer: BaseLayer{Contents: data[14:]},
733 lldp.Values = nil // test these in next stage
734 if !reflect.DeepEqual(lldp, want) {
735 t.Errorf("Values mismatch, \ngot %#v\nwant %#v\n", lldp, want)
738 infoL := p.Layer(LayerTypeLinkLayerDiscoveryInfo)
739 info := infoL.(*LinkLayerDiscoveryInfo)
740 wantinfo := &LinkLayerDiscoveryInfo{
741 PortDescription: "Summit300-48-Port 1001\x00",
742 SysName: "Summit300-48\x00",
743 SysDescription: "Summit300-48 - Version 7.4e.1 (Build 5) by Release_Master 05/27/05 04:53:11\x00",
744 SysCapabilities: LLDPSysCapabilities{
745 SystemCap: LLDPCapabilities{Bridge: true, Router: true},
746 EnabledCap: LLDPCapabilities{Bridge: true, Router: true},
748 MgmtAddress: LLDPMgmtAddress{IANAAddressFamily802, []byte{0x00, 0x01, 0x30, 0xf9, 0xad, 0xa0}, LLDPInterfaceSubtypeifIndex, 1001, ""},
749 OrgTLVs: []LLDPOrgSpecificTLV{
750 LLDPOrgSpecificTLV{OUI: 0x120f, SubType: 0x2, Info: []uint8{0x7, 0x1, 0x0}},
751 LLDPOrgSpecificTLV{OUI: 0x120f, SubType: 0x1, Info: []uint8{0x3, 0x6c, 0x0, 0x0, 0x10}},
752 LLDPOrgSpecificTLV{OUI: 0x120f, SubType: 0x3, Info: []uint8{0x1, 0x0, 0x0, 0x0, 0x0}},
753 LLDPOrgSpecificTLV{OUI: 0x120f, SubType: 0x4, Info: []uint8{0x5, 0xf2}},
754 LLDPOrgSpecificTLV{OUI: 0x80c2, SubType: 0x1, Info: []uint8{0x1, 0xe8}},
755 LLDPOrgSpecificTLV{OUI: 0x80c2, SubType: 0x2, Info: []uint8{0x1, 0x0, 0x0}},
756 LLDPOrgSpecificTLV{OUI: 0x80c2, SubType: 0x3, Info: []uint8{0x1, 0xe8, 0x10, 0x76, 0x32, 0x2d, 0x30, 0x34, 0x38, 0x38, 0x2d, 0x30, 0x33, 0x2d, 0x30, 0x35, 0x30, 0x35, 0x0}},
757 LLDPOrgSpecificTLV{OUI: 0x80c2, SubType: 0x4, Info: []uint8{0x0}},
761 if !reflect.DeepEqual(info, wantinfo) {
762 t.Errorf("Values mismatch, \ngot %#v\nwant %#v\n", info, wantinfo)
764 info8021, err := info.Decode8021()
766 t.Errorf("8021 Values decode error: %v", err)
768 want8021 := LLDPInfo8021{
770 PPVIDs: []PortProtocolVLANID{PortProtocolVLANID{false, false, 0}},
771 VLANNames: []VLANName{VLANName{488, "v2-0488-03-0505\x00"}},
772 ProtocolIdentities: nil,
775 LinkAggregation: LLDPLinkAggregation{false, false, 0},
777 if !reflect.DeepEqual(info8021, want8021) {
778 t.Errorf("Values mismatch, \ngot %#v\nwant %#v\n", info8021, want8021)
780 info8023, err := info.Decode8023()
782 t.Errorf("8023 Values decode error: %v", err)
784 want8023 := LLDPInfo8023{
785 LinkAggregation: LLDPLinkAggregation{true, false, 0},
786 MACPHYConfigStatus: LLDPMACPHYConfigStatus{true, true, 0x6c00, 0x0010},
787 PowerViaMDI: LLDPPowerViaMDI8023{true, true, true, false, 1, 0, 0, 0, 0, 0, 0},
791 if !reflect.DeepEqual(info8023, want8023) {
792 t.Errorf("Values mismatch, \ngot %#v\nwant %#v\n", info8023, want8023)
795 // http://wiki.wireshark.org/SampleCaptures?action=AttachFile&do=get&target=lldpmed_civicloc.pcap
797 0x01, 0x80, 0xc2, 0x00, 0x00, 0x0e, 0x00, 0x13, 0x21, 0x57, 0xca, 0x7f,
798 0x88, 0xcc, 0x02, 0x07, 0x04, 0x00, 0x13, 0x21, 0x57, 0xca, 0x40, 0x04,
799 0x02, 0x07, 0x31, 0x06, 0x02, 0x00, 0x78, 0x08, 0x01, 0x31, 0x0a, 0x1a,
800 0x50, 0x72, 0x6f, 0x43, 0x75, 0x72, 0x76, 0x65, 0x20, 0x53, 0x77, 0x69,
801 0x74, 0x63, 0x68, 0x20, 0x32, 0x36, 0x30, 0x30, 0x2d, 0x38, 0x2d, 0x50,
802 0x57, 0x52, 0x0c, 0x5f, 0x50, 0x72, 0x6f, 0x43, 0x75, 0x72, 0x76, 0x65,
803 0x20, 0x4a, 0x38, 0x37, 0x36, 0x32, 0x41, 0x20, 0x53, 0x77, 0x69, 0x74,
804 0x63, 0x68, 0x20, 0x32, 0x36, 0x30, 0x30, 0x2d, 0x38, 0x2d, 0x50, 0x57,
805 0x52, 0x2c, 0x20, 0x72, 0x65, 0x76, 0x69, 0x73, 0x69, 0x6f, 0x6e, 0x20,
806 0x48, 0x2e, 0x30, 0x38, 0x2e, 0x38, 0x39, 0x2c, 0x20, 0x52, 0x4f, 0x4d,
807 0x20, 0x48, 0x2e, 0x30, 0x38, 0x2e, 0x35, 0x58, 0x20, 0x28, 0x2f, 0x73,
808 0x77, 0x2f, 0x63, 0x6f, 0x64, 0x65, 0x2f, 0x62, 0x75, 0x69, 0x6c, 0x64,
809 0x2f, 0x66, 0x69, 0x73, 0x68, 0x28, 0x74, 0x73, 0x5f, 0x30, 0x38, 0x5f,
810 0x35, 0x29, 0x29, 0x0e, 0x04, 0x00, 0x14, 0x00, 0x04, 0x10, 0x0c, 0x05,
811 0x01, 0x0f, 0xff, 0x7a, 0x94, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfe,
812 0x09, 0x00, 0x12, 0x0f, 0x01, 0x03, 0x6c, 0x00, 0x00, 0x10, 0xfe, 0x07,
813 0x00, 0x12, 0xbb, 0x01, 0x00, 0x0f, 0x04, 0xfe, 0x08, 0x00, 0x12, 0xbb,
814 0x02, 0x01, 0x40, 0x65, 0xae, 0xfe, 0x2e, 0x00, 0x12, 0xbb, 0x03, 0x02,
815 0x28, 0x02, 0x55, 0x53, 0x01, 0x02, 0x43, 0x41, 0x03, 0x09, 0x52, 0x6f,
816 0x73, 0x65, 0x76, 0x69, 0x6c, 0x6c, 0x65, 0x06, 0x09, 0x46, 0x6f, 0x6f,
817 0x74, 0x68, 0x69, 0x6c, 0x6c, 0x73, 0x13, 0x04, 0x38, 0x30, 0x30, 0x30,
818 0x1a, 0x03, 0x52, 0x33, 0x4c, 0xfe, 0x07, 0x00, 0x12, 0xbb, 0x04, 0x03,
819 0x00, 0x41, 0x00, 0x00,
822 p = gopacket.NewPacket(data, LinkTypeEthernet, testDecodeOptions)
823 wantLayers = []gopacket.LayerType{LayerTypeEthernet, LayerTypeLinkLayerDiscovery, LayerTypeLinkLayerDiscoveryInfo}
824 checkLayers(p, wantLayers, t)
825 lldpL = p.Layer(LayerTypeLinkLayerDiscovery)
826 lldp = lldpL.(*LinkLayerDiscovery)
827 want = &LinkLayerDiscovery{
828 ChassisID: LLDPChassisID{LLDPChassisIDSubTypeMACAddr, []byte{0x00, 0x13, 0x21, 0x57, 0xca, 0x40}},
829 PortID: LLDPPortID{LLDPPortIDSubtypeLocal, []byte("1")},
831 BaseLayer: BaseLayer{Contents: data[14:]},
833 lldp.Values = nil // test these in next stage
834 if !reflect.DeepEqual(lldp, want) {
835 t.Errorf("Values mismatch, \ngot %#v\nwant %#v\n", lldp, want)
838 infoL = p.Layer(LayerTypeLinkLayerDiscoveryInfo)
839 info = infoL.(*LinkLayerDiscoveryInfo)
840 wantinfo = &LinkLayerDiscoveryInfo{
841 PortDescription: "1",
842 SysName: "ProCurve Switch 2600-8-PWR",
843 SysDescription: "ProCurve J8762A Switch 2600-8-PWR, revision H.08.89, ROM H.08.5X (/sw/code/build/fish(ts_08_5))",
844 SysCapabilities: LLDPSysCapabilities{
845 SystemCap: LLDPCapabilities{Bridge: true, Router: true},
846 EnabledCap: LLDPCapabilities{Bridge: true},
848 MgmtAddress: LLDPMgmtAddress{IANAAddressFamilyIPV4, []byte{0x0f, 0xff, 0x7a, 0x94}, LLDPInterfaceSubtypeifIndex, 0, ""},
849 OrgTLVs: []LLDPOrgSpecificTLV{
850 LLDPOrgSpecificTLV{OUI: 0x120f, SubType: 0x1, Info: []uint8{0x3, 0x6c, 0x0, 0x0, 0x10}},
851 LLDPOrgSpecificTLV{OUI: 0x12bb, SubType: 0x1, Info: []uint8{0x0, 0xf, 0x4}},
852 LLDPOrgSpecificTLV{OUI: 0x12bb, SubType: 0x2, Info: []uint8{0x1, 0x40, 0x65, 0xae}},
853 LLDPOrgSpecificTLV{OUI: 0x12bb, SubType: 0x3, Info: []uint8{0x2, 0x28, 0x2, 0x55, 0x53, 0x1, 0x2, 0x43, 0x41, 0x3, 0x9, 0x52, 0x6f, 0x73, 0x65, 0x76, 0x69, 0x6c, 0x6c, 0x65, 0x6, 0x9, 0x46, 0x6f, 0x6f, 0x74, 0x68, 0x69, 0x6c, 0x6c, 0x73, 0x13, 0x4, 0x38, 0x30, 0x30, 0x30, 0x1a, 0x3, 0x52, 0x33, 0x4c}},
854 LLDPOrgSpecificTLV{OUI: 0x12bb, SubType: 0x4, Info: []uint8{0x3, 0x0, 0x41}},
858 if !reflect.DeepEqual(info, wantinfo) {
859 t.Errorf("Values mismatch, \ngot %#v\nwant %#v\n", info, wantinfo)
861 info8023, err = info.Decode8023()
863 t.Errorf("8023 Values decode error: %v", err)
865 want8023 = LLDPInfo8023{
866 MACPHYConfigStatus: LLDPMACPHYConfigStatus{true, true, 0x6c00, 0x0010},
869 if !reflect.DeepEqual(info8023, want8023) {
870 t.Errorf("Values mismatch, \ngot %#v\nwant %#v\n", info8023, want8023)
873 infoMedia, err := info.DecodeMedia()
875 t.Errorf("8023 Values decode error: %v", err)
877 wantMedia := LLDPInfoMedia{
878 MediaCapabilities: LLDPMediaCapabilities{true, true, true, true, false, false, LLDPMediaClassNetwork},
879 NetworkPolicy: LLDPNetworkPolicy{LLDPAppTypeVoice, true, true, 50, 6, 46},
880 Location: LLDPLocation{Format: LLDPLocationFormatAddress, Address: LLDPLocationAddress{
881 What: LLDPLocationAddressWhatClient,
883 AddressLines: []LLDPLocationAddressLine{
884 LLDPLocationAddressLine{LLDPLocationAddressTypeNational, "CA"},
885 LLDPLocationAddressLine{LLDPLocationAddressTypeCity, "Roseville"},
886 LLDPLocationAddressLine{LLDPLocationAddressTypeStreet, "Foothills"},
887 LLDPLocationAddressLine{LLDPLocationAddressTypeHouseNum, "8000"},
888 LLDPLocationAddressLine{LLDPLocationAddressTypeUnit, "R3L"},
891 PowerViaMDI: LLDPPowerViaMDI{0, 0, LLDPPowerPriorityLow, 6500},
894 if !reflect.DeepEqual(infoMedia, wantMedia) {
895 t.Errorf("Values mismatch, \ngot %#v\nwant %#v\n", infoMedia, wantMedia)
900 func TestDecodeNortelDiscovery(t *testing.T) {
901 // http://www.thetechfirm.com/packets/nortel_btdp/btdp_nai.enc
903 0x01, 0x00, 0x81, 0x00, 0x01, 0x00, 0x00, 0x04, 0x38, 0xe0, 0xcc, 0xde,
904 0x00, 0x13, 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x81, 0x01, 0xa2, 0xac, 0x13,
905 0x58, 0x03, 0x00, 0x04, 0x15, 0x30, 0x0c, 0x02, 0x00, 0x00, 0x00, 0x00,
906 0x00, 0x04, 0x38, 0xe0, 0xcc, 0xde, 0x80, 0x6a, 0x00, 0x01, 0x14, 0x00,
907 0x02, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
909 p := gopacket.NewPacket(data, LinkTypeEthernet, testDecodeOptions)
910 wantLayers := []gopacket.LayerType{LayerTypeEthernet, LayerTypeLLC, LayerTypeSNAP, LayerTypeNortelDiscovery}
911 checkLayers(p, wantLayers, t)
913 want := &NortelDiscovery{
914 IPAddress: []byte{172, 19, 88, 3},
915 SegmentID: []byte{0x00, 0x04, 0x15},
916 Chassis: NDPChassisBayStack450101001000Switches,
917 Backplane: NDPBackplaneEthernetFastEthernetGigabitEthernet,
918 State: NDPStateHeartbeat,
921 ndpL := p.Layer(LayerTypeNortelDiscovery)
922 info, _ := ndpL.(*NortelDiscovery)
923 if !reflect.DeepEqual(info, want) {
924 t.Errorf("Values mismatch, \ngot %#v\nwant %#v\n", info, want)
928 func TestDecodeIPv6Jumbogram(t *testing.T) {
929 // Haven't found any of these in the wild or on example pcaps online, so had
930 // to generate one myself via scapy. Unfortunately, scapy can only
931 // str(packet) for packets with length < 65536, due to limitations in python's
932 // struct library, so I generated the header with:
933 // Ether() / IPv6(src='::1', dst='::2') / IPv6ExtHdrHopByHop(options=[Jumbo(jumboplen=70000)]) / TCP(sport=8888, dport=80)
934 // then added the payload manually ("payload" * 9996). The checksums here are
935 // not correct, but we don't check, so who cares ;)
936 dataStr := "\x00\x1f\xca\xb3v@$\xbe\x05'\x0b\x17\x86\xdd`\x00\x00\x00\x00\x00\x00@\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x06\x00\xc2\x04\x00\x01\x11p\"\xb8\x00P\x00\x00\x00\x00\x00\x00\x00\x00P\x02 \x00l\xd8\x00\x00"
937 payload := strings.Repeat("payload", 9996)
938 data := []byte(dataStr + payload)
939 p := gopacket.NewPacket(data, LinkTypeEthernet, testDecodeOptions)
940 checkLayers(p, []gopacket.LayerType{LayerTypeEthernet, LayerTypeIPv6, LayerTypeIPv6HopByHop, LayerTypeTCP, gopacket.LayerTypePayload}, t)
941 if p.ApplicationLayer() == nil {
942 t.Error("Packet has no application layer")
943 } else if string(p.ApplicationLayer().Payload()) != payload {
944 t.Errorf("Jumbogram payload wrong")
946 // Check truncated for jumbograms
947 data = data[:len(data)-1]
948 p = gopacket.NewPacket(data, LinkTypeEthernet, testDecodeOptions)
949 checkLayers(p, []gopacket.LayerType{LayerTypeEthernet, LayerTypeIPv6, LayerTypeIPv6HopByHop, LayerTypeTCP, gopacket.LayerTypePayload}, t)
950 if !p.Metadata().Truncated {
951 t.Error("Jumbogram should be truncated")
955 func TestDecodeUDPPacketTooSmall(t *testing.T) {
957 0x00, 0x15, 0x2c, 0x9d, 0xcc, 0x00, 0x00, 0x10, 0xdb, 0xff, 0x10, 0x00, 0x81, 0x00, 0x01, 0xf7,
958 0x08, 0x00, 0x45, 0x60, 0x00, 0x3c, 0x0f, 0xa9, 0x00, 0x00, 0x6e, 0x11, 0x01, 0x0a, 0x47, 0xe6,
959 0xee, 0x2e, 0xac, 0x16, 0x59, 0x73, 0x00, 0x50, 0x00, 0x50, 0x00, 0x28, 0x4d, 0xad, 0x00, 0x67,
960 0x00, 0x01, 0x00, 0x72, 0xd5, 0xc7, 0xf1, 0x07, 0x00, 0x00, 0x01, 0x01, 0x00, 0x0d, 0x00, 0x00,
961 0x00, 0x14, 0x00, 0x00, 0x19, 0xba,
963 p := gopacket.NewPacket(data, LinkTypeEthernet, testDecodeOptions)
964 checkLayers(p, []gopacket.LayerType{LayerTypeEthernet, LayerTypeDot1Q, LayerTypeIPv4, LayerTypeUDP, gopacket.LayerTypePayload}, t)
965 if !p.Metadata().Truncated {
966 t.Error("UDP short packet should be truncated")
970 func TestDecodingLayerParserFullTCPPacket(t *testing.T) {
971 dlp := gopacket.NewDecodingLayerParser(LayerTypeEthernet, &Ethernet{}, &IPv4{}, &TCP{}, &gopacket.Payload{})
972 decoded := make([]gopacket.LayerType, 1)
973 err := dlp.DecodeLayers(testSimpleTCPPacket, &decoded)
975 t.Error("Error from dlp parser: ", err)
977 if len(decoded) != 4 {
978 t.Error("Expected 4 layers parsed, instead got ", len(decoded))
982 // testICMP is the packet:
983 // 15:49:15.773265 IP 72.14.222.226 > 172.29.20.15: ICMP host 10.66.73.201 unreachable - admin prohibited filter, length 36
984 // 0x0000: 24be 0527 0b17 001f cab3 75c0 0800 4500 $..'......u...E.
985 // 0x0010: 0038 0000 0000 fc01 d7a7 480e dee2 ac1d .8........H.....
986 // 0x0020: 140f 030d 946e 0000 0000 4520 004d 0000 .....n....E..M..
987 // 0x0030: 4000 3e11 2849 ac1d 140f 0a42 49c9 8ecc @.>.(I.....BI...
988 // 0x0040: 62e1 0039 769d b..9v.
989 var testICMP = []byte{
990 0x24, 0xbe, 0x05, 0x27, 0x0b, 0x17, 0x00, 0x1f, 0xca, 0xb3, 0x75, 0xc0, 0x08, 0x00, 0x45, 0x00,
991 0x00, 0x38, 0x00, 0x00, 0x00, 0x00, 0xfc, 0x01, 0xd7, 0xa7, 0x48, 0x0e, 0xde, 0xe2, 0xac, 0x1d,
992 0x14, 0x0f, 0x03, 0x0d, 0x94, 0x6e, 0x00, 0x00, 0x00, 0x00, 0x45, 0x20, 0x00, 0x4d, 0x00, 0x00,
993 0x40, 0x00, 0x3e, 0x11, 0x28, 0x49, 0xac, 0x1d, 0x14, 0x0f, 0x0a, 0x42, 0x49, 0xc9, 0x8e, 0xcc,
994 0x62, 0xe1, 0x00, 0x39, 0x76, 0x9d,
997 func TestICMP(t *testing.T) {
998 p := gopacket.NewPacket(testICMP, LinkTypeEthernet, testDecodeOptions)
999 if p.ErrorLayer() != nil {
1000 t.Error("Failed to decode packet:", p.ErrorLayer().Error())
1002 checkLayers(p, []gopacket.LayerType{LayerTypeEthernet, LayerTypeIPv4, LayerTypeICMPv4, gopacket.LayerTypePayload}, t)
1003 testSerialization(t, p, testICMP)
1005 func BenchmarkDecodeICMP(b *testing.B) {
1006 for i := 0; i < b.N; i++ {
1007 gopacket.NewPacket(testICMP, LinkTypeEthernet, gopacket.NoCopy)
1011 // testICMP6 is the packet:
1012 // 16:17:37.758937 IP6 fe80::21f:caff:feb3:75c0 > 2620:0:1005:0:26be:5ff:fe27:b17: ICMP6, neighbor solicitation, who has 2620:0:1005:0:26be:5ff:fe27:b17, length 32
1013 // 0x0000: 24be 0527 0b17 001f cab3 75c0 86dd 6e00 $..'......u...n.
1014 // 0x0010: 0000 0020 3aff fe80 0000 0000 0000 021f ....:...........
1015 // 0x0020: caff feb3 75c0 2620 0000 1005 0000 26be ....u.&.......&.
1016 // 0x0030: 05ff fe27 0b17 8700 1eba 0000 0000 2620 ...'..........&.
1017 // 0x0040: 0000 1005 0000 26be 05ff fe27 0b17 0101 ......&....'....
1018 // 0x0050: 001f cab3 75c0 ....u.
1019 var testICMP6 = []byte{
1020 0x24, 0xbe, 0x05, 0x27, 0x0b, 0x17, 0x00, 0x1f, 0xca, 0xb3, 0x75, 0xc0, 0x86, 0xdd, 0x6e, 0x00,
1021 0x00, 0x00, 0x00, 0x20, 0x3a, 0xff, 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x1f,
1022 0xca, 0xff, 0xfe, 0xb3, 0x75, 0xc0, 0x26, 0x20, 0x00, 0x00, 0x10, 0x05, 0x00, 0x00, 0x26, 0xbe,
1023 0x05, 0xff, 0xfe, 0x27, 0x0b, 0x17, 0x87, 0x00, 0x1e, 0xba, 0x00, 0x00, 0x00, 0x00, 0x26, 0x20,
1024 0x00, 0x00, 0x10, 0x05, 0x00, 0x00, 0x26, 0xbe, 0x05, 0xff, 0xfe, 0x27, 0x0b, 0x17, 0x01, 0x01,
1025 0x00, 0x1f, 0xca, 0xb3, 0x75, 0xc0,
1028 func TestICMP6(t *testing.T) {
1029 p := gopacket.NewPacket(testICMP6, LinkTypeEthernet, testDecodeOptions)
1030 if p.ErrorLayer() != nil {
1031 t.Error("Failed to decode packet:", p.ErrorLayer().Error())
1033 checkLayers(p, []gopacket.LayerType{LayerTypeEthernet, LayerTypeIPv6, LayerTypeICMPv6, gopacket.LayerTypePayload}, t)
1034 testSerialization(t, p, testICMP6)
1036 func BenchmarkDecodeICMP6(b *testing.B) {
1037 for i := 0; i < b.N; i++ {
1038 gopacket.NewPacket(testICMP6, LinkTypeEthernet, gopacket.NoCopy)
1042 // testMPLS is the packet:
1043 // 12:48:57.201014 MPLS (label 29, exp 0, [S], ttl 255) IP 10.1.2.1 > 10.34.0.1: ICMP echo request, id 2618, seq 1579, length 80
1044 // 0x0000: 0030 96e6 fc39 0030 9605 2838 8847 0001 .0...9.0..(8.G..
1045 // 0x0010: d1ff 4500 0064 000b 0000 ff01 a569 0a01 ..E..d.......i..
1046 // 0x0020: 0201 0a22 0001 0800 3a76 0a3a 062b 0000 ..."....:v.:.+..
1047 // 0x0030: 0000 001f 3350 abcd abcd abcd abcd abcd ....3P..........
1048 // 0x0040: abcd abcd abcd abcd abcd abcd abcd abcd ................
1049 // 0x0050: abcd abcd abcd abcd abcd abcd abcd abcd ................
1050 // 0x0060: abcd abcd abcd abcd abcd abcd abcd abcd ................
1051 // 0x0070: abcd abcd abcd ......
1052 var testMPLS = []byte{
1053 0x00, 0x30, 0x96, 0xe6, 0xfc, 0x39, 0x00, 0x30, 0x96, 0x05, 0x28, 0x38, 0x88, 0x47, 0x00, 0x01,
1054 0xd1, 0xff, 0x45, 0x00, 0x00, 0x64, 0x00, 0x0b, 0x00, 0x00, 0xff, 0x01, 0xa5, 0x69, 0x0a, 0x01,
1055 0x02, 0x01, 0x0a, 0x22, 0x00, 0x01, 0x08, 0x00, 0x3a, 0x76, 0x0a, 0x3a, 0x06, 0x2b, 0x00, 0x00,
1056 0x00, 0x00, 0x00, 0x1f, 0x33, 0x50, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd,
1057 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd,
1058 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd,
1059 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd,
1060 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd,
1063 func TestMPLS(t *testing.T) {
1064 p := gopacket.NewPacket(testMPLS, LinkTypeEthernet, testDecodeOptions)
1065 if p.ErrorLayer() != nil {
1066 t.Error("Failed to decode packet:", p.ErrorLayer().Error())
1068 checkLayers(p, []gopacket.LayerType{LayerTypeEthernet, LayerTypeMPLS, LayerTypeIPv4, LayerTypeICMPv4, gopacket.LayerTypePayload}, t)
1069 testSerialization(t, p, testMPLS)
1071 func BenchmarkDecodeMPLS(b *testing.B) {
1072 for i := 0; i < b.N; i++ {
1073 gopacket.NewPacket(testMPLS, LinkTypeEthernet, gopacket.NoCopy)
1077 // testPPPoEICMPv6 is the packet:
1078 // 07:43:31.091560 PPPoE [ses 0x11] IP6 fe80::c801:eff:fe88:8 > ff02::1: ICMP6, neighbor advertisement, tgt is fe80::c801:eff:fe88:8, length 24
1079 // 0x0000: cc05 0e88 0000 ca01 0e88 0006 8864 1100 .............d..
1080 // 0x0010: 0011 0042 0057 6e00 0000 0018 3aff fe80 ...B.Wn.....:...
1081 // 0x0020: 0000 0000 0000 c801 0eff fe88 0008 ff02 ................
1082 // 0x0030: 0000 0000 0000 0000 0000 0000 0001 8800 ................
1083 // 0x0040: 5083 8000 0000 fe80 0000 0000 0000 c801 P...............
1084 // 0x0050: 0eff fe88 0008 ......
1085 var testPPPoEICMPv6 = []byte{
1086 0xcc, 0x05, 0x0e, 0x88, 0x00, 0x00, 0xca, 0x01, 0x0e, 0x88, 0x00, 0x06, 0x88, 0x64, 0x11, 0x00,
1087 0x00, 0x11, 0x00, 0x42, 0x00, 0x57, 0x6e, 0x00, 0x00, 0x00, 0x00, 0x18, 0x3a, 0xff, 0xfe, 0x80,
1088 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc8, 0x01, 0x0e, 0xff, 0xfe, 0x88, 0x00, 0x08, 0xff, 0x02,
1089 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x88, 0x00,
1090 0x50, 0x83, 0x80, 0x00, 0x00, 0x00, 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc8, 0x01,
1091 0x0e, 0xff, 0xfe, 0x88, 0x00, 0x08,
1094 func TestPPPoEICMPv6(t *testing.T) {
1095 p := gopacket.NewPacket(testPPPoEICMPv6, LinkTypeEthernet, testDecodeOptions)
1096 if p.ErrorLayer() != nil {
1097 t.Error("Failed to decode packet:", p.ErrorLayer().Error())
1099 checkLayers(p, []gopacket.LayerType{
1105 gopacket.LayerTypePayload,
1107 testSerialization(t, p, testPPPoEICMPv6)
1109 func BenchmarkDecodePPPoEICMPv6(b *testing.B) {
1110 for i := 0; i < b.N; i++ {
1111 gopacket.NewPacket(testPPPoEICMPv6, LinkTypeEthernet, gopacket.NoCopy)
1115 var testPFLogUDP = []byte{
1116 0x3d, 0x02, 0x00, 0x00, 0x65, 0x6e, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1117 0x00, 0x00, 0x00, 0x00, 0x6c, 0x6f, 0x67, 0x67, 0x69, 0x6e, 0x67, 0x00, 0x00, 0x00, 0x00, 0x00,
1118 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xae, 0xff, 0xff, 0xff, 0x7f,
1119 0xa0, 0x86, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0xb8, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
1120 0x45, 0x00, 0x00, 0x2c, 0x02, 0x16, 0x00, 0x00, 0x40, 0x11, 0x4e, 0xb0, 0xac, 0x17, 0xe8, 0xcc,
1121 0xac, 0x17, 0xe8, 0xff, 0xf0, 0xff, 0x21, 0xa4, 0x00, 0x18, 0x2a, 0x25, 0x50, 0x4e, 0x4a, 0x50,
1122 0x01, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1125 func TestPFLogUDP(t *testing.T) {
1126 p := gopacket.NewPacket(testPFLogUDP, LinkTypePFLog, testDecodeOptions)
1127 if p.ErrorLayer() != nil {
1128 t.Error("Failed to decode packet:", p.ErrorLayer().Error())
1130 checkLayers(p, []gopacket.LayerType{
1134 gopacket.LayerTypePayload,
1138 func TestRegressionDot1QPriority(t *testing.T) {
1142 out := gopacket.NewSerializeBuffer()
1143 gopacket.SerializeLayers(out, gopacket.SerializeOptions{}, d)
1144 if err := d.DecodeFromBytes(out.Bytes(), gopacket.NilDecodeFeedback); err != nil {
1145 t.Errorf("could not decode encoded dot1q")
1146 } else if d.Priority != 2 {
1147 t.Errorf("priority mismatch, want 2 got %d", d.Priority)
1151 // testPacketMPLSInMPLS is the packet:
1152 // 15:27:44.753678 MPLS (label 18, exp 0, ttl 255) (label 16, exp 0, [S], ttl
1153 // 255) IP 10.31.0.1 > 10.34.0.1: ICMP echo request, id 3941, seq 4768, length
1155 // 0x0000: 0030 96e6 fc39 0030 9605 2838 8847 0001 .0...9.0..(8.G..
1156 // 0x0010: 20ff 0001 01ff 4500 0064 0050 0000 ff01 ......E..d.P....
1157 // 0x0020: a706 0a1f 0001 0a22 0001 0800 bd11 0f65 .......".......e
1158 // 0x0030: 12a0 0000 0000 0053 9ee0 abcd abcd abcd .......S........
1159 // 0x0040: abcd abcd abcd abcd abcd abcd abcd abcd ................
1160 // 0x0050: abcd abcd abcd abcd abcd abcd abcd abcd ................
1161 // 0x0060: abcd abcd abcd abcd abcd abcd abcd abcd ................
1162 // 0x0070: abcd abcd abcd abcd abcd ..........
1163 var testPacketMPLSInMPLS = []byte{
1164 0x00, 0x30, 0x96, 0xe6, 0xfc, 0x39, 0x00, 0x30, 0x96, 0x05, 0x28, 0x38, 0x88, 0x47, 0x00, 0x01,
1165 0x20, 0xff, 0x00, 0x01, 0x01, 0xff, 0x45, 0x00, 0x00, 0x64, 0x00, 0x50, 0x00, 0x00, 0xff, 0x01,
1166 0xa7, 0x06, 0x0a, 0x1f, 0x00, 0x01, 0x0a, 0x22, 0x00, 0x01, 0x08, 0x00, 0xbd, 0x11, 0x0f, 0x65,
1167 0x12, 0xa0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x53, 0x9e, 0xe0, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd,
1168 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd,
1169 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd,
1170 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd,
1171 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd,
1174 func TestPacketMPLSInMPLS(t *testing.T) {
1175 p := gopacket.NewPacket(testPacketMPLSInMPLS, LinkTypeEthernet, testDecodeOptions)
1176 if p.ErrorLayer() != nil {
1177 t.Error("Failed to decode packet:", p.ErrorLayer().Error())
1179 checkLayers(p, []gopacket.LayerType{
1185 gopacket.LayerTypePayload}, t)
1188 // testPacketIPv4Fragmented is the packet:
1189 // 22:11:26.616090 IP 10.1.1.1.31915 > 129.111.30.27.20197: UDP, length 28
1190 // 0x0000: 0000 39cf d9cd 0040 33d9 7cfd 0800 4500 ..9....@3.|...E.
1191 // 0x0010: 0038 00f2 2000 4011 af37 0a01 0101 816f .8....@..7.....o
1192 // 0x0020: 1e1b 7cab 4ee5 0024 0000 0000 0000 0000 ..|.N..$........
1193 // 0x0030: 0000 0000 0000 0000 0000 0000 0000 0000 ................
1194 // 0x0040: 0000 0000 0000 ......
1195 var testPacketIPv4Fragmented = []byte{
1196 0x00, 0x00, 0x39, 0xcf, 0xd9, 0xcd, 0x00, 0x40, 0x33, 0xd9, 0x7c, 0xfd, 0x08, 0x00, 0x45, 0x00,
1197 0x00, 0x38, 0x00, 0xf2, 0x20, 0x00, 0x40, 0x11, 0xaf, 0x37, 0x0a, 0x01, 0x01, 0x01, 0x81, 0x6f,
1198 0x1e, 0x1b, 0x7c, 0xab, 0x4e, 0xe5, 0x00, 0x24, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1199 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1200 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1203 func TestPacketIPv4Fragmented(t *testing.T) {
1204 p := gopacket.NewPacket(testPacketIPv4Fragmented, LinkTypeEthernet, testDecodeOptions)
1205 if p.ErrorLayer() != nil {
1206 t.Error("Failed to decode packet:", p.ErrorLayer().Error())
1208 checkLayers(p, []gopacket.LayerType{LayerTypeEthernet, LayerTypeIPv4, gopacket.LayerTypeFragment}, t)
1209 testSerializationWithOpts(t, p, testPacketIPv4Fragmented, gopacket.SerializeOptions{FixLengths: true, ComputeChecksums: true})
1212 // TestSCTPChunkBadLength tests for issue #146
1213 func TestSCTPChunkBadLength(t *testing.T) {
1215 "0000\xad9$e\x11\xe4\xaeo\b\x00E\x00\x018\xb4\xa3" +
1216 "\x00\x00Y\x84\xc4@\x11gz\xc0\xa8\xee\x01\xc0\xa8" +
1217 "\xeeD\x007le\x03\x01\xc0\f\xdf\b\x01\x00\x00")
1219 // this panic'd previously due to a zero length chunk getting
1221 gopacket.NewPacket(data, LinkTypeEthernet, gopacket.Default)
1225 func TestSTP(t *testing.T) {
1226 testSTPpacket := []byte{
1227 0x01, 0x80, 0xC2, 0x00, 0x00, 0x00, 0x00, 0x1C, 0x0E, 0x87, 0x85, 0x04, 0x00, 0x26, 0x42, 0x42,
1228 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x64, 0x00, 0x1C, 0x0E, 0x87, 0x78, 0x00, 0x00, 0x00,
1229 0x00, 0x04, 0x80, 0x64, 0x00, 0x1C, 0x0E, 0x87, 0x85, 0x00, 0x80, 0x04, 0x01, 0x00, 0x14, 0x00,
1230 0x02, 0x00, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1232 p := gopacket.NewPacket(testSTPpacket, LinkTypeEthernet, testDecodeOptions)
1233 checkLayers(p, []gopacket.LayerType{LayerTypeEthernet, LayerTypeLLC, LayerTypeSTP}, t)