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