/src/PcapPlusPlus/Packet++/header/Sll2Layer.h
Line | Count | Source (jump to first uncovered line) |
1 | | #pragma once |
2 | | |
3 | | #include "MacAddress.h" |
4 | | #include "Layer.h" |
5 | | |
6 | | /// @file |
7 | | |
8 | | /// @namespace pcpp |
9 | | /// @brief The main namespace for the PcapPlusPlus lib |
10 | | namespace pcpp |
11 | | { |
12 | | /// @struct sll2_header |
13 | | /// Represents SLL2 header |
14 | | #pragma pack(push, 1) |
15 | | struct sll2_header |
16 | | { |
17 | | /// Contains an Ethernet protocol type of the next layer |
18 | | uint16_t protocol_type; |
19 | | /// The "Reserved (MBZ)" field is reserved, and must be set to zero |
20 | | uint16_t reserved; |
21 | | /// The interface index field is a signed integer in network byte |
22 | | /// order and contains the 1-based index of the interface on which the packet was observed |
23 | | uint32_t interface_index; |
24 | | /// Contains a Linux ARPHRD_ value for the link-layer device type |
25 | | uint16_t ARPHRD_type; |
26 | | /// Specifies whether packet was: specifically sent to us by somebody else (value=0); |
27 | | /// broadcast by somebody else (value=1); multicast, but not broadcast, by somebody else (value=2); |
28 | | /// sent to somebody else by somebody else (value=3); sent by us (value=4) |
29 | | uint8_t packet_type; |
30 | | /// Contains the length of the link-layer address of the sender of the packet. That length could be zero |
31 | | uint8_t link_layer_addr_len; |
32 | | /// Contains the link-layer address of the sender of the packet; the number of bytes of that field that are |
33 | | /// meaningful is specified by the link-layer address length field |
34 | | uint8_t link_layer_addr[8]; |
35 | | }; |
36 | | #pragma pack(pop) |
37 | | static_assert(sizeof(sll2_header) == 20, "sll2_header size is not 20 bytes"); |
38 | | |
39 | | /// @class Sll2Layer |
40 | | /// Represents an SLL2 (Linux cooked capture) protocol layer |
41 | | class Sll2Layer : public Layer |
42 | | { |
43 | | public: |
44 | | /// A constructor that creates the layer from an existing packet raw data |
45 | | /// @param[in] data A pointer to the raw data (will be casted to ether_header) |
46 | | /// @param[in] dataLen Size of the data in bytes |
47 | | /// @param[in] packet A pointer to the Packet instance where layer will be stored in |
48 | | Sll2Layer(uint8_t* data, size_t dataLen, Packet* packet) : Layer(data, dataLen, nullptr, packet, SLL2) |
49 | 0 | {} |
50 | | |
51 | | /// A constructor that creates a new SLL2 header and allocates the data |
52 | | /// @param[in] interfaceIndex The interface index |
53 | | /// @param[in] ARPHRDType The ARPHRD type |
54 | | /// @param[in] packetType The packet type |
55 | | Sll2Layer(uint32_t interfaceIndex, uint16_t ARPHRDType, uint8_t packetType); |
56 | | |
57 | | ~Sll2Layer() override = default; |
58 | | |
59 | | /// Get a pointer to the Sll header. Notice this points directly to the data, so every change will change the |
60 | | /// actual packet data |
61 | | /// @return A pointer to the sll2_header |
62 | | sll2_header* getSll2Header() const |
63 | 0 | { |
64 | 0 | return reinterpret_cast<sll2_header*>(m_Data); |
65 | 0 | } |
66 | | |
67 | | /// A static method that validates the input data |
68 | | /// @param[in] data The pointer to the beginning of a byte stream of an IEEE 802.3 Eth packet |
69 | | /// @param[in] dataLen The length of the byte stream |
70 | | /// @return True if the data is valid and can represent an IEEE 802.3 Eth packet |
71 | | static bool isDataValid(const uint8_t* data, size_t dataLen); |
72 | | |
73 | | /// Get a protocol type of this layer |
74 | | /// @return protocol type |
75 | | uint16_t getProtocolType() const; |
76 | | |
77 | | /// Set protocol type of this layer |
78 | | /// @param[in] protocolType type to set |
79 | | void setProtocolType(uint16_t protocolType); |
80 | | |
81 | | /// Get interface index of this layer |
82 | | /// @return interface index |
83 | | uint32_t getInterfaceIndex() const; |
84 | | |
85 | | /// Set interface index of this layer |
86 | | /// @param[in] interfaceIndex interface index to set |
87 | | void setInterfaceIndex(uint32_t interfaceIndex); |
88 | | |
89 | | /// Get arphrd type of this layer |
90 | | /// @return arphrd type |
91 | | uint16_t getArphrdType() const; |
92 | | |
93 | | /// Set arphrd type of this layer |
94 | | /// @param[in] arphrdType arphrd type to set |
95 | | void setArphrdType(uint16_t arphrdType); |
96 | | |
97 | | /// Get packet type of this layer |
98 | | /// @return packet type |
99 | | uint8_t getPacketType() const; |
100 | | |
101 | | /// Set packet type of this layer |
102 | | /// @param[in] packetType packet type to set |
103 | | void setPacketType(uint8_t packetType); |
104 | | |
105 | | /// Get link layer address length |
106 | | /// @return link layer address length |
107 | | uint8_t getLinkLayerAddrLen() const; |
108 | | |
109 | | /// Get link layer address data pointer |
110 | | /// @return link layer address data pointer |
111 | | const uint8_t* getLinkLayerAddr() const; |
112 | | |
113 | | /// A setter for the link layer address field |
114 | | /// @param[in] addr The address to set. Memory will be copied to packet |
115 | | /// @param[in] addrLength Address length, must be lower or equal to 8 (which is max length for SLL2 address) |
116 | | /// @return True if address was set successfully, or false of addrLength is out of bounds (0 or larger than 8) |
117 | | bool setLinkLayerAddr(const uint8_t* addr, size_t addrLength); |
118 | | |
119 | | /// Get a MAC address in the link layer address field |
120 | | /// @return return macAddress pointer was set successfully, null pointer if d MAC address isn't valid or if set |
121 | | /// failed |
122 | | MacAddress getLinkLayerAsMacAddress(); |
123 | | |
124 | | /// Set a MAC address in the link layer address field |
125 | | /// @param[in] macAddr MAC address to set |
126 | | /// @return True if address was set successfully, false if MAC address isn't valid or if set failed |
127 | | bool setMacAddressAsLinkLayer(const MacAddress& macAddr); |
128 | | |
129 | | // implement abstract methods |
130 | | |
131 | | /// Currently identifies the following next layers: IPv4Layer, IPv6Layer, ArpLayer, VlanLayer, |
132 | | /// PPPoESessionLayer, PPPoEDiscoveryLayer, MplsLayer. Otherwise sets PayloadLayer |
133 | | void parseNextLayer() override; |
134 | | |
135 | | /// Calculate the next protocol type for known protocols: IPv4, IPv6, ARP, VLAN |
136 | | void computeCalculateFields() override; |
137 | | |
138 | | /// @return Size of sll2_header |
139 | | size_t getHeaderLen() const override |
140 | 0 | { |
141 | 0 | return sizeof(sll2_header); |
142 | 0 | } |
143 | | |
144 | | std::string toString() const override; |
145 | | |
146 | | OsiModelLayer getOsiModelLayer() const override |
147 | 0 | { |
148 | 0 | return OsiModelDataLinkLayer; |
149 | 0 | } |
150 | | }; |
151 | | |
152 | | } // namespace pcpp |