/src/PcapPlusPlus/Packet++/header/DhcpV6Layer.h
Line | Count | Source |
1 | | #pragma once |
2 | | |
3 | | #include "Layer.h" |
4 | | #include "TLVData.h" |
5 | | #include "IpAddress.h" |
6 | | |
7 | | /// @file |
8 | | |
9 | | /// @namespace pcpp |
10 | | /// @brief The main namespace for the PcapPlusPlus lib |
11 | | namespace pcpp |
12 | | { |
13 | | /// DHCPv6 message types |
14 | | enum DhcpV6MessageType |
15 | | { |
16 | | /// Unknown message type |
17 | | DHCPV6_UNKNOWN_MSG_TYPE = 0, |
18 | | /// Solicit message type (Client to Server) |
19 | | DHCPV6_SOLICIT = 1, |
20 | | /// Advertise message type (Server to Client) |
21 | | DHCPV6_ADVERTISE = 2, |
22 | | /// Request message type (Client to Server) |
23 | | DHCPV6_REQUEST = 3, |
24 | | /// Confirm message type (Client to Server) |
25 | | DHCPV6_CONFIRM = 4, |
26 | | /// Renew message type (Client to Server) |
27 | | DHCPV6_RENEW = 5, |
28 | | /// Rebind message type (Client to Server) |
29 | | DHCPV6_REBIND = 6, |
30 | | /// Reply message type (Server to Client) |
31 | | DHCPV6_REPLY = 7, |
32 | | /// Release message type (Client to Server) |
33 | | DHCPV6_RELEASE = 8, |
34 | | /// Decline message type (Client to Server) |
35 | | DHCPV6_DECLINE = 9, |
36 | | /// Reconfigure message type (Server to Client) |
37 | | DHCPV6_RECONFIGURE = 10, |
38 | | /// Information-Request message type (Client to Server) |
39 | | DHCPV6_INFORMATION_REQUEST = 11, |
40 | | /// Relay-Forward message type (Relay agent to Server) |
41 | | DHCPV6_RELAY_FORWARD = 12, |
42 | | /// Relay-Reply message type (Server to Relay agent) |
43 | | DHCPV6_RELAY_REPLY = 13 |
44 | | }; |
45 | | |
46 | | /// DHCPv6 option types. |
47 | | /// Resources for more information: |
48 | | /// - https://onlinelibrary.wiley.com/doi/pdf/10.1002/9781118073810.app2 |
49 | | /// - https://datatracker.ietf.org/doc/html/rfc5970 |
50 | | /// - https://datatracker.ietf.org/doc/html/rfc6607 |
51 | | /// - https://datatracker.ietf.org/doc/html/rfc8520 |
52 | | enum DhcpV6OptionType |
53 | | { |
54 | | /// Unknown option type |
55 | | DHCPV6_OPT_UNKNOWN = 0, |
56 | | /// Client Identifier (DUID of client) |
57 | | DHCPV6_OPT_CLIENTID = 1, |
58 | | /// Server Identifier (DUID of server) |
59 | | DHCPV6_OPT_SERVERID = 2, |
60 | | /// Identity Association for Non-temporary addresses |
61 | | DHCPV6_OPT_IA_NA = 3, |
62 | | /// Identity Association for Temporary addresses |
63 | | DHCPV6_OPT_IA_TA = 4, |
64 | | /// IA Address option |
65 | | DHCPV6_OPT_IAADDR = 5, |
66 | | /// Option Request Option |
67 | | DHCPV6_OPT_ORO = 6, |
68 | | /// Preference setting |
69 | | DHCPV6_OPT_PREFERENCE = 7, |
70 | | /// The amount of time since the client began the current DHCP transaction |
71 | | DHCPV6_OPT_ELAPSED_TIME = 8, |
72 | | /// The DHCP message being relayed by a relay agent |
73 | | DHCPV6_OPT_RELAY_MSG = 9, |
74 | | /// Authentication information |
75 | | DHCPV6_OPT_AUTH = 11, |
76 | | /// Server unicast |
77 | | DHCPV6_OPT_UNICAST = 12, |
78 | | /// Status code |
79 | | DHCPV6_OPT_STATUS_CODE = 13, |
80 | | /// Rapid commit |
81 | | DHCPV6_OPT_RAPID_COMMIT = 14, |
82 | | /// User class |
83 | | DHCPV6_OPT_USER_CLASS = 15, |
84 | | /// Vendor class |
85 | | DHCPV6_OPT_VENDOR_CLASS = 16, |
86 | | /// Vendor specific information |
87 | | DHCPV6_OPT_VENDOR_OPTS = 17, |
88 | | /// Interface ID |
89 | | DHCPV6_OPT_INTERFACE_ID = 18, |
90 | | /// Reconfigure Message |
91 | | DHCPV6_OPT_RECONF_MSG = 19, |
92 | | /// Reconfigure Accept |
93 | | DHCPV6_OPT_RECONF_ACCEPT = 20, |
94 | | /// SIP Servers Domain Name |
95 | | DHCPV6_OPT_SIP_SERVERS_D = 21, |
96 | | /// SIP Servers IPv6 Address List |
97 | | DHCPV6_OPT_SIP_SERVERS_A = 22, |
98 | | /// DNS Recursive Name Server |
99 | | DHCPV6_OPT_DNS_SERVERS = 23, |
100 | | /// Domain Search List |
101 | | DHCPV6_OPT_DOMAIN_LIST = 24, |
102 | | /// Identity Association for Prefix Delegation |
103 | | DHCPV6_OPT_IA_PD = 25, |
104 | | /// IA_PD Prefix |
105 | | DHCPV6_OPT_IAPREFIX = 26, |
106 | | /// Network Information Service (NIS) Servers |
107 | | DHCPV6_OPT_NIS_SERVERS = 27, |
108 | | /// Network Information Service v2 (NIS+) Servers |
109 | | DHCPV6_OPT_NISP_SERVERS = 28, |
110 | | /// Network Information Service (NIS) domain name |
111 | | DHCPV6_OPT_NIS_DOMAIN_NAME = 29, |
112 | | /// Network Information Service v2 (NIS+) domain name |
113 | | DHCPV6_OPT_NISP_DOMAIN_NAME = 30, |
114 | | /// Simple Network Time Protocol (SNTP) servers |
115 | | DHCPV6_OPT_SNTP_SERVERS = 31, |
116 | | /// Information Refresh |
117 | | DHCPV6_OPT_INFORMATION_REFRESH_TIME = 32, |
118 | | /// Broadcast and Multicast Service (BCMCS) Domain Name List |
119 | | DHCPV6_OPT_BCMCS_SERVER_D = 33, |
120 | | /// Broadcast and Multicast Service (BCMCS) IPv6 Address List |
121 | | DHCPV6_OPT_BCMCS_SERVER_A = 34, |
122 | | /// Geographical location in civic (e.g., postal) format |
123 | | DHCPV6_OPT_GEOCONF_CIVIC = 36, |
124 | | /// Relay Agent Remote ID |
125 | | DHCPV6_OPT_REMOTE_ID = 37, |
126 | | /// Relay Agent Subscriber ID |
127 | | DHCPV6_OPT_SUBSCRIBER_ID = 38, |
128 | | /// FQDN |
129 | | DHCPV6_OPT_CLIENT_FQDN = 39, |
130 | | /// One or more IPv6 addresses associated with PANA (Protocol for carrying Authentication for Network Access) |
131 | | /// Authentication Agents |
132 | | DHCPV6_OPT_PANA_AGENT = 40, |
133 | | /// Time zone to be used by the client in IEEE 1003.1 format |
134 | | DHCPV6_OPT_NEW_POSIX_TIMEZONE = 41, |
135 | | /// Time zone (TZ) database entry referred to by entry name |
136 | | DHCPV6_OPT_NEW_TZDB_TIMEZONE = 42, |
137 | | /// Relay Agent Echo Request |
138 | | DHCPV6_OPT_ERO = 43, |
139 | | /// Query option |
140 | | DHCPV6_OPT_LQ_QUERY = 44, |
141 | | /// Client Data |
142 | | DHCPV6_OPT_CLIENT_DATA = 45, |
143 | | /// Client Last Transaction Time |
144 | | DHCPV6_OPT_CLT_TIME = 46, |
145 | | /// Relay data |
146 | | DHCPV6_OPT_LQ_RELAY_DATA = 47, |
147 | | /// Client link |
148 | | DHCPV6_OPT_LQ_CLIENT_LINK = 48, |
149 | | /// Mobile IPv6 Home Network Information |
150 | | DHCPV6_OPT_MIP6_HNINF = 49, |
151 | | /// Mobile IPv6 Relay Agent |
152 | | DHCPV6_OPT_MIP6_RELAY = 50, |
153 | | /// Location to Service Translation (LoST) server domain name |
154 | | DHCPV6_OPT_V6_LOST = 51, |
155 | | /// Access Points (CAPWAP) Access Controller IPv6 addresses |
156 | | DHCPV6_OPT_CAPWAP_AC_V6 = 52, |
157 | | /// DHCPv6 Bulk LeaseQuery |
158 | | DHCPV6_OPT_RELAY_ID = 53, |
159 | | /// List of IPv6 addresses for servers providing particular types of IEEE 802.21 Mobility Service (MoS) |
160 | | DHCPV6_OPT_IPH6_ADDRESS_MOS = 54, |
161 | | /// List of FQDNs for servers providing particular types of IEEE 802.21 Mobility Service (MoS) |
162 | | DHCPV6_OPT_IPV6_FQDN_MOS = 55, |
163 | | /// Network Time Protocol (NTP) or Simple NTP (SNTP) Server Location |
164 | | DHCPV6_OPT_NTP_SERVER = 56, |
165 | | /// Boot File Uniform Resource Locator (URL) |
166 | | DHCPV6_OPT_BOOTFILE_URL = 59, |
167 | | /// Boot File Parameters |
168 | | DHCPV6_OPT_BOOTFILE_PARAM = 60, |
169 | | /// Client System Architecture Type |
170 | | DHCPV6_OPT_CLIENT_ARCH_TYPE = 61, |
171 | | /// Client Network Interface Identifier |
172 | | DHCPV6_OPT_NII = 62, |
173 | | /// ERP Local Domain Name |
174 | | DHCPV6_OPT_ERP_LOCAL_DOMAIN_NAME = 65, |
175 | | /// Relay supplied options |
176 | | DHCPV6_OPT_RELAY_SUPPLIED_OPTIONS = 66, |
177 | | /// Virtual Subnet Selection |
178 | | DHCPV6_OPT_VSS = 68, |
179 | | /// Client link layer |
180 | | DHCPV6_OPT_CLIENT_LINKLAYER_ADDR = 79, |
181 | | /// Manufacturer Usage Description |
182 | | DHCPV6_OPT_MUD_URL = 112 |
183 | | }; |
184 | | |
185 | | /// @class DhcpV6Option |
186 | | /// A wrapper class for DHCPv6 options. This class does not create or modify DHCP option records, but rather |
187 | | /// serves as a wrapper and provides useful methods for setting and retrieving data to/from them |
188 | | class DhcpV6Option : public TLVRecord<uint16_t, uint16_t> |
189 | | { |
190 | | public: |
191 | | /// A c'tor for this class that gets a pointer to the option raw data (byte array) |
192 | | /// @param[in] optionRawData A pointer to the option raw data |
193 | 0 | explicit DhcpV6Option(uint8_t* optionRawData) : TLVRecord(optionRawData) |
194 | 0 | {} |
195 | | |
196 | | /// A d'tor for this class, currently does nothing |
197 | | ~DhcpV6Option() override = default; |
198 | | |
199 | | /// @return The option type converted to ::DhcpV6OptionType enum |
200 | | DhcpV6OptionType getType() const; |
201 | | |
202 | | /// @return The raw option value (byte array) as a hex string |
203 | | std::string getValueAsHexString() const; |
204 | | |
205 | | // implement abstract methods |
206 | | |
207 | | size_t getTotalSize() const override; |
208 | | size_t getDataSize() const override; |
209 | | }; |
210 | | |
211 | | /// @class DhcpV6OptionBuilder |
212 | | /// A class for building DHCPv6 options. This builder receives the option parameters in its c'tor, |
213 | | /// builds the DHCPv6 option raw buffer and provides a build() method to get a DhcpV6Option object out of it |
214 | | class DhcpV6OptionBuilder : public TLVRecordBuilder |
215 | | { |
216 | | public: |
217 | | /// A c'tor for building DHCPv6 options from a string representing the hex stream of the raw byte value. |
218 | | /// The DhcpV6Option object can later be retrieved by calling build() |
219 | | /// @param[in] optionType DHCPv6 option type |
220 | | /// @param[in] optionValueAsHexStream The value as a hex stream string |
221 | | DhcpV6OptionBuilder(DhcpV6OptionType optionType, const std::string& optionValueAsHexStream) |
222 | | : TLVRecordBuilder(static_cast<uint16_t>(optionType), optionValueAsHexStream, true) |
223 | 0 | {} |
224 | | |
225 | | /// A c'tor for building DHCPv6 options from a byte array representing their value. The DhcpV6Option object can |
226 | | /// be later retrieved by calling build() |
227 | | /// @param[in] optionType DHCPv6 option type |
228 | | /// @param[in] optionValue A buffer containing the option value. This buffer is read-only and isn't modified in |
229 | | /// any way. |
230 | | /// @param[in] optionValueLen Option value length in bytes |
231 | | DhcpV6OptionBuilder(DhcpV6OptionType optionType, const uint8_t* optionValue, uint8_t optionValueLen) |
232 | | : TLVRecordBuilder(static_cast<uint16_t>(optionType), optionValue, optionValueLen) |
233 | 0 | {} |
234 | | |
235 | | /// Build the DhcpV6Option object out of the parameters defined in the c'tor |
236 | | /// @return The DhcpV6Option object |
237 | | DhcpV6Option build() const; |
238 | | }; |
239 | | |
240 | | /// @struct dhcpv6_header |
241 | | /// Represents the basic DHCPv6 protocol header |
242 | | struct dhcpv6_header |
243 | | { |
244 | | /// DHCPv6 message type |
245 | | uint8_t messageType; |
246 | | /// DHCPv6 transaction ID (first byte) |
247 | | uint8_t transactionId1; |
248 | | /// DHCPv6 transaction ID (second byte) |
249 | | uint8_t transactionId2; |
250 | | /// DHCPv6 transaction ID (last byte) |
251 | | uint8_t transactionId3; |
252 | | }; |
253 | | static_assert(sizeof(dhcpv6_header) == 4, "dhcpv6_header size is not 4 bytes"); |
254 | | |
255 | | #pragma pack(push, 1) |
256 | | /// @struct dhcpv6_relay_header |
257 | | /// Represents the header of DHCPv6 relay-forward and relay-reply messages (RFC 8415 section 9) |
258 | | struct dhcpv6_relay_header |
259 | | { |
260 | | /// DHCPv6 message type (relay-forward or relay-reply) |
261 | | uint8_t messageType; |
262 | | /// Number of relay agents that have already relayed this message |
263 | | uint8_t hopCount; |
264 | | /// Address that identifies the link on which the client is located |
265 | | uint8_t linkAddress[16]; |
266 | | /// Address of the client or relay agent from which the message was received |
267 | | uint8_t peerAddress[16]; |
268 | | }; |
269 | | #pragma pack(pop) |
270 | | static_assert(sizeof(dhcpv6_relay_header) == 34, "dhcpv6_relay_header size is not 34 bytes"); |
271 | | |
272 | | /// @class DhcpV6LayerBase |
273 | | /// A base class for the DHCPv6 (Dynamic Host Configuration Protocol version 6) layers. It contains the message |
274 | | /// type and the options, which client/server messages (DhcpV6Layer) and relay messages (DhcpV6RelayLayer) share |
275 | | class DhcpV6LayerBase : public Layer |
276 | | { |
277 | | public: |
278 | | /// @return The message type of this DHCPv6 message |
279 | | DhcpV6MessageType getMessageType() const; |
280 | | |
281 | | /// @return The string value of the message type of this DHCPv6 message |
282 | | std::string getMessageTypeAsString() const; |
283 | | |
284 | | /// @return The first DHCPv6 option in the packet. If there are no DHCPv6 options the returned value will |
285 | | /// contain a logical null (DhcpV6Option#isNull() == true) |
286 | | DhcpV6Option getFirstOptionData() const; |
287 | | |
288 | | /// Get the DHCPv6 option that comes after a given option. If the given option was the last one, the |
289 | | /// returned value will contain a logical null (DhcpV6Option#isNull() == true) |
290 | | /// @param[in] dhcpv6Option A given DHCPv6 option |
291 | | /// @return A DhcpV6Option object containing the option data that comes next, or logical null if the given |
292 | | /// DHCPv6 option: (1) was the last one; (2) contains a logical null or (3) doesn't belong to this packet |
293 | | DhcpV6Option getNextOptionData(DhcpV6Option dhcpv6Option) const; |
294 | | |
295 | | /// Get a DHCPv6 option by type |
296 | | /// @param[in] option DHCPv6 option type |
297 | | /// @return A DhcpV6OptionType object containing the first DHCP option data that matches this type, or logical |
298 | | /// null (DhcpV6Option#isNull() == true) if no such option found |
299 | | DhcpV6Option getOptionData(DhcpV6OptionType option) const; |
300 | | |
301 | | /// @return The number of DHCPv6 options in this layer |
302 | | size_t getOptionCount() const; |
303 | | |
304 | | /// Add a new DHCPv6 option at the end of the layer |
305 | | /// @param[in] optionBuilder A DhcpV6OptionBuilder object that contains the requested DHCPv6 option data to add |
306 | | /// @return A DhcpV6Option object containing the newly added DHCP option data or logical null |
307 | | /// (DhcpV6Option#isNull() == true) if addition failed |
308 | | DhcpV6Option addOption(const DhcpV6OptionBuilder& optionBuilder); |
309 | | |
310 | | /// Add a new DHCPv6 option after an existing one |
311 | | /// @param[in] optionBuilder A DhcpV6OptionBuilder object that contains the requested DHCPv6 option data to add |
312 | | /// @param[in] optionType The DHCPv6 option type which the newly added option will come after |
313 | | /// @return A DhcpV6Option object containing the newly added DHCPv6 option data or logical null |
314 | | /// (DhcpV6Option#isNull() == true) if addition failed |
315 | | DhcpV6Option addOptionAfter(const DhcpV6OptionBuilder& optionBuilder, DhcpV6OptionType optionType); |
316 | | |
317 | | /// Add a new DHCPv6 option before an existing one |
318 | | /// @param[in] optionBuilder A DhcpV6OptionBuilder object that contains the requested DHCPv6 option data to add |
319 | | /// @param[in] optionType The DHCPv6 option type which the newly added option will come before |
320 | | /// @return A DhcpV6Option object containing the newly added DHCPv6 option data or logical null |
321 | | /// (DhcpV6Option#isNull() == true) if addition failed |
322 | | DhcpV6Option addOptionBefore(const DhcpV6OptionBuilder& optionBuilder, DhcpV6OptionType optionType); |
323 | | |
324 | | /// Remove an existing DHCPv6 option from the layer |
325 | | /// @param[in] optionType The DHCPv6 option type to remove |
326 | | /// @return True if DHCPv6 option was successfully removed or false if type wasn't found or if removal failed |
327 | | bool removeOption(DhcpV6OptionType optionType); |
328 | | |
329 | | /// Remove all DHCPv6 options in this layer |
330 | | /// @return True if all DHCPv6 options were successfully removed or false if removal failed for some reason |
331 | | bool removeAllOptions(); |
332 | | |
333 | | /// A static method that checks whether a port is considered as a DHCPv6 port |
334 | | /// @param[in] port The port number to check |
335 | | /// @return True if this is a DHCPv6 port number, false otherwise |
336 | | static inline bool isDhcpV6Port(uint16_t port); |
337 | | |
338 | | // implement abstract methods |
339 | | |
340 | | /// Does nothing for this layer (DHCPv6 layers are always last) |
341 | | void parseNextLayer() override |
342 | 0 | {} |
343 | | |
344 | | /// @return The size of the message header + size of options |
345 | | size_t getHeaderLen() const override |
346 | 0 | { |
347 | 0 | return m_DataLen; |
348 | 0 | } |
349 | | |
350 | | /// Does nothing for this layer |
351 | | void computeCalculateFields() override |
352 | 0 | {} |
353 | | |
354 | | std::string toString() const override; |
355 | | |
356 | | OsiModelLayer getOsiModelLayer() const override |
357 | 0 | { |
358 | 0 | return OsiModelApplicationLayer; |
359 | 0 | } |
360 | | |
361 | | protected: |
362 | | DhcpV6LayerBase(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet) |
363 | 0 | : Layer(data, dataLen, prevLayer, packet, DHCPv6) |
364 | 0 | {} |
365 | | |
366 | | explicit DhcpV6LayerBase(size_t headerLen); |
367 | | |
368 | | virtual size_t getOptionsOffset() const = 0; |
369 | | |
370 | | void serializeOptions(ObjectScope& serializer, const FieldDescriptor& optionsField, |
371 | | const FieldDescriptor& optionField) const; |
372 | | |
373 | | private: |
374 | | uint8_t* getOptionsBasePtr() const |
375 | 0 | { |
376 | 0 | return m_Data + getOptionsOffset(); |
377 | 0 | } |
378 | | DhcpV6Option addOptionAt(const DhcpV6OptionBuilder& optionBuilder, int offset); |
379 | | |
380 | | TLVRecordReader<DhcpV6Option> m_OptionReader; |
381 | | }; |
382 | | |
383 | | /// @class DhcpV6Layer |
384 | | /// Represents a DHCPv6 (Dynamic Host Configuration Protocol version 6) client/server message: every message type |
385 | | /// except relay-forward and relay-reply, which DhcpV6RelayLayer represents |
386 | | class DhcpV6Layer : public DhcpV6LayerBase |
387 | | { |
388 | | public: |
389 | | /// A constructor that creates the layer from an existing packet raw data |
390 | | /// @param[in] data A pointer to the raw data |
391 | | /// @param[in] dataLen Size of the data in bytes |
392 | | /// @param[in] prevLayer A pointer to the previous layer |
393 | | /// @param[in] packet A pointer to the Packet instance where layer will be stored in |
394 | | DhcpV6Layer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet); |
395 | | |
396 | | /// A constructor that creates the layer from scratch |
397 | | /// @param[in] messageType A DHCPv6 message type to be set |
398 | | /// @param[in] transactionId The transaction ID to be set. Notice the transaction ID is 3-byte long so the value |
399 | | /// shouldn't exceed 0xFFFFFF |
400 | | /// @throws std::invalid_argument if messageType is DHCPV6_RELAY_FORWARD or DHCPV6_RELAY_REPLY, which |
401 | | /// DhcpV6RelayLayer represents |
402 | | DhcpV6Layer(DhcpV6MessageType messageType, uint32_t transactionId); |
403 | | |
404 | | /// Set the message type for this layer |
405 | | /// @param[in] messageType The message type to set |
406 | | /// @throws std::invalid_argument if messageType is DHCPV6_RELAY_FORWARD or DHCPV6_RELAY_REPLY, which |
407 | | /// DhcpV6RelayLayer represents |
408 | | void setMessageType(DhcpV6MessageType messageType); |
409 | | |
410 | | /// @return The transaction ID of this DHCPv6 message |
411 | | uint32_t getTransactionID() const; |
412 | | |
413 | | /// Set the transaction ID for this DHCPv6 message |
414 | | /// @param[in] transactionId The transaction ID value to set |
415 | | void setTransactionID(uint32_t transactionId) const; |
416 | | |
417 | | /// A static method that validates the input data |
418 | | /// @param[in] data The pointer to the beginning of a byte stream of a DHCPv6 layer |
419 | | /// @param[in] dataLen The length of the byte stream |
420 | | /// @return True if the data is valid and can represent a DHCPv6 client/server message. False for relay-forward |
421 | | /// and relay-reply messages, which DhcpV6RelayLayer represents |
422 | | static inline bool isDataValid(const uint8_t* data, size_t dataLen); |
423 | | |
424 | | /// @struct SerializedFields |
425 | | /// Struct that contains field descriptors for serialization |
426 | | struct SerializedFields : Layer::SerializedFields |
427 | | { |
428 | | /// @return All field descriptors for DhcpV6Layer |
429 | | static std::vector<FieldDescriptor> all() |
430 | 0 | { |
431 | 0 | auto result = Layer::SerializedFields::all(); |
432 | 0 | std::initializer_list<FieldDescriptor> extra{ MessageType, TransactionID, Options }; |
433 | 0 | std::copy(extra.begin(), extra.end(), std::back_inserter(result)); |
434 | 0 | return result; |
435 | 0 | } |
436 | | /// @brief DHCPv6 message type name |
437 | | static const FieldDescriptor MessageType; |
438 | | /// @brief Transaction identifier in hexadecimal |
439 | | static const FieldDescriptor TransactionID; |
440 | | /// @brief Array of DHCPv6 option names |
441 | | static const FieldDescriptor Options; |
442 | | /// @brief Name of one option within the options array |
443 | | static const FieldDescriptor Option; |
444 | | }; |
445 | | |
446 | | protected: |
447 | | void serializeLayer(ObjectScope& serializer) const override; |
448 | | |
449 | | size_t getOptionsOffset() const override |
450 | 0 | { |
451 | 0 | return sizeof(dhcpv6_header); |
452 | 0 | } |
453 | | |
454 | | private: |
455 | | dhcpv6_header* getDhcpHeader() const |
456 | 0 | { |
457 | 0 | return reinterpret_cast<dhcpv6_header*>(m_Data); |
458 | 0 | } |
459 | | }; |
460 | | |
461 | | /// @class DhcpV6RelayLayer |
462 | | /// Represents a DHCPv6 relay-forward or relay-reply message (RFC 8415 section 9), which has a hop count, a link |
463 | | /// address and a peer address instead of a transaction ID |
464 | | class DhcpV6RelayLayer : public DhcpV6LayerBase |
465 | | { |
466 | | public: |
467 | | /// A constructor that creates the layer from an existing packet raw data |
468 | | /// @param[in] data A pointer to the raw data |
469 | | /// @param[in] dataLen Size of the data in bytes |
470 | | /// @param[in] prevLayer A pointer to the previous layer |
471 | | /// @param[in] packet A pointer to the Packet instance where layer will be stored in |
472 | | DhcpV6RelayLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet); |
473 | | |
474 | | /// A constructor that creates the layer from scratch |
475 | | /// @param[in] messageType DHCPV6_RELAY_FORWARD or DHCPV6_RELAY_REPLY |
476 | | /// @param[in] hopCount The hop count to be set |
477 | | /// @param[in] linkAddress The link address to be set |
478 | | /// @param[in] peerAddress The peer address to be set |
479 | | /// @throws std::invalid_argument if messageType is not a relay message type |
480 | | DhcpV6RelayLayer(DhcpV6MessageType messageType, uint8_t hopCount, const IPv6Address& linkAddress, |
481 | | const IPv6Address& peerAddress); |
482 | | |
483 | | /// Set the message type for this layer |
484 | | /// @param[in] messageType DHCPV6_RELAY_FORWARD or DHCPV6_RELAY_REPLY |
485 | | /// @throws std::invalid_argument if messageType is not a relay message type |
486 | | void setMessageType(DhcpV6MessageType messageType); |
487 | | |
488 | | /// @return The hop count of this message |
489 | | uint8_t getHopCount() const; |
490 | | |
491 | | /// Set the hop count of this message |
492 | | /// @param[in] hopCount The hop count to set |
493 | | void setHopCount(uint8_t hopCount); |
494 | | |
495 | | /// @return The link address of this message |
496 | | IPv6Address getLinkAddress() const; |
497 | | |
498 | | /// Set the link address of this message |
499 | | /// @param[in] linkAddress The link address to set |
500 | | void setLinkAddress(const IPv6Address& linkAddress); |
501 | | |
502 | | /// @return The peer address of this message |
503 | | IPv6Address getPeerAddress() const; |
504 | | |
505 | | /// Set the peer address of this message |
506 | | /// @param[in] peerAddress The peer address to set |
507 | | void setPeerAddress(const IPv6Address& peerAddress); |
508 | | |
509 | | /// A static method that validates the input data |
510 | | /// @param[in] data The pointer to the beginning of a byte stream of a DHCPv6 layer |
511 | | /// @param[in] dataLen The length of the byte stream |
512 | | /// @return True if the data is a relay-forward or relay-reply message that is long enough for its header |
513 | | static inline bool isDataValid(const uint8_t* data, size_t dataLen); |
514 | | |
515 | | /// @struct SerializedFields |
516 | | /// Struct that contains field descriptors for serialization |
517 | | struct SerializedFields : Layer::SerializedFields |
518 | | { |
519 | | /// @return All field descriptors for DhcpV6RelayLayer |
520 | | static std::vector<FieldDescriptor> all() |
521 | 0 | { |
522 | 0 | auto result = Layer::SerializedFields::all(); |
523 | 0 | std::initializer_list<FieldDescriptor> extra{ MessageType, HopCount, LinkAddress, PeerAddress, |
524 | 0 | Options }; |
525 | 0 | std::copy(extra.begin(), extra.end(), std::back_inserter(result)); |
526 | 0 | return result; |
527 | 0 | } |
528 | | /// @brief DHCPv6 message type name |
529 | | static const FieldDescriptor MessageType; |
530 | | /// @brief Hop count |
531 | | static const FieldDescriptor HopCount; |
532 | | /// @brief Link address |
533 | | static const FieldDescriptor LinkAddress; |
534 | | /// @brief Peer address |
535 | | static const FieldDescriptor PeerAddress; |
536 | | /// @brief Array of DHCPv6 option names |
537 | | static const FieldDescriptor Options; |
538 | | /// @brief Name of one option within the options array |
539 | | static const FieldDescriptor Option; |
540 | | }; |
541 | | |
542 | | protected: |
543 | | void serializeLayer(ObjectScope& serializer) const override; |
544 | | |
545 | | size_t getOptionsOffset() const override |
546 | 0 | { |
547 | 0 | return sizeof(dhcpv6_relay_header); |
548 | 0 | } |
549 | | |
550 | | private: |
551 | | dhcpv6_relay_header* getRelayHeader() const |
552 | 0 | { |
553 | 0 | return reinterpret_cast<dhcpv6_relay_header*>(m_Data); |
554 | 0 | } |
555 | | }; |
556 | | |
557 | | // implementation of inline methods |
558 | | |
559 | | bool DhcpV6LayerBase::isDhcpV6Port(uint16_t port) |
560 | 0 | { |
561 | 0 | return (port == 546) || (port == 547); |
562 | 0 | } |
563 | | |
564 | | bool DhcpV6Layer::isDataValid(const uint8_t* data, size_t dataLen) |
565 | 0 | { |
566 | 0 | return data && dataLen >= sizeof(dhcpv6_header) && data[0] != DHCPV6_RELAY_FORWARD && |
567 | 0 | data[0] != DHCPV6_RELAY_REPLY; |
568 | 0 | } |
569 | | |
570 | | bool DhcpV6RelayLayer::isDataValid(const uint8_t* data, size_t dataLen) |
571 | 0 | { |
572 | 0 | return data && dataLen >= sizeof(dhcpv6_relay_header) && |
573 | 0 | (data[0] == DHCPV6_RELAY_FORWARD || data[0] == DHCPV6_RELAY_REPLY); |
574 | 0 | } |
575 | | |
576 | | } // namespace pcpp |