github.com/gopacket/gopacket@v1.1.0/layers/decode_test.go (about) 1 // Copyright 2012, Google, Inc. All rights reserved. 2 // Copyright 2009-2011 Andreas Krennmair. All rights reserved. 3 // 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 6 // tree. 7 8 package layers 9 10 import ( 11 "bytes" 12 "encoding/hex" 13 "fmt" 14 "net" 15 "reflect" 16 "strings" 17 "testing" 18 19 "github.com/gopacket/gopacket" 20 "github.com/gopacket/gopacket/bytediff" 21 ) 22 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, 60 0x0d, 0x0a, 61 } 62 63 var testDecodeOptions = gopacket.DecodeOptions{ 64 SkipDecodeRecovery: true, 65 } 66 67 // Benchmarks for actual gopacket code 68 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) 73 } 74 } 75 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) 80 } 81 } 82 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) 86 } 87 } 88 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) 92 } 93 } 94 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) 98 } 99 } 100 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() 104 } 105 } 106 107 func BenchmarkDefault(b *testing.B) { 108 for i := 0; i < b.N; i++ { 109 gopacket.NewPacket(testSimpleTCPPacket, LinkTypeEthernet, gopacket.Default) 110 } 111 } 112 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)) 118 } 119 p.Layer(LayerTypeTCP).(*TCP).SetNetworkLayerForChecksum( 120 p.NetworkLayer()) 121 return slayers 122 } 123 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...) 130 } 131 } 132 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...) 139 } 140 } 141 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...) 148 } 149 } 150 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...) 157 } 158 } 159 160 func BenchmarkLazy(b *testing.B) { 161 for i := 0; i < b.N; i++ { 162 gopacket.NewPacket(testSimpleTCPPacket, LinkTypeEthernet, gopacket.Lazy) 163 } 164 } 165 166 func BenchmarkNoCopy(b *testing.B) { 167 for i := 0; i < b.N; i++ { 168 gopacket.NewPacket(testSimpleTCPPacket, LinkTypeEthernet, gopacket.NoCopy) 169 } 170 } 171 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}) 175 } 176 } 177 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() 186 } 187 } 188 } 189 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) 196 } 197 } 198 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) 205 } 206 } 207 208 func benchmarkDecodingLayerParser(b *testing.B, dlc gopacket.DecodingLayerContainer, ignorePanic bool) { 209 decoded := make([]gopacket.LayerType, 0, 20) 210 dlc = dlc.Put(&Ethernet{}) 211 dlc = dlc.Put(&IPv4{}) 212 dlc = dlc.Put(&TCP{}) 213 dlc = dlc.Put(&gopacket.Payload{}) 214 dlp := gopacket.NewDecodingLayerParser(LayerTypeEthernet) 215 dlp.SetDecodingLayerContainer(dlc) 216 dlp.IgnorePanic = ignorePanic 217 for i := 0; i < b.N; i++ { 218 dlp.DecodeLayers(testSimpleTCPPacket, &decoded) 219 } 220 } 221 222 func BenchmarkDecodingLayerParserSparseIgnorePanic(b *testing.B) { 223 benchmarkDecodingLayerParser(b, gopacket.DecodingLayerSparse(nil), true) 224 } 225 226 func BenchmarkDecodingLayerParserSparseHandlePanic(b *testing.B) { 227 benchmarkDecodingLayerParser(b, gopacket.DecodingLayerSparse(nil), false) 228 } 229 230 func BenchmarkDecodingLayerParserArrayIgnorePanic(b *testing.B) { 231 benchmarkDecodingLayerParser(b, gopacket.DecodingLayerArray(nil), true) 232 } 233 234 func BenchmarkDecodingLayerParserArrayHandlePanic(b *testing.B) { 235 benchmarkDecodingLayerParser(b, gopacket.DecodingLayerArray(nil), false) 236 } 237 238 func BenchmarkDecodingLayerParserMapIgnorePanic(b *testing.B) { 239 benchmarkDecodingLayerParser(b, gopacket.DecodingLayerMap(nil), true) 240 } 241 242 func BenchmarkDecodingLayerParserMapHandlePanic(b *testing.B) { 243 benchmarkDecodingLayerParser(b, gopacket.DecodingLayerMap(nil), false) 244 } 245 246 func benchmarkDecodingLayerContainer(b *testing.B, dlc gopacket.DecodingLayerContainer) { 247 decoded := make([]gopacket.LayerType, 0, 20) 248 dlc = dlc.Put(&Ethernet{}) 249 dlc = dlc.Put(&IPv4{}) 250 dlc = dlc.Put(&TCP{}) 251 dlc = dlc.Put(&gopacket.Payload{}) 252 df := gopacket.NewDecodingLayerParser(LayerTypeEthernet) 253 decoder := dlc.LayersDecoder(LayerTypeEthernet, df) 254 for i := 0; i < b.N; i++ { 255 decoder(testSimpleTCPPacket, &decoded) 256 } 257 } 258 259 func BenchmarkDecodingLayerArray(b *testing.B) { 260 benchmarkDecodingLayerContainer(b, gopacket.DecodingLayerArray(nil)) 261 } 262 263 func BenchmarkDecodingLayerMap(b *testing.B) { 264 benchmarkDecodingLayerContainer(b, gopacket.DecodingLayerMap(nil)) 265 } 266 267 func BenchmarkDecodingLayerSparse(b *testing.B) { 268 benchmarkDecodingLayerContainer(b, gopacket.DecodingLayerSparse(nil)) 269 } 270 271 func BenchmarkAlloc(b *testing.B) { 272 for i := 0; i < b.N; i++ { 273 _ = &TCP{} 274 } 275 } 276 277 func BenchmarkFlow(b *testing.B) { 278 p := gopacket.NewPacket(testSimpleTCPPacket, LinkTypeEthernet, gopacket.DecodeOptions{Lazy: true, NoCopy: true}) 279 net := p.NetworkLayer() 280 for i := 0; i < b.N; i++ { 281 net.NetworkFlow() 282 } 283 } 284 285 func BenchmarkEndpoints(b *testing.B) { 286 p := gopacket.NewPacket(testSimpleTCPPacket, LinkTypeEthernet, gopacket.DecodeOptions{Lazy: true, NoCopy: true}) 287 flow := p.NetworkLayer().NetworkFlow() 288 for i := 0; i < b.N; i++ { 289 flow.Endpoints() 290 } 291 } 292 293 func BenchmarkTCPLayerFromDecodedPacket(b *testing.B) { 294 b.StopTimer() 295 p := gopacket.NewPacket(testSimpleTCPPacket, LinkTypeEthernet, testDecodeOptions) 296 b.StartTimer() 297 for i := 0; i < b.N; i++ { 298 _ = p.Layer(LayerTypeTCP) 299 } 300 } 301 302 func BenchmarkTCPLayerClassFromDecodedPacket(b *testing.B) { 303 b.StopTimer() 304 p := gopacket.NewPacket(testSimpleTCPPacket, LinkTypeEthernet, testDecodeOptions) 305 lc := gopacket.NewLayerClass([]gopacket.LayerType{LayerTypeTCP}) 306 b.StartTimer() 307 for i := 0; i < b.N; i++ { 308 _ = p.LayerClass(lc) 309 } 310 } 311 312 func BenchmarkTCPTransportLayerFromDecodedPacket(b *testing.B) { 313 b.StopTimer() 314 p := gopacket.NewPacket(testSimpleTCPPacket, LinkTypeEthernet, testDecodeOptions) 315 b.StartTimer() 316 for i := 0; i < b.N; i++ { 317 _ = p.TransportLayer() 318 } 319 } 320 321 func testDecoder([]byte, gopacket.PacketBuilder) error { 322 return nil 323 } 324 325 func BenchmarkDecodeFuncCallOverheadDirectCall(b *testing.B) { 326 var data []byte 327 var pb gopacket.PacketBuilder 328 for i := 0; i < b.N; i++ { 329 _ = testDecoder(data, pb) 330 } 331 } 332 333 func BenchmarkDecodeFuncCallOverheadDecoderCall(b *testing.B) { 334 d := gopacket.DecodeFunc(testDecoder) 335 var data []byte 336 var pb gopacket.PacketBuilder 337 for i := 0; i < b.N; i++ { 338 _ = d.Decode(data, pb) 339 } 340 } 341 342 func BenchmarkDecodeFuncCallOverheadArrayCall(b *testing.B) { 343 EthernetTypeMetadata[1] = EnumMetadata{DecodeWith: gopacket.DecodeFunc(testDecoder)} 344 d := EthernetType(1) 345 var data []byte 346 var pb gopacket.PacketBuilder 347 for i := 0; i < b.N; i++ { 348 _ = d.Decode(data, pb) 349 } 350 } 351 352 func BenchmarkFmtVerboseString(b *testing.B) { 353 b.StopTimer() 354 p := gopacket.NewPacket(testSimpleTCPPacket, LinkTypeEthernet, testDecodeOptions) 355 b.StartTimer() 356 for i := 0; i < b.N; i++ { 357 _ = fmt.Sprintf("%#v", p) 358 } 359 } 360 361 func BenchmarkPacketString(b *testing.B) { 362 b.StopTimer() 363 p := gopacket.NewPacket(testSimpleTCPPacket, LinkTypeEthernet, testDecodeOptions) 364 b.StartTimer() 365 for i := 0; i < b.N; i++ { 366 _ = p.String() 367 } 368 } 369 370 func BenchmarkPacketDumpString(b *testing.B) { 371 b.StopTimer() 372 p := gopacket.NewPacket(testSimpleTCPPacket, LinkTypeEthernet, testDecodeOptions) 373 b.StartTimer() 374 for i := 0; i < b.N; i++ { 375 _ = p.String() 376 } 377 } 378 379 // TestFlowMapKey makes sure a flow and an endpoint can be used as map keys. 380 func TestFlowMapKey(t *testing.T) { 381 _ = map[gopacket.Flow]bool{} 382 _ = map[gopacket.Endpoint]bool{} 383 _ = map[[2]gopacket.Flow]bool{} 384 } 385 386 func TestDecodeSimpleTCPPacket(t *testing.T) { 387 equal := func(desc, want string, got fmt.Stringer) { 388 if want != got.String() { 389 t.Errorf("%s: got %q want %q", desc, got.String(), want) 390 } 391 } 392 p := gopacket.NewPacket(testSimpleTCPPacket, LinkTypeEthernet, gopacket.DecodeOptions{Lazy: true, NoCopy: true}) 393 if eth := p.LinkLayer(); eth == nil { 394 t.Error("No ethernet layer found") 395 } else { 396 equal("Eth Src", "bc:30:5b:e8:d3:49", eth.LinkFlow().Src()) 397 equal("Eth Dst", "00:00:0c:9f:f0:20", eth.LinkFlow().Dst()) 398 } 399 if net := p.NetworkLayer(); net == nil { 400 t.Error("No net layer found") 401 } else if ip, ok := net.(*IPv4); !ok { 402 t.Error("Net layer is not IP layer") 403 } else { 404 equal("IP Src", "172.17.81.73", net.NetworkFlow().Src()) 405 equal("IP Dst", "173.222.254.225", net.NetworkFlow().Dst()) 406 want := &IPv4{ 407 BaseLayer: BaseLayer{testSimpleTCPPacket[14:34], testSimpleTCPPacket[34:]}, 408 Version: 4, 409 IHL: 5, 410 TOS: 0, 411 Length: 420, 412 Id: 14815, 413 Flags: 0x02, 414 FragOffset: 0, 415 TTL: 64, 416 Protocol: 6, 417 Checksum: 0x555A, 418 SrcIP: []byte{172, 17, 81, 73}, 419 DstIP: []byte{173, 222, 254, 225}, 420 } 421 if !reflect.DeepEqual(ip, want) { 422 t.Errorf("IP layer mismatch, \ngot %#v\nwant %#v\n", ip, want) 423 } 424 } 425 if trans := p.TransportLayer(); trans == nil { 426 t.Error("No transport layer found") 427 } else if tcp, ok := trans.(*TCP); !ok { 428 t.Error("Transport layer is not TCP layer") 429 } else { 430 equal("TCP Src", "50679", trans.TransportFlow().Src()) 431 equal("TCP Dst", "80", trans.TransportFlow().Dst()) 432 want := &TCP{ 433 BaseLayer: BaseLayer{testSimpleTCPPacket[34:66], testSimpleTCPPacket[66:]}, 434 SrcPort: 50679, 435 DstPort: 80, 436 Seq: 0xc57e0e48, 437 Ack: 0x49074232, 438 DataOffset: 8, 439 ACK: true, 440 PSH: true, 441 Window: 0x73, 442 Checksum: 0x9a8f, 443 Urgent: 0, 444 sPort: []byte{0xc5, 0xf7}, 445 dPort: []byte{0x0, 0x50}, 446 Options: []TCPOption{ 447 TCPOption{ 448 OptionType: 0x1, 449 OptionLength: 0x1, 450 }, 451 TCPOption{ 452 OptionType: 0x1, 453 OptionLength: 0x1, 454 }, 455 TCPOption{ 456 OptionType: 0x8, 457 OptionLength: 0xa, 458 OptionData: []byte{0x3, 0x77, 0x37, 0x9c, 0x42, 0x77, 0x5e, 0x3a}, 459 }, 460 }, 461 opts: [4]TCPOption{ 462 TCPOption{ 463 OptionType: 0x1, 464 OptionLength: 0x1, 465 }, 466 TCPOption{ 467 OptionType: 0x1, 468 OptionLength: 0x1, 469 }, 470 TCPOption{ 471 OptionType: 0x8, 472 OptionLength: 0xa, 473 OptionData: []byte{0x3, 0x77, 0x37, 0x9c, 0x42, 0x77, 0x5e, 0x3a}, 474 }, 475 TCPOption{}, 476 }, 477 } 478 if !reflect.DeepEqual(tcp, want) { 479 t.Errorf("TCP layer mismatch\ngot %#v\nwant %#v", tcp, want) 480 } 481 } 482 if payload, ok := p.Layer(gopacket.LayerTypePayload).(*gopacket.Payload); payload == nil || !ok { 483 t.Error("No payload layer found") 484 } else { 485 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" { 486 t.Error("--- Payload STRING ---\n", string(payload.Payload()), "\n--- Payload BYTES ---\n", payload.Payload()) 487 } 488 } 489 490 // Test re-serialization. 491 testSerialization(t, p, testSimpleTCPPacket) 492 } 493 494 type canSetNetLayer interface { 495 SetNetworkLayerForChecksum(gopacket.NetworkLayer) error 496 } 497 498 func testSerialization(t *testing.T, p gopacket.Packet, data []byte) { 499 for _, opts := range []gopacket.SerializeOptions{ 500 gopacket.SerializeOptions{}, 501 gopacket.SerializeOptions{FixLengths: true}, 502 gopacket.SerializeOptions{ComputeChecksums: true}, 503 gopacket.SerializeOptions{FixLengths: true, ComputeChecksums: true}, 504 } { 505 testSerializationWithOpts(t, p, data, opts) 506 } 507 } 508 509 func testSerializationWithOpts(t *testing.T, p gopacket.Packet, data []byte, opts gopacket.SerializeOptions) { 510 // Test re-serialization. 511 slayers := []gopacket.SerializableLayer{} 512 for _, l := range p.Layers() { 513 slayers = append(slayers, l.(gopacket.SerializableLayer)) 514 if h, ok := l.(canSetNetLayer); ok { 515 if err := h.SetNetworkLayerForChecksum(p.NetworkLayer()); err != nil { 516 t.Fatal("can't set network layer:", err) 517 } 518 } 519 } 520 buf := gopacket.NewSerializeBuffer() 521 err := gopacket.SerializeLayers(buf, opts, slayers...) 522 if err != nil { 523 t.Errorf("unable to reserialize layers with opts %#v: %v", opts, err) 524 } else if !bytes.Equal(buf.Bytes(), data) { 525 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", 526 opts, hex.Dump(data), hex.Dump(buf.Bytes()), bytediff.BashOutput.String(bytediff.Diff(data, buf.Bytes())), p) 527 } 528 } 529 530 // Makes sure packet payload doesn't display the 6 trailing null of this packet 531 // as part of the payload. They're actually the ethernet trailer. 532 func TestDecodeSmallTCPPacketHasEmptyPayload(t *testing.T) { 533 smallPacket := []byte{ 534 0xbc, 0x30, 0x5b, 0xe8, 0xd3, 0x49, 0xb8, 0xac, 0x6f, 0x92, 0xd5, 0xbf, 535 0x08, 0x00, 0x45, 0x00, 0x00, 0x28, 0x00, 0x00, 0x40, 0x00, 0x40, 0x06, 536 0x3f, 0x9f, 0xac, 0x11, 0x51, 0xc5, 0xac, 0x11, 0x51, 0x49, 0x00, 0x63, 537 0x9a, 0xef, 0x00, 0x00, 0x00, 0x00, 0x2e, 0xc1, 0x27, 0x83, 0x50, 0x14, 538 0x00, 0x00, 0xc3, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 539 } 540 p := gopacket.NewPacket(smallPacket, LinkTypeEthernet, testDecodeOptions) 541 542 if payload := p.Layer(gopacket.LayerTypePayload); payload != nil { 543 t.Error("Payload found for empty TCP packet") 544 } 545 546 testSerialization(t, p, smallPacket) 547 } 548 549 func TestDecodeVLANPacket(t *testing.T) { 550 p := gopacket.NewPacket( 551 []byte{ 552 0x00, 0x10, 0xdb, 0xff, 0x10, 0x00, 0x00, 0x15, 0x2c, 0x9d, 0xcc, 0x00, 553 0x81, 0x00, 0x01, 0xf7, 0x08, 0x00, 0x45, 0x00, 0x00, 0x28, 0x29, 0x8d, 554 0x40, 0x00, 0x7d, 0x06, 0x83, 0xa0, 0xac, 0x1b, 0xca, 0x8e, 0x45, 0x16, 555 0x94, 0xe2, 0xd4, 0x0a, 0x00, 0x50, 0xdf, 0xab, 0x9c, 0xc6, 0xcd, 0x1e, 556 0xe5, 0xd1, 0x50, 0x10, 0x01, 0x00, 0x5a, 0x74, 0x00, 0x00, 0x00, 0x00, 557 0x00, 0x00, 0x00, 0x00, 558 }, LinkTypeEthernet, testDecodeOptions) 559 if err := p.ErrorLayer(); err != nil { 560 t.Error("Error while parsing vlan packet:", err) 561 } 562 if vlan := p.Layer(LayerTypeDot1Q); vlan == nil { 563 t.Error("Didn't detect vlan") 564 } else if _, ok := vlan.(*Dot1Q); !ok { 565 t.Error("LayerTypeDot1Q layer is not a Dot1Q object") 566 } 567 for i, l := range p.Layers() { 568 t.Logf("Layer %d: %#v", i, l) 569 } 570 want := []gopacket.LayerType{LayerTypeEthernet, LayerTypeDot1Q, LayerTypeIPv4, LayerTypeTCP} 571 checkLayers(p, want, t) 572 } 573 574 func TestDecodeSCTPPackets(t *testing.T) { 575 sctpPackets := [][]byte{ 576 []byte{ // INIT 577 0x00, 0x00, 0x0c, 0x9f, 0xf0, 0x1f, 0x24, 0xbe, 0x05, 0x27, 0x0b, 0x17, 0x08, 0x00, 0x45, 0x02, 578 0x00, 0x44, 0x00, 0x00, 0x40, 0x00, 0x40, 0x84, 0xc4, 0x22, 0xac, 0x1d, 0x14, 0x0f, 0xac, 0x19, 579 0x09, 0xcc, 0x27, 0x0f, 0x22, 0xb8, 0x00, 0x00, 0x00, 0x00, 0x19, 0x6b, 0x0b, 0x40, 0x01, 0x00, 580 0x00, 0x24, 0xb6, 0x96, 0xb0, 0x9e, 0x00, 0x01, 0xc0, 0x00, 0x00, 0x0a, 0xff, 0xff, 0xdb, 0x85, 581 0x60, 0x23, 0x00, 0x0c, 0x00, 0x06, 0x00, 0x05, 0x00, 0x00, 0x80, 0x00, 0x00, 0x04, 0xc0, 0x00, 582 0x00, 0x04, 583 }, []byte{ // INIT ACK 584 0x24, 0xbe, 0x05, 0x27, 0x0b, 0x17, 0x00, 0x1f, 0xca, 0xb3, 0x76, 0x40, 0x08, 0x00, 0x45, 0x20, 585 0x01, 0x24, 0x00, 0x00, 0x40, 0x00, 0x36, 0x84, 0xcd, 0x24, 0xac, 0x19, 0x09, 0xcc, 0xac, 0x1d, 586 0x14, 0x0f, 0x22, 0xb8, 0x27, 0x0f, 0xb6, 0x96, 0xb0, 0x9e, 0x4b, 0xab, 0x40, 0x9a, 0x02, 0x00, 587 0x01, 0x04, 0x32, 0x80, 0xfb, 0x42, 0x00, 0x00, 0xf4, 0x00, 0x00, 0x0a, 0x00, 0x0a, 0x85, 0x98, 588 0xb1, 0x26, 0x00, 0x07, 0x00, 0xe8, 0xd3, 0x08, 0xce, 0xe2, 0x52, 0x95, 0xcc, 0x09, 0xa1, 0x4c, 589 0x6f, 0xa7, 0x9e, 0xba, 0x03, 0xa1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 590 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x42, 0xfb, 0x80, 0x32, 0x9e, 0xb0, 591 0x96, 0xb6, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x46, 0xc2, 0x50, 0x00, 0x00, 592 0x00, 0x00, 0x5e, 0x25, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x0a, 0x00, 0x26, 0xb1, 593 0x98, 0x85, 0x02, 0x00, 0x27, 0x0f, 0xac, 0x1d, 0x14, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 594 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xb8, 0x22, 595 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x24, 0x6a, 0x72, 0x5c, 0x1c, 0x3c, 0xaa, 596 0x7a, 0xcd, 0xd3, 0x8f, 0x52, 0x78, 0x7c, 0x77, 0xfd, 0x46, 0xbd, 0x72, 0x82, 0xc1, 0x1f, 0x70, 597 0x44, 0xcc, 0xc7, 0x9b, 0x9b, 0x7b, 0x13, 0x54, 0x3f, 0x89, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 598 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 599 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x24, 0xb6, 0x96, 600 0xb0, 0x9e, 0x00, 0x01, 0xc0, 0x00, 0x00, 0x0a, 0xff, 0xff, 0xdb, 0x85, 0x60, 0x23, 0x00, 0x0c, 601 0x00, 0x06, 0x00, 0x05, 0x00, 0x00, 0x80, 0x00, 0x00, 0x04, 0xc0, 0x00, 0x00, 0x04, 0x00, 0x00, 602 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x04, 0xc0, 0x00, 603 0x00, 0x04, 604 }, []byte{ // COOKIE ECHO, DATA 605 0x00, 0x00, 0x0c, 0x9f, 0xf0, 0x1f, 0x24, 0xbe, 0x05, 0x27, 0x0b, 0x17, 0x08, 0x00, 0x45, 0x02, 606 0x01, 0x20, 0x00, 0x00, 0x40, 0x00, 0x40, 0x84, 0xc3, 0x46, 0xac, 0x1d, 0x14, 0x0f, 0xac, 0x19, 607 0x09, 0xcc, 0x27, 0x0f, 0x22, 0xb8, 0x32, 0x80, 0xfb, 0x42, 0x01, 0xf9, 0xf3, 0xa9, 0x0a, 0x00, 608 0x00, 0xe8, 0xd3, 0x08, 0xce, 0xe2, 0x52, 0x95, 0xcc, 0x09, 0xa1, 0x4c, 0x6f, 0xa7, 0x9e, 0xba, 609 0x03, 0xa1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 610 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x42, 0xfb, 0x80, 0x32, 0x9e, 0xb0, 0x96, 0xb6, 0x00, 0x00, 611 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x46, 0xc2, 0x50, 0x00, 0x00, 0x00, 0x00, 0x5e, 0x25, 612 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x0a, 0x00, 0x26, 0xb1, 0x98, 0x85, 0x02, 0x00, 613 0x27, 0x0f, 0xac, 0x1d, 0x14, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 614 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xb8, 0x22, 0x01, 0x00, 0x00, 0x00, 615 0x00, 0x00, 0x80, 0x02, 0x00, 0x24, 0x6a, 0x72, 0x5c, 0x1c, 0x3c, 0xaa, 0x7a, 0xcd, 0xd3, 0x8f, 616 0x52, 0x78, 0x7c, 0x77, 0xfd, 0x46, 0xbd, 0x72, 0x82, 0xc1, 0x1f, 0x70, 0x44, 0xcc, 0xc7, 0x9b, 617 0x9b, 0x7b, 0x13, 0x54, 0x3f, 0x89, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 618 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 619 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x24, 0xb6, 0x96, 0xb0, 0x9e, 0x00, 0x01, 620 0xc0, 0x00, 0x00, 0x0a, 0xff, 0xff, 0xdb, 0x85, 0x60, 0x23, 0x00, 0x0c, 0x00, 0x06, 0x00, 0x05, 621 0x00, 0x00, 0x80, 0x00, 0x00, 0x04, 0xc0, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 622 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x16, 0xdb, 0x85, 0x60, 0x23, 0x00, 0x00, 623 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x6f, 0x6f, 0x21, 0x0a, 0x00, 0x00, 0x00, 624 }, []byte{ // COOKIE ACK, SACK 625 0x24, 0xbe, 0x05, 0x27, 0x0b, 0x17, 0x00, 0x1f, 0xca, 0xb3, 0x76, 0x40, 0x08, 0x00, 0x45, 0x20, 626 0x00, 0x34, 0x00, 0x00, 0x40, 0x00, 0x36, 0x84, 0xce, 0x14, 0xac, 0x19, 0x09, 0xcc, 0xac, 0x1d, 627 0x14, 0x0f, 0x22, 0xb8, 0x27, 0x0f, 0xb6, 0x96, 0xb0, 0x9e, 0xed, 0x64, 0x30, 0x98, 0x0b, 0x00, 628 0x00, 0x04, 0x03, 0x00, 0x00, 0x10, 0xdb, 0x85, 0x60, 0x23, 0x00, 0x00, 0xf3, 0xfa, 0x00, 0x00, 629 0x00, 0x00, 630 }, []byte{ // DATA 631 0x00, 0x00, 0x0c, 0x9f, 0xf0, 0x1f, 0x24, 0xbe, 0x05, 0x27, 0x0b, 0x17, 0x08, 0x00, 0x45, 0x02, 632 0x00, 0x3c, 0x00, 0x00, 0x40, 0x00, 0x40, 0x84, 0xc4, 0x2a, 0xac, 0x1d, 0x14, 0x0f, 0xac, 0x19, 633 0x09, 0xcc, 0x27, 0x0f, 0x22, 0xb8, 0x32, 0x80, 0xfb, 0x42, 0xa1, 0xe3, 0xb2, 0x31, 0x00, 0x03, 634 0x00, 0x19, 0xdb, 0x85, 0x60, 0x24, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x62, 0x69, 635 0x7a, 0x7a, 0x6c, 0x65, 0x21, 0x0a, 0x00, 0x00, 0x00, 0x00, 636 }, []byte{ // SACK 637 0x24, 0xbe, 0x05, 0x27, 0x0b, 0x17, 0x00, 0x1f, 0xca, 0xb3, 0x76, 0x40, 0x08, 0x00, 0x45, 0x20, 638 0x00, 0x30, 0x00, 0x00, 0x40, 0x00, 0x36, 0x84, 0xce, 0x18, 0xac, 0x19, 0x09, 0xcc, 0xac, 0x1d, 639 0x14, 0x0f, 0x22, 0xb8, 0x27, 0x0f, 0xb6, 0x96, 0xb0, 0x9e, 0xfa, 0x49, 0x94, 0x3a, 0x03, 0x00, 640 0x00, 0x10, 0xdb, 0x85, 0x60, 0x24, 0x00, 0x00, 0xf4, 0x00, 0x00, 0x00, 0x00, 0x00, 641 }, []byte{ // SHUTDOWN 642 0x00, 0x00, 0x0c, 0x9f, 0xf0, 0x1f, 0x24, 0xbe, 0x05, 0x27, 0x0b, 0x17, 0x08, 0x00, 0x45, 0x02, 643 0x00, 0x28, 0x00, 0x00, 0x40, 0x00, 0x40, 0x84, 0xc4, 0x3e, 0xac, 0x1d, 0x14, 0x0f, 0xac, 0x19, 644 0x09, 0xcc, 0x27, 0x0f, 0x22, 0xb8, 0x32, 0x80, 0xfb, 0x42, 0x3f, 0x29, 0x59, 0x23, 0x07, 0x00, 645 0x00, 0x08, 0x85, 0x98, 0xb1, 0x25, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 646 }, []byte{ // SHUTDOWN ACK 647 0x24, 0xbe, 0x05, 0x27, 0x0b, 0x17, 0x00, 0x1f, 0xca, 0xb3, 0x76, 0x40, 0x08, 0x00, 0x45, 0x20, 648 0x00, 0x24, 0x00, 0x00, 0x40, 0x00, 0x36, 0x84, 0xce, 0x24, 0xac, 0x19, 0x09, 0xcc, 0xac, 0x1d, 649 0x14, 0x0f, 0x22, 0xb8, 0x27, 0x0f, 0xb6, 0x96, 0xb0, 0x9e, 0xb2, 0xc8, 0x99, 0x24, 0x08, 0x00, 650 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 651 }, []byte{ // SHUTDOWN COMPLETE 652 0x00, 0x00, 0x0c, 0x9f, 0xf0, 0x1f, 0x24, 0xbe, 0x05, 0x27, 0x0b, 0x17, 0x08, 0x00, 0x45, 0x02, 653 0x00, 0x24, 0x00, 0x00, 0x40, 0x00, 0x40, 0x84, 0xc4, 0x42, 0xac, 0x1d, 0x14, 0x0f, 0xac, 0x19, 654 0x09, 0xcc, 0x27, 0x0f, 0x22, 0xb8, 0x32, 0x80, 0xfb, 0x42, 0xa8, 0xd1, 0x86, 0x85, 0x0e, 0x00, 655 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 656 }} 657 wantLayers := [][]gopacket.LayerType{ 658 []gopacket.LayerType{LayerTypeSCTPInit}, 659 []gopacket.LayerType{LayerTypeSCTPInitAck}, 660 []gopacket.LayerType{LayerTypeSCTPCookieEcho, LayerTypeSCTPData, gopacket.LayerTypePayload}, 661 []gopacket.LayerType{LayerTypeSCTPCookieAck, LayerTypeSCTPSack}, 662 []gopacket.LayerType{LayerTypeSCTPData, gopacket.LayerTypePayload}, 663 []gopacket.LayerType{LayerTypeSCTPSack}, 664 []gopacket.LayerType{LayerTypeSCTPShutdown}, 665 []gopacket.LayerType{LayerTypeSCTPShutdownAck}, 666 []gopacket.LayerType{LayerTypeSCTPShutdownComplete}, 667 } 668 for i, data := range sctpPackets { 669 p := gopacket.NewPacket(data, LinkTypeEthernet, testDecodeOptions) 670 for _, typ := range wantLayers[i] { 671 if p.Layer(typ) == nil { 672 t.Errorf("Packet %d missing layer type %v, got:", i, typ) 673 for _, layer := range p.Layers() { 674 t.Errorf("\t%v", layer.LayerType()) 675 } 676 if p.ErrorLayer() != nil { 677 t.Error("\tPacket layer error:", p.ErrorLayer().Error()) 678 } 679 } 680 } 681 // Test re-serialization. 682 testSerializationWithOpts(t, p, data, gopacket.SerializeOptions{FixLengths: true, ComputeChecksums: true}) 683 } 684 } 685 686 func TestDecodeCiscoDiscovery(t *testing.T) { 687 // http://wiki.wireshark.org/SampleCaptures?action=AttachFile&do=get&target=cdp_v2.pcap 688 data := []byte{ 689 0x01, 0x00, 0x0c, 0xcc, 0xcc, 0xcc, 0x00, 0x0b, 0xbe, 0x18, 0x9a, 0x41, 0x01, 0xc3, 0xaa, 0xaa, 690 0x03, 0x00, 0x00, 0x0c, 0x20, 0x00, 0x02, 0xb4, 0x09, 0xa0, 0x00, 0x01, 0x00, 0x0c, 0x6d, 0x79, 691 0x73, 0x77, 0x69, 0x74, 0x63, 0x68, 0x00, 0x02, 0x00, 0x11, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 692 0xcc, 0x00, 0x04, 0xc0, 0xa8, 0x00, 0xfd, 0x00, 0x03, 0x00, 0x13, 0x46, 0x61, 0x73, 0x74, 0x45, 693 0x74, 0x68, 0x65, 0x72, 0x6e, 0x65, 0x74, 0x30, 0x2f, 0x31, 0x00, 0x04, 0x00, 0x08, 0x00, 0x00, 694 0x00, 0x28, 0x00, 0x05, 0x01, 0x14, 0x43, 0x69, 0x73, 0x63, 0x6f, 0x20, 0x49, 0x6e, 0x74, 0x65, 695 0x72, 0x6e, 0x65, 0x74, 0x77, 0x6f, 0x72, 0x6b, 0x20, 0x4f, 0x70, 0x65, 0x72, 0x61, 0x74, 0x69, 696 0x6e, 0x67, 0x20, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x20, 0x53, 0x6f, 0x66, 0x74, 0x77, 0x61, 697 0x72, 0x65, 0x20, 0x0a, 0x49, 0x4f, 0x53, 0x20, 0x28, 0x74, 0x6d, 0x29, 0x20, 0x43, 0x32, 0x39, 698 0x35, 0x30, 0x20, 0x53, 0x6f, 0x66, 0x74, 0x77, 0x61, 0x72, 0x65, 0x20, 0x28, 0x43, 0x32, 0x39, 699 0x35, 0x30, 0x2d, 0x49, 0x36, 0x4b, 0x32, 0x4c, 0x32, 0x51, 0x34, 0x2d, 0x4d, 0x29, 0x2c, 0x20, 700 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x20, 0x31, 0x32, 0x2e, 0x31, 0x28, 0x32, 0x32, 0x29, 701 0x45, 0x41, 0x31, 0x34, 0x2c, 0x20, 0x52, 0x45, 0x4c, 0x45, 0x41, 0x53, 0x45, 0x20, 0x53, 0x4f, 702 0x46, 0x54, 0x57, 0x41, 0x52, 0x45, 0x20, 0x28, 0x66, 0x63, 0x31, 0x29, 0x0a, 0x54, 0x65, 0x63, 703 0x68, 0x6e, 0x69, 0x63, 0x61, 0x6c, 0x20, 0x53, 0x75, 0x70, 0x70, 0x6f, 0x72, 0x74, 0x3a, 0x20, 704 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f, 0x77, 0x77, 0x77, 0x2e, 0x63, 0x69, 0x73, 0x63, 0x6f, 705 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x74, 0x65, 0x63, 0x68, 0x73, 0x75, 0x70, 0x70, 0x6f, 0x72, 0x74, 706 0x0a, 0x43, 0x6f, 0x70, 0x79, 0x72, 0x69, 0x67, 0x68, 0x74, 0x20, 0x28, 0x63, 0x29, 0x20, 0x31, 707 0x39, 0x38, 0x36, 0x2d, 0x32, 0x30, 0x31, 0x30, 0x20, 0x62, 0x79, 0x20, 0x63, 0x69, 0x73, 0x63, 708 0x6f, 0x20, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x73, 0x2c, 0x20, 0x49, 0x6e, 0x63, 0x2e, 0x0a, 709 0x43, 0x6f, 0x6d, 0x70, 0x69, 0x6c, 0x65, 0x64, 0x20, 0x54, 0x75, 0x65, 0x20, 0x32, 0x36, 0x2d, 710 0x4f, 0x63, 0x74, 0x2d, 0x31, 0x30, 0x20, 0x31, 0x30, 0x3a, 0x33, 0x35, 0x20, 0x62, 0x79, 0x20, 711 0x6e, 0x62, 0x75, 0x72, 0x72, 0x61, 0x00, 0x06, 0x00, 0x15, 0x63, 0x69, 0x73, 0x63, 0x6f, 0x20, 712 0x57, 0x53, 0x2d, 0x43, 0x32, 0x39, 0x35, 0x30, 0x2d, 0x31, 0x32, 0x00, 0x08, 0x00, 0x24, 0x00, 713 0x00, 0x0c, 0x01, 0x12, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x01, 0x02, 0x20, 0xff, 714 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0b, 0xbe, 0x18, 0x9a, 0x40, 0xff, 0x00, 0x00, 0x00, 715 0x09, 0x00, 0x0c, 0x4d, 0x59, 0x44, 0x4f, 0x4d, 0x41, 0x49, 0x4e, 0x00, 0x0a, 0x00, 0x06, 0x00, 716 0x01, 0x00, 0x0b, 0x00, 0x05, 0x01, 0x00, 0x12, 0x00, 0x05, 0x00, 0x00, 0x13, 0x00, 0x05, 0x00, 717 0x00, 0x16, 0x00, 0x11, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0xcc, 0x00, 0x04, 0xc0, 0xa8, 0x00, 718 0xfd, 719 } 720 p := gopacket.NewPacket(data, LinkTypeEthernet, testDecodeOptions) 721 wantLayers := []gopacket.LayerType{LayerTypeEthernet, LayerTypeLLC, LayerTypeSNAP, LayerTypeCiscoDiscovery, LayerTypeCiscoDiscoveryInfo} 722 checkLayers(p, wantLayers, t) 723 724 want := &CiscoDiscoveryInfo{ 725 CDPHello: CDPHello{ 726 OUI: []byte{0, 0, 12}, 727 ProtocolID: 274, 728 ClusterMaster: []byte{0, 0, 0, 0}, 729 Unknown1: []byte{255, 255, 255, 255}, 730 Version: 1, 731 SubVersion: 2, 732 Status: 32, 733 Unknown2: 255, 734 ClusterCommander: net.HardwareAddr{0, 0, 0, 0, 0, 0}, 735 SwitchMAC: net.HardwareAddr{0, 0x0b, 0xbe, 0x18, 0x9a, 0x40}, 736 Unknown3: 255, 737 ManagementVLAN: 0, 738 }, 739 DeviceID: "myswitch", 740 Addresses: []net.IP{net.IPv4(192, 168, 0, 253)}, 741 PortID: "FastEthernet0/1", 742 Capabilities: CDPCapabilities{false, false, false, true, false, true, false, false, false}, 743 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", 744 Platform: "cisco WS-C2950-12", 745 VTPDomain: "MYDOMAIN", 746 NativeVLAN: 1, 747 FullDuplex: true, 748 MgmtAddresses: []net.IP{net.IPv4(192, 168, 0, 253)}, 749 BaseLayer: BaseLayer{Contents: data[26:]}, 750 } 751 cdpL := p.Layer(LayerTypeCiscoDiscoveryInfo) 752 info, _ := cdpL.(*CiscoDiscoveryInfo) 753 if !reflect.DeepEqual(info, want) { 754 t.Errorf("Values mismatch, \ngot %#v\nwant %#v\n", info, want) 755 } 756 } 757 758 func TestDecodeLinkLayerDiscovery(t *testing.T) { 759 // http://wiki.wireshark.org/SampleCaptures?action=AttachFile&do=get&target=lldp.detailed.pcap 760 data := []byte{ 761 0x01, 0x80, 0xc2, 0x00, 0x00, 0x0e, 0x00, 0x01, 0x30, 0xf9, 0xad, 0xa0, 762 0x88, 0xcc, 0x02, 0x07, 0x04, 0x00, 0x01, 0x30, 0xf9, 0xad, 0xa0, 0x04, 763 0x04, 0x05, 0x31, 0x2f, 0x31, 0x06, 0x02, 0x00, 0x78, 0x08, 0x17, 0x53, 764 0x75, 0x6d, 0x6d, 0x69, 0x74, 0x33, 0x30, 0x30, 0x2d, 0x34, 0x38, 0x2d, 765 0x50, 0x6f, 0x72, 0x74, 0x20, 0x31, 0x30, 0x30, 0x31, 0x00, 0x0a, 0x0d, 766 0x53, 0x75, 0x6d, 0x6d, 0x69, 0x74, 0x33, 0x30, 0x30, 0x2d, 0x34, 0x38, 767 0x00, 0x0c, 0x4c, 0x53, 0x75, 0x6d, 0x6d, 0x69, 0x74, 0x33, 0x30, 0x30, 768 0x2d, 0x34, 0x38, 0x20, 0x2d, 0x20, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f, 769 0x6e, 0x20, 0x37, 0x2e, 0x34, 0x65, 0x2e, 0x31, 0x20, 0x28, 0x42, 0x75, 770 0x69, 0x6c, 0x64, 0x20, 0x35, 0x29, 0x20, 0x62, 0x79, 0x20, 0x52, 0x65, 771 0x6c, 0x65, 0x61, 0x73, 0x65, 0x5f, 0x4d, 0x61, 0x73, 0x74, 0x65, 0x72, 772 0x20, 0x30, 0x35, 0x2f, 0x32, 0x37, 0x2f, 0x30, 0x35, 0x20, 0x30, 0x34, 773 0x3a, 0x35, 0x33, 0x3a, 0x31, 0x31, 0x00, 0x0e, 0x04, 0x00, 0x14, 0x00, 774 0x14, 0x10, 0x0e, 0x07, 0x06, 0x00, 0x01, 0x30, 0xf9, 0xad, 0xa0, 0x02, 775 0x00, 0x00, 0x03, 0xe9, 0x00, 0xfe, 0x07, 0x00, 0x12, 0x0f, 0x02, 0x07, 776 0x01, 0x00, 0xfe, 0x09, 0x00, 0x12, 0x0f, 0x01, 0x03, 0x6c, 0x00, 0x00, 777 0x10, 0xfe, 0x09, 0x00, 0x12, 0x0f, 0x03, 0x01, 0x00, 0x00, 0x00, 0x00, 778 0xfe, 0x06, 0x00, 0x12, 0x0f, 0x04, 0x05, 0xf2, 0xfe, 0x06, 0x00, 0x80, 779 0xc2, 0x01, 0x01, 0xe8, 0xfe, 0x07, 0x00, 0x80, 0xc2, 0x02, 0x01, 0x00, 780 0x00, 0xfe, 0x17, 0x00, 0x80, 0xc2, 0x03, 0x01, 0xe8, 0x10, 0x76, 0x32, 781 0x2d, 0x30, 0x34, 0x38, 0x38, 0x2d, 0x30, 0x33, 0x2d, 0x30, 0x35, 0x30, 782 0x35, 0x00, 0xfe, 0x05, 0x00, 0x80, 0xc2, 0x04, 0x00, 0x00, 0x00, 783 } 784 785 p := gopacket.NewPacket(data, LinkTypeEthernet, testDecodeOptions) 786 wantLayers := []gopacket.LayerType{LayerTypeEthernet, LayerTypeLinkLayerDiscovery, LayerTypeLinkLayerDiscoveryInfo} 787 checkLayers(p, wantLayers, t) 788 lldpL := p.Layer(LayerTypeLinkLayerDiscovery) 789 lldp := lldpL.(*LinkLayerDiscovery) 790 want := &LinkLayerDiscovery{ 791 ChassisID: LLDPChassisID{LLDPChassisIDSubTypeMACAddr, []byte{0x00, 0x01, 0x30, 0xf9, 0xad, 0xa0}}, 792 PortID: LLDPPortID{LLDPPortIDSubtypeIfaceName, []byte("1/1")}, 793 TTL: 120, 794 BaseLayer: BaseLayer{Contents: data[14:]}, 795 } 796 lldp.Values = nil // test these in next stage 797 if !reflect.DeepEqual(lldp, want) { 798 t.Errorf("Values mismatch, \ngot %#v\nwant %#v\n", lldp, want) 799 } 800 801 infoL := p.Layer(LayerTypeLinkLayerDiscoveryInfo) 802 info := infoL.(*LinkLayerDiscoveryInfo) 803 wantinfo := &LinkLayerDiscoveryInfo{ 804 PortDescription: "Summit300-48-Port 1001\x00", 805 SysName: "Summit300-48\x00", 806 SysDescription: "Summit300-48 - Version 7.4e.1 (Build 5) by Release_Master 05/27/05 04:53:11\x00", 807 SysCapabilities: LLDPSysCapabilities{ 808 SystemCap: LLDPCapabilities{Bridge: true, Router: true}, 809 EnabledCap: LLDPCapabilities{Bridge: true, Router: true}, 810 }, 811 MgmtAddress: LLDPMgmtAddress{IANAAddressFamily802, []byte{0x00, 0x01, 0x30, 0xf9, 0xad, 0xa0}, LLDPInterfaceSubtypeifIndex, 1001, ""}, 812 OrgTLVs: []LLDPOrgSpecificTLV{ 813 LLDPOrgSpecificTLV{OUI: 0x120f, SubType: 0x2, Info: []uint8{0x7, 0x1, 0x0}}, 814 LLDPOrgSpecificTLV{OUI: 0x120f, SubType: 0x1, Info: []uint8{0x3, 0x6c, 0x0, 0x0, 0x10}}, 815 LLDPOrgSpecificTLV{OUI: 0x120f, SubType: 0x3, Info: []uint8{0x1, 0x0, 0x0, 0x0, 0x0}}, 816 LLDPOrgSpecificTLV{OUI: 0x120f, SubType: 0x4, Info: []uint8{0x5, 0xf2}}, 817 LLDPOrgSpecificTLV{OUI: 0x80c2, SubType: 0x1, Info: []uint8{0x1, 0xe8}}, 818 LLDPOrgSpecificTLV{OUI: 0x80c2, SubType: 0x2, Info: []uint8{0x1, 0x0, 0x0}}, 819 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}}, 820 LLDPOrgSpecificTLV{OUI: 0x80c2, SubType: 0x4, Info: []uint8{0x0}}, 821 }, 822 Unknown: nil, 823 } 824 if !reflect.DeepEqual(info, wantinfo) { 825 t.Errorf("Values mismatch, \ngot %#v\nwant %#v\n", info, wantinfo) 826 } 827 info8021, err := info.Decode8021() 828 if err != nil { 829 t.Errorf("8021 Values decode error: %v", err) 830 } 831 want8021 := LLDPInfo8021{ 832 PVID: 488, 833 PPVIDs: []PortProtocolVLANID{PortProtocolVLANID{false, false, 0}}, 834 VLANNames: []VLANName{VLANName{488, "v2-0488-03-0505\x00"}}, 835 ProtocolIdentities: nil, 836 VIDUsageDigest: 0, 837 ManagementVID: 0, 838 LinkAggregation: LLDPLinkAggregation{false, false, 0}, 839 } 840 if !reflect.DeepEqual(info8021, want8021) { 841 t.Errorf("Values mismatch, \ngot %#v\nwant %#v\n", info8021, want8021) 842 } 843 info8023, err := info.Decode8023() 844 if err != nil { 845 t.Errorf("8023 Values decode error: %v", err) 846 } 847 want8023 := LLDPInfo8023{ 848 LinkAggregation: LLDPLinkAggregation{true, false, 0}, 849 MACPHYConfigStatus: LLDPMACPHYConfigStatus{true, true, 0x6c00, 0x0010}, 850 PowerViaMDI: LLDPPowerViaMDI8023{true, true, true, false, 1, 0, 0, 0, 0, 0, 0}, 851 MTU: 1522, 852 } 853 854 if !reflect.DeepEqual(info8023, want8023) { 855 t.Errorf("Values mismatch, \ngot %#v\nwant %#v\n", info8023, want8023) 856 } 857 858 // http://wiki.wireshark.org/SampleCaptures?action=AttachFile&do=get&target=lldpmed_civicloc.pcap 859 data = []byte{ 860 0x01, 0x80, 0xc2, 0x00, 0x00, 0x0e, 0x00, 0x13, 0x21, 0x57, 0xca, 0x7f, 861 0x88, 0xcc, 0x02, 0x07, 0x04, 0x00, 0x13, 0x21, 0x57, 0xca, 0x40, 0x04, 862 0x02, 0x07, 0x31, 0x06, 0x02, 0x00, 0x78, 0x08, 0x01, 0x31, 0x0a, 0x1a, 863 0x50, 0x72, 0x6f, 0x43, 0x75, 0x72, 0x76, 0x65, 0x20, 0x53, 0x77, 0x69, 864 0x74, 0x63, 0x68, 0x20, 0x32, 0x36, 0x30, 0x30, 0x2d, 0x38, 0x2d, 0x50, 865 0x57, 0x52, 0x0c, 0x5f, 0x50, 0x72, 0x6f, 0x43, 0x75, 0x72, 0x76, 0x65, 866 0x20, 0x4a, 0x38, 0x37, 0x36, 0x32, 0x41, 0x20, 0x53, 0x77, 0x69, 0x74, 867 0x63, 0x68, 0x20, 0x32, 0x36, 0x30, 0x30, 0x2d, 0x38, 0x2d, 0x50, 0x57, 868 0x52, 0x2c, 0x20, 0x72, 0x65, 0x76, 0x69, 0x73, 0x69, 0x6f, 0x6e, 0x20, 869 0x48, 0x2e, 0x30, 0x38, 0x2e, 0x38, 0x39, 0x2c, 0x20, 0x52, 0x4f, 0x4d, 870 0x20, 0x48, 0x2e, 0x30, 0x38, 0x2e, 0x35, 0x58, 0x20, 0x28, 0x2f, 0x73, 871 0x77, 0x2f, 0x63, 0x6f, 0x64, 0x65, 0x2f, 0x62, 0x75, 0x69, 0x6c, 0x64, 872 0x2f, 0x66, 0x69, 0x73, 0x68, 0x28, 0x74, 0x73, 0x5f, 0x30, 0x38, 0x5f, 873 0x35, 0x29, 0x29, 0x0e, 0x04, 0x00, 0x14, 0x00, 0x04, 0x10, 0x0c, 0x05, 874 0x01, 0x0f, 0xff, 0x7a, 0x94, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfe, 875 0x09, 0x00, 0x12, 0x0f, 0x01, 0x03, 0x6c, 0x00, 0x00, 0x10, 0xfe, 0x07, 876 0x00, 0x12, 0xbb, 0x01, 0x00, 0x0f, 0x04, 0xfe, 0x08, 0x00, 0x12, 0xbb, 877 0x02, 0x01, 0x40, 0x65, 0xae, 0xfe, 0x2e, 0x00, 0x12, 0xbb, 0x03, 0x02, 878 0x28, 0x02, 0x55, 0x53, 0x01, 0x02, 0x43, 0x41, 0x03, 0x09, 0x52, 0x6f, 879 0x73, 0x65, 0x76, 0x69, 0x6c, 0x6c, 0x65, 0x06, 0x09, 0x46, 0x6f, 0x6f, 880 0x74, 0x68, 0x69, 0x6c, 0x6c, 0x73, 0x13, 0x04, 0x38, 0x30, 0x30, 0x30, 881 0x1a, 0x03, 0x52, 0x33, 0x4c, 0xfe, 0x07, 0x00, 0x12, 0xbb, 0x04, 0x03, 882 0x00, 0x41, 0x00, 0x00, 883 } 884 885 p = gopacket.NewPacket(data, LinkTypeEthernet, testDecodeOptions) 886 wantLayers = []gopacket.LayerType{LayerTypeEthernet, LayerTypeLinkLayerDiscovery, LayerTypeLinkLayerDiscoveryInfo} 887 checkLayers(p, wantLayers, t) 888 lldpL = p.Layer(LayerTypeLinkLayerDiscovery) 889 lldp = lldpL.(*LinkLayerDiscovery) 890 want = &LinkLayerDiscovery{ 891 ChassisID: LLDPChassisID{LLDPChassisIDSubTypeMACAddr, []byte{0x00, 0x13, 0x21, 0x57, 0xca, 0x40}}, 892 PortID: LLDPPortID{LLDPPortIDSubtypeLocal, []byte("1")}, 893 TTL: 120, 894 BaseLayer: BaseLayer{Contents: data[14:]}, 895 } 896 lldp.Values = nil // test these in next stage 897 if !reflect.DeepEqual(lldp, want) { 898 t.Errorf("Values mismatch, \ngot %#v\nwant %#v\n", lldp, want) 899 } 900 901 infoL = p.Layer(LayerTypeLinkLayerDiscoveryInfo) 902 info = infoL.(*LinkLayerDiscoveryInfo) 903 wantinfo = &LinkLayerDiscoveryInfo{ 904 PortDescription: "1", 905 SysName: "ProCurve Switch 2600-8-PWR", 906 SysDescription: "ProCurve J8762A Switch 2600-8-PWR, revision H.08.89, ROM H.08.5X (/sw/code/build/fish(ts_08_5))", 907 SysCapabilities: LLDPSysCapabilities{ 908 SystemCap: LLDPCapabilities{Bridge: true, Router: true}, 909 EnabledCap: LLDPCapabilities{Bridge: true}, 910 }, 911 MgmtAddress: LLDPMgmtAddress{IANAAddressFamilyIPV4, []byte{0x0f, 0xff, 0x7a, 0x94}, LLDPInterfaceSubtypeifIndex, 0, ""}, 912 OrgTLVs: []LLDPOrgSpecificTLV{ 913 LLDPOrgSpecificTLV{OUI: 0x120f, SubType: 0x1, Info: []uint8{0x3, 0x6c, 0x0, 0x0, 0x10}}, 914 LLDPOrgSpecificTLV{OUI: 0x12bb, SubType: 0x1, Info: []uint8{0x0, 0xf, 0x4}}, 915 LLDPOrgSpecificTLV{OUI: 0x12bb, SubType: 0x2, Info: []uint8{0x1, 0x40, 0x65, 0xae}}, 916 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}}, 917 LLDPOrgSpecificTLV{OUI: 0x12bb, SubType: 0x4, Info: []uint8{0x3, 0x0, 0x41}}, 918 }, 919 Unknown: nil, 920 } 921 if !reflect.DeepEqual(info, wantinfo) { 922 t.Errorf("Values mismatch, \ngot %#v\nwant %#v\n", info, wantinfo) 923 } 924 info8023, err = info.Decode8023() 925 if err != nil { 926 t.Errorf("8023 Values decode error: %v", err) 927 } 928 want8023 = LLDPInfo8023{ 929 MACPHYConfigStatus: LLDPMACPHYConfigStatus{true, true, 0x6c00, 0x0010}, 930 } 931 932 if !reflect.DeepEqual(info8023, want8023) { 933 t.Errorf("Values mismatch, \ngot %#v\nwant %#v\n", info8023, want8023) 934 } 935 936 infoMedia, err := info.DecodeMedia() 937 if err != nil { 938 t.Errorf("8023 Values decode error: %v", err) 939 } 940 wantMedia := LLDPInfoMedia{ 941 MediaCapabilities: LLDPMediaCapabilities{true, true, true, true, false, false, LLDPMediaClassNetwork}, 942 NetworkPolicy: LLDPNetworkPolicy{LLDPAppTypeVoice, true, true, 50, 6, 46}, 943 Location: LLDPLocation{Format: LLDPLocationFormatAddress, Address: LLDPLocationAddress{ 944 What: LLDPLocationAddressWhatClient, 945 CountryCode: "US", 946 AddressLines: []LLDPLocationAddressLine{ 947 LLDPLocationAddressLine{LLDPLocationAddressTypeNational, "CA"}, 948 LLDPLocationAddressLine{LLDPLocationAddressTypeCity, "Roseville"}, 949 LLDPLocationAddressLine{LLDPLocationAddressTypeStreet, "Foothills"}, 950 LLDPLocationAddressLine{LLDPLocationAddressTypeHouseNum, "8000"}, 951 LLDPLocationAddressLine{LLDPLocationAddressTypeUnit, "R3L"}, 952 }, 953 }}, 954 PowerViaMDI: LLDPPowerViaMDI{0, 0, LLDPPowerPriorityLow, 6500}, 955 } 956 957 if !reflect.DeepEqual(infoMedia, wantMedia) { 958 t.Errorf("Values mismatch, \ngot %#v\nwant %#v\n", infoMedia, wantMedia) 959 } 960 961 } 962 963 func TestDecodeNortelDiscovery(t *testing.T) { 964 // http://www.thetechfirm.com/packets/nortel_btdp/btdp_nai.enc 965 data := []byte{ 966 0x01, 0x00, 0x81, 0x00, 0x01, 0x00, 0x00, 0x04, 0x38, 0xe0, 0xcc, 0xde, 967 0x00, 0x13, 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x81, 0x01, 0xa2, 0xac, 0x13, 968 0x58, 0x03, 0x00, 0x04, 0x15, 0x30, 0x0c, 0x02, 0x00, 0x00, 0x00, 0x00, 969 0x00, 0x04, 0x38, 0xe0, 0xcc, 0xde, 0x80, 0x6a, 0x00, 0x01, 0x14, 0x00, 970 0x02, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 971 } 972 p := gopacket.NewPacket(data, LinkTypeEthernet, testDecodeOptions) 973 wantLayers := []gopacket.LayerType{LayerTypeEthernet, LayerTypeLLC, LayerTypeSNAP, LayerTypeNortelDiscovery} 974 checkLayers(p, wantLayers, t) 975 976 want := &NortelDiscovery{ 977 IPAddress: []byte{172, 19, 88, 3}, 978 SegmentID: []byte{0x00, 0x04, 0x15}, 979 Chassis: NDPChassisBayStack450101001000Switches, 980 Backplane: NDPBackplaneEthernetFastEthernetGigabitEthernet, 981 State: NDPStateHeartbeat, 982 NumLinks: 0, 983 } 984 ndpL := p.Layer(LayerTypeNortelDiscovery) 985 info, _ := ndpL.(*NortelDiscovery) 986 if !reflect.DeepEqual(info, want) { 987 t.Errorf("Values mismatch, \ngot %#v\nwant %#v\n", info, want) 988 } 989 } 990 991 func TestDecodeIPv6Jumbogram(t *testing.T) { 992 // Haven't found any of these in the wild or on example pcaps online, so had 993 // to generate one myself via scapy. Unfortunately, scapy can only 994 // str(packet) for packets with length < 65536, due to limitations in python's 995 // struct library, so I generated the header with: 996 // Ether() / IPv6(src='::1', dst='::2') / IPv6ExtHdrHopByHop(options=[Jumbo(jumboplen=70000)]) / TCP(sport=8888, dport=80) 997 // then added the payload manually ("payload" * 9996). The checksums here are 998 // not correct, but we don't check, so who cares ;) 999 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" 1000 payload := strings.Repeat("payload", 9996) 1001 data := []byte(dataStr + payload) 1002 p := gopacket.NewPacket(data, LinkTypeEthernet, testDecodeOptions) 1003 checkLayers(p, []gopacket.LayerType{LayerTypeEthernet, LayerTypeIPv6, LayerTypeIPv6HopByHop, LayerTypeTCP, gopacket.LayerTypePayload}, t) 1004 if p.ApplicationLayer() == nil { 1005 t.Error("Packet has no application layer") 1006 } else if string(p.ApplicationLayer().Payload()) != payload { 1007 t.Errorf("Jumbogram payload wrong") 1008 } 1009 // Check truncated for jumbograms 1010 data = data[:len(data)-1] 1011 p = gopacket.NewPacket(data, LinkTypeEthernet, testDecodeOptions) 1012 checkLayers(p, []gopacket.LayerType{LayerTypeEthernet, LayerTypeIPv6, LayerTypeIPv6HopByHop, LayerTypeTCP, gopacket.LayerTypePayload}, t) 1013 if !p.Metadata().Truncated { 1014 t.Error("Jumbogram should be truncated") 1015 } 1016 } 1017 1018 func TestDecodeUDPPacketTooSmall(t *testing.T) { 1019 data := []byte{ 1020 0x00, 0x15, 0x2c, 0x9d, 0xcc, 0x00, 0x00, 0x10, 0xdb, 0xff, 0x10, 0x00, 0x81, 0x00, 0x01, 0xf7, 1021 0x08, 0x00, 0x45, 0x60, 0x00, 0x3c, 0x0f, 0xa9, 0x00, 0x00, 0x6e, 0x11, 0x01, 0x0a, 0x47, 0xe6, 1022 0xee, 0x2e, 0xac, 0x16, 0x59, 0x73, 0x00, 0x50, 0x00, 0x50, 0x00, 0x28, 0x4d, 0xad, 0x00, 0x67, 1023 0x00, 0x01, 0x00, 0x72, 0xd5, 0xc7, 0xf1, 0x07, 0x00, 0x00, 0x01, 0x01, 0x00, 0x0d, 0x00, 0x00, 1024 0x00, 0x14, 0x00, 0x00, 0x19, 0xba, 1025 } 1026 p := gopacket.NewPacket(data, LinkTypeEthernet, testDecodeOptions) 1027 checkLayers(p, []gopacket.LayerType{LayerTypeEthernet, LayerTypeDot1Q, LayerTypeIPv4, LayerTypeUDP, gopacket.LayerTypePayload}, t) 1028 if !p.Metadata().Truncated { 1029 t.Error("UDP short packet should be truncated") 1030 } 1031 } 1032 1033 func TestDecodingLayerParserFullTCPPacket(t *testing.T) { 1034 dlp := gopacket.NewDecodingLayerParser(LayerTypeEthernet, &Ethernet{}, &IPv4{}, &TCP{}, &gopacket.Payload{}) 1035 decoded := make([]gopacket.LayerType, 1) 1036 err := dlp.DecodeLayers(testSimpleTCPPacket, &decoded) 1037 if err != nil { 1038 t.Error("Error from dlp parser: ", err) 1039 } 1040 if len(decoded) != 4 { 1041 t.Error("Expected 4 layers parsed, instead got ", len(decoded)) 1042 } 1043 } 1044 1045 func testDecodingLayerContainer(t *testing.T, dlc gopacket.DecodingLayerContainer) { 1046 dlc = dlc.Put(&Ethernet{}) 1047 dlc = dlc.Put(&IPv4{}) 1048 dlc = dlc.Put(&TCP{}) 1049 dlc = dlc.Put(&gopacket.Payload{}) 1050 decoded := make([]gopacket.LayerType, 1) 1051 1052 // just as a DecodeFeedback 1053 df := gopacket.NewDecodingLayerParser(LayerTypeEthernet) 1054 decoder := dlc.LayersDecoder(LayerTypeEthernet, df) 1055 typ, err := decoder(testSimpleTCPPacket, &decoded) 1056 if err != nil { 1057 t.Error("Error from decoder: ", err) 1058 } 1059 if typ != gopacket.LayerTypeZero { 1060 t.Error("Unsupported layer type", typ) 1061 } 1062 if len(decoded) != 4 { 1063 t.Error("Expected 4 layers parsed, instead got ", len(decoded)) 1064 } 1065 } 1066 1067 func TestDecodingLayerMap(t *testing.T) { 1068 testDecodingLayerContainer(t, gopacket.DecodingLayerMap(nil)) 1069 } 1070 1071 func TestDecodingLayerSparse(t *testing.T) { 1072 testDecodingLayerContainer(t, gopacket.DecodingLayerSparse(nil)) 1073 } 1074 1075 func TestDecodingLayerArray(t *testing.T) { 1076 testDecodingLayerContainer(t, gopacket.DecodingLayerArray(nil)) 1077 } 1078 1079 // testICMP is the packet: 1080 // 1081 // 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 1082 // 0x0000: 24be 0527 0b17 001f cab3 75c0 0800 4500 $..'......u...E. 1083 // 0x0010: 0038 0000 0000 fc01 d7a7 480e dee2 ac1d .8........H..... 1084 // 0x0020: 140f 030d 946e 0000 0000 4520 004d 0000 .....n....E..M.. 1085 // 0x0030: 4000 3e11 2849 ac1d 140f 0a42 49c9 8ecc @.>.(I.....BI... 1086 // 0x0040: 62e1 0039 769d b..9v. 1087 var testICMP = []byte{ 1088 0x24, 0xbe, 0x05, 0x27, 0x0b, 0x17, 0x00, 0x1f, 0xca, 0xb3, 0x75, 0xc0, 0x08, 0x00, 0x45, 0x00, 1089 0x00, 0x38, 0x00, 0x00, 0x00, 0x00, 0xfc, 0x01, 0xd7, 0xa7, 0x48, 0x0e, 0xde, 0xe2, 0xac, 0x1d, 1090 0x14, 0x0f, 0x03, 0x0d, 0x94, 0x6e, 0x00, 0x00, 0x00, 0x00, 0x45, 0x20, 0x00, 0x4d, 0x00, 0x00, 1091 0x40, 0x00, 0x3e, 0x11, 0x28, 0x49, 0xac, 0x1d, 0x14, 0x0f, 0x0a, 0x42, 0x49, 0xc9, 0x8e, 0xcc, 1092 0x62, 0xe1, 0x00, 0x39, 0x76, 0x9d, 1093 } 1094 1095 func TestICMP(t *testing.T) { 1096 p := gopacket.NewPacket(testICMP, LinkTypeEthernet, testDecodeOptions) 1097 if p.ErrorLayer() != nil { 1098 t.Error("Failed to decode packet:", p.ErrorLayer().Error()) 1099 } 1100 checkLayers(p, []gopacket.LayerType{LayerTypeEthernet, LayerTypeIPv4, LayerTypeICMPv4, gopacket.LayerTypePayload}, t) 1101 testSerialization(t, p, testICMP) 1102 } 1103 func BenchmarkDecodeICMP(b *testing.B) { 1104 for i := 0; i < b.N; i++ { 1105 gopacket.NewPacket(testICMP, LinkTypeEthernet, gopacket.NoCopy) 1106 } 1107 } 1108 1109 // testICMP6 is the packet: 1110 // 1111 // 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 1112 // 0x0000: 24be 0527 0b17 001f cab3 75c0 86dd 6e00 $..'......u...n. 1113 // 0x0010: 0000 0020 3aff fe80 0000 0000 0000 021f ....:........... 1114 // 0x0020: caff feb3 75c0 2620 0000 1005 0000 26be ....u.&.......&. 1115 // 0x0030: 05ff fe27 0b17 8700 1eba 0000 0000 2620 ...'..........&. 1116 // 0x0040: 0000 1005 0000 26be 05ff fe27 0b17 0101 ......&....'.... 1117 // 0x0050: 001f cab3 75c0 ....u. 1118 var testICMP6 = []byte{ 1119 0x24, 0xbe, 0x05, 0x27, 0x0b, 0x17, 0x00, 0x1f, 0xca, 0xb3, 0x75, 0xc0, 0x86, 0xdd, 0x6e, 0x00, 1120 0x00, 0x00, 0x00, 0x20, 0x3a, 0xff, 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x1f, 1121 0xca, 0xff, 0xfe, 0xb3, 0x75, 0xc0, 0x26, 0x20, 0x00, 0x00, 0x10, 0x05, 0x00, 0x00, 0x26, 0xbe, 1122 0x05, 0xff, 0xfe, 0x27, 0x0b, 0x17, 0x87, 0x00, 0x1e, 0xba, 0x00, 0x00, 0x00, 0x00, 0x26, 0x20, 1123 0x00, 0x00, 0x10, 0x05, 0x00, 0x00, 0x26, 0xbe, 0x05, 0xff, 0xfe, 0x27, 0x0b, 0x17, 0x01, 0x01, 1124 0x00, 0x1f, 0xca, 0xb3, 0x75, 0xc0, 1125 } 1126 1127 func TestICMP6(t *testing.T) { 1128 p := gopacket.NewPacket(testICMP6, LinkTypeEthernet, testDecodeOptions) 1129 if p.ErrorLayer() != nil { 1130 t.Error("Failed to decode packet:", p.ErrorLayer().Error()) 1131 } 1132 checkLayers(p, []gopacket.LayerType{LayerTypeEthernet, LayerTypeIPv6, LayerTypeICMPv6, gopacket.LayerTypePayload}, t) 1133 testSerialization(t, p, testICMP6) 1134 } 1135 func BenchmarkDecodeICMP6(b *testing.B) { 1136 for i := 0; i < b.N; i++ { 1137 gopacket.NewPacket(testICMP6, LinkTypeEthernet, gopacket.NoCopy) 1138 } 1139 } 1140 1141 // testMPLS is the packet: 1142 // 1143 // 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 1144 // 0x0000: 0030 96e6 fc39 0030 9605 2838 8847 0001 .0...9.0..(8.G.. 1145 // 0x0010: d1ff 4500 0064 000b 0000 ff01 a569 0a01 ..E..d.......i.. 1146 // 0x0020: 0201 0a22 0001 0800 3a76 0a3a 062b 0000 ..."....:v.:.+.. 1147 // 0x0030: 0000 001f 3350 abcd abcd abcd abcd abcd ....3P.......... 1148 // 0x0040: abcd abcd abcd abcd abcd abcd abcd abcd ................ 1149 // 0x0050: abcd abcd abcd abcd abcd abcd abcd abcd ................ 1150 // 0x0060: abcd abcd abcd abcd abcd abcd abcd abcd ................ 1151 // 0x0070: abcd abcd abcd ...... 1152 var testMPLS = []byte{ 1153 0x00, 0x30, 0x96, 0xe6, 0xfc, 0x39, 0x00, 0x30, 0x96, 0x05, 0x28, 0x38, 0x88, 0x47, 0x00, 0x01, 1154 0xd1, 0xff, 0x45, 0x00, 0x00, 0x64, 0x00, 0x0b, 0x00, 0x00, 0xff, 0x01, 0xa5, 0x69, 0x0a, 0x01, 1155 0x02, 0x01, 0x0a, 0x22, 0x00, 0x01, 0x08, 0x00, 0x3a, 0x76, 0x0a, 0x3a, 0x06, 0x2b, 0x00, 0x00, 1156 0x00, 0x00, 0x00, 0x1f, 0x33, 0x50, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 1157 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 1158 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 1159 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 1160 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 1161 } 1162 1163 func TestMPLS(t *testing.T) { 1164 p := gopacket.NewPacket(testMPLS, LinkTypeEthernet, testDecodeOptions) 1165 if p.ErrorLayer() != nil { 1166 t.Error("Failed to decode packet:", p.ErrorLayer().Error()) 1167 } 1168 checkLayers(p, []gopacket.LayerType{LayerTypeEthernet, LayerTypeMPLS, LayerTypeIPv4, LayerTypeICMPv4, gopacket.LayerTypePayload}, t) 1169 testSerialization(t, p, testMPLS) 1170 } 1171 func BenchmarkDecodeMPLS(b *testing.B) { 1172 for i := 0; i < b.N; i++ { 1173 gopacket.NewPacket(testMPLS, LinkTypeEthernet, gopacket.NoCopy) 1174 } 1175 } 1176 1177 // testPPPGREIPv4IPv6VLAN is the packet from http://packetlife.net/captures/gre_and_4over6.cap 1178 // 1179 // 0x0000: 6000 0000 008b 04f6 2402 f000 0001 8e01 `.......$....... 1180 // 0x0010: 0000 0000 0000 5555 2607 fcd0 0100 2300 ......UU&.....#. 1181 // 0x0020: 0000 0000 b108 2a6b 4500 008b 8caf 0000 ......*kE....... 1182 // 0x0030: 402f 75fe 1000 00c8 c034 a69a 3081 880b @/u......4..0... 1183 // 0x0040: 0067 1780 0006 8fb1 0008 3a76 ff03 0021 .g........:v...! 1184 // 0x0050: 4500 0063 0000 4000 3c11 5667 ac10 2c03 E..c..@.<.Vg..,. 1185 // 0x0060: 0808 0808 9f40 0035 004f 2d23 a62c 0100 .....@.5.O-#.,.. 1186 // 0x0070: 0001 0000 0000 0000 3578 7174 2d64 6574 ........5xqt-det 1187 // 0x0080: 6563 742d 6d6f 6465 322d 3937 3731 3265 ect-mode2-97712e 1188 // 0x0090: 3838 2d31 3637 612d 3435 6239 2d39 3365 88-167a-45b9-93e 1189 // 0x00a0: 652d 3931 3331 3430 6537 3636 3738 0000 e-913140e76678.. 1190 // 0x00b0: 1c00 01 1191 // 1192 //04:35:03.821897 IP6 2402:f000:1:8e01::5555 > 2607:fcd0:100:2300::b108:2a6b: IP 16.0.0.200 > 192.52.166.154: GREv1, call 6016, seq 430001, ack 539254, length 119: IP 172.16.44.3.40768 > 8.8.8.8.53: 42540+ AAAA? xqt-detect-mode2-97712e88-167a-45b9-93ee-913140e76678. (71) 1193 var testPPPGREIPv4IPv6VLAN = []byte{ 1194 0xc5, 0x00, 0x00, 0x00, 0x82, 0xc4, 0x00, 0x12, 0x1e, 0xf2, 0x61, 0x3d, 0x81, 0x00, 0x00, 0x64, 1195 0x86, 0xdd, 0x60, 0x00, 0x00, 0x00, 0x00, 0x8b, 0x04, 0xf6, 0x24, 0x02, 0xf0, 0x00, 0x00, 0x01, 1196 0x8e, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, 0x55, 0x26, 0x07, 0xfc, 0xd0, 0x01, 0x00, 1197 0x23, 0x00, 0x00, 0x00, 0x00, 0x00, 0xb1, 0x08, 0x2a, 0x6b, 0x45, 0x00, 0x00, 0x8b, 0x8c, 0xaf, 1198 0x00, 0x00, 0x40, 0x2f, 0x75, 0xfe, 0x10, 0x00, 0x00, 0xc8, 0xc0, 0x34, 0xa6, 0x9a, 0x30, 0x81, 1199 0x88, 0x0b, 0x00, 0x67, 0x17, 0x80, 0x00, 0x06, 0x8f, 0xb1, 0x00, 0x08, 0x3a, 0x76, 0xff, 0x03, 1200 0x00, 0x21, 0x45, 0x00, 0x00, 0x63, 0x00, 0x00, 0x40, 0x00, 0x3c, 0x11, 0x56, 0x67, 0xac, 0x10, 1201 0x2c, 0x03, 0x08, 0x08, 0x08, 0x08, 0x9f, 0x40, 0x00, 0x35, 0x00, 0x4f, 0xfb, 0x9f, 0xa6, 0x2c, 1202 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x35, 0x78, 0x71, 0x74, 0x2d, 0x64, 1203 0x65, 0x74, 0x65, 0x63, 0x74, 0x2d, 0x6d, 0x6f, 0x64, 0x65, 0x32, 0x2d, 0x39, 0x37, 0x37, 0x31, 1204 0x32, 0x65, 0x38, 0x38, 0x2d, 0x31, 0x36, 0x37, 0x61, 0x2d, 0x34, 0x35, 0x62, 0x39, 0x2d, 0x39, 1205 0x33, 0x65, 0x65, 0x2d, 0x39, 0x31, 0x33, 0x31, 0x34, 0x30, 0x65, 0x37, 0x36, 0x36, 0x37, 0x38, 1206 0x00, 0x00, 0x1c, 0x00, 0x01, 1207 } 1208 1209 func TestPPPGREIPv4IPv6VLAN(t *testing.T) { 1210 p := gopacket.NewPacket(testPPPGREIPv4IPv6VLAN, LinkTypeEthernet, testDecodeOptions) 1211 if p.ErrorLayer() != nil { 1212 t.Error("Failed to decode packet:", p.ErrorLayer().Error()) 1213 } 1214 checkLayers(p, []gopacket.LayerType{ 1215 LayerTypeEthernet, 1216 LayerTypeDot1Q, 1217 LayerTypeIPv6, 1218 LayerTypeIPv4, 1219 LayerTypeGRE, 1220 LayerTypePPP, 1221 LayerTypeIPv4, 1222 LayerTypeUDP, 1223 LayerTypeDNS, 1224 }, t) 1225 testSerialization(t, p, testPPPGREIPv4IPv6VLAN) 1226 } 1227 1228 // testPPPoEICMPv6 is the packet: 1229 // 1230 // 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 1231 // 0x0000: cc05 0e88 0000 ca01 0e88 0006 8864 1100 .............d.. 1232 // 0x0010: 0011 0042 0057 6e00 0000 0018 3aff fe80 ...B.Wn.....:... 1233 // 0x0020: 0000 0000 0000 c801 0eff fe88 0008 ff02 ................ 1234 // 0x0030: 0000 0000 0000 0000 0000 0000 0001 8800 ................ 1235 // 0x0040: 5083 8000 0000 fe80 0000 0000 0000 c801 P............... 1236 // 0x0050: 0eff fe88 0008 ...... 1237 var testPPPoEICMPv6 = []byte{ 1238 0xcc, 0x05, 0x0e, 0x88, 0x00, 0x00, 0xca, 0x01, 0x0e, 0x88, 0x00, 0x06, 0x88, 0x64, 0x11, 0x00, 1239 0x00, 0x11, 0x00, 0x42, 0x00, 0x57, 0x6e, 0x00, 0x00, 0x00, 0x00, 0x18, 0x3a, 0xff, 0xfe, 0x80, 1240 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc8, 0x01, 0x0e, 0xff, 0xfe, 0x88, 0x00, 0x08, 0xff, 0x02, 1241 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x88, 0x00, 1242 0x50, 0x83, 0x80, 0x00, 0x00, 0x00, 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc8, 0x01, 1243 0x0e, 0xff, 0xfe, 0x88, 0x00, 0x08, 1244 } 1245 1246 func TestPPPoEICMPv6(t *testing.T) { 1247 p := gopacket.NewPacket(testPPPoEICMPv6, LinkTypeEthernet, testDecodeOptions) 1248 if p.ErrorLayer() != nil { 1249 t.Error("Failed to decode packet:", p.ErrorLayer().Error()) 1250 } 1251 checkLayers(p, []gopacket.LayerType{ 1252 LayerTypeEthernet, 1253 LayerTypePPPoE, 1254 LayerTypePPP, 1255 LayerTypeIPv6, 1256 LayerTypeICMPv6, 1257 gopacket.LayerTypePayload, 1258 }, t) 1259 testSerialization(t, p, testPPPoEICMPv6) 1260 } 1261 func BenchmarkDecodePPPoEICMPv6(b *testing.B) { 1262 for i := 0; i < b.N; i++ { 1263 gopacket.NewPacket(testPPPoEICMPv6, LinkTypeEthernet, gopacket.NoCopy) 1264 } 1265 } 1266 1267 var testPFLogUDP = []byte{ 1268 0x3d, 0x02, 0x00, 0x00, 0x65, 0x6e, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 1269 0x00, 0x00, 0x00, 0x00, 0x6c, 0x6f, 0x67, 0x67, 0x69, 0x6e, 0x67, 0x00, 0x00, 0x00, 0x00, 0x00, 1270 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xae, 0xff, 0xff, 0xff, 0x7f, 1271 0xa0, 0x86, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0xb8, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 1272 0x45, 0x00, 0x00, 0x2c, 0x02, 0x16, 0x00, 0x00, 0x40, 0x11, 0x4e, 0xb0, 0xac, 0x17, 0xe8, 0xcc, 1273 0xac, 0x17, 0xe8, 0xff, 0xf0, 0xff, 0x21, 0xa4, 0x00, 0x18, 0x2a, 0x25, 0x50, 0x4e, 0x4a, 0x50, 1274 0x01, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 1275 } 1276 1277 func TestPFLogUDP(t *testing.T) { 1278 p := gopacket.NewPacket(testPFLogUDP, LinkTypePFLog, testDecodeOptions) 1279 if p.ErrorLayer() != nil { 1280 t.Error("Failed to decode packet:", p.ErrorLayer().Error()) 1281 } 1282 checkLayers(p, []gopacket.LayerType{ 1283 LayerTypePFLog, 1284 LayerTypeIPv4, 1285 LayerTypeUDP, 1286 gopacket.LayerTypePayload, 1287 }, t) 1288 } 1289 1290 func TestRegressionDot1QPriority(t *testing.T) { 1291 d := &Dot1Q{ 1292 Priority: 2, 1293 } 1294 out := gopacket.NewSerializeBuffer() 1295 gopacket.SerializeLayers(out, gopacket.SerializeOptions{}, d) 1296 if err := d.DecodeFromBytes(out.Bytes(), gopacket.NilDecodeFeedback); err != nil { 1297 t.Errorf("could not decode encoded dot1q") 1298 } else if d.Priority != 2 { 1299 t.Errorf("priority mismatch, want 2 got %d", d.Priority) 1300 } 1301 } 1302 1303 // testPacketMPLSInMPLS is the packet: 1304 // 1305 // 15:27:44.753678 MPLS (label 18, exp 0, ttl 255) (label 16, exp 0, [S], ttl 1306 // 255) IP 10.31.0.1 > 10.34.0.1: ICMP echo request, id 3941, seq 4768, length 1307 // 80 1308 // 0x0000: 0030 96e6 fc39 0030 9605 2838 8847 0001 .0...9.0..(8.G.. 1309 // 0x0010: 20ff 0001 01ff 4500 0064 0050 0000 ff01 ......E..d.P.... 1310 // 0x0020: a706 0a1f 0001 0a22 0001 0800 bd11 0f65 .......".......e 1311 // 0x0030: 12a0 0000 0000 0053 9ee0 abcd abcd abcd .......S........ 1312 // 0x0040: abcd abcd abcd abcd abcd abcd abcd abcd ................ 1313 // 0x0050: abcd abcd abcd abcd abcd abcd abcd abcd ................ 1314 // 0x0060: abcd abcd abcd abcd abcd abcd abcd abcd ................ 1315 // 0x0070: abcd abcd abcd abcd abcd .......... 1316 var testPacketMPLSInMPLS = []byte{ 1317 0x00, 0x30, 0x96, 0xe6, 0xfc, 0x39, 0x00, 0x30, 0x96, 0x05, 0x28, 0x38, 0x88, 0x47, 0x00, 0x01, 1318 0x20, 0xff, 0x00, 0x01, 0x01, 0xff, 0x45, 0x00, 0x00, 0x64, 0x00, 0x50, 0x00, 0x00, 0xff, 0x01, 1319 0xa7, 0x06, 0x0a, 0x1f, 0x00, 0x01, 0x0a, 0x22, 0x00, 0x01, 0x08, 0x00, 0xbd, 0x11, 0x0f, 0x65, 1320 0x12, 0xa0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x53, 0x9e, 0xe0, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 1321 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 1322 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 1323 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 1324 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 1325 } 1326 1327 func TestPacketMPLSInMPLS(t *testing.T) { 1328 p := gopacket.NewPacket(testPacketMPLSInMPLS, LinkTypeEthernet, testDecodeOptions) 1329 if p.ErrorLayer() != nil { 1330 t.Error("Failed to decode packet:", p.ErrorLayer().Error()) 1331 } 1332 checkLayers(p, []gopacket.LayerType{ 1333 LayerTypeEthernet, 1334 LayerTypeMPLS, 1335 LayerTypeMPLS, 1336 LayerTypeIPv4, 1337 LayerTypeICMPv4, 1338 gopacket.LayerTypePayload}, t) 1339 } 1340 1341 // testPacketIPv4Fragmented is the packet: 1342 // 1343 // 22:11:26.616090 IP 10.1.1.1.31915 > 129.111.30.27.20197: UDP, length 28 1344 // 0x0000: 0000 39cf d9cd 0040 33d9 7cfd 0800 4500 ..9....@3.|...E. 1345 // 0x0010: 0038 00f2 2000 4011 af37 0a01 0101 816f .8....@..7.....o 1346 // 0x0020: 1e1b 7cab 4ee5 0024 0000 0000 0000 0000 ..|.N..$........ 1347 // 0x0030: 0000 0000 0000 0000 0000 0000 0000 0000 ................ 1348 // 0x0040: 0000 0000 0000 ...... 1349 var testPacketIPv4Fragmented = []byte{ 1350 0x00, 0x00, 0x39, 0xcf, 0xd9, 0xcd, 0x00, 0x40, 0x33, 0xd9, 0x7c, 0xfd, 0x08, 0x00, 0x45, 0x00, 1351 0x00, 0x38, 0x00, 0xf2, 0x20, 0x00, 0x40, 0x11, 0xaf, 0x37, 0x0a, 0x01, 0x01, 0x01, 0x81, 0x6f, 1352 0x1e, 0x1b, 0x7c, 0xab, 0x4e, 0xe5, 0x00, 0x24, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 1353 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 1354 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 1355 } 1356 1357 func TestPacketIPv4Fragmented(t *testing.T) { 1358 p := gopacket.NewPacket(testPacketIPv4Fragmented, LinkTypeEthernet, testDecodeOptions) 1359 if p.ErrorLayer() != nil { 1360 t.Error("Failed to decode packet:", p.ErrorLayer().Error()) 1361 } 1362 checkLayers(p, []gopacket.LayerType{LayerTypeEthernet, LayerTypeIPv4, gopacket.LayerTypeFragment}, t) 1363 testSerializationWithOpts(t, p, testPacketIPv4Fragmented, gopacket.SerializeOptions{FixLengths: true, ComputeChecksums: true}) 1364 } 1365 1366 // TestSCTPChunkBadLength tests for issue #146 1367 func TestSCTPChunkBadLength(t *testing.T) { 1368 data := []byte( 1369 "0000\xad9$e\x11\xe4\xaeo\b\x00E\x00\x018\xb4\xa3" + 1370 "\x00\x00Y\x84\xc4@\x11gz\xc0\xa8\xee\x01\xc0\xa8" + 1371 "\xeeD\x007le\x03\x01\xc0\f\xdf\b\x01\x00\x00") 1372 1373 // this panic'd previously due to a zero length chunk getting 1374 // repeatedly read 1375 gopacket.NewPacket(data, LinkTypeEthernet, gopacket.Default) 1376 } 1377 1378 // TestSTP 1379 func TestSTP(t *testing.T) { 1380 testSTPpacket := []byte{ 1381 0x01, 0x80, 0xC2, 0x00, 0x00, 0x00, 0x00, 0x1C, 0x0E, 0x87, 0x85, 0x04, 0x00, 0x26, 0x42, 0x42, 1382 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x64, 0x00, 0x1C, 0x0E, 0x87, 0x78, 0x00, 0x00, 0x00, 1383 0x00, 0x04, 0x80, 0x64, 0x00, 0x1C, 0x0E, 0x87, 0x85, 0x00, 0x80, 0x04, 0x01, 0x00, 0x14, 0x00, 1384 0x02, 0x00, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 1385 } 1386 p := gopacket.NewPacket(testSTPpacket, LinkTypeEthernet, testDecodeOptions) 1387 checkLayers(p, []gopacket.LayerType{LayerTypeEthernet, LayerTypeLLC, LayerTypeSTP}, t) 1388 }