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