PcapPlusPlus  Next
RadiusLayer.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "Layer.h"
4 #include "TLVData.h"
5 
7 
12 namespace pcpp
13 {
14 
19 #pragma pack(push, 1)
21  {
23  uint8_t code;
25  uint8_t id;
27  uint16_t length;
29  uint8_t authenticator[16];
30  };
31 #pragma pack(pop)
32 
38  class RadiusAttribute : public TLVRecord<uint8_t, uint8_t>
39  {
40  public:
45  explicit RadiusAttribute(uint8_t* attrRawData) : TLVRecord(attrRawData)
46  {}
47 
51  ~RadiusAttribute() override = default;
52 
53  // implement abstract methods
54 
55  size_t getTotalSize() const override
56  {
57  if (m_Data == nullptr)
58  return 0;
59 
60  return static_cast<size_t>(m_Data->recordLen);
61  }
62 
63  size_t getDataSize() const override
64  {
65  if (m_Data == nullptr)
66  return 0;
67 
68  return static_cast<size_t>(m_Data->recordLen) - 2 * sizeof(uint8_t);
69  }
70  };
71 
78  {
79  public:
88  RadiusAttributeBuilder(uint8_t attrType, const uint8_t* attrValue, uint8_t attrValueLen)
89  : TLVRecordBuilder(attrType, attrValue, attrValueLen)
90  {}
91 
98  RadiusAttributeBuilder(uint8_t attrType, uint8_t attrValue) : TLVRecordBuilder(attrType, attrValue)
99  {}
100 
107  RadiusAttributeBuilder(uint8_t attrType, uint16_t attrValue) : TLVRecordBuilder(attrType, attrValue)
108  {}
109 
116  RadiusAttributeBuilder(uint8_t attrType, uint32_t attrValue) : TLVRecordBuilder(attrType, attrValue)
117  {}
118 
125  RadiusAttributeBuilder(uint8_t attrType, const IPv4Address& attrValue) : TLVRecordBuilder(attrType, attrValue)
126  {}
127 
134  RadiusAttributeBuilder(uint8_t attrType, const std::string& attrValue) : TLVRecordBuilder(attrType, attrValue)
135  {}
136 
142  {}
143 
149  {
150  TLVRecordBuilder::operator=(other);
151  return *this;
152  }
153 
159  };
160 
165  class RadiusLayer : public Layer
166  {
167  private:
168  TLVRecordReader<RadiusAttribute> m_AttributeReader;
169 
170  uint8_t* getAttributesBasePtr() const
171  {
172  return m_Data + sizeof(radius_header);
173  }
174 
175  RadiusAttribute addAttrAt(const RadiusAttributeBuilder& attrBuilder, int offset);
176 
177  public:
185  RadiusLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
186  : Layer(data, dataLen, prevLayer, packet, Radius)
187  {}
188 
199  RadiusLayer(uint8_t code, uint8_t id, const uint8_t* authenticator, uint8_t authenticatorArrSize);
200 
210  RadiusLayer(uint8_t code, uint8_t id, const std::string& authenticator);
211 
215  ~RadiusLayer() override = default;
216 
223  {
224  return reinterpret_cast<radius_header*>(m_Data);
225  }
226 
230  std::string getAuthenticatorValue() const;
231 
236  void setAuthenticatorValue(const std::string& authValue);
237 
244  static std::string getRadiusMessageString(uint8_t radiusMessageCode);
245 
251 
260 
267  RadiusAttribute getAttribute(uint8_t attrType) const;
268 
272  size_t getAttributeCount() const;
273 
281 
289  RadiusAttribute addAttributeAfter(const RadiusAttributeBuilder& attrBuilder, uint8_t prevAttrType);
290 
297  bool removeAttribute(uint8_t attrType);
298 
304 
311  static bool isDataValid(const uint8_t* udpData, size_t udpDataLen);
312 
317  static inline bool isRadiusPort(uint16_t port);
318 
319  // implement abstract methods
320 
324  size_t getHeaderLen() const override;
325 
329  void parseNextLayer() override
330  {}
331 
335  void computeCalculateFields() override;
336 
337  std::string toString() const override;
338 
340  {
342  }
343  };
344 
345  // implementation of inline methods
346 
347  bool RadiusLayer::isRadiusPort(uint16_t port)
348  {
349  switch (port)
350  {
351  case 1812:
352  case 1813:
353  case 3799:
354  return true;
355  default:
356  return false;
357  }
358  } // isRadiusPort
359 
360 } // namespace pcpp
Definition: IpAddress.h:32
Definition: Layer.h:69
Definition: Packet.h:27
Definition: RadiusLayer.h:78
RadiusAttributeBuilder & operator=(const RadiusAttributeBuilder &other)
Definition: RadiusLayer.h:148
RadiusAttribute build() const
RadiusAttributeBuilder(uint8_t attrType, uint8_t attrValue)
Definition: RadiusLayer.h:98
RadiusAttributeBuilder(uint8_t attrType, const std::string &attrValue)
Definition: RadiusLayer.h:134
RadiusAttributeBuilder(uint8_t attrType, uint32_t attrValue)
Definition: RadiusLayer.h:116
RadiusAttributeBuilder(uint8_t attrType, uint16_t attrValue)
Definition: RadiusLayer.h:107
RadiusAttributeBuilder(uint8_t attrType, const IPv4Address &attrValue)
Definition: RadiusLayer.h:125
RadiusAttributeBuilder(const RadiusAttributeBuilder &other)
Definition: RadiusLayer.h:141
RadiusAttributeBuilder(uint8_t attrType, const uint8_t *attrValue, uint8_t attrValueLen)
Definition: RadiusLayer.h:88
Definition: RadiusLayer.h:39
~RadiusAttribute() override=default
RadiusAttribute(uint8_t *attrRawData)
Definition: RadiusLayer.h:45
size_t getTotalSize() const override
Definition: RadiusLayer.h:55
size_t getDataSize() const override
Definition: RadiusLayer.h:63
Definition: RadiusLayer.h:166
size_t getHeaderLen() const override
RadiusAttribute getAttribute(uint8_t attrType) const
bool removeAllAttributes()
RadiusAttribute getNextAttribute(RadiusAttribute &attr) const
radius_header * getRadiusHeader() const
Definition: RadiusLayer.h:222
RadiusLayer(uint8_t code, uint8_t id, const uint8_t *authenticator, uint8_t authenticatorArrSize)
~RadiusLayer() override=default
RadiusAttribute getFirstAttribute() const
static bool isDataValid(const uint8_t *udpData, size_t udpDataLen)
RadiusAttribute addAttributeAfter(const RadiusAttributeBuilder &attrBuilder, uint8_t prevAttrType)
std::string getAuthenticatorValue() const
void setAuthenticatorValue(const std::string &authValue)
RadiusLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: RadiusLayer.h:185
RadiusAttribute addAttribute(const RadiusAttributeBuilder &attrBuilder)
size_t getAttributeCount() const
std::string toString() const override
bool removeAttribute(uint8_t attrType)
void parseNextLayer() override
Definition: RadiusLayer.h:329
static std::string getRadiusMessageString(uint8_t radiusMessageCode)
OsiModelLayer getOsiModelLayer() const override
Definition: RadiusLayer.h:339
RadiusLayer(uint8_t code, uint8_t id, const std::string &authenticator)
void computeCalculateFields() override
static bool isRadiusPort(uint16_t port)
Definition: RadiusLayer.h:347
Definition: TLVData.h:419
Definition: TLVData.h:23
Definition: TLVData.h:246
The main namespace for the PcapPlusPlus lib.
OsiModelLayer
Definition: ProtocolType.h:364
@ OsiModelApplicationLayer
Definition: ProtocolType.h:378
const ProtocolType Radius
Definition: ProtocolType.h:213
Definition: RadiusLayer.h:21
uint8_t authenticator[16]
Definition: RadiusLayer.h:29
uint16_t length
Definition: RadiusLayer.h:27
uint8_t id
Definition: RadiusLayer.h:25
uint8_t code
Definition: RadiusLayer.h:23