Coverage Report

Created: 2026-12-01 07:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/PcapPlusPlus/Packet++/header/IgmpLayer.h
Line
Count
Source
1
#pragma once
2
3
#include "Layer.h"
4
#include "IpAddress.h"
5
#include <vector>
6
7
/// @file
8
9
/// @namespace pcpp
10
/// @brief The main namespace for the PcapPlusPlus lib
11
namespace pcpp
12
{
13
  /// @struct igmp_header
14
  /// IGMPv1 and IGMPv2 basic protocol header
15
  struct igmp_header
16
  {
17
    /// Indicates the message type. The enum for message type is pcpp::IgmpType
18
    uint8_t type;
19
    /// Specifies the time limit for the corresponding report. The field has a resolution of 100 milliseconds
20
    uint8_t maxResponseTime;
21
    /// This is the 16-bit one's complement of the one's complement sum of the entire IGMP message
22
    uint16_t checksum;
23
    /// This is the multicast address being queried when sending a Group-Specific or Group-and-Source-Specific Query
24
    uint32_t groupAddress;
25
  };
26
  static_assert(sizeof(igmp_header) == 8, "igmp_header size is not 8 bytes");
27
28
  /// @struct igmpv3_query_header
29
  /// IGMPv3 membership query basic header
30
  struct igmpv3_query_header
31
  {
32
    /// IGMP message type. Should always have value of membership query (::IgmpType_MembershipQuery)
33
    uint8_t type;
34
    /// This field specifies the maximum time (in 1/10 second) allowed before sending a responding report
35
    uint8_t maxResponseTime;
36
    /// This is the 16-bit one's complement of the one's complement sum of the entire IGMP message
37
    uint16_t checksum;
38
    /// This is the multicast address being queried when sending a Group-Specific or Group-and-Source-Specific Query
39
    uint32_t groupAddress;
40
    /// Suppress Router-side Processing Flag + Querier's Robustness Variable
41
    uint8_t s_qrv;
42
    /// Querier's Query Interval Code
43
    uint8_t qqic;
44
    /// This field specifies the number of source addresses present in the Query
45
    uint16_t numOfSources;
46
  };
47
  static_assert(sizeof(igmpv3_query_header) == 12, "igmpv3_query_header size is not 12 bytes");
48
49
  /// @struct igmpv3_report_header
50
  /// IGMPv3 membership report basic header
51
  struct igmpv3_report_header
52
  {
53
    /// IGMP message type. Should always have value of IGMPv3 membership report (::IgmpType_MembershipReportV3)
54
    uint8_t type;
55
    /// Unused byte
56
    uint8_t reserved1;
57
    /// This is the 16-bit one's complement of the one's complement sum of the entire IGMP message
58
    uint16_t checksum;
59
    /// Unused bytes
60
    uint16_t reserved2;
61
    /// This field specifies the number of group records present in the Report
62
    uint16_t numOfGroupRecords;
63
  };
64
  static_assert(sizeof(igmpv3_report_header) == 8, "igmpv3_report_header size is not 8 bytes");
65
66
  /// @struct igmpv3_group_record
67
  /// A block of fields containing information pertaining to the sender's membership in a single multicast group on
68
  /// the interface from which the Report is sent. Relevant only for IGMPv3 membership report messages
69
  struct igmpv3_group_record
70
  {
71
    /// Group record type
72
    uint8_t recordType;
73
    /// Contains the length of the Auxiliary Data field in this Group Record. A value other than 0 isn't supported
74
    uint8_t auxDataLen;
75
    /// Specifies how many source addresses are present in this Group Record
76
    uint16_t numOfSources;
77
    /// Contains the IP multicast address to which this Group Record pertains
78
    uint32_t multicastAddress;
79
    /// A vector of n IP unicast addresses, where n is the value in this record's Number of Sources field
80
    uint8_t sourceAddresses[];
81
82
    /// @return The multicast address in igmpv3_group_record#multicastAddress as IPv4Address instance
83
    IPv4Address getMulticastAddress() const
84
0
    {
85
0
      return multicastAddress;
86
0
    }
87
88
    /// @return The number of source addresses in this group record
89
    uint16_t getSourceAddressCount() const;
90
91
    /// Get the source address at a certain index
92
    /// @param[in] index The index of the source address in the group record
93
    /// @return The source address in the requested index. If index is negative or higher than the number of source
94
    /// addresses in this group record the value if IPv4Address#Zero is returned
95
    IPv4Address getSourceAddressAtIndex(int index) const;
96
97
    /// @return The total size in bytes of the group record
98
    size_t getRecordLen() const;
99
  };
100
101
  /// IGMP message types
102
  enum IgmpType
103
  {
104
    /// Unknown message type
105
    IgmpType_Unknown = 0,
106
    /// IGMP Membership Query
107
    IgmpType_MembershipQuery = 0x11,
108
    /// IGMPv1 Membership Report
109
    IgmpType_MembershipReportV1 = 0x12,
110
    /// DVMRP
111
    IgmpType_DVMRP = 0x13,
112
    /// PIM version 1
113
    IgmpType_P1Mv1 = 0x14,
114
    /// Cisco Trace Messages
115
    IgmpType_CiscoTrace = 0x15,
116
    /// IGMPv2 Membership Report
117
    IgmpType_MembershipReportV2 = 0x16,
118
    /// IGMPv2 Leave Group
119
    IgmpType_LeaveGroup = 0x17,
120
    /// Multicast Traceroute Response
121
    IgmpType_MulticastTracerouteResponse = 0x1e,
122
    /// Multicast Traceroute
123
    IgmpType_MulticastTraceroute = 0x1f,
124
    /// IGMPv3 Membership Report
125
    IgmpType_MembershipReportV3 = 0x22,
126
    /// MRD, Multicast Router Advertisement
127
    IgmpType_MulticastRouterAdvertisement = 0x30,
128
    /// MRD, Multicast Router Solicitation
129
    IgmpType_MulticastRouterSolicitation = 0x31,
130
    /// MRD, Multicast Router Termination
131
    IgmpType_MulticastRouterTermination = 0x32,
132
  };
133
134
  /// @class IgmpLayer
135
  /// A base class for all IGMP (Internet Group Management Protocol) protocol classes. This is an abstract class and
136
  /// cannot be instantiated, only its child classes can be instantiated. The inherited classes represent the
137
  /// different versions of the protocol: IGMPv1, IGMPv2 and IGMPv3
138
  class IgmpLayer : public Layer
139
  {
140
  protected:
141
    IgmpLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet, ProtocolType igmpVer)
142
0
        : Layer(data, dataLen, prevLayer, packet, igmpVer)
143
0
    {}
144
145
    IgmpLayer(IgmpType type, const IPv4Address& groupAddr, uint8_t maxResponseTime, ProtocolType igmpVer);
146
147
    uint16_t calculateChecksum();
148
149
    size_t getHeaderSizeByVerAndType(ProtocolType igmpVer, IgmpType igmpType) const;
150
151
  public:
152
    ~IgmpLayer() override = default;
153
154
    /// Get a pointer to the raw IGMPv1/IGMPv2 header. Notice this points directly to the data, so every change will
155
    /// change the actual packet data
156
    /// @return A pointer to the @ref igmp_header
157
    igmp_header* getIgmpHeader() const
158
0
    {
159
0
      return reinterpret_cast<igmp_header*>(m_Data);
160
0
    }
161
162
    /// @return The IPv4 multicast address stored igmp_header#groupAddress
163
    IPv4Address getGroupAddress() const
164
0
    {
165
0
      return getIgmpHeader()->groupAddress;
166
0
    }
167
168
    /// Set the IPv4 multicast address
169
    /// @param[in] groupAddr The IPv4 address to set
170
    void setGroupAddress(const IPv4Address& groupAddr);
171
172
    /// @return IGMP type set in igmp_header#type as ::IgmpType enum. Notice that if igmp_header#type contains a
173
    /// value that doesn't appear in the ::IgmpType enum, ::IgmpType_Unknown will be returned
174
    IgmpType getType() const;
175
176
    /// Set IGMP type (will be written to igmp_header#type field)
177
    /// @param[in] type The type to set
178
    void setType(IgmpType type);
179
180
    /// A static method that gets raw IGMP data (byte stream) and returns the IGMP version of this IGMP message
181
    /// @param[in] data The IGMP raw data (byte stream)
182
    /// @param[in] dataLen Raw data length
183
    /// @param[out] isQuery Return true if IGMP message type is ::IgmpType_MembershipQuery and false otherwise
184
    /// @return One of the values ::IGMPv1, ::IGMPv2, ::IGMPv3 according to detected IGMP version or
185
    /// ::UnknownProtocol if couldn't detect IGMP version
186
    static ProtocolType getIGMPVerFromData(uint8_t* data, size_t dataLen, bool& isQuery);
187
188
    // implement abstract methods
189
190
    /// Does nothing for this layer (IGMP layer is always last)
191
    void parseNextLayer() override
192
0
    {}
193
194
    /// @return Size of IGMP header = 8B
195
    size_t getHeaderLen() const override
196
0
    {
197
0
      return sizeof(igmp_header);
198
0
    }
199
200
    std::string toString() const override;
201
202
    OsiModelLayer getOsiModelLayer() const override
203
0
    {
204
0
      return OsiModelNetworkLayer;
205
0
    }
206
207
    /// @struct SerializedFields
208
    /// Fields written by IgmpLayer's serializeLayer(), in addition to
209
    /// Layer::SerializedFields.
210
    struct SerializedFields : Layer::SerializedFields
211
    {
212
      /// @brief The highest field ID used by IgmpLayer
213
      static constexpr uint16_t MaxID = Layer::SerializedFields::MaxID + 5;
214
215
      /// @return All field descriptors for IgmpLayer
216
      static std::vector<FieldDescriptor> all()
217
0
      {
218
0
        auto result = Layer::SerializedFields::all();
219
0
        std::initializer_list<FieldDescriptor> extra{ Type, TypeName, MaxResponseTime, Checksum, GroupAddress };
220
0
        std::copy(extra.begin(), extra.end(), std::back_inserter(result));
221
0
        return result;
222
0
      }
223
224
      /// @brief IGMP message type, as an integer
225
      static const FieldDescriptor Type;
226
227
      /// @brief IGMP message type name, as a string
228
      static const FieldDescriptor TypeName;
229
230
      /// @brief Max Response Time (IGMPv2) or Max Response Code (IGMPv3), as the raw integer value. In IGMPv2 and
231
      /// in IGMPv3 values below 128 it's in units of 1/10 second, in IGMPv3 values of 128 and above it's encoded
232
      /// as a floating-point value (RFC 3376 section 4.1.1). Unused in IGMPv1
233
      static const FieldDescriptor MaxResponseTime;
234
235
      /// @brief Checksum, in hexadecimal notation
236
      static const FieldDescriptor Checksum;
237
238
      /// @brief Multicast group IPv4 address, as a string
239
      static const FieldDescriptor GroupAddress;
240
    };
241
242
  protected:
243
    void serializeLayer(ObjectScope& serializer) const override;
244
  };
245
246
  /// @class IgmpV1Layer
247
  /// Represents IGMPv1 (Internet Group Management Protocol ver 1) layer. This class represents all the different
248
  /// messages of IGMPv1
249
  class IgmpV1Layer : public IgmpLayer
250
  {
251
  public:
252
    /// A constructor that creates the layer from an existing packet raw data
253
    /// @param[in] data A pointer to the raw data
254
    /// @param[in] dataLen Size of the data in bytes
255
    /// @param[in] prevLayer A pointer to the previous layer
256
    /// @param[in] packet A pointer to the Packet instance where layer will be stored in
257
    IgmpV1Layer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
258
0
        : IgmpLayer(data, dataLen, prevLayer, packet, IGMPv1)
259
0
    {}
260
261
    /// A constructor that allocates a new IGMPv1 header
262
    /// @param[in] type The message type to set
263
    /// @param[in] groupAddr The multicast address to set. This is an optional parameter and has a default value of
264
    /// IPv4Address#Zero if not provided
265
    explicit IgmpV1Layer(IgmpType type, const IPv4Address& groupAddr = IPv4Address())
266
        : IgmpLayer(type, groupAddr, 0, IGMPv1)
267
0
    {}
268
269
    /// A destructor for this layer (does nothing)
270
    ~IgmpV1Layer() override = default;
271
272
    // implement abstract methods
273
274
    /// Calculate the IGMP checksum and set igmp_header#maxResponseTime to 0 (this field is unused in IGMPv1)
275
    void computeCalculateFields() override;
276
277
    /// A static method that validates the input data
278
    /// @param[in] data The pointer to the beginning of a byte stream of an IGMPv1 layer
279
    /// @param[in] dataLen The length of the byte stream
280
    /// @return True if the data is valid and can represent an IGMPv1 layer
281
    static bool isDataValid(uint8_t const* data, size_t dataLen)
282
0
    {
283
0
      return canReinterpretAs<igmp_header>(data, dataLen);
284
0
    }
285
  };
286
287
  /// @class IgmpV2Layer
288
  /// Represents IGMPv2 (Internet Group Management Protocol ver 2) layer. This class represents all the different
289
  /// messages of IGMPv2
290
  class IgmpV2Layer : public IgmpLayer
291
  {
292
  public:
293
    /// A constructor that creates the layer from an existing packet raw data
294
    /// @param[in] data A pointer to the raw data
295
    /// @param[in] dataLen Size of the data in bytes
296
    /// @param[in] prevLayer A pointer to the previous layer
297
    /// @param[in] packet A pointer to the Packet instance where layer will be stored in
298
    IgmpV2Layer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
299
0
        : IgmpLayer(data, dataLen, prevLayer, packet, IGMPv2)
300
0
    {}
301
302
    /// A constructor that allocates a new IGMPv2 header
303
    /// @param[in] type The message type to set
304
    /// @param[in] groupAddr The multicast address to set. This is an optional parameter and has a default value of
305
    /// unspecified/zero IPv4 address
306
    /// @param[in] maxResponseTime The max response time to set. This is an optional parameter and has a default
307
    /// value of 0 if not provided
308
    explicit IgmpV2Layer(IgmpType type, const IPv4Address& groupAddr = IPv4Address(), uint8_t maxResponseTime = 0)
309
        : IgmpLayer(type, groupAddr, maxResponseTime, IGMPv2)
310
0
    {}
311
312
    /// A destructor for this layer (does nothing)
313
    ~IgmpV2Layer() override = default;
314
315
    // implement abstract methods
316
317
    /// Calculate the IGMP checksum
318
    void computeCalculateFields() override;
319
320
    /// A static method that validates the input data
321
    /// @param[in] data The pointer to the beginning of a byte stream of an IGMPv2 layer
322
    /// @param[in] dataLen The length of the byte stream
323
    /// @return True if the data is valid and can represent an IGMPv2 layer
324
    static bool isDataValid(uint8_t const* data, size_t dataLen)
325
0
    {
326
0
      return canReinterpretAs<igmp_header>(data, dataLen);
327
0
    }
328
  };
329
330
  /// @class IgmpV3QueryLayer
331
  /// Represents an IGMPv3 (Internet Group Management Protocol ver 3) membership query message
332
  class IgmpV3QueryLayer : public IgmpLayer
333
  {
334
  public:
335
    /// A constructor that creates the layer from an existing packet raw data
336
    /// @param[in] data A pointer to the raw data
337
    /// @param[in] dataLen Size of the data in bytes
338
    /// @param[in] prevLayer A pointer to the previous layer
339
    /// @param[in] packet A pointer to the Packet instance where layer will be stored in
340
    IgmpV3QueryLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
341
342
    /// A constructor that allocates a new IGMPv3 membership query
343
    /// @param[in] multicastAddr The multicast address to set. This is an optional parameter and has a default value
344
    /// of unspecified/zero IPv4 address if not provided
345
    /// @param[in] maxResponseTime The max response time to set. This is an optional parameter and has a default
346
    /// value of 0 if not provided
347
    /// @param[in] s_qrv A 1-byte value representing the value in Suppress Router-side Processing Flag + Querier's
348
    /// Robustness Variable (igmpv3_query_header#s_qrv field). This is an optional parameter and has a default value
349
    /// of 0 if not provided
350
    explicit IgmpV3QueryLayer(const IPv4Address& multicastAddr = IPv4Address(), uint8_t maxResponseTime = 0,
351
                              uint8_t s_qrv = 0);
352
353
    /// Get a pointer to the raw IGMPv3 membership query header. Notice this points directly to the data, so every
354
    /// change will change the actual packet data
355
    /// @return A pointer to the @ref igmpv3_query_header
356
    igmpv3_query_header* getIgmpV3QueryHeader() const
357
0
    {
358
0
      return reinterpret_cast<igmpv3_query_header*>(m_Data);
359
0
    }
360
361
    /// @return The number of source addresses in this message (as extracted from the
362
    /// igmpv3_query_header#numOfSources field)
363
    uint16_t getSourceAddressCount() const;
364
365
    /// Get the IPV4 source address in a certain index
366
    /// @param[in] index The requested index of the source address
367
    /// @return The IPv4 source address, or IPv4Address#Zero if index is out of bounds (of the message or of the
368
    /// layer)
369
    IPv4Address getSourceAddressAtIndex(int index) const;
370
371
    /// Add a new source address at the end of the source address list. The igmpv3_query_header#numOfSources field
372
    /// will be incremented accordingly
373
    /// @param[in] addr The IPv4 source address to add
374
    /// @return True if source address was added successfully or false otherwise. If false is returned an
375
    /// appropriate error message will be printed to log
376
    bool addSourceAddress(const IPv4Address& addr);
377
378
    /// Add a new source address at a certain index of the source address list. The igmpv3_query_header#numOfSources
379
    /// field will be incremented accordingly
380
    /// @param[in] addr The IPv4 source address to add
381
    /// @param[in] index The index to add the new source address at
382
    /// @return True if source address was added successfully or false otherwise. If false is returned an
383
    /// appropriate error message will be printed to log
384
    bool addSourceAddressAtIndex(const IPv4Address& addr, int index);
385
386
    /// Remove a source address at a certain index. The igmpv3_query_header#numOfSources field will be decremented
387
    /// accordingly
388
    /// @param[in] index The index of the source address to be removed
389
    /// @return True if source address was removed successfully or false otherwise. If false is returned an
390
    /// appropriate error message will be printed to log
391
    bool removeSourceAddressAtIndex(int index);
392
393
    /// Remove all source addresses in the message. The igmpv3_query_header#numOfSources field will be set to 0
394
    /// @return True if all source addresses were cleared successfully or false otherwise. If false is returned an
395
    /// appropriate error message will be printed to log
396
    bool removeAllSourceAddresses();
397
398
    // implement abstract methods
399
400
    /// Calculate the IGMP checksum
401
    void computeCalculateFields() override;
402
403
    /// @return The message size in bytes which include the size of the basic header + the size of the source
404
    /// address list
405
    size_t getHeaderLen() const override;
406
407
    /// A static method that validates the input data
408
    /// @param[in] data The pointer to the beginning of a byte stream of an IGMPv3 Query layer
409
    /// @param[in] dataLen The length of the byte stream
410
    /// @return True if the data is valid and can represent an IGMPv3 Query layer
411
    static bool isDataValid(uint8_t const* data, size_t dataLen)
412
0
    {
413
0
      return canReinterpretAs<igmpv3_query_header>(data, dataLen);
414
0
    }
415
416
    /// @struct SerializedFields
417
    /// Fields written by IgmpV3QueryLayer's serializeLayer(), in addition to
418
    /// IgmpLayer::SerializedFields.
419
    struct SerializedFields : IgmpLayer::SerializedFields
420
    {
421
      /// @return All field descriptors for IgmpV3QueryLayer
422
      static std::vector<FieldDescriptor> all()
423
0
      {
424
0
        auto result = IgmpLayer::SerializedFields::all();
425
0
        std::initializer_list<FieldDescriptor> extra{ SuppressRouterSideProcessing, RobustnessVariable,
426
0
                                                    QueryIntervalCode, Sources };
427
0
        std::copy(extra.begin(), extra.end(), std::back_inserter(result));
428
0
        return result;
429
0
      }
430
431
      /// @brief Suppress Router-side Processing flag (S flag)
432
      static const FieldDescriptor SuppressRouterSideProcessing;
433
434
      /// @brief Querier's Robustness Variable (QRV)
435
      static const FieldDescriptor RobustnessVariable;
436
437
      /// @brief Querier's Query Interval Code (QQIC), as the raw integer value. Values below 128 are in seconds,
438
      /// values of 128 and above are encoded as a floating-point value (RFC 3376 section 4.1.7)
439
      static const FieldDescriptor QueryIntervalCode;
440
441
      /// @brief Array of unicast IPv4 source addresses; elements described by Source
442
      static const FieldDescriptor Sources;
443
444
      /// @brief Unicast IPv4 source address, as a string; element of Sources, not included in all()
445
      static const FieldDescriptor Source;
446
    };
447
448
  protected:
449
    void serializeLayer(ObjectScope& serializer) const override;
450
  };
451
452
  /// @class IgmpV3ReportLayer
453
  /// Represents an IGMPv3 (Internet Group Management Protocol ver 3) membership report message
454
  class IgmpV3ReportLayer : public IgmpLayer
455
  {
456
  private:
457
    igmpv3_group_record* addGroupRecordAt(uint8_t recordType, const IPv4Address& multicastAddress,
458
                                          const std::vector<IPv4Address>& sourceAddresses, int offset);
459
460
  public:
461
    /// A constructor that creates the layer from an existing packet raw data
462
    /// @param[in] data A pointer to the raw data
463
    /// @param[in] dataLen Size of the data in bytes
464
    /// @param[in] prevLayer A pointer to the previous layer
465
    /// @param[in] packet A pointer to the Packet instance where layer will be stored in
466
    IgmpV3ReportLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
467
0
        : IgmpLayer(data, dataLen, prevLayer, packet, IGMPv3)
468
0
    {}
469
470
    /// A constructor that allocates a new IGMPv3 membership report with 0 group addresses
471
    IgmpV3ReportLayer() : IgmpLayer(IgmpType_MembershipReportV3, IPv4Address(), 0, IGMPv3)
472
0
    {}
473
474
    /// Get a pointer to the raw IGMPv3 membership report header. Notice this points directly to the data, so every
475
    /// change will change the actual packet data
476
    /// @return A pointer to the @ref igmpv3_report_header
477
    igmpv3_report_header* getReportHeader() const
478
0
    {
479
0
      return reinterpret_cast<igmpv3_report_header*>(m_Data);
480
0
    }
481
482
    /// @return The number of group records in this message (as extracted from the
483
    /// igmpv3_report_header#numOfGroupRecords field)
484
    uint16_t getGroupRecordCount() const;
485
486
    /// @return A pointer to the first group record or nullptr if no group records exist. Notice the return value is
487
    /// a pointer to the real data, so changes in the return value will affect the packet data
488
    igmpv3_group_record* getFirstGroupRecord() const;
489
490
    /// Get the group record that comes next to a given group record. If "groupRecord" is nullptr then nullptr will
491
    /// be returned. If "groupRecord" is the last group record or if it is out of layer bounds nullptr will be
492
    /// returned also. Notice the return value is a pointer to the real data casted to igmpv3_group_record type (as
493
    /// opposed to a copy of the option data). So changes in the return value will affect the packet data
494
    /// @param[in] groupRecord The group record to start searching from
495
    /// @return The next group record or nullptr if "groupRecord" is nullptr, last or out of layer bounds
496
    igmpv3_group_record* getNextGroupRecord(igmpv3_group_record* groupRecord) const;
497
498
    /// Add a new group record at a the end of the group record list. The igmpv3_report_header#numOfGroupRecords
499
    /// field will be incremented accordingly
500
    /// @param[in] recordType The type of the new group record
501
    /// @param[in] multicastAddress The multicast address of the new group record
502
    /// @param[in] sourceAddresses A vector containing all the source addresses of the new group record
503
    /// @return The method constructs a new group record, adds it to the end of the group record list of IGMPv3
504
    /// report message and returns a pointer to the new message. If something went wrong in creating or adding the
505
    /// new group record a nullptr value is returned and an appropriate error message is printed to log
506
    igmpv3_group_record* addGroupRecord(uint8_t recordType, const IPv4Address& multicastAddress,
507
                                        const std::vector<IPv4Address>& sourceAddresses);
508
509
    /// Add a new group record at a certain index of the group record list. The
510
    /// igmpv3_report_header#numOfGroupRecords field will be incremented accordingly
511
    /// @param[in] recordType The type of the new group record
512
    /// @param[in] multicastAddress The multicast address of the new group record
513
    /// @param[in] sourceAddresses A vector containing all the source addresses of the new group record
514
    /// @param[in] index The index to add the new group address at
515
    /// @return The method constructs a new group record, adds it to the IGMPv3 report message and returns a pointer
516
    /// to the new message. If something went wrong in creating or adding the new group record a nullptr value is
517
    /// returned and an appropriate error message is printed to log
518
    igmpv3_group_record* addGroupRecordAtIndex(uint8_t recordType, const IPv4Address& multicastAddress,
519
                                               const std::vector<IPv4Address>& sourceAddresses, int index);
520
521
    /// Remove a group record at a certain index. The igmpv3_report_header#numOfGroupRecords field will be
522
    /// decremented accordingly
523
    /// @param[in] index The index of the group record to be removed
524
    /// @return True if group record was removed successfully or false otherwise. If false is returned an
525
    /// appropriate error message will be printed to log
526
    bool removeGroupRecordAtIndex(int index);
527
528
    /// Remove all group records in the message. The igmpv3_report_header#numOfGroupRecords field will be set to 0
529
    /// @return True if all group records were cleared successfully or false otherwise. If false is returned an
530
    /// appropriate error message will be printed to log
531
    bool removeAllGroupRecords();
532
533
    // implement abstract methods
534
535
    /// Calculate the IGMP checksum
536
    void computeCalculateFields() override;
537
538
    /// @return The message size in bytes which include the size of the basic header + the size of the group record
539
    /// list
540
    size_t getHeaderLen() const override
541
0
    {
542
0
      return m_DataLen;
543
0
    }
544
545
    /// A static method that validates the input data
546
    /// @param[in] data The pointer to the beginning of a byte stream of an IGMPv3 Report layer
547
    /// @param[in] dataLen The length of the byte stream
548
    /// @return True if the data is valid and can represent an IGMPv3 Report layer
549
    static bool isDataValid(uint8_t const* data, size_t dataLen)
550
0
    {
551
0
      return canReinterpretAs<igmpv3_report_header>(data, dataLen);
552
0
    }
553
554
    /// @struct SerializedFields
555
    /// Fields written by IgmpV3ReportLayer's serializeLayer(), in addition to
556
    /// IgmpLayer::SerializedFields.
557
    struct SerializedFields : IgmpLayer::SerializedFields
558
    {
559
      /// @return All field descriptors for IgmpV3ReportLayer
560
      static std::vector<FieldDescriptor> all()
561
0
      {
562
0
        auto result = Layer::SerializedFields::all();
563
0
        std::initializer_list<FieldDescriptor> extra{ Type, TypeName, Checksum, GroupRecords };
564
0
        std::copy(extra.begin(), extra.end(), std::back_inserter(result));
565
0
        return result;
566
0
      }
567
568
      /// @struct GroupRecordObject
569
      /// Fields describing one element of the GroupRecords array
570
      struct GroupRecordObject : ObjectFieldDescriptor<GroupRecordObject>
571
      {
572
        using ObjectFieldDescriptor::ObjectFieldDescriptor;
573
574
        /// @return All field descriptors for one group record
575
        static std::vector<FieldDescriptor> all()
576
0
        {
577
0
          return { RecordType, AuxDataLen, MulticastAddress, Sources };
578
0
        }
579
580
        /// @brief Group record type, as an integer
581
        static const FieldDescriptor RecordType;
582
583
        /// @brief Auxiliary data length, in 32-bit words
584
        static const FieldDescriptor AuxDataLen;
585
586
        /// @brief Multicast IPv4 address, as a string
587
        static const FieldDescriptor MulticastAddress;
588
589
        /// @brief Array of unicast IPv4 source addresses; elements described by Source
590
        static const FieldDescriptor Sources;
591
592
        /// @brief Unicast IPv4 source address, as a string; element of Sources, not included in all()
593
        static const FieldDescriptor Source;
594
      };
595
596
      /// @brief Array of group records; elements described by GroupRecordObject
597
      static const FieldDescriptor GroupRecords;
598
    };
599
600
  protected:
601
    void serializeLayer(ObjectScope& serializer) const override;
602
  };
603
604
}  // namespace pcpp