github.com/gopacket/gopacket@v1.1.0/packet.go (about) 1 // Copyright 2012 Google, Inc. All rights reserved. 2 // 3 // Use of this source code is governed by a BSD-style license 4 // that can be found in the LICENSE file in the root of the source 5 // tree. 6 7 package gopacket 8 9 import ( 10 "bytes" 11 "context" 12 "encoding/hex" 13 "errors" 14 "fmt" 15 "io" 16 "net" 17 "os" 18 "reflect" 19 "runtime/debug" 20 "strings" 21 "sync" 22 "syscall" 23 "time" 24 ) 25 26 const maximumMTU = 1500 27 28 var ( 29 ErrNoLayersAdded = errors.New("NextDecoder called, but no layers added yet") 30 poolPackedPool = &sync.Pool{ 31 New: func() interface{} { 32 b := make([]byte, maximumMTU) 33 return &b 34 }, 35 } 36 ) 37 38 // CaptureInfo provides standardized information about a packet captured off 39 // the wire or read from a file. 40 type CaptureInfo struct { 41 // Timestamp is the time the packet was captured, if that is known. 42 Timestamp time.Time 43 // CaptureLength is the total number of bytes read off of the wire. 44 CaptureLength int 45 // Length is the size of the original packet. Should always be >= 46 // CaptureLength. 47 Length int 48 // InterfaceIndex 49 InterfaceIndex int 50 // The packet source can place ancillary data of various types here. 51 // For example, the afpacket source can report the VLAN of captured 52 // packets this way. 53 AncillaryData []interface{} 54 } 55 56 // PacketMetadata contains metadata for a packet. 57 type PacketMetadata struct { 58 CaptureInfo 59 // Truncated is true if packet decoding logic detects that there are fewer 60 // bytes in the packet than are detailed in various headers (for example, if 61 // the number of bytes in the IPv4 contents/payload is less than IPv4.Length). 62 // This is also set automatically for packets captured off the wire if 63 // CaptureInfo.CaptureLength < CaptureInfo.Length. 64 Truncated bool 65 } 66 67 // Packet is the primary object used by gopacket. Packets are created by a 68 // Decoder's Decode call. A packet is made up of a set of Data, which 69 // is broken into a number of Layers as it is decoded. 70 type Packet interface { 71 //// Functions for outputting the packet as a human-readable string: 72 //// ------------------------------------------------------------------ 73 // String returns a human-readable string representation of the packet. 74 // It uses LayerString on each layer to output the layer. 75 String() string 76 // Dump returns a verbose human-readable string representation of the packet, 77 // including a hex dump of all layers. It uses LayerDump on each layer to 78 // output the layer. 79 Dump() string 80 81 //// Functions for accessing arbitrary packet layers: 82 //// ------------------------------------------------------------------ 83 // Layers returns all layers in this packet, computing them as necessary 84 Layers() []Layer 85 // Layer returns the first layer in this packet of the given type, or nil 86 Layer(LayerType) Layer 87 // LayerClass returns the first layer in this packet of the given class, 88 // or nil. 89 LayerClass(LayerClass) Layer 90 91 //// Functions for accessing specific types of packet layers. These functions 92 //// return the first layer of each type found within the packet. 93 //// ------------------------------------------------------------------ 94 // LinkLayer returns the first link layer in the packet 95 LinkLayer() LinkLayer 96 // NetworkLayer returns the first network layer in the packet 97 NetworkLayer() NetworkLayer 98 // TransportLayer returns the first transport layer in the packet 99 TransportLayer() TransportLayer 100 // ApplicationLayer returns the first application layer in the packet 101 ApplicationLayer() ApplicationLayer 102 // ErrorLayer is particularly useful, since it returns nil if the packet 103 // was fully decoded successfully, and non-nil if an error was encountered 104 // in decoding and the packet was only partially decoded. Thus, its output 105 // can be used to determine if the entire packet was able to be decoded. 106 ErrorLayer() ErrorLayer 107 108 //// Functions for accessing data specific to the packet: 109 //// ------------------------------------------------------------------ 110 // Data returns the set of bytes that make up this entire packet. 111 Data() []byte 112 // Metadata returns packet metadata associated with this packet. 113 Metadata() *PacketMetadata 114 } 115 116 type PooledPacket interface { 117 Packet 118 Dispose() 119 } 120 121 // packet contains all the information we need to fulfill the Packet interface, 122 // and its two "subclasses" (yes, no such thing in Go, bear with me), 123 // eagerPacket and lazyPacket, provide eager and lazy decoding logic around the 124 // various functions needed to access this information. 125 type packet struct { 126 // data contains the entire packet data for a packet 127 data []byte 128 // initialLayers is space for an initial set of layers already created inside 129 // the packet. 130 initialLayers [6]Layer 131 // layers contains each layer we've already decoded 132 layers []Layer 133 // last is the last layer added to the packet 134 last Layer 135 // metadata is the PacketMetadata for this packet 136 metadata PacketMetadata 137 138 decodeOptions DecodeOptions 139 140 // Pointers to the various important layers 141 link LinkLayer 142 network NetworkLayer 143 transport TransportLayer 144 application ApplicationLayer 145 failure ErrorLayer 146 } 147 148 func (p *packet) SetTruncated() { 149 p.metadata.Truncated = true 150 } 151 152 func (p *packet) SetLinkLayer(l LinkLayer) { 153 if p.link == nil { 154 p.link = l 155 } 156 } 157 158 func (p *packet) SetNetworkLayer(l NetworkLayer) { 159 if p.network == nil { 160 p.network = l 161 } 162 } 163 164 func (p *packet) SetTransportLayer(l TransportLayer) { 165 if p.transport == nil { 166 p.transport = l 167 } 168 } 169 170 func (p *packet) SetApplicationLayer(l ApplicationLayer) { 171 if p.application == nil { 172 p.application = l 173 } 174 } 175 176 func (p *packet) SetErrorLayer(l ErrorLayer) { 177 if p.failure == nil { 178 p.failure = l 179 } 180 } 181 182 func (p *packet) AddLayer(l Layer) { 183 p.layers = append(p.layers, l) 184 p.last = l 185 } 186 187 func (p *packet) DumpPacketData() { 188 fmt.Fprint(os.Stderr, p.packetDump()) 189 os.Stderr.Sync() 190 } 191 192 func (p *packet) Metadata() *PacketMetadata { 193 return &p.metadata 194 } 195 196 func (p *packet) Data() []byte { 197 return p.data 198 } 199 200 func (p *packet) DecodeOptions() *DecodeOptions { 201 return &p.decodeOptions 202 } 203 204 func (p *packet) addFinalDecodeError(err error, stack []byte) { 205 fail := &DecodeFailure{err: err, stack: stack} 206 if p.last == nil { 207 fail.data = p.data 208 } else { 209 fail.data = p.last.LayerPayload() 210 } 211 p.AddLayer(fail) 212 p.SetErrorLayer(fail) 213 } 214 215 func (p *packet) recoverDecodeError() { 216 if !p.decodeOptions.SkipDecodeRecovery { 217 if r := recover(); r != nil { 218 p.addFinalDecodeError(fmt.Errorf("%v", r), debug.Stack()) 219 } 220 } 221 } 222 223 type pooledPacket struct { 224 Packet 225 origData *[]byte 226 } 227 228 func (p pooledPacket) Dispose() { 229 poolPackedPool.Put(p.origData) 230 } 231 232 // LayerString outputs an individual layer as a string. The layer is output 233 // in a single line, with no trailing newline. This function is specifically 234 // designed to do the right thing for most layers... it follows the following 235 // rules: 236 // - If the Layer has a String function, just output that. 237 // - Otherwise, output all exported fields in the layer, recursing into 238 // exported slices and structs. 239 // 240 // NOTE: This is NOT THE SAME AS fmt's "%#v". %#v will output both exported 241 // and unexported fields... many times packet layers contain unexported stuff 242 // that would just mess up the output of the layer, see for example the 243 // Payload layer and it's internal 'data' field, which contains a large byte 244 // array that would really mess up formatting. 245 func LayerString(l Layer) string { 246 return fmt.Sprintf("%v\t%s", l.LayerType(), layerString(reflect.ValueOf(l), false, false)) 247 } 248 249 // Dumper dumps verbose information on a value. If a layer type implements 250 // Dumper, then its LayerDump() string will include the results in its output. 251 type Dumper interface { 252 Dump() string 253 } 254 255 // LayerDump outputs a very verbose string representation of a layer. Its 256 // output is a concatenation of LayerString(l) and hex.Dump(l.LayerContents()). 257 // It contains newlines and ends with a newline. 258 func LayerDump(l Layer) string { 259 var b bytes.Buffer 260 b.WriteString(LayerString(l)) 261 b.WriteByte('\n') 262 if d, ok := l.(Dumper); ok { 263 dump := d.Dump() 264 if dump != "" { 265 b.WriteString(dump) 266 if dump[len(dump)-1] != '\n' { 267 b.WriteByte('\n') 268 } 269 } 270 } 271 b.WriteString(hex.Dump(l.LayerContents())) 272 return b.String() 273 } 274 275 // layerString outputs, recursively, a layer in a "smart" way. See docs for 276 // LayerString for more details. 277 // 278 // Params: 279 // 280 // i - value to write out 281 // anonymous: if we're currently recursing an anonymous member of a struct 282 // writeSpace: if we've already written a value in a struct, and need to 283 // write a space before writing more. This happens when we write various 284 // anonymous values, and need to keep writing more. 285 func layerString(v reflect.Value, anonymous bool, writeSpace bool) string { 286 // Let String() functions take precedence. 287 if v.CanInterface() { 288 if s, ok := v.Interface().(fmt.Stringer); ok { 289 return s.String() 290 } 291 } 292 // Reflect, and spit out all the exported fields as key=value. 293 switch v.Type().Kind() { 294 case reflect.Interface, reflect.Ptr: 295 if v.IsNil() { 296 return "nil" 297 } 298 r := v.Elem() 299 return layerString(r, anonymous, writeSpace) 300 case reflect.Struct: 301 var b bytes.Buffer 302 typ := v.Type() 303 if !anonymous { 304 b.WriteByte('{') 305 } 306 for i := 0; i < v.NumField(); i++ { 307 // Check if this is upper-case. 308 ftype := typ.Field(i) 309 f := v.Field(i) 310 if ftype.Anonymous { 311 anonStr := layerString(f, true, writeSpace) 312 writeSpace = writeSpace || anonStr != "" 313 b.WriteString(anonStr) 314 } else if ftype.PkgPath == "" { // exported 315 if writeSpace { 316 b.WriteByte(' ') 317 } 318 writeSpace = true 319 fmt.Fprintf(&b, "%s=%s", typ.Field(i).Name, layerString(f, false, writeSpace)) 320 } 321 } 322 if !anonymous { 323 b.WriteByte('}') 324 } 325 return b.String() 326 case reflect.Slice: 327 var b bytes.Buffer 328 b.WriteByte('[') 329 if v.Len() > 4 { 330 fmt.Fprintf(&b, "..%d..", v.Len()) 331 } else { 332 for j := 0; j < v.Len(); j++ { 333 if j != 0 { 334 b.WriteString(", ") 335 } 336 b.WriteString(layerString(v.Index(j), false, false)) 337 } 338 } 339 b.WriteByte(']') 340 return b.String() 341 } 342 return fmt.Sprintf("%v", v.Interface()) 343 } 344 345 const ( 346 longBytesLength = 128 347 ) 348 349 // LongBytesGoString returns a string representation of the byte slice shortened 350 // using the format '<type>{<truncated slice> ... (<n> bytes)}' if it 351 // exceeds a predetermined length. Can be used to avoid filling the display with 352 // very long byte strings. 353 func LongBytesGoString(buf []byte) string { 354 if len(buf) < longBytesLength { 355 return fmt.Sprintf("%#v", buf) 356 } 357 s := fmt.Sprintf("%#v", buf[:longBytesLength-1]) 358 s = strings.TrimSuffix(s, "}") 359 return fmt.Sprintf("%s ... (%d bytes)}", s, len(buf)) 360 } 361 362 func baseLayerString(value reflect.Value) string { 363 t := value.Type() 364 content := value.Field(0) 365 c := make([]byte, content.Len()) 366 for i := range c { 367 c[i] = byte(content.Index(i).Uint()) 368 } 369 payload := value.Field(1) 370 p := make([]byte, payload.Len()) 371 for i := range p { 372 p[i] = byte(payload.Index(i).Uint()) 373 } 374 return fmt.Sprintf("%s{Contents:%s, Payload:%s}", t.String(), 375 LongBytesGoString(c), 376 LongBytesGoString(p)) 377 } 378 379 func layerGoString(i interface{}, b *bytes.Buffer) { 380 if s, ok := i.(fmt.GoStringer); ok { 381 b.WriteString(s.GoString()) 382 return 383 } 384 385 var v reflect.Value 386 var ok bool 387 if v, ok = i.(reflect.Value); !ok { 388 v = reflect.ValueOf(i) 389 } 390 switch v.Kind() { 391 case reflect.Ptr, reflect.Interface: 392 if v.Kind() == reflect.Ptr { 393 b.WriteByte('&') 394 } 395 layerGoString(v.Elem().Interface(), b) 396 case reflect.Struct: 397 t := v.Type() 398 b.WriteString(t.String()) 399 b.WriteByte('{') 400 for i := 0; i < v.NumField(); i++ { 401 if i > 0 { 402 b.WriteString(", ") 403 } 404 if t.Field(i).Name == "BaseLayer" { 405 fmt.Fprintf(b, "BaseLayer:%s", baseLayerString(v.Field(i))) 406 } else if v.Field(i).Kind() == reflect.Struct { 407 fmt.Fprintf(b, "%s:", t.Field(i).Name) 408 layerGoString(v.Field(i), b) 409 } else if v.Field(i).Kind() == reflect.Ptr { 410 b.WriteByte('&') 411 layerGoString(v.Field(i), b) 412 } else { 413 fmt.Fprintf(b, "%s:%#v", t.Field(i).Name, v.Field(i)) 414 } 415 } 416 b.WriteByte('}') 417 default: 418 fmt.Fprintf(b, "%#v", i) 419 } 420 } 421 422 // LayerGoString returns a representation of the layer in Go syntax, 423 // taking care to shorten "very long" BaseLayer byte slices 424 func LayerGoString(l Layer) string { 425 b := new(bytes.Buffer) 426 layerGoString(l, b) 427 return b.String() 428 } 429 430 func (p *packet) packetString() string { 431 var b bytes.Buffer 432 fmt.Fprintf(&b, "PACKET: %d bytes", len(p.Data())) 433 if p.metadata.Truncated { 434 b.WriteString(", truncated") 435 } 436 if p.metadata.Length > 0 { 437 fmt.Fprintf(&b, ", wire length %d cap length %d", p.metadata.Length, p.metadata.CaptureLength) 438 } 439 if !p.metadata.Timestamp.IsZero() { 440 fmt.Fprintf(&b, " @ %v", p.metadata.Timestamp) 441 } 442 b.WriteByte('\n') 443 for i, l := range p.layers { 444 fmt.Fprintf(&b, "- Layer %d (%02d bytes) = %s\n", i+1, len(l.LayerContents()), LayerString(l)) 445 } 446 return b.String() 447 } 448 449 func (p *packet) packetDump() string { 450 var b bytes.Buffer 451 fmt.Fprintf(&b, "-- FULL PACKET DATA (%d bytes) ------------------------------------\n%s", len(p.data), hex.Dump(p.data)) 452 for i, l := range p.layers { 453 fmt.Fprintf(&b, "--- Layer %d ---\n%s", i+1, LayerDump(l)) 454 } 455 return b.String() 456 } 457 458 // eagerPacket is a packet implementation that does eager decoding. Upon 459 // initial construction, it decodes all the layers it can from packet data. 460 // eagerPacket implements Packet and PacketBuilder. 461 type eagerPacket struct { 462 packet 463 } 464 465 var errNilDecoder = errors.New("NextDecoder passed nil decoder, probably an unsupported decode type") 466 467 func (p *eagerPacket) NextDecoder(next Decoder) error { 468 if next == nil { 469 return errNilDecoder 470 } 471 if p.last == nil { 472 return ErrNoLayersAdded 473 } 474 d := p.last.LayerPayload() 475 if len(d) == 0 { 476 return nil 477 } 478 // Since we're eager, immediately call the next decoder. 479 return next.Decode(d, p) 480 } 481 func (p *eagerPacket) initialDecode(dec Decoder) { 482 defer p.recoverDecodeError() 483 err := dec.Decode(p.data, p) 484 if err != nil { 485 p.addFinalDecodeError(err, nil) 486 } 487 } 488 func (p *eagerPacket) LinkLayer() LinkLayer { 489 return p.link 490 } 491 func (p *eagerPacket) NetworkLayer() NetworkLayer { 492 return p.network 493 } 494 func (p *eagerPacket) TransportLayer() TransportLayer { 495 return p.transport 496 } 497 func (p *eagerPacket) ApplicationLayer() ApplicationLayer { 498 return p.application 499 } 500 func (p *eagerPacket) ErrorLayer() ErrorLayer { 501 return p.failure 502 } 503 func (p *eagerPacket) Layers() []Layer { 504 return p.layers 505 } 506 func (p *eagerPacket) Layer(t LayerType) Layer { 507 for _, l := range p.layers { 508 if l.LayerType() == t { 509 return l 510 } 511 } 512 return nil 513 } 514 func (p *eagerPacket) LayerClass(lc LayerClass) Layer { 515 for _, l := range p.layers { 516 if lc.Contains(l.LayerType()) { 517 return l 518 } 519 } 520 return nil 521 } 522 func (p *eagerPacket) String() string { return p.packetString() } 523 func (p *eagerPacket) Dump() string { return p.packetDump() } 524 525 // lazyPacket does lazy decoding on its packet data. On construction it does 526 // no initial decoding. For each function call, it decodes only as many layers 527 // as are necessary to compute the return value for that function. 528 // lazyPacket implements Packet and PacketBuilder. 529 type lazyPacket struct { 530 packet 531 next Decoder 532 } 533 534 func (p *lazyPacket) NextDecoder(next Decoder) error { 535 if next == nil { 536 return errNilDecoder 537 } 538 p.next = next 539 return nil 540 } 541 func (p *lazyPacket) decodeNextLayer() { 542 if p.next == nil { 543 return 544 } 545 d := p.data 546 if p.last != nil { 547 d = p.last.LayerPayload() 548 } 549 next := p.next 550 p.next = nil 551 // We've just set p.next to nil, so if we see we have no data, this should be 552 // the final call we get to decodeNextLayer if we return here. 553 if len(d) == 0 { 554 return 555 } 556 defer p.recoverDecodeError() 557 err := next.Decode(d, p) 558 if err != nil { 559 p.addFinalDecodeError(err, nil) 560 } 561 } 562 func (p *lazyPacket) LinkLayer() LinkLayer { 563 for p.link == nil && p.next != nil { 564 p.decodeNextLayer() 565 } 566 return p.link 567 } 568 func (p *lazyPacket) NetworkLayer() NetworkLayer { 569 for p.network == nil && p.next != nil { 570 p.decodeNextLayer() 571 } 572 return p.network 573 } 574 func (p *lazyPacket) TransportLayer() TransportLayer { 575 for p.transport == nil && p.next != nil { 576 p.decodeNextLayer() 577 } 578 return p.transport 579 } 580 func (p *lazyPacket) ApplicationLayer() ApplicationLayer { 581 for p.application == nil && p.next != nil { 582 p.decodeNextLayer() 583 } 584 return p.application 585 } 586 func (p *lazyPacket) ErrorLayer() ErrorLayer { 587 for p.failure == nil && p.next != nil { 588 p.decodeNextLayer() 589 } 590 return p.failure 591 } 592 func (p *lazyPacket) Layers() []Layer { 593 for p.next != nil { 594 p.decodeNextLayer() 595 } 596 return p.layers 597 } 598 func (p *lazyPacket) Layer(t LayerType) Layer { 599 for _, l := range p.layers { 600 if l.LayerType() == t { 601 return l 602 } 603 } 604 numLayers := len(p.layers) 605 for p.next != nil { 606 p.decodeNextLayer() 607 for _, l := range p.layers[numLayers:] { 608 if l.LayerType() == t { 609 return l 610 } 611 } 612 numLayers = len(p.layers) 613 } 614 return nil 615 } 616 func (p *lazyPacket) LayerClass(lc LayerClass) Layer { 617 for _, l := range p.layers { 618 if lc.Contains(l.LayerType()) { 619 return l 620 } 621 } 622 numLayers := len(p.layers) 623 for p.next != nil { 624 p.decodeNextLayer() 625 for _, l := range p.layers[numLayers:] { 626 if lc.Contains(l.LayerType()) { 627 return l 628 } 629 } 630 numLayers = len(p.layers) 631 } 632 return nil 633 } 634 func (p *lazyPacket) String() string { p.Layers(); return p.packetString() } 635 func (p *lazyPacket) Dump() string { p.Layers(); return p.packetDump() } 636 637 // DecodeOptions tells gopacket how to decode a packet. 638 type DecodeOptions struct { 639 // Lazy decoding decodes the minimum number of layers needed to return data 640 // for a packet at each function call. Be careful using this with concurrent 641 // packet processors, as each call to packet.* could mutate the packet, and 642 // two concurrent function calls could interact poorly. 643 Lazy bool 644 // NoCopy decoding doesn't copy its input buffer into storage that's owned by 645 // the packet. If you can guarantee that the bytes underlying the slice 646 // passed into NewPacket aren't going to be modified, this can be faster. If 647 // there's any chance that those bytes WILL be changed, this will invalidate 648 // your packets. 649 NoCopy bool 650 // Pool decoding only applies if NoCopy is false. 651 // Instead of always allocating new memory it takes the memory from a pool. 652 // NewPacket then will return a PooledPacket instead of a Packet. 653 // As soon as you're done with the PooledPacket you should call PooledPacket.Dispose() to return it to the pool. 654 Pool bool 655 // SkipDecodeRecovery skips over panic recovery during packet decoding. 656 // Normally, when packets decode, if a panic occurs, that panic is captured 657 // by a recover(), and a DecodeFailure layer is added to the packet detailing 658 // the issue. If this flag is set, panics are instead allowed to continue up 659 // the stack. 660 SkipDecodeRecovery bool 661 // DecodeStreamsAsDatagrams enables routing of application-level layers in the TCP 662 // decoder. If true, we should try to decode layers after TCP in single packets. 663 // This is disabled by default because the reassembly package drives the decoding 664 // of TCP payload data after reassembly. 665 DecodeStreamsAsDatagrams bool 666 } 667 668 // Default decoding provides the safest (but slowest) method for decoding 669 // packets. It eagerly processes all layers (so it's concurrency-safe) and it 670 // copies its input buffer upon creation of the packet (so the packet remains 671 // valid if the underlying slice is modified. Both of these take time, 672 // though, so beware. If you can guarantee that the packet will only be used 673 // by one goroutine at a time, set Lazy decoding. If you can guarantee that 674 // the underlying slice won't change, set NoCopy decoding. 675 var Default = DecodeOptions{} 676 677 // Lazy is a DecodeOptions with just Lazy set. 678 var Lazy = DecodeOptions{Lazy: true} 679 680 // NoCopy is a DecodeOptions with just NoCopy set. 681 var NoCopy = DecodeOptions{NoCopy: true} 682 683 // DecodeStreamsAsDatagrams is a DecodeOptions with just DecodeStreamsAsDatagrams set. 684 var DecodeStreamsAsDatagrams = DecodeOptions{DecodeStreamsAsDatagrams: true} 685 686 // NewPacket creates a new Packet object from a set of bytes. The 687 // firstLayerDecoder tells it how to interpret the first layer from the bytes, 688 // future layers will be generated from that first layer automatically. 689 func NewPacket(data []byte, firstLayerDecoder Decoder, options DecodeOptions) (p Packet) { 690 if !options.NoCopy { 691 var ( 692 poolMemory *[]byte 693 dataCopy []byte 694 ) 695 if options.Pool && len(data) <= maximumMTU { 696 poolMemory = poolPackedPool.Get().(*[]byte) 697 dataCopy = (*poolMemory)[:len(data)] 698 copy(dataCopy, data) 699 data = dataCopy 700 defer func() { 701 p = &pooledPacket{Packet: p, origData: poolMemory} 702 }() 703 } else { 704 dataCopy = make([]byte, len(data)) 705 copy(dataCopy, data) 706 data = dataCopy 707 } 708 } 709 if options.Lazy { 710 lp := &lazyPacket{ 711 packet: packet{data: data, decodeOptions: options}, 712 next: firstLayerDecoder, 713 } 714 lp.layers = lp.initialLayers[:0] 715 // Crazy craziness: 716 // If the following return statemet is REMOVED, and Lazy is FALSE, then 717 // eager packet processing becomes 17% FASTER. No, there is no logical 718 // explanation for this. However, it's such a hacky micro-optimization that 719 // we really can't rely on it. It appears to have to do with the size the 720 // compiler guesses for this function's stack space, since one symptom is 721 // that with the return statement in place, we more than double calls to 722 // runtime.morestack/runtime.lessstack. We'll hope the compiler gets better 723 // over time and we get this optimization for free. Until then, we'll have 724 // to live with slower packet processing. 725 return lp 726 } 727 ep := &eagerPacket{ 728 packet: packet{data: data, decodeOptions: options}, 729 } 730 ep.layers = ep.initialLayers[:0] 731 ep.initialDecode(firstLayerDecoder) 732 return ep 733 } 734 735 // PacketDataSource is an interface for some source of packet data. Users may 736 // create their own implementations, or use the existing implementations in 737 // gopacket/pcap (libpcap, allows reading from live interfaces or from 738 // pcap files) or gopacket/pfring (PF_RING, allows reading from live 739 // interfaces). 740 type PacketDataSource interface { 741 // ReadPacketData returns the next packet available from this data source. 742 // It returns: 743 // data: The bytes of an individual packet. 744 // ci: Metadata about the capture 745 // err: An error encountered while reading packet data. If err != nil, 746 // then data/ci will be ignored. 747 ReadPacketData() (data []byte, ci CaptureInfo, err error) 748 } 749 750 // ConcatFinitePacketDataSources returns a PacketDataSource that wraps a set 751 // of internal PacketDataSources, each of which will stop with io.EOF after 752 // reading a finite number of packets. The returned PacketDataSource will 753 // return all packets from the first finite source, followed by all packets from 754 // the second, etc. Once all finite sources have returned io.EOF, the returned 755 // source will as well. 756 func ConcatFinitePacketDataSources(pds ...PacketDataSource) PacketDataSource { 757 c := concat(pds) 758 return &c 759 } 760 761 type concat []PacketDataSource 762 763 func (c *concat) ReadPacketData() (data []byte, ci CaptureInfo, err error) { 764 for len(*c) > 0 { 765 data, ci, err = (*c)[0].ReadPacketData() 766 if errors.Is(err, io.EOF) { 767 *c = (*c)[1:] 768 continue 769 } 770 return 771 } 772 return nil, CaptureInfo{}, io.EOF 773 } 774 775 // ZeroCopyPacketDataSource is an interface to pull packet data from sources 776 // that allow data to be returned without copying to a user-controlled buffer. 777 // It's very similar to PacketDataSource, except that the caller must be more 778 // careful in how the returned buffer is handled. 779 type ZeroCopyPacketDataSource interface { 780 // ZeroCopyReadPacketData returns the next packet available from this data source. 781 // It returns: 782 // data: The bytes of an individual packet. Unlike with 783 // PacketDataSource's ReadPacketData, the slice returned here points 784 // to a buffer owned by the data source. In particular, the bytes in 785 // this buffer may be changed by future calls to 786 // ZeroCopyReadPacketData. Do not use the returned buffer after 787 // subsequent ZeroCopyReadPacketData calls. 788 // ci: Metadata about the capture 789 // err: An error encountered while reading packet data. If err != nil, 790 // then data/ci will be ignored. 791 ZeroCopyReadPacketData() (data []byte, ci CaptureInfo, err error) 792 } 793 794 type PacketSourceOption interface { 795 apply(ps *PacketSource) 796 } 797 798 type packetSourceOptionFunc func(ps *PacketSource) 799 800 func (f packetSourceOptionFunc) apply(ps *PacketSource) { 801 f(ps) 802 } 803 804 func WithLazy(lazy bool) packetSourceOptionFunc { 805 return func(ps *PacketSource) { 806 ps.Lazy = lazy 807 } 808 } 809 810 func WithNoCopy(noCopy bool) packetSourceOptionFunc { 811 return func(ps *PacketSource) { 812 ps.NoCopy = noCopy 813 } 814 } 815 816 func WithPool(pool bool) packetSourceOptionFunc { 817 return func(ps *PacketSource) { 818 ps.Pool = pool 819 } 820 } 821 822 func WithSkipDecodeRecovery(skipDecodeRecovery bool) packetSourceOptionFunc { 823 return func(ps *PacketSource) { 824 ps.SkipDecodeRecovery = skipDecodeRecovery 825 } 826 } 827 828 func WithDecodeStreamsAsDatagrams(decodeStreamsAsDatagrams bool) packetSourceOptionFunc { 829 return func(ps *PacketSource) { 830 ps.DecodeStreamsAsDatagrams = decodeStreamsAsDatagrams 831 } 832 } 833 834 // PacketSource reads in packets from a PacketDataSource, decodes them, and 835 // returns them. 836 // 837 // There are currently two different methods for reading packets in through 838 // a PacketSource: 839 // 840 // # Reading With Packets Function 841 // 842 // This method is the most convenient and easiest to code, but lacks 843 // flexibility. Packets returns a 'chan Packet', then asynchronously writes 844 // packets into that channel. Packets uses a blocking channel, and closes 845 // it if an io.EOF is returned by the underlying PacketDataSource. All other 846 // PacketDataSource errors are ignored and discarded. 847 // 848 // for packet := range packetSource.Packets() { 849 // ... 850 // } 851 // 852 // # Reading With NextPacket Function 853 // 854 // This method is the most flexible, and exposes errors that may be 855 // encountered by the underlying PacketDataSource. It's also the fastest 856 // in a tight loop, since it doesn't have the overhead of a channel 857 // read/write. However, it requires the user to handle errors, most 858 // importantly the io.EOF error in cases where packets are being read from 859 // a file. 860 // 861 // for { 862 // packet, err := packetSource.NextPacket() 863 // if err == io.EOF { 864 // break 865 // } else if err != nil { 866 // log.Println("Error:", err) 867 // continue 868 // } 869 // handlePacket(packet) // Do something with each packet. 870 // } 871 type PacketSource struct { 872 zeroCopy bool 873 source func() (data []byte, ci CaptureInfo, err error) 874 decoder Decoder 875 // DecodeOptions is the set of options to use for decoding each piece 876 // of packet data. This can/should be changed by the user to reflect the 877 // way packets should be decoded. 878 DecodeOptions 879 c chan Packet 880 } 881 882 // NewZeroCopyPacketSource creates a zero copy packet data source. 883 func NewZeroCopyPacketSource(source ZeroCopyPacketDataSource, decoder Decoder, opts ...PacketSourceOption) *PacketSource { 884 ps := &PacketSource{ 885 source: source.ZeroCopyReadPacketData, 886 decoder: decoder, 887 } 888 889 for idx := range opts { 890 opts[idx].apply(ps) 891 } 892 893 return ps 894 } 895 896 // NewPacketSource creates a packet data source. 897 func NewPacketSource(source PacketDataSource, decoder Decoder, opts ...PacketSourceOption) *PacketSource { 898 ps := &PacketSource{ 899 source: source.ReadPacketData, 900 decoder: decoder, 901 } 902 903 for idx := range opts { 904 opts[idx].apply(ps) 905 } 906 907 return ps 908 } 909 910 // NextPacket returns the next decoded packet from the PacketSource. On error, 911 // it returns a nil packet and a non-nil error. 912 func (p *PacketSource) NextPacket() (Packet, error) { 913 data, ci, err := p.source() 914 if err != nil { 915 return nil, err 916 } 917 packet := NewPacket(data, p.decoder, p.DecodeOptions) 918 m := packet.Metadata() 919 m.CaptureInfo = ci 920 m.Truncated = m.Truncated || ci.CaptureLength < ci.Length 921 return packet, nil 922 } 923 924 // packetsToChannel reads in all packets from the packet source and sends them 925 // to the given channel. This routine terminates when a non-temporary error 926 // is returned by NextPacket(). 927 func (p *PacketSource) packetsToChannel(ctx context.Context) { 928 defer close(p.c) 929 for ctx.Err() == nil { 930 packet, err := p.NextPacket() 931 if err == nil { 932 select { 933 case p.c <- packet: 934 continue 935 case <-ctx.Done(): 936 return 937 } 938 } 939 940 // if timeout error sleep briefly and retry 941 var netErr net.Error 942 if ok := errors.As(err, &netErr); ok && netErr.Timeout() { 943 time.Sleep(time.Millisecond * time.Duration(5)) 944 continue 945 } 946 947 // Immediately break for known unrecoverable errors 948 if errors.Is(err, io.EOF) || errors.Is(err, io.ErrUnexpectedEOF) || 949 errors.Is(err, io.ErrNoProgress) || errors.Is(err, io.ErrClosedPipe) || errors.Is(err, io.ErrShortBuffer) || 950 errors.Is(err, syscall.EBADF) || 951 strings.Contains(err.Error(), "use of closed file") { 952 break 953 } 954 955 // Sleep briefly and try again 956 time.Sleep(time.Millisecond * time.Duration(5)) 957 } 958 } 959 960 // Packets returns a channel of packets, allowing easy iterating over 961 // packets. Packets will be asynchronously read in from the underlying 962 // PacketDataSource and written to the returned channel. If the underlying 963 // PacketDataSource returns an io.EOF error, the channel will be closed. 964 // If any other error is encountered, it is ignored. 965 // 966 // for packet := range packetSource.Packets() { 967 // handlePacket(packet) // Do something with each packet 968 // } 969 // 970 // If called more than once, returns the same channel. 971 func (p *PacketSource) Packets() chan Packet { 972 return p.PacketsCtx(context.Background()) 973 } 974 975 // PacketsCtx returns a channel of packets, allowing easy iterating over 976 // packets. Packets will be asynchronously read in from the underlying 977 // PacketDataSource and written to the returned channel. If the underlying 978 // PacketDataSource returns an io.EOF error, the channel will be closed. 979 // If any other error is encountered, it is ignored. 980 // The background Go routine will be canceled as soon as the given context 981 // returns an error either because it got canceled or it has reached its deadline. 982 // 983 // for packet := range packetSource.PacketsCtx(context.Background()) { 984 // handlePacket(packet) // Do something with each packet. 985 // } 986 // 987 // If called more than once, returns the same channel. 988 func (p *PacketSource) PacketsCtx(ctx context.Context) chan Packet { 989 if p.DecodeOptions.NoCopy && p.zeroCopy { 990 panic("PacketSource uses a zero copy datasource and NoCopy decoder option activated - Packets() uses a buffered channel hence packets are most likely overwritten") 991 } 992 993 const defaultPacketChannelSize = 1000 994 if p.c == nil { 995 p.c = make(chan Packet, defaultPacketChannelSize) 996 go p.packetsToChannel(ctx) 997 } 998 return p.c 999 }