doc.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 layers provides decoding layers for many common protocols.
  8. The layers package contains decode implementations for a number of different
  9. types of packet layers. Users of gopacket will almost always want to also use
  10. layers to actually decode packet data into useful pieces. To see the set of
  11. protocols that gopacket/layers is currently able to decode,
  12. look at the set of LayerTypes defined in the Variables sections. The
  13. layers package also defines endpoints for many of the common packet layers
  14. that have source/destination addresses associated with them, for example IPv4/6
  15. (IPs) and TCP/UDP (ports).
  16. Finally, layers contains a number of useful enumerations (IPProtocol,
  17. EthernetType, LinkType, PPPType, etc...). Many of these implement the
  18. gopacket.Decoder interface, so they can be passed into gopacket as decoders.
  19. Most common protocol layers are named using acronyms or other industry-common
  20. names (IPv4, TCP, PPP). Some of the less common ones have their names expanded
  21. (CiscoDiscoveryProtocol).
  22. For certain protocols, sub-parts of the protocol are split out into their own
  23. layers (SCTP, for example). This is done mostly in cases where portions of the
  24. protocol may fulfill the capabilities of interesting layers (SCTPData implements
  25. ApplicationLayer, while base SCTP implements TransportLayer), or possibly
  26. because splitting a protocol into a few layers makes decoding easier.
  27. This package is meant to be used with its parent,
  28. http://github.com/google/gopacket.
  29. Port Types
  30. Instead of using raw uint16 or uint8 values for ports, we use a different port
  31. type for every protocol, for example TCPPort and UDPPort. This allows us to
  32. override string behavior for each port, which we do by setting up port name
  33. maps (TCPPortNames, UDPPortNames, etc...). Well-known ports are annotated with
  34. their protocol names, and their String function displays these names:
  35. p := TCPPort(80)
  36. fmt.Printf("Number: %d String: %v", p, p)
  37. // Prints: "Number: 80 String: 80(http)"
  38. Modifying Decode Behavior
  39. layers links together decoding through its enumerations. For example, after
  40. decoding layer type Ethernet, it uses Ethernet.EthernetType as its next decoder.
  41. All enumerations that act as decoders, like EthernetType, can be modified by
  42. users depending on their preferences. For example, if you have a spiffy new
  43. IPv4 decoder that works way better than the one built into layers, you can do
  44. this:
  45. var mySpiffyIPv4Decoder gopacket.Decoder = ...
  46. layers.EthernetTypeMetadata[EthernetTypeIPv4].DecodeWith = mySpiffyIPv4Decoder
  47. This will make all future ethernet packets use your new decoder to decode IPv4
  48. packets, instead of the built-in decoder used by gopacket.
  49. */
  50. package layers