PcapPlusPlus  24.09
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  virtual ~RadiusAttribute()
52  {}
53 
54  // implement abstract methods
55 
56  size_t getTotalSize() const
57  {
58  if (m_Data == nullptr)
59  return 0;
60 
61  return (size_t)m_Data->recordLen;
62  }
63 
64  size_t getDataSize() const
65  {
66  if (m_Data == nullptr)
67  return 0;
68 
69  return (size_t)m_Data->recordLen - 2 * sizeof(uint8_t);
70  }
71  };
72 
79  {
80  public:
89  RadiusAttributeBuilder(uint8_t attrType, const uint8_t* attrValue, uint8_t attrValueLen)
90  : TLVRecordBuilder(attrType, attrValue, attrValueLen)
91  {}
92 
99  RadiusAttributeBuilder(uint8_t attrType, uint8_t attrValue) : TLVRecordBuilder(attrType, attrValue)
100  {}
101 
108  RadiusAttributeBuilder(uint8_t attrType, uint16_t attrValue) : TLVRecordBuilder(attrType, attrValue)
109  {}
110 
117  RadiusAttributeBuilder(uint8_t attrType, uint32_t attrValue) : TLVRecordBuilder(attrType, attrValue)
118  {}
119 
126  RadiusAttributeBuilder(uint8_t attrType, const IPv4Address& attrValue) : TLVRecordBuilder(attrType, attrValue)
127  {}
128 
135  RadiusAttributeBuilder(uint8_t attrType, const std::string& attrValue) : TLVRecordBuilder(attrType, attrValue)
136  {}
137 
143  {}
144 
150  {
151  TLVRecordBuilder::operator=(other);
152  return *this;
153  }
154 
160  };
161 
166  class RadiusLayer : public Layer
167  {
168  private:
169  TLVRecordReader<RadiusAttribute> m_AttributeReader;
170 
171  uint8_t* getAttributesBasePtr() const
172  {
173  return m_Data + sizeof(radius_header);
174  }
175 
176  RadiusAttribute addAttrAt(const RadiusAttributeBuilder& attrBuilder, int offset);
177 
178  public:
186  RadiusLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
187  : Layer(data, dataLen, prevLayer, packet, Radius)
188  {}
189 
200  RadiusLayer(uint8_t code, uint8_t id, const uint8_t* authenticator, uint8_t authenticatorArrSize);
201 
211  RadiusLayer(uint8_t code, uint8_t id, const std::string& authenticator);
212 
217  {}
218 
225  {
226  return (radius_header*)m_Data;
227  }
228 
232  std::string getAuthenticatorValue() const;
233 
238  void setAuthenticatorValue(const std::string& authValue);
239 
246  static std::string getRadiusMessageString(uint8_t radiusMessageCode);
247 
253 
262 
269  RadiusAttribute getAttribute(uint8_t attrType) const;
270 
274  size_t getAttributeCount() const;
275 
283 
291  RadiusAttribute addAttributeAfter(const RadiusAttributeBuilder& attrBuilder, uint8_t prevAttrType);
292 
299  bool removeAttribute(uint8_t attrType);
300 
306 
313  static bool isDataValid(const uint8_t* udpData, size_t udpDataLen);
314 
319  static inline bool isRadiusPort(uint16_t port);
320 
321  // implement abstract methods
322 
326  size_t getHeaderLen() const;
327 
332  {}
333 
338 
339  std::string toString() const;
340 
342  {
344  }
345  };
346 
347  // implementation of inline methods
348 
349  bool RadiusLayer::isRadiusPort(uint16_t port)
350  {
351  switch (port)
352  {
353  case 1812:
354  case 1813:
355  case 3799:
356  return true;
357  default:
358  return false;
359  }
360  } // isRadiusPort
361 
362 } // namespace pcpp
Definition: IpAddress.h:32
Definition: Layer.h:70
Definition: Packet.h:27
Definition: RadiusLayer.h:79
RadiusAttributeBuilder & operator=(const RadiusAttributeBuilder &other)
Definition: RadiusLayer.h:149
RadiusAttribute build() const
RadiusAttributeBuilder(uint8_t attrType, uint8_t attrValue)
Definition: RadiusLayer.h:99
RadiusAttributeBuilder(uint8_t attrType, const std::string &attrValue)
Definition: RadiusLayer.h:135
RadiusAttributeBuilder(uint8_t attrType, uint32_t attrValue)
Definition: RadiusLayer.h:117
RadiusAttributeBuilder(uint8_t attrType, uint16_t attrValue)
Definition: RadiusLayer.h:108
RadiusAttributeBuilder(uint8_t attrType, const IPv4Address &attrValue)
Definition: RadiusLayer.h:126
RadiusAttributeBuilder(const RadiusAttributeBuilder &other)
Definition: RadiusLayer.h:142
RadiusAttributeBuilder(uint8_t attrType, const uint8_t *attrValue, uint8_t attrValueLen)
Definition: RadiusLayer.h:89
Definition: RadiusLayer.h:39
size_t getTotalSize() const
Definition: RadiusLayer.h:56
RadiusAttribute(uint8_t *attrRawData)
Definition: RadiusLayer.h:45
size_t getDataSize() const
Definition: RadiusLayer.h:64
virtual ~RadiusAttribute()
Definition: RadiusLayer.h:51
Definition: RadiusLayer.h:167
RadiusAttribute getAttribute(uint8_t attrType) const
bool removeAllAttributes()
void computeCalculateFields()
RadiusAttribute getNextAttribute(RadiusAttribute &attr) const
radius_header * getRadiusHeader() const
Definition: RadiusLayer.h:224
std::string toString() const
RadiusLayer(uint8_t code, uint8_t id, const uint8_t *authenticator, uint8_t authenticatorArrSize)
~RadiusLayer()
Definition: RadiusLayer.h:216
RadiusAttribute getFirstAttribute() const
static bool isDataValid(const uint8_t *udpData, size_t udpDataLen)
RadiusAttribute addAttributeAfter(const RadiusAttributeBuilder &attrBuilder, uint8_t prevAttrType)
void parseNextLayer()
Definition: RadiusLayer.h:331
std::string getAuthenticatorValue() const
void setAuthenticatorValue(const std::string &authValue)
RadiusLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: RadiusLayer.h:186
RadiusAttribute addAttribute(const RadiusAttributeBuilder &attrBuilder)
size_t getAttributeCount() const
OsiModelLayer getOsiModelLayer() const
Definition: RadiusLayer.h:341
bool removeAttribute(uint8_t attrType)
static std::string getRadiusMessageString(uint8_t radiusMessageCode)
RadiusLayer(uint8_t code, uint8_t id, const std::string &authenticator)
size_t getHeaderLen() const
static bool isRadiusPort(uint16_t port)
Definition: RadiusLayer.h:349
Definition: TLVData.h:413
Definition: TLVData.h:23
Definition: TLVData.h:239
The main namespace for the PcapPlusPlus lib.
OsiModelLayer
Definition: ProtocolType.h:354
@ OsiModelApplicationLayer
Definition: ProtocolType.h:368
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