| 1 |
#ifndef _PROTO_PKT_IGMP |
| 2 |
#define _PROTO_PKT_IGMP |
| 3 |
|
| 4 |
#include "protoPkt.h" |
| 5 |
#include "protoAddress.h" |
| 6 |
|
| 7 |
class ProtoPktIGMP : public ProtoPkt |
| 8 |
{ |
| 9 |
public: |
| 10 |
ProtoPktIGMP(void* bufferPtr = NULL, |
| 11 |
unsigned int numBytes = 0, |
| 12 |
bool initFromBuffer = false, |
| 13 |
bool freeOnDestruct = false); |
| 14 |
~ProtoPktIGMP(); |
| 15 |
|
| 16 |
enum Type |
| 17 |
{ |
| 18 |
QUERY = 0x11, |
| 19 |
REPORT_V1 = 0x12, |
| 20 |
REPORT_V2 = 0x16, |
| 21 |
REPORT_V3 = 0x22, |
| 22 |
LEAVE = 0x17 |
| 23 |
}; |
| 24 |
|
| 25 |
static const UINT8 DEFAULT_QRV; // 2 per RFC 3376 |
| 26 |
static const double DEFAULT_QQIC; // 125 seconds per RFC 3376 |
| 27 |
static const double DEFAULT_MAX_RESP; // 10 seconds per RFC 3376 |
| 28 |
|
| 29 |
// IGMPv3 REPORT Group Record |
| 30 |
class GroupRecord : public ProtoPkt |
| 31 |
{ |
| 32 |
public: |
| 33 |
GroupRecord(void* bufferPtr = NULL, |
| 34 |
unsigned int numBytes = 0, |
| 35 |
bool initFromBuffer = false, |
| 36 |
bool freeOnDestruct = false); |
| 37 |
~GroupRecord(); |
| 38 |
|
| 39 |
enum Type |
| 40 |
{ |
| 41 |
INVALID_TYPE = 0, |
| 42 |
MODE_IS_INCLUDE = 1, |
| 43 |
MODE_IS_EXCLUDE = 2, |
| 44 |
CHANGE_TO_INCLUDE_MODE = 3, // include none == ASM leave, otherwise SSM join src list |
| 45 |
CHANGE_TO_EXCLUDE_MODE = 4, // exclude none == ASM join group, otherwise SSM leave src list |
| 46 |
ALLOW_NEW_SOURCES = 5, // source-specific join essentially? |
| 47 |
BLOCK_OLD_SOURCES = 6 // source-specific leave essentially? |
| 48 |
}; |
| 49 |
|
| 50 |
// Use these to parse Group Records |
| 51 |
bool InitFromBuffer(void* bufferPtr = NULL, |
| 52 |
unsigned int bufferBytes = 0, |
| 53 |
bool freeOnDestruct = false); |
| 54 |
|
| 55 |
Type GetType() const |
| 56 |
{return (Type)GetUINT8(OFFSET_TYPE);} |
| 57 |
unsigned int GetAuxDataLen() const |
| 58 |
{ |
| 59 |
unsigned int len = GetUINT8(OFFSET_AUX_LEN); |
| 60 |
return (len << 2); // returns length in bytes |
| 61 |
} |
| 62 |
UINT16 GetNumSources() const |
| 63 |
{return GetUINT16(OFFSET_NUM_SRC);} |
| 64 |
bool GetGroupAddress(ProtoAddress& groupAddr) |
| 65 |
{ |
| 66 |
groupAddr.SetRawHostAddress(ProtoAddress::IPv4, (char*)GetBuffer(OFFSET_GROUP), 4); |
| 67 |
return groupAddr.IsMulticast(); |
| 68 |
} |
| 69 |
bool GetSourceAddress(UINT16 index, ProtoAddress& srcAddr) const; |
| 70 |
const void* GetAuxData() const |
| 71 |
{return GetBuffer(OffsetAuxData());} |
| 72 |
|
| 73 |
// Use these to create group records |
| 74 |
bool InitIntoBuffer(void* bufferPtr = NULL, |
| 75 |
unsigned int numBytes = 0, |
| 76 |
bool freeOnDestruct = false); |
| 77 |
void SetType(Type type) |
| 78 |
{SetUINT8(OFFSET_TYPE, (UINT8)type);} |
| 79 |
void SetGroupAddress(const ProtoAddress* groupAddr = NULL); |
| 80 |
bool AppendSourceAddress(const ProtoAddress& srcAddr); |
| 81 |
bool AppendAuxiliaryData(const char* data, UINT16 len); |
| 82 |
|
| 83 |
private: |
| 84 |
enum |
| 85 |
{ |
| 86 |
OFFSET_TYPE = 0, |
| 87 |
OFFSET_AUX_LEN = OFFSET_TYPE + 1, |
| 88 |
OFFSET_NUM_SRC = OFFSET_AUX_LEN + 1, |
| 89 |
OFFSET_GROUP = OFFSET_NUM_SRC + 2, |
| 90 |
OFFSET_SRC_LIST = OFFSET_GROUP + 4 |
| 91 |
}; |
| 92 |
UINT16 OffsetAuxData() const |
| 93 |
{return (OFFSET_SRC_LIST + 4 * GetNumSources());} |
| 94 |
}; // end class ProtoPktIGMP::GroupRecord |
| 95 |
|
| 96 |
// Use these to parse the IGMP message |
| 97 |
bool InitFromBuffer(UINT16 pktLength, |
| 98 |
void* bufferPtr = NULL, |
| 99 |
unsigned int buferBytes = 0, |
| 100 |
bool freeOnDestruct = false); |
| 101 |
|
| 102 |
UINT8 GetVersion() const; |
| 103 |
|
| 104 |
Type GetType() const |
| 105 |
{return (Type)GetUINT8(OFFSET_TYPE);} |
| 106 |
double GetMaxResponseTime() const; // return value in seconds |
| 107 |
UINT16 GetChecksum() const |
| 108 |
{return GetUINT16(OFFSET_CHECKSUM);} |
| 109 |
// For QUERY, REPORT_V1, REPORT_V2, LEAVE only |
| 110 |
bool GetGroupAddress(ProtoAddress& groupAddr) const |
| 111 |
{ |
| 112 |
groupAddr.SetRawHostAddress(ProtoAddress::IPv4, (char*)GetBuffer(OFFSET_GROUP), 4); |
| 113 |
return groupAddr.IsMulticast(); |
| 114 |
} |
| 115 |
// These 5 are for IGMPv3 QUERY messages only |
| 116 |
bool GetSuppress() const |
| 117 |
{return (GetVersion() < 3) ? false : (0 != (GetUINT8(OFFSET_S) & 0x08));} |
| 118 |
UINT8 GetQRV() const |
| 119 |
{return ((GetVersion() < 3) ? 0 : (GetUINT8(OFFSET_QRV) & 0x07));} |
| 120 |
double GetQQIC() const; |
| 121 |
UINT16 GetNumSources() const |
| 122 |
{return (GetVersion() < 3) ? 0 : GetUINT16(OFFSET_NUM_SRC);} |
| 123 |
bool GetSourceAddress(UINT16 index, ProtoAddress& srcAddr) const; |
| 124 |
// These 2 are for IGMPv3 REPORT messages |
| 125 |
UINT16 GetNumRecords() const |
| 126 |
{return GetUINT16(OFFSET_NUM_REC);} |
| 127 |
// This uses the prev groupRecord state to fetch next. If "groupRecord" |
| 128 |
// passed in has a NULL buffer_ptr _or_ "first_ is true, then the |
| 129 |
// first record is fetched. |
| 130 |
bool GetNextGroupRecord(GroupRecord& groupRecord, bool first = false); |
| 131 |
|
| 132 |
|
| 133 |
// Use these for building IGMP packets |
| 134 |
bool InitIntoBuffer(Type type, |
| 135 |
unsigned int version, |
| 136 |
void* bufferPtr = NULL, |
| 137 |
unsigned int numBytes = 0, |
| 138 |
bool freeOnDestruct = false); |
| 139 |
|
| 140 |
// This should be set to zero for IGMPv1 queries and report messages |
| 141 |
void SetMaxResponseTime(double seconds, bool updateChecksum = true); |
| 142 |
// set groupAddr pointer to NULL for general query |
| 143 |
void SetGroupAddress(ProtoAddress* groupAddr = NULL, bool updateChecksum = true); |
| 144 |
// These 4 are for IGMPv3 QUERY messages only |
| 145 |
bool SetSuppress(bool state, bool updateChecksum = true); |
| 146 |
bool SetQRV(UINT8 qrv, bool updateChecksum = true); |
| 147 |
bool SetQQIC(double seconds, bool updateChecksum = true); |
| 148 |
bool AppendSourceAddress(ProtoAddress& srcAddr, bool updateChecksum = true); |
| 149 |
// These are for IGMPv3 REPORT messages only |
| 150 |
bool AttachGroupRecord(GroupRecord& groupRecord); // inits "groupRecord" |
| 151 |
bool AppendGroupRecord(const GroupRecord& groupRecord, bool updateChecksum = true); |
| 152 |
|
| 153 |
|
| 154 |
// Return computed checksum in host byte order and set by default |
| 155 |
UINT16 ComputeChecksum(bool set = true); |
| 156 |
UINT16 FinalizeChecksum() |
| 157 |
{return ComputeChecksum(true);} |
| 158 |
|
| 159 |
|
| 160 |
private: |
| 161 |
// shared offsets for IGMP query, report, and leave messages |
| 162 |
// (all UINT8 offsets used here) |
| 163 |
enum |
| 164 |
{ |
| 165 |
OFFSET_TYPE = 0, |
| 166 |
OFFSET_MAX_RESP = OFFSET_TYPE + 1, |
| 167 |
OFFSET_CHECKSUM = OFFSET_MAX_RESP + 1, |
| 168 |
OFFSET_GROUP = OFFSET_CHECKSUM + 2, |
| 169 |
OFFSET_RESERVED = OFFSET_GROUP + 4, |
| 170 |
OFFSET_S = OFFSET_RESERVED, |
| 171 |
OFFSET_QRV = OFFSET_S, |
| 172 |
OFFSET_QQIC = OFFSET_QRV + 1, |
| 173 |
OFFSET_NUM_SRC = OFFSET_QQIC + 1, // for IGMPv3 queries |
| 174 |
OFFSET_NUM_REC = OFFSET_GROUP + 2, // for IGMPv3 reports |
| 175 |
OFFSET_SRC_LIST = OFFSET_NUM_SRC + 2, // for IGMPv3 queries |
| 176 |
OFFSET_REC_LIST = OFFSET_NUM_REC + 2 // for IGMPv3 reports |
| 177 |
}; |
| 178 |
// IGMPv3 group record field offsets |
| 179 |
|
| 180 |
|
| 181 |
}; // end class ProtoPktIGMP |
| 182 |
|
| 183 |
// TBD - implement MLD (incl. MLDv2) ... ProtoPktMLD |
| 184 |
|
| 185 |
#endif // _PROTO_PKT_IGMP |