Coverage Report

Created: 2023-01-17 06:15

/src/PcapPlusPlus/Packet++/header/EthLayer.h
Line
Count
Source (jump to first uncovered line)
1
#ifndef PACKETPP_ETH_LAYER
2
#define PACKETPP_ETH_LAYER
3
4
#include "Layer.h"
5
#include "MacAddress.h"
6
7
/// @file
8
9
/**
10
 * \namespace pcpp
11
 * \brief The main namespace for the PcapPlusPlus lib
12
 */
13
namespace pcpp
14
{
15
16
  /**
17
   * @struct ether_header
18
   * Represents an Ethernet II header
19
   */
20
#pragma pack(push, 1)
21
  struct ether_header
22
  {
23
    /** Destination MAC */
24
    uint8_t dstMac[6];
25
    /** Source MAC */
26
    uint8_t srcMac[6];
27
    /** EtherType */
28
    uint16_t etherType;
29
  };
30
#pragma pack(pop)
31
32
  /* Ethernet protocol ID's */
33
34
  /** IP */
35
9.13k
#define PCPP_ETHERTYPE_IP            0x0800
36
  /** Address resolution */
37
1.19k
#define PCPP_ETHERTYPE_ARP           0x0806
38
  /** Transparent Ethernet Bridging */
39
2.00k
#define PCPP_ETHERTYPE_ETHBRIDGE     0x6558
40
  /** Reverse ARP */
41
#define PCPP_ETHERTYPE_REVARP        0x8035
42
  /** AppleTalk protocol */
43
#define PCPP_ETHERTYPE_AT            0x809B
44
  /** AppleTalk ARP */
45
#define PCPP_ETHERTYPE_AARP          0x80F3
46
  /** IEEE 802.1Q VLAN tagging */
47
6.64k
#define PCPP_ETHERTYPE_VLAN          0x8100
48
  /** IPX */
49
#define PCPP_ETHERTYPE_IPX           0x8137
50
  /** IP protocol version 6 */
51
2.13k
#define PCPP_ETHERTYPE_IPV6          0x86dd
52
  /** used to test interfaces */
53
#define PCPP_ETHERTYPE_LOOPBACK      0x9000
54
  /** PPPoE discovery */
55
1.29k
#define PCPP_ETHERTYPE_PPPOED        0x8863
56
  /** PPPoE session */
57
2.84k
#define PCPP_ETHERTYPE_PPPOES        0x8864
58
  /** MPLS */
59
3.63k
#define PCPP_ETHERTYPE_MPLS          0x8847
60
  /** Point-to-point protocol (PPP) */
61
2.94k
#define PCPP_ETHERTYPE_PPP           0x880B
62
  /** RDMA over Converged Ethernet (RoCEv1) */
63
#define PCPP_ETHERTYPE_ROCEV1        0x8915
64
  /** IEEE 802.1ad Provider Bridge, Q-in-Q */
65
7.79k
#define PCPP_ETHERTYPE_IEEE_802_1AD  0x88A8
66
  /** Wake on LAN */
67
724
#define PCPP_ETHERTYPE_WAKE_ON_LAN   0x0842
68
69
70
  /**
71
   * @class EthLayer
72
   * Represents an Ethernet II protocol layer
73
   */
74
  class EthLayer : public Layer
75
  {
76
  public:
77
    /**
78
     * A constructor that creates the layer from an existing packet raw data
79
     * @param[in] data A pointer to the raw data (will be casted to ether_header)
80
     * @param[in] dataLen Size of the data in bytes
81
     * @param[in] packet A pointer to the Packet instance where layer will be stored in
82
     */
83
14.4k
    EthLayer(uint8_t* data, size_t dataLen, Packet* packet) : Layer(data, dataLen, NULL, packet) { m_Protocol = Ethernet; }
84
85
    /**
86
     * A constructor that creates the layer from an existing packet raw data
87
     * @param[in] data A pointer to the raw data (will be casted to ether_header)
88
     * @param[in] dataLen Size of the data in bytes
89
     * @param[in] prevLayer A pointer to the previous layer
90
     * @param[in] packet A pointer to the Packet instance where layer will be stored in
91
     */
92
1.52k
    EthLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet) : Layer(data, dataLen, prevLayer, packet) { m_Protocol = Ethernet; }
93
94
    /**
95
     * A constructor that creates a new Ethernet header and allocates the data
96
     * @param[in] sourceMac The source MAC address
97
     * @param[in] destMac The destination MAC address
98
     * @param[in] etherType The EtherType to be used. It's an optional parameter, a value of 0 will be set if not provided
99
     */
100
    EthLayer(const MacAddress& sourceMac, const MacAddress& destMac, uint16_t etherType = 0);
101
102
0
    ~EthLayer() {}
103
104
    /**
105
     * Get a pointer to the Ethernet header. Notice this points directly to the data, so every change will change the actual packet data
106
     * @return A pointer to the ether_header
107
     */
108
15.7k
    inline ether_header* getEthHeader() const { return (ether_header*)m_Data; }
109
110
    /**
111
     * Get the source MAC address
112
     * @return The source MAC address
113
     */
114
0
    inline MacAddress getSourceMac() const { return MacAddress(getEthHeader()->srcMac); }
115
116
    /**
117
     * Set source MAC address
118
     * @param sourceMac Source MAC to set
119
     */
120
0
    inline void setSourceMac(const MacAddress& sourceMac) { sourceMac.copyTo(getEthHeader()->srcMac); }
121
122
    /**
123
     * Get the destination MAC address
124
     * @return The destination MAC address
125
     */
126
0
    inline MacAddress getDestMac() const { return MacAddress(getEthHeader()->dstMac); }
127
128
    /**
129
     * Set destination MAC address
130
     * @param destMac Destination MAC to set
131
     */
132
0
    inline void setDestMac(const MacAddress& destMac) { destMac.copyTo(getEthHeader()->dstMac); }
133
134
    // implement abstract methods
135
136
    /**
137
     * Currently identifies the following next layers: IPv4Layer, IPv6Layer, ArpLayer, VlanLayer, PPPoESessionLayer, PPPoEDiscoveryLayer,
138
     * MplsLayer.
139
     * Otherwise sets PayloadLayer
140
     */
141
    void parseNextLayer();
142
143
    /**
144
     * @return Size of ether_header
145
     */
146
0
    size_t getHeaderLen() const { return sizeof(ether_header); }
147
148
    /**
149
     * Calculate ether_header#etherType for known protocols: IPv4, IPv6, ARP, VLAN
150
     */
151
    void computeCalculateFields();
152
153
    std::string toString() const;
154
155
16.0k
    OsiModelLayer getOsiModelLayer() const { return OsiModelDataLinkLayer; }
156
157
    /**
158
     * A static method that validates the input data
159
     * @param[in] data The pointer to the beginning of a byte stream of an Ethernet II packet
160
     * @param[in] dataLen The length of the byte stream
161
     * @return True if the data is valid and can represent an Ethernet II packet
162
     */
163
    static bool isDataValid(const uint8_t* data, size_t dataLen);
164
  };
165
166
} // namespace pcpp
167
168
#endif /* PACKETPP_ETH_LAYER */